exynos_drm_gem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /* exynos_drm_gem.c
  2. *
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * Author: Inki Dae <inki.dae@samsung.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <drm/drmP.h>
  12. #include <drm/drm_vma_manager.h>
  13. #include <linux/shmem_fs.h>
  14. #include <linux/dma-buf.h>
  15. #include <linux/pfn_t.h>
  16. #include <drm/exynos_drm.h>
  17. #include "exynos_drm_drv.h"
  18. #include "exynos_drm_gem.h"
  19. #include "exynos_drm_iommu.h"
  20. static int exynos_drm_alloc_buf(struct exynos_drm_gem *exynos_gem)
  21. {
  22. struct drm_device *dev = exynos_gem->base.dev;
  23. unsigned long attr;
  24. unsigned int nr_pages;
  25. struct sg_table sgt;
  26. int ret = -ENOMEM;
  27. if (exynos_gem->dma_addr) {
  28. DRM_DEBUG_KMS("already allocated.\n");
  29. return 0;
  30. }
  31. exynos_gem->dma_attrs = 0;
  32. /*
  33. * if EXYNOS_BO_CONTIG, fully physically contiguous memory
  34. * region will be allocated else physically contiguous
  35. * as possible.
  36. */
  37. if (!(exynos_gem->flags & EXYNOS_BO_NONCONTIG))
  38. exynos_gem->dma_attrs |= DMA_ATTR_FORCE_CONTIGUOUS;
  39. /*
  40. * if EXYNOS_BO_WC or EXYNOS_BO_NONCACHABLE, writecombine mapping
  41. * else cachable mapping.
  42. */
  43. if (exynos_gem->flags & EXYNOS_BO_WC ||
  44. !(exynos_gem->flags & EXYNOS_BO_CACHABLE))
  45. attr = DMA_ATTR_WRITE_COMBINE;
  46. else
  47. attr = DMA_ATTR_NON_CONSISTENT;
  48. exynos_gem->dma_attrs |= attr;
  49. exynos_gem->dma_attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
  50. nr_pages = exynos_gem->size >> PAGE_SHIFT;
  51. exynos_gem->pages = kvmalloc_array(nr_pages, sizeof(struct page *),
  52. GFP_KERNEL | __GFP_ZERO);
  53. if (!exynos_gem->pages) {
  54. DRM_ERROR("failed to allocate pages.\n");
  55. return -ENOMEM;
  56. }
  57. exynos_gem->cookie = dma_alloc_attrs(to_dma_dev(dev), exynos_gem->size,
  58. &exynos_gem->dma_addr, GFP_KERNEL,
  59. exynos_gem->dma_attrs);
  60. if (!exynos_gem->cookie) {
  61. DRM_ERROR("failed to allocate buffer.\n");
  62. goto err_free;
  63. }
  64. ret = dma_get_sgtable_attrs(to_dma_dev(dev), &sgt, exynos_gem->cookie,
  65. exynos_gem->dma_addr, exynos_gem->size,
  66. exynos_gem->dma_attrs);
  67. if (ret < 0) {
  68. DRM_ERROR("failed to get sgtable.\n");
  69. goto err_dma_free;
  70. }
  71. if (drm_prime_sg_to_page_addr_arrays(&sgt, exynos_gem->pages, NULL,
  72. nr_pages)) {
  73. DRM_ERROR("invalid sgtable.\n");
  74. ret = -EINVAL;
  75. goto err_sgt_free;
  76. }
  77. sg_free_table(&sgt);
  78. DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
  79. (unsigned long)exynos_gem->dma_addr, exynos_gem->size);
  80. return 0;
  81. err_sgt_free:
  82. sg_free_table(&sgt);
  83. err_dma_free:
  84. dma_free_attrs(to_dma_dev(dev), exynos_gem->size, exynos_gem->cookie,
  85. exynos_gem->dma_addr, exynos_gem->dma_attrs);
  86. err_free:
  87. kvfree(exynos_gem->pages);
  88. return ret;
  89. }
  90. static void exynos_drm_free_buf(struct exynos_drm_gem *exynos_gem)
  91. {
  92. struct drm_device *dev = exynos_gem->base.dev;
  93. if (!exynos_gem->dma_addr) {
  94. DRM_DEBUG_KMS("dma_addr is invalid.\n");
  95. return;
  96. }
  97. DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
  98. (unsigned long)exynos_gem->dma_addr, exynos_gem->size);
  99. dma_free_attrs(to_dma_dev(dev), exynos_gem->size, exynos_gem->cookie,
  100. (dma_addr_t)exynos_gem->dma_addr,
  101. exynos_gem->dma_attrs);
  102. kvfree(exynos_gem->pages);
  103. }
  104. static int exynos_drm_gem_handle_create(struct drm_gem_object *obj,
  105. struct drm_file *file_priv,
  106. unsigned int *handle)
  107. {
  108. int ret;
  109. /*
  110. * allocate a id of idr table where the obj is registered
  111. * and handle has the id what user can see.
  112. */
  113. ret = drm_gem_handle_create(file_priv, obj, handle);
  114. if (ret)
  115. return ret;
  116. DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);
  117. /* drop reference from allocate - handle holds it now. */
  118. drm_gem_object_unreference_unlocked(obj);
  119. return 0;
  120. }
  121. void exynos_drm_gem_destroy(struct exynos_drm_gem *exynos_gem)
  122. {
  123. struct drm_gem_object *obj = &exynos_gem->base;
  124. DRM_DEBUG_KMS("handle count = %d\n", obj->handle_count);
  125. /*
  126. * do not release memory region from exporter.
  127. *
  128. * the region will be released by exporter
  129. * once dmabuf's refcount becomes 0.
  130. */
  131. if (obj->import_attach)
  132. drm_prime_gem_destroy(obj, exynos_gem->sgt);
  133. else
  134. exynos_drm_free_buf(exynos_gem);
  135. /* release file pointer to gem object. */
  136. drm_gem_object_release(obj);
  137. kfree(exynos_gem);
  138. }
  139. unsigned long exynos_drm_gem_get_size(struct drm_device *dev,
  140. unsigned int gem_handle,
  141. struct drm_file *file_priv)
  142. {
  143. struct exynos_drm_gem *exynos_gem;
  144. struct drm_gem_object *obj;
  145. obj = drm_gem_object_lookup(file_priv, gem_handle);
  146. if (!obj) {
  147. DRM_ERROR("failed to lookup gem object.\n");
  148. return 0;
  149. }
  150. exynos_gem = to_exynos_gem(obj);
  151. drm_gem_object_unreference_unlocked(obj);
  152. return exynos_gem->size;
  153. }
  154. static struct exynos_drm_gem *exynos_drm_gem_init(struct drm_device *dev,
  155. unsigned long size)
  156. {
  157. struct exynos_drm_gem *exynos_gem;
  158. struct drm_gem_object *obj;
  159. int ret;
  160. exynos_gem = kzalloc(sizeof(*exynos_gem), GFP_KERNEL);
  161. if (!exynos_gem)
  162. return ERR_PTR(-ENOMEM);
  163. exynos_gem->size = size;
  164. obj = &exynos_gem->base;
  165. ret = drm_gem_object_init(dev, obj, size);
  166. if (ret < 0) {
  167. DRM_ERROR("failed to initialize gem object\n");
  168. kfree(exynos_gem);
  169. return ERR_PTR(ret);
  170. }
  171. ret = drm_gem_create_mmap_offset(obj);
  172. if (ret < 0) {
  173. drm_gem_object_release(obj);
  174. kfree(exynos_gem);
  175. return ERR_PTR(ret);
  176. }
  177. DRM_DEBUG_KMS("created file object = %pK\n", obj->filp);
  178. return exynos_gem;
  179. }
  180. struct exynos_drm_gem *exynos_drm_gem_create(struct drm_device *dev,
  181. unsigned int flags,
  182. unsigned long size)
  183. {
  184. struct exynos_drm_gem *exynos_gem;
  185. int ret;
  186. if (flags & ~(EXYNOS_BO_MASK)) {
  187. DRM_ERROR("invalid GEM buffer flags: %u\n", flags);
  188. return ERR_PTR(-EINVAL);
  189. }
  190. if (!size) {
  191. DRM_ERROR("invalid GEM buffer size: %lu\n", size);
  192. return ERR_PTR(-EINVAL);
  193. }
  194. size = roundup(size, PAGE_SIZE);
  195. exynos_gem = exynos_drm_gem_init(dev, size);
  196. if (IS_ERR(exynos_gem))
  197. return exynos_gem;
  198. if (!is_drm_iommu_supported(dev) && (flags & EXYNOS_BO_NONCONTIG)) {
  199. /*
  200. * when no IOMMU is available, all allocated buffers are
  201. * contiguous anyway, so drop EXYNOS_BO_NONCONTIG flag
  202. */
  203. flags &= ~EXYNOS_BO_NONCONTIG;
  204. DRM_WARN("Non-contiguous allocation is not supported without IOMMU, falling back to contiguous buffer\n");
  205. }
  206. /* set memory type and cache attribute from user side. */
  207. exynos_gem->flags = flags;
  208. ret = exynos_drm_alloc_buf(exynos_gem);
  209. if (ret < 0) {
  210. drm_gem_object_release(&exynos_gem->base);
  211. kfree(exynos_gem);
  212. return ERR_PTR(ret);
  213. }
  214. return exynos_gem;
  215. }
  216. int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
  217. struct drm_file *file_priv)
  218. {
  219. struct drm_exynos_gem_create *args = data;
  220. struct exynos_drm_gem *exynos_gem;
  221. int ret;
  222. exynos_gem = exynos_drm_gem_create(dev, args->flags, args->size);
  223. if (IS_ERR(exynos_gem))
  224. return PTR_ERR(exynos_gem);
  225. ret = exynos_drm_gem_handle_create(&exynos_gem->base, file_priv,
  226. &args->handle);
  227. if (ret) {
  228. exynos_drm_gem_destroy(exynos_gem);
  229. return ret;
  230. }
  231. return 0;
  232. }
  233. int exynos_drm_gem_map_ioctl(struct drm_device *dev, void *data,
  234. struct drm_file *file_priv)
  235. {
  236. struct drm_exynos_gem_map *args = data;
  237. return drm_gem_dumb_map_offset(file_priv, dev, args->handle,
  238. &args->offset);
  239. }
  240. dma_addr_t *exynos_drm_gem_get_dma_addr(struct drm_device *dev,
  241. unsigned int gem_handle,
  242. struct drm_file *filp)
  243. {
  244. struct exynos_drm_gem *exynos_gem;
  245. struct drm_gem_object *obj;
  246. obj = drm_gem_object_lookup(filp, gem_handle);
  247. if (!obj) {
  248. DRM_ERROR("failed to lookup gem object.\n");
  249. return ERR_PTR(-EINVAL);
  250. }
  251. exynos_gem = to_exynos_gem(obj);
  252. return &exynos_gem->dma_addr;
  253. }
  254. void exynos_drm_gem_put_dma_addr(struct drm_device *dev,
  255. unsigned int gem_handle,
  256. struct drm_file *filp)
  257. {
  258. struct drm_gem_object *obj;
  259. obj = drm_gem_object_lookup(filp, gem_handle);
  260. if (!obj) {
  261. DRM_ERROR("failed to lookup gem object.\n");
  262. return;
  263. }
  264. drm_gem_object_unreference_unlocked(obj);
  265. /*
  266. * decrease obj->refcount one more time because we has already
  267. * increased it at exynos_drm_gem_get_dma_addr().
  268. */
  269. drm_gem_object_unreference_unlocked(obj);
  270. }
  271. static int exynos_drm_gem_mmap_buffer(struct exynos_drm_gem *exynos_gem,
  272. struct vm_area_struct *vma)
  273. {
  274. struct drm_device *drm_dev = exynos_gem->base.dev;
  275. unsigned long vm_size;
  276. int ret;
  277. vma->vm_flags &= ~VM_PFNMAP;
  278. vma->vm_pgoff = 0;
  279. vm_size = vma->vm_end - vma->vm_start;
  280. /* check if user-requested size is valid. */
  281. if (vm_size > exynos_gem->size)
  282. return -EINVAL;
  283. ret = dma_mmap_attrs(to_dma_dev(drm_dev), vma, exynos_gem->cookie,
  284. exynos_gem->dma_addr, exynos_gem->size,
  285. exynos_gem->dma_attrs);
  286. if (ret < 0) {
  287. DRM_ERROR("failed to mmap.\n");
  288. return ret;
  289. }
  290. return 0;
  291. }
  292. int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data,
  293. struct drm_file *file_priv)
  294. {
  295. struct exynos_drm_gem *exynos_gem;
  296. struct drm_exynos_gem_info *args = data;
  297. struct drm_gem_object *obj;
  298. obj = drm_gem_object_lookup(file_priv, args->handle);
  299. if (!obj) {
  300. DRM_ERROR("failed to lookup gem object.\n");
  301. return -EINVAL;
  302. }
  303. exynos_gem = to_exynos_gem(obj);
  304. args->flags = exynos_gem->flags;
  305. args->size = exynos_gem->size;
  306. drm_gem_object_unreference_unlocked(obj);
  307. return 0;
  308. }
  309. void exynos_drm_gem_free_object(struct drm_gem_object *obj)
  310. {
  311. exynos_drm_gem_destroy(to_exynos_gem(obj));
  312. }
  313. int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
  314. struct drm_device *dev,
  315. struct drm_mode_create_dumb *args)
  316. {
  317. struct exynos_drm_gem *exynos_gem;
  318. unsigned int flags;
  319. int ret;
  320. /*
  321. * allocate memory to be used for framebuffer.
  322. * - this callback would be called by user application
  323. * with DRM_IOCTL_MODE_CREATE_DUMB command.
  324. */
  325. args->pitch = args->width * ((args->bpp + 7) / 8);
  326. args->size = args->pitch * args->height;
  327. if (is_drm_iommu_supported(dev))
  328. flags = EXYNOS_BO_NONCONTIG | EXYNOS_BO_WC;
  329. else
  330. flags = EXYNOS_BO_CONTIG | EXYNOS_BO_WC;
  331. exynos_gem = exynos_drm_gem_create(dev, flags, args->size);
  332. if (IS_ERR(exynos_gem)) {
  333. dev_warn(dev->dev, "FB allocation failed.\n");
  334. return PTR_ERR(exynos_gem);
  335. }
  336. ret = exynos_drm_gem_handle_create(&exynos_gem->base, file_priv,
  337. &args->handle);
  338. if (ret) {
  339. exynos_drm_gem_destroy(exynos_gem);
  340. return ret;
  341. }
  342. return 0;
  343. }
  344. int exynos_drm_gem_fault(struct vm_fault *vmf)
  345. {
  346. struct vm_area_struct *vma = vmf->vma;
  347. struct drm_gem_object *obj = vma->vm_private_data;
  348. struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj);
  349. unsigned long pfn;
  350. pgoff_t page_offset;
  351. int ret;
  352. page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
  353. if (page_offset >= (exynos_gem->size >> PAGE_SHIFT)) {
  354. DRM_ERROR("invalid page offset\n");
  355. ret = -EINVAL;
  356. goto out;
  357. }
  358. pfn = page_to_pfn(exynos_gem->pages[page_offset]);
  359. ret = vm_insert_mixed(vma, vmf->address, __pfn_to_pfn_t(pfn, PFN_DEV));
  360. out:
  361. switch (ret) {
  362. case 0:
  363. case -ERESTARTSYS:
  364. case -EINTR:
  365. return VM_FAULT_NOPAGE;
  366. case -ENOMEM:
  367. return VM_FAULT_OOM;
  368. default:
  369. return VM_FAULT_SIGBUS;
  370. }
  371. }
  372. static int exynos_drm_gem_mmap_obj(struct drm_gem_object *obj,
  373. struct vm_area_struct *vma)
  374. {
  375. struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj);
  376. int ret;
  377. DRM_DEBUG_KMS("flags = 0x%x\n", exynos_gem->flags);
  378. /* non-cachable as default. */
  379. if (exynos_gem->flags & EXYNOS_BO_CACHABLE)
  380. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  381. else if (exynos_gem->flags & EXYNOS_BO_WC)
  382. vma->vm_page_prot =
  383. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  384. else
  385. vma->vm_page_prot =
  386. pgprot_noncached(vm_get_page_prot(vma->vm_flags));
  387. ret = exynos_drm_gem_mmap_buffer(exynos_gem, vma);
  388. if (ret)
  389. goto err_close_vm;
  390. return ret;
  391. err_close_vm:
  392. drm_gem_vm_close(vma);
  393. return ret;
  394. }
  395. int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  396. {
  397. struct drm_gem_object *obj;
  398. int ret;
  399. /* set vm_area_struct. */
  400. ret = drm_gem_mmap(filp, vma);
  401. if (ret < 0) {
  402. DRM_ERROR("failed to mmap.\n");
  403. return ret;
  404. }
  405. obj = vma->vm_private_data;
  406. if (obj->import_attach)
  407. return dma_buf_mmap(obj->dma_buf, vma, 0);
  408. return exynos_drm_gem_mmap_obj(obj, vma);
  409. }
  410. /* low-level interface prime helpers */
  411. struct sg_table *exynos_drm_gem_prime_get_sg_table(struct drm_gem_object *obj)
  412. {
  413. struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj);
  414. int npages;
  415. npages = exynos_gem->size >> PAGE_SHIFT;
  416. return drm_prime_pages_to_sg(exynos_gem->pages, npages);
  417. }
  418. struct drm_gem_object *
  419. exynos_drm_gem_prime_import_sg_table(struct drm_device *dev,
  420. struct dma_buf_attachment *attach,
  421. struct sg_table *sgt)
  422. {
  423. struct exynos_drm_gem *exynos_gem;
  424. int npages;
  425. int ret;
  426. exynos_gem = exynos_drm_gem_init(dev, attach->dmabuf->size);
  427. if (IS_ERR(exynos_gem)) {
  428. ret = PTR_ERR(exynos_gem);
  429. return ERR_PTR(ret);
  430. }
  431. exynos_gem->dma_addr = sg_dma_address(sgt->sgl);
  432. npages = exynos_gem->size >> PAGE_SHIFT;
  433. exynos_gem->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
  434. if (!exynos_gem->pages) {
  435. ret = -ENOMEM;
  436. goto err;
  437. }
  438. ret = drm_prime_sg_to_page_addr_arrays(sgt, exynos_gem->pages, NULL,
  439. npages);
  440. if (ret < 0)
  441. goto err_free_large;
  442. exynos_gem->sgt = sgt;
  443. if (sgt->nents == 1) {
  444. /* always physically continuous memory if sgt->nents is 1. */
  445. exynos_gem->flags |= EXYNOS_BO_CONTIG;
  446. } else {
  447. /*
  448. * this case could be CONTIG or NONCONTIG type but for now
  449. * sets NONCONTIG.
  450. * TODO. we have to find a way that exporter can notify
  451. * the type of its own buffer to importer.
  452. */
  453. exynos_gem->flags |= EXYNOS_BO_NONCONTIG;
  454. }
  455. return &exynos_gem->base;
  456. err_free_large:
  457. kvfree(exynos_gem->pages);
  458. err:
  459. drm_gem_object_release(&exynos_gem->base);
  460. kfree(exynos_gem);
  461. return ERR_PTR(ret);
  462. }
  463. void *exynos_drm_gem_prime_vmap(struct drm_gem_object *obj)
  464. {
  465. return NULL;
  466. }
  467. void exynos_drm_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
  468. {
  469. /* Nothing to do */
  470. }
  471. int exynos_drm_gem_prime_mmap(struct drm_gem_object *obj,
  472. struct vm_area_struct *vma)
  473. {
  474. int ret;
  475. ret = drm_gem_mmap_obj(obj, obj->size, vma);
  476. if (ret < 0)
  477. return ret;
  478. return exynos_drm_gem_mmap_obj(obj, vma);
  479. }