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