msm_gem_submit.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 "msm_drv.h"
  18. #include "msm_gpu.h"
  19. #include "msm_gem.h"
  20. /*
  21. * Cmdstream submission:
  22. */
  23. /* make sure these don't conflict w/ MSM_SUBMIT_BO_x */
  24. #define BO_VALID 0x8000 /* is current addr in cmdstream correct/valid? */
  25. #define BO_LOCKED 0x4000
  26. #define BO_PINNED 0x2000
  27. static struct msm_gem_submit *submit_create(struct drm_device *dev,
  28. struct msm_gpu *gpu, int nr)
  29. {
  30. struct msm_gem_submit *submit;
  31. int sz = sizeof(*submit) + (nr * sizeof(submit->bos[0]));
  32. submit = kmalloc(sz, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  33. if (!submit)
  34. return NULL;
  35. submit->dev = dev;
  36. submit->gpu = gpu;
  37. submit->pid = get_pid(task_pid(current));
  38. /* initially, until copy_from_user() and bo lookup succeeds: */
  39. submit->nr_bos = 0;
  40. submit->nr_cmds = 0;
  41. INIT_LIST_HEAD(&submit->bo_list);
  42. ww_acquire_init(&submit->ticket, &reservation_ww_class);
  43. return submit;
  44. }
  45. void msm_gem_submit_free(struct msm_gem_submit *submit)
  46. {
  47. fence_put(submit->fence);
  48. list_del(&submit->node);
  49. put_pid(submit->pid);
  50. kfree(submit);
  51. }
  52. static int submit_lookup_objects(struct msm_gem_submit *submit,
  53. struct drm_msm_gem_submit *args, struct drm_file *file)
  54. {
  55. unsigned i;
  56. int ret = 0;
  57. spin_lock(&file->table_lock);
  58. for (i = 0; i < args->nr_bos; i++) {
  59. struct drm_msm_gem_submit_bo submit_bo;
  60. struct drm_gem_object *obj;
  61. struct msm_gem_object *msm_obj;
  62. void __user *userptr =
  63. u64_to_user_ptr(args->bos + (i * sizeof(submit_bo)));
  64. ret = copy_from_user(&submit_bo, userptr, sizeof(submit_bo));
  65. if (ret) {
  66. ret = -EFAULT;
  67. goto out_unlock;
  68. }
  69. if (submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) {
  70. DRM_ERROR("invalid flags: %x\n", submit_bo.flags);
  71. ret = -EINVAL;
  72. goto out_unlock;
  73. }
  74. submit->bos[i].flags = submit_bo.flags;
  75. /* in validate_objects() we figure out if this is true: */
  76. submit->bos[i].iova = submit_bo.presumed;
  77. /* normally use drm_gem_object_lookup(), but for bulk lookup
  78. * all under single table_lock just hit object_idr directly:
  79. */
  80. obj = idr_find(&file->object_idr, submit_bo.handle);
  81. if (!obj) {
  82. DRM_ERROR("invalid handle %u at index %u\n", submit_bo.handle, i);
  83. ret = -EINVAL;
  84. goto out_unlock;
  85. }
  86. msm_obj = to_msm_bo(obj);
  87. if (!list_empty(&msm_obj->submit_entry)) {
  88. DRM_ERROR("handle %u at index %u already on submit list\n",
  89. submit_bo.handle, i);
  90. ret = -EINVAL;
  91. goto out_unlock;
  92. }
  93. drm_gem_object_reference(obj);
  94. submit->bos[i].obj = msm_obj;
  95. list_add_tail(&msm_obj->submit_entry, &submit->bo_list);
  96. }
  97. out_unlock:
  98. submit->nr_bos = i;
  99. spin_unlock(&file->table_lock);
  100. return ret;
  101. }
  102. static void submit_unlock_unpin_bo(struct msm_gem_submit *submit, int i)
  103. {
  104. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  105. if (submit->bos[i].flags & BO_PINNED)
  106. msm_gem_put_iova(&msm_obj->base, submit->gpu->id);
  107. if (submit->bos[i].flags & BO_LOCKED)
  108. ww_mutex_unlock(&msm_obj->resv->lock);
  109. if (!(submit->bos[i].flags & BO_VALID))
  110. submit->bos[i].iova = 0;
  111. submit->bos[i].flags &= ~(BO_LOCKED | BO_PINNED);
  112. }
  113. /* This is where we make sure all the bo's are reserved and pin'd: */
  114. static int submit_lock_objects(struct msm_gem_submit *submit)
  115. {
  116. int contended, slow_locked = -1, i, ret = 0;
  117. retry:
  118. for (i = 0; i < submit->nr_bos; i++) {
  119. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  120. if (slow_locked == i)
  121. slow_locked = -1;
  122. contended = i;
  123. if (!(submit->bos[i].flags & BO_LOCKED)) {
  124. ret = ww_mutex_lock_interruptible(&msm_obj->resv->lock,
  125. &submit->ticket);
  126. if (ret)
  127. goto fail;
  128. submit->bos[i].flags |= BO_LOCKED;
  129. }
  130. }
  131. ww_acquire_done(&submit->ticket);
  132. return 0;
  133. fail:
  134. for (; i >= 0; i--)
  135. submit_unlock_unpin_bo(submit, i);
  136. if (slow_locked > 0)
  137. submit_unlock_unpin_bo(submit, slow_locked);
  138. if (ret == -EDEADLK) {
  139. struct msm_gem_object *msm_obj = submit->bos[contended].obj;
  140. /* we lost out in a seqno race, lock and retry.. */
  141. ret = ww_mutex_lock_slow_interruptible(&msm_obj->resv->lock,
  142. &submit->ticket);
  143. if (!ret) {
  144. submit->bos[contended].flags |= BO_LOCKED;
  145. slow_locked = contended;
  146. goto retry;
  147. }
  148. }
  149. return ret;
  150. }
  151. static int submit_fence_sync(struct msm_gem_submit *submit)
  152. {
  153. int i, ret = 0;
  154. for (i = 0; i < submit->nr_bos; i++) {
  155. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  156. bool write = submit->bos[i].flags & MSM_SUBMIT_BO_WRITE;
  157. ret = msm_gem_sync_object(&msm_obj->base, submit->gpu->fctx, write);
  158. if (ret)
  159. break;
  160. }
  161. return ret;
  162. }
  163. static int submit_pin_objects(struct msm_gem_submit *submit)
  164. {
  165. int i, ret = 0;
  166. submit->valid = true;
  167. for (i = 0; i < submit->nr_bos; i++) {
  168. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  169. uint32_t iova;
  170. /* if locking succeeded, pin bo: */
  171. ret = msm_gem_get_iova_locked(&msm_obj->base,
  172. submit->gpu->id, &iova);
  173. if (ret)
  174. break;
  175. submit->bos[i].flags |= BO_PINNED;
  176. if (iova == submit->bos[i].iova) {
  177. submit->bos[i].flags |= BO_VALID;
  178. } else {
  179. submit->bos[i].iova = iova;
  180. /* iova changed, so address in cmdstream is not valid: */
  181. submit->bos[i].flags &= ~BO_VALID;
  182. submit->valid = false;
  183. }
  184. }
  185. return ret;
  186. }
  187. static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
  188. struct msm_gem_object **obj, uint32_t *iova, bool *valid)
  189. {
  190. if (idx >= submit->nr_bos) {
  191. DRM_ERROR("invalid buffer index: %u (out of %u)\n",
  192. idx, submit->nr_bos);
  193. return -EINVAL;
  194. }
  195. if (obj)
  196. *obj = submit->bos[idx].obj;
  197. if (iova)
  198. *iova = submit->bos[idx].iova;
  199. if (valid)
  200. *valid = !!(submit->bos[idx].flags & BO_VALID);
  201. return 0;
  202. }
  203. /* process the reloc's and patch up the cmdstream as needed: */
  204. static int submit_reloc(struct msm_gem_submit *submit, struct msm_gem_object *obj,
  205. uint32_t offset, uint32_t nr_relocs, uint64_t relocs)
  206. {
  207. uint32_t i, last_offset = 0;
  208. uint32_t *ptr;
  209. int ret;
  210. if (offset % 4) {
  211. DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset);
  212. return -EINVAL;
  213. }
  214. /* For now, just map the entire thing. Eventually we probably
  215. * to do it page-by-page, w/ kmap() if not vmap()d..
  216. */
  217. ptr = msm_gem_vaddr_locked(&obj->base);
  218. if (IS_ERR(ptr)) {
  219. ret = PTR_ERR(ptr);
  220. DBG("failed to map: %d", ret);
  221. return ret;
  222. }
  223. for (i = 0; i < nr_relocs; i++) {
  224. struct drm_msm_gem_submit_reloc submit_reloc;
  225. void __user *userptr =
  226. u64_to_user_ptr(relocs + (i * sizeof(submit_reloc)));
  227. uint32_t iova, off;
  228. bool valid;
  229. ret = copy_from_user(&submit_reloc, userptr, sizeof(submit_reloc));
  230. if (ret)
  231. return -EFAULT;
  232. if (submit_reloc.submit_offset % 4) {
  233. DRM_ERROR("non-aligned reloc offset: %u\n",
  234. submit_reloc.submit_offset);
  235. return -EINVAL;
  236. }
  237. /* offset in dwords: */
  238. off = submit_reloc.submit_offset / 4;
  239. if ((off >= (obj->base.size / 4)) ||
  240. (off < last_offset)) {
  241. DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
  242. return -EINVAL;
  243. }
  244. ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid);
  245. if (ret)
  246. return ret;
  247. if (valid)
  248. continue;
  249. iova += submit_reloc.reloc_offset;
  250. if (submit_reloc.shift < 0)
  251. iova >>= -submit_reloc.shift;
  252. else
  253. iova <<= submit_reloc.shift;
  254. ptr[off] = iova | submit_reloc.or;
  255. last_offset = off;
  256. }
  257. return 0;
  258. }
  259. static void submit_cleanup(struct msm_gem_submit *submit)
  260. {
  261. unsigned i;
  262. for (i = 0; i < submit->nr_bos; i++) {
  263. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  264. submit_unlock_unpin_bo(submit, i);
  265. list_del_init(&msm_obj->submit_entry);
  266. drm_gem_object_unreference(&msm_obj->base);
  267. }
  268. ww_acquire_fini(&submit->ticket);
  269. }
  270. int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
  271. struct drm_file *file)
  272. {
  273. struct msm_drm_private *priv = dev->dev_private;
  274. struct drm_msm_gem_submit *args = data;
  275. struct msm_file_private *ctx = file->driver_priv;
  276. struct msm_gem_submit *submit;
  277. struct msm_gpu *gpu = priv->gpu;
  278. unsigned i;
  279. int ret;
  280. if (!gpu)
  281. return -ENXIO;
  282. /* for now, we just have 3d pipe.. eventually this would need to
  283. * be more clever to dispatch to appropriate gpu module:
  284. */
  285. if (args->pipe != MSM_PIPE_3D0)
  286. return -EINVAL;
  287. if (args->nr_cmds > MAX_CMDS)
  288. return -EINVAL;
  289. submit = submit_create(dev, gpu, args->nr_bos);
  290. if (!submit)
  291. return -ENOMEM;
  292. mutex_lock(&dev->struct_mutex);
  293. ret = submit_lookup_objects(submit, args, file);
  294. if (ret)
  295. goto out;
  296. ret = submit_lock_objects(submit);
  297. if (ret)
  298. goto out;
  299. ret = submit_fence_sync(submit);
  300. if (ret)
  301. goto out;
  302. ret = submit_pin_objects(submit);
  303. if (ret)
  304. goto out;
  305. for (i = 0; i < args->nr_cmds; i++) {
  306. struct drm_msm_gem_submit_cmd submit_cmd;
  307. void __user *userptr =
  308. u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
  309. struct msm_gem_object *msm_obj;
  310. uint32_t iova;
  311. ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
  312. if (ret) {
  313. ret = -EFAULT;
  314. goto out;
  315. }
  316. /* validate input from userspace: */
  317. switch (submit_cmd.type) {
  318. case MSM_SUBMIT_CMD_BUF:
  319. case MSM_SUBMIT_CMD_IB_TARGET_BUF:
  320. case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
  321. break;
  322. default:
  323. DRM_ERROR("invalid type: %08x\n", submit_cmd.type);
  324. ret = -EINVAL;
  325. goto out;
  326. }
  327. ret = submit_bo(submit, submit_cmd.submit_idx,
  328. &msm_obj, &iova, NULL);
  329. if (ret)
  330. goto out;
  331. if (submit_cmd.size % 4) {
  332. DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
  333. submit_cmd.size);
  334. ret = -EINVAL;
  335. goto out;
  336. }
  337. if ((submit_cmd.size + submit_cmd.submit_offset) >=
  338. msm_obj->base.size) {
  339. DRM_ERROR("invalid cmdstream size: %u\n", submit_cmd.size);
  340. ret = -EINVAL;
  341. goto out;
  342. }
  343. submit->cmd[i].type = submit_cmd.type;
  344. submit->cmd[i].size = submit_cmd.size / 4;
  345. submit->cmd[i].iova = iova + submit_cmd.submit_offset;
  346. submit->cmd[i].idx = submit_cmd.submit_idx;
  347. if (submit->valid)
  348. continue;
  349. ret = submit_reloc(submit, msm_obj, submit_cmd.submit_offset,
  350. submit_cmd.nr_relocs, submit_cmd.relocs);
  351. if (ret)
  352. goto out;
  353. }
  354. submit->nr_cmds = i;
  355. ret = msm_gpu_submit(gpu, submit, ctx);
  356. args->fence = submit->fence->seqno;
  357. out:
  358. submit_cleanup(submit);
  359. if (ret)
  360. msm_gem_submit_free(submit);
  361. mutex_unlock(&dev->struct_mutex);
  362. return ret;
  363. }