File Index
Files and directories
Sign in
FILE

hyperv_drm_modeset.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/hyperv/hyperv_drm_modeset.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright 2021 Microsoft
  4 */
  5
  6#include <linux/hyperv.h>
  7
  8#include <drm/drm_atomic.h>
  9#include <drm/drm_crtc_helper.h>
 10#include <drm/drm_damage_helper.h>
 11#include <drm/drm_drv.h>
 12#include <drm/drm_edid.h>
 13#include <drm/drm_format_helper.h>
 14#include <drm/drm_fourcc.h>
 15#include <drm/drm_framebuffer.h>
 16#include <drm/drm_gem_atomic_helper.h>
 17#include <drm/drm_gem_framebuffer_helper.h>
 18#include <drm/drm_gem_shmem_helper.h>
 19#include <drm/drm_probe_helper.h>
 20#include <drm/drm_panic.h>
 21#include <drm/drm_plane.h>
 22#include <drm/drm_print.h>
 23#include <drm/drm_vblank.h>
 24#include <drm/drm_vblank_helper.h>
 25
 26#include "hyperv_drm.h"
 27
 28static int hyperv_blit_to_vram_rect(struct drm_framebuffer *fb,
 29				    const struct iosys_map *vmap,
 30				    struct drm_rect *rect)
 31{
 32	struct hyperv_drm_device *hv = to_hv(fb->dev);
 33	struct iosys_map dst = IOSYS_MAP_INIT_VADDR_IOMEM(hv->vram);
 34	int idx;
 35
 36	if (!drm_dev_enter(&hv->dev, &idx))
 37		return -ENODEV;
 38
 39	iosys_map_incr(&dst, drm_fb_clip_offset(fb->pitches[0], fb->format, rect));
 40	drm_fb_memcpy(&dst, fb->pitches, vmap, fb, rect);
 41
 42	drm_dev_exit(idx);
 43
 44	return 0;
 45}
 46
 47static int hyperv_connector_get_modes(struct drm_connector *connector)
 48{
 49	struct hyperv_drm_device *hv = to_hv(connector->dev);
 50	int count;
 51
 52	count = drm_add_modes_noedid(connector,
 53				     connector->dev->mode_config.max_width,
 54				     connector->dev->mode_config.max_height);
 55	drm_set_preferred_mode(connector, hv->preferred_width,
 56			       hv->preferred_height);
 57
 58	return count;
 59}
 60
 61static const struct drm_connector_helper_funcs hyperv_connector_helper_funcs = {
 62	.get_modes = hyperv_connector_get_modes,
 63};
 64
 65static const struct drm_connector_funcs hyperv_connector_funcs = {
 66	.fill_modes = drm_helper_probe_single_connector_modes,
 67	.destroy = drm_connector_cleanup,
 68	.reset = drm_atomic_helper_connector_reset,
 69	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
 70	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
 71};
 72
 73static inline int hyperv_conn_init(struct hyperv_drm_device *hv)
 74{
 75	drm_connector_helper_add(&hv->connector, &hyperv_connector_helper_funcs);
 76	return drm_connector_init(&hv->dev, &hv->connector,
 77				  &hyperv_connector_funcs,
 78				  DRM_MODE_CONNECTOR_VIRTUAL);
 79}
 80
 81static int hyperv_check_size(struct hyperv_drm_device *hv, int w, int h,
 82			     struct drm_framebuffer *fb)
 83{
 84	u32 pitch = w * (hv->screen_depth / 8);
 85
 86	if (fb)
 87		pitch = fb->pitches[0];
 88
 89	if (pitch * h > hv->fb_size)
 90		return -EINVAL;
 91
 92	return 0;
 93}
 94
 95static const uint32_t hyperv_formats[] = {
 96	DRM_FORMAT_XRGB8888,
 97};
 98
 99static const uint64_t hyperv_modifiers[] = {
100	DRM_FORMAT_MOD_LINEAR,
101	DRM_FORMAT_MOD_INVALID
102};
103
104static void hyperv_crtc_helper_atomic_enable(struct drm_crtc *crtc,
105					     struct drm_atomic_state *state)
106{
107	struct hyperv_drm_device *hv = to_hv(crtc->dev);
108	struct drm_plane *plane = &hv->plane;
109	struct drm_plane_state *plane_state = plane->state;
110	struct drm_crtc_state *crtc_state = crtc->state;
111
112	hyperv_hide_hw_ptr(hv->hdev);
113	hyperv_update_situation(hv->hdev, 1,  hv->screen_depth,
114				crtc_state->mode.hdisplay,
115				crtc_state->mode.vdisplay,
116				plane_state->fb->pitches[0]);
117
118	drm_crtc_vblank_on(crtc);
119}
120
121static const struct drm_crtc_helper_funcs hyperv_crtc_helper_funcs = {
122	.atomic_check = drm_crtc_helper_atomic_check,
123	.atomic_flush = drm_crtc_vblank_atomic_flush,
124	.atomic_enable = hyperv_crtc_helper_atomic_enable,
125	.atomic_disable = drm_crtc_vblank_atomic_disable,
126};
127
128static const struct drm_crtc_funcs hyperv_crtc_funcs = {
129	.reset = drm_atomic_helper_crtc_reset,
130	.destroy = drm_crtc_cleanup,
131	.set_config = drm_atomic_helper_set_config,
132	.page_flip = drm_atomic_helper_page_flip,
133	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
134	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
135	DRM_CRTC_VBLANK_TIMER_FUNCS,
136};
137
138static int hyperv_plane_atomic_check(struct drm_plane *plane,
139				     struct drm_atomic_state *state)
140{
141	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
142	struct hyperv_drm_device *hv = to_hv(plane->dev);
143	struct drm_framebuffer *fb = plane_state->fb;
144	struct drm_crtc *crtc = plane_state->crtc;
145	struct drm_crtc_state *crtc_state = NULL;
146	int ret;
147
148	if (crtc)
149		crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
150
151	ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
152						  DRM_PLANE_NO_SCALING,
153						  DRM_PLANE_NO_SCALING,
154						  false, false);
155	if (ret)
156		return ret;
157
158	if (!plane_state->visible)
159		return 0;
160
161	if (fb->pitches[0] * fb->height > hv->fb_size) {
162		drm_err(&hv->dev, "fb size requested by %s for %dX%d (pitch %d) greater than %ld\n",
163			current->comm, fb->width, fb->height, fb->pitches[0], hv->fb_size);
164		return -EINVAL;
165	}
166
167	return 0;
168}
169
170static void hyperv_plane_atomic_update(struct drm_plane *plane,
171				       struct drm_atomic_state *state)
172{
173	struct hyperv_drm_device *hv = to_hv(plane->dev);
174	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
175	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
176	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(new_state);
177	struct drm_rect damage;
178	struct drm_rect dst_clip;
179	struct drm_atomic_helper_damage_iter iter;
180
181	drm_atomic_helper_damage_iter_init(&iter, old_state, new_state);
182	drm_atomic_for_each_plane_damage(&iter, &damage) {
183		dst_clip = new_state->dst;
184
185		if (!drm_rect_intersect(&dst_clip, &damage))
186			continue;
187
188		hyperv_blit_to_vram_rect(new_state->fb, &shadow_plane_state->data[0], &damage);
189		hyperv_update_dirt(hv->hdev, &damage);
190	}
191}
192
193static int hyperv_plane_get_scanout_buffer(struct drm_plane *plane,
194					   struct drm_scanout_buffer *sb)
195{
196	struct hyperv_drm_device *hv = to_hv(plane->dev);
197	struct iosys_map map = IOSYS_MAP_INIT_VADDR_IOMEM(hv->vram);
198
199	if (plane->state && plane->state->fb) {
200		sb->format = plane->state->fb->format;
201		sb->width = plane->state->fb->width;
202		sb->height = plane->state->fb->height;
203		sb->pitch[0] = plane->state->fb->pitches[0];
204		sb->map[0] = map;
205		return 0;
206	}
207	return -ENODEV;
208}
209
210static void hyperv_plane_panic_flush(struct drm_plane *plane)
211{
212	struct hyperv_drm_device *hv = to_hv(plane->dev);
213	struct drm_rect rect;
214
215	if (!plane->state || !plane->state->fb)
216		return;
217
218	rect.x1 = 0;
219	rect.y1 = 0;
220	rect.x2 = plane->state->fb->width;
221	rect.y2 = plane->state->fb->height;
222
223	hyperv_update_dirt(hv->hdev, &rect);
224}
225
226static const struct drm_plane_helper_funcs hyperv_plane_helper_funcs = {
227	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
228	.atomic_check = hyperv_plane_atomic_check,
229	.atomic_update = hyperv_plane_atomic_update,
230	.get_scanout_buffer = hyperv_plane_get_scanout_buffer,
231	.panic_flush = hyperv_plane_panic_flush,
232};
233
234static const struct drm_plane_funcs hyperv_plane_funcs = {
235	.update_plane		= drm_atomic_helper_update_plane,
236	.disable_plane		= drm_atomic_helper_disable_plane,
237	.destroy		= drm_plane_cleanup,
238	DRM_GEM_SHADOW_PLANE_FUNCS,
239};
240
241static const struct drm_encoder_funcs hyperv_drm_simple_encoder_funcs_cleanup = {
242	.destroy = drm_encoder_cleanup,
243};
244
245static inline int hyperv_pipe_init(struct hyperv_drm_device *hv)
246{
247	struct drm_device *dev = &hv->dev;
248	struct drm_encoder *encoder = &hv->encoder;
249	struct drm_plane *plane = &hv->plane;
250	struct drm_crtc *crtc = &hv->crtc;
251	struct drm_connector *connector = &hv->connector;
252	int ret;
253
254	ret = drm_universal_plane_init(dev, plane, 0,
255				       &hyperv_plane_funcs,
256				       hyperv_formats, ARRAY_SIZE(hyperv_formats),
257				       hyperv_modifiers,
258				       DRM_PLANE_TYPE_PRIMARY, NULL);
259	if (ret)
260		return ret;
261	drm_plane_helper_add(plane, &hyperv_plane_helper_funcs);
262	drm_plane_enable_fb_damage_clips(plane);
263
264	ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
265					&hyperv_crtc_funcs, NULL);
266	if (ret)
267		return ret;
268	drm_crtc_helper_add(crtc, &hyperv_crtc_helper_funcs);
269
270	encoder->possible_crtcs = drm_crtc_mask(crtc);
271	ret = drm_encoder_init(dev, encoder,
272			       &hyperv_drm_simple_encoder_funcs_cleanup,
273			       DRM_MODE_ENCODER_NONE, NULL);
274	if (ret)
275		return ret;
276
277	ret = hyperv_conn_init(hv);
278	if (ret) {
279		drm_err(dev, "Failed to initialized connector.\n");
280		return ret;
281	}
282
283	return drm_connector_attach_encoder(connector, encoder);
284}
285
286static enum drm_mode_status
287hyperv_mode_valid(struct drm_device *dev,
288		  const struct drm_display_mode *mode)
289{
290	struct hyperv_drm_device *hv = to_hv(dev);
291
292	if (hyperv_check_size(hv, mode->hdisplay, mode->vdisplay, NULL))
293		return MODE_BAD;
294
295	return MODE_OK;
296}
297
298static const struct drm_mode_config_funcs hyperv_mode_config_funcs = {
299	.fb_create = drm_gem_fb_create_with_dirty,
300	.mode_valid = hyperv_mode_valid,
301	.atomic_check = drm_atomic_helper_check,
302	.atomic_commit = drm_atomic_helper_commit,
303};
304
305int hyperv_mode_config_init(struct hyperv_drm_device *hv)
306{
307	struct drm_device *dev = &hv->dev;
308	int ret;
309
310	ret = drmm_mode_config_init(dev);
311	if (ret) {
312		drm_err(dev, "Failed to initialized mode setting.\n");
313		return ret;
314	}
315
316	dev->mode_config.min_width = 0;
317	dev->mode_config.min_height = 0;
318	dev->mode_config.max_width = hv->screen_width_max;
319	dev->mode_config.max_height = hv->screen_height_max;
320
321	dev->mode_config.preferred_depth = hv->screen_depth;
322	dev->mode_config.prefer_shadow = 0;
323
324	dev->mode_config.funcs = &hyperv_mode_config_funcs;
325
326	ret = hyperv_pipe_init(hv);
327	if (ret) {
328		drm_err(dev, "Failed to initialized pipe.\n");
329		return ret;
330	}
331
332	ret = drm_vblank_init(dev, 1);
333	if (ret)
334		return ret;
335
336	drm_mode_config_reset(dev);
337
338	return 0;
339}
~/ Read-only file preview.