nouveau_gem.c 24 KB

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