nouveau_gem.c 22 KB

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