hyperv_drm_modeset.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2021 Microsoft
  4. */
  5. #include <linux/hyperv.h>
  6. #include <drm/drm_damage_helper.h>
  7. #include <drm/drm_drv.h>
  8. #include <drm/drm_edid.h>
  9. #include <drm/drm_format_helper.h>
  10. #include <drm/drm_fourcc.h>
  11. #include <drm/drm_framebuffer.h>
  12. #include <drm/drm_gem_atomic_helper.h>
  13. #include <drm/drm_gem_framebuffer_helper.h>
  14. #include <drm/drm_gem_shmem_helper.h>
  15. #include <drm/drm_probe_helper.h>
  16. #include <drm/drm_simple_kms_helper.h>
  17. #include "hyperv_drm.h"
  18. static int hyperv_blit_to_vram_rect(struct drm_framebuffer *fb,
  19. const struct iosys_map *vmap,
  20. struct drm_rect *rect)
  21. {
  22. struct hyperv_drm_device *hv = to_hv(fb->dev);
  23. struct iosys_map dst = IOSYS_MAP_INIT_VADDR_IOMEM(hv->vram);
  24. int idx;
  25. if (!drm_dev_enter(&hv->dev, &idx))
  26. return -ENODEV;
  27. iosys_map_incr(&dst, drm_fb_clip_offset(fb->pitches[0], fb->format, rect));
  28. drm_fb_memcpy(&dst, fb->pitches, vmap, fb, rect);
  29. drm_dev_exit(idx);
  30. return 0;
  31. }
  32. static int hyperv_blit_to_vram_fullscreen(struct drm_framebuffer *fb,
  33. const struct iosys_map *map)
  34. {
  35. struct drm_rect fullscreen = {
  36. .x1 = 0,
  37. .x2 = fb->width,
  38. .y1 = 0,
  39. .y2 = fb->height,
  40. };
  41. return hyperv_blit_to_vram_rect(fb, map, &fullscreen);
  42. }
  43. static int hyperv_connector_get_modes(struct drm_connector *connector)
  44. {
  45. struct hyperv_drm_device *hv = to_hv(connector->dev);
  46. int count;
  47. count = drm_add_modes_noedid(connector,
  48. connector->dev->mode_config.max_width,
  49. connector->dev->mode_config.max_height);
  50. drm_set_preferred_mode(connector, hv->preferred_width,
  51. hv->preferred_height);
  52. return count;
  53. }
  54. static const struct drm_connector_helper_funcs hyperv_connector_helper_funcs = {
  55. .get_modes = hyperv_connector_get_modes,
  56. };
  57. static const struct drm_connector_funcs hyperv_connector_funcs = {
  58. .fill_modes = drm_helper_probe_single_connector_modes,
  59. .destroy = drm_connector_cleanup,
  60. .reset = drm_atomic_helper_connector_reset,
  61. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  62. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  63. };
  64. static inline int hyperv_conn_init(struct hyperv_drm_device *hv)
  65. {
  66. drm_connector_helper_add(&hv->connector, &hyperv_connector_helper_funcs);
  67. return drm_connector_init(&hv->dev, &hv->connector,
  68. &hyperv_connector_funcs,
  69. DRM_MODE_CONNECTOR_VIRTUAL);
  70. }
  71. static int hyperv_check_size(struct hyperv_drm_device *hv, int w, int h,
  72. struct drm_framebuffer *fb)
  73. {
  74. u32 pitch = w * (hv->screen_depth / 8);
  75. if (fb)
  76. pitch = fb->pitches[0];
  77. if (pitch * h > hv->fb_size)
  78. return -EINVAL;
  79. return 0;
  80. }
  81. static void hyperv_pipe_enable(struct drm_simple_display_pipe *pipe,
  82. struct drm_crtc_state *crtc_state,
  83. struct drm_plane_state *plane_state)
  84. {
  85. struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
  86. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
  87. hyperv_hide_hw_ptr(hv->hdev);
  88. hyperv_update_situation(hv->hdev, 1, hv->screen_depth,
  89. crtc_state->mode.hdisplay,
  90. crtc_state->mode.vdisplay,
  91. plane_state->fb->pitches[0]);
  92. hyperv_blit_to_vram_fullscreen(plane_state->fb, &shadow_plane_state->data[0]);
  93. }
  94. static int hyperv_pipe_check(struct drm_simple_display_pipe *pipe,
  95. struct drm_plane_state *plane_state,
  96. struct drm_crtc_state *crtc_state)
  97. {
  98. struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
  99. struct drm_framebuffer *fb = plane_state->fb;
  100. if (fb->format->format != DRM_FORMAT_XRGB8888)
  101. return -EINVAL;
  102. if (fb->pitches[0] * fb->height > hv->fb_size) {
  103. drm_err(&hv->dev, "fb size requested by %s for %dX%d (pitch %d) greater than %ld\n",
  104. current->comm, fb->width, fb->height, fb->pitches[0], hv->fb_size);
  105. return -EINVAL;
  106. }
  107. return 0;
  108. }
  109. static void hyperv_pipe_update(struct drm_simple_display_pipe *pipe,
  110. struct drm_plane_state *old_state)
  111. {
  112. struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
  113. struct drm_plane_state *state = pipe->plane.state;
  114. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
  115. struct drm_rect rect;
  116. if (drm_atomic_helper_damage_merged(old_state, state, &rect)) {
  117. hyperv_blit_to_vram_rect(state->fb, &shadow_plane_state->data[0], &rect);
  118. hyperv_update_dirt(hv->hdev, &rect);
  119. }
  120. }
  121. static const struct drm_simple_display_pipe_funcs hyperv_pipe_funcs = {
  122. .enable = hyperv_pipe_enable,
  123. .check = hyperv_pipe_check,
  124. .update = hyperv_pipe_update,
  125. DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
  126. };
  127. static const uint32_t hyperv_formats[] = {
  128. DRM_FORMAT_XRGB8888,
  129. };
  130. static const uint64_t hyperv_modifiers[] = {
  131. DRM_FORMAT_MOD_LINEAR,
  132. DRM_FORMAT_MOD_INVALID
  133. };
  134. static inline int hyperv_pipe_init(struct hyperv_drm_device *hv)
  135. {
  136. int ret;
  137. ret = drm_simple_display_pipe_init(&hv->dev,
  138. &hv->pipe,
  139. &hyperv_pipe_funcs,
  140. hyperv_formats,
  141. ARRAY_SIZE(hyperv_formats),
  142. hyperv_modifiers,
  143. &hv->connector);
  144. if (ret)
  145. return ret;
  146. drm_plane_enable_fb_damage_clips(&hv->pipe.plane);
  147. return 0;
  148. }
  149. static enum drm_mode_status
  150. hyperv_mode_valid(struct drm_device *dev,
  151. const struct drm_display_mode *mode)
  152. {
  153. struct hyperv_drm_device *hv = to_hv(dev);
  154. if (hyperv_check_size(hv, mode->hdisplay, mode->vdisplay, NULL))
  155. return MODE_BAD;
  156. return MODE_OK;
  157. }
  158. static const struct drm_mode_config_funcs hyperv_mode_config_funcs = {
  159. .fb_create = drm_gem_fb_create_with_dirty,
  160. .mode_valid = hyperv_mode_valid,
  161. .atomic_check = drm_atomic_helper_check,
  162. .atomic_commit = drm_atomic_helper_commit,
  163. };
  164. int hyperv_mode_config_init(struct hyperv_drm_device *hv)
  165. {
  166. struct drm_device *dev = &hv->dev;
  167. int ret;
  168. ret = drmm_mode_config_init(dev);
  169. if (ret) {
  170. drm_err(dev, "Failed to initialized mode setting.\n");
  171. return ret;
  172. }
  173. dev->mode_config.min_width = 0;
  174. dev->mode_config.min_height = 0;
  175. dev->mode_config.max_width = hv->screen_width_max;
  176. dev->mode_config.max_height = hv->screen_height_max;
  177. dev->mode_config.preferred_depth = hv->screen_depth;
  178. dev->mode_config.prefer_shadow = 0;
  179. dev->mode_config.funcs = &hyperv_mode_config_funcs;
  180. ret = hyperv_conn_init(hv);
  181. if (ret) {
  182. drm_err(dev, "Failed to initialized connector.\n");
  183. return ret;
  184. }
  185. ret = hyperv_pipe_init(hv);
  186. if (ret) {
  187. drm_err(dev, "Failed to initialized pipe.\n");
  188. return ret;
  189. }
  190. drm_mode_config_reset(dev);
  191. return 0;
  192. }