nouveau_gem.c 24 KB

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