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