qxl_ioctl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright 2013 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Dave Airlie
  23. * Alon Levy
  24. */
  25. #include "qxl_drv.h"
  26. #include "qxl_object.h"
  27. /*
  28. * TODO: allocating a new gem(in qxl_bo) for each request.
  29. * This is wasteful since bo's are page aligned.
  30. */
  31. static int qxl_alloc_ioctl(struct drm_device *dev, void *data,
  32. struct drm_file *file_priv)
  33. {
  34. struct qxl_device *qdev = dev->dev_private;
  35. struct drm_qxl_alloc *qxl_alloc = data;
  36. int ret;
  37. struct qxl_bo *qobj;
  38. uint32_t handle;
  39. u32 domain = QXL_GEM_DOMAIN_VRAM;
  40. if (qxl_alloc->size == 0) {
  41. DRM_ERROR("invalid size %d\n", qxl_alloc->size);
  42. return -EINVAL;
  43. }
  44. ret = qxl_gem_object_create_with_handle(qdev, file_priv,
  45. domain,
  46. qxl_alloc->size,
  47. NULL,
  48. &qobj, &handle);
  49. if (ret) {
  50. DRM_ERROR("%s: failed to create gem ret=%d\n",
  51. __func__, ret);
  52. return -ENOMEM;
  53. }
  54. qxl_alloc->handle = handle;
  55. return 0;
  56. }
  57. static int qxl_map_ioctl(struct drm_device *dev, void *data,
  58. struct drm_file *file_priv)
  59. {
  60. struct qxl_device *qdev = dev->dev_private;
  61. struct drm_qxl_map *qxl_map = data;
  62. return qxl_mode_dumb_mmap(file_priv, qdev->ddev, qxl_map->handle,
  63. &qxl_map->offset);
  64. }
  65. /*
  66. * dst must be validated, i.e. whole bo on vram/surfacesram (right now all bo's
  67. * are on vram).
  68. * *(dst + dst_off) = qxl_bo_physical_address(src, src_off)
  69. */
  70. static void
  71. apply_reloc(struct qxl_device *qdev, struct qxl_bo *dst, uint64_t dst_off,
  72. struct qxl_bo *src, uint64_t src_off)
  73. {
  74. void *reloc_page;
  75. reloc_page = qxl_bo_kmap_atomic_page(qdev, dst, dst_off & PAGE_MASK);
  76. *(uint64_t *)(reloc_page + (dst_off & ~PAGE_MASK)) = qxl_bo_physical_address(qdev,
  77. src, src_off);
  78. qxl_bo_kunmap_atomic_page(qdev, dst, reloc_page);
  79. }
  80. static void
  81. apply_surf_reloc(struct qxl_device *qdev, struct qxl_bo *dst, uint64_t dst_off,
  82. struct qxl_bo *src)
  83. {
  84. uint32_t id = 0;
  85. void *reloc_page;
  86. if (src && !src->is_primary)
  87. id = src->surface_id;
  88. reloc_page = qxl_bo_kmap_atomic_page(qdev, dst, dst_off & PAGE_MASK);
  89. *(uint32_t *)(reloc_page + (dst_off & ~PAGE_MASK)) = id;
  90. qxl_bo_kunmap_atomic_page(qdev, dst, reloc_page);
  91. }
  92. /* return holding the reference to this object */
  93. static struct qxl_bo *qxlhw_handle_to_bo(struct qxl_device *qdev,
  94. struct drm_file *file_priv, uint64_t handle,
  95. struct qxl_reloc_list *reloc_list)
  96. {
  97. struct drm_gem_object *gobj;
  98. struct qxl_bo *qobj;
  99. int ret;
  100. gobj = drm_gem_object_lookup(qdev->ddev, file_priv, handle);
  101. if (!gobj) {
  102. DRM_ERROR("bad bo handle %lld\n", handle);
  103. return NULL;
  104. }
  105. qobj = gem_to_qxl_bo(gobj);
  106. ret = qxl_bo_list_add(reloc_list, qobj);
  107. if (ret)
  108. return NULL;
  109. return qobj;
  110. }
  111. /*
  112. * Usage of execbuffer:
  113. * Relocations need to take into account the full QXLDrawable size.
  114. * However, the command as passed from user space must *not* contain the initial
  115. * QXLReleaseInfo struct (first XXX bytes)
  116. */
  117. static int qxl_execbuffer_ioctl(struct drm_device *dev, void *data,
  118. struct drm_file *file_priv)
  119. {
  120. struct qxl_device *qdev = dev->dev_private;
  121. struct drm_qxl_execbuffer *execbuffer = data;
  122. struct drm_qxl_command user_cmd;
  123. int cmd_num;
  124. struct qxl_bo *reloc_src_bo;
  125. struct qxl_bo *reloc_dst_bo;
  126. struct drm_qxl_reloc reloc;
  127. void *fb_cmd;
  128. int i, ret;
  129. struct qxl_reloc_list reloc_list;
  130. int unwritten;
  131. uint32_t reloc_dst_offset;
  132. INIT_LIST_HEAD(&reloc_list.bos);
  133. for (cmd_num = 0; cmd_num < execbuffer->commands_num; ++cmd_num) {
  134. struct qxl_release *release;
  135. struct qxl_bo *cmd_bo;
  136. int release_type;
  137. struct drm_qxl_command *commands =
  138. (struct drm_qxl_command *)(uintptr_t)execbuffer->commands;
  139. if (DRM_COPY_FROM_USER(&user_cmd, &commands[cmd_num],
  140. sizeof(user_cmd)))
  141. return -EFAULT;
  142. switch (user_cmd.type) {
  143. case QXL_CMD_DRAW:
  144. release_type = QXL_RELEASE_DRAWABLE;
  145. break;
  146. case QXL_CMD_SURFACE:
  147. case QXL_CMD_CURSOR:
  148. default:
  149. DRM_DEBUG("Only draw commands in execbuffers\n");
  150. return -EINVAL;
  151. break;
  152. }
  153. if (user_cmd.command_size > PAGE_SIZE - sizeof(union qxl_release_info))
  154. return -EINVAL;
  155. if (!access_ok(VERIFY_READ,
  156. (void *)(unsigned long)user_cmd.command,
  157. user_cmd.command_size))
  158. return -EFAULT;
  159. ret = qxl_alloc_release_reserved(qdev,
  160. sizeof(union qxl_release_info) +
  161. user_cmd.command_size,
  162. release_type,
  163. &release,
  164. &cmd_bo);
  165. if (ret)
  166. return ret;
  167. /* TODO copy slow path code from i915 */
  168. fb_cmd = qxl_bo_kmap_atomic_page(qdev, cmd_bo, (release->release_offset & PAGE_SIZE));
  169. unwritten = __copy_from_user_inatomic_nocache(fb_cmd + sizeof(union qxl_release_info) + (release->release_offset & ~PAGE_SIZE), (void *)(unsigned long)user_cmd.command, user_cmd.command_size);
  170. {
  171. struct qxl_drawable *draw = fb_cmd;
  172. draw->mm_time = qdev->rom->mm_clock;
  173. }
  174. qxl_bo_kunmap_atomic_page(qdev, cmd_bo, fb_cmd);
  175. if (unwritten) {
  176. DRM_ERROR("got unwritten %d\n", unwritten);
  177. qxl_release_unreserve(qdev, release);
  178. qxl_release_free(qdev, release);
  179. return -EFAULT;
  180. }
  181. for (i = 0 ; i < user_cmd.relocs_num; ++i) {
  182. if (DRM_COPY_FROM_USER(&reloc,
  183. &((struct drm_qxl_reloc *)(uintptr_t)user_cmd.relocs)[i],
  184. sizeof(reloc))) {
  185. qxl_bo_list_unreserve(&reloc_list, true);
  186. qxl_release_unreserve(qdev, release);
  187. qxl_release_free(qdev, release);
  188. return -EFAULT;
  189. }
  190. /* add the bos to the list of bos to validate -
  191. need to validate first then process relocs? */
  192. if (reloc.dst_handle) {
  193. reloc_dst_bo = qxlhw_handle_to_bo(qdev, file_priv,
  194. reloc.dst_handle, &reloc_list);
  195. if (!reloc_dst_bo) {
  196. qxl_bo_list_unreserve(&reloc_list, true);
  197. qxl_release_unreserve(qdev, release);
  198. qxl_release_free(qdev, release);
  199. return -EINVAL;
  200. }
  201. reloc_dst_offset = 0;
  202. } else {
  203. reloc_dst_bo = cmd_bo;
  204. reloc_dst_offset = release->release_offset;
  205. }
  206. /* reserve and validate the reloc dst bo */
  207. if (reloc.reloc_type == QXL_RELOC_TYPE_BO || reloc.src_handle > 0) {
  208. reloc_src_bo =
  209. qxlhw_handle_to_bo(qdev, file_priv,
  210. reloc.src_handle, &reloc_list);
  211. if (!reloc_src_bo) {
  212. if (reloc_dst_bo != cmd_bo)
  213. drm_gem_object_unreference_unlocked(&reloc_dst_bo->gem_base);
  214. qxl_bo_list_unreserve(&reloc_list, true);
  215. qxl_release_unreserve(qdev, release);
  216. qxl_release_free(qdev, release);
  217. return -EINVAL;
  218. }
  219. } else
  220. reloc_src_bo = NULL;
  221. if (reloc.reloc_type == QXL_RELOC_TYPE_BO) {
  222. apply_reloc(qdev, reloc_dst_bo, reloc_dst_offset + reloc.dst_offset,
  223. reloc_src_bo, reloc.src_offset);
  224. } else if (reloc.reloc_type == QXL_RELOC_TYPE_SURF) {
  225. apply_surf_reloc(qdev, reloc_dst_bo, reloc_dst_offset + reloc.dst_offset, reloc_src_bo);
  226. } else {
  227. DRM_ERROR("unknown reloc type %d\n", reloc.reloc_type);
  228. return -EINVAL;
  229. }
  230. if (reloc_src_bo && reloc_src_bo != cmd_bo) {
  231. qxl_release_add_res(qdev, release, reloc_src_bo);
  232. drm_gem_object_unreference_unlocked(&reloc_src_bo->gem_base);
  233. }
  234. if (reloc_dst_bo != cmd_bo)
  235. drm_gem_object_unreference_unlocked(&reloc_dst_bo->gem_base);
  236. }
  237. qxl_fence_releaseable(qdev, release);
  238. ret = qxl_push_command_ring_release(qdev, release, user_cmd.type, true);
  239. if (ret == -ERESTARTSYS) {
  240. qxl_release_unreserve(qdev, release);
  241. qxl_release_free(qdev, release);
  242. qxl_bo_list_unreserve(&reloc_list, true);
  243. return ret;
  244. }
  245. qxl_release_unreserve(qdev, release);
  246. }
  247. qxl_bo_list_unreserve(&reloc_list, 0);
  248. return 0;
  249. }
  250. static int qxl_update_area_ioctl(struct drm_device *dev, void *data,
  251. struct drm_file *file)
  252. {
  253. struct qxl_device *qdev = dev->dev_private;
  254. struct drm_qxl_update_area *update_area = data;
  255. struct qxl_rect area = {.left = update_area->left,
  256. .top = update_area->top,
  257. .right = update_area->right,
  258. .bottom = update_area->bottom};
  259. int ret;
  260. struct drm_gem_object *gobj = NULL;
  261. struct qxl_bo *qobj = NULL;
  262. if (update_area->left >= update_area->right ||
  263. update_area->top >= update_area->bottom)
  264. return -EINVAL;
  265. gobj = drm_gem_object_lookup(dev, file, update_area->handle);
  266. if (gobj == NULL)
  267. return -ENOENT;
  268. qobj = gem_to_qxl_bo(gobj);
  269. ret = qxl_bo_reserve(qobj, false);
  270. if (ret)
  271. goto out;
  272. if (!qobj->pin_count) {
  273. qxl_ttm_placement_from_domain(qobj, qobj->type);
  274. ret = ttm_bo_validate(&qobj->tbo, &qobj->placement,
  275. true, false);
  276. if (unlikely(ret))
  277. goto out;
  278. }
  279. ret = qxl_bo_check_id(qdev, qobj);
  280. if (ret)
  281. goto out2;
  282. if (!qobj->surface_id)
  283. DRM_ERROR("got update area for surface with no id %d\n", update_area->handle);
  284. ret = qxl_io_update_area(qdev, qobj, &area);
  285. out2:
  286. qxl_bo_unreserve(qobj);
  287. out:
  288. drm_gem_object_unreference_unlocked(gobj);
  289. return ret;
  290. }
  291. static int qxl_getparam_ioctl(struct drm_device *dev, void *data,
  292. struct drm_file *file_priv)
  293. {
  294. struct qxl_device *qdev = dev->dev_private;
  295. struct drm_qxl_getparam *param = data;
  296. switch (param->param) {
  297. case QXL_PARAM_NUM_SURFACES:
  298. param->value = qdev->rom->n_surfaces;
  299. break;
  300. case QXL_PARAM_MAX_RELOCS:
  301. param->value = QXL_MAX_RES;
  302. break;
  303. default:
  304. return -EINVAL;
  305. }
  306. return 0;
  307. }
  308. static int qxl_clientcap_ioctl(struct drm_device *dev, void *data,
  309. struct drm_file *file_priv)
  310. {
  311. struct qxl_device *qdev = dev->dev_private;
  312. struct drm_qxl_clientcap *param = data;
  313. int byte, idx;
  314. byte = param->index / 8;
  315. idx = param->index % 8;
  316. if (qdev->pdev->revision < 4)
  317. return -ENOSYS;
  318. if (byte >= 58)
  319. return -ENOSYS;
  320. if (qdev->rom->client_capabilities[byte] & (1 << idx))
  321. return 0;
  322. return -ENOSYS;
  323. }
  324. static int qxl_alloc_surf_ioctl(struct drm_device *dev, void *data,
  325. struct drm_file *file)
  326. {
  327. struct qxl_device *qdev = dev->dev_private;
  328. struct drm_qxl_alloc_surf *param = data;
  329. struct qxl_bo *qobj;
  330. int handle;
  331. int ret;
  332. int size, actual_stride;
  333. struct qxl_surface surf;
  334. /* work out size allocate bo with handle */
  335. actual_stride = param->stride < 0 ? -param->stride : param->stride;
  336. size = actual_stride * param->height + actual_stride;
  337. surf.format = param->format;
  338. surf.width = param->width;
  339. surf.height = param->height;
  340. surf.stride = param->stride;
  341. surf.data = 0;
  342. ret = qxl_gem_object_create_with_handle(qdev, file,
  343. QXL_GEM_DOMAIN_SURFACE,
  344. size,
  345. &surf,
  346. &qobj, &handle);
  347. if (ret) {
  348. DRM_ERROR("%s: failed to create gem ret=%d\n",
  349. __func__, ret);
  350. return -ENOMEM;
  351. } else
  352. param->handle = handle;
  353. return ret;
  354. }
  355. struct drm_ioctl_desc qxl_ioctls[] = {
  356. DRM_IOCTL_DEF_DRV(QXL_ALLOC, qxl_alloc_ioctl, DRM_AUTH|DRM_UNLOCKED),
  357. DRM_IOCTL_DEF_DRV(QXL_MAP, qxl_map_ioctl, DRM_AUTH|DRM_UNLOCKED),
  358. DRM_IOCTL_DEF_DRV(QXL_EXECBUFFER, qxl_execbuffer_ioctl,
  359. DRM_AUTH|DRM_UNLOCKED),
  360. DRM_IOCTL_DEF_DRV(QXL_UPDATE_AREA, qxl_update_area_ioctl,
  361. DRM_AUTH|DRM_UNLOCKED),
  362. DRM_IOCTL_DEF_DRV(QXL_GETPARAM, qxl_getparam_ioctl,
  363. DRM_AUTH|DRM_UNLOCKED),
  364. DRM_IOCTL_DEF_DRV(QXL_CLIENTCAP, qxl_clientcap_ioctl,
  365. DRM_AUTH|DRM_UNLOCKED),
  366. DRM_IOCTL_DEF_DRV(QXL_ALLOC_SURF, qxl_alloc_surf_ioctl,
  367. DRM_AUTH|DRM_UNLOCKED),
  368. };
  369. int qxl_max_ioctls = DRM_ARRAY_SIZE(qxl_ioctls);