nouveau_gem.c 23 KB

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