nouveau_gem.c 21 KB

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