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. if (IS_ERR(cma_obj))
  182. return PTR_ERR(cma_obj);
  183. return 0;
  184. }
  185. EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
  186. /*
  187. * drm_gem_cma_dumb_map_offset - (struct drm_driver)->dumb_map_offset callback
  188. * function
  189. */
  190. int drm_gem_cma_dumb_map_offset(struct drm_file *file_priv,
  191. struct drm_device *drm, uint32_t handle, uint64_t *offset)
  192. {
  193. struct drm_gem_object *gem_obj;
  194. mutex_lock(&drm->struct_mutex);
  195. gem_obj = drm_gem_object_lookup(drm, file_priv, handle);
  196. if (!gem_obj) {
  197. dev_err(drm->dev, "failed to lookup gem object\n");
  198. mutex_unlock(&drm->struct_mutex);
  199. return -EINVAL;
  200. }
  201. *offset = get_gem_mmap_offset(gem_obj);
  202. drm_gem_object_unreference(gem_obj);
  203. mutex_unlock(&drm->struct_mutex);
  204. return 0;
  205. }
  206. EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_map_offset);
  207. const struct vm_operations_struct drm_gem_cma_vm_ops = {
  208. .open = drm_gem_vm_open,
  209. .close = drm_gem_vm_close,
  210. };
  211. EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops);
  212. static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object *cma_obj,
  213. struct vm_area_struct *vma)
  214. {
  215. int ret;
  216. ret = remap_pfn_range(vma, vma->vm_start, cma_obj->paddr >> PAGE_SHIFT,
  217. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  218. if (ret)
  219. drm_gem_vm_close(vma);
  220. return ret;
  221. }
  222. /*
  223. * drm_gem_cma_mmap - (struct file_operation)->mmap callback function
  224. */
  225. int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma)
  226. {
  227. struct drm_gem_cma_object *cma_obj;
  228. struct drm_gem_object *gem_obj;
  229. int ret;
  230. ret = drm_gem_mmap(filp, vma);
  231. if (ret)
  232. return ret;
  233. gem_obj = vma->vm_private_data;
  234. cma_obj = to_drm_gem_cma_obj(gem_obj);
  235. return drm_gem_cma_mmap_obj(cma_obj, vma);
  236. }
  237. EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);
  238. /*
  239. * drm_gem_cma_dumb_destroy - (struct drm_driver)->dumb_destroy callback function
  240. */
  241. int drm_gem_cma_dumb_destroy(struct drm_file *file_priv,
  242. struct drm_device *drm, unsigned int handle)
  243. {
  244. return drm_gem_handle_delete(file_priv, handle);
  245. }
  246. EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_destroy);
  247. #ifdef CONFIG_DEBUG_FS
  248. void drm_gem_cma_describe(struct drm_gem_cma_object *cma_obj, struct seq_file *m)
  249. {
  250. struct drm_gem_object *obj = &cma_obj->base;
  251. struct drm_device *dev = obj->dev;
  252. uint64_t off = 0;
  253. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  254. if (obj->map_list.map)
  255. off = (uint64_t)obj->map_list.hash.key;
  256. seq_printf(m, "%2d (%2d) %08llx %08Zx %p %d",
  257. obj->name, obj->refcount.refcount.counter,
  258. off, cma_obj->paddr, cma_obj->vaddr, obj->size);
  259. seq_printf(m, "\n");
  260. }
  261. EXPORT_SYMBOL_GPL(drm_gem_cma_describe);
  262. #endif
  263. /* -----------------------------------------------------------------------------
  264. * DMA-BUF
  265. */
  266. struct drm_gem_cma_dmabuf_attachment {
  267. struct sg_table sgt;
  268. enum dma_data_direction dir;
  269. };
  270. static int drm_gem_cma_dmabuf_attach(struct dma_buf *dmabuf, struct device *dev,
  271. struct dma_buf_attachment *attach)
  272. {
  273. struct drm_gem_cma_dmabuf_attachment *cma_attach;
  274. cma_attach = kzalloc(sizeof(*cma_attach), GFP_KERNEL);
  275. if (!cma_attach)
  276. return -ENOMEM;
  277. cma_attach->dir = DMA_NONE;
  278. attach->priv = cma_attach;
  279. return 0;
  280. }
  281. static void drm_gem_cma_dmabuf_detach(struct dma_buf *dmabuf,
  282. struct dma_buf_attachment *attach)
  283. {
  284. struct drm_gem_cma_dmabuf_attachment *cma_attach = attach->priv;
  285. struct sg_table *sgt;
  286. if (cma_attach == NULL)
  287. return;
  288. sgt = &cma_attach->sgt;
  289. if (cma_attach->dir != DMA_NONE)
  290. dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
  291. cma_attach->dir);
  292. sg_free_table(sgt);
  293. kfree(cma_attach);
  294. attach->priv = NULL;
  295. }
  296. static struct sg_table *
  297. drm_gem_cma_dmabuf_map(struct dma_buf_attachment *attach,
  298. enum dma_data_direction dir)
  299. {
  300. struct drm_gem_cma_dmabuf_attachment *cma_attach = attach->priv;
  301. struct drm_gem_cma_object *cma_obj = attach->dmabuf->priv;
  302. struct drm_device *drm = cma_obj->base.dev;
  303. struct scatterlist *rd, *wr;
  304. struct sg_table *sgt;
  305. unsigned int i;
  306. int nents, ret;
  307. DRM_DEBUG_PRIME("\n");
  308. if (WARN_ON(dir == DMA_NONE))
  309. return ERR_PTR(-EINVAL);
  310. /* Return the cached mapping when possible. */
  311. if (cma_attach->dir == dir)
  312. return &cma_attach->sgt;
  313. /* Two mappings with different directions for the same attachment are
  314. * not allowed.
  315. */
  316. if (WARN_ON(cma_attach->dir != DMA_NONE))
  317. return ERR_PTR(-EBUSY);
  318. sgt = &cma_attach->sgt;
  319. ret = sg_alloc_table(sgt, cma_obj->sgt->orig_nents, GFP_KERNEL);
  320. if (ret) {
  321. DRM_ERROR("failed to alloc sgt.\n");
  322. return ERR_PTR(-ENOMEM);
  323. }
  324. mutex_lock(&drm->struct_mutex);
  325. rd = cma_obj->sgt->sgl;
  326. wr = sgt->sgl;
  327. for (i = 0; i < sgt->orig_nents; ++i) {
  328. sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
  329. rd = sg_next(rd);
  330. wr = sg_next(wr);
  331. }
  332. nents = dma_map_sg(attach->dev, sgt->sgl, sgt->orig_nents, dir);
  333. if (!nents) {
  334. DRM_ERROR("failed to map sgl with iommu.\n");
  335. sg_free_table(sgt);
  336. sgt = ERR_PTR(-EIO);
  337. goto done;
  338. }
  339. cma_attach->dir = dir;
  340. attach->priv = cma_attach;
  341. DRM_DEBUG_PRIME("buffer size = %zu\n", cma_obj->base.size);
  342. done:
  343. mutex_unlock(&drm->struct_mutex);
  344. return sgt;
  345. }
  346. static void drm_gem_cma_dmabuf_unmap(struct dma_buf_attachment *attach,
  347. struct sg_table *sgt,
  348. enum dma_data_direction dir)
  349. {
  350. /* Nothing to do. */
  351. }
  352. static void drm_gem_cma_dmabuf_release(struct dma_buf *dmabuf)
  353. {
  354. struct drm_gem_cma_object *cma_obj = dmabuf->priv;
  355. DRM_DEBUG_PRIME("%s\n", __FILE__);
  356. /*
  357. * drm_gem_cma_dmabuf_release() call means that file object's
  358. * f_count is 0 and it calls drm_gem_object_handle_unreference()
  359. * to drop the references that these values had been increased
  360. * at drm_prime_handle_to_fd()
  361. */
  362. if (cma_obj->base.export_dma_buf == dmabuf) {
  363. cma_obj->base.export_dma_buf = NULL;
  364. /*
  365. * drop this gem object refcount to release allocated buffer
  366. * and resources.
  367. */
  368. drm_gem_object_unreference_unlocked(&cma_obj->base);
  369. }
  370. }
  371. static void *drm_gem_cma_dmabuf_kmap_atomic(struct dma_buf *dmabuf,
  372. unsigned long page_num)
  373. {
  374. /* TODO */
  375. return NULL;
  376. }
  377. static void drm_gem_cma_dmabuf_kunmap_atomic(struct dma_buf *dmabuf,
  378. unsigned long page_num, void *addr)
  379. {
  380. /* TODO */
  381. }
  382. static void *drm_gem_cma_dmabuf_kmap(struct dma_buf *dmabuf,
  383. unsigned long page_num)
  384. {
  385. /* TODO */
  386. return NULL;
  387. }
  388. static void drm_gem_cma_dmabuf_kunmap(struct dma_buf *dmabuf,
  389. unsigned long page_num, void *addr)
  390. {
  391. /* TODO */
  392. }
  393. static int drm_gem_cma_dmabuf_mmap(struct dma_buf *dmabuf,
  394. struct vm_area_struct *vma)
  395. {
  396. struct drm_gem_cma_object *cma_obj = dmabuf->priv;
  397. struct drm_gem_object *gem_obj = &cma_obj->base;
  398. int ret;
  399. ret = drm_gem_mmap_obj(gem_obj, gem_obj->size, vma);
  400. if (ret < 0)
  401. return ret;
  402. return drm_gem_cma_mmap_obj(cma_obj, vma);
  403. }
  404. static void *drm_gem_cma_dmabuf_vmap(struct dma_buf *dmabuf)
  405. {
  406. struct drm_gem_cma_object *cma_obj = dmabuf->priv;
  407. return cma_obj->vaddr;
  408. }
  409. static struct dma_buf_ops drm_gem_cma_dmabuf_ops = {
  410. .attach = drm_gem_cma_dmabuf_attach,
  411. .detach = drm_gem_cma_dmabuf_detach,
  412. .map_dma_buf = drm_gem_cma_dmabuf_map,
  413. .unmap_dma_buf = drm_gem_cma_dmabuf_unmap,
  414. .kmap = drm_gem_cma_dmabuf_kmap,
  415. .kmap_atomic = drm_gem_cma_dmabuf_kmap_atomic,
  416. .kunmap = drm_gem_cma_dmabuf_kunmap,
  417. .kunmap_atomic = drm_gem_cma_dmabuf_kunmap_atomic,
  418. .mmap = drm_gem_cma_dmabuf_mmap,
  419. .vmap = drm_gem_cma_dmabuf_vmap,
  420. .release = drm_gem_cma_dmabuf_release,
  421. };
  422. struct dma_buf *drm_gem_cma_dmabuf_export(struct drm_device *drm,
  423. struct drm_gem_object *obj, int flags)
  424. {
  425. struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
  426. return dma_buf_export(cma_obj, &drm_gem_cma_dmabuf_ops,
  427. cma_obj->base.size, flags);
  428. }
  429. EXPORT_SYMBOL_GPL(drm_gem_cma_dmabuf_export);
  430. struct drm_gem_object *drm_gem_cma_dmabuf_import(struct drm_device *drm,
  431. struct dma_buf *dma_buf)
  432. {
  433. struct drm_gem_cma_object *cma_obj;
  434. struct dma_buf_attachment *attach;
  435. struct sg_table *sgt;
  436. int ret;
  437. DRM_DEBUG_PRIME("%s\n", __FILE__);
  438. /* is this one of own objects? */
  439. if (dma_buf->ops == &drm_gem_cma_dmabuf_ops) {
  440. struct drm_gem_object *obj;
  441. cma_obj = dma_buf->priv;
  442. obj = &cma_obj->base;
  443. /* is it from our device? */
  444. if (obj->dev == drm) {
  445. /*
  446. * Importing dmabuf exported from out own gem increases
  447. * refcount on gem itself instead of f_count of dmabuf.
  448. */
  449. drm_gem_object_reference(obj);
  450. dma_buf_put(dma_buf);
  451. return obj;
  452. }
  453. }
  454. /* Create a CMA GEM buffer. */
  455. cma_obj = __drm_gem_cma_create(drm, dma_buf->size);
  456. if (IS_ERR(cma_obj))
  457. return ERR_PTR(PTR_ERR(cma_obj));
  458. /* Attach to the buffer and map it. Make sure the mapping is contiguous
  459. * on the device memory bus, as that's all we support.
  460. */
  461. attach = dma_buf_attach(dma_buf, drm->dev);
  462. if (IS_ERR(attach)) {
  463. ret = -EINVAL;
  464. goto error_gem_free;
  465. }
  466. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  467. if (IS_ERR_OR_NULL(sgt)) {
  468. ret = sgt ? PTR_ERR(sgt) : -ENOMEM;
  469. goto error_buf_detach;
  470. }
  471. if (sgt->nents != 1) {
  472. ret = -EINVAL;
  473. goto error_buf_unmap;
  474. }
  475. cma_obj->base.import_attach = attach;
  476. cma_obj->paddr = sg_dma_address(sgt->sgl);
  477. cma_obj->sgt = sgt;
  478. DRM_DEBUG_PRIME("dma_addr = 0x%x, size = %zu\n", cma_obj->paddr,
  479. dma_buf->size);
  480. return &cma_obj->base;
  481. error_buf_unmap:
  482. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  483. error_buf_detach:
  484. dma_buf_detach(dma_buf, attach);
  485. error_gem_free:
  486. drm_gem_cma_free_object(&cma_obj->base);
  487. return ERR_PTR(ret);
  488. }
  489. EXPORT_SYMBOL_GPL(drm_gem_cma_dmabuf_import);