qxl_ioctl.c 11 KB

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