2 * Copyright (C) 2012 Russell King
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 #include <linux/dma-buf.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/shmem_fs.h>
11 #include "armada_drm.h"
12 #include "armada_gem.h"
13 #include <drm/armada_drm.h>
14 #include "armada_ioctlP.h"
16 static vm_fault_t armada_gem_vm_fault(struct vm_fault *vmf)
18 struct drm_gem_object *gobj = vmf->vma->vm_private_data;
19 struct armada_gem_object *obj = drm_to_armada_gem(gobj);
20 unsigned long pfn = obj->phys_addr >> PAGE_SHIFT;
22 pfn += (vmf->address - vmf->vma->vm_start) >> PAGE_SHIFT;
23 return vmf_insert_pfn(vmf->vma, vmf->address, pfn);
26 const struct vm_operations_struct armada_gem_vm_ops = {
27 .fault = armada_gem_vm_fault,
28 .open = drm_gem_vm_open,
29 .close = drm_gem_vm_close,
32 static size_t roundup_gem_size(size_t size)
34 return roundup(size, PAGE_SIZE);
37 void armada_gem_free_object(struct drm_gem_object *obj)
39 struct armada_gem_object *dobj = drm_to_armada_gem(obj);
40 struct armada_private *priv = obj->dev->dev_private;
42 DRM_DEBUG_DRIVER("release obj %p\n", dobj);
44 drm_gem_free_mmap_offset(&dobj->obj);
46 might_lock(&priv->linear_lock);
49 /* page backed memory */
50 unsigned int order = get_order(dobj->obj.size);
51 __free_pages(dobj->page, order);
52 } else if (dobj->linear) {
53 /* linear backed memory */
54 mutex_lock(&priv->linear_lock);
55 drm_mm_remove_node(dobj->linear);
56 mutex_unlock(&priv->linear_lock);
62 if (dobj->obj.import_attach) {
63 /* We only ever display imported data */
65 dma_buf_unmap_attachment(dobj->obj.import_attach,
66 dobj->sgt, DMA_TO_DEVICE);
67 drm_prime_gem_destroy(&dobj->obj, NULL);
70 drm_gem_object_release(&dobj->obj);
76 armada_gem_linear_back(struct drm_device *dev, struct armada_gem_object *obj)
78 struct armada_private *priv = dev->dev_private;
79 size_t size = obj->obj.size;
81 if (obj->page || obj->linear)
85 * If it is a small allocation (typically cursor, which will
86 * be 32x64 or 64x32 ARGB pixels) try to get it from the system.
87 * Framebuffers will never be this small (our minimum size for
88 * framebuffers is larger than this anyway.) Such objects are
89 * only accessed by the CPU so we don't need any special handing
93 unsigned int order = get_order(size);
94 struct page *p = alloc_pages(GFP_KERNEL, order);
97 obj->addr = page_address(p);
98 obj->phys_addr = page_to_phys(p);
101 memset(obj->addr, 0, PAGE_ALIGN(size));
106 * We could grab something from CMA if it's enabled, but that
107 * involves building in a problem:
109 * CMA's interface uses dma_alloc_coherent(), which provides us
110 * with an CPU virtual address and a device address.
112 * The CPU virtual address may be either an address in the kernel
113 * direct mapped region (for example, as it would be on x86) or
114 * it may be remapped into another part of kernel memory space
115 * (eg, as it would be on ARM.) This means virt_to_phys() on the
116 * returned virtual address is invalid depending on the architecture
119 * The device address may also not be a physical address; it may
120 * be that there is some kind of remapping between the device and
121 * system RAM, which makes the use of the device address also
122 * unsafe to re-use as a physical address.
124 * This makes DRM usage of dma_alloc_coherent() in a generic way
125 * at best very questionable and unsafe.
128 /* Otherwise, grab it from our linear allocation */
130 struct drm_mm_node *node;
131 unsigned align = min_t(unsigned, size, SZ_2M);
135 node = kzalloc(sizeof(*node), GFP_KERNEL);
139 mutex_lock(&priv->linear_lock);
140 ret = drm_mm_insert_node_generic(&priv->linear, node,
142 mutex_unlock(&priv->linear_lock);
150 /* Ensure that the memory we're returning is cleared. */
151 ptr = ioremap_wc(obj->linear->start, size);
153 mutex_lock(&priv->linear_lock);
154 drm_mm_remove_node(obj->linear);
155 mutex_unlock(&priv->linear_lock);
161 memset_io(ptr, 0, size);
164 obj->phys_addr = obj->linear->start;
165 obj->dev_addr = obj->linear->start;
169 DRM_DEBUG_DRIVER("obj %p phys %#llx dev %#llx\n", obj,
170 (unsigned long long)obj->phys_addr,
171 (unsigned long long)obj->dev_addr);
177 armada_gem_map_object(struct drm_device *dev, struct armada_gem_object *dobj)
179 /* only linear objects need to be ioremap'd */
180 if (!dobj->addr && dobj->linear)
181 dobj->addr = ioremap_wc(dobj->phys_addr, dobj->obj.size);
185 struct armada_gem_object *
186 armada_gem_alloc_private_object(struct drm_device *dev, size_t size)
188 struct armada_gem_object *obj;
190 size = roundup_gem_size(size);
192 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
196 drm_gem_private_object_init(dev, &obj->obj, size);
198 DRM_DEBUG_DRIVER("alloc private obj %p size %zu\n", obj, size);
203 static struct armada_gem_object *armada_gem_alloc_object(struct drm_device *dev,
206 struct armada_gem_object *obj;
207 struct address_space *mapping;
209 size = roundup_gem_size(size);
211 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
215 if (drm_gem_object_init(dev, &obj->obj, size)) {
220 mapping = obj->obj.filp->f_mapping;
221 mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
223 DRM_DEBUG_DRIVER("alloc obj %p size %zu\n", obj, size);
228 /* Dumb alloc support */
229 int armada_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
230 struct drm_mode_create_dumb *args)
232 struct armada_gem_object *dobj;
237 args->pitch = armada_pitch(args->width, args->bpp);
238 args->size = size = args->pitch * args->height;
240 dobj = armada_gem_alloc_private_object(dev, size);
244 ret = armada_gem_linear_back(dev, dobj);
248 ret = drm_gem_handle_create(file, &dobj->obj, &handle);
252 args->handle = handle;
254 /* drop reference from allocate - handle holds it now */
255 DRM_DEBUG_DRIVER("obj %p size %zu handle %#x\n", dobj, size, handle);
257 drm_gem_object_put_unlocked(&dobj->obj);
261 /* Private driver gem ioctls */
262 int armada_gem_create_ioctl(struct drm_device *dev, void *data,
263 struct drm_file *file)
265 struct drm_armada_gem_create *args = data;
266 struct armada_gem_object *dobj;
276 dobj = armada_gem_alloc_object(dev, size);
280 ret = drm_gem_handle_create(file, &dobj->obj, &handle);
284 args->handle = handle;
286 /* drop reference from allocate - handle holds it now */
287 DRM_DEBUG_DRIVER("obj %p size %zu handle %#x\n", dobj, size, handle);
289 drm_gem_object_put_unlocked(&dobj->obj);
293 /* Map a shmem-backed object into process memory space */
294 int armada_gem_mmap_ioctl(struct drm_device *dev, void *data,
295 struct drm_file *file)
297 struct drm_armada_gem_mmap *args = data;
298 struct armada_gem_object *dobj;
301 dobj = armada_gem_object_lookup(file, args->handle);
305 if (!dobj->obj.filp) {
306 drm_gem_object_put_unlocked(&dobj->obj);
310 addr = vm_mmap(dobj->obj.filp, 0, args->size, PROT_READ | PROT_WRITE,
311 MAP_SHARED, args->offset);
312 drm_gem_object_put_unlocked(&dobj->obj);
313 if (IS_ERR_VALUE(addr))
321 int armada_gem_pwrite_ioctl(struct drm_device *dev, void *data,
322 struct drm_file *file)
324 struct drm_armada_gem_pwrite *args = data;
325 struct armada_gem_object *dobj;
329 DRM_DEBUG_DRIVER("handle %u off %u size %u ptr 0x%llx\n",
330 args->handle, args->offset, args->size, args->ptr);
335 ptr = (char __user *)(uintptr_t)args->ptr;
337 if (!access_ok(VERIFY_READ, ptr, args->size))
340 ret = fault_in_pages_readable(ptr, args->size);
344 dobj = armada_gem_object_lookup(file, args->handle);
348 /* Must be a kernel-mapped object */
352 if (args->offset > dobj->obj.size ||
353 args->size > dobj->obj.size - args->offset) {
354 DRM_ERROR("invalid size: object size %u\n", dobj->obj.size);
359 if (copy_from_user(dobj->addr + args->offset, ptr, args->size)) {
361 } else if (dobj->update) {
362 dobj->update(dobj->update_data);
367 drm_gem_object_put_unlocked(&dobj->obj);
372 static struct sg_table *
373 armada_gem_prime_map_dma_buf(struct dma_buf_attachment *attach,
374 enum dma_data_direction dir)
376 struct drm_gem_object *obj = attach->dmabuf->priv;
377 struct armada_gem_object *dobj = drm_to_armada_gem(obj);
378 struct scatterlist *sg;
379 struct sg_table *sgt;
382 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
386 if (dobj->obj.filp) {
387 struct address_space *mapping;
390 count = dobj->obj.size / PAGE_SIZE;
391 if (sg_alloc_table(sgt, count, GFP_KERNEL))
394 mapping = dobj->obj.filp->f_mapping;
396 for_each_sg(sgt->sgl, sg, count, i) {
399 page = shmem_read_mapping_page(mapping, i);
405 sg_set_page(sg, page, PAGE_SIZE, 0);
408 if (dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir) == 0) {
412 } else if (dobj->page) {
413 /* Single contiguous page */
414 if (sg_alloc_table(sgt, 1, GFP_KERNEL))
417 sg_set_page(sgt->sgl, dobj->page, dobj->obj.size, 0);
419 if (dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir) == 0)
421 } else if (dobj->linear) {
422 /* Single contiguous physical region - no struct page */
423 if (sg_alloc_table(sgt, 1, GFP_KERNEL))
425 sg_dma_address(sgt->sgl) = dobj->dev_addr;
426 sg_dma_len(sgt->sgl) = dobj->obj.size;
433 for_each_sg(sgt->sgl, sg, num, i)
434 put_page(sg_page(sg));
442 static void armada_gem_prime_unmap_dma_buf(struct dma_buf_attachment *attach,
443 struct sg_table *sgt, enum dma_data_direction dir)
445 struct drm_gem_object *obj = attach->dmabuf->priv;
446 struct armada_gem_object *dobj = drm_to_armada_gem(obj);
450 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
452 if (dobj->obj.filp) {
453 struct scatterlist *sg;
454 for_each_sg(sgt->sgl, sg, sgt->nents, i)
455 put_page(sg_page(sg));
462 static void *armada_gem_dmabuf_no_kmap(struct dma_buf *buf, unsigned long n)
468 armada_gem_dmabuf_no_kunmap(struct dma_buf *buf, unsigned long n, void *addr)
473 armada_gem_dmabuf_mmap(struct dma_buf *buf, struct vm_area_struct *vma)
478 static const struct dma_buf_ops armada_gem_prime_dmabuf_ops = {
479 .map_dma_buf = armada_gem_prime_map_dma_buf,
480 .unmap_dma_buf = armada_gem_prime_unmap_dma_buf,
481 .release = drm_gem_dmabuf_release,
482 .map = armada_gem_dmabuf_no_kmap,
483 .unmap = armada_gem_dmabuf_no_kunmap,
484 .mmap = armada_gem_dmabuf_mmap,
488 armada_gem_prime_export(struct drm_device *dev, struct drm_gem_object *obj,
491 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
493 exp_info.ops = &armada_gem_prime_dmabuf_ops;
494 exp_info.size = obj->size;
495 exp_info.flags = O_RDWR;
498 return drm_gem_dmabuf_export(dev, &exp_info);
501 struct drm_gem_object *
502 armada_gem_prime_import(struct drm_device *dev, struct dma_buf *buf)
504 struct dma_buf_attachment *attach;
505 struct armada_gem_object *dobj;
507 if (buf->ops == &armada_gem_prime_dmabuf_ops) {
508 struct drm_gem_object *obj = buf->priv;
509 if (obj->dev == dev) {
511 * Importing our own dmabuf(s) increases the
512 * refcount on the gem object itself.
514 drm_gem_object_get(obj);
519 attach = dma_buf_attach(buf, dev->dev);
521 return ERR_CAST(attach);
523 dobj = armada_gem_alloc_private_object(dev, buf->size);
525 dma_buf_detach(buf, attach);
526 return ERR_PTR(-ENOMEM);
529 dobj->obj.import_attach = attach;
533 * Don't call dma_buf_map_attachment() here - it maps the
534 * scatterlist immediately for DMA, and this is not always
535 * an appropriate thing to do.
540 int armada_gem_map_import(struct armada_gem_object *dobj)
544 dobj->sgt = dma_buf_map_attachment(dobj->obj.import_attach,
546 if (IS_ERR(dobj->sgt)) {
547 ret = PTR_ERR(dobj->sgt);
549 DRM_ERROR("dma_buf_map_attachment() error: %d\n", ret);
552 if (dobj->sgt->nents > 1) {
553 DRM_ERROR("dma_buf_map_attachment() returned an (unsupported) scattered list\n");
556 if (sg_dma_len(dobj->sgt->sgl) < dobj->obj.size) {
557 DRM_ERROR("dma_buf_map_attachment() returned a small buffer\n");
560 dobj->dev_addr = sg_dma_address(dobj->sgt->sgl);