virtgpu_kms.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (C) 2015 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <linux/virtio.h>
  26. #include <linux/virtio_config.h>
  27. #include <drm/drmP.h>
  28. #include "virtgpu_drv.h"
  29. static int virtio_gpu_fbdev = 1;
  30. MODULE_PARM_DESC(fbdev, "Disable/Enable framebuffer device & console");
  31. module_param_named(fbdev, virtio_gpu_fbdev, int, 0400);
  32. static void virtio_gpu_config_changed_work_func(struct work_struct *work)
  33. {
  34. struct virtio_gpu_device *vgdev =
  35. container_of(work, struct virtio_gpu_device,
  36. config_changed_work);
  37. u32 events_read, events_clear = 0;
  38. /* read the config space */
  39. virtio_cread(vgdev->vdev, struct virtio_gpu_config,
  40. events_read, &events_read);
  41. if (events_read & VIRTIO_GPU_EVENT_DISPLAY) {
  42. virtio_gpu_cmd_get_display_info(vgdev);
  43. drm_helper_hpd_irq_event(vgdev->ddev);
  44. events_clear |= VIRTIO_GPU_EVENT_DISPLAY;
  45. }
  46. virtio_cwrite(vgdev->vdev, struct virtio_gpu_config,
  47. events_clear, &events_clear);
  48. }
  49. static void virtio_gpu_ctx_id_get(struct virtio_gpu_device *vgdev,
  50. uint32_t *resid)
  51. {
  52. int handle;
  53. idr_preload(GFP_KERNEL);
  54. spin_lock(&vgdev->ctx_id_idr_lock);
  55. handle = idr_alloc(&vgdev->ctx_id_idr, NULL, 1, 0, 0);
  56. spin_unlock(&vgdev->ctx_id_idr_lock);
  57. idr_preload_end();
  58. *resid = handle;
  59. }
  60. static void virtio_gpu_ctx_id_put(struct virtio_gpu_device *vgdev, uint32_t id)
  61. {
  62. spin_lock(&vgdev->ctx_id_idr_lock);
  63. idr_remove(&vgdev->ctx_id_idr, id);
  64. spin_unlock(&vgdev->ctx_id_idr_lock);
  65. }
  66. static void virtio_gpu_context_create(struct virtio_gpu_device *vgdev,
  67. uint32_t nlen, const char *name,
  68. uint32_t *ctx_id)
  69. {
  70. virtio_gpu_ctx_id_get(vgdev, ctx_id);
  71. virtio_gpu_cmd_context_create(vgdev, *ctx_id, nlen, name);
  72. }
  73. static void virtio_gpu_context_destroy(struct virtio_gpu_device *vgdev,
  74. uint32_t ctx_id)
  75. {
  76. virtio_gpu_cmd_context_destroy(vgdev, ctx_id);
  77. virtio_gpu_ctx_id_put(vgdev, ctx_id);
  78. }
  79. static void virtio_gpu_init_vq(struct virtio_gpu_queue *vgvq,
  80. void (*work_func)(struct work_struct *work))
  81. {
  82. spin_lock_init(&vgvq->qlock);
  83. init_waitqueue_head(&vgvq->ack_queue);
  84. INIT_WORK(&vgvq->dequeue_work, work_func);
  85. }
  86. static void virtio_gpu_get_capsets(struct virtio_gpu_device *vgdev,
  87. int num_capsets)
  88. {
  89. int i, ret;
  90. vgdev->capsets = kcalloc(num_capsets,
  91. sizeof(struct virtio_gpu_drv_capset),
  92. GFP_KERNEL);
  93. if (!vgdev->capsets) {
  94. DRM_ERROR("failed to allocate cap sets\n");
  95. return;
  96. }
  97. for (i = 0; i < num_capsets; i++) {
  98. virtio_gpu_cmd_get_capset_info(vgdev, i);
  99. ret = wait_event_timeout(vgdev->resp_wq,
  100. vgdev->capsets[i].id > 0, 5 * HZ);
  101. if (ret == 0) {
  102. DRM_ERROR("timed out waiting for cap set %d\n", i);
  103. kfree(vgdev->capsets);
  104. vgdev->capsets = NULL;
  105. return;
  106. }
  107. DRM_INFO("cap set %d: id %d, max-version %d, max-size %d\n",
  108. i, vgdev->capsets[i].id,
  109. vgdev->capsets[i].max_version,
  110. vgdev->capsets[i].max_size);
  111. }
  112. vgdev->num_capsets = num_capsets;
  113. }
  114. int virtio_gpu_driver_load(struct drm_device *dev, unsigned long flags)
  115. {
  116. static vq_callback_t *callbacks[] = {
  117. virtio_gpu_ctrl_ack, virtio_gpu_cursor_ack
  118. };
  119. static const char * const names[] = { "control", "cursor" };
  120. struct virtio_gpu_device *vgdev;
  121. /* this will expand later */
  122. struct virtqueue *vqs[2];
  123. u32 num_scanouts, num_capsets;
  124. int ret;
  125. if (!virtio_has_feature(dev->virtdev, VIRTIO_F_VERSION_1))
  126. return -ENODEV;
  127. vgdev = kzalloc(sizeof(struct virtio_gpu_device), GFP_KERNEL);
  128. if (!vgdev)
  129. return -ENOMEM;
  130. vgdev->ddev = dev;
  131. dev->dev_private = vgdev;
  132. vgdev->vdev = dev->virtdev;
  133. vgdev->dev = dev->dev;
  134. spin_lock_init(&vgdev->display_info_lock);
  135. spin_lock_init(&vgdev->ctx_id_idr_lock);
  136. idr_init(&vgdev->ctx_id_idr);
  137. spin_lock_init(&vgdev->resource_idr_lock);
  138. idr_init(&vgdev->resource_idr);
  139. init_waitqueue_head(&vgdev->resp_wq);
  140. virtio_gpu_init_vq(&vgdev->ctrlq, virtio_gpu_dequeue_ctrl_func);
  141. virtio_gpu_init_vq(&vgdev->cursorq, virtio_gpu_dequeue_cursor_func);
  142. vgdev->fence_drv.context = dma_fence_context_alloc(1);
  143. spin_lock_init(&vgdev->fence_drv.lock);
  144. INIT_LIST_HEAD(&vgdev->fence_drv.fences);
  145. INIT_LIST_HEAD(&vgdev->cap_cache);
  146. INIT_WORK(&vgdev->config_changed_work,
  147. virtio_gpu_config_changed_work_func);
  148. #ifdef __LITTLE_ENDIAN
  149. if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_VIRGL))
  150. vgdev->has_virgl_3d = true;
  151. DRM_INFO("virgl 3d acceleration %s\n",
  152. vgdev->has_virgl_3d ? "enabled" : "not supported by host");
  153. #else
  154. DRM_INFO("virgl 3d acceleration not supported by guest\n");
  155. #endif
  156. ret = virtio_find_vqs(vgdev->vdev, 2, vqs, callbacks, names, NULL);
  157. if (ret) {
  158. DRM_ERROR("failed to find virt queues\n");
  159. goto err_vqs;
  160. }
  161. vgdev->ctrlq.vq = vqs[0];
  162. vgdev->cursorq.vq = vqs[1];
  163. ret = virtio_gpu_alloc_vbufs(vgdev);
  164. if (ret) {
  165. DRM_ERROR("failed to alloc vbufs\n");
  166. goto err_vbufs;
  167. }
  168. ret = virtio_gpu_ttm_init(vgdev);
  169. if (ret) {
  170. DRM_ERROR("failed to init ttm %d\n", ret);
  171. goto err_ttm;
  172. }
  173. /* get display info */
  174. virtio_cread(vgdev->vdev, struct virtio_gpu_config,
  175. num_scanouts, &num_scanouts);
  176. vgdev->num_scanouts = min_t(uint32_t, num_scanouts,
  177. VIRTIO_GPU_MAX_SCANOUTS);
  178. if (!vgdev->num_scanouts) {
  179. DRM_ERROR("num_scanouts is zero\n");
  180. ret = -EINVAL;
  181. goto err_scanouts;
  182. }
  183. DRM_INFO("number of scanouts: %d\n", num_scanouts);
  184. virtio_cread(vgdev->vdev, struct virtio_gpu_config,
  185. num_capsets, &num_capsets);
  186. DRM_INFO("number of cap sets: %d\n", num_capsets);
  187. ret = virtio_gpu_modeset_init(vgdev);
  188. if (ret)
  189. goto err_modeset;
  190. virtio_device_ready(vgdev->vdev);
  191. vgdev->vqs_ready = true;
  192. if (num_capsets)
  193. virtio_gpu_get_capsets(vgdev, num_capsets);
  194. virtio_gpu_cmd_get_display_info(vgdev);
  195. wait_event_timeout(vgdev->resp_wq, !vgdev->display_info_pending,
  196. 5 * HZ);
  197. if (virtio_gpu_fbdev)
  198. virtio_gpu_fbdev_init(vgdev);
  199. return 0;
  200. err_modeset:
  201. err_scanouts:
  202. virtio_gpu_ttm_fini(vgdev);
  203. err_ttm:
  204. virtio_gpu_free_vbufs(vgdev);
  205. err_vbufs:
  206. vgdev->vdev->config->del_vqs(vgdev->vdev);
  207. err_vqs:
  208. kfree(vgdev);
  209. return ret;
  210. }
  211. static void virtio_gpu_cleanup_cap_cache(struct virtio_gpu_device *vgdev)
  212. {
  213. struct virtio_gpu_drv_cap_cache *cache_ent, *tmp;
  214. list_for_each_entry_safe(cache_ent, tmp, &vgdev->cap_cache, head) {
  215. kfree(cache_ent->caps_cache);
  216. kfree(cache_ent);
  217. }
  218. }
  219. void virtio_gpu_driver_unload(struct drm_device *dev)
  220. {
  221. struct virtio_gpu_device *vgdev = dev->dev_private;
  222. vgdev->vqs_ready = false;
  223. flush_work(&vgdev->ctrlq.dequeue_work);
  224. flush_work(&vgdev->cursorq.dequeue_work);
  225. flush_work(&vgdev->config_changed_work);
  226. vgdev->vdev->config->del_vqs(vgdev->vdev);
  227. virtio_gpu_modeset_fini(vgdev);
  228. virtio_gpu_ttm_fini(vgdev);
  229. virtio_gpu_free_vbufs(vgdev);
  230. virtio_gpu_cleanup_cap_cache(vgdev);
  231. kfree(vgdev->capsets);
  232. kfree(vgdev);
  233. }
  234. int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file)
  235. {
  236. struct virtio_gpu_device *vgdev = dev->dev_private;
  237. struct virtio_gpu_fpriv *vfpriv;
  238. uint32_t id;
  239. char dbgname[64], tmpname[TASK_COMM_LEN];
  240. /* can't create contexts without 3d renderer */
  241. if (!vgdev->has_virgl_3d)
  242. return 0;
  243. get_task_comm(tmpname, current);
  244. snprintf(dbgname, sizeof(dbgname), "%s", tmpname);
  245. dbgname[63] = 0;
  246. /* allocate a virt GPU context for this opener */
  247. vfpriv = kzalloc(sizeof(*vfpriv), GFP_KERNEL);
  248. if (!vfpriv)
  249. return -ENOMEM;
  250. virtio_gpu_context_create(vgdev, strlen(dbgname), dbgname, &id);
  251. vfpriv->ctx_id = id;
  252. file->driver_priv = vfpriv;
  253. return 0;
  254. }
  255. void virtio_gpu_driver_postclose(struct drm_device *dev, struct drm_file *file)
  256. {
  257. struct virtio_gpu_device *vgdev = dev->dev_private;
  258. struct virtio_gpu_fpriv *vfpriv;
  259. if (!vgdev->has_virgl_3d)
  260. return;
  261. vfpriv = file->driver_priv;
  262. virtio_gpu_context_destroy(vgdev, vfpriv->ctx_id);
  263. kfree(vfpriv);
  264. file->driver_priv = NULL;
  265. }