nouveau_gem.c 23 KB

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