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