nouveau_gem.c 23 KB

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