nouveau_gem.c 24 KB

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