nouveau_gem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /*
  2. * Copyright (C) 2008 Ben Skeggs.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include "nouveau_drm.h"
  27. #include "nouveau_dma.h"
  28. #include "nouveau_fence.h"
  29. #include "nouveau_abi16.h"
  30. #include "nouveau_ttm.h"
  31. #include "nouveau_gem.h"
  32. void
  33. nouveau_gem_object_del(struct drm_gem_object *gem)
  34. {
  35. struct nouveau_bo *nvbo = nouveau_gem_object(gem);
  36. struct ttm_buffer_object *bo = &nvbo->bo;
  37. if (gem->import_attach)
  38. drm_prime_gem_destroy(gem, nvbo->bo.sg);
  39. drm_gem_object_release(gem);
  40. /* reset filp so nouveau_bo_del_ttm() can test for it */
  41. gem->filp = NULL;
  42. ttm_bo_unref(&bo);
  43. }
  44. int
  45. nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
  46. {
  47. struct nouveau_cli *cli = nouveau_cli(file_priv);
  48. struct nouveau_bo *nvbo = nouveau_gem_object(gem);
  49. struct nouveau_vma *vma;
  50. int ret;
  51. if (!cli->vm)
  52. return 0;
  53. ret = ttm_bo_reserve(&nvbo->bo, false, false, false, NULL);
  54. if (ret)
  55. return ret;
  56. vma = nouveau_bo_vma_find(nvbo, cli->vm);
  57. if (!vma) {
  58. vma = kzalloc(sizeof(*vma), GFP_KERNEL);
  59. if (!vma) {
  60. ret = -ENOMEM;
  61. goto out;
  62. }
  63. ret = nouveau_bo_vma_add(nvbo, cli->vm, vma);
  64. if (ret) {
  65. kfree(vma);
  66. goto out;
  67. }
  68. } else {
  69. vma->refcount++;
  70. }
  71. out:
  72. ttm_bo_unreserve(&nvbo->bo);
  73. return ret;
  74. }
  75. static void
  76. nouveau_gem_object_delete(void *data)
  77. {
  78. struct nouveau_vma *vma = data;
  79. nouveau_vm_unmap(vma);
  80. nouveau_vm_put(vma);
  81. kfree(vma);
  82. }
  83. static void
  84. nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
  85. {
  86. const bool mapped = nvbo->bo.mem.mem_type != TTM_PL_SYSTEM;
  87. struct nouveau_fence *fence = NULL;
  88. list_del(&vma->head);
  89. if (mapped) {
  90. spin_lock(&nvbo->bo.bdev->fence_lock);
  91. fence = nouveau_fence_ref(nvbo->bo.sync_obj);
  92. spin_unlock(&nvbo->bo.bdev->fence_lock);
  93. }
  94. if (fence) {
  95. nouveau_fence_work(fence, nouveau_gem_object_delete, vma);
  96. } else {
  97. if (mapped)
  98. nouveau_vm_unmap(vma);
  99. nouveau_vm_put(vma);
  100. kfree(vma);
  101. }
  102. nouveau_fence_unref(&fence);
  103. }
  104. void
  105. nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
  106. {
  107. struct nouveau_cli *cli = nouveau_cli(file_priv);
  108. struct nouveau_bo *nvbo = nouveau_gem_object(gem);
  109. struct nouveau_vma *vma;
  110. int ret;
  111. if (!cli->vm)
  112. return;
  113. ret = ttm_bo_reserve(&nvbo->bo, false, false, false, NULL);
  114. if (ret)
  115. return;
  116. vma = nouveau_bo_vma_find(nvbo, cli->vm);
  117. if (vma) {
  118. if (--vma->refcount == 0)
  119. nouveau_gem_object_unmap(nvbo, vma);
  120. }
  121. ttm_bo_unreserve(&nvbo->bo);
  122. }
  123. int
  124. nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
  125. uint32_t tile_mode, uint32_t tile_flags,
  126. struct nouveau_bo **pnvbo)
  127. {
  128. struct nouveau_drm *drm = nouveau_drm(dev);
  129. struct nouveau_bo *nvbo;
  130. u32 flags = 0;
  131. int ret;
  132. if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
  133. flags |= TTM_PL_FLAG_VRAM;
  134. if (domain & NOUVEAU_GEM_DOMAIN_GART)
  135. flags |= TTM_PL_FLAG_TT;
  136. if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
  137. flags |= TTM_PL_FLAG_SYSTEM;
  138. ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
  139. tile_flags, NULL, pnvbo);
  140. if (ret)
  141. return ret;
  142. nvbo = *pnvbo;
  143. /* we restrict allowed domains on nv50+ to only the types
  144. * that were requested at creation time. not possibly on
  145. * earlier chips without busting the ABI.
  146. */
  147. nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
  148. NOUVEAU_GEM_DOMAIN_GART;
  149. if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA)
  150. nvbo->valid_domains &= domain;
  151. /* Initialize the embedded gem-object. We return a single gem-reference
  152. * to the caller, instead of a normal nouveau_bo ttm reference. */
  153. ret = drm_gem_object_init(dev, &nvbo->gem, nvbo->bo.mem.size);
  154. if (ret) {
  155. nouveau_bo_ref(NULL, pnvbo);
  156. return -ENOMEM;
  157. }
  158. nvbo->bo.persistent_swap_storage = nvbo->gem.filp;
  159. return 0;
  160. }
  161. static int
  162. nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
  163. struct drm_nouveau_gem_info *rep)
  164. {
  165. struct nouveau_cli *cli = nouveau_cli(file_priv);
  166. struct nouveau_bo *nvbo = nouveau_gem_object(gem);
  167. struct nouveau_vma *vma;
  168. if (nvbo->bo.mem.mem_type == TTM_PL_TT)
  169. rep->domain = NOUVEAU_GEM_DOMAIN_GART;
  170. else
  171. rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
  172. rep->offset = nvbo->bo.offset;
  173. if (cli->vm) {
  174. vma = nouveau_bo_vma_find(nvbo, cli->vm);
  175. if (!vma)
  176. return -EINVAL;
  177. rep->offset = vma->offset;
  178. }
  179. rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
  180. rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node);
  181. rep->tile_mode = nvbo->tile_mode;
  182. rep->tile_flags = nvbo->tile_flags;
  183. return 0;
  184. }
  185. int
  186. nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
  187. struct drm_file *file_priv)
  188. {
  189. struct nouveau_drm *drm = nouveau_drm(dev);
  190. struct nouveau_cli *cli = nouveau_cli(file_priv);
  191. struct nouveau_fb *pfb = nvkm_fb(&drm->device);
  192. struct drm_nouveau_gem_new *req = data;
  193. struct nouveau_bo *nvbo = NULL;
  194. int ret = 0;
  195. if (!pfb->memtype_valid(pfb, req->info.tile_flags)) {
  196. NV_PRINTK(error, cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
  197. return -EINVAL;
  198. }
  199. ret = nouveau_gem_new(dev, req->info.size, req->align,
  200. req->info.domain, req->info.tile_mode,
  201. req->info.tile_flags, &nvbo);
  202. if (ret)
  203. return ret;
  204. ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle);
  205. if (ret == 0) {
  206. ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info);
  207. if (ret)
  208. drm_gem_handle_delete(file_priv, req->info.handle);
  209. }
  210. /* drop reference from allocate - handle holds it now */
  211. drm_gem_object_unreference_unlocked(&nvbo->gem);
  212. return ret;
  213. }
  214. static int
  215. nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
  216. uint32_t write_domains, uint32_t valid_domains)
  217. {
  218. struct nouveau_bo *nvbo = nouveau_gem_object(gem);
  219. struct ttm_buffer_object *bo = &nvbo->bo;
  220. uint32_t domains = valid_domains & nvbo->valid_domains &
  221. (write_domains ? write_domains : read_domains);
  222. uint32_t pref_flags = 0, valid_flags = 0;
  223. if (!domains)
  224. return -EINVAL;
  225. if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
  226. valid_flags |= TTM_PL_FLAG_VRAM;
  227. if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
  228. valid_flags |= TTM_PL_FLAG_TT;
  229. if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
  230. bo->mem.mem_type == TTM_PL_VRAM)
  231. pref_flags |= TTM_PL_FLAG_VRAM;
  232. else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
  233. bo->mem.mem_type == TTM_PL_TT)
  234. pref_flags |= TTM_PL_FLAG_TT;
  235. else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
  236. pref_flags |= TTM_PL_FLAG_VRAM;
  237. else
  238. pref_flags |= TTM_PL_FLAG_TT;
  239. nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
  240. return 0;
  241. }
  242. struct validate_op {
  243. struct list_head vram_list;
  244. struct list_head gart_list;
  245. struct list_head both_list;
  246. struct ww_acquire_ctx ticket;
  247. };
  248. static void
  249. validate_fini_list(struct list_head *list, struct nouveau_fence *fence,
  250. struct ww_acquire_ctx *ticket)
  251. {
  252. struct list_head *entry, *tmp;
  253. struct nouveau_bo *nvbo;
  254. list_for_each_safe(entry, tmp, list) {
  255. nvbo = list_entry(entry, struct nouveau_bo, entry);
  256. if (likely(fence))
  257. nouveau_bo_fence(nvbo, fence);
  258. if (unlikely(nvbo->validate_mapped)) {
  259. ttm_bo_kunmap(&nvbo->kmap);
  260. nvbo->validate_mapped = false;
  261. }
  262. list_del(&nvbo->entry);
  263. nvbo->reserved_by = NULL;
  264. ttm_bo_unreserve_ticket(&nvbo->bo, ticket);
  265. drm_gem_object_unreference_unlocked(&nvbo->gem);
  266. }
  267. }
  268. static void
  269. validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence)
  270. {
  271. validate_fini_list(&op->vram_list, fence, &op->ticket);
  272. validate_fini_list(&op->gart_list, fence, &op->ticket);
  273. validate_fini_list(&op->both_list, fence, &op->ticket);
  274. }
  275. static void
  276. validate_fini(struct validate_op *op, struct nouveau_fence *fence)
  277. {
  278. validate_fini_no_ticket(op, fence);
  279. ww_acquire_fini(&op->ticket);
  280. }
  281. static int
  282. validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
  283. struct drm_nouveau_gem_pushbuf_bo *pbbo,
  284. int nr_buffers, struct validate_op *op)
  285. {
  286. struct nouveau_cli *cli = nouveau_cli(file_priv);
  287. struct drm_device *dev = chan->drm->dev;
  288. int trycnt = 0;
  289. int ret, i;
  290. struct nouveau_bo *res_bo = NULL;
  291. ww_acquire_init(&op->ticket, &reservation_ww_class);
  292. retry:
  293. if (++trycnt > 100000) {
  294. NV_PRINTK(error, cli, "%s failed and gave up.\n", __func__);
  295. return -EINVAL;
  296. }
  297. for (i = 0; i < nr_buffers; i++) {
  298. struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
  299. struct drm_gem_object *gem;
  300. struct nouveau_bo *nvbo;
  301. gem = drm_gem_object_lookup(dev, file_priv, b->handle);
  302. if (!gem) {
  303. NV_PRINTK(error, cli, "Unknown handle 0x%08x\n", b->handle);
  304. ww_acquire_done(&op->ticket);
  305. validate_fini(op, NULL);
  306. return -ENOENT;
  307. }
  308. nvbo = nouveau_gem_object(gem);
  309. if (nvbo == res_bo) {
  310. res_bo = NULL;
  311. drm_gem_object_unreference_unlocked(gem);
  312. continue;
  313. }
  314. if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
  315. NV_PRINTK(error, cli, "multiple instances of buffer %d on "
  316. "validation list\n", b->handle);
  317. drm_gem_object_unreference_unlocked(gem);
  318. ww_acquire_done(&op->ticket);
  319. validate_fini(op, NULL);
  320. return -EINVAL;
  321. }
  322. ret = ttm_bo_reserve(&nvbo->bo, true, false, true, &op->ticket);
  323. if (ret) {
  324. validate_fini_no_ticket(op, NULL);
  325. if (unlikely(ret == -EDEADLK)) {
  326. ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
  327. &op->ticket);
  328. if (!ret)
  329. res_bo = nvbo;
  330. }
  331. if (unlikely(ret)) {
  332. ww_acquire_done(&op->ticket);
  333. ww_acquire_fini(&op->ticket);
  334. drm_gem_object_unreference_unlocked(gem);
  335. if (ret != -ERESTARTSYS)
  336. NV_PRINTK(error, cli, "fail reserve\n");
  337. return ret;
  338. }
  339. }
  340. b->user_priv = (uint64_t)(unsigned long)nvbo;
  341. nvbo->reserved_by = file_priv;
  342. nvbo->pbbo_index = i;
  343. if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
  344. (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
  345. list_add_tail(&nvbo->entry, &op->both_list);
  346. else
  347. if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
  348. list_add_tail(&nvbo->entry, &op->vram_list);
  349. else
  350. if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
  351. list_add_tail(&nvbo->entry, &op->gart_list);
  352. else {
  353. NV_PRINTK(error, cli, "invalid valid domains: 0x%08x\n",
  354. b->valid_domains);
  355. list_add_tail(&nvbo->entry, &op->both_list);
  356. ww_acquire_done(&op->ticket);
  357. validate_fini(op, NULL);
  358. return -EINVAL;
  359. }
  360. if (nvbo == res_bo)
  361. goto retry;
  362. }
  363. ww_acquire_done(&op->ticket);
  364. return 0;
  365. }
  366. static int
  367. validate_sync(struct nouveau_channel *chan, struct nouveau_bo *nvbo)
  368. {
  369. struct nouveau_fence *fence = NULL;
  370. int ret = 0;
  371. spin_lock(&nvbo->bo.bdev->fence_lock);
  372. fence = nouveau_fence_ref(nvbo->bo.sync_obj);
  373. spin_unlock(&nvbo->bo.bdev->fence_lock);
  374. if (fence) {
  375. ret = nouveau_fence_sync(fence, chan);
  376. nouveau_fence_unref(&fence);
  377. }
  378. return ret;
  379. }
  380. static int
  381. validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
  382. struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
  383. uint64_t user_pbbo_ptr)
  384. {
  385. struct nouveau_drm *drm = chan->drm;
  386. struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
  387. (void __force __user *)(uintptr_t)user_pbbo_ptr;
  388. struct nouveau_bo *nvbo;
  389. int ret, relocs = 0;
  390. list_for_each_entry(nvbo, list, entry) {
  391. struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
  392. ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
  393. b->write_domains,
  394. b->valid_domains);
  395. if (unlikely(ret)) {
  396. NV_PRINTK(error, cli, "fail set_domain\n");
  397. return ret;
  398. }
  399. ret = nouveau_bo_validate(nvbo, true, false);
  400. if (unlikely(ret)) {
  401. if (ret != -ERESTARTSYS)
  402. NV_PRINTK(error, cli, "fail ttm_validate\n");
  403. return ret;
  404. }
  405. ret = validate_sync(chan, nvbo);
  406. if (unlikely(ret)) {
  407. NV_PRINTK(error, cli, "fail post-validate sync\n");
  408. return ret;
  409. }
  410. if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  411. if (nvbo->bo.offset == b->presumed.offset &&
  412. ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
  413. b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
  414. (nvbo->bo.mem.mem_type == TTM_PL_TT &&
  415. b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
  416. continue;
  417. if (nvbo->bo.mem.mem_type == TTM_PL_TT)
  418. b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
  419. else
  420. b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
  421. b->presumed.offset = nvbo->bo.offset;
  422. b->presumed.valid = 0;
  423. relocs++;
  424. if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed,
  425. &b->presumed, sizeof(b->presumed)))
  426. return -EFAULT;
  427. }
  428. }
  429. return relocs;
  430. }
  431. static int
  432. nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
  433. struct drm_file *file_priv,
  434. struct drm_nouveau_gem_pushbuf_bo *pbbo,
  435. uint64_t user_buffers, int nr_buffers,
  436. struct validate_op *op, int *apply_relocs)
  437. {
  438. struct nouveau_cli *cli = nouveau_cli(file_priv);
  439. int ret, relocs = 0;
  440. INIT_LIST_HEAD(&op->vram_list);
  441. INIT_LIST_HEAD(&op->gart_list);
  442. INIT_LIST_HEAD(&op->both_list);
  443. if (nr_buffers == 0)
  444. return 0;
  445. ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
  446. if (unlikely(ret)) {
  447. if (ret != -ERESTARTSYS)
  448. NV_PRINTK(error, cli, "validate_init\n");
  449. return ret;
  450. }
  451. ret = validate_list(chan, cli, &op->vram_list, pbbo, user_buffers);
  452. if (unlikely(ret < 0)) {
  453. if (ret != -ERESTARTSYS)
  454. NV_PRINTK(error, cli, "validate vram_list\n");
  455. validate_fini(op, NULL);
  456. return ret;
  457. }
  458. relocs += ret;
  459. ret = validate_list(chan, cli, &op->gart_list, pbbo, user_buffers);
  460. if (unlikely(ret < 0)) {
  461. if (ret != -ERESTARTSYS)
  462. NV_PRINTK(error, cli, "validate gart_list\n");
  463. validate_fini(op, NULL);
  464. return ret;
  465. }
  466. relocs += ret;
  467. ret = validate_list(chan, cli, &op->both_list, pbbo, user_buffers);
  468. if (unlikely(ret < 0)) {
  469. if (ret != -ERESTARTSYS)
  470. NV_PRINTK(error, cli, "validate both_list\n");
  471. validate_fini(op, NULL);
  472. return ret;
  473. }
  474. relocs += ret;
  475. *apply_relocs = relocs;
  476. return 0;
  477. }
  478. static inline void
  479. u_free(void *addr)
  480. {
  481. if (!is_vmalloc_addr(addr))
  482. kfree(addr);
  483. else
  484. vfree(addr);
  485. }
  486. static inline void *
  487. u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
  488. {
  489. void *mem;
  490. void __user *userptr = (void __force __user *)(uintptr_t)user;
  491. size *= nmemb;
  492. mem = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
  493. if (!mem)
  494. mem = vmalloc(size);
  495. if (!mem)
  496. return ERR_PTR(-ENOMEM);
  497. if (copy_from_user(mem, userptr, size)) {
  498. u_free(mem);
  499. return ERR_PTR(-EFAULT);
  500. }
  501. return mem;
  502. }
  503. static int
  504. nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
  505. struct drm_nouveau_gem_pushbuf *req,
  506. struct drm_nouveau_gem_pushbuf_bo *bo)
  507. {
  508. struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
  509. int ret = 0;
  510. unsigned i;
  511. reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
  512. if (IS_ERR(reloc))
  513. return PTR_ERR(reloc);
  514. for (i = 0; i < req->nr_relocs; i++) {
  515. struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
  516. struct drm_nouveau_gem_pushbuf_bo *b;
  517. struct nouveau_bo *nvbo;
  518. uint32_t data;
  519. if (unlikely(r->bo_index > req->nr_buffers)) {
  520. NV_PRINTK(error, cli, "reloc bo index invalid\n");
  521. ret = -EINVAL;
  522. break;
  523. }
  524. b = &bo[r->bo_index];
  525. if (b->presumed.valid)
  526. continue;
  527. if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
  528. NV_PRINTK(error, cli, "reloc container bo index invalid\n");
  529. ret = -EINVAL;
  530. break;
  531. }
  532. nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
  533. if (unlikely(r->reloc_bo_offset + 4 >
  534. nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
  535. NV_PRINTK(error, cli, "reloc outside of bo\n");
  536. ret = -EINVAL;
  537. break;
  538. }
  539. if (!nvbo->kmap.virtual) {
  540. ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
  541. &nvbo->kmap);
  542. if (ret) {
  543. NV_PRINTK(error, cli, "failed kmap for reloc\n");
  544. break;
  545. }
  546. nvbo->validate_mapped = true;
  547. }
  548. if (r->flags & NOUVEAU_GEM_RELOC_LOW)
  549. data = b->presumed.offset + r->data;
  550. else
  551. if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
  552. data = (b->presumed.offset + r->data) >> 32;
  553. else
  554. data = r->data;
  555. if (r->flags & NOUVEAU_GEM_RELOC_OR) {
  556. if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
  557. data |= r->tor;
  558. else
  559. data |= r->vor;
  560. }
  561. spin_lock(&nvbo->bo.bdev->fence_lock);
  562. ret = ttm_bo_wait(&nvbo->bo, false, false, false);
  563. spin_unlock(&nvbo->bo.bdev->fence_lock);
  564. if (ret) {
  565. NV_PRINTK(error, cli, "reloc wait_idle failed: %d\n", ret);
  566. break;
  567. }
  568. nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
  569. }
  570. u_free(reloc);
  571. return ret;
  572. }
  573. int
  574. nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
  575. struct drm_file *file_priv)
  576. {
  577. struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
  578. struct nouveau_cli *cli = nouveau_cli(file_priv);
  579. struct nouveau_abi16_chan *temp;
  580. struct nouveau_drm *drm = nouveau_drm(dev);
  581. struct drm_nouveau_gem_pushbuf *req = data;
  582. struct drm_nouveau_gem_pushbuf_push *push;
  583. struct drm_nouveau_gem_pushbuf_bo *bo;
  584. struct nouveau_channel *chan = NULL;
  585. struct validate_op op;
  586. struct nouveau_fence *fence = NULL;
  587. int i, j, ret = 0, do_reloc = 0;
  588. if (unlikely(!abi16))
  589. return -ENOMEM;
  590. list_for_each_entry(temp, &abi16->channels, head) {
  591. if (temp->chan->object->handle == (NVDRM_CHAN | req->channel)) {
  592. chan = temp->chan;
  593. break;
  594. }
  595. }
  596. if (!chan)
  597. return nouveau_abi16_put(abi16, -ENOENT);
  598. req->vram_available = drm->gem.vram_available;
  599. req->gart_available = drm->gem.gart_available;
  600. if (unlikely(req->nr_push == 0))
  601. goto out_next;
  602. if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
  603. NV_PRINTK(error, cli, "pushbuf push count exceeds limit: %d max %d\n",
  604. req->nr_push, NOUVEAU_GEM_MAX_PUSH);
  605. return nouveau_abi16_put(abi16, -EINVAL);
  606. }
  607. if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
  608. NV_PRINTK(error, cli, "pushbuf bo count exceeds limit: %d max %d\n",
  609. req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
  610. return nouveau_abi16_put(abi16, -EINVAL);
  611. }
  612. if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
  613. NV_PRINTK(error, cli, "pushbuf reloc count exceeds limit: %d max %d\n",
  614. req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
  615. return nouveau_abi16_put(abi16, -EINVAL);
  616. }
  617. push = u_memcpya(req->push, req->nr_push, sizeof(*push));
  618. if (IS_ERR(push))
  619. return nouveau_abi16_put(abi16, PTR_ERR(push));
  620. bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
  621. if (IS_ERR(bo)) {
  622. u_free(push);
  623. return nouveau_abi16_put(abi16, PTR_ERR(bo));
  624. }
  625. /* Ensure all push buffers are on validate list */
  626. for (i = 0; i < req->nr_push; i++) {
  627. if (push[i].bo_index >= req->nr_buffers) {
  628. NV_PRINTK(error, cli, "push %d buffer not in list\n", i);
  629. ret = -EINVAL;
  630. goto out_prevalid;
  631. }
  632. }
  633. /* Validate buffer list */
  634. ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
  635. req->nr_buffers, &op, &do_reloc);
  636. if (ret) {
  637. if (ret != -ERESTARTSYS)
  638. NV_PRINTK(error, cli, "validate: %d\n", ret);
  639. goto out_prevalid;
  640. }
  641. /* Apply any relocations that are required */
  642. if (do_reloc) {
  643. ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
  644. if (ret) {
  645. NV_PRINTK(error, cli, "reloc apply: %d\n", ret);
  646. goto out;
  647. }
  648. }
  649. if (chan->dma.ib_max) {
  650. ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
  651. if (ret) {
  652. NV_PRINTK(error, cli, "nv50cal_space: %d\n", ret);
  653. goto out;
  654. }
  655. for (i = 0; i < req->nr_push; i++) {
  656. struct nouveau_bo *nvbo = (void *)(unsigned long)
  657. bo[push[i].bo_index].user_priv;
  658. nv50_dma_push(chan, nvbo, push[i].offset,
  659. push[i].length);
  660. }
  661. } else
  662. if (drm->device.info.chipset >= 0x25) {
  663. ret = RING_SPACE(chan, req->nr_push * 2);
  664. if (ret) {
  665. NV_PRINTK(error, cli, "cal_space: %d\n", ret);
  666. goto out;
  667. }
  668. for (i = 0; i < req->nr_push; i++) {
  669. struct nouveau_bo *nvbo = (void *)(unsigned long)
  670. bo[push[i].bo_index].user_priv;
  671. OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
  672. OUT_RING(chan, 0);
  673. }
  674. } else {
  675. ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
  676. if (ret) {
  677. NV_PRINTK(error, cli, "jmp_space: %d\n", ret);
  678. goto out;
  679. }
  680. for (i = 0; i < req->nr_push; i++) {
  681. struct nouveau_bo *nvbo = (void *)(unsigned long)
  682. bo[push[i].bo_index].user_priv;
  683. uint32_t cmd;
  684. cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
  685. cmd |= 0x20000000;
  686. if (unlikely(cmd != req->suffix0)) {
  687. if (!nvbo->kmap.virtual) {
  688. ret = ttm_bo_kmap(&nvbo->bo, 0,
  689. nvbo->bo.mem.
  690. num_pages,
  691. &nvbo->kmap);
  692. if (ret) {
  693. WIND_RING(chan);
  694. goto out;
  695. }
  696. nvbo->validate_mapped = true;
  697. }
  698. nouveau_bo_wr32(nvbo, (push[i].offset +
  699. push[i].length - 8) / 4, cmd);
  700. }
  701. OUT_RING(chan, 0x20000000 |
  702. (nvbo->bo.offset + push[i].offset));
  703. OUT_RING(chan, 0);
  704. for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
  705. OUT_RING(chan, 0);
  706. }
  707. }
  708. ret = nouveau_fence_new(chan, false, &fence);
  709. if (ret) {
  710. NV_PRINTK(error, cli, "error fencing pushbuf: %d\n", ret);
  711. WIND_RING(chan);
  712. goto out;
  713. }
  714. out:
  715. validate_fini(&op, fence);
  716. nouveau_fence_unref(&fence);
  717. out_prevalid:
  718. u_free(bo);
  719. u_free(push);
  720. out_next:
  721. if (chan->dma.ib_max) {
  722. req->suffix0 = 0x00000000;
  723. req->suffix1 = 0x00000000;
  724. } else
  725. if (drm->device.info.chipset >= 0x25) {
  726. req->suffix0 = 0x00020000;
  727. req->suffix1 = 0x00000000;
  728. } else {
  729. req->suffix0 = 0x20000000 |
  730. (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
  731. req->suffix1 = 0x00000000;
  732. }
  733. return nouveau_abi16_put(abi16, ret);
  734. }
  735. static inline uint32_t
  736. domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
  737. {
  738. uint32_t flags = 0;
  739. if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
  740. flags |= TTM_PL_FLAG_VRAM;
  741. if (domain & NOUVEAU_GEM_DOMAIN_GART)
  742. flags |= TTM_PL_FLAG_TT;
  743. return flags;
  744. }
  745. int
  746. nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
  747. struct drm_file *file_priv)
  748. {
  749. struct drm_nouveau_gem_cpu_prep *req = data;
  750. struct drm_gem_object *gem;
  751. struct nouveau_bo *nvbo;
  752. bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
  753. int ret = -EINVAL;
  754. gem = drm_gem_object_lookup(dev, file_priv, req->handle);
  755. if (!gem)
  756. return -ENOENT;
  757. nvbo = nouveau_gem_object(gem);
  758. spin_lock(&nvbo->bo.bdev->fence_lock);
  759. ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
  760. spin_unlock(&nvbo->bo.bdev->fence_lock);
  761. drm_gem_object_unreference_unlocked(gem);
  762. return ret;
  763. }
  764. int
  765. nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
  766. struct drm_file *file_priv)
  767. {
  768. return 0;
  769. }
  770. int
  771. nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
  772. struct drm_file *file_priv)
  773. {
  774. struct drm_nouveau_gem_info *req = data;
  775. struct drm_gem_object *gem;
  776. int ret;
  777. gem = drm_gem_object_lookup(dev, file_priv, req->handle);
  778. if (!gem)
  779. return -ENOENT;
  780. ret = nouveau_gem_info(file_priv, gem, req);
  781. drm_gem_object_unreference_unlocked(gem);
  782. return ret;
  783. }