amdxdna_gem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2024, Advanced Micro Devices, Inc.
  4. */
  5. #include <drm/amdxdna_accel.h>
  6. #include <drm/drm_cache.h>
  7. #include <drm/drm_device.h>
  8. #include <drm/drm_gem.h>
  9. #include <drm/drm_gem_shmem_helper.h>
  10. #include <drm/drm_print.h>
  11. #include <drm/gpu_scheduler.h>
  12. #include <linux/dma-buf.h>
  13. #include <linux/dma-direct.h>
  14. #include <linux/iosys-map.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/vmalloc.h>
  17. #include "amdxdna_ctx.h"
  18. #include "amdxdna_gem.h"
  19. #include "amdxdna_pci_drv.h"
  20. #include "amdxdna_ubuf.h"
  21. MODULE_IMPORT_NS("DMA_BUF");
  22. static int
  23. amdxdna_gem_heap_alloc(struct amdxdna_gem_obj *abo)
  24. {
  25. struct amdxdna_client *client = abo->client;
  26. struct amdxdna_dev *xdna = client->xdna;
  27. struct amdxdna_mem *mem = &abo->mem;
  28. struct amdxdna_gem_obj *heap;
  29. u64 offset;
  30. u32 align;
  31. int ret;
  32. mutex_lock(&client->mm_lock);
  33. heap = client->dev_heap;
  34. if (!heap) {
  35. ret = -EINVAL;
  36. goto unlock_out;
  37. }
  38. if (heap->mem.userptr == AMDXDNA_INVALID_ADDR) {
  39. XDNA_ERR(xdna, "Invalid dev heap userptr");
  40. ret = -EINVAL;
  41. goto unlock_out;
  42. }
  43. if (mem->size == 0 || mem->size > heap->mem.size) {
  44. XDNA_ERR(xdna, "Invalid dev bo size 0x%lx, limit 0x%lx",
  45. mem->size, heap->mem.size);
  46. ret = -EINVAL;
  47. goto unlock_out;
  48. }
  49. align = 1 << max(PAGE_SHIFT, xdna->dev_info->dev_mem_buf_shift);
  50. ret = drm_mm_insert_node_generic(&heap->mm, &abo->mm_node,
  51. mem->size, align,
  52. 0, DRM_MM_INSERT_BEST);
  53. if (ret) {
  54. XDNA_ERR(xdna, "Failed to alloc dev bo memory, ret %d", ret);
  55. goto unlock_out;
  56. }
  57. mem->dev_addr = abo->mm_node.start;
  58. offset = mem->dev_addr - heap->mem.dev_addr;
  59. mem->userptr = heap->mem.userptr + offset;
  60. mem->kva = heap->mem.kva + offset;
  61. drm_gem_object_get(to_gobj(heap));
  62. unlock_out:
  63. mutex_unlock(&client->mm_lock);
  64. return ret;
  65. }
  66. static void
  67. amdxdna_gem_destroy_obj(struct amdxdna_gem_obj *abo)
  68. {
  69. mutex_destroy(&abo->lock);
  70. kfree(abo);
  71. }
  72. static void
  73. amdxdna_gem_heap_free(struct amdxdna_gem_obj *abo)
  74. {
  75. struct amdxdna_gem_obj *heap;
  76. mutex_lock(&abo->client->mm_lock);
  77. drm_mm_remove_node(&abo->mm_node);
  78. heap = abo->client->dev_heap;
  79. drm_gem_object_put(to_gobj(heap));
  80. mutex_unlock(&abo->client->mm_lock);
  81. }
  82. static bool amdxdna_hmm_invalidate(struct mmu_interval_notifier *mni,
  83. const struct mmu_notifier_range *range,
  84. unsigned long cur_seq)
  85. {
  86. struct amdxdna_umap *mapp = container_of(mni, struct amdxdna_umap, notifier);
  87. struct amdxdna_gem_obj *abo = mapp->abo;
  88. struct amdxdna_dev *xdna;
  89. xdna = to_xdna_dev(to_gobj(abo)->dev);
  90. XDNA_DBG(xdna, "Invalidating range 0x%lx, 0x%lx, type %d",
  91. mapp->vma->vm_start, mapp->vma->vm_end, abo->type);
  92. if (!mmu_notifier_range_blockable(range))
  93. return false;
  94. down_write(&xdna->notifier_lock);
  95. abo->mem.map_invalid = true;
  96. mapp->invalid = true;
  97. mmu_interval_set_seq(&mapp->notifier, cur_seq);
  98. up_write(&xdna->notifier_lock);
  99. xdna->dev_info->ops->hmm_invalidate(abo, cur_seq);
  100. if (range->event == MMU_NOTIFY_UNMAP) {
  101. down_write(&xdna->notifier_lock);
  102. if (!mapp->unmapped) {
  103. queue_work(xdna->notifier_wq, &mapp->hmm_unreg_work);
  104. mapp->unmapped = true;
  105. }
  106. up_write(&xdna->notifier_lock);
  107. }
  108. return true;
  109. }
  110. static const struct mmu_interval_notifier_ops amdxdna_hmm_ops = {
  111. .invalidate = amdxdna_hmm_invalidate,
  112. };
  113. static void amdxdna_hmm_unregister(struct amdxdna_gem_obj *abo,
  114. struct vm_area_struct *vma)
  115. {
  116. struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
  117. struct amdxdna_umap *mapp;
  118. down_read(&xdna->notifier_lock);
  119. list_for_each_entry(mapp, &abo->mem.umap_list, node) {
  120. if (!vma || mapp->vma == vma) {
  121. if (!mapp->unmapped) {
  122. queue_work(xdna->notifier_wq, &mapp->hmm_unreg_work);
  123. mapp->unmapped = true;
  124. }
  125. if (vma)
  126. break;
  127. }
  128. }
  129. up_read(&xdna->notifier_lock);
  130. }
  131. static void amdxdna_umap_release(struct kref *ref)
  132. {
  133. struct amdxdna_umap *mapp = container_of(ref, struct amdxdna_umap, refcnt);
  134. struct vm_area_struct *vma = mapp->vma;
  135. struct amdxdna_dev *xdna;
  136. mmu_interval_notifier_remove(&mapp->notifier);
  137. if (is_import_bo(mapp->abo) && vma->vm_file && vma->vm_file->f_mapping)
  138. mapping_clear_unevictable(vma->vm_file->f_mapping);
  139. xdna = to_xdna_dev(to_gobj(mapp->abo)->dev);
  140. down_write(&xdna->notifier_lock);
  141. list_del(&mapp->node);
  142. up_write(&xdna->notifier_lock);
  143. kvfree(mapp->range.hmm_pfns);
  144. kfree(mapp);
  145. }
  146. void amdxdna_umap_put(struct amdxdna_umap *mapp)
  147. {
  148. kref_put(&mapp->refcnt, amdxdna_umap_release);
  149. }
  150. static void amdxdna_hmm_unreg_work(struct work_struct *work)
  151. {
  152. struct amdxdna_umap *mapp = container_of(work, struct amdxdna_umap,
  153. hmm_unreg_work);
  154. amdxdna_umap_put(mapp);
  155. }
  156. static int amdxdna_hmm_register(struct amdxdna_gem_obj *abo,
  157. struct vm_area_struct *vma)
  158. {
  159. struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
  160. unsigned long len = vma->vm_end - vma->vm_start;
  161. unsigned long addr = vma->vm_start;
  162. struct amdxdna_umap *mapp;
  163. u32 nr_pages;
  164. int ret;
  165. if (!xdna->dev_info->ops->hmm_invalidate)
  166. return 0;
  167. mapp = kzalloc_obj(*mapp);
  168. if (!mapp)
  169. return -ENOMEM;
  170. nr_pages = (PAGE_ALIGN(addr + len) - (addr & PAGE_MASK)) >> PAGE_SHIFT;
  171. mapp->range.hmm_pfns = kvzalloc_objs(*mapp->range.hmm_pfns, nr_pages);
  172. if (!mapp->range.hmm_pfns) {
  173. ret = -ENOMEM;
  174. goto free_map;
  175. }
  176. ret = mmu_interval_notifier_insert_locked(&mapp->notifier,
  177. current->mm,
  178. addr,
  179. len,
  180. &amdxdna_hmm_ops);
  181. if (ret) {
  182. XDNA_ERR(xdna, "Insert mmu notifier failed, ret %d", ret);
  183. goto free_pfns;
  184. }
  185. mapp->range.notifier = &mapp->notifier;
  186. mapp->range.start = vma->vm_start;
  187. mapp->range.end = vma->vm_end;
  188. mapp->range.default_flags = HMM_PFN_REQ_FAULT;
  189. mapp->vma = vma;
  190. mapp->abo = abo;
  191. kref_init(&mapp->refcnt);
  192. if (abo->mem.userptr == AMDXDNA_INVALID_ADDR)
  193. abo->mem.userptr = addr;
  194. INIT_WORK(&mapp->hmm_unreg_work, amdxdna_hmm_unreg_work);
  195. if (is_import_bo(abo) && vma->vm_file && vma->vm_file->f_mapping)
  196. mapping_set_unevictable(vma->vm_file->f_mapping);
  197. down_write(&xdna->notifier_lock);
  198. list_add_tail(&mapp->node, &abo->mem.umap_list);
  199. up_write(&xdna->notifier_lock);
  200. return 0;
  201. free_pfns:
  202. kvfree(mapp->range.hmm_pfns);
  203. free_map:
  204. kfree(mapp);
  205. return ret;
  206. }
  207. static void amdxdna_gem_dev_obj_free(struct drm_gem_object *gobj)
  208. {
  209. struct amdxdna_dev *xdna = to_xdna_dev(gobj->dev);
  210. struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
  211. XDNA_DBG(xdna, "BO type %d xdna_addr 0x%llx", abo->type, abo->mem.dev_addr);
  212. if (abo->pinned)
  213. amdxdna_gem_unpin(abo);
  214. amdxdna_gem_heap_free(abo);
  215. drm_gem_object_release(gobj);
  216. amdxdna_gem_destroy_obj(abo);
  217. }
  218. static int amdxdna_insert_pages(struct amdxdna_gem_obj *abo,
  219. struct vm_area_struct *vma)
  220. {
  221. struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
  222. unsigned long num_pages = vma_pages(vma);
  223. unsigned long offset = 0;
  224. int ret;
  225. if (!is_import_bo(abo)) {
  226. ret = drm_gem_shmem_mmap(&abo->base, vma);
  227. if (ret) {
  228. XDNA_ERR(xdna, "Failed shmem mmap %d", ret);
  229. return ret;
  230. }
  231. /* The buffer is based on memory pages. Fix the flag. */
  232. vm_flags_mod(vma, VM_MIXEDMAP, VM_PFNMAP);
  233. ret = vm_insert_pages(vma, vma->vm_start, abo->base.pages,
  234. &num_pages);
  235. if (ret) {
  236. XDNA_ERR(xdna, "Failed insert pages %d", ret);
  237. vma->vm_ops->close(vma);
  238. return ret;
  239. }
  240. return 0;
  241. }
  242. vma->vm_private_data = NULL;
  243. vma->vm_ops = NULL;
  244. ret = dma_buf_mmap(abo->dma_buf, vma, 0);
  245. if (ret) {
  246. XDNA_ERR(xdna, "Failed to mmap dma buf %d", ret);
  247. return ret;
  248. }
  249. do {
  250. vm_fault_t fault_ret;
  251. fault_ret = handle_mm_fault(vma, vma->vm_start + offset,
  252. FAULT_FLAG_WRITE, NULL);
  253. if (fault_ret & VM_FAULT_ERROR) {
  254. vma->vm_ops->close(vma);
  255. XDNA_ERR(xdna, "Fault in page failed");
  256. return -EFAULT;
  257. }
  258. offset += PAGE_SIZE;
  259. } while (--num_pages);
  260. /* Drop the reference drm_gem_mmap_obj() acquired.*/
  261. drm_gem_object_put(to_gobj(abo));
  262. return 0;
  263. }
  264. static int amdxdna_gem_obj_mmap(struct drm_gem_object *gobj,
  265. struct vm_area_struct *vma)
  266. {
  267. struct amdxdna_dev *xdna = to_xdna_dev(gobj->dev);
  268. struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
  269. int ret;
  270. ret = amdxdna_hmm_register(abo, vma);
  271. if (ret)
  272. return ret;
  273. ret = amdxdna_insert_pages(abo, vma);
  274. if (ret) {
  275. XDNA_ERR(xdna, "Failed insert pages, ret %d", ret);
  276. goto hmm_unreg;
  277. }
  278. XDNA_DBG(xdna, "BO map_offset 0x%llx type %d userptr 0x%lx size 0x%lx",
  279. drm_vma_node_offset_addr(&gobj->vma_node), abo->type,
  280. vma->vm_start, gobj->size);
  281. return 0;
  282. hmm_unreg:
  283. amdxdna_hmm_unregister(abo, vma);
  284. return ret;
  285. }
  286. static int amdxdna_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
  287. {
  288. struct drm_gem_object *gobj = dma_buf->priv;
  289. struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
  290. unsigned long num_pages = vma_pages(vma);
  291. int ret;
  292. vma->vm_ops = &drm_gem_shmem_vm_ops;
  293. vma->vm_private_data = gobj;
  294. drm_gem_object_get(gobj);
  295. ret = drm_gem_shmem_mmap(&abo->base, vma);
  296. if (ret)
  297. goto put_obj;
  298. /* The buffer is based on memory pages. Fix the flag. */
  299. vm_flags_mod(vma, VM_MIXEDMAP, VM_PFNMAP);
  300. ret = vm_insert_pages(vma, vma->vm_start, abo->base.pages,
  301. &num_pages);
  302. if (ret)
  303. goto close_vma;
  304. return 0;
  305. close_vma:
  306. vma->vm_ops->close(vma);
  307. put_obj:
  308. drm_gem_object_put(gobj);
  309. return ret;
  310. }
  311. static const struct dma_buf_ops amdxdna_dmabuf_ops = {
  312. .attach = drm_gem_map_attach,
  313. .detach = drm_gem_map_detach,
  314. .map_dma_buf = drm_gem_map_dma_buf,
  315. .unmap_dma_buf = drm_gem_unmap_dma_buf,
  316. .release = drm_gem_dmabuf_release,
  317. .mmap = amdxdna_gem_dmabuf_mmap,
  318. .vmap = drm_gem_dmabuf_vmap,
  319. .vunmap = drm_gem_dmabuf_vunmap,
  320. };
  321. static int amdxdna_gem_obj_vmap(struct amdxdna_gem_obj *abo, void **vaddr)
  322. {
  323. struct iosys_map map = IOSYS_MAP_INIT_VADDR(NULL);
  324. int ret;
  325. if (is_import_bo(abo))
  326. ret = dma_buf_vmap_unlocked(abo->dma_buf, &map);
  327. else
  328. ret = drm_gem_vmap(to_gobj(abo), &map);
  329. *vaddr = map.vaddr;
  330. return ret;
  331. }
  332. static void amdxdna_gem_obj_vunmap(struct amdxdna_gem_obj *abo)
  333. {
  334. struct iosys_map map;
  335. if (!abo->mem.kva)
  336. return;
  337. iosys_map_set_vaddr(&map, abo->mem.kva);
  338. if (is_import_bo(abo))
  339. dma_buf_vunmap_unlocked(abo->dma_buf, &map);
  340. else
  341. drm_gem_vunmap(to_gobj(abo), &map);
  342. }
  343. static struct dma_buf *amdxdna_gem_prime_export(struct drm_gem_object *gobj, int flags)
  344. {
  345. struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
  346. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  347. if (abo->dma_buf) {
  348. get_dma_buf(abo->dma_buf);
  349. return abo->dma_buf;
  350. }
  351. exp_info.ops = &amdxdna_dmabuf_ops;
  352. exp_info.size = gobj->size;
  353. exp_info.flags = flags;
  354. exp_info.priv = gobj;
  355. exp_info.resv = gobj->resv;
  356. return drm_gem_dmabuf_export(gobj->dev, &exp_info);
  357. }
  358. static void amdxdna_imported_obj_free(struct amdxdna_gem_obj *abo)
  359. {
  360. dma_buf_unmap_attachment_unlocked(abo->attach, abo->base.sgt, DMA_BIDIRECTIONAL);
  361. dma_buf_detach(abo->dma_buf, abo->attach);
  362. dma_buf_put(abo->dma_buf);
  363. drm_gem_object_release(to_gobj(abo));
  364. kfree(abo);
  365. }
  366. static void amdxdna_gem_obj_free(struct drm_gem_object *gobj)
  367. {
  368. struct amdxdna_dev *xdna = to_xdna_dev(gobj->dev);
  369. struct amdxdna_gem_obj *abo = to_xdna_obj(gobj);
  370. XDNA_DBG(xdna, "BO type %d xdna_addr 0x%llx", abo->type, abo->mem.dev_addr);
  371. amdxdna_hmm_unregister(abo, NULL);
  372. flush_workqueue(xdna->notifier_wq);
  373. if (abo->pinned)
  374. amdxdna_gem_unpin(abo);
  375. if (abo->type == AMDXDNA_BO_DEV_HEAP)
  376. drm_mm_takedown(&abo->mm);
  377. amdxdna_gem_obj_vunmap(abo);
  378. mutex_destroy(&abo->lock);
  379. if (is_import_bo(abo)) {
  380. amdxdna_imported_obj_free(abo);
  381. return;
  382. }
  383. drm_gem_shmem_free(&abo->base);
  384. }
  385. static const struct drm_gem_object_funcs amdxdna_gem_dev_obj_funcs = {
  386. .free = amdxdna_gem_dev_obj_free,
  387. };
  388. static const struct drm_gem_object_funcs amdxdna_gem_shmem_funcs = {
  389. .free = amdxdna_gem_obj_free,
  390. .print_info = drm_gem_shmem_object_print_info,
  391. .pin = drm_gem_shmem_object_pin,
  392. .unpin = drm_gem_shmem_object_unpin,
  393. .get_sg_table = drm_gem_shmem_object_get_sg_table,
  394. .vmap = drm_gem_shmem_object_vmap,
  395. .vunmap = drm_gem_shmem_object_vunmap,
  396. .mmap = amdxdna_gem_obj_mmap,
  397. .vm_ops = &drm_gem_shmem_vm_ops,
  398. .export = amdxdna_gem_prime_export,
  399. };
  400. static struct amdxdna_gem_obj *
  401. amdxdna_gem_create_obj(struct drm_device *dev, size_t size)
  402. {
  403. struct amdxdna_gem_obj *abo;
  404. abo = kzalloc_obj(*abo);
  405. if (!abo)
  406. return ERR_PTR(-ENOMEM);
  407. abo->pinned = false;
  408. abo->assigned_hwctx = AMDXDNA_INVALID_CTX_HANDLE;
  409. mutex_init(&abo->lock);
  410. abo->mem.userptr = AMDXDNA_INVALID_ADDR;
  411. abo->mem.dev_addr = AMDXDNA_INVALID_ADDR;
  412. abo->mem.size = size;
  413. INIT_LIST_HEAD(&abo->mem.umap_list);
  414. return abo;
  415. }
  416. /* For drm_driver->gem_create_object callback */
  417. struct drm_gem_object *
  418. amdxdna_gem_create_object_cb(struct drm_device *dev, size_t size)
  419. {
  420. struct amdxdna_gem_obj *abo;
  421. abo = amdxdna_gem_create_obj(dev, size);
  422. if (IS_ERR(abo))
  423. return ERR_CAST(abo);
  424. to_gobj(abo)->funcs = &amdxdna_gem_shmem_funcs;
  425. return to_gobj(abo);
  426. }
  427. static struct amdxdna_gem_obj *
  428. amdxdna_gem_create_shmem_object(struct drm_device *dev, size_t size)
  429. {
  430. struct drm_gem_shmem_object *shmem = drm_gem_shmem_create(dev, size);
  431. if (IS_ERR(shmem))
  432. return ERR_CAST(shmem);
  433. shmem->map_wc = false;
  434. return to_xdna_obj(&shmem->base);
  435. }
  436. static struct amdxdna_gem_obj *
  437. amdxdna_gem_create_ubuf_object(struct drm_device *dev, struct amdxdna_drm_create_bo *args)
  438. {
  439. struct amdxdna_dev *xdna = to_xdna_dev(dev);
  440. enum amdxdna_ubuf_flag flags = 0;
  441. struct amdxdna_drm_va_tbl va_tbl;
  442. struct drm_gem_object *gobj;
  443. struct dma_buf *dma_buf;
  444. if (copy_from_user(&va_tbl, u64_to_user_ptr(args->vaddr), sizeof(va_tbl))) {
  445. XDNA_DBG(xdna, "Access va table failed");
  446. return ERR_PTR(-EINVAL);
  447. }
  448. if (va_tbl.num_entries) {
  449. if (args->type == AMDXDNA_BO_CMD)
  450. flags |= AMDXDNA_UBUF_FLAG_MAP_DMA;
  451. dma_buf = amdxdna_get_ubuf(dev, flags, va_tbl.num_entries,
  452. u64_to_user_ptr(args->vaddr + sizeof(va_tbl)));
  453. } else {
  454. dma_buf = dma_buf_get(va_tbl.dmabuf_fd);
  455. }
  456. if (IS_ERR(dma_buf))
  457. return ERR_CAST(dma_buf);
  458. gobj = amdxdna_gem_prime_import(dev, dma_buf);
  459. if (IS_ERR(gobj)) {
  460. dma_buf_put(dma_buf);
  461. return ERR_CAST(gobj);
  462. }
  463. dma_buf_put(dma_buf);
  464. return to_xdna_obj(gobj);
  465. }
  466. static struct amdxdna_gem_obj *
  467. amdxdna_gem_create_object(struct drm_device *dev,
  468. struct amdxdna_drm_create_bo *args)
  469. {
  470. size_t aligned_sz = PAGE_ALIGN(args->size);
  471. if (args->vaddr)
  472. return amdxdna_gem_create_ubuf_object(dev, args);
  473. return amdxdna_gem_create_shmem_object(dev, aligned_sz);
  474. }
  475. struct drm_gem_object *
  476. amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)
  477. {
  478. struct dma_buf_attachment *attach;
  479. struct amdxdna_gem_obj *abo;
  480. struct drm_gem_object *gobj;
  481. struct sg_table *sgt;
  482. int ret;
  483. get_dma_buf(dma_buf);
  484. attach = dma_buf_attach(dma_buf, dev->dev);
  485. if (IS_ERR(attach)) {
  486. ret = PTR_ERR(attach);
  487. goto put_buf;
  488. }
  489. sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
  490. if (IS_ERR(sgt)) {
  491. ret = PTR_ERR(sgt);
  492. goto fail_detach;
  493. }
  494. gobj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt);
  495. if (IS_ERR(gobj)) {
  496. ret = PTR_ERR(gobj);
  497. goto fail_unmap;
  498. }
  499. abo = to_xdna_obj(gobj);
  500. abo->attach = attach;
  501. abo->dma_buf = dma_buf;
  502. return gobj;
  503. fail_unmap:
  504. dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
  505. fail_detach:
  506. dma_buf_detach(dma_buf, attach);
  507. put_buf:
  508. dma_buf_put(dma_buf);
  509. return ERR_PTR(ret);
  510. }
  511. static struct amdxdna_gem_obj *
  512. amdxdna_drm_alloc_shmem(struct drm_device *dev,
  513. struct amdxdna_drm_create_bo *args,
  514. struct drm_file *filp)
  515. {
  516. struct amdxdna_client *client = filp->driver_priv;
  517. struct amdxdna_gem_obj *abo;
  518. abo = amdxdna_gem_create_object(dev, args);
  519. if (IS_ERR(abo))
  520. return ERR_CAST(abo);
  521. abo->client = client;
  522. abo->type = AMDXDNA_BO_SHMEM;
  523. return abo;
  524. }
  525. static struct amdxdna_gem_obj *
  526. amdxdna_drm_create_dev_heap(struct drm_device *dev,
  527. struct amdxdna_drm_create_bo *args,
  528. struct drm_file *filp)
  529. {
  530. struct amdxdna_client *client = filp->driver_priv;
  531. struct amdxdna_dev *xdna = to_xdna_dev(dev);
  532. struct amdxdna_gem_obj *abo;
  533. int ret;
  534. if (args->size > xdna->dev_info->dev_mem_size) {
  535. XDNA_DBG(xdna, "Invalid dev heap size 0x%llx, limit 0x%lx",
  536. args->size, xdna->dev_info->dev_mem_size);
  537. return ERR_PTR(-EINVAL);
  538. }
  539. mutex_lock(&client->mm_lock);
  540. if (client->dev_heap) {
  541. XDNA_DBG(client->xdna, "dev heap is already created");
  542. ret = -EBUSY;
  543. goto mm_unlock;
  544. }
  545. abo = amdxdna_gem_create_object(dev, args);
  546. if (IS_ERR(abo)) {
  547. ret = PTR_ERR(abo);
  548. goto mm_unlock;
  549. }
  550. abo->type = AMDXDNA_BO_DEV_HEAP;
  551. abo->client = client;
  552. abo->mem.dev_addr = client->xdna->dev_info->dev_mem_base;
  553. drm_mm_init(&abo->mm, abo->mem.dev_addr, abo->mem.size);
  554. ret = amdxdna_gem_obj_vmap(abo, &abo->mem.kva);
  555. if (ret) {
  556. XDNA_ERR(xdna, "Vmap heap bo failed, ret %d", ret);
  557. goto release_obj;
  558. }
  559. client->dev_heap = abo;
  560. drm_gem_object_get(to_gobj(abo));
  561. mutex_unlock(&client->mm_lock);
  562. return abo;
  563. release_obj:
  564. drm_gem_object_put(to_gobj(abo));
  565. mm_unlock:
  566. mutex_unlock(&client->mm_lock);
  567. return ERR_PTR(ret);
  568. }
  569. struct amdxdna_gem_obj *
  570. amdxdna_drm_alloc_dev_bo(struct drm_device *dev,
  571. struct amdxdna_drm_create_bo *args,
  572. struct drm_file *filp)
  573. {
  574. struct amdxdna_client *client = filp->driver_priv;
  575. struct amdxdna_dev *xdna = to_xdna_dev(dev);
  576. size_t aligned_sz = PAGE_ALIGN(args->size);
  577. struct amdxdna_gem_obj *abo;
  578. int ret;
  579. abo = amdxdna_gem_create_obj(&xdna->ddev, aligned_sz);
  580. if (IS_ERR(abo))
  581. return abo;
  582. to_gobj(abo)->funcs = &amdxdna_gem_dev_obj_funcs;
  583. abo->type = AMDXDNA_BO_DEV;
  584. abo->client = client;
  585. ret = amdxdna_gem_heap_alloc(abo);
  586. if (ret) {
  587. XDNA_ERR(xdna, "Failed to alloc dev bo memory, ret %d", ret);
  588. amdxdna_gem_destroy_obj(abo);
  589. return ERR_PTR(ret);
  590. }
  591. drm_gem_private_object_init(&xdna->ddev, to_gobj(abo), aligned_sz);
  592. return abo;
  593. }
  594. static struct amdxdna_gem_obj *
  595. amdxdna_drm_create_cmd_bo(struct drm_device *dev,
  596. struct amdxdna_drm_create_bo *args,
  597. struct drm_file *filp)
  598. {
  599. struct amdxdna_dev *xdna = to_xdna_dev(dev);
  600. struct amdxdna_gem_obj *abo;
  601. if (args->size < sizeof(struct amdxdna_cmd)) {
  602. XDNA_DBG(xdna, "Command BO size 0x%llx too small", args->size);
  603. return ERR_PTR(-EINVAL);
  604. }
  605. abo = amdxdna_gem_create_object(dev, args);
  606. if (IS_ERR(abo))
  607. return ERR_CAST(abo);
  608. abo->type = AMDXDNA_BO_CMD;
  609. abo->client = filp->driver_priv;
  610. return abo;
  611. }
  612. int amdxdna_drm_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  613. {
  614. struct amdxdna_dev *xdna = to_xdna_dev(dev);
  615. struct amdxdna_drm_create_bo *args = data;
  616. struct amdxdna_gem_obj *abo;
  617. int ret;
  618. if (args->flags)
  619. return -EINVAL;
  620. XDNA_DBG(xdna, "BO arg type %d vaddr 0x%llx size 0x%llx flags 0x%llx",
  621. args->type, args->vaddr, args->size, args->flags);
  622. switch (args->type) {
  623. case AMDXDNA_BO_SHMEM:
  624. abo = amdxdna_drm_alloc_shmem(dev, args, filp);
  625. break;
  626. case AMDXDNA_BO_DEV_HEAP:
  627. abo = amdxdna_drm_create_dev_heap(dev, args, filp);
  628. break;
  629. case AMDXDNA_BO_DEV:
  630. abo = amdxdna_drm_alloc_dev_bo(dev, args, filp);
  631. break;
  632. case AMDXDNA_BO_CMD:
  633. abo = amdxdna_drm_create_cmd_bo(dev, args, filp);
  634. break;
  635. default:
  636. return -EINVAL;
  637. }
  638. if (IS_ERR(abo))
  639. return PTR_ERR(abo);
  640. /* ready to publish object to userspace */
  641. ret = drm_gem_handle_create(filp, to_gobj(abo), &args->handle);
  642. if (ret) {
  643. XDNA_ERR(xdna, "Create handle failed");
  644. goto put_obj;
  645. }
  646. XDNA_DBG(xdna, "BO hdl %d type %d userptr 0x%llx xdna_addr 0x%llx size 0x%lx",
  647. args->handle, args->type, abo->mem.userptr,
  648. abo->mem.dev_addr, abo->mem.size);
  649. put_obj:
  650. /* Dereference object reference. Handle holds it now. */
  651. drm_gem_object_put(to_gobj(abo));
  652. return ret;
  653. }
  654. int amdxdna_gem_pin_nolock(struct amdxdna_gem_obj *abo)
  655. {
  656. struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
  657. int ret;
  658. if (abo->type == AMDXDNA_BO_DEV)
  659. abo = abo->client->dev_heap;
  660. if (is_import_bo(abo))
  661. return 0;
  662. ret = drm_gem_shmem_pin(&abo->base);
  663. XDNA_DBG(xdna, "BO type %d ret %d", abo->type, ret);
  664. return ret;
  665. }
  666. int amdxdna_gem_pin(struct amdxdna_gem_obj *abo)
  667. {
  668. int ret;
  669. mutex_lock(&abo->lock);
  670. ret = amdxdna_gem_pin_nolock(abo);
  671. mutex_unlock(&abo->lock);
  672. return ret;
  673. }
  674. void amdxdna_gem_unpin(struct amdxdna_gem_obj *abo)
  675. {
  676. if (abo->type == AMDXDNA_BO_DEV)
  677. abo = abo->client->dev_heap;
  678. if (is_import_bo(abo))
  679. return;
  680. mutex_lock(&abo->lock);
  681. drm_gem_shmem_unpin(&abo->base);
  682. mutex_unlock(&abo->lock);
  683. }
  684. struct amdxdna_gem_obj *amdxdna_gem_get_obj(struct amdxdna_client *client,
  685. u32 bo_hdl, u8 bo_type)
  686. {
  687. struct amdxdna_dev *xdna = client->xdna;
  688. struct amdxdna_gem_obj *abo;
  689. struct drm_gem_object *gobj;
  690. int ret;
  691. gobj = drm_gem_object_lookup(client->filp, bo_hdl);
  692. if (!gobj) {
  693. XDNA_DBG(xdna, "Can not find bo %d", bo_hdl);
  694. return NULL;
  695. }
  696. abo = to_xdna_obj(gobj);
  697. if (bo_type != AMDXDNA_BO_INVALID && abo->type != bo_type)
  698. goto put_obj;
  699. if (bo_type != AMDXDNA_BO_CMD || abo->mem.kva)
  700. return abo;
  701. if (abo->mem.size > SZ_32K) {
  702. XDNA_ERR(xdna, "Cmd bo is too big %ld", abo->mem.size);
  703. goto put_obj;
  704. }
  705. ret = amdxdna_gem_obj_vmap(abo, &abo->mem.kva);
  706. if (ret) {
  707. XDNA_ERR(xdna, "Vmap cmd bo failed, ret %d", ret);
  708. goto put_obj;
  709. }
  710. return abo;
  711. put_obj:
  712. drm_gem_object_put(gobj);
  713. return NULL;
  714. }
  715. int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  716. {
  717. struct amdxdna_drm_get_bo_info *args = data;
  718. struct amdxdna_dev *xdna = to_xdna_dev(dev);
  719. struct amdxdna_gem_obj *abo;
  720. struct drm_gem_object *gobj;
  721. int ret = 0;
  722. if (args->ext || args->ext_flags || args->pad)
  723. return -EINVAL;
  724. gobj = drm_gem_object_lookup(filp, args->handle);
  725. if (!gobj) {
  726. XDNA_DBG(xdna, "Lookup GEM object %d failed", args->handle);
  727. return -ENOENT;
  728. }
  729. abo = to_xdna_obj(gobj);
  730. args->vaddr = abo->mem.userptr;
  731. args->xdna_addr = abo->mem.dev_addr;
  732. if (abo->type != AMDXDNA_BO_DEV)
  733. args->map_offset = drm_vma_node_offset_addr(&gobj->vma_node);
  734. else
  735. args->map_offset = AMDXDNA_INVALID_ADDR;
  736. XDNA_DBG(xdna, "BO hdl %d map_offset 0x%llx vaddr 0x%llx xdna_addr 0x%llx",
  737. args->handle, args->map_offset, args->vaddr, args->xdna_addr);
  738. drm_gem_object_put(gobj);
  739. return ret;
  740. }
  741. /*
  742. * The sync bo ioctl is to make sure the CPU cache is in sync with memory.
  743. * This is required because NPU is not cache coherent device. CPU cache
  744. * flushing/invalidation is expensive so it is best to handle this outside
  745. * of the command submission path. This ioctl allows explicit cache
  746. * flushing/invalidation outside of the critical path.
  747. */
  748. int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev,
  749. void *data, struct drm_file *filp)
  750. {
  751. struct amdxdna_dev *xdna = to_xdna_dev(dev);
  752. struct amdxdna_drm_sync_bo *args = data;
  753. struct amdxdna_gem_obj *abo;
  754. struct drm_gem_object *gobj;
  755. int ret;
  756. gobj = drm_gem_object_lookup(filp, args->handle);
  757. if (!gobj) {
  758. XDNA_ERR(xdna, "Lookup GEM object failed");
  759. return -ENOENT;
  760. }
  761. abo = to_xdna_obj(gobj);
  762. ret = amdxdna_gem_pin(abo);
  763. if (ret) {
  764. XDNA_ERR(xdna, "Pin BO %d failed, ret %d", args->handle, ret);
  765. goto put_obj;
  766. }
  767. if (is_import_bo(abo))
  768. drm_clflush_sg(abo->base.sgt);
  769. else if (abo->mem.kva)
  770. drm_clflush_virt_range(abo->mem.kva + args->offset, args->size);
  771. else if (abo->base.pages)
  772. drm_clflush_pages(abo->base.pages, gobj->size >> PAGE_SHIFT);
  773. else
  774. drm_WARN(&xdna->ddev, 1, "Can not get flush memory");
  775. amdxdna_gem_unpin(abo);
  776. XDNA_DBG(xdna, "Sync bo %d offset 0x%llx, size 0x%llx\n",
  777. args->handle, args->offset, args->size);
  778. if (args->direction == SYNC_DIRECT_FROM_DEVICE)
  779. ret = amdxdna_hwctx_sync_debug_bo(abo->client, args->handle);
  780. put_obj:
  781. drm_gem_object_put(gobj);
  782. return ret;
  783. }