1 // SPDX-License-Identifier: GPL-2.0+
3 #include <drm/drm_atomic_helper.h>
4 #include <drm/drm_simple_kms_helper.h>
5 #include <drm/drm_vblank.h>
8 #ifdef CONFIG_DRM_AMDGPU_SI
11 #ifdef CONFIG_DRM_AMDGPU_CIK
14 #include "dce_v10_0.h"
15 #include "dce_v11_0.h"
16 #include "ivsrcid/ivsrcid_vislands30.h"
17 #include "amdgpu_vkms.h"
18 #include "amdgpu_display.h"
20 #include "amdgpu_irq.h"
25 * The amdgpu vkms interface provides a virtual KMS interface for several use
26 * cases: devices without display hardware, platforms where the actual display
27 * hardware is not useful (e.g., servers), SR-IOV virtual functions, device
28 * emulation/simulation, and device bring up prior to display hardware being
29 * usable. We previously emulated a legacy KMS interface, but there was a desire
30 * to move to the atomic KMS interface. The vkms driver did everything we
31 * needed, but we wanted KMS support natively in the driver without buffer
32 * sharing and the ability to support an instance of VKMS per device. We first
33 * looked at splitting vkms into a stub driver and a helper module that other
34 * drivers could use to implement a virtual display, but this strategy ended up
35 * being messy due to driver specific callbacks needed for buffer management.
36 * Ultimately, it proved easier to import the vkms code as it mostly used core
40 static const u32 amdgpu_vkms_formats[] = {
44 static enum hrtimer_restart amdgpu_vkms_vblank_simulate(struct hrtimer *timer)
46 struct amdgpu_crtc *amdgpu_crtc = container_of(timer, struct amdgpu_crtc, vblank_timer);
47 struct drm_crtc *crtc = &amdgpu_crtc->base;
48 struct amdgpu_vkms_output *output = drm_crtc_to_amdgpu_vkms_output(crtc);
52 ret_overrun = hrtimer_forward_now(&amdgpu_crtc->vblank_timer,
55 DRM_WARN("%s: vblank timer overrun\n", __func__);
57 ret = drm_crtc_handle_vblank(crtc);
59 DRM_ERROR("amdgpu_vkms failure on handling vblank");
61 return HRTIMER_RESTART;
64 static int amdgpu_vkms_enable_vblank(struct drm_crtc *crtc)
66 struct drm_device *dev = crtc->dev;
67 unsigned int pipe = drm_crtc_index(crtc);
68 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
69 struct amdgpu_vkms_output *out = drm_crtc_to_amdgpu_vkms_output(crtc);
70 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
72 drm_calc_timestamping_constants(crtc, &crtc->mode);
74 out->period_ns = ktime_set(0, vblank->framedur_ns);
75 hrtimer_start(&amdgpu_crtc->vblank_timer, out->period_ns, HRTIMER_MODE_REL);
80 static void amdgpu_vkms_disable_vblank(struct drm_crtc *crtc)
82 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
84 hrtimer_cancel(&amdgpu_crtc->vblank_timer);
87 static bool amdgpu_vkms_get_vblank_timestamp(struct drm_crtc *crtc,
92 struct drm_device *dev = crtc->dev;
93 unsigned int pipe = crtc->index;
94 struct amdgpu_vkms_output *output = drm_crtc_to_amdgpu_vkms_output(crtc);
95 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
96 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
98 if (!READ_ONCE(vblank->enabled)) {
99 *vblank_time = ktime_get();
103 *vblank_time = READ_ONCE(amdgpu_crtc->vblank_timer.node.expires);
105 if (WARN_ON(*vblank_time == vblank->time))
109 * To prevent races we roll the hrtimer forward before we do any
110 * interrupt processing - this is how real hw works (the interrupt is
111 * only generated after all the vblank registers are updated) and what
112 * the vblank core expects. Therefore we need to always correct the
113 * timestampe by one frame.
115 *vblank_time -= output->period_ns;
120 static const struct drm_crtc_funcs amdgpu_vkms_crtc_funcs = {
121 .set_config = drm_atomic_helper_set_config,
122 .destroy = drm_crtc_cleanup,
123 .page_flip = drm_atomic_helper_page_flip,
124 .reset = drm_atomic_helper_crtc_reset,
125 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
126 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
127 .enable_vblank = amdgpu_vkms_enable_vblank,
128 .disable_vblank = amdgpu_vkms_disable_vblank,
129 .get_vblank_timestamp = amdgpu_vkms_get_vblank_timestamp,
132 static void amdgpu_vkms_crtc_atomic_enable(struct drm_crtc *crtc,
133 struct drm_atomic_state *state)
135 drm_crtc_vblank_on(crtc);
138 static void amdgpu_vkms_crtc_atomic_disable(struct drm_crtc *crtc,
139 struct drm_atomic_state *state)
141 drm_crtc_vblank_off(crtc);
144 static void amdgpu_vkms_crtc_atomic_flush(struct drm_crtc *crtc,
145 struct drm_atomic_state *state)
148 if (crtc->state->event) {
149 spin_lock_irqsave(&crtc->dev->event_lock, flags);
151 if (drm_crtc_vblank_get(crtc) != 0)
152 drm_crtc_send_vblank_event(crtc, crtc->state->event);
154 drm_crtc_arm_vblank_event(crtc, crtc->state->event);
156 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
158 crtc->state->event = NULL;
162 static const struct drm_crtc_helper_funcs amdgpu_vkms_crtc_helper_funcs = {
163 .atomic_flush = amdgpu_vkms_crtc_atomic_flush,
164 .atomic_enable = amdgpu_vkms_crtc_atomic_enable,
165 .atomic_disable = amdgpu_vkms_crtc_atomic_disable,
168 static int amdgpu_vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
169 struct drm_plane *primary, struct drm_plane *cursor)
171 struct amdgpu_device *adev = drm_to_adev(dev);
172 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
175 ret = drm_crtc_init_with_planes(dev, crtc, primary, cursor,
176 &amdgpu_vkms_crtc_funcs, NULL);
178 DRM_ERROR("Failed to init CRTC\n");
182 drm_crtc_helper_add(crtc, &amdgpu_vkms_crtc_helper_funcs);
184 amdgpu_crtc->crtc_id = drm_crtc_index(crtc);
185 adev->mode_info.crtcs[drm_crtc_index(crtc)] = amdgpu_crtc;
187 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
188 amdgpu_crtc->encoder = NULL;
189 amdgpu_crtc->connector = NULL;
190 amdgpu_crtc->vsync_timer_enabled = AMDGPU_IRQ_STATE_DISABLE;
192 hrtimer_init(&amdgpu_crtc->vblank_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
193 amdgpu_crtc->vblank_timer.function = &amdgpu_vkms_vblank_simulate;
198 static const struct drm_connector_funcs amdgpu_vkms_connector_funcs = {
199 .fill_modes = drm_helper_probe_single_connector_modes,
200 .destroy = drm_connector_cleanup,
201 .reset = drm_atomic_helper_connector_reset,
202 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
203 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
206 static int amdgpu_vkms_conn_get_modes(struct drm_connector *connector)
208 struct drm_device *dev = connector->dev;
209 struct drm_display_mode *mode = NULL;
211 static const struct mode_size {
239 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
240 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
241 drm_mode_probed_add(connector, mode);
244 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
246 return ARRAY_SIZE(common_modes);
249 static const struct drm_connector_helper_funcs amdgpu_vkms_conn_helper_funcs = {
250 .get_modes = amdgpu_vkms_conn_get_modes,
253 static const struct drm_plane_funcs amdgpu_vkms_plane_funcs = {
254 .update_plane = drm_atomic_helper_update_plane,
255 .disable_plane = drm_atomic_helper_disable_plane,
256 .destroy = drm_plane_cleanup,
257 .reset = drm_atomic_helper_plane_reset,
258 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
259 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
262 static void amdgpu_vkms_plane_atomic_update(struct drm_plane *plane,
263 struct drm_atomic_state *old_state)
268 static int amdgpu_vkms_plane_atomic_check(struct drm_plane *plane,
269 struct drm_atomic_state *state)
271 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
273 struct drm_crtc_state *crtc_state;
276 if (!new_plane_state->fb || WARN_ON(!new_plane_state->crtc))
279 crtc_state = drm_atomic_get_crtc_state(state,
280 new_plane_state->crtc);
281 if (IS_ERR(crtc_state))
282 return PTR_ERR(crtc_state);
284 ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
285 DRM_PLANE_NO_SCALING,
286 DRM_PLANE_NO_SCALING,
291 /* for now primary plane must be visible and full screen */
292 if (!new_plane_state->visible)
298 static int amdgpu_vkms_prepare_fb(struct drm_plane *plane,
299 struct drm_plane_state *new_state)
301 struct amdgpu_framebuffer *afb;
302 struct drm_gem_object *obj;
303 struct amdgpu_device *adev;
304 struct amdgpu_bo *rbo;
308 if (!new_state->fb) {
309 DRM_DEBUG_KMS("No FB bound\n");
312 afb = to_amdgpu_framebuffer(new_state->fb);
313 obj = new_state->fb->obj[0];
314 rbo = gem_to_amdgpu_bo(obj);
315 adev = amdgpu_ttm_adev(rbo->tbo.bdev);
317 r = amdgpu_bo_reserve(rbo, true);
319 dev_err(adev->dev, "fail to reserve bo (%d)\n", r);
323 r = dma_resv_reserve_fences(rbo->tbo.base.resv, 1);
325 dev_err(adev->dev, "allocating fence slot failed (%d)\n", r);
329 if (plane->type != DRM_PLANE_TYPE_CURSOR)
330 domain = amdgpu_display_supported_domains(adev, rbo->flags);
332 domain = AMDGPU_GEM_DOMAIN_VRAM;
334 r = amdgpu_bo_pin(rbo, domain);
335 if (unlikely(r != 0)) {
336 if (r != -ERESTARTSYS)
337 DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
341 r = amdgpu_ttm_alloc_gart(&rbo->tbo);
342 if (unlikely(r != 0)) {
343 DRM_ERROR("%p bind failed\n", rbo);
347 amdgpu_bo_unreserve(rbo);
349 afb->address = amdgpu_bo_gpu_offset(rbo);
356 amdgpu_bo_unpin(rbo);
359 amdgpu_bo_unreserve(rbo);
363 static void amdgpu_vkms_cleanup_fb(struct drm_plane *plane,
364 struct drm_plane_state *old_state)
366 struct amdgpu_bo *rbo;
372 rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
373 r = amdgpu_bo_reserve(rbo, false);
375 DRM_ERROR("failed to reserve rbo before unpin\n");
379 amdgpu_bo_unpin(rbo);
380 amdgpu_bo_unreserve(rbo);
381 amdgpu_bo_unref(&rbo);
384 static const struct drm_plane_helper_funcs amdgpu_vkms_primary_helper_funcs = {
385 .atomic_update = amdgpu_vkms_plane_atomic_update,
386 .atomic_check = amdgpu_vkms_plane_atomic_check,
387 .prepare_fb = amdgpu_vkms_prepare_fb,
388 .cleanup_fb = amdgpu_vkms_cleanup_fb,
391 static struct drm_plane *amdgpu_vkms_plane_init(struct drm_device *dev,
392 enum drm_plane_type type,
395 struct drm_plane *plane;
398 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
400 return ERR_PTR(-ENOMEM);
402 ret = drm_universal_plane_init(dev, plane, 1 << index,
403 &amdgpu_vkms_plane_funcs,
405 ARRAY_SIZE(amdgpu_vkms_formats),
412 drm_plane_helper_add(plane, &amdgpu_vkms_primary_helper_funcs);
417 static int amdgpu_vkms_output_init(struct drm_device *dev, struct
418 amdgpu_vkms_output *output, int index)
420 struct drm_connector *connector = &output->connector;
421 struct drm_encoder *encoder = &output->encoder;
422 struct drm_crtc *crtc = &output->crtc.base;
423 struct drm_plane *primary, *cursor = NULL;
426 primary = amdgpu_vkms_plane_init(dev, DRM_PLANE_TYPE_PRIMARY, index);
428 return PTR_ERR(primary);
430 ret = amdgpu_vkms_crtc_init(dev, crtc, primary, cursor);
434 ret = drm_connector_init(dev, connector, &amdgpu_vkms_connector_funcs,
435 DRM_MODE_CONNECTOR_VIRTUAL);
437 DRM_ERROR("Failed to init connector\n");
441 drm_connector_helper_add(connector, &amdgpu_vkms_conn_helper_funcs);
443 ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_VIRTUAL);
445 DRM_ERROR("Failed to init encoder\n");
448 encoder->possible_crtcs = 1 << index;
450 ret = drm_connector_attach_encoder(connector, encoder);
452 DRM_ERROR("Failed to attach connector to encoder\n");
456 drm_mode_config_reset(dev);
461 drm_encoder_cleanup(encoder);
464 drm_connector_cleanup(connector);
467 drm_crtc_cleanup(crtc);
470 drm_plane_cleanup(primary);
475 const struct drm_mode_config_funcs amdgpu_vkms_mode_funcs = {
476 .fb_create = amdgpu_display_user_framebuffer_create,
477 .atomic_check = drm_atomic_helper_check,
478 .atomic_commit = drm_atomic_helper_commit,
481 static int amdgpu_vkms_sw_init(void *handle)
484 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
486 adev->amdgpu_vkms_output = kcalloc(adev->mode_info.num_crtc,
487 sizeof(struct amdgpu_vkms_output), GFP_KERNEL);
488 if (!adev->amdgpu_vkms_output)
491 adev_to_drm(adev)->max_vblank_count = 0;
493 adev_to_drm(adev)->mode_config.funcs = &amdgpu_vkms_mode_funcs;
495 adev_to_drm(adev)->mode_config.max_width = XRES_MAX;
496 adev_to_drm(adev)->mode_config.max_height = YRES_MAX;
498 adev_to_drm(adev)->mode_config.preferred_depth = 24;
499 adev_to_drm(adev)->mode_config.prefer_shadow = 1;
501 adev_to_drm(adev)->mode_config.fb_modifiers_not_supported = true;
503 adev_to_drm(adev)->mode_config.fb_modifiers_not_supported = true;
505 r = amdgpu_display_modeset_create_props(adev);
509 /* allocate crtcs, encoders, connectors */
510 for (i = 0; i < adev->mode_info.num_crtc; i++) {
511 r = amdgpu_vkms_output_init(adev_to_drm(adev), &adev->amdgpu_vkms_output[i], i);
516 r = drm_vblank_init(adev_to_drm(adev), adev->mode_info.num_crtc);
520 drm_kms_helper_poll_init(adev_to_drm(adev));
522 adev->mode_info.mode_config_initialized = true;
526 static int amdgpu_vkms_sw_fini(void *handle)
528 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
531 for (i = 0; i < adev->mode_info.num_crtc; i++)
532 if (adev->mode_info.crtcs[i])
533 hrtimer_cancel(&adev->mode_info.crtcs[i]->vblank_timer);
535 drm_kms_helper_poll_fini(adev_to_drm(adev));
536 drm_mode_config_cleanup(adev_to_drm(adev));
538 adev->mode_info.mode_config_initialized = false;
540 kfree(adev->mode_info.bios_hardcoded_edid);
541 kfree(adev->amdgpu_vkms_output);
545 static int amdgpu_vkms_hw_init(void *handle)
547 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
549 switch (adev->asic_type) {
550 #ifdef CONFIG_DRM_AMDGPU_SI
555 dce_v6_0_disable_dce(adev);
558 #ifdef CONFIG_DRM_AMDGPU_CIK
564 dce_v8_0_disable_dce(adev);
569 dce_v10_0_disable_dce(adev);
576 dce_v11_0_disable_dce(adev);
579 #ifdef CONFIG_DRM_AMDGPU_SI
590 static int amdgpu_vkms_hw_fini(void *handle)
595 static int amdgpu_vkms_suspend(void *handle)
597 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
600 r = drm_mode_config_helper_suspend(adev_to_drm(adev));
603 return amdgpu_vkms_hw_fini(handle);
606 static int amdgpu_vkms_resume(void *handle)
608 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
611 r = amdgpu_vkms_hw_init(handle);
614 return drm_mode_config_helper_resume(adev_to_drm(adev));
617 static bool amdgpu_vkms_is_idle(void *handle)
622 static int amdgpu_vkms_wait_for_idle(void *handle)
627 static int amdgpu_vkms_soft_reset(void *handle)
632 static int amdgpu_vkms_set_clockgating_state(void *handle,
633 enum amd_clockgating_state state)
638 static int amdgpu_vkms_set_powergating_state(void *handle,
639 enum amd_powergating_state state)
644 static const struct amd_ip_funcs amdgpu_vkms_ip_funcs = {
645 .name = "amdgpu_vkms",
648 .sw_init = amdgpu_vkms_sw_init,
649 .sw_fini = amdgpu_vkms_sw_fini,
650 .hw_init = amdgpu_vkms_hw_init,
651 .hw_fini = amdgpu_vkms_hw_fini,
652 .suspend = amdgpu_vkms_suspend,
653 .resume = amdgpu_vkms_resume,
654 .is_idle = amdgpu_vkms_is_idle,
655 .wait_for_idle = amdgpu_vkms_wait_for_idle,
656 .soft_reset = amdgpu_vkms_soft_reset,
657 .set_clockgating_state = amdgpu_vkms_set_clockgating_state,
658 .set_powergating_state = amdgpu_vkms_set_powergating_state,
661 const struct amdgpu_ip_block_version amdgpu_vkms_ip_block =
663 .type = AMD_IP_BLOCK_TYPE_DCE,
667 .funcs = &amdgpu_vkms_ip_funcs,