virtgpu_display.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Copyright (C) 2015 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Authors:
  6. * Dave Airlie
  7. * Alon Levy
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. * OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #include "virtgpu_drv.h"
  28. #include <drm/drm_crtc_helper.h>
  29. #include <drm/drm_atomic_helper.h>
  30. #define XRES_MIN 32
  31. #define YRES_MIN 32
  32. #define XRES_DEF 1024
  33. #define YRES_DEF 768
  34. #define XRES_MAX 8192
  35. #define YRES_MAX 8192
  36. static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
  37. .set_config = drm_atomic_helper_set_config,
  38. .destroy = drm_crtc_cleanup,
  39. .page_flip = drm_atomic_helper_page_flip,
  40. .reset = drm_atomic_helper_crtc_reset,
  41. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  42. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  43. };
  44. static void virtio_gpu_user_framebuffer_destroy(struct drm_framebuffer *fb)
  45. {
  46. struct virtio_gpu_framebuffer *virtio_gpu_fb
  47. = to_virtio_gpu_framebuffer(fb);
  48. drm_gem_object_put_unlocked(virtio_gpu_fb->obj);
  49. drm_framebuffer_cleanup(fb);
  50. kfree(virtio_gpu_fb);
  51. }
  52. static int
  53. virtio_gpu_framebuffer_surface_dirty(struct drm_framebuffer *fb,
  54. struct drm_file *file_priv,
  55. unsigned int flags, unsigned int color,
  56. struct drm_clip_rect *clips,
  57. unsigned int num_clips)
  58. {
  59. struct virtio_gpu_framebuffer *virtio_gpu_fb
  60. = to_virtio_gpu_framebuffer(fb);
  61. return virtio_gpu_surface_dirty(virtio_gpu_fb, clips, num_clips);
  62. }
  63. static int
  64. virtio_gpu_framebuffer_create_handle(struct drm_framebuffer *fb,
  65. struct drm_file *file_priv,
  66. unsigned int *handle)
  67. {
  68. struct virtio_gpu_framebuffer *virtio_gpu_fb =
  69. to_virtio_gpu_framebuffer(fb);
  70. return drm_gem_handle_create(file_priv, virtio_gpu_fb->obj, handle);
  71. }
  72. static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
  73. .create_handle = virtio_gpu_framebuffer_create_handle,
  74. .destroy = virtio_gpu_user_framebuffer_destroy,
  75. .dirty = virtio_gpu_framebuffer_surface_dirty,
  76. };
  77. int
  78. virtio_gpu_framebuffer_init(struct drm_device *dev,
  79. struct virtio_gpu_framebuffer *vgfb,
  80. const struct drm_mode_fb_cmd2 *mode_cmd,
  81. struct drm_gem_object *obj)
  82. {
  83. int ret;
  84. struct virtio_gpu_object *bo;
  85. vgfb->obj = obj;
  86. bo = gem_to_virtio_gpu_obj(obj);
  87. drm_helper_mode_fill_fb_struct(dev, &vgfb->base, mode_cmd);
  88. ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs);
  89. if (ret) {
  90. vgfb->obj = NULL;
  91. return ret;
  92. }
  93. spin_lock_init(&vgfb->dirty_lock);
  94. vgfb->x1 = vgfb->y1 = INT_MAX;
  95. vgfb->x2 = vgfb->y2 = 0;
  96. return 0;
  97. }
  98. static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
  99. {
  100. struct drm_device *dev = crtc->dev;
  101. struct virtio_gpu_device *vgdev = dev->dev_private;
  102. struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
  103. virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
  104. crtc->mode.hdisplay,
  105. crtc->mode.vdisplay, 0, 0);
  106. }
  107. static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
  108. struct drm_crtc_state *old_state)
  109. {
  110. }
  111. static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,
  112. struct drm_crtc_state *old_state)
  113. {
  114. struct drm_device *dev = crtc->dev;
  115. struct virtio_gpu_device *vgdev = dev->dev_private;
  116. struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
  117. virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
  118. }
  119. static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
  120. struct drm_crtc_state *state)
  121. {
  122. return 0;
  123. }
  124. static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
  125. struct drm_crtc_state *old_state)
  126. {
  127. unsigned long flags;
  128. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  129. if (crtc->state->event)
  130. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  131. crtc->state->event = NULL;
  132. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  133. }
  134. static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
  135. .mode_set_nofb = virtio_gpu_crtc_mode_set_nofb,
  136. .atomic_check = virtio_gpu_crtc_atomic_check,
  137. .atomic_flush = virtio_gpu_crtc_atomic_flush,
  138. .atomic_enable = virtio_gpu_crtc_atomic_enable,
  139. .atomic_disable = virtio_gpu_crtc_atomic_disable,
  140. };
  141. static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder,
  142. struct drm_display_mode *mode,
  143. struct drm_display_mode *adjusted_mode)
  144. {
  145. }
  146. static void virtio_gpu_enc_enable(struct drm_encoder *encoder)
  147. {
  148. }
  149. static void virtio_gpu_enc_disable(struct drm_encoder *encoder)
  150. {
  151. }
  152. static int virtio_gpu_conn_get_modes(struct drm_connector *connector)
  153. {
  154. struct virtio_gpu_output *output =
  155. drm_connector_to_virtio_gpu_output(connector);
  156. struct drm_display_mode *mode = NULL;
  157. int count, width, height;
  158. width = le32_to_cpu(output->info.r.width);
  159. height = le32_to_cpu(output->info.r.height);
  160. count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
  161. if (width == 0 || height == 0) {
  162. width = XRES_DEF;
  163. height = YRES_DEF;
  164. drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
  165. } else {
  166. DRM_DEBUG("add mode: %dx%d\n", width, height);
  167. mode = drm_cvt_mode(connector->dev, width, height, 60,
  168. false, false, false);
  169. mode->type |= DRM_MODE_TYPE_PREFERRED;
  170. drm_mode_probed_add(connector, mode);
  171. count++;
  172. }
  173. return count;
  174. }
  175. static enum drm_mode_status virtio_gpu_conn_mode_valid(struct drm_connector *connector,
  176. struct drm_display_mode *mode)
  177. {
  178. struct virtio_gpu_output *output =
  179. drm_connector_to_virtio_gpu_output(connector);
  180. int width, height;
  181. width = le32_to_cpu(output->info.r.width);
  182. height = le32_to_cpu(output->info.r.height);
  183. if (!(mode->type & DRM_MODE_TYPE_PREFERRED))
  184. return MODE_OK;
  185. if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF)
  186. return MODE_OK;
  187. if (mode->hdisplay <= width && mode->hdisplay >= width - 16 &&
  188. mode->vdisplay <= height && mode->vdisplay >= height - 16)
  189. return MODE_OK;
  190. DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay);
  191. return MODE_BAD;
  192. }
  193. static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = {
  194. .mode_set = virtio_gpu_enc_mode_set,
  195. .enable = virtio_gpu_enc_enable,
  196. .disable = virtio_gpu_enc_disable,
  197. };
  198. static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = {
  199. .get_modes = virtio_gpu_conn_get_modes,
  200. .mode_valid = virtio_gpu_conn_mode_valid,
  201. };
  202. static enum drm_connector_status virtio_gpu_conn_detect(
  203. struct drm_connector *connector,
  204. bool force)
  205. {
  206. struct virtio_gpu_output *output =
  207. drm_connector_to_virtio_gpu_output(connector);
  208. if (output->info.enabled)
  209. return connector_status_connected;
  210. else
  211. return connector_status_disconnected;
  212. }
  213. static void virtio_gpu_conn_destroy(struct drm_connector *connector)
  214. {
  215. struct virtio_gpu_output *virtio_gpu_output =
  216. drm_connector_to_virtio_gpu_output(connector);
  217. drm_connector_unregister(connector);
  218. drm_connector_cleanup(connector);
  219. kfree(virtio_gpu_output);
  220. }
  221. static const struct drm_connector_funcs virtio_gpu_connector_funcs = {
  222. .detect = virtio_gpu_conn_detect,
  223. .fill_modes = drm_helper_probe_single_connector_modes,
  224. .destroy = virtio_gpu_conn_destroy,
  225. .reset = drm_atomic_helper_connector_reset,
  226. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  227. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  228. };
  229. static const struct drm_encoder_funcs virtio_gpu_enc_funcs = {
  230. .destroy = drm_encoder_cleanup,
  231. };
  232. static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index)
  233. {
  234. struct drm_device *dev = vgdev->ddev;
  235. struct virtio_gpu_output *output = vgdev->outputs + index;
  236. struct drm_connector *connector = &output->conn;
  237. struct drm_encoder *encoder = &output->enc;
  238. struct drm_crtc *crtc = &output->crtc;
  239. struct drm_plane *primary, *cursor;
  240. output->index = index;
  241. if (index == 0) {
  242. output->info.enabled = cpu_to_le32(true);
  243. output->info.r.width = cpu_to_le32(XRES_DEF);
  244. output->info.r.height = cpu_to_le32(YRES_DEF);
  245. }
  246. primary = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_PRIMARY, index);
  247. if (IS_ERR(primary))
  248. return PTR_ERR(primary);
  249. cursor = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_CURSOR, index);
  250. if (IS_ERR(cursor))
  251. return PTR_ERR(cursor);
  252. drm_crtc_init_with_planes(dev, crtc, primary, cursor,
  253. &virtio_gpu_crtc_funcs, NULL);
  254. drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs);
  255. primary->crtc = crtc;
  256. cursor->crtc = crtc;
  257. drm_connector_init(dev, connector, &virtio_gpu_connector_funcs,
  258. DRM_MODE_CONNECTOR_VIRTUAL);
  259. drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs);
  260. drm_encoder_init(dev, encoder, &virtio_gpu_enc_funcs,
  261. DRM_MODE_ENCODER_VIRTUAL, NULL);
  262. drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs);
  263. encoder->possible_crtcs = 1 << index;
  264. drm_mode_connector_attach_encoder(connector, encoder);
  265. drm_connector_register(connector);
  266. return 0;
  267. }
  268. static struct drm_framebuffer *
  269. virtio_gpu_user_framebuffer_create(struct drm_device *dev,
  270. struct drm_file *file_priv,
  271. const struct drm_mode_fb_cmd2 *mode_cmd)
  272. {
  273. struct drm_gem_object *obj = NULL;
  274. struct virtio_gpu_framebuffer *virtio_gpu_fb;
  275. int ret;
  276. /* lookup object associated with res handle */
  277. obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
  278. if (!obj)
  279. return ERR_PTR(-EINVAL);
  280. virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL);
  281. if (virtio_gpu_fb == NULL)
  282. return ERR_PTR(-ENOMEM);
  283. ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj);
  284. if (ret) {
  285. kfree(virtio_gpu_fb);
  286. drm_gem_object_put_unlocked(obj);
  287. return NULL;
  288. }
  289. return &virtio_gpu_fb->base;
  290. }
  291. static void vgdev_atomic_commit_tail(struct drm_atomic_state *state)
  292. {
  293. struct drm_device *dev = state->dev;
  294. drm_atomic_helper_commit_modeset_disables(dev, state);
  295. drm_atomic_helper_commit_modeset_enables(dev, state);
  296. drm_atomic_helper_commit_planes(dev, state, 0);
  297. drm_atomic_helper_commit_hw_done(state);
  298. drm_atomic_helper_wait_for_vblanks(dev, state);
  299. drm_atomic_helper_cleanup_planes(dev, state);
  300. }
  301. static const struct drm_mode_config_helper_funcs virtio_mode_config_helpers = {
  302. .atomic_commit_tail = vgdev_atomic_commit_tail,
  303. };
  304. static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = {
  305. .fb_create = virtio_gpu_user_framebuffer_create,
  306. .atomic_check = drm_atomic_helper_check,
  307. .atomic_commit = drm_atomic_helper_commit,
  308. };
  309. int virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
  310. {
  311. int i;
  312. drm_mode_config_init(vgdev->ddev);
  313. vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs;
  314. vgdev->ddev->mode_config.helper_private = &virtio_mode_config_helpers;
  315. /* modes will be validated against the framebuffer size */
  316. vgdev->ddev->mode_config.min_width = XRES_MIN;
  317. vgdev->ddev->mode_config.min_height = YRES_MIN;
  318. vgdev->ddev->mode_config.max_width = XRES_MAX;
  319. vgdev->ddev->mode_config.max_height = YRES_MAX;
  320. for (i = 0 ; i < vgdev->num_scanouts; ++i)
  321. vgdev_output_init(vgdev, i);
  322. drm_mode_config_reset(vgdev->ddev);
  323. return 0;
  324. }
  325. void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev)
  326. {
  327. virtio_gpu_fbdev_fini(vgdev);
  328. drm_mode_config_cleanup(vgdev->ddev);
  329. }