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 = region_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 if (sink->dc_edid.length == 0) {
3913 aconnector->drm_edid = NULL;
3914 hdmi_cec_unset_edid(aconnector);
3915 if (aconnector->dc_link->aux_mode) {
3916 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
3917 }
3918 } else {
3919 const struct edid *edid = (const struct edid *)sink->dc_edid.raw_edid;
3920
3921 aconnector->drm_edid = drm_edid_alloc(edid, sink->dc_edid.length);
3922 drm_edid_connector_update(connector, aconnector->drm_edid);
3923
3924 hdmi_cec_set_edid(aconnector);
3925 if (aconnector->dc_link->aux_mode)
3926 drm_dp_cec_attach(&aconnector->dm_dp_aux.aux,
3927 connector->display_info.source_physical_address);
3928 }
3929
3930 if (!aconnector->timing_requested) {
3931 aconnector->timing_requested =
3932 kzalloc_obj(struct dc_crtc_timing);
3933 if (!aconnector->timing_requested)
3934 drm_err(dev,
3935 "failed to create aconnector->requested_timing\n");
3936 }
3937
3938 amdgpu_dm_update_freesync_caps(connector, aconnector->drm_edid);
3939 update_connector_ext_caps(aconnector);
3940 } else {
3941 hdmi_cec_unset_edid(aconnector);
3942 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
3943 amdgpu_dm_update_freesync_caps(connector, NULL);
3944 aconnector->num_modes = 0;
3945 dc_sink_release(aconnector->dc_sink);
3946 aconnector->dc_sink = NULL;
3947 drm_edid_free(aconnector->drm_edid);
3948 aconnector->drm_edid = NULL;
3949 kfree(aconnector->timing_requested);
3950 aconnector->timing_requested = NULL;
3951 /* Set CP to DESIRED if it was ENABLED, so we can re-enable it again on hotplug */
3952 if (connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
3953 connector->state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
3954 }
3955
3956 update_subconnector_property(aconnector);
3957
3958 /* When polling, the mutex will be unlocked for us by DRM. */
3959 if (!drm_kms_helper_is_poll_worker())
3960 mutex_unlock(&dev->mode_config.mutex);
3961 }
3962
are_sinks_equal(const struct dc_sink * sink1,const struct dc_sink * sink2)3963 static bool are_sinks_equal(const struct dc_sink *sink1, const struct dc_sink *sink2)
3964 {
3965 if (!sink1 || !sink2)
3966 return false;
3967 if (sink1->sink_signal != sink2->sink_signal)
3968 return false;
3969
3970 if (sink1->dc_edid.length != sink2->dc_edid.length)
3971 return false;
3972
3973 if (memcmp(sink1->dc_edid.raw_edid, sink2->dc_edid.raw_edid,
3974 sink1->dc_edid.length) != 0)
3975 return false;
3976 return true;
3977 }
3978
3979
3980 /**
3981 * DOC: hdmi_hpd_debounce_work
3982 *
3983 * HDMI HPD debounce delay in milliseconds. When an HDMI display toggles HPD
3984 * (such as during power save transitions), this delay determines how long to
3985 * wait before processing the HPD event. This allows distinguishing between a
3986 * physical unplug (>hdmi_hpd_debounce_delay)
3987 * and a spontaneous RX HPD toggle (<hdmi_hpd_debounce_delay).
3988 *
3989 * If the toggle is less than this delay, the driver compares sink capabilities
3990 * and permits a hotplug event if they changed.
3991 *
3992 * The default value of 1500ms was chosen based on experimental testing with
3993 * various monitors that exhibit spontaneous HPD toggling behavior.
3994 */
hdmi_hpd_debounce_work(struct work_struct * work)3995 static void hdmi_hpd_debounce_work(struct work_struct *work)
3996 {
3997 struct amdgpu_dm_connector *aconnector =
3998 container_of(to_delayed_work(work), struct amdgpu_dm_connector,
3999 hdmi_hpd_debounce_work);
4000 struct drm_connector *connector = &aconnector->base;
4001 struct drm_device *dev = connector->dev;
4002 struct amdgpu_device *adev = drm_to_adev(dev);
4003 struct dc *dc = aconnector->dc_link->ctx->dc;
4004 bool fake_reconnect = false;
4005 bool reallow_idle = false;
4006 bool ret = false;
4007 guard(mutex)(&aconnector->hpd_lock);
4008
4009 /* Re-detect the display */
4010 scoped_guard(mutex, &adev->dm.dc_lock) {
4011 if (dc->caps.ips_support && dc->ctx->dmub_srv->idle_allowed) {
4012 dc_allow_idle_optimizations(dc, false);
4013 reallow_idle = true;
4014 }
4015 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
4016 }
4017
4018 if (ret) {
4019 /* Apply workaround delay for certain panels */
4020 apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink);
4021 /* Compare sinks to determine if this was a spontaneous HPD toggle */
4022 if (are_sinks_equal(aconnector->dc_link->local_sink, aconnector->hdmi_prev_sink)) {
4023 /*
4024 * Sinks match - this was a spontaneous HDMI HPD toggle.
4025 */
4026 drm_dbg_kms(dev, "HDMI HPD: Sink unchanged after debounce, internal re-enable\n");
4027 fake_reconnect = true;
4028 }
4029
4030 /* Update connector state */
4031 amdgpu_dm_update_connector_after_detect(aconnector);
4032
4033 drm_modeset_lock_all(dev);
4034 dm_restore_drm_connector_state(dev, connector);
4035 drm_modeset_unlock_all(dev);
4036
4037 /* Only notify OS if sink actually changed */
4038 if (!fake_reconnect && aconnector->base.force == DRM_FORCE_UNSPECIFIED)
4039 drm_kms_helper_hotplug_event(dev);
4040 }
4041
4042 /* Release the cached sink reference */
4043 if (aconnector->hdmi_prev_sink) {
4044 dc_sink_release(aconnector->hdmi_prev_sink);
4045 aconnector->hdmi_prev_sink = NULL;
4046 }
4047
4048 scoped_guard(mutex, &adev->dm.dc_lock) {
4049 if (reallow_idle && dc->caps.ips_support)
4050 dc_allow_idle_optimizations(dc, true);
4051 }
4052 }
4053
handle_hpd_irq_helper(struct amdgpu_dm_connector * aconnector)4054 static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector)
4055 {
4056 struct drm_connector *connector = &aconnector->base;
4057 struct drm_device *dev = connector->dev;
4058 enum dc_connection_type new_connection_type = dc_connection_none;
4059 struct amdgpu_device *adev = drm_to_adev(dev);
4060 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
4061 struct dc *dc = aconnector->dc_link->ctx->dc;
4062 bool ret = false;
4063 bool debounce_required = false;
4064
4065 if (adev->dm.disable_hpd_irq)
4066 return;
4067
4068 /*
4069 * In case of failure or MST no need to update connector status or notify the OS
4070 * since (for MST case) MST does this in its own context.
4071 */
4072 guard(mutex)(&aconnector->hpd_lock);
4073
4074 if (adev->dm.hdcp_workqueue) {
4075 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
4076 dm_con_state->update_hdcp = true;
4077 }
4078 if (aconnector->fake_enable)
4079 aconnector->fake_enable = false;
4080
4081 aconnector->timing_changed = false;
4082
4083 if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type))
4084 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
4085
4086 /*
4087 * Check for HDMI disconnect with debounce enabled.
4088 */
4089 debounce_required = (aconnector->hdmi_hpd_debounce_delay_ms > 0 &&
4090 dc_is_hdmi_signal(aconnector->dc_link->connector_signal) &&
4091 new_connection_type == dc_connection_none &&
4092 aconnector->dc_link->local_sink != NULL);
4093
4094 if (aconnector->base.force && new_connection_type == dc_connection_none) {
4095 emulated_link_detect(aconnector->dc_link);
4096
4097 drm_modeset_lock_all(dev);
4098 dm_restore_drm_connector_state(dev, connector);
4099 drm_modeset_unlock_all(dev);
4100
4101 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
4102 drm_kms_helper_connector_hotplug_event(connector);
4103 } else if (debounce_required) {
4104 /*
4105 * HDMI disconnect detected - schedule delayed work instead of
4106 * processing immediately. This allows us to coalesce spurious
4107 * HDMI signals from physical unplugs.
4108 */
4109 drm_dbg_kms(dev, "HDMI HPD: Disconnect detected, scheduling debounce work (%u ms)\n",
4110 aconnector->hdmi_hpd_debounce_delay_ms);
4111
4112 /* Cache the current sink for later comparison */
4113 if (aconnector->hdmi_prev_sink)
4114 dc_sink_release(aconnector->hdmi_prev_sink);
4115 aconnector->hdmi_prev_sink = aconnector->dc_link->local_sink;
4116 if (aconnector->hdmi_prev_sink)
4117 dc_sink_retain(aconnector->hdmi_prev_sink);
4118
4119 /* Schedule delayed detection. */
4120 if (mod_delayed_work(system_wq,
4121 &aconnector->hdmi_hpd_debounce_work,
4122 msecs_to_jiffies(aconnector->hdmi_hpd_debounce_delay_ms)))
4123 drm_dbg_kms(dev, "HDMI HPD: Re-scheduled debounce work\n");
4124
4125 } else {
4126
4127 /* If the aconnector->hdmi_hpd_debounce_work is scheduled, exit early */
4128 if (delayed_work_pending(&aconnector->hdmi_hpd_debounce_work))
4129 return;
4130
4131 scoped_guard(mutex, &adev->dm.dc_lock) {
4132 dc_exit_ips_for_hw_access(dc);
4133 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
4134 }
4135 if (ret) {
4136 /* w/a delay for certain panels */
4137 apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink);
4138 amdgpu_dm_update_connector_after_detect(aconnector);
4139
4140 drm_modeset_lock_all(dev);
4141 dm_restore_drm_connector_state(dev, connector);
4142 drm_modeset_unlock_all(dev);
4143
4144 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
4145 drm_kms_helper_connector_hotplug_event(connector);
4146 }
4147 }
4148 }
4149
handle_hpd_irq(void * param)4150 static void handle_hpd_irq(void *param)
4151 {
4152 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
4153
4154 handle_hpd_irq_helper(aconnector);
4155
4156 }
4157
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)4158 static void schedule_hpd_rx_offload_work(struct amdgpu_device *adev, struct hpd_rx_irq_offload_work_queue *offload_wq,
4159 union hpd_irq_data hpd_irq_data)
4160 {
4161 struct hpd_rx_irq_offload_work *offload_work = kzalloc_obj(*offload_work);
4162
4163 if (!offload_work) {
4164 drm_err(adev_to_drm(adev), "Failed to allocate hpd_rx_irq_offload_work.\n");
4165 return;
4166 }
4167
4168 INIT_WORK(&offload_work->work, dm_handle_hpd_rx_offload_work);
4169 offload_work->data = hpd_irq_data;
4170 offload_work->offload_wq = offload_wq;
4171 offload_work->adev = adev;
4172
4173 queue_work(offload_wq->wq, &offload_work->work);
4174 drm_dbg_kms(adev_to_drm(adev), "queue work to handle hpd_rx offload work");
4175 }
4176
handle_hpd_rx_irq(void * param)4177 static void handle_hpd_rx_irq(void *param)
4178 {
4179 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
4180 struct drm_connector *connector = &aconnector->base;
4181 struct drm_device *dev = connector->dev;
4182 struct dc_link *dc_link = aconnector->dc_link;
4183 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
4184 bool result = false;
4185 enum dc_connection_type new_connection_type = dc_connection_none;
4186 struct amdgpu_device *adev = drm_to_adev(dev);
4187 union hpd_irq_data hpd_irq_data;
4188 bool link_loss = false;
4189 bool has_left_work = false;
4190 int idx = dc_link->link_index;
4191 struct hpd_rx_irq_offload_work_queue *offload_wq = &adev->dm.hpd_rx_offload_wq[idx];
4192 struct dc *dc = aconnector->dc_link->ctx->dc;
4193
4194 memset(&hpd_irq_data, 0, sizeof(hpd_irq_data));
4195
4196 if (adev->dm.disable_hpd_irq)
4197 return;
4198
4199 /*
4200 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio
4201 * conflict, after implement i2c helper, this mutex should be
4202 * retired.
4203 */
4204 mutex_lock(&aconnector->hpd_lock);
4205
4206 result = dc_link_handle_hpd_rx_irq(dc_link, &hpd_irq_data,
4207 &link_loss, true, &has_left_work);
4208
4209 if (!has_left_work)
4210 goto out;
4211
4212 if (hpd_irq_data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
4213 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
4214 goto out;
4215 }
4216
4217 if (dc_link_dp_allow_hpd_rx_irq(dc_link)) {
4218 if (hpd_irq_data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY ||
4219 hpd_irq_data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) {
4220 bool skip = false;
4221
4222 /*
4223 * DOWN_REP_MSG_RDY is also handled by polling method
4224 * mgr->cbs->poll_hpd_irq()
4225 */
4226 spin_lock(&offload_wq->offload_lock);
4227 skip = offload_wq->is_handling_mst_msg_rdy_event;
4228
4229 if (!skip)
4230 offload_wq->is_handling_mst_msg_rdy_event = true;
4231
4232 spin_unlock(&offload_wq->offload_lock);
4233
4234 if (!skip)
4235 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
4236
4237 goto out;
4238 }
4239
4240 if (link_loss) {
4241 bool skip = false;
4242
4243 spin_lock(&offload_wq->offload_lock);
4244 skip = offload_wq->is_handling_link_loss;
4245
4246 if (!skip)
4247 offload_wq->is_handling_link_loss = true;
4248
4249 spin_unlock(&offload_wq->offload_lock);
4250
4251 if (!skip)
4252 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
4253
4254 goto out;
4255 }
4256 }
4257
4258 out:
4259 if (result && !is_mst_root_connector) {
4260 /* Downstream Port status changed. */
4261 if (!dc_link_detect_connection_type(dc_link, &new_connection_type))
4262 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
4263
4264 if (aconnector->base.force && new_connection_type == dc_connection_none) {
4265 emulated_link_detect(dc_link);
4266
4267 if (aconnector->fake_enable)
4268 aconnector->fake_enable = false;
4269
4270 amdgpu_dm_update_connector_after_detect(aconnector);
4271
4272
4273 drm_modeset_lock_all(dev);
4274 dm_restore_drm_connector_state(dev, connector);
4275 drm_modeset_unlock_all(dev);
4276
4277 drm_kms_helper_connector_hotplug_event(connector);
4278 } else {
4279 bool ret = false;
4280
4281 mutex_lock(&adev->dm.dc_lock);
4282 dc_exit_ips_for_hw_access(dc);
4283 ret = dc_link_detect(dc_link, DETECT_REASON_HPDRX);
4284 mutex_unlock(&adev->dm.dc_lock);
4285
4286 if (ret) {
4287 if (aconnector->fake_enable)
4288 aconnector->fake_enable = false;
4289
4290 amdgpu_dm_update_connector_after_detect(aconnector);
4291
4292 drm_modeset_lock_all(dev);
4293 dm_restore_drm_connector_state(dev, connector);
4294 drm_modeset_unlock_all(dev);
4295
4296 drm_kms_helper_connector_hotplug_event(connector);
4297 }
4298 }
4299 }
4300 if (hpd_irq_data.bytes.device_service_irq.bits.CP_IRQ) {
4301 if (adev->dm.hdcp_workqueue)
4302 hdcp_handle_cpirq(adev->dm.hdcp_workqueue, aconnector->base.index);
4303 }
4304
4305 if (dc_link->type != dc_connection_mst_branch)
4306 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux);
4307
4308 mutex_unlock(&aconnector->hpd_lock);
4309 }
4310
register_hpd_handlers(struct amdgpu_device * adev)4311 static int register_hpd_handlers(struct amdgpu_device *adev)
4312 {
4313 struct drm_device *dev = adev_to_drm(adev);
4314 struct drm_connector *connector;
4315 struct amdgpu_dm_connector *aconnector;
4316 const struct dc_link *dc_link;
4317 struct dc_interrupt_params int_params = {0};
4318
4319 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4320 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4321
4322 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
4323 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD,
4324 dmub_hpd_callback, true)) {
4325 drm_err(adev_to_drm(adev), "fail to register dmub hpd callback");
4326 return -EINVAL;
4327 }
4328
4329 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_IRQ,
4330 dmub_hpd_callback, true)) {
4331 drm_err(adev_to_drm(adev), "fail to register dmub hpd callback");
4332 return -EINVAL;
4333 }
4334
4335 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_SENSE_NOTIFY,
4336 dmub_hpd_sense_callback, true)) {
4337 drm_err(adev_to_drm(adev), "fail to register dmub hpd sense callback");
4338 return -EINVAL;
4339 }
4340 }
4341
4342 list_for_each_entry(connector,
4343 &dev->mode_config.connector_list, head) {
4344
4345 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
4346 continue;
4347
4348 aconnector = to_amdgpu_dm_connector(connector);
4349 dc_link = aconnector->dc_link;
4350
4351 if (dc_link->irq_source_hpd != DC_IRQ_SOURCE_INVALID) {
4352 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
4353 int_params.irq_source = dc_link->irq_source_hpd;
4354
4355 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4356 int_params.irq_source < DC_IRQ_SOURCE_HPD1 ||
4357 int_params.irq_source > DC_IRQ_SOURCE_HPD6) {
4358 drm_err(adev_to_drm(adev), "Failed to register hpd irq!\n");
4359 return -EINVAL;
4360 }
4361
4362 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4363 handle_hpd_irq, (void *) aconnector))
4364 return -ENOMEM;
4365 }
4366
4367 if (dc_link->irq_source_hpd_rx != DC_IRQ_SOURCE_INVALID) {
4368
4369 /* Also register for DP short pulse (hpd_rx). */
4370 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
4371 int_params.irq_source = dc_link->irq_source_hpd_rx;
4372
4373 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4374 int_params.irq_source < DC_IRQ_SOURCE_HPD1RX ||
4375 int_params.irq_source > DC_IRQ_SOURCE_HPD6RX) {
4376 drm_err(adev_to_drm(adev), "Failed to register hpd rx irq!\n");
4377 return -EINVAL;
4378 }
4379
4380 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4381 handle_hpd_rx_irq, (void *) aconnector))
4382 return -ENOMEM;
4383 }
4384 }
4385 return 0;
4386 }
4387
4388 #if defined(CONFIG_DRM_AMD_DC_SI)
4389 /* Register IRQ sources and initialize IRQ callbacks */
dce60_register_irq_handlers(struct amdgpu_device * adev)4390 static int dce60_register_irq_handlers(struct amdgpu_device *adev)
4391 {
4392 struct dc *dc = adev->dm.dc;
4393 struct common_irq_params *c_irq_params;
4394 struct dc_interrupt_params int_params = {0};
4395 int r;
4396 int i;
4397 unsigned int client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
4398
4399 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4400 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4401
4402 /*
4403 * Actions of amdgpu_irq_add_id():
4404 * 1. Register a set() function with base driver.
4405 * Base driver will call set() function to enable/disable an
4406 * interrupt in DC hardware.
4407 * 2. Register amdgpu_dm_irq_handler().
4408 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
4409 * coming from DC hardware.
4410 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
4411 * for acknowledging and handling.
4412 */
4413
4414 /* Use VBLANK interrupt */
4415 for (i = 0; i < adev->mode_info.num_crtc; i++) {
4416 r = amdgpu_irq_add_id(adev, client_id, i + 1, &adev->crtc_irq);
4417 if (r) {
4418 drm_err(adev_to_drm(adev), "Failed to add crtc irq id!\n");
4419 return r;
4420 }
4421
4422 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4423 int_params.irq_source =
4424 dc_interrupt_to_irq_source(dc, i + 1, 0);
4425
4426 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4427 int_params.irq_source < DC_IRQ_SOURCE_VBLANK1 ||
4428 int_params.irq_source > DC_IRQ_SOURCE_VBLANK6) {
4429 drm_err(adev_to_drm(adev), "Failed to register vblank irq!\n");
4430 return -EINVAL;
4431 }
4432
4433 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
4434
4435 c_irq_params->adev = adev;
4436 c_irq_params->irq_src = int_params.irq_source;
4437
4438 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4439 dm_crtc_high_irq, c_irq_params))
4440 return -ENOMEM;
4441 }
4442
4443 /* Use GRPH_PFLIP interrupt */
4444 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
4445 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
4446 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
4447 if (r) {
4448 drm_err(adev_to_drm(adev), "Failed to add page flip irq id!\n");
4449 return r;
4450 }
4451
4452 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4453 int_params.irq_source =
4454 dc_interrupt_to_irq_source(dc, i, 0);
4455
4456 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4457 int_params.irq_source < DC_IRQ_SOURCE_PFLIP_FIRST ||
4458 int_params.irq_source > DC_IRQ_SOURCE_PFLIP_LAST) {
4459 drm_err(adev_to_drm(adev), "Failed to register pflip irq!\n");
4460 return -EINVAL;
4461 }
4462
4463 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
4464
4465 c_irq_params->adev = adev;
4466 c_irq_params->irq_src = int_params.irq_source;
4467
4468 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4469 dm_pflip_high_irq, c_irq_params))
4470 return -ENOMEM;
4471 }
4472
4473 /* HPD */
4474 r = amdgpu_irq_add_id(adev, client_id,
4475 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
4476 if (r) {
4477 drm_err(adev_to_drm(adev), "Failed to add hpd irq id!\n");
4478 return r;
4479 }
4480
4481 r = register_hpd_handlers(adev);
4482
4483 return r;
4484 }
4485 #endif
4486
4487 /* Register IRQ sources and initialize IRQ callbacks */
dce110_register_irq_handlers(struct amdgpu_device * adev)4488 static int dce110_register_irq_handlers(struct amdgpu_device *adev)
4489 {
4490 struct dc *dc = adev->dm.dc;
4491 struct common_irq_params *c_irq_params;
4492 struct dc_interrupt_params int_params = {0};
4493 int r;
4494 int i;
4495 unsigned int client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
4496
4497 if (adev->family >= AMDGPU_FAMILY_AI)
4498 client_id = SOC15_IH_CLIENTID_DCE;
4499
4500 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4501 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4502
4503 /*
4504 * Actions of amdgpu_irq_add_id():
4505 * 1. Register a set() function with base driver.
4506 * Base driver will call set() function to enable/disable an
4507 * interrupt in DC hardware.
4508 * 2. Register amdgpu_dm_irq_handler().
4509 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
4510 * coming from DC hardware.
4511 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
4512 * for acknowledging and handling.
4513 */
4514
4515 /* Use VBLANK interrupt */
4516 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
4517 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
4518 if (r) {
4519 drm_err(adev_to_drm(adev), "Failed to add crtc irq id!\n");
4520 return r;
4521 }
4522
4523 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4524 int_params.irq_source =
4525 dc_interrupt_to_irq_source(dc, i, 0);
4526
4527 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4528 int_params.irq_source < DC_IRQ_SOURCE_VBLANK1 ||
4529 int_params.irq_source > DC_IRQ_SOURCE_VBLANK6) {
4530 drm_err(adev_to_drm(adev), "Failed to register vblank irq!\n");
4531 return -EINVAL;
4532 }
4533
4534 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
4535
4536 c_irq_params->adev = adev;
4537 c_irq_params->irq_src = int_params.irq_source;
4538
4539 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4540 dm_crtc_high_irq, c_irq_params))
4541 return -ENOMEM;
4542 }
4543
4544 /* Use VUPDATE interrupt */
4545 for (i = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT; i <= VISLANDS30_IV_SRCID_D6_V_UPDATE_INT; i += 2) {
4546 r = amdgpu_irq_add_id(adev, client_id, i, &adev->vupdate_irq);
4547 if (r) {
4548 drm_err(adev_to_drm(adev), "Failed to add vupdate irq id!\n");
4549 return r;
4550 }
4551
4552 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4553 int_params.irq_source =
4554 dc_interrupt_to_irq_source(dc, i, 0);
4555
4556 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4557 int_params.irq_source < DC_IRQ_SOURCE_VUPDATE1 ||
4558 int_params.irq_source > DC_IRQ_SOURCE_VUPDATE6) {
4559 drm_err(adev_to_drm(adev), "Failed to register vupdate irq!\n");
4560 return -EINVAL;
4561 }
4562
4563 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
4564
4565 c_irq_params->adev = adev;
4566 c_irq_params->irq_src = int_params.irq_source;
4567
4568 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4569 dm_vupdate_high_irq, c_irq_params))
4570 return -ENOMEM;
4571 }
4572
4573 /* Use GRPH_PFLIP interrupt */
4574 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
4575 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
4576 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
4577 if (r) {
4578 drm_err(adev_to_drm(adev), "Failed to add page flip irq id!\n");
4579 return r;
4580 }
4581
4582 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4583 int_params.irq_source =
4584 dc_interrupt_to_irq_source(dc, i, 0);
4585
4586 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4587 int_params.irq_source < DC_IRQ_SOURCE_PFLIP_FIRST ||
4588 int_params.irq_source > DC_IRQ_SOURCE_PFLIP_LAST) {
4589 drm_err(adev_to_drm(adev), "Failed to register pflip irq!\n");
4590 return -EINVAL;
4591 }
4592
4593 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
4594
4595 c_irq_params->adev = adev;
4596 c_irq_params->irq_src = int_params.irq_source;
4597
4598 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4599 dm_pflip_high_irq, c_irq_params))
4600 return -ENOMEM;
4601 }
4602
4603 /* HPD */
4604 r = amdgpu_irq_add_id(adev, client_id,
4605 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
4606 if (r) {
4607 drm_err(adev_to_drm(adev), "Failed to add hpd irq id!\n");
4608 return r;
4609 }
4610
4611 r = register_hpd_handlers(adev);
4612
4613 return r;
4614 }
4615
4616 /* Register IRQ sources and initialize IRQ callbacks */
dcn10_register_irq_handlers(struct amdgpu_device * adev)4617 static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
4618 {
4619 struct dc *dc = adev->dm.dc;
4620 struct common_irq_params *c_irq_params;
4621 struct dc_interrupt_params int_params = {0};
4622 int r;
4623 int i;
4624 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
4625 static const unsigned int vrtl_int_srcid[] = {
4626 DCN_1_0__SRCID__OTG1_VERTICAL_INTERRUPT0_CONTROL,
4627 DCN_1_0__SRCID__OTG2_VERTICAL_INTERRUPT0_CONTROL,
4628 DCN_1_0__SRCID__OTG3_VERTICAL_INTERRUPT0_CONTROL,
4629 DCN_1_0__SRCID__OTG4_VERTICAL_INTERRUPT0_CONTROL,
4630 DCN_1_0__SRCID__OTG5_VERTICAL_INTERRUPT0_CONTROL,
4631 DCN_1_0__SRCID__OTG6_VERTICAL_INTERRUPT0_CONTROL
4632 };
4633 #endif
4634
4635 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4636 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4637
4638 /*
4639 * Actions of amdgpu_irq_add_id():
4640 * 1. Register a set() function with base driver.
4641 * Base driver will call set() function to enable/disable an
4642 * interrupt in DC hardware.
4643 * 2. Register amdgpu_dm_irq_handler().
4644 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
4645 * coming from DC hardware.
4646 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
4647 * for acknowledging and handling.
4648 */
4649
4650 /* Use VSTARTUP interrupt */
4651 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
4652 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
4653 i++) {
4654 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
4655
4656 if (r) {
4657 drm_err(adev_to_drm(adev), "Failed to add crtc irq id!\n");
4658 return r;
4659 }
4660
4661 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4662 int_params.irq_source =
4663 dc_interrupt_to_irq_source(dc, i, 0);
4664
4665 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4666 int_params.irq_source < DC_IRQ_SOURCE_VBLANK1 ||
4667 int_params.irq_source > DC_IRQ_SOURCE_VBLANK6) {
4668 drm_err(adev_to_drm(adev), "Failed to register vblank irq!\n");
4669 return -EINVAL;
4670 }
4671
4672 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
4673
4674 c_irq_params->adev = adev;
4675 c_irq_params->irq_src = int_params.irq_source;
4676
4677 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4678 dm_crtc_high_irq, c_irq_params))
4679 return -ENOMEM;
4680 }
4681
4682 /* Use otg vertical line interrupt */
4683 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
4684 for (i = 0; i <= adev->mode_info.num_crtc - 1; i++) {
4685 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE,
4686 vrtl_int_srcid[i], &adev->vline0_irq);
4687
4688 if (r) {
4689 drm_err(adev_to_drm(adev), "Failed to add vline0 irq id!\n");
4690 return r;
4691 }
4692
4693 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4694 int_params.irq_source =
4695 dc_interrupt_to_irq_source(dc, vrtl_int_srcid[i], 0);
4696
4697 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4698 int_params.irq_source < DC_IRQ_SOURCE_DC1_VLINE0 ||
4699 int_params.irq_source > DC_IRQ_SOURCE_DC6_VLINE0) {
4700 drm_err(adev_to_drm(adev), "Failed to register vline0 irq!\n");
4701 return -EINVAL;
4702 }
4703
4704 c_irq_params = &adev->dm.vline0_params[int_params.irq_source
4705 - DC_IRQ_SOURCE_DC1_VLINE0];
4706
4707 c_irq_params->adev = adev;
4708 c_irq_params->irq_src = int_params.irq_source;
4709
4710 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4711 dm_dcn_vertical_interrupt0_high_irq,
4712 c_irq_params))
4713 return -ENOMEM;
4714 }
4715 #endif
4716
4717 /* Use VUPDATE_NO_LOCK interrupt on DCN, which seems to correspond to
4718 * the regular VUPDATE interrupt on DCE. We want DC_IRQ_SOURCE_VUPDATEx
4719 * to trigger at end of each vblank, regardless of state of the lock,
4720 * matching DCE behaviour.
4721 */
4722 for (i = DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT;
4723 i <= DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT + adev->mode_info.num_crtc - 1;
4724 i++) {
4725 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->vupdate_irq);
4726
4727 if (r) {
4728 drm_err(adev_to_drm(adev), "Failed to add vupdate irq id!\n");
4729 return r;
4730 }
4731
4732 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4733 int_params.irq_source =
4734 dc_interrupt_to_irq_source(dc, i, 0);
4735
4736 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4737 int_params.irq_source < DC_IRQ_SOURCE_VUPDATE1 ||
4738 int_params.irq_source > DC_IRQ_SOURCE_VUPDATE6) {
4739 drm_err(adev_to_drm(adev), "Failed to register vupdate irq!\n");
4740 return -EINVAL;
4741 }
4742
4743 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
4744
4745 c_irq_params->adev = adev;
4746 c_irq_params->irq_src = int_params.irq_source;
4747
4748 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4749 dm_vupdate_high_irq, c_irq_params))
4750 return -ENOMEM;
4751 }
4752
4753 /* Use GRPH_PFLIP interrupt */
4754 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
4755 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + dc->caps.max_otg_num - 1;
4756 i++) {
4757 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
4758 if (r) {
4759 drm_err(adev_to_drm(adev), "Failed to add page flip irq id!\n");
4760 return r;
4761 }
4762
4763 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4764 int_params.irq_source =
4765 dc_interrupt_to_irq_source(dc, i, 0);
4766
4767 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4768 int_params.irq_source < DC_IRQ_SOURCE_PFLIP_FIRST ||
4769 int_params.irq_source > DC_IRQ_SOURCE_PFLIP_LAST) {
4770 drm_err(adev_to_drm(adev), "Failed to register pflip irq!\n");
4771 return -EINVAL;
4772 }
4773
4774 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
4775
4776 c_irq_params->adev = adev;
4777 c_irq_params->irq_src = int_params.irq_source;
4778
4779 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4780 dm_pflip_high_irq, c_irq_params))
4781 return -ENOMEM;
4782 }
4783
4784 /* HPD */
4785 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
4786 &adev->hpd_irq);
4787 if (r) {
4788 drm_err(adev_to_drm(adev), "Failed to add hpd irq id!\n");
4789 return r;
4790 }
4791
4792 r = register_hpd_handlers(adev);
4793
4794 return r;
4795 }
4796 /* Register Outbox IRQ sources and initialize IRQ callbacks */
register_outbox_irq_handlers(struct amdgpu_device * adev)4797 static int register_outbox_irq_handlers(struct amdgpu_device *adev)
4798 {
4799 struct dc *dc = adev->dm.dc;
4800 struct common_irq_params *c_irq_params;
4801 struct dc_interrupt_params int_params = {0};
4802 int r, i;
4803
4804 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4805 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4806
4807 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT,
4808 &adev->dmub_outbox_irq);
4809 if (r) {
4810 drm_err(adev_to_drm(adev), "Failed to add outbox irq id!\n");
4811 return r;
4812 }
4813
4814 if (dc->ctx->dmub_srv) {
4815 i = DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT;
4816 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
4817 int_params.irq_source =
4818 dc_interrupt_to_irq_source(dc, i, 0);
4819
4820 c_irq_params = &adev->dm.dmub_outbox_params[0];
4821
4822 c_irq_params->adev = adev;
4823 c_irq_params->irq_src = int_params.irq_source;
4824
4825 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4826 dm_dmub_outbox1_low_irq, c_irq_params))
4827 return -ENOMEM;
4828 }
4829
4830 return 0;
4831 }
4832
4833 /*
4834 * Acquires the lock for the atomic state object and returns
4835 * the new atomic state.
4836 *
4837 * This should only be called during atomic check.
4838 */
dm_atomic_get_state(struct drm_atomic_state * state,struct dm_atomic_state ** dm_state)4839 int dm_atomic_get_state(struct drm_atomic_state *state,
4840 struct dm_atomic_state **dm_state)
4841 {
4842 struct drm_device *dev = state->dev;
4843 struct amdgpu_device *adev = drm_to_adev(dev);
4844 struct amdgpu_display_manager *dm = &adev->dm;
4845 struct drm_private_state *priv_state;
4846
4847 if (*dm_state)
4848 return 0;
4849
4850 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj);
4851 if (IS_ERR(priv_state))
4852 return PTR_ERR(priv_state);
4853
4854 *dm_state = to_dm_atomic_state(priv_state);
4855
4856 return 0;
4857 }
4858
4859 static struct dm_atomic_state *
dm_atomic_get_new_state(struct drm_atomic_state * state)4860 dm_atomic_get_new_state(struct drm_atomic_state *state)
4861 {
4862 struct drm_device *dev = state->dev;
4863 struct amdgpu_device *adev = drm_to_adev(dev);
4864 struct amdgpu_display_manager *dm = &adev->dm;
4865 struct drm_private_obj *obj;
4866 struct drm_private_state *new_obj_state;
4867 int i;
4868
4869 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) {
4870 if (obj->funcs == dm->atomic_obj.funcs)
4871 return to_dm_atomic_state(new_obj_state);
4872 }
4873
4874 return NULL;
4875 }
4876
4877 static struct drm_private_state *
dm_atomic_duplicate_state(struct drm_private_obj * obj)4878 dm_atomic_duplicate_state(struct drm_private_obj *obj)
4879 {
4880 struct dm_atomic_state *old_state, *new_state;
4881
4882 new_state = kzalloc_obj(*new_state);
4883 if (!new_state)
4884 return NULL;
4885
4886 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base);
4887
4888 old_state = to_dm_atomic_state(obj->state);
4889
4890 if (old_state && old_state->context)
4891 new_state->context = dc_state_create_copy(old_state->context);
4892
4893 if (!new_state->context) {
4894 kfree(new_state);
4895 return NULL;
4896 }
4897
4898 return &new_state->base;
4899 }
4900
dm_atomic_destroy_state(struct drm_private_obj * obj,struct drm_private_state * state)4901 static void dm_atomic_destroy_state(struct drm_private_obj *obj,
4902 struct drm_private_state *state)
4903 {
4904 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
4905
4906 if (dm_state && dm_state->context)
4907 dc_state_release(dm_state->context);
4908
4909 kfree(dm_state);
4910 }
4911
4912 static struct drm_private_state_funcs dm_atomic_state_funcs = {
4913 .atomic_duplicate_state = dm_atomic_duplicate_state,
4914 .atomic_destroy_state = dm_atomic_destroy_state,
4915 };
4916
amdgpu_dm_mode_config_init(struct amdgpu_device * adev)4917 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
4918 {
4919 struct dm_atomic_state *state;
4920 int r;
4921
4922 adev->mode_info.mode_config_initialized = true;
4923
4924 adev_to_drm(adev)->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
4925 adev_to_drm(adev)->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
4926
4927 adev_to_drm(adev)->mode_config.max_width = 16384;
4928 adev_to_drm(adev)->mode_config.max_height = 16384;
4929
4930 adev_to_drm(adev)->mode_config.preferred_depth = 24;
4931 if (adev->asic_type == CHIP_HAWAII)
4932 /* disable prefer shadow for now due to hibernation issues */
4933 adev_to_drm(adev)->mode_config.prefer_shadow = 0;
4934 else
4935 adev_to_drm(adev)->mode_config.prefer_shadow = 1;
4936 /* indicates support for immediate flip */
4937 adev_to_drm(adev)->mode_config.async_page_flip = true;
4938
4939 state = kzalloc_obj(*state);
4940 if (!state)
4941 return -ENOMEM;
4942
4943 state->context = dc_state_create_current_copy(adev->dm.dc);
4944 if (!state->context) {
4945 kfree(state);
4946 return -ENOMEM;
4947 }
4948
4949 drm_atomic_private_obj_init(adev_to_drm(adev),
4950 &adev->dm.atomic_obj,
4951 &state->base,
4952 &dm_atomic_state_funcs);
4953
4954 r = amdgpu_display_modeset_create_props(adev);
4955 if (r) {
4956 dc_state_release(state->context);
4957 kfree(state);
4958 return r;
4959 }
4960
4961 #ifdef AMD_PRIVATE_COLOR
4962 if (amdgpu_dm_create_color_properties(adev)) {
4963 dc_state_release(state->context);
4964 kfree(state);
4965 return -ENOMEM;
4966 }
4967 #endif
4968
4969 r = amdgpu_dm_audio_init(adev);
4970 if (r) {
4971 dc_state_release(state->context);
4972 kfree(state);
4973 return r;
4974 }
4975
4976 return 0;
4977 }
4978
4979 #define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12
4980 #define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255
4981 #define AMDGPU_DM_MIN_SPREAD ((AMDGPU_DM_DEFAULT_MAX_BACKLIGHT - AMDGPU_DM_DEFAULT_MIN_BACKLIGHT) / 2)
4982 #define AUX_BL_DEFAULT_TRANSITION_TIME_MS 50
4983
amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager * dm,int bl_idx)4984 static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm,
4985 int bl_idx)
4986 {
4987 struct amdgpu_dm_backlight_caps *caps = &dm->backlight_caps[bl_idx];
4988
4989 if (caps->caps_valid)
4990 return;
4991
4992 #if defined(CONFIG_ACPI)
4993 amdgpu_acpi_get_backlight_caps(caps);
4994
4995 /* validate the firmware value is sane */
4996 if (caps->caps_valid) {
4997 int spread = caps->max_input_signal - caps->min_input_signal;
4998
4999 if (caps->max_input_signal > AMDGPU_DM_DEFAULT_MAX_BACKLIGHT ||
5000 caps->min_input_signal < 0 ||
5001 spread > AMDGPU_DM_DEFAULT_MAX_BACKLIGHT ||
5002 spread < AMDGPU_DM_MIN_SPREAD) {
5003 drm_dbg_kms(adev_to_drm(dm->adev), "DM: Invalid backlight caps: min=%d, max=%d\n",
5004 caps->min_input_signal, caps->max_input_signal);
5005 caps->caps_valid = false;
5006 }
5007 }
5008
5009 if (!caps->caps_valid) {
5010 caps->min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
5011 caps->max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
5012 caps->caps_valid = true;
5013 }
5014 #else
5015 if (caps->aux_support)
5016 return;
5017
5018 caps->min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
5019 caps->max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
5020 caps->caps_valid = true;
5021 #endif
5022 }
5023
get_brightness_range(const struct amdgpu_dm_backlight_caps * caps,unsigned int * min,unsigned int * max)5024 static int get_brightness_range(const struct amdgpu_dm_backlight_caps *caps,
5025 unsigned int *min, unsigned int *max)
5026 {
5027 if (!caps)
5028 return 0;
5029
5030 if (caps->aux_support) {
5031 // Firmware limits are in nits, DC API wants millinits.
5032 *max = 1000 * caps->aux_max_input_signal;
5033 *min = 1000 * caps->aux_min_input_signal;
5034 } else {
5035 // Firmware limits are 8-bit, PWM control is 16-bit.
5036 *max = 0x101 * caps->max_input_signal;
5037 *min = 0x101 * caps->min_input_signal;
5038 }
5039 return 1;
5040 }
5041
5042 /* Rescale from [min..max] to [0..AMDGPU_MAX_BL_LEVEL] */
scale_input_to_fw(int min,int max,u64 input)5043 static inline u32 scale_input_to_fw(int min, int max, u64 input)
5044 {
5045 return DIV_ROUND_CLOSEST_ULL(input * AMDGPU_MAX_BL_LEVEL, max - min);
5046 }
5047
5048 /* Rescale from [0..AMDGPU_MAX_BL_LEVEL] to [min..max] */
scale_fw_to_input(int min,int max,u64 input)5049 static inline u32 scale_fw_to_input(int min, int max, u64 input)
5050 {
5051 return min + DIV_ROUND_CLOSEST_ULL(input * (max - min), AMDGPU_MAX_BL_LEVEL);
5052 }
5053
convert_custom_brightness(const struct amdgpu_dm_backlight_caps * caps,unsigned int min,unsigned int max,uint32_t * user_brightness)5054 static void convert_custom_brightness(const struct amdgpu_dm_backlight_caps *caps,
5055 unsigned int min, unsigned int max,
5056 uint32_t *user_brightness)
5057 {
5058 u32 brightness = scale_input_to_fw(min, max, *user_brightness);
5059 u8 lower_signal, upper_signal, upper_lum, lower_lum, lum;
5060 int left, right;
5061
5062 if (amdgpu_dc_debug_mask & DC_DISABLE_CUSTOM_BRIGHTNESS_CURVE)
5063 return;
5064
5065 if (!caps->data_points)
5066 return;
5067
5068 /*
5069 * Handle the case where brightness is below the first data point
5070 * Interpolate between (0,0) and (first_signal, first_lum)
5071 */
5072 if (brightness < caps->luminance_data[0].input_signal) {
5073 lum = DIV_ROUND_CLOSEST(caps->luminance_data[0].luminance * brightness,
5074 caps->luminance_data[0].input_signal);
5075 goto scale;
5076 }
5077
5078 left = 0;
5079 right = caps->data_points - 1;
5080 while (left <= right) {
5081 int mid = left + (right - left) / 2;
5082 u8 signal = caps->luminance_data[mid].input_signal;
5083
5084 /* Exact match found */
5085 if (signal == brightness) {
5086 lum = caps->luminance_data[mid].luminance;
5087 goto scale;
5088 }
5089
5090 if (signal < brightness)
5091 left = mid + 1;
5092 else
5093 right = mid - 1;
5094 }
5095
5096 /* verify bound */
5097 if (left >= caps->data_points)
5098 left = caps->data_points - 1;
5099
5100 /* At this point, left > right */
5101 lower_signal = caps->luminance_data[right].input_signal;
5102 upper_signal = caps->luminance_data[left].input_signal;
5103 lower_lum = caps->luminance_data[right].luminance;
5104 upper_lum = caps->luminance_data[left].luminance;
5105
5106 /* interpolate */
5107 if (right == left || !lower_lum)
5108 lum = upper_lum;
5109 else
5110 lum = lower_lum + DIV_ROUND_CLOSEST((upper_lum - lower_lum) *
5111 (brightness - lower_signal),
5112 upper_signal - lower_signal);
5113 scale:
5114 *user_brightness = scale_fw_to_input(min, max,
5115 DIV_ROUND_CLOSEST(lum * brightness, 101));
5116 }
5117
convert_brightness_from_user(const struct amdgpu_dm_backlight_caps * caps,uint32_t brightness)5118 static u32 convert_brightness_from_user(const struct amdgpu_dm_backlight_caps *caps,
5119 uint32_t brightness)
5120 {
5121 unsigned int min, max;
5122
5123 if (!get_brightness_range(caps, &min, &max))
5124 return brightness;
5125
5126 convert_custom_brightness(caps, min, max, &brightness);
5127
5128 // Rescale 0..max to min..max
5129 return min + DIV_ROUND_CLOSEST_ULL((u64)(max - min) * brightness, max);
5130 }
5131
convert_brightness_to_user(const struct amdgpu_dm_backlight_caps * caps,uint32_t brightness)5132 static u32 convert_brightness_to_user(const struct amdgpu_dm_backlight_caps *caps,
5133 uint32_t brightness)
5134 {
5135 unsigned int min, max;
5136
5137 if (!get_brightness_range(caps, &min, &max))
5138 return brightness;
5139
5140 if (brightness < min)
5141 return 0;
5142 // Rescale min..max to 0..max
5143 return DIV_ROUND_CLOSEST_ULL((u64)max * (brightness - min),
5144 max - min);
5145 }
5146
amdgpu_dm_backlight_set_level(struct amdgpu_display_manager * dm,int bl_idx,u32 user_brightness)5147 static void amdgpu_dm_backlight_set_level(struct amdgpu_display_manager *dm,
5148 int bl_idx,
5149 u32 user_brightness)
5150 {
5151 struct amdgpu_dm_backlight_caps *caps;
5152 struct dc_link *link;
5153 u32 brightness;
5154 bool rc, reallow_idle = false;
5155 struct drm_connector *connector;
5156
5157 list_for_each_entry(connector, &dm->ddev->mode_config.connector_list, head) {
5158 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
5159
5160 if (aconnector->bl_idx != bl_idx)
5161 continue;
5162
5163 /* if connector is off, save the brightness for next time it's on */
5164 if (!aconnector->base.encoder) {
5165 dm->brightness[bl_idx] = user_brightness;
5166 dm->actual_brightness[bl_idx] = 0;
5167 return;
5168 }
5169 }
5170
5171 amdgpu_dm_update_backlight_caps(dm, bl_idx);
5172 caps = &dm->backlight_caps[bl_idx];
5173
5174 dm->brightness[bl_idx] = user_brightness;
5175 /* update scratch register */
5176 if (bl_idx == 0)
5177 amdgpu_atombios_scratch_regs_set_backlight_level(dm->adev, dm->brightness[bl_idx]);
5178 brightness = convert_brightness_from_user(caps, dm->brightness[bl_idx]);
5179 link = (struct dc_link *)dm->backlight_link[bl_idx];
5180
5181 /* Apply brightness quirk */
5182 if (caps->brightness_mask)
5183 brightness |= caps->brightness_mask;
5184
5185 /* Change brightness based on AUX property */
5186 mutex_lock(&dm->dc_lock);
5187 if (dm->dc->caps.ips_support && dm->dc->ctx->dmub_srv->idle_allowed) {
5188 dc_allow_idle_optimizations(dm->dc, false);
5189 reallow_idle = true;
5190 }
5191
5192 if (trace_amdgpu_dm_brightness_enabled()) {
5193 trace_amdgpu_dm_brightness(__builtin_return_address(0),
5194 user_brightness,
5195 brightness,
5196 caps->aux_support,
5197 power_supply_is_system_supplied() > 0);
5198 }
5199
5200 if (caps->aux_support) {
5201 rc = dc_link_set_backlight_level_nits(link, true, brightness,
5202 AUX_BL_DEFAULT_TRANSITION_TIME_MS);
5203 if (!rc)
5204 DRM_DEBUG("DM: Failed to update backlight via AUX on eDP[%d]\n", bl_idx);
5205 } else {
5206 struct set_backlight_level_params backlight_level_params = { 0 };
5207
5208 backlight_level_params.backlight_pwm_u16_16 = brightness;
5209 backlight_level_params.transition_time_in_ms = 0;
5210
5211 rc = dc_link_set_backlight_level(link, &backlight_level_params);
5212 if (!rc)
5213 DRM_DEBUG("DM: Failed to update backlight on eDP[%d]\n", bl_idx);
5214 }
5215
5216 if (dm->dc->caps.ips_support && reallow_idle)
5217 dc_allow_idle_optimizations(dm->dc, true);
5218
5219 mutex_unlock(&dm->dc_lock);
5220
5221 if (rc)
5222 dm->actual_brightness[bl_idx] = user_brightness;
5223 }
5224
amdgpu_dm_backlight_update_status(struct backlight_device * bd)5225 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
5226 {
5227 struct amdgpu_display_manager *dm = bl_get_data(bd);
5228 int i;
5229
5230 for (i = 0; i < dm->num_of_edps; i++) {
5231 if (bd == dm->backlight_dev[i])
5232 break;
5233 }
5234 if (i >= AMDGPU_DM_MAX_NUM_EDP)
5235 i = 0;
5236 amdgpu_dm_backlight_set_level(dm, i, bd->props.brightness);
5237
5238 return 0;
5239 }
5240
amdgpu_dm_backlight_get_level(struct amdgpu_display_manager * dm,int bl_idx)5241 static u32 amdgpu_dm_backlight_get_level(struct amdgpu_display_manager *dm,
5242 int bl_idx)
5243 {
5244 int ret;
5245 struct amdgpu_dm_backlight_caps caps;
5246 struct dc_link *link = (struct dc_link *)dm->backlight_link[bl_idx];
5247
5248 amdgpu_dm_update_backlight_caps(dm, bl_idx);
5249 caps = dm->backlight_caps[bl_idx];
5250
5251 if (caps.aux_support) {
5252 u32 avg, peak;
5253
5254 if (!dc_link_get_backlight_level_nits(link, &avg, &peak))
5255 return dm->brightness[bl_idx];
5256 return convert_brightness_to_user(&caps, avg);
5257 }
5258
5259 ret = dc_link_get_backlight_level(link);
5260
5261 if (ret == DC_ERROR_UNEXPECTED)
5262 return dm->brightness[bl_idx];
5263
5264 return convert_brightness_to_user(&caps, ret);
5265 }
5266
amdgpu_dm_backlight_get_brightness(struct backlight_device * bd)5267 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
5268 {
5269 struct amdgpu_display_manager *dm = bl_get_data(bd);
5270 int i;
5271
5272 for (i = 0; i < dm->num_of_edps; i++) {
5273 if (bd == dm->backlight_dev[i])
5274 break;
5275 }
5276 if (i >= AMDGPU_DM_MAX_NUM_EDP)
5277 i = 0;
5278 return amdgpu_dm_backlight_get_level(dm, i);
5279 }
5280
5281 static const struct backlight_ops amdgpu_dm_backlight_ops = {
5282 .options = BL_CORE_SUSPENDRESUME,
5283 .get_brightness = amdgpu_dm_backlight_get_brightness,
5284 .update_status = amdgpu_dm_backlight_update_status,
5285 };
5286
5287 static void
amdgpu_dm_register_backlight_device(struct amdgpu_dm_connector * aconnector)5288 amdgpu_dm_register_backlight_device(struct amdgpu_dm_connector *aconnector)
5289 {
5290 struct drm_device *drm = aconnector->base.dev;
5291 struct amdgpu_display_manager *dm = &drm_to_adev(drm)->dm;
5292 struct backlight_properties props = { 0 };
5293 struct amdgpu_dm_backlight_caps *caps;
5294 char bl_name[16];
5295 int min, max;
5296 int real_brightness;
5297 int init_brightness;
5298
5299 if (aconnector->bl_idx == -1)
5300 return;
5301
5302 if (!acpi_video_backlight_use_native()) {
5303 drm_info(drm, "Skipping amdgpu DM backlight registration\n");
5304 /* Try registering an ACPI video backlight device instead. */
5305 acpi_video_register_backlight();
5306 return;
5307 }
5308
5309 caps = &dm->backlight_caps[aconnector->bl_idx];
5310 if (get_brightness_range(caps, &min, &max)) {
5311 if (power_supply_is_system_supplied() > 0)
5312 props.brightness = DIV_ROUND_CLOSEST((max - min) * caps->ac_level, 100);
5313 else
5314 props.brightness = DIV_ROUND_CLOSEST((max - min) * caps->dc_level, 100);
5315 /* min is zero, so max needs to be adjusted */
5316 props.max_brightness = max - min;
5317 drm_dbg(drm, "Backlight caps: min: %d, max: %d, ac %d, dc %d\n", min, max,
5318 caps->ac_level, caps->dc_level);
5319 } else
5320 props.brightness = props.max_brightness = MAX_BACKLIGHT_LEVEL;
5321
5322 init_brightness = props.brightness;
5323
5324 if (caps->data_points && !(amdgpu_dc_debug_mask & DC_DISABLE_CUSTOM_BRIGHTNESS_CURVE)) {
5325 drm_info(drm, "Using custom brightness curve\n");
5326 props.scale = BACKLIGHT_SCALE_NON_LINEAR;
5327 } else
5328 props.scale = BACKLIGHT_SCALE_LINEAR;
5329 props.type = BACKLIGHT_RAW;
5330
5331 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
5332 drm->primary->index + aconnector->bl_idx);
5333
5334 dm->backlight_dev[aconnector->bl_idx] =
5335 backlight_device_register(bl_name, aconnector->base.kdev, dm,
5336 &amdgpu_dm_backlight_ops, &props);
5337 dm->brightness[aconnector->bl_idx] = props.brightness;
5338
5339 if (IS_ERR(dm->backlight_dev[aconnector->bl_idx])) {
5340 drm_err(drm, "DM: Backlight registration failed!\n");
5341 dm->backlight_dev[aconnector->bl_idx] = NULL;
5342 } else {
5343 /*
5344 * dm->brightness[x] can be inconsistent just after startup until
5345 * ops.get_brightness is called.
5346 */
5347 real_brightness =
5348 amdgpu_dm_backlight_ops.get_brightness(dm->backlight_dev[aconnector->bl_idx]);
5349
5350 if (real_brightness != init_brightness) {
5351 dm->actual_brightness[aconnector->bl_idx] = real_brightness;
5352 dm->brightness[aconnector->bl_idx] = real_brightness;
5353 }
5354 drm_dbg_driver(drm, "DM: Registered Backlight device: %s\n", bl_name);
5355 }
5356 }
5357
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)5358 static int initialize_plane(struct amdgpu_display_manager *dm,
5359 struct amdgpu_mode_info *mode_info, int plane_id,
5360 enum drm_plane_type plane_type,
5361 const struct dc_plane_cap *plane_cap)
5362 {
5363 struct drm_plane *plane;
5364 unsigned long possible_crtcs;
5365 int ret = 0;
5366
5367 plane = kzalloc_obj(struct drm_plane);
5368 if (!plane) {
5369 drm_err(adev_to_drm(dm->adev), "KMS: Failed to allocate plane\n");
5370 return -ENOMEM;
5371 }
5372 plane->type = plane_type;
5373
5374 /*
5375 * HACK: IGT tests expect that the primary plane for a CRTC
5376 * can only have one possible CRTC. Only expose support for
5377 * any CRTC if they're not going to be used as a primary plane
5378 * for a CRTC - like overlay or underlay planes.
5379 */
5380 possible_crtcs = 1 << plane_id;
5381 if (plane_id >= dm->dc->caps.max_streams)
5382 possible_crtcs = 0xff;
5383
5384 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap);
5385
5386 if (ret) {
5387 drm_err(adev_to_drm(dm->adev), "KMS: Failed to initialize plane\n");
5388 kfree(plane);
5389 return ret;
5390 }
5391
5392 if (mode_info)
5393 mode_info->planes[plane_id] = plane;
5394
5395 return ret;
5396 }
5397
5398
setup_backlight_device(struct amdgpu_display_manager * dm,struct amdgpu_dm_connector * aconnector)5399 static void setup_backlight_device(struct amdgpu_display_manager *dm,
5400 struct amdgpu_dm_connector *aconnector)
5401 {
5402 struct amdgpu_dm_backlight_caps *caps;
5403 struct dc_link *link = aconnector->dc_link;
5404 int bl_idx = dm->num_of_edps;
5405
5406 if (!(link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) ||
5407 link->type == dc_connection_none)
5408 return;
5409
5410 if (dm->num_of_edps >= AMDGPU_DM_MAX_NUM_EDP) {
5411 drm_warn(adev_to_drm(dm->adev), "Too much eDP connections, skipping backlight setup for additional eDPs\n");
5412 return;
5413 }
5414
5415 aconnector->bl_idx = bl_idx;
5416
5417 amdgpu_dm_update_backlight_caps(dm, bl_idx);
5418 dm->backlight_link[bl_idx] = link;
5419 dm->num_of_edps++;
5420
5421 update_connector_ext_caps(aconnector);
5422 caps = &dm->backlight_caps[aconnector->bl_idx];
5423
5424 /* Only offer ABM property when non-OLED and user didn't turn off by module parameter */
5425 if (!caps->ext_caps->bits.oled && amdgpu_dm_abm_level < 0)
5426 drm_object_attach_property(&aconnector->base.base,
5427 dm->adev->mode_info.abm_level_property,
5428 ABM_SYSFS_CONTROL);
5429 }
5430
5431 static void amdgpu_set_panel_orientation(struct drm_connector *connector);
5432
5433 /*
5434 * In this architecture, the association
5435 * connector -> encoder -> crtc
5436 * id not really requried. The crtc and connector will hold the
5437 * display_index as an abstraction to use with DAL component
5438 *
5439 * Returns 0 on success
5440 */
amdgpu_dm_initialize_drm_device(struct amdgpu_device * adev)5441 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
5442 {
5443 struct amdgpu_display_manager *dm = &adev->dm;
5444 s32 i;
5445 struct amdgpu_dm_connector *aconnector = NULL;
5446 struct amdgpu_encoder *aencoder = NULL;
5447 struct amdgpu_mode_info *mode_info = &adev->mode_info;
5448 u32 link_cnt;
5449 s32 primary_planes;
5450 enum dc_connection_type new_connection_type = dc_connection_none;
5451 const struct dc_plane_cap *plane;
5452 bool psr_feature_enabled = false;
5453 bool replay_feature_enabled = false;
5454 int max_overlay = dm->dc->caps.max_slave_planes;
5455
5456 dm->display_indexes_num = dm->dc->caps.max_streams;
5457 /* Update the actual used number of crtc */
5458 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
5459
5460 amdgpu_dm_set_irq_funcs(adev);
5461
5462 link_cnt = dm->dc->caps.max_links;
5463 if (amdgpu_dm_mode_config_init(dm->adev)) {
5464 drm_err(adev_to_drm(adev), "DM: Failed to initialize mode config\n");
5465 return -EINVAL;
5466 }
5467
5468 /* There is one primary plane per CRTC */
5469 primary_planes = dm->dc->caps.max_streams;
5470 if (primary_planes > AMDGPU_MAX_PLANES) {
5471 drm_err(adev_to_drm(adev), "DM: Plane nums out of 6 planes\n");
5472 return -EINVAL;
5473 }
5474
5475 /*
5476 * Initialize primary planes, implicit planes for legacy IOCTLS.
5477 * Order is reversed to match iteration order in atomic check.
5478 */
5479 for (i = (primary_planes - 1); i >= 0; i--) {
5480 plane = &dm->dc->caps.planes[i];
5481
5482 if (initialize_plane(dm, mode_info, i,
5483 DRM_PLANE_TYPE_PRIMARY, plane)) {
5484 drm_err(adev_to_drm(adev), "KMS: Failed to initialize primary plane\n");
5485 goto fail;
5486 }
5487 }
5488
5489 /*
5490 * Initialize overlay planes, index starting after primary planes.
5491 * These planes have a higher DRM index than the primary planes since
5492 * they should be considered as having a higher z-order.
5493 * Order is reversed to match iteration order in atomic check.
5494 *
5495 * Only support DCN for now, and only expose one so we don't encourage
5496 * userspace to use up all the pipes.
5497 */
5498 for (i = 0; i < dm->dc->caps.max_planes; ++i) {
5499 struct dc_plane_cap *plane = &dm->dc->caps.planes[i];
5500
5501 /* Do not create overlay if MPO disabled */
5502 if (amdgpu_dc_debug_mask & DC_DISABLE_MPO)
5503 break;
5504
5505 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL)
5506 continue;
5507
5508 if (!plane->pixel_format_support.argb8888)
5509 continue;
5510
5511 if (max_overlay-- == 0)
5512 break;
5513
5514 if (initialize_plane(dm, NULL, primary_planes + i,
5515 DRM_PLANE_TYPE_OVERLAY, plane)) {
5516 drm_err(adev_to_drm(adev), "KMS: Failed to initialize overlay plane\n");
5517 goto fail;
5518 }
5519 }
5520
5521 for (i = 0; i < dm->dc->caps.max_streams; i++)
5522 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) {
5523 drm_err(adev_to_drm(adev), "KMS: Failed to initialize crtc\n");
5524 goto fail;
5525 }
5526
5527 /* Use Outbox interrupt */
5528 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5529 case IP_VERSION(3, 0, 0):
5530 case IP_VERSION(3, 1, 2):
5531 case IP_VERSION(3, 1, 3):
5532 case IP_VERSION(3, 1, 4):
5533 case IP_VERSION(3, 1, 5):
5534 case IP_VERSION(3, 1, 6):
5535 case IP_VERSION(3, 2, 0):
5536 case IP_VERSION(3, 2, 1):
5537 case IP_VERSION(2, 1, 0):
5538 case IP_VERSION(3, 5, 0):
5539 case IP_VERSION(3, 5, 1):
5540 case IP_VERSION(3, 6, 0):
5541 case IP_VERSION(4, 0, 1):
5542 if (register_outbox_irq_handlers(dm->adev)) {
5543 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5544 goto fail;
5545 }
5546 break;
5547 default:
5548 drm_dbg_kms(adev_to_drm(adev), "Unsupported DCN IP version for outbox: 0x%X\n",
5549 amdgpu_ip_version(adev, DCE_HWIP, 0));
5550 }
5551
5552 /* Determine whether to enable PSR support by default. */
5553 if (!(amdgpu_dc_debug_mask & DC_DISABLE_PSR)) {
5554 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5555 case IP_VERSION(3, 1, 2):
5556 case IP_VERSION(3, 1, 3):
5557 case IP_VERSION(3, 1, 4):
5558 case IP_VERSION(3, 1, 5):
5559 case IP_VERSION(3, 1, 6):
5560 case IP_VERSION(3, 2, 0):
5561 case IP_VERSION(3, 2, 1):
5562 case IP_VERSION(3, 5, 0):
5563 case IP_VERSION(3, 5, 1):
5564 case IP_VERSION(3, 6, 0):
5565 case IP_VERSION(4, 0, 1):
5566 psr_feature_enabled = true;
5567 break;
5568 default:
5569 psr_feature_enabled = amdgpu_dc_feature_mask & DC_PSR_MASK;
5570 break;
5571 }
5572 }
5573
5574 /* Determine whether to enable Replay support by default. */
5575 if (!(amdgpu_dc_debug_mask & DC_DISABLE_REPLAY)) {
5576 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5577 case IP_VERSION(3, 1, 4):
5578 case IP_VERSION(3, 2, 0):
5579 case IP_VERSION(3, 2, 1):
5580 case IP_VERSION(3, 5, 0):
5581 case IP_VERSION(3, 5, 1):
5582 case IP_VERSION(3, 6, 0):
5583 replay_feature_enabled = true;
5584 break;
5585
5586 default:
5587 replay_feature_enabled = amdgpu_dc_feature_mask & DC_REPLAY_MASK;
5588 break;
5589 }
5590 }
5591
5592 if (link_cnt > MAX_LINKS) {
5593 drm_err(adev_to_drm(adev),
5594 "KMS: Cannot support more than %d display indexes\n",
5595 MAX_LINKS);
5596 goto fail;
5597 }
5598
5599 /* loops over all connectors on the board */
5600 for (i = 0; i < link_cnt; i++) {
5601 struct dc_link *link = NULL;
5602
5603 link = dc_get_link_at_index(dm->dc, i);
5604
5605 if (link->connector_signal == SIGNAL_TYPE_VIRTUAL) {
5606 struct amdgpu_dm_wb_connector *wbcon = kzalloc_obj(*wbcon);
5607
5608 if (!wbcon) {
5609 drm_err(adev_to_drm(adev), "KMS: Failed to allocate writeback connector\n");
5610 continue;
5611 }
5612
5613 if (amdgpu_dm_wb_connector_init(dm, wbcon, i)) {
5614 drm_err(adev_to_drm(adev), "KMS: Failed to initialize writeback connector\n");
5615 kfree(wbcon);
5616 continue;
5617 }
5618
5619 link->psr_settings.psr_feature_enabled = false;
5620 link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
5621
5622 continue;
5623 }
5624
5625 aconnector = kzalloc_obj(*aconnector);
5626 if (!aconnector)
5627 goto fail;
5628
5629 aencoder = kzalloc_obj(*aencoder);
5630 if (!aencoder)
5631 goto fail;
5632
5633 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
5634 drm_err(adev_to_drm(adev), "KMS: Failed to initialize encoder\n");
5635 goto fail;
5636 }
5637
5638 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
5639 drm_err(adev_to_drm(adev), "KMS: Failed to initialize connector\n");
5640 goto fail;
5641 }
5642
5643 if (dm->hpd_rx_offload_wq)
5644 dm->hpd_rx_offload_wq[aconnector->base.index].aconnector =
5645 aconnector;
5646
5647 if (!dc_link_detect_connection_type(link, &new_connection_type))
5648 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
5649
5650 if (aconnector->base.force && new_connection_type == dc_connection_none) {
5651 emulated_link_detect(link);
5652 amdgpu_dm_update_connector_after_detect(aconnector);
5653 } else {
5654 bool ret = false;
5655
5656 mutex_lock(&dm->dc_lock);
5657 dc_exit_ips_for_hw_access(dm->dc);
5658 ret = dc_link_detect(link, DETECT_REASON_BOOT);
5659 mutex_unlock(&dm->dc_lock);
5660
5661 if (ret) {
5662 amdgpu_dm_update_connector_after_detect(aconnector);
5663 setup_backlight_device(dm, aconnector);
5664
5665 /* Disable PSR if Replay can be enabled */
5666 if (replay_feature_enabled)
5667 if (amdgpu_dm_set_replay_caps(link, aconnector))
5668 psr_feature_enabled = false;
5669
5670 if (psr_feature_enabled) {
5671 amdgpu_dm_set_psr_caps(link);
5672 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",
5673 aconnector->base.name,
5674 link->psr_settings.psr_feature_enabled,
5675 link->psr_settings.psr_version,
5676 link->dpcd_caps.psr_info.psr_version,
5677 link->dpcd_caps.psr_info.psr_dpcd_caps.raw,
5678 link->dpcd_caps.psr_info.psr2_su_y_granularity_cap);
5679 }
5680 }
5681 }
5682 amdgpu_set_panel_orientation(&aconnector->base);
5683 }
5684
5685 /* Debug dump: list all DC links and their associated sinks after detection
5686 * is complete for all connectors. This provides a comprehensive view of the
5687 * final state without repeating the dump for each connector.
5688 */
5689 amdgpu_dm_dump_links_and_sinks(adev);
5690
5691 /* Software is initialized. Now we can register interrupt handlers. */
5692 switch (adev->asic_type) {
5693 #if defined(CONFIG_DRM_AMD_DC_SI)
5694 case CHIP_TAHITI:
5695 case CHIP_PITCAIRN:
5696 case CHIP_VERDE:
5697 case CHIP_OLAND:
5698 if (dce60_register_irq_handlers(dm->adev)) {
5699 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5700 goto fail;
5701 }
5702 break;
5703 #endif
5704 case CHIP_BONAIRE:
5705 case CHIP_HAWAII:
5706 case CHIP_KAVERI:
5707 case CHIP_KABINI:
5708 case CHIP_MULLINS:
5709 case CHIP_TONGA:
5710 case CHIP_FIJI:
5711 case CHIP_CARRIZO:
5712 case CHIP_STONEY:
5713 case CHIP_POLARIS11:
5714 case CHIP_POLARIS10:
5715 case CHIP_POLARIS12:
5716 case CHIP_VEGAM:
5717 case CHIP_VEGA10:
5718 case CHIP_VEGA12:
5719 case CHIP_VEGA20:
5720 if (dce110_register_irq_handlers(dm->adev)) {
5721 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5722 goto fail;
5723 }
5724 break;
5725 default:
5726 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5727 case IP_VERSION(1, 0, 0):
5728 case IP_VERSION(1, 0, 1):
5729 case IP_VERSION(2, 0, 2):
5730 case IP_VERSION(2, 0, 3):
5731 case IP_VERSION(2, 0, 0):
5732 case IP_VERSION(2, 1, 0):
5733 case IP_VERSION(3, 0, 0):
5734 case IP_VERSION(3, 0, 2):
5735 case IP_VERSION(3, 0, 3):
5736 case IP_VERSION(3, 0, 1):
5737 case IP_VERSION(3, 1, 2):
5738 case IP_VERSION(3, 1, 3):
5739 case IP_VERSION(3, 1, 4):
5740 case IP_VERSION(3, 1, 5):
5741 case IP_VERSION(3, 1, 6):
5742 case IP_VERSION(3, 2, 0):
5743 case IP_VERSION(3, 2, 1):
5744 case IP_VERSION(3, 5, 0):
5745 case IP_VERSION(3, 5, 1):
5746 case IP_VERSION(3, 6, 0):
5747 case IP_VERSION(4, 0, 1):
5748 if (dcn10_register_irq_handlers(dm->adev)) {
5749 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5750 goto fail;
5751 }
5752 break;
5753 default:
5754 drm_err(adev_to_drm(adev), "Unsupported DCE IP versions: 0x%X\n",
5755 amdgpu_ip_version(adev, DCE_HWIP, 0));
5756 goto fail;
5757 }
5758 break;
5759 }
5760
5761 return 0;
5762 fail:
5763 kfree(aencoder);
5764 kfree(aconnector);
5765
5766 return -EINVAL;
5767 }
5768
amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager * dm)5769 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
5770 {
5771 if (dm->atomic_obj.state)
5772 drm_atomic_private_obj_fini(&dm->atomic_obj);
5773 }
5774
5775 /******************************************************************************
5776 * amdgpu_display_funcs functions
5777 *****************************************************************************/
5778
5779 /*
5780 * dm_bandwidth_update - program display watermarks
5781 *
5782 * @adev: amdgpu_device pointer
5783 *
5784 * Calculate and program the display watermarks and line buffer allocation.
5785 */
dm_bandwidth_update(struct amdgpu_device * adev)5786 static void dm_bandwidth_update(struct amdgpu_device *adev)
5787 {
5788 /* TODO: implement later */
5789 }
5790
5791 static const struct amdgpu_display_funcs dm_display_funcs = {
5792 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
5793 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
5794 .backlight_set_level = NULL, /* never called for DC */
5795 .backlight_get_level = NULL, /* never called for DC */
5796 .hpd_sense = NULL,/* called unconditionally */
5797 .hpd_set_polarity = NULL, /* called unconditionally */
5798 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
5799 .page_flip_get_scanoutpos =
5800 dm_crtc_get_scanoutpos,/* called unconditionally */
5801 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
5802 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
5803 };
5804
5805 #if defined(CONFIG_DEBUG_KERNEL_DC)
5806
s3_debug_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)5807 static ssize_t s3_debug_store(struct device *device,
5808 struct device_attribute *attr,
5809 const char *buf,
5810 size_t count)
5811 {
5812 int ret;
5813 int s3_state;
5814 struct drm_device *drm_dev = dev_get_drvdata(device);
5815 struct amdgpu_device *adev = drm_to_adev(drm_dev);
5816 struct amdgpu_ip_block *ip_block;
5817
5818 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_DCE);
5819 if (!ip_block)
5820 return -EINVAL;
5821
5822 ret = kstrtoint(buf, 0, &s3_state);
5823
5824 if (ret == 0) {
5825 if (s3_state) {
5826 dm_resume(ip_block);
5827 drm_kms_helper_hotplug_event(adev_to_drm(adev));
5828 } else
5829 dm_suspend(ip_block);
5830 }
5831
5832 return ret == 0 ? count : 0;
5833 }
5834
5835 DEVICE_ATTR_WO(s3_debug);
5836
5837 #endif
5838
dm_init_microcode(struct amdgpu_device * adev)5839 static int dm_init_microcode(struct amdgpu_device *adev)
5840 {
5841 char *fw_name_dmub;
5842 int r;
5843
5844 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5845 case IP_VERSION(2, 1, 0):
5846 fw_name_dmub = FIRMWARE_RENOIR_DMUB;
5847 if (ASICREV_IS_GREEN_SARDINE(adev->external_rev_id))
5848 fw_name_dmub = FIRMWARE_GREEN_SARDINE_DMUB;
5849 break;
5850 case IP_VERSION(3, 0, 0):
5851 if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(10, 3, 0))
5852 fw_name_dmub = FIRMWARE_SIENNA_CICHLID_DMUB;
5853 else
5854 fw_name_dmub = FIRMWARE_NAVY_FLOUNDER_DMUB;
5855 break;
5856 case IP_VERSION(3, 0, 1):
5857 fw_name_dmub = FIRMWARE_VANGOGH_DMUB;
5858 break;
5859 case IP_VERSION(3, 0, 2):
5860 fw_name_dmub = FIRMWARE_DIMGREY_CAVEFISH_DMUB;
5861 break;
5862 case IP_VERSION(3, 0, 3):
5863 fw_name_dmub = FIRMWARE_BEIGE_GOBY_DMUB;
5864 break;
5865 case IP_VERSION(3, 1, 2):
5866 case IP_VERSION(3, 1, 3):
5867 fw_name_dmub = FIRMWARE_YELLOW_CARP_DMUB;
5868 break;
5869 case IP_VERSION(3, 1, 4):
5870 fw_name_dmub = FIRMWARE_DCN_314_DMUB;
5871 break;
5872 case IP_VERSION(3, 1, 5):
5873 fw_name_dmub = FIRMWARE_DCN_315_DMUB;
5874 break;
5875 case IP_VERSION(3, 1, 6):
5876 fw_name_dmub = FIRMWARE_DCN316_DMUB;
5877 break;
5878 case IP_VERSION(3, 2, 0):
5879 fw_name_dmub = FIRMWARE_DCN_V3_2_0_DMCUB;
5880 break;
5881 case IP_VERSION(3, 2, 1):
5882 fw_name_dmub = FIRMWARE_DCN_V3_2_1_DMCUB;
5883 break;
5884 case IP_VERSION(3, 5, 0):
5885 fw_name_dmub = FIRMWARE_DCN_35_DMUB;
5886 break;
5887 case IP_VERSION(3, 5, 1):
5888 fw_name_dmub = FIRMWARE_DCN_351_DMUB;
5889 break;
5890 case IP_VERSION(3, 6, 0):
5891 fw_name_dmub = FIRMWARE_DCN_36_DMUB;
5892 break;
5893 case IP_VERSION(4, 0, 1):
5894 fw_name_dmub = FIRMWARE_DCN_401_DMUB;
5895 break;
5896 default:
5897 /* ASIC doesn't support DMUB. */
5898 return 0;
5899 }
5900 r = amdgpu_ucode_request(adev, &adev->dm.dmub_fw, AMDGPU_UCODE_REQUIRED,
5901 "%s", fw_name_dmub);
5902 return r;
5903 }
5904
dm_early_init(struct amdgpu_ip_block * ip_block)5905 static int dm_early_init(struct amdgpu_ip_block *ip_block)
5906 {
5907 struct amdgpu_device *adev = ip_block->adev;
5908 struct amdgpu_mode_info *mode_info = &adev->mode_info;
5909 struct atom_context *ctx = mode_info->atom_context;
5910 int index = GetIndexIntoMasterTable(DATA, Object_Header);
5911 u16 data_offset;
5912
5913 /* if there is no object header, skip DM */
5914 if (!amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
5915 adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK;
5916 drm_info(adev_to_drm(adev), "No object header, skipping DM\n");
5917 return -ENOENT;
5918 }
5919
5920 switch (adev->asic_type) {
5921 #if defined(CONFIG_DRM_AMD_DC_SI)
5922 case CHIP_TAHITI:
5923 case CHIP_PITCAIRN:
5924 case CHIP_VERDE:
5925 adev->mode_info.num_crtc = 6;
5926 adev->mode_info.num_hpd = 6;
5927 adev->mode_info.num_dig = 6;
5928 break;
5929 case CHIP_OLAND:
5930 adev->mode_info.num_crtc = 2;
5931 adev->mode_info.num_hpd = 2;
5932 adev->mode_info.num_dig = 2;
5933 break;
5934 #endif
5935 case CHIP_BONAIRE:
5936 case CHIP_HAWAII:
5937 adev->mode_info.num_crtc = 6;
5938 adev->mode_info.num_hpd = 6;
5939 adev->mode_info.num_dig = 6;
5940 break;
5941 case CHIP_KAVERI:
5942 adev->mode_info.num_crtc = 4;
5943 adev->mode_info.num_hpd = 6;
5944 adev->mode_info.num_dig = 7;
5945 break;
5946 case CHIP_KABINI:
5947 case CHIP_MULLINS:
5948 adev->mode_info.num_crtc = 2;
5949 adev->mode_info.num_hpd = 6;
5950 adev->mode_info.num_dig = 6;
5951 break;
5952 case CHIP_FIJI:
5953 case CHIP_TONGA:
5954 adev->mode_info.num_crtc = 6;
5955 adev->mode_info.num_hpd = 6;
5956 adev->mode_info.num_dig = 7;
5957 break;
5958 case CHIP_CARRIZO:
5959 adev->mode_info.num_crtc = 3;
5960 adev->mode_info.num_hpd = 6;
5961 adev->mode_info.num_dig = 9;
5962 break;
5963 case CHIP_STONEY:
5964 adev->mode_info.num_crtc = 2;
5965 adev->mode_info.num_hpd = 6;
5966 adev->mode_info.num_dig = 9;
5967 break;
5968 case CHIP_POLARIS11:
5969 case CHIP_POLARIS12:
5970 adev->mode_info.num_crtc = 5;
5971 adev->mode_info.num_hpd = 5;
5972 adev->mode_info.num_dig = 5;
5973 break;
5974 case CHIP_POLARIS10:
5975 case CHIP_VEGAM:
5976 adev->mode_info.num_crtc = 6;
5977 adev->mode_info.num_hpd = 6;
5978 adev->mode_info.num_dig = 6;
5979 break;
5980 case CHIP_VEGA10:
5981 case CHIP_VEGA12:
5982 case CHIP_VEGA20:
5983 adev->mode_info.num_crtc = 6;
5984 adev->mode_info.num_hpd = 6;
5985 adev->mode_info.num_dig = 6;
5986 break;
5987 default:
5988
5989 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5990 case IP_VERSION(2, 0, 2):
5991 case IP_VERSION(3, 0, 0):
5992 adev->mode_info.num_crtc = 6;
5993 adev->mode_info.num_hpd = 6;
5994 adev->mode_info.num_dig = 6;
5995 break;
5996 case IP_VERSION(2, 0, 0):
5997 case IP_VERSION(3, 0, 2):
5998 adev->mode_info.num_crtc = 5;
5999 adev->mode_info.num_hpd = 5;
6000 adev->mode_info.num_dig = 5;
6001 break;
6002 case IP_VERSION(2, 0, 3):
6003 case IP_VERSION(3, 0, 3):
6004 adev->mode_info.num_crtc = 2;
6005 adev->mode_info.num_hpd = 2;
6006 adev->mode_info.num_dig = 2;
6007 break;
6008 case IP_VERSION(1, 0, 0):
6009 case IP_VERSION(1, 0, 1):
6010 case IP_VERSION(3, 0, 1):
6011 case IP_VERSION(2, 1, 0):
6012 case IP_VERSION(3, 1, 2):
6013 case IP_VERSION(3, 1, 3):
6014 case IP_VERSION(3, 1, 4):
6015 case IP_VERSION(3, 1, 5):
6016 case IP_VERSION(3, 1, 6):
6017 case IP_VERSION(3, 2, 0):
6018 case IP_VERSION(3, 2, 1):
6019 case IP_VERSION(3, 5, 0):
6020 case IP_VERSION(3, 5, 1):
6021 case IP_VERSION(3, 6, 0):
6022 case IP_VERSION(4, 0, 1):
6023 adev->mode_info.num_crtc = 4;
6024 adev->mode_info.num_hpd = 4;
6025 adev->mode_info.num_dig = 4;
6026 break;
6027 default:
6028 drm_err(adev_to_drm(adev), "Unsupported DCE IP versions: 0x%x\n",
6029 amdgpu_ip_version(adev, DCE_HWIP, 0));
6030 return -EINVAL;
6031 }
6032 break;
6033 }
6034
6035 if (adev->mode_info.funcs == NULL)
6036 adev->mode_info.funcs = &dm_display_funcs;
6037
6038 /*
6039 * Note: Do NOT change adev->audio_endpt_rreg and
6040 * adev->audio_endpt_wreg because they are initialised in
6041 * amdgpu_device_init()
6042 */
6043 #if defined(CONFIG_DEBUG_KERNEL_DC)
6044 device_create_file(
6045 adev_to_drm(adev)->dev,
6046 &dev_attr_s3_debug);
6047 #endif
6048 adev->dc_enabled = true;
6049
6050 return dm_init_microcode(adev);
6051 }
6052
modereset_required(struct drm_crtc_state * crtc_state)6053 static bool modereset_required(struct drm_crtc_state *crtc_state)
6054 {
6055 return !crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state);
6056 }
6057
amdgpu_dm_encoder_destroy(struct drm_encoder * encoder)6058 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
6059 {
6060 drm_encoder_cleanup(encoder);
6061 kfree(encoder);
6062 }
6063
6064 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
6065 .destroy = amdgpu_dm_encoder_destroy,
6066 };
6067
6068 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)6069 fill_plane_color_attributes(const struct drm_plane_state *plane_state,
6070 const enum surface_pixel_format format,
6071 enum dc_color_space *color_space)
6072 {
6073 bool full_range;
6074
6075 *color_space = COLOR_SPACE_SRGB;
6076
6077 /* Ignore properties when DRM_CLIENT_CAP_PLANE_COLOR_PIPELINE is set */
6078 if (plane_state->state && plane_state->state->plane_color_pipeline)
6079 return 0;
6080
6081 /* DRM color properties only affect non-RGB formats. */
6082 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
6083 return 0;
6084
6085 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE);
6086
6087 switch (plane_state->color_encoding) {
6088 case DRM_COLOR_YCBCR_BT601:
6089 if (full_range)
6090 *color_space = COLOR_SPACE_YCBCR601;
6091 else
6092 *color_space = COLOR_SPACE_YCBCR601_LIMITED;
6093 break;
6094
6095 case DRM_COLOR_YCBCR_BT709:
6096 if (full_range)
6097 *color_space = COLOR_SPACE_YCBCR709;
6098 else
6099 *color_space = COLOR_SPACE_YCBCR709_LIMITED;
6100 break;
6101
6102 case DRM_COLOR_YCBCR_BT2020:
6103 if (full_range)
6104 *color_space = COLOR_SPACE_2020_YCBCR_FULL;
6105 else
6106 *color_space = COLOR_SPACE_2020_YCBCR_LIMITED;
6107 break;
6108
6109 default:
6110 return -EINVAL;
6111 }
6112
6113 return 0;
6114 }
6115
6116 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)6117 fill_dc_plane_info_and_addr(struct amdgpu_device *adev,
6118 const struct drm_plane_state *plane_state,
6119 const u64 tiling_flags,
6120 struct dc_plane_info *plane_info,
6121 struct dc_plane_address *address,
6122 bool tmz_surface)
6123 {
6124 const struct drm_framebuffer *fb = plane_state->fb;
6125 const struct amdgpu_framebuffer *afb =
6126 to_amdgpu_framebuffer(plane_state->fb);
6127 int ret;
6128
6129 memset(plane_info, 0, sizeof(*plane_info));
6130
6131 switch (fb->format->format) {
6132 case DRM_FORMAT_C8:
6133 plane_info->format =
6134 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
6135 break;
6136 case DRM_FORMAT_RGB565:
6137 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
6138 break;
6139 case DRM_FORMAT_XRGB8888:
6140 case DRM_FORMAT_ARGB8888:
6141 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
6142 break;
6143 case DRM_FORMAT_XRGB2101010:
6144 case DRM_FORMAT_ARGB2101010:
6145 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
6146 break;
6147 case DRM_FORMAT_XBGR2101010:
6148 case DRM_FORMAT_ABGR2101010:
6149 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
6150 break;
6151 case DRM_FORMAT_XBGR8888:
6152 case DRM_FORMAT_ABGR8888:
6153 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888;
6154 break;
6155 case DRM_FORMAT_NV21:
6156 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
6157 break;
6158 case DRM_FORMAT_NV12:
6159 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
6160 break;
6161 case DRM_FORMAT_P010:
6162 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb;
6163 break;
6164 case DRM_FORMAT_XRGB16161616F:
6165 case DRM_FORMAT_ARGB16161616F:
6166 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F;
6167 break;
6168 case DRM_FORMAT_XBGR16161616F:
6169 case DRM_FORMAT_ABGR16161616F:
6170 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F;
6171 break;
6172 case DRM_FORMAT_XRGB16161616:
6173 case DRM_FORMAT_ARGB16161616:
6174 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616;
6175 break;
6176 case DRM_FORMAT_XBGR16161616:
6177 case DRM_FORMAT_ABGR16161616:
6178 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616;
6179 break;
6180 default:
6181 drm_err(adev_to_drm(adev),
6182 "Unsupported screen format %p4cc\n",
6183 &fb->format->format);
6184 return -EINVAL;
6185 }
6186
6187 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
6188 case DRM_MODE_ROTATE_0:
6189 plane_info->rotation = ROTATION_ANGLE_0;
6190 break;
6191 case DRM_MODE_ROTATE_90:
6192 plane_info->rotation = ROTATION_ANGLE_90;
6193 break;
6194 case DRM_MODE_ROTATE_180:
6195 plane_info->rotation = ROTATION_ANGLE_180;
6196 break;
6197 case DRM_MODE_ROTATE_270:
6198 plane_info->rotation = ROTATION_ANGLE_270;
6199 break;
6200 default:
6201 plane_info->rotation = ROTATION_ANGLE_0;
6202 break;
6203 }
6204
6205
6206 plane_info->visible = true;
6207 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE;
6208
6209 plane_info->layer_index = plane_state->normalized_zpos;
6210
6211 ret = fill_plane_color_attributes(plane_state, plane_info->format,
6212 &plane_info->color_space);
6213 if (ret)
6214 return ret;
6215
6216 ret = amdgpu_dm_plane_fill_plane_buffer_attributes(adev, afb, plane_info->format,
6217 plane_info->rotation, tiling_flags,
6218 &plane_info->tiling_info,
6219 &plane_info->plane_size,
6220 &plane_info->dcc, address,
6221 tmz_surface);
6222 if (ret)
6223 return ret;
6224
6225 amdgpu_dm_plane_fill_blending_from_plane_state(
6226 plane_state, &plane_info->per_pixel_alpha, &plane_info->pre_multiplied_alpha,
6227 &plane_info->global_alpha, &plane_info->global_alpha_value);
6228
6229 return 0;
6230 }
6231
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)6232 static int fill_dc_plane_attributes(struct amdgpu_device *adev,
6233 struct dc_plane_state *dc_plane_state,
6234 struct drm_plane_state *plane_state,
6235 struct drm_crtc_state *crtc_state)
6236 {
6237 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
6238 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)plane_state->fb;
6239 struct dc_scaling_info scaling_info;
6240 struct dc_plane_info plane_info;
6241 int ret;
6242
6243 ret = amdgpu_dm_plane_fill_dc_scaling_info(adev, plane_state, &scaling_info);
6244 if (ret)
6245 return ret;
6246
6247 dc_plane_state->src_rect = scaling_info.src_rect;
6248 dc_plane_state->dst_rect = scaling_info.dst_rect;
6249 dc_plane_state->clip_rect = scaling_info.clip_rect;
6250 dc_plane_state->scaling_quality = scaling_info.scaling_quality;
6251
6252 ret = fill_dc_plane_info_and_addr(adev, plane_state,
6253 afb->tiling_flags,
6254 &plane_info,
6255 &dc_plane_state->address,
6256 afb->tmz_surface);
6257 if (ret)
6258 return ret;
6259
6260 dc_plane_state->format = plane_info.format;
6261 dc_plane_state->color_space = plane_info.color_space;
6262 dc_plane_state->format = plane_info.format;
6263 dc_plane_state->plane_size = plane_info.plane_size;
6264 dc_plane_state->rotation = plane_info.rotation;
6265 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror;
6266 dc_plane_state->stereo_format = plane_info.stereo_format;
6267 dc_plane_state->tiling_info = plane_info.tiling_info;
6268 dc_plane_state->visible = plane_info.visible;
6269 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha;
6270 dc_plane_state->pre_multiplied_alpha = plane_info.pre_multiplied_alpha;
6271 dc_plane_state->global_alpha = plane_info.global_alpha;
6272 dc_plane_state->global_alpha_value = plane_info.global_alpha_value;
6273 dc_plane_state->dcc = plane_info.dcc;
6274 dc_plane_state->layer_index = plane_info.layer_index;
6275 dc_plane_state->flip_int_enabled = true;
6276
6277 /*
6278 * Always set input transfer function, since plane state is refreshed
6279 * every time.
6280 */
6281 ret = amdgpu_dm_update_plane_color_mgmt(dm_crtc_state,
6282 plane_state,
6283 dc_plane_state);
6284 if (ret)
6285 return ret;
6286
6287 return 0;
6288 }
6289
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)6290 static inline void fill_dc_dirty_rect(struct drm_plane *plane,
6291 struct rect *dirty_rect, int32_t x,
6292 s32 y, s32 width, s32 height,
6293 int *i, bool ffu)
6294 {
6295 WARN_ON(*i >= DC_MAX_DIRTY_RECTS);
6296
6297 dirty_rect->x = x;
6298 dirty_rect->y = y;
6299 dirty_rect->width = width;
6300 dirty_rect->height = height;
6301
6302 if (ffu)
6303 drm_dbg(plane->dev,
6304 "[PLANE:%d] PSR FFU dirty rect size (%d, %d)\n",
6305 plane->base.id, width, height);
6306 else
6307 drm_dbg(plane->dev,
6308 "[PLANE:%d] PSR SU dirty rect at (%d, %d) size (%d, %d)",
6309 plane->base.id, x, y, width, height);
6310
6311 (*i)++;
6312 }
6313
6314 /**
6315 * fill_dc_dirty_rects() - Fill DC dirty regions for PSR selective updates
6316 *
6317 * @plane: DRM plane containing dirty regions that need to be flushed to the eDP
6318 * remote fb
6319 * @old_plane_state: Old state of @plane
6320 * @new_plane_state: New state of @plane
6321 * @crtc_state: New state of CRTC connected to the @plane
6322 * @flip_addrs: DC flip tracking struct, which also tracts dirty rects
6323 * @is_psr_su: Flag indicating whether Panel Self Refresh Selective Update (PSR SU) is enabled.
6324 * If PSR SU is enabled and damage clips are available, only the regions of the screen
6325 * that have changed will be updated. If PSR SU is not enabled,
6326 * or if damage clips are not available, the entire screen will be updated.
6327 * @dirty_regions_changed: dirty regions changed
6328 *
6329 * For PSR SU, DC informs the DMUB uController of dirty rectangle regions
6330 * (referred to as "damage clips" in DRM nomenclature) that require updating on
6331 * the eDP remote buffer. The responsibility of specifying the dirty regions is
6332 * amdgpu_dm's.
6333 *
6334 * A damage-aware DRM client should fill the FB_DAMAGE_CLIPS property on the
6335 * plane with regions that require flushing to the eDP remote buffer. In
6336 * addition, certain use cases - such as cursor and multi-plane overlay (MPO) -
6337 * implicitly provide damage clips without any client support via the plane
6338 * bounds.
6339 */
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)6340 static void fill_dc_dirty_rects(struct drm_plane *plane,
6341 struct drm_plane_state *old_plane_state,
6342 struct drm_plane_state *new_plane_state,
6343 struct drm_crtc_state *crtc_state,
6344 struct dc_flip_addrs *flip_addrs,
6345 bool is_psr_su,
6346 bool *dirty_regions_changed)
6347 {
6348 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
6349 struct rect *dirty_rects = flip_addrs->dirty_rects;
6350 u32 num_clips;
6351 struct drm_mode_rect *clips;
6352 bool bb_changed;
6353 bool fb_changed;
6354 u32 i = 0;
6355 *dirty_regions_changed = false;
6356
6357 /*
6358 * Cursor plane has it's own dirty rect update interface. See
6359 * dcn10_dmub_update_cursor_data and dmub_cmd_update_cursor_info_data
6360 */
6361 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6362 return;
6363
6364 if (new_plane_state->rotation != DRM_MODE_ROTATE_0)
6365 goto ffu;
6366
6367 num_clips = drm_plane_get_damage_clips_count(new_plane_state);
6368 clips = drm_plane_get_damage_clips(new_plane_state);
6369
6370 if (num_clips && (!amdgpu_damage_clips || (amdgpu_damage_clips < 0 &&
6371 is_psr_su)))
6372 goto ffu;
6373
6374 if (!dm_crtc_state->mpo_requested) {
6375 if (!num_clips || num_clips > DC_MAX_DIRTY_RECTS)
6376 goto ffu;
6377
6378 for (; flip_addrs->dirty_rect_count < num_clips; clips++)
6379 fill_dc_dirty_rect(new_plane_state->plane,
6380 &dirty_rects[flip_addrs->dirty_rect_count],
6381 clips->x1, clips->y1,
6382 clips->x2 - clips->x1, clips->y2 - clips->y1,
6383 &flip_addrs->dirty_rect_count,
6384 false);
6385 return;
6386 }
6387
6388 /*
6389 * MPO is requested. Add entire plane bounding box to dirty rects if
6390 * flipped to or damaged.
6391 *
6392 * If plane is moved or resized, also add old bounding box to dirty
6393 * rects.
6394 */
6395 fb_changed = old_plane_state->fb->base.id !=
6396 new_plane_state->fb->base.id;
6397 bb_changed = (old_plane_state->crtc_x != new_plane_state->crtc_x ||
6398 old_plane_state->crtc_y != new_plane_state->crtc_y ||
6399 old_plane_state->crtc_w != new_plane_state->crtc_w ||
6400 old_plane_state->crtc_h != new_plane_state->crtc_h);
6401
6402 drm_dbg(plane->dev,
6403 "[PLANE:%d] PSR bb_changed:%d fb_changed:%d num_clips:%d\n",
6404 new_plane_state->plane->base.id,
6405 bb_changed, fb_changed, num_clips);
6406
6407 *dirty_regions_changed = bb_changed;
6408
6409 if ((num_clips + (bb_changed ? 2 : 0)) > DC_MAX_DIRTY_RECTS)
6410 goto ffu;
6411
6412 if (bb_changed) {
6413 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
6414 new_plane_state->crtc_x,
6415 new_plane_state->crtc_y,
6416 new_plane_state->crtc_w,
6417 new_plane_state->crtc_h, &i, false);
6418
6419 /* Add old plane bounding-box if plane is moved or resized */
6420 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
6421 old_plane_state->crtc_x,
6422 old_plane_state->crtc_y,
6423 old_plane_state->crtc_w,
6424 old_plane_state->crtc_h, &i, false);
6425 }
6426
6427 if (num_clips) {
6428 for (; i < num_clips; clips++)
6429 fill_dc_dirty_rect(new_plane_state->plane,
6430 &dirty_rects[i], clips->x1,
6431 clips->y1, clips->x2 - clips->x1,
6432 clips->y2 - clips->y1, &i, false);
6433 } else if (fb_changed && !bb_changed) {
6434 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
6435 new_plane_state->crtc_x,
6436 new_plane_state->crtc_y,
6437 new_plane_state->crtc_w,
6438 new_plane_state->crtc_h, &i, false);
6439 }
6440
6441 flip_addrs->dirty_rect_count = i;
6442 return;
6443
6444 ffu:
6445 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[0], 0, 0,
6446 dm_crtc_state->base.mode.crtc_hdisplay,
6447 dm_crtc_state->base.mode.crtc_vdisplay,
6448 &flip_addrs->dirty_rect_count, true);
6449 }
6450
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)6451 static void update_stream_scaling_settings(struct drm_device *dev,
6452 const struct drm_display_mode *mode,
6453 const struct dm_connector_state *dm_state,
6454 struct dc_stream_state *stream)
6455 {
6456 enum amdgpu_rmx_type rmx_type;
6457
6458 struct rect src = { 0 }; /* viewport in composition space*/
6459 struct rect dst = { 0 }; /* stream addressable area */
6460
6461 /* no mode. nothing to be done */
6462 if (!mode)
6463 return;
6464
6465 /* Full screen scaling by default */
6466 src.width = mode->hdisplay;
6467 src.height = mode->vdisplay;
6468 dst.width = stream->timing.h_addressable;
6469 dst.height = stream->timing.v_addressable;
6470
6471 if (dm_state) {
6472 rmx_type = dm_state->scaling;
6473 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
6474 if (src.width * dst.height <
6475 src.height * dst.width) {
6476 /* height needs less upscaling/more downscaling */
6477 dst.width = src.width *
6478 dst.height / src.height;
6479 } else {
6480 /* width needs less upscaling/more downscaling */
6481 dst.height = src.height *
6482 dst.width / src.width;
6483 }
6484 } else if (rmx_type == RMX_CENTER) {
6485 dst = src;
6486 }
6487
6488 dst.x = (stream->timing.h_addressable - dst.width) / 2;
6489 dst.y = (stream->timing.v_addressable - dst.height) / 2;
6490
6491 if (dm_state->underscan_enable) {
6492 dst.x += dm_state->underscan_hborder / 2;
6493 dst.y += dm_state->underscan_vborder / 2;
6494 dst.width -= dm_state->underscan_hborder;
6495 dst.height -= dm_state->underscan_vborder;
6496 }
6497 }
6498
6499 stream->src = src;
6500 stream->dst = dst;
6501
6502 drm_dbg_kms(dev, "Destination Rectangle x:%d y:%d width:%d height:%d\n",
6503 dst.x, dst.y, dst.width, dst.height);
6504
6505 }
6506
6507 static enum dc_color_depth
convert_color_depth_from_display_info(const struct drm_connector * connector,bool is_y420,int requested_bpc)6508 convert_color_depth_from_display_info(const struct drm_connector *connector,
6509 bool is_y420, int requested_bpc)
6510 {
6511 u8 bpc;
6512
6513 if (is_y420) {
6514 bpc = 8;
6515
6516 /* Cap display bpc based on HDMI 2.0 HF-VSDB */
6517 if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48)
6518 bpc = 16;
6519 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36)
6520 bpc = 12;
6521 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30)
6522 bpc = 10;
6523 } else {
6524 bpc = (uint8_t)connector->display_info.bpc;
6525 /* Assume 8 bpc by default if no bpc is specified. */
6526 bpc = bpc ? bpc : 8;
6527 }
6528
6529 if (requested_bpc > 0) {
6530 /*
6531 * Cap display bpc based on the user requested value.
6532 *
6533 * The value for state->max_bpc may not correctly updated
6534 * depending on when the connector gets added to the state
6535 * or if this was called outside of atomic check, so it
6536 * can't be used directly.
6537 */
6538 bpc = min_t(u8, bpc, requested_bpc);
6539
6540 /* Round down to the nearest even number. */
6541 bpc = bpc - (bpc & 1);
6542 }
6543
6544 switch (bpc) {
6545 case 0:
6546 /*
6547 * Temporary Work around, DRM doesn't parse color depth for
6548 * EDID revision before 1.4
6549 * TODO: Fix edid parsing
6550 */
6551 return COLOR_DEPTH_888;
6552 case 6:
6553 return COLOR_DEPTH_666;
6554 case 8:
6555 return COLOR_DEPTH_888;
6556 case 10:
6557 return COLOR_DEPTH_101010;
6558 case 12:
6559 return COLOR_DEPTH_121212;
6560 case 14:
6561 return COLOR_DEPTH_141414;
6562 case 16:
6563 return COLOR_DEPTH_161616;
6564 default:
6565 return COLOR_DEPTH_UNDEFINED;
6566 }
6567 }
6568
6569 static enum dc_aspect_ratio
get_aspect_ratio(const struct drm_display_mode * mode_in)6570 get_aspect_ratio(const struct drm_display_mode *mode_in)
6571 {
6572 /* 1-1 mapping, since both enums follow the HDMI spec. */
6573 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
6574 }
6575
6576 static enum dc_color_space
get_output_color_space(const struct dc_crtc_timing * dc_crtc_timing,const struct drm_connector_state * connector_state)6577 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing,
6578 const struct drm_connector_state *connector_state)
6579 {
6580 enum dc_color_space color_space = COLOR_SPACE_SRGB;
6581
6582 switch (connector_state->colorspace) {
6583 case DRM_MODE_COLORIMETRY_BT601_YCC:
6584 if (dc_crtc_timing->flags.Y_ONLY)
6585 color_space = COLOR_SPACE_YCBCR601_LIMITED;
6586 else
6587 color_space = COLOR_SPACE_YCBCR601;
6588 break;
6589 case DRM_MODE_COLORIMETRY_BT709_YCC:
6590 if (dc_crtc_timing->flags.Y_ONLY)
6591 color_space = COLOR_SPACE_YCBCR709_LIMITED;
6592 else
6593 color_space = COLOR_SPACE_YCBCR709;
6594 break;
6595 case DRM_MODE_COLORIMETRY_OPRGB:
6596 color_space = COLOR_SPACE_ADOBERGB;
6597 break;
6598 case DRM_MODE_COLORIMETRY_BT2020_RGB:
6599 case DRM_MODE_COLORIMETRY_BT2020_YCC:
6600 if (dc_crtc_timing->pixel_encoding == PIXEL_ENCODING_RGB)
6601 color_space = COLOR_SPACE_2020_RGB_FULLRANGE;
6602 else
6603 color_space = COLOR_SPACE_2020_YCBCR_LIMITED;
6604 break;
6605 case DRM_MODE_COLORIMETRY_DEFAULT: // ITU601
6606 default:
6607 if (dc_crtc_timing->pixel_encoding == PIXEL_ENCODING_RGB) {
6608 color_space = COLOR_SPACE_SRGB;
6609 if (connector_state->hdmi.broadcast_rgb == DRM_HDMI_BROADCAST_RGB_LIMITED)
6610 color_space = COLOR_SPACE_SRGB_LIMITED;
6611 /*
6612 * 27030khz is the separation point between HDTV and SDTV
6613 * according to HDMI spec, we use YCbCr709 and YCbCr601
6614 * respectively
6615 */
6616 } else if (dc_crtc_timing->pix_clk_100hz > 270300) {
6617 if (dc_crtc_timing->flags.Y_ONLY)
6618 color_space =
6619 COLOR_SPACE_YCBCR709_LIMITED;
6620 else
6621 color_space = COLOR_SPACE_YCBCR709;
6622 } else {
6623 if (dc_crtc_timing->flags.Y_ONLY)
6624 color_space =
6625 COLOR_SPACE_YCBCR601_LIMITED;
6626 else
6627 color_space = COLOR_SPACE_YCBCR601;
6628 }
6629 break;
6630 }
6631
6632 return color_space;
6633 }
6634
6635 static enum display_content_type
get_output_content_type(const struct drm_connector_state * connector_state)6636 get_output_content_type(const struct drm_connector_state *connector_state)
6637 {
6638 switch (connector_state->content_type) {
6639 default:
6640 case DRM_MODE_CONTENT_TYPE_NO_DATA:
6641 return DISPLAY_CONTENT_TYPE_NO_DATA;
6642 case DRM_MODE_CONTENT_TYPE_GRAPHICS:
6643 return DISPLAY_CONTENT_TYPE_GRAPHICS;
6644 case DRM_MODE_CONTENT_TYPE_PHOTO:
6645 return DISPLAY_CONTENT_TYPE_PHOTO;
6646 case DRM_MODE_CONTENT_TYPE_CINEMA:
6647 return DISPLAY_CONTENT_TYPE_CINEMA;
6648 case DRM_MODE_CONTENT_TYPE_GAME:
6649 return DISPLAY_CONTENT_TYPE_GAME;
6650 }
6651 }
6652
adjust_colour_depth_from_display_info(struct dc_crtc_timing * timing_out,const struct drm_display_info * info)6653 static bool adjust_colour_depth_from_display_info(
6654 struct dc_crtc_timing *timing_out,
6655 const struct drm_display_info *info)
6656 {
6657 enum dc_color_depth depth = timing_out->display_color_depth;
6658 int normalized_clk;
6659
6660 do {
6661 normalized_clk = timing_out->pix_clk_100hz / 10;
6662 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
6663 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
6664 normalized_clk /= 2;
6665 /* Adjusting pix clock following on HDMI spec based on colour depth */
6666 switch (depth) {
6667 case COLOR_DEPTH_888:
6668 break;
6669 case COLOR_DEPTH_101010:
6670 normalized_clk = (normalized_clk * 30) / 24;
6671 break;
6672 case COLOR_DEPTH_121212:
6673 normalized_clk = (normalized_clk * 36) / 24;
6674 break;
6675 case COLOR_DEPTH_161616:
6676 normalized_clk = (normalized_clk * 48) / 24;
6677 break;
6678 default:
6679 /* The above depths are the only ones valid for HDMI. */
6680 return false;
6681 }
6682 if (normalized_clk <= info->max_tmds_clock) {
6683 timing_out->display_color_depth = depth;
6684 return true;
6685 }
6686 } while (--depth > COLOR_DEPTH_666);
6687 return false;
6688 }
6689
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)6690 static void fill_stream_properties_from_drm_display_mode(
6691 struct dc_stream_state *stream,
6692 const struct drm_display_mode *mode_in,
6693 const struct drm_connector *connector,
6694 const struct drm_connector_state *connector_state,
6695 const struct dc_stream_state *old_stream,
6696 int requested_bpc)
6697 {
6698 struct dc_crtc_timing *timing_out = &stream->timing;
6699 const struct drm_display_info *info = &connector->display_info;
6700 struct amdgpu_dm_connector *aconnector = NULL;
6701 struct hdmi_vendor_infoframe hv_frame;
6702 struct hdmi_avi_infoframe avi_frame;
6703 ssize_t err;
6704
6705 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
6706 aconnector = to_amdgpu_dm_connector(connector);
6707
6708 memset(&hv_frame, 0, sizeof(hv_frame));
6709 memset(&avi_frame, 0, sizeof(avi_frame));
6710
6711 timing_out->h_border_left = 0;
6712 timing_out->h_border_right = 0;
6713 timing_out->v_border_top = 0;
6714 timing_out->v_border_bottom = 0;
6715 /* TODO: un-hardcode */
6716 if (drm_mode_is_420_only(info, mode_in)
6717 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
6718 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
6719 else if (drm_mode_is_420_also(info, mode_in)
6720 && aconnector
6721 && aconnector->force_yuv420_output)
6722 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
6723 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCBCR422)
6724 && aconnector
6725 && aconnector->force_yuv422_output)
6726 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR422;
6727 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCBCR444)
6728 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
6729 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
6730 else
6731 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
6732
6733 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
6734 timing_out->display_color_depth = convert_color_depth_from_display_info(
6735 connector,
6736 (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420),
6737 requested_bpc);
6738 timing_out->scan_type = SCANNING_TYPE_NODATA;
6739 timing_out->hdmi_vic = 0;
6740
6741 if (old_stream) {
6742 timing_out->vic = old_stream->timing.vic;
6743 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY;
6744 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY;
6745 } else {
6746 timing_out->vic = drm_match_cea_mode(mode_in);
6747 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
6748 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
6749 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
6750 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
6751 }
6752
6753 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
6754 err = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame,
6755 (struct drm_connector *)connector,
6756 mode_in);
6757 if (err < 0)
6758 drm_warn_once(connector->dev, "Failed to setup avi infoframe on connector %s: %zd\n",
6759 connector->name, err);
6760 timing_out->vic = avi_frame.video_code;
6761 err = drm_hdmi_vendor_infoframe_from_display_mode(&hv_frame,
6762 (struct drm_connector *)connector,
6763 mode_in);
6764 if (err < 0)
6765 drm_warn_once(connector->dev, "Failed to setup vendor infoframe on connector %s: %zd\n",
6766 connector->name, err);
6767 timing_out->hdmi_vic = hv_frame.vic;
6768 }
6769
6770 if (aconnector && is_freesync_video_mode(mode_in, aconnector)) {
6771 timing_out->h_addressable = mode_in->hdisplay;
6772 timing_out->h_total = mode_in->htotal;
6773 timing_out->h_sync_width = mode_in->hsync_end - mode_in->hsync_start;
6774 timing_out->h_front_porch = mode_in->hsync_start - mode_in->hdisplay;
6775 timing_out->v_total = mode_in->vtotal;
6776 timing_out->v_addressable = mode_in->vdisplay;
6777 timing_out->v_front_porch = mode_in->vsync_start - mode_in->vdisplay;
6778 timing_out->v_sync_width = mode_in->vsync_end - mode_in->vsync_start;
6779 timing_out->pix_clk_100hz = mode_in->clock * 10;
6780 } else {
6781 timing_out->h_addressable = mode_in->crtc_hdisplay;
6782 timing_out->h_total = mode_in->crtc_htotal;
6783 timing_out->h_sync_width = mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
6784 timing_out->h_front_porch = mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
6785 timing_out->v_total = mode_in->crtc_vtotal;
6786 timing_out->v_addressable = mode_in->crtc_vdisplay;
6787 timing_out->v_front_porch = mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
6788 timing_out->v_sync_width = mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
6789 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10;
6790 }
6791
6792 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
6793
6794 stream->out_transfer_func.type = TF_TYPE_PREDEFINED;
6795 stream->out_transfer_func.tf = TRANSFER_FUNCTION_SRGB;
6796 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
6797 if (!adjust_colour_depth_from_display_info(timing_out, info) &&
6798 drm_mode_is_420_also(info, mode_in) &&
6799 timing_out->pixel_encoding != PIXEL_ENCODING_YCBCR420) {
6800 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
6801 adjust_colour_depth_from_display_info(timing_out, info);
6802 }
6803 }
6804
6805 stream->output_color_space = get_output_color_space(timing_out, connector_state);
6806 stream->content_type = get_output_content_type(connector_state);
6807 }
6808
fill_audio_info(struct audio_info * audio_info,const struct drm_connector * drm_connector,const struct dc_sink * dc_sink)6809 static void fill_audio_info(struct audio_info *audio_info,
6810 const struct drm_connector *drm_connector,
6811 const struct dc_sink *dc_sink)
6812 {
6813 int i = 0;
6814 int cea_revision = 0;
6815 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
6816
6817 audio_info->manufacture_id = edid_caps->manufacturer_id;
6818 audio_info->product_id = edid_caps->product_id;
6819
6820 cea_revision = drm_connector->display_info.cea_rev;
6821
6822 strscpy(audio_info->display_name,
6823 edid_caps->display_name,
6824 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
6825
6826 if (cea_revision >= 3) {
6827 audio_info->mode_count = edid_caps->audio_mode_count;
6828
6829 for (i = 0; i < audio_info->mode_count; ++i) {
6830 audio_info->modes[i].format_code =
6831 (enum audio_format_code)
6832 (edid_caps->audio_modes[i].format_code);
6833 audio_info->modes[i].channel_count =
6834 edid_caps->audio_modes[i].channel_count;
6835 audio_info->modes[i].sample_rates.all =
6836 edid_caps->audio_modes[i].sample_rate;
6837 audio_info->modes[i].sample_size =
6838 edid_caps->audio_modes[i].sample_size;
6839 }
6840 }
6841
6842 audio_info->flags.all = edid_caps->speaker_flags;
6843
6844 /* TODO: We only check for the progressive mode, check for interlace mode too */
6845 if (drm_connector->latency_present[0]) {
6846 audio_info->video_latency = drm_connector->video_latency[0];
6847 audio_info->audio_latency = drm_connector->audio_latency[0];
6848 }
6849
6850 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
6851
6852 }
6853
6854 static void
copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode * src_mode,struct drm_display_mode * dst_mode)6855 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
6856 struct drm_display_mode *dst_mode)
6857 {
6858 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
6859 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
6860 dst_mode->crtc_clock = src_mode->crtc_clock;
6861 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
6862 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
6863 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
6864 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
6865 dst_mode->crtc_htotal = src_mode->crtc_htotal;
6866 dst_mode->crtc_hskew = src_mode->crtc_hskew;
6867 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
6868 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
6869 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
6870 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
6871 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
6872 }
6873
6874 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)6875 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
6876 const struct drm_display_mode *native_mode,
6877 bool scale_enabled)
6878 {
6879 if (scale_enabled || (
6880 native_mode->clock == drm_mode->clock &&
6881 native_mode->htotal == drm_mode->htotal &&
6882 native_mode->vtotal == drm_mode->vtotal)) {
6883 if (native_mode->crtc_clock)
6884 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
6885 } else {
6886 /* no scaling nor amdgpu inserted, no need to patch */
6887 }
6888 }
6889
6890 static struct dc_sink *
create_fake_sink(struct drm_device * dev,struct dc_link * link)6891 create_fake_sink(struct drm_device *dev, struct dc_link *link)
6892 {
6893 struct dc_sink_init_data sink_init_data = { 0 };
6894 struct dc_sink *sink = NULL;
6895
6896 sink_init_data.link = link;
6897 sink_init_data.sink_signal = link->connector_signal;
6898
6899 sink = dc_sink_create(&sink_init_data);
6900 if (!sink) {
6901 drm_err(dev, "Failed to create sink!\n");
6902 return NULL;
6903 }
6904 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
6905
6906 return sink;
6907 }
6908
set_multisync_trigger_params(struct dc_stream_state * stream)6909 static void set_multisync_trigger_params(
6910 struct dc_stream_state *stream)
6911 {
6912 struct dc_stream_state *master = NULL;
6913
6914 if (stream->triggered_crtc_reset.enabled) {
6915 master = stream->triggered_crtc_reset.event_source;
6916 stream->triggered_crtc_reset.event =
6917 master->timing.flags.VSYNC_POSITIVE_POLARITY ?
6918 CRTC_EVENT_VSYNC_RISING : CRTC_EVENT_VSYNC_FALLING;
6919 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_PIXEL;
6920 }
6921 }
6922
set_master_stream(struct dc_stream_state * stream_set[],int stream_count)6923 static void set_master_stream(struct dc_stream_state *stream_set[],
6924 int stream_count)
6925 {
6926 int j, highest_rfr = 0, master_stream = 0;
6927
6928 for (j = 0; j < stream_count; j++) {
6929 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
6930 int refresh_rate = 0;
6931
6932 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/
6933 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
6934 if (refresh_rate > highest_rfr) {
6935 highest_rfr = refresh_rate;
6936 master_stream = j;
6937 }
6938 }
6939 }
6940 for (j = 0; j < stream_count; j++) {
6941 if (stream_set[j])
6942 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
6943 }
6944 }
6945
dm_enable_per_frame_crtc_master_sync(struct dc_state * context)6946 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
6947 {
6948 int i = 0;
6949 struct dc_stream_state *stream;
6950
6951 if (context->stream_count < 2)
6952 return;
6953 for (i = 0; i < context->stream_count ; i++) {
6954 if (!context->streams[i])
6955 continue;
6956 /*
6957 * TODO: add a function to read AMD VSDB bits and set
6958 * crtc_sync_master.multi_sync_enabled flag
6959 * For now it's set to false
6960 */
6961 }
6962
6963 set_master_stream(context->streams, context->stream_count);
6964
6965 for (i = 0; i < context->stream_count ; i++) {
6966 stream = context->streams[i];
6967
6968 if (!stream)
6969 continue;
6970
6971 set_multisync_trigger_params(stream);
6972 }
6973 }
6974
6975 /**
6976 * DOC: FreeSync Video
6977 *
6978 * When a userspace application wants to play a video, the content follows a
6979 * standard format definition that usually specifies the FPS for that format.
6980 * The below list illustrates some video format and the expected FPS,
6981 * respectively:
6982 *
6983 * - TV/NTSC (23.976 FPS)
6984 * - Cinema (24 FPS)
6985 * - TV/PAL (25 FPS)
6986 * - TV/NTSC (29.97 FPS)
6987 * - TV/NTSC (30 FPS)
6988 * - Cinema HFR (48 FPS)
6989 * - TV/PAL (50 FPS)
6990 * - Commonly used (60 FPS)
6991 * - Multiples of 24 (48,72,96 FPS)
6992 *
6993 * The list of standards video format is not huge and can be added to the
6994 * connector modeset list beforehand. With that, userspace can leverage
6995 * FreeSync to extends the front porch in order to attain the target refresh
6996 * rate. Such a switch will happen seamlessly, without screen blanking or
6997 * reprogramming of the output in any other way. If the userspace requests a
6998 * modesetting change compatible with FreeSync modes that only differ in the
6999 * refresh rate, DC will skip the full update and avoid blink during the
7000 * transition. For example, the video player can change the modesetting from
7001 * 60Hz to 30Hz for playing TV/NTSC content when it goes full screen without
7002 * causing any display blink. This same concept can be applied to a mode
7003 * setting change.
7004 */
7005 static struct drm_display_mode *
get_highest_refresh_rate_mode(struct amdgpu_dm_connector * aconnector,bool use_probed_modes)7006 get_highest_refresh_rate_mode(struct amdgpu_dm_connector *aconnector,
7007 bool use_probed_modes)
7008 {
7009 struct drm_display_mode *m, *m_pref = NULL;
7010 u16 current_refresh, highest_refresh;
7011 struct list_head *list_head = use_probed_modes ?
7012 &aconnector->base.probed_modes :
7013 &aconnector->base.modes;
7014
7015 if (aconnector->base.connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
7016 return NULL;
7017
7018 if (aconnector->freesync_vid_base.clock != 0)
7019 return &aconnector->freesync_vid_base;
7020
7021 /* Find the preferred mode */
7022 list_for_each_entry(m, list_head, head) {
7023 if (m->type & DRM_MODE_TYPE_PREFERRED) {
7024 m_pref = m;
7025 break;
7026 }
7027 }
7028
7029 if (!m_pref) {
7030 /* Probably an EDID with no preferred mode. Fallback to first entry */
7031 m_pref = list_first_entry_or_null(
7032 &aconnector->base.modes, struct drm_display_mode, head);
7033 if (!m_pref) {
7034 drm_dbg_driver(aconnector->base.dev, "No preferred mode found in EDID\n");
7035 return NULL;
7036 }
7037 }
7038
7039 highest_refresh = drm_mode_vrefresh(m_pref);
7040
7041 /*
7042 * Find the mode with highest refresh rate with same resolution.
7043 * For some monitors, preferred mode is not the mode with highest
7044 * supported refresh rate.
7045 */
7046 list_for_each_entry(m, list_head, head) {
7047 current_refresh = drm_mode_vrefresh(m);
7048
7049 if (m->hdisplay == m_pref->hdisplay &&
7050 m->vdisplay == m_pref->vdisplay &&
7051 highest_refresh < current_refresh) {
7052 highest_refresh = current_refresh;
7053 m_pref = m;
7054 }
7055 }
7056
7057 drm_mode_copy(&aconnector->freesync_vid_base, m_pref);
7058 return m_pref;
7059 }
7060
is_freesync_video_mode(const struct drm_display_mode * mode,struct amdgpu_dm_connector * aconnector)7061 static bool is_freesync_video_mode(const struct drm_display_mode *mode,
7062 struct amdgpu_dm_connector *aconnector)
7063 {
7064 struct drm_display_mode *high_mode;
7065 int timing_diff;
7066
7067 high_mode = get_highest_refresh_rate_mode(aconnector, false);
7068 if (!high_mode || !mode)
7069 return false;
7070
7071 timing_diff = high_mode->vtotal - mode->vtotal;
7072
7073 if (high_mode->clock == 0 || high_mode->clock != mode->clock ||
7074 high_mode->hdisplay != mode->hdisplay ||
7075 high_mode->vdisplay != mode->vdisplay ||
7076 high_mode->hsync_start != mode->hsync_start ||
7077 high_mode->hsync_end != mode->hsync_end ||
7078 high_mode->htotal != mode->htotal ||
7079 high_mode->hskew != mode->hskew ||
7080 high_mode->vscan != mode->vscan ||
7081 high_mode->vsync_start - mode->vsync_start != timing_diff ||
7082 high_mode->vsync_end - mode->vsync_end != timing_diff)
7083 return false;
7084 else
7085 return true;
7086 }
7087
7088 #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)7089 static void update_dsc_caps(struct amdgpu_dm_connector *aconnector,
7090 struct dc_sink *sink, struct dc_stream_state *stream,
7091 struct dsc_dec_dpcd_caps *dsc_caps)
7092 {
7093 stream->timing.flags.DSC = 0;
7094 dsc_caps->is_dsc_supported = false;
7095
7096 if (aconnector->dc_link && (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT ||
7097 sink->sink_signal == SIGNAL_TYPE_EDP)) {
7098 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE ||
7099 sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER)
7100 dc_dsc_parse_dsc_dpcd(aconnector->dc_link->ctx->dc,
7101 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.raw,
7102 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.raw,
7103 dsc_caps);
7104 }
7105 }
7106
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)7107 static void apply_dsc_policy_for_edp(struct amdgpu_dm_connector *aconnector,
7108 struct dc_sink *sink, struct dc_stream_state *stream,
7109 struct dsc_dec_dpcd_caps *dsc_caps,
7110 uint32_t max_dsc_target_bpp_limit_override)
7111 {
7112 const struct dc_link_settings *verified_link_cap = NULL;
7113 u32 link_bw_in_kbps;
7114 u32 edp_min_bpp_x16, edp_max_bpp_x16;
7115 struct dc *dc = sink->ctx->dc;
7116 struct dc_dsc_bw_range bw_range = {0};
7117 struct dc_dsc_config dsc_cfg = {0};
7118 struct dc_dsc_config_options dsc_options = {0};
7119
7120 dc_dsc_get_default_config_option(dc, &dsc_options);
7121 dsc_options.max_target_bpp_limit_override_x16 = max_dsc_target_bpp_limit_override * 16;
7122
7123 verified_link_cap = dc_link_get_link_cap(stream->link);
7124 link_bw_in_kbps = dc_link_bandwidth_kbps(stream->link, verified_link_cap);
7125 edp_min_bpp_x16 = 8 * 16;
7126 edp_max_bpp_x16 = 8 * 16;
7127
7128 if (edp_max_bpp_x16 > dsc_caps->edp_max_bits_per_pixel)
7129 edp_max_bpp_x16 = dsc_caps->edp_max_bits_per_pixel;
7130
7131 if (edp_max_bpp_x16 < edp_min_bpp_x16)
7132 edp_min_bpp_x16 = edp_max_bpp_x16;
7133
7134 if (dc_dsc_compute_bandwidth_range(dc->res_pool->dscs[0],
7135 dc->debug.dsc_min_slice_height_override,
7136 edp_min_bpp_x16, edp_max_bpp_x16,
7137 dsc_caps,
7138 &stream->timing,
7139 dc_link_get_highest_encoding_format(aconnector->dc_link),
7140 &bw_range)) {
7141
7142 if (bw_range.max_kbps < link_bw_in_kbps) {
7143 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
7144 dsc_caps,
7145 &dsc_options,
7146 0,
7147 &stream->timing,
7148 dc_link_get_highest_encoding_format(aconnector->dc_link),
7149 &dsc_cfg)) {
7150 stream->timing.dsc_cfg = dsc_cfg;
7151 stream->timing.flags.DSC = 1;
7152 stream->timing.dsc_cfg.bits_per_pixel = edp_max_bpp_x16;
7153 }
7154 return;
7155 }
7156 }
7157
7158 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
7159 dsc_caps,
7160 &dsc_options,
7161 link_bw_in_kbps,
7162 &stream->timing,
7163 dc_link_get_highest_encoding_format(aconnector->dc_link),
7164 &dsc_cfg)) {
7165 stream->timing.dsc_cfg = dsc_cfg;
7166 stream->timing.flags.DSC = 1;
7167 }
7168 }
7169
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)7170 static void apply_dsc_policy_for_stream(struct amdgpu_dm_connector *aconnector,
7171 struct dc_sink *sink, struct dc_stream_state *stream,
7172 struct dsc_dec_dpcd_caps *dsc_caps)
7173 {
7174 struct drm_connector *drm_connector = &aconnector->base;
7175 u32 link_bandwidth_kbps;
7176 struct dc *dc = sink->ctx->dc;
7177 u32 max_supported_bw_in_kbps, timing_bw_in_kbps;
7178 u32 dsc_max_supported_bw_in_kbps;
7179 u32 max_dsc_target_bpp_limit_override =
7180 drm_connector->display_info.max_dsc_bpp;
7181 struct dc_dsc_config_options dsc_options = {0};
7182
7183 dc_dsc_get_default_config_option(dc, &dsc_options);
7184 dsc_options.max_target_bpp_limit_override_x16 = max_dsc_target_bpp_limit_override * 16;
7185
7186 link_bandwidth_kbps = dc_link_bandwidth_kbps(aconnector->dc_link,
7187 dc_link_get_link_cap(aconnector->dc_link));
7188
7189 /* Set DSC policy according to dsc_clock_en */
7190 dc_dsc_policy_set_enable_dsc_when_not_needed(
7191 aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE);
7192
7193 if (sink->sink_signal == SIGNAL_TYPE_EDP &&
7194 !aconnector->dc_link->panel_config.dsc.disable_dsc_edp &&
7195 dc->caps.edp_dsc_support && aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE) {
7196
7197 apply_dsc_policy_for_edp(aconnector, sink, stream, dsc_caps, max_dsc_target_bpp_limit_override);
7198
7199 } else if (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT) {
7200 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE) {
7201 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
7202 dsc_caps,
7203 &dsc_options,
7204 link_bandwidth_kbps,
7205 &stream->timing,
7206 dc_link_get_highest_encoding_format(aconnector->dc_link),
7207 &stream->timing.dsc_cfg)) {
7208 stream->timing.flags.DSC = 1;
7209 drm_dbg_driver(drm_connector->dev, "%s: SST_DSC [%s] DSC is selected from SST RX\n",
7210 __func__, drm_connector->name);
7211 }
7212 } else if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER) {
7213 timing_bw_in_kbps = dc_bandwidth_in_kbps_from_timing(&stream->timing,
7214 dc_link_get_highest_encoding_format(aconnector->dc_link));
7215 max_supported_bw_in_kbps = link_bandwidth_kbps;
7216 dsc_max_supported_bw_in_kbps = link_bandwidth_kbps;
7217
7218 if (timing_bw_in_kbps > max_supported_bw_in_kbps &&
7219 max_supported_bw_in_kbps > 0 &&
7220 dsc_max_supported_bw_in_kbps > 0)
7221 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
7222 dsc_caps,
7223 &dsc_options,
7224 dsc_max_supported_bw_in_kbps,
7225 &stream->timing,
7226 dc_link_get_highest_encoding_format(aconnector->dc_link),
7227 &stream->timing.dsc_cfg)) {
7228 stream->timing.flags.DSC = 1;
7229 drm_dbg_driver(drm_connector->dev, "%s: SST_DSC [%s] DSC is selected from DP-HDMI PCON\n",
7230 __func__, drm_connector->name);
7231 }
7232 }
7233 }
7234
7235 /* Overwrite the stream flag if DSC is enabled through debugfs */
7236 if (aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE)
7237 stream->timing.flags.DSC = 1;
7238
7239 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_h)
7240 stream->timing.dsc_cfg.num_slices_h = aconnector->dsc_settings.dsc_num_slices_h;
7241
7242 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_v)
7243 stream->timing.dsc_cfg.num_slices_v = aconnector->dsc_settings.dsc_num_slices_v;
7244
7245 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_bits_per_pixel)
7246 stream->timing.dsc_cfg.bits_per_pixel = aconnector->dsc_settings.dsc_bits_per_pixel;
7247 }
7248 #endif
7249
7250 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)7251 create_stream_for_sink(struct drm_connector *connector,
7252 const struct drm_display_mode *drm_mode,
7253 const struct dm_connector_state *dm_state,
7254 const struct dc_stream_state *old_stream,
7255 int requested_bpc)
7256 {
7257 struct drm_device *dev = connector->dev;
7258 struct amdgpu_dm_connector *aconnector = NULL;
7259 struct drm_display_mode *preferred_mode = NULL;
7260 const struct drm_connector_state *con_state = &dm_state->base;
7261 struct dc_stream_state *stream = NULL;
7262 struct drm_display_mode mode;
7263 struct drm_display_mode saved_mode;
7264 struct drm_display_mode *freesync_mode = NULL;
7265 bool native_mode_found = false;
7266 bool recalculate_timing = false;
7267 bool scale = dm_state->scaling != RMX_OFF;
7268 int mode_refresh;
7269 int preferred_refresh = 0;
7270 enum color_transfer_func tf = TRANSFER_FUNC_UNKNOWN;
7271 #if defined(CONFIG_DRM_AMD_DC_FP)
7272 struct dsc_dec_dpcd_caps dsc_caps;
7273 #endif
7274 struct dc_link *link = NULL;
7275 struct dc_sink *sink = NULL;
7276
7277 drm_mode_init(&mode, drm_mode);
7278 memset(&saved_mode, 0, sizeof(saved_mode));
7279
7280 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) {
7281 aconnector = NULL;
7282 aconnector = to_amdgpu_dm_connector(connector);
7283 link = aconnector->dc_link;
7284 } else {
7285 struct drm_writeback_connector *wbcon = NULL;
7286 struct amdgpu_dm_wb_connector *dm_wbcon = NULL;
7287
7288 wbcon = drm_connector_to_writeback(connector);
7289 dm_wbcon = to_amdgpu_dm_wb_connector(wbcon);
7290 link = dm_wbcon->link;
7291 }
7292
7293 if (!aconnector || !aconnector->dc_sink) {
7294 sink = create_fake_sink(dev, link);
7295 if (!sink)
7296 return stream;
7297
7298 } else {
7299 sink = aconnector->dc_sink;
7300 dc_sink_retain(sink);
7301 }
7302
7303 stream = dc_create_stream_for_sink(sink);
7304
7305 if (stream == NULL) {
7306 drm_err(dev, "Failed to create stream for sink!\n");
7307 goto finish;
7308 }
7309
7310 /* We leave this NULL for writeback connectors */
7311 stream->dm_stream_context = aconnector;
7312
7313 stream->timing.flags.LTE_340MCSC_SCRAMBLE =
7314 connector->display_info.hdmi.scdc.scrambling.low_rates;
7315
7316 list_for_each_entry(preferred_mode, &connector->modes, head) {
7317 /* Search for preferred mode */
7318 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
7319 native_mode_found = true;
7320 break;
7321 }
7322 }
7323 if (!native_mode_found)
7324 preferred_mode = list_first_entry_or_null(
7325 &connector->modes,
7326 struct drm_display_mode,
7327 head);
7328
7329 mode_refresh = drm_mode_vrefresh(&mode);
7330
7331 if (preferred_mode == NULL) {
7332 /*
7333 * This may not be an error, the use case is when we have no
7334 * usermode calls to reset and set mode upon hotplug. In this
7335 * case, we call set mode ourselves to restore the previous mode
7336 * and the modelist may not be filled in time.
7337 */
7338 drm_dbg_driver(dev, "No preferred mode found\n");
7339 } else if (aconnector) {
7340 recalculate_timing = amdgpu_freesync_vid_mode &&
7341 is_freesync_video_mode(&mode, aconnector);
7342 if (recalculate_timing) {
7343 freesync_mode = get_highest_refresh_rate_mode(aconnector, false);
7344 drm_mode_copy(&saved_mode, &mode);
7345 saved_mode.picture_aspect_ratio = mode.picture_aspect_ratio;
7346 drm_mode_copy(&mode, freesync_mode);
7347 mode.picture_aspect_ratio = saved_mode.picture_aspect_ratio;
7348 } else {
7349 decide_crtc_timing_for_drm_display_mode(
7350 &mode, preferred_mode, scale);
7351
7352 preferred_refresh = drm_mode_vrefresh(preferred_mode);
7353 }
7354 }
7355
7356 if (recalculate_timing)
7357 drm_mode_set_crtcinfo(&saved_mode, 0);
7358
7359 /*
7360 * If scaling is enabled and refresh rate didn't change
7361 * we copy the vic and polarities of the old timings
7362 */
7363 if (!scale || mode_refresh != preferred_refresh)
7364 fill_stream_properties_from_drm_display_mode(
7365 stream, &mode, connector, con_state, NULL,
7366 requested_bpc);
7367 else
7368 fill_stream_properties_from_drm_display_mode(
7369 stream, &mode, connector, con_state, old_stream,
7370 requested_bpc);
7371
7372 /* The rest isn't needed for writeback connectors */
7373 if (!aconnector)
7374 goto finish;
7375
7376 if (aconnector->timing_changed) {
7377 drm_dbg(aconnector->base.dev,
7378 "overriding timing for automated test, bpc %d, changing to %d\n",
7379 stream->timing.display_color_depth,
7380 aconnector->timing_requested->display_color_depth);
7381 stream->timing = *aconnector->timing_requested;
7382 }
7383
7384 #if defined(CONFIG_DRM_AMD_DC_FP)
7385 /* SST DSC determination policy */
7386 update_dsc_caps(aconnector, sink, stream, &dsc_caps);
7387 if (aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE && dsc_caps.is_dsc_supported)
7388 apply_dsc_policy_for_stream(aconnector, sink, stream, &dsc_caps);
7389 #endif
7390
7391 update_stream_scaling_settings(dev, &mode, dm_state, stream);
7392
7393 fill_audio_info(
7394 &stream->audio_info,
7395 connector,
7396 sink);
7397
7398 update_stream_signal(stream, sink);
7399
7400 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
7401 mod_build_hf_vsif_infopacket(stream, &stream->vsp_infopacket);
7402
7403 if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT ||
7404 stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST ||
7405 stream->signal == SIGNAL_TYPE_EDP) {
7406 const struct dc_edid_caps *edid_caps;
7407 unsigned int disable_colorimetry = 0;
7408
7409 if (aconnector->dc_sink) {
7410 edid_caps = &aconnector->dc_sink->edid_caps;
7411 disable_colorimetry = edid_caps->panel_patch.disable_colorimetry;
7412 }
7413
7414 //
7415 // should decide stream support vsc sdp colorimetry capability
7416 // before building vsc info packet
7417 //
7418 stream->use_vsc_sdp_for_colorimetry = stream->link->dpcd_caps.dpcd_rev.raw >= 0x14 &&
7419 stream->link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED &&
7420 !disable_colorimetry;
7421
7422 if (stream->out_transfer_func.tf == TRANSFER_FUNCTION_GAMMA22)
7423 tf = TRANSFER_FUNC_GAMMA_22;
7424 mod_build_vsc_infopacket(stream, &stream->vsc_infopacket, stream->output_color_space, tf);
7425 aconnector->sr_skip_count = AMDGPU_DM_PSR_ENTRY_DELAY;
7426
7427 }
7428 finish:
7429 dc_sink_release(sink);
7430
7431 return stream;
7432 }
7433
7434 /**
7435 * amdgpu_dm_connector_poll - Poll a connector to see if it's connected to a display
7436 * @aconnector: DM connector to poll (owns @base drm_connector and @dc_link)
7437 * @force: if true, force polling even when DAC load detection was used
7438 *
7439 * Used for connectors that don't support HPD (hotplug detection) to
7440 * periodically check whether the connector is connected to a display.
7441 *
7442 * When connection was determined via DAC load detection, we avoid
7443 * re-running it on normal polls to prevent visible glitches, unless
7444 * @force is set.
7445 *
7446 * Return: The probed connector status (connected/disconnected/unknown).
7447 */
7448 static enum drm_connector_status
amdgpu_dm_connector_poll(struct amdgpu_dm_connector * aconnector,bool force)7449 amdgpu_dm_connector_poll(struct amdgpu_dm_connector *aconnector, bool force)
7450 {
7451 struct drm_connector *connector = &aconnector->base;
7452 struct drm_device *dev = connector->dev;
7453 struct amdgpu_device *adev = drm_to_adev(dev);
7454 struct dc_link *link = aconnector->dc_link;
7455 enum dc_connection_type conn_type = dc_connection_none;
7456 enum drm_connector_status status = connector_status_disconnected;
7457
7458 /* When we determined the connection using DAC load detection,
7459 * do NOT poll the connector do detect disconnect because
7460 * that would run DAC load detection again which can cause
7461 * visible visual glitches.
7462 *
7463 * Only allow to poll such a connector again when forcing.
7464 */
7465 if (!force && link->local_sink && link->type == dc_connection_analog_load)
7466 return connector->status;
7467
7468 mutex_lock(&aconnector->hpd_lock);
7469
7470 if (dc_link_detect_connection_type(aconnector->dc_link, &conn_type) &&
7471 conn_type != dc_connection_none) {
7472 mutex_lock(&adev->dm.dc_lock);
7473
7474 /* Only call full link detection when a sink isn't created yet,
7475 * ie. just when the display is plugged in, otherwise we risk flickering.
7476 */
7477 if (link->local_sink ||
7478 dc_link_detect(link, DETECT_REASON_HPD))
7479 status = connector_status_connected;
7480
7481 mutex_unlock(&adev->dm.dc_lock);
7482 }
7483
7484 if (connector->status != status) {
7485 if (status == connector_status_disconnected) {
7486 if (link->local_sink)
7487 dc_sink_release(link->local_sink);
7488
7489 link->local_sink = NULL;
7490 link->dpcd_sink_count = 0;
7491 link->type = dc_connection_none;
7492 }
7493
7494 amdgpu_dm_update_connector_after_detect(aconnector);
7495 }
7496
7497 mutex_unlock(&aconnector->hpd_lock);
7498 return status;
7499 }
7500
7501 /**
7502 * amdgpu_dm_connector_detect() - Detect whether a DRM connector is connected to a display
7503 *
7504 * A connector is considered connected when it has a sink that is not NULL.
7505 * For connectors that support HPD (hotplug detection), the connection is
7506 * handled in the HPD interrupt.
7507 * For connectors that may not support HPD, such as analog connectors,
7508 * DRM will call this function repeatedly to poll them.
7509 *
7510 * Notes:
7511 * 1. This interface is NOT called in context of HPD irq.
7512 * 2. This interface *is called* in context of user-mode ioctl. Which
7513 * makes it a bad place for *any* MST-related activity.
7514 *
7515 * @connector: The DRM connector we are checking. We convert it to
7516 * amdgpu_dm_connector so we can read the DC link and state.
7517 * @force: If true, do a full detect again. This is used even when
7518 * a lighter check would normally be used to avoid flicker.
7519 *
7520 * Return: The connector status (connected, disconnected, or unknown).
7521 *
7522 */
7523 static enum drm_connector_status
amdgpu_dm_connector_detect(struct drm_connector * connector,bool force)7524 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
7525 {
7526 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7527
7528 update_subconnector_property(aconnector);
7529
7530 if (aconnector->base.force == DRM_FORCE_ON ||
7531 aconnector->base.force == DRM_FORCE_ON_DIGITAL)
7532 return connector_status_connected;
7533 else if (aconnector->base.force == DRM_FORCE_OFF)
7534 return connector_status_disconnected;
7535
7536 /* Poll analog connectors and only when either
7537 * disconnected or connected to an analog display.
7538 */
7539 if (drm_kms_helper_is_poll_worker() &&
7540 dc_connector_supports_analog(aconnector->dc_link->link_id.id) &&
7541 (!aconnector->dc_sink || aconnector->dc_sink->edid_caps.analog))
7542 return amdgpu_dm_connector_poll(aconnector, force);
7543
7544 return (aconnector->dc_sink ? connector_status_connected :
7545 connector_status_disconnected);
7546 }
7547
amdgpu_dm_connector_atomic_set_property(struct drm_connector * connector,struct drm_connector_state * connector_state,struct drm_property * property,uint64_t val)7548 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
7549 struct drm_connector_state *connector_state,
7550 struct drm_property *property,
7551 uint64_t val)
7552 {
7553 struct drm_device *dev = connector->dev;
7554 struct amdgpu_device *adev = drm_to_adev(dev);
7555 struct dm_connector_state *dm_old_state =
7556 to_dm_connector_state(connector->state);
7557 struct dm_connector_state *dm_new_state =
7558 to_dm_connector_state(connector_state);
7559
7560 int ret = -EINVAL;
7561
7562 if (property == dev->mode_config.scaling_mode_property) {
7563 enum amdgpu_rmx_type rmx_type;
7564
7565 switch (val) {
7566 case DRM_MODE_SCALE_CENTER:
7567 rmx_type = RMX_CENTER;
7568 break;
7569 case DRM_MODE_SCALE_ASPECT:
7570 rmx_type = RMX_ASPECT;
7571 break;
7572 case DRM_MODE_SCALE_FULLSCREEN:
7573 rmx_type = RMX_FULL;
7574 break;
7575 case DRM_MODE_SCALE_NONE:
7576 default:
7577 rmx_type = RMX_OFF;
7578 break;
7579 }
7580
7581 if (dm_old_state->scaling == rmx_type)
7582 return 0;
7583
7584 dm_new_state->scaling = rmx_type;
7585 ret = 0;
7586 } else if (property == adev->mode_info.underscan_hborder_property) {
7587 dm_new_state->underscan_hborder = val;
7588 ret = 0;
7589 } else if (property == adev->mode_info.underscan_vborder_property) {
7590 dm_new_state->underscan_vborder = val;
7591 ret = 0;
7592 } else if (property == adev->mode_info.underscan_property) {
7593 dm_new_state->underscan_enable = val;
7594 ret = 0;
7595 } else if (property == adev->mode_info.abm_level_property) {
7596 switch (val) {
7597 case ABM_SYSFS_CONTROL:
7598 dm_new_state->abm_sysfs_forbidden = false;
7599 break;
7600 case ABM_LEVEL_OFF:
7601 dm_new_state->abm_sysfs_forbidden = true;
7602 dm_new_state->abm_level = ABM_LEVEL_IMMEDIATE_DISABLE;
7603 break;
7604 default:
7605 dm_new_state->abm_sysfs_forbidden = true;
7606 dm_new_state->abm_level = val;
7607 }
7608 ret = 0;
7609 }
7610
7611 return ret;
7612 }
7613
amdgpu_dm_connector_atomic_get_property(struct drm_connector * connector,const struct drm_connector_state * state,struct drm_property * property,uint64_t * val)7614 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
7615 const struct drm_connector_state *state,
7616 struct drm_property *property,
7617 uint64_t *val)
7618 {
7619 struct drm_device *dev = connector->dev;
7620 struct amdgpu_device *adev = drm_to_adev(dev);
7621 struct dm_connector_state *dm_state =
7622 to_dm_connector_state(state);
7623 int ret = -EINVAL;
7624
7625 if (property == dev->mode_config.scaling_mode_property) {
7626 switch (dm_state->scaling) {
7627 case RMX_CENTER:
7628 *val = DRM_MODE_SCALE_CENTER;
7629 break;
7630 case RMX_ASPECT:
7631 *val = DRM_MODE_SCALE_ASPECT;
7632 break;
7633 case RMX_FULL:
7634 *val = DRM_MODE_SCALE_FULLSCREEN;
7635 break;
7636 case RMX_OFF:
7637 default:
7638 *val = DRM_MODE_SCALE_NONE;
7639 break;
7640 }
7641 ret = 0;
7642 } else if (property == adev->mode_info.underscan_hborder_property) {
7643 *val = dm_state->underscan_hborder;
7644 ret = 0;
7645 } else if (property == adev->mode_info.underscan_vborder_property) {
7646 *val = dm_state->underscan_vborder;
7647 ret = 0;
7648 } else if (property == adev->mode_info.underscan_property) {
7649 *val = dm_state->underscan_enable;
7650 ret = 0;
7651 } else if (property == adev->mode_info.abm_level_property) {
7652 if (!dm_state->abm_sysfs_forbidden)
7653 *val = ABM_SYSFS_CONTROL;
7654 else
7655 *val = (dm_state->abm_level != ABM_LEVEL_IMMEDIATE_DISABLE) ?
7656 dm_state->abm_level : 0;
7657 ret = 0;
7658 }
7659
7660 return ret;
7661 }
7662
7663 /**
7664 * DOC: panel power savings
7665 *
7666 * The display manager allows you to set your desired **panel power savings**
7667 * level (between 0-4, with 0 representing off), e.g. using the following::
7668 *
7669 * # echo 3 > /sys/class/drm/card0-eDP-1/amdgpu/panel_power_savings
7670 *
7671 * Modifying this value can have implications on color accuracy, so tread
7672 * carefully.
7673 */
7674
panel_power_savings_show(struct device * device,struct device_attribute * attr,char * buf)7675 static ssize_t panel_power_savings_show(struct device *device,
7676 struct device_attribute *attr,
7677 char *buf)
7678 {
7679 struct drm_connector *connector = dev_get_drvdata(device);
7680 struct drm_device *dev = connector->dev;
7681 u8 val;
7682
7683 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
7684 val = to_dm_connector_state(connector->state)->abm_level ==
7685 ABM_LEVEL_IMMEDIATE_DISABLE ? 0 :
7686 to_dm_connector_state(connector->state)->abm_level;
7687 drm_modeset_unlock(&dev->mode_config.connection_mutex);
7688
7689 return sysfs_emit(buf, "%u\n", val);
7690 }
7691
panel_power_savings_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)7692 static ssize_t panel_power_savings_store(struct device *device,
7693 struct device_attribute *attr,
7694 const char *buf, size_t count)
7695 {
7696 struct drm_connector *connector = dev_get_drvdata(device);
7697 struct drm_device *dev = connector->dev;
7698 long val;
7699 int ret;
7700
7701 ret = kstrtol(buf, 0, &val);
7702
7703 if (ret)
7704 return ret;
7705
7706 if (val < 0 || val > 4)
7707 return -EINVAL;
7708
7709 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
7710 if (to_dm_connector_state(connector->state)->abm_sysfs_forbidden)
7711 ret = -EBUSY;
7712 else
7713 to_dm_connector_state(connector->state)->abm_level = val ?:
7714 ABM_LEVEL_IMMEDIATE_DISABLE;
7715 drm_modeset_unlock(&dev->mode_config.connection_mutex);
7716
7717 if (ret)
7718 return ret;
7719
7720 drm_kms_helper_hotplug_event(dev);
7721
7722 return count;
7723 }
7724
7725 static DEVICE_ATTR_RW(panel_power_savings);
7726
7727 static struct attribute *amdgpu_attrs[] = {
7728 &dev_attr_panel_power_savings.attr,
7729 NULL
7730 };
7731
7732 static const struct attribute_group amdgpu_group = {
7733 .name = "amdgpu",
7734 .attrs = amdgpu_attrs
7735 };
7736
7737 static bool
amdgpu_dm_should_create_sysfs(struct amdgpu_dm_connector * amdgpu_dm_connector)7738 amdgpu_dm_should_create_sysfs(struct amdgpu_dm_connector *amdgpu_dm_connector)
7739 {
7740 if (amdgpu_dm_abm_level >= 0)
7741 return false;
7742
7743 if (amdgpu_dm_connector->base.connector_type != DRM_MODE_CONNECTOR_eDP)
7744 return false;
7745
7746 /* check for OLED panels */
7747 if (amdgpu_dm_connector->bl_idx >= 0) {
7748 struct drm_device *drm = amdgpu_dm_connector->base.dev;
7749 struct amdgpu_display_manager *dm = &drm_to_adev(drm)->dm;
7750 struct amdgpu_dm_backlight_caps *caps;
7751
7752 caps = &dm->backlight_caps[amdgpu_dm_connector->bl_idx];
7753 if (caps->aux_support)
7754 return false;
7755 }
7756
7757 return true;
7758 }
7759
amdgpu_dm_connector_unregister(struct drm_connector * connector)7760 static void amdgpu_dm_connector_unregister(struct drm_connector *connector)
7761 {
7762 struct amdgpu_dm_connector *amdgpu_dm_connector = to_amdgpu_dm_connector(connector);
7763
7764 if (amdgpu_dm_should_create_sysfs(amdgpu_dm_connector))
7765 sysfs_remove_group(&connector->kdev->kobj, &amdgpu_group);
7766
7767 cec_notifier_conn_unregister(amdgpu_dm_connector->notifier);
7768 drm_dp_aux_unregister(&amdgpu_dm_connector->dm_dp_aux.aux);
7769 }
7770
amdgpu_dm_connector_destroy(struct drm_connector * connector)7771 static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
7772 {
7773 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7774 struct amdgpu_device *adev = drm_to_adev(connector->dev);
7775 struct amdgpu_display_manager *dm = &adev->dm;
7776
7777 /*
7778 * Call only if mst_mgr was initialized before since it's not done
7779 * for all connector types.
7780 */
7781 if (aconnector->mst_mgr.dev)
7782 drm_dp_mst_topology_mgr_destroy(&aconnector->mst_mgr);
7783
7784 /* Cancel and flush any pending HDMI HPD debounce work */
7785 if (aconnector->hdmi_hpd_debounce_delay_ms) {
7786 cancel_delayed_work_sync(&aconnector->hdmi_hpd_debounce_work);
7787 if (aconnector->hdmi_prev_sink) {
7788 dc_sink_release(aconnector->hdmi_prev_sink);
7789 aconnector->hdmi_prev_sink = NULL;
7790 }
7791 }
7792
7793 if (aconnector->bl_idx != -1) {
7794 backlight_device_unregister(dm->backlight_dev[aconnector->bl_idx]);
7795 dm->backlight_dev[aconnector->bl_idx] = NULL;
7796 }
7797
7798 if (aconnector->dc_em_sink)
7799 dc_sink_release(aconnector->dc_em_sink);
7800 aconnector->dc_em_sink = NULL;
7801 if (aconnector->dc_sink)
7802 dc_sink_release(aconnector->dc_sink);
7803 aconnector->dc_sink = NULL;
7804
7805 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
7806 drm_connector_unregister(connector);
7807 drm_connector_cleanup(connector);
7808 kfree(aconnector->dm_dp_aux.aux.name);
7809
7810 kfree(connector);
7811 }
7812
amdgpu_dm_connector_funcs_reset(struct drm_connector * connector)7813 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
7814 {
7815 struct dm_connector_state *state =
7816 to_dm_connector_state(connector->state);
7817
7818 if (connector->state)
7819 __drm_atomic_helper_connector_destroy_state(connector->state);
7820
7821 kfree(state);
7822
7823 state = kzalloc_obj(*state);
7824
7825 if (state) {
7826 state->scaling = RMX_OFF;
7827 state->underscan_enable = false;
7828 state->underscan_hborder = 0;
7829 state->underscan_vborder = 0;
7830 state->base.max_requested_bpc = 8;
7831 state->vcpi_slots = 0;
7832 state->pbn = 0;
7833
7834 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
7835 if (amdgpu_dm_abm_level <= 0)
7836 state->abm_level = ABM_LEVEL_IMMEDIATE_DISABLE;
7837 else
7838 state->abm_level = amdgpu_dm_abm_level;
7839 }
7840
7841 __drm_atomic_helper_connector_reset(connector, &state->base);
7842 }
7843 }
7844
7845 struct drm_connector_state *
amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector * connector)7846 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
7847 {
7848 struct dm_connector_state *state =
7849 to_dm_connector_state(connector->state);
7850
7851 struct dm_connector_state *new_state =
7852 kmemdup(state, sizeof(*state), GFP_KERNEL);
7853
7854 if (!new_state)
7855 return NULL;
7856
7857 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
7858
7859 new_state->freesync_capable = state->freesync_capable;
7860 new_state->abm_level = state->abm_level;
7861 new_state->scaling = state->scaling;
7862 new_state->underscan_enable = state->underscan_enable;
7863 new_state->underscan_hborder = state->underscan_hborder;
7864 new_state->underscan_vborder = state->underscan_vborder;
7865 new_state->vcpi_slots = state->vcpi_slots;
7866 new_state->pbn = state->pbn;
7867 return &new_state->base;
7868 }
7869
7870 static int
amdgpu_dm_connector_late_register(struct drm_connector * connector)7871 amdgpu_dm_connector_late_register(struct drm_connector *connector)
7872 {
7873 struct amdgpu_dm_connector *amdgpu_dm_connector =
7874 to_amdgpu_dm_connector(connector);
7875 int r;
7876
7877 if (amdgpu_dm_should_create_sysfs(amdgpu_dm_connector)) {
7878 r = sysfs_create_group(&connector->kdev->kobj,
7879 &amdgpu_group);
7880 if (r)
7881 return r;
7882 }
7883
7884 amdgpu_dm_register_backlight_device(amdgpu_dm_connector);
7885
7886 if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
7887 (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) {
7888 amdgpu_dm_connector->dm_dp_aux.aux.dev = connector->kdev;
7889 r = drm_dp_aux_register(&amdgpu_dm_connector->dm_dp_aux.aux);
7890 if (r)
7891 return r;
7892 }
7893
7894 #if defined(CONFIG_DEBUG_FS)
7895 connector_debugfs_init(amdgpu_dm_connector);
7896 #endif
7897
7898 return 0;
7899 }
7900
amdgpu_dm_connector_funcs_force(struct drm_connector * connector)7901 static void amdgpu_dm_connector_funcs_force(struct drm_connector *connector)
7902 {
7903 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7904 struct dc_link *dc_link = aconnector->dc_link;
7905 struct dc_sink *dc_em_sink = aconnector->dc_em_sink;
7906 const struct drm_edid *drm_edid;
7907 struct i2c_adapter *ddc;
7908 struct drm_device *dev = connector->dev;
7909
7910 if (dc_link && dc_link->aux_mode)
7911 ddc = &aconnector->dm_dp_aux.aux.ddc;
7912 else
7913 ddc = &aconnector->i2c->base;
7914
7915 drm_edid = drm_edid_read_ddc(connector, ddc);
7916 drm_edid_connector_update(connector, drm_edid);
7917 if (!drm_edid) {
7918 drm_err(dev, "No EDID found on connector: %s.\n", connector->name);
7919 return;
7920 }
7921
7922 aconnector->drm_edid = drm_edid;
7923 /* Update emulated (virtual) sink's EDID */
7924 if (dc_em_sink && dc_link) {
7925 // FIXME: Get rid of drm_edid_raw()
7926 const struct edid *edid = drm_edid_raw(drm_edid);
7927
7928 memset(&dc_em_sink->edid_caps, 0, sizeof(struct dc_edid_caps));
7929 memmove(dc_em_sink->dc_edid.raw_edid, edid,
7930 (edid->extensions + 1) * EDID_LENGTH);
7931 dm_helpers_parse_edid_caps(
7932 dc_link,
7933 &dc_em_sink->dc_edid,
7934 &dc_em_sink->edid_caps);
7935 }
7936 }
7937
7938 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
7939 .reset = amdgpu_dm_connector_funcs_reset,
7940 .detect = amdgpu_dm_connector_detect,
7941 .fill_modes = drm_helper_probe_single_connector_modes,
7942 .destroy = amdgpu_dm_connector_destroy,
7943 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
7944 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
7945 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
7946 .atomic_get_property = amdgpu_dm_connector_atomic_get_property,
7947 .late_register = amdgpu_dm_connector_late_register,
7948 .early_unregister = amdgpu_dm_connector_unregister,
7949 .force = amdgpu_dm_connector_funcs_force
7950 };
7951
get_modes(struct drm_connector * connector)7952 static int get_modes(struct drm_connector *connector)
7953 {
7954 return amdgpu_dm_connector_get_modes(connector);
7955 }
7956
create_eml_sink(struct amdgpu_dm_connector * aconnector)7957 static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
7958 {
7959 struct drm_connector *connector = &aconnector->base;
7960 struct dc_link *dc_link = aconnector->dc_link;
7961 struct dc_sink_init_data init_params = {
7962 .link = aconnector->dc_link,
7963 .sink_signal = SIGNAL_TYPE_VIRTUAL
7964 };
7965 const struct drm_edid *drm_edid;
7966 const struct edid *edid;
7967 struct i2c_adapter *ddc;
7968
7969 if (dc_link && dc_link->aux_mode)
7970 ddc = &aconnector->dm_dp_aux.aux.ddc;
7971 else
7972 ddc = &aconnector->i2c->base;
7973
7974 drm_edid = drm_edid_read_ddc(connector, ddc);
7975 drm_edid_connector_update(connector, drm_edid);
7976 if (!drm_edid) {
7977 drm_err(connector->dev, "No EDID found on connector: %s.\n", connector->name);
7978 return;
7979 }
7980
7981 if (connector->display_info.is_hdmi)
7982 init_params.sink_signal = SIGNAL_TYPE_HDMI_TYPE_A;
7983
7984 aconnector->drm_edid = drm_edid;
7985
7986 edid = drm_edid_raw(drm_edid); // FIXME: Get rid of drm_edid_raw()
7987 aconnector->dc_em_sink = dc_link_add_remote_sink(
7988 aconnector->dc_link,
7989 (uint8_t *)edid,
7990 (edid->extensions + 1) * EDID_LENGTH,
7991 &init_params);
7992
7993 if (aconnector->base.force == DRM_FORCE_ON) {
7994 aconnector->dc_sink = aconnector->dc_link->local_sink ?
7995 aconnector->dc_link->local_sink :
7996 aconnector->dc_em_sink;
7997 if (aconnector->dc_sink)
7998 dc_sink_retain(aconnector->dc_sink);
7999 }
8000 }
8001
handle_edid_mgmt(struct amdgpu_dm_connector * aconnector)8002 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
8003 {
8004 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
8005
8006 /*
8007 * In case of headless boot with force on for DP managed connector
8008 * Those settings have to be != 0 to get initial modeset
8009 */
8010 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
8011 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
8012 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
8013 }
8014
8015 create_eml_sink(aconnector);
8016 }
8017
dm_validate_stream_and_context(struct dc * dc,struct dc_stream_state * stream)8018 static enum dc_status dm_validate_stream_and_context(struct dc *dc,
8019 struct dc_stream_state *stream)
8020 {
8021 enum dc_status dc_result = DC_ERROR_UNEXPECTED;
8022 struct dc_plane_state *dc_plane_state = NULL;
8023 struct dc_state *dc_state = NULL;
8024
8025 if (!stream)
8026 goto cleanup;
8027
8028 dc_plane_state = dc_create_plane_state(dc);
8029 if (!dc_plane_state)
8030 goto cleanup;
8031
8032 dc_state = dc_state_create(dc, NULL);
8033 if (!dc_state)
8034 goto cleanup;
8035
8036 /* populate stream to plane */
8037 dc_plane_state->src_rect.height = stream->src.height;
8038 dc_plane_state->src_rect.width = stream->src.width;
8039 dc_plane_state->dst_rect.height = stream->src.height;
8040 dc_plane_state->dst_rect.width = stream->src.width;
8041 dc_plane_state->clip_rect.height = stream->src.height;
8042 dc_plane_state->clip_rect.width = stream->src.width;
8043 dc_plane_state->plane_size.surface_pitch = ((stream->src.width + 255) / 256) * 256;
8044 dc_plane_state->plane_size.surface_size.height = stream->src.height;
8045 dc_plane_state->plane_size.surface_size.width = stream->src.width;
8046 dc_plane_state->plane_size.chroma_size.height = stream->src.height;
8047 dc_plane_state->plane_size.chroma_size.width = stream->src.width;
8048 dc_plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
8049 dc_plane_state->tiling_info.gfx9.swizzle = DC_SW_UNKNOWN;
8050 dc_plane_state->rotation = ROTATION_ANGLE_0;
8051 dc_plane_state->is_tiling_rotated = false;
8052 dc_plane_state->tiling_info.gfx8.array_mode = DC_ARRAY_LINEAR_GENERAL;
8053
8054 dc_result = dc_validate_stream(dc, stream);
8055 if (dc_result == DC_OK)
8056 dc_result = dc_validate_plane(dc, dc_plane_state);
8057
8058 if (dc_result == DC_OK)
8059 dc_result = dc_state_add_stream(dc, dc_state, stream);
8060
8061 if (dc_result == DC_OK && !dc_state_add_plane(
8062 dc,
8063 stream,
8064 dc_plane_state,
8065 dc_state))
8066 dc_result = DC_FAIL_ATTACH_SURFACES;
8067
8068 if (dc_result == DC_OK)
8069 dc_result = dc_validate_global_state(dc, dc_state, DC_VALIDATE_MODE_ONLY);
8070
8071 cleanup:
8072 if (dc_state)
8073 dc_state_release(dc_state);
8074
8075 if (dc_plane_state)
8076 dc_plane_state_release(dc_plane_state);
8077
8078 return dc_result;
8079 }
8080
8081 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)8082 create_validate_stream_for_sink(struct drm_connector *connector,
8083 const struct drm_display_mode *drm_mode,
8084 const struct dm_connector_state *dm_state,
8085 const struct dc_stream_state *old_stream)
8086 {
8087 struct amdgpu_dm_connector *aconnector = NULL;
8088 struct amdgpu_device *adev = drm_to_adev(connector->dev);
8089 struct dc_stream_state *stream;
8090 const struct drm_connector_state *drm_state = dm_state ? &dm_state->base : NULL;
8091 int requested_bpc = drm_state ? drm_state->max_requested_bpc : 8;
8092 enum dc_status dc_result = DC_OK;
8093 uint8_t bpc_limit = 6;
8094
8095 if (!dm_state)
8096 return NULL;
8097
8098 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
8099 aconnector = to_amdgpu_dm_connector(connector);
8100
8101 if (aconnector &&
8102 (aconnector->dc_link->connector_signal == SIGNAL_TYPE_HDMI_TYPE_A ||
8103 aconnector->dc_link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER))
8104 bpc_limit = 8;
8105
8106 do {
8107 drm_dbg_kms(connector->dev, "Trying with %d bpc\n", requested_bpc);
8108 stream = create_stream_for_sink(connector, drm_mode,
8109 dm_state, old_stream,
8110 requested_bpc);
8111 if (stream == NULL) {
8112 drm_err(adev_to_drm(adev), "Failed to create stream for sink!\n");
8113 break;
8114 }
8115
8116 dc_result = dc_validate_stream(adev->dm.dc, stream);
8117
8118 if (!aconnector) /* writeback connector */
8119 return stream;
8120
8121 if (dc_result == DC_OK && stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
8122 dc_result = dm_dp_mst_is_port_support_mode(aconnector, stream);
8123
8124 if (dc_result == DC_OK)
8125 dc_result = dm_validate_stream_and_context(adev->dm.dc, stream);
8126
8127 if (dc_result != DC_OK) {
8128 drm_dbg_kms(connector->dev, "Pruned mode %d x %d (clk %d) %s %s -- %s\n",
8129 drm_mode->hdisplay,
8130 drm_mode->vdisplay,
8131 drm_mode->clock,
8132 dc_pixel_encoding_to_str(stream->timing.pixel_encoding),
8133 dc_color_depth_to_str(stream->timing.display_color_depth),
8134 dc_status_to_str(dc_result));
8135
8136 dc_stream_release(stream);
8137 stream = NULL;
8138 requested_bpc -= 2; /* lower bpc to retry validation */
8139 }
8140
8141 } while (stream == NULL && requested_bpc >= bpc_limit);
8142
8143 switch (dc_result) {
8144 /*
8145 * If we failed to validate DP bandwidth stream with the requested RGB color depth,
8146 * we try to fallback and configure in order:
8147 * YUV422 (8bpc, 6bpc)
8148 * YUV420 (8bpc, 6bpc)
8149 */
8150 case DC_FAIL_ENC_VALIDATE:
8151 case DC_EXCEED_DONGLE_CAP:
8152 case DC_NO_DP_LINK_BANDWIDTH:
8153 /* recursively entered twice and already tried both YUV422 and YUV420 */
8154 if (aconnector->force_yuv422_output && aconnector->force_yuv420_output)
8155 break;
8156 /* first failure; try YUV422 */
8157 if (!aconnector->force_yuv422_output) {
8158 drm_dbg_kms(connector->dev, "%s:%d Validation failed with %d, retrying w/ YUV422\n",
8159 __func__, __LINE__, dc_result);
8160 aconnector->force_yuv422_output = true;
8161 /* recursively entered and YUV422 failed, try YUV420 */
8162 } else if (!aconnector->force_yuv420_output) {
8163 drm_dbg_kms(connector->dev, "%s:%d Validation failed with %d, retrying w/ YUV420\n",
8164 __func__, __LINE__, dc_result);
8165 aconnector->force_yuv420_output = true;
8166 }
8167 stream = create_validate_stream_for_sink(connector, drm_mode,
8168 dm_state, old_stream);
8169 aconnector->force_yuv422_output = false;
8170 aconnector->force_yuv420_output = false;
8171 break;
8172 case DC_OK:
8173 break;
8174 default:
8175 drm_dbg_kms(connector->dev, "%s:%d Unhandled validation failure %d\n",
8176 __func__, __LINE__, dc_result);
8177 break;
8178 }
8179
8180 return stream;
8181 }
8182
amdgpu_dm_connector_mode_valid(struct drm_connector * connector,const struct drm_display_mode * mode)8183 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
8184 const struct drm_display_mode *mode)
8185 {
8186 int result = MODE_ERROR;
8187 struct dc_sink *dc_sink;
8188 struct drm_display_mode *test_mode;
8189 /* TODO: Unhardcode stream count */
8190 struct dc_stream_state *stream;
8191 /* we always have an amdgpu_dm_connector here since we got
8192 * here via the amdgpu_dm_connector_helper_funcs
8193 */
8194 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
8195
8196 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
8197 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
8198 return result;
8199
8200 /*
8201 * Only run this the first time mode_valid is called to initilialize
8202 * EDID mgmt
8203 */
8204 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
8205 !aconnector->dc_em_sink)
8206 handle_edid_mgmt(aconnector);
8207
8208 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
8209
8210 if (dc_sink == NULL && aconnector->base.force != DRM_FORCE_ON_DIGITAL &&
8211 aconnector->base.force != DRM_FORCE_ON) {
8212 drm_err(connector->dev, "dc_sink is NULL!\n");
8213 goto fail;
8214 }
8215
8216 test_mode = drm_mode_duplicate(connector->dev, mode);
8217 if (!test_mode)
8218 goto fail;
8219
8220 drm_mode_set_crtcinfo(test_mode, 0);
8221
8222 stream = create_validate_stream_for_sink(connector, test_mode,
8223 to_dm_connector_state(connector->state),
8224 NULL);
8225 drm_mode_destroy(connector->dev, test_mode);
8226 if (stream) {
8227 dc_stream_release(stream);
8228 result = MODE_OK;
8229 }
8230
8231 fail:
8232 /* TODO: error handling*/
8233 return result;
8234 }
8235
fill_hdr_info_packet(const struct drm_connector_state * state,struct dc_info_packet * out)8236 static int fill_hdr_info_packet(const struct drm_connector_state *state,
8237 struct dc_info_packet *out)
8238 {
8239 struct hdmi_drm_infoframe frame;
8240 unsigned char buf[30]; /* 26 + 4 */
8241 ssize_t len;
8242 int ret, i;
8243
8244 memset(out, 0, sizeof(*out));
8245
8246 if (!state->hdr_output_metadata)
8247 return 0;
8248
8249 ret = drm_hdmi_infoframe_set_hdr_metadata(&frame, state);
8250 if (ret)
8251 return ret;
8252
8253 len = hdmi_drm_infoframe_pack_only(&frame, buf, sizeof(buf));
8254 if (len < 0)
8255 return (int)len;
8256
8257 /* Static metadata is a fixed 26 bytes + 4 byte header. */
8258 if (len != 30)
8259 return -EINVAL;
8260
8261 /* Prepare the infopacket for DC. */
8262 switch (state->connector->connector_type) {
8263 case DRM_MODE_CONNECTOR_HDMIA:
8264 out->hb0 = 0x87; /* type */
8265 out->hb1 = 0x01; /* version */
8266 out->hb2 = 0x1A; /* length */
8267 out->sb[0] = buf[3]; /* checksum */
8268 i = 1;
8269 break;
8270
8271 case DRM_MODE_CONNECTOR_DisplayPort:
8272 case DRM_MODE_CONNECTOR_eDP:
8273 out->hb0 = 0x00; /* sdp id, zero */
8274 out->hb1 = 0x87; /* type */
8275 out->hb2 = 0x1D; /* payload len - 1 */
8276 out->hb3 = (0x13 << 2); /* sdp version */
8277 out->sb[0] = 0x01; /* version */
8278 out->sb[1] = 0x1A; /* length */
8279 i = 2;
8280 break;
8281
8282 default:
8283 return -EINVAL;
8284 }
8285
8286 memcpy(&out->sb[i], &buf[4], 26);
8287 out->valid = true;
8288
8289 print_hex_dump(KERN_DEBUG, "HDR SB:", DUMP_PREFIX_NONE, 16, 1, out->sb,
8290 sizeof(out->sb), false);
8291
8292 return 0;
8293 }
8294
8295 static int
amdgpu_dm_connector_atomic_check(struct drm_connector * conn,struct drm_atomic_state * state)8296 amdgpu_dm_connector_atomic_check(struct drm_connector *conn,
8297 struct drm_atomic_state *state)
8298 {
8299 struct drm_connector_state *new_con_state =
8300 drm_atomic_get_new_connector_state(state, conn);
8301 struct drm_connector_state *old_con_state =
8302 drm_atomic_get_old_connector_state(state, conn);
8303 struct drm_crtc *crtc = new_con_state->crtc;
8304 struct drm_crtc_state *new_crtc_state;
8305 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(conn);
8306 int ret;
8307
8308 if (WARN_ON(unlikely(!old_con_state || !new_con_state)))
8309 return -EINVAL;
8310
8311 trace_amdgpu_dm_connector_atomic_check(new_con_state);
8312
8313 if (conn->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
8314 ret = drm_dp_mst_root_conn_atomic_check(new_con_state, &aconn->mst_mgr);
8315 if (ret < 0)
8316 return ret;
8317 }
8318
8319 if (!crtc)
8320 return 0;
8321
8322 if (new_con_state->privacy_screen_sw_state != old_con_state->privacy_screen_sw_state) {
8323 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8324 if (IS_ERR(new_crtc_state))
8325 return PTR_ERR(new_crtc_state);
8326
8327 new_crtc_state->mode_changed = true;
8328 }
8329
8330 if (new_con_state->colorspace != old_con_state->colorspace) {
8331 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8332 if (IS_ERR(new_crtc_state))
8333 return PTR_ERR(new_crtc_state);
8334
8335 new_crtc_state->mode_changed = true;
8336 }
8337
8338 if (new_con_state->content_type != old_con_state->content_type) {
8339 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8340 if (IS_ERR(new_crtc_state))
8341 return PTR_ERR(new_crtc_state);
8342
8343 new_crtc_state->mode_changed = true;
8344 }
8345
8346 if (!drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state)) {
8347 struct dc_info_packet hdr_infopacket;
8348
8349 ret = fill_hdr_info_packet(new_con_state, &hdr_infopacket);
8350 if (ret)
8351 return ret;
8352
8353 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8354 if (IS_ERR(new_crtc_state))
8355 return PTR_ERR(new_crtc_state);
8356
8357 /*
8358 * DC considers the stream backends changed if the
8359 * static metadata changes. Forcing the modeset also
8360 * gives a simple way for userspace to switch from
8361 * 8bpc to 10bpc when setting the metadata to enter
8362 * or exit HDR.
8363 *
8364 * Changing the static metadata after it's been
8365 * set is permissible, however. So only force a
8366 * modeset if we're entering or exiting HDR.
8367 */
8368 new_crtc_state->mode_changed = new_crtc_state->mode_changed ||
8369 !old_con_state->hdr_output_metadata ||
8370 !new_con_state->hdr_output_metadata;
8371 }
8372
8373 return 0;
8374 }
8375
8376 static const struct drm_connector_helper_funcs
8377 amdgpu_dm_connector_helper_funcs = {
8378 /*
8379 * If hotplugging a second bigger display in FB Con mode, bigger resolution
8380 * modes will be filtered by drm_mode_validate_size(), and those modes
8381 * are missing after user start lightdm. So we need to renew modes list.
8382 * in get_modes call back, not just return the modes count
8383 */
8384 .get_modes = get_modes,
8385 .mode_valid = amdgpu_dm_connector_mode_valid,
8386 .atomic_check = amdgpu_dm_connector_atomic_check,
8387 };
8388
dm_encoder_helper_disable(struct drm_encoder * encoder)8389 static void dm_encoder_helper_disable(struct drm_encoder *encoder)
8390 {
8391
8392 }
8393
convert_dc_color_depth_into_bpc(enum dc_color_depth display_color_depth)8394 int convert_dc_color_depth_into_bpc(enum dc_color_depth display_color_depth)
8395 {
8396 switch (display_color_depth) {
8397 case COLOR_DEPTH_666:
8398 return 6;
8399 case COLOR_DEPTH_888:
8400 return 8;
8401 case COLOR_DEPTH_101010:
8402 return 10;
8403 case COLOR_DEPTH_121212:
8404 return 12;
8405 case COLOR_DEPTH_141414:
8406 return 14;
8407 case COLOR_DEPTH_161616:
8408 return 16;
8409 default:
8410 break;
8411 }
8412 return 0;
8413 }
8414
dm_encoder_helper_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)8415 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
8416 struct drm_crtc_state *crtc_state,
8417 struct drm_connector_state *conn_state)
8418 {
8419 struct drm_atomic_state *state = crtc_state->state;
8420 struct drm_connector *connector = conn_state->connector;
8421 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
8422 struct dm_connector_state *dm_new_connector_state = to_dm_connector_state(conn_state);
8423 const struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
8424 struct drm_dp_mst_topology_mgr *mst_mgr;
8425 struct drm_dp_mst_port *mst_port;
8426 struct drm_dp_mst_topology_state *mst_state;
8427 enum dc_color_depth color_depth;
8428 int clock, bpp = 0;
8429 bool is_y420 = false;
8430
8431 if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
8432 (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
8433 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8434 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8435 enum drm_mode_status result;
8436
8437 result = drm_crtc_helper_mode_valid_fixed(encoder->crtc, adjusted_mode, native_mode);
8438 if (result != MODE_OK && dm_new_connector_state->scaling == RMX_OFF) {
8439 drm_dbg_driver(encoder->dev,
8440 "mode %dx%d@%dHz is not native, enabling scaling\n",
8441 adjusted_mode->hdisplay, adjusted_mode->vdisplay,
8442 drm_mode_vrefresh(adjusted_mode));
8443 dm_new_connector_state->scaling = RMX_ASPECT;
8444 }
8445 return 0;
8446 }
8447
8448 if (!aconnector->mst_output_port)
8449 return 0;
8450
8451 mst_port = aconnector->mst_output_port;
8452 mst_mgr = &aconnector->mst_root->mst_mgr;
8453
8454 if (!crtc_state->connectors_changed && !crtc_state->mode_changed)
8455 return 0;
8456
8457 mst_state = drm_atomic_get_mst_topology_state(state, mst_mgr);
8458 if (IS_ERR(mst_state))
8459 return PTR_ERR(mst_state);
8460
8461 mst_state->pbn_div.full = dm_mst_get_pbn_divider(aconnector->mst_root->dc_link);
8462
8463 if (!state->duplicated) {
8464 int max_bpc = conn_state->max_requested_bpc;
8465
8466 is_y420 = drm_mode_is_420_also(&connector->display_info, adjusted_mode) &&
8467 aconnector->force_yuv420_output;
8468 color_depth = convert_color_depth_from_display_info(connector,
8469 is_y420,
8470 max_bpc);
8471 bpp = convert_dc_color_depth_into_bpc(color_depth) * 3;
8472 clock = adjusted_mode->clock;
8473 dm_new_connector_state->pbn = drm_dp_calc_pbn_mode(clock, bpp << 4);
8474 }
8475
8476 dm_new_connector_state->vcpi_slots =
8477 drm_dp_atomic_find_time_slots(state, mst_mgr, mst_port,
8478 dm_new_connector_state->pbn);
8479 if (dm_new_connector_state->vcpi_slots < 0) {
8480 drm_dbg_atomic(connector->dev, "failed finding vcpi slots: %d\n", (int)dm_new_connector_state->vcpi_slots);
8481 return dm_new_connector_state->vcpi_slots;
8482 }
8483 return 0;
8484 }
8485
8486 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
8487 .disable = dm_encoder_helper_disable,
8488 .atomic_check = dm_encoder_helper_atomic_check
8489 };
8490
dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_state * state,struct dc_state * dc_state,struct dsc_mst_fairness_vars * vars)8491 static int dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_state *state,
8492 struct dc_state *dc_state,
8493 struct dsc_mst_fairness_vars *vars)
8494 {
8495 struct dc_stream_state *stream = NULL;
8496 struct drm_connector *connector;
8497 struct drm_connector_state *new_con_state;
8498 struct amdgpu_dm_connector *aconnector;
8499 struct dm_connector_state *dm_conn_state;
8500 int i, j, ret;
8501 int vcpi, pbn_div, pbn = 0, slot_num = 0;
8502
8503 for_each_new_connector_in_state(state, connector, new_con_state, i) {
8504
8505 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
8506 continue;
8507
8508 aconnector = to_amdgpu_dm_connector(connector);
8509
8510 if (!aconnector->mst_output_port)
8511 continue;
8512
8513 if (!new_con_state || !new_con_state->crtc)
8514 continue;
8515
8516 dm_conn_state = to_dm_connector_state(new_con_state);
8517
8518 for (j = 0; j < dc_state->stream_count; j++) {
8519 stream = dc_state->streams[j];
8520 if (!stream)
8521 continue;
8522
8523 if ((struct amdgpu_dm_connector *)stream->dm_stream_context == aconnector)
8524 break;
8525
8526 stream = NULL;
8527 }
8528
8529 if (!stream)
8530 continue;
8531
8532 pbn_div = dm_mst_get_pbn_divider(stream->link);
8533 /* pbn is calculated by compute_mst_dsc_configs_for_state*/
8534 for (j = 0; j < dc_state->stream_count; j++) {
8535 if (vars[j].aconnector == aconnector) {
8536 pbn = vars[j].pbn;
8537 break;
8538 }
8539 }
8540
8541 if (j == dc_state->stream_count || pbn_div == 0)
8542 continue;
8543
8544 slot_num = DIV_ROUND_UP(pbn, pbn_div);
8545
8546 if (stream->timing.flags.DSC != 1) {
8547 dm_conn_state->pbn = pbn;
8548 dm_conn_state->vcpi_slots = slot_num;
8549
8550 ret = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port,
8551 dm_conn_state->pbn, false);
8552 if (ret < 0)
8553 return ret;
8554
8555 continue;
8556 }
8557
8558 vcpi = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port, pbn, true);
8559 if (vcpi < 0)
8560 return vcpi;
8561
8562 dm_conn_state->pbn = pbn;
8563 dm_conn_state->vcpi_slots = vcpi;
8564 }
8565 return 0;
8566 }
8567
to_drm_connector_type(enum signal_type st,uint32_t connector_id)8568 static int to_drm_connector_type(enum signal_type st, uint32_t connector_id)
8569 {
8570 switch (st) {
8571 case SIGNAL_TYPE_HDMI_TYPE_A:
8572 return DRM_MODE_CONNECTOR_HDMIA;
8573 case SIGNAL_TYPE_EDP:
8574 return DRM_MODE_CONNECTOR_eDP;
8575 case SIGNAL_TYPE_LVDS:
8576 return DRM_MODE_CONNECTOR_LVDS;
8577 case SIGNAL_TYPE_RGB:
8578 return DRM_MODE_CONNECTOR_VGA;
8579 case SIGNAL_TYPE_DISPLAY_PORT:
8580 case SIGNAL_TYPE_DISPLAY_PORT_MST:
8581 return DRM_MODE_CONNECTOR_DisplayPort;
8582 case SIGNAL_TYPE_DVI_DUAL_LINK:
8583 case SIGNAL_TYPE_DVI_SINGLE_LINK:
8584 if (connector_id == CONNECTOR_ID_SINGLE_LINK_DVII ||
8585 connector_id == CONNECTOR_ID_DUAL_LINK_DVII)
8586 return DRM_MODE_CONNECTOR_DVII;
8587
8588 return DRM_MODE_CONNECTOR_DVID;
8589 case SIGNAL_TYPE_VIRTUAL:
8590 return DRM_MODE_CONNECTOR_VIRTUAL;
8591
8592 default:
8593 return DRM_MODE_CONNECTOR_Unknown;
8594 }
8595 }
8596
amdgpu_dm_connector_to_encoder(struct drm_connector * connector)8597 static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector)
8598 {
8599 struct drm_encoder *encoder;
8600
8601 /* There is only one encoder per connector */
8602 drm_connector_for_each_possible_encoder(connector, encoder)
8603 return encoder;
8604
8605 return NULL;
8606 }
8607
amdgpu_dm_get_native_mode(struct drm_connector * connector)8608 static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
8609 {
8610 struct drm_encoder *encoder;
8611 struct amdgpu_encoder *amdgpu_encoder;
8612
8613 encoder = amdgpu_dm_connector_to_encoder(connector);
8614
8615 if (encoder == NULL)
8616 return;
8617
8618 amdgpu_encoder = to_amdgpu_encoder(encoder);
8619
8620 amdgpu_encoder->native_mode.clock = 0;
8621
8622 if (!list_empty(&connector->probed_modes)) {
8623 struct drm_display_mode *preferred_mode = NULL;
8624
8625 list_for_each_entry(preferred_mode,
8626 &connector->probed_modes,
8627 head) {
8628 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
8629 amdgpu_encoder->native_mode = *preferred_mode;
8630
8631 break;
8632 }
8633
8634 }
8635 }
8636
8637 static struct drm_display_mode *
amdgpu_dm_create_common_mode(struct drm_encoder * encoder,const char * name,int hdisplay,int vdisplay)8638 amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
8639 const char *name,
8640 int hdisplay, int vdisplay)
8641 {
8642 struct drm_device *dev = encoder->dev;
8643 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8644 struct drm_display_mode *mode = NULL;
8645 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8646
8647 mode = drm_mode_duplicate(dev, native_mode);
8648
8649 if (mode == NULL)
8650 return NULL;
8651
8652 mode->hdisplay = hdisplay;
8653 mode->vdisplay = vdisplay;
8654 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
8655 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
8656
8657 return mode;
8658
8659 }
8660
8661 static const struct amdgpu_dm_mode_size {
8662 char name[DRM_DISPLAY_MODE_LEN];
8663 int w;
8664 int h;
8665 } common_modes[] = {
8666 { "640x480", 640, 480},
8667 { "800x600", 800, 600},
8668 { "1024x768", 1024, 768},
8669 { "1280x720", 1280, 720},
8670 { "1280x800", 1280, 800},
8671 {"1280x1024", 1280, 1024},
8672 { "1440x900", 1440, 900},
8673 {"1680x1050", 1680, 1050},
8674 {"1600x1200", 1600, 1200},
8675 {"1920x1080", 1920, 1080},
8676 {"1920x1200", 1920, 1200}
8677 };
8678
amdgpu_dm_connector_add_common_modes(struct drm_encoder * encoder,struct drm_connector * connector)8679 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
8680 struct drm_connector *connector)
8681 {
8682 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8683 struct drm_display_mode *mode = NULL;
8684 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8685 struct amdgpu_dm_connector *amdgpu_dm_connector =
8686 to_amdgpu_dm_connector(connector);
8687 int i;
8688 int n;
8689
8690 if ((connector->connector_type != DRM_MODE_CONNECTOR_eDP) &&
8691 (connector->connector_type != DRM_MODE_CONNECTOR_LVDS))
8692 return;
8693
8694 n = ARRAY_SIZE(common_modes);
8695
8696 for (i = 0; i < n; i++) {
8697 struct drm_display_mode *curmode = NULL;
8698 bool mode_existed = false;
8699
8700 if (common_modes[i].w > native_mode->hdisplay ||
8701 common_modes[i].h > native_mode->vdisplay ||
8702 (common_modes[i].w == native_mode->hdisplay &&
8703 common_modes[i].h == native_mode->vdisplay))
8704 continue;
8705
8706 list_for_each_entry(curmode, &connector->probed_modes, head) {
8707 if (common_modes[i].w == curmode->hdisplay &&
8708 common_modes[i].h == curmode->vdisplay) {
8709 mode_existed = true;
8710 break;
8711 }
8712 }
8713
8714 if (mode_existed)
8715 continue;
8716
8717 mode = amdgpu_dm_create_common_mode(encoder,
8718 common_modes[i].name, common_modes[i].w,
8719 common_modes[i].h);
8720 if (!mode)
8721 continue;
8722
8723 drm_mode_probed_add(connector, mode);
8724 amdgpu_dm_connector->num_modes++;
8725 }
8726 }
8727
amdgpu_set_panel_orientation(struct drm_connector * connector)8728 static void amdgpu_set_panel_orientation(struct drm_connector *connector)
8729 {
8730 struct drm_encoder *encoder;
8731 struct amdgpu_encoder *amdgpu_encoder;
8732 const struct drm_display_mode *native_mode;
8733
8734 if (connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
8735 connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
8736 return;
8737
8738 mutex_lock(&connector->dev->mode_config.mutex);
8739 amdgpu_dm_connector_get_modes(connector);
8740 mutex_unlock(&connector->dev->mode_config.mutex);
8741
8742 encoder = amdgpu_dm_connector_to_encoder(connector);
8743 if (!encoder)
8744 return;
8745
8746 amdgpu_encoder = to_amdgpu_encoder(encoder);
8747
8748 native_mode = &amdgpu_encoder->native_mode;
8749 if (native_mode->hdisplay == 0 || native_mode->vdisplay == 0)
8750 return;
8751
8752 drm_connector_set_panel_orientation_with_quirk(connector,
8753 DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
8754 native_mode->hdisplay,
8755 native_mode->vdisplay);
8756 }
8757
amdgpu_dm_connector_ddc_get_modes(struct drm_connector * connector,const struct drm_edid * drm_edid)8758 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
8759 const struct drm_edid *drm_edid)
8760 {
8761 struct amdgpu_dm_connector *amdgpu_dm_connector =
8762 to_amdgpu_dm_connector(connector);
8763
8764 if (drm_edid) {
8765 /* empty probed_modes */
8766 INIT_LIST_HEAD(&connector->probed_modes);
8767 amdgpu_dm_connector->num_modes =
8768 drm_edid_connector_add_modes(connector);
8769
8770 /* sorting the probed modes before calling function
8771 * amdgpu_dm_get_native_mode() since EDID can have
8772 * more than one preferred mode. The modes that are
8773 * later in the probed mode list could be of higher
8774 * and preferred resolution. For example, 3840x2160
8775 * resolution in base EDID preferred timing and 4096x2160
8776 * preferred resolution in DID extension block later.
8777 */
8778 drm_mode_sort(&connector->probed_modes);
8779 amdgpu_dm_get_native_mode(connector);
8780
8781 /* Freesync capabilities are reset by calling
8782 * drm_edid_connector_add_modes() and need to be
8783 * restored here.
8784 */
8785 amdgpu_dm_update_freesync_caps(connector, drm_edid);
8786 } else {
8787 amdgpu_dm_connector->num_modes = 0;
8788 }
8789 }
8790
is_duplicate_mode(struct amdgpu_dm_connector * aconnector,struct drm_display_mode * mode)8791 static bool is_duplicate_mode(struct amdgpu_dm_connector *aconnector,
8792 struct drm_display_mode *mode)
8793 {
8794 struct drm_display_mode *m;
8795
8796 list_for_each_entry(m, &aconnector->base.probed_modes, head) {
8797 if (drm_mode_equal(m, mode))
8798 return true;
8799 }
8800
8801 return false;
8802 }
8803
add_fs_modes(struct amdgpu_dm_connector * aconnector)8804 static uint add_fs_modes(struct amdgpu_dm_connector *aconnector)
8805 {
8806 const struct drm_display_mode *m;
8807 struct drm_display_mode *new_mode;
8808 uint i;
8809 u32 new_modes_count = 0;
8810
8811 /* Standard FPS values
8812 *
8813 * 23.976 - TV/NTSC
8814 * 24 - Cinema
8815 * 25 - TV/PAL
8816 * 29.97 - TV/NTSC
8817 * 30 - TV/NTSC
8818 * 48 - Cinema HFR
8819 * 50 - TV/PAL
8820 * 60 - Commonly used
8821 * 48,72,96,120 - Multiples of 24
8822 */
8823 static const u32 common_rates[] = {
8824 23976, 24000, 25000, 29970, 30000,
8825 48000, 50000, 60000, 72000, 96000, 120000
8826 };
8827
8828 /*
8829 * Find mode with highest refresh rate with the same resolution
8830 * as the preferred mode. Some monitors report a preferred mode
8831 * with lower resolution than the highest refresh rate supported.
8832 */
8833
8834 m = get_highest_refresh_rate_mode(aconnector, true);
8835 if (!m)
8836 return 0;
8837
8838 for (i = 0; i < ARRAY_SIZE(common_rates); i++) {
8839 u64 target_vtotal, target_vtotal_diff;
8840 u64 num, den;
8841
8842 if (drm_mode_vrefresh(m) * 1000 < common_rates[i])
8843 continue;
8844
8845 if (common_rates[i] < aconnector->min_vfreq * 1000 ||
8846 common_rates[i] > aconnector->max_vfreq * 1000)
8847 continue;
8848
8849 num = (unsigned long long)m->clock * 1000 * 1000;
8850 den = common_rates[i] * (unsigned long long)m->htotal;
8851 target_vtotal = div_u64(num, den);
8852 target_vtotal_diff = target_vtotal - m->vtotal;
8853
8854 /* Check for illegal modes */
8855 if (m->vsync_start + target_vtotal_diff < m->vdisplay ||
8856 m->vsync_end + target_vtotal_diff < m->vsync_start ||
8857 m->vtotal + target_vtotal_diff < m->vsync_end)
8858 continue;
8859
8860 new_mode = drm_mode_duplicate(aconnector->base.dev, m);
8861 if (!new_mode)
8862 goto out;
8863
8864 new_mode->vtotal += (u16)target_vtotal_diff;
8865 new_mode->vsync_start += (u16)target_vtotal_diff;
8866 new_mode->vsync_end += (u16)target_vtotal_diff;
8867 new_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
8868 new_mode->type |= DRM_MODE_TYPE_DRIVER;
8869
8870 if (!is_duplicate_mode(aconnector, new_mode)) {
8871 drm_mode_probed_add(&aconnector->base, new_mode);
8872 new_modes_count += 1;
8873 } else
8874 drm_mode_destroy(aconnector->base.dev, new_mode);
8875 }
8876 out:
8877 return new_modes_count;
8878 }
8879
amdgpu_dm_connector_add_freesync_modes(struct drm_connector * connector,const struct drm_edid * drm_edid)8880 static void amdgpu_dm_connector_add_freesync_modes(struct drm_connector *connector,
8881 const struct drm_edid *drm_edid)
8882 {
8883 struct amdgpu_dm_connector *amdgpu_dm_connector =
8884 to_amdgpu_dm_connector(connector);
8885
8886 if (!(amdgpu_freesync_vid_mode && drm_edid))
8887 return;
8888
8889 if (!amdgpu_dm_connector->dc_sink || !amdgpu_dm_connector->dc_link)
8890 return;
8891
8892 if (!dc_supports_vrr(amdgpu_dm_connector->dc_sink->ctx->dce_version))
8893 return;
8894
8895 if (dc_connector_supports_analog(amdgpu_dm_connector->dc_link->link_id.id) &&
8896 amdgpu_dm_connector->dc_sink->edid_caps.analog)
8897 return;
8898
8899 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
8900 amdgpu_dm_connector->num_modes +=
8901 add_fs_modes(amdgpu_dm_connector);
8902 }
8903
amdgpu_dm_connector_get_modes(struct drm_connector * connector)8904 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
8905 {
8906 struct amdgpu_dm_connector *amdgpu_dm_connector =
8907 to_amdgpu_dm_connector(connector);
8908 struct dc_link *dc_link = amdgpu_dm_connector->dc_link;
8909 struct drm_encoder *encoder;
8910 const struct drm_edid *drm_edid = amdgpu_dm_connector->drm_edid;
8911 struct dc_link_settings *verified_link_cap = &dc_link->verified_link_cap;
8912 const struct dc *dc = dc_link->dc;
8913
8914 encoder = amdgpu_dm_connector_to_encoder(connector);
8915
8916 if (!drm_edid) {
8917 amdgpu_dm_connector->num_modes =
8918 drm_add_modes_noedid(connector, 640, 480);
8919 if (dc->link_srv->dp_get_encoding_format(verified_link_cap) == DP_128b_132b_ENCODING)
8920 amdgpu_dm_connector->num_modes +=
8921 drm_add_modes_noedid(connector, 1920, 1080);
8922
8923 if (amdgpu_dm_connector->dc_sink &&
8924 amdgpu_dm_connector->dc_sink->edid_caps.analog &&
8925 dc_connector_supports_analog(dc_link->link_id.id)) {
8926 /* Analog monitor connected by DAC load detection.
8927 * Add common modes. It will be up to the user to select one that works.
8928 */
8929 for (int i = 0; i < ARRAY_SIZE(common_modes); i++)
8930 amdgpu_dm_connector->num_modes += drm_add_modes_noedid(
8931 connector, common_modes[i].w, common_modes[i].h);
8932 }
8933 } else {
8934 amdgpu_dm_connector_ddc_get_modes(connector, drm_edid);
8935 if (encoder)
8936 amdgpu_dm_connector_add_common_modes(encoder, connector);
8937 amdgpu_dm_connector_add_freesync_modes(connector, drm_edid);
8938 }
8939 amdgpu_dm_fbc_init(connector);
8940
8941 return amdgpu_dm_connector->num_modes;
8942 }
8943
8944 static const u32 supported_colorspaces =
8945 BIT(DRM_MODE_COLORIMETRY_BT709_YCC) |
8946 BIT(DRM_MODE_COLORIMETRY_OPRGB) |
8947 BIT(DRM_MODE_COLORIMETRY_BT2020_RGB) |
8948 BIT(DRM_MODE_COLORIMETRY_BT2020_YCC);
8949
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)8950 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
8951 struct amdgpu_dm_connector *aconnector,
8952 int connector_type,
8953 struct dc_link *link,
8954 int link_index)
8955 {
8956 struct amdgpu_device *adev = drm_to_adev(dm->ddev);
8957
8958 /*
8959 * Some of the properties below require access to state, like bpc.
8960 * Allocate some default initial connector state with our reset helper.
8961 */
8962 if (aconnector->base.funcs->reset)
8963 aconnector->base.funcs->reset(&aconnector->base);
8964
8965 aconnector->connector_id = link_index;
8966 aconnector->bl_idx = -1;
8967 aconnector->dc_link = link;
8968 aconnector->base.interlace_allowed = false;
8969 aconnector->base.doublescan_allowed = false;
8970 aconnector->base.stereo_allowed = false;
8971 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
8972 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
8973 aconnector->audio_inst = -1;
8974 aconnector->pack_sdp_v1_3 = false;
8975 aconnector->as_type = ADAPTIVE_SYNC_TYPE_NONE;
8976 memset(&aconnector->vsdb_info, 0, sizeof(aconnector->vsdb_info));
8977 mutex_init(&aconnector->hpd_lock);
8978 mutex_init(&aconnector->handle_mst_msg_ready);
8979
8980 /*
8981 * If HDMI HPD debounce delay is set, use the minimum between selected
8982 * value and AMDGPU_DM_MAX_HDMI_HPD_DEBOUNCE_MS
8983 */
8984 if (amdgpu_hdmi_hpd_debounce_delay_ms) {
8985 aconnector->hdmi_hpd_debounce_delay_ms = min(amdgpu_hdmi_hpd_debounce_delay_ms,
8986 AMDGPU_DM_MAX_HDMI_HPD_DEBOUNCE_MS);
8987 INIT_DELAYED_WORK(&aconnector->hdmi_hpd_debounce_work, hdmi_hpd_debounce_work);
8988 aconnector->hdmi_prev_sink = NULL;
8989 } else {
8990 aconnector->hdmi_hpd_debounce_delay_ms = 0;
8991 }
8992
8993 /*
8994 * configure support HPD hot plug connector_>polled default value is 0
8995 * which means HPD hot plug not supported
8996 */
8997 switch (connector_type) {
8998 case DRM_MODE_CONNECTOR_HDMIA:
8999 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
9000 aconnector->base.ycbcr_420_allowed =
9001 link->link_enc->features.hdmi_ycbcr420_supported ? true : false;
9002 break;
9003 case DRM_MODE_CONNECTOR_DisplayPort:
9004 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
9005 link->link_enc = link_enc_cfg_get_link_enc(link);
9006 ASSERT(link->link_enc);
9007 if (link->link_enc)
9008 aconnector->base.ycbcr_420_allowed =
9009 link->link_enc->features.dp_ycbcr420_supported ? true : false;
9010 break;
9011 case DRM_MODE_CONNECTOR_DVID:
9012 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
9013 break;
9014 case DRM_MODE_CONNECTOR_DVII:
9015 case DRM_MODE_CONNECTOR_VGA:
9016 aconnector->base.polled =
9017 DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
9018 break;
9019 default:
9020 break;
9021 }
9022
9023 drm_object_attach_property(&aconnector->base.base,
9024 dm->ddev->mode_config.scaling_mode_property,
9025 DRM_MODE_SCALE_NONE);
9026
9027 if (connector_type == DRM_MODE_CONNECTOR_HDMIA
9028 || (connector_type == DRM_MODE_CONNECTOR_DisplayPort && !aconnector->mst_root))
9029 drm_connector_attach_broadcast_rgb_property(&aconnector->base);
9030
9031 drm_object_attach_property(&aconnector->base.base,
9032 adev->mode_info.underscan_property,
9033 UNDERSCAN_OFF);
9034 drm_object_attach_property(&aconnector->base.base,
9035 adev->mode_info.underscan_hborder_property,
9036 0);
9037 drm_object_attach_property(&aconnector->base.base,
9038 adev->mode_info.underscan_vborder_property,
9039 0);
9040
9041 if (!aconnector->mst_root)
9042 drm_connector_attach_max_bpc_property(&aconnector->base, 8, 16);
9043
9044 aconnector->base.state->max_bpc = 16;
9045 aconnector->base.state->max_requested_bpc = aconnector->base.state->max_bpc;
9046
9047 if (connector_type == DRM_MODE_CONNECTOR_HDMIA) {
9048 /* Content Type is currently only implemented for HDMI. */
9049 drm_connector_attach_content_type_property(&aconnector->base);
9050 }
9051
9052 if (connector_type == DRM_MODE_CONNECTOR_HDMIA) {
9053 if (!drm_mode_create_hdmi_colorspace_property(&aconnector->base, supported_colorspaces))
9054 drm_connector_attach_colorspace_property(&aconnector->base);
9055 } else if ((connector_type == DRM_MODE_CONNECTOR_DisplayPort && !aconnector->mst_root) ||
9056 connector_type == DRM_MODE_CONNECTOR_eDP) {
9057 if (!drm_mode_create_dp_colorspace_property(&aconnector->base, supported_colorspaces))
9058 drm_connector_attach_colorspace_property(&aconnector->base);
9059 }
9060
9061 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
9062 connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
9063 connector_type == DRM_MODE_CONNECTOR_eDP) {
9064 drm_connector_attach_hdr_output_metadata_property(&aconnector->base);
9065
9066 if (!aconnector->mst_root)
9067 drm_connector_attach_vrr_capable_property(&aconnector->base);
9068
9069 if (adev->dm.hdcp_workqueue)
9070 drm_connector_attach_content_protection_property(&aconnector->base, true);
9071 }
9072
9073 if (connector_type == DRM_MODE_CONNECTOR_eDP) {
9074 struct drm_privacy_screen *privacy_screen;
9075
9076 privacy_screen = drm_privacy_screen_get(adev_to_drm(adev)->dev, NULL);
9077 if (!IS_ERR(privacy_screen)) {
9078 drm_connector_attach_privacy_screen_provider(&aconnector->base,
9079 privacy_screen);
9080 } else if (PTR_ERR(privacy_screen) != -ENODEV) {
9081 drm_warn(adev_to_drm(adev), "Error getting privacy-screen\n");
9082 }
9083 }
9084 }
9085
amdgpu_dm_i2c_xfer(struct i2c_adapter * i2c_adap,struct i2c_msg * msgs,int num)9086 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
9087 struct i2c_msg *msgs, int num)
9088 {
9089 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
9090 struct ddc_service *ddc_service = i2c->ddc_service;
9091 struct i2c_command cmd;
9092 int i;
9093 int result = -EIO;
9094
9095 if (!ddc_service->ddc_pin)
9096 return result;
9097
9098 cmd.payloads = kzalloc_objs(struct i2c_payload, num);
9099
9100 if (!cmd.payloads)
9101 return result;
9102
9103 cmd.number_of_payloads = num;
9104 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
9105 cmd.speed = 100;
9106
9107 for (i = 0; i < num; i++) {
9108 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
9109 cmd.payloads[i].address = msgs[i].addr;
9110 cmd.payloads[i].length = msgs[i].len;
9111 cmd.payloads[i].data = msgs[i].buf;
9112 }
9113
9114 if (i2c->oem) {
9115 if (dc_submit_i2c_oem(
9116 ddc_service->ctx->dc,
9117 &cmd))
9118 result = num;
9119 } else {
9120 if (dc_submit_i2c(
9121 ddc_service->ctx->dc,
9122 ddc_service->link->link_index,
9123 &cmd))
9124 result = num;
9125 }
9126
9127 kfree(cmd.payloads);
9128 return result;
9129 }
9130
amdgpu_dm_i2c_func(struct i2c_adapter * adap)9131 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
9132 {
9133 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
9134 }
9135
9136 static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
9137 .master_xfer = amdgpu_dm_i2c_xfer,
9138 .functionality = amdgpu_dm_i2c_func,
9139 };
9140
9141 static struct amdgpu_i2c_adapter *
create_i2c(struct ddc_service * ddc_service,bool oem)9142 create_i2c(struct ddc_service *ddc_service, bool oem)
9143 {
9144 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
9145 struct amdgpu_i2c_adapter *i2c;
9146
9147 i2c = kzalloc_obj(struct amdgpu_i2c_adapter);
9148 if (!i2c)
9149 return NULL;
9150 i2c->base.owner = THIS_MODULE;
9151 i2c->base.dev.parent = &adev->pdev->dev;
9152 i2c->base.algo = &amdgpu_dm_i2c_algo;
9153 if (oem)
9154 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c OEM bus");
9155 else
9156 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d",
9157 ddc_service->link->link_index);
9158 i2c_set_adapdata(&i2c->base, i2c);
9159 i2c->ddc_service = ddc_service;
9160 i2c->oem = oem;
9161
9162 return i2c;
9163 }
9164
amdgpu_dm_initialize_hdmi_connector(struct amdgpu_dm_connector * aconnector)9165 int amdgpu_dm_initialize_hdmi_connector(struct amdgpu_dm_connector *aconnector)
9166 {
9167 struct cec_connector_info conn_info;
9168 struct drm_device *ddev = aconnector->base.dev;
9169 struct device *hdmi_dev = ddev->dev;
9170
9171 if (amdgpu_dc_debug_mask & DC_DISABLE_HDMI_CEC) {
9172 drm_info(ddev, "HDMI-CEC feature masked\n");
9173 return -EINVAL;
9174 }
9175
9176 cec_fill_conn_info_from_drm(&conn_info, &aconnector->base);
9177 aconnector->notifier =
9178 cec_notifier_conn_register(hdmi_dev, NULL, &conn_info);
9179 if (!aconnector->notifier) {
9180 drm_err(ddev, "Failed to create cec notifier\n");
9181 return -ENOMEM;
9182 }
9183
9184 return 0;
9185 }
9186
9187 /*
9188 * Note: this function assumes that dc_link_detect() was called for the
9189 * dc_link which will be represented by this aconnector.
9190 */
amdgpu_dm_connector_init(struct amdgpu_display_manager * dm,struct amdgpu_dm_connector * aconnector,u32 link_index,struct amdgpu_encoder * aencoder)9191 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
9192 struct amdgpu_dm_connector *aconnector,
9193 u32 link_index,
9194 struct amdgpu_encoder *aencoder)
9195 {
9196 int res = 0;
9197 int connector_type;
9198 struct dc *dc = dm->dc;
9199 struct dc_link *link = dc_get_link_at_index(dc, link_index);
9200 struct amdgpu_i2c_adapter *i2c;
9201
9202 /* Not needed for writeback connector */
9203 link->priv = aconnector;
9204
9205
9206 i2c = create_i2c(link->ddc, false);
9207 if (!i2c) {
9208 drm_err(adev_to_drm(dm->adev), "Failed to create i2c adapter data\n");
9209 return -ENOMEM;
9210 }
9211
9212 aconnector->i2c = i2c;
9213 res = devm_i2c_add_adapter(dm->adev->dev, &i2c->base);
9214
9215 if (res) {
9216 drm_err(adev_to_drm(dm->adev), "Failed to register hw i2c %d\n", link->link_index);
9217 goto out_free;
9218 }
9219
9220 connector_type = to_drm_connector_type(link->connector_signal, link->link_id.id);
9221
9222 res = drm_connector_init_with_ddc(
9223 dm->ddev,
9224 &aconnector->base,
9225 &amdgpu_dm_connector_funcs,
9226 connector_type,
9227 &i2c->base);
9228
9229 if (res) {
9230 drm_err(adev_to_drm(dm->adev), "connector_init failed\n");
9231 aconnector->connector_id = -1;
9232 goto out_free;
9233 }
9234
9235 drm_connector_helper_add(
9236 &aconnector->base,
9237 &amdgpu_dm_connector_helper_funcs);
9238
9239 amdgpu_dm_connector_init_helper(
9240 dm,
9241 aconnector,
9242 connector_type,
9243 link,
9244 link_index);
9245
9246 drm_connector_attach_encoder(
9247 &aconnector->base, &aencoder->base);
9248
9249 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
9250 connector_type == DRM_MODE_CONNECTOR_HDMIB)
9251 amdgpu_dm_initialize_hdmi_connector(aconnector);
9252
9253 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
9254 || connector_type == DRM_MODE_CONNECTOR_eDP)
9255 amdgpu_dm_initialize_dp_connector(dm, aconnector, link->link_index);
9256
9257 out_free:
9258 if (res) {
9259 kfree(i2c);
9260 aconnector->i2c = NULL;
9261 }
9262 return res;
9263 }
9264
amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device * adev)9265 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
9266 {
9267 switch (adev->mode_info.num_crtc) {
9268 case 1:
9269 return 0x1;
9270 case 2:
9271 return 0x3;
9272 case 3:
9273 return 0x7;
9274 case 4:
9275 return 0xf;
9276 case 5:
9277 return 0x1f;
9278 case 6:
9279 default:
9280 return 0x3f;
9281 }
9282 }
9283
amdgpu_dm_encoder_init(struct drm_device * dev,struct amdgpu_encoder * aencoder,uint32_t link_index)9284 static int amdgpu_dm_encoder_init(struct drm_device *dev,
9285 struct amdgpu_encoder *aencoder,
9286 uint32_t link_index)
9287 {
9288 struct amdgpu_device *adev = drm_to_adev(dev);
9289
9290 int res = drm_encoder_init(dev,
9291 &aencoder->base,
9292 &amdgpu_dm_encoder_funcs,
9293 DRM_MODE_ENCODER_TMDS,
9294 NULL);
9295
9296 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
9297
9298 if (!res)
9299 aencoder->encoder_id = link_index;
9300 else
9301 aencoder->encoder_id = -1;
9302
9303 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
9304
9305 return res;
9306 }
9307
manage_dm_interrupts(struct amdgpu_device * adev,struct amdgpu_crtc * acrtc,struct dm_crtc_state * acrtc_state)9308 static void manage_dm_interrupts(struct amdgpu_device *adev,
9309 struct amdgpu_crtc *acrtc,
9310 struct dm_crtc_state *acrtc_state)
9311 { /*
9312 * We cannot be sure that the frontend index maps to the same
9313 * backend index - some even map to more than one.
9314 * So we have to go through the CRTC to find the right IRQ.
9315 */
9316 int irq_type = amdgpu_display_crtc_idx_to_irq_type(
9317 adev,
9318 acrtc->crtc_id);
9319 struct drm_device *dev = adev_to_drm(adev);
9320
9321 struct drm_vblank_crtc_config config = {0};
9322 struct dc_crtc_timing *timing;
9323 int offdelay;
9324
9325 if (acrtc_state) {
9326 timing = &acrtc_state->stream->timing;
9327
9328 /*
9329 * Depending on when the HW latching event of double-buffered
9330 * registers happen relative to the PSR SDP deadline, and how
9331 * bad the Panel clock has drifted since the last ALPM off
9332 * event, there can be up to 3 frames of delay between sending
9333 * the PSR exit cmd to DMUB fw, and when the panel starts
9334 * displaying live frames.
9335 *
9336 * We can set:
9337 *
9338 * 20/100 * offdelay_ms = 3_frames_ms
9339 * => offdelay_ms = 5 * 3_frames_ms
9340 *
9341 * This ensures that `3_frames_ms` will only be experienced as a
9342 * 20% delay on top how long the display has been static, and
9343 * thus make the delay less perceivable.
9344 */
9345 if (acrtc_state->stream->link->psr_settings.psr_version <
9346 DC_PSR_VERSION_UNSUPPORTED) {
9347 offdelay = DIV64_U64_ROUND_UP((u64)5 * 3 * 10 *
9348 timing->v_total *
9349 timing->h_total,
9350 timing->pix_clk_100hz);
9351 config.offdelay_ms = offdelay ?: 30;
9352 } else if (amdgpu_ip_version(adev, DCE_HWIP, 0) <
9353 IP_VERSION(3, 5, 0) ||
9354 !(adev->flags & AMD_IS_APU)) {
9355 /*
9356 * Older HW and DGPU have issues with instant off;
9357 * use a 2 frame offdelay.
9358 */
9359 offdelay = DIV64_U64_ROUND_UP((u64)20 *
9360 timing->v_total *
9361 timing->h_total,
9362 timing->pix_clk_100hz);
9363
9364 config.offdelay_ms = offdelay ?: 30;
9365 } else {
9366 /* offdelay_ms = 0 will never disable vblank */
9367 config.offdelay_ms = 1;
9368 config.disable_immediate = true;
9369 }
9370
9371 drm_crtc_vblank_on_config(&acrtc->base,
9372 &config);
9373 /* Allow RX6xxx, RX7700, RX7800 GPUs to call amdgpu_irq_get.*/
9374 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
9375 case IP_VERSION(3, 0, 0):
9376 case IP_VERSION(3, 0, 2):
9377 case IP_VERSION(3, 0, 3):
9378 case IP_VERSION(3, 2, 0):
9379 if (amdgpu_irq_get(adev, &adev->pageflip_irq, irq_type))
9380 drm_err(dev, "DM_IRQ: Cannot get pageflip irq!\n");
9381 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
9382 if (amdgpu_irq_get(adev, &adev->vline0_irq, irq_type))
9383 drm_err(dev, "DM_IRQ: Cannot get vline0 irq!\n");
9384 #endif
9385 }
9386
9387 } else {
9388 /* Allow RX6xxx, RX7700, RX7800 GPUs to call amdgpu_irq_put.*/
9389 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
9390 case IP_VERSION(3, 0, 0):
9391 case IP_VERSION(3, 0, 2):
9392 case IP_VERSION(3, 0, 3):
9393 case IP_VERSION(3, 2, 0):
9394 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
9395 if (amdgpu_irq_put(adev, &adev->vline0_irq, irq_type))
9396 drm_err(dev, "DM_IRQ: Cannot put vline0 irq!\n");
9397 #endif
9398 if (amdgpu_irq_put(adev, &adev->pageflip_irq, irq_type))
9399 drm_err(dev, "DM_IRQ: Cannot put pageflip irq!\n");
9400 }
9401
9402 drm_crtc_vblank_off(&acrtc->base);
9403 }
9404 }
9405
dm_update_pflip_irq_state(struct amdgpu_device * adev,struct amdgpu_crtc * acrtc)9406 static void dm_update_pflip_irq_state(struct amdgpu_device *adev,
9407 struct amdgpu_crtc *acrtc)
9408 {
9409 int irq_type =
9410 amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
9411
9412 /**
9413 * This reads the current state for the IRQ and force reapplies
9414 * the setting to hardware.
9415 */
9416 amdgpu_irq_update(adev, &adev->pageflip_irq, irq_type);
9417 }
9418
9419 static bool
is_scaling_state_different(const struct dm_connector_state * dm_state,const struct dm_connector_state * old_dm_state)9420 is_scaling_state_different(const struct dm_connector_state *dm_state,
9421 const struct dm_connector_state *old_dm_state)
9422 {
9423 if (dm_state->scaling != old_dm_state->scaling)
9424 return true;
9425 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
9426 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
9427 return true;
9428 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
9429 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
9430 return true;
9431 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
9432 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
9433 return true;
9434 return false;
9435 }
9436
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)9437 static bool is_content_protection_different(struct drm_crtc_state *new_crtc_state,
9438 struct drm_crtc_state *old_crtc_state,
9439 struct drm_connector_state *new_conn_state,
9440 struct drm_connector_state *old_conn_state,
9441 const struct drm_connector *connector,
9442 struct hdcp_workqueue *hdcp_w)
9443 {
9444 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
9445 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
9446
9447 pr_debug("[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n",
9448 connector->index, connector->status, connector->dpms);
9449 pr_debug("[HDCP_DM] state protection old: %x new: %x\n",
9450 old_conn_state->content_protection, new_conn_state->content_protection);
9451
9452 if (old_crtc_state)
9453 pr_debug("[HDCP_DM] old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
9454 old_crtc_state->enable,
9455 old_crtc_state->active,
9456 old_crtc_state->mode_changed,
9457 old_crtc_state->active_changed,
9458 old_crtc_state->connectors_changed);
9459
9460 if (new_crtc_state)
9461 pr_debug("[HDCP_DM] NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
9462 new_crtc_state->enable,
9463 new_crtc_state->active,
9464 new_crtc_state->mode_changed,
9465 new_crtc_state->active_changed,
9466 new_crtc_state->connectors_changed);
9467
9468 /* hdcp content type change */
9469 if (old_conn_state->hdcp_content_type != new_conn_state->hdcp_content_type &&
9470 new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
9471 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
9472 pr_debug("[HDCP_DM] Type0/1 change %s :true\n", __func__);
9473 return true;
9474 }
9475
9476 /* CP is being re enabled, ignore this */
9477 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED &&
9478 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
9479 if (new_crtc_state && new_crtc_state->mode_changed) {
9480 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
9481 pr_debug("[HDCP_DM] ENABLED->DESIRED & mode_changed %s :true\n", __func__);
9482 return true;
9483 }
9484 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED;
9485 pr_debug("[HDCP_DM] ENABLED -> DESIRED %s :false\n", __func__);
9486 return false;
9487 }
9488
9489 /* S3 resume case, since old state will always be 0 (UNDESIRED) and the restored state will be ENABLED
9490 *
9491 * Handles: UNDESIRED -> ENABLED
9492 */
9493 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED &&
9494 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
9495 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
9496
9497 /* Stream removed and re-enabled
9498 *
9499 * Can sometimes overlap with the HPD case,
9500 * thus set update_hdcp to false to avoid
9501 * setting HDCP multiple times.
9502 *
9503 * Handles: DESIRED -> DESIRED (Special case)
9504 */
9505 if (!(old_conn_state->crtc && old_conn_state->crtc->enabled) &&
9506 new_conn_state->crtc && new_conn_state->crtc->enabled &&
9507 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
9508 dm_con_state->update_hdcp = false;
9509 pr_debug("[HDCP_DM] DESIRED->DESIRED (Stream removed and re-enabled) %s :true\n",
9510 __func__);
9511 return true;
9512 }
9513
9514 /* Hot-plug, headless s3, dpms
9515 *
9516 * Only start HDCP if the display is connected/enabled.
9517 * update_hdcp flag will be set to false until the next
9518 * HPD comes in.
9519 *
9520 * Handles: DESIRED -> DESIRED (Special case)
9521 */
9522 if (dm_con_state->update_hdcp &&
9523 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED &&
9524 connector->dpms == DRM_MODE_DPMS_ON && aconnector->dc_sink != NULL) {
9525 dm_con_state->update_hdcp = false;
9526 pr_debug("[HDCP_DM] DESIRED->DESIRED (Hot-plug, headless s3, dpms) %s :true\n",
9527 __func__);
9528 return true;
9529 }
9530
9531 if (old_conn_state->content_protection == new_conn_state->content_protection) {
9532 if (new_conn_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED) {
9533 if (new_crtc_state && new_crtc_state->mode_changed) {
9534 pr_debug("[HDCP_DM] DESIRED->DESIRED or ENABLE->ENABLE mode_change %s :true\n",
9535 __func__);
9536 return true;
9537 }
9538 pr_debug("[HDCP_DM] DESIRED->DESIRED & ENABLE->ENABLE %s :false\n",
9539 __func__);
9540 return false;
9541 }
9542
9543 pr_debug("[HDCP_DM] UNDESIRED->UNDESIRED %s :false\n", __func__);
9544 return false;
9545 }
9546
9547 if (new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_ENABLED) {
9548 pr_debug("[HDCP_DM] UNDESIRED->DESIRED or DESIRED->UNDESIRED or ENABLED->UNDESIRED %s :true\n",
9549 __func__);
9550 return true;
9551 }
9552
9553 pr_debug("[HDCP_DM] DESIRED->ENABLED %s :false\n", __func__);
9554 return false;
9555 }
9556
remove_stream(struct amdgpu_device * adev,struct amdgpu_crtc * acrtc,struct dc_stream_state * stream)9557 static void remove_stream(struct amdgpu_device *adev,
9558 struct amdgpu_crtc *acrtc,
9559 struct dc_stream_state *stream)
9560 {
9561 /* this is the update mode case */
9562
9563 acrtc->otg_inst = -1;
9564 acrtc->enabled = false;
9565 }
9566
prepare_flip_isr(struct amdgpu_crtc * acrtc)9567 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
9568 {
9569
9570 assert_spin_locked(&acrtc->base.dev->event_lock);
9571 WARN_ON(acrtc->event);
9572
9573 acrtc->event = acrtc->base.state->event;
9574
9575 /* Set the flip status */
9576 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
9577
9578 /* Mark this event as consumed */
9579 acrtc->base.state->event = NULL;
9580
9581 drm_dbg_state(acrtc->base.dev,
9582 "crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
9583 acrtc->crtc_id);
9584 }
9585
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)9586 static void update_freesync_state_on_stream(
9587 struct amdgpu_display_manager *dm,
9588 struct dm_crtc_state *new_crtc_state,
9589 struct dc_stream_state *new_stream,
9590 struct dc_plane_state *surface,
9591 u32 flip_timestamp_in_us)
9592 {
9593 struct mod_vrr_params vrr_params;
9594 struct dc_info_packet vrr_infopacket = {0};
9595 struct amdgpu_device *adev = dm->adev;
9596 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
9597 unsigned long flags;
9598 bool pack_sdp_v1_3 = false;
9599 struct amdgpu_dm_connector *aconn;
9600 enum vrr_packet_type packet_type = PACKET_TYPE_VRR;
9601
9602 if (!new_stream)
9603 return;
9604
9605 /*
9606 * TODO: Determine why min/max totals and vrefresh can be 0 here.
9607 * For now it's sufficient to just guard against these conditions.
9608 */
9609
9610 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
9611 return;
9612
9613 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9614 vrr_params = acrtc->dm_irq_params.vrr_params;
9615
9616 if (surface) {
9617 mod_freesync_handle_preflip(
9618 dm->freesync_module,
9619 surface,
9620 new_stream,
9621 flip_timestamp_in_us,
9622 &vrr_params);
9623
9624 if (adev->family < AMDGPU_FAMILY_AI &&
9625 amdgpu_dm_crtc_vrr_active(new_crtc_state)) {
9626 mod_freesync_handle_v_update(dm->freesync_module,
9627 new_stream, &vrr_params);
9628
9629 /* Need to call this before the frame ends. */
9630 dc_stream_adjust_vmin_vmax(dm->dc,
9631 new_crtc_state->stream,
9632 &vrr_params.adjust);
9633 }
9634 }
9635
9636 aconn = (struct amdgpu_dm_connector *)new_stream->dm_stream_context;
9637
9638 if (aconn && (aconn->as_type == FREESYNC_TYPE_PCON_IN_WHITELIST || aconn->vsdb_info.replay_mode)) {
9639 pack_sdp_v1_3 = aconn->pack_sdp_v1_3;
9640
9641 if (aconn->vsdb_info.amd_vsdb_version == 1)
9642 packet_type = PACKET_TYPE_FS_V1;
9643 else if (aconn->vsdb_info.amd_vsdb_version == 2)
9644 packet_type = PACKET_TYPE_FS_V2;
9645 else if (aconn->vsdb_info.amd_vsdb_version == 3)
9646 packet_type = PACKET_TYPE_FS_V3;
9647
9648 mod_build_adaptive_sync_infopacket(new_stream, aconn->as_type, NULL,
9649 &new_stream->adaptive_sync_infopacket);
9650 }
9651
9652 mod_freesync_build_vrr_infopacket(
9653 dm->freesync_module,
9654 new_stream,
9655 &vrr_params,
9656 packet_type,
9657 TRANSFER_FUNC_UNKNOWN,
9658 &vrr_infopacket,
9659 pack_sdp_v1_3);
9660
9661 new_crtc_state->freesync_vrr_info_changed |=
9662 (memcmp(&new_crtc_state->vrr_infopacket,
9663 &vrr_infopacket,
9664 sizeof(vrr_infopacket)) != 0);
9665
9666 acrtc->dm_irq_params.vrr_params = vrr_params;
9667 new_crtc_state->vrr_infopacket = vrr_infopacket;
9668
9669 new_stream->vrr_infopacket = vrr_infopacket;
9670 new_stream->allow_freesync = mod_freesync_get_freesync_enabled(&vrr_params);
9671
9672 if (new_crtc_state->freesync_vrr_info_changed)
9673 drm_dbg_kms(adev_to_drm(adev), "VRR packet update: crtc=%u enabled=%d state=%d",
9674 new_crtc_state->base.crtc->base.id,
9675 (int)new_crtc_state->base.vrr_enabled,
9676 (int)vrr_params.state);
9677
9678 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9679 }
9680
update_stream_irq_parameters(struct amdgpu_display_manager * dm,struct dm_crtc_state * new_crtc_state)9681 static void update_stream_irq_parameters(
9682 struct amdgpu_display_manager *dm,
9683 struct dm_crtc_state *new_crtc_state)
9684 {
9685 struct dc_stream_state *new_stream = new_crtc_state->stream;
9686 struct mod_vrr_params vrr_params;
9687 struct mod_freesync_config config = new_crtc_state->freesync_config;
9688 struct amdgpu_device *adev = dm->adev;
9689 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
9690 unsigned long flags;
9691
9692 if (!new_stream)
9693 return;
9694
9695 /*
9696 * TODO: Determine why min/max totals and vrefresh can be 0 here.
9697 * For now it's sufficient to just guard against these conditions.
9698 */
9699 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
9700 return;
9701
9702 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9703 vrr_params = acrtc->dm_irq_params.vrr_params;
9704
9705 if (new_crtc_state->vrr_supported &&
9706 config.min_refresh_in_uhz &&
9707 config.max_refresh_in_uhz) {
9708 /*
9709 * if freesync compatible mode was set, config.state will be set
9710 * in atomic check
9711 */
9712 if (config.state == VRR_STATE_ACTIVE_FIXED && config.fixed_refresh_in_uhz &&
9713 (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) ||
9714 new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)) {
9715 vrr_params.max_refresh_in_uhz = config.max_refresh_in_uhz;
9716 vrr_params.min_refresh_in_uhz = config.min_refresh_in_uhz;
9717 vrr_params.fixed_refresh_in_uhz = config.fixed_refresh_in_uhz;
9718 vrr_params.state = VRR_STATE_ACTIVE_FIXED;
9719 } else {
9720 config.state = new_crtc_state->base.vrr_enabled ?
9721 VRR_STATE_ACTIVE_VARIABLE :
9722 VRR_STATE_INACTIVE;
9723 }
9724 } else {
9725 config.state = VRR_STATE_UNSUPPORTED;
9726 }
9727
9728 mod_freesync_build_vrr_params(dm->freesync_module,
9729 new_stream,
9730 &config, &vrr_params);
9731
9732 new_crtc_state->freesync_config = config;
9733 /* Copy state for access from DM IRQ handler */
9734 acrtc->dm_irq_params.freesync_config = config;
9735 acrtc->dm_irq_params.active_planes = new_crtc_state->active_planes;
9736 acrtc->dm_irq_params.vrr_params = vrr_params;
9737 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9738 }
9739
amdgpu_dm_handle_vrr_transition(struct dm_crtc_state * old_state,struct dm_crtc_state * new_state)9740 static void amdgpu_dm_handle_vrr_transition(struct dm_crtc_state *old_state,
9741 struct dm_crtc_state *new_state)
9742 {
9743 bool old_vrr_active = amdgpu_dm_crtc_vrr_active(old_state);
9744 bool new_vrr_active = amdgpu_dm_crtc_vrr_active(new_state);
9745
9746 if (!old_vrr_active && new_vrr_active) {
9747 /* Transition VRR inactive -> active:
9748 * While VRR is active, we must not disable vblank irq, as a
9749 * reenable after disable would compute bogus vblank/pflip
9750 * timestamps if it likely happened inside display front-porch.
9751 *
9752 * We also need vupdate irq for the actual core vblank handling
9753 * at end of vblank.
9754 */
9755 WARN_ON(amdgpu_dm_crtc_set_vupdate_irq(new_state->base.crtc, true) != 0);
9756 WARN_ON(drm_crtc_vblank_get(new_state->base.crtc) != 0);
9757 drm_dbg_driver(new_state->base.crtc->dev, "%s: crtc=%u VRR off->on: Get vblank ref\n",
9758 __func__, new_state->base.crtc->base.id);
9759 } else if (old_vrr_active && !new_vrr_active) {
9760 /* Transition VRR active -> inactive:
9761 * Allow vblank irq disable again for fixed refresh rate.
9762 */
9763 WARN_ON(amdgpu_dm_crtc_set_vupdate_irq(new_state->base.crtc, false) != 0);
9764 drm_crtc_vblank_put(new_state->base.crtc);
9765 drm_dbg_driver(new_state->base.crtc->dev, "%s: crtc=%u VRR on->off: Drop vblank ref\n",
9766 __func__, new_state->base.crtc->base.id);
9767 }
9768 }
9769
amdgpu_dm_commit_cursors(struct drm_atomic_state * state)9770 static void amdgpu_dm_commit_cursors(struct drm_atomic_state *state)
9771 {
9772 struct drm_plane *plane;
9773 struct drm_plane_state *old_plane_state;
9774 int i;
9775
9776 /*
9777 * TODO: Make this per-stream so we don't issue redundant updates for
9778 * commits with multiple streams.
9779 */
9780 for_each_old_plane_in_state(state, plane, old_plane_state, i)
9781 if (plane->type == DRM_PLANE_TYPE_CURSOR)
9782 amdgpu_dm_plane_handle_cursor_update(plane, old_plane_state);
9783 }
9784
get_mem_type(struct drm_framebuffer * fb)9785 static inline uint32_t get_mem_type(struct drm_framebuffer *fb)
9786 {
9787 struct amdgpu_bo *abo = gem_to_amdgpu_bo(fb->obj[0]);
9788
9789 return abo->tbo.resource ? abo->tbo.resource->mem_type : 0;
9790 }
9791
amdgpu_dm_update_cursor(struct drm_plane * plane,struct drm_plane_state * old_plane_state,struct dc_stream_update * update)9792 static void amdgpu_dm_update_cursor(struct drm_plane *plane,
9793 struct drm_plane_state *old_plane_state,
9794 struct dc_stream_update *update)
9795 {
9796 struct amdgpu_device *adev = drm_to_adev(plane->dev);
9797 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
9798 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
9799 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
9800 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
9801 uint64_t address = afb ? afb->address : 0;
9802 struct dc_cursor_position position = {0};
9803 struct dc_cursor_attributes attributes;
9804 int ret;
9805
9806 if (!plane->state->fb && !old_plane_state->fb)
9807 return;
9808
9809 drm_dbg_atomic(plane->dev, "crtc_id=%d with size %d to %d\n",
9810 amdgpu_crtc->crtc_id, plane->state->crtc_w,
9811 plane->state->crtc_h);
9812
9813 ret = amdgpu_dm_plane_get_cursor_position(plane, crtc, &position);
9814 if (ret)
9815 return;
9816
9817 if (!position.enable) {
9818 /* turn off cursor */
9819 if (crtc_state && crtc_state->stream) {
9820 dc_stream_set_cursor_position(crtc_state->stream,
9821 &position);
9822 update->cursor_position = &crtc_state->stream->cursor_position;
9823 }
9824 return;
9825 }
9826
9827 amdgpu_crtc->cursor_width = plane->state->crtc_w;
9828 amdgpu_crtc->cursor_height = plane->state->crtc_h;
9829
9830 memset(&attributes, 0, sizeof(attributes));
9831 attributes.address.high_part = upper_32_bits(address);
9832 attributes.address.low_part = lower_32_bits(address);
9833 attributes.width = plane->state->crtc_w;
9834 attributes.height = plane->state->crtc_h;
9835 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
9836 attributes.rotation_angle = 0;
9837 attributes.attribute_flags.value = 0;
9838
9839 /* Enable cursor degamma ROM on DCN3+ for implicit sRGB degamma in DRM
9840 * legacy gamma setup.
9841 */
9842 if (crtc_state->cm_is_degamma_srgb &&
9843 adev->dm.dc->caps.color.dpp.gamma_corr)
9844 attributes.attribute_flags.bits.ENABLE_CURSOR_DEGAMMA = 1;
9845
9846 if (afb)
9847 attributes.pitch = afb->base.pitches[0] / afb->base.format->cpp[0];
9848
9849 if (crtc_state->stream) {
9850 if (!dc_stream_set_cursor_attributes(crtc_state->stream,
9851 &attributes))
9852 drm_err(adev_to_drm(adev), "DC failed to set cursor attributes\n");
9853
9854 update->cursor_attributes = &crtc_state->stream->cursor_attributes;
9855
9856 if (!dc_stream_set_cursor_position(crtc_state->stream,
9857 &position))
9858 drm_err(adev_to_drm(adev), "DC failed to set cursor position\n");
9859
9860 update->cursor_position = &crtc_state->stream->cursor_position;
9861 }
9862 }
9863
amdgpu_dm_enable_self_refresh(struct amdgpu_crtc * acrtc_attach,const struct dm_crtc_state * acrtc_state,const u64 current_ts)9864 static void amdgpu_dm_enable_self_refresh(struct amdgpu_crtc *acrtc_attach,
9865 const struct dm_crtc_state *acrtc_state,
9866 const u64 current_ts)
9867 {
9868 struct psr_settings *psr = &acrtc_state->stream->link->psr_settings;
9869 struct replay_settings *pr = &acrtc_state->stream->link->replay_settings;
9870 struct amdgpu_dm_connector *aconn =
9871 (struct amdgpu_dm_connector *)acrtc_state->stream->dm_stream_context;
9872 bool vrr_active = amdgpu_dm_crtc_vrr_active(acrtc_state);
9873
9874 if (acrtc_state->update_type > UPDATE_TYPE_FAST) {
9875 if (pr->config.replay_supported && !pr->replay_feature_enabled)
9876 amdgpu_dm_link_setup_replay(acrtc_state->stream->link, aconn);
9877 else if (psr->psr_version != DC_PSR_VERSION_UNSUPPORTED &&
9878 !psr->psr_feature_enabled)
9879 if (!aconn->disallow_edp_enter_psr)
9880 amdgpu_dm_link_setup_psr(acrtc_state->stream);
9881 }
9882
9883 /* Decrement skip count when SR is enabled and we're doing fast updates. */
9884 if (acrtc_state->update_type == UPDATE_TYPE_FAST &&
9885 (psr->psr_feature_enabled || pr->config.replay_supported)) {
9886 if (aconn->sr_skip_count > 0)
9887 aconn->sr_skip_count--;
9888
9889 /* Allow SR when skip count is 0. */
9890 acrtc_attach->dm_irq_params.allow_sr_entry = !aconn->sr_skip_count;
9891
9892 /*
9893 * If sink supports PSR SU/Panel Replay, there is no need to rely on
9894 * a vblank event disable request to enable PSR/RP. PSR SU/RP
9895 * can be enabled immediately once OS demonstrates an
9896 * adequate number of fast atomic commits to notify KMD
9897 * of update events. See `vblank_control_worker()`.
9898 */
9899 if (!vrr_active &&
9900 acrtc_attach->dm_irq_params.allow_sr_entry &&
9901 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
9902 !amdgpu_dm_crc_window_is_activated(acrtc_state->base.crtc) &&
9903 #endif
9904 (current_ts - psr->psr_dirty_rects_change_timestamp_ns) > 500000000) {
9905 if (pr->replay_feature_enabled && !pr->replay_allow_active)
9906 amdgpu_dm_replay_enable(acrtc_state->stream, true);
9907 if (psr->psr_version == DC_PSR_VERSION_SU_1 &&
9908 !psr->psr_allow_active && !aconn->disallow_edp_enter_psr)
9909 amdgpu_dm_psr_enable(acrtc_state->stream);
9910 }
9911 } else {
9912 acrtc_attach->dm_irq_params.allow_sr_entry = false;
9913 }
9914 }
9915
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)9916 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
9917 struct drm_device *dev,
9918 struct amdgpu_display_manager *dm,
9919 struct drm_crtc *pcrtc,
9920 bool wait_for_vblank)
9921 {
9922 u32 i;
9923 u64 timestamp_ns = ktime_get_ns();
9924 struct drm_plane *plane;
9925 struct drm_plane_state *old_plane_state, *new_plane_state;
9926 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
9927 struct drm_crtc_state *new_pcrtc_state =
9928 drm_atomic_get_new_crtc_state(state, pcrtc);
9929 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
9930 struct dm_crtc_state *dm_old_crtc_state =
9931 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
9932 int planes_count = 0, vpos, hpos;
9933 unsigned long flags;
9934 u32 target_vblank, last_flip_vblank;
9935 bool vrr_active = amdgpu_dm_crtc_vrr_active(acrtc_state);
9936 bool cursor_update = false;
9937 bool pflip_present = false;
9938 bool dirty_rects_changed = false;
9939 bool updated_planes_and_streams = false;
9940 struct {
9941 struct dc_surface_update surface_updates[MAX_SURFACES];
9942 struct dc_plane_info plane_infos[MAX_SURFACES];
9943 struct dc_scaling_info scaling_infos[MAX_SURFACES];
9944 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
9945 struct dc_stream_update stream_update;
9946 } *bundle;
9947
9948 bundle = kzalloc_obj(*bundle);
9949
9950 if (!bundle) {
9951 drm_err(dev, "Failed to allocate update bundle\n");
9952 goto cleanup;
9953 }
9954
9955 /*
9956 * Disable the cursor first if we're disabling all the planes.
9957 * It'll remain on the screen after the planes are re-enabled
9958 * if we don't.
9959 *
9960 * If the cursor is transitioning from native to overlay mode, the
9961 * native cursor needs to be disabled first.
9962 */
9963 if (acrtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE &&
9964 dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE) {
9965 struct dc_cursor_position cursor_position = {0};
9966
9967 if (!dc_stream_set_cursor_position(acrtc_state->stream,
9968 &cursor_position))
9969 drm_err(dev, "DC failed to disable native cursor\n");
9970
9971 bundle->stream_update.cursor_position =
9972 &acrtc_state->stream->cursor_position;
9973 }
9974
9975 if (acrtc_state->active_planes == 0 &&
9976 dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE)
9977 amdgpu_dm_commit_cursors(state);
9978
9979 /* update planes when needed */
9980 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
9981 struct drm_crtc *crtc = new_plane_state->crtc;
9982 struct drm_crtc_state *new_crtc_state;
9983 struct drm_framebuffer *fb = new_plane_state->fb;
9984 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)fb;
9985 bool plane_needs_flip;
9986 struct dc_plane_state *dc_plane;
9987 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
9988
9989 /* Cursor plane is handled after stream updates */
9990 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
9991 acrtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE) {
9992 if ((fb && crtc == pcrtc) ||
9993 (old_plane_state->fb && old_plane_state->crtc == pcrtc)) {
9994 cursor_update = true;
9995 if (amdgpu_ip_version(dm->adev, DCE_HWIP, 0) != 0)
9996 amdgpu_dm_update_cursor(plane, old_plane_state, &bundle->stream_update);
9997 }
9998
9999 continue;
10000 }
10001
10002 if (!fb || !crtc || pcrtc != crtc)
10003 continue;
10004
10005 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
10006 if (!new_crtc_state->active)
10007 continue;
10008
10009 dc_plane = dm_new_plane_state->dc_state;
10010 if (!dc_plane)
10011 continue;
10012
10013 bundle->surface_updates[planes_count].surface = dc_plane;
10014 if (new_pcrtc_state->color_mgmt_changed) {
10015 bundle->surface_updates[planes_count].gamma = &dc_plane->gamma_correction;
10016 bundle->surface_updates[planes_count].in_transfer_func = &dc_plane->in_transfer_func;
10017 bundle->surface_updates[planes_count].gamut_remap_matrix = &dc_plane->gamut_remap_matrix;
10018 bundle->surface_updates[planes_count].hdr_mult = dc_plane->hdr_mult;
10019 bundle->surface_updates[planes_count].func_shaper = &dc_plane->in_shaper_func;
10020 bundle->surface_updates[planes_count].lut3d_func = &dc_plane->lut3d_func;
10021 bundle->surface_updates[planes_count].blend_tf = &dc_plane->blend_tf;
10022 }
10023
10024 amdgpu_dm_plane_fill_dc_scaling_info(dm->adev, new_plane_state,
10025 &bundle->scaling_infos[planes_count]);
10026
10027 bundle->surface_updates[planes_count].scaling_info =
10028 &bundle->scaling_infos[planes_count];
10029
10030 plane_needs_flip = old_plane_state->fb && new_plane_state->fb;
10031
10032 pflip_present = pflip_present || plane_needs_flip;
10033
10034 if (!plane_needs_flip) {
10035 planes_count += 1;
10036 continue;
10037 }
10038
10039 fill_dc_plane_info_and_addr(
10040 dm->adev, new_plane_state,
10041 afb->tiling_flags,
10042 &bundle->plane_infos[planes_count],
10043 &bundle->flip_addrs[planes_count].address,
10044 afb->tmz_surface);
10045
10046 drm_dbg_state(state->dev, "plane: id=%d dcc_en=%d\n",
10047 new_plane_state->plane->index,
10048 bundle->plane_infos[planes_count].dcc.enable);
10049
10050 bundle->surface_updates[planes_count].plane_info =
10051 &bundle->plane_infos[planes_count];
10052
10053 if (acrtc_state->stream->link->psr_settings.psr_feature_enabled ||
10054 acrtc_state->stream->link->replay_settings.replay_feature_enabled) {
10055 fill_dc_dirty_rects(plane, old_plane_state,
10056 new_plane_state, new_crtc_state,
10057 &bundle->flip_addrs[planes_count],
10058 acrtc_state->stream->link->psr_settings.psr_version ==
10059 DC_PSR_VERSION_SU_1,
10060 &dirty_rects_changed);
10061
10062 /*
10063 * If the dirty regions changed, PSR-SU need to be disabled temporarily
10064 * and enabled it again after dirty regions are stable to avoid video glitch.
10065 * PSR-SU will be enabled in vblank_control_worker() if user pause the video
10066 * during the PSR-SU was disabled.
10067 */
10068 if (acrtc_state->stream->link->psr_settings.psr_version >= DC_PSR_VERSION_SU_1 &&
10069 acrtc_attach->dm_irq_params.allow_sr_entry &&
10070 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
10071 !amdgpu_dm_crc_window_is_activated(acrtc_state->base.crtc) &&
10072 #endif
10073 dirty_rects_changed) {
10074 mutex_lock(&dm->dc_lock);
10075 acrtc_state->stream->link->psr_settings.psr_dirty_rects_change_timestamp_ns =
10076 timestamp_ns;
10077 if (acrtc_state->stream->link->psr_settings.psr_allow_active)
10078 amdgpu_dm_psr_disable(acrtc_state->stream, true);
10079 mutex_unlock(&dm->dc_lock);
10080 }
10081 }
10082
10083 /*
10084 * Only allow immediate flips for fast updates that don't
10085 * change memory domain, FB pitch, DCC state, rotation or
10086 * mirroring.
10087 *
10088 * dm_crtc_helper_atomic_check() only accepts async flips with
10089 * fast updates.
10090 */
10091 if (crtc->state->async_flip &&
10092 (acrtc_state->update_type != UPDATE_TYPE_FAST ||
10093 get_mem_type(old_plane_state->fb) != get_mem_type(fb)))
10094 drm_warn_once(state->dev,
10095 "[PLANE:%d:%s] async flip with non-fast update\n",
10096 plane->base.id, plane->name);
10097
10098 bundle->flip_addrs[planes_count].flip_immediate =
10099 crtc->state->async_flip &&
10100 acrtc_state->update_type == UPDATE_TYPE_FAST &&
10101 get_mem_type(old_plane_state->fb) == get_mem_type(fb);
10102
10103 timestamp_ns = ktime_get_ns();
10104 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000);
10105 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count];
10106 bundle->surface_updates[planes_count].surface = dc_plane;
10107
10108 if (!bundle->surface_updates[planes_count].surface) {
10109 drm_err(dev, "No surface for CRTC: id=%d\n",
10110 acrtc_attach->crtc_id);
10111 continue;
10112 }
10113
10114 if (plane == pcrtc->primary)
10115 update_freesync_state_on_stream(
10116 dm,
10117 acrtc_state,
10118 acrtc_state->stream,
10119 dc_plane,
10120 bundle->flip_addrs[planes_count].flip_timestamp_in_us);
10121
10122 drm_dbg_state(state->dev, "%s Flipping to hi: 0x%x, low: 0x%x\n",
10123 __func__,
10124 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
10125 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
10126
10127 planes_count += 1;
10128
10129 }
10130
10131 if (pflip_present) {
10132 if (!vrr_active) {
10133 /* Use old throttling in non-vrr fixed refresh rate mode
10134 * to keep flip scheduling based on target vblank counts
10135 * working in a backwards compatible way, e.g., for
10136 * clients using the GLX_OML_sync_control extension or
10137 * DRI3/Present extension with defined target_msc.
10138 */
10139 last_flip_vblank = amdgpu_get_vblank_counter_kms(pcrtc);
10140 } else {
10141 /* For variable refresh rate mode only:
10142 * Get vblank of last completed flip to avoid > 1 vrr
10143 * flips per video frame by use of throttling, but allow
10144 * flip programming anywhere in the possibly large
10145 * variable vrr vblank interval for fine-grained flip
10146 * timing control and more opportunity to avoid stutter
10147 * on late submission of flips.
10148 */
10149 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10150 last_flip_vblank = acrtc_attach->dm_irq_params.last_flip_vblank;
10151 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10152 }
10153
10154 target_vblank = last_flip_vblank + wait_for_vblank;
10155
10156 /*
10157 * Wait until we're out of the vertical blank period before the one
10158 * targeted by the flip
10159 */
10160 while ((acrtc_attach->enabled &&
10161 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id,
10162 0, &vpos, &hpos, NULL,
10163 NULL, &pcrtc->hwmode)
10164 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
10165 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
10166 (int)(target_vblank -
10167 amdgpu_get_vblank_counter_kms(pcrtc)) > 0)) {
10168 usleep_range(1000, 1100);
10169 }
10170
10171 /**
10172 * Prepare the flip event for the pageflip interrupt to handle.
10173 *
10174 * This only works in the case where we've already turned on the
10175 * appropriate hardware blocks (eg. HUBP) so in the transition case
10176 * from 0 -> n planes we have to skip a hardware generated event
10177 * and rely on sending it from software.
10178 */
10179 if (acrtc_attach->base.state->event &&
10180 acrtc_state->active_planes > 0) {
10181 drm_crtc_vblank_get(pcrtc);
10182
10183 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10184
10185 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE);
10186 prepare_flip_isr(acrtc_attach);
10187
10188 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10189 }
10190
10191 if (acrtc_state->stream) {
10192 if (acrtc_state->freesync_vrr_info_changed)
10193 bundle->stream_update.vrr_infopacket =
10194 &acrtc_state->stream->vrr_infopacket;
10195 }
10196 } else if (cursor_update && acrtc_state->active_planes > 0) {
10197 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10198 if (acrtc_attach->base.state->event) {
10199 drm_crtc_vblank_get(pcrtc);
10200 acrtc_attach->event = acrtc_attach->base.state->event;
10201 acrtc_attach->base.state->event = NULL;
10202 }
10203 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10204 }
10205
10206 /* Update the planes if changed or disable if we don't have any. */
10207 if ((planes_count || acrtc_state->active_planes == 0) &&
10208 acrtc_state->stream) {
10209 /*
10210 * If PSR or idle optimizations are enabled then flush out
10211 * any pending work before hardware programming.
10212 */
10213 if (dm->vblank_control_workqueue)
10214 flush_workqueue(dm->vblank_control_workqueue);
10215
10216 bundle->stream_update.stream = acrtc_state->stream;
10217 if (new_pcrtc_state->mode_changed) {
10218 bundle->stream_update.src = acrtc_state->stream->src;
10219 bundle->stream_update.dst = acrtc_state->stream->dst;
10220 }
10221
10222 if (new_pcrtc_state->color_mgmt_changed) {
10223 /*
10224 * TODO: This isn't fully correct since we've actually
10225 * already modified the stream in place.
10226 */
10227 bundle->stream_update.gamut_remap =
10228 &acrtc_state->stream->gamut_remap_matrix;
10229 bundle->stream_update.output_csc_transform =
10230 &acrtc_state->stream->csc_color_matrix;
10231 bundle->stream_update.out_transfer_func =
10232 &acrtc_state->stream->out_transfer_func;
10233 bundle->stream_update.lut3d_func =
10234 (struct dc_3dlut *) acrtc_state->stream->lut3d_func;
10235 bundle->stream_update.func_shaper =
10236 (struct dc_transfer_func *) acrtc_state->stream->func_shaper;
10237 }
10238
10239 acrtc_state->stream->abm_level = acrtc_state->abm_level;
10240 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level)
10241 bundle->stream_update.abm_level = &acrtc_state->abm_level;
10242
10243 mutex_lock(&dm->dc_lock);
10244 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) || vrr_active) {
10245 if (acrtc_state->stream->link->replay_settings.replay_allow_active)
10246 amdgpu_dm_replay_disable(acrtc_state->stream);
10247 if (acrtc_state->stream->link->psr_settings.psr_allow_active)
10248 amdgpu_dm_psr_disable(acrtc_state->stream, true);
10249 }
10250 mutex_unlock(&dm->dc_lock);
10251
10252 /*
10253 * If FreeSync state on the stream has changed then we need to
10254 * re-adjust the min/max bounds now that DC doesn't handle this
10255 * as part of commit.
10256 */
10257 if (is_dc_timing_adjust_needed(dm_old_crtc_state, acrtc_state)) {
10258 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10259 dc_stream_adjust_vmin_vmax(
10260 dm->dc, acrtc_state->stream,
10261 &acrtc_attach->dm_irq_params.vrr_params.adjust);
10262 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10263 }
10264 mutex_lock(&dm->dc_lock);
10265 update_planes_and_stream_adapter(dm->dc,
10266 acrtc_state->update_type,
10267 planes_count,
10268 acrtc_state->stream,
10269 &bundle->stream_update,
10270 bundle->surface_updates);
10271 updated_planes_and_streams = true;
10272
10273 /**
10274 * Enable or disable the interrupts on the backend.
10275 *
10276 * Most pipes are put into power gating when unused.
10277 *
10278 * When power gating is enabled on a pipe we lose the
10279 * interrupt enablement state when power gating is disabled.
10280 *
10281 * So we need to update the IRQ control state in hardware
10282 * whenever the pipe turns on (since it could be previously
10283 * power gated) or off (since some pipes can't be power gated
10284 * on some ASICs).
10285 */
10286 if (dm_old_crtc_state->active_planes != acrtc_state->active_planes)
10287 dm_update_pflip_irq_state(drm_to_adev(dev),
10288 acrtc_attach);
10289
10290 amdgpu_dm_enable_self_refresh(acrtc_attach, acrtc_state, timestamp_ns);
10291 mutex_unlock(&dm->dc_lock);
10292 }
10293
10294 /*
10295 * Update cursor state *after* programming all the planes.
10296 * This avoids redundant programming in the case where we're going
10297 * to be disabling a single plane - those pipes are being disabled.
10298 */
10299 if (acrtc_state->active_planes &&
10300 (!updated_planes_and_streams || amdgpu_ip_version(dm->adev, DCE_HWIP, 0) == 0) &&
10301 acrtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE)
10302 amdgpu_dm_commit_cursors(state);
10303
10304 cleanup:
10305 kfree(bundle);
10306 }
10307
amdgpu_dm_commit_audio(struct drm_device * dev,struct drm_atomic_state * state)10308 static void amdgpu_dm_commit_audio(struct drm_device *dev,
10309 struct drm_atomic_state *state)
10310 {
10311 struct amdgpu_device *adev = drm_to_adev(dev);
10312 struct amdgpu_dm_connector *aconnector;
10313 struct drm_connector *connector;
10314 struct drm_connector_state *old_con_state, *new_con_state;
10315 struct drm_crtc_state *new_crtc_state;
10316 struct dm_crtc_state *new_dm_crtc_state;
10317 const struct dc_stream_status *status;
10318 int i, inst;
10319
10320 /* Notify device removals. */
10321 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10322 if (old_con_state->crtc != new_con_state->crtc) {
10323 /* CRTC changes require notification. */
10324 goto notify;
10325 }
10326
10327 if (!new_con_state->crtc)
10328 continue;
10329
10330 new_crtc_state = drm_atomic_get_new_crtc_state(
10331 state, new_con_state->crtc);
10332
10333 if (!new_crtc_state)
10334 continue;
10335
10336 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
10337 continue;
10338
10339 notify:
10340 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
10341 continue;
10342
10343 aconnector = to_amdgpu_dm_connector(connector);
10344
10345 mutex_lock(&adev->dm.audio_lock);
10346 inst = aconnector->audio_inst;
10347 aconnector->audio_inst = -1;
10348 mutex_unlock(&adev->dm.audio_lock);
10349
10350 amdgpu_dm_audio_eld_notify(adev, inst);
10351 }
10352
10353 /* Notify audio device additions. */
10354 for_each_new_connector_in_state(state, connector, new_con_state, i) {
10355 if (!new_con_state->crtc)
10356 continue;
10357
10358 new_crtc_state = drm_atomic_get_new_crtc_state(
10359 state, new_con_state->crtc);
10360
10361 if (!new_crtc_state)
10362 continue;
10363
10364 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
10365 continue;
10366
10367 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
10368 if (!new_dm_crtc_state->stream)
10369 continue;
10370
10371 status = dc_stream_get_status(new_dm_crtc_state->stream);
10372 if (!status)
10373 continue;
10374
10375 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
10376 continue;
10377
10378 aconnector = to_amdgpu_dm_connector(connector);
10379
10380 mutex_lock(&adev->dm.audio_lock);
10381 inst = status->audio_inst;
10382 aconnector->audio_inst = inst;
10383 mutex_unlock(&adev->dm.audio_lock);
10384
10385 amdgpu_dm_audio_eld_notify(adev, inst);
10386 }
10387 }
10388
10389 /*
10390 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
10391 * @crtc_state: the DRM CRTC state
10392 * @stream_state: the DC stream state.
10393 *
10394 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
10395 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
10396 */
amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state * crtc_state,struct dc_stream_state * stream_state)10397 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
10398 struct dc_stream_state *stream_state)
10399 {
10400 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
10401 }
10402
dm_clear_writeback(struct amdgpu_display_manager * dm,struct dm_crtc_state * crtc_state)10403 static void dm_clear_writeback(struct amdgpu_display_manager *dm,
10404 struct dm_crtc_state *crtc_state)
10405 {
10406 dc_stream_remove_writeback(dm->dc, crtc_state->stream, 0);
10407 }
10408
amdgpu_dm_commit_streams(struct drm_atomic_state * state,struct dc_state * dc_state)10409 static void amdgpu_dm_commit_streams(struct drm_atomic_state *state,
10410 struct dc_state *dc_state)
10411 {
10412 struct drm_device *dev = state->dev;
10413 struct amdgpu_device *adev = drm_to_adev(dev);
10414 struct amdgpu_display_manager *dm = &adev->dm;
10415 struct drm_crtc *crtc;
10416 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10417 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10418 struct drm_connector_state *old_con_state;
10419 struct drm_connector *connector;
10420 bool mode_set_reset_required = false;
10421 u32 i;
10422 struct dc_commit_streams_params params = {dc_state->streams, dc_state->stream_count};
10423 bool set_backlight_level = false;
10424
10425 /* Disable writeback */
10426 for_each_old_connector_in_state(state, connector, old_con_state, i) {
10427 struct dm_connector_state *dm_old_con_state;
10428 struct amdgpu_crtc *acrtc;
10429
10430 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
10431 continue;
10432
10433 old_crtc_state = NULL;
10434
10435 dm_old_con_state = to_dm_connector_state(old_con_state);
10436 if (!dm_old_con_state->base.crtc)
10437 continue;
10438
10439 acrtc = to_amdgpu_crtc(dm_old_con_state->base.crtc);
10440 if (acrtc)
10441 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
10442
10443 if (!acrtc || !acrtc->wb_enabled)
10444 continue;
10445
10446 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10447
10448 dm_clear_writeback(dm, dm_old_crtc_state);
10449 acrtc->wb_enabled = false;
10450 }
10451
10452 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
10453 new_crtc_state, i) {
10454 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
10455
10456 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10457
10458 if (old_crtc_state->active &&
10459 (!new_crtc_state->active ||
10460 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
10461 manage_dm_interrupts(adev, acrtc, NULL);
10462 dc_stream_release(dm_old_crtc_state->stream);
10463 }
10464 }
10465
10466 drm_atomic_helper_calc_timestamping_constants(state);
10467
10468 /* update changed items */
10469 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10470 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
10471
10472 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10473 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10474
10475 drm_dbg_state(state->dev,
10476 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n",
10477 acrtc->crtc_id,
10478 new_crtc_state->enable,
10479 new_crtc_state->active,
10480 new_crtc_state->planes_changed,
10481 new_crtc_state->mode_changed,
10482 new_crtc_state->active_changed,
10483 new_crtc_state->connectors_changed);
10484
10485 /* Disable cursor if disabling crtc */
10486 if (old_crtc_state->active && !new_crtc_state->active) {
10487 struct dc_cursor_position position;
10488
10489 memset(&position, 0, sizeof(position));
10490 mutex_lock(&dm->dc_lock);
10491 dc_exit_ips_for_hw_access(dm->dc);
10492 dc_stream_program_cursor_position(dm_old_crtc_state->stream, &position);
10493 mutex_unlock(&dm->dc_lock);
10494 }
10495
10496 /* Copy all transient state flags into dc state */
10497 if (dm_new_crtc_state->stream) {
10498 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
10499 dm_new_crtc_state->stream);
10500 }
10501
10502 /* handles headless hotplug case, updating new_state and
10503 * aconnector as needed
10504 */
10505
10506 if (amdgpu_dm_crtc_modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
10507
10508 drm_dbg_atomic(dev,
10509 "Atomic commit: SET crtc id %d: [%p]\n",
10510 acrtc->crtc_id, acrtc);
10511
10512 if (!dm_new_crtc_state->stream) {
10513 /*
10514 * this could happen because of issues with
10515 * userspace notifications delivery.
10516 * In this case userspace tries to set mode on
10517 * display which is disconnected in fact.
10518 * dc_sink is NULL in this case on aconnector.
10519 * We expect reset mode will come soon.
10520 *
10521 * This can also happen when unplug is done
10522 * during resume sequence ended
10523 *
10524 * In this case, we want to pretend we still
10525 * have a sink to keep the pipe running so that
10526 * hw state is consistent with the sw state
10527 */
10528 drm_dbg_atomic(dev,
10529 "Failed to create new stream for crtc %d\n",
10530 acrtc->base.base.id);
10531 continue;
10532 }
10533
10534 if (dm_old_crtc_state->stream)
10535 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
10536
10537 pm_runtime_get_noresume(dev->dev);
10538
10539 acrtc->enabled = true;
10540 acrtc->hw_mode = new_crtc_state->mode;
10541 crtc->hwmode = new_crtc_state->mode;
10542 mode_set_reset_required = true;
10543 set_backlight_level = true;
10544 } else if (modereset_required(new_crtc_state)) {
10545 drm_dbg_atomic(dev,
10546 "Atomic commit: RESET. crtc id %d:[%p]\n",
10547 acrtc->crtc_id, acrtc);
10548 /* i.e. reset mode */
10549 if (dm_old_crtc_state->stream)
10550 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
10551
10552 mode_set_reset_required = true;
10553 }
10554 } /* for_each_crtc_in_state() */
10555
10556 /* if there mode set or reset, disable eDP PSR, Replay */
10557 if (mode_set_reset_required) {
10558 if (dm->vblank_control_workqueue)
10559 flush_workqueue(dm->vblank_control_workqueue);
10560
10561 amdgpu_dm_replay_disable_all(dm);
10562 amdgpu_dm_psr_disable_all(dm);
10563 }
10564
10565 dm_enable_per_frame_crtc_master_sync(dc_state);
10566 mutex_lock(&dm->dc_lock);
10567 dc_exit_ips_for_hw_access(dm->dc);
10568 WARN_ON(!dc_commit_streams(dm->dc, ¶ms));
10569
10570 /* Allow idle optimization when vblank count is 0 for display off */
10571 if ((dm->active_vblank_irq_count == 0) && amdgpu_dm_is_headless(dm->adev))
10572 dc_allow_idle_optimizations(dm->dc, true);
10573 mutex_unlock(&dm->dc_lock);
10574
10575 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
10576 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
10577
10578 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10579
10580 if (dm_new_crtc_state->stream != NULL) {
10581 const struct dc_stream_status *status =
10582 dc_stream_get_status(dm_new_crtc_state->stream);
10583
10584 if (!status)
10585 status = dc_state_get_stream_status(dc_state,
10586 dm_new_crtc_state->stream);
10587 if (!status)
10588 drm_err(dev,
10589 "got no status for stream %p on acrtc%p\n",
10590 dm_new_crtc_state->stream, acrtc);
10591 else
10592 acrtc->otg_inst = status->primary_otg_inst;
10593 }
10594 }
10595
10596 /* During boot up and resume the DC layer will reset the panel brightness
10597 * to fix a flicker issue.
10598 * It will cause the dm->actual_brightness is not the current panel brightness
10599 * level. (the dm->brightness is the correct panel level)
10600 * So we set the backlight level with dm->brightness value after set mode
10601 */
10602 if (set_backlight_level) {
10603 for (i = 0; i < dm->num_of_edps; i++) {
10604 if (dm->backlight_dev[i])
10605 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
10606 }
10607 }
10608 }
10609
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)10610 static void dm_set_writeback(struct amdgpu_display_manager *dm,
10611 struct dm_crtc_state *crtc_state,
10612 struct drm_connector *connector,
10613 struct drm_connector_state *new_con_state)
10614 {
10615 struct drm_writeback_connector *wb_conn = drm_connector_to_writeback(connector);
10616 struct amdgpu_device *adev = dm->adev;
10617 struct amdgpu_crtc *acrtc;
10618 struct dc_writeback_info *wb_info;
10619 struct pipe_ctx *pipe = NULL;
10620 struct amdgpu_framebuffer *afb;
10621 int i = 0;
10622
10623 wb_info = kzalloc_obj(*wb_info);
10624 if (!wb_info) {
10625 drm_err(adev_to_drm(adev), "Failed to allocate wb_info\n");
10626 return;
10627 }
10628
10629 acrtc = to_amdgpu_crtc(wb_conn->encoder.crtc);
10630 if (!acrtc) {
10631 drm_err(adev_to_drm(adev), "no amdgpu_crtc found\n");
10632 kfree(wb_info);
10633 return;
10634 }
10635
10636 afb = to_amdgpu_framebuffer(new_con_state->writeback_job->fb);
10637 if (!afb) {
10638 drm_err(adev_to_drm(adev), "No amdgpu_framebuffer found\n");
10639 kfree(wb_info);
10640 return;
10641 }
10642
10643 for (i = 0; i < MAX_PIPES; i++) {
10644 if (dm->dc->current_state->res_ctx.pipe_ctx[i].stream == crtc_state->stream) {
10645 pipe = &dm->dc->current_state->res_ctx.pipe_ctx[i];
10646 break;
10647 }
10648 }
10649
10650 /* fill in wb_info */
10651 wb_info->wb_enabled = true;
10652
10653 wb_info->dwb_pipe_inst = 0;
10654 wb_info->dwb_params.dwbscl_black_color = 0;
10655 wb_info->dwb_params.hdr_mult = 0x1F000;
10656 wb_info->dwb_params.csc_params.gamut_adjust_type = CM_GAMUT_ADJUST_TYPE_BYPASS;
10657 wb_info->dwb_params.csc_params.gamut_coef_format = CM_GAMUT_REMAP_COEF_FORMAT_S2_13;
10658 wb_info->dwb_params.output_depth = DWB_OUTPUT_PIXEL_DEPTH_10BPC;
10659 wb_info->dwb_params.cnv_params.cnv_out_bpc = DWB_CNV_OUT_BPC_10BPC;
10660
10661 /* width & height from crtc */
10662 wb_info->dwb_params.cnv_params.src_width = acrtc->base.mode.crtc_hdisplay;
10663 wb_info->dwb_params.cnv_params.src_height = acrtc->base.mode.crtc_vdisplay;
10664 wb_info->dwb_params.dest_width = acrtc->base.mode.crtc_hdisplay;
10665 wb_info->dwb_params.dest_height = acrtc->base.mode.crtc_vdisplay;
10666
10667 wb_info->dwb_params.cnv_params.crop_en = false;
10668 wb_info->dwb_params.stereo_params.stereo_enabled = false;
10669
10670 wb_info->dwb_params.cnv_params.out_max_pix_val = 0x3ff; // 10 bits
10671 wb_info->dwb_params.cnv_params.out_min_pix_val = 0;
10672 wb_info->dwb_params.cnv_params.fc_out_format = DWB_OUT_FORMAT_32BPP_ARGB;
10673 wb_info->dwb_params.cnv_params.out_denorm_mode = DWB_OUT_DENORM_BYPASS;
10674
10675 wb_info->dwb_params.out_format = dwb_scaler_mode_bypass444;
10676
10677 wb_info->dwb_params.capture_rate = dwb_capture_rate_0;
10678
10679 wb_info->dwb_params.scaler_taps.h_taps = 1;
10680 wb_info->dwb_params.scaler_taps.v_taps = 1;
10681 wb_info->dwb_params.scaler_taps.h_taps_c = 1;
10682 wb_info->dwb_params.scaler_taps.v_taps_c = 1;
10683 wb_info->dwb_params.subsample_position = DWB_INTERSTITIAL_SUBSAMPLING;
10684
10685 wb_info->mcif_buf_params.luma_pitch = afb->base.pitches[0];
10686 wb_info->mcif_buf_params.chroma_pitch = afb->base.pitches[1];
10687
10688 for (i = 0; i < DWB_MCIF_BUF_COUNT; i++) {
10689 wb_info->mcif_buf_params.luma_address[i] = afb->address;
10690 wb_info->mcif_buf_params.chroma_address[i] = 0;
10691 }
10692
10693 wb_info->mcif_buf_params.p_vmid = 1;
10694 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 0, 0)) {
10695 wb_info->mcif_warmup_params.start_address.quad_part = afb->address;
10696 wb_info->mcif_warmup_params.region_size =
10697 wb_info->mcif_buf_params.luma_pitch * wb_info->dwb_params.dest_height;
10698 }
10699 wb_info->mcif_warmup_params.p_vmid = 1;
10700 wb_info->writeback_source_plane = pipe->plane_state;
10701
10702 dc_stream_add_writeback(dm->dc, crtc_state->stream, wb_info);
10703
10704 acrtc->wb_pending = true;
10705 acrtc->wb_conn = wb_conn;
10706 drm_writeback_queue_job(wb_conn, new_con_state);
10707 }
10708
amdgpu_dm_update_hdcp(struct drm_atomic_state * state)10709 static void amdgpu_dm_update_hdcp(struct drm_atomic_state *state)
10710 {
10711 struct drm_connector_state *old_con_state, *new_con_state;
10712 struct drm_device *dev = state->dev;
10713 struct drm_connector *connector;
10714 struct amdgpu_device *adev = drm_to_adev(dev);
10715 int i;
10716
10717 if (!adev->dm.hdcp_workqueue)
10718 return;
10719
10720 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10721 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
10722 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
10723 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10724 struct dm_crtc_state *dm_new_crtc_state;
10725 struct amdgpu_dm_connector *aconnector;
10726
10727 if (!connector || connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
10728 continue;
10729
10730 aconnector = to_amdgpu_dm_connector(connector);
10731
10732 drm_dbg(dev, "[HDCP_DM] -------------- i : %x ----------\n", i);
10733
10734 drm_dbg(dev, "[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n",
10735 connector->index, connector->status, connector->dpms);
10736 drm_dbg(dev, "[HDCP_DM] state protection old: %x new: %x\n",
10737 old_con_state->content_protection, new_con_state->content_protection);
10738
10739 if (aconnector->dc_sink) {
10740 if (aconnector->dc_sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
10741 aconnector->dc_sink->sink_signal != SIGNAL_TYPE_NONE) {
10742 drm_dbg(dev, "[HDCP_DM] pipe_ctx dispname=%s\n",
10743 aconnector->dc_sink->edid_caps.display_name);
10744 }
10745 }
10746
10747 new_crtc_state = NULL;
10748 old_crtc_state = NULL;
10749
10750 if (acrtc) {
10751 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
10752 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
10753 }
10754
10755 if (old_crtc_state)
10756 drm_dbg(dev, "old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
10757 old_crtc_state->enable,
10758 old_crtc_state->active,
10759 old_crtc_state->mode_changed,
10760 old_crtc_state->active_changed,
10761 old_crtc_state->connectors_changed);
10762
10763 if (new_crtc_state)
10764 drm_dbg(dev, "NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
10765 new_crtc_state->enable,
10766 new_crtc_state->active,
10767 new_crtc_state->mode_changed,
10768 new_crtc_state->active_changed,
10769 new_crtc_state->connectors_changed);
10770
10771
10772 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10773
10774 if (dm_new_crtc_state && dm_new_crtc_state->stream == NULL &&
10775 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
10776 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
10777 new_con_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
10778 dm_new_con_state->update_hdcp = true;
10779 continue;
10780 }
10781
10782 if (is_content_protection_different(new_crtc_state, old_crtc_state, new_con_state,
10783 old_con_state, connector, adev->dm.hdcp_workqueue)) {
10784 /* when display is unplugged from mst hub, connctor will
10785 * be destroyed within dm_dp_mst_connector_destroy. connector
10786 * hdcp perperties, like type, undesired, desired, enabled,
10787 * will be lost. So, save hdcp properties into hdcp_work within
10788 * amdgpu_dm_atomic_commit_tail. if the same display is
10789 * plugged back with same display index, its hdcp properties
10790 * will be retrieved from hdcp_work within dm_dp_mst_get_modes
10791 */
10792
10793 bool enable_encryption = false;
10794
10795 if (new_con_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED)
10796 enable_encryption = true;
10797
10798 if (aconnector->dc_link && aconnector->dc_sink &&
10799 aconnector->dc_link->type == dc_connection_mst_branch) {
10800 struct hdcp_workqueue *hdcp_work = adev->dm.hdcp_workqueue;
10801 struct hdcp_workqueue *hdcp_w =
10802 &hdcp_work[aconnector->dc_link->link_index];
10803
10804 hdcp_w->hdcp_content_type[connector->index] =
10805 new_con_state->hdcp_content_type;
10806 hdcp_w->content_protection[connector->index] =
10807 new_con_state->content_protection;
10808 }
10809
10810 if (new_crtc_state && new_crtc_state->mode_changed &&
10811 new_con_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED)
10812 enable_encryption = true;
10813
10814 drm_info(dev, "[HDCP_DM] hdcp_update_display enable_encryption = %x\n", enable_encryption);
10815
10816 if (aconnector->dc_link)
10817 hdcp_update_display(
10818 adev->dm.hdcp_workqueue, aconnector->dc_link->link_index, aconnector,
10819 new_con_state->hdcp_content_type, enable_encryption);
10820 }
10821 }
10822 }
10823
amdgpu_dm_atomic_setup_commit(struct drm_atomic_state * state)10824 static int amdgpu_dm_atomic_setup_commit(struct drm_atomic_state *state)
10825 {
10826 struct drm_crtc *crtc;
10827 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10828 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10829 int i, ret;
10830
10831 ret = drm_dp_mst_atomic_setup_commit(state);
10832 if (ret)
10833 return ret;
10834
10835 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10836 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10837 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10838 /*
10839 * Color management settings. We also update color properties
10840 * when a modeset is needed, to ensure it gets reprogrammed.
10841 */
10842 if (dm_new_crtc_state->base.active && dm_new_crtc_state->stream &&
10843 (dm_new_crtc_state->base.color_mgmt_changed ||
10844 dm_old_crtc_state->regamma_tf != dm_new_crtc_state->regamma_tf ||
10845 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
10846 ret = amdgpu_dm_update_crtc_color_mgmt(dm_new_crtc_state);
10847 if (ret) {
10848 drm_dbg_atomic(state->dev, "Failed to update color state\n");
10849 return ret;
10850 }
10851 }
10852 }
10853
10854 return 0;
10855 }
10856
10857 /**
10858 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation.
10859 * @state: The atomic state to commit
10860 *
10861 * This will tell DC to commit the constructed DC state from atomic_check,
10862 * programming the hardware. Any failures here implies a hardware failure, since
10863 * atomic check should have filtered anything non-kosher.
10864 */
amdgpu_dm_atomic_commit_tail(struct drm_atomic_state * state)10865 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
10866 {
10867 struct drm_device *dev = state->dev;
10868 struct amdgpu_device *adev = drm_to_adev(dev);
10869 struct amdgpu_display_manager *dm = &adev->dm;
10870 struct dm_atomic_state *dm_state;
10871 struct dc_state *dc_state = NULL;
10872 u32 i, j;
10873 struct drm_crtc *crtc;
10874 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10875 unsigned long flags;
10876 bool wait_for_vblank = true;
10877 struct drm_connector *connector;
10878 struct drm_connector_state *old_con_state = NULL, *new_con_state = NULL;
10879 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10880 int crtc_disable_count = 0;
10881
10882 trace_amdgpu_dm_atomic_commit_tail_begin(state);
10883
10884 drm_atomic_helper_update_legacy_modeset_state(dev, state);
10885 drm_dp_mst_atomic_wait_for_dependencies(state);
10886
10887 dm_state = dm_atomic_get_new_state(state);
10888 if (dm_state && dm_state->context) {
10889 dc_state = dm_state->context;
10890 amdgpu_dm_commit_streams(state, dc_state);
10891 }
10892
10893 amdgpu_dm_update_hdcp(state);
10894
10895 /* Handle connector state changes */
10896 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10897 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
10898 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
10899 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
10900 struct dc_surface_update *dummy_updates;
10901 struct dc_stream_update stream_update;
10902 struct dc_info_packet hdr_packet;
10903 struct dc_stream_status *status = NULL;
10904 bool abm_changed, hdr_changed, scaling_changed, output_color_space_changed = false;
10905
10906 memset(&stream_update, 0, sizeof(stream_update));
10907
10908 if (acrtc) {
10909 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
10910 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
10911 }
10912
10913 /* Skip any modesets/resets */
10914 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
10915 continue;
10916
10917 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10918 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10919
10920 scaling_changed = is_scaling_state_different(dm_new_con_state,
10921 dm_old_con_state);
10922
10923 if ((new_con_state->hdmi.broadcast_rgb != old_con_state->hdmi.broadcast_rgb) &&
10924 (dm_old_crtc_state->stream->output_color_space !=
10925 get_output_color_space(&dm_new_crtc_state->stream->timing, new_con_state)))
10926 output_color_space_changed = true;
10927
10928 abm_changed = dm_new_crtc_state->abm_level !=
10929 dm_old_crtc_state->abm_level;
10930
10931 hdr_changed =
10932 !drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state);
10933
10934 if (!scaling_changed && !abm_changed && !hdr_changed && !output_color_space_changed)
10935 continue;
10936
10937 stream_update.stream = dm_new_crtc_state->stream;
10938 if (scaling_changed) {
10939 update_stream_scaling_settings(dev, &dm_new_con_state->base.crtc->mode,
10940 dm_new_con_state, dm_new_crtc_state->stream);
10941
10942 stream_update.src = dm_new_crtc_state->stream->src;
10943 stream_update.dst = dm_new_crtc_state->stream->dst;
10944 }
10945
10946 if (output_color_space_changed) {
10947 dm_new_crtc_state->stream->output_color_space
10948 = get_output_color_space(&dm_new_crtc_state->stream->timing, new_con_state);
10949
10950 stream_update.output_color_space = &dm_new_crtc_state->stream->output_color_space;
10951 }
10952
10953 if (abm_changed) {
10954 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level;
10955
10956 stream_update.abm_level = &dm_new_crtc_state->abm_level;
10957 }
10958
10959 if (hdr_changed) {
10960 fill_hdr_info_packet(new_con_state, &hdr_packet);
10961 stream_update.hdr_static_metadata = &hdr_packet;
10962 }
10963
10964 status = dc_stream_get_status(dm_new_crtc_state->stream);
10965
10966 if (WARN_ON(!status))
10967 continue;
10968
10969 WARN_ON(!status->plane_count);
10970
10971 /*
10972 * TODO: DC refuses to perform stream updates without a dc_surface_update.
10973 * Here we create an empty update on each plane.
10974 * To fix this, DC should permit updating only stream properties.
10975 */
10976 dummy_updates = kzalloc(sizeof(struct dc_surface_update) * MAX_SURFACES, GFP_KERNEL);
10977 if (!dummy_updates) {
10978 drm_err(adev_to_drm(adev), "Failed to allocate memory for dummy_updates.\n");
10979 continue;
10980 }
10981 for (j = 0; j < status->plane_count; j++)
10982 dummy_updates[j].surface = status->plane_states[j];
10983
10984 sort(dummy_updates, status->plane_count,
10985 sizeof(*dummy_updates), dm_plane_layer_index_cmp, NULL);
10986
10987 mutex_lock(&dm->dc_lock);
10988 dc_exit_ips_for_hw_access(dm->dc);
10989 dc_update_planes_and_stream(dm->dc,
10990 dummy_updates,
10991 status->plane_count,
10992 dm_new_crtc_state->stream,
10993 &stream_update);
10994 mutex_unlock(&dm->dc_lock);
10995 kfree(dummy_updates);
10996
10997 drm_connector_update_privacy_screen(new_con_state);
10998 }
10999
11000 /**
11001 * Enable interrupts for CRTCs that are newly enabled or went through
11002 * a modeset. It was intentionally deferred until after the front end
11003 * state was modified to wait until the OTG was on and so the IRQ
11004 * handlers didn't access stale or invalid state.
11005 */
11006 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
11007 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
11008 #ifdef CONFIG_DEBUG_FS
11009 enum amdgpu_dm_pipe_crc_source cur_crc_src;
11010 #endif
11011 /* Count number of newly disabled CRTCs for dropping PM refs later. */
11012 if (old_crtc_state->active && !new_crtc_state->active)
11013 crtc_disable_count++;
11014
11015 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11016 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
11017
11018 /* For freesync config update on crtc state and params for irq */
11019 update_stream_irq_parameters(dm, dm_new_crtc_state);
11020
11021 #ifdef CONFIG_DEBUG_FS
11022 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
11023 cur_crc_src = acrtc->dm_irq_params.crc_src;
11024 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
11025 #endif
11026
11027 if (new_crtc_state->active &&
11028 (!old_crtc_state->active ||
11029 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
11030 dc_stream_retain(dm_new_crtc_state->stream);
11031 acrtc->dm_irq_params.stream = dm_new_crtc_state->stream;
11032 manage_dm_interrupts(adev, acrtc, dm_new_crtc_state);
11033 }
11034 /* Handle vrr on->off / off->on transitions */
11035 amdgpu_dm_handle_vrr_transition(dm_old_crtc_state, dm_new_crtc_state);
11036
11037 #ifdef CONFIG_DEBUG_FS
11038 if (new_crtc_state->active &&
11039 (!old_crtc_state->active ||
11040 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
11041 /**
11042 * Frontend may have changed so reapply the CRC capture
11043 * settings for the stream.
11044 */
11045 if (amdgpu_dm_is_valid_crc_source(cur_crc_src)) {
11046 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
11047 if (amdgpu_dm_crc_window_is_activated(crtc)) {
11048 uint8_t cnt;
11049
11050 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
11051 for (cnt = 0; cnt < MAX_CRC_WINDOW_NUM; cnt++) {
11052 if (acrtc->dm_irq_params.window_param[cnt].enable) {
11053 acrtc->dm_irq_params.window_param[cnt].update_win = true;
11054
11055 /**
11056 * It takes 2 frames for HW to stably generate CRC when
11057 * resuming from suspend, so we set skip_frame_cnt 2.
11058 */
11059 acrtc->dm_irq_params.window_param[cnt].skip_frame_cnt = 2;
11060 }
11061 }
11062 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
11063 }
11064 #endif
11065 if (amdgpu_dm_crtc_configure_crc_source(
11066 crtc, dm_new_crtc_state, cur_crc_src))
11067 drm_dbg_atomic(dev, "Failed to configure crc source");
11068 }
11069 }
11070 #endif
11071 }
11072
11073 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j)
11074 if (new_crtc_state->async_flip)
11075 wait_for_vblank = false;
11076
11077 /* update planes when needed per crtc*/
11078 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
11079 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11080
11081 if (dm_new_crtc_state->stream)
11082 amdgpu_dm_commit_planes(state, dev, dm, crtc, wait_for_vblank);
11083 }
11084
11085 /* Enable writeback */
11086 for_each_new_connector_in_state(state, connector, new_con_state, i) {
11087 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
11088 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
11089
11090 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
11091 continue;
11092
11093 if (!new_con_state->writeback_job)
11094 continue;
11095
11096 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
11097
11098 if (!new_crtc_state)
11099 continue;
11100
11101 if (acrtc->wb_enabled)
11102 continue;
11103
11104 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11105
11106 dm_set_writeback(dm, dm_new_crtc_state, connector, new_con_state);
11107 acrtc->wb_enabled = true;
11108 }
11109
11110 /* Update audio instances for each connector. */
11111 amdgpu_dm_commit_audio(dev, state);
11112
11113 /* restore the backlight level */
11114 for (i = 0; i < dm->num_of_edps; i++) {
11115 if (dm->backlight_dev[i] &&
11116 (dm->actual_brightness[i] != dm->brightness[i]))
11117 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
11118 }
11119
11120 /*
11121 * send vblank event on all events not handled in flip and
11122 * mark consumed event for drm_atomic_helper_commit_hw_done
11123 */
11124 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
11125 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
11126
11127 if (new_crtc_state->event)
11128 drm_send_event_locked(dev, &new_crtc_state->event->base);
11129
11130 new_crtc_state->event = NULL;
11131 }
11132 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
11133
11134 /* Signal HW programming completion */
11135 drm_atomic_helper_commit_hw_done(state);
11136
11137 if (wait_for_vblank)
11138 drm_atomic_helper_wait_for_flip_done(dev, state);
11139
11140 drm_atomic_helper_cleanup_planes(dev, state);
11141
11142 /* Don't free the memory if we are hitting this as part of suspend.
11143 * This way we don't free any memory during suspend; see
11144 * amdgpu_bo_free_kernel(). The memory will be freed in the first
11145 * non-suspend modeset or when the driver is torn down.
11146 */
11147 if (!adev->in_suspend) {
11148 /* return the stolen vga memory back to VRAM */
11149 if (!adev->mman.keep_stolen_vga_memory)
11150 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
11151 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL);
11152 }
11153
11154 /*
11155 * Finally, drop a runtime PM reference for each newly disabled CRTC,
11156 * so we can put the GPU into runtime suspend if we're not driving any
11157 * displays anymore
11158 */
11159 for (i = 0; i < crtc_disable_count; i++)
11160 pm_runtime_put_autosuspend(dev->dev);
11161 pm_runtime_mark_last_busy(dev->dev);
11162
11163 trace_amdgpu_dm_atomic_commit_tail_finish(state);
11164 }
11165
dm_force_atomic_commit(struct drm_connector * connector)11166 static int dm_force_atomic_commit(struct drm_connector *connector)
11167 {
11168 int ret = 0;
11169 struct drm_device *ddev = connector->dev;
11170 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
11171 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
11172 struct drm_plane *plane = disconnected_acrtc->base.primary;
11173 struct drm_connector_state *conn_state;
11174 struct drm_crtc_state *crtc_state;
11175 struct drm_plane_state *plane_state;
11176
11177 if (!state)
11178 return -ENOMEM;
11179
11180 state->acquire_ctx = ddev->mode_config.acquire_ctx;
11181
11182 /* Construct an atomic state to restore previous display setting */
11183
11184 /*
11185 * Attach connectors to drm_atomic_state
11186 */
11187 conn_state = drm_atomic_get_connector_state(state, connector);
11188
11189 /* Check for error in getting connector state */
11190 if (IS_ERR(conn_state)) {
11191 ret = PTR_ERR(conn_state);
11192 goto out;
11193 }
11194
11195 /* Attach crtc to drm_atomic_state*/
11196 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
11197
11198 /* Check for error in getting crtc state */
11199 if (IS_ERR(crtc_state)) {
11200 ret = PTR_ERR(crtc_state);
11201 goto out;
11202 }
11203
11204 /* force a restore */
11205 crtc_state->mode_changed = true;
11206
11207 /* Attach plane to drm_atomic_state */
11208 plane_state = drm_atomic_get_plane_state(state, plane);
11209
11210 /* Check for error in getting plane state */
11211 if (IS_ERR(plane_state)) {
11212 ret = PTR_ERR(plane_state);
11213 goto out;
11214 }
11215
11216 /* Call commit internally with the state we just constructed */
11217 ret = drm_atomic_commit(state);
11218
11219 out:
11220 drm_atomic_state_put(state);
11221 if (ret)
11222 drm_err(ddev, "Restoring old state failed with %i\n", ret);
11223
11224 return ret;
11225 }
11226
11227 /*
11228 * This function handles all cases when set mode does not come upon hotplug.
11229 * This includes when a display is unplugged then plugged back into the
11230 * same port and when running without usermode desktop manager supprot
11231 */
dm_restore_drm_connector_state(struct drm_device * dev,struct drm_connector * connector)11232 void dm_restore_drm_connector_state(struct drm_device *dev,
11233 struct drm_connector *connector)
11234 {
11235 struct amdgpu_dm_connector *aconnector;
11236 struct amdgpu_crtc *disconnected_acrtc;
11237 struct dm_crtc_state *acrtc_state;
11238
11239 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
11240 return;
11241
11242 aconnector = to_amdgpu_dm_connector(connector);
11243
11244 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
11245 return;
11246
11247 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
11248 if (!disconnected_acrtc)
11249 return;
11250
11251 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
11252 if (!acrtc_state->stream)
11253 return;
11254
11255 /*
11256 * If the previous sink is not released and different from the current,
11257 * we deduce we are in a state where we can not rely on usermode call
11258 * to turn on the display, so we do it here
11259 */
11260 if (acrtc_state->stream->sink != aconnector->dc_sink)
11261 dm_force_atomic_commit(&aconnector->base);
11262 }
11263
11264 /*
11265 * Grabs all modesetting locks to serialize against any blocking commits,
11266 * Waits for completion of all non blocking commits.
11267 */
do_aquire_global_lock(struct drm_device * dev,struct drm_atomic_state * state)11268 static int do_aquire_global_lock(struct drm_device *dev,
11269 struct drm_atomic_state *state)
11270 {
11271 struct drm_crtc *crtc;
11272 struct drm_crtc_commit *commit;
11273 long ret;
11274
11275 /*
11276 * Adding all modeset locks to aquire_ctx will
11277 * ensure that when the framework release it the
11278 * extra locks we are locking here will get released to
11279 */
11280 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
11281 if (ret)
11282 return ret;
11283
11284 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11285 spin_lock(&crtc->commit_lock);
11286 commit = list_first_entry_or_null(&crtc->commit_list,
11287 struct drm_crtc_commit, commit_entry);
11288 if (commit)
11289 drm_crtc_commit_get(commit);
11290 spin_unlock(&crtc->commit_lock);
11291
11292 if (!commit)
11293 continue;
11294
11295 /*
11296 * Make sure all pending HW programming completed and
11297 * page flips done
11298 */
11299 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
11300
11301 if (ret > 0)
11302 ret = wait_for_completion_interruptible_timeout(
11303 &commit->flip_done, 10*HZ);
11304
11305 if (ret == 0)
11306 drm_err(dev, "[CRTC:%d:%s] hw_done or flip_done timed out\n",
11307 crtc->base.id, crtc->name);
11308
11309 drm_crtc_commit_put(commit);
11310 }
11311
11312 return ret < 0 ? ret : 0;
11313 }
11314
get_freesync_config_for_crtc(struct dm_crtc_state * new_crtc_state,struct dm_connector_state * new_con_state)11315 static void get_freesync_config_for_crtc(
11316 struct dm_crtc_state *new_crtc_state,
11317 struct dm_connector_state *new_con_state)
11318 {
11319 struct mod_freesync_config config = {0};
11320 struct amdgpu_dm_connector *aconnector;
11321 struct drm_display_mode *mode = &new_crtc_state->base.mode;
11322 int vrefresh = drm_mode_vrefresh(mode);
11323 bool fs_vid_mode = false;
11324
11325 if (new_con_state->base.connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
11326 return;
11327
11328 aconnector = to_amdgpu_dm_connector(new_con_state->base.connector);
11329
11330 new_crtc_state->vrr_supported = new_con_state->freesync_capable &&
11331 vrefresh >= aconnector->min_vfreq &&
11332 vrefresh <= aconnector->max_vfreq;
11333
11334 if (new_crtc_state->vrr_supported) {
11335 new_crtc_state->stream->ignore_msa_timing_param = true;
11336 fs_vid_mode = new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
11337
11338 config.min_refresh_in_uhz = aconnector->min_vfreq * 1000000;
11339 config.max_refresh_in_uhz = aconnector->max_vfreq * 1000000;
11340 config.vsif_supported = true;
11341 config.btr = true;
11342
11343 if (fs_vid_mode) {
11344 config.state = VRR_STATE_ACTIVE_FIXED;
11345 config.fixed_refresh_in_uhz = new_crtc_state->freesync_config.fixed_refresh_in_uhz;
11346 goto out;
11347 } else if (new_crtc_state->base.vrr_enabled) {
11348 config.state = VRR_STATE_ACTIVE_VARIABLE;
11349 } else {
11350 config.state = VRR_STATE_INACTIVE;
11351 }
11352 } else {
11353 config.state = VRR_STATE_UNSUPPORTED;
11354 }
11355 out:
11356 new_crtc_state->freesync_config = config;
11357 }
11358
reset_freesync_config_for_crtc(struct dm_crtc_state * new_crtc_state)11359 static void reset_freesync_config_for_crtc(
11360 struct dm_crtc_state *new_crtc_state)
11361 {
11362 new_crtc_state->vrr_supported = false;
11363
11364 memset(&new_crtc_state->vrr_infopacket, 0,
11365 sizeof(new_crtc_state->vrr_infopacket));
11366 }
11367
11368 static bool
is_timing_unchanged_for_freesync(struct drm_crtc_state * old_crtc_state,struct drm_crtc_state * new_crtc_state)11369 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
11370 struct drm_crtc_state *new_crtc_state)
11371 {
11372 const struct drm_display_mode *old_mode, *new_mode;
11373
11374 if (!old_crtc_state || !new_crtc_state)
11375 return false;
11376
11377 old_mode = &old_crtc_state->mode;
11378 new_mode = &new_crtc_state->mode;
11379
11380 if (old_mode->clock == new_mode->clock &&
11381 old_mode->hdisplay == new_mode->hdisplay &&
11382 old_mode->vdisplay == new_mode->vdisplay &&
11383 old_mode->htotal == new_mode->htotal &&
11384 old_mode->vtotal != new_mode->vtotal &&
11385 old_mode->hsync_start == new_mode->hsync_start &&
11386 old_mode->vsync_start != new_mode->vsync_start &&
11387 old_mode->hsync_end == new_mode->hsync_end &&
11388 old_mode->vsync_end != new_mode->vsync_end &&
11389 old_mode->hskew == new_mode->hskew &&
11390 old_mode->vscan == new_mode->vscan &&
11391 (old_mode->vsync_end - old_mode->vsync_start) ==
11392 (new_mode->vsync_end - new_mode->vsync_start))
11393 return true;
11394
11395 return false;
11396 }
11397
set_freesync_fixed_config(struct dm_crtc_state * dm_new_crtc_state)11398 static void set_freesync_fixed_config(struct dm_crtc_state *dm_new_crtc_state)
11399 {
11400 u64 num, den, res;
11401 struct drm_crtc_state *new_crtc_state = &dm_new_crtc_state->base;
11402
11403 dm_new_crtc_state->freesync_config.state = VRR_STATE_ACTIVE_FIXED;
11404
11405 num = (unsigned long long)new_crtc_state->mode.clock * 1000 * 1000000;
11406 den = (unsigned long long)new_crtc_state->mode.htotal *
11407 (unsigned long long)new_crtc_state->mode.vtotal;
11408
11409 res = div_u64(num, den);
11410 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = res;
11411 }
11412
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)11413 static int dm_update_crtc_state(struct amdgpu_display_manager *dm,
11414 struct drm_atomic_state *state,
11415 struct drm_crtc *crtc,
11416 struct drm_crtc_state *old_crtc_state,
11417 struct drm_crtc_state *new_crtc_state,
11418 bool enable,
11419 bool *lock_and_validation_needed)
11420 {
11421 struct dm_atomic_state *dm_state = NULL;
11422 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
11423 struct dc_stream_state *new_stream;
11424 struct amdgpu_device *adev = dm->adev;
11425 int ret = 0;
11426
11427 /*
11428 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
11429 * update changed items
11430 */
11431 struct amdgpu_crtc *acrtc = NULL;
11432 struct drm_connector *connector = NULL;
11433 struct amdgpu_dm_connector *aconnector = NULL;
11434 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
11435 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
11436
11437 new_stream = NULL;
11438
11439 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
11440 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11441 acrtc = to_amdgpu_crtc(crtc);
11442 connector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
11443 if (connector)
11444 aconnector = to_amdgpu_dm_connector(connector);
11445
11446 /* TODO This hack should go away */
11447 if (connector && enable) {
11448 /* Make sure fake sink is created in plug-in scenario */
11449 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
11450 connector);
11451 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
11452 connector);
11453
11454 if (WARN_ON(!drm_new_conn_state)) {
11455 ret = -EINVAL;
11456 goto fail;
11457 }
11458
11459 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
11460 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
11461
11462 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
11463 goto skip_modeset;
11464
11465 new_stream = create_validate_stream_for_sink(connector,
11466 &new_crtc_state->mode,
11467 dm_new_conn_state,
11468 dm_old_crtc_state->stream);
11469
11470 /*
11471 * we can have no stream on ACTION_SET if a display
11472 * was disconnected during S3, in this case it is not an
11473 * error, the OS will be updated after detection, and
11474 * will do the right thing on next atomic commit
11475 */
11476
11477 if (!new_stream) {
11478 drm_dbg_driver(adev_to_drm(adev), "%s: Failed to create new stream for crtc %d\n",
11479 __func__, acrtc->base.base.id);
11480 ret = -ENOMEM;
11481 goto fail;
11482 }
11483
11484 /*
11485 * TODO: Check VSDB bits to decide whether this should
11486 * be enabled or not.
11487 */
11488 new_stream->triggered_crtc_reset.enabled =
11489 dm->force_timing_sync;
11490
11491 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
11492
11493 ret = fill_hdr_info_packet(drm_new_conn_state,
11494 &new_stream->hdr_static_metadata);
11495 if (ret)
11496 goto fail;
11497
11498 /*
11499 * If we already removed the old stream from the context
11500 * (and set the new stream to NULL) then we can't reuse
11501 * the old stream even if the stream and scaling are unchanged.
11502 * We'll hit the BUG_ON and black screen.
11503 *
11504 * TODO: Refactor this function to allow this check to work
11505 * in all conditions.
11506 */
11507 if (amdgpu_freesync_vid_mode &&
11508 dm_new_crtc_state->stream &&
11509 is_timing_unchanged_for_freesync(new_crtc_state, old_crtc_state))
11510 goto skip_modeset;
11511
11512 if (dm_new_crtc_state->stream &&
11513 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
11514 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
11515 new_crtc_state->mode_changed = false;
11516 drm_dbg_driver(adev_to_drm(adev), "Mode change not required, setting mode_changed to %d",
11517 new_crtc_state->mode_changed);
11518 }
11519 }
11520
11521 /* mode_changed flag may get updated above, need to check again */
11522 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
11523 goto skip_modeset;
11524
11525 drm_dbg_state(state->dev,
11526 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n",
11527 acrtc->crtc_id,
11528 new_crtc_state->enable,
11529 new_crtc_state->active,
11530 new_crtc_state->planes_changed,
11531 new_crtc_state->mode_changed,
11532 new_crtc_state->active_changed,
11533 new_crtc_state->connectors_changed);
11534
11535 /* Remove stream for any changed/disabled CRTC */
11536 if (!enable) {
11537
11538 if (!dm_old_crtc_state->stream)
11539 goto skip_modeset;
11540
11541 /* Unset freesync video if it was active before */
11542 if (dm_old_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED) {
11543 dm_new_crtc_state->freesync_config.state = VRR_STATE_INACTIVE;
11544 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = 0;
11545 }
11546
11547 /* Now check if we should set freesync video mode */
11548 if (amdgpu_freesync_vid_mode && dm_new_crtc_state->stream &&
11549 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
11550 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream) &&
11551 is_timing_unchanged_for_freesync(new_crtc_state,
11552 old_crtc_state)) {
11553 new_crtc_state->mode_changed = false;
11554 drm_dbg_driver(adev_to_drm(adev),
11555 "Mode change not required for front porch change, setting mode_changed to %d",
11556 new_crtc_state->mode_changed);
11557
11558 set_freesync_fixed_config(dm_new_crtc_state);
11559
11560 goto skip_modeset;
11561 } else if (amdgpu_freesync_vid_mode && aconnector &&
11562 is_freesync_video_mode(&new_crtc_state->mode,
11563 aconnector)) {
11564 struct drm_display_mode *high_mode;
11565
11566 high_mode = get_highest_refresh_rate_mode(aconnector, false);
11567 if (!drm_mode_equal(&new_crtc_state->mode, high_mode))
11568 set_freesync_fixed_config(dm_new_crtc_state);
11569 }
11570
11571 ret = dm_atomic_get_state(state, &dm_state);
11572 if (ret)
11573 goto fail;
11574
11575 drm_dbg_driver(adev_to_drm(adev), "Disabling DRM crtc: %d\n",
11576 crtc->base.id);
11577
11578 /* i.e. reset mode */
11579 if (dc_state_remove_stream(
11580 dm->dc,
11581 dm_state->context,
11582 dm_old_crtc_state->stream) != DC_OK) {
11583 ret = -EINVAL;
11584 goto fail;
11585 }
11586
11587 dc_stream_release(dm_old_crtc_state->stream);
11588 dm_new_crtc_state->stream = NULL;
11589
11590 reset_freesync_config_for_crtc(dm_new_crtc_state);
11591
11592 *lock_and_validation_needed = true;
11593
11594 } else {/* Add stream for any updated/enabled CRTC */
11595 /*
11596 * Quick fix to prevent NULL pointer on new_stream when
11597 * added MST connectors not found in existing crtc_state in the chained mode
11598 * TODO: need to dig out the root cause of that
11599 */
11600 if (!connector)
11601 goto skip_modeset;
11602
11603 if (modereset_required(new_crtc_state))
11604 goto skip_modeset;
11605
11606 if (amdgpu_dm_crtc_modeset_required(new_crtc_state, new_stream,
11607 dm_old_crtc_state->stream)) {
11608
11609 WARN_ON(dm_new_crtc_state->stream);
11610
11611 ret = dm_atomic_get_state(state, &dm_state);
11612 if (ret)
11613 goto fail;
11614
11615 dm_new_crtc_state->stream = new_stream;
11616
11617 dc_stream_retain(new_stream);
11618
11619 drm_dbg_atomic(adev_to_drm(adev), "Enabling DRM crtc: %d\n",
11620 crtc->base.id);
11621
11622 if (dc_state_add_stream(
11623 dm->dc,
11624 dm_state->context,
11625 dm_new_crtc_state->stream) != DC_OK) {
11626 ret = -EINVAL;
11627 goto fail;
11628 }
11629
11630 *lock_and_validation_needed = true;
11631 }
11632 }
11633
11634 skip_modeset:
11635 /* Release extra reference */
11636 if (new_stream)
11637 dc_stream_release(new_stream);
11638
11639 /*
11640 * We want to do dc stream updates that do not require a
11641 * full modeset below.
11642 */
11643 if (!(enable && connector && new_crtc_state->active))
11644 return 0;
11645 /*
11646 * Given above conditions, the dc state cannot be NULL because:
11647 * 1. We're in the process of enabling CRTCs (just been added
11648 * to the dc context, or already is on the context)
11649 * 2. Has a valid connector attached, and
11650 * 3. Is currently active and enabled.
11651 * => The dc stream state currently exists.
11652 */
11653 BUG_ON(dm_new_crtc_state->stream == NULL);
11654
11655 /* Scaling or underscan settings */
11656 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state) ||
11657 drm_atomic_crtc_needs_modeset(new_crtc_state))
11658 update_stream_scaling_settings(adev_to_drm(adev),
11659 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
11660
11661 /* ABM settings */
11662 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
11663
11664 /*
11665 * Color management settings. We also update color properties
11666 * when a modeset is needed, to ensure it gets reprogrammed.
11667 */
11668 if (dm_new_crtc_state->base.color_mgmt_changed ||
11669 dm_old_crtc_state->regamma_tf != dm_new_crtc_state->regamma_tf ||
11670 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
11671 ret = amdgpu_dm_check_crtc_color_mgmt(dm_new_crtc_state, true);
11672 if (ret)
11673 goto fail;
11674 }
11675
11676 /* Update Freesync settings. */
11677 get_freesync_config_for_crtc(dm_new_crtc_state,
11678 dm_new_conn_state);
11679
11680 return ret;
11681
11682 fail:
11683 if (new_stream)
11684 dc_stream_release(new_stream);
11685 return ret;
11686 }
11687
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)11688 static bool should_reset_plane(struct drm_atomic_state *state,
11689 struct drm_plane *plane,
11690 struct drm_plane_state *old_plane_state,
11691 struct drm_plane_state *new_plane_state)
11692 {
11693 struct drm_plane *other;
11694 struct drm_plane_state *old_other_state, *new_other_state;
11695 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
11696 struct dm_crtc_state *old_dm_crtc_state, *new_dm_crtc_state;
11697 struct amdgpu_device *adev = drm_to_adev(plane->dev);
11698 struct drm_connector_state *new_con_state;
11699 struct drm_connector *connector;
11700 int i;
11701
11702 /*
11703 * TODO: Remove this hack for all asics once it proves that the
11704 * fast updates works fine on DCN3.2+.
11705 */
11706 if (amdgpu_ip_version(adev, DCE_HWIP, 0) < IP_VERSION(3, 2, 0) &&
11707 state->allow_modeset)
11708 return true;
11709
11710 /* Check for writeback commit */
11711 for_each_new_connector_in_state(state, connector, new_con_state, i) {
11712 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
11713 continue;
11714
11715 if (new_con_state->writeback_job)
11716 return true;
11717 }
11718
11719 if (amdgpu_in_reset(adev) && state->allow_modeset)
11720 return true;
11721
11722 /* Exit early if we know that we're adding or removing the plane. */
11723 if (old_plane_state->crtc != new_plane_state->crtc)
11724 return true;
11725
11726 /* old crtc == new_crtc == NULL, plane not in context. */
11727 if (!new_plane_state->crtc)
11728 return false;
11729
11730 new_crtc_state =
11731 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
11732 old_crtc_state =
11733 drm_atomic_get_old_crtc_state(state, old_plane_state->crtc);
11734
11735 if (!new_crtc_state)
11736 return true;
11737
11738 /*
11739 * A change in cursor mode means a new dc pipe needs to be acquired or
11740 * released from the state
11741 */
11742 old_dm_crtc_state = to_dm_crtc_state(old_crtc_state);
11743 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
11744 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
11745 old_dm_crtc_state != NULL &&
11746 old_dm_crtc_state->cursor_mode != new_dm_crtc_state->cursor_mode) {
11747 return true;
11748 }
11749
11750 /* CRTC Degamma changes currently require us to recreate planes. */
11751 if (new_crtc_state->color_mgmt_changed)
11752 return true;
11753
11754 /*
11755 * On zpos change, planes need to be reordered by removing and re-adding
11756 * them one by one to the dc state, in order of descending zpos.
11757 *
11758 * TODO: We can likely skip bandwidth validation if the only thing that
11759 * changed about the plane was it'z z-ordering.
11760 */
11761 if (old_plane_state->normalized_zpos != new_plane_state->normalized_zpos)
11762 return true;
11763
11764 if (drm_atomic_crtc_needs_modeset(new_crtc_state))
11765 return true;
11766
11767 /*
11768 * If there are any new primary or overlay planes being added or
11769 * removed then the z-order can potentially change. To ensure
11770 * correct z-order and pipe acquisition the current DC architecture
11771 * requires us to remove and recreate all existing planes.
11772 *
11773 * TODO: Come up with a more elegant solution for this.
11774 */
11775 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) {
11776 struct amdgpu_framebuffer *old_afb, *new_afb;
11777 struct dm_plane_state *dm_new_other_state, *dm_old_other_state;
11778
11779 dm_new_other_state = to_dm_plane_state(new_other_state);
11780 dm_old_other_state = to_dm_plane_state(old_other_state);
11781
11782 if (other->type == DRM_PLANE_TYPE_CURSOR)
11783 continue;
11784
11785 if (old_other_state->crtc != new_plane_state->crtc &&
11786 new_other_state->crtc != new_plane_state->crtc)
11787 continue;
11788
11789 if (old_other_state->crtc != new_other_state->crtc)
11790 return true;
11791
11792 /* Src/dst size and scaling updates. */
11793 if (old_other_state->src_w != new_other_state->src_w ||
11794 old_other_state->src_h != new_other_state->src_h ||
11795 old_other_state->crtc_w != new_other_state->crtc_w ||
11796 old_other_state->crtc_h != new_other_state->crtc_h)
11797 return true;
11798
11799 /* Rotation / mirroring updates. */
11800 if (old_other_state->rotation != new_other_state->rotation)
11801 return true;
11802
11803 /* Blending updates. */
11804 if (old_other_state->pixel_blend_mode !=
11805 new_other_state->pixel_blend_mode)
11806 return true;
11807
11808 /* Alpha updates. */
11809 if (old_other_state->alpha != new_other_state->alpha)
11810 return true;
11811
11812 /* Colorspace changes. */
11813 if (old_other_state->color_range != new_other_state->color_range ||
11814 old_other_state->color_encoding != new_other_state->color_encoding)
11815 return true;
11816
11817 /* HDR/Transfer Function changes. */
11818 if (dm_old_other_state->degamma_tf != dm_new_other_state->degamma_tf ||
11819 dm_old_other_state->degamma_lut != dm_new_other_state->degamma_lut ||
11820 dm_old_other_state->hdr_mult != dm_new_other_state->hdr_mult ||
11821 dm_old_other_state->ctm != dm_new_other_state->ctm ||
11822 dm_old_other_state->shaper_lut != dm_new_other_state->shaper_lut ||
11823 dm_old_other_state->shaper_tf != dm_new_other_state->shaper_tf ||
11824 dm_old_other_state->lut3d != dm_new_other_state->lut3d ||
11825 dm_old_other_state->blend_lut != dm_new_other_state->blend_lut ||
11826 dm_old_other_state->blend_tf != dm_new_other_state->blend_tf)
11827 return true;
11828
11829 /* Framebuffer checks fall at the end. */
11830 if (!old_other_state->fb || !new_other_state->fb)
11831 continue;
11832
11833 /* Pixel format changes can require bandwidth updates. */
11834 if (old_other_state->fb->format != new_other_state->fb->format)
11835 return true;
11836
11837 old_afb = (struct amdgpu_framebuffer *)old_other_state->fb;
11838 new_afb = (struct amdgpu_framebuffer *)new_other_state->fb;
11839
11840 /* Tiling and DCC changes also require bandwidth updates. */
11841 if (old_afb->tiling_flags != new_afb->tiling_flags ||
11842 old_afb->base.modifier != new_afb->base.modifier)
11843 return true;
11844 }
11845
11846 return false;
11847 }
11848
dm_check_cursor_fb(struct amdgpu_crtc * new_acrtc,struct drm_plane_state * new_plane_state,struct drm_framebuffer * fb)11849 static int dm_check_cursor_fb(struct amdgpu_crtc *new_acrtc,
11850 struct drm_plane_state *new_plane_state,
11851 struct drm_framebuffer *fb)
11852 {
11853 struct amdgpu_device *adev = drm_to_adev(new_acrtc->base.dev);
11854 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
11855 unsigned int pitch;
11856 bool linear;
11857
11858 if (fb->width > new_acrtc->max_cursor_width ||
11859 fb->height > new_acrtc->max_cursor_height) {
11860 drm_dbg_atomic(adev_to_drm(adev), "Bad cursor FB size %dx%d\n",
11861 new_plane_state->fb->width,
11862 new_plane_state->fb->height);
11863 return -EINVAL;
11864 }
11865 if (new_plane_state->src_w != fb->width << 16 ||
11866 new_plane_state->src_h != fb->height << 16) {
11867 drm_dbg_atomic(adev_to_drm(adev), "Cropping not supported for cursor plane\n");
11868 return -EINVAL;
11869 }
11870
11871 /* Pitch in pixels */
11872 pitch = fb->pitches[0] / fb->format->cpp[0];
11873
11874 if (fb->width != pitch) {
11875 drm_dbg_atomic(adev_to_drm(adev), "Cursor FB width %d doesn't match pitch %d",
11876 fb->width, pitch);
11877 return -EINVAL;
11878 }
11879
11880 switch (pitch) {
11881 case 64:
11882 case 128:
11883 case 256:
11884 /* FB pitch is supported by cursor plane */
11885 break;
11886 default:
11887 drm_dbg_atomic(adev_to_drm(adev), "Bad cursor FB pitch %d px\n", pitch);
11888 return -EINVAL;
11889 }
11890
11891 /* Core DRM takes care of checking FB modifiers, so we only need to
11892 * check tiling flags when the FB doesn't have a modifier.
11893 */
11894 if (!(fb->flags & DRM_MODE_FB_MODIFIERS)) {
11895 if (adev->family == AMDGPU_FAMILY_GC_12_0_0) {
11896 linear = AMDGPU_TILING_GET(afb->tiling_flags, GFX12_SWIZZLE_MODE) == 0;
11897 } else if (adev->family >= AMDGPU_FAMILY_AI) {
11898 linear = AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE) == 0;
11899 } else {
11900 linear = AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_2D_TILED_THIN1 &&
11901 AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_1D_TILED_THIN1 &&
11902 AMDGPU_TILING_GET(afb->tiling_flags, MICRO_TILE_MODE) == 0;
11903 }
11904 if (!linear) {
11905 drm_dbg_atomic(adev_to_drm(adev), "Cursor FB not linear");
11906 return -EINVAL;
11907 }
11908 }
11909
11910 return 0;
11911 }
11912
11913 /*
11914 * Helper function for checking the cursor in native mode
11915 */
dm_check_native_cursor_state(struct drm_crtc * new_plane_crtc,struct drm_plane * plane,struct drm_plane_state * new_plane_state,bool enable)11916 static int dm_check_native_cursor_state(struct drm_crtc *new_plane_crtc,
11917 struct drm_plane *plane,
11918 struct drm_plane_state *new_plane_state,
11919 bool enable)
11920 {
11921
11922 struct amdgpu_crtc *new_acrtc;
11923 int ret;
11924
11925 if (!enable || !new_plane_crtc ||
11926 drm_atomic_plane_disabling(plane->state, new_plane_state))
11927 return 0;
11928
11929 new_acrtc = to_amdgpu_crtc(new_plane_crtc);
11930
11931 if (new_plane_state->src_x != 0 || new_plane_state->src_y != 0) {
11932 drm_dbg_atomic(new_plane_crtc->dev, "Cropping not supported for cursor plane\n");
11933 return -EINVAL;
11934 }
11935
11936 if (new_plane_state->fb) {
11937 ret = dm_check_cursor_fb(new_acrtc, new_plane_state,
11938 new_plane_state->fb);
11939 if (ret)
11940 return ret;
11941 }
11942
11943 return 0;
11944 }
11945
dm_should_update_native_cursor(struct drm_atomic_state * state,struct drm_crtc * old_plane_crtc,struct drm_crtc * new_plane_crtc,bool enable)11946 static bool dm_should_update_native_cursor(struct drm_atomic_state *state,
11947 struct drm_crtc *old_plane_crtc,
11948 struct drm_crtc *new_plane_crtc,
11949 bool enable)
11950 {
11951 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
11952 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
11953
11954 if (!enable) {
11955 if (old_plane_crtc == NULL)
11956 return true;
11957
11958 old_crtc_state = drm_atomic_get_old_crtc_state(
11959 state, old_plane_crtc);
11960 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
11961
11962 return dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE;
11963 } else {
11964 if (new_plane_crtc == NULL)
11965 return true;
11966
11967 new_crtc_state = drm_atomic_get_new_crtc_state(
11968 state, new_plane_crtc);
11969 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11970
11971 return dm_new_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE;
11972 }
11973 }
11974
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)11975 static int dm_update_plane_state(struct dc *dc,
11976 struct drm_atomic_state *state,
11977 struct drm_plane *plane,
11978 struct drm_plane_state *old_plane_state,
11979 struct drm_plane_state *new_plane_state,
11980 bool enable,
11981 bool *lock_and_validation_needed,
11982 bool *is_top_most_overlay)
11983 {
11984
11985 struct dm_atomic_state *dm_state = NULL;
11986 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
11987 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
11988 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
11989 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
11990 bool needs_reset, update_native_cursor;
11991 int ret = 0;
11992
11993
11994 new_plane_crtc = new_plane_state->crtc;
11995 old_plane_crtc = old_plane_state->crtc;
11996 dm_new_plane_state = to_dm_plane_state(new_plane_state);
11997 dm_old_plane_state = to_dm_plane_state(old_plane_state);
11998
11999 update_native_cursor = dm_should_update_native_cursor(state,
12000 old_plane_crtc,
12001 new_plane_crtc,
12002 enable);
12003
12004 if (plane->type == DRM_PLANE_TYPE_CURSOR && update_native_cursor) {
12005 ret = dm_check_native_cursor_state(new_plane_crtc, plane,
12006 new_plane_state, enable);
12007 if (ret)
12008 return ret;
12009
12010 return 0;
12011 }
12012
12013 needs_reset = should_reset_plane(state, plane, old_plane_state,
12014 new_plane_state);
12015
12016 /* Remove any changed/removed planes */
12017 if (!enable) {
12018 if (!needs_reset)
12019 return 0;
12020
12021 if (!old_plane_crtc)
12022 return 0;
12023
12024 old_crtc_state = drm_atomic_get_old_crtc_state(
12025 state, old_plane_crtc);
12026 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
12027
12028 if (!dm_old_crtc_state->stream)
12029 return 0;
12030
12031 drm_dbg_atomic(old_plane_crtc->dev, "Disabling DRM plane: %d on DRM crtc %d\n",
12032 plane->base.id, old_plane_crtc->base.id);
12033
12034 ret = dm_atomic_get_state(state, &dm_state);
12035 if (ret)
12036 return ret;
12037
12038 if (!dc_state_remove_plane(
12039 dc,
12040 dm_old_crtc_state->stream,
12041 dm_old_plane_state->dc_state,
12042 dm_state->context)) {
12043
12044 return -EINVAL;
12045 }
12046
12047 if (dm_old_plane_state->dc_state)
12048 dc_plane_state_release(dm_old_plane_state->dc_state);
12049
12050 dm_new_plane_state->dc_state = NULL;
12051
12052 *lock_and_validation_needed = true;
12053
12054 } else { /* Add new planes */
12055 struct dc_plane_state *dc_new_plane_state;
12056
12057 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
12058 return 0;
12059
12060 if (!new_plane_crtc)
12061 return 0;
12062
12063 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
12064 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12065
12066 if (!dm_new_crtc_state->stream)
12067 return 0;
12068
12069 if (!needs_reset)
12070 return 0;
12071
12072 ret = amdgpu_dm_plane_helper_check_state(new_plane_state, new_crtc_state);
12073 if (ret)
12074 goto out;
12075
12076 WARN_ON(dm_new_plane_state->dc_state);
12077
12078 dc_new_plane_state = dc_create_plane_state(dc);
12079 if (!dc_new_plane_state) {
12080 ret = -ENOMEM;
12081 goto out;
12082 }
12083
12084 drm_dbg_atomic(new_plane_crtc->dev, "Enabling DRM plane: %d on DRM crtc %d\n",
12085 plane->base.id, new_plane_crtc->base.id);
12086
12087 ret = fill_dc_plane_attributes(
12088 drm_to_adev(new_plane_crtc->dev),
12089 dc_new_plane_state,
12090 new_plane_state,
12091 new_crtc_state);
12092 if (ret) {
12093 dc_plane_state_release(dc_new_plane_state);
12094 goto out;
12095 }
12096
12097 ret = dm_atomic_get_state(state, &dm_state);
12098 if (ret) {
12099 dc_plane_state_release(dc_new_plane_state);
12100 goto out;
12101 }
12102
12103 /*
12104 * Any atomic check errors that occur after this will
12105 * not need a release. The plane state will be attached
12106 * to the stream, and therefore part of the atomic
12107 * state. It'll be released when the atomic state is
12108 * cleaned.
12109 */
12110 if (!dc_state_add_plane(
12111 dc,
12112 dm_new_crtc_state->stream,
12113 dc_new_plane_state,
12114 dm_state->context)) {
12115
12116 dc_plane_state_release(dc_new_plane_state);
12117 ret = -EINVAL;
12118 goto out;
12119 }
12120
12121 dm_new_plane_state->dc_state = dc_new_plane_state;
12122
12123 dm_new_crtc_state->mpo_requested |= (plane->type == DRM_PLANE_TYPE_OVERLAY);
12124
12125 /* Tell DC to do a full surface update every time there
12126 * is a plane change. Inefficient, but works for now.
12127 */
12128 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
12129
12130 *lock_and_validation_needed = true;
12131 }
12132
12133 out:
12134 /* If enabling cursor overlay failed, attempt fallback to native mode */
12135 if (enable && ret == -EINVAL && plane->type == DRM_PLANE_TYPE_CURSOR) {
12136 ret = dm_check_native_cursor_state(new_plane_crtc, plane,
12137 new_plane_state, enable);
12138 if (ret)
12139 return ret;
12140
12141 dm_new_crtc_state->cursor_mode = DM_CURSOR_NATIVE_MODE;
12142 }
12143
12144 return ret;
12145 }
12146
dm_get_oriented_plane_size(struct drm_plane_state * plane_state,int * src_w,int * src_h)12147 static void dm_get_oriented_plane_size(struct drm_plane_state *plane_state,
12148 int *src_w, int *src_h)
12149 {
12150 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
12151 case DRM_MODE_ROTATE_90:
12152 case DRM_MODE_ROTATE_270:
12153 *src_w = plane_state->src_h >> 16;
12154 *src_h = plane_state->src_w >> 16;
12155 break;
12156 case DRM_MODE_ROTATE_0:
12157 case DRM_MODE_ROTATE_180:
12158 default:
12159 *src_w = plane_state->src_w >> 16;
12160 *src_h = plane_state->src_h >> 16;
12161 break;
12162 }
12163 }
12164
12165 static void
dm_get_plane_scale(struct drm_plane_state * plane_state,int * out_plane_scale_w,int * out_plane_scale_h)12166 dm_get_plane_scale(struct drm_plane_state *plane_state,
12167 int *out_plane_scale_w, int *out_plane_scale_h)
12168 {
12169 int plane_src_w, plane_src_h;
12170
12171 dm_get_oriented_plane_size(plane_state, &plane_src_w, &plane_src_h);
12172 *out_plane_scale_w = plane_src_w ? plane_state->crtc_w * 1000 / plane_src_w : 0;
12173 *out_plane_scale_h = plane_src_h ? plane_state->crtc_h * 1000 / plane_src_h : 0;
12174 }
12175
12176 /*
12177 * The normalized_zpos value cannot be used by this iterator directly. It's only
12178 * calculated for enabled planes, potentially causing normalized_zpos collisions
12179 * between enabled/disabled planes in the atomic state. We need a unique value
12180 * so that the iterator will not generate the same object twice, or loop
12181 * indefinitely.
12182 */
__get_next_zpos(struct drm_atomic_state * state,struct __drm_planes_state * prev)12183 static inline struct __drm_planes_state *__get_next_zpos(
12184 struct drm_atomic_state *state,
12185 struct __drm_planes_state *prev)
12186 {
12187 unsigned int highest_zpos = 0, prev_zpos = 256;
12188 uint32_t highest_id = 0, prev_id = UINT_MAX;
12189 struct drm_plane_state *new_plane_state;
12190 struct drm_plane *plane;
12191 int i, highest_i = -1;
12192
12193 if (prev != NULL) {
12194 prev_zpos = prev->new_state->zpos;
12195 prev_id = prev->ptr->base.id;
12196 }
12197
12198 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
12199 /* Skip planes with higher zpos than the previously returned */
12200 if (new_plane_state->zpos > prev_zpos ||
12201 (new_plane_state->zpos == prev_zpos &&
12202 plane->base.id >= prev_id))
12203 continue;
12204
12205 /* Save the index of the plane with highest zpos */
12206 if (new_plane_state->zpos > highest_zpos ||
12207 (new_plane_state->zpos == highest_zpos &&
12208 plane->base.id > highest_id)) {
12209 highest_zpos = new_plane_state->zpos;
12210 highest_id = plane->base.id;
12211 highest_i = i;
12212 }
12213 }
12214
12215 if (highest_i < 0)
12216 return NULL;
12217
12218 return &state->planes[highest_i];
12219 }
12220
12221 /*
12222 * Use the uniqueness of the plane's (zpos, drm obj ID) combination to iterate
12223 * by descending zpos, as read from the new plane state. This is the same
12224 * ordering as defined by drm_atomic_normalize_zpos().
12225 */
12226 #define for_each_oldnew_plane_in_descending_zpos(__state, plane, old_plane_state, new_plane_state) \
12227 for (struct __drm_planes_state *__i = __get_next_zpos((__state), NULL); \
12228 __i != NULL; __i = __get_next_zpos((__state), __i)) \
12229 for_each_if(((plane) = __i->ptr, \
12230 (void)(plane) /* Only to avoid unused-but-set-variable warning */, \
12231 (old_plane_state) = __i->old_state, \
12232 (new_plane_state) = __i->new_state, 1))
12233
add_affected_mst_dsc_crtcs(struct drm_atomic_state * state,struct drm_crtc * crtc)12234 static int add_affected_mst_dsc_crtcs(struct drm_atomic_state *state, struct drm_crtc *crtc)
12235 {
12236 struct drm_connector *connector;
12237 struct drm_connector_state *conn_state, *old_conn_state;
12238 struct amdgpu_dm_connector *aconnector = NULL;
12239 int i;
12240
12241 for_each_oldnew_connector_in_state(state, connector, old_conn_state, conn_state, i) {
12242 if (!conn_state->crtc)
12243 conn_state = old_conn_state;
12244
12245 if (conn_state->crtc != crtc)
12246 continue;
12247
12248 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
12249 continue;
12250
12251 aconnector = to_amdgpu_dm_connector(connector);
12252 if (!aconnector->mst_output_port || !aconnector->mst_root)
12253 aconnector = NULL;
12254 else
12255 break;
12256 }
12257
12258 if (!aconnector)
12259 return 0;
12260
12261 return drm_dp_mst_add_affected_dsc_crtcs(state, &aconnector->mst_root->mst_mgr);
12262 }
12263
12264 /**
12265 * DOC: Cursor Modes - Native vs Overlay
12266 *
12267 * In native mode, the cursor uses a integrated cursor pipe within each DCN hw
12268 * plane. It does not require a dedicated hw plane to enable, but it is
12269 * subjected to the same z-order and scaling as the hw plane. It also has format
12270 * restrictions, a RGB cursor in native mode cannot be enabled within a non-RGB
12271 * hw plane.
12272 *
12273 * In overlay mode, the cursor uses a separate DCN hw plane, and thus has its
12274 * own scaling and z-pos. It also has no blending restrictions. It lends to a
12275 * cursor behavior more akin to a DRM client's expectations. However, it does
12276 * occupy an extra DCN plane, and therefore will only be used if a DCN plane is
12277 * available.
12278 */
12279
12280 /**
12281 * dm_crtc_get_cursor_mode() - Determine the required cursor mode on crtc
12282 * @adev: amdgpu device
12283 * @state: DRM atomic state
12284 * @dm_crtc_state: amdgpu state for the CRTC containing the cursor
12285 * @cursor_mode: Returns the required cursor mode on dm_crtc_state
12286 *
12287 * Get whether the cursor should be enabled in native mode, or overlay mode, on
12288 * the dm_crtc_state.
12289 *
12290 * The cursor should be enabled in overlay mode if there exists an underlying
12291 * plane - on which the cursor may be blended - that is either YUV formatted, or
12292 * scaled differently from the cursor.
12293 *
12294 * Since zpos info is required, drm_atomic_normalize_zpos must be called before
12295 * calling this function.
12296 *
12297 * Return: 0 on success, or an error code if getting the cursor plane state
12298 * failed.
12299 */
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)12300 static int dm_crtc_get_cursor_mode(struct amdgpu_device *adev,
12301 struct drm_atomic_state *state,
12302 struct dm_crtc_state *dm_crtc_state,
12303 enum amdgpu_dm_cursor_mode *cursor_mode)
12304 {
12305 struct drm_plane_state *old_plane_state, *plane_state, *cursor_state;
12306 struct drm_crtc_state *crtc_state = &dm_crtc_state->base;
12307 struct drm_plane *plane;
12308 bool consider_mode_change = false;
12309 bool entire_crtc_covered = false;
12310 bool cursor_changed = false;
12311 int underlying_scale_w, underlying_scale_h;
12312 int cursor_scale_w, cursor_scale_h;
12313 int i;
12314
12315 /* Overlay cursor not supported on HW before DCN
12316 * DCN401 does not have the cursor-on-scaled-plane or cursor-on-yuv-plane restrictions
12317 * as previous DCN generations, so enable native mode on DCN401
12318 */
12319 if (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 0, 1)) {
12320 *cursor_mode = DM_CURSOR_NATIVE_MODE;
12321 return 0;
12322 }
12323
12324 /* Init cursor_mode to be the same as current */
12325 *cursor_mode = dm_crtc_state->cursor_mode;
12326
12327 /*
12328 * Cursor mode can change if a plane's format changes, scale changes, is
12329 * enabled/disabled, or z-order changes.
12330 */
12331 for_each_oldnew_plane_in_state(state, plane, old_plane_state, plane_state, i) {
12332 int new_scale_w, new_scale_h, old_scale_w, old_scale_h;
12333
12334 /* Only care about planes on this CRTC */
12335 if ((drm_plane_mask(plane) & crtc_state->plane_mask) == 0)
12336 continue;
12337
12338 if (plane->type == DRM_PLANE_TYPE_CURSOR)
12339 cursor_changed = true;
12340
12341 if (drm_atomic_plane_enabling(old_plane_state, plane_state) ||
12342 drm_atomic_plane_disabling(old_plane_state, plane_state) ||
12343 old_plane_state->fb->format != plane_state->fb->format) {
12344 consider_mode_change = true;
12345 break;
12346 }
12347
12348 dm_get_plane_scale(plane_state, &new_scale_w, &new_scale_h);
12349 dm_get_plane_scale(old_plane_state, &old_scale_w, &old_scale_h);
12350 if (new_scale_w != old_scale_w || new_scale_h != old_scale_h) {
12351 consider_mode_change = true;
12352 break;
12353 }
12354 }
12355
12356 if (!consider_mode_change && !crtc_state->zpos_changed)
12357 return 0;
12358
12359 /*
12360 * If no cursor change on this CRTC, and not enabled on this CRTC, then
12361 * no need to set cursor mode. This avoids needlessly locking the cursor
12362 * state.
12363 */
12364 if (!cursor_changed &&
12365 !(drm_plane_mask(crtc_state->crtc->cursor) & crtc_state->plane_mask)) {
12366 return 0;
12367 }
12368
12369 cursor_state = drm_atomic_get_plane_state(state,
12370 crtc_state->crtc->cursor);
12371 if (IS_ERR(cursor_state))
12372 return PTR_ERR(cursor_state);
12373
12374 /* Cursor is disabled */
12375 if (!cursor_state->fb)
12376 return 0;
12377
12378 /* For all planes in descending z-order (all of which are below cursor
12379 * as per zpos definitions), check their scaling and format
12380 */
12381 for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, plane_state) {
12382
12383 /* Only care about non-cursor planes on this CRTC */
12384 if ((drm_plane_mask(plane) & crtc_state->plane_mask) == 0 ||
12385 plane->type == DRM_PLANE_TYPE_CURSOR)
12386 continue;
12387
12388 /* Underlying plane is YUV format - use overlay cursor */
12389 if (amdgpu_dm_plane_is_video_format(plane_state->fb->format->format)) {
12390 *cursor_mode = DM_CURSOR_OVERLAY_MODE;
12391 return 0;
12392 }
12393
12394 dm_get_plane_scale(plane_state,
12395 &underlying_scale_w, &underlying_scale_h);
12396 dm_get_plane_scale(cursor_state,
12397 &cursor_scale_w, &cursor_scale_h);
12398
12399 /* Underlying plane has different scale - use overlay cursor */
12400 if (cursor_scale_w != underlying_scale_w &&
12401 cursor_scale_h != underlying_scale_h) {
12402 *cursor_mode = DM_CURSOR_OVERLAY_MODE;
12403 return 0;
12404 }
12405
12406 /* If this plane covers the whole CRTC, no need to check planes underneath */
12407 if (plane_state->crtc_x <= 0 && plane_state->crtc_y <= 0 &&
12408 plane_state->crtc_x + plane_state->crtc_w >= crtc_state->mode.hdisplay &&
12409 plane_state->crtc_y + plane_state->crtc_h >= crtc_state->mode.vdisplay) {
12410 entire_crtc_covered = true;
12411 break;
12412 }
12413 }
12414
12415 /* If planes do not cover the entire CRTC, use overlay mode to enable
12416 * cursor over holes
12417 */
12418 if (entire_crtc_covered)
12419 *cursor_mode = DM_CURSOR_NATIVE_MODE;
12420 else
12421 *cursor_mode = DM_CURSOR_OVERLAY_MODE;
12422
12423 return 0;
12424 }
12425
amdgpu_dm_crtc_mem_type_changed(struct drm_device * dev,struct drm_atomic_state * state,struct drm_crtc_state * crtc_state)12426 static bool amdgpu_dm_crtc_mem_type_changed(struct drm_device *dev,
12427 struct drm_atomic_state *state,
12428 struct drm_crtc_state *crtc_state)
12429 {
12430 struct drm_plane *plane;
12431 struct drm_plane_state *new_plane_state, *old_plane_state;
12432
12433 drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
12434 new_plane_state = drm_atomic_get_plane_state(state, plane);
12435 old_plane_state = drm_atomic_get_plane_state(state, plane);
12436
12437 if (IS_ERR(new_plane_state) || IS_ERR(old_plane_state)) {
12438 drm_err(dev, "Failed to get plane state for plane %s\n", plane->name);
12439 return false;
12440 }
12441
12442 if (old_plane_state->fb && new_plane_state->fb &&
12443 get_mem_type(old_plane_state->fb) != get_mem_type(new_plane_state->fb))
12444 return true;
12445 }
12446
12447 return false;
12448 }
12449
12450 /**
12451 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM.
12452 *
12453 * @dev: The DRM device
12454 * @state: The atomic state to commit
12455 *
12456 * Validate that the given atomic state is programmable by DC into hardware.
12457 * This involves constructing a &struct dc_state reflecting the new hardware
12458 * state we wish to commit, then querying DC to see if it is programmable. It's
12459 * important not to modify the existing DC state. Otherwise, atomic_check
12460 * may unexpectedly commit hardware changes.
12461 *
12462 * When validating the DC state, it's important that the right locks are
12463 * acquired. For full updates case which removes/adds/updates streams on one
12464 * CRTC while flipping on another CRTC, acquiring global lock will guarantee
12465 * that any such full update commit will wait for completion of any outstanding
12466 * flip using DRMs synchronization events.
12467 *
12468 * Note that DM adds the affected connectors for all CRTCs in state, when that
12469 * might not seem necessary. This is because DC stream creation requires the
12470 * DC sink, which is tied to the DRM connector state. Cleaning this up should
12471 * be possible but non-trivial - a possible TODO item.
12472 *
12473 * Return: -Error code if validation failed.
12474 */
amdgpu_dm_atomic_check(struct drm_device * dev,struct drm_atomic_state * state)12475 static int amdgpu_dm_atomic_check(struct drm_device *dev,
12476 struct drm_atomic_state *state)
12477 {
12478 struct amdgpu_device *adev = drm_to_adev(dev);
12479 struct dm_atomic_state *dm_state = NULL;
12480 struct dc *dc = adev->dm.dc;
12481 struct drm_connector *connector;
12482 struct drm_connector_state *old_con_state, *new_con_state;
12483 struct drm_crtc *crtc;
12484 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
12485 struct drm_plane *plane;
12486 struct drm_plane_state *old_plane_state, *new_plane_state, *new_cursor_state;
12487 enum dc_status status;
12488 int ret, i;
12489 bool lock_and_validation_needed = false;
12490 bool is_top_most_overlay = true;
12491 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
12492 struct drm_dp_mst_topology_mgr *mgr;
12493 struct drm_dp_mst_topology_state *mst_state;
12494 struct dsc_mst_fairness_vars vars[MAX_PIPES] = {0};
12495
12496 trace_amdgpu_dm_atomic_check_begin(state);
12497
12498 ret = drm_atomic_helper_check_modeset(dev, state);
12499 if (ret) {
12500 drm_dbg_atomic(dev, "drm_atomic_helper_check_modeset() failed\n");
12501 goto fail;
12502 }
12503
12504 /* Check connector changes */
12505 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
12506 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
12507 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
12508
12509 /* Skip connectors that are disabled or part of modeset already. */
12510 if (!new_con_state->crtc)
12511 continue;
12512
12513 new_crtc_state = drm_atomic_get_crtc_state(state, new_con_state->crtc);
12514 if (IS_ERR(new_crtc_state)) {
12515 drm_dbg_atomic(dev, "drm_atomic_get_crtc_state() failed\n");
12516 ret = PTR_ERR(new_crtc_state);
12517 goto fail;
12518 }
12519
12520 if (dm_old_con_state->abm_level != dm_new_con_state->abm_level ||
12521 dm_old_con_state->scaling != dm_new_con_state->scaling)
12522 new_crtc_state->connectors_changed = true;
12523 }
12524
12525 if (dc_resource_is_dsc_encoding_supported(dc)) {
12526 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12527 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
12528 ret = add_affected_mst_dsc_crtcs(state, crtc);
12529 if (ret) {
12530 drm_dbg_atomic(dev, "add_affected_mst_dsc_crtcs() failed\n");
12531 goto fail;
12532 }
12533 }
12534 }
12535 }
12536 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12537 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
12538
12539 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
12540 !new_crtc_state->color_mgmt_changed &&
12541 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled &&
12542 dm_old_crtc_state->dsc_force_changed == false)
12543 continue;
12544
12545 ret = amdgpu_dm_verify_lut_sizes(new_crtc_state);
12546 if (ret) {
12547 drm_dbg_atomic(dev, "amdgpu_dm_verify_lut_sizes() failed\n");
12548 goto fail;
12549 }
12550
12551 if (!new_crtc_state->enable)
12552 continue;
12553
12554 ret = drm_atomic_add_affected_connectors(state, crtc);
12555 if (ret) {
12556 drm_dbg_atomic(dev, "drm_atomic_add_affected_connectors() failed\n");
12557 goto fail;
12558 }
12559
12560 ret = drm_atomic_add_affected_planes(state, crtc);
12561 if (ret) {
12562 drm_dbg_atomic(dev, "drm_atomic_add_affected_planes() failed\n");
12563 goto fail;
12564 }
12565
12566 if (dm_old_crtc_state->dsc_force_changed)
12567 new_crtc_state->mode_changed = true;
12568 }
12569
12570 /*
12571 * Add all primary and overlay planes on the CRTC to the state
12572 * whenever a plane is enabled to maintain correct z-ordering
12573 * and to enable fast surface updates.
12574 */
12575 drm_for_each_crtc(crtc, dev) {
12576 bool modified = false;
12577
12578 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
12579 if (plane->type == DRM_PLANE_TYPE_CURSOR)
12580 continue;
12581
12582 if (new_plane_state->crtc == crtc ||
12583 old_plane_state->crtc == crtc) {
12584 modified = true;
12585 break;
12586 }
12587 }
12588
12589 if (!modified)
12590 continue;
12591
12592 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
12593 if (plane->type == DRM_PLANE_TYPE_CURSOR)
12594 continue;
12595
12596 new_plane_state =
12597 drm_atomic_get_plane_state(state, plane);
12598
12599 if (IS_ERR(new_plane_state)) {
12600 ret = PTR_ERR(new_plane_state);
12601 drm_dbg_atomic(dev, "new_plane_state is BAD\n");
12602 goto fail;
12603 }
12604 }
12605 }
12606
12607 /*
12608 * DC consults the zpos (layer_index in DC terminology) to determine the
12609 * hw plane on which to enable the hw cursor (see
12610 * `dcn10_can_pipe_disable_cursor`). By now, all modified planes are in
12611 * atomic state, so call drm helper to normalize zpos.
12612 */
12613 ret = drm_atomic_normalize_zpos(dev, state);
12614 if (ret) {
12615 drm_dbg(dev, "drm_atomic_normalize_zpos() failed\n");
12616 goto fail;
12617 }
12618
12619 /*
12620 * Determine whether cursors on each CRTC should be enabled in native or
12621 * overlay mode.
12622 */
12623 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12624 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12625
12626 ret = dm_crtc_get_cursor_mode(adev, state, dm_new_crtc_state,
12627 &dm_new_crtc_state->cursor_mode);
12628 if (ret) {
12629 drm_dbg(dev, "Failed to determine cursor mode\n");
12630 goto fail;
12631 }
12632
12633 /*
12634 * If overlay cursor is needed, DC cannot go through the
12635 * native cursor update path. All enabled planes on the CRTC
12636 * need to be added for DC to not disable a plane by mistake
12637 */
12638 if (dm_new_crtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE) {
12639 if (amdgpu_ip_version(adev, DCE_HWIP, 0) == 0) {
12640 drm_dbg(dev, "Overlay cursor not supported on DCE\n");
12641 ret = -EINVAL;
12642 goto fail;
12643 }
12644
12645 ret = drm_atomic_add_affected_planes(state, crtc);
12646 if (ret)
12647 goto fail;
12648 }
12649 }
12650
12651 /* Remove exiting planes if they are modified */
12652 for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, new_plane_state) {
12653
12654 ret = dm_update_plane_state(dc, state, plane,
12655 old_plane_state,
12656 new_plane_state,
12657 false,
12658 &lock_and_validation_needed,
12659 &is_top_most_overlay);
12660 if (ret) {
12661 drm_dbg_atomic(dev, "dm_update_plane_state() failed\n");
12662 goto fail;
12663 }
12664 }
12665
12666 /* Disable all crtcs which require disable */
12667 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12668 ret = dm_update_crtc_state(&adev->dm, state, crtc,
12669 old_crtc_state,
12670 new_crtc_state,
12671 false,
12672 &lock_and_validation_needed);
12673 if (ret) {
12674 drm_dbg_atomic(dev, "DISABLE: dm_update_crtc_state() failed\n");
12675 goto fail;
12676 }
12677 }
12678
12679 /* Enable all crtcs which require enable */
12680 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12681 ret = dm_update_crtc_state(&adev->dm, state, crtc,
12682 old_crtc_state,
12683 new_crtc_state,
12684 true,
12685 &lock_and_validation_needed);
12686 if (ret) {
12687 drm_dbg_atomic(dev, "ENABLE: dm_update_crtc_state() failed\n");
12688 goto fail;
12689 }
12690 }
12691
12692 /* Add new/modified planes */
12693 for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, new_plane_state) {
12694 ret = dm_update_plane_state(dc, state, plane,
12695 old_plane_state,
12696 new_plane_state,
12697 true,
12698 &lock_and_validation_needed,
12699 &is_top_most_overlay);
12700 if (ret) {
12701 drm_dbg_atomic(dev, "dm_update_plane_state() failed\n");
12702 goto fail;
12703 }
12704 }
12705
12706 #if defined(CONFIG_DRM_AMD_DC_FP)
12707 if (dc_resource_is_dsc_encoding_supported(dc)) {
12708 ret = pre_validate_dsc(state, &dm_state, vars);
12709 if (ret != 0)
12710 goto fail;
12711 }
12712 #endif
12713
12714 /* Run this here since we want to validate the streams we created */
12715 ret = drm_atomic_helper_check_planes(dev, state);
12716 if (ret) {
12717 drm_dbg_atomic(dev, "drm_atomic_helper_check_planes() failed\n");
12718 goto fail;
12719 }
12720
12721 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12722 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12723 if (dm_new_crtc_state->mpo_requested)
12724 drm_dbg_atomic(dev, "MPO enablement requested on crtc:[%p]\n", crtc);
12725 }
12726
12727 /* Check cursor restrictions */
12728 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12729 enum amdgpu_dm_cursor_mode required_cursor_mode;
12730 int is_rotated, is_scaled;
12731
12732 /* Overlay cusor not subject to native cursor restrictions */
12733 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12734 if (dm_new_crtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE)
12735 continue;
12736
12737 /* Check if rotation or scaling is enabled on DCN401 */
12738 if ((drm_plane_mask(crtc->cursor) & new_crtc_state->plane_mask) &&
12739 amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 0, 1)) {
12740 new_cursor_state = drm_atomic_get_new_plane_state(state, crtc->cursor);
12741
12742 is_rotated = new_cursor_state &&
12743 ((new_cursor_state->rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0);
12744 is_scaled = new_cursor_state && ((new_cursor_state->src_w >> 16 != new_cursor_state->crtc_w) ||
12745 (new_cursor_state->src_h >> 16 != new_cursor_state->crtc_h));
12746
12747 if (is_rotated || is_scaled) {
12748 drm_dbg_driver(
12749 crtc->dev,
12750 "[CRTC:%d:%s] cannot enable hardware cursor due to rotation/scaling\n",
12751 crtc->base.id, crtc->name);
12752 ret = -EINVAL;
12753 goto fail;
12754 }
12755 }
12756
12757 /* If HW can only do native cursor, check restrictions again */
12758 ret = dm_crtc_get_cursor_mode(adev, state, dm_new_crtc_state,
12759 &required_cursor_mode);
12760 if (ret) {
12761 drm_dbg_driver(crtc->dev,
12762 "[CRTC:%d:%s] Checking cursor mode failed\n",
12763 crtc->base.id, crtc->name);
12764 goto fail;
12765 } else if (required_cursor_mode == DM_CURSOR_OVERLAY_MODE) {
12766 drm_dbg_driver(crtc->dev,
12767 "[CRTC:%d:%s] Cannot enable native cursor due to scaling or YUV restrictions\n",
12768 crtc->base.id, crtc->name);
12769 ret = -EINVAL;
12770 goto fail;
12771 }
12772 }
12773
12774 if (state->legacy_cursor_update) {
12775 /*
12776 * This is a fast cursor update coming from the plane update
12777 * helper, check if it can be done asynchronously for better
12778 * performance.
12779 */
12780 state->async_update =
12781 !drm_atomic_helper_async_check(dev, state);
12782
12783 /*
12784 * Skip the remaining global validation if this is an async
12785 * update. Cursor updates can be done without affecting
12786 * state or bandwidth calcs and this avoids the performance
12787 * penalty of locking the private state object and
12788 * allocating a new dc_state.
12789 */
12790 if (state->async_update)
12791 return 0;
12792 }
12793
12794 /* Check scaling and underscan changes*/
12795 /* TODO Removed scaling changes validation due to inability to commit
12796 * new stream into context w\o causing full reset. Need to
12797 * decide how to handle.
12798 */
12799 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
12800 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
12801 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
12802 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
12803
12804 /* Skip any modesets/resets */
12805 if (!acrtc || drm_atomic_crtc_needs_modeset(
12806 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
12807 continue;
12808
12809 /* Skip any thing not scale or underscan changes */
12810 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
12811 continue;
12812
12813 lock_and_validation_needed = true;
12814 }
12815
12816 /* set the slot info for each mst_state based on the link encoding format */
12817 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
12818 struct amdgpu_dm_connector *aconnector;
12819 struct drm_connector *connector;
12820 struct drm_connector_list_iter iter;
12821 u8 link_coding_cap;
12822
12823 drm_connector_list_iter_begin(dev, &iter);
12824 drm_for_each_connector_iter(connector, &iter) {
12825 if (connector->index == mst_state->mgr->conn_base_id) {
12826 aconnector = to_amdgpu_dm_connector(connector);
12827 link_coding_cap = dc_link_dp_mst_decide_link_encoding_format(aconnector->dc_link);
12828 drm_dp_mst_update_slots(mst_state, link_coding_cap);
12829
12830 break;
12831 }
12832 }
12833 drm_connector_list_iter_end(&iter);
12834 }
12835
12836 /**
12837 * Streams and planes are reset when there are changes that affect
12838 * bandwidth. Anything that affects bandwidth needs to go through
12839 * DC global validation to ensure that the configuration can be applied
12840 * to hardware.
12841 *
12842 * We have to currently stall out here in atomic_check for outstanding
12843 * commits to finish in this case because our IRQ handlers reference
12844 * DRM state directly - we can end up disabling interrupts too early
12845 * if we don't.
12846 *
12847 * TODO: Remove this stall and drop DM state private objects.
12848 */
12849 if (lock_and_validation_needed) {
12850 ret = dm_atomic_get_state(state, &dm_state);
12851 if (ret) {
12852 drm_dbg_atomic(dev, "dm_atomic_get_state() failed\n");
12853 goto fail;
12854 }
12855
12856 ret = do_aquire_global_lock(dev, state);
12857 if (ret) {
12858 drm_dbg_atomic(dev, "do_aquire_global_lock() failed\n");
12859 goto fail;
12860 }
12861
12862 #if defined(CONFIG_DRM_AMD_DC_FP)
12863 if (dc_resource_is_dsc_encoding_supported(dc)) {
12864 ret = compute_mst_dsc_configs_for_state(state, dm_state->context, vars);
12865 if (ret) {
12866 drm_dbg_atomic(dev, "MST_DSC compute_mst_dsc_configs_for_state() failed\n");
12867 ret = -EINVAL;
12868 goto fail;
12869 }
12870 }
12871 #endif
12872
12873 ret = dm_update_mst_vcpi_slots_for_dsc(state, dm_state->context, vars);
12874 if (ret) {
12875 drm_dbg_atomic(dev, "dm_update_mst_vcpi_slots_for_dsc() failed\n");
12876 goto fail;
12877 }
12878
12879 /*
12880 * Perform validation of MST topology in the state:
12881 * We need to perform MST atomic check before calling
12882 * dc_validate_global_state(), or there is a chance
12883 * to get stuck in an infinite loop and hang eventually.
12884 */
12885 ret = drm_dp_mst_atomic_check(state);
12886 if (ret) {
12887 drm_dbg_atomic(dev, "MST drm_dp_mst_atomic_check() failed\n");
12888 goto fail;
12889 }
12890 status = dc_validate_global_state(dc, dm_state->context, DC_VALIDATE_MODE_ONLY);
12891 if (status != DC_OK) {
12892 drm_dbg_atomic(dev, "DC global validation failure: %s (%d)",
12893 dc_status_to_str(status), status);
12894 ret = -EINVAL;
12895 goto fail;
12896 }
12897 } else {
12898 /*
12899 * The commit is a fast update. Fast updates shouldn't change
12900 * the DC context, affect global validation, and can have their
12901 * commit work done in parallel with other commits not touching
12902 * the same resource. If we have a new DC context as part of
12903 * the DM atomic state from validation we need to free it and
12904 * retain the existing one instead.
12905 *
12906 * Furthermore, since the DM atomic state only contains the DC
12907 * context and can safely be annulled, we can free the state
12908 * and clear the associated private object now to free
12909 * some memory and avoid a possible use-after-free later.
12910 */
12911
12912 for (i = 0; i < state->num_private_objs; i++) {
12913 struct drm_private_obj *obj = state->private_objs[i].ptr;
12914
12915 if (obj->funcs == adev->dm.atomic_obj.funcs) {
12916 int j = state->num_private_objs-1;
12917
12918 dm_atomic_destroy_state(obj,
12919 state->private_objs[i].state_to_destroy);
12920
12921 /* If i is not at the end of the array then the
12922 * last element needs to be moved to where i was
12923 * before the array can safely be truncated.
12924 */
12925 if (i != j)
12926 state->private_objs[i] =
12927 state->private_objs[j];
12928
12929 state->private_objs[j].ptr = NULL;
12930 state->private_objs[j].state_to_destroy = NULL;
12931 state->private_objs[j].old_state = NULL;
12932 state->private_objs[j].new_state = NULL;
12933
12934 state->num_private_objs = j;
12935 break;
12936 }
12937 }
12938 }
12939
12940 /* Store the overall update type for use later in atomic check. */
12941 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12942 struct dm_crtc_state *dm_new_crtc_state =
12943 to_dm_crtc_state(new_crtc_state);
12944
12945 /*
12946 * Only allow async flips for fast updates that don't change
12947 * the FB pitch, the DCC state, rotation, mem_type, etc.
12948 */
12949 if (new_crtc_state->async_flip &&
12950 (lock_and_validation_needed ||
12951 amdgpu_dm_crtc_mem_type_changed(dev, state, new_crtc_state))) {
12952 drm_dbg_atomic(crtc->dev,
12953 "[CRTC:%d:%s] async flips are only supported for fast updates\n",
12954 crtc->base.id, crtc->name);
12955 ret = -EINVAL;
12956 goto fail;
12957 }
12958
12959 dm_new_crtc_state->update_type = lock_and_validation_needed ?
12960 UPDATE_TYPE_FULL : UPDATE_TYPE_FAST;
12961 }
12962
12963 /* Must be success */
12964 WARN_ON(ret);
12965
12966 trace_amdgpu_dm_atomic_check_finish(state, ret);
12967
12968 return ret;
12969
12970 fail:
12971 if (ret == -EDEADLK)
12972 drm_dbg_atomic(dev, "Atomic check stopped to avoid deadlock.\n");
12973 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
12974 drm_dbg_atomic(dev, "Atomic check stopped due to signal.\n");
12975 else
12976 drm_dbg_atomic(dev, "Atomic check failed with err: %d\n", ret);
12977
12978 trace_amdgpu_dm_atomic_check_finish(state, ret);
12979
12980 return ret;
12981 }
12982
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)12983 static bool dm_edid_parser_send_cea(struct amdgpu_display_manager *dm,
12984 unsigned int offset,
12985 unsigned int total_length,
12986 u8 *data,
12987 unsigned int length,
12988 struct amdgpu_hdmi_vsdb_info *vsdb)
12989 {
12990 bool res;
12991 union dmub_rb_cmd cmd;
12992 struct dmub_cmd_send_edid_cea *input;
12993 struct dmub_cmd_edid_cea_output *output;
12994
12995 if (length > DMUB_EDID_CEA_DATA_CHUNK_BYTES)
12996 return false;
12997
12998 memset(&cmd, 0, sizeof(cmd));
12999
13000 input = &cmd.edid_cea.data.input;
13001
13002 cmd.edid_cea.header.type = DMUB_CMD__EDID_CEA;
13003 cmd.edid_cea.header.sub_type = 0;
13004 cmd.edid_cea.header.payload_bytes =
13005 sizeof(cmd.edid_cea) - sizeof(cmd.edid_cea.header);
13006 input->offset = offset;
13007 input->length = length;
13008 input->cea_total_length = total_length;
13009 memcpy(input->payload, data, length);
13010
13011 res = dc_wake_and_execute_dmub_cmd(dm->dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY);
13012 if (!res) {
13013 drm_err(adev_to_drm(dm->adev), "EDID CEA parser failed\n");
13014 return false;
13015 }
13016
13017 output = &cmd.edid_cea.data.output;
13018
13019 if (output->type == DMUB_CMD__EDID_CEA_ACK) {
13020 if (!output->ack.success) {
13021 drm_err(adev_to_drm(dm->adev), "EDID CEA ack failed at offset %d\n",
13022 output->ack.offset);
13023 }
13024 } else if (output->type == DMUB_CMD__EDID_CEA_AMD_VSDB) {
13025 if (!output->amd_vsdb.vsdb_found)
13026 return false;
13027
13028 vsdb->freesync_supported = output->amd_vsdb.freesync_supported;
13029 vsdb->amd_vsdb_version = output->amd_vsdb.amd_vsdb_version;
13030 vsdb->min_refresh_rate_hz = output->amd_vsdb.min_frame_rate;
13031 vsdb->max_refresh_rate_hz = output->amd_vsdb.max_frame_rate;
13032 } else {
13033 drm_warn(adev_to_drm(dm->adev), "Unknown EDID CEA parser results\n");
13034 return false;
13035 }
13036
13037 return true;
13038 }
13039
parse_edid_cea_dmcu(struct amdgpu_display_manager * dm,u8 * edid_ext,int len,struct amdgpu_hdmi_vsdb_info * vsdb_info)13040 static bool parse_edid_cea_dmcu(struct amdgpu_display_manager *dm,
13041 u8 *edid_ext, int len,
13042 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13043 {
13044 int i;
13045
13046 /* send extension block to DMCU for parsing */
13047 for (i = 0; i < len; i += 8) {
13048 bool res;
13049 int offset;
13050
13051 /* send 8 bytes a time */
13052 if (!dc_edid_parser_send_cea(dm->dc, i, len, &edid_ext[i], 8))
13053 return false;
13054
13055 if (i+8 == len) {
13056 /* EDID block sent completed, expect result */
13057 int version, min_rate, max_rate;
13058
13059 res = dc_edid_parser_recv_amd_vsdb(dm->dc, &version, &min_rate, &max_rate);
13060 if (res) {
13061 /* amd vsdb found */
13062 vsdb_info->freesync_supported = 1;
13063 vsdb_info->amd_vsdb_version = version;
13064 vsdb_info->min_refresh_rate_hz = min_rate;
13065 vsdb_info->max_refresh_rate_hz = max_rate;
13066 return true;
13067 }
13068 /* not amd vsdb */
13069 return false;
13070 }
13071
13072 /* check for ack*/
13073 res = dc_edid_parser_recv_cea_ack(dm->dc, &offset);
13074 if (!res)
13075 return false;
13076 }
13077
13078 return false;
13079 }
13080
parse_edid_cea_dmub(struct amdgpu_display_manager * dm,u8 * edid_ext,int len,struct amdgpu_hdmi_vsdb_info * vsdb_info)13081 static bool parse_edid_cea_dmub(struct amdgpu_display_manager *dm,
13082 u8 *edid_ext, int len,
13083 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13084 {
13085 int i;
13086
13087 /* send extension block to DMCU for parsing */
13088 for (i = 0; i < len; i += 8) {
13089 /* send 8 bytes a time */
13090 if (!dm_edid_parser_send_cea(dm, i, len, &edid_ext[i], 8, vsdb_info))
13091 return false;
13092 }
13093
13094 return vsdb_info->freesync_supported;
13095 }
13096
parse_edid_cea(struct amdgpu_dm_connector * aconnector,u8 * edid_ext,int len,struct amdgpu_hdmi_vsdb_info * vsdb_info)13097 static bool parse_edid_cea(struct amdgpu_dm_connector *aconnector,
13098 u8 *edid_ext, int len,
13099 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13100 {
13101 struct amdgpu_device *adev = drm_to_adev(aconnector->base.dev);
13102 bool ret;
13103
13104 mutex_lock(&adev->dm.dc_lock);
13105 if (adev->dm.dmub_srv)
13106 ret = parse_edid_cea_dmub(&adev->dm, edid_ext, len, vsdb_info);
13107 else
13108 ret = parse_edid_cea_dmcu(&adev->dm, edid_ext, len, vsdb_info);
13109 mutex_unlock(&adev->dm.dc_lock);
13110 return ret;
13111 }
13112
parse_edid_displayid_vrr(struct drm_connector * connector,const struct edid * edid)13113 static void parse_edid_displayid_vrr(struct drm_connector *connector,
13114 const struct edid *edid)
13115 {
13116 u8 *edid_ext = NULL;
13117 int i;
13118 int j = 0;
13119 u16 min_vfreq;
13120 u16 max_vfreq;
13121
13122 if (edid == NULL || edid->extensions == 0)
13123 return;
13124
13125 /* Find DisplayID extension */
13126 for (i = 0; i < edid->extensions; i++) {
13127 edid_ext = (void *)(edid + (i + 1));
13128 if (edid_ext[0] == DISPLAYID_EXT)
13129 break;
13130 }
13131
13132 if (edid_ext == NULL)
13133 return;
13134
13135 while (j < EDID_LENGTH) {
13136 /* Get dynamic video timing range from DisplayID if available */
13137 if (EDID_LENGTH - j > 13 && edid_ext[j] == 0x25 &&
13138 (edid_ext[j+1] & 0xFE) == 0 && (edid_ext[j+2] == 9)) {
13139 min_vfreq = edid_ext[j+9];
13140 if (edid_ext[j+1] & 7)
13141 max_vfreq = edid_ext[j+10] + ((edid_ext[j+11] & 3) << 8);
13142 else
13143 max_vfreq = edid_ext[j+10];
13144
13145 if (max_vfreq && min_vfreq) {
13146 connector->display_info.monitor_range.max_vfreq = max_vfreq;
13147 connector->display_info.monitor_range.min_vfreq = min_vfreq;
13148
13149 return;
13150 }
13151 }
13152 j++;
13153 }
13154 }
13155
parse_amd_vsdb(struct amdgpu_dm_connector * aconnector,const struct edid * edid,struct amdgpu_hdmi_vsdb_info * vsdb_info)13156 static int parse_amd_vsdb(struct amdgpu_dm_connector *aconnector,
13157 const struct edid *edid, struct amdgpu_hdmi_vsdb_info *vsdb_info)
13158 {
13159 u8 *edid_ext = NULL;
13160 int i;
13161 int j = 0;
13162 int total_ext_block_len;
13163
13164 if (edid == NULL || edid->extensions == 0)
13165 return -ENODEV;
13166
13167 /* Find DisplayID extension */
13168 for (i = 0; i < edid->extensions; i++) {
13169 edid_ext = (void *)(edid + (i + 1));
13170 if (edid_ext[0] == DISPLAYID_EXT)
13171 break;
13172 }
13173
13174 total_ext_block_len = EDID_LENGTH * edid->extensions;
13175 while (j < total_ext_block_len - sizeof(struct amd_vsdb_block)) {
13176 struct amd_vsdb_block *amd_vsdb = (struct amd_vsdb_block *)&edid_ext[j];
13177 unsigned int ieeeId = (amd_vsdb->ieee_id[2] << 16) | (amd_vsdb->ieee_id[1] << 8) | (amd_vsdb->ieee_id[0]);
13178
13179 if (ieeeId == HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_IEEE_REGISTRATION_ID &&
13180 amd_vsdb->version == HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_VERSION_3) {
13181 u8 panel_type;
13182 vsdb_info->replay_mode = (amd_vsdb->feature_caps & AMD_VSDB_VERSION_3_FEATURECAP_REPLAYMODE) ? true : false;
13183 vsdb_info->amd_vsdb_version = HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_VERSION_3;
13184 drm_dbg_kms(aconnector->base.dev, "Panel supports Replay Mode: %d\n", vsdb_info->replay_mode);
13185 panel_type = (amd_vsdb->color_space_eotf_support & AMD_VDSB_VERSION_3_PANEL_TYPE_MASK) >> AMD_VDSB_VERSION_3_PANEL_TYPE_SHIFT;
13186 switch (panel_type) {
13187 case AMD_VSDB_PANEL_TYPE_OLED:
13188 aconnector->dc_link->panel_type = PANEL_TYPE_OLED;
13189 break;
13190 case AMD_VSDB_PANEL_TYPE_MINILED:
13191 aconnector->dc_link->panel_type = PANEL_TYPE_MINILED;
13192 break;
13193 default:
13194 aconnector->dc_link->panel_type = PANEL_TYPE_NONE;
13195 break;
13196 }
13197 drm_dbg_kms(aconnector->base.dev, "Panel type: %d\n",
13198 aconnector->dc_link->panel_type);
13199
13200 return true;
13201 }
13202 j++;
13203 }
13204
13205 return false;
13206 }
13207
parse_hdmi_amd_vsdb(struct amdgpu_dm_connector * aconnector,const struct edid * edid,struct amdgpu_hdmi_vsdb_info * vsdb_info)13208 static int parse_hdmi_amd_vsdb(struct amdgpu_dm_connector *aconnector,
13209 const struct edid *edid,
13210 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13211 {
13212 u8 *edid_ext = NULL;
13213 int i;
13214 bool valid_vsdb_found = false;
13215
13216 /*----- drm_find_cea_extension() -----*/
13217 /* No EDID or EDID extensions */
13218 if (edid == NULL || edid->extensions == 0)
13219 return -ENODEV;
13220
13221 /* Find CEA extension */
13222 for (i = 0; i < edid->extensions; i++) {
13223 edid_ext = (uint8_t *)edid + EDID_LENGTH * (i + 1);
13224 if (edid_ext[0] == CEA_EXT)
13225 break;
13226 }
13227
13228 if (i == edid->extensions)
13229 return -ENODEV;
13230
13231 /*----- cea_db_offsets() -----*/
13232 if (edid_ext[0] != CEA_EXT)
13233 return -ENODEV;
13234
13235 valid_vsdb_found = parse_edid_cea(aconnector, edid_ext, EDID_LENGTH, vsdb_info);
13236
13237 return valid_vsdb_found ? i : -ENODEV;
13238 }
13239
13240 /**
13241 * amdgpu_dm_update_freesync_caps - Update Freesync capabilities
13242 *
13243 * @connector: Connector to query.
13244 * @drm_edid: DRM EDID from monitor
13245 *
13246 * Amdgpu supports Freesync in DP and HDMI displays, and it is required to keep
13247 * track of some of the display information in the internal data struct used by
13248 * amdgpu_dm. This function checks which type of connector we need to set the
13249 * FreeSync parameters.
13250 */
amdgpu_dm_update_freesync_caps(struct drm_connector * connector,const struct drm_edid * drm_edid)13251 void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
13252 const struct drm_edid *drm_edid)
13253 {
13254 int i = 0;
13255 struct amdgpu_dm_connector *amdgpu_dm_connector =
13256 to_amdgpu_dm_connector(connector);
13257 struct dm_connector_state *dm_con_state = NULL;
13258 struct dc_sink *sink;
13259 struct amdgpu_device *adev = drm_to_adev(connector->dev);
13260 struct amdgpu_hdmi_vsdb_info vsdb_info = {0};
13261 const struct edid *edid;
13262 bool freesync_capable = false;
13263 enum adaptive_sync_type as_type = ADAPTIVE_SYNC_TYPE_NONE;
13264
13265 if (!connector->state) {
13266 drm_err(adev_to_drm(adev), "%s - Connector has no state", __func__);
13267 goto update;
13268 }
13269
13270 sink = amdgpu_dm_connector->dc_sink ?
13271 amdgpu_dm_connector->dc_sink :
13272 amdgpu_dm_connector->dc_em_sink;
13273
13274 drm_edid_connector_update(connector, drm_edid);
13275
13276 if (!drm_edid || !sink) {
13277 dm_con_state = to_dm_connector_state(connector->state);
13278
13279 amdgpu_dm_connector->min_vfreq = 0;
13280 amdgpu_dm_connector->max_vfreq = 0;
13281 freesync_capable = false;
13282
13283 goto update;
13284 }
13285
13286 dm_con_state = to_dm_connector_state(connector->state);
13287
13288 if (!adev->dm.freesync_module || !dc_supports_vrr(sink->ctx->dce_version))
13289 goto update;
13290
13291 edid = drm_edid_raw(drm_edid); // FIXME: Get rid of drm_edid_raw()
13292
13293 /* Some eDP panels only have the refresh rate range info in DisplayID */
13294 if ((connector->display_info.monitor_range.min_vfreq == 0 ||
13295 connector->display_info.monitor_range.max_vfreq == 0))
13296 parse_edid_displayid_vrr(connector, edid);
13297
13298 if (edid && (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT ||
13299 sink->sink_signal == SIGNAL_TYPE_EDP)) {
13300 if (amdgpu_dm_connector->dc_link &&
13301 amdgpu_dm_connector->dc_link->dpcd_caps.allow_invalid_MSA_timing_param) {
13302 amdgpu_dm_connector->min_vfreq = connector->display_info.monitor_range.min_vfreq;
13303 amdgpu_dm_connector->max_vfreq = connector->display_info.monitor_range.max_vfreq;
13304 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
13305 freesync_capable = true;
13306 }
13307
13308 parse_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
13309
13310 if (vsdb_info.replay_mode) {
13311 amdgpu_dm_connector->vsdb_info.replay_mode = vsdb_info.replay_mode;
13312 amdgpu_dm_connector->vsdb_info.amd_vsdb_version = vsdb_info.amd_vsdb_version;
13313 amdgpu_dm_connector->as_type = ADAPTIVE_SYNC_TYPE_EDP;
13314 }
13315
13316 } else if (drm_edid && sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A) {
13317 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
13318 if (i >= 0 && vsdb_info.freesync_supported) {
13319 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz;
13320 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz;
13321 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
13322 freesync_capable = true;
13323
13324 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz;
13325 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz;
13326 }
13327 }
13328
13329 if (amdgpu_dm_connector->dc_link)
13330 as_type = dm_get_adaptive_sync_support_type(amdgpu_dm_connector->dc_link);
13331
13332 if (as_type == FREESYNC_TYPE_PCON_IN_WHITELIST) {
13333 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
13334 if (i >= 0 && vsdb_info.freesync_supported && vsdb_info.amd_vsdb_version > 0) {
13335
13336 amdgpu_dm_connector->pack_sdp_v1_3 = true;
13337 amdgpu_dm_connector->as_type = as_type;
13338 amdgpu_dm_connector->vsdb_info = vsdb_info;
13339
13340 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz;
13341 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz;
13342 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
13343 freesync_capable = true;
13344
13345 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz;
13346 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz;
13347 }
13348 }
13349
13350 update:
13351 if (dm_con_state)
13352 dm_con_state->freesync_capable = freesync_capable;
13353
13354 if (connector->state && amdgpu_dm_connector->dc_link && !freesync_capable &&
13355 amdgpu_dm_connector->dc_link->replay_settings.config.replay_supported) {
13356 amdgpu_dm_connector->dc_link->replay_settings.config.replay_supported = false;
13357 amdgpu_dm_connector->dc_link->replay_settings.replay_feature_enabled = false;
13358 }
13359
13360 if (connector->vrr_capable_property)
13361 drm_connector_set_vrr_capable_property(connector,
13362 freesync_capable);
13363 }
13364
amdgpu_dm_trigger_timing_sync(struct drm_device * dev)13365 void amdgpu_dm_trigger_timing_sync(struct drm_device *dev)
13366 {
13367 struct amdgpu_device *adev = drm_to_adev(dev);
13368 struct dc *dc = adev->dm.dc;
13369 int i;
13370
13371 mutex_lock(&adev->dm.dc_lock);
13372 if (dc->current_state) {
13373 for (i = 0; i < dc->current_state->stream_count; ++i)
13374 dc->current_state->streams[i]
13375 ->triggered_crtc_reset.enabled =
13376 adev->dm.force_timing_sync;
13377
13378 dm_enable_per_frame_crtc_master_sync(dc->current_state);
13379 dc_trigger_sync(dc, dc->current_state);
13380 }
13381 mutex_unlock(&adev->dm.dc_lock);
13382 }
13383
amdgpu_dm_exit_ips_for_hw_access(struct dc * dc)13384 static inline void amdgpu_dm_exit_ips_for_hw_access(struct dc *dc)
13385 {
13386 if (dc->ctx->dmub_srv && !dc->ctx->dmub_srv->idle_exit_counter)
13387 dc_exit_ips_for_hw_access(dc);
13388 }
13389
dm_write_reg_func(const struct dc_context * ctx,uint32_t address,u32 value,const char * func_name)13390 void dm_write_reg_func(const struct dc_context *ctx, uint32_t address,
13391 u32 value, const char *func_name)
13392 {
13393 #ifdef DM_CHECK_ADDR_0
13394 if (address == 0) {
13395 drm_err(adev_to_drm(ctx->driver_context),
13396 "invalid register write. address = 0");
13397 return;
13398 }
13399 #endif
13400
13401 amdgpu_dm_exit_ips_for_hw_access(ctx->dc);
13402 cgs_write_register(ctx->cgs_device, address, value);
13403 trace_amdgpu_dc_wreg(&ctx->perf_trace->write_count, address, value);
13404 }
13405
dm_read_reg_func(const struct dc_context * ctx,uint32_t address,const char * func_name)13406 uint32_t dm_read_reg_func(const struct dc_context *ctx, uint32_t address,
13407 const char *func_name)
13408 {
13409 u32 value;
13410 #ifdef DM_CHECK_ADDR_0
13411 if (address == 0) {
13412 drm_err(adev_to_drm(ctx->driver_context),
13413 "invalid register read; address = 0\n");
13414 return 0;
13415 }
13416 #endif
13417
13418 if (ctx->dmub_srv &&
13419 ctx->dmub_srv->reg_helper_offload.gather_in_progress &&
13420 !ctx->dmub_srv->reg_helper_offload.should_burst_write) {
13421 ASSERT(false);
13422 return 0;
13423 }
13424
13425 amdgpu_dm_exit_ips_for_hw_access(ctx->dc);
13426
13427 value = cgs_read_register(ctx->cgs_device, address);
13428
13429 trace_amdgpu_dc_rreg(&ctx->perf_trace->read_count, address, value);
13430
13431 return value;
13432 }
13433
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)13434 int amdgpu_dm_process_dmub_aux_transfer_sync(
13435 struct dc_context *ctx,
13436 unsigned int link_index,
13437 struct aux_payload *payload,
13438 enum aux_return_code_type *operation_result)
13439 {
13440 struct amdgpu_device *adev = ctx->driver_context;
13441 struct dmub_notification *p_notify = adev->dm.dmub_notify;
13442 int ret = -1;
13443
13444 mutex_lock(&adev->dm.dpia_aux_lock);
13445 if (!dc_process_dmub_aux_transfer_async(ctx->dc, link_index, payload)) {
13446 *operation_result = AUX_RET_ERROR_ENGINE_ACQUIRE;
13447 goto out;
13448 }
13449
13450 if (!wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ)) {
13451 drm_err(adev_to_drm(adev), "wait_for_completion_timeout timeout!");
13452 *operation_result = AUX_RET_ERROR_TIMEOUT;
13453 goto out;
13454 }
13455
13456 if (p_notify->result != AUX_RET_SUCCESS) {
13457 /*
13458 * Transient states before tunneling is enabled could
13459 * lead to this error. We can ignore this for now.
13460 */
13461 if (p_notify->result == AUX_RET_ERROR_PROTOCOL_ERROR) {
13462 drm_warn(adev_to_drm(adev), "DPIA AUX failed on 0x%x(%d), error %d\n",
13463 payload->address, payload->length,
13464 p_notify->result);
13465 }
13466 *operation_result = p_notify->result;
13467 goto out;
13468 }
13469
13470 payload->reply[0] = adev->dm.dmub_notify->aux_reply.command & 0xF;
13471 if (adev->dm.dmub_notify->aux_reply.command & 0xF0)
13472 /* The reply is stored in the top nibble of the command. */
13473 payload->reply[0] = (adev->dm.dmub_notify->aux_reply.command >> 4) & 0xF;
13474
13475 /*write req may receive a byte indicating partially written number as well*/
13476 if (p_notify->aux_reply.length)
13477 memcpy(payload->data, p_notify->aux_reply.data,
13478 p_notify->aux_reply.length);
13479
13480 /* success */
13481 ret = p_notify->aux_reply.length;
13482 *operation_result = p_notify->result;
13483 out:
13484 reinit_completion(&adev->dm.dmub_aux_transfer_done);
13485 mutex_unlock(&adev->dm.dpia_aux_lock);
13486 return ret;
13487 }
13488
abort_fused_io(struct dc_context * ctx,const struct dmub_cmd_fused_request * request)13489 static void abort_fused_io(
13490 struct dc_context *ctx,
13491 const struct dmub_cmd_fused_request *request
13492 )
13493 {
13494 union dmub_rb_cmd command = { 0 };
13495 struct dmub_rb_cmd_fused_io *io = &command.fused_io;
13496
13497 io->header.type = DMUB_CMD__FUSED_IO;
13498 io->header.sub_type = DMUB_CMD__FUSED_IO_ABORT;
13499 io->header.payload_bytes = sizeof(*io) - sizeof(io->header);
13500 io->request = *request;
13501 dm_execute_dmub_cmd(ctx, &command, DM_DMUB_WAIT_TYPE_NO_WAIT);
13502 }
13503
execute_fused_io(struct amdgpu_device * dev,struct dc_context * ctx,union dmub_rb_cmd * commands,uint8_t count,uint32_t timeout_us)13504 static bool execute_fused_io(
13505 struct amdgpu_device *dev,
13506 struct dc_context *ctx,
13507 union dmub_rb_cmd *commands,
13508 uint8_t count,
13509 uint32_t timeout_us
13510 )
13511 {
13512 const uint8_t ddc_line = commands[0].fused_io.request.u.aux.ddc_line;
13513
13514 if (ddc_line >= ARRAY_SIZE(dev->dm.fused_io))
13515 return false;
13516
13517 struct fused_io_sync *sync = &dev->dm.fused_io[ddc_line];
13518 struct dmub_rb_cmd_fused_io *first = &commands[0].fused_io;
13519 const bool result = dm_execute_dmub_cmd_list(ctx, count, commands, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY)
13520 && first->header.ret_status
13521 && first->request.status == FUSED_REQUEST_STATUS_SUCCESS;
13522
13523 if (!result)
13524 return false;
13525
13526 while (wait_for_completion_timeout(&sync->replied, usecs_to_jiffies(timeout_us))) {
13527 reinit_completion(&sync->replied);
13528
13529 struct dmub_cmd_fused_request *reply = (struct dmub_cmd_fused_request *) sync->reply_data;
13530
13531 static_assert(sizeof(*reply) <= sizeof(sync->reply_data), "Size mismatch");
13532
13533 if (reply->identifier == first->request.identifier) {
13534 first->request = *reply;
13535 return true;
13536 }
13537 }
13538
13539 reinit_completion(&sync->replied);
13540 first->request.status = FUSED_REQUEST_STATUS_TIMEOUT;
13541 abort_fused_io(ctx, &first->request);
13542 return false;
13543 }
13544
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)13545 bool amdgpu_dm_execute_fused_io(
13546 struct amdgpu_device *dev,
13547 struct dc_link *link,
13548 union dmub_rb_cmd *commands,
13549 uint8_t count,
13550 uint32_t timeout_us)
13551 {
13552 struct amdgpu_display_manager *dm = &dev->dm;
13553
13554 mutex_lock(&dm->dpia_aux_lock);
13555
13556 const bool result = execute_fused_io(dev, link->ctx, commands, count, timeout_us);
13557
13558 mutex_unlock(&dm->dpia_aux_lock);
13559 return result;
13560 }
13561
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)13562 int amdgpu_dm_process_dmub_set_config_sync(
13563 struct dc_context *ctx,
13564 unsigned int link_index,
13565 struct set_config_cmd_payload *payload,
13566 enum set_config_status *operation_result)
13567 {
13568 struct amdgpu_device *adev = ctx->driver_context;
13569 bool is_cmd_complete;
13570 int ret;
13571
13572 mutex_lock(&adev->dm.dpia_aux_lock);
13573 is_cmd_complete = dc_process_dmub_set_config_async(ctx->dc,
13574 link_index, payload, adev->dm.dmub_notify);
13575
13576 if (is_cmd_complete || wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ)) {
13577 ret = 0;
13578 *operation_result = adev->dm.dmub_notify->sc_status;
13579 } else {
13580 drm_err(adev_to_drm(adev), "wait_for_completion_timeout timeout!");
13581 ret = -1;
13582 *operation_result = SET_CONFIG_UNKNOWN_ERROR;
13583 }
13584
13585 if (!is_cmd_complete)
13586 reinit_completion(&adev->dm.dmub_aux_transfer_done);
13587 mutex_unlock(&adev->dm.dpia_aux_lock);
13588 return ret;
13589 }
13590
dm_execute_dmub_cmd(const struct dc_context * ctx,union dmub_rb_cmd * cmd,enum dm_dmub_wait_type wait_type)13591 bool dm_execute_dmub_cmd(const struct dc_context *ctx, union dmub_rb_cmd *cmd, enum dm_dmub_wait_type wait_type)
13592 {
13593 return dc_dmub_srv_cmd_run(ctx->dmub_srv, cmd, wait_type);
13594 }
13595
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)13596 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)
13597 {
13598 return dc_dmub_srv_cmd_run_list(ctx->dmub_srv, count, cmd, wait_type);
13599 }
13600
dm_acpi_process_phy_transition_interlock(const struct dc_context * ctx,struct dm_process_phy_transition_init_params process_phy_transition_init_params)13601 void dm_acpi_process_phy_transition_interlock(
13602 const struct dc_context *ctx,
13603 struct dm_process_phy_transition_init_params process_phy_transition_init_params)
13604 {
13605 // Not yet implemented
13606 }
13607