nouveau_gem.c 24 KB

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