nouveau_gem.c 22 KB

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