nouveau_gem.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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 "drmP.h"
  27. #include "drm.h"
  28. #include "nouveau_drv.h"
  29. #include "nouveau_drm.h"
  30. #include "nouveau_dma.h"
  31. #define nouveau_gem_pushbuf_sync(chan) 0
  32. int
  33. nouveau_gem_object_new(struct drm_gem_object *gem)
  34. {
  35. return 0;
  36. }
  37. void
  38. nouveau_gem_object_del(struct drm_gem_object *gem)
  39. {
  40. struct nouveau_bo *nvbo = gem->driver_private;
  41. struct ttm_buffer_object *bo = &nvbo->bo;
  42. if (!nvbo)
  43. return;
  44. nvbo->gem = NULL;
  45. if (unlikely(nvbo->pin_refcnt)) {
  46. nvbo->pin_refcnt = 1;
  47. nouveau_bo_unpin(nvbo);
  48. }
  49. ttm_bo_unref(&bo);
  50. drm_gem_object_release(gem);
  51. kfree(gem);
  52. }
  53. int
  54. nouveau_gem_new(struct drm_device *dev, struct nouveau_channel *chan,
  55. int size, int align, uint32_t flags, uint32_t tile_mode,
  56. uint32_t tile_flags, struct nouveau_bo **pnvbo)
  57. {
  58. struct nouveau_bo *nvbo;
  59. int ret;
  60. ret = nouveau_bo_new(dev, chan, size, align, flags, tile_mode,
  61. tile_flags, pnvbo);
  62. if (ret)
  63. return ret;
  64. nvbo = *pnvbo;
  65. nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
  66. if (!nvbo->gem) {
  67. nouveau_bo_ref(NULL, pnvbo);
  68. return -ENOMEM;
  69. }
  70. nvbo->bo.persistant_swap_storage = nvbo->gem->filp;
  71. nvbo->gem->driver_private = nvbo;
  72. return 0;
  73. }
  74. static int
  75. nouveau_gem_info(struct drm_gem_object *gem, struct drm_nouveau_gem_info *rep)
  76. {
  77. struct nouveau_bo *nvbo = nouveau_gem_object(gem);
  78. if (nvbo->bo.mem.mem_type == TTM_PL_TT)
  79. rep->domain = NOUVEAU_GEM_DOMAIN_GART;
  80. else
  81. rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
  82. rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
  83. rep->offset = nvbo->bo.offset;
  84. rep->map_handle = nvbo->bo.addr_space_offset;
  85. rep->tile_mode = nvbo->tile_mode;
  86. rep->tile_flags = nvbo->tile_flags;
  87. return 0;
  88. }
  89. int
  90. nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
  91. struct drm_file *file_priv)
  92. {
  93. struct drm_nouveau_private *dev_priv = dev->dev_private;
  94. struct drm_nouveau_gem_new *req = data;
  95. struct nouveau_bo *nvbo = NULL;
  96. struct nouveau_channel *chan = NULL;
  97. uint32_t flags = 0;
  98. int ret = 0;
  99. if (unlikely(dev_priv->ttm.bdev.dev_mapping == NULL))
  100. dev_priv->ttm.bdev.dev_mapping = dev_priv->dev->dev_mapping;
  101. if (req->info.domain & NOUVEAU_GEM_DOMAIN_VRAM)
  102. flags |= TTM_PL_FLAG_VRAM;
  103. if (req->info.domain & NOUVEAU_GEM_DOMAIN_GART)
  104. flags |= TTM_PL_FLAG_TT;
  105. if (!flags || req->info.domain & NOUVEAU_GEM_DOMAIN_CPU)
  106. flags |= TTM_PL_FLAG_SYSTEM;
  107. if (!dev_priv->engine.vram.flags_valid(dev, req->info.tile_flags)) {
  108. NV_ERROR(dev, "bad page flags: 0x%08x\n", req->info.tile_flags);
  109. return -EINVAL;
  110. }
  111. if (req->channel_hint) {
  112. chan = nouveau_channel_get(dev, file_priv, req->channel_hint);
  113. if (IS_ERR(chan))
  114. return PTR_ERR(chan);
  115. }
  116. ret = nouveau_gem_new(dev, chan, req->info.size, req->align, flags,
  117. req->info.tile_mode, req->info.tile_flags, &nvbo);
  118. if (chan)
  119. nouveau_channel_put(&chan);
  120. if (ret)
  121. return ret;
  122. ret = nouveau_gem_info(nvbo->gem, &req->info);
  123. if (ret)
  124. goto out;
  125. ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle);
  126. /* drop reference from allocate - handle holds it now */
  127. drm_gem_object_unreference_unlocked(nvbo->gem);
  128. out:
  129. return ret;
  130. }
  131. static int
  132. nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
  133. uint32_t write_domains, uint32_t valid_domains)
  134. {
  135. struct nouveau_bo *nvbo = gem->driver_private;
  136. struct ttm_buffer_object *bo = &nvbo->bo;
  137. uint32_t domains = valid_domains &
  138. (write_domains ? write_domains : read_domains);
  139. uint32_t pref_flags = 0, valid_flags = 0;
  140. if (!domains)
  141. return -EINVAL;
  142. if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
  143. valid_flags |= TTM_PL_FLAG_VRAM;
  144. if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
  145. valid_flags |= TTM_PL_FLAG_TT;
  146. if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
  147. bo->mem.mem_type == TTM_PL_VRAM)
  148. pref_flags |= TTM_PL_FLAG_VRAM;
  149. else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
  150. bo->mem.mem_type == TTM_PL_TT)
  151. pref_flags |= TTM_PL_FLAG_TT;
  152. else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
  153. pref_flags |= TTM_PL_FLAG_VRAM;
  154. else
  155. pref_flags |= TTM_PL_FLAG_TT;
  156. nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
  157. return 0;
  158. }
  159. struct validate_op {
  160. struct list_head vram_list;
  161. struct list_head gart_list;
  162. struct list_head both_list;
  163. };
  164. static void
  165. validate_fini_list(struct list_head *list, struct nouveau_fence *fence)
  166. {
  167. struct list_head *entry, *tmp;
  168. struct nouveau_bo *nvbo;
  169. list_for_each_safe(entry, tmp, list) {
  170. nvbo = list_entry(entry, struct nouveau_bo, entry);
  171. nouveau_bo_fence(nvbo, fence);
  172. if (unlikely(nvbo->validate_mapped)) {
  173. ttm_bo_kunmap(&nvbo->kmap);
  174. nvbo->validate_mapped = false;
  175. }
  176. list_del(&nvbo->entry);
  177. nvbo->reserved_by = NULL;
  178. ttm_bo_unreserve(&nvbo->bo);
  179. drm_gem_object_unreference_unlocked(nvbo->gem);
  180. }
  181. }
  182. static void
  183. validate_fini(struct validate_op *op, struct nouveau_fence* fence)
  184. {
  185. validate_fini_list(&op->vram_list, fence);
  186. validate_fini_list(&op->gart_list, fence);
  187. validate_fini_list(&op->both_list, fence);
  188. }
  189. static int
  190. validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
  191. struct drm_nouveau_gem_pushbuf_bo *pbbo,
  192. int nr_buffers, struct validate_op *op)
  193. {
  194. struct drm_device *dev = chan->dev;
  195. struct drm_nouveau_private *dev_priv = dev->dev_private;
  196. uint32_t sequence;
  197. int trycnt = 0;
  198. int ret, i;
  199. sequence = atomic_add_return(1, &dev_priv->ttm.validate_sequence);
  200. retry:
  201. if (++trycnt > 100000) {
  202. NV_ERROR(dev, "%s failed and gave up.\n", __func__);
  203. return -EINVAL;
  204. }
  205. for (i = 0; i < nr_buffers; i++) {
  206. struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
  207. struct drm_gem_object *gem;
  208. struct nouveau_bo *nvbo;
  209. gem = drm_gem_object_lookup(dev, file_priv, b->handle);
  210. if (!gem) {
  211. NV_ERROR(dev, "Unknown handle 0x%08x\n", b->handle);
  212. validate_fini(op, NULL);
  213. return -ENOENT;
  214. }
  215. nvbo = gem->driver_private;
  216. if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
  217. NV_ERROR(dev, "multiple instances of buffer %d on "
  218. "validation list\n", b->handle);
  219. validate_fini(op, NULL);
  220. return -EINVAL;
  221. }
  222. ret = ttm_bo_reserve(&nvbo->bo, true, false, true, sequence);
  223. if (ret) {
  224. validate_fini(op, NULL);
  225. if (unlikely(ret == -EAGAIN))
  226. ret = ttm_bo_wait_unreserved(&nvbo->bo, true);
  227. drm_gem_object_unreference_unlocked(gem);
  228. if (unlikely(ret)) {
  229. if (ret != -ERESTARTSYS)
  230. NV_ERROR(dev, "fail reserve\n");
  231. return ret;
  232. }
  233. goto retry;
  234. }
  235. b->user_priv = (uint64_t)(unsigned long)nvbo;
  236. nvbo->reserved_by = file_priv;
  237. nvbo->pbbo_index = i;
  238. if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
  239. (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
  240. list_add_tail(&nvbo->entry, &op->both_list);
  241. else
  242. if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
  243. list_add_tail(&nvbo->entry, &op->vram_list);
  244. else
  245. if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
  246. list_add_tail(&nvbo->entry, &op->gart_list);
  247. else {
  248. NV_ERROR(dev, "invalid valid domains: 0x%08x\n",
  249. b->valid_domains);
  250. list_add_tail(&nvbo->entry, &op->both_list);
  251. validate_fini(op, NULL);
  252. return -EINVAL;
  253. }
  254. }
  255. return 0;
  256. }
  257. static int
  258. validate_list(struct nouveau_channel *chan, struct list_head *list,
  259. struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr)
  260. {
  261. struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
  262. (void __force __user *)(uintptr_t)user_pbbo_ptr;
  263. struct drm_device *dev = chan->dev;
  264. struct nouveau_bo *nvbo;
  265. int ret, relocs = 0;
  266. list_for_each_entry(nvbo, list, entry) {
  267. struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
  268. ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan);
  269. if (unlikely(ret)) {
  270. NV_ERROR(dev, "fail pre-validate sync\n");
  271. return ret;
  272. }
  273. ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
  274. b->write_domains,
  275. b->valid_domains);
  276. if (unlikely(ret)) {
  277. NV_ERROR(dev, "fail set_domain\n");
  278. return ret;
  279. }
  280. nvbo->channel = (b->read_domains & (1 << 31)) ? NULL : chan;
  281. ret = nouveau_bo_validate(nvbo, true, false, false);
  282. nvbo->channel = NULL;
  283. if (unlikely(ret)) {
  284. if (ret != -ERESTARTSYS)
  285. NV_ERROR(dev, "fail ttm_validate\n");
  286. return ret;
  287. }
  288. ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan);
  289. if (unlikely(ret)) {
  290. NV_ERROR(dev, "fail post-validate sync\n");
  291. return ret;
  292. }
  293. if (nvbo->bo.offset == b->presumed.offset &&
  294. ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
  295. b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
  296. (nvbo->bo.mem.mem_type == TTM_PL_TT &&
  297. b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
  298. continue;
  299. if (nvbo->bo.mem.mem_type == TTM_PL_TT)
  300. b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
  301. else
  302. b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
  303. b->presumed.offset = nvbo->bo.offset;
  304. b->presumed.valid = 0;
  305. relocs++;
  306. if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
  307. &b->presumed, sizeof(b->presumed)))
  308. return -EFAULT;
  309. }
  310. return relocs;
  311. }
  312. static int
  313. nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
  314. struct drm_file *file_priv,
  315. struct drm_nouveau_gem_pushbuf_bo *pbbo,
  316. uint64_t user_buffers, int nr_buffers,
  317. struct validate_op *op, int *apply_relocs)
  318. {
  319. struct drm_device *dev = chan->dev;
  320. int ret, relocs = 0;
  321. INIT_LIST_HEAD(&op->vram_list);
  322. INIT_LIST_HEAD(&op->gart_list);
  323. INIT_LIST_HEAD(&op->both_list);
  324. if (nr_buffers == 0)
  325. return 0;
  326. ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
  327. if (unlikely(ret)) {
  328. if (ret != -ERESTARTSYS)
  329. NV_ERROR(dev, "validate_init\n");
  330. return ret;
  331. }
  332. ret = validate_list(chan, &op->vram_list, pbbo, user_buffers);
  333. if (unlikely(ret < 0)) {
  334. if (ret != -ERESTARTSYS)
  335. NV_ERROR(dev, "validate vram_list\n");
  336. validate_fini(op, NULL);
  337. return ret;
  338. }
  339. relocs += ret;
  340. ret = validate_list(chan, &op->gart_list, pbbo, user_buffers);
  341. if (unlikely(ret < 0)) {
  342. if (ret != -ERESTARTSYS)
  343. NV_ERROR(dev, "validate gart_list\n");
  344. validate_fini(op, NULL);
  345. return ret;
  346. }
  347. relocs += ret;
  348. ret = validate_list(chan, &op->both_list, pbbo, user_buffers);
  349. if (unlikely(ret < 0)) {
  350. if (ret != -ERESTARTSYS)
  351. NV_ERROR(dev, "validate both_list\n");
  352. validate_fini(op, NULL);
  353. return ret;
  354. }
  355. relocs += ret;
  356. *apply_relocs = relocs;
  357. return 0;
  358. }
  359. static inline void *
  360. u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
  361. {
  362. void *mem;
  363. void __user *userptr = (void __force __user *)(uintptr_t)user;
  364. mem = kmalloc(nmemb * size, GFP_KERNEL);
  365. if (!mem)
  366. return ERR_PTR(-ENOMEM);
  367. if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
  368. kfree(mem);
  369. return ERR_PTR(-EFAULT);
  370. }
  371. return mem;
  372. }
  373. static int
  374. nouveau_gem_pushbuf_reloc_apply(struct drm_device *dev,
  375. struct drm_nouveau_gem_pushbuf *req,
  376. struct drm_nouveau_gem_pushbuf_bo *bo)
  377. {
  378. struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
  379. int ret = 0;
  380. unsigned i;
  381. reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
  382. if (IS_ERR(reloc))
  383. return PTR_ERR(reloc);
  384. for (i = 0; i < req->nr_relocs; i++) {
  385. struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
  386. struct drm_nouveau_gem_pushbuf_bo *b;
  387. struct nouveau_bo *nvbo;
  388. uint32_t data;
  389. if (unlikely(r->bo_index > req->nr_buffers)) {
  390. NV_ERROR(dev, "reloc bo index invalid\n");
  391. ret = -EINVAL;
  392. break;
  393. }
  394. b = &bo[r->bo_index];
  395. if (b->presumed.valid)
  396. continue;
  397. if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
  398. NV_ERROR(dev, "reloc container bo index invalid\n");
  399. ret = -EINVAL;
  400. break;
  401. }
  402. nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
  403. if (unlikely(r->reloc_bo_offset + 4 >
  404. nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
  405. NV_ERROR(dev, "reloc outside of bo\n");
  406. ret = -EINVAL;
  407. break;
  408. }
  409. if (!nvbo->kmap.virtual) {
  410. ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
  411. &nvbo->kmap);
  412. if (ret) {
  413. NV_ERROR(dev, "failed kmap for reloc\n");
  414. break;
  415. }
  416. nvbo->validate_mapped = true;
  417. }
  418. if (r->flags & NOUVEAU_GEM_RELOC_LOW)
  419. data = b->presumed.offset + r->data;
  420. else
  421. if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
  422. data = (b->presumed.offset + r->data) >> 32;
  423. else
  424. data = r->data;
  425. if (r->flags & NOUVEAU_GEM_RELOC_OR) {
  426. if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
  427. data |= r->tor;
  428. else
  429. data |= r->vor;
  430. }
  431. spin_lock(&nvbo->bo.bdev->fence_lock);
  432. ret = ttm_bo_wait(&nvbo->bo, false, false, false);
  433. spin_unlock(&nvbo->bo.bdev->fence_lock);
  434. if (ret) {
  435. NV_ERROR(dev, "reloc wait_idle failed: %d\n", ret);
  436. break;
  437. }
  438. nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
  439. }
  440. kfree(reloc);
  441. return ret;
  442. }
  443. int
  444. nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
  445. struct drm_file *file_priv)
  446. {
  447. struct drm_nouveau_private *dev_priv = dev->dev_private;
  448. struct drm_nouveau_gem_pushbuf *req = data;
  449. struct drm_nouveau_gem_pushbuf_push *push;
  450. struct drm_nouveau_gem_pushbuf_bo *bo;
  451. struct nouveau_channel *chan;
  452. struct validate_op op;
  453. struct nouveau_fence *fence = NULL;
  454. int i, j, ret = 0, do_reloc = 0;
  455. chan = nouveau_channel_get(dev, file_priv, req->channel);
  456. if (IS_ERR(chan))
  457. return PTR_ERR(chan);
  458. req->vram_available = dev_priv->fb_aper_free;
  459. req->gart_available = dev_priv->gart_info.aper_free;
  460. if (unlikely(req->nr_push == 0))
  461. goto out_next;
  462. if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
  463. NV_ERROR(dev, "pushbuf push count exceeds limit: %d max %d\n",
  464. req->nr_push, NOUVEAU_GEM_MAX_PUSH);
  465. nouveau_channel_put(&chan);
  466. return -EINVAL;
  467. }
  468. if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
  469. NV_ERROR(dev, "pushbuf bo count exceeds limit: %d max %d\n",
  470. req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
  471. nouveau_channel_put(&chan);
  472. return -EINVAL;
  473. }
  474. if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
  475. NV_ERROR(dev, "pushbuf reloc count exceeds limit: %d max %d\n",
  476. req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
  477. nouveau_channel_put(&chan);
  478. return -EINVAL;
  479. }
  480. push = u_memcpya(req->push, req->nr_push, sizeof(*push));
  481. if (IS_ERR(push)) {
  482. nouveau_channel_put(&chan);
  483. return PTR_ERR(push);
  484. }
  485. bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
  486. if (IS_ERR(bo)) {
  487. kfree(push);
  488. nouveau_channel_put(&chan);
  489. return PTR_ERR(bo);
  490. }
  491. /* Mark push buffers as being used on PFIFO, the validation code
  492. * will then make sure that if the pushbuf bo moves, that they
  493. * happen on the kernel channel, which will in turn cause a sync
  494. * to happen before we try and submit the push buffer.
  495. */
  496. for (i = 0; i < req->nr_push; i++) {
  497. if (push[i].bo_index >= req->nr_buffers) {
  498. NV_ERROR(dev, "push %d buffer not in list\n", i);
  499. ret = -EINVAL;
  500. goto out;
  501. }
  502. bo[push[i].bo_index].read_domains |= (1 << 31);
  503. }
  504. /* Validate buffer list */
  505. ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
  506. req->nr_buffers, &op, &do_reloc);
  507. if (ret) {
  508. if (ret != -ERESTARTSYS)
  509. NV_ERROR(dev, "validate: %d\n", ret);
  510. goto out;
  511. }
  512. /* Apply any relocations that are required */
  513. if (do_reloc) {
  514. ret = nouveau_gem_pushbuf_reloc_apply(dev, req, bo);
  515. if (ret) {
  516. NV_ERROR(dev, "reloc apply: %d\n", ret);
  517. goto out;
  518. }
  519. }
  520. if (chan->dma.ib_max) {
  521. ret = nouveau_dma_wait(chan, req->nr_push + 1, 6);
  522. if (ret) {
  523. NV_INFO(dev, "nv50cal_space: %d\n", ret);
  524. goto out;
  525. }
  526. for (i = 0; i < req->nr_push; i++) {
  527. struct nouveau_bo *nvbo = (void *)(unsigned long)
  528. bo[push[i].bo_index].user_priv;
  529. nv50_dma_push(chan, nvbo, push[i].offset,
  530. push[i].length);
  531. }
  532. } else
  533. if (dev_priv->chipset >= 0x25) {
  534. ret = RING_SPACE(chan, req->nr_push * 2);
  535. if (ret) {
  536. NV_ERROR(dev, "cal_space: %d\n", ret);
  537. goto out;
  538. }
  539. for (i = 0; i < req->nr_push; i++) {
  540. struct nouveau_bo *nvbo = (void *)(unsigned long)
  541. bo[push[i].bo_index].user_priv;
  542. struct drm_mm_node *mem = nvbo->bo.mem.mm_node;
  543. OUT_RING(chan, ((mem->start << PAGE_SHIFT) +
  544. push[i].offset) | 2);
  545. OUT_RING(chan, 0);
  546. }
  547. } else {
  548. ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
  549. if (ret) {
  550. NV_ERROR(dev, "jmp_space: %d\n", ret);
  551. goto out;
  552. }
  553. for (i = 0; i < req->nr_push; i++) {
  554. struct nouveau_bo *nvbo = (void *)(unsigned long)
  555. bo[push[i].bo_index].user_priv;
  556. struct drm_mm_node *mem = nvbo->bo.mem.mm_node;
  557. uint32_t cmd;
  558. cmd = chan->pushbuf_base + ((chan->dma.cur + 2) << 2);
  559. cmd |= 0x20000000;
  560. if (unlikely(cmd != req->suffix0)) {
  561. if (!nvbo->kmap.virtual) {
  562. ret = ttm_bo_kmap(&nvbo->bo, 0,
  563. nvbo->bo.mem.
  564. num_pages,
  565. &nvbo->kmap);
  566. if (ret) {
  567. WIND_RING(chan);
  568. goto out;
  569. }
  570. nvbo->validate_mapped = true;
  571. }
  572. nouveau_bo_wr32(nvbo, (push[i].offset +
  573. push[i].length - 8) / 4, cmd);
  574. }
  575. OUT_RING(chan, ((mem->start << PAGE_SHIFT) +
  576. push[i].offset) | 0x20000000);
  577. OUT_RING(chan, 0);
  578. for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
  579. OUT_RING(chan, 0);
  580. }
  581. }
  582. ret = nouveau_fence_new(chan, &fence, true);
  583. if (ret) {
  584. NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
  585. WIND_RING(chan);
  586. goto out;
  587. }
  588. out:
  589. validate_fini(&op, fence);
  590. nouveau_fence_unref(&fence);
  591. kfree(bo);
  592. kfree(push);
  593. out_next:
  594. if (chan->dma.ib_max) {
  595. req->suffix0 = 0x00000000;
  596. req->suffix1 = 0x00000000;
  597. } else
  598. if (dev_priv->chipset >= 0x25) {
  599. req->suffix0 = 0x00020000;
  600. req->suffix1 = 0x00000000;
  601. } else {
  602. req->suffix0 = 0x20000000 |
  603. (chan->pushbuf_base + ((chan->dma.cur + 2) << 2));
  604. req->suffix1 = 0x00000000;
  605. }
  606. nouveau_channel_put(&chan);
  607. return ret;
  608. }
  609. static inline uint32_t
  610. domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
  611. {
  612. uint32_t flags = 0;
  613. if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
  614. flags |= TTM_PL_FLAG_VRAM;
  615. if (domain & NOUVEAU_GEM_DOMAIN_GART)
  616. flags |= TTM_PL_FLAG_TT;
  617. return flags;
  618. }
  619. int
  620. nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
  621. struct drm_file *file_priv)
  622. {
  623. struct drm_nouveau_gem_cpu_prep *req = data;
  624. struct drm_gem_object *gem;
  625. struct nouveau_bo *nvbo;
  626. bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
  627. int ret = -EINVAL;
  628. gem = drm_gem_object_lookup(dev, file_priv, req->handle);
  629. if (!gem)
  630. return -ENOENT;
  631. nvbo = nouveau_gem_object(gem);
  632. spin_lock(&nvbo->bo.bdev->fence_lock);
  633. ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
  634. spin_unlock(&nvbo->bo.bdev->fence_lock);
  635. drm_gem_object_unreference_unlocked(gem);
  636. return ret;
  637. }
  638. int
  639. nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
  640. struct drm_file *file_priv)
  641. {
  642. return 0;
  643. }
  644. int
  645. nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
  646. struct drm_file *file_priv)
  647. {
  648. struct drm_nouveau_gem_info *req = data;
  649. struct drm_gem_object *gem;
  650. int ret;
  651. gem = drm_gem_object_lookup(dev, file_priv, req->handle);
  652. if (!gem)
  653. return -ENOENT;
  654. ret = nouveau_gem_info(gem, req);
  655. drm_gem_object_unreference_unlocked(gem);
  656. return ret;
  657. }