1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Russell King
6 #include <linux/dma-buf.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/mman.h>
9 #include <linux/shmem_fs.h>
11 #include <drm/armada_drm.h>
12 #include <drm/drm_prime.h>
14 #include "armada_drm.h"
15 #include "armada_gem.h"
16 #include "armada_ioctlP.h"
18 static vm_fault_t
armada_gem_vm_fault(struct vm_fault
*vmf
)
20 struct drm_gem_object
*gobj
= vmf
->vma
->vm_private_data
;
21 struct armada_gem_object
*obj
= drm_to_armada_gem(gobj
);
22 unsigned long pfn
= obj
->phys_addr
>> PAGE_SHIFT
;
24 pfn
+= (vmf
->address
- vmf
->vma
->vm_start
) >> PAGE_SHIFT
;
25 return vmf_insert_pfn(vmf
->vma
, vmf
->address
, pfn
);
28 const struct vm_operations_struct armada_gem_vm_ops
= {
29 .fault
= armada_gem_vm_fault
,
30 .open
= drm_gem_vm_open
,
31 .close
= drm_gem_vm_close
,
34 static size_t roundup_gem_size(size_t size
)
36 return roundup(size
, PAGE_SIZE
);
39 void armada_gem_free_object(struct drm_gem_object
*obj
)
41 struct armada_gem_object
*dobj
= drm_to_armada_gem(obj
);
42 struct armada_private
*priv
= obj
->dev
->dev_private
;
44 DRM_DEBUG_DRIVER("release obj %p\n", dobj
);
46 drm_gem_free_mmap_offset(&dobj
->obj
);
48 might_lock(&priv
->linear_lock
);
51 /* page backed memory */
52 unsigned int order
= get_order(dobj
->obj
.size
);
53 __free_pages(dobj
->page
, order
);
54 } else if (dobj
->linear
) {
55 /* linear backed memory */
56 mutex_lock(&priv
->linear_lock
);
57 drm_mm_remove_node(dobj
->linear
);
58 mutex_unlock(&priv
->linear_lock
);
64 if (dobj
->obj
.import_attach
) {
65 /* We only ever display imported data */
67 dma_buf_unmap_attachment(dobj
->obj
.import_attach
,
68 dobj
->sgt
, DMA_TO_DEVICE
);
69 drm_prime_gem_destroy(&dobj
->obj
, NULL
);
72 drm_gem_object_release(&dobj
->obj
);
78 armada_gem_linear_back(struct drm_device
*dev
, struct armada_gem_object
*obj
)
80 struct armada_private
*priv
= dev
->dev_private
;
81 size_t size
= obj
->obj
.size
;
83 if (obj
->page
|| obj
->linear
)
87 * If it is a small allocation (typically cursor, which will
88 * be 32x64 or 64x32 ARGB pixels) try to get it from the system.
89 * Framebuffers will never be this small (our minimum size for
90 * framebuffers is larger than this anyway.) Such objects are
91 * only accessed by the CPU so we don't need any special handing
95 unsigned int order
= get_order(size
);
96 struct page
*p
= alloc_pages(GFP_KERNEL
, order
);
99 obj
->addr
= page_address(p
);
100 obj
->phys_addr
= page_to_phys(p
);
103 memset(obj
->addr
, 0, PAGE_ALIGN(size
));
108 * We could grab something from CMA if it's enabled, but that
109 * involves building in a problem:
111 * CMA's interface uses dma_alloc_coherent(), which provides us
112 * with an CPU virtual address and a device address.
114 * The CPU virtual address may be either an address in the kernel
115 * direct mapped region (for example, as it would be on x86) or
116 * it may be remapped into another part of kernel memory space
117 * (eg, as it would be on ARM.) This means virt_to_phys() on the
118 * returned virtual address is invalid depending on the architecture
121 * The device address may also not be a physical address; it may
122 * be that there is some kind of remapping between the device and
123 * system RAM, which makes the use of the device address also
124 * unsafe to re-use as a physical address.
126 * This makes DRM usage of dma_alloc_coherent() in a generic way
127 * at best very questionable and unsafe.
130 /* Otherwise, grab it from our linear allocation */
132 struct drm_mm_node
*node
;
133 unsigned align
= min_t(unsigned, size
, SZ_2M
);
137 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
141 mutex_lock(&priv
->linear_lock
);
142 ret
= drm_mm_insert_node_generic(&priv
->linear
, node
,
144 mutex_unlock(&priv
->linear_lock
);
152 /* Ensure that the memory we're returning is cleared. */
153 ptr
= ioremap_wc(obj
->linear
->start
, size
);
155 mutex_lock(&priv
->linear_lock
);
156 drm_mm_remove_node(obj
->linear
);
157 mutex_unlock(&priv
->linear_lock
);
163 memset_io(ptr
, 0, size
);
166 obj
->phys_addr
= obj
->linear
->start
;
167 obj
->dev_addr
= obj
->linear
->start
;
171 DRM_DEBUG_DRIVER("obj %p phys %#llx dev %#llx\n", obj
,
172 (unsigned long long)obj
->phys_addr
,
173 (unsigned long long)obj
->dev_addr
);
179 armada_gem_map_object(struct drm_device
*dev
, struct armada_gem_object
*dobj
)
181 /* only linear objects need to be ioremap'd */
182 if (!dobj
->addr
&& dobj
->linear
)
183 dobj
->addr
= ioremap_wc(dobj
->phys_addr
, dobj
->obj
.size
);
187 struct armada_gem_object
*
188 armada_gem_alloc_private_object(struct drm_device
*dev
, size_t size
)
190 struct armada_gem_object
*obj
;
192 size
= roundup_gem_size(size
);
194 obj
= kzalloc(sizeof(*obj
), GFP_KERNEL
);
198 drm_gem_private_object_init(dev
, &obj
->obj
, size
);
200 DRM_DEBUG_DRIVER("alloc private obj %p size %zu\n", obj
, size
);
205 static struct armada_gem_object
*armada_gem_alloc_object(struct drm_device
*dev
,
208 struct armada_gem_object
*obj
;
209 struct address_space
*mapping
;
211 size
= roundup_gem_size(size
);
213 obj
= kzalloc(sizeof(*obj
), GFP_KERNEL
);
217 if (drm_gem_object_init(dev
, &obj
->obj
, size
)) {
222 mapping
= obj
->obj
.filp
->f_mapping
;
223 mapping_set_gfp_mask(mapping
, GFP_HIGHUSER
| __GFP_RECLAIMABLE
);
225 DRM_DEBUG_DRIVER("alloc obj %p size %zu\n", obj
, size
);
230 /* Dumb alloc support */
231 int armada_gem_dumb_create(struct drm_file
*file
, struct drm_device
*dev
,
232 struct drm_mode_create_dumb
*args
)
234 struct armada_gem_object
*dobj
;
239 args
->pitch
= armada_pitch(args
->width
, args
->bpp
);
240 args
->size
= size
= args
->pitch
* args
->height
;
242 dobj
= armada_gem_alloc_private_object(dev
, size
);
246 ret
= armada_gem_linear_back(dev
, dobj
);
250 ret
= drm_gem_handle_create(file
, &dobj
->obj
, &handle
);
254 args
->handle
= handle
;
256 /* drop reference from allocate - handle holds it now */
257 DRM_DEBUG_DRIVER("obj %p size %zu handle %#x\n", dobj
, size
, handle
);
259 drm_gem_object_put_unlocked(&dobj
->obj
);
263 /* Private driver gem ioctls */
264 int armada_gem_create_ioctl(struct drm_device
*dev
, void *data
,
265 struct drm_file
*file
)
267 struct drm_armada_gem_create
*args
= data
;
268 struct armada_gem_object
*dobj
;
278 dobj
= armada_gem_alloc_object(dev
, size
);
282 ret
= drm_gem_handle_create(file
, &dobj
->obj
, &handle
);
286 args
->handle
= handle
;
288 /* drop reference from allocate - handle holds it now */
289 DRM_DEBUG_DRIVER("obj %p size %zu handle %#x\n", dobj
, size
, handle
);
291 drm_gem_object_put_unlocked(&dobj
->obj
);
295 /* Map a shmem-backed object into process memory space */
296 int armada_gem_mmap_ioctl(struct drm_device
*dev
, void *data
,
297 struct drm_file
*file
)
299 struct drm_armada_gem_mmap
*args
= data
;
300 struct armada_gem_object
*dobj
;
303 dobj
= armada_gem_object_lookup(file
, args
->handle
);
307 if (!dobj
->obj
.filp
) {
308 drm_gem_object_put_unlocked(&dobj
->obj
);
312 addr
= vm_mmap(dobj
->obj
.filp
, 0, args
->size
, PROT_READ
| PROT_WRITE
,
313 MAP_SHARED
, args
->offset
);
314 drm_gem_object_put_unlocked(&dobj
->obj
);
315 if (IS_ERR_VALUE(addr
))
323 int armada_gem_pwrite_ioctl(struct drm_device
*dev
, void *data
,
324 struct drm_file
*file
)
326 struct drm_armada_gem_pwrite
*args
= data
;
327 struct armada_gem_object
*dobj
;
331 DRM_DEBUG_DRIVER("handle %u off %u size %u ptr 0x%llx\n",
332 args
->handle
, args
->offset
, args
->size
, args
->ptr
);
337 ptr
= (char __user
*)(uintptr_t)args
->ptr
;
339 if (!access_ok(ptr
, args
->size
))
342 ret
= fault_in_pages_readable(ptr
, args
->size
);
346 dobj
= armada_gem_object_lookup(file
, args
->handle
);
350 /* Must be a kernel-mapped object */
354 if (args
->offset
> dobj
->obj
.size
||
355 args
->size
> dobj
->obj
.size
- args
->offset
) {
356 DRM_ERROR("invalid size: object size %u\n", dobj
->obj
.size
);
361 if (copy_from_user(dobj
->addr
+ args
->offset
, ptr
, args
->size
)) {
363 } else if (dobj
->update
) {
364 dobj
->update(dobj
->update_data
);
369 drm_gem_object_put_unlocked(&dobj
->obj
);
374 static struct sg_table
*
375 armada_gem_prime_map_dma_buf(struct dma_buf_attachment
*attach
,
376 enum dma_data_direction dir
)
378 struct drm_gem_object
*obj
= attach
->dmabuf
->priv
;
379 struct armada_gem_object
*dobj
= drm_to_armada_gem(obj
);
380 struct scatterlist
*sg
;
381 struct sg_table
*sgt
;
384 sgt
= kmalloc(sizeof(*sgt
), GFP_KERNEL
);
388 if (dobj
->obj
.filp
) {
389 struct address_space
*mapping
;
392 count
= dobj
->obj
.size
/ PAGE_SIZE
;
393 if (sg_alloc_table(sgt
, count
, GFP_KERNEL
))
396 mapping
= dobj
->obj
.filp
->f_mapping
;
398 for_each_sg(sgt
->sgl
, sg
, count
, i
) {
401 page
= shmem_read_mapping_page(mapping
, i
);
407 sg_set_page(sg
, page
, PAGE_SIZE
, 0);
410 if (dma_map_sg(attach
->dev
, sgt
->sgl
, sgt
->nents
, dir
) == 0) {
414 } else if (dobj
->page
) {
415 /* Single contiguous page */
416 if (sg_alloc_table(sgt
, 1, GFP_KERNEL
))
419 sg_set_page(sgt
->sgl
, dobj
->page
, dobj
->obj
.size
, 0);
421 if (dma_map_sg(attach
->dev
, sgt
->sgl
, sgt
->nents
, dir
) == 0)
423 } else if (dobj
->linear
) {
424 /* Single contiguous physical region - no struct page */
425 if (sg_alloc_table(sgt
, 1, GFP_KERNEL
))
427 sg_dma_address(sgt
->sgl
) = dobj
->dev_addr
;
428 sg_dma_len(sgt
->sgl
) = dobj
->obj
.size
;
435 for_each_sg(sgt
->sgl
, sg
, num
, i
)
436 put_page(sg_page(sg
));
444 static void armada_gem_prime_unmap_dma_buf(struct dma_buf_attachment
*attach
,
445 struct sg_table
*sgt
, enum dma_data_direction dir
)
447 struct drm_gem_object
*obj
= attach
->dmabuf
->priv
;
448 struct armada_gem_object
*dobj
= drm_to_armada_gem(obj
);
452 dma_unmap_sg(attach
->dev
, sgt
->sgl
, sgt
->nents
, dir
);
454 if (dobj
->obj
.filp
) {
455 struct scatterlist
*sg
;
456 for_each_sg(sgt
->sgl
, sg
, sgt
->nents
, i
)
457 put_page(sg_page(sg
));
465 armada_gem_dmabuf_mmap(struct dma_buf
*buf
, struct vm_area_struct
*vma
)
470 static const struct dma_buf_ops armada_gem_prime_dmabuf_ops
= {
471 .map_dma_buf
= armada_gem_prime_map_dma_buf
,
472 .unmap_dma_buf
= armada_gem_prime_unmap_dma_buf
,
473 .release
= drm_gem_dmabuf_release
,
474 .mmap
= armada_gem_dmabuf_mmap
,
478 armada_gem_prime_export(struct drm_gem_object
*obj
, int flags
)
480 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
482 exp_info
.ops
= &armada_gem_prime_dmabuf_ops
;
483 exp_info
.size
= obj
->size
;
484 exp_info
.flags
= O_RDWR
;
487 return drm_gem_dmabuf_export(obj
->dev
, &exp_info
);
490 struct drm_gem_object
*
491 armada_gem_prime_import(struct drm_device
*dev
, struct dma_buf
*buf
)
493 struct dma_buf_attachment
*attach
;
494 struct armada_gem_object
*dobj
;
496 if (buf
->ops
== &armada_gem_prime_dmabuf_ops
) {
497 struct drm_gem_object
*obj
= buf
->priv
;
498 if (obj
->dev
== dev
) {
500 * Importing our own dmabuf(s) increases the
501 * refcount on the gem object itself.
503 drm_gem_object_get(obj
);
508 attach
= dma_buf_attach(buf
, dev
->dev
);
510 return ERR_CAST(attach
);
512 dobj
= armada_gem_alloc_private_object(dev
, buf
->size
);
514 dma_buf_detach(buf
, attach
);
515 return ERR_PTR(-ENOMEM
);
518 dobj
->obj
.import_attach
= attach
;
522 * Don't call dma_buf_map_attachment() here - it maps the
523 * scatterlist immediately for DMA, and this is not always
524 * an appropriate thing to do.
529 int armada_gem_map_import(struct armada_gem_object
*dobj
)
533 dobj
->sgt
= dma_buf_map_attachment(dobj
->obj
.import_attach
,
535 if (IS_ERR(dobj
->sgt
)) {
536 ret
= PTR_ERR(dobj
->sgt
);
538 DRM_ERROR("dma_buf_map_attachment() error: %d\n", ret
);
541 if (dobj
->sgt
->nents
> 1) {
542 DRM_ERROR("dma_buf_map_attachment() returned an (unsupported) scattered list\n");
545 if (sg_dma_len(dobj
->sgt
->sgl
) < dobj
->obj
.size
) {
546 DRM_ERROR("dma_buf_map_attachment() returned a small buffer\n");
549 dobj
->dev_addr
= sg_dma_address(dobj
->sgt
->sgl
);