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 void virtio_gpu_crtc_gamma_set(struct drm_crtc
*crtc
,
42 u16
*red
, u16
*green
, u16
*blue
,
43 uint32_t start
, uint32_t size
)
49 virtio_gpu_hide_cursor(struct virtio_gpu_device
*vgdev
,
50 struct virtio_gpu_output
*output
)
52 output
->cursor
.hdr
.type
= cpu_to_le32(VIRTIO_GPU_CMD_UPDATE_CURSOR
);
53 output
->cursor
.resource_id
= 0;
54 virtio_gpu_cursor_ping(vgdev
, output
);
57 static int virtio_gpu_crtc_cursor_set(struct drm_crtc
*crtc
,
58 struct drm_file
*file_priv
,
62 int32_t hot_x
, int32_t hot_y
)
64 struct virtio_gpu_device
*vgdev
= crtc
->dev
->dev_private
;
65 struct virtio_gpu_output
*output
=
66 container_of(crtc
, struct virtio_gpu_output
, crtc
);
67 struct drm_gem_object
*gobj
= NULL
;
68 struct virtio_gpu_object
*qobj
= NULL
;
69 struct virtio_gpu_fence
*fence
= NULL
;
73 virtio_gpu_hide_cursor(vgdev
, output
);
77 /* lookup the cursor */
78 gobj
= drm_gem_object_lookup(crtc
->dev
, file_priv
, handle
);
82 qobj
= gem_to_virtio_gpu_obj(gobj
);
84 if (!qobj
->hw_res_handle
) {
89 virtio_gpu_cmd_transfer_to_host_2d(vgdev
, qobj
->hw_res_handle
, 0,
94 output
->cursor
.hdr
.type
= cpu_to_le32(VIRTIO_GPU_CMD_UPDATE_CURSOR
);
95 output
->cursor
.resource_id
= cpu_to_le32(qobj
->hw_res_handle
);
96 output
->cursor
.hot_x
= cpu_to_le32(hot_x
);
97 output
->cursor
.hot_y
= cpu_to_le32(hot_y
);
98 virtio_gpu_cursor_ping(vgdev
, output
);
102 drm_gem_object_unreference_unlocked(gobj
);
106 static int virtio_gpu_crtc_cursor_move(struct drm_crtc
*crtc
,
109 struct virtio_gpu_device
*vgdev
= crtc
->dev
->dev_private
;
110 struct virtio_gpu_output
*output
=
111 container_of(crtc
, struct virtio_gpu_output
, crtc
);
113 output
->cursor
.hdr
.type
= cpu_to_le32(VIRTIO_GPU_CMD_MOVE_CURSOR
);
114 output
->cursor
.pos
.x
= cpu_to_le32(x
);
115 output
->cursor
.pos
.y
= cpu_to_le32(y
);
116 virtio_gpu_cursor_ping(vgdev
, output
);
120 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs
= {
121 .cursor_set2
= virtio_gpu_crtc_cursor_set
,
122 .cursor_move
= virtio_gpu_crtc_cursor_move
,
123 .gamma_set
= virtio_gpu_crtc_gamma_set
,
124 .set_config
= drm_atomic_helper_set_config
,
125 .destroy
= drm_crtc_cleanup
,
127 #if 0 /* not (yet) working without vblank support according to docs */
128 .page_flip
= drm_atomic_helper_page_flip
,
130 .reset
= drm_atomic_helper_crtc_reset
,
131 .atomic_duplicate_state
= drm_atomic_helper_crtc_duplicate_state
,
132 .atomic_destroy_state
= drm_atomic_helper_crtc_destroy_state
,
135 static void virtio_gpu_user_framebuffer_destroy(struct drm_framebuffer
*fb
)
137 struct virtio_gpu_framebuffer
*virtio_gpu_fb
138 = to_virtio_gpu_framebuffer(fb
);
140 if (virtio_gpu_fb
->obj
)
141 drm_gem_object_unreference_unlocked(virtio_gpu_fb
->obj
);
142 drm_framebuffer_cleanup(fb
);
143 kfree(virtio_gpu_fb
);
147 virtio_gpu_framebuffer_surface_dirty(struct drm_framebuffer
*fb
,
148 struct drm_file
*file_priv
,
149 unsigned flags
, unsigned color
,
150 struct drm_clip_rect
*clips
,
153 struct virtio_gpu_framebuffer
*virtio_gpu_fb
154 = to_virtio_gpu_framebuffer(fb
);
156 return virtio_gpu_surface_dirty(virtio_gpu_fb
, clips
, num_clips
);
159 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs
= {
160 .destroy
= virtio_gpu_user_framebuffer_destroy
,
161 .dirty
= virtio_gpu_framebuffer_surface_dirty
,
165 virtio_gpu_framebuffer_init(struct drm_device
*dev
,
166 struct virtio_gpu_framebuffer
*vgfb
,
167 struct drm_mode_fb_cmd2
*mode_cmd
,
168 struct drm_gem_object
*obj
)
171 struct virtio_gpu_object
*bo
;
174 bo
= gem_to_virtio_gpu_obj(obj
);
176 ret
= drm_framebuffer_init(dev
, &vgfb
->base
, &virtio_gpu_fb_funcs
);
181 drm_helper_mode_fill_fb_struct(&vgfb
->base
, mode_cmd
);
183 spin_lock_init(&vgfb
->dirty_lock
);
184 vgfb
->x1
= vgfb
->y1
= INT_MAX
;
185 vgfb
->x2
= vgfb
->y2
= 0;
189 static bool virtio_gpu_crtc_mode_fixup(struct drm_crtc
*crtc
,
190 const struct drm_display_mode
*mode
,
191 struct drm_display_mode
*adjusted_mode
)
196 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc
*crtc
)
198 struct drm_device
*dev
= crtc
->dev
;
199 struct virtio_gpu_device
*vgdev
= dev
->dev_private
;
200 struct virtio_gpu_output
*output
= drm_crtc_to_virtio_gpu_output(crtc
);
202 virtio_gpu_cmd_set_scanout(vgdev
, output
->index
, 0,
204 crtc
->mode
.vdisplay
, 0, 0);
207 static void virtio_gpu_crtc_enable(struct drm_crtc
*crtc
)
211 static void virtio_gpu_crtc_disable(struct drm_crtc
*crtc
)
213 struct drm_device
*dev
= crtc
->dev
;
214 struct virtio_gpu_device
*vgdev
= dev
->dev_private
;
215 struct virtio_gpu_output
*output
= drm_crtc_to_virtio_gpu_output(crtc
);
217 virtio_gpu_cmd_set_scanout(vgdev
, output
->index
, 0, 0, 0, 0, 0);
220 static int virtio_gpu_crtc_atomic_check(struct drm_crtc
*crtc
,
221 struct drm_crtc_state
*state
)
226 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs
= {
227 .enable
= virtio_gpu_crtc_enable
,
228 .disable
= virtio_gpu_crtc_disable
,
229 .mode_fixup
= virtio_gpu_crtc_mode_fixup
,
230 .mode_set_nofb
= virtio_gpu_crtc_mode_set_nofb
,
231 .atomic_check
= virtio_gpu_crtc_atomic_check
,
234 static bool virtio_gpu_enc_mode_fixup(struct drm_encoder
*encoder
,
235 const struct drm_display_mode
*mode
,
236 struct drm_display_mode
*adjusted_mode
)
241 static void virtio_gpu_enc_mode_set(struct drm_encoder
*encoder
,
242 struct drm_display_mode
*mode
,
243 struct drm_display_mode
*adjusted_mode
)
247 static void virtio_gpu_enc_enable(struct drm_encoder
*encoder
)
251 static void virtio_gpu_enc_disable(struct drm_encoder
*encoder
)
255 static int virtio_gpu_conn_get_modes(struct drm_connector
*connector
)
257 struct virtio_gpu_output
*output
=
258 drm_connector_to_virtio_gpu_output(connector
);
259 struct drm_display_mode
*mode
= NULL
;
260 int count
, width
, height
;
262 width
= le32_to_cpu(output
->info
.r
.width
);
263 height
= le32_to_cpu(output
->info
.r
.height
);
264 count
= drm_add_modes_noedid(connector
, XRES_MAX
, YRES_MAX
);
266 if (width
== 0 || height
== 0) {
269 drm_set_preferred_mode(connector
, XRES_DEF
, YRES_DEF
);
271 DRM_DEBUG("add mode: %dx%d\n", width
, height
);
272 mode
= drm_cvt_mode(connector
->dev
, width
, height
, 60,
273 false, false, false);
274 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
275 drm_mode_probed_add(connector
, mode
);
282 static int virtio_gpu_conn_mode_valid(struct drm_connector
*connector
,
283 struct drm_display_mode
*mode
)
285 struct virtio_gpu_output
*output
=
286 drm_connector_to_virtio_gpu_output(connector
);
289 width
= le32_to_cpu(output
->info
.r
.width
);
290 height
= le32_to_cpu(output
->info
.r
.height
);
292 if (!(mode
->type
& DRM_MODE_TYPE_PREFERRED
))
294 if (mode
->hdisplay
== XRES_DEF
&& mode
->vdisplay
== YRES_DEF
)
296 if (mode
->hdisplay
<= width
&& mode
->hdisplay
>= width
- 16 &&
297 mode
->vdisplay
<= height
&& mode
->vdisplay
>= height
- 16)
300 DRM_DEBUG("del mode: %dx%d\n", mode
->hdisplay
, mode
->vdisplay
);
304 static struct drm_encoder
*
305 virtio_gpu_best_encoder(struct drm_connector
*connector
)
307 struct virtio_gpu_output
*virtio_gpu_output
=
308 drm_connector_to_virtio_gpu_output(connector
);
310 return &virtio_gpu_output
->enc
;
313 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs
= {
314 .mode_fixup
= virtio_gpu_enc_mode_fixup
,
315 .mode_set
= virtio_gpu_enc_mode_set
,
316 .enable
= virtio_gpu_enc_enable
,
317 .disable
= virtio_gpu_enc_disable
,
320 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs
= {
321 .get_modes
= virtio_gpu_conn_get_modes
,
322 .mode_valid
= virtio_gpu_conn_mode_valid
,
323 .best_encoder
= virtio_gpu_best_encoder
,
326 static void virtio_gpu_conn_save(struct drm_connector
*connector
)
331 static void virtio_gpu_conn_restore(struct drm_connector
*connector
)
336 static enum drm_connector_status
virtio_gpu_conn_detect(
337 struct drm_connector
*connector
,
340 struct virtio_gpu_output
*output
=
341 drm_connector_to_virtio_gpu_output(connector
);
343 if (output
->info
.enabled
)
344 return connector_status_connected
;
346 return connector_status_disconnected
;
349 static void virtio_gpu_conn_destroy(struct drm_connector
*connector
)
351 struct virtio_gpu_output
*virtio_gpu_output
=
352 drm_connector_to_virtio_gpu_output(connector
);
354 drm_connector_unregister(connector
);
355 drm_connector_cleanup(connector
);
356 kfree(virtio_gpu_output
);
359 static const struct drm_connector_funcs virtio_gpu_connector_funcs
= {
360 .dpms
= drm_atomic_helper_connector_dpms
,
361 .save
= virtio_gpu_conn_save
,
362 .restore
= virtio_gpu_conn_restore
,
363 .detect
= virtio_gpu_conn_detect
,
364 .fill_modes
= drm_helper_probe_single_connector_modes
,
365 .destroy
= virtio_gpu_conn_destroy
,
366 .reset
= drm_atomic_helper_connector_reset
,
367 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
368 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
371 static const struct drm_encoder_funcs virtio_gpu_enc_funcs
= {
372 .destroy
= drm_encoder_cleanup
,
375 static int vgdev_output_init(struct virtio_gpu_device
*vgdev
, int index
)
377 struct drm_device
*dev
= vgdev
->ddev
;
378 struct virtio_gpu_output
*output
= vgdev
->outputs
+ index
;
379 struct drm_connector
*connector
= &output
->conn
;
380 struct drm_encoder
*encoder
= &output
->enc
;
381 struct drm_crtc
*crtc
= &output
->crtc
;
382 struct drm_plane
*plane
;
384 output
->index
= index
;
386 output
->info
.enabled
= cpu_to_le32(true);
387 output
->info
.r
.width
= cpu_to_le32(XRES_DEF
);
388 output
->info
.r
.height
= cpu_to_le32(YRES_DEF
);
391 plane
= virtio_gpu_plane_init(vgdev
, index
);
393 return PTR_ERR(plane
);
394 drm_crtc_init_with_planes(dev
, crtc
, plane
, NULL
,
395 &virtio_gpu_crtc_funcs
);
396 drm_mode_crtc_set_gamma_size(crtc
, 256);
397 drm_crtc_helper_add(crtc
, &virtio_gpu_crtc_helper_funcs
);
400 drm_connector_init(dev
, connector
, &virtio_gpu_connector_funcs
,
401 DRM_MODE_CONNECTOR_VIRTUAL
);
402 drm_connector_helper_add(connector
, &virtio_gpu_conn_helper_funcs
);
404 drm_encoder_init(dev
, encoder
, &virtio_gpu_enc_funcs
,
405 DRM_MODE_ENCODER_VIRTUAL
);
406 drm_encoder_helper_add(encoder
, &virtio_gpu_enc_helper_funcs
);
407 encoder
->possible_crtcs
= 1 << index
;
409 drm_mode_connector_attach_encoder(connector
, encoder
);
410 drm_connector_register(connector
);
414 static struct drm_framebuffer
*
415 virtio_gpu_user_framebuffer_create(struct drm_device
*dev
,
416 struct drm_file
*file_priv
,
417 struct drm_mode_fb_cmd2
*mode_cmd
)
419 struct drm_gem_object
*obj
= NULL
;
420 struct virtio_gpu_framebuffer
*virtio_gpu_fb
;
423 /* lookup object associated with res handle */
424 obj
= drm_gem_object_lookup(dev
, file_priv
, mode_cmd
->handles
[0]);
426 return ERR_PTR(-EINVAL
);
428 virtio_gpu_fb
= kzalloc(sizeof(*virtio_gpu_fb
), GFP_KERNEL
);
429 if (virtio_gpu_fb
== NULL
)
430 return ERR_PTR(-ENOMEM
);
432 ret
= virtio_gpu_framebuffer_init(dev
, virtio_gpu_fb
, mode_cmd
, obj
);
434 kfree(virtio_gpu_fb
);
436 drm_gem_object_unreference_unlocked(obj
);
440 return &virtio_gpu_fb
->base
;
443 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs
= {
444 .fb_create
= virtio_gpu_user_framebuffer_create
,
445 .atomic_check
= drm_atomic_helper_check
,
446 .atomic_commit
= drm_atomic_helper_commit
,
449 int virtio_gpu_modeset_init(struct virtio_gpu_device
*vgdev
)
453 drm_mode_config_init(vgdev
->ddev
);
454 vgdev
->ddev
->mode_config
.funcs
= (void *)&virtio_gpu_mode_funcs
;
456 /* modes will be validated against the framebuffer size */
457 vgdev
->ddev
->mode_config
.min_width
= XRES_MIN
;
458 vgdev
->ddev
->mode_config
.min_height
= YRES_MIN
;
459 vgdev
->ddev
->mode_config
.max_width
= XRES_MAX
;
460 vgdev
->ddev
->mode_config
.max_height
= YRES_MAX
;
462 for (i
= 0 ; i
< vgdev
->num_scanouts
; ++i
)
463 vgdev_output_init(vgdev
, i
);
465 drm_mode_config_reset(vgdev
->ddev
);
469 void virtio_gpu_modeset_fini(struct virtio_gpu_device
*vgdev
)
471 virtio_gpu_fbdev_fini(vgdev
);
472 drm_mode_config_cleanup(vgdev
->ddev
);