1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright 2015 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_utils.h>
95 #include <drm/drm_vblank.h>
96 #include <drm/drm_audio_component.h>
97 #include <drm/drm_gem_atomic_helper.h>
98
99 #include <media/cec-notifier.h>
100 #include <acpi/video.h>
101
102 #include "ivsrcid/dcn/irqsrcs_dcn_1_0.h"
103
104 #include "modules/inc/mod_freesync.h"
105 #include "modules/power/power_helpers.h"
106
107 static_assert(AMDGPU_DMUB_NOTIFICATION_MAX == DMUB_NOTIFICATION_MAX, "AMDGPU_DMUB_NOTIFICATION_MAX mismatch");
108
109 #define FIRMWARE_RENOIR_DMUB "amdgpu/renoir_dmcub.bin"
110 MODULE_FIRMWARE(FIRMWARE_RENOIR_DMUB);
111 #define FIRMWARE_SIENNA_CICHLID_DMUB "amdgpu/sienna_cichlid_dmcub.bin"
112 MODULE_FIRMWARE(FIRMWARE_SIENNA_CICHLID_DMUB);
113 #define FIRMWARE_NAVY_FLOUNDER_DMUB "amdgpu/navy_flounder_dmcub.bin"
114 MODULE_FIRMWARE(FIRMWARE_NAVY_FLOUNDER_DMUB);
115 #define FIRMWARE_GREEN_SARDINE_DMUB "amdgpu/green_sardine_dmcub.bin"
116 MODULE_FIRMWARE(FIRMWARE_GREEN_SARDINE_DMUB);
117 #define FIRMWARE_VANGOGH_DMUB "amdgpu/vangogh_dmcub.bin"
118 MODULE_FIRMWARE(FIRMWARE_VANGOGH_DMUB);
119 #define FIRMWARE_DIMGREY_CAVEFISH_DMUB "amdgpu/dimgrey_cavefish_dmcub.bin"
120 MODULE_FIRMWARE(FIRMWARE_DIMGREY_CAVEFISH_DMUB);
121 #define FIRMWARE_BEIGE_GOBY_DMUB "amdgpu/beige_goby_dmcub.bin"
122 MODULE_FIRMWARE(FIRMWARE_BEIGE_GOBY_DMUB);
123 #define FIRMWARE_YELLOW_CARP_DMUB "amdgpu/yellow_carp_dmcub.bin"
124 MODULE_FIRMWARE(FIRMWARE_YELLOW_CARP_DMUB);
125 #define FIRMWARE_DCN_314_DMUB "amdgpu/dcn_3_1_4_dmcub.bin"
126 MODULE_FIRMWARE(FIRMWARE_DCN_314_DMUB);
127 #define FIRMWARE_DCN_315_DMUB "amdgpu/dcn_3_1_5_dmcub.bin"
128 MODULE_FIRMWARE(FIRMWARE_DCN_315_DMUB);
129 #define FIRMWARE_DCN316_DMUB "amdgpu/dcn_3_1_6_dmcub.bin"
130 MODULE_FIRMWARE(FIRMWARE_DCN316_DMUB);
131
132 #define FIRMWARE_DCN_V3_2_0_DMCUB "amdgpu/dcn_3_2_0_dmcub.bin"
133 MODULE_FIRMWARE(FIRMWARE_DCN_V3_2_0_DMCUB);
134 #define FIRMWARE_DCN_V3_2_1_DMCUB "amdgpu/dcn_3_2_1_dmcub.bin"
135 MODULE_FIRMWARE(FIRMWARE_DCN_V3_2_1_DMCUB);
136
137 #define FIRMWARE_RAVEN_DMCU "amdgpu/raven_dmcu.bin"
138 MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU);
139
140 #define FIRMWARE_NAVI12_DMCU "amdgpu/navi12_dmcu.bin"
141 MODULE_FIRMWARE(FIRMWARE_NAVI12_DMCU);
142
143 #define FIRMWARE_DCN_35_DMUB "amdgpu/dcn_3_5_dmcub.bin"
144 MODULE_FIRMWARE(FIRMWARE_DCN_35_DMUB);
145
146 #define FIRMWARE_DCN_351_DMUB "amdgpu/dcn_3_5_1_dmcub.bin"
147 MODULE_FIRMWARE(FIRMWARE_DCN_351_DMUB);
148
149 #define FIRMWARE_DCN_36_DMUB "amdgpu/dcn_3_6_dmcub.bin"
150 MODULE_FIRMWARE(FIRMWARE_DCN_36_DMUB);
151
152 #define FIRMWARE_DCN_401_DMUB "amdgpu/dcn_4_0_1_dmcub.bin"
153 MODULE_FIRMWARE(FIRMWARE_DCN_401_DMUB);
154
155 /**
156 * DOC: overview
157 *
158 * The AMDgpu display manager, **amdgpu_dm** (or even simpler,
159 * **dm**) sits between DRM and DC. It acts as a liaison, converting DRM
160 * requests into DC requests, and DC responses into DRM responses.
161 *
162 * The root control structure is &struct amdgpu_display_manager.
163 */
164
165 /* basic init/fini API */
166 static int amdgpu_dm_init(struct amdgpu_device *adev);
167 static void amdgpu_dm_fini(struct amdgpu_device *adev);
168 static bool is_freesync_video_mode(const struct drm_display_mode *mode, struct amdgpu_dm_connector *aconnector);
169 static void reset_freesync_config_for_crtc(struct dm_crtc_state *new_crtc_state);
170 static struct amdgpu_i2c_adapter *
171 create_i2c(struct ddc_service *ddc_service, bool oem);
172
get_subconnector_type(struct dc_link * link)173 static enum drm_mode_subconnector get_subconnector_type(struct dc_link *link)
174 {
175 switch (link->dpcd_caps.dongle_type) {
176 case DISPLAY_DONGLE_NONE:
177 return DRM_MODE_SUBCONNECTOR_Native;
178 case DISPLAY_DONGLE_DP_VGA_CONVERTER:
179 return DRM_MODE_SUBCONNECTOR_VGA;
180 case DISPLAY_DONGLE_DP_DVI_CONVERTER:
181 case DISPLAY_DONGLE_DP_DVI_DONGLE:
182 return DRM_MODE_SUBCONNECTOR_DVID;
183 case DISPLAY_DONGLE_DP_HDMI_CONVERTER:
184 case DISPLAY_DONGLE_DP_HDMI_DONGLE:
185 return DRM_MODE_SUBCONNECTOR_HDMIA;
186 case DISPLAY_DONGLE_DP_HDMI_MISMATCHED_DONGLE:
187 default:
188 return DRM_MODE_SUBCONNECTOR_Unknown;
189 }
190 }
191
update_subconnector_property(struct amdgpu_dm_connector * aconnector)192 static void update_subconnector_property(struct amdgpu_dm_connector *aconnector)
193 {
194 struct dc_link *link = aconnector->dc_link;
195 struct drm_connector *connector = &aconnector->base;
196 enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
197
198 if (connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort)
199 return;
200
201 if (aconnector->dc_sink)
202 subconnector = get_subconnector_type(link);
203
204 drm_object_property_set_value(&connector->base,
205 connector->dev->mode_config.dp_subconnector_property,
206 subconnector);
207 }
208
209 /*
210 * initializes drm_device display related structures, based on the information
211 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
212 * drm_encoder, drm_mode_config
213 *
214 * Returns 0 on success
215 */
216 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
217 /* removes and deallocates the drm structures, created by the above function */
218 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
219
220 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
221 struct amdgpu_dm_connector *amdgpu_dm_connector,
222 u32 link_index,
223 struct amdgpu_encoder *amdgpu_encoder);
224 static int amdgpu_dm_encoder_init(struct drm_device *dev,
225 struct amdgpu_encoder *aencoder,
226 uint32_t link_index);
227
228 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
229
230 static int amdgpu_dm_atomic_setup_commit(struct drm_atomic_state *state);
231 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
232
233 static int amdgpu_dm_atomic_check(struct drm_device *dev,
234 struct drm_atomic_state *state);
235
236 static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector);
237 static void handle_hpd_rx_irq(void *param);
238
239 static void amdgpu_dm_backlight_set_level(struct amdgpu_display_manager *dm,
240 int bl_idx,
241 u32 user_brightness);
242
243 static bool
244 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
245 struct drm_crtc_state *new_crtc_state);
246 /*
247 * dm_vblank_get_counter
248 *
249 * @brief
250 * Get counter for number of vertical blanks
251 *
252 * @param
253 * struct amdgpu_device *adev - [in] desired amdgpu device
254 * int disp_idx - [in] which CRTC to get the counter from
255 *
256 * @return
257 * Counter for vertical blanks
258 */
dm_vblank_get_counter(struct amdgpu_device * adev,int crtc)259 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
260 {
261 struct amdgpu_crtc *acrtc = NULL;
262
263 if (crtc >= adev->mode_info.num_crtc)
264 return 0;
265
266 acrtc = adev->mode_info.crtcs[crtc];
267
268 if (!acrtc->dm_irq_params.stream) {
269 drm_err(adev_to_drm(adev), "dc_stream_state is NULL for crtc '%d'!\n",
270 crtc);
271 return 0;
272 }
273
274 return dc_stream_get_vblank_counter(acrtc->dm_irq_params.stream);
275 }
276
dm_crtc_get_scanoutpos(struct amdgpu_device * adev,int crtc,u32 * vbl,u32 * position)277 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
278 u32 *vbl, u32 *position)
279 {
280 u32 v_blank_start = 0, v_blank_end = 0, h_position = 0, v_position = 0;
281 struct amdgpu_crtc *acrtc = NULL;
282 struct dc *dc = adev->dm.dc;
283
284 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
285 return -EINVAL;
286
287 acrtc = adev->mode_info.crtcs[crtc];
288
289 if (!acrtc->dm_irq_params.stream) {
290 drm_err(adev_to_drm(adev), "dc_stream_state is NULL for crtc '%d'!\n",
291 crtc);
292 return 0;
293 }
294
295 if (dc && dc->caps.ips_support && dc->idle_optimizations_allowed)
296 dc_allow_idle_optimizations(dc, false);
297
298 /*
299 * TODO rework base driver to use values directly.
300 * for now parse it back into reg-format
301 */
302 dc_stream_get_scanoutpos(acrtc->dm_irq_params.stream,
303 &v_blank_start,
304 &v_blank_end,
305 &h_position,
306 &v_position);
307
308 *position = v_position | (h_position << 16);
309 *vbl = v_blank_start | (v_blank_end << 16);
310
311 return 0;
312 }
313
dm_is_idle(struct amdgpu_ip_block * ip_block)314 static bool dm_is_idle(struct amdgpu_ip_block *ip_block)
315 {
316 /* XXX todo */
317 return true;
318 }
319
dm_wait_for_idle(struct amdgpu_ip_block * ip_block)320 static int dm_wait_for_idle(struct amdgpu_ip_block *ip_block)
321 {
322 /* XXX todo */
323 return 0;
324 }
325
dm_check_soft_reset(struct amdgpu_ip_block * ip_block)326 static bool dm_check_soft_reset(struct amdgpu_ip_block *ip_block)
327 {
328 return false;
329 }
330
dm_soft_reset(struct amdgpu_ip_block * ip_block)331 static int dm_soft_reset(struct amdgpu_ip_block *ip_block)
332 {
333 /* XXX todo */
334 return 0;
335 }
336
337 static struct amdgpu_crtc *
get_crtc_by_otg_inst(struct amdgpu_device * adev,int otg_inst)338 get_crtc_by_otg_inst(struct amdgpu_device *adev,
339 int otg_inst)
340 {
341 struct drm_device *dev = adev_to_drm(adev);
342 struct drm_crtc *crtc;
343 struct amdgpu_crtc *amdgpu_crtc;
344
345 if (WARN_ON(otg_inst == -1))
346 return adev->mode_info.crtcs[0];
347
348 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
349 amdgpu_crtc = to_amdgpu_crtc(crtc);
350
351 if (amdgpu_crtc->otg_inst == otg_inst)
352 return amdgpu_crtc;
353 }
354
355 return NULL;
356 }
357
is_dc_timing_adjust_needed(struct dm_crtc_state * old_state,struct dm_crtc_state * new_state)358 static inline bool is_dc_timing_adjust_needed(struct dm_crtc_state *old_state,
359 struct dm_crtc_state *new_state)
360 {
361 if (new_state->stream->adjust.timing_adjust_pending)
362 return true;
363 if (new_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)
364 return true;
365 else if (amdgpu_dm_crtc_vrr_active(old_state) != amdgpu_dm_crtc_vrr_active(new_state))
366 return true;
367 else
368 return false;
369 }
370
371 /*
372 * DC will program planes with their z-order determined by their ordering
373 * in the dc_surface_updates array. This comparator is used to sort them
374 * by descending zpos.
375 */
dm_plane_layer_index_cmp(const void * a,const void * b)376 static int dm_plane_layer_index_cmp(const void *a, const void *b)
377 {
378 const struct dc_surface_update *sa = (struct dc_surface_update *)a;
379 const struct dc_surface_update *sb = (struct dc_surface_update *)b;
380
381 /* Sort by descending dc_plane layer_index (i.e. normalized_zpos) */
382 return sb->surface->layer_index - sa->surface->layer_index;
383 }
384
385 /**
386 * update_planes_and_stream_adapter() - Send planes to be updated in DC
387 *
388 * DC has a generic way to update planes and stream via
389 * dc_update_planes_and_stream function; however, DM might need some
390 * adjustments and preparation before calling it. This function is a wrapper
391 * for the dc_update_planes_and_stream that does any required configuration
392 * before passing control to DC.
393 *
394 * @dc: Display Core control structure
395 * @update_type: specify whether it is FULL/MEDIUM/FAST update
396 * @planes_count: planes count to update
397 * @stream: stream state
398 * @stream_update: stream update
399 * @array_of_surface_update: dc surface update pointer
400 *
401 */
update_planes_and_stream_adapter(struct dc * dc,int update_type,int planes_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update,struct dc_surface_update * array_of_surface_update)402 static inline bool update_planes_and_stream_adapter(struct dc *dc,
403 int update_type,
404 int planes_count,
405 struct dc_stream_state *stream,
406 struct dc_stream_update *stream_update,
407 struct dc_surface_update *array_of_surface_update)
408 {
409 sort(array_of_surface_update, planes_count,
410 sizeof(*array_of_surface_update), dm_plane_layer_index_cmp, NULL);
411
412 /*
413 * Previous frame finished and HW is ready for optimization.
414 */
415 dc_post_update_surfaces_to_stream(dc);
416
417 return dc_update_planes_and_stream(dc,
418 array_of_surface_update,
419 planes_count,
420 stream,
421 stream_update);
422 }
423
424 /**
425 * dm_pflip_high_irq() - Handle pageflip interrupt
426 * @interrupt_params: ignored
427 *
428 * Handles the pageflip interrupt by notifying all interested parties
429 * that the pageflip has been completed.
430 */
dm_pflip_high_irq(void * interrupt_params)431 static void dm_pflip_high_irq(void *interrupt_params)
432 {
433 struct amdgpu_crtc *amdgpu_crtc;
434 struct common_irq_params *irq_params = interrupt_params;
435 struct amdgpu_device *adev = irq_params->adev;
436 struct drm_device *dev = adev_to_drm(adev);
437 unsigned long flags;
438 struct drm_pending_vblank_event *e;
439 u32 vpos, hpos, v_blank_start, v_blank_end;
440 bool vrr_active;
441
442 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
443
444 /* IRQ could occur when in initial stage */
445 /* TODO work and BO cleanup */
446 if (amdgpu_crtc == NULL) {
447 drm_dbg_state(dev, "CRTC is null, returning.\n");
448 return;
449 }
450
451 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
452
453 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
454 drm_dbg_state(dev,
455 "amdgpu_crtc->pflip_status = %d != AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p]\n",
456 amdgpu_crtc->pflip_status, AMDGPU_FLIP_SUBMITTED,
457 amdgpu_crtc->crtc_id, amdgpu_crtc);
458 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
459 return;
460 }
461
462 /* page flip completed. */
463 e = amdgpu_crtc->event;
464 amdgpu_crtc->event = NULL;
465
466 WARN_ON(!e);
467
468 vrr_active = amdgpu_dm_crtc_vrr_active_irq(amdgpu_crtc);
469
470 /* Fixed refresh rate, or VRR scanout position outside front-porch? */
471 if (!vrr_active ||
472 !dc_stream_get_scanoutpos(amdgpu_crtc->dm_irq_params.stream, &v_blank_start,
473 &v_blank_end, &hpos, &vpos) ||
474 (vpos < v_blank_start)) {
475 /* Update to correct count and vblank timestamp if racing with
476 * vblank irq. This also updates to the correct vblank timestamp
477 * even in VRR mode, as scanout is past the front-porch atm.
478 */
479 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
480
481 /* Wake up userspace by sending the pageflip event with proper
482 * count and timestamp of vblank of flip completion.
483 */
484 if (e) {
485 drm_crtc_send_vblank_event(&amdgpu_crtc->base, e);
486
487 /* Event sent, so done with vblank for this flip */
488 drm_crtc_vblank_put(&amdgpu_crtc->base);
489 }
490 } else if (e) {
491 /* VRR active and inside front-porch: vblank count and
492 * timestamp for pageflip event will only be up to date after
493 * drm_crtc_handle_vblank() has been executed from late vblank
494 * irq handler after start of back-porch (vline 0). We queue the
495 * pageflip event for send-out by drm_crtc_handle_vblank() with
496 * updated timestamp and count, once it runs after us.
497 *
498 * We need to open-code this instead of using the helper
499 * drm_crtc_arm_vblank_event(), as that helper would
500 * call drm_crtc_accurate_vblank_count(), which we must
501 * not call in VRR mode while we are in front-porch!
502 */
503
504 /* sequence will be replaced by real count during send-out. */
505 e->sequence = drm_crtc_vblank_count(&amdgpu_crtc->base);
506 e->pipe = amdgpu_crtc->crtc_id;
507
508 list_add_tail(&e->base.link, &adev_to_drm(adev)->vblank_event_list);
509 e = NULL;
510 }
511
512 /* Keep track of vblank of this flip for flip throttling. We use the
513 * cooked hw counter, as that one incremented at start of this vblank
514 * of pageflip completion, so last_flip_vblank is the forbidden count
515 * for queueing new pageflips if vsync + VRR is enabled.
516 */
517 amdgpu_crtc->dm_irq_params.last_flip_vblank =
518 amdgpu_get_vblank_counter_kms(&amdgpu_crtc->base);
519
520 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
521 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
522
523 drm_dbg_state(dev,
524 "crtc:%d[%p], pflip_stat:AMDGPU_FLIP_NONE, vrr[%d]-fp %d\n",
525 amdgpu_crtc->crtc_id, amdgpu_crtc, vrr_active, (int)!e);
526 }
527
dm_handle_vmin_vmax_update(struct work_struct * offload_work)528 static void dm_handle_vmin_vmax_update(struct work_struct *offload_work)
529 {
530 struct vupdate_offload_work *work = container_of(offload_work, struct vupdate_offload_work, work);
531 struct amdgpu_device *adev = work->adev;
532 struct dc_stream_state *stream = work->stream;
533 struct dc_crtc_timing_adjust *adjust = work->adjust;
534
535 mutex_lock(&adev->dm.dc_lock);
536 dc_stream_adjust_vmin_vmax(adev->dm.dc, stream, adjust);
537 mutex_unlock(&adev->dm.dc_lock);
538
539 dc_stream_release(stream);
540 kfree(work->adjust);
541 kfree(work);
542 }
543
schedule_dc_vmin_vmax(struct amdgpu_device * adev,struct dc_stream_state * stream,struct dc_crtc_timing_adjust * adjust)544 static void schedule_dc_vmin_vmax(struct amdgpu_device *adev,
545 struct dc_stream_state *stream,
546 struct dc_crtc_timing_adjust *adjust)
547 {
548 struct vupdate_offload_work *offload_work = kzalloc_obj(*offload_work,
549 GFP_NOWAIT);
550 if (!offload_work) {
551 drm_dbg_driver(adev_to_drm(adev), "Failed to allocate vupdate_offload_work\n");
552 return;
553 }
554
555 struct dc_crtc_timing_adjust *adjust_copy = kzalloc_obj(*adjust_copy,
556 GFP_NOWAIT);
557 if (!adjust_copy) {
558 drm_dbg_driver(adev_to_drm(adev), "Failed to allocate adjust_copy\n");
559 kfree(offload_work);
560 return;
561 }
562
563 dc_stream_retain(stream);
564 memcpy(adjust_copy, adjust, sizeof(*adjust_copy));
565
566 INIT_WORK(&offload_work->work, dm_handle_vmin_vmax_update);
567 offload_work->adev = adev;
568 offload_work->stream = stream;
569 offload_work->adjust = adjust_copy;
570
571 queue_work(system_wq, &offload_work->work);
572 }
573
dm_vupdate_high_irq(void * interrupt_params)574 static void dm_vupdate_high_irq(void *interrupt_params)
575 {
576 struct common_irq_params *irq_params = interrupt_params;
577 struct amdgpu_device *adev = irq_params->adev;
578 struct amdgpu_crtc *acrtc;
579 struct drm_device *drm_dev;
580 struct drm_vblank_crtc *vblank;
581 ktime_t frame_duration_ns, previous_timestamp;
582 unsigned long flags;
583 int vrr_active;
584
585 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VUPDATE);
586
587 if (acrtc) {
588 vrr_active = amdgpu_dm_crtc_vrr_active_irq(acrtc);
589 drm_dev = acrtc->base.dev;
590 vblank = drm_crtc_vblank_crtc(&acrtc->base);
591 previous_timestamp = atomic64_read(&irq_params->previous_timestamp);
592 frame_duration_ns = vblank->time - previous_timestamp;
593
594 if (frame_duration_ns > 0) {
595 trace_amdgpu_refresh_rate_track(acrtc->base.index,
596 frame_duration_ns,
597 ktime_divns(NSEC_PER_SEC, frame_duration_ns));
598 atomic64_set(&irq_params->previous_timestamp, vblank->time);
599 }
600
601 drm_dbg_vbl(drm_dev,
602 "crtc:%d, vupdate-vrr:%d\n", acrtc->crtc_id,
603 vrr_active);
604
605 /* Core vblank handling is done here after end of front-porch in
606 * vrr mode, as vblank timestamping will give valid results
607 * while now done after front-porch. This will also deliver
608 * page-flip completion events that have been queued to us
609 * if a pageflip happened inside front-porch.
610 */
611 if (vrr_active && acrtc->dm_irq_params.stream) {
612 bool replay_en = acrtc->dm_irq_params.stream->link->replay_settings.replay_feature_enabled;
613 bool psr_en = acrtc->dm_irq_params.stream->link->psr_settings.psr_feature_enabled;
614 bool fs_active_var_en = acrtc->dm_irq_params.freesync_config.state
615 == VRR_STATE_ACTIVE_VARIABLE;
616
617 amdgpu_dm_crtc_handle_vblank(acrtc);
618
619 /* BTR processing for pre-DCE12 ASICs */
620 if (adev->family < AMDGPU_FAMILY_AI) {
621 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
622 mod_freesync_handle_v_update(
623 adev->dm.freesync_module,
624 acrtc->dm_irq_params.stream,
625 &acrtc->dm_irq_params.vrr_params);
626
627 if (fs_active_var_en || (!fs_active_var_en && !replay_en && !psr_en)) {
628 schedule_dc_vmin_vmax(adev,
629 acrtc->dm_irq_params.stream,
630 &acrtc->dm_irq_params.vrr_params.adjust);
631 }
632 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
633 }
634 }
635 }
636 }
637
638 /**
639 * dm_crtc_high_irq() - Handles CRTC interrupt
640 * @interrupt_params: used for determining the CRTC instance
641 *
642 * Handles the CRTC/VSYNC interrupt by notfying DRM's VBLANK
643 * event handler.
644 */
dm_crtc_high_irq(void * interrupt_params)645 static void dm_crtc_high_irq(void *interrupt_params)
646 {
647 struct common_irq_params *irq_params = interrupt_params;
648 struct amdgpu_device *adev = irq_params->adev;
649 struct drm_writeback_job *job;
650 struct amdgpu_crtc *acrtc;
651 unsigned long flags;
652 int vrr_active;
653
654 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
655 if (!acrtc)
656 return;
657
658 if (acrtc->wb_conn) {
659 spin_lock_irqsave(&acrtc->wb_conn->job_lock, flags);
660
661 if (acrtc->wb_pending) {
662 job = list_first_entry_or_null(&acrtc->wb_conn->job_queue,
663 struct drm_writeback_job,
664 list_entry);
665 acrtc->wb_pending = false;
666 spin_unlock_irqrestore(&acrtc->wb_conn->job_lock, flags);
667
668 if (job) {
669 unsigned int v_total, refresh_hz;
670 struct dc_stream_state *stream = acrtc->dm_irq_params.stream;
671
672 v_total = stream->adjust.v_total_max ?
673 stream->adjust.v_total_max : stream->timing.v_total;
674 refresh_hz = div_u64((uint64_t) stream->timing.pix_clk_100hz *
675 100LL, (v_total * stream->timing.h_total));
676 mdelay(1000 / refresh_hz);
677
678 drm_writeback_signal_completion(acrtc->wb_conn, 0);
679 dc_stream_fc_disable_writeback(adev->dm.dc,
680 acrtc->dm_irq_params.stream, 0);
681 }
682 } else
683 spin_unlock_irqrestore(&acrtc->wb_conn->job_lock, flags);
684 }
685
686 vrr_active = amdgpu_dm_crtc_vrr_active_irq(acrtc);
687
688 drm_dbg_vbl(adev_to_drm(adev),
689 "crtc:%d, vupdate-vrr:%d, planes:%d\n", acrtc->crtc_id,
690 vrr_active, acrtc->dm_irq_params.active_planes);
691
692 /**
693 * Core vblank handling at start of front-porch is only possible
694 * in non-vrr mode, as only there vblank timestamping will give
695 * valid results while done in front-porch. Otherwise defer it
696 * to dm_vupdate_high_irq after end of front-porch.
697 */
698 if (!vrr_active)
699 amdgpu_dm_crtc_handle_vblank(acrtc);
700
701 /**
702 * Following stuff must happen at start of vblank, for crc
703 * computation and below-the-range btr support in vrr mode.
704 */
705 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
706
707 /* BTR updates need to happen before VUPDATE on Vega and above. */
708 if (adev->family < AMDGPU_FAMILY_AI)
709 return;
710
711 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
712
713 if (acrtc->dm_irq_params.stream &&
714 acrtc->dm_irq_params.vrr_params.supported) {
715 bool replay_en = acrtc->dm_irq_params.stream->link->replay_settings.replay_feature_enabled;
716 bool psr_en = acrtc->dm_irq_params.stream->link->psr_settings.psr_feature_enabled;
717 bool fs_active_var_en = acrtc->dm_irq_params.freesync_config.state == VRR_STATE_ACTIVE_VARIABLE;
718
719 mod_freesync_handle_v_update(adev->dm.freesync_module,
720 acrtc->dm_irq_params.stream,
721 &acrtc->dm_irq_params.vrr_params);
722
723 /* update vmin_vmax only if freesync is enabled, or only if PSR and REPLAY are disabled */
724 if (fs_active_var_en || (!fs_active_var_en && !replay_en && !psr_en)) {
725 schedule_dc_vmin_vmax(adev, acrtc->dm_irq_params.stream,
726 &acrtc->dm_irq_params.vrr_params.adjust);
727 }
728 }
729
730 /*
731 * If there aren't any active_planes then DCH HUBP may be clock-gated.
732 * In that case, pageflip completion interrupts won't fire and pageflip
733 * completion events won't get delivered. Prevent this by sending
734 * pending pageflip events from here if a flip is still pending.
735 *
736 * If any planes are enabled, use dm_pflip_high_irq() instead, to
737 * avoid race conditions between flip programming and completion,
738 * which could cause too early flip completion events.
739 */
740 if (adev->family >= AMDGPU_FAMILY_RV &&
741 acrtc->pflip_status == AMDGPU_FLIP_SUBMITTED &&
742 acrtc->dm_irq_params.active_planes == 0) {
743 if (acrtc->event) {
744 drm_crtc_send_vblank_event(&acrtc->base, acrtc->event);
745 acrtc->event = NULL;
746 drm_crtc_vblank_put(&acrtc->base);
747 }
748 acrtc->pflip_status = AMDGPU_FLIP_NONE;
749 }
750
751 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
752 }
753
754 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
755 /**
756 * dm_dcn_vertical_interrupt0_high_irq() - Handles OTG Vertical interrupt0 for
757 * DCN generation ASICs
758 * @interrupt_params: interrupt parameters
759 *
760 * Used to set crc window/read out crc value at vertical line 0 position
761 */
dm_dcn_vertical_interrupt0_high_irq(void * interrupt_params)762 static void dm_dcn_vertical_interrupt0_high_irq(void *interrupt_params)
763 {
764 struct common_irq_params *irq_params = interrupt_params;
765 struct amdgpu_device *adev = irq_params->adev;
766 struct amdgpu_crtc *acrtc;
767
768 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VLINE0);
769
770 if (!acrtc)
771 return;
772
773 amdgpu_dm_crtc_handle_crc_window_irq(&acrtc->base);
774 }
775 #endif /* CONFIG_DRM_AMD_SECURE_DISPLAY */
776
777 /**
778 * dmub_aux_setconfig_callback - Callback for AUX or SET_CONFIG command.
779 * @adev: amdgpu_device pointer
780 * @notify: dmub notification structure
781 *
782 * Dmub AUX or SET_CONFIG command completion processing callback
783 * Copies dmub notification to DM which is to be read by AUX command.
784 * issuing thread and also signals the event to wake up the thread.
785 */
dmub_aux_setconfig_callback(struct amdgpu_device * adev,struct dmub_notification * notify)786 static void dmub_aux_setconfig_callback(struct amdgpu_device *adev,
787 struct dmub_notification *notify)
788 {
789 if (adev->dm.dmub_notify)
790 memcpy(adev->dm.dmub_notify, notify, sizeof(struct dmub_notification));
791 if (notify->type == DMUB_NOTIFICATION_AUX_REPLY)
792 complete(&adev->dm.dmub_aux_transfer_done);
793 }
794
dmub_aux_fused_io_callback(struct amdgpu_device * adev,struct dmub_notification * notify)795 static void dmub_aux_fused_io_callback(struct amdgpu_device *adev,
796 struct dmub_notification *notify)
797 {
798 if (!adev || !notify) {
799 ASSERT(false);
800 return;
801 }
802
803 const struct dmub_cmd_fused_request *req = ¬ify->fused_request;
804 const uint8_t ddc_line = req->u.aux.ddc_line;
805
806 if (ddc_line >= ARRAY_SIZE(adev->dm.fused_io)) {
807 ASSERT(false);
808 return;
809 }
810
811 struct fused_io_sync *sync = &adev->dm.fused_io[ddc_line];
812
813 static_assert(sizeof(*req) <= sizeof(sync->reply_data), "Size mismatch");
814 memcpy(sync->reply_data, req, sizeof(*req));
815 complete(&sync->replied);
816 }
817
818 /**
819 * dmub_hpd_callback - DMUB HPD interrupt processing callback.
820 * @adev: amdgpu_device pointer
821 * @notify: dmub notification structure
822 *
823 * Dmub Hpd interrupt processing callback. Gets displayindex through the
824 * ink index and calls helper to do the processing.
825 */
dmub_hpd_callback(struct amdgpu_device * adev,struct dmub_notification * notify)826 static void dmub_hpd_callback(struct amdgpu_device *adev,
827 struct dmub_notification *notify)
828 {
829 struct amdgpu_dm_connector *aconnector;
830 struct amdgpu_dm_connector *hpd_aconnector = NULL;
831 struct drm_connector *connector;
832 struct drm_connector_list_iter iter;
833 struct dc_link *link;
834 u8 link_index = 0;
835 struct drm_device *dev;
836
837 if (adev == NULL)
838 return;
839
840 if (notify == NULL) {
841 drm_err(adev_to_drm(adev), "DMUB HPD callback notification was NULL");
842 return;
843 }
844
845 if (notify->link_index > adev->dm.dc->link_count) {
846 drm_err(adev_to_drm(adev), "DMUB HPD index (%u)is abnormal", notify->link_index);
847 return;
848 }
849
850 /* Skip DMUB HPD IRQ in suspend/resume. We will probe them later. */
851 if (notify->type == DMUB_NOTIFICATION_HPD && adev->in_suspend) {
852 drm_info(adev_to_drm(adev), "Skip DMUB HPD IRQ callback in suspend/resume\n");
853 return;
854 }
855
856 link_index = notify->link_index;
857 link = adev->dm.dc->links[link_index];
858 dev = adev->dm.ddev;
859
860 drm_connector_list_iter_begin(dev, &iter);
861 drm_for_each_connector_iter(connector, &iter) {
862
863 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
864 continue;
865
866 aconnector = to_amdgpu_dm_connector(connector);
867 if (link && aconnector->dc_link == link) {
868 if (notify->type == DMUB_NOTIFICATION_HPD)
869 drm_info(adev_to_drm(adev), "DMUB HPD IRQ callback: link_index=%u\n", link_index);
870 else if (notify->type == DMUB_NOTIFICATION_HPD_IRQ)
871 drm_info(adev_to_drm(adev), "DMUB HPD RX IRQ callback: link_index=%u\n", link_index);
872 else
873 drm_warn(adev_to_drm(adev), "DMUB Unknown HPD callback type %d, link_index=%u\n",
874 notify->type, link_index);
875
876 hpd_aconnector = aconnector;
877 break;
878 }
879 }
880 drm_connector_list_iter_end(&iter);
881
882 if (hpd_aconnector) {
883 if (notify->type == DMUB_NOTIFICATION_HPD) {
884 if (hpd_aconnector->dc_link->hpd_status == (notify->hpd_status == DP_HPD_PLUG))
885 drm_warn(adev_to_drm(adev), "DMUB reported hpd status unchanged. link_index=%u\n", link_index);
886 handle_hpd_irq_helper(hpd_aconnector);
887 } else if (notify->type == DMUB_NOTIFICATION_HPD_IRQ) {
888 handle_hpd_rx_irq(hpd_aconnector);
889 }
890 }
891 }
892
893 /**
894 * dmub_hpd_sense_callback - DMUB HPD sense processing callback.
895 * @adev: amdgpu_device pointer
896 * @notify: dmub notification structure
897 *
898 * HPD sense changes can occur during low power states and need to be
899 * notified from firmware to driver.
900 */
dmub_hpd_sense_callback(struct amdgpu_device * adev,struct dmub_notification * notify)901 static void dmub_hpd_sense_callback(struct amdgpu_device *adev,
902 struct dmub_notification *notify)
903 {
904 drm_dbg_driver(adev_to_drm(adev), "DMUB HPD SENSE callback.\n");
905 }
906
907 /**
908 * register_dmub_notify_callback - Sets callback for DMUB notify
909 * @adev: amdgpu_device pointer
910 * @type: Type of dmub notification
911 * @callback: Dmub interrupt callback function
912 * @dmub_int_thread_offload: offload indicator
913 *
914 * API to register a dmub callback handler for a dmub notification
915 * Also sets indicator whether callback processing to be offloaded.
916 * to dmub interrupt handling thread
917 * Return: true if successfully registered, false if there is existing registration
918 */
register_dmub_notify_callback(struct amdgpu_device * adev,enum dmub_notification_type type,dmub_notify_interrupt_callback_t callback,bool dmub_int_thread_offload)919 static bool register_dmub_notify_callback(struct amdgpu_device *adev,
920 enum dmub_notification_type type,
921 dmub_notify_interrupt_callback_t callback,
922 bool dmub_int_thread_offload)
923 {
924 if (callback != NULL && type < ARRAY_SIZE(adev->dm.dmub_thread_offload)) {
925 adev->dm.dmub_callback[type] = callback;
926 adev->dm.dmub_thread_offload[type] = dmub_int_thread_offload;
927 } else
928 return false;
929
930 return true;
931 }
932
dm_handle_hpd_work(struct work_struct * work)933 static void dm_handle_hpd_work(struct work_struct *work)
934 {
935 struct dmub_hpd_work *dmub_hpd_wrk;
936
937 dmub_hpd_wrk = container_of(work, struct dmub_hpd_work, handle_hpd_work);
938
939 if (!dmub_hpd_wrk->dmub_notify) {
940 drm_err(adev_to_drm(dmub_hpd_wrk->adev), "dmub_hpd_wrk dmub_notify is NULL");
941 return;
942 }
943
944 if (dmub_hpd_wrk->dmub_notify->type < ARRAY_SIZE(dmub_hpd_wrk->adev->dm.dmub_callback)) {
945 dmub_hpd_wrk->adev->dm.dmub_callback[dmub_hpd_wrk->dmub_notify->type](dmub_hpd_wrk->adev,
946 dmub_hpd_wrk->dmub_notify);
947 }
948
949 kfree(dmub_hpd_wrk->dmub_notify);
950 kfree(dmub_hpd_wrk);
951
952 }
953
dmub_notification_type_str(enum dmub_notification_type e)954 static const char *dmub_notification_type_str(enum dmub_notification_type e)
955 {
956 switch (e) {
957 case DMUB_NOTIFICATION_NO_DATA:
958 return "NO_DATA";
959 case DMUB_NOTIFICATION_AUX_REPLY:
960 return "AUX_REPLY";
961 case DMUB_NOTIFICATION_HPD:
962 return "HPD";
963 case DMUB_NOTIFICATION_HPD_IRQ:
964 return "HPD_IRQ";
965 case DMUB_NOTIFICATION_SET_CONFIG_REPLY:
966 return "SET_CONFIG_REPLY";
967 case DMUB_NOTIFICATION_DPIA_NOTIFICATION:
968 return "DPIA_NOTIFICATION";
969 case DMUB_NOTIFICATION_HPD_SENSE_NOTIFY:
970 return "HPD_SENSE_NOTIFY";
971 case DMUB_NOTIFICATION_FUSED_IO:
972 return "FUSED_IO";
973 default:
974 return "<unknown>";
975 }
976 }
977
978 #define DMUB_TRACE_MAX_READ 64
979 /**
980 * dm_dmub_outbox1_low_irq() - Handles Outbox interrupt
981 * @interrupt_params: used for determining the Outbox instance
982 *
983 * Handles the Outbox Interrupt
984 * event handler.
985 */
dm_dmub_outbox1_low_irq(void * interrupt_params)986 static void dm_dmub_outbox1_low_irq(void *interrupt_params)
987 {
988 struct dmub_notification notify = {0};
989 struct common_irq_params *irq_params = interrupt_params;
990 struct amdgpu_device *adev = irq_params->adev;
991 struct amdgpu_display_manager *dm = &adev->dm;
992 struct dmcub_trace_buf_entry entry = { 0 };
993 u32 count = 0;
994 struct dmub_hpd_work *dmub_hpd_wrk;
995
996 do {
997 if (dc_dmub_srv_get_dmub_outbox0_msg(dm->dc, &entry)) {
998 trace_amdgpu_dmub_trace_high_irq(entry.trace_code, entry.tick_count,
999 entry.param0, entry.param1);
1000
1001 drm_dbg_driver(adev_to_drm(adev), "trace_code:%u, tick_count:%u, param0:%u, param1:%u\n",
1002 entry.trace_code, entry.tick_count, entry.param0, entry.param1);
1003 } else
1004 break;
1005
1006 count++;
1007
1008 } while (count <= DMUB_TRACE_MAX_READ);
1009
1010 if (count > DMUB_TRACE_MAX_READ)
1011 drm_dbg_driver(adev_to_drm(adev), "Warning : count > DMUB_TRACE_MAX_READ");
1012
1013 if (dc_enable_dmub_notifications(adev->dm.dc) &&
1014 irq_params->irq_src == DC_IRQ_SOURCE_DMCUB_OUTBOX) {
1015
1016 do {
1017 dc_stat_get_dmub_notification(adev->dm.dc, ¬ify);
1018 if (notify.type >= ARRAY_SIZE(dm->dmub_thread_offload)) {
1019 drm_err(adev_to_drm(adev), "DM: notify type %d invalid!", notify.type);
1020 continue;
1021 }
1022 if (!dm->dmub_callback[notify.type]) {
1023 drm_warn(adev_to_drm(adev), "DMUB notification skipped due to no handler: type=%s\n",
1024 dmub_notification_type_str(notify.type));
1025 continue;
1026 }
1027 if (dm->dmub_thread_offload[notify.type] == true) {
1028 dmub_hpd_wrk = kzalloc_obj(*dmub_hpd_wrk,
1029 GFP_ATOMIC);
1030 if (!dmub_hpd_wrk) {
1031 drm_err(adev_to_drm(adev), "Failed to allocate dmub_hpd_wrk");
1032 return;
1033 }
1034 dmub_hpd_wrk->dmub_notify = kmemdup(¬ify, sizeof(struct dmub_notification),
1035 GFP_ATOMIC);
1036 if (!dmub_hpd_wrk->dmub_notify) {
1037 kfree(dmub_hpd_wrk);
1038 drm_err(adev_to_drm(adev), "Failed to allocate dmub_hpd_wrk->dmub_notify");
1039 return;
1040 }
1041 INIT_WORK(&dmub_hpd_wrk->handle_hpd_work, dm_handle_hpd_work);
1042 dmub_hpd_wrk->adev = adev;
1043 queue_work(adev->dm.delayed_hpd_wq, &dmub_hpd_wrk->handle_hpd_work);
1044 } else {
1045 dm->dmub_callback[notify.type](adev, ¬ify);
1046 }
1047 } while (notify.pending_notification);
1048 }
1049 }
1050
dm_set_clockgating_state(struct amdgpu_ip_block * ip_block,enum amd_clockgating_state state)1051 static int dm_set_clockgating_state(struct amdgpu_ip_block *ip_block,
1052 enum amd_clockgating_state state)
1053 {
1054 return 0;
1055 }
1056
dm_set_powergating_state(struct amdgpu_ip_block * ip_block,enum amd_powergating_state state)1057 static int dm_set_powergating_state(struct amdgpu_ip_block *ip_block,
1058 enum amd_powergating_state state)
1059 {
1060 return 0;
1061 }
1062
1063 /* Prototypes of private functions */
1064 static int dm_early_init(struct amdgpu_ip_block *ip_block);
1065
1066 /* Allocate memory for FBC compressed data */
amdgpu_dm_fbc_init(struct drm_connector * connector)1067 static void amdgpu_dm_fbc_init(struct drm_connector *connector)
1068 {
1069 struct amdgpu_device *adev = drm_to_adev(connector->dev);
1070 struct dm_compressor_info *compressor = &adev->dm.compressor;
1071 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
1072 struct drm_display_mode *mode;
1073 unsigned long max_size = 0;
1074
1075 if (adev->dm.dc->fbc_compressor == NULL)
1076 return;
1077
1078 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
1079 return;
1080
1081 if (compressor->bo_ptr)
1082 return;
1083
1084
1085 list_for_each_entry(mode, &connector->modes, head) {
1086 if (max_size < (unsigned long) mode->htotal * mode->vtotal)
1087 max_size = (unsigned long) mode->htotal * mode->vtotal;
1088 }
1089
1090 if (max_size) {
1091 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
1092 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
1093 &compressor->gpu_addr, &compressor->cpu_addr);
1094
1095 if (r)
1096 drm_err(adev_to_drm(adev), "DM: Failed to initialize FBC\n");
1097 else {
1098 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
1099 drm_info(adev_to_drm(adev), "DM: FBC alloc %lu\n", max_size*4);
1100 }
1101
1102 }
1103
1104 }
1105
amdgpu_dm_audio_component_get_eld(struct device * kdev,int port,int pipe,bool * enabled,unsigned char * buf,int max_bytes)1106 static int amdgpu_dm_audio_component_get_eld(struct device *kdev, int port,
1107 int pipe, bool *enabled,
1108 unsigned char *buf, int max_bytes)
1109 {
1110 struct drm_device *dev = dev_get_drvdata(kdev);
1111 struct amdgpu_device *adev = drm_to_adev(dev);
1112 struct drm_connector *connector;
1113 struct drm_connector_list_iter conn_iter;
1114 struct amdgpu_dm_connector *aconnector;
1115 int ret = 0;
1116
1117 *enabled = false;
1118
1119 mutex_lock(&adev->dm.audio_lock);
1120
1121 drm_connector_list_iter_begin(dev, &conn_iter);
1122 drm_for_each_connector_iter(connector, &conn_iter) {
1123
1124 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1125 continue;
1126
1127 aconnector = to_amdgpu_dm_connector(connector);
1128 if (aconnector->audio_inst != port)
1129 continue;
1130
1131 *enabled = true;
1132 mutex_lock(&connector->eld_mutex);
1133 ret = drm_eld_size(connector->eld);
1134 memcpy(buf, connector->eld, min(max_bytes, ret));
1135 mutex_unlock(&connector->eld_mutex);
1136
1137 break;
1138 }
1139 drm_connector_list_iter_end(&conn_iter);
1140
1141 mutex_unlock(&adev->dm.audio_lock);
1142
1143 drm_dbg_kms(adev_to_drm(adev), "Get ELD : idx=%d ret=%d en=%d\n", port, ret, *enabled);
1144
1145 return ret;
1146 }
1147
1148 static const struct drm_audio_component_ops amdgpu_dm_audio_component_ops = {
1149 .get_eld = amdgpu_dm_audio_component_get_eld,
1150 };
1151
amdgpu_dm_audio_component_bind(struct device * kdev,struct device * hda_kdev,void * data)1152 static int amdgpu_dm_audio_component_bind(struct device *kdev,
1153 struct device *hda_kdev, void *data)
1154 {
1155 struct drm_device *dev = dev_get_drvdata(kdev);
1156 struct amdgpu_device *adev = drm_to_adev(dev);
1157 struct drm_audio_component *acomp = data;
1158
1159 acomp->ops = &amdgpu_dm_audio_component_ops;
1160 acomp->dev = kdev;
1161 adev->dm.audio_component = acomp;
1162
1163 return 0;
1164 }
1165
amdgpu_dm_audio_component_unbind(struct device * kdev,struct device * hda_kdev,void * data)1166 static void amdgpu_dm_audio_component_unbind(struct device *kdev,
1167 struct device *hda_kdev, void *data)
1168 {
1169 struct amdgpu_device *adev = drm_to_adev(dev_get_drvdata(kdev));
1170 struct drm_audio_component *acomp = data;
1171
1172 acomp->ops = NULL;
1173 acomp->dev = NULL;
1174 adev->dm.audio_component = NULL;
1175 }
1176
1177 static const struct component_ops amdgpu_dm_audio_component_bind_ops = {
1178 .bind = amdgpu_dm_audio_component_bind,
1179 .unbind = amdgpu_dm_audio_component_unbind,
1180 };
1181
amdgpu_dm_audio_init(struct amdgpu_device * adev)1182 static int amdgpu_dm_audio_init(struct amdgpu_device *adev)
1183 {
1184 int i, ret;
1185
1186 if (!amdgpu_audio)
1187 return 0;
1188
1189 adev->mode_info.audio.enabled = true;
1190
1191 adev->mode_info.audio.num_pins = adev->dm.dc->res_pool->audio_count;
1192
1193 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1194 adev->mode_info.audio.pin[i].channels = -1;
1195 adev->mode_info.audio.pin[i].rate = -1;
1196 adev->mode_info.audio.pin[i].bits_per_sample = -1;
1197 adev->mode_info.audio.pin[i].status_bits = 0;
1198 adev->mode_info.audio.pin[i].category_code = 0;
1199 adev->mode_info.audio.pin[i].connected = false;
1200 adev->mode_info.audio.pin[i].id =
1201 adev->dm.dc->res_pool->audios[i]->inst;
1202 adev->mode_info.audio.pin[i].offset = 0;
1203 }
1204
1205 ret = component_add(adev->dev, &amdgpu_dm_audio_component_bind_ops);
1206 if (ret < 0)
1207 return ret;
1208
1209 adev->dm.audio_registered = true;
1210
1211 return 0;
1212 }
1213
amdgpu_dm_audio_fini(struct amdgpu_device * adev)1214 static void amdgpu_dm_audio_fini(struct amdgpu_device *adev)
1215 {
1216 if (!amdgpu_audio)
1217 return;
1218
1219 if (!adev->mode_info.audio.enabled)
1220 return;
1221
1222 if (adev->dm.audio_registered) {
1223 component_del(adev->dev, &amdgpu_dm_audio_component_bind_ops);
1224 adev->dm.audio_registered = false;
1225 }
1226
1227 /* TODO: Disable audio? */
1228
1229 adev->mode_info.audio.enabled = false;
1230 }
1231
amdgpu_dm_audio_eld_notify(struct amdgpu_device * adev,int pin)1232 static void amdgpu_dm_audio_eld_notify(struct amdgpu_device *adev, int pin)
1233 {
1234 struct drm_audio_component *acomp = adev->dm.audio_component;
1235
1236 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify) {
1237 drm_dbg_kms(adev_to_drm(adev), "Notify ELD: %d\n", pin);
1238
1239 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
1240 pin, -1);
1241 }
1242 }
1243
dm_dmub_hw_init(struct amdgpu_device * adev)1244 static int dm_dmub_hw_init(struct amdgpu_device *adev)
1245 {
1246 const struct dmcub_firmware_header_v1_0 *hdr;
1247 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
1248 struct dmub_srv_fb_info *fb_info = adev->dm.dmub_fb_info;
1249 const struct firmware *dmub_fw = adev->dm.dmub_fw;
1250 struct dc *dc = adev->dm.dc;
1251 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu;
1252 struct abm *abm = adev->dm.dc->res_pool->abm;
1253 struct dc_context *ctx = adev->dm.dc->ctx;
1254 struct dmub_srv_hw_params hw_params;
1255 enum dmub_status status;
1256 const unsigned char *fw_inst_const, *fw_bss_data;
1257 u32 i, fw_inst_const_size, fw_bss_data_size;
1258 bool has_hw_support;
1259
1260 if (!dmub_srv)
1261 /* DMUB isn't supported on the ASIC. */
1262 return 0;
1263
1264 if (!fb_info) {
1265 drm_err(adev_to_drm(adev), "No framebuffer info for DMUB service.\n");
1266 return -EINVAL;
1267 }
1268
1269 if (!dmub_fw) {
1270 /* Firmware required for DMUB support. */
1271 drm_err(adev_to_drm(adev), "No firmware provided for DMUB.\n");
1272 return -EINVAL;
1273 }
1274
1275 /* initialize register offsets for ASICs with runtime initialization available */
1276 if (dmub_srv->hw_funcs.init_reg_offsets)
1277 dmub_srv->hw_funcs.init_reg_offsets(dmub_srv, ctx);
1278
1279 status = dmub_srv_has_hw_support(dmub_srv, &has_hw_support);
1280 if (status != DMUB_STATUS_OK) {
1281 drm_err(adev_to_drm(adev), "Error checking HW support for DMUB: %d\n", status);
1282 return -EINVAL;
1283 }
1284
1285 if (!has_hw_support) {
1286 drm_info(adev_to_drm(adev), "DMUB unsupported on ASIC\n");
1287 return 0;
1288 }
1289
1290 /* Reset DMCUB if it was previously running - before we overwrite its memory. */
1291 status = dmub_srv_hw_reset(dmub_srv);
1292 if (status != DMUB_STATUS_OK)
1293 drm_warn(adev_to_drm(adev), "Error resetting DMUB HW: %d\n", status);
1294
1295 hdr = (const struct dmcub_firmware_header_v1_0 *)dmub_fw->data;
1296
1297 fw_inst_const = dmub_fw->data +
1298 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1299 PSP_HEADER_BYTES_256;
1300
1301 fw_bss_data = dmub_fw->data +
1302 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1303 le32_to_cpu(hdr->inst_const_bytes);
1304
1305 /* Copy firmware and bios info into FB memory. */
1306 fw_inst_const_size = adev->dm.fw_inst_size;
1307
1308 fw_bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
1309
1310 /* if adev->firmware.load_type == AMDGPU_FW_LOAD_PSP,
1311 * amdgpu_ucode_init_single_fw will load dmub firmware
1312 * fw_inst_const part to cw0; otherwise, the firmware back door load
1313 * will be done by dm_dmub_hw_init
1314 */
1315 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1316 memcpy(fb_info->fb[DMUB_WINDOW_0_INST_CONST].cpu_addr, fw_inst_const,
1317 fw_inst_const_size);
1318 }
1319
1320 if (fw_bss_data_size)
1321 memcpy(fb_info->fb[DMUB_WINDOW_2_BSS_DATA].cpu_addr,
1322 fw_bss_data, fw_bss_data_size);
1323
1324 /* Copy firmware bios info into FB memory. */
1325 memcpy(fb_info->fb[DMUB_WINDOW_3_VBIOS].cpu_addr, adev->bios,
1326 adev->bios_size);
1327
1328 /* Reset regions that need to be reset. */
1329 memset(fb_info->fb[DMUB_WINDOW_4_MAILBOX].cpu_addr, 0,
1330 fb_info->fb[DMUB_WINDOW_4_MAILBOX].size);
1331
1332 memset(fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].cpu_addr, 0,
1333 fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].size);
1334
1335 memset(fb_info->fb[DMUB_WINDOW_6_FW_STATE].cpu_addr, 0,
1336 fb_info->fb[DMUB_WINDOW_6_FW_STATE].size);
1337
1338 memset(fb_info->fb[DMUB_WINDOW_SHARED_STATE].cpu_addr, 0,
1339 fb_info->fb[DMUB_WINDOW_SHARED_STATE].size);
1340
1341 /* Initialize hardware. */
1342 memset(&hw_params, 0, sizeof(hw_params));
1343 hw_params.soc_fb_info.fb_base = adev->gmc.fb_start;
1344 hw_params.soc_fb_info.fb_offset = adev->vm_manager.vram_base_offset;
1345
1346 /* backdoor load firmware and trigger dmub running */
1347 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
1348 hw_params.load_inst_const = true;
1349
1350 if (dmcu)
1351 hw_params.psp_version = dmcu->psp_version;
1352
1353 for (i = 0; i < fb_info->num_fb; ++i)
1354 hw_params.fb[i] = &fb_info->fb[i];
1355
1356 /* Enable usb4 dpia in the FW APU */
1357 if (dc->caps.is_apu &&
1358 dc->res_pool->usb4_dpia_count != 0 &&
1359 !dc->debug.dpia_debug.bits.disable_dpia) {
1360 hw_params.dpia_supported = true;
1361 hw_params.disable_dpia = dc->debug.dpia_debug.bits.disable_dpia;
1362 hw_params.dpia_hpd_int_enable_supported = false;
1363 hw_params.enable_non_transparent_setconfig = dc->config.consolidated_dpia_dp_lt;
1364 hw_params.disable_dpia_bw_allocation = !dc->config.usb4_bw_alloc_support;
1365 }
1366
1367 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1368 case IP_VERSION(3, 5, 0):
1369 case IP_VERSION(3, 5, 1):
1370 case IP_VERSION(3, 6, 0):
1371 hw_params.ips_sequential_ono = adev->external_rev_id > 0x10;
1372 hw_params.lower_hbr3_phy_ssc = true;
1373 break;
1374 default:
1375 break;
1376 }
1377
1378 status = dmub_srv_hw_init(dmub_srv, &hw_params);
1379 if (status != DMUB_STATUS_OK) {
1380 drm_err(adev_to_drm(adev), "Error initializing DMUB HW: %d\n", status);
1381 return -EINVAL;
1382 }
1383
1384 /* Wait for firmware load to finish. */
1385 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
1386 if (status != DMUB_STATUS_OK)
1387 drm_warn(adev_to_drm(adev), "Wait for DMUB auto-load failed: %d\n", status);
1388
1389 /* Init DMCU and ABM if available. */
1390 if (dmcu && abm) {
1391 dmcu->funcs->dmcu_init(dmcu);
1392 abm->dmcu_is_running = dmcu->funcs->is_dmcu_initialized(dmcu);
1393 }
1394
1395 if (!adev->dm.dc->ctx->dmub_srv)
1396 adev->dm.dc->ctx->dmub_srv = dc_dmub_srv_create(adev->dm.dc, dmub_srv);
1397 if (!adev->dm.dc->ctx->dmub_srv) {
1398 drm_err(adev_to_drm(adev), "Couldn't allocate DC DMUB server!\n");
1399 return -ENOMEM;
1400 }
1401
1402 drm_info(adev_to_drm(adev), "DMUB hardware initialized: version=0x%08X\n",
1403 adev->dm.dmcub_fw_version);
1404
1405 /* Keeping sanity checks off if
1406 * DCN31 >= 4.0.59.0
1407 * DCN314 >= 8.0.16.0
1408 * Otherwise, turn on sanity checks
1409 */
1410 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1411 case IP_VERSION(3, 1, 2):
1412 case IP_VERSION(3, 1, 3):
1413 if (adev->dm.dmcub_fw_version &&
1414 adev->dm.dmcub_fw_version >= DMUB_FW_VERSION(4, 0, 0) &&
1415 adev->dm.dmcub_fw_version < DMUB_FW_VERSION(4, 0, 59))
1416 adev->dm.dc->debug.sanity_checks = true;
1417 break;
1418 case IP_VERSION(3, 1, 4):
1419 if (adev->dm.dmcub_fw_version &&
1420 adev->dm.dmcub_fw_version >= DMUB_FW_VERSION(4, 0, 0) &&
1421 adev->dm.dmcub_fw_version < DMUB_FW_VERSION(8, 0, 16))
1422 adev->dm.dc->debug.sanity_checks = true;
1423 break;
1424 default:
1425 break;
1426 }
1427
1428 return 0;
1429 }
1430
dm_dmub_hw_resume(struct amdgpu_device * adev)1431 static void dm_dmub_hw_resume(struct amdgpu_device *adev)
1432 {
1433 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
1434 enum dmub_status status;
1435 bool init;
1436 int r;
1437
1438 if (!dmub_srv) {
1439 /* DMUB isn't supported on the ASIC. */
1440 return;
1441 }
1442
1443 status = dmub_srv_is_hw_init(dmub_srv, &init);
1444 if (status != DMUB_STATUS_OK)
1445 drm_warn(adev_to_drm(adev), "DMUB hardware init check failed: %d\n", status);
1446
1447 if (status == DMUB_STATUS_OK && init) {
1448 /* Wait for firmware load to finish. */
1449 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
1450 if (status != DMUB_STATUS_OK)
1451 drm_warn(adev_to_drm(adev), "Wait for DMUB auto-load failed: %d\n", status);
1452 } else {
1453 /* Perform the full hardware initialization. */
1454 r = dm_dmub_hw_init(adev);
1455 if (r)
1456 drm_err(adev_to_drm(adev), "DMUB interface failed to initialize: status=%d\n", r);
1457 }
1458 }
1459
mmhub_read_system_context(struct amdgpu_device * adev,struct dc_phy_addr_space_config * pa_config)1460 static void mmhub_read_system_context(struct amdgpu_device *adev, struct dc_phy_addr_space_config *pa_config)
1461 {
1462 u64 pt_base;
1463 u32 logical_addr_low;
1464 u32 logical_addr_high;
1465 u32 agp_base, agp_bot, agp_top;
1466 PHYSICAL_ADDRESS_LOC page_table_start, page_table_end, page_table_base;
1467
1468 memset(pa_config, 0, sizeof(*pa_config));
1469
1470 agp_base = 0;
1471 agp_bot = adev->gmc.agp_start >> 24;
1472 agp_top = adev->gmc.agp_end >> 24;
1473
1474 /* AGP aperture is disabled */
1475 if (agp_bot > agp_top) {
1476 logical_addr_low = adev->gmc.fb_start >> 18;
1477 if (adev->apu_flags & (AMD_APU_IS_RAVEN2 |
1478 AMD_APU_IS_RENOIR |
1479 AMD_APU_IS_GREEN_SARDINE))
1480 /*
1481 * Raven2 has a HW issue that it is unable to use the vram which
1482 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the
1483 * workaround that increase system aperture high address (add 1)
1484 * to get rid of the VM fault and hardware hang.
1485 */
1486 logical_addr_high = (adev->gmc.fb_end >> 18) + 0x1;
1487 else
1488 logical_addr_high = adev->gmc.fb_end >> 18;
1489 } else {
1490 logical_addr_low = min(adev->gmc.fb_start, adev->gmc.agp_start) >> 18;
1491 if (adev->apu_flags & (AMD_APU_IS_RAVEN2 |
1492 AMD_APU_IS_RENOIR |
1493 AMD_APU_IS_GREEN_SARDINE))
1494 /*
1495 * Raven2 has a HW issue that it is unable to use the vram which
1496 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the
1497 * workaround that increase system aperture high address (add 1)
1498 * to get rid of the VM fault and hardware hang.
1499 */
1500 logical_addr_high = max((adev->gmc.fb_end >> 18) + 0x1, adev->gmc.agp_end >> 18);
1501 else
1502 logical_addr_high = max(adev->gmc.fb_end, adev->gmc.agp_end) >> 18;
1503 }
1504
1505 pt_base = amdgpu_gmc_pd_addr(adev->gart.bo);
1506
1507 page_table_start.high_part = upper_32_bits(adev->gmc.gart_start >>
1508 AMDGPU_GPU_PAGE_SHIFT);
1509 page_table_start.low_part = lower_32_bits(adev->gmc.gart_start >>
1510 AMDGPU_GPU_PAGE_SHIFT);
1511 page_table_end.high_part = upper_32_bits(adev->gmc.gart_end >>
1512 AMDGPU_GPU_PAGE_SHIFT);
1513 page_table_end.low_part = lower_32_bits(adev->gmc.gart_end >>
1514 AMDGPU_GPU_PAGE_SHIFT);
1515 page_table_base.high_part = upper_32_bits(pt_base);
1516 page_table_base.low_part = lower_32_bits(pt_base);
1517
1518 pa_config->system_aperture.start_addr = (uint64_t)logical_addr_low << 18;
1519 pa_config->system_aperture.end_addr = (uint64_t)logical_addr_high << 18;
1520
1521 pa_config->system_aperture.agp_base = (uint64_t)agp_base << 24;
1522 pa_config->system_aperture.agp_bot = (uint64_t)agp_bot << 24;
1523 pa_config->system_aperture.agp_top = (uint64_t)agp_top << 24;
1524
1525 pa_config->system_aperture.fb_base = adev->gmc.fb_start;
1526 pa_config->system_aperture.fb_offset = adev->vm_manager.vram_base_offset;
1527 pa_config->system_aperture.fb_top = adev->gmc.fb_end;
1528
1529 pa_config->gart_config.page_table_start_addr = page_table_start.quad_part << 12;
1530 pa_config->gart_config.page_table_end_addr = page_table_end.quad_part << 12;
1531 pa_config->gart_config.page_table_base_addr = page_table_base.quad_part;
1532
1533 pa_config->is_hvm_enabled = adev->mode_info.gpu_vm_support;
1534
1535 }
1536
force_connector_state(struct amdgpu_dm_connector * aconnector,enum drm_connector_force force_state)1537 static void force_connector_state(
1538 struct amdgpu_dm_connector *aconnector,
1539 enum drm_connector_force force_state)
1540 {
1541 struct drm_connector *connector = &aconnector->base;
1542
1543 mutex_lock(&connector->dev->mode_config.mutex);
1544 aconnector->base.force = force_state;
1545 mutex_unlock(&connector->dev->mode_config.mutex);
1546
1547 mutex_lock(&aconnector->hpd_lock);
1548 drm_kms_helper_connector_hotplug_event(connector);
1549 mutex_unlock(&aconnector->hpd_lock);
1550 }
1551
dm_handle_hpd_rx_offload_work(struct work_struct * work)1552 static void dm_handle_hpd_rx_offload_work(struct work_struct *work)
1553 {
1554 struct hpd_rx_irq_offload_work *offload_work;
1555 struct amdgpu_dm_connector *aconnector;
1556 struct dc_link *dc_link;
1557 struct amdgpu_device *adev;
1558 enum dc_connection_type new_connection_type = dc_connection_none;
1559 unsigned long flags;
1560 union test_response test_response;
1561
1562 memset(&test_response, 0, sizeof(test_response));
1563
1564 offload_work = container_of(work, struct hpd_rx_irq_offload_work, work);
1565 aconnector = offload_work->offload_wq->aconnector;
1566 adev = offload_work->adev;
1567
1568 if (!aconnector) {
1569 drm_err(adev_to_drm(adev), "Can't retrieve aconnector in hpd_rx_irq_offload_work");
1570 goto skip;
1571 }
1572
1573 dc_link = aconnector->dc_link;
1574
1575 mutex_lock(&aconnector->hpd_lock);
1576 if (!dc_link_detect_connection_type(dc_link, &new_connection_type))
1577 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
1578 mutex_unlock(&aconnector->hpd_lock);
1579
1580 if (new_connection_type == dc_connection_none)
1581 goto skip;
1582
1583 if (amdgpu_in_reset(adev))
1584 goto skip;
1585
1586 if (offload_work->data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY ||
1587 offload_work->data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) {
1588 dm_handle_mst_sideband_msg_ready_event(&aconnector->mst_mgr, DOWN_OR_UP_MSG_RDY_EVENT);
1589 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags);
1590 offload_work->offload_wq->is_handling_mst_msg_rdy_event = false;
1591 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags);
1592 goto skip;
1593 }
1594
1595 mutex_lock(&adev->dm.dc_lock);
1596 if (offload_work->data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
1597 dc_link_dp_handle_automated_test(dc_link);
1598
1599 if (aconnector->timing_changed) {
1600 /* force connector disconnect and reconnect */
1601 force_connector_state(aconnector, DRM_FORCE_OFF);
1602 msleep(100);
1603 force_connector_state(aconnector, DRM_FORCE_UNSPECIFIED);
1604 }
1605
1606 test_response.bits.ACK = 1;
1607
1608 core_link_write_dpcd(
1609 dc_link,
1610 DP_TEST_RESPONSE,
1611 &test_response.raw,
1612 sizeof(test_response));
1613 } else if ((dc_link->connector_signal != SIGNAL_TYPE_EDP) &&
1614 dc_link_check_link_loss_status(dc_link, &offload_work->data) &&
1615 dc_link_dp_allow_hpd_rx_irq(dc_link)) {
1616 /* offload_work->data is from handle_hpd_rx_irq->
1617 * schedule_hpd_rx_offload_work.this is defer handle
1618 * for hpd short pulse. upon here, link status may be
1619 * changed, need get latest link status from dpcd
1620 * registers. if link status is good, skip run link
1621 * training again.
1622 */
1623 union hpd_irq_data irq_data;
1624
1625 memset(&irq_data, 0, sizeof(irq_data));
1626
1627 /* before dc_link_dp_handle_link_loss, allow new link lost handle
1628 * request be added to work queue if link lost at end of dc_link_
1629 * dp_handle_link_loss
1630 */
1631 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags);
1632 offload_work->offload_wq->is_handling_link_loss = false;
1633 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags);
1634
1635 if ((dc_link_dp_read_hpd_rx_irq_data(dc_link, &irq_data) == DC_OK) &&
1636 dc_link_check_link_loss_status(dc_link, &irq_data))
1637 dc_link_dp_handle_link_loss(dc_link);
1638 }
1639 mutex_unlock(&adev->dm.dc_lock);
1640
1641 skip:
1642 kfree(offload_work);
1643
1644 }
1645
hpd_rx_irq_create_workqueue(struct amdgpu_device * adev)1646 static struct hpd_rx_irq_offload_work_queue *hpd_rx_irq_create_workqueue(struct amdgpu_device *adev)
1647 {
1648 struct dc *dc = adev->dm.dc;
1649 int max_caps = dc->caps.max_links;
1650 int i = 0;
1651 struct hpd_rx_irq_offload_work_queue *hpd_rx_offload_wq = NULL;
1652
1653 hpd_rx_offload_wq = kzalloc_objs(*hpd_rx_offload_wq, max_caps);
1654
1655 if (!hpd_rx_offload_wq)
1656 return NULL;
1657
1658
1659 for (i = 0; i < max_caps; i++) {
1660 hpd_rx_offload_wq[i].wq =
1661 create_singlethread_workqueue("amdgpu_dm_hpd_rx_offload_wq");
1662
1663 if (hpd_rx_offload_wq[i].wq == NULL) {
1664 drm_err(adev_to_drm(adev), "create amdgpu_dm_hpd_rx_offload_wq fail!");
1665 goto out_err;
1666 }
1667
1668 spin_lock_init(&hpd_rx_offload_wq[i].offload_lock);
1669 }
1670
1671 return hpd_rx_offload_wq;
1672
1673 out_err:
1674 for (i = 0; i < max_caps; i++) {
1675 if (hpd_rx_offload_wq[i].wq)
1676 destroy_workqueue(hpd_rx_offload_wq[i].wq);
1677 }
1678 kfree(hpd_rx_offload_wq);
1679 return NULL;
1680 }
1681
1682 struct amdgpu_stutter_quirk {
1683 u16 chip_vendor;
1684 u16 chip_device;
1685 u16 subsys_vendor;
1686 u16 subsys_device;
1687 u8 revision;
1688 };
1689
1690 static const struct amdgpu_stutter_quirk amdgpu_stutter_quirk_list[] = {
1691 /* https://bugzilla.kernel.org/show_bug.cgi?id=214417 */
1692 { 0x1002, 0x15dd, 0x1002, 0x15dd, 0xc8 },
1693 { 0, 0, 0, 0, 0 },
1694 };
1695
dm_should_disable_stutter(struct pci_dev * pdev)1696 static bool dm_should_disable_stutter(struct pci_dev *pdev)
1697 {
1698 const struct amdgpu_stutter_quirk *p = amdgpu_stutter_quirk_list;
1699
1700 while (p && p->chip_device != 0) {
1701 if (pdev->vendor == p->chip_vendor &&
1702 pdev->device == p->chip_device &&
1703 pdev->subsystem_vendor == p->subsys_vendor &&
1704 pdev->subsystem_device == p->subsys_device &&
1705 pdev->revision == p->revision) {
1706 return true;
1707 }
1708 ++p;
1709 }
1710 return false;
1711 }
1712
1713
1714 void*
dm_allocate_gpu_mem(struct amdgpu_device * adev,enum dc_gpu_mem_alloc_type type,size_t size,long long * addr)1715 dm_allocate_gpu_mem(
1716 struct amdgpu_device *adev,
1717 enum dc_gpu_mem_alloc_type type,
1718 size_t size,
1719 long long *addr)
1720 {
1721 struct dal_allocation *da;
1722 u32 domain = (type == DC_MEM_ALLOC_TYPE_GART) ?
1723 AMDGPU_GEM_DOMAIN_GTT : AMDGPU_GEM_DOMAIN_VRAM;
1724 int ret;
1725
1726 da = kzalloc_obj(struct dal_allocation);
1727 if (!da)
1728 return NULL;
1729
1730 ret = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE,
1731 domain, &da->bo,
1732 &da->gpu_addr, &da->cpu_ptr);
1733
1734 *addr = da->gpu_addr;
1735
1736 if (ret) {
1737 kfree(da);
1738 return NULL;
1739 }
1740
1741 /* add da to list in dm */
1742 list_add(&da->list, &adev->dm.da_list);
1743
1744 return da->cpu_ptr;
1745 }
1746
1747 void
dm_free_gpu_mem(struct amdgpu_device * adev,enum dc_gpu_mem_alloc_type type,void * pvMem)1748 dm_free_gpu_mem(
1749 struct amdgpu_device *adev,
1750 enum dc_gpu_mem_alloc_type type,
1751 void *pvMem)
1752 {
1753 struct dal_allocation *da;
1754
1755 /* walk the da list in DM */
1756 list_for_each_entry(da, &adev->dm.da_list, list) {
1757 if (pvMem == da->cpu_ptr) {
1758 amdgpu_bo_free_kernel(&da->bo, &da->gpu_addr, &da->cpu_ptr);
1759 list_del(&da->list);
1760 kfree(da);
1761 break;
1762 }
1763 }
1764
1765 }
1766
1767 static enum dmub_status
dm_dmub_send_vbios_gpint_command(struct amdgpu_device * adev,enum dmub_gpint_command command_code,uint16_t param,uint32_t timeout_us)1768 dm_dmub_send_vbios_gpint_command(struct amdgpu_device *adev,
1769 enum dmub_gpint_command command_code,
1770 uint16_t param,
1771 uint32_t timeout_us)
1772 {
1773 union dmub_gpint_data_register reg, test;
1774 uint32_t i;
1775
1776 /* Assume that VBIOS DMUB is ready to take commands */
1777
1778 reg.bits.status = 1;
1779 reg.bits.command_code = command_code;
1780 reg.bits.param = param;
1781
1782 cgs_write_register(adev->dm.cgs_device, 0x34c0 + 0x01f8, reg.all);
1783
1784 for (i = 0; i < timeout_us; ++i) {
1785 udelay(1);
1786
1787 /* Check if our GPINT got acked */
1788 reg.bits.status = 0;
1789 test = (union dmub_gpint_data_register)
1790 cgs_read_register(adev->dm.cgs_device, 0x34c0 + 0x01f8);
1791
1792 if (test.all == reg.all)
1793 return DMUB_STATUS_OK;
1794 }
1795
1796 return DMUB_STATUS_TIMEOUT;
1797 }
1798
dm_dmub_get_vbios_bounding_box(struct amdgpu_device * adev)1799 static void *dm_dmub_get_vbios_bounding_box(struct amdgpu_device *adev)
1800 {
1801 void *bb;
1802 long long addr;
1803 unsigned int bb_size;
1804 int i = 0;
1805 uint16_t chunk;
1806 enum dmub_gpint_command send_addrs[] = {
1807 DMUB_GPINT__SET_BB_ADDR_WORD0,
1808 DMUB_GPINT__SET_BB_ADDR_WORD1,
1809 DMUB_GPINT__SET_BB_ADDR_WORD2,
1810 DMUB_GPINT__SET_BB_ADDR_WORD3,
1811 };
1812 enum dmub_status ret;
1813
1814 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1815 case IP_VERSION(4, 0, 1):
1816 bb_size = sizeof(struct dml2_soc_bb);
1817 break;
1818 default:
1819 return NULL;
1820 }
1821
1822 bb = dm_allocate_gpu_mem(adev,
1823 DC_MEM_ALLOC_TYPE_GART,
1824 bb_size,
1825 &addr);
1826 if (!bb)
1827 return NULL;
1828
1829 for (i = 0; i < 4; i++) {
1830 /* Extract 16-bit chunk */
1831 chunk = ((uint64_t) addr >> (i * 16)) & 0xFFFF;
1832 /* Send the chunk */
1833 ret = dm_dmub_send_vbios_gpint_command(adev, send_addrs[i], chunk, 30000);
1834 if (ret != DMUB_STATUS_OK)
1835 goto free_bb;
1836 }
1837
1838 /* Now ask DMUB to copy the bb */
1839 ret = dm_dmub_send_vbios_gpint_command(adev, DMUB_GPINT__BB_COPY, 1, 200000);
1840 if (ret != DMUB_STATUS_OK)
1841 goto free_bb;
1842
1843 return bb;
1844
1845 free_bb:
1846 dm_free_gpu_mem(adev, DC_MEM_ALLOC_TYPE_GART, (void *) bb);
1847 return NULL;
1848
1849 }
1850
dm_get_default_ips_mode(struct amdgpu_device * adev)1851 static enum dmub_ips_disable_type dm_get_default_ips_mode(
1852 struct amdgpu_device *adev)
1853 {
1854 enum dmub_ips_disable_type ret = DMUB_IPS_ENABLE;
1855
1856 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1857 case IP_VERSION(3, 5, 0):
1858 case IP_VERSION(3, 6, 0):
1859 case IP_VERSION(3, 5, 1):
1860 ret = DMUB_IPS_RCG_IN_ACTIVE_IPS2_IN_OFF;
1861 break;
1862 default:
1863 /* ASICs older than DCN35 do not have IPSs */
1864 if (amdgpu_ip_version(adev, DCE_HWIP, 0) < IP_VERSION(3, 5, 0))
1865 ret = DMUB_IPS_DISABLE_ALL;
1866 break;
1867 }
1868
1869 return ret;
1870 }
1871
amdgpu_dm_init(struct amdgpu_device * adev)1872 static int amdgpu_dm_init(struct amdgpu_device *adev)
1873 {
1874 struct dc_init_data init_data;
1875 struct dc_callback_init init_params;
1876 int r;
1877
1878 adev->dm.ddev = adev_to_drm(adev);
1879 adev->dm.adev = adev;
1880
1881 /* Zero all the fields */
1882 memset(&init_data, 0, sizeof(init_data));
1883 memset(&init_params, 0, sizeof(init_params));
1884
1885 mutex_init(&adev->dm.dpia_aux_lock);
1886 mutex_init(&adev->dm.dc_lock);
1887 mutex_init(&adev->dm.audio_lock);
1888
1889 if (amdgpu_dm_irq_init(adev)) {
1890 drm_err(adev_to_drm(adev), "failed to initialize DM IRQ support.\n");
1891 goto error;
1892 }
1893
1894 init_data.asic_id.chip_family = adev->family;
1895
1896 init_data.asic_id.pci_revision_id = adev->pdev->revision;
1897 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
1898 init_data.asic_id.chip_id = adev->pdev->device;
1899
1900 init_data.asic_id.vram_width = adev->gmc.vram_width;
1901 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
1902 init_data.asic_id.atombios_base_address =
1903 adev->mode_info.atom_context->bios;
1904
1905 init_data.driver = adev;
1906
1907 /* cgs_device was created in dm_sw_init() */
1908 init_data.cgs_device = adev->dm.cgs_device;
1909
1910 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
1911
1912 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1913 case IP_VERSION(2, 1, 0):
1914 switch (adev->dm.dmcub_fw_version) {
1915 case 0: /* development */
1916 case 0x1: /* linux-firmware.git hash 6d9f399 */
1917 case 0x01000000: /* linux-firmware.git hash 9a0b0f4 */
1918 init_data.flags.disable_dmcu = false;
1919 break;
1920 default:
1921 init_data.flags.disable_dmcu = true;
1922 }
1923 break;
1924 case IP_VERSION(2, 0, 3):
1925 init_data.flags.disable_dmcu = true;
1926 break;
1927 default:
1928 break;
1929 }
1930
1931 /* APU support S/G display by default except:
1932 * ASICs before Carrizo,
1933 * RAVEN1 (Users reported stability issue)
1934 */
1935
1936 if (adev->asic_type < CHIP_CARRIZO) {
1937 init_data.flags.gpu_vm_support = false;
1938 } else if (adev->asic_type == CHIP_RAVEN) {
1939 if (adev->apu_flags & AMD_APU_IS_RAVEN)
1940 init_data.flags.gpu_vm_support = false;
1941 else
1942 init_data.flags.gpu_vm_support = (amdgpu_sg_display != 0);
1943 } else {
1944 if (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(2, 0, 3))
1945 init_data.flags.gpu_vm_support = (amdgpu_sg_display == 1);
1946 else
1947 init_data.flags.gpu_vm_support =
1948 (amdgpu_sg_display != 0) && (adev->flags & AMD_IS_APU);
1949 }
1950
1951 adev->mode_info.gpu_vm_support = init_data.flags.gpu_vm_support;
1952
1953 if (amdgpu_dc_feature_mask & DC_FBC_MASK)
1954 init_data.flags.fbc_support = true;
1955
1956 if (amdgpu_dc_feature_mask & DC_MULTI_MON_PP_MCLK_SWITCH_MASK)
1957 init_data.flags.multi_mon_pp_mclk_switch = true;
1958
1959 if (amdgpu_dc_feature_mask & DC_DISABLE_FRACTIONAL_PWM_MASK)
1960 init_data.flags.disable_fractional_pwm = true;
1961
1962 if (amdgpu_dc_feature_mask & DC_EDP_NO_POWER_SEQUENCING)
1963 init_data.flags.edp_no_power_sequencing = true;
1964
1965 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP1_4A)
1966 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP1_4A = true;
1967 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP2_0)
1968 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP2_0 = true;
1969
1970 init_data.flags.seamless_boot_edp_requested = false;
1971
1972 if (amdgpu_device_seamless_boot_supported(adev)) {
1973 init_data.flags.seamless_boot_edp_requested = true;
1974 init_data.flags.allow_seamless_boot_optimization = true;
1975 drm_dbg(adev->dm.ddev, "Seamless boot requested\n");
1976 }
1977
1978 init_data.flags.enable_mipi_converter_optimization = true;
1979
1980 init_data.dcn_reg_offsets = adev->reg_offset[DCE_HWIP][0];
1981 init_data.nbio_reg_offsets = adev->reg_offset[NBIO_HWIP][0];
1982 init_data.clk_reg_offsets = adev->reg_offset[CLK_HWIP][0];
1983
1984 if (amdgpu_dc_debug_mask & DC_DISABLE_IPS)
1985 init_data.flags.disable_ips = DMUB_IPS_DISABLE_ALL;
1986 else if (amdgpu_dc_debug_mask & DC_DISABLE_IPS_DYNAMIC)
1987 init_data.flags.disable_ips = DMUB_IPS_DISABLE_DYNAMIC;
1988 else if (amdgpu_dc_debug_mask & DC_DISABLE_IPS2_DYNAMIC)
1989 init_data.flags.disable_ips = DMUB_IPS_RCG_IN_ACTIVE_IPS2_IN_OFF;
1990 else if (amdgpu_dc_debug_mask & DC_FORCE_IPS_ENABLE)
1991 init_data.flags.disable_ips = DMUB_IPS_ENABLE;
1992 else
1993 init_data.flags.disable_ips = dm_get_default_ips_mode(adev);
1994
1995 init_data.flags.disable_ips_in_vpb = 0;
1996
1997 /* DCN35 and above supports dynamic DTBCLK switch */
1998 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 5, 0))
1999 init_data.flags.allow_0_dtb_clk = true;
2000
2001 /* Enable DWB for tested platforms only */
2002 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 0, 0))
2003 init_data.num_virtual_links = 1;
2004
2005 retrieve_dmi_info(&adev->dm);
2006 if (adev->dm.edp0_on_dp1_quirk)
2007 init_data.flags.support_edp0_on_dp1 = true;
2008
2009 if (adev->dm.bb_from_dmub)
2010 init_data.bb_from_dmub = adev->dm.bb_from_dmub;
2011 else
2012 init_data.bb_from_dmub = NULL;
2013
2014 /* Display Core create. */
2015 adev->dm.dc = dc_create(&init_data);
2016
2017 if (adev->dm.dc) {
2018 drm_info(adev_to_drm(adev), "Display Core v%s initialized on %s\n", DC_VER,
2019 dce_version_to_string(adev->dm.dc->ctx->dce_version));
2020 } else {
2021 drm_info(adev_to_drm(adev), "Display Core failed to initialize with v%s!\n", DC_VER);
2022 goto error;
2023 }
2024
2025 if (amdgpu_dc_debug_mask & DC_DISABLE_PIPE_SPLIT) {
2026 adev->dm.dc->debug.force_single_disp_pipe_split = false;
2027 adev->dm.dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
2028 }
2029
2030 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
2031 adev->dm.dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
2032 if (dm_should_disable_stutter(adev->pdev))
2033 adev->dm.dc->debug.disable_stutter = true;
2034
2035 if (amdgpu_dc_debug_mask & DC_DISABLE_STUTTER)
2036 adev->dm.dc->debug.disable_stutter = true;
2037
2038 if (amdgpu_dc_debug_mask & DC_DISABLE_DSC)
2039 adev->dm.dc->debug.disable_dsc = true;
2040
2041 if (amdgpu_dc_debug_mask & DC_DISABLE_CLOCK_GATING)
2042 adev->dm.dc->debug.disable_clock_gate = true;
2043
2044 if (amdgpu_dc_debug_mask & DC_FORCE_SUBVP_MCLK_SWITCH)
2045 adev->dm.dc->debug.force_subvp_mclk_switch = true;
2046
2047 if (amdgpu_dc_debug_mask & DC_DISABLE_SUBVP_FAMS) {
2048 adev->dm.dc->debug.force_disable_subvp = true;
2049 adev->dm.dc->debug.fams2_config.bits.enable = false;
2050 }
2051
2052 if (amdgpu_dc_debug_mask & DC_ENABLE_DML2) {
2053 adev->dm.dc->debug.using_dml2 = true;
2054 adev->dm.dc->debug.using_dml21 = true;
2055 }
2056
2057 if (amdgpu_dc_debug_mask & DC_HDCP_LC_FORCE_FW_ENABLE)
2058 adev->dm.dc->debug.hdcp_lc_force_fw_enable = true;
2059
2060 if (amdgpu_dc_debug_mask & DC_HDCP_LC_ENABLE_SW_FALLBACK)
2061 adev->dm.dc->debug.hdcp_lc_enable_sw_fallback = true;
2062
2063 if (amdgpu_dc_debug_mask & DC_SKIP_DETECTION_LT)
2064 adev->dm.dc->debug.skip_detection_link_training = true;
2065
2066 adev->dm.dc->debug.visual_confirm = amdgpu_dc_visual_confirm;
2067
2068 /* TODO: Remove after DP2 receiver gets proper support of Cable ID feature */
2069 adev->dm.dc->debug.ignore_cable_id = true;
2070
2071 if (adev->dm.dc->caps.dp_hdmi21_pcon_support)
2072 drm_info(adev_to_drm(adev), "DP-HDMI FRL PCON supported\n");
2073
2074 r = dm_dmub_hw_init(adev);
2075 if (r) {
2076 drm_err(adev_to_drm(adev), "DMUB interface failed to initialize: status=%d\n", r);
2077 goto error;
2078 }
2079
2080 dc_hardware_init(adev->dm.dc);
2081
2082 adev->dm.hpd_rx_offload_wq = hpd_rx_irq_create_workqueue(adev);
2083 if (!adev->dm.hpd_rx_offload_wq) {
2084 drm_err(adev_to_drm(adev), "failed to create hpd rx offload workqueue.\n");
2085 goto error;
2086 }
2087
2088 if ((adev->flags & AMD_IS_APU) && (adev->asic_type >= CHIP_CARRIZO)) {
2089 struct dc_phy_addr_space_config pa_config;
2090
2091 mmhub_read_system_context(adev, &pa_config);
2092
2093 // Call the DC init_memory func
2094 dc_setup_system_context(adev->dm.dc, &pa_config);
2095 }
2096
2097 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
2098 if (!adev->dm.freesync_module) {
2099 drm_err(adev_to_drm(adev),
2100 "failed to initialize freesync_module.\n");
2101 } else
2102 drm_dbg_driver(adev_to_drm(adev), "freesync_module init done %p.\n",
2103 adev->dm.freesync_module);
2104
2105 amdgpu_dm_init_color_mod();
2106
2107 if (adev->dm.dc->caps.max_links > 0) {
2108 adev->dm.vblank_control_workqueue =
2109 create_singlethread_workqueue("dm_vblank_control_workqueue");
2110 if (!adev->dm.vblank_control_workqueue)
2111 drm_err(adev_to_drm(adev), "failed to initialize vblank_workqueue.\n");
2112 }
2113
2114 if (adev->dm.dc->caps.ips_support &&
2115 adev->dm.dc->config.disable_ips != DMUB_IPS_DISABLE_ALL)
2116 adev->dm.idle_workqueue = idle_create_workqueue(adev);
2117
2118 if (adev->dm.dc->caps.max_links > 0 && adev->family >= AMDGPU_FAMILY_RV) {
2119 adev->dm.hdcp_workqueue = hdcp_create_workqueue(adev, &init_params.cp_psp, adev->dm.dc);
2120
2121 if (!adev->dm.hdcp_workqueue)
2122 drm_err(adev_to_drm(adev), "failed to initialize hdcp_workqueue.\n");
2123 else
2124 drm_dbg_driver(adev_to_drm(adev),
2125 "hdcp_workqueue init done %p.\n",
2126 adev->dm.hdcp_workqueue);
2127
2128 dc_init_callbacks(adev->dm.dc, &init_params);
2129 }
2130 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
2131 init_completion(&adev->dm.dmub_aux_transfer_done);
2132 adev->dm.dmub_notify = kzalloc_obj(struct dmub_notification);
2133 if (!adev->dm.dmub_notify) {
2134 drm_info(adev_to_drm(adev), "fail to allocate adev->dm.dmub_notify");
2135 goto error;
2136 }
2137
2138 adev->dm.delayed_hpd_wq = create_singlethread_workqueue("amdgpu_dm_hpd_wq");
2139 if (!adev->dm.delayed_hpd_wq) {
2140 drm_err(adev_to_drm(adev), "failed to create hpd offload workqueue.\n");
2141 goto error;
2142 }
2143
2144 amdgpu_dm_outbox_init(adev);
2145 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_AUX_REPLY,
2146 dmub_aux_setconfig_callback, false)) {
2147 drm_err(adev_to_drm(adev), "fail to register dmub aux callback");
2148 goto error;
2149 }
2150
2151 for (size_t i = 0; i < ARRAY_SIZE(adev->dm.fused_io); i++)
2152 init_completion(&adev->dm.fused_io[i].replied);
2153
2154 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_FUSED_IO,
2155 dmub_aux_fused_io_callback, false)) {
2156 drm_err(adev_to_drm(adev), "fail to register dmub fused io callback");
2157 goto error;
2158 }
2159 /* Enable outbox notification only after IRQ handlers are registered and DMUB is alive.
2160 * It is expected that DMUB will resend any pending notifications at this point. Note
2161 * that hpd and hpd_irq handler registration are deferred to register_hpd_handlers() to
2162 * align legacy interface initialization sequence. Connection status will be proactivly
2163 * detected once in the amdgpu_dm_initialize_drm_device.
2164 */
2165 dc_enable_dmub_outbox(adev->dm.dc);
2166
2167 /* DPIA trace goes to dmesg logs only if outbox is enabled */
2168 if (amdgpu_dc_debug_mask & DC_ENABLE_DPIA_TRACE)
2169 dc_dmub_srv_enable_dpia_trace(adev->dm.dc);
2170 }
2171
2172 if (amdgpu_dm_initialize_drm_device(adev)) {
2173 drm_err(adev_to_drm(adev),
2174 "failed to initialize sw for display support.\n");
2175 goto error;
2176 }
2177
2178 /* create fake encoders for MST */
2179 dm_dp_create_fake_mst_encoders(adev);
2180
2181 /* TODO: Add_display_info? */
2182
2183 /* TODO use dynamic cursor width */
2184 adev_to_drm(adev)->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
2185 adev_to_drm(adev)->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
2186
2187 if (drm_vblank_init(adev_to_drm(adev), adev->dm.display_indexes_num)) {
2188 drm_err(adev_to_drm(adev),
2189 "failed to initialize vblank for display support.\n");
2190 goto error;
2191 }
2192
2193 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
2194 amdgpu_dm_crtc_secure_display_create_contexts(adev);
2195 if (!adev->dm.secure_display_ctx.crtc_ctx)
2196 drm_err(adev_to_drm(adev), "failed to initialize secure display contexts.\n");
2197
2198 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(4, 0, 1))
2199 adev->dm.secure_display_ctx.support_mul_roi = true;
2200
2201 #endif
2202
2203 drm_dbg_driver(adev_to_drm(adev), "KMS initialized.\n");
2204
2205 return 0;
2206 error:
2207 amdgpu_dm_fini(adev);
2208
2209 return -EINVAL;
2210 }
2211
amdgpu_dm_early_fini(struct amdgpu_ip_block * ip_block)2212 static int amdgpu_dm_early_fini(struct amdgpu_ip_block *ip_block)
2213 {
2214 struct amdgpu_device *adev = ip_block->adev;
2215
2216 amdgpu_dm_audio_fini(adev);
2217
2218 return 0;
2219 }
2220
amdgpu_dm_fini(struct amdgpu_device * adev)2221 static void amdgpu_dm_fini(struct amdgpu_device *adev)
2222 {
2223 int i;
2224
2225 if (adev->dm.vblank_control_workqueue) {
2226 destroy_workqueue(adev->dm.vblank_control_workqueue);
2227 adev->dm.vblank_control_workqueue = NULL;
2228 }
2229
2230 if (adev->dm.idle_workqueue) {
2231 if (adev->dm.idle_workqueue->running) {
2232 adev->dm.idle_workqueue->enable = false;
2233 flush_work(&adev->dm.idle_workqueue->work);
2234 }
2235
2236 kfree(adev->dm.idle_workqueue);
2237 adev->dm.idle_workqueue = NULL;
2238 }
2239
2240 amdgpu_dm_destroy_drm_device(&adev->dm);
2241
2242 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
2243 if (adev->dm.secure_display_ctx.crtc_ctx) {
2244 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2245 if (adev->dm.secure_display_ctx.crtc_ctx[i].crtc) {
2246 flush_work(&adev->dm.secure_display_ctx.crtc_ctx[i].notify_ta_work);
2247 flush_work(&adev->dm.secure_display_ctx.crtc_ctx[i].forward_roi_work);
2248 }
2249 }
2250 kfree(adev->dm.secure_display_ctx.crtc_ctx);
2251 adev->dm.secure_display_ctx.crtc_ctx = NULL;
2252 }
2253 #endif
2254 if (adev->dm.hdcp_workqueue) {
2255 hdcp_destroy(&adev->dev->kobj, adev->dm.hdcp_workqueue);
2256 adev->dm.hdcp_workqueue = NULL;
2257 }
2258
2259 if (adev->dm.dc) {
2260 dc_deinit_callbacks(adev->dm.dc);
2261 dc_dmub_srv_destroy(&adev->dm.dc->ctx->dmub_srv);
2262 if (dc_enable_dmub_notifications(adev->dm.dc)) {
2263 kfree(adev->dm.dmub_notify);
2264 adev->dm.dmub_notify = NULL;
2265 destroy_workqueue(adev->dm.delayed_hpd_wq);
2266 adev->dm.delayed_hpd_wq = NULL;
2267 }
2268 }
2269
2270 if (adev->dm.dmub_bo)
2271 amdgpu_bo_free_kernel(&adev->dm.dmub_bo,
2272 &adev->dm.dmub_bo_gpu_addr,
2273 &adev->dm.dmub_bo_cpu_addr);
2274
2275 if (adev->dm.hpd_rx_offload_wq && adev->dm.dc) {
2276 for (i = 0; i < adev->dm.dc->caps.max_links; i++) {
2277 if (adev->dm.hpd_rx_offload_wq[i].wq) {
2278 destroy_workqueue(adev->dm.hpd_rx_offload_wq[i].wq);
2279 adev->dm.hpd_rx_offload_wq[i].wq = NULL;
2280 }
2281 }
2282
2283 kfree(adev->dm.hpd_rx_offload_wq);
2284 adev->dm.hpd_rx_offload_wq = NULL;
2285 }
2286
2287 /* DC Destroy TODO: Replace destroy DAL */
2288 if (adev->dm.dc)
2289 dc_destroy(&adev->dm.dc);
2290 /*
2291 * TODO: pageflip, vlank interrupt
2292 *
2293 * amdgpu_dm_irq_fini(adev);
2294 */
2295
2296 if (adev->dm.cgs_device) {
2297 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
2298 adev->dm.cgs_device = NULL;
2299 }
2300 if (adev->dm.freesync_module) {
2301 mod_freesync_destroy(adev->dm.freesync_module);
2302 adev->dm.freesync_module = NULL;
2303 }
2304
2305 mutex_destroy(&adev->dm.audio_lock);
2306 mutex_destroy(&adev->dm.dc_lock);
2307 mutex_destroy(&adev->dm.dpia_aux_lock);
2308 }
2309
load_dmcu_fw(struct amdgpu_device * adev)2310 static int load_dmcu_fw(struct amdgpu_device *adev)
2311 {
2312 const char *fw_name_dmcu = NULL;
2313 int r;
2314 const struct dmcu_firmware_header_v1_0 *hdr;
2315
2316 switch (adev->asic_type) {
2317 #if defined(CONFIG_DRM_AMD_DC_SI)
2318 case CHIP_TAHITI:
2319 case CHIP_PITCAIRN:
2320 case CHIP_VERDE:
2321 case CHIP_OLAND:
2322 #endif
2323 case CHIP_BONAIRE:
2324 case CHIP_HAWAII:
2325 case CHIP_KAVERI:
2326 case CHIP_KABINI:
2327 case CHIP_MULLINS:
2328 case CHIP_TONGA:
2329 case CHIP_FIJI:
2330 case CHIP_CARRIZO:
2331 case CHIP_STONEY:
2332 case CHIP_POLARIS11:
2333 case CHIP_POLARIS10:
2334 case CHIP_POLARIS12:
2335 case CHIP_VEGAM:
2336 case CHIP_VEGA10:
2337 case CHIP_VEGA12:
2338 case CHIP_VEGA20:
2339 return 0;
2340 case CHIP_NAVI12:
2341 fw_name_dmcu = FIRMWARE_NAVI12_DMCU;
2342 break;
2343 case CHIP_RAVEN:
2344 if (ASICREV_IS_PICASSO(adev->external_rev_id))
2345 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
2346 else if (ASICREV_IS_RAVEN2(adev->external_rev_id))
2347 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
2348 else
2349 return 0;
2350 break;
2351 default:
2352 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
2353 case IP_VERSION(2, 0, 2):
2354 case IP_VERSION(2, 0, 3):
2355 case IP_VERSION(2, 0, 0):
2356 case IP_VERSION(2, 1, 0):
2357 case IP_VERSION(3, 0, 0):
2358 case IP_VERSION(3, 0, 2):
2359 case IP_VERSION(3, 0, 3):
2360 case IP_VERSION(3, 0, 1):
2361 case IP_VERSION(3, 1, 2):
2362 case IP_VERSION(3, 1, 3):
2363 case IP_VERSION(3, 1, 4):
2364 case IP_VERSION(3, 1, 5):
2365 case IP_VERSION(3, 1, 6):
2366 case IP_VERSION(3, 2, 0):
2367 case IP_VERSION(3, 2, 1):
2368 case IP_VERSION(3, 5, 0):
2369 case IP_VERSION(3, 5, 1):
2370 case IP_VERSION(3, 6, 0):
2371 case IP_VERSION(4, 0, 1):
2372 return 0;
2373 default:
2374 break;
2375 }
2376 drm_err(adev_to_drm(adev), "Unsupported ASIC type: 0x%X\n", adev->asic_type);
2377 return -EINVAL;
2378 }
2379
2380 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
2381 drm_dbg_kms(adev_to_drm(adev), "dm: DMCU firmware not supported on direct or SMU loading\n");
2382 return 0;
2383 }
2384
2385 r = amdgpu_ucode_request(adev, &adev->dm.fw_dmcu, AMDGPU_UCODE_REQUIRED,
2386 "%s", fw_name_dmcu);
2387 if (r == -ENODEV) {
2388 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */
2389 drm_dbg_kms(adev_to_drm(adev), "dm: DMCU firmware not found\n");
2390 adev->dm.fw_dmcu = NULL;
2391 return 0;
2392 }
2393 if (r) {
2394 drm_err(adev_to_drm(adev), "amdgpu_dm: Can't validate firmware \"%s\"\n",
2395 fw_name_dmcu);
2396 amdgpu_ucode_release(&adev->dm.fw_dmcu);
2397 return r;
2398 }
2399
2400 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data;
2401 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM;
2402 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu;
2403 adev->firmware.fw_size +=
2404 ALIGN(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
2405
2406 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV;
2407 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu;
2408 adev->firmware.fw_size +=
2409 ALIGN(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
2410
2411 adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version);
2412
2413 drm_dbg_kms(adev_to_drm(adev), "PSP loading DMCU firmware\n");
2414
2415 return 0;
2416 }
2417
amdgpu_dm_dmub_reg_read(void * ctx,uint32_t address)2418 static uint32_t amdgpu_dm_dmub_reg_read(void *ctx, uint32_t address)
2419 {
2420 struct amdgpu_device *adev = ctx;
2421
2422 return dm_read_reg(adev->dm.dc->ctx, address);
2423 }
2424
amdgpu_dm_dmub_reg_write(void * ctx,uint32_t address,uint32_t value)2425 static void amdgpu_dm_dmub_reg_write(void *ctx, uint32_t address,
2426 uint32_t value)
2427 {
2428 struct amdgpu_device *adev = ctx;
2429
2430 return dm_write_reg(adev->dm.dc->ctx, address, value);
2431 }
2432
dm_dmub_sw_init(struct amdgpu_device * adev)2433 static int dm_dmub_sw_init(struct amdgpu_device *adev)
2434 {
2435 struct dmub_srv_create_params create_params;
2436 struct dmub_srv_fw_meta_info_params fw_meta_info_params;
2437 struct dmub_srv_region_params region_params;
2438 struct dmub_srv_region_info region_info;
2439 struct dmub_srv_memory_params memory_params;
2440 struct dmub_fw_meta_info fw_info;
2441 struct dmub_srv_fb_info *fb_info;
2442 struct dmub_srv *dmub_srv;
2443 const struct dmcub_firmware_header_v1_0 *hdr;
2444 enum dmub_asic dmub_asic;
2445 enum dmub_status status;
2446 static enum dmub_window_memory_type window_memory_type[DMUB_WINDOW_TOTAL] = {
2447 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_0_INST_CONST
2448 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_1_STACK
2449 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_2_BSS_DATA
2450 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_3_VBIOS
2451 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_4_MAILBOX
2452 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_5_TRACEBUFF
2453 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_6_FW_STATE
2454 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_7_SCRATCH_MEM
2455 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_IB_MEM
2456 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_SHARED_STATE
2457 };
2458 int r;
2459
2460 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
2461 case IP_VERSION(2, 1, 0):
2462 dmub_asic = DMUB_ASIC_DCN21;
2463 break;
2464 case IP_VERSION(3, 0, 0):
2465 dmub_asic = DMUB_ASIC_DCN30;
2466 break;
2467 case IP_VERSION(3, 0, 1):
2468 dmub_asic = DMUB_ASIC_DCN301;
2469 break;
2470 case IP_VERSION(3, 0, 2):
2471 dmub_asic = DMUB_ASIC_DCN302;
2472 break;
2473 case IP_VERSION(3, 0, 3):
2474 dmub_asic = DMUB_ASIC_DCN303;
2475 break;
2476 case IP_VERSION(3, 1, 2):
2477 case IP_VERSION(3, 1, 3):
2478 dmub_asic = (adev->external_rev_id == YELLOW_CARP_B0) ? DMUB_ASIC_DCN31B : DMUB_ASIC_DCN31;
2479 break;
2480 case IP_VERSION(3, 1, 4):
2481 dmub_asic = DMUB_ASIC_DCN314;
2482 break;
2483 case IP_VERSION(3, 1, 5):
2484 dmub_asic = DMUB_ASIC_DCN315;
2485 break;
2486 case IP_VERSION(3, 1, 6):
2487 dmub_asic = DMUB_ASIC_DCN316;
2488 break;
2489 case IP_VERSION(3, 2, 0):
2490 dmub_asic = DMUB_ASIC_DCN32;
2491 break;
2492 case IP_VERSION(3, 2, 1):
2493 dmub_asic = DMUB_ASIC_DCN321;
2494 break;
2495 case IP_VERSION(3, 5, 0):
2496 case IP_VERSION(3, 5, 1):
2497 dmub_asic = DMUB_ASIC_DCN35;
2498 break;
2499 case IP_VERSION(3, 6, 0):
2500 dmub_asic = DMUB_ASIC_DCN36;
2501 break;
2502 case IP_VERSION(4, 0, 1):
2503 dmub_asic = DMUB_ASIC_DCN401;
2504 break;
2505
2506 default:
2507 /* ASIC doesn't support DMUB. */
2508 return 0;
2509 }
2510
2511 hdr = (const struct dmcub_firmware_header_v1_0 *)adev->dm.dmub_fw->data;
2512 adev->dm.dmcub_fw_version = le32_to_cpu(hdr->header.ucode_version);
2513
2514 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
2515 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].ucode_id =
2516 AMDGPU_UCODE_ID_DMCUB;
2517 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].fw =
2518 adev->dm.dmub_fw;
2519 adev->firmware.fw_size +=
2520 ALIGN(le32_to_cpu(hdr->inst_const_bytes), PAGE_SIZE);
2521
2522 drm_info(adev_to_drm(adev), "Loading DMUB firmware via PSP: version=0x%08X\n",
2523 adev->dm.dmcub_fw_version);
2524 }
2525
2526
2527 adev->dm.dmub_srv = kzalloc_obj(*adev->dm.dmub_srv);
2528 dmub_srv = adev->dm.dmub_srv;
2529
2530 if (!dmub_srv) {
2531 drm_err(adev_to_drm(adev), "Failed to allocate DMUB service!\n");
2532 return -ENOMEM;
2533 }
2534
2535 memset(&create_params, 0, sizeof(create_params));
2536 create_params.user_ctx = adev;
2537 create_params.funcs.reg_read = amdgpu_dm_dmub_reg_read;
2538 create_params.funcs.reg_write = amdgpu_dm_dmub_reg_write;
2539 create_params.asic = dmub_asic;
2540
2541 /* Create the DMUB service. */
2542 status = dmub_srv_create(dmub_srv, &create_params);
2543 if (status != DMUB_STATUS_OK) {
2544 drm_err(adev_to_drm(adev), "Error creating DMUB service: %d\n", status);
2545 return -EINVAL;
2546 }
2547
2548 /* Extract the FW meta info. */
2549 memset(&fw_meta_info_params, 0, sizeof(fw_meta_info_params));
2550
2551 fw_meta_info_params.inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
2552 PSP_HEADER_BYTES_256;
2553 fw_meta_info_params.bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
2554 fw_meta_info_params.fw_inst_const = adev->dm.dmub_fw->data +
2555 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
2556 PSP_HEADER_BYTES_256;
2557 fw_meta_info_params.fw_bss_data = fw_meta_info_params.bss_data_size ? adev->dm.dmub_fw->data +
2558 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
2559 le32_to_cpu(hdr->inst_const_bytes) : NULL;
2560 fw_meta_info_params.custom_psp_footer_size = 0;
2561
2562 status = dmub_srv_get_fw_meta_info_from_raw_fw(&fw_meta_info_params, &fw_info);
2563 if (status != DMUB_STATUS_OK) {
2564 /* Skip returning early, just log the error. */
2565 drm_err(adev_to_drm(adev), "Error getting DMUB FW meta info: %d\n", status);
2566 // return -EINVAL;
2567 }
2568
2569 /* Calculate the size of all the regions for the DMUB service. */
2570 memset(®ion_params, 0, sizeof(region_params));
2571
2572 region_params.inst_const_size = fw_meta_info_params.inst_const_size;
2573 region_params.bss_data_size = fw_meta_info_params.bss_data_size;
2574 region_params.vbios_size = adev->bios_size;
2575 region_params.fw_bss_data = fw_meta_info_params.fw_bss_data;
2576 region_params.fw_inst_const = fw_meta_info_params.fw_inst_const;
2577 region_params.window_memory_type = window_memory_type;
2578 region_params.fw_info = (status == DMUB_STATUS_OK) ? &fw_info : NULL;
2579
2580 status = dmub_srv_calc_region_info(dmub_srv, ®ion_params,
2581 ®ion_info);
2582
2583 if (status != DMUB_STATUS_OK) {
2584 drm_err(adev_to_drm(adev), "Error calculating DMUB region info: %d\n", status);
2585 return -EINVAL;
2586 }
2587
2588 /*
2589 * Allocate a framebuffer based on the total size of all the regions.
2590 * TODO: Move this into GART.
2591 */
2592 r = amdgpu_bo_create_kernel(adev, region_info.fb_size, PAGE_SIZE,
2593 AMDGPU_GEM_DOMAIN_VRAM |
2594 AMDGPU_GEM_DOMAIN_GTT,
2595 &adev->dm.dmub_bo,
2596 &adev->dm.dmub_bo_gpu_addr,
2597 &adev->dm.dmub_bo_cpu_addr);
2598 if (r)
2599 return r;
2600
2601 /* Rebase the regions on the framebuffer address. */
2602 memset(&memory_params, 0, sizeof(memory_params));
2603 memory_params.cpu_fb_addr = adev->dm.dmub_bo_cpu_addr;
2604 memory_params.gpu_fb_addr = adev->dm.dmub_bo_gpu_addr;
2605 memory_params.region_info = ®ion_info;
2606 memory_params.window_memory_type = window_memory_type;
2607
2608 adev->dm.dmub_fb_info = kzalloc_obj(*adev->dm.dmub_fb_info);
2609 fb_info = adev->dm.dmub_fb_info;
2610
2611 if (!fb_info) {
2612 drm_err(adev_to_drm(adev),
2613 "Failed to allocate framebuffer info for DMUB service!\n");
2614 return -ENOMEM;
2615 }
2616
2617 status = dmub_srv_calc_mem_info(dmub_srv, &memory_params, fb_info);
2618 if (status != DMUB_STATUS_OK) {
2619 drm_err(adev_to_drm(adev), "Error calculating DMUB FB info: %d\n", status);
2620 return -EINVAL;
2621 }
2622
2623 adev->dm.bb_from_dmub = dm_dmub_get_vbios_bounding_box(adev);
2624 adev->dm.fw_inst_size = fw_meta_info_params.inst_const_size;
2625
2626 return 0;
2627 }
2628
dm_sw_init(struct amdgpu_ip_block * ip_block)2629 static int dm_sw_init(struct amdgpu_ip_block *ip_block)
2630 {
2631 struct amdgpu_device *adev = ip_block->adev;
2632 int r;
2633
2634 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
2635
2636 if (!adev->dm.cgs_device) {
2637 drm_err(adev_to_drm(adev), "failed to create cgs device.\n");
2638 return -EINVAL;
2639 }
2640
2641 /* Moved from dm init since we need to use allocations for storing bounding box data */
2642 INIT_LIST_HEAD(&adev->dm.da_list);
2643
2644 r = dm_dmub_sw_init(adev);
2645 if (r)
2646 return r;
2647
2648 return load_dmcu_fw(adev);
2649 }
2650
dm_sw_fini(struct amdgpu_ip_block * ip_block)2651 static int dm_sw_fini(struct amdgpu_ip_block *ip_block)
2652 {
2653 struct amdgpu_device *adev = ip_block->adev;
2654 struct dal_allocation *da;
2655
2656 list_for_each_entry(da, &adev->dm.da_list, list) {
2657 if (adev->dm.bb_from_dmub == (void *) da->cpu_ptr) {
2658 amdgpu_bo_free_kernel(&da->bo, &da->gpu_addr, &da->cpu_ptr);
2659 list_del(&da->list);
2660 kfree(da);
2661 adev->dm.bb_from_dmub = NULL;
2662 break;
2663 }
2664 }
2665
2666
2667 kfree(adev->dm.dmub_fb_info);
2668 adev->dm.dmub_fb_info = NULL;
2669
2670 if (adev->dm.dmub_srv) {
2671 dmub_srv_destroy(adev->dm.dmub_srv);
2672 kfree(adev->dm.dmub_srv);
2673 adev->dm.dmub_srv = NULL;
2674 }
2675
2676 amdgpu_ucode_release(&adev->dm.dmub_fw);
2677 amdgpu_ucode_release(&adev->dm.fw_dmcu);
2678
2679 return 0;
2680 }
2681
detect_mst_link_for_all_connectors(struct drm_device * dev)2682 static int detect_mst_link_for_all_connectors(struct drm_device *dev)
2683 {
2684 struct amdgpu_dm_connector *aconnector;
2685 struct drm_connector *connector;
2686 struct drm_connector_list_iter iter;
2687 int ret = 0;
2688
2689 drm_connector_list_iter_begin(dev, &iter);
2690 drm_for_each_connector_iter(connector, &iter) {
2691
2692 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
2693 continue;
2694
2695 aconnector = to_amdgpu_dm_connector(connector);
2696 if (aconnector->dc_link->type == dc_connection_mst_branch &&
2697 aconnector->mst_mgr.aux) {
2698 drm_dbg_kms(dev, "DM_MST: starting TM on aconnector: %p [id: %d]\n",
2699 aconnector,
2700 aconnector->base.base.id);
2701
2702 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
2703 if (ret < 0) {
2704 drm_err(dev, "DM_MST: Failed to start MST\n");
2705 aconnector->dc_link->type =
2706 dc_connection_single;
2707 ret = dm_helpers_dp_mst_stop_top_mgr(aconnector->dc_link->ctx,
2708 aconnector->dc_link);
2709 break;
2710 }
2711 }
2712 }
2713 drm_connector_list_iter_end(&iter);
2714
2715 return ret;
2716 }
2717
dm_late_init(struct amdgpu_ip_block * ip_block)2718 static int dm_late_init(struct amdgpu_ip_block *ip_block)
2719 {
2720 struct amdgpu_device *adev = ip_block->adev;
2721
2722 struct dmcu_iram_parameters params;
2723 unsigned int linear_lut[16];
2724 int i;
2725 struct dmcu *dmcu = NULL;
2726
2727 dmcu = adev->dm.dc->res_pool->dmcu;
2728
2729 for (i = 0; i < 16; i++)
2730 linear_lut[i] = 0xFFFF * i / 15;
2731
2732 params.set = 0;
2733 params.backlight_ramping_override = false;
2734 params.backlight_ramping_start = 0xCCCC;
2735 params.backlight_ramping_reduction = 0xCCCCCCCC;
2736 params.backlight_lut_array_size = 16;
2737 params.backlight_lut_array = linear_lut;
2738
2739 /* Min backlight level after ABM reduction, Don't allow below 1%
2740 * 0xFFFF x 0.01 = 0x28F
2741 */
2742 params.min_abm_backlight = 0x28F;
2743 /* In the case where abm is implemented on dmcub,
2744 * dmcu object will be null.
2745 * ABM 2.4 and up are implemented on dmcub.
2746 */
2747 if (dmcu) {
2748 if (!dmcu_load_iram(dmcu, params))
2749 return -EINVAL;
2750 } else if (adev->dm.dc->ctx->dmub_srv) {
2751 struct dc_link *edp_links[MAX_NUM_EDP];
2752 int edp_num;
2753
2754 dc_get_edp_links(adev->dm.dc, edp_links, &edp_num);
2755 for (i = 0; i < edp_num; i++) {
2756 if (!dmub_init_abm_config(adev->dm.dc->res_pool, params, i))
2757 return -EINVAL;
2758 }
2759 }
2760
2761 return detect_mst_link_for_all_connectors(adev_to_drm(adev));
2762 }
2763
resume_mst_branch_status(struct drm_dp_mst_topology_mgr * mgr)2764 static void resume_mst_branch_status(struct drm_dp_mst_topology_mgr *mgr)
2765 {
2766 u8 buf[UUID_SIZE];
2767 guid_t guid;
2768 int ret;
2769
2770 mutex_lock(&mgr->lock);
2771 if (!mgr->mst_primary)
2772 goto out_fail;
2773
2774 if (drm_dp_read_dpcd_caps(mgr->aux, mgr->dpcd) < 0) {
2775 drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n");
2776 goto out_fail;
2777 }
2778
2779 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2780 DP_MST_EN |
2781 DP_UP_REQ_EN |
2782 DP_UPSTREAM_IS_SRC);
2783 if (ret < 0) {
2784 drm_dbg_kms(mgr->dev, "mst write failed - undocked during suspend?\n");
2785 goto out_fail;
2786 }
2787
2788 /* Some hubs forget their guids after they resume */
2789 ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, buf, sizeof(buf));
2790 if (ret != sizeof(buf)) {
2791 drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n");
2792 goto out_fail;
2793 }
2794
2795 import_guid(&guid, buf);
2796
2797 if (guid_is_null(&guid)) {
2798 guid_gen(&guid);
2799 export_guid(buf, &guid);
2800
2801 ret = drm_dp_dpcd_write(mgr->aux, DP_GUID, buf, sizeof(buf));
2802
2803 if (ret != sizeof(buf)) {
2804 drm_dbg_kms(mgr->dev, "check mstb guid failed - undocked during suspend?\n");
2805 goto out_fail;
2806 }
2807 }
2808
2809 guid_copy(&mgr->mst_primary->guid, &guid);
2810
2811 out_fail:
2812 mutex_unlock(&mgr->lock);
2813 }
2814
hdmi_cec_unset_edid(struct amdgpu_dm_connector * aconnector)2815 void hdmi_cec_unset_edid(struct amdgpu_dm_connector *aconnector)
2816 {
2817 struct cec_notifier *n = aconnector->notifier;
2818
2819 if (!n)
2820 return;
2821
2822 cec_notifier_phys_addr_invalidate(n);
2823 }
2824
hdmi_cec_set_edid(struct amdgpu_dm_connector * aconnector)2825 void hdmi_cec_set_edid(struct amdgpu_dm_connector *aconnector)
2826 {
2827 struct drm_connector *connector = &aconnector->base;
2828 struct cec_notifier *n = aconnector->notifier;
2829
2830 if (!n)
2831 return;
2832
2833 cec_notifier_set_phys_addr(n,
2834 connector->display_info.source_physical_address);
2835 }
2836
s3_handle_hdmi_cec(struct drm_device * ddev,bool suspend)2837 static void s3_handle_hdmi_cec(struct drm_device *ddev, bool suspend)
2838 {
2839 struct amdgpu_dm_connector *aconnector;
2840 struct drm_connector *connector;
2841 struct drm_connector_list_iter conn_iter;
2842
2843 drm_connector_list_iter_begin(ddev, &conn_iter);
2844 drm_for_each_connector_iter(connector, &conn_iter) {
2845 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
2846 continue;
2847
2848 aconnector = to_amdgpu_dm_connector(connector);
2849 if (suspend)
2850 hdmi_cec_unset_edid(aconnector);
2851 else
2852 hdmi_cec_set_edid(aconnector);
2853 }
2854 drm_connector_list_iter_end(&conn_iter);
2855 }
2856
s3_handle_mst(struct drm_device * dev,bool suspend)2857 static void s3_handle_mst(struct drm_device *dev, bool suspend)
2858 {
2859 struct amdgpu_dm_connector *aconnector;
2860 struct drm_connector *connector;
2861 struct drm_connector_list_iter iter;
2862 struct drm_dp_mst_topology_mgr *mgr;
2863
2864 drm_connector_list_iter_begin(dev, &iter);
2865 drm_for_each_connector_iter(connector, &iter) {
2866
2867 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
2868 continue;
2869
2870 aconnector = to_amdgpu_dm_connector(connector);
2871 if (aconnector->dc_link->type != dc_connection_mst_branch ||
2872 aconnector->mst_root)
2873 continue;
2874
2875 mgr = &aconnector->mst_mgr;
2876
2877 if (suspend) {
2878 drm_dp_mst_topology_mgr_suspend(mgr);
2879 } else {
2880 /* if extended timeout is supported in hardware,
2881 * default to LTTPR timeout (3.2ms) first as a W/A for DP link layer
2882 * CTS 4.2.1.1 regression introduced by CTS specs requirement update.
2883 */
2884 try_to_configure_aux_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_LTTPR_TIMEOUT_PERIOD);
2885 if (!dp_is_lttpr_present(aconnector->dc_link))
2886 try_to_configure_aux_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_TIMEOUT_PERIOD);
2887
2888 /* TODO: move resume_mst_branch_status() into drm mst resume again
2889 * once topology probing work is pulled out from mst resume into mst
2890 * resume 2nd step. mst resume 2nd step should be called after old
2891 * state getting restored (i.e. drm_atomic_helper_resume()).
2892 */
2893 resume_mst_branch_status(mgr);
2894 }
2895 }
2896 drm_connector_list_iter_end(&iter);
2897 }
2898
amdgpu_dm_smu_write_watermarks_table(struct amdgpu_device * adev)2899 static int amdgpu_dm_smu_write_watermarks_table(struct amdgpu_device *adev)
2900 {
2901 int ret = 0;
2902
2903 /* This interface is for dGPU Navi1x.Linux dc-pplib interface depends
2904 * on window driver dc implementation.
2905 * For Navi1x, clock settings of dcn watermarks are fixed. the settings
2906 * should be passed to smu during boot up and resume from s3.
2907 * boot up: dc calculate dcn watermark clock settings within dc_create,
2908 * dcn20_resource_construct
2909 * then call pplib functions below to pass the settings to smu:
2910 * smu_set_watermarks_for_clock_ranges
2911 * smu_set_watermarks_table
2912 * navi10_set_watermarks_table
2913 * smu_write_watermarks_table
2914 *
2915 * For Renoir, clock settings of dcn watermark are also fixed values.
2916 * dc has implemented different flow for window driver:
2917 * dc_hardware_init / dc_set_power_state
2918 * dcn10_init_hw
2919 * notify_wm_ranges
2920 * set_wm_ranges
2921 * -- Linux
2922 * smu_set_watermarks_for_clock_ranges
2923 * renoir_set_watermarks_table
2924 * smu_write_watermarks_table
2925 *
2926 * For Linux,
2927 * dc_hardware_init -> amdgpu_dm_init
2928 * dc_set_power_state --> dm_resume
2929 *
2930 * therefore, this function apply to navi10/12/14 but not Renoir
2931 * *
2932 */
2933 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
2934 case IP_VERSION(2, 0, 2):
2935 case IP_VERSION(2, 0, 0):
2936 break;
2937 default:
2938 return 0;
2939 }
2940
2941 ret = amdgpu_dpm_write_watermarks_table(adev);
2942 if (ret) {
2943 drm_err(adev_to_drm(adev), "Failed to update WMTABLE!\n");
2944 return ret;
2945 }
2946
2947 return 0;
2948 }
2949
dm_oem_i2c_hw_init(struct amdgpu_device * adev)2950 static int dm_oem_i2c_hw_init(struct amdgpu_device *adev)
2951 {
2952 struct amdgpu_display_manager *dm = &adev->dm;
2953 struct amdgpu_i2c_adapter *oem_i2c;
2954 struct ddc_service *oem_ddc_service;
2955 int r;
2956
2957 oem_ddc_service = dc_get_oem_i2c_device(adev->dm.dc);
2958 if (oem_ddc_service) {
2959 oem_i2c = create_i2c(oem_ddc_service, true);
2960 if (!oem_i2c) {
2961 drm_info(adev_to_drm(adev), "Failed to create oem i2c adapter data\n");
2962 return -ENOMEM;
2963 }
2964
2965 r = devm_i2c_add_adapter(adev->dev, &oem_i2c->base);
2966 if (r) {
2967 drm_info(adev_to_drm(adev), "Failed to register oem i2c\n");
2968 kfree(oem_i2c);
2969 return r;
2970 }
2971 dm->oem_i2c = oem_i2c;
2972 }
2973
2974 return 0;
2975 }
2976
2977 /**
2978 * dm_hw_init() - Initialize DC device
2979 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
2980 *
2981 * Initialize the &struct amdgpu_display_manager device. This involves calling
2982 * the initializers of each DM component, then populating the struct with them.
2983 *
2984 * Although the function implies hardware initialization, both hardware and
2985 * software are initialized here. Splitting them out to their relevant init
2986 * hooks is a future TODO item.
2987 *
2988 * Some notable things that are initialized here:
2989 *
2990 * - Display Core, both software and hardware
2991 * - DC modules that we need (freesync and color management)
2992 * - DRM software states
2993 * - Interrupt sources and handlers
2994 * - Vblank support
2995 * - Debug FS entries, if enabled
2996 */
dm_hw_init(struct amdgpu_ip_block * ip_block)2997 static int dm_hw_init(struct amdgpu_ip_block *ip_block)
2998 {
2999 struct amdgpu_device *adev = ip_block->adev;
3000 int r;
3001
3002 /* Create DAL display manager */
3003 r = amdgpu_dm_init(adev);
3004 if (r)
3005 return r;
3006 amdgpu_dm_hpd_init(adev);
3007
3008 r = dm_oem_i2c_hw_init(adev);
3009 if (r)
3010 drm_info(adev_to_drm(adev), "Failed to add OEM i2c bus\n");
3011
3012 return 0;
3013 }
3014
3015 /**
3016 * dm_hw_fini() - Teardown DC device
3017 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
3018 *
3019 * Teardown components within &struct amdgpu_display_manager that require
3020 * cleanup. This involves cleaning up the DRM device, DC, and any modules that
3021 * were loaded. Also flush IRQ workqueues and disable them.
3022 */
dm_hw_fini(struct amdgpu_ip_block * ip_block)3023 static int dm_hw_fini(struct amdgpu_ip_block *ip_block)
3024 {
3025 struct amdgpu_device *adev = ip_block->adev;
3026
3027 amdgpu_dm_hpd_fini(adev);
3028
3029 amdgpu_dm_irq_fini(adev);
3030 amdgpu_dm_fini(adev);
3031 return 0;
3032 }
3033
3034
dm_gpureset_toggle_interrupts(struct amdgpu_device * adev,struct dc_state * state,bool enable)3035 static void dm_gpureset_toggle_interrupts(struct amdgpu_device *adev,
3036 struct dc_state *state, bool enable)
3037 {
3038 enum dc_irq_source irq_source;
3039 struct amdgpu_crtc *acrtc;
3040 int rc = -EBUSY;
3041 int i = 0;
3042
3043 for (i = 0; i < state->stream_count; i++) {
3044 acrtc = get_crtc_by_otg_inst(
3045 adev, state->stream_status[i].primary_otg_inst);
3046
3047 if (acrtc && state->stream_status[i].plane_count != 0) {
3048 irq_source = IRQ_TYPE_PFLIP + acrtc->otg_inst;
3049 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
3050 if (rc)
3051 drm_warn(adev_to_drm(adev), "Failed to %s pflip interrupts\n",
3052 enable ? "enable" : "disable");
3053
3054 if (dc_supports_vrr(adev->dm.dc->ctx->dce_version)) {
3055 if (enable) {
3056 if (amdgpu_dm_crtc_vrr_active(
3057 to_dm_crtc_state(acrtc->base.state)))
3058 rc = amdgpu_dm_crtc_set_vupdate_irq(
3059 &acrtc->base, true);
3060 } else
3061 rc = amdgpu_dm_crtc_set_vupdate_irq(
3062 &acrtc->base, false);
3063
3064 if (rc)
3065 drm_warn(adev_to_drm(adev), "Failed to %sable vupdate interrupt\n",
3066 enable ? "en" : "dis");
3067 }
3068
3069 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
3070 /* During gpu-reset we disable and then enable vblank irq, so
3071 * don't use amdgpu_irq_get/put() to avoid refcount change.
3072 */
3073 if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
3074 drm_warn(adev_to_drm(adev), "Failed to %sable vblank interrupt\n", enable ? "en" : "dis");
3075 }
3076 }
3077
3078 }
3079
DEFINE_FREE(state_release,struct dc_state *,if (_T)dc_state_release (_T))3080 DEFINE_FREE(state_release, struct dc_state *, if (_T) dc_state_release(_T))
3081
3082 static enum dc_status amdgpu_dm_commit_zero_streams(struct dc *dc)
3083 {
3084 struct dc_state *context __free(state_release) = NULL;
3085 int i;
3086 struct dc_stream_state *del_streams[MAX_PIPES];
3087 int del_streams_count = 0;
3088 struct dc_commit_streams_params params = {};
3089
3090 memset(del_streams, 0, sizeof(del_streams));
3091
3092 context = dc_state_create_current_copy(dc);
3093 if (context == NULL)
3094 return DC_ERROR_UNEXPECTED;
3095
3096 /* First remove from context all streams */
3097 for (i = 0; i < context->stream_count; i++) {
3098 struct dc_stream_state *stream = context->streams[i];
3099
3100 del_streams[del_streams_count++] = stream;
3101 }
3102
3103 /* Remove all planes for removed streams and then remove the streams */
3104 for (i = 0; i < del_streams_count; i++) {
3105 enum dc_status res;
3106
3107 if (!dc_state_rem_all_planes_for_stream(dc, del_streams[i], context))
3108 return DC_FAIL_DETACH_SURFACES;
3109
3110 res = dc_state_remove_stream(dc, context, del_streams[i]);
3111 if (res != DC_OK)
3112 return res;
3113 }
3114
3115 params.streams = context->streams;
3116 params.stream_count = context->stream_count;
3117
3118 return dc_commit_streams(dc, ¶ms);
3119 }
3120
hpd_rx_irq_work_suspend(struct amdgpu_display_manager * dm)3121 static void hpd_rx_irq_work_suspend(struct amdgpu_display_manager *dm)
3122 {
3123 int i;
3124
3125 if (dm->hpd_rx_offload_wq) {
3126 for (i = 0; i < dm->dc->caps.max_links; i++)
3127 flush_workqueue(dm->hpd_rx_offload_wq[i].wq);
3128 }
3129 }
3130
dm_cache_state(struct amdgpu_device * adev)3131 static int dm_cache_state(struct amdgpu_device *adev)
3132 {
3133 int r;
3134
3135 adev->dm.cached_state = drm_atomic_helper_suspend(adev_to_drm(adev));
3136 if (IS_ERR(adev->dm.cached_state)) {
3137 r = PTR_ERR(adev->dm.cached_state);
3138 adev->dm.cached_state = NULL;
3139 }
3140
3141 return adev->dm.cached_state ? 0 : r;
3142 }
3143
dm_destroy_cached_state(struct amdgpu_device * adev)3144 static void dm_destroy_cached_state(struct amdgpu_device *adev)
3145 {
3146 struct amdgpu_display_manager *dm = &adev->dm;
3147 struct drm_device *ddev = adev_to_drm(adev);
3148 struct dm_plane_state *dm_new_plane_state;
3149 struct drm_plane_state *new_plane_state;
3150 struct dm_crtc_state *dm_new_crtc_state;
3151 struct drm_crtc_state *new_crtc_state;
3152 struct drm_plane *plane;
3153 struct drm_crtc *crtc;
3154 int i;
3155
3156 if (!dm->cached_state)
3157 return;
3158
3159 /* Force mode set in atomic commit */
3160 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
3161 new_crtc_state->active_changed = true;
3162 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
3163 reset_freesync_config_for_crtc(dm_new_crtc_state);
3164 }
3165
3166 /*
3167 * atomic_check is expected to create the dc states. We need to release
3168 * them here, since they were duplicated as part of the suspend
3169 * procedure.
3170 */
3171 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
3172 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
3173 if (dm_new_crtc_state->stream) {
3174 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
3175 dc_stream_release(dm_new_crtc_state->stream);
3176 dm_new_crtc_state->stream = NULL;
3177 }
3178 dm_new_crtc_state->base.color_mgmt_changed = true;
3179 }
3180
3181 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
3182 dm_new_plane_state = to_dm_plane_state(new_plane_state);
3183 if (dm_new_plane_state->dc_state) {
3184 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
3185 dc_plane_state_release(dm_new_plane_state->dc_state);
3186 dm_new_plane_state->dc_state = NULL;
3187 }
3188 }
3189
3190 drm_atomic_helper_resume(ddev, dm->cached_state);
3191
3192 dm->cached_state = NULL;
3193 }
3194
dm_suspend(struct amdgpu_ip_block * ip_block)3195 static int dm_suspend(struct amdgpu_ip_block *ip_block)
3196 {
3197 struct amdgpu_device *adev = ip_block->adev;
3198 struct amdgpu_display_manager *dm = &adev->dm;
3199
3200 if (amdgpu_in_reset(adev)) {
3201 enum dc_status res;
3202
3203 mutex_lock(&dm->dc_lock);
3204
3205 dc_allow_idle_optimizations(adev->dm.dc, false);
3206
3207 dm->cached_dc_state = dc_state_create_copy(dm->dc->current_state);
3208
3209 if (dm->cached_dc_state)
3210 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, false);
3211
3212 res = amdgpu_dm_commit_zero_streams(dm->dc);
3213 if (res != DC_OK) {
3214 drm_err(adev_to_drm(adev), "Failed to commit zero streams: %d\n", res);
3215 return -EINVAL;
3216 }
3217
3218 amdgpu_dm_irq_suspend(adev);
3219
3220 hpd_rx_irq_work_suspend(dm);
3221
3222 return 0;
3223 }
3224
3225 if (!adev->dm.cached_state) {
3226 int r = dm_cache_state(adev);
3227
3228 if (r)
3229 return r;
3230 }
3231
3232 s3_handle_hdmi_cec(adev_to_drm(adev), true);
3233
3234 s3_handle_mst(adev_to_drm(adev), true);
3235
3236 amdgpu_dm_irq_suspend(adev);
3237
3238 hpd_rx_irq_work_suspend(dm);
3239
3240 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
3241
3242 if (dm->dc->caps.ips_support && adev->in_s0ix)
3243 dc_allow_idle_optimizations(dm->dc, true);
3244
3245 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D3);
3246
3247 return 0;
3248 }
3249
3250 struct drm_connector *
amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state * state,struct drm_crtc * crtc)3251 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
3252 struct drm_crtc *crtc)
3253 {
3254 u32 i;
3255 struct drm_connector_state *new_con_state;
3256 struct drm_connector *connector;
3257 struct drm_crtc *crtc_from_state;
3258
3259 for_each_new_connector_in_state(state, connector, new_con_state, i) {
3260 crtc_from_state = new_con_state->crtc;
3261
3262 if (crtc_from_state == crtc)
3263 return connector;
3264 }
3265
3266 return NULL;
3267 }
3268
emulated_link_detect(struct dc_link * link)3269 static void emulated_link_detect(struct dc_link *link)
3270 {
3271 struct dc_sink_init_data sink_init_data = { 0 };
3272 struct display_sink_capability sink_caps = { 0 };
3273 enum dc_edid_status edid_status;
3274 struct dc_context *dc_ctx = link->ctx;
3275 struct drm_device *dev = adev_to_drm(dc_ctx->driver_context);
3276 struct dc_sink *sink = NULL;
3277 struct dc_sink *prev_sink = NULL;
3278
3279 link->type = dc_connection_none;
3280 prev_sink = link->local_sink;
3281
3282 if (prev_sink)
3283 dc_sink_release(prev_sink);
3284
3285 switch (link->connector_signal) {
3286 case SIGNAL_TYPE_HDMI_TYPE_A: {
3287 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
3288 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
3289 break;
3290 }
3291
3292 case SIGNAL_TYPE_DVI_SINGLE_LINK: {
3293 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
3294 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
3295 break;
3296 }
3297
3298 case SIGNAL_TYPE_DVI_DUAL_LINK: {
3299 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
3300 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
3301 break;
3302 }
3303
3304 case SIGNAL_TYPE_LVDS: {
3305 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
3306 sink_caps.signal = SIGNAL_TYPE_LVDS;
3307 break;
3308 }
3309
3310 case SIGNAL_TYPE_EDP: {
3311 sink_caps.transaction_type =
3312 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
3313 sink_caps.signal = SIGNAL_TYPE_EDP;
3314 break;
3315 }
3316
3317 case SIGNAL_TYPE_DISPLAY_PORT: {
3318 sink_caps.transaction_type =
3319 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
3320 sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
3321 break;
3322 }
3323
3324 default:
3325 drm_err(dev, "Invalid connector type! signal:%d\n",
3326 link->connector_signal);
3327 return;
3328 }
3329
3330 sink_init_data.link = link;
3331 sink_init_data.sink_signal = sink_caps.signal;
3332
3333 sink = dc_sink_create(&sink_init_data);
3334 if (!sink) {
3335 drm_err(dev, "Failed to create sink!\n");
3336 return;
3337 }
3338
3339 /* dc_sink_create returns a new reference */
3340 link->local_sink = sink;
3341
3342 edid_status = dm_helpers_read_local_edid(
3343 link->ctx,
3344 link,
3345 sink);
3346
3347 if (edid_status != EDID_OK)
3348 drm_err(dev, "Failed to read EDID\n");
3349
3350 }
3351
dm_gpureset_commit_state(struct dc_state * dc_state,struct amdgpu_display_manager * dm)3352 static void dm_gpureset_commit_state(struct dc_state *dc_state,
3353 struct amdgpu_display_manager *dm)
3354 {
3355 struct {
3356 struct dc_surface_update surface_updates[MAX_SURFACES];
3357 struct dc_plane_info plane_infos[MAX_SURFACES];
3358 struct dc_scaling_info scaling_infos[MAX_SURFACES];
3359 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
3360 struct dc_stream_update stream_update;
3361 } *bundle __free(kfree);
3362 int k, m;
3363
3364 bundle = kzalloc_obj(*bundle);
3365
3366 if (!bundle) {
3367 drm_err(dm->ddev, "Failed to allocate update bundle\n");
3368 return;
3369 }
3370
3371 for (k = 0; k < dc_state->stream_count; k++) {
3372 bundle->stream_update.stream = dc_state->streams[k];
3373
3374 for (m = 0; m < dc_state->stream_status[k].plane_count; m++) {
3375 bundle->surface_updates[m].surface =
3376 dc_state->stream_status[k].plane_states[m];
3377 bundle->surface_updates[m].surface->force_full_update =
3378 true;
3379 }
3380
3381 update_planes_and_stream_adapter(dm->dc,
3382 UPDATE_TYPE_FULL,
3383 dc_state->stream_status[k].plane_count,
3384 dc_state->streams[k],
3385 &bundle->stream_update,
3386 bundle->surface_updates);
3387 }
3388 }
3389
apply_delay_after_dpcd_poweroff(struct amdgpu_device * adev,struct dc_sink * sink)3390 static void apply_delay_after_dpcd_poweroff(struct amdgpu_device *adev,
3391 struct dc_sink *sink)
3392 {
3393 struct dc_panel_patch *ppatch = NULL;
3394
3395 if (!sink)
3396 return;
3397
3398 ppatch = &sink->edid_caps.panel_patch;
3399 if (ppatch->wait_after_dpcd_poweroff_ms) {
3400 msleep(ppatch->wait_after_dpcd_poweroff_ms);
3401 drm_dbg_driver(adev_to_drm(adev),
3402 "%s: adding a %ds delay as w/a for panel\n",
3403 __func__,
3404 ppatch->wait_after_dpcd_poweroff_ms / 1000);
3405 }
3406 }
3407
3408 /**
3409 * amdgpu_dm_dump_links_and_sinks - Debug dump of all DC links and their sinks
3410 * @adev: amdgpu device pointer
3411 *
3412 * Iterates through all DC links and dumps information about local and remote
3413 * (MST) sinks. Should be called after connector detection is complete to see
3414 * the final state of all links.
3415 */
amdgpu_dm_dump_links_and_sinks(struct amdgpu_device * adev)3416 static void amdgpu_dm_dump_links_and_sinks(struct amdgpu_device *adev)
3417 {
3418 struct dc *dc = adev->dm.dc;
3419 struct drm_device *dev = adev_to_drm(adev);
3420 int li;
3421
3422 if (!dc)
3423 return;
3424
3425 for (li = 0; li < dc->link_count; li++) {
3426 struct dc_link *l = dc->links[li];
3427 const char *name = NULL;
3428 int rs;
3429
3430 if (!l)
3431 continue;
3432 if (l->local_sink && l->local_sink->edid_caps.display_name[0])
3433 name = l->local_sink->edid_caps.display_name;
3434 else
3435 name = "n/a";
3436
3437 drm_dbg_kms(dev,
3438 "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",
3439 li,
3440 l->local_sink,
3441 l->type,
3442 l->local_sink ? l->local_sink->sink_signal : SIGNAL_TYPE_NONE,
3443 l->sink_count,
3444 name,
3445 l->dpcd_caps.is_mst_capable,
3446 l->mst_stream_alloc_table.stream_count);
3447
3448 /* Dump remote (MST) sinks if any */
3449 for (rs = 0; rs < l->sink_count; rs++) {
3450 struct dc_sink *rsink = l->remote_sinks[rs];
3451 const char *rname = NULL;
3452
3453 if (!rsink)
3454 continue;
3455 if (rsink->edid_caps.display_name[0])
3456 rname = rsink->edid_caps.display_name;
3457 else
3458 rname = "n/a";
3459 drm_dbg_kms(dev,
3460 " REMOTE_SINK[%d:%d]: sink=%p signal=%d edid_name=%s\n",
3461 li, rs,
3462 rsink,
3463 rsink->sink_signal,
3464 rname);
3465 }
3466 }
3467 }
3468
dm_resume(struct amdgpu_ip_block * ip_block)3469 static int dm_resume(struct amdgpu_ip_block *ip_block)
3470 {
3471 struct amdgpu_device *adev = ip_block->adev;
3472 struct drm_device *ddev = adev_to_drm(adev);
3473 struct amdgpu_display_manager *dm = &adev->dm;
3474 struct amdgpu_dm_connector *aconnector;
3475 struct drm_connector *connector;
3476 struct drm_connector_list_iter iter;
3477 struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state);
3478 enum dc_connection_type new_connection_type = dc_connection_none;
3479 struct dc_state *dc_state;
3480 int i, r, j;
3481 struct dc_commit_streams_params commit_params = {};
3482
3483 if (dm->dc->caps.ips_support) {
3484 if (!amdgpu_in_reset(adev))
3485 mutex_lock(&dm->dc_lock);
3486
3487 /* Need to set POWER_STATE_D0 first or it will not execute
3488 * idle_power_optimizations command to DMUB.
3489 */
3490 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0);
3491 dc_dmub_srv_apply_idle_power_optimizations(dm->dc, false);
3492
3493 if (!amdgpu_in_reset(adev))
3494 mutex_unlock(&dm->dc_lock);
3495 }
3496
3497 if (amdgpu_in_reset(adev)) {
3498 dc_state = dm->cached_dc_state;
3499
3500 /*
3501 * The dc->current_state is backed up into dm->cached_dc_state
3502 * before we commit 0 streams.
3503 *
3504 * DC will clear link encoder assignments on the real state
3505 * but the changes won't propagate over to the copy we made
3506 * before the 0 streams commit.
3507 *
3508 * DC expects that link encoder assignments are *not* valid
3509 * when committing a state, so as a workaround we can copy
3510 * off of the current state.
3511 *
3512 * We lose the previous assignments, but we had already
3513 * commit 0 streams anyway.
3514 */
3515 link_enc_cfg_copy(adev->dm.dc->current_state, dc_state);
3516
3517 r = dm_dmub_hw_init(adev);
3518 if (r) {
3519 drm_err(adev_to_drm(adev), "DMUB interface failed to initialize: status=%d\n", r);
3520 return r;
3521 }
3522
3523 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0);
3524 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
3525
3526 dc_resume(dm->dc);
3527
3528 amdgpu_dm_irq_resume_early(adev);
3529
3530 for (i = 0; i < dc_state->stream_count; i++) {
3531 dc_state->streams[i]->mode_changed = true;
3532 for (j = 0; j < dc_state->stream_status[i].plane_count; j++) {
3533 dc_state->stream_status[i].plane_states[j]->update_flags.raw
3534 = 0xffffffff;
3535 }
3536 }
3537
3538 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
3539 amdgpu_dm_outbox_init(adev);
3540 dc_enable_dmub_outbox(adev->dm.dc);
3541 }
3542
3543 commit_params.streams = dc_state->streams;
3544 commit_params.stream_count = dc_state->stream_count;
3545 dc_exit_ips_for_hw_access(dm->dc);
3546 WARN_ON(!dc_commit_streams(dm->dc, &commit_params));
3547
3548 dm_gpureset_commit_state(dm->cached_dc_state, dm);
3549
3550 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, true);
3551
3552 dc_state_release(dm->cached_dc_state);
3553 dm->cached_dc_state = NULL;
3554
3555 amdgpu_dm_irq_resume_late(adev);
3556
3557 mutex_unlock(&dm->dc_lock);
3558
3559 /* set the backlight after a reset */
3560 for (i = 0; i < dm->num_of_edps; i++) {
3561 if (dm->backlight_dev[i])
3562 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
3563 }
3564
3565 return 0;
3566 }
3567 /* Recreate dc_state - DC invalidates it when setting power state to S3. */
3568 dc_state_release(dm_state->context);
3569 dm_state->context = dc_state_create(dm->dc, NULL);
3570 /* TODO: Remove dc_state->dccg, use dc->dccg directly. */
3571
3572 /* Before powering on DC we need to re-initialize DMUB. */
3573 dm_dmub_hw_resume(adev);
3574
3575 /* Re-enable outbox interrupts for DPIA. */
3576 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
3577 amdgpu_dm_outbox_init(adev);
3578 dc_enable_dmub_outbox(adev->dm.dc);
3579 }
3580
3581 /* power on hardware */
3582 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0);
3583 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
3584
3585 /* program HPD filter */
3586 dc_resume(dm->dc);
3587
3588 /*
3589 * early enable HPD Rx IRQ, should be done before set mode as short
3590 * pulse interrupts are used for MST
3591 */
3592 amdgpu_dm_irq_resume_early(adev);
3593
3594 s3_handle_hdmi_cec(ddev, false);
3595
3596 /* On resume we need to rewrite the MSTM control bits to enable MST*/
3597 s3_handle_mst(ddev, false);
3598
3599 /* Do detection*/
3600 drm_connector_list_iter_begin(ddev, &iter);
3601 drm_for_each_connector_iter(connector, &iter) {
3602 bool ret;
3603
3604 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
3605 continue;
3606
3607 aconnector = to_amdgpu_dm_connector(connector);
3608
3609 if (!aconnector->dc_link)
3610 continue;
3611
3612 /*
3613 * this is the case when traversing through already created end sink
3614 * MST connectors, should be skipped
3615 */
3616 if (aconnector->mst_root)
3617 continue;
3618
3619 /* Skip eDP detection, when there is no sink present */
3620 if (aconnector->dc_link->connector_signal == SIGNAL_TYPE_EDP &&
3621 !aconnector->dc_link->edp_sink_present)
3622 continue;
3623
3624 guard(mutex)(&aconnector->hpd_lock);
3625 if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type))
3626 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
3627
3628 if (aconnector->base.force && new_connection_type == dc_connection_none) {
3629 emulated_link_detect(aconnector->dc_link);
3630 } else {
3631 guard(mutex)(&dm->dc_lock);
3632 dc_exit_ips_for_hw_access(dm->dc);
3633 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_RESUMEFROMS3S4);
3634 if (ret) {
3635 /* w/a delay for certain panels */
3636 apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink);
3637 }
3638 }
3639
3640 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
3641 aconnector->fake_enable = false;
3642
3643 if (aconnector->dc_sink)
3644 dc_sink_release(aconnector->dc_sink);
3645 aconnector->dc_sink = NULL;
3646 amdgpu_dm_update_connector_after_detect(aconnector);
3647 }
3648 drm_connector_list_iter_end(&iter);
3649
3650 dm_destroy_cached_state(adev);
3651
3652 /* Do mst topology probing after resuming cached state*/
3653 drm_connector_list_iter_begin(ddev, &iter);
3654 drm_for_each_connector_iter(connector, &iter) {
3655 bool init = false;
3656
3657 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
3658 continue;
3659
3660 aconnector = to_amdgpu_dm_connector(connector);
3661 if (aconnector->dc_link->type != dc_connection_mst_branch ||
3662 aconnector->mst_root)
3663 continue;
3664
3665 scoped_guard(mutex, &aconnector->mst_mgr.lock) {
3666 init = !aconnector->mst_mgr.mst_primary;
3667 }
3668 if (init)
3669 dm_helpers_dp_mst_start_top_mgr(aconnector->dc_link->ctx,
3670 aconnector->dc_link, false);
3671 else
3672 drm_dp_mst_topology_queue_probe(&aconnector->mst_mgr);
3673 }
3674 drm_connector_list_iter_end(&iter);
3675
3676 /* Debug dump: list all DC links and their associated sinks after detection
3677 * is complete for all connectors. This provides a comprehensive view of the
3678 * final state without repeating the dump for each connector.
3679 */
3680 amdgpu_dm_dump_links_and_sinks(adev);
3681
3682 amdgpu_dm_irq_resume_late(adev);
3683
3684 amdgpu_dm_smu_write_watermarks_table(adev);
3685
3686 drm_kms_helper_hotplug_event(ddev);
3687
3688 return 0;
3689 }
3690
3691 /**
3692 * DOC: DM Lifecycle
3693 *
3694 * DM (and consequently DC) is registered in the amdgpu base driver as a IP
3695 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to
3696 * the base driver's device list to be initialized and torn down accordingly.
3697 *
3698 * The functions to do so are provided as hooks in &struct amd_ip_funcs.
3699 */
3700
3701 static const struct amd_ip_funcs amdgpu_dm_funcs = {
3702 .name = "dm",
3703 .early_init = dm_early_init,
3704 .late_init = dm_late_init,
3705 .sw_init = dm_sw_init,
3706 .sw_fini = dm_sw_fini,
3707 .early_fini = amdgpu_dm_early_fini,
3708 .hw_init = dm_hw_init,
3709 .hw_fini = dm_hw_fini,
3710 .suspend = dm_suspend,
3711 .resume = dm_resume,
3712 .is_idle = dm_is_idle,
3713 .wait_for_idle = dm_wait_for_idle,
3714 .check_soft_reset = dm_check_soft_reset,
3715 .soft_reset = dm_soft_reset,
3716 .set_clockgating_state = dm_set_clockgating_state,
3717 .set_powergating_state = dm_set_powergating_state,
3718 };
3719
3720 const struct amdgpu_ip_block_version dm_ip_block = {
3721 .type = AMD_IP_BLOCK_TYPE_DCE,
3722 .major = 1,
3723 .minor = 0,
3724 .rev = 0,
3725 .funcs = &amdgpu_dm_funcs,
3726 };
3727
3728
3729 /**
3730 * DOC: atomic
3731 *
3732 * *WIP*
3733 */
3734
3735 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
3736 .fb_create = amdgpu_display_user_framebuffer_create,
3737 .get_format_info = amdgpu_dm_plane_get_format_info,
3738 .atomic_check = amdgpu_dm_atomic_check,
3739 .atomic_commit = drm_atomic_helper_commit,
3740 };
3741
3742 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
3743 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail,
3744 .atomic_commit_setup = amdgpu_dm_atomic_setup_commit,
3745 };
3746
update_connector_ext_caps(struct amdgpu_dm_connector * aconnector)3747 static void update_connector_ext_caps(struct amdgpu_dm_connector *aconnector)
3748 {
3749 const struct drm_panel_backlight_quirk *panel_backlight_quirk;
3750 struct amdgpu_dm_backlight_caps *caps;
3751 struct drm_connector *conn_base;
3752 struct amdgpu_device *adev;
3753 struct drm_luminance_range_info *luminance_range;
3754 struct drm_device *drm;
3755
3756 if (aconnector->bl_idx == -1 ||
3757 aconnector->dc_link->connector_signal != SIGNAL_TYPE_EDP)
3758 return;
3759
3760 conn_base = &aconnector->base;
3761 drm = conn_base->dev;
3762 adev = drm_to_adev(drm);
3763
3764 caps = &adev->dm.backlight_caps[aconnector->bl_idx];
3765 caps->ext_caps = &aconnector->dc_link->dpcd_sink_ext_caps;
3766 caps->aux_support = false;
3767
3768 if (caps->ext_caps->bits.oled == 1
3769 /*
3770 * ||
3771 * caps->ext_caps->bits.sdr_aux_backlight_control == 1 ||
3772 * caps->ext_caps->bits.hdr_aux_backlight_control == 1
3773 */)
3774 caps->aux_support = true;
3775
3776 if (amdgpu_backlight == 0)
3777 caps->aux_support = false;
3778 else if (amdgpu_backlight == 1)
3779 caps->aux_support = true;
3780 if (caps->aux_support)
3781 aconnector->dc_link->backlight_control_type = BACKLIGHT_CONTROL_AMD_AUX;
3782
3783 luminance_range = &conn_base->display_info.luminance_range;
3784
3785 if (luminance_range->max_luminance)
3786 caps->aux_max_input_signal = luminance_range->max_luminance;
3787 else
3788 caps->aux_max_input_signal = 512;
3789
3790 if (luminance_range->min_luminance)
3791 caps->aux_min_input_signal = luminance_range->min_luminance;
3792 else
3793 caps->aux_min_input_signal = 1;
3794
3795 panel_backlight_quirk =
3796 drm_get_panel_backlight_quirk(aconnector->drm_edid);
3797 if (!IS_ERR_OR_NULL(panel_backlight_quirk)) {
3798 if (panel_backlight_quirk->min_brightness) {
3799 caps->min_input_signal =
3800 panel_backlight_quirk->min_brightness - 1;
3801 drm_info(drm,
3802 "Applying panel backlight quirk, min_brightness: %d\n",
3803 caps->min_input_signal);
3804 }
3805 if (panel_backlight_quirk->brightness_mask) {
3806 drm_info(drm,
3807 "Applying panel backlight quirk, brightness_mask: 0x%X\n",
3808 panel_backlight_quirk->brightness_mask);
3809 caps->brightness_mask =
3810 panel_backlight_quirk->brightness_mask;
3811 }
3812 }
3813 }
3814
DEFINE_FREE(sink_release,struct dc_sink *,if (_T)dc_sink_release (_T))3815 DEFINE_FREE(sink_release, struct dc_sink *, if (_T) dc_sink_release(_T))
3816
3817 void amdgpu_dm_update_connector_after_detect(
3818 struct amdgpu_dm_connector *aconnector)
3819 {
3820 struct drm_connector *connector = &aconnector->base;
3821 struct dc_sink *sink __free(sink_release) = NULL;
3822 struct drm_device *dev = connector->dev;
3823
3824 /* MST handled by drm_mst framework */
3825 if (aconnector->mst_mgr.mst_state == true)
3826 return;
3827
3828 sink = aconnector->dc_link->local_sink;
3829 if (sink)
3830 dc_sink_retain(sink);
3831
3832 /*
3833 * Edid mgmt connector gets first update only in mode_valid hook and then
3834 * the connector sink is set to either fake or physical sink depends on link status.
3835 * Skip if already done during boot.
3836 */
3837 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
3838 && aconnector->dc_em_sink) {
3839
3840 /*
3841 * For S3 resume with headless use eml_sink to fake stream
3842 * because on resume connector->sink is set to NULL
3843 */
3844 guard(mutex)(&dev->mode_config.mutex);
3845
3846 if (sink) {
3847 if (aconnector->dc_sink) {
3848 amdgpu_dm_update_freesync_caps(connector, NULL);
3849 /*
3850 * retain and release below are used to
3851 * bump up refcount for sink because the link doesn't point
3852 * to it anymore after disconnect, so on next crtc to connector
3853 * reshuffle by UMD we will get into unwanted dc_sink release
3854 */
3855 dc_sink_release(aconnector->dc_sink);
3856 }
3857 aconnector->dc_sink = sink;
3858 dc_sink_retain(aconnector->dc_sink);
3859 amdgpu_dm_update_freesync_caps(connector,
3860 aconnector->drm_edid);
3861 } else {
3862 amdgpu_dm_update_freesync_caps(connector, NULL);
3863 if (!aconnector->dc_sink) {
3864 aconnector->dc_sink = aconnector->dc_em_sink;
3865 dc_sink_retain(aconnector->dc_sink);
3866 }
3867 }
3868
3869 return;
3870 }
3871
3872 /*
3873 * TODO: temporary guard to look for proper fix
3874 * if this sink is MST sink, we should not do anything
3875 */
3876 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
3877 return;
3878
3879 if (aconnector->dc_sink == sink) {
3880 /*
3881 * We got a DP short pulse (Link Loss, DP CTS, etc...).
3882 * Do nothing!!
3883 */
3884 drm_dbg_kms(dev, "DCHPD: connector_id=%d: dc_sink didn't change.\n",
3885 aconnector->connector_id);
3886 return;
3887 }
3888
3889 drm_dbg_kms(dev, "DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
3890 aconnector->connector_id, aconnector->dc_sink, sink);
3891
3892 /* When polling, DRM has already locked the mutex for us. */
3893 if (!drm_kms_helper_is_poll_worker())
3894 mutex_lock(&dev->mode_config.mutex);
3895
3896 /*
3897 * 1. Update status of the drm connector
3898 * 2. Send an event and let userspace tell us what to do
3899 */
3900 if (sink) {
3901 /*
3902 * TODO: check if we still need the S3 mode update workaround.
3903 * If yes, put it here.
3904 */
3905 if (aconnector->dc_sink) {
3906 amdgpu_dm_update_freesync_caps(connector, NULL);
3907 dc_sink_release(aconnector->dc_sink);
3908 }
3909
3910 aconnector->dc_sink = sink;
3911 dc_sink_retain(aconnector->dc_sink);
3912 drm_edid_free(aconnector->drm_edid);
3913 aconnector->drm_edid = NULL;
3914 if (sink->dc_edid.length == 0) {
3915 hdmi_cec_unset_edid(aconnector);
3916 if (aconnector->dc_link->aux_mode) {
3917 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
3918 }
3919 } else {
3920 const struct edid *edid = (const struct edid *)sink->dc_edid.raw_edid;
3921
3922 aconnector->drm_edid = drm_edid_alloc(edid, sink->dc_edid.length);
3923 drm_edid_connector_update(connector, aconnector->drm_edid);
3924
3925 hdmi_cec_set_edid(aconnector);
3926 if (aconnector->dc_link->aux_mode)
3927 drm_dp_cec_attach(&aconnector->dm_dp_aux.aux,
3928 connector->display_info.source_physical_address);
3929 }
3930
3931 if (!aconnector->timing_requested) {
3932 aconnector->timing_requested =
3933 kzalloc_obj(struct dc_crtc_timing);
3934 if (!aconnector->timing_requested)
3935 drm_err(dev,
3936 "failed to create aconnector->requested_timing\n");
3937 }
3938
3939 amdgpu_dm_update_freesync_caps(connector, aconnector->drm_edid);
3940 update_connector_ext_caps(aconnector);
3941 } else {
3942 hdmi_cec_unset_edid(aconnector);
3943 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
3944 amdgpu_dm_update_freesync_caps(connector, NULL);
3945 aconnector->num_modes = 0;
3946 dc_sink_release(aconnector->dc_sink);
3947 aconnector->dc_sink = NULL;
3948 drm_edid_free(aconnector->drm_edid);
3949 aconnector->drm_edid = NULL;
3950 kfree(aconnector->timing_requested);
3951 aconnector->timing_requested = NULL;
3952 /* Set CP to DESIRED if it was ENABLED, so we can re-enable it again on hotplug */
3953 if (connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
3954 connector->state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
3955 }
3956
3957 update_subconnector_property(aconnector);
3958
3959 /* When polling, the mutex will be unlocked for us by DRM. */
3960 if (!drm_kms_helper_is_poll_worker())
3961 mutex_unlock(&dev->mode_config.mutex);
3962 }
3963
are_sinks_equal(const struct dc_sink * sink1,const struct dc_sink * sink2)3964 static bool are_sinks_equal(const struct dc_sink *sink1, const struct dc_sink *sink2)
3965 {
3966 if (!sink1 || !sink2)
3967 return false;
3968 if (sink1->sink_signal != sink2->sink_signal)
3969 return false;
3970
3971 if (sink1->dc_edid.length != sink2->dc_edid.length)
3972 return false;
3973
3974 if (memcmp(sink1->dc_edid.raw_edid, sink2->dc_edid.raw_edid,
3975 sink1->dc_edid.length) != 0)
3976 return false;
3977 return true;
3978 }
3979
3980
3981 /**
3982 * DOC: hdmi_hpd_debounce_work
3983 *
3984 * HDMI HPD debounce delay in milliseconds. When an HDMI display toggles HPD
3985 * (such as during power save transitions), this delay determines how long to
3986 * wait before processing the HPD event. This allows distinguishing between a
3987 * physical unplug (>hdmi_hpd_debounce_delay)
3988 * and a spontaneous RX HPD toggle (<hdmi_hpd_debounce_delay).
3989 *
3990 * If the toggle is less than this delay, the driver compares sink capabilities
3991 * and permits a hotplug event if they changed.
3992 *
3993 * The default value of 1500ms was chosen based on experimental testing with
3994 * various monitors that exhibit spontaneous HPD toggling behavior.
3995 */
hdmi_hpd_debounce_work(struct work_struct * work)3996 static void hdmi_hpd_debounce_work(struct work_struct *work)
3997 {
3998 struct amdgpu_dm_connector *aconnector =
3999 container_of(to_delayed_work(work), struct amdgpu_dm_connector,
4000 hdmi_hpd_debounce_work);
4001 struct drm_connector *connector = &aconnector->base;
4002 struct drm_device *dev = connector->dev;
4003 struct amdgpu_device *adev = drm_to_adev(dev);
4004 struct dc *dc = aconnector->dc_link->ctx->dc;
4005 bool fake_reconnect = false;
4006 bool reallow_idle = false;
4007 bool ret = false;
4008 guard(mutex)(&aconnector->hpd_lock);
4009
4010 /* Re-detect the display */
4011 scoped_guard(mutex, &adev->dm.dc_lock) {
4012 if (dc->caps.ips_support && dc->ctx->dmub_srv->idle_allowed) {
4013 dc_allow_idle_optimizations(dc, false);
4014 reallow_idle = true;
4015 }
4016 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
4017 }
4018
4019 if (ret) {
4020 /* Apply workaround delay for certain panels */
4021 apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink);
4022 /* Compare sinks to determine if this was a spontaneous HPD toggle */
4023 if (are_sinks_equal(aconnector->dc_link->local_sink, aconnector->hdmi_prev_sink)) {
4024 /*
4025 * Sinks match - this was a spontaneous HDMI HPD toggle.
4026 */
4027 drm_dbg_kms(dev, "HDMI HPD: Sink unchanged after debounce, internal re-enable\n");
4028 fake_reconnect = true;
4029 }
4030
4031 /* Update connector state */
4032 amdgpu_dm_update_connector_after_detect(aconnector);
4033
4034 drm_modeset_lock_all(dev);
4035 dm_restore_drm_connector_state(dev, connector);
4036 drm_modeset_unlock_all(dev);
4037
4038 /* Only notify OS if sink actually changed */
4039 if (!fake_reconnect && aconnector->base.force == DRM_FORCE_UNSPECIFIED)
4040 drm_kms_helper_hotplug_event(dev);
4041 }
4042
4043 /* Release the cached sink reference */
4044 if (aconnector->hdmi_prev_sink) {
4045 dc_sink_release(aconnector->hdmi_prev_sink);
4046 aconnector->hdmi_prev_sink = NULL;
4047 }
4048
4049 scoped_guard(mutex, &adev->dm.dc_lock) {
4050 if (reallow_idle && dc->caps.ips_support)
4051 dc_allow_idle_optimizations(dc, true);
4052 }
4053 }
4054
handle_hpd_irq_helper(struct amdgpu_dm_connector * aconnector)4055 static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector)
4056 {
4057 struct drm_connector *connector = &aconnector->base;
4058 struct drm_device *dev = connector->dev;
4059 enum dc_connection_type new_connection_type = dc_connection_none;
4060 struct amdgpu_device *adev = drm_to_adev(dev);
4061 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
4062 struct dc *dc = aconnector->dc_link->ctx->dc;
4063 bool ret = false;
4064 bool debounce_required = false;
4065
4066 if (adev->dm.disable_hpd_irq)
4067 return;
4068
4069 /*
4070 * In case of failure or MST no need to update connector status or notify the OS
4071 * since (for MST case) MST does this in its own context.
4072 */
4073 guard(mutex)(&aconnector->hpd_lock);
4074
4075 if (adev->dm.hdcp_workqueue) {
4076 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
4077 dm_con_state->update_hdcp = true;
4078 }
4079 if (aconnector->fake_enable)
4080 aconnector->fake_enable = false;
4081
4082 aconnector->timing_changed = false;
4083
4084 if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type))
4085 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
4086
4087 /*
4088 * Check for HDMI disconnect with debounce enabled.
4089 */
4090 debounce_required = (aconnector->hdmi_hpd_debounce_delay_ms > 0 &&
4091 dc_is_hdmi_signal(aconnector->dc_link->connector_signal) &&
4092 new_connection_type == dc_connection_none &&
4093 aconnector->dc_link->local_sink != NULL);
4094
4095 if (aconnector->base.force && new_connection_type == dc_connection_none) {
4096 emulated_link_detect(aconnector->dc_link);
4097
4098 drm_modeset_lock_all(dev);
4099 dm_restore_drm_connector_state(dev, connector);
4100 drm_modeset_unlock_all(dev);
4101
4102 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
4103 drm_kms_helper_connector_hotplug_event(connector);
4104 } else if (debounce_required) {
4105 /*
4106 * HDMI disconnect detected - schedule delayed work instead of
4107 * processing immediately. This allows us to coalesce spurious
4108 * HDMI signals from physical unplugs.
4109 */
4110 drm_dbg_kms(dev, "HDMI HPD: Disconnect detected, scheduling debounce work (%u ms)\n",
4111 aconnector->hdmi_hpd_debounce_delay_ms);
4112
4113 /* Cache the current sink for later comparison */
4114 if (aconnector->hdmi_prev_sink)
4115 dc_sink_release(aconnector->hdmi_prev_sink);
4116 aconnector->hdmi_prev_sink = aconnector->dc_link->local_sink;
4117 if (aconnector->hdmi_prev_sink)
4118 dc_sink_retain(aconnector->hdmi_prev_sink);
4119
4120 /* Schedule delayed detection. */
4121 if (mod_delayed_work(system_wq,
4122 &aconnector->hdmi_hpd_debounce_work,
4123 msecs_to_jiffies(aconnector->hdmi_hpd_debounce_delay_ms)))
4124 drm_dbg_kms(dev, "HDMI HPD: Re-scheduled debounce work\n");
4125
4126 } else {
4127
4128 /* If the aconnector->hdmi_hpd_debounce_work is scheduled, exit early */
4129 if (delayed_work_pending(&aconnector->hdmi_hpd_debounce_work))
4130 return;
4131
4132 scoped_guard(mutex, &adev->dm.dc_lock) {
4133 dc_exit_ips_for_hw_access(dc);
4134 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
4135 }
4136 if (ret) {
4137 /* w/a delay for certain panels */
4138 apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink);
4139 amdgpu_dm_update_connector_after_detect(aconnector);
4140
4141 drm_modeset_lock_all(dev);
4142 dm_restore_drm_connector_state(dev, connector);
4143 drm_modeset_unlock_all(dev);
4144
4145 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
4146 drm_kms_helper_connector_hotplug_event(connector);
4147 }
4148 }
4149 }
4150
handle_hpd_irq(void * param)4151 static void handle_hpd_irq(void *param)
4152 {
4153 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
4154
4155 handle_hpd_irq_helper(aconnector);
4156
4157 }
4158
schedule_hpd_rx_offload_work(struct amdgpu_device * adev,struct hpd_rx_irq_offload_work_queue * offload_wq,union hpd_irq_data hpd_irq_data)4159 static void schedule_hpd_rx_offload_work(struct amdgpu_device *adev, struct hpd_rx_irq_offload_work_queue *offload_wq,
4160 union hpd_irq_data hpd_irq_data)
4161 {
4162 struct hpd_rx_irq_offload_work *offload_work = kzalloc_obj(*offload_work);
4163
4164 if (!offload_work) {
4165 drm_err(adev_to_drm(adev), "Failed to allocate hpd_rx_irq_offload_work.\n");
4166 return;
4167 }
4168
4169 INIT_WORK(&offload_work->work, dm_handle_hpd_rx_offload_work);
4170 offload_work->data = hpd_irq_data;
4171 offload_work->offload_wq = offload_wq;
4172 offload_work->adev = adev;
4173
4174 queue_work(offload_wq->wq, &offload_work->work);
4175 drm_dbg_kms(adev_to_drm(adev), "queue work to handle hpd_rx offload work");
4176 }
4177
handle_hpd_rx_irq(void * param)4178 static void handle_hpd_rx_irq(void *param)
4179 {
4180 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
4181 struct drm_connector *connector = &aconnector->base;
4182 struct drm_device *dev = connector->dev;
4183 struct dc_link *dc_link = aconnector->dc_link;
4184 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
4185 bool result = false;
4186 enum dc_connection_type new_connection_type = dc_connection_none;
4187 struct amdgpu_device *adev = drm_to_adev(dev);
4188 union hpd_irq_data hpd_irq_data;
4189 bool link_loss = false;
4190 bool has_left_work = false;
4191 int idx = dc_link->link_index;
4192 struct hpd_rx_irq_offload_work_queue *offload_wq = &adev->dm.hpd_rx_offload_wq[idx];
4193 struct dc *dc = aconnector->dc_link->ctx->dc;
4194
4195 memset(&hpd_irq_data, 0, sizeof(hpd_irq_data));
4196
4197 if (adev->dm.disable_hpd_irq)
4198 return;
4199
4200 /*
4201 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio
4202 * conflict, after implement i2c helper, this mutex should be
4203 * retired.
4204 */
4205 mutex_lock(&aconnector->hpd_lock);
4206
4207 result = dc_link_handle_hpd_rx_irq(dc_link, &hpd_irq_data,
4208 &link_loss, true, &has_left_work);
4209
4210 if (!has_left_work)
4211 goto out;
4212
4213 if (hpd_irq_data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
4214 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
4215 goto out;
4216 }
4217
4218 if (dc_link_dp_allow_hpd_rx_irq(dc_link)) {
4219 if (hpd_irq_data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY ||
4220 hpd_irq_data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) {
4221 bool skip = false;
4222
4223 /*
4224 * DOWN_REP_MSG_RDY is also handled by polling method
4225 * mgr->cbs->poll_hpd_irq()
4226 */
4227 spin_lock(&offload_wq->offload_lock);
4228 skip = offload_wq->is_handling_mst_msg_rdy_event;
4229
4230 if (!skip)
4231 offload_wq->is_handling_mst_msg_rdy_event = true;
4232
4233 spin_unlock(&offload_wq->offload_lock);
4234
4235 if (!skip)
4236 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
4237
4238 goto out;
4239 }
4240
4241 if (link_loss) {
4242 bool skip = false;
4243
4244 spin_lock(&offload_wq->offload_lock);
4245 skip = offload_wq->is_handling_link_loss;
4246
4247 if (!skip)
4248 offload_wq->is_handling_link_loss = true;
4249
4250 spin_unlock(&offload_wq->offload_lock);
4251
4252 if (!skip)
4253 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
4254
4255 goto out;
4256 }
4257 }
4258
4259 out:
4260 if (result && !is_mst_root_connector) {
4261 /* Downstream Port status changed. */
4262 if (!dc_link_detect_connection_type(dc_link, &new_connection_type))
4263 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
4264
4265 if (aconnector->base.force && new_connection_type == dc_connection_none) {
4266 emulated_link_detect(dc_link);
4267
4268 if (aconnector->fake_enable)
4269 aconnector->fake_enable = false;
4270
4271 amdgpu_dm_update_connector_after_detect(aconnector);
4272
4273
4274 drm_modeset_lock_all(dev);
4275 dm_restore_drm_connector_state(dev, connector);
4276 drm_modeset_unlock_all(dev);
4277
4278 drm_kms_helper_connector_hotplug_event(connector);
4279 } else {
4280 bool ret = false;
4281
4282 mutex_lock(&adev->dm.dc_lock);
4283 dc_exit_ips_for_hw_access(dc);
4284 ret = dc_link_detect(dc_link, DETECT_REASON_HPDRX);
4285 mutex_unlock(&adev->dm.dc_lock);
4286
4287 if (ret) {
4288 if (aconnector->fake_enable)
4289 aconnector->fake_enable = false;
4290
4291 amdgpu_dm_update_connector_after_detect(aconnector);
4292
4293 drm_modeset_lock_all(dev);
4294 dm_restore_drm_connector_state(dev, connector);
4295 drm_modeset_unlock_all(dev);
4296
4297 drm_kms_helper_connector_hotplug_event(connector);
4298 }
4299 }
4300 }
4301 if (hpd_irq_data.bytes.device_service_irq.bits.CP_IRQ) {
4302 if (adev->dm.hdcp_workqueue)
4303 hdcp_handle_cpirq(adev->dm.hdcp_workqueue, aconnector->base.index);
4304 }
4305
4306 if (dc_link->type != dc_connection_mst_branch)
4307 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux);
4308
4309 mutex_unlock(&aconnector->hpd_lock);
4310 }
4311
register_hpd_handlers(struct amdgpu_device * adev)4312 static int register_hpd_handlers(struct amdgpu_device *adev)
4313 {
4314 struct drm_device *dev = adev_to_drm(adev);
4315 struct drm_connector *connector;
4316 struct amdgpu_dm_connector *aconnector;
4317 const struct dc_link *dc_link;
4318 struct dc_interrupt_params int_params = {0};
4319
4320 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4321 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4322
4323 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
4324 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD,
4325 dmub_hpd_callback, true)) {
4326 drm_err(adev_to_drm(adev), "fail to register dmub hpd callback");
4327 return -EINVAL;
4328 }
4329
4330 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_IRQ,
4331 dmub_hpd_callback, true)) {
4332 drm_err(adev_to_drm(adev), "fail to register dmub hpd callback");
4333 return -EINVAL;
4334 }
4335
4336 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_SENSE_NOTIFY,
4337 dmub_hpd_sense_callback, true)) {
4338 drm_err(adev_to_drm(adev), "fail to register dmub hpd sense callback");
4339 return -EINVAL;
4340 }
4341 }
4342
4343 list_for_each_entry(connector,
4344 &dev->mode_config.connector_list, head) {
4345
4346 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
4347 continue;
4348
4349 aconnector = to_amdgpu_dm_connector(connector);
4350 dc_link = aconnector->dc_link;
4351
4352 if (dc_link->irq_source_hpd != DC_IRQ_SOURCE_INVALID) {
4353 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
4354 int_params.irq_source = dc_link->irq_source_hpd;
4355
4356 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4357 int_params.irq_source < DC_IRQ_SOURCE_HPD1 ||
4358 int_params.irq_source > DC_IRQ_SOURCE_HPD6) {
4359 drm_err(adev_to_drm(adev), "Failed to register hpd irq!\n");
4360 return -EINVAL;
4361 }
4362
4363 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4364 handle_hpd_irq, (void *) aconnector))
4365 return -ENOMEM;
4366 }
4367
4368 if (dc_link->irq_source_hpd_rx != DC_IRQ_SOURCE_INVALID) {
4369
4370 /* Also register for DP short pulse (hpd_rx). */
4371 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
4372 int_params.irq_source = dc_link->irq_source_hpd_rx;
4373
4374 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4375 int_params.irq_source < DC_IRQ_SOURCE_HPD1RX ||
4376 int_params.irq_source > DC_IRQ_SOURCE_HPD6RX) {
4377 drm_err(adev_to_drm(adev), "Failed to register hpd rx irq!\n");
4378 return -EINVAL;
4379 }
4380
4381 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4382 handle_hpd_rx_irq, (void *) aconnector))
4383 return -ENOMEM;
4384 }
4385 }
4386 return 0;
4387 }
4388
4389 #if defined(CONFIG_DRM_AMD_DC_SI)
4390 /* Register IRQ sources and initialize IRQ callbacks */
dce60_register_irq_handlers(struct amdgpu_device * adev)4391 static int dce60_register_irq_handlers(struct amdgpu_device *adev)
4392 {
4393 struct dc *dc = adev->dm.dc;
4394 struct common_irq_params *c_irq_params;
4395 struct dc_interrupt_params int_params = {0};
4396 int r;
4397 int i;
4398 unsigned int client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
4399
4400 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4401 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4402
4403 /*
4404 * Actions of amdgpu_irq_add_id():
4405 * 1. Register a set() function with base driver.
4406 * Base driver will call set() function to enable/disable an
4407 * interrupt in DC hardware.
4408 * 2. Register amdgpu_dm_irq_handler().
4409 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
4410 * coming from DC hardware.
4411 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
4412 * for acknowledging and handling.
4413 */
4414
4415 /* Use VBLANK interrupt */
4416 for (i = 0; i < adev->mode_info.num_crtc; i++) {
4417 r = amdgpu_irq_add_id(adev, client_id, i + 1, &adev->crtc_irq);
4418 if (r) {
4419 drm_err(adev_to_drm(adev), "Failed to add crtc irq id!\n");
4420 return r;
4421 }
4422
4423 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4424 int_params.irq_source =
4425 dc_interrupt_to_irq_source(dc, i + 1, 0);
4426
4427 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4428 int_params.irq_source < DC_IRQ_SOURCE_VBLANK1 ||
4429 int_params.irq_source > DC_IRQ_SOURCE_VBLANK6) {
4430 drm_err(adev_to_drm(adev), "Failed to register vblank irq!\n");
4431 return -EINVAL;
4432 }
4433
4434 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
4435
4436 c_irq_params->adev = adev;
4437 c_irq_params->irq_src = int_params.irq_source;
4438
4439 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4440 dm_crtc_high_irq, c_irq_params))
4441 return -ENOMEM;
4442 }
4443
4444 /* Use GRPH_PFLIP interrupt */
4445 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
4446 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
4447 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
4448 if (r) {
4449 drm_err(adev_to_drm(adev), "Failed to add page flip irq id!\n");
4450 return r;
4451 }
4452
4453 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4454 int_params.irq_source =
4455 dc_interrupt_to_irq_source(dc, i, 0);
4456
4457 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4458 int_params.irq_source < DC_IRQ_SOURCE_PFLIP_FIRST ||
4459 int_params.irq_source > DC_IRQ_SOURCE_PFLIP_LAST) {
4460 drm_err(adev_to_drm(adev), "Failed to register pflip irq!\n");
4461 return -EINVAL;
4462 }
4463
4464 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
4465
4466 c_irq_params->adev = adev;
4467 c_irq_params->irq_src = int_params.irq_source;
4468
4469 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4470 dm_pflip_high_irq, c_irq_params))
4471 return -ENOMEM;
4472 }
4473
4474 /* HPD */
4475 r = amdgpu_irq_add_id(adev, client_id,
4476 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
4477 if (r) {
4478 drm_err(adev_to_drm(adev), "Failed to add hpd irq id!\n");
4479 return r;
4480 }
4481
4482 r = register_hpd_handlers(adev);
4483
4484 return r;
4485 }
4486 #endif
4487
4488 /* Register IRQ sources and initialize IRQ callbacks */
dce110_register_irq_handlers(struct amdgpu_device * adev)4489 static int dce110_register_irq_handlers(struct amdgpu_device *adev)
4490 {
4491 struct dc *dc = adev->dm.dc;
4492 struct common_irq_params *c_irq_params;
4493 struct dc_interrupt_params int_params = {0};
4494 int r;
4495 int i;
4496 unsigned int client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
4497
4498 if (adev->family >= AMDGPU_FAMILY_AI)
4499 client_id = SOC15_IH_CLIENTID_DCE;
4500
4501 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4502 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4503
4504 /*
4505 * Actions of amdgpu_irq_add_id():
4506 * 1. Register a set() function with base driver.
4507 * Base driver will call set() function to enable/disable an
4508 * interrupt in DC hardware.
4509 * 2. Register amdgpu_dm_irq_handler().
4510 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
4511 * coming from DC hardware.
4512 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
4513 * for acknowledging and handling.
4514 */
4515
4516 /* Use VBLANK interrupt */
4517 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
4518 r = amdgpu_irq_add_id(adev, client_id, i, &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, i, 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 /* Use VUPDATE interrupt */
4546 for (i = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT; i <= VISLANDS30_IV_SRCID_D6_V_UPDATE_INT; i += 2) {
4547 r = amdgpu_irq_add_id(adev, client_id, i, &adev->vupdate_irq);
4548 if (r) {
4549 drm_err(adev_to_drm(adev), "Failed to add vupdate irq id!\n");
4550 return r;
4551 }
4552
4553 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4554 int_params.irq_source =
4555 dc_interrupt_to_irq_source(dc, i, 0);
4556
4557 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4558 int_params.irq_source < DC_IRQ_SOURCE_VUPDATE1 ||
4559 int_params.irq_source > DC_IRQ_SOURCE_VUPDATE6) {
4560 drm_err(adev_to_drm(adev), "Failed to register vupdate irq!\n");
4561 return -EINVAL;
4562 }
4563
4564 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
4565
4566 c_irq_params->adev = adev;
4567 c_irq_params->irq_src = int_params.irq_source;
4568
4569 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4570 dm_vupdate_high_irq, c_irq_params))
4571 return -ENOMEM;
4572 }
4573
4574 /* Use GRPH_PFLIP interrupt */
4575 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
4576 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
4577 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
4578 if (r) {
4579 drm_err(adev_to_drm(adev), "Failed to add page flip irq id!\n");
4580 return r;
4581 }
4582
4583 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4584 int_params.irq_source =
4585 dc_interrupt_to_irq_source(dc, i, 0);
4586
4587 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4588 int_params.irq_source < DC_IRQ_SOURCE_PFLIP_FIRST ||
4589 int_params.irq_source > DC_IRQ_SOURCE_PFLIP_LAST) {
4590 drm_err(adev_to_drm(adev), "Failed to register pflip irq!\n");
4591 return -EINVAL;
4592 }
4593
4594 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
4595
4596 c_irq_params->adev = adev;
4597 c_irq_params->irq_src = int_params.irq_source;
4598
4599 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4600 dm_pflip_high_irq, c_irq_params))
4601 return -ENOMEM;
4602 }
4603
4604 /* HPD */
4605 r = amdgpu_irq_add_id(adev, client_id,
4606 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
4607 if (r) {
4608 drm_err(adev_to_drm(adev), "Failed to add hpd irq id!\n");
4609 return r;
4610 }
4611
4612 r = register_hpd_handlers(adev);
4613
4614 return r;
4615 }
4616
4617 /* Register IRQ sources and initialize IRQ callbacks */
dcn10_register_irq_handlers(struct amdgpu_device * adev)4618 static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
4619 {
4620 struct dc *dc = adev->dm.dc;
4621 struct common_irq_params *c_irq_params;
4622 struct dc_interrupt_params int_params = {0};
4623 int r;
4624 int i;
4625 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
4626 static const unsigned int vrtl_int_srcid[] = {
4627 DCN_1_0__SRCID__OTG1_VERTICAL_INTERRUPT0_CONTROL,
4628 DCN_1_0__SRCID__OTG2_VERTICAL_INTERRUPT0_CONTROL,
4629 DCN_1_0__SRCID__OTG3_VERTICAL_INTERRUPT0_CONTROL,
4630 DCN_1_0__SRCID__OTG4_VERTICAL_INTERRUPT0_CONTROL,
4631 DCN_1_0__SRCID__OTG5_VERTICAL_INTERRUPT0_CONTROL,
4632 DCN_1_0__SRCID__OTG6_VERTICAL_INTERRUPT0_CONTROL
4633 };
4634 #endif
4635
4636 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4637 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4638
4639 /*
4640 * Actions of amdgpu_irq_add_id():
4641 * 1. Register a set() function with base driver.
4642 * Base driver will call set() function to enable/disable an
4643 * interrupt in DC hardware.
4644 * 2. Register amdgpu_dm_irq_handler().
4645 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
4646 * coming from DC hardware.
4647 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
4648 * for acknowledging and handling.
4649 */
4650
4651 /* Use VSTARTUP interrupt */
4652 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
4653 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
4654 i++) {
4655 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
4656
4657 if (r) {
4658 drm_err(adev_to_drm(adev), "Failed to add crtc irq id!\n");
4659 return r;
4660 }
4661
4662 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4663 int_params.irq_source =
4664 dc_interrupt_to_irq_source(dc, i, 0);
4665
4666 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4667 int_params.irq_source < DC_IRQ_SOURCE_VBLANK1 ||
4668 int_params.irq_source > DC_IRQ_SOURCE_VBLANK6) {
4669 drm_err(adev_to_drm(adev), "Failed to register vblank irq!\n");
4670 return -EINVAL;
4671 }
4672
4673 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
4674
4675 c_irq_params->adev = adev;
4676 c_irq_params->irq_src = int_params.irq_source;
4677
4678 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4679 dm_crtc_high_irq, c_irq_params))
4680 return -ENOMEM;
4681 }
4682
4683 /* Use otg vertical line interrupt */
4684 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
4685 for (i = 0; i <= adev->mode_info.num_crtc - 1; i++) {
4686 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE,
4687 vrtl_int_srcid[i], &adev->vline0_irq);
4688
4689 if (r) {
4690 drm_err(adev_to_drm(adev), "Failed to add vline0 irq id!\n");
4691 return r;
4692 }
4693
4694 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4695 int_params.irq_source =
4696 dc_interrupt_to_irq_source(dc, vrtl_int_srcid[i], 0);
4697
4698 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4699 int_params.irq_source < DC_IRQ_SOURCE_DC1_VLINE0 ||
4700 int_params.irq_source > DC_IRQ_SOURCE_DC6_VLINE0) {
4701 drm_err(adev_to_drm(adev), "Failed to register vline0 irq!\n");
4702 return -EINVAL;
4703 }
4704
4705 c_irq_params = &adev->dm.vline0_params[int_params.irq_source
4706 - DC_IRQ_SOURCE_DC1_VLINE0];
4707
4708 c_irq_params->adev = adev;
4709 c_irq_params->irq_src = int_params.irq_source;
4710
4711 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4712 dm_dcn_vertical_interrupt0_high_irq,
4713 c_irq_params))
4714 return -ENOMEM;
4715 }
4716 #endif
4717
4718 /* Use VUPDATE_NO_LOCK interrupt on DCN, which seems to correspond to
4719 * the regular VUPDATE interrupt on DCE. We want DC_IRQ_SOURCE_VUPDATEx
4720 * to trigger at end of each vblank, regardless of state of the lock,
4721 * matching DCE behaviour.
4722 */
4723 for (i = DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT;
4724 i <= DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT + adev->mode_info.num_crtc - 1;
4725 i++) {
4726 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->vupdate_irq);
4727
4728 if (r) {
4729 drm_err(adev_to_drm(adev), "Failed to add vupdate irq id!\n");
4730 return r;
4731 }
4732
4733 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4734 int_params.irq_source =
4735 dc_interrupt_to_irq_source(dc, i, 0);
4736
4737 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4738 int_params.irq_source < DC_IRQ_SOURCE_VUPDATE1 ||
4739 int_params.irq_source > DC_IRQ_SOURCE_VUPDATE6) {
4740 drm_err(adev_to_drm(adev), "Failed to register vupdate irq!\n");
4741 return -EINVAL;
4742 }
4743
4744 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
4745
4746 c_irq_params->adev = adev;
4747 c_irq_params->irq_src = int_params.irq_source;
4748
4749 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4750 dm_vupdate_high_irq, c_irq_params))
4751 return -ENOMEM;
4752 }
4753
4754 /* Use GRPH_PFLIP interrupt */
4755 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
4756 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + dc->caps.max_otg_num - 1;
4757 i++) {
4758 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
4759 if (r) {
4760 drm_err(adev_to_drm(adev), "Failed to add page flip irq id!\n");
4761 return r;
4762 }
4763
4764 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4765 int_params.irq_source =
4766 dc_interrupt_to_irq_source(dc, i, 0);
4767
4768 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4769 int_params.irq_source < DC_IRQ_SOURCE_PFLIP_FIRST ||
4770 int_params.irq_source > DC_IRQ_SOURCE_PFLIP_LAST) {
4771 drm_err(adev_to_drm(adev), "Failed to register pflip irq!\n");
4772 return -EINVAL;
4773 }
4774
4775 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
4776
4777 c_irq_params->adev = adev;
4778 c_irq_params->irq_src = int_params.irq_source;
4779
4780 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4781 dm_pflip_high_irq, c_irq_params))
4782 return -ENOMEM;
4783 }
4784
4785 /* HPD */
4786 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
4787 &adev->hpd_irq);
4788 if (r) {
4789 drm_err(adev_to_drm(adev), "Failed to add hpd irq id!\n");
4790 return r;
4791 }
4792
4793 r = register_hpd_handlers(adev);
4794
4795 return r;
4796 }
4797 /* Register Outbox IRQ sources and initialize IRQ callbacks */
register_outbox_irq_handlers(struct amdgpu_device * adev)4798 static int register_outbox_irq_handlers(struct amdgpu_device *adev)
4799 {
4800 struct dc *dc = adev->dm.dc;
4801 struct common_irq_params *c_irq_params;
4802 struct dc_interrupt_params int_params = {0};
4803 int r, i;
4804
4805 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4806 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4807
4808 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT,
4809 &adev->dmub_outbox_irq);
4810 if (r) {
4811 drm_err(adev_to_drm(adev), "Failed to add outbox irq id!\n");
4812 return r;
4813 }
4814
4815 if (dc->ctx->dmub_srv) {
4816 i = DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT;
4817 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
4818 int_params.irq_source =
4819 dc_interrupt_to_irq_source(dc, i, 0);
4820
4821 c_irq_params = &adev->dm.dmub_outbox_params[0];
4822
4823 c_irq_params->adev = adev;
4824 c_irq_params->irq_src = int_params.irq_source;
4825
4826 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4827 dm_dmub_outbox1_low_irq, c_irq_params))
4828 return -ENOMEM;
4829 }
4830
4831 return 0;
4832 }
4833
4834 /*
4835 * Acquires the lock for the atomic state object and returns
4836 * the new atomic state.
4837 *
4838 * This should only be called during atomic check.
4839 */
dm_atomic_get_state(struct drm_atomic_state * state,struct dm_atomic_state ** dm_state)4840 int dm_atomic_get_state(struct drm_atomic_state *state,
4841 struct dm_atomic_state **dm_state)
4842 {
4843 struct drm_device *dev = state->dev;
4844 struct amdgpu_device *adev = drm_to_adev(dev);
4845 struct amdgpu_display_manager *dm = &adev->dm;
4846 struct drm_private_state *priv_state;
4847
4848 if (*dm_state)
4849 return 0;
4850
4851 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj);
4852 if (IS_ERR(priv_state))
4853 return PTR_ERR(priv_state);
4854
4855 *dm_state = to_dm_atomic_state(priv_state);
4856
4857 return 0;
4858 }
4859
4860 static struct dm_atomic_state *
dm_atomic_get_new_state(struct drm_atomic_state * state)4861 dm_atomic_get_new_state(struct drm_atomic_state *state)
4862 {
4863 struct drm_device *dev = state->dev;
4864 struct amdgpu_device *adev = drm_to_adev(dev);
4865 struct amdgpu_display_manager *dm = &adev->dm;
4866 struct drm_private_obj *obj;
4867 struct drm_private_state *new_obj_state;
4868 int i;
4869
4870 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) {
4871 if (obj->funcs == dm->atomic_obj.funcs)
4872 return to_dm_atomic_state(new_obj_state);
4873 }
4874
4875 return NULL;
4876 }
4877
4878 static struct drm_private_state *
dm_atomic_duplicate_state(struct drm_private_obj * obj)4879 dm_atomic_duplicate_state(struct drm_private_obj *obj)
4880 {
4881 struct dm_atomic_state *old_state, *new_state;
4882
4883 new_state = kzalloc_obj(*new_state);
4884 if (!new_state)
4885 return NULL;
4886
4887 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base);
4888
4889 old_state = to_dm_atomic_state(obj->state);
4890
4891 if (old_state && old_state->context)
4892 new_state->context = dc_state_create_copy(old_state->context);
4893
4894 if (!new_state->context) {
4895 kfree(new_state);
4896 return NULL;
4897 }
4898
4899 return &new_state->base;
4900 }
4901
dm_atomic_destroy_state(struct drm_private_obj * obj,struct drm_private_state * state)4902 static void dm_atomic_destroy_state(struct drm_private_obj *obj,
4903 struct drm_private_state *state)
4904 {
4905 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
4906
4907 if (dm_state && dm_state->context)
4908 dc_state_release(dm_state->context);
4909
4910 kfree(dm_state);
4911 }
4912
4913 static struct drm_private_state_funcs dm_atomic_state_funcs = {
4914 .atomic_duplicate_state = dm_atomic_duplicate_state,
4915 .atomic_destroy_state = dm_atomic_destroy_state,
4916 };
4917
amdgpu_dm_mode_config_init(struct amdgpu_device * adev)4918 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
4919 {
4920 struct dm_atomic_state *state;
4921 int r;
4922
4923 adev->mode_info.mode_config_initialized = true;
4924
4925 adev_to_drm(adev)->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
4926 adev_to_drm(adev)->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
4927
4928 adev_to_drm(adev)->mode_config.max_width = 16384;
4929 adev_to_drm(adev)->mode_config.max_height = 16384;
4930
4931 adev_to_drm(adev)->mode_config.preferred_depth = 24;
4932 if (adev->asic_type == CHIP_HAWAII)
4933 /* disable prefer shadow for now due to hibernation issues */
4934 adev_to_drm(adev)->mode_config.prefer_shadow = 0;
4935 else
4936 adev_to_drm(adev)->mode_config.prefer_shadow = 1;
4937 /* indicates support for immediate flip */
4938 adev_to_drm(adev)->mode_config.async_page_flip = true;
4939
4940 state = kzalloc_obj(*state);
4941 if (!state)
4942 return -ENOMEM;
4943
4944 state->context = dc_state_create_current_copy(adev->dm.dc);
4945 if (!state->context) {
4946 kfree(state);
4947 return -ENOMEM;
4948 }
4949
4950 drm_atomic_private_obj_init(adev_to_drm(adev),
4951 &adev->dm.atomic_obj,
4952 &state->base,
4953 &dm_atomic_state_funcs);
4954
4955 r = amdgpu_display_modeset_create_props(adev);
4956 if (r) {
4957 dc_state_release(state->context);
4958 kfree(state);
4959 return r;
4960 }
4961
4962 #ifdef AMD_PRIVATE_COLOR
4963 if (amdgpu_dm_create_color_properties(adev)) {
4964 dc_state_release(state->context);
4965 kfree(state);
4966 return -ENOMEM;
4967 }
4968 #endif
4969
4970 r = amdgpu_dm_audio_init(adev);
4971 if (r) {
4972 dc_state_release(state->context);
4973 kfree(state);
4974 return r;
4975 }
4976
4977 return 0;
4978 }
4979
4980 #define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12
4981 #define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255
4982 #define AMDGPU_DM_MIN_SPREAD ((AMDGPU_DM_DEFAULT_MAX_BACKLIGHT - AMDGPU_DM_DEFAULT_MIN_BACKLIGHT) / 2)
4983 #define AUX_BL_DEFAULT_TRANSITION_TIME_MS 50
4984
amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager * dm,int bl_idx)4985 static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm,
4986 int bl_idx)
4987 {
4988 struct amdgpu_dm_backlight_caps *caps = &dm->backlight_caps[bl_idx];
4989
4990 if (caps->caps_valid)
4991 return;
4992
4993 #if defined(CONFIG_ACPI)
4994 amdgpu_acpi_get_backlight_caps(caps);
4995
4996 /* validate the firmware value is sane */
4997 if (caps->caps_valid) {
4998 int spread = caps->max_input_signal - caps->min_input_signal;
4999
5000 if (caps->max_input_signal > AMDGPU_DM_DEFAULT_MAX_BACKLIGHT ||
5001 caps->min_input_signal < 0 ||
5002 spread > AMDGPU_DM_DEFAULT_MAX_BACKLIGHT ||
5003 spread < AMDGPU_DM_MIN_SPREAD) {
5004 drm_dbg_kms(adev_to_drm(dm->adev), "DM: Invalid backlight caps: min=%d, max=%d\n",
5005 caps->min_input_signal, caps->max_input_signal);
5006 caps->caps_valid = false;
5007 }
5008 }
5009
5010 if (!caps->caps_valid) {
5011 caps->min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
5012 caps->max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
5013 caps->caps_valid = true;
5014 }
5015 #else
5016 if (caps->aux_support)
5017 return;
5018
5019 caps->min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
5020 caps->max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
5021 caps->caps_valid = true;
5022 #endif
5023 }
5024
get_brightness_range(const struct amdgpu_dm_backlight_caps * caps,unsigned int * min,unsigned int * max)5025 static int get_brightness_range(const struct amdgpu_dm_backlight_caps *caps,
5026 unsigned int *min, unsigned int *max)
5027 {
5028 if (!caps)
5029 return 0;
5030
5031 if (caps->aux_support) {
5032 // Firmware limits are in nits, DC API wants millinits.
5033 *max = 1000 * caps->aux_max_input_signal;
5034 *min = 1000 * caps->aux_min_input_signal;
5035 } else {
5036 // Firmware limits are 8-bit, PWM control is 16-bit.
5037 *max = 0x101 * caps->max_input_signal;
5038 *min = 0x101 * caps->min_input_signal;
5039 }
5040 return 1;
5041 }
5042
5043 /* Rescale from [min..max] to [0..AMDGPU_MAX_BL_LEVEL] */
scale_input_to_fw(int min,int max,u64 input)5044 static inline u32 scale_input_to_fw(int min, int max, u64 input)
5045 {
5046 return DIV_ROUND_CLOSEST_ULL(input * AMDGPU_MAX_BL_LEVEL, max - min);
5047 }
5048
5049 /* Rescale from [0..AMDGPU_MAX_BL_LEVEL] to [min..max] */
scale_fw_to_input(int min,int max,u64 input)5050 static inline u32 scale_fw_to_input(int min, int max, u64 input)
5051 {
5052 return min + DIV_ROUND_CLOSEST_ULL(input * (max - min), AMDGPU_MAX_BL_LEVEL);
5053 }
5054
convert_custom_brightness(const struct amdgpu_dm_backlight_caps * caps,unsigned int min,unsigned int max,uint32_t * user_brightness)5055 static void convert_custom_brightness(const struct amdgpu_dm_backlight_caps *caps,
5056 unsigned int min, unsigned int max,
5057 uint32_t *user_brightness)
5058 {
5059 u32 brightness = scale_input_to_fw(min, max, *user_brightness);
5060 u8 lower_signal, upper_signal, upper_lum, lower_lum, lum;
5061 int left, right;
5062
5063 if (amdgpu_dc_debug_mask & DC_DISABLE_CUSTOM_BRIGHTNESS_CURVE)
5064 return;
5065
5066 if (!caps->data_points)
5067 return;
5068
5069 /*
5070 * Handle the case where brightness is below the first data point
5071 * Interpolate between (0,0) and (first_signal, first_lum)
5072 */
5073 if (brightness < caps->luminance_data[0].input_signal) {
5074 lum = DIV_ROUND_CLOSEST(caps->luminance_data[0].luminance * brightness,
5075 caps->luminance_data[0].input_signal);
5076 goto scale;
5077 }
5078
5079 left = 0;
5080 right = caps->data_points - 1;
5081 while (left <= right) {
5082 int mid = left + (right - left) / 2;
5083 u8 signal = caps->luminance_data[mid].input_signal;
5084
5085 /* Exact match found */
5086 if (signal == brightness) {
5087 lum = caps->luminance_data[mid].luminance;
5088 goto scale;
5089 }
5090
5091 if (signal < brightness)
5092 left = mid + 1;
5093 else
5094 right = mid - 1;
5095 }
5096
5097 /* verify bound */
5098 if (left >= caps->data_points)
5099 left = caps->data_points - 1;
5100
5101 /* At this point, left > right */
5102 lower_signal = caps->luminance_data[right].input_signal;
5103 upper_signal = caps->luminance_data[left].input_signal;
5104 lower_lum = caps->luminance_data[right].luminance;
5105 upper_lum = caps->luminance_data[left].luminance;
5106
5107 /* interpolate */
5108 if (right == left || !lower_lum)
5109 lum = upper_lum;
5110 else
5111 lum = lower_lum + DIV_ROUND_CLOSEST((upper_lum - lower_lum) *
5112 (brightness - lower_signal),
5113 upper_signal - lower_signal);
5114 scale:
5115 *user_brightness = scale_fw_to_input(min, max,
5116 DIV_ROUND_CLOSEST(lum * brightness, 101));
5117 }
5118
convert_brightness_from_user(const struct amdgpu_dm_backlight_caps * caps,uint32_t brightness)5119 static u32 convert_brightness_from_user(const struct amdgpu_dm_backlight_caps *caps,
5120 uint32_t brightness)
5121 {
5122 unsigned int min, max;
5123
5124 if (!get_brightness_range(caps, &min, &max))
5125 return brightness;
5126
5127 convert_custom_brightness(caps, min, max, &brightness);
5128
5129 // Rescale 0..max to min..max
5130 return min + DIV_ROUND_CLOSEST_ULL((u64)(max - min) * brightness, max);
5131 }
5132
convert_brightness_to_user(const struct amdgpu_dm_backlight_caps * caps,uint32_t brightness)5133 static u32 convert_brightness_to_user(const struct amdgpu_dm_backlight_caps *caps,
5134 uint32_t brightness)
5135 {
5136 unsigned int min, max;
5137
5138 if (!get_brightness_range(caps, &min, &max))
5139 return brightness;
5140
5141 if (brightness < min)
5142 return 0;
5143 // Rescale min..max to 0..max
5144 return DIV_ROUND_CLOSEST_ULL((u64)max * (brightness - min),
5145 max - min);
5146 }
5147
amdgpu_dm_backlight_set_level(struct amdgpu_display_manager * dm,int bl_idx,u32 user_brightness)5148 static void amdgpu_dm_backlight_set_level(struct amdgpu_display_manager *dm,
5149 int bl_idx,
5150 u32 user_brightness)
5151 {
5152 struct amdgpu_dm_backlight_caps *caps;
5153 struct dc_link *link;
5154 u32 brightness;
5155 bool rc, reallow_idle = false;
5156 struct drm_connector *connector;
5157
5158 list_for_each_entry(connector, &dm->ddev->mode_config.connector_list, head) {
5159 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
5160
5161 if (aconnector->bl_idx != bl_idx)
5162 continue;
5163
5164 /* if connector is off, save the brightness for next time it's on */
5165 if (!aconnector->base.encoder) {
5166 dm->brightness[bl_idx] = user_brightness;
5167 dm->actual_brightness[bl_idx] = 0;
5168 return;
5169 }
5170 }
5171
5172 amdgpu_dm_update_backlight_caps(dm, bl_idx);
5173 caps = &dm->backlight_caps[bl_idx];
5174
5175 dm->brightness[bl_idx] = user_brightness;
5176 /* update scratch register */
5177 if (bl_idx == 0)
5178 amdgpu_atombios_scratch_regs_set_backlight_level(dm->adev, dm->brightness[bl_idx]);
5179 brightness = convert_brightness_from_user(caps, dm->brightness[bl_idx]);
5180 link = (struct dc_link *)dm->backlight_link[bl_idx];
5181
5182 /* Apply brightness quirk */
5183 if (caps->brightness_mask)
5184 brightness |= caps->brightness_mask;
5185
5186 /* Change brightness based on AUX property */
5187 mutex_lock(&dm->dc_lock);
5188 if (dm->dc->caps.ips_support && dm->dc->ctx->dmub_srv->idle_allowed) {
5189 dc_allow_idle_optimizations(dm->dc, false);
5190 reallow_idle = true;
5191 }
5192
5193 if (trace_amdgpu_dm_brightness_enabled()) {
5194 trace_amdgpu_dm_brightness(__builtin_return_address(0),
5195 user_brightness,
5196 brightness,
5197 caps->aux_support,
5198 power_supply_is_system_supplied() > 0);
5199 }
5200
5201 if (caps->aux_support) {
5202 rc = dc_link_set_backlight_level_nits(link, true, brightness,
5203 AUX_BL_DEFAULT_TRANSITION_TIME_MS);
5204 if (!rc)
5205 DRM_DEBUG("DM: Failed to update backlight via AUX on eDP[%d]\n", bl_idx);
5206 } else {
5207 struct set_backlight_level_params backlight_level_params = { 0 };
5208
5209 backlight_level_params.backlight_pwm_u16_16 = brightness;
5210 backlight_level_params.transition_time_in_ms = 0;
5211
5212 rc = dc_link_set_backlight_level(link, &backlight_level_params);
5213 if (!rc)
5214 DRM_DEBUG("DM: Failed to update backlight on eDP[%d]\n", bl_idx);
5215 }
5216
5217 if (dm->dc->caps.ips_support && reallow_idle)
5218 dc_allow_idle_optimizations(dm->dc, true);
5219
5220 mutex_unlock(&dm->dc_lock);
5221
5222 if (rc)
5223 dm->actual_brightness[bl_idx] = user_brightness;
5224 }
5225
amdgpu_dm_backlight_update_status(struct backlight_device * bd)5226 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
5227 {
5228 struct amdgpu_display_manager *dm = bl_get_data(bd);
5229 int i;
5230
5231 for (i = 0; i < dm->num_of_edps; i++) {
5232 if (bd == dm->backlight_dev[i])
5233 break;
5234 }
5235 if (i >= AMDGPU_DM_MAX_NUM_EDP)
5236 i = 0;
5237 amdgpu_dm_backlight_set_level(dm, i, bd->props.brightness);
5238
5239 return 0;
5240 }
5241
amdgpu_dm_backlight_get_level(struct amdgpu_display_manager * dm,int bl_idx)5242 static u32 amdgpu_dm_backlight_get_level(struct amdgpu_display_manager *dm,
5243 int bl_idx)
5244 {
5245 int ret;
5246 struct amdgpu_dm_backlight_caps caps;
5247 struct dc_link *link = (struct dc_link *)dm->backlight_link[bl_idx];
5248
5249 amdgpu_dm_update_backlight_caps(dm, bl_idx);
5250 caps = dm->backlight_caps[bl_idx];
5251
5252 if (caps.aux_support) {
5253 u32 avg, peak;
5254
5255 if (!dc_link_get_backlight_level_nits(link, &avg, &peak))
5256 return dm->brightness[bl_idx];
5257 return convert_brightness_to_user(&caps, avg);
5258 }
5259
5260 ret = dc_link_get_backlight_level(link);
5261
5262 if (ret == DC_ERROR_UNEXPECTED)
5263 return dm->brightness[bl_idx];
5264
5265 return convert_brightness_to_user(&caps, ret);
5266 }
5267
amdgpu_dm_backlight_get_brightness(struct backlight_device * bd)5268 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
5269 {
5270 struct amdgpu_display_manager *dm = bl_get_data(bd);
5271 int i;
5272
5273 for (i = 0; i < dm->num_of_edps; i++) {
5274 if (bd == dm->backlight_dev[i])
5275 break;
5276 }
5277 if (i >= AMDGPU_DM_MAX_NUM_EDP)
5278 i = 0;
5279 return amdgpu_dm_backlight_get_level(dm, i);
5280 }
5281
5282 static const struct backlight_ops amdgpu_dm_backlight_ops = {
5283 .options = BL_CORE_SUSPENDRESUME,
5284 .get_brightness = amdgpu_dm_backlight_get_brightness,
5285 .update_status = amdgpu_dm_backlight_update_status,
5286 };
5287
5288 static void
amdgpu_dm_register_backlight_device(struct amdgpu_dm_connector * aconnector)5289 amdgpu_dm_register_backlight_device(struct amdgpu_dm_connector *aconnector)
5290 {
5291 struct drm_device *drm = aconnector->base.dev;
5292 struct amdgpu_display_manager *dm = &drm_to_adev(drm)->dm;
5293 struct backlight_properties props = { 0 };
5294 struct amdgpu_dm_backlight_caps *caps;
5295 char bl_name[16];
5296 int min, max;
5297 int real_brightness;
5298 int init_brightness;
5299
5300 if (aconnector->bl_idx == -1)
5301 return;
5302
5303 if (!acpi_video_backlight_use_native()) {
5304 drm_info(drm, "Skipping amdgpu DM backlight registration\n");
5305 /* Try registering an ACPI video backlight device instead. */
5306 acpi_video_register_backlight();
5307 return;
5308 }
5309
5310 caps = &dm->backlight_caps[aconnector->bl_idx];
5311 if (get_brightness_range(caps, &min, &max)) {
5312 if (power_supply_is_system_supplied() > 0)
5313 props.brightness = DIV_ROUND_CLOSEST((max - min) * caps->ac_level, 100);
5314 else
5315 props.brightness = DIV_ROUND_CLOSEST((max - min) * caps->dc_level, 100);
5316 /* min is zero, so max needs to be adjusted */
5317 props.max_brightness = max - min;
5318 drm_dbg(drm, "Backlight caps: min: %d, max: %d, ac %d, dc %d\n", min, max,
5319 caps->ac_level, caps->dc_level);
5320 } else
5321 props.brightness = props.max_brightness = MAX_BACKLIGHT_LEVEL;
5322
5323 init_brightness = props.brightness;
5324
5325 if (caps->data_points && !(amdgpu_dc_debug_mask & DC_DISABLE_CUSTOM_BRIGHTNESS_CURVE)) {
5326 drm_info(drm, "Using custom brightness curve\n");
5327 props.scale = BACKLIGHT_SCALE_NON_LINEAR;
5328 } else
5329 props.scale = BACKLIGHT_SCALE_LINEAR;
5330 props.type = BACKLIGHT_RAW;
5331
5332 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
5333 drm->primary->index + aconnector->bl_idx);
5334
5335 dm->backlight_dev[aconnector->bl_idx] =
5336 backlight_device_register(bl_name, aconnector->base.kdev, dm,
5337 &amdgpu_dm_backlight_ops, &props);
5338 dm->brightness[aconnector->bl_idx] = props.brightness;
5339
5340 if (IS_ERR(dm->backlight_dev[aconnector->bl_idx])) {
5341 drm_err(drm, "DM: Backlight registration failed!\n");
5342 dm->backlight_dev[aconnector->bl_idx] = NULL;
5343 } else {
5344 /*
5345 * dm->brightness[x] can be inconsistent just after startup until
5346 * ops.get_brightness is called.
5347 */
5348 real_brightness =
5349 amdgpu_dm_backlight_ops.get_brightness(dm->backlight_dev[aconnector->bl_idx]);
5350
5351 if (real_brightness != init_brightness) {
5352 dm->actual_brightness[aconnector->bl_idx] = real_brightness;
5353 dm->brightness[aconnector->bl_idx] = real_brightness;
5354 }
5355 drm_dbg_driver(drm, "DM: Registered Backlight device: %s\n", bl_name);
5356 }
5357 }
5358
initialize_plane(struct amdgpu_display_manager * dm,struct amdgpu_mode_info * mode_info,int plane_id,enum drm_plane_type plane_type,const struct dc_plane_cap * plane_cap)5359 static int initialize_plane(struct amdgpu_display_manager *dm,
5360 struct amdgpu_mode_info *mode_info, int plane_id,
5361 enum drm_plane_type plane_type,
5362 const struct dc_plane_cap *plane_cap)
5363 {
5364 struct drm_plane *plane;
5365 unsigned long possible_crtcs;
5366 int ret = 0;
5367
5368 plane = kzalloc_obj(struct drm_plane);
5369 if (!plane) {
5370 drm_err(adev_to_drm(dm->adev), "KMS: Failed to allocate plane\n");
5371 return -ENOMEM;
5372 }
5373 plane->type = plane_type;
5374
5375 /*
5376 * HACK: IGT tests expect that the primary plane for a CRTC
5377 * can only have one possible CRTC. Only expose support for
5378 * any CRTC if they're not going to be used as a primary plane
5379 * for a CRTC - like overlay or underlay planes.
5380 */
5381 possible_crtcs = 1 << plane_id;
5382 if (plane_id >= dm->dc->caps.max_streams)
5383 possible_crtcs = 0xff;
5384
5385 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap);
5386
5387 if (ret) {
5388 drm_err(adev_to_drm(dm->adev), "KMS: Failed to initialize plane\n");
5389 kfree(plane);
5390 return ret;
5391 }
5392
5393 if (mode_info)
5394 mode_info->planes[plane_id] = plane;
5395
5396 return ret;
5397 }
5398
5399
setup_backlight_device(struct amdgpu_display_manager * dm,struct amdgpu_dm_connector * aconnector)5400 static void setup_backlight_device(struct amdgpu_display_manager *dm,
5401 struct amdgpu_dm_connector *aconnector)
5402 {
5403 struct amdgpu_dm_backlight_caps *caps;
5404 struct dc_link *link = aconnector->dc_link;
5405 int bl_idx = dm->num_of_edps;
5406
5407 if (!(link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) ||
5408 link->type == dc_connection_none)
5409 return;
5410
5411 if (dm->num_of_edps >= AMDGPU_DM_MAX_NUM_EDP) {
5412 drm_warn(adev_to_drm(dm->adev), "Too much eDP connections, skipping backlight setup for additional eDPs\n");
5413 return;
5414 }
5415
5416 aconnector->bl_idx = bl_idx;
5417
5418 amdgpu_dm_update_backlight_caps(dm, bl_idx);
5419 dm->backlight_link[bl_idx] = link;
5420 dm->num_of_edps++;
5421
5422 update_connector_ext_caps(aconnector);
5423 caps = &dm->backlight_caps[aconnector->bl_idx];
5424
5425 /* Only offer ABM property when non-OLED and user didn't turn off by module parameter */
5426 if (caps->ext_caps && !caps->ext_caps->bits.oled && amdgpu_dm_abm_level < 0)
5427 drm_object_attach_property(&aconnector->base.base,
5428 dm->adev->mode_info.abm_level_property,
5429 ABM_SYSFS_CONTROL);
5430 }
5431
5432 static void amdgpu_set_panel_orientation(struct drm_connector *connector);
5433
5434 /*
5435 * In this architecture, the association
5436 * connector -> encoder -> crtc
5437 * id not really requried. The crtc and connector will hold the
5438 * display_index as an abstraction to use with DAL component
5439 *
5440 * Returns 0 on success
5441 */
amdgpu_dm_initialize_drm_device(struct amdgpu_device * adev)5442 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
5443 {
5444 struct amdgpu_display_manager *dm = &adev->dm;
5445 s32 i;
5446 struct amdgpu_dm_connector *aconnector = NULL;
5447 struct amdgpu_encoder *aencoder = NULL;
5448 struct amdgpu_mode_info *mode_info = &adev->mode_info;
5449 u32 link_cnt;
5450 s32 primary_planes;
5451 enum dc_connection_type new_connection_type = dc_connection_none;
5452 const struct dc_plane_cap *plane;
5453 bool psr_feature_enabled = false;
5454 bool replay_feature_enabled = false;
5455 int max_overlay = dm->dc->caps.max_slave_planes;
5456
5457 dm->display_indexes_num = dm->dc->caps.max_streams;
5458 /* Update the actual used number of crtc */
5459 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
5460
5461 amdgpu_dm_set_irq_funcs(adev);
5462
5463 link_cnt = dm->dc->caps.max_links;
5464 if (amdgpu_dm_mode_config_init(dm->adev)) {
5465 drm_err(adev_to_drm(adev), "DM: Failed to initialize mode config\n");
5466 return -EINVAL;
5467 }
5468
5469 /* There is one primary plane per CRTC */
5470 primary_planes = dm->dc->caps.max_streams;
5471 if (primary_planes > AMDGPU_MAX_PLANES) {
5472 drm_err(adev_to_drm(adev), "DM: Plane nums out of 6 planes\n");
5473 return -EINVAL;
5474 }
5475
5476 /*
5477 * Initialize primary planes, implicit planes for legacy IOCTLS.
5478 * Order is reversed to match iteration order in atomic check.
5479 */
5480 for (i = (primary_planes - 1); i >= 0; i--) {
5481 plane = &dm->dc->caps.planes[i];
5482
5483 if (initialize_plane(dm, mode_info, i,
5484 DRM_PLANE_TYPE_PRIMARY, plane)) {
5485 drm_err(adev_to_drm(adev), "KMS: Failed to initialize primary plane\n");
5486 goto fail;
5487 }
5488 }
5489
5490 /*
5491 * Initialize overlay planes, index starting after primary planes.
5492 * These planes have a higher DRM index than the primary planes since
5493 * they should be considered as having a higher z-order.
5494 * Order is reversed to match iteration order in atomic check.
5495 *
5496 * Only support DCN for now, and only expose one so we don't encourage
5497 * userspace to use up all the pipes.
5498 */
5499 for (i = 0; i < dm->dc->caps.max_planes; ++i) {
5500 struct dc_plane_cap *plane = &dm->dc->caps.planes[i];
5501
5502 /* Do not create overlay if MPO disabled */
5503 if (amdgpu_dc_debug_mask & DC_DISABLE_MPO)
5504 break;
5505
5506 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL)
5507 continue;
5508
5509 if (!plane->pixel_format_support.argb8888)
5510 continue;
5511
5512 if (max_overlay-- == 0)
5513 break;
5514
5515 if (initialize_plane(dm, NULL, primary_planes + i,
5516 DRM_PLANE_TYPE_OVERLAY, plane)) {
5517 drm_err(adev_to_drm(adev), "KMS: Failed to initialize overlay plane\n");
5518 goto fail;
5519 }
5520 }
5521
5522 for (i = 0; i < dm->dc->caps.max_streams; i++)
5523 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) {
5524 drm_err(adev_to_drm(adev), "KMS: Failed to initialize crtc\n");
5525 goto fail;
5526 }
5527
5528 /* Use Outbox interrupt */
5529 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5530 case IP_VERSION(3, 0, 0):
5531 case IP_VERSION(3, 1, 2):
5532 case IP_VERSION(3, 1, 3):
5533 case IP_VERSION(3, 1, 4):
5534 case IP_VERSION(3, 1, 5):
5535 case IP_VERSION(3, 1, 6):
5536 case IP_VERSION(3, 2, 0):
5537 case IP_VERSION(3, 2, 1):
5538 case IP_VERSION(2, 1, 0):
5539 case IP_VERSION(3, 5, 0):
5540 case IP_VERSION(3, 5, 1):
5541 case IP_VERSION(3, 6, 0):
5542 case IP_VERSION(4, 0, 1):
5543 if (register_outbox_irq_handlers(dm->adev)) {
5544 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5545 goto fail;
5546 }
5547 break;
5548 default:
5549 drm_dbg_kms(adev_to_drm(adev), "Unsupported DCN IP version for outbox: 0x%X\n",
5550 amdgpu_ip_version(adev, DCE_HWIP, 0));
5551 }
5552
5553 /* Determine whether to enable PSR support by default. */
5554 if (!(amdgpu_dc_debug_mask & DC_DISABLE_PSR)) {
5555 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5556 case IP_VERSION(3, 1, 2):
5557 case IP_VERSION(3, 1, 3):
5558 case IP_VERSION(3, 1, 4):
5559 case IP_VERSION(3, 1, 5):
5560 case IP_VERSION(3, 1, 6):
5561 case IP_VERSION(3, 2, 0):
5562 case IP_VERSION(3, 2, 1):
5563 case IP_VERSION(3, 5, 0):
5564 case IP_VERSION(3, 5, 1):
5565 case IP_VERSION(3, 6, 0):
5566 case IP_VERSION(4, 0, 1):
5567 psr_feature_enabled = true;
5568 break;
5569 default:
5570 psr_feature_enabled = amdgpu_dc_feature_mask & DC_PSR_MASK;
5571 break;
5572 }
5573 }
5574
5575 /* Determine whether to enable Replay support by default. */
5576 if (!(amdgpu_dc_debug_mask & DC_DISABLE_REPLAY)) {
5577 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5578 case IP_VERSION(3, 1, 4):
5579 case IP_VERSION(3, 2, 0):
5580 case IP_VERSION(3, 2, 1):
5581 case IP_VERSION(3, 5, 0):
5582 case IP_VERSION(3, 5, 1):
5583 case IP_VERSION(3, 6, 0):
5584 replay_feature_enabled = true;
5585 break;
5586
5587 default:
5588 replay_feature_enabled = amdgpu_dc_feature_mask & DC_REPLAY_MASK;
5589 break;
5590 }
5591 }
5592
5593 if (link_cnt > MAX_LINKS) {
5594 drm_err(adev_to_drm(adev),
5595 "KMS: Cannot support more than %d display indexes\n",
5596 MAX_LINKS);
5597 goto fail;
5598 }
5599
5600 /* loops over all connectors on the board */
5601 for (i = 0; i < link_cnt; i++) {
5602 struct dc_link *link = NULL;
5603
5604 link = dc_get_link_at_index(dm->dc, i);
5605
5606 if (link->connector_signal == SIGNAL_TYPE_VIRTUAL) {
5607 struct amdgpu_dm_wb_connector *wbcon = kzalloc_obj(*wbcon);
5608
5609 if (!wbcon) {
5610 drm_err(adev_to_drm(adev), "KMS: Failed to allocate writeback connector\n");
5611 continue;
5612 }
5613
5614 if (amdgpu_dm_wb_connector_init(dm, wbcon, i)) {
5615 drm_err(adev_to_drm(adev), "KMS: Failed to initialize writeback connector\n");
5616 kfree(wbcon);
5617 continue;
5618 }
5619
5620 link->psr_settings.psr_feature_enabled = false;
5621 link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
5622
5623 continue;
5624 }
5625
5626 aconnector = kzalloc_obj(*aconnector);
5627 if (!aconnector)
5628 goto fail;
5629
5630 aencoder = kzalloc_obj(*aencoder);
5631 if (!aencoder)
5632 goto fail;
5633
5634 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
5635 drm_err(adev_to_drm(adev), "KMS: Failed to initialize encoder\n");
5636 goto fail;
5637 }
5638
5639 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
5640 drm_err(adev_to_drm(adev), "KMS: Failed to initialize connector\n");
5641 goto fail;
5642 }
5643
5644 if (dm->hpd_rx_offload_wq)
5645 dm->hpd_rx_offload_wq[aconnector->base.index].aconnector =
5646 aconnector;
5647
5648 if (!dc_link_detect_connection_type(link, &new_connection_type))
5649 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
5650
5651 if (aconnector->base.force && new_connection_type == dc_connection_none) {
5652 emulated_link_detect(link);
5653 amdgpu_dm_update_connector_after_detect(aconnector);
5654 } else {
5655 bool ret = false;
5656
5657 mutex_lock(&dm->dc_lock);
5658 dc_exit_ips_for_hw_access(dm->dc);
5659 ret = dc_link_detect(link, DETECT_REASON_BOOT);
5660 mutex_unlock(&dm->dc_lock);
5661
5662 if (ret) {
5663 amdgpu_dm_update_connector_after_detect(aconnector);
5664 setup_backlight_device(dm, aconnector);
5665
5666 /* Disable PSR if Replay can be enabled */
5667 if (replay_feature_enabled)
5668 if (amdgpu_dm_set_replay_caps(link, aconnector))
5669 psr_feature_enabled = false;
5670
5671 if (psr_feature_enabled) {
5672 amdgpu_dm_set_psr_caps(link);
5673 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",
5674 aconnector->base.name,
5675 link->psr_settings.psr_feature_enabled,
5676 link->psr_settings.psr_version,
5677 link->dpcd_caps.psr_info.psr_version,
5678 link->dpcd_caps.psr_info.psr_dpcd_caps.raw,
5679 link->dpcd_caps.psr_info.psr2_su_y_granularity_cap);
5680 }
5681 }
5682 }
5683 amdgpu_set_panel_orientation(&aconnector->base);
5684 }
5685
5686 /* Debug dump: list all DC links and their associated sinks after detection
5687 * is complete for all connectors. This provides a comprehensive view of the
5688 * final state without repeating the dump for each connector.
5689 */
5690 amdgpu_dm_dump_links_and_sinks(adev);
5691
5692 /* Software is initialized. Now we can register interrupt handlers. */
5693 switch (adev->asic_type) {
5694 #if defined(CONFIG_DRM_AMD_DC_SI)
5695 case CHIP_TAHITI:
5696 case CHIP_PITCAIRN:
5697 case CHIP_VERDE:
5698 case CHIP_OLAND:
5699 if (dce60_register_irq_handlers(dm->adev)) {
5700 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5701 goto fail;
5702 }
5703 break;
5704 #endif
5705 case CHIP_BONAIRE:
5706 case CHIP_HAWAII:
5707 case CHIP_KAVERI:
5708 case CHIP_KABINI:
5709 case CHIP_MULLINS:
5710 case CHIP_TONGA:
5711 case CHIP_FIJI:
5712 case CHIP_CARRIZO:
5713 case CHIP_STONEY:
5714 case CHIP_POLARIS11:
5715 case CHIP_POLARIS10:
5716 case CHIP_POLARIS12:
5717 case CHIP_VEGAM:
5718 case CHIP_VEGA10:
5719 case CHIP_VEGA12:
5720 case CHIP_VEGA20:
5721 if (dce110_register_irq_handlers(dm->adev)) {
5722 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5723 goto fail;
5724 }
5725 break;
5726 default:
5727 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5728 case IP_VERSION(1, 0, 0):
5729 case IP_VERSION(1, 0, 1):
5730 case IP_VERSION(2, 0, 2):
5731 case IP_VERSION(2, 0, 3):
5732 case IP_VERSION(2, 0, 0):
5733 case IP_VERSION(2, 1, 0):
5734 case IP_VERSION(3, 0, 0):
5735 case IP_VERSION(3, 0, 2):
5736 case IP_VERSION(3, 0, 3):
5737 case IP_VERSION(3, 0, 1):
5738 case IP_VERSION(3, 1, 2):
5739 case IP_VERSION(3, 1, 3):
5740 case IP_VERSION(3, 1, 4):
5741 case IP_VERSION(3, 1, 5):
5742 case IP_VERSION(3, 1, 6):
5743 case IP_VERSION(3, 2, 0):
5744 case IP_VERSION(3, 2, 1):
5745 case IP_VERSION(3, 5, 0):
5746 case IP_VERSION(3, 5, 1):
5747 case IP_VERSION(3, 6, 0):
5748 case IP_VERSION(4, 0, 1):
5749 if (dcn10_register_irq_handlers(dm->adev)) {
5750 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5751 goto fail;
5752 }
5753 break;
5754 default:
5755 drm_err(adev_to_drm(adev), "Unsupported DCE IP versions: 0x%X\n",
5756 amdgpu_ip_version(adev, DCE_HWIP, 0));
5757 goto fail;
5758 }
5759 break;
5760 }
5761
5762 return 0;
5763 fail:
5764 kfree(aencoder);
5765 kfree(aconnector);
5766
5767 return -EINVAL;
5768 }
5769
amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager * dm)5770 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
5771 {
5772 if (dm->atomic_obj.state)
5773 drm_atomic_private_obj_fini(&dm->atomic_obj);
5774 }
5775
5776 /******************************************************************************
5777 * amdgpu_display_funcs functions
5778 *****************************************************************************/
5779
5780 /*
5781 * dm_bandwidth_update - program display watermarks
5782 *
5783 * @adev: amdgpu_device pointer
5784 *
5785 * Calculate and program the display watermarks and line buffer allocation.
5786 */
dm_bandwidth_update(struct amdgpu_device * adev)5787 static void dm_bandwidth_update(struct amdgpu_device *adev)
5788 {
5789 /* TODO: implement later */
5790 }
5791
5792 static const struct amdgpu_display_funcs dm_display_funcs = {
5793 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
5794 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
5795 .backlight_set_level = NULL, /* never called for DC */
5796 .backlight_get_level = NULL, /* never called for DC */
5797 .hpd_sense = NULL,/* called unconditionally */
5798 .hpd_set_polarity = NULL, /* called unconditionally */
5799 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
5800 .page_flip_get_scanoutpos =
5801 dm_crtc_get_scanoutpos,/* called unconditionally */
5802 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
5803 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
5804 };
5805
5806 #if defined(CONFIG_DEBUG_KERNEL_DC)
5807
s3_debug_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)5808 static ssize_t s3_debug_store(struct device *device,
5809 struct device_attribute *attr,
5810 const char *buf,
5811 size_t count)
5812 {
5813 int ret;
5814 int s3_state;
5815 struct drm_device *drm_dev = dev_get_drvdata(device);
5816 struct amdgpu_device *adev = drm_to_adev(drm_dev);
5817 struct amdgpu_ip_block *ip_block;
5818
5819 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_DCE);
5820 if (!ip_block)
5821 return -EINVAL;
5822
5823 ret = kstrtoint(buf, 0, &s3_state);
5824
5825 if (ret == 0) {
5826 if (s3_state) {
5827 dm_resume(ip_block);
5828 drm_kms_helper_hotplug_event(adev_to_drm(adev));
5829 } else
5830 dm_suspend(ip_block);
5831 }
5832
5833 return ret == 0 ? count : 0;
5834 }
5835
5836 DEVICE_ATTR_WO(s3_debug);
5837
5838 #endif
5839
dm_init_microcode(struct amdgpu_device * adev)5840 static int dm_init_microcode(struct amdgpu_device *adev)
5841 {
5842 char *fw_name_dmub;
5843 int r;
5844
5845 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5846 case IP_VERSION(2, 1, 0):
5847 fw_name_dmub = FIRMWARE_RENOIR_DMUB;
5848 if (ASICREV_IS_GREEN_SARDINE(adev->external_rev_id))
5849 fw_name_dmub = FIRMWARE_GREEN_SARDINE_DMUB;
5850 break;
5851 case IP_VERSION(3, 0, 0):
5852 if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(10, 3, 0))
5853 fw_name_dmub = FIRMWARE_SIENNA_CICHLID_DMUB;
5854 else
5855 fw_name_dmub = FIRMWARE_NAVY_FLOUNDER_DMUB;
5856 break;
5857 case IP_VERSION(3, 0, 1):
5858 fw_name_dmub = FIRMWARE_VANGOGH_DMUB;
5859 break;
5860 case IP_VERSION(3, 0, 2):
5861 fw_name_dmub = FIRMWARE_DIMGREY_CAVEFISH_DMUB;
5862 break;
5863 case IP_VERSION(3, 0, 3):
5864 fw_name_dmub = FIRMWARE_BEIGE_GOBY_DMUB;
5865 break;
5866 case IP_VERSION(3, 1, 2):
5867 case IP_VERSION(3, 1, 3):
5868 fw_name_dmub = FIRMWARE_YELLOW_CARP_DMUB;
5869 break;
5870 case IP_VERSION(3, 1, 4):
5871 fw_name_dmub = FIRMWARE_DCN_314_DMUB;
5872 break;
5873 case IP_VERSION(3, 1, 5):
5874 fw_name_dmub = FIRMWARE_DCN_315_DMUB;
5875 break;
5876 case IP_VERSION(3, 1, 6):
5877 fw_name_dmub = FIRMWARE_DCN316_DMUB;
5878 break;
5879 case IP_VERSION(3, 2, 0):
5880 fw_name_dmub = FIRMWARE_DCN_V3_2_0_DMCUB;
5881 break;
5882 case IP_VERSION(3, 2, 1):
5883 fw_name_dmub = FIRMWARE_DCN_V3_2_1_DMCUB;
5884 break;
5885 case IP_VERSION(3, 5, 0):
5886 fw_name_dmub = FIRMWARE_DCN_35_DMUB;
5887 break;
5888 case IP_VERSION(3, 5, 1):
5889 fw_name_dmub = FIRMWARE_DCN_351_DMUB;
5890 break;
5891 case IP_VERSION(3, 6, 0):
5892 fw_name_dmub = FIRMWARE_DCN_36_DMUB;
5893 break;
5894 case IP_VERSION(4, 0, 1):
5895 fw_name_dmub = FIRMWARE_DCN_401_DMUB;
5896 break;
5897 default:
5898 /* ASIC doesn't support DMUB. */
5899 return 0;
5900 }
5901 r = amdgpu_ucode_request(adev, &adev->dm.dmub_fw, AMDGPU_UCODE_REQUIRED,
5902 "%s", fw_name_dmub);
5903 return r;
5904 }
5905
dm_early_init(struct amdgpu_ip_block * ip_block)5906 static int dm_early_init(struct amdgpu_ip_block *ip_block)
5907 {
5908 struct amdgpu_device *adev = ip_block->adev;
5909 struct amdgpu_mode_info *mode_info = &adev->mode_info;
5910 struct atom_context *ctx = mode_info->atom_context;
5911 int index = GetIndexIntoMasterTable(DATA, Object_Header);
5912 u16 data_offset;
5913
5914 /* if there is no object header, skip DM */
5915 if (!amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
5916 adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK;
5917 drm_info(adev_to_drm(adev), "No object header, skipping DM\n");
5918 return -ENOENT;
5919 }
5920
5921 switch (adev->asic_type) {
5922 #if defined(CONFIG_DRM_AMD_DC_SI)
5923 case CHIP_TAHITI:
5924 case CHIP_PITCAIRN:
5925 case CHIP_VERDE:
5926 adev->mode_info.num_crtc = 6;
5927 adev->mode_info.num_hpd = 6;
5928 adev->mode_info.num_dig = 6;
5929 break;
5930 case CHIP_OLAND:
5931 adev->mode_info.num_crtc = 2;
5932 adev->mode_info.num_hpd = 2;
5933 adev->mode_info.num_dig = 2;
5934 break;
5935 #endif
5936 case CHIP_BONAIRE:
5937 case CHIP_HAWAII:
5938 adev->mode_info.num_crtc = 6;
5939 adev->mode_info.num_hpd = 6;
5940 adev->mode_info.num_dig = 6;
5941 break;
5942 case CHIP_KAVERI:
5943 adev->mode_info.num_crtc = 4;
5944 adev->mode_info.num_hpd = 6;
5945 adev->mode_info.num_dig = 7;
5946 break;
5947 case CHIP_KABINI:
5948 case CHIP_MULLINS:
5949 adev->mode_info.num_crtc = 2;
5950 adev->mode_info.num_hpd = 6;
5951 adev->mode_info.num_dig = 6;
5952 break;
5953 case CHIP_FIJI:
5954 case CHIP_TONGA:
5955 adev->mode_info.num_crtc = 6;
5956 adev->mode_info.num_hpd = 6;
5957 adev->mode_info.num_dig = 7;
5958 break;
5959 case CHIP_CARRIZO:
5960 adev->mode_info.num_crtc = 3;
5961 adev->mode_info.num_hpd = 6;
5962 adev->mode_info.num_dig = 9;
5963 break;
5964 case CHIP_STONEY:
5965 adev->mode_info.num_crtc = 2;
5966 adev->mode_info.num_hpd = 6;
5967 adev->mode_info.num_dig = 9;
5968 break;
5969 case CHIP_POLARIS11:
5970 case CHIP_POLARIS12:
5971 adev->mode_info.num_crtc = 5;
5972 adev->mode_info.num_hpd = 5;
5973 adev->mode_info.num_dig = 5;
5974 break;
5975 case CHIP_POLARIS10:
5976 case CHIP_VEGAM:
5977 adev->mode_info.num_crtc = 6;
5978 adev->mode_info.num_hpd = 6;
5979 adev->mode_info.num_dig = 6;
5980 break;
5981 case CHIP_VEGA10:
5982 case CHIP_VEGA12:
5983 case CHIP_VEGA20:
5984 adev->mode_info.num_crtc = 6;
5985 adev->mode_info.num_hpd = 6;
5986 adev->mode_info.num_dig = 6;
5987 break;
5988 default:
5989
5990 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5991 case IP_VERSION(2, 0, 2):
5992 case IP_VERSION(3, 0, 0):
5993 adev->mode_info.num_crtc = 6;
5994 adev->mode_info.num_hpd = 6;
5995 adev->mode_info.num_dig = 6;
5996 break;
5997 case IP_VERSION(2, 0, 0):
5998 case IP_VERSION(3, 0, 2):
5999 adev->mode_info.num_crtc = 5;
6000 adev->mode_info.num_hpd = 5;
6001 adev->mode_info.num_dig = 5;
6002 break;
6003 case IP_VERSION(2, 0, 3):
6004 case IP_VERSION(3, 0, 3):
6005 adev->mode_info.num_crtc = 2;
6006 adev->mode_info.num_hpd = 2;
6007 adev->mode_info.num_dig = 2;
6008 break;
6009 case IP_VERSION(1, 0, 0):
6010 case IP_VERSION(1, 0, 1):
6011 case IP_VERSION(3, 0, 1):
6012 case IP_VERSION(2, 1, 0):
6013 case IP_VERSION(3, 1, 2):
6014 case IP_VERSION(3, 1, 3):
6015 case IP_VERSION(3, 1, 4):
6016 case IP_VERSION(3, 1, 5):
6017 case IP_VERSION(3, 1, 6):
6018 case IP_VERSION(3, 2, 0):
6019 case IP_VERSION(3, 2, 1):
6020 case IP_VERSION(3, 5, 0):
6021 case IP_VERSION(3, 5, 1):
6022 case IP_VERSION(3, 6, 0):
6023 case IP_VERSION(4, 0, 1):
6024 adev->mode_info.num_crtc = 4;
6025 adev->mode_info.num_hpd = 4;
6026 adev->mode_info.num_dig = 4;
6027 break;
6028 default:
6029 drm_err(adev_to_drm(adev), "Unsupported DCE IP versions: 0x%x\n",
6030 amdgpu_ip_version(adev, DCE_HWIP, 0));
6031 return -EINVAL;
6032 }
6033 break;
6034 }
6035
6036 if (adev->mode_info.funcs == NULL)
6037 adev->mode_info.funcs = &dm_display_funcs;
6038
6039 /*
6040 * Note: Do NOT change adev->audio_endpt_rreg and
6041 * adev->audio_endpt_wreg because they are initialised in
6042 * amdgpu_device_init()
6043 */
6044 #if defined(CONFIG_DEBUG_KERNEL_DC)
6045 device_create_file(
6046 adev_to_drm(adev)->dev,
6047 &dev_attr_s3_debug);
6048 #endif
6049 adev->dc_enabled = true;
6050
6051 return dm_init_microcode(adev);
6052 }
6053
modereset_required(struct drm_crtc_state * crtc_state)6054 static bool modereset_required(struct drm_crtc_state *crtc_state)
6055 {
6056 return !crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state);
6057 }
6058
amdgpu_dm_encoder_destroy(struct drm_encoder * encoder)6059 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
6060 {
6061 drm_encoder_cleanup(encoder);
6062 kfree(encoder);
6063 }
6064
6065 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
6066 .destroy = amdgpu_dm_encoder_destroy,
6067 };
6068
6069 static int
fill_plane_color_attributes(const struct drm_plane_state * plane_state,const enum surface_pixel_format format,enum dc_color_space * color_space)6070 fill_plane_color_attributes(const struct drm_plane_state *plane_state,
6071 const enum surface_pixel_format format,
6072 enum dc_color_space *color_space)
6073 {
6074 bool full_range;
6075
6076 *color_space = COLOR_SPACE_SRGB;
6077
6078 /* Ignore properties when DRM_CLIENT_CAP_PLANE_COLOR_PIPELINE is set */
6079 if (plane_state->state && plane_state->state->plane_color_pipeline)
6080 return 0;
6081
6082 /* DRM color properties only affect non-RGB formats. */
6083 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
6084 return 0;
6085
6086 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE);
6087
6088 switch (plane_state->color_encoding) {
6089 case DRM_COLOR_YCBCR_BT601:
6090 if (full_range)
6091 *color_space = COLOR_SPACE_YCBCR601;
6092 else
6093 *color_space = COLOR_SPACE_YCBCR601_LIMITED;
6094 break;
6095
6096 case DRM_COLOR_YCBCR_BT709:
6097 if (full_range)
6098 *color_space = COLOR_SPACE_YCBCR709;
6099 else
6100 *color_space = COLOR_SPACE_YCBCR709_LIMITED;
6101 break;
6102
6103 case DRM_COLOR_YCBCR_BT2020:
6104 if (full_range)
6105 *color_space = COLOR_SPACE_2020_YCBCR_FULL;
6106 else
6107 *color_space = COLOR_SPACE_2020_YCBCR_LIMITED;
6108 break;
6109
6110 default:
6111 return -EINVAL;
6112 }
6113
6114 return 0;
6115 }
6116
6117 static int
fill_dc_plane_info_and_addr(struct amdgpu_device * adev,const struct drm_plane_state * plane_state,const u64 tiling_flags,struct dc_plane_info * plane_info,struct dc_plane_address * address,bool tmz_surface)6118 fill_dc_plane_info_and_addr(struct amdgpu_device *adev,
6119 const struct drm_plane_state *plane_state,
6120 const u64 tiling_flags,
6121 struct dc_plane_info *plane_info,
6122 struct dc_plane_address *address,
6123 bool tmz_surface)
6124 {
6125 const struct drm_framebuffer *fb = plane_state->fb;
6126 const struct amdgpu_framebuffer *afb =
6127 to_amdgpu_framebuffer(plane_state->fb);
6128 int ret;
6129
6130 memset(plane_info, 0, sizeof(*plane_info));
6131
6132 switch (fb->format->format) {
6133 case DRM_FORMAT_C8:
6134 plane_info->format =
6135 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
6136 break;
6137 case DRM_FORMAT_RGB565:
6138 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
6139 break;
6140 case DRM_FORMAT_XRGB8888:
6141 case DRM_FORMAT_ARGB8888:
6142 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
6143 break;
6144 case DRM_FORMAT_XRGB2101010:
6145 case DRM_FORMAT_ARGB2101010:
6146 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
6147 break;
6148 case DRM_FORMAT_XBGR2101010:
6149 case DRM_FORMAT_ABGR2101010:
6150 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
6151 break;
6152 case DRM_FORMAT_XBGR8888:
6153 case DRM_FORMAT_ABGR8888:
6154 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888;
6155 break;
6156 case DRM_FORMAT_NV21:
6157 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
6158 break;
6159 case DRM_FORMAT_NV12:
6160 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
6161 break;
6162 case DRM_FORMAT_P010:
6163 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb;
6164 break;
6165 case DRM_FORMAT_XRGB16161616F:
6166 case DRM_FORMAT_ARGB16161616F:
6167 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F;
6168 break;
6169 case DRM_FORMAT_XBGR16161616F:
6170 case DRM_FORMAT_ABGR16161616F:
6171 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F;
6172 break;
6173 case DRM_FORMAT_XRGB16161616:
6174 case DRM_FORMAT_ARGB16161616:
6175 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616;
6176 break;
6177 case DRM_FORMAT_XBGR16161616:
6178 case DRM_FORMAT_ABGR16161616:
6179 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616;
6180 break;
6181 default:
6182 drm_err(adev_to_drm(adev),
6183 "Unsupported screen format %p4cc\n",
6184 &fb->format->format);
6185 return -EINVAL;
6186 }
6187
6188 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
6189 case DRM_MODE_ROTATE_0:
6190 plane_info->rotation = ROTATION_ANGLE_0;
6191 break;
6192 case DRM_MODE_ROTATE_90:
6193 plane_info->rotation = ROTATION_ANGLE_90;
6194 break;
6195 case DRM_MODE_ROTATE_180:
6196 plane_info->rotation = ROTATION_ANGLE_180;
6197 break;
6198 case DRM_MODE_ROTATE_270:
6199 plane_info->rotation = ROTATION_ANGLE_270;
6200 break;
6201 default:
6202 plane_info->rotation = ROTATION_ANGLE_0;
6203 break;
6204 }
6205
6206
6207 plane_info->visible = true;
6208 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE;
6209
6210 plane_info->layer_index = plane_state->normalized_zpos;
6211
6212 ret = fill_plane_color_attributes(plane_state, plane_info->format,
6213 &plane_info->color_space);
6214 if (ret)
6215 return ret;
6216
6217 ret = amdgpu_dm_plane_fill_plane_buffer_attributes(adev, afb, plane_info->format,
6218 plane_info->rotation, tiling_flags,
6219 &plane_info->tiling_info,
6220 &plane_info->plane_size,
6221 &plane_info->dcc, address,
6222 tmz_surface);
6223 if (ret)
6224 return ret;
6225
6226 amdgpu_dm_plane_fill_blending_from_plane_state(
6227 plane_state, &plane_info->per_pixel_alpha, &plane_info->pre_multiplied_alpha,
6228 &plane_info->global_alpha, &plane_info->global_alpha_value);
6229
6230 return 0;
6231 }
6232
fill_dc_plane_attributes(struct amdgpu_device * adev,struct dc_plane_state * dc_plane_state,struct drm_plane_state * plane_state,struct drm_crtc_state * crtc_state)6233 static int fill_dc_plane_attributes(struct amdgpu_device *adev,
6234 struct dc_plane_state *dc_plane_state,
6235 struct drm_plane_state *plane_state,
6236 struct drm_crtc_state *crtc_state)
6237 {
6238 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
6239 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)plane_state->fb;
6240 struct dc_scaling_info scaling_info;
6241 struct dc_plane_info plane_info;
6242 int ret;
6243
6244 ret = amdgpu_dm_plane_fill_dc_scaling_info(adev, plane_state, &scaling_info);
6245 if (ret)
6246 return ret;
6247
6248 dc_plane_state->src_rect = scaling_info.src_rect;
6249 dc_plane_state->dst_rect = scaling_info.dst_rect;
6250 dc_plane_state->clip_rect = scaling_info.clip_rect;
6251 dc_plane_state->scaling_quality = scaling_info.scaling_quality;
6252
6253 ret = fill_dc_plane_info_and_addr(adev, plane_state,
6254 afb->tiling_flags,
6255 &plane_info,
6256 &dc_plane_state->address,
6257 afb->tmz_surface);
6258 if (ret)
6259 return ret;
6260
6261 dc_plane_state->format = plane_info.format;
6262 dc_plane_state->color_space = plane_info.color_space;
6263 dc_plane_state->format = plane_info.format;
6264 dc_plane_state->plane_size = plane_info.plane_size;
6265 dc_plane_state->rotation = plane_info.rotation;
6266 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror;
6267 dc_plane_state->stereo_format = plane_info.stereo_format;
6268 dc_plane_state->tiling_info = plane_info.tiling_info;
6269 dc_plane_state->visible = plane_info.visible;
6270 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha;
6271 dc_plane_state->pre_multiplied_alpha = plane_info.pre_multiplied_alpha;
6272 dc_plane_state->global_alpha = plane_info.global_alpha;
6273 dc_plane_state->global_alpha_value = plane_info.global_alpha_value;
6274 dc_plane_state->dcc = plane_info.dcc;
6275 dc_plane_state->layer_index = plane_info.layer_index;
6276 dc_plane_state->flip_int_enabled = true;
6277
6278 /*
6279 * Always set input transfer function, since plane state is refreshed
6280 * every time.
6281 */
6282 ret = amdgpu_dm_update_plane_color_mgmt(dm_crtc_state,
6283 plane_state,
6284 dc_plane_state);
6285 if (ret)
6286 return ret;
6287
6288 return 0;
6289 }
6290
fill_dc_dirty_rect(struct drm_plane * plane,struct rect * dirty_rect,int32_t x,s32 y,s32 width,s32 height,int * i,bool ffu)6291 static inline void fill_dc_dirty_rect(struct drm_plane *plane,
6292 struct rect *dirty_rect, int32_t x,
6293 s32 y, s32 width, s32 height,
6294 int *i, bool ffu)
6295 {
6296 WARN_ON(*i >= DC_MAX_DIRTY_RECTS);
6297
6298 dirty_rect->x = x;
6299 dirty_rect->y = y;
6300 dirty_rect->width = width;
6301 dirty_rect->height = height;
6302
6303 if (ffu)
6304 drm_dbg(plane->dev,
6305 "[PLANE:%d] PSR FFU dirty rect size (%d, %d)\n",
6306 plane->base.id, width, height);
6307 else
6308 drm_dbg(plane->dev,
6309 "[PLANE:%d] PSR SU dirty rect at (%d, %d) size (%d, %d)",
6310 plane->base.id, x, y, width, height);
6311
6312 (*i)++;
6313 }
6314
6315 /**
6316 * fill_dc_dirty_rects() - Fill DC dirty regions for PSR selective updates
6317 *
6318 * @plane: DRM plane containing dirty regions that need to be flushed to the eDP
6319 * remote fb
6320 * @old_plane_state: Old state of @plane
6321 * @new_plane_state: New state of @plane
6322 * @crtc_state: New state of CRTC connected to the @plane
6323 * @flip_addrs: DC flip tracking struct, which also tracts dirty rects
6324 * @is_psr_su: Flag indicating whether Panel Self Refresh Selective Update (PSR SU) is enabled.
6325 * If PSR SU is enabled and damage clips are available, only the regions of the screen
6326 * that have changed will be updated. If PSR SU is not enabled,
6327 * or if damage clips are not available, the entire screen will be updated.
6328 * @dirty_regions_changed: dirty regions changed
6329 *
6330 * For PSR SU, DC informs the DMUB uController of dirty rectangle regions
6331 * (referred to as "damage clips" in DRM nomenclature) that require updating on
6332 * the eDP remote buffer. The responsibility of specifying the dirty regions is
6333 * amdgpu_dm's.
6334 *
6335 * A damage-aware DRM client should fill the FB_DAMAGE_CLIPS property on the
6336 * plane with regions that require flushing to the eDP remote buffer. In
6337 * addition, certain use cases - such as cursor and multi-plane overlay (MPO) -
6338 * implicitly provide damage clips without any client support via the plane
6339 * bounds.
6340 */
fill_dc_dirty_rects(struct drm_plane * plane,struct drm_plane_state * old_plane_state,struct drm_plane_state * new_plane_state,struct drm_crtc_state * crtc_state,struct dc_flip_addrs * flip_addrs,bool is_psr_su,bool * dirty_regions_changed)6341 static void fill_dc_dirty_rects(struct drm_plane *plane,
6342 struct drm_plane_state *old_plane_state,
6343 struct drm_plane_state *new_plane_state,
6344 struct drm_crtc_state *crtc_state,
6345 struct dc_flip_addrs *flip_addrs,
6346 bool is_psr_su,
6347 bool *dirty_regions_changed)
6348 {
6349 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
6350 struct rect *dirty_rects = flip_addrs->dirty_rects;
6351 u32 num_clips;
6352 struct drm_mode_rect *clips;
6353 bool bb_changed;
6354 bool fb_changed;
6355 u32 i = 0;
6356 *dirty_regions_changed = false;
6357
6358 /*
6359 * Cursor plane has it's own dirty rect update interface. See
6360 * dcn10_dmub_update_cursor_data and dmub_cmd_update_cursor_info_data
6361 */
6362 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6363 return;
6364
6365 if (new_plane_state->rotation != DRM_MODE_ROTATE_0)
6366 goto ffu;
6367
6368 num_clips = drm_plane_get_damage_clips_count(new_plane_state);
6369 clips = drm_plane_get_damage_clips(new_plane_state);
6370
6371 if (num_clips && (!amdgpu_damage_clips || (amdgpu_damage_clips < 0 &&
6372 is_psr_su)))
6373 goto ffu;
6374
6375 if (!dm_crtc_state->mpo_requested) {
6376 if (!num_clips || num_clips > DC_MAX_DIRTY_RECTS)
6377 goto ffu;
6378
6379 for (; flip_addrs->dirty_rect_count < num_clips; clips++)
6380 fill_dc_dirty_rect(new_plane_state->plane,
6381 &dirty_rects[flip_addrs->dirty_rect_count],
6382 clips->x1, clips->y1,
6383 clips->x2 - clips->x1, clips->y2 - clips->y1,
6384 &flip_addrs->dirty_rect_count,
6385 false);
6386 return;
6387 }
6388
6389 /*
6390 * MPO is requested. Add entire plane bounding box to dirty rects if
6391 * flipped to or damaged.
6392 *
6393 * If plane is moved or resized, also add old bounding box to dirty
6394 * rects.
6395 */
6396 fb_changed = old_plane_state->fb->base.id !=
6397 new_plane_state->fb->base.id;
6398 bb_changed = (old_plane_state->crtc_x != new_plane_state->crtc_x ||
6399 old_plane_state->crtc_y != new_plane_state->crtc_y ||
6400 old_plane_state->crtc_w != new_plane_state->crtc_w ||
6401 old_plane_state->crtc_h != new_plane_state->crtc_h);
6402
6403 drm_dbg(plane->dev,
6404 "[PLANE:%d] PSR bb_changed:%d fb_changed:%d num_clips:%d\n",
6405 new_plane_state->plane->base.id,
6406 bb_changed, fb_changed, num_clips);
6407
6408 *dirty_regions_changed = bb_changed;
6409
6410 if ((num_clips + (bb_changed ? 2 : 0)) > DC_MAX_DIRTY_RECTS)
6411 goto ffu;
6412
6413 if (bb_changed) {
6414 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
6415 new_plane_state->crtc_x,
6416 new_plane_state->crtc_y,
6417 new_plane_state->crtc_w,
6418 new_plane_state->crtc_h, &i, false);
6419
6420 /* Add old plane bounding-box if plane is moved or resized */
6421 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
6422 old_plane_state->crtc_x,
6423 old_plane_state->crtc_y,
6424 old_plane_state->crtc_w,
6425 old_plane_state->crtc_h, &i, false);
6426 }
6427
6428 if (num_clips) {
6429 for (; i < num_clips; clips++)
6430 fill_dc_dirty_rect(new_plane_state->plane,
6431 &dirty_rects[i], clips->x1,
6432 clips->y1, clips->x2 - clips->x1,
6433 clips->y2 - clips->y1, &i, false);
6434 } else if (fb_changed && !bb_changed) {
6435 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
6436 new_plane_state->crtc_x,
6437 new_plane_state->crtc_y,
6438 new_plane_state->crtc_w,
6439 new_plane_state->crtc_h, &i, false);
6440 }
6441
6442 flip_addrs->dirty_rect_count = i;
6443 return;
6444
6445 ffu:
6446 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[0], 0, 0,
6447 dm_crtc_state->base.mode.crtc_hdisplay,
6448 dm_crtc_state->base.mode.crtc_vdisplay,
6449 &flip_addrs->dirty_rect_count, true);
6450 }
6451
update_stream_scaling_settings(struct drm_device * dev,const struct drm_display_mode * mode,const struct dm_connector_state * dm_state,struct dc_stream_state * stream)6452 static void update_stream_scaling_settings(struct drm_device *dev,
6453 const struct drm_display_mode *mode,
6454 const struct dm_connector_state *dm_state,
6455 struct dc_stream_state *stream)
6456 {
6457 enum amdgpu_rmx_type rmx_type;
6458
6459 struct rect src = { 0 }; /* viewport in composition space*/
6460 struct rect dst = { 0 }; /* stream addressable area */
6461
6462 /* no mode. nothing to be done */
6463 if (!mode)
6464 return;
6465
6466 /* Full screen scaling by default */
6467 src.width = mode->hdisplay;
6468 src.height = mode->vdisplay;
6469 dst.width = stream->timing.h_addressable;
6470 dst.height = stream->timing.v_addressable;
6471
6472 if (dm_state) {
6473 rmx_type = dm_state->scaling;
6474 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
6475 if (src.width * dst.height <
6476 src.height * dst.width) {
6477 /* height needs less upscaling/more downscaling */
6478 dst.width = src.width *
6479 dst.height / src.height;
6480 } else {
6481 /* width needs less upscaling/more downscaling */
6482 dst.height = src.height *
6483 dst.width / src.width;
6484 }
6485 } else if (rmx_type == RMX_CENTER) {
6486 dst = src;
6487 }
6488
6489 dst.x = (stream->timing.h_addressable - dst.width) / 2;
6490 dst.y = (stream->timing.v_addressable - dst.height) / 2;
6491
6492 if (dm_state->underscan_enable) {
6493 dst.x += dm_state->underscan_hborder / 2;
6494 dst.y += dm_state->underscan_vborder / 2;
6495 dst.width -= dm_state->underscan_hborder;
6496 dst.height -= dm_state->underscan_vborder;
6497 }
6498 }
6499
6500 stream->src = src;
6501 stream->dst = dst;
6502
6503 drm_dbg_kms(dev, "Destination Rectangle x:%d y:%d width:%d height:%d\n",
6504 dst.x, dst.y, dst.width, dst.height);
6505
6506 }
6507
6508 static enum dc_color_depth
convert_color_depth_from_display_info(const struct drm_connector * connector,bool is_y420,int requested_bpc)6509 convert_color_depth_from_display_info(const struct drm_connector *connector,
6510 bool is_y420, int requested_bpc)
6511 {
6512 u8 bpc;
6513
6514 if (is_y420) {
6515 bpc = 8;
6516
6517 /* Cap display bpc based on HDMI 2.0 HF-VSDB */
6518 if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48)
6519 bpc = 16;
6520 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36)
6521 bpc = 12;
6522 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30)
6523 bpc = 10;
6524 } else {
6525 bpc = (uint8_t)connector->display_info.bpc;
6526 /* Assume 8 bpc by default if no bpc is specified. */
6527 bpc = bpc ? bpc : 8;
6528 }
6529
6530 if (requested_bpc > 0) {
6531 /*
6532 * Cap display bpc based on the user requested value.
6533 *
6534 * The value for state->max_bpc may not correctly updated
6535 * depending on when the connector gets added to the state
6536 * or if this was called outside of atomic check, so it
6537 * can't be used directly.
6538 */
6539 bpc = min_t(u8, bpc, requested_bpc);
6540
6541 /* Round down to the nearest even number. */
6542 bpc = bpc - (bpc & 1);
6543 }
6544
6545 switch (bpc) {
6546 case 0:
6547 /*
6548 * Temporary Work around, DRM doesn't parse color depth for
6549 * EDID revision before 1.4
6550 * TODO: Fix edid parsing
6551 */
6552 return COLOR_DEPTH_888;
6553 case 6:
6554 return COLOR_DEPTH_666;
6555 case 8:
6556 return COLOR_DEPTH_888;
6557 case 10:
6558 return COLOR_DEPTH_101010;
6559 case 12:
6560 return COLOR_DEPTH_121212;
6561 case 14:
6562 return COLOR_DEPTH_141414;
6563 case 16:
6564 return COLOR_DEPTH_161616;
6565 default:
6566 return COLOR_DEPTH_UNDEFINED;
6567 }
6568 }
6569
6570 static enum dc_aspect_ratio
get_aspect_ratio(const struct drm_display_mode * mode_in)6571 get_aspect_ratio(const struct drm_display_mode *mode_in)
6572 {
6573 /* 1-1 mapping, since both enums follow the HDMI spec. */
6574 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
6575 }
6576
6577 static enum dc_color_space
get_output_color_space(const struct dc_crtc_timing * dc_crtc_timing,const struct drm_connector_state * connector_state)6578 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing,
6579 const struct drm_connector_state *connector_state)
6580 {
6581 enum dc_color_space color_space = COLOR_SPACE_SRGB;
6582
6583 switch (connector_state->colorspace) {
6584 case DRM_MODE_COLORIMETRY_BT601_YCC:
6585 if (dc_crtc_timing->flags.Y_ONLY)
6586 color_space = COLOR_SPACE_YCBCR601_LIMITED;
6587 else
6588 color_space = COLOR_SPACE_YCBCR601;
6589 break;
6590 case DRM_MODE_COLORIMETRY_BT709_YCC:
6591 if (dc_crtc_timing->flags.Y_ONLY)
6592 color_space = COLOR_SPACE_YCBCR709_LIMITED;
6593 else
6594 color_space = COLOR_SPACE_YCBCR709;
6595 break;
6596 case DRM_MODE_COLORIMETRY_OPRGB:
6597 color_space = COLOR_SPACE_ADOBERGB;
6598 break;
6599 case DRM_MODE_COLORIMETRY_BT2020_RGB:
6600 case DRM_MODE_COLORIMETRY_BT2020_YCC:
6601 if (dc_crtc_timing->pixel_encoding == PIXEL_ENCODING_RGB)
6602 color_space = COLOR_SPACE_2020_RGB_FULLRANGE;
6603 else
6604 color_space = COLOR_SPACE_2020_YCBCR_LIMITED;
6605 break;
6606 case DRM_MODE_COLORIMETRY_DEFAULT: // ITU601
6607 default:
6608 if (dc_crtc_timing->pixel_encoding == PIXEL_ENCODING_RGB) {
6609 color_space = COLOR_SPACE_SRGB;
6610 if (connector_state->hdmi.broadcast_rgb == DRM_HDMI_BROADCAST_RGB_LIMITED)
6611 color_space = COLOR_SPACE_SRGB_LIMITED;
6612 /*
6613 * 27030khz is the separation point between HDTV and SDTV
6614 * according to HDMI spec, we use YCbCr709 and YCbCr601
6615 * respectively
6616 */
6617 } else if (dc_crtc_timing->pix_clk_100hz > 270300) {
6618 if (dc_crtc_timing->flags.Y_ONLY)
6619 color_space =
6620 COLOR_SPACE_YCBCR709_LIMITED;
6621 else
6622 color_space = COLOR_SPACE_YCBCR709;
6623 } else {
6624 if (dc_crtc_timing->flags.Y_ONLY)
6625 color_space =
6626 COLOR_SPACE_YCBCR601_LIMITED;
6627 else
6628 color_space = COLOR_SPACE_YCBCR601;
6629 }
6630 break;
6631 }
6632
6633 return color_space;
6634 }
6635
6636 static enum display_content_type
get_output_content_type(const struct drm_connector_state * connector_state)6637 get_output_content_type(const struct drm_connector_state *connector_state)
6638 {
6639 switch (connector_state->content_type) {
6640 default:
6641 case DRM_MODE_CONTENT_TYPE_NO_DATA:
6642 return DISPLAY_CONTENT_TYPE_NO_DATA;
6643 case DRM_MODE_CONTENT_TYPE_GRAPHICS:
6644 return DISPLAY_CONTENT_TYPE_GRAPHICS;
6645 case DRM_MODE_CONTENT_TYPE_PHOTO:
6646 return DISPLAY_CONTENT_TYPE_PHOTO;
6647 case DRM_MODE_CONTENT_TYPE_CINEMA:
6648 return DISPLAY_CONTENT_TYPE_CINEMA;
6649 case DRM_MODE_CONTENT_TYPE_GAME:
6650 return DISPLAY_CONTENT_TYPE_GAME;
6651 }
6652 }
6653
adjust_colour_depth_from_display_info(struct dc_crtc_timing * timing_out,const struct drm_display_info * info)6654 static bool adjust_colour_depth_from_display_info(
6655 struct dc_crtc_timing *timing_out,
6656 const struct drm_display_info *info)
6657 {
6658 enum dc_color_depth depth = timing_out->display_color_depth;
6659 int normalized_clk;
6660
6661 do {
6662 normalized_clk = timing_out->pix_clk_100hz / 10;
6663 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
6664 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
6665 normalized_clk /= 2;
6666 /* Adjusting pix clock following on HDMI spec based on colour depth */
6667 switch (depth) {
6668 case COLOR_DEPTH_888:
6669 break;
6670 case COLOR_DEPTH_101010:
6671 normalized_clk = (normalized_clk * 30) / 24;
6672 break;
6673 case COLOR_DEPTH_121212:
6674 normalized_clk = (normalized_clk * 36) / 24;
6675 break;
6676 case COLOR_DEPTH_161616:
6677 normalized_clk = (normalized_clk * 48) / 24;
6678 break;
6679 default:
6680 /* The above depths are the only ones valid for HDMI. */
6681 return false;
6682 }
6683 if (normalized_clk <= info->max_tmds_clock) {
6684 timing_out->display_color_depth = depth;
6685 return true;
6686 }
6687 } while (--depth > COLOR_DEPTH_666);
6688 return false;
6689 }
6690
fill_stream_properties_from_drm_display_mode(struct dc_stream_state * stream,const struct drm_display_mode * mode_in,const struct drm_connector * connector,const struct drm_connector_state * connector_state,const struct dc_stream_state * old_stream,int requested_bpc)6691 static void fill_stream_properties_from_drm_display_mode(
6692 struct dc_stream_state *stream,
6693 const struct drm_display_mode *mode_in,
6694 const struct drm_connector *connector,
6695 const struct drm_connector_state *connector_state,
6696 const struct dc_stream_state *old_stream,
6697 int requested_bpc)
6698 {
6699 struct dc_crtc_timing *timing_out = &stream->timing;
6700 const struct drm_display_info *info = &connector->display_info;
6701 struct amdgpu_dm_connector *aconnector = NULL;
6702 struct hdmi_vendor_infoframe hv_frame;
6703 struct hdmi_avi_infoframe avi_frame;
6704 ssize_t err;
6705
6706 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
6707 aconnector = to_amdgpu_dm_connector(connector);
6708
6709 memset(&hv_frame, 0, sizeof(hv_frame));
6710 memset(&avi_frame, 0, sizeof(avi_frame));
6711
6712 timing_out->h_border_left = 0;
6713 timing_out->h_border_right = 0;
6714 timing_out->v_border_top = 0;
6715 timing_out->v_border_bottom = 0;
6716 /* TODO: un-hardcode */
6717 if (drm_mode_is_420_only(info, mode_in)
6718 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
6719 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
6720 else if (drm_mode_is_420_also(info, mode_in)
6721 && aconnector
6722 && aconnector->force_yuv420_output)
6723 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
6724 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCBCR422)
6725 && aconnector
6726 && aconnector->force_yuv422_output)
6727 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR422;
6728 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCBCR444)
6729 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
6730 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
6731 else
6732 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
6733
6734 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
6735 timing_out->display_color_depth = convert_color_depth_from_display_info(
6736 connector,
6737 (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420),
6738 requested_bpc);
6739 timing_out->scan_type = SCANNING_TYPE_NODATA;
6740 timing_out->hdmi_vic = 0;
6741
6742 if (old_stream) {
6743 timing_out->vic = old_stream->timing.vic;
6744 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY;
6745 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY;
6746 } else {
6747 timing_out->vic = drm_match_cea_mode(mode_in);
6748 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
6749 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
6750 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
6751 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
6752 }
6753
6754 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
6755 err = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame,
6756 (struct drm_connector *)connector,
6757 mode_in);
6758 if (err < 0)
6759 drm_warn_once(connector->dev, "Failed to setup avi infoframe on connector %s: %zd\n",
6760 connector->name, err);
6761 timing_out->vic = avi_frame.video_code;
6762 err = drm_hdmi_vendor_infoframe_from_display_mode(&hv_frame,
6763 (struct drm_connector *)connector,
6764 mode_in);
6765 if (err < 0)
6766 drm_warn_once(connector->dev, "Failed to setup vendor infoframe on connector %s: %zd\n",
6767 connector->name, err);
6768 timing_out->hdmi_vic = hv_frame.vic;
6769 }
6770
6771 if (aconnector && is_freesync_video_mode(mode_in, aconnector)) {
6772 timing_out->h_addressable = mode_in->hdisplay;
6773 timing_out->h_total = mode_in->htotal;
6774 timing_out->h_sync_width = mode_in->hsync_end - mode_in->hsync_start;
6775 timing_out->h_front_porch = mode_in->hsync_start - mode_in->hdisplay;
6776 timing_out->v_total = mode_in->vtotal;
6777 timing_out->v_addressable = mode_in->vdisplay;
6778 timing_out->v_front_porch = mode_in->vsync_start - mode_in->vdisplay;
6779 timing_out->v_sync_width = mode_in->vsync_end - mode_in->vsync_start;
6780 timing_out->pix_clk_100hz = mode_in->clock * 10;
6781 } else {
6782 timing_out->h_addressable = mode_in->crtc_hdisplay;
6783 timing_out->h_total = mode_in->crtc_htotal;
6784 timing_out->h_sync_width = mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
6785 timing_out->h_front_porch = mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
6786 timing_out->v_total = mode_in->crtc_vtotal;
6787 timing_out->v_addressable = mode_in->crtc_vdisplay;
6788 timing_out->v_front_porch = mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
6789 timing_out->v_sync_width = mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
6790 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10;
6791 }
6792
6793 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
6794
6795 stream->out_transfer_func.type = TF_TYPE_PREDEFINED;
6796 stream->out_transfer_func.tf = TRANSFER_FUNCTION_SRGB;
6797 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
6798 if (!adjust_colour_depth_from_display_info(timing_out, info) &&
6799 drm_mode_is_420_also(info, mode_in) &&
6800 timing_out->pixel_encoding != PIXEL_ENCODING_YCBCR420) {
6801 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
6802 adjust_colour_depth_from_display_info(timing_out, info);
6803 }
6804 }
6805
6806 stream->output_color_space = get_output_color_space(timing_out, connector_state);
6807 stream->content_type = get_output_content_type(connector_state);
6808 }
6809
fill_audio_info(struct audio_info * audio_info,const struct drm_connector * drm_connector,const struct dc_sink * dc_sink)6810 static void fill_audio_info(struct audio_info *audio_info,
6811 const struct drm_connector *drm_connector,
6812 const struct dc_sink *dc_sink)
6813 {
6814 int i = 0;
6815 int cea_revision = 0;
6816 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
6817
6818 audio_info->manufacture_id = edid_caps->manufacturer_id;
6819 audio_info->product_id = edid_caps->product_id;
6820
6821 cea_revision = drm_connector->display_info.cea_rev;
6822
6823 strscpy(audio_info->display_name,
6824 edid_caps->display_name,
6825 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
6826
6827 if (cea_revision >= 3) {
6828 audio_info->mode_count = edid_caps->audio_mode_count;
6829
6830 for (i = 0; i < audio_info->mode_count; ++i) {
6831 audio_info->modes[i].format_code =
6832 (enum audio_format_code)
6833 (edid_caps->audio_modes[i].format_code);
6834 audio_info->modes[i].channel_count =
6835 edid_caps->audio_modes[i].channel_count;
6836 audio_info->modes[i].sample_rates.all =
6837 edid_caps->audio_modes[i].sample_rate;
6838 audio_info->modes[i].sample_size =
6839 edid_caps->audio_modes[i].sample_size;
6840 }
6841 }
6842
6843 audio_info->flags.all = edid_caps->speaker_flags;
6844
6845 /* TODO: We only check for the progressive mode, check for interlace mode too */
6846 if (drm_connector->latency_present[0]) {
6847 audio_info->video_latency = drm_connector->video_latency[0];
6848 audio_info->audio_latency = drm_connector->audio_latency[0];
6849 }
6850
6851 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
6852
6853 }
6854
6855 static void
copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode * src_mode,struct drm_display_mode * dst_mode)6856 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
6857 struct drm_display_mode *dst_mode)
6858 {
6859 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
6860 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
6861 dst_mode->crtc_clock = src_mode->crtc_clock;
6862 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
6863 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
6864 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
6865 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
6866 dst_mode->crtc_htotal = src_mode->crtc_htotal;
6867 dst_mode->crtc_hskew = src_mode->crtc_hskew;
6868 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
6869 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
6870 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
6871 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
6872 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
6873 }
6874
6875 static void
decide_crtc_timing_for_drm_display_mode(struct drm_display_mode * drm_mode,const struct drm_display_mode * native_mode,bool scale_enabled)6876 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
6877 const struct drm_display_mode *native_mode,
6878 bool scale_enabled)
6879 {
6880 if (scale_enabled || (
6881 native_mode->clock == drm_mode->clock &&
6882 native_mode->htotal == drm_mode->htotal &&
6883 native_mode->vtotal == drm_mode->vtotal)) {
6884 if (native_mode->crtc_clock)
6885 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
6886 } else {
6887 /* no scaling nor amdgpu inserted, no need to patch */
6888 }
6889 }
6890
6891 static struct dc_sink *
create_fake_sink(struct drm_device * dev,struct dc_link * link)6892 create_fake_sink(struct drm_device *dev, struct dc_link *link)
6893 {
6894 struct dc_sink_init_data sink_init_data = { 0 };
6895 struct dc_sink *sink = NULL;
6896
6897 sink_init_data.link = link;
6898 sink_init_data.sink_signal = link->connector_signal;
6899
6900 sink = dc_sink_create(&sink_init_data);
6901 if (!sink) {
6902 drm_err(dev, "Failed to create sink!\n");
6903 return NULL;
6904 }
6905 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
6906
6907 return sink;
6908 }
6909
set_multisync_trigger_params(struct dc_stream_state * stream)6910 static void set_multisync_trigger_params(
6911 struct dc_stream_state *stream)
6912 {
6913 struct dc_stream_state *master = NULL;
6914
6915 if (stream->triggered_crtc_reset.enabled) {
6916 master = stream->triggered_crtc_reset.event_source;
6917 stream->triggered_crtc_reset.event =
6918 master->timing.flags.VSYNC_POSITIVE_POLARITY ?
6919 CRTC_EVENT_VSYNC_RISING : CRTC_EVENT_VSYNC_FALLING;
6920 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_PIXEL;
6921 }
6922 }
6923
set_master_stream(struct dc_stream_state * stream_set[],int stream_count)6924 static void set_master_stream(struct dc_stream_state *stream_set[],
6925 int stream_count)
6926 {
6927 int j, highest_rfr = 0, master_stream = 0;
6928
6929 for (j = 0; j < stream_count; j++) {
6930 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
6931 int refresh_rate = 0;
6932
6933 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/
6934 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
6935 if (refresh_rate > highest_rfr) {
6936 highest_rfr = refresh_rate;
6937 master_stream = j;
6938 }
6939 }
6940 }
6941 for (j = 0; j < stream_count; j++) {
6942 if (stream_set[j])
6943 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
6944 }
6945 }
6946
dm_enable_per_frame_crtc_master_sync(struct dc_state * context)6947 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
6948 {
6949 int i = 0;
6950 struct dc_stream_state *stream;
6951
6952 if (context->stream_count < 2)
6953 return;
6954 for (i = 0; i < context->stream_count ; i++) {
6955 if (!context->streams[i])
6956 continue;
6957 /*
6958 * TODO: add a function to read AMD VSDB bits and set
6959 * crtc_sync_master.multi_sync_enabled flag
6960 * For now it's set to false
6961 */
6962 }
6963
6964 set_master_stream(context->streams, context->stream_count);
6965
6966 for (i = 0; i < context->stream_count ; i++) {
6967 stream = context->streams[i];
6968
6969 if (!stream)
6970 continue;
6971
6972 set_multisync_trigger_params(stream);
6973 }
6974 }
6975
6976 /**
6977 * DOC: FreeSync Video
6978 *
6979 * When a userspace application wants to play a video, the content follows a
6980 * standard format definition that usually specifies the FPS for that format.
6981 * The below list illustrates some video format and the expected FPS,
6982 * respectively:
6983 *
6984 * - TV/NTSC (23.976 FPS)
6985 * - Cinema (24 FPS)
6986 * - TV/PAL (25 FPS)
6987 * - TV/NTSC (29.97 FPS)
6988 * - TV/NTSC (30 FPS)
6989 * - Cinema HFR (48 FPS)
6990 * - TV/PAL (50 FPS)
6991 * - Commonly used (60 FPS)
6992 * - Multiples of 24 (48,72,96 FPS)
6993 *
6994 * The list of standards video format is not huge and can be added to the
6995 * connector modeset list beforehand. With that, userspace can leverage
6996 * FreeSync to extends the front porch in order to attain the target refresh
6997 * rate. Such a switch will happen seamlessly, without screen blanking or
6998 * reprogramming of the output in any other way. If the userspace requests a
6999 * modesetting change compatible with FreeSync modes that only differ in the
7000 * refresh rate, DC will skip the full update and avoid blink during the
7001 * transition. For example, the video player can change the modesetting from
7002 * 60Hz to 30Hz for playing TV/NTSC content when it goes full screen without
7003 * causing any display blink. This same concept can be applied to a mode
7004 * setting change.
7005 */
7006 static struct drm_display_mode *
get_highest_refresh_rate_mode(struct amdgpu_dm_connector * aconnector,bool use_probed_modes)7007 get_highest_refresh_rate_mode(struct amdgpu_dm_connector *aconnector,
7008 bool use_probed_modes)
7009 {
7010 struct drm_display_mode *m, *m_pref = NULL;
7011 u16 current_refresh, highest_refresh;
7012 struct list_head *list_head = use_probed_modes ?
7013 &aconnector->base.probed_modes :
7014 &aconnector->base.modes;
7015
7016 if (aconnector->base.connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
7017 return NULL;
7018
7019 if (aconnector->freesync_vid_base.clock != 0)
7020 return &aconnector->freesync_vid_base;
7021
7022 /* Find the preferred mode */
7023 list_for_each_entry(m, list_head, head) {
7024 if (m->type & DRM_MODE_TYPE_PREFERRED) {
7025 m_pref = m;
7026 break;
7027 }
7028 }
7029
7030 if (!m_pref) {
7031 /* Probably an EDID with no preferred mode. Fallback to first entry */
7032 m_pref = list_first_entry_or_null(
7033 &aconnector->base.modes, struct drm_display_mode, head);
7034 if (!m_pref) {
7035 drm_dbg_driver(aconnector->base.dev, "No preferred mode found in EDID\n");
7036 return NULL;
7037 }
7038 }
7039
7040 highest_refresh = drm_mode_vrefresh(m_pref);
7041
7042 /*
7043 * Find the mode with highest refresh rate with same resolution.
7044 * For some monitors, preferred mode is not the mode with highest
7045 * supported refresh rate.
7046 */
7047 list_for_each_entry(m, list_head, head) {
7048 current_refresh = drm_mode_vrefresh(m);
7049
7050 if (m->hdisplay == m_pref->hdisplay &&
7051 m->vdisplay == m_pref->vdisplay &&
7052 highest_refresh < current_refresh) {
7053 highest_refresh = current_refresh;
7054 m_pref = m;
7055 }
7056 }
7057
7058 drm_mode_copy(&aconnector->freesync_vid_base, m_pref);
7059 return m_pref;
7060 }
7061
is_freesync_video_mode(const struct drm_display_mode * mode,struct amdgpu_dm_connector * aconnector)7062 static bool is_freesync_video_mode(const struct drm_display_mode *mode,
7063 struct amdgpu_dm_connector *aconnector)
7064 {
7065 struct drm_display_mode *high_mode;
7066 int timing_diff;
7067
7068 high_mode = get_highest_refresh_rate_mode(aconnector, false);
7069 if (!high_mode || !mode)
7070 return false;
7071
7072 timing_diff = high_mode->vtotal - mode->vtotal;
7073
7074 if (high_mode->clock == 0 || high_mode->clock != mode->clock ||
7075 high_mode->hdisplay != mode->hdisplay ||
7076 high_mode->vdisplay != mode->vdisplay ||
7077 high_mode->hsync_start != mode->hsync_start ||
7078 high_mode->hsync_end != mode->hsync_end ||
7079 high_mode->htotal != mode->htotal ||
7080 high_mode->hskew != mode->hskew ||
7081 high_mode->vscan != mode->vscan ||
7082 high_mode->vsync_start - mode->vsync_start != timing_diff ||
7083 high_mode->vsync_end - mode->vsync_end != timing_diff)
7084 return false;
7085 else
7086 return true;
7087 }
7088
7089 #if defined(CONFIG_DRM_AMD_DC_FP)
update_dsc_caps(struct amdgpu_dm_connector * aconnector,struct dc_sink * sink,struct dc_stream_state * stream,struct dsc_dec_dpcd_caps * dsc_caps)7090 static void update_dsc_caps(struct amdgpu_dm_connector *aconnector,
7091 struct dc_sink *sink, struct dc_stream_state *stream,
7092 struct dsc_dec_dpcd_caps *dsc_caps)
7093 {
7094 stream->timing.flags.DSC = 0;
7095 dsc_caps->is_dsc_supported = false;
7096
7097 if (aconnector->dc_link && (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT ||
7098 sink->sink_signal == SIGNAL_TYPE_EDP)) {
7099 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE ||
7100 sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER)
7101 dc_dsc_parse_dsc_dpcd(aconnector->dc_link->ctx->dc,
7102 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.raw,
7103 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.raw,
7104 dsc_caps);
7105 }
7106 }
7107
apply_dsc_policy_for_edp(struct amdgpu_dm_connector * aconnector,struct dc_sink * sink,struct dc_stream_state * stream,struct dsc_dec_dpcd_caps * dsc_caps,uint32_t max_dsc_target_bpp_limit_override)7108 static void apply_dsc_policy_for_edp(struct amdgpu_dm_connector *aconnector,
7109 struct dc_sink *sink, struct dc_stream_state *stream,
7110 struct dsc_dec_dpcd_caps *dsc_caps,
7111 uint32_t max_dsc_target_bpp_limit_override)
7112 {
7113 const struct dc_link_settings *verified_link_cap = NULL;
7114 u32 link_bw_in_kbps;
7115 u32 edp_min_bpp_x16, edp_max_bpp_x16;
7116 struct dc *dc = sink->ctx->dc;
7117 struct dc_dsc_bw_range bw_range = {0};
7118 struct dc_dsc_config dsc_cfg = {0};
7119 struct dc_dsc_config_options dsc_options = {0};
7120
7121 dc_dsc_get_default_config_option(dc, &dsc_options);
7122 dsc_options.max_target_bpp_limit_override_x16 = max_dsc_target_bpp_limit_override * 16;
7123
7124 verified_link_cap = dc_link_get_link_cap(stream->link);
7125 link_bw_in_kbps = dc_link_bandwidth_kbps(stream->link, verified_link_cap);
7126 edp_min_bpp_x16 = 8 * 16;
7127 edp_max_bpp_x16 = 8 * 16;
7128
7129 if (edp_max_bpp_x16 > dsc_caps->edp_max_bits_per_pixel)
7130 edp_max_bpp_x16 = dsc_caps->edp_max_bits_per_pixel;
7131
7132 if (edp_max_bpp_x16 < edp_min_bpp_x16)
7133 edp_min_bpp_x16 = edp_max_bpp_x16;
7134
7135 if (dc_dsc_compute_bandwidth_range(dc->res_pool->dscs[0],
7136 dc->debug.dsc_min_slice_height_override,
7137 edp_min_bpp_x16, edp_max_bpp_x16,
7138 dsc_caps,
7139 &stream->timing,
7140 dc_link_get_highest_encoding_format(aconnector->dc_link),
7141 &bw_range)) {
7142
7143 if (bw_range.max_kbps < link_bw_in_kbps) {
7144 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
7145 dsc_caps,
7146 &dsc_options,
7147 0,
7148 &stream->timing,
7149 dc_link_get_highest_encoding_format(aconnector->dc_link),
7150 &dsc_cfg)) {
7151 stream->timing.dsc_cfg = dsc_cfg;
7152 stream->timing.flags.DSC = 1;
7153 stream->timing.dsc_cfg.bits_per_pixel = edp_max_bpp_x16;
7154 }
7155 return;
7156 }
7157 }
7158
7159 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
7160 dsc_caps,
7161 &dsc_options,
7162 link_bw_in_kbps,
7163 &stream->timing,
7164 dc_link_get_highest_encoding_format(aconnector->dc_link),
7165 &dsc_cfg)) {
7166 stream->timing.dsc_cfg = dsc_cfg;
7167 stream->timing.flags.DSC = 1;
7168 }
7169 }
7170
apply_dsc_policy_for_stream(struct amdgpu_dm_connector * aconnector,struct dc_sink * sink,struct dc_stream_state * stream,struct dsc_dec_dpcd_caps * dsc_caps)7171 static void apply_dsc_policy_for_stream(struct amdgpu_dm_connector *aconnector,
7172 struct dc_sink *sink, struct dc_stream_state *stream,
7173 struct dsc_dec_dpcd_caps *dsc_caps)
7174 {
7175 struct drm_connector *drm_connector = &aconnector->base;
7176 u32 link_bandwidth_kbps;
7177 struct dc *dc = sink->ctx->dc;
7178 u32 max_supported_bw_in_kbps, timing_bw_in_kbps;
7179 u32 dsc_max_supported_bw_in_kbps;
7180 u32 max_dsc_target_bpp_limit_override =
7181 drm_connector->display_info.max_dsc_bpp;
7182 struct dc_dsc_config_options dsc_options = {0};
7183
7184 dc_dsc_get_default_config_option(dc, &dsc_options);
7185 dsc_options.max_target_bpp_limit_override_x16 = max_dsc_target_bpp_limit_override * 16;
7186
7187 link_bandwidth_kbps = dc_link_bandwidth_kbps(aconnector->dc_link,
7188 dc_link_get_link_cap(aconnector->dc_link));
7189
7190 /* Set DSC policy according to dsc_clock_en */
7191 dc_dsc_policy_set_enable_dsc_when_not_needed(
7192 aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE);
7193
7194 if (sink->sink_signal == SIGNAL_TYPE_EDP &&
7195 !aconnector->dc_link->panel_config.dsc.disable_dsc_edp &&
7196 dc->caps.edp_dsc_support && aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE) {
7197
7198 apply_dsc_policy_for_edp(aconnector, sink, stream, dsc_caps, max_dsc_target_bpp_limit_override);
7199
7200 } else if (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT) {
7201 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE) {
7202 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
7203 dsc_caps,
7204 &dsc_options,
7205 link_bandwidth_kbps,
7206 &stream->timing,
7207 dc_link_get_highest_encoding_format(aconnector->dc_link),
7208 &stream->timing.dsc_cfg)) {
7209 stream->timing.flags.DSC = 1;
7210 drm_dbg_driver(drm_connector->dev, "%s: SST_DSC [%s] DSC is selected from SST RX\n",
7211 __func__, drm_connector->name);
7212 }
7213 } else if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER) {
7214 timing_bw_in_kbps = dc_bandwidth_in_kbps_from_timing(&stream->timing,
7215 dc_link_get_highest_encoding_format(aconnector->dc_link));
7216 max_supported_bw_in_kbps = link_bandwidth_kbps;
7217 dsc_max_supported_bw_in_kbps = link_bandwidth_kbps;
7218
7219 if (timing_bw_in_kbps > max_supported_bw_in_kbps &&
7220 max_supported_bw_in_kbps > 0 &&
7221 dsc_max_supported_bw_in_kbps > 0)
7222 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
7223 dsc_caps,
7224 &dsc_options,
7225 dsc_max_supported_bw_in_kbps,
7226 &stream->timing,
7227 dc_link_get_highest_encoding_format(aconnector->dc_link),
7228 &stream->timing.dsc_cfg)) {
7229 stream->timing.flags.DSC = 1;
7230 drm_dbg_driver(drm_connector->dev, "%s: SST_DSC [%s] DSC is selected from DP-HDMI PCON\n",
7231 __func__, drm_connector->name);
7232 }
7233 }
7234 }
7235
7236 /* Overwrite the stream flag if DSC is enabled through debugfs */
7237 if (aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE)
7238 stream->timing.flags.DSC = 1;
7239
7240 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_h)
7241 stream->timing.dsc_cfg.num_slices_h = aconnector->dsc_settings.dsc_num_slices_h;
7242
7243 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_v)
7244 stream->timing.dsc_cfg.num_slices_v = aconnector->dsc_settings.dsc_num_slices_v;
7245
7246 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_bits_per_pixel)
7247 stream->timing.dsc_cfg.bits_per_pixel = aconnector->dsc_settings.dsc_bits_per_pixel;
7248 }
7249 #endif
7250
7251 static struct dc_stream_state *
create_stream_for_sink(struct drm_connector * connector,const struct drm_display_mode * drm_mode,const struct dm_connector_state * dm_state,const struct dc_stream_state * old_stream,int requested_bpc)7252 create_stream_for_sink(struct drm_connector *connector,
7253 const struct drm_display_mode *drm_mode,
7254 const struct dm_connector_state *dm_state,
7255 const struct dc_stream_state *old_stream,
7256 int requested_bpc)
7257 {
7258 struct drm_device *dev = connector->dev;
7259 struct amdgpu_dm_connector *aconnector = NULL;
7260 struct drm_display_mode *preferred_mode = NULL;
7261 const struct drm_connector_state *con_state = &dm_state->base;
7262 struct dc_stream_state *stream = NULL;
7263 struct drm_display_mode mode;
7264 struct drm_display_mode saved_mode;
7265 struct drm_display_mode *freesync_mode = NULL;
7266 bool native_mode_found = false;
7267 bool recalculate_timing = false;
7268 bool scale = dm_state->scaling != RMX_OFF;
7269 int mode_refresh;
7270 int preferred_refresh = 0;
7271 enum color_transfer_func tf = TRANSFER_FUNC_UNKNOWN;
7272 #if defined(CONFIG_DRM_AMD_DC_FP)
7273 struct dsc_dec_dpcd_caps dsc_caps;
7274 #endif
7275 struct dc_link *link = NULL;
7276 struct dc_sink *sink = NULL;
7277
7278 drm_mode_init(&mode, drm_mode);
7279 memset(&saved_mode, 0, sizeof(saved_mode));
7280
7281 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) {
7282 aconnector = NULL;
7283 aconnector = to_amdgpu_dm_connector(connector);
7284 link = aconnector->dc_link;
7285 } else {
7286 struct drm_writeback_connector *wbcon = NULL;
7287 struct amdgpu_dm_wb_connector *dm_wbcon = NULL;
7288
7289 wbcon = drm_connector_to_writeback(connector);
7290 dm_wbcon = to_amdgpu_dm_wb_connector(wbcon);
7291 link = dm_wbcon->link;
7292 }
7293
7294 if (!aconnector || !aconnector->dc_sink) {
7295 sink = create_fake_sink(dev, link);
7296 if (!sink)
7297 return stream;
7298
7299 } else {
7300 sink = aconnector->dc_sink;
7301 dc_sink_retain(sink);
7302 }
7303
7304 stream = dc_create_stream_for_sink(sink);
7305
7306 if (stream == NULL) {
7307 drm_err(dev, "Failed to create stream for sink!\n");
7308 goto finish;
7309 }
7310
7311 /* We leave this NULL for writeback connectors */
7312 stream->dm_stream_context = aconnector;
7313
7314 stream->timing.flags.LTE_340MCSC_SCRAMBLE =
7315 connector->display_info.hdmi.scdc.scrambling.low_rates;
7316
7317 list_for_each_entry(preferred_mode, &connector->modes, head) {
7318 /* Search for preferred mode */
7319 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
7320 native_mode_found = true;
7321 break;
7322 }
7323 }
7324 if (!native_mode_found)
7325 preferred_mode = list_first_entry_or_null(
7326 &connector->modes,
7327 struct drm_display_mode,
7328 head);
7329
7330 mode_refresh = drm_mode_vrefresh(&mode);
7331
7332 if (preferred_mode == NULL) {
7333 /*
7334 * This may not be an error, the use case is when we have no
7335 * usermode calls to reset and set mode upon hotplug. In this
7336 * case, we call set mode ourselves to restore the previous mode
7337 * and the modelist may not be filled in time.
7338 */
7339 drm_dbg_driver(dev, "No preferred mode found\n");
7340 } else if (aconnector) {
7341 recalculate_timing = amdgpu_freesync_vid_mode &&
7342 is_freesync_video_mode(&mode, aconnector);
7343 if (recalculate_timing) {
7344 freesync_mode = get_highest_refresh_rate_mode(aconnector, false);
7345 drm_mode_copy(&saved_mode, &mode);
7346 saved_mode.picture_aspect_ratio = mode.picture_aspect_ratio;
7347 drm_mode_copy(&mode, freesync_mode);
7348 mode.picture_aspect_ratio = saved_mode.picture_aspect_ratio;
7349 } else {
7350 decide_crtc_timing_for_drm_display_mode(
7351 &mode, preferred_mode, scale);
7352
7353 preferred_refresh = drm_mode_vrefresh(preferred_mode);
7354 }
7355 }
7356
7357 if (recalculate_timing)
7358 drm_mode_set_crtcinfo(&saved_mode, 0);
7359
7360 /*
7361 * If scaling is enabled and refresh rate didn't change
7362 * we copy the vic and polarities of the old timings
7363 */
7364 if (!scale || mode_refresh != preferred_refresh)
7365 fill_stream_properties_from_drm_display_mode(
7366 stream, &mode, connector, con_state, NULL,
7367 requested_bpc);
7368 else
7369 fill_stream_properties_from_drm_display_mode(
7370 stream, &mode, connector, con_state, old_stream,
7371 requested_bpc);
7372
7373 /* The rest isn't needed for writeback connectors */
7374 if (!aconnector)
7375 goto finish;
7376
7377 if (aconnector->timing_changed) {
7378 drm_dbg(aconnector->base.dev,
7379 "overriding timing for automated test, bpc %d, changing to %d\n",
7380 stream->timing.display_color_depth,
7381 aconnector->timing_requested->display_color_depth);
7382 stream->timing = *aconnector->timing_requested;
7383 }
7384
7385 #if defined(CONFIG_DRM_AMD_DC_FP)
7386 /* SST DSC determination policy */
7387 update_dsc_caps(aconnector, sink, stream, &dsc_caps);
7388 if (aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE && dsc_caps.is_dsc_supported)
7389 apply_dsc_policy_for_stream(aconnector, sink, stream, &dsc_caps);
7390 #endif
7391
7392 update_stream_scaling_settings(dev, &mode, dm_state, stream);
7393
7394 fill_audio_info(
7395 &stream->audio_info,
7396 connector,
7397 sink);
7398
7399 update_stream_signal(stream, sink);
7400
7401 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
7402 mod_build_hf_vsif_infopacket(stream, &stream->vsp_infopacket);
7403
7404 if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT ||
7405 stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST ||
7406 stream->signal == SIGNAL_TYPE_EDP) {
7407 const struct dc_edid_caps *edid_caps;
7408 unsigned int disable_colorimetry = 0;
7409
7410 if (aconnector->dc_sink) {
7411 edid_caps = &aconnector->dc_sink->edid_caps;
7412 disable_colorimetry = edid_caps->panel_patch.disable_colorimetry;
7413 }
7414
7415 //
7416 // should decide stream support vsc sdp colorimetry capability
7417 // before building vsc info packet
7418 //
7419 stream->use_vsc_sdp_for_colorimetry = stream->link->dpcd_caps.dpcd_rev.raw >= 0x14 &&
7420 stream->link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED &&
7421 !disable_colorimetry;
7422
7423 if (stream->out_transfer_func.tf == TRANSFER_FUNCTION_GAMMA22)
7424 tf = TRANSFER_FUNC_GAMMA_22;
7425 mod_build_vsc_infopacket(stream, &stream->vsc_infopacket, stream->output_color_space, tf);
7426 aconnector->sr_skip_count = AMDGPU_DM_PSR_ENTRY_DELAY;
7427
7428 }
7429 finish:
7430 dc_sink_release(sink);
7431
7432 return stream;
7433 }
7434
7435 /**
7436 * amdgpu_dm_connector_poll - Poll a connector to see if it's connected to a display
7437 * @aconnector: DM connector to poll (owns @base drm_connector and @dc_link)
7438 * @force: if true, force polling even when DAC load detection was used
7439 *
7440 * Used for connectors that don't support HPD (hotplug detection) to
7441 * periodically check whether the connector is connected to a display.
7442 *
7443 * When connection was determined via DAC load detection, we avoid
7444 * re-running it on normal polls to prevent visible glitches, unless
7445 * @force is set.
7446 *
7447 * Return: The probed connector status (connected/disconnected/unknown).
7448 */
7449 static enum drm_connector_status
amdgpu_dm_connector_poll(struct amdgpu_dm_connector * aconnector,bool force)7450 amdgpu_dm_connector_poll(struct amdgpu_dm_connector *aconnector, bool force)
7451 {
7452 struct drm_connector *connector = &aconnector->base;
7453 struct drm_device *dev = connector->dev;
7454 struct amdgpu_device *adev = drm_to_adev(dev);
7455 struct dc_link *link = aconnector->dc_link;
7456 enum dc_connection_type conn_type = dc_connection_none;
7457 enum drm_connector_status status = connector_status_disconnected;
7458
7459 /* When we determined the connection using DAC load detection,
7460 * do NOT poll the connector do detect disconnect because
7461 * that would run DAC load detection again which can cause
7462 * visible visual glitches.
7463 *
7464 * Only allow to poll such a connector again when forcing.
7465 */
7466 if (!force && link->local_sink && link->type == dc_connection_analog_load)
7467 return connector->status;
7468
7469 mutex_lock(&aconnector->hpd_lock);
7470
7471 if (dc_link_detect_connection_type(aconnector->dc_link, &conn_type) &&
7472 conn_type != dc_connection_none) {
7473 mutex_lock(&adev->dm.dc_lock);
7474
7475 /* Only call full link detection when a sink isn't created yet,
7476 * ie. just when the display is plugged in, otherwise we risk flickering.
7477 */
7478 if (link->local_sink ||
7479 dc_link_detect(link, DETECT_REASON_HPD))
7480 status = connector_status_connected;
7481
7482 mutex_unlock(&adev->dm.dc_lock);
7483 }
7484
7485 if (connector->status != status) {
7486 if (status == connector_status_disconnected) {
7487 if (link->local_sink)
7488 dc_sink_release(link->local_sink);
7489
7490 link->local_sink = NULL;
7491 link->dpcd_sink_count = 0;
7492 link->type = dc_connection_none;
7493 }
7494
7495 amdgpu_dm_update_connector_after_detect(aconnector);
7496 }
7497
7498 mutex_unlock(&aconnector->hpd_lock);
7499 return status;
7500 }
7501
7502 /**
7503 * amdgpu_dm_connector_detect() - Detect whether a DRM connector is connected to a display
7504 *
7505 * A connector is considered connected when it has a sink that is not NULL.
7506 * For connectors that support HPD (hotplug detection), the connection is
7507 * handled in the HPD interrupt.
7508 * For connectors that may not support HPD, such as analog connectors,
7509 * DRM will call this function repeatedly to poll them.
7510 *
7511 * Notes:
7512 * 1. This interface is NOT called in context of HPD irq.
7513 * 2. This interface *is called* in context of user-mode ioctl. Which
7514 * makes it a bad place for *any* MST-related activity.
7515 *
7516 * @connector: The DRM connector we are checking. We convert it to
7517 * amdgpu_dm_connector so we can read the DC link and state.
7518 * @force: If true, do a full detect again. This is used even when
7519 * a lighter check would normally be used to avoid flicker.
7520 *
7521 * Return: The connector status (connected, disconnected, or unknown).
7522 *
7523 */
7524 static enum drm_connector_status
amdgpu_dm_connector_detect(struct drm_connector * connector,bool force)7525 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
7526 {
7527 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7528
7529 update_subconnector_property(aconnector);
7530
7531 if (aconnector->base.force == DRM_FORCE_ON ||
7532 aconnector->base.force == DRM_FORCE_ON_DIGITAL)
7533 return connector_status_connected;
7534 else if (aconnector->base.force == DRM_FORCE_OFF)
7535 return connector_status_disconnected;
7536
7537 /* Poll analog connectors and only when either
7538 * disconnected or connected to an analog display.
7539 */
7540 if (drm_kms_helper_is_poll_worker() &&
7541 dc_connector_supports_analog(aconnector->dc_link->link_id.id) &&
7542 (!aconnector->dc_sink || aconnector->dc_sink->edid_caps.analog))
7543 return amdgpu_dm_connector_poll(aconnector, force);
7544
7545 return (aconnector->dc_sink ? connector_status_connected :
7546 connector_status_disconnected);
7547 }
7548
amdgpu_dm_connector_atomic_set_property(struct drm_connector * connector,struct drm_connector_state * connector_state,struct drm_property * property,uint64_t val)7549 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
7550 struct drm_connector_state *connector_state,
7551 struct drm_property *property,
7552 uint64_t val)
7553 {
7554 struct drm_device *dev = connector->dev;
7555 struct amdgpu_device *adev = drm_to_adev(dev);
7556 struct dm_connector_state *dm_old_state =
7557 to_dm_connector_state(connector->state);
7558 struct dm_connector_state *dm_new_state =
7559 to_dm_connector_state(connector_state);
7560
7561 int ret = -EINVAL;
7562
7563 if (property == dev->mode_config.scaling_mode_property) {
7564 enum amdgpu_rmx_type rmx_type;
7565
7566 switch (val) {
7567 case DRM_MODE_SCALE_CENTER:
7568 rmx_type = RMX_CENTER;
7569 break;
7570 case DRM_MODE_SCALE_ASPECT:
7571 rmx_type = RMX_ASPECT;
7572 break;
7573 case DRM_MODE_SCALE_FULLSCREEN:
7574 rmx_type = RMX_FULL;
7575 break;
7576 case DRM_MODE_SCALE_NONE:
7577 default:
7578 rmx_type = RMX_OFF;
7579 break;
7580 }
7581
7582 if (dm_old_state->scaling == rmx_type)
7583 return 0;
7584
7585 dm_new_state->scaling = rmx_type;
7586 ret = 0;
7587 } else if (property == adev->mode_info.underscan_hborder_property) {
7588 dm_new_state->underscan_hborder = val;
7589 ret = 0;
7590 } else if (property == adev->mode_info.underscan_vborder_property) {
7591 dm_new_state->underscan_vborder = val;
7592 ret = 0;
7593 } else if (property == adev->mode_info.underscan_property) {
7594 dm_new_state->underscan_enable = val;
7595 ret = 0;
7596 } else if (property == adev->mode_info.abm_level_property) {
7597 switch (val) {
7598 case ABM_SYSFS_CONTROL:
7599 dm_new_state->abm_sysfs_forbidden = false;
7600 break;
7601 case ABM_LEVEL_OFF:
7602 dm_new_state->abm_sysfs_forbidden = true;
7603 dm_new_state->abm_level = ABM_LEVEL_IMMEDIATE_DISABLE;
7604 break;
7605 default:
7606 dm_new_state->abm_sysfs_forbidden = true;
7607 dm_new_state->abm_level = val;
7608 }
7609 ret = 0;
7610 }
7611
7612 return ret;
7613 }
7614
amdgpu_dm_connector_atomic_get_property(struct drm_connector * connector,const struct drm_connector_state * state,struct drm_property * property,uint64_t * val)7615 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
7616 const struct drm_connector_state *state,
7617 struct drm_property *property,
7618 uint64_t *val)
7619 {
7620 struct drm_device *dev = connector->dev;
7621 struct amdgpu_device *adev = drm_to_adev(dev);
7622 struct dm_connector_state *dm_state =
7623 to_dm_connector_state(state);
7624 int ret = -EINVAL;
7625
7626 if (property == dev->mode_config.scaling_mode_property) {
7627 switch (dm_state->scaling) {
7628 case RMX_CENTER:
7629 *val = DRM_MODE_SCALE_CENTER;
7630 break;
7631 case RMX_ASPECT:
7632 *val = DRM_MODE_SCALE_ASPECT;
7633 break;
7634 case RMX_FULL:
7635 *val = DRM_MODE_SCALE_FULLSCREEN;
7636 break;
7637 case RMX_OFF:
7638 default:
7639 *val = DRM_MODE_SCALE_NONE;
7640 break;
7641 }
7642 ret = 0;
7643 } else if (property == adev->mode_info.underscan_hborder_property) {
7644 *val = dm_state->underscan_hborder;
7645 ret = 0;
7646 } else if (property == adev->mode_info.underscan_vborder_property) {
7647 *val = dm_state->underscan_vborder;
7648 ret = 0;
7649 } else if (property == adev->mode_info.underscan_property) {
7650 *val = dm_state->underscan_enable;
7651 ret = 0;
7652 } else if (property == adev->mode_info.abm_level_property) {
7653 if (!dm_state->abm_sysfs_forbidden)
7654 *val = ABM_SYSFS_CONTROL;
7655 else
7656 *val = (dm_state->abm_level != ABM_LEVEL_IMMEDIATE_DISABLE) ?
7657 dm_state->abm_level : 0;
7658 ret = 0;
7659 }
7660
7661 return ret;
7662 }
7663
7664 /**
7665 * DOC: panel power savings
7666 *
7667 * The display manager allows you to set your desired **panel power savings**
7668 * level (between 0-4, with 0 representing off), e.g. using the following::
7669 *
7670 * # echo 3 > /sys/class/drm/card0-eDP-1/amdgpu/panel_power_savings
7671 *
7672 * Modifying this value can have implications on color accuracy, so tread
7673 * carefully.
7674 */
7675
panel_power_savings_show(struct device * device,struct device_attribute * attr,char * buf)7676 static ssize_t panel_power_savings_show(struct device *device,
7677 struct device_attribute *attr,
7678 char *buf)
7679 {
7680 struct drm_connector *connector = dev_get_drvdata(device);
7681 struct drm_device *dev = connector->dev;
7682 u8 val;
7683
7684 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
7685 val = to_dm_connector_state(connector->state)->abm_level ==
7686 ABM_LEVEL_IMMEDIATE_DISABLE ? 0 :
7687 to_dm_connector_state(connector->state)->abm_level;
7688 drm_modeset_unlock(&dev->mode_config.connection_mutex);
7689
7690 return sysfs_emit(buf, "%u\n", val);
7691 }
7692
panel_power_savings_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)7693 static ssize_t panel_power_savings_store(struct device *device,
7694 struct device_attribute *attr,
7695 const char *buf, size_t count)
7696 {
7697 struct drm_connector *connector = dev_get_drvdata(device);
7698 struct drm_device *dev = connector->dev;
7699 long val;
7700 int ret;
7701
7702 ret = kstrtol(buf, 0, &val);
7703
7704 if (ret)
7705 return ret;
7706
7707 if (val < 0 || val > 4)
7708 return -EINVAL;
7709
7710 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
7711 if (to_dm_connector_state(connector->state)->abm_sysfs_forbidden)
7712 ret = -EBUSY;
7713 else
7714 to_dm_connector_state(connector->state)->abm_level = val ?:
7715 ABM_LEVEL_IMMEDIATE_DISABLE;
7716 drm_modeset_unlock(&dev->mode_config.connection_mutex);
7717
7718 if (ret)
7719 return ret;
7720
7721 drm_kms_helper_hotplug_event(dev);
7722
7723 return count;
7724 }
7725
7726 static DEVICE_ATTR_RW(panel_power_savings);
7727
7728 static struct attribute *amdgpu_attrs[] = {
7729 &dev_attr_panel_power_savings.attr,
7730 NULL
7731 };
7732
7733 static const struct attribute_group amdgpu_group = {
7734 .name = "amdgpu",
7735 .attrs = amdgpu_attrs
7736 };
7737
7738 static bool
amdgpu_dm_should_create_sysfs(struct amdgpu_dm_connector * amdgpu_dm_connector)7739 amdgpu_dm_should_create_sysfs(struct amdgpu_dm_connector *amdgpu_dm_connector)
7740 {
7741 if (amdgpu_dm_abm_level >= 0)
7742 return false;
7743
7744 if (amdgpu_dm_connector->base.connector_type != DRM_MODE_CONNECTOR_eDP)
7745 return false;
7746
7747 /* check for OLED panels */
7748 if (amdgpu_dm_connector->bl_idx >= 0) {
7749 struct drm_device *drm = amdgpu_dm_connector->base.dev;
7750 struct amdgpu_display_manager *dm = &drm_to_adev(drm)->dm;
7751 struct amdgpu_dm_backlight_caps *caps;
7752
7753 caps = &dm->backlight_caps[amdgpu_dm_connector->bl_idx];
7754 if (caps->aux_support)
7755 return false;
7756 }
7757
7758 return true;
7759 }
7760
amdgpu_dm_connector_unregister(struct drm_connector * connector)7761 static void amdgpu_dm_connector_unregister(struct drm_connector *connector)
7762 {
7763 struct amdgpu_dm_connector *amdgpu_dm_connector = to_amdgpu_dm_connector(connector);
7764
7765 if (amdgpu_dm_should_create_sysfs(amdgpu_dm_connector))
7766 sysfs_remove_group(&connector->kdev->kobj, &amdgpu_group);
7767
7768 cec_notifier_conn_unregister(amdgpu_dm_connector->notifier);
7769 drm_dp_aux_unregister(&amdgpu_dm_connector->dm_dp_aux.aux);
7770 }
7771
amdgpu_dm_connector_destroy(struct drm_connector * connector)7772 static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
7773 {
7774 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7775 struct amdgpu_device *adev = drm_to_adev(connector->dev);
7776 struct amdgpu_display_manager *dm = &adev->dm;
7777
7778 /*
7779 * Call only if mst_mgr was initialized before since it's not done
7780 * for all connector types.
7781 */
7782 if (aconnector->mst_mgr.dev)
7783 drm_dp_mst_topology_mgr_destroy(&aconnector->mst_mgr);
7784
7785 /* Cancel and flush any pending HDMI HPD debounce work */
7786 if (aconnector->hdmi_hpd_debounce_delay_ms) {
7787 cancel_delayed_work_sync(&aconnector->hdmi_hpd_debounce_work);
7788 if (aconnector->hdmi_prev_sink) {
7789 dc_sink_release(aconnector->hdmi_prev_sink);
7790 aconnector->hdmi_prev_sink = NULL;
7791 }
7792 }
7793
7794 if (aconnector->bl_idx != -1) {
7795 backlight_device_unregister(dm->backlight_dev[aconnector->bl_idx]);
7796 dm->backlight_dev[aconnector->bl_idx] = NULL;
7797 }
7798
7799 if (aconnector->dc_em_sink)
7800 dc_sink_release(aconnector->dc_em_sink);
7801 aconnector->dc_em_sink = NULL;
7802 if (aconnector->dc_sink)
7803 dc_sink_release(aconnector->dc_sink);
7804 aconnector->dc_sink = NULL;
7805
7806 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
7807 drm_connector_unregister(connector);
7808 drm_connector_cleanup(connector);
7809 kfree(aconnector->dm_dp_aux.aux.name);
7810
7811 kfree(connector);
7812 }
7813
amdgpu_dm_connector_funcs_reset(struct drm_connector * connector)7814 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
7815 {
7816 struct dm_connector_state *state =
7817 to_dm_connector_state(connector->state);
7818
7819 if (connector->state)
7820 __drm_atomic_helper_connector_destroy_state(connector->state);
7821
7822 kfree(state);
7823
7824 state = kzalloc_obj(*state);
7825
7826 if (state) {
7827 state->scaling = RMX_OFF;
7828 state->underscan_enable = false;
7829 state->underscan_hborder = 0;
7830 state->underscan_vborder = 0;
7831 state->base.max_requested_bpc = 8;
7832 state->vcpi_slots = 0;
7833 state->pbn = 0;
7834
7835 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
7836 if (amdgpu_dm_abm_level <= 0)
7837 state->abm_level = ABM_LEVEL_IMMEDIATE_DISABLE;
7838 else
7839 state->abm_level = amdgpu_dm_abm_level;
7840 }
7841
7842 __drm_atomic_helper_connector_reset(connector, &state->base);
7843 }
7844 }
7845
7846 struct drm_connector_state *
amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector * connector)7847 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
7848 {
7849 struct dm_connector_state *state =
7850 to_dm_connector_state(connector->state);
7851
7852 struct dm_connector_state *new_state =
7853 kmemdup(state, sizeof(*state), GFP_KERNEL);
7854
7855 if (!new_state)
7856 return NULL;
7857
7858 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
7859
7860 new_state->freesync_capable = state->freesync_capable;
7861 new_state->abm_level = state->abm_level;
7862 new_state->scaling = state->scaling;
7863 new_state->underscan_enable = state->underscan_enable;
7864 new_state->underscan_hborder = state->underscan_hborder;
7865 new_state->underscan_vborder = state->underscan_vborder;
7866 new_state->vcpi_slots = state->vcpi_slots;
7867 new_state->pbn = state->pbn;
7868 return &new_state->base;
7869 }
7870
7871 static int
amdgpu_dm_connector_late_register(struct drm_connector * connector)7872 amdgpu_dm_connector_late_register(struct drm_connector *connector)
7873 {
7874 struct amdgpu_dm_connector *amdgpu_dm_connector =
7875 to_amdgpu_dm_connector(connector);
7876 int r;
7877
7878 if (amdgpu_dm_should_create_sysfs(amdgpu_dm_connector)) {
7879 r = sysfs_create_group(&connector->kdev->kobj,
7880 &amdgpu_group);
7881 if (r)
7882 return r;
7883 }
7884
7885 amdgpu_dm_register_backlight_device(amdgpu_dm_connector);
7886
7887 if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
7888 (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) {
7889 amdgpu_dm_connector->dm_dp_aux.aux.dev = connector->kdev;
7890 r = drm_dp_aux_register(&amdgpu_dm_connector->dm_dp_aux.aux);
7891 if (r)
7892 return r;
7893 }
7894
7895 #if defined(CONFIG_DEBUG_FS)
7896 connector_debugfs_init(amdgpu_dm_connector);
7897 #endif
7898
7899 return 0;
7900 }
7901
amdgpu_dm_connector_funcs_force(struct drm_connector * connector)7902 static void amdgpu_dm_connector_funcs_force(struct drm_connector *connector)
7903 {
7904 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7905 struct dc_link *dc_link = aconnector->dc_link;
7906 struct dc_sink *dc_em_sink = aconnector->dc_em_sink;
7907 const struct drm_edid *drm_edid;
7908 struct i2c_adapter *ddc;
7909 struct drm_device *dev = connector->dev;
7910
7911 if (dc_link && dc_link->aux_mode)
7912 ddc = &aconnector->dm_dp_aux.aux.ddc;
7913 else
7914 ddc = &aconnector->i2c->base;
7915
7916 drm_edid = drm_edid_read_ddc(connector, ddc);
7917 drm_edid_connector_update(connector, drm_edid);
7918 if (!drm_edid) {
7919 drm_err(dev, "No EDID found on connector: %s.\n", connector->name);
7920 return;
7921 }
7922
7923 aconnector->drm_edid = drm_edid;
7924 /* Update emulated (virtual) sink's EDID */
7925 if (dc_em_sink && dc_link) {
7926 // FIXME: Get rid of drm_edid_raw()
7927 const struct edid *edid = drm_edid_raw(drm_edid);
7928
7929 memset(&dc_em_sink->edid_caps, 0, sizeof(struct dc_edid_caps));
7930 memmove(dc_em_sink->dc_edid.raw_edid, edid,
7931 (edid->extensions + 1) * EDID_LENGTH);
7932 dm_helpers_parse_edid_caps(
7933 dc_link,
7934 &dc_em_sink->dc_edid,
7935 &dc_em_sink->edid_caps);
7936 }
7937 }
7938
7939 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
7940 .reset = amdgpu_dm_connector_funcs_reset,
7941 .detect = amdgpu_dm_connector_detect,
7942 .fill_modes = drm_helper_probe_single_connector_modes,
7943 .destroy = amdgpu_dm_connector_destroy,
7944 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
7945 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
7946 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
7947 .atomic_get_property = amdgpu_dm_connector_atomic_get_property,
7948 .late_register = amdgpu_dm_connector_late_register,
7949 .early_unregister = amdgpu_dm_connector_unregister,
7950 .force = amdgpu_dm_connector_funcs_force
7951 };
7952
get_modes(struct drm_connector * connector)7953 static int get_modes(struct drm_connector *connector)
7954 {
7955 return amdgpu_dm_connector_get_modes(connector);
7956 }
7957
create_eml_sink(struct amdgpu_dm_connector * aconnector)7958 static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
7959 {
7960 struct drm_connector *connector = &aconnector->base;
7961 struct dc_link *dc_link = aconnector->dc_link;
7962 struct dc_sink_init_data init_params = {
7963 .link = aconnector->dc_link,
7964 .sink_signal = SIGNAL_TYPE_VIRTUAL
7965 };
7966 const struct drm_edid *drm_edid;
7967 const struct edid *edid;
7968 struct i2c_adapter *ddc;
7969
7970 if (dc_link && dc_link->aux_mode)
7971 ddc = &aconnector->dm_dp_aux.aux.ddc;
7972 else
7973 ddc = &aconnector->i2c->base;
7974
7975 drm_edid = drm_edid_read_ddc(connector, ddc);
7976 drm_edid_connector_update(connector, drm_edid);
7977 if (!drm_edid) {
7978 drm_err(connector->dev, "No EDID found on connector: %s.\n", connector->name);
7979 return;
7980 }
7981
7982 if (connector->display_info.is_hdmi)
7983 init_params.sink_signal = SIGNAL_TYPE_HDMI_TYPE_A;
7984
7985 aconnector->drm_edid = drm_edid;
7986
7987 edid = drm_edid_raw(drm_edid); // FIXME: Get rid of drm_edid_raw()
7988 aconnector->dc_em_sink = dc_link_add_remote_sink(
7989 aconnector->dc_link,
7990 (uint8_t *)edid,
7991 (edid->extensions + 1) * EDID_LENGTH,
7992 &init_params);
7993
7994 if (aconnector->base.force == DRM_FORCE_ON) {
7995 aconnector->dc_sink = aconnector->dc_link->local_sink ?
7996 aconnector->dc_link->local_sink :
7997 aconnector->dc_em_sink;
7998 if (aconnector->dc_sink)
7999 dc_sink_retain(aconnector->dc_sink);
8000 }
8001 }
8002
handle_edid_mgmt(struct amdgpu_dm_connector * aconnector)8003 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
8004 {
8005 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
8006
8007 /*
8008 * In case of headless boot with force on for DP managed connector
8009 * Those settings have to be != 0 to get initial modeset
8010 */
8011 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
8012 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
8013 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
8014 }
8015
8016 create_eml_sink(aconnector);
8017 }
8018
dm_validate_stream_and_context(struct dc * dc,struct dc_stream_state * stream)8019 static enum dc_status dm_validate_stream_and_context(struct dc *dc,
8020 struct dc_stream_state *stream)
8021 {
8022 enum dc_status dc_result = DC_ERROR_UNEXPECTED;
8023 struct dc_plane_state *dc_plane_state = NULL;
8024 struct dc_state *dc_state = NULL;
8025
8026 if (!stream)
8027 goto cleanup;
8028
8029 dc_plane_state = dc_create_plane_state(dc);
8030 if (!dc_plane_state)
8031 goto cleanup;
8032
8033 dc_state = dc_state_create(dc, NULL);
8034 if (!dc_state)
8035 goto cleanup;
8036
8037 /* populate stream to plane */
8038 dc_plane_state->src_rect.height = stream->src.height;
8039 dc_plane_state->src_rect.width = stream->src.width;
8040 dc_plane_state->dst_rect.height = stream->src.height;
8041 dc_plane_state->dst_rect.width = stream->src.width;
8042 dc_plane_state->clip_rect.height = stream->src.height;
8043 dc_plane_state->clip_rect.width = stream->src.width;
8044 dc_plane_state->plane_size.surface_pitch = ((stream->src.width + 255) / 256) * 256;
8045 dc_plane_state->plane_size.surface_size.height = stream->src.height;
8046 dc_plane_state->plane_size.surface_size.width = stream->src.width;
8047 dc_plane_state->plane_size.chroma_size.height = stream->src.height;
8048 dc_plane_state->plane_size.chroma_size.width = stream->src.width;
8049 dc_plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
8050 dc_plane_state->tiling_info.gfx9.swizzle = DC_SW_UNKNOWN;
8051 dc_plane_state->rotation = ROTATION_ANGLE_0;
8052 dc_plane_state->is_tiling_rotated = false;
8053 dc_plane_state->tiling_info.gfx8.array_mode = DC_ARRAY_LINEAR_GENERAL;
8054
8055 dc_result = dc_validate_stream(dc, stream);
8056 if (dc_result == DC_OK)
8057 dc_result = dc_validate_plane(dc, dc_plane_state);
8058
8059 if (dc_result == DC_OK)
8060 dc_result = dc_state_add_stream(dc, dc_state, stream);
8061
8062 if (dc_result == DC_OK && !dc_state_add_plane(
8063 dc,
8064 stream,
8065 dc_plane_state,
8066 dc_state))
8067 dc_result = DC_FAIL_ATTACH_SURFACES;
8068
8069 if (dc_result == DC_OK)
8070 dc_result = dc_validate_global_state(dc, dc_state, DC_VALIDATE_MODE_ONLY);
8071
8072 cleanup:
8073 if (dc_state)
8074 dc_state_release(dc_state);
8075
8076 if (dc_plane_state)
8077 dc_plane_state_release(dc_plane_state);
8078
8079 return dc_result;
8080 }
8081
8082 struct dc_stream_state *
create_validate_stream_for_sink(struct drm_connector * connector,const struct drm_display_mode * drm_mode,const struct dm_connector_state * dm_state,const struct dc_stream_state * old_stream)8083 create_validate_stream_for_sink(struct drm_connector *connector,
8084 const struct drm_display_mode *drm_mode,
8085 const struct dm_connector_state *dm_state,
8086 const struct dc_stream_state *old_stream)
8087 {
8088 struct amdgpu_dm_connector *aconnector = NULL;
8089 struct amdgpu_device *adev = drm_to_adev(connector->dev);
8090 struct dc_stream_state *stream;
8091 const struct drm_connector_state *drm_state = dm_state ? &dm_state->base : NULL;
8092 int requested_bpc = drm_state ? drm_state->max_requested_bpc : 8;
8093 enum dc_status dc_result = DC_OK;
8094 uint8_t bpc_limit = 6;
8095
8096 if (!dm_state)
8097 return NULL;
8098
8099 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
8100 aconnector = to_amdgpu_dm_connector(connector);
8101
8102 if (aconnector &&
8103 (aconnector->dc_link->connector_signal == SIGNAL_TYPE_HDMI_TYPE_A ||
8104 aconnector->dc_link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER))
8105 bpc_limit = 8;
8106
8107 do {
8108 drm_dbg_kms(connector->dev, "Trying with %d bpc\n", requested_bpc);
8109 stream = create_stream_for_sink(connector, drm_mode,
8110 dm_state, old_stream,
8111 requested_bpc);
8112 if (stream == NULL) {
8113 drm_err(adev_to_drm(adev), "Failed to create stream for sink!\n");
8114 break;
8115 }
8116
8117 dc_result = dc_validate_stream(adev->dm.dc, stream);
8118
8119 if (!aconnector) /* writeback connector */
8120 return stream;
8121
8122 if (dc_result == DC_OK && stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
8123 dc_result = dm_dp_mst_is_port_support_mode(aconnector, stream);
8124
8125 if (dc_result == DC_OK)
8126 dc_result = dm_validate_stream_and_context(adev->dm.dc, stream);
8127
8128 if (dc_result != DC_OK) {
8129 drm_dbg_kms(connector->dev, "Pruned mode %d x %d (clk %d) %s %s -- %s\n",
8130 drm_mode->hdisplay,
8131 drm_mode->vdisplay,
8132 drm_mode->clock,
8133 dc_pixel_encoding_to_str(stream->timing.pixel_encoding),
8134 dc_color_depth_to_str(stream->timing.display_color_depth),
8135 dc_status_to_str(dc_result));
8136
8137 dc_stream_release(stream);
8138 stream = NULL;
8139 requested_bpc -= 2; /* lower bpc to retry validation */
8140 }
8141
8142 } while (stream == NULL && requested_bpc >= bpc_limit);
8143
8144 switch (dc_result) {
8145 /*
8146 * If we failed to validate DP bandwidth stream with the requested RGB color depth,
8147 * we try to fallback and configure in order:
8148 * YUV422 (8bpc, 6bpc)
8149 * YUV420 (8bpc, 6bpc)
8150 */
8151 case DC_FAIL_ENC_VALIDATE:
8152 case DC_EXCEED_DONGLE_CAP:
8153 case DC_NO_DP_LINK_BANDWIDTH:
8154 /* recursively entered twice and already tried both YUV422 and YUV420 */
8155 if (aconnector->force_yuv422_output && aconnector->force_yuv420_output)
8156 break;
8157 /* first failure; try YUV422 */
8158 if (!aconnector->force_yuv422_output) {
8159 drm_dbg_kms(connector->dev, "%s:%d Validation failed with %d, retrying w/ YUV422\n",
8160 __func__, __LINE__, dc_result);
8161 aconnector->force_yuv422_output = true;
8162 /* recursively entered and YUV422 failed, try YUV420 */
8163 } else if (!aconnector->force_yuv420_output) {
8164 drm_dbg_kms(connector->dev, "%s:%d Validation failed with %d, retrying w/ YUV420\n",
8165 __func__, __LINE__, dc_result);
8166 aconnector->force_yuv420_output = true;
8167 }
8168 stream = create_validate_stream_for_sink(connector, drm_mode,
8169 dm_state, old_stream);
8170 aconnector->force_yuv422_output = false;
8171 aconnector->force_yuv420_output = false;
8172 break;
8173 case DC_OK:
8174 break;
8175 default:
8176 drm_dbg_kms(connector->dev, "%s:%d Unhandled validation failure %d\n",
8177 __func__, __LINE__, dc_result);
8178 break;
8179 }
8180
8181 return stream;
8182 }
8183
amdgpu_dm_connector_mode_valid(struct drm_connector * connector,const struct drm_display_mode * mode)8184 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
8185 const struct drm_display_mode *mode)
8186 {
8187 int result = MODE_ERROR;
8188 struct dc_sink *dc_sink;
8189 struct drm_display_mode *test_mode;
8190 /* TODO: Unhardcode stream count */
8191 struct dc_stream_state *stream;
8192 /* we always have an amdgpu_dm_connector here since we got
8193 * here via the amdgpu_dm_connector_helper_funcs
8194 */
8195 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
8196
8197 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
8198 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
8199 return result;
8200
8201 /*
8202 * Only run this the first time mode_valid is called to initilialize
8203 * EDID mgmt
8204 */
8205 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
8206 !aconnector->dc_em_sink)
8207 handle_edid_mgmt(aconnector);
8208
8209 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
8210
8211 if (dc_sink == NULL && aconnector->base.force != DRM_FORCE_ON_DIGITAL &&
8212 aconnector->base.force != DRM_FORCE_ON) {
8213 drm_err(connector->dev, "dc_sink is NULL!\n");
8214 goto fail;
8215 }
8216
8217 test_mode = drm_mode_duplicate(connector->dev, mode);
8218 if (!test_mode)
8219 goto fail;
8220
8221 drm_mode_set_crtcinfo(test_mode, 0);
8222
8223 stream = create_validate_stream_for_sink(connector, test_mode,
8224 to_dm_connector_state(connector->state),
8225 NULL);
8226 drm_mode_destroy(connector->dev, test_mode);
8227 if (stream) {
8228 dc_stream_release(stream);
8229 result = MODE_OK;
8230 }
8231
8232 fail:
8233 /* TODO: error handling*/
8234 return result;
8235 }
8236
fill_hdr_info_packet(const struct drm_connector_state * state,struct dc_info_packet * out)8237 static int fill_hdr_info_packet(const struct drm_connector_state *state,
8238 struct dc_info_packet *out)
8239 {
8240 struct hdmi_drm_infoframe frame;
8241 unsigned char buf[30]; /* 26 + 4 */
8242 ssize_t len;
8243 int ret, i;
8244
8245 memset(out, 0, sizeof(*out));
8246
8247 if (!state->hdr_output_metadata)
8248 return 0;
8249
8250 ret = drm_hdmi_infoframe_set_hdr_metadata(&frame, state);
8251 if (ret)
8252 return ret;
8253
8254 len = hdmi_drm_infoframe_pack_only(&frame, buf, sizeof(buf));
8255 if (len < 0)
8256 return (int)len;
8257
8258 /* Static metadata is a fixed 26 bytes + 4 byte header. */
8259 if (len != 30)
8260 return -EINVAL;
8261
8262 /* Prepare the infopacket for DC. */
8263 switch (state->connector->connector_type) {
8264 case DRM_MODE_CONNECTOR_HDMIA:
8265 out->hb0 = 0x87; /* type */
8266 out->hb1 = 0x01; /* version */
8267 out->hb2 = 0x1A; /* length */
8268 out->sb[0] = buf[3]; /* checksum */
8269 i = 1;
8270 break;
8271
8272 case DRM_MODE_CONNECTOR_DisplayPort:
8273 case DRM_MODE_CONNECTOR_eDP:
8274 out->hb0 = 0x00; /* sdp id, zero */
8275 out->hb1 = 0x87; /* type */
8276 out->hb2 = 0x1D; /* payload len - 1 */
8277 out->hb3 = (0x13 << 2); /* sdp version */
8278 out->sb[0] = 0x01; /* version */
8279 out->sb[1] = 0x1A; /* length */
8280 i = 2;
8281 break;
8282
8283 default:
8284 return -EINVAL;
8285 }
8286
8287 memcpy(&out->sb[i], &buf[4], 26);
8288 out->valid = true;
8289
8290 print_hex_dump(KERN_DEBUG, "HDR SB:", DUMP_PREFIX_NONE, 16, 1, out->sb,
8291 sizeof(out->sb), false);
8292
8293 return 0;
8294 }
8295
8296 static int
amdgpu_dm_connector_atomic_check(struct drm_connector * conn,struct drm_atomic_state * state)8297 amdgpu_dm_connector_atomic_check(struct drm_connector *conn,
8298 struct drm_atomic_state *state)
8299 {
8300 struct drm_connector_state *new_con_state =
8301 drm_atomic_get_new_connector_state(state, conn);
8302 struct drm_connector_state *old_con_state =
8303 drm_atomic_get_old_connector_state(state, conn);
8304 struct drm_crtc *crtc = new_con_state->crtc;
8305 struct drm_crtc_state *new_crtc_state;
8306 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(conn);
8307 int ret;
8308
8309 if (WARN_ON(unlikely(!old_con_state || !new_con_state)))
8310 return -EINVAL;
8311
8312 trace_amdgpu_dm_connector_atomic_check(new_con_state);
8313
8314 if (conn->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
8315 ret = drm_dp_mst_root_conn_atomic_check(new_con_state, &aconn->mst_mgr);
8316 if (ret < 0)
8317 return ret;
8318 }
8319
8320 if (!crtc)
8321 return 0;
8322
8323 if (new_con_state->privacy_screen_sw_state != old_con_state->privacy_screen_sw_state) {
8324 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8325 if (IS_ERR(new_crtc_state))
8326 return PTR_ERR(new_crtc_state);
8327
8328 new_crtc_state->mode_changed = true;
8329 }
8330
8331 if (new_con_state->colorspace != old_con_state->colorspace) {
8332 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8333 if (IS_ERR(new_crtc_state))
8334 return PTR_ERR(new_crtc_state);
8335
8336 new_crtc_state->mode_changed = true;
8337 }
8338
8339 if (new_con_state->content_type != old_con_state->content_type) {
8340 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8341 if (IS_ERR(new_crtc_state))
8342 return PTR_ERR(new_crtc_state);
8343
8344 new_crtc_state->mode_changed = true;
8345 }
8346
8347 if (!drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state)) {
8348 struct dc_info_packet hdr_infopacket;
8349
8350 ret = fill_hdr_info_packet(new_con_state, &hdr_infopacket);
8351 if (ret)
8352 return ret;
8353
8354 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8355 if (IS_ERR(new_crtc_state))
8356 return PTR_ERR(new_crtc_state);
8357
8358 /*
8359 * DC considers the stream backends changed if the
8360 * static metadata changes. Forcing the modeset also
8361 * gives a simple way for userspace to switch from
8362 * 8bpc to 10bpc when setting the metadata to enter
8363 * or exit HDR.
8364 *
8365 * Changing the static metadata after it's been
8366 * set is permissible, however. So only force a
8367 * modeset if we're entering or exiting HDR.
8368 */
8369 new_crtc_state->mode_changed = new_crtc_state->mode_changed ||
8370 !old_con_state->hdr_output_metadata ||
8371 !new_con_state->hdr_output_metadata;
8372 }
8373
8374 return 0;
8375 }
8376
8377 static const struct drm_connector_helper_funcs
8378 amdgpu_dm_connector_helper_funcs = {
8379 /*
8380 * If hotplugging a second bigger display in FB Con mode, bigger resolution
8381 * modes will be filtered by drm_mode_validate_size(), and those modes
8382 * are missing after user start lightdm. So we need to renew modes list.
8383 * in get_modes call back, not just return the modes count
8384 */
8385 .get_modes = get_modes,
8386 .mode_valid = amdgpu_dm_connector_mode_valid,
8387 .atomic_check = amdgpu_dm_connector_atomic_check,
8388 };
8389
dm_encoder_helper_disable(struct drm_encoder * encoder)8390 static void dm_encoder_helper_disable(struct drm_encoder *encoder)
8391 {
8392
8393 }
8394
convert_dc_color_depth_into_bpc(enum dc_color_depth display_color_depth)8395 int convert_dc_color_depth_into_bpc(enum dc_color_depth display_color_depth)
8396 {
8397 switch (display_color_depth) {
8398 case COLOR_DEPTH_666:
8399 return 6;
8400 case COLOR_DEPTH_888:
8401 return 8;
8402 case COLOR_DEPTH_101010:
8403 return 10;
8404 case COLOR_DEPTH_121212:
8405 return 12;
8406 case COLOR_DEPTH_141414:
8407 return 14;
8408 case COLOR_DEPTH_161616:
8409 return 16;
8410 default:
8411 break;
8412 }
8413 return 0;
8414 }
8415
dm_encoder_helper_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)8416 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
8417 struct drm_crtc_state *crtc_state,
8418 struct drm_connector_state *conn_state)
8419 {
8420 struct drm_atomic_state *state = crtc_state->state;
8421 struct drm_connector *connector = conn_state->connector;
8422 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
8423 struct dm_connector_state *dm_new_connector_state = to_dm_connector_state(conn_state);
8424 const struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
8425 struct drm_dp_mst_topology_mgr *mst_mgr;
8426 struct drm_dp_mst_port *mst_port;
8427 struct drm_dp_mst_topology_state *mst_state;
8428 enum dc_color_depth color_depth;
8429 int clock, bpp = 0;
8430 bool is_y420 = false;
8431
8432 if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
8433 (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
8434 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8435 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8436 enum drm_mode_status result;
8437
8438 result = drm_crtc_helper_mode_valid_fixed(encoder->crtc, adjusted_mode, native_mode);
8439 if (result != MODE_OK && dm_new_connector_state->scaling == RMX_OFF) {
8440 drm_dbg_driver(encoder->dev,
8441 "mode %dx%d@%dHz is not native, enabling scaling\n",
8442 adjusted_mode->hdisplay, adjusted_mode->vdisplay,
8443 drm_mode_vrefresh(adjusted_mode));
8444 dm_new_connector_state->scaling = RMX_ASPECT;
8445 }
8446 return 0;
8447 }
8448
8449 if (!aconnector->mst_output_port)
8450 return 0;
8451
8452 mst_port = aconnector->mst_output_port;
8453 mst_mgr = &aconnector->mst_root->mst_mgr;
8454
8455 if (!crtc_state->connectors_changed && !crtc_state->mode_changed)
8456 return 0;
8457
8458 mst_state = drm_atomic_get_mst_topology_state(state, mst_mgr);
8459 if (IS_ERR(mst_state))
8460 return PTR_ERR(mst_state);
8461
8462 mst_state->pbn_div.full = dm_mst_get_pbn_divider(aconnector->mst_root->dc_link);
8463
8464 if (!state->duplicated) {
8465 int max_bpc = conn_state->max_requested_bpc;
8466
8467 is_y420 = drm_mode_is_420_also(&connector->display_info, adjusted_mode) &&
8468 aconnector->force_yuv420_output;
8469 color_depth = convert_color_depth_from_display_info(connector,
8470 is_y420,
8471 max_bpc);
8472 bpp = convert_dc_color_depth_into_bpc(color_depth) * 3;
8473 clock = adjusted_mode->clock;
8474 dm_new_connector_state->pbn = drm_dp_calc_pbn_mode(clock, bpp << 4);
8475 }
8476
8477 dm_new_connector_state->vcpi_slots =
8478 drm_dp_atomic_find_time_slots(state, mst_mgr, mst_port,
8479 dm_new_connector_state->pbn);
8480 if (dm_new_connector_state->vcpi_slots < 0) {
8481 drm_dbg_atomic(connector->dev, "failed finding vcpi slots: %d\n", (int)dm_new_connector_state->vcpi_slots);
8482 return dm_new_connector_state->vcpi_slots;
8483 }
8484 return 0;
8485 }
8486
8487 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
8488 .disable = dm_encoder_helper_disable,
8489 .atomic_check = dm_encoder_helper_atomic_check
8490 };
8491
dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_state * state,struct dc_state * dc_state,struct dsc_mst_fairness_vars * vars)8492 static int dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_state *state,
8493 struct dc_state *dc_state,
8494 struct dsc_mst_fairness_vars *vars)
8495 {
8496 struct dc_stream_state *stream = NULL;
8497 struct drm_connector *connector;
8498 struct drm_connector_state *new_con_state;
8499 struct amdgpu_dm_connector *aconnector;
8500 struct dm_connector_state *dm_conn_state;
8501 int i, j, ret;
8502 int vcpi, pbn_div, pbn = 0, slot_num = 0;
8503
8504 for_each_new_connector_in_state(state, connector, new_con_state, i) {
8505
8506 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
8507 continue;
8508
8509 aconnector = to_amdgpu_dm_connector(connector);
8510
8511 if (!aconnector->mst_output_port)
8512 continue;
8513
8514 if (!new_con_state || !new_con_state->crtc)
8515 continue;
8516
8517 dm_conn_state = to_dm_connector_state(new_con_state);
8518
8519 for (j = 0; j < dc_state->stream_count; j++) {
8520 stream = dc_state->streams[j];
8521 if (!stream)
8522 continue;
8523
8524 if ((struct amdgpu_dm_connector *)stream->dm_stream_context == aconnector)
8525 break;
8526
8527 stream = NULL;
8528 }
8529
8530 if (!stream)
8531 continue;
8532
8533 pbn_div = dm_mst_get_pbn_divider(stream->link);
8534 /* pbn is calculated by compute_mst_dsc_configs_for_state*/
8535 for (j = 0; j < dc_state->stream_count; j++) {
8536 if (vars[j].aconnector == aconnector) {
8537 pbn = vars[j].pbn;
8538 break;
8539 }
8540 }
8541
8542 if (j == dc_state->stream_count || pbn_div == 0)
8543 continue;
8544
8545 slot_num = DIV_ROUND_UP(pbn, pbn_div);
8546
8547 if (stream->timing.flags.DSC != 1) {
8548 dm_conn_state->pbn = pbn;
8549 dm_conn_state->vcpi_slots = slot_num;
8550
8551 ret = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port,
8552 dm_conn_state->pbn, false);
8553 if (ret < 0)
8554 return ret;
8555
8556 continue;
8557 }
8558
8559 vcpi = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port, pbn, true);
8560 if (vcpi < 0)
8561 return vcpi;
8562
8563 dm_conn_state->pbn = pbn;
8564 dm_conn_state->vcpi_slots = vcpi;
8565 }
8566 return 0;
8567 }
8568
to_drm_connector_type(enum signal_type st,uint32_t connector_id)8569 static int to_drm_connector_type(enum signal_type st, uint32_t connector_id)
8570 {
8571 switch (st) {
8572 case SIGNAL_TYPE_HDMI_TYPE_A:
8573 return DRM_MODE_CONNECTOR_HDMIA;
8574 case SIGNAL_TYPE_EDP:
8575 return DRM_MODE_CONNECTOR_eDP;
8576 case SIGNAL_TYPE_LVDS:
8577 return DRM_MODE_CONNECTOR_LVDS;
8578 case SIGNAL_TYPE_RGB:
8579 return DRM_MODE_CONNECTOR_VGA;
8580 case SIGNAL_TYPE_DISPLAY_PORT:
8581 case SIGNAL_TYPE_DISPLAY_PORT_MST:
8582 return DRM_MODE_CONNECTOR_DisplayPort;
8583 case SIGNAL_TYPE_DVI_DUAL_LINK:
8584 case SIGNAL_TYPE_DVI_SINGLE_LINK:
8585 if (connector_id == CONNECTOR_ID_SINGLE_LINK_DVII ||
8586 connector_id == CONNECTOR_ID_DUAL_LINK_DVII)
8587 return DRM_MODE_CONNECTOR_DVII;
8588
8589 return DRM_MODE_CONNECTOR_DVID;
8590 case SIGNAL_TYPE_VIRTUAL:
8591 return DRM_MODE_CONNECTOR_VIRTUAL;
8592
8593 default:
8594 return DRM_MODE_CONNECTOR_Unknown;
8595 }
8596 }
8597
amdgpu_dm_connector_to_encoder(struct drm_connector * connector)8598 static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector)
8599 {
8600 struct drm_encoder *encoder;
8601
8602 /* There is only one encoder per connector */
8603 drm_connector_for_each_possible_encoder(connector, encoder)
8604 return encoder;
8605
8606 return NULL;
8607 }
8608
amdgpu_dm_get_native_mode(struct drm_connector * connector)8609 static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
8610 {
8611 struct drm_encoder *encoder;
8612 struct amdgpu_encoder *amdgpu_encoder;
8613
8614 encoder = amdgpu_dm_connector_to_encoder(connector);
8615
8616 if (encoder == NULL)
8617 return;
8618
8619 amdgpu_encoder = to_amdgpu_encoder(encoder);
8620
8621 amdgpu_encoder->native_mode.clock = 0;
8622
8623 if (!list_empty(&connector->probed_modes)) {
8624 struct drm_display_mode *preferred_mode = NULL;
8625
8626 list_for_each_entry(preferred_mode,
8627 &connector->probed_modes,
8628 head) {
8629 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
8630 amdgpu_encoder->native_mode = *preferred_mode;
8631
8632 break;
8633 }
8634
8635 }
8636 }
8637
8638 static struct drm_display_mode *
amdgpu_dm_create_common_mode(struct drm_encoder * encoder,const char * name,int hdisplay,int vdisplay)8639 amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
8640 const char *name,
8641 int hdisplay, int vdisplay)
8642 {
8643 struct drm_device *dev = encoder->dev;
8644 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8645 struct drm_display_mode *mode = NULL;
8646 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8647
8648 mode = drm_mode_duplicate(dev, native_mode);
8649
8650 if (mode == NULL)
8651 return NULL;
8652
8653 mode->hdisplay = hdisplay;
8654 mode->vdisplay = vdisplay;
8655 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
8656 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
8657
8658 return mode;
8659
8660 }
8661
8662 static const struct amdgpu_dm_mode_size {
8663 char name[DRM_DISPLAY_MODE_LEN];
8664 int w;
8665 int h;
8666 } common_modes[] = {
8667 { "640x480", 640, 480},
8668 { "800x600", 800, 600},
8669 { "1024x768", 1024, 768},
8670 { "1280x720", 1280, 720},
8671 { "1280x800", 1280, 800},
8672 {"1280x1024", 1280, 1024},
8673 { "1440x900", 1440, 900},
8674 {"1680x1050", 1680, 1050},
8675 {"1600x1200", 1600, 1200},
8676 {"1920x1080", 1920, 1080},
8677 {"1920x1200", 1920, 1200}
8678 };
8679
amdgpu_dm_connector_add_common_modes(struct drm_encoder * encoder,struct drm_connector * connector)8680 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
8681 struct drm_connector *connector)
8682 {
8683 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8684 struct drm_display_mode *mode = NULL;
8685 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8686 struct amdgpu_dm_connector *amdgpu_dm_connector =
8687 to_amdgpu_dm_connector(connector);
8688 int i;
8689 int n;
8690
8691 if ((connector->connector_type != DRM_MODE_CONNECTOR_eDP) &&
8692 (connector->connector_type != DRM_MODE_CONNECTOR_LVDS))
8693 return;
8694
8695 n = ARRAY_SIZE(common_modes);
8696
8697 for (i = 0; i < n; i++) {
8698 struct drm_display_mode *curmode = NULL;
8699 bool mode_existed = false;
8700
8701 if (common_modes[i].w > native_mode->hdisplay ||
8702 common_modes[i].h > native_mode->vdisplay ||
8703 (common_modes[i].w == native_mode->hdisplay &&
8704 common_modes[i].h == native_mode->vdisplay))
8705 continue;
8706
8707 list_for_each_entry(curmode, &connector->probed_modes, head) {
8708 if (common_modes[i].w == curmode->hdisplay &&
8709 common_modes[i].h == curmode->vdisplay) {
8710 mode_existed = true;
8711 break;
8712 }
8713 }
8714
8715 if (mode_existed)
8716 continue;
8717
8718 mode = amdgpu_dm_create_common_mode(encoder,
8719 common_modes[i].name, common_modes[i].w,
8720 common_modes[i].h);
8721 if (!mode)
8722 continue;
8723
8724 drm_mode_probed_add(connector, mode);
8725 amdgpu_dm_connector->num_modes++;
8726 }
8727 }
8728
amdgpu_set_panel_orientation(struct drm_connector * connector)8729 static void amdgpu_set_panel_orientation(struct drm_connector *connector)
8730 {
8731 struct drm_encoder *encoder;
8732 struct amdgpu_encoder *amdgpu_encoder;
8733 const struct drm_display_mode *native_mode;
8734
8735 if (connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
8736 connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
8737 return;
8738
8739 mutex_lock(&connector->dev->mode_config.mutex);
8740 amdgpu_dm_connector_get_modes(connector);
8741 mutex_unlock(&connector->dev->mode_config.mutex);
8742
8743 encoder = amdgpu_dm_connector_to_encoder(connector);
8744 if (!encoder)
8745 return;
8746
8747 amdgpu_encoder = to_amdgpu_encoder(encoder);
8748
8749 native_mode = &amdgpu_encoder->native_mode;
8750 if (native_mode->hdisplay == 0 || native_mode->vdisplay == 0)
8751 return;
8752
8753 drm_connector_set_panel_orientation_with_quirk(connector,
8754 DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
8755 native_mode->hdisplay,
8756 native_mode->vdisplay);
8757 }
8758
amdgpu_dm_connector_ddc_get_modes(struct drm_connector * connector,const struct drm_edid * drm_edid)8759 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
8760 const struct drm_edid *drm_edid)
8761 {
8762 struct amdgpu_dm_connector *amdgpu_dm_connector =
8763 to_amdgpu_dm_connector(connector);
8764
8765 if (drm_edid) {
8766 /* empty probed_modes */
8767 INIT_LIST_HEAD(&connector->probed_modes);
8768 amdgpu_dm_connector->num_modes =
8769 drm_edid_connector_add_modes(connector);
8770
8771 /* sorting the probed modes before calling function
8772 * amdgpu_dm_get_native_mode() since EDID can have
8773 * more than one preferred mode. The modes that are
8774 * later in the probed mode list could be of higher
8775 * and preferred resolution. For example, 3840x2160
8776 * resolution in base EDID preferred timing and 4096x2160
8777 * preferred resolution in DID extension block later.
8778 */
8779 drm_mode_sort(&connector->probed_modes);
8780 amdgpu_dm_get_native_mode(connector);
8781
8782 /* Freesync capabilities are reset by calling
8783 * drm_edid_connector_add_modes() and need to be
8784 * restored here.
8785 */
8786 amdgpu_dm_update_freesync_caps(connector, drm_edid);
8787 } else {
8788 amdgpu_dm_connector->num_modes = 0;
8789 }
8790 }
8791
is_duplicate_mode(struct amdgpu_dm_connector * aconnector,struct drm_display_mode * mode)8792 static bool is_duplicate_mode(struct amdgpu_dm_connector *aconnector,
8793 struct drm_display_mode *mode)
8794 {
8795 struct drm_display_mode *m;
8796
8797 list_for_each_entry(m, &aconnector->base.probed_modes, head) {
8798 if (drm_mode_equal(m, mode))
8799 return true;
8800 }
8801
8802 return false;
8803 }
8804
add_fs_modes(struct amdgpu_dm_connector * aconnector)8805 static uint add_fs_modes(struct amdgpu_dm_connector *aconnector)
8806 {
8807 const struct drm_display_mode *m;
8808 struct drm_display_mode *new_mode;
8809 uint i;
8810 u32 new_modes_count = 0;
8811
8812 /* Standard FPS values
8813 *
8814 * 23.976 - TV/NTSC
8815 * 24 - Cinema
8816 * 25 - TV/PAL
8817 * 29.97 - TV/NTSC
8818 * 30 - TV/NTSC
8819 * 48 - Cinema HFR
8820 * 50 - TV/PAL
8821 * 60 - Commonly used
8822 * 48,72,96,120 - Multiples of 24
8823 */
8824 static const u32 common_rates[] = {
8825 23976, 24000, 25000, 29970, 30000,
8826 48000, 50000, 60000, 72000, 96000, 120000
8827 };
8828
8829 /*
8830 * Find mode with highest refresh rate with the same resolution
8831 * as the preferred mode. Some monitors report a preferred mode
8832 * with lower resolution than the highest refresh rate supported.
8833 */
8834
8835 m = get_highest_refresh_rate_mode(aconnector, true);
8836 if (!m)
8837 return 0;
8838
8839 for (i = 0; i < ARRAY_SIZE(common_rates); i++) {
8840 u64 target_vtotal, target_vtotal_diff;
8841 u64 num, den;
8842
8843 if (drm_mode_vrefresh(m) * 1000 < common_rates[i])
8844 continue;
8845
8846 if (common_rates[i] < aconnector->min_vfreq * 1000 ||
8847 common_rates[i] > aconnector->max_vfreq * 1000)
8848 continue;
8849
8850 num = (unsigned long long)m->clock * 1000 * 1000;
8851 den = common_rates[i] * (unsigned long long)m->htotal;
8852 target_vtotal = div_u64(num, den);
8853 target_vtotal_diff = target_vtotal - m->vtotal;
8854
8855 /* Check for illegal modes */
8856 if (m->vsync_start + target_vtotal_diff < m->vdisplay ||
8857 m->vsync_end + target_vtotal_diff < m->vsync_start ||
8858 m->vtotal + target_vtotal_diff < m->vsync_end)
8859 continue;
8860
8861 new_mode = drm_mode_duplicate(aconnector->base.dev, m);
8862 if (!new_mode)
8863 goto out;
8864
8865 new_mode->vtotal += (u16)target_vtotal_diff;
8866 new_mode->vsync_start += (u16)target_vtotal_diff;
8867 new_mode->vsync_end += (u16)target_vtotal_diff;
8868 new_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
8869 new_mode->type |= DRM_MODE_TYPE_DRIVER;
8870
8871 if (!is_duplicate_mode(aconnector, new_mode)) {
8872 drm_mode_probed_add(&aconnector->base, new_mode);
8873 new_modes_count += 1;
8874 } else
8875 drm_mode_destroy(aconnector->base.dev, new_mode);
8876 }
8877 out:
8878 return new_modes_count;
8879 }
8880
amdgpu_dm_connector_add_freesync_modes(struct drm_connector * connector,const struct drm_edid * drm_edid)8881 static void amdgpu_dm_connector_add_freesync_modes(struct drm_connector *connector,
8882 const struct drm_edid *drm_edid)
8883 {
8884 struct amdgpu_dm_connector *amdgpu_dm_connector =
8885 to_amdgpu_dm_connector(connector);
8886
8887 if (!(amdgpu_freesync_vid_mode && drm_edid))
8888 return;
8889
8890 if (!amdgpu_dm_connector->dc_sink || !amdgpu_dm_connector->dc_link)
8891 return;
8892
8893 if (!dc_supports_vrr(amdgpu_dm_connector->dc_sink->ctx->dce_version))
8894 return;
8895
8896 if (dc_connector_supports_analog(amdgpu_dm_connector->dc_link->link_id.id) &&
8897 amdgpu_dm_connector->dc_sink->edid_caps.analog)
8898 return;
8899
8900 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
8901 amdgpu_dm_connector->num_modes +=
8902 add_fs_modes(amdgpu_dm_connector);
8903 }
8904
amdgpu_dm_connector_get_modes(struct drm_connector * connector)8905 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
8906 {
8907 struct amdgpu_dm_connector *amdgpu_dm_connector =
8908 to_amdgpu_dm_connector(connector);
8909 struct dc_link *dc_link = amdgpu_dm_connector->dc_link;
8910 struct drm_encoder *encoder;
8911 const struct drm_edid *drm_edid = amdgpu_dm_connector->drm_edid;
8912 struct dc_link_settings *verified_link_cap = &dc_link->verified_link_cap;
8913 const struct dc *dc = dc_link->dc;
8914
8915 encoder = amdgpu_dm_connector_to_encoder(connector);
8916
8917 if (!drm_edid) {
8918 amdgpu_dm_connector->num_modes =
8919 drm_add_modes_noedid(connector, 640, 480);
8920 if (dc->link_srv->dp_get_encoding_format(verified_link_cap) == DP_128b_132b_ENCODING)
8921 amdgpu_dm_connector->num_modes +=
8922 drm_add_modes_noedid(connector, 1920, 1080);
8923
8924 if (amdgpu_dm_connector->dc_sink &&
8925 amdgpu_dm_connector->dc_sink->edid_caps.analog &&
8926 dc_connector_supports_analog(dc_link->link_id.id)) {
8927 /* Analog monitor connected by DAC load detection.
8928 * Add common modes. It will be up to the user to select one that works.
8929 */
8930 for (int i = 0; i < ARRAY_SIZE(common_modes); i++)
8931 amdgpu_dm_connector->num_modes += drm_add_modes_noedid(
8932 connector, common_modes[i].w, common_modes[i].h);
8933 }
8934 } else {
8935 amdgpu_dm_connector_ddc_get_modes(connector, drm_edid);
8936 if (encoder)
8937 amdgpu_dm_connector_add_common_modes(encoder, connector);
8938 amdgpu_dm_connector_add_freesync_modes(connector, drm_edid);
8939 }
8940 amdgpu_dm_fbc_init(connector);
8941
8942 return amdgpu_dm_connector->num_modes;
8943 }
8944
8945 static const u32 supported_colorspaces =
8946 BIT(DRM_MODE_COLORIMETRY_BT709_YCC) |
8947 BIT(DRM_MODE_COLORIMETRY_OPRGB) |
8948 BIT(DRM_MODE_COLORIMETRY_BT2020_RGB) |
8949 BIT(DRM_MODE_COLORIMETRY_BT2020_YCC);
8950
amdgpu_dm_connector_init_helper(struct amdgpu_display_manager * dm,struct amdgpu_dm_connector * aconnector,int connector_type,struct dc_link * link,int link_index)8951 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
8952 struct amdgpu_dm_connector *aconnector,
8953 int connector_type,
8954 struct dc_link *link,
8955 int link_index)
8956 {
8957 struct amdgpu_device *adev = drm_to_adev(dm->ddev);
8958
8959 /*
8960 * Some of the properties below require access to state, like bpc.
8961 * Allocate some default initial connector state with our reset helper.
8962 */
8963 if (aconnector->base.funcs->reset)
8964 aconnector->base.funcs->reset(&aconnector->base);
8965
8966 aconnector->connector_id = link_index;
8967 aconnector->bl_idx = -1;
8968 aconnector->dc_link = link;
8969 aconnector->base.interlace_allowed = false;
8970 aconnector->base.doublescan_allowed = false;
8971 aconnector->base.stereo_allowed = false;
8972 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
8973 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
8974 aconnector->audio_inst = -1;
8975 aconnector->pack_sdp_v1_3 = false;
8976 aconnector->as_type = ADAPTIVE_SYNC_TYPE_NONE;
8977 memset(&aconnector->vsdb_info, 0, sizeof(aconnector->vsdb_info));
8978 mutex_init(&aconnector->hpd_lock);
8979 mutex_init(&aconnector->handle_mst_msg_ready);
8980
8981 /*
8982 * If HDMI HPD debounce delay is set, use the minimum between selected
8983 * value and AMDGPU_DM_MAX_HDMI_HPD_DEBOUNCE_MS
8984 */
8985 if (amdgpu_hdmi_hpd_debounce_delay_ms) {
8986 aconnector->hdmi_hpd_debounce_delay_ms = min(amdgpu_hdmi_hpd_debounce_delay_ms,
8987 AMDGPU_DM_MAX_HDMI_HPD_DEBOUNCE_MS);
8988 INIT_DELAYED_WORK(&aconnector->hdmi_hpd_debounce_work, hdmi_hpd_debounce_work);
8989 aconnector->hdmi_prev_sink = NULL;
8990 } else {
8991 aconnector->hdmi_hpd_debounce_delay_ms = 0;
8992 }
8993
8994 /*
8995 * configure support HPD hot plug connector_>polled default value is 0
8996 * which means HPD hot plug not supported
8997 */
8998 switch (connector_type) {
8999 case DRM_MODE_CONNECTOR_HDMIA:
9000 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
9001 aconnector->base.ycbcr_420_allowed =
9002 link->link_enc->features.hdmi_ycbcr420_supported ? true : false;
9003 break;
9004 case DRM_MODE_CONNECTOR_DisplayPort:
9005 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
9006 link->link_enc = link_enc_cfg_get_link_enc(link);
9007 ASSERT(link->link_enc);
9008 if (link->link_enc)
9009 aconnector->base.ycbcr_420_allowed =
9010 link->link_enc->features.dp_ycbcr420_supported ? true : false;
9011 break;
9012 case DRM_MODE_CONNECTOR_DVID:
9013 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
9014 break;
9015 case DRM_MODE_CONNECTOR_DVII:
9016 case DRM_MODE_CONNECTOR_VGA:
9017 aconnector->base.polled =
9018 DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
9019 break;
9020 default:
9021 break;
9022 }
9023
9024 drm_object_attach_property(&aconnector->base.base,
9025 dm->ddev->mode_config.scaling_mode_property,
9026 DRM_MODE_SCALE_NONE);
9027
9028 if (connector_type == DRM_MODE_CONNECTOR_HDMIA
9029 || (connector_type == DRM_MODE_CONNECTOR_DisplayPort && !aconnector->mst_root))
9030 drm_connector_attach_broadcast_rgb_property(&aconnector->base);
9031
9032 drm_object_attach_property(&aconnector->base.base,
9033 adev->mode_info.underscan_property,
9034 UNDERSCAN_OFF);
9035 drm_object_attach_property(&aconnector->base.base,
9036 adev->mode_info.underscan_hborder_property,
9037 0);
9038 drm_object_attach_property(&aconnector->base.base,
9039 adev->mode_info.underscan_vborder_property,
9040 0);
9041
9042 if (!aconnector->mst_root)
9043 drm_connector_attach_max_bpc_property(&aconnector->base, 8, 16);
9044
9045 aconnector->base.state->max_bpc = 16;
9046 aconnector->base.state->max_requested_bpc = aconnector->base.state->max_bpc;
9047
9048 if (connector_type == DRM_MODE_CONNECTOR_HDMIA) {
9049 /* Content Type is currently only implemented for HDMI. */
9050 drm_connector_attach_content_type_property(&aconnector->base);
9051 }
9052
9053 if (connector_type == DRM_MODE_CONNECTOR_HDMIA) {
9054 if (!drm_mode_create_hdmi_colorspace_property(&aconnector->base, supported_colorspaces))
9055 drm_connector_attach_colorspace_property(&aconnector->base);
9056 } else if ((connector_type == DRM_MODE_CONNECTOR_DisplayPort && !aconnector->mst_root) ||
9057 connector_type == DRM_MODE_CONNECTOR_eDP) {
9058 if (!drm_mode_create_dp_colorspace_property(&aconnector->base, supported_colorspaces))
9059 drm_connector_attach_colorspace_property(&aconnector->base);
9060 }
9061
9062 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
9063 connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
9064 connector_type == DRM_MODE_CONNECTOR_eDP) {
9065 drm_connector_attach_hdr_output_metadata_property(&aconnector->base);
9066
9067 if (!aconnector->mst_root)
9068 drm_connector_attach_vrr_capable_property(&aconnector->base);
9069
9070 if (adev->dm.hdcp_workqueue)
9071 drm_connector_attach_content_protection_property(&aconnector->base, true);
9072 }
9073
9074 if (connector_type == DRM_MODE_CONNECTOR_eDP) {
9075 struct drm_privacy_screen *privacy_screen;
9076
9077 privacy_screen = drm_privacy_screen_get(adev_to_drm(adev)->dev, NULL);
9078 if (!IS_ERR(privacy_screen)) {
9079 drm_connector_attach_privacy_screen_provider(&aconnector->base,
9080 privacy_screen);
9081 } else if (PTR_ERR(privacy_screen) != -ENODEV) {
9082 drm_warn(adev_to_drm(adev), "Error getting privacy-screen\n");
9083 }
9084 }
9085 }
9086
amdgpu_dm_i2c_xfer(struct i2c_adapter * i2c_adap,struct i2c_msg * msgs,int num)9087 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
9088 struct i2c_msg *msgs, int num)
9089 {
9090 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
9091 struct ddc_service *ddc_service = i2c->ddc_service;
9092 struct i2c_command cmd;
9093 int i;
9094 int result = -EIO;
9095
9096 if (!ddc_service->ddc_pin)
9097 return result;
9098
9099 cmd.payloads = kzalloc_objs(struct i2c_payload, num);
9100
9101 if (!cmd.payloads)
9102 return result;
9103
9104 cmd.number_of_payloads = num;
9105 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
9106 cmd.speed = 100;
9107
9108 for (i = 0; i < num; i++) {
9109 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
9110 cmd.payloads[i].address = msgs[i].addr;
9111 cmd.payloads[i].length = msgs[i].len;
9112 cmd.payloads[i].data = msgs[i].buf;
9113 }
9114
9115 if (i2c->oem) {
9116 if (dc_submit_i2c_oem(
9117 ddc_service->ctx->dc,
9118 &cmd))
9119 result = num;
9120 } else {
9121 if (dc_submit_i2c(
9122 ddc_service->ctx->dc,
9123 ddc_service->link->link_index,
9124 &cmd))
9125 result = num;
9126 }
9127
9128 kfree(cmd.payloads);
9129 return result;
9130 }
9131
amdgpu_dm_i2c_func(struct i2c_adapter * adap)9132 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
9133 {
9134 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
9135 }
9136
9137 static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
9138 .master_xfer = amdgpu_dm_i2c_xfer,
9139 .functionality = amdgpu_dm_i2c_func,
9140 };
9141
9142 static struct amdgpu_i2c_adapter *
create_i2c(struct ddc_service * ddc_service,bool oem)9143 create_i2c(struct ddc_service *ddc_service, bool oem)
9144 {
9145 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
9146 struct amdgpu_i2c_adapter *i2c;
9147
9148 i2c = kzalloc_obj(struct amdgpu_i2c_adapter);
9149 if (!i2c)
9150 return NULL;
9151 i2c->base.owner = THIS_MODULE;
9152 i2c->base.dev.parent = &adev->pdev->dev;
9153 i2c->base.algo = &amdgpu_dm_i2c_algo;
9154 if (oem)
9155 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c OEM bus");
9156 else
9157 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d",
9158 ddc_service->link->link_index);
9159 i2c_set_adapdata(&i2c->base, i2c);
9160 i2c->ddc_service = ddc_service;
9161 i2c->oem = oem;
9162
9163 return i2c;
9164 }
9165
amdgpu_dm_initialize_hdmi_connector(struct amdgpu_dm_connector * aconnector)9166 int amdgpu_dm_initialize_hdmi_connector(struct amdgpu_dm_connector *aconnector)
9167 {
9168 struct cec_connector_info conn_info;
9169 struct drm_device *ddev = aconnector->base.dev;
9170 struct device *hdmi_dev = ddev->dev;
9171
9172 if (amdgpu_dc_debug_mask & DC_DISABLE_HDMI_CEC) {
9173 drm_info(ddev, "HDMI-CEC feature masked\n");
9174 return -EINVAL;
9175 }
9176
9177 cec_fill_conn_info_from_drm(&conn_info, &aconnector->base);
9178 aconnector->notifier =
9179 cec_notifier_conn_register(hdmi_dev, NULL, &conn_info);
9180 if (!aconnector->notifier) {
9181 drm_err(ddev, "Failed to create cec notifier\n");
9182 return -ENOMEM;
9183 }
9184
9185 return 0;
9186 }
9187
9188 /*
9189 * Note: this function assumes that dc_link_detect() was called for the
9190 * dc_link which will be represented by this aconnector.
9191 */
amdgpu_dm_connector_init(struct amdgpu_display_manager * dm,struct amdgpu_dm_connector * aconnector,u32 link_index,struct amdgpu_encoder * aencoder)9192 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
9193 struct amdgpu_dm_connector *aconnector,
9194 u32 link_index,
9195 struct amdgpu_encoder *aencoder)
9196 {
9197 int res = 0;
9198 int connector_type;
9199 struct dc *dc = dm->dc;
9200 struct dc_link *link = dc_get_link_at_index(dc, link_index);
9201 struct amdgpu_i2c_adapter *i2c;
9202
9203 /* Not needed for writeback connector */
9204 link->priv = aconnector;
9205
9206
9207 i2c = create_i2c(link->ddc, false);
9208 if (!i2c) {
9209 drm_err(adev_to_drm(dm->adev), "Failed to create i2c adapter data\n");
9210 return -ENOMEM;
9211 }
9212
9213 aconnector->i2c = i2c;
9214 res = devm_i2c_add_adapter(dm->adev->dev, &i2c->base);
9215
9216 if (res) {
9217 drm_err(adev_to_drm(dm->adev), "Failed to register hw i2c %d\n", link->link_index);
9218 goto out_free;
9219 }
9220
9221 connector_type = to_drm_connector_type(link->connector_signal, link->link_id.id);
9222
9223 res = drm_connector_init_with_ddc(
9224 dm->ddev,
9225 &aconnector->base,
9226 &amdgpu_dm_connector_funcs,
9227 connector_type,
9228 &i2c->base);
9229
9230 if (res) {
9231 drm_err(adev_to_drm(dm->adev), "connector_init failed\n");
9232 aconnector->connector_id = -1;
9233 goto out_free;
9234 }
9235
9236 drm_connector_helper_add(
9237 &aconnector->base,
9238 &amdgpu_dm_connector_helper_funcs);
9239
9240 amdgpu_dm_connector_init_helper(
9241 dm,
9242 aconnector,
9243 connector_type,
9244 link,
9245 link_index);
9246
9247 drm_connector_attach_encoder(
9248 &aconnector->base, &aencoder->base);
9249
9250 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
9251 connector_type == DRM_MODE_CONNECTOR_HDMIB)
9252 amdgpu_dm_initialize_hdmi_connector(aconnector);
9253
9254 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
9255 || connector_type == DRM_MODE_CONNECTOR_eDP)
9256 amdgpu_dm_initialize_dp_connector(dm, aconnector, link->link_index);
9257
9258 out_free:
9259 if (res) {
9260 kfree(i2c);
9261 aconnector->i2c = NULL;
9262 }
9263 return res;
9264 }
9265
amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device * adev)9266 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
9267 {
9268 switch (adev->mode_info.num_crtc) {
9269 case 1:
9270 return 0x1;
9271 case 2:
9272 return 0x3;
9273 case 3:
9274 return 0x7;
9275 case 4:
9276 return 0xf;
9277 case 5:
9278 return 0x1f;
9279 case 6:
9280 default:
9281 return 0x3f;
9282 }
9283 }
9284
amdgpu_dm_encoder_init(struct drm_device * dev,struct amdgpu_encoder * aencoder,uint32_t link_index)9285 static int amdgpu_dm_encoder_init(struct drm_device *dev,
9286 struct amdgpu_encoder *aencoder,
9287 uint32_t link_index)
9288 {
9289 struct amdgpu_device *adev = drm_to_adev(dev);
9290
9291 int res = drm_encoder_init(dev,
9292 &aencoder->base,
9293 &amdgpu_dm_encoder_funcs,
9294 DRM_MODE_ENCODER_TMDS,
9295 NULL);
9296
9297 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
9298
9299 if (!res)
9300 aencoder->encoder_id = link_index;
9301 else
9302 aencoder->encoder_id = -1;
9303
9304 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
9305
9306 return res;
9307 }
9308
manage_dm_interrupts(struct amdgpu_device * adev,struct amdgpu_crtc * acrtc,struct dm_crtc_state * acrtc_state)9309 static void manage_dm_interrupts(struct amdgpu_device *adev,
9310 struct amdgpu_crtc *acrtc,
9311 struct dm_crtc_state *acrtc_state)
9312 { /*
9313 * We cannot be sure that the frontend index maps to the same
9314 * backend index - some even map to more than one.
9315 * So we have to go through the CRTC to find the right IRQ.
9316 */
9317 int irq_type = amdgpu_display_crtc_idx_to_irq_type(
9318 adev,
9319 acrtc->crtc_id);
9320 struct drm_device *dev = adev_to_drm(adev);
9321
9322 struct drm_vblank_crtc_config config = {0};
9323 struct dc_crtc_timing *timing;
9324 int offdelay;
9325
9326 if (acrtc_state) {
9327 timing = &acrtc_state->stream->timing;
9328
9329 /*
9330 * Depending on when the HW latching event of double-buffered
9331 * registers happen relative to the PSR SDP deadline, and how
9332 * bad the Panel clock has drifted since the last ALPM off
9333 * event, there can be up to 3 frames of delay between sending
9334 * the PSR exit cmd to DMUB fw, and when the panel starts
9335 * displaying live frames.
9336 *
9337 * We can set:
9338 *
9339 * 20/100 * offdelay_ms = 3_frames_ms
9340 * => offdelay_ms = 5 * 3_frames_ms
9341 *
9342 * This ensures that `3_frames_ms` will only be experienced as a
9343 * 20% delay on top how long the display has been static, and
9344 * thus make the delay less perceivable.
9345 */
9346 if (acrtc_state->stream->link->psr_settings.psr_version <
9347 DC_PSR_VERSION_UNSUPPORTED) {
9348 offdelay = DIV64_U64_ROUND_UP((u64)5 * 3 * 10 *
9349 timing->v_total *
9350 timing->h_total,
9351 timing->pix_clk_100hz);
9352 config.offdelay_ms = offdelay ?: 30;
9353 } else if (amdgpu_ip_version(adev, DCE_HWIP, 0) <
9354 IP_VERSION(3, 5, 0) ||
9355 !(adev->flags & AMD_IS_APU)) {
9356 /*
9357 * Older HW and DGPU have issues with instant off;
9358 * use a 2 frame offdelay.
9359 */
9360 offdelay = DIV64_U64_ROUND_UP((u64)20 *
9361 timing->v_total *
9362 timing->h_total,
9363 timing->pix_clk_100hz);
9364
9365 config.offdelay_ms = offdelay ?: 30;
9366 } else {
9367 /* offdelay_ms = 0 will never disable vblank */
9368 config.offdelay_ms = 1;
9369 config.disable_immediate = true;
9370 }
9371
9372 drm_crtc_vblank_on_config(&acrtc->base,
9373 &config);
9374 /* Allow RX6xxx, RX7700, RX7800 GPUs to call amdgpu_irq_get.*/
9375 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
9376 case IP_VERSION(3, 0, 0):
9377 case IP_VERSION(3, 0, 2):
9378 case IP_VERSION(3, 0, 3):
9379 case IP_VERSION(3, 2, 0):
9380 if (amdgpu_irq_get(adev, &adev->pageflip_irq, irq_type))
9381 drm_err(dev, "DM_IRQ: Cannot get pageflip irq!\n");
9382 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
9383 if (amdgpu_irq_get(adev, &adev->vline0_irq, irq_type))
9384 drm_err(dev, "DM_IRQ: Cannot get vline0 irq!\n");
9385 #endif
9386 }
9387
9388 } else {
9389 /* Allow RX6xxx, RX7700, RX7800 GPUs to call amdgpu_irq_put.*/
9390 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
9391 case IP_VERSION(3, 0, 0):
9392 case IP_VERSION(3, 0, 2):
9393 case IP_VERSION(3, 0, 3):
9394 case IP_VERSION(3, 2, 0):
9395 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
9396 if (amdgpu_irq_put(adev, &adev->vline0_irq, irq_type))
9397 drm_err(dev, "DM_IRQ: Cannot put vline0 irq!\n");
9398 #endif
9399 if (amdgpu_irq_put(adev, &adev->pageflip_irq, irq_type))
9400 drm_err(dev, "DM_IRQ: Cannot put pageflip irq!\n");
9401 }
9402
9403 drm_crtc_vblank_off(&acrtc->base);
9404 }
9405 }
9406
dm_update_pflip_irq_state(struct amdgpu_device * adev,struct amdgpu_crtc * acrtc)9407 static void dm_update_pflip_irq_state(struct amdgpu_device *adev,
9408 struct amdgpu_crtc *acrtc)
9409 {
9410 int irq_type =
9411 amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
9412
9413 /**
9414 * This reads the current state for the IRQ and force reapplies
9415 * the setting to hardware.
9416 */
9417 amdgpu_irq_update(adev, &adev->pageflip_irq, irq_type);
9418 }
9419
9420 static bool
is_scaling_state_different(const struct dm_connector_state * dm_state,const struct dm_connector_state * old_dm_state)9421 is_scaling_state_different(const struct dm_connector_state *dm_state,
9422 const struct dm_connector_state *old_dm_state)
9423 {
9424 if (dm_state->scaling != old_dm_state->scaling)
9425 return true;
9426 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
9427 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
9428 return true;
9429 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
9430 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
9431 return true;
9432 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
9433 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
9434 return true;
9435 return false;
9436 }
9437
is_content_protection_different(struct drm_crtc_state * new_crtc_state,struct drm_crtc_state * old_crtc_state,struct drm_connector_state * new_conn_state,struct drm_connector_state * old_conn_state,const struct drm_connector * connector,struct hdcp_workqueue * hdcp_w)9438 static bool is_content_protection_different(struct drm_crtc_state *new_crtc_state,
9439 struct drm_crtc_state *old_crtc_state,
9440 struct drm_connector_state *new_conn_state,
9441 struct drm_connector_state *old_conn_state,
9442 const struct drm_connector *connector,
9443 struct hdcp_workqueue *hdcp_w)
9444 {
9445 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
9446 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
9447
9448 pr_debug("[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n",
9449 connector->index, connector->status, connector->dpms);
9450 pr_debug("[HDCP_DM] state protection old: %x new: %x\n",
9451 old_conn_state->content_protection, new_conn_state->content_protection);
9452
9453 if (old_crtc_state)
9454 pr_debug("[HDCP_DM] old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
9455 old_crtc_state->enable,
9456 old_crtc_state->active,
9457 old_crtc_state->mode_changed,
9458 old_crtc_state->active_changed,
9459 old_crtc_state->connectors_changed);
9460
9461 if (new_crtc_state)
9462 pr_debug("[HDCP_DM] NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
9463 new_crtc_state->enable,
9464 new_crtc_state->active,
9465 new_crtc_state->mode_changed,
9466 new_crtc_state->active_changed,
9467 new_crtc_state->connectors_changed);
9468
9469 /* hdcp content type change */
9470 if (old_conn_state->hdcp_content_type != new_conn_state->hdcp_content_type &&
9471 new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
9472 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
9473 pr_debug("[HDCP_DM] Type0/1 change %s :true\n", __func__);
9474 return true;
9475 }
9476
9477 /* CP is being re enabled, ignore this */
9478 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED &&
9479 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
9480 if (new_crtc_state && new_crtc_state->mode_changed) {
9481 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
9482 pr_debug("[HDCP_DM] ENABLED->DESIRED & mode_changed %s :true\n", __func__);
9483 return true;
9484 }
9485 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED;
9486 pr_debug("[HDCP_DM] ENABLED -> DESIRED %s :false\n", __func__);
9487 return false;
9488 }
9489
9490 /* S3 resume case, since old state will always be 0 (UNDESIRED) and the restored state will be ENABLED
9491 *
9492 * Handles: UNDESIRED -> ENABLED
9493 */
9494 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED &&
9495 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
9496 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
9497
9498 /* Stream removed and re-enabled
9499 *
9500 * Can sometimes overlap with the HPD case,
9501 * thus set update_hdcp to false to avoid
9502 * setting HDCP multiple times.
9503 *
9504 * Handles: DESIRED -> DESIRED (Special case)
9505 */
9506 if (!(old_conn_state->crtc && old_conn_state->crtc->enabled) &&
9507 new_conn_state->crtc && new_conn_state->crtc->enabled &&
9508 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
9509 dm_con_state->update_hdcp = false;
9510 pr_debug("[HDCP_DM] DESIRED->DESIRED (Stream removed and re-enabled) %s :true\n",
9511 __func__);
9512 return true;
9513 }
9514
9515 /* Hot-plug, headless s3, dpms
9516 *
9517 * Only start HDCP if the display is connected/enabled.
9518 * update_hdcp flag will be set to false until the next
9519 * HPD comes in.
9520 *
9521 * Handles: DESIRED -> DESIRED (Special case)
9522 */
9523 if (dm_con_state->update_hdcp &&
9524 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED &&
9525 connector->dpms == DRM_MODE_DPMS_ON && aconnector->dc_sink != NULL) {
9526 dm_con_state->update_hdcp = false;
9527 pr_debug("[HDCP_DM] DESIRED->DESIRED (Hot-plug, headless s3, dpms) %s :true\n",
9528 __func__);
9529 return true;
9530 }
9531
9532 if (old_conn_state->content_protection == new_conn_state->content_protection) {
9533 if (new_conn_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED) {
9534 if (new_crtc_state && new_crtc_state->mode_changed) {
9535 pr_debug("[HDCP_DM] DESIRED->DESIRED or ENABLE->ENABLE mode_change %s :true\n",
9536 __func__);
9537 return true;
9538 }
9539 pr_debug("[HDCP_DM] DESIRED->DESIRED & ENABLE->ENABLE %s :false\n",
9540 __func__);
9541 return false;
9542 }
9543
9544 pr_debug("[HDCP_DM] UNDESIRED->UNDESIRED %s :false\n", __func__);
9545 return false;
9546 }
9547
9548 if (new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_ENABLED) {
9549 pr_debug("[HDCP_DM] UNDESIRED->DESIRED or DESIRED->UNDESIRED or ENABLED->UNDESIRED %s :true\n",
9550 __func__);
9551 return true;
9552 }
9553
9554 pr_debug("[HDCP_DM] DESIRED->ENABLED %s :false\n", __func__);
9555 return false;
9556 }
9557
remove_stream(struct amdgpu_device * adev,struct amdgpu_crtc * acrtc,struct dc_stream_state * stream)9558 static void remove_stream(struct amdgpu_device *adev,
9559 struct amdgpu_crtc *acrtc,
9560 struct dc_stream_state *stream)
9561 {
9562 /* this is the update mode case */
9563
9564 acrtc->otg_inst = -1;
9565 acrtc->enabled = false;
9566 }
9567
prepare_flip_isr(struct amdgpu_crtc * acrtc)9568 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
9569 {
9570
9571 assert_spin_locked(&acrtc->base.dev->event_lock);
9572 WARN_ON(acrtc->event);
9573
9574 acrtc->event = acrtc->base.state->event;
9575
9576 /* Set the flip status */
9577 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
9578
9579 /* Mark this event as consumed */
9580 acrtc->base.state->event = NULL;
9581
9582 drm_dbg_state(acrtc->base.dev,
9583 "crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
9584 acrtc->crtc_id);
9585 }
9586
update_freesync_state_on_stream(struct amdgpu_display_manager * dm,struct dm_crtc_state * new_crtc_state,struct dc_stream_state * new_stream,struct dc_plane_state * surface,u32 flip_timestamp_in_us)9587 static void update_freesync_state_on_stream(
9588 struct amdgpu_display_manager *dm,
9589 struct dm_crtc_state *new_crtc_state,
9590 struct dc_stream_state *new_stream,
9591 struct dc_plane_state *surface,
9592 u32 flip_timestamp_in_us)
9593 {
9594 struct mod_vrr_params vrr_params;
9595 struct dc_info_packet vrr_infopacket = {0};
9596 struct amdgpu_device *adev = dm->adev;
9597 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
9598 unsigned long flags;
9599 bool pack_sdp_v1_3 = false;
9600 struct amdgpu_dm_connector *aconn;
9601 enum vrr_packet_type packet_type = PACKET_TYPE_VRR;
9602
9603 if (!new_stream)
9604 return;
9605
9606 /*
9607 * TODO: Determine why min/max totals and vrefresh can be 0 here.
9608 * For now it's sufficient to just guard against these conditions.
9609 */
9610
9611 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
9612 return;
9613
9614 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9615 vrr_params = acrtc->dm_irq_params.vrr_params;
9616
9617 if (surface) {
9618 mod_freesync_handle_preflip(
9619 dm->freesync_module,
9620 surface,
9621 new_stream,
9622 flip_timestamp_in_us,
9623 &vrr_params);
9624
9625 if (adev->family < AMDGPU_FAMILY_AI &&
9626 amdgpu_dm_crtc_vrr_active(new_crtc_state)) {
9627 mod_freesync_handle_v_update(dm->freesync_module,
9628 new_stream, &vrr_params);
9629
9630 /* Need to call this before the frame ends. */
9631 dc_stream_adjust_vmin_vmax(dm->dc,
9632 new_crtc_state->stream,
9633 &vrr_params.adjust);
9634 }
9635 }
9636
9637 aconn = (struct amdgpu_dm_connector *)new_stream->dm_stream_context;
9638
9639 if (aconn && (aconn->as_type == FREESYNC_TYPE_PCON_IN_WHITELIST || aconn->vsdb_info.replay_mode)) {
9640 pack_sdp_v1_3 = aconn->pack_sdp_v1_3;
9641
9642 if (aconn->vsdb_info.amd_vsdb_version == 1)
9643 packet_type = PACKET_TYPE_FS_V1;
9644 else if (aconn->vsdb_info.amd_vsdb_version == 2)
9645 packet_type = PACKET_TYPE_FS_V2;
9646 else if (aconn->vsdb_info.amd_vsdb_version == 3)
9647 packet_type = PACKET_TYPE_FS_V3;
9648
9649 mod_build_adaptive_sync_infopacket(new_stream, aconn->as_type, NULL,
9650 &new_stream->adaptive_sync_infopacket);
9651 }
9652
9653 mod_freesync_build_vrr_infopacket(
9654 dm->freesync_module,
9655 new_stream,
9656 &vrr_params,
9657 packet_type,
9658 TRANSFER_FUNC_UNKNOWN,
9659 &vrr_infopacket,
9660 pack_sdp_v1_3);
9661
9662 new_crtc_state->freesync_vrr_info_changed |=
9663 (memcmp(&new_crtc_state->vrr_infopacket,
9664 &vrr_infopacket,
9665 sizeof(vrr_infopacket)) != 0);
9666
9667 acrtc->dm_irq_params.vrr_params = vrr_params;
9668 new_crtc_state->vrr_infopacket = vrr_infopacket;
9669
9670 new_stream->vrr_infopacket = vrr_infopacket;
9671 new_stream->allow_freesync = mod_freesync_get_freesync_enabled(&vrr_params);
9672
9673 if (new_crtc_state->freesync_vrr_info_changed)
9674 drm_dbg_kms(adev_to_drm(adev), "VRR packet update: crtc=%u enabled=%d state=%d",
9675 new_crtc_state->base.crtc->base.id,
9676 (int)new_crtc_state->base.vrr_enabled,
9677 (int)vrr_params.state);
9678
9679 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9680 }
9681
update_stream_irq_parameters(struct amdgpu_display_manager * dm,struct dm_crtc_state * new_crtc_state)9682 static void update_stream_irq_parameters(
9683 struct amdgpu_display_manager *dm,
9684 struct dm_crtc_state *new_crtc_state)
9685 {
9686 struct dc_stream_state *new_stream = new_crtc_state->stream;
9687 struct mod_vrr_params vrr_params;
9688 struct mod_freesync_config config = new_crtc_state->freesync_config;
9689 struct amdgpu_device *adev = dm->adev;
9690 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
9691 unsigned long flags;
9692
9693 if (!new_stream)
9694 return;
9695
9696 /*
9697 * TODO: Determine why min/max totals and vrefresh can be 0 here.
9698 * For now it's sufficient to just guard against these conditions.
9699 */
9700 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
9701 return;
9702
9703 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9704 vrr_params = acrtc->dm_irq_params.vrr_params;
9705
9706 if (new_crtc_state->vrr_supported &&
9707 config.min_refresh_in_uhz &&
9708 config.max_refresh_in_uhz) {
9709 /*
9710 * if freesync compatible mode was set, config.state will be set
9711 * in atomic check
9712 */
9713 if (config.state == VRR_STATE_ACTIVE_FIXED && config.fixed_refresh_in_uhz &&
9714 (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) ||
9715 new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)) {
9716 vrr_params.max_refresh_in_uhz = config.max_refresh_in_uhz;
9717 vrr_params.min_refresh_in_uhz = config.min_refresh_in_uhz;
9718 vrr_params.fixed_refresh_in_uhz = config.fixed_refresh_in_uhz;
9719 vrr_params.state = VRR_STATE_ACTIVE_FIXED;
9720 } else {
9721 config.state = new_crtc_state->base.vrr_enabled ?
9722 VRR_STATE_ACTIVE_VARIABLE :
9723 VRR_STATE_INACTIVE;
9724 }
9725 } else {
9726 config.state = VRR_STATE_UNSUPPORTED;
9727 }
9728
9729 mod_freesync_build_vrr_params(dm->freesync_module,
9730 new_stream,
9731 &config, &vrr_params);
9732
9733 new_crtc_state->freesync_config = config;
9734 /* Copy state for access from DM IRQ handler */
9735 acrtc->dm_irq_params.freesync_config = config;
9736 acrtc->dm_irq_params.active_planes = new_crtc_state->active_planes;
9737 acrtc->dm_irq_params.vrr_params = vrr_params;
9738 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9739 }
9740
amdgpu_dm_handle_vrr_transition(struct dm_crtc_state * old_state,struct dm_crtc_state * new_state)9741 static void amdgpu_dm_handle_vrr_transition(struct dm_crtc_state *old_state,
9742 struct dm_crtc_state *new_state)
9743 {
9744 bool old_vrr_active = amdgpu_dm_crtc_vrr_active(old_state);
9745 bool new_vrr_active = amdgpu_dm_crtc_vrr_active(new_state);
9746
9747 if (!old_vrr_active && new_vrr_active) {
9748 /* Transition VRR inactive -> active:
9749 * While VRR is active, we must not disable vblank irq, as a
9750 * reenable after disable would compute bogus vblank/pflip
9751 * timestamps if it likely happened inside display front-porch.
9752 *
9753 * We also need vupdate irq for the actual core vblank handling
9754 * at end of vblank.
9755 */
9756 WARN_ON(amdgpu_dm_crtc_set_vupdate_irq(new_state->base.crtc, true) != 0);
9757 WARN_ON(drm_crtc_vblank_get(new_state->base.crtc) != 0);
9758 drm_dbg_driver(new_state->base.crtc->dev, "%s: crtc=%u VRR off->on: Get vblank ref\n",
9759 __func__, new_state->base.crtc->base.id);
9760 } else if (old_vrr_active && !new_vrr_active) {
9761 /* Transition VRR active -> inactive:
9762 * Allow vblank irq disable again for fixed refresh rate.
9763 */
9764 WARN_ON(amdgpu_dm_crtc_set_vupdate_irq(new_state->base.crtc, false) != 0);
9765 drm_crtc_vblank_put(new_state->base.crtc);
9766 drm_dbg_driver(new_state->base.crtc->dev, "%s: crtc=%u VRR on->off: Drop vblank ref\n",
9767 __func__, new_state->base.crtc->base.id);
9768 }
9769 }
9770
amdgpu_dm_commit_cursors(struct drm_atomic_state * state)9771 static void amdgpu_dm_commit_cursors(struct drm_atomic_state *state)
9772 {
9773 struct drm_plane *plane;
9774 struct drm_plane_state *old_plane_state;
9775 int i;
9776
9777 /*
9778 * TODO: Make this per-stream so we don't issue redundant updates for
9779 * commits with multiple streams.
9780 */
9781 for_each_old_plane_in_state(state, plane, old_plane_state, i)
9782 if (plane->type == DRM_PLANE_TYPE_CURSOR)
9783 amdgpu_dm_plane_handle_cursor_update(plane, old_plane_state);
9784 }
9785
get_mem_type(struct drm_framebuffer * fb)9786 static inline uint32_t get_mem_type(struct drm_framebuffer *fb)
9787 {
9788 struct amdgpu_bo *abo = gem_to_amdgpu_bo(fb->obj[0]);
9789
9790 return abo->tbo.resource ? abo->tbo.resource->mem_type : 0;
9791 }
9792
amdgpu_dm_update_cursor(struct drm_plane * plane,struct drm_plane_state * old_plane_state,struct dc_stream_update * update)9793 static void amdgpu_dm_update_cursor(struct drm_plane *plane,
9794 struct drm_plane_state *old_plane_state,
9795 struct dc_stream_update *update)
9796 {
9797 struct amdgpu_device *adev = drm_to_adev(plane->dev);
9798 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
9799 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
9800 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
9801 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
9802 uint64_t address = afb ? afb->address : 0;
9803 struct dc_cursor_position position = {0};
9804 struct dc_cursor_attributes attributes;
9805 int ret;
9806
9807 if (!plane->state->fb && !old_plane_state->fb)
9808 return;
9809
9810 drm_dbg_atomic(plane->dev, "crtc_id=%d with size %d to %d\n",
9811 amdgpu_crtc->crtc_id, plane->state->crtc_w,
9812 plane->state->crtc_h);
9813
9814 ret = amdgpu_dm_plane_get_cursor_position(plane, crtc, &position);
9815 if (ret)
9816 return;
9817
9818 if (!position.enable) {
9819 /* turn off cursor */
9820 if (crtc_state && crtc_state->stream) {
9821 dc_stream_set_cursor_position(crtc_state->stream,
9822 &position);
9823 update->cursor_position = &crtc_state->stream->cursor_position;
9824 }
9825 return;
9826 }
9827
9828 amdgpu_crtc->cursor_width = plane->state->crtc_w;
9829 amdgpu_crtc->cursor_height = plane->state->crtc_h;
9830
9831 memset(&attributes, 0, sizeof(attributes));
9832 attributes.address.high_part = upper_32_bits(address);
9833 attributes.address.low_part = lower_32_bits(address);
9834 attributes.width = plane->state->crtc_w;
9835 attributes.height = plane->state->crtc_h;
9836 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
9837 attributes.rotation_angle = 0;
9838 attributes.attribute_flags.value = 0;
9839
9840 /* Enable cursor degamma ROM on DCN3+ for implicit sRGB degamma in DRM
9841 * legacy gamma setup.
9842 */
9843 if (crtc_state->cm_is_degamma_srgb &&
9844 adev->dm.dc->caps.color.dpp.gamma_corr)
9845 attributes.attribute_flags.bits.ENABLE_CURSOR_DEGAMMA = 1;
9846
9847 if (afb)
9848 attributes.pitch = afb->base.pitches[0] / afb->base.format->cpp[0];
9849
9850 if (crtc_state->stream) {
9851 if (!dc_stream_set_cursor_attributes(crtc_state->stream,
9852 &attributes))
9853 drm_err(adev_to_drm(adev), "DC failed to set cursor attributes\n");
9854
9855 update->cursor_attributes = &crtc_state->stream->cursor_attributes;
9856
9857 if (!dc_stream_set_cursor_position(crtc_state->stream,
9858 &position))
9859 drm_err(adev_to_drm(adev), "DC failed to set cursor position\n");
9860
9861 update->cursor_position = &crtc_state->stream->cursor_position;
9862 }
9863 }
9864
amdgpu_dm_enable_self_refresh(struct amdgpu_crtc * acrtc_attach,const struct dm_crtc_state * acrtc_state,const u64 current_ts)9865 static void amdgpu_dm_enable_self_refresh(struct amdgpu_crtc *acrtc_attach,
9866 const struct dm_crtc_state *acrtc_state,
9867 const u64 current_ts)
9868 {
9869 struct psr_settings *psr = &acrtc_state->stream->link->psr_settings;
9870 struct replay_settings *pr = &acrtc_state->stream->link->replay_settings;
9871 struct amdgpu_dm_connector *aconn =
9872 (struct amdgpu_dm_connector *)acrtc_state->stream->dm_stream_context;
9873 bool vrr_active = amdgpu_dm_crtc_vrr_active(acrtc_state);
9874
9875 if (acrtc_state->update_type > UPDATE_TYPE_FAST) {
9876 if (pr->config.replay_supported && !pr->replay_feature_enabled)
9877 amdgpu_dm_link_setup_replay(acrtc_state->stream->link, aconn);
9878 else if (psr->psr_version != DC_PSR_VERSION_UNSUPPORTED &&
9879 !psr->psr_feature_enabled)
9880 if (!aconn->disallow_edp_enter_psr)
9881 amdgpu_dm_link_setup_psr(acrtc_state->stream);
9882 }
9883
9884 /* Decrement skip count when SR is enabled and we're doing fast updates. */
9885 if (acrtc_state->update_type == UPDATE_TYPE_FAST &&
9886 (psr->psr_feature_enabled || pr->config.replay_supported)) {
9887 if (aconn->sr_skip_count > 0)
9888 aconn->sr_skip_count--;
9889
9890 /* Allow SR when skip count is 0. */
9891 acrtc_attach->dm_irq_params.allow_sr_entry = !aconn->sr_skip_count;
9892
9893 /*
9894 * If sink supports PSR SU/Panel Replay, there is no need to rely on
9895 * a vblank event disable request to enable PSR/RP. PSR SU/RP
9896 * can be enabled immediately once OS demonstrates an
9897 * adequate number of fast atomic commits to notify KMD
9898 * of update events. See `vblank_control_worker()`.
9899 */
9900 if (!vrr_active &&
9901 acrtc_attach->dm_irq_params.allow_sr_entry &&
9902 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
9903 !amdgpu_dm_crc_window_is_activated(acrtc_state->base.crtc) &&
9904 #endif
9905 (current_ts - psr->psr_dirty_rects_change_timestamp_ns) > 500000000) {
9906 if (pr->replay_feature_enabled && !pr->replay_allow_active)
9907 amdgpu_dm_replay_enable(acrtc_state->stream, true);
9908 if (psr->psr_version == DC_PSR_VERSION_SU_1 &&
9909 !psr->psr_allow_active && !aconn->disallow_edp_enter_psr)
9910 amdgpu_dm_psr_enable(acrtc_state->stream);
9911 }
9912 } else {
9913 acrtc_attach->dm_irq_params.allow_sr_entry = false;
9914 }
9915 }
9916
amdgpu_dm_commit_planes(struct drm_atomic_state * state,struct drm_device * dev,struct amdgpu_display_manager * dm,struct drm_crtc * pcrtc,bool wait_for_vblank)9917 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
9918 struct drm_device *dev,
9919 struct amdgpu_display_manager *dm,
9920 struct drm_crtc *pcrtc,
9921 bool wait_for_vblank)
9922 {
9923 u32 i;
9924 u64 timestamp_ns = ktime_get_ns();
9925 struct drm_plane *plane;
9926 struct drm_plane_state *old_plane_state, *new_plane_state;
9927 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
9928 struct drm_crtc_state *new_pcrtc_state =
9929 drm_atomic_get_new_crtc_state(state, pcrtc);
9930 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
9931 struct dm_crtc_state *dm_old_crtc_state =
9932 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
9933 int planes_count = 0, vpos, hpos;
9934 unsigned long flags;
9935 u32 target_vblank, last_flip_vblank;
9936 bool vrr_active = amdgpu_dm_crtc_vrr_active(acrtc_state);
9937 bool cursor_update = false;
9938 bool pflip_present = false;
9939 bool dirty_rects_changed = false;
9940 bool updated_planes_and_streams = false;
9941 struct {
9942 struct dc_surface_update surface_updates[MAX_SURFACES];
9943 struct dc_plane_info plane_infos[MAX_SURFACES];
9944 struct dc_scaling_info scaling_infos[MAX_SURFACES];
9945 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
9946 struct dc_stream_update stream_update;
9947 } *bundle;
9948
9949 bundle = kzalloc_obj(*bundle);
9950
9951 if (!bundle) {
9952 drm_err(dev, "Failed to allocate update bundle\n");
9953 goto cleanup;
9954 }
9955
9956 /*
9957 * Disable the cursor first if we're disabling all the planes.
9958 * It'll remain on the screen after the planes are re-enabled
9959 * if we don't.
9960 *
9961 * If the cursor is transitioning from native to overlay mode, the
9962 * native cursor needs to be disabled first.
9963 */
9964 if (acrtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE &&
9965 dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE) {
9966 struct dc_cursor_position cursor_position = {0};
9967
9968 if (!dc_stream_set_cursor_position(acrtc_state->stream,
9969 &cursor_position))
9970 drm_err(dev, "DC failed to disable native cursor\n");
9971
9972 bundle->stream_update.cursor_position =
9973 &acrtc_state->stream->cursor_position;
9974 }
9975
9976 if (acrtc_state->active_planes == 0 &&
9977 dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE)
9978 amdgpu_dm_commit_cursors(state);
9979
9980 /* update planes when needed */
9981 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
9982 struct drm_crtc *crtc = new_plane_state->crtc;
9983 struct drm_crtc_state *new_crtc_state;
9984 struct drm_framebuffer *fb = new_plane_state->fb;
9985 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)fb;
9986 bool plane_needs_flip;
9987 struct dc_plane_state *dc_plane;
9988 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
9989
9990 /* Cursor plane is handled after stream updates */
9991 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
9992 acrtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE) {
9993 if ((fb && crtc == pcrtc) ||
9994 (old_plane_state->fb && old_plane_state->crtc == pcrtc)) {
9995 cursor_update = true;
9996 if (amdgpu_ip_version(dm->adev, DCE_HWIP, 0) != 0)
9997 amdgpu_dm_update_cursor(plane, old_plane_state, &bundle->stream_update);
9998 }
9999
10000 continue;
10001 }
10002
10003 if (!fb || !crtc || pcrtc != crtc)
10004 continue;
10005
10006 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
10007 if (!new_crtc_state->active)
10008 continue;
10009
10010 dc_plane = dm_new_plane_state->dc_state;
10011 if (!dc_plane)
10012 continue;
10013
10014 bundle->surface_updates[planes_count].surface = dc_plane;
10015 if (new_pcrtc_state->color_mgmt_changed) {
10016 bundle->surface_updates[planes_count].gamma = &dc_plane->gamma_correction;
10017 bundle->surface_updates[planes_count].in_transfer_func = &dc_plane->in_transfer_func;
10018 bundle->surface_updates[planes_count].gamut_remap_matrix = &dc_plane->gamut_remap_matrix;
10019 bundle->surface_updates[planes_count].hdr_mult = dc_plane->hdr_mult;
10020 bundle->surface_updates[planes_count].func_shaper = &dc_plane->in_shaper_func;
10021 bundle->surface_updates[planes_count].lut3d_func = &dc_plane->lut3d_func;
10022 bundle->surface_updates[planes_count].blend_tf = &dc_plane->blend_tf;
10023 }
10024
10025 amdgpu_dm_plane_fill_dc_scaling_info(dm->adev, new_plane_state,
10026 &bundle->scaling_infos[planes_count]);
10027
10028 bundle->surface_updates[planes_count].scaling_info =
10029 &bundle->scaling_infos[planes_count];
10030
10031 plane_needs_flip = old_plane_state->fb && new_plane_state->fb;
10032
10033 pflip_present = pflip_present || plane_needs_flip;
10034
10035 if (!plane_needs_flip) {
10036 planes_count += 1;
10037 continue;
10038 }
10039
10040 fill_dc_plane_info_and_addr(
10041 dm->adev, new_plane_state,
10042 afb->tiling_flags,
10043 &bundle->plane_infos[planes_count],
10044 &bundle->flip_addrs[planes_count].address,
10045 afb->tmz_surface);
10046
10047 drm_dbg_state(state->dev, "plane: id=%d dcc_en=%d\n",
10048 new_plane_state->plane->index,
10049 bundle->plane_infos[planes_count].dcc.enable);
10050
10051 bundle->surface_updates[planes_count].plane_info =
10052 &bundle->plane_infos[planes_count];
10053
10054 if (acrtc_state->stream->link->psr_settings.psr_feature_enabled ||
10055 acrtc_state->stream->link->replay_settings.replay_feature_enabled) {
10056 fill_dc_dirty_rects(plane, old_plane_state,
10057 new_plane_state, new_crtc_state,
10058 &bundle->flip_addrs[planes_count],
10059 acrtc_state->stream->link->psr_settings.psr_version ==
10060 DC_PSR_VERSION_SU_1,
10061 &dirty_rects_changed);
10062
10063 /*
10064 * If the dirty regions changed, PSR-SU need to be disabled temporarily
10065 * and enabled it again after dirty regions are stable to avoid video glitch.
10066 * PSR-SU will be enabled in vblank_control_worker() if user pause the video
10067 * during the PSR-SU was disabled.
10068 */
10069 if (acrtc_state->stream->link->psr_settings.psr_version >= DC_PSR_VERSION_SU_1 &&
10070 acrtc_attach->dm_irq_params.allow_sr_entry &&
10071 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
10072 !amdgpu_dm_crc_window_is_activated(acrtc_state->base.crtc) &&
10073 #endif
10074 dirty_rects_changed) {
10075 mutex_lock(&dm->dc_lock);
10076 acrtc_state->stream->link->psr_settings.psr_dirty_rects_change_timestamp_ns =
10077 timestamp_ns;
10078 if (acrtc_state->stream->link->psr_settings.psr_allow_active)
10079 amdgpu_dm_psr_disable(acrtc_state->stream, true);
10080 mutex_unlock(&dm->dc_lock);
10081 }
10082 }
10083
10084 /*
10085 * Only allow immediate flips for fast updates that don't
10086 * change memory domain, FB pitch, DCC state, rotation or
10087 * mirroring.
10088 *
10089 * dm_crtc_helper_atomic_check() only accepts async flips with
10090 * fast updates.
10091 */
10092 if (crtc->state->async_flip &&
10093 (acrtc_state->update_type != UPDATE_TYPE_FAST ||
10094 get_mem_type(old_plane_state->fb) != get_mem_type(fb)))
10095 drm_warn_once(state->dev,
10096 "[PLANE:%d:%s] async flip with non-fast update\n",
10097 plane->base.id, plane->name);
10098
10099 bundle->flip_addrs[planes_count].flip_immediate =
10100 crtc->state->async_flip &&
10101 acrtc_state->update_type == UPDATE_TYPE_FAST &&
10102 get_mem_type(old_plane_state->fb) == get_mem_type(fb);
10103
10104 timestamp_ns = ktime_get_ns();
10105 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000);
10106 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count];
10107 bundle->surface_updates[planes_count].surface = dc_plane;
10108
10109 if (!bundle->surface_updates[planes_count].surface) {
10110 drm_err(dev, "No surface for CRTC: id=%d\n",
10111 acrtc_attach->crtc_id);
10112 continue;
10113 }
10114
10115 if (plane == pcrtc->primary)
10116 update_freesync_state_on_stream(
10117 dm,
10118 acrtc_state,
10119 acrtc_state->stream,
10120 dc_plane,
10121 bundle->flip_addrs[planes_count].flip_timestamp_in_us);
10122
10123 drm_dbg_state(state->dev, "%s Flipping to hi: 0x%x, low: 0x%x\n",
10124 __func__,
10125 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
10126 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
10127
10128 planes_count += 1;
10129
10130 }
10131
10132 if (pflip_present) {
10133 if (!vrr_active) {
10134 /* Use old throttling in non-vrr fixed refresh rate mode
10135 * to keep flip scheduling based on target vblank counts
10136 * working in a backwards compatible way, e.g., for
10137 * clients using the GLX_OML_sync_control extension or
10138 * DRI3/Present extension with defined target_msc.
10139 */
10140 last_flip_vblank = amdgpu_get_vblank_counter_kms(pcrtc);
10141 } else {
10142 /* For variable refresh rate mode only:
10143 * Get vblank of last completed flip to avoid > 1 vrr
10144 * flips per video frame by use of throttling, but allow
10145 * flip programming anywhere in the possibly large
10146 * variable vrr vblank interval for fine-grained flip
10147 * timing control and more opportunity to avoid stutter
10148 * on late submission of flips.
10149 */
10150 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10151 last_flip_vblank = acrtc_attach->dm_irq_params.last_flip_vblank;
10152 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10153 }
10154
10155 target_vblank = last_flip_vblank + wait_for_vblank;
10156
10157 /*
10158 * Wait until we're out of the vertical blank period before the one
10159 * targeted by the flip
10160 */
10161 while ((acrtc_attach->enabled &&
10162 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id,
10163 0, &vpos, &hpos, NULL,
10164 NULL, &pcrtc->hwmode)
10165 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
10166 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
10167 (int)(target_vblank -
10168 amdgpu_get_vblank_counter_kms(pcrtc)) > 0)) {
10169 usleep_range(1000, 1100);
10170 }
10171
10172 /**
10173 * Prepare the flip event for the pageflip interrupt to handle.
10174 *
10175 * This only works in the case where we've already turned on the
10176 * appropriate hardware blocks (eg. HUBP) so in the transition case
10177 * from 0 -> n planes we have to skip a hardware generated event
10178 * and rely on sending it from software.
10179 */
10180 if (acrtc_attach->base.state->event &&
10181 acrtc_state->active_planes > 0) {
10182 drm_crtc_vblank_get(pcrtc);
10183
10184 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10185
10186 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE);
10187 prepare_flip_isr(acrtc_attach);
10188
10189 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10190 }
10191
10192 if (acrtc_state->stream) {
10193 if (acrtc_state->freesync_vrr_info_changed)
10194 bundle->stream_update.vrr_infopacket =
10195 &acrtc_state->stream->vrr_infopacket;
10196 }
10197 } else if (cursor_update && acrtc_state->active_planes > 0) {
10198 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10199 if (acrtc_attach->base.state->event) {
10200 drm_crtc_vblank_get(pcrtc);
10201 acrtc_attach->event = acrtc_attach->base.state->event;
10202 acrtc_attach->base.state->event = NULL;
10203 }
10204 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10205 }
10206
10207 /* Update the planes if changed or disable if we don't have any. */
10208 if ((planes_count || acrtc_state->active_planes == 0) &&
10209 acrtc_state->stream) {
10210 /*
10211 * If PSR or idle optimizations are enabled then flush out
10212 * any pending work before hardware programming.
10213 */
10214 if (dm->vblank_control_workqueue)
10215 flush_workqueue(dm->vblank_control_workqueue);
10216
10217 bundle->stream_update.stream = acrtc_state->stream;
10218 if (new_pcrtc_state->mode_changed) {
10219 bundle->stream_update.src = acrtc_state->stream->src;
10220 bundle->stream_update.dst = acrtc_state->stream->dst;
10221 }
10222
10223 if (new_pcrtc_state->color_mgmt_changed) {
10224 /*
10225 * TODO: This isn't fully correct since we've actually
10226 * already modified the stream in place.
10227 */
10228 bundle->stream_update.gamut_remap =
10229 &acrtc_state->stream->gamut_remap_matrix;
10230 bundle->stream_update.output_csc_transform =
10231 &acrtc_state->stream->csc_color_matrix;
10232 bundle->stream_update.out_transfer_func =
10233 &acrtc_state->stream->out_transfer_func;
10234 bundle->stream_update.lut3d_func =
10235 (struct dc_3dlut *) acrtc_state->stream->lut3d_func;
10236 bundle->stream_update.func_shaper =
10237 (struct dc_transfer_func *) acrtc_state->stream->func_shaper;
10238 }
10239
10240 acrtc_state->stream->abm_level = acrtc_state->abm_level;
10241 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level)
10242 bundle->stream_update.abm_level = &acrtc_state->abm_level;
10243
10244 mutex_lock(&dm->dc_lock);
10245 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) || vrr_active) {
10246 if (acrtc_state->stream->link->replay_settings.replay_allow_active)
10247 amdgpu_dm_replay_disable(acrtc_state->stream);
10248 if (acrtc_state->stream->link->psr_settings.psr_allow_active)
10249 amdgpu_dm_psr_disable(acrtc_state->stream, true);
10250 }
10251 mutex_unlock(&dm->dc_lock);
10252
10253 /*
10254 * If FreeSync state on the stream has changed then we need to
10255 * re-adjust the min/max bounds now that DC doesn't handle this
10256 * as part of commit.
10257 */
10258 if (is_dc_timing_adjust_needed(dm_old_crtc_state, acrtc_state)) {
10259 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10260 dc_stream_adjust_vmin_vmax(
10261 dm->dc, acrtc_state->stream,
10262 &acrtc_attach->dm_irq_params.vrr_params.adjust);
10263 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10264 }
10265 mutex_lock(&dm->dc_lock);
10266 update_planes_and_stream_adapter(dm->dc,
10267 acrtc_state->update_type,
10268 planes_count,
10269 acrtc_state->stream,
10270 &bundle->stream_update,
10271 bundle->surface_updates);
10272 updated_planes_and_streams = true;
10273
10274 /**
10275 * Enable or disable the interrupts on the backend.
10276 *
10277 * Most pipes are put into power gating when unused.
10278 *
10279 * When power gating is enabled on a pipe we lose the
10280 * interrupt enablement state when power gating is disabled.
10281 *
10282 * So we need to update the IRQ control state in hardware
10283 * whenever the pipe turns on (since it could be previously
10284 * power gated) or off (since some pipes can't be power gated
10285 * on some ASICs).
10286 */
10287 if (dm_old_crtc_state->active_planes != acrtc_state->active_planes)
10288 dm_update_pflip_irq_state(drm_to_adev(dev),
10289 acrtc_attach);
10290
10291 amdgpu_dm_enable_self_refresh(acrtc_attach, acrtc_state, timestamp_ns);
10292 mutex_unlock(&dm->dc_lock);
10293 }
10294
10295 /*
10296 * Update cursor state *after* programming all the planes.
10297 * This avoids redundant programming in the case where we're going
10298 * to be disabling a single plane - those pipes are being disabled.
10299 */
10300 if (acrtc_state->active_planes &&
10301 (!updated_planes_and_streams || amdgpu_ip_version(dm->adev, DCE_HWIP, 0) == 0) &&
10302 acrtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE)
10303 amdgpu_dm_commit_cursors(state);
10304
10305 cleanup:
10306 kfree(bundle);
10307 }
10308
amdgpu_dm_commit_audio(struct drm_device * dev,struct drm_atomic_state * state)10309 static void amdgpu_dm_commit_audio(struct drm_device *dev,
10310 struct drm_atomic_state *state)
10311 {
10312 struct amdgpu_device *adev = drm_to_adev(dev);
10313 struct amdgpu_dm_connector *aconnector;
10314 struct drm_connector *connector;
10315 struct drm_connector_state *old_con_state, *new_con_state;
10316 struct drm_crtc_state *new_crtc_state;
10317 struct dm_crtc_state *new_dm_crtc_state;
10318 const struct dc_stream_status *status;
10319 int i, inst;
10320
10321 /* Notify device removals. */
10322 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10323 if (old_con_state->crtc != new_con_state->crtc) {
10324 /* CRTC changes require notification. */
10325 goto notify;
10326 }
10327
10328 if (!new_con_state->crtc)
10329 continue;
10330
10331 new_crtc_state = drm_atomic_get_new_crtc_state(
10332 state, new_con_state->crtc);
10333
10334 if (!new_crtc_state)
10335 continue;
10336
10337 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
10338 continue;
10339
10340 notify:
10341 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
10342 continue;
10343
10344 aconnector = to_amdgpu_dm_connector(connector);
10345
10346 mutex_lock(&adev->dm.audio_lock);
10347 inst = aconnector->audio_inst;
10348 aconnector->audio_inst = -1;
10349 mutex_unlock(&adev->dm.audio_lock);
10350
10351 amdgpu_dm_audio_eld_notify(adev, inst);
10352 }
10353
10354 /* Notify audio device additions. */
10355 for_each_new_connector_in_state(state, connector, new_con_state, i) {
10356 if (!new_con_state->crtc)
10357 continue;
10358
10359 new_crtc_state = drm_atomic_get_new_crtc_state(
10360 state, new_con_state->crtc);
10361
10362 if (!new_crtc_state)
10363 continue;
10364
10365 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
10366 continue;
10367
10368 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
10369 if (!new_dm_crtc_state->stream)
10370 continue;
10371
10372 status = dc_stream_get_status(new_dm_crtc_state->stream);
10373 if (!status)
10374 continue;
10375
10376 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
10377 continue;
10378
10379 aconnector = to_amdgpu_dm_connector(connector);
10380
10381 mutex_lock(&adev->dm.audio_lock);
10382 inst = status->audio_inst;
10383 aconnector->audio_inst = inst;
10384 mutex_unlock(&adev->dm.audio_lock);
10385
10386 amdgpu_dm_audio_eld_notify(adev, inst);
10387 }
10388 }
10389
10390 /*
10391 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
10392 * @crtc_state: the DRM CRTC state
10393 * @stream_state: the DC stream state.
10394 *
10395 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
10396 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
10397 */
amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state * crtc_state,struct dc_stream_state * stream_state)10398 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
10399 struct dc_stream_state *stream_state)
10400 {
10401 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
10402 }
10403
dm_clear_writeback(struct amdgpu_display_manager * dm,struct dm_crtc_state * crtc_state)10404 static void dm_clear_writeback(struct amdgpu_display_manager *dm,
10405 struct dm_crtc_state *crtc_state)
10406 {
10407 dc_stream_remove_writeback(dm->dc, crtc_state->stream, 0);
10408 }
10409
amdgpu_dm_commit_streams(struct drm_atomic_state * state,struct dc_state * dc_state)10410 static void amdgpu_dm_commit_streams(struct drm_atomic_state *state,
10411 struct dc_state *dc_state)
10412 {
10413 struct drm_device *dev = state->dev;
10414 struct amdgpu_device *adev = drm_to_adev(dev);
10415 struct amdgpu_display_manager *dm = &adev->dm;
10416 struct drm_crtc *crtc;
10417 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10418 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10419 struct drm_connector_state *old_con_state;
10420 struct drm_connector *connector;
10421 bool mode_set_reset_required = false;
10422 u32 i;
10423 struct dc_commit_streams_params params = {dc_state->streams, dc_state->stream_count};
10424 bool set_backlight_level = false;
10425
10426 /* Disable writeback */
10427 for_each_old_connector_in_state(state, connector, old_con_state, i) {
10428 struct dm_connector_state *dm_old_con_state;
10429 struct amdgpu_crtc *acrtc;
10430
10431 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
10432 continue;
10433
10434 old_crtc_state = NULL;
10435
10436 dm_old_con_state = to_dm_connector_state(old_con_state);
10437 if (!dm_old_con_state->base.crtc)
10438 continue;
10439
10440 acrtc = to_amdgpu_crtc(dm_old_con_state->base.crtc);
10441 if (acrtc)
10442 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
10443
10444 if (!acrtc || !acrtc->wb_enabled)
10445 continue;
10446
10447 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10448
10449 dm_clear_writeback(dm, dm_old_crtc_state);
10450 acrtc->wb_enabled = false;
10451 }
10452
10453 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
10454 new_crtc_state, i) {
10455 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
10456
10457 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10458
10459 if (old_crtc_state->active &&
10460 (!new_crtc_state->active ||
10461 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
10462 manage_dm_interrupts(adev, acrtc, NULL);
10463 dc_stream_release(dm_old_crtc_state->stream);
10464 }
10465 }
10466
10467 drm_atomic_helper_calc_timestamping_constants(state);
10468
10469 /* update changed items */
10470 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10471 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
10472
10473 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10474 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10475
10476 drm_dbg_state(state->dev,
10477 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n",
10478 acrtc->crtc_id,
10479 new_crtc_state->enable,
10480 new_crtc_state->active,
10481 new_crtc_state->planes_changed,
10482 new_crtc_state->mode_changed,
10483 new_crtc_state->active_changed,
10484 new_crtc_state->connectors_changed);
10485
10486 /* Disable cursor if disabling crtc */
10487 if (old_crtc_state->active && !new_crtc_state->active) {
10488 struct dc_cursor_position position;
10489
10490 memset(&position, 0, sizeof(position));
10491 mutex_lock(&dm->dc_lock);
10492 dc_exit_ips_for_hw_access(dm->dc);
10493 dc_stream_program_cursor_position(dm_old_crtc_state->stream, &position);
10494 mutex_unlock(&dm->dc_lock);
10495 }
10496
10497 /* Copy all transient state flags into dc state */
10498 if (dm_new_crtc_state->stream) {
10499 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
10500 dm_new_crtc_state->stream);
10501 }
10502
10503 /* handles headless hotplug case, updating new_state and
10504 * aconnector as needed
10505 */
10506
10507 if (amdgpu_dm_crtc_modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
10508
10509 drm_dbg_atomic(dev,
10510 "Atomic commit: SET crtc id %d: [%p]\n",
10511 acrtc->crtc_id, acrtc);
10512
10513 if (!dm_new_crtc_state->stream) {
10514 /*
10515 * this could happen because of issues with
10516 * userspace notifications delivery.
10517 * In this case userspace tries to set mode on
10518 * display which is disconnected in fact.
10519 * dc_sink is NULL in this case on aconnector.
10520 * We expect reset mode will come soon.
10521 *
10522 * This can also happen when unplug is done
10523 * during resume sequence ended
10524 *
10525 * In this case, we want to pretend we still
10526 * have a sink to keep the pipe running so that
10527 * hw state is consistent with the sw state
10528 */
10529 drm_dbg_atomic(dev,
10530 "Failed to create new stream for crtc %d\n",
10531 acrtc->base.base.id);
10532 continue;
10533 }
10534
10535 if (dm_old_crtc_state->stream)
10536 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
10537
10538 pm_runtime_get_noresume(dev->dev);
10539
10540 acrtc->enabled = true;
10541 acrtc->hw_mode = new_crtc_state->mode;
10542 crtc->hwmode = new_crtc_state->mode;
10543 mode_set_reset_required = true;
10544 set_backlight_level = true;
10545 } else if (modereset_required(new_crtc_state)) {
10546 drm_dbg_atomic(dev,
10547 "Atomic commit: RESET. crtc id %d:[%p]\n",
10548 acrtc->crtc_id, acrtc);
10549 /* i.e. reset mode */
10550 if (dm_old_crtc_state->stream)
10551 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
10552
10553 mode_set_reset_required = true;
10554 }
10555 } /* for_each_crtc_in_state() */
10556
10557 /* if there mode set or reset, disable eDP PSR, Replay */
10558 if (mode_set_reset_required) {
10559 if (dm->vblank_control_workqueue)
10560 flush_workqueue(dm->vblank_control_workqueue);
10561
10562 amdgpu_dm_replay_disable_all(dm);
10563 amdgpu_dm_psr_disable_all(dm);
10564 }
10565
10566 dm_enable_per_frame_crtc_master_sync(dc_state);
10567 mutex_lock(&dm->dc_lock);
10568 dc_exit_ips_for_hw_access(dm->dc);
10569 WARN_ON(!dc_commit_streams(dm->dc, ¶ms));
10570
10571 /* Allow idle optimization when vblank count is 0 for display off */
10572 if ((dm->active_vblank_irq_count == 0) && amdgpu_dm_is_headless(dm->adev))
10573 dc_allow_idle_optimizations(dm->dc, true);
10574 mutex_unlock(&dm->dc_lock);
10575
10576 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
10577 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
10578
10579 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10580
10581 if (dm_new_crtc_state->stream != NULL) {
10582 const struct dc_stream_status *status =
10583 dc_stream_get_status(dm_new_crtc_state->stream);
10584
10585 if (!status)
10586 status = dc_state_get_stream_status(dc_state,
10587 dm_new_crtc_state->stream);
10588 if (!status)
10589 drm_err(dev,
10590 "got no status for stream %p on acrtc%p\n",
10591 dm_new_crtc_state->stream, acrtc);
10592 else
10593 acrtc->otg_inst = status->primary_otg_inst;
10594 }
10595 }
10596
10597 /* During boot up and resume the DC layer will reset the panel brightness
10598 * to fix a flicker issue.
10599 * It will cause the dm->actual_brightness is not the current panel brightness
10600 * level. (the dm->brightness is the correct panel level)
10601 * So we set the backlight level with dm->brightness value after set mode
10602 */
10603 if (set_backlight_level) {
10604 for (i = 0; i < dm->num_of_edps; i++) {
10605 if (dm->backlight_dev[i])
10606 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
10607 }
10608 }
10609 }
10610
dm_set_writeback(struct amdgpu_display_manager * dm,struct dm_crtc_state * crtc_state,struct drm_connector * connector,struct drm_connector_state * new_con_state)10611 static void dm_set_writeback(struct amdgpu_display_manager *dm,
10612 struct dm_crtc_state *crtc_state,
10613 struct drm_connector *connector,
10614 struct drm_connector_state *new_con_state)
10615 {
10616 struct drm_writeback_connector *wb_conn = drm_connector_to_writeback(connector);
10617 struct amdgpu_device *adev = dm->adev;
10618 struct amdgpu_crtc *acrtc;
10619 struct dc_writeback_info *wb_info;
10620 struct pipe_ctx *pipe = NULL;
10621 struct amdgpu_framebuffer *afb;
10622 int i = 0;
10623
10624 wb_info = kzalloc_obj(*wb_info);
10625 if (!wb_info) {
10626 drm_err(adev_to_drm(adev), "Failed to allocate wb_info\n");
10627 return;
10628 }
10629
10630 acrtc = to_amdgpu_crtc(wb_conn->encoder.crtc);
10631 if (!acrtc) {
10632 drm_err(adev_to_drm(adev), "no amdgpu_crtc found\n");
10633 kfree(wb_info);
10634 return;
10635 }
10636
10637 afb = to_amdgpu_framebuffer(new_con_state->writeback_job->fb);
10638 if (!afb) {
10639 drm_err(adev_to_drm(adev), "No amdgpu_framebuffer found\n");
10640 kfree(wb_info);
10641 return;
10642 }
10643
10644 for (i = 0; i < MAX_PIPES; i++) {
10645 if (dm->dc->current_state->res_ctx.pipe_ctx[i].stream == crtc_state->stream) {
10646 pipe = &dm->dc->current_state->res_ctx.pipe_ctx[i];
10647 break;
10648 }
10649 }
10650
10651 /* fill in wb_info */
10652 wb_info->wb_enabled = true;
10653
10654 wb_info->dwb_pipe_inst = 0;
10655 wb_info->dwb_params.dwbscl_black_color = 0;
10656 wb_info->dwb_params.hdr_mult = 0x1F000;
10657 wb_info->dwb_params.csc_params.gamut_adjust_type = CM_GAMUT_ADJUST_TYPE_BYPASS;
10658 wb_info->dwb_params.csc_params.gamut_coef_format = CM_GAMUT_REMAP_COEF_FORMAT_S2_13;
10659 wb_info->dwb_params.output_depth = DWB_OUTPUT_PIXEL_DEPTH_10BPC;
10660 wb_info->dwb_params.cnv_params.cnv_out_bpc = DWB_CNV_OUT_BPC_10BPC;
10661
10662 /* width & height from crtc */
10663 wb_info->dwb_params.cnv_params.src_width = acrtc->base.mode.crtc_hdisplay;
10664 wb_info->dwb_params.cnv_params.src_height = acrtc->base.mode.crtc_vdisplay;
10665 wb_info->dwb_params.dest_width = acrtc->base.mode.crtc_hdisplay;
10666 wb_info->dwb_params.dest_height = acrtc->base.mode.crtc_vdisplay;
10667
10668 wb_info->dwb_params.cnv_params.crop_en = false;
10669 wb_info->dwb_params.stereo_params.stereo_enabled = false;
10670
10671 wb_info->dwb_params.cnv_params.out_max_pix_val = 0x3ff; // 10 bits
10672 wb_info->dwb_params.cnv_params.out_min_pix_val = 0;
10673 wb_info->dwb_params.cnv_params.fc_out_format = DWB_OUT_FORMAT_32BPP_ARGB;
10674 wb_info->dwb_params.cnv_params.out_denorm_mode = DWB_OUT_DENORM_BYPASS;
10675
10676 wb_info->dwb_params.out_format = dwb_scaler_mode_bypass444;
10677
10678 wb_info->dwb_params.capture_rate = dwb_capture_rate_0;
10679
10680 wb_info->dwb_params.scaler_taps.h_taps = 1;
10681 wb_info->dwb_params.scaler_taps.v_taps = 1;
10682 wb_info->dwb_params.scaler_taps.h_taps_c = 1;
10683 wb_info->dwb_params.scaler_taps.v_taps_c = 1;
10684 wb_info->dwb_params.subsample_position = DWB_INTERSTITIAL_SUBSAMPLING;
10685
10686 wb_info->mcif_buf_params.luma_pitch = afb->base.pitches[0];
10687 wb_info->mcif_buf_params.chroma_pitch = afb->base.pitches[1];
10688
10689 for (i = 0; i < DWB_MCIF_BUF_COUNT; i++) {
10690 wb_info->mcif_buf_params.luma_address[i] = afb->address;
10691 wb_info->mcif_buf_params.chroma_address[i] = 0;
10692 }
10693
10694 wb_info->mcif_buf_params.p_vmid = 1;
10695 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 0, 0)) {
10696 wb_info->mcif_warmup_params.start_address.quad_part = afb->address;
10697 wb_info->mcif_warmup_params.region_size =
10698 wb_info->mcif_buf_params.luma_pitch * wb_info->dwb_params.dest_height;
10699 }
10700 wb_info->mcif_warmup_params.p_vmid = 1;
10701 wb_info->writeback_source_plane = pipe->plane_state;
10702
10703 dc_stream_add_writeback(dm->dc, crtc_state->stream, wb_info);
10704
10705 acrtc->wb_pending = true;
10706 acrtc->wb_conn = wb_conn;
10707 drm_writeback_queue_job(wb_conn, new_con_state);
10708 }
10709
amdgpu_dm_update_hdcp(struct drm_atomic_state * state)10710 static void amdgpu_dm_update_hdcp(struct drm_atomic_state *state)
10711 {
10712 struct drm_connector_state *old_con_state, *new_con_state;
10713 struct drm_device *dev = state->dev;
10714 struct drm_connector *connector;
10715 struct amdgpu_device *adev = drm_to_adev(dev);
10716 int i;
10717
10718 if (!adev->dm.hdcp_workqueue)
10719 return;
10720
10721 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10722 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
10723 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
10724 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10725 struct dm_crtc_state *dm_new_crtc_state;
10726 struct amdgpu_dm_connector *aconnector;
10727
10728 if (!connector || connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
10729 continue;
10730
10731 aconnector = to_amdgpu_dm_connector(connector);
10732
10733 drm_dbg(dev, "[HDCP_DM] -------------- i : %x ----------\n", i);
10734
10735 drm_dbg(dev, "[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n",
10736 connector->index, connector->status, connector->dpms);
10737 drm_dbg(dev, "[HDCP_DM] state protection old: %x new: %x\n",
10738 old_con_state->content_protection, new_con_state->content_protection);
10739
10740 if (aconnector->dc_sink) {
10741 if (aconnector->dc_sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
10742 aconnector->dc_sink->sink_signal != SIGNAL_TYPE_NONE) {
10743 drm_dbg(dev, "[HDCP_DM] pipe_ctx dispname=%s\n",
10744 aconnector->dc_sink->edid_caps.display_name);
10745 }
10746 }
10747
10748 new_crtc_state = NULL;
10749 old_crtc_state = NULL;
10750
10751 if (acrtc) {
10752 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
10753 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
10754 }
10755
10756 if (old_crtc_state)
10757 drm_dbg(dev, "old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
10758 old_crtc_state->enable,
10759 old_crtc_state->active,
10760 old_crtc_state->mode_changed,
10761 old_crtc_state->active_changed,
10762 old_crtc_state->connectors_changed);
10763
10764 if (new_crtc_state)
10765 drm_dbg(dev, "NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
10766 new_crtc_state->enable,
10767 new_crtc_state->active,
10768 new_crtc_state->mode_changed,
10769 new_crtc_state->active_changed,
10770 new_crtc_state->connectors_changed);
10771
10772
10773 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10774
10775 if (dm_new_crtc_state && dm_new_crtc_state->stream == NULL &&
10776 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
10777 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
10778 new_con_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
10779 dm_new_con_state->update_hdcp = true;
10780 continue;
10781 }
10782
10783 if (is_content_protection_different(new_crtc_state, old_crtc_state, new_con_state,
10784 old_con_state, connector, adev->dm.hdcp_workqueue)) {
10785 /* when display is unplugged from mst hub, connctor will
10786 * be destroyed within dm_dp_mst_connector_destroy. connector
10787 * hdcp perperties, like type, undesired, desired, enabled,
10788 * will be lost. So, save hdcp properties into hdcp_work within
10789 * amdgpu_dm_atomic_commit_tail. if the same display is
10790 * plugged back with same display index, its hdcp properties
10791 * will be retrieved from hdcp_work within dm_dp_mst_get_modes
10792 */
10793
10794 bool enable_encryption = false;
10795
10796 if (new_con_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED)
10797 enable_encryption = true;
10798
10799 if (aconnector->dc_link && aconnector->dc_sink &&
10800 aconnector->dc_link->type == dc_connection_mst_branch) {
10801 struct hdcp_workqueue *hdcp_work = adev->dm.hdcp_workqueue;
10802 struct hdcp_workqueue *hdcp_w =
10803 &hdcp_work[aconnector->dc_link->link_index];
10804
10805 hdcp_w->hdcp_content_type[connector->index] =
10806 new_con_state->hdcp_content_type;
10807 hdcp_w->content_protection[connector->index] =
10808 new_con_state->content_protection;
10809 }
10810
10811 if (new_crtc_state && new_crtc_state->mode_changed &&
10812 new_con_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED)
10813 enable_encryption = true;
10814
10815 drm_info(dev, "[HDCP_DM] hdcp_update_display enable_encryption = %x\n", enable_encryption);
10816
10817 if (aconnector->dc_link)
10818 hdcp_update_display(
10819 adev->dm.hdcp_workqueue, aconnector->dc_link->link_index, aconnector,
10820 new_con_state->hdcp_content_type, enable_encryption);
10821 }
10822 }
10823 }
10824
amdgpu_dm_atomic_setup_commit(struct drm_atomic_state * state)10825 static int amdgpu_dm_atomic_setup_commit(struct drm_atomic_state *state)
10826 {
10827 struct drm_crtc *crtc;
10828 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10829 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10830 int i, ret;
10831
10832 ret = drm_dp_mst_atomic_setup_commit(state);
10833 if (ret)
10834 return ret;
10835
10836 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10837 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10838 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10839 /*
10840 * Color management settings. We also update color properties
10841 * when a modeset is needed, to ensure it gets reprogrammed.
10842 */
10843 if (dm_new_crtc_state->base.active && dm_new_crtc_state->stream &&
10844 (dm_new_crtc_state->base.color_mgmt_changed ||
10845 dm_old_crtc_state->regamma_tf != dm_new_crtc_state->regamma_tf ||
10846 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
10847 ret = amdgpu_dm_update_crtc_color_mgmt(dm_new_crtc_state);
10848 if (ret) {
10849 drm_dbg_atomic(state->dev, "Failed to update color state\n");
10850 return ret;
10851 }
10852 }
10853 }
10854
10855 return 0;
10856 }
10857
10858 /**
10859 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation.
10860 * @state: The atomic state to commit
10861 *
10862 * This will tell DC to commit the constructed DC state from atomic_check,
10863 * programming the hardware. Any failures here implies a hardware failure, since
10864 * atomic check should have filtered anything non-kosher.
10865 */
amdgpu_dm_atomic_commit_tail(struct drm_atomic_state * state)10866 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
10867 {
10868 struct drm_device *dev = state->dev;
10869 struct amdgpu_device *adev = drm_to_adev(dev);
10870 struct amdgpu_display_manager *dm = &adev->dm;
10871 struct dm_atomic_state *dm_state;
10872 struct dc_state *dc_state = NULL;
10873 u32 i, j;
10874 struct drm_crtc *crtc;
10875 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10876 unsigned long flags;
10877 bool wait_for_vblank = true;
10878 struct drm_connector *connector;
10879 struct drm_connector_state *old_con_state = NULL, *new_con_state = NULL;
10880 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10881 int crtc_disable_count = 0;
10882
10883 trace_amdgpu_dm_atomic_commit_tail_begin(state);
10884
10885 drm_atomic_helper_update_legacy_modeset_state(dev, state);
10886 drm_dp_mst_atomic_wait_for_dependencies(state);
10887
10888 dm_state = dm_atomic_get_new_state(state);
10889 if (dm_state && dm_state->context) {
10890 dc_state = dm_state->context;
10891 amdgpu_dm_commit_streams(state, dc_state);
10892 }
10893
10894 amdgpu_dm_update_hdcp(state);
10895
10896 /* Handle connector state changes */
10897 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10898 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
10899 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
10900 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
10901 struct dc_surface_update *dummy_updates;
10902 struct dc_stream_update stream_update;
10903 struct dc_info_packet hdr_packet;
10904 struct dc_stream_status *status = NULL;
10905 bool abm_changed, hdr_changed, scaling_changed, output_color_space_changed = false;
10906
10907 memset(&stream_update, 0, sizeof(stream_update));
10908
10909 if (acrtc) {
10910 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
10911 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
10912 }
10913
10914 /* Skip any modesets/resets */
10915 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
10916 continue;
10917
10918 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10919 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10920
10921 scaling_changed = is_scaling_state_different(dm_new_con_state,
10922 dm_old_con_state);
10923
10924 if ((new_con_state->hdmi.broadcast_rgb != old_con_state->hdmi.broadcast_rgb) &&
10925 (dm_old_crtc_state->stream->output_color_space !=
10926 get_output_color_space(&dm_new_crtc_state->stream->timing, new_con_state)))
10927 output_color_space_changed = true;
10928
10929 abm_changed = dm_new_crtc_state->abm_level !=
10930 dm_old_crtc_state->abm_level;
10931
10932 hdr_changed =
10933 !drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state);
10934
10935 if (!scaling_changed && !abm_changed && !hdr_changed && !output_color_space_changed)
10936 continue;
10937
10938 stream_update.stream = dm_new_crtc_state->stream;
10939 if (scaling_changed) {
10940 update_stream_scaling_settings(dev, &dm_new_con_state->base.crtc->mode,
10941 dm_new_con_state, dm_new_crtc_state->stream);
10942
10943 stream_update.src = dm_new_crtc_state->stream->src;
10944 stream_update.dst = dm_new_crtc_state->stream->dst;
10945 }
10946
10947 if (output_color_space_changed) {
10948 dm_new_crtc_state->stream->output_color_space
10949 = get_output_color_space(&dm_new_crtc_state->stream->timing, new_con_state);
10950
10951 stream_update.output_color_space = &dm_new_crtc_state->stream->output_color_space;
10952 }
10953
10954 if (abm_changed) {
10955 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level;
10956
10957 stream_update.abm_level = &dm_new_crtc_state->abm_level;
10958 }
10959
10960 if (hdr_changed) {
10961 fill_hdr_info_packet(new_con_state, &hdr_packet);
10962 stream_update.hdr_static_metadata = &hdr_packet;
10963 }
10964
10965 status = dc_stream_get_status(dm_new_crtc_state->stream);
10966
10967 if (WARN_ON(!status))
10968 continue;
10969
10970 WARN_ON(!status->plane_count);
10971
10972 /*
10973 * TODO: DC refuses to perform stream updates without a dc_surface_update.
10974 * Here we create an empty update on each plane.
10975 * To fix this, DC should permit updating only stream properties.
10976 */
10977 dummy_updates = kzalloc(sizeof(struct dc_surface_update) * MAX_SURFACES, GFP_KERNEL);
10978 if (!dummy_updates) {
10979 drm_err(adev_to_drm(adev), "Failed to allocate memory for dummy_updates.\n");
10980 continue;
10981 }
10982 for (j = 0; j < status->plane_count; j++)
10983 dummy_updates[j].surface = status->plane_states[j];
10984
10985 sort(dummy_updates, status->plane_count,
10986 sizeof(*dummy_updates), dm_plane_layer_index_cmp, NULL);
10987
10988 mutex_lock(&dm->dc_lock);
10989 dc_exit_ips_for_hw_access(dm->dc);
10990 dc_update_planes_and_stream(dm->dc,
10991 dummy_updates,
10992 status->plane_count,
10993 dm_new_crtc_state->stream,
10994 &stream_update);
10995 mutex_unlock(&dm->dc_lock);
10996 kfree(dummy_updates);
10997
10998 drm_connector_update_privacy_screen(new_con_state);
10999 }
11000
11001 /**
11002 * Enable interrupts for CRTCs that are newly enabled or went through
11003 * a modeset. It was intentionally deferred until after the front end
11004 * state was modified to wait until the OTG was on and so the IRQ
11005 * handlers didn't access stale or invalid state.
11006 */
11007 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
11008 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
11009 #ifdef CONFIG_DEBUG_FS
11010 enum amdgpu_dm_pipe_crc_source cur_crc_src;
11011 #endif
11012 /* Count number of newly disabled CRTCs for dropping PM refs later. */
11013 if (old_crtc_state->active && !new_crtc_state->active)
11014 crtc_disable_count++;
11015
11016 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11017 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
11018
11019 /* For freesync config update on crtc state and params for irq */
11020 update_stream_irq_parameters(dm, dm_new_crtc_state);
11021
11022 #ifdef CONFIG_DEBUG_FS
11023 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
11024 cur_crc_src = acrtc->dm_irq_params.crc_src;
11025 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
11026 #endif
11027
11028 if (new_crtc_state->active &&
11029 (!old_crtc_state->active ||
11030 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
11031 dc_stream_retain(dm_new_crtc_state->stream);
11032 acrtc->dm_irq_params.stream = dm_new_crtc_state->stream;
11033 manage_dm_interrupts(adev, acrtc, dm_new_crtc_state);
11034 }
11035 /* Handle vrr on->off / off->on transitions */
11036 amdgpu_dm_handle_vrr_transition(dm_old_crtc_state, dm_new_crtc_state);
11037
11038 #ifdef CONFIG_DEBUG_FS
11039 if (new_crtc_state->active &&
11040 (!old_crtc_state->active ||
11041 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
11042 /**
11043 * Frontend may have changed so reapply the CRC capture
11044 * settings for the stream.
11045 */
11046 if (amdgpu_dm_is_valid_crc_source(cur_crc_src)) {
11047 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
11048 if (amdgpu_dm_crc_window_is_activated(crtc)) {
11049 uint8_t cnt;
11050
11051 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
11052 for (cnt = 0; cnt < MAX_CRC_WINDOW_NUM; cnt++) {
11053 if (acrtc->dm_irq_params.window_param[cnt].enable) {
11054 acrtc->dm_irq_params.window_param[cnt].update_win = true;
11055
11056 /**
11057 * It takes 2 frames for HW to stably generate CRC when
11058 * resuming from suspend, so we set skip_frame_cnt 2.
11059 */
11060 acrtc->dm_irq_params.window_param[cnt].skip_frame_cnt = 2;
11061 }
11062 }
11063 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
11064 }
11065 #endif
11066 if (amdgpu_dm_crtc_configure_crc_source(
11067 crtc, dm_new_crtc_state, cur_crc_src))
11068 drm_dbg_atomic(dev, "Failed to configure crc source");
11069 }
11070 }
11071 #endif
11072 }
11073
11074 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j)
11075 if (new_crtc_state->async_flip)
11076 wait_for_vblank = false;
11077
11078 /* update planes when needed per crtc*/
11079 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
11080 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11081
11082 if (dm_new_crtc_state->stream)
11083 amdgpu_dm_commit_planes(state, dev, dm, crtc, wait_for_vblank);
11084 }
11085
11086 /* Enable writeback */
11087 for_each_new_connector_in_state(state, connector, new_con_state, i) {
11088 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
11089 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
11090
11091 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
11092 continue;
11093
11094 if (!new_con_state->writeback_job)
11095 continue;
11096
11097 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
11098
11099 if (!new_crtc_state)
11100 continue;
11101
11102 if (acrtc->wb_enabled)
11103 continue;
11104
11105 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11106
11107 dm_set_writeback(dm, dm_new_crtc_state, connector, new_con_state);
11108 acrtc->wb_enabled = true;
11109 }
11110
11111 /* Update audio instances for each connector. */
11112 amdgpu_dm_commit_audio(dev, state);
11113
11114 /* restore the backlight level */
11115 for (i = 0; i < dm->num_of_edps; i++) {
11116 if (dm->backlight_dev[i] &&
11117 (dm->actual_brightness[i] != dm->brightness[i]))
11118 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
11119 }
11120
11121 /*
11122 * send vblank event on all events not handled in flip and
11123 * mark consumed event for drm_atomic_helper_commit_hw_done
11124 */
11125 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
11126 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
11127
11128 if (new_crtc_state->event)
11129 drm_send_event_locked(dev, &new_crtc_state->event->base);
11130
11131 new_crtc_state->event = NULL;
11132 }
11133 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
11134
11135 /* Signal HW programming completion */
11136 drm_atomic_helper_commit_hw_done(state);
11137
11138 if (wait_for_vblank)
11139 drm_atomic_helper_wait_for_flip_done(dev, state);
11140
11141 drm_atomic_helper_cleanup_planes(dev, state);
11142
11143 /* Don't free the memory if we are hitting this as part of suspend.
11144 * This way we don't free any memory during suspend; see
11145 * amdgpu_bo_free_kernel(). The memory will be freed in the first
11146 * non-suspend modeset or when the driver is torn down.
11147 */
11148 if (!adev->in_suspend) {
11149 /* return the stolen vga memory back to VRAM */
11150 if (!adev->mman.keep_stolen_vga_memory)
11151 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
11152 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL);
11153 }
11154
11155 /*
11156 * Finally, drop a runtime PM reference for each newly disabled CRTC,
11157 * so we can put the GPU into runtime suspend if we're not driving any
11158 * displays anymore
11159 */
11160 for (i = 0; i < crtc_disable_count; i++)
11161 pm_runtime_put_autosuspend(dev->dev);
11162 pm_runtime_mark_last_busy(dev->dev);
11163
11164 trace_amdgpu_dm_atomic_commit_tail_finish(state);
11165 }
11166
dm_force_atomic_commit(struct drm_connector * connector)11167 static int dm_force_atomic_commit(struct drm_connector *connector)
11168 {
11169 int ret = 0;
11170 struct drm_device *ddev = connector->dev;
11171 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
11172 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
11173 struct drm_plane *plane = disconnected_acrtc->base.primary;
11174 struct drm_connector_state *conn_state;
11175 struct drm_crtc_state *crtc_state;
11176 struct drm_plane_state *plane_state;
11177
11178 if (!state)
11179 return -ENOMEM;
11180
11181 state->acquire_ctx = ddev->mode_config.acquire_ctx;
11182
11183 /* Construct an atomic state to restore previous display setting */
11184
11185 /*
11186 * Attach connectors to drm_atomic_state
11187 */
11188 conn_state = drm_atomic_get_connector_state(state, connector);
11189
11190 /* Check for error in getting connector state */
11191 if (IS_ERR(conn_state)) {
11192 ret = PTR_ERR(conn_state);
11193 goto out;
11194 }
11195
11196 /* Attach crtc to drm_atomic_state*/
11197 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
11198
11199 /* Check for error in getting crtc state */
11200 if (IS_ERR(crtc_state)) {
11201 ret = PTR_ERR(crtc_state);
11202 goto out;
11203 }
11204
11205 /* force a restore */
11206 crtc_state->mode_changed = true;
11207
11208 /* Attach plane to drm_atomic_state */
11209 plane_state = drm_atomic_get_plane_state(state, plane);
11210
11211 /* Check for error in getting plane state */
11212 if (IS_ERR(plane_state)) {
11213 ret = PTR_ERR(plane_state);
11214 goto out;
11215 }
11216
11217 /* Call commit internally with the state we just constructed */
11218 ret = drm_atomic_commit(state);
11219
11220 out:
11221 drm_atomic_state_put(state);
11222 if (ret)
11223 drm_err(ddev, "Restoring old state failed with %i\n", ret);
11224
11225 return ret;
11226 }
11227
11228 /*
11229 * This function handles all cases when set mode does not come upon hotplug.
11230 * This includes when a display is unplugged then plugged back into the
11231 * same port and when running without usermode desktop manager supprot
11232 */
dm_restore_drm_connector_state(struct drm_device * dev,struct drm_connector * connector)11233 void dm_restore_drm_connector_state(struct drm_device *dev,
11234 struct drm_connector *connector)
11235 {
11236 struct amdgpu_dm_connector *aconnector;
11237 struct amdgpu_crtc *disconnected_acrtc;
11238 struct dm_crtc_state *acrtc_state;
11239
11240 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
11241 return;
11242
11243 aconnector = to_amdgpu_dm_connector(connector);
11244
11245 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
11246 return;
11247
11248 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
11249 if (!disconnected_acrtc)
11250 return;
11251
11252 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
11253 if (!acrtc_state->stream)
11254 return;
11255
11256 /*
11257 * If the previous sink is not released and different from the current,
11258 * we deduce we are in a state where we can not rely on usermode call
11259 * to turn on the display, so we do it here
11260 */
11261 if (acrtc_state->stream->sink != aconnector->dc_sink)
11262 dm_force_atomic_commit(&aconnector->base);
11263 }
11264
11265 /*
11266 * Grabs all modesetting locks to serialize against any blocking commits,
11267 * Waits for completion of all non blocking commits.
11268 */
do_aquire_global_lock(struct drm_device * dev,struct drm_atomic_state * state)11269 static int do_aquire_global_lock(struct drm_device *dev,
11270 struct drm_atomic_state *state)
11271 {
11272 struct drm_crtc *crtc;
11273 struct drm_crtc_commit *commit;
11274 long ret;
11275
11276 /*
11277 * Adding all modeset locks to aquire_ctx will
11278 * ensure that when the framework release it the
11279 * extra locks we are locking here will get released to
11280 */
11281 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
11282 if (ret)
11283 return ret;
11284
11285 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11286 spin_lock(&crtc->commit_lock);
11287 commit = list_first_entry_or_null(&crtc->commit_list,
11288 struct drm_crtc_commit, commit_entry);
11289 if (commit)
11290 drm_crtc_commit_get(commit);
11291 spin_unlock(&crtc->commit_lock);
11292
11293 if (!commit)
11294 continue;
11295
11296 /*
11297 * Make sure all pending HW programming completed and
11298 * page flips done
11299 */
11300 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
11301
11302 if (ret > 0)
11303 ret = wait_for_completion_interruptible_timeout(
11304 &commit->flip_done, 10*HZ);
11305
11306 if (ret == 0)
11307 drm_err(dev, "[CRTC:%d:%s] hw_done or flip_done timed out\n",
11308 crtc->base.id, crtc->name);
11309
11310 drm_crtc_commit_put(commit);
11311 }
11312
11313 return ret < 0 ? ret : 0;
11314 }
11315
get_freesync_config_for_crtc(struct dm_crtc_state * new_crtc_state,struct dm_connector_state * new_con_state)11316 static void get_freesync_config_for_crtc(
11317 struct dm_crtc_state *new_crtc_state,
11318 struct dm_connector_state *new_con_state)
11319 {
11320 struct mod_freesync_config config = {0};
11321 struct amdgpu_dm_connector *aconnector;
11322 struct drm_display_mode *mode = &new_crtc_state->base.mode;
11323 int vrefresh = drm_mode_vrefresh(mode);
11324 bool fs_vid_mode = false;
11325
11326 if (new_con_state->base.connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
11327 return;
11328
11329 aconnector = to_amdgpu_dm_connector(new_con_state->base.connector);
11330
11331 new_crtc_state->vrr_supported = new_con_state->freesync_capable &&
11332 vrefresh >= aconnector->min_vfreq &&
11333 vrefresh <= aconnector->max_vfreq;
11334
11335 if (new_crtc_state->vrr_supported) {
11336 new_crtc_state->stream->ignore_msa_timing_param = true;
11337 fs_vid_mode = new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
11338
11339 config.min_refresh_in_uhz = aconnector->min_vfreq * 1000000;
11340 config.max_refresh_in_uhz = aconnector->max_vfreq * 1000000;
11341 config.vsif_supported = true;
11342 config.btr = true;
11343
11344 if (fs_vid_mode) {
11345 config.state = VRR_STATE_ACTIVE_FIXED;
11346 config.fixed_refresh_in_uhz = new_crtc_state->freesync_config.fixed_refresh_in_uhz;
11347 goto out;
11348 } else if (new_crtc_state->base.vrr_enabled) {
11349 config.state = VRR_STATE_ACTIVE_VARIABLE;
11350 } else {
11351 config.state = VRR_STATE_INACTIVE;
11352 }
11353 } else {
11354 config.state = VRR_STATE_UNSUPPORTED;
11355 }
11356 out:
11357 new_crtc_state->freesync_config = config;
11358 }
11359
reset_freesync_config_for_crtc(struct dm_crtc_state * new_crtc_state)11360 static void reset_freesync_config_for_crtc(
11361 struct dm_crtc_state *new_crtc_state)
11362 {
11363 new_crtc_state->vrr_supported = false;
11364
11365 memset(&new_crtc_state->vrr_infopacket, 0,
11366 sizeof(new_crtc_state->vrr_infopacket));
11367 }
11368
11369 static bool
is_timing_unchanged_for_freesync(struct drm_crtc_state * old_crtc_state,struct drm_crtc_state * new_crtc_state)11370 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
11371 struct drm_crtc_state *new_crtc_state)
11372 {
11373 const struct drm_display_mode *old_mode, *new_mode;
11374
11375 if (!old_crtc_state || !new_crtc_state)
11376 return false;
11377
11378 old_mode = &old_crtc_state->mode;
11379 new_mode = &new_crtc_state->mode;
11380
11381 if (old_mode->clock == new_mode->clock &&
11382 old_mode->hdisplay == new_mode->hdisplay &&
11383 old_mode->vdisplay == new_mode->vdisplay &&
11384 old_mode->htotal == new_mode->htotal &&
11385 old_mode->vtotal != new_mode->vtotal &&
11386 old_mode->hsync_start == new_mode->hsync_start &&
11387 old_mode->vsync_start != new_mode->vsync_start &&
11388 old_mode->hsync_end == new_mode->hsync_end &&
11389 old_mode->vsync_end != new_mode->vsync_end &&
11390 old_mode->hskew == new_mode->hskew &&
11391 old_mode->vscan == new_mode->vscan &&
11392 (old_mode->vsync_end - old_mode->vsync_start) ==
11393 (new_mode->vsync_end - new_mode->vsync_start))
11394 return true;
11395
11396 return false;
11397 }
11398
set_freesync_fixed_config(struct dm_crtc_state * dm_new_crtc_state)11399 static void set_freesync_fixed_config(struct dm_crtc_state *dm_new_crtc_state)
11400 {
11401 u64 num, den, res;
11402 struct drm_crtc_state *new_crtc_state = &dm_new_crtc_state->base;
11403
11404 dm_new_crtc_state->freesync_config.state = VRR_STATE_ACTIVE_FIXED;
11405
11406 num = (unsigned long long)new_crtc_state->mode.clock * 1000 * 1000000;
11407 den = (unsigned long long)new_crtc_state->mode.htotal *
11408 (unsigned long long)new_crtc_state->mode.vtotal;
11409
11410 res = div_u64(num, den);
11411 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = res;
11412 }
11413
dm_update_crtc_state(struct amdgpu_display_manager * dm,struct drm_atomic_state * state,struct drm_crtc * crtc,struct drm_crtc_state * old_crtc_state,struct drm_crtc_state * new_crtc_state,bool enable,bool * lock_and_validation_needed)11414 static int dm_update_crtc_state(struct amdgpu_display_manager *dm,
11415 struct drm_atomic_state *state,
11416 struct drm_crtc *crtc,
11417 struct drm_crtc_state *old_crtc_state,
11418 struct drm_crtc_state *new_crtc_state,
11419 bool enable,
11420 bool *lock_and_validation_needed)
11421 {
11422 struct dm_atomic_state *dm_state = NULL;
11423 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
11424 struct dc_stream_state *new_stream;
11425 struct amdgpu_device *adev = dm->adev;
11426 int ret = 0;
11427
11428 /*
11429 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
11430 * update changed items
11431 */
11432 struct amdgpu_crtc *acrtc = NULL;
11433 struct drm_connector *connector = NULL;
11434 struct amdgpu_dm_connector *aconnector = NULL;
11435 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
11436 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
11437
11438 new_stream = NULL;
11439
11440 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
11441 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11442 acrtc = to_amdgpu_crtc(crtc);
11443 connector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
11444 if (connector)
11445 aconnector = to_amdgpu_dm_connector(connector);
11446
11447 /* TODO This hack should go away */
11448 if (connector && enable) {
11449 /* Make sure fake sink is created in plug-in scenario */
11450 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
11451 connector);
11452 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
11453 connector);
11454
11455 if (WARN_ON(!drm_new_conn_state)) {
11456 ret = -EINVAL;
11457 goto fail;
11458 }
11459
11460 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
11461 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
11462
11463 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
11464 goto skip_modeset;
11465
11466 new_stream = create_validate_stream_for_sink(connector,
11467 &new_crtc_state->mode,
11468 dm_new_conn_state,
11469 dm_old_crtc_state->stream);
11470
11471 /*
11472 * we can have no stream on ACTION_SET if a display
11473 * was disconnected during S3, in this case it is not an
11474 * error, the OS will be updated after detection, and
11475 * will do the right thing on next atomic commit
11476 */
11477
11478 if (!new_stream) {
11479 drm_dbg_driver(adev_to_drm(adev), "%s: Failed to create new stream for crtc %d\n",
11480 __func__, acrtc->base.base.id);
11481 ret = -ENOMEM;
11482 goto fail;
11483 }
11484
11485 /*
11486 * TODO: Check VSDB bits to decide whether this should
11487 * be enabled or not.
11488 */
11489 new_stream->triggered_crtc_reset.enabled =
11490 dm->force_timing_sync;
11491
11492 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
11493
11494 ret = fill_hdr_info_packet(drm_new_conn_state,
11495 &new_stream->hdr_static_metadata);
11496 if (ret)
11497 goto fail;
11498
11499 /*
11500 * If we already removed the old stream from the context
11501 * (and set the new stream to NULL) then we can't reuse
11502 * the old stream even if the stream and scaling are unchanged.
11503 * We'll hit the BUG_ON and black screen.
11504 *
11505 * TODO: Refactor this function to allow this check to work
11506 * in all conditions.
11507 */
11508 if (amdgpu_freesync_vid_mode &&
11509 dm_new_crtc_state->stream &&
11510 is_timing_unchanged_for_freesync(new_crtc_state, old_crtc_state))
11511 goto skip_modeset;
11512
11513 if (dm_new_crtc_state->stream &&
11514 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
11515 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
11516 new_crtc_state->mode_changed = false;
11517 drm_dbg_driver(adev_to_drm(adev), "Mode change not required, setting mode_changed to %d",
11518 new_crtc_state->mode_changed);
11519 }
11520 }
11521
11522 /* mode_changed flag may get updated above, need to check again */
11523 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
11524 goto skip_modeset;
11525
11526 drm_dbg_state(state->dev,
11527 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n",
11528 acrtc->crtc_id,
11529 new_crtc_state->enable,
11530 new_crtc_state->active,
11531 new_crtc_state->planes_changed,
11532 new_crtc_state->mode_changed,
11533 new_crtc_state->active_changed,
11534 new_crtc_state->connectors_changed);
11535
11536 /* Remove stream for any changed/disabled CRTC */
11537 if (!enable) {
11538
11539 if (!dm_old_crtc_state->stream)
11540 goto skip_modeset;
11541
11542 /* Unset freesync video if it was active before */
11543 if (dm_old_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED) {
11544 dm_new_crtc_state->freesync_config.state = VRR_STATE_INACTIVE;
11545 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = 0;
11546 }
11547
11548 /* Now check if we should set freesync video mode */
11549 if (amdgpu_freesync_vid_mode && dm_new_crtc_state->stream &&
11550 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
11551 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream) &&
11552 is_timing_unchanged_for_freesync(new_crtc_state,
11553 old_crtc_state)) {
11554 new_crtc_state->mode_changed = false;
11555 drm_dbg_driver(adev_to_drm(adev),
11556 "Mode change not required for front porch change, setting mode_changed to %d",
11557 new_crtc_state->mode_changed);
11558
11559 set_freesync_fixed_config(dm_new_crtc_state);
11560
11561 goto skip_modeset;
11562 } else if (amdgpu_freesync_vid_mode && aconnector &&
11563 is_freesync_video_mode(&new_crtc_state->mode,
11564 aconnector)) {
11565 struct drm_display_mode *high_mode;
11566
11567 high_mode = get_highest_refresh_rate_mode(aconnector, false);
11568 if (!drm_mode_equal(&new_crtc_state->mode, high_mode))
11569 set_freesync_fixed_config(dm_new_crtc_state);
11570 }
11571
11572 ret = dm_atomic_get_state(state, &dm_state);
11573 if (ret)
11574 goto fail;
11575
11576 drm_dbg_driver(adev_to_drm(adev), "Disabling DRM crtc: %d\n",
11577 crtc->base.id);
11578
11579 /* i.e. reset mode */
11580 if (dc_state_remove_stream(
11581 dm->dc,
11582 dm_state->context,
11583 dm_old_crtc_state->stream) != DC_OK) {
11584 ret = -EINVAL;
11585 goto fail;
11586 }
11587
11588 dc_stream_release(dm_old_crtc_state->stream);
11589 dm_new_crtc_state->stream = NULL;
11590
11591 reset_freesync_config_for_crtc(dm_new_crtc_state);
11592
11593 *lock_and_validation_needed = true;
11594
11595 } else {/* Add stream for any updated/enabled CRTC */
11596 /*
11597 * Quick fix to prevent NULL pointer on new_stream when
11598 * added MST connectors not found in existing crtc_state in the chained mode
11599 * TODO: need to dig out the root cause of that
11600 */
11601 if (!connector)
11602 goto skip_modeset;
11603
11604 if (modereset_required(new_crtc_state))
11605 goto skip_modeset;
11606
11607 if (amdgpu_dm_crtc_modeset_required(new_crtc_state, new_stream,
11608 dm_old_crtc_state->stream)) {
11609
11610 WARN_ON(dm_new_crtc_state->stream);
11611
11612 ret = dm_atomic_get_state(state, &dm_state);
11613 if (ret)
11614 goto fail;
11615
11616 dm_new_crtc_state->stream = new_stream;
11617
11618 dc_stream_retain(new_stream);
11619
11620 drm_dbg_atomic(adev_to_drm(adev), "Enabling DRM crtc: %d\n",
11621 crtc->base.id);
11622
11623 if (dc_state_add_stream(
11624 dm->dc,
11625 dm_state->context,
11626 dm_new_crtc_state->stream) != DC_OK) {
11627 ret = -EINVAL;
11628 goto fail;
11629 }
11630
11631 *lock_and_validation_needed = true;
11632 }
11633 }
11634
11635 skip_modeset:
11636 /* Release extra reference */
11637 if (new_stream)
11638 dc_stream_release(new_stream);
11639
11640 /*
11641 * We want to do dc stream updates that do not require a
11642 * full modeset below.
11643 */
11644 if (!(enable && connector && new_crtc_state->active))
11645 return 0;
11646 /*
11647 * Given above conditions, the dc state cannot be NULL because:
11648 * 1. We're in the process of enabling CRTCs (just been added
11649 * to the dc context, or already is on the context)
11650 * 2. Has a valid connector attached, and
11651 * 3. Is currently active and enabled.
11652 * => The dc stream state currently exists.
11653 */
11654 BUG_ON(dm_new_crtc_state->stream == NULL);
11655
11656 /* Scaling or underscan settings */
11657 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state) ||
11658 drm_atomic_crtc_needs_modeset(new_crtc_state))
11659 update_stream_scaling_settings(adev_to_drm(adev),
11660 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
11661
11662 /* ABM settings */
11663 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
11664
11665 /*
11666 * Color management settings. We also update color properties
11667 * when a modeset is needed, to ensure it gets reprogrammed.
11668 */
11669 if (dm_new_crtc_state->base.color_mgmt_changed ||
11670 dm_old_crtc_state->regamma_tf != dm_new_crtc_state->regamma_tf ||
11671 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
11672 ret = amdgpu_dm_check_crtc_color_mgmt(dm_new_crtc_state, true);
11673 if (ret)
11674 goto fail;
11675 }
11676
11677 /* Update Freesync settings. */
11678 get_freesync_config_for_crtc(dm_new_crtc_state,
11679 dm_new_conn_state);
11680
11681 return ret;
11682
11683 fail:
11684 if (new_stream)
11685 dc_stream_release(new_stream);
11686 return ret;
11687 }
11688
should_reset_plane(struct drm_atomic_state * state,struct drm_plane * plane,struct drm_plane_state * old_plane_state,struct drm_plane_state * new_plane_state)11689 static bool should_reset_plane(struct drm_atomic_state *state,
11690 struct drm_plane *plane,
11691 struct drm_plane_state *old_plane_state,
11692 struct drm_plane_state *new_plane_state)
11693 {
11694 struct drm_plane *other;
11695 struct drm_plane_state *old_other_state, *new_other_state;
11696 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
11697 struct dm_crtc_state *old_dm_crtc_state, *new_dm_crtc_state;
11698 struct amdgpu_device *adev = drm_to_adev(plane->dev);
11699 struct drm_connector_state *new_con_state;
11700 struct drm_connector *connector;
11701 int i;
11702
11703 /*
11704 * TODO: Remove this hack for all asics once it proves that the
11705 * fast updates works fine on DCN3.2+.
11706 */
11707 if (amdgpu_ip_version(adev, DCE_HWIP, 0) < IP_VERSION(3, 2, 0) &&
11708 state->allow_modeset)
11709 return true;
11710
11711 /* Check for writeback commit */
11712 for_each_new_connector_in_state(state, connector, new_con_state, i) {
11713 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
11714 continue;
11715
11716 if (new_con_state->writeback_job)
11717 return true;
11718 }
11719
11720 if (amdgpu_in_reset(adev) && state->allow_modeset)
11721 return true;
11722
11723 /* Exit early if we know that we're adding or removing the plane. */
11724 if (old_plane_state->crtc != new_plane_state->crtc)
11725 return true;
11726
11727 /* old crtc == new_crtc == NULL, plane not in context. */
11728 if (!new_plane_state->crtc)
11729 return false;
11730
11731 new_crtc_state =
11732 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
11733 old_crtc_state =
11734 drm_atomic_get_old_crtc_state(state, old_plane_state->crtc);
11735
11736 if (!new_crtc_state)
11737 return true;
11738
11739 /*
11740 * A change in cursor mode means a new dc pipe needs to be acquired or
11741 * released from the state
11742 */
11743 old_dm_crtc_state = to_dm_crtc_state(old_crtc_state);
11744 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
11745 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
11746 old_dm_crtc_state != NULL &&
11747 old_dm_crtc_state->cursor_mode != new_dm_crtc_state->cursor_mode) {
11748 return true;
11749 }
11750
11751 /* CRTC Degamma changes currently require us to recreate planes. */
11752 if (new_crtc_state->color_mgmt_changed)
11753 return true;
11754
11755 /*
11756 * On zpos change, planes need to be reordered by removing and re-adding
11757 * them one by one to the dc state, in order of descending zpos.
11758 *
11759 * TODO: We can likely skip bandwidth validation if the only thing that
11760 * changed about the plane was it'z z-ordering.
11761 */
11762 if (old_plane_state->normalized_zpos != new_plane_state->normalized_zpos)
11763 return true;
11764
11765 if (drm_atomic_crtc_needs_modeset(new_crtc_state))
11766 return true;
11767
11768 /*
11769 * If there are any new primary or overlay planes being added or
11770 * removed then the z-order can potentially change. To ensure
11771 * correct z-order and pipe acquisition the current DC architecture
11772 * requires us to remove and recreate all existing planes.
11773 *
11774 * TODO: Come up with a more elegant solution for this.
11775 */
11776 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) {
11777 struct amdgpu_framebuffer *old_afb, *new_afb;
11778 struct dm_plane_state *dm_new_other_state, *dm_old_other_state;
11779
11780 dm_new_other_state = to_dm_plane_state(new_other_state);
11781 dm_old_other_state = to_dm_plane_state(old_other_state);
11782
11783 if (other->type == DRM_PLANE_TYPE_CURSOR)
11784 continue;
11785
11786 if (old_other_state->crtc != new_plane_state->crtc &&
11787 new_other_state->crtc != new_plane_state->crtc)
11788 continue;
11789
11790 if (old_other_state->crtc != new_other_state->crtc)
11791 return true;
11792
11793 /* Src/dst size and scaling updates. */
11794 if (old_other_state->src_w != new_other_state->src_w ||
11795 old_other_state->src_h != new_other_state->src_h ||
11796 old_other_state->crtc_w != new_other_state->crtc_w ||
11797 old_other_state->crtc_h != new_other_state->crtc_h)
11798 return true;
11799
11800 /* Rotation / mirroring updates. */
11801 if (old_other_state->rotation != new_other_state->rotation)
11802 return true;
11803
11804 /* Blending updates. */
11805 if (old_other_state->pixel_blend_mode !=
11806 new_other_state->pixel_blend_mode)
11807 return true;
11808
11809 /* Alpha updates. */
11810 if (old_other_state->alpha != new_other_state->alpha)
11811 return true;
11812
11813 /* Colorspace changes. */
11814 if (old_other_state->color_range != new_other_state->color_range ||
11815 old_other_state->color_encoding != new_other_state->color_encoding)
11816 return true;
11817
11818 /* HDR/Transfer Function changes. */
11819 if (dm_old_other_state->degamma_tf != dm_new_other_state->degamma_tf ||
11820 dm_old_other_state->degamma_lut != dm_new_other_state->degamma_lut ||
11821 dm_old_other_state->hdr_mult != dm_new_other_state->hdr_mult ||
11822 dm_old_other_state->ctm != dm_new_other_state->ctm ||
11823 dm_old_other_state->shaper_lut != dm_new_other_state->shaper_lut ||
11824 dm_old_other_state->shaper_tf != dm_new_other_state->shaper_tf ||
11825 dm_old_other_state->lut3d != dm_new_other_state->lut3d ||
11826 dm_old_other_state->blend_lut != dm_new_other_state->blend_lut ||
11827 dm_old_other_state->blend_tf != dm_new_other_state->blend_tf)
11828 return true;
11829
11830 /* Framebuffer checks fall at the end. */
11831 if (!old_other_state->fb || !new_other_state->fb)
11832 continue;
11833
11834 /* Pixel format changes can require bandwidth updates. */
11835 if (old_other_state->fb->format != new_other_state->fb->format)
11836 return true;
11837
11838 old_afb = (struct amdgpu_framebuffer *)old_other_state->fb;
11839 new_afb = (struct amdgpu_framebuffer *)new_other_state->fb;
11840
11841 /* Tiling and DCC changes also require bandwidth updates. */
11842 if (old_afb->tiling_flags != new_afb->tiling_flags ||
11843 old_afb->base.modifier != new_afb->base.modifier)
11844 return true;
11845 }
11846
11847 return false;
11848 }
11849
dm_check_cursor_fb(struct amdgpu_crtc * new_acrtc,struct drm_plane_state * new_plane_state,struct drm_framebuffer * fb)11850 static int dm_check_cursor_fb(struct amdgpu_crtc *new_acrtc,
11851 struct drm_plane_state *new_plane_state,
11852 struct drm_framebuffer *fb)
11853 {
11854 struct amdgpu_device *adev = drm_to_adev(new_acrtc->base.dev);
11855 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
11856 unsigned int pitch;
11857 bool linear;
11858
11859 if (fb->width > new_acrtc->max_cursor_width ||
11860 fb->height > new_acrtc->max_cursor_height) {
11861 drm_dbg_atomic(adev_to_drm(adev), "Bad cursor FB size %dx%d\n",
11862 new_plane_state->fb->width,
11863 new_plane_state->fb->height);
11864 return -EINVAL;
11865 }
11866 if (new_plane_state->src_w != fb->width << 16 ||
11867 new_plane_state->src_h != fb->height << 16) {
11868 drm_dbg_atomic(adev_to_drm(adev), "Cropping not supported for cursor plane\n");
11869 return -EINVAL;
11870 }
11871
11872 /* Pitch in pixels */
11873 pitch = fb->pitches[0] / fb->format->cpp[0];
11874
11875 if (fb->width != pitch) {
11876 drm_dbg_atomic(adev_to_drm(adev), "Cursor FB width %d doesn't match pitch %d",
11877 fb->width, pitch);
11878 return -EINVAL;
11879 }
11880
11881 switch (pitch) {
11882 case 64:
11883 case 128:
11884 case 256:
11885 /* FB pitch is supported by cursor plane */
11886 break;
11887 default:
11888 drm_dbg_atomic(adev_to_drm(adev), "Bad cursor FB pitch %d px\n", pitch);
11889 return -EINVAL;
11890 }
11891
11892 /* Core DRM takes care of checking FB modifiers, so we only need to
11893 * check tiling flags when the FB doesn't have a modifier.
11894 */
11895 if (!(fb->flags & DRM_MODE_FB_MODIFIERS)) {
11896 if (adev->family == AMDGPU_FAMILY_GC_12_0_0) {
11897 linear = AMDGPU_TILING_GET(afb->tiling_flags, GFX12_SWIZZLE_MODE) == 0;
11898 } else if (adev->family >= AMDGPU_FAMILY_AI) {
11899 linear = AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE) == 0;
11900 } else {
11901 linear = AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_2D_TILED_THIN1 &&
11902 AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_1D_TILED_THIN1 &&
11903 AMDGPU_TILING_GET(afb->tiling_flags, MICRO_TILE_MODE) == 0;
11904 }
11905 if (!linear) {
11906 drm_dbg_atomic(adev_to_drm(adev), "Cursor FB not linear");
11907 return -EINVAL;
11908 }
11909 }
11910
11911 return 0;
11912 }
11913
11914 /*
11915 * Helper function for checking the cursor in native mode
11916 */
dm_check_native_cursor_state(struct drm_crtc * new_plane_crtc,struct drm_plane * plane,struct drm_plane_state * new_plane_state,bool enable)11917 static int dm_check_native_cursor_state(struct drm_crtc *new_plane_crtc,
11918 struct drm_plane *plane,
11919 struct drm_plane_state *new_plane_state,
11920 bool enable)
11921 {
11922
11923 struct amdgpu_crtc *new_acrtc;
11924 int ret;
11925
11926 if (!enable || !new_plane_crtc ||
11927 drm_atomic_plane_disabling(plane->state, new_plane_state))
11928 return 0;
11929
11930 new_acrtc = to_amdgpu_crtc(new_plane_crtc);
11931
11932 if (new_plane_state->src_x != 0 || new_plane_state->src_y != 0) {
11933 drm_dbg_atomic(new_plane_crtc->dev, "Cropping not supported for cursor plane\n");
11934 return -EINVAL;
11935 }
11936
11937 if (new_plane_state->fb) {
11938 ret = dm_check_cursor_fb(new_acrtc, new_plane_state,
11939 new_plane_state->fb);
11940 if (ret)
11941 return ret;
11942 }
11943
11944 return 0;
11945 }
11946
dm_should_update_native_cursor(struct drm_atomic_state * state,struct drm_crtc * old_plane_crtc,struct drm_crtc * new_plane_crtc,bool enable)11947 static bool dm_should_update_native_cursor(struct drm_atomic_state *state,
11948 struct drm_crtc *old_plane_crtc,
11949 struct drm_crtc *new_plane_crtc,
11950 bool enable)
11951 {
11952 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
11953 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
11954
11955 if (!enable) {
11956 if (old_plane_crtc == NULL)
11957 return true;
11958
11959 old_crtc_state = drm_atomic_get_old_crtc_state(
11960 state, old_plane_crtc);
11961 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
11962
11963 return dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE;
11964 } else {
11965 if (new_plane_crtc == NULL)
11966 return true;
11967
11968 new_crtc_state = drm_atomic_get_new_crtc_state(
11969 state, new_plane_crtc);
11970 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11971
11972 return dm_new_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE;
11973 }
11974 }
11975
dm_update_plane_state(struct dc * dc,struct drm_atomic_state * state,struct drm_plane * plane,struct drm_plane_state * old_plane_state,struct drm_plane_state * new_plane_state,bool enable,bool * lock_and_validation_needed,bool * is_top_most_overlay)11976 static int dm_update_plane_state(struct dc *dc,
11977 struct drm_atomic_state *state,
11978 struct drm_plane *plane,
11979 struct drm_plane_state *old_plane_state,
11980 struct drm_plane_state *new_plane_state,
11981 bool enable,
11982 bool *lock_and_validation_needed,
11983 bool *is_top_most_overlay)
11984 {
11985
11986 struct dm_atomic_state *dm_state = NULL;
11987 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
11988 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
11989 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
11990 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
11991 bool needs_reset, update_native_cursor;
11992 int ret = 0;
11993
11994
11995 new_plane_crtc = new_plane_state->crtc;
11996 old_plane_crtc = old_plane_state->crtc;
11997 dm_new_plane_state = to_dm_plane_state(new_plane_state);
11998 dm_old_plane_state = to_dm_plane_state(old_plane_state);
11999
12000 update_native_cursor = dm_should_update_native_cursor(state,
12001 old_plane_crtc,
12002 new_plane_crtc,
12003 enable);
12004
12005 if (plane->type == DRM_PLANE_TYPE_CURSOR && update_native_cursor) {
12006 ret = dm_check_native_cursor_state(new_plane_crtc, plane,
12007 new_plane_state, enable);
12008 if (ret)
12009 return ret;
12010
12011 return 0;
12012 }
12013
12014 needs_reset = should_reset_plane(state, plane, old_plane_state,
12015 new_plane_state);
12016
12017 /* Remove any changed/removed planes */
12018 if (!enable) {
12019 if (!needs_reset)
12020 return 0;
12021
12022 if (!old_plane_crtc)
12023 return 0;
12024
12025 old_crtc_state = drm_atomic_get_old_crtc_state(
12026 state, old_plane_crtc);
12027 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
12028
12029 if (!dm_old_crtc_state->stream)
12030 return 0;
12031
12032 drm_dbg_atomic(old_plane_crtc->dev, "Disabling DRM plane: %d on DRM crtc %d\n",
12033 plane->base.id, old_plane_crtc->base.id);
12034
12035 ret = dm_atomic_get_state(state, &dm_state);
12036 if (ret)
12037 return ret;
12038
12039 if (!dc_state_remove_plane(
12040 dc,
12041 dm_old_crtc_state->stream,
12042 dm_old_plane_state->dc_state,
12043 dm_state->context)) {
12044
12045 return -EINVAL;
12046 }
12047
12048 if (dm_old_plane_state->dc_state)
12049 dc_plane_state_release(dm_old_plane_state->dc_state);
12050
12051 dm_new_plane_state->dc_state = NULL;
12052
12053 *lock_and_validation_needed = true;
12054
12055 } else { /* Add new planes */
12056 struct dc_plane_state *dc_new_plane_state;
12057
12058 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
12059 return 0;
12060
12061 if (!new_plane_crtc)
12062 return 0;
12063
12064 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
12065 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12066
12067 if (!dm_new_crtc_state->stream)
12068 return 0;
12069
12070 if (!needs_reset)
12071 return 0;
12072
12073 ret = amdgpu_dm_plane_helper_check_state(new_plane_state, new_crtc_state);
12074 if (ret)
12075 goto out;
12076
12077 WARN_ON(dm_new_plane_state->dc_state);
12078
12079 dc_new_plane_state = dc_create_plane_state(dc);
12080 if (!dc_new_plane_state) {
12081 ret = -ENOMEM;
12082 goto out;
12083 }
12084
12085 drm_dbg_atomic(new_plane_crtc->dev, "Enabling DRM plane: %d on DRM crtc %d\n",
12086 plane->base.id, new_plane_crtc->base.id);
12087
12088 ret = fill_dc_plane_attributes(
12089 drm_to_adev(new_plane_crtc->dev),
12090 dc_new_plane_state,
12091 new_plane_state,
12092 new_crtc_state);
12093 if (ret) {
12094 dc_plane_state_release(dc_new_plane_state);
12095 goto out;
12096 }
12097
12098 ret = dm_atomic_get_state(state, &dm_state);
12099 if (ret) {
12100 dc_plane_state_release(dc_new_plane_state);
12101 goto out;
12102 }
12103
12104 /*
12105 * Any atomic check errors that occur after this will
12106 * not need a release. The plane state will be attached
12107 * to the stream, and therefore part of the atomic
12108 * state. It'll be released when the atomic state is
12109 * cleaned.
12110 */
12111 if (!dc_state_add_plane(
12112 dc,
12113 dm_new_crtc_state->stream,
12114 dc_new_plane_state,
12115 dm_state->context)) {
12116
12117 dc_plane_state_release(dc_new_plane_state);
12118 ret = -EINVAL;
12119 goto out;
12120 }
12121
12122 dm_new_plane_state->dc_state = dc_new_plane_state;
12123
12124 dm_new_crtc_state->mpo_requested |= (plane->type == DRM_PLANE_TYPE_OVERLAY);
12125
12126 /* Tell DC to do a full surface update every time there
12127 * is a plane change. Inefficient, but works for now.
12128 */
12129 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
12130
12131 *lock_and_validation_needed = true;
12132 }
12133
12134 out:
12135 /* If enabling cursor overlay failed, attempt fallback to native mode */
12136 if (enable && ret == -EINVAL && plane->type == DRM_PLANE_TYPE_CURSOR) {
12137 ret = dm_check_native_cursor_state(new_plane_crtc, plane,
12138 new_plane_state, enable);
12139 if (ret)
12140 return ret;
12141
12142 dm_new_crtc_state->cursor_mode = DM_CURSOR_NATIVE_MODE;
12143 }
12144
12145 return ret;
12146 }
12147
dm_get_oriented_plane_size(struct drm_plane_state * plane_state,int * src_w,int * src_h)12148 static void dm_get_oriented_plane_size(struct drm_plane_state *plane_state,
12149 int *src_w, int *src_h)
12150 {
12151 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
12152 case DRM_MODE_ROTATE_90:
12153 case DRM_MODE_ROTATE_270:
12154 *src_w = plane_state->src_h >> 16;
12155 *src_h = plane_state->src_w >> 16;
12156 break;
12157 case DRM_MODE_ROTATE_0:
12158 case DRM_MODE_ROTATE_180:
12159 default:
12160 *src_w = plane_state->src_w >> 16;
12161 *src_h = plane_state->src_h >> 16;
12162 break;
12163 }
12164 }
12165
12166 static void
dm_get_plane_scale(struct drm_plane_state * plane_state,int * out_plane_scale_w,int * out_plane_scale_h)12167 dm_get_plane_scale(struct drm_plane_state *plane_state,
12168 int *out_plane_scale_w, int *out_plane_scale_h)
12169 {
12170 int plane_src_w, plane_src_h;
12171
12172 dm_get_oriented_plane_size(plane_state, &plane_src_w, &plane_src_h);
12173 *out_plane_scale_w = plane_src_w ? plane_state->crtc_w * 1000 / plane_src_w : 0;
12174 *out_plane_scale_h = plane_src_h ? plane_state->crtc_h * 1000 / plane_src_h : 0;
12175 }
12176
12177 /*
12178 * The normalized_zpos value cannot be used by this iterator directly. It's only
12179 * calculated for enabled planes, potentially causing normalized_zpos collisions
12180 * between enabled/disabled planes in the atomic state. We need a unique value
12181 * so that the iterator will not generate the same object twice, or loop
12182 * indefinitely.
12183 */
__get_next_zpos(struct drm_atomic_state * state,struct __drm_planes_state * prev)12184 static inline struct __drm_planes_state *__get_next_zpos(
12185 struct drm_atomic_state *state,
12186 struct __drm_planes_state *prev)
12187 {
12188 unsigned int highest_zpos = 0, prev_zpos = 256;
12189 uint32_t highest_id = 0, prev_id = UINT_MAX;
12190 struct drm_plane_state *new_plane_state;
12191 struct drm_plane *plane;
12192 int i, highest_i = -1;
12193
12194 if (prev != NULL) {
12195 prev_zpos = prev->new_state->zpos;
12196 prev_id = prev->ptr->base.id;
12197 }
12198
12199 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
12200 /* Skip planes with higher zpos than the previously returned */
12201 if (new_plane_state->zpos > prev_zpos ||
12202 (new_plane_state->zpos == prev_zpos &&
12203 plane->base.id >= prev_id))
12204 continue;
12205
12206 /* Save the index of the plane with highest zpos */
12207 if (new_plane_state->zpos > highest_zpos ||
12208 (new_plane_state->zpos == highest_zpos &&
12209 plane->base.id > highest_id)) {
12210 highest_zpos = new_plane_state->zpos;
12211 highest_id = plane->base.id;
12212 highest_i = i;
12213 }
12214 }
12215
12216 if (highest_i < 0)
12217 return NULL;
12218
12219 return &state->planes[highest_i];
12220 }
12221
12222 /*
12223 * Use the uniqueness of the plane's (zpos, drm obj ID) combination to iterate
12224 * by descending zpos, as read from the new plane state. This is the same
12225 * ordering as defined by drm_atomic_normalize_zpos().
12226 */
12227 #define for_each_oldnew_plane_in_descending_zpos(__state, plane, old_plane_state, new_plane_state) \
12228 for (struct __drm_planes_state *__i = __get_next_zpos((__state), NULL); \
12229 __i != NULL; __i = __get_next_zpos((__state), __i)) \
12230 for_each_if(((plane) = __i->ptr, \
12231 (void)(plane) /* Only to avoid unused-but-set-variable warning */, \
12232 (old_plane_state) = __i->old_state, \
12233 (new_plane_state) = __i->new_state, 1))
12234
add_affected_mst_dsc_crtcs(struct drm_atomic_state * state,struct drm_crtc * crtc)12235 static int add_affected_mst_dsc_crtcs(struct drm_atomic_state *state, struct drm_crtc *crtc)
12236 {
12237 struct drm_connector *connector;
12238 struct drm_connector_state *conn_state, *old_conn_state;
12239 struct amdgpu_dm_connector *aconnector = NULL;
12240 int i;
12241
12242 for_each_oldnew_connector_in_state(state, connector, old_conn_state, conn_state, i) {
12243 if (!conn_state->crtc)
12244 conn_state = old_conn_state;
12245
12246 if (conn_state->crtc != crtc)
12247 continue;
12248
12249 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
12250 continue;
12251
12252 aconnector = to_amdgpu_dm_connector(connector);
12253 if (!aconnector->mst_output_port || !aconnector->mst_root)
12254 aconnector = NULL;
12255 else
12256 break;
12257 }
12258
12259 if (!aconnector)
12260 return 0;
12261
12262 return drm_dp_mst_add_affected_dsc_crtcs(state, &aconnector->mst_root->mst_mgr);
12263 }
12264
12265 /**
12266 * DOC: Cursor Modes - Native vs Overlay
12267 *
12268 * In native mode, the cursor uses a integrated cursor pipe within each DCN hw
12269 * plane. It does not require a dedicated hw plane to enable, but it is
12270 * subjected to the same z-order and scaling as the hw plane. It also has format
12271 * restrictions, a RGB cursor in native mode cannot be enabled within a non-RGB
12272 * hw plane.
12273 *
12274 * In overlay mode, the cursor uses a separate DCN hw plane, and thus has its
12275 * own scaling and z-pos. It also has no blending restrictions. It lends to a
12276 * cursor behavior more akin to a DRM client's expectations. However, it does
12277 * occupy an extra DCN plane, and therefore will only be used if a DCN plane is
12278 * available.
12279 */
12280
12281 /**
12282 * dm_crtc_get_cursor_mode() - Determine the required cursor mode on crtc
12283 * @adev: amdgpu device
12284 * @state: DRM atomic state
12285 * @dm_crtc_state: amdgpu state for the CRTC containing the cursor
12286 * @cursor_mode: Returns the required cursor mode on dm_crtc_state
12287 *
12288 * Get whether the cursor should be enabled in native mode, or overlay mode, on
12289 * the dm_crtc_state.
12290 *
12291 * The cursor should be enabled in overlay mode if there exists an underlying
12292 * plane - on which the cursor may be blended - that is either YUV formatted, or
12293 * scaled differently from the cursor.
12294 *
12295 * Since zpos info is required, drm_atomic_normalize_zpos must be called before
12296 * calling this function.
12297 *
12298 * Return: 0 on success, or an error code if getting the cursor plane state
12299 * failed.
12300 */
dm_crtc_get_cursor_mode(struct amdgpu_device * adev,struct drm_atomic_state * state,struct dm_crtc_state * dm_crtc_state,enum amdgpu_dm_cursor_mode * cursor_mode)12301 static int dm_crtc_get_cursor_mode(struct amdgpu_device *adev,
12302 struct drm_atomic_state *state,
12303 struct dm_crtc_state *dm_crtc_state,
12304 enum amdgpu_dm_cursor_mode *cursor_mode)
12305 {
12306 struct drm_plane_state *old_plane_state, *plane_state, *cursor_state;
12307 struct drm_crtc_state *crtc_state = &dm_crtc_state->base;
12308 struct drm_plane *plane;
12309 bool consider_mode_change = false;
12310 bool entire_crtc_covered = false;
12311 bool cursor_changed = false;
12312 int underlying_scale_w, underlying_scale_h;
12313 int cursor_scale_w, cursor_scale_h;
12314 int i;
12315
12316 /* Overlay cursor not supported on HW before DCN
12317 * DCN401 does not have the cursor-on-scaled-plane or cursor-on-yuv-plane restrictions
12318 * as previous DCN generations, so enable native mode on DCN401
12319 */
12320 if (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 0, 1)) {
12321 *cursor_mode = DM_CURSOR_NATIVE_MODE;
12322 return 0;
12323 }
12324
12325 /* Init cursor_mode to be the same as current */
12326 *cursor_mode = dm_crtc_state->cursor_mode;
12327
12328 /*
12329 * Cursor mode can change if a plane's format changes, scale changes, is
12330 * enabled/disabled, or z-order changes.
12331 */
12332 for_each_oldnew_plane_in_state(state, plane, old_plane_state, plane_state, i) {
12333 int new_scale_w, new_scale_h, old_scale_w, old_scale_h;
12334
12335 /* Only care about planes on this CRTC */
12336 if ((drm_plane_mask(plane) & crtc_state->plane_mask) == 0)
12337 continue;
12338
12339 if (plane->type == DRM_PLANE_TYPE_CURSOR)
12340 cursor_changed = true;
12341
12342 if (drm_atomic_plane_enabling(old_plane_state, plane_state) ||
12343 drm_atomic_plane_disabling(old_plane_state, plane_state) ||
12344 old_plane_state->fb->format != plane_state->fb->format) {
12345 consider_mode_change = true;
12346 break;
12347 }
12348
12349 dm_get_plane_scale(plane_state, &new_scale_w, &new_scale_h);
12350 dm_get_plane_scale(old_plane_state, &old_scale_w, &old_scale_h);
12351 if (new_scale_w != old_scale_w || new_scale_h != old_scale_h) {
12352 consider_mode_change = true;
12353 break;
12354 }
12355 }
12356
12357 if (!consider_mode_change && !crtc_state->zpos_changed)
12358 return 0;
12359
12360 /*
12361 * If no cursor change on this CRTC, and not enabled on this CRTC, then
12362 * no need to set cursor mode. This avoids needlessly locking the cursor
12363 * state.
12364 */
12365 if (!cursor_changed &&
12366 !(drm_plane_mask(crtc_state->crtc->cursor) & crtc_state->plane_mask)) {
12367 return 0;
12368 }
12369
12370 cursor_state = drm_atomic_get_plane_state(state,
12371 crtc_state->crtc->cursor);
12372 if (IS_ERR(cursor_state))
12373 return PTR_ERR(cursor_state);
12374
12375 /* Cursor is disabled */
12376 if (!cursor_state->fb)
12377 return 0;
12378
12379 /* For all planes in descending z-order (all of which are below cursor
12380 * as per zpos definitions), check their scaling and format
12381 */
12382 for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, plane_state) {
12383
12384 /* Only care about non-cursor planes on this CRTC */
12385 if ((drm_plane_mask(plane) & crtc_state->plane_mask) == 0 ||
12386 plane->type == DRM_PLANE_TYPE_CURSOR)
12387 continue;
12388
12389 /* Underlying plane is YUV format - use overlay cursor */
12390 if (amdgpu_dm_plane_is_video_format(plane_state->fb->format->format)) {
12391 *cursor_mode = DM_CURSOR_OVERLAY_MODE;
12392 return 0;
12393 }
12394
12395 dm_get_plane_scale(plane_state,
12396 &underlying_scale_w, &underlying_scale_h);
12397 dm_get_plane_scale(cursor_state,
12398 &cursor_scale_w, &cursor_scale_h);
12399
12400 /* Underlying plane has different scale - use overlay cursor */
12401 if (cursor_scale_w != underlying_scale_w &&
12402 cursor_scale_h != underlying_scale_h) {
12403 *cursor_mode = DM_CURSOR_OVERLAY_MODE;
12404 return 0;
12405 }
12406
12407 /* If this plane covers the whole CRTC, no need to check planes underneath */
12408 if (plane_state->crtc_x <= 0 && plane_state->crtc_y <= 0 &&
12409 plane_state->crtc_x + plane_state->crtc_w >= crtc_state->mode.hdisplay &&
12410 plane_state->crtc_y + plane_state->crtc_h >= crtc_state->mode.vdisplay) {
12411 entire_crtc_covered = true;
12412 break;
12413 }
12414 }
12415
12416 /* If planes do not cover the entire CRTC, use overlay mode to enable
12417 * cursor over holes
12418 */
12419 if (entire_crtc_covered)
12420 *cursor_mode = DM_CURSOR_NATIVE_MODE;
12421 else
12422 *cursor_mode = DM_CURSOR_OVERLAY_MODE;
12423
12424 return 0;
12425 }
12426
amdgpu_dm_crtc_mem_type_changed(struct drm_device * dev,struct drm_atomic_state * state,struct drm_crtc_state * crtc_state)12427 static bool amdgpu_dm_crtc_mem_type_changed(struct drm_device *dev,
12428 struct drm_atomic_state *state,
12429 struct drm_crtc_state *crtc_state)
12430 {
12431 struct drm_plane *plane;
12432 struct drm_plane_state *new_plane_state, *old_plane_state;
12433
12434 drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
12435 new_plane_state = drm_atomic_get_plane_state(state, plane);
12436 old_plane_state = drm_atomic_get_plane_state(state, plane);
12437
12438 if (IS_ERR(new_plane_state) || IS_ERR(old_plane_state)) {
12439 drm_err(dev, "Failed to get plane state for plane %s\n", plane->name);
12440 return false;
12441 }
12442
12443 if (old_plane_state->fb && new_plane_state->fb &&
12444 get_mem_type(old_plane_state->fb) != get_mem_type(new_plane_state->fb))
12445 return true;
12446 }
12447
12448 return false;
12449 }
12450
12451 /**
12452 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM.
12453 *
12454 * @dev: The DRM device
12455 * @state: The atomic state to commit
12456 *
12457 * Validate that the given atomic state is programmable by DC into hardware.
12458 * This involves constructing a &struct dc_state reflecting the new hardware
12459 * state we wish to commit, then querying DC to see if it is programmable. It's
12460 * important not to modify the existing DC state. Otherwise, atomic_check
12461 * may unexpectedly commit hardware changes.
12462 *
12463 * When validating the DC state, it's important that the right locks are
12464 * acquired. For full updates case which removes/adds/updates streams on one
12465 * CRTC while flipping on another CRTC, acquiring global lock will guarantee
12466 * that any such full update commit will wait for completion of any outstanding
12467 * flip using DRMs synchronization events.
12468 *
12469 * Note that DM adds the affected connectors for all CRTCs in state, when that
12470 * might not seem necessary. This is because DC stream creation requires the
12471 * DC sink, which is tied to the DRM connector state. Cleaning this up should
12472 * be possible but non-trivial - a possible TODO item.
12473 *
12474 * Return: -Error code if validation failed.
12475 */
amdgpu_dm_atomic_check(struct drm_device * dev,struct drm_atomic_state * state)12476 static int amdgpu_dm_atomic_check(struct drm_device *dev,
12477 struct drm_atomic_state *state)
12478 {
12479 struct amdgpu_device *adev = drm_to_adev(dev);
12480 struct dm_atomic_state *dm_state = NULL;
12481 struct dc *dc = adev->dm.dc;
12482 struct drm_connector *connector;
12483 struct drm_connector_state *old_con_state, *new_con_state;
12484 struct drm_crtc *crtc;
12485 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
12486 struct drm_plane *plane;
12487 struct drm_plane_state *old_plane_state, *new_plane_state, *new_cursor_state;
12488 enum dc_status status;
12489 int ret, i;
12490 bool lock_and_validation_needed = false;
12491 bool is_top_most_overlay = true;
12492 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
12493 struct drm_dp_mst_topology_mgr *mgr;
12494 struct drm_dp_mst_topology_state *mst_state;
12495 struct dsc_mst_fairness_vars vars[MAX_PIPES] = {0};
12496
12497 trace_amdgpu_dm_atomic_check_begin(state);
12498
12499 ret = drm_atomic_helper_check_modeset(dev, state);
12500 if (ret) {
12501 drm_dbg_atomic(dev, "drm_atomic_helper_check_modeset() failed\n");
12502 goto fail;
12503 }
12504
12505 /* Check connector changes */
12506 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
12507 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
12508 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
12509
12510 /* Skip connectors that are disabled or part of modeset already. */
12511 if (!new_con_state->crtc)
12512 continue;
12513
12514 new_crtc_state = drm_atomic_get_crtc_state(state, new_con_state->crtc);
12515 if (IS_ERR(new_crtc_state)) {
12516 drm_dbg_atomic(dev, "drm_atomic_get_crtc_state() failed\n");
12517 ret = PTR_ERR(new_crtc_state);
12518 goto fail;
12519 }
12520
12521 if (dm_old_con_state->abm_level != dm_new_con_state->abm_level ||
12522 dm_old_con_state->scaling != dm_new_con_state->scaling)
12523 new_crtc_state->connectors_changed = true;
12524 }
12525
12526 if (dc_resource_is_dsc_encoding_supported(dc)) {
12527 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12528 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12529 dm_new_crtc_state->mode_changed_independent_from_dsc = new_crtc_state->mode_changed;
12530 }
12531
12532 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12533 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
12534 ret = add_affected_mst_dsc_crtcs(state, crtc);
12535 if (ret) {
12536 drm_dbg_atomic(dev, "add_affected_mst_dsc_crtcs() failed\n");
12537 goto fail;
12538 }
12539 }
12540 }
12541 }
12542 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12543 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
12544
12545 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
12546 !new_crtc_state->color_mgmt_changed &&
12547 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled &&
12548 dm_old_crtc_state->dsc_force_changed == false)
12549 continue;
12550
12551 ret = amdgpu_dm_verify_lut_sizes(new_crtc_state);
12552 if (ret) {
12553 drm_dbg_atomic(dev, "amdgpu_dm_verify_lut_sizes() failed\n");
12554 goto fail;
12555 }
12556
12557 if (!new_crtc_state->enable)
12558 continue;
12559
12560 ret = drm_atomic_add_affected_connectors(state, crtc);
12561 if (ret) {
12562 drm_dbg_atomic(dev, "drm_atomic_add_affected_connectors() failed\n");
12563 goto fail;
12564 }
12565
12566 ret = drm_atomic_add_affected_planes(state, crtc);
12567 if (ret) {
12568 drm_dbg_atomic(dev, "drm_atomic_add_affected_planes() failed\n");
12569 goto fail;
12570 }
12571
12572 if (dm_old_crtc_state->dsc_force_changed)
12573 new_crtc_state->mode_changed = true;
12574 }
12575
12576 /*
12577 * Add all primary and overlay planes on the CRTC to the state
12578 * whenever a plane is enabled to maintain correct z-ordering
12579 * and to enable fast surface updates.
12580 */
12581 drm_for_each_crtc(crtc, dev) {
12582 bool modified = false;
12583
12584 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
12585 if (plane->type == DRM_PLANE_TYPE_CURSOR)
12586 continue;
12587
12588 if (new_plane_state->crtc == crtc ||
12589 old_plane_state->crtc == crtc) {
12590 modified = true;
12591 break;
12592 }
12593 }
12594
12595 if (!modified)
12596 continue;
12597
12598 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
12599 if (plane->type == DRM_PLANE_TYPE_CURSOR)
12600 continue;
12601
12602 new_plane_state =
12603 drm_atomic_get_plane_state(state, plane);
12604
12605 if (IS_ERR(new_plane_state)) {
12606 ret = PTR_ERR(new_plane_state);
12607 drm_dbg_atomic(dev, "new_plane_state is BAD\n");
12608 goto fail;
12609 }
12610 }
12611 }
12612
12613 /*
12614 * DC consults the zpos (layer_index in DC terminology) to determine the
12615 * hw plane on which to enable the hw cursor (see
12616 * `dcn10_can_pipe_disable_cursor`). By now, all modified planes are in
12617 * atomic state, so call drm helper to normalize zpos.
12618 */
12619 ret = drm_atomic_normalize_zpos(dev, state);
12620 if (ret) {
12621 drm_dbg(dev, "drm_atomic_normalize_zpos() failed\n");
12622 goto fail;
12623 }
12624
12625 /*
12626 * Determine whether cursors on each CRTC should be enabled in native or
12627 * overlay mode.
12628 */
12629 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12630 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12631
12632 ret = dm_crtc_get_cursor_mode(adev, state, dm_new_crtc_state,
12633 &dm_new_crtc_state->cursor_mode);
12634 if (ret) {
12635 drm_dbg(dev, "Failed to determine cursor mode\n");
12636 goto fail;
12637 }
12638
12639 /*
12640 * If overlay cursor is needed, DC cannot go through the
12641 * native cursor update path. All enabled planes on the CRTC
12642 * need to be added for DC to not disable a plane by mistake
12643 */
12644 if (dm_new_crtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE) {
12645 if (amdgpu_ip_version(adev, DCE_HWIP, 0) == 0) {
12646 drm_dbg(dev, "Overlay cursor not supported on DCE\n");
12647 ret = -EINVAL;
12648 goto fail;
12649 }
12650
12651 ret = drm_atomic_add_affected_planes(state, crtc);
12652 if (ret)
12653 goto fail;
12654 }
12655 }
12656
12657 /* Remove exiting planes if they are modified */
12658 for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, new_plane_state) {
12659
12660 ret = dm_update_plane_state(dc, state, plane,
12661 old_plane_state,
12662 new_plane_state,
12663 false,
12664 &lock_and_validation_needed,
12665 &is_top_most_overlay);
12666 if (ret) {
12667 drm_dbg_atomic(dev, "dm_update_plane_state() failed\n");
12668 goto fail;
12669 }
12670 }
12671
12672 /* Disable all crtcs which require disable */
12673 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12674 ret = dm_update_crtc_state(&adev->dm, state, crtc,
12675 old_crtc_state,
12676 new_crtc_state,
12677 false,
12678 &lock_and_validation_needed);
12679 if (ret) {
12680 drm_dbg_atomic(dev, "DISABLE: dm_update_crtc_state() failed\n");
12681 goto fail;
12682 }
12683 }
12684
12685 /* Enable all crtcs which require enable */
12686 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12687 ret = dm_update_crtc_state(&adev->dm, state, crtc,
12688 old_crtc_state,
12689 new_crtc_state,
12690 true,
12691 &lock_and_validation_needed);
12692 if (ret) {
12693 drm_dbg_atomic(dev, "ENABLE: dm_update_crtc_state() failed\n");
12694 goto fail;
12695 }
12696 }
12697
12698 /* Add new/modified planes */
12699 for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, new_plane_state) {
12700 ret = dm_update_plane_state(dc, state, plane,
12701 old_plane_state,
12702 new_plane_state,
12703 true,
12704 &lock_and_validation_needed,
12705 &is_top_most_overlay);
12706 if (ret) {
12707 drm_dbg_atomic(dev, "dm_update_plane_state() failed\n");
12708 goto fail;
12709 }
12710 }
12711
12712 #if defined(CONFIG_DRM_AMD_DC_FP)
12713 if (dc_resource_is_dsc_encoding_supported(dc)) {
12714 ret = pre_validate_dsc(state, &dm_state, vars);
12715 if (ret != 0)
12716 goto fail;
12717 }
12718 #endif
12719
12720 /* Run this here since we want to validate the streams we created */
12721 ret = drm_atomic_helper_check_planes(dev, state);
12722 if (ret) {
12723 drm_dbg_atomic(dev, "drm_atomic_helper_check_planes() failed\n");
12724 goto fail;
12725 }
12726
12727 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12728 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12729 if (dm_new_crtc_state->mpo_requested)
12730 drm_dbg_atomic(dev, "MPO enablement requested on crtc:[%p]\n", crtc);
12731 }
12732
12733 /* Check cursor restrictions */
12734 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12735 enum amdgpu_dm_cursor_mode required_cursor_mode;
12736 int is_rotated, is_scaled;
12737
12738 /* Overlay cusor not subject to native cursor restrictions */
12739 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12740 if (dm_new_crtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE)
12741 continue;
12742
12743 /* Check if rotation or scaling is enabled on DCN401 */
12744 if ((drm_plane_mask(crtc->cursor) & new_crtc_state->plane_mask) &&
12745 amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 0, 1)) {
12746 new_cursor_state = drm_atomic_get_new_plane_state(state, crtc->cursor);
12747
12748 is_rotated = new_cursor_state &&
12749 ((new_cursor_state->rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0);
12750 is_scaled = new_cursor_state && ((new_cursor_state->src_w >> 16 != new_cursor_state->crtc_w) ||
12751 (new_cursor_state->src_h >> 16 != new_cursor_state->crtc_h));
12752
12753 if (is_rotated || is_scaled) {
12754 drm_dbg_driver(
12755 crtc->dev,
12756 "[CRTC:%d:%s] cannot enable hardware cursor due to rotation/scaling\n",
12757 crtc->base.id, crtc->name);
12758 ret = -EINVAL;
12759 goto fail;
12760 }
12761 }
12762
12763 /* If HW can only do native cursor, check restrictions again */
12764 ret = dm_crtc_get_cursor_mode(adev, state, dm_new_crtc_state,
12765 &required_cursor_mode);
12766 if (ret) {
12767 drm_dbg_driver(crtc->dev,
12768 "[CRTC:%d:%s] Checking cursor mode failed\n",
12769 crtc->base.id, crtc->name);
12770 goto fail;
12771 } else if (required_cursor_mode == DM_CURSOR_OVERLAY_MODE) {
12772 drm_dbg_driver(crtc->dev,
12773 "[CRTC:%d:%s] Cannot enable native cursor due to scaling or YUV restrictions\n",
12774 crtc->base.id, crtc->name);
12775 ret = -EINVAL;
12776 goto fail;
12777 }
12778 }
12779
12780 if (state->legacy_cursor_update) {
12781 /*
12782 * This is a fast cursor update coming from the plane update
12783 * helper, check if it can be done asynchronously for better
12784 * performance.
12785 */
12786 state->async_update =
12787 !drm_atomic_helper_async_check(dev, state);
12788
12789 /*
12790 * Skip the remaining global validation if this is an async
12791 * update. Cursor updates can be done without affecting
12792 * state or bandwidth calcs and this avoids the performance
12793 * penalty of locking the private state object and
12794 * allocating a new dc_state.
12795 */
12796 if (state->async_update)
12797 return 0;
12798 }
12799
12800 /* Check scaling and underscan changes*/
12801 /* TODO Removed scaling changes validation due to inability to commit
12802 * new stream into context w\o causing full reset. Need to
12803 * decide how to handle.
12804 */
12805 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
12806 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
12807 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
12808 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
12809
12810 /* Skip any modesets/resets */
12811 if (!acrtc || drm_atomic_crtc_needs_modeset(
12812 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
12813 continue;
12814
12815 /* Skip any thing not scale or underscan changes */
12816 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
12817 continue;
12818
12819 lock_and_validation_needed = true;
12820 }
12821
12822 /* set the slot info for each mst_state based on the link encoding format */
12823 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
12824 struct amdgpu_dm_connector *aconnector;
12825 struct drm_connector *connector;
12826 struct drm_connector_list_iter iter;
12827 u8 link_coding_cap;
12828
12829 drm_connector_list_iter_begin(dev, &iter);
12830 drm_for_each_connector_iter(connector, &iter) {
12831 if (connector->index == mst_state->mgr->conn_base_id) {
12832 aconnector = to_amdgpu_dm_connector(connector);
12833 link_coding_cap = dc_link_dp_mst_decide_link_encoding_format(aconnector->dc_link);
12834 drm_dp_mst_update_slots(mst_state, link_coding_cap);
12835
12836 break;
12837 }
12838 }
12839 drm_connector_list_iter_end(&iter);
12840 }
12841
12842 /**
12843 * Streams and planes are reset when there are changes that affect
12844 * bandwidth. Anything that affects bandwidth needs to go through
12845 * DC global validation to ensure that the configuration can be applied
12846 * to hardware.
12847 *
12848 * We have to currently stall out here in atomic_check for outstanding
12849 * commits to finish in this case because our IRQ handlers reference
12850 * DRM state directly - we can end up disabling interrupts too early
12851 * if we don't.
12852 *
12853 * TODO: Remove this stall and drop DM state private objects.
12854 */
12855 if (lock_and_validation_needed) {
12856 ret = dm_atomic_get_state(state, &dm_state);
12857 if (ret) {
12858 drm_dbg_atomic(dev, "dm_atomic_get_state() failed\n");
12859 goto fail;
12860 }
12861
12862 ret = do_aquire_global_lock(dev, state);
12863 if (ret) {
12864 drm_dbg_atomic(dev, "do_aquire_global_lock() failed\n");
12865 goto fail;
12866 }
12867
12868 #if defined(CONFIG_DRM_AMD_DC_FP)
12869 if (dc_resource_is_dsc_encoding_supported(dc)) {
12870 ret = compute_mst_dsc_configs_for_state(state, dm_state->context, vars);
12871 if (ret) {
12872 drm_dbg_atomic(dev, "MST_DSC compute_mst_dsc_configs_for_state() failed\n");
12873 ret = -EINVAL;
12874 goto fail;
12875 }
12876 }
12877 #endif
12878
12879 ret = dm_update_mst_vcpi_slots_for_dsc(state, dm_state->context, vars);
12880 if (ret) {
12881 drm_dbg_atomic(dev, "dm_update_mst_vcpi_slots_for_dsc() failed\n");
12882 goto fail;
12883 }
12884
12885 /*
12886 * Perform validation of MST topology in the state:
12887 * We need to perform MST atomic check before calling
12888 * dc_validate_global_state(), or there is a chance
12889 * to get stuck in an infinite loop and hang eventually.
12890 */
12891 ret = drm_dp_mst_atomic_check(state);
12892 if (ret) {
12893 drm_dbg_atomic(dev, "MST drm_dp_mst_atomic_check() failed\n");
12894 goto fail;
12895 }
12896 status = dc_validate_global_state(dc, dm_state->context, DC_VALIDATE_MODE_ONLY);
12897 if (status != DC_OK) {
12898 drm_dbg_atomic(dev, "DC global validation failure: %s (%d)",
12899 dc_status_to_str(status), status);
12900 ret = -EINVAL;
12901 goto fail;
12902 }
12903 } else {
12904 /*
12905 * The commit is a fast update. Fast updates shouldn't change
12906 * the DC context, affect global validation, and can have their
12907 * commit work done in parallel with other commits not touching
12908 * the same resource. If we have a new DC context as part of
12909 * the DM atomic state from validation we need to free it and
12910 * retain the existing one instead.
12911 *
12912 * Furthermore, since the DM atomic state only contains the DC
12913 * context and can safely be annulled, we can free the state
12914 * and clear the associated private object now to free
12915 * some memory and avoid a possible use-after-free later.
12916 */
12917
12918 for (i = 0; i < state->num_private_objs; i++) {
12919 struct drm_private_obj *obj = state->private_objs[i].ptr;
12920
12921 if (obj->funcs == adev->dm.atomic_obj.funcs) {
12922 int j = state->num_private_objs-1;
12923
12924 dm_atomic_destroy_state(obj,
12925 state->private_objs[i].state_to_destroy);
12926
12927 /* If i is not at the end of the array then the
12928 * last element needs to be moved to where i was
12929 * before the array can safely be truncated.
12930 */
12931 if (i != j)
12932 state->private_objs[i] =
12933 state->private_objs[j];
12934
12935 state->private_objs[j].ptr = NULL;
12936 state->private_objs[j].state_to_destroy = NULL;
12937 state->private_objs[j].old_state = NULL;
12938 state->private_objs[j].new_state = NULL;
12939
12940 state->num_private_objs = j;
12941 break;
12942 }
12943 }
12944 }
12945
12946 /* Store the overall update type for use later in atomic check. */
12947 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12948 struct dm_crtc_state *dm_new_crtc_state =
12949 to_dm_crtc_state(new_crtc_state);
12950
12951 /*
12952 * Only allow async flips for fast updates that don't change
12953 * the FB pitch, the DCC state, rotation, mem_type, etc.
12954 */
12955 if (new_crtc_state->async_flip &&
12956 (lock_and_validation_needed ||
12957 amdgpu_dm_crtc_mem_type_changed(dev, state, new_crtc_state))) {
12958 drm_dbg_atomic(crtc->dev,
12959 "[CRTC:%d:%s] async flips are only supported for fast updates\n",
12960 crtc->base.id, crtc->name);
12961 ret = -EINVAL;
12962 goto fail;
12963 }
12964
12965 dm_new_crtc_state->update_type = lock_and_validation_needed ?
12966 UPDATE_TYPE_FULL : UPDATE_TYPE_FAST;
12967 }
12968
12969 /* Must be success */
12970 WARN_ON(ret);
12971
12972 trace_amdgpu_dm_atomic_check_finish(state, ret);
12973
12974 return ret;
12975
12976 fail:
12977 if (ret == -EDEADLK)
12978 drm_dbg_atomic(dev, "Atomic check stopped to avoid deadlock.\n");
12979 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
12980 drm_dbg_atomic(dev, "Atomic check stopped due to signal.\n");
12981 else
12982 drm_dbg_atomic(dev, "Atomic check failed with err: %d\n", ret);
12983
12984 trace_amdgpu_dm_atomic_check_finish(state, ret);
12985
12986 return ret;
12987 }
12988
dm_edid_parser_send_cea(struct amdgpu_display_manager * dm,unsigned int offset,unsigned int total_length,u8 * data,unsigned int length,struct amdgpu_hdmi_vsdb_info * vsdb)12989 static bool dm_edid_parser_send_cea(struct amdgpu_display_manager *dm,
12990 unsigned int offset,
12991 unsigned int total_length,
12992 u8 *data,
12993 unsigned int length,
12994 struct amdgpu_hdmi_vsdb_info *vsdb)
12995 {
12996 bool res;
12997 union dmub_rb_cmd cmd;
12998 struct dmub_cmd_send_edid_cea *input;
12999 struct dmub_cmd_edid_cea_output *output;
13000
13001 if (length > DMUB_EDID_CEA_DATA_CHUNK_BYTES)
13002 return false;
13003
13004 memset(&cmd, 0, sizeof(cmd));
13005
13006 input = &cmd.edid_cea.data.input;
13007
13008 cmd.edid_cea.header.type = DMUB_CMD__EDID_CEA;
13009 cmd.edid_cea.header.sub_type = 0;
13010 cmd.edid_cea.header.payload_bytes =
13011 sizeof(cmd.edid_cea) - sizeof(cmd.edid_cea.header);
13012 input->offset = offset;
13013 input->length = length;
13014 input->cea_total_length = total_length;
13015 memcpy(input->payload, data, length);
13016
13017 res = dc_wake_and_execute_dmub_cmd(dm->dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY);
13018 if (!res) {
13019 drm_err(adev_to_drm(dm->adev), "EDID CEA parser failed\n");
13020 return false;
13021 }
13022
13023 output = &cmd.edid_cea.data.output;
13024
13025 if (output->type == DMUB_CMD__EDID_CEA_ACK) {
13026 if (!output->ack.success) {
13027 drm_err(adev_to_drm(dm->adev), "EDID CEA ack failed at offset %d\n",
13028 output->ack.offset);
13029 }
13030 } else if (output->type == DMUB_CMD__EDID_CEA_AMD_VSDB) {
13031 if (!output->amd_vsdb.vsdb_found)
13032 return false;
13033
13034 vsdb->freesync_supported = output->amd_vsdb.freesync_supported;
13035 vsdb->amd_vsdb_version = output->amd_vsdb.amd_vsdb_version;
13036 vsdb->min_refresh_rate_hz = output->amd_vsdb.min_frame_rate;
13037 vsdb->max_refresh_rate_hz = output->amd_vsdb.max_frame_rate;
13038 } else {
13039 drm_warn(adev_to_drm(dm->adev), "Unknown EDID CEA parser results\n");
13040 return false;
13041 }
13042
13043 return true;
13044 }
13045
parse_edid_cea_dmcu(struct amdgpu_display_manager * dm,u8 * edid_ext,int len,struct amdgpu_hdmi_vsdb_info * vsdb_info)13046 static bool parse_edid_cea_dmcu(struct amdgpu_display_manager *dm,
13047 u8 *edid_ext, int len,
13048 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13049 {
13050 int i;
13051
13052 /* send extension block to DMCU for parsing */
13053 for (i = 0; i < len; i += 8) {
13054 bool res;
13055 int offset;
13056
13057 /* send 8 bytes a time */
13058 if (!dc_edid_parser_send_cea(dm->dc, i, len, &edid_ext[i], 8))
13059 return false;
13060
13061 if (i+8 == len) {
13062 /* EDID block sent completed, expect result */
13063 int version, min_rate, max_rate;
13064
13065 res = dc_edid_parser_recv_amd_vsdb(dm->dc, &version, &min_rate, &max_rate);
13066 if (res) {
13067 /* amd vsdb found */
13068 vsdb_info->freesync_supported = 1;
13069 vsdb_info->amd_vsdb_version = version;
13070 vsdb_info->min_refresh_rate_hz = min_rate;
13071 vsdb_info->max_refresh_rate_hz = max_rate;
13072 return true;
13073 }
13074 /* not amd vsdb */
13075 return false;
13076 }
13077
13078 /* check for ack*/
13079 res = dc_edid_parser_recv_cea_ack(dm->dc, &offset);
13080 if (!res)
13081 return false;
13082 }
13083
13084 return false;
13085 }
13086
parse_edid_cea_dmub(struct amdgpu_display_manager * dm,u8 * edid_ext,int len,struct amdgpu_hdmi_vsdb_info * vsdb_info)13087 static bool parse_edid_cea_dmub(struct amdgpu_display_manager *dm,
13088 u8 *edid_ext, int len,
13089 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13090 {
13091 int i;
13092
13093 /* send extension block to DMCU for parsing */
13094 for (i = 0; i < len; i += 8) {
13095 /* send 8 bytes a time */
13096 if (!dm_edid_parser_send_cea(dm, i, len, &edid_ext[i], 8, vsdb_info))
13097 return false;
13098 }
13099
13100 return vsdb_info->freesync_supported;
13101 }
13102
parse_edid_cea(struct amdgpu_dm_connector * aconnector,u8 * edid_ext,int len,struct amdgpu_hdmi_vsdb_info * vsdb_info)13103 static bool parse_edid_cea(struct amdgpu_dm_connector *aconnector,
13104 u8 *edid_ext, int len,
13105 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13106 {
13107 struct amdgpu_device *adev = drm_to_adev(aconnector->base.dev);
13108 bool ret;
13109
13110 mutex_lock(&adev->dm.dc_lock);
13111 if (adev->dm.dmub_srv)
13112 ret = parse_edid_cea_dmub(&adev->dm, edid_ext, len, vsdb_info);
13113 else
13114 ret = parse_edid_cea_dmcu(&adev->dm, edid_ext, len, vsdb_info);
13115 mutex_unlock(&adev->dm.dc_lock);
13116 return ret;
13117 }
13118
parse_edid_displayid_vrr(struct drm_connector * connector,const struct edid * edid)13119 static void parse_edid_displayid_vrr(struct drm_connector *connector,
13120 const struct edid *edid)
13121 {
13122 u8 *edid_ext = NULL;
13123 int i;
13124 int j = 0;
13125 u16 min_vfreq;
13126 u16 max_vfreq;
13127
13128 if (!edid || !edid->extensions)
13129 return;
13130
13131 /* Find DisplayID extension */
13132 for (i = 0; i < edid->extensions; i++) {
13133 edid_ext = (void *)(edid + (i + 1));
13134 if (edid_ext[0] == DISPLAYID_EXT)
13135 break;
13136 }
13137
13138 if (i == edid->extensions)
13139 return;
13140
13141 while (j < EDID_LENGTH) {
13142 /* Get dynamic video timing range from DisplayID if available */
13143 if (EDID_LENGTH - j > 13 && edid_ext[j] == 0x25 &&
13144 (edid_ext[j+1] & 0xFE) == 0 && (edid_ext[j+2] == 9)) {
13145 min_vfreq = edid_ext[j+9];
13146 if (edid_ext[j+1] & 7)
13147 max_vfreq = edid_ext[j+10] + ((edid_ext[j+11] & 3) << 8);
13148 else
13149 max_vfreq = edid_ext[j+10];
13150
13151 if (max_vfreq && min_vfreq) {
13152 connector->display_info.monitor_range.max_vfreq = max_vfreq;
13153 connector->display_info.monitor_range.min_vfreq = min_vfreq;
13154
13155 return;
13156 }
13157 }
13158 j++;
13159 }
13160 }
13161
parse_amd_vsdb(struct amdgpu_dm_connector * aconnector,const struct edid * edid,struct amdgpu_hdmi_vsdb_info * vsdb_info)13162 static int parse_amd_vsdb(struct amdgpu_dm_connector *aconnector,
13163 const struct edid *edid, struct amdgpu_hdmi_vsdb_info *vsdb_info)
13164 {
13165 u8 *edid_ext = NULL;
13166 int i;
13167 int j = 0;
13168 int total_ext_block_len;
13169
13170 if (edid == NULL || edid->extensions == 0)
13171 return -ENODEV;
13172
13173 /* Find DisplayID extension */
13174 for (i = 0; i < edid->extensions; i++) {
13175 edid_ext = (void *)(edid + (i + 1));
13176 if (edid_ext[0] == DISPLAYID_EXT)
13177 break;
13178 }
13179
13180 total_ext_block_len = EDID_LENGTH * edid->extensions;
13181 while (j < total_ext_block_len - sizeof(struct amd_vsdb_block)) {
13182 struct amd_vsdb_block *amd_vsdb = (struct amd_vsdb_block *)&edid_ext[j];
13183 unsigned int ieeeId = (amd_vsdb->ieee_id[2] << 16) | (amd_vsdb->ieee_id[1] << 8) | (amd_vsdb->ieee_id[0]);
13184
13185 if (ieeeId == HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_IEEE_REGISTRATION_ID &&
13186 amd_vsdb->version == HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_VERSION_3) {
13187 u8 panel_type;
13188 vsdb_info->replay_mode = (amd_vsdb->feature_caps & AMD_VSDB_VERSION_3_FEATURECAP_REPLAYMODE) ? true : false;
13189 vsdb_info->amd_vsdb_version = HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_VERSION_3;
13190 drm_dbg_kms(aconnector->base.dev, "Panel supports Replay Mode: %d\n", vsdb_info->replay_mode);
13191 panel_type = (amd_vsdb->color_space_eotf_support & AMD_VDSB_VERSION_3_PANEL_TYPE_MASK) >> AMD_VDSB_VERSION_3_PANEL_TYPE_SHIFT;
13192 switch (panel_type) {
13193 case AMD_VSDB_PANEL_TYPE_OLED:
13194 aconnector->dc_link->panel_type = PANEL_TYPE_OLED;
13195 break;
13196 case AMD_VSDB_PANEL_TYPE_MINILED:
13197 aconnector->dc_link->panel_type = PANEL_TYPE_MINILED;
13198 break;
13199 default:
13200 aconnector->dc_link->panel_type = PANEL_TYPE_NONE;
13201 break;
13202 }
13203 drm_dbg_kms(aconnector->base.dev, "Panel type: %d\n",
13204 aconnector->dc_link->panel_type);
13205
13206 return true;
13207 }
13208 j++;
13209 }
13210
13211 return false;
13212 }
13213
parse_hdmi_amd_vsdb(struct amdgpu_dm_connector * aconnector,const struct edid * edid,struct amdgpu_hdmi_vsdb_info * vsdb_info)13214 static int parse_hdmi_amd_vsdb(struct amdgpu_dm_connector *aconnector,
13215 const struct edid *edid,
13216 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13217 {
13218 u8 *edid_ext = NULL;
13219 int i;
13220 bool valid_vsdb_found = false;
13221
13222 /*----- drm_find_cea_extension() -----*/
13223 /* No EDID or EDID extensions */
13224 if (edid == NULL || edid->extensions == 0)
13225 return -ENODEV;
13226
13227 /* Find CEA extension */
13228 for (i = 0; i < edid->extensions; i++) {
13229 edid_ext = (uint8_t *)edid + EDID_LENGTH * (i + 1);
13230 if (edid_ext[0] == CEA_EXT)
13231 break;
13232 }
13233
13234 if (i == edid->extensions)
13235 return -ENODEV;
13236
13237 /*----- cea_db_offsets() -----*/
13238 if (edid_ext[0] != CEA_EXT)
13239 return -ENODEV;
13240
13241 valid_vsdb_found = parse_edid_cea(aconnector, edid_ext, EDID_LENGTH, vsdb_info);
13242
13243 return valid_vsdb_found ? i : -ENODEV;
13244 }
13245
13246 /**
13247 * amdgpu_dm_update_freesync_caps - Update Freesync capabilities
13248 *
13249 * @connector: Connector to query.
13250 * @drm_edid: DRM EDID from monitor
13251 *
13252 * Amdgpu supports Freesync in DP and HDMI displays, and it is required to keep
13253 * track of some of the display information in the internal data struct used by
13254 * amdgpu_dm. This function checks which type of connector we need to set the
13255 * FreeSync parameters.
13256 */
amdgpu_dm_update_freesync_caps(struct drm_connector * connector,const struct drm_edid * drm_edid)13257 void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
13258 const struct drm_edid *drm_edid)
13259 {
13260 int i = 0;
13261 struct amdgpu_dm_connector *amdgpu_dm_connector =
13262 to_amdgpu_dm_connector(connector);
13263 struct dm_connector_state *dm_con_state = NULL;
13264 struct dc_sink *sink;
13265 struct amdgpu_device *adev = drm_to_adev(connector->dev);
13266 struct amdgpu_hdmi_vsdb_info vsdb_info = {0};
13267 const struct edid *edid;
13268 bool freesync_capable = false;
13269 enum adaptive_sync_type as_type = ADAPTIVE_SYNC_TYPE_NONE;
13270
13271 if (!connector->state) {
13272 drm_err(adev_to_drm(adev), "%s - Connector has no state", __func__);
13273 goto update;
13274 }
13275
13276 sink = amdgpu_dm_connector->dc_sink ?
13277 amdgpu_dm_connector->dc_sink :
13278 amdgpu_dm_connector->dc_em_sink;
13279
13280 drm_edid_connector_update(connector, drm_edid);
13281
13282 if (!drm_edid || !sink) {
13283 dm_con_state = to_dm_connector_state(connector->state);
13284
13285 amdgpu_dm_connector->min_vfreq = 0;
13286 amdgpu_dm_connector->max_vfreq = 0;
13287 freesync_capable = false;
13288
13289 goto update;
13290 }
13291
13292 dm_con_state = to_dm_connector_state(connector->state);
13293
13294 if (!adev->dm.freesync_module || !dc_supports_vrr(sink->ctx->dce_version))
13295 goto update;
13296
13297 edid = drm_edid_raw(drm_edid); // FIXME: Get rid of drm_edid_raw()
13298
13299 /* Some eDP panels only have the refresh rate range info in DisplayID */
13300 if ((connector->display_info.monitor_range.min_vfreq == 0 ||
13301 connector->display_info.monitor_range.max_vfreq == 0))
13302 parse_edid_displayid_vrr(connector, edid);
13303
13304 if (edid && (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT ||
13305 sink->sink_signal == SIGNAL_TYPE_EDP)) {
13306 if (amdgpu_dm_connector->dc_link &&
13307 amdgpu_dm_connector->dc_link->dpcd_caps.allow_invalid_MSA_timing_param) {
13308 amdgpu_dm_connector->min_vfreq = connector->display_info.monitor_range.min_vfreq;
13309 amdgpu_dm_connector->max_vfreq = connector->display_info.monitor_range.max_vfreq;
13310 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
13311 freesync_capable = true;
13312 }
13313
13314 parse_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
13315
13316 if (vsdb_info.replay_mode) {
13317 amdgpu_dm_connector->vsdb_info.replay_mode = vsdb_info.replay_mode;
13318 amdgpu_dm_connector->vsdb_info.amd_vsdb_version = vsdb_info.amd_vsdb_version;
13319 amdgpu_dm_connector->as_type = ADAPTIVE_SYNC_TYPE_EDP;
13320 }
13321
13322 } else if (drm_edid && sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A) {
13323 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
13324 if (i >= 0 && vsdb_info.freesync_supported) {
13325 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz;
13326 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz;
13327 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
13328 freesync_capable = true;
13329
13330 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz;
13331 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz;
13332 }
13333 }
13334
13335 if (amdgpu_dm_connector->dc_link)
13336 as_type = dm_get_adaptive_sync_support_type(amdgpu_dm_connector->dc_link);
13337
13338 if (as_type == FREESYNC_TYPE_PCON_IN_WHITELIST) {
13339 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
13340 if (i >= 0 && vsdb_info.freesync_supported && vsdb_info.amd_vsdb_version > 0) {
13341
13342 amdgpu_dm_connector->pack_sdp_v1_3 = true;
13343 amdgpu_dm_connector->as_type = as_type;
13344 amdgpu_dm_connector->vsdb_info = vsdb_info;
13345
13346 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz;
13347 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz;
13348 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
13349 freesync_capable = true;
13350
13351 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz;
13352 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz;
13353 }
13354 }
13355
13356 update:
13357 if (dm_con_state)
13358 dm_con_state->freesync_capable = freesync_capable;
13359
13360 if (connector->state && amdgpu_dm_connector->dc_link && !freesync_capable &&
13361 amdgpu_dm_connector->dc_link->replay_settings.config.replay_supported) {
13362 amdgpu_dm_connector->dc_link->replay_settings.config.replay_supported = false;
13363 amdgpu_dm_connector->dc_link->replay_settings.replay_feature_enabled = false;
13364 }
13365
13366 if (connector->vrr_capable_property)
13367 drm_connector_set_vrr_capable_property(connector,
13368 freesync_capable);
13369 }
13370
amdgpu_dm_trigger_timing_sync(struct drm_device * dev)13371 void amdgpu_dm_trigger_timing_sync(struct drm_device *dev)
13372 {
13373 struct amdgpu_device *adev = drm_to_adev(dev);
13374 struct dc *dc = adev->dm.dc;
13375 int i;
13376
13377 mutex_lock(&adev->dm.dc_lock);
13378 if (dc->current_state) {
13379 for (i = 0; i < dc->current_state->stream_count; ++i)
13380 dc->current_state->streams[i]
13381 ->triggered_crtc_reset.enabled =
13382 adev->dm.force_timing_sync;
13383
13384 dm_enable_per_frame_crtc_master_sync(dc->current_state);
13385 dc_trigger_sync(dc, dc->current_state);
13386 }
13387 mutex_unlock(&adev->dm.dc_lock);
13388 }
13389
amdgpu_dm_exit_ips_for_hw_access(struct dc * dc)13390 static inline void amdgpu_dm_exit_ips_for_hw_access(struct dc *dc)
13391 {
13392 if (dc->ctx->dmub_srv && !dc->ctx->dmub_srv->idle_exit_counter)
13393 dc_exit_ips_for_hw_access(dc);
13394 }
13395
dm_write_reg_func(const struct dc_context * ctx,uint32_t address,u32 value,const char * func_name)13396 void dm_write_reg_func(const struct dc_context *ctx, uint32_t address,
13397 u32 value, const char *func_name)
13398 {
13399 #ifdef DM_CHECK_ADDR_0
13400 if (address == 0) {
13401 drm_err(adev_to_drm(ctx->driver_context),
13402 "invalid register write. address = 0");
13403 return;
13404 }
13405 #endif
13406
13407 amdgpu_dm_exit_ips_for_hw_access(ctx->dc);
13408 cgs_write_register(ctx->cgs_device, address, value);
13409 trace_amdgpu_dc_wreg(&ctx->perf_trace->write_count, address, value);
13410 }
13411
dm_read_reg_func(const struct dc_context * ctx,uint32_t address,const char * func_name)13412 uint32_t dm_read_reg_func(const struct dc_context *ctx, uint32_t address,
13413 const char *func_name)
13414 {
13415 u32 value;
13416 #ifdef DM_CHECK_ADDR_0
13417 if (address == 0) {
13418 drm_err(adev_to_drm(ctx->driver_context),
13419 "invalid register read; address = 0\n");
13420 return 0;
13421 }
13422 #endif
13423
13424 if (ctx->dmub_srv &&
13425 ctx->dmub_srv->reg_helper_offload.gather_in_progress &&
13426 !ctx->dmub_srv->reg_helper_offload.should_burst_write) {
13427 ASSERT(false);
13428 return 0;
13429 }
13430
13431 amdgpu_dm_exit_ips_for_hw_access(ctx->dc);
13432
13433 value = cgs_read_register(ctx->cgs_device, address);
13434
13435 trace_amdgpu_dc_rreg(&ctx->perf_trace->read_count, address, value);
13436
13437 return value;
13438 }
13439
amdgpu_dm_process_dmub_aux_transfer_sync(struct dc_context * ctx,unsigned int link_index,struct aux_payload * payload,enum aux_return_code_type * operation_result)13440 int amdgpu_dm_process_dmub_aux_transfer_sync(
13441 struct dc_context *ctx,
13442 unsigned int link_index,
13443 struct aux_payload *payload,
13444 enum aux_return_code_type *operation_result)
13445 {
13446 struct amdgpu_device *adev = ctx->driver_context;
13447 struct dmub_notification *p_notify = adev->dm.dmub_notify;
13448 int ret = -1;
13449
13450 mutex_lock(&adev->dm.dpia_aux_lock);
13451 if (!dc_process_dmub_aux_transfer_async(ctx->dc, link_index, payload)) {
13452 *operation_result = AUX_RET_ERROR_ENGINE_ACQUIRE;
13453 goto out;
13454 }
13455
13456 if (!wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ)) {
13457 drm_err(adev_to_drm(adev), "wait_for_completion_timeout timeout!");
13458 *operation_result = AUX_RET_ERROR_TIMEOUT;
13459 goto out;
13460 }
13461
13462 if (p_notify->result != AUX_RET_SUCCESS) {
13463 /*
13464 * Transient states before tunneling is enabled could
13465 * lead to this error. We can ignore this for now.
13466 */
13467 if (p_notify->result == AUX_RET_ERROR_PROTOCOL_ERROR) {
13468 drm_warn(adev_to_drm(adev), "DPIA AUX failed on 0x%x(%d), error %d\n",
13469 payload->address, payload->length,
13470 p_notify->result);
13471 }
13472 *operation_result = p_notify->result;
13473 goto out;
13474 }
13475
13476 payload->reply[0] = adev->dm.dmub_notify->aux_reply.command & 0xF;
13477 if (adev->dm.dmub_notify->aux_reply.command & 0xF0)
13478 /* The reply is stored in the top nibble of the command. */
13479 payload->reply[0] = (adev->dm.dmub_notify->aux_reply.command >> 4) & 0xF;
13480
13481 /*write req may receive a byte indicating partially written number as well*/
13482 if (p_notify->aux_reply.length)
13483 memcpy(payload->data, p_notify->aux_reply.data,
13484 p_notify->aux_reply.length);
13485
13486 /* success */
13487 ret = p_notify->aux_reply.length;
13488 *operation_result = p_notify->result;
13489 out:
13490 reinit_completion(&adev->dm.dmub_aux_transfer_done);
13491 mutex_unlock(&adev->dm.dpia_aux_lock);
13492 return ret;
13493 }
13494
abort_fused_io(struct dc_context * ctx,const struct dmub_cmd_fused_request * request)13495 static void abort_fused_io(
13496 struct dc_context *ctx,
13497 const struct dmub_cmd_fused_request *request
13498 )
13499 {
13500 union dmub_rb_cmd command = { 0 };
13501 struct dmub_rb_cmd_fused_io *io = &command.fused_io;
13502
13503 io->header.type = DMUB_CMD__FUSED_IO;
13504 io->header.sub_type = DMUB_CMD__FUSED_IO_ABORT;
13505 io->header.payload_bytes = sizeof(*io) - sizeof(io->header);
13506 io->request = *request;
13507 dm_execute_dmub_cmd(ctx, &command, DM_DMUB_WAIT_TYPE_NO_WAIT);
13508 }
13509
execute_fused_io(struct amdgpu_device * dev,struct dc_context * ctx,union dmub_rb_cmd * commands,uint8_t count,uint32_t timeout_us)13510 static bool execute_fused_io(
13511 struct amdgpu_device *dev,
13512 struct dc_context *ctx,
13513 union dmub_rb_cmd *commands,
13514 uint8_t count,
13515 uint32_t timeout_us
13516 )
13517 {
13518 const uint8_t ddc_line = commands[0].fused_io.request.u.aux.ddc_line;
13519
13520 if (ddc_line >= ARRAY_SIZE(dev->dm.fused_io))
13521 return false;
13522
13523 struct fused_io_sync *sync = &dev->dm.fused_io[ddc_line];
13524 struct dmub_rb_cmd_fused_io *first = &commands[0].fused_io;
13525 const bool result = dm_execute_dmub_cmd_list(ctx, count, commands, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY)
13526 && first->header.ret_status
13527 && first->request.status == FUSED_REQUEST_STATUS_SUCCESS;
13528
13529 if (!result)
13530 return false;
13531
13532 while (wait_for_completion_timeout(&sync->replied, usecs_to_jiffies(timeout_us))) {
13533 reinit_completion(&sync->replied);
13534
13535 struct dmub_cmd_fused_request *reply = (struct dmub_cmd_fused_request *) sync->reply_data;
13536
13537 static_assert(sizeof(*reply) <= sizeof(sync->reply_data), "Size mismatch");
13538
13539 if (reply->identifier == first->request.identifier) {
13540 first->request = *reply;
13541 return true;
13542 }
13543 }
13544
13545 reinit_completion(&sync->replied);
13546 first->request.status = FUSED_REQUEST_STATUS_TIMEOUT;
13547 abort_fused_io(ctx, &first->request);
13548 return false;
13549 }
13550
amdgpu_dm_execute_fused_io(struct amdgpu_device * dev,struct dc_link * link,union dmub_rb_cmd * commands,uint8_t count,uint32_t timeout_us)13551 bool amdgpu_dm_execute_fused_io(
13552 struct amdgpu_device *dev,
13553 struct dc_link *link,
13554 union dmub_rb_cmd *commands,
13555 uint8_t count,
13556 uint32_t timeout_us)
13557 {
13558 struct amdgpu_display_manager *dm = &dev->dm;
13559
13560 mutex_lock(&dm->dpia_aux_lock);
13561
13562 const bool result = execute_fused_io(dev, link->ctx, commands, count, timeout_us);
13563
13564 mutex_unlock(&dm->dpia_aux_lock);
13565 return result;
13566 }
13567
amdgpu_dm_process_dmub_set_config_sync(struct dc_context * ctx,unsigned int link_index,struct set_config_cmd_payload * payload,enum set_config_status * operation_result)13568 int amdgpu_dm_process_dmub_set_config_sync(
13569 struct dc_context *ctx,
13570 unsigned int link_index,
13571 struct set_config_cmd_payload *payload,
13572 enum set_config_status *operation_result)
13573 {
13574 struct amdgpu_device *adev = ctx->driver_context;
13575 bool is_cmd_complete;
13576 int ret;
13577
13578 mutex_lock(&adev->dm.dpia_aux_lock);
13579 is_cmd_complete = dc_process_dmub_set_config_async(ctx->dc,
13580 link_index, payload, adev->dm.dmub_notify);
13581
13582 if (is_cmd_complete || wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ)) {
13583 ret = 0;
13584 *operation_result = adev->dm.dmub_notify->sc_status;
13585 } else {
13586 drm_err(adev_to_drm(adev), "wait_for_completion_timeout timeout!");
13587 ret = -1;
13588 *operation_result = SET_CONFIG_UNKNOWN_ERROR;
13589 }
13590
13591 if (!is_cmd_complete)
13592 reinit_completion(&adev->dm.dmub_aux_transfer_done);
13593 mutex_unlock(&adev->dm.dpia_aux_lock);
13594 return ret;
13595 }
13596
dm_execute_dmub_cmd(const struct dc_context * ctx,union dmub_rb_cmd * cmd,enum dm_dmub_wait_type wait_type)13597 bool dm_execute_dmub_cmd(const struct dc_context *ctx, union dmub_rb_cmd *cmd, enum dm_dmub_wait_type wait_type)
13598 {
13599 return dc_dmub_srv_cmd_run(ctx->dmub_srv, cmd, wait_type);
13600 }
13601
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)13602 bool 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)
13603 {
13604 return dc_dmub_srv_cmd_run_list(ctx->dmub_srv, count, cmd, wait_type);
13605 }
13606
dm_acpi_process_phy_transition_interlock(const struct dc_context * ctx,struct dm_process_phy_transition_init_params process_phy_transition_init_params)13607 void dm_acpi_process_phy_transition_interlock(
13608 const struct dc_context *ctx,
13609 struct dm_process_phy_transition_init_params process_phy_transition_init_params)
13610 {
13611 // Not yet implemented
13612 }
13613