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