msm_gem_submit.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/sync_file.h>
  18. #include "msm_drv.h"
  19. #include "msm_gpu.h"
  20. #include "msm_gem.h"
  21. /*
  22. * Cmdstream submission:
  23. */
  24. /* make sure these don't conflict w/ MSM_SUBMIT_BO_x */
  25. #define BO_VALID 0x8000 /* is current addr in cmdstream correct/valid? */
  26. #define BO_LOCKED 0x4000
  27. #define BO_PINNED 0x2000
  28. static struct msm_gem_submit *submit_create(struct drm_device *dev,
  29. struct msm_gpu *gpu, uint32_t nr_bos, uint32_t nr_cmds)
  30. {
  31. struct msm_gem_submit *submit;
  32. uint64_t sz = sizeof(*submit) + ((u64)nr_bos * sizeof(submit->bos[0])) +
  33. ((u64)nr_cmds * sizeof(submit->cmd[0]));
  34. if (sz > SIZE_MAX)
  35. return NULL;
  36. submit = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  37. if (!submit)
  38. return NULL;
  39. submit->dev = dev;
  40. submit->gpu = gpu;
  41. submit->fence = NULL;
  42. submit->pid = get_pid(task_pid(current));
  43. submit->cmd = (void *)&submit->bos[nr_bos];
  44. /* initially, until copy_from_user() and bo lookup succeeds: */
  45. submit->nr_bos = 0;
  46. submit->nr_cmds = 0;
  47. INIT_LIST_HEAD(&submit->node);
  48. INIT_LIST_HEAD(&submit->bo_list);
  49. ww_acquire_init(&submit->ticket, &reservation_ww_class);
  50. return submit;
  51. }
  52. void msm_gem_submit_free(struct msm_gem_submit *submit)
  53. {
  54. dma_fence_put(submit->fence);
  55. list_del(&submit->node);
  56. put_pid(submit->pid);
  57. kfree(submit);
  58. }
  59. static inline unsigned long __must_check
  60. copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
  61. {
  62. if (access_ok(VERIFY_READ, from, n))
  63. return __copy_from_user_inatomic(to, from, n);
  64. return -EFAULT;
  65. }
  66. static int submit_lookup_objects(struct msm_gem_submit *submit,
  67. struct drm_msm_gem_submit *args, struct drm_file *file)
  68. {
  69. unsigned i;
  70. int ret = 0;
  71. spin_lock(&file->table_lock);
  72. pagefault_disable();
  73. for (i = 0; i < args->nr_bos; i++) {
  74. struct drm_msm_gem_submit_bo submit_bo;
  75. struct drm_gem_object *obj;
  76. struct msm_gem_object *msm_obj;
  77. void __user *userptr =
  78. u64_to_user_ptr(args->bos + (i * sizeof(submit_bo)));
  79. /* make sure we don't have garbage flags, in case we hit
  80. * error path before flags is initialized:
  81. */
  82. submit->bos[i].flags = 0;
  83. if (copy_from_user_inatomic(&submit_bo, userptr, sizeof(submit_bo))) {
  84. pagefault_enable();
  85. spin_unlock(&file->table_lock);
  86. if (copy_from_user(&submit_bo, userptr, sizeof(submit_bo))) {
  87. ret = -EFAULT;
  88. goto out;
  89. }
  90. spin_lock(&file->table_lock);
  91. pagefault_disable();
  92. }
  93. if ((submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) ||
  94. !(submit_bo.flags & MSM_SUBMIT_BO_FLAGS)) {
  95. DRM_ERROR("invalid flags: %x\n", submit_bo.flags);
  96. ret = -EINVAL;
  97. goto out_unlock;
  98. }
  99. submit->bos[i].flags = submit_bo.flags;
  100. /* in validate_objects() we figure out if this is true: */
  101. submit->bos[i].iova = submit_bo.presumed;
  102. /* normally use drm_gem_object_lookup(), but for bulk lookup
  103. * all under single table_lock just hit object_idr directly:
  104. */
  105. obj = idr_find(&file->object_idr, submit_bo.handle);
  106. if (!obj) {
  107. DRM_ERROR("invalid handle %u at index %u\n", submit_bo.handle, i);
  108. ret = -EINVAL;
  109. goto out_unlock;
  110. }
  111. msm_obj = to_msm_bo(obj);
  112. if (!list_empty(&msm_obj->submit_entry)) {
  113. DRM_ERROR("handle %u at index %u already on submit list\n",
  114. submit_bo.handle, i);
  115. ret = -EINVAL;
  116. goto out_unlock;
  117. }
  118. drm_gem_object_reference(obj);
  119. submit->bos[i].obj = msm_obj;
  120. list_add_tail(&msm_obj->submit_entry, &submit->bo_list);
  121. }
  122. out_unlock:
  123. pagefault_enable();
  124. spin_unlock(&file->table_lock);
  125. out:
  126. submit->nr_bos = i;
  127. return ret;
  128. }
  129. static void submit_unlock_unpin_bo(struct msm_gem_submit *submit, int i)
  130. {
  131. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  132. if (submit->bos[i].flags & BO_PINNED)
  133. msm_gem_put_iova(&msm_obj->base, submit->gpu->aspace);
  134. if (submit->bos[i].flags & BO_LOCKED)
  135. ww_mutex_unlock(&msm_obj->resv->lock);
  136. if (!(submit->bos[i].flags & BO_VALID))
  137. submit->bos[i].iova = 0;
  138. submit->bos[i].flags &= ~(BO_LOCKED | BO_PINNED);
  139. }
  140. /* This is where we make sure all the bo's are reserved and pin'd: */
  141. static int submit_lock_objects(struct msm_gem_submit *submit)
  142. {
  143. int contended, slow_locked = -1, i, ret = 0;
  144. retry:
  145. for (i = 0; i < submit->nr_bos; i++) {
  146. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  147. if (slow_locked == i)
  148. slow_locked = -1;
  149. contended = i;
  150. if (!(submit->bos[i].flags & BO_LOCKED)) {
  151. ret = ww_mutex_lock_interruptible(&msm_obj->resv->lock,
  152. &submit->ticket);
  153. if (ret)
  154. goto fail;
  155. submit->bos[i].flags |= BO_LOCKED;
  156. }
  157. }
  158. ww_acquire_done(&submit->ticket);
  159. return 0;
  160. fail:
  161. for (; i >= 0; i--)
  162. submit_unlock_unpin_bo(submit, i);
  163. if (slow_locked > 0)
  164. submit_unlock_unpin_bo(submit, slow_locked);
  165. if (ret == -EDEADLK) {
  166. struct msm_gem_object *msm_obj = submit->bos[contended].obj;
  167. /* we lost out in a seqno race, lock and retry.. */
  168. ret = ww_mutex_lock_slow_interruptible(&msm_obj->resv->lock,
  169. &submit->ticket);
  170. if (!ret) {
  171. submit->bos[contended].flags |= BO_LOCKED;
  172. slow_locked = contended;
  173. goto retry;
  174. }
  175. }
  176. return ret;
  177. }
  178. static int submit_fence_sync(struct msm_gem_submit *submit)
  179. {
  180. int i, ret = 0;
  181. for (i = 0; i < submit->nr_bos; i++) {
  182. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  183. bool write = submit->bos[i].flags & MSM_SUBMIT_BO_WRITE;
  184. ret = msm_gem_sync_object(&msm_obj->base, submit->gpu->fctx, write);
  185. if (ret)
  186. break;
  187. }
  188. return ret;
  189. }
  190. static int submit_pin_objects(struct msm_gem_submit *submit)
  191. {
  192. int i, ret = 0;
  193. submit->valid = true;
  194. for (i = 0; i < submit->nr_bos; i++) {
  195. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  196. uint64_t iova;
  197. /* if locking succeeded, pin bo: */
  198. ret = msm_gem_get_iova(&msm_obj->base,
  199. submit->gpu->aspace, &iova);
  200. if (ret)
  201. break;
  202. submit->bos[i].flags |= BO_PINNED;
  203. if (iova == submit->bos[i].iova) {
  204. submit->bos[i].flags |= BO_VALID;
  205. } else {
  206. submit->bos[i].iova = iova;
  207. /* iova changed, so address in cmdstream is not valid: */
  208. submit->bos[i].flags &= ~BO_VALID;
  209. submit->valid = false;
  210. }
  211. }
  212. return ret;
  213. }
  214. static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
  215. struct msm_gem_object **obj, uint64_t *iova, bool *valid)
  216. {
  217. if (idx >= submit->nr_bos) {
  218. DRM_ERROR("invalid buffer index: %u (out of %u)\n",
  219. idx, submit->nr_bos);
  220. return -EINVAL;
  221. }
  222. if (obj)
  223. *obj = submit->bos[idx].obj;
  224. if (iova)
  225. *iova = submit->bos[idx].iova;
  226. if (valid)
  227. *valid = !!(submit->bos[idx].flags & BO_VALID);
  228. return 0;
  229. }
  230. /* process the reloc's and patch up the cmdstream as needed: */
  231. static int submit_reloc(struct msm_gem_submit *submit, struct msm_gem_object *obj,
  232. uint32_t offset, uint32_t nr_relocs, uint64_t relocs)
  233. {
  234. uint32_t i, last_offset = 0;
  235. uint32_t *ptr;
  236. int ret = 0;
  237. if (offset % 4) {
  238. DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset);
  239. return -EINVAL;
  240. }
  241. /* For now, just map the entire thing. Eventually we probably
  242. * to do it page-by-page, w/ kmap() if not vmap()d..
  243. */
  244. ptr = msm_gem_get_vaddr(&obj->base);
  245. if (IS_ERR(ptr)) {
  246. ret = PTR_ERR(ptr);
  247. DBG("failed to map: %d", ret);
  248. return ret;
  249. }
  250. for (i = 0; i < nr_relocs; i++) {
  251. struct drm_msm_gem_submit_reloc submit_reloc;
  252. void __user *userptr =
  253. u64_to_user_ptr(relocs + (i * sizeof(submit_reloc)));
  254. uint32_t off;
  255. uint64_t iova;
  256. bool valid;
  257. if (copy_from_user(&submit_reloc, userptr, sizeof(submit_reloc))) {
  258. ret = -EFAULT;
  259. goto out;
  260. }
  261. if (submit_reloc.submit_offset % 4) {
  262. DRM_ERROR("non-aligned reloc offset: %u\n",
  263. submit_reloc.submit_offset);
  264. ret = -EINVAL;
  265. goto out;
  266. }
  267. /* offset in dwords: */
  268. off = submit_reloc.submit_offset / 4;
  269. if ((off >= (obj->base.size / 4)) ||
  270. (off < last_offset)) {
  271. DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
  272. ret = -EINVAL;
  273. goto out;
  274. }
  275. ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid);
  276. if (ret)
  277. goto out;
  278. if (valid)
  279. continue;
  280. iova += submit_reloc.reloc_offset;
  281. if (submit_reloc.shift < 0)
  282. iova >>= -submit_reloc.shift;
  283. else
  284. iova <<= submit_reloc.shift;
  285. ptr[off] = iova | submit_reloc.or;
  286. last_offset = off;
  287. }
  288. out:
  289. msm_gem_put_vaddr(&obj->base);
  290. return ret;
  291. }
  292. static void submit_cleanup(struct msm_gem_submit *submit)
  293. {
  294. unsigned i;
  295. for (i = 0; i < submit->nr_bos; i++) {
  296. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  297. submit_unlock_unpin_bo(submit, i);
  298. list_del_init(&msm_obj->submit_entry);
  299. drm_gem_object_unreference(&msm_obj->base);
  300. }
  301. ww_acquire_fini(&submit->ticket);
  302. }
  303. int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
  304. struct drm_file *file)
  305. {
  306. struct msm_drm_private *priv = dev->dev_private;
  307. struct drm_msm_gem_submit *args = data;
  308. struct msm_file_private *ctx = file->driver_priv;
  309. struct msm_gem_submit *submit;
  310. struct msm_gpu *gpu = priv->gpu;
  311. struct dma_fence *in_fence = NULL;
  312. struct sync_file *sync_file = NULL;
  313. int out_fence_fd = -1;
  314. unsigned i;
  315. int ret;
  316. if (!gpu)
  317. return -ENXIO;
  318. /* for now, we just have 3d pipe.. eventually this would need to
  319. * be more clever to dispatch to appropriate gpu module:
  320. */
  321. if (MSM_PIPE_ID(args->flags) != MSM_PIPE_3D0)
  322. return -EINVAL;
  323. if (MSM_PIPE_FLAGS(args->flags) & ~MSM_SUBMIT_FLAGS)
  324. return -EINVAL;
  325. if (args->flags & MSM_SUBMIT_FENCE_FD_IN) {
  326. in_fence = sync_file_get_fence(args->fence_fd);
  327. if (!in_fence)
  328. return -EINVAL;
  329. /*
  330. * Wait if the fence is from a foreign context, or if the fence
  331. * array contains any fence from a foreign context.
  332. */
  333. if (!dma_fence_match_context(in_fence, gpu->fctx->context)) {
  334. ret = dma_fence_wait(in_fence, true);
  335. if (ret)
  336. return ret;
  337. }
  338. }
  339. ret = mutex_lock_interruptible(&dev->struct_mutex);
  340. if (ret)
  341. return ret;
  342. if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
  343. out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
  344. if (out_fence_fd < 0) {
  345. ret = out_fence_fd;
  346. goto out_unlock;
  347. }
  348. }
  349. priv->struct_mutex_task = current;
  350. submit = submit_create(dev, gpu, args->nr_bos, args->nr_cmds);
  351. if (!submit) {
  352. ret = -ENOMEM;
  353. goto out_unlock;
  354. }
  355. ret = submit_lookup_objects(submit, args, file);
  356. if (ret)
  357. goto out;
  358. ret = submit_lock_objects(submit);
  359. if (ret)
  360. goto out;
  361. if (!(args->flags & MSM_SUBMIT_NO_IMPLICIT)) {
  362. ret = submit_fence_sync(submit);
  363. if (ret)
  364. goto out;
  365. }
  366. ret = submit_pin_objects(submit);
  367. if (ret)
  368. goto out;
  369. for (i = 0; i < args->nr_cmds; i++) {
  370. struct drm_msm_gem_submit_cmd submit_cmd;
  371. void __user *userptr =
  372. u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
  373. struct msm_gem_object *msm_obj;
  374. uint64_t iova;
  375. ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
  376. if (ret) {
  377. ret = -EFAULT;
  378. goto out;
  379. }
  380. /* validate input from userspace: */
  381. switch (submit_cmd.type) {
  382. case MSM_SUBMIT_CMD_BUF:
  383. case MSM_SUBMIT_CMD_IB_TARGET_BUF:
  384. case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
  385. break;
  386. default:
  387. DRM_ERROR("invalid type: %08x\n", submit_cmd.type);
  388. ret = -EINVAL;
  389. goto out;
  390. }
  391. ret = submit_bo(submit, submit_cmd.submit_idx,
  392. &msm_obj, &iova, NULL);
  393. if (ret)
  394. goto out;
  395. if (submit_cmd.size % 4) {
  396. DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
  397. submit_cmd.size);
  398. ret = -EINVAL;
  399. goto out;
  400. }
  401. if (!submit_cmd.size ||
  402. ((submit_cmd.size + submit_cmd.submit_offset) >
  403. msm_obj->base.size)) {
  404. DRM_ERROR("invalid cmdstream size: %u\n", submit_cmd.size);
  405. ret = -EINVAL;
  406. goto out;
  407. }
  408. submit->cmd[i].type = submit_cmd.type;
  409. submit->cmd[i].size = submit_cmd.size / 4;
  410. submit->cmd[i].iova = iova + submit_cmd.submit_offset;
  411. submit->cmd[i].idx = submit_cmd.submit_idx;
  412. if (submit->valid)
  413. continue;
  414. ret = submit_reloc(submit, msm_obj, submit_cmd.submit_offset,
  415. submit_cmd.nr_relocs, submit_cmd.relocs);
  416. if (ret)
  417. goto out;
  418. }
  419. submit->nr_cmds = i;
  420. submit->fence = msm_fence_alloc(gpu->fctx);
  421. if (IS_ERR(submit->fence)) {
  422. ret = PTR_ERR(submit->fence);
  423. submit->fence = NULL;
  424. goto out;
  425. }
  426. if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
  427. sync_file = sync_file_create(submit->fence);
  428. if (!sync_file) {
  429. ret = -ENOMEM;
  430. goto out;
  431. }
  432. }
  433. msm_gpu_submit(gpu, submit, ctx);
  434. args->fence = submit->fence->seqno;
  435. if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
  436. fd_install(out_fence_fd, sync_file->file);
  437. args->fence_fd = out_fence_fd;
  438. }
  439. out:
  440. if (in_fence)
  441. dma_fence_put(in_fence);
  442. submit_cleanup(submit);
  443. if (ret)
  444. msm_gem_submit_free(submit);
  445. out_unlock:
  446. if (ret && (out_fence_fd >= 0))
  447. put_unused_fd(out_fence_fd);
  448. priv->struct_mutex_task = NULL;
  449. mutex_unlock(&dev->struct_mutex);
  450. return ret;
  451. }