nouveau_gem.c 20 KB

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