gem.c 9.2 KB

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