1 // SPDX-License-Identifier: GPL-2.0+
3 #include <drm/drm_atomic_helper.h>
4 #include <drm/drm_edid.h>
5 #include <drm/drm_simple_kms_helper.h>
6 #include <drm/drm_vblank.h>
9 #ifdef CONFIG_DRM_AMDGPU_SI
12 #ifdef CONFIG_DRM_AMDGPU_CIK
15 #include "dce_v10_0.h"
16 #include "dce_v11_0.h"
17 #include "ivsrcid/ivsrcid_vislands30.h"
18 #include "amdgpu_vkms.h"
19 #include "amdgpu_display.h"
21 #include "amdgpu_irq.h"
26 * The amdgpu vkms interface provides a virtual KMS interface for several use
27 * cases: devices without display hardware, platforms where the actual display
28 * hardware is not useful (e.g., servers), SR-IOV virtual functions, device
29 * emulation/simulation, and device bring up prior to display hardware being
30 * usable. We previously emulated a legacy KMS interface, but there was a desire
31 * to move to the atomic KMS interface. The vkms driver did everything we
32 * needed, but we wanted KMS support natively in the driver without buffer
33 * sharing and the ability to support an instance of VKMS per device. We first
34 * looked at splitting vkms into a stub driver and a helper module that other
35 * drivers could use to implement a virtual display, but this strategy ended up
36 * being messy due to driver specific callbacks needed for buffer management.
37 * Ultimately, it proved easier to import the vkms code as it mostly used core
41 static const u32 amdgpu_vkms_formats[] = {
45 static enum hrtimer_restart amdgpu_vkms_vblank_simulate(struct hrtimer *timer)
47 struct amdgpu_crtc *amdgpu_crtc = container_of(timer, struct amdgpu_crtc, vblank_timer);
48 struct drm_crtc *crtc = &amdgpu_crtc->base;
49 struct amdgpu_vkms_output *output = drm_crtc_to_amdgpu_vkms_output(crtc);
53 ret_overrun = hrtimer_forward_now(&amdgpu_crtc->vblank_timer,
56 DRM_WARN("%s: vblank timer overrun\n", __func__);
58 ret = drm_crtc_handle_vblank(crtc);
59 /* Don't queue timer again when vblank is disabled. */
61 return HRTIMER_NORESTART;
63 return HRTIMER_RESTART;
66 static int amdgpu_vkms_enable_vblank(struct drm_crtc *crtc)
68 struct drm_device *dev = crtc->dev;
69 unsigned int pipe = drm_crtc_index(crtc);
70 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
71 struct amdgpu_vkms_output *out = drm_crtc_to_amdgpu_vkms_output(crtc);
72 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
74 drm_calc_timestamping_constants(crtc, &crtc->mode);
76 out->period_ns = ktime_set(0, vblank->framedur_ns);
77 hrtimer_start(&amdgpu_crtc->vblank_timer, out->period_ns, HRTIMER_MODE_REL);
82 static void amdgpu_vkms_disable_vblank(struct drm_crtc *crtc)
84 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
86 hrtimer_try_to_cancel(&amdgpu_crtc->vblank_timer);
89 static bool amdgpu_vkms_get_vblank_timestamp(struct drm_crtc *crtc,
94 struct drm_device *dev = crtc->dev;
95 unsigned int pipe = crtc->index;
96 struct amdgpu_vkms_output *output = drm_crtc_to_amdgpu_vkms_output(crtc);
97 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
98 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
100 if (!READ_ONCE(vblank->enabled)) {
101 *vblank_time = ktime_get();
105 *vblank_time = READ_ONCE(amdgpu_crtc->vblank_timer.node.expires);
107 if (WARN_ON(*vblank_time == vblank->time))
111 * To prevent races we roll the hrtimer forward before we do any
112 * interrupt processing - this is how real hw works (the interrupt is
113 * only generated after all the vblank registers are updated) and what
114 * the vblank core expects. Therefore we need to always correct the
115 * timestampe by one frame.
117 *vblank_time -= output->period_ns;
122 static const struct drm_crtc_funcs amdgpu_vkms_crtc_funcs = {
123 .set_config = drm_atomic_helper_set_config,
124 .destroy = drm_crtc_cleanup,
125 .page_flip = drm_atomic_helper_page_flip,
126 .reset = drm_atomic_helper_crtc_reset,
127 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
128 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
129 .enable_vblank = amdgpu_vkms_enable_vblank,
130 .disable_vblank = amdgpu_vkms_disable_vblank,
131 .get_vblank_timestamp = amdgpu_vkms_get_vblank_timestamp,
134 static void amdgpu_vkms_crtc_atomic_enable(struct drm_crtc *crtc,
135 struct drm_atomic_state *state)
137 drm_crtc_vblank_on(crtc);
140 static void amdgpu_vkms_crtc_atomic_disable(struct drm_crtc *crtc,
141 struct drm_atomic_state *state)
143 drm_crtc_vblank_off(crtc);
146 static void amdgpu_vkms_crtc_atomic_flush(struct drm_crtc *crtc,
147 struct drm_atomic_state *state)
150 if (crtc->state->event) {
151 spin_lock_irqsave(&crtc->dev->event_lock, flags);
153 if (drm_crtc_vblank_get(crtc) != 0)
154 drm_crtc_send_vblank_event(crtc, crtc->state->event);
156 drm_crtc_arm_vblank_event(crtc, crtc->state->event);
158 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
160 crtc->state->event = NULL;
164 static const struct drm_crtc_helper_funcs amdgpu_vkms_crtc_helper_funcs = {
165 .atomic_flush = amdgpu_vkms_crtc_atomic_flush,
166 .atomic_enable = amdgpu_vkms_crtc_atomic_enable,
167 .atomic_disable = amdgpu_vkms_crtc_atomic_disable,
170 static int amdgpu_vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
171 struct drm_plane *primary, struct drm_plane *cursor)
173 struct amdgpu_device *adev = drm_to_adev(dev);
174 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
177 ret = drm_crtc_init_with_planes(dev, crtc, primary, cursor,
178 &amdgpu_vkms_crtc_funcs, NULL);
180 DRM_ERROR("Failed to init CRTC\n");
184 drm_crtc_helper_add(crtc, &amdgpu_vkms_crtc_helper_funcs);
186 amdgpu_crtc->crtc_id = drm_crtc_index(crtc);
187 adev->mode_info.crtcs[drm_crtc_index(crtc)] = amdgpu_crtc;
189 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
190 amdgpu_crtc->encoder = NULL;
191 amdgpu_crtc->connector = NULL;
192 amdgpu_crtc->vsync_timer_enabled = AMDGPU_IRQ_STATE_DISABLE;
194 hrtimer_init(&amdgpu_crtc->vblank_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
195 amdgpu_crtc->vblank_timer.function = &amdgpu_vkms_vblank_simulate;
200 static const struct drm_connector_funcs amdgpu_vkms_connector_funcs = {
201 .fill_modes = drm_helper_probe_single_connector_modes,
202 .destroy = drm_connector_cleanup,
203 .reset = drm_atomic_helper_connector_reset,
204 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
205 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
208 static int amdgpu_vkms_conn_get_modes(struct drm_connector *connector)
210 struct drm_device *dev = connector->dev;
211 struct drm_display_mode *mode = NULL;
213 static const struct mode_size {
241 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
242 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
245 drm_mode_probed_add(connector, mode);
248 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
250 return ARRAY_SIZE(common_modes);
253 static const struct drm_connector_helper_funcs amdgpu_vkms_conn_helper_funcs = {
254 .get_modes = amdgpu_vkms_conn_get_modes,
257 static const struct drm_plane_funcs amdgpu_vkms_plane_funcs = {
258 .update_plane = drm_atomic_helper_update_plane,
259 .disable_plane = drm_atomic_helper_disable_plane,
260 .destroy = drm_plane_cleanup,
261 .reset = drm_atomic_helper_plane_reset,
262 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
263 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
266 static void amdgpu_vkms_plane_atomic_update(struct drm_plane *plane,
267 struct drm_atomic_state *old_state)
272 static int amdgpu_vkms_plane_atomic_check(struct drm_plane *plane,
273 struct drm_atomic_state *state)
275 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
277 struct drm_crtc_state *crtc_state;
280 if (!new_plane_state->fb || WARN_ON(!new_plane_state->crtc))
283 crtc_state = drm_atomic_get_crtc_state(state,
284 new_plane_state->crtc);
285 if (IS_ERR(crtc_state))
286 return PTR_ERR(crtc_state);
288 ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
289 DRM_PLANE_NO_SCALING,
290 DRM_PLANE_NO_SCALING,
295 /* for now primary plane must be visible and full screen */
296 if (!new_plane_state->visible)
302 static int amdgpu_vkms_prepare_fb(struct drm_plane *plane,
303 struct drm_plane_state *new_state)
305 struct amdgpu_framebuffer *afb;
306 struct drm_gem_object *obj;
307 struct amdgpu_device *adev;
308 struct amdgpu_bo *rbo;
312 if (!new_state->fb) {
313 DRM_DEBUG_KMS("No FB bound\n");
316 afb = to_amdgpu_framebuffer(new_state->fb);
317 obj = new_state->fb->obj[0];
318 rbo = gem_to_amdgpu_bo(obj);
319 adev = amdgpu_ttm_adev(rbo->tbo.bdev);
321 r = amdgpu_bo_reserve(rbo, true);
323 dev_err(adev->dev, "fail to reserve bo (%d)\n", r);
327 r = dma_resv_reserve_fences(rbo->tbo.base.resv, 1);
329 dev_err(adev->dev, "allocating fence slot failed (%d)\n", r);
333 if (plane->type != DRM_PLANE_TYPE_CURSOR)
334 domain = amdgpu_display_supported_domains(adev, rbo->flags);
336 domain = AMDGPU_GEM_DOMAIN_VRAM;
338 r = amdgpu_bo_pin(rbo, domain);
339 if (unlikely(r != 0)) {
340 if (r != -ERESTARTSYS)
341 DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
345 r = amdgpu_ttm_alloc_gart(&rbo->tbo);
346 if (unlikely(r != 0)) {
347 DRM_ERROR("%p bind failed\n", rbo);
351 amdgpu_bo_unreserve(rbo);
353 afb->address = amdgpu_bo_gpu_offset(rbo);
360 amdgpu_bo_unpin(rbo);
363 amdgpu_bo_unreserve(rbo);
367 static void amdgpu_vkms_cleanup_fb(struct drm_plane *plane,
368 struct drm_plane_state *old_state)
370 struct amdgpu_bo *rbo;
376 rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
377 r = amdgpu_bo_reserve(rbo, false);
379 DRM_ERROR("failed to reserve rbo before unpin\n");
383 amdgpu_bo_unpin(rbo);
384 amdgpu_bo_unreserve(rbo);
385 amdgpu_bo_unref(&rbo);
388 static const struct drm_plane_helper_funcs amdgpu_vkms_primary_helper_funcs = {
389 .atomic_update = amdgpu_vkms_plane_atomic_update,
390 .atomic_check = amdgpu_vkms_plane_atomic_check,
391 .prepare_fb = amdgpu_vkms_prepare_fb,
392 .cleanup_fb = amdgpu_vkms_cleanup_fb,
395 static struct drm_plane *amdgpu_vkms_plane_init(struct drm_device *dev,
396 enum drm_plane_type type,
399 struct drm_plane *plane;
402 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
404 return ERR_PTR(-ENOMEM);
406 ret = drm_universal_plane_init(dev, plane, 1 << index,
407 &amdgpu_vkms_plane_funcs,
409 ARRAY_SIZE(amdgpu_vkms_formats),
416 drm_plane_helper_add(plane, &amdgpu_vkms_primary_helper_funcs);
421 static int amdgpu_vkms_output_init(struct drm_device *dev, struct
422 amdgpu_vkms_output *output, int index)
424 struct drm_connector *connector = &output->connector;
425 struct drm_encoder *encoder = &output->encoder;
426 struct drm_crtc *crtc = &output->crtc.base;
427 struct drm_plane *primary, *cursor = NULL;
430 primary = amdgpu_vkms_plane_init(dev, DRM_PLANE_TYPE_PRIMARY, index);
432 return PTR_ERR(primary);
434 ret = amdgpu_vkms_crtc_init(dev, crtc, primary, cursor);
438 ret = drm_connector_init(dev, connector, &amdgpu_vkms_connector_funcs,
439 DRM_MODE_CONNECTOR_VIRTUAL);
441 DRM_ERROR("Failed to init connector\n");
445 drm_connector_helper_add(connector, &amdgpu_vkms_conn_helper_funcs);
447 ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_VIRTUAL);
449 DRM_ERROR("Failed to init encoder\n");
452 encoder->possible_crtcs = 1 << index;
454 ret = drm_connector_attach_encoder(connector, encoder);
456 DRM_ERROR("Failed to attach connector to encoder\n");
460 drm_mode_config_reset(dev);
465 drm_encoder_cleanup(encoder);
468 drm_connector_cleanup(connector);
471 drm_crtc_cleanup(crtc);
474 drm_plane_cleanup(primary);
479 const struct drm_mode_config_funcs amdgpu_vkms_mode_funcs = {
480 .fb_create = amdgpu_display_user_framebuffer_create,
481 .atomic_check = drm_atomic_helper_check,
482 .atomic_commit = drm_atomic_helper_commit,
485 static int amdgpu_vkms_sw_init(void *handle)
488 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
490 adev->amdgpu_vkms_output = kcalloc(adev->mode_info.num_crtc,
491 sizeof(struct amdgpu_vkms_output), GFP_KERNEL);
492 if (!adev->amdgpu_vkms_output)
495 adev_to_drm(adev)->max_vblank_count = 0;
497 adev_to_drm(adev)->mode_config.funcs = &amdgpu_vkms_mode_funcs;
499 adev_to_drm(adev)->mode_config.max_width = XRES_MAX;
500 adev_to_drm(adev)->mode_config.max_height = YRES_MAX;
502 adev_to_drm(adev)->mode_config.preferred_depth = 24;
503 adev_to_drm(adev)->mode_config.prefer_shadow = 1;
505 adev_to_drm(adev)->mode_config.fb_modifiers_not_supported = true;
507 r = amdgpu_display_modeset_create_props(adev);
511 /* allocate crtcs, encoders, connectors */
512 for (i = 0; i < adev->mode_info.num_crtc; i++) {
513 r = amdgpu_vkms_output_init(adev_to_drm(adev), &adev->amdgpu_vkms_output[i], i);
518 r = drm_vblank_init(adev_to_drm(adev), adev->mode_info.num_crtc);
522 drm_kms_helper_poll_init(adev_to_drm(adev));
524 adev->mode_info.mode_config_initialized = true;
528 static int amdgpu_vkms_sw_fini(void *handle)
530 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
533 for (i = 0; i < adev->mode_info.num_crtc; i++)
534 if (adev->mode_info.crtcs[i])
535 hrtimer_cancel(&adev->mode_info.crtcs[i]->vblank_timer);
537 drm_kms_helper_poll_fini(adev_to_drm(adev));
538 drm_mode_config_cleanup(adev_to_drm(adev));
540 adev->mode_info.mode_config_initialized = false;
542 kfree(adev->mode_info.bios_hardcoded_edid);
543 kfree(adev->amdgpu_vkms_output);
547 static int amdgpu_vkms_hw_init(void *handle)
549 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
551 switch (adev->asic_type) {
552 #ifdef CONFIG_DRM_AMDGPU_SI
557 dce_v6_0_disable_dce(adev);
560 #ifdef CONFIG_DRM_AMDGPU_CIK
566 dce_v8_0_disable_dce(adev);
571 dce_v10_0_disable_dce(adev);
578 dce_v11_0_disable_dce(adev);
581 #ifdef CONFIG_DRM_AMDGPU_SI
592 static int amdgpu_vkms_hw_fini(void *handle)
597 static int amdgpu_vkms_suspend(void *handle)
599 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
602 r = drm_mode_config_helper_suspend(adev_to_drm(adev));
605 return amdgpu_vkms_hw_fini(handle);
608 static int amdgpu_vkms_resume(void *handle)
610 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
613 r = amdgpu_vkms_hw_init(handle);
616 return drm_mode_config_helper_resume(adev_to_drm(adev));
619 static bool amdgpu_vkms_is_idle(void *handle)
624 static int amdgpu_vkms_wait_for_idle(void *handle)
629 static int amdgpu_vkms_soft_reset(void *handle)
634 static int amdgpu_vkms_set_clockgating_state(void *handle,
635 enum amd_clockgating_state state)
640 static int amdgpu_vkms_set_powergating_state(void *handle,
641 enum amd_powergating_state state)
646 static const struct amd_ip_funcs amdgpu_vkms_ip_funcs = {
647 .name = "amdgpu_vkms",
650 .sw_init = amdgpu_vkms_sw_init,
651 .sw_fini = amdgpu_vkms_sw_fini,
652 .hw_init = amdgpu_vkms_hw_init,
653 .hw_fini = amdgpu_vkms_hw_fini,
654 .suspend = amdgpu_vkms_suspend,
655 .resume = amdgpu_vkms_resume,
656 .is_idle = amdgpu_vkms_is_idle,
657 .wait_for_idle = amdgpu_vkms_wait_for_idle,
658 .soft_reset = amdgpu_vkms_soft_reset,
659 .set_clockgating_state = amdgpu_vkms_set_clockgating_state,
660 .set_powergating_state = amdgpu_vkms_set_powergating_state,
663 const struct amdgpu_ip_block_version amdgpu_vkms_ip_block = {
664 .type = AMD_IP_BLOCK_TYPE_DCE,
668 .funcs = &amdgpu_vkms_ip_funcs,