nouveau_gem.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. uint64_t flags;
  156. if (!valid_domains || (!read_domains && !write_domains))
  157. return -EINVAL;
  158. if (write_domains) {
  159. if ((valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
  160. (write_domains & NOUVEAU_GEM_DOMAIN_VRAM))
  161. flags = TTM_PL_FLAG_VRAM;
  162. else
  163. if ((valid_domains & NOUVEAU_GEM_DOMAIN_GART) &&
  164. (write_domains & NOUVEAU_GEM_DOMAIN_GART))
  165. flags = TTM_PL_FLAG_TT;
  166. else
  167. return -EINVAL;
  168. } else {
  169. if ((valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
  170. (read_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
  171. bo->mem.mem_type == TTM_PL_VRAM)
  172. flags = TTM_PL_FLAG_VRAM;
  173. else
  174. if ((valid_domains & NOUVEAU_GEM_DOMAIN_GART) &&
  175. (read_domains & NOUVEAU_GEM_DOMAIN_GART) &&
  176. bo->mem.mem_type == TTM_PL_TT)
  177. flags = TTM_PL_FLAG_TT;
  178. else
  179. if ((valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
  180. (read_domains & NOUVEAU_GEM_DOMAIN_VRAM))
  181. flags = TTM_PL_FLAG_VRAM;
  182. else
  183. flags = TTM_PL_FLAG_TT;
  184. }
  185. nouveau_bo_placement_set(nvbo, flags);
  186. return 0;
  187. }
  188. struct validate_op {
  189. struct list_head vram_list;
  190. struct list_head gart_list;
  191. struct list_head both_list;
  192. };
  193. static void
  194. validate_fini_list(struct list_head *list, struct nouveau_fence *fence)
  195. {
  196. struct list_head *entry, *tmp;
  197. struct nouveau_bo *nvbo;
  198. list_for_each_safe(entry, tmp, list) {
  199. nvbo = list_entry(entry, struct nouveau_bo, entry);
  200. if (likely(fence)) {
  201. struct nouveau_fence *prev_fence;
  202. spin_lock(&nvbo->bo.lock);
  203. prev_fence = nvbo->bo.sync_obj;
  204. nvbo->bo.sync_obj = nouveau_fence_ref(fence);
  205. spin_unlock(&nvbo->bo.lock);
  206. nouveau_fence_unref((void *)&prev_fence);
  207. }
  208. list_del(&nvbo->entry);
  209. nvbo->reserved_by = NULL;
  210. ttm_bo_unreserve(&nvbo->bo);
  211. drm_gem_object_unreference(nvbo->gem);
  212. }
  213. }
  214. static void
  215. validate_fini(struct validate_op *op, struct nouveau_fence* fence)
  216. {
  217. validate_fini_list(&op->vram_list, fence);
  218. validate_fini_list(&op->gart_list, fence);
  219. validate_fini_list(&op->both_list, fence);
  220. }
  221. static int
  222. validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
  223. struct drm_nouveau_gem_pushbuf_bo *pbbo,
  224. int nr_buffers, struct validate_op *op)
  225. {
  226. struct drm_device *dev = chan->dev;
  227. struct drm_nouveau_private *dev_priv = dev->dev_private;
  228. uint32_t sequence;
  229. int trycnt = 0;
  230. int ret, i;
  231. sequence = atomic_add_return(1, &dev_priv->ttm.validate_sequence);
  232. retry:
  233. if (++trycnt > 100000) {
  234. NV_ERROR(dev, "%s failed and gave up.\n", __func__);
  235. return -EINVAL;
  236. }
  237. for (i = 0; i < nr_buffers; i++) {
  238. struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
  239. struct drm_gem_object *gem;
  240. struct nouveau_bo *nvbo;
  241. gem = drm_gem_object_lookup(dev, file_priv, b->handle);
  242. if (!gem) {
  243. NV_ERROR(dev, "Unknown handle 0x%08x\n", b->handle);
  244. validate_fini(op, NULL);
  245. return -EINVAL;
  246. }
  247. nvbo = gem->driver_private;
  248. if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
  249. NV_ERROR(dev, "multiple instances of buffer %d on "
  250. "validation list\n", b->handle);
  251. validate_fini(op, NULL);
  252. return -EINVAL;
  253. }
  254. ret = ttm_bo_reserve(&nvbo->bo, false, false, true, sequence);
  255. if (ret) {
  256. validate_fini(op, NULL);
  257. if (ret == -EAGAIN)
  258. ret = ttm_bo_wait_unreserved(&nvbo->bo, false);
  259. drm_gem_object_unreference(gem);
  260. if (ret)
  261. return ret;
  262. goto retry;
  263. }
  264. nvbo->reserved_by = file_priv;
  265. nvbo->pbbo_index = i;
  266. if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
  267. (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
  268. list_add_tail(&nvbo->entry, &op->both_list);
  269. else
  270. if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
  271. list_add_tail(&nvbo->entry, &op->vram_list);
  272. else
  273. if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
  274. list_add_tail(&nvbo->entry, &op->gart_list);
  275. else {
  276. NV_ERROR(dev, "invalid valid domains: 0x%08x\n",
  277. b->valid_domains);
  278. list_add_tail(&nvbo->entry, &op->both_list);
  279. validate_fini(op, NULL);
  280. return -EINVAL;
  281. }
  282. if (unlikely(atomic_read(&nvbo->bo.cpu_writers) > 0)) {
  283. validate_fini(op, NULL);
  284. if (nvbo->cpu_filp == file_priv) {
  285. NV_ERROR(dev, "bo %p mapped by process trying "
  286. "to validate it!\n", nvbo);
  287. return -EINVAL;
  288. }
  289. ret = ttm_bo_wait_cpu(&nvbo->bo, false);
  290. if (ret)
  291. return ret;
  292. goto retry;
  293. }
  294. }
  295. return 0;
  296. }
  297. static int
  298. validate_list(struct nouveau_channel *chan, struct list_head *list,
  299. struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr)
  300. {
  301. struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
  302. (void __force __user *)(uintptr_t)user_pbbo_ptr;
  303. struct nouveau_bo *nvbo;
  304. int ret, relocs = 0;
  305. list_for_each_entry(nvbo, list, entry) {
  306. struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
  307. struct nouveau_fence *prev_fence = nvbo->bo.sync_obj;
  308. if (prev_fence && nouveau_fence_channel(prev_fence) != chan) {
  309. spin_lock(&nvbo->bo.lock);
  310. ret = ttm_bo_wait(&nvbo->bo, false, false, false);
  311. spin_unlock(&nvbo->bo.lock);
  312. if (unlikely(ret))
  313. return ret;
  314. }
  315. ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
  316. b->write_domains,
  317. b->valid_domains);
  318. if (unlikely(ret))
  319. return ret;
  320. nvbo->channel = chan;
  321. ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement,
  322. false, false);
  323. nvbo->channel = NULL;
  324. if (unlikely(ret))
  325. return ret;
  326. if (nvbo->bo.offset == b->presumed_offset &&
  327. ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
  328. b->presumed_domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
  329. (nvbo->bo.mem.mem_type == TTM_PL_TT &&
  330. b->presumed_domain & NOUVEAU_GEM_DOMAIN_GART)))
  331. continue;
  332. if (nvbo->bo.mem.mem_type == TTM_PL_TT)
  333. b->presumed_domain = NOUVEAU_GEM_DOMAIN_GART;
  334. else
  335. b->presumed_domain = NOUVEAU_GEM_DOMAIN_VRAM;
  336. b->presumed_offset = nvbo->bo.offset;
  337. b->presumed_ok = 0;
  338. relocs++;
  339. if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index], b, sizeof(*b)))
  340. return -EFAULT;
  341. }
  342. return relocs;
  343. }
  344. static int
  345. nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
  346. struct drm_file *file_priv,
  347. struct drm_nouveau_gem_pushbuf_bo *pbbo,
  348. uint64_t user_buffers, int nr_buffers,
  349. struct validate_op *op, int *apply_relocs)
  350. {
  351. int ret, relocs = 0;
  352. INIT_LIST_HEAD(&op->vram_list);
  353. INIT_LIST_HEAD(&op->gart_list);
  354. INIT_LIST_HEAD(&op->both_list);
  355. if (nr_buffers == 0)
  356. return 0;
  357. ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
  358. if (unlikely(ret))
  359. return ret;
  360. ret = validate_list(chan, &op->vram_list, pbbo, user_buffers);
  361. if (unlikely(ret < 0)) {
  362. validate_fini(op, NULL);
  363. return ret;
  364. }
  365. relocs += ret;
  366. ret = validate_list(chan, &op->gart_list, pbbo, user_buffers);
  367. if (unlikely(ret < 0)) {
  368. validate_fini(op, NULL);
  369. return ret;
  370. }
  371. relocs += ret;
  372. ret = validate_list(chan, &op->both_list, pbbo, user_buffers);
  373. if (unlikely(ret < 0)) {
  374. validate_fini(op, NULL);
  375. return ret;
  376. }
  377. relocs += ret;
  378. *apply_relocs = relocs;
  379. return 0;
  380. }
  381. static inline void *
  382. u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
  383. {
  384. void *mem;
  385. void __user *userptr = (void __force __user *)(uintptr_t)user;
  386. mem = kmalloc(nmemb * size, GFP_KERNEL);
  387. if (!mem)
  388. return ERR_PTR(-ENOMEM);
  389. if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
  390. kfree(mem);
  391. return ERR_PTR(-EFAULT);
  392. }
  393. return mem;
  394. }
  395. static int
  396. nouveau_gem_pushbuf_reloc_apply(struct nouveau_channel *chan, int nr_bo,
  397. struct drm_nouveau_gem_pushbuf_bo *bo,
  398. unsigned nr_relocs, uint64_t ptr_relocs,
  399. unsigned nr_dwords, unsigned first_dword,
  400. uint32_t *pushbuf, bool is_iomem)
  401. {
  402. struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
  403. struct drm_device *dev = chan->dev;
  404. int ret = 0;
  405. unsigned i;
  406. reloc = u_memcpya(ptr_relocs, nr_relocs, sizeof(*reloc));
  407. if (IS_ERR(reloc))
  408. return PTR_ERR(reloc);
  409. for (i = 0; i < nr_relocs; i++) {
  410. struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
  411. struct drm_nouveau_gem_pushbuf_bo *b;
  412. uint32_t data;
  413. if (r->bo_index >= nr_bo || r->reloc_index < first_dword ||
  414. r->reloc_index >= first_dword + nr_dwords) {
  415. NV_ERROR(dev, "Bad relocation %d\n", i);
  416. NV_ERROR(dev, " bo: %d max %d\n", r->bo_index, nr_bo);
  417. NV_ERROR(dev, " id: %d max %d\n", r->reloc_index, nr_dwords);
  418. ret = -EINVAL;
  419. break;
  420. }
  421. b = &bo[r->bo_index];
  422. if (b->presumed_ok)
  423. continue;
  424. if (r->flags & NOUVEAU_GEM_RELOC_LOW)
  425. data = b->presumed_offset + r->data;
  426. else
  427. if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
  428. data = (b->presumed_offset + r->data) >> 32;
  429. else
  430. data = r->data;
  431. if (r->flags & NOUVEAU_GEM_RELOC_OR) {
  432. if (b->presumed_domain == NOUVEAU_GEM_DOMAIN_GART)
  433. data |= r->tor;
  434. else
  435. data |= r->vor;
  436. }
  437. if (is_iomem)
  438. iowrite32_native(data, (void __force __iomem *)
  439. &pushbuf[r->reloc_index]);
  440. else
  441. pushbuf[r->reloc_index] = 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_gem_pushbuf *req = data;
  451. struct drm_nouveau_gem_pushbuf_bo *bo = NULL;
  452. struct nouveau_channel *chan;
  453. struct validate_op op;
  454. struct nouveau_fence* fence = 0;
  455. uint32_t *pushbuf = NULL;
  456. int ret = 0, do_reloc = 0, i;
  457. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  458. NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req->channel, file_priv, chan);
  459. if (req->nr_dwords >= chan->dma.max ||
  460. req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS ||
  461. req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS) {
  462. NV_ERROR(dev, "Pushbuf config exceeds limits:\n");
  463. NV_ERROR(dev, " dwords : %d max %d\n", req->nr_dwords,
  464. chan->dma.max - 1);
  465. NV_ERROR(dev, " buffers: %d max %d\n", req->nr_buffers,
  466. NOUVEAU_GEM_MAX_BUFFERS);
  467. NV_ERROR(dev, " relocs : %d max %d\n", req->nr_relocs,
  468. NOUVEAU_GEM_MAX_RELOCS);
  469. return -EINVAL;
  470. }
  471. pushbuf = u_memcpya(req->dwords, req->nr_dwords, sizeof(uint32_t));
  472. if (IS_ERR(pushbuf))
  473. return PTR_ERR(pushbuf);
  474. bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
  475. if (IS_ERR(bo)) {
  476. kfree(pushbuf);
  477. return PTR_ERR(bo);
  478. }
  479. mutex_lock(&dev->struct_mutex);
  480. /* Validate buffer list */
  481. ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
  482. req->nr_buffers, &op, &do_reloc);
  483. if (ret)
  484. goto out;
  485. /* Apply any relocations that are required */
  486. if (do_reloc) {
  487. ret = nouveau_gem_pushbuf_reloc_apply(chan, req->nr_buffers,
  488. bo, req->nr_relocs,
  489. req->relocs,
  490. req->nr_dwords, 0,
  491. pushbuf, false);
  492. if (ret)
  493. goto out;
  494. }
  495. /* Emit push buffer to the hw
  496. */
  497. ret = RING_SPACE(chan, req->nr_dwords);
  498. if (ret)
  499. goto out;
  500. OUT_RINGp(chan, pushbuf, req->nr_dwords);
  501. ret = nouveau_fence_new(chan, &fence, true);
  502. if (ret) {
  503. NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
  504. WIND_RING(chan);
  505. goto out;
  506. }
  507. if (nouveau_gem_pushbuf_sync(chan)) {
  508. ret = nouveau_fence_wait(fence, NULL, false, false);
  509. if (ret) {
  510. for (i = 0; i < req->nr_dwords; i++)
  511. NV_ERROR(dev, "0x%08x\n", pushbuf[i]);
  512. NV_ERROR(dev, "^^ above push buffer is fail :(\n");
  513. }
  514. }
  515. out:
  516. validate_fini(&op, fence);
  517. nouveau_fence_unref((void**)&fence);
  518. mutex_unlock(&dev->struct_mutex);
  519. kfree(pushbuf);
  520. kfree(bo);
  521. return ret;
  522. }
  523. #define PUSHBUF_CAL (dev_priv->card_type >= NV_20)
  524. int
  525. nouveau_gem_ioctl_pushbuf_call(struct drm_device *dev, void *data,
  526. struct drm_file *file_priv)
  527. {
  528. struct drm_nouveau_private *dev_priv = dev->dev_private;
  529. struct drm_nouveau_gem_pushbuf_call *req = data;
  530. struct drm_nouveau_gem_pushbuf_bo *bo = NULL;
  531. struct nouveau_channel *chan;
  532. struct drm_gem_object *gem;
  533. struct nouveau_bo *pbbo;
  534. struct validate_op op;
  535. struct nouveau_fence* fence = 0;
  536. int i, ret = 0, do_reloc = 0;
  537. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  538. NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req->channel, file_priv, chan);
  539. if (unlikely(req->handle == 0))
  540. goto out_next;
  541. if (req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS ||
  542. req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS) {
  543. NV_ERROR(dev, "Pushbuf config exceeds limits:\n");
  544. NV_ERROR(dev, " buffers: %d max %d\n", req->nr_buffers,
  545. NOUVEAU_GEM_MAX_BUFFERS);
  546. NV_ERROR(dev, " relocs : %d max %d\n", req->nr_relocs,
  547. NOUVEAU_GEM_MAX_RELOCS);
  548. return -EINVAL;
  549. }
  550. bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
  551. if (IS_ERR(bo))
  552. return PTR_ERR(bo);
  553. mutex_lock(&dev->struct_mutex);
  554. /* Validate buffer list */
  555. ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
  556. req->nr_buffers, &op, &do_reloc);
  557. if (ret) {
  558. NV_ERROR(dev, "validate: %d\n", ret);
  559. goto out;
  560. }
  561. /* Validate DMA push buffer */
  562. gem = drm_gem_object_lookup(dev, file_priv, req->handle);
  563. if (!gem) {
  564. NV_ERROR(dev, "Unknown pb handle 0x%08x\n", req->handle);
  565. ret = -EINVAL;
  566. goto out;
  567. }
  568. pbbo = nouveau_gem_object(gem);
  569. if ((req->offset & 3) || req->nr_dwords < 2 ||
  570. (unsigned long)req->offset > (unsigned long)pbbo->bo.mem.size ||
  571. (unsigned long)req->nr_dwords >
  572. ((unsigned long)(pbbo->bo.mem.size - req->offset ) >> 2)) {
  573. NV_ERROR(dev, "pb call misaligned or out of bounds: "
  574. "%d + %d * 4 > %ld\n",
  575. req->offset, req->nr_dwords, pbbo->bo.mem.size);
  576. ret = -EINVAL;
  577. drm_gem_object_unreference(gem);
  578. goto out;
  579. }
  580. ret = ttm_bo_reserve(&pbbo->bo, false, false, true,
  581. chan->fence.sequence);
  582. if (ret) {
  583. NV_ERROR(dev, "resv pb: %d\n", ret);
  584. drm_gem_object_unreference(gem);
  585. goto out;
  586. }
  587. nouveau_bo_placement_set(pbbo, 1 << chan->pushbuf_bo->bo.mem.mem_type);
  588. ret = ttm_bo_validate(&pbbo->bo, &pbbo->placement, false, false);
  589. if (ret) {
  590. NV_ERROR(dev, "validate pb: %d\n", ret);
  591. ttm_bo_unreserve(&pbbo->bo);
  592. drm_gem_object_unreference(gem);
  593. goto out;
  594. }
  595. list_add_tail(&pbbo->entry, &op.both_list);
  596. /* If presumed return address doesn't match, we need to map the
  597. * push buffer and fix it..
  598. */
  599. if (!PUSHBUF_CAL) {
  600. uint32_t retaddy;
  601. if (chan->dma.free < 4 + NOUVEAU_DMA_SKIPS) {
  602. ret = nouveau_dma_wait(chan, 4 + NOUVEAU_DMA_SKIPS);
  603. if (ret) {
  604. NV_ERROR(dev, "jmp_space: %d\n", ret);
  605. goto out;
  606. }
  607. }
  608. retaddy = chan->pushbuf_base + ((chan->dma.cur + 2) << 2);
  609. retaddy |= 0x20000000;
  610. if (retaddy != req->suffix0) {
  611. req->suffix0 = retaddy;
  612. do_reloc = 1;
  613. }
  614. }
  615. /* Apply any relocations that are required */
  616. if (do_reloc) {
  617. void *pbvirt;
  618. bool is_iomem;
  619. ret = ttm_bo_kmap(&pbbo->bo, 0, pbbo->bo.mem.num_pages,
  620. &pbbo->kmap);
  621. if (ret) {
  622. NV_ERROR(dev, "kmap pb: %d\n", ret);
  623. goto out;
  624. }
  625. pbvirt = ttm_kmap_obj_virtual(&pbbo->kmap, &is_iomem);
  626. ret = nouveau_gem_pushbuf_reloc_apply(chan, req->nr_buffers, bo,
  627. req->nr_relocs,
  628. req->relocs,
  629. req->nr_dwords,
  630. req->offset / 4,
  631. pbvirt, is_iomem);
  632. if (!PUSHBUF_CAL) {
  633. nouveau_bo_wr32(pbbo,
  634. req->offset / 4 + req->nr_dwords - 2,
  635. req->suffix0);
  636. }
  637. ttm_bo_kunmap(&pbbo->kmap);
  638. if (ret) {
  639. NV_ERROR(dev, "reloc apply: %d\n", ret);
  640. goto out;
  641. }
  642. }
  643. if (PUSHBUF_CAL) {
  644. ret = RING_SPACE(chan, 2);
  645. if (ret) {
  646. NV_ERROR(dev, "cal_space: %d\n", ret);
  647. goto out;
  648. }
  649. OUT_RING(chan, ((pbbo->bo.mem.mm_node->start << PAGE_SHIFT) +
  650. req->offset) | 2);
  651. OUT_RING(chan, 0);
  652. } else {
  653. ret = RING_SPACE(chan, 2 + NOUVEAU_DMA_SKIPS);
  654. if (ret) {
  655. NV_ERROR(dev, "jmp_space: %d\n", ret);
  656. goto out;
  657. }
  658. OUT_RING(chan, ((pbbo->bo.mem.mm_node->start << PAGE_SHIFT) +
  659. req->offset) | 0x20000000);
  660. OUT_RING(chan, 0);
  661. /* Space the jumps apart with NOPs. */
  662. for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
  663. OUT_RING(chan, 0);
  664. }
  665. ret = nouveau_fence_new(chan, &fence, true);
  666. if (ret) {
  667. NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
  668. WIND_RING(chan);
  669. goto out;
  670. }
  671. out:
  672. validate_fini(&op, fence);
  673. nouveau_fence_unref((void**)&fence);
  674. mutex_unlock(&dev->struct_mutex);
  675. kfree(bo);
  676. out_next:
  677. if (PUSHBUF_CAL) {
  678. req->suffix0 = 0x00020000;
  679. req->suffix1 = 0x00000000;
  680. } else {
  681. req->suffix0 = 0x20000000 |
  682. (chan->pushbuf_base + ((chan->dma.cur + 2) << 2));
  683. req->suffix1 = 0x00000000;
  684. }
  685. return ret;
  686. }
  687. int
  688. nouveau_gem_ioctl_pushbuf_call2(struct drm_device *dev, void *data,
  689. struct drm_file *file_priv)
  690. {
  691. struct drm_nouveau_private *dev_priv = dev->dev_private;
  692. struct drm_nouveau_gem_pushbuf_call *req = data;
  693. req->vram_available = dev_priv->fb_aper_free;
  694. req->gart_available = dev_priv->gart_info.aper_free;
  695. return nouveau_gem_ioctl_pushbuf_call(dev, data, file_priv);
  696. }
  697. static inline uint32_t
  698. domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
  699. {
  700. uint32_t flags = 0;
  701. if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
  702. flags |= TTM_PL_FLAG_VRAM;
  703. if (domain & NOUVEAU_GEM_DOMAIN_GART)
  704. flags |= TTM_PL_FLAG_TT;
  705. return flags;
  706. }
  707. int
  708. nouveau_gem_ioctl_pin(struct drm_device *dev, void *data,
  709. struct drm_file *file_priv)
  710. {
  711. struct drm_nouveau_gem_pin *req = data;
  712. struct drm_gem_object *gem;
  713. struct nouveau_bo *nvbo;
  714. int ret = 0;
  715. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  716. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  717. NV_ERROR(dev, "pin only allowed without kernel modesetting\n");
  718. return -EINVAL;
  719. }
  720. if (!DRM_SUSER(DRM_CURPROC))
  721. return -EPERM;
  722. gem = drm_gem_object_lookup(dev, file_priv, req->handle);
  723. if (!gem)
  724. return -EINVAL;
  725. nvbo = nouveau_gem_object(gem);
  726. ret = nouveau_bo_pin(nvbo, domain_to_ttm(nvbo, req->domain));
  727. if (ret)
  728. goto out;
  729. req->offset = nvbo->bo.offset;
  730. if (nvbo->bo.mem.mem_type == TTM_PL_TT)
  731. req->domain = NOUVEAU_GEM_DOMAIN_GART;
  732. else
  733. req->domain = NOUVEAU_GEM_DOMAIN_VRAM;
  734. out:
  735. drm_gem_object_unreference_unlocked(gem);
  736. return ret;
  737. }
  738. int
  739. nouveau_gem_ioctl_unpin(struct drm_device *dev, void *data,
  740. struct drm_file *file_priv)
  741. {
  742. struct drm_nouveau_gem_pin *req = data;
  743. struct drm_gem_object *gem;
  744. int ret;
  745. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  746. if (drm_core_check_feature(dev, DRIVER_MODESET))
  747. return -EINVAL;
  748. gem = drm_gem_object_lookup(dev, file_priv, req->handle);
  749. if (!gem)
  750. return -EINVAL;
  751. ret = nouveau_bo_unpin(nouveau_gem_object(gem));
  752. drm_gem_object_unreference_unlocked(gem);
  753. return ret;
  754. }
  755. int
  756. nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
  757. struct drm_file *file_priv)
  758. {
  759. struct drm_nouveau_gem_cpu_prep *req = data;
  760. struct drm_gem_object *gem;
  761. struct nouveau_bo *nvbo;
  762. bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
  763. int ret = -EINVAL;
  764. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  765. gem = drm_gem_object_lookup(dev, file_priv, req->handle);
  766. if (!gem)
  767. return ret;
  768. nvbo = nouveau_gem_object(gem);
  769. if (nvbo->cpu_filp) {
  770. if (nvbo->cpu_filp == file_priv)
  771. goto out;
  772. ret = ttm_bo_wait_cpu(&nvbo->bo, no_wait);
  773. if (ret)
  774. goto out;
  775. }
  776. if (req->flags & NOUVEAU_GEM_CPU_PREP_NOBLOCK) {
  777. spin_lock(&nvbo->bo.lock);
  778. ret = ttm_bo_wait(&nvbo->bo, false, false, no_wait);
  779. spin_unlock(&nvbo->bo.lock);
  780. } else {
  781. ret = ttm_bo_synccpu_write_grab(&nvbo->bo, no_wait);
  782. if (ret == 0)
  783. nvbo->cpu_filp = file_priv;
  784. }
  785. out:
  786. drm_gem_object_unreference_unlocked(gem);
  787. return ret;
  788. }
  789. int
  790. nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
  791. struct drm_file *file_priv)
  792. {
  793. struct drm_nouveau_gem_cpu_prep *req = data;
  794. struct drm_gem_object *gem;
  795. struct nouveau_bo *nvbo;
  796. int ret = -EINVAL;
  797. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  798. gem = drm_gem_object_lookup(dev, file_priv, req->handle);
  799. if (!gem)
  800. return ret;
  801. nvbo = nouveau_gem_object(gem);
  802. if (nvbo->cpu_filp != file_priv)
  803. goto out;
  804. nvbo->cpu_filp = NULL;
  805. ttm_bo_synccpu_write_release(&nvbo->bo);
  806. ret = 0;
  807. out:
  808. drm_gem_object_unreference_unlocked(gem);
  809. return ret;
  810. }
  811. int
  812. nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
  813. struct drm_file *file_priv)
  814. {
  815. struct drm_nouveau_gem_info *req = data;
  816. struct drm_gem_object *gem;
  817. int ret;
  818. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  819. gem = drm_gem_object_lookup(dev, file_priv, req->handle);
  820. if (!gem)
  821. return -EINVAL;
  822. ret = nouveau_gem_info(gem, req);
  823. drm_gem_object_unreference_unlocked(gem);
  824. return ret;
  825. }