2 * Copyright 2011 Red Hat, Inc.
3 * Copyright © 2014 The Chromium OS Authors
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software")
7 * to deal in the software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * them Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
20 * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * Adam Jackson <ajax@redhat.com>
25 * Ben Widawsky <ben@bwidawsk.net>
29 * This is vgem, a (non-hardware-backed) GEM service. This is used by Mesa's
30 * software renderer and the X server for efficient buffer sharing.
33 #include <linux/dma-buf.h>
34 #include <linux/module.h>
35 #include <linux/platform_device.h>
36 #include <linux/shmem_fs.h>
37 #include <linux/vmalloc.h>
39 #include <drm/drm_drv.h>
40 #include <drm/drm_file.h>
41 #include <drm/drm_ioctl.h>
42 #include <drm/drm_prime.h>
46 #define DRIVER_NAME "vgem"
47 #define DRIVER_DESC "Virtual GEM provider"
48 #define DRIVER_DATE "20120112"
49 #define DRIVER_MAJOR 1
50 #define DRIVER_MINOR 0
52 static struct vgem_device
{
53 struct drm_device drm
;
54 struct platform_device
*platform
;
57 static void vgem_gem_free_object(struct drm_gem_object
*obj
)
59 struct drm_vgem_gem_object
*vgem_obj
= to_vgem_bo(obj
);
61 kvfree(vgem_obj
->pages
);
62 mutex_destroy(&vgem_obj
->pages_lock
);
64 if (obj
->import_attach
)
65 drm_prime_gem_destroy(obj
, vgem_obj
->table
);
67 drm_gem_object_release(obj
);
71 static vm_fault_t
vgem_gem_fault(struct vm_fault
*vmf
)
73 struct vm_area_struct
*vma
= vmf
->vma
;
74 struct drm_vgem_gem_object
*obj
= vma
->vm_private_data
;
75 /* We don't use vmf->pgoff since that has the fake offset */
76 unsigned long vaddr
= vmf
->address
;
77 vm_fault_t ret
= VM_FAULT_SIGBUS
;
80 page_offset
= (vaddr
- vma
->vm_start
) >> PAGE_SHIFT
;
82 num_pages
= DIV_ROUND_UP(obj
->base
.size
, PAGE_SIZE
);
84 if (page_offset
>= num_pages
)
85 return VM_FAULT_SIGBUS
;
87 mutex_lock(&obj
->pages_lock
);
89 get_page(obj
->pages
[page_offset
]);
90 vmf
->page
= obj
->pages
[page_offset
];
93 mutex_unlock(&obj
->pages_lock
);
97 page
= shmem_read_mapping_page(
98 file_inode(obj
->base
.filp
)->i_mapping
,
103 } else switch (PTR_ERR(page
)) {
109 ret
= VM_FAULT_RETRY
;
113 ret
= VM_FAULT_SIGBUS
;
116 WARN_ON(PTR_ERR(page
));
117 ret
= VM_FAULT_SIGBUS
;
125 static const struct vm_operations_struct vgem_gem_vm_ops
= {
126 .fault
= vgem_gem_fault
,
127 .open
= drm_gem_vm_open
,
128 .close
= drm_gem_vm_close
,
131 static int vgem_open(struct drm_device
*dev
, struct drm_file
*file
)
133 struct vgem_file
*vfile
;
136 vfile
= kzalloc(sizeof(*vfile
), GFP_KERNEL
);
140 file
->driver_priv
= vfile
;
142 ret
= vgem_fence_open(vfile
);
151 static void vgem_postclose(struct drm_device
*dev
, struct drm_file
*file
)
153 struct vgem_file
*vfile
= file
->driver_priv
;
155 vgem_fence_close(vfile
);
159 static struct drm_vgem_gem_object
*__vgem_gem_create(struct drm_device
*dev
,
162 struct drm_vgem_gem_object
*obj
;
165 obj
= kzalloc(sizeof(*obj
), GFP_KERNEL
);
167 return ERR_PTR(-ENOMEM
);
169 ret
= drm_gem_object_init(dev
, &obj
->base
, roundup(size
, PAGE_SIZE
));
175 mutex_init(&obj
->pages_lock
);
180 static void __vgem_gem_destroy(struct drm_vgem_gem_object
*obj
)
182 drm_gem_object_release(&obj
->base
);
186 static struct drm_gem_object
*vgem_gem_create(struct drm_device
*dev
,
187 struct drm_file
*file
,
188 unsigned int *handle
,
191 struct drm_vgem_gem_object
*obj
;
194 obj
= __vgem_gem_create(dev
, size
);
196 return ERR_CAST(obj
);
198 ret
= drm_gem_handle_create(file
, &obj
->base
, handle
);
199 drm_gem_object_put_unlocked(&obj
->base
);
206 static int vgem_gem_dumb_create(struct drm_file
*file
, struct drm_device
*dev
,
207 struct drm_mode_create_dumb
*args
)
209 struct drm_gem_object
*gem_object
;
212 pitch
= args
->width
* DIV_ROUND_UP(args
->bpp
, 8);
213 size
= args
->height
* pitch
;
217 gem_object
= vgem_gem_create(dev
, file
, &args
->handle
, size
);
218 if (IS_ERR(gem_object
))
219 return PTR_ERR(gem_object
);
221 args
->size
= gem_object
->size
;
224 DRM_DEBUG("Created object of size %lld\n", size
);
229 static int vgem_gem_dumb_map(struct drm_file
*file
, struct drm_device
*dev
,
230 uint32_t handle
, uint64_t *offset
)
232 struct drm_gem_object
*obj
;
235 obj
= drm_gem_object_lookup(file
, handle
);
244 ret
= drm_gem_create_mmap_offset(obj
);
248 *offset
= drm_vma_node_offset_addr(&obj
->vma_node
);
250 drm_gem_object_put_unlocked(obj
);
255 static struct drm_ioctl_desc vgem_ioctls
[] = {
256 DRM_IOCTL_DEF_DRV(VGEM_FENCE_ATTACH
, vgem_fence_attach_ioctl
, DRM_RENDER_ALLOW
),
257 DRM_IOCTL_DEF_DRV(VGEM_FENCE_SIGNAL
, vgem_fence_signal_ioctl
, DRM_RENDER_ALLOW
),
260 static int vgem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
262 unsigned long flags
= vma
->vm_flags
;
265 ret
= drm_gem_mmap(filp
, vma
);
269 /* Keep the WC mmaping set by drm_gem_mmap() but our pages
270 * are ordinary and not special.
272 vma
->vm_flags
= flags
| VM_DONTEXPAND
| VM_DONTDUMP
;
276 static const struct file_operations vgem_driver_fops
= {
277 .owner
= THIS_MODULE
,
282 .unlocked_ioctl
= drm_ioctl
,
283 .compat_ioctl
= drm_compat_ioctl
,
284 .release
= drm_release
,
287 static struct page
**vgem_pin_pages(struct drm_vgem_gem_object
*bo
)
289 mutex_lock(&bo
->pages_lock
);
290 if (bo
->pages_pin_count
++ == 0) {
293 pages
= drm_gem_get_pages(&bo
->base
);
295 bo
->pages_pin_count
--;
296 mutex_unlock(&bo
->pages_lock
);
302 mutex_unlock(&bo
->pages_lock
);
307 static void vgem_unpin_pages(struct drm_vgem_gem_object
*bo
)
309 mutex_lock(&bo
->pages_lock
);
310 if (--bo
->pages_pin_count
== 0) {
311 drm_gem_put_pages(&bo
->base
, bo
->pages
, true, true);
314 mutex_unlock(&bo
->pages_lock
);
317 static int vgem_prime_pin(struct drm_gem_object
*obj
)
319 struct drm_vgem_gem_object
*bo
= to_vgem_bo(obj
);
320 long n_pages
= obj
->size
>> PAGE_SHIFT
;
323 pages
= vgem_pin_pages(bo
);
325 return PTR_ERR(pages
);
327 /* Flush the object from the CPU cache so that importers can rely
328 * on coherent indirect access via the exported dma-address.
330 drm_clflush_pages(pages
, n_pages
);
335 static void vgem_prime_unpin(struct drm_gem_object
*obj
)
337 struct drm_vgem_gem_object
*bo
= to_vgem_bo(obj
);
339 vgem_unpin_pages(bo
);
342 static struct sg_table
*vgem_prime_get_sg_table(struct drm_gem_object
*obj
)
344 struct drm_vgem_gem_object
*bo
= to_vgem_bo(obj
);
346 return drm_prime_pages_to_sg(bo
->pages
, bo
->base
.size
>> PAGE_SHIFT
);
349 static struct drm_gem_object
* vgem_prime_import(struct drm_device
*dev
,
350 struct dma_buf
*dma_buf
)
352 struct vgem_device
*vgem
= container_of(dev
, typeof(*vgem
), drm
);
354 return drm_gem_prime_import_dev(dev
, dma_buf
, &vgem
->platform
->dev
);
357 static struct drm_gem_object
*vgem_prime_import_sg_table(struct drm_device
*dev
,
358 struct dma_buf_attachment
*attach
, struct sg_table
*sg
)
360 struct drm_vgem_gem_object
*obj
;
363 obj
= __vgem_gem_create(dev
, attach
->dmabuf
->size
);
365 return ERR_CAST(obj
);
367 npages
= PAGE_ALIGN(attach
->dmabuf
->size
) / PAGE_SIZE
;
370 obj
->pages
= kvmalloc_array(npages
, sizeof(struct page
*), GFP_KERNEL
);
372 __vgem_gem_destroy(obj
);
373 return ERR_PTR(-ENOMEM
);
376 obj
->pages_pin_count
++; /* perma-pinned */
377 drm_prime_sg_to_page_addr_arrays(obj
->table
, obj
->pages
, NULL
,
382 static void *vgem_prime_vmap(struct drm_gem_object
*obj
)
384 struct drm_vgem_gem_object
*bo
= to_vgem_bo(obj
);
385 long n_pages
= obj
->size
>> PAGE_SHIFT
;
388 pages
= vgem_pin_pages(bo
);
392 return vmap(pages
, n_pages
, 0, pgprot_writecombine(PAGE_KERNEL
));
395 static void vgem_prime_vunmap(struct drm_gem_object
*obj
, void *vaddr
)
397 struct drm_vgem_gem_object
*bo
= to_vgem_bo(obj
);
400 vgem_unpin_pages(bo
);
403 static int vgem_prime_mmap(struct drm_gem_object
*obj
,
404 struct vm_area_struct
*vma
)
408 if (obj
->size
< vma
->vm_end
- vma
->vm_start
)
414 ret
= call_mmap(obj
->filp
, vma
);
419 vma
->vm_file
= get_file(obj
->filp
);
420 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
421 vma
->vm_page_prot
= pgprot_writecombine(vm_get_page_prot(vma
->vm_flags
));
426 static void vgem_release(struct drm_device
*dev
)
428 struct vgem_device
*vgem
= container_of(dev
, typeof(*vgem
), drm
);
430 platform_device_unregister(vgem
->platform
);
431 drm_dev_fini(&vgem
->drm
);
436 static struct drm_driver vgem_driver
= {
437 .driver_features
= DRIVER_GEM
| DRIVER_RENDER
,
438 .release
= vgem_release
,
440 .postclose
= vgem_postclose
,
441 .gem_free_object_unlocked
= vgem_gem_free_object
,
442 .gem_vm_ops
= &vgem_gem_vm_ops
,
443 .ioctls
= vgem_ioctls
,
444 .num_ioctls
= ARRAY_SIZE(vgem_ioctls
),
445 .fops
= &vgem_driver_fops
,
447 .dumb_create
= vgem_gem_dumb_create
,
448 .dumb_map_offset
= vgem_gem_dumb_map
,
450 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
451 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
452 .gem_prime_pin
= vgem_prime_pin
,
453 .gem_prime_unpin
= vgem_prime_unpin
,
454 .gem_prime_import
= vgem_prime_import
,
455 .gem_prime_import_sg_table
= vgem_prime_import_sg_table
,
456 .gem_prime_get_sg_table
= vgem_prime_get_sg_table
,
457 .gem_prime_vmap
= vgem_prime_vmap
,
458 .gem_prime_vunmap
= vgem_prime_vunmap
,
459 .gem_prime_mmap
= vgem_prime_mmap
,
464 .major
= DRIVER_MAJOR
,
465 .minor
= DRIVER_MINOR
,
468 static int __init
vgem_init(void)
472 vgem_device
= kzalloc(sizeof(*vgem_device
), GFP_KERNEL
);
476 vgem_device
->platform
=
477 platform_device_register_simple("vgem", -1, NULL
, 0);
478 if (IS_ERR(vgem_device
->platform
)) {
479 ret
= PTR_ERR(vgem_device
->platform
);
483 dma_coerce_mask_and_coherent(&vgem_device
->platform
->dev
,
485 ret
= drm_dev_init(&vgem_device
->drm
, &vgem_driver
,
486 &vgem_device
->platform
->dev
);
490 /* Final step: expose the device/driver to userspace */
491 ret
= drm_dev_register(&vgem_device
->drm
, 0);
498 drm_dev_fini(&vgem_device
->drm
);
500 platform_device_unregister(vgem_device
->platform
);
506 static void __exit
vgem_exit(void)
508 drm_dev_unregister(&vgem_device
->drm
);
509 drm_dev_put(&vgem_device
->drm
);
512 module_init(vgem_init
);
513 module_exit(vgem_exit
);
515 MODULE_AUTHOR("Red Hat, Inc.");
516 MODULE_AUTHOR("Intel Corporation");
517 MODULE_DESCRIPTION(DRIVER_DESC
);
518 MODULE_LICENSE("GPL and additional rights");