nouveau_gem.c 23 KB

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