nouveau_gem.c 20 KB

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