qxl_ioctl.c 11 KB

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