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