Merge tag 'hardening-v6.2-rc1-fixes' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / drivers / gpu / drm / drm_gem_vram_helper.c
CommitLineData
85438a8d
TZ
1// SPDX-License-Identifier: GPL-2.0-or-later
2
7938f421 3#include <linux/iosys-map.h>
b22b51a0
TZ
4#include <linux/module.h>
5
6b5ce4a1
TZ
6#include <drm/drm_debugfs.h>
7#include <drm/drm_device.h>
0fb5f69e 8#include <drm/drm_drv.h>
6b5ce4a1 9#include <drm/drm_file.h>
6542ad89 10#include <drm/drm_framebuffer.h>
820c1707 11#include <drm/drm_gem_atomic_helper.h>
1c89b4b7 12#include <drm/drm_gem_framebuffer_helper.h>
527f6d91 13#include <drm/drm_gem_ttm_helper.h>
85438a8d 14#include <drm/drm_gem_vram_helper.h>
a5f23a72 15#include <drm/drm_managed.h>
fed1eec0 16#include <drm/drm_mode.h>
6542ad89 17#include <drm/drm_plane.h>
1f460b49 18#include <drm/drm_prime.h>
6542ad89 19#include <drm/drm_simple_kms_helper.h>
85438a8d 20
3eb7d96e
CK
21#include <drm/ttm/ttm_range_manager.h>
22
31070a87
TZ
23static const struct drm_gem_object_funcs drm_gem_vram_object_funcs;
24
85438a8d
TZ
25/**
26 * DOC: overview
27 *
b22b51a0
TZ
28 * This library provides &struct drm_gem_vram_object (GEM VRAM), a GEM
29 * buffer object that is backed by video RAM (VRAM). It can be used for
30 * framebuffer devices with dedicated memory.
6b5ce4a1
TZ
31 *
32 * The data structure &struct drm_vram_mm and its helpers implement a memory
b22b51a0
TZ
33 * manager for simple framebuffer devices with dedicated video memory. GEM
34 * VRAM buffer objects are either placed in the video memory or remain evicted
35 * to system memory.
36 *
37 * With the GEM interface userspace applications create, manage and destroy
38 * graphics buffers, such as an on-screen framebuffer. GEM does not provide
39 * an implementation of these interfaces. It's up to the DRM driver to
40 * provide an implementation that suits the hardware. If the hardware device
41 * contains dedicated video memory, the DRM driver can use the VRAM helper
42 * library. Each active buffer object is stored in video RAM. Active
43 * buffer are used for drawing the current frame, typically something like
44 * the frame's scanout buffer or the cursor image. If there's no more space
45 * left in VRAM, inactive GEM objects can be moved to system memory.
46 *
a5f23a72
TZ
47 * To initialize the VRAM helper library call drmm_vram_helper_alloc_mm().
48 * The function allocates and initializes an instance of &struct drm_vram_mm
49 * in &struct drm_device.vram_mm . Use &DRM_GEM_VRAM_DRIVER to initialize
50 * &struct drm_driver and &DRM_VRAM_MM_FILE_OPERATIONS to initialize
51 * &struct file_operations; as illustrated below.
b22b51a0
TZ
52 *
53 * .. code-block:: c
54 *
55 * struct file_operations fops ={
56 * .owner = THIS_MODULE,
57 * DRM_VRAM_MM_FILE_OPERATION
58 * };
59 * struct drm_driver drv = {
60 * .driver_feature = DRM_ ... ,
61 * .fops = &fops,
62 * DRM_GEM_VRAM_DRIVER
63 * };
64 *
65 * int init_drm_driver()
66 * {
67 * struct drm_device *dev;
68 * uint64_t vram_base;
69 * unsigned long vram_size;
70 * int ret;
71 *
72 * // setup device, vram base and size
73 * // ...
74 *
a5f23a72 75 * ret = drmm_vram_helper_alloc_mm(dev, vram_base, vram_size);
b22b51a0
TZ
76 * if (ret)
77 * return ret;
78 * return 0;
79 * }
80 *
81 * This creates an instance of &struct drm_vram_mm, exports DRM userspace
82 * interfaces for GEM buffer management and initializes file operations to
83 * allow for accessing created GEM buffers. With this setup, the DRM driver
84 * manages an area of video RAM with VRAM MM and provides GEM VRAM objects
85 * to userspace.
86 *
a5f23a72
TZ
87 * You don't have to clean up the instance of VRAM MM.
88 * drmm_vram_helper_alloc_mm() is a managed interface that installs a
89 * clean-up handler to run during the DRM device's release.
b22b51a0 90 *
a5f23a72
TZ
91 * For drawing or scanout operations, rsp. buffer objects have to be pinned
92 * in video RAM. Call drm_gem_vram_pin() with &DRM_GEM_VRAM_PL_FLAG_VRAM or
b22b51a0
TZ
93 * &DRM_GEM_VRAM_PL_FLAG_SYSTEM to pin a buffer object in video RAM or system
94 * memory. Call drm_gem_vram_unpin() to release the pinned object afterwards.
95 *
96 * A buffer object that is pinned in video RAM has a fixed address within that
97 * memory region. Call drm_gem_vram_offset() to retrieve this value. Typically
98 * it's used to program the hardware's scanout engine for framebuffers, set
99 * the cursor overlay's image for a mouse cursor, or use it as input to the
0ae865ef 100 * hardware's drawing engine.
b22b51a0
TZ
101 *
102 * To access a buffer object's memory from the DRM driver, call
d88656f4
TZ
103 * drm_gem_vram_vmap(). It maps the buffer into kernel address
104 * space and returns the memory address. Use drm_gem_vram_vunmap() to
b22b51a0 105 * release the mapping.
85438a8d
TZ
106 */
107
108/*
109 * Buffer-objects helpers
110 */
111
112static void drm_gem_vram_cleanup(struct drm_gem_vram_object *gbo)
113{
114 /* We got here via ttm_bo_put(), which means that the
115 * TTM buffer object in 'bo' has already been cleaned
116 * up; only release the GEM object.
117 */
37a48adf 118
49a3f51d 119 WARN_ON(gbo->vmap_use_count);
7938f421 120 WARN_ON(iosys_map_is_set(&gbo->map));
37a48adf 121
0e580c6d 122 drm_gem_object_release(&gbo->bo.base);
85438a8d
TZ
123}
124
125static void drm_gem_vram_destroy(struct drm_gem_vram_object *gbo)
126{
127 drm_gem_vram_cleanup(gbo);
128 kfree(gbo);
129}
130
131static void ttm_buffer_object_destroy(struct ttm_buffer_object *bo)
132{
133 struct drm_gem_vram_object *gbo = drm_gem_vram_of_bo(bo);
134
135 drm_gem_vram_destroy(gbo);
136}
137
138static void drm_gem_vram_placement(struct drm_gem_vram_object *gbo,
139 unsigned long pl_flag)
140{
7053e0ea 141 u32 invariant_flags = 0;
85438a8d
TZ
142 unsigned int i;
143 unsigned int c = 0;
7053e0ea
CK
144
145 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_TOPDOWN)
b8f8dbf6 146 invariant_flags = TTM_PL_FLAG_TOPDOWN;
85438a8d
TZ
147
148 gbo->placement.placement = gbo->placements;
149 gbo->placement.busy_placement = gbo->placements;
150
48e07c23
CK
151 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_VRAM) {
152 gbo->placements[c].mem_type = TTM_PL_VRAM;
ce65b874 153 gbo->placements[c++].flags = invariant_flags;
48e07c23 154 }
85438a8d 155
48e07c23
CK
156 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_SYSTEM || !c) {
157 gbo->placements[c].mem_type = TTM_PL_SYSTEM;
ce65b874 158 gbo->placements[c++].flags = invariant_flags;
48e07c23 159 }
85438a8d
TZ
160
161 gbo->placement.num_placement = c;
162 gbo->placement.num_busy_placement = c;
163
164 for (i = 0; i < c; ++i) {
165 gbo->placements[i].fpfn = 0;
166 gbo->placements[i].lpfn = 0;
167 }
168}
169
85438a8d
TZ
170/**
171 * drm_gem_vram_create() - Creates a VRAM-backed GEM object
172 * @dev: the DRM device
85438a8d
TZ
173 * @size: the buffer size in bytes
174 * @pg_align: the buffer's alignment in multiples of the page size
85438a8d 175 *
4d92d7d7
TZ
176 * GEM objects are allocated by calling struct drm_driver.gem_create_object,
177 * if set. Otherwise kzalloc() will be used. Drivers can set their own GEM
178 * object functions in struct drm_driver.gem_create_object. If no functions
179 * are set, the new GEM object will use the default functions from GEM VRAM
180 * helpers.
181 *
85438a8d
TZ
182 * Returns:
183 * A new instance of &struct drm_gem_vram_object on success, or
184 * an ERR_PTR()-encoded error code otherwise.
185 */
186struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
85438a8d 187 size_t size,
ebe9428b 188 unsigned long pg_align)
85438a8d
TZ
189{
190 struct drm_gem_vram_object *gbo;
4d92d7d7 191 struct drm_gem_object *gem;
7faa92df 192 struct drm_vram_mm *vmm = dev->vram_mm;
8af8a109 193 struct ttm_device *bdev;
85438a8d 194 int ret;
7faa92df
TZ
195
196 if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
197 return ERR_PTR(-EINVAL);
85438a8d 198
0fb5f69e 199 if (dev->driver->gem_create_object) {
4d92d7d7 200 gem = dev->driver->gem_create_object(dev, size);
4ff22f48
TZ
201 if (IS_ERR(gem))
202 return ERR_CAST(gem);
0fb5f69e
TZ
203 gbo = drm_gem_vram_of_gem(gem);
204 } else {
205 gbo = kzalloc(sizeof(*gbo), GFP_KERNEL);
206 if (!gbo)
207 return ERR_PTR(-ENOMEM);
4d92d7d7 208 gem = &gbo->bo.base;
0fb5f69e 209 }
85438a8d 210
4d92d7d7
TZ
211 if (!gem->funcs)
212 gem->funcs = &drm_gem_vram_object_funcs;
7faa92df 213
4d92d7d7 214 ret = drm_gem_object_init(dev, gem, size);
7faa92df
TZ
215 if (ret) {
216 kfree(gbo);
217 return ERR_PTR(ret);
218 }
219
220 bdev = &vmm->bdev;
7faa92df
TZ
221
222 gbo->bo.bdev = bdev;
8bde6c0d 223 drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
7faa92df
TZ
224
225 /*
226 * A failing ttm_bo_init will call ttm_buffer_object_destroy
227 * to release gbo->bo.base and kfree gbo.
228 */
347987a2
CK
229 ret = ttm_bo_init_validate(bdev, &gbo->bo, ttm_bo_type_device,
230 &gbo->placement, pg_align, false, NULL, NULL,
231 ttm_buffer_object_destroy);
7faa92df 232 if (ret)
da62cb72 233 return ERR_PTR(ret);
85438a8d
TZ
234
235 return gbo;
85438a8d
TZ
236}
237EXPORT_SYMBOL(drm_gem_vram_create);
238
239/**
240 * drm_gem_vram_put() - Releases a reference to a VRAM-backed GEM object
241 * @gbo: the GEM VRAM object
242 *
243 * See ttm_bo_put() for more information.
244 */
245void drm_gem_vram_put(struct drm_gem_vram_object *gbo)
246{
247 ttm_bo_put(&gbo->bo);
248}
249EXPORT_SYMBOL(drm_gem_vram_put);
250
46642a7d
ND
251static u64 drm_gem_vram_pg_offset(struct drm_gem_vram_object *gbo)
252{
253 /* Keep TTM behavior for now, remove when drivers are audited */
cb1c8146
CK
254 if (WARN_ON_ONCE(!gbo->bo.resource ||
255 gbo->bo.resource->mem_type == TTM_PL_SYSTEM))
46642a7d
ND
256 return 0;
257
d3116756 258 return gbo->bo.resource->start;
46642a7d
ND
259}
260
85438a8d
TZ
261/**
262 * drm_gem_vram_offset() - \
263 Returns a GEM VRAM object's offset in video memory
264 * @gbo: the GEM VRAM object
265 *
266 * This function returns the buffer object's offset in the device's video
267 * memory. The buffer object has to be pinned to %TTM_PL_VRAM.
268 *
269 * Returns:
270 * The buffer object's offset in video memory on success, or
271 * a negative errno code otherwise.
272 */
273s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo)
274{
d582723d 275 if (WARN_ON_ONCE(!gbo->bo.pin_count))
85438a8d 276 return (s64)-ENODEV;
46642a7d 277 return drm_gem_vram_pg_offset(gbo) << PAGE_SHIFT;
85438a8d
TZ
278}
279EXPORT_SYMBOL(drm_gem_vram_offset);
280
bc25bb91
TZ
281static int drm_gem_vram_pin_locked(struct drm_gem_vram_object *gbo,
282 unsigned long pl_flag)
85438a8d 283{
85438a8d 284 struct ttm_operation_ctx ctx = { false, false };
d582723d 285 int ret;
85438a8d 286
d582723d 287 if (gbo->bo.pin_count)
5b24f715 288 goto out;
85438a8d 289
a6c3464f
TZ
290 if (pl_flag)
291 drm_gem_vram_placement(gbo, pl_flag);
292
85438a8d
TZ
293 ret = ttm_bo_validate(&gbo->bo, &gbo->placement, &ctx);
294 if (ret < 0)
bc25bb91 295 return ret;
85438a8d 296
5b24f715 297out:
d582723d 298 ttm_bo_pin(&gbo->bo);
85438a8d
TZ
299
300 return 0;
301}
85438a8d
TZ
302
303/**
bc25bb91 304 * drm_gem_vram_pin() - Pins a GEM VRAM object in a region.
85438a8d 305 * @gbo: the GEM VRAM object
bc25bb91
TZ
306 * @pl_flag: a bitmask of possible memory regions
307 *
308 * Pinning a buffer object ensures that it is not evicted from
309 * a memory region. A pinned buffer object has to be unpinned before
310 * it can be pinned to another region. If the pl_flag argument is 0,
311 * the buffer is pinned at its current location (video RAM or system
312 * memory).
85438a8d 313 *
245f44e7
TZ
314 * Small buffer objects, such as cursor images, can lead to memory
315 * fragmentation if they are pinned in the middle of video RAM. This
316 * is especially a problem on devices with only a small amount of
317 * video RAM. Fragmentation can prevent the primary framebuffer from
318 * fitting in, even though there's enough memory overall. The modifier
319 * DRM_GEM_VRAM_PL_FLAG_TOPDOWN marks the buffer object to be pinned
320 * at the high end of the memory region to avoid fragmentation.
321 *
85438a8d
TZ
322 * Returns:
323 * 0 on success, or
324 * a negative error code otherwise.
325 */
bc25bb91 326int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag)
85438a8d 327{
bc25bb91 328 int ret;
85438a8d 329
5b24f715 330 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
bc25bb91 331 if (ret)
5b24f715 332 return ret;
bc25bb91
TZ
333 ret = drm_gem_vram_pin_locked(gbo, pl_flag);
334 ttm_bo_unreserve(&gbo->bo);
335
336 return ret;
337}
338EXPORT_SYMBOL(drm_gem_vram_pin);
339
d582723d 340static void drm_gem_vram_unpin_locked(struct drm_gem_vram_object *gbo)
bc25bb91 341{
d582723d 342 ttm_bo_unpin(&gbo->bo);
bc25bb91 343}
5b24f715 344
bc25bb91
TZ
345/**
346 * drm_gem_vram_unpin() - Unpins a GEM VRAM object
347 * @gbo: the GEM VRAM object
348 *
349 * Returns:
350 * 0 on success, or
351 * a negative error code otherwise.
352 */
353int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo)
354{
355 int ret;
356
357 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
358 if (ret)
359 return ret;
d582723d
CK
360
361 drm_gem_vram_unpin_locked(gbo);
5b24f715 362 ttm_bo_unreserve(&gbo->bo);
bc25bb91 363
d582723d 364 return 0;
85438a8d
TZ
365}
366EXPORT_SYMBOL(drm_gem_vram_unpin);
367
49a3f51d 368static int drm_gem_vram_kmap_locked(struct drm_gem_vram_object *gbo,
7938f421 369 struct iosys_map *map)
37a48adf
TZ
370{
371 int ret;
37a48adf 372
49a3f51d 373 if (gbo->vmap_use_count > 0)
37a48adf
TZ
374 goto out;
375
abbc4d6e
TZ
376 /*
377 * VRAM helpers unmap the BO only on demand. So the previous
378 * page mapping might still be around. Only vmap if the there's
379 * no mapping present.
380 */
7938f421 381 if (iosys_map_is_null(&gbo->map)) {
abbc4d6e
TZ
382 ret = ttm_bo_vmap(&gbo->bo, &gbo->map);
383 if (ret)
384 return ret;
385 }
37a48adf
TZ
386
387out:
49a3f51d
TZ
388 ++gbo->vmap_use_count;
389 *map = gbo->map;
390
391 return 0;
37a48adf
TZ
392}
393
49a3f51d 394static void drm_gem_vram_kunmap_locked(struct drm_gem_vram_object *gbo,
7938f421 395 struct iosys_map *map)
85438a8d 396{
49a3f51d
TZ
397 struct drm_device *dev = gbo->bo.base.dev;
398
399 if (drm_WARN_ON_ONCE(dev, !gbo->vmap_use_count))
37a48adf 400 return;
49a3f51d 401
7938f421 402 if (drm_WARN_ON_ONCE(dev, !iosys_map_is_equal(&gbo->map, map)))
49a3f51d
TZ
403 return; /* BUG: map not mapped from this BO */
404
405 if (--gbo->vmap_use_count > 0)
37a48adf
TZ
406 return;
407
2236439b
TZ
408 /*
409 * Permanently mapping and unmapping buffers adds overhead from
410 * updating the page tables and creates debugging output. Therefore,
411 * we delay the actual unmap operation until the BO gets evicted
412 * from memory. See drm_gem_vram_bo_driver_move_notify().
413 */
85438a8d 414}
37a48adf 415
c8908bde
TZ
416/**
417 * drm_gem_vram_vmap() - Pins and maps a GEM VRAM object into kernel address
418 * space
49a3f51d
TZ
419 * @gbo: The GEM VRAM object to map
420 * @map: Returns the kernel virtual address of the VRAM GEM object's backing
421 * store.
c8908bde
TZ
422 *
423 * The vmap function pins a GEM VRAM object to its current location, either
424 * system or video memory, and maps its buffer into kernel address space.
425 * As pinned object cannot be relocated, you should avoid pinning objects
426 * permanently. Call drm_gem_vram_vunmap() with the returned address to
427 * unmap and unpin the GEM VRAM object.
428 *
c8908bde 429 * Returns:
49a3f51d 430 * 0 on success, or a negative error code otherwise.
c8908bde 431 */
7938f421 432int drm_gem_vram_vmap(struct drm_gem_vram_object *gbo, struct iosys_map *map)
c8908bde
TZ
433{
434 int ret;
c8908bde 435
6d0bfef7 436 dma_resv_assert_held(gbo->bo.base.resv);
c8908bde
TZ
437
438 ret = drm_gem_vram_pin_locked(gbo, 0);
439 if (ret)
6d0bfef7 440 return ret;
49a3f51d
TZ
441 ret = drm_gem_vram_kmap_locked(gbo, map);
442 if (ret)
c8908bde 443 goto err_drm_gem_vram_unpin_locked;
c8908bde 444
49a3f51d 445 return 0;
c8908bde
TZ
446
447err_drm_gem_vram_unpin_locked:
448 drm_gem_vram_unpin_locked(gbo);
49a3f51d 449 return ret;
c8908bde
TZ
450}
451EXPORT_SYMBOL(drm_gem_vram_vmap);
452
453/**
454 * drm_gem_vram_vunmap() - Unmaps and unpins a GEM VRAM object
49a3f51d
TZ
455 * @gbo: The GEM VRAM object to unmap
456 * @map: Kernel virtual address where the VRAM GEM object was mapped
c8908bde
TZ
457 *
458 * A call to drm_gem_vram_vunmap() unmaps and unpins a GEM VRAM buffer. See
459 * the documentation for drm_gem_vram_vmap() for more information.
460 */
7938f421
LDM
461void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo,
462 struct iosys_map *map)
c8908bde 463{
6d0bfef7 464 dma_resv_assert_held(gbo->bo.base.resv);
c8908bde 465
49a3f51d 466 drm_gem_vram_kunmap_locked(gbo, map);
c8908bde 467 drm_gem_vram_unpin_locked(gbo);
c8908bde
TZ
468}
469EXPORT_SYMBOL(drm_gem_vram_vunmap);
470
fed1eec0
TZ
471/**
472 * drm_gem_vram_fill_create_dumb() - \
473 Helper for implementing &struct drm_driver.dumb_create
474 * @file: the DRM file
475 * @dev: the DRM device
fed1eec0 476 * @pg_align: the buffer's alignment in multiples of the page size
98707327 477 * @pitch_align: the scanline's alignment in powers of 2
fed1eec0
TZ
478 * @args: the arguments as provided to \
479 &struct drm_driver.dumb_create
480 *
481 * This helper function fills &struct drm_mode_create_dumb, which is used
482 * by &struct drm_driver.dumb_create. Implementations of this interface
483 * should forwards their arguments to this helper, plus the driver-specific
484 * parameters.
485 *
486 * Returns:
487 * 0 on success, or
488 * a negative error code otherwise.
489 */
490int drm_gem_vram_fill_create_dumb(struct drm_file *file,
491 struct drm_device *dev,
fed1eec0 492 unsigned long pg_align,
98707327 493 unsigned long pitch_align,
fed1eec0
TZ
494 struct drm_mode_create_dumb *args)
495{
496 size_t pitch, size;
497 struct drm_gem_vram_object *gbo;
498 int ret;
499 u32 handle;
500
98707327
TZ
501 pitch = args->width * DIV_ROUND_UP(args->bpp, 8);
502 if (pitch_align) {
503 if (WARN_ON_ONCE(!is_power_of_2(pitch_align)))
504 return -EINVAL;
505 pitch = ALIGN(pitch, pitch_align);
506 }
fed1eec0
TZ
507 size = pitch * args->height;
508
509 size = roundup(size, PAGE_SIZE);
510 if (!size)
511 return -EINVAL;
512
a4d46a8e 513 gbo = drm_gem_vram_create(dev, size, pg_align);
fed1eec0
TZ
514 if (IS_ERR(gbo))
515 return PTR_ERR(gbo);
516
0e580c6d 517 ret = drm_gem_handle_create(file, &gbo->bo.base, &handle);
fed1eec0 518 if (ret)
be6ee102 519 goto err_drm_gem_object_put;
fed1eec0 520
be6ee102 521 drm_gem_object_put(&gbo->bo.base);
fed1eec0
TZ
522
523 args->pitch = pitch;
524 args->size = size;
525 args->handle = handle;
526
527 return 0;
528
be6ee102
EV
529err_drm_gem_object_put:
530 drm_gem_object_put(&gbo->bo.base);
fed1eec0
TZ
531 return ret;
532}
533EXPORT_SYMBOL(drm_gem_vram_fill_create_dumb);
534
6c812bc5 535/*
8af8a109 536 * Helpers for struct ttm_device_funcs
6c812bc5
TZ
537 */
538
539static bool drm_is_gem_vram(struct ttm_buffer_object *bo)
540{
541 return (bo->destroy == ttm_buffer_object_destroy);
542}
543
b0e40e08
TZ
544static void drm_gem_vram_bo_driver_evict_flags(struct drm_gem_vram_object *gbo,
545 struct ttm_placement *pl)
6c812bc5 546{
7053e0ea 547 drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
6c812bc5
TZ
548 *pl = gbo->placement;
549}
6c812bc5 550
10073770 551static void drm_gem_vram_bo_driver_move_notify(struct drm_gem_vram_object *gbo)
2236439b 552{
49a3f51d
TZ
553 struct ttm_buffer_object *bo = &gbo->bo;
554 struct drm_device *dev = bo->base.dev;
2236439b 555
49a3f51d 556 if (drm_WARN_ON_ONCE(dev, gbo->vmap_use_count))
2236439b
TZ
557 return;
558
49a3f51d 559 ttm_bo_vunmap(bo, &gbo->map);
7938f421 560 iosys_map_clear(&gbo->map); /* explicitly clear mapping for next vmap call */
2236439b 561}
5c9dcacf 562
2b8283ff
DA
563static int drm_gem_vram_bo_driver_move(struct drm_gem_vram_object *gbo,
564 bool evict,
565 struct ttm_operation_ctx *ctx,
566 struct ttm_resource *new_mem)
567{
10073770
CK
568 drm_gem_vram_bo_driver_move_notify(gbo);
569 return ttm_bo_move_memcpy(&gbo->bo, ctx, new_mem);
2b8283ff
DA
570}
571
737000fd 572/*
0ccf52ba 573 * Helpers for struct drm_gem_object_funcs
737000fd
TZ
574 */
575
576/**
0ccf52ba
TZ
577 * drm_gem_vram_object_free() - \
578 Implements &struct drm_gem_object_funcs.free
579 * @gem: GEM object. Refers to &struct drm_gem_vram_object.gem
737000fd 580 */
0ccf52ba 581static void drm_gem_vram_object_free(struct drm_gem_object *gem)
737000fd
TZ
582{
583 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
584
585 drm_gem_vram_put(gbo);
586}
0ccf52ba
TZ
587
588/*
589 * Helpers for dump buffers
590 */
737000fd 591
59f5989a 592/**
e9d2871f 593 * drm_gem_vram_driver_dumb_create() - \
59f5989a
TZ
594 Implements &struct drm_driver.dumb_create
595 * @file: the DRM file
596 * @dev: the DRM device
597 * @args: the arguments as provided to \
598 &struct drm_driver.dumb_create
599 *
600 * This function requires the driver to use @drm_device.vram_mm for its
601 * instance of VRAM MM.
602 *
603 * Returns:
604 * 0 on success, or
605 * a negative error code otherwise.
606 */
607int drm_gem_vram_driver_dumb_create(struct drm_file *file,
608 struct drm_device *dev,
609 struct drm_mode_create_dumb *args)
610{
611 if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized"))
612 return -EINVAL;
613
a4d46a8e 614 return drm_gem_vram_fill_create_dumb(file, dev, 0, 0, args);
59f5989a
TZ
615}
616EXPORT_SYMBOL(drm_gem_vram_driver_dumb_create);
617
6542ad89
TZ
618/*
619 * Helpers for struct drm_plane_helper_funcs
620 */
621
1c89b4b7
TZ
622static void __drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
623 struct drm_plane_state *state,
624 unsigned int num_planes)
625{
626 struct drm_gem_object *obj;
627 struct drm_gem_vram_object *gbo;
628 struct drm_framebuffer *fb = state->fb;
629
630 while (num_planes) {
631 --num_planes;
632 obj = drm_gem_fb_get_obj(fb, num_planes);
633 if (!obj)
634 continue;
635 gbo = drm_gem_vram_of_gem(obj);
636 drm_gem_vram_unpin(gbo);
637 }
638}
639
6542ad89
TZ
640/**
641 * drm_gem_vram_plane_helper_prepare_fb() - \
642 * Implements &struct drm_plane_helper_funcs.prepare_fb
643 * @plane: a DRM plane
644 * @new_state: the plane's new state
645 *
d7b001d8
TZ
646 * During plane updates, this function sets the plane's fence and
647 * pins the GEM VRAM objects of the plane's new framebuffer to VRAM.
648 * Call drm_gem_vram_plane_helper_cleanup_fb() to unpin them.
6542ad89
TZ
649 *
650 * Returns:
651 * 0 on success, or
652 * a negative errno code otherwise.
653 */
654int
655drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane,
656 struct drm_plane_state *new_state)
657{
a8d6e127 658 struct drm_framebuffer *fb = new_state->fb;
6542ad89 659 struct drm_gem_vram_object *gbo;
a8d6e127
TZ
660 struct drm_gem_object *obj;
661 unsigned int i;
6542ad89
TZ
662 int ret;
663
a8d6e127 664 if (!fb)
6542ad89
TZ
665 return 0;
666
a8d6e127
TZ
667 for (i = 0; i < fb->format->num_planes; ++i) {
668 obj = drm_gem_fb_get_obj(fb, i);
669 if (!obj) {
670 ret = -EINVAL;
671 goto err_drm_gem_vram_unpin;
672 }
673 gbo = drm_gem_vram_of_gem(obj);
6542ad89
TZ
674 ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
675 if (ret)
676 goto err_drm_gem_vram_unpin;
677 }
678
820c1707 679 ret = drm_gem_plane_helper_prepare_fb(plane, new_state);
d7b001d8
TZ
680 if (ret)
681 goto err_drm_gem_vram_unpin;
682
6542ad89
TZ
683 return 0;
684
685err_drm_gem_vram_unpin:
1c89b4b7 686 __drm_gem_vram_plane_helper_cleanup_fb(plane, new_state, i);
6542ad89
TZ
687 return ret;
688}
689EXPORT_SYMBOL(drm_gem_vram_plane_helper_prepare_fb);
690
691/**
692 * drm_gem_vram_plane_helper_cleanup_fb() - \
693 * Implements &struct drm_plane_helper_funcs.cleanup_fb
694 * @plane: a DRM plane
695 * @old_state: the plane's old state
696 *
697 * During plane updates, this function unpins the GEM VRAM
698 * objects of the plane's old framebuffer from VRAM. Complements
699 * drm_gem_vram_plane_helper_prepare_fb().
700 */
701void
702drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
703 struct drm_plane_state *old_state)
704{
1c89b4b7 705 struct drm_framebuffer *fb = old_state->fb;
6542ad89 706
1c89b4b7 707 if (!fb)
6542ad89
TZ
708 return;
709
a8d6e127 710 __drm_gem_vram_plane_helper_cleanup_fb(plane, old_state, fb->format->num_planes);
6542ad89
TZ
711}
712EXPORT_SYMBOL(drm_gem_vram_plane_helper_cleanup_fb);
713
714/*
715 * Helpers for struct drm_simple_display_pipe_funcs
716 */
717
718/**
719 * drm_gem_vram_simple_display_pipe_prepare_fb() - \
720 * Implements &struct drm_simple_display_pipe_funcs.prepare_fb
721 * @pipe: a simple display pipe
722 * @new_state: the plane's new state
723 *
724 * During plane updates, this function pins the GEM VRAM
725 * objects of the plane's new framebuffer to VRAM. Call
726 * drm_gem_vram_simple_display_pipe_cleanup_fb() to unpin them.
727 *
728 * Returns:
729 * 0 on success, or
730 * a negative errno code otherwise.
731 */
732int drm_gem_vram_simple_display_pipe_prepare_fb(
733 struct drm_simple_display_pipe *pipe,
734 struct drm_plane_state *new_state)
735{
736 return drm_gem_vram_plane_helper_prepare_fb(&pipe->plane, new_state);
737}
738EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_prepare_fb);
739
740/**
741 * drm_gem_vram_simple_display_pipe_cleanup_fb() - \
742 * Implements &struct drm_simple_display_pipe_funcs.cleanup_fb
743 * @pipe: a simple display pipe
744 * @old_state: the plane's old state
745 *
746 * During plane updates, this function unpins the GEM VRAM
747 * objects of the plane's old framebuffer from VRAM. Complements
748 * drm_gem_vram_simple_display_pipe_prepare_fb().
749 */
750void drm_gem_vram_simple_display_pipe_cleanup_fb(
751 struct drm_simple_display_pipe *pipe,
752 struct drm_plane_state *old_state)
753{
754 drm_gem_vram_plane_helper_cleanup_fb(&pipe->plane, old_state);
755}
756EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_cleanup_fb);
757
1f460b49 758/*
0ccf52ba 759 * PRIME helpers
1f460b49
TZ
760 */
761
762/**
0ccf52ba
TZ
763 * drm_gem_vram_object_pin() - \
764 Implements &struct drm_gem_object_funcs.pin
1f460b49
TZ
765 * @gem: The GEM object to pin
766 *
767 * Returns:
768 * 0 on success, or
769 * a negative errno code otherwise.
770 */
0ccf52ba 771static int drm_gem_vram_object_pin(struct drm_gem_object *gem)
1f460b49
TZ
772{
773 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
774
a6c3464f
TZ
775 /* Fbdev console emulation is the use case of these PRIME
776 * helpers. This may involve updating a hardware buffer from
777 * a shadow FB. We pin the buffer to it's current location
778 * (either video RAM or system memory) to prevent it from
779 * being relocated during the update operation. If you require
780 * the buffer to be pinned to VRAM, implement a callback that
781 * sets the flags accordingly.
782 */
783 return drm_gem_vram_pin(gbo, 0);
1f460b49 784}
1f460b49
TZ
785
786/**
0ccf52ba
TZ
787 * drm_gem_vram_object_unpin() - \
788 Implements &struct drm_gem_object_funcs.unpin
1f460b49
TZ
789 * @gem: The GEM object to unpin
790 */
0ccf52ba 791static void drm_gem_vram_object_unpin(struct drm_gem_object *gem)
1f460b49
TZ
792{
793 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
794
795 drm_gem_vram_unpin(gbo);
796}
1f460b49
TZ
797
798/**
49a3f51d
TZ
799 * drm_gem_vram_object_vmap() -
800 * Implements &struct drm_gem_object_funcs.vmap
801 * @gem: The GEM object to map
802 * @map: Returns the kernel virtual address of the VRAM GEM object's backing
803 * store.
1f460b49
TZ
804 *
805 * Returns:
49a3f51d 806 * 0 on success, or a negative error code otherwise.
1f460b49 807 */
7938f421
LDM
808static int drm_gem_vram_object_vmap(struct drm_gem_object *gem,
809 struct iosys_map *map)
1f460b49
TZ
810{
811 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
1f460b49 812
49a3f51d 813 return drm_gem_vram_vmap(gbo, map);
1f460b49 814}
1f460b49
TZ
815
816/**
49a3f51d
TZ
817 * drm_gem_vram_object_vunmap() -
818 * Implements &struct drm_gem_object_funcs.vunmap
819 * @gem: The GEM object to unmap
820 * @map: Kernel virtual address where the VRAM GEM object was mapped
1f460b49 821 */
7938f421
LDM
822static void drm_gem_vram_object_vunmap(struct drm_gem_object *gem,
823 struct iosys_map *map)
1f460b49
TZ
824{
825 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
bc25bb91 826
49a3f51d 827 drm_gem_vram_vunmap(gbo, map);
1f460b49 828}
31070a87
TZ
829
830/*
831 * GEM object funcs
832 */
833
834static const struct drm_gem_object_funcs drm_gem_vram_object_funcs = {
0ccf52ba
TZ
835 .free = drm_gem_vram_object_free,
836 .pin = drm_gem_vram_object_pin,
837 .unpin = drm_gem_vram_object_unpin,
838 .vmap = drm_gem_vram_object_vmap,
527f6d91 839 .vunmap = drm_gem_vram_object_vunmap,
5a8b7cf9 840 .mmap = drm_gem_ttm_mmap,
527f6d91 841 .print_info = drm_gem_ttm_print_info,
31070a87 842};
6b5ce4a1
TZ
843
844/*
845 * VRAM memory manager
846 */
847
848/*
849 * TTM TT
850 */
851
8af8a109 852static void bo_driver_ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *tt)
6b5ce4a1
TZ
853{
854 ttm_tt_fini(tt);
855 kfree(tt);
856}
857
6b5ce4a1
TZ
858/*
859 * TTM BO device
860 */
861
862static struct ttm_tt *bo_driver_ttm_tt_create(struct ttm_buffer_object *bo,
863 uint32_t page_flags)
864{
865 struct ttm_tt *tt;
866 int ret;
867
868 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
869 if (!tt)
870 return NULL;
871
e36764ec 872 ret = ttm_tt_init(tt, bo, page_flags, ttm_cached, 0);
6b5ce4a1
TZ
873 if (ret < 0)
874 goto err_ttm_tt_init;
875
876 return tt;
877
878err_ttm_tt_init:
879 kfree(tt);
880 return NULL;
881}
882
6b5ce4a1
TZ
883static void bo_driver_evict_flags(struct ttm_buffer_object *bo,
884 struct ttm_placement *placement)
885{
b0e40e08 886 struct drm_gem_vram_object *gbo;
6b5ce4a1 887
b0e40e08
TZ
888 /* TTM may pass BOs that are not GEM VRAM BOs. */
889 if (!drm_is_gem_vram(bo))
890 return;
891
892 gbo = drm_gem_vram_of_bo(bo);
893
894 drm_gem_vram_bo_driver_evict_flags(gbo, placement);
6b5ce4a1
TZ
895}
896
6a6e5988 897static void bo_driver_delete_mem_notify(struct ttm_buffer_object *bo)
6b5ce4a1 898{
b0e40e08 899 struct drm_gem_vram_object *gbo;
6b5ce4a1 900
b0e40e08
TZ
901 /* TTM may pass BOs that are not GEM VRAM BOs. */
902 if (!drm_is_gem_vram(bo))
6b5ce4a1 903 return;
b0e40e08
TZ
904
905 gbo = drm_gem_vram_of_bo(bo);
906
10073770 907 drm_gem_vram_bo_driver_move_notify(gbo);
6b5ce4a1
TZ
908}
909
2b8283ff
DA
910static int bo_driver_move(struct ttm_buffer_object *bo,
911 bool evict,
912 struct ttm_operation_ctx *ctx,
ebdf5651
DA
913 struct ttm_resource *new_mem,
914 struct ttm_place *hop)
2b8283ff
DA
915{
916 struct drm_gem_vram_object *gbo;
917
918 gbo = drm_gem_vram_of_bo(bo);
919
920 return drm_gem_vram_bo_driver_move(gbo, evict, ctx, new_mem);
921}
922
8af8a109 923static int bo_driver_io_mem_reserve(struct ttm_device *bdev,
2966141a 924 struct ttm_resource *mem)
6b5ce4a1 925{
6b5ce4a1
TZ
926 struct drm_vram_mm *vmm = drm_vram_mm_of_bdev(bdev);
927
6b5ce4a1
TZ
928 switch (mem->mem_type) {
929 case TTM_PL_SYSTEM: /* nothing to do */
6b5ce4a1
TZ
930 break;
931 case TTM_PL_VRAM:
54d04ea8 932 mem->bus.offset = (mem->start << PAGE_SHIFT) + vmm->vram_base;
6b5ce4a1 933 mem->bus.is_iomem = true;
1cf65c45 934 mem->bus.caching = ttm_write_combined;
6b5ce4a1
TZ
935 break;
936 default:
937 return -EINVAL;
938 }
939
940 return 0;
941}
942
8af8a109 943static struct ttm_device_funcs bo_driver = {
6b5ce4a1 944 .ttm_tt_create = bo_driver_ttm_tt_create,
84693830 945 .ttm_tt_destroy = bo_driver_ttm_tt_destroy,
6b5ce4a1
TZ
946 .eviction_valuable = ttm_bo_eviction_valuable,
947 .evict_flags = bo_driver_evict_flags,
2b8283ff 948 .move = bo_driver_move,
6a6e5988 949 .delete_mem_notify = bo_driver_delete_mem_notify,
6b5ce4a1 950 .io_mem_reserve = bo_driver_io_mem_reserve,
6b5ce4a1
TZ
951};
952
953/*
954 * struct drm_vram_mm
955 */
956
6b5ce4a1
TZ
957static int drm_vram_mm_debugfs(struct seq_file *m, void *data)
958{
959 struct drm_info_node *node = (struct drm_info_node *) m->private;
960 struct drm_vram_mm *vmm = node->minor->dev->vram_mm;
9de59bc2 961 struct ttm_resource_manager *man = ttm_manager_type(&vmm->bdev, TTM_PL_VRAM);
6b5ce4a1
TZ
962 struct drm_printer p = drm_seq_file_printer(m);
963
9de59bc2 964 ttm_resource_manager_debug(man, &p);
6b5ce4a1
TZ
965 return 0;
966}
967
968static const struct drm_info_list drm_vram_mm_debugfs_list[] = {
969 { "vram-mm", drm_vram_mm_debugfs, 0, NULL },
970};
6b5ce4a1
TZ
971
972/**
973 * drm_vram_mm_debugfs_init() - Register VRAM MM debugfs file.
974 *
975 * @minor: drm minor device.
976 *
6b5ce4a1 977 */
7ce84471 978void drm_vram_mm_debugfs_init(struct drm_minor *minor)
6b5ce4a1 979{
3a748157
WK
980 drm_debugfs_create_files(drm_vram_mm_debugfs_list,
981 ARRAY_SIZE(drm_vram_mm_debugfs_list),
982 minor->debugfs_root, minor);
6b5ce4a1
TZ
983}
984EXPORT_SYMBOL(drm_vram_mm_debugfs_init);
985
c30b225d
TZ
986static int drm_vram_mm_init(struct drm_vram_mm *vmm, struct drm_device *dev,
987 uint64_t vram_base, size_t vram_size)
6b5ce4a1
TZ
988{
989 int ret;
990
991 vmm->vram_base = vram_base;
992 vmm->vram_size = vram_size;
6b5ce4a1 993
8af8a109 994 ret = ttm_device_init(&vmm->bdev, &bo_driver, dev->dev,
6b5ce4a1
TZ
995 dev->anon_inode->i_mapping,
996 dev->vma_offset_manager,
ee5d2a8e 997 false, true);
6b5ce4a1
TZ
998 if (ret)
999 return ret;
1000
37205891 1001 ret = ttm_range_man_init(&vmm->bdev, TTM_PL_VRAM,
0fe438ce 1002 false, vram_size >> PAGE_SHIFT);
6b5ce4a1
TZ
1003 if (ret)
1004 return ret;
1005
1006 return 0;
1007}
6b5ce4a1 1008
c30b225d 1009static void drm_vram_mm_cleanup(struct drm_vram_mm *vmm)
6b5ce4a1 1010{
37205891 1011 ttm_range_man_fini(&vmm->bdev, TTM_PL_VRAM);
8af8a109 1012 ttm_device_fini(&vmm->bdev);
6b5ce4a1 1013}
6b5ce4a1 1014
6b5ce4a1
TZ
1015/*
1016 * Helpers for integration with struct drm_device
1017 */
1018
9aa02674
TZ
1019static struct drm_vram_mm *drm_vram_helper_alloc_mm(struct drm_device *dev, uint64_t vram_base,
1020 size_t vram_size)
6b5ce4a1
TZ
1021{
1022 int ret;
1023
1024 if (WARN_ON(dev->vram_mm))
1025 return dev->vram_mm;
1026
1027 dev->vram_mm = kzalloc(sizeof(*dev->vram_mm), GFP_KERNEL);
1028 if (!dev->vram_mm)
1029 return ERR_PTR(-ENOMEM);
1030
b0e40e08 1031 ret = drm_vram_mm_init(dev->vram_mm, dev, vram_base, vram_size);
6b5ce4a1
TZ
1032 if (ret)
1033 goto err_kfree;
1034
1035 return dev->vram_mm;
1036
1037err_kfree:
1038 kfree(dev->vram_mm);
1039 dev->vram_mm = NULL;
1040 return ERR_PTR(ret);
1041}
6b5ce4a1 1042
9aa02674 1043static void drm_vram_helper_release_mm(struct drm_device *dev)
6b5ce4a1
TZ
1044{
1045 if (!dev->vram_mm)
1046 return;
1047
1048 drm_vram_mm_cleanup(dev->vram_mm);
1049 kfree(dev->vram_mm);
1050 dev->vram_mm = NULL;
1051}
80f7c3f7 1052
a5f23a72
TZ
1053static void drm_vram_mm_release(struct drm_device *dev, void *ptr)
1054{
1055 drm_vram_helper_release_mm(dev);
1056}
1057
1058/**
1059 * drmm_vram_helper_init - Initializes a device's instance of
1060 * &struct drm_vram_mm
1061 * @dev: the DRM device
1062 * @vram_base: the base address of the video memory
1063 * @vram_size: the size of the video memory in bytes
1064 *
1065 * Creates a new instance of &struct drm_vram_mm and stores it in
1066 * struct &drm_device.vram_mm. The instance is auto-managed and cleaned
1067 * up as part of device cleanup. Calling this function multiple times
1068 * will generate an error message.
1069 *
1070 * Returns:
1071 * 0 on success, or a negative errno code otherwise.
1072 */
1073int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base,
1074 size_t vram_size)
1075{
1076 struct drm_vram_mm *vram_mm;
1077
1078 if (drm_WARN_ON_ONCE(dev, dev->vram_mm))
1079 return 0;
1080
1081 vram_mm = drm_vram_helper_alloc_mm(dev, vram_base, vram_size);
1082 if (IS_ERR(vram_mm))
1083 return PTR_ERR(vram_mm);
1084 return drmm_add_action_or_reset(dev, drm_vram_mm_release, NULL);
1085}
1086EXPORT_SYMBOL(drmm_vram_helper_init);
1087
80f7c3f7
TZ
1088/*
1089 * Mode-config helpers
1090 */
1091
1092static enum drm_mode_status
1093drm_vram_helper_mode_valid_internal(struct drm_device *dev,
1094 const struct drm_display_mode *mode,
1095 unsigned long max_bpp)
1096{
1097 struct drm_vram_mm *vmm = dev->vram_mm;
1098 unsigned long fbsize, fbpages, max_fbpages;
1099
1100 if (WARN_ON(!dev->vram_mm))
1101 return MODE_BAD;
1102
1103 max_fbpages = (vmm->vram_size / 2) >> PAGE_SHIFT;
1104
1105 fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
1106 fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
1107
1108 if (fbpages > max_fbpages)
1109 return MODE_MEM;
1110
1111 return MODE_OK;
1112}
1113
1114/**
1115 * drm_vram_helper_mode_valid - Tests if a display mode's
1116 * framebuffer fits into the available video memory.
1117 * @dev: the DRM device
1118 * @mode: the mode to test
1119 *
1120 * This function tests if enough video memory is available for using the
1121 * specified display mode. Atomic modesetting requires importing the
1122 * designated framebuffer into video memory before evicting the active
1123 * one. Hence, any framebuffer may consume at most half of the available
1124 * VRAM. Display modes that require a larger framebuffer can not be used,
1125 * even if the CRTC does support them. Each framebuffer is assumed to
1126 * have 32-bit color depth.
1127 *
1128 * Note:
1129 * The function can only test if the display mode is supported in
1130 * general. If there are too many framebuffers pinned to video memory,
1131 * a display mode may still not be usable in practice. The color depth of
1132 * 32-bit fits all current use case. A more flexible test can be added
1133 * when necessary.
1134 *
1135 * Returns:
1136 * MODE_OK if the display mode is supported, or an error code of type
1137 * enum drm_mode_status otherwise.
1138 */
1139enum drm_mode_status
1140drm_vram_helper_mode_valid(struct drm_device *dev,
1141 const struct drm_display_mode *mode)
1142{
1143 static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGB8888 */
1144
1145 return drm_vram_helper_mode_valid_internal(dev, mode, max_bpp);
1146}
1147EXPORT_SYMBOL(drm_vram_helper_mode_valid);
b22b51a0
TZ
1148
1149MODULE_DESCRIPTION("DRM VRAM memory-management helpers");
1150MODULE_LICENSE("GPL");