exynos_drm_gem.c 15 KB

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