nouveau_gem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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 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. 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->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 = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
  486. if (!mem)
  487. mem = vmalloc(size);
  488. if (!mem)
  489. return ERR_PTR(-ENOMEM);
  490. if (copy_from_user(mem, userptr, size)) {
  491. u_free(mem);
  492. return ERR_PTR(-EFAULT);
  493. }
  494. return mem;
  495. }
  496. static int
  497. nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
  498. struct drm_nouveau_gem_pushbuf *req,
  499. struct drm_nouveau_gem_pushbuf_bo *bo)
  500. {
  501. struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
  502. int ret = 0;
  503. unsigned i;
  504. reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
  505. if (IS_ERR(reloc))
  506. return PTR_ERR(reloc);
  507. for (i = 0; i < req->nr_relocs; i++) {
  508. struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
  509. struct drm_nouveau_gem_pushbuf_bo *b;
  510. struct nouveau_bo *nvbo;
  511. uint32_t data;
  512. if (unlikely(r->bo_index > req->nr_buffers)) {
  513. NV_PRINTK(err, cli, "reloc bo index invalid\n");
  514. ret = -EINVAL;
  515. break;
  516. }
  517. b = &bo[r->bo_index];
  518. if (b->presumed.valid)
  519. continue;
  520. if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
  521. NV_PRINTK(err, cli, "reloc container bo index invalid\n");
  522. ret = -EINVAL;
  523. break;
  524. }
  525. nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
  526. if (unlikely(r->reloc_bo_offset + 4 >
  527. nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
  528. NV_PRINTK(err, cli, "reloc outside of bo\n");
  529. ret = -EINVAL;
  530. break;
  531. }
  532. if (!nvbo->kmap.virtual) {
  533. ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
  534. &nvbo->kmap);
  535. if (ret) {
  536. NV_PRINTK(err, cli, "failed kmap for reloc\n");
  537. break;
  538. }
  539. nvbo->validate_mapped = true;
  540. }
  541. if (r->flags & NOUVEAU_GEM_RELOC_LOW)
  542. data = b->presumed.offset + r->data;
  543. else
  544. if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
  545. data = (b->presumed.offset + r->data) >> 32;
  546. else
  547. data = r->data;
  548. if (r->flags & NOUVEAU_GEM_RELOC_OR) {
  549. if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
  550. data |= r->tor;
  551. else
  552. data |= r->vor;
  553. }
  554. ret = ttm_bo_wait(&nvbo->bo, false, false);
  555. if (ret) {
  556. NV_PRINTK(err, cli, "reloc wait_idle failed: %d\n", ret);
  557. break;
  558. }
  559. nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
  560. }
  561. u_free(reloc);
  562. return ret;
  563. }
  564. int
  565. nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
  566. struct drm_file *file_priv)
  567. {
  568. struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
  569. struct nouveau_cli *cli = nouveau_cli(file_priv);
  570. struct nouveau_abi16_chan *temp;
  571. struct nouveau_drm *drm = nouveau_drm(dev);
  572. struct drm_nouveau_gem_pushbuf *req = data;
  573. struct drm_nouveau_gem_pushbuf_push *push;
  574. struct drm_nouveau_gem_pushbuf_bo *bo;
  575. struct nouveau_channel *chan = NULL;
  576. struct validate_op op;
  577. struct nouveau_fence *fence = NULL;
  578. int i, j, ret = 0, do_reloc = 0;
  579. if (unlikely(!abi16))
  580. return -ENOMEM;
  581. list_for_each_entry(temp, &abi16->channels, head) {
  582. if (temp->chan->chid == req->channel) {
  583. chan = temp->chan;
  584. break;
  585. }
  586. }
  587. if (!chan)
  588. return nouveau_abi16_put(abi16, -ENOENT);
  589. req->vram_available = drm->gem.vram_available;
  590. req->gart_available = drm->gem.gart_available;
  591. if (unlikely(req->nr_push == 0))
  592. goto out_next;
  593. if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
  594. NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n",
  595. req->nr_push, NOUVEAU_GEM_MAX_PUSH);
  596. return nouveau_abi16_put(abi16, -EINVAL);
  597. }
  598. if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
  599. NV_PRINTK(err, cli, "pushbuf bo count exceeds limit: %d max %d\n",
  600. req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
  601. return nouveau_abi16_put(abi16, -EINVAL);
  602. }
  603. if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
  604. NV_PRINTK(err, cli, "pushbuf reloc count exceeds limit: %d max %d\n",
  605. req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
  606. return nouveau_abi16_put(abi16, -EINVAL);
  607. }
  608. push = u_memcpya(req->push, req->nr_push, sizeof(*push));
  609. if (IS_ERR(push))
  610. return nouveau_abi16_put(abi16, PTR_ERR(push));
  611. bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
  612. if (IS_ERR(bo)) {
  613. u_free(push);
  614. return nouveau_abi16_put(abi16, PTR_ERR(bo));
  615. }
  616. /* Ensure all push buffers are on validate list */
  617. for (i = 0; i < req->nr_push; i++) {
  618. if (push[i].bo_index >= req->nr_buffers) {
  619. NV_PRINTK(err, cli, "push %d buffer not in list\n", i);
  620. ret = -EINVAL;
  621. goto out_prevalid;
  622. }
  623. }
  624. /* Validate buffer list */
  625. ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
  626. req->nr_buffers, &op, &do_reloc);
  627. if (ret) {
  628. if (ret != -ERESTARTSYS)
  629. NV_PRINTK(err, cli, "validate: %d\n", ret);
  630. goto out_prevalid;
  631. }
  632. /* Apply any relocations that are required */
  633. if (do_reloc) {
  634. ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
  635. if (ret) {
  636. NV_PRINTK(err, cli, "reloc apply: %d\n", ret);
  637. goto out;
  638. }
  639. }
  640. if (chan->dma.ib_max) {
  641. ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
  642. if (ret) {
  643. NV_PRINTK(err, cli, "nv50cal_space: %d\n", ret);
  644. goto out;
  645. }
  646. for (i = 0; i < req->nr_push; i++) {
  647. struct nouveau_bo *nvbo = (void *)(unsigned long)
  648. bo[push[i].bo_index].user_priv;
  649. nv50_dma_push(chan, nvbo, push[i].offset,
  650. push[i].length);
  651. }
  652. } else
  653. if (drm->device.info.chipset >= 0x25) {
  654. ret = RING_SPACE(chan, req->nr_push * 2);
  655. if (ret) {
  656. NV_PRINTK(err, cli, "cal_space: %d\n", ret);
  657. goto out;
  658. }
  659. for (i = 0; i < req->nr_push; i++) {
  660. struct nouveau_bo *nvbo = (void *)(unsigned long)
  661. bo[push[i].bo_index].user_priv;
  662. OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
  663. OUT_RING(chan, 0);
  664. }
  665. } else {
  666. ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
  667. if (ret) {
  668. NV_PRINTK(err, cli, "jmp_space: %d\n", ret);
  669. goto out;
  670. }
  671. for (i = 0; i < req->nr_push; i++) {
  672. struct nouveau_bo *nvbo = (void *)(unsigned long)
  673. bo[push[i].bo_index].user_priv;
  674. uint32_t cmd;
  675. cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
  676. cmd |= 0x20000000;
  677. if (unlikely(cmd != req->suffix0)) {
  678. if (!nvbo->kmap.virtual) {
  679. ret = ttm_bo_kmap(&nvbo->bo, 0,
  680. nvbo->bo.mem.
  681. num_pages,
  682. &nvbo->kmap);
  683. if (ret) {
  684. WIND_RING(chan);
  685. goto out;
  686. }
  687. nvbo->validate_mapped = true;
  688. }
  689. nouveau_bo_wr32(nvbo, (push[i].offset +
  690. push[i].length - 8) / 4, cmd);
  691. }
  692. OUT_RING(chan, 0x20000000 |
  693. (nvbo->bo.offset + push[i].offset));
  694. OUT_RING(chan, 0);
  695. for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
  696. OUT_RING(chan, 0);
  697. }
  698. }
  699. ret = nouveau_fence_new(chan, false, &fence);
  700. if (ret) {
  701. NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
  702. WIND_RING(chan);
  703. goto out;
  704. }
  705. out:
  706. validate_fini(&op, fence, bo);
  707. nouveau_fence_unref(&fence);
  708. out_prevalid:
  709. u_free(bo);
  710. u_free(push);
  711. out_next:
  712. if (chan->dma.ib_max) {
  713. req->suffix0 = 0x00000000;
  714. req->suffix1 = 0x00000000;
  715. } else
  716. if (drm->device.info.chipset >= 0x25) {
  717. req->suffix0 = 0x00020000;
  718. req->suffix1 = 0x00000000;
  719. } else {
  720. req->suffix0 = 0x20000000 |
  721. (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
  722. req->suffix1 = 0x00000000;
  723. }
  724. return nouveau_abi16_put(abi16, ret);
  725. }
  726. int
  727. nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
  728. struct drm_file *file_priv)
  729. {
  730. struct drm_nouveau_gem_cpu_prep *req = data;
  731. struct drm_gem_object *gem;
  732. struct nouveau_bo *nvbo;
  733. bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
  734. bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
  735. long lret;
  736. int ret;
  737. gem = drm_gem_object_lookup(file_priv, req->handle);
  738. if (!gem)
  739. return -ENOENT;
  740. nvbo = nouveau_gem_object(gem);
  741. lret = reservation_object_wait_timeout_rcu(nvbo->bo.resv, write, true,
  742. no_wait ? 0 : 30 * HZ);
  743. if (!lret)
  744. ret = -EBUSY;
  745. else if (lret > 0)
  746. ret = 0;
  747. else
  748. ret = lret;
  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(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(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. }