nouveau_gem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. 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, 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 dma_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, 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, 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 nouveau_cli *cli, u64 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(cli->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(cli, 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->client.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(drm->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->client.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(cli, 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. int trycnt = 0;
  312. int ret = -EINVAL, i;
  313. struct nouveau_bo *res_bo = NULL;
  314. LIST_HEAD(gart_list);
  315. LIST_HEAD(vram_list);
  316. LIST_HEAD(both_list);
  317. ww_acquire_init(&op->ticket, &reservation_ww_class);
  318. retry:
  319. if (++trycnt > 100000) {
  320. NV_PRINTK(err, cli, "%s failed and gave up.\n", __func__);
  321. return -EINVAL;
  322. }
  323. for (i = 0; i < nr_buffers; i++) {
  324. struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
  325. struct drm_gem_object *gem;
  326. struct nouveau_bo *nvbo;
  327. gem = drm_gem_object_lookup(file_priv, b->handle);
  328. if (!gem) {
  329. NV_PRINTK(err, cli, "Unknown handle 0x%08x\n", b->handle);
  330. ret = -ENOENT;
  331. break;
  332. }
  333. nvbo = nouveau_gem_object(gem);
  334. if (nvbo == res_bo) {
  335. res_bo = NULL;
  336. drm_gem_object_unreference_unlocked(gem);
  337. continue;
  338. }
  339. if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
  340. NV_PRINTK(err, cli, "multiple instances of buffer %d on "
  341. "validation list\n", b->handle);
  342. drm_gem_object_unreference_unlocked(gem);
  343. ret = -EINVAL;
  344. break;
  345. }
  346. ret = ttm_bo_reserve(&nvbo->bo, true, false, &op->ticket);
  347. if (ret) {
  348. list_splice_tail_init(&vram_list, &op->list);
  349. list_splice_tail_init(&gart_list, &op->list);
  350. list_splice_tail_init(&both_list, &op->list);
  351. validate_fini_no_ticket(op, NULL, NULL);
  352. if (unlikely(ret == -EDEADLK)) {
  353. ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
  354. &op->ticket);
  355. if (!ret)
  356. res_bo = nvbo;
  357. }
  358. if (unlikely(ret)) {
  359. if (ret != -ERESTARTSYS)
  360. NV_PRINTK(err, cli, "fail reserve\n");
  361. break;
  362. }
  363. }
  364. b->user_priv = (uint64_t)(unsigned long)nvbo;
  365. nvbo->reserved_by = file_priv;
  366. nvbo->pbbo_index = i;
  367. if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
  368. (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
  369. list_add_tail(&nvbo->entry, &both_list);
  370. else
  371. if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
  372. list_add_tail(&nvbo->entry, &vram_list);
  373. else
  374. if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
  375. list_add_tail(&nvbo->entry, &gart_list);
  376. else {
  377. NV_PRINTK(err, cli, "invalid valid domains: 0x%08x\n",
  378. b->valid_domains);
  379. list_add_tail(&nvbo->entry, &both_list);
  380. ret = -EINVAL;
  381. break;
  382. }
  383. if (nvbo == res_bo)
  384. goto retry;
  385. }
  386. ww_acquire_done(&op->ticket);
  387. list_splice_tail(&vram_list, &op->list);
  388. list_splice_tail(&gart_list, &op->list);
  389. list_splice_tail(&both_list, &op->list);
  390. if (ret)
  391. validate_fini(op, NULL, NULL);
  392. return ret;
  393. }
  394. static int
  395. validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
  396. struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
  397. uint64_t user_pbbo_ptr)
  398. {
  399. struct nouveau_drm *drm = chan->drm;
  400. struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
  401. (void __force __user *)(uintptr_t)user_pbbo_ptr;
  402. struct nouveau_bo *nvbo;
  403. int ret, relocs = 0;
  404. list_for_each_entry(nvbo, list, entry) {
  405. struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
  406. ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
  407. b->write_domains,
  408. b->valid_domains);
  409. if (unlikely(ret)) {
  410. NV_PRINTK(err, cli, "fail set_domain\n");
  411. return ret;
  412. }
  413. ret = nouveau_bo_validate(nvbo, true, false);
  414. if (unlikely(ret)) {
  415. if (ret != -ERESTARTSYS)
  416. NV_PRINTK(err, cli, "fail ttm_validate\n");
  417. return ret;
  418. }
  419. ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true);
  420. if (unlikely(ret)) {
  421. if (ret != -ERESTARTSYS)
  422. NV_PRINTK(err, cli, "fail post-validate sync\n");
  423. return ret;
  424. }
  425. if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  426. if (nvbo->bo.offset == b->presumed.offset &&
  427. ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
  428. b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
  429. (nvbo->bo.mem.mem_type == TTM_PL_TT &&
  430. b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
  431. continue;
  432. if (nvbo->bo.mem.mem_type == TTM_PL_TT)
  433. b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
  434. else
  435. b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
  436. b->presumed.offset = nvbo->bo.offset;
  437. b->presumed.valid = 0;
  438. relocs++;
  439. if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed,
  440. &b->presumed, sizeof(b->presumed)))
  441. return -EFAULT;
  442. }
  443. }
  444. return relocs;
  445. }
  446. static int
  447. nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
  448. struct drm_file *file_priv,
  449. struct drm_nouveau_gem_pushbuf_bo *pbbo,
  450. uint64_t user_buffers, int nr_buffers,
  451. struct validate_op *op, int *apply_relocs)
  452. {
  453. struct nouveau_cli *cli = nouveau_cli(file_priv);
  454. int ret;
  455. INIT_LIST_HEAD(&op->list);
  456. if (nr_buffers == 0)
  457. return 0;
  458. ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
  459. if (unlikely(ret)) {
  460. if (ret != -ERESTARTSYS)
  461. NV_PRINTK(err, cli, "validate_init\n");
  462. return ret;
  463. }
  464. ret = validate_list(chan, cli, &op->list, pbbo, user_buffers);
  465. if (unlikely(ret < 0)) {
  466. if (ret != -ERESTARTSYS)
  467. NV_PRINTK(err, cli, "validating bo list\n");
  468. validate_fini(op, NULL, NULL);
  469. return ret;
  470. }
  471. *apply_relocs = ret;
  472. return 0;
  473. }
  474. static inline void
  475. u_free(void *addr)
  476. {
  477. kvfree(addr);
  478. }
  479. static inline void *
  480. u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
  481. {
  482. void *mem;
  483. void __user *userptr = (void __force __user *)(uintptr_t)user;
  484. size *= nmemb;
  485. mem = kvmalloc(size, GFP_KERNEL);
  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(err, 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(err, 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(err, 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(err, 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, false, false);
  553. if (ret) {
  554. NV_PRINTK(err, 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);
  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->chid == 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(err, 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(err, 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(err, 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(err, 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(err, 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(err, 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(err, 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->client.device.info.chipset >= 0x25) {
  652. ret = RING_SPACE(chan, req->nr_push * 2);
  653. if (ret) {
  654. NV_PRINTK(err, 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(err, 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(err, 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->client.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. int
  725. nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
  726. struct drm_file *file_priv)
  727. {
  728. struct drm_nouveau_gem_cpu_prep *req = data;
  729. struct drm_gem_object *gem;
  730. struct nouveau_bo *nvbo;
  731. bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
  732. bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
  733. long lret;
  734. int ret;
  735. gem = drm_gem_object_lookup(file_priv, req->handle);
  736. if (!gem)
  737. return -ENOENT;
  738. nvbo = nouveau_gem_object(gem);
  739. lret = reservation_object_wait_timeout_rcu(nvbo->bo.resv, write, true,
  740. no_wait ? 0 : 30 * HZ);
  741. if (!lret)
  742. ret = -EBUSY;
  743. else if (lret > 0)
  744. ret = 0;
  745. else
  746. ret = lret;
  747. nouveau_bo_sync_for_cpu(nvbo);
  748. drm_gem_object_unreference_unlocked(gem);
  749. return ret;
  750. }
  751. int
  752. nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
  753. struct drm_file *file_priv)
  754. {
  755. struct drm_nouveau_gem_cpu_fini *req = data;
  756. struct drm_gem_object *gem;
  757. struct nouveau_bo *nvbo;
  758. gem = drm_gem_object_lookup(file_priv, req->handle);
  759. if (!gem)
  760. return -ENOENT;
  761. nvbo = nouveau_gem_object(gem);
  762. nouveau_bo_sync_for_device(nvbo);
  763. drm_gem_object_unreference_unlocked(gem);
  764. return 0;
  765. }
  766. int
  767. nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
  768. struct drm_file *file_priv)
  769. {
  770. struct drm_nouveau_gem_info *req = data;
  771. struct drm_gem_object *gem;
  772. int ret;
  773. gem = drm_gem_object_lookup(file_priv, req->handle);
  774. if (!gem)
  775. return -ENOENT;
  776. ret = nouveau_gem_info(file_priv, gem, req);
  777. drm_gem_object_unreference_unlocked(gem);
  778. return ret;
  779. }