qxl_ioctl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. 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 qxl_device *qdev,
  100. struct drm_file *file_priv, uint64_t handle,
  101. struct qxl_release *release, struct qxl_bo **qbo_p)
  102. {
  103. struct drm_gem_object *gobj;
  104. struct qxl_bo *qobj;
  105. int ret;
  106. gobj = drm_gem_object_lookup(qdev->ddev, file_priv, handle);
  107. if (!gobj)
  108. return -EINVAL;
  109. qobj = gem_to_qxl_bo(gobj);
  110. ret = qxl_release_list_add(release, qobj);
  111. drm_gem_object_unreference_unlocked(gobj);
  112. if (ret)
  113. return ret;
  114. *qbo_p = qobj;
  115. return 0;
  116. }
  117. /*
  118. * Usage of execbuffer:
  119. * Relocations need to take into account the full QXLDrawable size.
  120. * However, the command as passed from user space must *not* contain the initial
  121. * QXLReleaseInfo struct (first XXX bytes)
  122. */
  123. static int qxl_process_single_command(struct qxl_device *qdev,
  124. struct drm_qxl_command *cmd,
  125. struct drm_file *file_priv)
  126. {
  127. struct qxl_reloc_info *reloc_info;
  128. int release_type;
  129. struct qxl_release *release;
  130. struct qxl_bo *cmd_bo;
  131. void *fb_cmd;
  132. int i, ret, num_relocs;
  133. int unwritten;
  134. switch (cmd->type) {
  135. case QXL_CMD_DRAW:
  136. release_type = QXL_RELEASE_DRAWABLE;
  137. break;
  138. case QXL_CMD_SURFACE:
  139. case QXL_CMD_CURSOR:
  140. default:
  141. DRM_DEBUG("Only draw commands in execbuffers\n");
  142. return -EINVAL;
  143. break;
  144. }
  145. if (cmd->command_size > PAGE_SIZE - sizeof(union qxl_release_info))
  146. return -EINVAL;
  147. if (!access_ok(VERIFY_READ,
  148. (void *)(unsigned long)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_SIZE));
  165. unwritten = __copy_from_user_inatomic_nocache(fb_cmd + sizeof(union qxl_release_info) + (release->release_offset & ~PAGE_SIZE), (void *)(unsigned long)cmd->command, cmd->command_size);
  166. {
  167. struct qxl_drawable *draw = fb_cmd;
  168. draw->mm_time = qdev->rom->mm_clock;
  169. }
  170. qxl_bo_kunmap_atomic_page(qdev, cmd_bo, fb_cmd);
  171. if (unwritten) {
  172. DRM_ERROR("got unwritten %d\n", unwritten);
  173. ret = -EFAULT;
  174. goto out_free_release;
  175. }
  176. /* fill out reloc info structs */
  177. num_relocs = 0;
  178. for (i = 0; i < cmd->relocs_num; ++i) {
  179. struct drm_qxl_reloc reloc;
  180. if (copy_from_user(&reloc,
  181. &((struct drm_qxl_reloc *)(uintptr_t)cmd->relocs)[i],
  182. sizeof(reloc))) {
  183. ret = -EFAULT;
  184. goto out_free_bos;
  185. }
  186. /* add the bos to the list of bos to validate -
  187. need to validate first then process relocs? */
  188. if (reloc.reloc_type != QXL_RELOC_TYPE_BO && reloc.reloc_type != QXL_RELOC_TYPE_SURF) {
  189. DRM_DEBUG("unknown reloc type %d\n", reloc.reloc_type);
  190. ret = -EINVAL;
  191. goto out_free_bos;
  192. }
  193. reloc_info[i].type = reloc.reloc_type;
  194. if (reloc.dst_handle) {
  195. ret = qxlhw_handle_to_bo(qdev, file_priv, reloc.dst_handle, release,
  196. &reloc_info[i].dst_bo);
  197. if (ret)
  198. goto out_free_bos;
  199. reloc_info[i].dst_offset = reloc.dst_offset;
  200. } else {
  201. reloc_info[i].dst_bo = cmd_bo;
  202. reloc_info[i].dst_offset = reloc.dst_offset + release->release_offset;
  203. }
  204. num_relocs++;
  205. /* reserve and validate the reloc dst bo */
  206. if (reloc.reloc_type == QXL_RELOC_TYPE_BO || reloc.src_handle) {
  207. ret = qxlhw_handle_to_bo(qdev, file_priv, reloc.src_handle, release,
  208. &reloc_info[i].src_bo);
  209. if (ret)
  210. goto out_free_bos;
  211. reloc_info[i].src_offset = reloc.src_offset;
  212. } else {
  213. reloc_info[i].src_bo = NULL;
  214. reloc_info[i].src_offset = 0;
  215. }
  216. }
  217. /* validate all buffers */
  218. ret = qxl_release_reserve_list(release, false);
  219. if (ret)
  220. goto out_free_bos;
  221. for (i = 0; i < cmd->relocs_num; ++i) {
  222. if (reloc_info[i].type == QXL_RELOC_TYPE_BO)
  223. apply_reloc(qdev, &reloc_info[i]);
  224. else if (reloc_info[i].type == QXL_RELOC_TYPE_SURF)
  225. apply_surf_reloc(qdev, &reloc_info[i]);
  226. }
  227. ret = qxl_push_command_ring_release(qdev, release, cmd->type, true);
  228. if (ret)
  229. qxl_release_backoff_reserve_list(release);
  230. else
  231. qxl_release_fence_buffer_objects(release);
  232. out_free_bos:
  233. out_free_release:
  234. if (ret)
  235. qxl_release_free(qdev, release);
  236. out_free_reloc:
  237. kfree(reloc_info);
  238. return ret;
  239. }
  240. static int qxl_execbuffer_ioctl(struct drm_device *dev, void *data,
  241. struct drm_file *file_priv)
  242. {
  243. struct qxl_device *qdev = dev->dev_private;
  244. struct drm_qxl_execbuffer *execbuffer = data;
  245. struct drm_qxl_command user_cmd;
  246. int cmd_num;
  247. int ret;
  248. for (cmd_num = 0; cmd_num < execbuffer->commands_num; ++cmd_num) {
  249. struct drm_qxl_command *commands =
  250. (struct drm_qxl_command *)(uintptr_t)execbuffer->commands;
  251. if (copy_from_user(&user_cmd, &commands[cmd_num],
  252. sizeof(user_cmd)))
  253. return -EFAULT;
  254. ret = qxl_process_single_command(qdev, &user_cmd, file_priv);
  255. if (ret)
  256. return ret;
  257. }
  258. return 0;
  259. }
  260. static int qxl_update_area_ioctl(struct drm_device *dev, void *data,
  261. struct drm_file *file)
  262. {
  263. struct qxl_device *qdev = dev->dev_private;
  264. struct drm_qxl_update_area *update_area = data;
  265. struct qxl_rect area = {.left = update_area->left,
  266. .top = update_area->top,
  267. .right = update_area->right,
  268. .bottom = update_area->bottom};
  269. int ret;
  270. struct drm_gem_object *gobj = NULL;
  271. struct qxl_bo *qobj = NULL;
  272. if (update_area->left >= update_area->right ||
  273. update_area->top >= update_area->bottom)
  274. return -EINVAL;
  275. gobj = drm_gem_object_lookup(dev, file, update_area->handle);
  276. if (gobj == NULL)
  277. return -ENOENT;
  278. qobj = gem_to_qxl_bo(gobj);
  279. ret = qxl_bo_reserve(qobj, false);
  280. if (ret)
  281. goto out;
  282. if (!qobj->pin_count) {
  283. qxl_ttm_placement_from_domain(qobj, qobj->type, false);
  284. ret = ttm_bo_validate(&qobj->tbo, &qobj->placement,
  285. true, false);
  286. if (unlikely(ret))
  287. goto out;
  288. }
  289. ret = qxl_bo_check_id(qdev, qobj);
  290. if (ret)
  291. goto out2;
  292. if (!qobj->surface_id)
  293. DRM_ERROR("got update area for surface with no id %d\n", update_area->handle);
  294. ret = qxl_io_update_area(qdev, qobj, &area);
  295. out2:
  296. qxl_bo_unreserve(qobj);
  297. out:
  298. drm_gem_object_unreference_unlocked(gobj);
  299. return ret;
  300. }
  301. static int qxl_getparam_ioctl(struct drm_device *dev, void *data,
  302. struct drm_file *file_priv)
  303. {
  304. struct qxl_device *qdev = dev->dev_private;
  305. struct drm_qxl_getparam *param = data;
  306. switch (param->param) {
  307. case QXL_PARAM_NUM_SURFACES:
  308. param->value = qdev->rom->n_surfaces;
  309. break;
  310. case QXL_PARAM_MAX_RELOCS:
  311. param->value = QXL_MAX_RES;
  312. break;
  313. default:
  314. return -EINVAL;
  315. }
  316. return 0;
  317. }
  318. static int qxl_clientcap_ioctl(struct drm_device *dev, void *data,
  319. struct drm_file *file_priv)
  320. {
  321. struct qxl_device *qdev = dev->dev_private;
  322. struct drm_qxl_clientcap *param = data;
  323. int byte, idx;
  324. byte = param->index / 8;
  325. idx = param->index % 8;
  326. if (qdev->pdev->revision < 4)
  327. return -ENOSYS;
  328. if (byte >= 58)
  329. return -ENOSYS;
  330. if (qdev->rom->client_capabilities[byte] & (1 << idx))
  331. return 0;
  332. return -ENOSYS;
  333. }
  334. static int qxl_alloc_surf_ioctl(struct drm_device *dev, void *data,
  335. struct drm_file *file)
  336. {
  337. struct qxl_device *qdev = dev->dev_private;
  338. struct drm_qxl_alloc_surf *param = data;
  339. struct qxl_bo *qobj;
  340. int handle;
  341. int ret;
  342. int size, actual_stride;
  343. struct qxl_surface surf;
  344. /* work out size allocate bo with handle */
  345. actual_stride = param->stride < 0 ? -param->stride : param->stride;
  346. size = actual_stride * param->height + actual_stride;
  347. surf.format = param->format;
  348. surf.width = param->width;
  349. surf.height = param->height;
  350. surf.stride = param->stride;
  351. surf.data = 0;
  352. ret = qxl_gem_object_create_with_handle(qdev, file,
  353. QXL_GEM_DOMAIN_SURFACE,
  354. size,
  355. &surf,
  356. &qobj, &handle);
  357. if (ret) {
  358. DRM_ERROR("%s: failed to create gem ret=%d\n",
  359. __func__, ret);
  360. return -ENOMEM;
  361. } else
  362. param->handle = handle;
  363. return ret;
  364. }
  365. const struct drm_ioctl_desc qxl_ioctls[] = {
  366. DRM_IOCTL_DEF_DRV(QXL_ALLOC, qxl_alloc_ioctl, DRM_AUTH),
  367. DRM_IOCTL_DEF_DRV(QXL_MAP, qxl_map_ioctl, DRM_AUTH),
  368. DRM_IOCTL_DEF_DRV(QXL_EXECBUFFER, qxl_execbuffer_ioctl,
  369. DRM_AUTH),
  370. DRM_IOCTL_DEF_DRV(QXL_UPDATE_AREA, qxl_update_area_ioctl,
  371. DRM_AUTH),
  372. DRM_IOCTL_DEF_DRV(QXL_GETPARAM, qxl_getparam_ioctl,
  373. DRM_AUTH),
  374. DRM_IOCTL_DEF_DRV(QXL_CLIENTCAP, qxl_clientcap_ioctl,
  375. DRM_AUTH),
  376. DRM_IOCTL_DEF_DRV(QXL_ALLOC_SURF, qxl_alloc_surf_ioctl,
  377. DRM_AUTH),
  378. };
  379. int qxl_max_ioctls = ARRAY_SIZE(qxl_ioctls);