qxl_ioctl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 struct qxl_bo *qxlhw_handle_to_bo(struct qxl_device *qdev,
  100. struct drm_file *file_priv, uint64_t handle,
  101. struct qxl_release *release)
  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 NULL;
  109. qobj = gem_to_qxl_bo(gobj);
  110. ret = qxl_release_list_add(release, qobj);
  111. if (ret)
  112. return NULL;
  113. return qobj;
  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, j, 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. break;
  142. }
  143. if (cmd->command_size > PAGE_SIZE - sizeof(union qxl_release_info))
  144. return -EINVAL;
  145. if (!access_ok(VERIFY_READ,
  146. (void *)(unsigned long)cmd->command,
  147. cmd->command_size))
  148. return -EFAULT;
  149. reloc_info = kmalloc(sizeof(struct qxl_reloc_info) * cmd->relocs_num, GFP_KERNEL);
  150. if (!reloc_info)
  151. return -ENOMEM;
  152. ret = qxl_alloc_release_reserved(qdev,
  153. sizeof(union qxl_release_info) +
  154. cmd->command_size,
  155. release_type,
  156. &release,
  157. &cmd_bo);
  158. if (ret)
  159. goto out_free_reloc;
  160. /* TODO copy slow path code from i915 */
  161. fb_cmd = qxl_bo_kmap_atomic_page(qdev, cmd_bo, (release->release_offset & PAGE_SIZE));
  162. 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);
  163. {
  164. struct qxl_drawable *draw = fb_cmd;
  165. draw->mm_time = qdev->rom->mm_clock;
  166. }
  167. qxl_bo_kunmap_atomic_page(qdev, cmd_bo, fb_cmd);
  168. if (unwritten) {
  169. DRM_ERROR("got unwritten %d\n", unwritten);
  170. ret = -EFAULT;
  171. goto out_free_release;
  172. }
  173. /* fill out reloc info structs */
  174. num_relocs = 0;
  175. for (i = 0; i < cmd->relocs_num; ++i) {
  176. struct drm_qxl_reloc reloc;
  177. if (DRM_COPY_FROM_USER(&reloc,
  178. &((struct drm_qxl_reloc *)(uintptr_t)cmd->relocs)[i],
  179. 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_info[i].type);
  187. ret = -EINVAL;
  188. goto out_free_bos;
  189. }
  190. reloc_info[i].type = reloc.reloc_type;
  191. if (reloc.dst_handle) {
  192. reloc_info[i].dst_bo = qxlhw_handle_to_bo(qdev, file_priv,
  193. reloc.dst_handle, release);
  194. if (!reloc_info[i].dst_bo) {
  195. ret = -EINVAL;
  196. reloc_info[i].src_bo = NULL;
  197. goto out_free_bos;
  198. }
  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 > 0) {
  207. reloc_info[i].src_bo =
  208. qxlhw_handle_to_bo(qdev, file_priv,
  209. reloc.src_handle, release);
  210. if (!reloc_info[i].src_bo) {
  211. if (reloc_info[i].dst_bo != cmd_bo)
  212. drm_gem_object_unreference_unlocked(&reloc_info[i].dst_bo->gem_base);
  213. ret = -EINVAL;
  214. goto out_free_bos;
  215. }
  216. reloc_info[i].src_offset = reloc.src_offset;
  217. } else {
  218. reloc_info[i].src_bo = NULL;
  219. reloc_info[i].src_offset = 0;
  220. }
  221. }
  222. /* validate all buffers */
  223. ret = qxl_release_reserve_list(release, false);
  224. if (ret)
  225. goto out_free_bos;
  226. for (i = 0; i < cmd->relocs_num; ++i) {
  227. if (reloc_info[i].type == QXL_RELOC_TYPE_BO)
  228. apply_reloc(qdev, &reloc_info[i]);
  229. else if (reloc_info[i].type == QXL_RELOC_TYPE_SURF)
  230. apply_surf_reloc(qdev, &reloc_info[i]);
  231. }
  232. ret = qxl_push_command_ring_release(qdev, release, cmd->type, true);
  233. if (ret)
  234. qxl_release_backoff_reserve_list(release);
  235. else
  236. qxl_release_fence_buffer_objects(release);
  237. out_free_bos:
  238. for (j = 0; j < num_relocs; j++) {
  239. if (reloc_info[j].dst_bo != cmd_bo)
  240. drm_gem_object_unreference_unlocked(&reloc_info[j].dst_bo->gem_base);
  241. if (reloc_info[j].src_bo && reloc_info[j].src_bo != cmd_bo)
  242. drm_gem_object_unreference_unlocked(&reloc_info[j].src_bo->gem_base);
  243. }
  244. out_free_release:
  245. if (ret)
  246. qxl_release_free(qdev, release);
  247. out_free_reloc:
  248. kfree(reloc_info);
  249. return ret;
  250. }
  251. static int qxl_execbuffer_ioctl(struct drm_device *dev, void *data,
  252. struct drm_file *file_priv)
  253. {
  254. struct qxl_device *qdev = dev->dev_private;
  255. struct drm_qxl_execbuffer *execbuffer = data;
  256. struct drm_qxl_command user_cmd;
  257. int cmd_num;
  258. int ret;
  259. for (cmd_num = 0; cmd_num < execbuffer->commands_num; ++cmd_num) {
  260. struct drm_qxl_command *commands =
  261. (struct drm_qxl_command *)(uintptr_t)execbuffer->commands;
  262. if (DRM_COPY_FROM_USER(&user_cmd, &commands[cmd_num],
  263. sizeof(user_cmd)))
  264. return -EFAULT;
  265. ret = qxl_process_single_command(qdev, &user_cmd, file_priv);
  266. if (ret)
  267. return ret;
  268. }
  269. return 0;
  270. }
  271. static int qxl_update_area_ioctl(struct drm_device *dev, void *data,
  272. struct drm_file *file)
  273. {
  274. struct qxl_device *qdev = dev->dev_private;
  275. struct drm_qxl_update_area *update_area = data;
  276. struct qxl_rect area = {.left = update_area->left,
  277. .top = update_area->top,
  278. .right = update_area->right,
  279. .bottom = update_area->bottom};
  280. int ret;
  281. struct drm_gem_object *gobj = NULL;
  282. struct qxl_bo *qobj = NULL;
  283. if (update_area->left >= update_area->right ||
  284. update_area->top >= update_area->bottom)
  285. return -EINVAL;
  286. gobj = drm_gem_object_lookup(dev, file, update_area->handle);
  287. if (gobj == NULL)
  288. return -ENOENT;
  289. qobj = gem_to_qxl_bo(gobj);
  290. ret = qxl_bo_reserve(qobj, false);
  291. if (ret)
  292. goto out;
  293. if (!qobj->pin_count) {
  294. qxl_ttm_placement_from_domain(qobj, qobj->type, false);
  295. ret = ttm_bo_validate(&qobj->tbo, &qobj->placement,
  296. true, false);
  297. if (unlikely(ret))
  298. goto out;
  299. }
  300. ret = qxl_bo_check_id(qdev, qobj);
  301. if (ret)
  302. goto out2;
  303. if (!qobj->surface_id)
  304. DRM_ERROR("got update area for surface with no id %d\n", update_area->handle);
  305. ret = qxl_io_update_area(qdev, qobj, &area);
  306. out2:
  307. qxl_bo_unreserve(qobj);
  308. out:
  309. drm_gem_object_unreference_unlocked(gobj);
  310. return ret;
  311. }
  312. static int qxl_getparam_ioctl(struct drm_device *dev, void *data,
  313. struct drm_file *file_priv)
  314. {
  315. struct qxl_device *qdev = dev->dev_private;
  316. struct drm_qxl_getparam *param = data;
  317. switch (param->param) {
  318. case QXL_PARAM_NUM_SURFACES:
  319. param->value = qdev->rom->n_surfaces;
  320. break;
  321. case QXL_PARAM_MAX_RELOCS:
  322. param->value = QXL_MAX_RES;
  323. break;
  324. default:
  325. return -EINVAL;
  326. }
  327. return 0;
  328. }
  329. static int qxl_clientcap_ioctl(struct drm_device *dev, void *data,
  330. struct drm_file *file_priv)
  331. {
  332. struct qxl_device *qdev = dev->dev_private;
  333. struct drm_qxl_clientcap *param = data;
  334. int byte, idx;
  335. byte = param->index / 8;
  336. idx = param->index % 8;
  337. if (qdev->pdev->revision < 4)
  338. return -ENOSYS;
  339. if (byte >= 58)
  340. return -ENOSYS;
  341. if (qdev->rom->client_capabilities[byte] & (1 << idx))
  342. return 0;
  343. return -ENOSYS;
  344. }
  345. static int qxl_alloc_surf_ioctl(struct drm_device *dev, void *data,
  346. struct drm_file *file)
  347. {
  348. struct qxl_device *qdev = dev->dev_private;
  349. struct drm_qxl_alloc_surf *param = data;
  350. struct qxl_bo *qobj;
  351. int handle;
  352. int ret;
  353. int size, actual_stride;
  354. struct qxl_surface surf;
  355. /* work out size allocate bo with handle */
  356. actual_stride = param->stride < 0 ? -param->stride : param->stride;
  357. size = actual_stride * param->height + actual_stride;
  358. surf.format = param->format;
  359. surf.width = param->width;
  360. surf.height = param->height;
  361. surf.stride = param->stride;
  362. surf.data = 0;
  363. ret = qxl_gem_object_create_with_handle(qdev, file,
  364. QXL_GEM_DOMAIN_SURFACE,
  365. size,
  366. &surf,
  367. &qobj, &handle);
  368. if (ret) {
  369. DRM_ERROR("%s: failed to create gem ret=%d\n",
  370. __func__, ret);
  371. return -ENOMEM;
  372. } else
  373. param->handle = handle;
  374. return ret;
  375. }
  376. const struct drm_ioctl_desc qxl_ioctls[] = {
  377. DRM_IOCTL_DEF_DRV(QXL_ALLOC, qxl_alloc_ioctl, DRM_AUTH|DRM_UNLOCKED),
  378. DRM_IOCTL_DEF_DRV(QXL_MAP, qxl_map_ioctl, DRM_AUTH|DRM_UNLOCKED),
  379. DRM_IOCTL_DEF_DRV(QXL_EXECBUFFER, qxl_execbuffer_ioctl,
  380. DRM_AUTH|DRM_UNLOCKED),
  381. DRM_IOCTL_DEF_DRV(QXL_UPDATE_AREA, qxl_update_area_ioctl,
  382. DRM_AUTH|DRM_UNLOCKED),
  383. DRM_IOCTL_DEF_DRV(QXL_GETPARAM, qxl_getparam_ioctl,
  384. DRM_AUTH|DRM_UNLOCKED),
  385. DRM_IOCTL_DEF_DRV(QXL_CLIENTCAP, qxl_clientcap_ioctl,
  386. DRM_AUTH|DRM_UNLOCKED),
  387. DRM_IOCTL_DEF_DRV(QXL_ALLOC_SURF, qxl_alloc_surf_ioctl,
  388. DRM_AUTH|DRM_UNLOCKED),
  389. };
  390. int qxl_max_ioctls = DRM_ARRAY_SIZE(qxl_ioctls);