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_gem_framebuffer_helper.h>
7 #include <drm/drm_vblank.h>
10 #ifdef CONFIG_DRM_AMDGPU_SI
13 #ifdef CONFIG_DRM_AMDGPU_CIK
16 #include "dce_v10_0.h"
17 #include "dce_v11_0.h"
18 #include "ivsrcid/ivsrcid_vislands30.h"
19 #include "amdgpu_vkms.h"
20 #include "amdgpu_display.h"
22 #include "amdgpu_irq.h"
27 * The amdgpu vkms interface provides a virtual KMS interface for several use
28 * cases: devices without display hardware, platforms where the actual display
29 * hardware is not useful (e.g., servers), SR-IOV virtual functions, device
30 * emulation/simulation, and device bring up prior to display hardware being
31 * usable. We previously emulated a legacy KMS interface, but there was a desire
32 * to move to the atomic KMS interface. The vkms driver did everything we
33 * needed, but we wanted KMS support natively in the driver without buffer
34 * sharing and the ability to support an instance of VKMS per device. We first
35 * looked at splitting vkms into a stub driver and a helper module that other
36 * drivers could use to implement a virtual display, but this strategy ended up
37 * being messy due to driver specific callbacks needed for buffer management.
38 * Ultimately, it proved easier to import the vkms code as it mostly used core
42 static const u32 amdgpu_vkms_formats[] = {
46 static enum hrtimer_restart amdgpu_vkms_vblank_simulate(struct hrtimer *timer)
48 struct amdgpu_crtc *amdgpu_crtc = container_of(timer, struct amdgpu_crtc, vblank_timer);
49 struct drm_crtc *crtc = &amdgpu_crtc->base;
50 struct amdgpu_vkms_output *output = drm_crtc_to_amdgpu_vkms_output(crtc);
54 ret_overrun = hrtimer_forward_now(&amdgpu_crtc->vblank_timer,
57 DRM_WARN("%s: vblank timer overrun\n", __func__);
59 ret = drm_crtc_handle_vblank(crtc);
60 /* Don't queue timer again when vblank is disabled. */
62 return HRTIMER_NORESTART;
64 return HRTIMER_RESTART;
67 static int amdgpu_vkms_enable_vblank(struct drm_crtc *crtc)
69 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
70 struct amdgpu_vkms_output *out = drm_crtc_to_amdgpu_vkms_output(crtc);
71 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
73 drm_calc_timestamping_constants(crtc, &crtc->mode);
75 out->period_ns = ktime_set(0, vblank->framedur_ns);
76 hrtimer_start(&amdgpu_crtc->vblank_timer, out->period_ns, HRTIMER_MODE_REL);
81 static void amdgpu_vkms_disable_vblank(struct drm_crtc *crtc)
83 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
85 hrtimer_try_to_cancel(&amdgpu_crtc->vblank_timer);
88 static bool amdgpu_vkms_get_vblank_timestamp(struct drm_crtc *crtc,
93 struct amdgpu_vkms_output *output = drm_crtc_to_amdgpu_vkms_output(crtc);
94 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
95 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
97 if (!READ_ONCE(vblank->enabled)) {
98 *vblank_time = ktime_get();
102 *vblank_time = READ_ONCE(amdgpu_crtc->vblank_timer.node.expires);
104 if (WARN_ON(*vblank_time == vblank->time))
108 * To prevent races we roll the hrtimer forward before we do any
109 * interrupt processing - this is how real hw works (the interrupt is
110 * only generated after all the vblank registers are updated) and what
111 * the vblank core expects. Therefore we need to always correct the
112 * timestampe by one frame.
114 *vblank_time -= output->period_ns;
119 static const struct drm_crtc_funcs amdgpu_vkms_crtc_funcs = {
120 .set_config = drm_atomic_helper_set_config,
121 .destroy = drm_crtc_cleanup,
122 .page_flip = drm_atomic_helper_page_flip,
123 .reset = drm_atomic_helper_crtc_reset,
124 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
125 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
126 .enable_vblank = amdgpu_vkms_enable_vblank,
127 .disable_vblank = amdgpu_vkms_disable_vblank,
128 .get_vblank_timestamp = amdgpu_vkms_get_vblank_timestamp,
131 static void amdgpu_vkms_crtc_atomic_enable(struct drm_crtc *crtc,
132 struct drm_atomic_state *state)
134 drm_crtc_vblank_on(crtc);
137 static void amdgpu_vkms_crtc_atomic_disable(struct drm_crtc *crtc,
138 struct drm_atomic_state *state)
140 drm_crtc_vblank_off(crtc);
143 static void amdgpu_vkms_crtc_atomic_flush(struct drm_crtc *crtc,
144 struct drm_atomic_state *state)
147 if (crtc->state->event) {
148 spin_lock_irqsave(&crtc->dev->event_lock, flags);
150 if (drm_crtc_vblank_get(crtc) != 0)
151 drm_crtc_send_vblank_event(crtc, crtc->state->event);
153 drm_crtc_arm_vblank_event(crtc, crtc->state->event);
155 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
157 crtc->state->event = NULL;
161 static const struct drm_crtc_helper_funcs amdgpu_vkms_crtc_helper_funcs = {
162 .atomic_flush = amdgpu_vkms_crtc_atomic_flush,
163 .atomic_enable = amdgpu_vkms_crtc_atomic_enable,
164 .atomic_disable = amdgpu_vkms_crtc_atomic_disable,
167 static int amdgpu_vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
168 struct drm_plane *primary, struct drm_plane *cursor)
170 struct amdgpu_device *adev = drm_to_adev(dev);
171 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
174 ret = drm_crtc_init_with_planes(dev, crtc, primary, cursor,
175 &amdgpu_vkms_crtc_funcs, NULL);
177 DRM_ERROR("Failed to init CRTC\n");
181 drm_crtc_helper_add(crtc, &amdgpu_vkms_crtc_helper_funcs);
183 amdgpu_crtc->crtc_id = drm_crtc_index(crtc);
184 adev->mode_info.crtcs[drm_crtc_index(crtc)] = amdgpu_crtc;
186 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
187 amdgpu_crtc->encoder = NULL;
188 amdgpu_crtc->connector = NULL;
189 amdgpu_crtc->vsync_timer_enabled = AMDGPU_IRQ_STATE_DISABLE;
191 hrtimer_init(&amdgpu_crtc->vblank_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
192 amdgpu_crtc->vblank_timer.function = &amdgpu_vkms_vblank_simulate;
197 static const struct drm_connector_funcs amdgpu_vkms_connector_funcs = {
198 .fill_modes = drm_helper_probe_single_connector_modes,
199 .destroy = drm_connector_cleanup,
200 .reset = drm_atomic_helper_connector_reset,
201 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
202 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
205 static int amdgpu_vkms_conn_get_modes(struct drm_connector *connector)
207 struct drm_device *dev = connector->dev;
208 struct drm_display_mode *mode = NULL;
210 static const struct mode_size {
238 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
239 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
242 drm_mode_probed_add(connector, mode);
245 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
247 return ARRAY_SIZE(common_modes);
250 static const struct drm_connector_helper_funcs amdgpu_vkms_conn_helper_funcs = {
251 .get_modes = amdgpu_vkms_conn_get_modes,
254 static const struct drm_plane_funcs amdgpu_vkms_plane_funcs = {
255 .update_plane = drm_atomic_helper_update_plane,
256 .disable_plane = drm_atomic_helper_disable_plane,
257 .destroy = drm_plane_cleanup,
258 .reset = drm_atomic_helper_plane_reset,
259 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
260 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
263 static void amdgpu_vkms_plane_atomic_update(struct drm_plane *plane,
264 struct drm_atomic_state *old_state)
269 static int amdgpu_vkms_plane_atomic_check(struct drm_plane *plane,
270 struct drm_atomic_state *state)
272 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
274 struct drm_crtc_state *crtc_state;
277 if (!new_plane_state->fb || WARN_ON(!new_plane_state->crtc))
280 crtc_state = drm_atomic_get_crtc_state(state,
281 new_plane_state->crtc);
282 if (IS_ERR(crtc_state))
283 return PTR_ERR(crtc_state);
285 ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
286 DRM_PLANE_NO_SCALING,
287 DRM_PLANE_NO_SCALING,
292 /* for now primary plane must be visible and full screen */
293 if (!new_plane_state->visible)
299 static int amdgpu_vkms_prepare_fb(struct drm_plane *plane,
300 struct drm_plane_state *new_state)
302 struct amdgpu_framebuffer *afb;
303 struct drm_gem_object *obj;
304 struct amdgpu_device *adev;
305 struct amdgpu_bo *rbo;
309 if (!new_state->fb) {
310 DRM_DEBUG_KMS("No FB bound\n");
313 afb = to_amdgpu_framebuffer(new_state->fb);
315 obj = drm_gem_fb_get_obj(new_state->fb, 0);
317 DRM_ERROR("Failed to get obj from framebuffer\n");
321 rbo = gem_to_amdgpu_bo(obj);
322 adev = amdgpu_ttm_adev(rbo->tbo.bdev);
324 r = amdgpu_bo_reserve(rbo, true);
326 dev_err(adev->dev, "fail to reserve bo (%d)\n", r);
330 r = dma_resv_reserve_fences(rbo->tbo.base.resv, 1);
332 dev_err(adev->dev, "allocating fence slot failed (%d)\n", r);
336 if (plane->type != DRM_PLANE_TYPE_CURSOR)
337 domain = amdgpu_display_supported_domains(adev, rbo->flags);
339 domain = AMDGPU_GEM_DOMAIN_VRAM;
341 r = amdgpu_bo_pin(rbo, domain);
342 if (unlikely(r != 0)) {
343 if (r != -ERESTARTSYS)
344 DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
348 r = amdgpu_ttm_alloc_gart(&rbo->tbo);
349 if (unlikely(r != 0)) {
350 DRM_ERROR("%p bind failed\n", rbo);
354 amdgpu_bo_unreserve(rbo);
356 afb->address = amdgpu_bo_gpu_offset(rbo);
363 amdgpu_bo_unpin(rbo);
366 amdgpu_bo_unreserve(rbo);
370 static void amdgpu_vkms_cleanup_fb(struct drm_plane *plane,
371 struct drm_plane_state *old_state)
373 struct amdgpu_bo *rbo;
374 struct drm_gem_object *obj;
380 obj = drm_gem_fb_get_obj(old_state->fb, 0);
382 DRM_ERROR("Failed to get obj from framebuffer\n");
386 rbo = gem_to_amdgpu_bo(obj);
387 r = amdgpu_bo_reserve(rbo, false);
389 DRM_ERROR("failed to reserve rbo before unpin\n");
393 amdgpu_bo_unpin(rbo);
394 amdgpu_bo_unreserve(rbo);
395 amdgpu_bo_unref(&rbo);
398 static const struct drm_plane_helper_funcs amdgpu_vkms_primary_helper_funcs = {
399 .atomic_update = amdgpu_vkms_plane_atomic_update,
400 .atomic_check = amdgpu_vkms_plane_atomic_check,
401 .prepare_fb = amdgpu_vkms_prepare_fb,
402 .cleanup_fb = amdgpu_vkms_cleanup_fb,
405 static struct drm_plane *amdgpu_vkms_plane_init(struct drm_device *dev,
406 enum drm_plane_type type,
409 struct drm_plane *plane;
412 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
414 return ERR_PTR(-ENOMEM);
416 ret = drm_universal_plane_init(dev, plane, 1 << index,
417 &amdgpu_vkms_plane_funcs,
419 ARRAY_SIZE(amdgpu_vkms_formats),
426 drm_plane_helper_add(plane, &amdgpu_vkms_primary_helper_funcs);
431 static int amdgpu_vkms_output_init(struct drm_device *dev, struct
432 amdgpu_vkms_output *output, int index)
434 struct drm_connector *connector = &output->connector;
435 struct drm_encoder *encoder = &output->encoder;
436 struct drm_crtc *crtc = &output->crtc.base;
437 struct drm_plane *primary, *cursor = NULL;
440 primary = amdgpu_vkms_plane_init(dev, DRM_PLANE_TYPE_PRIMARY, index);
442 return PTR_ERR(primary);
444 ret = amdgpu_vkms_crtc_init(dev, crtc, primary, cursor);
448 ret = drm_connector_init(dev, connector, &amdgpu_vkms_connector_funcs,
449 DRM_MODE_CONNECTOR_VIRTUAL);
451 DRM_ERROR("Failed to init connector\n");
455 drm_connector_helper_add(connector, &amdgpu_vkms_conn_helper_funcs);
457 ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_VIRTUAL);
459 DRM_ERROR("Failed to init encoder\n");
462 encoder->possible_crtcs = 1 << index;
464 ret = drm_connector_attach_encoder(connector, encoder);
466 DRM_ERROR("Failed to attach connector to encoder\n");
470 drm_mode_config_reset(dev);
475 drm_encoder_cleanup(encoder);
478 drm_connector_cleanup(connector);
481 drm_crtc_cleanup(crtc);
484 drm_plane_cleanup(primary);
489 const struct drm_mode_config_funcs amdgpu_vkms_mode_funcs = {
490 .fb_create = amdgpu_display_user_framebuffer_create,
491 .atomic_check = drm_atomic_helper_check,
492 .atomic_commit = drm_atomic_helper_commit,
495 static int amdgpu_vkms_sw_init(void *handle)
498 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
500 adev->amdgpu_vkms_output = kcalloc(adev->mode_info.num_crtc,
501 sizeof(struct amdgpu_vkms_output), GFP_KERNEL);
502 if (!adev->amdgpu_vkms_output)
505 adev_to_drm(adev)->max_vblank_count = 0;
507 adev_to_drm(adev)->mode_config.funcs = &amdgpu_vkms_mode_funcs;
509 adev_to_drm(adev)->mode_config.max_width = XRES_MAX;
510 adev_to_drm(adev)->mode_config.max_height = YRES_MAX;
512 adev_to_drm(adev)->mode_config.preferred_depth = 24;
513 adev_to_drm(adev)->mode_config.prefer_shadow = 1;
515 adev_to_drm(adev)->mode_config.fb_modifiers_not_supported = true;
517 r = amdgpu_display_modeset_create_props(adev);
521 /* allocate crtcs, encoders, connectors */
522 for (i = 0; i < adev->mode_info.num_crtc; i++) {
523 r = amdgpu_vkms_output_init(adev_to_drm(adev), &adev->amdgpu_vkms_output[i], i);
528 r = drm_vblank_init(adev_to_drm(adev), adev->mode_info.num_crtc);
532 drm_kms_helper_poll_init(adev_to_drm(adev));
534 adev->mode_info.mode_config_initialized = true;
538 static int amdgpu_vkms_sw_fini(void *handle)
540 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
543 for (i = 0; i < adev->mode_info.num_crtc; i++)
544 if (adev->mode_info.crtcs[i])
545 hrtimer_cancel(&adev->mode_info.crtcs[i]->vblank_timer);
547 drm_kms_helper_poll_fini(adev_to_drm(adev));
548 drm_mode_config_cleanup(adev_to_drm(adev));
550 adev->mode_info.mode_config_initialized = false;
552 drm_edid_free(adev->mode_info.bios_hardcoded_edid);
553 kfree(adev->amdgpu_vkms_output);
557 static int amdgpu_vkms_hw_init(void *handle)
559 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
561 switch (adev->asic_type) {
562 #ifdef CONFIG_DRM_AMDGPU_SI
567 dce_v6_0_disable_dce(adev);
570 #ifdef CONFIG_DRM_AMDGPU_CIK
576 dce_v8_0_disable_dce(adev);
581 dce_v10_0_disable_dce(adev);
588 dce_v11_0_disable_dce(adev);
591 #ifdef CONFIG_DRM_AMDGPU_SI
602 static int amdgpu_vkms_hw_fini(void *handle)
607 static int amdgpu_vkms_suspend(void *handle)
609 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
612 r = drm_mode_config_helper_suspend(adev_to_drm(adev));
615 return amdgpu_vkms_hw_fini(handle);
618 static int amdgpu_vkms_resume(void *handle)
620 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
623 r = amdgpu_vkms_hw_init(handle);
626 return drm_mode_config_helper_resume(adev_to_drm(adev));
629 static bool amdgpu_vkms_is_idle(void *handle)
634 static int amdgpu_vkms_wait_for_idle(void *handle)
639 static int amdgpu_vkms_soft_reset(void *handle)
644 static int amdgpu_vkms_set_clockgating_state(void *handle,
645 enum amd_clockgating_state state)
650 static int amdgpu_vkms_set_powergating_state(void *handle,
651 enum amd_powergating_state state)
656 static const struct amd_ip_funcs amdgpu_vkms_ip_funcs = {
657 .name = "amdgpu_vkms",
660 .sw_init = amdgpu_vkms_sw_init,
661 .sw_fini = amdgpu_vkms_sw_fini,
662 .hw_init = amdgpu_vkms_hw_init,
663 .hw_fini = amdgpu_vkms_hw_fini,
664 .suspend = amdgpu_vkms_suspend,
665 .resume = amdgpu_vkms_resume,
666 .is_idle = amdgpu_vkms_is_idle,
667 .wait_for_idle = amdgpu_vkms_wait_for_idle,
668 .soft_reset = amdgpu_vkms_soft_reset,
669 .set_clockgating_state = amdgpu_vkms_set_clockgating_state,
670 .set_powergating_state = amdgpu_vkms_set_powergating_state,
671 .dump_ip_state = NULL,
672 .print_ip_state = NULL,
675 const struct amdgpu_ip_block_version amdgpu_vkms_ip_block = {
676 .type = AMD_IP_BLOCK_TYPE_DCE,
680 .funcs = &amdgpu_vkms_ip_funcs,