qxl_ioctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 <linux/pci.h>
  26. #include <linux/uaccess.h>
  27. #include <drm/drm_print.h>
  28. #include "qxl_drv.h"
  29. #include "qxl_object.h"
  30. /*
  31. * TODO: allocating a new gem(in qxl_bo) for each request.
  32. * This is wasteful since bo's are page aligned.
  33. */
  34. int qxl_alloc_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
  35. {
  36. struct qxl_device *qdev = to_qxl(dev);
  37. struct drm_qxl_alloc *qxl_alloc = data;
  38. int ret;
  39. uint32_t handle;
  40. u32 domain = QXL_GEM_DOMAIN_VRAM;
  41. if (qxl_alloc->size == 0) {
  42. DRM_ERROR("invalid size %d\n", qxl_alloc->size);
  43. return -EINVAL;
  44. }
  45. ret = qxl_gem_object_create_with_handle(qdev, file_priv,
  46. domain,
  47. qxl_alloc->size,
  48. NULL,
  49. NULL, &handle);
  50. if (ret) {
  51. DRM_ERROR("%s: failed to create gem ret=%d\n",
  52. __func__, ret);
  53. return -ENOMEM;
  54. }
  55. qxl_alloc->handle = handle;
  56. return 0;
  57. }
  58. int qxl_map_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
  59. {
  60. struct qxl_device *qdev = to_qxl(dev);
  61. struct drm_qxl_map *qxl_map = data;
  62. return drm_gem_ttm_dumb_map_offset(file_priv, &qdev->ddev, qxl_map->handle,
  63. &qxl_map->offset);
  64. }
  65. struct qxl_reloc_info {
  66. int type;
  67. struct qxl_bo *dst_bo;
  68. uint32_t dst_offset;
  69. struct qxl_bo *src_bo;
  70. int src_offset;
  71. };
  72. /*
  73. * dst must be validated, i.e. whole bo on vram/surfacesram (right now all bo's
  74. * are on vram).
  75. * *(dst + dst_off) = qxl_bo_physical_address(src, src_off)
  76. */
  77. static void
  78. apply_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info)
  79. {
  80. void *reloc_page;
  81. reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK);
  82. *(uint64_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = qxl_bo_physical_address(qdev,
  83. info->src_bo,
  84. info->src_offset);
  85. qxl_bo_kunmap_atomic_page(qdev, info->dst_bo, reloc_page);
  86. }
  87. static void
  88. apply_surf_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info)
  89. {
  90. uint32_t id = 0;
  91. void *reloc_page;
  92. if (info->src_bo && !info->src_bo->is_primary)
  93. id = info->src_bo->surface_id;
  94. reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK);
  95. *(uint32_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = id;
  96. qxl_bo_kunmap_atomic_page(qdev, info->dst_bo, reloc_page);
  97. }
  98. /* return holding the reference to this object */
  99. static int qxlhw_handle_to_bo(struct drm_file *file_priv, uint64_t handle,
  100. struct qxl_release *release, struct qxl_bo **qbo_p)
  101. {
  102. struct drm_gem_object *gobj;
  103. struct qxl_bo *qobj;
  104. int ret;
  105. gobj = drm_gem_object_lookup(file_priv, handle);
  106. if (!gobj)
  107. return -EINVAL;
  108. qobj = gem_to_qxl_bo(gobj);
  109. ret = qxl_release_list_add(release, qobj);
  110. drm_gem_object_put(gobj);
  111. if (ret)
  112. return ret;
  113. *qbo_p = qobj;
  114. return 0;
  115. }
  116. /*
  117. * Usage of execbuffer:
  118. * Relocations need to take into account the full QXLDrawable size.
  119. * However, the command as passed from user space must *not* contain the initial
  120. * QXLReleaseInfo struct (first XXX bytes)
  121. */
  122. static int qxl_process_single_command(struct qxl_device *qdev,
  123. struct drm_qxl_command *cmd,
  124. struct drm_file *file_priv)
  125. {
  126. struct qxl_reloc_info *reloc_info;
  127. int release_type;
  128. struct qxl_release *release;
  129. struct qxl_bo *cmd_bo;
  130. void *fb_cmd;
  131. int i, ret;
  132. int unwritten;
  133. switch (cmd->type) {
  134. case QXL_CMD_DRAW:
  135. release_type = QXL_RELEASE_DRAWABLE;
  136. break;
  137. case QXL_CMD_SURFACE:
  138. case QXL_CMD_CURSOR:
  139. default:
  140. DRM_DEBUG("Only draw commands in execbuffers\n");
  141. return -EINVAL;
  142. }
  143. if (cmd->command_size > PAGE_SIZE - sizeof(union qxl_release_info))
  144. return -EINVAL;
  145. if (!access_ok(u64_to_user_ptr(cmd->command),
  146. cmd->command_size))
  147. return -EFAULT;
  148. reloc_info = kmalloc_objs(struct qxl_reloc_info, cmd->relocs_num);
  149. if (!reloc_info)
  150. return -ENOMEM;
  151. ret = qxl_alloc_release_reserved(qdev,
  152. sizeof(union qxl_release_info) +
  153. cmd->command_size,
  154. release_type,
  155. &release,
  156. &cmd_bo);
  157. if (ret)
  158. goto out_free_reloc;
  159. /* TODO copy slow path code from i915 */
  160. fb_cmd = qxl_bo_kmap_atomic_page(qdev, cmd_bo, (release->release_offset & PAGE_MASK));
  161. unwritten = __copy_from_user_inatomic_nocache
  162. (fb_cmd + sizeof(union qxl_release_info) + (release->release_offset & ~PAGE_MASK),
  163. u64_to_user_ptr(cmd->command), cmd->command_size);
  164. {
  165. struct qxl_drawable *draw = fb_cmd;
  166. draw->mm_time = qdev->rom->mm_clock;
  167. }
  168. qxl_bo_kunmap_atomic_page(qdev, cmd_bo, fb_cmd);
  169. if (unwritten) {
  170. DRM_ERROR("got unwritten %d\n", unwritten);
  171. ret = -EFAULT;
  172. goto out_free_release;
  173. }
  174. /* fill out reloc info structs */
  175. for (i = 0; i < cmd->relocs_num; ++i) {
  176. struct drm_qxl_reloc reloc;
  177. struct drm_qxl_reloc __user *u = u64_to_user_ptr(cmd->relocs);
  178. if (copy_from_user(&reloc, u + i, sizeof(reloc))) {
  179. ret = -EFAULT;
  180. goto out_free_bos;
  181. }
  182. /* add the bos to the list of bos to validate -
  183. need to validate first then process relocs? */
  184. if (reloc.reloc_type != QXL_RELOC_TYPE_BO && reloc.reloc_type != QXL_RELOC_TYPE_SURF) {
  185. DRM_DEBUG("unknown reloc type %d\n", reloc.reloc_type);
  186. ret = -EINVAL;
  187. goto out_free_bos;
  188. }
  189. reloc_info[i].type = reloc.reloc_type;
  190. if (reloc.dst_handle) {
  191. ret = qxlhw_handle_to_bo(file_priv, reloc.dst_handle, release,
  192. &reloc_info[i].dst_bo);
  193. if (ret)
  194. goto out_free_bos;
  195. reloc_info[i].dst_offset = reloc.dst_offset;
  196. } else {
  197. reloc_info[i].dst_bo = cmd_bo;
  198. reloc_info[i].dst_offset = reloc.dst_offset + release->release_offset;
  199. }
  200. /* reserve and validate the reloc dst bo */
  201. if (reloc.reloc_type == QXL_RELOC_TYPE_BO || reloc.src_handle) {
  202. ret = qxlhw_handle_to_bo(file_priv, reloc.src_handle, release,
  203. &reloc_info[i].src_bo);
  204. if (ret)
  205. goto out_free_bos;
  206. reloc_info[i].src_offset = reloc.src_offset;
  207. } else {
  208. reloc_info[i].src_bo = NULL;
  209. reloc_info[i].src_offset = 0;
  210. }
  211. }
  212. /* validate all buffers */
  213. ret = qxl_release_reserve_list(release, false);
  214. if (ret)
  215. goto out_free_bos;
  216. for (i = 0; i < cmd->relocs_num; ++i) {
  217. if (reloc_info[i].type == QXL_RELOC_TYPE_BO)
  218. apply_reloc(qdev, &reloc_info[i]);
  219. else if (reloc_info[i].type == QXL_RELOC_TYPE_SURF)
  220. apply_surf_reloc(qdev, &reloc_info[i]);
  221. }
  222. qxl_release_fence_buffer_objects(release);
  223. ret = qxl_push_command_ring_release(qdev, release, cmd->type, true);
  224. out_free_bos:
  225. out_free_release:
  226. if (ret)
  227. qxl_release_free(qdev, release);
  228. out_free_reloc:
  229. kfree(reloc_info);
  230. return ret;
  231. }
  232. int qxl_execbuffer_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
  233. {
  234. struct qxl_device *qdev = to_qxl(dev);
  235. struct drm_qxl_execbuffer *execbuffer = data;
  236. struct drm_qxl_command user_cmd;
  237. int cmd_num;
  238. int ret;
  239. for (cmd_num = 0; cmd_num < execbuffer->commands_num; ++cmd_num) {
  240. struct drm_qxl_command __user *commands =
  241. u64_to_user_ptr(execbuffer->commands);
  242. if (copy_from_user(&user_cmd, commands + cmd_num,
  243. sizeof(user_cmd)))
  244. return -EFAULT;
  245. ret = qxl_process_single_command(qdev, &user_cmd, file_priv);
  246. if (ret)
  247. return ret;
  248. }
  249. return 0;
  250. }
  251. int qxl_update_area_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  252. {
  253. struct qxl_device *qdev = to_qxl(dev);
  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. struct ttm_operation_ctx ctx = { true, false };
  263. if (update_area->left >= update_area->right ||
  264. update_area->top >= update_area->bottom)
  265. return -EINVAL;
  266. gobj = drm_gem_object_lookup(file, update_area->handle);
  267. if (gobj == NULL)
  268. return -ENOENT;
  269. qobj = gem_to_qxl_bo(gobj);
  270. ret = qxl_bo_reserve(qobj);
  271. if (ret)
  272. goto out;
  273. if (!qobj->tbo.pin_count) {
  274. qxl_ttm_placement_from_domain(qobj, qobj->type);
  275. ret = ttm_bo_validate(&qobj->tbo, &qobj->placement, &ctx);
  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_put(gobj);
  289. return ret;
  290. }
  291. int qxl_getparam_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
  292. {
  293. struct qxl_device *qdev = to_qxl(dev);
  294. struct drm_qxl_getparam *param = data;
  295. switch (param->param) {
  296. case QXL_PARAM_NUM_SURFACES:
  297. param->value = qdev->rom->n_surfaces;
  298. break;
  299. case QXL_PARAM_MAX_RELOCS:
  300. param->value = QXL_MAX_RES;
  301. break;
  302. default:
  303. return -EINVAL;
  304. }
  305. return 0;
  306. }
  307. int qxl_clientcap_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
  308. {
  309. struct qxl_device *qdev = to_qxl(dev);
  310. struct pci_dev *pdev = to_pci_dev(dev->dev);
  311. struct drm_qxl_clientcap *param = data;
  312. int byte, idx;
  313. byte = param->index / 8;
  314. idx = param->index % 8;
  315. if (pdev->revision < 4)
  316. return -ENOSYS;
  317. if (byte >= 58)
  318. return -ENOSYS;
  319. if (qdev->rom->client_capabilities[byte] & (1 << idx))
  320. return 0;
  321. return -ENOSYS;
  322. }
  323. int qxl_alloc_surf_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  324. {
  325. struct qxl_device *qdev = to_qxl(dev);
  326. struct drm_qxl_alloc_surf *param = data;
  327. int handle;
  328. int ret;
  329. int size, actual_stride;
  330. struct qxl_surface surf;
  331. /* work out size allocate bo with handle */
  332. actual_stride = param->stride < 0 ? -param->stride : param->stride;
  333. size = actual_stride * param->height + actual_stride;
  334. surf.format = param->format;
  335. surf.width = param->width;
  336. surf.height = param->height;
  337. surf.stride = param->stride;
  338. surf.data = 0;
  339. ret = qxl_gem_object_create_with_handle(qdev, file,
  340. QXL_GEM_DOMAIN_SURFACE,
  341. size,
  342. &surf,
  343. NULL, &handle);
  344. if (ret) {
  345. DRM_ERROR("%s: failed to create gem ret=%d\n",
  346. __func__, ret);
  347. return -ENOMEM;
  348. } else
  349. param->handle = handle;
  350. return ret;
  351. }