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