armada_gem.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Russell King
  4. */
  5. #include <linux/dma-buf.h>
  6. #include <linux/dma-mapping.h>
  7. #include <linux/mman.h>
  8. #include <linux/shmem_fs.h>
  9. #include <drm/armada_drm.h>
  10. #include <drm/drm_prime.h>
  11. #include <drm/drm_print.h>
  12. #include "armada_drm.h"
  13. #include "armada_gem.h"
  14. #include "armada_ioctlP.h"
  15. MODULE_IMPORT_NS("DMA_BUF");
  16. static vm_fault_t armada_gem_vm_fault(struct vm_fault *vmf)
  17. {
  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;
  21. pfn += (vmf->address - vmf->vma->vm_start) >> PAGE_SHIFT;
  22. return vmf_insert_pfn(vmf->vma, vmf->address, pfn);
  23. }
  24. static const struct vm_operations_struct armada_gem_vm_ops = {
  25. .fault = armada_gem_vm_fault,
  26. .open = drm_gem_vm_open,
  27. .close = drm_gem_vm_close,
  28. };
  29. static size_t roundup_gem_size(size_t size)
  30. {
  31. return roundup(size, PAGE_SIZE);
  32. }
  33. void armada_gem_free_object(struct drm_gem_object *obj)
  34. {
  35. struct armada_gem_object *dobj = drm_to_armada_gem(obj);
  36. struct armada_private *priv = drm_to_armada_dev(obj->dev);
  37. DRM_DEBUG_DRIVER("release obj %p\n", dobj);
  38. drm_gem_free_mmap_offset(&dobj->obj);
  39. might_lock(&priv->linear_lock);
  40. if (dobj->page) {
  41. /* page backed memory */
  42. unsigned int order = get_order(dobj->obj.size);
  43. __free_pages(dobj->page, order);
  44. } else if (dobj->linear) {
  45. /* linear backed memory */
  46. mutex_lock(&priv->linear_lock);
  47. drm_mm_remove_node(dobj->linear);
  48. mutex_unlock(&priv->linear_lock);
  49. kfree(dobj->linear);
  50. if (dobj->addr)
  51. iounmap(dobj->addr);
  52. }
  53. if (dobj->obj.import_attach) {
  54. /* We only ever display imported data */
  55. if (dobj->sgt)
  56. dma_buf_unmap_attachment_unlocked(dobj->obj.import_attach,
  57. dobj->sgt, DMA_TO_DEVICE);
  58. drm_prime_gem_destroy(&dobj->obj, NULL);
  59. }
  60. drm_gem_object_release(&dobj->obj);
  61. kfree(dobj);
  62. }
  63. int
  64. armada_gem_linear_back(struct drm_device *dev, struct armada_gem_object *obj)
  65. {
  66. struct armada_private *priv = drm_to_armada_dev(dev);
  67. size_t size = obj->obj.size;
  68. if (obj->page || obj->linear)
  69. return 0;
  70. /*
  71. * If it is a small allocation (typically cursor, which will
  72. * be 32x64 or 64x32 ARGB pixels) try to get it from the system.
  73. * Framebuffers will never be this small (our minimum size for
  74. * framebuffers is larger than this anyway.) Such objects are
  75. * only accessed by the CPU so we don't need any special handing
  76. * here.
  77. */
  78. if (size <= 8192) {
  79. unsigned int order = get_order(size);
  80. struct page *p = alloc_pages(GFP_KERNEL, order);
  81. if (p) {
  82. obj->addr = page_address(p);
  83. obj->phys_addr = page_to_phys(p);
  84. obj->page = p;
  85. memset(obj->addr, 0, PAGE_ALIGN(size));
  86. }
  87. }
  88. /*
  89. * We could grab something from DMA if it's enabled, but that
  90. * involves building in a problem:
  91. *
  92. * GEM DMA helper interface uses dma_alloc_coherent(), which provides
  93. * us with an CPU virtual address and a device address.
  94. *
  95. * The CPU virtual address may be either an address in the kernel
  96. * direct mapped region (for example, as it would be on x86) or
  97. * it may be remapped into another part of kernel memory space
  98. * (eg, as it would be on ARM.) This means virt_to_phys() on the
  99. * returned virtual address is invalid depending on the architecture
  100. * implementation.
  101. *
  102. * The device address may also not be a physical address; it may
  103. * be that there is some kind of remapping between the device and
  104. * system RAM, which makes the use of the device address also
  105. * unsafe to re-use as a physical address.
  106. *
  107. * This makes DRM usage of dma_alloc_coherent() in a generic way
  108. * at best very questionable and unsafe.
  109. */
  110. /* Otherwise, grab it from our linear allocation */
  111. if (!obj->page) {
  112. struct drm_mm_node *node;
  113. unsigned align = min_t(unsigned, size, SZ_2M);
  114. void __iomem *ptr;
  115. int ret;
  116. node = kzalloc_obj(*node);
  117. if (!node)
  118. return -ENOSPC;
  119. mutex_lock(&priv->linear_lock);
  120. ret = drm_mm_insert_node_generic(&priv->linear, node,
  121. size, align, 0, 0);
  122. mutex_unlock(&priv->linear_lock);
  123. if (ret) {
  124. kfree(node);
  125. return ret;
  126. }
  127. obj->linear = node;
  128. /* Ensure that the memory we're returning is cleared. */
  129. ptr = ioremap_wc(obj->linear->start, size);
  130. if (!ptr) {
  131. mutex_lock(&priv->linear_lock);
  132. drm_mm_remove_node(obj->linear);
  133. mutex_unlock(&priv->linear_lock);
  134. kfree(obj->linear);
  135. obj->linear = NULL;
  136. return -ENOMEM;
  137. }
  138. memset_io(ptr, 0, size);
  139. iounmap(ptr);
  140. obj->phys_addr = obj->linear->start;
  141. obj->dev_addr = obj->linear->start;
  142. obj->mapped = true;
  143. }
  144. DRM_DEBUG_DRIVER("obj %p phys %#llx dev %#llx\n", obj,
  145. (unsigned long long)obj->phys_addr,
  146. (unsigned long long)obj->dev_addr);
  147. return 0;
  148. }
  149. void *
  150. armada_gem_map_object(struct drm_device *dev, struct armada_gem_object *dobj)
  151. {
  152. /* only linear objects need to be ioremap'd */
  153. if (!dobj->addr && dobj->linear)
  154. dobj->addr = ioremap_wc(dobj->phys_addr, dobj->obj.size);
  155. return dobj->addr;
  156. }
  157. static const struct drm_gem_object_funcs armada_gem_object_funcs = {
  158. .free = armada_gem_free_object,
  159. .export = armada_gem_prime_export,
  160. .vm_ops = &armada_gem_vm_ops,
  161. };
  162. struct armada_gem_object *
  163. armada_gem_alloc_private_object(struct drm_device *dev, size_t size)
  164. {
  165. struct armada_gem_object *obj;
  166. size = roundup_gem_size(size);
  167. obj = kzalloc_obj(*obj);
  168. if (!obj)
  169. return NULL;
  170. obj->obj.funcs = &armada_gem_object_funcs;
  171. drm_gem_private_object_init(dev, &obj->obj, size);
  172. DRM_DEBUG_DRIVER("alloc private obj %p size %zu\n", obj, size);
  173. return obj;
  174. }
  175. static struct armada_gem_object *armada_gem_alloc_object(struct drm_device *dev,
  176. size_t size)
  177. {
  178. struct armada_gem_object *obj;
  179. struct address_space *mapping;
  180. size = roundup_gem_size(size);
  181. obj = kzalloc_obj(*obj);
  182. if (!obj)
  183. return NULL;
  184. obj->obj.funcs = &armada_gem_object_funcs;
  185. if (drm_gem_object_init(dev, &obj->obj, size)) {
  186. kfree(obj);
  187. return NULL;
  188. }
  189. mapping = obj->obj.filp->f_mapping;
  190. mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
  191. DRM_DEBUG_DRIVER("alloc obj %p size %zu\n", obj, size);
  192. return obj;
  193. }
  194. /* Dumb alloc support */
  195. int armada_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
  196. struct drm_mode_create_dumb *args)
  197. {
  198. struct armada_gem_object *dobj;
  199. u32 handle;
  200. size_t size;
  201. int ret;
  202. args->pitch = armada_pitch(args->width, args->bpp);
  203. args->size = size = args->pitch * args->height;
  204. dobj = armada_gem_alloc_private_object(dev, size);
  205. if (dobj == NULL)
  206. return -ENOMEM;
  207. ret = armada_gem_linear_back(dev, dobj);
  208. if (ret)
  209. goto err;
  210. ret = drm_gem_handle_create(file, &dobj->obj, &handle);
  211. if (ret)
  212. goto err;
  213. args->handle = handle;
  214. /* drop reference from allocate - handle holds it now */
  215. DRM_DEBUG_DRIVER("obj %p size %zu handle %#x\n", dobj, size, handle);
  216. err:
  217. drm_gem_object_put(&dobj->obj);
  218. return ret;
  219. }
  220. /* Private driver gem ioctls */
  221. int armada_gem_create_ioctl(struct drm_device *dev, void *data,
  222. struct drm_file *file)
  223. {
  224. struct drm_armada_gem_create *args = data;
  225. struct armada_gem_object *dobj;
  226. size_t size;
  227. u32 handle;
  228. int ret;
  229. if (args->size == 0)
  230. return -ENOMEM;
  231. size = args->size;
  232. dobj = armada_gem_alloc_object(dev, size);
  233. if (dobj == NULL)
  234. return -ENOMEM;
  235. ret = drm_gem_handle_create(file, &dobj->obj, &handle);
  236. if (ret)
  237. goto err;
  238. args->handle = handle;
  239. /* drop reference from allocate - handle holds it now */
  240. DRM_DEBUG_DRIVER("obj %p size %zu handle %#x\n", dobj, size, handle);
  241. err:
  242. drm_gem_object_put(&dobj->obj);
  243. return ret;
  244. }
  245. /* Map a shmem-backed object into process memory space */
  246. int armada_gem_mmap_ioctl(struct drm_device *dev, void *data,
  247. struct drm_file *file)
  248. {
  249. struct drm_armada_gem_mmap *args = data;
  250. struct armada_gem_object *dobj;
  251. unsigned long addr;
  252. dobj = armada_gem_object_lookup(file, args->handle);
  253. if (dobj == NULL)
  254. return -ENOENT;
  255. if (!dobj->obj.filp) {
  256. drm_gem_object_put(&dobj->obj);
  257. return -EINVAL;
  258. }
  259. addr = vm_mmap(dobj->obj.filp, 0, args->size, PROT_READ | PROT_WRITE,
  260. MAP_SHARED, args->offset);
  261. drm_gem_object_put(&dobj->obj);
  262. if (IS_ERR_VALUE(addr))
  263. return addr;
  264. args->addr = addr;
  265. return 0;
  266. }
  267. int armada_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  268. struct drm_file *file)
  269. {
  270. struct drm_armada_gem_pwrite *args = data;
  271. struct armada_gem_object *dobj;
  272. char __user *ptr;
  273. int ret = 0;
  274. DRM_DEBUG_DRIVER("handle %u off %u size %u ptr 0x%llx\n",
  275. args->handle, args->offset, args->size, args->ptr);
  276. if (args->size == 0)
  277. return 0;
  278. ptr = (char __user *)(uintptr_t)args->ptr;
  279. if (!access_ok(ptr, args->size))
  280. return -EFAULT;
  281. if (fault_in_readable(ptr, args->size))
  282. return -EFAULT;
  283. dobj = armada_gem_object_lookup(file, args->handle);
  284. if (dobj == NULL)
  285. return -ENOENT;
  286. /* Must be a kernel-mapped object */
  287. if (!dobj->addr)
  288. return -EINVAL;
  289. if (args->offset > dobj->obj.size ||
  290. args->size > dobj->obj.size - args->offset) {
  291. DRM_ERROR("invalid size: object size %u\n", dobj->obj.size);
  292. ret = -EINVAL;
  293. goto unref;
  294. }
  295. if (copy_from_user(dobj->addr + args->offset, ptr, args->size)) {
  296. ret = -EFAULT;
  297. } else if (dobj->update) {
  298. dobj->update(dobj->update_data);
  299. ret = 0;
  300. }
  301. unref:
  302. drm_gem_object_put(&dobj->obj);
  303. return ret;
  304. }
  305. /* Prime support */
  306. static struct sg_table *
  307. armada_gem_prime_map_dma_buf(struct dma_buf_attachment *attach,
  308. enum dma_data_direction dir)
  309. {
  310. struct drm_gem_object *obj = attach->dmabuf->priv;
  311. struct armada_gem_object *dobj = drm_to_armada_gem(obj);
  312. struct scatterlist *sg;
  313. struct sg_table *sgt;
  314. int i;
  315. sgt = kmalloc_obj(*sgt);
  316. if (!sgt)
  317. return NULL;
  318. if (dobj->obj.filp) {
  319. struct address_space *mapping;
  320. int count;
  321. count = dobj->obj.size / PAGE_SIZE;
  322. if (sg_alloc_table(sgt, count, GFP_KERNEL))
  323. goto free_sgt;
  324. mapping = dobj->obj.filp->f_mapping;
  325. for_each_sgtable_sg(sgt, sg, i) {
  326. struct page *page;
  327. page = shmem_read_mapping_page(mapping, i);
  328. if (IS_ERR(page))
  329. goto release;
  330. sg_set_page(sg, page, PAGE_SIZE, 0);
  331. }
  332. if (dma_map_sgtable(attach->dev, sgt, dir, 0))
  333. goto release;
  334. } else if (dobj->page) {
  335. /* Single contiguous page */
  336. if (sg_alloc_table(sgt, 1, GFP_KERNEL))
  337. goto free_sgt;
  338. sg_set_page(sgt->sgl, dobj->page, dobj->obj.size, 0);
  339. if (dma_map_sgtable(attach->dev, sgt, dir, 0))
  340. goto free_table;
  341. } else if (dobj->linear) {
  342. /* Single contiguous physical region - no struct page */
  343. if (sg_alloc_table(sgt, 1, GFP_KERNEL))
  344. goto free_sgt;
  345. sg_dma_address(sgt->sgl) = dobj->dev_addr;
  346. sg_dma_len(sgt->sgl) = dobj->obj.size;
  347. } else {
  348. goto free_sgt;
  349. }
  350. return sgt;
  351. release:
  352. for_each_sgtable_sg(sgt, sg, i)
  353. if (sg_page(sg))
  354. put_page(sg_page(sg));
  355. free_table:
  356. sg_free_table(sgt);
  357. free_sgt:
  358. kfree(sgt);
  359. return NULL;
  360. }
  361. static void armada_gem_prime_unmap_dma_buf(struct dma_buf_attachment *attach,
  362. struct sg_table *sgt, enum dma_data_direction dir)
  363. {
  364. struct drm_gem_object *obj = attach->dmabuf->priv;
  365. struct armada_gem_object *dobj = drm_to_armada_gem(obj);
  366. int i;
  367. if (!dobj->linear)
  368. dma_unmap_sgtable(attach->dev, sgt, dir, 0);
  369. if (dobj->obj.filp) {
  370. struct scatterlist *sg;
  371. for_each_sgtable_sg(sgt, sg, i)
  372. put_page(sg_page(sg));
  373. }
  374. sg_free_table(sgt);
  375. kfree(sgt);
  376. }
  377. static int
  378. armada_gem_dmabuf_mmap(struct dma_buf *buf, struct vm_area_struct *vma)
  379. {
  380. return -EINVAL;
  381. }
  382. static const struct dma_buf_ops armada_gem_prime_dmabuf_ops = {
  383. .map_dma_buf = armada_gem_prime_map_dma_buf,
  384. .unmap_dma_buf = armada_gem_prime_unmap_dma_buf,
  385. .release = drm_gem_dmabuf_release,
  386. .mmap = armada_gem_dmabuf_mmap,
  387. };
  388. struct dma_buf *
  389. armada_gem_prime_export(struct drm_gem_object *obj, int flags)
  390. {
  391. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  392. exp_info.ops = &armada_gem_prime_dmabuf_ops;
  393. exp_info.size = obj->size;
  394. exp_info.flags = O_RDWR;
  395. exp_info.priv = obj;
  396. return drm_gem_dmabuf_export(obj->dev, &exp_info);
  397. }
  398. struct drm_gem_object *
  399. armada_gem_prime_import(struct drm_device *dev, struct dma_buf *buf)
  400. {
  401. struct dma_buf_attachment *attach;
  402. struct armada_gem_object *dobj;
  403. if (buf->ops == &armada_gem_prime_dmabuf_ops) {
  404. struct drm_gem_object *obj = buf->priv;
  405. if (obj->dev == dev) {
  406. /*
  407. * Importing our own dmabuf(s) increases the
  408. * refcount on the gem object itself.
  409. */
  410. drm_gem_object_get(obj);
  411. return obj;
  412. }
  413. }
  414. attach = dma_buf_attach(buf, dev->dev);
  415. if (IS_ERR(attach))
  416. return ERR_CAST(attach);
  417. dobj = armada_gem_alloc_private_object(dev, buf->size);
  418. if (!dobj) {
  419. dma_buf_detach(buf, attach);
  420. return ERR_PTR(-ENOMEM);
  421. }
  422. dobj->obj.import_attach = attach;
  423. get_dma_buf(buf);
  424. /*
  425. * Don't call dma_buf_map_attachment() here - it maps the
  426. * scatterlist immediately for DMA, and this is not always
  427. * an appropriate thing to do.
  428. */
  429. return &dobj->obj;
  430. }
  431. int armada_gem_map_import(struct armada_gem_object *dobj)
  432. {
  433. int ret;
  434. dobj->sgt = dma_buf_map_attachment_unlocked(dobj->obj.import_attach,
  435. DMA_TO_DEVICE);
  436. if (IS_ERR(dobj->sgt)) {
  437. ret = PTR_ERR(dobj->sgt);
  438. dobj->sgt = NULL;
  439. DRM_ERROR("dma_buf_map_attachment() error: %d\n", ret);
  440. return ret;
  441. }
  442. if (dobj->sgt->nents > 1) {
  443. DRM_ERROR("dma_buf_map_attachment() returned an (unsupported) scattered list\n");
  444. return -EINVAL;
  445. }
  446. if (sg_dma_len(dobj->sgt->sgl) < dobj->obj.size) {
  447. DRM_ERROR("dma_buf_map_attachment() returned a small buffer\n");
  448. return -EINVAL;
  449. }
  450. dobj->dev_addr = sg_dma_address(dobj->sgt->sgl);
  451. dobj->mapped = true;
  452. return 0;
  453. }