virtgpu_kms.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 <linux/virtio_ring.h>
  28. #include <drm/drm_file.h>
  29. #include <drm/drm_managed.h>
  30. #include "virtgpu_drv.h"
  31. static void virtio_gpu_config_changed_work_func(struct work_struct *work)
  32. {
  33. struct virtio_gpu_device *vgdev =
  34. container_of(work, struct virtio_gpu_device,
  35. config_changed_work);
  36. u32 events_read, events_clear = 0;
  37. /* read the config space */
  38. virtio_cread_le(vgdev->vdev, struct virtio_gpu_config,
  39. events_read, &events_read);
  40. if (events_read & VIRTIO_GPU_EVENT_DISPLAY) {
  41. if (vgdev->num_scanouts) {
  42. if (vgdev->has_edid)
  43. virtio_gpu_cmd_get_edids(vgdev);
  44. virtio_gpu_cmd_get_display_info(vgdev);
  45. virtio_gpu_notify(vgdev);
  46. drm_helper_hpd_irq_event(vgdev->ddev);
  47. }
  48. events_clear |= VIRTIO_GPU_EVENT_DISPLAY;
  49. }
  50. virtio_cwrite_le(vgdev->vdev, struct virtio_gpu_config,
  51. events_clear, &events_clear);
  52. }
  53. static void virtio_gpu_init_vq(struct virtio_gpu_queue *vgvq,
  54. void (*work_func)(struct work_struct *work))
  55. {
  56. spin_lock_init(&vgvq->qlock);
  57. init_waitqueue_head(&vgvq->ack_queue);
  58. INIT_WORK(&vgvq->dequeue_work, work_func);
  59. }
  60. static void virtio_gpu_get_capsets(struct virtio_gpu_device *vgdev,
  61. int num_capsets)
  62. {
  63. int i, ret;
  64. bool invalid_capset_id = false;
  65. struct drm_device *drm = vgdev->ddev;
  66. vgdev->capsets = drmm_kcalloc(drm, num_capsets,
  67. sizeof(struct virtio_gpu_drv_capset),
  68. GFP_KERNEL);
  69. if (!vgdev->capsets) {
  70. DRM_ERROR("failed to allocate cap sets\n");
  71. return;
  72. }
  73. for (i = 0; i < num_capsets; i++) {
  74. virtio_gpu_cmd_get_capset_info(vgdev, i);
  75. virtio_gpu_notify(vgdev);
  76. ret = wait_event_timeout(vgdev->resp_wq,
  77. vgdev->capsets[i].id > 0, 5 * HZ);
  78. /*
  79. * Capability ids are defined in the virtio-gpu spec and are
  80. * between 1 to 63, inclusive.
  81. */
  82. if (!vgdev->capsets[i].id ||
  83. vgdev->capsets[i].id > MAX_CAPSET_ID)
  84. invalid_capset_id = true;
  85. if (ret == 0)
  86. DRM_ERROR("timed out waiting for cap set %d\n", i);
  87. else if (invalid_capset_id)
  88. DRM_ERROR("invalid capset id %u", vgdev->capsets[i].id);
  89. if (ret == 0 || invalid_capset_id) {
  90. spin_lock(&vgdev->display_info_lock);
  91. drmm_kfree(drm, vgdev->capsets);
  92. vgdev->capsets = NULL;
  93. spin_unlock(&vgdev->display_info_lock);
  94. return;
  95. }
  96. vgdev->capset_id_mask |= 1 << vgdev->capsets[i].id;
  97. DRM_INFO("cap set %d: id %d, max-version %d, max-size %d\n",
  98. i, vgdev->capsets[i].id,
  99. vgdev->capsets[i].max_version,
  100. vgdev->capsets[i].max_size);
  101. }
  102. vgdev->num_capsets = num_capsets;
  103. }
  104. int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
  105. {
  106. struct virtqueue_info vqs_info[] = {
  107. { "control", virtio_gpu_ctrl_ack },
  108. { "cursor", virtio_gpu_cursor_ack },
  109. };
  110. struct virtio_gpu_device *vgdev;
  111. /* this will expand later */
  112. struct virtqueue *vqs[2];
  113. u32 num_scanouts, num_capsets;
  114. int ret = 0;
  115. if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  116. return -ENODEV;
  117. vgdev = drmm_kzalloc(dev, sizeof(struct virtio_gpu_device), GFP_KERNEL);
  118. if (!vgdev)
  119. return -ENOMEM;
  120. vgdev->ddev = dev;
  121. dev->dev_private = vgdev;
  122. vgdev->vdev = vdev;
  123. spin_lock_init(&vgdev->display_info_lock);
  124. spin_lock_init(&vgdev->resource_export_lock);
  125. spin_lock_init(&vgdev->host_visible_lock);
  126. ida_init(&vgdev->ctx_id_ida);
  127. ida_init(&vgdev->resource_ida);
  128. init_waitqueue_head(&vgdev->resp_wq);
  129. virtio_gpu_init_vq(&vgdev->ctrlq, virtio_gpu_dequeue_ctrl_func);
  130. virtio_gpu_init_vq(&vgdev->cursorq, virtio_gpu_dequeue_cursor_func);
  131. vgdev->fence_drv.context = dma_fence_context_alloc(1);
  132. spin_lock_init(&vgdev->fence_drv.lock);
  133. INIT_LIST_HEAD(&vgdev->fence_drv.fences);
  134. INIT_LIST_HEAD(&vgdev->cap_cache);
  135. INIT_WORK(&vgdev->config_changed_work,
  136. virtio_gpu_config_changed_work_func);
  137. INIT_WORK(&vgdev->obj_free_work,
  138. virtio_gpu_array_put_free_work);
  139. INIT_LIST_HEAD(&vgdev->obj_free_list);
  140. spin_lock_init(&vgdev->obj_free_lock);
  141. #ifdef __LITTLE_ENDIAN
  142. if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_VIRGL))
  143. vgdev->has_virgl_3d = true;
  144. #endif
  145. if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_EDID)) {
  146. vgdev->has_edid = true;
  147. }
  148. if (virtio_has_feature(vgdev->vdev, VIRTIO_RING_F_INDIRECT_DESC)) {
  149. vgdev->has_indirect = true;
  150. }
  151. if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_RESOURCE_UUID)) {
  152. vgdev->has_resource_assign_uuid = true;
  153. }
  154. if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_RESOURCE_BLOB)) {
  155. vgdev->has_resource_blob = true;
  156. }
  157. if (virtio_get_shm_region(vgdev->vdev, &vgdev->host_visible_region,
  158. VIRTIO_GPU_SHM_ID_HOST_VISIBLE)) {
  159. if (!devm_request_mem_region(&vgdev->vdev->dev,
  160. vgdev->host_visible_region.addr,
  161. vgdev->host_visible_region.len,
  162. dev_name(&vgdev->vdev->dev))) {
  163. DRM_ERROR("Could not reserve host visible region\n");
  164. ret = -EBUSY;
  165. goto err_vqs;
  166. }
  167. DRM_INFO("Host memory window: 0x%lx +0x%lx\n",
  168. (unsigned long)vgdev->host_visible_region.addr,
  169. (unsigned long)vgdev->host_visible_region.len);
  170. vgdev->has_host_visible = true;
  171. drm_mm_init(&vgdev->host_visible_mm,
  172. (unsigned long)vgdev->host_visible_region.addr,
  173. (unsigned long)vgdev->host_visible_region.len);
  174. }
  175. if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_CONTEXT_INIT)) {
  176. vgdev->has_context_init = true;
  177. }
  178. DRM_INFO("features: %cvirgl %cedid %cresource_blob %chost_visible",
  179. vgdev->has_virgl_3d ? '+' : '-',
  180. vgdev->has_edid ? '+' : '-',
  181. vgdev->has_resource_blob ? '+' : '-',
  182. vgdev->has_host_visible ? '+' : '-');
  183. DRM_INFO("features: %ccontext_init\n",
  184. vgdev->has_context_init ? '+' : '-');
  185. ret = virtio_find_vqs(vgdev->vdev, 2, vqs, vqs_info, NULL);
  186. if (ret) {
  187. DRM_ERROR("failed to find virt queues\n");
  188. goto err_vqs;
  189. }
  190. vgdev->ctrlq.vq = vqs[0];
  191. vgdev->cursorq.vq = vqs[1];
  192. ret = virtio_gpu_alloc_vbufs(vgdev);
  193. if (ret) {
  194. DRM_ERROR("failed to alloc vbufs\n");
  195. goto err_vbufs;
  196. }
  197. /* get display info */
  198. virtio_cread_le(vgdev->vdev, struct virtio_gpu_config,
  199. num_scanouts, &num_scanouts);
  200. vgdev->num_scanouts = min_t(uint32_t, num_scanouts,
  201. VIRTIO_GPU_MAX_SCANOUTS);
  202. if (!IS_ENABLED(CONFIG_DRM_VIRTIO_GPU_KMS) || !vgdev->num_scanouts) {
  203. DRM_INFO("KMS disabled\n");
  204. vgdev->num_scanouts = 0;
  205. vgdev->has_edid = false;
  206. dev->driver_features &= ~(DRIVER_MODESET | DRIVER_ATOMIC);
  207. } else {
  208. DRM_INFO("number of scanouts: %d\n", num_scanouts);
  209. }
  210. virtio_cread_le(vgdev->vdev, struct virtio_gpu_config,
  211. num_capsets, &num_capsets);
  212. DRM_INFO("number of cap sets: %d\n", num_capsets);
  213. ret = virtio_gpu_modeset_init(vgdev);
  214. if (ret) {
  215. DRM_ERROR("modeset init failed\n");
  216. goto err_scanouts;
  217. }
  218. virtio_device_ready(vgdev->vdev);
  219. if (num_capsets)
  220. virtio_gpu_get_capsets(vgdev, num_capsets);
  221. if (vgdev->num_scanouts) {
  222. if (vgdev->has_edid)
  223. virtio_gpu_cmd_get_edids(vgdev);
  224. virtio_gpu_cmd_get_display_info(vgdev);
  225. virtio_gpu_notify(vgdev);
  226. wait_event_timeout(vgdev->resp_wq, !vgdev->display_info_pending,
  227. 5 * HZ);
  228. }
  229. return 0;
  230. err_scanouts:
  231. virtio_gpu_free_vbufs(vgdev);
  232. err_vbufs:
  233. vgdev->vdev->config->del_vqs(vgdev->vdev);
  234. err_vqs:
  235. dev->dev_private = NULL;
  236. return ret;
  237. }
  238. static void virtio_gpu_cleanup_cap_cache(struct virtio_gpu_device *vgdev)
  239. {
  240. struct virtio_gpu_drv_cap_cache *cache_ent, *tmp;
  241. list_for_each_entry_safe(cache_ent, tmp, &vgdev->cap_cache, head) {
  242. kfree(cache_ent->caps_cache);
  243. kfree(cache_ent);
  244. }
  245. }
  246. void virtio_gpu_deinit(struct drm_device *dev)
  247. {
  248. struct virtio_gpu_device *vgdev = dev->dev_private;
  249. flush_work(&vgdev->obj_free_work);
  250. flush_work(&vgdev->ctrlq.dequeue_work);
  251. flush_work(&vgdev->cursorq.dequeue_work);
  252. flush_work(&vgdev->config_changed_work);
  253. virtio_reset_device(vgdev->vdev);
  254. vgdev->vdev->config->del_vqs(vgdev->vdev);
  255. }
  256. void virtio_gpu_release(struct drm_device *dev)
  257. {
  258. struct virtio_gpu_device *vgdev = dev->dev_private;
  259. if (!vgdev)
  260. return;
  261. virtio_gpu_modeset_fini(vgdev);
  262. virtio_gpu_free_vbufs(vgdev);
  263. virtio_gpu_cleanup_cap_cache(vgdev);
  264. if (vgdev->has_host_visible)
  265. drm_mm_takedown(&vgdev->host_visible_mm);
  266. }
  267. int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file)
  268. {
  269. struct virtio_gpu_device *vgdev = dev->dev_private;
  270. struct virtio_gpu_fpriv *vfpriv;
  271. int handle;
  272. /* can't create contexts without 3d renderer */
  273. if (!vgdev->has_virgl_3d)
  274. return 0;
  275. /* allocate a virt GPU context for this opener */
  276. vfpriv = kzalloc(sizeof(*vfpriv), GFP_KERNEL);
  277. if (!vfpriv)
  278. return -ENOMEM;
  279. mutex_init(&vfpriv->context_lock);
  280. handle = ida_alloc(&vgdev->ctx_id_ida, GFP_KERNEL);
  281. if (handle < 0) {
  282. kfree(vfpriv);
  283. return handle;
  284. }
  285. vfpriv->ctx_id = handle + 1;
  286. file->driver_priv = vfpriv;
  287. return 0;
  288. }
  289. void virtio_gpu_driver_postclose(struct drm_device *dev, struct drm_file *file)
  290. {
  291. struct virtio_gpu_device *vgdev = dev->dev_private;
  292. struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
  293. if (!vgdev->has_virgl_3d)
  294. return;
  295. if (vfpriv->context_created) {
  296. virtio_gpu_cmd_context_destroy(vgdev, vfpriv->ctx_id);
  297. virtio_gpu_notify(vgdev);
  298. }
  299. ida_free(&vgdev->ctx_id_ida, vfpriv->ctx_id - 1);
  300. mutex_destroy(&vfpriv->context_lock);
  301. kfree(vfpriv);
  302. file->driver_priv = NULL;
  303. }