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