msm_gem_submit.c 14 KB

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