nouveau_gem.c 20 KB

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