armada_gem.c 14 KB

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