gem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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 <linux/iommu.h>
  17. #include <drm/tegra_drm.h>
  18. #include "drm.h"
  19. #include "gem.h"
  20. static inline struct tegra_bo *host1x_to_tegra_bo(struct host1x_bo *bo)
  21. {
  22. return container_of(bo, struct tegra_bo, base);
  23. }
  24. static void tegra_bo_put(struct host1x_bo *bo)
  25. {
  26. struct tegra_bo *obj = host1x_to_tegra_bo(bo);
  27. struct drm_device *drm = obj->gem.dev;
  28. mutex_lock(&drm->struct_mutex);
  29. drm_gem_object_unreference(&obj->gem);
  30. mutex_unlock(&drm->struct_mutex);
  31. }
  32. static dma_addr_t tegra_bo_pin(struct host1x_bo *bo, struct sg_table **sgt)
  33. {
  34. struct tegra_bo *obj = host1x_to_tegra_bo(bo);
  35. return obj->paddr;
  36. }
  37. static void tegra_bo_unpin(struct host1x_bo *bo, struct sg_table *sgt)
  38. {
  39. }
  40. static void *tegra_bo_mmap(struct host1x_bo *bo)
  41. {
  42. struct tegra_bo *obj = host1x_to_tegra_bo(bo);
  43. return obj->vaddr;
  44. }
  45. static void tegra_bo_munmap(struct host1x_bo *bo, void *addr)
  46. {
  47. }
  48. static void *tegra_bo_kmap(struct host1x_bo *bo, unsigned int page)
  49. {
  50. struct tegra_bo *obj = host1x_to_tegra_bo(bo);
  51. return obj->vaddr + page * PAGE_SIZE;
  52. }
  53. static void tegra_bo_kunmap(struct host1x_bo *bo, unsigned int page,
  54. void *addr)
  55. {
  56. }
  57. static struct host1x_bo *tegra_bo_get(struct host1x_bo *bo)
  58. {
  59. struct tegra_bo *obj = host1x_to_tegra_bo(bo);
  60. struct drm_device *drm = obj->gem.dev;
  61. mutex_lock(&drm->struct_mutex);
  62. drm_gem_object_reference(&obj->gem);
  63. mutex_unlock(&drm->struct_mutex);
  64. return bo;
  65. }
  66. static const struct host1x_bo_ops tegra_bo_ops = {
  67. .get = tegra_bo_get,
  68. .put = tegra_bo_put,
  69. .pin = tegra_bo_pin,
  70. .unpin = tegra_bo_unpin,
  71. .mmap = tegra_bo_mmap,
  72. .munmap = tegra_bo_munmap,
  73. .kmap = tegra_bo_kmap,
  74. .kunmap = tegra_bo_kunmap,
  75. };
  76. /*
  77. * A generic iommu_map_sg() function is being reviewed and will hopefully be
  78. * merged soon. At that point this function can be dropped in favour of the
  79. * one provided by the IOMMU API.
  80. */
  81. static ssize_t __iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
  82. struct scatterlist *sg, unsigned int nents,
  83. int prot)
  84. {
  85. struct scatterlist *s;
  86. size_t offset = 0;
  87. unsigned int i;
  88. int err;
  89. for_each_sg(sg, s, nents, i) {
  90. phys_addr_t phys = page_to_phys(sg_page(s));
  91. size_t length = s->offset + s->length;
  92. err = iommu_map(domain, iova + offset, phys, length, prot);
  93. if (err < 0) {
  94. iommu_unmap(domain, iova, offset);
  95. return err;
  96. }
  97. offset += length;
  98. }
  99. return offset;
  100. }
  101. static int tegra_bo_iommu_map(struct tegra_drm *tegra, struct tegra_bo *bo)
  102. {
  103. int prot = IOMMU_READ | IOMMU_WRITE;
  104. ssize_t err;
  105. if (bo->mm)
  106. return -EBUSY;
  107. bo->mm = kzalloc(sizeof(*bo->mm), GFP_KERNEL);
  108. if (!bo->mm)
  109. return -ENOMEM;
  110. err = drm_mm_insert_node_generic(&tegra->mm, bo->mm, bo->gem.size,
  111. PAGE_SIZE, 0, 0, 0);
  112. if (err < 0) {
  113. dev_err(tegra->drm->dev, "out of I/O virtual memory: %zd\n",
  114. err);
  115. goto free;
  116. }
  117. bo->paddr = bo->mm->start;
  118. err = __iommu_map_sg(tegra->domain, bo->paddr, bo->sgt->sgl,
  119. bo->sgt->nents, prot);
  120. if (err < 0) {
  121. dev_err(tegra->drm->dev, "failed to map buffer: %zd\n", err);
  122. goto remove;
  123. }
  124. bo->size = err;
  125. return 0;
  126. remove:
  127. drm_mm_remove_node(bo->mm);
  128. free:
  129. kfree(bo->mm);
  130. return err;
  131. }
  132. static int tegra_bo_iommu_unmap(struct tegra_drm *tegra, struct tegra_bo *bo)
  133. {
  134. if (!bo->mm)
  135. return 0;
  136. iommu_unmap(tegra->domain, bo->paddr, bo->size);
  137. drm_mm_remove_node(bo->mm);
  138. kfree(bo->mm);
  139. return 0;
  140. }
  141. static struct tegra_bo *tegra_bo_alloc_object(struct drm_device *drm,
  142. size_t size)
  143. {
  144. struct tegra_bo *bo;
  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(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. return bo;
  158. release:
  159. drm_gem_object_release(&bo->gem);
  160. free:
  161. kfree(bo);
  162. return ERR_PTR(err);
  163. }
  164. static void tegra_bo_free(struct drm_device *drm, struct tegra_bo *bo)
  165. {
  166. if (bo->pages) {
  167. drm_gem_put_pages(&bo->gem, bo->pages, true, true);
  168. sg_free_table(bo->sgt);
  169. kfree(bo->sgt);
  170. } else if (bo->vaddr) {
  171. dma_free_writecombine(drm->dev, bo->gem.size, bo->vaddr,
  172. bo->paddr);
  173. }
  174. }
  175. static int tegra_bo_get_pages(struct drm_device *drm, struct tegra_bo *bo)
  176. {
  177. struct scatterlist *s;
  178. struct sg_table *sgt;
  179. unsigned int i;
  180. bo->pages = drm_gem_get_pages(&bo->gem);
  181. if (IS_ERR(bo->pages))
  182. return PTR_ERR(bo->pages);
  183. bo->num_pages = bo->gem.size >> PAGE_SHIFT;
  184. sgt = drm_prime_pages_to_sg(bo->pages, bo->num_pages);
  185. if (IS_ERR(sgt))
  186. goto put_pages;
  187. /*
  188. * Fake up the SG table so that dma_map_sg() can be used to flush the
  189. * pages associated with it. Note that this relies on the fact that
  190. * the DMA API doesn't hook into IOMMU on Tegra, therefore mapping is
  191. * only cache maintenance.
  192. *
  193. * TODO: Replace this by drm_clflash_sg() once it can be implemented
  194. * without relying on symbols that are not exported.
  195. */
  196. for_each_sg(sgt->sgl, s, sgt->nents, i)
  197. sg_dma_address(s) = sg_phys(s);
  198. if (dma_map_sg(drm->dev, sgt->sgl, sgt->nents, DMA_TO_DEVICE) == 0) {
  199. sgt = ERR_PTR(-ENOMEM);
  200. goto release_sgt;
  201. }
  202. bo->sgt = sgt;
  203. return 0;
  204. release_sgt:
  205. sg_free_table(sgt);
  206. kfree(sgt);
  207. put_pages:
  208. drm_gem_put_pages(&bo->gem, bo->pages, false, false);
  209. return PTR_ERR(sgt);
  210. }
  211. static int tegra_bo_alloc(struct drm_device *drm, struct tegra_bo *bo)
  212. {
  213. struct tegra_drm *tegra = drm->dev_private;
  214. int err;
  215. if (tegra->domain) {
  216. err = tegra_bo_get_pages(drm, bo);
  217. if (err < 0)
  218. return err;
  219. err = tegra_bo_iommu_map(tegra, bo);
  220. if (err < 0) {
  221. tegra_bo_free(drm, bo);
  222. return err;
  223. }
  224. } else {
  225. size_t size = bo->gem.size;
  226. bo->vaddr = dma_alloc_writecombine(drm->dev, size, &bo->paddr,
  227. GFP_KERNEL | __GFP_NOWARN);
  228. if (!bo->vaddr) {
  229. dev_err(drm->dev,
  230. "failed to allocate buffer of size %zu\n",
  231. size);
  232. return -ENOMEM;
  233. }
  234. }
  235. return 0;
  236. }
  237. struct tegra_bo *tegra_bo_create(struct drm_device *drm, size_t size,
  238. unsigned long flags)
  239. {
  240. struct tegra_bo *bo;
  241. int err;
  242. bo = tegra_bo_alloc_object(drm, size);
  243. if (IS_ERR(bo))
  244. return bo;
  245. err = tegra_bo_alloc(drm, bo);
  246. if (err < 0)
  247. goto release;
  248. if (flags & DRM_TEGRA_GEM_CREATE_TILED)
  249. bo->tiling.mode = TEGRA_BO_TILING_MODE_TILED;
  250. if (flags & DRM_TEGRA_GEM_CREATE_BOTTOM_UP)
  251. bo->flags |= TEGRA_BO_BOTTOM_UP;
  252. return bo;
  253. release:
  254. drm_gem_object_release(&bo->gem);
  255. kfree(bo);
  256. return ERR_PTR(err);
  257. }
  258. struct tegra_bo *tegra_bo_create_with_handle(struct drm_file *file,
  259. struct drm_device *drm,
  260. size_t size,
  261. unsigned long flags,
  262. u32 *handle)
  263. {
  264. struct tegra_bo *bo;
  265. int err;
  266. bo = tegra_bo_create(drm, size, flags);
  267. if (IS_ERR(bo))
  268. return bo;
  269. err = drm_gem_handle_create(file, &bo->gem, handle);
  270. if (err) {
  271. tegra_bo_free_object(&bo->gem);
  272. return ERR_PTR(err);
  273. }
  274. drm_gem_object_unreference_unlocked(&bo->gem);
  275. return bo;
  276. }
  277. static struct tegra_bo *tegra_bo_import(struct drm_device *drm,
  278. struct dma_buf *buf)
  279. {
  280. struct tegra_drm *tegra = drm->dev_private;
  281. struct dma_buf_attachment *attach;
  282. struct tegra_bo *bo;
  283. int err;
  284. bo = tegra_bo_alloc_object(drm, buf->size);
  285. if (IS_ERR(bo))
  286. return bo;
  287. attach = dma_buf_attach(buf, drm->dev);
  288. if (IS_ERR(attach)) {
  289. err = PTR_ERR(attach);
  290. goto free;
  291. }
  292. get_dma_buf(buf);
  293. bo->sgt = dma_buf_map_attachment(attach, DMA_TO_DEVICE);
  294. if (!bo->sgt) {
  295. err = -ENOMEM;
  296. goto detach;
  297. }
  298. if (IS_ERR(bo->sgt)) {
  299. err = PTR_ERR(bo->sgt);
  300. goto detach;
  301. }
  302. if (tegra->domain) {
  303. err = tegra_bo_iommu_map(tegra, bo);
  304. if (err < 0)
  305. goto detach;
  306. } else {
  307. if (bo->sgt->nents > 1) {
  308. err = -EINVAL;
  309. goto detach;
  310. }
  311. bo->paddr = sg_dma_address(bo->sgt->sgl);
  312. }
  313. bo->gem.import_attach = attach;
  314. return bo;
  315. detach:
  316. if (!IS_ERR_OR_NULL(bo->sgt))
  317. dma_buf_unmap_attachment(attach, bo->sgt, DMA_TO_DEVICE);
  318. dma_buf_detach(buf, attach);
  319. dma_buf_put(buf);
  320. free:
  321. drm_gem_object_release(&bo->gem);
  322. kfree(bo);
  323. return ERR_PTR(err);
  324. }
  325. void tegra_bo_free_object(struct drm_gem_object *gem)
  326. {
  327. struct tegra_drm *tegra = gem->dev->dev_private;
  328. struct tegra_bo *bo = to_tegra_bo(gem);
  329. if (tegra->domain)
  330. tegra_bo_iommu_unmap(tegra, bo);
  331. if (gem->import_attach) {
  332. dma_buf_unmap_attachment(gem->import_attach, bo->sgt,
  333. DMA_TO_DEVICE);
  334. drm_prime_gem_destroy(gem, NULL);
  335. } else {
  336. tegra_bo_free(gem->dev, bo);
  337. }
  338. drm_gem_object_release(gem);
  339. kfree(bo);
  340. }
  341. int tegra_bo_dumb_create(struct drm_file *file, struct drm_device *drm,
  342. struct drm_mode_create_dumb *args)
  343. {
  344. unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
  345. struct tegra_drm *tegra = drm->dev_private;
  346. struct tegra_bo *bo;
  347. args->pitch = round_up(min_pitch, tegra->pitch_align);
  348. args->size = args->pitch * args->height;
  349. bo = tegra_bo_create_with_handle(file, drm, args->size, 0,
  350. &args->handle);
  351. if (IS_ERR(bo))
  352. return PTR_ERR(bo);
  353. return 0;
  354. }
  355. int tegra_bo_dumb_map_offset(struct drm_file *file, struct drm_device *drm,
  356. u32 handle, u64 *offset)
  357. {
  358. struct drm_gem_object *gem;
  359. struct tegra_bo *bo;
  360. mutex_lock(&drm->struct_mutex);
  361. gem = drm_gem_object_lookup(drm, file, handle);
  362. if (!gem) {
  363. dev_err(drm->dev, "failed to lookup GEM object\n");
  364. mutex_unlock(&drm->struct_mutex);
  365. return -EINVAL;
  366. }
  367. bo = to_tegra_bo(gem);
  368. *offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
  369. drm_gem_object_unreference(gem);
  370. mutex_unlock(&drm->struct_mutex);
  371. return 0;
  372. }
  373. static int tegra_bo_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  374. {
  375. struct drm_gem_object *gem = vma->vm_private_data;
  376. struct tegra_bo *bo = to_tegra_bo(gem);
  377. struct page *page;
  378. pgoff_t offset;
  379. int err;
  380. if (!bo->pages)
  381. return VM_FAULT_SIGBUS;
  382. offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >> PAGE_SHIFT;
  383. page = bo->pages[offset];
  384. err = vm_insert_page(vma, (unsigned long)vmf->virtual_address, page);
  385. switch (err) {
  386. case -EAGAIN:
  387. case 0:
  388. case -ERESTARTSYS:
  389. case -EINTR:
  390. case -EBUSY:
  391. return VM_FAULT_NOPAGE;
  392. case -ENOMEM:
  393. return VM_FAULT_OOM;
  394. }
  395. return VM_FAULT_SIGBUS;
  396. }
  397. const struct vm_operations_struct tegra_bo_vm_ops = {
  398. .fault = tegra_bo_fault,
  399. .open = drm_gem_vm_open,
  400. .close = drm_gem_vm_close,
  401. };
  402. int tegra_drm_mmap(struct file *file, struct vm_area_struct *vma)
  403. {
  404. struct drm_gem_object *gem;
  405. struct tegra_bo *bo;
  406. int ret;
  407. ret = drm_gem_mmap(file, vma);
  408. if (ret)
  409. return ret;
  410. gem = vma->vm_private_data;
  411. bo = to_tegra_bo(gem);
  412. if (!bo->pages) {
  413. unsigned long vm_pgoff = vma->vm_pgoff;
  414. vma->vm_flags &= ~VM_PFNMAP;
  415. vma->vm_pgoff = 0;
  416. ret = dma_mmap_writecombine(gem->dev->dev, vma, bo->vaddr,
  417. bo->paddr, gem->size);
  418. if (ret) {
  419. drm_gem_vm_close(vma);
  420. return ret;
  421. }
  422. vma->vm_pgoff = vm_pgoff;
  423. } else {
  424. pgprot_t prot = vm_get_page_prot(vma->vm_flags);
  425. vma->vm_flags |= VM_MIXEDMAP;
  426. vma->vm_flags &= ~VM_PFNMAP;
  427. vma->vm_page_prot = pgprot_writecombine(prot);
  428. }
  429. return 0;
  430. }
  431. static struct sg_table *
  432. tegra_gem_prime_map_dma_buf(struct dma_buf_attachment *attach,
  433. enum dma_data_direction dir)
  434. {
  435. struct drm_gem_object *gem = attach->dmabuf->priv;
  436. struct tegra_bo *bo = to_tegra_bo(gem);
  437. struct sg_table *sgt;
  438. sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
  439. if (!sgt)
  440. return NULL;
  441. if (bo->pages) {
  442. struct scatterlist *sg;
  443. unsigned int i;
  444. if (sg_alloc_table(sgt, bo->num_pages, GFP_KERNEL))
  445. goto free;
  446. for_each_sg(sgt->sgl, sg, bo->num_pages, i)
  447. sg_set_page(sg, bo->pages[i], PAGE_SIZE, 0);
  448. if (dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir) == 0)
  449. goto free;
  450. } else {
  451. if (sg_alloc_table(sgt, 1, GFP_KERNEL))
  452. goto free;
  453. sg_dma_address(sgt->sgl) = bo->paddr;
  454. sg_dma_len(sgt->sgl) = gem->size;
  455. }
  456. return sgt;
  457. free:
  458. sg_free_table(sgt);
  459. kfree(sgt);
  460. return NULL;
  461. }
  462. static void tegra_gem_prime_unmap_dma_buf(struct dma_buf_attachment *attach,
  463. struct sg_table *sgt,
  464. enum dma_data_direction dir)
  465. {
  466. struct drm_gem_object *gem = attach->dmabuf->priv;
  467. struct tegra_bo *bo = to_tegra_bo(gem);
  468. if (bo->pages)
  469. dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
  470. sg_free_table(sgt);
  471. kfree(sgt);
  472. }
  473. static void tegra_gem_prime_release(struct dma_buf *buf)
  474. {
  475. drm_gem_dmabuf_release(buf);
  476. }
  477. static void *tegra_gem_prime_kmap_atomic(struct dma_buf *buf,
  478. unsigned long page)
  479. {
  480. return NULL;
  481. }
  482. static void tegra_gem_prime_kunmap_atomic(struct dma_buf *buf,
  483. unsigned long page,
  484. void *addr)
  485. {
  486. }
  487. static void *tegra_gem_prime_kmap(struct dma_buf *buf, unsigned long page)
  488. {
  489. return NULL;
  490. }
  491. static void tegra_gem_prime_kunmap(struct dma_buf *buf, unsigned long page,
  492. void *addr)
  493. {
  494. }
  495. static int tegra_gem_prime_mmap(struct dma_buf *buf, struct vm_area_struct *vma)
  496. {
  497. return -EINVAL;
  498. }
  499. static void *tegra_gem_prime_vmap(struct dma_buf *buf)
  500. {
  501. struct drm_gem_object *gem = buf->priv;
  502. struct tegra_bo *bo = to_tegra_bo(gem);
  503. return bo->vaddr;
  504. }
  505. static void tegra_gem_prime_vunmap(struct dma_buf *buf, void *vaddr)
  506. {
  507. }
  508. static const struct dma_buf_ops tegra_gem_prime_dmabuf_ops = {
  509. .map_dma_buf = tegra_gem_prime_map_dma_buf,
  510. .unmap_dma_buf = tegra_gem_prime_unmap_dma_buf,
  511. .release = tegra_gem_prime_release,
  512. .kmap_atomic = tegra_gem_prime_kmap_atomic,
  513. .kunmap_atomic = tegra_gem_prime_kunmap_atomic,
  514. .kmap = tegra_gem_prime_kmap,
  515. .kunmap = tegra_gem_prime_kunmap,
  516. .mmap = tegra_gem_prime_mmap,
  517. .vmap = tegra_gem_prime_vmap,
  518. .vunmap = tegra_gem_prime_vunmap,
  519. };
  520. struct dma_buf *tegra_gem_prime_export(struct drm_device *drm,
  521. struct drm_gem_object *gem,
  522. int flags)
  523. {
  524. return dma_buf_export(gem, &tegra_gem_prime_dmabuf_ops, gem->size,
  525. flags, NULL);
  526. }
  527. struct drm_gem_object *tegra_gem_prime_import(struct drm_device *drm,
  528. struct dma_buf *buf)
  529. {
  530. struct tegra_bo *bo;
  531. if (buf->ops == &tegra_gem_prime_dmabuf_ops) {
  532. struct drm_gem_object *gem = buf->priv;
  533. if (gem->dev == drm) {
  534. drm_gem_object_reference(gem);
  535. return gem;
  536. }
  537. }
  538. bo = tegra_bo_import(drm, buf);
  539. if (IS_ERR(bo))
  540. return ERR_CAST(bo);
  541. return &bo->gem;
  542. }