drm_gem_cma_helper.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * drm gem CMA (contiguous memory allocator) helper functions
  3. *
  4. * Copyright (C) 2012 Sascha Hauer, Pengutronix
  5. *
  6. * Based on Samsung Exynos code
  7. *
  8. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/mm.h>
  20. #include <linux/slab.h>
  21. #include <linux/mutex.h>
  22. #include <linux/export.h>
  23. #include <linux/dma-buf.h>
  24. #include <linux/dma-mapping.h>
  25. #include <drm/drmP.h>
  26. #include <drm/drm.h>
  27. #include <drm/drm_gem_cma_helper.h>
  28. static unsigned int get_gem_mmap_offset(struct drm_gem_object *obj)
  29. {
  30. return (unsigned int)obj->map_list.hash.key << PAGE_SHIFT;
  31. }
  32. /*
  33. * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
  34. * @drm: The drm device
  35. * @size: The GEM object size
  36. *
  37. * This function creates and initializes a GEM CMA object of the given size, but
  38. * doesn't allocate any memory to back the object.
  39. *
  40. * Return a struct drm_gem_cma_object* on success or ERR_PTR values on failure.
  41. */
  42. static struct drm_gem_cma_object *
  43. __drm_gem_cma_create(struct drm_device *drm, unsigned int size)
  44. {
  45. struct drm_gem_cma_object *cma_obj;
  46. struct drm_gem_object *gem_obj;
  47. int ret;
  48. cma_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
  49. if (!cma_obj)
  50. return ERR_PTR(-ENOMEM);
  51. gem_obj = &cma_obj->base;
  52. ret = drm_gem_object_init(drm, gem_obj, size);
  53. if (ret)
  54. goto error;
  55. ret = drm_gem_create_mmap_offset(gem_obj);
  56. if (ret) {
  57. drm_gem_object_release(gem_obj);
  58. goto error;
  59. }
  60. return cma_obj;
  61. error:
  62. kfree(cma_obj);
  63. return ERR_PTR(ret);
  64. }
  65. /*
  66. * drm_gem_cma_create - allocate an object with the given size
  67. *
  68. * returns a struct drm_gem_cma_object* on success or ERR_PTR values
  69. * on failure.
  70. */
  71. struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
  72. unsigned int size)
  73. {
  74. struct drm_gem_cma_object *cma_obj;
  75. struct sg_table *sgt = NULL;
  76. int ret;
  77. size = round_up(size, PAGE_SIZE);
  78. cma_obj = __drm_gem_cma_create(drm, size);
  79. if (IS_ERR(cma_obj))
  80. return cma_obj;
  81. cma_obj->vaddr = dma_alloc_writecombine(drm->dev, size,
  82. &cma_obj->paddr, GFP_KERNEL | __GFP_NOWARN);
  83. if (!cma_obj->vaddr) {
  84. dev_err(drm->dev, "failed to allocate buffer with size %d\n",
  85. size);
  86. ret = -ENOMEM;
  87. goto error;
  88. }
  89. sgt = kzalloc(sizeof(*cma_obj->sgt), GFP_KERNEL);
  90. if (sgt == NULL) {
  91. ret = -ENOMEM;
  92. goto error;
  93. }
  94. ret = dma_get_sgtable(drm->dev, sgt, cma_obj->vaddr,
  95. cma_obj->paddr, size);
  96. if (ret < 0)
  97. goto error;
  98. cma_obj->sgt = sgt;
  99. return cma_obj;
  100. error:
  101. kfree(sgt);
  102. drm_gem_cma_free_object(&cma_obj->base);
  103. return ERR_PTR(ret);
  104. }
  105. EXPORT_SYMBOL_GPL(drm_gem_cma_create);
  106. /*
  107. * drm_gem_cma_create_with_handle - allocate an object with the given
  108. * size and create a gem handle on it
  109. *
  110. * returns a struct drm_gem_cma_object* on success or ERR_PTR values
  111. * on failure.
  112. */
  113. static struct drm_gem_cma_object *drm_gem_cma_create_with_handle(
  114. struct drm_file *file_priv,
  115. struct drm_device *drm, unsigned int size,
  116. unsigned int *handle)
  117. {
  118. struct drm_gem_cma_object *cma_obj;
  119. struct drm_gem_object *gem_obj;
  120. int ret;
  121. cma_obj = drm_gem_cma_create(drm, size);
  122. if (IS_ERR(cma_obj))
  123. return cma_obj;
  124. gem_obj = &cma_obj->base;
  125. /*
  126. * allocate a id of idr table where the obj is registered
  127. * and handle has the id what user can see.
  128. */
  129. ret = drm_gem_handle_create(file_priv, gem_obj, handle);
  130. if (ret)
  131. goto err_handle_create;
  132. /* drop reference from allocate - handle holds it now. */
  133. drm_gem_object_unreference_unlocked(gem_obj);
  134. return cma_obj;
  135. err_handle_create:
  136. drm_gem_cma_free_object(gem_obj);
  137. return ERR_PTR(ret);
  138. }
  139. /*
  140. * drm_gem_cma_free_object - (struct drm_driver)->gem_free_object callback
  141. * function
  142. */
  143. void drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
  144. {
  145. struct drm_gem_cma_object *cma_obj;
  146. if (gem_obj->map_list.map)
  147. drm_gem_free_mmap_offset(gem_obj);
  148. cma_obj = to_drm_gem_cma_obj(gem_obj);
  149. if (cma_obj->vaddr) {
  150. dma_free_writecombine(gem_obj->dev->dev, cma_obj->base.size,
  151. cma_obj->vaddr, cma_obj->paddr);
  152. if (cma_obj->sgt) {
  153. sg_free_table(cma_obj->sgt);
  154. kfree(cma_obj->sgt);
  155. }
  156. } else if (gem_obj->import_attach) {
  157. drm_prime_gem_destroy(gem_obj, cma_obj->sgt);
  158. }
  159. drm_gem_object_release(gem_obj);
  160. kfree(cma_obj);
  161. }
  162. EXPORT_SYMBOL_GPL(drm_gem_cma_free_object);
  163. /*
  164. * drm_gem_cma_dumb_create - (struct drm_driver)->dumb_create callback
  165. * function
  166. *
  167. * This aligns the pitch and size arguments to the minimum required. wrap
  168. * this into your own function if you need bigger alignment.
  169. */
  170. int drm_gem_cma_dumb_create(struct drm_file *file_priv,
  171. struct drm_device *dev, struct drm_mode_create_dumb *args)
  172. {
  173. struct drm_gem_cma_object *cma_obj;
  174. int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
  175. if (args->pitch < min_pitch)
  176. args->pitch = min_pitch;
  177. if (args->size < args->pitch * args->height)
  178. args->size = args->pitch * args->height;
  179. cma_obj = drm_gem_cma_create_with_handle(file_priv, dev,
  180. args->size, &args->handle);
  181. return PTR_RET(cma_obj);
  182. }
  183. EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
  184. /*
  185. * drm_gem_cma_dumb_map_offset - (struct drm_driver)->dumb_map_offset callback
  186. * function
  187. */
  188. int drm_gem_cma_dumb_map_offset(struct drm_file *file_priv,
  189. struct drm_device *drm, uint32_t handle, uint64_t *offset)
  190. {
  191. struct drm_gem_object *gem_obj;
  192. mutex_lock(&drm->struct_mutex);
  193. gem_obj = drm_gem_object_lookup(drm, file_priv, handle);
  194. if (!gem_obj) {
  195. dev_err(drm->dev, "failed to lookup gem object\n");
  196. mutex_unlock(&drm->struct_mutex);
  197. return -EINVAL;
  198. }
  199. *offset = get_gem_mmap_offset(gem_obj);
  200. drm_gem_object_unreference(gem_obj);
  201. mutex_unlock(&drm->struct_mutex);
  202. return 0;
  203. }
  204. EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_map_offset);
  205. const struct vm_operations_struct drm_gem_cma_vm_ops = {
  206. .open = drm_gem_vm_open,
  207. .close = drm_gem_vm_close,
  208. };
  209. EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops);
  210. static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object *cma_obj,
  211. struct vm_area_struct *vma)
  212. {
  213. int ret;
  214. ret = remap_pfn_range(vma, vma->vm_start, cma_obj->paddr >> PAGE_SHIFT,
  215. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  216. if (ret)
  217. drm_gem_vm_close(vma);
  218. return ret;
  219. }
  220. /*
  221. * drm_gem_cma_mmap - (struct file_operation)->mmap callback function
  222. */
  223. int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma)
  224. {
  225. struct drm_gem_cma_object *cma_obj;
  226. struct drm_gem_object *gem_obj;
  227. int ret;
  228. ret = drm_gem_mmap(filp, vma);
  229. if (ret)
  230. return ret;
  231. gem_obj = vma->vm_private_data;
  232. cma_obj = to_drm_gem_cma_obj(gem_obj);
  233. return drm_gem_cma_mmap_obj(cma_obj, vma);
  234. }
  235. EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);
  236. /*
  237. * drm_gem_cma_dumb_destroy - (struct drm_driver)->dumb_destroy callback function
  238. */
  239. int drm_gem_cma_dumb_destroy(struct drm_file *file_priv,
  240. struct drm_device *drm, unsigned int handle)
  241. {
  242. return drm_gem_handle_delete(file_priv, handle);
  243. }
  244. EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_destroy);
  245. #ifdef CONFIG_DEBUG_FS
  246. void drm_gem_cma_describe(struct drm_gem_cma_object *cma_obj, struct seq_file *m)
  247. {
  248. struct drm_gem_object *obj = &cma_obj->base;
  249. struct drm_device *dev = obj->dev;
  250. uint64_t off = 0;
  251. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  252. if (obj->map_list.map)
  253. off = (uint64_t)obj->map_list.hash.key;
  254. seq_printf(m, "%2d (%2d) %08llx %08Zx %p %d",
  255. obj->name, obj->refcount.refcount.counter,
  256. off, cma_obj->paddr, cma_obj->vaddr, obj->size);
  257. seq_printf(m, "\n");
  258. }
  259. EXPORT_SYMBOL_GPL(drm_gem_cma_describe);
  260. #endif
  261. /* -----------------------------------------------------------------------------
  262. * DMA-BUF
  263. */
  264. struct drm_gem_cma_dmabuf_attachment {
  265. struct sg_table sgt;
  266. enum dma_data_direction dir;
  267. };
  268. static int drm_gem_cma_dmabuf_attach(struct dma_buf *dmabuf, struct device *dev,
  269. struct dma_buf_attachment *attach)
  270. {
  271. struct drm_gem_cma_dmabuf_attachment *cma_attach;
  272. cma_attach = kzalloc(sizeof(*cma_attach), GFP_KERNEL);
  273. if (!cma_attach)
  274. return -ENOMEM;
  275. cma_attach->dir = DMA_NONE;
  276. attach->priv = cma_attach;
  277. return 0;
  278. }
  279. static void drm_gem_cma_dmabuf_detach(struct dma_buf *dmabuf,
  280. struct dma_buf_attachment *attach)
  281. {
  282. struct drm_gem_cma_dmabuf_attachment *cma_attach = attach->priv;
  283. struct sg_table *sgt;
  284. if (cma_attach == NULL)
  285. return;
  286. sgt = &cma_attach->sgt;
  287. if (cma_attach->dir != DMA_NONE)
  288. dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
  289. cma_attach->dir);
  290. sg_free_table(sgt);
  291. kfree(cma_attach);
  292. attach->priv = NULL;
  293. }
  294. static struct sg_table *
  295. drm_gem_cma_dmabuf_map(struct dma_buf_attachment *attach,
  296. enum dma_data_direction dir)
  297. {
  298. struct drm_gem_cma_dmabuf_attachment *cma_attach = attach->priv;
  299. struct drm_gem_cma_object *cma_obj = attach->dmabuf->priv;
  300. struct drm_device *drm = cma_obj->base.dev;
  301. struct scatterlist *rd, *wr;
  302. struct sg_table *sgt;
  303. unsigned int i;
  304. int nents, ret;
  305. DRM_DEBUG_PRIME("\n");
  306. if (WARN_ON(dir == DMA_NONE))
  307. return ERR_PTR(-EINVAL);
  308. /* Return the cached mapping when possible. */
  309. if (cma_attach->dir == dir)
  310. return &cma_attach->sgt;
  311. /* Two mappings with different directions for the same attachment are
  312. * not allowed.
  313. */
  314. if (WARN_ON(cma_attach->dir != DMA_NONE))
  315. return ERR_PTR(-EBUSY);
  316. sgt = &cma_attach->sgt;
  317. ret = sg_alloc_table(sgt, cma_obj->sgt->orig_nents, GFP_KERNEL);
  318. if (ret) {
  319. DRM_ERROR("failed to alloc sgt.\n");
  320. return ERR_PTR(-ENOMEM);
  321. }
  322. mutex_lock(&drm->struct_mutex);
  323. rd = cma_obj->sgt->sgl;
  324. wr = sgt->sgl;
  325. for (i = 0; i < sgt->orig_nents; ++i) {
  326. sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
  327. rd = sg_next(rd);
  328. wr = sg_next(wr);
  329. }
  330. nents = dma_map_sg(attach->dev, sgt->sgl, sgt->orig_nents, dir);
  331. if (!nents) {
  332. DRM_ERROR("failed to map sgl with iommu.\n");
  333. sg_free_table(sgt);
  334. sgt = ERR_PTR(-EIO);
  335. goto done;
  336. }
  337. cma_attach->dir = dir;
  338. attach->priv = cma_attach;
  339. DRM_DEBUG_PRIME("buffer size = %zu\n", cma_obj->base.size);
  340. done:
  341. mutex_unlock(&drm->struct_mutex);
  342. return sgt;
  343. }
  344. static void drm_gem_cma_dmabuf_unmap(struct dma_buf_attachment *attach,
  345. struct sg_table *sgt,
  346. enum dma_data_direction dir)
  347. {
  348. /* Nothing to do. */
  349. }
  350. static void drm_gem_cma_dmabuf_release(struct dma_buf *dmabuf)
  351. {
  352. struct drm_gem_cma_object *cma_obj = dmabuf->priv;
  353. DRM_DEBUG_PRIME("%s\n", __FILE__);
  354. /*
  355. * drm_gem_cma_dmabuf_release() call means that file object's
  356. * f_count is 0 and it calls drm_gem_object_handle_unreference()
  357. * to drop the references that these values had been increased
  358. * at drm_prime_handle_to_fd()
  359. */
  360. if (cma_obj->base.export_dma_buf == dmabuf) {
  361. cma_obj->base.export_dma_buf = NULL;
  362. /*
  363. * drop this gem object refcount to release allocated buffer
  364. * and resources.
  365. */
  366. drm_gem_object_unreference_unlocked(&cma_obj->base);
  367. }
  368. }
  369. static void *drm_gem_cma_dmabuf_kmap_atomic(struct dma_buf *dmabuf,
  370. unsigned long page_num)
  371. {
  372. /* TODO */
  373. return NULL;
  374. }
  375. static void drm_gem_cma_dmabuf_kunmap_atomic(struct dma_buf *dmabuf,
  376. unsigned long page_num, void *addr)
  377. {
  378. /* TODO */
  379. }
  380. static void *drm_gem_cma_dmabuf_kmap(struct dma_buf *dmabuf,
  381. unsigned long page_num)
  382. {
  383. /* TODO */
  384. return NULL;
  385. }
  386. static void drm_gem_cma_dmabuf_kunmap(struct dma_buf *dmabuf,
  387. unsigned long page_num, void *addr)
  388. {
  389. /* TODO */
  390. }
  391. static int drm_gem_cma_dmabuf_mmap(struct dma_buf *dmabuf,
  392. struct vm_area_struct *vma)
  393. {
  394. struct drm_gem_cma_object *cma_obj = dmabuf->priv;
  395. struct drm_gem_object *gem_obj = &cma_obj->base;
  396. struct drm_device *dev = gem_obj->dev;
  397. int ret;
  398. mutex_lock(&dev->struct_mutex);
  399. ret = drm_gem_mmap_obj(gem_obj, gem_obj->size, vma);
  400. mutex_unlock(&dev->struct_mutex);
  401. if (ret < 0)
  402. return ret;
  403. return drm_gem_cma_mmap_obj(cma_obj, vma);
  404. }
  405. static void *drm_gem_cma_dmabuf_vmap(struct dma_buf *dmabuf)
  406. {
  407. struct drm_gem_cma_object *cma_obj = dmabuf->priv;
  408. return cma_obj->vaddr;
  409. }
  410. static struct dma_buf_ops drm_gem_cma_dmabuf_ops = {
  411. .attach = drm_gem_cma_dmabuf_attach,
  412. .detach = drm_gem_cma_dmabuf_detach,
  413. .map_dma_buf = drm_gem_cma_dmabuf_map,
  414. .unmap_dma_buf = drm_gem_cma_dmabuf_unmap,
  415. .kmap = drm_gem_cma_dmabuf_kmap,
  416. .kmap_atomic = drm_gem_cma_dmabuf_kmap_atomic,
  417. .kunmap = drm_gem_cma_dmabuf_kunmap,
  418. .kunmap_atomic = drm_gem_cma_dmabuf_kunmap_atomic,
  419. .mmap = drm_gem_cma_dmabuf_mmap,
  420. .vmap = drm_gem_cma_dmabuf_vmap,
  421. .release = drm_gem_cma_dmabuf_release,
  422. };
  423. struct dma_buf *drm_gem_cma_dmabuf_export(struct drm_device *drm,
  424. struct drm_gem_object *obj, int flags)
  425. {
  426. struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
  427. return dma_buf_export(cma_obj, &drm_gem_cma_dmabuf_ops,
  428. cma_obj->base.size, flags);
  429. }
  430. EXPORT_SYMBOL_GPL(drm_gem_cma_dmabuf_export);
  431. struct drm_gem_object *drm_gem_cma_dmabuf_import(struct drm_device *drm,
  432. struct dma_buf *dma_buf)
  433. {
  434. struct drm_gem_cma_object *cma_obj;
  435. struct dma_buf_attachment *attach;
  436. struct sg_table *sgt;
  437. int ret;
  438. DRM_DEBUG_PRIME("%s\n", __FILE__);
  439. /* is this one of own objects? */
  440. if (dma_buf->ops == &drm_gem_cma_dmabuf_ops) {
  441. struct drm_gem_object *obj;
  442. cma_obj = dma_buf->priv;
  443. obj = &cma_obj->base;
  444. /* is it from our device? */
  445. if (obj->dev == drm) {
  446. /*
  447. * Importing dmabuf exported from out own gem increases
  448. * refcount on gem itself instead of f_count of dmabuf.
  449. */
  450. drm_gem_object_reference(obj);
  451. dma_buf_put(dma_buf);
  452. return obj;
  453. }
  454. }
  455. /* Create a CMA GEM buffer. */
  456. cma_obj = __drm_gem_cma_create(drm, dma_buf->size);
  457. if (IS_ERR(cma_obj))
  458. return ERR_PTR(PTR_ERR(cma_obj));
  459. /* Attach to the buffer and map it. Make sure the mapping is contiguous
  460. * on the device memory bus, as that's all we support.
  461. */
  462. attach = dma_buf_attach(dma_buf, drm->dev);
  463. if (IS_ERR(attach)) {
  464. ret = -EINVAL;
  465. goto error_gem_free;
  466. }
  467. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  468. if (IS_ERR_OR_NULL(sgt)) {
  469. ret = sgt ? PTR_ERR(sgt) : -ENOMEM;
  470. goto error_buf_detach;
  471. }
  472. if (sgt->nents != 1) {
  473. ret = -EINVAL;
  474. goto error_buf_unmap;
  475. }
  476. cma_obj->base.import_attach = attach;
  477. cma_obj->paddr = sg_dma_address(sgt->sgl);
  478. cma_obj->sgt = sgt;
  479. DRM_DEBUG_PRIME("dma_addr = 0x%x, size = %zu\n", cma_obj->paddr,
  480. dma_buf->size);
  481. return &cma_obj->base;
  482. error_buf_unmap:
  483. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  484. error_buf_detach:
  485. dma_buf_detach(dma_buf, attach);
  486. error_gem_free:
  487. drm_gem_cma_free_object(&cma_obj->base);
  488. return ERR_PTR(ret);
  489. }
  490. EXPORT_SYMBOL_GPL(drm_gem_cma_dmabuf_import);