gem.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * NVIDIA Tegra DRM GEM helper functions
  3. *
  4. * Copyright (C) 2012 Sascha Hauer, Pengutronix
  5. * Copyright (C) 2013 NVIDIA CORPORATION, All rights reserved.
  6. *
  7. * Based on the GEM/CMA helpers
  8. *
  9. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/dma-buf.h>
  16. #include <drm/tegra_drm.h>
  17. #include "gem.h"
  18. static inline struct tegra_bo *host1x_to_tegra_bo(struct host1x_bo *bo)
  19. {
  20. return container_of(bo, struct tegra_bo, base);
  21. }
  22. static void tegra_bo_put(struct host1x_bo *bo)
  23. {
  24. struct tegra_bo *obj = host1x_to_tegra_bo(bo);
  25. struct drm_device *drm = obj->gem.dev;
  26. mutex_lock(&drm->struct_mutex);
  27. drm_gem_object_unreference(&obj->gem);
  28. mutex_unlock(&drm->struct_mutex);
  29. }
  30. static dma_addr_t tegra_bo_pin(struct host1x_bo *bo, struct sg_table **sgt)
  31. {
  32. struct tegra_bo *obj = host1x_to_tegra_bo(bo);
  33. return obj->paddr;
  34. }
  35. static void tegra_bo_unpin(struct host1x_bo *bo, struct sg_table *sgt)
  36. {
  37. }
  38. static void *tegra_bo_mmap(struct host1x_bo *bo)
  39. {
  40. struct tegra_bo *obj = host1x_to_tegra_bo(bo);
  41. return obj->vaddr;
  42. }
  43. static void tegra_bo_munmap(struct host1x_bo *bo, void *addr)
  44. {
  45. }
  46. static void *tegra_bo_kmap(struct host1x_bo *bo, unsigned int page)
  47. {
  48. struct tegra_bo *obj = host1x_to_tegra_bo(bo);
  49. return obj->vaddr + page * PAGE_SIZE;
  50. }
  51. static void tegra_bo_kunmap(struct host1x_bo *bo, unsigned int page,
  52. void *addr)
  53. {
  54. }
  55. static struct host1x_bo *tegra_bo_get(struct host1x_bo *bo)
  56. {
  57. struct tegra_bo *obj = host1x_to_tegra_bo(bo);
  58. struct drm_device *drm = obj->gem.dev;
  59. mutex_lock(&drm->struct_mutex);
  60. drm_gem_object_reference(&obj->gem);
  61. mutex_unlock(&drm->struct_mutex);
  62. return bo;
  63. }
  64. static const struct host1x_bo_ops tegra_bo_ops = {
  65. .get = tegra_bo_get,
  66. .put = tegra_bo_put,
  67. .pin = tegra_bo_pin,
  68. .unpin = tegra_bo_unpin,
  69. .mmap = tegra_bo_mmap,
  70. .munmap = tegra_bo_munmap,
  71. .kmap = tegra_bo_kmap,
  72. .kunmap = tegra_bo_kunmap,
  73. };
  74. static void tegra_bo_destroy(struct drm_device *drm, struct tegra_bo *bo)
  75. {
  76. dma_free_writecombine(drm->dev, bo->gem.size, bo->vaddr, bo->paddr);
  77. }
  78. struct tegra_bo *tegra_bo_create(struct drm_device *drm, unsigned int size,
  79. unsigned long flags)
  80. {
  81. struct tegra_bo *bo;
  82. int err;
  83. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  84. if (!bo)
  85. return ERR_PTR(-ENOMEM);
  86. host1x_bo_init(&bo->base, &tegra_bo_ops);
  87. size = round_up(size, PAGE_SIZE);
  88. bo->vaddr = dma_alloc_writecombine(drm->dev, size, &bo->paddr,
  89. GFP_KERNEL | __GFP_NOWARN);
  90. if (!bo->vaddr) {
  91. dev_err(drm->dev, "failed to allocate buffer with size %u\n",
  92. size);
  93. err = -ENOMEM;
  94. goto err_dma;
  95. }
  96. err = drm_gem_object_init(drm, &bo->gem, size);
  97. if (err)
  98. goto err_init;
  99. err = drm_gem_create_mmap_offset(&bo->gem);
  100. if (err)
  101. goto err_mmap;
  102. if (flags & DRM_TEGRA_GEM_CREATE_TILED)
  103. bo->flags |= TEGRA_BO_TILED;
  104. if (flags & DRM_TEGRA_GEM_CREATE_BOTTOM_UP)
  105. bo->flags |= TEGRA_BO_BOTTOM_UP;
  106. return bo;
  107. err_mmap:
  108. drm_gem_object_release(&bo->gem);
  109. err_init:
  110. tegra_bo_destroy(drm, bo);
  111. err_dma:
  112. kfree(bo);
  113. return ERR_PTR(err);
  114. }
  115. struct tegra_bo *tegra_bo_create_with_handle(struct drm_file *file,
  116. struct drm_device *drm,
  117. unsigned int size,
  118. unsigned long flags,
  119. unsigned int *handle)
  120. {
  121. struct tegra_bo *bo;
  122. int ret;
  123. bo = tegra_bo_create(drm, size, flags);
  124. if (IS_ERR(bo))
  125. return bo;
  126. ret = drm_gem_handle_create(file, &bo->gem, handle);
  127. if (ret)
  128. goto err;
  129. drm_gem_object_unreference_unlocked(&bo->gem);
  130. return bo;
  131. err:
  132. tegra_bo_free_object(&bo->gem);
  133. return ERR_PTR(ret);
  134. }
  135. static struct tegra_bo *tegra_bo_import(struct drm_device *drm,
  136. struct dma_buf *buf)
  137. {
  138. struct dma_buf_attachment *attach;
  139. struct tegra_bo *bo;
  140. ssize_t size;
  141. int err;
  142. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  143. if (!bo)
  144. return ERR_PTR(-ENOMEM);
  145. host1x_bo_init(&bo->base, &tegra_bo_ops);
  146. size = round_up(buf->size, PAGE_SIZE);
  147. err = drm_gem_object_init(drm, &bo->gem, size);
  148. if (err < 0)
  149. goto free;
  150. err = drm_gem_create_mmap_offset(&bo->gem);
  151. if (err < 0)
  152. goto release;
  153. attach = dma_buf_attach(buf, drm->dev);
  154. if (IS_ERR(attach)) {
  155. err = PTR_ERR(attach);
  156. goto free_mmap;
  157. }
  158. get_dma_buf(buf);
  159. bo->sgt = dma_buf_map_attachment(attach, DMA_TO_DEVICE);
  160. if (!bo->sgt) {
  161. err = -ENOMEM;
  162. goto detach;
  163. }
  164. if (IS_ERR(bo->sgt)) {
  165. err = PTR_ERR(bo->sgt);
  166. goto detach;
  167. }
  168. if (bo->sgt->nents > 1) {
  169. err = -EINVAL;
  170. goto detach;
  171. }
  172. bo->paddr = sg_dma_address(bo->sgt->sgl);
  173. bo->gem.import_attach = attach;
  174. return bo;
  175. detach:
  176. if (!IS_ERR_OR_NULL(bo->sgt))
  177. dma_buf_unmap_attachment(attach, bo->sgt, DMA_TO_DEVICE);
  178. dma_buf_detach(buf, attach);
  179. dma_buf_put(buf);
  180. free_mmap:
  181. drm_gem_free_mmap_offset(&bo->gem);
  182. release:
  183. drm_gem_object_release(&bo->gem);
  184. free:
  185. kfree(bo);
  186. return ERR_PTR(err);
  187. }
  188. void tegra_bo_free_object(struct drm_gem_object *gem)
  189. {
  190. struct tegra_bo *bo = to_tegra_bo(gem);
  191. if (gem->import_attach) {
  192. dma_buf_unmap_attachment(gem->import_attach, bo->sgt,
  193. DMA_TO_DEVICE);
  194. drm_prime_gem_destroy(gem, NULL);
  195. } else {
  196. tegra_bo_destroy(gem->dev, bo);
  197. }
  198. drm_gem_free_mmap_offset(gem);
  199. drm_gem_object_release(gem);
  200. kfree(bo);
  201. }
  202. int tegra_bo_dumb_create(struct drm_file *file, struct drm_device *drm,
  203. struct drm_mode_create_dumb *args)
  204. {
  205. int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
  206. struct tegra_bo *bo;
  207. if (args->pitch < min_pitch)
  208. args->pitch = min_pitch;
  209. if (args->size < args->pitch * args->height)
  210. args->size = args->pitch * args->height;
  211. bo = tegra_bo_create_with_handle(file, drm, args->size, 0,
  212. &args->handle);
  213. if (IS_ERR(bo))
  214. return PTR_ERR(bo);
  215. return 0;
  216. }
  217. int tegra_bo_dumb_map_offset(struct drm_file *file, struct drm_device *drm,
  218. uint32_t handle, uint64_t *offset)
  219. {
  220. struct drm_gem_object *gem;
  221. struct tegra_bo *bo;
  222. mutex_lock(&drm->struct_mutex);
  223. gem = drm_gem_object_lookup(drm, file, handle);
  224. if (!gem) {
  225. dev_err(drm->dev, "failed to lookup GEM object\n");
  226. mutex_unlock(&drm->struct_mutex);
  227. return -EINVAL;
  228. }
  229. bo = to_tegra_bo(gem);
  230. *offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
  231. drm_gem_object_unreference(gem);
  232. mutex_unlock(&drm->struct_mutex);
  233. return 0;
  234. }
  235. const struct vm_operations_struct tegra_bo_vm_ops = {
  236. .open = drm_gem_vm_open,
  237. .close = drm_gem_vm_close,
  238. };
  239. int tegra_drm_mmap(struct file *file, struct vm_area_struct *vma)
  240. {
  241. struct drm_gem_object *gem;
  242. struct tegra_bo *bo;
  243. int ret;
  244. ret = drm_gem_mmap(file, vma);
  245. if (ret)
  246. return ret;
  247. gem = vma->vm_private_data;
  248. bo = to_tegra_bo(gem);
  249. ret = remap_pfn_range(vma, vma->vm_start, bo->paddr >> PAGE_SHIFT,
  250. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  251. if (ret)
  252. drm_gem_vm_close(vma);
  253. return ret;
  254. }
  255. static struct sg_table *
  256. tegra_gem_prime_map_dma_buf(struct dma_buf_attachment *attach,
  257. enum dma_data_direction dir)
  258. {
  259. struct drm_gem_object *gem = attach->dmabuf->priv;
  260. struct tegra_bo *bo = to_tegra_bo(gem);
  261. struct sg_table *sgt;
  262. sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
  263. if (!sgt)
  264. return NULL;
  265. if (sg_alloc_table(sgt, 1, GFP_KERNEL)) {
  266. kfree(sgt);
  267. return NULL;
  268. }
  269. sg_dma_address(sgt->sgl) = bo->paddr;
  270. sg_dma_len(sgt->sgl) = gem->size;
  271. return sgt;
  272. }
  273. static void tegra_gem_prime_unmap_dma_buf(struct dma_buf_attachment *attach,
  274. struct sg_table *sgt,
  275. enum dma_data_direction dir)
  276. {
  277. sg_free_table(sgt);
  278. kfree(sgt);
  279. }
  280. static void tegra_gem_prime_release(struct dma_buf *buf)
  281. {
  282. drm_gem_dmabuf_release(buf);
  283. }
  284. static void *tegra_gem_prime_kmap_atomic(struct dma_buf *buf,
  285. unsigned long page)
  286. {
  287. return NULL;
  288. }
  289. static void tegra_gem_prime_kunmap_atomic(struct dma_buf *buf,
  290. unsigned long page,
  291. void *addr)
  292. {
  293. }
  294. static void *tegra_gem_prime_kmap(struct dma_buf *buf, unsigned long page)
  295. {
  296. return NULL;
  297. }
  298. static void tegra_gem_prime_kunmap(struct dma_buf *buf, unsigned long page,
  299. void *addr)
  300. {
  301. }
  302. static int tegra_gem_prime_mmap(struct dma_buf *buf, struct vm_area_struct *vma)
  303. {
  304. return -EINVAL;
  305. }
  306. static void *tegra_gem_prime_vmap(struct dma_buf *buf)
  307. {
  308. struct drm_gem_object *gem = buf->priv;
  309. struct tegra_bo *bo = to_tegra_bo(gem);
  310. return bo->vaddr;
  311. }
  312. static void tegra_gem_prime_vunmap(struct dma_buf *buf, void *vaddr)
  313. {
  314. }
  315. static const struct dma_buf_ops tegra_gem_prime_dmabuf_ops = {
  316. .map_dma_buf = tegra_gem_prime_map_dma_buf,
  317. .unmap_dma_buf = tegra_gem_prime_unmap_dma_buf,
  318. .release = tegra_gem_prime_release,
  319. .kmap_atomic = tegra_gem_prime_kmap_atomic,
  320. .kunmap_atomic = tegra_gem_prime_kunmap_atomic,
  321. .kmap = tegra_gem_prime_kmap,
  322. .kunmap = tegra_gem_prime_kunmap,
  323. .mmap = tegra_gem_prime_mmap,
  324. .vmap = tegra_gem_prime_vmap,
  325. .vunmap = tegra_gem_prime_vunmap,
  326. };
  327. struct dma_buf *tegra_gem_prime_export(struct drm_device *drm,
  328. struct drm_gem_object *gem,
  329. int flags)
  330. {
  331. return dma_buf_export(gem, &tegra_gem_prime_dmabuf_ops, gem->size,
  332. flags);
  333. }
  334. struct drm_gem_object *tegra_gem_prime_import(struct drm_device *drm,
  335. struct dma_buf *buf)
  336. {
  337. struct tegra_bo *bo;
  338. if (buf->ops == &tegra_gem_prime_dmabuf_ops) {
  339. struct drm_gem_object *gem = buf->priv;
  340. if (gem->dev == drm) {
  341. drm_gem_object_reference(gem);
  342. return gem;
  343. }
  344. }
  345. bo = tegra_bo_import(drm, buf);
  346. if (IS_ERR(bo))
  347. return ERR_CAST(bo);
  348. return &bo->gem;
  349. }