Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: MIT
2/*
3 * Copyright 2015-2026 Advanced Micro Devices, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: AMD
24 *
25 */
26
27/* The caprices of the preprocessor require that this be declared right here */
28#define CREATE_TRACE_POINTS
29
30#include "dm_services_types.h"
31#include "dc.h"
32#include "link_enc_cfg.h"
33#include "dc/inc/core_types.h"
34#include "dal_asic_id.h"
35#include "dmub/dmub_srv.h"
36#include "dc/inc/hw/dmcu.h"
37#include "dc/inc/hw/abm.h"
38#include "dc/dc_dmub_srv.h"
39#include "dc/dc_edid_parser.h"
40#include "dc/dc_stat.h"
41#include "dc/dc_state.h"
42#include "amdgpu_dm_trace.h"
43#include "link/protocols/link_dpcd.h"
44#include "link_service_types.h"
45#include "link/protocols/link_dp_capability.h"
46#include "link/protocols/link_ddc.h"
47
48#include "amdgpu.h"
49#include "amdgpu_display.h"
50#include "amdgpu_ucode.h"
51#include "atom.h"
52#include "amdgpu_dm.h"
53#include "amdgpu_dm_plane.h"
54#include "amdgpu_dm_crtc.h"
55#include "amdgpu_dm_hdcp.h"
56#include <drm/display/drm_hdcp_helper.h>
57#include "amdgpu_dm_wb.h"
58#include "amdgpu_atombios.h"
59
60#include "amd_shared.h"
61#include "amdgpu_dm_irq.h"
62#include "dm_helpers.h"
63#include "amdgpu_dm_mst_types.h"
64#if defined(CONFIG_DEBUG_FS)
65#include "amdgpu_dm_debugfs.h"
66#endif
67#include "amdgpu_dm_psr.h"
68#include "amdgpu_dm_replay.h"
69
70#include "ivsrcid/ivsrcid_vislands30.h"
71
72#include <linux/backlight.h>
73#include <linux/module.h>
74#include <linux/moduleparam.h>
75#include <linux/types.h>
76#include <linux/pm_runtime.h>
77#include <linux/pci.h>
78#include <linux/power_supply.h>
79#include <linux/firmware.h>
80#include <linux/component.h>
81#include <linux/sort.h>
82
83#include <drm/drm_privacy_screen_consumer.h>
84#include <drm/display/drm_dp_mst_helper.h>
85#include <drm/display/drm_hdmi_helper.h>
86#include <drm/drm_atomic.h>
87#include <drm/drm_atomic_uapi.h>
88#include <drm/drm_atomic_helper.h>
89#include <drm/drm_blend.h>
90#include <drm/drm_fixed.h>
91#include <drm/drm_fourcc.h>
92#include <drm/drm_edid.h>
93#include <drm/drm_eld.h>
94#include <drm/drm_mode.h>
95#include <drm/drm_utils.h>
96#include <drm/drm_vblank.h>
97#include <drm/drm_audio_component.h>
98#include <drm/drm_gem_atomic_helper.h>
99
100#include <media/cec-notifier.h>
101#include <acpi/video.h>
102
103#include "ivsrcid/dcn/irqsrcs_dcn_1_0.h"
104
105#include "modules/inc/mod_freesync.h"
106#include "modules/power/power_helpers.h"
107
108static_assert(AMDGPU_DMUB_NOTIFICATION_MAX == DMUB_NOTIFICATION_MAX, "AMDGPU_DMUB_NOTIFICATION_MAX mismatch");
109
110#define FIRMWARE_RENOIR_DMUB "amdgpu/renoir_dmcub.bin"
111MODULE_FIRMWARE(FIRMWARE_RENOIR_DMUB);
112#define FIRMWARE_SIENNA_CICHLID_DMUB "amdgpu/sienna_cichlid_dmcub.bin"
113MODULE_FIRMWARE(FIRMWARE_SIENNA_CICHLID_DMUB);
114#define FIRMWARE_NAVY_FLOUNDER_DMUB "amdgpu/navy_flounder_dmcub.bin"
115MODULE_FIRMWARE(FIRMWARE_NAVY_FLOUNDER_DMUB);
116#define FIRMWARE_GREEN_SARDINE_DMUB "amdgpu/green_sardine_dmcub.bin"
117MODULE_FIRMWARE(FIRMWARE_GREEN_SARDINE_DMUB);
118#define FIRMWARE_VANGOGH_DMUB "amdgpu/vangogh_dmcub.bin"
119MODULE_FIRMWARE(FIRMWARE_VANGOGH_DMUB);
120#define FIRMWARE_DIMGREY_CAVEFISH_DMUB "amdgpu/dimgrey_cavefish_dmcub.bin"
121MODULE_FIRMWARE(FIRMWARE_DIMGREY_CAVEFISH_DMUB);
122#define FIRMWARE_BEIGE_GOBY_DMUB "amdgpu/beige_goby_dmcub.bin"
123MODULE_FIRMWARE(FIRMWARE_BEIGE_GOBY_DMUB);
124#define FIRMWARE_YELLOW_CARP_DMUB "amdgpu/yellow_carp_dmcub.bin"
125MODULE_FIRMWARE(FIRMWARE_YELLOW_CARP_DMUB);
126#define FIRMWARE_DCN_314_DMUB "amdgpu/dcn_3_1_4_dmcub.bin"
127MODULE_FIRMWARE(FIRMWARE_DCN_314_DMUB);
128#define FIRMWARE_DCN_315_DMUB "amdgpu/dcn_3_1_5_dmcub.bin"
129MODULE_FIRMWARE(FIRMWARE_DCN_315_DMUB);
130#define FIRMWARE_DCN316_DMUB "amdgpu/dcn_3_1_6_dmcub.bin"
131MODULE_FIRMWARE(FIRMWARE_DCN316_DMUB);
132
133#define FIRMWARE_DCN_V3_2_0_DMCUB "amdgpu/dcn_3_2_0_dmcub.bin"
134MODULE_FIRMWARE(FIRMWARE_DCN_V3_2_0_DMCUB);
135#define FIRMWARE_DCN_V3_2_1_DMCUB "amdgpu/dcn_3_2_1_dmcub.bin"
136MODULE_FIRMWARE(FIRMWARE_DCN_V3_2_1_DMCUB);
137
138#define FIRMWARE_RAVEN_DMCU "amdgpu/raven_dmcu.bin"
139MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU);
140
141#define FIRMWARE_NAVI12_DMCU "amdgpu/navi12_dmcu.bin"
142MODULE_FIRMWARE(FIRMWARE_NAVI12_DMCU);
143
144#define FIRMWARE_DCN_35_DMUB "amdgpu/dcn_3_5_dmcub.bin"
145MODULE_FIRMWARE(FIRMWARE_DCN_35_DMUB);
146
147#define FIRMWARE_DCN_351_DMUB "amdgpu/dcn_3_5_1_dmcub.bin"
148MODULE_FIRMWARE(FIRMWARE_DCN_351_DMUB);
149
150#define FIRMWARE_DCN_36_DMUB "amdgpu/dcn_3_6_dmcub.bin"
151MODULE_FIRMWARE(FIRMWARE_DCN_36_DMUB);
152
153#define FIRMWARE_DCN_401_DMUB "amdgpu/dcn_4_0_1_dmcub.bin"
154MODULE_FIRMWARE(FIRMWARE_DCN_401_DMUB);
155
156#define FIRMWARE_DCN_42_DMUB "amdgpu/dcn_4_2_dmcub.bin"
157MODULE_FIRMWARE(FIRMWARE_DCN_42_DMUB);
158
159/**
160 * DOC: overview
161 *
162 * The AMDgpu display manager, **amdgpu_dm** (or even simpler,
163 * **dm**) sits between DRM and DC. It acts as a liaison, converting DRM
164 * requests into DC requests, and DC responses into DRM responses.
165 *
166 * The root control structure is &struct amdgpu_display_manager.
167 */
168
169/* basic init/fini API */
170static int amdgpu_dm_init(struct amdgpu_device *adev);
171static void amdgpu_dm_fini(struct amdgpu_device *adev);
172static bool is_freesync_video_mode(const struct drm_display_mode *mode, struct amdgpu_dm_connector *aconnector);
173static void reset_freesync_config_for_crtc(struct dm_crtc_state *new_crtc_state);
174static struct amdgpu_i2c_adapter *
175create_i2c(struct ddc_service *ddc_service, bool oem);
176
177static enum drm_mode_subconnector get_subconnector_type(struct dc_link *link)
178{
179 switch (link->dpcd_caps.dongle_type) {
180 case DISPLAY_DONGLE_NONE:
181 return DRM_MODE_SUBCONNECTOR_Native;
182 case DISPLAY_DONGLE_DP_VGA_CONVERTER:
183 return DRM_MODE_SUBCONNECTOR_VGA;
184 case DISPLAY_DONGLE_DP_DVI_CONVERTER:
185 case DISPLAY_DONGLE_DP_DVI_DONGLE:
186 return DRM_MODE_SUBCONNECTOR_DVID;
187 case DISPLAY_DONGLE_DP_HDMI_CONVERTER:
188 case DISPLAY_DONGLE_DP_HDMI_DONGLE:
189 return DRM_MODE_SUBCONNECTOR_HDMIA;
190 case DISPLAY_DONGLE_DP_HDMI_MISMATCHED_DONGLE:
191 default:
192 return DRM_MODE_SUBCONNECTOR_Unknown;
193 }
194}
195
196static void update_subconnector_property(struct amdgpu_dm_connector *aconnector)
197{
198 struct dc_link *link = aconnector->dc_link;
199 struct drm_connector *connector = &aconnector->base;
200 enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
201
202 if (connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort)
203 return;
204
205 if (aconnector->dc_sink)
206 subconnector = get_subconnector_type(link);
207
208 drm_object_property_set_value(&connector->base,
209 connector->dev->mode_config.dp_subconnector_property,
210 subconnector);
211}
212
213/*
214 * initializes drm_device display related structures, based on the information
215 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
216 * drm_encoder, drm_mode_config
217 *
218 * Returns 0 on success
219 */
220static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
221/* removes and deallocates the drm structures, created by the above function */
222static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
223
224static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
225 struct amdgpu_dm_connector *amdgpu_dm_connector,
226 u32 link_index,
227 struct amdgpu_encoder *amdgpu_encoder);
228static int amdgpu_dm_encoder_init(struct drm_device *dev,
229 struct amdgpu_encoder *aencoder,
230 uint32_t link_index);
231
232static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
233
234static int amdgpu_dm_atomic_setup_commit(struct drm_atomic_state *state);
235static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
236
237static int amdgpu_dm_atomic_check(struct drm_device *dev,
238 struct drm_atomic_state *state);
239
240static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector);
241static void handle_hpd_rx_irq(void *param);
242
243static void amdgpu_dm_backlight_set_level(struct amdgpu_display_manager *dm,
244 int bl_idx,
245 u32 user_brightness);
246
247static bool
248is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
249 struct drm_crtc_state *new_crtc_state);
250/*
251 * dm_vblank_get_counter
252 *
253 * @brief
254 * Get counter for number of vertical blanks
255 *
256 * @param
257 * struct amdgpu_device *adev - [in] desired amdgpu device
258 * int disp_idx - [in] which CRTC to get the counter from
259 *
260 * @return
261 * Counter for vertical blanks
262 */
263static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
264{
265 struct amdgpu_crtc *acrtc = NULL;
266
267 if (crtc >= adev->mode_info.num_crtc)
268 return 0;
269
270 acrtc = adev->mode_info.crtcs[crtc];
271
272 if (!acrtc->dm_irq_params.stream) {
273 drm_err(adev_to_drm(adev), "dc_stream_state is NULL for crtc '%d'!\n",
274 crtc);
275 return 0;
276 }
277
278 return dc_stream_get_vblank_counter(acrtc->dm_irq_params.stream);
279}
280
281static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
282 u32 *vbl, u32 *position)
283{
284 u32 v_blank_start = 0, v_blank_end = 0, h_position = 0, v_position = 0;
285 struct amdgpu_crtc *acrtc = NULL;
286 struct dc *dc = adev->dm.dc;
287
288 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
289 return -EINVAL;
290
291 acrtc = adev->mode_info.crtcs[crtc];
292
293 if (!acrtc->dm_irq_params.stream) {
294 drm_err(adev_to_drm(adev), "dc_stream_state is NULL for crtc '%d'!\n",
295 crtc);
296 return 0;
297 }
298
299 if (dc && dc->caps.ips_support && dc->idle_optimizations_allowed)
300 dc_allow_idle_optimizations(dc, false);
301
302 /*
303 * TODO rework base driver to use values directly.
304 * for now parse it back into reg-format
305 */
306 dc_stream_get_scanoutpos(acrtc->dm_irq_params.stream,
307 &v_blank_start,
308 &v_blank_end,
309 &h_position,
310 &v_position);
311
312 *position = v_position | (h_position << 16);
313 *vbl = v_blank_start | (v_blank_end << 16);
314
315 return 0;
316}
317
318static bool dm_is_idle(struct amdgpu_ip_block *ip_block)
319{
320 /* XXX todo */
321 return true;
322}
323
324static int dm_wait_for_idle(struct amdgpu_ip_block *ip_block)
325{
326 /* XXX todo */
327 return 0;
328}
329
330static bool dm_check_soft_reset(struct amdgpu_ip_block *ip_block)
331{
332 return false;
333}
334
335static int dm_soft_reset(struct amdgpu_ip_block *ip_block)
336{
337 /* XXX todo */
338 return 0;
339}
340
341static struct amdgpu_crtc *
342get_crtc_by_otg_inst(struct amdgpu_device *adev,
343 int otg_inst)
344{
345 struct drm_device *dev = adev_to_drm(adev);
346 struct drm_crtc *crtc;
347 struct amdgpu_crtc *amdgpu_crtc;
348
349 if (WARN_ON(otg_inst == -1))
350 return adev->mode_info.crtcs[0];
351
352 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
353 amdgpu_crtc = to_amdgpu_crtc(crtc);
354
355 if (amdgpu_crtc->otg_inst == otg_inst)
356 return amdgpu_crtc;
357 }
358
359 return NULL;
360}
361
362static inline bool is_dc_timing_adjust_needed(struct dm_crtc_state *old_state,
363 struct dm_crtc_state *new_state)
364{
365 if (new_state->stream->adjust.timing_adjust_pending)
366 return true;
367 if (new_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)
368 return true;
369 else if (amdgpu_dm_crtc_vrr_active(old_state) != amdgpu_dm_crtc_vrr_active(new_state))
370 return true;
371 else
372 return false;
373}
374
375/*
376 * DC will program planes with their z-order determined by their ordering
377 * in the dc_surface_updates array. This comparator is used to sort them
378 * by descending zpos.
379 */
380static int dm_plane_layer_index_cmp(const void *a, const void *b)
381{
382 const struct dc_surface_update *sa = (struct dc_surface_update *)a;
383 const struct dc_surface_update *sb = (struct dc_surface_update *)b;
384
385 /* Sort by descending dc_plane layer_index (i.e. normalized_zpos) */
386 return sb->surface->layer_index - sa->surface->layer_index;
387}
388
389/**
390 * update_planes_and_stream_adapter() - Send planes to be updated in DC
391 *
392 * DC has a generic way to update planes and stream via
393 * dc_update_planes_and_stream function; however, DM might need some
394 * adjustments and preparation before calling it. This function is a wrapper
395 * for the dc_update_planes_and_stream that does any required configuration
396 * before passing control to DC.
397 *
398 * @dc: Display Core control structure
399 * @update_type: specify whether it is FULL/MEDIUM/FAST update
400 * @planes_count: planes count to update
401 * @stream: stream state
402 * @stream_update: stream update
403 * @array_of_surface_update: dc surface update pointer
404 *
405 */
406static inline bool update_planes_and_stream_adapter(struct dc *dc,
407 int update_type,
408 int planes_count,
409 struct dc_stream_state *stream,
410 struct dc_stream_update *stream_update,
411 struct dc_surface_update *array_of_surface_update)
412{
413 sort(array_of_surface_update, planes_count,
414 sizeof(*array_of_surface_update), dm_plane_layer_index_cmp, NULL);
415
416 /*
417 * Previous frame finished and HW is ready for optimization.
418 */
419 dc_post_update_surfaces_to_stream(dc);
420
421 return dc_update_planes_and_stream(dc,
422 array_of_surface_update,
423 planes_count,
424 stream,
425 stream_update);
426}
427
428/**
429 * dm_pflip_high_irq() - Handle pageflip interrupt
430 * @interrupt_params: ignored
431 *
432 * Handles the pageflip interrupt by notifying all interested parties
433 * that the pageflip has been completed.
434 */
435static void dm_pflip_high_irq(void *interrupt_params)
436{
437 struct amdgpu_crtc *amdgpu_crtc;
438 struct common_irq_params *irq_params = interrupt_params;
439 struct amdgpu_device *adev = irq_params->adev;
440 struct drm_device *dev = adev_to_drm(adev);
441 unsigned long flags;
442 struct drm_pending_vblank_event *e;
443 u32 vpos, hpos, v_blank_start, v_blank_end;
444 bool vrr_active;
445
446 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
447
448 /* IRQ could occur when in initial stage */
449 /* TODO work and BO cleanup */
450 if (amdgpu_crtc == NULL) {
451 drm_dbg_state(dev, "CRTC is null, returning.\n");
452 return;
453 }
454
455 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
456
457 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
458 drm_dbg_state(dev,
459 "amdgpu_crtc->pflip_status = %d != AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p]\n",
460 amdgpu_crtc->pflip_status, AMDGPU_FLIP_SUBMITTED,
461 amdgpu_crtc->crtc_id, amdgpu_crtc);
462 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
463 return;
464 }
465
466 /* page flip completed. */
467 e = amdgpu_crtc->event;
468 amdgpu_crtc->event = NULL;
469
470 WARN_ON(!e);
471
472 vrr_active = amdgpu_dm_crtc_vrr_active_irq(amdgpu_crtc);
473
474 /* Fixed refresh rate, or VRR scanout position outside front-porch? */
475 if (!vrr_active ||
476 !dc_stream_get_scanoutpos(amdgpu_crtc->dm_irq_params.stream, &v_blank_start,
477 &v_blank_end, &hpos, &vpos) ||
478 (vpos < v_blank_start)) {
479 /* Update to correct count and vblank timestamp if racing with
480 * vblank irq. This also updates to the correct vblank timestamp
481 * even in VRR mode, as scanout is past the front-porch atm.
482 */
483 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
484
485 /* Wake up userspace by sending the pageflip event with proper
486 * count and timestamp of vblank of flip completion.
487 */
488 if (e) {
489 drm_crtc_send_vblank_event(&amdgpu_crtc->base, e);
490
491 /* Event sent, so done with vblank for this flip */
492 drm_crtc_vblank_put(&amdgpu_crtc->base);
493 }
494 } else if (e) {
495 /* VRR active and inside front-porch: vblank count and
496 * timestamp for pageflip event will only be up to date after
497 * drm_crtc_handle_vblank() has been executed from late vblank
498 * irq handler after start of back-porch (vline 0). We queue the
499 * pageflip event for send-out by drm_crtc_handle_vblank() with
500 * updated timestamp and count, once it runs after us.
501 *
502 * We need to open-code this instead of using the helper
503 * drm_crtc_arm_vblank_event(), as that helper would
504 * call drm_crtc_accurate_vblank_count(), which we must
505 * not call in VRR mode while we are in front-porch!
506 */
507
508 /* sequence will be replaced by real count during send-out. */
509 e->sequence = drm_crtc_vblank_count(&amdgpu_crtc->base);
510 e->pipe = amdgpu_crtc->crtc_id;
511
512 list_add_tail(&e->base.link, &adev_to_drm(adev)->vblank_event_list);
513 e = NULL;
514 }
515
516 /* Keep track of vblank of this flip for flip throttling. We use the
517 * cooked hw counter, as that one incremented at start of this vblank
518 * of pageflip completion, so last_flip_vblank is the forbidden count
519 * for queueing new pageflips if vsync + VRR is enabled.
520 */
521 amdgpu_crtc->dm_irq_params.last_flip_vblank =
522 amdgpu_get_vblank_counter_kms(&amdgpu_crtc->base);
523
524 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
525 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
526
527 drm_dbg_state(dev,
528 "crtc:%d[%p], pflip_stat:AMDGPU_FLIP_NONE, vrr[%d]-fp %d\n",
529 amdgpu_crtc->crtc_id, amdgpu_crtc, vrr_active, (int)!e);
530}
531
532static void dm_handle_vmin_vmax_update(struct work_struct *offload_work)
533{
534 struct vupdate_offload_work *work = container_of(offload_work, struct vupdate_offload_work, work);
535 struct amdgpu_device *adev = work->adev;
536 struct dc_stream_state *stream = work->stream;
537 struct dc_crtc_timing_adjust *adjust = work->adjust;
538
539 mutex_lock(&adev->dm.dc_lock);
540 dc_stream_adjust_vmin_vmax(adev->dm.dc, stream, adjust);
541 mutex_unlock(&adev->dm.dc_lock);
542
543 dc_stream_release(stream);
544 kfree(work->adjust);
545 kfree(work);
546}
547
548static void schedule_dc_vmin_vmax(struct amdgpu_device *adev,
549 struct dc_stream_state *stream,
550 struct dc_crtc_timing_adjust *adjust)
551{
552 struct vupdate_offload_work *offload_work = kzalloc_obj(*offload_work,
553 GFP_NOWAIT);
554 if (!offload_work) {
555 drm_dbg_driver(adev_to_drm(adev), "Failed to allocate vupdate_offload_work\n");
556 return;
557 }
558
559 struct dc_crtc_timing_adjust *adjust_copy = kzalloc_obj(*adjust_copy,
560 GFP_NOWAIT);
561 if (!adjust_copy) {
562 drm_dbg_driver(adev_to_drm(adev), "Failed to allocate adjust_copy\n");
563 kfree(offload_work);
564 return;
565 }
566
567 dc_stream_retain(stream);
568 memcpy(adjust_copy, adjust, sizeof(*adjust_copy));
569
570 INIT_WORK(&offload_work->work, dm_handle_vmin_vmax_update);
571 offload_work->adev = adev;
572 offload_work->stream = stream;
573 offload_work->adjust = adjust_copy;
574
575 queue_work(system_wq, &offload_work->work);
576}
577
578static void dm_vupdate_high_irq(void *interrupt_params)
579{
580 struct common_irq_params *irq_params = interrupt_params;
581 struct amdgpu_device *adev = irq_params->adev;
582 struct amdgpu_crtc *acrtc;
583 struct drm_device *drm_dev;
584 struct drm_vblank_crtc *vblank;
585 ktime_t frame_duration_ns, previous_timestamp;
586 unsigned long flags;
587 int vrr_active;
588
589 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VUPDATE);
590
591 if (acrtc) {
592 vrr_active = amdgpu_dm_crtc_vrr_active_irq(acrtc);
593 drm_dev = acrtc->base.dev;
594 vblank = drm_crtc_vblank_crtc(&acrtc->base);
595 previous_timestamp = atomic64_read(&irq_params->previous_timestamp);
596 frame_duration_ns = vblank->time - previous_timestamp;
597
598 if (frame_duration_ns > 0) {
599 trace_amdgpu_refresh_rate_track(acrtc->base.index,
600 frame_duration_ns,
601 ktime_divns(NSEC_PER_SEC, frame_duration_ns));
602 atomic64_set(&irq_params->previous_timestamp, vblank->time);
603 }
604
605 drm_dbg_vbl(drm_dev,
606 "crtc:%d, vupdate-vrr:%d\n", acrtc->crtc_id,
607 vrr_active);
608
609 /* Core vblank handling is done here after end of front-porch in
610 * vrr mode, as vblank timestamping will give valid results
611 * while now done after front-porch. This will also deliver
612 * page-flip completion events that have been queued to us
613 * if a pageflip happened inside front-porch.
614 */
615 if (vrr_active && acrtc->dm_irq_params.stream) {
616 bool replay_en = acrtc->dm_irq_params.stream->link->replay_settings.replay_feature_enabled;
617 bool psr_en = acrtc->dm_irq_params.stream->link->psr_settings.psr_feature_enabled;
618 bool fs_active_var_en = acrtc->dm_irq_params.freesync_config.state
619 == VRR_STATE_ACTIVE_VARIABLE;
620
621 amdgpu_dm_crtc_handle_vblank(acrtc);
622
623 /* BTR processing for pre-DCE12 ASICs */
624 if (adev->family < AMDGPU_FAMILY_AI) {
625 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
626 mod_freesync_handle_v_update(
627 adev->dm.freesync_module,
628 acrtc->dm_irq_params.stream,
629 &acrtc->dm_irq_params.vrr_params);
630
631 if (fs_active_var_en || (!fs_active_var_en && !replay_en && !psr_en)) {
632 schedule_dc_vmin_vmax(adev,
633 acrtc->dm_irq_params.stream,
634 &acrtc->dm_irq_params.vrr_params.adjust);
635 }
636 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
637 }
638 }
639 }
640}
641
642/**
643 * dm_crtc_high_irq() - Handles CRTC interrupt
644 * @interrupt_params: used for determining the CRTC instance
645 *
646 * Handles the CRTC/VSYNC interrupt by notfying DRM's VBLANK
647 * event handler.
648 */
649static void dm_crtc_high_irq(void *interrupt_params)
650{
651 struct common_irq_params *irq_params = interrupt_params;
652 struct amdgpu_device *adev = irq_params->adev;
653 struct drm_writeback_job *job;
654 struct amdgpu_crtc *acrtc;
655 unsigned long flags;
656 int vrr_active;
657
658 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
659 if (!acrtc)
660 return;
661
662 if (acrtc->wb_conn) {
663 spin_lock_irqsave(&acrtc->wb_conn->job_lock, flags);
664
665 if (acrtc->wb_pending) {
666 job = list_first_entry_or_null(&acrtc->wb_conn->job_queue,
667 struct drm_writeback_job,
668 list_entry);
669 acrtc->wb_pending = false;
670 spin_unlock_irqrestore(&acrtc->wb_conn->job_lock, flags);
671
672 if (job) {
673 unsigned int v_total, refresh_hz;
674 struct dc_stream_state *stream = acrtc->dm_irq_params.stream;
675
676 v_total = stream->adjust.v_total_max ?
677 stream->adjust.v_total_max : stream->timing.v_total;
678 refresh_hz = div_u64((uint64_t) stream->timing.pix_clk_100hz *
679 100LL, (v_total * stream->timing.h_total));
680 mdelay(1000 / refresh_hz);
681
682 drm_writeback_signal_completion(acrtc->wb_conn, 0);
683 dc_stream_fc_disable_writeback(adev->dm.dc,
684 acrtc->dm_irq_params.stream, 0);
685 }
686 } else
687 spin_unlock_irqrestore(&acrtc->wb_conn->job_lock, flags);
688 }
689
690 vrr_active = amdgpu_dm_crtc_vrr_active_irq(acrtc);
691
692 drm_dbg_vbl(adev_to_drm(adev),
693 "crtc:%d, vupdate-vrr:%d, planes:%d\n", acrtc->crtc_id,
694 vrr_active, acrtc->dm_irq_params.active_planes);
695
696 /**
697 * Core vblank handling at start of front-porch is only possible
698 * in non-vrr mode, as only there vblank timestamping will give
699 * valid results while done in front-porch. Otherwise defer it
700 * to dm_vupdate_high_irq after end of front-porch.
701 */
702 if (!vrr_active)
703 amdgpu_dm_crtc_handle_vblank(acrtc);
704
705 /**
706 * Following stuff must happen at start of vblank, for crc
707 * computation and below-the-range btr support in vrr mode.
708 */
709 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
710
711 /* BTR updates need to happen before VUPDATE on Vega and above. */
712 if (adev->family < AMDGPU_FAMILY_AI)
713 return;
714
715 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
716
717 if (acrtc->dm_irq_params.stream &&
718 acrtc->dm_irq_params.vrr_params.supported) {
719 bool replay_en = acrtc->dm_irq_params.stream->link->replay_settings.replay_feature_enabled;
720 bool psr_en = acrtc->dm_irq_params.stream->link->psr_settings.psr_feature_enabled;
721 bool fs_active_var_en = acrtc->dm_irq_params.freesync_config.state == VRR_STATE_ACTIVE_VARIABLE;
722
723 mod_freesync_handle_v_update(adev->dm.freesync_module,
724 acrtc->dm_irq_params.stream,
725 &acrtc->dm_irq_params.vrr_params);
726
727 /* update vmin_vmax only if freesync is enabled, or only if PSR and REPLAY are disabled */
728 if (fs_active_var_en || (!fs_active_var_en && !replay_en && !psr_en)) {
729 schedule_dc_vmin_vmax(adev, acrtc->dm_irq_params.stream,
730 &acrtc->dm_irq_params.vrr_params.adjust);
731 }
732 }
733
734 /*
735 * If there aren't any active_planes then DCH HUBP may be clock-gated.
736 * In that case, pageflip completion interrupts won't fire and pageflip
737 * completion events won't get delivered. Prevent this by sending
738 * pending pageflip events from here if a flip is still pending.
739 *
740 * If any planes are enabled, use dm_pflip_high_irq() instead, to
741 * avoid race conditions between flip programming and completion,
742 * which could cause too early flip completion events.
743 */
744 if (adev->family >= AMDGPU_FAMILY_RV &&
745 acrtc->pflip_status == AMDGPU_FLIP_SUBMITTED &&
746 acrtc->dm_irq_params.active_planes == 0) {
747 if (acrtc->event) {
748 drm_crtc_send_vblank_event(&acrtc->base, acrtc->event);
749 acrtc->event = NULL;
750 drm_crtc_vblank_put(&acrtc->base);
751 }
752 acrtc->pflip_status = AMDGPU_FLIP_NONE;
753 }
754
755 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
756}
757
758#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
759/**
760 * dm_dcn_vertical_interrupt0_high_irq() - Handles OTG Vertical interrupt0 for
761 * DCN generation ASICs
762 * @interrupt_params: interrupt parameters
763 *
764 * Used to set crc window/read out crc value at vertical line 0 position
765 */
766static void dm_dcn_vertical_interrupt0_high_irq(void *interrupt_params)
767{
768 struct common_irq_params *irq_params = interrupt_params;
769 struct amdgpu_device *adev = irq_params->adev;
770 struct amdgpu_crtc *acrtc;
771
772 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VLINE0);
773
774 if (!acrtc)
775 return;
776
777 amdgpu_dm_crtc_handle_crc_window_irq(&acrtc->base);
778}
779#endif /* CONFIG_DRM_AMD_SECURE_DISPLAY */
780
781/**
782 * dmub_aux_setconfig_callback - Callback for AUX or SET_CONFIG command.
783 * @adev: amdgpu_device pointer
784 * @notify: dmub notification structure
785 *
786 * Dmub AUX or SET_CONFIG command completion processing callback
787 * Copies dmub notification to DM which is to be read by AUX command.
788 * issuing thread and also signals the event to wake up the thread.
789 */
790static void dmub_aux_setconfig_callback(struct amdgpu_device *adev,
791 struct dmub_notification *notify)
792{
793 if (adev->dm.dmub_notify)
794 memcpy(adev->dm.dmub_notify, notify, sizeof(struct dmub_notification));
795 if (notify->type == DMUB_NOTIFICATION_AUX_REPLY)
796 complete(&adev->dm.dmub_aux_transfer_done);
797}
798
799static void dmub_aux_fused_io_callback(struct amdgpu_device *adev,
800 struct dmub_notification *notify)
801{
802 if (!adev || !notify) {
803 ASSERT(false);
804 return;
805 }
806
807 const struct dmub_cmd_fused_request *req = ¬ify->fused_request;
808 const uint8_t ddc_line = req->u.aux.ddc_line;
809
810 if (ddc_line >= ARRAY_SIZE(adev->dm.fused_io)) {
811 ASSERT(false);
812 return;
813 }
814
815 struct fused_io_sync *sync = &adev->dm.fused_io[ddc_line];
816
817 static_assert(sizeof(*req) <= sizeof(sync->reply_data), "Size mismatch");
818 memcpy(sync->reply_data, req, sizeof(*req));
819 complete(&sync->replied);
820}
821
822/**
823 * dmub_hpd_callback - DMUB HPD interrupt processing callback.
824 * @adev: amdgpu_device pointer
825 * @notify: dmub notification structure
826 *
827 * Dmub Hpd interrupt processing callback. Gets displayindex through the
828 * ink index and calls helper to do the processing.
829 */
830static void dmub_hpd_callback(struct amdgpu_device *adev,
831 struct dmub_notification *notify)
832{
833 struct amdgpu_dm_connector *aconnector;
834 struct amdgpu_dm_connector *hpd_aconnector = NULL;
835 struct drm_connector *connector;
836 struct drm_connector_list_iter iter;
837 struct dc_link *link;
838 u8 link_index = 0;
839 struct drm_device *dev;
840
841 if (adev == NULL)
842 return;
843
844 if (notify == NULL) {
845 drm_err(adev_to_drm(adev), "DMUB HPD callback notification was NULL");
846 return;
847 }
848
849 if (notify->link_index > adev->dm.dc->link_count) {
850 drm_err(adev_to_drm(adev), "DMUB HPD index (%u)is abnormal", notify->link_index);
851 return;
852 }
853
854 /* Skip DMUB HPD IRQ in suspend/resume. We will probe them later. */
855 if (notify->type == DMUB_NOTIFICATION_HPD && adev->in_suspend) {
856 drm_info(adev_to_drm(adev), "Skip DMUB HPD IRQ callback in suspend/resume\n");
857 return;
858 }
859
860 link_index = notify->link_index;
861 link = adev->dm.dc->links[link_index];
862 dev = adev->dm.ddev;
863
864 drm_connector_list_iter_begin(dev, &iter);
865 drm_for_each_connector_iter(connector, &iter) {
866
867 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
868 continue;
869
870 aconnector = to_amdgpu_dm_connector(connector);
871 if (link && aconnector->dc_link == link) {
872 if (notify->type == DMUB_NOTIFICATION_HPD)
873 drm_info(adev_to_drm(adev), "DMUB HPD IRQ callback: link_index=%u\n", link_index);
874 else if (notify->type == DMUB_NOTIFICATION_HPD_IRQ)
875 drm_info(adev_to_drm(adev), "DMUB HPD RX IRQ callback: link_index=%u\n", link_index);
876 else
877 drm_warn(adev_to_drm(adev), "DMUB Unknown HPD callback type %d, link_index=%u\n",
878 notify->type, link_index);
879
880 hpd_aconnector = aconnector;
881 break;
882 }
883 }
884 drm_connector_list_iter_end(&iter);
885
886 if (hpd_aconnector) {
887 if (notify->type == DMUB_NOTIFICATION_HPD) {
888 if (hpd_aconnector->dc_link->hpd_status == (notify->hpd_status == DP_HPD_PLUG))
889 drm_warn(adev_to_drm(adev), "DMUB reported hpd status unchanged. link_index=%u\n", link_index);
890 handle_hpd_irq_helper(hpd_aconnector);
891 } else if (notify->type == DMUB_NOTIFICATION_HPD_IRQ) {
892 handle_hpd_rx_irq(hpd_aconnector);
893 }
894 }
895}
896
897/**
898 * dmub_hpd_sense_callback - DMUB HPD sense processing callback.
899 * @adev: amdgpu_device pointer
900 * @notify: dmub notification structure
901 *
902 * HPD sense changes can occur during low power states and need to be
903 * notified from firmware to driver.
904 */
905static void dmub_hpd_sense_callback(struct amdgpu_device *adev,
906 struct dmub_notification *notify)
907{
908 drm_dbg_driver(adev_to_drm(adev), "DMUB HPD SENSE callback.\n");
909}
910
911/**
912 * register_dmub_notify_callback - Sets callback for DMUB notify
913 * @adev: amdgpu_device pointer
914 * @type: Type of dmub notification
915 * @callback: Dmub interrupt callback function
916 * @dmub_int_thread_offload: offload indicator
917 *
918 * API to register a dmub callback handler for a dmub notification
919 * Also sets indicator whether callback processing to be offloaded.
920 * to dmub interrupt handling thread
921 * Return: true if successfully registered, false if there is existing registration
922 */
923static bool register_dmub_notify_callback(struct amdgpu_device *adev,
924 enum dmub_notification_type type,
925 dmub_notify_interrupt_callback_t callback,
926 bool dmub_int_thread_offload)
927{
928 if (callback != NULL && type < ARRAY_SIZE(adev->dm.dmub_thread_offload)) {
929 adev->dm.dmub_callback[type] = callback;
930 adev->dm.dmub_thread_offload[type] = dmub_int_thread_offload;
931 } else
932 return false;
933
934 return true;
935}
936
937static void dm_handle_hpd_work(struct work_struct *work)
938{
939 struct dmub_hpd_work *dmub_hpd_wrk;
940
941 dmub_hpd_wrk = container_of(work, struct dmub_hpd_work, handle_hpd_work);
942
943 if (!dmub_hpd_wrk->dmub_notify) {
944 drm_err(adev_to_drm(dmub_hpd_wrk->adev), "dmub_hpd_wrk dmub_notify is NULL");
945 return;
946 }
947
948 if (dmub_hpd_wrk->dmub_notify->type < ARRAY_SIZE(dmub_hpd_wrk->adev->dm.dmub_callback)) {
949 dmub_hpd_wrk->adev->dm.dmub_callback[dmub_hpd_wrk->dmub_notify->type](dmub_hpd_wrk->adev,
950 dmub_hpd_wrk->dmub_notify);
951 }
952
953 kfree(dmub_hpd_wrk->dmub_notify);
954 kfree(dmub_hpd_wrk);
955
956}
957
958static const char *dmub_notification_type_str(enum dmub_notification_type e)
959{
960 switch (e) {
961 case DMUB_NOTIFICATION_NO_DATA:
962 return "NO_DATA";
963 case DMUB_NOTIFICATION_AUX_REPLY:
964 return "AUX_REPLY";
965 case DMUB_NOTIFICATION_HPD:
966 return "HPD";
967 case DMUB_NOTIFICATION_HPD_IRQ:
968 return "HPD_IRQ";
969 case DMUB_NOTIFICATION_SET_CONFIG_REPLY:
970 return "SET_CONFIG_REPLY";
971 case DMUB_NOTIFICATION_DPIA_NOTIFICATION:
972 return "DPIA_NOTIFICATION";
973 case DMUB_NOTIFICATION_HPD_SENSE_NOTIFY:
974 return "HPD_SENSE_NOTIFY";
975 case DMUB_NOTIFICATION_FUSED_IO:
976 return "FUSED_IO";
977 default:
978 return "<unknown>";
979 }
980}
981
982#define DMUB_TRACE_MAX_READ 64
983/**
984 * dm_dmub_outbox1_low_irq() - Handles Outbox interrupt
985 * @interrupt_params: used for determining the Outbox instance
986 *
987 * Handles the Outbox Interrupt
988 * event handler.
989 */
990static void dm_dmub_outbox1_low_irq(void *interrupt_params)
991{
992 struct dmub_notification notify = {0};
993 struct common_irq_params *irq_params = interrupt_params;
994 struct amdgpu_device *adev = irq_params->adev;
995 struct amdgpu_display_manager *dm = &adev->dm;
996 struct dmcub_trace_buf_entry entry = { 0 };
997 u32 count = 0;
998 struct dmub_hpd_work *dmub_hpd_wrk;
999
1000 do {
1001 if (dc_dmub_srv_get_dmub_outbox0_msg(dm->dc, &entry)) {
1002 trace_amdgpu_dmub_trace_high_irq(entry.trace_code, entry.tick_count,
1003 entry.param0, entry.param1);
1004
1005 drm_dbg_driver(adev_to_drm(adev), "trace_code:%u, tick_count:%u, param0:%u, param1:%u\n",
1006 entry.trace_code, entry.tick_count, entry.param0, entry.param1);
1007 } else
1008 break;
1009
1010 count++;
1011
1012 } while (count <= DMUB_TRACE_MAX_READ);
1013
1014 if (count > DMUB_TRACE_MAX_READ)
1015 drm_dbg_driver(adev_to_drm(adev), "Warning : count > DMUB_TRACE_MAX_READ");
1016
1017 if (dc_enable_dmub_notifications(adev->dm.dc) &&
1018 irq_params->irq_src == DC_IRQ_SOURCE_DMCUB_OUTBOX) {
1019
1020 do {
1021 dc_stat_get_dmub_notification(adev->dm.dc, ¬ify);
1022 if (notify.type >= ARRAY_SIZE(dm->dmub_thread_offload)) {
1023 drm_err(adev_to_drm(adev), "DM: notify type %d invalid!", notify.type);
1024 continue;
1025 }
1026 if (!dm->dmub_callback[notify.type]) {
1027 drm_warn(adev_to_drm(adev), "DMUB notification skipped due to no handler: type=%s\n",
1028 dmub_notification_type_str(notify.type));
1029 continue;
1030 }
1031 if (dm->dmub_thread_offload[notify.type] == true) {
1032 dmub_hpd_wrk = kzalloc_obj(*dmub_hpd_wrk,
1033 GFP_ATOMIC);
1034 if (!dmub_hpd_wrk) {
1035 drm_err(adev_to_drm(adev), "Failed to allocate dmub_hpd_wrk");
1036 return;
1037 }
1038 dmub_hpd_wrk->dmub_notify = kmemdup(¬ify, sizeof(struct dmub_notification),
1039 GFP_ATOMIC);
1040 if (!dmub_hpd_wrk->dmub_notify) {
1041 kfree(dmub_hpd_wrk);
1042 drm_err(adev_to_drm(adev), "Failed to allocate dmub_hpd_wrk->dmub_notify");
1043 return;
1044 }
1045 INIT_WORK(&dmub_hpd_wrk->handle_hpd_work, dm_handle_hpd_work);
1046 dmub_hpd_wrk->adev = adev;
1047 queue_work(adev->dm.delayed_hpd_wq, &dmub_hpd_wrk->handle_hpd_work);
1048 } else {
1049 dm->dmub_callback[notify.type](adev, ¬ify);
1050 }
1051 } while (notify.pending_notification);
1052 }
1053}
1054
1055static int dm_set_clockgating_state(struct amdgpu_ip_block *ip_block,
1056 enum amd_clockgating_state state)
1057{
1058 return 0;
1059}
1060
1061static int dm_set_powergating_state(struct amdgpu_ip_block *ip_block,
1062 enum amd_powergating_state state)
1063{
1064 return 0;
1065}
1066
1067/* Prototypes of private functions */
1068static int dm_early_init(struct amdgpu_ip_block *ip_block);
1069
1070/* Allocate memory for FBC compressed data */
1071static void amdgpu_dm_fbc_init(struct drm_connector *connector)
1072{
1073 struct amdgpu_device *adev = drm_to_adev(connector->dev);
1074 struct dm_compressor_info *compressor = &adev->dm.compressor;
1075 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
1076 struct drm_display_mode *mode;
1077 unsigned long max_size = 0;
1078
1079 if (adev->dm.dc->fbc_compressor == NULL)
1080 return;
1081
1082 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
1083 return;
1084
1085 if (compressor->bo_ptr)
1086 return;
1087
1088
1089 list_for_each_entry(mode, &connector->modes, head) {
1090 if (max_size < (unsigned long) mode->htotal * mode->vtotal)
1091 max_size = (unsigned long) mode->htotal * mode->vtotal;
1092 }
1093
1094 if (max_size) {
1095 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
1096 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
1097 &compressor->gpu_addr, &compressor->cpu_addr);
1098
1099 if (r)
1100 drm_err(adev_to_drm(adev), "DM: Failed to initialize FBC\n");
1101 else {
1102 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
1103 drm_info(adev_to_drm(adev), "DM: FBC alloc %lu\n", max_size*4);
1104 }
1105
1106 }
1107
1108}
1109
1110static int amdgpu_dm_audio_component_get_eld(struct device *kdev, int port,
1111 int pipe, bool *enabled,
1112 unsigned char *buf, int max_bytes)
1113{
1114 struct drm_device *dev = dev_get_drvdata(kdev);
1115 struct amdgpu_device *adev = drm_to_adev(dev);
1116 struct drm_connector *connector;
1117 struct drm_connector_list_iter conn_iter;
1118 struct amdgpu_dm_connector *aconnector;
1119 int ret = 0;
1120
1121 *enabled = false;
1122
1123 mutex_lock(&adev->dm.audio_lock);
1124
1125 drm_connector_list_iter_begin(dev, &conn_iter);
1126 drm_for_each_connector_iter(connector, &conn_iter) {
1127
1128 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1129 continue;
1130
1131 aconnector = to_amdgpu_dm_connector(connector);
1132 if (aconnector->audio_inst != port)
1133 continue;
1134
1135 *enabled = true;
1136 mutex_lock(&connector->eld_mutex);
1137 ret = drm_eld_size(connector->eld);
1138 memcpy(buf, connector->eld, min(max_bytes, ret));
1139 mutex_unlock(&connector->eld_mutex);
1140
1141 break;
1142 }
1143 drm_connector_list_iter_end(&conn_iter);
1144
1145 mutex_unlock(&adev->dm.audio_lock);
1146
1147 drm_dbg_kms(adev_to_drm(adev), "Get ELD : idx=%d ret=%d en=%d\n", port, ret, *enabled);
1148
1149 return ret;
1150}
1151
1152static const struct drm_audio_component_ops amdgpu_dm_audio_component_ops = {
1153 .get_eld = amdgpu_dm_audio_component_get_eld,
1154};
1155
1156static int amdgpu_dm_audio_component_bind(struct device *kdev,
1157 struct device *hda_kdev, void *data)
1158{
1159 struct drm_device *dev = dev_get_drvdata(kdev);
1160 struct amdgpu_device *adev = drm_to_adev(dev);
1161 struct drm_audio_component *acomp = data;
1162
1163 acomp->ops = &amdgpu_dm_audio_component_ops;
1164 acomp->dev = kdev;
1165 adev->dm.audio_component = acomp;
1166
1167 return 0;
1168}
1169
1170static void amdgpu_dm_audio_component_unbind(struct device *kdev,
1171 struct device *hda_kdev, void *data)
1172{
1173 struct amdgpu_device *adev = drm_to_adev(dev_get_drvdata(kdev));
1174 struct drm_audio_component *acomp = data;
1175
1176 acomp->ops = NULL;
1177 acomp->dev = NULL;
1178 adev->dm.audio_component = NULL;
1179}
1180
1181static const struct component_ops amdgpu_dm_audio_component_bind_ops = {
1182 .bind = amdgpu_dm_audio_component_bind,
1183 .unbind = amdgpu_dm_audio_component_unbind,
1184};
1185
1186static int amdgpu_dm_audio_init(struct amdgpu_device *adev)
1187{
1188 int i, ret;
1189
1190 if (!amdgpu_audio)
1191 return 0;
1192
1193 adev->mode_info.audio.enabled = true;
1194
1195 adev->mode_info.audio.num_pins = adev->dm.dc->res_pool->audio_count;
1196
1197 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1198 adev->mode_info.audio.pin[i].channels = -1;
1199 adev->mode_info.audio.pin[i].rate = -1;
1200 adev->mode_info.audio.pin[i].bits_per_sample = -1;
1201 adev->mode_info.audio.pin[i].status_bits = 0;
1202 adev->mode_info.audio.pin[i].category_code = 0;
1203 adev->mode_info.audio.pin[i].connected = false;
1204 adev->mode_info.audio.pin[i].id =
1205 adev->dm.dc->res_pool->audios[i]->inst;
1206 adev->mode_info.audio.pin[i].offset = 0;
1207 }
1208
1209 ret = component_add(adev->dev, &amdgpu_dm_audio_component_bind_ops);
1210 if (ret < 0)
1211 return ret;
1212
1213 adev->dm.audio_registered = true;
1214
1215 return 0;
1216}
1217
1218static void amdgpu_dm_audio_fini(struct amdgpu_device *adev)
1219{
1220 if (!amdgpu_audio)
1221 return;
1222
1223 if (!adev->mode_info.audio.enabled)
1224 return;
1225
1226 if (adev->dm.audio_registered) {
1227 component_del(adev->dev, &amdgpu_dm_audio_component_bind_ops);
1228 adev->dm.audio_registered = false;
1229 }
1230
1231 /* TODO: Disable audio? */
1232
1233 adev->mode_info.audio.enabled = false;
1234}
1235
1236static void amdgpu_dm_audio_eld_notify(struct amdgpu_device *adev, int pin)
1237{
1238 struct drm_audio_component *acomp = adev->dm.audio_component;
1239
1240 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify) {
1241 drm_dbg_kms(adev_to_drm(adev), "Notify ELD: %d\n", pin);
1242
1243 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
1244 pin, -1);
1245 }
1246}
1247
1248static int dm_dmub_hw_init(struct amdgpu_device *adev)
1249{
1250 const struct dmcub_firmware_header_v1_0 *hdr;
1251 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
1252 struct dmub_srv_fb_info *fb_info = adev->dm.dmub_fb_info;
1253 const struct firmware *dmub_fw = adev->dm.dmub_fw;
1254 struct dc *dc = adev->dm.dc;
1255 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu;
1256 struct abm *abm = adev->dm.dc->res_pool->abm;
1257 struct dc_context *ctx = adev->dm.dc->ctx;
1258 struct dmub_srv_hw_params hw_params;
1259 enum dmub_status status;
1260 const unsigned char *fw_inst_const, *fw_bss_data;
1261 u32 i, fw_inst_const_size, fw_bss_data_size;
1262 bool has_hw_support;
1263
1264 if (!dmub_srv)
1265 /* DMUB isn't supported on the ASIC. */
1266 return 0;
1267
1268 if (!fb_info) {
1269 drm_err(adev_to_drm(adev), "No framebuffer info for DMUB service.\n");
1270 return -EINVAL;
1271 }
1272
1273 if (!dmub_fw) {
1274 /* Firmware required for DMUB support. */
1275 drm_err(adev_to_drm(adev), "No firmware provided for DMUB.\n");
1276 return -EINVAL;
1277 }
1278
1279 /* initialize register offsets for ASICs with runtime initialization available */
1280 if (dmub_srv->hw_funcs.init_reg_offsets)
1281 dmub_srv->hw_funcs.init_reg_offsets(dmub_srv, ctx);
1282
1283 status = dmub_srv_has_hw_support(dmub_srv, &has_hw_support);
1284 if (status != DMUB_STATUS_OK) {
1285 drm_err(adev_to_drm(adev), "Error checking HW support for DMUB: %d\n", status);
1286 return -EINVAL;
1287 }
1288
1289 if (!has_hw_support) {
1290 drm_info(adev_to_drm(adev), "DMUB unsupported on ASIC\n");
1291 return 0;
1292 }
1293
1294 /* Reset DMCUB if it was previously running - before we overwrite its memory. */
1295 status = dmub_srv_hw_reset(dmub_srv);
1296 if (status != DMUB_STATUS_OK)
1297 drm_warn(adev_to_drm(adev), "Error resetting DMUB HW: %d\n", status);
1298
1299 hdr = (const struct dmcub_firmware_header_v1_0 *)dmub_fw->data;
1300
1301 fw_inst_const = dmub_fw->data +
1302 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1303 PSP_HEADER_BYTES_256;
1304
1305 fw_bss_data = dmub_fw->data +
1306 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1307 le32_to_cpu(hdr->inst_const_bytes);
1308
1309 /* Copy firmware and bios info into FB memory. */
1310 fw_inst_const_size = adev->dm.fw_inst_size;
1311
1312 fw_bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
1313
1314 /* if adev->firmware.load_type == AMDGPU_FW_LOAD_PSP,
1315 * amdgpu_ucode_init_single_fw will load dmub firmware
1316 * fw_inst_const part to cw0; otherwise, the firmware back door load
1317 * will be done by dm_dmub_hw_init
1318 */
1319 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1320 memcpy(fb_info->fb[DMUB_WINDOW_0_INST_CONST].cpu_addr, fw_inst_const,
1321 fw_inst_const_size);
1322 }
1323
1324 if (fw_bss_data_size)
1325 memcpy(fb_info->fb[DMUB_WINDOW_2_BSS_DATA].cpu_addr,
1326 fw_bss_data, fw_bss_data_size);
1327
1328 /* Copy firmware bios info into FB memory. */
1329 memcpy(fb_info->fb[DMUB_WINDOW_3_VBIOS].cpu_addr, adev->bios,
1330 adev->bios_size);
1331
1332 /* Reset regions that need to be reset. */
1333 memset(fb_info->fb[DMUB_WINDOW_4_MAILBOX].cpu_addr, 0,
1334 fb_info->fb[DMUB_WINDOW_4_MAILBOX].size);
1335
1336 memset(fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].cpu_addr, 0,
1337 fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].size);
1338
1339 memset(fb_info->fb[DMUB_WINDOW_6_FW_STATE].cpu_addr, 0,
1340 fb_info->fb[DMUB_WINDOW_6_FW_STATE].size);
1341
1342 memset(fb_info->fb[DMUB_WINDOW_SHARED_STATE].cpu_addr, 0,
1343 fb_info->fb[DMUB_WINDOW_SHARED_STATE].size);
1344
1345 /* Initialize hardware. */
1346 memset(&hw_params, 0, sizeof(hw_params));
1347 hw_params.soc_fb_info.fb_base = adev->gmc.fb_start;
1348 hw_params.soc_fb_info.fb_offset = adev->vm_manager.vram_base_offset;
1349
1350 /* backdoor load firmware and trigger dmub running */
1351 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
1352 hw_params.load_inst_const = true;
1353
1354 if (dmcu)
1355 hw_params.psp_version = dmcu->psp_version;
1356
1357 for (i = 0; i < fb_info->num_fb; ++i)
1358 hw_params.fb[i] = &fb_info->fb[i];
1359
1360 /* Enable usb4 dpia in the FW APU */
1361 if (dc->caps.is_apu &&
1362 dc->res_pool->usb4_dpia_count != 0 &&
1363 !dc->debug.dpia_debug.bits.disable_dpia) {
1364 hw_params.dpia_supported = true;
1365 hw_params.disable_dpia = dc->debug.dpia_debug.bits.disable_dpia;
1366 hw_params.dpia_hpd_int_enable_supported = false;
1367 hw_params.enable_non_transparent_setconfig = dc->config.consolidated_dpia_dp_lt;
1368 hw_params.disable_dpia_bw_allocation = !dc->config.usb4_bw_alloc_support;
1369 }
1370
1371 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1372 case IP_VERSION(3, 5, 0):
1373 case IP_VERSION(3, 5, 1):
1374 case IP_VERSION(3, 6, 0):
1375 case IP_VERSION(4, 2, 0):
1376 hw_params.ips_sequential_ono = adev->external_rev_id > 0x10;
1377 hw_params.lower_hbr3_phy_ssc = true;
1378 break;
1379 default:
1380 break;
1381 }
1382
1383 status = dmub_srv_hw_init(dmub_srv, &hw_params);
1384 if (status != DMUB_STATUS_OK) {
1385 drm_err(adev_to_drm(adev), "Error initializing DMUB HW: %d\n", status);
1386 return -EINVAL;
1387 }
1388
1389 /* Wait for firmware load to finish. */
1390 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
1391 if (status != DMUB_STATUS_OK)
1392 drm_warn(adev_to_drm(adev), "Wait for DMUB auto-load failed: %d\n", status);
1393
1394 /* Init DMCU and ABM if available. */
1395 if (dmcu && abm) {
1396 dmcu->funcs->dmcu_init(dmcu);
1397 abm->dmcu_is_running = dmcu->funcs->is_dmcu_initialized(dmcu);
1398 }
1399
1400 if (!adev->dm.dc->ctx->dmub_srv)
1401 adev->dm.dc->ctx->dmub_srv = dc_dmub_srv_create(adev->dm.dc, dmub_srv);
1402 if (!adev->dm.dc->ctx->dmub_srv) {
1403 drm_err(adev_to_drm(adev), "Couldn't allocate DC DMUB server!\n");
1404 return -ENOMEM;
1405 }
1406
1407 drm_info(adev_to_drm(adev), "DMUB hardware initialized: version=0x%08X\n",
1408 adev->dm.dmcub_fw_version);
1409
1410 /* Keeping sanity checks off if
1411 * DCN31 >= 4.0.59.0
1412 * DCN314 >= 8.0.16.0
1413 * Otherwise, turn on sanity checks
1414 */
1415 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1416 case IP_VERSION(3, 1, 2):
1417 case IP_VERSION(3, 1, 3):
1418 if (adev->dm.dmcub_fw_version &&
1419 adev->dm.dmcub_fw_version >= DMUB_FW_VERSION(4, 0, 0) &&
1420 adev->dm.dmcub_fw_version < DMUB_FW_VERSION(4, 0, 59))
1421 adev->dm.dc->debug.sanity_checks = true;
1422 break;
1423 case IP_VERSION(3, 1, 4):
1424 if (adev->dm.dmcub_fw_version &&
1425 adev->dm.dmcub_fw_version >= DMUB_FW_VERSION(4, 0, 0) &&
1426 adev->dm.dmcub_fw_version < DMUB_FW_VERSION(8, 0, 16))
1427 adev->dm.dc->debug.sanity_checks = true;
1428 break;
1429 default:
1430 break;
1431 }
1432
1433 return 0;
1434}
1435
1436static void dm_dmub_hw_resume(struct amdgpu_device *adev)
1437{
1438 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
1439 enum dmub_status status;
1440 bool init;
1441 int r;
1442
1443 if (!dmub_srv) {
1444 /* DMUB isn't supported on the ASIC. */
1445 return;
1446 }
1447
1448 status = dmub_srv_is_hw_init(dmub_srv, &init);
1449 if (status != DMUB_STATUS_OK)
1450 drm_warn(adev_to_drm(adev), "DMUB hardware init check failed: %d\n", status);
1451
1452 if (status == DMUB_STATUS_OK && init) {
1453 /* Wait for firmware load to finish. */
1454 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
1455 if (status != DMUB_STATUS_OK)
1456 drm_warn(adev_to_drm(adev), "Wait for DMUB auto-load failed: %d\n", status);
1457 } else {
1458 /* Perform the full hardware initialization. */
1459 r = dm_dmub_hw_init(adev);
1460 if (r)
1461 drm_err(adev_to_drm(adev), "DMUB interface failed to initialize: status=%d\n", r);
1462 }
1463}
1464
1465static void mmhub_read_system_context(struct amdgpu_device *adev, struct dc_phy_addr_space_config *pa_config)
1466{
1467 u64 pt_base;
1468 u32 logical_addr_low;
1469 u32 logical_addr_high;
1470 u32 agp_base, agp_bot, agp_top;
1471 PHYSICAL_ADDRESS_LOC page_table_start, page_table_end, page_table_base;
1472
1473 memset(pa_config, 0, sizeof(*pa_config));
1474
1475 agp_base = 0;
1476 agp_bot = adev->gmc.agp_start >> 24;
1477 agp_top = adev->gmc.agp_end >> 24;
1478
1479 /* AGP aperture is disabled */
1480 if (agp_bot > agp_top) {
1481 logical_addr_low = adev->gmc.fb_start >> 18;
1482 if (adev->apu_flags & (AMD_APU_IS_RAVEN2 |
1483 AMD_APU_IS_RENOIR |
1484 AMD_APU_IS_GREEN_SARDINE))
1485 /*
1486 * Raven2 has a HW issue that it is unable to use the vram which
1487 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the
1488 * workaround that increase system aperture high address (add 1)
1489 * to get rid of the VM fault and hardware hang.
1490 */
1491 logical_addr_high = (adev->gmc.fb_end >> 18) + 0x1;
1492 else
1493 logical_addr_high = adev->gmc.fb_end >> 18;
1494 } else {
1495 logical_addr_low = min(adev->gmc.fb_start, adev->gmc.agp_start) >> 18;
1496 if (adev->apu_flags & (AMD_APU_IS_RAVEN2 |
1497 AMD_APU_IS_RENOIR |
1498 AMD_APU_IS_GREEN_SARDINE))
1499 /*
1500 * Raven2 has a HW issue that it is unable to use the vram which
1501 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the
1502 * workaround that increase system aperture high address (add 1)
1503 * to get rid of the VM fault and hardware hang.
1504 */
1505 logical_addr_high = max((adev->gmc.fb_end >> 18) + 0x1, adev->gmc.agp_end >> 18);
1506 else
1507 logical_addr_high = max(adev->gmc.fb_end, adev->gmc.agp_end) >> 18;
1508 }
1509
1510 pt_base = amdgpu_gmc_pd_addr(adev->gart.bo);
1511
1512 page_table_start.high_part = upper_32_bits(adev->gmc.gart_start >>
1513 AMDGPU_GPU_PAGE_SHIFT);
1514 page_table_start.low_part = lower_32_bits(adev->gmc.gart_start >>
1515 AMDGPU_GPU_PAGE_SHIFT);
1516 page_table_end.high_part = upper_32_bits(adev->gmc.gart_end >>
1517 AMDGPU_GPU_PAGE_SHIFT);
1518 page_table_end.low_part = lower_32_bits(adev->gmc.gart_end >>
1519 AMDGPU_GPU_PAGE_SHIFT);
1520 page_table_base.high_part = upper_32_bits(pt_base);
1521 page_table_base.low_part = lower_32_bits(pt_base);
1522
1523 pa_config->system_aperture.start_addr = (uint64_t)logical_addr_low << 18;
1524 pa_config->system_aperture.end_addr = (uint64_t)logical_addr_high << 18;
1525
1526 pa_config->system_aperture.agp_base = (uint64_t)agp_base << 24;
1527 pa_config->system_aperture.agp_bot = (uint64_t)agp_bot << 24;
1528 pa_config->system_aperture.agp_top = (uint64_t)agp_top << 24;
1529
1530 pa_config->system_aperture.fb_base = adev->gmc.fb_start;
1531 pa_config->system_aperture.fb_offset = adev->vm_manager.vram_base_offset;
1532 pa_config->system_aperture.fb_top = adev->gmc.fb_end;
1533
1534 pa_config->gart_config.page_table_start_addr = page_table_start.quad_part << 12;
1535 pa_config->gart_config.page_table_end_addr = page_table_end.quad_part << 12;
1536 pa_config->gart_config.page_table_base_addr = page_table_base.quad_part;
1537
1538 pa_config->is_hvm_enabled = adev->mode_info.gpu_vm_support;
1539
1540}
1541
1542static void force_connector_state(
1543 struct amdgpu_dm_connector *aconnector,
1544 enum drm_connector_force force_state)
1545{
1546 struct drm_connector *connector = &aconnector->base;
1547
1548 mutex_lock(&connector->dev->mode_config.mutex);
1549 aconnector->base.force = force_state;
1550 mutex_unlock(&connector->dev->mode_config.mutex);
1551
1552 mutex_lock(&aconnector->hpd_lock);
1553 drm_kms_helper_connector_hotplug_event(connector);
1554 mutex_unlock(&aconnector->hpd_lock);
1555}
1556
1557static void dm_handle_hpd_rx_offload_work(struct work_struct *work)
1558{
1559 struct hpd_rx_irq_offload_work *offload_work;
1560 struct amdgpu_dm_connector *aconnector;
1561 struct dc_link *dc_link;
1562 struct amdgpu_device *adev;
1563 enum dc_connection_type new_connection_type = dc_connection_none;
1564 unsigned long flags;
1565 union test_response test_response;
1566
1567 memset(&test_response, 0, sizeof(test_response));
1568
1569 offload_work = container_of(work, struct hpd_rx_irq_offload_work, work);
1570 aconnector = offload_work->offload_wq->aconnector;
1571 adev = offload_work->adev;
1572
1573 if (!aconnector) {
1574 drm_err(adev_to_drm(adev), "Can't retrieve aconnector in hpd_rx_irq_offload_work");
1575 goto skip;
1576 }
1577
1578 dc_link = aconnector->dc_link;
1579
1580 mutex_lock(&aconnector->hpd_lock);
1581 if (!dc_link_detect_connection_type(dc_link, &new_connection_type))
1582 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
1583 mutex_unlock(&aconnector->hpd_lock);
1584
1585 if (new_connection_type == dc_connection_none)
1586 goto skip;
1587
1588 if (amdgpu_in_reset(adev))
1589 goto skip;
1590
1591 if (offload_work->data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY ||
1592 offload_work->data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) {
1593 dm_handle_mst_sideband_msg_ready_event(&aconnector->mst_mgr, DOWN_OR_UP_MSG_RDY_EVENT);
1594 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags);
1595 offload_work->offload_wq->is_handling_mst_msg_rdy_event = false;
1596 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags);
1597 goto skip;
1598 }
1599
1600 mutex_lock(&adev->dm.dc_lock);
1601 if (offload_work->data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
1602 dc_link_dp_handle_automated_test(dc_link);
1603
1604 if (aconnector->timing_changed) {
1605 /* force connector disconnect and reconnect */
1606 force_connector_state(aconnector, DRM_FORCE_OFF);
1607 msleep(100);
1608 force_connector_state(aconnector, DRM_FORCE_UNSPECIFIED);
1609 }
1610
1611 test_response.bits.ACK = 1;
1612
1613 core_link_write_dpcd(
1614 dc_link,
1615 DP_TEST_RESPONSE,
1616 &test_response.raw,
1617 sizeof(test_response));
1618 } else if ((dc_link->connector_signal != SIGNAL_TYPE_EDP) &&
1619 dc_link_check_link_loss_status(dc_link, &offload_work->data) &&
1620 dc_link_dp_allow_hpd_rx_irq(dc_link)) {
1621 /* offload_work->data is from handle_hpd_rx_irq->
1622 * schedule_hpd_rx_offload_work.this is defer handle
1623 * for hpd short pulse. upon here, link status may be
1624 * changed, need get latest link status from dpcd
1625 * registers. if link status is good, skip run link
1626 * training again.
1627 */
1628 union hpd_irq_data irq_data;
1629
1630 memset(&irq_data, 0, sizeof(irq_data));
1631
1632 /* before dc_link_dp_handle_link_loss, allow new link lost handle
1633 * request be added to work queue if link lost at end of dc_link_
1634 * dp_handle_link_loss
1635 */
1636 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags);
1637 offload_work->offload_wq->is_handling_link_loss = false;
1638 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags);
1639
1640 if ((dc_link_dp_read_hpd_rx_irq_data(dc_link, &irq_data) == DC_OK) &&
1641 dc_link_check_link_loss_status(dc_link, &irq_data))
1642 dc_link_dp_handle_link_loss(dc_link);
1643 }
1644 mutex_unlock(&adev->dm.dc_lock);
1645
1646skip:
1647 kfree(offload_work);
1648
1649}
1650
1651static struct hpd_rx_irq_offload_work_queue *hpd_rx_irq_create_workqueue(struct amdgpu_device *adev)
1652{
1653 struct dc *dc = adev->dm.dc;
1654 int max_caps = dc->caps.max_links;
1655 int i = 0;
1656 struct hpd_rx_irq_offload_work_queue *hpd_rx_offload_wq = NULL;
1657
1658 hpd_rx_offload_wq = kzalloc_objs(*hpd_rx_offload_wq, max_caps);
1659
1660 if (!hpd_rx_offload_wq)
1661 return NULL;
1662
1663
1664 for (i = 0; i < max_caps; i++) {
1665 hpd_rx_offload_wq[i].wq =
1666 create_singlethread_workqueue("amdgpu_dm_hpd_rx_offload_wq");
1667
1668 if (hpd_rx_offload_wq[i].wq == NULL) {
1669 drm_err(adev_to_drm(adev), "create amdgpu_dm_hpd_rx_offload_wq fail!");
1670 goto out_err;
1671 }
1672
1673 spin_lock_init(&hpd_rx_offload_wq[i].offload_lock);
1674 }
1675
1676 return hpd_rx_offload_wq;
1677
1678out_err:
1679 for (i = 0; i < max_caps; i++) {
1680 if (hpd_rx_offload_wq[i].wq)
1681 destroy_workqueue(hpd_rx_offload_wq[i].wq);
1682 }
1683 kfree(hpd_rx_offload_wq);
1684 return NULL;
1685}
1686
1687struct amdgpu_stutter_quirk {
1688 u16 chip_vendor;
1689 u16 chip_device;
1690 u16 subsys_vendor;
1691 u16 subsys_device;
1692 u8 revision;
1693};
1694
1695static const struct amdgpu_stutter_quirk amdgpu_stutter_quirk_list[] = {
1696 /* https://bugzilla.kernel.org/show_bug.cgi?id=214417 */
1697 { 0x1002, 0x15dd, 0x1002, 0x15dd, 0xc8 },
1698 { 0, 0, 0, 0, 0 },
1699};
1700
1701static bool dm_should_disable_stutter(struct pci_dev *pdev)
1702{
1703 const struct amdgpu_stutter_quirk *p = amdgpu_stutter_quirk_list;
1704
1705 while (p && p->chip_device != 0) {
1706 if (pdev->vendor == p->chip_vendor &&
1707 pdev->device == p->chip_device &&
1708 pdev->subsystem_vendor == p->subsys_vendor &&
1709 pdev->subsystem_device == p->subsys_device &&
1710 pdev->revision == p->revision) {
1711 return true;
1712 }
1713 ++p;
1714 }
1715 return false;
1716}
1717
1718
1719void*
1720dm_allocate_gpu_mem(
1721 struct amdgpu_device *adev,
1722 enum dc_gpu_mem_alloc_type type,
1723 size_t size,
1724 long long *addr)
1725{
1726 struct dal_allocation *da;
1727 u32 domain = (type == DC_MEM_ALLOC_TYPE_GART) ?
1728 AMDGPU_GEM_DOMAIN_GTT : AMDGPU_GEM_DOMAIN_VRAM;
1729 int ret;
1730
1731 da = kzalloc_obj(struct dal_allocation);
1732 if (!da)
1733 return NULL;
1734
1735 ret = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE,
1736 domain, &da->bo,
1737 &da->gpu_addr, &da->cpu_ptr);
1738
1739 *addr = da->gpu_addr;
1740
1741 if (ret) {
1742 kfree(da);
1743 return NULL;
1744 }
1745
1746 /* add da to list in dm */
1747 list_add(&da->list, &adev->dm.da_list);
1748
1749 return da->cpu_ptr;
1750}
1751
1752void
1753dm_free_gpu_mem(
1754 struct amdgpu_device *adev,
1755 enum dc_gpu_mem_alloc_type type,
1756 void *pvMem)
1757{
1758 struct dal_allocation *da;
1759
1760 /* walk the da list in DM */
1761 list_for_each_entry(da, &adev->dm.da_list, list) {
1762 if (pvMem == da->cpu_ptr) {
1763 amdgpu_bo_free_kernel(&da->bo, &da->gpu_addr, &da->cpu_ptr);
1764 list_del(&da->list);
1765 kfree(da);
1766 break;
1767 }
1768 }
1769
1770}
1771
1772static enum dmub_status
1773dm_dmub_send_vbios_gpint_command(struct amdgpu_device *adev,
1774 enum dmub_gpint_command command_code,
1775 uint16_t param,
1776 uint32_t timeout_us)
1777{
1778 union dmub_gpint_data_register reg, test;
1779 uint32_t i;
1780
1781 /* Assume that VBIOS DMUB is ready to take commands */
1782
1783 reg.bits.status = 1;
1784 reg.bits.command_code = command_code;
1785 reg.bits.param = param;
1786
1787 cgs_write_register(adev->dm.cgs_device, 0x34c0 + 0x01f8, reg.all);
1788
1789 for (i = 0; i < timeout_us; ++i) {
1790 udelay(1);
1791
1792 /* Check if our GPINT got acked */
1793 reg.bits.status = 0;
1794 test = (union dmub_gpint_data_register)
1795 cgs_read_register(adev->dm.cgs_device, 0x34c0 + 0x01f8);
1796
1797 if (test.all == reg.all)
1798 return DMUB_STATUS_OK;
1799 }
1800
1801 return DMUB_STATUS_TIMEOUT;
1802}
1803
1804static void *dm_dmub_get_vbios_bounding_box(struct amdgpu_device *adev)
1805{
1806 void *bb;
1807 long long addr;
1808 unsigned int bb_size;
1809 int i = 0;
1810 uint16_t chunk;
1811 enum dmub_gpint_command send_addrs[] = {
1812 DMUB_GPINT__SET_BB_ADDR_WORD0,
1813 DMUB_GPINT__SET_BB_ADDR_WORD1,
1814 DMUB_GPINT__SET_BB_ADDR_WORD2,
1815 DMUB_GPINT__SET_BB_ADDR_WORD3,
1816 };
1817 enum dmub_status ret;
1818
1819 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1820 case IP_VERSION(4, 0, 1):
1821 bb_size = sizeof(struct dml2_soc_bb);
1822 break;
1823 case IP_VERSION(4, 2, 0):
1824 bb_size = sizeof(struct dml2_soc_bb);
1825 break;
1826 default:
1827 return NULL;
1828 }
1829
1830 bb = dm_allocate_gpu_mem(adev,
1831 DC_MEM_ALLOC_TYPE_GART,
1832 bb_size,
1833 &addr);
1834 if (!bb)
1835 return NULL;
1836
1837 for (i = 0; i < 4; i++) {
1838 /* Extract 16-bit chunk */
1839 chunk = ((uint64_t) addr >> (i * 16)) & 0xFFFF;
1840 /* Send the chunk */
1841 ret = dm_dmub_send_vbios_gpint_command(adev, send_addrs[i], chunk, 30000);
1842 if (ret != DMUB_STATUS_OK)
1843 goto free_bb;
1844 }
1845
1846 /* Now ask DMUB to copy the bb */
1847 ret = dm_dmub_send_vbios_gpint_command(adev, DMUB_GPINT__BB_COPY, 1, 200000);
1848 if (ret != DMUB_STATUS_OK)
1849 goto free_bb;
1850
1851 return bb;
1852
1853free_bb:
1854 dm_free_gpu_mem(adev, DC_MEM_ALLOC_TYPE_GART, (void *) bb);
1855 return NULL;
1856
1857}
1858
1859static enum dmub_ips_disable_type dm_get_default_ips_mode(
1860 struct amdgpu_device *adev)
1861{
1862 enum dmub_ips_disable_type ret = DMUB_IPS_ENABLE;
1863
1864 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1865 case IP_VERSION(3, 5, 0):
1866 case IP_VERSION(3, 6, 0):
1867 case IP_VERSION(3, 5, 1):
1868 ret = DMUB_IPS_RCG_IN_ACTIVE_IPS2_IN_OFF;
1869 break;
1870 case IP_VERSION(4, 2, 0):
1871 ret = DMUB_IPS_DISABLE_ALL;
1872 break;
1873 default:
1874 /* ASICs older than DCN35 do not have IPSs */
1875 if (amdgpu_ip_version(adev, DCE_HWIP, 0) < IP_VERSION(3, 5, 0))
1876 ret = DMUB_IPS_DISABLE_ALL;
1877 break;
1878 }
1879
1880 return ret;
1881}
1882
1883static int amdgpu_dm_init(struct amdgpu_device *adev)
1884{
1885 struct dc_init_data init_data;
1886 struct dc_callback_init init_params;
1887 int r;
1888
1889 adev->dm.ddev = adev_to_drm(adev);
1890 adev->dm.adev = adev;
1891
1892 /* Zero all the fields */
1893 memset(&init_data, 0, sizeof(init_data));
1894 memset(&init_params, 0, sizeof(init_params));
1895
1896 mutex_init(&adev->dm.dpia_aux_lock);
1897 mutex_init(&adev->dm.dc_lock);
1898 mutex_init(&adev->dm.audio_lock);
1899
1900 if (amdgpu_dm_irq_init(adev)) {
1901 drm_err(adev_to_drm(adev), "failed to initialize DM IRQ support.\n");
1902 goto error;
1903 }
1904
1905 init_data.asic_id.chip_family = adev->family;
1906
1907 init_data.asic_id.pci_revision_id = adev->pdev->revision;
1908 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
1909 init_data.asic_id.chip_id = adev->pdev->device;
1910
1911 init_data.asic_id.vram_width = adev->gmc.vram_width;
1912 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
1913 init_data.asic_id.atombios_base_address =
1914 adev->mode_info.atom_context->bios;
1915
1916 init_data.driver = adev;
1917
1918 /* cgs_device was created in dm_sw_init() */
1919 init_data.cgs_device = adev->dm.cgs_device;
1920
1921 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
1922
1923 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1924 case IP_VERSION(2, 1, 0):
1925 switch (adev->dm.dmcub_fw_version) {
1926 case 0: /* development */
1927 case 0x1: /* linux-firmware.git hash 6d9f399 */
1928 case 0x01000000: /* linux-firmware.git hash 9a0b0f4 */
1929 init_data.flags.disable_dmcu = false;
1930 break;
1931 default:
1932 init_data.flags.disable_dmcu = true;
1933 }
1934 break;
1935 case IP_VERSION(2, 0, 3):
1936 init_data.flags.disable_dmcu = true;
1937 break;
1938 default:
1939 break;
1940 }
1941
1942 /* APU support S/G display by default except:
1943 * ASICs before Carrizo,
1944 * RAVEN1 (Users reported stability issue)
1945 */
1946
1947 if (adev->asic_type < CHIP_CARRIZO) {
1948 init_data.flags.gpu_vm_support = false;
1949 } else if (adev->asic_type == CHIP_RAVEN) {
1950 if (adev->apu_flags & AMD_APU_IS_RAVEN)
1951 init_data.flags.gpu_vm_support = false;
1952 else
1953 init_data.flags.gpu_vm_support = (amdgpu_sg_display != 0);
1954 } else {
1955 if (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(2, 0, 3))
1956 init_data.flags.gpu_vm_support = (amdgpu_sg_display == 1);
1957 else
1958 init_data.flags.gpu_vm_support =
1959 (amdgpu_sg_display != 0) && (adev->flags & AMD_IS_APU);
1960 }
1961
1962 adev->mode_info.gpu_vm_support = init_data.flags.gpu_vm_support;
1963
1964 if (amdgpu_dc_feature_mask & DC_FBC_MASK)
1965 init_data.flags.fbc_support = true;
1966
1967 if (amdgpu_dc_feature_mask & DC_MULTI_MON_PP_MCLK_SWITCH_MASK)
1968 init_data.flags.multi_mon_pp_mclk_switch = true;
1969
1970 if (amdgpu_dc_feature_mask & DC_DISABLE_FRACTIONAL_PWM_MASK)
1971 init_data.flags.disable_fractional_pwm = true;
1972
1973 if (amdgpu_dc_feature_mask & DC_EDP_NO_POWER_SEQUENCING)
1974 init_data.flags.edp_no_power_sequencing = true;
1975
1976 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP1_4A)
1977 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP1_4A = true;
1978 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP2_0)
1979 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP2_0 = true;
1980
1981 init_data.flags.seamless_boot_edp_requested = false;
1982
1983 if (amdgpu_device_seamless_boot_supported(adev)) {
1984 init_data.flags.seamless_boot_edp_requested = true;
1985 init_data.flags.allow_seamless_boot_optimization = true;
1986 drm_dbg(adev->dm.ddev, "Seamless boot requested\n");
1987 }
1988
1989 init_data.flags.enable_mipi_converter_optimization = true;
1990
1991 init_data.dcn_reg_offsets = adev->reg_offset[DCE_HWIP][0];
1992 init_data.nbio_reg_offsets = adev->reg_offset[NBIO_HWIP][0];
1993 init_data.clk_reg_offsets = adev->reg_offset[CLK_HWIP][0];
1994
1995 if (amdgpu_dc_debug_mask & DC_DISABLE_IPS)
1996 init_data.flags.disable_ips = DMUB_IPS_DISABLE_ALL;
1997 else if (amdgpu_dc_debug_mask & DC_DISABLE_IPS_DYNAMIC)
1998 init_data.flags.disable_ips = DMUB_IPS_DISABLE_DYNAMIC;
1999 else if (amdgpu_dc_debug_mask & DC_DISABLE_IPS2_DYNAMIC)
2000 init_data.flags.disable_ips = DMUB_IPS_RCG_IN_ACTIVE_IPS2_IN_OFF;
2001 else if (amdgpu_dc_debug_mask & DC_FORCE_IPS_ENABLE)
2002 init_data.flags.disable_ips = DMUB_IPS_ENABLE;
2003 else
2004 init_data.flags.disable_ips = dm_get_default_ips_mode(adev);
2005
2006 init_data.flags.disable_ips_in_vpb = 0;
2007
2008 /* DCN35 and above supports dynamic DTBCLK switch */
2009 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 5, 0))
2010 init_data.flags.allow_0_dtb_clk = true;
2011
2012 /* Enable DWB for tested platforms only */
2013 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 0, 0))
2014 init_data.num_virtual_links = 1;
2015
2016 /* DCN42 and above dpia switch to unified link training path */
2017 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(4, 2, 0)) {
2018 init_data.flags.consolidated_dpia_dp_lt = true;
2019 init_data.flags.enable_dpia_pre_training = true;
2020 init_data.flags.unify_link_enc_assignment = true;
2021 init_data.flags.usb4_bw_alloc_support = true;
2022 }
2023 retrieve_dmi_info(&adev->dm);
2024 if (adev->dm.edp0_on_dp1_quirk)
2025 init_data.flags.support_edp0_on_dp1 = true;
2026
2027 if (adev->dm.bb_from_dmub)
2028 init_data.bb_from_dmub = adev->dm.bb_from_dmub;
2029 else
2030 init_data.bb_from_dmub = NULL;
2031
2032 /* Display Core create. */
2033 adev->dm.dc = dc_create(&init_data);
2034
2035 if (adev->dm.dc) {
2036 drm_info(adev_to_drm(adev), "Display Core v%s initialized on %s\n", DC_VER,
2037 dce_version_to_string(adev->dm.dc->ctx->dce_version));
2038 } else {
2039 drm_info(adev_to_drm(adev), "Display Core failed to initialize with v%s!\n", DC_VER);
2040 goto error;
2041 }
2042
2043 if (amdgpu_dc_debug_mask & DC_DISABLE_PIPE_SPLIT) {
2044 adev->dm.dc->debug.force_single_disp_pipe_split = false;
2045 adev->dm.dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
2046 }
2047
2048 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
2049 adev->dm.dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
2050 if (dm_should_disable_stutter(adev->pdev))
2051 adev->dm.dc->debug.disable_stutter = true;
2052
2053 if (amdgpu_dc_debug_mask & DC_DISABLE_STUTTER)
2054 adev->dm.dc->debug.disable_stutter = true;
2055
2056 if (amdgpu_dc_debug_mask & DC_DISABLE_DSC)
2057 adev->dm.dc->debug.disable_dsc = true;
2058
2059 if (amdgpu_dc_debug_mask & DC_DISABLE_CLOCK_GATING)
2060 adev->dm.dc->debug.disable_clock_gate = true;
2061
2062 if (amdgpu_dc_debug_mask & DC_FORCE_SUBVP_MCLK_SWITCH)
2063 adev->dm.dc->debug.force_subvp_mclk_switch = true;
2064
2065 if (amdgpu_dc_debug_mask & DC_DISABLE_SUBVP_FAMS) {
2066 adev->dm.dc->debug.force_disable_subvp = true;
2067 adev->dm.dc->debug.fams2_config.bits.enable = false;
2068 }
2069
2070 if (amdgpu_dc_debug_mask & DC_ENABLE_DML2) {
2071 adev->dm.dc->debug.using_dml2 = true;
2072 adev->dm.dc->debug.using_dml21 = true;
2073 }
2074
2075 if (amdgpu_dc_debug_mask & DC_HDCP_LC_FORCE_FW_ENABLE)
2076 adev->dm.dc->debug.hdcp_lc_force_fw_enable = true;
2077
2078 if (amdgpu_dc_debug_mask & DC_HDCP_LC_ENABLE_SW_FALLBACK)
2079 adev->dm.dc->debug.hdcp_lc_enable_sw_fallback = true;
2080
2081 if (amdgpu_dc_debug_mask & DC_SKIP_DETECTION_LT)
2082 adev->dm.dc->debug.skip_detection_link_training = true;
2083
2084 adev->dm.dc->debug.visual_confirm = amdgpu_dc_visual_confirm;
2085
2086 /* TODO: Remove after DP2 receiver gets proper support of Cable ID feature */
2087 adev->dm.dc->debug.ignore_cable_id = true;
2088
2089 if (adev->dm.dc->caps.dp_hdmi21_pcon_support)
2090 drm_info(adev_to_drm(adev), "DP-HDMI FRL PCON supported\n");
2091
2092 r = dm_dmub_hw_init(adev);
2093 if (r) {
2094 drm_err(adev_to_drm(adev), "DMUB interface failed to initialize: status=%d\n", r);
2095 goto error;
2096 }
2097
2098 dc_hardware_init(adev->dm.dc);
2099
2100 adev->dm.hpd_rx_offload_wq = hpd_rx_irq_create_workqueue(adev);
2101 if (!adev->dm.hpd_rx_offload_wq) {
2102 drm_err(adev_to_drm(adev), "failed to create hpd rx offload workqueue.\n");
2103 goto error;
2104 }
2105
2106 if ((adev->flags & AMD_IS_APU) && (adev->asic_type >= CHIP_CARRIZO)) {
2107 struct dc_phy_addr_space_config pa_config;
2108
2109 mmhub_read_system_context(adev, &pa_config);
2110
2111 // Call the DC init_memory func
2112 dc_setup_system_context(adev->dm.dc, &pa_config);
2113 }
2114
2115 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
2116 if (!adev->dm.freesync_module) {
2117 drm_err(adev_to_drm(adev),
2118 "failed to initialize freesync_module.\n");
2119 } else
2120 drm_dbg_driver(adev_to_drm(adev), "freesync_module init done %p.\n",
2121 adev->dm.freesync_module);
2122
2123 amdgpu_dm_init_color_mod();
2124
2125 if (adev->dm.dc->caps.max_links > 0) {
2126 adev->dm.vblank_control_workqueue =
2127 create_singlethread_workqueue("dm_vblank_control_workqueue");
2128 if (!adev->dm.vblank_control_workqueue)
2129 drm_err(adev_to_drm(adev), "failed to initialize vblank_workqueue.\n");
2130 }
2131
2132 if (adev->dm.dc->caps.ips_support &&
2133 adev->dm.dc->config.disable_ips != DMUB_IPS_DISABLE_ALL)
2134 adev->dm.idle_workqueue = idle_create_workqueue(adev);
2135
2136 if (adev->dm.dc->caps.max_links > 0 && adev->family >= AMDGPU_FAMILY_RV) {
2137 adev->dm.hdcp_workqueue = hdcp_create_workqueue(adev, &init_params.cp_psp, adev->dm.dc);
2138
2139 if (!adev->dm.hdcp_workqueue)
2140 drm_err(adev_to_drm(adev), "failed to initialize hdcp_workqueue.\n");
2141 else
2142 drm_dbg_driver(adev_to_drm(adev),
2143 "hdcp_workqueue init done %p.\n",
2144 adev->dm.hdcp_workqueue);
2145
2146 dc_init_callbacks(adev->dm.dc, &init_params);
2147 }
2148 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
2149 init_completion(&adev->dm.dmub_aux_transfer_done);
2150 adev->dm.dmub_notify = kzalloc_obj(struct dmub_notification);
2151 if (!adev->dm.dmub_notify) {
2152 drm_info(adev_to_drm(adev), "fail to allocate adev->dm.dmub_notify");
2153 goto error;
2154 }
2155
2156 adev->dm.delayed_hpd_wq = create_singlethread_workqueue("amdgpu_dm_hpd_wq");
2157 if (!adev->dm.delayed_hpd_wq) {
2158 drm_err(adev_to_drm(adev), "failed to create hpd offload workqueue.\n");
2159 goto error;
2160 }
2161
2162 amdgpu_dm_outbox_init(adev);
2163 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_AUX_REPLY,
2164 dmub_aux_setconfig_callback, false)) {
2165 drm_err(adev_to_drm(adev), "fail to register dmub aux callback");
2166 goto error;
2167 }
2168
2169 for (size_t i = 0; i < ARRAY_SIZE(adev->dm.fused_io); i++)
2170 init_completion(&adev->dm.fused_io[i].replied);
2171
2172 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_FUSED_IO,
2173 dmub_aux_fused_io_callback, false)) {
2174 drm_err(adev_to_drm(adev), "fail to register dmub fused io callback");
2175 goto error;
2176 }
2177 /* Enable outbox notification only after IRQ handlers are registered and DMUB is alive.
2178 * It is expected that DMUB will resend any pending notifications at this point. Note
2179 * that hpd and hpd_irq handler registration are deferred to register_hpd_handlers() to
2180 * align legacy interface initialization sequence. Connection status will be proactivly
2181 * detected once in the amdgpu_dm_initialize_drm_device.
2182 */
2183 dc_enable_dmub_outbox(adev->dm.dc);
2184
2185 /* DPIA trace goes to dmesg logs only if outbox is enabled */
2186 if (amdgpu_dc_debug_mask & DC_ENABLE_DPIA_TRACE)
2187 dc_dmub_srv_enable_dpia_trace(adev->dm.dc);
2188 }
2189
2190 if (amdgpu_dm_initialize_drm_device(adev)) {
2191 drm_err(adev_to_drm(adev),
2192 "failed to initialize sw for display support.\n");
2193 goto error;
2194 }
2195
2196 /* create fake encoders for MST */
2197 dm_dp_create_fake_mst_encoders(adev);
2198
2199 /* TODO: Add_display_info? */
2200
2201 /* TODO use dynamic cursor width */
2202 adev_to_drm(adev)->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
2203 adev_to_drm(adev)->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
2204
2205 if (drm_vblank_init(adev_to_drm(adev), adev->dm.display_indexes_num)) {
2206 drm_err(adev_to_drm(adev),
2207 "failed to initialize vblank for display support.\n");
2208 goto error;
2209 }
2210
2211#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
2212 amdgpu_dm_crtc_secure_display_create_contexts(adev);
2213 if (!adev->dm.secure_display_ctx.crtc_ctx)
2214 drm_err(adev_to_drm(adev), "failed to initialize secure display contexts.\n");
2215
2216 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(4, 0, 1))
2217 adev->dm.secure_display_ctx.support_mul_roi = true;
2218
2219#endif
2220
2221 drm_dbg_driver(adev_to_drm(adev), "KMS initialized.\n");
2222
2223 return 0;
2224error:
2225 amdgpu_dm_fini(adev);
2226
2227 return -EINVAL;
2228}
2229
2230static int amdgpu_dm_early_fini(struct amdgpu_ip_block *ip_block)
2231{
2232 struct amdgpu_device *adev = ip_block->adev;
2233
2234 amdgpu_dm_audio_fini(adev);
2235
2236 return 0;
2237}
2238
2239static void amdgpu_dm_fini(struct amdgpu_device *adev)
2240{
2241 int i;
2242
2243 if (adev->dm.vblank_control_workqueue) {
2244 destroy_workqueue(adev->dm.vblank_control_workqueue);
2245 adev->dm.vblank_control_workqueue = NULL;
2246 }
2247
2248 if (adev->dm.idle_workqueue) {
2249 if (adev->dm.idle_workqueue->running) {
2250 adev->dm.idle_workqueue->enable = false;
2251 flush_work(&adev->dm.idle_workqueue->work);
2252 }
2253
2254 kfree(adev->dm.idle_workqueue);
2255 adev->dm.idle_workqueue = NULL;
2256 }
2257
2258 amdgpu_dm_destroy_drm_device(&adev->dm);
2259
2260#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
2261 if (adev->dm.secure_display_ctx.crtc_ctx) {
2262 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2263 if (adev->dm.secure_display_ctx.crtc_ctx[i].crtc) {
2264 flush_work(&adev->dm.secure_display_ctx.crtc_ctx[i].notify_ta_work);
2265 flush_work(&adev->dm.secure_display_ctx.crtc_ctx[i].forward_roi_work);
2266 }
2267 }
2268 kfree(adev->dm.secure_display_ctx.crtc_ctx);
2269 adev->dm.secure_display_ctx.crtc_ctx = NULL;
2270 }
2271#endif
2272 if (adev->dm.hdcp_workqueue) {
2273 hdcp_destroy(&adev->dev->kobj, adev->dm.hdcp_workqueue);
2274 adev->dm.hdcp_workqueue = NULL;
2275 }
2276
2277 if (adev->dm.dc) {
2278 dc_deinit_callbacks(adev->dm.dc);
2279 dc_dmub_srv_destroy(&adev->dm.dc->ctx->dmub_srv);
2280 if (dc_enable_dmub_notifications(adev->dm.dc)) {
2281 kfree(adev->dm.dmub_notify);
2282 adev->dm.dmub_notify = NULL;
2283 destroy_workqueue(adev->dm.delayed_hpd_wq);
2284 adev->dm.delayed_hpd_wq = NULL;
2285 }
2286 }
2287
2288 if (adev->dm.dmub_bo)
2289 amdgpu_bo_free_kernel(&adev->dm.dmub_bo,
2290 &adev->dm.dmub_bo_gpu_addr,
2291 &adev->dm.dmub_bo_cpu_addr);
2292
2293 if (adev->dm.boot_time_crc_info.bo_ptr)
2294 amdgpu_bo_free_kernel(&adev->dm.boot_time_crc_info.bo_ptr,
2295 &adev->dm.boot_time_crc_info.gpu_addr,
2296 &adev->dm.boot_time_crc_info.cpu_addr);
2297
2298 if (adev->dm.hpd_rx_offload_wq && adev->dm.dc) {
2299 for (i = 0; i < adev->dm.dc->caps.max_links; i++) {
2300 if (adev->dm.hpd_rx_offload_wq[i].wq) {
2301 destroy_workqueue(adev->dm.hpd_rx_offload_wq[i].wq);
2302 adev->dm.hpd_rx_offload_wq[i].wq = NULL;
2303 }
2304 }
2305
2306 kfree(adev->dm.hpd_rx_offload_wq);
2307 adev->dm.hpd_rx_offload_wq = NULL;
2308 }
2309
2310 /* DC Destroy TODO: Replace destroy DAL */
2311 if (adev->dm.dc)
2312 dc_destroy(&adev->dm.dc);
2313 /*
2314 * TODO: pageflip, vlank interrupt
2315 *
2316 * amdgpu_dm_irq_fini(adev);
2317 */
2318
2319 if (adev->dm.cgs_device) {
2320 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
2321 adev->dm.cgs_device = NULL;
2322 }
2323 if (adev->dm.freesync_module) {
2324 mod_freesync_destroy(adev->dm.freesync_module);
2325 adev->dm.freesync_module = NULL;
2326 }
2327
2328 mutex_destroy(&adev->dm.audio_lock);
2329 mutex_destroy(&adev->dm.dc_lock);
2330 mutex_destroy(&adev->dm.dpia_aux_lock);
2331}
2332
2333static int load_dmcu_fw(struct amdgpu_device *adev)
2334{
2335 const char *fw_name_dmcu = NULL;
2336 int r;
2337 const struct dmcu_firmware_header_v1_0 *hdr;
2338
2339 switch (adev->asic_type) {
2340#if defined(CONFIG_DRM_AMD_DC_SI)
2341 case CHIP_TAHITI:
2342 case CHIP_PITCAIRN:
2343 case CHIP_VERDE:
2344 case CHIP_OLAND:
2345#endif
2346 case CHIP_BONAIRE:
2347 case CHIP_HAWAII:
2348 case CHIP_KAVERI:
2349 case CHIP_KABINI:
2350 case CHIP_MULLINS:
2351 case CHIP_TONGA:
2352 case CHIP_FIJI:
2353 case CHIP_CARRIZO:
2354 case CHIP_STONEY:
2355 case CHIP_POLARIS11:
2356 case CHIP_POLARIS10:
2357 case CHIP_POLARIS12:
2358 case CHIP_VEGAM:
2359 case CHIP_VEGA10:
2360 case CHIP_VEGA12:
2361 case CHIP_VEGA20:
2362 return 0;
2363 case CHIP_NAVI12:
2364 fw_name_dmcu = FIRMWARE_NAVI12_DMCU;
2365 break;
2366 case CHIP_RAVEN:
2367 if (ASICREV_IS_PICASSO(adev->external_rev_id))
2368 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
2369 else if (ASICREV_IS_RAVEN2(adev->external_rev_id))
2370 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
2371 else
2372 return 0;
2373 break;
2374 default:
2375 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
2376 case IP_VERSION(2, 0, 2):
2377 case IP_VERSION(2, 0, 3):
2378 case IP_VERSION(2, 0, 0):
2379 case IP_VERSION(2, 1, 0):
2380 case IP_VERSION(3, 0, 0):
2381 case IP_VERSION(3, 0, 2):
2382 case IP_VERSION(3, 0, 3):
2383 case IP_VERSION(3, 0, 1):
2384 case IP_VERSION(3, 1, 2):
2385 case IP_VERSION(3, 1, 3):
2386 case IP_VERSION(3, 1, 4):
2387 case IP_VERSION(3, 1, 5):
2388 case IP_VERSION(3, 1, 6):
2389 case IP_VERSION(3, 2, 0):
2390 case IP_VERSION(3, 2, 1):
2391 case IP_VERSION(3, 5, 0):
2392 case IP_VERSION(3, 5, 1):
2393 case IP_VERSION(3, 6, 0):
2394 case IP_VERSION(4, 0, 1):
2395 case IP_VERSION(4, 2, 0):
2396 return 0;
2397 default:
2398 break;
2399 }
2400 drm_err(adev_to_drm(adev), "Unsupported ASIC type: 0x%X\n", adev->asic_type);
2401 return -EINVAL;
2402 }
2403
2404 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
2405 drm_dbg_kms(adev_to_drm(adev), "dm: DMCU firmware not supported on direct or SMU loading\n");
2406 return 0;
2407 }
2408
2409 r = amdgpu_ucode_request(adev, &adev->dm.fw_dmcu, AMDGPU_UCODE_REQUIRED,
2410 "%s", fw_name_dmcu);
2411 if (r == -ENODEV) {
2412 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */
2413 drm_dbg_kms(adev_to_drm(adev), "dm: DMCU firmware not found\n");
2414 adev->dm.fw_dmcu = NULL;
2415 return 0;
2416 }
2417 if (r) {
2418 drm_err(adev_to_drm(adev), "amdgpu_dm: Can't validate firmware \"%s\"\n",
2419 fw_name_dmcu);
2420 amdgpu_ucode_release(&adev->dm.fw_dmcu);
2421 return r;
2422 }
2423
2424 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data;
2425 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM;
2426 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu;
2427 adev->firmware.fw_size +=
2428 ALIGN(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
2429
2430 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV;
2431 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu;
2432 adev->firmware.fw_size +=
2433 ALIGN(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
2434
2435 adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version);
2436
2437 drm_dbg_kms(adev_to_drm(adev), "PSP loading DMCU firmware\n");
2438
2439 return 0;
2440}
2441
2442static uint32_t amdgpu_dm_dmub_reg_read(void *ctx, uint32_t address)
2443{
2444 struct amdgpu_device *adev = ctx;
2445
2446 return dm_read_reg(adev->dm.dc->ctx, address);
2447}
2448
2449static void amdgpu_dm_dmub_reg_write(void *ctx, uint32_t address,
2450 uint32_t value)
2451{
2452 struct amdgpu_device *adev = ctx;
2453
2454 return dm_write_reg(adev->dm.dc->ctx, address, value);
2455}
2456
2457static int dm_dmub_sw_init(struct amdgpu_device *adev)
2458{
2459 struct dmub_srv_create_params create_params;
2460 struct dmub_srv_fw_meta_info_params fw_meta_info_params;
2461 struct dmub_srv_region_params region_params;
2462 struct dmub_srv_region_info region_info;
2463 struct dmub_srv_memory_params memory_params;
2464 struct dmub_fw_meta_info fw_info;
2465 struct dmub_srv_fb_info *fb_info;
2466 struct dmub_srv *dmub_srv;
2467 const struct dmcub_firmware_header_v1_0 *hdr;
2468 enum dmub_asic dmub_asic;
2469 enum dmub_status status;
2470 static enum dmub_window_memory_type window_memory_type[DMUB_WINDOW_TOTAL] = {
2471 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_0_INST_CONST
2472 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_1_STACK
2473 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_2_BSS_DATA
2474 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_3_VBIOS
2475 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_4_MAILBOX
2476 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_5_TRACEBUFF
2477 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_6_FW_STATE
2478 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_7_SCRATCH_MEM
2479 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_IB_MEM
2480 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_SHARED_STATE
2481 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_LSDMA_BUFFER
2482 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_CURSOR_OFFLOAD
2483 };
2484 int r;
2485
2486 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
2487 case IP_VERSION(2, 1, 0):
2488 dmub_asic = DMUB_ASIC_DCN21;
2489 break;
2490 case IP_VERSION(3, 0, 0):
2491 dmub_asic = DMUB_ASIC_DCN30;
2492 break;
2493 case IP_VERSION(3, 0, 1):
2494 dmub_asic = DMUB_ASIC_DCN301;
2495 break;
2496 case IP_VERSION(3, 0, 2):
2497 dmub_asic = DMUB_ASIC_DCN302;
2498 break;
2499 case IP_VERSION(3, 0, 3):
2500 dmub_asic = DMUB_ASIC_DCN303;
2501 break;
2502 case IP_VERSION(3, 1, 2):
2503 case IP_VERSION(3, 1, 3):
2504 dmub_asic = (adev->external_rev_id == YELLOW_CARP_B0) ? DMUB_ASIC_DCN31B : DMUB_ASIC_DCN31;
2505 break;
2506 case IP_VERSION(3, 1, 4):
2507 dmub_asic = DMUB_ASIC_DCN314;
2508 break;
2509 case IP_VERSION(3, 1, 5):
2510 dmub_asic = DMUB_ASIC_DCN315;
2511 break;
2512 case IP_VERSION(3, 1, 6):
2513 dmub_asic = DMUB_ASIC_DCN316;
2514 break;
2515 case IP_VERSION(3, 2, 0):
2516 dmub_asic = DMUB_ASIC_DCN32;
2517 break;
2518 case IP_VERSION(3, 2, 1):
2519 dmub_asic = DMUB_ASIC_DCN321;
2520 break;
2521 case IP_VERSION(3, 5, 0):
2522 case IP_VERSION(3, 5, 1):
2523 dmub_asic = DMUB_ASIC_DCN35;
2524 break;
2525 case IP_VERSION(3, 6, 0):
2526 dmub_asic = DMUB_ASIC_DCN36;
2527 break;
2528 case IP_VERSION(4, 0, 1):
2529 dmub_asic = DMUB_ASIC_DCN401;
2530 break;
2531 case IP_VERSION(4, 2, 0):
2532 dmub_asic = DMUB_ASIC_DCN42;
2533 break;
2534 default:
2535 /* ASIC doesn't support DMUB. */
2536 return 0;
2537 }
2538
2539 hdr = (const struct dmcub_firmware_header_v1_0 *)adev->dm.dmub_fw->data;
2540 adev->dm.dmcub_fw_version = le32_to_cpu(hdr->header.ucode_version);
2541
2542 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
2543 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].ucode_id =
2544 AMDGPU_UCODE_ID_DMCUB;
2545 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].fw =
2546 adev->dm.dmub_fw;
2547 adev->firmware.fw_size +=
2548 ALIGN(le32_to_cpu(hdr->inst_const_bytes), PAGE_SIZE);
2549
2550 drm_info(adev_to_drm(adev), "Loading DMUB firmware via PSP: version=0x%08X\n",
2551 adev->dm.dmcub_fw_version);
2552 }
2553
2554
2555 adev->dm.dmub_srv = kzalloc_obj(*adev->dm.dmub_srv);
2556 dmub_srv = adev->dm.dmub_srv;
2557
2558 if (!dmub_srv) {
2559 drm_err(adev_to_drm(adev), "Failed to allocate DMUB service!\n");
2560 return -ENOMEM;
2561 }
2562
2563 memset(&create_params, 0, sizeof(create_params));
2564 create_params.user_ctx = adev;
2565 create_params.funcs.reg_read = amdgpu_dm_dmub_reg_read;
2566 create_params.funcs.reg_write = amdgpu_dm_dmub_reg_write;
2567 create_params.asic = dmub_asic;
2568
2569 /* Create the DMUB service. */
2570 status = dmub_srv_create(dmub_srv, &create_params);
2571 if (status != DMUB_STATUS_OK) {
2572 drm_err(adev_to_drm(adev), "Error creating DMUB service: %d\n", status);
2573 return -EINVAL;
2574 }
2575
2576 /* Extract the FW meta info. */
2577 memset(&fw_meta_info_params, 0, sizeof(fw_meta_info_params));
2578
2579 fw_meta_info_params.inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
2580 PSP_HEADER_BYTES_256;
2581 fw_meta_info_params.bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
2582 fw_meta_info_params.fw_inst_const = adev->dm.dmub_fw->data +
2583 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
2584 PSP_HEADER_BYTES_256;
2585 fw_meta_info_params.fw_bss_data = fw_meta_info_params.bss_data_size ? adev->dm.dmub_fw->data +
2586 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
2587 le32_to_cpu(hdr->inst_const_bytes) : NULL;
2588 fw_meta_info_params.custom_psp_footer_size = 0;
2589
2590 status = dmub_srv_get_fw_meta_info_from_raw_fw(&fw_meta_info_params, &fw_info);
2591 if (status != DMUB_STATUS_OK) {
2592 /* Skip returning early, just log the error. */
2593 drm_err(adev_to_drm(adev), "Error getting DMUB FW meta info: %d\n", status);
2594 // return -EINVAL;
2595 }
2596
2597 /* Calculate the size of all the regions for the DMUB service. */
2598 memset(®ion_params, 0, sizeof(region_params));
2599
2600 region_params.inst_const_size = fw_meta_info_params.inst_const_size;
2601 region_params.bss_data_size = fw_meta_info_params.bss_data_size;
2602 region_params.vbios_size = adev->bios_size;
2603 region_params.fw_bss_data = fw_meta_info_params.fw_bss_data;
2604 region_params.fw_inst_const = fw_meta_info_params.fw_inst_const;
2605 region_params.window_memory_type = window_memory_type;
2606 region_params.fw_info = (status == DMUB_STATUS_OK) ? &fw_info : NULL;
2607
2608 status = dmub_srv_calc_region_info(dmub_srv, ®ion_params,
2609 ®ion_info);
2610
2611 if (status != DMUB_STATUS_OK) {
2612 drm_err(adev_to_drm(adev), "Error calculating DMUB region info: %d\n", status);
2613 return -EINVAL;
2614 }
2615
2616 /*
2617 * Allocate a framebuffer based on the total size of all the regions.
2618 * TODO: Move this into GART.
2619 */
2620 r = amdgpu_bo_create_kernel(adev, region_info.fb_size, PAGE_SIZE,
2621 AMDGPU_GEM_DOMAIN_VRAM |
2622 AMDGPU_GEM_DOMAIN_GTT,
2623 &adev->dm.dmub_bo,
2624 &adev->dm.dmub_bo_gpu_addr,
2625 &adev->dm.dmub_bo_cpu_addr);
2626 if (r)
2627 return r;
2628
2629 /* Rebase the regions on the framebuffer address. */
2630 memset(&memory_params, 0, sizeof(memory_params));
2631 memory_params.cpu_fb_addr = adev->dm.dmub_bo_cpu_addr;
2632 memory_params.gpu_fb_addr = adev->dm.dmub_bo_gpu_addr;
2633 memory_params.region_info = ®ion_info;
2634 memory_params.window_memory_type = window_memory_type;
2635
2636 adev->dm.dmub_fb_info = kzalloc_obj(*adev->dm.dmub_fb_info);
2637 fb_info = adev->dm.dmub_fb_info;
2638
2639 if (!fb_info) {
2640 drm_err(adev_to_drm(adev),
2641 "Failed to allocate framebuffer info for DMUB service!\n");
2642 return -ENOMEM;
2643 }
2644
2645 status = dmub_srv_calc_mem_info(dmub_srv, &memory_params, fb_info);
2646 if (status != DMUB_STATUS_OK) {
2647 drm_err(adev_to_drm(adev), "Error calculating DMUB FB info: %d\n", status);
2648 return -EINVAL;
2649 }
2650
2651 adev->dm.bb_from_dmub = dm_dmub_get_vbios_bounding_box(adev);
2652 adev->dm.fw_inst_size = fw_meta_info_params.inst_const_size;
2653
2654 return 0;
2655}
2656
2657static int dm_sw_init(struct amdgpu_ip_block *ip_block)
2658{
2659 struct amdgpu_device *adev = ip_block->adev;
2660 int r;
2661
2662 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
2663
2664 if (!adev->dm.cgs_device) {
2665 drm_err(adev_to_drm(adev), "failed to create cgs device.\n");
2666 return -EINVAL;
2667 }
2668
2669 /* Moved from dm init since we need to use allocations for storing bounding box data */
2670 INIT_LIST_HEAD(&adev->dm.da_list);
2671
2672 r = dm_dmub_sw_init(adev);
2673 if (r)
2674 return r;
2675
2676 return load_dmcu_fw(adev);
2677}
2678
2679static int dm_sw_fini(struct amdgpu_ip_block *ip_block)
2680{
2681 struct amdgpu_device *adev = ip_block->adev;
2682 struct dal_allocation *da;
2683
2684 list_for_each_entry(da, &adev->dm.da_list, list) {
2685 if (adev->dm.bb_from_dmub == (void *) da->cpu_ptr) {
2686 amdgpu_bo_free_kernel(&da->bo, &da->gpu_addr, &da->cpu_ptr);
2687 list_del(&da->list);
2688 kfree(da);
2689 adev->dm.bb_from_dmub = NULL;
2690 break;
2691 }
2692 }
2693
2694
2695 kfree(adev->dm.dmub_fb_info);
2696 adev->dm.dmub_fb_info = NULL;
2697
2698 if (adev->dm.dmub_srv) {
2699 dmub_srv_destroy(adev->dm.dmub_srv);
2700 kfree(adev->dm.dmub_srv);
2701 adev->dm.dmub_srv = NULL;
2702 }
2703
2704 amdgpu_ucode_release(&adev->dm.dmub_fw);
2705 amdgpu_ucode_release(&adev->dm.fw_dmcu);
2706
2707 return 0;
2708}
2709
2710static int detect_mst_link_for_all_connectors(struct drm_device *dev)
2711{
2712 struct amdgpu_dm_connector *aconnector;
2713 struct drm_connector *connector;
2714 struct drm_connector_list_iter iter;
2715 int ret = 0;
2716
2717 drm_connector_list_iter_begin(dev, &iter);
2718 drm_for_each_connector_iter(connector, &iter) {
2719
2720 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
2721 continue;
2722
2723 aconnector = to_amdgpu_dm_connector(connector);
2724 if (aconnector->dc_link->type == dc_connection_mst_branch &&
2725 aconnector->mst_mgr.aux) {
2726 drm_dbg_kms(dev, "DM_MST: starting TM on aconnector: %p [id: %d]\n",
2727 aconnector,
2728 aconnector->base.base.id);
2729
2730 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
2731 if (ret < 0) {
2732 drm_err(dev, "DM_MST: Failed to start MST\n");
2733 aconnector->dc_link->type =
2734 dc_connection_single;
2735 ret = dm_helpers_dp_mst_stop_top_mgr(aconnector->dc_link->ctx,
2736 aconnector->dc_link);
2737 break;
2738 }
2739 }
2740 }
2741 drm_connector_list_iter_end(&iter);
2742
2743 return ret;
2744}
2745
2746static void amdgpu_dm_boot_time_crc_init(struct amdgpu_device *adev)
2747{
2748 struct dm_boot_time_crc_info *bootcrc_info = NULL;
2749 struct dmub_srv *dmub = NULL;
2750 union dmub_fw_boot_options option = {0};
2751 int ret = 0;
2752 const uint32_t fb_size = 3 * 1024 * 1024; /* 3MB for DCC pattern */
2753
2754 if (!adev || !adev->dm.dc || !adev->dm.dc->ctx ||
2755 !adev->dm.dc->ctx->dmub_srv) {
2756 return;
2757 }
2758
2759 dmub = adev->dm.dc->ctx->dmub_srv->dmub;
2760 bootcrc_info = &adev->dm.boot_time_crc_info;
2761
2762 if (!dmub || !dmub->hw_funcs.get_fw_boot_option) {
2763 drm_dbg(adev_to_drm(adev), "failed to init boot time crc buffer\n");
2764 return;
2765 }
2766
2767 option = dmub->hw_funcs.get_fw_boot_option(dmub);
2768
2769 /* Return if boot time CRC is not enabled */
2770 if (option.bits.bootcrc_en_at_S0i3 == 0)
2771 return;
2772
2773 /* Create a buffer for boot time CRC */
2774 ret = amdgpu_bo_create_kernel(adev, fb_size, PAGE_SIZE,
2775 AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT,
2776 &bootcrc_info->bo_ptr,
2777 &bootcrc_info->gpu_addr,
2778 &bootcrc_info->cpu_addr);
2779
2780 if (ret) {
2781 drm_dbg(adev_to_drm(adev), "failed to create boot time crc buffer\n");
2782 } else {
2783 bootcrc_info->size = fb_size;
2784
2785 drm_dbg(adev_to_drm(adev), "boot time crc buffer created addr 0x%llx, size %u\n",
2786 bootcrc_info->gpu_addr, bootcrc_info->size);
2787
2788 /* Send the buffer info to DMUB */
2789 dc_dmub_srv_boot_time_crc_init(adev->dm.dc,
2790 bootcrc_info->gpu_addr, bootcrc_info->size);
2791 }
2792}
2793
2794static int dm_late_init(struct amdgpu_ip_block *ip_block)
2795{
2796 struct amdgpu_device *adev = ip_block->adev;
2797
2798 struct dmcu_iram_parameters params;
2799 unsigned int linear_lut[16];
2800 int i;
2801 struct dmcu *dmcu = NULL;
2802
2803 dmcu = adev->dm.dc->res_pool->dmcu;
2804
2805 /* Init the boot time CRC (skip in resume) */
2806 if ((adev->in_suspend == 0) &&
2807 (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(3, 6, 0)))
2808 amdgpu_dm_boot_time_crc_init(adev);
2809
2810 for (i = 0; i < 16; i++)
2811 linear_lut[i] = 0xFFFF * i / 15;
2812
2813 params.set = 0;
2814 params.backlight_ramping_override = false;
2815 params.backlight_ramping_start = 0xCCCC;
2816 params.backlight_ramping_reduction = 0xCCCCCCCC;
2817 params.backlight_lut_array_size = 16;
2818 params.backlight_lut_array = linear_lut;
2819
2820 /* Min backlight level after ABM reduction, Don't allow below 1%
2821 * 0xFFFF x 0.01 = 0x28F
2822 */
2823 params.min_abm_backlight = 0x28F;
2824 /* In the case where abm is implemented on dmcub,
2825 * dmcu object will be null.
2826 * ABM 2.4 and up are implemented on dmcub.
2827 */
2828 if (dmcu) {
2829 if (!dmcu_load_iram(dmcu, params))
2830 return -EINVAL;
2831 } else if (adev->dm.dc->ctx->dmub_srv) {
2832 struct dc_link *edp_links[MAX_NUM_EDP];
2833 int edp_num;
2834
2835 dc_get_edp_links(adev->dm.dc, edp_links, &edp_num);
2836 for (i = 0; i < edp_num; i++) {
2837 if (!dmub_init_abm_config(adev->dm.dc->res_pool, params, i))
2838 return -EINVAL;
2839 }
2840 }
2841
2842 return detect_mst_link_for_all_connectors(adev_to_drm(adev));
2843}
2844
2845static void resume_mst_branch_status(struct drm_dp_mst_topology_mgr *mgr)
2846{
2847 u8 buf[UUID_SIZE];
2848 guid_t guid;
2849 int ret;
2850
2851 mutex_lock(&mgr->lock);
2852 if (!mgr->mst_primary)
2853 goto out_fail;
2854
2855 if (drm_dp_read_dpcd_caps(mgr->aux, mgr->dpcd) < 0) {
2856 drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n");
2857 goto out_fail;
2858 }
2859
2860 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2861 DP_MST_EN |
2862 DP_UP_REQ_EN |
2863 DP_UPSTREAM_IS_SRC);
2864 if (ret < 0) {
2865 drm_dbg_kms(mgr->dev, "mst write failed - undocked during suspend?\n");
2866 goto out_fail;
2867 }
2868
2869 /* Some hubs forget their guids after they resume */
2870 ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, buf, sizeof(buf));
2871 if (ret != sizeof(buf)) {
2872 drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n");
2873 goto out_fail;
2874 }
2875
2876 import_guid(&guid, buf);
2877
2878 if (guid_is_null(&guid)) {
2879 guid_gen(&guid);
2880 export_guid(buf, &guid);
2881
2882 ret = drm_dp_dpcd_write(mgr->aux, DP_GUID, buf, sizeof(buf));
2883
2884 if (ret != sizeof(buf)) {
2885 drm_dbg_kms(mgr->dev, "check mstb guid failed - undocked during suspend?\n");
2886 goto out_fail;
2887 }
2888 }
2889
2890 guid_copy(&mgr->mst_primary->guid, &guid);
2891
2892out_fail:
2893 mutex_unlock(&mgr->lock);
2894}
2895
2896void hdmi_cec_unset_edid(struct amdgpu_dm_connector *aconnector)
2897{
2898 struct cec_notifier *n = aconnector->notifier;
2899
2900 if (!n)
2901 return;
2902
2903 cec_notifier_phys_addr_invalidate(n);
2904}
2905
2906void hdmi_cec_set_edid(struct amdgpu_dm_connector *aconnector)
2907{
2908 struct drm_connector *connector = &aconnector->base;
2909 struct cec_notifier *n = aconnector->notifier;
2910
2911 if (!n)
2912 return;
2913
2914 cec_notifier_set_phys_addr(n,
2915 connector->display_info.source_physical_address);
2916}
2917
2918static void s3_handle_hdmi_cec(struct drm_device *ddev, bool suspend)
2919{
2920 struct amdgpu_dm_connector *aconnector;
2921 struct drm_connector *connector;
2922 struct drm_connector_list_iter conn_iter;
2923
2924 drm_connector_list_iter_begin(ddev, &conn_iter);
2925 drm_for_each_connector_iter(connector, &conn_iter) {
2926 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
2927 continue;
2928
2929 aconnector = to_amdgpu_dm_connector(connector);
2930 if (suspend)
2931 hdmi_cec_unset_edid(aconnector);
2932 else
2933 hdmi_cec_set_edid(aconnector);
2934 }
2935 drm_connector_list_iter_end(&conn_iter);
2936}
2937
2938static void s3_handle_mst(struct drm_device *dev, bool suspend)
2939{
2940 struct amdgpu_dm_connector *aconnector;
2941 struct drm_connector *connector;
2942 struct drm_connector_list_iter iter;
2943 struct drm_dp_mst_topology_mgr *mgr;
2944
2945 drm_connector_list_iter_begin(dev, &iter);
2946 drm_for_each_connector_iter(connector, &iter) {
2947
2948 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
2949 continue;
2950
2951 aconnector = to_amdgpu_dm_connector(connector);
2952 if (aconnector->dc_link->type != dc_connection_mst_branch ||
2953 aconnector->mst_root)
2954 continue;
2955
2956 mgr = &aconnector->mst_mgr;
2957
2958 if (suspend) {
2959 drm_dp_mst_topology_mgr_suspend(mgr);
2960 } else {
2961 /* if extended timeout is supported in hardware,
2962 * default to LTTPR timeout (3.2ms) first as a W/A for DP link layer
2963 * CTS 4.2.1.1 regression introduced by CTS specs requirement update.
2964 */
2965 try_to_configure_aux_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_LTTPR_TIMEOUT_PERIOD);
2966 if (!dp_is_lttpr_present(aconnector->dc_link))
2967 try_to_configure_aux_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_TIMEOUT_PERIOD);
2968
2969 /* TODO: move resume_mst_branch_status() into drm mst resume again
2970 * once topology probing work is pulled out from mst resume into mst
2971 * resume 2nd step. mst resume 2nd step should be called after old
2972 * state getting restored (i.e. drm_atomic_helper_resume()).
2973 */
2974 resume_mst_branch_status(mgr);
2975 }
2976 }
2977 drm_connector_list_iter_end(&iter);
2978}
2979
2980static int amdgpu_dm_smu_write_watermarks_table(struct amdgpu_device *adev)
2981{
2982 int ret = 0;
2983
2984 /* This interface is for dGPU Navi1x.Linux dc-pplib interface depends
2985 * on window driver dc implementation.
2986 * For Navi1x, clock settings of dcn watermarks are fixed. the settings
2987 * should be passed to smu during boot up and resume from s3.
2988 * boot up: dc calculate dcn watermark clock settings within dc_create,
2989 * dcn20_resource_construct
2990 * then call pplib functions below to pass the settings to smu:
2991 * smu_set_watermarks_for_clock_ranges
2992 * smu_set_watermarks_table
2993 * navi10_set_watermarks_table
2994 * smu_write_watermarks_table
2995 *
2996 * For Renoir, clock settings of dcn watermark are also fixed values.
2997 * dc has implemented different flow for window driver:
2998 * dc_hardware_init / dc_set_power_state
2999 * dcn10_init_hw
3000 * notify_wm_ranges
3001 * set_wm_ranges
3002 * -- Linux
3003 * smu_set_watermarks_for_clock_ranges
3004 * renoir_set_watermarks_table
3005 * smu_write_watermarks_table
3006 *
3007 * For Linux,
3008 * dc_hardware_init -> amdgpu_dm_init
3009 * dc_set_power_state --> dm_resume
3010 *
3011 * therefore, this function apply to navi10/12/14 but not Renoir
3012 * *
3013 */
3014 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
3015 case IP_VERSION(2, 0, 2):
3016 case IP_VERSION(2, 0, 0):
3017 break;
3018 default:
3019 return 0;
3020 }
3021
3022 ret = amdgpu_dpm_write_watermarks_table(adev);
3023 if (ret) {
3024 drm_err(adev_to_drm(adev), "Failed to update WMTABLE!\n");
3025 return ret;
3026 }
3027
3028 return 0;
3029}
3030
3031static int dm_oem_i2c_hw_init(struct amdgpu_device *adev)
3032{
3033 struct amdgpu_display_manager *dm = &adev->dm;
3034 struct amdgpu_i2c_adapter *oem_i2c;
3035 struct ddc_service *oem_ddc_service;
3036 int r;
3037
3038 oem_ddc_service = dc_get_oem_i2c_device(adev->dm.dc);
3039 if (oem_ddc_service) {
3040 oem_i2c = create_i2c(oem_ddc_service, true);
3041 if (!oem_i2c) {
3042 drm_info(adev_to_drm(adev), "Failed to create oem i2c adapter data\n");
3043 return -ENOMEM;
3044 }
3045
3046 r = devm_i2c_add_adapter(adev->dev, &oem_i2c->base);
3047 if (r) {
3048 drm_info(adev_to_drm(adev), "Failed to register oem i2c\n");
3049 kfree(oem_i2c);
3050 return r;
3051 }
3052 dm->oem_i2c = oem_i2c;
3053 }
3054
3055 return 0;
3056}
3057
3058/**
3059 * dm_hw_init() - Initialize DC device
3060 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
3061 *
3062 * Initialize the &struct amdgpu_display_manager device. This involves calling
3063 * the initializers of each DM component, then populating the struct with them.
3064 *
3065 * Although the function implies hardware initialization, both hardware and
3066 * software are initialized here. Splitting them out to their relevant init
3067 * hooks is a future TODO item.
3068 *
3069 * Some notable things that are initialized here:
3070 *
3071 * - Display Core, both software and hardware
3072 * - DC modules that we need (freesync and color management)
3073 * - DRM software states
3074 * - Interrupt sources and handlers
3075 * - Vblank support
3076 * - Debug FS entries, if enabled
3077 */
3078static int dm_hw_init(struct amdgpu_ip_block *ip_block)
3079{
3080 struct amdgpu_device *adev = ip_block->adev;
3081 int r;
3082
3083 /* Create DAL display manager */
3084 r = amdgpu_dm_init(adev);
3085 if (r)
3086 return r;
3087 amdgpu_dm_hpd_init(adev);
3088
3089 r = dm_oem_i2c_hw_init(adev);
3090 if (r)
3091 drm_info(adev_to_drm(adev), "Failed to add OEM i2c bus\n");
3092
3093 return 0;
3094}
3095
3096/**
3097 * dm_hw_fini() - Teardown DC device
3098 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
3099 *
3100 * Teardown components within &struct amdgpu_display_manager that require
3101 * cleanup. This involves cleaning up the DRM device, DC, and any modules that
3102 * were loaded. Also flush IRQ workqueues and disable them.
3103 */
3104static int dm_hw_fini(struct amdgpu_ip_block *ip_block)
3105{
3106 struct amdgpu_device *adev = ip_block->adev;
3107
3108 amdgpu_dm_hpd_fini(adev);
3109
3110 amdgpu_dm_irq_fini(adev);
3111 amdgpu_dm_fini(adev);
3112 return 0;
3113}
3114
3115
3116static void dm_gpureset_toggle_interrupts(struct amdgpu_device *adev,
3117 struct dc_state *state, bool enable)
3118{
3119 enum dc_irq_source irq_source;
3120 struct amdgpu_crtc *acrtc;
3121 int rc = -EBUSY;
3122 int i = 0;
3123
3124 for (i = 0; i < state->stream_count; i++) {
3125 acrtc = get_crtc_by_otg_inst(
3126 adev, state->stream_status[i].primary_otg_inst);
3127
3128 if (acrtc && state->stream_status[i].plane_count != 0) {
3129 irq_source = IRQ_TYPE_PFLIP + acrtc->otg_inst;
3130 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
3131 if (rc)
3132 drm_warn(adev_to_drm(adev), "Failed to %s pflip interrupts\n",
3133 enable ? "enable" : "disable");
3134
3135 if (dc_supports_vrr(adev->dm.dc->ctx->dce_version)) {
3136 if (enable) {
3137 if (amdgpu_dm_crtc_vrr_active(
3138 to_dm_crtc_state(acrtc->base.state)))
3139 rc = amdgpu_dm_crtc_set_vupdate_irq(
3140 &acrtc->base, true);
3141 } else
3142 rc = amdgpu_dm_crtc_set_vupdate_irq(
3143 &acrtc->base, false);
3144
3145 if (rc)
3146 drm_warn(adev_to_drm(adev), "Failed to %sable vupdate interrupt\n",
3147 enable ? "en" : "dis");
3148 }
3149
3150 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
3151 /* During gpu-reset we disable and then enable vblank irq, so
3152 * don't use amdgpu_irq_get/put() to avoid refcount change.
3153 */
3154 if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
3155 drm_warn(adev_to_drm(adev), "Failed to %sable vblank interrupt\n", enable ? "en" : "dis");
3156 }
3157 }
3158
3159}
3160
3161DEFINE_FREE(state_release, struct dc_state *, if (_T) dc_state_release(_T))
3162
3163static enum dc_status amdgpu_dm_commit_zero_streams(struct dc *dc)
3164{
3165 struct dc_state *context __free(state_release) = NULL;
3166 int i;
3167 struct dc_stream_state *del_streams[MAX_PIPES];
3168 int del_streams_count = 0;
3169 struct dc_commit_streams_params params = {};
3170
3171 memset(del_streams, 0, sizeof(del_streams));
3172
3173 context = dc_state_create_current_copy(dc);
3174 if (context == NULL)
3175 return DC_ERROR_UNEXPECTED;
3176
3177 /* First remove from context all streams */
3178 for (i = 0; i < context->stream_count; i++) {
3179 struct dc_stream_state *stream = context->streams[i];
3180
3181 del_streams[del_streams_count++] = stream;
3182 }
3183
3184 /* Remove all planes for removed streams and then remove the streams */
3185 for (i = 0; i < del_streams_count; i++) {
3186 enum dc_status res;
3187
3188 if (!dc_state_rem_all_planes_for_stream(dc, del_streams[i], context))
3189 return DC_FAIL_DETACH_SURFACES;
3190
3191 res = dc_state_remove_stream(dc, context, del_streams[i]);
3192 if (res != DC_OK)
3193 return res;
3194 }
3195
3196 params.streams = context->streams;
3197 params.stream_count = context->stream_count;
3198
3199 return dc_commit_streams(dc, ¶ms);
3200}
3201
3202static void hpd_rx_irq_work_suspend(struct amdgpu_display_manager *dm)
3203{
3204 int i;
3205
3206 if (dm->hpd_rx_offload_wq) {
3207 for (i = 0; i < dm->dc->caps.max_links; i++)
3208 flush_workqueue(dm->hpd_rx_offload_wq[i].wq);
3209 }
3210}
3211
3212static int dm_cache_state(struct amdgpu_device *adev)
3213{
3214 int r;
3215
3216 adev->dm.cached_state = drm_atomic_helper_suspend(adev_to_drm(adev));
3217 if (IS_ERR(adev->dm.cached_state)) {
3218 r = PTR_ERR(adev->dm.cached_state);
3219 adev->dm.cached_state = NULL;
3220 }
3221
3222 return adev->dm.cached_state ? 0 : r;
3223}
3224
3225static void dm_destroy_cached_state(struct amdgpu_device *adev)
3226{
3227 struct amdgpu_display_manager *dm = &adev->dm;
3228 struct drm_device *ddev = adev_to_drm(adev);
3229 struct dm_plane_state *dm_new_plane_state;
3230 struct drm_plane_state *new_plane_state;
3231 struct dm_crtc_state *dm_new_crtc_state;
3232 struct drm_crtc_state *new_crtc_state;
3233 struct drm_plane *plane;
3234 struct drm_crtc *crtc;
3235 int i;
3236
3237 if (!dm->cached_state)
3238 return;
3239
3240 /* Force mode set in atomic commit */
3241 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
3242 new_crtc_state->active_changed = true;
3243 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
3244 reset_freesync_config_for_crtc(dm_new_crtc_state);
3245 }
3246
3247 /*
3248 * atomic_check is expected to create the dc states. We need to release
3249 * them here, since they were duplicated as part of the suspend
3250 * procedure.
3251 */
3252 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
3253 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
3254 if (dm_new_crtc_state->stream) {
3255 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
3256 dc_stream_release(dm_new_crtc_state->stream);
3257 dm_new_crtc_state->stream = NULL;
3258 }
3259 dm_new_crtc_state->base.color_mgmt_changed = true;
3260 }
3261
3262 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
3263 dm_new_plane_state = to_dm_plane_state(new_plane_state);
3264 if (dm_new_plane_state->dc_state) {
3265 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
3266 dc_plane_state_release(dm_new_plane_state->dc_state);
3267 dm_new_plane_state->dc_state = NULL;
3268 }
3269 }
3270
3271 drm_atomic_helper_resume(ddev, dm->cached_state);
3272
3273 dm->cached_state = NULL;
3274}
3275
3276static int dm_suspend(struct amdgpu_ip_block *ip_block)
3277{
3278 struct amdgpu_device *adev = ip_block->adev;
3279 struct amdgpu_display_manager *dm = &adev->dm;
3280
3281 if (amdgpu_in_reset(adev)) {
3282 enum dc_status res;
3283
3284 mutex_lock(&dm->dc_lock);
3285
3286 amdgpu_dm_ism_disable(dm);
3287 dc_allow_idle_optimizations(adev->dm.dc, false);
3288
3289 dm->cached_dc_state = dc_state_create_copy(dm->dc->current_state);
3290
3291 if (dm->cached_dc_state)
3292 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, false);
3293
3294 res = amdgpu_dm_commit_zero_streams(dm->dc);
3295 if (res != DC_OK) {
3296 drm_err(adev_to_drm(adev), "Failed to commit zero streams: %d\n", res);
3297 return -EINVAL;
3298 }
3299
3300 amdgpu_dm_irq_suspend(adev);
3301
3302 hpd_rx_irq_work_suspend(dm);
3303
3304 return 0;
3305 }
3306
3307 if (!adev->dm.cached_state) {
3308 int r = dm_cache_state(adev);
3309
3310 if (r)
3311 return r;
3312 }
3313
3314 s3_handle_hdmi_cec(adev_to_drm(adev), true);
3315
3316 s3_handle_mst(adev_to_drm(adev), true);
3317
3318 amdgpu_dm_irq_suspend(adev);
3319
3320 scoped_guard(mutex, &dm->dc_lock)
3321 amdgpu_dm_ism_disable(dm);
3322
3323 hpd_rx_irq_work_suspend(dm);
3324
3325 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
3326
3327 if (dm->dc->caps.ips_support && adev->in_s0ix)
3328 dc_allow_idle_optimizations(dm->dc, true);
3329
3330 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D3);
3331
3332 return 0;
3333}
3334
3335struct drm_connector *
3336amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
3337 struct drm_crtc *crtc)
3338{
3339 u32 i;
3340 struct drm_connector_state *new_con_state;
3341 struct drm_connector *connector;
3342 struct drm_crtc *crtc_from_state;
3343
3344 for_each_new_connector_in_state(state, connector, new_con_state, i) {
3345 crtc_from_state = new_con_state->crtc;
3346
3347 if (crtc_from_state == crtc)
3348 return connector;
3349 }
3350
3351 return NULL;
3352}
3353
3354static void emulated_link_detect(struct dc_link *link)
3355{
3356 struct dc_sink_init_data sink_init_data = { 0 };
3357 struct display_sink_capability sink_caps = { 0 };
3358 enum dc_edid_status edid_status;
3359 struct dc_context *dc_ctx = link->ctx;
3360 struct drm_device *dev = adev_to_drm(dc_ctx->driver_context);
3361 struct dc_sink *sink = NULL;
3362 struct dc_sink *prev_sink = NULL;
3363
3364 link->type = dc_connection_none;
3365 prev_sink = link->local_sink;
3366
3367 if (prev_sink)
3368 dc_sink_release(prev_sink);
3369
3370 switch (link->connector_signal) {
3371 case SIGNAL_TYPE_HDMI_TYPE_A: {
3372 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
3373 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
3374 break;
3375 }
3376
3377 case SIGNAL_TYPE_DVI_SINGLE_LINK: {
3378 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
3379 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
3380 break;
3381 }
3382
3383 case SIGNAL_TYPE_DVI_DUAL_LINK: {
3384 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
3385 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
3386 break;
3387 }
3388
3389 case SIGNAL_TYPE_LVDS: {
3390 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
3391 sink_caps.signal = SIGNAL_TYPE_LVDS;
3392 break;
3393 }
3394
3395 case SIGNAL_TYPE_EDP: {
3396 sink_caps.transaction_type =
3397 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
3398 sink_caps.signal = SIGNAL_TYPE_EDP;
3399 break;
3400 }
3401
3402 case SIGNAL_TYPE_DISPLAY_PORT: {
3403 sink_caps.transaction_type =
3404 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
3405 sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
3406 break;
3407 }
3408
3409 default:
3410 drm_err(dev, "Invalid connector type! signal:%d\n",
3411 link->connector_signal);
3412 return;
3413 }
3414
3415 sink_init_data.link = link;
3416 sink_init_data.sink_signal = sink_caps.signal;
3417
3418 sink = dc_sink_create(&sink_init_data);
3419 if (!sink) {
3420 drm_err(dev, "Failed to create sink!\n");
3421 return;
3422 }
3423
3424 /* dc_sink_create returns a new reference */
3425 link->local_sink = sink;
3426
3427 edid_status = dm_helpers_read_local_edid(
3428 link->ctx,
3429 link,
3430 sink);
3431
3432 if (edid_status != EDID_OK)
3433 drm_err(dev, "Failed to read EDID\n");
3434
3435}
3436
3437static void dm_gpureset_commit_state(struct dc_state *dc_state,
3438 struct amdgpu_display_manager *dm)
3439{
3440 struct {
3441 struct dc_surface_update surface_updates[MAX_SURFACES];
3442 struct dc_plane_info plane_infos[MAX_SURFACES];
3443 struct dc_scaling_info scaling_infos[MAX_SURFACES];
3444 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
3445 struct dc_stream_update stream_update;
3446 } *bundle __free(kfree);
3447 int k, m;
3448
3449 bundle = kzalloc_obj(*bundle);
3450
3451 if (!bundle) {
3452 drm_err(dm->ddev, "Failed to allocate update bundle\n");
3453 return;
3454 }
3455
3456 for (k = 0; k < dc_state->stream_count; k++) {
3457 bundle->stream_update.stream = dc_state->streams[k];
3458
3459 for (m = 0; m < dc_state->stream_status[k].plane_count; m++) {
3460 bundle->surface_updates[m].surface =
3461 dc_state->stream_status[k].plane_states[m];
3462 bundle->surface_updates[m].surface->force_full_update =
3463 true;
3464 }
3465
3466 update_planes_and_stream_adapter(dm->dc,
3467 UPDATE_TYPE_FULL,
3468 dc_state->stream_status[k].plane_count,
3469 dc_state->streams[k],
3470 &bundle->stream_update,
3471 bundle->surface_updates);
3472 }
3473}
3474
3475static void apply_delay_after_dpcd_poweroff(struct amdgpu_device *adev,
3476 struct dc_sink *sink)
3477{
3478 struct dc_panel_patch *ppatch = NULL;
3479
3480 if (!sink)
3481 return;
3482
3483 ppatch = &sink->edid_caps.panel_patch;
3484 if (ppatch->wait_after_dpcd_poweroff_ms) {
3485 msleep(ppatch->wait_after_dpcd_poweroff_ms);
3486 drm_dbg_driver(adev_to_drm(adev),
3487 "%s: adding a %ds delay as w/a for panel\n",
3488 __func__,
3489 ppatch->wait_after_dpcd_poweroff_ms / 1000);
3490 }
3491}
3492
3493/**
3494 * amdgpu_dm_dump_links_and_sinks - Debug dump of all DC links and their sinks
3495 * @adev: amdgpu device pointer
3496 *
3497 * Iterates through all DC links and dumps information about local and remote
3498 * (MST) sinks. Should be called after connector detection is complete to see
3499 * the final state of all links.
3500 */
3501static void amdgpu_dm_dump_links_and_sinks(struct amdgpu_device *adev)
3502{
3503 struct dc *dc = adev->dm.dc;
3504 struct drm_device *dev = adev_to_drm(adev);
3505 int li;
3506
3507 if (!dc)
3508 return;
3509
3510 for (li = 0; li < dc->link_count; li++) {
3511 struct dc_link *l = dc->links[li];
3512 const char *name = NULL;
3513 int rs;
3514
3515 if (!l)
3516 continue;
3517 if (l->local_sink && l->local_sink->edid_caps.display_name[0])
3518 name = l->local_sink->edid_caps.display_name;
3519 else
3520 name = "n/a";
3521
3522 drm_dbg_kms(dev,
3523 "LINK_DUMP[%d]: local_sink=%p type=%d sink_signal=%d sink_count=%u edid_name=%s mst_capable=%d mst_alloc_streams=%d\n",
3524 li,
3525 l->local_sink,
3526 l->type,
3527 l->local_sink ? l->local_sink->sink_signal : SIGNAL_TYPE_NONE,
3528 l->sink_count,
3529 name,
3530 l->dpcd_caps.is_mst_capable,
3531 l->mst_stream_alloc_table.stream_count);
3532
3533 /* Dump remote (MST) sinks if any */
3534 for (rs = 0; rs < l->sink_count; rs++) {
3535 struct dc_sink *rsink = l->remote_sinks[rs];
3536 const char *rname = NULL;
3537
3538 if (!rsink)
3539 continue;
3540 if (rsink->edid_caps.display_name[0])
3541 rname = rsink->edid_caps.display_name;
3542 else
3543 rname = "n/a";
3544 drm_dbg_kms(dev,
3545 " REMOTE_SINK[%d:%d]: sink=%p signal=%d edid_name=%s\n",
3546 li, rs,
3547 rsink,
3548 rsink->sink_signal,
3549 rname);
3550 }
3551 }
3552}
3553
3554static int dm_resume(struct amdgpu_ip_block *ip_block)
3555{
3556 struct amdgpu_device *adev = ip_block->adev;
3557 struct drm_device *ddev = adev_to_drm(adev);
3558 struct amdgpu_display_manager *dm = &adev->dm;
3559 struct amdgpu_dm_connector *aconnector;
3560 struct drm_connector *connector;
3561 struct drm_connector_list_iter iter;
3562 struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state);
3563 enum dc_connection_type new_connection_type = dc_connection_none;
3564 struct dc_state *dc_state;
3565 int i, r, j;
3566 struct dc_commit_streams_params commit_params = {};
3567
3568 if (dm->dc->caps.ips_support) {
3569 if (!amdgpu_in_reset(adev))
3570 mutex_lock(&dm->dc_lock);
3571
3572 /* Need to set POWER_STATE_D0 first or it will not execute
3573 * idle_power_optimizations command to DMUB.
3574 */
3575 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0);
3576 dc_dmub_srv_apply_idle_power_optimizations(dm->dc, false);
3577
3578 if (!amdgpu_in_reset(adev))
3579 mutex_unlock(&dm->dc_lock);
3580 }
3581
3582 if (amdgpu_in_reset(adev)) {
3583 dc_state = dm->cached_dc_state;
3584
3585 /*
3586 * The dc->current_state is backed up into dm->cached_dc_state
3587 * before we commit 0 streams.
3588 *
3589 * DC will clear link encoder assignments on the real state
3590 * but the changes won't propagate over to the copy we made
3591 * before the 0 streams commit.
3592 *
3593 * DC expects that link encoder assignments are *not* valid
3594 * when committing a state, so as a workaround we can copy
3595 * off of the current state.
3596 *
3597 * We lose the previous assignments, but we had already
3598 * commit 0 streams anyway.
3599 */
3600 link_enc_cfg_copy(adev->dm.dc->current_state, dc_state);
3601
3602 r = dm_dmub_hw_init(adev);
3603 if (r) {
3604 drm_err(adev_to_drm(adev), "DMUB interface failed to initialize: status=%d\n", r);
3605 return r;
3606 }
3607
3608 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0);
3609 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
3610
3611 dc_resume(dm->dc);
3612
3613 amdgpu_dm_ism_enable(dm);
3614 amdgpu_dm_irq_resume_early(adev);
3615
3616 for (i = 0; i < dc_state->stream_count; i++) {
3617 dc_state->streams[i]->mode_changed = true;
3618 for (j = 0; j < dc_state->stream_status[i].plane_count; j++) {
3619 dc_state->stream_status[i].plane_states[j]->update_flags.raw
3620 = 0xffffffff;
3621 }
3622 }
3623
3624 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
3625 amdgpu_dm_outbox_init(adev);
3626 dc_enable_dmub_outbox(adev->dm.dc);
3627 }
3628
3629 commit_params.streams = dc_state->streams;
3630 commit_params.stream_count = dc_state->stream_count;
3631 dc_exit_ips_for_hw_access(dm->dc);
3632 WARN_ON(!dc_commit_streams(dm->dc, &commit_params));
3633
3634 dm_gpureset_commit_state(dm->cached_dc_state, dm);
3635
3636 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, true);
3637
3638 dc_state_release(dm->cached_dc_state);
3639 dm->cached_dc_state = NULL;
3640
3641 amdgpu_dm_irq_resume_late(adev);
3642
3643 mutex_unlock(&dm->dc_lock);
3644
3645 /* set the backlight after a reset */
3646 for (i = 0; i < dm->num_of_edps; i++) {
3647 if (dm->backlight_dev[i])
3648 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
3649 }
3650
3651 return 0;
3652 }
3653 /* Recreate dc_state - DC invalidates it when setting power state to S3. */
3654 dc_state_release(dm_state->context);
3655 dm_state->context = dc_state_create(dm->dc, NULL);
3656 /* TODO: Remove dc_state->dccg, use dc->dccg directly. */
3657
3658 /* Before powering on DC we need to re-initialize DMUB. */
3659 dm_dmub_hw_resume(adev);
3660
3661 /* Re-enable outbox interrupts for DPIA. */
3662 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
3663 amdgpu_dm_outbox_init(adev);
3664 dc_enable_dmub_outbox(adev->dm.dc);
3665 }
3666
3667 /* power on hardware */
3668 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0);
3669 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
3670
3671 /* program HPD filter */
3672 dc_resume(dm->dc);
3673
3674 scoped_guard(mutex, &dm->dc_lock)
3675 amdgpu_dm_ism_enable(dm);
3676
3677 /*
3678 * early enable HPD Rx IRQ, should be done before set mode as short
3679 * pulse interrupts are used for MST
3680 */
3681 amdgpu_dm_irq_resume_early(adev);
3682
3683 s3_handle_hdmi_cec(ddev, false);
3684
3685 /* On resume we need to rewrite the MSTM control bits to enable MST*/
3686 s3_handle_mst(ddev, false);
3687
3688 /* Do detection*/
3689 drm_connector_list_iter_begin(ddev, &iter);
3690 drm_for_each_connector_iter(connector, &iter) {
3691 bool ret;
3692
3693 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
3694 continue;
3695
3696 aconnector = to_amdgpu_dm_connector(connector);
3697
3698 if (!aconnector->dc_link)
3699 continue;
3700
3701 /*
3702 * this is the case when traversing through already created end sink
3703 * MST connectors, should be skipped
3704 */
3705 if (aconnector->mst_root)
3706 continue;
3707
3708 /* Skip eDP detection, when there is no sink present */
3709 if (aconnector->dc_link->connector_signal == SIGNAL_TYPE_EDP &&
3710 !aconnector->dc_link->edp_sink_present)
3711 continue;
3712
3713 guard(mutex)(&aconnector->hpd_lock);
3714 if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type))
3715 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
3716
3717 if (aconnector->base.force && new_connection_type == dc_connection_none) {
3718 emulated_link_detect(aconnector->dc_link);
3719 } else {
3720 guard(mutex)(&dm->dc_lock);
3721 dc_exit_ips_for_hw_access(dm->dc);
3722 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_RESUMEFROMS3S4);
3723 if (ret) {
3724 /* w/a delay for certain panels */
3725 apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink);
3726 }
3727 }
3728
3729 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
3730 aconnector->fake_enable = false;
3731
3732 if (aconnector->dc_sink)
3733 dc_sink_release(aconnector->dc_sink);
3734 aconnector->dc_sink = NULL;
3735 amdgpu_dm_update_connector_after_detect(aconnector);
3736 }
3737 drm_connector_list_iter_end(&iter);
3738
3739 dm_destroy_cached_state(adev);
3740
3741 /* Do mst topology probing after resuming cached state*/
3742 drm_connector_list_iter_begin(ddev, &iter);
3743 drm_for_each_connector_iter(connector, &iter) {
3744 bool init = false;
3745
3746 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
3747 continue;
3748
3749 aconnector = to_amdgpu_dm_connector(connector);
3750 if (aconnector->dc_link->type != dc_connection_mst_branch ||
3751 aconnector->mst_root)
3752 continue;
3753
3754 scoped_guard(mutex, &aconnector->mst_mgr.lock) {
3755 init = !aconnector->mst_mgr.mst_primary;
3756 }
3757 if (init)
3758 dm_helpers_dp_mst_start_top_mgr(aconnector->dc_link->ctx,
3759 aconnector->dc_link, false);
3760 else
3761 drm_dp_mst_topology_queue_probe(&aconnector->mst_mgr);
3762 }
3763 drm_connector_list_iter_end(&iter);
3764
3765 /* Debug dump: list all DC links and their associated sinks after detection
3766 * is complete for all connectors. This provides a comprehensive view of the
3767 * final state without repeating the dump for each connector.
3768 */
3769 amdgpu_dm_dump_links_and_sinks(adev);
3770
3771 amdgpu_dm_irq_resume_late(adev);
3772
3773 amdgpu_dm_smu_write_watermarks_table(adev);
3774
3775 drm_kms_helper_hotplug_event(ddev);
3776
3777 return 0;
3778}
3779
3780/**
3781 * DOC: DM Lifecycle
3782 *
3783 * DM (and consequently DC) is registered in the amdgpu base driver as a IP
3784 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to
3785 * the base driver's device list to be initialized and torn down accordingly.
3786 *
3787 * The functions to do so are provided as hooks in &struct amd_ip_funcs.
3788 */
3789
3790static const struct amd_ip_funcs amdgpu_dm_funcs = {
3791 .name = "dm",
3792 .early_init = dm_early_init,
3793 .late_init = dm_late_init,
3794 .sw_init = dm_sw_init,
3795 .sw_fini = dm_sw_fini,
3796 .early_fini = amdgpu_dm_early_fini,
3797 .hw_init = dm_hw_init,
3798 .hw_fini = dm_hw_fini,
3799 .suspend = dm_suspend,
3800 .resume = dm_resume,
3801 .is_idle = dm_is_idle,
3802 .wait_for_idle = dm_wait_for_idle,
3803 .check_soft_reset = dm_check_soft_reset,
3804 .soft_reset = dm_soft_reset,
3805 .set_clockgating_state = dm_set_clockgating_state,
3806 .set_powergating_state = dm_set_powergating_state,
3807};
3808
3809const struct amdgpu_ip_block_version dm_ip_block = {
3810 .type = AMD_IP_BLOCK_TYPE_DCE,
3811 .major = 1,
3812 .minor = 0,
3813 .rev = 0,
3814 .funcs = &amdgpu_dm_funcs,
3815};
3816
3817
3818/**
3819 * DOC: atomic
3820 *
3821 * *WIP*
3822 */
3823
3824static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
3825 .fb_create = amdgpu_display_user_framebuffer_create,
3826 .get_format_info = amdgpu_dm_plane_get_format_info,
3827 .atomic_check = amdgpu_dm_atomic_check,
3828 .atomic_commit = drm_atomic_helper_commit,
3829};
3830
3831static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
3832 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail,
3833 .atomic_commit_setup = amdgpu_dm_atomic_setup_commit,
3834};
3835
3836static void update_connector_ext_caps(struct amdgpu_dm_connector *aconnector)
3837{
3838 const struct drm_panel_backlight_quirk *panel_backlight_quirk;
3839 struct amdgpu_dm_backlight_caps *caps;
3840 struct drm_connector *conn_base;
3841 struct amdgpu_device *adev;
3842 struct drm_luminance_range_info *luminance_range;
3843 struct drm_device *drm;
3844
3845 if (aconnector->bl_idx == -1 ||
3846 aconnector->dc_link->connector_signal != SIGNAL_TYPE_EDP)
3847 return;
3848
3849 conn_base = &aconnector->base;
3850 drm = conn_base->dev;
3851 adev = drm_to_adev(drm);
3852
3853 caps = &adev->dm.backlight_caps[aconnector->bl_idx];
3854 caps->ext_caps = &aconnector->dc_link->dpcd_sink_ext_caps;
3855 caps->aux_support = false;
3856
3857 drm_object_property_set_value(&conn_base->base,
3858 adev_to_drm(adev)->mode_config.panel_type_property,
3859 caps->ext_caps->bits.oled ? DRM_MODE_PANEL_TYPE_OLED : DRM_MODE_PANEL_TYPE_UNKNOWN);
3860
3861 if (caps->ext_caps->bits.oled == 1
3862 /*
3863 * ||
3864 * caps->ext_caps->bits.sdr_aux_backlight_control == 1 ||
3865 * caps->ext_caps->bits.hdr_aux_backlight_control == 1
3866 */)
3867 caps->aux_support = true;
3868
3869 if (amdgpu_backlight == 0)
3870 caps->aux_support = false;
3871 else if (amdgpu_backlight == 1)
3872 caps->aux_support = true;
3873 if (caps->aux_support)
3874 aconnector->dc_link->backlight_control_type = BACKLIGHT_CONTROL_AMD_AUX;
3875
3876 luminance_range = &conn_base->display_info.luminance_range;
3877
3878 if (luminance_range->max_luminance)
3879 caps->aux_max_input_signal = luminance_range->max_luminance;
3880 else
3881 caps->aux_max_input_signal = 512;
3882
3883 if (luminance_range->min_luminance)
3884 caps->aux_min_input_signal = luminance_range->min_luminance;
3885 else
3886 caps->aux_min_input_signal = 1;
3887
3888 panel_backlight_quirk =
3889 drm_get_panel_backlight_quirk(aconnector->drm_edid);
3890 if (!IS_ERR_OR_NULL(panel_backlight_quirk)) {
3891 if (panel_backlight_quirk->min_brightness) {
3892 caps->min_input_signal =
3893 panel_backlight_quirk->min_brightness - 1;
3894 drm_info(drm,
3895 "Applying panel backlight quirk, min_brightness: %d\n",
3896 caps->min_input_signal);
3897 }
3898 if (panel_backlight_quirk->brightness_mask) {
3899 drm_info(drm,
3900 "Applying panel backlight quirk, brightness_mask: 0x%X\n",
3901 panel_backlight_quirk->brightness_mask);
3902 caps->brightness_mask =
3903 panel_backlight_quirk->brightness_mask;
3904 }
3905 }
3906}
3907
3908DEFINE_FREE(sink_release, struct dc_sink *, if (_T) dc_sink_release(_T))
3909
3910void amdgpu_dm_update_connector_after_detect(
3911 struct amdgpu_dm_connector *aconnector)
3912{
3913 struct drm_connector *connector = &aconnector->base;
3914 struct dc_sink *sink __free(sink_release) = NULL;
3915 struct drm_device *dev = connector->dev;
3916
3917 /* MST handled by drm_mst framework */
3918 if (aconnector->mst_mgr.mst_state == true)
3919 return;
3920
3921 sink = aconnector->dc_link->local_sink;
3922 if (sink)
3923 dc_sink_retain(sink);
3924
3925 /*
3926 * Edid mgmt connector gets first update only in mode_valid hook and then
3927 * the connector sink is set to either fake or physical sink depends on link status.
3928 * Skip if already done during boot.
3929 */
3930 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
3931 && aconnector->dc_em_sink) {
3932
3933 /*
3934 * For S3 resume with headless use eml_sink to fake stream
3935 * because on resume connector->sink is set to NULL
3936 */
3937 guard(mutex)(&dev->mode_config.mutex);
3938
3939 if (sink) {
3940 if (aconnector->dc_sink) {
3941 amdgpu_dm_update_freesync_caps(connector, NULL);
3942 /*
3943 * retain and release below are used to
3944 * bump up refcount for sink because the link doesn't point
3945 * to it anymore after disconnect, so on next crtc to connector
3946 * reshuffle by UMD we will get into unwanted dc_sink release
3947 */
3948 dc_sink_release(aconnector->dc_sink);
3949 }
3950 aconnector->dc_sink = sink;
3951 dc_sink_retain(aconnector->dc_sink);
3952 amdgpu_dm_update_freesync_caps(connector,
3953 aconnector->drm_edid);
3954 } else {
3955 amdgpu_dm_update_freesync_caps(connector, NULL);
3956 if (!aconnector->dc_sink) {
3957 aconnector->dc_sink = aconnector->dc_em_sink;
3958 dc_sink_retain(aconnector->dc_sink);
3959 }
3960 }
3961
3962 return;
3963 }
3964
3965 /*
3966 * TODO: temporary guard to look for proper fix
3967 * if this sink is MST sink, we should not do anything
3968 */
3969 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
3970 return;
3971
3972 if (aconnector->dc_sink == sink) {
3973 /*
3974 * We got a DP short pulse (Link Loss, DP CTS, etc...).
3975 * Do nothing!!
3976 */
3977 drm_dbg_kms(dev, "DCHPD: connector_id=%d: dc_sink didn't change.\n",
3978 aconnector->connector_id);
3979 return;
3980 }
3981
3982 drm_dbg_kms(dev, "DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
3983 aconnector->connector_id, aconnector->dc_sink, sink);
3984
3985 /* When polling, DRM has already locked the mutex for us. */
3986 if (!drm_kms_helper_is_poll_worker())
3987 mutex_lock(&dev->mode_config.mutex);
3988
3989 /*
3990 * 1. Update status of the drm connector
3991 * 2. Send an event and let userspace tell us what to do
3992 */
3993 if (sink) {
3994 /*
3995 * TODO: check if we still need the S3 mode update workaround.
3996 * If yes, put it here.
3997 */
3998 if (aconnector->dc_sink) {
3999 amdgpu_dm_update_freesync_caps(connector, NULL);
4000 dc_sink_release(aconnector->dc_sink);
4001 }
4002
4003 aconnector->dc_sink = sink;
4004 dc_sink_retain(aconnector->dc_sink);
4005 drm_edid_free(aconnector->drm_edid);
4006 aconnector->drm_edid = NULL;
4007 if (sink->dc_edid.length == 0) {
4008 hdmi_cec_unset_edid(aconnector);
4009 if (aconnector->dc_link->aux_mode) {
4010 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
4011 }
4012 } else {
4013 const struct edid *edid = (const struct edid *)sink->dc_edid.raw_edid;
4014
4015 aconnector->drm_edid = drm_edid_alloc(edid, sink->dc_edid.length);
4016 drm_edid_connector_update(connector, aconnector->drm_edid);
4017
4018 hdmi_cec_set_edid(aconnector);
4019 if (aconnector->dc_link->aux_mode)
4020 drm_dp_cec_attach(&aconnector->dm_dp_aux.aux,
4021 connector->display_info.source_physical_address);
4022 }
4023
4024 if (!aconnector->timing_requested) {
4025 aconnector->timing_requested =
4026 kzalloc_obj(struct dc_crtc_timing);
4027 if (!aconnector->timing_requested)
4028 drm_err(dev,
4029 "failed to create aconnector->requested_timing\n");
4030 }
4031
4032 amdgpu_dm_update_freesync_caps(connector, aconnector->drm_edid);
4033 update_connector_ext_caps(aconnector);
4034 } else {
4035 hdmi_cec_unset_edid(aconnector);
4036 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
4037 amdgpu_dm_update_freesync_caps(connector, NULL);
4038 aconnector->num_modes = 0;
4039 dc_sink_release(aconnector->dc_sink);
4040 aconnector->dc_sink = NULL;
4041 drm_edid_free(aconnector->drm_edid);
4042 aconnector->drm_edid = NULL;
4043 kfree(aconnector->timing_requested);
4044 aconnector->timing_requested = NULL;
4045 /* Set CP to DESIRED if it was ENABLED, so we can re-enable it again on hotplug */
4046 if (connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
4047 connector->state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
4048 }
4049
4050 update_subconnector_property(aconnector);
4051
4052 /* When polling, the mutex will be unlocked for us by DRM. */
4053 if (!drm_kms_helper_is_poll_worker())
4054 mutex_unlock(&dev->mode_config.mutex);
4055}
4056
4057static bool are_sinks_equal(const struct dc_sink *sink1, const struct dc_sink *sink2)
4058{
4059 if (!sink1 || !sink2)
4060 return false;
4061 if (sink1->sink_signal != sink2->sink_signal)
4062 return false;
4063
4064 if (sink1->dc_edid.length != sink2->dc_edid.length)
4065 return false;
4066
4067 if (memcmp(sink1->dc_edid.raw_edid, sink2->dc_edid.raw_edid,
4068 sink1->dc_edid.length) != 0)
4069 return false;
4070 return true;
4071}
4072
4073
4074/**
4075 * DOC: hdmi_hpd_debounce_work
4076 *
4077 * HDMI HPD debounce delay in milliseconds. When an HDMI display toggles HPD
4078 * (such as during power save transitions), this delay determines how long to
4079 * wait before processing the HPD event. This allows distinguishing between a
4080 * physical unplug (>hdmi_hpd_debounce_delay)
4081 * and a spontaneous RX HPD toggle (<hdmi_hpd_debounce_delay).
4082 *
4083 * If the toggle is less than this delay, the driver compares sink capabilities
4084 * and permits a hotplug event if they changed.
4085 *
4086 * The default value of 1500ms was chosen based on experimental testing with
4087 * various monitors that exhibit spontaneous HPD toggling behavior.
4088 */
4089static void hdmi_hpd_debounce_work(struct work_struct *work)
4090{
4091 struct amdgpu_dm_connector *aconnector =
4092 container_of(to_delayed_work(work), struct amdgpu_dm_connector,
4093 hdmi_hpd_debounce_work);
4094 struct drm_connector *connector = &aconnector->base;
4095 struct drm_device *dev = connector->dev;
4096 struct amdgpu_device *adev = drm_to_adev(dev);
4097 struct dc *dc = aconnector->dc_link->ctx->dc;
4098 bool fake_reconnect = false;
4099 bool reallow_idle = false;
4100 bool ret = false;
4101 guard(mutex)(&aconnector->hpd_lock);
4102
4103 /* Re-detect the display */
4104 scoped_guard(mutex, &adev->dm.dc_lock) {
4105 if (dc->caps.ips_support && dc->ctx->dmub_srv->idle_allowed) {
4106 dc_allow_idle_optimizations(dc, false);
4107 reallow_idle = true;
4108 }
4109 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
4110 }
4111
4112 if (ret) {
4113 /* Apply workaround delay for certain panels */
4114 apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink);
4115 /* Compare sinks to determine if this was a spontaneous HPD toggle */
4116 if (are_sinks_equal(aconnector->dc_link->local_sink, aconnector->hdmi_prev_sink)) {
4117 /*
4118 * Sinks match - this was a spontaneous HDMI HPD toggle.
4119 */
4120 drm_dbg_kms(dev, "HDMI HPD: Sink unchanged after debounce, internal re-enable\n");
4121 fake_reconnect = true;
4122 }
4123
4124 /* Update connector state */
4125 amdgpu_dm_update_connector_after_detect(aconnector);
4126
4127 drm_modeset_lock_all(dev);
4128 dm_restore_drm_connector_state(dev, connector);
4129 drm_modeset_unlock_all(dev);
4130
4131 /* Only notify OS if sink actually changed */
4132 if (!fake_reconnect && aconnector->base.force == DRM_FORCE_UNSPECIFIED)
4133 drm_kms_helper_hotplug_event(dev);
4134 }
4135
4136 /* Release the cached sink reference */
4137 if (aconnector->hdmi_prev_sink) {
4138 dc_sink_release(aconnector->hdmi_prev_sink);
4139 aconnector->hdmi_prev_sink = NULL;
4140 }
4141
4142 scoped_guard(mutex, &adev->dm.dc_lock) {
4143 if (reallow_idle && dc->caps.ips_support)
4144 dc_allow_idle_optimizations(dc, true);
4145 }
4146}
4147
4148static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector)
4149{
4150 struct drm_connector *connector = &aconnector->base;
4151 struct drm_device *dev = connector->dev;
4152 enum dc_connection_type new_connection_type = dc_connection_none;
4153 struct amdgpu_device *adev = drm_to_adev(dev);
4154 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
4155 struct dc *dc = aconnector->dc_link->ctx->dc;
4156 bool ret = false;
4157 bool debounce_required = false;
4158
4159 if (adev->dm.disable_hpd_irq)
4160 return;
4161
4162 /*
4163 * In case of failure or MST no need to update connector status or notify the OS
4164 * since (for MST case) MST does this in its own context.
4165 */
4166 guard(mutex)(&aconnector->hpd_lock);
4167
4168 if (adev->dm.hdcp_workqueue) {
4169 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
4170 dm_con_state->update_hdcp = true;
4171 }
4172 if (aconnector->fake_enable)
4173 aconnector->fake_enable = false;
4174
4175 aconnector->timing_changed = false;
4176
4177 if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type))
4178 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
4179
4180 /*
4181 * Check for HDMI disconnect with debounce enabled.
4182 */
4183 debounce_required = (aconnector->hdmi_hpd_debounce_delay_ms > 0 &&
4184 dc_is_hdmi_signal(aconnector->dc_link->connector_signal) &&
4185 new_connection_type == dc_connection_none &&
4186 aconnector->dc_link->local_sink != NULL);
4187
4188 if (aconnector->base.force && new_connection_type == dc_connection_none) {
4189 emulated_link_detect(aconnector->dc_link);
4190
4191 drm_modeset_lock_all(dev);
4192 dm_restore_drm_connector_state(dev, connector);
4193 drm_modeset_unlock_all(dev);
4194
4195 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
4196 drm_kms_helper_connector_hotplug_event(connector);
4197 } else if (debounce_required) {
4198 /*
4199 * HDMI disconnect detected - schedule delayed work instead of
4200 * processing immediately. This allows us to coalesce spurious
4201 * HDMI signals from physical unplugs.
4202 */
4203 drm_dbg_kms(dev, "HDMI HPD: Disconnect detected, scheduling debounce work (%u ms)\n",
4204 aconnector->hdmi_hpd_debounce_delay_ms);
4205
4206 /* Cache the current sink for later comparison */
4207 if (aconnector->hdmi_prev_sink)
4208 dc_sink_release(aconnector->hdmi_prev_sink);
4209 aconnector->hdmi_prev_sink = aconnector->dc_link->local_sink;
4210 if (aconnector->hdmi_prev_sink)
4211 dc_sink_retain(aconnector->hdmi_prev_sink);
4212
4213 /* Schedule delayed detection. */
4214 if (mod_delayed_work(system_wq,
4215 &aconnector->hdmi_hpd_debounce_work,
4216 msecs_to_jiffies(aconnector->hdmi_hpd_debounce_delay_ms)))
4217 drm_dbg_kms(dev, "HDMI HPD: Re-scheduled debounce work\n");
4218
4219 } else {
4220
4221 /* If the aconnector->hdmi_hpd_debounce_work is scheduled, exit early */
4222 if (delayed_work_pending(&aconnector->hdmi_hpd_debounce_work))
4223 return;
4224
4225 scoped_guard(mutex, &adev->dm.dc_lock) {
4226 dc_exit_ips_for_hw_access(dc);
4227 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
4228 }
4229 if (ret) {
4230 /* w/a delay for certain panels */
4231 apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink);
4232 amdgpu_dm_update_connector_after_detect(aconnector);
4233
4234 drm_modeset_lock_all(dev);
4235 dm_restore_drm_connector_state(dev, connector);
4236 drm_modeset_unlock_all(dev);
4237
4238 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
4239 drm_kms_helper_connector_hotplug_event(connector);
4240 }
4241 }
4242}
4243
4244static void handle_hpd_irq(void *param)
4245{
4246 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
4247
4248 handle_hpd_irq_helper(aconnector);
4249
4250}
4251
4252static void schedule_hpd_rx_offload_work(struct amdgpu_device *adev, struct hpd_rx_irq_offload_work_queue *offload_wq,
4253 union hpd_irq_data hpd_irq_data)
4254{
4255 struct hpd_rx_irq_offload_work *offload_work = kzalloc_obj(*offload_work);
4256
4257 if (!offload_work) {
4258 drm_err(adev_to_drm(adev), "Failed to allocate hpd_rx_irq_offload_work.\n");
4259 return;
4260 }
4261
4262 INIT_WORK(&offload_work->work, dm_handle_hpd_rx_offload_work);
4263 offload_work->data = hpd_irq_data;
4264 offload_work->offload_wq = offload_wq;
4265 offload_work->adev = adev;
4266
4267 queue_work(offload_wq->wq, &offload_work->work);
4268 drm_dbg_kms(adev_to_drm(adev), "queue work to handle hpd_rx offload work");
4269}
4270
4271static void handle_hpd_rx_irq(void *param)
4272{
4273 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
4274 struct drm_connector *connector = &aconnector->base;
4275 struct drm_device *dev = connector->dev;
4276 struct dc_link *dc_link = aconnector->dc_link;
4277 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
4278 bool result = false;
4279 enum dc_connection_type new_connection_type = dc_connection_none;
4280 struct amdgpu_device *adev = drm_to_adev(dev);
4281 union hpd_irq_data hpd_irq_data;
4282 bool link_loss = false;
4283 bool has_left_work = false;
4284 int idx = dc_link->link_index;
4285 struct hpd_rx_irq_offload_work_queue *offload_wq = &adev->dm.hpd_rx_offload_wq[idx];
4286 struct dc *dc = aconnector->dc_link->ctx->dc;
4287
4288 memset(&hpd_irq_data, 0, sizeof(hpd_irq_data));
4289
4290 if (adev->dm.disable_hpd_irq)
4291 return;
4292
4293 /*
4294 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio
4295 * conflict, after implement i2c helper, this mutex should be
4296 * retired.
4297 */
4298 mutex_lock(&aconnector->hpd_lock);
4299
4300 result = dc_link_handle_hpd_rx_irq(dc_link, &hpd_irq_data,
4301 &link_loss, true, &has_left_work);
4302
4303 if (!has_left_work)
4304 goto out;
4305
4306 if (hpd_irq_data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
4307 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
4308 goto out;
4309 }
4310
4311 if (dc_link_dp_allow_hpd_rx_irq(dc_link)) {
4312 if (hpd_irq_data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY ||
4313 hpd_irq_data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) {
4314 bool skip = false;
4315
4316 /*
4317 * DOWN_REP_MSG_RDY is also handled by polling method
4318 * mgr->cbs->poll_hpd_irq()
4319 */
4320 spin_lock(&offload_wq->offload_lock);
4321 skip = offload_wq->is_handling_mst_msg_rdy_event;
4322
4323 if (!skip)
4324 offload_wq->is_handling_mst_msg_rdy_event = true;
4325
4326 spin_unlock(&offload_wq->offload_lock);
4327
4328 if (!skip)
4329 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
4330
4331 goto out;
4332 }
4333
4334 if (link_loss) {
4335 bool skip = false;
4336
4337 spin_lock(&offload_wq->offload_lock);
4338 skip = offload_wq->is_handling_link_loss;
4339
4340 if (!skip)
4341 offload_wq->is_handling_link_loss = true;
4342
4343 spin_unlock(&offload_wq->offload_lock);
4344
4345 if (!skip)
4346 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
4347
4348 goto out;
4349 }
4350 }
4351
4352out:
4353 if (result && !is_mst_root_connector) {
4354 /* Downstream Port status changed. */
4355 if (!dc_link_detect_connection_type(dc_link, &new_connection_type))
4356 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
4357
4358 if (aconnector->base.force && new_connection_type == dc_connection_none) {
4359 emulated_link_detect(dc_link);
4360
4361 if (aconnector->fake_enable)
4362 aconnector->fake_enable = false;
4363
4364 amdgpu_dm_update_connector_after_detect(aconnector);
4365
4366
4367 drm_modeset_lock_all(dev);
4368 dm_restore_drm_connector_state(dev, connector);
4369 drm_modeset_unlock_all(dev);
4370
4371 drm_kms_helper_connector_hotplug_event(connector);
4372 } else {
4373 bool ret = false;
4374
4375 mutex_lock(&adev->dm.dc_lock);
4376 dc_exit_ips_for_hw_access(dc);
4377 ret = dc_link_detect(dc_link, DETECT_REASON_HPDRX);
4378 mutex_unlock(&adev->dm.dc_lock);
4379
4380 if (ret) {
4381 if (aconnector->fake_enable)
4382 aconnector->fake_enable = false;
4383
4384 amdgpu_dm_update_connector_after_detect(aconnector);
4385
4386 drm_modeset_lock_all(dev);
4387 dm_restore_drm_connector_state(dev, connector);
4388 drm_modeset_unlock_all(dev);
4389
4390 drm_kms_helper_connector_hotplug_event(connector);
4391 }
4392 }
4393 }
4394 if (hpd_irq_data.bytes.device_service_irq.bits.CP_IRQ) {
4395 if (adev->dm.hdcp_workqueue)
4396 hdcp_handle_cpirq(adev->dm.hdcp_workqueue, aconnector->base.index);
4397 }
4398
4399 if (dc_link->type != dc_connection_mst_branch)
4400 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux);
4401
4402 mutex_unlock(&aconnector->hpd_lock);
4403}
4404
4405static int register_hpd_handlers(struct amdgpu_device *adev)
4406{
4407 struct drm_device *dev = adev_to_drm(adev);
4408 struct drm_connector *connector;
4409 struct amdgpu_dm_connector *aconnector;
4410 const struct dc_link *dc_link;
4411 struct dc_interrupt_params int_params = {0};
4412
4413 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4414 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4415
4416 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
4417 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD,
4418 dmub_hpd_callback, true)) {
4419 drm_err(adev_to_drm(adev), "fail to register dmub hpd callback");
4420 return -EINVAL;
4421 }
4422
4423 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_IRQ,
4424 dmub_hpd_callback, true)) {
4425 drm_err(adev_to_drm(adev), "fail to register dmub hpd callback");
4426 return -EINVAL;
4427 }
4428
4429 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_SENSE_NOTIFY,
4430 dmub_hpd_sense_callback, true)) {
4431 drm_err(adev_to_drm(adev), "fail to register dmub hpd sense callback");
4432 return -EINVAL;
4433 }
4434 }
4435
4436 list_for_each_entry(connector,
4437 &dev->mode_config.connector_list, head) {
4438
4439 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
4440 continue;
4441
4442 aconnector = to_amdgpu_dm_connector(connector);
4443 dc_link = aconnector->dc_link;
4444
4445 if (dc_link->irq_source_hpd != DC_IRQ_SOURCE_INVALID) {
4446 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
4447 int_params.irq_source = dc_link->irq_source_hpd;
4448
4449 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4450 int_params.irq_source < DC_IRQ_SOURCE_HPD1 ||
4451 int_params.irq_source > DC_IRQ_SOURCE_HPD6) {
4452 drm_err(adev_to_drm(adev), "Failed to register hpd irq!\n");
4453 return -EINVAL;
4454 }
4455
4456 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4457 handle_hpd_irq, (void *) aconnector))
4458 return -ENOMEM;
4459 }
4460
4461 if (dc_link->irq_source_hpd_rx != DC_IRQ_SOURCE_INVALID) {
4462
4463 /* Also register for DP short pulse (hpd_rx). */
4464 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
4465 int_params.irq_source = dc_link->irq_source_hpd_rx;
4466
4467 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4468 int_params.irq_source < DC_IRQ_SOURCE_HPD1RX ||
4469 int_params.irq_source > DC_IRQ_SOURCE_HPD6RX) {
4470 drm_err(adev_to_drm(adev), "Failed to register hpd rx irq!\n");
4471 return -EINVAL;
4472 }
4473
4474 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4475 handle_hpd_rx_irq, (void *) aconnector))
4476 return -ENOMEM;
4477 }
4478 }
4479 return 0;
4480}
4481
4482/* Register IRQ sources and initialize IRQ callbacks */
4483static int dce110_register_irq_handlers(struct amdgpu_device *adev)
4484{
4485 struct dc *dc = adev->dm.dc;
4486 struct common_irq_params *c_irq_params;
4487 struct dc_interrupt_params int_params = {0};
4488 int r;
4489 int i;
4490 unsigned int src_id;
4491 unsigned int client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
4492 /* Use different interrupts for VBLANK on DCE 6 vs. newer. */
4493 const unsigned int vblank_d1 =
4494 adev->dm.dc->ctx->dce_version >= DCE_VERSION_8_0
4495 ? VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0 : 1;
4496
4497 if (adev->family >= AMDGPU_FAMILY_AI)
4498 client_id = SOC15_IH_CLIENTID_DCE;
4499
4500 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4501 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4502
4503 /*
4504 * Actions of amdgpu_irq_add_id():
4505 * 1. Register a set() function with base driver.
4506 * Base driver will call set() function to enable/disable an
4507 * interrupt in DC hardware.
4508 * 2. Register amdgpu_dm_irq_handler().
4509 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
4510 * coming from DC hardware.
4511 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
4512 * for acknowledging and handling.
4513 */
4514
4515 /* Use VBLANK interrupt */
4516 for (i = 0; i < adev->mode_info.num_crtc; i++) {
4517 src_id = vblank_d1 + i;
4518 r = amdgpu_irq_add_id(adev, client_id, src_id, &adev->crtc_irq);
4519 if (r) {
4520 drm_err(adev_to_drm(adev), "Failed to add crtc irq id!\n");
4521 return r;
4522 }
4523
4524 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4525 int_params.irq_source =
4526 dc_interrupt_to_irq_source(dc, src_id, 0);
4527
4528 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4529 int_params.irq_source < DC_IRQ_SOURCE_VBLANK1 ||
4530 int_params.irq_source > DC_IRQ_SOURCE_VBLANK6) {
4531 drm_err(adev_to_drm(adev), "Failed to register vblank irq!\n");
4532 return -EINVAL;
4533 }
4534
4535 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
4536
4537 c_irq_params->adev = adev;
4538 c_irq_params->irq_src = int_params.irq_source;
4539
4540 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4541 dm_crtc_high_irq, c_irq_params))
4542 return -ENOMEM;
4543 }
4544
4545 if (dc_supports_vrr(adev->dm.dc->ctx->dce_version)) {
4546 /* Use VUPDATE interrupt */
4547 for (i = 0; i < adev->mode_info.num_crtc; i++) {
4548 src_id = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT + i * 2;
4549 r = amdgpu_irq_add_id(adev, client_id, src_id, &adev->vupdate_irq);
4550 if (r) {
4551 drm_err(adev_to_drm(adev), "Failed to add vupdate irq id!\n");
4552 return r;
4553 }
4554
4555 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4556 int_params.irq_source =
4557 dc_interrupt_to_irq_source(dc, src_id, 0);
4558
4559 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4560 int_params.irq_source < DC_IRQ_SOURCE_VUPDATE1 ||
4561 int_params.irq_source > DC_IRQ_SOURCE_VUPDATE6) {
4562 drm_err(adev_to_drm(adev), "Failed to register vupdate irq!\n");
4563 return -EINVAL;
4564 }
4565
4566 c_irq_params = &adev->dm.vupdate_params[
4567 int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
4568 c_irq_params->adev = adev;
4569 c_irq_params->irq_src = int_params.irq_source;
4570
4571 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4572 dm_vupdate_high_irq, c_irq_params))
4573 return -ENOMEM;
4574 }
4575 }
4576
4577 /* Use GRPH_PFLIP interrupt */
4578 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
4579 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
4580 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
4581 if (r) {
4582 drm_err(adev_to_drm(adev), "Failed to add page flip irq id!\n");
4583 return r;
4584 }
4585
4586 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4587 int_params.irq_source =
4588 dc_interrupt_to_irq_source(dc, i, 0);
4589
4590 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4591 int_params.irq_source < DC_IRQ_SOURCE_PFLIP_FIRST ||
4592 int_params.irq_source > DC_IRQ_SOURCE_PFLIP_LAST) {
4593 drm_err(adev_to_drm(adev), "Failed to register pflip irq!\n");
4594 return -EINVAL;
4595 }
4596
4597 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
4598
4599 c_irq_params->adev = adev;
4600 c_irq_params->irq_src = int_params.irq_source;
4601
4602 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4603 dm_pflip_high_irq, c_irq_params))
4604 return -ENOMEM;
4605 }
4606
4607 /* HPD */
4608 r = amdgpu_irq_add_id(adev, client_id,
4609 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
4610 if (r) {
4611 drm_err(adev_to_drm(adev), "Failed to add hpd irq id!\n");
4612 return r;
4613 }
4614
4615 r = register_hpd_handlers(adev);
4616
4617 return r;
4618}
4619
4620/* Register IRQ sources and initialize IRQ callbacks */
4621static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
4622{
4623 struct dc *dc = adev->dm.dc;
4624 struct common_irq_params *c_irq_params;
4625 struct dc_interrupt_params int_params = {0};
4626 int r;
4627 int i;
4628#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
4629 static const unsigned int vrtl_int_srcid[] = {
4630 DCN_1_0__SRCID__OTG1_VERTICAL_INTERRUPT0_CONTROL,
4631 DCN_1_0__SRCID__OTG2_VERTICAL_INTERRUPT0_CONTROL,
4632 DCN_1_0__SRCID__OTG3_VERTICAL_INTERRUPT0_CONTROL,
4633 DCN_1_0__SRCID__OTG4_VERTICAL_INTERRUPT0_CONTROL,
4634 DCN_1_0__SRCID__OTG5_VERTICAL_INTERRUPT0_CONTROL,
4635 DCN_1_0__SRCID__OTG6_VERTICAL_INTERRUPT0_CONTROL
4636 };
4637#endif
4638
4639 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4640 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4641
4642 /*
4643 * Actions of amdgpu_irq_add_id():
4644 * 1. Register a set() function with base driver.
4645 * Base driver will call set() function to enable/disable an
4646 * interrupt in DC hardware.
4647 * 2. Register amdgpu_dm_irq_handler().
4648 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
4649 * coming from DC hardware.
4650 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
4651 * for acknowledging and handling.
4652 */
4653
4654 /* Use VSTARTUP interrupt */
4655 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
4656 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
4657 i++) {
4658 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
4659
4660 if (r) {
4661 drm_err(adev_to_drm(adev), "Failed to add crtc irq id!\n");
4662 return r;
4663 }
4664
4665 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4666 int_params.irq_source =
4667 dc_interrupt_to_irq_source(dc, i, 0);
4668
4669 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4670 int_params.irq_source < DC_IRQ_SOURCE_VBLANK1 ||
4671 int_params.irq_source > DC_IRQ_SOURCE_VBLANK6) {
4672 drm_err(adev_to_drm(adev), "Failed to register vblank irq!\n");
4673 return -EINVAL;
4674 }
4675
4676 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
4677
4678 c_irq_params->adev = adev;
4679 c_irq_params->irq_src = int_params.irq_source;
4680
4681 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4682 dm_crtc_high_irq, c_irq_params))
4683 return -ENOMEM;
4684 }
4685
4686 /* Use otg vertical line interrupt */
4687#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
4688 for (i = 0; i <= adev->mode_info.num_crtc - 1; i++) {
4689 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE,
4690 vrtl_int_srcid[i], &adev->vline0_irq);
4691
4692 if (r) {
4693 drm_err(adev_to_drm(adev), "Failed to add vline0 irq id!\n");
4694 return r;
4695 }
4696
4697 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4698 int_params.irq_source =
4699 dc_interrupt_to_irq_source(dc, vrtl_int_srcid[i], 0);
4700
4701 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4702 int_params.irq_source < DC_IRQ_SOURCE_DC1_VLINE0 ||
4703 int_params.irq_source > DC_IRQ_SOURCE_DC6_VLINE0) {
4704 drm_err(adev_to_drm(adev), "Failed to register vline0 irq!\n");
4705 return -EINVAL;
4706 }
4707
4708 c_irq_params = &adev->dm.vline0_params[int_params.irq_source
4709 - DC_IRQ_SOURCE_DC1_VLINE0];
4710
4711 c_irq_params->adev = adev;
4712 c_irq_params->irq_src = int_params.irq_source;
4713
4714 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4715 dm_dcn_vertical_interrupt0_high_irq,
4716 c_irq_params))
4717 return -ENOMEM;
4718 }
4719#endif
4720
4721 /* Use VUPDATE_NO_LOCK interrupt on DCN, which seems to correspond to
4722 * the regular VUPDATE interrupt on DCE. We want DC_IRQ_SOURCE_VUPDATEx
4723 * to trigger at end of each vblank, regardless of state of the lock,
4724 * matching DCE behaviour.
4725 */
4726 for (i = DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT;
4727 i <= DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT + adev->mode_info.num_crtc - 1;
4728 i++) {
4729 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->vupdate_irq);
4730
4731 if (r) {
4732 drm_err(adev_to_drm(adev), "Failed to add vupdate irq id!\n");
4733 return r;
4734 }
4735
4736 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4737 int_params.irq_source =
4738 dc_interrupt_to_irq_source(dc, i, 0);
4739
4740 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4741 int_params.irq_source < DC_IRQ_SOURCE_VUPDATE1 ||
4742 int_params.irq_source > DC_IRQ_SOURCE_VUPDATE6) {
4743 drm_err(adev_to_drm(adev), "Failed to register vupdate irq!\n");
4744 return -EINVAL;
4745 }
4746
4747 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
4748
4749 c_irq_params->adev = adev;
4750 c_irq_params->irq_src = int_params.irq_source;
4751
4752 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4753 dm_vupdate_high_irq, c_irq_params))
4754 return -ENOMEM;
4755 }
4756
4757 /* Use GRPH_PFLIP interrupt */
4758 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
4759 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + dc->caps.max_otg_num - 1;
4760 i++) {
4761 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
4762 if (r) {
4763 drm_err(adev_to_drm(adev), "Failed to add page flip irq id!\n");
4764 return r;
4765 }
4766
4767 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4768 int_params.irq_source =
4769 dc_interrupt_to_irq_source(dc, i, 0);
4770
4771 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4772 int_params.irq_source < DC_IRQ_SOURCE_PFLIP_FIRST ||
4773 int_params.irq_source > DC_IRQ_SOURCE_PFLIP_LAST) {
4774 drm_err(adev_to_drm(adev), "Failed to register pflip irq!\n");
4775 return -EINVAL;
4776 }
4777
4778 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
4779
4780 c_irq_params->adev = adev;
4781 c_irq_params->irq_src = int_params.irq_source;
4782
4783 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4784 dm_pflip_high_irq, c_irq_params))
4785 return -ENOMEM;
4786 }
4787
4788 /* HPD */
4789 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
4790 &adev->hpd_irq);
4791 if (r) {
4792 drm_err(adev_to_drm(adev), "Failed to add hpd irq id!\n");
4793 return r;
4794 }
4795
4796 r = register_hpd_handlers(adev);
4797
4798 return r;
4799}
4800/* Register Outbox IRQ sources and initialize IRQ callbacks */
4801static int register_outbox_irq_handlers(struct amdgpu_device *adev)
4802{
4803 struct dc *dc = adev->dm.dc;
4804 struct common_irq_params *c_irq_params;
4805 struct dc_interrupt_params int_params = {0};
4806 int r, i;
4807
4808 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4809 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4810
4811 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT,
4812 &adev->dmub_outbox_irq);
4813 if (r) {
4814 drm_err(adev_to_drm(adev), "Failed to add outbox irq id!\n");
4815 return r;
4816 }
4817
4818 if (dc->ctx->dmub_srv) {
4819 i = DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT;
4820 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
4821 int_params.irq_source =
4822 dc_interrupt_to_irq_source(dc, i, 0);
4823
4824 c_irq_params = &adev->dm.dmub_outbox_params[0];
4825
4826 c_irq_params->adev = adev;
4827 c_irq_params->irq_src = int_params.irq_source;
4828
4829 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4830 dm_dmub_outbox1_low_irq, c_irq_params))
4831 return -ENOMEM;
4832 }
4833
4834 return 0;
4835}
4836
4837/*
4838 * Acquires the lock for the atomic state object and returns
4839 * the new atomic state.
4840 *
4841 * This should only be called during atomic check.
4842 */
4843int dm_atomic_get_state(struct drm_atomic_state *state,
4844 struct dm_atomic_state **dm_state)
4845{
4846 struct drm_device *dev = state->dev;
4847 struct amdgpu_device *adev = drm_to_adev(dev);
4848 struct amdgpu_display_manager *dm = &adev->dm;
4849 struct drm_private_state *priv_state;
4850
4851 if (*dm_state)
4852 return 0;
4853
4854 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj);
4855 if (IS_ERR(priv_state))
4856 return PTR_ERR(priv_state);
4857
4858 *dm_state = to_dm_atomic_state(priv_state);
4859
4860 return 0;
4861}
4862
4863static struct dm_atomic_state *
4864dm_atomic_get_new_state(struct drm_atomic_state *state)
4865{
4866 struct drm_device *dev = state->dev;
4867 struct amdgpu_device *adev = drm_to_adev(dev);
4868 struct amdgpu_display_manager *dm = &adev->dm;
4869 struct drm_private_obj *obj;
4870 struct drm_private_state *new_obj_state;
4871 int i;
4872
4873 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) {
4874 if (obj->funcs == dm->atomic_obj.funcs)
4875 return to_dm_atomic_state(new_obj_state);
4876 }
4877
4878 return NULL;
4879}
4880
4881static struct drm_private_state *
4882dm_atomic_duplicate_state(struct drm_private_obj *obj)
4883{
4884 struct dm_atomic_state *old_state, *new_state;
4885
4886 new_state = kzalloc_obj(*new_state);
4887 if (!new_state)
4888 return NULL;
4889
4890 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base);
4891
4892 old_state = to_dm_atomic_state(obj->state);
4893
4894 if (old_state && old_state->context)
4895 new_state->context = dc_state_create_copy(old_state->context);
4896
4897 if (!new_state->context) {
4898 kfree(new_state);
4899 return NULL;
4900 }
4901
4902 return &new_state->base;
4903}
4904
4905static void dm_atomic_destroy_state(struct drm_private_obj *obj,
4906 struct drm_private_state *state)
4907{
4908 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
4909
4910 if (dm_state && dm_state->context)
4911 dc_state_release(dm_state->context);
4912
4913 kfree(dm_state);
4914}
4915
4916static struct drm_private_state *
4917dm_atomic_create_state(struct drm_private_obj *obj)
4918{
4919 struct amdgpu_device *adev = drm_to_adev(obj->dev);
4920 struct dm_atomic_state *dm_state;
4921 struct dc_state *context;
4922
4923 dm_state = kzalloc_obj(*dm_state);
4924 if (!dm_state)
4925 return ERR_PTR(-ENOMEM);
4926
4927 context = dc_state_create_current_copy(adev->dm.dc);
4928 if (!context) {
4929 kfree(dm_state);
4930 return ERR_PTR(-ENOMEM);
4931 }
4932
4933 __drm_atomic_helper_private_obj_create_state(obj, &dm_state->base);
4934 dm_state->context = context;
4935
4936 return &dm_state->base;
4937}
4938
4939static struct drm_private_state_funcs dm_atomic_state_funcs = {
4940 .atomic_create_state = dm_atomic_create_state,
4941 .atomic_duplicate_state = dm_atomic_duplicate_state,
4942 .atomic_destroy_state = dm_atomic_destroy_state,
4943};
4944
4945static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
4946{
4947 int r;
4948
4949 adev->mode_info.mode_config_initialized = true;
4950
4951 adev_to_drm(adev)->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
4952 adev_to_drm(adev)->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
4953
4954 adev_to_drm(adev)->mode_config.max_width = 16384;
4955 adev_to_drm(adev)->mode_config.max_height = 16384;
4956
4957 adev_to_drm(adev)->mode_config.preferred_depth = 24;
4958 if (adev->asic_type == CHIP_HAWAII)
4959 /* disable prefer shadow for now due to hibernation issues */
4960 adev_to_drm(adev)->mode_config.prefer_shadow = 0;
4961 else
4962 adev_to_drm(adev)->mode_config.prefer_shadow = 1;
4963 /* indicates support for immediate flip */
4964 adev_to_drm(adev)->mode_config.async_page_flip = true;
4965
4966 drm_atomic_private_obj_init(adev_to_drm(adev),
4967 &adev->dm.atomic_obj,
4968 &dm_atomic_state_funcs);
4969
4970 r = amdgpu_display_modeset_create_props(adev);
4971 if (r)
4972 return r;
4973
4974#ifdef AMD_PRIVATE_COLOR
4975 if (amdgpu_dm_create_color_properties(adev))
4976 return -ENOMEM;
4977#endif
4978
4979 r = amdgpu_dm_audio_init(adev);
4980 if (r)
4981 return r;
4982
4983 return 0;
4984}
4985
4986#define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12
4987#define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255
4988#define AMDGPU_DM_MIN_SPREAD ((AMDGPU_DM_DEFAULT_MAX_BACKLIGHT - AMDGPU_DM_DEFAULT_MIN_BACKLIGHT) / 2)
4989#define AUX_BL_DEFAULT_TRANSITION_TIME_MS 50
4990
4991static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm,
4992 int bl_idx)
4993{
4994 struct amdgpu_dm_backlight_caps *caps = &dm->backlight_caps[bl_idx];
4995
4996 if (caps->caps_valid)
4997 return;
4998
4999#if defined(CONFIG_ACPI)
5000 amdgpu_acpi_get_backlight_caps(caps);
5001
5002 /* validate the firmware value is sane */
5003 if (caps->caps_valid) {
5004 int spread = caps->max_input_signal - caps->min_input_signal;
5005
5006 if (caps->max_input_signal > AMDGPU_DM_DEFAULT_MAX_BACKLIGHT ||
5007 caps->min_input_signal < 0 ||
5008 spread > AMDGPU_DM_DEFAULT_MAX_BACKLIGHT ||
5009 spread < AMDGPU_DM_MIN_SPREAD) {
5010 drm_dbg_kms(adev_to_drm(dm->adev), "DM: Invalid backlight caps: min=%d, max=%d\n",
5011 caps->min_input_signal, caps->max_input_signal);
5012 caps->caps_valid = false;
5013 }
5014 }
5015
5016 if (!caps->caps_valid) {
5017 caps->min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
5018 caps->max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
5019 caps->caps_valid = true;
5020 }
5021#else
5022 if (caps->aux_support)
5023 return;
5024
5025 caps->min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
5026 caps->max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
5027 caps->caps_valid = true;
5028#endif
5029}
5030
5031static int get_brightness_range(const struct amdgpu_dm_backlight_caps *caps,
5032 unsigned int *min, unsigned int *max)
5033{
5034 if (!caps)
5035 return 0;
5036
5037 if (caps->aux_support) {
5038 // Firmware limits are in nits, DC API wants millinits.
5039 *max = 1000 * caps->aux_max_input_signal;
5040 *min = 1000 * caps->aux_min_input_signal;
5041 } else {
5042 // Firmware limits are 8-bit, PWM control is 16-bit.
5043 *max = 0x101 * caps->max_input_signal;
5044 *min = 0x101 * caps->min_input_signal;
5045 }
5046 return 1;
5047}
5048
5049/* Rescale from [min..max] to [0..AMDGPU_MAX_BL_LEVEL] */
5050static inline u32 scale_input_to_fw(int min, int max, u64 input)
5051{
5052 return DIV_ROUND_CLOSEST_ULL(input * AMDGPU_MAX_BL_LEVEL, max - min);
5053}
5054
5055/* Rescale from [0..AMDGPU_MAX_BL_LEVEL] to [min..max] */
5056static inline u32 scale_fw_to_input(int min, int max, u64 input)
5057{
5058 return min + DIV_ROUND_CLOSEST_ULL(input * (max - min), AMDGPU_MAX_BL_LEVEL);
5059}
5060
5061static void convert_custom_brightness(const struct amdgpu_dm_backlight_caps *caps,
5062 unsigned int min, unsigned int max,
5063 uint32_t *user_brightness)
5064{
5065 u32 brightness = scale_input_to_fw(min, max, *user_brightness);
5066 u8 lower_signal, upper_signal, upper_lum, lower_lum, lum;
5067 int left, right;
5068
5069 if (amdgpu_dc_debug_mask & DC_DISABLE_CUSTOM_BRIGHTNESS_CURVE)
5070 return;
5071
5072 if (!caps->data_points)
5073 return;
5074
5075 /*
5076 * Handle the case where brightness is below the first data point
5077 * Interpolate between (0,0) and (first_signal, first_lum)
5078 */
5079 if (brightness < caps->luminance_data[0].input_signal) {
5080 lum = DIV_ROUND_CLOSEST(caps->luminance_data[0].luminance * brightness,
5081 caps->luminance_data[0].input_signal);
5082 goto scale;
5083 }
5084
5085 left = 0;
5086 right = caps->data_points - 1;
5087 while (left <= right) {
5088 int mid = left + (right - left) / 2;
5089 u8 signal = caps->luminance_data[mid].input_signal;
5090
5091 /* Exact match found */
5092 if (signal == brightness) {
5093 lum = caps->luminance_data[mid].luminance;
5094 goto scale;
5095 }
5096
5097 if (signal < brightness)
5098 left = mid + 1;
5099 else
5100 right = mid - 1;
5101 }
5102
5103 /* verify bound */
5104 if (left >= caps->data_points)
5105 left = caps->data_points - 1;
5106
5107 /* At this point, left > right */
5108 lower_signal = caps->luminance_data[right].input_signal;
5109 upper_signal = caps->luminance_data[left].input_signal;
5110 lower_lum = caps->luminance_data[right].luminance;
5111 upper_lum = caps->luminance_data[left].luminance;
5112
5113 /* interpolate */
5114 if (right == left || !lower_lum)
5115 lum = upper_lum;
5116 else
5117 lum = lower_lum + DIV_ROUND_CLOSEST((upper_lum - lower_lum) *
5118 (brightness - lower_signal),
5119 upper_signal - lower_signal);
5120scale:
5121 *user_brightness = scale_fw_to_input(min, max,
5122 DIV_ROUND_CLOSEST(lum * brightness, 101));
5123}
5124
5125static u32 convert_brightness_from_user(const struct amdgpu_dm_backlight_caps *caps,
5126 uint32_t brightness)
5127{
5128 unsigned int min, max;
5129
5130 if (!get_brightness_range(caps, &min, &max))
5131 return brightness;
5132
5133 convert_custom_brightness(caps, min, max, &brightness);
5134
5135 // Rescale 0..max to min..max
5136 return min + DIV_ROUND_CLOSEST_ULL((u64)(max - min) * brightness, max);
5137}
5138
5139static u32 convert_brightness_to_user(const struct amdgpu_dm_backlight_caps *caps,
5140 uint32_t brightness)
5141{
5142 unsigned int min, max;
5143
5144 if (!get_brightness_range(caps, &min, &max))
5145 return brightness;
5146
5147 if (brightness < min)
5148 return 0;
5149 // Rescale min..max to 0..max
5150 return DIV_ROUND_CLOSEST_ULL((u64)max * (brightness - min),
5151 max - min);
5152}
5153
5154static void amdgpu_dm_backlight_set_level(struct amdgpu_display_manager *dm,
5155 int bl_idx,
5156 u32 user_brightness)
5157{
5158 struct amdgpu_dm_backlight_caps *caps;
5159 struct dc_link *link;
5160 u32 brightness;
5161 bool rc, reallow_idle = false;
5162 struct drm_connector *connector;
5163
5164 list_for_each_entry(connector, &dm->ddev->mode_config.connector_list, head) {
5165 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
5166
5167 if (aconnector->bl_idx != bl_idx)
5168 continue;
5169
5170 /* if connector is off, save the brightness for next time it's on */
5171 if (!aconnector->base.encoder) {
5172 dm->brightness[bl_idx] = user_brightness;
5173 dm->actual_brightness[bl_idx] = 0;
5174 return;
5175 }
5176 }
5177
5178 amdgpu_dm_update_backlight_caps(dm, bl_idx);
5179 caps = &dm->backlight_caps[bl_idx];
5180
5181 dm->brightness[bl_idx] = user_brightness;
5182 /* update scratch register */
5183 if (bl_idx == 0)
5184 amdgpu_atombios_scratch_regs_set_backlight_level(dm->adev, dm->brightness[bl_idx]);
5185 brightness = convert_brightness_from_user(caps, dm->brightness[bl_idx]);
5186 link = (struct dc_link *)dm->backlight_link[bl_idx];
5187
5188 /* Apply brightness quirk */
5189 if (caps->brightness_mask)
5190 brightness |= caps->brightness_mask;
5191
5192 /* Change brightness based on AUX property */
5193 mutex_lock(&dm->dc_lock);
5194 if (dm->dc->caps.ips_support && dm->dc->ctx->dmub_srv->idle_allowed) {
5195 dc_allow_idle_optimizations(dm->dc, false);
5196 reallow_idle = true;
5197 }
5198
5199 if (trace_amdgpu_dm_brightness_enabled()) {
5200 trace_amdgpu_dm_brightness(__builtin_return_address(0),
5201 user_brightness,
5202 brightness,
5203 caps->aux_support,
5204 power_supply_is_system_supplied() > 0);
5205 }
5206
5207 if (caps->aux_support) {
5208 rc = dc_link_set_backlight_level_nits(link, true, brightness,
5209 AUX_BL_DEFAULT_TRANSITION_TIME_MS);
5210 if (!rc)
5211 DRM_DEBUG("DM: Failed to update backlight via AUX on eDP[%d]\n", bl_idx);
5212 } else {
5213 struct set_backlight_level_params backlight_level_params = { 0 };
5214
5215 backlight_level_params.backlight_pwm_u16_16 = brightness;
5216 backlight_level_params.transition_time_in_ms = 0;
5217
5218 rc = dc_link_set_backlight_level(link, &backlight_level_params);
5219 if (!rc)
5220 DRM_DEBUG("DM: Failed to update backlight on eDP[%d]\n", bl_idx);
5221 }
5222
5223 if (dm->dc->caps.ips_support && reallow_idle)
5224 dc_allow_idle_optimizations(dm->dc, true);
5225
5226 mutex_unlock(&dm->dc_lock);
5227
5228 if (rc)
5229 dm->actual_brightness[bl_idx] = user_brightness;
5230}
5231
5232static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
5233{
5234 struct amdgpu_display_manager *dm = bl_get_data(bd);
5235 int i;
5236
5237 for (i = 0; i < dm->num_of_edps; i++) {
5238 if (bd == dm->backlight_dev[i])
5239 break;
5240 }
5241 if (i >= AMDGPU_DM_MAX_NUM_EDP)
5242 i = 0;
5243 amdgpu_dm_backlight_set_level(dm, i, bd->props.brightness);
5244
5245 return 0;
5246}
5247
5248static u32 amdgpu_dm_backlight_get_level(struct amdgpu_display_manager *dm,
5249 int bl_idx)
5250{
5251 int ret;
5252 struct amdgpu_dm_backlight_caps caps;
5253 struct dc_link *link = (struct dc_link *)dm->backlight_link[bl_idx];
5254
5255 amdgpu_dm_update_backlight_caps(dm, bl_idx);
5256 caps = dm->backlight_caps[bl_idx];
5257
5258 if (caps.aux_support) {
5259 u32 avg, peak;
5260
5261 if (!dc_link_get_backlight_level_nits(link, &avg, &peak))
5262 return dm->brightness[bl_idx];
5263 return convert_brightness_to_user(&caps, avg);
5264 }
5265
5266 ret = dc_link_get_backlight_level(link);
5267
5268 if (ret == DC_ERROR_UNEXPECTED)
5269 return dm->brightness[bl_idx];
5270
5271 return convert_brightness_to_user(&caps, ret);
5272}
5273
5274static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
5275{
5276 struct amdgpu_display_manager *dm = bl_get_data(bd);
5277 int i;
5278
5279 for (i = 0; i < dm->num_of_edps; i++) {
5280 if (bd == dm->backlight_dev[i])
5281 break;
5282 }
5283 if (i >= AMDGPU_DM_MAX_NUM_EDP)
5284 i = 0;
5285 return amdgpu_dm_backlight_get_level(dm, i);
5286}
5287
5288static const struct backlight_ops amdgpu_dm_backlight_ops = {
5289 .options = BL_CORE_SUSPENDRESUME,
5290 .get_brightness = amdgpu_dm_backlight_get_brightness,
5291 .update_status = amdgpu_dm_backlight_update_status,
5292};
5293
5294static void
5295amdgpu_dm_register_backlight_device(struct amdgpu_dm_connector *aconnector)
5296{
5297 struct drm_device *drm = aconnector->base.dev;
5298 struct amdgpu_display_manager *dm = &drm_to_adev(drm)->dm;
5299 struct backlight_properties props = { 0 };
5300 struct amdgpu_dm_backlight_caps *caps;
5301 char bl_name[16];
5302 int min, max;
5303 int real_brightness;
5304 int init_brightness;
5305
5306 if (aconnector->bl_idx == -1)
5307 return;
5308
5309 if (!acpi_video_backlight_use_native()) {
5310 drm_info(drm, "Skipping amdgpu DM backlight registration\n");
5311 /* Try registering an ACPI video backlight device instead. */
5312 acpi_video_register_backlight();
5313 return;
5314 }
5315
5316 caps = &dm->backlight_caps[aconnector->bl_idx];
5317 if (get_brightness_range(caps, &min, &max)) {
5318 if (power_supply_is_system_supplied() > 0)
5319 props.brightness = DIV_ROUND_CLOSEST((max - min) * caps->ac_level, 100);
5320 else
5321 props.brightness = DIV_ROUND_CLOSEST((max - min) * caps->dc_level, 100);
5322 /* min is zero, so max needs to be adjusted */
5323 props.max_brightness = max - min;
5324 drm_dbg(drm, "Backlight caps: min: %d, max: %d, ac %d, dc %d\n", min, max,
5325 caps->ac_level, caps->dc_level);
5326 } else
5327 props.brightness = props.max_brightness = MAX_BACKLIGHT_LEVEL;
5328
5329 init_brightness = props.brightness;
5330
5331 if (caps->data_points && !(amdgpu_dc_debug_mask & DC_DISABLE_CUSTOM_BRIGHTNESS_CURVE)) {
5332 drm_info(drm, "Using custom brightness curve\n");
5333 props.scale = BACKLIGHT_SCALE_NON_LINEAR;
5334 } else
5335 props.scale = BACKLIGHT_SCALE_LINEAR;
5336 props.type = BACKLIGHT_RAW;
5337
5338 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
5339 drm->primary->index + aconnector->bl_idx);
5340
5341 dm->backlight_dev[aconnector->bl_idx] =
5342 backlight_device_register(bl_name, aconnector->base.kdev, dm,
5343 &amdgpu_dm_backlight_ops, &props);
5344 dm->brightness[aconnector->bl_idx] = props.brightness;
5345
5346 if (IS_ERR(dm->backlight_dev[aconnector->bl_idx])) {
5347 drm_err(drm, "DM: Backlight registration failed!\n");
5348 dm->backlight_dev[aconnector->bl_idx] = NULL;
5349 } else {
5350 /*
5351 * dm->brightness[x] can be inconsistent just after startup until
5352 * ops.get_brightness is called.
5353 */
5354 real_brightness =
5355 amdgpu_dm_backlight_ops.get_brightness(dm->backlight_dev[aconnector->bl_idx]);
5356
5357 if (real_brightness != init_brightness) {
5358 dm->actual_brightness[aconnector->bl_idx] = real_brightness;
5359 dm->brightness[aconnector->bl_idx] = real_brightness;
5360 }
5361 drm_dbg_driver(drm, "DM: Registered Backlight device: %s\n", bl_name);
5362 }
5363}
5364
5365static int initialize_plane(struct amdgpu_display_manager *dm,
5366 struct amdgpu_mode_info *mode_info, int plane_id,
5367 enum drm_plane_type plane_type,
5368 const struct dc_plane_cap *plane_cap)
5369{
5370 struct drm_plane *plane;
5371 unsigned long possible_crtcs;
5372 int ret = 0;
5373
5374 plane = kzalloc_obj(struct drm_plane);
5375 if (!plane) {
5376 drm_err(adev_to_drm(dm->adev), "KMS: Failed to allocate plane\n");
5377 return -ENOMEM;
5378 }
5379 plane->type = plane_type;
5380
5381 /*
5382 * HACK: IGT tests expect that the primary plane for a CRTC
5383 * can only have one possible CRTC. Only expose support for
5384 * any CRTC if they're not going to be used as a primary plane
5385 * for a CRTC - like overlay or underlay planes.
5386 */
5387 possible_crtcs = 1 << plane_id;
5388 if (plane_id >= dm->dc->caps.max_streams)
5389 possible_crtcs = 0xff;
5390
5391 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap);
5392
5393 if (ret) {
5394 drm_err(adev_to_drm(dm->adev), "KMS: Failed to initialize plane\n");
5395 kfree(plane);
5396 return ret;
5397 }
5398
5399 if (mode_info)
5400 mode_info->planes[plane_id] = plane;
5401
5402 return ret;
5403}
5404
5405
5406static void setup_backlight_device(struct amdgpu_display_manager *dm,
5407 struct amdgpu_dm_connector *aconnector)
5408{
5409 struct amdgpu_dm_backlight_caps *caps;
5410 struct dc_link *link = aconnector->dc_link;
5411 int bl_idx = dm->num_of_edps;
5412
5413 if (!(link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) ||
5414 link->type == dc_connection_none)
5415 return;
5416
5417 if (dm->num_of_edps >= AMDGPU_DM_MAX_NUM_EDP) {
5418 drm_warn(adev_to_drm(dm->adev), "Too much eDP connections, skipping backlight setup for additional eDPs\n");
5419 return;
5420 }
5421
5422 aconnector->bl_idx = bl_idx;
5423
5424 amdgpu_dm_update_backlight_caps(dm, bl_idx);
5425 dm->backlight_link[bl_idx] = link;
5426 dm->num_of_edps++;
5427
5428 update_connector_ext_caps(aconnector);
5429 caps = &dm->backlight_caps[aconnector->bl_idx];
5430
5431 /* Only offer ABM property when non-OLED and user didn't turn off by module parameter */
5432 if (caps->ext_caps && !caps->ext_caps->bits.oled && amdgpu_dm_abm_level < 0)
5433 drm_object_attach_property(&aconnector->base.base,
5434 dm->adev->mode_info.abm_level_property,
5435 ABM_SYSFS_CONTROL);
5436}
5437
5438static void amdgpu_set_panel_orientation(struct drm_connector *connector);
5439
5440/*
5441 * In this architecture, the association
5442 * connector -> encoder -> crtc
5443 * id not really requried. The crtc and connector will hold the
5444 * display_index as an abstraction to use with DAL component
5445 *
5446 * Returns 0 on success
5447 */
5448static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
5449{
5450 struct amdgpu_display_manager *dm = &adev->dm;
5451 s32 i;
5452 struct amdgpu_dm_connector *aconnector = NULL;
5453 struct amdgpu_encoder *aencoder = NULL;
5454 struct amdgpu_mode_info *mode_info = &adev->mode_info;
5455 u32 link_cnt;
5456 s32 primary_planes;
5457 enum dc_connection_type new_connection_type = dc_connection_none;
5458 const struct dc_plane_cap *plane;
5459 bool psr_feature_enabled = false;
5460 bool replay_feature_enabled = false;
5461 int max_overlay = dm->dc->caps.max_slave_planes;
5462
5463 dm->display_indexes_num = dm->dc->caps.max_streams;
5464 /* Update the actual used number of crtc */
5465 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
5466
5467 amdgpu_dm_set_irq_funcs(adev);
5468
5469 link_cnt = dm->dc->caps.max_links;
5470 if (amdgpu_dm_mode_config_init(dm->adev)) {
5471 drm_err(adev_to_drm(adev), "DM: Failed to initialize mode config\n");
5472 return -EINVAL;
5473 }
5474
5475 /* There is one primary plane per CRTC */
5476 primary_planes = dm->dc->caps.max_streams;
5477 if (primary_planes > AMDGPU_MAX_PLANES) {
5478 drm_err(adev_to_drm(adev), "DM: Plane nums out of 6 planes\n");
5479 return -EINVAL;
5480 }
5481
5482 /*
5483 * Initialize primary planes, implicit planes for legacy IOCTLS.
5484 * Order is reversed to match iteration order in atomic check.
5485 */
5486 for (i = (primary_planes - 1); i >= 0; i--) {
5487 plane = &dm->dc->caps.planes[i];
5488
5489 if (initialize_plane(dm, mode_info, i,
5490 DRM_PLANE_TYPE_PRIMARY, plane)) {
5491 drm_err(adev_to_drm(adev), "KMS: Failed to initialize primary plane\n");
5492 goto fail;
5493 }
5494 }
5495
5496 /*
5497 * Initialize overlay planes, index starting after primary planes.
5498 * These planes have a higher DRM index than the primary planes since
5499 * they should be considered as having a higher z-order.
5500 * Order is reversed to match iteration order in atomic check.
5501 *
5502 * Only support DCN for now, and only expose one so we don't encourage
5503 * userspace to use up all the pipes.
5504 */
5505 for (i = 0; i < dm->dc->caps.max_planes; ++i) {
5506 struct dc_plane_cap *plane = &dm->dc->caps.planes[i];
5507
5508 /* Do not create overlay if MPO disabled */
5509 if (amdgpu_dc_debug_mask & DC_DISABLE_MPO)
5510 break;
5511
5512 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL)
5513 continue;
5514
5515 if (!plane->pixel_format_support.argb8888)
5516 continue;
5517
5518 if (max_overlay-- == 0)
5519 break;
5520
5521 if (initialize_plane(dm, NULL, primary_planes + i,
5522 DRM_PLANE_TYPE_OVERLAY, plane)) {
5523 drm_err(adev_to_drm(adev), "KMS: Failed to initialize overlay plane\n");
5524 goto fail;
5525 }
5526 }
5527
5528 for (i = 0; i < dm->dc->caps.max_streams; i++)
5529 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) {
5530 drm_err(adev_to_drm(adev), "KMS: Failed to initialize crtc\n");
5531 goto fail;
5532 }
5533
5534 /* Use Outbox interrupt */
5535 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5536 case IP_VERSION(3, 0, 0):
5537 case IP_VERSION(3, 1, 2):
5538 case IP_VERSION(3, 1, 3):
5539 case IP_VERSION(3, 1, 4):
5540 case IP_VERSION(3, 1, 5):
5541 case IP_VERSION(3, 1, 6):
5542 case IP_VERSION(3, 2, 0):
5543 case IP_VERSION(3, 2, 1):
5544 case IP_VERSION(2, 1, 0):
5545 case IP_VERSION(3, 5, 0):
5546 case IP_VERSION(3, 5, 1):
5547 case IP_VERSION(3, 6, 0):
5548 case IP_VERSION(4, 0, 1):
5549 case IP_VERSION(4, 2, 0):
5550 if (register_outbox_irq_handlers(dm->adev)) {
5551 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5552 goto fail;
5553 }
5554 break;
5555 default:
5556 drm_dbg_kms(adev_to_drm(adev), "Unsupported DCN IP version for outbox: 0x%X\n",
5557 amdgpu_ip_version(adev, DCE_HWIP, 0));
5558 }
5559
5560 /* Determine whether to enable PSR support by default. */
5561 if (!(amdgpu_dc_debug_mask & DC_DISABLE_PSR)) {
5562 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5563 case IP_VERSION(3, 1, 2):
5564 case IP_VERSION(3, 1, 3):
5565 case IP_VERSION(3, 1, 4):
5566 case IP_VERSION(3, 1, 5):
5567 case IP_VERSION(3, 1, 6):
5568 case IP_VERSION(3, 2, 0):
5569 case IP_VERSION(3, 2, 1):
5570 case IP_VERSION(3, 5, 0):
5571 case IP_VERSION(3, 5, 1):
5572 case IP_VERSION(3, 6, 0):
5573 case IP_VERSION(4, 0, 1):
5574 case IP_VERSION(4, 2, 0):
5575 psr_feature_enabled = true;
5576 break;
5577 default:
5578 psr_feature_enabled = amdgpu_dc_feature_mask & DC_PSR_MASK;
5579 break;
5580 }
5581 }
5582
5583 /* Determine whether to enable Replay support by default. */
5584 if (!(amdgpu_dc_debug_mask & DC_DISABLE_REPLAY)) {
5585 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5586 case IP_VERSION(3, 1, 4):
5587 case IP_VERSION(3, 2, 0):
5588 case IP_VERSION(3, 2, 1):
5589 case IP_VERSION(3, 5, 0):
5590 case IP_VERSION(3, 5, 1):
5591 case IP_VERSION(3, 6, 0):
5592 case IP_VERSION(4, 2, 0):
5593 replay_feature_enabled = true;
5594 break;
5595
5596 default:
5597 replay_feature_enabled = amdgpu_dc_feature_mask & DC_REPLAY_MASK;
5598 break;
5599 }
5600 }
5601
5602 if (link_cnt > MAX_LINKS) {
5603 drm_err(adev_to_drm(adev),
5604 "KMS: Cannot support more than %d display indexes\n",
5605 MAX_LINKS);
5606 goto fail;
5607 }
5608
5609 /* loops over all connectors on the board */
5610 for (i = 0; i < link_cnt; i++) {
5611 struct dc_link *link = NULL;
5612
5613 link = dc_get_link_at_index(dm->dc, i);
5614
5615 if (link->connector_signal == SIGNAL_TYPE_VIRTUAL) {
5616 struct amdgpu_dm_wb_connector *wbcon = kzalloc_obj(*wbcon);
5617
5618 if (!wbcon) {
5619 drm_err(adev_to_drm(adev), "KMS: Failed to allocate writeback connector\n");
5620 continue;
5621 }
5622
5623 if (amdgpu_dm_wb_connector_init(dm, wbcon, i)) {
5624 drm_err(adev_to_drm(adev), "KMS: Failed to initialize writeback connector\n");
5625 kfree(wbcon);
5626 continue;
5627 }
5628
5629 link->psr_settings.psr_feature_enabled = false;
5630 link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
5631
5632 continue;
5633 }
5634
5635 aconnector = kzalloc_obj(*aconnector);
5636 if (!aconnector)
5637 goto fail;
5638
5639 aencoder = kzalloc_obj(*aencoder);
5640 if (!aencoder)
5641 goto fail;
5642
5643 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
5644 drm_err(adev_to_drm(adev), "KMS: Failed to initialize encoder\n");
5645 goto fail;
5646 }
5647
5648 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
5649 drm_err(adev_to_drm(adev), "KMS: Failed to initialize connector\n");
5650 goto fail;
5651 }
5652
5653 if (dm->hpd_rx_offload_wq)
5654 dm->hpd_rx_offload_wq[aconnector->base.index].aconnector =
5655 aconnector;
5656
5657 if (!dc_link_detect_connection_type(link, &new_connection_type))
5658 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
5659
5660 if (aconnector->base.force && new_connection_type == dc_connection_none) {
5661 emulated_link_detect(link);
5662 amdgpu_dm_update_connector_after_detect(aconnector);
5663 } else {
5664 bool ret = false;
5665
5666 mutex_lock(&dm->dc_lock);
5667 dc_exit_ips_for_hw_access(dm->dc);
5668 ret = dc_link_detect(link, DETECT_REASON_BOOT);
5669 mutex_unlock(&dm->dc_lock);
5670
5671 if (ret) {
5672 amdgpu_dm_update_connector_after_detect(aconnector);
5673 setup_backlight_device(dm, aconnector);
5674
5675 /* Disable PSR if Replay can be enabled */
5676 if (replay_feature_enabled)
5677 if (amdgpu_dm_set_replay_caps(link, aconnector))
5678 psr_feature_enabled = false;
5679
5680 if (psr_feature_enabled) {
5681 amdgpu_dm_set_psr_caps(link);
5682 drm_info(adev_to_drm(adev), "%s: PSR support %d, DC PSR ver %d, sink PSR ver %d DPCD caps 0x%x su_y_granularity %d\n",
5683 aconnector->base.name,
5684 link->psr_settings.psr_feature_enabled,
5685 link->psr_settings.psr_version,
5686 link->dpcd_caps.psr_info.psr_version,
5687 link->dpcd_caps.psr_info.psr_dpcd_caps.raw,
5688 link->dpcd_caps.psr_info.psr2_su_y_granularity_cap);
5689 }
5690 }
5691 }
5692 amdgpu_set_panel_orientation(&aconnector->base);
5693 }
5694
5695 /* Debug dump: list all DC links and their associated sinks after detection
5696 * is complete for all connectors. This provides a comprehensive view of the
5697 * final state without repeating the dump for each connector.
5698 */
5699 amdgpu_dm_dump_links_and_sinks(adev);
5700
5701 /* Software is initialized. Now we can register interrupt handlers. */
5702 switch (adev->asic_type) {
5703#if defined(CONFIG_DRM_AMD_DC_SI)
5704 case CHIP_TAHITI:
5705 case CHIP_PITCAIRN:
5706 case CHIP_VERDE:
5707 case CHIP_OLAND:
5708#endif
5709 case CHIP_BONAIRE:
5710 case CHIP_HAWAII:
5711 case CHIP_KAVERI:
5712 case CHIP_KABINI:
5713 case CHIP_MULLINS:
5714 case CHIP_TONGA:
5715 case CHIP_FIJI:
5716 case CHIP_CARRIZO:
5717 case CHIP_STONEY:
5718 case CHIP_POLARIS11:
5719 case CHIP_POLARIS10:
5720 case CHIP_POLARIS12:
5721 case CHIP_VEGAM:
5722 case CHIP_VEGA10:
5723 case CHIP_VEGA12:
5724 case CHIP_VEGA20:
5725 if (dce110_register_irq_handlers(dm->adev)) {
5726 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5727 goto fail;
5728 }
5729 break;
5730 default:
5731 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5732 case IP_VERSION(1, 0, 0):
5733 case IP_VERSION(1, 0, 1):
5734 case IP_VERSION(2, 0, 2):
5735 case IP_VERSION(2, 0, 3):
5736 case IP_VERSION(2, 0, 0):
5737 case IP_VERSION(2, 1, 0):
5738 case IP_VERSION(3, 0, 0):
5739 case IP_VERSION(3, 0, 2):
5740 case IP_VERSION(3, 0, 3):
5741 case IP_VERSION(3, 0, 1):
5742 case IP_VERSION(3, 1, 2):
5743 case IP_VERSION(3, 1, 3):
5744 case IP_VERSION(3, 1, 4):
5745 case IP_VERSION(3, 1, 5):
5746 case IP_VERSION(3, 1, 6):
5747 case IP_VERSION(3, 2, 0):
5748 case IP_VERSION(3, 2, 1):
5749 case IP_VERSION(3, 5, 0):
5750 case IP_VERSION(3, 5, 1):
5751 case IP_VERSION(3, 6, 0):
5752 case IP_VERSION(4, 0, 1):
5753 case IP_VERSION(4, 2, 0):
5754 if (dcn10_register_irq_handlers(dm->adev)) {
5755 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5756 goto fail;
5757 }
5758 break;
5759 default:
5760 drm_err(adev_to_drm(adev), "Unsupported DCE IP versions: 0x%X\n",
5761 amdgpu_ip_version(adev, DCE_HWIP, 0));
5762 goto fail;
5763 }
5764 break;
5765 }
5766
5767 return 0;
5768fail:
5769 kfree(aencoder);
5770 kfree(aconnector);
5771
5772 return -EINVAL;
5773}
5774
5775static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
5776{
5777 if (dm->atomic_obj.state)
5778 drm_atomic_private_obj_fini(&dm->atomic_obj);
5779}
5780
5781/******************************************************************************
5782 * amdgpu_display_funcs functions
5783 *****************************************************************************/
5784
5785/*
5786 * dm_bandwidth_update - program display watermarks
5787 *
5788 * @adev: amdgpu_device pointer
5789 *
5790 * Calculate and program the display watermarks and line buffer allocation.
5791 */
5792static void dm_bandwidth_update(struct amdgpu_device *adev)
5793{
5794 /* TODO: implement later */
5795}
5796
5797static const struct amdgpu_display_funcs dm_display_funcs = {
5798 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
5799 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
5800 .backlight_set_level = NULL, /* never called for DC */
5801 .backlight_get_level = NULL, /* never called for DC */
5802 .hpd_sense = NULL,/* called unconditionally */
5803 .hpd_set_polarity = NULL, /* called unconditionally */
5804 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
5805 .page_flip_get_scanoutpos =
5806 dm_crtc_get_scanoutpos,/* called unconditionally */
5807 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
5808 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
5809};
5810
5811#if defined(CONFIG_DEBUG_KERNEL_DC)
5812
5813static ssize_t s3_debug_store(struct device *device,
5814 struct device_attribute *attr,
5815 const char *buf,
5816 size_t count)
5817{
5818 int ret;
5819 int s3_state;
5820 struct drm_device *drm_dev = dev_get_drvdata(device);
5821 struct amdgpu_device *adev = drm_to_adev(drm_dev);
5822 struct amdgpu_ip_block *ip_block;
5823
5824 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_DCE);
5825 if (!ip_block)
5826 return -EINVAL;
5827
5828 ret = kstrtoint(buf, 0, &s3_state);
5829
5830 if (ret == 0) {
5831 if (s3_state) {
5832 dm_resume(ip_block);
5833 drm_kms_helper_hotplug_event(adev_to_drm(adev));
5834 } else
5835 dm_suspend(ip_block);
5836 }
5837
5838 return ret == 0 ? count : 0;
5839}
5840
5841DEVICE_ATTR_WO(s3_debug);
5842
5843#endif
5844
5845static int dm_init_microcode(struct amdgpu_device *adev)
5846{
5847 char *fw_name_dmub;
5848 int r;
5849
5850 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5851 case IP_VERSION(2, 1, 0):
5852 fw_name_dmub = FIRMWARE_RENOIR_DMUB;
5853 if (ASICREV_IS_GREEN_SARDINE(adev->external_rev_id))
5854 fw_name_dmub = FIRMWARE_GREEN_SARDINE_DMUB;
5855 break;
5856 case IP_VERSION(3, 0, 0):
5857 if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(10, 3, 0))
5858 fw_name_dmub = FIRMWARE_SIENNA_CICHLID_DMUB;
5859 else
5860 fw_name_dmub = FIRMWARE_NAVY_FLOUNDER_DMUB;
5861 break;
5862 case IP_VERSION(3, 0, 1):
5863 fw_name_dmub = FIRMWARE_VANGOGH_DMUB;
5864 break;
5865 case IP_VERSION(3, 0, 2):
5866 fw_name_dmub = FIRMWARE_DIMGREY_CAVEFISH_DMUB;
5867 break;
5868 case IP_VERSION(3, 0, 3):
5869 fw_name_dmub = FIRMWARE_BEIGE_GOBY_DMUB;
5870 break;
5871 case IP_VERSION(3, 1, 2):
5872 case IP_VERSION(3, 1, 3):
5873 fw_name_dmub = FIRMWARE_YELLOW_CARP_DMUB;
5874 break;
5875 case IP_VERSION(3, 1, 4):
5876 fw_name_dmub = FIRMWARE_DCN_314_DMUB;
5877 break;
5878 case IP_VERSION(3, 1, 5):
5879 fw_name_dmub = FIRMWARE_DCN_315_DMUB;
5880 break;
5881 case IP_VERSION(3, 1, 6):
5882 fw_name_dmub = FIRMWARE_DCN316_DMUB;
5883 break;
5884 case IP_VERSION(3, 2, 0):
5885 fw_name_dmub = FIRMWARE_DCN_V3_2_0_DMCUB;
5886 break;
5887 case IP_VERSION(3, 2, 1):
5888 fw_name_dmub = FIRMWARE_DCN_V3_2_1_DMCUB;
5889 break;
5890 case IP_VERSION(3, 5, 0):
5891 fw_name_dmub = FIRMWARE_DCN_35_DMUB;
5892 break;
5893 case IP_VERSION(3, 5, 1):
5894 fw_name_dmub = FIRMWARE_DCN_351_DMUB;
5895 break;
5896 case IP_VERSION(3, 6, 0):
5897 fw_name_dmub = FIRMWARE_DCN_36_DMUB;
5898 break;
5899 case IP_VERSION(4, 0, 1):
5900 fw_name_dmub = FIRMWARE_DCN_401_DMUB;
5901 break;
5902 case IP_VERSION(4, 2, 0):
5903 fw_name_dmub = FIRMWARE_DCN_42_DMUB;
5904 break;
5905 default:
5906 /* ASIC doesn't support DMUB. */
5907 return 0;
5908 }
5909 r = amdgpu_ucode_request(adev, &adev->dm.dmub_fw, AMDGPU_UCODE_REQUIRED,
5910 "%s", fw_name_dmub);
5911 return r;
5912}
5913
5914static int dm_early_init(struct amdgpu_ip_block *ip_block)
5915{
5916 struct amdgpu_device *adev = ip_block->adev;
5917 struct amdgpu_mode_info *mode_info = &adev->mode_info;
5918 struct atom_context *ctx = mode_info->atom_context;
5919 int index = GetIndexIntoMasterTable(DATA, Object_Header);
5920 u16 data_offset;
5921
5922 /* if there is no object header, skip DM */
5923 if (!amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
5924 adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK;
5925 drm_info(adev_to_drm(adev), "No object header, skipping DM\n");
5926 return -ENOENT;
5927 }
5928
5929 switch (adev->asic_type) {
5930#if defined(CONFIG_DRM_AMD_DC_SI)
5931 case CHIP_TAHITI:
5932 case CHIP_PITCAIRN:
5933 case CHIP_VERDE:
5934 adev->mode_info.num_crtc = 6;
5935 adev->mode_info.num_hpd = 6;
5936 adev->mode_info.num_dig = 6;
5937 break;
5938 case CHIP_OLAND:
5939 adev->mode_info.num_crtc = 2;
5940 adev->mode_info.num_hpd = 2;
5941 adev->mode_info.num_dig = 2;
5942 break;
5943#endif
5944 case CHIP_BONAIRE:
5945 case CHIP_HAWAII:
5946 adev->mode_info.num_crtc = 6;
5947 adev->mode_info.num_hpd = 6;
5948 adev->mode_info.num_dig = 6;
5949 break;
5950 case CHIP_KAVERI:
5951 adev->mode_info.num_crtc = 4;
5952 adev->mode_info.num_hpd = 6;
5953 adev->mode_info.num_dig = 7;
5954 break;
5955 case CHIP_KABINI:
5956 case CHIP_MULLINS:
5957 adev->mode_info.num_crtc = 2;
5958 adev->mode_info.num_hpd = 6;
5959 adev->mode_info.num_dig = 6;
5960 break;
5961 case CHIP_FIJI:
5962 case CHIP_TONGA:
5963 adev->mode_info.num_crtc = 6;
5964 adev->mode_info.num_hpd = 6;
5965 adev->mode_info.num_dig = 7;
5966 break;
5967 case CHIP_CARRIZO:
5968 adev->mode_info.num_crtc = 3;
5969 adev->mode_info.num_hpd = 6;
5970 adev->mode_info.num_dig = 9;
5971 break;
5972 case CHIP_STONEY:
5973 adev->mode_info.num_crtc = 2;
5974 adev->mode_info.num_hpd = 6;
5975 adev->mode_info.num_dig = 9;
5976 break;
5977 case CHIP_POLARIS11:
5978 case CHIP_POLARIS12:
5979 adev->mode_info.num_crtc = 5;
5980 adev->mode_info.num_hpd = 5;
5981 adev->mode_info.num_dig = 5;
5982 break;
5983 case CHIP_POLARIS10:
5984 case CHIP_VEGAM:
5985 adev->mode_info.num_crtc = 6;
5986 adev->mode_info.num_hpd = 6;
5987 adev->mode_info.num_dig = 6;
5988 break;
5989 case CHIP_VEGA10:
5990 case CHIP_VEGA12:
5991 case CHIP_VEGA20:
5992 adev->mode_info.num_crtc = 6;
5993 adev->mode_info.num_hpd = 6;
5994 adev->mode_info.num_dig = 6;
5995 break;
5996 default:
5997
5998 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5999 case IP_VERSION(2, 0, 2):
6000 case IP_VERSION(3, 0, 0):
6001 adev->mode_info.num_crtc = 6;
6002 adev->mode_info.num_hpd = 6;
6003 adev->mode_info.num_dig = 6;
6004 break;
6005 case IP_VERSION(2, 0, 0):
6006 case IP_VERSION(3, 0, 2):
6007 adev->mode_info.num_crtc = 5;
6008 adev->mode_info.num_hpd = 5;
6009 adev->mode_info.num_dig = 5;
6010 break;
6011 case IP_VERSION(2, 0, 3):
6012 case IP_VERSION(3, 0, 3):
6013 adev->mode_info.num_crtc = 2;
6014 adev->mode_info.num_hpd = 2;
6015 adev->mode_info.num_dig = 2;
6016 break;
6017 case IP_VERSION(1, 0, 0):
6018 case IP_VERSION(1, 0, 1):
6019 case IP_VERSION(3, 0, 1):
6020 case IP_VERSION(2, 1, 0):
6021 case IP_VERSION(3, 1, 2):
6022 case IP_VERSION(3, 1, 3):
6023 case IP_VERSION(3, 1, 4):
6024 case IP_VERSION(3, 1, 5):
6025 case IP_VERSION(3, 1, 6):
6026 case IP_VERSION(3, 2, 0):
6027 case IP_VERSION(3, 2, 1):
6028 case IP_VERSION(3, 5, 0):
6029 case IP_VERSION(3, 5, 1):
6030 case IP_VERSION(3, 6, 0):
6031 case IP_VERSION(4, 0, 1):
6032 case IP_VERSION(4, 2, 0):
6033 adev->mode_info.num_crtc = 4;
6034 adev->mode_info.num_hpd = 4;
6035 adev->mode_info.num_dig = 4;
6036 break;
6037 default:
6038 drm_err(adev_to_drm(adev), "Unsupported DCE IP versions: 0x%x\n",
6039 amdgpu_ip_version(adev, DCE_HWIP, 0));
6040 return -EINVAL;
6041 }
6042 break;
6043 }
6044
6045 if (adev->mode_info.funcs == NULL)
6046 adev->mode_info.funcs = &dm_display_funcs;
6047
6048 /*
6049 * Note: Do NOT change adev->reg.audio_endpt.rreg and
6050 * adev->reg.audio_endpt.wreg because they are initialised in
6051 * amdgpu_device_init()
6052 */
6053#if defined(CONFIG_DEBUG_KERNEL_DC)
6054 device_create_file(
6055 adev_to_drm(adev)->dev,
6056 &dev_attr_s3_debug);
6057#endif
6058 adev->dc_enabled = true;
6059
6060 return dm_init_microcode(adev);
6061}
6062
6063static bool modereset_required(struct drm_crtc_state *crtc_state)
6064{
6065 return !crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state);
6066}
6067
6068static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
6069{
6070 drm_encoder_cleanup(encoder);
6071 kfree(encoder);
6072}
6073
6074static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
6075 .destroy = amdgpu_dm_encoder_destroy,
6076};
6077
6078static int
6079fill_plane_color_attributes(const struct drm_plane_state *plane_state,
6080 const enum surface_pixel_format format,
6081 enum dc_color_space *color_space)
6082{
6083 bool full_range;
6084
6085 *color_space = COLOR_SPACE_SRGB;
6086
6087 /* Ignore properties when DRM_CLIENT_CAP_PLANE_COLOR_PIPELINE is set */
6088 if (plane_state->state && plane_state->state->plane_color_pipeline)
6089 return 0;
6090
6091 /* DRM color properties only affect non-RGB formats. */
6092 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
6093 return 0;
6094
6095 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE);
6096
6097 switch (plane_state->color_encoding) {
6098 case DRM_COLOR_YCBCR_BT601:
6099 if (full_range)
6100 *color_space = COLOR_SPACE_YCBCR601;
6101 else
6102 *color_space = COLOR_SPACE_YCBCR601_LIMITED;
6103 break;
6104
6105 case DRM_COLOR_YCBCR_BT709:
6106 if (full_range)
6107 *color_space = COLOR_SPACE_YCBCR709;
6108 else
6109 *color_space = COLOR_SPACE_YCBCR709_LIMITED;
6110 break;
6111
6112 case DRM_COLOR_YCBCR_BT2020:
6113 if (full_range)
6114 *color_space = COLOR_SPACE_2020_YCBCR_FULL;
6115 else
6116 *color_space = COLOR_SPACE_2020_YCBCR_LIMITED;
6117 break;
6118
6119 default:
6120 return -EINVAL;
6121 }
6122
6123 return 0;
6124}
6125
6126static int
6127fill_dc_plane_info_and_addr(struct amdgpu_device *adev,
6128 const struct drm_plane_state *plane_state,
6129 const u64 tiling_flags,
6130 struct dc_plane_info *plane_info,
6131 struct dc_plane_address *address,
6132 bool tmz_surface)
6133{
6134 const struct drm_framebuffer *fb = plane_state->fb;
6135 const struct amdgpu_framebuffer *afb =
6136 to_amdgpu_framebuffer(plane_state->fb);
6137 int ret;
6138
6139 memset(plane_info, 0, sizeof(*plane_info));
6140
6141 switch (fb->format->format) {
6142 case DRM_FORMAT_C8:
6143 plane_info->format =
6144 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
6145 break;
6146 case DRM_FORMAT_RGB565:
6147 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
6148 break;
6149 case DRM_FORMAT_XRGB8888:
6150 case DRM_FORMAT_ARGB8888:
6151 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
6152 break;
6153 case DRM_FORMAT_XRGB2101010:
6154 case DRM_FORMAT_ARGB2101010:
6155 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
6156 break;
6157 case DRM_FORMAT_XBGR2101010:
6158 case DRM_FORMAT_ABGR2101010:
6159 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
6160 break;
6161 case DRM_FORMAT_XBGR8888:
6162 case DRM_FORMAT_ABGR8888:
6163 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888;
6164 break;
6165 case DRM_FORMAT_NV21:
6166 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
6167 break;
6168 case DRM_FORMAT_NV12:
6169 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
6170 break;
6171 case DRM_FORMAT_P010:
6172 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb;
6173 break;
6174 case DRM_FORMAT_XRGB16161616F:
6175 case DRM_FORMAT_ARGB16161616F:
6176 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F;
6177 break;
6178 case DRM_FORMAT_XBGR16161616F:
6179 case DRM_FORMAT_ABGR16161616F:
6180 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F;
6181 break;
6182 case DRM_FORMAT_XRGB16161616:
6183 case DRM_FORMAT_ARGB16161616:
6184 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616;
6185 break;
6186 case DRM_FORMAT_XBGR16161616:
6187 case DRM_FORMAT_ABGR16161616:
6188 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616;
6189 break;
6190 default:
6191 drm_err(adev_to_drm(adev),
6192 "Unsupported screen format %p4cc\n",
6193 &fb->format->format);
6194 return -EINVAL;
6195 }
6196
6197 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
6198 case DRM_MODE_ROTATE_0:
6199 plane_info->rotation = ROTATION_ANGLE_0;
6200 break;
6201 case DRM_MODE_ROTATE_90:
6202 plane_info->rotation = ROTATION_ANGLE_90;
6203 break;
6204 case DRM_MODE_ROTATE_180:
6205 plane_info->rotation = ROTATION_ANGLE_180;
6206 break;
6207 case DRM_MODE_ROTATE_270:
6208 plane_info->rotation = ROTATION_ANGLE_270;
6209 break;
6210 default:
6211 plane_info->rotation = ROTATION_ANGLE_0;
6212 break;
6213 }
6214
6215
6216 plane_info->visible = true;
6217 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE;
6218
6219 plane_info->layer_index = plane_state->normalized_zpos;
6220
6221 ret = fill_plane_color_attributes(plane_state, plane_info->format,
6222 &plane_info->color_space);
6223 if (ret)
6224 return ret;
6225
6226 ret = amdgpu_dm_plane_fill_plane_buffer_attributes(adev, afb, plane_info->format,
6227 plane_info->rotation, tiling_flags,
6228 &plane_info->tiling_info,
6229 &plane_info->plane_size,
6230 &plane_info->dcc, address,
6231 tmz_surface);
6232 if (ret)
6233 return ret;
6234
6235 amdgpu_dm_plane_fill_blending_from_plane_state(
6236 plane_state, &plane_info->per_pixel_alpha, &plane_info->pre_multiplied_alpha,
6237 &plane_info->global_alpha, &plane_info->global_alpha_value);
6238
6239 return 0;
6240}
6241
6242static int fill_dc_plane_attributes(struct amdgpu_device *adev,
6243 struct dc_plane_state *dc_plane_state,
6244 struct drm_plane_state *plane_state,
6245 struct drm_crtc_state *crtc_state)
6246{
6247 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
6248 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)plane_state->fb;
6249 struct dc_scaling_info scaling_info;
6250 struct dc_plane_info plane_info;
6251 int ret;
6252
6253 ret = amdgpu_dm_plane_fill_dc_scaling_info(adev, plane_state, &scaling_info);
6254 if (ret)
6255 return ret;
6256
6257 dc_plane_state->src_rect = scaling_info.src_rect;
6258 dc_plane_state->dst_rect = scaling_info.dst_rect;
6259 dc_plane_state->clip_rect = scaling_info.clip_rect;
6260 dc_plane_state->scaling_quality = scaling_info.scaling_quality;
6261
6262 ret = fill_dc_plane_info_and_addr(adev, plane_state,
6263 afb->tiling_flags,
6264 &plane_info,
6265 &dc_plane_state->address,
6266 afb->tmz_surface);
6267 if (ret)
6268 return ret;
6269
6270 dc_plane_state->format = plane_info.format;
6271 dc_plane_state->color_space = plane_info.color_space;
6272 dc_plane_state->format = plane_info.format;
6273 dc_plane_state->plane_size = plane_info.plane_size;
6274 dc_plane_state->rotation = plane_info.rotation;
6275 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror;
6276 dc_plane_state->stereo_format = plane_info.stereo_format;
6277 dc_plane_state->tiling_info = plane_info.tiling_info;
6278 dc_plane_state->visible = plane_info.visible;
6279 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha;
6280 dc_plane_state->pre_multiplied_alpha = plane_info.pre_multiplied_alpha;
6281 dc_plane_state->global_alpha = plane_info.global_alpha;
6282 dc_plane_state->global_alpha_value = plane_info.global_alpha_value;
6283 dc_plane_state->dcc = plane_info.dcc;
6284 dc_plane_state->layer_index = plane_info.layer_index;
6285 dc_plane_state->flip_int_enabled = true;
6286
6287 /*
6288 * Always set input transfer function, since plane state is refreshed
6289 * every time.
6290 */
6291 ret = amdgpu_dm_update_plane_color_mgmt(dm_crtc_state,
6292 plane_state,
6293 dc_plane_state);
6294 if (ret)
6295 return ret;
6296
6297 return 0;
6298}
6299
6300static inline void fill_dc_dirty_rect(struct drm_plane *plane,
6301 struct rect *dirty_rect, int32_t x,
6302 s32 y, s32 width, s32 height,
6303 int *i, bool ffu)
6304{
6305 WARN_ON(*i >= DC_MAX_DIRTY_RECTS);
6306
6307 dirty_rect->x = x;
6308 dirty_rect->y = y;
6309 dirty_rect->width = width;
6310 dirty_rect->height = height;
6311
6312 if (ffu)
6313 drm_dbg(plane->dev,
6314 "[PLANE:%d] PSR FFU dirty rect size (%d, %d)\n",
6315 plane->base.id, width, height);
6316 else
6317 drm_dbg(plane->dev,
6318 "[PLANE:%d] PSR SU dirty rect at (%d, %d) size (%d, %d)",
6319 plane->base.id, x, y, width, height);
6320
6321 (*i)++;
6322}
6323
6324/**
6325 * fill_dc_dirty_rects() - Fill DC dirty regions for PSR selective updates
6326 *
6327 * @plane: DRM plane containing dirty regions that need to be flushed to the eDP
6328 * remote fb
6329 * @old_plane_state: Old state of @plane
6330 * @new_plane_state: New state of @plane
6331 * @crtc_state: New state of CRTC connected to the @plane
6332 * @flip_addrs: DC flip tracking struct, which also tracts dirty rects
6333 * @is_psr_su: Flag indicating whether Panel Self Refresh Selective Update (PSR SU) is enabled.
6334 * If PSR SU is enabled and damage clips are available, only the regions of the screen
6335 * that have changed will be updated. If PSR SU is not enabled,
6336 * or if damage clips are not available, the entire screen will be updated.
6337 * @dirty_regions_changed: dirty regions changed
6338 *
6339 * For PSR SU, DC informs the DMUB uController of dirty rectangle regions
6340 * (referred to as "damage clips" in DRM nomenclature) that require updating on
6341 * the eDP remote buffer. The responsibility of specifying the dirty regions is
6342 * amdgpu_dm's.
6343 *
6344 * A damage-aware DRM client should fill the FB_DAMAGE_CLIPS property on the
6345 * plane with regions that require flushing to the eDP remote buffer. In
6346 * addition, certain use cases - such as cursor and multi-plane overlay (MPO) -
6347 * implicitly provide damage clips without any client support via the plane
6348 * bounds.
6349 */
6350static void fill_dc_dirty_rects(struct drm_plane *plane,
6351 struct drm_plane_state *old_plane_state,
6352 struct drm_plane_state *new_plane_state,
6353 struct drm_crtc_state *crtc_state,
6354 struct dc_flip_addrs *flip_addrs,
6355 bool is_psr_su,
6356 bool *dirty_regions_changed)
6357{
6358 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
6359 struct rect *dirty_rects = flip_addrs->dirty_rects;
6360 u32 num_clips;
6361 struct drm_mode_rect *clips;
6362 bool bb_changed;
6363 bool fb_changed;
6364 u32 i = 0;
6365 *dirty_regions_changed = false;
6366
6367 /*
6368 * Cursor plane has it's own dirty rect update interface. See
6369 * dcn10_dmub_update_cursor_data and dmub_cmd_update_cursor_info_data
6370 */
6371 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6372 return;
6373
6374 if (new_plane_state->rotation != DRM_MODE_ROTATE_0)
6375 goto ffu;
6376
6377 num_clips = drm_plane_get_damage_clips_count(new_plane_state);
6378 clips = drm_plane_get_damage_clips(new_plane_state);
6379
6380 if (num_clips && (!amdgpu_damage_clips || (amdgpu_damage_clips < 0 &&
6381 is_psr_su)))
6382 goto ffu;
6383
6384 if (!dm_crtc_state->mpo_requested) {
6385 if (!num_clips || num_clips > DC_MAX_DIRTY_RECTS)
6386 goto ffu;
6387
6388 for (; flip_addrs->dirty_rect_count < num_clips; clips++)
6389 fill_dc_dirty_rect(new_plane_state->plane,
6390 &dirty_rects[flip_addrs->dirty_rect_count],
6391 clips->x1, clips->y1,
6392 clips->x2 - clips->x1, clips->y2 - clips->y1,
6393 &flip_addrs->dirty_rect_count,
6394 false);
6395 return;
6396 }
6397
6398 /*
6399 * MPO is requested. Add entire plane bounding box to dirty rects if
6400 * flipped to or damaged.
6401 *
6402 * If plane is moved or resized, also add old bounding box to dirty
6403 * rects.
6404 */
6405 fb_changed = old_plane_state->fb->base.id !=
6406 new_plane_state->fb->base.id;
6407 bb_changed = (old_plane_state->crtc_x != new_plane_state->crtc_x ||
6408 old_plane_state->crtc_y != new_plane_state->crtc_y ||
6409 old_plane_state->crtc_w != new_plane_state->crtc_w ||
6410 old_plane_state->crtc_h != new_plane_state->crtc_h);
6411
6412 drm_dbg(plane->dev,
6413 "[PLANE:%d] PSR bb_changed:%d fb_changed:%d num_clips:%d\n",
6414 new_plane_state->plane->base.id,
6415 bb_changed, fb_changed, num_clips);
6416
6417 *dirty_regions_changed = bb_changed;
6418
6419 if ((num_clips + (bb_changed ? 2 : 0)) > DC_MAX_DIRTY_RECTS)
6420 goto ffu;
6421
6422 if (bb_changed) {
6423 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
6424 new_plane_state->crtc_x,
6425 new_plane_state->crtc_y,
6426 new_plane_state->crtc_w,
6427 new_plane_state->crtc_h, &i, false);
6428
6429 /* Add old plane bounding-box if plane is moved or resized */
6430 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
6431 old_plane_state->crtc_x,
6432 old_plane_state->crtc_y,
6433 old_plane_state->crtc_w,
6434 old_plane_state->crtc_h, &i, false);
6435 }
6436
6437 if (num_clips) {
6438 for (; i < num_clips; clips++)
6439 fill_dc_dirty_rect(new_plane_state->plane,
6440 &dirty_rects[i], clips->x1,
6441 clips->y1, clips->x2 - clips->x1,
6442 clips->y2 - clips->y1, &i, false);
6443 } else if (fb_changed && !bb_changed) {
6444 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
6445 new_plane_state->crtc_x,
6446 new_plane_state->crtc_y,
6447 new_plane_state->crtc_w,
6448 new_plane_state->crtc_h, &i, false);
6449 }
6450
6451 flip_addrs->dirty_rect_count = i;
6452 return;
6453
6454ffu:
6455 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[0], 0, 0,
6456 dm_crtc_state->base.mode.crtc_hdisplay,
6457 dm_crtc_state->base.mode.crtc_vdisplay,
6458 &flip_addrs->dirty_rect_count, true);
6459}
6460
6461static void update_stream_scaling_settings(struct drm_device *dev,
6462 const struct drm_display_mode *mode,
6463 const struct dm_connector_state *dm_state,
6464 struct dc_stream_state *stream)
6465{
6466 enum amdgpu_rmx_type rmx_type;
6467
6468 struct rect src = { 0 }; /* viewport in composition space*/
6469 struct rect dst = { 0 }; /* stream addressable area */
6470
6471 /* no mode. nothing to be done */
6472 if (!mode)
6473 return;
6474
6475 /* Full screen scaling by default */
6476 src.width = mode->hdisplay;
6477 src.height = mode->vdisplay;
6478 dst.width = stream->timing.h_addressable;
6479 dst.height = stream->timing.v_addressable;
6480
6481 if (dm_state) {
6482 rmx_type = dm_state->scaling;
6483 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
6484 if (src.width * dst.height <
6485 src.height * dst.width) {
6486 /* height needs less upscaling/more downscaling */
6487 dst.width = src.width *
6488 dst.height / src.height;
6489 } else {
6490 /* width needs less upscaling/more downscaling */
6491 dst.height = src.height *
6492 dst.width / src.width;
6493 }
6494 } else if (rmx_type == RMX_CENTER) {
6495 dst = src;
6496 }
6497
6498 dst.x = (stream->timing.h_addressable - dst.width) / 2;
6499 dst.y = (stream->timing.v_addressable - dst.height) / 2;
6500
6501 if (dm_state->underscan_enable) {
6502 dst.x += dm_state->underscan_hborder / 2;
6503 dst.y += dm_state->underscan_vborder / 2;
6504 dst.width -= dm_state->underscan_hborder;
6505 dst.height -= dm_state->underscan_vborder;
6506 }
6507 }
6508
6509 stream->src = src;
6510 stream->dst = dst;
6511
6512 drm_dbg_kms(dev, "Destination Rectangle x:%d y:%d width:%d height:%d\n",
6513 dst.x, dst.y, dst.width, dst.height);
6514
6515}
6516
6517static enum dc_color_depth
6518convert_color_depth_from_display_info(const struct drm_connector *connector,
6519 bool is_y420, int requested_bpc)
6520{
6521 u8 bpc;
6522
6523 if (is_y420) {
6524 bpc = 8;
6525
6526 /* Cap display bpc based on HDMI 2.0 HF-VSDB */
6527 if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48)
6528 bpc = 16;
6529 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36)
6530 bpc = 12;
6531 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30)
6532 bpc = 10;
6533 } else {
6534 bpc = (uint8_t)connector->display_info.bpc;
6535 /* Assume 8 bpc by default if no bpc is specified. */
6536 bpc = bpc ? bpc : 8;
6537 }
6538
6539 if (requested_bpc > 0) {
6540 /*
6541 * Cap display bpc based on the user requested value.
6542 *
6543 * The value for state->max_bpc may not correctly updated
6544 * depending on when the connector gets added to the state
6545 * or if this was called outside of atomic check, so it
6546 * can't be used directly.
6547 */
6548 bpc = min_t(u8, bpc, requested_bpc);
6549
6550 /* Round down to the nearest even number. */
6551 bpc = bpc - (bpc & 1);
6552 }
6553
6554 switch (bpc) {
6555 case 0:
6556 /*
6557 * Temporary Work around, DRM doesn't parse color depth for
6558 * EDID revision before 1.4
6559 * TODO: Fix edid parsing
6560 */
6561 return COLOR_DEPTH_888;
6562 case 6:
6563 return COLOR_DEPTH_666;
6564 case 8:
6565 return COLOR_DEPTH_888;
6566 case 10:
6567 return COLOR_DEPTH_101010;
6568 case 12:
6569 return COLOR_DEPTH_121212;
6570 case 14:
6571 return COLOR_DEPTH_141414;
6572 case 16:
6573 return COLOR_DEPTH_161616;
6574 default:
6575 return COLOR_DEPTH_UNDEFINED;
6576 }
6577}
6578
6579static enum dc_aspect_ratio
6580get_aspect_ratio(const struct drm_display_mode *mode_in)
6581{
6582 /* 1-1 mapping, since both enums follow the HDMI spec. */
6583 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
6584}
6585
6586static enum dc_color_space
6587get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing,
6588 const struct drm_connector_state *connector_state)
6589{
6590 enum dc_color_space color_space = COLOR_SPACE_SRGB;
6591
6592 switch (connector_state->colorspace) {
6593 case DRM_MODE_COLORIMETRY_BT601_YCC:
6594 if (dc_crtc_timing->flags.Y_ONLY)
6595 color_space = COLOR_SPACE_YCBCR601_LIMITED;
6596 else
6597 color_space = COLOR_SPACE_YCBCR601;
6598 break;
6599 case DRM_MODE_COLORIMETRY_BT709_YCC:
6600 if (dc_crtc_timing->flags.Y_ONLY)
6601 color_space = COLOR_SPACE_YCBCR709_LIMITED;
6602 else
6603 color_space = COLOR_SPACE_YCBCR709;
6604 break;
6605 case DRM_MODE_COLORIMETRY_OPRGB:
6606 color_space = COLOR_SPACE_ADOBERGB;
6607 break;
6608 case DRM_MODE_COLORIMETRY_BT2020_RGB:
6609 case DRM_MODE_COLORIMETRY_BT2020_YCC:
6610 if (dc_crtc_timing->pixel_encoding == PIXEL_ENCODING_RGB)
6611 color_space = COLOR_SPACE_2020_RGB_FULLRANGE;
6612 else
6613 color_space = COLOR_SPACE_2020_YCBCR_LIMITED;
6614 break;
6615 case DRM_MODE_COLORIMETRY_DEFAULT: // ITU601
6616 default:
6617 if (dc_crtc_timing->pixel_encoding == PIXEL_ENCODING_RGB) {
6618 color_space = COLOR_SPACE_SRGB;
6619 if (connector_state->hdmi.broadcast_rgb == DRM_HDMI_BROADCAST_RGB_LIMITED)
6620 color_space = COLOR_SPACE_SRGB_LIMITED;
6621 /*
6622 * 27030khz is the separation point between HDTV and SDTV
6623 * according to HDMI spec, we use YCbCr709 and YCbCr601
6624 * respectively
6625 */
6626 } else if (dc_crtc_timing->pix_clk_100hz > 270300) {
6627 if (dc_crtc_timing->flags.Y_ONLY)
6628 color_space =
6629 COLOR_SPACE_YCBCR709_LIMITED;
6630 else
6631 color_space = COLOR_SPACE_YCBCR709;
6632 } else {
6633 if (dc_crtc_timing->flags.Y_ONLY)
6634 color_space =
6635 COLOR_SPACE_YCBCR601_LIMITED;
6636 else
6637 color_space = COLOR_SPACE_YCBCR601;
6638 }
6639 break;
6640 }
6641
6642 return color_space;
6643}
6644
6645static enum display_content_type
6646get_output_content_type(const struct drm_connector_state *connector_state)
6647{
6648 switch (connector_state->content_type) {
6649 default:
6650 case DRM_MODE_CONTENT_TYPE_NO_DATA:
6651 return DISPLAY_CONTENT_TYPE_NO_DATA;
6652 case DRM_MODE_CONTENT_TYPE_GRAPHICS:
6653 return DISPLAY_CONTENT_TYPE_GRAPHICS;
6654 case DRM_MODE_CONTENT_TYPE_PHOTO:
6655 return DISPLAY_CONTENT_TYPE_PHOTO;
6656 case DRM_MODE_CONTENT_TYPE_CINEMA:
6657 return DISPLAY_CONTENT_TYPE_CINEMA;
6658 case DRM_MODE_CONTENT_TYPE_GAME:
6659 return DISPLAY_CONTENT_TYPE_GAME;
6660 }
6661}
6662
6663static bool adjust_colour_depth_from_display_info(
6664 struct dc_crtc_timing *timing_out,
6665 const struct drm_display_info *info)
6666{
6667 enum dc_color_depth depth = timing_out->display_color_depth;
6668 int normalized_clk;
6669
6670 do {
6671 normalized_clk = timing_out->pix_clk_100hz / 10;
6672 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
6673 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
6674 normalized_clk /= 2;
6675 /* Adjusting pix clock following on HDMI spec based on colour depth */
6676 switch (depth) {
6677 case COLOR_DEPTH_888:
6678 break;
6679 case COLOR_DEPTH_101010:
6680 normalized_clk = (normalized_clk * 30) / 24;
6681 break;
6682 case COLOR_DEPTH_121212:
6683 normalized_clk = (normalized_clk * 36) / 24;
6684 break;
6685 case COLOR_DEPTH_161616:
6686 normalized_clk = (normalized_clk * 48) / 24;
6687 break;
6688 default:
6689 /* The above depths are the only ones valid for HDMI. */
6690 return false;
6691 }
6692 if (normalized_clk <= info->max_tmds_clock) {
6693 timing_out->display_color_depth = depth;
6694 return true;
6695 }
6696 } while (--depth > COLOR_DEPTH_666);
6697 return false;
6698}
6699
6700static void fill_stream_properties_from_drm_display_mode(
6701 struct dc_stream_state *stream,
6702 const struct drm_display_mode *mode_in,
6703 const struct drm_connector *connector,
6704 const struct drm_connector_state *connector_state,
6705 const struct dc_stream_state *old_stream,
6706 int requested_bpc)
6707{
6708 struct dc_crtc_timing *timing_out = &stream->timing;
6709 const struct drm_display_info *info = &connector->display_info;
6710 struct amdgpu_dm_connector *aconnector = NULL;
6711 struct hdmi_vendor_infoframe hv_frame;
6712 struct hdmi_avi_infoframe avi_frame;
6713 ssize_t err;
6714
6715 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
6716 aconnector = to_amdgpu_dm_connector(connector);
6717
6718 memset(&hv_frame, 0, sizeof(hv_frame));
6719 memset(&avi_frame, 0, sizeof(avi_frame));
6720
6721 timing_out->h_border_left = 0;
6722 timing_out->h_border_right = 0;
6723 timing_out->v_border_top = 0;
6724 timing_out->v_border_bottom = 0;
6725 /* TODO: un-hardcode */
6726 if (drm_mode_is_420_only(info, mode_in)
6727 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
6728 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
6729 else if (drm_mode_is_420_also(info, mode_in)
6730 && aconnector
6731 && aconnector->force_yuv420_output)
6732 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
6733 else if ((connector->display_info.color_formats & BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR422))
6734 && aconnector
6735 && aconnector->force_yuv422_output)
6736 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR422;
6737 else if ((connector->display_info.color_formats & BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR444))
6738 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
6739 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
6740 else
6741 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
6742
6743 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
6744 timing_out->display_color_depth = convert_color_depth_from_display_info(
6745 connector,
6746 (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420),
6747 requested_bpc);
6748 timing_out->scan_type = SCANNING_TYPE_NODATA;
6749 timing_out->hdmi_vic = 0;
6750
6751 if (old_stream) {
6752 timing_out->vic = old_stream->timing.vic;
6753 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY;
6754 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY;
6755 } else {
6756 timing_out->vic = drm_match_cea_mode(mode_in);
6757 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
6758 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
6759 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
6760 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
6761 }
6762
6763 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
6764 err = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame,
6765 (struct drm_connector *)connector,
6766 mode_in);
6767 if (err < 0)
6768 drm_warn_once(connector->dev, "Failed to setup avi infoframe on connector %s: %zd\n",
6769 connector->name, err);
6770 timing_out->vic = avi_frame.video_code;
6771 err = drm_hdmi_vendor_infoframe_from_display_mode(&hv_frame,
6772 (struct drm_connector *)connector,
6773 mode_in);
6774 if (err < 0)
6775 drm_warn_once(connector->dev, "Failed to setup vendor infoframe on connector %s: %zd\n",
6776 connector->name, err);
6777 timing_out->hdmi_vic = hv_frame.vic;
6778 }
6779
6780 if (aconnector && is_freesync_video_mode(mode_in, aconnector)) {
6781 timing_out->h_addressable = mode_in->hdisplay;
6782 timing_out->h_total = mode_in->htotal;
6783 timing_out->h_sync_width = mode_in->hsync_end - mode_in->hsync_start;
6784 timing_out->h_front_porch = mode_in->hsync_start - mode_in->hdisplay;
6785 timing_out->v_total = mode_in->vtotal;
6786 timing_out->v_addressable = mode_in->vdisplay;
6787 timing_out->v_front_porch = mode_in->vsync_start - mode_in->vdisplay;
6788 timing_out->v_sync_width = mode_in->vsync_end - mode_in->vsync_start;
6789 timing_out->pix_clk_100hz = mode_in->clock * 10;
6790 } else {
6791 timing_out->h_addressable = mode_in->crtc_hdisplay;
6792 timing_out->h_total = mode_in->crtc_htotal;
6793 timing_out->h_sync_width = mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
6794 timing_out->h_front_porch = mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
6795 timing_out->v_total = mode_in->crtc_vtotal;
6796 timing_out->v_addressable = mode_in->crtc_vdisplay;
6797 timing_out->v_front_porch = mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
6798 timing_out->v_sync_width = mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
6799 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10;
6800 }
6801
6802 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
6803
6804 stream->out_transfer_func.type = TF_TYPE_PREDEFINED;
6805 stream->out_transfer_func.tf = TRANSFER_FUNCTION_SRGB;
6806 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
6807 if (!adjust_colour_depth_from_display_info(timing_out, info) &&
6808 drm_mode_is_420_also(info, mode_in) &&
6809 timing_out->pixel_encoding != PIXEL_ENCODING_YCBCR420) {
6810 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
6811 adjust_colour_depth_from_display_info(timing_out, info);
6812 }
6813 }
6814
6815 stream->output_color_space = get_output_color_space(timing_out, connector_state);
6816 stream->content_type = get_output_content_type(connector_state);
6817}
6818
6819static void fill_audio_info(struct audio_info *audio_info,
6820 const struct drm_connector *drm_connector,
6821 const struct dc_sink *dc_sink)
6822{
6823 int i = 0;
6824 int cea_revision = 0;
6825 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
6826
6827 audio_info->manufacture_id = edid_caps->manufacturer_id;
6828 audio_info->product_id = edid_caps->product_id;
6829
6830 cea_revision = drm_connector->display_info.cea_rev;
6831
6832 strscpy(audio_info->display_name,
6833 edid_caps->display_name,
6834 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
6835
6836 if (cea_revision >= 3) {
6837 audio_info->mode_count = edid_caps->audio_mode_count;
6838
6839 for (i = 0; i < audio_info->mode_count; ++i) {
6840 audio_info->modes[i].format_code =
6841 (enum audio_format_code)
6842 (edid_caps->audio_modes[i].format_code);
6843 audio_info->modes[i].channel_count =
6844 edid_caps->audio_modes[i].channel_count;
6845 audio_info->modes[i].sample_rates.all =
6846 edid_caps->audio_modes[i].sample_rate;
6847 audio_info->modes[i].sample_size =
6848 edid_caps->audio_modes[i].sample_size;
6849 }
6850 }
6851
6852 audio_info->flags.all = edid_caps->speaker_flags;
6853
6854 /* TODO: We only check for the progressive mode, check for interlace mode too */
6855 if (drm_connector->latency_present[0]) {
6856 audio_info->video_latency = drm_connector->video_latency[0];
6857 audio_info->audio_latency = drm_connector->audio_latency[0];
6858 }
6859
6860 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
6861
6862}
6863
6864static void
6865copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
6866 struct drm_display_mode *dst_mode)
6867{
6868 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
6869 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
6870 dst_mode->crtc_clock = src_mode->crtc_clock;
6871 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
6872 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
6873 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
6874 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
6875 dst_mode->crtc_htotal = src_mode->crtc_htotal;
6876 dst_mode->crtc_hskew = src_mode->crtc_hskew;
6877 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
6878 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
6879 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
6880 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
6881 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
6882}
6883
6884static void
6885decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
6886 const struct drm_display_mode *native_mode,
6887 bool scale_enabled)
6888{
6889 if (scale_enabled || (
6890 native_mode->clock == drm_mode->clock &&
6891 native_mode->htotal == drm_mode->htotal &&
6892 native_mode->vtotal == drm_mode->vtotal)) {
6893 if (native_mode->crtc_clock)
6894 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
6895 } else {
6896 /* no scaling nor amdgpu inserted, no need to patch */
6897 }
6898}
6899
6900static struct dc_sink *
6901create_fake_sink(struct drm_device *dev, struct dc_link *link)
6902{
6903 struct dc_sink_init_data sink_init_data = { 0 };
6904 struct dc_sink *sink = NULL;
6905
6906 sink_init_data.link = link;
6907 sink_init_data.sink_signal = link->connector_signal;
6908
6909 sink = dc_sink_create(&sink_init_data);
6910 if (!sink) {
6911 drm_err(dev, "Failed to create sink!\n");
6912 return NULL;
6913 }
6914 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
6915
6916 return sink;
6917}
6918
6919static void set_multisync_trigger_params(
6920 struct dc_stream_state *stream)
6921{
6922 struct dc_stream_state *master = NULL;
6923
6924 if (stream->triggered_crtc_reset.enabled) {
6925 master = stream->triggered_crtc_reset.event_source;
6926 stream->triggered_crtc_reset.event =
6927 master->timing.flags.VSYNC_POSITIVE_POLARITY ?
6928 CRTC_EVENT_VSYNC_RISING : CRTC_EVENT_VSYNC_FALLING;
6929 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_PIXEL;
6930 }
6931}
6932
6933static void set_master_stream(struct dc_stream_state *stream_set[],
6934 int stream_count)
6935{
6936 int j, highest_rfr = 0, master_stream = 0;
6937
6938 for (j = 0; j < stream_count; j++) {
6939 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
6940 int refresh_rate = 0;
6941
6942 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/
6943 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
6944 if (refresh_rate > highest_rfr) {
6945 highest_rfr = refresh_rate;
6946 master_stream = j;
6947 }
6948 }
6949 }
6950 for (j = 0; j < stream_count; j++) {
6951 if (stream_set[j])
6952 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
6953 }
6954}
6955
6956static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
6957{
6958 int i = 0;
6959 struct dc_stream_state *stream;
6960
6961 if (context->stream_count < 2)
6962 return;
6963 for (i = 0; i < context->stream_count ; i++) {
6964 if (!context->streams[i])
6965 continue;
6966 /*
6967 * TODO: add a function to read AMD VSDB bits and set
6968 * crtc_sync_master.multi_sync_enabled flag
6969 * For now it's set to false
6970 */
6971 }
6972
6973 set_master_stream(context->streams, context->stream_count);
6974
6975 for (i = 0; i < context->stream_count ; i++) {
6976 stream = context->streams[i];
6977
6978 if (!stream)
6979 continue;
6980
6981 set_multisync_trigger_params(stream);
6982 }
6983}
6984
6985/**
6986 * DOC: FreeSync Video
6987 *
6988 * When a userspace application wants to play a video, the content follows a
6989 * standard format definition that usually specifies the FPS for that format.
6990 * The below list illustrates some video format and the expected FPS,
6991 * respectively:
6992 *
6993 * - TV/NTSC (23.976 FPS)
6994 * - Cinema (24 FPS)
6995 * - TV/PAL (25 FPS)
6996 * - TV/NTSC (29.97 FPS)
6997 * - TV/NTSC (30 FPS)
6998 * - Cinema HFR (48 FPS)
6999 * - TV/PAL (50 FPS)
7000 * - Commonly used (60 FPS)
7001 * - Multiples of 24 (48,72,96 FPS)
7002 *
7003 * The list of standards video format is not huge and can be added to the
7004 * connector modeset list beforehand. With that, userspace can leverage
7005 * FreeSync to extends the front porch in order to attain the target refresh
7006 * rate. Such a switch will happen seamlessly, without screen blanking or
7007 * reprogramming of the output in any other way. If the userspace requests a
7008 * modesetting change compatible with FreeSync modes that only differ in the
7009 * refresh rate, DC will skip the full update and avoid blink during the
7010 * transition. For example, the video player can change the modesetting from
7011 * 60Hz to 30Hz for playing TV/NTSC content when it goes full screen without
7012 * causing any display blink. This same concept can be applied to a mode
7013 * setting change.
7014 */
7015static struct drm_display_mode *
7016get_highest_refresh_rate_mode(struct amdgpu_dm_connector *aconnector,
7017 bool use_probed_modes)
7018{
7019 struct drm_display_mode *m, *m_pref = NULL;
7020 u16 current_refresh, highest_refresh;
7021 struct list_head *list_head = use_probed_modes ?
7022 &aconnector->base.probed_modes :
7023 &aconnector->base.modes;
7024
7025 if (aconnector->base.connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
7026 return NULL;
7027
7028 if (aconnector->freesync_vid_base.clock != 0)
7029 return &aconnector->freesync_vid_base;
7030
7031 /* Find the preferred mode */
7032 list_for_each_entry(m, list_head, head) {
7033 if (m->type & DRM_MODE_TYPE_PREFERRED) {
7034 m_pref = m;
7035 break;
7036 }
7037 }
7038
7039 if (!m_pref) {
7040 /* Probably an EDID with no preferred mode. Fallback to first entry */
7041 m_pref = list_first_entry_or_null(
7042 &aconnector->base.modes, struct drm_display_mode, head);
7043 if (!m_pref) {
7044 drm_dbg_driver(aconnector->base.dev, "No preferred mode found in EDID\n");
7045 return NULL;
7046 }
7047 }
7048
7049 highest_refresh = drm_mode_vrefresh(m_pref);
7050
7051 /*
7052 * Find the mode with highest refresh rate with same resolution.
7053 * For some monitors, preferred mode is not the mode with highest
7054 * supported refresh rate.
7055 */
7056 list_for_each_entry(m, list_head, head) {
7057 current_refresh = drm_mode_vrefresh(m);
7058
7059 if (m->hdisplay == m_pref->hdisplay &&
7060 m->vdisplay == m_pref->vdisplay &&
7061 highest_refresh < current_refresh) {
7062 highest_refresh = current_refresh;
7063 m_pref = m;
7064 }
7065 }
7066
7067 drm_mode_copy(&aconnector->freesync_vid_base, m_pref);
7068 return m_pref;
7069}
7070
7071static bool is_freesync_video_mode(const struct drm_display_mode *mode,
7072 struct amdgpu_dm_connector *aconnector)
7073{
7074 struct drm_display_mode *high_mode;
7075 int timing_diff;
7076
7077 high_mode = get_highest_refresh_rate_mode(aconnector, false);
7078 if (!high_mode || !mode)
7079 return false;
7080
7081 timing_diff = high_mode->vtotal - mode->vtotal;
7082
7083 if (high_mode->clock == 0 || high_mode->clock != mode->clock ||
7084 high_mode->hdisplay != mode->hdisplay ||
7085 high_mode->vdisplay != mode->vdisplay ||
7086 high_mode->hsync_start != mode->hsync_start ||
7087 high_mode->hsync_end != mode->hsync_end ||
7088 high_mode->htotal != mode->htotal ||
7089 high_mode->hskew != mode->hskew ||
7090 high_mode->vscan != mode->vscan ||
7091 high_mode->vsync_start - mode->vsync_start != timing_diff ||
7092 high_mode->vsync_end - mode->vsync_end != timing_diff)
7093 return false;
7094 else
7095 return true;
7096}
7097
7098#if defined(CONFIG_DRM_AMD_DC_FP)
7099static void update_dsc_caps(struct amdgpu_dm_connector *aconnector,
7100 struct dc_sink *sink, struct dc_stream_state *stream,
7101 struct dsc_dec_dpcd_caps *dsc_caps)
7102{
7103 stream->timing.flags.DSC = 0;
7104 dsc_caps->is_dsc_supported = false;
7105
7106 if (aconnector->dc_link && (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT ||
7107 sink->sink_signal == SIGNAL_TYPE_EDP)) {
7108 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE ||
7109 sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER)
7110 dc_dsc_parse_dsc_dpcd(aconnector->dc_link->ctx->dc,
7111 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.raw,
7112 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.raw,
7113 dsc_caps);
7114 }
7115}
7116
7117static void apply_dsc_policy_for_edp(struct amdgpu_dm_connector *aconnector,
7118 struct dc_sink *sink, struct dc_stream_state *stream,
7119 struct dsc_dec_dpcd_caps *dsc_caps,
7120 uint32_t max_dsc_target_bpp_limit_override)
7121{
7122 const struct dc_link_settings *verified_link_cap = NULL;
7123 u32 link_bw_in_kbps;
7124 u32 edp_min_bpp_x16, edp_max_bpp_x16;
7125 struct dc *dc = sink->ctx->dc;
7126 struct dc_dsc_bw_range bw_range = {0};
7127 struct dc_dsc_config dsc_cfg = {0};
7128 struct dc_dsc_config_options dsc_options = {0};
7129
7130 dc_dsc_get_default_config_option(dc, &dsc_options);
7131 dsc_options.max_target_bpp_limit_override_x16 = max_dsc_target_bpp_limit_override * 16;
7132
7133 verified_link_cap = dc_link_get_link_cap(stream->link);
7134 link_bw_in_kbps = dc_link_bandwidth_kbps(stream->link, verified_link_cap);
7135 edp_min_bpp_x16 = 8 * 16;
7136 edp_max_bpp_x16 = 8 * 16;
7137
7138 if (edp_max_bpp_x16 > dsc_caps->edp_max_bits_per_pixel)
7139 edp_max_bpp_x16 = dsc_caps->edp_max_bits_per_pixel;
7140
7141 if (edp_max_bpp_x16 < edp_min_bpp_x16)
7142 edp_min_bpp_x16 = edp_max_bpp_x16;
7143
7144 if (dc_dsc_compute_bandwidth_range(dc->res_pool->dscs[0],
7145 dc->debug.dsc_min_slice_height_override,
7146 edp_min_bpp_x16, edp_max_bpp_x16,
7147 dsc_caps,
7148 &stream->timing,
7149 dc_link_get_highest_encoding_format(aconnector->dc_link),
7150 &bw_range)) {
7151
7152 if (bw_range.max_kbps < link_bw_in_kbps) {
7153 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
7154 dsc_caps,
7155 &dsc_options,
7156 0,
7157 &stream->timing,
7158 dc_link_get_highest_encoding_format(aconnector->dc_link),
7159 &dsc_cfg)) {
7160 stream->timing.dsc_cfg = dsc_cfg;
7161 stream->timing.flags.DSC = 1;
7162 stream->timing.dsc_cfg.bits_per_pixel = edp_max_bpp_x16;
7163 }
7164 return;
7165 }
7166 }
7167
7168 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
7169 dsc_caps,
7170 &dsc_options,
7171 link_bw_in_kbps,
7172 &stream->timing,
7173 dc_link_get_highest_encoding_format(aconnector->dc_link),
7174 &dsc_cfg)) {
7175 stream->timing.dsc_cfg = dsc_cfg;
7176 stream->timing.flags.DSC = 1;
7177 }
7178}
7179
7180static void apply_dsc_policy_for_stream(struct amdgpu_dm_connector *aconnector,
7181 struct dc_sink *sink, struct dc_stream_state *stream,
7182 struct dsc_dec_dpcd_caps *dsc_caps)
7183{
7184 struct drm_connector *drm_connector = &aconnector->base;
7185 u32 link_bandwidth_kbps;
7186 struct dc *dc = sink->ctx->dc;
7187 u32 max_supported_bw_in_kbps, timing_bw_in_kbps;
7188 u32 dsc_max_supported_bw_in_kbps;
7189 u32 max_dsc_target_bpp_limit_override =
7190 drm_connector->display_info.max_dsc_bpp;
7191 struct dc_dsc_config_options dsc_options = {0};
7192
7193 dc_dsc_get_default_config_option(dc, &dsc_options);
7194 dsc_options.max_target_bpp_limit_override_x16 = max_dsc_target_bpp_limit_override * 16;
7195
7196 link_bandwidth_kbps = dc_link_bandwidth_kbps(aconnector->dc_link,
7197 dc_link_get_link_cap(aconnector->dc_link));
7198
7199 /* Set DSC policy according to dsc_clock_en */
7200 dc_dsc_policy_set_enable_dsc_when_not_needed(
7201 aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE);
7202
7203 if (sink->sink_signal == SIGNAL_TYPE_EDP &&
7204 !aconnector->dc_link->panel_config.dsc.disable_dsc_edp &&
7205 dc->caps.edp_dsc_support && aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE) {
7206
7207 apply_dsc_policy_for_edp(aconnector, sink, stream, dsc_caps, max_dsc_target_bpp_limit_override);
7208
7209 } else if (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT) {
7210 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE) {
7211 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
7212 dsc_caps,
7213 &dsc_options,
7214 link_bandwidth_kbps,
7215 &stream->timing,
7216 dc_link_get_highest_encoding_format(aconnector->dc_link),
7217 &stream->timing.dsc_cfg)) {
7218 stream->timing.flags.DSC = 1;
7219 drm_dbg_driver(drm_connector->dev, "%s: SST_DSC [%s] DSC is selected from SST RX\n",
7220 __func__, drm_connector->name);
7221 }
7222 } else if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER) {
7223 timing_bw_in_kbps = dc_bandwidth_in_kbps_from_timing(&stream->timing,
7224 dc_link_get_highest_encoding_format(aconnector->dc_link));
7225 max_supported_bw_in_kbps = link_bandwidth_kbps;
7226 dsc_max_supported_bw_in_kbps = link_bandwidth_kbps;
7227
7228 if (timing_bw_in_kbps > max_supported_bw_in_kbps &&
7229 max_supported_bw_in_kbps > 0 &&
7230 dsc_max_supported_bw_in_kbps > 0)
7231 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
7232 dsc_caps,
7233 &dsc_options,
7234 dsc_max_supported_bw_in_kbps,
7235 &stream->timing,
7236 dc_link_get_highest_encoding_format(aconnector->dc_link),
7237 &stream->timing.dsc_cfg)) {
7238 stream->timing.flags.DSC = 1;
7239 drm_dbg_driver(drm_connector->dev, "%s: SST_DSC [%s] DSC is selected from DP-HDMI PCON\n",
7240 __func__, drm_connector->name);
7241 }
7242 }
7243 }
7244
7245 /* Overwrite the stream flag if DSC is enabled through debugfs */
7246 if (aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE)
7247 stream->timing.flags.DSC = 1;
7248
7249 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_h)
7250 stream->timing.dsc_cfg.num_slices_h = aconnector->dsc_settings.dsc_num_slices_h;
7251
7252 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_v)
7253 stream->timing.dsc_cfg.num_slices_v = aconnector->dsc_settings.dsc_num_slices_v;
7254
7255 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_bits_per_pixel)
7256 stream->timing.dsc_cfg.bits_per_pixel = aconnector->dsc_settings.dsc_bits_per_pixel;
7257}
7258#endif
7259
7260static struct dc_stream_state *
7261create_stream_for_sink(struct drm_connector *connector,
7262 const struct drm_display_mode *drm_mode,
7263 const struct dm_connector_state *dm_state,
7264 const struct dc_stream_state *old_stream,
7265 int requested_bpc)
7266{
7267 struct drm_device *dev = connector->dev;
7268 struct amdgpu_dm_connector *aconnector = NULL;
7269 struct drm_display_mode *preferred_mode = NULL;
7270 const struct drm_connector_state *con_state = &dm_state->base;
7271 struct dc_stream_state *stream = NULL;
7272 struct drm_display_mode mode;
7273 struct drm_display_mode saved_mode;
7274 struct drm_display_mode *freesync_mode = NULL;
7275 bool native_mode_found = false;
7276 bool recalculate_timing = false;
7277 bool scale = dm_state->scaling != RMX_OFF;
7278 int mode_refresh;
7279 int preferred_refresh = 0;
7280 enum color_transfer_func tf = TRANSFER_FUNC_UNKNOWN;
7281#if defined(CONFIG_DRM_AMD_DC_FP)
7282 struct dsc_dec_dpcd_caps dsc_caps;
7283#endif
7284 struct dc_link *link = NULL;
7285 struct dc_sink *sink = NULL;
7286
7287 drm_mode_init(&mode, drm_mode);
7288 memset(&saved_mode, 0, sizeof(saved_mode));
7289
7290 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) {
7291 aconnector = NULL;
7292 aconnector = to_amdgpu_dm_connector(connector);
7293 link = aconnector->dc_link;
7294 } else {
7295 struct drm_writeback_connector *wbcon = NULL;
7296 struct amdgpu_dm_wb_connector *dm_wbcon = NULL;
7297
7298 wbcon = drm_connector_to_writeback(connector);
7299 dm_wbcon = to_amdgpu_dm_wb_connector(wbcon);
7300 link = dm_wbcon->link;
7301 }
7302
7303 if (!aconnector || !aconnector->dc_sink) {
7304 sink = create_fake_sink(dev, link);
7305 if (!sink)
7306 return stream;
7307
7308 } else {
7309 sink = aconnector->dc_sink;
7310 dc_sink_retain(sink);
7311 }
7312
7313 stream = dc_create_stream_for_sink(sink);
7314
7315 if (stream == NULL) {
7316 drm_err(dev, "Failed to create stream for sink!\n");
7317 goto finish;
7318 }
7319
7320 /* We leave this NULL for writeback connectors */
7321 stream->dm_stream_context = aconnector;
7322
7323 stream->timing.flags.LTE_340MCSC_SCRAMBLE =
7324 connector->display_info.hdmi.scdc.scrambling.low_rates;
7325
7326 list_for_each_entry(preferred_mode, &connector->modes, head) {
7327 /* Search for preferred mode */
7328 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
7329 native_mode_found = true;
7330 break;
7331 }
7332 }
7333 if (!native_mode_found)
7334 preferred_mode = list_first_entry_or_null(
7335 &connector->modes,
7336 struct drm_display_mode,
7337 head);
7338
7339 mode_refresh = drm_mode_vrefresh(&mode);
7340
7341 if (preferred_mode == NULL) {
7342 /*
7343 * This may not be an error, the use case is when we have no
7344 * usermode calls to reset and set mode upon hotplug. In this
7345 * case, we call set mode ourselves to restore the previous mode
7346 * and the modelist may not be filled in time.
7347 */
7348 drm_dbg_driver(dev, "No preferred mode found\n");
7349 } else if (aconnector) {
7350 recalculate_timing = amdgpu_freesync_vid_mode &&
7351 is_freesync_video_mode(&mode, aconnector);
7352 if (recalculate_timing) {
7353 freesync_mode = get_highest_refresh_rate_mode(aconnector, false);
7354 drm_mode_copy(&saved_mode, &mode);
7355 saved_mode.picture_aspect_ratio = mode.picture_aspect_ratio;
7356 drm_mode_copy(&mode, freesync_mode);
7357 mode.picture_aspect_ratio = saved_mode.picture_aspect_ratio;
7358 } else {
7359 decide_crtc_timing_for_drm_display_mode(
7360 &mode, preferred_mode, scale);
7361
7362 preferred_refresh = drm_mode_vrefresh(preferred_mode);
7363 }
7364 }
7365
7366 if (recalculate_timing)
7367 drm_mode_set_crtcinfo(&saved_mode, 0);
7368
7369 /*
7370 * If scaling is enabled and refresh rate didn't change
7371 * we copy the vic and polarities of the old timings
7372 */
7373 if (!scale || mode_refresh != preferred_refresh)
7374 fill_stream_properties_from_drm_display_mode(
7375 stream, &mode, connector, con_state, NULL,
7376 requested_bpc);
7377 else
7378 fill_stream_properties_from_drm_display_mode(
7379 stream, &mode, connector, con_state, old_stream,
7380 requested_bpc);
7381
7382 /* The rest isn't needed for writeback connectors */
7383 if (!aconnector)
7384 goto finish;
7385
7386 if (aconnector->timing_changed) {
7387 drm_dbg(aconnector->base.dev,
7388 "overriding timing for automated test, bpc %d, changing to %d\n",
7389 stream->timing.display_color_depth,
7390 aconnector->timing_requested->display_color_depth);
7391 stream->timing = *aconnector->timing_requested;
7392 }
7393
7394#if defined(CONFIG_DRM_AMD_DC_FP)
7395 /* SST DSC determination policy */
7396 update_dsc_caps(aconnector, sink, stream, &dsc_caps);
7397 if (aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE && dsc_caps.is_dsc_supported)
7398 apply_dsc_policy_for_stream(aconnector, sink, stream, &dsc_caps);
7399#endif
7400
7401 update_stream_scaling_settings(dev, &mode, dm_state, stream);
7402
7403 fill_audio_info(
7404 &stream->audio_info,
7405 connector,
7406 sink);
7407
7408 update_stream_signal(stream, sink);
7409
7410 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
7411 mod_build_hf_vsif_infopacket(stream, &stream->vsp_infopacket);
7412
7413 if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT ||
7414 stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST ||
7415 stream->signal == SIGNAL_TYPE_EDP) {
7416 const struct dc_edid_caps *edid_caps;
7417 unsigned int disable_colorimetry = 0;
7418
7419 if (aconnector->dc_sink) {
7420 edid_caps = &aconnector->dc_sink->edid_caps;
7421 disable_colorimetry = edid_caps->panel_patch.disable_colorimetry;
7422 }
7423
7424 //
7425 // should decide stream support vsc sdp colorimetry capability
7426 // before building vsc info packet
7427 //
7428 stream->use_vsc_sdp_for_colorimetry = stream->link->dpcd_caps.dpcd_rev.raw >= 0x14 &&
7429 stream->link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED &&
7430 !disable_colorimetry;
7431
7432 if (stream->out_transfer_func.tf == TRANSFER_FUNCTION_GAMMA22)
7433 tf = TRANSFER_FUNC_GAMMA_22;
7434 mod_build_vsc_infopacket(stream, &stream->vsc_infopacket, stream->output_color_space, tf);
7435 aconnector->sr_skip_count = AMDGPU_DM_PSR_ENTRY_DELAY;
7436
7437 }
7438finish:
7439 dc_sink_release(sink);
7440
7441 return stream;
7442}
7443
7444/**
7445 * amdgpu_dm_connector_poll - Poll a connector to see if it's connected to a display
7446 * @aconnector: DM connector to poll (owns @base drm_connector and @dc_link)
7447 * @force: if true, force polling even when DAC load detection was used
7448 *
7449 * Used for connectors that don't support HPD (hotplug detection) to
7450 * periodically check whether the connector is connected to a display.
7451 *
7452 * When connection was determined via DAC load detection, we avoid
7453 * re-running it on normal polls to prevent visible glitches, unless
7454 * @force is set.
7455 *
7456 * Return: The probed connector status (connected/disconnected/unknown).
7457 */
7458static enum drm_connector_status
7459amdgpu_dm_connector_poll(struct amdgpu_dm_connector *aconnector, bool force)
7460{
7461 struct drm_connector *connector = &aconnector->base;
7462 struct drm_device *dev = connector->dev;
7463 struct amdgpu_device *adev = drm_to_adev(dev);
7464 struct dc_link *link = aconnector->dc_link;
7465 enum dc_connection_type conn_type = dc_connection_none;
7466 enum drm_connector_status status = connector_status_disconnected;
7467
7468 /* When we determined the connection using DAC load detection,
7469 * do NOT poll the connector do detect disconnect because
7470 * that would run DAC load detection again which can cause
7471 * visible visual glitches.
7472 *
7473 * Only allow to poll such a connector again when forcing.
7474 */
7475 if (!force && link->local_sink && link->type == dc_connection_analog_load)
7476 return connector->status;
7477
7478 mutex_lock(&aconnector->hpd_lock);
7479
7480 if (dc_link_detect_connection_type(aconnector->dc_link, &conn_type) &&
7481 conn_type != dc_connection_none) {
7482 mutex_lock(&adev->dm.dc_lock);
7483
7484 /* Only call full link detection when a sink isn't created yet,
7485 * ie. just when the display is plugged in, otherwise we risk flickering.
7486 */
7487 if (link->local_sink ||
7488 dc_link_detect(link, DETECT_REASON_HPD))
7489 status = connector_status_connected;
7490
7491 mutex_unlock(&adev->dm.dc_lock);
7492 }
7493
7494 if (connector->status != status) {
7495 if (status == connector_status_disconnected) {
7496 if (link->local_sink)
7497 dc_sink_release(link->local_sink);
7498
7499 link->local_sink = NULL;
7500 link->dpcd_sink_count = 0;
7501 link->type = dc_connection_none;
7502 }
7503
7504 amdgpu_dm_update_connector_after_detect(aconnector);
7505 }
7506
7507 mutex_unlock(&aconnector->hpd_lock);
7508 return status;
7509}
7510
7511/**
7512 * amdgpu_dm_connector_detect() - Detect whether a DRM connector is connected to a display
7513 *
7514 * A connector is considered connected when it has a sink that is not NULL.
7515 * For connectors that support HPD (hotplug detection), the connection is
7516 * handled in the HPD interrupt.
7517 * For connectors that may not support HPD, such as analog connectors,
7518 * DRM will call this function repeatedly to poll them.
7519 *
7520 * Notes:
7521 * 1. This interface is NOT called in context of HPD irq.
7522 * 2. This interface *is called* in context of user-mode ioctl. Which
7523 * makes it a bad place for *any* MST-related activity.
7524 *
7525 * @connector: The DRM connector we are checking. We convert it to
7526 * amdgpu_dm_connector so we can read the DC link and state.
7527 * @force: If true, do a full detect again. This is used even when
7528 * a lighter check would normally be used to avoid flicker.
7529 *
7530 * Return: The connector status (connected, disconnected, or unknown).
7531 *
7532 */
7533static enum drm_connector_status
7534amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
7535{
7536 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7537
7538 update_subconnector_property(aconnector);
7539
7540 if (aconnector->base.force == DRM_FORCE_ON ||
7541 aconnector->base.force == DRM_FORCE_ON_DIGITAL)
7542 return connector_status_connected;
7543 else if (aconnector->base.force == DRM_FORCE_OFF)
7544 return connector_status_disconnected;
7545
7546 /* Poll analog connectors and only when either
7547 * disconnected or connected to an analog display.
7548 */
7549 if (drm_kms_helper_is_poll_worker() &&
7550 dc_connector_supports_analog(aconnector->dc_link->link_id.id) &&
7551 (!aconnector->dc_sink || aconnector->dc_sink->edid_caps.analog))
7552 return amdgpu_dm_connector_poll(aconnector, force);
7553
7554 return (aconnector->dc_sink ? connector_status_connected :
7555 connector_status_disconnected);
7556}
7557
7558int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
7559 struct drm_connector_state *connector_state,
7560 struct drm_property *property,
7561 uint64_t val)
7562{
7563 struct drm_device *dev = connector->dev;
7564 struct amdgpu_device *adev = drm_to_adev(dev);
7565 struct dm_connector_state *dm_old_state =
7566 to_dm_connector_state(connector->state);
7567 struct dm_connector_state *dm_new_state =
7568 to_dm_connector_state(connector_state);
7569
7570 int ret = -EINVAL;
7571
7572 if (property == dev->mode_config.scaling_mode_property) {
7573 enum amdgpu_rmx_type rmx_type;
7574
7575 switch (val) {
7576 case DRM_MODE_SCALE_CENTER:
7577 rmx_type = RMX_CENTER;
7578 break;
7579 case DRM_MODE_SCALE_ASPECT:
7580 rmx_type = RMX_ASPECT;
7581 break;
7582 case DRM_MODE_SCALE_FULLSCREEN:
7583 rmx_type = RMX_FULL;
7584 break;
7585 case DRM_MODE_SCALE_NONE:
7586 default:
7587 rmx_type = RMX_OFF;
7588 break;
7589 }
7590
7591 if (dm_old_state->scaling == rmx_type)
7592 return 0;
7593
7594 dm_new_state->scaling = rmx_type;
7595 ret = 0;
7596 } else if (property == adev->mode_info.underscan_hborder_property) {
7597 dm_new_state->underscan_hborder = val;
7598 ret = 0;
7599 } else if (property == adev->mode_info.underscan_vborder_property) {
7600 dm_new_state->underscan_vborder = val;
7601 ret = 0;
7602 } else if (property == adev->mode_info.underscan_property) {
7603 dm_new_state->underscan_enable = val;
7604 ret = 0;
7605 } else if (property == adev->mode_info.abm_level_property) {
7606 switch (val) {
7607 case ABM_SYSFS_CONTROL:
7608 dm_new_state->abm_sysfs_forbidden = false;
7609 break;
7610 case ABM_LEVEL_OFF:
7611 dm_new_state->abm_sysfs_forbidden = true;
7612 dm_new_state->abm_level = ABM_LEVEL_IMMEDIATE_DISABLE;
7613 break;
7614 default:
7615 dm_new_state->abm_sysfs_forbidden = true;
7616 dm_new_state->abm_level = val;
7617 }
7618 ret = 0;
7619 }
7620
7621 return ret;
7622}
7623
7624int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
7625 const struct drm_connector_state *state,
7626 struct drm_property *property,
7627 uint64_t *val)
7628{
7629 struct drm_device *dev = connector->dev;
7630 struct amdgpu_device *adev = drm_to_adev(dev);
7631 struct dm_connector_state *dm_state =
7632 to_dm_connector_state(state);
7633 int ret = -EINVAL;
7634
7635 if (property == dev->mode_config.scaling_mode_property) {
7636 switch (dm_state->scaling) {
7637 case RMX_CENTER:
7638 *val = DRM_MODE_SCALE_CENTER;
7639 break;
7640 case RMX_ASPECT:
7641 *val = DRM_MODE_SCALE_ASPECT;
7642 break;
7643 case RMX_FULL:
7644 *val = DRM_MODE_SCALE_FULLSCREEN;
7645 break;
7646 case RMX_OFF:
7647 default:
7648 *val = DRM_MODE_SCALE_NONE;
7649 break;
7650 }
7651 ret = 0;
7652 } else if (property == adev->mode_info.underscan_hborder_property) {
7653 *val = dm_state->underscan_hborder;
7654 ret = 0;
7655 } else if (property == adev->mode_info.underscan_vborder_property) {
7656 *val = dm_state->underscan_vborder;
7657 ret = 0;
7658 } else if (property == adev->mode_info.underscan_property) {
7659 *val = dm_state->underscan_enable;
7660 ret = 0;
7661 } else if (property == adev->mode_info.abm_level_property) {
7662 if (!dm_state->abm_sysfs_forbidden)
7663 *val = ABM_SYSFS_CONTROL;
7664 else
7665 *val = (dm_state->abm_level != ABM_LEVEL_IMMEDIATE_DISABLE) ?
7666 dm_state->abm_level : 0;
7667 ret = 0;
7668 }
7669
7670 return ret;
7671}
7672
7673/**
7674 * DOC: panel power savings
7675 *
7676 * The display manager allows you to set your desired **panel power savings**
7677 * level (between 0-4, with 0 representing off), e.g. using the following::
7678 *
7679 * # echo 3 > /sys/class/drm/card0-eDP-1/amdgpu/panel_power_savings
7680 *
7681 * Modifying this value can have implications on color accuracy, so tread
7682 * carefully.
7683 */
7684
7685static ssize_t panel_power_savings_show(struct device *device,
7686 struct device_attribute *attr,
7687 char *buf)
7688{
7689 struct drm_connector *connector = dev_get_drvdata(device);
7690 struct drm_device *dev = connector->dev;
7691 u8 val;
7692
7693 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
7694 val = to_dm_connector_state(connector->state)->abm_level ==
7695 ABM_LEVEL_IMMEDIATE_DISABLE ? 0 :
7696 to_dm_connector_state(connector->state)->abm_level;
7697 drm_modeset_unlock(&dev->mode_config.connection_mutex);
7698
7699 return sysfs_emit(buf, "%u\n", val);
7700}
7701
7702static ssize_t panel_power_savings_store(struct device *device,
7703 struct device_attribute *attr,
7704 const char *buf, size_t count)
7705{
7706 struct drm_connector *connector = dev_get_drvdata(device);
7707 struct drm_device *dev = connector->dev;
7708 long val;
7709 int ret;
7710
7711 ret = kstrtol(buf, 0, &val);
7712
7713 if (ret)
7714 return ret;
7715
7716 if (val < 0 || val > 4)
7717 return -EINVAL;
7718
7719 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
7720 if (to_dm_connector_state(connector->state)->abm_sysfs_forbidden)
7721 ret = -EBUSY;
7722 else
7723 to_dm_connector_state(connector->state)->abm_level = val ?:
7724 ABM_LEVEL_IMMEDIATE_DISABLE;
7725 drm_modeset_unlock(&dev->mode_config.connection_mutex);
7726
7727 if (ret)
7728 return ret;
7729
7730 drm_kms_helper_hotplug_event(dev);
7731
7732 return count;
7733}
7734
7735static DEVICE_ATTR_RW(panel_power_savings);
7736
7737static struct attribute *amdgpu_attrs[] = {
7738 &dev_attr_panel_power_savings.attr,
7739 NULL
7740};
7741
7742static const struct attribute_group amdgpu_group = {
7743 .name = "amdgpu",
7744 .attrs = amdgpu_attrs
7745};
7746
7747static bool
7748amdgpu_dm_should_create_sysfs(struct amdgpu_dm_connector *amdgpu_dm_connector)
7749{
7750 if (amdgpu_dm_abm_level >= 0)
7751 return false;
7752
7753 if (amdgpu_dm_connector->base.connector_type != DRM_MODE_CONNECTOR_eDP)
7754 return false;
7755
7756 /* check for OLED panels */
7757 if (amdgpu_dm_connector->bl_idx >= 0) {
7758 struct drm_device *drm = amdgpu_dm_connector->base.dev;
7759 struct amdgpu_display_manager *dm = &drm_to_adev(drm)->dm;
7760 struct amdgpu_dm_backlight_caps *caps;
7761
7762 caps = &dm->backlight_caps[amdgpu_dm_connector->bl_idx];
7763 if (caps->aux_support)
7764 return false;
7765 }
7766
7767 return true;
7768}
7769
7770static void amdgpu_dm_connector_unregister(struct drm_connector *connector)
7771{
7772 struct amdgpu_dm_connector *amdgpu_dm_connector = to_amdgpu_dm_connector(connector);
7773
7774 if (amdgpu_dm_should_create_sysfs(amdgpu_dm_connector))
7775 sysfs_remove_group(&connector->kdev->kobj, &amdgpu_group);
7776
7777 cec_notifier_conn_unregister(amdgpu_dm_connector->notifier);
7778 drm_dp_aux_unregister(&amdgpu_dm_connector->dm_dp_aux.aux);
7779}
7780
7781static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
7782{
7783 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7784 struct amdgpu_device *adev = drm_to_adev(connector->dev);
7785 struct amdgpu_display_manager *dm = &adev->dm;
7786
7787 /*
7788 * Call only if mst_mgr was initialized before since it's not done
7789 * for all connector types.
7790 */
7791 if (aconnector->mst_mgr.dev)
7792 drm_dp_mst_topology_mgr_destroy(&aconnector->mst_mgr);
7793
7794 /* Cancel and flush any pending HDMI HPD debounce work */
7795 if (aconnector->hdmi_hpd_debounce_delay_ms) {
7796 cancel_delayed_work_sync(&aconnector->hdmi_hpd_debounce_work);
7797 if (aconnector->hdmi_prev_sink) {
7798 dc_sink_release(aconnector->hdmi_prev_sink);
7799 aconnector->hdmi_prev_sink = NULL;
7800 }
7801 }
7802
7803 if (aconnector->bl_idx != -1) {
7804 backlight_device_unregister(dm->backlight_dev[aconnector->bl_idx]);
7805 dm->backlight_dev[aconnector->bl_idx] = NULL;
7806 }
7807
7808 if (aconnector->dc_em_sink)
7809 dc_sink_release(aconnector->dc_em_sink);
7810 aconnector->dc_em_sink = NULL;
7811 if (aconnector->dc_sink)
7812 dc_sink_release(aconnector->dc_sink);
7813 aconnector->dc_sink = NULL;
7814
7815 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
7816 drm_connector_unregister(connector);
7817 drm_connector_cleanup(connector);
7818 kfree(aconnector->dm_dp_aux.aux.name);
7819
7820 kfree(connector);
7821}
7822
7823void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
7824{
7825 struct dm_connector_state *state =
7826 to_dm_connector_state(connector->state);
7827
7828 if (connector->state)
7829 __drm_atomic_helper_connector_destroy_state(connector->state);
7830
7831 kfree(state);
7832
7833 state = kzalloc_obj(*state);
7834
7835 if (state) {
7836 state->scaling = RMX_OFF;
7837 state->underscan_enable = false;
7838 state->underscan_hborder = 0;
7839 state->underscan_vborder = 0;
7840 state->base.max_requested_bpc = 8;
7841 state->vcpi_slots = 0;
7842 state->pbn = 0;
7843
7844 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
7845 if (amdgpu_dm_abm_level <= 0)
7846 state->abm_level = ABM_LEVEL_IMMEDIATE_DISABLE;
7847 else
7848 state->abm_level = amdgpu_dm_abm_level;
7849 }
7850
7851 __drm_atomic_helper_connector_reset(connector, &state->base);
7852 }
7853}
7854
7855struct drm_connector_state *
7856amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
7857{
7858 struct dm_connector_state *state =
7859 to_dm_connector_state(connector->state);
7860
7861 struct dm_connector_state *new_state =
7862 kmemdup(state, sizeof(*state), GFP_KERNEL);
7863
7864 if (!new_state)
7865 return NULL;
7866
7867 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
7868
7869 new_state->freesync_capable = state->freesync_capable;
7870 new_state->abm_level = state->abm_level;
7871 new_state->scaling = state->scaling;
7872 new_state->underscan_enable = state->underscan_enable;
7873 new_state->underscan_hborder = state->underscan_hborder;
7874 new_state->underscan_vborder = state->underscan_vborder;
7875 new_state->vcpi_slots = state->vcpi_slots;
7876 new_state->pbn = state->pbn;
7877 return &new_state->base;
7878}
7879
7880static int
7881amdgpu_dm_connector_late_register(struct drm_connector *connector)
7882{
7883 struct amdgpu_dm_connector *amdgpu_dm_connector =
7884 to_amdgpu_dm_connector(connector);
7885 int r;
7886
7887 if (amdgpu_dm_should_create_sysfs(amdgpu_dm_connector)) {
7888 r = sysfs_create_group(&connector->kdev->kobj,
7889 &amdgpu_group);
7890 if (r)
7891 return r;
7892 }
7893
7894 amdgpu_dm_register_backlight_device(amdgpu_dm_connector);
7895
7896 if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
7897 (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) {
7898 amdgpu_dm_connector->dm_dp_aux.aux.dev = connector->kdev;
7899 r = drm_dp_aux_register(&amdgpu_dm_connector->dm_dp_aux.aux);
7900 if (r)
7901 return r;
7902 }
7903
7904#if defined(CONFIG_DEBUG_FS)
7905 connector_debugfs_init(amdgpu_dm_connector);
7906#endif
7907
7908 return 0;
7909}
7910
7911static void amdgpu_dm_connector_funcs_force(struct drm_connector *connector)
7912{
7913 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7914 struct dc_link *dc_link = aconnector->dc_link;
7915 struct dc_sink *dc_em_sink = aconnector->dc_em_sink;
7916 const struct drm_edid *drm_edid;
7917 struct i2c_adapter *ddc;
7918 struct drm_device *dev = connector->dev;
7919
7920 if (dc_link && dc_link->aux_mode)
7921 ddc = &aconnector->dm_dp_aux.aux.ddc;
7922 else
7923 ddc = &aconnector->i2c->base;
7924
7925 drm_edid = drm_edid_read_ddc(connector, ddc);
7926 drm_edid_connector_update(connector, drm_edid);
7927 if (!drm_edid) {
7928 drm_err(dev, "No EDID found on connector: %s.\n", connector->name);
7929 return;
7930 }
7931
7932 aconnector->drm_edid = drm_edid;
7933 /* Update emulated (virtual) sink's EDID */
7934 if (dc_em_sink && dc_link) {
7935 // FIXME: Get rid of drm_edid_raw()
7936 const struct edid *edid = drm_edid_raw(drm_edid);
7937
7938 memset(&dc_em_sink->edid_caps, 0, sizeof(struct dc_edid_caps));
7939 memmove(dc_em_sink->dc_edid.raw_edid, edid,
7940 (edid->extensions + 1) * EDID_LENGTH);
7941 dm_helpers_parse_edid_caps(
7942 dc_link,
7943 &dc_em_sink->dc_edid,
7944 &dc_em_sink->edid_caps);
7945 }
7946}
7947
7948static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
7949 .reset = amdgpu_dm_connector_funcs_reset,
7950 .detect = amdgpu_dm_connector_detect,
7951 .fill_modes = drm_helper_probe_single_connector_modes,
7952 .destroy = amdgpu_dm_connector_destroy,
7953 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
7954 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
7955 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
7956 .atomic_get_property = amdgpu_dm_connector_atomic_get_property,
7957 .late_register = amdgpu_dm_connector_late_register,
7958 .early_unregister = amdgpu_dm_connector_unregister,
7959 .force = amdgpu_dm_connector_funcs_force
7960};
7961
7962static int get_modes(struct drm_connector *connector)
7963{
7964 return amdgpu_dm_connector_get_modes(connector);
7965}
7966
7967static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
7968{
7969 struct drm_connector *connector = &aconnector->base;
7970 struct dc_link *dc_link = aconnector->dc_link;
7971 struct dc_sink_init_data init_params = {
7972 .link = aconnector->dc_link,
7973 .sink_signal = SIGNAL_TYPE_VIRTUAL
7974 };
7975 const struct drm_edid *drm_edid;
7976 const struct edid *edid;
7977 struct i2c_adapter *ddc;
7978
7979 if (dc_link && dc_link->aux_mode)
7980 ddc = &aconnector->dm_dp_aux.aux.ddc;
7981 else
7982 ddc = &aconnector->i2c->base;
7983
7984 drm_edid = drm_edid_read_ddc(connector, ddc);
7985 drm_edid_connector_update(connector, drm_edid);
7986 if (!drm_edid) {
7987 drm_err(connector->dev, "No EDID found on connector: %s.\n", connector->name);
7988 return;
7989 }
7990
7991 if (connector->display_info.is_hdmi)
7992 init_params.sink_signal = SIGNAL_TYPE_HDMI_TYPE_A;
7993
7994 aconnector->drm_edid = drm_edid;
7995
7996 edid = drm_edid_raw(drm_edid); // FIXME: Get rid of drm_edid_raw()
7997 aconnector->dc_em_sink = dc_link_add_remote_sink(
7998 aconnector->dc_link,
7999 (uint8_t *)edid,
8000 (edid->extensions + 1) * EDID_LENGTH,
8001 &init_params);
8002
8003 if (aconnector->base.force == DRM_FORCE_ON) {
8004 aconnector->dc_sink = aconnector->dc_link->local_sink ?
8005 aconnector->dc_link->local_sink :
8006 aconnector->dc_em_sink;
8007 if (aconnector->dc_sink)
8008 dc_sink_retain(aconnector->dc_sink);
8009 }
8010}
8011
8012static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
8013{
8014 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
8015
8016 /*
8017 * In case of headless boot with force on for DP managed connector
8018 * Those settings have to be != 0 to get initial modeset
8019 */
8020 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
8021 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
8022 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
8023 }
8024
8025 create_eml_sink(aconnector);
8026}
8027
8028static enum dc_status dm_validate_stream_and_context(struct dc *dc,
8029 struct dc_stream_state *stream)
8030{
8031 enum dc_status dc_result = DC_ERROR_UNEXPECTED;
8032 struct dc_plane_state *dc_plane_state = NULL;
8033 struct dc_state *dc_state = NULL;
8034
8035 if (!stream)
8036 goto cleanup;
8037
8038 dc_plane_state = dc_create_plane_state(dc);
8039 if (!dc_plane_state)
8040 goto cleanup;
8041
8042 dc_state = dc_state_create(dc, NULL);
8043 if (!dc_state)
8044 goto cleanup;
8045
8046 /* populate stream to plane */
8047 dc_plane_state->src_rect.height = stream->src.height;
8048 dc_plane_state->src_rect.width = stream->src.width;
8049 dc_plane_state->dst_rect.height = stream->src.height;
8050 dc_plane_state->dst_rect.width = stream->src.width;
8051 dc_plane_state->clip_rect.height = stream->src.height;
8052 dc_plane_state->clip_rect.width = stream->src.width;
8053 dc_plane_state->plane_size.surface_pitch = ((stream->src.width + 255) / 256) * 256;
8054 dc_plane_state->plane_size.surface_size.height = stream->src.height;
8055 dc_plane_state->plane_size.surface_size.width = stream->src.width;
8056 dc_plane_state->plane_size.chroma_size.height = stream->src.height;
8057 dc_plane_state->plane_size.chroma_size.width = stream->src.width;
8058 dc_plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
8059 dc_plane_state->tiling_info.gfx9.swizzle = DC_SW_UNKNOWN;
8060 dc_plane_state->rotation = ROTATION_ANGLE_0;
8061 dc_plane_state->is_tiling_rotated = false;
8062 dc_plane_state->tiling_info.gfx8.array_mode = DC_ARRAY_LINEAR_GENERAL;
8063
8064 dc_result = dc_validate_stream(dc, stream);
8065 if (dc_result == DC_OK)
8066 dc_result = dc_validate_plane(dc, dc_plane_state);
8067
8068 if (dc_result == DC_OK)
8069 dc_result = dc_state_add_stream(dc, dc_state, stream);
8070
8071 if (dc_result == DC_OK && !dc_state_add_plane(
8072 dc,
8073 stream,
8074 dc_plane_state,
8075 dc_state))
8076 dc_result = DC_FAIL_ATTACH_SURFACES;
8077
8078 if (dc_result == DC_OK)
8079 dc_result = dc_validate_global_state(dc, dc_state, DC_VALIDATE_MODE_ONLY);
8080
8081cleanup:
8082 if (dc_state)
8083 dc_state_release(dc_state);
8084
8085 if (dc_plane_state)
8086 dc_plane_state_release(dc_plane_state);
8087
8088 return dc_result;
8089}
8090
8091struct dc_stream_state *
8092create_validate_stream_for_sink(struct drm_connector *connector,
8093 const struct drm_display_mode *drm_mode,
8094 const struct dm_connector_state *dm_state,
8095 const struct dc_stream_state *old_stream)
8096{
8097 struct amdgpu_dm_connector *aconnector = NULL;
8098 struct amdgpu_device *adev = drm_to_adev(connector->dev);
8099 struct dc_stream_state *stream;
8100 const struct drm_connector_state *drm_state = dm_state ? &dm_state->base : NULL;
8101 int requested_bpc = drm_state ? drm_state->max_requested_bpc : 8;
8102 enum dc_status dc_result = DC_OK;
8103 uint8_t bpc_limit = 6;
8104
8105 if (!dm_state)
8106 return NULL;
8107
8108 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
8109 aconnector = to_amdgpu_dm_connector(connector);
8110
8111 if (aconnector &&
8112 (aconnector->dc_link->connector_signal == SIGNAL_TYPE_HDMI_TYPE_A ||
8113 aconnector->dc_link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER))
8114 bpc_limit = 8;
8115
8116 do {
8117 drm_dbg_kms(connector->dev, "Trying with %d bpc\n", requested_bpc);
8118 stream = create_stream_for_sink(connector, drm_mode,
8119 dm_state, old_stream,
8120 requested_bpc);
8121 if (stream == NULL) {
8122 drm_err(adev_to_drm(adev), "Failed to create stream for sink!\n");
8123 break;
8124 }
8125
8126 dc_result = dc_validate_stream(adev->dm.dc, stream);
8127
8128 if (!aconnector) /* writeback connector */
8129 return stream;
8130
8131 if (dc_result == DC_OK && stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
8132 dc_result = dm_dp_mst_is_port_support_mode(aconnector, stream);
8133
8134 if (dc_result == DC_OK)
8135 dc_result = dm_validate_stream_and_context(adev->dm.dc, stream);
8136
8137 if (dc_result != DC_OK) {
8138 drm_dbg_kms(connector->dev, "Pruned mode %d x %d (clk %d) %s %s -- %s\n",
8139 drm_mode->hdisplay,
8140 drm_mode->vdisplay,
8141 drm_mode->clock,
8142 dc_pixel_encoding_to_str(stream->timing.pixel_encoding),
8143 dc_color_depth_to_str(stream->timing.display_color_depth),
8144 dc_status_to_str(dc_result));
8145
8146 dc_stream_release(stream);
8147 stream = NULL;
8148 requested_bpc -= 2; /* lower bpc to retry validation */
8149 }
8150
8151 } while (stream == NULL && requested_bpc >= bpc_limit);
8152
8153 switch (dc_result) {
8154 /*
8155 * If we failed to validate DP bandwidth stream with the requested RGB color depth,
8156 * we try to fallback and configure in order:
8157 * YUV422 (8bpc, 6bpc)
8158 * YUV420 (8bpc, 6bpc)
8159 */
8160 case DC_FAIL_ENC_VALIDATE:
8161 case DC_EXCEED_DONGLE_CAP:
8162 case DC_NO_DP_LINK_BANDWIDTH:
8163 /* recursively entered twice and already tried both YUV422 and YUV420 */
8164 if (aconnector->force_yuv422_output && aconnector->force_yuv420_output)
8165 break;
8166 /* first failure; try YUV422 */
8167 if (!aconnector->force_yuv422_output) {
8168 drm_dbg_kms(connector->dev, "%s:%d Validation failed with %d, retrying w/ YUV422\n",
8169 __func__, __LINE__, dc_result);
8170 aconnector->force_yuv422_output = true;
8171 /* recursively entered and YUV422 failed, try YUV420 */
8172 } else if (!aconnector->force_yuv420_output) {
8173 drm_dbg_kms(connector->dev, "%s:%d Validation failed with %d, retrying w/ YUV420\n",
8174 __func__, __LINE__, dc_result);
8175 aconnector->force_yuv420_output = true;
8176 }
8177 stream = create_validate_stream_for_sink(connector, drm_mode,
8178 dm_state, old_stream);
8179 aconnector->force_yuv422_output = false;
8180 aconnector->force_yuv420_output = false;
8181 break;
8182 case DC_OK:
8183 break;
8184 default:
8185 drm_dbg_kms(connector->dev, "%s:%d Unhandled validation failure %d\n",
8186 __func__, __LINE__, dc_result);
8187 break;
8188 }
8189
8190 return stream;
8191}
8192
8193enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
8194 const struct drm_display_mode *mode)
8195{
8196 int result = MODE_ERROR;
8197 struct dc_sink *dc_sink;
8198 struct drm_display_mode *test_mode;
8199 /* TODO: Unhardcode stream count */
8200 struct dc_stream_state *stream;
8201 /* we always have an amdgpu_dm_connector here since we got
8202 * here via the amdgpu_dm_connector_helper_funcs
8203 */
8204 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
8205
8206 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
8207 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
8208 return result;
8209
8210 /*
8211 * Only run this the first time mode_valid is called to initilialize
8212 * EDID mgmt
8213 */
8214 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
8215 !aconnector->dc_em_sink)
8216 handle_edid_mgmt(aconnector);
8217
8218 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
8219
8220 if (dc_sink == NULL && aconnector->base.force != DRM_FORCE_ON_DIGITAL &&
8221 aconnector->base.force != DRM_FORCE_ON) {
8222 drm_err(connector->dev, "dc_sink is NULL!\n");
8223 goto fail;
8224 }
8225
8226 test_mode = drm_mode_duplicate(connector->dev, mode);
8227 if (!test_mode)
8228 goto fail;
8229
8230 drm_mode_set_crtcinfo(test_mode, 0);
8231
8232 stream = create_validate_stream_for_sink(connector, test_mode,
8233 to_dm_connector_state(connector->state),
8234 NULL);
8235 drm_mode_destroy(connector->dev, test_mode);
8236 if (stream) {
8237 dc_stream_release(stream);
8238 result = MODE_OK;
8239 }
8240
8241fail:
8242 /* TODO: error handling*/
8243 return result;
8244}
8245
8246static int fill_hdr_info_packet(const struct drm_connector_state *state,
8247 struct dc_info_packet *out)
8248{
8249 struct hdmi_drm_infoframe frame;
8250 unsigned char buf[30]; /* 26 + 4 */
8251 ssize_t len;
8252 int ret, i;
8253
8254 memset(out, 0, sizeof(*out));
8255
8256 if (!state->hdr_output_metadata)
8257 return 0;
8258
8259 ret = drm_hdmi_infoframe_set_hdr_metadata(&frame, state);
8260 if (ret)
8261 return ret;
8262
8263 len = hdmi_drm_infoframe_pack_only(&frame, buf, sizeof(buf));
8264 if (len < 0)
8265 return (int)len;
8266
8267 /* Static metadata is a fixed 26 bytes + 4 byte header. */
8268 if (len != 30)
8269 return -EINVAL;
8270
8271 /* Prepare the infopacket for DC. */
8272 switch (state->connector->connector_type) {
8273 case DRM_MODE_CONNECTOR_HDMIA:
8274 out->hb0 = 0x87; /* type */
8275 out->hb1 = 0x01; /* version */
8276 out->hb2 = 0x1A; /* length */
8277 out->sb[0] = buf[3]; /* checksum */
8278 i = 1;
8279 break;
8280
8281 case DRM_MODE_CONNECTOR_DisplayPort:
8282 case DRM_MODE_CONNECTOR_eDP:
8283 out->hb0 = 0x00; /* sdp id, zero */
8284 out->hb1 = 0x87; /* type */
8285 out->hb2 = 0x1D; /* payload len - 1 */
8286 out->hb3 = (0x13 << 2); /* sdp version */
8287 out->sb[0] = 0x01; /* version */
8288 out->sb[1] = 0x1A; /* length */
8289 i = 2;
8290 break;
8291
8292 default:
8293 return -EINVAL;
8294 }
8295
8296 memcpy(&out->sb[i], &buf[4], 26);
8297 out->valid = true;
8298
8299 print_hex_dump(KERN_DEBUG, "HDR SB:", DUMP_PREFIX_NONE, 16, 1, out->sb,
8300 sizeof(out->sb), false);
8301
8302 return 0;
8303}
8304
8305static int
8306amdgpu_dm_connector_atomic_check(struct drm_connector *conn,
8307 struct drm_atomic_state *state)
8308{
8309 struct drm_connector_state *new_con_state =
8310 drm_atomic_get_new_connector_state(state, conn);
8311 struct drm_connector_state *old_con_state =
8312 drm_atomic_get_old_connector_state(state, conn);
8313 struct drm_crtc *crtc = new_con_state->crtc;
8314 struct drm_crtc_state *new_crtc_state;
8315 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(conn);
8316 int ret;
8317
8318 if (WARN_ON(unlikely(!old_con_state || !new_con_state)))
8319 return -EINVAL;
8320
8321 trace_amdgpu_dm_connector_atomic_check(new_con_state);
8322
8323 if (conn->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
8324 ret = drm_dp_mst_root_conn_atomic_check(new_con_state, &aconn->mst_mgr);
8325 if (ret < 0)
8326 return ret;
8327 }
8328
8329 if (!crtc)
8330 return 0;
8331
8332 if (new_con_state->privacy_screen_sw_state != old_con_state->privacy_screen_sw_state) {
8333 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8334 if (IS_ERR(new_crtc_state))
8335 return PTR_ERR(new_crtc_state);
8336
8337 new_crtc_state->mode_changed = true;
8338 }
8339
8340 if (new_con_state->colorspace != old_con_state->colorspace) {
8341 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8342 if (IS_ERR(new_crtc_state))
8343 return PTR_ERR(new_crtc_state);
8344
8345 new_crtc_state->mode_changed = true;
8346 }
8347
8348 if (new_con_state->content_type != old_con_state->content_type) {
8349 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8350 if (IS_ERR(new_crtc_state))
8351 return PTR_ERR(new_crtc_state);
8352
8353 new_crtc_state->mode_changed = true;
8354 }
8355
8356 if (!drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state)) {
8357 struct dc_info_packet hdr_infopacket;
8358
8359 ret = fill_hdr_info_packet(new_con_state, &hdr_infopacket);
8360 if (ret)
8361 return ret;
8362
8363 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8364 if (IS_ERR(new_crtc_state))
8365 return PTR_ERR(new_crtc_state);
8366
8367 /*
8368 * DC considers the stream backends changed if the
8369 * static metadata changes. Forcing the modeset also
8370 * gives a simple way for userspace to switch from
8371 * 8bpc to 10bpc when setting the metadata to enter
8372 * or exit HDR.
8373 *
8374 * Changing the static metadata after it's been
8375 * set is permissible, however. So only force a
8376 * modeset if we're entering or exiting HDR.
8377 */
8378 new_crtc_state->mode_changed = new_crtc_state->mode_changed ||
8379 !old_con_state->hdr_output_metadata ||
8380 !new_con_state->hdr_output_metadata;
8381 }
8382
8383 return 0;
8384}
8385
8386static const struct drm_connector_helper_funcs
8387amdgpu_dm_connector_helper_funcs = {
8388 /*
8389 * If hotplugging a second bigger display in FB Con mode, bigger resolution
8390 * modes will be filtered by drm_mode_validate_size(), and those modes
8391 * are missing after user start lightdm. So we need to renew modes list.
8392 * in get_modes call back, not just return the modes count
8393 */
8394 .get_modes = get_modes,
8395 .mode_valid = amdgpu_dm_connector_mode_valid,
8396 .atomic_check = amdgpu_dm_connector_atomic_check,
8397};
8398
8399static void dm_encoder_helper_disable(struct drm_encoder *encoder)
8400{
8401
8402}
8403
8404int convert_dc_color_depth_into_bpc(enum dc_color_depth display_color_depth)
8405{
8406 switch (display_color_depth) {
8407 case COLOR_DEPTH_666:
8408 return 6;
8409 case COLOR_DEPTH_888:
8410 return 8;
8411 case COLOR_DEPTH_101010:
8412 return 10;
8413 case COLOR_DEPTH_121212:
8414 return 12;
8415 case COLOR_DEPTH_141414:
8416 return 14;
8417 case COLOR_DEPTH_161616:
8418 return 16;
8419 default:
8420 break;
8421 }
8422 return 0;
8423}
8424
8425static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
8426 struct drm_crtc_state *crtc_state,
8427 struct drm_connector_state *conn_state)
8428{
8429 struct drm_atomic_state *state = crtc_state->state;
8430 struct drm_connector *connector = conn_state->connector;
8431 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
8432 struct dm_connector_state *dm_new_connector_state = to_dm_connector_state(conn_state);
8433 const struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
8434 struct drm_dp_mst_topology_mgr *mst_mgr;
8435 struct drm_dp_mst_port *mst_port;
8436 struct drm_dp_mst_topology_state *mst_state;
8437 enum dc_color_depth color_depth;
8438 int clock, bpp = 0;
8439 bool is_y420 = false;
8440
8441 if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
8442 (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
8443 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8444 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8445 enum drm_mode_status result;
8446
8447 result = drm_crtc_helper_mode_valid_fixed(encoder->crtc, adjusted_mode, native_mode);
8448 if (result != MODE_OK && dm_new_connector_state->scaling == RMX_OFF) {
8449 drm_dbg_driver(encoder->dev,
8450 "mode %dx%d@%dHz is not native, enabling scaling\n",
8451 adjusted_mode->hdisplay, adjusted_mode->vdisplay,
8452 drm_mode_vrefresh(adjusted_mode));
8453 dm_new_connector_state->scaling = RMX_ASPECT;
8454 }
8455 return 0;
8456 }
8457
8458 if (!aconnector->mst_output_port)
8459 return 0;
8460
8461 mst_port = aconnector->mst_output_port;
8462 mst_mgr = &aconnector->mst_root->mst_mgr;
8463
8464 if (!crtc_state->connectors_changed && !crtc_state->mode_changed)
8465 return 0;
8466
8467 mst_state = drm_atomic_get_mst_topology_state(state, mst_mgr);
8468 if (IS_ERR(mst_state))
8469 return PTR_ERR(mst_state);
8470
8471 mst_state->pbn_div.full = dm_mst_get_pbn_divider(aconnector->mst_root->dc_link);
8472
8473 if (!state->duplicated) {
8474 int max_bpc = conn_state->max_requested_bpc;
8475
8476 is_y420 = drm_mode_is_420_also(&connector->display_info, adjusted_mode) &&
8477 aconnector->force_yuv420_output;
8478 color_depth = convert_color_depth_from_display_info(connector,
8479 is_y420,
8480 max_bpc);
8481 bpp = convert_dc_color_depth_into_bpc(color_depth) * 3;
8482 clock = adjusted_mode->clock;
8483 dm_new_connector_state->pbn = drm_dp_calc_pbn_mode(clock, bpp << 4);
8484 }
8485
8486 dm_new_connector_state->vcpi_slots =
8487 drm_dp_atomic_find_time_slots(state, mst_mgr, mst_port,
8488 dm_new_connector_state->pbn);
8489 if (dm_new_connector_state->vcpi_slots < 0) {
8490 drm_dbg_atomic(connector->dev, "failed finding vcpi slots: %d\n", (int)dm_new_connector_state->vcpi_slots);
8491 return dm_new_connector_state->vcpi_slots;
8492 }
8493 return 0;
8494}
8495
8496const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
8497 .disable = dm_encoder_helper_disable,
8498 .atomic_check = dm_encoder_helper_atomic_check
8499};
8500
8501static int dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_state *state,
8502 struct dc_state *dc_state,
8503 struct dsc_mst_fairness_vars *vars)
8504{
8505 struct dc_stream_state *stream = NULL;
8506 struct drm_connector *connector;
8507 struct drm_connector_state *new_con_state;
8508 struct amdgpu_dm_connector *aconnector;
8509 struct dm_connector_state *dm_conn_state;
8510 int i, j, ret;
8511 int vcpi, pbn_div, pbn = 0, slot_num = 0;
8512
8513 for_each_new_connector_in_state(state, connector, new_con_state, i) {
8514
8515 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
8516 continue;
8517
8518 aconnector = to_amdgpu_dm_connector(connector);
8519
8520 if (!aconnector->mst_output_port)
8521 continue;
8522
8523 if (!new_con_state || !new_con_state->crtc)
8524 continue;
8525
8526 dm_conn_state = to_dm_connector_state(new_con_state);
8527
8528 for (j = 0; j < dc_state->stream_count; j++) {
8529 stream = dc_state->streams[j];
8530 if (!stream)
8531 continue;
8532
8533 if ((struct amdgpu_dm_connector *)stream->dm_stream_context == aconnector)
8534 break;
8535
8536 stream = NULL;
8537 }
8538
8539 if (!stream)
8540 continue;
8541
8542 pbn_div = dm_mst_get_pbn_divider(stream->link);
8543 /* pbn is calculated by compute_mst_dsc_configs_for_state*/
8544 for (j = 0; j < dc_state->stream_count; j++) {
8545 if (vars[j].aconnector == aconnector) {
8546 pbn = vars[j].pbn;
8547 break;
8548 }
8549 }
8550
8551 if (j == dc_state->stream_count || pbn_div == 0)
8552 continue;
8553
8554 slot_num = DIV_ROUND_UP(pbn, pbn_div);
8555
8556 if (stream->timing.flags.DSC != 1) {
8557 dm_conn_state->pbn = pbn;
8558 dm_conn_state->vcpi_slots = slot_num;
8559
8560 ret = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port,
8561 dm_conn_state->pbn, false);
8562 if (ret < 0)
8563 return ret;
8564
8565 continue;
8566 }
8567
8568 vcpi = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port, pbn, true);
8569 if (vcpi < 0)
8570 return vcpi;
8571
8572 dm_conn_state->pbn = pbn;
8573 dm_conn_state->vcpi_slots = vcpi;
8574 }
8575 return 0;
8576}
8577
8578static int to_drm_connector_type(enum signal_type st, uint32_t connector_id)
8579{
8580 switch (st) {
8581 case SIGNAL_TYPE_HDMI_TYPE_A:
8582 return DRM_MODE_CONNECTOR_HDMIA;
8583 case SIGNAL_TYPE_EDP:
8584 return DRM_MODE_CONNECTOR_eDP;
8585 case SIGNAL_TYPE_LVDS:
8586 return DRM_MODE_CONNECTOR_LVDS;
8587 case SIGNAL_TYPE_RGB:
8588 return DRM_MODE_CONNECTOR_VGA;
8589 case SIGNAL_TYPE_DISPLAY_PORT:
8590 case SIGNAL_TYPE_DISPLAY_PORT_MST:
8591 /* External DP bridges have a different connector type. */
8592 if (connector_id == CONNECTOR_ID_VGA)
8593 return DRM_MODE_CONNECTOR_VGA;
8594 else if (connector_id == CONNECTOR_ID_LVDS)
8595 return DRM_MODE_CONNECTOR_LVDS;
8596
8597 return DRM_MODE_CONNECTOR_DisplayPort;
8598 case SIGNAL_TYPE_DVI_DUAL_LINK:
8599 case SIGNAL_TYPE_DVI_SINGLE_LINK:
8600 if (connector_id == CONNECTOR_ID_SINGLE_LINK_DVII ||
8601 connector_id == CONNECTOR_ID_DUAL_LINK_DVII)
8602 return DRM_MODE_CONNECTOR_DVII;
8603
8604 return DRM_MODE_CONNECTOR_DVID;
8605 case SIGNAL_TYPE_VIRTUAL:
8606 return DRM_MODE_CONNECTOR_VIRTUAL;
8607
8608 default:
8609 return DRM_MODE_CONNECTOR_Unknown;
8610 }
8611}
8612
8613static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector)
8614{
8615 struct drm_encoder *encoder;
8616
8617 /* There is only one encoder per connector */
8618 drm_connector_for_each_possible_encoder(connector, encoder)
8619 return encoder;
8620
8621 return NULL;
8622}
8623
8624static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
8625{
8626 struct drm_encoder *encoder;
8627 struct amdgpu_encoder *amdgpu_encoder;
8628
8629 encoder = amdgpu_dm_connector_to_encoder(connector);
8630
8631 if (encoder == NULL)
8632 return;
8633
8634 amdgpu_encoder = to_amdgpu_encoder(encoder);
8635
8636 amdgpu_encoder->native_mode.clock = 0;
8637
8638 if (!list_empty(&connector->probed_modes)) {
8639 struct drm_display_mode *preferred_mode = NULL;
8640
8641 list_for_each_entry(preferred_mode,
8642 &connector->probed_modes,
8643 head) {
8644 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
8645 amdgpu_encoder->native_mode = *preferred_mode;
8646
8647 break;
8648 }
8649
8650 }
8651}
8652
8653static struct drm_display_mode *
8654amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
8655 const char *name,
8656 int hdisplay, int vdisplay)
8657{
8658 struct drm_device *dev = encoder->dev;
8659 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8660 struct drm_display_mode *mode = NULL;
8661 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8662
8663 mode = drm_mode_duplicate(dev, native_mode);
8664
8665 if (mode == NULL)
8666 return NULL;
8667
8668 mode->hdisplay = hdisplay;
8669 mode->vdisplay = vdisplay;
8670 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
8671 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
8672
8673 return mode;
8674
8675}
8676
8677static const struct amdgpu_dm_mode_size {
8678 char name[DRM_DISPLAY_MODE_LEN];
8679 int w;
8680 int h;
8681} common_modes[] = {
8682 { "640x480", 640, 480},
8683 { "800x600", 800, 600},
8684 { "1024x768", 1024, 768},
8685 { "1280x720", 1280, 720},
8686 { "1280x800", 1280, 800},
8687 {"1280x1024", 1280, 1024},
8688 { "1440x900", 1440, 900},
8689 {"1680x1050", 1680, 1050},
8690 {"1600x1200", 1600, 1200},
8691 {"1920x1080", 1920, 1080},
8692 {"1920x1200", 1920, 1200}
8693};
8694
8695static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
8696 struct drm_connector *connector)
8697{
8698 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8699 struct drm_display_mode *mode = NULL;
8700 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8701 struct amdgpu_dm_connector *amdgpu_dm_connector =
8702 to_amdgpu_dm_connector(connector);
8703 int i;
8704 int n;
8705
8706 if ((connector->connector_type != DRM_MODE_CONNECTOR_eDP) &&
8707 (connector->connector_type != DRM_MODE_CONNECTOR_LVDS))
8708 return;
8709
8710 n = ARRAY_SIZE(common_modes);
8711
8712 for (i = 0; i < n; i++) {
8713 struct drm_display_mode *curmode = NULL;
8714 bool mode_existed = false;
8715
8716 if (common_modes[i].w > native_mode->hdisplay ||
8717 common_modes[i].h > native_mode->vdisplay ||
8718 (common_modes[i].w == native_mode->hdisplay &&
8719 common_modes[i].h == native_mode->vdisplay))
8720 continue;
8721
8722 list_for_each_entry(curmode, &connector->probed_modes, head) {
8723 if (common_modes[i].w == curmode->hdisplay &&
8724 common_modes[i].h == curmode->vdisplay) {
8725 mode_existed = true;
8726 break;
8727 }
8728 }
8729
8730 if (mode_existed)
8731 continue;
8732
8733 mode = amdgpu_dm_create_common_mode(encoder,
8734 common_modes[i].name, common_modes[i].w,
8735 common_modes[i].h);
8736 if (!mode)
8737 continue;
8738
8739 drm_mode_probed_add(connector, mode);
8740 amdgpu_dm_connector->num_modes++;
8741 }
8742}
8743
8744static void amdgpu_set_panel_orientation(struct drm_connector *connector)
8745{
8746 struct drm_encoder *encoder;
8747 struct amdgpu_encoder *amdgpu_encoder;
8748 const struct drm_display_mode *native_mode;
8749
8750 if (connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
8751 connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
8752 return;
8753
8754 mutex_lock(&connector->dev->mode_config.mutex);
8755 amdgpu_dm_connector_get_modes(connector);
8756 mutex_unlock(&connector->dev->mode_config.mutex);
8757
8758 encoder = amdgpu_dm_connector_to_encoder(connector);
8759 if (!encoder)
8760 return;
8761
8762 amdgpu_encoder = to_amdgpu_encoder(encoder);
8763
8764 native_mode = &amdgpu_encoder->native_mode;
8765 if (native_mode->hdisplay == 0 || native_mode->vdisplay == 0)
8766 return;
8767
8768 drm_connector_set_panel_orientation_with_quirk(connector,
8769 DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
8770 native_mode->hdisplay,
8771 native_mode->vdisplay);
8772}
8773
8774static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
8775 const struct drm_edid *drm_edid)
8776{
8777 struct amdgpu_dm_connector *amdgpu_dm_connector =
8778 to_amdgpu_dm_connector(connector);
8779
8780 if (drm_edid) {
8781 /* empty probed_modes */
8782 INIT_LIST_HEAD(&connector->probed_modes);
8783 amdgpu_dm_connector->num_modes =
8784 drm_edid_connector_add_modes(connector);
8785
8786 /* sorting the probed modes before calling function
8787 * amdgpu_dm_get_native_mode() since EDID can have
8788 * more than one preferred mode. The modes that are
8789 * later in the probed mode list could be of higher
8790 * and preferred resolution. For example, 3840x2160
8791 * resolution in base EDID preferred timing and 4096x2160
8792 * preferred resolution in DID extension block later.
8793 */
8794 drm_mode_sort(&connector->probed_modes);
8795 amdgpu_dm_get_native_mode(connector);
8796
8797 /* Freesync capabilities are reset by calling
8798 * drm_edid_connector_add_modes() and need to be
8799 * restored here.
8800 */
8801 amdgpu_dm_update_freesync_caps(connector, drm_edid);
8802 } else {
8803 amdgpu_dm_connector->num_modes = 0;
8804 }
8805}
8806
8807static bool is_duplicate_mode(struct amdgpu_dm_connector *aconnector,
8808 struct drm_display_mode *mode)
8809{
8810 struct drm_display_mode *m;
8811
8812 list_for_each_entry(m, &aconnector->base.probed_modes, head) {
8813 if (drm_mode_equal(m, mode))
8814 return true;
8815 }
8816
8817 return false;
8818}
8819
8820static uint add_fs_modes(struct amdgpu_dm_connector *aconnector)
8821{
8822 const struct drm_display_mode *m;
8823 struct drm_display_mode *new_mode;
8824 uint i;
8825 u32 new_modes_count = 0;
8826
8827 /* Standard FPS values
8828 *
8829 * 23.976 - TV/NTSC
8830 * 24 - Cinema
8831 * 25 - TV/PAL
8832 * 29.97 - TV/NTSC
8833 * 30 - TV/NTSC
8834 * 48 - Cinema HFR
8835 * 50 - TV/PAL
8836 * 60 - Commonly used
8837 * 48,72,96,120 - Multiples of 24
8838 */
8839 static const u32 common_rates[] = {
8840 23976, 24000, 25000, 29970, 30000,
8841 48000, 50000, 60000, 72000, 96000, 120000
8842 };
8843
8844 /*
8845 * Find mode with highest refresh rate with the same resolution
8846 * as the preferred mode. Some monitors report a preferred mode
8847 * with lower resolution than the highest refresh rate supported.
8848 */
8849
8850 m = get_highest_refresh_rate_mode(aconnector, true);
8851 if (!m)
8852 return 0;
8853
8854 for (i = 0; i < ARRAY_SIZE(common_rates); i++) {
8855 u64 target_vtotal, target_vtotal_diff;
8856 u64 num, den;
8857
8858 if (drm_mode_vrefresh(m) * 1000 < common_rates[i])
8859 continue;
8860
8861 if (common_rates[i] < aconnector->min_vfreq * 1000 ||
8862 common_rates[i] > aconnector->max_vfreq * 1000)
8863 continue;
8864
8865 num = (unsigned long long)m->clock * 1000 * 1000;
8866 den = common_rates[i] * (unsigned long long)m->htotal;
8867 target_vtotal = div_u64(num, den);
8868 target_vtotal_diff = target_vtotal - m->vtotal;
8869
8870 /* Check for illegal modes */
8871 if (m->vsync_start + target_vtotal_diff < m->vdisplay ||
8872 m->vsync_end + target_vtotal_diff < m->vsync_start ||
8873 m->vtotal + target_vtotal_diff < m->vsync_end)
8874 continue;
8875
8876 new_mode = drm_mode_duplicate(aconnector->base.dev, m);
8877 if (!new_mode)
8878 goto out;
8879
8880 new_mode->vtotal += (u16)target_vtotal_diff;
8881 new_mode->vsync_start += (u16)target_vtotal_diff;
8882 new_mode->vsync_end += (u16)target_vtotal_diff;
8883 new_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
8884 new_mode->type |= DRM_MODE_TYPE_DRIVER;
8885
8886 if (!is_duplicate_mode(aconnector, new_mode)) {
8887 drm_mode_probed_add(&aconnector->base, new_mode);
8888 new_modes_count += 1;
8889 } else
8890 drm_mode_destroy(aconnector->base.dev, new_mode);
8891 }
8892 out:
8893 return new_modes_count;
8894}
8895
8896static void amdgpu_dm_connector_add_freesync_modes(struct drm_connector *connector,
8897 const struct drm_edid *drm_edid)
8898{
8899 struct amdgpu_dm_connector *amdgpu_dm_connector =
8900 to_amdgpu_dm_connector(connector);
8901
8902 if (!(amdgpu_freesync_vid_mode && drm_edid))
8903 return;
8904
8905 if (!amdgpu_dm_connector->dc_sink || !amdgpu_dm_connector->dc_link)
8906 return;
8907
8908 if (!dc_supports_vrr(amdgpu_dm_connector->dc_sink->ctx->dce_version))
8909 return;
8910
8911 if (dc_connector_supports_analog(amdgpu_dm_connector->dc_link->link_id.id) &&
8912 amdgpu_dm_connector->dc_sink->edid_caps.analog)
8913 return;
8914
8915 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
8916 amdgpu_dm_connector->num_modes +=
8917 add_fs_modes(amdgpu_dm_connector);
8918}
8919
8920static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
8921{
8922 struct amdgpu_dm_connector *amdgpu_dm_connector =
8923 to_amdgpu_dm_connector(connector);
8924 struct dc_link *dc_link = amdgpu_dm_connector->dc_link;
8925 struct drm_encoder *encoder;
8926 const struct drm_edid *drm_edid = amdgpu_dm_connector->drm_edid;
8927 struct dc_link_settings *verified_link_cap = &dc_link->verified_link_cap;
8928 const struct dc *dc = dc_link->dc;
8929
8930 encoder = amdgpu_dm_connector_to_encoder(connector);
8931
8932 if (!drm_edid) {
8933 amdgpu_dm_connector->num_modes =
8934 drm_add_modes_noedid(connector, 640, 480);
8935 if (dc->link_srv->dp_get_encoding_format(verified_link_cap) == DP_128b_132b_ENCODING)
8936 amdgpu_dm_connector->num_modes +=
8937 drm_add_modes_noedid(connector, 1920, 1080);
8938
8939 if (amdgpu_dm_connector->dc_sink &&
8940 amdgpu_dm_connector->dc_sink->edid_caps.analog &&
8941 dc_connector_supports_analog(dc_link->link_id.id)) {
8942 /* Analog monitor connected by DAC load detection.
8943 * Add common modes. It will be up to the user to select one that works.
8944 */
8945 for (int i = 0; i < ARRAY_SIZE(common_modes); i++)
8946 amdgpu_dm_connector->num_modes += drm_add_modes_noedid(
8947 connector, common_modes[i].w, common_modes[i].h);
8948 }
8949 } else {
8950 amdgpu_dm_connector_ddc_get_modes(connector, drm_edid);
8951 if (encoder)
8952 amdgpu_dm_connector_add_common_modes(encoder, connector);
8953 amdgpu_dm_connector_add_freesync_modes(connector, drm_edid);
8954 }
8955 amdgpu_dm_fbc_init(connector);
8956
8957 return amdgpu_dm_connector->num_modes;
8958}
8959
8960static const u32 supported_colorspaces =
8961 BIT(DRM_MODE_COLORIMETRY_BT709_YCC) |
8962 BIT(DRM_MODE_COLORIMETRY_OPRGB) |
8963 BIT(DRM_MODE_COLORIMETRY_BT2020_RGB) |
8964 BIT(DRM_MODE_COLORIMETRY_BT2020_YCC);
8965
8966void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
8967 struct amdgpu_dm_connector *aconnector,
8968 int connector_type,
8969 struct dc_link *link,
8970 int link_index)
8971{
8972 struct amdgpu_device *adev = drm_to_adev(dm->ddev);
8973
8974 /*
8975 * Some of the properties below require access to state, like bpc.
8976 * Allocate some default initial connector state with our reset helper.
8977 */
8978 if (aconnector->base.funcs->reset)
8979 aconnector->base.funcs->reset(&aconnector->base);
8980
8981 aconnector->connector_id = link_index;
8982 aconnector->bl_idx = -1;
8983 aconnector->dc_link = link;
8984 aconnector->base.interlace_allowed = false;
8985 aconnector->base.doublescan_allowed = false;
8986 aconnector->base.stereo_allowed = false;
8987 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
8988 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
8989 aconnector->audio_inst = -1;
8990 aconnector->pack_sdp_v1_3 = false;
8991 aconnector->as_type = ADAPTIVE_SYNC_TYPE_NONE;
8992 memset(&aconnector->vsdb_info, 0, sizeof(aconnector->vsdb_info));
8993 mutex_init(&aconnector->hpd_lock);
8994 mutex_init(&aconnector->handle_mst_msg_ready);
8995
8996 /*
8997 * If HDMI HPD debounce delay is set, use the minimum between selected
8998 * value and AMDGPU_DM_MAX_HDMI_HPD_DEBOUNCE_MS
8999 */
9000 if (amdgpu_hdmi_hpd_debounce_delay_ms) {
9001 aconnector->hdmi_hpd_debounce_delay_ms = min(amdgpu_hdmi_hpd_debounce_delay_ms,
9002 AMDGPU_DM_MAX_HDMI_HPD_DEBOUNCE_MS);
9003 INIT_DELAYED_WORK(&aconnector->hdmi_hpd_debounce_work, hdmi_hpd_debounce_work);
9004 aconnector->hdmi_prev_sink = NULL;
9005 } else {
9006 aconnector->hdmi_hpd_debounce_delay_ms = 0;
9007 }
9008
9009 /*
9010 * configure support HPD hot plug connector_>polled default value is 0
9011 * which means HPD hot plug not supported
9012 */
9013 switch (connector_type) {
9014 case DRM_MODE_CONNECTOR_HDMIA:
9015 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
9016 aconnector->base.ycbcr_420_allowed =
9017 link->link_enc->features.hdmi_ycbcr420_supported ? true : false;
9018 break;
9019 case DRM_MODE_CONNECTOR_DisplayPort:
9020 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
9021 link->link_enc = link_enc_cfg_get_link_enc(link);
9022 ASSERT(link->link_enc);
9023 if (link->link_enc)
9024 aconnector->base.ycbcr_420_allowed =
9025 link->link_enc->features.dp_ycbcr420_supported ? true : false;
9026 break;
9027 case DRM_MODE_CONNECTOR_DVID:
9028 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
9029 break;
9030 case DRM_MODE_CONNECTOR_DVII:
9031 case DRM_MODE_CONNECTOR_VGA:
9032 aconnector->base.polled =
9033 DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
9034 break;
9035 default:
9036 break;
9037 }
9038
9039 drm_object_attach_property(&aconnector->base.base,
9040 dm->ddev->mode_config.scaling_mode_property,
9041 DRM_MODE_SCALE_NONE);
9042
9043 if (connector_type == DRM_MODE_CONNECTOR_HDMIA
9044 || (connector_type == DRM_MODE_CONNECTOR_DisplayPort && !aconnector->mst_root))
9045 drm_connector_attach_broadcast_rgb_property(&aconnector->base);
9046
9047 drm_object_attach_property(&aconnector->base.base,
9048 adev->mode_info.underscan_property,
9049 UNDERSCAN_OFF);
9050 drm_object_attach_property(&aconnector->base.base,
9051 adev->mode_info.underscan_hborder_property,
9052 0);
9053 drm_object_attach_property(&aconnector->base.base,
9054 adev->mode_info.underscan_vborder_property,
9055 0);
9056
9057 if (!aconnector->mst_root)
9058 drm_connector_attach_max_bpc_property(&aconnector->base, 8, 16);
9059
9060 aconnector->base.state->max_bpc = 16;
9061 aconnector->base.state->max_requested_bpc = aconnector->base.state->max_bpc;
9062
9063 if (connector_type == DRM_MODE_CONNECTOR_HDMIA) {
9064 /* Content Type is currently only implemented for HDMI. */
9065 drm_connector_attach_content_type_property(&aconnector->base);
9066 }
9067
9068 if (connector_type == DRM_MODE_CONNECTOR_HDMIA) {
9069 if (!drm_mode_create_hdmi_colorspace_property(&aconnector->base, supported_colorspaces))
9070 drm_connector_attach_colorspace_property(&aconnector->base);
9071 } else if ((connector_type == DRM_MODE_CONNECTOR_DisplayPort && !aconnector->mst_root) ||
9072 connector_type == DRM_MODE_CONNECTOR_eDP) {
9073 if (!drm_mode_create_dp_colorspace_property(&aconnector->base, supported_colorspaces))
9074 drm_connector_attach_colorspace_property(&aconnector->base);
9075 }
9076
9077 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
9078 connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
9079 connector_type == DRM_MODE_CONNECTOR_eDP) {
9080 drm_connector_attach_hdr_output_metadata_property(&aconnector->base);
9081
9082 if (!aconnector->mst_root)
9083 drm_connector_attach_vrr_capable_property(&aconnector->base);
9084
9085 if (adev->dm.hdcp_workqueue)
9086 drm_connector_attach_content_protection_property(&aconnector->base, true);
9087 }
9088
9089 if (connector_type == DRM_MODE_CONNECTOR_eDP) {
9090 struct drm_privacy_screen *privacy_screen;
9091
9092 drm_connector_attach_panel_type_property(&aconnector->base);
9093
9094 privacy_screen = drm_privacy_screen_get(adev_to_drm(adev)->dev, NULL);
9095 if (!IS_ERR(privacy_screen)) {
9096 drm_connector_attach_privacy_screen_provider(&aconnector->base,
9097 privacy_screen);
9098 } else if (PTR_ERR(privacy_screen) != -ENODEV) {
9099 drm_warn(adev_to_drm(adev), "Error getting privacy-screen\n");
9100 }
9101 }
9102}
9103
9104static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
9105 struct i2c_msg *msgs, int num)
9106{
9107 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
9108 struct ddc_service *ddc_service = i2c->ddc_service;
9109 struct i2c_command cmd;
9110 int i;
9111 int result = -EIO;
9112
9113 if (!ddc_service->ddc_pin)
9114 return result;
9115
9116 cmd.payloads = kzalloc_objs(struct i2c_payload, num);
9117
9118 if (!cmd.payloads)
9119 return result;
9120
9121 cmd.number_of_payloads = num;
9122 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
9123 cmd.speed = 100;
9124
9125 for (i = 0; i < num; i++) {
9126 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
9127 cmd.payloads[i].address = msgs[i].addr;
9128 cmd.payloads[i].length = msgs[i].len;
9129 cmd.payloads[i].data = msgs[i].buf;
9130 }
9131
9132 if (i2c->oem) {
9133 if (dc_submit_i2c_oem(
9134 ddc_service->ctx->dc,
9135 &cmd))
9136 result = num;
9137 } else {
9138 if (dc_submit_i2c(
9139 ddc_service->ctx->dc,
9140 ddc_service->link->link_index,
9141 &cmd))
9142 result = num;
9143 }
9144
9145 kfree(cmd.payloads);
9146 return result;
9147}
9148
9149static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
9150{
9151 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
9152}
9153
9154static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
9155 .master_xfer = amdgpu_dm_i2c_xfer,
9156 .functionality = amdgpu_dm_i2c_func,
9157};
9158
9159static struct amdgpu_i2c_adapter *
9160create_i2c(struct ddc_service *ddc_service, bool oem)
9161{
9162 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
9163 struct amdgpu_i2c_adapter *i2c;
9164
9165 i2c = kzalloc_obj(struct amdgpu_i2c_adapter);
9166 if (!i2c)
9167 return NULL;
9168 i2c->base.owner = THIS_MODULE;
9169 i2c->base.dev.parent = &adev->pdev->dev;
9170 i2c->base.algo = &amdgpu_dm_i2c_algo;
9171 if (oem)
9172 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c OEM bus");
9173 else
9174 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d",
9175 ddc_service->link->link_index);
9176 i2c_set_adapdata(&i2c->base, i2c);
9177 i2c->ddc_service = ddc_service;
9178 i2c->oem = oem;
9179
9180 return i2c;
9181}
9182
9183int amdgpu_dm_initialize_hdmi_connector(struct amdgpu_dm_connector *aconnector)
9184{
9185 struct cec_connector_info conn_info;
9186 struct drm_device *ddev = aconnector->base.dev;
9187 struct device *hdmi_dev = ddev->dev;
9188
9189 if (amdgpu_dc_debug_mask & DC_DISABLE_HDMI_CEC) {
9190 drm_info(ddev, "HDMI-CEC feature masked\n");
9191 return -EINVAL;
9192 }
9193
9194 cec_fill_conn_info_from_drm(&conn_info, &aconnector->base);
9195 aconnector->notifier =
9196 cec_notifier_conn_register(hdmi_dev, NULL, &conn_info);
9197 if (!aconnector->notifier) {
9198 drm_err(ddev, "Failed to create cec notifier\n");
9199 return -ENOMEM;
9200 }
9201
9202 return 0;
9203}
9204
9205/*
9206 * Note: this function assumes that dc_link_detect() was called for the
9207 * dc_link which will be represented by this aconnector.
9208 */
9209static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
9210 struct amdgpu_dm_connector *aconnector,
9211 u32 link_index,
9212 struct amdgpu_encoder *aencoder)
9213{
9214 int res = 0;
9215 int connector_type;
9216 struct dc *dc = dm->dc;
9217 struct dc_link *link = dc_get_link_at_index(dc, link_index);
9218 struct amdgpu_i2c_adapter *i2c;
9219
9220 /* Not needed for writeback connector */
9221 link->priv = aconnector;
9222
9223
9224 i2c = create_i2c(link->ddc, false);
9225 if (!i2c) {
9226 drm_err(adev_to_drm(dm->adev), "Failed to create i2c adapter data\n");
9227 return -ENOMEM;
9228 }
9229
9230 aconnector->i2c = i2c;
9231 res = devm_i2c_add_adapter(dm->adev->dev, &i2c->base);
9232
9233 if (res) {
9234 drm_err(adev_to_drm(dm->adev), "Failed to register hw i2c %d\n", link->link_index);
9235 goto out_free;
9236 }
9237
9238 connector_type = to_drm_connector_type(link->connector_signal, link->link_id.id);
9239
9240 res = drm_connector_init_with_ddc(
9241 dm->ddev,
9242 &aconnector->base,
9243 &amdgpu_dm_connector_funcs,
9244 connector_type,
9245 &i2c->base);
9246
9247 if (res) {
9248 drm_err(adev_to_drm(dm->adev), "connector_init failed\n");
9249 aconnector->connector_id = -1;
9250 goto out_free;
9251 }
9252
9253 drm_connector_helper_add(
9254 &aconnector->base,
9255 &amdgpu_dm_connector_helper_funcs);
9256
9257 amdgpu_dm_connector_init_helper(
9258 dm,
9259 aconnector,
9260 connector_type,
9261 link,
9262 link_index);
9263
9264 drm_connector_attach_encoder(
9265 &aconnector->base, &aencoder->base);
9266
9267 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
9268 connector_type == DRM_MODE_CONNECTOR_HDMIB)
9269 amdgpu_dm_initialize_hdmi_connector(aconnector);
9270
9271 if (dc_is_dp_signal(link->connector_signal))
9272 amdgpu_dm_initialize_dp_connector(dm, aconnector, link->link_index);
9273
9274out_free:
9275 if (res) {
9276 kfree(i2c);
9277 aconnector->i2c = NULL;
9278 }
9279 return res;
9280}
9281
9282int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
9283{
9284 switch (adev->mode_info.num_crtc) {
9285 case 1:
9286 return 0x1;
9287 case 2:
9288 return 0x3;
9289 case 3:
9290 return 0x7;
9291 case 4:
9292 return 0xf;
9293 case 5:
9294 return 0x1f;
9295 case 6:
9296 default:
9297 return 0x3f;
9298 }
9299}
9300
9301static int amdgpu_dm_encoder_init(struct drm_device *dev,
9302 struct amdgpu_encoder *aencoder,
9303 uint32_t link_index)
9304{
9305 struct amdgpu_device *adev = drm_to_adev(dev);
9306
9307 int res = drm_encoder_init(dev,
9308 &aencoder->base,
9309 &amdgpu_dm_encoder_funcs,
9310 DRM_MODE_ENCODER_TMDS,
9311 NULL);
9312
9313 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
9314
9315 if (!res)
9316 aencoder->encoder_id = link_index;
9317 else
9318 aencoder->encoder_id = -1;
9319
9320 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
9321
9322 return res;
9323}
9324
9325static void manage_dm_interrupts(struct amdgpu_device *adev,
9326 struct amdgpu_crtc *acrtc,
9327 struct dm_crtc_state *acrtc_state)
9328{ /*
9329 * We cannot be sure that the frontend index maps to the same
9330 * backend index - some even map to more than one.
9331 * So we have to go through the CRTC to find the right IRQ.
9332 */
9333 int irq_type = amdgpu_display_crtc_idx_to_irq_type(
9334 adev,
9335 acrtc->crtc_id);
9336 struct drm_device *dev = adev_to_drm(adev);
9337
9338 struct drm_vblank_crtc_config config = {0};
9339 struct dc_crtc_timing *timing;
9340 int offdelay;
9341
9342 if (acrtc_state) {
9343 timing = &acrtc_state->stream->timing;
9344
9345 if (amdgpu_ip_version(adev, DCE_HWIP, 0) <
9346 IP_VERSION(3, 5, 0) ||
9347 !(adev->flags & AMD_IS_APU)) {
9348 /*
9349 * Older HW and DGPU have issues with instant off;
9350 * use a 2 frame offdelay.
9351 */
9352 offdelay = DIV64_U64_ROUND_UP((u64)20 *
9353 timing->v_total *
9354 timing->h_total,
9355 timing->pix_clk_100hz);
9356
9357 config.offdelay_ms = offdelay ?: 30;
9358 } else {
9359 /* offdelay_ms = 0 will never disable vblank */
9360 config.offdelay_ms = 1;
9361 config.disable_immediate = true;
9362 }
9363
9364 drm_crtc_vblank_on_config(&acrtc->base,
9365 &config);
9366 /* Allow RX6xxx, RX7700, RX7800 GPUs to call amdgpu_irq_get.*/
9367 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
9368 case IP_VERSION(3, 0, 0):
9369 case IP_VERSION(3, 0, 2):
9370 case IP_VERSION(3, 0, 3):
9371 case IP_VERSION(3, 2, 0):
9372 if (amdgpu_irq_get(adev, &adev->pageflip_irq, irq_type))
9373 drm_err(dev, "DM_IRQ: Cannot get pageflip irq!\n");
9374#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
9375 if (amdgpu_irq_get(adev, &adev->vline0_irq, irq_type))
9376 drm_err(dev, "DM_IRQ: Cannot get vline0 irq!\n");
9377#endif
9378 }
9379
9380 } else {
9381 /* Allow RX6xxx, RX7700, RX7800 GPUs to call amdgpu_irq_put.*/
9382 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
9383 case IP_VERSION(3, 0, 0):
9384 case IP_VERSION(3, 0, 2):
9385 case IP_VERSION(3, 0, 3):
9386 case IP_VERSION(3, 2, 0):
9387#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
9388 if (amdgpu_irq_put(adev, &adev->vline0_irq, irq_type))
9389 drm_err(dev, "DM_IRQ: Cannot put vline0 irq!\n");
9390#endif
9391 if (amdgpu_irq_put(adev, &adev->pageflip_irq, irq_type))
9392 drm_err(dev, "DM_IRQ: Cannot put pageflip irq!\n");
9393 }
9394
9395 drm_crtc_vblank_off(&acrtc->base);
9396 }
9397}
9398
9399static void dm_update_pflip_irq_state(struct amdgpu_device *adev,
9400 struct amdgpu_crtc *acrtc)
9401{
9402 int irq_type =
9403 amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
9404
9405 /**
9406 * This reads the current state for the IRQ and force reapplies
9407 * the setting to hardware.
9408 */
9409 amdgpu_irq_update(adev, &adev->pageflip_irq, irq_type);
9410}
9411
9412static bool
9413is_scaling_state_different(const struct dm_connector_state *dm_state,
9414 const struct dm_connector_state *old_dm_state)
9415{
9416 if (dm_state->scaling != old_dm_state->scaling)
9417 return true;
9418 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
9419 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
9420 return true;
9421 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
9422 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
9423 return true;
9424 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
9425 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
9426 return true;
9427 return false;
9428}
9429
9430static bool is_content_protection_different(struct drm_crtc_state *new_crtc_state,
9431 struct drm_crtc_state *old_crtc_state,
9432 struct drm_connector_state *new_conn_state,
9433 struct drm_connector_state *old_conn_state,
9434 const struct drm_connector *connector,
9435 struct hdcp_workqueue *hdcp_w)
9436{
9437 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
9438 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
9439
9440 pr_debug("[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n",
9441 connector->index, connector->status, connector->dpms);
9442 pr_debug("[HDCP_DM] state protection old: %x new: %x\n",
9443 old_conn_state->content_protection, new_conn_state->content_protection);
9444
9445 if (old_crtc_state)
9446 pr_debug("[HDCP_DM] old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
9447 old_crtc_state->enable,
9448 old_crtc_state->active,
9449 old_crtc_state->mode_changed,
9450 old_crtc_state->active_changed,
9451 old_crtc_state->connectors_changed);
9452
9453 if (new_crtc_state)
9454 pr_debug("[HDCP_DM] NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
9455 new_crtc_state->enable,
9456 new_crtc_state->active,
9457 new_crtc_state->mode_changed,
9458 new_crtc_state->active_changed,
9459 new_crtc_state->connectors_changed);
9460
9461 /* hdcp content type change */
9462 if (old_conn_state->hdcp_content_type != new_conn_state->hdcp_content_type &&
9463 new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
9464 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
9465 pr_debug("[HDCP_DM] Type0/1 change %s :true\n", __func__);
9466 return true;
9467 }
9468
9469 /* CP is being re enabled, ignore this */
9470 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED &&
9471 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
9472 if (new_crtc_state && new_crtc_state->mode_changed) {
9473 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
9474 pr_debug("[HDCP_DM] ENABLED->DESIRED & mode_changed %s :true\n", __func__);
9475 return true;
9476 }
9477 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED;
9478 pr_debug("[HDCP_DM] ENABLED -> DESIRED %s :false\n", __func__);
9479 return false;
9480 }
9481
9482 /* S3 resume case, since old state will always be 0 (UNDESIRED) and the restored state will be ENABLED
9483 *
9484 * Handles: UNDESIRED -> ENABLED
9485 */
9486 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED &&
9487 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
9488 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
9489
9490 /* Stream removed and re-enabled
9491 *
9492 * Can sometimes overlap with the HPD case,
9493 * thus set update_hdcp to false to avoid
9494 * setting HDCP multiple times.
9495 *
9496 * Handles: DESIRED -> DESIRED (Special case)
9497 */
9498 if (!(old_conn_state->crtc && old_conn_state->crtc->enabled) &&
9499 new_conn_state->crtc && new_conn_state->crtc->enabled &&
9500 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
9501 dm_con_state->update_hdcp = false;
9502 pr_debug("[HDCP_DM] DESIRED->DESIRED (Stream removed and re-enabled) %s :true\n",
9503 __func__);
9504 return true;
9505 }
9506
9507 /* Hot-plug, headless s3, dpms
9508 *
9509 * Only start HDCP if the display is connected/enabled.
9510 * update_hdcp flag will be set to false until the next
9511 * HPD comes in.
9512 *
9513 * Handles: DESIRED -> DESIRED (Special case)
9514 */
9515 if (dm_con_state->update_hdcp &&
9516 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED &&
9517 connector->dpms == DRM_MODE_DPMS_ON && aconnector->dc_sink != NULL) {
9518 dm_con_state->update_hdcp = false;
9519 pr_debug("[HDCP_DM] DESIRED->DESIRED (Hot-plug, headless s3, dpms) %s :true\n",
9520 __func__);
9521 return true;
9522 }
9523
9524 if (old_conn_state->content_protection == new_conn_state->content_protection) {
9525 if (new_conn_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED) {
9526 if (new_crtc_state && new_crtc_state->mode_changed) {
9527 pr_debug("[HDCP_DM] DESIRED->DESIRED or ENABLE->ENABLE mode_change %s :true\n",
9528 __func__);
9529 return true;
9530 }
9531 pr_debug("[HDCP_DM] DESIRED->DESIRED & ENABLE->ENABLE %s :false\n",
9532 __func__);
9533 return false;
9534 }
9535
9536 pr_debug("[HDCP_DM] UNDESIRED->UNDESIRED %s :false\n", __func__);
9537 return false;
9538 }
9539
9540 if (new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_ENABLED) {
9541 pr_debug("[HDCP_DM] UNDESIRED->DESIRED or DESIRED->UNDESIRED or ENABLED->UNDESIRED %s :true\n",
9542 __func__);
9543 return true;
9544 }
9545
9546 pr_debug("[HDCP_DM] DESIRED->ENABLED %s :false\n", __func__);
9547 return false;
9548}
9549
9550static void remove_stream(struct amdgpu_device *adev,
9551 struct amdgpu_crtc *acrtc,
9552 struct dc_stream_state *stream)
9553{
9554 /* this is the update mode case */
9555
9556 acrtc->otg_inst = -1;
9557 acrtc->enabled = false;
9558}
9559
9560static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
9561{
9562
9563 assert_spin_locked(&acrtc->base.dev->event_lock);
9564 WARN_ON(acrtc->event);
9565
9566 acrtc->event = acrtc->base.state->event;
9567
9568 /* Set the flip status */
9569 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
9570
9571 /* Mark this event as consumed */
9572 acrtc->base.state->event = NULL;
9573
9574 drm_dbg_state(acrtc->base.dev,
9575 "crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
9576 acrtc->crtc_id);
9577}
9578
9579static void update_freesync_state_on_stream(
9580 struct amdgpu_display_manager *dm,
9581 struct dm_crtc_state *new_crtc_state,
9582 struct dc_stream_state *new_stream,
9583 struct dc_plane_state *surface,
9584 u32 flip_timestamp_in_us)
9585{
9586 struct mod_vrr_params vrr_params;
9587 struct dc_info_packet vrr_infopacket = {0};
9588 struct amdgpu_device *adev = dm->adev;
9589 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
9590 unsigned long flags;
9591 bool pack_sdp_v1_3 = false;
9592 struct amdgpu_dm_connector *aconn;
9593 enum vrr_packet_type packet_type = PACKET_TYPE_VRR;
9594
9595 if (!new_stream)
9596 return;
9597
9598 /*
9599 * TODO: Determine why min/max totals and vrefresh can be 0 here.
9600 * For now it's sufficient to just guard against these conditions.
9601 */
9602
9603 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
9604 return;
9605
9606 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9607 vrr_params = acrtc->dm_irq_params.vrr_params;
9608
9609 if (surface) {
9610 mod_freesync_handle_preflip(
9611 dm->freesync_module,
9612 surface,
9613 new_stream,
9614 flip_timestamp_in_us,
9615 &vrr_params);
9616
9617 if (adev->family < AMDGPU_FAMILY_AI &&
9618 amdgpu_dm_crtc_vrr_active(new_crtc_state)) {
9619 mod_freesync_handle_v_update(dm->freesync_module,
9620 new_stream, &vrr_params);
9621
9622 /* Need to call this before the frame ends. */
9623 dc_stream_adjust_vmin_vmax(dm->dc,
9624 new_crtc_state->stream,
9625 &vrr_params.adjust);
9626 }
9627 }
9628
9629 aconn = (struct amdgpu_dm_connector *)new_stream->dm_stream_context;
9630
9631 if (aconn && (aconn->as_type == FREESYNC_TYPE_PCON_IN_WHITELIST || aconn->vsdb_info.replay_mode)) {
9632 pack_sdp_v1_3 = aconn->pack_sdp_v1_3;
9633
9634 if (aconn->vsdb_info.amd_vsdb_version == 1)
9635 packet_type = PACKET_TYPE_FS_V1;
9636 else if (aconn->vsdb_info.amd_vsdb_version == 2)
9637 packet_type = PACKET_TYPE_FS_V2;
9638 else if (aconn->vsdb_info.amd_vsdb_version == 3)
9639 packet_type = PACKET_TYPE_FS_V3;
9640
9641 mod_build_adaptive_sync_infopacket(new_stream, aconn->as_type, NULL,
9642 &new_stream->adaptive_sync_infopacket);
9643 }
9644
9645 mod_freesync_build_vrr_infopacket(
9646 dm->freesync_module,
9647 new_stream,
9648 &vrr_params,
9649 packet_type,
9650 TRANSFER_FUNC_UNKNOWN,
9651 &vrr_infopacket,
9652 pack_sdp_v1_3);
9653
9654 new_crtc_state->freesync_vrr_info_changed |=
9655 (memcmp(&new_crtc_state->vrr_infopacket,
9656 &vrr_infopacket,
9657 sizeof(vrr_infopacket)) != 0);
9658
9659 acrtc->dm_irq_params.vrr_params = vrr_params;
9660 new_crtc_state->vrr_infopacket = vrr_infopacket;
9661
9662 new_stream->vrr_infopacket = vrr_infopacket;
9663 new_stream->allow_freesync = mod_freesync_get_freesync_enabled(&vrr_params);
9664
9665 if (new_crtc_state->freesync_vrr_info_changed)
9666 drm_dbg_kms(adev_to_drm(adev), "VRR packet update: crtc=%u enabled=%d state=%d",
9667 new_crtc_state->base.crtc->base.id,
9668 (int)new_crtc_state->base.vrr_enabled,
9669 (int)vrr_params.state);
9670
9671 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9672}
9673
9674static void update_stream_irq_parameters(
9675 struct amdgpu_display_manager *dm,
9676 struct dm_crtc_state *new_crtc_state)
9677{
9678 struct dc_stream_state *new_stream = new_crtc_state->stream;
9679 struct mod_vrr_params vrr_params;
9680 struct mod_freesync_config config = new_crtc_state->freesync_config;
9681 struct amdgpu_device *adev = dm->adev;
9682 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
9683 unsigned long flags;
9684
9685 if (!new_stream)
9686 return;
9687
9688 /*
9689 * TODO: Determine why min/max totals and vrefresh can be 0 here.
9690 * For now it's sufficient to just guard against these conditions.
9691 */
9692 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
9693 return;
9694
9695 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9696 vrr_params = acrtc->dm_irq_params.vrr_params;
9697
9698 if (new_crtc_state->vrr_supported &&
9699 config.min_refresh_in_uhz &&
9700 config.max_refresh_in_uhz) {
9701 /*
9702 * if freesync compatible mode was set, config.state will be set
9703 * in atomic check
9704 */
9705 if (config.state == VRR_STATE_ACTIVE_FIXED && config.fixed_refresh_in_uhz &&
9706 (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) ||
9707 new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)) {
9708 vrr_params.max_refresh_in_uhz = config.max_refresh_in_uhz;
9709 vrr_params.min_refresh_in_uhz = config.min_refresh_in_uhz;
9710 vrr_params.fixed_refresh_in_uhz = config.fixed_refresh_in_uhz;
9711 vrr_params.state = VRR_STATE_ACTIVE_FIXED;
9712 } else {
9713 config.state = new_crtc_state->base.vrr_enabled ?
9714 VRR_STATE_ACTIVE_VARIABLE :
9715 VRR_STATE_INACTIVE;
9716 }
9717 } else {
9718 config.state = VRR_STATE_UNSUPPORTED;
9719 }
9720
9721 mod_freesync_build_vrr_params(dm->freesync_module,
9722 new_stream,
9723 &config, &vrr_params);
9724
9725 new_crtc_state->freesync_config = config;
9726 /* Copy state for access from DM IRQ handler */
9727 acrtc->dm_irq_params.freesync_config = config;
9728 acrtc->dm_irq_params.active_planes = new_crtc_state->active_planes;
9729 acrtc->dm_irq_params.vrr_params = vrr_params;
9730 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9731}
9732
9733static void amdgpu_dm_handle_vrr_transition(struct dm_crtc_state *old_state,
9734 struct dm_crtc_state *new_state)
9735{
9736 bool old_vrr_active = amdgpu_dm_crtc_vrr_active(old_state);
9737 bool new_vrr_active = amdgpu_dm_crtc_vrr_active(new_state);
9738
9739 if (!old_vrr_active && new_vrr_active) {
9740 /* Transition VRR inactive -> active:
9741 * While VRR is active, we must not disable vblank irq, as a
9742 * reenable after disable would compute bogus vblank/pflip
9743 * timestamps if it likely happened inside display front-porch.
9744 *
9745 * We also need vupdate irq for the actual core vblank handling
9746 * at end of vblank.
9747 */
9748 WARN_ON(amdgpu_dm_crtc_set_vupdate_irq(new_state->base.crtc, true) != 0);
9749 WARN_ON(drm_crtc_vblank_get(new_state->base.crtc) != 0);
9750 drm_dbg_driver(new_state->base.crtc->dev, "%s: crtc=%u VRR off->on: Get vblank ref\n",
9751 __func__, new_state->base.crtc->base.id);
9752 } else if (old_vrr_active && !new_vrr_active) {
9753 /* Transition VRR active -> inactive:
9754 * Allow vblank irq disable again for fixed refresh rate.
9755 */
9756 WARN_ON(amdgpu_dm_crtc_set_vupdate_irq(new_state->base.crtc, false) != 0);
9757 drm_crtc_vblank_put(new_state->base.crtc);
9758 drm_dbg_driver(new_state->base.crtc->dev, "%s: crtc=%u VRR on->off: Drop vblank ref\n",
9759 __func__, new_state->base.crtc->base.id);
9760 }
9761}
9762
9763static void amdgpu_dm_commit_cursors(struct drm_atomic_state *state)
9764{
9765 struct drm_plane *plane;
9766 struct drm_plane_state *old_plane_state;
9767 int i;
9768
9769 /*
9770 * TODO: Make this per-stream so we don't issue redundant updates for
9771 * commits with multiple streams.
9772 */
9773 for_each_old_plane_in_state(state, plane, old_plane_state, i)
9774 if (plane->type == DRM_PLANE_TYPE_CURSOR)
9775 amdgpu_dm_plane_handle_cursor_update(plane, old_plane_state);
9776}
9777
9778static inline uint32_t get_mem_type(struct drm_framebuffer *fb)
9779{
9780 struct amdgpu_bo *abo = gem_to_amdgpu_bo(fb->obj[0]);
9781
9782 return abo->tbo.resource ? abo->tbo.resource->mem_type : 0;
9783}
9784
9785static void amdgpu_dm_update_cursor(struct drm_plane *plane,
9786 struct drm_plane_state *old_plane_state,
9787 struct dc_stream_update *update)
9788{
9789 struct amdgpu_device *adev = drm_to_adev(plane->dev);
9790 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
9791 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
9792 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
9793 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
9794 uint64_t address = afb ? afb->address : 0;
9795 struct dc_cursor_position position = {0};
9796 struct dc_cursor_attributes attributes;
9797 int ret;
9798
9799 if (!plane->state->fb && !old_plane_state->fb)
9800 return;
9801
9802 drm_dbg_atomic(plane->dev, "crtc_id=%d with size %d to %d\n",
9803 amdgpu_crtc->crtc_id, plane->state->crtc_w,
9804 plane->state->crtc_h);
9805
9806 ret = amdgpu_dm_plane_get_cursor_position(plane, crtc, &position);
9807 if (ret)
9808 return;
9809
9810 if (!position.enable) {
9811 /* turn off cursor */
9812 if (crtc_state && crtc_state->stream) {
9813 dc_stream_set_cursor_position(crtc_state->stream,
9814 &position);
9815 update->cursor_position = &crtc_state->stream->cursor_position;
9816 }
9817 return;
9818 }
9819
9820 amdgpu_crtc->cursor_width = plane->state->crtc_w;
9821 amdgpu_crtc->cursor_height = plane->state->crtc_h;
9822
9823 memset(&attributes, 0, sizeof(attributes));
9824 attributes.address.high_part = upper_32_bits(address);
9825 attributes.address.low_part = lower_32_bits(address);
9826 attributes.width = plane->state->crtc_w;
9827 attributes.height = plane->state->crtc_h;
9828 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
9829 attributes.rotation_angle = 0;
9830 attributes.attribute_flags.value = 0;
9831
9832 /* Enable cursor degamma ROM on DCN3+ for implicit sRGB degamma in DRM
9833 * legacy gamma setup.
9834 */
9835 if (crtc_state->cm_is_degamma_srgb &&
9836 adev->dm.dc->caps.color.dpp.gamma_corr)
9837 attributes.attribute_flags.bits.ENABLE_CURSOR_DEGAMMA = 1;
9838
9839 if (afb)
9840 attributes.pitch = afb->base.pitches[0] / afb->base.format->cpp[0];
9841
9842 if (crtc_state->stream) {
9843 if (!dc_stream_set_cursor_attributes(crtc_state->stream,
9844 &attributes))
9845 drm_err(adev_to_drm(adev), "DC failed to set cursor attributes\n");
9846
9847 update->cursor_attributes = &crtc_state->stream->cursor_attributes;
9848
9849 if (!dc_stream_set_cursor_position(crtc_state->stream,
9850 &position))
9851 drm_err(adev_to_drm(adev), "DC failed to set cursor position\n");
9852
9853 update->cursor_position = &crtc_state->stream->cursor_position;
9854 }
9855}
9856
9857static void amdgpu_dm_enable_self_refresh(struct amdgpu_crtc *acrtc_attach,
9858 const struct dm_crtc_state *acrtc_state,
9859 const u64 current_ts)
9860{
9861 struct psr_settings *psr = &acrtc_state->stream->link->psr_settings;
9862 struct replay_settings *pr = &acrtc_state->stream->link->replay_settings;
9863 struct amdgpu_dm_connector *aconn =
9864 (struct amdgpu_dm_connector *)acrtc_state->stream->dm_stream_context;
9865 bool vrr_active = amdgpu_dm_crtc_vrr_active(acrtc_state);
9866
9867 if (acrtc_state->update_type > UPDATE_TYPE_FAST) {
9868 if (pr->config.replay_supported && !pr->replay_feature_enabled)
9869 amdgpu_dm_link_setup_replay(acrtc_state->stream->link, aconn);
9870 else if (psr->psr_version != DC_PSR_VERSION_UNSUPPORTED &&
9871 !psr->psr_feature_enabled)
9872 if (!aconn->disallow_edp_enter_psr)
9873 amdgpu_dm_link_setup_psr(acrtc_state->stream);
9874 }
9875
9876 /* Decrement skip count when SR is enabled and we're doing fast updates. */
9877 if (acrtc_state->update_type == UPDATE_TYPE_FAST &&
9878 (psr->psr_feature_enabled || pr->config.replay_supported)) {
9879 if (aconn->sr_skip_count > 0)
9880 aconn->sr_skip_count--;
9881
9882 /* Allow SR when skip count is 0. */
9883 acrtc_attach->dm_irq_params.allow_sr_entry = !aconn->sr_skip_count;
9884
9885 /*
9886 * If sink supports PSR SU/Panel Replay, there is no need to rely on
9887 * a vblank event disable request to enable PSR/RP. PSR SU/RP
9888 * can be enabled immediately once OS demonstrates an
9889 * adequate number of fast atomic commits to notify KMD
9890 * of update events.
9891 * See `amdgpu_dm_crtc_vblank_control_worker()`.
9892 */
9893 if (!vrr_active &&
9894 acrtc_attach->dm_irq_params.allow_sr_entry &&
9895#ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
9896 !amdgpu_dm_crc_window_is_activated(acrtc_state->base.crtc) &&
9897#endif
9898 (current_ts - psr->psr_dirty_rects_change_timestamp_ns) > 500000000) {
9899 if (pr->replay_feature_enabled && !pr->replay_allow_active)
9900 amdgpu_dm_replay_enable(acrtc_state->stream, true);
9901 if (psr->psr_version == DC_PSR_VERSION_SU_1 &&
9902 !psr->psr_allow_active && !aconn->disallow_edp_enter_psr)
9903 amdgpu_dm_psr_enable(acrtc_state->stream);
9904 }
9905 } else {
9906 acrtc_attach->dm_irq_params.allow_sr_entry = false;
9907 }
9908}
9909
9910static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
9911 struct drm_device *dev,
9912 struct amdgpu_display_manager *dm,
9913 struct drm_crtc *pcrtc,
9914 bool wait_for_vblank)
9915{
9916 u32 i;
9917 u64 timestamp_ns = ktime_get_ns();
9918 struct drm_plane *plane;
9919 struct drm_plane_state *old_plane_state, *new_plane_state;
9920 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
9921 struct drm_crtc_state *new_pcrtc_state =
9922 drm_atomic_get_new_crtc_state(state, pcrtc);
9923 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
9924 struct dm_crtc_state *dm_old_crtc_state =
9925 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
9926 int planes_count = 0, vpos, hpos;
9927 unsigned long flags;
9928 u32 target_vblank, last_flip_vblank;
9929 bool vrr_active = amdgpu_dm_crtc_vrr_active(acrtc_state);
9930 bool cursor_update = false;
9931 bool pflip_present = false;
9932 bool dirty_rects_changed = false;
9933 bool updated_planes_and_streams = false;
9934 struct {
9935 struct dc_surface_update surface_updates[MAX_SURFACES];
9936 struct dc_plane_info plane_infos[MAX_SURFACES];
9937 struct dc_scaling_info scaling_infos[MAX_SURFACES];
9938 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
9939 struct dc_stream_update stream_update;
9940 } *bundle;
9941
9942 bundle = kzalloc_obj(*bundle);
9943
9944 if (!bundle) {
9945 drm_err(dev, "Failed to allocate update bundle\n");
9946 goto cleanup;
9947 }
9948
9949 /*
9950 * Disable the cursor first if we're disabling all the planes.
9951 * It'll remain on the screen after the planes are re-enabled
9952 * if we don't.
9953 *
9954 * If the cursor is transitioning from native to overlay mode, the
9955 * native cursor needs to be disabled first.
9956 */
9957 if (acrtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE &&
9958 dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE) {
9959 struct dc_cursor_position cursor_position = {0};
9960
9961 if (!dc_stream_set_cursor_position(acrtc_state->stream,
9962 &cursor_position))
9963 drm_err(dev, "DC failed to disable native cursor\n");
9964
9965 bundle->stream_update.cursor_position =
9966 &acrtc_state->stream->cursor_position;
9967 }
9968
9969 if (acrtc_state->active_planes == 0 &&
9970 dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE)
9971 amdgpu_dm_commit_cursors(state);
9972
9973 /* update planes when needed */
9974 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
9975 struct drm_crtc *crtc = new_plane_state->crtc;
9976 struct drm_crtc_state *new_crtc_state;
9977 struct drm_framebuffer *fb = new_plane_state->fb;
9978 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)fb;
9979 bool plane_needs_flip;
9980 struct dc_plane_state *dc_plane;
9981 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
9982
9983 /* Cursor plane is handled after stream updates */
9984 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
9985 acrtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE) {
9986 if ((fb && crtc == pcrtc) ||
9987 (old_plane_state->fb && old_plane_state->crtc == pcrtc)) {
9988 cursor_update = true;
9989 if (amdgpu_ip_version(dm->adev, DCE_HWIP, 0) != 0)
9990 amdgpu_dm_update_cursor(plane, old_plane_state, &bundle->stream_update);
9991 }
9992
9993 continue;
9994 }
9995
9996 if (!fb || !crtc || pcrtc != crtc)
9997 continue;
9998
9999 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
10000 if (!new_crtc_state->active)
10001 continue;
10002
10003 dc_plane = dm_new_plane_state->dc_state;
10004 if (!dc_plane)
10005 continue;
10006
10007 bundle->surface_updates[planes_count].surface = dc_plane;
10008 if (new_pcrtc_state->color_mgmt_changed) {
10009 bundle->surface_updates[planes_count].gamma = &dc_plane->gamma_correction;
10010 bundle->surface_updates[planes_count].in_transfer_func = &dc_plane->in_transfer_func;
10011 bundle->surface_updates[planes_count].gamut_remap_matrix = &dc_plane->gamut_remap_matrix;
10012 bundle->surface_updates[planes_count].hdr_mult = dc_plane->hdr_mult;
10013 bundle->surface_updates[planes_count].func_shaper = &dc_plane->in_shaper_func;
10014 bundle->surface_updates[planes_count].lut3d_func = &dc_plane->lut3d_func;
10015 bundle->surface_updates[planes_count].blend_tf = &dc_plane->blend_tf;
10016 }
10017
10018 amdgpu_dm_plane_fill_dc_scaling_info(dm->adev, new_plane_state,
10019 &bundle->scaling_infos[planes_count]);
10020
10021 bundle->surface_updates[planes_count].scaling_info =
10022 &bundle->scaling_infos[planes_count];
10023
10024 plane_needs_flip = old_plane_state->fb && new_plane_state->fb;
10025
10026 pflip_present = pflip_present || plane_needs_flip;
10027
10028 if (!plane_needs_flip) {
10029 planes_count += 1;
10030 continue;
10031 }
10032
10033 fill_dc_plane_info_and_addr(
10034 dm->adev, new_plane_state,
10035 afb->tiling_flags,
10036 &bundle->plane_infos[planes_count],
10037 &bundle->flip_addrs[planes_count].address,
10038 afb->tmz_surface);
10039
10040 drm_dbg_state(state->dev, "plane: id=%d dcc_en=%d\n",
10041 new_plane_state->plane->index,
10042 bundle->plane_infos[planes_count].dcc.enable);
10043
10044 bundle->surface_updates[planes_count].plane_info =
10045 &bundle->plane_infos[planes_count];
10046
10047 if (acrtc_state->stream->link->psr_settings.psr_feature_enabled ||
10048 acrtc_state->stream->link->replay_settings.replay_feature_enabled) {
10049 fill_dc_dirty_rects(plane, old_plane_state,
10050 new_plane_state, new_crtc_state,
10051 &bundle->flip_addrs[planes_count],
10052 acrtc_state->stream->link->psr_settings.psr_version ==
10053 DC_PSR_VERSION_SU_1,
10054 &dirty_rects_changed);
10055
10056 /*
10057 * If the dirty regions changed, PSR-SU need to be disabled temporarily
10058 * and enabled it again after dirty regions are stable to avoid video glitch.
10059 * PSR-SU will be enabled in
10060 * amdgpu_dm_crtc_vblank_control_worker() if user
10061 * pause the video during the PSR-SU was disabled.
10062 */
10063 if (acrtc_state->stream->link->psr_settings.psr_version >= DC_PSR_VERSION_SU_1 &&
10064 acrtc_attach->dm_irq_params.allow_sr_entry &&
10065#ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
10066 !amdgpu_dm_crc_window_is_activated(acrtc_state->base.crtc) &&
10067#endif
10068 dirty_rects_changed) {
10069 mutex_lock(&dm->dc_lock);
10070 acrtc_state->stream->link->psr_settings.psr_dirty_rects_change_timestamp_ns =
10071 timestamp_ns;
10072 if (acrtc_state->stream->link->psr_settings.psr_allow_active)
10073 amdgpu_dm_psr_disable(acrtc_state->stream, true);
10074 mutex_unlock(&dm->dc_lock);
10075 }
10076 }
10077
10078 /*
10079 * Only allow immediate flips for fast updates that don't
10080 * change memory domain, FB pitch, DCC state, rotation or
10081 * mirroring.
10082 *
10083 * dm_crtc_helper_atomic_check() only accepts async flips with
10084 * fast updates.
10085 */
10086 if (crtc->state->async_flip &&
10087 (acrtc_state->update_type != UPDATE_TYPE_FAST ||
10088 get_mem_type(old_plane_state->fb) != get_mem_type(fb)))
10089 drm_warn_once(state->dev,
10090 "[PLANE:%d:%s] async flip with non-fast update\n",
10091 plane->base.id, plane->name);
10092
10093 bundle->flip_addrs[planes_count].flip_immediate =
10094 crtc->state->async_flip &&
10095 acrtc_state->update_type == UPDATE_TYPE_FAST &&
10096 get_mem_type(old_plane_state->fb) == get_mem_type(fb);
10097
10098 timestamp_ns = ktime_get_ns();
10099 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000);
10100 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count];
10101 bundle->surface_updates[planes_count].surface = dc_plane;
10102
10103 if (!bundle->surface_updates[planes_count].surface) {
10104 drm_err(dev, "No surface for CRTC: id=%d\n",
10105 acrtc_attach->crtc_id);
10106 continue;
10107 }
10108
10109 if (plane == pcrtc->primary)
10110 update_freesync_state_on_stream(
10111 dm,
10112 acrtc_state,
10113 acrtc_state->stream,
10114 dc_plane,
10115 bundle->flip_addrs[planes_count].flip_timestamp_in_us);
10116
10117 drm_dbg_state(state->dev, "%s Flipping to hi: 0x%x, low: 0x%x\n",
10118 __func__,
10119 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
10120 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
10121
10122 planes_count += 1;
10123
10124 }
10125
10126 if (pflip_present) {
10127 if (!vrr_active) {
10128 /* Use old throttling in non-vrr fixed refresh rate mode
10129 * to keep flip scheduling based on target vblank counts
10130 * working in a backwards compatible way, e.g., for
10131 * clients using the GLX_OML_sync_control extension or
10132 * DRI3/Present extension with defined target_msc.
10133 */
10134 last_flip_vblank = amdgpu_get_vblank_counter_kms(pcrtc);
10135 } else {
10136 /* For variable refresh rate mode only:
10137 * Get vblank of last completed flip to avoid > 1 vrr
10138 * flips per video frame by use of throttling, but allow
10139 * flip programming anywhere in the possibly large
10140 * variable vrr vblank interval for fine-grained flip
10141 * timing control and more opportunity to avoid stutter
10142 * on late submission of flips.
10143 */
10144 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10145 last_flip_vblank = acrtc_attach->dm_irq_params.last_flip_vblank;
10146 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10147 }
10148
10149 target_vblank = last_flip_vblank + wait_for_vblank;
10150
10151 /*
10152 * Wait until we're out of the vertical blank period before the one
10153 * targeted by the flip
10154 */
10155 while ((acrtc_attach->enabled &&
10156 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id,
10157 0, &vpos, &hpos, NULL,
10158 NULL, &pcrtc->hwmode)
10159 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
10160 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
10161 (int)(target_vblank -
10162 amdgpu_get_vblank_counter_kms(pcrtc)) > 0)) {
10163 usleep_range(1000, 1100);
10164 }
10165
10166 /**
10167 * Prepare the flip event for the pageflip interrupt to handle.
10168 *
10169 * This only works in the case where we've already turned on the
10170 * appropriate hardware blocks (eg. HUBP) so in the transition case
10171 * from 0 -> n planes we have to skip a hardware generated event
10172 * and rely on sending it from software.
10173 */
10174 if (acrtc_attach->base.state->event &&
10175 acrtc_state->active_planes > 0) {
10176 drm_crtc_vblank_get(pcrtc);
10177
10178 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10179
10180 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE);
10181 prepare_flip_isr(acrtc_attach);
10182
10183 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10184 }
10185
10186 if (acrtc_state->stream) {
10187 if (acrtc_state->freesync_vrr_info_changed)
10188 bundle->stream_update.vrr_infopacket =
10189 &acrtc_state->stream->vrr_infopacket;
10190 }
10191 } else if (cursor_update && acrtc_state->active_planes > 0) {
10192 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10193 if (acrtc_attach->base.state->event) {
10194 drm_crtc_vblank_get(pcrtc);
10195 acrtc_attach->event = acrtc_attach->base.state->event;
10196 acrtc_attach->base.state->event = NULL;
10197 }
10198 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10199 }
10200
10201 /* Update the planes if changed or disable if we don't have any. */
10202 if ((planes_count || acrtc_state->active_planes == 0) &&
10203 acrtc_state->stream) {
10204 /*
10205 * If PSR or idle optimizations are enabled then flush out
10206 * any pending work before hardware programming.
10207 */
10208 if (dm->vblank_control_workqueue)
10209 flush_workqueue(dm->vblank_control_workqueue);
10210
10211 bundle->stream_update.stream = acrtc_state->stream;
10212 if (new_pcrtc_state->mode_changed) {
10213 bundle->stream_update.src = acrtc_state->stream->src;
10214 bundle->stream_update.dst = acrtc_state->stream->dst;
10215 }
10216
10217 if (new_pcrtc_state->color_mgmt_changed) {
10218 /*
10219 * TODO: This isn't fully correct since we've actually
10220 * already modified the stream in place.
10221 */
10222 bundle->stream_update.gamut_remap =
10223 &acrtc_state->stream->gamut_remap_matrix;
10224 bundle->stream_update.output_csc_transform =
10225 &acrtc_state->stream->csc_color_matrix;
10226 bundle->stream_update.out_transfer_func =
10227 &acrtc_state->stream->out_transfer_func;
10228 bundle->stream_update.lut3d_func =
10229 (struct dc_3dlut *) acrtc_state->stream->lut3d_func;
10230 bundle->stream_update.func_shaper =
10231 (struct dc_transfer_func *) acrtc_state->stream->func_shaper;
10232 }
10233
10234 acrtc_state->stream->abm_level = acrtc_state->abm_level;
10235 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level)
10236 bundle->stream_update.abm_level = &acrtc_state->abm_level;
10237
10238 mutex_lock(&dm->dc_lock);
10239 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) || vrr_active) {
10240 if (acrtc_state->stream->link->replay_settings.replay_allow_active)
10241 amdgpu_dm_replay_disable(acrtc_state->stream);
10242 if (acrtc_state->stream->link->psr_settings.psr_allow_active)
10243 amdgpu_dm_psr_disable(acrtc_state->stream, true);
10244 }
10245 mutex_unlock(&dm->dc_lock);
10246
10247 /*
10248 * If FreeSync state on the stream has changed then we need to
10249 * re-adjust the min/max bounds now that DC doesn't handle this
10250 * as part of commit.
10251 */
10252 if (is_dc_timing_adjust_needed(dm_old_crtc_state, acrtc_state)) {
10253 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10254 dc_stream_adjust_vmin_vmax(
10255 dm->dc, acrtc_state->stream,
10256 &acrtc_attach->dm_irq_params.vrr_params.adjust);
10257 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10258 }
10259 mutex_lock(&dm->dc_lock);
10260 update_planes_and_stream_adapter(dm->dc,
10261 acrtc_state->update_type,
10262 planes_count,
10263 acrtc_state->stream,
10264 &bundle->stream_update,
10265 bundle->surface_updates);
10266 updated_planes_and_streams = true;
10267
10268 /**
10269 * Enable or disable the interrupts on the backend.
10270 *
10271 * Most pipes are put into power gating when unused.
10272 *
10273 * When power gating is enabled on a pipe we lose the
10274 * interrupt enablement state when power gating is disabled.
10275 *
10276 * So we need to update the IRQ control state in hardware
10277 * whenever the pipe turns on (since it could be previously
10278 * power gated) or off (since some pipes can't be power gated
10279 * on some ASICs).
10280 */
10281 if (dm_old_crtc_state->active_planes != acrtc_state->active_planes)
10282 dm_update_pflip_irq_state(drm_to_adev(dev),
10283 acrtc_attach);
10284
10285 amdgpu_dm_enable_self_refresh(acrtc_attach, acrtc_state, timestamp_ns);
10286 mutex_unlock(&dm->dc_lock);
10287 }
10288
10289 /*
10290 * Update cursor state *after* programming all the planes.
10291 * This avoids redundant programming in the case where we're going
10292 * to be disabling a single plane - those pipes are being disabled.
10293 */
10294 if (acrtc_state->active_planes &&
10295 (!updated_planes_and_streams || amdgpu_ip_version(dm->adev, DCE_HWIP, 0) == 0) &&
10296 acrtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE)
10297 amdgpu_dm_commit_cursors(state);
10298
10299cleanup:
10300 kfree(bundle);
10301}
10302
10303static void amdgpu_dm_commit_audio(struct drm_device *dev,
10304 struct drm_atomic_state *state)
10305{
10306 struct amdgpu_device *adev = drm_to_adev(dev);
10307 struct amdgpu_dm_connector *aconnector;
10308 struct drm_connector *connector;
10309 struct drm_connector_state *old_con_state, *new_con_state;
10310 struct drm_crtc_state *new_crtc_state;
10311 struct dm_crtc_state *new_dm_crtc_state;
10312 const struct dc_stream_status *status;
10313 int i, inst;
10314
10315 /* Notify device removals. */
10316 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10317 if (old_con_state->crtc != new_con_state->crtc) {
10318 /* CRTC changes require notification. */
10319 goto notify;
10320 }
10321
10322 if (!new_con_state->crtc)
10323 continue;
10324
10325 new_crtc_state = drm_atomic_get_new_crtc_state(
10326 state, new_con_state->crtc);
10327
10328 if (!new_crtc_state)
10329 continue;
10330
10331 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
10332 continue;
10333
10334notify:
10335 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
10336 continue;
10337
10338 aconnector = to_amdgpu_dm_connector(connector);
10339
10340 mutex_lock(&adev->dm.audio_lock);
10341 inst = aconnector->audio_inst;
10342 aconnector->audio_inst = -1;
10343 mutex_unlock(&adev->dm.audio_lock);
10344
10345 amdgpu_dm_audio_eld_notify(adev, inst);
10346 }
10347
10348 /* Notify audio device additions. */
10349 for_each_new_connector_in_state(state, connector, new_con_state, i) {
10350 if (!new_con_state->crtc)
10351 continue;
10352
10353 new_crtc_state = drm_atomic_get_new_crtc_state(
10354 state, new_con_state->crtc);
10355
10356 if (!new_crtc_state)
10357 continue;
10358
10359 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
10360 continue;
10361
10362 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
10363 if (!new_dm_crtc_state->stream)
10364 continue;
10365
10366 status = dc_stream_get_status(new_dm_crtc_state->stream);
10367 if (!status)
10368 continue;
10369
10370 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
10371 continue;
10372
10373 aconnector = to_amdgpu_dm_connector(connector);
10374
10375 mutex_lock(&adev->dm.audio_lock);
10376 inst = status->audio_inst;
10377 aconnector->audio_inst = inst;
10378 mutex_unlock(&adev->dm.audio_lock);
10379
10380 amdgpu_dm_audio_eld_notify(adev, inst);
10381 }
10382}
10383
10384/*
10385 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
10386 * @crtc_state: the DRM CRTC state
10387 * @stream_state: the DC stream state.
10388 *
10389 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
10390 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
10391 */
10392static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
10393 struct dc_stream_state *stream_state)
10394{
10395 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
10396}
10397
10398static void dm_clear_writeback(struct amdgpu_display_manager *dm,
10399 struct dm_crtc_state *crtc_state)
10400{
10401 dc_stream_remove_writeback(dm->dc, crtc_state->stream, 0);
10402}
10403
10404static void amdgpu_dm_commit_streams(struct drm_atomic_state *state,
10405 struct dc_state *dc_state)
10406{
10407 struct drm_device *dev = state->dev;
10408 struct amdgpu_device *adev = drm_to_adev(dev);
10409 struct amdgpu_display_manager *dm = &adev->dm;
10410 struct drm_crtc *crtc;
10411 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10412 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10413 struct drm_connector_state *old_con_state;
10414 struct drm_connector *connector;
10415 bool mode_set_reset_required = false;
10416 u32 i;
10417 struct dc_commit_streams_params params = {dc_state->streams, dc_state->stream_count};
10418 bool set_backlight_level = false;
10419
10420 /* Disable writeback */
10421 for_each_old_connector_in_state(state, connector, old_con_state, i) {
10422 struct dm_connector_state *dm_old_con_state;
10423 struct amdgpu_crtc *acrtc;
10424
10425 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
10426 continue;
10427
10428 old_crtc_state = NULL;
10429
10430 dm_old_con_state = to_dm_connector_state(old_con_state);
10431 if (!dm_old_con_state->base.crtc)
10432 continue;
10433
10434 acrtc = to_amdgpu_crtc(dm_old_con_state->base.crtc);
10435 if (acrtc)
10436 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
10437
10438 if (!acrtc || !acrtc->wb_enabled)
10439 continue;
10440
10441 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10442
10443 dm_clear_writeback(dm, dm_old_crtc_state);
10444 acrtc->wb_enabled = false;
10445 }
10446
10447 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
10448 new_crtc_state, i) {
10449 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
10450
10451 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10452
10453 if (old_crtc_state->active &&
10454 (!new_crtc_state->active ||
10455 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
10456 manage_dm_interrupts(adev, acrtc, NULL);
10457 dc_stream_release(dm_old_crtc_state->stream);
10458 }
10459 }
10460
10461 drm_atomic_helper_calc_timestamping_constants(state);
10462
10463 /* update changed items */
10464 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10465 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
10466
10467 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10468 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10469
10470 drm_dbg_state(state->dev,
10471 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n",
10472 acrtc->crtc_id,
10473 new_crtc_state->enable,
10474 new_crtc_state->active,
10475 new_crtc_state->planes_changed,
10476 new_crtc_state->mode_changed,
10477 new_crtc_state->active_changed,
10478 new_crtc_state->connectors_changed);
10479
10480 /* Disable cursor if disabling crtc */
10481 if (old_crtc_state->active && !new_crtc_state->active) {
10482 struct dc_cursor_position position;
10483
10484 memset(&position, 0, sizeof(position));
10485 mutex_lock(&dm->dc_lock);
10486 dc_exit_ips_for_hw_access(dm->dc);
10487 dc_stream_program_cursor_position(dm_old_crtc_state->stream, &position);
10488 mutex_unlock(&dm->dc_lock);
10489 }
10490
10491 /* Copy all transient state flags into dc state */
10492 if (dm_new_crtc_state->stream) {
10493 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
10494 dm_new_crtc_state->stream);
10495 }
10496
10497 /* handles headless hotplug case, updating new_state and
10498 * aconnector as needed
10499 */
10500
10501 if (amdgpu_dm_crtc_modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
10502
10503 drm_dbg_atomic(dev,
10504 "Atomic commit: SET crtc id %d: [%p]\n",
10505 acrtc->crtc_id, acrtc);
10506
10507 if (!dm_new_crtc_state->stream) {
10508 /*
10509 * this could happen because of issues with
10510 * userspace notifications delivery.
10511 * In this case userspace tries to set mode on
10512 * display which is disconnected in fact.
10513 * dc_sink is NULL in this case on aconnector.
10514 * We expect reset mode will come soon.
10515 *
10516 * This can also happen when unplug is done
10517 * during resume sequence ended
10518 *
10519 * In this case, we want to pretend we still
10520 * have a sink to keep the pipe running so that
10521 * hw state is consistent with the sw state
10522 */
10523 drm_dbg_atomic(dev,
10524 "Failed to create new stream for crtc %d\n",
10525 acrtc->base.base.id);
10526 continue;
10527 }
10528
10529 if (dm_old_crtc_state->stream)
10530 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
10531
10532 pm_runtime_get_noresume(dev->dev);
10533
10534 acrtc->enabled = true;
10535 acrtc->hw_mode = new_crtc_state->mode;
10536 crtc->hwmode = new_crtc_state->mode;
10537 mode_set_reset_required = true;
10538 set_backlight_level = true;
10539 } else if (modereset_required(new_crtc_state)) {
10540 drm_dbg_atomic(dev,
10541 "Atomic commit: RESET. crtc id %d:[%p]\n",
10542 acrtc->crtc_id, acrtc);
10543 /* i.e. reset mode */
10544 if (dm_old_crtc_state->stream)
10545 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
10546
10547 mode_set_reset_required = true;
10548 }
10549 } /* for_each_crtc_in_state() */
10550
10551 /* if there mode set or reset, disable eDP PSR, Replay */
10552 if (mode_set_reset_required) {
10553 if (dm->vblank_control_workqueue)
10554 flush_workqueue(dm->vblank_control_workqueue);
10555
10556 amdgpu_dm_replay_disable_all(dm);
10557 amdgpu_dm_psr_disable_all(dm);
10558 }
10559
10560 dm_enable_per_frame_crtc_master_sync(dc_state);
10561 mutex_lock(&dm->dc_lock);
10562 dc_exit_ips_for_hw_access(dm->dc);
10563 WARN_ON(!dc_commit_streams(dm->dc, ¶ms));
10564
10565 /* Allow idle optimization when vblank count is 0 for display off */
10566 if ((dm->active_vblank_irq_count == 0) && amdgpu_dm_is_headless(dm->adev))
10567 dc_allow_idle_optimizations(dm->dc, true);
10568 mutex_unlock(&dm->dc_lock);
10569
10570 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
10571 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
10572
10573 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10574
10575 if (dm_new_crtc_state->stream != NULL) {
10576 const struct dc_stream_status *status =
10577 dc_stream_get_status(dm_new_crtc_state->stream);
10578
10579 if (!status)
10580 status = dc_state_get_stream_status(dc_state,
10581 dm_new_crtc_state->stream);
10582 if (!status)
10583 drm_err(dev,
10584 "got no status for stream %p on acrtc%p\n",
10585 dm_new_crtc_state->stream, acrtc);
10586 else
10587 acrtc->otg_inst = status->primary_otg_inst;
10588 }
10589 }
10590
10591 /* During boot up and resume the DC layer will reset the panel brightness
10592 * to fix a flicker issue.
10593 * It will cause the dm->actual_brightness is not the current panel brightness
10594 * level. (the dm->brightness is the correct panel level)
10595 * So we set the backlight level with dm->brightness value after set mode
10596 */
10597 if (set_backlight_level) {
10598 for (i = 0; i < dm->num_of_edps; i++) {
10599 if (dm->backlight_dev[i])
10600 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
10601 }
10602 }
10603}
10604
10605static void dm_set_writeback(struct amdgpu_display_manager *dm,
10606 struct dm_crtc_state *crtc_state,
10607 struct drm_connector *connector,
10608 struct drm_connector_state *new_con_state)
10609{
10610 struct drm_writeback_connector *wb_conn = drm_connector_to_writeback(connector);
10611 struct amdgpu_device *adev = dm->adev;
10612 struct amdgpu_crtc *acrtc;
10613 struct dc_writeback_info *wb_info;
10614 struct pipe_ctx *pipe = NULL;
10615 struct amdgpu_framebuffer *afb;
10616 int i = 0;
10617
10618 wb_info = kzalloc_obj(*wb_info);
10619 if (!wb_info) {
10620 drm_err(adev_to_drm(adev), "Failed to allocate wb_info\n");
10621 return;
10622 }
10623
10624 acrtc = to_amdgpu_crtc(wb_conn->encoder.crtc);
10625 if (!acrtc) {
10626 drm_err(adev_to_drm(adev), "no amdgpu_crtc found\n");
10627 kfree(wb_info);
10628 return;
10629 }
10630
10631 afb = to_amdgpu_framebuffer(new_con_state->writeback_job->fb);
10632 if (!afb) {
10633 drm_err(adev_to_drm(adev), "No amdgpu_framebuffer found\n");
10634 kfree(wb_info);
10635 return;
10636 }
10637
10638 for (i = 0; i < MAX_PIPES; i++) {
10639 if (dm->dc->current_state->res_ctx.pipe_ctx[i].stream == crtc_state->stream) {
10640 pipe = &dm->dc->current_state->res_ctx.pipe_ctx[i];
10641 break;
10642 }
10643 }
10644
10645 /* fill in wb_info */
10646 wb_info->wb_enabled = true;
10647
10648 wb_info->dwb_pipe_inst = 0;
10649 wb_info->dwb_params.dwbscl_black_color = 0;
10650 wb_info->dwb_params.hdr_mult = 0x1F000;
10651 wb_info->dwb_params.csc_params.gamut_adjust_type = CM_GAMUT_ADJUST_TYPE_BYPASS;
10652 wb_info->dwb_params.csc_params.gamut_coef_format = CM_GAMUT_REMAP_COEF_FORMAT_S2_13;
10653 wb_info->dwb_params.output_depth = DWB_OUTPUT_PIXEL_DEPTH_10BPC;
10654 wb_info->dwb_params.cnv_params.cnv_out_bpc = DWB_CNV_OUT_BPC_10BPC;
10655
10656 /* width & height from crtc */
10657 wb_info->dwb_params.cnv_params.src_width = acrtc->base.mode.crtc_hdisplay;
10658 wb_info->dwb_params.cnv_params.src_height = acrtc->base.mode.crtc_vdisplay;
10659 wb_info->dwb_params.dest_width = acrtc->base.mode.crtc_hdisplay;
10660 wb_info->dwb_params.dest_height = acrtc->base.mode.crtc_vdisplay;
10661
10662 wb_info->dwb_params.cnv_params.crop_en = false;
10663 wb_info->dwb_params.stereo_params.stereo_enabled = false;
10664
10665 wb_info->dwb_params.cnv_params.out_max_pix_val = 0x3ff; // 10 bits
10666 wb_info->dwb_params.cnv_params.out_min_pix_val = 0;
10667 wb_info->dwb_params.cnv_params.fc_out_format = DWB_OUT_FORMAT_32BPP_ARGB;
10668 wb_info->dwb_params.cnv_params.out_denorm_mode = DWB_OUT_DENORM_BYPASS;
10669
10670 wb_info->dwb_params.out_format = dwb_scaler_mode_bypass444;
10671
10672 wb_info->dwb_params.capture_rate = dwb_capture_rate_0;
10673
10674 wb_info->dwb_params.scaler_taps.h_taps = 1;
10675 wb_info->dwb_params.scaler_taps.v_taps = 1;
10676 wb_info->dwb_params.scaler_taps.h_taps_c = 1;
10677 wb_info->dwb_params.scaler_taps.v_taps_c = 1;
10678 wb_info->dwb_params.subsample_position = DWB_INTERSTITIAL_SUBSAMPLING;
10679
10680 wb_info->mcif_buf_params.luma_pitch = afb->base.pitches[0];
10681 wb_info->mcif_buf_params.chroma_pitch = afb->base.pitches[1];
10682
10683 for (i = 0; i < DWB_MCIF_BUF_COUNT; i++) {
10684 wb_info->mcif_buf_params.luma_address[i] = afb->address;
10685 wb_info->mcif_buf_params.chroma_address[i] = 0;
10686 }
10687
10688 wb_info->mcif_buf_params.p_vmid = 1;
10689 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 0, 0)) {
10690 wb_info->mcif_warmup_params.start_address.quad_part = afb->address;
10691 wb_info->mcif_warmup_params.region_size =
10692 wb_info->mcif_buf_params.luma_pitch * wb_info->dwb_params.dest_height;
10693 }
10694 wb_info->mcif_warmup_params.p_vmid = 1;
10695 wb_info->writeback_source_plane = pipe->plane_state;
10696
10697 dc_stream_add_writeback(dm->dc, crtc_state->stream, wb_info);
10698
10699 acrtc->wb_pending = true;
10700 acrtc->wb_conn = wb_conn;
10701 drm_writeback_queue_job(wb_conn, new_con_state);
10702}
10703
10704static void amdgpu_dm_update_hdcp(struct drm_atomic_state *state)
10705{
10706 struct drm_connector_state *old_con_state, *new_con_state;
10707 struct drm_device *dev = state->dev;
10708 struct drm_connector *connector;
10709 struct amdgpu_device *adev = drm_to_adev(dev);
10710 int i;
10711
10712 if (!adev->dm.hdcp_workqueue)
10713 return;
10714
10715 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10716 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
10717 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
10718 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10719 struct dm_crtc_state *dm_new_crtc_state;
10720 struct amdgpu_dm_connector *aconnector;
10721
10722 if (!connector || connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
10723 continue;
10724
10725 aconnector = to_amdgpu_dm_connector(connector);
10726
10727 drm_dbg(dev, "[HDCP_DM] -------------- i : %x ----------\n", i);
10728
10729 drm_dbg(dev, "[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n",
10730 connector->index, connector->status, connector->dpms);
10731 drm_dbg(dev, "[HDCP_DM] state protection old: %x new: %x\n",
10732 old_con_state->content_protection, new_con_state->content_protection);
10733
10734 if (aconnector->dc_sink) {
10735 if (aconnector->dc_sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
10736 aconnector->dc_sink->sink_signal != SIGNAL_TYPE_NONE) {
10737 drm_dbg(dev, "[HDCP_DM] pipe_ctx dispname=%s\n",
10738 aconnector->dc_sink->edid_caps.display_name);
10739 }
10740 }
10741
10742 new_crtc_state = NULL;
10743 old_crtc_state = NULL;
10744
10745 if (acrtc) {
10746 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
10747 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
10748 }
10749
10750 if (old_crtc_state)
10751 drm_dbg(dev, "old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
10752 old_crtc_state->enable,
10753 old_crtc_state->active,
10754 old_crtc_state->mode_changed,
10755 old_crtc_state->active_changed,
10756 old_crtc_state->connectors_changed);
10757
10758 if (new_crtc_state)
10759 drm_dbg(dev, "NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
10760 new_crtc_state->enable,
10761 new_crtc_state->active,
10762 new_crtc_state->mode_changed,
10763 new_crtc_state->active_changed,
10764 new_crtc_state->connectors_changed);
10765
10766
10767 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10768
10769 if (dm_new_crtc_state && dm_new_crtc_state->stream == NULL &&
10770 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
10771 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
10772 new_con_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
10773 dm_new_con_state->update_hdcp = true;
10774 continue;
10775 }
10776
10777 if (is_content_protection_different(new_crtc_state, old_crtc_state, new_con_state,
10778 old_con_state, connector, adev->dm.hdcp_workqueue)) {
10779 /* when display is unplugged from mst hub, connctor will
10780 * be destroyed within dm_dp_mst_connector_destroy. connector
10781 * hdcp perperties, like type, undesired, desired, enabled,
10782 * will be lost. So, save hdcp properties into hdcp_work within
10783 * amdgpu_dm_atomic_commit_tail. if the same display is
10784 * plugged back with same display index, its hdcp properties
10785 * will be retrieved from hdcp_work within dm_dp_mst_get_modes
10786 */
10787
10788 bool enable_encryption = false;
10789
10790 if (new_con_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED)
10791 enable_encryption = true;
10792
10793 if (aconnector->dc_link && aconnector->dc_sink &&
10794 aconnector->dc_link->type == dc_connection_mst_branch) {
10795 struct hdcp_workqueue *hdcp_work = adev->dm.hdcp_workqueue;
10796 struct hdcp_workqueue *hdcp_w =
10797 &hdcp_work[aconnector->dc_link->link_index];
10798
10799 hdcp_w->hdcp_content_type[connector->index] =
10800 new_con_state->hdcp_content_type;
10801 hdcp_w->content_protection[connector->index] =
10802 new_con_state->content_protection;
10803 }
10804
10805 if (new_crtc_state && new_crtc_state->mode_changed &&
10806 new_con_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED)
10807 enable_encryption = true;
10808
10809 drm_info(dev, "[HDCP_DM] hdcp_update_display enable_encryption = %x\n", enable_encryption);
10810
10811 if (aconnector->dc_link)
10812 hdcp_update_display(
10813 adev->dm.hdcp_workqueue, aconnector->dc_link->link_index, aconnector,
10814 new_con_state->hdcp_content_type, enable_encryption);
10815 }
10816 }
10817}
10818
10819static int amdgpu_dm_atomic_setup_commit(struct drm_atomic_state *state)
10820{
10821 struct drm_crtc *crtc;
10822 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10823 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10824 int i, ret;
10825
10826 ret = drm_dp_mst_atomic_setup_commit(state);
10827 if (ret)
10828 return ret;
10829
10830 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10831 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10832 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10833 /*
10834 * Color management settings. We also update color properties
10835 * when a modeset is needed, to ensure it gets reprogrammed.
10836 */
10837 if (dm_new_crtc_state->base.active && dm_new_crtc_state->stream &&
10838 (dm_new_crtc_state->base.color_mgmt_changed ||
10839 dm_old_crtc_state->regamma_tf != dm_new_crtc_state->regamma_tf ||
10840 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
10841 ret = amdgpu_dm_update_crtc_color_mgmt(dm_new_crtc_state);
10842 if (ret) {
10843 drm_dbg_atomic(state->dev, "Failed to update color state\n");
10844 return ret;
10845 }
10846 }
10847 }
10848
10849 return 0;
10850}
10851
10852/**
10853 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation.
10854 * @state: The atomic state to commit
10855 *
10856 * This will tell DC to commit the constructed DC state from atomic_check,
10857 * programming the hardware. Any failures here implies a hardware failure, since
10858 * atomic check should have filtered anything non-kosher.
10859 */
10860static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
10861{
10862 struct drm_device *dev = state->dev;
10863 struct amdgpu_device *adev = drm_to_adev(dev);
10864 struct amdgpu_display_manager *dm = &adev->dm;
10865 struct dm_atomic_state *dm_state;
10866 struct dc_state *dc_state = NULL;
10867 u32 i, j;
10868 struct drm_crtc *crtc;
10869 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10870 unsigned long flags;
10871 bool wait_for_vblank = true;
10872 struct drm_connector *connector;
10873 struct drm_connector_state *old_con_state = NULL, *new_con_state = NULL;
10874 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10875 int crtc_disable_count = 0;
10876
10877 trace_amdgpu_dm_atomic_commit_tail_begin(state);
10878
10879 drm_atomic_helper_update_legacy_modeset_state(dev, state);
10880 drm_dp_mst_atomic_wait_for_dependencies(state);
10881
10882 dm_state = dm_atomic_get_new_state(state);
10883 if (dm_state && dm_state->context) {
10884 dc_state = dm_state->context;
10885 amdgpu_dm_commit_streams(state, dc_state);
10886 }
10887
10888 amdgpu_dm_update_hdcp(state);
10889
10890 /* Handle connector state changes */
10891 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10892 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
10893 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
10894 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
10895 struct dc_surface_update *dummy_updates;
10896 struct dc_stream_update stream_update;
10897 struct dc_info_packet hdr_packet;
10898 struct dc_stream_status *status = NULL;
10899 bool abm_changed, hdr_changed, scaling_changed, output_color_space_changed = false;
10900
10901 memset(&stream_update, 0, sizeof(stream_update));
10902
10903 if (acrtc) {
10904 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
10905 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
10906 }
10907
10908 /* Skip any modesets/resets */
10909 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
10910 continue;
10911
10912 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10913 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10914
10915 scaling_changed = is_scaling_state_different(dm_new_con_state,
10916 dm_old_con_state);
10917
10918 if ((new_con_state->hdmi.broadcast_rgb != old_con_state->hdmi.broadcast_rgb) &&
10919 (dm_old_crtc_state->stream->output_color_space !=
10920 get_output_color_space(&dm_new_crtc_state->stream->timing, new_con_state)))
10921 output_color_space_changed = true;
10922
10923 abm_changed = dm_new_crtc_state->abm_level !=
10924 dm_old_crtc_state->abm_level;
10925
10926 hdr_changed =
10927 !drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state);
10928
10929 if (!scaling_changed && !abm_changed && !hdr_changed && !output_color_space_changed)
10930 continue;
10931
10932 stream_update.stream = dm_new_crtc_state->stream;
10933 if (scaling_changed) {
10934 update_stream_scaling_settings(dev, &dm_new_con_state->base.crtc->mode,
10935 dm_new_con_state, dm_new_crtc_state->stream);
10936
10937 stream_update.src = dm_new_crtc_state->stream->src;
10938 stream_update.dst = dm_new_crtc_state->stream->dst;
10939 }
10940
10941 if (output_color_space_changed) {
10942 dm_new_crtc_state->stream->output_color_space
10943 = get_output_color_space(&dm_new_crtc_state->stream->timing, new_con_state);
10944
10945 stream_update.output_color_space = &dm_new_crtc_state->stream->output_color_space;
10946 }
10947
10948 if (abm_changed) {
10949 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level;
10950
10951 stream_update.abm_level = &dm_new_crtc_state->abm_level;
10952 }
10953
10954 if (hdr_changed) {
10955 fill_hdr_info_packet(new_con_state, &hdr_packet);
10956 stream_update.hdr_static_metadata = &hdr_packet;
10957 }
10958
10959 status = dc_stream_get_status(dm_new_crtc_state->stream);
10960
10961 if (WARN_ON(!status))
10962 continue;
10963
10964 WARN_ON(!status->plane_count);
10965
10966 /*
10967 * TODO: DC refuses to perform stream updates without a dc_surface_update.
10968 * Here we create an empty update on each plane.
10969 * To fix this, DC should permit updating only stream properties.
10970 */
10971 dummy_updates = kzalloc(sizeof(struct dc_surface_update) * MAX_SURFACES, GFP_KERNEL);
10972 if (!dummy_updates) {
10973 drm_err(adev_to_drm(adev), "Failed to allocate memory for dummy_updates.\n");
10974 continue;
10975 }
10976 for (j = 0; j < status->plane_count; j++)
10977 dummy_updates[j].surface = status->plane_states[j];
10978
10979 sort(dummy_updates, status->plane_count,
10980 sizeof(*dummy_updates), dm_plane_layer_index_cmp, NULL);
10981
10982 mutex_lock(&dm->dc_lock);
10983 dc_exit_ips_for_hw_access(dm->dc);
10984 dc_update_planes_and_stream(dm->dc,
10985 dummy_updates,
10986 status->plane_count,
10987 dm_new_crtc_state->stream,
10988 &stream_update);
10989 mutex_unlock(&dm->dc_lock);
10990 kfree(dummy_updates);
10991
10992 drm_connector_update_privacy_screen(new_con_state);
10993 }
10994
10995 /**
10996 * Enable interrupts for CRTCs that are newly enabled or went through
10997 * a modeset. It was intentionally deferred until after the front end
10998 * state was modified to wait until the OTG was on and so the IRQ
10999 * handlers didn't access stale or invalid state.
11000 */
11001 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
11002 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
11003#ifdef CONFIG_DEBUG_FS
11004 enum amdgpu_dm_pipe_crc_source cur_crc_src;
11005#endif
11006 /* Count number of newly disabled CRTCs for dropping PM refs later. */
11007 if (old_crtc_state->active && !new_crtc_state->active)
11008 crtc_disable_count++;
11009
11010 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11011 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
11012
11013 /* For freesync config update on crtc state and params for irq */
11014 update_stream_irq_parameters(dm, dm_new_crtc_state);
11015
11016#ifdef CONFIG_DEBUG_FS
11017 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
11018 cur_crc_src = acrtc->dm_irq_params.crc_src;
11019 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
11020#endif
11021
11022 if (new_crtc_state->active &&
11023 (!old_crtc_state->active ||
11024 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
11025 dc_stream_retain(dm_new_crtc_state->stream);
11026 acrtc->dm_irq_params.stream = dm_new_crtc_state->stream;
11027 manage_dm_interrupts(adev, acrtc, dm_new_crtc_state);
11028 }
11029 /* Handle vrr on->off / off->on transitions */
11030 amdgpu_dm_handle_vrr_transition(dm_old_crtc_state, dm_new_crtc_state);
11031
11032#ifdef CONFIG_DEBUG_FS
11033 if (new_crtc_state->active &&
11034 (!old_crtc_state->active ||
11035 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
11036 /**
11037 * Frontend may have changed so reapply the CRC capture
11038 * settings for the stream.
11039 */
11040 if (amdgpu_dm_is_valid_crc_source(cur_crc_src)) {
11041#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
11042 if (amdgpu_dm_crc_window_is_activated(crtc)) {
11043 uint8_t cnt;
11044
11045 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
11046 for (cnt = 0; cnt < MAX_CRC_WINDOW_NUM; cnt++) {
11047 if (acrtc->dm_irq_params.window_param[cnt].enable) {
11048 acrtc->dm_irq_params.window_param[cnt].update_win = true;
11049
11050 /**
11051 * It takes 2 frames for HW to stably generate CRC when
11052 * resuming from suspend, so we set skip_frame_cnt 2.
11053 */
11054 acrtc->dm_irq_params.window_param[cnt].skip_frame_cnt = 2;
11055 }
11056 }
11057 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
11058 }
11059#endif
11060 if (amdgpu_dm_crtc_configure_crc_source(
11061 crtc, dm_new_crtc_state, cur_crc_src))
11062 drm_dbg_atomic(dev, "Failed to configure crc source");
11063 }
11064 }
11065#endif
11066 }
11067
11068 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j)
11069 if (new_crtc_state->async_flip)
11070 wait_for_vblank = false;
11071
11072 /* update planes when needed per crtc*/
11073 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
11074 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11075
11076 if (dm_new_crtc_state->stream)
11077 amdgpu_dm_commit_planes(state, dev, dm, crtc, wait_for_vblank);
11078 }
11079
11080 /* Enable writeback */
11081 for_each_new_connector_in_state(state, connector, new_con_state, i) {
11082 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
11083 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
11084
11085 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
11086 continue;
11087
11088 if (!new_con_state->writeback_job)
11089 continue;
11090
11091 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
11092
11093 if (!new_crtc_state)
11094 continue;
11095
11096 if (acrtc->wb_enabled)
11097 continue;
11098
11099 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11100
11101 dm_set_writeback(dm, dm_new_crtc_state, connector, new_con_state);
11102 acrtc->wb_enabled = true;
11103 }
11104
11105 /* Update audio instances for each connector. */
11106 amdgpu_dm_commit_audio(dev, state);
11107
11108 /* restore the backlight level */
11109 for (i = 0; i < dm->num_of_edps; i++) {
11110 if (dm->backlight_dev[i] &&
11111 (dm->actual_brightness[i] != dm->brightness[i]))
11112 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
11113 }
11114
11115 /*
11116 * send vblank event on all events not handled in flip and
11117 * mark consumed event for drm_atomic_helper_commit_hw_done
11118 */
11119 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
11120 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
11121
11122 if (new_crtc_state->event)
11123 drm_send_event_locked(dev, &new_crtc_state->event->base);
11124
11125 new_crtc_state->event = NULL;
11126 }
11127 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
11128
11129 /* Signal HW programming completion */
11130 drm_atomic_helper_commit_hw_done(state);
11131
11132 if (wait_for_vblank)
11133 drm_atomic_helper_wait_for_flip_done(dev, state);
11134
11135 drm_atomic_helper_cleanup_planes(dev, state);
11136
11137 /* Don't free the memory if we are hitting this as part of suspend.
11138 * This way we don't free any memory during suspend; see
11139 * amdgpu_bo_free_kernel(). The memory will be freed in the first
11140 * non-suspend modeset or when the driver is torn down.
11141 */
11142 if (!adev->in_suspend) {
11143 /* return the stolen vga memory back to VRAM */
11144 if (!adev->mman.keep_stolen_vga_memory)
11145 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
11146 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL);
11147 }
11148
11149 /*
11150 * Finally, drop a runtime PM reference for each newly disabled CRTC,
11151 * so we can put the GPU into runtime suspend if we're not driving any
11152 * displays anymore
11153 */
11154 for (i = 0; i < crtc_disable_count; i++)
11155 pm_runtime_put_autosuspend(dev->dev);
11156 pm_runtime_mark_last_busy(dev->dev);
11157
11158 trace_amdgpu_dm_atomic_commit_tail_finish(state);
11159}
11160
11161static int dm_force_atomic_commit(struct drm_connector *connector)
11162{
11163 int ret = 0;
11164 struct drm_device *ddev = connector->dev;
11165 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
11166 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
11167 struct drm_plane *plane = disconnected_acrtc->base.primary;
11168 struct drm_connector_state *conn_state;
11169 struct drm_crtc_state *crtc_state;
11170 struct drm_plane_state *plane_state;
11171
11172 if (!state)
11173 return -ENOMEM;
11174
11175 state->acquire_ctx = ddev->mode_config.acquire_ctx;
11176
11177 /* Construct an atomic state to restore previous display setting */
11178
11179 /*
11180 * Attach connectors to drm_atomic_state
11181 */
11182 conn_state = drm_atomic_get_connector_state(state, connector);
11183
11184 /* Check for error in getting connector state */
11185 if (IS_ERR(conn_state)) {
11186 ret = PTR_ERR(conn_state);
11187 goto out;
11188 }
11189
11190 /* Attach crtc to drm_atomic_state*/
11191 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
11192
11193 /* Check for error in getting crtc state */
11194 if (IS_ERR(crtc_state)) {
11195 ret = PTR_ERR(crtc_state);
11196 goto out;
11197 }
11198
11199 /* force a restore */
11200 crtc_state->mode_changed = true;
11201
11202 /* Attach plane to drm_atomic_state */
11203 plane_state = drm_atomic_get_plane_state(state, plane);
11204
11205 /* Check for error in getting plane state */
11206 if (IS_ERR(plane_state)) {
11207 ret = PTR_ERR(plane_state);
11208 goto out;
11209 }
11210
11211 /* Call commit internally with the state we just constructed */
11212 ret = drm_atomic_commit(state);
11213
11214out:
11215 drm_atomic_state_put(state);
11216 if (ret)
11217 drm_err(ddev, "Restoring old state failed with %i\n", ret);
11218
11219 return ret;
11220}
11221
11222/*
11223 * This function handles all cases when set mode does not come upon hotplug.
11224 * This includes when a display is unplugged then plugged back into the
11225 * same port and when running without usermode desktop manager supprot
11226 */
11227void dm_restore_drm_connector_state(struct drm_device *dev,
11228 struct drm_connector *connector)
11229{
11230 struct amdgpu_dm_connector *aconnector;
11231 struct amdgpu_crtc *disconnected_acrtc;
11232 struct dm_crtc_state *acrtc_state;
11233
11234 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
11235 return;
11236
11237 aconnector = to_amdgpu_dm_connector(connector);
11238
11239 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
11240 return;
11241
11242 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
11243 if (!disconnected_acrtc)
11244 return;
11245
11246 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
11247 if (!acrtc_state->stream)
11248 return;
11249
11250 /*
11251 * If the previous sink is not released and different from the current,
11252 * we deduce we are in a state where we can not rely on usermode call
11253 * to turn on the display, so we do it here
11254 */
11255 if (acrtc_state->stream->sink != aconnector->dc_sink)
11256 dm_force_atomic_commit(&aconnector->base);
11257}
11258
11259/*
11260 * Grabs all modesetting locks to serialize against any blocking commits,
11261 * Waits for completion of all non blocking commits.
11262 */
11263static int do_aquire_global_lock(struct drm_device *dev,
11264 struct drm_atomic_state *state)
11265{
11266 struct drm_crtc *crtc;
11267 struct drm_crtc_commit *commit;
11268 long ret;
11269
11270 /*
11271 * Adding all modeset locks to aquire_ctx will
11272 * ensure that when the framework release it the
11273 * extra locks we are locking here will get released to
11274 */
11275 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
11276 if (ret)
11277 return ret;
11278
11279 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11280 spin_lock(&crtc->commit_lock);
11281 commit = list_first_entry_or_null(&crtc->commit_list,
11282 struct drm_crtc_commit, commit_entry);
11283 if (commit)
11284 drm_crtc_commit_get(commit);
11285 spin_unlock(&crtc->commit_lock);
11286
11287 if (!commit)
11288 continue;
11289
11290 /*
11291 * Make sure all pending HW programming completed and
11292 * page flips done
11293 */
11294 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
11295
11296 if (ret > 0)
11297 ret = wait_for_completion_interruptible_timeout(
11298 &commit->flip_done, 10*HZ);
11299
11300 if (ret == 0)
11301 drm_err(dev, "[CRTC:%d:%s] hw_done or flip_done timed out\n",
11302 crtc->base.id, crtc->name);
11303
11304 drm_crtc_commit_put(commit);
11305 }
11306
11307 return ret < 0 ? ret : 0;
11308}
11309
11310static void get_freesync_config_for_crtc(
11311 struct dm_crtc_state *new_crtc_state,
11312 struct dm_connector_state *new_con_state)
11313{
11314 struct mod_freesync_config config = {0};
11315 struct amdgpu_dm_connector *aconnector;
11316 struct drm_display_mode *mode = &new_crtc_state->base.mode;
11317 int vrefresh = drm_mode_vrefresh(mode);
11318 bool fs_vid_mode = false;
11319
11320 if (new_con_state->base.connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
11321 return;
11322
11323 aconnector = to_amdgpu_dm_connector(new_con_state->base.connector);
11324
11325 new_crtc_state->vrr_supported = new_con_state->freesync_capable &&
11326 vrefresh >= aconnector->min_vfreq &&
11327 vrefresh <= aconnector->max_vfreq;
11328
11329 if (new_crtc_state->vrr_supported) {
11330 new_crtc_state->stream->ignore_msa_timing_param = true;
11331 fs_vid_mode = new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
11332
11333 config.min_refresh_in_uhz = aconnector->min_vfreq * 1000000;
11334 config.max_refresh_in_uhz = aconnector->max_vfreq * 1000000;
11335 config.vsif_supported = true;
11336 config.btr = true;
11337
11338 if (fs_vid_mode) {
11339 config.state = VRR_STATE_ACTIVE_FIXED;
11340 config.fixed_refresh_in_uhz = new_crtc_state->freesync_config.fixed_refresh_in_uhz;
11341 goto out;
11342 } else if (new_crtc_state->base.vrr_enabled) {
11343 config.state = VRR_STATE_ACTIVE_VARIABLE;
11344 } else {
11345 config.state = VRR_STATE_INACTIVE;
11346 }
11347 } else {
11348 config.state = VRR_STATE_UNSUPPORTED;
11349 }
11350out:
11351 new_crtc_state->freesync_config = config;
11352}
11353
11354static void reset_freesync_config_for_crtc(
11355 struct dm_crtc_state *new_crtc_state)
11356{
11357 new_crtc_state->vrr_supported = false;
11358
11359 memset(&new_crtc_state->vrr_infopacket, 0,
11360 sizeof(new_crtc_state->vrr_infopacket));
11361}
11362
11363static bool
11364is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
11365 struct drm_crtc_state *new_crtc_state)
11366{
11367 const struct drm_display_mode *old_mode, *new_mode;
11368
11369 if (!old_crtc_state || !new_crtc_state)
11370 return false;
11371
11372 old_mode = &old_crtc_state->mode;
11373 new_mode = &new_crtc_state->mode;
11374
11375 if (old_mode->clock == new_mode->clock &&
11376 old_mode->hdisplay == new_mode->hdisplay &&
11377 old_mode->vdisplay == new_mode->vdisplay &&
11378 old_mode->htotal == new_mode->htotal &&
11379 old_mode->vtotal != new_mode->vtotal &&
11380 old_mode->hsync_start == new_mode->hsync_start &&
11381 old_mode->vsync_start != new_mode->vsync_start &&
11382 old_mode->hsync_end == new_mode->hsync_end &&
11383 old_mode->vsync_end != new_mode->vsync_end &&
11384 old_mode->hskew == new_mode->hskew &&
11385 old_mode->vscan == new_mode->vscan &&
11386 (old_mode->vsync_end - old_mode->vsync_start) ==
11387 (new_mode->vsync_end - new_mode->vsync_start))
11388 return true;
11389
11390 return false;
11391}
11392
11393static void set_freesync_fixed_config(struct dm_crtc_state *dm_new_crtc_state)
11394{
11395 u64 num, den, res;
11396 struct drm_crtc_state *new_crtc_state = &dm_new_crtc_state->base;
11397
11398 dm_new_crtc_state->freesync_config.state = VRR_STATE_ACTIVE_FIXED;
11399
11400 num = (unsigned long long)new_crtc_state->mode.clock * 1000 * 1000000;
11401 den = (unsigned long long)new_crtc_state->mode.htotal *
11402 (unsigned long long)new_crtc_state->mode.vtotal;
11403
11404 res = div_u64(num, den);
11405 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = res;
11406}
11407
11408static int dm_update_crtc_state(struct amdgpu_display_manager *dm,
11409 struct drm_atomic_state *state,
11410 struct drm_crtc *crtc,
11411 struct drm_crtc_state *old_crtc_state,
11412 struct drm_crtc_state *new_crtc_state,
11413 bool enable,
11414 bool *lock_and_validation_needed)
11415{
11416 struct dm_atomic_state *dm_state = NULL;
11417 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
11418 struct dc_stream_state *new_stream;
11419 struct amdgpu_device *adev = dm->adev;
11420 int ret = 0;
11421
11422 /*
11423 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
11424 * update changed items
11425 */
11426 struct amdgpu_crtc *acrtc = NULL;
11427 struct drm_connector *connector = NULL;
11428 struct amdgpu_dm_connector *aconnector = NULL;
11429 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
11430 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
11431
11432 new_stream = NULL;
11433
11434 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
11435 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11436 acrtc = to_amdgpu_crtc(crtc);
11437 connector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
11438 if (connector)
11439 aconnector = to_amdgpu_dm_connector(connector);
11440
11441 /* TODO This hack should go away */
11442 if (connector && enable) {
11443 /* Make sure fake sink is created in plug-in scenario */
11444 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
11445 connector);
11446 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
11447 connector);
11448
11449 if (WARN_ON(!drm_new_conn_state)) {
11450 ret = -EINVAL;
11451 goto fail;
11452 }
11453
11454 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
11455 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
11456
11457 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
11458 goto skip_modeset;
11459
11460 new_stream = create_validate_stream_for_sink(connector,
11461 &new_crtc_state->mode,
11462 dm_new_conn_state,
11463 dm_old_crtc_state->stream);
11464
11465 /*
11466 * we can have no stream on ACTION_SET if a display
11467 * was disconnected during S3, in this case it is not an
11468 * error, the OS will be updated after detection, and
11469 * will do the right thing on next atomic commit
11470 */
11471
11472 if (!new_stream) {
11473 drm_dbg_driver(adev_to_drm(adev), "%s: Failed to create new stream for crtc %d\n",
11474 __func__, acrtc->base.base.id);
11475 ret = -ENOMEM;
11476 goto fail;
11477 }
11478
11479 /*
11480 * TODO: Check VSDB bits to decide whether this should
11481 * be enabled or not.
11482 */
11483 new_stream->triggered_crtc_reset.enabled =
11484 dm->force_timing_sync;
11485
11486 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
11487
11488 ret = fill_hdr_info_packet(drm_new_conn_state,
11489 &new_stream->hdr_static_metadata);
11490 if (ret)
11491 goto fail;
11492
11493 /*
11494 * If we already removed the old stream from the context
11495 * (and set the new stream to NULL) then we can't reuse
11496 * the old stream even if the stream and scaling are unchanged.
11497 * We'll hit the BUG_ON and black screen.
11498 *
11499 * TODO: Refactor this function to allow this check to work
11500 * in all conditions.
11501 */
11502 if (amdgpu_freesync_vid_mode &&
11503 dm_new_crtc_state->stream &&
11504 is_timing_unchanged_for_freesync(new_crtc_state, old_crtc_state))
11505 goto skip_modeset;
11506
11507 if (dm_new_crtc_state->stream &&
11508 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
11509 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
11510 new_crtc_state->mode_changed = false;
11511 drm_dbg_driver(adev_to_drm(adev), "Mode change not required, setting mode_changed to %d",
11512 new_crtc_state->mode_changed);
11513 }
11514 }
11515
11516 /* mode_changed flag may get updated above, need to check again */
11517 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
11518 goto skip_modeset;
11519
11520 drm_dbg_state(state->dev,
11521 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n",
11522 acrtc->crtc_id,
11523 new_crtc_state->enable,
11524 new_crtc_state->active,
11525 new_crtc_state->planes_changed,
11526 new_crtc_state->mode_changed,
11527 new_crtc_state->active_changed,
11528 new_crtc_state->connectors_changed);
11529
11530 /* Remove stream for any changed/disabled CRTC */
11531 if (!enable) {
11532
11533 if (!dm_old_crtc_state->stream)
11534 goto skip_modeset;
11535
11536 /* Unset freesync video if it was active before */
11537 if (dm_old_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED) {
11538 dm_new_crtc_state->freesync_config.state = VRR_STATE_INACTIVE;
11539 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = 0;
11540 }
11541
11542 /* Now check if we should set freesync video mode */
11543 if (amdgpu_freesync_vid_mode && dm_new_crtc_state->stream &&
11544 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
11545 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream) &&
11546 is_timing_unchanged_for_freesync(new_crtc_state,
11547 old_crtc_state)) {
11548 new_crtc_state->mode_changed = false;
11549 drm_dbg_driver(adev_to_drm(adev),
11550 "Mode change not required for front porch change, setting mode_changed to %d",
11551 new_crtc_state->mode_changed);
11552
11553 set_freesync_fixed_config(dm_new_crtc_state);
11554
11555 goto skip_modeset;
11556 } else if (amdgpu_freesync_vid_mode && aconnector &&
11557 is_freesync_video_mode(&new_crtc_state->mode,
11558 aconnector)) {
11559 struct drm_display_mode *high_mode;
11560
11561 high_mode = get_highest_refresh_rate_mode(aconnector, false);
11562 if (!drm_mode_equal(&new_crtc_state->mode, high_mode))
11563 set_freesync_fixed_config(dm_new_crtc_state);
11564 }
11565
11566 ret = dm_atomic_get_state(state, &dm_state);
11567 if (ret)
11568 goto fail;
11569
11570 drm_dbg_driver(adev_to_drm(adev), "Disabling DRM crtc: %d\n",
11571 crtc->base.id);
11572
11573 /* i.e. reset mode */
11574 if (dc_state_remove_stream(
11575 dm->dc,
11576 dm_state->context,
11577 dm_old_crtc_state->stream) != DC_OK) {
11578 ret = -EINVAL;
11579 goto fail;
11580 }
11581
11582 dc_stream_release(dm_old_crtc_state->stream);
11583 dm_new_crtc_state->stream = NULL;
11584
11585 reset_freesync_config_for_crtc(dm_new_crtc_state);
11586
11587 *lock_and_validation_needed = true;
11588
11589 } else {/* Add stream for any updated/enabled CRTC */
11590 /*
11591 * Quick fix to prevent NULL pointer on new_stream when
11592 * added MST connectors not found in existing crtc_state in the chained mode
11593 * TODO: need to dig out the root cause of that
11594 */
11595 if (!connector)
11596 goto skip_modeset;
11597
11598 if (modereset_required(new_crtc_state))
11599 goto skip_modeset;
11600
11601 if (amdgpu_dm_crtc_modeset_required(new_crtc_state, new_stream,
11602 dm_old_crtc_state->stream)) {
11603
11604 WARN_ON(dm_new_crtc_state->stream);
11605
11606 ret = dm_atomic_get_state(state, &dm_state);
11607 if (ret)
11608 goto fail;
11609
11610 dm_new_crtc_state->stream = new_stream;
11611
11612 dc_stream_retain(new_stream);
11613
11614 drm_dbg_atomic(adev_to_drm(adev), "Enabling DRM crtc: %d\n",
11615 crtc->base.id);
11616
11617 if (dc_state_add_stream(
11618 dm->dc,
11619 dm_state->context,
11620 dm_new_crtc_state->stream) != DC_OK) {
11621 ret = -EINVAL;
11622 goto fail;
11623 }
11624
11625 *lock_and_validation_needed = true;
11626 }
11627 }
11628
11629skip_modeset:
11630 /* Release extra reference */
11631 if (new_stream)
11632 dc_stream_release(new_stream);
11633
11634 /*
11635 * We want to do dc stream updates that do not require a
11636 * full modeset below.
11637 */
11638 if (!(enable && connector && new_crtc_state->active))
11639 return 0;
11640 /*
11641 * Given above conditions, the dc state cannot be NULL because:
11642 * 1. We're in the process of enabling CRTCs (just been added
11643 * to the dc context, or already is on the context)
11644 * 2. Has a valid connector attached, and
11645 * 3. Is currently active and enabled.
11646 * => The dc stream state currently exists.
11647 */
11648 BUG_ON(dm_new_crtc_state->stream == NULL);
11649
11650 /* Scaling or underscan settings */
11651 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state) ||
11652 drm_atomic_crtc_needs_modeset(new_crtc_state))
11653 update_stream_scaling_settings(adev_to_drm(adev),
11654 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
11655
11656 /* ABM settings */
11657 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
11658
11659 /*
11660 * Color management settings. We also update color properties
11661 * when a modeset is needed, to ensure it gets reprogrammed.
11662 */
11663 if (dm_new_crtc_state->base.color_mgmt_changed ||
11664 dm_old_crtc_state->regamma_tf != dm_new_crtc_state->regamma_tf ||
11665 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
11666 ret = amdgpu_dm_check_crtc_color_mgmt(dm_new_crtc_state, true);
11667 if (ret)
11668 goto fail;
11669 }
11670
11671 /* Update Freesync settings. */
11672 get_freesync_config_for_crtc(dm_new_crtc_state,
11673 dm_new_conn_state);
11674
11675 return ret;
11676
11677fail:
11678 if (new_stream)
11679 dc_stream_release(new_stream);
11680 return ret;
11681}
11682
11683static bool should_reset_plane(struct drm_atomic_state *state,
11684 struct drm_plane *plane,
11685 struct drm_plane_state *old_plane_state,
11686 struct drm_plane_state *new_plane_state)
11687{
11688 struct drm_plane *other;
11689 struct drm_plane_state *old_other_state, *new_other_state;
11690 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
11691 struct dm_crtc_state *old_dm_crtc_state, *new_dm_crtc_state;
11692 struct amdgpu_device *adev = drm_to_adev(plane->dev);
11693 struct drm_connector_state *new_con_state;
11694 struct drm_connector *connector;
11695 int i;
11696
11697 /*
11698 * TODO: Remove this hack for all asics once it proves that the
11699 * fast updates works fine on DCN3.2+.
11700 */
11701 if (amdgpu_ip_version(adev, DCE_HWIP, 0) < IP_VERSION(3, 2, 0) &&
11702 state->allow_modeset)
11703 return true;
11704
11705 /* Check for writeback commit */
11706 for_each_new_connector_in_state(state, connector, new_con_state, i) {
11707 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
11708 continue;
11709
11710 if (new_con_state->writeback_job)
11711 return true;
11712 }
11713
11714 if (amdgpu_in_reset(adev) && state->allow_modeset)
11715 return true;
11716
11717 /* Exit early if we know that we're adding or removing the plane. */
11718 if (old_plane_state->crtc != new_plane_state->crtc)
11719 return true;
11720
11721 /* old crtc == new_crtc == NULL, plane not in context. */
11722 if (!new_plane_state->crtc)
11723 return false;
11724
11725 new_crtc_state =
11726 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
11727 old_crtc_state =
11728 drm_atomic_get_old_crtc_state(state, old_plane_state->crtc);
11729
11730 if (!new_crtc_state)
11731 return true;
11732
11733 /*
11734 * A change in cursor mode means a new dc pipe needs to be acquired or
11735 * released from the state
11736 */
11737 old_dm_crtc_state = to_dm_crtc_state(old_crtc_state);
11738 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
11739 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
11740 old_dm_crtc_state != NULL &&
11741 old_dm_crtc_state->cursor_mode != new_dm_crtc_state->cursor_mode) {
11742 return true;
11743 }
11744
11745 /* CRTC Degamma changes currently require us to recreate planes. */
11746 if (new_crtc_state->color_mgmt_changed)
11747 return true;
11748
11749 /*
11750 * On zpos change, planes need to be reordered by removing and re-adding
11751 * them one by one to the dc state, in order of descending zpos.
11752 *
11753 * TODO: We can likely skip bandwidth validation if the only thing that
11754 * changed about the plane was it'z z-ordering.
11755 */
11756 if (old_plane_state->normalized_zpos != new_plane_state->normalized_zpos)
11757 return true;
11758
11759 if (drm_atomic_crtc_needs_modeset(new_crtc_state))
11760 return true;
11761
11762 /*
11763 * If there are any new primary or overlay planes being added or
11764 * removed then the z-order can potentially change. To ensure
11765 * correct z-order and pipe acquisition the current DC architecture
11766 * requires us to remove and recreate all existing planes.
11767 *
11768 * TODO: Come up with a more elegant solution for this.
11769 */
11770 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) {
11771 struct amdgpu_framebuffer *old_afb, *new_afb;
11772 struct dm_plane_state *dm_new_other_state, *dm_old_other_state;
11773
11774 dm_new_other_state = to_dm_plane_state(new_other_state);
11775 dm_old_other_state = to_dm_plane_state(old_other_state);
11776
11777 if (other->type == DRM_PLANE_TYPE_CURSOR)
11778 continue;
11779
11780 if (old_other_state->crtc != new_plane_state->crtc &&
11781 new_other_state->crtc != new_plane_state->crtc)
11782 continue;
11783
11784 if (old_other_state->crtc != new_other_state->crtc)
11785 return true;
11786
11787 /* Src/dst size and scaling updates. */
11788 if (old_other_state->src_w != new_other_state->src_w ||
11789 old_other_state->src_h != new_other_state->src_h ||
11790 old_other_state->crtc_w != new_other_state->crtc_w ||
11791 old_other_state->crtc_h != new_other_state->crtc_h)
11792 return true;
11793
11794 /* Rotation / mirroring updates. */
11795 if (old_other_state->rotation != new_other_state->rotation)
11796 return true;
11797
11798 /* Blending updates. */
11799 if (old_other_state->pixel_blend_mode !=
11800 new_other_state->pixel_blend_mode)
11801 return true;
11802
11803 /* Alpha updates. */
11804 if (old_other_state->alpha != new_other_state->alpha)
11805 return true;
11806
11807 /* Colorspace changes. */
11808 if (old_other_state->color_range != new_other_state->color_range ||
11809 old_other_state->color_encoding != new_other_state->color_encoding)
11810 return true;
11811
11812 /* HDR/Transfer Function changes. */
11813 if (dm_old_other_state->degamma_tf != dm_new_other_state->degamma_tf ||
11814 dm_old_other_state->degamma_lut != dm_new_other_state->degamma_lut ||
11815 dm_old_other_state->hdr_mult != dm_new_other_state->hdr_mult ||
11816 dm_old_other_state->ctm != dm_new_other_state->ctm ||
11817 dm_old_other_state->shaper_lut != dm_new_other_state->shaper_lut ||
11818 dm_old_other_state->shaper_tf != dm_new_other_state->shaper_tf ||
11819 dm_old_other_state->lut3d != dm_new_other_state->lut3d ||
11820 dm_old_other_state->blend_lut != dm_new_other_state->blend_lut ||
11821 dm_old_other_state->blend_tf != dm_new_other_state->blend_tf)
11822 return true;
11823
11824 /* Framebuffer checks fall at the end. */
11825 if (!old_other_state->fb || !new_other_state->fb)
11826 continue;
11827
11828 /* Pixel format changes can require bandwidth updates. */
11829 if (old_other_state->fb->format != new_other_state->fb->format)
11830 return true;
11831
11832 old_afb = (struct amdgpu_framebuffer *)old_other_state->fb;
11833 new_afb = (struct amdgpu_framebuffer *)new_other_state->fb;
11834
11835 /* Tiling and DCC changes also require bandwidth updates. */
11836 if (old_afb->tiling_flags != new_afb->tiling_flags ||
11837 old_afb->base.modifier != new_afb->base.modifier)
11838 return true;
11839 }
11840
11841 return false;
11842}
11843
11844static int dm_check_cursor_fb(struct amdgpu_crtc *new_acrtc,
11845 struct drm_plane_state *new_plane_state,
11846 struct drm_framebuffer *fb)
11847{
11848 struct amdgpu_device *adev = drm_to_adev(new_acrtc->base.dev);
11849 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
11850 unsigned int pitch;
11851 bool linear;
11852
11853 if (fb->width > new_acrtc->max_cursor_width ||
11854 fb->height > new_acrtc->max_cursor_height) {
11855 drm_dbg_atomic(adev_to_drm(adev), "Bad cursor FB size %dx%d\n",
11856 new_plane_state->fb->width,
11857 new_plane_state->fb->height);
11858 return -EINVAL;
11859 }
11860 if (new_plane_state->src_w != fb->width << 16 ||
11861 new_plane_state->src_h != fb->height << 16) {
11862 drm_dbg_atomic(adev_to_drm(adev), "Cropping not supported for cursor plane\n");
11863 return -EINVAL;
11864 }
11865
11866 /* Pitch in pixels */
11867 pitch = fb->pitches[0] / fb->format->cpp[0];
11868
11869 if (fb->width != pitch) {
11870 drm_dbg_atomic(adev_to_drm(adev), "Cursor FB width %d doesn't match pitch %d",
11871 fb->width, pitch);
11872 return -EINVAL;
11873 }
11874
11875 switch (pitch) {
11876 case 64:
11877 case 128:
11878 case 256:
11879 /* FB pitch is supported by cursor plane */
11880 break;
11881 default:
11882 drm_dbg_atomic(adev_to_drm(adev), "Bad cursor FB pitch %d px\n", pitch);
11883 return -EINVAL;
11884 }
11885
11886 /* Core DRM takes care of checking FB modifiers, so we only need to
11887 * check tiling flags when the FB doesn't have a modifier.
11888 */
11889 if (!(fb->flags & DRM_MODE_FB_MODIFIERS)) {
11890 if (adev->family == AMDGPU_FAMILY_GC_12_0_0) {
11891 linear = AMDGPU_TILING_GET(afb->tiling_flags, GFX12_SWIZZLE_MODE) == 0;
11892 } else if (adev->family >= AMDGPU_FAMILY_AI) {
11893 linear = AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE) == 0;
11894 } else {
11895 linear = AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_2D_TILED_THIN1 &&
11896 AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_1D_TILED_THIN1 &&
11897 AMDGPU_TILING_GET(afb->tiling_flags, MICRO_TILE_MODE) == 0;
11898 }
11899 if (!linear) {
11900 drm_dbg_atomic(adev_to_drm(adev), "Cursor FB not linear");
11901 return -EINVAL;
11902 }
11903 }
11904
11905 return 0;
11906}
11907
11908/*
11909 * Helper function for checking the cursor in native mode
11910 */
11911static int dm_check_native_cursor_state(struct drm_crtc *new_plane_crtc,
11912 struct drm_plane *plane,
11913 struct drm_plane_state *new_plane_state,
11914 bool enable)
11915{
11916
11917 struct amdgpu_crtc *new_acrtc;
11918 int ret;
11919
11920 if (!enable || !new_plane_crtc ||
11921 drm_atomic_plane_disabling(plane->state, new_plane_state))
11922 return 0;
11923
11924 new_acrtc = to_amdgpu_crtc(new_plane_crtc);
11925
11926 if (new_plane_state->src_x != 0 || new_plane_state->src_y != 0) {
11927 drm_dbg_atomic(new_plane_crtc->dev, "Cropping not supported for cursor plane\n");
11928 return -EINVAL;
11929 }
11930
11931 if (new_plane_state->fb) {
11932 ret = dm_check_cursor_fb(new_acrtc, new_plane_state,
11933 new_plane_state->fb);
11934 if (ret)
11935 return ret;
11936 }
11937
11938 return 0;
11939}
11940
11941static bool dm_should_update_native_cursor(struct drm_atomic_state *state,
11942 struct drm_crtc *old_plane_crtc,
11943 struct drm_crtc *new_plane_crtc,
11944 bool enable)
11945{
11946 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
11947 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
11948
11949 if (!enable) {
11950 if (old_plane_crtc == NULL)
11951 return true;
11952
11953 old_crtc_state = drm_atomic_get_old_crtc_state(
11954 state, old_plane_crtc);
11955 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
11956
11957 return dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE;
11958 } else {
11959 if (new_plane_crtc == NULL)
11960 return true;
11961
11962 new_crtc_state = drm_atomic_get_new_crtc_state(
11963 state, new_plane_crtc);
11964 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11965
11966 return dm_new_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE;
11967 }
11968}
11969
11970static int dm_update_plane_state(struct dc *dc,
11971 struct drm_atomic_state *state,
11972 struct drm_plane *plane,
11973 struct drm_plane_state *old_plane_state,
11974 struct drm_plane_state *new_plane_state,
11975 bool enable,
11976 bool *lock_and_validation_needed,
11977 bool *is_top_most_overlay)
11978{
11979
11980 struct dm_atomic_state *dm_state = NULL;
11981 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
11982 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
11983 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
11984 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
11985 bool needs_reset, update_native_cursor;
11986 int ret = 0;
11987
11988
11989 new_plane_crtc = new_plane_state->crtc;
11990 old_plane_crtc = old_plane_state->crtc;
11991 dm_new_plane_state = to_dm_plane_state(new_plane_state);
11992 dm_old_plane_state = to_dm_plane_state(old_plane_state);
11993
11994 update_native_cursor = dm_should_update_native_cursor(state,
11995 old_plane_crtc,
11996 new_plane_crtc,
11997 enable);
11998
11999 if (plane->type == DRM_PLANE_TYPE_CURSOR && update_native_cursor) {
12000 ret = dm_check_native_cursor_state(new_plane_crtc, plane,
12001 new_plane_state, enable);
12002 if (ret)
12003 return ret;
12004
12005 return 0;
12006 }
12007
12008 needs_reset = should_reset_plane(state, plane, old_plane_state,
12009 new_plane_state);
12010
12011 /* Remove any changed/removed planes */
12012 if (!enable) {
12013 if (!needs_reset)
12014 return 0;
12015
12016 if (!old_plane_crtc)
12017 return 0;
12018
12019 old_crtc_state = drm_atomic_get_old_crtc_state(
12020 state, old_plane_crtc);
12021 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
12022
12023 if (!dm_old_crtc_state->stream)
12024 return 0;
12025
12026 drm_dbg_atomic(old_plane_crtc->dev, "Disabling DRM plane: %d on DRM crtc %d\n",
12027 plane->base.id, old_plane_crtc->base.id);
12028
12029 ret = dm_atomic_get_state(state, &dm_state);
12030 if (ret)
12031 return ret;
12032
12033 if (!dc_state_remove_plane(
12034 dc,
12035 dm_old_crtc_state->stream,
12036 dm_old_plane_state->dc_state,
12037 dm_state->context)) {
12038
12039 return -EINVAL;
12040 }
12041
12042 if (dm_old_plane_state->dc_state)
12043 dc_plane_state_release(dm_old_plane_state->dc_state);
12044
12045 dm_new_plane_state->dc_state = NULL;
12046
12047 *lock_and_validation_needed = true;
12048
12049 } else { /* Add new planes */
12050 struct dc_plane_state *dc_new_plane_state;
12051
12052 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
12053 return 0;
12054
12055 if (!new_plane_crtc)
12056 return 0;
12057
12058 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
12059 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12060
12061 if (!dm_new_crtc_state->stream)
12062 return 0;
12063
12064 if (!needs_reset)
12065 return 0;
12066
12067 ret = amdgpu_dm_plane_helper_check_state(new_plane_state, new_crtc_state);
12068 if (ret)
12069 goto out;
12070
12071 WARN_ON(dm_new_plane_state->dc_state);
12072
12073 dc_new_plane_state = dc_create_plane_state(dc);
12074 if (!dc_new_plane_state) {
12075 ret = -ENOMEM;
12076 goto out;
12077 }
12078
12079 drm_dbg_atomic(new_plane_crtc->dev, "Enabling DRM plane: %d on DRM crtc %d\n",
12080 plane->base.id, new_plane_crtc->base.id);
12081
12082 ret = fill_dc_plane_attributes(
12083 drm_to_adev(new_plane_crtc->dev),
12084 dc_new_plane_state,
12085 new_plane_state,
12086 new_crtc_state);
12087 if (ret) {
12088 dc_plane_state_release(dc_new_plane_state);
12089 goto out;
12090 }
12091
12092 ret = dm_atomic_get_state(state, &dm_state);
12093 if (ret) {
12094 dc_plane_state_release(dc_new_plane_state);
12095 goto out;
12096 }
12097
12098 /*
12099 * Any atomic check errors that occur after this will
12100 * not need a release. The plane state will be attached
12101 * to the stream, and therefore part of the atomic
12102 * state. It'll be released when the atomic state is
12103 * cleaned.
12104 */
12105 if (!dc_state_add_plane(
12106 dc,
12107 dm_new_crtc_state->stream,
12108 dc_new_plane_state,
12109 dm_state->context)) {
12110
12111 dc_plane_state_release(dc_new_plane_state);
12112 ret = -EINVAL;
12113 goto out;
12114 }
12115
12116 dm_new_plane_state->dc_state = dc_new_plane_state;
12117
12118 dm_new_crtc_state->mpo_requested |= (plane->type == DRM_PLANE_TYPE_OVERLAY);
12119
12120 /* Tell DC to do a full surface update every time there
12121 * is a plane change. Inefficient, but works for now.
12122 */
12123 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
12124
12125 *lock_and_validation_needed = true;
12126 }
12127
12128out:
12129 /* If enabling cursor overlay failed, attempt fallback to native mode */
12130 if (enable && ret == -EINVAL && plane->type == DRM_PLANE_TYPE_CURSOR) {
12131 ret = dm_check_native_cursor_state(new_plane_crtc, plane,
12132 new_plane_state, enable);
12133 if (ret)
12134 return ret;
12135
12136 dm_new_crtc_state->cursor_mode = DM_CURSOR_NATIVE_MODE;
12137 }
12138
12139 return ret;
12140}
12141
12142static void dm_get_oriented_plane_size(struct drm_plane_state *plane_state,
12143 int *src_w, int *src_h)
12144{
12145 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
12146 case DRM_MODE_ROTATE_90:
12147 case DRM_MODE_ROTATE_270:
12148 *src_w = plane_state->src_h >> 16;
12149 *src_h = plane_state->src_w >> 16;
12150 break;
12151 case DRM_MODE_ROTATE_0:
12152 case DRM_MODE_ROTATE_180:
12153 default:
12154 *src_w = plane_state->src_w >> 16;
12155 *src_h = plane_state->src_h >> 16;
12156 break;
12157 }
12158}
12159
12160static void
12161dm_get_plane_scale(struct drm_plane_state *plane_state,
12162 int *out_plane_scale_w, int *out_plane_scale_h)
12163{
12164 int plane_src_w, plane_src_h;
12165
12166 dm_get_oriented_plane_size(plane_state, &plane_src_w, &plane_src_h);
12167 *out_plane_scale_w = plane_src_w ? plane_state->crtc_w * 1000 / plane_src_w : 0;
12168 *out_plane_scale_h = plane_src_h ? plane_state->crtc_h * 1000 / plane_src_h : 0;
12169}
12170
12171/*
12172 * The normalized_zpos value cannot be used by this iterator directly. It's only
12173 * calculated for enabled planes, potentially causing normalized_zpos collisions
12174 * between enabled/disabled planes in the atomic state. We need a unique value
12175 * so that the iterator will not generate the same object twice, or loop
12176 * indefinitely.
12177 */
12178static inline struct __drm_planes_state *__get_next_zpos(
12179 struct drm_atomic_state *state,
12180 struct __drm_planes_state *prev)
12181{
12182 unsigned int highest_zpos = 0, prev_zpos = 256;
12183 uint32_t highest_id = 0, prev_id = UINT_MAX;
12184 struct drm_plane_state *new_plane_state;
12185 struct drm_plane *plane;
12186 int i, highest_i = -1;
12187
12188 if (prev != NULL) {
12189 prev_zpos = prev->new_state->zpos;
12190 prev_id = prev->ptr->base.id;
12191 }
12192
12193 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
12194 /* Skip planes with higher zpos than the previously returned */
12195 if (new_plane_state->zpos > prev_zpos ||
12196 (new_plane_state->zpos == prev_zpos &&
12197 plane->base.id >= prev_id))
12198 continue;
12199
12200 /* Save the index of the plane with highest zpos */
12201 if (new_plane_state->zpos > highest_zpos ||
12202 (new_plane_state->zpos == highest_zpos &&
12203 plane->base.id > highest_id)) {
12204 highest_zpos = new_plane_state->zpos;
12205 highest_id = plane->base.id;
12206 highest_i = i;
12207 }
12208 }
12209
12210 if (highest_i < 0)
12211 return NULL;
12212
12213 return &state->planes[highest_i];
12214}
12215
12216/*
12217 * Use the uniqueness of the plane's (zpos, drm obj ID) combination to iterate
12218 * by descending zpos, as read from the new plane state. This is the same
12219 * ordering as defined by drm_atomic_normalize_zpos().
12220 */
12221#define for_each_oldnew_plane_in_descending_zpos(__state, plane, old_plane_state, new_plane_state) \
12222 for (struct __drm_planes_state *__i = __get_next_zpos((__state), NULL); \
12223 __i != NULL; __i = __get_next_zpos((__state), __i)) \
12224 for_each_if(((plane) = __i->ptr, \
12225 (void)(plane) /* Only to avoid unused-but-set-variable warning */, \
12226 (old_plane_state) = __i->old_state, \
12227 (new_plane_state) = __i->new_state, 1))
12228
12229static int add_affected_mst_dsc_crtcs(struct drm_atomic_state *state, struct drm_crtc *crtc)
12230{
12231 struct drm_connector *connector;
12232 struct drm_connector_state *conn_state, *old_conn_state;
12233 struct amdgpu_dm_connector *aconnector = NULL;
12234 int i;
12235
12236 for_each_oldnew_connector_in_state(state, connector, old_conn_state, conn_state, i) {
12237 if (!conn_state->crtc)
12238 conn_state = old_conn_state;
12239
12240 if (conn_state->crtc != crtc)
12241 continue;
12242
12243 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
12244 continue;
12245
12246 aconnector = to_amdgpu_dm_connector(connector);
12247 if (!aconnector->mst_output_port || !aconnector->mst_root)
12248 aconnector = NULL;
12249 else
12250 break;
12251 }
12252
12253 if (!aconnector)
12254 return 0;
12255
12256 return drm_dp_mst_add_affected_dsc_crtcs(state, &aconnector->mst_root->mst_mgr);
12257}
12258
12259/**
12260 * DOC: Cursor Modes - Native vs Overlay
12261 *
12262 * In native mode, the cursor uses a integrated cursor pipe within each DCN hw
12263 * plane. It does not require a dedicated hw plane to enable, but it is
12264 * subjected to the same z-order and scaling as the hw plane. It also has format
12265 * restrictions, a RGB cursor in native mode cannot be enabled within a non-RGB
12266 * hw plane.
12267 *
12268 * In overlay mode, the cursor uses a separate DCN hw plane, and thus has its
12269 * own scaling and z-pos. It also has no blending restrictions. It lends to a
12270 * cursor behavior more akin to a DRM client's expectations. However, it does
12271 * occupy an extra DCN plane, and therefore will only be used if a DCN plane is
12272 * available.
12273 */
12274
12275/**
12276 * dm_crtc_get_cursor_mode() - Determine the required cursor mode on crtc
12277 * @adev: amdgpu device
12278 * @state: DRM atomic state
12279 * @dm_crtc_state: amdgpu state for the CRTC containing the cursor
12280 * @cursor_mode: Returns the required cursor mode on dm_crtc_state
12281 *
12282 * Get whether the cursor should be enabled in native mode, or overlay mode, on
12283 * the dm_crtc_state.
12284 *
12285 * The cursor should be enabled in overlay mode if there exists an underlying
12286 * plane - on which the cursor may be blended - that is either YUV formatted, or
12287 * scaled differently from the cursor.
12288 *
12289 * Since zpos info is required, drm_atomic_normalize_zpos must be called before
12290 * calling this function.
12291 *
12292 * Return: 0 on success, or an error code if getting the cursor plane state
12293 * failed.
12294 */
12295static int dm_crtc_get_cursor_mode(struct amdgpu_device *adev,
12296 struct drm_atomic_state *state,
12297 struct dm_crtc_state *dm_crtc_state,
12298 enum amdgpu_dm_cursor_mode *cursor_mode)
12299{
12300 struct drm_plane_state *old_plane_state, *plane_state, *cursor_state;
12301 struct drm_crtc_state *crtc_state = &dm_crtc_state->base;
12302 struct drm_plane *plane;
12303 bool consider_mode_change = false;
12304 bool entire_crtc_covered = false;
12305 bool cursor_changed = false;
12306 int underlying_scale_w, underlying_scale_h;
12307 int cursor_scale_w, cursor_scale_h;
12308 int i;
12309
12310 /* Overlay cursor not supported on HW before DCN
12311 * DCN401/420 does not have the cursor-on-scaled-plane or cursor-on-yuv-plane restrictions
12312 * as previous DCN generations, so enable native mode on DCN401/420
12313 */
12314 if (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 0, 1) ||
12315 amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 2, 0)) {
12316 *cursor_mode = DM_CURSOR_NATIVE_MODE;
12317 return 0;
12318 }
12319
12320 /* Init cursor_mode to be the same as current */
12321 *cursor_mode = dm_crtc_state->cursor_mode;
12322
12323 /*
12324 * Cursor mode can change if a plane's format changes, scale changes, is
12325 * enabled/disabled, or z-order changes.
12326 */
12327 for_each_oldnew_plane_in_state(state, plane, old_plane_state, plane_state, i) {
12328 int new_scale_w, new_scale_h, old_scale_w, old_scale_h;
12329
12330 /* Only care about planes on this CRTC */
12331 if ((drm_plane_mask(plane) & crtc_state->plane_mask) == 0)
12332 continue;
12333
12334 if (plane->type == DRM_PLANE_TYPE_CURSOR)
12335 cursor_changed = true;
12336
12337 if (drm_atomic_plane_enabling(old_plane_state, plane_state) ||
12338 drm_atomic_plane_disabling(old_plane_state, plane_state) ||
12339 old_plane_state->fb->format != plane_state->fb->format) {
12340 consider_mode_change = true;
12341 break;
12342 }
12343
12344 dm_get_plane_scale(plane_state, &new_scale_w, &new_scale_h);
12345 dm_get_plane_scale(old_plane_state, &old_scale_w, &old_scale_h);
12346 if (new_scale_w != old_scale_w || new_scale_h != old_scale_h) {
12347 consider_mode_change = true;
12348 break;
12349 }
12350 }
12351
12352 if (!consider_mode_change && !crtc_state->zpos_changed)
12353 return 0;
12354
12355 /*
12356 * If no cursor change on this CRTC, and not enabled on this CRTC, then
12357 * no need to set cursor mode. This avoids needlessly locking the cursor
12358 * state.
12359 */
12360 if (!cursor_changed &&
12361 !(drm_plane_mask(crtc_state->crtc->cursor) & crtc_state->plane_mask)) {
12362 return 0;
12363 }
12364
12365 cursor_state = drm_atomic_get_plane_state(state,
12366 crtc_state->crtc->cursor);
12367 if (IS_ERR(cursor_state))
12368 return PTR_ERR(cursor_state);
12369
12370 /* Cursor is disabled */
12371 if (!cursor_state->fb)
12372 return 0;
12373
12374 /* For all planes in descending z-order (all of which are below cursor
12375 * as per zpos definitions), check their scaling and format
12376 */
12377 for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, plane_state) {
12378
12379 /* Only care about non-cursor planes on this CRTC */
12380 if ((drm_plane_mask(plane) & crtc_state->plane_mask) == 0 ||
12381 plane->type == DRM_PLANE_TYPE_CURSOR)
12382 continue;
12383
12384 /* Underlying plane is YUV format - use overlay cursor */
12385 if (amdgpu_dm_plane_is_video_format(plane_state->fb->format->format)) {
12386 *cursor_mode = DM_CURSOR_OVERLAY_MODE;
12387 return 0;
12388 }
12389
12390 dm_get_plane_scale(plane_state,
12391 &underlying_scale_w, &underlying_scale_h);
12392 dm_get_plane_scale(cursor_state,
12393 &cursor_scale_w, &cursor_scale_h);
12394
12395 /* Underlying plane has different scale - use overlay cursor */
12396 if (cursor_scale_w != underlying_scale_w &&
12397 cursor_scale_h != underlying_scale_h) {
12398 *cursor_mode = DM_CURSOR_OVERLAY_MODE;
12399 return 0;
12400 }
12401
12402 /* If this plane covers the whole CRTC, no need to check planes underneath */
12403 if (plane_state->crtc_x <= 0 && plane_state->crtc_y <= 0 &&
12404 plane_state->crtc_x + plane_state->crtc_w >= crtc_state->mode.hdisplay &&
12405 plane_state->crtc_y + plane_state->crtc_h >= crtc_state->mode.vdisplay) {
12406 entire_crtc_covered = true;
12407 break;
12408 }
12409 }
12410
12411 /* If planes do not cover the entire CRTC, use overlay mode to enable
12412 * cursor over holes
12413 */
12414 if (entire_crtc_covered)
12415 *cursor_mode = DM_CURSOR_NATIVE_MODE;
12416 else
12417 *cursor_mode = DM_CURSOR_OVERLAY_MODE;
12418
12419 return 0;
12420}
12421
12422static bool amdgpu_dm_crtc_mem_type_changed(struct drm_device *dev,
12423 struct drm_atomic_state *state,
12424 struct drm_crtc_state *crtc_state)
12425{
12426 struct drm_plane *plane;
12427 struct drm_plane_state *new_plane_state, *old_plane_state;
12428
12429 drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
12430 new_plane_state = drm_atomic_get_plane_state(state, plane);
12431 old_plane_state = drm_atomic_get_plane_state(state, plane);
12432
12433 if (IS_ERR(new_plane_state) || IS_ERR(old_plane_state)) {
12434 drm_err(dev, "Failed to get plane state for plane %s\n", plane->name);
12435 return false;
12436 }
12437
12438 if (old_plane_state->fb && new_plane_state->fb &&
12439 get_mem_type(old_plane_state->fb) != get_mem_type(new_plane_state->fb))
12440 return true;
12441 }
12442
12443 return false;
12444}
12445
12446/**
12447 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM.
12448 *
12449 * @dev: The DRM device
12450 * @state: The atomic state to commit
12451 *
12452 * Validate that the given atomic state is programmable by DC into hardware.
12453 * This involves constructing a &struct dc_state reflecting the new hardware
12454 * state we wish to commit, then querying DC to see if it is programmable. It's
12455 * important not to modify the existing DC state. Otherwise, atomic_check
12456 * may unexpectedly commit hardware changes.
12457 *
12458 * When validating the DC state, it's important that the right locks are
12459 * acquired. For full updates case which removes/adds/updates streams on one
12460 * CRTC while flipping on another CRTC, acquiring global lock will guarantee
12461 * that any such full update commit will wait for completion of any outstanding
12462 * flip using DRMs synchronization events.
12463 *
12464 * Note that DM adds the affected connectors for all CRTCs in state, when that
12465 * might not seem necessary. This is because DC stream creation requires the
12466 * DC sink, which is tied to the DRM connector state. Cleaning this up should
12467 * be possible but non-trivial - a possible TODO item.
12468 *
12469 * Return: -Error code if validation failed.
12470 */
12471static int amdgpu_dm_atomic_check(struct drm_device *dev,
12472 struct drm_atomic_state *state)
12473{
12474 struct amdgpu_device *adev = drm_to_adev(dev);
12475 struct dm_atomic_state *dm_state = NULL;
12476 struct dc *dc = adev->dm.dc;
12477 struct drm_connector *connector;
12478 struct drm_connector_state *old_con_state, *new_con_state;
12479 struct drm_crtc *crtc;
12480 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
12481 struct drm_plane *plane;
12482 struct drm_plane_state *old_plane_state, *new_plane_state, *new_cursor_state;
12483 enum dc_status status;
12484 int ret, i;
12485 bool lock_and_validation_needed = false;
12486 bool is_top_most_overlay = true;
12487 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
12488 struct drm_dp_mst_topology_mgr *mgr;
12489 struct drm_dp_mst_topology_state *mst_state;
12490 struct dsc_mst_fairness_vars vars[MAX_PIPES] = {0};
12491
12492 trace_amdgpu_dm_atomic_check_begin(state);
12493
12494 ret = drm_atomic_helper_check_modeset(dev, state);
12495 if (ret) {
12496 drm_dbg_atomic(dev, "drm_atomic_helper_check_modeset() failed\n");
12497 goto fail;
12498 }
12499
12500 /* Check connector changes */
12501 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
12502 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
12503 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
12504
12505 /* Skip connectors that are disabled or part of modeset already. */
12506 if (!new_con_state->crtc)
12507 continue;
12508
12509 new_crtc_state = drm_atomic_get_crtc_state(state, new_con_state->crtc);
12510 if (IS_ERR(new_crtc_state)) {
12511 drm_dbg_atomic(dev, "drm_atomic_get_crtc_state() failed\n");
12512 ret = PTR_ERR(new_crtc_state);
12513 goto fail;
12514 }
12515
12516 if (dm_old_con_state->abm_level != dm_new_con_state->abm_level ||
12517 dm_old_con_state->scaling != dm_new_con_state->scaling)
12518 new_crtc_state->connectors_changed = true;
12519 }
12520
12521 if (dc_resource_is_dsc_encoding_supported(dc)) {
12522 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12523 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12524 dm_new_crtc_state->mode_changed_independent_from_dsc = new_crtc_state->mode_changed;
12525 }
12526
12527 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12528 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
12529 ret = add_affected_mst_dsc_crtcs(state, crtc);
12530 if (ret) {
12531 drm_dbg_atomic(dev, "add_affected_mst_dsc_crtcs() failed\n");
12532 goto fail;
12533 }
12534 }
12535 }
12536 }
12537 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12538 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
12539
12540 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
12541 !new_crtc_state->color_mgmt_changed &&
12542 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled &&
12543 dm_old_crtc_state->dsc_force_changed == false)
12544 continue;
12545
12546 ret = amdgpu_dm_verify_lut_sizes(new_crtc_state);
12547 if (ret) {
12548 drm_dbg_atomic(dev, "amdgpu_dm_verify_lut_sizes() failed\n");
12549 goto fail;
12550 }
12551
12552 if (!new_crtc_state->enable)
12553 continue;
12554
12555 ret = drm_atomic_add_affected_connectors(state, crtc);
12556 if (ret) {
12557 drm_dbg_atomic(dev, "drm_atomic_add_affected_connectors() failed\n");
12558 goto fail;
12559 }
12560
12561 ret = drm_atomic_add_affected_planes(state, crtc);
12562 if (ret) {
12563 drm_dbg_atomic(dev, "drm_atomic_add_affected_planes() failed\n");
12564 goto fail;
12565 }
12566
12567 if (dm_old_crtc_state->dsc_force_changed)
12568 new_crtc_state->mode_changed = true;
12569 }
12570
12571 /*
12572 * Add all primary and overlay planes on the CRTC to the state
12573 * whenever a plane is enabled to maintain correct z-ordering
12574 * and to enable fast surface updates.
12575 */
12576 drm_for_each_crtc(crtc, dev) {
12577 bool modified = false;
12578
12579 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
12580 if (plane->type == DRM_PLANE_TYPE_CURSOR)
12581 continue;
12582
12583 if (new_plane_state->crtc == crtc ||
12584 old_plane_state->crtc == crtc) {
12585 modified = true;
12586 break;
12587 }
12588 }
12589
12590 if (!modified)
12591 continue;
12592
12593 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
12594 if (plane->type == DRM_PLANE_TYPE_CURSOR)
12595 continue;
12596
12597 new_plane_state =
12598 drm_atomic_get_plane_state(state, plane);
12599
12600 if (IS_ERR(new_plane_state)) {
12601 ret = PTR_ERR(new_plane_state);
12602 drm_dbg_atomic(dev, "new_plane_state is BAD\n");
12603 goto fail;
12604 }
12605 }
12606 }
12607
12608 /*
12609 * DC consults the zpos (layer_index in DC terminology) to determine the
12610 * hw plane on which to enable the hw cursor (see
12611 * `dcn10_can_pipe_disable_cursor`). By now, all modified planes are in
12612 * atomic state, so call drm helper to normalize zpos.
12613 */
12614 ret = drm_atomic_normalize_zpos(dev, state);
12615 if (ret) {
12616 drm_dbg(dev, "drm_atomic_normalize_zpos() failed\n");
12617 goto fail;
12618 }
12619
12620 /*
12621 * Determine whether cursors on each CRTC should be enabled in native or
12622 * overlay mode.
12623 */
12624 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12625 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12626
12627 ret = dm_crtc_get_cursor_mode(adev, state, dm_new_crtc_state,
12628 &dm_new_crtc_state->cursor_mode);
12629 if (ret) {
12630 drm_dbg(dev, "Failed to determine cursor mode\n");
12631 goto fail;
12632 }
12633
12634 /*
12635 * If overlay cursor is needed, DC cannot go through the
12636 * native cursor update path. All enabled planes on the CRTC
12637 * need to be added for DC to not disable a plane by mistake
12638 */
12639 if (dm_new_crtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE) {
12640 if (amdgpu_ip_version(adev, DCE_HWIP, 0) == 0) {
12641 drm_dbg(dev, "Overlay cursor not supported on DCE\n");
12642 ret = -EINVAL;
12643 goto fail;
12644 }
12645
12646 ret = drm_atomic_add_affected_planes(state, crtc);
12647 if (ret)
12648 goto fail;
12649 }
12650 }
12651
12652 /* Remove exiting planes if they are modified */
12653 for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, new_plane_state) {
12654
12655 ret = dm_update_plane_state(dc, state, plane,
12656 old_plane_state,
12657 new_plane_state,
12658 false,
12659 &lock_and_validation_needed,
12660 &is_top_most_overlay);
12661 if (ret) {
12662 drm_dbg_atomic(dev, "dm_update_plane_state() failed\n");
12663 goto fail;
12664 }
12665 }
12666
12667 /* Disable all crtcs which require disable */
12668 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12669 ret = dm_update_crtc_state(&adev->dm, state, crtc,
12670 old_crtc_state,
12671 new_crtc_state,
12672 false,
12673 &lock_and_validation_needed);
12674 if (ret) {
12675 drm_dbg_atomic(dev, "DISABLE: dm_update_crtc_state() failed\n");
12676 goto fail;
12677 }
12678 }
12679
12680 /* Enable all crtcs which require enable */
12681 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12682 ret = dm_update_crtc_state(&adev->dm, state, crtc,
12683 old_crtc_state,
12684 new_crtc_state,
12685 true,
12686 &lock_and_validation_needed);
12687 if (ret) {
12688 drm_dbg_atomic(dev, "ENABLE: dm_update_crtc_state() failed\n");
12689 goto fail;
12690 }
12691 }
12692
12693 /* Add new/modified planes */
12694 for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, new_plane_state) {
12695 ret = dm_update_plane_state(dc, state, plane,
12696 old_plane_state,
12697 new_plane_state,
12698 true,
12699 &lock_and_validation_needed,
12700 &is_top_most_overlay);
12701 if (ret) {
12702 drm_dbg_atomic(dev, "dm_update_plane_state() failed\n");
12703 goto fail;
12704 }
12705 }
12706
12707#if defined(CONFIG_DRM_AMD_DC_FP)
12708 if (dc_resource_is_dsc_encoding_supported(dc)) {
12709 ret = pre_validate_dsc(state, &dm_state, vars);
12710 if (ret != 0)
12711 goto fail;
12712 }
12713#endif
12714
12715 /* Run this here since we want to validate the streams we created */
12716 ret = drm_atomic_helper_check_planes(dev, state);
12717 if (ret) {
12718 drm_dbg_atomic(dev, "drm_atomic_helper_check_planes() failed\n");
12719 goto fail;
12720 }
12721
12722 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12723 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12724 if (dm_new_crtc_state->mpo_requested)
12725 drm_dbg_atomic(dev, "MPO enablement requested on crtc:[%p]\n", crtc);
12726 }
12727
12728 /* Check cursor restrictions */
12729 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12730 enum amdgpu_dm_cursor_mode required_cursor_mode;
12731 int is_rotated, is_scaled;
12732
12733 /* Overlay cusor not subject to native cursor restrictions */
12734 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12735 if (dm_new_crtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE)
12736 continue;
12737
12738 /* Check if rotation or scaling is enabled on DCN401 */
12739 if ((drm_plane_mask(crtc->cursor) & new_crtc_state->plane_mask) &&
12740 (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 2, 0) ||
12741 amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 0, 1))) {
12742 new_cursor_state = drm_atomic_get_new_plane_state(state, crtc->cursor);
12743
12744 is_rotated = new_cursor_state &&
12745 ((new_cursor_state->rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0);
12746 is_scaled = new_cursor_state && ((new_cursor_state->src_w >> 16 != new_cursor_state->crtc_w) ||
12747 (new_cursor_state->src_h >> 16 != new_cursor_state->crtc_h));
12748
12749 if (is_rotated || is_scaled) {
12750 drm_dbg_driver(
12751 crtc->dev,
12752 "[CRTC:%d:%s] cannot enable hardware cursor due to rotation/scaling\n",
12753 crtc->base.id, crtc->name);
12754 ret = -EINVAL;
12755 goto fail;
12756 }
12757 }
12758
12759 /* If HW can only do native cursor, check restrictions again */
12760 ret = dm_crtc_get_cursor_mode(adev, state, dm_new_crtc_state,
12761 &required_cursor_mode);
12762 if (ret) {
12763 drm_dbg_driver(crtc->dev,
12764 "[CRTC:%d:%s] Checking cursor mode failed\n",
12765 crtc->base.id, crtc->name);
12766 goto fail;
12767 } else if (required_cursor_mode == DM_CURSOR_OVERLAY_MODE) {
12768 drm_dbg_driver(crtc->dev,
12769 "[CRTC:%d:%s] Cannot enable native cursor due to scaling or YUV restrictions\n",
12770 crtc->base.id, crtc->name);
12771 ret = -EINVAL;
12772 goto fail;
12773 }
12774 }
12775
12776 if (state->legacy_cursor_update) {
12777 /*
12778 * This is a fast cursor update coming from the plane update
12779 * helper, check if it can be done asynchronously for better
12780 * performance.
12781 */
12782 state->async_update =
12783 !drm_atomic_helper_async_check(dev, state);
12784
12785 /*
12786 * Skip the remaining global validation if this is an async
12787 * update. Cursor updates can be done without affecting
12788 * state or bandwidth calcs and this avoids the performance
12789 * penalty of locking the private state object and
12790 * allocating a new dc_state.
12791 */
12792 if (state->async_update)
12793 return 0;
12794 }
12795
12796 /* Check scaling and underscan changes*/
12797 /* TODO Removed scaling changes validation due to inability to commit
12798 * new stream into context w\o causing full reset. Need to
12799 * decide how to handle.
12800 */
12801 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
12802 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
12803 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
12804 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
12805
12806 /* Skip any modesets/resets */
12807 if (!acrtc || drm_atomic_crtc_needs_modeset(
12808 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
12809 continue;
12810
12811 /* Skip any thing not scale or underscan changes */
12812 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
12813 continue;
12814
12815 lock_and_validation_needed = true;
12816 }
12817
12818 /* set the slot info for each mst_state based on the link encoding format */
12819 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
12820 struct amdgpu_dm_connector *aconnector;
12821 struct drm_connector *connector;
12822 struct drm_connector_list_iter iter;
12823 u8 link_coding_cap;
12824
12825 drm_connector_list_iter_begin(dev, &iter);
12826 drm_for_each_connector_iter(connector, &iter) {
12827 if (connector->index == mst_state->mgr->conn_base_id) {
12828 aconnector = to_amdgpu_dm_connector(connector);
12829 link_coding_cap = dc_link_dp_mst_decide_link_encoding_format(aconnector->dc_link);
12830 drm_dp_mst_update_slots(mst_state, link_coding_cap);
12831
12832 break;
12833 }
12834 }
12835 drm_connector_list_iter_end(&iter);
12836 }
12837
12838 /**
12839 * Streams and planes are reset when there are changes that affect
12840 * bandwidth. Anything that affects bandwidth needs to go through
12841 * DC global validation to ensure that the configuration can be applied
12842 * to hardware.
12843 *
12844 * We have to currently stall out here in atomic_check for outstanding
12845 * commits to finish in this case because our IRQ handlers reference
12846 * DRM state directly - we can end up disabling interrupts too early
12847 * if we don't.
12848 *
12849 * TODO: Remove this stall and drop DM state private objects.
12850 */
12851 if (lock_and_validation_needed) {
12852 ret = dm_atomic_get_state(state, &dm_state);
12853 if (ret) {
12854 drm_dbg_atomic(dev, "dm_atomic_get_state() failed\n");
12855 goto fail;
12856 }
12857
12858 ret = do_aquire_global_lock(dev, state);
12859 if (ret) {
12860 drm_dbg_atomic(dev, "do_aquire_global_lock() failed\n");
12861 goto fail;
12862 }
12863
12864#if defined(CONFIG_DRM_AMD_DC_FP)
12865 if (dc_resource_is_dsc_encoding_supported(dc)) {
12866 ret = compute_mst_dsc_configs_for_state(state, dm_state->context, vars);
12867 if (ret) {
12868 drm_dbg_atomic(dev, "MST_DSC compute_mst_dsc_configs_for_state() failed\n");
12869 ret = -EINVAL;
12870 goto fail;
12871 }
12872 }
12873#endif
12874
12875 ret = dm_update_mst_vcpi_slots_for_dsc(state, dm_state->context, vars);
12876 if (ret) {
12877 drm_dbg_atomic(dev, "dm_update_mst_vcpi_slots_for_dsc() failed\n");
12878 goto fail;
12879 }
12880
12881 /*
12882 * Perform validation of MST topology in the state:
12883 * We need to perform MST atomic check before calling
12884 * dc_validate_global_state(), or there is a chance
12885 * to get stuck in an infinite loop and hang eventually.
12886 */
12887 ret = drm_dp_mst_atomic_check(state);
12888 if (ret) {
12889 drm_dbg_atomic(dev, "MST drm_dp_mst_atomic_check() failed\n");
12890 goto fail;
12891 }
12892 status = dc_validate_global_state(dc, dm_state->context, DC_VALIDATE_MODE_ONLY);
12893 if (status != DC_OK) {
12894 drm_dbg_atomic(dev, "DC global validation failure: %s (%d)",
12895 dc_status_to_str(status), status);
12896 ret = -EINVAL;
12897 goto fail;
12898 }
12899 } else {
12900 /*
12901 * The commit is a fast update. Fast updates shouldn't change
12902 * the DC context, affect global validation, and can have their
12903 * commit work done in parallel with other commits not touching
12904 * the same resource. If we have a new DC context as part of
12905 * the DM atomic state from validation we need to free it and
12906 * retain the existing one instead.
12907 *
12908 * Furthermore, since the DM atomic state only contains the DC
12909 * context and can safely be annulled, we can free the state
12910 * and clear the associated private object now to free
12911 * some memory and avoid a possible use-after-free later.
12912 */
12913
12914 for (i = 0; i < state->num_private_objs; i++) {
12915 struct drm_private_obj *obj = state->private_objs[i].ptr;
12916
12917 if (obj->funcs == adev->dm.atomic_obj.funcs) {
12918 int j = state->num_private_objs-1;
12919
12920 dm_atomic_destroy_state(obj,
12921 state->private_objs[i].state_to_destroy);
12922
12923 /* If i is not at the end of the array then the
12924 * last element needs to be moved to where i was
12925 * before the array can safely be truncated.
12926 */
12927 if (i != j)
12928 state->private_objs[i] =
12929 state->private_objs[j];
12930
12931 state->private_objs[j].ptr = NULL;
12932 state->private_objs[j].state_to_destroy = NULL;
12933 state->private_objs[j].old_state = NULL;
12934 state->private_objs[j].new_state = NULL;
12935
12936 state->num_private_objs = j;
12937 break;
12938 }
12939 }
12940 }
12941
12942 /* Store the overall update type for use later in atomic check. */
12943 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12944 struct dm_crtc_state *dm_new_crtc_state =
12945 to_dm_crtc_state(new_crtc_state);
12946
12947 /*
12948 * Only allow async flips for fast updates that don't change
12949 * the FB pitch, the DCC state, rotation, mem_type, etc.
12950 */
12951 if (new_crtc_state->async_flip &&
12952 (lock_and_validation_needed ||
12953 amdgpu_dm_crtc_mem_type_changed(dev, state, new_crtc_state))) {
12954 drm_dbg_atomic(crtc->dev,
12955 "[CRTC:%d:%s] async flips are only supported for fast updates\n",
12956 crtc->base.id, crtc->name);
12957 ret = -EINVAL;
12958 goto fail;
12959 }
12960
12961 dm_new_crtc_state->update_type = lock_and_validation_needed ?
12962 UPDATE_TYPE_FULL : UPDATE_TYPE_FAST;
12963 }
12964
12965 /* Must be success */
12966 WARN_ON(ret);
12967
12968 trace_amdgpu_dm_atomic_check_finish(state, ret);
12969
12970 return ret;
12971
12972fail:
12973 if (ret == -EDEADLK)
12974 drm_dbg_atomic(dev, "Atomic check stopped to avoid deadlock.\n");
12975 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
12976 drm_dbg_atomic(dev, "Atomic check stopped due to signal.\n");
12977 else
12978 drm_dbg_atomic(dev, "Atomic check failed with err: %d\n", ret);
12979
12980 trace_amdgpu_dm_atomic_check_finish(state, ret);
12981
12982 return ret;
12983}
12984
12985static bool dm_edid_parser_send_cea(struct amdgpu_display_manager *dm,
12986 unsigned int offset,
12987 unsigned int total_length,
12988 u8 *data,
12989 unsigned int length,
12990 struct amdgpu_hdmi_vsdb_info *vsdb)
12991{
12992 bool res;
12993 union dmub_rb_cmd cmd;
12994 struct dmub_cmd_send_edid_cea *input;
12995 struct dmub_cmd_edid_cea_output *output;
12996
12997 if (length > DMUB_EDID_CEA_DATA_CHUNK_BYTES)
12998 return false;
12999
13000 memset(&cmd, 0, sizeof(cmd));
13001
13002 input = &cmd.edid_cea.data.input;
13003
13004 cmd.edid_cea.header.type = DMUB_CMD__EDID_CEA;
13005 cmd.edid_cea.header.sub_type = 0;
13006 cmd.edid_cea.header.payload_bytes =
13007 sizeof(cmd.edid_cea) - sizeof(cmd.edid_cea.header);
13008 input->offset = offset;
13009 input->length = length;
13010 input->cea_total_length = total_length;
13011 memcpy(input->payload, data, length);
13012
13013 res = dc_wake_and_execute_dmub_cmd(dm->dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY);
13014 if (!res) {
13015 drm_err(adev_to_drm(dm->adev), "EDID CEA parser failed\n");
13016 return false;
13017 }
13018
13019 output = &cmd.edid_cea.data.output;
13020
13021 if (output->type == DMUB_CMD__EDID_CEA_ACK) {
13022 if (!output->ack.success) {
13023 drm_err(adev_to_drm(dm->adev), "EDID CEA ack failed at offset %d\n",
13024 output->ack.offset);
13025 }
13026 } else if (output->type == DMUB_CMD__EDID_CEA_AMD_VSDB) {
13027 if (!output->amd_vsdb.vsdb_found)
13028 return false;
13029
13030 vsdb->freesync_supported = output->amd_vsdb.freesync_supported;
13031 vsdb->amd_vsdb_version = output->amd_vsdb.amd_vsdb_version;
13032 vsdb->min_refresh_rate_hz = output->amd_vsdb.min_frame_rate;
13033 vsdb->max_refresh_rate_hz = output->amd_vsdb.max_frame_rate;
13034 } else {
13035 drm_warn(adev_to_drm(dm->adev), "Unknown EDID CEA parser results\n");
13036 return false;
13037 }
13038
13039 return true;
13040}
13041
13042static bool parse_edid_cea_dmcu(struct amdgpu_display_manager *dm,
13043 u8 *edid_ext, int len,
13044 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13045{
13046 int i;
13047
13048 /* send extension block to DMCU for parsing */
13049 for (i = 0; i < len; i += 8) {
13050 bool res;
13051 int offset;
13052
13053 /* send 8 bytes a time */
13054 if (!dc_edid_parser_send_cea(dm->dc, i, len, &edid_ext[i], 8))
13055 return false;
13056
13057 if (i+8 == len) {
13058 /* EDID block sent completed, expect result */
13059 int version, min_rate, max_rate;
13060
13061 res = dc_edid_parser_recv_amd_vsdb(dm->dc, &version, &min_rate, &max_rate);
13062 if (res) {
13063 /* amd vsdb found */
13064 vsdb_info->freesync_supported = 1;
13065 vsdb_info->amd_vsdb_version = version;
13066 vsdb_info->min_refresh_rate_hz = min_rate;
13067 vsdb_info->max_refresh_rate_hz = max_rate;
13068 return true;
13069 }
13070 /* not amd vsdb */
13071 return false;
13072 }
13073
13074 /* check for ack*/
13075 res = dc_edid_parser_recv_cea_ack(dm->dc, &offset);
13076 if (!res)
13077 return false;
13078 }
13079
13080 return false;
13081}
13082
13083static bool parse_edid_cea_dmub(struct amdgpu_display_manager *dm,
13084 u8 *edid_ext, int len,
13085 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13086{
13087 int i;
13088
13089 /* send extension block to DMCU for parsing */
13090 for (i = 0; i < len; i += 8) {
13091 /* send 8 bytes a time */
13092 if (!dm_edid_parser_send_cea(dm, i, len, &edid_ext[i], 8, vsdb_info))
13093 return false;
13094 }
13095
13096 return vsdb_info->freesync_supported;
13097}
13098
13099static bool parse_edid_cea(struct amdgpu_dm_connector *aconnector,
13100 u8 *edid_ext, int len,
13101 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13102{
13103 struct amdgpu_device *adev = drm_to_adev(aconnector->base.dev);
13104 bool ret;
13105
13106 mutex_lock(&adev->dm.dc_lock);
13107 if (adev->dm.dmub_srv)
13108 ret = parse_edid_cea_dmub(&adev->dm, edid_ext, len, vsdb_info);
13109 else
13110 ret = parse_edid_cea_dmcu(&adev->dm, edid_ext, len, vsdb_info);
13111 mutex_unlock(&adev->dm.dc_lock);
13112 return ret;
13113}
13114
13115static void parse_edid_displayid_vrr(struct drm_connector *connector,
13116 const struct edid *edid)
13117{
13118 u8 *edid_ext = NULL;
13119 int i;
13120 int j = 0;
13121 u16 min_vfreq;
13122 u16 max_vfreq;
13123
13124 if (!edid || !edid->extensions)
13125 return;
13126
13127 /* Find DisplayID extension */
13128 for (i = 0; i < edid->extensions; i++) {
13129 edid_ext = (void *)(edid + (i + 1));
13130 if (edid_ext[0] == DISPLAYID_EXT)
13131 break;
13132 }
13133
13134 if (i == edid->extensions)
13135 return;
13136
13137 while (j < EDID_LENGTH) {
13138 /* Get dynamic video timing range from DisplayID if available */
13139 if (EDID_LENGTH - j > 13 && edid_ext[j] == 0x25 &&
13140 (edid_ext[j+1] & 0xFE) == 0 && (edid_ext[j+2] == 9)) {
13141 min_vfreq = edid_ext[j+9];
13142 if (edid_ext[j+1] & 7)
13143 max_vfreq = edid_ext[j+10] + ((edid_ext[j+11] & 3) << 8);
13144 else
13145 max_vfreq = edid_ext[j+10];
13146
13147 if (max_vfreq && min_vfreq) {
13148 connector->display_info.monitor_range.max_vfreq = max_vfreq;
13149 connector->display_info.monitor_range.min_vfreq = min_vfreq;
13150
13151 return;
13152 }
13153 }
13154 j++;
13155 }
13156}
13157
13158static int parse_amd_vsdb(struct amdgpu_dm_connector *aconnector,
13159 const struct edid *edid, struct amdgpu_hdmi_vsdb_info *vsdb_info)
13160{
13161 u8 *edid_ext = NULL;
13162 int i;
13163 int j = 0;
13164 int total_ext_block_len;
13165
13166 if (edid == NULL || edid->extensions == 0)
13167 return -ENODEV;
13168
13169 /* Find DisplayID extension */
13170 for (i = 0; i < edid->extensions; i++) {
13171 edid_ext = (void *)(edid + (i + 1));
13172 if (edid_ext[0] == DISPLAYID_EXT)
13173 break;
13174 }
13175
13176 total_ext_block_len = EDID_LENGTH * edid->extensions;
13177 while (j < total_ext_block_len - sizeof(struct amd_vsdb_block)) {
13178 struct amd_vsdb_block *amd_vsdb = (struct amd_vsdb_block *)&edid_ext[j];
13179 unsigned int ieeeId = (amd_vsdb->ieee_id[2] << 16) | (amd_vsdb->ieee_id[1] << 8) | (amd_vsdb->ieee_id[0]);
13180
13181 if (ieeeId == HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_IEEE_REGISTRATION_ID &&
13182 amd_vsdb->version == HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_VERSION_3) {
13183 u8 panel_type;
13184 vsdb_info->replay_mode = (amd_vsdb->feature_caps & AMD_VSDB_VERSION_3_FEATURECAP_REPLAYMODE) ? true : false;
13185 vsdb_info->amd_vsdb_version = HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_VERSION_3;
13186 drm_dbg_kms(aconnector->base.dev, "Panel supports Replay Mode: %d\n", vsdb_info->replay_mode);
13187 panel_type = (amd_vsdb->color_space_eotf_support & AMD_VDSB_VERSION_3_PANEL_TYPE_MASK) >> AMD_VDSB_VERSION_3_PANEL_TYPE_SHIFT;
13188 switch (panel_type) {
13189 case AMD_VSDB_PANEL_TYPE_OLED:
13190 aconnector->dc_link->panel_type = PANEL_TYPE_OLED;
13191 break;
13192 case AMD_VSDB_PANEL_TYPE_MINILED:
13193 aconnector->dc_link->panel_type = PANEL_TYPE_MINILED;
13194 break;
13195 default:
13196 aconnector->dc_link->panel_type = PANEL_TYPE_NONE;
13197 break;
13198 }
13199 drm_dbg_kms(aconnector->base.dev, "Panel type: %d\n",
13200 aconnector->dc_link->panel_type);
13201
13202 return true;
13203 }
13204 j++;
13205 }
13206
13207 return false;
13208}
13209
13210static int parse_hdmi_amd_vsdb(struct amdgpu_dm_connector *aconnector,
13211 const struct edid *edid,
13212 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13213{
13214 u8 *edid_ext = NULL;
13215 int i;
13216 bool valid_vsdb_found = false;
13217
13218 /*----- drm_find_cea_extension() -----*/
13219 /* No EDID or EDID extensions */
13220 if (edid == NULL || edid->extensions == 0)
13221 return -ENODEV;
13222
13223 /* Find CEA extension */
13224 for (i = 0; i < edid->extensions; i++) {
13225 edid_ext = (uint8_t *)edid + EDID_LENGTH * (i + 1);
13226 if (edid_ext[0] == CEA_EXT)
13227 break;
13228 }
13229
13230 if (i == edid->extensions)
13231 return -ENODEV;
13232
13233 /*----- cea_db_offsets() -----*/
13234 if (edid_ext[0] != CEA_EXT)
13235 return -ENODEV;
13236
13237 valid_vsdb_found = parse_edid_cea(aconnector, edid_ext, EDID_LENGTH, vsdb_info);
13238
13239 return valid_vsdb_found ? i : -ENODEV;
13240}
13241
13242/**
13243 * amdgpu_dm_update_freesync_caps - Update Freesync capabilities
13244 *
13245 * @connector: Connector to query.
13246 * @drm_edid: DRM EDID from monitor
13247 *
13248 * Amdgpu supports Freesync in DP and HDMI displays, and it is required to keep
13249 * track of some of the display information in the internal data struct used by
13250 * amdgpu_dm. This function checks which type of connector we need to set the
13251 * FreeSync parameters.
13252 */
13253void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
13254 const struct drm_edid *drm_edid)
13255{
13256 int i = 0;
13257 struct amdgpu_dm_connector *amdgpu_dm_connector =
13258 to_amdgpu_dm_connector(connector);
13259 struct dm_connector_state *dm_con_state = NULL;
13260 struct dc_sink *sink;
13261 struct amdgpu_device *adev = drm_to_adev(connector->dev);
13262 struct amdgpu_hdmi_vsdb_info vsdb_info = {0};
13263 const struct edid *edid;
13264 bool freesync_capable = false;
13265 enum adaptive_sync_type as_type = ADAPTIVE_SYNC_TYPE_NONE;
13266
13267 if (!connector->state) {
13268 drm_err(adev_to_drm(adev), "%s - Connector has no state", __func__);
13269 goto update;
13270 }
13271
13272 sink = amdgpu_dm_connector->dc_sink ?
13273 amdgpu_dm_connector->dc_sink :
13274 amdgpu_dm_connector->dc_em_sink;
13275
13276 drm_edid_connector_update(connector, drm_edid);
13277
13278 if (!drm_edid || !sink) {
13279 dm_con_state = to_dm_connector_state(connector->state);
13280
13281 amdgpu_dm_connector->min_vfreq = 0;
13282 amdgpu_dm_connector->max_vfreq = 0;
13283 freesync_capable = false;
13284
13285 goto update;
13286 }
13287
13288 dm_con_state = to_dm_connector_state(connector->state);
13289
13290 if (!adev->dm.freesync_module || !dc_supports_vrr(sink->ctx->dce_version))
13291 goto update;
13292
13293 edid = drm_edid_raw(drm_edid); // FIXME: Get rid of drm_edid_raw()
13294
13295 /* Some eDP panels only have the refresh rate range info in DisplayID */
13296 if ((connector->display_info.monitor_range.min_vfreq == 0 ||
13297 connector->display_info.monitor_range.max_vfreq == 0))
13298 parse_edid_displayid_vrr(connector, edid);
13299
13300 if (edid && (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT ||
13301 sink->sink_signal == SIGNAL_TYPE_EDP)) {
13302 if (amdgpu_dm_connector->dc_link &&
13303 amdgpu_dm_connector->dc_link->dpcd_caps.allow_invalid_MSA_timing_param) {
13304 amdgpu_dm_connector->min_vfreq = connector->display_info.monitor_range.min_vfreq;
13305 amdgpu_dm_connector->max_vfreq = connector->display_info.monitor_range.max_vfreq;
13306 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
13307 freesync_capable = true;
13308 }
13309
13310 parse_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
13311
13312 if (vsdb_info.replay_mode) {
13313 amdgpu_dm_connector->vsdb_info.replay_mode = vsdb_info.replay_mode;
13314 amdgpu_dm_connector->vsdb_info.amd_vsdb_version = vsdb_info.amd_vsdb_version;
13315 amdgpu_dm_connector->as_type = ADAPTIVE_SYNC_TYPE_EDP;
13316 }
13317
13318 } else if (drm_edid && sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A) {
13319 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
13320 if (i >= 0 && vsdb_info.freesync_supported) {
13321 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz;
13322 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz;
13323 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
13324 freesync_capable = true;
13325
13326 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz;
13327 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz;
13328 }
13329 }
13330
13331 if (amdgpu_dm_connector->dc_link)
13332 as_type = dm_get_adaptive_sync_support_type(amdgpu_dm_connector->dc_link);
13333
13334 if (as_type == FREESYNC_TYPE_PCON_IN_WHITELIST) {
13335 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
13336 if (i >= 0 && vsdb_info.freesync_supported && vsdb_info.amd_vsdb_version > 0) {
13337
13338 amdgpu_dm_connector->pack_sdp_v1_3 = true;
13339 amdgpu_dm_connector->as_type = as_type;
13340 amdgpu_dm_connector->vsdb_info = vsdb_info;
13341
13342 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz;
13343 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz;
13344 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
13345 freesync_capable = true;
13346
13347 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz;
13348 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz;
13349 }
13350 }
13351
13352update:
13353 if (dm_con_state)
13354 dm_con_state->freesync_capable = freesync_capable;
13355
13356 if (connector->state && amdgpu_dm_connector->dc_link && !freesync_capable &&
13357 amdgpu_dm_connector->dc_link->replay_settings.config.replay_supported) {
13358 amdgpu_dm_connector->dc_link->replay_settings.config.replay_supported = false;
13359 amdgpu_dm_connector->dc_link->replay_settings.replay_feature_enabled = false;
13360 }
13361
13362 if (connector->vrr_capable_property)
13363 drm_connector_set_vrr_capable_property(connector,
13364 freesync_capable);
13365}
13366
13367void amdgpu_dm_trigger_timing_sync(struct drm_device *dev)
13368{
13369 struct amdgpu_device *adev = drm_to_adev(dev);
13370 struct dc *dc = adev->dm.dc;
13371 int i;
13372
13373 mutex_lock(&adev->dm.dc_lock);
13374 if (dc->current_state) {
13375 for (i = 0; i < dc->current_state->stream_count; ++i)
13376 dc->current_state->streams[i]
13377 ->triggered_crtc_reset.enabled =
13378 adev->dm.force_timing_sync;
13379
13380 dm_enable_per_frame_crtc_master_sync(dc->current_state);
13381 dc_trigger_sync(dc, dc->current_state);
13382 }
13383 mutex_unlock(&adev->dm.dc_lock);
13384}
13385
13386static inline void amdgpu_dm_exit_ips_for_hw_access(struct dc *dc)
13387{
13388 if (dc->ctx->dmub_srv && !dc->ctx->dmub_srv->idle_exit_counter)
13389 dc_exit_ips_for_hw_access(dc);
13390}
13391
13392void dm_write_reg_func(const struct dc_context *ctx, uint32_t address,
13393 u32 value, const char *func_name)
13394{
13395#ifdef DM_CHECK_ADDR_0
13396 if (address == 0) {
13397 drm_err(adev_to_drm(ctx->driver_context),
13398 "invalid register write. address = 0");
13399 return;
13400 }
13401#endif
13402
13403 amdgpu_dm_exit_ips_for_hw_access(ctx->dc);
13404 cgs_write_register(ctx->cgs_device, address, value);
13405 trace_amdgpu_dc_wreg(&ctx->perf_trace->write_count, address, value);
13406}
13407
13408uint32_t dm_read_reg_func(const struct dc_context *ctx, uint32_t address,
13409 const char *func_name)
13410{
13411 u32 value;
13412#ifdef DM_CHECK_ADDR_0
13413 if (address == 0) {
13414 drm_err(adev_to_drm(ctx->driver_context),
13415 "invalid register read; address = 0\n");
13416 return 0;
13417 }
13418#endif
13419
13420 if (ctx->dmub_srv &&
13421 ctx->dmub_srv->reg_helper_offload.gather_in_progress &&
13422 !ctx->dmub_srv->reg_helper_offload.should_burst_write) {
13423 ASSERT(false);
13424 return 0;
13425 }
13426
13427 amdgpu_dm_exit_ips_for_hw_access(ctx->dc);
13428
13429 value = cgs_read_register(ctx->cgs_device, address);
13430
13431 trace_amdgpu_dc_rreg(&ctx->perf_trace->read_count, address, value);
13432
13433 return value;
13434}
13435
13436int amdgpu_dm_process_dmub_aux_transfer_sync(
13437 struct dc_context *ctx,
13438 unsigned int link_index,
13439 struct aux_payload *payload,
13440 enum aux_return_code_type *operation_result)
13441{
13442 struct amdgpu_device *adev = ctx->driver_context;
13443 struct dmub_notification *p_notify = adev->dm.dmub_notify;
13444 int ret = -1;
13445
13446 mutex_lock(&adev->dm.dpia_aux_lock);
13447 if (!dc_process_dmub_aux_transfer_async(ctx->dc, link_index, payload)) {
13448 *operation_result = AUX_RET_ERROR_ENGINE_ACQUIRE;
13449 goto out;
13450 }
13451
13452 if (!wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ)) {
13453 drm_err(adev_to_drm(adev), "wait_for_completion_timeout timeout!");
13454 *operation_result = AUX_RET_ERROR_TIMEOUT;
13455 goto out;
13456 }
13457
13458 if (p_notify->result != AUX_RET_SUCCESS) {
13459 /*
13460 * Transient states before tunneling is enabled could
13461 * lead to this error. We can ignore this for now.
13462 */
13463 if (p_notify->result == AUX_RET_ERROR_PROTOCOL_ERROR) {
13464 drm_warn(adev_to_drm(adev), "DPIA AUX failed on 0x%x(%d), error %d\n",
13465 payload->address, payload->length,
13466 p_notify->result);
13467 }
13468 *operation_result = p_notify->result;
13469 goto out;
13470 }
13471
13472 payload->reply[0] = adev->dm.dmub_notify->aux_reply.command & 0xF;
13473 if (adev->dm.dmub_notify->aux_reply.command & 0xF0)
13474 /* The reply is stored in the top nibble of the command. */
13475 payload->reply[0] = (adev->dm.dmub_notify->aux_reply.command >> 4) & 0xF;
13476
13477 /*write req may receive a byte indicating partially written number as well*/
13478 if (p_notify->aux_reply.length)
13479 memcpy(payload->data, p_notify->aux_reply.data,
13480 p_notify->aux_reply.length);
13481
13482 /* success */
13483 ret = p_notify->aux_reply.length;
13484 *operation_result = p_notify->result;
13485out:
13486 reinit_completion(&adev->dm.dmub_aux_transfer_done);
13487 mutex_unlock(&adev->dm.dpia_aux_lock);
13488 return ret;
13489}
13490
13491static void abort_fused_io(
13492 struct dc_context *ctx,
13493 const struct dmub_cmd_fused_request *request
13494)
13495{
13496 union dmub_rb_cmd command = { 0 };
13497 struct dmub_rb_cmd_fused_io *io = &command.fused_io;
13498
13499 io->header.type = DMUB_CMD__FUSED_IO;
13500 io->header.sub_type = DMUB_CMD__FUSED_IO_ABORT;
13501 io->header.payload_bytes = sizeof(*io) - sizeof(io->header);
13502 io->request = *request;
13503 dm_execute_dmub_cmd(ctx, &command, DM_DMUB_WAIT_TYPE_NO_WAIT);
13504}
13505
13506static bool execute_fused_io(
13507 struct amdgpu_device *dev,
13508 struct dc_context *ctx,
13509 union dmub_rb_cmd *commands,
13510 uint8_t count,
13511 uint32_t timeout_us
13512)
13513{
13514 const uint8_t ddc_line = commands[0].fused_io.request.u.aux.ddc_line;
13515
13516 if (ddc_line >= ARRAY_SIZE(dev->dm.fused_io))
13517 return false;
13518
13519 struct fused_io_sync *sync = &dev->dm.fused_io[ddc_line];
13520 struct dmub_rb_cmd_fused_io *first = &commands[0].fused_io;
13521 const bool result = dm_execute_dmub_cmd_list(ctx, count, commands, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY)
13522 && first->header.ret_status
13523 && first->request.status == FUSED_REQUEST_STATUS_SUCCESS;
13524
13525 if (!result)
13526 return false;
13527
13528 while (wait_for_completion_timeout(&sync->replied, usecs_to_jiffies(timeout_us))) {
13529 reinit_completion(&sync->replied);
13530
13531 struct dmub_cmd_fused_request *reply = (struct dmub_cmd_fused_request *) sync->reply_data;
13532
13533 static_assert(sizeof(*reply) <= sizeof(sync->reply_data), "Size mismatch");
13534
13535 if (reply->identifier == first->request.identifier) {
13536 first->request = *reply;
13537 return true;
13538 }
13539 }
13540
13541 reinit_completion(&sync->replied);
13542 first->request.status = FUSED_REQUEST_STATUS_TIMEOUT;
13543 abort_fused_io(ctx, &first->request);
13544 return false;
13545}
13546
13547bool amdgpu_dm_execute_fused_io(
13548 struct amdgpu_device *dev,
13549 struct dc_link *link,
13550 union dmub_rb_cmd *commands,
13551 uint8_t count,
13552 uint32_t timeout_us)
13553{
13554 struct amdgpu_display_manager *dm = &dev->dm;
13555
13556 mutex_lock(&dm->dpia_aux_lock);
13557
13558 const bool result = execute_fused_io(dev, link->ctx, commands, count, timeout_us);
13559
13560 mutex_unlock(&dm->dpia_aux_lock);
13561 return result;
13562}
13563
13564int amdgpu_dm_process_dmub_set_config_sync(
13565 struct dc_context *ctx,
13566 unsigned int link_index,
13567 struct set_config_cmd_payload *payload,
13568 enum set_config_status *operation_result)
13569{
13570 struct amdgpu_device *adev = ctx->driver_context;
13571 bool is_cmd_complete;
13572 int ret;
13573
13574 mutex_lock(&adev->dm.dpia_aux_lock);
13575 is_cmd_complete = dc_process_dmub_set_config_async(ctx->dc,
13576 link_index, payload, adev->dm.dmub_notify);
13577
13578 if (is_cmd_complete || wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ)) {
13579 ret = 0;
13580 *operation_result = adev->dm.dmub_notify->sc_status;
13581 } else {
13582 drm_err(adev_to_drm(adev), "wait_for_completion_timeout timeout!");
13583 ret = -1;
13584 *operation_result = SET_CONFIG_UNKNOWN_ERROR;
13585 }
13586
13587 if (!is_cmd_complete)
13588 reinit_completion(&adev->dm.dmub_aux_transfer_done);
13589 mutex_unlock(&adev->dm.dpia_aux_lock);
13590 return ret;
13591}
13592
13593bool dm_execute_dmub_cmd(const struct dc_context *ctx, union dmub_rb_cmd *cmd, enum dm_dmub_wait_type wait_type)
13594{
13595 return dc_dmub_srv_cmd_run(ctx->dmub_srv, cmd, wait_type);
13596}
13597
13598bool dm_execute_dmub_cmd_list(const struct dc_context *ctx, unsigned int count, union dmub_rb_cmd *cmd, enum dm_dmub_wait_type wait_type)
13599{
13600 return dc_dmub_srv_cmd_run_list(ctx->dmub_srv, count, cmd, wait_type);
13601}
13602
13603void dm_acpi_process_phy_transition_interlock(
13604 const struct dc_context *ctx,
13605 struct dm_process_phy_transition_init_params process_phy_transition_init_params)
13606{
13607 // Not yet implemented
13608}