2 * Copyright (C) 2015 Red Hat, Inc.
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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.
28 #include "virtgpu_drv.h"
29 #include <drm/drm_crtc_helper.h>
30 #include <drm/drm_atomic_helper.h>
41 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs
= {
42 .set_config
= drm_atomic_helper_set_config
,
43 .destroy
= drm_crtc_cleanup
,
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
,
51 static void virtio_gpu_user_framebuffer_destroy(struct drm_framebuffer
*fb
)
53 struct virtio_gpu_framebuffer
*virtio_gpu_fb
54 = to_virtio_gpu_framebuffer(fb
);
56 drm_gem_object_unreference_unlocked(virtio_gpu_fb
->obj
);
57 drm_framebuffer_cleanup(fb
);
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
,
68 struct virtio_gpu_framebuffer
*virtio_gpu_fb
69 = to_virtio_gpu_framebuffer(fb
);
71 return virtio_gpu_surface_dirty(virtio_gpu_fb
, clips
, num_clips
);
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
,
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
)
86 struct virtio_gpu_object
*bo
;
89 bo
= gem_to_virtio_gpu_obj(obj
);
91 ret
= drm_framebuffer_init(dev
, &vgfb
->base
, &virtio_gpu_fb_funcs
);
96 drm_helper_mode_fill_fb_struct(&vgfb
->base
, mode_cmd
);
98 spin_lock_init(&vgfb
->dirty_lock
);
99 vgfb
->x1
= vgfb
->y1
= INT_MAX
;
100 vgfb
->x2
= vgfb
->y2
= 0;
104 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc
*crtc
)
106 struct drm_device
*dev
= crtc
->dev
;
107 struct virtio_gpu_device
*vgdev
= dev
->dev_private
;
108 struct virtio_gpu_output
*output
= drm_crtc_to_virtio_gpu_output(crtc
);
110 virtio_gpu_cmd_set_scanout(vgdev
, output
->index
, 0,
112 crtc
->mode
.vdisplay
, 0, 0);
115 static void virtio_gpu_crtc_enable(struct drm_crtc
*crtc
)
119 static void virtio_gpu_crtc_disable(struct drm_crtc
*crtc
)
121 struct drm_device
*dev
= crtc
->dev
;
122 struct virtio_gpu_device
*vgdev
= dev
->dev_private
;
123 struct virtio_gpu_output
*output
= drm_crtc_to_virtio_gpu_output(crtc
);
125 virtio_gpu_cmd_set_scanout(vgdev
, output
->index
, 0, 0, 0, 0, 0);
128 static int virtio_gpu_crtc_atomic_check(struct drm_crtc
*crtc
,
129 struct drm_crtc_state
*state
)
134 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc
*crtc
,
135 struct drm_crtc_state
*old_state
)
139 spin_lock_irqsave(&crtc
->dev
->event_lock
, flags
);
140 if (crtc
->state
->event
)
141 drm_crtc_send_vblank_event(crtc
, crtc
->state
->event
);
142 crtc
->state
->event
= NULL
;
143 spin_unlock_irqrestore(&crtc
->dev
->event_lock
, flags
);
146 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs
= {
147 .enable
= virtio_gpu_crtc_enable
,
148 .disable
= virtio_gpu_crtc_disable
,
149 .mode_set_nofb
= virtio_gpu_crtc_mode_set_nofb
,
150 .atomic_check
= virtio_gpu_crtc_atomic_check
,
151 .atomic_flush
= virtio_gpu_crtc_atomic_flush
,
154 static void virtio_gpu_enc_mode_set(struct drm_encoder
*encoder
,
155 struct drm_display_mode
*mode
,
156 struct drm_display_mode
*adjusted_mode
)
160 static void virtio_gpu_enc_enable(struct drm_encoder
*encoder
)
164 static void virtio_gpu_enc_disable(struct drm_encoder
*encoder
)
168 static int virtio_gpu_conn_get_modes(struct drm_connector
*connector
)
170 struct virtio_gpu_output
*output
=
171 drm_connector_to_virtio_gpu_output(connector
);
172 struct drm_display_mode
*mode
= NULL
;
173 int count
, width
, height
;
175 width
= le32_to_cpu(output
->info
.r
.width
);
176 height
= le32_to_cpu(output
->info
.r
.height
);
177 count
= drm_add_modes_noedid(connector
, XRES_MAX
, YRES_MAX
);
179 if (width
== 0 || height
== 0) {
182 drm_set_preferred_mode(connector
, XRES_DEF
, YRES_DEF
);
184 DRM_DEBUG("add mode: %dx%d\n", width
, height
);
185 mode
= drm_cvt_mode(connector
->dev
, width
, height
, 60,
186 false, false, false);
187 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
188 drm_mode_probed_add(connector
, mode
);
195 static int virtio_gpu_conn_mode_valid(struct drm_connector
*connector
,
196 struct drm_display_mode
*mode
)
198 struct virtio_gpu_output
*output
=
199 drm_connector_to_virtio_gpu_output(connector
);
202 width
= le32_to_cpu(output
->info
.r
.width
);
203 height
= le32_to_cpu(output
->info
.r
.height
);
205 if (!(mode
->type
& DRM_MODE_TYPE_PREFERRED
))
207 if (mode
->hdisplay
== XRES_DEF
&& mode
->vdisplay
== YRES_DEF
)
209 if (mode
->hdisplay
<= width
&& mode
->hdisplay
>= width
- 16 &&
210 mode
->vdisplay
<= height
&& mode
->vdisplay
>= height
- 16)
213 DRM_DEBUG("del mode: %dx%d\n", mode
->hdisplay
, mode
->vdisplay
);
217 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs
= {
218 .mode_set
= virtio_gpu_enc_mode_set
,
219 .enable
= virtio_gpu_enc_enable
,
220 .disable
= virtio_gpu_enc_disable
,
223 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs
= {
224 .get_modes
= virtio_gpu_conn_get_modes
,
225 .mode_valid
= virtio_gpu_conn_mode_valid
,
228 static enum drm_connector_status
virtio_gpu_conn_detect(
229 struct drm_connector
*connector
,
232 struct virtio_gpu_output
*output
=
233 drm_connector_to_virtio_gpu_output(connector
);
235 if (output
->info
.enabled
)
236 return connector_status_connected
;
238 return connector_status_disconnected
;
241 static void virtio_gpu_conn_destroy(struct drm_connector
*connector
)
243 struct virtio_gpu_output
*virtio_gpu_output
=
244 drm_connector_to_virtio_gpu_output(connector
);
246 drm_connector_unregister(connector
);
247 drm_connector_cleanup(connector
);
248 kfree(virtio_gpu_output
);
251 static const struct drm_connector_funcs virtio_gpu_connector_funcs
= {
252 .dpms
= drm_atomic_helper_connector_dpms
,
253 .detect
= virtio_gpu_conn_detect
,
254 .fill_modes
= drm_helper_probe_single_connector_modes
,
255 .destroy
= virtio_gpu_conn_destroy
,
256 .reset
= drm_atomic_helper_connector_reset
,
257 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
258 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
261 static const struct drm_encoder_funcs virtio_gpu_enc_funcs
= {
262 .destroy
= drm_encoder_cleanup
,
265 static int vgdev_output_init(struct virtio_gpu_device
*vgdev
, int index
)
267 struct drm_device
*dev
= vgdev
->ddev
;
268 struct virtio_gpu_output
*output
= vgdev
->outputs
+ index
;
269 struct drm_connector
*connector
= &output
->conn
;
270 struct drm_encoder
*encoder
= &output
->enc
;
271 struct drm_crtc
*crtc
= &output
->crtc
;
272 struct drm_plane
*primary
, *cursor
;
274 output
->index
= index
;
276 output
->info
.enabled
= cpu_to_le32(true);
277 output
->info
.r
.width
= cpu_to_le32(XRES_DEF
);
278 output
->info
.r
.height
= cpu_to_le32(YRES_DEF
);
281 primary
= virtio_gpu_plane_init(vgdev
, DRM_PLANE_TYPE_PRIMARY
, index
);
283 return PTR_ERR(primary
);
284 cursor
= virtio_gpu_plane_init(vgdev
, DRM_PLANE_TYPE_CURSOR
, index
);
286 return PTR_ERR(cursor
);
287 drm_crtc_init_with_planes(dev
, crtc
, primary
, cursor
,
288 &virtio_gpu_crtc_funcs
, NULL
);
289 drm_crtc_helper_add(crtc
, &virtio_gpu_crtc_helper_funcs
);
290 primary
->crtc
= crtc
;
293 drm_connector_init(dev
, connector
, &virtio_gpu_connector_funcs
,
294 DRM_MODE_CONNECTOR_VIRTUAL
);
295 drm_connector_helper_add(connector
, &virtio_gpu_conn_helper_funcs
);
297 drm_encoder_init(dev
, encoder
, &virtio_gpu_enc_funcs
,
298 DRM_MODE_ENCODER_VIRTUAL
, NULL
);
299 drm_encoder_helper_add(encoder
, &virtio_gpu_enc_helper_funcs
);
300 encoder
->possible_crtcs
= 1 << index
;
302 drm_mode_connector_attach_encoder(connector
, encoder
);
303 drm_connector_register(connector
);
307 static struct drm_framebuffer
*
308 virtio_gpu_user_framebuffer_create(struct drm_device
*dev
,
309 struct drm_file
*file_priv
,
310 const struct drm_mode_fb_cmd2
*mode_cmd
)
312 struct drm_gem_object
*obj
= NULL
;
313 struct virtio_gpu_framebuffer
*virtio_gpu_fb
;
316 /* lookup object associated with res handle */
317 obj
= drm_gem_object_lookup(file_priv
, mode_cmd
->handles
[0]);
319 return ERR_PTR(-EINVAL
);
321 virtio_gpu_fb
= kzalloc(sizeof(*virtio_gpu_fb
), GFP_KERNEL
);
322 if (virtio_gpu_fb
== NULL
)
323 return ERR_PTR(-ENOMEM
);
325 ret
= virtio_gpu_framebuffer_init(dev
, virtio_gpu_fb
, mode_cmd
, obj
);
327 kfree(virtio_gpu_fb
);
328 drm_gem_object_unreference_unlocked(obj
);
332 return &virtio_gpu_fb
->base
;
335 static void vgdev_atomic_commit_tail(struct drm_atomic_state
*state
)
337 struct drm_device
*dev
= state
->dev
;
339 drm_atomic_helper_commit_modeset_disables(dev
, state
);
340 drm_atomic_helper_commit_modeset_enables(dev
, state
);
341 drm_atomic_helper_commit_planes(dev
, state
, 0);
343 drm_atomic_helper_commit_hw_done(state
);
345 drm_atomic_helper_wait_for_vblanks(dev
, state
);
346 drm_atomic_helper_cleanup_planes(dev
, state
);
349 static struct drm_mode_config_helper_funcs virtio_mode_config_helpers
= {
350 .atomic_commit_tail
= vgdev_atomic_commit_tail
,
353 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs
= {
354 .fb_create
= virtio_gpu_user_framebuffer_create
,
355 .atomic_check
= drm_atomic_helper_check
,
356 .atomic_commit
= drm_atomic_helper_commit
,
359 int virtio_gpu_modeset_init(struct virtio_gpu_device
*vgdev
)
363 drm_mode_config_init(vgdev
->ddev
);
364 vgdev
->ddev
->mode_config
.funcs
= &virtio_gpu_mode_funcs
;
365 vgdev
->ddev
->mode_config
.helper_private
= &virtio_mode_config_helpers
;
367 /* modes will be validated against the framebuffer size */
368 vgdev
->ddev
->mode_config
.min_width
= XRES_MIN
;
369 vgdev
->ddev
->mode_config
.min_height
= YRES_MIN
;
370 vgdev
->ddev
->mode_config
.max_width
= XRES_MAX
;
371 vgdev
->ddev
->mode_config
.max_height
= YRES_MAX
;
373 for (i
= 0 ; i
< vgdev
->num_scanouts
; ++i
)
374 vgdev_output_init(vgdev
, i
);
376 drm_mode_config_reset(vgdev
->ddev
);
380 void virtio_gpu_modeset_fini(struct virtio_gpu_device
*vgdev
)
382 virtio_gpu_fbdev_fini(vgdev
);
383 drm_mode_config_cleanup(vgdev
->ddev
);