nouveau_gem.c 24 KB

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