vgem_drv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright 2011 Red Hat, Inc.
  3. * Copyright © 2014 The Chromium OS Authors
  4. *
  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:
  11. *
  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
  14. * Software.
  15. *
  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.
  22. *
  23. * Authors:
  24. * Adam Jackson <ajax@redhat.com>
  25. * Ben Widawsky <ben@bwidawsk.net>
  26. */
  27. /**
  28. * This is vgem, a (non-hardware-backed) GEM service. This is used by Mesa's
  29. * software renderer and the X server for efficient buffer sharing.
  30. */
  31. #include <linux/module.h>
  32. #include <linux/ramfs.h>
  33. #include <linux/shmem_fs.h>
  34. #include <linux/dma-buf.h>
  35. #include "vgem_drv.h"
  36. #define DRIVER_NAME "vgem"
  37. #define DRIVER_DESC "Virtual GEM provider"
  38. #define DRIVER_DATE "20120112"
  39. #define DRIVER_MAJOR 1
  40. #define DRIVER_MINOR 0
  41. static struct vgem_device {
  42. struct drm_device drm;
  43. struct platform_device *platform;
  44. } *vgem_device;
  45. static void vgem_gem_free_object(struct drm_gem_object *obj)
  46. {
  47. struct drm_vgem_gem_object *vgem_obj = to_vgem_bo(obj);
  48. kvfree(vgem_obj->pages);
  49. mutex_destroy(&vgem_obj->pages_lock);
  50. if (obj->import_attach)
  51. drm_prime_gem_destroy(obj, vgem_obj->table);
  52. drm_gem_object_release(obj);
  53. kfree(vgem_obj);
  54. }
  55. static int vgem_gem_fault(struct vm_fault *vmf)
  56. {
  57. struct vm_area_struct *vma = vmf->vma;
  58. struct drm_vgem_gem_object *obj = vma->vm_private_data;
  59. /* We don't use vmf->pgoff since that has the fake offset */
  60. unsigned long vaddr = vmf->address;
  61. int ret;
  62. loff_t num_pages;
  63. pgoff_t page_offset;
  64. page_offset = (vaddr - vma->vm_start) >> PAGE_SHIFT;
  65. num_pages = DIV_ROUND_UP(obj->base.size, PAGE_SIZE);
  66. if (page_offset > num_pages)
  67. return VM_FAULT_SIGBUS;
  68. ret = -ENOENT;
  69. mutex_lock(&obj->pages_lock);
  70. if (obj->pages) {
  71. get_page(obj->pages[page_offset]);
  72. vmf->page = obj->pages[page_offset];
  73. ret = 0;
  74. }
  75. mutex_unlock(&obj->pages_lock);
  76. if (ret) {
  77. struct page *page;
  78. page = shmem_read_mapping_page(
  79. file_inode(obj->base.filp)->i_mapping,
  80. page_offset);
  81. if (!IS_ERR(page)) {
  82. vmf->page = page;
  83. ret = 0;
  84. } else switch (PTR_ERR(page)) {
  85. case -ENOSPC:
  86. case -ENOMEM:
  87. ret = VM_FAULT_OOM;
  88. break;
  89. case -EBUSY:
  90. ret = VM_FAULT_RETRY;
  91. break;
  92. case -EFAULT:
  93. case -EINVAL:
  94. ret = VM_FAULT_SIGBUS;
  95. break;
  96. default:
  97. WARN_ON(PTR_ERR(page));
  98. ret = VM_FAULT_SIGBUS;
  99. break;
  100. }
  101. }
  102. return ret;
  103. }
  104. static const struct vm_operations_struct vgem_gem_vm_ops = {
  105. .fault = vgem_gem_fault,
  106. .open = drm_gem_vm_open,
  107. .close = drm_gem_vm_close,
  108. };
  109. static int vgem_open(struct drm_device *dev, struct drm_file *file)
  110. {
  111. struct vgem_file *vfile;
  112. int ret;
  113. vfile = kzalloc(sizeof(*vfile), GFP_KERNEL);
  114. if (!vfile)
  115. return -ENOMEM;
  116. file->driver_priv = vfile;
  117. ret = vgem_fence_open(vfile);
  118. if (ret) {
  119. kfree(vfile);
  120. return ret;
  121. }
  122. return 0;
  123. }
  124. static void vgem_postclose(struct drm_device *dev, struct drm_file *file)
  125. {
  126. struct vgem_file *vfile = file->driver_priv;
  127. vgem_fence_close(vfile);
  128. kfree(vfile);
  129. }
  130. static struct drm_vgem_gem_object *__vgem_gem_create(struct drm_device *dev,
  131. unsigned long size)
  132. {
  133. struct drm_vgem_gem_object *obj;
  134. int ret;
  135. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  136. if (!obj)
  137. return ERR_PTR(-ENOMEM);
  138. ret = drm_gem_object_init(dev, &obj->base, roundup(size, PAGE_SIZE));
  139. if (ret) {
  140. kfree(obj);
  141. return ERR_PTR(ret);
  142. }
  143. mutex_init(&obj->pages_lock);
  144. return obj;
  145. }
  146. static void __vgem_gem_destroy(struct drm_vgem_gem_object *obj)
  147. {
  148. drm_gem_object_release(&obj->base);
  149. kfree(obj);
  150. }
  151. static struct drm_gem_object *vgem_gem_create(struct drm_device *dev,
  152. struct drm_file *file,
  153. unsigned int *handle,
  154. unsigned long size)
  155. {
  156. struct drm_vgem_gem_object *obj;
  157. int ret;
  158. obj = __vgem_gem_create(dev, size);
  159. if (IS_ERR(obj))
  160. return ERR_CAST(obj);
  161. ret = drm_gem_handle_create(file, &obj->base, handle);
  162. drm_gem_object_put_unlocked(&obj->base);
  163. if (ret)
  164. return ERR_PTR(ret);
  165. return &obj->base;
  166. }
  167. static int vgem_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
  168. struct drm_mode_create_dumb *args)
  169. {
  170. struct drm_gem_object *gem_object;
  171. u64 pitch, size;
  172. pitch = args->width * DIV_ROUND_UP(args->bpp, 8);
  173. size = args->height * pitch;
  174. if (size == 0)
  175. return -EINVAL;
  176. gem_object = vgem_gem_create(dev, file, &args->handle, size);
  177. if (IS_ERR(gem_object))
  178. return PTR_ERR(gem_object);
  179. args->size = gem_object->size;
  180. args->pitch = pitch;
  181. DRM_DEBUG_DRIVER("Created object of size %lld\n", size);
  182. return 0;
  183. }
  184. static struct drm_ioctl_desc vgem_ioctls[] = {
  185. DRM_IOCTL_DEF_DRV(VGEM_FENCE_ATTACH, vgem_fence_attach_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  186. DRM_IOCTL_DEF_DRV(VGEM_FENCE_SIGNAL, vgem_fence_signal_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  187. };
  188. static int vgem_mmap(struct file *filp, struct vm_area_struct *vma)
  189. {
  190. unsigned long flags = vma->vm_flags;
  191. int ret;
  192. ret = drm_gem_mmap(filp, vma);
  193. if (ret)
  194. return ret;
  195. /* Keep the WC mmaping set by drm_gem_mmap() but our pages
  196. * are ordinary and not special.
  197. */
  198. vma->vm_flags = flags | VM_DONTEXPAND | VM_DONTDUMP;
  199. return 0;
  200. }
  201. static const struct file_operations vgem_driver_fops = {
  202. .owner = THIS_MODULE,
  203. .open = drm_open,
  204. .mmap = vgem_mmap,
  205. .poll = drm_poll,
  206. .read = drm_read,
  207. .unlocked_ioctl = drm_ioctl,
  208. .compat_ioctl = drm_compat_ioctl,
  209. .release = drm_release,
  210. };
  211. static struct page **vgem_pin_pages(struct drm_vgem_gem_object *bo)
  212. {
  213. mutex_lock(&bo->pages_lock);
  214. if (bo->pages_pin_count++ == 0) {
  215. struct page **pages;
  216. pages = drm_gem_get_pages(&bo->base);
  217. if (IS_ERR(pages)) {
  218. bo->pages_pin_count--;
  219. mutex_unlock(&bo->pages_lock);
  220. return pages;
  221. }
  222. bo->pages = pages;
  223. }
  224. mutex_unlock(&bo->pages_lock);
  225. return bo->pages;
  226. }
  227. static void vgem_unpin_pages(struct drm_vgem_gem_object *bo)
  228. {
  229. mutex_lock(&bo->pages_lock);
  230. if (--bo->pages_pin_count == 0) {
  231. drm_gem_put_pages(&bo->base, bo->pages, true, true);
  232. bo->pages = NULL;
  233. }
  234. mutex_unlock(&bo->pages_lock);
  235. }
  236. static int vgem_prime_pin(struct drm_gem_object *obj)
  237. {
  238. struct drm_vgem_gem_object *bo = to_vgem_bo(obj);
  239. long n_pages = obj->size >> PAGE_SHIFT;
  240. struct page **pages;
  241. pages = vgem_pin_pages(bo);
  242. if (IS_ERR(pages))
  243. return PTR_ERR(pages);
  244. /* Flush the object from the CPU cache so that importers can rely
  245. * on coherent indirect access via the exported dma-address.
  246. */
  247. drm_clflush_pages(pages, n_pages);
  248. return 0;
  249. }
  250. static void vgem_prime_unpin(struct drm_gem_object *obj)
  251. {
  252. struct drm_vgem_gem_object *bo = to_vgem_bo(obj);
  253. vgem_unpin_pages(bo);
  254. }
  255. static struct sg_table *vgem_prime_get_sg_table(struct drm_gem_object *obj)
  256. {
  257. struct drm_vgem_gem_object *bo = to_vgem_bo(obj);
  258. return drm_prime_pages_to_sg(bo->pages, bo->base.size >> PAGE_SHIFT);
  259. }
  260. static struct drm_gem_object* vgem_prime_import(struct drm_device *dev,
  261. struct dma_buf *dma_buf)
  262. {
  263. struct vgem_device *vgem = container_of(dev, typeof(*vgem), drm);
  264. return drm_gem_prime_import_dev(dev, dma_buf, &vgem->platform->dev);
  265. }
  266. static struct drm_gem_object *vgem_prime_import_sg_table(struct drm_device *dev,
  267. struct dma_buf_attachment *attach, struct sg_table *sg)
  268. {
  269. struct drm_vgem_gem_object *obj;
  270. int npages;
  271. obj = __vgem_gem_create(dev, attach->dmabuf->size);
  272. if (IS_ERR(obj))
  273. return ERR_CAST(obj);
  274. npages = PAGE_ALIGN(attach->dmabuf->size) / PAGE_SIZE;
  275. obj->table = sg;
  276. obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
  277. if (!obj->pages) {
  278. __vgem_gem_destroy(obj);
  279. return ERR_PTR(-ENOMEM);
  280. }
  281. obj->pages_pin_count++; /* perma-pinned */
  282. drm_prime_sg_to_page_addr_arrays(obj->table, obj->pages, NULL,
  283. npages);
  284. return &obj->base;
  285. }
  286. static void *vgem_prime_vmap(struct drm_gem_object *obj)
  287. {
  288. struct drm_vgem_gem_object *bo = to_vgem_bo(obj);
  289. long n_pages = obj->size >> PAGE_SHIFT;
  290. struct page **pages;
  291. pages = vgem_pin_pages(bo);
  292. if (IS_ERR(pages))
  293. return NULL;
  294. return vmap(pages, n_pages, 0, pgprot_writecombine(PAGE_KERNEL));
  295. }
  296. static void vgem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
  297. {
  298. struct drm_vgem_gem_object *bo = to_vgem_bo(obj);
  299. vunmap(vaddr);
  300. vgem_unpin_pages(bo);
  301. }
  302. static int vgem_prime_mmap(struct drm_gem_object *obj,
  303. struct vm_area_struct *vma)
  304. {
  305. int ret;
  306. if (obj->size < vma->vm_end - vma->vm_start)
  307. return -EINVAL;
  308. if (!obj->filp)
  309. return -ENODEV;
  310. ret = call_mmap(obj->filp, vma);
  311. if (ret)
  312. return ret;
  313. fput(vma->vm_file);
  314. vma->vm_file = get_file(obj->filp);
  315. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  316. vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  317. return 0;
  318. }
  319. static void vgem_release(struct drm_device *dev)
  320. {
  321. struct vgem_device *vgem = container_of(dev, typeof(*vgem), drm);
  322. platform_device_unregister(vgem->platform);
  323. drm_dev_fini(&vgem->drm);
  324. kfree(vgem);
  325. }
  326. static struct drm_driver vgem_driver = {
  327. .driver_features = DRIVER_GEM | DRIVER_PRIME,
  328. .release = vgem_release,
  329. .open = vgem_open,
  330. .postclose = vgem_postclose,
  331. .gem_free_object_unlocked = vgem_gem_free_object,
  332. .gem_vm_ops = &vgem_gem_vm_ops,
  333. .ioctls = vgem_ioctls,
  334. .num_ioctls = ARRAY_SIZE(vgem_ioctls),
  335. .fops = &vgem_driver_fops,
  336. .dumb_create = vgem_gem_dumb_create,
  337. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  338. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  339. .gem_prime_pin = vgem_prime_pin,
  340. .gem_prime_unpin = vgem_prime_unpin,
  341. .gem_prime_import = vgem_prime_import,
  342. .gem_prime_export = drm_gem_prime_export,
  343. .gem_prime_import_sg_table = vgem_prime_import_sg_table,
  344. .gem_prime_get_sg_table = vgem_prime_get_sg_table,
  345. .gem_prime_vmap = vgem_prime_vmap,
  346. .gem_prime_vunmap = vgem_prime_vunmap,
  347. .gem_prime_mmap = vgem_prime_mmap,
  348. .name = DRIVER_NAME,
  349. .desc = DRIVER_DESC,
  350. .date = DRIVER_DATE,
  351. .major = DRIVER_MAJOR,
  352. .minor = DRIVER_MINOR,
  353. };
  354. static int __init vgem_init(void)
  355. {
  356. int ret;
  357. vgem_device = kzalloc(sizeof(*vgem_device), GFP_KERNEL);
  358. if (!vgem_device)
  359. return -ENOMEM;
  360. vgem_device->platform =
  361. platform_device_register_simple("vgem", -1, NULL, 0);
  362. if (IS_ERR(vgem_device->platform)) {
  363. ret = PTR_ERR(vgem_device->platform);
  364. goto out_free;
  365. }
  366. dma_coerce_mask_and_coherent(&vgem_device->platform->dev,
  367. DMA_BIT_MASK(64));
  368. ret = drm_dev_init(&vgem_device->drm, &vgem_driver,
  369. &vgem_device->platform->dev);
  370. if (ret)
  371. goto out_unregister;
  372. /* Final step: expose the device/driver to userspace */
  373. ret = drm_dev_register(&vgem_device->drm, 0);
  374. if (ret)
  375. goto out_fini;
  376. return 0;
  377. out_fini:
  378. drm_dev_fini(&vgem_device->drm);
  379. out_unregister:
  380. platform_device_unregister(vgem_device->platform);
  381. out_free:
  382. kfree(vgem_device);
  383. return ret;
  384. }
  385. static void __exit vgem_exit(void)
  386. {
  387. drm_dev_unregister(&vgem_device->drm);
  388. drm_dev_unref(&vgem_device->drm);
  389. }
  390. module_init(vgem_init);
  391. module_exit(vgem_exit);
  392. MODULE_AUTHOR("Red Hat, Inc.");
  393. MODULE_AUTHOR("Intel Corporation");
  394. MODULE_DESCRIPTION(DRIVER_DESC);
  395. MODULE_LICENSE("GPL and additional rights");