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 
28 #include "virtgpu_drv.h"
29 #include <drm/drm_atomic_helper.h>
30 #include <drm/drm_gem_framebuffer_helper.h>
31 #include <drm/drm_probe_helper.h>
32 #include <drm/drm_damage_helper.h>
33 
34 #define XRES_MIN    32
35 #define YRES_MIN    32
36 
37 #define XRES_DEF  1024
38 #define YRES_DEF   768
39 
40 #define XRES_MAX  8192
41 #define YRES_MAX  8192
42 
43 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
44 	.set_config             = drm_atomic_helper_set_config,
45 	.destroy                = drm_crtc_cleanup,
46 
47 	.page_flip              = drm_atomic_helper_page_flip,
48 	.reset                  = drm_atomic_helper_crtc_reset,
49 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
50 	.atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
51 };
52 
53 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
54 	.create_handle = drm_gem_fb_create_handle,
55 	.destroy = drm_gem_fb_destroy,
56 	.dirty = drm_atomic_helper_dirtyfb,
57 };
58 
59 int
60 virtio_gpu_framebuffer_init(struct drm_device *dev,
61 			    struct virtio_gpu_framebuffer *vgfb,
62 			    const struct drm_mode_fb_cmd2 *mode_cmd,
63 			    struct drm_gem_object *obj)
64 {
65 	int ret;
66 
67 	vgfb->base.obj[0] = obj;
68 
69 	drm_helper_mode_fill_fb_struct(dev, &vgfb->base, mode_cmd);
70 
71 	ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs);
72 	if (ret) {
73 		vgfb->base.obj[0] = NULL;
74 		return ret;
75 	}
76 	return 0;
77 }
78 
79 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
80 {
81 	struct drm_device *dev = crtc->dev;
82 	struct virtio_gpu_device *vgdev = dev->dev_private;
83 	struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
84 
85 	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
86 				   crtc->mode.hdisplay,
87 				   crtc->mode.vdisplay, 0, 0);
88 }
89 
90 static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
91 					  struct drm_crtc_state *old_state)
92 {
93 	struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
94 
95 	output->enabled = true;
96 }
97 
98 static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,
99 					   struct drm_crtc_state *old_state)
100 {
101 	struct drm_device *dev = crtc->dev;
102 	struct virtio_gpu_device *vgdev = dev->dev_private;
103 	struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
104 
105 	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
106 	output->enabled = false;
107 }
108 
109 static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
110 					struct drm_crtc_state *state)
111 {
112 	return 0;
113 }
114 
115 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
116 					 struct drm_crtc_state *old_state)
117 {
118 	unsigned long flags;
119 
120 	spin_lock_irqsave(&crtc->dev->event_lock, flags);
121 	if (crtc->state->event)
122 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
123 	crtc->state->event = NULL;
124 	spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
125 }
126 
127 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
128 	.mode_set_nofb = virtio_gpu_crtc_mode_set_nofb,
129 	.atomic_check  = virtio_gpu_crtc_atomic_check,
130 	.atomic_flush  = virtio_gpu_crtc_atomic_flush,
131 	.atomic_enable = virtio_gpu_crtc_atomic_enable,
132 	.atomic_disable = virtio_gpu_crtc_atomic_disable,
133 };
134 
135 static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder,
136 				    struct drm_display_mode *mode,
137 				    struct drm_display_mode *adjusted_mode)
138 {
139 }
140 
141 static void virtio_gpu_enc_enable(struct drm_encoder *encoder)
142 {
143 }
144 
145 static void virtio_gpu_enc_disable(struct drm_encoder *encoder)
146 {
147 }
148 
149 static int virtio_gpu_conn_get_modes(struct drm_connector *connector)
150 {
151 	struct virtio_gpu_output *output =
152 		drm_connector_to_virtio_gpu_output(connector);
153 	struct drm_display_mode *mode = NULL;
154 	int count, width, height;
155 
156 	if (output->edid) {
157 		count = drm_add_edid_modes(connector, output->edid);
158 		if (count)
159 			return count;
160 	}
161 
162 	width  = le32_to_cpu(output->info.r.width);
163 	height = le32_to_cpu(output->info.r.height);
164 	count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
165 
166 	if (width == 0 || height == 0) {
167 		width = XRES_DEF;
168 		height = YRES_DEF;
169 		drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
170 	} else {
171 		DRM_DEBUG("add mode: %dx%d\n", width, height);
172 		mode = drm_cvt_mode(connector->dev, width, height, 60,
173 				    false, false, false);
174 		mode->type |= DRM_MODE_TYPE_PREFERRED;
175 		drm_mode_probed_add(connector, mode);
176 		count++;
177 	}
178 
179 	return count;
180 }
181 
182 static enum drm_mode_status virtio_gpu_conn_mode_valid(struct drm_connector *connector,
183 				      struct drm_display_mode *mode)
184 {
185 	struct virtio_gpu_output *output =
186 		drm_connector_to_virtio_gpu_output(connector);
187 	int width, height;
188 
189 	width  = le32_to_cpu(output->info.r.width);
190 	height = le32_to_cpu(output->info.r.height);
191 
192 	if (!(mode->type & DRM_MODE_TYPE_PREFERRED))
193 		return MODE_OK;
194 	if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF)
195 		return MODE_OK;
196 	if (mode->hdisplay <= width  && mode->hdisplay >= width - 16 &&
197 	    mode->vdisplay <= height && mode->vdisplay >= height - 16)
198 		return MODE_OK;
199 
200 	DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay);
201 	return MODE_BAD;
202 }
203 
204 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = {
205 	.mode_set   = virtio_gpu_enc_mode_set,
206 	.enable     = virtio_gpu_enc_enable,
207 	.disable    = virtio_gpu_enc_disable,
208 };
209 
210 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = {
211 	.get_modes    = virtio_gpu_conn_get_modes,
212 	.mode_valid   = virtio_gpu_conn_mode_valid,
213 };
214 
215 static enum drm_connector_status virtio_gpu_conn_detect(
216 			struct drm_connector *connector,
217 			bool force)
218 {
219 	struct virtio_gpu_output *output =
220 		drm_connector_to_virtio_gpu_output(connector);
221 
222 	if (output->info.enabled)
223 		return connector_status_connected;
224 	else
225 		return connector_status_disconnected;
226 }
227 
228 static void virtio_gpu_conn_destroy(struct drm_connector *connector)
229 {
230 	drm_connector_unregister(connector);
231 	drm_connector_cleanup(connector);
232 }
233 
234 static const struct drm_connector_funcs virtio_gpu_connector_funcs = {
235 	.detect = virtio_gpu_conn_detect,
236 	.fill_modes = drm_helper_probe_single_connector_modes,
237 	.destroy = virtio_gpu_conn_destroy,
238 	.reset = drm_atomic_helper_connector_reset,
239 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
240 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
241 };
242 
243 static const struct drm_encoder_funcs virtio_gpu_enc_funcs = {
244 	.destroy = drm_encoder_cleanup,
245 };
246 
247 static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index)
248 {
249 	struct drm_device *dev = vgdev->ddev;
250 	struct virtio_gpu_output *output = vgdev->outputs + index;
251 	struct drm_connector *connector = &output->conn;
252 	struct drm_encoder *encoder = &output->enc;
253 	struct drm_crtc *crtc = &output->crtc;
254 	struct drm_plane *primary, *cursor;
255 
256 	output->index = index;
257 	if (index == 0) {
258 		output->info.enabled = cpu_to_le32(true);
259 		output->info.r.width = cpu_to_le32(XRES_DEF);
260 		output->info.r.height = cpu_to_le32(YRES_DEF);
261 	}
262 
263 	primary = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_PRIMARY, index);
264 	if (IS_ERR(primary))
265 		return PTR_ERR(primary);
266 	cursor = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_CURSOR, index);
267 	if (IS_ERR(cursor))
268 		return PTR_ERR(cursor);
269 	drm_crtc_init_with_planes(dev, crtc, primary, cursor,
270 				  &virtio_gpu_crtc_funcs, NULL);
271 	drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs);
272 
273 	drm_connector_init(dev, connector, &virtio_gpu_connector_funcs,
274 			   DRM_MODE_CONNECTOR_VIRTUAL);
275 	drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs);
276 	if (vgdev->has_edid)
277 		drm_connector_attach_edid_property(connector);
278 
279 	drm_encoder_init(dev, encoder, &virtio_gpu_enc_funcs,
280 			 DRM_MODE_ENCODER_VIRTUAL, NULL);
281 	drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs);
282 	encoder->possible_crtcs = 1 << index;
283 
284 	drm_connector_attach_encoder(connector, encoder);
285 	drm_connector_register(connector);
286 	return 0;
287 }
288 
289 static struct drm_framebuffer *
290 virtio_gpu_user_framebuffer_create(struct drm_device *dev,
291 				   struct drm_file *file_priv,
292 				   const struct drm_mode_fb_cmd2 *mode_cmd)
293 {
294 	struct drm_gem_object *obj = NULL;
295 	struct virtio_gpu_framebuffer *virtio_gpu_fb;
296 	int ret;
297 
298 	if (mode_cmd->pixel_format != DRM_FORMAT_HOST_XRGB8888 &&
299 	    mode_cmd->pixel_format != DRM_FORMAT_HOST_ARGB8888)
300 		return ERR_PTR(-ENOENT);
301 
302 	/* lookup object associated with res handle */
303 	obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
304 	if (!obj)
305 		return ERR_PTR(-EINVAL);
306 
307 	virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL);
308 	if (virtio_gpu_fb == NULL)
309 		return ERR_PTR(-ENOMEM);
310 
311 	ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj);
312 	if (ret) {
313 		kfree(virtio_gpu_fb);
314 		drm_gem_object_put_unlocked(obj);
315 		return NULL;
316 	}
317 
318 	return &virtio_gpu_fb->base;
319 }
320 
321 static void vgdev_atomic_commit_tail(struct drm_atomic_state *state)
322 {
323 	struct drm_device *dev = state->dev;
324 
325 	drm_atomic_helper_commit_modeset_disables(dev, state);
326 	drm_atomic_helper_commit_modeset_enables(dev, state);
327 	drm_atomic_helper_commit_planes(dev, state, 0);
328 
329 	drm_atomic_helper_commit_hw_done(state);
330 
331 	drm_atomic_helper_wait_for_vblanks(dev, state);
332 	drm_atomic_helper_cleanup_planes(dev, state);
333 }
334 
335 static const struct drm_mode_config_helper_funcs virtio_mode_config_helpers = {
336 	.atomic_commit_tail = vgdev_atomic_commit_tail,
337 };
338 
339 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = {
340 	.fb_create = virtio_gpu_user_framebuffer_create,
341 	.atomic_check = drm_atomic_helper_check,
342 	.atomic_commit = drm_atomic_helper_commit,
343 };
344 
345 void virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
346 {
347 	int i;
348 
349 	drm_mode_config_init(vgdev->ddev);
350 	vgdev->ddev->mode_config.quirk_addfb_prefer_host_byte_order = true;
351 	vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs;
352 	vgdev->ddev->mode_config.helper_private = &virtio_mode_config_helpers;
353 
354 	/* modes will be validated against the framebuffer size */
355 	vgdev->ddev->mode_config.min_width = XRES_MIN;
356 	vgdev->ddev->mode_config.min_height = YRES_MIN;
357 	vgdev->ddev->mode_config.max_width = XRES_MAX;
358 	vgdev->ddev->mode_config.max_height = YRES_MAX;
359 
360 	for (i = 0 ; i < vgdev->num_scanouts; ++i)
361 		vgdev_output_init(vgdev, i);
362 
363 	drm_mode_config_reset(vgdev->ddev);
364 }
365 
366 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev)
367 {
368 	int i;
369 
370 	for (i = 0 ; i < vgdev->num_scanouts; ++i)
371 		kfree(vgdev->outputs[i].edid);
372 	drm_atomic_helper_shutdown(vgdev->ddev);
373 	drm_mode_config_cleanup(vgdev->ddev);
374 }
375