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_put_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
);
75 virtio_gpu_framebuffer_create_handle(struct drm_framebuffer
*fb
,
76 struct drm_file
*file_priv
,
79 struct virtio_gpu_framebuffer
*virtio_gpu_fb
=
80 to_virtio_gpu_framebuffer(fb
);
82 return drm_gem_handle_create(file_priv
, virtio_gpu_fb
->obj
, handle
);
85 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs
= {
86 .create_handle
= virtio_gpu_framebuffer_create_handle
,
87 .destroy
= virtio_gpu_user_framebuffer_destroy
,
88 .dirty
= virtio_gpu_framebuffer_surface_dirty
,
92 virtio_gpu_framebuffer_init(struct drm_device
*dev
,
93 struct virtio_gpu_framebuffer
*vgfb
,
94 const struct drm_mode_fb_cmd2
*mode_cmd
,
95 struct drm_gem_object
*obj
)
98 struct virtio_gpu_object
*bo
;
101 bo
= gem_to_virtio_gpu_obj(obj
);
103 drm_helper_mode_fill_fb_struct(dev
, &vgfb
->base
, mode_cmd
);
105 ret
= drm_framebuffer_init(dev
, &vgfb
->base
, &virtio_gpu_fb_funcs
);
111 spin_lock_init(&vgfb
->dirty_lock
);
112 vgfb
->x1
= vgfb
->y1
= INT_MAX
;
113 vgfb
->x2
= vgfb
->y2
= 0;
117 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc
*crtc
)
119 struct drm_device
*dev
= crtc
->dev
;
120 struct virtio_gpu_device
*vgdev
= dev
->dev_private
;
121 struct virtio_gpu_output
*output
= drm_crtc_to_virtio_gpu_output(crtc
);
123 virtio_gpu_cmd_set_scanout(vgdev
, output
->index
, 0,
125 crtc
->mode
.vdisplay
, 0, 0);
128 static void virtio_gpu_crtc_atomic_enable(struct drm_crtc
*crtc
,
129 struct drm_crtc_state
*old_state
)
133 static void virtio_gpu_crtc_atomic_disable(struct drm_crtc
*crtc
,
134 struct drm_crtc_state
*old_state
)
136 struct drm_device
*dev
= crtc
->dev
;
137 struct virtio_gpu_device
*vgdev
= dev
->dev_private
;
138 struct virtio_gpu_output
*output
= drm_crtc_to_virtio_gpu_output(crtc
);
140 virtio_gpu_cmd_set_scanout(vgdev
, output
->index
, 0, 0, 0, 0, 0);
143 static int virtio_gpu_crtc_atomic_check(struct drm_crtc
*crtc
,
144 struct drm_crtc_state
*state
)
149 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc
*crtc
,
150 struct drm_crtc_state
*old_state
)
154 spin_lock_irqsave(&crtc
->dev
->event_lock
, flags
);
155 if (crtc
->state
->event
)
156 drm_crtc_send_vblank_event(crtc
, crtc
->state
->event
);
157 crtc
->state
->event
= NULL
;
158 spin_unlock_irqrestore(&crtc
->dev
->event_lock
, flags
);
161 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs
= {
162 .mode_set_nofb
= virtio_gpu_crtc_mode_set_nofb
,
163 .atomic_check
= virtio_gpu_crtc_atomic_check
,
164 .atomic_flush
= virtio_gpu_crtc_atomic_flush
,
165 .atomic_enable
= virtio_gpu_crtc_atomic_enable
,
166 .atomic_disable
= virtio_gpu_crtc_atomic_disable
,
169 static void virtio_gpu_enc_mode_set(struct drm_encoder
*encoder
,
170 struct drm_display_mode
*mode
,
171 struct drm_display_mode
*adjusted_mode
)
175 static void virtio_gpu_enc_enable(struct drm_encoder
*encoder
)
179 static void virtio_gpu_enc_disable(struct drm_encoder
*encoder
)
183 static int virtio_gpu_conn_get_modes(struct drm_connector
*connector
)
185 struct virtio_gpu_output
*output
=
186 drm_connector_to_virtio_gpu_output(connector
);
187 struct drm_display_mode
*mode
= NULL
;
188 int count
, width
, height
;
190 width
= le32_to_cpu(output
->info
.r
.width
);
191 height
= le32_to_cpu(output
->info
.r
.height
);
192 count
= drm_add_modes_noedid(connector
, XRES_MAX
, YRES_MAX
);
194 if (width
== 0 || height
== 0) {
197 drm_set_preferred_mode(connector
, XRES_DEF
, YRES_DEF
);
199 DRM_DEBUG("add mode: %dx%d\n", width
, height
);
200 mode
= drm_cvt_mode(connector
->dev
, width
, height
, 60,
201 false, false, false);
202 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
203 drm_mode_probed_add(connector
, mode
);
210 static int virtio_gpu_conn_mode_valid(struct drm_connector
*connector
,
211 struct drm_display_mode
*mode
)
213 struct virtio_gpu_output
*output
=
214 drm_connector_to_virtio_gpu_output(connector
);
217 width
= le32_to_cpu(output
->info
.r
.width
);
218 height
= le32_to_cpu(output
->info
.r
.height
);
220 if (!(mode
->type
& DRM_MODE_TYPE_PREFERRED
))
222 if (mode
->hdisplay
== XRES_DEF
&& mode
->vdisplay
== YRES_DEF
)
224 if (mode
->hdisplay
<= width
&& mode
->hdisplay
>= width
- 16 &&
225 mode
->vdisplay
<= height
&& mode
->vdisplay
>= height
- 16)
228 DRM_DEBUG("del mode: %dx%d\n", mode
->hdisplay
, mode
->vdisplay
);
232 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs
= {
233 .mode_set
= virtio_gpu_enc_mode_set
,
234 .enable
= virtio_gpu_enc_enable
,
235 .disable
= virtio_gpu_enc_disable
,
238 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs
= {
239 .get_modes
= virtio_gpu_conn_get_modes
,
240 .mode_valid
= virtio_gpu_conn_mode_valid
,
243 static enum drm_connector_status
virtio_gpu_conn_detect(
244 struct drm_connector
*connector
,
247 struct virtio_gpu_output
*output
=
248 drm_connector_to_virtio_gpu_output(connector
);
250 if (output
->info
.enabled
)
251 return connector_status_connected
;
253 return connector_status_disconnected
;
256 static void virtio_gpu_conn_destroy(struct drm_connector
*connector
)
258 struct virtio_gpu_output
*virtio_gpu_output
=
259 drm_connector_to_virtio_gpu_output(connector
);
261 drm_connector_unregister(connector
);
262 drm_connector_cleanup(connector
);
263 kfree(virtio_gpu_output
);
266 static const struct drm_connector_funcs virtio_gpu_connector_funcs
= {
267 .detect
= virtio_gpu_conn_detect
,
268 .fill_modes
= drm_helper_probe_single_connector_modes
,
269 .destroy
= virtio_gpu_conn_destroy
,
270 .reset
= drm_atomic_helper_connector_reset
,
271 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
272 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
275 static const struct drm_encoder_funcs virtio_gpu_enc_funcs
= {
276 .destroy
= drm_encoder_cleanup
,
279 static int vgdev_output_init(struct virtio_gpu_device
*vgdev
, int index
)
281 struct drm_device
*dev
= vgdev
->ddev
;
282 struct virtio_gpu_output
*output
= vgdev
->outputs
+ index
;
283 struct drm_connector
*connector
= &output
->conn
;
284 struct drm_encoder
*encoder
= &output
->enc
;
285 struct drm_crtc
*crtc
= &output
->crtc
;
286 struct drm_plane
*primary
, *cursor
;
288 output
->index
= index
;
290 output
->info
.enabled
= cpu_to_le32(true);
291 output
->info
.r
.width
= cpu_to_le32(XRES_DEF
);
292 output
->info
.r
.height
= cpu_to_le32(YRES_DEF
);
295 primary
= virtio_gpu_plane_init(vgdev
, DRM_PLANE_TYPE_PRIMARY
, index
);
297 return PTR_ERR(primary
);
298 cursor
= virtio_gpu_plane_init(vgdev
, DRM_PLANE_TYPE_CURSOR
, index
);
300 return PTR_ERR(cursor
);
301 drm_crtc_init_with_planes(dev
, crtc
, primary
, cursor
,
302 &virtio_gpu_crtc_funcs
, NULL
);
303 drm_crtc_helper_add(crtc
, &virtio_gpu_crtc_helper_funcs
);
304 primary
->crtc
= crtc
;
307 drm_connector_init(dev
, connector
, &virtio_gpu_connector_funcs
,
308 DRM_MODE_CONNECTOR_VIRTUAL
);
309 drm_connector_helper_add(connector
, &virtio_gpu_conn_helper_funcs
);
311 drm_encoder_init(dev
, encoder
, &virtio_gpu_enc_funcs
,
312 DRM_MODE_ENCODER_VIRTUAL
, NULL
);
313 drm_encoder_helper_add(encoder
, &virtio_gpu_enc_helper_funcs
);
314 encoder
->possible_crtcs
= 1 << index
;
316 drm_mode_connector_attach_encoder(connector
, encoder
);
317 drm_connector_register(connector
);
321 static struct drm_framebuffer
*
322 virtio_gpu_user_framebuffer_create(struct drm_device
*dev
,
323 struct drm_file
*file_priv
,
324 const struct drm_mode_fb_cmd2
*mode_cmd
)
326 struct drm_gem_object
*obj
= NULL
;
327 struct virtio_gpu_framebuffer
*virtio_gpu_fb
;
330 /* lookup object associated with res handle */
331 obj
= drm_gem_object_lookup(file_priv
, mode_cmd
->handles
[0]);
333 return ERR_PTR(-EINVAL
);
335 virtio_gpu_fb
= kzalloc(sizeof(*virtio_gpu_fb
), GFP_KERNEL
);
336 if (virtio_gpu_fb
== NULL
)
337 return ERR_PTR(-ENOMEM
);
339 ret
= virtio_gpu_framebuffer_init(dev
, virtio_gpu_fb
, mode_cmd
, obj
);
341 kfree(virtio_gpu_fb
);
342 drm_gem_object_put_unlocked(obj
);
346 return &virtio_gpu_fb
->base
;
349 static void vgdev_atomic_commit_tail(struct drm_atomic_state
*state
)
351 struct drm_device
*dev
= state
->dev
;
353 drm_atomic_helper_commit_modeset_disables(dev
, state
);
354 drm_atomic_helper_commit_modeset_enables(dev
, state
);
355 drm_atomic_helper_commit_planes(dev
, state
, 0);
357 drm_atomic_helper_commit_hw_done(state
);
359 drm_atomic_helper_wait_for_vblanks(dev
, state
);
360 drm_atomic_helper_cleanup_planes(dev
, state
);
363 static const struct drm_mode_config_helper_funcs virtio_mode_config_helpers
= {
364 .atomic_commit_tail
= vgdev_atomic_commit_tail
,
367 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs
= {
368 .fb_create
= virtio_gpu_user_framebuffer_create
,
369 .atomic_check
= drm_atomic_helper_check
,
370 .atomic_commit
= drm_atomic_helper_commit
,
373 int virtio_gpu_modeset_init(struct virtio_gpu_device
*vgdev
)
377 drm_mode_config_init(vgdev
->ddev
);
378 vgdev
->ddev
->mode_config
.funcs
= &virtio_gpu_mode_funcs
;
379 vgdev
->ddev
->mode_config
.helper_private
= &virtio_mode_config_helpers
;
381 /* modes will be validated against the framebuffer size */
382 vgdev
->ddev
->mode_config
.min_width
= XRES_MIN
;
383 vgdev
->ddev
->mode_config
.min_height
= YRES_MIN
;
384 vgdev
->ddev
->mode_config
.max_width
= XRES_MAX
;
385 vgdev
->ddev
->mode_config
.max_height
= YRES_MAX
;
387 for (i
= 0 ; i
< vgdev
->num_scanouts
; ++i
)
388 vgdev_output_init(vgdev
, i
);
390 drm_mode_config_reset(vgdev
->ddev
);
394 void virtio_gpu_modeset_fini(struct virtio_gpu_device
*vgdev
)
396 virtio_gpu_fbdev_fini(vgdev
);
397 drm_mode_config_cleanup(vgdev
->ddev
);