nouveau_gem.c 22 KB

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