]> Git Repo - linux.git/blob - drivers/gpu/drm/hyperv/hyperv_drm_modeset.c
Merge tag 'regulator-fix-v5.17-rc2' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / drivers / gpu / drm / hyperv / hyperv_drm_modeset.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_damage_helper.h>
9 #include <drm/drm_drv.h>
10 #include <drm/drm_fb_helper.h>
11 #include <drm/drm_format_helper.h>
12 #include <drm/drm_fourcc.h>
13 #include <drm/drm_gem_atomic_helper.h>
14 #include <drm/drm_gem_framebuffer_helper.h>
15 #include <drm/drm_gem_shmem_helper.h>
16 #include <drm/drm_probe_helper.h>
17 #include <drm/drm_simple_kms_helper.h>
18
19 #include "hyperv_drm.h"
20
21 static int hyperv_blit_to_vram_rect(struct drm_framebuffer *fb,
22                                     const struct dma_buf_map *map,
23                                     struct drm_rect *rect)
24 {
25         struct hyperv_drm_device *hv = to_hv(fb->dev);
26         void __iomem *dst = hv->vram;
27         void *vmap = map->vaddr; /* TODO: Use mapping abstraction properly */
28         int idx;
29
30         if (!drm_dev_enter(&hv->dev, &idx))
31                 return -ENODEV;
32
33         dst += drm_fb_clip_offset(fb->pitches[0], fb->format, rect);
34         drm_fb_memcpy_toio(dst, fb->pitches[0], vmap, fb, rect);
35
36         drm_dev_exit(idx);
37
38         return 0;
39 }
40
41 static int hyperv_blit_to_vram_fullscreen(struct drm_framebuffer *fb, const struct dma_buf_map *map)
42 {
43         struct drm_rect fullscreen = {
44                 .x1 = 0,
45                 .x2 = fb->width,
46                 .y1 = 0,
47                 .y2 = fb->height,
48         };
49         return hyperv_blit_to_vram_rect(fb, map, &fullscreen);
50 }
51
52 static int hyperv_connector_get_modes(struct drm_connector *connector)
53 {
54         struct hyperv_drm_device *hv = to_hv(connector->dev);
55         int count;
56
57         count = drm_add_modes_noedid(connector,
58                                      connector->dev->mode_config.max_width,
59                                      connector->dev->mode_config.max_height);
60         drm_set_preferred_mode(connector, hv->preferred_width,
61                                hv->preferred_height);
62
63         return count;
64 }
65
66 static const struct drm_connector_helper_funcs hyperv_connector_helper_funcs = {
67         .get_modes = hyperv_connector_get_modes,
68 };
69
70 static const struct drm_connector_funcs hyperv_connector_funcs = {
71         .fill_modes = drm_helper_probe_single_connector_modes,
72         .destroy = drm_connector_cleanup,
73         .reset = drm_atomic_helper_connector_reset,
74         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
75         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
76 };
77
78 static inline int hyperv_conn_init(struct hyperv_drm_device *hv)
79 {
80         drm_connector_helper_add(&hv->connector, &hyperv_connector_helper_funcs);
81         return drm_connector_init(&hv->dev, &hv->connector,
82                                   &hyperv_connector_funcs,
83                                   DRM_MODE_CONNECTOR_VIRTUAL);
84 }
85
86 static int hyperv_check_size(struct hyperv_drm_device *hv, int w, int h,
87                              struct drm_framebuffer *fb)
88 {
89         u32 pitch = w * (hv->screen_depth / 8);
90
91         if (fb)
92                 pitch = fb->pitches[0];
93
94         if (pitch * h > hv->fb_size)
95                 return -EINVAL;
96
97         return 0;
98 }
99
100 static void hyperv_pipe_enable(struct drm_simple_display_pipe *pipe,
101                                struct drm_crtc_state *crtc_state,
102                                struct drm_plane_state *plane_state)
103 {
104         struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
105         struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
106
107         hyperv_hide_hw_ptr(hv->hdev);
108         hyperv_update_situation(hv->hdev, 1,  hv->screen_depth,
109                                 crtc_state->mode.hdisplay,
110                                 crtc_state->mode.vdisplay,
111                                 plane_state->fb->pitches[0]);
112         hyperv_blit_to_vram_fullscreen(plane_state->fb, &shadow_plane_state->data[0]);
113 }
114
115 static int hyperv_pipe_check(struct drm_simple_display_pipe *pipe,
116                              struct drm_plane_state *plane_state,
117                              struct drm_crtc_state *crtc_state)
118 {
119         struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
120         struct drm_framebuffer *fb = plane_state->fb;
121
122         if (fb->format->format != DRM_FORMAT_XRGB8888)
123                 return -EINVAL;
124
125         if (fb->pitches[0] * fb->height > hv->fb_size)
126                 return -EINVAL;
127
128         return 0;
129 }
130
131 static void hyperv_pipe_update(struct drm_simple_display_pipe *pipe,
132                                struct drm_plane_state *old_state)
133 {
134         struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
135         struct drm_plane_state *state = pipe->plane.state;
136         struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
137         struct drm_rect rect;
138
139         if (drm_atomic_helper_damage_merged(old_state, state, &rect)) {
140                 hyperv_blit_to_vram_rect(state->fb, &shadow_plane_state->data[0], &rect);
141                 hyperv_update_dirt(hv->hdev, &rect);
142         }
143 }
144
145 static const struct drm_simple_display_pipe_funcs hyperv_pipe_funcs = {
146         .enable = hyperv_pipe_enable,
147         .check = hyperv_pipe_check,
148         .update = hyperv_pipe_update,
149         DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
150 };
151
152 static const uint32_t hyperv_formats[] = {
153         DRM_FORMAT_XRGB8888,
154 };
155
156 static const uint64_t hyperv_modifiers[] = {
157         DRM_FORMAT_MOD_LINEAR,
158         DRM_FORMAT_MOD_INVALID
159 };
160
161 static inline int hyperv_pipe_init(struct hyperv_drm_device *hv)
162 {
163         int ret;
164
165         ret = drm_simple_display_pipe_init(&hv->dev,
166                                            &hv->pipe,
167                                            &hyperv_pipe_funcs,
168                                            hyperv_formats,
169                                            ARRAY_SIZE(hyperv_formats),
170                                            hyperv_modifiers,
171                                            &hv->connector);
172         if (ret)
173                 return ret;
174
175         drm_plane_enable_fb_damage_clips(&hv->pipe.plane);
176
177         return 0;
178 }
179
180 static enum drm_mode_status
181 hyperv_mode_valid(struct drm_device *dev,
182                   const struct drm_display_mode *mode)
183 {
184         struct hyperv_drm_device *hv = to_hv(dev);
185
186         if (hyperv_check_size(hv, mode->hdisplay, mode->vdisplay, NULL))
187                 return MODE_BAD;
188
189         return MODE_OK;
190 }
191
192 static const struct drm_mode_config_funcs hyperv_mode_config_funcs = {
193         .fb_create = drm_gem_fb_create_with_dirty,
194         .mode_valid = hyperv_mode_valid,
195         .atomic_check = drm_atomic_helper_check,
196         .atomic_commit = drm_atomic_helper_commit,
197 };
198
199 int hyperv_mode_config_init(struct hyperv_drm_device *hv)
200 {
201         struct drm_device *dev = &hv->dev;
202         int ret;
203
204         ret = drmm_mode_config_init(dev);
205         if (ret) {
206                 drm_err(dev, "Failed to initialized mode setting.\n");
207                 return ret;
208         }
209
210         dev->mode_config.min_width = 0;
211         dev->mode_config.min_height = 0;
212         dev->mode_config.max_width = hv->screen_width_max;
213         dev->mode_config.max_height = hv->screen_height_max;
214
215         dev->mode_config.preferred_depth = hv->screen_depth;
216         dev->mode_config.prefer_shadow = 0;
217
218         dev->mode_config.funcs = &hyperv_mode_config_funcs;
219
220         ret = hyperv_conn_init(hv);
221         if (ret) {
222                 drm_err(dev, "Failed to initialized connector.\n");
223                 return ret;
224         }
225
226         ret = hyperv_pipe_init(hv);
227         if (ret) {
228                 drm_err(dev, "Failed to initialized pipe.\n");
229                 return ret;
230         }
231
232         drm_mode_config_reset(dev);
233
234         return 0;
235 }
This page took 0.045864 seconds and 4 git commands to generate.