1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright 2015 Advanced Micro Devices, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: AMD
24 *
25 */
26
27 /* The caprices of the preprocessor require that this be declared right here */
28 #define CREATE_TRACE_POINTS
29
30 #include "dm_services_types.h"
31 #include "dc.h"
32 #include "link_enc_cfg.h"
33 #include "dc/inc/core_types.h"
34 #include "dal_asic_id.h"
35 #include "dmub/dmub_srv.h"
36 #include "dc/inc/hw/dmcu.h"
37 #include "dc/inc/hw/abm.h"
38 #include "dc/dc_dmub_srv.h"
39 #include "dc/dc_edid_parser.h"
40 #include "dc/dc_stat.h"
41 #include "dc/dc_state.h"
42 #include "amdgpu_dm_trace.h"
43 #include "link/protocols/link_dpcd.h"
44 #include "link_service_types.h"
45 #include "link/protocols/link_dp_capability.h"
46 #include "link/protocols/link_ddc.h"
47
48 #include "amdgpu.h"
49 #include "amdgpu_display.h"
50 #include "amdgpu_ucode.h"
51 #include "atom.h"
52 #include "amdgpu_dm.h"
53 #include "amdgpu_dm_plane.h"
54 #include "amdgpu_dm_crtc.h"
55 #include "amdgpu_dm_hdcp.h"
56 #include <drm/display/drm_hdcp_helper.h>
57 #include "amdgpu_dm_wb.h"
58 #include "amdgpu_atombios.h"
59
60 #include "amd_shared.h"
61 #include "amdgpu_dm_irq.h"
62 #include "dm_helpers.h"
63 #include "amdgpu_dm_mst_types.h"
64 #if defined(CONFIG_DEBUG_FS)
65 #include "amdgpu_dm_debugfs.h"
66 #endif
67 #include "amdgpu_dm_psr.h"
68 #include "amdgpu_dm_replay.h"
69
70 #include "ivsrcid/ivsrcid_vislands30.h"
71
72 #include <linux/backlight.h>
73 #include <linux/module.h>
74 #include <linux/moduleparam.h>
75 #include <linux/types.h>
76 #include <linux/pm_runtime.h>
77 #include <linux/pci.h>
78 #include <linux/power_supply.h>
79 #include <linux/firmware.h>
80 #include <linux/component.h>
81 #include <linux/sort.h>
82
83 #include <drm/drm_privacy_screen_consumer.h>
84 #include <drm/display/drm_dp_mst_helper.h>
85 #include <drm/display/drm_hdmi_helper.h>
86 #include <drm/drm_atomic.h>
87 #include <drm/drm_atomic_uapi.h>
88 #include <drm/drm_atomic_helper.h>
89 #include <drm/drm_blend.h>
90 #include <drm/drm_fixed.h>
91 #include <drm/drm_fourcc.h>
92 #include <drm/drm_edid.h>
93 #include <drm/drm_eld.h>
94 #include <drm/drm_utils.h>
95 #include <drm/drm_vblank.h>
96 #include <drm/drm_audio_component.h>
97 #include <drm/drm_gem_atomic_helper.h>
98
99 #include <media/cec-notifier.h>
100 #include <acpi/video.h>
101
102 #include "ivsrcid/dcn/irqsrcs_dcn_1_0.h"
103
104 #include "modules/inc/mod_freesync.h"
105 #include "modules/power/power_helpers.h"
106
107 static_assert(AMDGPU_DMUB_NOTIFICATION_MAX == DMUB_NOTIFICATION_MAX, "AMDGPU_DMUB_NOTIFICATION_MAX mismatch");
108
109 #define FIRMWARE_RENOIR_DMUB "amdgpu/renoir_dmcub.bin"
110 MODULE_FIRMWARE(FIRMWARE_RENOIR_DMUB);
111 #define FIRMWARE_SIENNA_CICHLID_DMUB "amdgpu/sienna_cichlid_dmcub.bin"
112 MODULE_FIRMWARE(FIRMWARE_SIENNA_CICHLID_DMUB);
113 #define FIRMWARE_NAVY_FLOUNDER_DMUB "amdgpu/navy_flounder_dmcub.bin"
114 MODULE_FIRMWARE(FIRMWARE_NAVY_FLOUNDER_DMUB);
115 #define FIRMWARE_GREEN_SARDINE_DMUB "amdgpu/green_sardine_dmcub.bin"
116 MODULE_FIRMWARE(FIRMWARE_GREEN_SARDINE_DMUB);
117 #define FIRMWARE_VANGOGH_DMUB "amdgpu/vangogh_dmcub.bin"
118 MODULE_FIRMWARE(FIRMWARE_VANGOGH_DMUB);
119 #define FIRMWARE_DIMGREY_CAVEFISH_DMUB "amdgpu/dimgrey_cavefish_dmcub.bin"
120 MODULE_FIRMWARE(FIRMWARE_DIMGREY_CAVEFISH_DMUB);
121 #define FIRMWARE_BEIGE_GOBY_DMUB "amdgpu/beige_goby_dmcub.bin"
122 MODULE_FIRMWARE(FIRMWARE_BEIGE_GOBY_DMUB);
123 #define FIRMWARE_YELLOW_CARP_DMUB "amdgpu/yellow_carp_dmcub.bin"
124 MODULE_FIRMWARE(FIRMWARE_YELLOW_CARP_DMUB);
125 #define FIRMWARE_DCN_314_DMUB "amdgpu/dcn_3_1_4_dmcub.bin"
126 MODULE_FIRMWARE(FIRMWARE_DCN_314_DMUB);
127 #define FIRMWARE_DCN_315_DMUB "amdgpu/dcn_3_1_5_dmcub.bin"
128 MODULE_FIRMWARE(FIRMWARE_DCN_315_DMUB);
129 #define FIRMWARE_DCN316_DMUB "amdgpu/dcn_3_1_6_dmcub.bin"
130 MODULE_FIRMWARE(FIRMWARE_DCN316_DMUB);
131
132 #define FIRMWARE_DCN_V3_2_0_DMCUB "amdgpu/dcn_3_2_0_dmcub.bin"
133 MODULE_FIRMWARE(FIRMWARE_DCN_V3_2_0_DMCUB);
134 #define FIRMWARE_DCN_V3_2_1_DMCUB "amdgpu/dcn_3_2_1_dmcub.bin"
135 MODULE_FIRMWARE(FIRMWARE_DCN_V3_2_1_DMCUB);
136
137 #define FIRMWARE_RAVEN_DMCU "amdgpu/raven_dmcu.bin"
138 MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU);
139
140 #define FIRMWARE_NAVI12_DMCU "amdgpu/navi12_dmcu.bin"
141 MODULE_FIRMWARE(FIRMWARE_NAVI12_DMCU);
142
143 #define FIRMWARE_DCN_35_DMUB "amdgpu/dcn_3_5_dmcub.bin"
144 MODULE_FIRMWARE(FIRMWARE_DCN_35_DMUB);
145
146 #define FIRMWARE_DCN_351_DMUB "amdgpu/dcn_3_5_1_dmcub.bin"
147 MODULE_FIRMWARE(FIRMWARE_DCN_351_DMUB);
148
149 #define FIRMWARE_DCN_36_DMUB "amdgpu/dcn_3_6_dmcub.bin"
150 MODULE_FIRMWARE(FIRMWARE_DCN_36_DMUB);
151
152 #define FIRMWARE_DCN_401_DMUB "amdgpu/dcn_4_0_1_dmcub.bin"
153 MODULE_FIRMWARE(FIRMWARE_DCN_401_DMUB);
154
155 /* Number of bytes in PSP header for firmware. */
156 #define PSP_HEADER_BYTES 0x100
157
158 /* Number of bytes in PSP footer for firmware. */
159 #define PSP_FOOTER_BYTES 0x100
160
161 /**
162 * DOC: overview
163 *
164 * The AMDgpu display manager, **amdgpu_dm** (or even simpler,
165 * **dm**) sits between DRM and DC. It acts as a liaison, converting DRM
166 * requests into DC requests, and DC responses into DRM responses.
167 *
168 * The root control structure is &struct amdgpu_display_manager.
169 */
170
171 /* basic init/fini API */
172 static int amdgpu_dm_init(struct amdgpu_device *adev);
173 static void amdgpu_dm_fini(struct amdgpu_device *adev);
174 static bool is_freesync_video_mode(const struct drm_display_mode *mode, struct amdgpu_dm_connector *aconnector);
175 static void reset_freesync_config_for_crtc(struct dm_crtc_state *new_crtc_state);
176 static struct amdgpu_i2c_adapter *
177 create_i2c(struct ddc_service *ddc_service, bool oem);
178
get_subconnector_type(struct dc_link * link)179 static enum drm_mode_subconnector get_subconnector_type(struct dc_link *link)
180 {
181 switch (link->dpcd_caps.dongle_type) {
182 case DISPLAY_DONGLE_NONE:
183 return DRM_MODE_SUBCONNECTOR_Native;
184 case DISPLAY_DONGLE_DP_VGA_CONVERTER:
185 return DRM_MODE_SUBCONNECTOR_VGA;
186 case DISPLAY_DONGLE_DP_DVI_CONVERTER:
187 case DISPLAY_DONGLE_DP_DVI_DONGLE:
188 return DRM_MODE_SUBCONNECTOR_DVID;
189 case DISPLAY_DONGLE_DP_HDMI_CONVERTER:
190 case DISPLAY_DONGLE_DP_HDMI_DONGLE:
191 return DRM_MODE_SUBCONNECTOR_HDMIA;
192 case DISPLAY_DONGLE_DP_HDMI_MISMATCHED_DONGLE:
193 default:
194 return DRM_MODE_SUBCONNECTOR_Unknown;
195 }
196 }
197
update_subconnector_property(struct amdgpu_dm_connector * aconnector)198 static void update_subconnector_property(struct amdgpu_dm_connector *aconnector)
199 {
200 struct dc_link *link = aconnector->dc_link;
201 struct drm_connector *connector = &aconnector->base;
202 enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
203
204 if (connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort)
205 return;
206
207 if (aconnector->dc_sink)
208 subconnector = get_subconnector_type(link);
209
210 drm_object_property_set_value(&connector->base,
211 connector->dev->mode_config.dp_subconnector_property,
212 subconnector);
213 }
214
215 /*
216 * initializes drm_device display related structures, based on the information
217 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
218 * drm_encoder, drm_mode_config
219 *
220 * Returns 0 on success
221 */
222 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
223 /* removes and deallocates the drm structures, created by the above function */
224 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
225
226 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
227 struct amdgpu_dm_connector *amdgpu_dm_connector,
228 u32 link_index,
229 struct amdgpu_encoder *amdgpu_encoder);
230 static int amdgpu_dm_encoder_init(struct drm_device *dev,
231 struct amdgpu_encoder *aencoder,
232 uint32_t link_index);
233
234 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
235
236 static int amdgpu_dm_atomic_setup_commit(struct drm_atomic_state *state);
237 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
238
239 static int amdgpu_dm_atomic_check(struct drm_device *dev,
240 struct drm_atomic_state *state);
241
242 static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector);
243 static void handle_hpd_rx_irq(void *param);
244
245 static void amdgpu_dm_backlight_set_level(struct amdgpu_display_manager *dm,
246 int bl_idx,
247 u32 user_brightness);
248
249 static bool
250 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
251 struct drm_crtc_state *new_crtc_state);
252 /*
253 * dm_vblank_get_counter
254 *
255 * @brief
256 * Get counter for number of vertical blanks
257 *
258 * @param
259 * struct amdgpu_device *adev - [in] desired amdgpu device
260 * int disp_idx - [in] which CRTC to get the counter from
261 *
262 * @return
263 * Counter for vertical blanks
264 */
dm_vblank_get_counter(struct amdgpu_device * adev,int crtc)265 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
266 {
267 struct amdgpu_crtc *acrtc = NULL;
268
269 if (crtc >= adev->mode_info.num_crtc)
270 return 0;
271
272 acrtc = adev->mode_info.crtcs[crtc];
273
274 if (!acrtc->dm_irq_params.stream) {
275 drm_err(adev_to_drm(adev), "dc_stream_state is NULL for crtc '%d'!\n",
276 crtc);
277 return 0;
278 }
279
280 return dc_stream_get_vblank_counter(acrtc->dm_irq_params.stream);
281 }
282
dm_crtc_get_scanoutpos(struct amdgpu_device * adev,int crtc,u32 * vbl,u32 * position)283 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
284 u32 *vbl, u32 *position)
285 {
286 u32 v_blank_start = 0, v_blank_end = 0, h_position = 0, v_position = 0;
287 struct amdgpu_crtc *acrtc = NULL;
288 struct dc *dc = adev->dm.dc;
289
290 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
291 return -EINVAL;
292
293 acrtc = adev->mode_info.crtcs[crtc];
294
295 if (!acrtc->dm_irq_params.stream) {
296 drm_err(adev_to_drm(adev), "dc_stream_state is NULL for crtc '%d'!\n",
297 crtc);
298 return 0;
299 }
300
301 if (dc && dc->caps.ips_support && dc->idle_optimizations_allowed)
302 dc_allow_idle_optimizations(dc, false);
303
304 /*
305 * TODO rework base driver to use values directly.
306 * for now parse it back into reg-format
307 */
308 dc_stream_get_scanoutpos(acrtc->dm_irq_params.stream,
309 &v_blank_start,
310 &v_blank_end,
311 &h_position,
312 &v_position);
313
314 *position = v_position | (h_position << 16);
315 *vbl = v_blank_start | (v_blank_end << 16);
316
317 return 0;
318 }
319
dm_is_idle(struct amdgpu_ip_block * ip_block)320 static bool dm_is_idle(struct amdgpu_ip_block *ip_block)
321 {
322 /* XXX todo */
323 return true;
324 }
325
dm_wait_for_idle(struct amdgpu_ip_block * ip_block)326 static int dm_wait_for_idle(struct amdgpu_ip_block *ip_block)
327 {
328 /* XXX todo */
329 return 0;
330 }
331
dm_check_soft_reset(struct amdgpu_ip_block * ip_block)332 static bool dm_check_soft_reset(struct amdgpu_ip_block *ip_block)
333 {
334 return false;
335 }
336
dm_soft_reset(struct amdgpu_ip_block * ip_block)337 static int dm_soft_reset(struct amdgpu_ip_block *ip_block)
338 {
339 /* XXX todo */
340 return 0;
341 }
342
343 static struct amdgpu_crtc *
get_crtc_by_otg_inst(struct amdgpu_device * adev,int otg_inst)344 get_crtc_by_otg_inst(struct amdgpu_device *adev,
345 int otg_inst)
346 {
347 struct drm_device *dev = adev_to_drm(adev);
348 struct drm_crtc *crtc;
349 struct amdgpu_crtc *amdgpu_crtc;
350
351 if (WARN_ON(otg_inst == -1))
352 return adev->mode_info.crtcs[0];
353
354 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
355 amdgpu_crtc = to_amdgpu_crtc(crtc);
356
357 if (amdgpu_crtc->otg_inst == otg_inst)
358 return amdgpu_crtc;
359 }
360
361 return NULL;
362 }
363
is_dc_timing_adjust_needed(struct dm_crtc_state * old_state,struct dm_crtc_state * new_state)364 static inline bool is_dc_timing_adjust_needed(struct dm_crtc_state *old_state,
365 struct dm_crtc_state *new_state)
366 {
367 if (new_state->stream->adjust.timing_adjust_pending)
368 return true;
369 if (new_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)
370 return true;
371 else if (amdgpu_dm_crtc_vrr_active(old_state) != amdgpu_dm_crtc_vrr_active(new_state))
372 return true;
373 else
374 return false;
375 }
376
377 /*
378 * DC will program planes with their z-order determined by their ordering
379 * in the dc_surface_updates array. This comparator is used to sort them
380 * by descending zpos.
381 */
dm_plane_layer_index_cmp(const void * a,const void * b)382 static int dm_plane_layer_index_cmp(const void *a, const void *b)
383 {
384 const struct dc_surface_update *sa = (struct dc_surface_update *)a;
385 const struct dc_surface_update *sb = (struct dc_surface_update *)b;
386
387 /* Sort by descending dc_plane layer_index (i.e. normalized_zpos) */
388 return sb->surface->layer_index - sa->surface->layer_index;
389 }
390
391 /**
392 * update_planes_and_stream_adapter() - Send planes to be updated in DC
393 *
394 * DC has a generic way to update planes and stream via
395 * dc_update_planes_and_stream function; however, DM might need some
396 * adjustments and preparation before calling it. This function is a wrapper
397 * for the dc_update_planes_and_stream that does any required configuration
398 * before passing control to DC.
399 *
400 * @dc: Display Core control structure
401 * @update_type: specify whether it is FULL/MEDIUM/FAST update
402 * @planes_count: planes count to update
403 * @stream: stream state
404 * @stream_update: stream update
405 * @array_of_surface_update: dc surface update pointer
406 *
407 */
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)408 static inline bool update_planes_and_stream_adapter(struct dc *dc,
409 int update_type,
410 int planes_count,
411 struct dc_stream_state *stream,
412 struct dc_stream_update *stream_update,
413 struct dc_surface_update *array_of_surface_update)
414 {
415 sort(array_of_surface_update, planes_count,
416 sizeof(*array_of_surface_update), dm_plane_layer_index_cmp, NULL);
417
418 /*
419 * Previous frame finished and HW is ready for optimization.
420 */
421 dc_post_update_surfaces_to_stream(dc);
422
423 return dc_update_planes_and_stream(dc,
424 array_of_surface_update,
425 planes_count,
426 stream,
427 stream_update);
428 }
429
430 /**
431 * dm_pflip_high_irq() - Handle pageflip interrupt
432 * @interrupt_params: ignored
433 *
434 * Handles the pageflip interrupt by notifying all interested parties
435 * that the pageflip has been completed.
436 */
dm_pflip_high_irq(void * interrupt_params)437 static void dm_pflip_high_irq(void *interrupt_params)
438 {
439 struct amdgpu_crtc *amdgpu_crtc;
440 struct common_irq_params *irq_params = interrupt_params;
441 struct amdgpu_device *adev = irq_params->adev;
442 struct drm_device *dev = adev_to_drm(adev);
443 unsigned long flags;
444 struct drm_pending_vblank_event *e;
445 u32 vpos, hpos, v_blank_start, v_blank_end;
446 bool vrr_active;
447
448 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
449
450 /* IRQ could occur when in initial stage */
451 /* TODO work and BO cleanup */
452 if (amdgpu_crtc == NULL) {
453 drm_dbg_state(dev, "CRTC is null, returning.\n");
454 return;
455 }
456
457 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
458
459 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
460 drm_dbg_state(dev,
461 "amdgpu_crtc->pflip_status = %d != AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p]\n",
462 amdgpu_crtc->pflip_status, AMDGPU_FLIP_SUBMITTED,
463 amdgpu_crtc->crtc_id, amdgpu_crtc);
464 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
465 return;
466 }
467
468 /* page flip completed. */
469 e = amdgpu_crtc->event;
470 amdgpu_crtc->event = NULL;
471
472 WARN_ON(!e);
473
474 vrr_active = amdgpu_dm_crtc_vrr_active_irq(amdgpu_crtc);
475
476 /* Fixed refresh rate, or VRR scanout position outside front-porch? */
477 if (!vrr_active ||
478 !dc_stream_get_scanoutpos(amdgpu_crtc->dm_irq_params.stream, &v_blank_start,
479 &v_blank_end, &hpos, &vpos) ||
480 (vpos < v_blank_start)) {
481 /* Update to correct count and vblank timestamp if racing with
482 * vblank irq. This also updates to the correct vblank timestamp
483 * even in VRR mode, as scanout is past the front-porch atm.
484 */
485 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
486
487 /* Wake up userspace by sending the pageflip event with proper
488 * count and timestamp of vblank of flip completion.
489 */
490 if (e) {
491 drm_crtc_send_vblank_event(&amdgpu_crtc->base, e);
492
493 /* Event sent, so done with vblank for this flip */
494 drm_crtc_vblank_put(&amdgpu_crtc->base);
495 }
496 } else if (e) {
497 /* VRR active and inside front-porch: vblank count and
498 * timestamp for pageflip event will only be up to date after
499 * drm_crtc_handle_vblank() has been executed from late vblank
500 * irq handler after start of back-porch (vline 0). We queue the
501 * pageflip event for send-out by drm_crtc_handle_vblank() with
502 * updated timestamp and count, once it runs after us.
503 *
504 * We need to open-code this instead of using the helper
505 * drm_crtc_arm_vblank_event(), as that helper would
506 * call drm_crtc_accurate_vblank_count(), which we must
507 * not call in VRR mode while we are in front-porch!
508 */
509
510 /* sequence will be replaced by real count during send-out. */
511 e->sequence = drm_crtc_vblank_count(&amdgpu_crtc->base);
512 e->pipe = amdgpu_crtc->crtc_id;
513
514 list_add_tail(&e->base.link, &adev_to_drm(adev)->vblank_event_list);
515 e = NULL;
516 }
517
518 /* Keep track of vblank of this flip for flip throttling. We use the
519 * cooked hw counter, as that one incremented at start of this vblank
520 * of pageflip completion, so last_flip_vblank is the forbidden count
521 * for queueing new pageflips if vsync + VRR is enabled.
522 */
523 amdgpu_crtc->dm_irq_params.last_flip_vblank =
524 amdgpu_get_vblank_counter_kms(&amdgpu_crtc->base);
525
526 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
527 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
528
529 drm_dbg_state(dev,
530 "crtc:%d[%p], pflip_stat:AMDGPU_FLIP_NONE, vrr[%d]-fp %d\n",
531 amdgpu_crtc->crtc_id, amdgpu_crtc, vrr_active, (int)!e);
532 }
533
dm_handle_vmin_vmax_update(struct work_struct * offload_work)534 static void dm_handle_vmin_vmax_update(struct work_struct *offload_work)
535 {
536 struct vupdate_offload_work *work = container_of(offload_work, struct vupdate_offload_work, work);
537 struct amdgpu_device *adev = work->adev;
538 struct dc_stream_state *stream = work->stream;
539 struct dc_crtc_timing_adjust *adjust = work->adjust;
540
541 mutex_lock(&adev->dm.dc_lock);
542 dc_stream_adjust_vmin_vmax(adev->dm.dc, stream, adjust);
543 mutex_unlock(&adev->dm.dc_lock);
544
545 dc_stream_release(stream);
546 kfree(work->adjust);
547 kfree(work);
548 }
549
schedule_dc_vmin_vmax(struct amdgpu_device * adev,struct dc_stream_state * stream,struct dc_crtc_timing_adjust * adjust)550 static void schedule_dc_vmin_vmax(struct amdgpu_device *adev,
551 struct dc_stream_state *stream,
552 struct dc_crtc_timing_adjust *adjust)
553 {
554 struct vupdate_offload_work *offload_work = kzalloc(sizeof(*offload_work), GFP_NOWAIT);
555 if (!offload_work) {
556 drm_dbg_driver(adev_to_drm(adev), "Failed to allocate vupdate_offload_work\n");
557 return;
558 }
559
560 struct dc_crtc_timing_adjust *adjust_copy = kzalloc(sizeof(*adjust_copy), GFP_NOWAIT);
561 if (!adjust_copy) {
562 drm_dbg_driver(adev_to_drm(adev), "Failed to allocate adjust_copy\n");
563 kfree(offload_work);
564 return;
565 }
566
567 dc_stream_retain(stream);
568 memcpy(adjust_copy, adjust, sizeof(*adjust_copy));
569
570 INIT_WORK(&offload_work->work, dm_handle_vmin_vmax_update);
571 offload_work->adev = adev;
572 offload_work->stream = stream;
573 offload_work->adjust = adjust_copy;
574
575 queue_work(system_wq, &offload_work->work);
576 }
577
dm_vupdate_high_irq(void * interrupt_params)578 static void dm_vupdate_high_irq(void *interrupt_params)
579 {
580 struct common_irq_params *irq_params = interrupt_params;
581 struct amdgpu_device *adev = irq_params->adev;
582 struct amdgpu_crtc *acrtc;
583 struct drm_device *drm_dev;
584 struct drm_vblank_crtc *vblank;
585 ktime_t frame_duration_ns, previous_timestamp;
586 unsigned long flags;
587 int vrr_active;
588
589 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VUPDATE);
590
591 if (acrtc) {
592 vrr_active = amdgpu_dm_crtc_vrr_active_irq(acrtc);
593 drm_dev = acrtc->base.dev;
594 vblank = drm_crtc_vblank_crtc(&acrtc->base);
595 previous_timestamp = atomic64_read(&irq_params->previous_timestamp);
596 frame_duration_ns = vblank->time - previous_timestamp;
597
598 if (frame_duration_ns > 0) {
599 trace_amdgpu_refresh_rate_track(acrtc->base.index,
600 frame_duration_ns,
601 ktime_divns(NSEC_PER_SEC, frame_duration_ns));
602 atomic64_set(&irq_params->previous_timestamp, vblank->time);
603 }
604
605 drm_dbg_vbl(drm_dev,
606 "crtc:%d, vupdate-vrr:%d\n", acrtc->crtc_id,
607 vrr_active);
608
609 /* Core vblank handling is done here after end of front-porch in
610 * vrr mode, as vblank timestamping will give valid results
611 * while now done after front-porch. This will also deliver
612 * page-flip completion events that have been queued to us
613 * if a pageflip happened inside front-porch.
614 */
615 if (vrr_active && acrtc->dm_irq_params.stream) {
616 bool replay_en = acrtc->dm_irq_params.stream->link->replay_settings.replay_feature_enabled;
617 bool psr_en = acrtc->dm_irq_params.stream->link->psr_settings.psr_feature_enabled;
618 bool fs_active_var_en = acrtc->dm_irq_params.freesync_config.state
619 == VRR_STATE_ACTIVE_VARIABLE;
620
621 amdgpu_dm_crtc_handle_vblank(acrtc);
622
623 /* BTR processing for pre-DCE12 ASICs */
624 if (adev->family < AMDGPU_FAMILY_AI) {
625 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
626 mod_freesync_handle_v_update(
627 adev->dm.freesync_module,
628 acrtc->dm_irq_params.stream,
629 &acrtc->dm_irq_params.vrr_params);
630
631 if (fs_active_var_en || (!fs_active_var_en && !replay_en && !psr_en)) {
632 schedule_dc_vmin_vmax(adev,
633 acrtc->dm_irq_params.stream,
634 &acrtc->dm_irq_params.vrr_params.adjust);
635 }
636 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
637 }
638 }
639 }
640 }
641
642 /**
643 * dm_crtc_high_irq() - Handles CRTC interrupt
644 * @interrupt_params: used for determining the CRTC instance
645 *
646 * Handles the CRTC/VSYNC interrupt by notfying DRM's VBLANK
647 * event handler.
648 */
dm_crtc_high_irq(void * interrupt_params)649 static void dm_crtc_high_irq(void *interrupt_params)
650 {
651 struct common_irq_params *irq_params = interrupt_params;
652 struct amdgpu_device *adev = irq_params->adev;
653 struct drm_writeback_job *job;
654 struct amdgpu_crtc *acrtc;
655 unsigned long flags;
656 int vrr_active;
657
658 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
659 if (!acrtc)
660 return;
661
662 if (acrtc->wb_conn) {
663 spin_lock_irqsave(&acrtc->wb_conn->job_lock, flags);
664
665 if (acrtc->wb_pending) {
666 job = list_first_entry_or_null(&acrtc->wb_conn->job_queue,
667 struct drm_writeback_job,
668 list_entry);
669 acrtc->wb_pending = false;
670 spin_unlock_irqrestore(&acrtc->wb_conn->job_lock, flags);
671
672 if (job) {
673 unsigned int v_total, refresh_hz;
674 struct dc_stream_state *stream = acrtc->dm_irq_params.stream;
675
676 v_total = stream->adjust.v_total_max ?
677 stream->adjust.v_total_max : stream->timing.v_total;
678 refresh_hz = div_u64((uint64_t) stream->timing.pix_clk_100hz *
679 100LL, (v_total * stream->timing.h_total));
680 mdelay(1000 / refresh_hz);
681
682 drm_writeback_signal_completion(acrtc->wb_conn, 0);
683 dc_stream_fc_disable_writeback(adev->dm.dc,
684 acrtc->dm_irq_params.stream, 0);
685 }
686 } else
687 spin_unlock_irqrestore(&acrtc->wb_conn->job_lock, flags);
688 }
689
690 vrr_active = amdgpu_dm_crtc_vrr_active_irq(acrtc);
691
692 drm_dbg_vbl(adev_to_drm(adev),
693 "crtc:%d, vupdate-vrr:%d, planes:%d\n", acrtc->crtc_id,
694 vrr_active, acrtc->dm_irq_params.active_planes);
695
696 /**
697 * Core vblank handling at start of front-porch is only possible
698 * in non-vrr mode, as only there vblank timestamping will give
699 * valid results while done in front-porch. Otherwise defer it
700 * to dm_vupdate_high_irq after end of front-porch.
701 */
702 if (!vrr_active)
703 amdgpu_dm_crtc_handle_vblank(acrtc);
704
705 /**
706 * Following stuff must happen at start of vblank, for crc
707 * computation and below-the-range btr support in vrr mode.
708 */
709 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
710
711 /* BTR updates need to happen before VUPDATE on Vega and above. */
712 if (adev->family < AMDGPU_FAMILY_AI)
713 return;
714
715 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
716
717 if (acrtc->dm_irq_params.stream &&
718 acrtc->dm_irq_params.vrr_params.supported) {
719 bool replay_en = acrtc->dm_irq_params.stream->link->replay_settings.replay_feature_enabled;
720 bool psr_en = acrtc->dm_irq_params.stream->link->psr_settings.psr_feature_enabled;
721 bool fs_active_var_en = acrtc->dm_irq_params.freesync_config.state == VRR_STATE_ACTIVE_VARIABLE;
722
723 mod_freesync_handle_v_update(adev->dm.freesync_module,
724 acrtc->dm_irq_params.stream,
725 &acrtc->dm_irq_params.vrr_params);
726
727 /* update vmin_vmax only if freesync is enabled, or only if PSR and REPLAY are disabled */
728 if (fs_active_var_en || (!fs_active_var_en && !replay_en && !psr_en)) {
729 schedule_dc_vmin_vmax(adev, acrtc->dm_irq_params.stream,
730 &acrtc->dm_irq_params.vrr_params.adjust);
731 }
732 }
733
734 /*
735 * If there aren't any active_planes then DCH HUBP may be clock-gated.
736 * In that case, pageflip completion interrupts won't fire and pageflip
737 * completion events won't get delivered. Prevent this by sending
738 * pending pageflip events from here if a flip is still pending.
739 *
740 * If any planes are enabled, use dm_pflip_high_irq() instead, to
741 * avoid race conditions between flip programming and completion,
742 * which could cause too early flip completion events.
743 */
744 if (adev->family >= AMDGPU_FAMILY_RV &&
745 acrtc->pflip_status == AMDGPU_FLIP_SUBMITTED &&
746 acrtc->dm_irq_params.active_planes == 0) {
747 if (acrtc->event) {
748 drm_crtc_send_vblank_event(&acrtc->base, acrtc->event);
749 acrtc->event = NULL;
750 drm_crtc_vblank_put(&acrtc->base);
751 }
752 acrtc->pflip_status = AMDGPU_FLIP_NONE;
753 }
754
755 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
756 }
757
758 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
759 /**
760 * dm_dcn_vertical_interrupt0_high_irq() - Handles OTG Vertical interrupt0 for
761 * DCN generation ASICs
762 * @interrupt_params: interrupt parameters
763 *
764 * Used to set crc window/read out crc value at vertical line 0 position
765 */
dm_dcn_vertical_interrupt0_high_irq(void * interrupt_params)766 static void dm_dcn_vertical_interrupt0_high_irq(void *interrupt_params)
767 {
768 struct common_irq_params *irq_params = interrupt_params;
769 struct amdgpu_device *adev = irq_params->adev;
770 struct amdgpu_crtc *acrtc;
771
772 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VLINE0);
773
774 if (!acrtc)
775 return;
776
777 amdgpu_dm_crtc_handle_crc_window_irq(&acrtc->base);
778 }
779 #endif /* CONFIG_DRM_AMD_SECURE_DISPLAY */
780
781 /**
782 * dmub_aux_setconfig_callback - Callback for AUX or SET_CONFIG command.
783 * @adev: amdgpu_device pointer
784 * @notify: dmub notification structure
785 *
786 * Dmub AUX or SET_CONFIG command completion processing callback
787 * Copies dmub notification to DM which is to be read by AUX command.
788 * issuing thread and also signals the event to wake up the thread.
789 */
dmub_aux_setconfig_callback(struct amdgpu_device * adev,struct dmub_notification * notify)790 static void dmub_aux_setconfig_callback(struct amdgpu_device *adev,
791 struct dmub_notification *notify)
792 {
793 if (adev->dm.dmub_notify)
794 memcpy(adev->dm.dmub_notify, notify, sizeof(struct dmub_notification));
795 if (notify->type == DMUB_NOTIFICATION_AUX_REPLY)
796 complete(&adev->dm.dmub_aux_transfer_done);
797 }
798
dmub_aux_fused_io_callback(struct amdgpu_device * adev,struct dmub_notification * notify)799 static void dmub_aux_fused_io_callback(struct amdgpu_device *adev,
800 struct dmub_notification *notify)
801 {
802 if (!adev || !notify) {
803 ASSERT(false);
804 return;
805 }
806
807 const struct dmub_cmd_fused_request *req = ¬ify->fused_request;
808 const uint8_t ddc_line = req->u.aux.ddc_line;
809
810 if (ddc_line >= ARRAY_SIZE(adev->dm.fused_io)) {
811 ASSERT(false);
812 return;
813 }
814
815 struct fused_io_sync *sync = &adev->dm.fused_io[ddc_line];
816
817 static_assert(sizeof(*req) <= sizeof(sync->reply_data), "Size mismatch");
818 memcpy(sync->reply_data, req, sizeof(*req));
819 complete(&sync->replied);
820 }
821
822 /**
823 * dmub_hpd_callback - DMUB HPD interrupt processing callback.
824 * @adev: amdgpu_device pointer
825 * @notify: dmub notification structure
826 *
827 * Dmub Hpd interrupt processing callback. Gets displayindex through the
828 * ink index and calls helper to do the processing.
829 */
dmub_hpd_callback(struct amdgpu_device * adev,struct dmub_notification * notify)830 static void dmub_hpd_callback(struct amdgpu_device *adev,
831 struct dmub_notification *notify)
832 {
833 struct amdgpu_dm_connector *aconnector;
834 struct amdgpu_dm_connector *hpd_aconnector = NULL;
835 struct drm_connector *connector;
836 struct drm_connector_list_iter iter;
837 struct dc_link *link;
838 u8 link_index = 0;
839 struct drm_device *dev;
840
841 if (adev == NULL)
842 return;
843
844 if (notify == NULL) {
845 drm_err(adev_to_drm(adev), "DMUB HPD callback notification was NULL");
846 return;
847 }
848
849 if (notify->link_index > adev->dm.dc->link_count) {
850 drm_err(adev_to_drm(adev), "DMUB HPD index (%u)is abnormal", notify->link_index);
851 return;
852 }
853
854 /* Skip DMUB HPD IRQ in suspend/resume. We will probe them later. */
855 if (notify->type == DMUB_NOTIFICATION_HPD && adev->in_suspend) {
856 drm_info(adev_to_drm(adev), "Skip DMUB HPD IRQ callback in suspend/resume\n");
857 return;
858 }
859
860 link_index = notify->link_index;
861 link = adev->dm.dc->links[link_index];
862 dev = adev->dm.ddev;
863
864 drm_connector_list_iter_begin(dev, &iter);
865 drm_for_each_connector_iter(connector, &iter) {
866
867 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
868 continue;
869
870 aconnector = to_amdgpu_dm_connector(connector);
871 if (link && aconnector->dc_link == link) {
872 if (notify->type == DMUB_NOTIFICATION_HPD)
873 drm_info(adev_to_drm(adev), "DMUB HPD IRQ callback: link_index=%u\n", link_index);
874 else if (notify->type == DMUB_NOTIFICATION_HPD_IRQ)
875 drm_info(adev_to_drm(adev), "DMUB HPD RX IRQ callback: link_index=%u\n", link_index);
876 else
877 drm_warn(adev_to_drm(adev), "DMUB Unknown HPD callback type %d, link_index=%u\n",
878 notify->type, link_index);
879
880 hpd_aconnector = aconnector;
881 break;
882 }
883 }
884 drm_connector_list_iter_end(&iter);
885
886 if (hpd_aconnector) {
887 if (notify->type == DMUB_NOTIFICATION_HPD) {
888 if (hpd_aconnector->dc_link->hpd_status == (notify->hpd_status == DP_HPD_PLUG))
889 drm_warn(adev_to_drm(adev), "DMUB reported hpd status unchanged. link_index=%u\n", link_index);
890 handle_hpd_irq_helper(hpd_aconnector);
891 } else if (notify->type == DMUB_NOTIFICATION_HPD_IRQ) {
892 handle_hpd_rx_irq(hpd_aconnector);
893 }
894 }
895 }
896
897 /**
898 * dmub_hpd_sense_callback - DMUB HPD sense processing callback.
899 * @adev: amdgpu_device pointer
900 * @notify: dmub notification structure
901 *
902 * HPD sense changes can occur during low power states and need to be
903 * notified from firmware to driver.
904 */
dmub_hpd_sense_callback(struct amdgpu_device * adev,struct dmub_notification * notify)905 static void dmub_hpd_sense_callback(struct amdgpu_device *adev,
906 struct dmub_notification *notify)
907 {
908 drm_dbg_driver(adev_to_drm(adev), "DMUB HPD SENSE callback.\n");
909 }
910
911 /**
912 * register_dmub_notify_callback - Sets callback for DMUB notify
913 * @adev: amdgpu_device pointer
914 * @type: Type of dmub notification
915 * @callback: Dmub interrupt callback function
916 * @dmub_int_thread_offload: offload indicator
917 *
918 * API to register a dmub callback handler for a dmub notification
919 * Also sets indicator whether callback processing to be offloaded.
920 * to dmub interrupt handling thread
921 * Return: true if successfully registered, false if there is existing registration
922 */
register_dmub_notify_callback(struct amdgpu_device * adev,enum dmub_notification_type type,dmub_notify_interrupt_callback_t callback,bool dmub_int_thread_offload)923 static bool register_dmub_notify_callback(struct amdgpu_device *adev,
924 enum dmub_notification_type type,
925 dmub_notify_interrupt_callback_t callback,
926 bool dmub_int_thread_offload)
927 {
928 if (callback != NULL && type < ARRAY_SIZE(adev->dm.dmub_thread_offload)) {
929 adev->dm.dmub_callback[type] = callback;
930 adev->dm.dmub_thread_offload[type] = dmub_int_thread_offload;
931 } else
932 return false;
933
934 return true;
935 }
936
dm_handle_hpd_work(struct work_struct * work)937 static void dm_handle_hpd_work(struct work_struct *work)
938 {
939 struct dmub_hpd_work *dmub_hpd_wrk;
940
941 dmub_hpd_wrk = container_of(work, struct dmub_hpd_work, handle_hpd_work);
942
943 if (!dmub_hpd_wrk->dmub_notify) {
944 drm_err(adev_to_drm(dmub_hpd_wrk->adev), "dmub_hpd_wrk dmub_notify is NULL");
945 return;
946 }
947
948 if (dmub_hpd_wrk->dmub_notify->type < ARRAY_SIZE(dmub_hpd_wrk->adev->dm.dmub_callback)) {
949 dmub_hpd_wrk->adev->dm.dmub_callback[dmub_hpd_wrk->dmub_notify->type](dmub_hpd_wrk->adev,
950 dmub_hpd_wrk->dmub_notify);
951 }
952
953 kfree(dmub_hpd_wrk->dmub_notify);
954 kfree(dmub_hpd_wrk);
955
956 }
957
dmub_notification_type_str(enum dmub_notification_type e)958 static const char *dmub_notification_type_str(enum dmub_notification_type e)
959 {
960 switch (e) {
961 case DMUB_NOTIFICATION_NO_DATA:
962 return "NO_DATA";
963 case DMUB_NOTIFICATION_AUX_REPLY:
964 return "AUX_REPLY";
965 case DMUB_NOTIFICATION_HPD:
966 return "HPD";
967 case DMUB_NOTIFICATION_HPD_IRQ:
968 return "HPD_IRQ";
969 case DMUB_NOTIFICATION_SET_CONFIG_REPLY:
970 return "SET_CONFIG_REPLY";
971 case DMUB_NOTIFICATION_DPIA_NOTIFICATION:
972 return "DPIA_NOTIFICATION";
973 case DMUB_NOTIFICATION_HPD_SENSE_NOTIFY:
974 return "HPD_SENSE_NOTIFY";
975 case DMUB_NOTIFICATION_FUSED_IO:
976 return "FUSED_IO";
977 default:
978 return "<unknown>";
979 }
980 }
981
982 #define DMUB_TRACE_MAX_READ 64
983 /**
984 * dm_dmub_outbox1_low_irq() - Handles Outbox interrupt
985 * @interrupt_params: used for determining the Outbox instance
986 *
987 * Handles the Outbox Interrupt
988 * event handler.
989 */
dm_dmub_outbox1_low_irq(void * interrupt_params)990 static void dm_dmub_outbox1_low_irq(void *interrupt_params)
991 {
992 struct dmub_notification notify = {0};
993 struct common_irq_params *irq_params = interrupt_params;
994 struct amdgpu_device *adev = irq_params->adev;
995 struct amdgpu_display_manager *dm = &adev->dm;
996 struct dmcub_trace_buf_entry entry = { 0 };
997 u32 count = 0;
998 struct dmub_hpd_work *dmub_hpd_wrk;
999
1000 do {
1001 if (dc_dmub_srv_get_dmub_outbox0_msg(dm->dc, &entry)) {
1002 trace_amdgpu_dmub_trace_high_irq(entry.trace_code, entry.tick_count,
1003 entry.param0, entry.param1);
1004
1005 drm_dbg_driver(adev_to_drm(adev), "trace_code:%u, tick_count:%u, param0:%u, param1:%u\n",
1006 entry.trace_code, entry.tick_count, entry.param0, entry.param1);
1007 } else
1008 break;
1009
1010 count++;
1011
1012 } while (count <= DMUB_TRACE_MAX_READ);
1013
1014 if (count > DMUB_TRACE_MAX_READ)
1015 drm_dbg_driver(adev_to_drm(adev), "Warning : count > DMUB_TRACE_MAX_READ");
1016
1017 if (dc_enable_dmub_notifications(adev->dm.dc) &&
1018 irq_params->irq_src == DC_IRQ_SOURCE_DMCUB_OUTBOX) {
1019
1020 do {
1021 dc_stat_get_dmub_notification(adev->dm.dc, ¬ify);
1022 if (notify.type >= ARRAY_SIZE(dm->dmub_thread_offload)) {
1023 drm_err(adev_to_drm(adev), "DM: notify type %d invalid!", notify.type);
1024 continue;
1025 }
1026 if (!dm->dmub_callback[notify.type]) {
1027 drm_warn(adev_to_drm(adev), "DMUB notification skipped due to no handler: type=%s\n",
1028 dmub_notification_type_str(notify.type));
1029 continue;
1030 }
1031 if (dm->dmub_thread_offload[notify.type] == true) {
1032 dmub_hpd_wrk = kzalloc(sizeof(*dmub_hpd_wrk), GFP_ATOMIC);
1033 if (!dmub_hpd_wrk) {
1034 drm_err(adev_to_drm(adev), "Failed to allocate dmub_hpd_wrk");
1035 return;
1036 }
1037 dmub_hpd_wrk->dmub_notify = kmemdup(¬ify, sizeof(struct dmub_notification),
1038 GFP_ATOMIC);
1039 if (!dmub_hpd_wrk->dmub_notify) {
1040 kfree(dmub_hpd_wrk);
1041 drm_err(adev_to_drm(adev), "Failed to allocate dmub_hpd_wrk->dmub_notify");
1042 return;
1043 }
1044 INIT_WORK(&dmub_hpd_wrk->handle_hpd_work, dm_handle_hpd_work);
1045 dmub_hpd_wrk->adev = adev;
1046 queue_work(adev->dm.delayed_hpd_wq, &dmub_hpd_wrk->handle_hpd_work);
1047 } else {
1048 dm->dmub_callback[notify.type](adev, ¬ify);
1049 }
1050 } while (notify.pending_notification);
1051 }
1052 }
1053
dm_set_clockgating_state(struct amdgpu_ip_block * ip_block,enum amd_clockgating_state state)1054 static int dm_set_clockgating_state(struct amdgpu_ip_block *ip_block,
1055 enum amd_clockgating_state state)
1056 {
1057 return 0;
1058 }
1059
dm_set_powergating_state(struct amdgpu_ip_block * ip_block,enum amd_powergating_state state)1060 static int dm_set_powergating_state(struct amdgpu_ip_block *ip_block,
1061 enum amd_powergating_state state)
1062 {
1063 return 0;
1064 }
1065
1066 /* Prototypes of private functions */
1067 static int dm_early_init(struct amdgpu_ip_block *ip_block);
1068
1069 /* Allocate memory for FBC compressed data */
amdgpu_dm_fbc_init(struct drm_connector * connector)1070 static void amdgpu_dm_fbc_init(struct drm_connector *connector)
1071 {
1072 struct amdgpu_device *adev = drm_to_adev(connector->dev);
1073 struct dm_compressor_info *compressor = &adev->dm.compressor;
1074 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
1075 struct drm_display_mode *mode;
1076 unsigned long max_size = 0;
1077
1078 if (adev->dm.dc->fbc_compressor == NULL)
1079 return;
1080
1081 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
1082 return;
1083
1084 if (compressor->bo_ptr)
1085 return;
1086
1087
1088 list_for_each_entry(mode, &connector->modes, head) {
1089 if (max_size < (unsigned long) mode->htotal * mode->vtotal)
1090 max_size = (unsigned long) mode->htotal * mode->vtotal;
1091 }
1092
1093 if (max_size) {
1094 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
1095 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
1096 &compressor->gpu_addr, &compressor->cpu_addr);
1097
1098 if (r)
1099 drm_err(adev_to_drm(adev), "DM: Failed to initialize FBC\n");
1100 else {
1101 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
1102 drm_info(adev_to_drm(adev), "DM: FBC alloc %lu\n", max_size*4);
1103 }
1104
1105 }
1106
1107 }
1108
amdgpu_dm_audio_component_get_eld(struct device * kdev,int port,int pipe,bool * enabled,unsigned char * buf,int max_bytes)1109 static int amdgpu_dm_audio_component_get_eld(struct device *kdev, int port,
1110 int pipe, bool *enabled,
1111 unsigned char *buf, int max_bytes)
1112 {
1113 struct drm_device *dev = dev_get_drvdata(kdev);
1114 struct amdgpu_device *adev = drm_to_adev(dev);
1115 struct drm_connector *connector;
1116 struct drm_connector_list_iter conn_iter;
1117 struct amdgpu_dm_connector *aconnector;
1118 int ret = 0;
1119
1120 *enabled = false;
1121
1122 mutex_lock(&adev->dm.audio_lock);
1123
1124 drm_connector_list_iter_begin(dev, &conn_iter);
1125 drm_for_each_connector_iter(connector, &conn_iter) {
1126
1127 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1128 continue;
1129
1130 aconnector = to_amdgpu_dm_connector(connector);
1131 if (aconnector->audio_inst != port)
1132 continue;
1133
1134 *enabled = true;
1135 mutex_lock(&connector->eld_mutex);
1136 ret = drm_eld_size(connector->eld);
1137 memcpy(buf, connector->eld, min(max_bytes, ret));
1138 mutex_unlock(&connector->eld_mutex);
1139
1140 break;
1141 }
1142 drm_connector_list_iter_end(&conn_iter);
1143
1144 mutex_unlock(&adev->dm.audio_lock);
1145
1146 DRM_DEBUG_KMS("Get ELD : idx=%d ret=%d en=%d\n", port, ret, *enabled);
1147
1148 return ret;
1149 }
1150
1151 static const struct drm_audio_component_ops amdgpu_dm_audio_component_ops = {
1152 .get_eld = amdgpu_dm_audio_component_get_eld,
1153 };
1154
amdgpu_dm_audio_component_bind(struct device * kdev,struct device * hda_kdev,void * data)1155 static int amdgpu_dm_audio_component_bind(struct device *kdev,
1156 struct device *hda_kdev, void *data)
1157 {
1158 struct drm_device *dev = dev_get_drvdata(kdev);
1159 struct amdgpu_device *adev = drm_to_adev(dev);
1160 struct drm_audio_component *acomp = data;
1161
1162 acomp->ops = &amdgpu_dm_audio_component_ops;
1163 acomp->dev = kdev;
1164 adev->dm.audio_component = acomp;
1165
1166 return 0;
1167 }
1168
amdgpu_dm_audio_component_unbind(struct device * kdev,struct device * hda_kdev,void * data)1169 static void amdgpu_dm_audio_component_unbind(struct device *kdev,
1170 struct device *hda_kdev, void *data)
1171 {
1172 struct amdgpu_device *adev = drm_to_adev(dev_get_drvdata(kdev));
1173 struct drm_audio_component *acomp = data;
1174
1175 acomp->ops = NULL;
1176 acomp->dev = NULL;
1177 adev->dm.audio_component = NULL;
1178 }
1179
1180 static const struct component_ops amdgpu_dm_audio_component_bind_ops = {
1181 .bind = amdgpu_dm_audio_component_bind,
1182 .unbind = amdgpu_dm_audio_component_unbind,
1183 };
1184
amdgpu_dm_audio_init(struct amdgpu_device * adev)1185 static int amdgpu_dm_audio_init(struct amdgpu_device *adev)
1186 {
1187 int i, ret;
1188
1189 if (!amdgpu_audio)
1190 return 0;
1191
1192 adev->mode_info.audio.enabled = true;
1193
1194 adev->mode_info.audio.num_pins = adev->dm.dc->res_pool->audio_count;
1195
1196 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1197 adev->mode_info.audio.pin[i].channels = -1;
1198 adev->mode_info.audio.pin[i].rate = -1;
1199 adev->mode_info.audio.pin[i].bits_per_sample = -1;
1200 adev->mode_info.audio.pin[i].status_bits = 0;
1201 adev->mode_info.audio.pin[i].category_code = 0;
1202 adev->mode_info.audio.pin[i].connected = false;
1203 adev->mode_info.audio.pin[i].id =
1204 adev->dm.dc->res_pool->audios[i]->inst;
1205 adev->mode_info.audio.pin[i].offset = 0;
1206 }
1207
1208 ret = component_add(adev->dev, &amdgpu_dm_audio_component_bind_ops);
1209 if (ret < 0)
1210 return ret;
1211
1212 adev->dm.audio_registered = true;
1213
1214 return 0;
1215 }
1216
amdgpu_dm_audio_fini(struct amdgpu_device * adev)1217 static void amdgpu_dm_audio_fini(struct amdgpu_device *adev)
1218 {
1219 if (!amdgpu_audio)
1220 return;
1221
1222 if (!adev->mode_info.audio.enabled)
1223 return;
1224
1225 if (adev->dm.audio_registered) {
1226 component_del(adev->dev, &amdgpu_dm_audio_component_bind_ops);
1227 adev->dm.audio_registered = false;
1228 }
1229
1230 /* TODO: Disable audio? */
1231
1232 adev->mode_info.audio.enabled = false;
1233 }
1234
amdgpu_dm_audio_eld_notify(struct amdgpu_device * adev,int pin)1235 static void amdgpu_dm_audio_eld_notify(struct amdgpu_device *adev, int pin)
1236 {
1237 struct drm_audio_component *acomp = adev->dm.audio_component;
1238
1239 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify) {
1240 DRM_DEBUG_KMS("Notify ELD: %d\n", pin);
1241
1242 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
1243 pin, -1);
1244 }
1245 }
1246
dm_dmub_hw_init(struct amdgpu_device * adev)1247 static int dm_dmub_hw_init(struct amdgpu_device *adev)
1248 {
1249 const struct dmcub_firmware_header_v1_0 *hdr;
1250 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
1251 struct dmub_srv_fb_info *fb_info = adev->dm.dmub_fb_info;
1252 const struct firmware *dmub_fw = adev->dm.dmub_fw;
1253 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu;
1254 struct abm *abm = adev->dm.dc->res_pool->abm;
1255 struct dc_context *ctx = adev->dm.dc->ctx;
1256 struct dmub_srv_hw_params hw_params;
1257 enum dmub_status status;
1258 const unsigned char *fw_inst_const, *fw_bss_data;
1259 u32 i, fw_inst_const_size, fw_bss_data_size;
1260 bool has_hw_support;
1261
1262 if (!dmub_srv)
1263 /* DMUB isn't supported on the ASIC. */
1264 return 0;
1265
1266 if (!fb_info) {
1267 drm_err(adev_to_drm(adev), "No framebuffer info for DMUB service.\n");
1268 return -EINVAL;
1269 }
1270
1271 if (!dmub_fw) {
1272 /* Firmware required for DMUB support. */
1273 drm_err(adev_to_drm(adev), "No firmware provided for DMUB.\n");
1274 return -EINVAL;
1275 }
1276
1277 /* initialize register offsets for ASICs with runtime initialization available */
1278 if (dmub_srv->hw_funcs.init_reg_offsets)
1279 dmub_srv->hw_funcs.init_reg_offsets(dmub_srv, ctx);
1280
1281 status = dmub_srv_has_hw_support(dmub_srv, &has_hw_support);
1282 if (status != DMUB_STATUS_OK) {
1283 drm_err(adev_to_drm(adev), "Error checking HW support for DMUB: %d\n", status);
1284 return -EINVAL;
1285 }
1286
1287 if (!has_hw_support) {
1288 drm_info(adev_to_drm(adev), "DMUB unsupported on ASIC\n");
1289 return 0;
1290 }
1291
1292 /* Reset DMCUB if it was previously running - before we overwrite its memory. */
1293 status = dmub_srv_hw_reset(dmub_srv);
1294 if (status != DMUB_STATUS_OK)
1295 drm_warn(adev_to_drm(adev), "Error resetting DMUB HW: %d\n", status);
1296
1297 hdr = (const struct dmcub_firmware_header_v1_0 *)dmub_fw->data;
1298
1299 fw_inst_const = dmub_fw->data +
1300 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1301 PSP_HEADER_BYTES;
1302
1303 fw_bss_data = dmub_fw->data +
1304 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1305 le32_to_cpu(hdr->inst_const_bytes);
1306
1307 /* Copy firmware and bios info into FB memory. */
1308 fw_inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
1309 PSP_HEADER_BYTES - PSP_FOOTER_BYTES;
1310
1311 fw_bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
1312
1313 /* if adev->firmware.load_type == AMDGPU_FW_LOAD_PSP,
1314 * amdgpu_ucode_init_single_fw will load dmub firmware
1315 * fw_inst_const part to cw0; otherwise, the firmware back door load
1316 * will be done by dm_dmub_hw_init
1317 */
1318 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1319 memcpy(fb_info->fb[DMUB_WINDOW_0_INST_CONST].cpu_addr, fw_inst_const,
1320 fw_inst_const_size);
1321 }
1322
1323 if (fw_bss_data_size)
1324 memcpy(fb_info->fb[DMUB_WINDOW_2_BSS_DATA].cpu_addr,
1325 fw_bss_data, fw_bss_data_size);
1326
1327 /* Copy firmware bios info into FB memory. */
1328 memcpy(fb_info->fb[DMUB_WINDOW_3_VBIOS].cpu_addr, adev->bios,
1329 adev->bios_size);
1330
1331 /* Reset regions that need to be reset. */
1332 memset(fb_info->fb[DMUB_WINDOW_4_MAILBOX].cpu_addr, 0,
1333 fb_info->fb[DMUB_WINDOW_4_MAILBOX].size);
1334
1335 memset(fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].cpu_addr, 0,
1336 fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].size);
1337
1338 memset(fb_info->fb[DMUB_WINDOW_6_FW_STATE].cpu_addr, 0,
1339 fb_info->fb[DMUB_WINDOW_6_FW_STATE].size);
1340
1341 memset(fb_info->fb[DMUB_WINDOW_SHARED_STATE].cpu_addr, 0,
1342 fb_info->fb[DMUB_WINDOW_SHARED_STATE].size);
1343
1344 /* Initialize hardware. */
1345 memset(&hw_params, 0, sizeof(hw_params));
1346 hw_params.fb_base = adev->gmc.fb_start;
1347 hw_params.fb_offset = adev->vm_manager.vram_base_offset;
1348
1349 /* backdoor load firmware and trigger dmub running */
1350 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
1351 hw_params.load_inst_const = true;
1352
1353 if (dmcu)
1354 hw_params.psp_version = dmcu->psp_version;
1355
1356 for (i = 0; i < fb_info->num_fb; ++i)
1357 hw_params.fb[i] = &fb_info->fb[i];
1358
1359 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1360 case IP_VERSION(3, 1, 3):
1361 case IP_VERSION(3, 1, 4):
1362 case IP_VERSION(3, 5, 0):
1363 case IP_VERSION(3, 5, 1):
1364 case IP_VERSION(3, 6, 0):
1365 case IP_VERSION(4, 0, 1):
1366 hw_params.dpia_supported = true;
1367 hw_params.disable_dpia = adev->dm.dc->debug.dpia_debug.bits.disable_dpia;
1368 break;
1369 default:
1370 break;
1371 }
1372
1373 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1374 case IP_VERSION(3, 5, 0):
1375 case IP_VERSION(3, 5, 1):
1376 case IP_VERSION(3, 6, 0):
1377 hw_params.ips_sequential_ono = adev->external_rev_id > 0x10;
1378 hw_params.lower_hbr3_phy_ssc = true;
1379 break;
1380 default:
1381 break;
1382 }
1383
1384 status = dmub_srv_hw_init(dmub_srv, &hw_params);
1385 if (status != DMUB_STATUS_OK) {
1386 drm_err(adev_to_drm(adev), "Error initializing DMUB HW: %d\n", status);
1387 return -EINVAL;
1388 }
1389
1390 /* Wait for firmware load to finish. */
1391 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
1392 if (status != DMUB_STATUS_OK)
1393 drm_warn(adev_to_drm(adev), "Wait for DMUB auto-load failed: %d\n", status);
1394
1395 /* Init DMCU and ABM if available. */
1396 if (dmcu && abm) {
1397 dmcu->funcs->dmcu_init(dmcu);
1398 abm->dmcu_is_running = dmcu->funcs->is_dmcu_initialized(dmcu);
1399 }
1400
1401 if (!adev->dm.dc->ctx->dmub_srv)
1402 adev->dm.dc->ctx->dmub_srv = dc_dmub_srv_create(adev->dm.dc, dmub_srv);
1403 if (!adev->dm.dc->ctx->dmub_srv) {
1404 drm_err(adev_to_drm(adev), "Couldn't allocate DC DMUB server!\n");
1405 return -ENOMEM;
1406 }
1407
1408 drm_info(adev_to_drm(adev), "DMUB hardware initialized: version=0x%08X\n",
1409 adev->dm.dmcub_fw_version);
1410
1411 /* Keeping sanity checks off if
1412 * DCN31 >= 4.0.59.0
1413 * DCN314 >= 8.0.16.0
1414 * Otherwise, turn on sanity checks
1415 */
1416 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1417 case IP_VERSION(3, 1, 2):
1418 case IP_VERSION(3, 1, 3):
1419 if (adev->dm.dmcub_fw_version &&
1420 adev->dm.dmcub_fw_version >= DMUB_FW_VERSION(4, 0, 0) &&
1421 adev->dm.dmcub_fw_version < DMUB_FW_VERSION(4, 0, 59))
1422 adev->dm.dc->debug.sanity_checks = true;
1423 break;
1424 case IP_VERSION(3, 1, 4):
1425 if (adev->dm.dmcub_fw_version &&
1426 adev->dm.dmcub_fw_version >= DMUB_FW_VERSION(4, 0, 0) &&
1427 adev->dm.dmcub_fw_version < DMUB_FW_VERSION(8, 0, 16))
1428 adev->dm.dc->debug.sanity_checks = true;
1429 break;
1430 default:
1431 break;
1432 }
1433
1434 return 0;
1435 }
1436
dm_dmub_hw_resume(struct amdgpu_device * adev)1437 static void dm_dmub_hw_resume(struct amdgpu_device *adev)
1438 {
1439 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
1440 enum dmub_status status;
1441 bool init;
1442 int r;
1443
1444 if (!dmub_srv) {
1445 /* DMUB isn't supported on the ASIC. */
1446 return;
1447 }
1448
1449 status = dmub_srv_is_hw_init(dmub_srv, &init);
1450 if (status != DMUB_STATUS_OK)
1451 drm_warn(adev_to_drm(adev), "DMUB hardware init check failed: %d\n", status);
1452
1453 if (status == DMUB_STATUS_OK && init) {
1454 /* Wait for firmware load to finish. */
1455 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
1456 if (status != DMUB_STATUS_OK)
1457 drm_warn(adev_to_drm(adev), "Wait for DMUB auto-load failed: %d\n", status);
1458 } else {
1459 /* Perform the full hardware initialization. */
1460 r = dm_dmub_hw_init(adev);
1461 if (r)
1462 drm_err(adev_to_drm(adev), "DMUB interface failed to initialize: status=%d\n", r);
1463 }
1464 }
1465
mmhub_read_system_context(struct amdgpu_device * adev,struct dc_phy_addr_space_config * pa_config)1466 static void mmhub_read_system_context(struct amdgpu_device *adev, struct dc_phy_addr_space_config *pa_config)
1467 {
1468 u64 pt_base;
1469 u32 logical_addr_low;
1470 u32 logical_addr_high;
1471 u32 agp_base, agp_bot, agp_top;
1472 PHYSICAL_ADDRESS_LOC page_table_start, page_table_end, page_table_base;
1473
1474 memset(pa_config, 0, sizeof(*pa_config));
1475
1476 agp_base = 0;
1477 agp_bot = adev->gmc.agp_start >> 24;
1478 agp_top = adev->gmc.agp_end >> 24;
1479
1480 /* AGP aperture is disabled */
1481 if (agp_bot > agp_top) {
1482 logical_addr_low = adev->gmc.fb_start >> 18;
1483 if (adev->apu_flags & (AMD_APU_IS_RAVEN2 |
1484 AMD_APU_IS_RENOIR |
1485 AMD_APU_IS_GREEN_SARDINE))
1486 /*
1487 * Raven2 has a HW issue that it is unable to use the vram which
1488 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the
1489 * workaround that increase system aperture high address (add 1)
1490 * to get rid of the VM fault and hardware hang.
1491 */
1492 logical_addr_high = (adev->gmc.fb_end >> 18) + 0x1;
1493 else
1494 logical_addr_high = adev->gmc.fb_end >> 18;
1495 } else {
1496 logical_addr_low = min(adev->gmc.fb_start, adev->gmc.agp_start) >> 18;
1497 if (adev->apu_flags & (AMD_APU_IS_RAVEN2 |
1498 AMD_APU_IS_RENOIR |
1499 AMD_APU_IS_GREEN_SARDINE))
1500 /*
1501 * Raven2 has a HW issue that it is unable to use the vram which
1502 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the
1503 * workaround that increase system aperture high address (add 1)
1504 * to get rid of the VM fault and hardware hang.
1505 */
1506 logical_addr_high = max((adev->gmc.fb_end >> 18) + 0x1, adev->gmc.agp_end >> 18);
1507 else
1508 logical_addr_high = max(adev->gmc.fb_end, adev->gmc.agp_end) >> 18;
1509 }
1510
1511 pt_base = amdgpu_gmc_pd_addr(adev->gart.bo);
1512
1513 page_table_start.high_part = upper_32_bits(adev->gmc.gart_start >>
1514 AMDGPU_GPU_PAGE_SHIFT);
1515 page_table_start.low_part = lower_32_bits(adev->gmc.gart_start >>
1516 AMDGPU_GPU_PAGE_SHIFT);
1517 page_table_end.high_part = upper_32_bits(adev->gmc.gart_end >>
1518 AMDGPU_GPU_PAGE_SHIFT);
1519 page_table_end.low_part = lower_32_bits(adev->gmc.gart_end >>
1520 AMDGPU_GPU_PAGE_SHIFT);
1521 page_table_base.high_part = upper_32_bits(pt_base);
1522 page_table_base.low_part = lower_32_bits(pt_base);
1523
1524 pa_config->system_aperture.start_addr = (uint64_t)logical_addr_low << 18;
1525 pa_config->system_aperture.end_addr = (uint64_t)logical_addr_high << 18;
1526
1527 pa_config->system_aperture.agp_base = (uint64_t)agp_base << 24;
1528 pa_config->system_aperture.agp_bot = (uint64_t)agp_bot << 24;
1529 pa_config->system_aperture.agp_top = (uint64_t)agp_top << 24;
1530
1531 pa_config->system_aperture.fb_base = adev->gmc.fb_start;
1532 pa_config->system_aperture.fb_offset = adev->vm_manager.vram_base_offset;
1533 pa_config->system_aperture.fb_top = adev->gmc.fb_end;
1534
1535 pa_config->gart_config.page_table_start_addr = page_table_start.quad_part << 12;
1536 pa_config->gart_config.page_table_end_addr = page_table_end.quad_part << 12;
1537 pa_config->gart_config.page_table_base_addr = page_table_base.quad_part;
1538
1539 pa_config->is_hvm_enabled = adev->mode_info.gpu_vm_support;
1540
1541 }
1542
force_connector_state(struct amdgpu_dm_connector * aconnector,enum drm_connector_force force_state)1543 static void force_connector_state(
1544 struct amdgpu_dm_connector *aconnector,
1545 enum drm_connector_force force_state)
1546 {
1547 struct drm_connector *connector = &aconnector->base;
1548
1549 mutex_lock(&connector->dev->mode_config.mutex);
1550 aconnector->base.force = force_state;
1551 mutex_unlock(&connector->dev->mode_config.mutex);
1552
1553 mutex_lock(&aconnector->hpd_lock);
1554 drm_kms_helper_connector_hotplug_event(connector);
1555 mutex_unlock(&aconnector->hpd_lock);
1556 }
1557
dm_handle_hpd_rx_offload_work(struct work_struct * work)1558 static void dm_handle_hpd_rx_offload_work(struct work_struct *work)
1559 {
1560 struct hpd_rx_irq_offload_work *offload_work;
1561 struct amdgpu_dm_connector *aconnector;
1562 struct dc_link *dc_link;
1563 struct amdgpu_device *adev;
1564 enum dc_connection_type new_connection_type = dc_connection_none;
1565 unsigned long flags;
1566 union test_response test_response;
1567
1568 memset(&test_response, 0, sizeof(test_response));
1569
1570 offload_work = container_of(work, struct hpd_rx_irq_offload_work, work);
1571 aconnector = offload_work->offload_wq->aconnector;
1572 adev = offload_work->adev;
1573
1574 if (!aconnector) {
1575 drm_err(adev_to_drm(adev), "Can't retrieve aconnector in hpd_rx_irq_offload_work");
1576 goto skip;
1577 }
1578
1579 dc_link = aconnector->dc_link;
1580
1581 mutex_lock(&aconnector->hpd_lock);
1582 if (!dc_link_detect_connection_type(dc_link, &new_connection_type))
1583 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
1584 mutex_unlock(&aconnector->hpd_lock);
1585
1586 if (new_connection_type == dc_connection_none)
1587 goto skip;
1588
1589 if (amdgpu_in_reset(adev))
1590 goto skip;
1591
1592 if (offload_work->data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY ||
1593 offload_work->data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) {
1594 dm_handle_mst_sideband_msg_ready_event(&aconnector->mst_mgr, DOWN_OR_UP_MSG_RDY_EVENT);
1595 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags);
1596 offload_work->offload_wq->is_handling_mst_msg_rdy_event = false;
1597 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags);
1598 goto skip;
1599 }
1600
1601 mutex_lock(&adev->dm.dc_lock);
1602 if (offload_work->data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
1603 dc_link_dp_handle_automated_test(dc_link);
1604
1605 if (aconnector->timing_changed) {
1606 /* force connector disconnect and reconnect */
1607 force_connector_state(aconnector, DRM_FORCE_OFF);
1608 msleep(100);
1609 force_connector_state(aconnector, DRM_FORCE_UNSPECIFIED);
1610 }
1611
1612 test_response.bits.ACK = 1;
1613
1614 core_link_write_dpcd(
1615 dc_link,
1616 DP_TEST_RESPONSE,
1617 &test_response.raw,
1618 sizeof(test_response));
1619 } else if ((dc_link->connector_signal != SIGNAL_TYPE_EDP) &&
1620 dc_link_check_link_loss_status(dc_link, &offload_work->data) &&
1621 dc_link_dp_allow_hpd_rx_irq(dc_link)) {
1622 /* offload_work->data is from handle_hpd_rx_irq->
1623 * schedule_hpd_rx_offload_work.this is defer handle
1624 * for hpd short pulse. upon here, link status may be
1625 * changed, need get latest link status from dpcd
1626 * registers. if link status is good, skip run link
1627 * training again.
1628 */
1629 union hpd_irq_data irq_data;
1630
1631 memset(&irq_data, 0, sizeof(irq_data));
1632
1633 /* before dc_link_dp_handle_link_loss, allow new link lost handle
1634 * request be added to work queue if link lost at end of dc_link_
1635 * dp_handle_link_loss
1636 */
1637 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags);
1638 offload_work->offload_wq->is_handling_link_loss = false;
1639 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags);
1640
1641 if ((dc_link_dp_read_hpd_rx_irq_data(dc_link, &irq_data) == DC_OK) &&
1642 dc_link_check_link_loss_status(dc_link, &irq_data))
1643 dc_link_dp_handle_link_loss(dc_link);
1644 }
1645 mutex_unlock(&adev->dm.dc_lock);
1646
1647 skip:
1648 kfree(offload_work);
1649
1650 }
1651
hpd_rx_irq_create_workqueue(struct amdgpu_device * adev)1652 static struct hpd_rx_irq_offload_work_queue *hpd_rx_irq_create_workqueue(struct amdgpu_device *adev)
1653 {
1654 struct dc *dc = adev->dm.dc;
1655 int max_caps = dc->caps.max_links;
1656 int i = 0;
1657 struct hpd_rx_irq_offload_work_queue *hpd_rx_offload_wq = NULL;
1658
1659 hpd_rx_offload_wq = kcalloc(max_caps, sizeof(*hpd_rx_offload_wq), GFP_KERNEL);
1660
1661 if (!hpd_rx_offload_wq)
1662 return NULL;
1663
1664
1665 for (i = 0; i < max_caps; i++) {
1666 hpd_rx_offload_wq[i].wq =
1667 create_singlethread_workqueue("amdgpu_dm_hpd_rx_offload_wq");
1668
1669 if (hpd_rx_offload_wq[i].wq == NULL) {
1670 drm_err(adev_to_drm(adev), "create amdgpu_dm_hpd_rx_offload_wq fail!");
1671 goto out_err;
1672 }
1673
1674 spin_lock_init(&hpd_rx_offload_wq[i].offload_lock);
1675 }
1676
1677 return hpd_rx_offload_wq;
1678
1679 out_err:
1680 for (i = 0; i < max_caps; i++) {
1681 if (hpd_rx_offload_wq[i].wq)
1682 destroy_workqueue(hpd_rx_offload_wq[i].wq);
1683 }
1684 kfree(hpd_rx_offload_wq);
1685 return NULL;
1686 }
1687
1688 struct amdgpu_stutter_quirk {
1689 u16 chip_vendor;
1690 u16 chip_device;
1691 u16 subsys_vendor;
1692 u16 subsys_device;
1693 u8 revision;
1694 };
1695
1696 static const struct amdgpu_stutter_quirk amdgpu_stutter_quirk_list[] = {
1697 /* https://bugzilla.kernel.org/show_bug.cgi?id=214417 */
1698 { 0x1002, 0x15dd, 0x1002, 0x15dd, 0xc8 },
1699 { 0, 0, 0, 0, 0 },
1700 };
1701
dm_should_disable_stutter(struct pci_dev * pdev)1702 static bool dm_should_disable_stutter(struct pci_dev *pdev)
1703 {
1704 const struct amdgpu_stutter_quirk *p = amdgpu_stutter_quirk_list;
1705
1706 while (p && p->chip_device != 0) {
1707 if (pdev->vendor == p->chip_vendor &&
1708 pdev->device == p->chip_device &&
1709 pdev->subsystem_vendor == p->subsys_vendor &&
1710 pdev->subsystem_device == p->subsys_device &&
1711 pdev->revision == p->revision) {
1712 return true;
1713 }
1714 ++p;
1715 }
1716 return false;
1717 }
1718
1719
1720 void*
dm_allocate_gpu_mem(struct amdgpu_device * adev,enum dc_gpu_mem_alloc_type type,size_t size,long long * addr)1721 dm_allocate_gpu_mem(
1722 struct amdgpu_device *adev,
1723 enum dc_gpu_mem_alloc_type type,
1724 size_t size,
1725 long long *addr)
1726 {
1727 struct dal_allocation *da;
1728 u32 domain = (type == DC_MEM_ALLOC_TYPE_GART) ?
1729 AMDGPU_GEM_DOMAIN_GTT : AMDGPU_GEM_DOMAIN_VRAM;
1730 int ret;
1731
1732 da = kzalloc(sizeof(struct dal_allocation), GFP_KERNEL);
1733 if (!da)
1734 return NULL;
1735
1736 ret = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE,
1737 domain, &da->bo,
1738 &da->gpu_addr, &da->cpu_ptr);
1739
1740 *addr = da->gpu_addr;
1741
1742 if (ret) {
1743 kfree(da);
1744 return NULL;
1745 }
1746
1747 /* add da to list in dm */
1748 list_add(&da->list, &adev->dm.da_list);
1749
1750 return da->cpu_ptr;
1751 }
1752
1753 void
dm_free_gpu_mem(struct amdgpu_device * adev,enum dc_gpu_mem_alloc_type type,void * pvMem)1754 dm_free_gpu_mem(
1755 struct amdgpu_device *adev,
1756 enum dc_gpu_mem_alloc_type type,
1757 void *pvMem)
1758 {
1759 struct dal_allocation *da;
1760
1761 /* walk the da list in DM */
1762 list_for_each_entry(da, &adev->dm.da_list, list) {
1763 if (pvMem == da->cpu_ptr) {
1764 amdgpu_bo_free_kernel(&da->bo, &da->gpu_addr, &da->cpu_ptr);
1765 list_del(&da->list);
1766 kfree(da);
1767 break;
1768 }
1769 }
1770
1771 }
1772
1773 static enum dmub_status
dm_dmub_send_vbios_gpint_command(struct amdgpu_device * adev,enum dmub_gpint_command command_code,uint16_t param,uint32_t timeout_us)1774 dm_dmub_send_vbios_gpint_command(struct amdgpu_device *adev,
1775 enum dmub_gpint_command command_code,
1776 uint16_t param,
1777 uint32_t timeout_us)
1778 {
1779 union dmub_gpint_data_register reg, test;
1780 uint32_t i;
1781
1782 /* Assume that VBIOS DMUB is ready to take commands */
1783
1784 reg.bits.status = 1;
1785 reg.bits.command_code = command_code;
1786 reg.bits.param = param;
1787
1788 cgs_write_register(adev->dm.cgs_device, 0x34c0 + 0x01f8, reg.all);
1789
1790 for (i = 0; i < timeout_us; ++i) {
1791 udelay(1);
1792
1793 /* Check if our GPINT got acked */
1794 reg.bits.status = 0;
1795 test = (union dmub_gpint_data_register)
1796 cgs_read_register(adev->dm.cgs_device, 0x34c0 + 0x01f8);
1797
1798 if (test.all == reg.all)
1799 return DMUB_STATUS_OK;
1800 }
1801
1802 return DMUB_STATUS_TIMEOUT;
1803 }
1804
dm_dmub_get_vbios_bounding_box(struct amdgpu_device * adev)1805 static void *dm_dmub_get_vbios_bounding_box(struct amdgpu_device *adev)
1806 {
1807 void *bb;
1808 long long addr;
1809 unsigned int bb_size;
1810 int i = 0;
1811 uint16_t chunk;
1812 enum dmub_gpint_command send_addrs[] = {
1813 DMUB_GPINT__SET_BB_ADDR_WORD0,
1814 DMUB_GPINT__SET_BB_ADDR_WORD1,
1815 DMUB_GPINT__SET_BB_ADDR_WORD2,
1816 DMUB_GPINT__SET_BB_ADDR_WORD3,
1817 };
1818 enum dmub_status ret;
1819
1820 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1821 case IP_VERSION(4, 0, 1):
1822 bb_size = sizeof(struct dml2_soc_bb);
1823 break;
1824 default:
1825 return NULL;
1826 }
1827
1828 bb = dm_allocate_gpu_mem(adev,
1829 DC_MEM_ALLOC_TYPE_GART,
1830 bb_size,
1831 &addr);
1832 if (!bb)
1833 return NULL;
1834
1835 for (i = 0; i < 4; i++) {
1836 /* Extract 16-bit chunk */
1837 chunk = ((uint64_t) addr >> (i * 16)) & 0xFFFF;
1838 /* Send the chunk */
1839 ret = dm_dmub_send_vbios_gpint_command(adev, send_addrs[i], chunk, 30000);
1840 if (ret != DMUB_STATUS_OK)
1841 goto free_bb;
1842 }
1843
1844 /* Now ask DMUB to copy the bb */
1845 ret = dm_dmub_send_vbios_gpint_command(adev, DMUB_GPINT__BB_COPY, 1, 200000);
1846 if (ret != DMUB_STATUS_OK)
1847 goto free_bb;
1848
1849 return bb;
1850
1851 free_bb:
1852 dm_free_gpu_mem(adev, DC_MEM_ALLOC_TYPE_GART, (void *) bb);
1853 return NULL;
1854
1855 }
1856
dm_get_default_ips_mode(struct amdgpu_device * adev)1857 static enum dmub_ips_disable_type dm_get_default_ips_mode(
1858 struct amdgpu_device *adev)
1859 {
1860 enum dmub_ips_disable_type ret = DMUB_IPS_ENABLE;
1861
1862 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1863 case IP_VERSION(3, 5, 0):
1864 case IP_VERSION(3, 6, 0):
1865 case IP_VERSION(3, 5, 1):
1866 ret = DMUB_IPS_RCG_IN_ACTIVE_IPS2_IN_OFF;
1867 break;
1868 default:
1869 /* ASICs older than DCN35 do not have IPSs */
1870 if (amdgpu_ip_version(adev, DCE_HWIP, 0) < IP_VERSION(3, 5, 0))
1871 ret = DMUB_IPS_DISABLE_ALL;
1872 break;
1873 }
1874
1875 return ret;
1876 }
1877
amdgpu_dm_init(struct amdgpu_device * adev)1878 static int amdgpu_dm_init(struct amdgpu_device *adev)
1879 {
1880 struct dc_init_data init_data;
1881 struct dc_callback_init init_params;
1882 int r;
1883
1884 adev->dm.ddev = adev_to_drm(adev);
1885 adev->dm.adev = adev;
1886
1887 /* Zero all the fields */
1888 memset(&init_data, 0, sizeof(init_data));
1889 memset(&init_params, 0, sizeof(init_params));
1890
1891 mutex_init(&adev->dm.dpia_aux_lock);
1892 mutex_init(&adev->dm.dc_lock);
1893 mutex_init(&adev->dm.audio_lock);
1894
1895 if (amdgpu_dm_irq_init(adev)) {
1896 drm_err(adev_to_drm(adev), "failed to initialize DM IRQ support.\n");
1897 goto error;
1898 }
1899
1900 init_data.asic_id.chip_family = adev->family;
1901
1902 init_data.asic_id.pci_revision_id = adev->pdev->revision;
1903 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
1904 init_data.asic_id.chip_id = adev->pdev->device;
1905
1906 init_data.asic_id.vram_width = adev->gmc.vram_width;
1907 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
1908 init_data.asic_id.atombios_base_address =
1909 adev->mode_info.atom_context->bios;
1910
1911 init_data.driver = adev;
1912
1913 /* cgs_device was created in dm_sw_init() */
1914 init_data.cgs_device = adev->dm.cgs_device;
1915
1916 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
1917
1918 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1919 case IP_VERSION(2, 1, 0):
1920 switch (adev->dm.dmcub_fw_version) {
1921 case 0: /* development */
1922 case 0x1: /* linux-firmware.git hash 6d9f399 */
1923 case 0x01000000: /* linux-firmware.git hash 9a0b0f4 */
1924 init_data.flags.disable_dmcu = false;
1925 break;
1926 default:
1927 init_data.flags.disable_dmcu = true;
1928 }
1929 break;
1930 case IP_VERSION(2, 0, 3):
1931 init_data.flags.disable_dmcu = true;
1932 break;
1933 default:
1934 break;
1935 }
1936
1937 /* APU support S/G display by default except:
1938 * ASICs before Carrizo,
1939 * RAVEN1 (Users reported stability issue)
1940 */
1941
1942 if (adev->asic_type < CHIP_CARRIZO) {
1943 init_data.flags.gpu_vm_support = false;
1944 } else if (adev->asic_type == CHIP_RAVEN) {
1945 if (adev->apu_flags & AMD_APU_IS_RAVEN)
1946 init_data.flags.gpu_vm_support = false;
1947 else
1948 init_data.flags.gpu_vm_support = (amdgpu_sg_display != 0);
1949 } else {
1950 if (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(2, 0, 3))
1951 init_data.flags.gpu_vm_support = (amdgpu_sg_display == 1);
1952 else
1953 init_data.flags.gpu_vm_support =
1954 (amdgpu_sg_display != 0) && (adev->flags & AMD_IS_APU);
1955 }
1956
1957 adev->mode_info.gpu_vm_support = init_data.flags.gpu_vm_support;
1958
1959 if (amdgpu_dc_feature_mask & DC_FBC_MASK)
1960 init_data.flags.fbc_support = true;
1961
1962 if (amdgpu_dc_feature_mask & DC_MULTI_MON_PP_MCLK_SWITCH_MASK)
1963 init_data.flags.multi_mon_pp_mclk_switch = true;
1964
1965 if (amdgpu_dc_feature_mask & DC_DISABLE_FRACTIONAL_PWM_MASK)
1966 init_data.flags.disable_fractional_pwm = true;
1967
1968 if (amdgpu_dc_feature_mask & DC_EDP_NO_POWER_SEQUENCING)
1969 init_data.flags.edp_no_power_sequencing = true;
1970
1971 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP1_4A)
1972 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP1_4A = true;
1973 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP2_0)
1974 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP2_0 = true;
1975
1976 init_data.flags.seamless_boot_edp_requested = false;
1977
1978 if (amdgpu_device_seamless_boot_supported(adev)) {
1979 init_data.flags.seamless_boot_edp_requested = true;
1980 init_data.flags.allow_seamless_boot_optimization = true;
1981 drm_dbg(adev->dm.ddev, "Seamless boot requested\n");
1982 }
1983
1984 init_data.flags.enable_mipi_converter_optimization = true;
1985
1986 init_data.dcn_reg_offsets = adev->reg_offset[DCE_HWIP][0];
1987 init_data.nbio_reg_offsets = adev->reg_offset[NBIO_HWIP][0];
1988 init_data.clk_reg_offsets = adev->reg_offset[CLK_HWIP][0];
1989
1990 if (amdgpu_dc_debug_mask & DC_DISABLE_IPS)
1991 init_data.flags.disable_ips = DMUB_IPS_DISABLE_ALL;
1992 else if (amdgpu_dc_debug_mask & DC_DISABLE_IPS_DYNAMIC)
1993 init_data.flags.disable_ips = DMUB_IPS_DISABLE_DYNAMIC;
1994 else if (amdgpu_dc_debug_mask & DC_DISABLE_IPS2_DYNAMIC)
1995 init_data.flags.disable_ips = DMUB_IPS_RCG_IN_ACTIVE_IPS2_IN_OFF;
1996 else if (amdgpu_dc_debug_mask & DC_FORCE_IPS_ENABLE)
1997 init_data.flags.disable_ips = DMUB_IPS_ENABLE;
1998 else
1999 init_data.flags.disable_ips = dm_get_default_ips_mode(adev);
2000
2001 init_data.flags.disable_ips_in_vpb = 0;
2002
2003 /* DCN35 and above supports dynamic DTBCLK switch */
2004 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 5, 0))
2005 init_data.flags.allow_0_dtb_clk = true;
2006
2007 /* Enable DWB for tested platforms only */
2008 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 0, 0))
2009 init_data.num_virtual_links = 1;
2010
2011 retrieve_dmi_info(&adev->dm);
2012 if (adev->dm.edp0_on_dp1_quirk)
2013 init_data.flags.support_edp0_on_dp1 = true;
2014
2015 if (adev->dm.bb_from_dmub)
2016 init_data.bb_from_dmub = adev->dm.bb_from_dmub;
2017 else
2018 init_data.bb_from_dmub = NULL;
2019
2020 /* Display Core create. */
2021 adev->dm.dc = dc_create(&init_data);
2022
2023 if (adev->dm.dc) {
2024 drm_info(adev_to_drm(adev), "Display Core v%s initialized on %s\n", DC_VER,
2025 dce_version_to_string(adev->dm.dc->ctx->dce_version));
2026 } else {
2027 drm_info(adev_to_drm(adev), "Display Core failed to initialize with v%s!\n", DC_VER);
2028 goto error;
2029 }
2030
2031 if (amdgpu_dc_debug_mask & DC_DISABLE_PIPE_SPLIT) {
2032 adev->dm.dc->debug.force_single_disp_pipe_split = false;
2033 adev->dm.dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
2034 }
2035
2036 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
2037 adev->dm.dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
2038 if (dm_should_disable_stutter(adev->pdev))
2039 adev->dm.dc->debug.disable_stutter = true;
2040
2041 if (amdgpu_dc_debug_mask & DC_DISABLE_STUTTER)
2042 adev->dm.dc->debug.disable_stutter = true;
2043
2044 if (amdgpu_dc_debug_mask & DC_DISABLE_DSC)
2045 adev->dm.dc->debug.disable_dsc = true;
2046
2047 if (amdgpu_dc_debug_mask & DC_DISABLE_CLOCK_GATING)
2048 adev->dm.dc->debug.disable_clock_gate = true;
2049
2050 if (amdgpu_dc_debug_mask & DC_FORCE_SUBVP_MCLK_SWITCH)
2051 adev->dm.dc->debug.force_subvp_mclk_switch = true;
2052
2053 if (amdgpu_dc_debug_mask & DC_DISABLE_SUBVP_FAMS) {
2054 adev->dm.dc->debug.force_disable_subvp = true;
2055 adev->dm.dc->debug.fams2_config.bits.enable = false;
2056 }
2057
2058 if (amdgpu_dc_debug_mask & DC_ENABLE_DML2) {
2059 adev->dm.dc->debug.using_dml2 = true;
2060 adev->dm.dc->debug.using_dml21 = true;
2061 }
2062
2063 if (amdgpu_dc_debug_mask & DC_HDCP_LC_FORCE_FW_ENABLE)
2064 adev->dm.dc->debug.hdcp_lc_force_fw_enable = true;
2065
2066 if (amdgpu_dc_debug_mask & DC_HDCP_LC_ENABLE_SW_FALLBACK)
2067 adev->dm.dc->debug.hdcp_lc_enable_sw_fallback = true;
2068
2069 if (amdgpu_dc_debug_mask & DC_SKIP_DETECTION_LT)
2070 adev->dm.dc->debug.skip_detection_link_training = true;
2071
2072 adev->dm.dc->debug.visual_confirm = amdgpu_dc_visual_confirm;
2073
2074 /* TODO: Remove after DP2 receiver gets proper support of Cable ID feature */
2075 adev->dm.dc->debug.ignore_cable_id = true;
2076
2077 if (adev->dm.dc->caps.dp_hdmi21_pcon_support)
2078 drm_info(adev_to_drm(adev), "DP-HDMI FRL PCON supported\n");
2079
2080 r = dm_dmub_hw_init(adev);
2081 if (r) {
2082 drm_err(adev_to_drm(adev), "DMUB interface failed to initialize: status=%d\n", r);
2083 goto error;
2084 }
2085
2086 dc_hardware_init(adev->dm.dc);
2087
2088 adev->dm.hpd_rx_offload_wq = hpd_rx_irq_create_workqueue(adev);
2089 if (!adev->dm.hpd_rx_offload_wq) {
2090 drm_err(adev_to_drm(adev), "failed to create hpd rx offload workqueue.\n");
2091 goto error;
2092 }
2093
2094 if ((adev->flags & AMD_IS_APU) && (adev->asic_type >= CHIP_CARRIZO)) {
2095 struct dc_phy_addr_space_config pa_config;
2096
2097 mmhub_read_system_context(adev, &pa_config);
2098
2099 // Call the DC init_memory func
2100 dc_setup_system_context(adev->dm.dc, &pa_config);
2101 }
2102
2103 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
2104 if (!adev->dm.freesync_module) {
2105 drm_err(adev_to_drm(adev),
2106 "failed to initialize freesync_module.\n");
2107 } else
2108 drm_dbg_driver(adev_to_drm(adev), "amdgpu: freesync_module init done %p.\n",
2109 adev->dm.freesync_module);
2110
2111 amdgpu_dm_init_color_mod();
2112
2113 if (adev->dm.dc->caps.max_links > 0) {
2114 adev->dm.vblank_control_workqueue =
2115 create_singlethread_workqueue("dm_vblank_control_workqueue");
2116 if (!adev->dm.vblank_control_workqueue)
2117 drm_err(adev_to_drm(adev), "failed to initialize vblank_workqueue.\n");
2118 }
2119
2120 if (adev->dm.dc->caps.ips_support &&
2121 adev->dm.dc->config.disable_ips != DMUB_IPS_DISABLE_ALL)
2122 adev->dm.idle_workqueue = idle_create_workqueue(adev);
2123
2124 if (adev->dm.dc->caps.max_links > 0 && adev->family >= AMDGPU_FAMILY_RV) {
2125 adev->dm.hdcp_workqueue = hdcp_create_workqueue(adev, &init_params.cp_psp, adev->dm.dc);
2126
2127 if (!adev->dm.hdcp_workqueue)
2128 drm_err(adev_to_drm(adev), "failed to initialize hdcp_workqueue.\n");
2129 else
2130 drm_dbg_driver(adev_to_drm(adev), "amdgpu: hdcp_workqueue init done %p.\n", adev->dm.hdcp_workqueue);
2131
2132 dc_init_callbacks(adev->dm.dc, &init_params);
2133 }
2134 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
2135 init_completion(&adev->dm.dmub_aux_transfer_done);
2136 adev->dm.dmub_notify = kzalloc(sizeof(struct dmub_notification), GFP_KERNEL);
2137 if (!adev->dm.dmub_notify) {
2138 drm_info(adev_to_drm(adev), "fail to allocate adev->dm.dmub_notify");
2139 goto error;
2140 }
2141
2142 adev->dm.delayed_hpd_wq = create_singlethread_workqueue("amdgpu_dm_hpd_wq");
2143 if (!adev->dm.delayed_hpd_wq) {
2144 drm_err(adev_to_drm(adev), "failed to create hpd offload workqueue.\n");
2145 goto error;
2146 }
2147
2148 amdgpu_dm_outbox_init(adev);
2149 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_AUX_REPLY,
2150 dmub_aux_setconfig_callback, false)) {
2151 drm_err(adev_to_drm(adev), "fail to register dmub aux callback");
2152 goto error;
2153 }
2154
2155 for (size_t i = 0; i < ARRAY_SIZE(adev->dm.fused_io); i++)
2156 init_completion(&adev->dm.fused_io[i].replied);
2157
2158 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_FUSED_IO,
2159 dmub_aux_fused_io_callback, false)) {
2160 drm_err(adev_to_drm(adev), "fail to register dmub fused io callback");
2161 goto error;
2162 }
2163 /* Enable outbox notification only after IRQ handlers are registered and DMUB is alive.
2164 * It is expected that DMUB will resend any pending notifications at this point. Note
2165 * that hpd and hpd_irq handler registration are deferred to register_hpd_handlers() to
2166 * align legacy interface initialization sequence. Connection status will be proactivly
2167 * detected once in the amdgpu_dm_initialize_drm_device.
2168 */
2169 dc_enable_dmub_outbox(adev->dm.dc);
2170
2171 /* DPIA trace goes to dmesg logs only if outbox is enabled */
2172 if (amdgpu_dc_debug_mask & DC_ENABLE_DPIA_TRACE)
2173 dc_dmub_srv_enable_dpia_trace(adev->dm.dc);
2174 }
2175
2176 if (amdgpu_dm_initialize_drm_device(adev)) {
2177 drm_err(adev_to_drm(adev),
2178 "failed to initialize sw for display support.\n");
2179 goto error;
2180 }
2181
2182 /* create fake encoders for MST */
2183 dm_dp_create_fake_mst_encoders(adev);
2184
2185 /* TODO: Add_display_info? */
2186
2187 /* TODO use dynamic cursor width */
2188 adev_to_drm(adev)->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
2189 adev_to_drm(adev)->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
2190
2191 if (drm_vblank_init(adev_to_drm(adev), adev->dm.display_indexes_num)) {
2192 drm_err(adev_to_drm(adev),
2193 "failed to initialize vblank for display support.\n");
2194 goto error;
2195 }
2196
2197 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
2198 amdgpu_dm_crtc_secure_display_create_contexts(adev);
2199 if (!adev->dm.secure_display_ctx.crtc_ctx)
2200 drm_err(adev_to_drm(adev), "failed to initialize secure display contexts.\n");
2201
2202 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(4, 0, 1))
2203 adev->dm.secure_display_ctx.support_mul_roi = true;
2204
2205 #endif
2206
2207 drm_dbg_driver(adev_to_drm(adev), "KMS initialized.\n");
2208
2209 return 0;
2210 error:
2211 amdgpu_dm_fini(adev);
2212
2213 return -EINVAL;
2214 }
2215
amdgpu_dm_early_fini(struct amdgpu_ip_block * ip_block)2216 static int amdgpu_dm_early_fini(struct amdgpu_ip_block *ip_block)
2217 {
2218 struct amdgpu_device *adev = ip_block->adev;
2219
2220 amdgpu_dm_audio_fini(adev);
2221
2222 return 0;
2223 }
2224
amdgpu_dm_fini(struct amdgpu_device * adev)2225 static void amdgpu_dm_fini(struct amdgpu_device *adev)
2226 {
2227 int i;
2228
2229 if (adev->dm.vblank_control_workqueue) {
2230 destroy_workqueue(adev->dm.vblank_control_workqueue);
2231 adev->dm.vblank_control_workqueue = NULL;
2232 }
2233
2234 if (adev->dm.idle_workqueue) {
2235 if (adev->dm.idle_workqueue->running) {
2236 adev->dm.idle_workqueue->enable = false;
2237 flush_work(&adev->dm.idle_workqueue->work);
2238 }
2239
2240 kfree(adev->dm.idle_workqueue);
2241 adev->dm.idle_workqueue = NULL;
2242 }
2243
2244 amdgpu_dm_destroy_drm_device(&adev->dm);
2245
2246 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
2247 if (adev->dm.secure_display_ctx.crtc_ctx) {
2248 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2249 if (adev->dm.secure_display_ctx.crtc_ctx[i].crtc) {
2250 flush_work(&adev->dm.secure_display_ctx.crtc_ctx[i].notify_ta_work);
2251 flush_work(&adev->dm.secure_display_ctx.crtc_ctx[i].forward_roi_work);
2252 }
2253 }
2254 kfree(adev->dm.secure_display_ctx.crtc_ctx);
2255 adev->dm.secure_display_ctx.crtc_ctx = NULL;
2256 }
2257 #endif
2258 if (adev->dm.hdcp_workqueue) {
2259 hdcp_destroy(&adev->dev->kobj, adev->dm.hdcp_workqueue);
2260 adev->dm.hdcp_workqueue = NULL;
2261 }
2262
2263 if (adev->dm.dc) {
2264 dc_deinit_callbacks(adev->dm.dc);
2265 dc_dmub_srv_destroy(&adev->dm.dc->ctx->dmub_srv);
2266 if (dc_enable_dmub_notifications(adev->dm.dc)) {
2267 kfree(adev->dm.dmub_notify);
2268 adev->dm.dmub_notify = NULL;
2269 destroy_workqueue(adev->dm.delayed_hpd_wq);
2270 adev->dm.delayed_hpd_wq = NULL;
2271 }
2272 }
2273
2274 if (adev->dm.dmub_bo)
2275 amdgpu_bo_free_kernel(&adev->dm.dmub_bo,
2276 &adev->dm.dmub_bo_gpu_addr,
2277 &adev->dm.dmub_bo_cpu_addr);
2278
2279 if (adev->dm.hpd_rx_offload_wq && adev->dm.dc) {
2280 for (i = 0; i < adev->dm.dc->caps.max_links; i++) {
2281 if (adev->dm.hpd_rx_offload_wq[i].wq) {
2282 destroy_workqueue(adev->dm.hpd_rx_offload_wq[i].wq);
2283 adev->dm.hpd_rx_offload_wq[i].wq = NULL;
2284 }
2285 }
2286
2287 kfree(adev->dm.hpd_rx_offload_wq);
2288 adev->dm.hpd_rx_offload_wq = NULL;
2289 }
2290
2291 /* DC Destroy TODO: Replace destroy DAL */
2292 if (adev->dm.dc)
2293 dc_destroy(&adev->dm.dc);
2294 /*
2295 * TODO: pageflip, vlank interrupt
2296 *
2297 * amdgpu_dm_irq_fini(adev);
2298 */
2299
2300 if (adev->dm.cgs_device) {
2301 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
2302 adev->dm.cgs_device = NULL;
2303 }
2304 if (adev->dm.freesync_module) {
2305 mod_freesync_destroy(adev->dm.freesync_module);
2306 adev->dm.freesync_module = NULL;
2307 }
2308
2309 mutex_destroy(&adev->dm.audio_lock);
2310 mutex_destroy(&adev->dm.dc_lock);
2311 mutex_destroy(&adev->dm.dpia_aux_lock);
2312 }
2313
load_dmcu_fw(struct amdgpu_device * adev)2314 static int load_dmcu_fw(struct amdgpu_device *adev)
2315 {
2316 const char *fw_name_dmcu = NULL;
2317 int r;
2318 const struct dmcu_firmware_header_v1_0 *hdr;
2319
2320 switch (adev->asic_type) {
2321 #if defined(CONFIG_DRM_AMD_DC_SI)
2322 case CHIP_TAHITI:
2323 case CHIP_PITCAIRN:
2324 case CHIP_VERDE:
2325 case CHIP_OLAND:
2326 #endif
2327 case CHIP_BONAIRE:
2328 case CHIP_HAWAII:
2329 case CHIP_KAVERI:
2330 case CHIP_KABINI:
2331 case CHIP_MULLINS:
2332 case CHIP_TONGA:
2333 case CHIP_FIJI:
2334 case CHIP_CARRIZO:
2335 case CHIP_STONEY:
2336 case CHIP_POLARIS11:
2337 case CHIP_POLARIS10:
2338 case CHIP_POLARIS12:
2339 case CHIP_VEGAM:
2340 case CHIP_VEGA10:
2341 case CHIP_VEGA12:
2342 case CHIP_VEGA20:
2343 return 0;
2344 case CHIP_NAVI12:
2345 fw_name_dmcu = FIRMWARE_NAVI12_DMCU;
2346 break;
2347 case CHIP_RAVEN:
2348 if (ASICREV_IS_PICASSO(adev->external_rev_id))
2349 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
2350 else if (ASICREV_IS_RAVEN2(adev->external_rev_id))
2351 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
2352 else
2353 return 0;
2354 break;
2355 default:
2356 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
2357 case IP_VERSION(2, 0, 2):
2358 case IP_VERSION(2, 0, 3):
2359 case IP_VERSION(2, 0, 0):
2360 case IP_VERSION(2, 1, 0):
2361 case IP_VERSION(3, 0, 0):
2362 case IP_VERSION(3, 0, 2):
2363 case IP_VERSION(3, 0, 3):
2364 case IP_VERSION(3, 0, 1):
2365 case IP_VERSION(3, 1, 2):
2366 case IP_VERSION(3, 1, 3):
2367 case IP_VERSION(3, 1, 4):
2368 case IP_VERSION(3, 1, 5):
2369 case IP_VERSION(3, 1, 6):
2370 case IP_VERSION(3, 2, 0):
2371 case IP_VERSION(3, 2, 1):
2372 case IP_VERSION(3, 5, 0):
2373 case IP_VERSION(3, 5, 1):
2374 case IP_VERSION(3, 6, 0):
2375 case IP_VERSION(4, 0, 1):
2376 return 0;
2377 default:
2378 break;
2379 }
2380 drm_err(adev_to_drm(adev), "Unsupported ASIC type: 0x%X\n", adev->asic_type);
2381 return -EINVAL;
2382 }
2383
2384 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
2385 DRM_DEBUG_KMS("dm: DMCU firmware not supported on direct or SMU loading\n");
2386 return 0;
2387 }
2388
2389 r = amdgpu_ucode_request(adev, &adev->dm.fw_dmcu, AMDGPU_UCODE_REQUIRED,
2390 "%s", fw_name_dmcu);
2391 if (r == -ENODEV) {
2392 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */
2393 DRM_DEBUG_KMS("dm: DMCU firmware not found\n");
2394 adev->dm.fw_dmcu = NULL;
2395 return 0;
2396 }
2397 if (r) {
2398 drm_err(adev_to_drm(adev), "amdgpu_dm: Can't validate firmware \"%s\"\n",
2399 fw_name_dmcu);
2400 amdgpu_ucode_release(&adev->dm.fw_dmcu);
2401 return r;
2402 }
2403
2404 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data;
2405 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM;
2406 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu;
2407 adev->firmware.fw_size +=
2408 ALIGN(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
2409
2410 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV;
2411 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu;
2412 adev->firmware.fw_size +=
2413 ALIGN(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
2414
2415 adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version);
2416
2417 DRM_DEBUG_KMS("PSP loading DMCU firmware\n");
2418
2419 return 0;
2420 }
2421
amdgpu_dm_dmub_reg_read(void * ctx,uint32_t address)2422 static uint32_t amdgpu_dm_dmub_reg_read(void *ctx, uint32_t address)
2423 {
2424 struct amdgpu_device *adev = ctx;
2425
2426 return dm_read_reg(adev->dm.dc->ctx, address);
2427 }
2428
amdgpu_dm_dmub_reg_write(void * ctx,uint32_t address,uint32_t value)2429 static void amdgpu_dm_dmub_reg_write(void *ctx, uint32_t address,
2430 uint32_t value)
2431 {
2432 struct amdgpu_device *adev = ctx;
2433
2434 return dm_write_reg(adev->dm.dc->ctx, address, value);
2435 }
2436
dm_dmub_sw_init(struct amdgpu_device * adev)2437 static int dm_dmub_sw_init(struct amdgpu_device *adev)
2438 {
2439 struct dmub_srv_create_params create_params;
2440 struct dmub_srv_region_params region_params;
2441 struct dmub_srv_region_info region_info;
2442 struct dmub_srv_memory_params memory_params;
2443 struct dmub_srv_fb_info *fb_info;
2444 struct dmub_srv *dmub_srv;
2445 const struct dmcub_firmware_header_v1_0 *hdr;
2446 enum dmub_asic dmub_asic;
2447 enum dmub_status status;
2448 static enum dmub_window_memory_type window_memory_type[DMUB_WINDOW_TOTAL] = {
2449 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_0_INST_CONST
2450 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_1_STACK
2451 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_2_BSS_DATA
2452 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_3_VBIOS
2453 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_4_MAILBOX
2454 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_5_TRACEBUFF
2455 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_6_FW_STATE
2456 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_7_SCRATCH_MEM
2457 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_IB_MEM
2458 DMUB_WINDOW_MEMORY_TYPE_FB, //DMUB_WINDOW_SHARED_STATE
2459 };
2460 int r;
2461
2462 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
2463 case IP_VERSION(2, 1, 0):
2464 dmub_asic = DMUB_ASIC_DCN21;
2465 break;
2466 case IP_VERSION(3, 0, 0):
2467 dmub_asic = DMUB_ASIC_DCN30;
2468 break;
2469 case IP_VERSION(3, 0, 1):
2470 dmub_asic = DMUB_ASIC_DCN301;
2471 break;
2472 case IP_VERSION(3, 0, 2):
2473 dmub_asic = DMUB_ASIC_DCN302;
2474 break;
2475 case IP_VERSION(3, 0, 3):
2476 dmub_asic = DMUB_ASIC_DCN303;
2477 break;
2478 case IP_VERSION(3, 1, 2):
2479 case IP_VERSION(3, 1, 3):
2480 dmub_asic = (adev->external_rev_id == YELLOW_CARP_B0) ? DMUB_ASIC_DCN31B : DMUB_ASIC_DCN31;
2481 break;
2482 case IP_VERSION(3, 1, 4):
2483 dmub_asic = DMUB_ASIC_DCN314;
2484 break;
2485 case IP_VERSION(3, 1, 5):
2486 dmub_asic = DMUB_ASIC_DCN315;
2487 break;
2488 case IP_VERSION(3, 1, 6):
2489 dmub_asic = DMUB_ASIC_DCN316;
2490 break;
2491 case IP_VERSION(3, 2, 0):
2492 dmub_asic = DMUB_ASIC_DCN32;
2493 break;
2494 case IP_VERSION(3, 2, 1):
2495 dmub_asic = DMUB_ASIC_DCN321;
2496 break;
2497 case IP_VERSION(3, 5, 0):
2498 case IP_VERSION(3, 5, 1):
2499 dmub_asic = DMUB_ASIC_DCN35;
2500 break;
2501 case IP_VERSION(3, 6, 0):
2502 dmub_asic = DMUB_ASIC_DCN36;
2503 break;
2504 case IP_VERSION(4, 0, 1):
2505 dmub_asic = DMUB_ASIC_DCN401;
2506 break;
2507
2508 default:
2509 /* ASIC doesn't support DMUB. */
2510 return 0;
2511 }
2512
2513 hdr = (const struct dmcub_firmware_header_v1_0 *)adev->dm.dmub_fw->data;
2514 adev->dm.dmcub_fw_version = le32_to_cpu(hdr->header.ucode_version);
2515
2516 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
2517 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].ucode_id =
2518 AMDGPU_UCODE_ID_DMCUB;
2519 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].fw =
2520 adev->dm.dmub_fw;
2521 adev->firmware.fw_size +=
2522 ALIGN(le32_to_cpu(hdr->inst_const_bytes), PAGE_SIZE);
2523
2524 drm_info(adev_to_drm(adev), "Loading DMUB firmware via PSP: version=0x%08X\n",
2525 adev->dm.dmcub_fw_version);
2526 }
2527
2528
2529 adev->dm.dmub_srv = kzalloc(sizeof(*adev->dm.dmub_srv), GFP_KERNEL);
2530 dmub_srv = adev->dm.dmub_srv;
2531
2532 if (!dmub_srv) {
2533 drm_err(adev_to_drm(adev), "Failed to allocate DMUB service!\n");
2534 return -ENOMEM;
2535 }
2536
2537 memset(&create_params, 0, sizeof(create_params));
2538 create_params.user_ctx = adev;
2539 create_params.funcs.reg_read = amdgpu_dm_dmub_reg_read;
2540 create_params.funcs.reg_write = amdgpu_dm_dmub_reg_write;
2541 create_params.asic = dmub_asic;
2542
2543 /* Create the DMUB service. */
2544 status = dmub_srv_create(dmub_srv, &create_params);
2545 if (status != DMUB_STATUS_OK) {
2546 drm_err(adev_to_drm(adev), "Error creating DMUB service: %d\n", status);
2547 return -EINVAL;
2548 }
2549
2550 /* Calculate the size of all the regions for the DMUB service. */
2551 memset(®ion_params, 0, sizeof(region_params));
2552
2553 region_params.inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
2554 PSP_HEADER_BYTES - PSP_FOOTER_BYTES;
2555 region_params.bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
2556 region_params.vbios_size = adev->bios_size;
2557 region_params.fw_bss_data = region_params.bss_data_size ?
2558 adev->dm.dmub_fw->data +
2559 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
2560 le32_to_cpu(hdr->inst_const_bytes) : NULL;
2561 region_params.fw_inst_const =
2562 adev->dm.dmub_fw->data +
2563 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
2564 PSP_HEADER_BYTES;
2565 region_params.window_memory_type = window_memory_type;
2566
2567 status = dmub_srv_calc_region_info(dmub_srv, ®ion_params,
2568 ®ion_info);
2569
2570 if (status != DMUB_STATUS_OK) {
2571 drm_err(adev_to_drm(adev), "Error calculating DMUB region info: %d\n", status);
2572 return -EINVAL;
2573 }
2574
2575 /*
2576 * Allocate a framebuffer based on the total size of all the regions.
2577 * TODO: Move this into GART.
2578 */
2579 r = amdgpu_bo_create_kernel(adev, region_info.fb_size, PAGE_SIZE,
2580 AMDGPU_GEM_DOMAIN_VRAM |
2581 AMDGPU_GEM_DOMAIN_GTT,
2582 &adev->dm.dmub_bo,
2583 &adev->dm.dmub_bo_gpu_addr,
2584 &adev->dm.dmub_bo_cpu_addr);
2585 if (r)
2586 return r;
2587
2588 /* Rebase the regions on the framebuffer address. */
2589 memset(&memory_params, 0, sizeof(memory_params));
2590 memory_params.cpu_fb_addr = adev->dm.dmub_bo_cpu_addr;
2591 memory_params.gpu_fb_addr = adev->dm.dmub_bo_gpu_addr;
2592 memory_params.region_info = ®ion_info;
2593 memory_params.window_memory_type = window_memory_type;
2594
2595 adev->dm.dmub_fb_info =
2596 kzalloc(sizeof(*adev->dm.dmub_fb_info), GFP_KERNEL);
2597 fb_info = adev->dm.dmub_fb_info;
2598
2599 if (!fb_info) {
2600 drm_err(adev_to_drm(adev),
2601 "Failed to allocate framebuffer info for DMUB service!\n");
2602 return -ENOMEM;
2603 }
2604
2605 status = dmub_srv_calc_mem_info(dmub_srv, &memory_params, fb_info);
2606 if (status != DMUB_STATUS_OK) {
2607 drm_err(adev_to_drm(adev), "Error calculating DMUB FB info: %d\n", status);
2608 return -EINVAL;
2609 }
2610
2611 adev->dm.bb_from_dmub = dm_dmub_get_vbios_bounding_box(adev);
2612
2613 return 0;
2614 }
2615
dm_sw_init(struct amdgpu_ip_block * ip_block)2616 static int dm_sw_init(struct amdgpu_ip_block *ip_block)
2617 {
2618 struct amdgpu_device *adev = ip_block->adev;
2619 int r;
2620
2621 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
2622
2623 if (!adev->dm.cgs_device) {
2624 drm_err(adev_to_drm(adev), "failed to create cgs device.\n");
2625 return -EINVAL;
2626 }
2627
2628 /* Moved from dm init since we need to use allocations for storing bounding box data */
2629 INIT_LIST_HEAD(&adev->dm.da_list);
2630
2631 r = dm_dmub_sw_init(adev);
2632 if (r)
2633 return r;
2634
2635 return load_dmcu_fw(adev);
2636 }
2637
dm_sw_fini(struct amdgpu_ip_block * ip_block)2638 static int dm_sw_fini(struct amdgpu_ip_block *ip_block)
2639 {
2640 struct amdgpu_device *adev = ip_block->adev;
2641 struct dal_allocation *da;
2642
2643 list_for_each_entry(da, &adev->dm.da_list, list) {
2644 if (adev->dm.bb_from_dmub == (void *) da->cpu_ptr) {
2645 amdgpu_bo_free_kernel(&da->bo, &da->gpu_addr, &da->cpu_ptr);
2646 list_del(&da->list);
2647 kfree(da);
2648 adev->dm.bb_from_dmub = NULL;
2649 break;
2650 }
2651 }
2652
2653
2654 kfree(adev->dm.dmub_fb_info);
2655 adev->dm.dmub_fb_info = NULL;
2656
2657 if (adev->dm.dmub_srv) {
2658 dmub_srv_destroy(adev->dm.dmub_srv);
2659 kfree(adev->dm.dmub_srv);
2660 adev->dm.dmub_srv = NULL;
2661 }
2662
2663 amdgpu_ucode_release(&adev->dm.dmub_fw);
2664 amdgpu_ucode_release(&adev->dm.fw_dmcu);
2665
2666 return 0;
2667 }
2668
detect_mst_link_for_all_connectors(struct drm_device * dev)2669 static int detect_mst_link_for_all_connectors(struct drm_device *dev)
2670 {
2671 struct amdgpu_dm_connector *aconnector;
2672 struct drm_connector *connector;
2673 struct drm_connector_list_iter iter;
2674 int ret = 0;
2675
2676 drm_connector_list_iter_begin(dev, &iter);
2677 drm_for_each_connector_iter(connector, &iter) {
2678
2679 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
2680 continue;
2681
2682 aconnector = to_amdgpu_dm_connector(connector);
2683 if (aconnector->dc_link->type == dc_connection_mst_branch &&
2684 aconnector->mst_mgr.aux) {
2685 drm_dbg_kms(dev, "DM_MST: starting TM on aconnector: %p [id: %d]\n",
2686 aconnector,
2687 aconnector->base.base.id);
2688
2689 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
2690 if (ret < 0) {
2691 drm_err(dev, "DM_MST: Failed to start MST\n");
2692 aconnector->dc_link->type =
2693 dc_connection_single;
2694 ret = dm_helpers_dp_mst_stop_top_mgr(aconnector->dc_link->ctx,
2695 aconnector->dc_link);
2696 break;
2697 }
2698 }
2699 }
2700 drm_connector_list_iter_end(&iter);
2701
2702 return ret;
2703 }
2704
dm_late_init(struct amdgpu_ip_block * ip_block)2705 static int dm_late_init(struct amdgpu_ip_block *ip_block)
2706 {
2707 struct amdgpu_device *adev = ip_block->adev;
2708
2709 struct dmcu_iram_parameters params;
2710 unsigned int linear_lut[16];
2711 int i;
2712 struct dmcu *dmcu = NULL;
2713
2714 dmcu = adev->dm.dc->res_pool->dmcu;
2715
2716 for (i = 0; i < 16; i++)
2717 linear_lut[i] = 0xFFFF * i / 15;
2718
2719 params.set = 0;
2720 params.backlight_ramping_override = false;
2721 params.backlight_ramping_start = 0xCCCC;
2722 params.backlight_ramping_reduction = 0xCCCCCCCC;
2723 params.backlight_lut_array_size = 16;
2724 params.backlight_lut_array = linear_lut;
2725
2726 /* Min backlight level after ABM reduction, Don't allow below 1%
2727 * 0xFFFF x 0.01 = 0x28F
2728 */
2729 params.min_abm_backlight = 0x28F;
2730 /* In the case where abm is implemented on dmcub,
2731 * dmcu object will be null.
2732 * ABM 2.4 and up are implemented on dmcub.
2733 */
2734 if (dmcu) {
2735 if (!dmcu_load_iram(dmcu, params))
2736 return -EINVAL;
2737 } else if (adev->dm.dc->ctx->dmub_srv) {
2738 struct dc_link *edp_links[MAX_NUM_EDP];
2739 int edp_num;
2740
2741 dc_get_edp_links(adev->dm.dc, edp_links, &edp_num);
2742 for (i = 0; i < edp_num; i++) {
2743 if (!dmub_init_abm_config(adev->dm.dc->res_pool, params, i))
2744 return -EINVAL;
2745 }
2746 }
2747
2748 return detect_mst_link_for_all_connectors(adev_to_drm(adev));
2749 }
2750
resume_mst_branch_status(struct drm_dp_mst_topology_mgr * mgr)2751 static void resume_mst_branch_status(struct drm_dp_mst_topology_mgr *mgr)
2752 {
2753 u8 buf[UUID_SIZE];
2754 guid_t guid;
2755 int ret;
2756
2757 mutex_lock(&mgr->lock);
2758 if (!mgr->mst_primary)
2759 goto out_fail;
2760
2761 if (drm_dp_read_dpcd_caps(mgr->aux, mgr->dpcd) < 0) {
2762 drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n");
2763 goto out_fail;
2764 }
2765
2766 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2767 DP_MST_EN |
2768 DP_UP_REQ_EN |
2769 DP_UPSTREAM_IS_SRC);
2770 if (ret < 0) {
2771 drm_dbg_kms(mgr->dev, "mst write failed - undocked during suspend?\n");
2772 goto out_fail;
2773 }
2774
2775 /* Some hubs forget their guids after they resume */
2776 ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, buf, sizeof(buf));
2777 if (ret != sizeof(buf)) {
2778 drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n");
2779 goto out_fail;
2780 }
2781
2782 import_guid(&guid, buf);
2783
2784 if (guid_is_null(&guid)) {
2785 guid_gen(&guid);
2786 export_guid(buf, &guid);
2787
2788 ret = drm_dp_dpcd_write(mgr->aux, DP_GUID, buf, sizeof(buf));
2789
2790 if (ret != sizeof(buf)) {
2791 drm_dbg_kms(mgr->dev, "check mstb guid failed - undocked during suspend?\n");
2792 goto out_fail;
2793 }
2794 }
2795
2796 guid_copy(&mgr->mst_primary->guid, &guid);
2797
2798 out_fail:
2799 mutex_unlock(&mgr->lock);
2800 }
2801
hdmi_cec_unset_edid(struct amdgpu_dm_connector * aconnector)2802 void hdmi_cec_unset_edid(struct amdgpu_dm_connector *aconnector)
2803 {
2804 struct cec_notifier *n = aconnector->notifier;
2805
2806 if (!n)
2807 return;
2808
2809 cec_notifier_phys_addr_invalidate(n);
2810 }
2811
hdmi_cec_set_edid(struct amdgpu_dm_connector * aconnector)2812 void hdmi_cec_set_edid(struct amdgpu_dm_connector *aconnector)
2813 {
2814 struct drm_connector *connector = &aconnector->base;
2815 struct cec_notifier *n = aconnector->notifier;
2816
2817 if (!n)
2818 return;
2819
2820 cec_notifier_set_phys_addr(n,
2821 connector->display_info.source_physical_address);
2822 }
2823
s3_handle_hdmi_cec(struct drm_device * ddev,bool suspend)2824 static void s3_handle_hdmi_cec(struct drm_device *ddev, bool suspend)
2825 {
2826 struct amdgpu_dm_connector *aconnector;
2827 struct drm_connector *connector;
2828 struct drm_connector_list_iter conn_iter;
2829
2830 drm_connector_list_iter_begin(ddev, &conn_iter);
2831 drm_for_each_connector_iter(connector, &conn_iter) {
2832 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
2833 continue;
2834
2835 aconnector = to_amdgpu_dm_connector(connector);
2836 if (suspend)
2837 hdmi_cec_unset_edid(aconnector);
2838 else
2839 hdmi_cec_set_edid(aconnector);
2840 }
2841 drm_connector_list_iter_end(&conn_iter);
2842 }
2843
s3_handle_mst(struct drm_device * dev,bool suspend)2844 static void s3_handle_mst(struct drm_device *dev, bool suspend)
2845 {
2846 struct amdgpu_dm_connector *aconnector;
2847 struct drm_connector *connector;
2848 struct drm_connector_list_iter iter;
2849 struct drm_dp_mst_topology_mgr *mgr;
2850
2851 drm_connector_list_iter_begin(dev, &iter);
2852 drm_for_each_connector_iter(connector, &iter) {
2853
2854 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
2855 continue;
2856
2857 aconnector = to_amdgpu_dm_connector(connector);
2858 if (aconnector->dc_link->type != dc_connection_mst_branch ||
2859 aconnector->mst_root)
2860 continue;
2861
2862 mgr = &aconnector->mst_mgr;
2863
2864 if (suspend) {
2865 drm_dp_mst_topology_mgr_suspend(mgr);
2866 } else {
2867 /* if extended timeout is supported in hardware,
2868 * default to LTTPR timeout (3.2ms) first as a W/A for DP link layer
2869 * CTS 4.2.1.1 regression introduced by CTS specs requirement update.
2870 */
2871 try_to_configure_aux_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_LTTPR_TIMEOUT_PERIOD);
2872 if (!dp_is_lttpr_present(aconnector->dc_link))
2873 try_to_configure_aux_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_TIMEOUT_PERIOD);
2874
2875 /* TODO: move resume_mst_branch_status() into drm mst resume again
2876 * once topology probing work is pulled out from mst resume into mst
2877 * resume 2nd step. mst resume 2nd step should be called after old
2878 * state getting restored (i.e. drm_atomic_helper_resume()).
2879 */
2880 resume_mst_branch_status(mgr);
2881 }
2882 }
2883 drm_connector_list_iter_end(&iter);
2884 }
2885
amdgpu_dm_smu_write_watermarks_table(struct amdgpu_device * adev)2886 static int amdgpu_dm_smu_write_watermarks_table(struct amdgpu_device *adev)
2887 {
2888 int ret = 0;
2889
2890 /* This interface is for dGPU Navi1x.Linux dc-pplib interface depends
2891 * on window driver dc implementation.
2892 * For Navi1x, clock settings of dcn watermarks are fixed. the settings
2893 * should be passed to smu during boot up and resume from s3.
2894 * boot up: dc calculate dcn watermark clock settings within dc_create,
2895 * dcn20_resource_construct
2896 * then call pplib functions below to pass the settings to smu:
2897 * smu_set_watermarks_for_clock_ranges
2898 * smu_set_watermarks_table
2899 * navi10_set_watermarks_table
2900 * smu_write_watermarks_table
2901 *
2902 * For Renoir, clock settings of dcn watermark are also fixed values.
2903 * dc has implemented different flow for window driver:
2904 * dc_hardware_init / dc_set_power_state
2905 * dcn10_init_hw
2906 * notify_wm_ranges
2907 * set_wm_ranges
2908 * -- Linux
2909 * smu_set_watermarks_for_clock_ranges
2910 * renoir_set_watermarks_table
2911 * smu_write_watermarks_table
2912 *
2913 * For Linux,
2914 * dc_hardware_init -> amdgpu_dm_init
2915 * dc_set_power_state --> dm_resume
2916 *
2917 * therefore, this function apply to navi10/12/14 but not Renoir
2918 * *
2919 */
2920 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
2921 case IP_VERSION(2, 0, 2):
2922 case IP_VERSION(2, 0, 0):
2923 break;
2924 default:
2925 return 0;
2926 }
2927
2928 ret = amdgpu_dpm_write_watermarks_table(adev);
2929 if (ret) {
2930 drm_err(adev_to_drm(adev), "Failed to update WMTABLE!\n");
2931 return ret;
2932 }
2933
2934 return 0;
2935 }
2936
dm_oem_i2c_hw_init(struct amdgpu_device * adev)2937 static int dm_oem_i2c_hw_init(struct amdgpu_device *adev)
2938 {
2939 struct amdgpu_display_manager *dm = &adev->dm;
2940 struct amdgpu_i2c_adapter *oem_i2c;
2941 struct ddc_service *oem_ddc_service;
2942 int r;
2943
2944 oem_ddc_service = dc_get_oem_i2c_device(adev->dm.dc);
2945 if (oem_ddc_service) {
2946 oem_i2c = create_i2c(oem_ddc_service, true);
2947 if (!oem_i2c) {
2948 drm_info(adev_to_drm(adev), "Failed to create oem i2c adapter data\n");
2949 return -ENOMEM;
2950 }
2951
2952 r = devm_i2c_add_adapter(adev->dev, &oem_i2c->base);
2953 if (r) {
2954 drm_info(adev_to_drm(adev), "Failed to register oem i2c\n");
2955 kfree(oem_i2c);
2956 return r;
2957 }
2958 dm->oem_i2c = oem_i2c;
2959 }
2960
2961 return 0;
2962 }
2963
2964 /**
2965 * dm_hw_init() - Initialize DC device
2966 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
2967 *
2968 * Initialize the &struct amdgpu_display_manager device. This involves calling
2969 * the initializers of each DM component, then populating the struct with them.
2970 *
2971 * Although the function implies hardware initialization, both hardware and
2972 * software are initialized here. Splitting them out to their relevant init
2973 * hooks is a future TODO item.
2974 *
2975 * Some notable things that are initialized here:
2976 *
2977 * - Display Core, both software and hardware
2978 * - DC modules that we need (freesync and color management)
2979 * - DRM software states
2980 * - Interrupt sources and handlers
2981 * - Vblank support
2982 * - Debug FS entries, if enabled
2983 */
dm_hw_init(struct amdgpu_ip_block * ip_block)2984 static int dm_hw_init(struct amdgpu_ip_block *ip_block)
2985 {
2986 struct amdgpu_device *adev = ip_block->adev;
2987 int r;
2988
2989 /* Create DAL display manager */
2990 r = amdgpu_dm_init(adev);
2991 if (r)
2992 return r;
2993 amdgpu_dm_hpd_init(adev);
2994
2995 r = dm_oem_i2c_hw_init(adev);
2996 if (r)
2997 drm_info(adev_to_drm(adev), "Failed to add OEM i2c bus\n");
2998
2999 return 0;
3000 }
3001
3002 /**
3003 * dm_hw_fini() - Teardown DC device
3004 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
3005 *
3006 * Teardown components within &struct amdgpu_display_manager that require
3007 * cleanup. This involves cleaning up the DRM device, DC, and any modules that
3008 * were loaded. Also flush IRQ workqueues and disable them.
3009 */
dm_hw_fini(struct amdgpu_ip_block * ip_block)3010 static int dm_hw_fini(struct amdgpu_ip_block *ip_block)
3011 {
3012 struct amdgpu_device *adev = ip_block->adev;
3013
3014 amdgpu_dm_hpd_fini(adev);
3015
3016 amdgpu_dm_irq_fini(adev);
3017 amdgpu_dm_fini(adev);
3018 return 0;
3019 }
3020
3021
dm_gpureset_toggle_interrupts(struct amdgpu_device * adev,struct dc_state * state,bool enable)3022 static void dm_gpureset_toggle_interrupts(struct amdgpu_device *adev,
3023 struct dc_state *state, bool enable)
3024 {
3025 enum dc_irq_source irq_source;
3026 struct amdgpu_crtc *acrtc;
3027 int rc = -EBUSY;
3028 int i = 0;
3029
3030 for (i = 0; i < state->stream_count; i++) {
3031 acrtc = get_crtc_by_otg_inst(
3032 adev, state->stream_status[i].primary_otg_inst);
3033
3034 if (acrtc && state->stream_status[i].plane_count != 0) {
3035 irq_source = IRQ_TYPE_PFLIP + acrtc->otg_inst;
3036 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
3037 if (rc)
3038 drm_warn(adev_to_drm(adev), "Failed to %s pflip interrupts\n",
3039 enable ? "enable" : "disable");
3040
3041 if (dc_supports_vrr(adev->dm.dc->ctx->dce_version)) {
3042 if (enable) {
3043 if (amdgpu_dm_crtc_vrr_active(
3044 to_dm_crtc_state(acrtc->base.state)))
3045 rc = amdgpu_dm_crtc_set_vupdate_irq(
3046 &acrtc->base, true);
3047 } else
3048 rc = amdgpu_dm_crtc_set_vupdate_irq(
3049 &acrtc->base, false);
3050
3051 if (rc)
3052 drm_warn(adev_to_drm(adev), "Failed to %sable vupdate interrupt\n",
3053 enable ? "en" : "dis");
3054 }
3055
3056 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
3057 /* During gpu-reset we disable and then enable vblank irq, so
3058 * don't use amdgpu_irq_get/put() to avoid refcount change.
3059 */
3060 if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
3061 drm_warn(adev_to_drm(adev), "Failed to %sable vblank interrupt\n", enable ? "en" : "dis");
3062 }
3063 }
3064
3065 }
3066
DEFINE_FREE(state_release,struct dc_state *,if (_T)dc_state_release (_T))3067 DEFINE_FREE(state_release, struct dc_state *, if (_T) dc_state_release(_T))
3068
3069 static enum dc_status amdgpu_dm_commit_zero_streams(struct dc *dc)
3070 {
3071 struct dc_state *context __free(state_release) = NULL;
3072 int i;
3073 struct dc_stream_state *del_streams[MAX_PIPES];
3074 int del_streams_count = 0;
3075 struct dc_commit_streams_params params = {};
3076
3077 memset(del_streams, 0, sizeof(del_streams));
3078
3079 context = dc_state_create_current_copy(dc);
3080 if (context == NULL)
3081 return DC_ERROR_UNEXPECTED;
3082
3083 /* First remove from context all streams */
3084 for (i = 0; i < context->stream_count; i++) {
3085 struct dc_stream_state *stream = context->streams[i];
3086
3087 del_streams[del_streams_count++] = stream;
3088 }
3089
3090 /* Remove all planes for removed streams and then remove the streams */
3091 for (i = 0; i < del_streams_count; i++) {
3092 enum dc_status res;
3093
3094 if (!dc_state_rem_all_planes_for_stream(dc, del_streams[i], context))
3095 return DC_FAIL_DETACH_SURFACES;
3096
3097 res = dc_state_remove_stream(dc, context, del_streams[i]);
3098 if (res != DC_OK)
3099 return res;
3100 }
3101
3102 params.streams = context->streams;
3103 params.stream_count = context->stream_count;
3104
3105 return dc_commit_streams(dc, ¶ms);
3106 }
3107
hpd_rx_irq_work_suspend(struct amdgpu_display_manager * dm)3108 static void hpd_rx_irq_work_suspend(struct amdgpu_display_manager *dm)
3109 {
3110 int i;
3111
3112 if (dm->hpd_rx_offload_wq) {
3113 for (i = 0; i < dm->dc->caps.max_links; i++)
3114 flush_workqueue(dm->hpd_rx_offload_wq[i].wq);
3115 }
3116 }
3117
dm_cache_state(struct amdgpu_device * adev)3118 static int dm_cache_state(struct amdgpu_device *adev)
3119 {
3120 int r;
3121
3122 adev->dm.cached_state = drm_atomic_helper_suspend(adev_to_drm(adev));
3123 if (IS_ERR(adev->dm.cached_state)) {
3124 r = PTR_ERR(adev->dm.cached_state);
3125 adev->dm.cached_state = NULL;
3126 }
3127
3128 return adev->dm.cached_state ? 0 : r;
3129 }
3130
dm_destroy_cached_state(struct amdgpu_device * adev)3131 static void dm_destroy_cached_state(struct amdgpu_device *adev)
3132 {
3133 struct amdgpu_display_manager *dm = &adev->dm;
3134 struct drm_device *ddev = adev_to_drm(adev);
3135 struct dm_plane_state *dm_new_plane_state;
3136 struct drm_plane_state *new_plane_state;
3137 struct dm_crtc_state *dm_new_crtc_state;
3138 struct drm_crtc_state *new_crtc_state;
3139 struct drm_plane *plane;
3140 struct drm_crtc *crtc;
3141 int i;
3142
3143 if (!dm->cached_state)
3144 return;
3145
3146 /* Force mode set in atomic commit */
3147 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
3148 new_crtc_state->active_changed = true;
3149 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
3150 reset_freesync_config_for_crtc(dm_new_crtc_state);
3151 }
3152
3153 /*
3154 * atomic_check is expected to create the dc states. We need to release
3155 * them here, since they were duplicated as part of the suspend
3156 * procedure.
3157 */
3158 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
3159 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
3160 if (dm_new_crtc_state->stream) {
3161 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
3162 dc_stream_release(dm_new_crtc_state->stream);
3163 dm_new_crtc_state->stream = NULL;
3164 }
3165 dm_new_crtc_state->base.color_mgmt_changed = true;
3166 }
3167
3168 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
3169 dm_new_plane_state = to_dm_plane_state(new_plane_state);
3170 if (dm_new_plane_state->dc_state) {
3171 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
3172 dc_plane_state_release(dm_new_plane_state->dc_state);
3173 dm_new_plane_state->dc_state = NULL;
3174 }
3175 }
3176
3177 drm_atomic_helper_resume(ddev, dm->cached_state);
3178
3179 dm->cached_state = NULL;
3180 }
3181
dm_suspend(struct amdgpu_ip_block * ip_block)3182 static int dm_suspend(struct amdgpu_ip_block *ip_block)
3183 {
3184 struct amdgpu_device *adev = ip_block->adev;
3185 struct amdgpu_display_manager *dm = &adev->dm;
3186
3187 if (amdgpu_in_reset(adev)) {
3188 enum dc_status res;
3189
3190 mutex_lock(&dm->dc_lock);
3191
3192 dc_allow_idle_optimizations(adev->dm.dc, false);
3193
3194 dm->cached_dc_state = dc_state_create_copy(dm->dc->current_state);
3195
3196 if (dm->cached_dc_state)
3197 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, false);
3198
3199 res = amdgpu_dm_commit_zero_streams(dm->dc);
3200 if (res != DC_OK) {
3201 drm_err(adev_to_drm(adev), "Failed to commit zero streams: %d\n", res);
3202 return -EINVAL;
3203 }
3204
3205 amdgpu_dm_irq_suspend(adev);
3206
3207 hpd_rx_irq_work_suspend(dm);
3208
3209 return 0;
3210 }
3211
3212 if (!adev->dm.cached_state) {
3213 int r = dm_cache_state(adev);
3214
3215 if (r)
3216 return r;
3217 }
3218
3219 s3_handle_hdmi_cec(adev_to_drm(adev), true);
3220
3221 s3_handle_mst(adev_to_drm(adev), true);
3222
3223 amdgpu_dm_irq_suspend(adev);
3224
3225 hpd_rx_irq_work_suspend(dm);
3226
3227 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
3228
3229 if (dm->dc->caps.ips_support && adev->in_s0ix)
3230 dc_allow_idle_optimizations(dm->dc, true);
3231
3232 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D3);
3233
3234 return 0;
3235 }
3236
3237 struct drm_connector *
amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state * state,struct drm_crtc * crtc)3238 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
3239 struct drm_crtc *crtc)
3240 {
3241 u32 i;
3242 struct drm_connector_state *new_con_state;
3243 struct drm_connector *connector;
3244 struct drm_crtc *crtc_from_state;
3245
3246 for_each_new_connector_in_state(state, connector, new_con_state, i) {
3247 crtc_from_state = new_con_state->crtc;
3248
3249 if (crtc_from_state == crtc)
3250 return connector;
3251 }
3252
3253 return NULL;
3254 }
3255
emulated_link_detect(struct dc_link * link)3256 static void emulated_link_detect(struct dc_link *link)
3257 {
3258 struct dc_sink_init_data sink_init_data = { 0 };
3259 struct display_sink_capability sink_caps = { 0 };
3260 enum dc_edid_status edid_status;
3261 struct dc_context *dc_ctx = link->ctx;
3262 struct drm_device *dev = adev_to_drm(dc_ctx->driver_context);
3263 struct dc_sink *sink = NULL;
3264 struct dc_sink *prev_sink = NULL;
3265
3266 link->type = dc_connection_none;
3267 prev_sink = link->local_sink;
3268
3269 if (prev_sink)
3270 dc_sink_release(prev_sink);
3271
3272 switch (link->connector_signal) {
3273 case SIGNAL_TYPE_HDMI_TYPE_A: {
3274 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
3275 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
3276 break;
3277 }
3278
3279 case SIGNAL_TYPE_DVI_SINGLE_LINK: {
3280 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
3281 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
3282 break;
3283 }
3284
3285 case SIGNAL_TYPE_DVI_DUAL_LINK: {
3286 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
3287 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
3288 break;
3289 }
3290
3291 case SIGNAL_TYPE_LVDS: {
3292 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
3293 sink_caps.signal = SIGNAL_TYPE_LVDS;
3294 break;
3295 }
3296
3297 case SIGNAL_TYPE_EDP: {
3298 sink_caps.transaction_type =
3299 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
3300 sink_caps.signal = SIGNAL_TYPE_EDP;
3301 break;
3302 }
3303
3304 case SIGNAL_TYPE_DISPLAY_PORT: {
3305 sink_caps.transaction_type =
3306 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
3307 sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
3308 break;
3309 }
3310
3311 default:
3312 drm_err(dev, "Invalid connector type! signal:%d\n",
3313 link->connector_signal);
3314 return;
3315 }
3316
3317 sink_init_data.link = link;
3318 sink_init_data.sink_signal = sink_caps.signal;
3319
3320 sink = dc_sink_create(&sink_init_data);
3321 if (!sink) {
3322 drm_err(dev, "Failed to create sink!\n");
3323 return;
3324 }
3325
3326 /* dc_sink_create returns a new reference */
3327 link->local_sink = sink;
3328
3329 edid_status = dm_helpers_read_local_edid(
3330 link->ctx,
3331 link,
3332 sink);
3333
3334 if (edid_status != EDID_OK)
3335 drm_err(dev, "Failed to read EDID\n");
3336
3337 }
3338
dm_gpureset_commit_state(struct dc_state * dc_state,struct amdgpu_display_manager * dm)3339 static void dm_gpureset_commit_state(struct dc_state *dc_state,
3340 struct amdgpu_display_manager *dm)
3341 {
3342 struct {
3343 struct dc_surface_update surface_updates[MAX_SURFACES];
3344 struct dc_plane_info plane_infos[MAX_SURFACES];
3345 struct dc_scaling_info scaling_infos[MAX_SURFACES];
3346 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
3347 struct dc_stream_update stream_update;
3348 } *bundle __free(kfree);
3349 int k, m;
3350
3351 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
3352
3353 if (!bundle) {
3354 drm_err(dm->ddev, "Failed to allocate update bundle\n");
3355 return;
3356 }
3357
3358 for (k = 0; k < dc_state->stream_count; k++) {
3359 bundle->stream_update.stream = dc_state->streams[k];
3360
3361 for (m = 0; m < dc_state->stream_status[k].plane_count; m++) {
3362 bundle->surface_updates[m].surface =
3363 dc_state->stream_status[k].plane_states[m];
3364 bundle->surface_updates[m].surface->force_full_update =
3365 true;
3366 }
3367
3368 update_planes_and_stream_adapter(dm->dc,
3369 UPDATE_TYPE_FULL,
3370 dc_state->stream_status[k].plane_count,
3371 dc_state->streams[k],
3372 &bundle->stream_update,
3373 bundle->surface_updates);
3374 }
3375 }
3376
apply_delay_after_dpcd_poweroff(struct amdgpu_device * adev,struct dc_sink * sink)3377 static void apply_delay_after_dpcd_poweroff(struct amdgpu_device *adev,
3378 struct dc_sink *sink)
3379 {
3380 struct dc_panel_patch *ppatch = NULL;
3381
3382 if (!sink)
3383 return;
3384
3385 ppatch = &sink->edid_caps.panel_patch;
3386 if (ppatch->wait_after_dpcd_poweroff_ms) {
3387 msleep(ppatch->wait_after_dpcd_poweroff_ms);
3388 drm_dbg_driver(adev_to_drm(adev),
3389 "%s: adding a %ds delay as w/a for panel\n",
3390 __func__,
3391 ppatch->wait_after_dpcd_poweroff_ms / 1000);
3392 }
3393 }
3394
3395 /**
3396 * amdgpu_dm_dump_links_and_sinks - Debug dump of all DC links and their sinks
3397 * @adev: amdgpu device pointer
3398 *
3399 * Iterates through all DC links and dumps information about local and remote
3400 * (MST) sinks. Should be called after connector detection is complete to see
3401 * the final state of all links.
3402 */
amdgpu_dm_dump_links_and_sinks(struct amdgpu_device * adev)3403 static void amdgpu_dm_dump_links_and_sinks(struct amdgpu_device *adev)
3404 {
3405 struct dc *dc = adev->dm.dc;
3406 struct drm_device *dev = adev_to_drm(adev);
3407 int li;
3408
3409 if (!dc)
3410 return;
3411
3412 for (li = 0; li < dc->link_count; li++) {
3413 struct dc_link *l = dc->links[li];
3414 const char *name = NULL;
3415 int rs;
3416
3417 if (!l)
3418 continue;
3419 if (l->local_sink && l->local_sink->edid_caps.display_name[0])
3420 name = l->local_sink->edid_caps.display_name;
3421 else
3422 name = "n/a";
3423
3424 drm_dbg_kms(dev,
3425 "LINK_DUMP[%d]: local_sink=%p type=%d sink_signal=%d sink_count=%u edid_name=%s mst_capable=%d mst_alloc_streams=%d\n",
3426 li,
3427 l->local_sink,
3428 l->type,
3429 l->local_sink ? l->local_sink->sink_signal : SIGNAL_TYPE_NONE,
3430 l->sink_count,
3431 name,
3432 l->dpcd_caps.is_mst_capable,
3433 l->mst_stream_alloc_table.stream_count);
3434
3435 /* Dump remote (MST) sinks if any */
3436 for (rs = 0; rs < l->sink_count; rs++) {
3437 struct dc_sink *rsink = l->remote_sinks[rs];
3438 const char *rname = NULL;
3439
3440 if (!rsink)
3441 continue;
3442 if (rsink->edid_caps.display_name[0])
3443 rname = rsink->edid_caps.display_name;
3444 else
3445 rname = "n/a";
3446 drm_dbg_kms(dev,
3447 " REMOTE_SINK[%d:%d]: sink=%p signal=%d edid_name=%s\n",
3448 li, rs,
3449 rsink,
3450 rsink->sink_signal,
3451 rname);
3452 }
3453 }
3454 }
3455
dm_resume(struct amdgpu_ip_block * ip_block)3456 static int dm_resume(struct amdgpu_ip_block *ip_block)
3457 {
3458 struct amdgpu_device *adev = ip_block->adev;
3459 struct drm_device *ddev = adev_to_drm(adev);
3460 struct amdgpu_display_manager *dm = &adev->dm;
3461 struct amdgpu_dm_connector *aconnector;
3462 struct drm_connector *connector;
3463 struct drm_connector_list_iter iter;
3464 struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state);
3465 enum dc_connection_type new_connection_type = dc_connection_none;
3466 struct dc_state *dc_state;
3467 int i, r, j;
3468 struct dc_commit_streams_params commit_params = {};
3469
3470 if (dm->dc->caps.ips_support) {
3471 dc_dmub_srv_apply_idle_power_optimizations(dm->dc, false);
3472 }
3473
3474 if (amdgpu_in_reset(adev)) {
3475 dc_state = dm->cached_dc_state;
3476
3477 /*
3478 * The dc->current_state is backed up into dm->cached_dc_state
3479 * before we commit 0 streams.
3480 *
3481 * DC will clear link encoder assignments on the real state
3482 * but the changes won't propagate over to the copy we made
3483 * before the 0 streams commit.
3484 *
3485 * DC expects that link encoder assignments are *not* valid
3486 * when committing a state, so as a workaround we can copy
3487 * off of the current state.
3488 *
3489 * We lose the previous assignments, but we had already
3490 * commit 0 streams anyway.
3491 */
3492 link_enc_cfg_copy(adev->dm.dc->current_state, dc_state);
3493
3494 r = dm_dmub_hw_init(adev);
3495 if (r) {
3496 drm_err(adev_to_drm(adev), "DMUB interface failed to initialize: status=%d\n", r);
3497 return r;
3498 }
3499
3500 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0);
3501 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
3502
3503 dc_resume(dm->dc);
3504
3505 amdgpu_dm_irq_resume_early(adev);
3506
3507 for (i = 0; i < dc_state->stream_count; i++) {
3508 dc_state->streams[i]->mode_changed = true;
3509 for (j = 0; j < dc_state->stream_status[i].plane_count; j++) {
3510 dc_state->stream_status[i].plane_states[j]->update_flags.raw
3511 = 0xffffffff;
3512 }
3513 }
3514
3515 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
3516 amdgpu_dm_outbox_init(adev);
3517 dc_enable_dmub_outbox(adev->dm.dc);
3518 }
3519
3520 commit_params.streams = dc_state->streams;
3521 commit_params.stream_count = dc_state->stream_count;
3522 dc_exit_ips_for_hw_access(dm->dc);
3523 WARN_ON(!dc_commit_streams(dm->dc, &commit_params));
3524
3525 dm_gpureset_commit_state(dm->cached_dc_state, dm);
3526
3527 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, true);
3528
3529 dc_state_release(dm->cached_dc_state);
3530 dm->cached_dc_state = NULL;
3531
3532 amdgpu_dm_irq_resume_late(adev);
3533
3534 mutex_unlock(&dm->dc_lock);
3535
3536 /* set the backlight after a reset */
3537 for (i = 0; i < dm->num_of_edps; i++) {
3538 if (dm->backlight_dev[i])
3539 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
3540 }
3541
3542 return 0;
3543 }
3544 /* Recreate dc_state - DC invalidates it when setting power state to S3. */
3545 dc_state_release(dm_state->context);
3546 dm_state->context = dc_state_create(dm->dc, NULL);
3547 /* TODO: Remove dc_state->dccg, use dc->dccg directly. */
3548
3549 /* Before powering on DC we need to re-initialize DMUB. */
3550 dm_dmub_hw_resume(adev);
3551
3552 /* Re-enable outbox interrupts for DPIA. */
3553 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
3554 amdgpu_dm_outbox_init(adev);
3555 dc_enable_dmub_outbox(adev->dm.dc);
3556 }
3557
3558 /* power on hardware */
3559 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0);
3560 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
3561
3562 /* program HPD filter */
3563 dc_resume(dm->dc);
3564
3565 /*
3566 * early enable HPD Rx IRQ, should be done before set mode as short
3567 * pulse interrupts are used for MST
3568 */
3569 amdgpu_dm_irq_resume_early(adev);
3570
3571 s3_handle_hdmi_cec(ddev, false);
3572
3573 /* On resume we need to rewrite the MSTM control bits to enable MST*/
3574 s3_handle_mst(ddev, false);
3575
3576 /* Do detection*/
3577 drm_connector_list_iter_begin(ddev, &iter);
3578 drm_for_each_connector_iter(connector, &iter) {
3579 bool ret;
3580
3581 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
3582 continue;
3583
3584 aconnector = to_amdgpu_dm_connector(connector);
3585
3586 if (!aconnector->dc_link)
3587 continue;
3588
3589 /*
3590 * this is the case when traversing through already created end sink
3591 * MST connectors, should be skipped
3592 */
3593 if (aconnector->mst_root)
3594 continue;
3595
3596 guard(mutex)(&aconnector->hpd_lock);
3597 if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type))
3598 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
3599
3600 if (aconnector->base.force && new_connection_type == dc_connection_none) {
3601 emulated_link_detect(aconnector->dc_link);
3602 } else {
3603 guard(mutex)(&dm->dc_lock);
3604 dc_exit_ips_for_hw_access(dm->dc);
3605 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_RESUMEFROMS3S4);
3606 if (ret) {
3607 /* w/a delay for certain panels */
3608 apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink);
3609 }
3610 }
3611
3612 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
3613 aconnector->fake_enable = false;
3614
3615 if (aconnector->dc_sink)
3616 dc_sink_release(aconnector->dc_sink);
3617 aconnector->dc_sink = NULL;
3618 amdgpu_dm_update_connector_after_detect(aconnector);
3619 }
3620 drm_connector_list_iter_end(&iter);
3621
3622 dm_destroy_cached_state(adev);
3623
3624 /* Do mst topology probing after resuming cached state*/
3625 drm_connector_list_iter_begin(ddev, &iter);
3626 drm_for_each_connector_iter(connector, &iter) {
3627 bool init = false;
3628
3629 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
3630 continue;
3631
3632 aconnector = to_amdgpu_dm_connector(connector);
3633 if (aconnector->dc_link->type != dc_connection_mst_branch ||
3634 aconnector->mst_root)
3635 continue;
3636
3637 scoped_guard(mutex, &aconnector->mst_mgr.lock) {
3638 init = !aconnector->mst_mgr.mst_primary;
3639 }
3640 if (init)
3641 dm_helpers_dp_mst_start_top_mgr(aconnector->dc_link->ctx,
3642 aconnector->dc_link, false);
3643 else
3644 drm_dp_mst_topology_queue_probe(&aconnector->mst_mgr);
3645 }
3646 drm_connector_list_iter_end(&iter);
3647
3648 /* Debug dump: list all DC links and their associated sinks after detection
3649 * is complete for all connectors. This provides a comprehensive view of the
3650 * final state without repeating the dump for each connector.
3651 */
3652 amdgpu_dm_dump_links_and_sinks(adev);
3653
3654 amdgpu_dm_irq_resume_late(adev);
3655
3656 amdgpu_dm_smu_write_watermarks_table(adev);
3657
3658 drm_kms_helper_hotplug_event(ddev);
3659
3660 return 0;
3661 }
3662
3663 /**
3664 * DOC: DM Lifecycle
3665 *
3666 * DM (and consequently DC) is registered in the amdgpu base driver as a IP
3667 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to
3668 * the base driver's device list to be initialized and torn down accordingly.
3669 *
3670 * The functions to do so are provided as hooks in &struct amd_ip_funcs.
3671 */
3672
3673 static const struct amd_ip_funcs amdgpu_dm_funcs = {
3674 .name = "dm",
3675 .early_init = dm_early_init,
3676 .late_init = dm_late_init,
3677 .sw_init = dm_sw_init,
3678 .sw_fini = dm_sw_fini,
3679 .early_fini = amdgpu_dm_early_fini,
3680 .hw_init = dm_hw_init,
3681 .hw_fini = dm_hw_fini,
3682 .suspend = dm_suspend,
3683 .resume = dm_resume,
3684 .is_idle = dm_is_idle,
3685 .wait_for_idle = dm_wait_for_idle,
3686 .check_soft_reset = dm_check_soft_reset,
3687 .soft_reset = dm_soft_reset,
3688 .set_clockgating_state = dm_set_clockgating_state,
3689 .set_powergating_state = dm_set_powergating_state,
3690 };
3691
3692 const struct amdgpu_ip_block_version dm_ip_block = {
3693 .type = AMD_IP_BLOCK_TYPE_DCE,
3694 .major = 1,
3695 .minor = 0,
3696 .rev = 0,
3697 .funcs = &amdgpu_dm_funcs,
3698 };
3699
3700
3701 /**
3702 * DOC: atomic
3703 *
3704 * *WIP*
3705 */
3706
3707 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
3708 .fb_create = amdgpu_display_user_framebuffer_create,
3709 .get_format_info = amdgpu_dm_plane_get_format_info,
3710 .atomic_check = amdgpu_dm_atomic_check,
3711 .atomic_commit = drm_atomic_helper_commit,
3712 };
3713
3714 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
3715 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail,
3716 .atomic_commit_setup = amdgpu_dm_atomic_setup_commit,
3717 };
3718
update_connector_ext_caps(struct amdgpu_dm_connector * aconnector)3719 static void update_connector_ext_caps(struct amdgpu_dm_connector *aconnector)
3720 {
3721 const struct drm_panel_backlight_quirk *panel_backlight_quirk;
3722 struct amdgpu_dm_backlight_caps *caps;
3723 struct drm_connector *conn_base;
3724 struct amdgpu_device *adev;
3725 struct drm_luminance_range_info *luminance_range;
3726 struct drm_device *drm;
3727
3728 if (aconnector->bl_idx == -1 ||
3729 aconnector->dc_link->connector_signal != SIGNAL_TYPE_EDP)
3730 return;
3731
3732 conn_base = &aconnector->base;
3733 drm = conn_base->dev;
3734 adev = drm_to_adev(drm);
3735
3736 caps = &adev->dm.backlight_caps[aconnector->bl_idx];
3737 caps->ext_caps = &aconnector->dc_link->dpcd_sink_ext_caps;
3738 caps->aux_support = false;
3739
3740 if (caps->ext_caps->bits.oled == 1
3741 /*
3742 * ||
3743 * caps->ext_caps->bits.sdr_aux_backlight_control == 1 ||
3744 * caps->ext_caps->bits.hdr_aux_backlight_control == 1
3745 */)
3746 caps->aux_support = true;
3747
3748 if (amdgpu_backlight == 0)
3749 caps->aux_support = false;
3750 else if (amdgpu_backlight == 1)
3751 caps->aux_support = true;
3752 if (caps->aux_support)
3753 aconnector->dc_link->backlight_control_type = BACKLIGHT_CONTROL_AMD_AUX;
3754
3755 luminance_range = &conn_base->display_info.luminance_range;
3756
3757 if (luminance_range->max_luminance)
3758 caps->aux_max_input_signal = luminance_range->max_luminance;
3759 else
3760 caps->aux_max_input_signal = 512;
3761
3762 if (luminance_range->min_luminance)
3763 caps->aux_min_input_signal = luminance_range->min_luminance;
3764 else
3765 caps->aux_min_input_signal = 1;
3766
3767 panel_backlight_quirk =
3768 drm_get_panel_backlight_quirk(aconnector->drm_edid);
3769 if (!IS_ERR_OR_NULL(panel_backlight_quirk)) {
3770 if (panel_backlight_quirk->min_brightness) {
3771 caps->min_input_signal =
3772 panel_backlight_quirk->min_brightness - 1;
3773 drm_info(drm,
3774 "Applying panel backlight quirk, min_brightness: %d\n",
3775 caps->min_input_signal);
3776 }
3777 if (panel_backlight_quirk->brightness_mask) {
3778 drm_info(drm,
3779 "Applying panel backlight quirk, brightness_mask: 0x%X\n",
3780 panel_backlight_quirk->brightness_mask);
3781 caps->brightness_mask =
3782 panel_backlight_quirk->brightness_mask;
3783 }
3784 }
3785 }
3786
DEFINE_FREE(sink_release,struct dc_sink *,if (_T)dc_sink_release (_T))3787 DEFINE_FREE(sink_release, struct dc_sink *, if (_T) dc_sink_release(_T))
3788
3789 void amdgpu_dm_update_connector_after_detect(
3790 struct amdgpu_dm_connector *aconnector)
3791 {
3792 struct drm_connector *connector = &aconnector->base;
3793 struct dc_sink *sink __free(sink_release) = NULL;
3794 struct drm_device *dev = connector->dev;
3795
3796 /* MST handled by drm_mst framework */
3797 if (aconnector->mst_mgr.mst_state == true)
3798 return;
3799
3800 sink = aconnector->dc_link->local_sink;
3801 if (sink)
3802 dc_sink_retain(sink);
3803
3804 /*
3805 * Edid mgmt connector gets first update only in mode_valid hook and then
3806 * the connector sink is set to either fake or physical sink depends on link status.
3807 * Skip if already done during boot.
3808 */
3809 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
3810 && aconnector->dc_em_sink) {
3811
3812 /*
3813 * For S3 resume with headless use eml_sink to fake stream
3814 * because on resume connector->sink is set to NULL
3815 */
3816 guard(mutex)(&dev->mode_config.mutex);
3817
3818 if (sink) {
3819 if (aconnector->dc_sink) {
3820 amdgpu_dm_update_freesync_caps(connector, NULL);
3821 /*
3822 * retain and release below are used to
3823 * bump up refcount for sink because the link doesn't point
3824 * to it anymore after disconnect, so on next crtc to connector
3825 * reshuffle by UMD we will get into unwanted dc_sink release
3826 */
3827 dc_sink_release(aconnector->dc_sink);
3828 }
3829 aconnector->dc_sink = sink;
3830 dc_sink_retain(aconnector->dc_sink);
3831 amdgpu_dm_update_freesync_caps(connector,
3832 aconnector->drm_edid);
3833 } else {
3834 amdgpu_dm_update_freesync_caps(connector, NULL);
3835 if (!aconnector->dc_sink) {
3836 aconnector->dc_sink = aconnector->dc_em_sink;
3837 dc_sink_retain(aconnector->dc_sink);
3838 }
3839 }
3840
3841 return;
3842 }
3843
3844 /*
3845 * TODO: temporary guard to look for proper fix
3846 * if this sink is MST sink, we should not do anything
3847 */
3848 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
3849 return;
3850
3851 if (aconnector->dc_sink == sink) {
3852 /*
3853 * We got a DP short pulse (Link Loss, DP CTS, etc...).
3854 * Do nothing!!
3855 */
3856 drm_dbg_kms(dev, "DCHPD: connector_id=%d: dc_sink didn't change.\n",
3857 aconnector->connector_id);
3858 return;
3859 }
3860
3861 drm_dbg_kms(dev, "DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
3862 aconnector->connector_id, aconnector->dc_sink, sink);
3863
3864 /* When polling, DRM has already locked the mutex for us. */
3865 if (!drm_kms_helper_is_poll_worker())
3866 mutex_lock(&dev->mode_config.mutex);
3867
3868 /*
3869 * 1. Update status of the drm connector
3870 * 2. Send an event and let userspace tell us what to do
3871 */
3872 if (sink) {
3873 /*
3874 * TODO: check if we still need the S3 mode update workaround.
3875 * If yes, put it here.
3876 */
3877 if (aconnector->dc_sink) {
3878 amdgpu_dm_update_freesync_caps(connector, NULL);
3879 dc_sink_release(aconnector->dc_sink);
3880 }
3881
3882 aconnector->dc_sink = sink;
3883 dc_sink_retain(aconnector->dc_sink);
3884 if (sink->dc_edid.length == 0) {
3885 aconnector->drm_edid = NULL;
3886 hdmi_cec_unset_edid(aconnector);
3887 if (aconnector->dc_link->aux_mode) {
3888 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
3889 }
3890 } else {
3891 const struct edid *edid = (const struct edid *)sink->dc_edid.raw_edid;
3892
3893 aconnector->drm_edid = drm_edid_alloc(edid, sink->dc_edid.length);
3894 drm_edid_connector_update(connector, aconnector->drm_edid);
3895
3896 hdmi_cec_set_edid(aconnector);
3897 if (aconnector->dc_link->aux_mode)
3898 drm_dp_cec_attach(&aconnector->dm_dp_aux.aux,
3899 connector->display_info.source_physical_address);
3900 }
3901
3902 if (!aconnector->timing_requested) {
3903 aconnector->timing_requested =
3904 kzalloc(sizeof(struct dc_crtc_timing), GFP_KERNEL);
3905 if (!aconnector->timing_requested)
3906 drm_err(dev,
3907 "failed to create aconnector->requested_timing\n");
3908 }
3909
3910 amdgpu_dm_update_freesync_caps(connector, aconnector->drm_edid);
3911 update_connector_ext_caps(aconnector);
3912 } else {
3913 hdmi_cec_unset_edid(aconnector);
3914 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
3915 amdgpu_dm_update_freesync_caps(connector, NULL);
3916 aconnector->num_modes = 0;
3917 dc_sink_release(aconnector->dc_sink);
3918 aconnector->dc_sink = NULL;
3919 drm_edid_free(aconnector->drm_edid);
3920 aconnector->drm_edid = NULL;
3921 kfree(aconnector->timing_requested);
3922 aconnector->timing_requested = NULL;
3923 /* Set CP to DESIRED if it was ENABLED, so we can re-enable it again on hotplug */
3924 if (connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
3925 connector->state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
3926 }
3927
3928 update_subconnector_property(aconnector);
3929
3930 /* When polling, the mutex will be unlocked for us by DRM. */
3931 if (!drm_kms_helper_is_poll_worker())
3932 mutex_unlock(&dev->mode_config.mutex);
3933 }
3934
are_sinks_equal(const struct dc_sink * sink1,const struct dc_sink * sink2)3935 static bool are_sinks_equal(const struct dc_sink *sink1, const struct dc_sink *sink2)
3936 {
3937 if (!sink1 || !sink2)
3938 return false;
3939 if (sink1->sink_signal != sink2->sink_signal)
3940 return false;
3941
3942 if (sink1->dc_edid.length != sink2->dc_edid.length)
3943 return false;
3944
3945 if (memcmp(sink1->dc_edid.raw_edid, sink2->dc_edid.raw_edid,
3946 sink1->dc_edid.length) != 0)
3947 return false;
3948 return true;
3949 }
3950
3951
3952 /**
3953 * DOC: hdmi_hpd_debounce_work
3954 *
3955 * HDMI HPD debounce delay in milliseconds. When an HDMI display toggles HPD
3956 * (such as during power save transitions), this delay determines how long to
3957 * wait before processing the HPD event. This allows distinguishing between a
3958 * physical unplug (>hdmi_hpd_debounce_delay)
3959 * and a spontaneous RX HPD toggle (<hdmi_hpd_debounce_delay).
3960 *
3961 * If the toggle is less than this delay, the driver compares sink capabilities
3962 * and permits a hotplug event if they changed.
3963 *
3964 * The default value of 1500ms was chosen based on experimental testing with
3965 * various monitors that exhibit spontaneous HPD toggling behavior.
3966 */
hdmi_hpd_debounce_work(struct work_struct * work)3967 static void hdmi_hpd_debounce_work(struct work_struct *work)
3968 {
3969 struct amdgpu_dm_connector *aconnector =
3970 container_of(to_delayed_work(work), struct amdgpu_dm_connector,
3971 hdmi_hpd_debounce_work);
3972 struct drm_connector *connector = &aconnector->base;
3973 struct drm_device *dev = connector->dev;
3974 struct amdgpu_device *adev = drm_to_adev(dev);
3975 struct dc *dc = aconnector->dc_link->ctx->dc;
3976 bool fake_reconnect = false;
3977 bool reallow_idle = false;
3978 bool ret = false;
3979 guard(mutex)(&aconnector->hpd_lock);
3980
3981 /* Re-detect the display */
3982 scoped_guard(mutex, &adev->dm.dc_lock) {
3983 if (dc->caps.ips_support && dc->ctx->dmub_srv->idle_allowed) {
3984 dc_allow_idle_optimizations(dc, false);
3985 reallow_idle = true;
3986 }
3987 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
3988 }
3989
3990 if (ret) {
3991 /* Apply workaround delay for certain panels */
3992 apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink);
3993 /* Compare sinks to determine if this was a spontaneous HPD toggle */
3994 if (are_sinks_equal(aconnector->dc_link->local_sink, aconnector->hdmi_prev_sink)) {
3995 /*
3996 * Sinks match - this was a spontaneous HDMI HPD toggle.
3997 */
3998 drm_dbg_kms(dev, "HDMI HPD: Sink unchanged after debounce, internal re-enable\n");
3999 fake_reconnect = true;
4000 }
4001
4002 /* Update connector state */
4003 amdgpu_dm_update_connector_after_detect(aconnector);
4004
4005 drm_modeset_lock_all(dev);
4006 dm_restore_drm_connector_state(dev, connector);
4007 drm_modeset_unlock_all(dev);
4008
4009 /* Only notify OS if sink actually changed */
4010 if (!fake_reconnect && aconnector->base.force == DRM_FORCE_UNSPECIFIED)
4011 drm_kms_helper_hotplug_event(dev);
4012 }
4013
4014 /* Release the cached sink reference */
4015 if (aconnector->hdmi_prev_sink) {
4016 dc_sink_release(aconnector->hdmi_prev_sink);
4017 aconnector->hdmi_prev_sink = NULL;
4018 }
4019
4020 scoped_guard(mutex, &adev->dm.dc_lock) {
4021 if (reallow_idle && dc->caps.ips_support)
4022 dc_allow_idle_optimizations(dc, true);
4023 }
4024 }
4025
handle_hpd_irq_helper(struct amdgpu_dm_connector * aconnector)4026 static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector)
4027 {
4028 struct drm_connector *connector = &aconnector->base;
4029 struct drm_device *dev = connector->dev;
4030 enum dc_connection_type new_connection_type = dc_connection_none;
4031 struct amdgpu_device *adev = drm_to_adev(dev);
4032 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
4033 struct dc *dc = aconnector->dc_link->ctx->dc;
4034 bool ret = false;
4035 bool debounce_required = false;
4036
4037 if (adev->dm.disable_hpd_irq)
4038 return;
4039
4040 /*
4041 * In case of failure or MST no need to update connector status or notify the OS
4042 * since (for MST case) MST does this in its own context.
4043 */
4044 guard(mutex)(&aconnector->hpd_lock);
4045
4046 if (adev->dm.hdcp_workqueue) {
4047 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
4048 dm_con_state->update_hdcp = true;
4049 }
4050 if (aconnector->fake_enable)
4051 aconnector->fake_enable = false;
4052
4053 aconnector->timing_changed = false;
4054
4055 if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type))
4056 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
4057
4058 /*
4059 * Check for HDMI disconnect with debounce enabled.
4060 */
4061 debounce_required = (aconnector->hdmi_hpd_debounce_delay_ms > 0 &&
4062 dc_is_hdmi_signal(aconnector->dc_link->connector_signal) &&
4063 new_connection_type == dc_connection_none &&
4064 aconnector->dc_link->local_sink != NULL);
4065
4066 if (aconnector->base.force && new_connection_type == dc_connection_none) {
4067 emulated_link_detect(aconnector->dc_link);
4068
4069 drm_modeset_lock_all(dev);
4070 dm_restore_drm_connector_state(dev, connector);
4071 drm_modeset_unlock_all(dev);
4072
4073 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
4074 drm_kms_helper_connector_hotplug_event(connector);
4075 } else if (debounce_required) {
4076 /*
4077 * HDMI disconnect detected - schedule delayed work instead of
4078 * processing immediately. This allows us to coalesce spurious
4079 * HDMI signals from physical unplugs.
4080 */
4081 drm_dbg_kms(dev, "HDMI HPD: Disconnect detected, scheduling debounce work (%u ms)\n",
4082 aconnector->hdmi_hpd_debounce_delay_ms);
4083
4084 /* Cache the current sink for later comparison */
4085 if (aconnector->hdmi_prev_sink)
4086 dc_sink_release(aconnector->hdmi_prev_sink);
4087 aconnector->hdmi_prev_sink = aconnector->dc_link->local_sink;
4088 if (aconnector->hdmi_prev_sink)
4089 dc_sink_retain(aconnector->hdmi_prev_sink);
4090
4091 /* Schedule delayed detection. */
4092 if (mod_delayed_work(system_wq,
4093 &aconnector->hdmi_hpd_debounce_work,
4094 msecs_to_jiffies(aconnector->hdmi_hpd_debounce_delay_ms)))
4095 drm_dbg_kms(dev, "HDMI HPD: Re-scheduled debounce work\n");
4096
4097 } else {
4098
4099 /* If the aconnector->hdmi_hpd_debounce_work is scheduled, exit early */
4100 if (delayed_work_pending(&aconnector->hdmi_hpd_debounce_work))
4101 return;
4102
4103 scoped_guard(mutex, &adev->dm.dc_lock) {
4104 dc_exit_ips_for_hw_access(dc);
4105 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
4106 }
4107 if (ret) {
4108 /* w/a delay for certain panels */
4109 apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink);
4110 amdgpu_dm_update_connector_after_detect(aconnector);
4111
4112 drm_modeset_lock_all(dev);
4113 dm_restore_drm_connector_state(dev, connector);
4114 drm_modeset_unlock_all(dev);
4115
4116 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
4117 drm_kms_helper_connector_hotplug_event(connector);
4118 }
4119 }
4120 }
4121
handle_hpd_irq(void * param)4122 static void handle_hpd_irq(void *param)
4123 {
4124 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
4125
4126 handle_hpd_irq_helper(aconnector);
4127
4128 }
4129
schedule_hpd_rx_offload_work(struct amdgpu_device * adev,struct hpd_rx_irq_offload_work_queue * offload_wq,union hpd_irq_data hpd_irq_data)4130 static void schedule_hpd_rx_offload_work(struct amdgpu_device *adev, struct hpd_rx_irq_offload_work_queue *offload_wq,
4131 union hpd_irq_data hpd_irq_data)
4132 {
4133 struct hpd_rx_irq_offload_work *offload_work =
4134 kzalloc(sizeof(*offload_work), GFP_KERNEL);
4135
4136 if (!offload_work) {
4137 drm_err(adev_to_drm(adev), "Failed to allocate hpd_rx_irq_offload_work.\n");
4138 return;
4139 }
4140
4141 INIT_WORK(&offload_work->work, dm_handle_hpd_rx_offload_work);
4142 offload_work->data = hpd_irq_data;
4143 offload_work->offload_wq = offload_wq;
4144 offload_work->adev = adev;
4145
4146 queue_work(offload_wq->wq, &offload_work->work);
4147 DRM_DEBUG_KMS("queue work to handle hpd_rx offload work");
4148 }
4149
handle_hpd_rx_irq(void * param)4150 static void handle_hpd_rx_irq(void *param)
4151 {
4152 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
4153 struct drm_connector *connector = &aconnector->base;
4154 struct drm_device *dev = connector->dev;
4155 struct dc_link *dc_link = aconnector->dc_link;
4156 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
4157 bool result = false;
4158 enum dc_connection_type new_connection_type = dc_connection_none;
4159 struct amdgpu_device *adev = drm_to_adev(dev);
4160 union hpd_irq_data hpd_irq_data;
4161 bool link_loss = false;
4162 bool has_left_work = false;
4163 int idx = dc_link->link_index;
4164 struct hpd_rx_irq_offload_work_queue *offload_wq = &adev->dm.hpd_rx_offload_wq[idx];
4165 struct dc *dc = aconnector->dc_link->ctx->dc;
4166
4167 memset(&hpd_irq_data, 0, sizeof(hpd_irq_data));
4168
4169 if (adev->dm.disable_hpd_irq)
4170 return;
4171
4172 /*
4173 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio
4174 * conflict, after implement i2c helper, this mutex should be
4175 * retired.
4176 */
4177 mutex_lock(&aconnector->hpd_lock);
4178
4179 result = dc_link_handle_hpd_rx_irq(dc_link, &hpd_irq_data,
4180 &link_loss, true, &has_left_work);
4181
4182 if (!has_left_work)
4183 goto out;
4184
4185 if (hpd_irq_data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
4186 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
4187 goto out;
4188 }
4189
4190 if (dc_link_dp_allow_hpd_rx_irq(dc_link)) {
4191 if (hpd_irq_data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY ||
4192 hpd_irq_data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) {
4193 bool skip = false;
4194
4195 /*
4196 * DOWN_REP_MSG_RDY is also handled by polling method
4197 * mgr->cbs->poll_hpd_irq()
4198 */
4199 spin_lock(&offload_wq->offload_lock);
4200 skip = offload_wq->is_handling_mst_msg_rdy_event;
4201
4202 if (!skip)
4203 offload_wq->is_handling_mst_msg_rdy_event = true;
4204
4205 spin_unlock(&offload_wq->offload_lock);
4206
4207 if (!skip)
4208 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
4209
4210 goto out;
4211 }
4212
4213 if (link_loss) {
4214 bool skip = false;
4215
4216 spin_lock(&offload_wq->offload_lock);
4217 skip = offload_wq->is_handling_link_loss;
4218
4219 if (!skip)
4220 offload_wq->is_handling_link_loss = true;
4221
4222 spin_unlock(&offload_wq->offload_lock);
4223
4224 if (!skip)
4225 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
4226
4227 goto out;
4228 }
4229 }
4230
4231 out:
4232 if (result && !is_mst_root_connector) {
4233 /* Downstream Port status changed. */
4234 if (!dc_link_detect_connection_type(dc_link, &new_connection_type))
4235 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
4236
4237 if (aconnector->base.force && new_connection_type == dc_connection_none) {
4238 emulated_link_detect(dc_link);
4239
4240 if (aconnector->fake_enable)
4241 aconnector->fake_enable = false;
4242
4243 amdgpu_dm_update_connector_after_detect(aconnector);
4244
4245
4246 drm_modeset_lock_all(dev);
4247 dm_restore_drm_connector_state(dev, connector);
4248 drm_modeset_unlock_all(dev);
4249
4250 drm_kms_helper_connector_hotplug_event(connector);
4251 } else {
4252 bool ret = false;
4253
4254 mutex_lock(&adev->dm.dc_lock);
4255 dc_exit_ips_for_hw_access(dc);
4256 ret = dc_link_detect(dc_link, DETECT_REASON_HPDRX);
4257 mutex_unlock(&adev->dm.dc_lock);
4258
4259 if (ret) {
4260 if (aconnector->fake_enable)
4261 aconnector->fake_enable = false;
4262
4263 amdgpu_dm_update_connector_after_detect(aconnector);
4264
4265 drm_modeset_lock_all(dev);
4266 dm_restore_drm_connector_state(dev, connector);
4267 drm_modeset_unlock_all(dev);
4268
4269 drm_kms_helper_connector_hotplug_event(connector);
4270 }
4271 }
4272 }
4273 if (hpd_irq_data.bytes.device_service_irq.bits.CP_IRQ) {
4274 if (adev->dm.hdcp_workqueue)
4275 hdcp_handle_cpirq(adev->dm.hdcp_workqueue, aconnector->base.index);
4276 }
4277
4278 if (dc_link->type != dc_connection_mst_branch)
4279 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux);
4280
4281 mutex_unlock(&aconnector->hpd_lock);
4282 }
4283
register_hpd_handlers(struct amdgpu_device * adev)4284 static int register_hpd_handlers(struct amdgpu_device *adev)
4285 {
4286 struct drm_device *dev = adev_to_drm(adev);
4287 struct drm_connector *connector;
4288 struct amdgpu_dm_connector *aconnector;
4289 const struct dc_link *dc_link;
4290 struct dc_interrupt_params int_params = {0};
4291
4292 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4293 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4294
4295 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
4296 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD,
4297 dmub_hpd_callback, true)) {
4298 drm_err(adev_to_drm(adev), "fail to register dmub hpd callback");
4299 return -EINVAL;
4300 }
4301
4302 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_IRQ,
4303 dmub_hpd_callback, true)) {
4304 drm_err(adev_to_drm(adev), "fail to register dmub hpd callback");
4305 return -EINVAL;
4306 }
4307
4308 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_SENSE_NOTIFY,
4309 dmub_hpd_sense_callback, true)) {
4310 drm_err(adev_to_drm(adev), "fail to register dmub hpd sense callback");
4311 return -EINVAL;
4312 }
4313 }
4314
4315 list_for_each_entry(connector,
4316 &dev->mode_config.connector_list, head) {
4317
4318 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
4319 continue;
4320
4321 aconnector = to_amdgpu_dm_connector(connector);
4322 dc_link = aconnector->dc_link;
4323
4324 if (dc_link->irq_source_hpd != DC_IRQ_SOURCE_INVALID) {
4325 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
4326 int_params.irq_source = dc_link->irq_source_hpd;
4327
4328 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4329 int_params.irq_source < DC_IRQ_SOURCE_HPD1 ||
4330 int_params.irq_source > DC_IRQ_SOURCE_HPD6) {
4331 drm_err(adev_to_drm(adev), "Failed to register hpd irq!\n");
4332 return -EINVAL;
4333 }
4334
4335 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4336 handle_hpd_irq, (void *) aconnector))
4337 return -ENOMEM;
4338 }
4339
4340 if (dc_link->irq_source_hpd_rx != DC_IRQ_SOURCE_INVALID) {
4341
4342 /* Also register for DP short pulse (hpd_rx). */
4343 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
4344 int_params.irq_source = dc_link->irq_source_hpd_rx;
4345
4346 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4347 int_params.irq_source < DC_IRQ_SOURCE_HPD1RX ||
4348 int_params.irq_source > DC_IRQ_SOURCE_HPD6RX) {
4349 drm_err(adev_to_drm(adev), "Failed to register hpd rx irq!\n");
4350 return -EINVAL;
4351 }
4352
4353 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4354 handle_hpd_rx_irq, (void *) aconnector))
4355 return -ENOMEM;
4356 }
4357 }
4358 return 0;
4359 }
4360
4361 #if defined(CONFIG_DRM_AMD_DC_SI)
4362 /* Register IRQ sources and initialize IRQ callbacks */
dce60_register_irq_handlers(struct amdgpu_device * adev)4363 static int dce60_register_irq_handlers(struct amdgpu_device *adev)
4364 {
4365 struct dc *dc = adev->dm.dc;
4366 struct common_irq_params *c_irq_params;
4367 struct dc_interrupt_params int_params = {0};
4368 int r;
4369 int i;
4370 unsigned int client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
4371
4372 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4373 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4374
4375 /*
4376 * Actions of amdgpu_irq_add_id():
4377 * 1. Register a set() function with base driver.
4378 * Base driver will call set() function to enable/disable an
4379 * interrupt in DC hardware.
4380 * 2. Register amdgpu_dm_irq_handler().
4381 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
4382 * coming from DC hardware.
4383 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
4384 * for acknowledging and handling.
4385 */
4386
4387 /* Use VBLANK interrupt */
4388 for (i = 0; i < adev->mode_info.num_crtc; i++) {
4389 r = amdgpu_irq_add_id(adev, client_id, i + 1, &adev->crtc_irq);
4390 if (r) {
4391 drm_err(adev_to_drm(adev), "Failed to add crtc irq id!\n");
4392 return r;
4393 }
4394
4395 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4396 int_params.irq_source =
4397 dc_interrupt_to_irq_source(dc, i + 1, 0);
4398
4399 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4400 int_params.irq_source < DC_IRQ_SOURCE_VBLANK1 ||
4401 int_params.irq_source > DC_IRQ_SOURCE_VBLANK6) {
4402 drm_err(adev_to_drm(adev), "Failed to register vblank irq!\n");
4403 return -EINVAL;
4404 }
4405
4406 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
4407
4408 c_irq_params->adev = adev;
4409 c_irq_params->irq_src = int_params.irq_source;
4410
4411 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4412 dm_crtc_high_irq, c_irq_params))
4413 return -ENOMEM;
4414 }
4415
4416 /* Use GRPH_PFLIP interrupt */
4417 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
4418 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
4419 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
4420 if (r) {
4421 drm_err(adev_to_drm(adev), "Failed to add page flip irq id!\n");
4422 return r;
4423 }
4424
4425 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4426 int_params.irq_source =
4427 dc_interrupt_to_irq_source(dc, i, 0);
4428
4429 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4430 int_params.irq_source < DC_IRQ_SOURCE_PFLIP_FIRST ||
4431 int_params.irq_source > DC_IRQ_SOURCE_PFLIP_LAST) {
4432 drm_err(adev_to_drm(adev), "Failed to register pflip irq!\n");
4433 return -EINVAL;
4434 }
4435
4436 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
4437
4438 c_irq_params->adev = adev;
4439 c_irq_params->irq_src = int_params.irq_source;
4440
4441 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4442 dm_pflip_high_irq, c_irq_params))
4443 return -ENOMEM;
4444 }
4445
4446 /* HPD */
4447 r = amdgpu_irq_add_id(adev, client_id,
4448 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
4449 if (r) {
4450 drm_err(adev_to_drm(adev), "Failed to add hpd irq id!\n");
4451 return r;
4452 }
4453
4454 r = register_hpd_handlers(adev);
4455
4456 return r;
4457 }
4458 #endif
4459
4460 /* Register IRQ sources and initialize IRQ callbacks */
dce110_register_irq_handlers(struct amdgpu_device * adev)4461 static int dce110_register_irq_handlers(struct amdgpu_device *adev)
4462 {
4463 struct dc *dc = adev->dm.dc;
4464 struct common_irq_params *c_irq_params;
4465 struct dc_interrupt_params int_params = {0};
4466 int r;
4467 int i;
4468 unsigned int client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
4469
4470 if (adev->family >= AMDGPU_FAMILY_AI)
4471 client_id = SOC15_IH_CLIENTID_DCE;
4472
4473 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4474 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4475
4476 /*
4477 * Actions of amdgpu_irq_add_id():
4478 * 1. Register a set() function with base driver.
4479 * Base driver will call set() function to enable/disable an
4480 * interrupt in DC hardware.
4481 * 2. Register amdgpu_dm_irq_handler().
4482 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
4483 * coming from DC hardware.
4484 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
4485 * for acknowledging and handling.
4486 */
4487
4488 /* Use VBLANK interrupt */
4489 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
4490 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
4491 if (r) {
4492 drm_err(adev_to_drm(adev), "Failed to add crtc irq id!\n");
4493 return r;
4494 }
4495
4496 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4497 int_params.irq_source =
4498 dc_interrupt_to_irq_source(dc, i, 0);
4499
4500 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4501 int_params.irq_source < DC_IRQ_SOURCE_VBLANK1 ||
4502 int_params.irq_source > DC_IRQ_SOURCE_VBLANK6) {
4503 drm_err(adev_to_drm(adev), "Failed to register vblank irq!\n");
4504 return -EINVAL;
4505 }
4506
4507 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
4508
4509 c_irq_params->adev = adev;
4510 c_irq_params->irq_src = int_params.irq_source;
4511
4512 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4513 dm_crtc_high_irq, c_irq_params))
4514 return -ENOMEM;
4515 }
4516
4517 /* Use VUPDATE interrupt */
4518 for (i = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT; i <= VISLANDS30_IV_SRCID_D6_V_UPDATE_INT; i += 2) {
4519 r = amdgpu_irq_add_id(adev, client_id, i, &adev->vupdate_irq);
4520 if (r) {
4521 drm_err(adev_to_drm(adev), "Failed to add vupdate irq id!\n");
4522 return r;
4523 }
4524
4525 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4526 int_params.irq_source =
4527 dc_interrupt_to_irq_source(dc, i, 0);
4528
4529 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4530 int_params.irq_source < DC_IRQ_SOURCE_VUPDATE1 ||
4531 int_params.irq_source > DC_IRQ_SOURCE_VUPDATE6) {
4532 drm_err(adev_to_drm(adev), "Failed to register vupdate irq!\n");
4533 return -EINVAL;
4534 }
4535
4536 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
4537
4538 c_irq_params->adev = adev;
4539 c_irq_params->irq_src = int_params.irq_source;
4540
4541 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4542 dm_vupdate_high_irq, c_irq_params))
4543 return -ENOMEM;
4544 }
4545
4546 /* Use GRPH_PFLIP interrupt */
4547 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
4548 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
4549 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
4550 if (r) {
4551 drm_err(adev_to_drm(adev), "Failed to add page flip irq id!\n");
4552 return r;
4553 }
4554
4555 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4556 int_params.irq_source =
4557 dc_interrupt_to_irq_source(dc, i, 0);
4558
4559 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4560 int_params.irq_source < DC_IRQ_SOURCE_PFLIP_FIRST ||
4561 int_params.irq_source > DC_IRQ_SOURCE_PFLIP_LAST) {
4562 drm_err(adev_to_drm(adev), "Failed to register pflip irq!\n");
4563 return -EINVAL;
4564 }
4565
4566 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
4567
4568 c_irq_params->adev = adev;
4569 c_irq_params->irq_src = int_params.irq_source;
4570
4571 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4572 dm_pflip_high_irq, c_irq_params))
4573 return -ENOMEM;
4574 }
4575
4576 /* HPD */
4577 r = amdgpu_irq_add_id(adev, client_id,
4578 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
4579 if (r) {
4580 drm_err(adev_to_drm(adev), "Failed to add hpd irq id!\n");
4581 return r;
4582 }
4583
4584 r = register_hpd_handlers(adev);
4585
4586 return r;
4587 }
4588
4589 /* Register IRQ sources and initialize IRQ callbacks */
dcn10_register_irq_handlers(struct amdgpu_device * adev)4590 static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
4591 {
4592 struct dc *dc = adev->dm.dc;
4593 struct common_irq_params *c_irq_params;
4594 struct dc_interrupt_params int_params = {0};
4595 int r;
4596 int i;
4597 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
4598 static const unsigned int vrtl_int_srcid[] = {
4599 DCN_1_0__SRCID__OTG1_VERTICAL_INTERRUPT0_CONTROL,
4600 DCN_1_0__SRCID__OTG2_VERTICAL_INTERRUPT0_CONTROL,
4601 DCN_1_0__SRCID__OTG3_VERTICAL_INTERRUPT0_CONTROL,
4602 DCN_1_0__SRCID__OTG4_VERTICAL_INTERRUPT0_CONTROL,
4603 DCN_1_0__SRCID__OTG5_VERTICAL_INTERRUPT0_CONTROL,
4604 DCN_1_0__SRCID__OTG6_VERTICAL_INTERRUPT0_CONTROL
4605 };
4606 #endif
4607
4608 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4609 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4610
4611 /*
4612 * Actions of amdgpu_irq_add_id():
4613 * 1. Register a set() function with base driver.
4614 * Base driver will call set() function to enable/disable an
4615 * interrupt in DC hardware.
4616 * 2. Register amdgpu_dm_irq_handler().
4617 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
4618 * coming from DC hardware.
4619 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
4620 * for acknowledging and handling.
4621 */
4622
4623 /* Use VSTARTUP interrupt */
4624 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
4625 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
4626 i++) {
4627 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
4628
4629 if (r) {
4630 drm_err(adev_to_drm(adev), "Failed to add crtc irq id!\n");
4631 return r;
4632 }
4633
4634 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4635 int_params.irq_source =
4636 dc_interrupt_to_irq_source(dc, i, 0);
4637
4638 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4639 int_params.irq_source < DC_IRQ_SOURCE_VBLANK1 ||
4640 int_params.irq_source > DC_IRQ_SOURCE_VBLANK6) {
4641 drm_err(adev_to_drm(adev), "Failed to register vblank irq!\n");
4642 return -EINVAL;
4643 }
4644
4645 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
4646
4647 c_irq_params->adev = adev;
4648 c_irq_params->irq_src = int_params.irq_source;
4649
4650 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4651 dm_crtc_high_irq, c_irq_params))
4652 return -ENOMEM;
4653 }
4654
4655 /* Use otg vertical line interrupt */
4656 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
4657 for (i = 0; i <= adev->mode_info.num_crtc - 1; i++) {
4658 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE,
4659 vrtl_int_srcid[i], &adev->vline0_irq);
4660
4661 if (r) {
4662 drm_err(adev_to_drm(adev), "Failed to add vline0 irq id!\n");
4663 return r;
4664 }
4665
4666 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4667 int_params.irq_source =
4668 dc_interrupt_to_irq_source(dc, vrtl_int_srcid[i], 0);
4669
4670 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4671 int_params.irq_source < DC_IRQ_SOURCE_DC1_VLINE0 ||
4672 int_params.irq_source > DC_IRQ_SOURCE_DC6_VLINE0) {
4673 drm_err(adev_to_drm(adev), "Failed to register vline0 irq!\n");
4674 return -EINVAL;
4675 }
4676
4677 c_irq_params = &adev->dm.vline0_params[int_params.irq_source
4678 - DC_IRQ_SOURCE_DC1_VLINE0];
4679
4680 c_irq_params->adev = adev;
4681 c_irq_params->irq_src = int_params.irq_source;
4682
4683 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4684 dm_dcn_vertical_interrupt0_high_irq,
4685 c_irq_params))
4686 return -ENOMEM;
4687 }
4688 #endif
4689
4690 /* Use VUPDATE_NO_LOCK interrupt on DCN, which seems to correspond to
4691 * the regular VUPDATE interrupt on DCE. We want DC_IRQ_SOURCE_VUPDATEx
4692 * to trigger at end of each vblank, regardless of state of the lock,
4693 * matching DCE behaviour.
4694 */
4695 for (i = DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT;
4696 i <= DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT + adev->mode_info.num_crtc - 1;
4697 i++) {
4698 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->vupdate_irq);
4699
4700 if (r) {
4701 drm_err(adev_to_drm(adev), "Failed to add vupdate irq id!\n");
4702 return r;
4703 }
4704
4705 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4706 int_params.irq_source =
4707 dc_interrupt_to_irq_source(dc, i, 0);
4708
4709 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4710 int_params.irq_source < DC_IRQ_SOURCE_VUPDATE1 ||
4711 int_params.irq_source > DC_IRQ_SOURCE_VUPDATE6) {
4712 drm_err(adev_to_drm(adev), "Failed to register vupdate irq!\n");
4713 return -EINVAL;
4714 }
4715
4716 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
4717
4718 c_irq_params->adev = adev;
4719 c_irq_params->irq_src = int_params.irq_source;
4720
4721 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4722 dm_vupdate_high_irq, c_irq_params))
4723 return -ENOMEM;
4724 }
4725
4726 /* Use GRPH_PFLIP interrupt */
4727 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
4728 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + dc->caps.max_otg_num - 1;
4729 i++) {
4730 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
4731 if (r) {
4732 drm_err(adev_to_drm(adev), "Failed to add page flip irq id!\n");
4733 return r;
4734 }
4735
4736 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
4737 int_params.irq_source =
4738 dc_interrupt_to_irq_source(dc, i, 0);
4739
4740 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
4741 int_params.irq_source < DC_IRQ_SOURCE_PFLIP_FIRST ||
4742 int_params.irq_source > DC_IRQ_SOURCE_PFLIP_LAST) {
4743 drm_err(adev_to_drm(adev), "Failed to register pflip irq!\n");
4744 return -EINVAL;
4745 }
4746
4747 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
4748
4749 c_irq_params->adev = adev;
4750 c_irq_params->irq_src = int_params.irq_source;
4751
4752 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4753 dm_pflip_high_irq, c_irq_params))
4754 return -ENOMEM;
4755 }
4756
4757 /* HPD */
4758 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
4759 &adev->hpd_irq);
4760 if (r) {
4761 drm_err(adev_to_drm(adev), "Failed to add hpd irq id!\n");
4762 return r;
4763 }
4764
4765 r = register_hpd_handlers(adev);
4766
4767 return r;
4768 }
4769 /* Register Outbox IRQ sources and initialize IRQ callbacks */
register_outbox_irq_handlers(struct amdgpu_device * adev)4770 static int register_outbox_irq_handlers(struct amdgpu_device *adev)
4771 {
4772 struct dc *dc = adev->dm.dc;
4773 struct common_irq_params *c_irq_params;
4774 struct dc_interrupt_params int_params = {0};
4775 int r, i;
4776
4777 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
4778 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
4779
4780 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT,
4781 &adev->dmub_outbox_irq);
4782 if (r) {
4783 drm_err(adev_to_drm(adev), "Failed to add outbox irq id!\n");
4784 return r;
4785 }
4786
4787 if (dc->ctx->dmub_srv) {
4788 i = DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT;
4789 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
4790 int_params.irq_source =
4791 dc_interrupt_to_irq_source(dc, i, 0);
4792
4793 c_irq_params = &adev->dm.dmub_outbox_params[0];
4794
4795 c_irq_params->adev = adev;
4796 c_irq_params->irq_src = int_params.irq_source;
4797
4798 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
4799 dm_dmub_outbox1_low_irq, c_irq_params))
4800 return -ENOMEM;
4801 }
4802
4803 return 0;
4804 }
4805
4806 /*
4807 * Acquires the lock for the atomic state object and returns
4808 * the new atomic state.
4809 *
4810 * This should only be called during atomic check.
4811 */
dm_atomic_get_state(struct drm_atomic_state * state,struct dm_atomic_state ** dm_state)4812 int dm_atomic_get_state(struct drm_atomic_state *state,
4813 struct dm_atomic_state **dm_state)
4814 {
4815 struct drm_device *dev = state->dev;
4816 struct amdgpu_device *adev = drm_to_adev(dev);
4817 struct amdgpu_display_manager *dm = &adev->dm;
4818 struct drm_private_state *priv_state;
4819
4820 if (*dm_state)
4821 return 0;
4822
4823 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj);
4824 if (IS_ERR(priv_state))
4825 return PTR_ERR(priv_state);
4826
4827 *dm_state = to_dm_atomic_state(priv_state);
4828
4829 return 0;
4830 }
4831
4832 static struct dm_atomic_state *
dm_atomic_get_new_state(struct drm_atomic_state * state)4833 dm_atomic_get_new_state(struct drm_atomic_state *state)
4834 {
4835 struct drm_device *dev = state->dev;
4836 struct amdgpu_device *adev = drm_to_adev(dev);
4837 struct amdgpu_display_manager *dm = &adev->dm;
4838 struct drm_private_obj *obj;
4839 struct drm_private_state *new_obj_state;
4840 int i;
4841
4842 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) {
4843 if (obj->funcs == dm->atomic_obj.funcs)
4844 return to_dm_atomic_state(new_obj_state);
4845 }
4846
4847 return NULL;
4848 }
4849
4850 static struct drm_private_state *
dm_atomic_duplicate_state(struct drm_private_obj * obj)4851 dm_atomic_duplicate_state(struct drm_private_obj *obj)
4852 {
4853 struct dm_atomic_state *old_state, *new_state;
4854
4855 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
4856 if (!new_state)
4857 return NULL;
4858
4859 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base);
4860
4861 old_state = to_dm_atomic_state(obj->state);
4862
4863 if (old_state && old_state->context)
4864 new_state->context = dc_state_create_copy(old_state->context);
4865
4866 if (!new_state->context) {
4867 kfree(new_state);
4868 return NULL;
4869 }
4870
4871 return &new_state->base;
4872 }
4873
dm_atomic_destroy_state(struct drm_private_obj * obj,struct drm_private_state * state)4874 static void dm_atomic_destroy_state(struct drm_private_obj *obj,
4875 struct drm_private_state *state)
4876 {
4877 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
4878
4879 if (dm_state && dm_state->context)
4880 dc_state_release(dm_state->context);
4881
4882 kfree(dm_state);
4883 }
4884
4885 static struct drm_private_state_funcs dm_atomic_state_funcs = {
4886 .atomic_duplicate_state = dm_atomic_duplicate_state,
4887 .atomic_destroy_state = dm_atomic_destroy_state,
4888 };
4889
amdgpu_dm_mode_config_init(struct amdgpu_device * adev)4890 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
4891 {
4892 struct dm_atomic_state *state;
4893 int r;
4894
4895 adev->mode_info.mode_config_initialized = true;
4896
4897 adev_to_drm(adev)->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
4898 adev_to_drm(adev)->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
4899
4900 adev_to_drm(adev)->mode_config.max_width = 16384;
4901 adev_to_drm(adev)->mode_config.max_height = 16384;
4902
4903 adev_to_drm(adev)->mode_config.preferred_depth = 24;
4904 if (adev->asic_type == CHIP_HAWAII)
4905 /* disable prefer shadow for now due to hibernation issues */
4906 adev_to_drm(adev)->mode_config.prefer_shadow = 0;
4907 else
4908 adev_to_drm(adev)->mode_config.prefer_shadow = 1;
4909 /* indicates support for immediate flip */
4910 adev_to_drm(adev)->mode_config.async_page_flip = true;
4911
4912 state = kzalloc(sizeof(*state), GFP_KERNEL);
4913 if (!state)
4914 return -ENOMEM;
4915
4916 state->context = dc_state_create_current_copy(adev->dm.dc);
4917 if (!state->context) {
4918 kfree(state);
4919 return -ENOMEM;
4920 }
4921
4922 drm_atomic_private_obj_init(adev_to_drm(adev),
4923 &adev->dm.atomic_obj,
4924 &state->base,
4925 &dm_atomic_state_funcs);
4926
4927 r = amdgpu_display_modeset_create_props(adev);
4928 if (r) {
4929 dc_state_release(state->context);
4930 kfree(state);
4931 return r;
4932 }
4933
4934 #ifdef AMD_PRIVATE_COLOR
4935 if (amdgpu_dm_create_color_properties(adev)) {
4936 dc_state_release(state->context);
4937 kfree(state);
4938 return -ENOMEM;
4939 }
4940 #endif
4941
4942 r = amdgpu_dm_audio_init(adev);
4943 if (r) {
4944 dc_state_release(state->context);
4945 kfree(state);
4946 return r;
4947 }
4948
4949 return 0;
4950 }
4951
4952 #define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12
4953 #define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255
4954 #define AMDGPU_DM_MIN_SPREAD ((AMDGPU_DM_DEFAULT_MAX_BACKLIGHT - AMDGPU_DM_DEFAULT_MIN_BACKLIGHT) / 2)
4955 #define AUX_BL_DEFAULT_TRANSITION_TIME_MS 50
4956
amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager * dm,int bl_idx)4957 static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm,
4958 int bl_idx)
4959 {
4960 struct amdgpu_dm_backlight_caps *caps = &dm->backlight_caps[bl_idx];
4961
4962 if (caps->caps_valid)
4963 return;
4964
4965 #if defined(CONFIG_ACPI)
4966 amdgpu_acpi_get_backlight_caps(caps);
4967
4968 /* validate the firmware value is sane */
4969 if (caps->caps_valid) {
4970 int spread = caps->max_input_signal - caps->min_input_signal;
4971
4972 if (caps->max_input_signal > AMDGPU_DM_DEFAULT_MAX_BACKLIGHT ||
4973 caps->min_input_signal < 0 ||
4974 spread > AMDGPU_DM_DEFAULT_MAX_BACKLIGHT ||
4975 spread < AMDGPU_DM_MIN_SPREAD) {
4976 DRM_DEBUG_KMS("DM: Invalid backlight caps: min=%d, max=%d\n",
4977 caps->min_input_signal, caps->max_input_signal);
4978 caps->caps_valid = false;
4979 }
4980 }
4981
4982 if (!caps->caps_valid) {
4983 caps->min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
4984 caps->max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
4985 caps->caps_valid = true;
4986 }
4987 #else
4988 if (caps->aux_support)
4989 return;
4990
4991 caps->min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
4992 caps->max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
4993 caps->caps_valid = true;
4994 #endif
4995 }
4996
get_brightness_range(const struct amdgpu_dm_backlight_caps * caps,unsigned int * min,unsigned int * max)4997 static int get_brightness_range(const struct amdgpu_dm_backlight_caps *caps,
4998 unsigned int *min, unsigned int *max)
4999 {
5000 if (!caps)
5001 return 0;
5002
5003 if (caps->aux_support) {
5004 // Firmware limits are in nits, DC API wants millinits.
5005 *max = 1000 * caps->aux_max_input_signal;
5006 *min = 1000 * caps->aux_min_input_signal;
5007 } else {
5008 // Firmware limits are 8-bit, PWM control is 16-bit.
5009 *max = 0x101 * caps->max_input_signal;
5010 *min = 0x101 * caps->min_input_signal;
5011 }
5012 return 1;
5013 }
5014
5015 /* Rescale from [min..max] to [0..AMDGPU_MAX_BL_LEVEL] */
scale_input_to_fw(int min,int max,u64 input)5016 static inline u32 scale_input_to_fw(int min, int max, u64 input)
5017 {
5018 return DIV_ROUND_CLOSEST_ULL(input * AMDGPU_MAX_BL_LEVEL, max - min);
5019 }
5020
5021 /* Rescale from [0..AMDGPU_MAX_BL_LEVEL] to [min..max] */
scale_fw_to_input(int min,int max,u64 input)5022 static inline u32 scale_fw_to_input(int min, int max, u64 input)
5023 {
5024 return min + DIV_ROUND_CLOSEST_ULL(input * (max - min), AMDGPU_MAX_BL_LEVEL);
5025 }
5026
convert_custom_brightness(const struct amdgpu_dm_backlight_caps * caps,unsigned int min,unsigned int max,uint32_t * user_brightness)5027 static void convert_custom_brightness(const struct amdgpu_dm_backlight_caps *caps,
5028 unsigned int min, unsigned int max,
5029 uint32_t *user_brightness)
5030 {
5031 u32 brightness = scale_input_to_fw(min, max, *user_brightness);
5032 u8 lower_signal, upper_signal, upper_lum, lower_lum, lum;
5033 int left, right;
5034
5035 if (amdgpu_dc_debug_mask & DC_DISABLE_CUSTOM_BRIGHTNESS_CURVE)
5036 return;
5037
5038 if (!caps->data_points)
5039 return;
5040
5041 /*
5042 * Handle the case where brightness is below the first data point
5043 * Interpolate between (0,0) and (first_signal, first_lum)
5044 */
5045 if (brightness < caps->luminance_data[0].input_signal) {
5046 lum = DIV_ROUND_CLOSEST(caps->luminance_data[0].luminance * brightness,
5047 caps->luminance_data[0].input_signal);
5048 goto scale;
5049 }
5050
5051 left = 0;
5052 right = caps->data_points - 1;
5053 while (left <= right) {
5054 int mid = left + (right - left) / 2;
5055 u8 signal = caps->luminance_data[mid].input_signal;
5056
5057 /* Exact match found */
5058 if (signal == brightness) {
5059 lum = caps->luminance_data[mid].luminance;
5060 goto scale;
5061 }
5062
5063 if (signal < brightness)
5064 left = mid + 1;
5065 else
5066 right = mid - 1;
5067 }
5068
5069 /* verify bound */
5070 if (left >= caps->data_points)
5071 left = caps->data_points - 1;
5072
5073 /* At this point, left > right */
5074 lower_signal = caps->luminance_data[right].input_signal;
5075 upper_signal = caps->luminance_data[left].input_signal;
5076 lower_lum = caps->luminance_data[right].luminance;
5077 upper_lum = caps->luminance_data[left].luminance;
5078
5079 /* interpolate */
5080 if (right == left || !lower_lum)
5081 lum = upper_lum;
5082 else
5083 lum = lower_lum + DIV_ROUND_CLOSEST((upper_lum - lower_lum) *
5084 (brightness - lower_signal),
5085 upper_signal - lower_signal);
5086 scale:
5087 *user_brightness = scale_fw_to_input(min, max,
5088 DIV_ROUND_CLOSEST(lum * brightness, 101));
5089 }
5090
convert_brightness_from_user(const struct amdgpu_dm_backlight_caps * caps,uint32_t brightness)5091 static u32 convert_brightness_from_user(const struct amdgpu_dm_backlight_caps *caps,
5092 uint32_t brightness)
5093 {
5094 unsigned int min, max;
5095
5096 if (!get_brightness_range(caps, &min, &max))
5097 return brightness;
5098
5099 convert_custom_brightness(caps, min, max, &brightness);
5100
5101 // Rescale 0..max to min..max
5102 return min + DIV_ROUND_CLOSEST_ULL((u64)(max - min) * brightness, max);
5103 }
5104
convert_brightness_to_user(const struct amdgpu_dm_backlight_caps * caps,uint32_t brightness)5105 static u32 convert_brightness_to_user(const struct amdgpu_dm_backlight_caps *caps,
5106 uint32_t brightness)
5107 {
5108 unsigned int min, max;
5109
5110 if (!get_brightness_range(caps, &min, &max))
5111 return brightness;
5112
5113 if (brightness < min)
5114 return 0;
5115 // Rescale min..max to 0..max
5116 return DIV_ROUND_CLOSEST_ULL((u64)max * (brightness - min),
5117 max - min);
5118 }
5119
amdgpu_dm_backlight_set_level(struct amdgpu_display_manager * dm,int bl_idx,u32 user_brightness)5120 static void amdgpu_dm_backlight_set_level(struct amdgpu_display_manager *dm,
5121 int bl_idx,
5122 u32 user_brightness)
5123 {
5124 struct amdgpu_dm_backlight_caps *caps;
5125 struct dc_link *link;
5126 u32 brightness;
5127 bool rc, reallow_idle = false;
5128 struct drm_connector *connector;
5129
5130 list_for_each_entry(connector, &dm->ddev->mode_config.connector_list, head) {
5131 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
5132
5133 if (aconnector->bl_idx != bl_idx)
5134 continue;
5135
5136 /* if connector is off, save the brightness for next time it's on */
5137 if (!aconnector->base.encoder) {
5138 dm->brightness[bl_idx] = user_brightness;
5139 dm->actual_brightness[bl_idx] = 0;
5140 return;
5141 }
5142 }
5143
5144 amdgpu_dm_update_backlight_caps(dm, bl_idx);
5145 caps = &dm->backlight_caps[bl_idx];
5146
5147 dm->brightness[bl_idx] = user_brightness;
5148 /* update scratch register */
5149 if (bl_idx == 0)
5150 amdgpu_atombios_scratch_regs_set_backlight_level(dm->adev, dm->brightness[bl_idx]);
5151 brightness = convert_brightness_from_user(caps, dm->brightness[bl_idx]);
5152 link = (struct dc_link *)dm->backlight_link[bl_idx];
5153
5154 /* Apply brightness quirk */
5155 if (caps->brightness_mask)
5156 brightness |= caps->brightness_mask;
5157
5158 /* Change brightness based on AUX property */
5159 mutex_lock(&dm->dc_lock);
5160 if (dm->dc->caps.ips_support && dm->dc->ctx->dmub_srv->idle_allowed) {
5161 dc_allow_idle_optimizations(dm->dc, false);
5162 reallow_idle = true;
5163 }
5164
5165 if (trace_amdgpu_dm_brightness_enabled()) {
5166 trace_amdgpu_dm_brightness(__builtin_return_address(0),
5167 user_brightness,
5168 brightness,
5169 caps->aux_support,
5170 power_supply_is_system_supplied() > 0);
5171 }
5172
5173 if (caps->aux_support) {
5174 rc = dc_link_set_backlight_level_nits(link, true, brightness,
5175 AUX_BL_DEFAULT_TRANSITION_TIME_MS);
5176 if (!rc)
5177 DRM_DEBUG("DM: Failed to update backlight via AUX on eDP[%d]\n", bl_idx);
5178 } else {
5179 struct set_backlight_level_params backlight_level_params = { 0 };
5180
5181 backlight_level_params.backlight_pwm_u16_16 = brightness;
5182 backlight_level_params.transition_time_in_ms = 0;
5183
5184 rc = dc_link_set_backlight_level(link, &backlight_level_params);
5185 if (!rc)
5186 DRM_DEBUG("DM: Failed to update backlight on eDP[%d]\n", bl_idx);
5187 }
5188
5189 if (dm->dc->caps.ips_support && reallow_idle)
5190 dc_allow_idle_optimizations(dm->dc, true);
5191
5192 mutex_unlock(&dm->dc_lock);
5193
5194 if (rc)
5195 dm->actual_brightness[bl_idx] = user_brightness;
5196 }
5197
amdgpu_dm_backlight_update_status(struct backlight_device * bd)5198 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
5199 {
5200 struct amdgpu_display_manager *dm = bl_get_data(bd);
5201 int i;
5202
5203 for (i = 0; i < dm->num_of_edps; i++) {
5204 if (bd == dm->backlight_dev[i])
5205 break;
5206 }
5207 if (i >= AMDGPU_DM_MAX_NUM_EDP)
5208 i = 0;
5209 amdgpu_dm_backlight_set_level(dm, i, bd->props.brightness);
5210
5211 return 0;
5212 }
5213
amdgpu_dm_backlight_get_level(struct amdgpu_display_manager * dm,int bl_idx)5214 static u32 amdgpu_dm_backlight_get_level(struct amdgpu_display_manager *dm,
5215 int bl_idx)
5216 {
5217 int ret;
5218 struct amdgpu_dm_backlight_caps caps;
5219 struct dc_link *link = (struct dc_link *)dm->backlight_link[bl_idx];
5220
5221 amdgpu_dm_update_backlight_caps(dm, bl_idx);
5222 caps = dm->backlight_caps[bl_idx];
5223
5224 if (caps.aux_support) {
5225 u32 avg, peak;
5226
5227 if (!dc_link_get_backlight_level_nits(link, &avg, &peak))
5228 return dm->brightness[bl_idx];
5229 return convert_brightness_to_user(&caps, avg);
5230 }
5231
5232 ret = dc_link_get_backlight_level(link);
5233
5234 if (ret == DC_ERROR_UNEXPECTED)
5235 return dm->brightness[bl_idx];
5236
5237 return convert_brightness_to_user(&caps, ret);
5238 }
5239
amdgpu_dm_backlight_get_brightness(struct backlight_device * bd)5240 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
5241 {
5242 struct amdgpu_display_manager *dm = bl_get_data(bd);
5243 int i;
5244
5245 for (i = 0; i < dm->num_of_edps; i++) {
5246 if (bd == dm->backlight_dev[i])
5247 break;
5248 }
5249 if (i >= AMDGPU_DM_MAX_NUM_EDP)
5250 i = 0;
5251 return amdgpu_dm_backlight_get_level(dm, i);
5252 }
5253
5254 static const struct backlight_ops amdgpu_dm_backlight_ops = {
5255 .options = BL_CORE_SUSPENDRESUME,
5256 .get_brightness = amdgpu_dm_backlight_get_brightness,
5257 .update_status = amdgpu_dm_backlight_update_status,
5258 };
5259
5260 static void
amdgpu_dm_register_backlight_device(struct amdgpu_dm_connector * aconnector)5261 amdgpu_dm_register_backlight_device(struct amdgpu_dm_connector *aconnector)
5262 {
5263 struct drm_device *drm = aconnector->base.dev;
5264 struct amdgpu_display_manager *dm = &drm_to_adev(drm)->dm;
5265 struct backlight_properties props = { 0 };
5266 struct amdgpu_dm_backlight_caps *caps;
5267 char bl_name[16];
5268 int min, max;
5269 int real_brightness;
5270 int init_brightness;
5271
5272 if (aconnector->bl_idx == -1)
5273 return;
5274
5275 if (!acpi_video_backlight_use_native()) {
5276 drm_info(drm, "Skipping amdgpu DM backlight registration\n");
5277 /* Try registering an ACPI video backlight device instead. */
5278 acpi_video_register_backlight();
5279 return;
5280 }
5281
5282 caps = &dm->backlight_caps[aconnector->bl_idx];
5283 if (get_brightness_range(caps, &min, &max)) {
5284 if (power_supply_is_system_supplied() > 0)
5285 props.brightness = DIV_ROUND_CLOSEST((max - min) * caps->ac_level, 100);
5286 else
5287 props.brightness = DIV_ROUND_CLOSEST((max - min) * caps->dc_level, 100);
5288 /* min is zero, so max needs to be adjusted */
5289 props.max_brightness = max - min;
5290 drm_dbg(drm, "Backlight caps: min: %d, max: %d, ac %d, dc %d\n", min, max,
5291 caps->ac_level, caps->dc_level);
5292 } else
5293 props.brightness = props.max_brightness = MAX_BACKLIGHT_LEVEL;
5294
5295 init_brightness = props.brightness;
5296
5297 if (caps->data_points && !(amdgpu_dc_debug_mask & DC_DISABLE_CUSTOM_BRIGHTNESS_CURVE)) {
5298 drm_info(drm, "Using custom brightness curve\n");
5299 props.scale = BACKLIGHT_SCALE_NON_LINEAR;
5300 } else
5301 props.scale = BACKLIGHT_SCALE_LINEAR;
5302 props.type = BACKLIGHT_RAW;
5303
5304 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
5305 drm->primary->index + aconnector->bl_idx);
5306
5307 dm->backlight_dev[aconnector->bl_idx] =
5308 backlight_device_register(bl_name, aconnector->base.kdev, dm,
5309 &amdgpu_dm_backlight_ops, &props);
5310 dm->brightness[aconnector->bl_idx] = props.brightness;
5311
5312 if (IS_ERR(dm->backlight_dev[aconnector->bl_idx])) {
5313 drm_err(drm, "DM: Backlight registration failed!\n");
5314 dm->backlight_dev[aconnector->bl_idx] = NULL;
5315 } else {
5316 /*
5317 * dm->brightness[x] can be inconsistent just after startup until
5318 * ops.get_brightness is called.
5319 */
5320 real_brightness =
5321 amdgpu_dm_backlight_ops.get_brightness(dm->backlight_dev[aconnector->bl_idx]);
5322
5323 if (real_brightness != init_brightness) {
5324 dm->actual_brightness[aconnector->bl_idx] = real_brightness;
5325 dm->brightness[aconnector->bl_idx] = real_brightness;
5326 }
5327 drm_dbg_driver(drm, "DM: Registered Backlight device: %s\n", bl_name);
5328 }
5329 }
5330
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)5331 static int initialize_plane(struct amdgpu_display_manager *dm,
5332 struct amdgpu_mode_info *mode_info, int plane_id,
5333 enum drm_plane_type plane_type,
5334 const struct dc_plane_cap *plane_cap)
5335 {
5336 struct drm_plane *plane;
5337 unsigned long possible_crtcs;
5338 int ret = 0;
5339
5340 plane = kzalloc(sizeof(struct drm_plane), GFP_KERNEL);
5341 if (!plane) {
5342 drm_err(adev_to_drm(dm->adev), "KMS: Failed to allocate plane\n");
5343 return -ENOMEM;
5344 }
5345 plane->type = plane_type;
5346
5347 /*
5348 * HACK: IGT tests expect that the primary plane for a CRTC
5349 * can only have one possible CRTC. Only expose support for
5350 * any CRTC if they're not going to be used as a primary plane
5351 * for a CRTC - like overlay or underlay planes.
5352 */
5353 possible_crtcs = 1 << plane_id;
5354 if (plane_id >= dm->dc->caps.max_streams)
5355 possible_crtcs = 0xff;
5356
5357 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap);
5358
5359 if (ret) {
5360 drm_err(adev_to_drm(dm->adev), "KMS: Failed to initialize plane\n");
5361 kfree(plane);
5362 return ret;
5363 }
5364
5365 if (mode_info)
5366 mode_info->planes[plane_id] = plane;
5367
5368 return ret;
5369 }
5370
5371
setup_backlight_device(struct amdgpu_display_manager * dm,struct amdgpu_dm_connector * aconnector)5372 static void setup_backlight_device(struct amdgpu_display_manager *dm,
5373 struct amdgpu_dm_connector *aconnector)
5374 {
5375 struct amdgpu_dm_backlight_caps *caps;
5376 struct dc_link *link = aconnector->dc_link;
5377 int bl_idx = dm->num_of_edps;
5378
5379 if (!(link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) ||
5380 link->type == dc_connection_none)
5381 return;
5382
5383 if (dm->num_of_edps >= AMDGPU_DM_MAX_NUM_EDP) {
5384 drm_warn(adev_to_drm(dm->adev), "Too much eDP connections, skipping backlight setup for additional eDPs\n");
5385 return;
5386 }
5387
5388 aconnector->bl_idx = bl_idx;
5389
5390 amdgpu_dm_update_backlight_caps(dm, bl_idx);
5391 dm->backlight_link[bl_idx] = link;
5392 dm->num_of_edps++;
5393
5394 update_connector_ext_caps(aconnector);
5395 caps = &dm->backlight_caps[aconnector->bl_idx];
5396
5397 /* Only offer ABM property when non-OLED and user didn't turn off by module parameter */
5398 if (!caps->ext_caps->bits.oled && amdgpu_dm_abm_level < 0)
5399 drm_object_attach_property(&aconnector->base.base,
5400 dm->adev->mode_info.abm_level_property,
5401 ABM_SYSFS_CONTROL);
5402 }
5403
5404 static void amdgpu_set_panel_orientation(struct drm_connector *connector);
5405
5406 /*
5407 * In this architecture, the association
5408 * connector -> encoder -> crtc
5409 * id not really requried. The crtc and connector will hold the
5410 * display_index as an abstraction to use with DAL component
5411 *
5412 * Returns 0 on success
5413 */
amdgpu_dm_initialize_drm_device(struct amdgpu_device * adev)5414 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
5415 {
5416 struct amdgpu_display_manager *dm = &adev->dm;
5417 s32 i;
5418 struct amdgpu_dm_connector *aconnector = NULL;
5419 struct amdgpu_encoder *aencoder = NULL;
5420 struct amdgpu_mode_info *mode_info = &adev->mode_info;
5421 u32 link_cnt;
5422 s32 primary_planes;
5423 enum dc_connection_type new_connection_type = dc_connection_none;
5424 const struct dc_plane_cap *plane;
5425 bool psr_feature_enabled = false;
5426 bool replay_feature_enabled = false;
5427 int max_overlay = dm->dc->caps.max_slave_planes;
5428
5429 dm->display_indexes_num = dm->dc->caps.max_streams;
5430 /* Update the actual used number of crtc */
5431 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
5432
5433 amdgpu_dm_set_irq_funcs(adev);
5434
5435 link_cnt = dm->dc->caps.max_links;
5436 if (amdgpu_dm_mode_config_init(dm->adev)) {
5437 drm_err(adev_to_drm(adev), "DM: Failed to initialize mode config\n");
5438 return -EINVAL;
5439 }
5440
5441 /* There is one primary plane per CRTC */
5442 primary_planes = dm->dc->caps.max_streams;
5443 if (primary_planes > AMDGPU_MAX_PLANES) {
5444 drm_err(adev_to_drm(adev), "DM: Plane nums out of 6 planes\n");
5445 return -EINVAL;
5446 }
5447
5448 /*
5449 * Initialize primary planes, implicit planes for legacy IOCTLS.
5450 * Order is reversed to match iteration order in atomic check.
5451 */
5452 for (i = (primary_planes - 1); i >= 0; i--) {
5453 plane = &dm->dc->caps.planes[i];
5454
5455 if (initialize_plane(dm, mode_info, i,
5456 DRM_PLANE_TYPE_PRIMARY, plane)) {
5457 drm_err(adev_to_drm(adev), "KMS: Failed to initialize primary plane\n");
5458 goto fail;
5459 }
5460 }
5461
5462 /*
5463 * Initialize overlay planes, index starting after primary planes.
5464 * These planes have a higher DRM index than the primary planes since
5465 * they should be considered as having a higher z-order.
5466 * Order is reversed to match iteration order in atomic check.
5467 *
5468 * Only support DCN for now, and only expose one so we don't encourage
5469 * userspace to use up all the pipes.
5470 */
5471 for (i = 0; i < dm->dc->caps.max_planes; ++i) {
5472 struct dc_plane_cap *plane = &dm->dc->caps.planes[i];
5473
5474 /* Do not create overlay if MPO disabled */
5475 if (amdgpu_dc_debug_mask & DC_DISABLE_MPO)
5476 break;
5477
5478 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL)
5479 continue;
5480
5481 if (!plane->pixel_format_support.argb8888)
5482 continue;
5483
5484 if (max_overlay-- == 0)
5485 break;
5486
5487 if (initialize_plane(dm, NULL, primary_planes + i,
5488 DRM_PLANE_TYPE_OVERLAY, plane)) {
5489 drm_err(adev_to_drm(adev), "KMS: Failed to initialize overlay plane\n");
5490 goto fail;
5491 }
5492 }
5493
5494 for (i = 0; i < dm->dc->caps.max_streams; i++)
5495 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) {
5496 drm_err(adev_to_drm(adev), "KMS: Failed to initialize crtc\n");
5497 goto fail;
5498 }
5499
5500 /* Use Outbox interrupt */
5501 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5502 case IP_VERSION(3, 0, 0):
5503 case IP_VERSION(3, 1, 2):
5504 case IP_VERSION(3, 1, 3):
5505 case IP_VERSION(3, 1, 4):
5506 case IP_VERSION(3, 1, 5):
5507 case IP_VERSION(3, 1, 6):
5508 case IP_VERSION(3, 2, 0):
5509 case IP_VERSION(3, 2, 1):
5510 case IP_VERSION(2, 1, 0):
5511 case IP_VERSION(3, 5, 0):
5512 case IP_VERSION(3, 5, 1):
5513 case IP_VERSION(3, 6, 0):
5514 case IP_VERSION(4, 0, 1):
5515 if (register_outbox_irq_handlers(dm->adev)) {
5516 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5517 goto fail;
5518 }
5519 break;
5520 default:
5521 DRM_DEBUG_KMS("Unsupported DCN IP version for outbox: 0x%X\n",
5522 amdgpu_ip_version(adev, DCE_HWIP, 0));
5523 }
5524
5525 /* Determine whether to enable PSR support by default. */
5526 if (!(amdgpu_dc_debug_mask & DC_DISABLE_PSR)) {
5527 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5528 case IP_VERSION(3, 1, 2):
5529 case IP_VERSION(3, 1, 3):
5530 case IP_VERSION(3, 1, 4):
5531 case IP_VERSION(3, 1, 5):
5532 case IP_VERSION(3, 1, 6):
5533 case IP_VERSION(3, 2, 0):
5534 case IP_VERSION(3, 2, 1):
5535 case IP_VERSION(3, 5, 0):
5536 case IP_VERSION(3, 5, 1):
5537 case IP_VERSION(3, 6, 0):
5538 case IP_VERSION(4, 0, 1):
5539 psr_feature_enabled = true;
5540 break;
5541 default:
5542 psr_feature_enabled = amdgpu_dc_feature_mask & DC_PSR_MASK;
5543 break;
5544 }
5545 }
5546
5547 /* Determine whether to enable Replay support by default. */
5548 if (!(amdgpu_dc_debug_mask & DC_DISABLE_REPLAY)) {
5549 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5550 case IP_VERSION(3, 1, 4):
5551 case IP_VERSION(3, 2, 0):
5552 case IP_VERSION(3, 2, 1):
5553 case IP_VERSION(3, 5, 0):
5554 case IP_VERSION(3, 5, 1):
5555 case IP_VERSION(3, 6, 0):
5556 replay_feature_enabled = true;
5557 break;
5558
5559 default:
5560 replay_feature_enabled = amdgpu_dc_feature_mask & DC_REPLAY_MASK;
5561 break;
5562 }
5563 }
5564
5565 if (link_cnt > MAX_LINKS) {
5566 drm_err(adev_to_drm(adev),
5567 "KMS: Cannot support more than %d display indexes\n",
5568 MAX_LINKS);
5569 goto fail;
5570 }
5571
5572 /* loops over all connectors on the board */
5573 for (i = 0; i < link_cnt; i++) {
5574 struct dc_link *link = NULL;
5575
5576 link = dc_get_link_at_index(dm->dc, i);
5577
5578 if (link->connector_signal == SIGNAL_TYPE_VIRTUAL) {
5579 struct amdgpu_dm_wb_connector *wbcon = kzalloc(sizeof(*wbcon), GFP_KERNEL);
5580
5581 if (!wbcon) {
5582 drm_err(adev_to_drm(adev), "KMS: Failed to allocate writeback connector\n");
5583 continue;
5584 }
5585
5586 if (amdgpu_dm_wb_connector_init(dm, wbcon, i)) {
5587 drm_err(adev_to_drm(adev), "KMS: Failed to initialize writeback connector\n");
5588 kfree(wbcon);
5589 continue;
5590 }
5591
5592 link->psr_settings.psr_feature_enabled = false;
5593 link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
5594
5595 continue;
5596 }
5597
5598 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
5599 if (!aconnector)
5600 goto fail;
5601
5602 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
5603 if (!aencoder)
5604 goto fail;
5605
5606 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
5607 drm_err(adev_to_drm(adev), "KMS: Failed to initialize encoder\n");
5608 goto fail;
5609 }
5610
5611 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
5612 drm_err(adev_to_drm(adev), "KMS: Failed to initialize connector\n");
5613 goto fail;
5614 }
5615
5616 if (dm->hpd_rx_offload_wq)
5617 dm->hpd_rx_offload_wq[aconnector->base.index].aconnector =
5618 aconnector;
5619
5620 if (!dc_link_detect_connection_type(link, &new_connection_type))
5621 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
5622
5623 if (aconnector->base.force && new_connection_type == dc_connection_none) {
5624 emulated_link_detect(link);
5625 amdgpu_dm_update_connector_after_detect(aconnector);
5626 } else {
5627 bool ret = false;
5628
5629 mutex_lock(&dm->dc_lock);
5630 dc_exit_ips_for_hw_access(dm->dc);
5631 ret = dc_link_detect(link, DETECT_REASON_BOOT);
5632 mutex_unlock(&dm->dc_lock);
5633
5634 if (ret) {
5635 amdgpu_dm_update_connector_after_detect(aconnector);
5636 setup_backlight_device(dm, aconnector);
5637
5638 /* Disable PSR if Replay can be enabled */
5639 if (replay_feature_enabled)
5640 if (amdgpu_dm_set_replay_caps(link, aconnector))
5641 psr_feature_enabled = false;
5642
5643 if (psr_feature_enabled) {
5644 amdgpu_dm_set_psr_caps(link);
5645 drm_info(adev_to_drm(adev), "%s: PSR support %d, DC PSR ver %d, sink PSR ver %d DPCD caps 0x%x su_y_granularity %d\n",
5646 aconnector->base.name,
5647 link->psr_settings.psr_feature_enabled,
5648 link->psr_settings.psr_version,
5649 link->dpcd_caps.psr_info.psr_version,
5650 link->dpcd_caps.psr_info.psr_dpcd_caps.raw,
5651 link->dpcd_caps.psr_info.psr2_su_y_granularity_cap);
5652 }
5653 }
5654 }
5655 amdgpu_set_panel_orientation(&aconnector->base);
5656 }
5657
5658 /* Debug dump: list all DC links and their associated sinks after detection
5659 * is complete for all connectors. This provides a comprehensive view of the
5660 * final state without repeating the dump for each connector.
5661 */
5662 amdgpu_dm_dump_links_and_sinks(adev);
5663
5664 /* Software is initialized. Now we can register interrupt handlers. */
5665 switch (adev->asic_type) {
5666 #if defined(CONFIG_DRM_AMD_DC_SI)
5667 case CHIP_TAHITI:
5668 case CHIP_PITCAIRN:
5669 case CHIP_VERDE:
5670 case CHIP_OLAND:
5671 if (dce60_register_irq_handlers(dm->adev)) {
5672 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5673 goto fail;
5674 }
5675 break;
5676 #endif
5677 case CHIP_BONAIRE:
5678 case CHIP_HAWAII:
5679 case CHIP_KAVERI:
5680 case CHIP_KABINI:
5681 case CHIP_MULLINS:
5682 case CHIP_TONGA:
5683 case CHIP_FIJI:
5684 case CHIP_CARRIZO:
5685 case CHIP_STONEY:
5686 case CHIP_POLARIS11:
5687 case CHIP_POLARIS10:
5688 case CHIP_POLARIS12:
5689 case CHIP_VEGAM:
5690 case CHIP_VEGA10:
5691 case CHIP_VEGA12:
5692 case CHIP_VEGA20:
5693 if (dce110_register_irq_handlers(dm->adev)) {
5694 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5695 goto fail;
5696 }
5697 break;
5698 default:
5699 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5700 case IP_VERSION(1, 0, 0):
5701 case IP_VERSION(1, 0, 1):
5702 case IP_VERSION(2, 0, 2):
5703 case IP_VERSION(2, 0, 3):
5704 case IP_VERSION(2, 0, 0):
5705 case IP_VERSION(2, 1, 0):
5706 case IP_VERSION(3, 0, 0):
5707 case IP_VERSION(3, 0, 2):
5708 case IP_VERSION(3, 0, 3):
5709 case IP_VERSION(3, 0, 1):
5710 case IP_VERSION(3, 1, 2):
5711 case IP_VERSION(3, 1, 3):
5712 case IP_VERSION(3, 1, 4):
5713 case IP_VERSION(3, 1, 5):
5714 case IP_VERSION(3, 1, 6):
5715 case IP_VERSION(3, 2, 0):
5716 case IP_VERSION(3, 2, 1):
5717 case IP_VERSION(3, 5, 0):
5718 case IP_VERSION(3, 5, 1):
5719 case IP_VERSION(3, 6, 0):
5720 case IP_VERSION(4, 0, 1):
5721 if (dcn10_register_irq_handlers(dm->adev)) {
5722 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n");
5723 goto fail;
5724 }
5725 break;
5726 default:
5727 drm_err(adev_to_drm(adev), "Unsupported DCE IP versions: 0x%X\n",
5728 amdgpu_ip_version(adev, DCE_HWIP, 0));
5729 goto fail;
5730 }
5731 break;
5732 }
5733
5734 return 0;
5735 fail:
5736 kfree(aencoder);
5737 kfree(aconnector);
5738
5739 return -EINVAL;
5740 }
5741
amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager * dm)5742 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
5743 {
5744 if (dm->atomic_obj.state)
5745 drm_atomic_private_obj_fini(&dm->atomic_obj);
5746 }
5747
5748 /******************************************************************************
5749 * amdgpu_display_funcs functions
5750 *****************************************************************************/
5751
5752 /*
5753 * dm_bandwidth_update - program display watermarks
5754 *
5755 * @adev: amdgpu_device pointer
5756 *
5757 * Calculate and program the display watermarks and line buffer allocation.
5758 */
dm_bandwidth_update(struct amdgpu_device * adev)5759 static void dm_bandwidth_update(struct amdgpu_device *adev)
5760 {
5761 /* TODO: implement later */
5762 }
5763
5764 static const struct amdgpu_display_funcs dm_display_funcs = {
5765 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
5766 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
5767 .backlight_set_level = NULL, /* never called for DC */
5768 .backlight_get_level = NULL, /* never called for DC */
5769 .hpd_sense = NULL,/* called unconditionally */
5770 .hpd_set_polarity = NULL, /* called unconditionally */
5771 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
5772 .page_flip_get_scanoutpos =
5773 dm_crtc_get_scanoutpos,/* called unconditionally */
5774 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
5775 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
5776 };
5777
5778 #if defined(CONFIG_DEBUG_KERNEL_DC)
5779
s3_debug_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)5780 static ssize_t s3_debug_store(struct device *device,
5781 struct device_attribute *attr,
5782 const char *buf,
5783 size_t count)
5784 {
5785 int ret;
5786 int s3_state;
5787 struct drm_device *drm_dev = dev_get_drvdata(device);
5788 struct amdgpu_device *adev = drm_to_adev(drm_dev);
5789 struct amdgpu_ip_block *ip_block;
5790
5791 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_DCE);
5792 if (!ip_block)
5793 return -EINVAL;
5794
5795 ret = kstrtoint(buf, 0, &s3_state);
5796
5797 if (ret == 0) {
5798 if (s3_state) {
5799 dm_resume(ip_block);
5800 drm_kms_helper_hotplug_event(adev_to_drm(adev));
5801 } else
5802 dm_suspend(ip_block);
5803 }
5804
5805 return ret == 0 ? count : 0;
5806 }
5807
5808 DEVICE_ATTR_WO(s3_debug);
5809
5810 #endif
5811
dm_init_microcode(struct amdgpu_device * adev)5812 static int dm_init_microcode(struct amdgpu_device *adev)
5813 {
5814 char *fw_name_dmub;
5815 int r;
5816
5817 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5818 case IP_VERSION(2, 1, 0):
5819 fw_name_dmub = FIRMWARE_RENOIR_DMUB;
5820 if (ASICREV_IS_GREEN_SARDINE(adev->external_rev_id))
5821 fw_name_dmub = FIRMWARE_GREEN_SARDINE_DMUB;
5822 break;
5823 case IP_VERSION(3, 0, 0):
5824 if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(10, 3, 0))
5825 fw_name_dmub = FIRMWARE_SIENNA_CICHLID_DMUB;
5826 else
5827 fw_name_dmub = FIRMWARE_NAVY_FLOUNDER_DMUB;
5828 break;
5829 case IP_VERSION(3, 0, 1):
5830 fw_name_dmub = FIRMWARE_VANGOGH_DMUB;
5831 break;
5832 case IP_VERSION(3, 0, 2):
5833 fw_name_dmub = FIRMWARE_DIMGREY_CAVEFISH_DMUB;
5834 break;
5835 case IP_VERSION(3, 0, 3):
5836 fw_name_dmub = FIRMWARE_BEIGE_GOBY_DMUB;
5837 break;
5838 case IP_VERSION(3, 1, 2):
5839 case IP_VERSION(3, 1, 3):
5840 fw_name_dmub = FIRMWARE_YELLOW_CARP_DMUB;
5841 break;
5842 case IP_VERSION(3, 1, 4):
5843 fw_name_dmub = FIRMWARE_DCN_314_DMUB;
5844 break;
5845 case IP_VERSION(3, 1, 5):
5846 fw_name_dmub = FIRMWARE_DCN_315_DMUB;
5847 break;
5848 case IP_VERSION(3, 1, 6):
5849 fw_name_dmub = FIRMWARE_DCN316_DMUB;
5850 break;
5851 case IP_VERSION(3, 2, 0):
5852 fw_name_dmub = FIRMWARE_DCN_V3_2_0_DMCUB;
5853 break;
5854 case IP_VERSION(3, 2, 1):
5855 fw_name_dmub = FIRMWARE_DCN_V3_2_1_DMCUB;
5856 break;
5857 case IP_VERSION(3, 5, 0):
5858 fw_name_dmub = FIRMWARE_DCN_35_DMUB;
5859 break;
5860 case IP_VERSION(3, 5, 1):
5861 fw_name_dmub = FIRMWARE_DCN_351_DMUB;
5862 break;
5863 case IP_VERSION(3, 6, 0):
5864 fw_name_dmub = FIRMWARE_DCN_36_DMUB;
5865 break;
5866 case IP_VERSION(4, 0, 1):
5867 fw_name_dmub = FIRMWARE_DCN_401_DMUB;
5868 break;
5869 default:
5870 /* ASIC doesn't support DMUB. */
5871 return 0;
5872 }
5873 r = amdgpu_ucode_request(adev, &adev->dm.dmub_fw, AMDGPU_UCODE_REQUIRED,
5874 "%s", fw_name_dmub);
5875 return r;
5876 }
5877
dm_early_init(struct amdgpu_ip_block * ip_block)5878 static int dm_early_init(struct amdgpu_ip_block *ip_block)
5879 {
5880 struct amdgpu_device *adev = ip_block->adev;
5881 struct amdgpu_mode_info *mode_info = &adev->mode_info;
5882 struct atom_context *ctx = mode_info->atom_context;
5883 int index = GetIndexIntoMasterTable(DATA, Object_Header);
5884 u16 data_offset;
5885
5886 /* if there is no object header, skip DM */
5887 if (!amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
5888 adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK;
5889 drm_info(adev_to_drm(adev), "No object header, skipping DM\n");
5890 return -ENOENT;
5891 }
5892
5893 switch (adev->asic_type) {
5894 #if defined(CONFIG_DRM_AMD_DC_SI)
5895 case CHIP_TAHITI:
5896 case CHIP_PITCAIRN:
5897 case CHIP_VERDE:
5898 adev->mode_info.num_crtc = 6;
5899 adev->mode_info.num_hpd = 6;
5900 adev->mode_info.num_dig = 6;
5901 break;
5902 case CHIP_OLAND:
5903 adev->mode_info.num_crtc = 2;
5904 adev->mode_info.num_hpd = 2;
5905 adev->mode_info.num_dig = 2;
5906 break;
5907 #endif
5908 case CHIP_BONAIRE:
5909 case CHIP_HAWAII:
5910 adev->mode_info.num_crtc = 6;
5911 adev->mode_info.num_hpd = 6;
5912 adev->mode_info.num_dig = 6;
5913 break;
5914 case CHIP_KAVERI:
5915 adev->mode_info.num_crtc = 4;
5916 adev->mode_info.num_hpd = 6;
5917 adev->mode_info.num_dig = 7;
5918 break;
5919 case CHIP_KABINI:
5920 case CHIP_MULLINS:
5921 adev->mode_info.num_crtc = 2;
5922 adev->mode_info.num_hpd = 6;
5923 adev->mode_info.num_dig = 6;
5924 break;
5925 case CHIP_FIJI:
5926 case CHIP_TONGA:
5927 adev->mode_info.num_crtc = 6;
5928 adev->mode_info.num_hpd = 6;
5929 adev->mode_info.num_dig = 7;
5930 break;
5931 case CHIP_CARRIZO:
5932 adev->mode_info.num_crtc = 3;
5933 adev->mode_info.num_hpd = 6;
5934 adev->mode_info.num_dig = 9;
5935 break;
5936 case CHIP_STONEY:
5937 adev->mode_info.num_crtc = 2;
5938 adev->mode_info.num_hpd = 6;
5939 adev->mode_info.num_dig = 9;
5940 break;
5941 case CHIP_POLARIS11:
5942 case CHIP_POLARIS12:
5943 adev->mode_info.num_crtc = 5;
5944 adev->mode_info.num_hpd = 5;
5945 adev->mode_info.num_dig = 5;
5946 break;
5947 case CHIP_POLARIS10:
5948 case CHIP_VEGAM:
5949 adev->mode_info.num_crtc = 6;
5950 adev->mode_info.num_hpd = 6;
5951 adev->mode_info.num_dig = 6;
5952 break;
5953 case CHIP_VEGA10:
5954 case CHIP_VEGA12:
5955 case CHIP_VEGA20:
5956 adev->mode_info.num_crtc = 6;
5957 adev->mode_info.num_hpd = 6;
5958 adev->mode_info.num_dig = 6;
5959 break;
5960 default:
5961
5962 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
5963 case IP_VERSION(2, 0, 2):
5964 case IP_VERSION(3, 0, 0):
5965 adev->mode_info.num_crtc = 6;
5966 adev->mode_info.num_hpd = 6;
5967 adev->mode_info.num_dig = 6;
5968 break;
5969 case IP_VERSION(2, 0, 0):
5970 case IP_VERSION(3, 0, 2):
5971 adev->mode_info.num_crtc = 5;
5972 adev->mode_info.num_hpd = 5;
5973 adev->mode_info.num_dig = 5;
5974 break;
5975 case IP_VERSION(2, 0, 3):
5976 case IP_VERSION(3, 0, 3):
5977 adev->mode_info.num_crtc = 2;
5978 adev->mode_info.num_hpd = 2;
5979 adev->mode_info.num_dig = 2;
5980 break;
5981 case IP_VERSION(1, 0, 0):
5982 case IP_VERSION(1, 0, 1):
5983 case IP_VERSION(3, 0, 1):
5984 case IP_VERSION(2, 1, 0):
5985 case IP_VERSION(3, 1, 2):
5986 case IP_VERSION(3, 1, 3):
5987 case IP_VERSION(3, 1, 4):
5988 case IP_VERSION(3, 1, 5):
5989 case IP_VERSION(3, 1, 6):
5990 case IP_VERSION(3, 2, 0):
5991 case IP_VERSION(3, 2, 1):
5992 case IP_VERSION(3, 5, 0):
5993 case IP_VERSION(3, 5, 1):
5994 case IP_VERSION(3, 6, 0):
5995 case IP_VERSION(4, 0, 1):
5996 adev->mode_info.num_crtc = 4;
5997 adev->mode_info.num_hpd = 4;
5998 adev->mode_info.num_dig = 4;
5999 break;
6000 default:
6001 drm_err(adev_to_drm(adev), "Unsupported DCE IP versions: 0x%x\n",
6002 amdgpu_ip_version(adev, DCE_HWIP, 0));
6003 return -EINVAL;
6004 }
6005 break;
6006 }
6007
6008 if (adev->mode_info.funcs == NULL)
6009 adev->mode_info.funcs = &dm_display_funcs;
6010
6011 /*
6012 * Note: Do NOT change adev->audio_endpt_rreg and
6013 * adev->audio_endpt_wreg because they are initialised in
6014 * amdgpu_device_init()
6015 */
6016 #if defined(CONFIG_DEBUG_KERNEL_DC)
6017 device_create_file(
6018 adev_to_drm(adev)->dev,
6019 &dev_attr_s3_debug);
6020 #endif
6021 adev->dc_enabled = true;
6022
6023 return dm_init_microcode(adev);
6024 }
6025
modereset_required(struct drm_crtc_state * crtc_state)6026 static bool modereset_required(struct drm_crtc_state *crtc_state)
6027 {
6028 return !crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state);
6029 }
6030
amdgpu_dm_encoder_destroy(struct drm_encoder * encoder)6031 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
6032 {
6033 drm_encoder_cleanup(encoder);
6034 kfree(encoder);
6035 }
6036
6037 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
6038 .destroy = amdgpu_dm_encoder_destroy,
6039 };
6040
6041 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)6042 fill_plane_color_attributes(const struct drm_plane_state *plane_state,
6043 const enum surface_pixel_format format,
6044 enum dc_color_space *color_space)
6045 {
6046 bool full_range;
6047
6048 *color_space = COLOR_SPACE_SRGB;
6049
6050 /* Ignore properties when DRM_CLIENT_CAP_PLANE_COLOR_PIPELINE is set */
6051 if (plane_state->state && plane_state->state->plane_color_pipeline)
6052 return 0;
6053
6054 /* DRM color properties only affect non-RGB formats. */
6055 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
6056 return 0;
6057
6058 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE);
6059
6060 switch (plane_state->color_encoding) {
6061 case DRM_COLOR_YCBCR_BT601:
6062 if (full_range)
6063 *color_space = COLOR_SPACE_YCBCR601;
6064 else
6065 *color_space = COLOR_SPACE_YCBCR601_LIMITED;
6066 break;
6067
6068 case DRM_COLOR_YCBCR_BT709:
6069 if (full_range)
6070 *color_space = COLOR_SPACE_YCBCR709;
6071 else
6072 *color_space = COLOR_SPACE_YCBCR709_LIMITED;
6073 break;
6074
6075 case DRM_COLOR_YCBCR_BT2020:
6076 if (full_range)
6077 *color_space = COLOR_SPACE_2020_YCBCR_FULL;
6078 else
6079 *color_space = COLOR_SPACE_2020_YCBCR_LIMITED;
6080 break;
6081
6082 default:
6083 return -EINVAL;
6084 }
6085
6086 return 0;
6087 }
6088
6089 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)6090 fill_dc_plane_info_and_addr(struct amdgpu_device *adev,
6091 const struct drm_plane_state *plane_state,
6092 const u64 tiling_flags,
6093 struct dc_plane_info *plane_info,
6094 struct dc_plane_address *address,
6095 bool tmz_surface)
6096 {
6097 const struct drm_framebuffer *fb = plane_state->fb;
6098 const struct amdgpu_framebuffer *afb =
6099 to_amdgpu_framebuffer(plane_state->fb);
6100 int ret;
6101
6102 memset(plane_info, 0, sizeof(*plane_info));
6103
6104 switch (fb->format->format) {
6105 case DRM_FORMAT_C8:
6106 plane_info->format =
6107 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
6108 break;
6109 case DRM_FORMAT_RGB565:
6110 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
6111 break;
6112 case DRM_FORMAT_XRGB8888:
6113 case DRM_FORMAT_ARGB8888:
6114 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
6115 break;
6116 case DRM_FORMAT_XRGB2101010:
6117 case DRM_FORMAT_ARGB2101010:
6118 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
6119 break;
6120 case DRM_FORMAT_XBGR2101010:
6121 case DRM_FORMAT_ABGR2101010:
6122 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
6123 break;
6124 case DRM_FORMAT_XBGR8888:
6125 case DRM_FORMAT_ABGR8888:
6126 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888;
6127 break;
6128 case DRM_FORMAT_NV21:
6129 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
6130 break;
6131 case DRM_FORMAT_NV12:
6132 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
6133 break;
6134 case DRM_FORMAT_P010:
6135 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb;
6136 break;
6137 case DRM_FORMAT_XRGB16161616F:
6138 case DRM_FORMAT_ARGB16161616F:
6139 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F;
6140 break;
6141 case DRM_FORMAT_XBGR16161616F:
6142 case DRM_FORMAT_ABGR16161616F:
6143 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F;
6144 break;
6145 case DRM_FORMAT_XRGB16161616:
6146 case DRM_FORMAT_ARGB16161616:
6147 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616;
6148 break;
6149 case DRM_FORMAT_XBGR16161616:
6150 case DRM_FORMAT_ABGR16161616:
6151 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616;
6152 break;
6153 default:
6154 drm_err(adev_to_drm(adev),
6155 "Unsupported screen format %p4cc\n",
6156 &fb->format->format);
6157 return -EINVAL;
6158 }
6159
6160 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
6161 case DRM_MODE_ROTATE_0:
6162 plane_info->rotation = ROTATION_ANGLE_0;
6163 break;
6164 case DRM_MODE_ROTATE_90:
6165 plane_info->rotation = ROTATION_ANGLE_90;
6166 break;
6167 case DRM_MODE_ROTATE_180:
6168 plane_info->rotation = ROTATION_ANGLE_180;
6169 break;
6170 case DRM_MODE_ROTATE_270:
6171 plane_info->rotation = ROTATION_ANGLE_270;
6172 break;
6173 default:
6174 plane_info->rotation = ROTATION_ANGLE_0;
6175 break;
6176 }
6177
6178
6179 plane_info->visible = true;
6180 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE;
6181
6182 plane_info->layer_index = plane_state->normalized_zpos;
6183
6184 ret = fill_plane_color_attributes(plane_state, plane_info->format,
6185 &plane_info->color_space);
6186 if (ret)
6187 return ret;
6188
6189 ret = amdgpu_dm_plane_fill_plane_buffer_attributes(adev, afb, plane_info->format,
6190 plane_info->rotation, tiling_flags,
6191 &plane_info->tiling_info,
6192 &plane_info->plane_size,
6193 &plane_info->dcc, address,
6194 tmz_surface);
6195 if (ret)
6196 return ret;
6197
6198 amdgpu_dm_plane_fill_blending_from_plane_state(
6199 plane_state, &plane_info->per_pixel_alpha, &plane_info->pre_multiplied_alpha,
6200 &plane_info->global_alpha, &plane_info->global_alpha_value);
6201
6202 return 0;
6203 }
6204
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)6205 static int fill_dc_plane_attributes(struct amdgpu_device *adev,
6206 struct dc_plane_state *dc_plane_state,
6207 struct drm_plane_state *plane_state,
6208 struct drm_crtc_state *crtc_state)
6209 {
6210 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
6211 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)plane_state->fb;
6212 struct dc_scaling_info scaling_info;
6213 struct dc_plane_info plane_info;
6214 int ret;
6215
6216 ret = amdgpu_dm_plane_fill_dc_scaling_info(adev, plane_state, &scaling_info);
6217 if (ret)
6218 return ret;
6219
6220 dc_plane_state->src_rect = scaling_info.src_rect;
6221 dc_plane_state->dst_rect = scaling_info.dst_rect;
6222 dc_plane_state->clip_rect = scaling_info.clip_rect;
6223 dc_plane_state->scaling_quality = scaling_info.scaling_quality;
6224
6225 ret = fill_dc_plane_info_and_addr(adev, plane_state,
6226 afb->tiling_flags,
6227 &plane_info,
6228 &dc_plane_state->address,
6229 afb->tmz_surface);
6230 if (ret)
6231 return ret;
6232
6233 dc_plane_state->format = plane_info.format;
6234 dc_plane_state->color_space = plane_info.color_space;
6235 dc_plane_state->format = plane_info.format;
6236 dc_plane_state->plane_size = plane_info.plane_size;
6237 dc_plane_state->rotation = plane_info.rotation;
6238 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror;
6239 dc_plane_state->stereo_format = plane_info.stereo_format;
6240 dc_plane_state->tiling_info = plane_info.tiling_info;
6241 dc_plane_state->visible = plane_info.visible;
6242 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha;
6243 dc_plane_state->pre_multiplied_alpha = plane_info.pre_multiplied_alpha;
6244 dc_plane_state->global_alpha = plane_info.global_alpha;
6245 dc_plane_state->global_alpha_value = plane_info.global_alpha_value;
6246 dc_plane_state->dcc = plane_info.dcc;
6247 dc_plane_state->layer_index = plane_info.layer_index;
6248 dc_plane_state->flip_int_enabled = true;
6249
6250 /*
6251 * Always set input transfer function, since plane state is refreshed
6252 * every time.
6253 */
6254 ret = amdgpu_dm_update_plane_color_mgmt(dm_crtc_state,
6255 plane_state,
6256 dc_plane_state);
6257 if (ret)
6258 return ret;
6259
6260 return 0;
6261 }
6262
fill_dc_dirty_rect(struct drm_plane * plane,struct rect * dirty_rect,int32_t x,s32 y,s32 width,s32 height,int * i,bool ffu)6263 static inline void fill_dc_dirty_rect(struct drm_plane *plane,
6264 struct rect *dirty_rect, int32_t x,
6265 s32 y, s32 width, s32 height,
6266 int *i, bool ffu)
6267 {
6268 WARN_ON(*i >= DC_MAX_DIRTY_RECTS);
6269
6270 dirty_rect->x = x;
6271 dirty_rect->y = y;
6272 dirty_rect->width = width;
6273 dirty_rect->height = height;
6274
6275 if (ffu)
6276 drm_dbg(plane->dev,
6277 "[PLANE:%d] PSR FFU dirty rect size (%d, %d)\n",
6278 plane->base.id, width, height);
6279 else
6280 drm_dbg(plane->dev,
6281 "[PLANE:%d] PSR SU dirty rect at (%d, %d) size (%d, %d)",
6282 plane->base.id, x, y, width, height);
6283
6284 (*i)++;
6285 }
6286
6287 /**
6288 * fill_dc_dirty_rects() - Fill DC dirty regions for PSR selective updates
6289 *
6290 * @plane: DRM plane containing dirty regions that need to be flushed to the eDP
6291 * remote fb
6292 * @old_plane_state: Old state of @plane
6293 * @new_plane_state: New state of @plane
6294 * @crtc_state: New state of CRTC connected to the @plane
6295 * @flip_addrs: DC flip tracking struct, which also tracts dirty rects
6296 * @is_psr_su: Flag indicating whether Panel Self Refresh Selective Update (PSR SU) is enabled.
6297 * If PSR SU is enabled and damage clips are available, only the regions of the screen
6298 * that have changed will be updated. If PSR SU is not enabled,
6299 * or if damage clips are not available, the entire screen will be updated.
6300 * @dirty_regions_changed: dirty regions changed
6301 *
6302 * For PSR SU, DC informs the DMUB uController of dirty rectangle regions
6303 * (referred to as "damage clips" in DRM nomenclature) that require updating on
6304 * the eDP remote buffer. The responsibility of specifying the dirty regions is
6305 * amdgpu_dm's.
6306 *
6307 * A damage-aware DRM client should fill the FB_DAMAGE_CLIPS property on the
6308 * plane with regions that require flushing to the eDP remote buffer. In
6309 * addition, certain use cases - such as cursor and multi-plane overlay (MPO) -
6310 * implicitly provide damage clips without any client support via the plane
6311 * bounds.
6312 */
fill_dc_dirty_rects(struct drm_plane * plane,struct drm_plane_state * old_plane_state,struct drm_plane_state * new_plane_state,struct drm_crtc_state * crtc_state,struct dc_flip_addrs * flip_addrs,bool is_psr_su,bool * dirty_regions_changed)6313 static void fill_dc_dirty_rects(struct drm_plane *plane,
6314 struct drm_plane_state *old_plane_state,
6315 struct drm_plane_state *new_plane_state,
6316 struct drm_crtc_state *crtc_state,
6317 struct dc_flip_addrs *flip_addrs,
6318 bool is_psr_su,
6319 bool *dirty_regions_changed)
6320 {
6321 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
6322 struct rect *dirty_rects = flip_addrs->dirty_rects;
6323 u32 num_clips;
6324 struct drm_mode_rect *clips;
6325 bool bb_changed;
6326 bool fb_changed;
6327 u32 i = 0;
6328 *dirty_regions_changed = false;
6329
6330 /*
6331 * Cursor plane has it's own dirty rect update interface. See
6332 * dcn10_dmub_update_cursor_data and dmub_cmd_update_cursor_info_data
6333 */
6334 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6335 return;
6336
6337 if (new_plane_state->rotation != DRM_MODE_ROTATE_0)
6338 goto ffu;
6339
6340 num_clips = drm_plane_get_damage_clips_count(new_plane_state);
6341 clips = drm_plane_get_damage_clips(new_plane_state);
6342
6343 if (num_clips && (!amdgpu_damage_clips || (amdgpu_damage_clips < 0 &&
6344 is_psr_su)))
6345 goto ffu;
6346
6347 if (!dm_crtc_state->mpo_requested) {
6348 if (!num_clips || num_clips > DC_MAX_DIRTY_RECTS)
6349 goto ffu;
6350
6351 for (; flip_addrs->dirty_rect_count < num_clips; clips++)
6352 fill_dc_dirty_rect(new_plane_state->plane,
6353 &dirty_rects[flip_addrs->dirty_rect_count],
6354 clips->x1, clips->y1,
6355 clips->x2 - clips->x1, clips->y2 - clips->y1,
6356 &flip_addrs->dirty_rect_count,
6357 false);
6358 return;
6359 }
6360
6361 /*
6362 * MPO is requested. Add entire plane bounding box to dirty rects if
6363 * flipped to or damaged.
6364 *
6365 * If plane is moved or resized, also add old bounding box to dirty
6366 * rects.
6367 */
6368 fb_changed = old_plane_state->fb->base.id !=
6369 new_plane_state->fb->base.id;
6370 bb_changed = (old_plane_state->crtc_x != new_plane_state->crtc_x ||
6371 old_plane_state->crtc_y != new_plane_state->crtc_y ||
6372 old_plane_state->crtc_w != new_plane_state->crtc_w ||
6373 old_plane_state->crtc_h != new_plane_state->crtc_h);
6374
6375 drm_dbg(plane->dev,
6376 "[PLANE:%d] PSR bb_changed:%d fb_changed:%d num_clips:%d\n",
6377 new_plane_state->plane->base.id,
6378 bb_changed, fb_changed, num_clips);
6379
6380 *dirty_regions_changed = bb_changed;
6381
6382 if ((num_clips + (bb_changed ? 2 : 0)) > DC_MAX_DIRTY_RECTS)
6383 goto ffu;
6384
6385 if (bb_changed) {
6386 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
6387 new_plane_state->crtc_x,
6388 new_plane_state->crtc_y,
6389 new_plane_state->crtc_w,
6390 new_plane_state->crtc_h, &i, false);
6391
6392 /* Add old plane bounding-box if plane is moved or resized */
6393 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
6394 old_plane_state->crtc_x,
6395 old_plane_state->crtc_y,
6396 old_plane_state->crtc_w,
6397 old_plane_state->crtc_h, &i, false);
6398 }
6399
6400 if (num_clips) {
6401 for (; i < num_clips; clips++)
6402 fill_dc_dirty_rect(new_plane_state->plane,
6403 &dirty_rects[i], clips->x1,
6404 clips->y1, clips->x2 - clips->x1,
6405 clips->y2 - clips->y1, &i, false);
6406 } else if (fb_changed && !bb_changed) {
6407 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i],
6408 new_plane_state->crtc_x,
6409 new_plane_state->crtc_y,
6410 new_plane_state->crtc_w,
6411 new_plane_state->crtc_h, &i, false);
6412 }
6413
6414 flip_addrs->dirty_rect_count = i;
6415 return;
6416
6417 ffu:
6418 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[0], 0, 0,
6419 dm_crtc_state->base.mode.crtc_hdisplay,
6420 dm_crtc_state->base.mode.crtc_vdisplay,
6421 &flip_addrs->dirty_rect_count, true);
6422 }
6423
update_stream_scaling_settings(const struct drm_display_mode * mode,const struct dm_connector_state * dm_state,struct dc_stream_state * stream)6424 static void update_stream_scaling_settings(const struct drm_display_mode *mode,
6425 const struct dm_connector_state *dm_state,
6426 struct dc_stream_state *stream)
6427 {
6428 enum amdgpu_rmx_type rmx_type;
6429
6430 struct rect src = { 0 }; /* viewport in composition space*/
6431 struct rect dst = { 0 }; /* stream addressable area */
6432
6433 /* no mode. nothing to be done */
6434 if (!mode)
6435 return;
6436
6437 /* Full screen scaling by default */
6438 src.width = mode->hdisplay;
6439 src.height = mode->vdisplay;
6440 dst.width = stream->timing.h_addressable;
6441 dst.height = stream->timing.v_addressable;
6442
6443 if (dm_state) {
6444 rmx_type = dm_state->scaling;
6445 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
6446 if (src.width * dst.height <
6447 src.height * dst.width) {
6448 /* height needs less upscaling/more downscaling */
6449 dst.width = src.width *
6450 dst.height / src.height;
6451 } else {
6452 /* width needs less upscaling/more downscaling */
6453 dst.height = src.height *
6454 dst.width / src.width;
6455 }
6456 } else if (rmx_type == RMX_CENTER) {
6457 dst = src;
6458 }
6459
6460 dst.x = (stream->timing.h_addressable - dst.width) / 2;
6461 dst.y = (stream->timing.v_addressable - dst.height) / 2;
6462
6463 if (dm_state->underscan_enable) {
6464 dst.x += dm_state->underscan_hborder / 2;
6465 dst.y += dm_state->underscan_vborder / 2;
6466 dst.width -= dm_state->underscan_hborder;
6467 dst.height -= dm_state->underscan_vborder;
6468 }
6469 }
6470
6471 stream->src = src;
6472 stream->dst = dst;
6473
6474 DRM_DEBUG_KMS("Destination Rectangle x:%d y:%d width:%d height:%d\n",
6475 dst.x, dst.y, dst.width, dst.height);
6476
6477 }
6478
6479 static enum dc_color_depth
convert_color_depth_from_display_info(const struct drm_connector * connector,bool is_y420,int requested_bpc)6480 convert_color_depth_from_display_info(const struct drm_connector *connector,
6481 bool is_y420, int requested_bpc)
6482 {
6483 u8 bpc;
6484
6485 if (is_y420) {
6486 bpc = 8;
6487
6488 /* Cap display bpc based on HDMI 2.0 HF-VSDB */
6489 if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48)
6490 bpc = 16;
6491 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36)
6492 bpc = 12;
6493 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30)
6494 bpc = 10;
6495 } else {
6496 bpc = (uint8_t)connector->display_info.bpc;
6497 /* Assume 8 bpc by default if no bpc is specified. */
6498 bpc = bpc ? bpc : 8;
6499 }
6500
6501 if (requested_bpc > 0) {
6502 /*
6503 * Cap display bpc based on the user requested value.
6504 *
6505 * The value for state->max_bpc may not correctly updated
6506 * depending on when the connector gets added to the state
6507 * or if this was called outside of atomic check, so it
6508 * can't be used directly.
6509 */
6510 bpc = min_t(u8, bpc, requested_bpc);
6511
6512 /* Round down to the nearest even number. */
6513 bpc = bpc - (bpc & 1);
6514 }
6515
6516 switch (bpc) {
6517 case 0:
6518 /*
6519 * Temporary Work around, DRM doesn't parse color depth for
6520 * EDID revision before 1.4
6521 * TODO: Fix edid parsing
6522 */
6523 return COLOR_DEPTH_888;
6524 case 6:
6525 return COLOR_DEPTH_666;
6526 case 8:
6527 return COLOR_DEPTH_888;
6528 case 10:
6529 return COLOR_DEPTH_101010;
6530 case 12:
6531 return COLOR_DEPTH_121212;
6532 case 14:
6533 return COLOR_DEPTH_141414;
6534 case 16:
6535 return COLOR_DEPTH_161616;
6536 default:
6537 return COLOR_DEPTH_UNDEFINED;
6538 }
6539 }
6540
6541 static enum dc_aspect_ratio
get_aspect_ratio(const struct drm_display_mode * mode_in)6542 get_aspect_ratio(const struct drm_display_mode *mode_in)
6543 {
6544 /* 1-1 mapping, since both enums follow the HDMI spec. */
6545 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
6546 }
6547
6548 static enum dc_color_space
get_output_color_space(const struct dc_crtc_timing * dc_crtc_timing,const struct drm_connector_state * connector_state)6549 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing,
6550 const struct drm_connector_state *connector_state)
6551 {
6552 enum dc_color_space color_space = COLOR_SPACE_SRGB;
6553
6554 switch (connector_state->colorspace) {
6555 case DRM_MODE_COLORIMETRY_BT601_YCC:
6556 if (dc_crtc_timing->flags.Y_ONLY)
6557 color_space = COLOR_SPACE_YCBCR601_LIMITED;
6558 else
6559 color_space = COLOR_SPACE_YCBCR601;
6560 break;
6561 case DRM_MODE_COLORIMETRY_BT709_YCC:
6562 if (dc_crtc_timing->flags.Y_ONLY)
6563 color_space = COLOR_SPACE_YCBCR709_LIMITED;
6564 else
6565 color_space = COLOR_SPACE_YCBCR709;
6566 break;
6567 case DRM_MODE_COLORIMETRY_OPRGB:
6568 color_space = COLOR_SPACE_ADOBERGB;
6569 break;
6570 case DRM_MODE_COLORIMETRY_BT2020_RGB:
6571 case DRM_MODE_COLORIMETRY_BT2020_YCC:
6572 if (dc_crtc_timing->pixel_encoding == PIXEL_ENCODING_RGB)
6573 color_space = COLOR_SPACE_2020_RGB_FULLRANGE;
6574 else
6575 color_space = COLOR_SPACE_2020_YCBCR_LIMITED;
6576 break;
6577 case DRM_MODE_COLORIMETRY_DEFAULT: // ITU601
6578 default:
6579 if (dc_crtc_timing->pixel_encoding == PIXEL_ENCODING_RGB) {
6580 color_space = COLOR_SPACE_SRGB;
6581 if (connector_state->hdmi.broadcast_rgb == DRM_HDMI_BROADCAST_RGB_LIMITED)
6582 color_space = COLOR_SPACE_SRGB_LIMITED;
6583 /*
6584 * 27030khz is the separation point between HDTV and SDTV
6585 * according to HDMI spec, we use YCbCr709 and YCbCr601
6586 * respectively
6587 */
6588 } else if (dc_crtc_timing->pix_clk_100hz > 270300) {
6589 if (dc_crtc_timing->flags.Y_ONLY)
6590 color_space =
6591 COLOR_SPACE_YCBCR709_LIMITED;
6592 else
6593 color_space = COLOR_SPACE_YCBCR709;
6594 } else {
6595 if (dc_crtc_timing->flags.Y_ONLY)
6596 color_space =
6597 COLOR_SPACE_YCBCR601_LIMITED;
6598 else
6599 color_space = COLOR_SPACE_YCBCR601;
6600 }
6601 break;
6602 }
6603
6604 return color_space;
6605 }
6606
6607 static enum display_content_type
get_output_content_type(const struct drm_connector_state * connector_state)6608 get_output_content_type(const struct drm_connector_state *connector_state)
6609 {
6610 switch (connector_state->content_type) {
6611 default:
6612 case DRM_MODE_CONTENT_TYPE_NO_DATA:
6613 return DISPLAY_CONTENT_TYPE_NO_DATA;
6614 case DRM_MODE_CONTENT_TYPE_GRAPHICS:
6615 return DISPLAY_CONTENT_TYPE_GRAPHICS;
6616 case DRM_MODE_CONTENT_TYPE_PHOTO:
6617 return DISPLAY_CONTENT_TYPE_PHOTO;
6618 case DRM_MODE_CONTENT_TYPE_CINEMA:
6619 return DISPLAY_CONTENT_TYPE_CINEMA;
6620 case DRM_MODE_CONTENT_TYPE_GAME:
6621 return DISPLAY_CONTENT_TYPE_GAME;
6622 }
6623 }
6624
adjust_colour_depth_from_display_info(struct dc_crtc_timing * timing_out,const struct drm_display_info * info)6625 static bool adjust_colour_depth_from_display_info(
6626 struct dc_crtc_timing *timing_out,
6627 const struct drm_display_info *info)
6628 {
6629 enum dc_color_depth depth = timing_out->display_color_depth;
6630 int normalized_clk;
6631
6632 do {
6633 normalized_clk = timing_out->pix_clk_100hz / 10;
6634 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
6635 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
6636 normalized_clk /= 2;
6637 /* Adjusting pix clock following on HDMI spec based on colour depth */
6638 switch (depth) {
6639 case COLOR_DEPTH_888:
6640 break;
6641 case COLOR_DEPTH_101010:
6642 normalized_clk = (normalized_clk * 30) / 24;
6643 break;
6644 case COLOR_DEPTH_121212:
6645 normalized_clk = (normalized_clk * 36) / 24;
6646 break;
6647 case COLOR_DEPTH_161616:
6648 normalized_clk = (normalized_clk * 48) / 24;
6649 break;
6650 default:
6651 /* The above depths are the only ones valid for HDMI. */
6652 return false;
6653 }
6654 if (normalized_clk <= info->max_tmds_clock) {
6655 timing_out->display_color_depth = depth;
6656 return true;
6657 }
6658 } while (--depth > COLOR_DEPTH_666);
6659 return false;
6660 }
6661
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)6662 static void fill_stream_properties_from_drm_display_mode(
6663 struct dc_stream_state *stream,
6664 const struct drm_display_mode *mode_in,
6665 const struct drm_connector *connector,
6666 const struct drm_connector_state *connector_state,
6667 const struct dc_stream_state *old_stream,
6668 int requested_bpc)
6669 {
6670 struct dc_crtc_timing *timing_out = &stream->timing;
6671 const struct drm_display_info *info = &connector->display_info;
6672 struct amdgpu_dm_connector *aconnector = NULL;
6673 struct hdmi_vendor_infoframe hv_frame;
6674 struct hdmi_avi_infoframe avi_frame;
6675 ssize_t err;
6676
6677 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
6678 aconnector = to_amdgpu_dm_connector(connector);
6679
6680 memset(&hv_frame, 0, sizeof(hv_frame));
6681 memset(&avi_frame, 0, sizeof(avi_frame));
6682
6683 timing_out->h_border_left = 0;
6684 timing_out->h_border_right = 0;
6685 timing_out->v_border_top = 0;
6686 timing_out->v_border_bottom = 0;
6687 /* TODO: un-hardcode */
6688 if (drm_mode_is_420_only(info, mode_in)
6689 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
6690 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
6691 else if (drm_mode_is_420_also(info, mode_in)
6692 && aconnector
6693 && aconnector->force_yuv420_output)
6694 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
6695 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCBCR422)
6696 && aconnector
6697 && aconnector->force_yuv422_output)
6698 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR422;
6699 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCBCR444)
6700 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
6701 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
6702 else
6703 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
6704
6705 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
6706 timing_out->display_color_depth = convert_color_depth_from_display_info(
6707 connector,
6708 (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420),
6709 requested_bpc);
6710 timing_out->scan_type = SCANNING_TYPE_NODATA;
6711 timing_out->hdmi_vic = 0;
6712
6713 if (old_stream) {
6714 timing_out->vic = old_stream->timing.vic;
6715 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY;
6716 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY;
6717 } else {
6718 timing_out->vic = drm_match_cea_mode(mode_in);
6719 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
6720 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
6721 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
6722 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
6723 }
6724
6725 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
6726 err = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame,
6727 (struct drm_connector *)connector,
6728 mode_in);
6729 if (err < 0)
6730 drm_warn_once(connector->dev, "Failed to setup avi infoframe on connector %s: %zd\n",
6731 connector->name, err);
6732 timing_out->vic = avi_frame.video_code;
6733 err = drm_hdmi_vendor_infoframe_from_display_mode(&hv_frame,
6734 (struct drm_connector *)connector,
6735 mode_in);
6736 if (err < 0)
6737 drm_warn_once(connector->dev, "Failed to setup vendor infoframe on connector %s: %zd\n",
6738 connector->name, err);
6739 timing_out->hdmi_vic = hv_frame.vic;
6740 }
6741
6742 if (aconnector && is_freesync_video_mode(mode_in, aconnector)) {
6743 timing_out->h_addressable = mode_in->hdisplay;
6744 timing_out->h_total = mode_in->htotal;
6745 timing_out->h_sync_width = mode_in->hsync_end - mode_in->hsync_start;
6746 timing_out->h_front_porch = mode_in->hsync_start - mode_in->hdisplay;
6747 timing_out->v_total = mode_in->vtotal;
6748 timing_out->v_addressable = mode_in->vdisplay;
6749 timing_out->v_front_porch = mode_in->vsync_start - mode_in->vdisplay;
6750 timing_out->v_sync_width = mode_in->vsync_end - mode_in->vsync_start;
6751 timing_out->pix_clk_100hz = mode_in->clock * 10;
6752 } else {
6753 timing_out->h_addressable = mode_in->crtc_hdisplay;
6754 timing_out->h_total = mode_in->crtc_htotal;
6755 timing_out->h_sync_width = mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
6756 timing_out->h_front_porch = mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
6757 timing_out->v_total = mode_in->crtc_vtotal;
6758 timing_out->v_addressable = mode_in->crtc_vdisplay;
6759 timing_out->v_front_porch = mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
6760 timing_out->v_sync_width = mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
6761 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10;
6762 }
6763
6764 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
6765
6766 stream->out_transfer_func.type = TF_TYPE_PREDEFINED;
6767 stream->out_transfer_func.tf = TRANSFER_FUNCTION_SRGB;
6768 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
6769 if (!adjust_colour_depth_from_display_info(timing_out, info) &&
6770 drm_mode_is_420_also(info, mode_in) &&
6771 timing_out->pixel_encoding != PIXEL_ENCODING_YCBCR420) {
6772 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
6773 adjust_colour_depth_from_display_info(timing_out, info);
6774 }
6775 }
6776
6777 stream->output_color_space = get_output_color_space(timing_out, connector_state);
6778 stream->content_type = get_output_content_type(connector_state);
6779 }
6780
fill_audio_info(struct audio_info * audio_info,const struct drm_connector * drm_connector,const struct dc_sink * dc_sink)6781 static void fill_audio_info(struct audio_info *audio_info,
6782 const struct drm_connector *drm_connector,
6783 const struct dc_sink *dc_sink)
6784 {
6785 int i = 0;
6786 int cea_revision = 0;
6787 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
6788
6789 audio_info->manufacture_id = edid_caps->manufacturer_id;
6790 audio_info->product_id = edid_caps->product_id;
6791
6792 cea_revision = drm_connector->display_info.cea_rev;
6793
6794 strscpy(audio_info->display_name,
6795 edid_caps->display_name,
6796 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
6797
6798 if (cea_revision >= 3) {
6799 audio_info->mode_count = edid_caps->audio_mode_count;
6800
6801 for (i = 0; i < audio_info->mode_count; ++i) {
6802 audio_info->modes[i].format_code =
6803 (enum audio_format_code)
6804 (edid_caps->audio_modes[i].format_code);
6805 audio_info->modes[i].channel_count =
6806 edid_caps->audio_modes[i].channel_count;
6807 audio_info->modes[i].sample_rates.all =
6808 edid_caps->audio_modes[i].sample_rate;
6809 audio_info->modes[i].sample_size =
6810 edid_caps->audio_modes[i].sample_size;
6811 }
6812 }
6813
6814 audio_info->flags.all = edid_caps->speaker_flags;
6815
6816 /* TODO: We only check for the progressive mode, check for interlace mode too */
6817 if (drm_connector->latency_present[0]) {
6818 audio_info->video_latency = drm_connector->video_latency[0];
6819 audio_info->audio_latency = drm_connector->audio_latency[0];
6820 }
6821
6822 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
6823
6824 }
6825
6826 static void
copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode * src_mode,struct drm_display_mode * dst_mode)6827 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
6828 struct drm_display_mode *dst_mode)
6829 {
6830 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
6831 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
6832 dst_mode->crtc_clock = src_mode->crtc_clock;
6833 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
6834 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
6835 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
6836 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
6837 dst_mode->crtc_htotal = src_mode->crtc_htotal;
6838 dst_mode->crtc_hskew = src_mode->crtc_hskew;
6839 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
6840 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
6841 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
6842 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
6843 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
6844 }
6845
6846 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)6847 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
6848 const struct drm_display_mode *native_mode,
6849 bool scale_enabled)
6850 {
6851 if (scale_enabled || (
6852 native_mode->clock == drm_mode->clock &&
6853 native_mode->htotal == drm_mode->htotal &&
6854 native_mode->vtotal == drm_mode->vtotal)) {
6855 if (native_mode->crtc_clock)
6856 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
6857 } else {
6858 /* no scaling nor amdgpu inserted, no need to patch */
6859 }
6860 }
6861
6862 static struct dc_sink *
create_fake_sink(struct drm_device * dev,struct dc_link * link)6863 create_fake_sink(struct drm_device *dev, struct dc_link *link)
6864 {
6865 struct dc_sink_init_data sink_init_data = { 0 };
6866 struct dc_sink *sink = NULL;
6867
6868 sink_init_data.link = link;
6869 sink_init_data.sink_signal = link->connector_signal;
6870
6871 sink = dc_sink_create(&sink_init_data);
6872 if (!sink) {
6873 drm_err(dev, "Failed to create sink!\n");
6874 return NULL;
6875 }
6876 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
6877
6878 return sink;
6879 }
6880
set_multisync_trigger_params(struct dc_stream_state * stream)6881 static void set_multisync_trigger_params(
6882 struct dc_stream_state *stream)
6883 {
6884 struct dc_stream_state *master = NULL;
6885
6886 if (stream->triggered_crtc_reset.enabled) {
6887 master = stream->triggered_crtc_reset.event_source;
6888 stream->triggered_crtc_reset.event =
6889 master->timing.flags.VSYNC_POSITIVE_POLARITY ?
6890 CRTC_EVENT_VSYNC_RISING : CRTC_EVENT_VSYNC_FALLING;
6891 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_PIXEL;
6892 }
6893 }
6894
set_master_stream(struct dc_stream_state * stream_set[],int stream_count)6895 static void set_master_stream(struct dc_stream_state *stream_set[],
6896 int stream_count)
6897 {
6898 int j, highest_rfr = 0, master_stream = 0;
6899
6900 for (j = 0; j < stream_count; j++) {
6901 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
6902 int refresh_rate = 0;
6903
6904 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/
6905 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
6906 if (refresh_rate > highest_rfr) {
6907 highest_rfr = refresh_rate;
6908 master_stream = j;
6909 }
6910 }
6911 }
6912 for (j = 0; j < stream_count; j++) {
6913 if (stream_set[j])
6914 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
6915 }
6916 }
6917
dm_enable_per_frame_crtc_master_sync(struct dc_state * context)6918 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
6919 {
6920 int i = 0;
6921 struct dc_stream_state *stream;
6922
6923 if (context->stream_count < 2)
6924 return;
6925 for (i = 0; i < context->stream_count ; i++) {
6926 if (!context->streams[i])
6927 continue;
6928 /*
6929 * TODO: add a function to read AMD VSDB bits and set
6930 * crtc_sync_master.multi_sync_enabled flag
6931 * For now it's set to false
6932 */
6933 }
6934
6935 set_master_stream(context->streams, context->stream_count);
6936
6937 for (i = 0; i < context->stream_count ; i++) {
6938 stream = context->streams[i];
6939
6940 if (!stream)
6941 continue;
6942
6943 set_multisync_trigger_params(stream);
6944 }
6945 }
6946
6947 /**
6948 * DOC: FreeSync Video
6949 *
6950 * When a userspace application wants to play a video, the content follows a
6951 * standard format definition that usually specifies the FPS for that format.
6952 * The below list illustrates some video format and the expected FPS,
6953 * respectively:
6954 *
6955 * - TV/NTSC (23.976 FPS)
6956 * - Cinema (24 FPS)
6957 * - TV/PAL (25 FPS)
6958 * - TV/NTSC (29.97 FPS)
6959 * - TV/NTSC (30 FPS)
6960 * - Cinema HFR (48 FPS)
6961 * - TV/PAL (50 FPS)
6962 * - Commonly used (60 FPS)
6963 * - Multiples of 24 (48,72,96 FPS)
6964 *
6965 * The list of standards video format is not huge and can be added to the
6966 * connector modeset list beforehand. With that, userspace can leverage
6967 * FreeSync to extends the front porch in order to attain the target refresh
6968 * rate. Such a switch will happen seamlessly, without screen blanking or
6969 * reprogramming of the output in any other way. If the userspace requests a
6970 * modesetting change compatible with FreeSync modes that only differ in the
6971 * refresh rate, DC will skip the full update and avoid blink during the
6972 * transition. For example, the video player can change the modesetting from
6973 * 60Hz to 30Hz for playing TV/NTSC content when it goes full screen without
6974 * causing any display blink. This same concept can be applied to a mode
6975 * setting change.
6976 */
6977 static struct drm_display_mode *
get_highest_refresh_rate_mode(struct amdgpu_dm_connector * aconnector,bool use_probed_modes)6978 get_highest_refresh_rate_mode(struct amdgpu_dm_connector *aconnector,
6979 bool use_probed_modes)
6980 {
6981 struct drm_display_mode *m, *m_pref = NULL;
6982 u16 current_refresh, highest_refresh;
6983 struct list_head *list_head = use_probed_modes ?
6984 &aconnector->base.probed_modes :
6985 &aconnector->base.modes;
6986
6987 if (aconnector->base.connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
6988 return NULL;
6989
6990 if (aconnector->freesync_vid_base.clock != 0)
6991 return &aconnector->freesync_vid_base;
6992
6993 /* Find the preferred mode */
6994 list_for_each_entry(m, list_head, head) {
6995 if (m->type & DRM_MODE_TYPE_PREFERRED) {
6996 m_pref = m;
6997 break;
6998 }
6999 }
7000
7001 if (!m_pref) {
7002 /* Probably an EDID with no preferred mode. Fallback to first entry */
7003 m_pref = list_first_entry_or_null(
7004 &aconnector->base.modes, struct drm_display_mode, head);
7005 if (!m_pref) {
7006 drm_dbg_driver(aconnector->base.dev, "No preferred mode found in EDID\n");
7007 return NULL;
7008 }
7009 }
7010
7011 highest_refresh = drm_mode_vrefresh(m_pref);
7012
7013 /*
7014 * Find the mode with highest refresh rate with same resolution.
7015 * For some monitors, preferred mode is not the mode with highest
7016 * supported refresh rate.
7017 */
7018 list_for_each_entry(m, list_head, head) {
7019 current_refresh = drm_mode_vrefresh(m);
7020
7021 if (m->hdisplay == m_pref->hdisplay &&
7022 m->vdisplay == m_pref->vdisplay &&
7023 highest_refresh < current_refresh) {
7024 highest_refresh = current_refresh;
7025 m_pref = m;
7026 }
7027 }
7028
7029 drm_mode_copy(&aconnector->freesync_vid_base, m_pref);
7030 return m_pref;
7031 }
7032
is_freesync_video_mode(const struct drm_display_mode * mode,struct amdgpu_dm_connector * aconnector)7033 static bool is_freesync_video_mode(const struct drm_display_mode *mode,
7034 struct amdgpu_dm_connector *aconnector)
7035 {
7036 struct drm_display_mode *high_mode;
7037 int timing_diff;
7038
7039 high_mode = get_highest_refresh_rate_mode(aconnector, false);
7040 if (!high_mode || !mode)
7041 return false;
7042
7043 timing_diff = high_mode->vtotal - mode->vtotal;
7044
7045 if (high_mode->clock == 0 || high_mode->clock != mode->clock ||
7046 high_mode->hdisplay != mode->hdisplay ||
7047 high_mode->vdisplay != mode->vdisplay ||
7048 high_mode->hsync_start != mode->hsync_start ||
7049 high_mode->hsync_end != mode->hsync_end ||
7050 high_mode->htotal != mode->htotal ||
7051 high_mode->hskew != mode->hskew ||
7052 high_mode->vscan != mode->vscan ||
7053 high_mode->vsync_start - mode->vsync_start != timing_diff ||
7054 high_mode->vsync_end - mode->vsync_end != timing_diff)
7055 return false;
7056 else
7057 return true;
7058 }
7059
7060 #if defined(CONFIG_DRM_AMD_DC_FP)
update_dsc_caps(struct amdgpu_dm_connector * aconnector,struct dc_sink * sink,struct dc_stream_state * stream,struct dsc_dec_dpcd_caps * dsc_caps)7061 static void update_dsc_caps(struct amdgpu_dm_connector *aconnector,
7062 struct dc_sink *sink, struct dc_stream_state *stream,
7063 struct dsc_dec_dpcd_caps *dsc_caps)
7064 {
7065 stream->timing.flags.DSC = 0;
7066 dsc_caps->is_dsc_supported = false;
7067
7068 if (aconnector->dc_link && (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT ||
7069 sink->sink_signal == SIGNAL_TYPE_EDP)) {
7070 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE ||
7071 sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER)
7072 dc_dsc_parse_dsc_dpcd(aconnector->dc_link->ctx->dc,
7073 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.raw,
7074 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.raw,
7075 dsc_caps);
7076 }
7077 }
7078
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)7079 static void apply_dsc_policy_for_edp(struct amdgpu_dm_connector *aconnector,
7080 struct dc_sink *sink, struct dc_stream_state *stream,
7081 struct dsc_dec_dpcd_caps *dsc_caps,
7082 uint32_t max_dsc_target_bpp_limit_override)
7083 {
7084 const struct dc_link_settings *verified_link_cap = NULL;
7085 u32 link_bw_in_kbps;
7086 u32 edp_min_bpp_x16, edp_max_bpp_x16;
7087 struct dc *dc = sink->ctx->dc;
7088 struct dc_dsc_bw_range bw_range = {0};
7089 struct dc_dsc_config dsc_cfg = {0};
7090 struct dc_dsc_config_options dsc_options = {0};
7091
7092 dc_dsc_get_default_config_option(dc, &dsc_options);
7093 dsc_options.max_target_bpp_limit_override_x16 = max_dsc_target_bpp_limit_override * 16;
7094
7095 verified_link_cap = dc_link_get_link_cap(stream->link);
7096 link_bw_in_kbps = dc_link_bandwidth_kbps(stream->link, verified_link_cap);
7097 edp_min_bpp_x16 = 8 * 16;
7098 edp_max_bpp_x16 = 8 * 16;
7099
7100 if (edp_max_bpp_x16 > dsc_caps->edp_max_bits_per_pixel)
7101 edp_max_bpp_x16 = dsc_caps->edp_max_bits_per_pixel;
7102
7103 if (edp_max_bpp_x16 < edp_min_bpp_x16)
7104 edp_min_bpp_x16 = edp_max_bpp_x16;
7105
7106 if (dc_dsc_compute_bandwidth_range(dc->res_pool->dscs[0],
7107 dc->debug.dsc_min_slice_height_override,
7108 edp_min_bpp_x16, edp_max_bpp_x16,
7109 dsc_caps,
7110 &stream->timing,
7111 dc_link_get_highest_encoding_format(aconnector->dc_link),
7112 &bw_range)) {
7113
7114 if (bw_range.max_kbps < link_bw_in_kbps) {
7115 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
7116 dsc_caps,
7117 &dsc_options,
7118 0,
7119 &stream->timing,
7120 dc_link_get_highest_encoding_format(aconnector->dc_link),
7121 &dsc_cfg)) {
7122 stream->timing.dsc_cfg = dsc_cfg;
7123 stream->timing.flags.DSC = 1;
7124 stream->timing.dsc_cfg.bits_per_pixel = edp_max_bpp_x16;
7125 }
7126 return;
7127 }
7128 }
7129
7130 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
7131 dsc_caps,
7132 &dsc_options,
7133 link_bw_in_kbps,
7134 &stream->timing,
7135 dc_link_get_highest_encoding_format(aconnector->dc_link),
7136 &dsc_cfg)) {
7137 stream->timing.dsc_cfg = dsc_cfg;
7138 stream->timing.flags.DSC = 1;
7139 }
7140 }
7141
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)7142 static void apply_dsc_policy_for_stream(struct amdgpu_dm_connector *aconnector,
7143 struct dc_sink *sink, struct dc_stream_state *stream,
7144 struct dsc_dec_dpcd_caps *dsc_caps)
7145 {
7146 struct drm_connector *drm_connector = &aconnector->base;
7147 u32 link_bandwidth_kbps;
7148 struct dc *dc = sink->ctx->dc;
7149 u32 max_supported_bw_in_kbps, timing_bw_in_kbps;
7150 u32 dsc_max_supported_bw_in_kbps;
7151 u32 max_dsc_target_bpp_limit_override =
7152 drm_connector->display_info.max_dsc_bpp;
7153 struct dc_dsc_config_options dsc_options = {0};
7154
7155 dc_dsc_get_default_config_option(dc, &dsc_options);
7156 dsc_options.max_target_bpp_limit_override_x16 = max_dsc_target_bpp_limit_override * 16;
7157
7158 link_bandwidth_kbps = dc_link_bandwidth_kbps(aconnector->dc_link,
7159 dc_link_get_link_cap(aconnector->dc_link));
7160
7161 /* Set DSC policy according to dsc_clock_en */
7162 dc_dsc_policy_set_enable_dsc_when_not_needed(
7163 aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE);
7164
7165 if (sink->sink_signal == SIGNAL_TYPE_EDP &&
7166 !aconnector->dc_link->panel_config.dsc.disable_dsc_edp &&
7167 dc->caps.edp_dsc_support && aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE) {
7168
7169 apply_dsc_policy_for_edp(aconnector, sink, stream, dsc_caps, max_dsc_target_bpp_limit_override);
7170
7171 } else if (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT) {
7172 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE) {
7173 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
7174 dsc_caps,
7175 &dsc_options,
7176 link_bandwidth_kbps,
7177 &stream->timing,
7178 dc_link_get_highest_encoding_format(aconnector->dc_link),
7179 &stream->timing.dsc_cfg)) {
7180 stream->timing.flags.DSC = 1;
7181 drm_dbg_driver(drm_connector->dev, "%s: SST_DSC [%s] DSC is selected from SST RX\n",
7182 __func__, drm_connector->name);
7183 }
7184 } else if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER) {
7185 timing_bw_in_kbps = dc_bandwidth_in_kbps_from_timing(&stream->timing,
7186 dc_link_get_highest_encoding_format(aconnector->dc_link));
7187 max_supported_bw_in_kbps = link_bandwidth_kbps;
7188 dsc_max_supported_bw_in_kbps = link_bandwidth_kbps;
7189
7190 if (timing_bw_in_kbps > max_supported_bw_in_kbps &&
7191 max_supported_bw_in_kbps > 0 &&
7192 dsc_max_supported_bw_in_kbps > 0)
7193 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
7194 dsc_caps,
7195 &dsc_options,
7196 dsc_max_supported_bw_in_kbps,
7197 &stream->timing,
7198 dc_link_get_highest_encoding_format(aconnector->dc_link),
7199 &stream->timing.dsc_cfg)) {
7200 stream->timing.flags.DSC = 1;
7201 drm_dbg_driver(drm_connector->dev, "%s: SST_DSC [%s] DSC is selected from DP-HDMI PCON\n",
7202 __func__, drm_connector->name);
7203 }
7204 }
7205 }
7206
7207 /* Overwrite the stream flag if DSC is enabled through debugfs */
7208 if (aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE)
7209 stream->timing.flags.DSC = 1;
7210
7211 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_h)
7212 stream->timing.dsc_cfg.num_slices_h = aconnector->dsc_settings.dsc_num_slices_h;
7213
7214 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_v)
7215 stream->timing.dsc_cfg.num_slices_v = aconnector->dsc_settings.dsc_num_slices_v;
7216
7217 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_bits_per_pixel)
7218 stream->timing.dsc_cfg.bits_per_pixel = aconnector->dsc_settings.dsc_bits_per_pixel;
7219 }
7220 #endif
7221
7222 static struct dc_stream_state *
create_stream_for_sink(struct drm_connector * connector,const struct drm_display_mode * drm_mode,const struct dm_connector_state * dm_state,const struct dc_stream_state * old_stream,int requested_bpc)7223 create_stream_for_sink(struct drm_connector *connector,
7224 const struct drm_display_mode *drm_mode,
7225 const struct dm_connector_state *dm_state,
7226 const struct dc_stream_state *old_stream,
7227 int requested_bpc)
7228 {
7229 struct drm_device *dev = connector->dev;
7230 struct amdgpu_dm_connector *aconnector = NULL;
7231 struct drm_display_mode *preferred_mode = NULL;
7232 const struct drm_connector_state *con_state = &dm_state->base;
7233 struct dc_stream_state *stream = NULL;
7234 struct drm_display_mode mode;
7235 struct drm_display_mode saved_mode;
7236 struct drm_display_mode *freesync_mode = NULL;
7237 bool native_mode_found = false;
7238 bool recalculate_timing = false;
7239 bool scale = dm_state->scaling != RMX_OFF;
7240 int mode_refresh;
7241 int preferred_refresh = 0;
7242 enum color_transfer_func tf = TRANSFER_FUNC_UNKNOWN;
7243 #if defined(CONFIG_DRM_AMD_DC_FP)
7244 struct dsc_dec_dpcd_caps dsc_caps;
7245 #endif
7246 struct dc_link *link = NULL;
7247 struct dc_sink *sink = NULL;
7248
7249 drm_mode_init(&mode, drm_mode);
7250 memset(&saved_mode, 0, sizeof(saved_mode));
7251
7252 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) {
7253 aconnector = NULL;
7254 aconnector = to_amdgpu_dm_connector(connector);
7255 link = aconnector->dc_link;
7256 } else {
7257 struct drm_writeback_connector *wbcon = NULL;
7258 struct amdgpu_dm_wb_connector *dm_wbcon = NULL;
7259
7260 wbcon = drm_connector_to_writeback(connector);
7261 dm_wbcon = to_amdgpu_dm_wb_connector(wbcon);
7262 link = dm_wbcon->link;
7263 }
7264
7265 if (!aconnector || !aconnector->dc_sink) {
7266 sink = create_fake_sink(dev, link);
7267 if (!sink)
7268 return stream;
7269
7270 } else {
7271 sink = aconnector->dc_sink;
7272 dc_sink_retain(sink);
7273 }
7274
7275 stream = dc_create_stream_for_sink(sink);
7276
7277 if (stream == NULL) {
7278 drm_err(dev, "Failed to create stream for sink!\n");
7279 goto finish;
7280 }
7281
7282 /* We leave this NULL for writeback connectors */
7283 stream->dm_stream_context = aconnector;
7284
7285 stream->timing.flags.LTE_340MCSC_SCRAMBLE =
7286 connector->display_info.hdmi.scdc.scrambling.low_rates;
7287
7288 list_for_each_entry(preferred_mode, &connector->modes, head) {
7289 /* Search for preferred mode */
7290 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
7291 native_mode_found = true;
7292 break;
7293 }
7294 }
7295 if (!native_mode_found)
7296 preferred_mode = list_first_entry_or_null(
7297 &connector->modes,
7298 struct drm_display_mode,
7299 head);
7300
7301 mode_refresh = drm_mode_vrefresh(&mode);
7302
7303 if (preferred_mode == NULL) {
7304 /*
7305 * This may not be an error, the use case is when we have no
7306 * usermode calls to reset and set mode upon hotplug. In this
7307 * case, we call set mode ourselves to restore the previous mode
7308 * and the modelist may not be filled in time.
7309 */
7310 drm_dbg_driver(dev, "No preferred mode found\n");
7311 } else if (aconnector) {
7312 recalculate_timing = amdgpu_freesync_vid_mode &&
7313 is_freesync_video_mode(&mode, aconnector);
7314 if (recalculate_timing) {
7315 freesync_mode = get_highest_refresh_rate_mode(aconnector, false);
7316 drm_mode_copy(&saved_mode, &mode);
7317 saved_mode.picture_aspect_ratio = mode.picture_aspect_ratio;
7318 drm_mode_copy(&mode, freesync_mode);
7319 mode.picture_aspect_ratio = saved_mode.picture_aspect_ratio;
7320 } else {
7321 decide_crtc_timing_for_drm_display_mode(
7322 &mode, preferred_mode, scale);
7323
7324 preferred_refresh = drm_mode_vrefresh(preferred_mode);
7325 }
7326 }
7327
7328 if (recalculate_timing)
7329 drm_mode_set_crtcinfo(&saved_mode, 0);
7330
7331 /*
7332 * If scaling is enabled and refresh rate didn't change
7333 * we copy the vic and polarities of the old timings
7334 */
7335 if (!scale || mode_refresh != preferred_refresh)
7336 fill_stream_properties_from_drm_display_mode(
7337 stream, &mode, connector, con_state, NULL,
7338 requested_bpc);
7339 else
7340 fill_stream_properties_from_drm_display_mode(
7341 stream, &mode, connector, con_state, old_stream,
7342 requested_bpc);
7343
7344 /* The rest isn't needed for writeback connectors */
7345 if (!aconnector)
7346 goto finish;
7347
7348 if (aconnector->timing_changed) {
7349 drm_dbg(aconnector->base.dev,
7350 "overriding timing for automated test, bpc %d, changing to %d\n",
7351 stream->timing.display_color_depth,
7352 aconnector->timing_requested->display_color_depth);
7353 stream->timing = *aconnector->timing_requested;
7354 }
7355
7356 #if defined(CONFIG_DRM_AMD_DC_FP)
7357 /* SST DSC determination policy */
7358 update_dsc_caps(aconnector, sink, stream, &dsc_caps);
7359 if (aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE && dsc_caps.is_dsc_supported)
7360 apply_dsc_policy_for_stream(aconnector, sink, stream, &dsc_caps);
7361 #endif
7362
7363 update_stream_scaling_settings(&mode, dm_state, stream);
7364
7365 fill_audio_info(
7366 &stream->audio_info,
7367 connector,
7368 sink);
7369
7370 update_stream_signal(stream, sink);
7371
7372 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
7373 mod_build_hf_vsif_infopacket(stream, &stream->vsp_infopacket);
7374
7375 if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT ||
7376 stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST ||
7377 stream->signal == SIGNAL_TYPE_EDP) {
7378 const struct dc_edid_caps *edid_caps;
7379 unsigned int disable_colorimetry = 0;
7380
7381 if (aconnector->dc_sink) {
7382 edid_caps = &aconnector->dc_sink->edid_caps;
7383 disable_colorimetry = edid_caps->panel_patch.disable_colorimetry;
7384 }
7385
7386 //
7387 // should decide stream support vsc sdp colorimetry capability
7388 // before building vsc info packet
7389 //
7390 stream->use_vsc_sdp_for_colorimetry = stream->link->dpcd_caps.dpcd_rev.raw >= 0x14 &&
7391 stream->link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED &&
7392 !disable_colorimetry;
7393
7394 if (stream->out_transfer_func.tf == TRANSFER_FUNCTION_GAMMA22)
7395 tf = TRANSFER_FUNC_GAMMA_22;
7396 mod_build_vsc_infopacket(stream, &stream->vsc_infopacket, stream->output_color_space, tf);
7397 aconnector->sr_skip_count = AMDGPU_DM_PSR_ENTRY_DELAY;
7398
7399 }
7400 finish:
7401 dc_sink_release(sink);
7402
7403 return stream;
7404 }
7405
7406 /**
7407 * amdgpu_dm_connector_poll - Poll a connector to see if it's connected to a display
7408 * @aconnector: DM connector to poll (owns @base drm_connector and @dc_link)
7409 * @force: if true, force polling even when DAC load detection was used
7410 *
7411 * Used for connectors that don't support HPD (hotplug detection) to
7412 * periodically check whether the connector is connected to a display.
7413 *
7414 * When connection was determined via DAC load detection, we avoid
7415 * re-running it on normal polls to prevent visible glitches, unless
7416 * @force is set.
7417 *
7418 * Return: The probed connector status (connected/disconnected/unknown).
7419 */
7420 static enum drm_connector_status
amdgpu_dm_connector_poll(struct amdgpu_dm_connector * aconnector,bool force)7421 amdgpu_dm_connector_poll(struct amdgpu_dm_connector *aconnector, bool force)
7422 {
7423 struct drm_connector *connector = &aconnector->base;
7424 struct drm_device *dev = connector->dev;
7425 struct amdgpu_device *adev = drm_to_adev(dev);
7426 struct dc_link *link = aconnector->dc_link;
7427 enum dc_connection_type conn_type = dc_connection_none;
7428 enum drm_connector_status status = connector_status_disconnected;
7429
7430 /* When we determined the connection using DAC load detection,
7431 * do NOT poll the connector do detect disconnect because
7432 * that would run DAC load detection again which can cause
7433 * visible visual glitches.
7434 *
7435 * Only allow to poll such a connector again when forcing.
7436 */
7437 if (!force && link->local_sink && link->type == dc_connection_dac_load)
7438 return connector->status;
7439
7440 mutex_lock(&aconnector->hpd_lock);
7441
7442 if (dc_link_detect_connection_type(aconnector->dc_link, &conn_type) &&
7443 conn_type != dc_connection_none) {
7444 mutex_lock(&adev->dm.dc_lock);
7445
7446 /* Only call full link detection when a sink isn't created yet,
7447 * ie. just when the display is plugged in, otherwise we risk flickering.
7448 */
7449 if (link->local_sink ||
7450 dc_link_detect(link, DETECT_REASON_HPD))
7451 status = connector_status_connected;
7452
7453 mutex_unlock(&adev->dm.dc_lock);
7454 }
7455
7456 if (connector->status != status) {
7457 if (status == connector_status_disconnected) {
7458 if (link->local_sink)
7459 dc_sink_release(link->local_sink);
7460
7461 link->local_sink = NULL;
7462 link->dpcd_sink_count = 0;
7463 link->type = dc_connection_none;
7464 }
7465
7466 amdgpu_dm_update_connector_after_detect(aconnector);
7467 }
7468
7469 mutex_unlock(&aconnector->hpd_lock);
7470 return status;
7471 }
7472
7473 /**
7474 * amdgpu_dm_connector_detect() - Detect whether a DRM connector is connected to a display
7475 *
7476 * A connector is considered connected when it has a sink that is not NULL.
7477 * For connectors that support HPD (hotplug detection), the connection is
7478 * handled in the HPD interrupt.
7479 * For connectors that may not support HPD, such as analog connectors,
7480 * DRM will call this function repeatedly to poll them.
7481 *
7482 * Notes:
7483 * 1. This interface is NOT called in context of HPD irq.
7484 * 2. This interface *is called* in context of user-mode ioctl. Which
7485 * makes it a bad place for *any* MST-related activity.
7486 *
7487 * @connector: The DRM connector we are checking. We convert it to
7488 * amdgpu_dm_connector so we can read the DC link and state.
7489 * @force: If true, do a full detect again. This is used even when
7490 * a lighter check would normally be used to avoid flicker.
7491 *
7492 * Return: The connector status (connected, disconnected, or unknown).
7493 *
7494 */
7495 static enum drm_connector_status
amdgpu_dm_connector_detect(struct drm_connector * connector,bool force)7496 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
7497 {
7498 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7499
7500 update_subconnector_property(aconnector);
7501
7502 if (aconnector->base.force == DRM_FORCE_ON ||
7503 aconnector->base.force == DRM_FORCE_ON_DIGITAL)
7504 return connector_status_connected;
7505 else if (aconnector->base.force == DRM_FORCE_OFF)
7506 return connector_status_disconnected;
7507
7508 /* Poll analog connectors and only when either
7509 * disconnected or connected to an analog display.
7510 */
7511 if (drm_kms_helper_is_poll_worker() &&
7512 dc_connector_supports_analog(aconnector->dc_link->link_id.id) &&
7513 (!aconnector->dc_sink || aconnector->dc_sink->edid_caps.analog))
7514 return amdgpu_dm_connector_poll(aconnector, force);
7515
7516 return (aconnector->dc_sink ? connector_status_connected :
7517 connector_status_disconnected);
7518 }
7519
amdgpu_dm_connector_atomic_set_property(struct drm_connector * connector,struct drm_connector_state * connector_state,struct drm_property * property,uint64_t val)7520 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
7521 struct drm_connector_state *connector_state,
7522 struct drm_property *property,
7523 uint64_t val)
7524 {
7525 struct drm_device *dev = connector->dev;
7526 struct amdgpu_device *adev = drm_to_adev(dev);
7527 struct dm_connector_state *dm_old_state =
7528 to_dm_connector_state(connector->state);
7529 struct dm_connector_state *dm_new_state =
7530 to_dm_connector_state(connector_state);
7531
7532 int ret = -EINVAL;
7533
7534 if (property == dev->mode_config.scaling_mode_property) {
7535 enum amdgpu_rmx_type rmx_type;
7536
7537 switch (val) {
7538 case DRM_MODE_SCALE_CENTER:
7539 rmx_type = RMX_CENTER;
7540 break;
7541 case DRM_MODE_SCALE_ASPECT:
7542 rmx_type = RMX_ASPECT;
7543 break;
7544 case DRM_MODE_SCALE_FULLSCREEN:
7545 rmx_type = RMX_FULL;
7546 break;
7547 case DRM_MODE_SCALE_NONE:
7548 default:
7549 rmx_type = RMX_OFF;
7550 break;
7551 }
7552
7553 if (dm_old_state->scaling == rmx_type)
7554 return 0;
7555
7556 dm_new_state->scaling = rmx_type;
7557 ret = 0;
7558 } else if (property == adev->mode_info.underscan_hborder_property) {
7559 dm_new_state->underscan_hborder = val;
7560 ret = 0;
7561 } else if (property == adev->mode_info.underscan_vborder_property) {
7562 dm_new_state->underscan_vborder = val;
7563 ret = 0;
7564 } else if (property == adev->mode_info.underscan_property) {
7565 dm_new_state->underscan_enable = val;
7566 ret = 0;
7567 } else if (property == adev->mode_info.abm_level_property) {
7568 switch (val) {
7569 case ABM_SYSFS_CONTROL:
7570 dm_new_state->abm_sysfs_forbidden = false;
7571 break;
7572 case ABM_LEVEL_OFF:
7573 dm_new_state->abm_sysfs_forbidden = true;
7574 dm_new_state->abm_level = ABM_LEVEL_IMMEDIATE_DISABLE;
7575 break;
7576 default:
7577 dm_new_state->abm_sysfs_forbidden = true;
7578 dm_new_state->abm_level = val;
7579 }
7580 ret = 0;
7581 }
7582
7583 return ret;
7584 }
7585
amdgpu_dm_connector_atomic_get_property(struct drm_connector * connector,const struct drm_connector_state * state,struct drm_property * property,uint64_t * val)7586 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
7587 const struct drm_connector_state *state,
7588 struct drm_property *property,
7589 uint64_t *val)
7590 {
7591 struct drm_device *dev = connector->dev;
7592 struct amdgpu_device *adev = drm_to_adev(dev);
7593 struct dm_connector_state *dm_state =
7594 to_dm_connector_state(state);
7595 int ret = -EINVAL;
7596
7597 if (property == dev->mode_config.scaling_mode_property) {
7598 switch (dm_state->scaling) {
7599 case RMX_CENTER:
7600 *val = DRM_MODE_SCALE_CENTER;
7601 break;
7602 case RMX_ASPECT:
7603 *val = DRM_MODE_SCALE_ASPECT;
7604 break;
7605 case RMX_FULL:
7606 *val = DRM_MODE_SCALE_FULLSCREEN;
7607 break;
7608 case RMX_OFF:
7609 default:
7610 *val = DRM_MODE_SCALE_NONE;
7611 break;
7612 }
7613 ret = 0;
7614 } else if (property == adev->mode_info.underscan_hborder_property) {
7615 *val = dm_state->underscan_hborder;
7616 ret = 0;
7617 } else if (property == adev->mode_info.underscan_vborder_property) {
7618 *val = dm_state->underscan_vborder;
7619 ret = 0;
7620 } else if (property == adev->mode_info.underscan_property) {
7621 *val = dm_state->underscan_enable;
7622 ret = 0;
7623 } else if (property == adev->mode_info.abm_level_property) {
7624 if (!dm_state->abm_sysfs_forbidden)
7625 *val = ABM_SYSFS_CONTROL;
7626 else
7627 *val = (dm_state->abm_level != ABM_LEVEL_IMMEDIATE_DISABLE) ?
7628 dm_state->abm_level : 0;
7629 ret = 0;
7630 }
7631
7632 return ret;
7633 }
7634
7635 /**
7636 * DOC: panel power savings
7637 *
7638 * The display manager allows you to set your desired **panel power savings**
7639 * level (between 0-4, with 0 representing off), e.g. using the following::
7640 *
7641 * # echo 3 > /sys/class/drm/card0-eDP-1/amdgpu/panel_power_savings
7642 *
7643 * Modifying this value can have implications on color accuracy, so tread
7644 * carefully.
7645 */
7646
panel_power_savings_show(struct device * device,struct device_attribute * attr,char * buf)7647 static ssize_t panel_power_savings_show(struct device *device,
7648 struct device_attribute *attr,
7649 char *buf)
7650 {
7651 struct drm_connector *connector = dev_get_drvdata(device);
7652 struct drm_device *dev = connector->dev;
7653 u8 val;
7654
7655 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
7656 val = to_dm_connector_state(connector->state)->abm_level ==
7657 ABM_LEVEL_IMMEDIATE_DISABLE ? 0 :
7658 to_dm_connector_state(connector->state)->abm_level;
7659 drm_modeset_unlock(&dev->mode_config.connection_mutex);
7660
7661 return sysfs_emit(buf, "%u\n", val);
7662 }
7663
panel_power_savings_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)7664 static ssize_t panel_power_savings_store(struct device *device,
7665 struct device_attribute *attr,
7666 const char *buf, size_t count)
7667 {
7668 struct drm_connector *connector = dev_get_drvdata(device);
7669 struct drm_device *dev = connector->dev;
7670 long val;
7671 int ret;
7672
7673 ret = kstrtol(buf, 0, &val);
7674
7675 if (ret)
7676 return ret;
7677
7678 if (val < 0 || val > 4)
7679 return -EINVAL;
7680
7681 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
7682 if (to_dm_connector_state(connector->state)->abm_sysfs_forbidden)
7683 ret = -EBUSY;
7684 else
7685 to_dm_connector_state(connector->state)->abm_level = val ?:
7686 ABM_LEVEL_IMMEDIATE_DISABLE;
7687 drm_modeset_unlock(&dev->mode_config.connection_mutex);
7688
7689 if (ret)
7690 return ret;
7691
7692 drm_kms_helper_hotplug_event(dev);
7693
7694 return count;
7695 }
7696
7697 static DEVICE_ATTR_RW(panel_power_savings);
7698
7699 static struct attribute *amdgpu_attrs[] = {
7700 &dev_attr_panel_power_savings.attr,
7701 NULL
7702 };
7703
7704 static const struct attribute_group amdgpu_group = {
7705 .name = "amdgpu",
7706 .attrs = amdgpu_attrs
7707 };
7708
7709 static bool
amdgpu_dm_should_create_sysfs(struct amdgpu_dm_connector * amdgpu_dm_connector)7710 amdgpu_dm_should_create_sysfs(struct amdgpu_dm_connector *amdgpu_dm_connector)
7711 {
7712 if (amdgpu_dm_abm_level >= 0)
7713 return false;
7714
7715 if (amdgpu_dm_connector->base.connector_type != DRM_MODE_CONNECTOR_eDP)
7716 return false;
7717
7718 /* check for OLED panels */
7719 if (amdgpu_dm_connector->bl_idx >= 0) {
7720 struct drm_device *drm = amdgpu_dm_connector->base.dev;
7721 struct amdgpu_display_manager *dm = &drm_to_adev(drm)->dm;
7722 struct amdgpu_dm_backlight_caps *caps;
7723
7724 caps = &dm->backlight_caps[amdgpu_dm_connector->bl_idx];
7725 if (caps->aux_support)
7726 return false;
7727 }
7728
7729 return true;
7730 }
7731
amdgpu_dm_connector_unregister(struct drm_connector * connector)7732 static void amdgpu_dm_connector_unregister(struct drm_connector *connector)
7733 {
7734 struct amdgpu_dm_connector *amdgpu_dm_connector = to_amdgpu_dm_connector(connector);
7735
7736 if (amdgpu_dm_should_create_sysfs(amdgpu_dm_connector))
7737 sysfs_remove_group(&connector->kdev->kobj, &amdgpu_group);
7738
7739 cec_notifier_conn_unregister(amdgpu_dm_connector->notifier);
7740 drm_dp_aux_unregister(&amdgpu_dm_connector->dm_dp_aux.aux);
7741 }
7742
amdgpu_dm_connector_destroy(struct drm_connector * connector)7743 static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
7744 {
7745 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7746 struct amdgpu_device *adev = drm_to_adev(connector->dev);
7747 struct amdgpu_display_manager *dm = &adev->dm;
7748
7749 /*
7750 * Call only if mst_mgr was initialized before since it's not done
7751 * for all connector types.
7752 */
7753 if (aconnector->mst_mgr.dev)
7754 drm_dp_mst_topology_mgr_destroy(&aconnector->mst_mgr);
7755
7756 /* Cancel and flush any pending HDMI HPD debounce work */
7757 cancel_delayed_work_sync(&aconnector->hdmi_hpd_debounce_work);
7758 if (aconnector->hdmi_prev_sink) {
7759 dc_sink_release(aconnector->hdmi_prev_sink);
7760 aconnector->hdmi_prev_sink = NULL;
7761 }
7762
7763 if (aconnector->bl_idx != -1) {
7764 backlight_device_unregister(dm->backlight_dev[aconnector->bl_idx]);
7765 dm->backlight_dev[aconnector->bl_idx] = NULL;
7766 }
7767
7768 if (aconnector->dc_em_sink)
7769 dc_sink_release(aconnector->dc_em_sink);
7770 aconnector->dc_em_sink = NULL;
7771 if (aconnector->dc_sink)
7772 dc_sink_release(aconnector->dc_sink);
7773 aconnector->dc_sink = NULL;
7774
7775 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
7776 drm_connector_unregister(connector);
7777 drm_connector_cleanup(connector);
7778 kfree(aconnector->dm_dp_aux.aux.name);
7779
7780 kfree(connector);
7781 }
7782
amdgpu_dm_connector_funcs_reset(struct drm_connector * connector)7783 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
7784 {
7785 struct dm_connector_state *state =
7786 to_dm_connector_state(connector->state);
7787
7788 if (connector->state)
7789 __drm_atomic_helper_connector_destroy_state(connector->state);
7790
7791 kfree(state);
7792
7793 state = kzalloc(sizeof(*state), GFP_KERNEL);
7794
7795 if (state) {
7796 state->scaling = RMX_OFF;
7797 state->underscan_enable = false;
7798 state->underscan_hborder = 0;
7799 state->underscan_vborder = 0;
7800 state->base.max_requested_bpc = 8;
7801 state->vcpi_slots = 0;
7802 state->pbn = 0;
7803
7804 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
7805 if (amdgpu_dm_abm_level <= 0)
7806 state->abm_level = ABM_LEVEL_IMMEDIATE_DISABLE;
7807 else
7808 state->abm_level = amdgpu_dm_abm_level;
7809 }
7810
7811 __drm_atomic_helper_connector_reset(connector, &state->base);
7812 }
7813 }
7814
7815 struct drm_connector_state *
amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector * connector)7816 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
7817 {
7818 struct dm_connector_state *state =
7819 to_dm_connector_state(connector->state);
7820
7821 struct dm_connector_state *new_state =
7822 kmemdup(state, sizeof(*state), GFP_KERNEL);
7823
7824 if (!new_state)
7825 return NULL;
7826
7827 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
7828
7829 new_state->freesync_capable = state->freesync_capable;
7830 new_state->abm_level = state->abm_level;
7831 new_state->scaling = state->scaling;
7832 new_state->underscan_enable = state->underscan_enable;
7833 new_state->underscan_hborder = state->underscan_hborder;
7834 new_state->underscan_vborder = state->underscan_vborder;
7835 new_state->vcpi_slots = state->vcpi_slots;
7836 new_state->pbn = state->pbn;
7837 return &new_state->base;
7838 }
7839
7840 static int
amdgpu_dm_connector_late_register(struct drm_connector * connector)7841 amdgpu_dm_connector_late_register(struct drm_connector *connector)
7842 {
7843 struct amdgpu_dm_connector *amdgpu_dm_connector =
7844 to_amdgpu_dm_connector(connector);
7845 int r;
7846
7847 if (amdgpu_dm_should_create_sysfs(amdgpu_dm_connector)) {
7848 r = sysfs_create_group(&connector->kdev->kobj,
7849 &amdgpu_group);
7850 if (r)
7851 return r;
7852 }
7853
7854 amdgpu_dm_register_backlight_device(amdgpu_dm_connector);
7855
7856 if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
7857 (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) {
7858 amdgpu_dm_connector->dm_dp_aux.aux.dev = connector->kdev;
7859 r = drm_dp_aux_register(&amdgpu_dm_connector->dm_dp_aux.aux);
7860 if (r)
7861 return r;
7862 }
7863
7864 #if defined(CONFIG_DEBUG_FS)
7865 connector_debugfs_init(amdgpu_dm_connector);
7866 #endif
7867
7868 return 0;
7869 }
7870
amdgpu_dm_connector_funcs_force(struct drm_connector * connector)7871 static void amdgpu_dm_connector_funcs_force(struct drm_connector *connector)
7872 {
7873 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7874 struct dc_link *dc_link = aconnector->dc_link;
7875 struct dc_sink *dc_em_sink = aconnector->dc_em_sink;
7876 const struct drm_edid *drm_edid;
7877 struct i2c_adapter *ddc;
7878 struct drm_device *dev = connector->dev;
7879
7880 if (dc_link && dc_link->aux_mode)
7881 ddc = &aconnector->dm_dp_aux.aux.ddc;
7882 else
7883 ddc = &aconnector->i2c->base;
7884
7885 drm_edid = drm_edid_read_ddc(connector, ddc);
7886 drm_edid_connector_update(connector, drm_edid);
7887 if (!drm_edid) {
7888 drm_err(dev, "No EDID found on connector: %s.\n", connector->name);
7889 return;
7890 }
7891
7892 aconnector->drm_edid = drm_edid;
7893 /* Update emulated (virtual) sink's EDID */
7894 if (dc_em_sink && dc_link) {
7895 // FIXME: Get rid of drm_edid_raw()
7896 const struct edid *edid = drm_edid_raw(drm_edid);
7897
7898 memset(&dc_em_sink->edid_caps, 0, sizeof(struct dc_edid_caps));
7899 memmove(dc_em_sink->dc_edid.raw_edid, edid,
7900 (edid->extensions + 1) * EDID_LENGTH);
7901 dm_helpers_parse_edid_caps(
7902 dc_link,
7903 &dc_em_sink->dc_edid,
7904 &dc_em_sink->edid_caps);
7905 }
7906 }
7907
7908 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
7909 .reset = amdgpu_dm_connector_funcs_reset,
7910 .detect = amdgpu_dm_connector_detect,
7911 .fill_modes = drm_helper_probe_single_connector_modes,
7912 .destroy = amdgpu_dm_connector_destroy,
7913 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
7914 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
7915 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
7916 .atomic_get_property = amdgpu_dm_connector_atomic_get_property,
7917 .late_register = amdgpu_dm_connector_late_register,
7918 .early_unregister = amdgpu_dm_connector_unregister,
7919 .force = amdgpu_dm_connector_funcs_force
7920 };
7921
get_modes(struct drm_connector * connector)7922 static int get_modes(struct drm_connector *connector)
7923 {
7924 return amdgpu_dm_connector_get_modes(connector);
7925 }
7926
create_eml_sink(struct amdgpu_dm_connector * aconnector)7927 static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
7928 {
7929 struct drm_connector *connector = &aconnector->base;
7930 struct dc_link *dc_link = aconnector->dc_link;
7931 struct dc_sink_init_data init_params = {
7932 .link = aconnector->dc_link,
7933 .sink_signal = SIGNAL_TYPE_VIRTUAL
7934 };
7935 const struct drm_edid *drm_edid;
7936 const struct edid *edid;
7937 struct i2c_adapter *ddc;
7938
7939 if (dc_link && dc_link->aux_mode)
7940 ddc = &aconnector->dm_dp_aux.aux.ddc;
7941 else
7942 ddc = &aconnector->i2c->base;
7943
7944 drm_edid = drm_edid_read_ddc(connector, ddc);
7945 drm_edid_connector_update(connector, drm_edid);
7946 if (!drm_edid) {
7947 drm_err(connector->dev, "No EDID found on connector: %s.\n", connector->name);
7948 return;
7949 }
7950
7951 if (connector->display_info.is_hdmi)
7952 init_params.sink_signal = SIGNAL_TYPE_HDMI_TYPE_A;
7953
7954 aconnector->drm_edid = drm_edid;
7955
7956 edid = drm_edid_raw(drm_edid); // FIXME: Get rid of drm_edid_raw()
7957 aconnector->dc_em_sink = dc_link_add_remote_sink(
7958 aconnector->dc_link,
7959 (uint8_t *)edid,
7960 (edid->extensions + 1) * EDID_LENGTH,
7961 &init_params);
7962
7963 if (aconnector->base.force == DRM_FORCE_ON) {
7964 aconnector->dc_sink = aconnector->dc_link->local_sink ?
7965 aconnector->dc_link->local_sink :
7966 aconnector->dc_em_sink;
7967 if (aconnector->dc_sink)
7968 dc_sink_retain(aconnector->dc_sink);
7969 }
7970 }
7971
handle_edid_mgmt(struct amdgpu_dm_connector * aconnector)7972 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
7973 {
7974 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
7975
7976 /*
7977 * In case of headless boot with force on for DP managed connector
7978 * Those settings have to be != 0 to get initial modeset
7979 */
7980 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
7981 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
7982 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
7983 }
7984
7985 create_eml_sink(aconnector);
7986 }
7987
dm_validate_stream_and_context(struct dc * dc,struct dc_stream_state * stream)7988 static enum dc_status dm_validate_stream_and_context(struct dc *dc,
7989 struct dc_stream_state *stream)
7990 {
7991 enum dc_status dc_result = DC_ERROR_UNEXPECTED;
7992 struct dc_plane_state *dc_plane_state = NULL;
7993 struct dc_state *dc_state = NULL;
7994
7995 if (!stream)
7996 goto cleanup;
7997
7998 dc_plane_state = dc_create_plane_state(dc);
7999 if (!dc_plane_state)
8000 goto cleanup;
8001
8002 dc_state = dc_state_create(dc, NULL);
8003 if (!dc_state)
8004 goto cleanup;
8005
8006 /* populate stream to plane */
8007 dc_plane_state->src_rect.height = stream->src.height;
8008 dc_plane_state->src_rect.width = stream->src.width;
8009 dc_plane_state->dst_rect.height = stream->src.height;
8010 dc_plane_state->dst_rect.width = stream->src.width;
8011 dc_plane_state->clip_rect.height = stream->src.height;
8012 dc_plane_state->clip_rect.width = stream->src.width;
8013 dc_plane_state->plane_size.surface_pitch = ((stream->src.width + 255) / 256) * 256;
8014 dc_plane_state->plane_size.surface_size.height = stream->src.height;
8015 dc_plane_state->plane_size.surface_size.width = stream->src.width;
8016 dc_plane_state->plane_size.chroma_size.height = stream->src.height;
8017 dc_plane_state->plane_size.chroma_size.width = stream->src.width;
8018 dc_plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
8019 dc_plane_state->tiling_info.gfx9.swizzle = DC_SW_UNKNOWN;
8020 dc_plane_state->rotation = ROTATION_ANGLE_0;
8021 dc_plane_state->is_tiling_rotated = false;
8022 dc_plane_state->tiling_info.gfx8.array_mode = DC_ARRAY_LINEAR_GENERAL;
8023
8024 dc_result = dc_validate_stream(dc, stream);
8025 if (dc_result == DC_OK)
8026 dc_result = dc_validate_plane(dc, dc_plane_state);
8027
8028 if (dc_result == DC_OK)
8029 dc_result = dc_state_add_stream(dc, dc_state, stream);
8030
8031 if (dc_result == DC_OK && !dc_state_add_plane(
8032 dc,
8033 stream,
8034 dc_plane_state,
8035 dc_state))
8036 dc_result = DC_FAIL_ATTACH_SURFACES;
8037
8038 if (dc_result == DC_OK)
8039 dc_result = dc_validate_global_state(dc, dc_state, DC_VALIDATE_MODE_ONLY);
8040
8041 cleanup:
8042 if (dc_state)
8043 dc_state_release(dc_state);
8044
8045 if (dc_plane_state)
8046 dc_plane_state_release(dc_plane_state);
8047
8048 return dc_result;
8049 }
8050
8051 struct dc_stream_state *
create_validate_stream_for_sink(struct drm_connector * connector,const struct drm_display_mode * drm_mode,const struct dm_connector_state * dm_state,const struct dc_stream_state * old_stream)8052 create_validate_stream_for_sink(struct drm_connector *connector,
8053 const struct drm_display_mode *drm_mode,
8054 const struct dm_connector_state *dm_state,
8055 const struct dc_stream_state *old_stream)
8056 {
8057 struct amdgpu_dm_connector *aconnector = NULL;
8058 struct amdgpu_device *adev = drm_to_adev(connector->dev);
8059 struct dc_stream_state *stream;
8060 const struct drm_connector_state *drm_state = dm_state ? &dm_state->base : NULL;
8061 int requested_bpc = drm_state ? drm_state->max_requested_bpc : 8;
8062 enum dc_status dc_result = DC_OK;
8063 uint8_t bpc_limit = 6;
8064
8065 if (!dm_state)
8066 return NULL;
8067
8068 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
8069 aconnector = to_amdgpu_dm_connector(connector);
8070
8071 if (aconnector &&
8072 (aconnector->dc_link->connector_signal == SIGNAL_TYPE_HDMI_TYPE_A ||
8073 aconnector->dc_link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER))
8074 bpc_limit = 8;
8075
8076 do {
8077 drm_dbg_kms(connector->dev, "Trying with %d bpc\n", requested_bpc);
8078 stream = create_stream_for_sink(connector, drm_mode,
8079 dm_state, old_stream,
8080 requested_bpc);
8081 if (stream == NULL) {
8082 drm_err(adev_to_drm(adev), "Failed to create stream for sink!\n");
8083 break;
8084 }
8085
8086 dc_result = dc_validate_stream(adev->dm.dc, stream);
8087
8088 if (!aconnector) /* writeback connector */
8089 return stream;
8090
8091 if (dc_result == DC_OK && stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
8092 dc_result = dm_dp_mst_is_port_support_mode(aconnector, stream);
8093
8094 if (dc_result == DC_OK)
8095 dc_result = dm_validate_stream_and_context(adev->dm.dc, stream);
8096
8097 if (dc_result != DC_OK) {
8098 DRM_DEBUG_KMS("Pruned mode %d x %d (clk %d) %s %s -- %s\n",
8099 drm_mode->hdisplay,
8100 drm_mode->vdisplay,
8101 drm_mode->clock,
8102 dc_pixel_encoding_to_str(stream->timing.pixel_encoding),
8103 dc_color_depth_to_str(stream->timing.display_color_depth),
8104 dc_status_to_str(dc_result));
8105
8106 dc_stream_release(stream);
8107 stream = NULL;
8108 requested_bpc -= 2; /* lower bpc to retry validation */
8109 }
8110
8111 } while (stream == NULL && requested_bpc >= bpc_limit);
8112
8113 switch (dc_result) {
8114 /*
8115 * If we failed to validate DP bandwidth stream with the requested RGB color depth,
8116 * we try to fallback and configure in order:
8117 * YUV422 (8bpc, 6bpc)
8118 * YUV420 (8bpc, 6bpc)
8119 */
8120 case DC_FAIL_ENC_VALIDATE:
8121 case DC_EXCEED_DONGLE_CAP:
8122 case DC_NO_DP_LINK_BANDWIDTH:
8123 /* recursively entered twice and already tried both YUV422 and YUV420 */
8124 if (aconnector->force_yuv422_output && aconnector->force_yuv420_output)
8125 break;
8126 /* first failure; try YUV422 */
8127 if (!aconnector->force_yuv422_output) {
8128 drm_dbg_kms(connector->dev, "%s:%d Validation failed with %d, retrying w/ YUV422\n",
8129 __func__, __LINE__, dc_result);
8130 aconnector->force_yuv422_output = true;
8131 /* recursively entered and YUV422 failed, try YUV420 */
8132 } else if (!aconnector->force_yuv420_output) {
8133 drm_dbg_kms(connector->dev, "%s:%d Validation failed with %d, retrying w/ YUV420\n",
8134 __func__, __LINE__, dc_result);
8135 aconnector->force_yuv420_output = true;
8136 }
8137 stream = create_validate_stream_for_sink(connector, drm_mode,
8138 dm_state, old_stream);
8139 aconnector->force_yuv422_output = false;
8140 aconnector->force_yuv420_output = false;
8141 break;
8142 case DC_OK:
8143 break;
8144 default:
8145 drm_dbg_kms(connector->dev, "%s:%d Unhandled validation failure %d\n",
8146 __func__, __LINE__, dc_result);
8147 break;
8148 }
8149
8150 return stream;
8151 }
8152
amdgpu_dm_connector_mode_valid(struct drm_connector * connector,const struct drm_display_mode * mode)8153 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
8154 const struct drm_display_mode *mode)
8155 {
8156 int result = MODE_ERROR;
8157 struct dc_sink *dc_sink;
8158 struct drm_display_mode *test_mode;
8159 /* TODO: Unhardcode stream count */
8160 struct dc_stream_state *stream;
8161 /* we always have an amdgpu_dm_connector here since we got
8162 * here via the amdgpu_dm_connector_helper_funcs
8163 */
8164 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
8165
8166 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
8167 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
8168 return result;
8169
8170 /*
8171 * Only run this the first time mode_valid is called to initilialize
8172 * EDID mgmt
8173 */
8174 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
8175 !aconnector->dc_em_sink)
8176 handle_edid_mgmt(aconnector);
8177
8178 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
8179
8180 if (dc_sink == NULL && aconnector->base.force != DRM_FORCE_ON_DIGITAL &&
8181 aconnector->base.force != DRM_FORCE_ON) {
8182 drm_err(connector->dev, "dc_sink is NULL!\n");
8183 goto fail;
8184 }
8185
8186 test_mode = drm_mode_duplicate(connector->dev, mode);
8187 if (!test_mode)
8188 goto fail;
8189
8190 drm_mode_set_crtcinfo(test_mode, 0);
8191
8192 stream = create_validate_stream_for_sink(connector, test_mode,
8193 to_dm_connector_state(connector->state),
8194 NULL);
8195 drm_mode_destroy(connector->dev, test_mode);
8196 if (stream) {
8197 dc_stream_release(stream);
8198 result = MODE_OK;
8199 }
8200
8201 fail:
8202 /* TODO: error handling*/
8203 return result;
8204 }
8205
fill_hdr_info_packet(const struct drm_connector_state * state,struct dc_info_packet * out)8206 static int fill_hdr_info_packet(const struct drm_connector_state *state,
8207 struct dc_info_packet *out)
8208 {
8209 struct hdmi_drm_infoframe frame;
8210 unsigned char buf[30]; /* 26 + 4 */
8211 ssize_t len;
8212 int ret, i;
8213
8214 memset(out, 0, sizeof(*out));
8215
8216 if (!state->hdr_output_metadata)
8217 return 0;
8218
8219 ret = drm_hdmi_infoframe_set_hdr_metadata(&frame, state);
8220 if (ret)
8221 return ret;
8222
8223 len = hdmi_drm_infoframe_pack_only(&frame, buf, sizeof(buf));
8224 if (len < 0)
8225 return (int)len;
8226
8227 /* Static metadata is a fixed 26 bytes + 4 byte header. */
8228 if (len != 30)
8229 return -EINVAL;
8230
8231 /* Prepare the infopacket for DC. */
8232 switch (state->connector->connector_type) {
8233 case DRM_MODE_CONNECTOR_HDMIA:
8234 out->hb0 = 0x87; /* type */
8235 out->hb1 = 0x01; /* version */
8236 out->hb2 = 0x1A; /* length */
8237 out->sb[0] = buf[3]; /* checksum */
8238 i = 1;
8239 break;
8240
8241 case DRM_MODE_CONNECTOR_DisplayPort:
8242 case DRM_MODE_CONNECTOR_eDP:
8243 out->hb0 = 0x00; /* sdp id, zero */
8244 out->hb1 = 0x87; /* type */
8245 out->hb2 = 0x1D; /* payload len - 1 */
8246 out->hb3 = (0x13 << 2); /* sdp version */
8247 out->sb[0] = 0x01; /* version */
8248 out->sb[1] = 0x1A; /* length */
8249 i = 2;
8250 break;
8251
8252 default:
8253 return -EINVAL;
8254 }
8255
8256 memcpy(&out->sb[i], &buf[4], 26);
8257 out->valid = true;
8258
8259 print_hex_dump(KERN_DEBUG, "HDR SB:", DUMP_PREFIX_NONE, 16, 1, out->sb,
8260 sizeof(out->sb), false);
8261
8262 return 0;
8263 }
8264
8265 static int
amdgpu_dm_connector_atomic_check(struct drm_connector * conn,struct drm_atomic_state * state)8266 amdgpu_dm_connector_atomic_check(struct drm_connector *conn,
8267 struct drm_atomic_state *state)
8268 {
8269 struct drm_connector_state *new_con_state =
8270 drm_atomic_get_new_connector_state(state, conn);
8271 struct drm_connector_state *old_con_state =
8272 drm_atomic_get_old_connector_state(state, conn);
8273 struct drm_crtc *crtc = new_con_state->crtc;
8274 struct drm_crtc_state *new_crtc_state;
8275 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(conn);
8276 int ret;
8277
8278 if (WARN_ON(unlikely(!old_con_state || !new_con_state)))
8279 return -EINVAL;
8280
8281 trace_amdgpu_dm_connector_atomic_check(new_con_state);
8282
8283 if (conn->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
8284 ret = drm_dp_mst_root_conn_atomic_check(new_con_state, &aconn->mst_mgr);
8285 if (ret < 0)
8286 return ret;
8287 }
8288
8289 if (!crtc)
8290 return 0;
8291
8292 if (new_con_state->privacy_screen_sw_state != old_con_state->privacy_screen_sw_state) {
8293 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8294 if (IS_ERR(new_crtc_state))
8295 return PTR_ERR(new_crtc_state);
8296
8297 new_crtc_state->mode_changed = true;
8298 }
8299
8300 if (new_con_state->colorspace != old_con_state->colorspace) {
8301 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8302 if (IS_ERR(new_crtc_state))
8303 return PTR_ERR(new_crtc_state);
8304
8305 new_crtc_state->mode_changed = true;
8306 }
8307
8308 if (new_con_state->content_type != old_con_state->content_type) {
8309 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8310 if (IS_ERR(new_crtc_state))
8311 return PTR_ERR(new_crtc_state);
8312
8313 new_crtc_state->mode_changed = true;
8314 }
8315
8316 if (!drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state)) {
8317 struct dc_info_packet hdr_infopacket;
8318
8319 ret = fill_hdr_info_packet(new_con_state, &hdr_infopacket);
8320 if (ret)
8321 return ret;
8322
8323 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
8324 if (IS_ERR(new_crtc_state))
8325 return PTR_ERR(new_crtc_state);
8326
8327 /*
8328 * DC considers the stream backends changed if the
8329 * static metadata changes. Forcing the modeset also
8330 * gives a simple way for userspace to switch from
8331 * 8bpc to 10bpc when setting the metadata to enter
8332 * or exit HDR.
8333 *
8334 * Changing the static metadata after it's been
8335 * set is permissible, however. So only force a
8336 * modeset if we're entering or exiting HDR.
8337 */
8338 new_crtc_state->mode_changed = new_crtc_state->mode_changed ||
8339 !old_con_state->hdr_output_metadata ||
8340 !new_con_state->hdr_output_metadata;
8341 }
8342
8343 return 0;
8344 }
8345
8346 static const struct drm_connector_helper_funcs
8347 amdgpu_dm_connector_helper_funcs = {
8348 /*
8349 * If hotplugging a second bigger display in FB Con mode, bigger resolution
8350 * modes will be filtered by drm_mode_validate_size(), and those modes
8351 * are missing after user start lightdm. So we need to renew modes list.
8352 * in get_modes call back, not just return the modes count
8353 */
8354 .get_modes = get_modes,
8355 .mode_valid = amdgpu_dm_connector_mode_valid,
8356 .atomic_check = amdgpu_dm_connector_atomic_check,
8357 };
8358
dm_encoder_helper_disable(struct drm_encoder * encoder)8359 static void dm_encoder_helper_disable(struct drm_encoder *encoder)
8360 {
8361
8362 }
8363
convert_dc_color_depth_into_bpc(enum dc_color_depth display_color_depth)8364 int convert_dc_color_depth_into_bpc(enum dc_color_depth display_color_depth)
8365 {
8366 switch (display_color_depth) {
8367 case COLOR_DEPTH_666:
8368 return 6;
8369 case COLOR_DEPTH_888:
8370 return 8;
8371 case COLOR_DEPTH_101010:
8372 return 10;
8373 case COLOR_DEPTH_121212:
8374 return 12;
8375 case COLOR_DEPTH_141414:
8376 return 14;
8377 case COLOR_DEPTH_161616:
8378 return 16;
8379 default:
8380 break;
8381 }
8382 return 0;
8383 }
8384
dm_encoder_helper_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)8385 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
8386 struct drm_crtc_state *crtc_state,
8387 struct drm_connector_state *conn_state)
8388 {
8389 struct drm_atomic_state *state = crtc_state->state;
8390 struct drm_connector *connector = conn_state->connector;
8391 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
8392 struct dm_connector_state *dm_new_connector_state = to_dm_connector_state(conn_state);
8393 const struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
8394 struct drm_dp_mst_topology_mgr *mst_mgr;
8395 struct drm_dp_mst_port *mst_port;
8396 struct drm_dp_mst_topology_state *mst_state;
8397 enum dc_color_depth color_depth;
8398 int clock, bpp = 0;
8399 bool is_y420 = false;
8400
8401 if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
8402 (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
8403 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8404 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8405 enum drm_mode_status result;
8406
8407 result = drm_crtc_helper_mode_valid_fixed(encoder->crtc, adjusted_mode, native_mode);
8408 if (result != MODE_OK && dm_new_connector_state->scaling == RMX_OFF) {
8409 drm_dbg_driver(encoder->dev,
8410 "mode %dx%d@%dHz is not native, enabling scaling\n",
8411 adjusted_mode->hdisplay, adjusted_mode->vdisplay,
8412 drm_mode_vrefresh(adjusted_mode));
8413 dm_new_connector_state->scaling = RMX_ASPECT;
8414 }
8415 return 0;
8416 }
8417
8418 if (!aconnector->mst_output_port)
8419 return 0;
8420
8421 mst_port = aconnector->mst_output_port;
8422 mst_mgr = &aconnector->mst_root->mst_mgr;
8423
8424 if (!crtc_state->connectors_changed && !crtc_state->mode_changed)
8425 return 0;
8426
8427 mst_state = drm_atomic_get_mst_topology_state(state, mst_mgr);
8428 if (IS_ERR(mst_state))
8429 return PTR_ERR(mst_state);
8430
8431 mst_state->pbn_div.full = dm_mst_get_pbn_divider(aconnector->mst_root->dc_link);
8432
8433 if (!state->duplicated) {
8434 int max_bpc = conn_state->max_requested_bpc;
8435
8436 is_y420 = drm_mode_is_420_also(&connector->display_info, adjusted_mode) &&
8437 aconnector->force_yuv420_output;
8438 color_depth = convert_color_depth_from_display_info(connector,
8439 is_y420,
8440 max_bpc);
8441 bpp = convert_dc_color_depth_into_bpc(color_depth) * 3;
8442 clock = adjusted_mode->clock;
8443 dm_new_connector_state->pbn = drm_dp_calc_pbn_mode(clock, bpp << 4);
8444 }
8445
8446 dm_new_connector_state->vcpi_slots =
8447 drm_dp_atomic_find_time_slots(state, mst_mgr, mst_port,
8448 dm_new_connector_state->pbn);
8449 if (dm_new_connector_state->vcpi_slots < 0) {
8450 DRM_DEBUG_ATOMIC("failed finding vcpi slots: %d\n", (int)dm_new_connector_state->vcpi_slots);
8451 return dm_new_connector_state->vcpi_slots;
8452 }
8453 return 0;
8454 }
8455
8456 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
8457 .disable = dm_encoder_helper_disable,
8458 .atomic_check = dm_encoder_helper_atomic_check
8459 };
8460
dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_state * state,struct dc_state * dc_state,struct dsc_mst_fairness_vars * vars)8461 static int dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_state *state,
8462 struct dc_state *dc_state,
8463 struct dsc_mst_fairness_vars *vars)
8464 {
8465 struct dc_stream_state *stream = NULL;
8466 struct drm_connector *connector;
8467 struct drm_connector_state *new_con_state;
8468 struct amdgpu_dm_connector *aconnector;
8469 struct dm_connector_state *dm_conn_state;
8470 int i, j, ret;
8471 int vcpi, pbn_div, pbn = 0, slot_num = 0;
8472
8473 for_each_new_connector_in_state(state, connector, new_con_state, i) {
8474
8475 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
8476 continue;
8477
8478 aconnector = to_amdgpu_dm_connector(connector);
8479
8480 if (!aconnector->mst_output_port)
8481 continue;
8482
8483 if (!new_con_state || !new_con_state->crtc)
8484 continue;
8485
8486 dm_conn_state = to_dm_connector_state(new_con_state);
8487
8488 for (j = 0; j < dc_state->stream_count; j++) {
8489 stream = dc_state->streams[j];
8490 if (!stream)
8491 continue;
8492
8493 if ((struct amdgpu_dm_connector *)stream->dm_stream_context == aconnector)
8494 break;
8495
8496 stream = NULL;
8497 }
8498
8499 if (!stream)
8500 continue;
8501
8502 pbn_div = dm_mst_get_pbn_divider(stream->link);
8503 /* pbn is calculated by compute_mst_dsc_configs_for_state*/
8504 for (j = 0; j < dc_state->stream_count; j++) {
8505 if (vars[j].aconnector == aconnector) {
8506 pbn = vars[j].pbn;
8507 break;
8508 }
8509 }
8510
8511 if (j == dc_state->stream_count || pbn_div == 0)
8512 continue;
8513
8514 slot_num = DIV_ROUND_UP(pbn, pbn_div);
8515
8516 if (stream->timing.flags.DSC != 1) {
8517 dm_conn_state->pbn = pbn;
8518 dm_conn_state->vcpi_slots = slot_num;
8519
8520 ret = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port,
8521 dm_conn_state->pbn, false);
8522 if (ret < 0)
8523 return ret;
8524
8525 continue;
8526 }
8527
8528 vcpi = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port, pbn, true);
8529 if (vcpi < 0)
8530 return vcpi;
8531
8532 dm_conn_state->pbn = pbn;
8533 dm_conn_state->vcpi_slots = vcpi;
8534 }
8535 return 0;
8536 }
8537
to_drm_connector_type(enum signal_type st,uint32_t connector_id)8538 static int to_drm_connector_type(enum signal_type st, uint32_t connector_id)
8539 {
8540 switch (st) {
8541 case SIGNAL_TYPE_HDMI_TYPE_A:
8542 return DRM_MODE_CONNECTOR_HDMIA;
8543 case SIGNAL_TYPE_EDP:
8544 return DRM_MODE_CONNECTOR_eDP;
8545 case SIGNAL_TYPE_LVDS:
8546 return DRM_MODE_CONNECTOR_LVDS;
8547 case SIGNAL_TYPE_RGB:
8548 return DRM_MODE_CONNECTOR_VGA;
8549 case SIGNAL_TYPE_DISPLAY_PORT:
8550 case SIGNAL_TYPE_DISPLAY_PORT_MST:
8551 return DRM_MODE_CONNECTOR_DisplayPort;
8552 case SIGNAL_TYPE_DVI_DUAL_LINK:
8553 case SIGNAL_TYPE_DVI_SINGLE_LINK:
8554 if (connector_id == CONNECTOR_ID_SINGLE_LINK_DVII ||
8555 connector_id == CONNECTOR_ID_DUAL_LINK_DVII)
8556 return DRM_MODE_CONNECTOR_DVII;
8557
8558 return DRM_MODE_CONNECTOR_DVID;
8559 case SIGNAL_TYPE_VIRTUAL:
8560 return DRM_MODE_CONNECTOR_VIRTUAL;
8561
8562 default:
8563 return DRM_MODE_CONNECTOR_Unknown;
8564 }
8565 }
8566
amdgpu_dm_connector_to_encoder(struct drm_connector * connector)8567 static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector)
8568 {
8569 struct drm_encoder *encoder;
8570
8571 /* There is only one encoder per connector */
8572 drm_connector_for_each_possible_encoder(connector, encoder)
8573 return encoder;
8574
8575 return NULL;
8576 }
8577
amdgpu_dm_get_native_mode(struct drm_connector * connector)8578 static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
8579 {
8580 struct drm_encoder *encoder;
8581 struct amdgpu_encoder *amdgpu_encoder;
8582
8583 encoder = amdgpu_dm_connector_to_encoder(connector);
8584
8585 if (encoder == NULL)
8586 return;
8587
8588 amdgpu_encoder = to_amdgpu_encoder(encoder);
8589
8590 amdgpu_encoder->native_mode.clock = 0;
8591
8592 if (!list_empty(&connector->probed_modes)) {
8593 struct drm_display_mode *preferred_mode = NULL;
8594
8595 list_for_each_entry(preferred_mode,
8596 &connector->probed_modes,
8597 head) {
8598 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
8599 amdgpu_encoder->native_mode = *preferred_mode;
8600
8601 break;
8602 }
8603
8604 }
8605 }
8606
8607 static struct drm_display_mode *
amdgpu_dm_create_common_mode(struct drm_encoder * encoder,const char * name,int hdisplay,int vdisplay)8608 amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
8609 const char *name,
8610 int hdisplay, int vdisplay)
8611 {
8612 struct drm_device *dev = encoder->dev;
8613 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8614 struct drm_display_mode *mode = NULL;
8615 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8616
8617 mode = drm_mode_duplicate(dev, native_mode);
8618
8619 if (mode == NULL)
8620 return NULL;
8621
8622 mode->hdisplay = hdisplay;
8623 mode->vdisplay = vdisplay;
8624 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
8625 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
8626
8627 return mode;
8628
8629 }
8630
8631 static const struct amdgpu_dm_mode_size {
8632 char name[DRM_DISPLAY_MODE_LEN];
8633 int w;
8634 int h;
8635 } common_modes[] = {
8636 { "640x480", 640, 480},
8637 { "800x600", 800, 600},
8638 { "1024x768", 1024, 768},
8639 { "1280x720", 1280, 720},
8640 { "1280x800", 1280, 800},
8641 {"1280x1024", 1280, 1024},
8642 { "1440x900", 1440, 900},
8643 {"1680x1050", 1680, 1050},
8644 {"1600x1200", 1600, 1200},
8645 {"1920x1080", 1920, 1080},
8646 {"1920x1200", 1920, 1200}
8647 };
8648
amdgpu_dm_connector_add_common_modes(struct drm_encoder * encoder,struct drm_connector * connector)8649 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
8650 struct drm_connector *connector)
8651 {
8652 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8653 struct drm_display_mode *mode = NULL;
8654 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8655 struct amdgpu_dm_connector *amdgpu_dm_connector =
8656 to_amdgpu_dm_connector(connector);
8657 int i;
8658 int n;
8659
8660 if ((connector->connector_type != DRM_MODE_CONNECTOR_eDP) &&
8661 (connector->connector_type != DRM_MODE_CONNECTOR_LVDS))
8662 return;
8663
8664 n = ARRAY_SIZE(common_modes);
8665
8666 for (i = 0; i < n; i++) {
8667 struct drm_display_mode *curmode = NULL;
8668 bool mode_existed = false;
8669
8670 if (common_modes[i].w > native_mode->hdisplay ||
8671 common_modes[i].h > native_mode->vdisplay ||
8672 (common_modes[i].w == native_mode->hdisplay &&
8673 common_modes[i].h == native_mode->vdisplay))
8674 continue;
8675
8676 list_for_each_entry(curmode, &connector->probed_modes, head) {
8677 if (common_modes[i].w == curmode->hdisplay &&
8678 common_modes[i].h == curmode->vdisplay) {
8679 mode_existed = true;
8680 break;
8681 }
8682 }
8683
8684 if (mode_existed)
8685 continue;
8686
8687 mode = amdgpu_dm_create_common_mode(encoder,
8688 common_modes[i].name, common_modes[i].w,
8689 common_modes[i].h);
8690 if (!mode)
8691 continue;
8692
8693 drm_mode_probed_add(connector, mode);
8694 amdgpu_dm_connector->num_modes++;
8695 }
8696 }
8697
amdgpu_set_panel_orientation(struct drm_connector * connector)8698 static void amdgpu_set_panel_orientation(struct drm_connector *connector)
8699 {
8700 struct drm_encoder *encoder;
8701 struct amdgpu_encoder *amdgpu_encoder;
8702 const struct drm_display_mode *native_mode;
8703
8704 if (connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
8705 connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
8706 return;
8707
8708 mutex_lock(&connector->dev->mode_config.mutex);
8709 amdgpu_dm_connector_get_modes(connector);
8710 mutex_unlock(&connector->dev->mode_config.mutex);
8711
8712 encoder = amdgpu_dm_connector_to_encoder(connector);
8713 if (!encoder)
8714 return;
8715
8716 amdgpu_encoder = to_amdgpu_encoder(encoder);
8717
8718 native_mode = &amdgpu_encoder->native_mode;
8719 if (native_mode->hdisplay == 0 || native_mode->vdisplay == 0)
8720 return;
8721
8722 drm_connector_set_panel_orientation_with_quirk(connector,
8723 DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
8724 native_mode->hdisplay,
8725 native_mode->vdisplay);
8726 }
8727
amdgpu_dm_connector_ddc_get_modes(struct drm_connector * connector,const struct drm_edid * drm_edid)8728 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
8729 const struct drm_edid *drm_edid)
8730 {
8731 struct amdgpu_dm_connector *amdgpu_dm_connector =
8732 to_amdgpu_dm_connector(connector);
8733
8734 if (drm_edid) {
8735 /* empty probed_modes */
8736 INIT_LIST_HEAD(&connector->probed_modes);
8737 amdgpu_dm_connector->num_modes =
8738 drm_edid_connector_add_modes(connector);
8739
8740 /* sorting the probed modes before calling function
8741 * amdgpu_dm_get_native_mode() since EDID can have
8742 * more than one preferred mode. The modes that are
8743 * later in the probed mode list could be of higher
8744 * and preferred resolution. For example, 3840x2160
8745 * resolution in base EDID preferred timing and 4096x2160
8746 * preferred resolution in DID extension block later.
8747 */
8748 drm_mode_sort(&connector->probed_modes);
8749 amdgpu_dm_get_native_mode(connector);
8750
8751 /* Freesync capabilities are reset by calling
8752 * drm_edid_connector_add_modes() and need to be
8753 * restored here.
8754 */
8755 amdgpu_dm_update_freesync_caps(connector, drm_edid);
8756 } else {
8757 amdgpu_dm_connector->num_modes = 0;
8758 }
8759 }
8760
is_duplicate_mode(struct amdgpu_dm_connector * aconnector,struct drm_display_mode * mode)8761 static bool is_duplicate_mode(struct amdgpu_dm_connector *aconnector,
8762 struct drm_display_mode *mode)
8763 {
8764 struct drm_display_mode *m;
8765
8766 list_for_each_entry(m, &aconnector->base.probed_modes, head) {
8767 if (drm_mode_equal(m, mode))
8768 return true;
8769 }
8770
8771 return false;
8772 }
8773
add_fs_modes(struct amdgpu_dm_connector * aconnector)8774 static uint add_fs_modes(struct amdgpu_dm_connector *aconnector)
8775 {
8776 const struct drm_display_mode *m;
8777 struct drm_display_mode *new_mode;
8778 uint i;
8779 u32 new_modes_count = 0;
8780
8781 /* Standard FPS values
8782 *
8783 * 23.976 - TV/NTSC
8784 * 24 - Cinema
8785 * 25 - TV/PAL
8786 * 29.97 - TV/NTSC
8787 * 30 - TV/NTSC
8788 * 48 - Cinema HFR
8789 * 50 - TV/PAL
8790 * 60 - Commonly used
8791 * 48,72,96,120 - Multiples of 24
8792 */
8793 static const u32 common_rates[] = {
8794 23976, 24000, 25000, 29970, 30000,
8795 48000, 50000, 60000, 72000, 96000, 120000
8796 };
8797
8798 /*
8799 * Find mode with highest refresh rate with the same resolution
8800 * as the preferred mode. Some monitors report a preferred mode
8801 * with lower resolution than the highest refresh rate supported.
8802 */
8803
8804 m = get_highest_refresh_rate_mode(aconnector, true);
8805 if (!m)
8806 return 0;
8807
8808 for (i = 0; i < ARRAY_SIZE(common_rates); i++) {
8809 u64 target_vtotal, target_vtotal_diff;
8810 u64 num, den;
8811
8812 if (drm_mode_vrefresh(m) * 1000 < common_rates[i])
8813 continue;
8814
8815 if (common_rates[i] < aconnector->min_vfreq * 1000 ||
8816 common_rates[i] > aconnector->max_vfreq * 1000)
8817 continue;
8818
8819 num = (unsigned long long)m->clock * 1000 * 1000;
8820 den = common_rates[i] * (unsigned long long)m->htotal;
8821 target_vtotal = div_u64(num, den);
8822 target_vtotal_diff = target_vtotal - m->vtotal;
8823
8824 /* Check for illegal modes */
8825 if (m->vsync_start + target_vtotal_diff < m->vdisplay ||
8826 m->vsync_end + target_vtotal_diff < m->vsync_start ||
8827 m->vtotal + target_vtotal_diff < m->vsync_end)
8828 continue;
8829
8830 new_mode = drm_mode_duplicate(aconnector->base.dev, m);
8831 if (!new_mode)
8832 goto out;
8833
8834 new_mode->vtotal += (u16)target_vtotal_diff;
8835 new_mode->vsync_start += (u16)target_vtotal_diff;
8836 new_mode->vsync_end += (u16)target_vtotal_diff;
8837 new_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
8838 new_mode->type |= DRM_MODE_TYPE_DRIVER;
8839
8840 if (!is_duplicate_mode(aconnector, new_mode)) {
8841 drm_mode_probed_add(&aconnector->base, new_mode);
8842 new_modes_count += 1;
8843 } else
8844 drm_mode_destroy(aconnector->base.dev, new_mode);
8845 }
8846 out:
8847 return new_modes_count;
8848 }
8849
amdgpu_dm_connector_add_freesync_modes(struct drm_connector * connector,const struct drm_edid * drm_edid)8850 static void amdgpu_dm_connector_add_freesync_modes(struct drm_connector *connector,
8851 const struct drm_edid *drm_edid)
8852 {
8853 struct amdgpu_dm_connector *amdgpu_dm_connector =
8854 to_amdgpu_dm_connector(connector);
8855
8856 if (!(amdgpu_freesync_vid_mode && drm_edid))
8857 return;
8858
8859 if (!amdgpu_dm_connector->dc_sink || !amdgpu_dm_connector->dc_link)
8860 return;
8861
8862 if (!dc_supports_vrr(amdgpu_dm_connector->dc_sink->ctx->dce_version))
8863 return;
8864
8865 if (dc_connector_supports_analog(amdgpu_dm_connector->dc_link->link_id.id) &&
8866 amdgpu_dm_connector->dc_sink->edid_caps.analog)
8867 return;
8868
8869 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
8870 amdgpu_dm_connector->num_modes +=
8871 add_fs_modes(amdgpu_dm_connector);
8872 }
8873
amdgpu_dm_connector_get_modes(struct drm_connector * connector)8874 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
8875 {
8876 struct amdgpu_dm_connector *amdgpu_dm_connector =
8877 to_amdgpu_dm_connector(connector);
8878 struct dc_link *dc_link = amdgpu_dm_connector->dc_link;
8879 struct drm_encoder *encoder;
8880 const struct drm_edid *drm_edid = amdgpu_dm_connector->drm_edid;
8881 struct dc_link_settings *verified_link_cap = &dc_link->verified_link_cap;
8882 const struct dc *dc = dc_link->dc;
8883
8884 encoder = amdgpu_dm_connector_to_encoder(connector);
8885
8886 if (!drm_edid) {
8887 amdgpu_dm_connector->num_modes =
8888 drm_add_modes_noedid(connector, 640, 480);
8889 if (dc->link_srv->dp_get_encoding_format(verified_link_cap) == DP_128b_132b_ENCODING)
8890 amdgpu_dm_connector->num_modes +=
8891 drm_add_modes_noedid(connector, 1920, 1080);
8892
8893 if (amdgpu_dm_connector->dc_sink &&
8894 amdgpu_dm_connector->dc_sink->edid_caps.analog &&
8895 dc_connector_supports_analog(dc_link->link_id.id)) {
8896 /* Analog monitor connected by DAC load detection.
8897 * Add common modes. It will be up to the user to select one that works.
8898 */
8899 for (int i = 0; i < ARRAY_SIZE(common_modes); i++)
8900 amdgpu_dm_connector->num_modes += drm_add_modes_noedid(
8901 connector, common_modes[i].w, common_modes[i].h);
8902 }
8903 } else {
8904 amdgpu_dm_connector_ddc_get_modes(connector, drm_edid);
8905 if (encoder)
8906 amdgpu_dm_connector_add_common_modes(encoder, connector);
8907 amdgpu_dm_connector_add_freesync_modes(connector, drm_edid);
8908 }
8909 amdgpu_dm_fbc_init(connector);
8910
8911 return amdgpu_dm_connector->num_modes;
8912 }
8913
8914 static const u32 supported_colorspaces =
8915 BIT(DRM_MODE_COLORIMETRY_BT709_YCC) |
8916 BIT(DRM_MODE_COLORIMETRY_OPRGB) |
8917 BIT(DRM_MODE_COLORIMETRY_BT2020_RGB) |
8918 BIT(DRM_MODE_COLORIMETRY_BT2020_YCC);
8919
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)8920 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
8921 struct amdgpu_dm_connector *aconnector,
8922 int connector_type,
8923 struct dc_link *link,
8924 int link_index)
8925 {
8926 struct amdgpu_device *adev = drm_to_adev(dm->ddev);
8927
8928 /*
8929 * Some of the properties below require access to state, like bpc.
8930 * Allocate some default initial connector state with our reset helper.
8931 */
8932 if (aconnector->base.funcs->reset)
8933 aconnector->base.funcs->reset(&aconnector->base);
8934
8935 aconnector->connector_id = link_index;
8936 aconnector->bl_idx = -1;
8937 aconnector->dc_link = link;
8938 aconnector->base.interlace_allowed = false;
8939 aconnector->base.doublescan_allowed = false;
8940 aconnector->base.stereo_allowed = false;
8941 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
8942 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
8943 aconnector->audio_inst = -1;
8944 aconnector->pack_sdp_v1_3 = false;
8945 aconnector->as_type = ADAPTIVE_SYNC_TYPE_NONE;
8946 memset(&aconnector->vsdb_info, 0, sizeof(aconnector->vsdb_info));
8947 mutex_init(&aconnector->hpd_lock);
8948 mutex_init(&aconnector->handle_mst_msg_ready);
8949
8950 /*
8951 * If HDMI HPD debounce delay is set, use the minimum between selected
8952 * value and AMDGPU_DM_MAX_HDMI_HPD_DEBOUNCE_MS
8953 */
8954 if (amdgpu_hdmi_hpd_debounce_delay_ms) {
8955 aconnector->hdmi_hpd_debounce_delay_ms = min(amdgpu_hdmi_hpd_debounce_delay_ms,
8956 AMDGPU_DM_MAX_HDMI_HPD_DEBOUNCE_MS);
8957 INIT_DELAYED_WORK(&aconnector->hdmi_hpd_debounce_work, hdmi_hpd_debounce_work);
8958 aconnector->hdmi_prev_sink = NULL;
8959 } else {
8960 aconnector->hdmi_hpd_debounce_delay_ms = 0;
8961 }
8962
8963 /*
8964 * configure support HPD hot plug connector_>polled default value is 0
8965 * which means HPD hot plug not supported
8966 */
8967 switch (connector_type) {
8968 case DRM_MODE_CONNECTOR_HDMIA:
8969 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
8970 aconnector->base.ycbcr_420_allowed =
8971 link->link_enc->features.hdmi_ycbcr420_supported ? true : false;
8972 break;
8973 case DRM_MODE_CONNECTOR_DisplayPort:
8974 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
8975 link->link_enc = link_enc_cfg_get_link_enc(link);
8976 ASSERT(link->link_enc);
8977 if (link->link_enc)
8978 aconnector->base.ycbcr_420_allowed =
8979 link->link_enc->features.dp_ycbcr420_supported ? true : false;
8980 break;
8981 case DRM_MODE_CONNECTOR_DVID:
8982 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
8983 break;
8984 case DRM_MODE_CONNECTOR_DVII:
8985 case DRM_MODE_CONNECTOR_VGA:
8986 aconnector->base.polled =
8987 DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
8988 break;
8989 default:
8990 break;
8991 }
8992
8993 drm_object_attach_property(&aconnector->base.base,
8994 dm->ddev->mode_config.scaling_mode_property,
8995 DRM_MODE_SCALE_NONE);
8996
8997 if (connector_type == DRM_MODE_CONNECTOR_HDMIA
8998 || (connector_type == DRM_MODE_CONNECTOR_DisplayPort && !aconnector->mst_root))
8999 drm_connector_attach_broadcast_rgb_property(&aconnector->base);
9000
9001 drm_object_attach_property(&aconnector->base.base,
9002 adev->mode_info.underscan_property,
9003 UNDERSCAN_OFF);
9004 drm_object_attach_property(&aconnector->base.base,
9005 adev->mode_info.underscan_hborder_property,
9006 0);
9007 drm_object_attach_property(&aconnector->base.base,
9008 adev->mode_info.underscan_vborder_property,
9009 0);
9010
9011 if (!aconnector->mst_root)
9012 drm_connector_attach_max_bpc_property(&aconnector->base, 8, 16);
9013
9014 aconnector->base.state->max_bpc = 16;
9015 aconnector->base.state->max_requested_bpc = aconnector->base.state->max_bpc;
9016
9017 if (connector_type == DRM_MODE_CONNECTOR_HDMIA) {
9018 /* Content Type is currently only implemented for HDMI. */
9019 drm_connector_attach_content_type_property(&aconnector->base);
9020 }
9021
9022 if (connector_type == DRM_MODE_CONNECTOR_HDMIA) {
9023 if (!drm_mode_create_hdmi_colorspace_property(&aconnector->base, supported_colorspaces))
9024 drm_connector_attach_colorspace_property(&aconnector->base);
9025 } else if ((connector_type == DRM_MODE_CONNECTOR_DisplayPort && !aconnector->mst_root) ||
9026 connector_type == DRM_MODE_CONNECTOR_eDP) {
9027 if (!drm_mode_create_dp_colorspace_property(&aconnector->base, supported_colorspaces))
9028 drm_connector_attach_colorspace_property(&aconnector->base);
9029 }
9030
9031 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
9032 connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
9033 connector_type == DRM_MODE_CONNECTOR_eDP) {
9034 drm_connector_attach_hdr_output_metadata_property(&aconnector->base);
9035
9036 if (!aconnector->mst_root)
9037 drm_connector_attach_vrr_capable_property(&aconnector->base);
9038
9039 if (adev->dm.hdcp_workqueue)
9040 drm_connector_attach_content_protection_property(&aconnector->base, true);
9041 }
9042
9043 if (connector_type == DRM_MODE_CONNECTOR_eDP) {
9044 struct drm_privacy_screen *privacy_screen;
9045
9046 privacy_screen = drm_privacy_screen_get(adev_to_drm(adev)->dev, NULL);
9047 if (!IS_ERR(privacy_screen)) {
9048 drm_connector_attach_privacy_screen_provider(&aconnector->base,
9049 privacy_screen);
9050 } else if (PTR_ERR(privacy_screen) != -ENODEV) {
9051 drm_warn(adev_to_drm(adev), "Error getting privacy-screen\n");
9052 }
9053 }
9054 }
9055
amdgpu_dm_i2c_xfer(struct i2c_adapter * i2c_adap,struct i2c_msg * msgs,int num)9056 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
9057 struct i2c_msg *msgs, int num)
9058 {
9059 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
9060 struct ddc_service *ddc_service = i2c->ddc_service;
9061 struct i2c_command cmd;
9062 int i;
9063 int result = -EIO;
9064
9065 if (!ddc_service->ddc_pin)
9066 return result;
9067
9068 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
9069
9070 if (!cmd.payloads)
9071 return result;
9072
9073 cmd.number_of_payloads = num;
9074 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
9075 cmd.speed = 100;
9076
9077 for (i = 0; i < num; i++) {
9078 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
9079 cmd.payloads[i].address = msgs[i].addr;
9080 cmd.payloads[i].length = msgs[i].len;
9081 cmd.payloads[i].data = msgs[i].buf;
9082 }
9083
9084 if (i2c->oem) {
9085 if (dc_submit_i2c_oem(
9086 ddc_service->ctx->dc,
9087 &cmd))
9088 result = num;
9089 } else {
9090 if (dc_submit_i2c(
9091 ddc_service->ctx->dc,
9092 ddc_service->link->link_index,
9093 &cmd))
9094 result = num;
9095 }
9096
9097 kfree(cmd.payloads);
9098 return result;
9099 }
9100
amdgpu_dm_i2c_func(struct i2c_adapter * adap)9101 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
9102 {
9103 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
9104 }
9105
9106 static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
9107 .master_xfer = amdgpu_dm_i2c_xfer,
9108 .functionality = amdgpu_dm_i2c_func,
9109 };
9110
9111 static struct amdgpu_i2c_adapter *
create_i2c(struct ddc_service * ddc_service,bool oem)9112 create_i2c(struct ddc_service *ddc_service, bool oem)
9113 {
9114 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
9115 struct amdgpu_i2c_adapter *i2c;
9116
9117 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
9118 if (!i2c)
9119 return NULL;
9120 i2c->base.owner = THIS_MODULE;
9121 i2c->base.dev.parent = &adev->pdev->dev;
9122 i2c->base.algo = &amdgpu_dm_i2c_algo;
9123 if (oem)
9124 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c OEM bus");
9125 else
9126 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d",
9127 ddc_service->link->link_index);
9128 i2c_set_adapdata(&i2c->base, i2c);
9129 i2c->ddc_service = ddc_service;
9130 i2c->oem = oem;
9131
9132 return i2c;
9133 }
9134
amdgpu_dm_initialize_hdmi_connector(struct amdgpu_dm_connector * aconnector)9135 int amdgpu_dm_initialize_hdmi_connector(struct amdgpu_dm_connector *aconnector)
9136 {
9137 struct cec_connector_info conn_info;
9138 struct drm_device *ddev = aconnector->base.dev;
9139 struct device *hdmi_dev = ddev->dev;
9140
9141 if (amdgpu_dc_debug_mask & DC_DISABLE_HDMI_CEC) {
9142 drm_info(ddev, "HDMI-CEC feature masked\n");
9143 return -EINVAL;
9144 }
9145
9146 cec_fill_conn_info_from_drm(&conn_info, &aconnector->base);
9147 aconnector->notifier =
9148 cec_notifier_conn_register(hdmi_dev, NULL, &conn_info);
9149 if (!aconnector->notifier) {
9150 drm_err(ddev, "Failed to create cec notifier\n");
9151 return -ENOMEM;
9152 }
9153
9154 return 0;
9155 }
9156
9157 /*
9158 * Note: this function assumes that dc_link_detect() was called for the
9159 * dc_link which will be represented by this aconnector.
9160 */
amdgpu_dm_connector_init(struct amdgpu_display_manager * dm,struct amdgpu_dm_connector * aconnector,u32 link_index,struct amdgpu_encoder * aencoder)9161 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
9162 struct amdgpu_dm_connector *aconnector,
9163 u32 link_index,
9164 struct amdgpu_encoder *aencoder)
9165 {
9166 int res = 0;
9167 int connector_type;
9168 struct dc *dc = dm->dc;
9169 struct dc_link *link = dc_get_link_at_index(dc, link_index);
9170 struct amdgpu_i2c_adapter *i2c;
9171
9172 /* Not needed for writeback connector */
9173 link->priv = aconnector;
9174
9175
9176 i2c = create_i2c(link->ddc, false);
9177 if (!i2c) {
9178 drm_err(adev_to_drm(dm->adev), "Failed to create i2c adapter data\n");
9179 return -ENOMEM;
9180 }
9181
9182 aconnector->i2c = i2c;
9183 res = devm_i2c_add_adapter(dm->adev->dev, &i2c->base);
9184
9185 if (res) {
9186 drm_err(adev_to_drm(dm->adev), "Failed to register hw i2c %d\n", link->link_index);
9187 goto out_free;
9188 }
9189
9190 connector_type = to_drm_connector_type(link->connector_signal, link->link_id.id);
9191
9192 res = drm_connector_init_with_ddc(
9193 dm->ddev,
9194 &aconnector->base,
9195 &amdgpu_dm_connector_funcs,
9196 connector_type,
9197 &i2c->base);
9198
9199 if (res) {
9200 drm_err(adev_to_drm(dm->adev), "connector_init failed\n");
9201 aconnector->connector_id = -1;
9202 goto out_free;
9203 }
9204
9205 drm_connector_helper_add(
9206 &aconnector->base,
9207 &amdgpu_dm_connector_helper_funcs);
9208
9209 amdgpu_dm_connector_init_helper(
9210 dm,
9211 aconnector,
9212 connector_type,
9213 link,
9214 link_index);
9215
9216 drm_connector_attach_encoder(
9217 &aconnector->base, &aencoder->base);
9218
9219 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
9220 connector_type == DRM_MODE_CONNECTOR_HDMIB)
9221 amdgpu_dm_initialize_hdmi_connector(aconnector);
9222
9223 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
9224 || connector_type == DRM_MODE_CONNECTOR_eDP)
9225 amdgpu_dm_initialize_dp_connector(dm, aconnector, link->link_index);
9226
9227 out_free:
9228 if (res) {
9229 kfree(i2c);
9230 aconnector->i2c = NULL;
9231 }
9232 return res;
9233 }
9234
amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device * adev)9235 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
9236 {
9237 switch (adev->mode_info.num_crtc) {
9238 case 1:
9239 return 0x1;
9240 case 2:
9241 return 0x3;
9242 case 3:
9243 return 0x7;
9244 case 4:
9245 return 0xf;
9246 case 5:
9247 return 0x1f;
9248 case 6:
9249 default:
9250 return 0x3f;
9251 }
9252 }
9253
amdgpu_dm_encoder_init(struct drm_device * dev,struct amdgpu_encoder * aencoder,uint32_t link_index)9254 static int amdgpu_dm_encoder_init(struct drm_device *dev,
9255 struct amdgpu_encoder *aencoder,
9256 uint32_t link_index)
9257 {
9258 struct amdgpu_device *adev = drm_to_adev(dev);
9259
9260 int res = drm_encoder_init(dev,
9261 &aencoder->base,
9262 &amdgpu_dm_encoder_funcs,
9263 DRM_MODE_ENCODER_TMDS,
9264 NULL);
9265
9266 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
9267
9268 if (!res)
9269 aencoder->encoder_id = link_index;
9270 else
9271 aencoder->encoder_id = -1;
9272
9273 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
9274
9275 return res;
9276 }
9277
manage_dm_interrupts(struct amdgpu_device * adev,struct amdgpu_crtc * acrtc,struct dm_crtc_state * acrtc_state)9278 static void manage_dm_interrupts(struct amdgpu_device *adev,
9279 struct amdgpu_crtc *acrtc,
9280 struct dm_crtc_state *acrtc_state)
9281 { /*
9282 * We cannot be sure that the frontend index maps to the same
9283 * backend index - some even map to more than one.
9284 * So we have to go through the CRTC to find the right IRQ.
9285 */
9286 int irq_type = amdgpu_display_crtc_idx_to_irq_type(
9287 adev,
9288 acrtc->crtc_id);
9289 struct drm_device *dev = adev_to_drm(adev);
9290
9291 struct drm_vblank_crtc_config config = {0};
9292 struct dc_crtc_timing *timing;
9293 int offdelay;
9294
9295 if (acrtc_state) {
9296 timing = &acrtc_state->stream->timing;
9297
9298 /*
9299 * Depending on when the HW latching event of double-buffered
9300 * registers happen relative to the PSR SDP deadline, and how
9301 * bad the Panel clock has drifted since the last ALPM off
9302 * event, there can be up to 3 frames of delay between sending
9303 * the PSR exit cmd to DMUB fw, and when the panel starts
9304 * displaying live frames.
9305 *
9306 * We can set:
9307 *
9308 * 20/100 * offdelay_ms = 3_frames_ms
9309 * => offdelay_ms = 5 * 3_frames_ms
9310 *
9311 * This ensures that `3_frames_ms` will only be experienced as a
9312 * 20% delay on top how long the display has been static, and
9313 * thus make the delay less perceivable.
9314 */
9315 if (acrtc_state->stream->link->psr_settings.psr_version <
9316 DC_PSR_VERSION_UNSUPPORTED) {
9317 offdelay = DIV64_U64_ROUND_UP((u64)5 * 3 * 10 *
9318 timing->v_total *
9319 timing->h_total,
9320 timing->pix_clk_100hz);
9321 config.offdelay_ms = offdelay ?: 30;
9322 } else if (amdgpu_ip_version(adev, DCE_HWIP, 0) <
9323 IP_VERSION(3, 5, 0) ||
9324 !(adev->flags & AMD_IS_APU)) {
9325 /*
9326 * Older HW and DGPU have issues with instant off;
9327 * use a 2 frame offdelay.
9328 */
9329 offdelay = DIV64_U64_ROUND_UP((u64)20 *
9330 timing->v_total *
9331 timing->h_total,
9332 timing->pix_clk_100hz);
9333
9334 config.offdelay_ms = offdelay ?: 30;
9335 } else {
9336 /* offdelay_ms = 0 will never disable vblank */
9337 config.offdelay_ms = 1;
9338 config.disable_immediate = true;
9339 }
9340
9341 drm_crtc_vblank_on_config(&acrtc->base,
9342 &config);
9343 /* Allow RX6xxx, RX7700, RX7800 GPUs to call amdgpu_irq_get.*/
9344 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
9345 case IP_VERSION(3, 0, 0):
9346 case IP_VERSION(3, 0, 2):
9347 case IP_VERSION(3, 0, 3):
9348 case IP_VERSION(3, 2, 0):
9349 if (amdgpu_irq_get(adev, &adev->pageflip_irq, irq_type))
9350 drm_err(dev, "DM_IRQ: Cannot get pageflip irq!\n");
9351 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
9352 if (amdgpu_irq_get(adev, &adev->vline0_irq, irq_type))
9353 drm_err(dev, "DM_IRQ: Cannot get vline0 irq!\n");
9354 #endif
9355 }
9356
9357 } else {
9358 /* Allow RX6xxx, RX7700, RX7800 GPUs to call amdgpu_irq_put.*/
9359 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
9360 case IP_VERSION(3, 0, 0):
9361 case IP_VERSION(3, 0, 2):
9362 case IP_VERSION(3, 0, 3):
9363 case IP_VERSION(3, 2, 0):
9364 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
9365 if (amdgpu_irq_put(adev, &adev->vline0_irq, irq_type))
9366 drm_err(dev, "DM_IRQ: Cannot put vline0 irq!\n");
9367 #endif
9368 if (amdgpu_irq_put(adev, &adev->pageflip_irq, irq_type))
9369 drm_err(dev, "DM_IRQ: Cannot put pageflip irq!\n");
9370 }
9371
9372 drm_crtc_vblank_off(&acrtc->base);
9373 }
9374 }
9375
dm_update_pflip_irq_state(struct amdgpu_device * adev,struct amdgpu_crtc * acrtc)9376 static void dm_update_pflip_irq_state(struct amdgpu_device *adev,
9377 struct amdgpu_crtc *acrtc)
9378 {
9379 int irq_type =
9380 amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
9381
9382 /**
9383 * This reads the current state for the IRQ and force reapplies
9384 * the setting to hardware.
9385 */
9386 amdgpu_irq_update(adev, &adev->pageflip_irq, irq_type);
9387 }
9388
9389 static bool
is_scaling_state_different(const struct dm_connector_state * dm_state,const struct dm_connector_state * old_dm_state)9390 is_scaling_state_different(const struct dm_connector_state *dm_state,
9391 const struct dm_connector_state *old_dm_state)
9392 {
9393 if (dm_state->scaling != old_dm_state->scaling)
9394 return true;
9395 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
9396 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
9397 return true;
9398 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
9399 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
9400 return true;
9401 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
9402 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
9403 return true;
9404 return false;
9405 }
9406
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)9407 static bool is_content_protection_different(struct drm_crtc_state *new_crtc_state,
9408 struct drm_crtc_state *old_crtc_state,
9409 struct drm_connector_state *new_conn_state,
9410 struct drm_connector_state *old_conn_state,
9411 const struct drm_connector *connector,
9412 struct hdcp_workqueue *hdcp_w)
9413 {
9414 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
9415 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
9416
9417 pr_debug("[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n",
9418 connector->index, connector->status, connector->dpms);
9419 pr_debug("[HDCP_DM] state protection old: %x new: %x\n",
9420 old_conn_state->content_protection, new_conn_state->content_protection);
9421
9422 if (old_crtc_state)
9423 pr_debug("[HDCP_DM] old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
9424 old_crtc_state->enable,
9425 old_crtc_state->active,
9426 old_crtc_state->mode_changed,
9427 old_crtc_state->active_changed,
9428 old_crtc_state->connectors_changed);
9429
9430 if (new_crtc_state)
9431 pr_debug("[HDCP_DM] NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
9432 new_crtc_state->enable,
9433 new_crtc_state->active,
9434 new_crtc_state->mode_changed,
9435 new_crtc_state->active_changed,
9436 new_crtc_state->connectors_changed);
9437
9438 /* hdcp content type change */
9439 if (old_conn_state->hdcp_content_type != new_conn_state->hdcp_content_type &&
9440 new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
9441 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
9442 pr_debug("[HDCP_DM] Type0/1 change %s :true\n", __func__);
9443 return true;
9444 }
9445
9446 /* CP is being re enabled, ignore this */
9447 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED &&
9448 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
9449 if (new_crtc_state && new_crtc_state->mode_changed) {
9450 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
9451 pr_debug("[HDCP_DM] ENABLED->DESIRED & mode_changed %s :true\n", __func__);
9452 return true;
9453 }
9454 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED;
9455 pr_debug("[HDCP_DM] ENABLED -> DESIRED %s :false\n", __func__);
9456 return false;
9457 }
9458
9459 /* S3 resume case, since old state will always be 0 (UNDESIRED) and the restored state will be ENABLED
9460 *
9461 * Handles: UNDESIRED -> ENABLED
9462 */
9463 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED &&
9464 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
9465 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
9466
9467 /* Stream removed and re-enabled
9468 *
9469 * Can sometimes overlap with the HPD case,
9470 * thus set update_hdcp to false to avoid
9471 * setting HDCP multiple times.
9472 *
9473 * Handles: DESIRED -> DESIRED (Special case)
9474 */
9475 if (!(old_conn_state->crtc && old_conn_state->crtc->enabled) &&
9476 new_conn_state->crtc && new_conn_state->crtc->enabled &&
9477 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
9478 dm_con_state->update_hdcp = false;
9479 pr_debug("[HDCP_DM] DESIRED->DESIRED (Stream removed and re-enabled) %s :true\n",
9480 __func__);
9481 return true;
9482 }
9483
9484 /* Hot-plug, headless s3, dpms
9485 *
9486 * Only start HDCP if the display is connected/enabled.
9487 * update_hdcp flag will be set to false until the next
9488 * HPD comes in.
9489 *
9490 * Handles: DESIRED -> DESIRED (Special case)
9491 */
9492 if (dm_con_state->update_hdcp &&
9493 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED &&
9494 connector->dpms == DRM_MODE_DPMS_ON && aconnector->dc_sink != NULL) {
9495 dm_con_state->update_hdcp = false;
9496 pr_debug("[HDCP_DM] DESIRED->DESIRED (Hot-plug, headless s3, dpms) %s :true\n",
9497 __func__);
9498 return true;
9499 }
9500
9501 if (old_conn_state->content_protection == new_conn_state->content_protection) {
9502 if (new_conn_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED) {
9503 if (new_crtc_state && new_crtc_state->mode_changed) {
9504 pr_debug("[HDCP_DM] DESIRED->DESIRED or ENABLE->ENABLE mode_change %s :true\n",
9505 __func__);
9506 return true;
9507 }
9508 pr_debug("[HDCP_DM] DESIRED->DESIRED & ENABLE->ENABLE %s :false\n",
9509 __func__);
9510 return false;
9511 }
9512
9513 pr_debug("[HDCP_DM] UNDESIRED->UNDESIRED %s :false\n", __func__);
9514 return false;
9515 }
9516
9517 if (new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_ENABLED) {
9518 pr_debug("[HDCP_DM] UNDESIRED->DESIRED or DESIRED->UNDESIRED or ENABLED->UNDESIRED %s :true\n",
9519 __func__);
9520 return true;
9521 }
9522
9523 pr_debug("[HDCP_DM] DESIRED->ENABLED %s :false\n", __func__);
9524 return false;
9525 }
9526
remove_stream(struct amdgpu_device * adev,struct amdgpu_crtc * acrtc,struct dc_stream_state * stream)9527 static void remove_stream(struct amdgpu_device *adev,
9528 struct amdgpu_crtc *acrtc,
9529 struct dc_stream_state *stream)
9530 {
9531 /* this is the update mode case */
9532
9533 acrtc->otg_inst = -1;
9534 acrtc->enabled = false;
9535 }
9536
prepare_flip_isr(struct amdgpu_crtc * acrtc)9537 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
9538 {
9539
9540 assert_spin_locked(&acrtc->base.dev->event_lock);
9541 WARN_ON(acrtc->event);
9542
9543 acrtc->event = acrtc->base.state->event;
9544
9545 /* Set the flip status */
9546 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
9547
9548 /* Mark this event as consumed */
9549 acrtc->base.state->event = NULL;
9550
9551 drm_dbg_state(acrtc->base.dev,
9552 "crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
9553 acrtc->crtc_id);
9554 }
9555
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)9556 static void update_freesync_state_on_stream(
9557 struct amdgpu_display_manager *dm,
9558 struct dm_crtc_state *new_crtc_state,
9559 struct dc_stream_state *new_stream,
9560 struct dc_plane_state *surface,
9561 u32 flip_timestamp_in_us)
9562 {
9563 struct mod_vrr_params vrr_params;
9564 struct dc_info_packet vrr_infopacket = {0};
9565 struct amdgpu_device *adev = dm->adev;
9566 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
9567 unsigned long flags;
9568 bool pack_sdp_v1_3 = false;
9569 struct amdgpu_dm_connector *aconn;
9570 enum vrr_packet_type packet_type = PACKET_TYPE_VRR;
9571
9572 if (!new_stream)
9573 return;
9574
9575 /*
9576 * TODO: Determine why min/max totals and vrefresh can be 0 here.
9577 * For now it's sufficient to just guard against these conditions.
9578 */
9579
9580 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
9581 return;
9582
9583 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9584 vrr_params = acrtc->dm_irq_params.vrr_params;
9585
9586 if (surface) {
9587 mod_freesync_handle_preflip(
9588 dm->freesync_module,
9589 surface,
9590 new_stream,
9591 flip_timestamp_in_us,
9592 &vrr_params);
9593
9594 if (adev->family < AMDGPU_FAMILY_AI &&
9595 amdgpu_dm_crtc_vrr_active(new_crtc_state)) {
9596 mod_freesync_handle_v_update(dm->freesync_module,
9597 new_stream, &vrr_params);
9598
9599 /* Need to call this before the frame ends. */
9600 dc_stream_adjust_vmin_vmax(dm->dc,
9601 new_crtc_state->stream,
9602 &vrr_params.adjust);
9603 }
9604 }
9605
9606 aconn = (struct amdgpu_dm_connector *)new_stream->dm_stream_context;
9607
9608 if (aconn && (aconn->as_type == FREESYNC_TYPE_PCON_IN_WHITELIST || aconn->vsdb_info.replay_mode)) {
9609 pack_sdp_v1_3 = aconn->pack_sdp_v1_3;
9610
9611 if (aconn->vsdb_info.amd_vsdb_version == 1)
9612 packet_type = PACKET_TYPE_FS_V1;
9613 else if (aconn->vsdb_info.amd_vsdb_version == 2)
9614 packet_type = PACKET_TYPE_FS_V2;
9615 else if (aconn->vsdb_info.amd_vsdb_version == 3)
9616 packet_type = PACKET_TYPE_FS_V3;
9617
9618 mod_build_adaptive_sync_infopacket(new_stream, aconn->as_type, NULL,
9619 &new_stream->adaptive_sync_infopacket);
9620 }
9621
9622 mod_freesync_build_vrr_infopacket(
9623 dm->freesync_module,
9624 new_stream,
9625 &vrr_params,
9626 packet_type,
9627 TRANSFER_FUNC_UNKNOWN,
9628 &vrr_infopacket,
9629 pack_sdp_v1_3);
9630
9631 new_crtc_state->freesync_vrr_info_changed |=
9632 (memcmp(&new_crtc_state->vrr_infopacket,
9633 &vrr_infopacket,
9634 sizeof(vrr_infopacket)) != 0);
9635
9636 acrtc->dm_irq_params.vrr_params = vrr_params;
9637 new_crtc_state->vrr_infopacket = vrr_infopacket;
9638
9639 new_stream->vrr_infopacket = vrr_infopacket;
9640 new_stream->allow_freesync = mod_freesync_get_freesync_enabled(&vrr_params);
9641
9642 if (new_crtc_state->freesync_vrr_info_changed)
9643 DRM_DEBUG_KMS("VRR packet update: crtc=%u enabled=%d state=%d",
9644 new_crtc_state->base.crtc->base.id,
9645 (int)new_crtc_state->base.vrr_enabled,
9646 (int)vrr_params.state);
9647
9648 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9649 }
9650
update_stream_irq_parameters(struct amdgpu_display_manager * dm,struct dm_crtc_state * new_crtc_state)9651 static void update_stream_irq_parameters(
9652 struct amdgpu_display_manager *dm,
9653 struct dm_crtc_state *new_crtc_state)
9654 {
9655 struct dc_stream_state *new_stream = new_crtc_state->stream;
9656 struct mod_vrr_params vrr_params;
9657 struct mod_freesync_config config = new_crtc_state->freesync_config;
9658 struct amdgpu_device *adev = dm->adev;
9659 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
9660 unsigned long flags;
9661
9662 if (!new_stream)
9663 return;
9664
9665 /*
9666 * TODO: Determine why min/max totals and vrefresh can be 0 here.
9667 * For now it's sufficient to just guard against these conditions.
9668 */
9669 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
9670 return;
9671
9672 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9673 vrr_params = acrtc->dm_irq_params.vrr_params;
9674
9675 if (new_crtc_state->vrr_supported &&
9676 config.min_refresh_in_uhz &&
9677 config.max_refresh_in_uhz) {
9678 /*
9679 * if freesync compatible mode was set, config.state will be set
9680 * in atomic check
9681 */
9682 if (config.state == VRR_STATE_ACTIVE_FIXED && config.fixed_refresh_in_uhz &&
9683 (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) ||
9684 new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)) {
9685 vrr_params.max_refresh_in_uhz = config.max_refresh_in_uhz;
9686 vrr_params.min_refresh_in_uhz = config.min_refresh_in_uhz;
9687 vrr_params.fixed_refresh_in_uhz = config.fixed_refresh_in_uhz;
9688 vrr_params.state = VRR_STATE_ACTIVE_FIXED;
9689 } else {
9690 config.state = new_crtc_state->base.vrr_enabled ?
9691 VRR_STATE_ACTIVE_VARIABLE :
9692 VRR_STATE_INACTIVE;
9693 }
9694 } else {
9695 config.state = VRR_STATE_UNSUPPORTED;
9696 }
9697
9698 mod_freesync_build_vrr_params(dm->freesync_module,
9699 new_stream,
9700 &config, &vrr_params);
9701
9702 new_crtc_state->freesync_config = config;
9703 /* Copy state for access from DM IRQ handler */
9704 acrtc->dm_irq_params.freesync_config = config;
9705 acrtc->dm_irq_params.active_planes = new_crtc_state->active_planes;
9706 acrtc->dm_irq_params.vrr_params = vrr_params;
9707 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9708 }
9709
amdgpu_dm_handle_vrr_transition(struct dm_crtc_state * old_state,struct dm_crtc_state * new_state)9710 static void amdgpu_dm_handle_vrr_transition(struct dm_crtc_state *old_state,
9711 struct dm_crtc_state *new_state)
9712 {
9713 bool old_vrr_active = amdgpu_dm_crtc_vrr_active(old_state);
9714 bool new_vrr_active = amdgpu_dm_crtc_vrr_active(new_state);
9715
9716 if (!old_vrr_active && new_vrr_active) {
9717 /* Transition VRR inactive -> active:
9718 * While VRR is active, we must not disable vblank irq, as a
9719 * reenable after disable would compute bogus vblank/pflip
9720 * timestamps if it likely happened inside display front-porch.
9721 *
9722 * We also need vupdate irq for the actual core vblank handling
9723 * at end of vblank.
9724 */
9725 WARN_ON(amdgpu_dm_crtc_set_vupdate_irq(new_state->base.crtc, true) != 0);
9726 WARN_ON(drm_crtc_vblank_get(new_state->base.crtc) != 0);
9727 drm_dbg_driver(new_state->base.crtc->dev, "%s: crtc=%u VRR off->on: Get vblank ref\n",
9728 __func__, new_state->base.crtc->base.id);
9729 } else if (old_vrr_active && !new_vrr_active) {
9730 /* Transition VRR active -> inactive:
9731 * Allow vblank irq disable again for fixed refresh rate.
9732 */
9733 WARN_ON(amdgpu_dm_crtc_set_vupdate_irq(new_state->base.crtc, false) != 0);
9734 drm_crtc_vblank_put(new_state->base.crtc);
9735 drm_dbg_driver(new_state->base.crtc->dev, "%s: crtc=%u VRR on->off: Drop vblank ref\n",
9736 __func__, new_state->base.crtc->base.id);
9737 }
9738 }
9739
amdgpu_dm_commit_cursors(struct drm_atomic_state * state)9740 static void amdgpu_dm_commit_cursors(struct drm_atomic_state *state)
9741 {
9742 struct drm_plane *plane;
9743 struct drm_plane_state *old_plane_state;
9744 int i;
9745
9746 /*
9747 * TODO: Make this per-stream so we don't issue redundant updates for
9748 * commits with multiple streams.
9749 */
9750 for_each_old_plane_in_state(state, plane, old_plane_state, i)
9751 if (plane->type == DRM_PLANE_TYPE_CURSOR)
9752 amdgpu_dm_plane_handle_cursor_update(plane, old_plane_state);
9753 }
9754
get_mem_type(struct drm_framebuffer * fb)9755 static inline uint32_t get_mem_type(struct drm_framebuffer *fb)
9756 {
9757 struct amdgpu_bo *abo = gem_to_amdgpu_bo(fb->obj[0]);
9758
9759 return abo->tbo.resource ? abo->tbo.resource->mem_type : 0;
9760 }
9761
amdgpu_dm_update_cursor(struct drm_plane * plane,struct drm_plane_state * old_plane_state,struct dc_stream_update * update)9762 static void amdgpu_dm_update_cursor(struct drm_plane *plane,
9763 struct drm_plane_state *old_plane_state,
9764 struct dc_stream_update *update)
9765 {
9766 struct amdgpu_device *adev = drm_to_adev(plane->dev);
9767 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
9768 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
9769 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
9770 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
9771 uint64_t address = afb ? afb->address : 0;
9772 struct dc_cursor_position position = {0};
9773 struct dc_cursor_attributes attributes;
9774 int ret;
9775
9776 if (!plane->state->fb && !old_plane_state->fb)
9777 return;
9778
9779 drm_dbg_atomic(plane->dev, "crtc_id=%d with size %d to %d\n",
9780 amdgpu_crtc->crtc_id, plane->state->crtc_w,
9781 plane->state->crtc_h);
9782
9783 ret = amdgpu_dm_plane_get_cursor_position(plane, crtc, &position);
9784 if (ret)
9785 return;
9786
9787 if (!position.enable) {
9788 /* turn off cursor */
9789 if (crtc_state && crtc_state->stream) {
9790 dc_stream_set_cursor_position(crtc_state->stream,
9791 &position);
9792 update->cursor_position = &crtc_state->stream->cursor_position;
9793 }
9794 return;
9795 }
9796
9797 amdgpu_crtc->cursor_width = plane->state->crtc_w;
9798 amdgpu_crtc->cursor_height = plane->state->crtc_h;
9799
9800 memset(&attributes, 0, sizeof(attributes));
9801 attributes.address.high_part = upper_32_bits(address);
9802 attributes.address.low_part = lower_32_bits(address);
9803 attributes.width = plane->state->crtc_w;
9804 attributes.height = plane->state->crtc_h;
9805 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
9806 attributes.rotation_angle = 0;
9807 attributes.attribute_flags.value = 0;
9808
9809 /* Enable cursor degamma ROM on DCN3+ for implicit sRGB degamma in DRM
9810 * legacy gamma setup.
9811 */
9812 if (crtc_state->cm_is_degamma_srgb &&
9813 adev->dm.dc->caps.color.dpp.gamma_corr)
9814 attributes.attribute_flags.bits.ENABLE_CURSOR_DEGAMMA = 1;
9815
9816 if (afb)
9817 attributes.pitch = afb->base.pitches[0] / afb->base.format->cpp[0];
9818
9819 if (crtc_state->stream) {
9820 if (!dc_stream_set_cursor_attributes(crtc_state->stream,
9821 &attributes))
9822 drm_err(adev_to_drm(adev), "DC failed to set cursor attributes\n");
9823
9824 update->cursor_attributes = &crtc_state->stream->cursor_attributes;
9825
9826 if (!dc_stream_set_cursor_position(crtc_state->stream,
9827 &position))
9828 drm_err(adev_to_drm(adev), "DC failed to set cursor position\n");
9829
9830 update->cursor_position = &crtc_state->stream->cursor_position;
9831 }
9832 }
9833
amdgpu_dm_enable_self_refresh(struct amdgpu_crtc * acrtc_attach,const struct dm_crtc_state * acrtc_state,const u64 current_ts)9834 static void amdgpu_dm_enable_self_refresh(struct amdgpu_crtc *acrtc_attach,
9835 const struct dm_crtc_state *acrtc_state,
9836 const u64 current_ts)
9837 {
9838 struct psr_settings *psr = &acrtc_state->stream->link->psr_settings;
9839 struct replay_settings *pr = &acrtc_state->stream->link->replay_settings;
9840 struct amdgpu_dm_connector *aconn =
9841 (struct amdgpu_dm_connector *)acrtc_state->stream->dm_stream_context;
9842 bool vrr_active = amdgpu_dm_crtc_vrr_active(acrtc_state);
9843
9844 if (acrtc_state->update_type > UPDATE_TYPE_FAST) {
9845 if (pr->config.replay_supported && !pr->replay_feature_enabled)
9846 amdgpu_dm_link_setup_replay(acrtc_state->stream->link, aconn);
9847 else if (psr->psr_version != DC_PSR_VERSION_UNSUPPORTED &&
9848 !psr->psr_feature_enabled)
9849 if (!aconn->disallow_edp_enter_psr)
9850 amdgpu_dm_link_setup_psr(acrtc_state->stream);
9851 }
9852
9853 /* Decrement skip count when SR is enabled and we're doing fast updates. */
9854 if (acrtc_state->update_type == UPDATE_TYPE_FAST &&
9855 (psr->psr_feature_enabled || pr->config.replay_supported)) {
9856 if (aconn->sr_skip_count > 0)
9857 aconn->sr_skip_count--;
9858
9859 /* Allow SR when skip count is 0. */
9860 acrtc_attach->dm_irq_params.allow_sr_entry = !aconn->sr_skip_count;
9861
9862 /*
9863 * If sink supports PSR SU/Panel Replay, there is no need to rely on
9864 * a vblank event disable request to enable PSR/RP. PSR SU/RP
9865 * can be enabled immediately once OS demonstrates an
9866 * adequate number of fast atomic commits to notify KMD
9867 * of update events. See `vblank_control_worker()`.
9868 */
9869 if (!vrr_active &&
9870 acrtc_attach->dm_irq_params.allow_sr_entry &&
9871 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
9872 !amdgpu_dm_crc_window_is_activated(acrtc_state->base.crtc) &&
9873 #endif
9874 (current_ts - psr->psr_dirty_rects_change_timestamp_ns) > 500000000) {
9875 if (pr->replay_feature_enabled && !pr->replay_allow_active)
9876 amdgpu_dm_replay_enable(acrtc_state->stream, true);
9877 if (psr->psr_version == DC_PSR_VERSION_SU_1 &&
9878 !psr->psr_allow_active && !aconn->disallow_edp_enter_psr)
9879 amdgpu_dm_psr_enable(acrtc_state->stream);
9880 }
9881 } else {
9882 acrtc_attach->dm_irq_params.allow_sr_entry = false;
9883 }
9884 }
9885
amdgpu_dm_commit_planes(struct drm_atomic_state * state,struct drm_device * dev,struct amdgpu_display_manager * dm,struct drm_crtc * pcrtc,bool wait_for_vblank)9886 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
9887 struct drm_device *dev,
9888 struct amdgpu_display_manager *dm,
9889 struct drm_crtc *pcrtc,
9890 bool wait_for_vblank)
9891 {
9892 u32 i;
9893 u64 timestamp_ns = ktime_get_ns();
9894 struct drm_plane *plane;
9895 struct drm_plane_state *old_plane_state, *new_plane_state;
9896 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
9897 struct drm_crtc_state *new_pcrtc_state =
9898 drm_atomic_get_new_crtc_state(state, pcrtc);
9899 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
9900 struct dm_crtc_state *dm_old_crtc_state =
9901 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
9902 int planes_count = 0, vpos, hpos;
9903 unsigned long flags;
9904 u32 target_vblank, last_flip_vblank;
9905 bool vrr_active = amdgpu_dm_crtc_vrr_active(acrtc_state);
9906 bool cursor_update = false;
9907 bool pflip_present = false;
9908 bool dirty_rects_changed = false;
9909 bool updated_planes_and_streams = false;
9910 struct {
9911 struct dc_surface_update surface_updates[MAX_SURFACES];
9912 struct dc_plane_info plane_infos[MAX_SURFACES];
9913 struct dc_scaling_info scaling_infos[MAX_SURFACES];
9914 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
9915 struct dc_stream_update stream_update;
9916 } *bundle;
9917
9918 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
9919
9920 if (!bundle) {
9921 drm_err(dev, "Failed to allocate update bundle\n");
9922 goto cleanup;
9923 }
9924
9925 /*
9926 * Disable the cursor first if we're disabling all the planes.
9927 * It'll remain on the screen after the planes are re-enabled
9928 * if we don't.
9929 *
9930 * If the cursor is transitioning from native to overlay mode, the
9931 * native cursor needs to be disabled first.
9932 */
9933 if (acrtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE &&
9934 dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE) {
9935 struct dc_cursor_position cursor_position = {0};
9936
9937 if (!dc_stream_set_cursor_position(acrtc_state->stream,
9938 &cursor_position))
9939 drm_err(dev, "DC failed to disable native cursor\n");
9940
9941 bundle->stream_update.cursor_position =
9942 &acrtc_state->stream->cursor_position;
9943 }
9944
9945 if (acrtc_state->active_planes == 0 &&
9946 dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE)
9947 amdgpu_dm_commit_cursors(state);
9948
9949 /* update planes when needed */
9950 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
9951 struct drm_crtc *crtc = new_plane_state->crtc;
9952 struct drm_crtc_state *new_crtc_state;
9953 struct drm_framebuffer *fb = new_plane_state->fb;
9954 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)fb;
9955 bool plane_needs_flip;
9956 struct dc_plane_state *dc_plane;
9957 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
9958
9959 /* Cursor plane is handled after stream updates */
9960 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
9961 acrtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE) {
9962 if ((fb && crtc == pcrtc) ||
9963 (old_plane_state->fb && old_plane_state->crtc == pcrtc)) {
9964 cursor_update = true;
9965 if (amdgpu_ip_version(dm->adev, DCE_HWIP, 0) != 0)
9966 amdgpu_dm_update_cursor(plane, old_plane_state, &bundle->stream_update);
9967 }
9968
9969 continue;
9970 }
9971
9972 if (!fb || !crtc || pcrtc != crtc)
9973 continue;
9974
9975 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
9976 if (!new_crtc_state->active)
9977 continue;
9978
9979 dc_plane = dm_new_plane_state->dc_state;
9980 if (!dc_plane)
9981 continue;
9982
9983 bundle->surface_updates[planes_count].surface = dc_plane;
9984 if (new_pcrtc_state->color_mgmt_changed) {
9985 bundle->surface_updates[planes_count].gamma = &dc_plane->gamma_correction;
9986 bundle->surface_updates[planes_count].in_transfer_func = &dc_plane->in_transfer_func;
9987 bundle->surface_updates[planes_count].gamut_remap_matrix = &dc_plane->gamut_remap_matrix;
9988 bundle->surface_updates[planes_count].hdr_mult = dc_plane->hdr_mult;
9989 bundle->surface_updates[planes_count].func_shaper = &dc_plane->in_shaper_func;
9990 bundle->surface_updates[planes_count].lut3d_func = &dc_plane->lut3d_func;
9991 bundle->surface_updates[planes_count].blend_tf = &dc_plane->blend_tf;
9992 }
9993
9994 amdgpu_dm_plane_fill_dc_scaling_info(dm->adev, new_plane_state,
9995 &bundle->scaling_infos[planes_count]);
9996
9997 bundle->surface_updates[planes_count].scaling_info =
9998 &bundle->scaling_infos[planes_count];
9999
10000 plane_needs_flip = old_plane_state->fb && new_plane_state->fb;
10001
10002 pflip_present = pflip_present || plane_needs_flip;
10003
10004 if (!plane_needs_flip) {
10005 planes_count += 1;
10006 continue;
10007 }
10008
10009 fill_dc_plane_info_and_addr(
10010 dm->adev, new_plane_state,
10011 afb->tiling_flags,
10012 &bundle->plane_infos[planes_count],
10013 &bundle->flip_addrs[planes_count].address,
10014 afb->tmz_surface);
10015
10016 drm_dbg_state(state->dev, "plane: id=%d dcc_en=%d\n",
10017 new_plane_state->plane->index,
10018 bundle->plane_infos[planes_count].dcc.enable);
10019
10020 bundle->surface_updates[planes_count].plane_info =
10021 &bundle->plane_infos[planes_count];
10022
10023 if (acrtc_state->stream->link->psr_settings.psr_feature_enabled ||
10024 acrtc_state->stream->link->replay_settings.replay_feature_enabled) {
10025 fill_dc_dirty_rects(plane, old_plane_state,
10026 new_plane_state, new_crtc_state,
10027 &bundle->flip_addrs[planes_count],
10028 acrtc_state->stream->link->psr_settings.psr_version ==
10029 DC_PSR_VERSION_SU_1,
10030 &dirty_rects_changed);
10031
10032 /*
10033 * If the dirty regions changed, PSR-SU need to be disabled temporarily
10034 * and enabled it again after dirty regions are stable to avoid video glitch.
10035 * PSR-SU will be enabled in vblank_control_worker() if user pause the video
10036 * during the PSR-SU was disabled.
10037 */
10038 if (acrtc_state->stream->link->psr_settings.psr_version >= DC_PSR_VERSION_SU_1 &&
10039 acrtc_attach->dm_irq_params.allow_sr_entry &&
10040 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
10041 !amdgpu_dm_crc_window_is_activated(acrtc_state->base.crtc) &&
10042 #endif
10043 dirty_rects_changed) {
10044 mutex_lock(&dm->dc_lock);
10045 acrtc_state->stream->link->psr_settings.psr_dirty_rects_change_timestamp_ns =
10046 timestamp_ns;
10047 if (acrtc_state->stream->link->psr_settings.psr_allow_active)
10048 amdgpu_dm_psr_disable(acrtc_state->stream, true);
10049 mutex_unlock(&dm->dc_lock);
10050 }
10051 }
10052
10053 /*
10054 * Only allow immediate flips for fast updates that don't
10055 * change memory domain, FB pitch, DCC state, rotation or
10056 * mirroring.
10057 *
10058 * dm_crtc_helper_atomic_check() only accepts async flips with
10059 * fast updates.
10060 */
10061 if (crtc->state->async_flip &&
10062 (acrtc_state->update_type != UPDATE_TYPE_FAST ||
10063 get_mem_type(old_plane_state->fb) != get_mem_type(fb)))
10064 drm_warn_once(state->dev,
10065 "[PLANE:%d:%s] async flip with non-fast update\n",
10066 plane->base.id, plane->name);
10067
10068 bundle->flip_addrs[planes_count].flip_immediate =
10069 crtc->state->async_flip &&
10070 acrtc_state->update_type == UPDATE_TYPE_FAST &&
10071 get_mem_type(old_plane_state->fb) == get_mem_type(fb);
10072
10073 timestamp_ns = ktime_get_ns();
10074 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000);
10075 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count];
10076 bundle->surface_updates[planes_count].surface = dc_plane;
10077
10078 if (!bundle->surface_updates[planes_count].surface) {
10079 drm_err(dev, "No surface for CRTC: id=%d\n",
10080 acrtc_attach->crtc_id);
10081 continue;
10082 }
10083
10084 if (plane == pcrtc->primary)
10085 update_freesync_state_on_stream(
10086 dm,
10087 acrtc_state,
10088 acrtc_state->stream,
10089 dc_plane,
10090 bundle->flip_addrs[planes_count].flip_timestamp_in_us);
10091
10092 drm_dbg_state(state->dev, "%s Flipping to hi: 0x%x, low: 0x%x\n",
10093 __func__,
10094 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
10095 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
10096
10097 planes_count += 1;
10098
10099 }
10100
10101 if (pflip_present) {
10102 if (!vrr_active) {
10103 /* Use old throttling in non-vrr fixed refresh rate mode
10104 * to keep flip scheduling based on target vblank counts
10105 * working in a backwards compatible way, e.g., for
10106 * clients using the GLX_OML_sync_control extension or
10107 * DRI3/Present extension with defined target_msc.
10108 */
10109 last_flip_vblank = amdgpu_get_vblank_counter_kms(pcrtc);
10110 } else {
10111 /* For variable refresh rate mode only:
10112 * Get vblank of last completed flip to avoid > 1 vrr
10113 * flips per video frame by use of throttling, but allow
10114 * flip programming anywhere in the possibly large
10115 * variable vrr vblank interval for fine-grained flip
10116 * timing control and more opportunity to avoid stutter
10117 * on late submission of flips.
10118 */
10119 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10120 last_flip_vblank = acrtc_attach->dm_irq_params.last_flip_vblank;
10121 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10122 }
10123
10124 target_vblank = last_flip_vblank + wait_for_vblank;
10125
10126 /*
10127 * Wait until we're out of the vertical blank period before the one
10128 * targeted by the flip
10129 */
10130 while ((acrtc_attach->enabled &&
10131 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id,
10132 0, &vpos, &hpos, NULL,
10133 NULL, &pcrtc->hwmode)
10134 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
10135 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
10136 (int)(target_vblank -
10137 amdgpu_get_vblank_counter_kms(pcrtc)) > 0)) {
10138 usleep_range(1000, 1100);
10139 }
10140
10141 /**
10142 * Prepare the flip event for the pageflip interrupt to handle.
10143 *
10144 * This only works in the case where we've already turned on the
10145 * appropriate hardware blocks (eg. HUBP) so in the transition case
10146 * from 0 -> n planes we have to skip a hardware generated event
10147 * and rely on sending it from software.
10148 */
10149 if (acrtc_attach->base.state->event &&
10150 acrtc_state->active_planes > 0) {
10151 drm_crtc_vblank_get(pcrtc);
10152
10153 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10154
10155 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE);
10156 prepare_flip_isr(acrtc_attach);
10157
10158 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10159 }
10160
10161 if (acrtc_state->stream) {
10162 if (acrtc_state->freesync_vrr_info_changed)
10163 bundle->stream_update.vrr_infopacket =
10164 &acrtc_state->stream->vrr_infopacket;
10165 }
10166 } else if (cursor_update && acrtc_state->active_planes > 0) {
10167 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10168 if (acrtc_attach->base.state->event) {
10169 drm_crtc_vblank_get(pcrtc);
10170 acrtc_attach->event = acrtc_attach->base.state->event;
10171 acrtc_attach->base.state->event = NULL;
10172 }
10173 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10174 }
10175
10176 /* Update the planes if changed or disable if we don't have any. */
10177 if ((planes_count || acrtc_state->active_planes == 0) &&
10178 acrtc_state->stream) {
10179 /*
10180 * If PSR or idle optimizations are enabled then flush out
10181 * any pending work before hardware programming.
10182 */
10183 if (dm->vblank_control_workqueue)
10184 flush_workqueue(dm->vblank_control_workqueue);
10185
10186 bundle->stream_update.stream = acrtc_state->stream;
10187 if (new_pcrtc_state->mode_changed) {
10188 bundle->stream_update.src = acrtc_state->stream->src;
10189 bundle->stream_update.dst = acrtc_state->stream->dst;
10190 }
10191
10192 if (new_pcrtc_state->color_mgmt_changed) {
10193 /*
10194 * TODO: This isn't fully correct since we've actually
10195 * already modified the stream in place.
10196 */
10197 bundle->stream_update.gamut_remap =
10198 &acrtc_state->stream->gamut_remap_matrix;
10199 bundle->stream_update.output_csc_transform =
10200 &acrtc_state->stream->csc_color_matrix;
10201 bundle->stream_update.out_transfer_func =
10202 &acrtc_state->stream->out_transfer_func;
10203 bundle->stream_update.lut3d_func =
10204 (struct dc_3dlut *) acrtc_state->stream->lut3d_func;
10205 bundle->stream_update.func_shaper =
10206 (struct dc_transfer_func *) acrtc_state->stream->func_shaper;
10207 }
10208
10209 acrtc_state->stream->abm_level = acrtc_state->abm_level;
10210 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level)
10211 bundle->stream_update.abm_level = &acrtc_state->abm_level;
10212
10213 mutex_lock(&dm->dc_lock);
10214 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) || vrr_active) {
10215 if (acrtc_state->stream->link->replay_settings.replay_allow_active)
10216 amdgpu_dm_replay_disable(acrtc_state->stream);
10217 if (acrtc_state->stream->link->psr_settings.psr_allow_active)
10218 amdgpu_dm_psr_disable(acrtc_state->stream, true);
10219 }
10220 mutex_unlock(&dm->dc_lock);
10221
10222 /*
10223 * If FreeSync state on the stream has changed then we need to
10224 * re-adjust the min/max bounds now that DC doesn't handle this
10225 * as part of commit.
10226 */
10227 if (is_dc_timing_adjust_needed(dm_old_crtc_state, acrtc_state)) {
10228 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
10229 dc_stream_adjust_vmin_vmax(
10230 dm->dc, acrtc_state->stream,
10231 &acrtc_attach->dm_irq_params.vrr_params.adjust);
10232 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
10233 }
10234 mutex_lock(&dm->dc_lock);
10235 update_planes_and_stream_adapter(dm->dc,
10236 acrtc_state->update_type,
10237 planes_count,
10238 acrtc_state->stream,
10239 &bundle->stream_update,
10240 bundle->surface_updates);
10241 updated_planes_and_streams = true;
10242
10243 /**
10244 * Enable or disable the interrupts on the backend.
10245 *
10246 * Most pipes are put into power gating when unused.
10247 *
10248 * When power gating is enabled on a pipe we lose the
10249 * interrupt enablement state when power gating is disabled.
10250 *
10251 * So we need to update the IRQ control state in hardware
10252 * whenever the pipe turns on (since it could be previously
10253 * power gated) or off (since some pipes can't be power gated
10254 * on some ASICs).
10255 */
10256 if (dm_old_crtc_state->active_planes != acrtc_state->active_planes)
10257 dm_update_pflip_irq_state(drm_to_adev(dev),
10258 acrtc_attach);
10259
10260 amdgpu_dm_enable_self_refresh(acrtc_attach, acrtc_state, timestamp_ns);
10261 mutex_unlock(&dm->dc_lock);
10262 }
10263
10264 /*
10265 * Update cursor state *after* programming all the planes.
10266 * This avoids redundant programming in the case where we're going
10267 * to be disabling a single plane - those pipes are being disabled.
10268 */
10269 if (acrtc_state->active_planes &&
10270 (!updated_planes_and_streams || amdgpu_ip_version(dm->adev, DCE_HWIP, 0) == 0) &&
10271 acrtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE)
10272 amdgpu_dm_commit_cursors(state);
10273
10274 cleanup:
10275 kfree(bundle);
10276 }
10277
amdgpu_dm_commit_audio(struct drm_device * dev,struct drm_atomic_state * state)10278 static void amdgpu_dm_commit_audio(struct drm_device *dev,
10279 struct drm_atomic_state *state)
10280 {
10281 struct amdgpu_device *adev = drm_to_adev(dev);
10282 struct amdgpu_dm_connector *aconnector;
10283 struct drm_connector *connector;
10284 struct drm_connector_state *old_con_state, *new_con_state;
10285 struct drm_crtc_state *new_crtc_state;
10286 struct dm_crtc_state *new_dm_crtc_state;
10287 const struct dc_stream_status *status;
10288 int i, inst;
10289
10290 /* Notify device removals. */
10291 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10292 if (old_con_state->crtc != new_con_state->crtc) {
10293 /* CRTC changes require notification. */
10294 goto notify;
10295 }
10296
10297 if (!new_con_state->crtc)
10298 continue;
10299
10300 new_crtc_state = drm_atomic_get_new_crtc_state(
10301 state, new_con_state->crtc);
10302
10303 if (!new_crtc_state)
10304 continue;
10305
10306 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
10307 continue;
10308
10309 notify:
10310 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
10311 continue;
10312
10313 aconnector = to_amdgpu_dm_connector(connector);
10314
10315 mutex_lock(&adev->dm.audio_lock);
10316 inst = aconnector->audio_inst;
10317 aconnector->audio_inst = -1;
10318 mutex_unlock(&adev->dm.audio_lock);
10319
10320 amdgpu_dm_audio_eld_notify(adev, inst);
10321 }
10322
10323 /* Notify audio device additions. */
10324 for_each_new_connector_in_state(state, connector, new_con_state, i) {
10325 if (!new_con_state->crtc)
10326 continue;
10327
10328 new_crtc_state = drm_atomic_get_new_crtc_state(
10329 state, new_con_state->crtc);
10330
10331 if (!new_crtc_state)
10332 continue;
10333
10334 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
10335 continue;
10336
10337 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
10338 if (!new_dm_crtc_state->stream)
10339 continue;
10340
10341 status = dc_stream_get_status(new_dm_crtc_state->stream);
10342 if (!status)
10343 continue;
10344
10345 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
10346 continue;
10347
10348 aconnector = to_amdgpu_dm_connector(connector);
10349
10350 mutex_lock(&adev->dm.audio_lock);
10351 inst = status->audio_inst;
10352 aconnector->audio_inst = inst;
10353 mutex_unlock(&adev->dm.audio_lock);
10354
10355 amdgpu_dm_audio_eld_notify(adev, inst);
10356 }
10357 }
10358
10359 /*
10360 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
10361 * @crtc_state: the DRM CRTC state
10362 * @stream_state: the DC stream state.
10363 *
10364 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
10365 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
10366 */
amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state * crtc_state,struct dc_stream_state * stream_state)10367 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
10368 struct dc_stream_state *stream_state)
10369 {
10370 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
10371 }
10372
dm_clear_writeback(struct amdgpu_display_manager * dm,struct dm_crtc_state * crtc_state)10373 static void dm_clear_writeback(struct amdgpu_display_manager *dm,
10374 struct dm_crtc_state *crtc_state)
10375 {
10376 dc_stream_remove_writeback(dm->dc, crtc_state->stream, 0);
10377 }
10378
amdgpu_dm_commit_streams(struct drm_atomic_state * state,struct dc_state * dc_state)10379 static void amdgpu_dm_commit_streams(struct drm_atomic_state *state,
10380 struct dc_state *dc_state)
10381 {
10382 struct drm_device *dev = state->dev;
10383 struct amdgpu_device *adev = drm_to_adev(dev);
10384 struct amdgpu_display_manager *dm = &adev->dm;
10385 struct drm_crtc *crtc;
10386 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10387 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10388 struct drm_connector_state *old_con_state;
10389 struct drm_connector *connector;
10390 bool mode_set_reset_required = false;
10391 u32 i;
10392 struct dc_commit_streams_params params = {dc_state->streams, dc_state->stream_count};
10393 bool set_backlight_level = false;
10394
10395 /* Disable writeback */
10396 for_each_old_connector_in_state(state, connector, old_con_state, i) {
10397 struct dm_connector_state *dm_old_con_state;
10398 struct amdgpu_crtc *acrtc;
10399
10400 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
10401 continue;
10402
10403 old_crtc_state = NULL;
10404
10405 dm_old_con_state = to_dm_connector_state(old_con_state);
10406 if (!dm_old_con_state->base.crtc)
10407 continue;
10408
10409 acrtc = to_amdgpu_crtc(dm_old_con_state->base.crtc);
10410 if (acrtc)
10411 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
10412
10413 if (!acrtc || !acrtc->wb_enabled)
10414 continue;
10415
10416 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10417
10418 dm_clear_writeback(dm, dm_old_crtc_state);
10419 acrtc->wb_enabled = false;
10420 }
10421
10422 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
10423 new_crtc_state, i) {
10424 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
10425
10426 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10427
10428 if (old_crtc_state->active &&
10429 (!new_crtc_state->active ||
10430 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
10431 manage_dm_interrupts(adev, acrtc, NULL);
10432 dc_stream_release(dm_old_crtc_state->stream);
10433 }
10434 }
10435
10436 drm_atomic_helper_calc_timestamping_constants(state);
10437
10438 /* update changed items */
10439 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10440 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
10441
10442 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10443 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10444
10445 drm_dbg_state(state->dev,
10446 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n",
10447 acrtc->crtc_id,
10448 new_crtc_state->enable,
10449 new_crtc_state->active,
10450 new_crtc_state->planes_changed,
10451 new_crtc_state->mode_changed,
10452 new_crtc_state->active_changed,
10453 new_crtc_state->connectors_changed);
10454
10455 /* Disable cursor if disabling crtc */
10456 if (old_crtc_state->active && !new_crtc_state->active) {
10457 struct dc_cursor_position position;
10458
10459 memset(&position, 0, sizeof(position));
10460 mutex_lock(&dm->dc_lock);
10461 dc_exit_ips_for_hw_access(dm->dc);
10462 dc_stream_program_cursor_position(dm_old_crtc_state->stream, &position);
10463 mutex_unlock(&dm->dc_lock);
10464 }
10465
10466 /* Copy all transient state flags into dc state */
10467 if (dm_new_crtc_state->stream) {
10468 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
10469 dm_new_crtc_state->stream);
10470 }
10471
10472 /* handles headless hotplug case, updating new_state and
10473 * aconnector as needed
10474 */
10475
10476 if (amdgpu_dm_crtc_modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
10477
10478 drm_dbg_atomic(dev,
10479 "Atomic commit: SET crtc id %d: [%p]\n",
10480 acrtc->crtc_id, acrtc);
10481
10482 if (!dm_new_crtc_state->stream) {
10483 /*
10484 * this could happen because of issues with
10485 * userspace notifications delivery.
10486 * In this case userspace tries to set mode on
10487 * display which is disconnected in fact.
10488 * dc_sink is NULL in this case on aconnector.
10489 * We expect reset mode will come soon.
10490 *
10491 * This can also happen when unplug is done
10492 * during resume sequence ended
10493 *
10494 * In this case, we want to pretend we still
10495 * have a sink to keep the pipe running so that
10496 * hw state is consistent with the sw state
10497 */
10498 drm_dbg_atomic(dev,
10499 "Failed to create new stream for crtc %d\n",
10500 acrtc->base.base.id);
10501 continue;
10502 }
10503
10504 if (dm_old_crtc_state->stream)
10505 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
10506
10507 pm_runtime_get_noresume(dev->dev);
10508
10509 acrtc->enabled = true;
10510 acrtc->hw_mode = new_crtc_state->mode;
10511 crtc->hwmode = new_crtc_state->mode;
10512 mode_set_reset_required = true;
10513 set_backlight_level = true;
10514 } else if (modereset_required(new_crtc_state)) {
10515 drm_dbg_atomic(dev,
10516 "Atomic commit: RESET. crtc id %d:[%p]\n",
10517 acrtc->crtc_id, acrtc);
10518 /* i.e. reset mode */
10519 if (dm_old_crtc_state->stream)
10520 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
10521
10522 mode_set_reset_required = true;
10523 }
10524 } /* for_each_crtc_in_state() */
10525
10526 /* if there mode set or reset, disable eDP PSR, Replay */
10527 if (mode_set_reset_required) {
10528 if (dm->vblank_control_workqueue)
10529 flush_workqueue(dm->vblank_control_workqueue);
10530
10531 amdgpu_dm_replay_disable_all(dm);
10532 amdgpu_dm_psr_disable_all(dm);
10533 }
10534
10535 dm_enable_per_frame_crtc_master_sync(dc_state);
10536 mutex_lock(&dm->dc_lock);
10537 dc_exit_ips_for_hw_access(dm->dc);
10538 WARN_ON(!dc_commit_streams(dm->dc, ¶ms));
10539
10540 /* Allow idle optimization when vblank count is 0 for display off */
10541 if ((dm->active_vblank_irq_count == 0) && amdgpu_dm_is_headless(dm->adev))
10542 dc_allow_idle_optimizations(dm->dc, true);
10543 mutex_unlock(&dm->dc_lock);
10544
10545 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
10546 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
10547
10548 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10549
10550 if (dm_new_crtc_state->stream != NULL) {
10551 const struct dc_stream_status *status =
10552 dc_stream_get_status(dm_new_crtc_state->stream);
10553
10554 if (!status)
10555 status = dc_state_get_stream_status(dc_state,
10556 dm_new_crtc_state->stream);
10557 if (!status)
10558 drm_err(dev,
10559 "got no status for stream %p on acrtc%p\n",
10560 dm_new_crtc_state->stream, acrtc);
10561 else
10562 acrtc->otg_inst = status->primary_otg_inst;
10563 }
10564 }
10565
10566 /* During boot up and resume the DC layer will reset the panel brightness
10567 * to fix a flicker issue.
10568 * It will cause the dm->actual_brightness is not the current panel brightness
10569 * level. (the dm->brightness is the correct panel level)
10570 * So we set the backlight level with dm->brightness value after set mode
10571 */
10572 if (set_backlight_level) {
10573 for (i = 0; i < dm->num_of_edps; i++) {
10574 if (dm->backlight_dev[i])
10575 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
10576 }
10577 }
10578 }
10579
dm_set_writeback(struct amdgpu_display_manager * dm,struct dm_crtc_state * crtc_state,struct drm_connector * connector,struct drm_connector_state * new_con_state)10580 static void dm_set_writeback(struct amdgpu_display_manager *dm,
10581 struct dm_crtc_state *crtc_state,
10582 struct drm_connector *connector,
10583 struct drm_connector_state *new_con_state)
10584 {
10585 struct drm_writeback_connector *wb_conn = drm_connector_to_writeback(connector);
10586 struct amdgpu_device *adev = dm->adev;
10587 struct amdgpu_crtc *acrtc;
10588 struct dc_writeback_info *wb_info;
10589 struct pipe_ctx *pipe = NULL;
10590 struct amdgpu_framebuffer *afb;
10591 int i = 0;
10592
10593 wb_info = kzalloc(sizeof(*wb_info), GFP_KERNEL);
10594 if (!wb_info) {
10595 drm_err(adev_to_drm(adev), "Failed to allocate wb_info\n");
10596 return;
10597 }
10598
10599 acrtc = to_amdgpu_crtc(wb_conn->encoder.crtc);
10600 if (!acrtc) {
10601 drm_err(adev_to_drm(adev), "no amdgpu_crtc found\n");
10602 kfree(wb_info);
10603 return;
10604 }
10605
10606 afb = to_amdgpu_framebuffer(new_con_state->writeback_job->fb);
10607 if (!afb) {
10608 drm_err(adev_to_drm(adev), "No amdgpu_framebuffer found\n");
10609 kfree(wb_info);
10610 return;
10611 }
10612
10613 for (i = 0; i < MAX_PIPES; i++) {
10614 if (dm->dc->current_state->res_ctx.pipe_ctx[i].stream == crtc_state->stream) {
10615 pipe = &dm->dc->current_state->res_ctx.pipe_ctx[i];
10616 break;
10617 }
10618 }
10619
10620 /* fill in wb_info */
10621 wb_info->wb_enabled = true;
10622
10623 wb_info->dwb_pipe_inst = 0;
10624 wb_info->dwb_params.dwbscl_black_color = 0;
10625 wb_info->dwb_params.hdr_mult = 0x1F000;
10626 wb_info->dwb_params.csc_params.gamut_adjust_type = CM_GAMUT_ADJUST_TYPE_BYPASS;
10627 wb_info->dwb_params.csc_params.gamut_coef_format = CM_GAMUT_REMAP_COEF_FORMAT_S2_13;
10628 wb_info->dwb_params.output_depth = DWB_OUTPUT_PIXEL_DEPTH_10BPC;
10629 wb_info->dwb_params.cnv_params.cnv_out_bpc = DWB_CNV_OUT_BPC_10BPC;
10630
10631 /* width & height from crtc */
10632 wb_info->dwb_params.cnv_params.src_width = acrtc->base.mode.crtc_hdisplay;
10633 wb_info->dwb_params.cnv_params.src_height = acrtc->base.mode.crtc_vdisplay;
10634 wb_info->dwb_params.dest_width = acrtc->base.mode.crtc_hdisplay;
10635 wb_info->dwb_params.dest_height = acrtc->base.mode.crtc_vdisplay;
10636
10637 wb_info->dwb_params.cnv_params.crop_en = false;
10638 wb_info->dwb_params.stereo_params.stereo_enabled = false;
10639
10640 wb_info->dwb_params.cnv_params.out_max_pix_val = 0x3ff; // 10 bits
10641 wb_info->dwb_params.cnv_params.out_min_pix_val = 0;
10642 wb_info->dwb_params.cnv_params.fc_out_format = DWB_OUT_FORMAT_32BPP_ARGB;
10643 wb_info->dwb_params.cnv_params.out_denorm_mode = DWB_OUT_DENORM_BYPASS;
10644
10645 wb_info->dwb_params.out_format = dwb_scaler_mode_bypass444;
10646
10647 wb_info->dwb_params.capture_rate = dwb_capture_rate_0;
10648
10649 wb_info->dwb_params.scaler_taps.h_taps = 4;
10650 wb_info->dwb_params.scaler_taps.v_taps = 4;
10651 wb_info->dwb_params.scaler_taps.h_taps_c = 2;
10652 wb_info->dwb_params.scaler_taps.v_taps_c = 2;
10653 wb_info->dwb_params.subsample_position = DWB_INTERSTITIAL_SUBSAMPLING;
10654
10655 wb_info->mcif_buf_params.luma_pitch = afb->base.pitches[0];
10656 wb_info->mcif_buf_params.chroma_pitch = afb->base.pitches[1];
10657
10658 for (i = 0; i < DWB_MCIF_BUF_COUNT; i++) {
10659 wb_info->mcif_buf_params.luma_address[i] = afb->address;
10660 wb_info->mcif_buf_params.chroma_address[i] = 0;
10661 }
10662
10663 wb_info->mcif_buf_params.p_vmid = 1;
10664 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 0, 0)) {
10665 wb_info->mcif_warmup_params.start_address.quad_part = afb->address;
10666 wb_info->mcif_warmup_params.region_size =
10667 wb_info->mcif_buf_params.luma_pitch * wb_info->dwb_params.dest_height;
10668 }
10669 wb_info->mcif_warmup_params.p_vmid = 1;
10670 wb_info->writeback_source_plane = pipe->plane_state;
10671
10672 dc_stream_add_writeback(dm->dc, crtc_state->stream, wb_info);
10673
10674 acrtc->wb_pending = true;
10675 acrtc->wb_conn = wb_conn;
10676 drm_writeback_queue_job(wb_conn, new_con_state);
10677 }
10678
amdgpu_dm_update_hdcp(struct drm_atomic_state * state)10679 static void amdgpu_dm_update_hdcp(struct drm_atomic_state *state)
10680 {
10681 struct drm_connector_state *old_con_state, *new_con_state;
10682 struct drm_device *dev = state->dev;
10683 struct drm_connector *connector;
10684 struct amdgpu_device *adev = drm_to_adev(dev);
10685 int i;
10686
10687 if (!adev->dm.hdcp_workqueue)
10688 return;
10689
10690 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10691 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
10692 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
10693 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10694 struct dm_crtc_state *dm_new_crtc_state;
10695 struct amdgpu_dm_connector *aconnector;
10696
10697 if (!connector || connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
10698 continue;
10699
10700 aconnector = to_amdgpu_dm_connector(connector);
10701
10702 drm_dbg(dev, "[HDCP_DM] -------------- i : %x ----------\n", i);
10703
10704 drm_dbg(dev, "[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n",
10705 connector->index, connector->status, connector->dpms);
10706 drm_dbg(dev, "[HDCP_DM] state protection old: %x new: %x\n",
10707 old_con_state->content_protection, new_con_state->content_protection);
10708
10709 if (aconnector->dc_sink) {
10710 if (aconnector->dc_sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
10711 aconnector->dc_sink->sink_signal != SIGNAL_TYPE_NONE) {
10712 drm_dbg(dev, "[HDCP_DM] pipe_ctx dispname=%s\n",
10713 aconnector->dc_sink->edid_caps.display_name);
10714 }
10715 }
10716
10717 new_crtc_state = NULL;
10718 old_crtc_state = NULL;
10719
10720 if (acrtc) {
10721 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
10722 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
10723 }
10724
10725 if (old_crtc_state)
10726 drm_dbg(dev, "old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
10727 old_crtc_state->enable,
10728 old_crtc_state->active,
10729 old_crtc_state->mode_changed,
10730 old_crtc_state->active_changed,
10731 old_crtc_state->connectors_changed);
10732
10733 if (new_crtc_state)
10734 drm_dbg(dev, "NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n",
10735 new_crtc_state->enable,
10736 new_crtc_state->active,
10737 new_crtc_state->mode_changed,
10738 new_crtc_state->active_changed,
10739 new_crtc_state->connectors_changed);
10740
10741
10742 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10743
10744 if (dm_new_crtc_state && dm_new_crtc_state->stream == NULL &&
10745 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
10746 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
10747 new_con_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
10748 dm_new_con_state->update_hdcp = true;
10749 continue;
10750 }
10751
10752 if (is_content_protection_different(new_crtc_state, old_crtc_state, new_con_state,
10753 old_con_state, connector, adev->dm.hdcp_workqueue)) {
10754 /* when display is unplugged from mst hub, connctor will
10755 * be destroyed within dm_dp_mst_connector_destroy. connector
10756 * hdcp perperties, like type, undesired, desired, enabled,
10757 * will be lost. So, save hdcp properties into hdcp_work within
10758 * amdgpu_dm_atomic_commit_tail. if the same display is
10759 * plugged back with same display index, its hdcp properties
10760 * will be retrieved from hdcp_work within dm_dp_mst_get_modes
10761 */
10762
10763 bool enable_encryption = false;
10764
10765 if (new_con_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED)
10766 enable_encryption = true;
10767
10768 if (aconnector->dc_link && aconnector->dc_sink &&
10769 aconnector->dc_link->type == dc_connection_mst_branch) {
10770 struct hdcp_workqueue *hdcp_work = adev->dm.hdcp_workqueue;
10771 struct hdcp_workqueue *hdcp_w =
10772 &hdcp_work[aconnector->dc_link->link_index];
10773
10774 hdcp_w->hdcp_content_type[connector->index] =
10775 new_con_state->hdcp_content_type;
10776 hdcp_w->content_protection[connector->index] =
10777 new_con_state->content_protection;
10778 }
10779
10780 if (new_crtc_state && new_crtc_state->mode_changed &&
10781 new_con_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED)
10782 enable_encryption = true;
10783
10784 drm_info(dev, "[HDCP_DM] hdcp_update_display enable_encryption = %x\n", enable_encryption);
10785
10786 if (aconnector->dc_link)
10787 hdcp_update_display(
10788 adev->dm.hdcp_workqueue, aconnector->dc_link->link_index, aconnector,
10789 new_con_state->hdcp_content_type, enable_encryption);
10790 }
10791 }
10792 }
10793
amdgpu_dm_atomic_setup_commit(struct drm_atomic_state * state)10794 static int amdgpu_dm_atomic_setup_commit(struct drm_atomic_state *state)
10795 {
10796 struct drm_crtc *crtc;
10797 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10798 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10799 int i, ret;
10800
10801 ret = drm_dp_mst_atomic_setup_commit(state);
10802 if (ret)
10803 return ret;
10804
10805 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10806 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10807 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10808 /*
10809 * Color management settings. We also update color properties
10810 * when a modeset is needed, to ensure it gets reprogrammed.
10811 */
10812 if (dm_new_crtc_state->base.active && dm_new_crtc_state->stream &&
10813 (dm_new_crtc_state->base.color_mgmt_changed ||
10814 dm_old_crtc_state->regamma_tf != dm_new_crtc_state->regamma_tf ||
10815 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
10816 ret = amdgpu_dm_update_crtc_color_mgmt(dm_new_crtc_state);
10817 if (ret) {
10818 drm_dbg_atomic(state->dev, "Failed to update color state\n");
10819 return ret;
10820 }
10821 }
10822 }
10823
10824 return 0;
10825 }
10826
10827 /**
10828 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation.
10829 * @state: The atomic state to commit
10830 *
10831 * This will tell DC to commit the constructed DC state from atomic_check,
10832 * programming the hardware. Any failures here implies a hardware failure, since
10833 * atomic check should have filtered anything non-kosher.
10834 */
amdgpu_dm_atomic_commit_tail(struct drm_atomic_state * state)10835 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
10836 {
10837 struct drm_device *dev = state->dev;
10838 struct amdgpu_device *adev = drm_to_adev(dev);
10839 struct amdgpu_display_manager *dm = &adev->dm;
10840 struct dm_atomic_state *dm_state;
10841 struct dc_state *dc_state = NULL;
10842 u32 i, j;
10843 struct drm_crtc *crtc;
10844 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10845 unsigned long flags;
10846 bool wait_for_vblank = true;
10847 struct drm_connector *connector;
10848 struct drm_connector_state *old_con_state = NULL, *new_con_state = NULL;
10849 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10850 int crtc_disable_count = 0;
10851
10852 trace_amdgpu_dm_atomic_commit_tail_begin(state);
10853
10854 drm_atomic_helper_update_legacy_modeset_state(dev, state);
10855 drm_dp_mst_atomic_wait_for_dependencies(state);
10856
10857 dm_state = dm_atomic_get_new_state(state);
10858 if (dm_state && dm_state->context) {
10859 dc_state = dm_state->context;
10860 amdgpu_dm_commit_streams(state, dc_state);
10861 }
10862
10863 amdgpu_dm_update_hdcp(state);
10864
10865 /* Handle connector state changes */
10866 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10867 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
10868 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
10869 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
10870 struct dc_surface_update *dummy_updates;
10871 struct dc_stream_update stream_update;
10872 struct dc_info_packet hdr_packet;
10873 struct dc_stream_status *status = NULL;
10874 bool abm_changed, hdr_changed, scaling_changed, output_color_space_changed = false;
10875
10876 memset(&stream_update, 0, sizeof(stream_update));
10877
10878 if (acrtc) {
10879 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
10880 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
10881 }
10882
10883 /* Skip any modesets/resets */
10884 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
10885 continue;
10886
10887 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10888 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10889
10890 scaling_changed = is_scaling_state_different(dm_new_con_state,
10891 dm_old_con_state);
10892
10893 if ((new_con_state->hdmi.broadcast_rgb != old_con_state->hdmi.broadcast_rgb) &&
10894 (dm_old_crtc_state->stream->output_color_space !=
10895 get_output_color_space(&dm_new_crtc_state->stream->timing, new_con_state)))
10896 output_color_space_changed = true;
10897
10898 abm_changed = dm_new_crtc_state->abm_level !=
10899 dm_old_crtc_state->abm_level;
10900
10901 hdr_changed =
10902 !drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state);
10903
10904 if (!scaling_changed && !abm_changed && !hdr_changed && !output_color_space_changed)
10905 continue;
10906
10907 stream_update.stream = dm_new_crtc_state->stream;
10908 if (scaling_changed) {
10909 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
10910 dm_new_con_state, dm_new_crtc_state->stream);
10911
10912 stream_update.src = dm_new_crtc_state->stream->src;
10913 stream_update.dst = dm_new_crtc_state->stream->dst;
10914 }
10915
10916 if (output_color_space_changed) {
10917 dm_new_crtc_state->stream->output_color_space
10918 = get_output_color_space(&dm_new_crtc_state->stream->timing, new_con_state);
10919
10920 stream_update.output_color_space = &dm_new_crtc_state->stream->output_color_space;
10921 }
10922
10923 if (abm_changed) {
10924 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level;
10925
10926 stream_update.abm_level = &dm_new_crtc_state->abm_level;
10927 }
10928
10929 if (hdr_changed) {
10930 fill_hdr_info_packet(new_con_state, &hdr_packet);
10931 stream_update.hdr_static_metadata = &hdr_packet;
10932 }
10933
10934 status = dc_stream_get_status(dm_new_crtc_state->stream);
10935
10936 if (WARN_ON(!status))
10937 continue;
10938
10939 WARN_ON(!status->plane_count);
10940
10941 /*
10942 * TODO: DC refuses to perform stream updates without a dc_surface_update.
10943 * Here we create an empty update on each plane.
10944 * To fix this, DC should permit updating only stream properties.
10945 */
10946 dummy_updates = kzalloc(sizeof(struct dc_surface_update) * MAX_SURFACES, GFP_KERNEL);
10947 if (!dummy_updates) {
10948 drm_err(adev_to_drm(adev), "Failed to allocate memory for dummy_updates.\n");
10949 continue;
10950 }
10951 for (j = 0; j < status->plane_count; j++)
10952 dummy_updates[j].surface = status->plane_states[0];
10953
10954 sort(dummy_updates, status->plane_count,
10955 sizeof(*dummy_updates), dm_plane_layer_index_cmp, NULL);
10956
10957 mutex_lock(&dm->dc_lock);
10958 dc_exit_ips_for_hw_access(dm->dc);
10959 dc_update_planes_and_stream(dm->dc,
10960 dummy_updates,
10961 status->plane_count,
10962 dm_new_crtc_state->stream,
10963 &stream_update);
10964 mutex_unlock(&dm->dc_lock);
10965 kfree(dummy_updates);
10966
10967 drm_connector_update_privacy_screen(new_con_state);
10968 }
10969
10970 /**
10971 * Enable interrupts for CRTCs that are newly enabled or went through
10972 * a modeset. It was intentionally deferred until after the front end
10973 * state was modified to wait until the OTG was on and so the IRQ
10974 * handlers didn't access stale or invalid state.
10975 */
10976 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10977 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
10978 #ifdef CONFIG_DEBUG_FS
10979 enum amdgpu_dm_pipe_crc_source cur_crc_src;
10980 #endif
10981 /* Count number of newly disabled CRTCs for dropping PM refs later. */
10982 if (old_crtc_state->active && !new_crtc_state->active)
10983 crtc_disable_count++;
10984
10985 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10986 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10987
10988 /* For freesync config update on crtc state and params for irq */
10989 update_stream_irq_parameters(dm, dm_new_crtc_state);
10990
10991 #ifdef CONFIG_DEBUG_FS
10992 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
10993 cur_crc_src = acrtc->dm_irq_params.crc_src;
10994 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
10995 #endif
10996
10997 if (new_crtc_state->active &&
10998 (!old_crtc_state->active ||
10999 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
11000 dc_stream_retain(dm_new_crtc_state->stream);
11001 acrtc->dm_irq_params.stream = dm_new_crtc_state->stream;
11002 manage_dm_interrupts(adev, acrtc, dm_new_crtc_state);
11003 }
11004 /* Handle vrr on->off / off->on transitions */
11005 amdgpu_dm_handle_vrr_transition(dm_old_crtc_state, dm_new_crtc_state);
11006
11007 #ifdef CONFIG_DEBUG_FS
11008 if (new_crtc_state->active &&
11009 (!old_crtc_state->active ||
11010 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
11011 /**
11012 * Frontend may have changed so reapply the CRC capture
11013 * settings for the stream.
11014 */
11015 if (amdgpu_dm_is_valid_crc_source(cur_crc_src)) {
11016 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
11017 if (amdgpu_dm_crc_window_is_activated(crtc)) {
11018 uint8_t cnt;
11019
11020 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
11021 for (cnt = 0; cnt < MAX_CRC_WINDOW_NUM; cnt++) {
11022 if (acrtc->dm_irq_params.window_param[cnt].enable) {
11023 acrtc->dm_irq_params.window_param[cnt].update_win = true;
11024
11025 /**
11026 * It takes 2 frames for HW to stably generate CRC when
11027 * resuming from suspend, so we set skip_frame_cnt 2.
11028 */
11029 acrtc->dm_irq_params.window_param[cnt].skip_frame_cnt = 2;
11030 }
11031 }
11032 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
11033 }
11034 #endif
11035 if (amdgpu_dm_crtc_configure_crc_source(
11036 crtc, dm_new_crtc_state, cur_crc_src))
11037 drm_dbg_atomic(dev, "Failed to configure crc source");
11038 }
11039 }
11040 #endif
11041 }
11042
11043 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j)
11044 if (new_crtc_state->async_flip)
11045 wait_for_vblank = false;
11046
11047 /* update planes when needed per crtc*/
11048 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
11049 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11050
11051 if (dm_new_crtc_state->stream)
11052 amdgpu_dm_commit_planes(state, dev, dm, crtc, wait_for_vblank);
11053 }
11054
11055 /* Enable writeback */
11056 for_each_new_connector_in_state(state, connector, new_con_state, i) {
11057 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
11058 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
11059
11060 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
11061 continue;
11062
11063 if (!new_con_state->writeback_job)
11064 continue;
11065
11066 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
11067
11068 if (!new_crtc_state)
11069 continue;
11070
11071 if (acrtc->wb_enabled)
11072 continue;
11073
11074 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11075
11076 dm_set_writeback(dm, dm_new_crtc_state, connector, new_con_state);
11077 acrtc->wb_enabled = true;
11078 }
11079
11080 /* Update audio instances for each connector. */
11081 amdgpu_dm_commit_audio(dev, state);
11082
11083 /* restore the backlight level */
11084 for (i = 0; i < dm->num_of_edps; i++) {
11085 if (dm->backlight_dev[i] &&
11086 (dm->actual_brightness[i] != dm->brightness[i]))
11087 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
11088 }
11089
11090 /*
11091 * send vblank event on all events not handled in flip and
11092 * mark consumed event for drm_atomic_helper_commit_hw_done
11093 */
11094 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
11095 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
11096
11097 if (new_crtc_state->event)
11098 drm_send_event_locked(dev, &new_crtc_state->event->base);
11099
11100 new_crtc_state->event = NULL;
11101 }
11102 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
11103
11104 /* Signal HW programming completion */
11105 drm_atomic_helper_commit_hw_done(state);
11106
11107 if (wait_for_vblank)
11108 drm_atomic_helper_wait_for_flip_done(dev, state);
11109
11110 drm_atomic_helper_cleanup_planes(dev, state);
11111
11112 /* Don't free the memory if we are hitting this as part of suspend.
11113 * This way we don't free any memory during suspend; see
11114 * amdgpu_bo_free_kernel(). The memory will be freed in the first
11115 * non-suspend modeset or when the driver is torn down.
11116 */
11117 if (!adev->in_suspend) {
11118 /* return the stolen vga memory back to VRAM */
11119 if (!adev->mman.keep_stolen_vga_memory)
11120 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
11121 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL);
11122 }
11123
11124 /*
11125 * Finally, drop a runtime PM reference for each newly disabled CRTC,
11126 * so we can put the GPU into runtime suspend if we're not driving any
11127 * displays anymore
11128 */
11129 for (i = 0; i < crtc_disable_count; i++)
11130 pm_runtime_put_autosuspend(dev->dev);
11131 pm_runtime_mark_last_busy(dev->dev);
11132
11133 trace_amdgpu_dm_atomic_commit_tail_finish(state);
11134 }
11135
dm_force_atomic_commit(struct drm_connector * connector)11136 static int dm_force_atomic_commit(struct drm_connector *connector)
11137 {
11138 int ret = 0;
11139 struct drm_device *ddev = connector->dev;
11140 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
11141 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
11142 struct drm_plane *plane = disconnected_acrtc->base.primary;
11143 struct drm_connector_state *conn_state;
11144 struct drm_crtc_state *crtc_state;
11145 struct drm_plane_state *plane_state;
11146
11147 if (!state)
11148 return -ENOMEM;
11149
11150 state->acquire_ctx = ddev->mode_config.acquire_ctx;
11151
11152 /* Construct an atomic state to restore previous display setting */
11153
11154 /*
11155 * Attach connectors to drm_atomic_state
11156 */
11157 conn_state = drm_atomic_get_connector_state(state, connector);
11158
11159 /* Check for error in getting connector state */
11160 if (IS_ERR(conn_state)) {
11161 ret = PTR_ERR(conn_state);
11162 goto out;
11163 }
11164
11165 /* Attach crtc to drm_atomic_state*/
11166 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
11167
11168 /* Check for error in getting crtc state */
11169 if (IS_ERR(crtc_state)) {
11170 ret = PTR_ERR(crtc_state);
11171 goto out;
11172 }
11173
11174 /* force a restore */
11175 crtc_state->mode_changed = true;
11176
11177 /* Attach plane to drm_atomic_state */
11178 plane_state = drm_atomic_get_plane_state(state, plane);
11179
11180 /* Check for error in getting plane state */
11181 if (IS_ERR(plane_state)) {
11182 ret = PTR_ERR(plane_state);
11183 goto out;
11184 }
11185
11186 /* Call commit internally with the state we just constructed */
11187 ret = drm_atomic_commit(state);
11188
11189 out:
11190 drm_atomic_state_put(state);
11191 if (ret)
11192 drm_err(ddev, "Restoring old state failed with %i\n", ret);
11193
11194 return ret;
11195 }
11196
11197 /*
11198 * This function handles all cases when set mode does not come upon hotplug.
11199 * This includes when a display is unplugged then plugged back into the
11200 * same port and when running without usermode desktop manager supprot
11201 */
dm_restore_drm_connector_state(struct drm_device * dev,struct drm_connector * connector)11202 void dm_restore_drm_connector_state(struct drm_device *dev,
11203 struct drm_connector *connector)
11204 {
11205 struct amdgpu_dm_connector *aconnector;
11206 struct amdgpu_crtc *disconnected_acrtc;
11207 struct dm_crtc_state *acrtc_state;
11208
11209 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
11210 return;
11211
11212 aconnector = to_amdgpu_dm_connector(connector);
11213
11214 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
11215 return;
11216
11217 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
11218 if (!disconnected_acrtc)
11219 return;
11220
11221 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
11222 if (!acrtc_state->stream)
11223 return;
11224
11225 /*
11226 * If the previous sink is not released and different from the current,
11227 * we deduce we are in a state where we can not rely on usermode call
11228 * to turn on the display, so we do it here
11229 */
11230 if (acrtc_state->stream->sink != aconnector->dc_sink)
11231 dm_force_atomic_commit(&aconnector->base);
11232 }
11233
11234 /*
11235 * Grabs all modesetting locks to serialize against any blocking commits,
11236 * Waits for completion of all non blocking commits.
11237 */
do_aquire_global_lock(struct drm_device * dev,struct drm_atomic_state * state)11238 static int do_aquire_global_lock(struct drm_device *dev,
11239 struct drm_atomic_state *state)
11240 {
11241 struct drm_crtc *crtc;
11242 struct drm_crtc_commit *commit;
11243 long ret;
11244
11245 /*
11246 * Adding all modeset locks to aquire_ctx will
11247 * ensure that when the framework release it the
11248 * extra locks we are locking here will get released to
11249 */
11250 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
11251 if (ret)
11252 return ret;
11253
11254 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11255 spin_lock(&crtc->commit_lock);
11256 commit = list_first_entry_or_null(&crtc->commit_list,
11257 struct drm_crtc_commit, commit_entry);
11258 if (commit)
11259 drm_crtc_commit_get(commit);
11260 spin_unlock(&crtc->commit_lock);
11261
11262 if (!commit)
11263 continue;
11264
11265 /*
11266 * Make sure all pending HW programming completed and
11267 * page flips done
11268 */
11269 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
11270
11271 if (ret > 0)
11272 ret = wait_for_completion_interruptible_timeout(
11273 &commit->flip_done, 10*HZ);
11274
11275 if (ret == 0)
11276 drm_err(dev, "[CRTC:%d:%s] hw_done or flip_done timed out\n",
11277 crtc->base.id, crtc->name);
11278
11279 drm_crtc_commit_put(commit);
11280 }
11281
11282 return ret < 0 ? ret : 0;
11283 }
11284
get_freesync_config_for_crtc(struct dm_crtc_state * new_crtc_state,struct dm_connector_state * new_con_state)11285 static void get_freesync_config_for_crtc(
11286 struct dm_crtc_state *new_crtc_state,
11287 struct dm_connector_state *new_con_state)
11288 {
11289 struct mod_freesync_config config = {0};
11290 struct amdgpu_dm_connector *aconnector;
11291 struct drm_display_mode *mode = &new_crtc_state->base.mode;
11292 int vrefresh = drm_mode_vrefresh(mode);
11293 bool fs_vid_mode = false;
11294
11295 if (new_con_state->base.connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
11296 return;
11297
11298 aconnector = to_amdgpu_dm_connector(new_con_state->base.connector);
11299
11300 new_crtc_state->vrr_supported = new_con_state->freesync_capable &&
11301 vrefresh >= aconnector->min_vfreq &&
11302 vrefresh <= aconnector->max_vfreq;
11303
11304 if (new_crtc_state->vrr_supported) {
11305 new_crtc_state->stream->ignore_msa_timing_param = true;
11306 fs_vid_mode = new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
11307
11308 config.min_refresh_in_uhz = aconnector->min_vfreq * 1000000;
11309 config.max_refresh_in_uhz = aconnector->max_vfreq * 1000000;
11310 config.vsif_supported = true;
11311 config.btr = true;
11312
11313 if (fs_vid_mode) {
11314 config.state = VRR_STATE_ACTIVE_FIXED;
11315 config.fixed_refresh_in_uhz = new_crtc_state->freesync_config.fixed_refresh_in_uhz;
11316 goto out;
11317 } else if (new_crtc_state->base.vrr_enabled) {
11318 config.state = VRR_STATE_ACTIVE_VARIABLE;
11319 } else {
11320 config.state = VRR_STATE_INACTIVE;
11321 }
11322 } else {
11323 config.state = VRR_STATE_UNSUPPORTED;
11324 }
11325 out:
11326 new_crtc_state->freesync_config = config;
11327 }
11328
reset_freesync_config_for_crtc(struct dm_crtc_state * new_crtc_state)11329 static void reset_freesync_config_for_crtc(
11330 struct dm_crtc_state *new_crtc_state)
11331 {
11332 new_crtc_state->vrr_supported = false;
11333
11334 memset(&new_crtc_state->vrr_infopacket, 0,
11335 sizeof(new_crtc_state->vrr_infopacket));
11336 }
11337
11338 static bool
is_timing_unchanged_for_freesync(struct drm_crtc_state * old_crtc_state,struct drm_crtc_state * new_crtc_state)11339 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
11340 struct drm_crtc_state *new_crtc_state)
11341 {
11342 const struct drm_display_mode *old_mode, *new_mode;
11343
11344 if (!old_crtc_state || !new_crtc_state)
11345 return false;
11346
11347 old_mode = &old_crtc_state->mode;
11348 new_mode = &new_crtc_state->mode;
11349
11350 if (old_mode->clock == new_mode->clock &&
11351 old_mode->hdisplay == new_mode->hdisplay &&
11352 old_mode->vdisplay == new_mode->vdisplay &&
11353 old_mode->htotal == new_mode->htotal &&
11354 old_mode->vtotal != new_mode->vtotal &&
11355 old_mode->hsync_start == new_mode->hsync_start &&
11356 old_mode->vsync_start != new_mode->vsync_start &&
11357 old_mode->hsync_end == new_mode->hsync_end &&
11358 old_mode->vsync_end != new_mode->vsync_end &&
11359 old_mode->hskew == new_mode->hskew &&
11360 old_mode->vscan == new_mode->vscan &&
11361 (old_mode->vsync_end - old_mode->vsync_start) ==
11362 (new_mode->vsync_end - new_mode->vsync_start))
11363 return true;
11364
11365 return false;
11366 }
11367
set_freesync_fixed_config(struct dm_crtc_state * dm_new_crtc_state)11368 static void set_freesync_fixed_config(struct dm_crtc_state *dm_new_crtc_state)
11369 {
11370 u64 num, den, res;
11371 struct drm_crtc_state *new_crtc_state = &dm_new_crtc_state->base;
11372
11373 dm_new_crtc_state->freesync_config.state = VRR_STATE_ACTIVE_FIXED;
11374
11375 num = (unsigned long long)new_crtc_state->mode.clock * 1000 * 1000000;
11376 den = (unsigned long long)new_crtc_state->mode.htotal *
11377 (unsigned long long)new_crtc_state->mode.vtotal;
11378
11379 res = div_u64(num, den);
11380 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = res;
11381 }
11382
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)11383 static int dm_update_crtc_state(struct amdgpu_display_manager *dm,
11384 struct drm_atomic_state *state,
11385 struct drm_crtc *crtc,
11386 struct drm_crtc_state *old_crtc_state,
11387 struct drm_crtc_state *new_crtc_state,
11388 bool enable,
11389 bool *lock_and_validation_needed)
11390 {
11391 struct dm_atomic_state *dm_state = NULL;
11392 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
11393 struct dc_stream_state *new_stream;
11394 struct amdgpu_device *adev = dm->adev;
11395 int ret = 0;
11396
11397 /*
11398 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
11399 * update changed items
11400 */
11401 struct amdgpu_crtc *acrtc = NULL;
11402 struct drm_connector *connector = NULL;
11403 struct amdgpu_dm_connector *aconnector = NULL;
11404 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
11405 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
11406
11407 new_stream = NULL;
11408
11409 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
11410 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11411 acrtc = to_amdgpu_crtc(crtc);
11412 connector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
11413 if (connector)
11414 aconnector = to_amdgpu_dm_connector(connector);
11415
11416 /* TODO This hack should go away */
11417 if (connector && enable) {
11418 /* Make sure fake sink is created in plug-in scenario */
11419 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
11420 connector);
11421 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
11422 connector);
11423
11424 if (WARN_ON(!drm_new_conn_state)) {
11425 ret = -EINVAL;
11426 goto fail;
11427 }
11428
11429 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
11430 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
11431
11432 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
11433 goto skip_modeset;
11434
11435 new_stream = create_validate_stream_for_sink(connector,
11436 &new_crtc_state->mode,
11437 dm_new_conn_state,
11438 dm_old_crtc_state->stream);
11439
11440 /*
11441 * we can have no stream on ACTION_SET if a display
11442 * was disconnected during S3, in this case it is not an
11443 * error, the OS will be updated after detection, and
11444 * will do the right thing on next atomic commit
11445 */
11446
11447 if (!new_stream) {
11448 drm_dbg_driver(adev_to_drm(adev), "%s: Failed to create new stream for crtc %d\n",
11449 __func__, acrtc->base.base.id);
11450 ret = -ENOMEM;
11451 goto fail;
11452 }
11453
11454 /*
11455 * TODO: Check VSDB bits to decide whether this should
11456 * be enabled or not.
11457 */
11458 new_stream->triggered_crtc_reset.enabled =
11459 dm->force_timing_sync;
11460
11461 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
11462
11463 ret = fill_hdr_info_packet(drm_new_conn_state,
11464 &new_stream->hdr_static_metadata);
11465 if (ret)
11466 goto fail;
11467
11468 /*
11469 * If we already removed the old stream from the context
11470 * (and set the new stream to NULL) then we can't reuse
11471 * the old stream even if the stream and scaling are unchanged.
11472 * We'll hit the BUG_ON and black screen.
11473 *
11474 * TODO: Refactor this function to allow this check to work
11475 * in all conditions.
11476 */
11477 if (amdgpu_freesync_vid_mode &&
11478 dm_new_crtc_state->stream &&
11479 is_timing_unchanged_for_freesync(new_crtc_state, old_crtc_state))
11480 goto skip_modeset;
11481
11482 if (dm_new_crtc_state->stream &&
11483 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
11484 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
11485 new_crtc_state->mode_changed = false;
11486 drm_dbg_driver(adev_to_drm(adev), "Mode change not required, setting mode_changed to %d",
11487 new_crtc_state->mode_changed);
11488 }
11489 }
11490
11491 /* mode_changed flag may get updated above, need to check again */
11492 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
11493 goto skip_modeset;
11494
11495 drm_dbg_state(state->dev,
11496 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n",
11497 acrtc->crtc_id,
11498 new_crtc_state->enable,
11499 new_crtc_state->active,
11500 new_crtc_state->planes_changed,
11501 new_crtc_state->mode_changed,
11502 new_crtc_state->active_changed,
11503 new_crtc_state->connectors_changed);
11504
11505 /* Remove stream for any changed/disabled CRTC */
11506 if (!enable) {
11507
11508 if (!dm_old_crtc_state->stream)
11509 goto skip_modeset;
11510
11511 /* Unset freesync video if it was active before */
11512 if (dm_old_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED) {
11513 dm_new_crtc_state->freesync_config.state = VRR_STATE_INACTIVE;
11514 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = 0;
11515 }
11516
11517 /* Now check if we should set freesync video mode */
11518 if (amdgpu_freesync_vid_mode && dm_new_crtc_state->stream &&
11519 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
11520 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream) &&
11521 is_timing_unchanged_for_freesync(new_crtc_state,
11522 old_crtc_state)) {
11523 new_crtc_state->mode_changed = false;
11524 drm_dbg_driver(adev_to_drm(adev),
11525 "Mode change not required for front porch change, setting mode_changed to %d",
11526 new_crtc_state->mode_changed);
11527
11528 set_freesync_fixed_config(dm_new_crtc_state);
11529
11530 goto skip_modeset;
11531 } else if (amdgpu_freesync_vid_mode && aconnector &&
11532 is_freesync_video_mode(&new_crtc_state->mode,
11533 aconnector)) {
11534 struct drm_display_mode *high_mode;
11535
11536 high_mode = get_highest_refresh_rate_mode(aconnector, false);
11537 if (!drm_mode_equal(&new_crtc_state->mode, high_mode))
11538 set_freesync_fixed_config(dm_new_crtc_state);
11539 }
11540
11541 ret = dm_atomic_get_state(state, &dm_state);
11542 if (ret)
11543 goto fail;
11544
11545 drm_dbg_driver(adev_to_drm(adev), "Disabling DRM crtc: %d\n",
11546 crtc->base.id);
11547
11548 /* i.e. reset mode */
11549 if (dc_state_remove_stream(
11550 dm->dc,
11551 dm_state->context,
11552 dm_old_crtc_state->stream) != DC_OK) {
11553 ret = -EINVAL;
11554 goto fail;
11555 }
11556
11557 dc_stream_release(dm_old_crtc_state->stream);
11558 dm_new_crtc_state->stream = NULL;
11559
11560 reset_freesync_config_for_crtc(dm_new_crtc_state);
11561
11562 *lock_and_validation_needed = true;
11563
11564 } else {/* Add stream for any updated/enabled CRTC */
11565 /*
11566 * Quick fix to prevent NULL pointer on new_stream when
11567 * added MST connectors not found in existing crtc_state in the chained mode
11568 * TODO: need to dig out the root cause of that
11569 */
11570 if (!connector)
11571 goto skip_modeset;
11572
11573 if (modereset_required(new_crtc_state))
11574 goto skip_modeset;
11575
11576 if (amdgpu_dm_crtc_modeset_required(new_crtc_state, new_stream,
11577 dm_old_crtc_state->stream)) {
11578
11579 WARN_ON(dm_new_crtc_state->stream);
11580
11581 ret = dm_atomic_get_state(state, &dm_state);
11582 if (ret)
11583 goto fail;
11584
11585 dm_new_crtc_state->stream = new_stream;
11586
11587 dc_stream_retain(new_stream);
11588
11589 DRM_DEBUG_ATOMIC("Enabling DRM crtc: %d\n",
11590 crtc->base.id);
11591
11592 if (dc_state_add_stream(
11593 dm->dc,
11594 dm_state->context,
11595 dm_new_crtc_state->stream) != DC_OK) {
11596 ret = -EINVAL;
11597 goto fail;
11598 }
11599
11600 *lock_and_validation_needed = true;
11601 }
11602 }
11603
11604 skip_modeset:
11605 /* Release extra reference */
11606 if (new_stream)
11607 dc_stream_release(new_stream);
11608
11609 /*
11610 * We want to do dc stream updates that do not require a
11611 * full modeset below.
11612 */
11613 if (!(enable && connector && new_crtc_state->active))
11614 return 0;
11615 /*
11616 * Given above conditions, the dc state cannot be NULL because:
11617 * 1. We're in the process of enabling CRTCs (just been added
11618 * to the dc context, or already is on the context)
11619 * 2. Has a valid connector attached, and
11620 * 3. Is currently active and enabled.
11621 * => The dc stream state currently exists.
11622 */
11623 BUG_ON(dm_new_crtc_state->stream == NULL);
11624
11625 /* Scaling or underscan settings */
11626 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state) ||
11627 drm_atomic_crtc_needs_modeset(new_crtc_state))
11628 update_stream_scaling_settings(
11629 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
11630
11631 /* ABM settings */
11632 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
11633
11634 /*
11635 * Color management settings. We also update color properties
11636 * when a modeset is needed, to ensure it gets reprogrammed.
11637 */
11638 if (dm_new_crtc_state->base.color_mgmt_changed ||
11639 dm_old_crtc_state->regamma_tf != dm_new_crtc_state->regamma_tf ||
11640 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
11641 ret = amdgpu_dm_check_crtc_color_mgmt(dm_new_crtc_state, true);
11642 if (ret)
11643 goto fail;
11644 }
11645
11646 /* Update Freesync settings. */
11647 get_freesync_config_for_crtc(dm_new_crtc_state,
11648 dm_new_conn_state);
11649
11650 return ret;
11651
11652 fail:
11653 if (new_stream)
11654 dc_stream_release(new_stream);
11655 return ret;
11656 }
11657
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)11658 static bool should_reset_plane(struct drm_atomic_state *state,
11659 struct drm_plane *plane,
11660 struct drm_plane_state *old_plane_state,
11661 struct drm_plane_state *new_plane_state)
11662 {
11663 struct drm_plane *other;
11664 struct drm_plane_state *old_other_state, *new_other_state;
11665 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
11666 struct dm_crtc_state *old_dm_crtc_state, *new_dm_crtc_state;
11667 struct amdgpu_device *adev = drm_to_adev(plane->dev);
11668 int i;
11669
11670 /*
11671 * TODO: Remove this hack for all asics once it proves that the
11672 * fast updates works fine on DCN3.2+.
11673 */
11674 if (amdgpu_ip_version(adev, DCE_HWIP, 0) < IP_VERSION(3, 2, 0) &&
11675 state->allow_modeset)
11676 return true;
11677
11678 if (amdgpu_in_reset(adev) && state->allow_modeset)
11679 return true;
11680
11681 /* Exit early if we know that we're adding or removing the plane. */
11682 if (old_plane_state->crtc != new_plane_state->crtc)
11683 return true;
11684
11685 /* old crtc == new_crtc == NULL, plane not in context. */
11686 if (!new_plane_state->crtc)
11687 return false;
11688
11689 new_crtc_state =
11690 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
11691 old_crtc_state =
11692 drm_atomic_get_old_crtc_state(state, old_plane_state->crtc);
11693
11694 if (!new_crtc_state)
11695 return true;
11696
11697 /*
11698 * A change in cursor mode means a new dc pipe needs to be acquired or
11699 * released from the state
11700 */
11701 old_dm_crtc_state = to_dm_crtc_state(old_crtc_state);
11702 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
11703 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
11704 old_dm_crtc_state != NULL &&
11705 old_dm_crtc_state->cursor_mode != new_dm_crtc_state->cursor_mode) {
11706 return true;
11707 }
11708
11709 /* CRTC Degamma changes currently require us to recreate planes. */
11710 if (new_crtc_state->color_mgmt_changed)
11711 return true;
11712
11713 /*
11714 * On zpos change, planes need to be reordered by removing and re-adding
11715 * them one by one to the dc state, in order of descending zpos.
11716 *
11717 * TODO: We can likely skip bandwidth validation if the only thing that
11718 * changed about the plane was it'z z-ordering.
11719 */
11720 if (old_plane_state->normalized_zpos != new_plane_state->normalized_zpos)
11721 return true;
11722
11723 if (drm_atomic_crtc_needs_modeset(new_crtc_state))
11724 return true;
11725
11726 /*
11727 * If there are any new primary or overlay planes being added or
11728 * removed then the z-order can potentially change. To ensure
11729 * correct z-order and pipe acquisition the current DC architecture
11730 * requires us to remove and recreate all existing planes.
11731 *
11732 * TODO: Come up with a more elegant solution for this.
11733 */
11734 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) {
11735 struct amdgpu_framebuffer *old_afb, *new_afb;
11736 struct dm_plane_state *dm_new_other_state, *dm_old_other_state;
11737
11738 dm_new_other_state = to_dm_plane_state(new_other_state);
11739 dm_old_other_state = to_dm_plane_state(old_other_state);
11740
11741 if (other->type == DRM_PLANE_TYPE_CURSOR)
11742 continue;
11743
11744 if (old_other_state->crtc != new_plane_state->crtc &&
11745 new_other_state->crtc != new_plane_state->crtc)
11746 continue;
11747
11748 if (old_other_state->crtc != new_other_state->crtc)
11749 return true;
11750
11751 /* Src/dst size and scaling updates. */
11752 if (old_other_state->src_w != new_other_state->src_w ||
11753 old_other_state->src_h != new_other_state->src_h ||
11754 old_other_state->crtc_w != new_other_state->crtc_w ||
11755 old_other_state->crtc_h != new_other_state->crtc_h)
11756 return true;
11757
11758 /* Rotation / mirroring updates. */
11759 if (old_other_state->rotation != new_other_state->rotation)
11760 return true;
11761
11762 /* Blending updates. */
11763 if (old_other_state->pixel_blend_mode !=
11764 new_other_state->pixel_blend_mode)
11765 return true;
11766
11767 /* Alpha updates. */
11768 if (old_other_state->alpha != new_other_state->alpha)
11769 return true;
11770
11771 /* Colorspace changes. */
11772 if (old_other_state->color_range != new_other_state->color_range ||
11773 old_other_state->color_encoding != new_other_state->color_encoding)
11774 return true;
11775
11776 /* HDR/Transfer Function changes. */
11777 if (dm_old_other_state->degamma_tf != dm_new_other_state->degamma_tf ||
11778 dm_old_other_state->degamma_lut != dm_new_other_state->degamma_lut ||
11779 dm_old_other_state->hdr_mult != dm_new_other_state->hdr_mult ||
11780 dm_old_other_state->ctm != dm_new_other_state->ctm ||
11781 dm_old_other_state->shaper_lut != dm_new_other_state->shaper_lut ||
11782 dm_old_other_state->shaper_tf != dm_new_other_state->shaper_tf ||
11783 dm_old_other_state->lut3d != dm_new_other_state->lut3d ||
11784 dm_old_other_state->blend_lut != dm_new_other_state->blend_lut ||
11785 dm_old_other_state->blend_tf != dm_new_other_state->blend_tf)
11786 return true;
11787
11788 /* Framebuffer checks fall at the end. */
11789 if (!old_other_state->fb || !new_other_state->fb)
11790 continue;
11791
11792 /* Pixel format changes can require bandwidth updates. */
11793 if (old_other_state->fb->format != new_other_state->fb->format)
11794 return true;
11795
11796 old_afb = (struct amdgpu_framebuffer *)old_other_state->fb;
11797 new_afb = (struct amdgpu_framebuffer *)new_other_state->fb;
11798
11799 /* Tiling and DCC changes also require bandwidth updates. */
11800 if (old_afb->tiling_flags != new_afb->tiling_flags ||
11801 old_afb->base.modifier != new_afb->base.modifier)
11802 return true;
11803 }
11804
11805 return false;
11806 }
11807
dm_check_cursor_fb(struct amdgpu_crtc * new_acrtc,struct drm_plane_state * new_plane_state,struct drm_framebuffer * fb)11808 static int dm_check_cursor_fb(struct amdgpu_crtc *new_acrtc,
11809 struct drm_plane_state *new_plane_state,
11810 struct drm_framebuffer *fb)
11811 {
11812 struct amdgpu_device *adev = drm_to_adev(new_acrtc->base.dev);
11813 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
11814 unsigned int pitch;
11815 bool linear;
11816
11817 if (fb->width > new_acrtc->max_cursor_width ||
11818 fb->height > new_acrtc->max_cursor_height) {
11819 DRM_DEBUG_ATOMIC("Bad cursor FB size %dx%d\n",
11820 new_plane_state->fb->width,
11821 new_plane_state->fb->height);
11822 return -EINVAL;
11823 }
11824 if (new_plane_state->src_w != fb->width << 16 ||
11825 new_plane_state->src_h != fb->height << 16) {
11826 DRM_DEBUG_ATOMIC("Cropping not supported for cursor plane\n");
11827 return -EINVAL;
11828 }
11829
11830 /* Pitch in pixels */
11831 pitch = fb->pitches[0] / fb->format->cpp[0];
11832
11833 if (fb->width != pitch) {
11834 DRM_DEBUG_ATOMIC("Cursor FB width %d doesn't match pitch %d",
11835 fb->width, pitch);
11836 return -EINVAL;
11837 }
11838
11839 switch (pitch) {
11840 case 64:
11841 case 128:
11842 case 256:
11843 /* FB pitch is supported by cursor plane */
11844 break;
11845 default:
11846 DRM_DEBUG_ATOMIC("Bad cursor FB pitch %d px\n", pitch);
11847 return -EINVAL;
11848 }
11849
11850 /* Core DRM takes care of checking FB modifiers, so we only need to
11851 * check tiling flags when the FB doesn't have a modifier.
11852 */
11853 if (!(fb->flags & DRM_MODE_FB_MODIFIERS)) {
11854 if (adev->family >= AMDGPU_FAMILY_GC_12_0_0) {
11855 linear = AMDGPU_TILING_GET(afb->tiling_flags, GFX12_SWIZZLE_MODE) == 0;
11856 } else if (adev->family >= AMDGPU_FAMILY_AI) {
11857 linear = AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE) == 0;
11858 } else {
11859 linear = AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_2D_TILED_THIN1 &&
11860 AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_1D_TILED_THIN1 &&
11861 AMDGPU_TILING_GET(afb->tiling_flags, MICRO_TILE_MODE) == 0;
11862 }
11863 if (!linear) {
11864 DRM_DEBUG_ATOMIC("Cursor FB not linear");
11865 return -EINVAL;
11866 }
11867 }
11868
11869 return 0;
11870 }
11871
11872 /*
11873 * Helper function for checking the cursor in native mode
11874 */
dm_check_native_cursor_state(struct drm_crtc * new_plane_crtc,struct drm_plane * plane,struct drm_plane_state * new_plane_state,bool enable)11875 static int dm_check_native_cursor_state(struct drm_crtc *new_plane_crtc,
11876 struct drm_plane *plane,
11877 struct drm_plane_state *new_plane_state,
11878 bool enable)
11879 {
11880
11881 struct amdgpu_crtc *new_acrtc;
11882 int ret;
11883
11884 if (!enable || !new_plane_crtc ||
11885 drm_atomic_plane_disabling(plane->state, new_plane_state))
11886 return 0;
11887
11888 new_acrtc = to_amdgpu_crtc(new_plane_crtc);
11889
11890 if (new_plane_state->src_x != 0 || new_plane_state->src_y != 0) {
11891 DRM_DEBUG_ATOMIC("Cropping not supported for cursor plane\n");
11892 return -EINVAL;
11893 }
11894
11895 if (new_plane_state->fb) {
11896 ret = dm_check_cursor_fb(new_acrtc, new_plane_state,
11897 new_plane_state->fb);
11898 if (ret)
11899 return ret;
11900 }
11901
11902 return 0;
11903 }
11904
dm_should_update_native_cursor(struct drm_atomic_state * state,struct drm_crtc * old_plane_crtc,struct drm_crtc * new_plane_crtc,bool enable)11905 static bool dm_should_update_native_cursor(struct drm_atomic_state *state,
11906 struct drm_crtc *old_plane_crtc,
11907 struct drm_crtc *new_plane_crtc,
11908 bool enable)
11909 {
11910 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
11911 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
11912
11913 if (!enable) {
11914 if (old_plane_crtc == NULL)
11915 return true;
11916
11917 old_crtc_state = drm_atomic_get_old_crtc_state(
11918 state, old_plane_crtc);
11919 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
11920
11921 return dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE;
11922 } else {
11923 if (new_plane_crtc == NULL)
11924 return true;
11925
11926 new_crtc_state = drm_atomic_get_new_crtc_state(
11927 state, new_plane_crtc);
11928 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11929
11930 return dm_new_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE;
11931 }
11932 }
11933
dm_update_plane_state(struct dc * dc,struct drm_atomic_state * state,struct drm_plane * plane,struct drm_plane_state * old_plane_state,struct drm_plane_state * new_plane_state,bool enable,bool * lock_and_validation_needed,bool * is_top_most_overlay)11934 static int dm_update_plane_state(struct dc *dc,
11935 struct drm_atomic_state *state,
11936 struct drm_plane *plane,
11937 struct drm_plane_state *old_plane_state,
11938 struct drm_plane_state *new_plane_state,
11939 bool enable,
11940 bool *lock_and_validation_needed,
11941 bool *is_top_most_overlay)
11942 {
11943
11944 struct dm_atomic_state *dm_state = NULL;
11945 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
11946 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
11947 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
11948 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
11949 bool needs_reset, update_native_cursor;
11950 int ret = 0;
11951
11952
11953 new_plane_crtc = new_plane_state->crtc;
11954 old_plane_crtc = old_plane_state->crtc;
11955 dm_new_plane_state = to_dm_plane_state(new_plane_state);
11956 dm_old_plane_state = to_dm_plane_state(old_plane_state);
11957
11958 update_native_cursor = dm_should_update_native_cursor(state,
11959 old_plane_crtc,
11960 new_plane_crtc,
11961 enable);
11962
11963 if (plane->type == DRM_PLANE_TYPE_CURSOR && update_native_cursor) {
11964 ret = dm_check_native_cursor_state(new_plane_crtc, plane,
11965 new_plane_state, enable);
11966 if (ret)
11967 return ret;
11968
11969 return 0;
11970 }
11971
11972 needs_reset = should_reset_plane(state, plane, old_plane_state,
11973 new_plane_state);
11974
11975 /* Remove any changed/removed planes */
11976 if (!enable) {
11977 if (!needs_reset)
11978 return 0;
11979
11980 if (!old_plane_crtc)
11981 return 0;
11982
11983 old_crtc_state = drm_atomic_get_old_crtc_state(
11984 state, old_plane_crtc);
11985 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
11986
11987 if (!dm_old_crtc_state->stream)
11988 return 0;
11989
11990 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
11991 plane->base.id, old_plane_crtc->base.id);
11992
11993 ret = dm_atomic_get_state(state, &dm_state);
11994 if (ret)
11995 return ret;
11996
11997 if (!dc_state_remove_plane(
11998 dc,
11999 dm_old_crtc_state->stream,
12000 dm_old_plane_state->dc_state,
12001 dm_state->context)) {
12002
12003 return -EINVAL;
12004 }
12005
12006 if (dm_old_plane_state->dc_state)
12007 dc_plane_state_release(dm_old_plane_state->dc_state);
12008
12009 dm_new_plane_state->dc_state = NULL;
12010
12011 *lock_and_validation_needed = true;
12012
12013 } else { /* Add new planes */
12014 struct dc_plane_state *dc_new_plane_state;
12015
12016 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
12017 return 0;
12018
12019 if (!new_plane_crtc)
12020 return 0;
12021
12022 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
12023 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12024
12025 if (!dm_new_crtc_state->stream)
12026 return 0;
12027
12028 if (!needs_reset)
12029 return 0;
12030
12031 ret = amdgpu_dm_plane_helper_check_state(new_plane_state, new_crtc_state);
12032 if (ret)
12033 goto out;
12034
12035 WARN_ON(dm_new_plane_state->dc_state);
12036
12037 dc_new_plane_state = dc_create_plane_state(dc);
12038 if (!dc_new_plane_state) {
12039 ret = -ENOMEM;
12040 goto out;
12041 }
12042
12043 DRM_DEBUG_ATOMIC("Enabling DRM plane: %d on DRM crtc %d\n",
12044 plane->base.id, new_plane_crtc->base.id);
12045
12046 ret = fill_dc_plane_attributes(
12047 drm_to_adev(new_plane_crtc->dev),
12048 dc_new_plane_state,
12049 new_plane_state,
12050 new_crtc_state);
12051 if (ret) {
12052 dc_plane_state_release(dc_new_plane_state);
12053 goto out;
12054 }
12055
12056 ret = dm_atomic_get_state(state, &dm_state);
12057 if (ret) {
12058 dc_plane_state_release(dc_new_plane_state);
12059 goto out;
12060 }
12061
12062 /*
12063 * Any atomic check errors that occur after this will
12064 * not need a release. The plane state will be attached
12065 * to the stream, and therefore part of the atomic
12066 * state. It'll be released when the atomic state is
12067 * cleaned.
12068 */
12069 if (!dc_state_add_plane(
12070 dc,
12071 dm_new_crtc_state->stream,
12072 dc_new_plane_state,
12073 dm_state->context)) {
12074
12075 dc_plane_state_release(dc_new_plane_state);
12076 ret = -EINVAL;
12077 goto out;
12078 }
12079
12080 dm_new_plane_state->dc_state = dc_new_plane_state;
12081
12082 dm_new_crtc_state->mpo_requested |= (plane->type == DRM_PLANE_TYPE_OVERLAY);
12083
12084 /* Tell DC to do a full surface update every time there
12085 * is a plane change. Inefficient, but works for now.
12086 */
12087 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
12088
12089 *lock_and_validation_needed = true;
12090 }
12091
12092 out:
12093 /* If enabling cursor overlay failed, attempt fallback to native mode */
12094 if (enable && ret == -EINVAL && plane->type == DRM_PLANE_TYPE_CURSOR) {
12095 ret = dm_check_native_cursor_state(new_plane_crtc, plane,
12096 new_plane_state, enable);
12097 if (ret)
12098 return ret;
12099
12100 dm_new_crtc_state->cursor_mode = DM_CURSOR_NATIVE_MODE;
12101 }
12102
12103 return ret;
12104 }
12105
dm_get_oriented_plane_size(struct drm_plane_state * plane_state,int * src_w,int * src_h)12106 static void dm_get_oriented_plane_size(struct drm_plane_state *plane_state,
12107 int *src_w, int *src_h)
12108 {
12109 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
12110 case DRM_MODE_ROTATE_90:
12111 case DRM_MODE_ROTATE_270:
12112 *src_w = plane_state->src_h >> 16;
12113 *src_h = plane_state->src_w >> 16;
12114 break;
12115 case DRM_MODE_ROTATE_0:
12116 case DRM_MODE_ROTATE_180:
12117 default:
12118 *src_w = plane_state->src_w >> 16;
12119 *src_h = plane_state->src_h >> 16;
12120 break;
12121 }
12122 }
12123
12124 static void
dm_get_plane_scale(struct drm_plane_state * plane_state,int * out_plane_scale_w,int * out_plane_scale_h)12125 dm_get_plane_scale(struct drm_plane_state *plane_state,
12126 int *out_plane_scale_w, int *out_plane_scale_h)
12127 {
12128 int plane_src_w, plane_src_h;
12129
12130 dm_get_oriented_plane_size(plane_state, &plane_src_w, &plane_src_h);
12131 *out_plane_scale_w = plane_src_w ? plane_state->crtc_w * 1000 / plane_src_w : 0;
12132 *out_plane_scale_h = plane_src_h ? plane_state->crtc_h * 1000 / plane_src_h : 0;
12133 }
12134
12135 /*
12136 * The normalized_zpos value cannot be used by this iterator directly. It's only
12137 * calculated for enabled planes, potentially causing normalized_zpos collisions
12138 * between enabled/disabled planes in the atomic state. We need a unique value
12139 * so that the iterator will not generate the same object twice, or loop
12140 * indefinitely.
12141 */
__get_next_zpos(struct drm_atomic_state * state,struct __drm_planes_state * prev)12142 static inline struct __drm_planes_state *__get_next_zpos(
12143 struct drm_atomic_state *state,
12144 struct __drm_planes_state *prev)
12145 {
12146 unsigned int highest_zpos = 0, prev_zpos = 256;
12147 uint32_t highest_id = 0, prev_id = UINT_MAX;
12148 struct drm_plane_state *new_plane_state;
12149 struct drm_plane *plane;
12150 int i, highest_i = -1;
12151
12152 if (prev != NULL) {
12153 prev_zpos = prev->new_state->zpos;
12154 prev_id = prev->ptr->base.id;
12155 }
12156
12157 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
12158 /* Skip planes with higher zpos than the previously returned */
12159 if (new_plane_state->zpos > prev_zpos ||
12160 (new_plane_state->zpos == prev_zpos &&
12161 plane->base.id >= prev_id))
12162 continue;
12163
12164 /* Save the index of the plane with highest zpos */
12165 if (new_plane_state->zpos > highest_zpos ||
12166 (new_plane_state->zpos == highest_zpos &&
12167 plane->base.id > highest_id)) {
12168 highest_zpos = new_plane_state->zpos;
12169 highest_id = plane->base.id;
12170 highest_i = i;
12171 }
12172 }
12173
12174 if (highest_i < 0)
12175 return NULL;
12176
12177 return &state->planes[highest_i];
12178 }
12179
12180 /*
12181 * Use the uniqueness of the plane's (zpos, drm obj ID) combination to iterate
12182 * by descending zpos, as read from the new plane state. This is the same
12183 * ordering as defined by drm_atomic_normalize_zpos().
12184 */
12185 #define for_each_oldnew_plane_in_descending_zpos(__state, plane, old_plane_state, new_plane_state) \
12186 for (struct __drm_planes_state *__i = __get_next_zpos((__state), NULL); \
12187 __i != NULL; __i = __get_next_zpos((__state), __i)) \
12188 for_each_if(((plane) = __i->ptr, \
12189 (void)(plane) /* Only to avoid unused-but-set-variable warning */, \
12190 (old_plane_state) = __i->old_state, \
12191 (new_plane_state) = __i->new_state, 1))
12192
add_affected_mst_dsc_crtcs(struct drm_atomic_state * state,struct drm_crtc * crtc)12193 static int add_affected_mst_dsc_crtcs(struct drm_atomic_state *state, struct drm_crtc *crtc)
12194 {
12195 struct drm_connector *connector;
12196 struct drm_connector_state *conn_state, *old_conn_state;
12197 struct amdgpu_dm_connector *aconnector = NULL;
12198 int i;
12199
12200 for_each_oldnew_connector_in_state(state, connector, old_conn_state, conn_state, i) {
12201 if (!conn_state->crtc)
12202 conn_state = old_conn_state;
12203
12204 if (conn_state->crtc != crtc)
12205 continue;
12206
12207 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
12208 continue;
12209
12210 aconnector = to_amdgpu_dm_connector(connector);
12211 if (!aconnector->mst_output_port || !aconnector->mst_root)
12212 aconnector = NULL;
12213 else
12214 break;
12215 }
12216
12217 if (!aconnector)
12218 return 0;
12219
12220 return drm_dp_mst_add_affected_dsc_crtcs(state, &aconnector->mst_root->mst_mgr);
12221 }
12222
12223 /**
12224 * DOC: Cursor Modes - Native vs Overlay
12225 *
12226 * In native mode, the cursor uses a integrated cursor pipe within each DCN hw
12227 * plane. It does not require a dedicated hw plane to enable, but it is
12228 * subjected to the same z-order and scaling as the hw plane. It also has format
12229 * restrictions, a RGB cursor in native mode cannot be enabled within a non-RGB
12230 * hw plane.
12231 *
12232 * In overlay mode, the cursor uses a separate DCN hw plane, and thus has its
12233 * own scaling and z-pos. It also has no blending restrictions. It lends to a
12234 * cursor behavior more akin to a DRM client's expectations. However, it does
12235 * occupy an extra DCN plane, and therefore will only be used if a DCN plane is
12236 * available.
12237 */
12238
12239 /**
12240 * dm_crtc_get_cursor_mode() - Determine the required cursor mode on crtc
12241 * @adev: amdgpu device
12242 * @state: DRM atomic state
12243 * @dm_crtc_state: amdgpu state for the CRTC containing the cursor
12244 * @cursor_mode: Returns the required cursor mode on dm_crtc_state
12245 *
12246 * Get whether the cursor should be enabled in native mode, or overlay mode, on
12247 * the dm_crtc_state.
12248 *
12249 * The cursor should be enabled in overlay mode if there exists an underlying
12250 * plane - on which the cursor may be blended - that is either YUV formatted, or
12251 * scaled differently from the cursor.
12252 *
12253 * Since zpos info is required, drm_atomic_normalize_zpos must be called before
12254 * calling this function.
12255 *
12256 * Return: 0 on success, or an error code if getting the cursor plane state
12257 * failed.
12258 */
dm_crtc_get_cursor_mode(struct amdgpu_device * adev,struct drm_atomic_state * state,struct dm_crtc_state * dm_crtc_state,enum amdgpu_dm_cursor_mode * cursor_mode)12259 static int dm_crtc_get_cursor_mode(struct amdgpu_device *adev,
12260 struct drm_atomic_state *state,
12261 struct dm_crtc_state *dm_crtc_state,
12262 enum amdgpu_dm_cursor_mode *cursor_mode)
12263 {
12264 struct drm_plane_state *old_plane_state, *plane_state, *cursor_state;
12265 struct drm_crtc_state *crtc_state = &dm_crtc_state->base;
12266 struct drm_plane *plane;
12267 bool consider_mode_change = false;
12268 bool entire_crtc_covered = false;
12269 bool cursor_changed = false;
12270 int underlying_scale_w, underlying_scale_h;
12271 int cursor_scale_w, cursor_scale_h;
12272 int i;
12273
12274 /* Overlay cursor not supported on HW before DCN
12275 * DCN401 does not have the cursor-on-scaled-plane or cursor-on-yuv-plane restrictions
12276 * as previous DCN generations, so enable native mode on DCN401 in addition to DCE
12277 */
12278 if (amdgpu_ip_version(adev, DCE_HWIP, 0) == 0 ||
12279 amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 0, 1)) {
12280 *cursor_mode = DM_CURSOR_NATIVE_MODE;
12281 return 0;
12282 }
12283
12284 /* Init cursor_mode to be the same as current */
12285 *cursor_mode = dm_crtc_state->cursor_mode;
12286
12287 /*
12288 * Cursor mode can change if a plane's format changes, scale changes, is
12289 * enabled/disabled, or z-order changes.
12290 */
12291 for_each_oldnew_plane_in_state(state, plane, old_plane_state, plane_state, i) {
12292 int new_scale_w, new_scale_h, old_scale_w, old_scale_h;
12293
12294 /* Only care about planes on this CRTC */
12295 if ((drm_plane_mask(plane) & crtc_state->plane_mask) == 0)
12296 continue;
12297
12298 if (plane->type == DRM_PLANE_TYPE_CURSOR)
12299 cursor_changed = true;
12300
12301 if (drm_atomic_plane_enabling(old_plane_state, plane_state) ||
12302 drm_atomic_plane_disabling(old_plane_state, plane_state) ||
12303 old_plane_state->fb->format != plane_state->fb->format) {
12304 consider_mode_change = true;
12305 break;
12306 }
12307
12308 dm_get_plane_scale(plane_state, &new_scale_w, &new_scale_h);
12309 dm_get_plane_scale(old_plane_state, &old_scale_w, &old_scale_h);
12310 if (new_scale_w != old_scale_w || new_scale_h != old_scale_h) {
12311 consider_mode_change = true;
12312 break;
12313 }
12314 }
12315
12316 if (!consider_mode_change && !crtc_state->zpos_changed)
12317 return 0;
12318
12319 /*
12320 * If no cursor change on this CRTC, and not enabled on this CRTC, then
12321 * no need to set cursor mode. This avoids needlessly locking the cursor
12322 * state.
12323 */
12324 if (!cursor_changed &&
12325 !(drm_plane_mask(crtc_state->crtc->cursor) & crtc_state->plane_mask)) {
12326 return 0;
12327 }
12328
12329 cursor_state = drm_atomic_get_plane_state(state,
12330 crtc_state->crtc->cursor);
12331 if (IS_ERR(cursor_state))
12332 return PTR_ERR(cursor_state);
12333
12334 /* Cursor is disabled */
12335 if (!cursor_state->fb)
12336 return 0;
12337
12338 /* For all planes in descending z-order (all of which are below cursor
12339 * as per zpos definitions), check their scaling and format
12340 */
12341 for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, plane_state) {
12342
12343 /* Only care about non-cursor planes on this CRTC */
12344 if ((drm_plane_mask(plane) & crtc_state->plane_mask) == 0 ||
12345 plane->type == DRM_PLANE_TYPE_CURSOR)
12346 continue;
12347
12348 /* Underlying plane is YUV format - use overlay cursor */
12349 if (amdgpu_dm_plane_is_video_format(plane_state->fb->format->format)) {
12350 *cursor_mode = DM_CURSOR_OVERLAY_MODE;
12351 return 0;
12352 }
12353
12354 dm_get_plane_scale(plane_state,
12355 &underlying_scale_w, &underlying_scale_h);
12356 dm_get_plane_scale(cursor_state,
12357 &cursor_scale_w, &cursor_scale_h);
12358
12359 /* Underlying plane has different scale - use overlay cursor */
12360 if (cursor_scale_w != underlying_scale_w &&
12361 cursor_scale_h != underlying_scale_h) {
12362 *cursor_mode = DM_CURSOR_OVERLAY_MODE;
12363 return 0;
12364 }
12365
12366 /* If this plane covers the whole CRTC, no need to check planes underneath */
12367 if (plane_state->crtc_x <= 0 && plane_state->crtc_y <= 0 &&
12368 plane_state->crtc_x + plane_state->crtc_w >= crtc_state->mode.hdisplay &&
12369 plane_state->crtc_y + plane_state->crtc_h >= crtc_state->mode.vdisplay) {
12370 entire_crtc_covered = true;
12371 break;
12372 }
12373 }
12374
12375 /* If planes do not cover the entire CRTC, use overlay mode to enable
12376 * cursor over holes
12377 */
12378 if (entire_crtc_covered)
12379 *cursor_mode = DM_CURSOR_NATIVE_MODE;
12380 else
12381 *cursor_mode = DM_CURSOR_OVERLAY_MODE;
12382
12383 return 0;
12384 }
12385
amdgpu_dm_crtc_mem_type_changed(struct drm_device * dev,struct drm_atomic_state * state,struct drm_crtc_state * crtc_state)12386 static bool amdgpu_dm_crtc_mem_type_changed(struct drm_device *dev,
12387 struct drm_atomic_state *state,
12388 struct drm_crtc_state *crtc_state)
12389 {
12390 struct drm_plane *plane;
12391 struct drm_plane_state *new_plane_state, *old_plane_state;
12392
12393 drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
12394 new_plane_state = drm_atomic_get_plane_state(state, plane);
12395 old_plane_state = drm_atomic_get_plane_state(state, plane);
12396
12397 if (IS_ERR(new_plane_state) || IS_ERR(old_plane_state)) {
12398 drm_err(dev, "Failed to get plane state for plane %s\n", plane->name);
12399 return false;
12400 }
12401
12402 if (old_plane_state->fb && new_plane_state->fb &&
12403 get_mem_type(old_plane_state->fb) != get_mem_type(new_plane_state->fb))
12404 return true;
12405 }
12406
12407 return false;
12408 }
12409
12410 /**
12411 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM.
12412 *
12413 * @dev: The DRM device
12414 * @state: The atomic state to commit
12415 *
12416 * Validate that the given atomic state is programmable by DC into hardware.
12417 * This involves constructing a &struct dc_state reflecting the new hardware
12418 * state we wish to commit, then querying DC to see if it is programmable. It's
12419 * important not to modify the existing DC state. Otherwise, atomic_check
12420 * may unexpectedly commit hardware changes.
12421 *
12422 * When validating the DC state, it's important that the right locks are
12423 * acquired. For full updates case which removes/adds/updates streams on one
12424 * CRTC while flipping on another CRTC, acquiring global lock will guarantee
12425 * that any such full update commit will wait for completion of any outstanding
12426 * flip using DRMs synchronization events.
12427 *
12428 * Note that DM adds the affected connectors for all CRTCs in state, when that
12429 * might not seem necessary. This is because DC stream creation requires the
12430 * DC sink, which is tied to the DRM connector state. Cleaning this up should
12431 * be possible but non-trivial - a possible TODO item.
12432 *
12433 * Return: -Error code if validation failed.
12434 */
amdgpu_dm_atomic_check(struct drm_device * dev,struct drm_atomic_state * state)12435 static int amdgpu_dm_atomic_check(struct drm_device *dev,
12436 struct drm_atomic_state *state)
12437 {
12438 struct amdgpu_device *adev = drm_to_adev(dev);
12439 struct dm_atomic_state *dm_state = NULL;
12440 struct dc *dc = adev->dm.dc;
12441 struct drm_connector *connector;
12442 struct drm_connector_state *old_con_state, *new_con_state;
12443 struct drm_crtc *crtc;
12444 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
12445 struct drm_plane *plane;
12446 struct drm_plane_state *old_plane_state, *new_plane_state, *new_cursor_state;
12447 enum dc_status status;
12448 int ret, i;
12449 bool lock_and_validation_needed = false;
12450 bool is_top_most_overlay = true;
12451 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
12452 struct drm_dp_mst_topology_mgr *mgr;
12453 struct drm_dp_mst_topology_state *mst_state;
12454 struct dsc_mst_fairness_vars vars[MAX_PIPES] = {0};
12455
12456 trace_amdgpu_dm_atomic_check_begin(state);
12457
12458 ret = drm_atomic_helper_check_modeset(dev, state);
12459 if (ret) {
12460 drm_dbg_atomic(dev, "drm_atomic_helper_check_modeset() failed\n");
12461 goto fail;
12462 }
12463
12464 /* Check connector changes */
12465 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
12466 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
12467 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
12468
12469 /* Skip connectors that are disabled or part of modeset already. */
12470 if (!new_con_state->crtc)
12471 continue;
12472
12473 new_crtc_state = drm_atomic_get_crtc_state(state, new_con_state->crtc);
12474 if (IS_ERR(new_crtc_state)) {
12475 drm_dbg_atomic(dev, "drm_atomic_get_crtc_state() failed\n");
12476 ret = PTR_ERR(new_crtc_state);
12477 goto fail;
12478 }
12479
12480 if (dm_old_con_state->abm_level != dm_new_con_state->abm_level ||
12481 dm_old_con_state->scaling != dm_new_con_state->scaling)
12482 new_crtc_state->connectors_changed = true;
12483 }
12484
12485 if (dc_resource_is_dsc_encoding_supported(dc)) {
12486 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12487 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
12488 ret = add_affected_mst_dsc_crtcs(state, crtc);
12489 if (ret) {
12490 drm_dbg_atomic(dev, "add_affected_mst_dsc_crtcs() failed\n");
12491 goto fail;
12492 }
12493 }
12494 }
12495 }
12496 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12497 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
12498
12499 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
12500 !new_crtc_state->color_mgmt_changed &&
12501 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled &&
12502 dm_old_crtc_state->dsc_force_changed == false)
12503 continue;
12504
12505 ret = amdgpu_dm_verify_lut_sizes(new_crtc_state);
12506 if (ret) {
12507 drm_dbg_atomic(dev, "amdgpu_dm_verify_lut_sizes() failed\n");
12508 goto fail;
12509 }
12510
12511 if (!new_crtc_state->enable)
12512 continue;
12513
12514 ret = drm_atomic_add_affected_connectors(state, crtc);
12515 if (ret) {
12516 drm_dbg_atomic(dev, "drm_atomic_add_affected_connectors() failed\n");
12517 goto fail;
12518 }
12519
12520 ret = drm_atomic_add_affected_planes(state, crtc);
12521 if (ret) {
12522 drm_dbg_atomic(dev, "drm_atomic_add_affected_planes() failed\n");
12523 goto fail;
12524 }
12525
12526 if (dm_old_crtc_state->dsc_force_changed)
12527 new_crtc_state->mode_changed = true;
12528 }
12529
12530 /*
12531 * Add all primary and overlay planes on the CRTC to the state
12532 * whenever a plane is enabled to maintain correct z-ordering
12533 * and to enable fast surface updates.
12534 */
12535 drm_for_each_crtc(crtc, dev) {
12536 bool modified = false;
12537
12538 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
12539 if (plane->type == DRM_PLANE_TYPE_CURSOR)
12540 continue;
12541
12542 if (new_plane_state->crtc == crtc ||
12543 old_plane_state->crtc == crtc) {
12544 modified = true;
12545 break;
12546 }
12547 }
12548
12549 if (!modified)
12550 continue;
12551
12552 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
12553 if (plane->type == DRM_PLANE_TYPE_CURSOR)
12554 continue;
12555
12556 new_plane_state =
12557 drm_atomic_get_plane_state(state, plane);
12558
12559 if (IS_ERR(new_plane_state)) {
12560 ret = PTR_ERR(new_plane_state);
12561 drm_dbg_atomic(dev, "new_plane_state is BAD\n");
12562 goto fail;
12563 }
12564 }
12565 }
12566
12567 /*
12568 * DC consults the zpos (layer_index in DC terminology) to determine the
12569 * hw plane on which to enable the hw cursor (see
12570 * `dcn10_can_pipe_disable_cursor`). By now, all modified planes are in
12571 * atomic state, so call drm helper to normalize zpos.
12572 */
12573 ret = drm_atomic_normalize_zpos(dev, state);
12574 if (ret) {
12575 drm_dbg(dev, "drm_atomic_normalize_zpos() failed\n");
12576 goto fail;
12577 }
12578
12579 /*
12580 * Determine whether cursors on each CRTC should be enabled in native or
12581 * overlay mode.
12582 */
12583 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12584 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12585
12586 ret = dm_crtc_get_cursor_mode(adev, state, dm_new_crtc_state,
12587 &dm_new_crtc_state->cursor_mode);
12588 if (ret) {
12589 drm_dbg(dev, "Failed to determine cursor mode\n");
12590 goto fail;
12591 }
12592
12593 /*
12594 * If overlay cursor is needed, DC cannot go through the
12595 * native cursor update path. All enabled planes on the CRTC
12596 * need to be added for DC to not disable a plane by mistake
12597 */
12598 if (dm_new_crtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE) {
12599 ret = drm_atomic_add_affected_planes(state, crtc);
12600 if (ret)
12601 goto fail;
12602 }
12603 }
12604
12605 /* Remove exiting planes if they are modified */
12606 for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, new_plane_state) {
12607
12608 ret = dm_update_plane_state(dc, state, plane,
12609 old_plane_state,
12610 new_plane_state,
12611 false,
12612 &lock_and_validation_needed,
12613 &is_top_most_overlay);
12614 if (ret) {
12615 drm_dbg_atomic(dev, "dm_update_plane_state() failed\n");
12616 goto fail;
12617 }
12618 }
12619
12620 /* Disable all crtcs which require disable */
12621 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12622 ret = dm_update_crtc_state(&adev->dm, state, crtc,
12623 old_crtc_state,
12624 new_crtc_state,
12625 false,
12626 &lock_and_validation_needed);
12627 if (ret) {
12628 drm_dbg_atomic(dev, "DISABLE: dm_update_crtc_state() failed\n");
12629 goto fail;
12630 }
12631 }
12632
12633 /* Enable all crtcs which require enable */
12634 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12635 ret = dm_update_crtc_state(&adev->dm, state, crtc,
12636 old_crtc_state,
12637 new_crtc_state,
12638 true,
12639 &lock_and_validation_needed);
12640 if (ret) {
12641 drm_dbg_atomic(dev, "ENABLE: dm_update_crtc_state() failed\n");
12642 goto fail;
12643 }
12644 }
12645
12646 /* Add new/modified planes */
12647 for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, new_plane_state) {
12648 ret = dm_update_plane_state(dc, state, plane,
12649 old_plane_state,
12650 new_plane_state,
12651 true,
12652 &lock_and_validation_needed,
12653 &is_top_most_overlay);
12654 if (ret) {
12655 drm_dbg_atomic(dev, "dm_update_plane_state() failed\n");
12656 goto fail;
12657 }
12658 }
12659
12660 #if defined(CONFIG_DRM_AMD_DC_FP)
12661 if (dc_resource_is_dsc_encoding_supported(dc)) {
12662 ret = pre_validate_dsc(state, &dm_state, vars);
12663 if (ret != 0)
12664 goto fail;
12665 }
12666 #endif
12667
12668 /* Run this here since we want to validate the streams we created */
12669 ret = drm_atomic_helper_check_planes(dev, state);
12670 if (ret) {
12671 drm_dbg_atomic(dev, "drm_atomic_helper_check_planes() failed\n");
12672 goto fail;
12673 }
12674
12675 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12676 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12677 if (dm_new_crtc_state->mpo_requested)
12678 drm_dbg_atomic(dev, "MPO enablement requested on crtc:[%p]\n", crtc);
12679 }
12680
12681 /* Check cursor restrictions */
12682 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12683 enum amdgpu_dm_cursor_mode required_cursor_mode;
12684 int is_rotated, is_scaled;
12685
12686 /* Overlay cusor not subject to native cursor restrictions */
12687 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
12688 if (dm_new_crtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE)
12689 continue;
12690
12691 /* Check if rotation or scaling is enabled on DCN401 */
12692 if ((drm_plane_mask(crtc->cursor) & new_crtc_state->plane_mask) &&
12693 amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 0, 1)) {
12694 new_cursor_state = drm_atomic_get_new_plane_state(state, crtc->cursor);
12695
12696 is_rotated = new_cursor_state &&
12697 ((new_cursor_state->rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0);
12698 is_scaled = new_cursor_state && ((new_cursor_state->src_w >> 16 != new_cursor_state->crtc_w) ||
12699 (new_cursor_state->src_h >> 16 != new_cursor_state->crtc_h));
12700
12701 if (is_rotated || is_scaled) {
12702 drm_dbg_driver(
12703 crtc->dev,
12704 "[CRTC:%d:%s] cannot enable hardware cursor due to rotation/scaling\n",
12705 crtc->base.id, crtc->name);
12706 ret = -EINVAL;
12707 goto fail;
12708 }
12709 }
12710
12711 /* If HW can only do native cursor, check restrictions again */
12712 ret = dm_crtc_get_cursor_mode(adev, state, dm_new_crtc_state,
12713 &required_cursor_mode);
12714 if (ret) {
12715 drm_dbg_driver(crtc->dev,
12716 "[CRTC:%d:%s] Checking cursor mode failed\n",
12717 crtc->base.id, crtc->name);
12718 goto fail;
12719 } else if (required_cursor_mode == DM_CURSOR_OVERLAY_MODE) {
12720 drm_dbg_driver(crtc->dev,
12721 "[CRTC:%d:%s] Cannot enable native cursor due to scaling or YUV restrictions\n",
12722 crtc->base.id, crtc->name);
12723 ret = -EINVAL;
12724 goto fail;
12725 }
12726 }
12727
12728 if (state->legacy_cursor_update) {
12729 /*
12730 * This is a fast cursor update coming from the plane update
12731 * helper, check if it can be done asynchronously for better
12732 * performance.
12733 */
12734 state->async_update =
12735 !drm_atomic_helper_async_check(dev, state);
12736
12737 /*
12738 * Skip the remaining global validation if this is an async
12739 * update. Cursor updates can be done without affecting
12740 * state or bandwidth calcs and this avoids the performance
12741 * penalty of locking the private state object and
12742 * allocating a new dc_state.
12743 */
12744 if (state->async_update)
12745 return 0;
12746 }
12747
12748 /* Check scaling and underscan changes*/
12749 /* TODO Removed scaling changes validation due to inability to commit
12750 * new stream into context w\o causing full reset. Need to
12751 * decide how to handle.
12752 */
12753 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
12754 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
12755 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
12756 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
12757
12758 /* Skip any modesets/resets */
12759 if (!acrtc || drm_atomic_crtc_needs_modeset(
12760 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
12761 continue;
12762
12763 /* Skip any thing not scale or underscan changes */
12764 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
12765 continue;
12766
12767 lock_and_validation_needed = true;
12768 }
12769
12770 /* set the slot info for each mst_state based on the link encoding format */
12771 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
12772 struct amdgpu_dm_connector *aconnector;
12773 struct drm_connector *connector;
12774 struct drm_connector_list_iter iter;
12775 u8 link_coding_cap;
12776
12777 drm_connector_list_iter_begin(dev, &iter);
12778 drm_for_each_connector_iter(connector, &iter) {
12779 if (connector->index == mst_state->mgr->conn_base_id) {
12780 aconnector = to_amdgpu_dm_connector(connector);
12781 link_coding_cap = dc_link_dp_mst_decide_link_encoding_format(aconnector->dc_link);
12782 drm_dp_mst_update_slots(mst_state, link_coding_cap);
12783
12784 break;
12785 }
12786 }
12787 drm_connector_list_iter_end(&iter);
12788 }
12789
12790 /**
12791 * Streams and planes are reset when there are changes that affect
12792 * bandwidth. Anything that affects bandwidth needs to go through
12793 * DC global validation to ensure that the configuration can be applied
12794 * to hardware.
12795 *
12796 * We have to currently stall out here in atomic_check for outstanding
12797 * commits to finish in this case because our IRQ handlers reference
12798 * DRM state directly - we can end up disabling interrupts too early
12799 * if we don't.
12800 *
12801 * TODO: Remove this stall and drop DM state private objects.
12802 */
12803 if (lock_and_validation_needed) {
12804 ret = dm_atomic_get_state(state, &dm_state);
12805 if (ret) {
12806 drm_dbg_atomic(dev, "dm_atomic_get_state() failed\n");
12807 goto fail;
12808 }
12809
12810 ret = do_aquire_global_lock(dev, state);
12811 if (ret) {
12812 drm_dbg_atomic(dev, "do_aquire_global_lock() failed\n");
12813 goto fail;
12814 }
12815
12816 #if defined(CONFIG_DRM_AMD_DC_FP)
12817 if (dc_resource_is_dsc_encoding_supported(dc)) {
12818 ret = compute_mst_dsc_configs_for_state(state, dm_state->context, vars);
12819 if (ret) {
12820 drm_dbg_atomic(dev, "MST_DSC compute_mst_dsc_configs_for_state() failed\n");
12821 ret = -EINVAL;
12822 goto fail;
12823 }
12824 }
12825 #endif
12826
12827 ret = dm_update_mst_vcpi_slots_for_dsc(state, dm_state->context, vars);
12828 if (ret) {
12829 drm_dbg_atomic(dev, "dm_update_mst_vcpi_slots_for_dsc() failed\n");
12830 goto fail;
12831 }
12832
12833 /*
12834 * Perform validation of MST topology in the state:
12835 * We need to perform MST atomic check before calling
12836 * dc_validate_global_state(), or there is a chance
12837 * to get stuck in an infinite loop and hang eventually.
12838 */
12839 ret = drm_dp_mst_atomic_check(state);
12840 if (ret) {
12841 drm_dbg_atomic(dev, "MST drm_dp_mst_atomic_check() failed\n");
12842 goto fail;
12843 }
12844 status = dc_validate_global_state(dc, dm_state->context, DC_VALIDATE_MODE_ONLY);
12845 if (status != DC_OK) {
12846 drm_dbg_atomic(dev, "DC global validation failure: %s (%d)",
12847 dc_status_to_str(status), status);
12848 ret = -EINVAL;
12849 goto fail;
12850 }
12851 } else {
12852 /*
12853 * The commit is a fast update. Fast updates shouldn't change
12854 * the DC context, affect global validation, and can have their
12855 * commit work done in parallel with other commits not touching
12856 * the same resource. If we have a new DC context as part of
12857 * the DM atomic state from validation we need to free it and
12858 * retain the existing one instead.
12859 *
12860 * Furthermore, since the DM atomic state only contains the DC
12861 * context and can safely be annulled, we can free the state
12862 * and clear the associated private object now to free
12863 * some memory and avoid a possible use-after-free later.
12864 */
12865
12866 for (i = 0; i < state->num_private_objs; i++) {
12867 struct drm_private_obj *obj = state->private_objs[i].ptr;
12868
12869 if (obj->funcs == adev->dm.atomic_obj.funcs) {
12870 int j = state->num_private_objs-1;
12871
12872 dm_atomic_destroy_state(obj,
12873 state->private_objs[i].state_to_destroy);
12874
12875 /* If i is not at the end of the array then the
12876 * last element needs to be moved to where i was
12877 * before the array can safely be truncated.
12878 */
12879 if (i != j)
12880 state->private_objs[i] =
12881 state->private_objs[j];
12882
12883 state->private_objs[j].ptr = NULL;
12884 state->private_objs[j].state_to_destroy = NULL;
12885 state->private_objs[j].old_state = NULL;
12886 state->private_objs[j].new_state = NULL;
12887
12888 state->num_private_objs = j;
12889 break;
12890 }
12891 }
12892 }
12893
12894 /* Store the overall update type for use later in atomic check. */
12895 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12896 struct dm_crtc_state *dm_new_crtc_state =
12897 to_dm_crtc_state(new_crtc_state);
12898
12899 /*
12900 * Only allow async flips for fast updates that don't change
12901 * the FB pitch, the DCC state, rotation, mem_type, etc.
12902 */
12903 if (new_crtc_state->async_flip &&
12904 (lock_and_validation_needed ||
12905 amdgpu_dm_crtc_mem_type_changed(dev, state, new_crtc_state))) {
12906 drm_dbg_atomic(crtc->dev,
12907 "[CRTC:%d:%s] async flips are only supported for fast updates\n",
12908 crtc->base.id, crtc->name);
12909 ret = -EINVAL;
12910 goto fail;
12911 }
12912
12913 dm_new_crtc_state->update_type = lock_and_validation_needed ?
12914 UPDATE_TYPE_FULL : UPDATE_TYPE_FAST;
12915 }
12916
12917 /* Must be success */
12918 WARN_ON(ret);
12919
12920 trace_amdgpu_dm_atomic_check_finish(state, ret);
12921
12922 return ret;
12923
12924 fail:
12925 if (ret == -EDEADLK)
12926 drm_dbg_atomic(dev, "Atomic check stopped to avoid deadlock.\n");
12927 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
12928 drm_dbg_atomic(dev, "Atomic check stopped due to signal.\n");
12929 else
12930 drm_dbg_atomic(dev, "Atomic check failed with err: %d\n", ret);
12931
12932 trace_amdgpu_dm_atomic_check_finish(state, ret);
12933
12934 return ret;
12935 }
12936
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)12937 static bool dm_edid_parser_send_cea(struct amdgpu_display_manager *dm,
12938 unsigned int offset,
12939 unsigned int total_length,
12940 u8 *data,
12941 unsigned int length,
12942 struct amdgpu_hdmi_vsdb_info *vsdb)
12943 {
12944 bool res;
12945 union dmub_rb_cmd cmd;
12946 struct dmub_cmd_send_edid_cea *input;
12947 struct dmub_cmd_edid_cea_output *output;
12948
12949 if (length > DMUB_EDID_CEA_DATA_CHUNK_BYTES)
12950 return false;
12951
12952 memset(&cmd, 0, sizeof(cmd));
12953
12954 input = &cmd.edid_cea.data.input;
12955
12956 cmd.edid_cea.header.type = DMUB_CMD__EDID_CEA;
12957 cmd.edid_cea.header.sub_type = 0;
12958 cmd.edid_cea.header.payload_bytes =
12959 sizeof(cmd.edid_cea) - sizeof(cmd.edid_cea.header);
12960 input->offset = offset;
12961 input->length = length;
12962 input->cea_total_length = total_length;
12963 memcpy(input->payload, data, length);
12964
12965 res = dc_wake_and_execute_dmub_cmd(dm->dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY);
12966 if (!res) {
12967 drm_err(adev_to_drm(dm->adev), "EDID CEA parser failed\n");
12968 return false;
12969 }
12970
12971 output = &cmd.edid_cea.data.output;
12972
12973 if (output->type == DMUB_CMD__EDID_CEA_ACK) {
12974 if (!output->ack.success) {
12975 drm_err(adev_to_drm(dm->adev), "EDID CEA ack failed at offset %d\n",
12976 output->ack.offset);
12977 }
12978 } else if (output->type == DMUB_CMD__EDID_CEA_AMD_VSDB) {
12979 if (!output->amd_vsdb.vsdb_found)
12980 return false;
12981
12982 vsdb->freesync_supported = output->amd_vsdb.freesync_supported;
12983 vsdb->amd_vsdb_version = output->amd_vsdb.amd_vsdb_version;
12984 vsdb->min_refresh_rate_hz = output->amd_vsdb.min_frame_rate;
12985 vsdb->max_refresh_rate_hz = output->amd_vsdb.max_frame_rate;
12986 } else {
12987 drm_warn(adev_to_drm(dm->adev), "Unknown EDID CEA parser results\n");
12988 return false;
12989 }
12990
12991 return true;
12992 }
12993
parse_edid_cea_dmcu(struct amdgpu_display_manager * dm,u8 * edid_ext,int len,struct amdgpu_hdmi_vsdb_info * vsdb_info)12994 static bool parse_edid_cea_dmcu(struct amdgpu_display_manager *dm,
12995 u8 *edid_ext, int len,
12996 struct amdgpu_hdmi_vsdb_info *vsdb_info)
12997 {
12998 int i;
12999
13000 /* send extension block to DMCU for parsing */
13001 for (i = 0; i < len; i += 8) {
13002 bool res;
13003 int offset;
13004
13005 /* send 8 bytes a time */
13006 if (!dc_edid_parser_send_cea(dm->dc, i, len, &edid_ext[i], 8))
13007 return false;
13008
13009 if (i+8 == len) {
13010 /* EDID block sent completed, expect result */
13011 int version, min_rate, max_rate;
13012
13013 res = dc_edid_parser_recv_amd_vsdb(dm->dc, &version, &min_rate, &max_rate);
13014 if (res) {
13015 /* amd vsdb found */
13016 vsdb_info->freesync_supported = 1;
13017 vsdb_info->amd_vsdb_version = version;
13018 vsdb_info->min_refresh_rate_hz = min_rate;
13019 vsdb_info->max_refresh_rate_hz = max_rate;
13020 return true;
13021 }
13022 /* not amd vsdb */
13023 return false;
13024 }
13025
13026 /* check for ack*/
13027 res = dc_edid_parser_recv_cea_ack(dm->dc, &offset);
13028 if (!res)
13029 return false;
13030 }
13031
13032 return false;
13033 }
13034
parse_edid_cea_dmub(struct amdgpu_display_manager * dm,u8 * edid_ext,int len,struct amdgpu_hdmi_vsdb_info * vsdb_info)13035 static bool parse_edid_cea_dmub(struct amdgpu_display_manager *dm,
13036 u8 *edid_ext, int len,
13037 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13038 {
13039 int i;
13040
13041 /* send extension block to DMCU for parsing */
13042 for (i = 0; i < len; i += 8) {
13043 /* send 8 bytes a time */
13044 if (!dm_edid_parser_send_cea(dm, i, len, &edid_ext[i], 8, vsdb_info))
13045 return false;
13046 }
13047
13048 return vsdb_info->freesync_supported;
13049 }
13050
parse_edid_cea(struct amdgpu_dm_connector * aconnector,u8 * edid_ext,int len,struct amdgpu_hdmi_vsdb_info * vsdb_info)13051 static bool parse_edid_cea(struct amdgpu_dm_connector *aconnector,
13052 u8 *edid_ext, int len,
13053 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13054 {
13055 struct amdgpu_device *adev = drm_to_adev(aconnector->base.dev);
13056 bool ret;
13057
13058 mutex_lock(&adev->dm.dc_lock);
13059 if (adev->dm.dmub_srv)
13060 ret = parse_edid_cea_dmub(&adev->dm, edid_ext, len, vsdb_info);
13061 else
13062 ret = parse_edid_cea_dmcu(&adev->dm, edid_ext, len, vsdb_info);
13063 mutex_unlock(&adev->dm.dc_lock);
13064 return ret;
13065 }
13066
parse_edid_displayid_vrr(struct drm_connector * connector,const struct edid * edid)13067 static void parse_edid_displayid_vrr(struct drm_connector *connector,
13068 const struct edid *edid)
13069 {
13070 u8 *edid_ext = NULL;
13071 int i;
13072 int j = 0;
13073 u16 min_vfreq;
13074 u16 max_vfreq;
13075
13076 if (edid == NULL || edid->extensions == 0)
13077 return;
13078
13079 /* Find DisplayID extension */
13080 for (i = 0; i < edid->extensions; i++) {
13081 edid_ext = (void *)(edid + (i + 1));
13082 if (edid_ext[0] == DISPLAYID_EXT)
13083 break;
13084 }
13085
13086 if (edid_ext == NULL)
13087 return;
13088
13089 while (j < EDID_LENGTH) {
13090 /* Get dynamic video timing range from DisplayID if available */
13091 if (EDID_LENGTH - j > 13 && edid_ext[j] == 0x25 &&
13092 (edid_ext[j+1] & 0xFE) == 0 && (edid_ext[j+2] == 9)) {
13093 min_vfreq = edid_ext[j+9];
13094 if (edid_ext[j+1] & 7)
13095 max_vfreq = edid_ext[j+10] + ((edid_ext[j+11] & 3) << 8);
13096 else
13097 max_vfreq = edid_ext[j+10];
13098
13099 if (max_vfreq && min_vfreq) {
13100 connector->display_info.monitor_range.max_vfreq = max_vfreq;
13101 connector->display_info.monitor_range.min_vfreq = min_vfreq;
13102
13103 return;
13104 }
13105 }
13106 j++;
13107 }
13108 }
13109
parse_amd_vsdb(struct amdgpu_dm_connector * aconnector,const struct edid * edid,struct amdgpu_hdmi_vsdb_info * vsdb_info)13110 static int parse_amd_vsdb(struct amdgpu_dm_connector *aconnector,
13111 const struct edid *edid, struct amdgpu_hdmi_vsdb_info *vsdb_info)
13112 {
13113 u8 *edid_ext = NULL;
13114 int i;
13115 int j = 0;
13116
13117 if (edid == NULL || edid->extensions == 0)
13118 return -ENODEV;
13119
13120 /* Find DisplayID extension */
13121 for (i = 0; i < edid->extensions; i++) {
13122 edid_ext = (void *)(edid + (i + 1));
13123 if (edid_ext[0] == DISPLAYID_EXT)
13124 break;
13125 }
13126
13127 while (j < EDID_LENGTH - sizeof(struct amd_vsdb_block)) {
13128 struct amd_vsdb_block *amd_vsdb = (struct amd_vsdb_block *)&edid_ext[j];
13129 unsigned int ieeeId = (amd_vsdb->ieee_id[2] << 16) | (amd_vsdb->ieee_id[1] << 8) | (amd_vsdb->ieee_id[0]);
13130
13131 if (ieeeId == HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_IEEE_REGISTRATION_ID &&
13132 amd_vsdb->version == HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_VERSION_3) {
13133 vsdb_info->replay_mode = (amd_vsdb->feature_caps & AMD_VSDB_VERSION_3_FEATURECAP_REPLAYMODE) ? true : false;
13134 vsdb_info->amd_vsdb_version = HDMI_AMD_VENDOR_SPECIFIC_DATA_BLOCK_VERSION_3;
13135 DRM_DEBUG_KMS("Panel supports Replay Mode: %d\n", vsdb_info->replay_mode);
13136
13137 return true;
13138 }
13139 j++;
13140 }
13141
13142 return false;
13143 }
13144
parse_hdmi_amd_vsdb(struct amdgpu_dm_connector * aconnector,const struct edid * edid,struct amdgpu_hdmi_vsdb_info * vsdb_info)13145 static int parse_hdmi_amd_vsdb(struct amdgpu_dm_connector *aconnector,
13146 const struct edid *edid,
13147 struct amdgpu_hdmi_vsdb_info *vsdb_info)
13148 {
13149 u8 *edid_ext = NULL;
13150 int i;
13151 bool valid_vsdb_found = false;
13152
13153 /*----- drm_find_cea_extension() -----*/
13154 /* No EDID or EDID extensions */
13155 if (edid == NULL || edid->extensions == 0)
13156 return -ENODEV;
13157
13158 /* Find CEA extension */
13159 for (i = 0; i < edid->extensions; i++) {
13160 edid_ext = (uint8_t *)edid + EDID_LENGTH * (i + 1);
13161 if (edid_ext[0] == CEA_EXT)
13162 break;
13163 }
13164
13165 if (i == edid->extensions)
13166 return -ENODEV;
13167
13168 /*----- cea_db_offsets() -----*/
13169 if (edid_ext[0] != CEA_EXT)
13170 return -ENODEV;
13171
13172 valid_vsdb_found = parse_edid_cea(aconnector, edid_ext, EDID_LENGTH, vsdb_info);
13173
13174 return valid_vsdb_found ? i : -ENODEV;
13175 }
13176
13177 /**
13178 * amdgpu_dm_update_freesync_caps - Update Freesync capabilities
13179 *
13180 * @connector: Connector to query.
13181 * @drm_edid: DRM EDID from monitor
13182 *
13183 * Amdgpu supports Freesync in DP and HDMI displays, and it is required to keep
13184 * track of some of the display information in the internal data struct used by
13185 * amdgpu_dm. This function checks which type of connector we need to set the
13186 * FreeSync parameters.
13187 */
amdgpu_dm_update_freesync_caps(struct drm_connector * connector,const struct drm_edid * drm_edid)13188 void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
13189 const struct drm_edid *drm_edid)
13190 {
13191 int i = 0;
13192 struct amdgpu_dm_connector *amdgpu_dm_connector =
13193 to_amdgpu_dm_connector(connector);
13194 struct dm_connector_state *dm_con_state = NULL;
13195 struct dc_sink *sink;
13196 struct amdgpu_device *adev = drm_to_adev(connector->dev);
13197 struct amdgpu_hdmi_vsdb_info vsdb_info = {0};
13198 const struct edid *edid;
13199 bool freesync_capable = false;
13200 enum adaptive_sync_type as_type = ADAPTIVE_SYNC_TYPE_NONE;
13201
13202 if (!connector->state) {
13203 drm_err(adev_to_drm(adev), "%s - Connector has no state", __func__);
13204 goto update;
13205 }
13206
13207 sink = amdgpu_dm_connector->dc_sink ?
13208 amdgpu_dm_connector->dc_sink :
13209 amdgpu_dm_connector->dc_em_sink;
13210
13211 drm_edid_connector_update(connector, drm_edid);
13212
13213 if (!drm_edid || !sink) {
13214 dm_con_state = to_dm_connector_state(connector->state);
13215
13216 amdgpu_dm_connector->min_vfreq = 0;
13217 amdgpu_dm_connector->max_vfreq = 0;
13218 freesync_capable = false;
13219
13220 goto update;
13221 }
13222
13223 dm_con_state = to_dm_connector_state(connector->state);
13224
13225 if (!adev->dm.freesync_module || !dc_supports_vrr(sink->ctx->dce_version))
13226 goto update;
13227
13228 edid = drm_edid_raw(drm_edid); // FIXME: Get rid of drm_edid_raw()
13229
13230 /* Some eDP panels only have the refresh rate range info in DisplayID */
13231 if ((connector->display_info.monitor_range.min_vfreq == 0 ||
13232 connector->display_info.monitor_range.max_vfreq == 0))
13233 parse_edid_displayid_vrr(connector, edid);
13234
13235 if (edid && (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT ||
13236 sink->sink_signal == SIGNAL_TYPE_EDP)) {
13237 if (amdgpu_dm_connector->dc_link &&
13238 amdgpu_dm_connector->dc_link->dpcd_caps.allow_invalid_MSA_timing_param) {
13239 amdgpu_dm_connector->min_vfreq = connector->display_info.monitor_range.min_vfreq;
13240 amdgpu_dm_connector->max_vfreq = connector->display_info.monitor_range.max_vfreq;
13241 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
13242 freesync_capable = true;
13243 }
13244
13245 parse_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
13246
13247 if (vsdb_info.replay_mode) {
13248 amdgpu_dm_connector->vsdb_info.replay_mode = vsdb_info.replay_mode;
13249 amdgpu_dm_connector->vsdb_info.amd_vsdb_version = vsdb_info.amd_vsdb_version;
13250 amdgpu_dm_connector->as_type = ADAPTIVE_SYNC_TYPE_EDP;
13251 }
13252
13253 } else if (drm_edid && sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A) {
13254 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
13255 if (i >= 0 && vsdb_info.freesync_supported) {
13256 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz;
13257 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz;
13258 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
13259 freesync_capable = true;
13260
13261 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz;
13262 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz;
13263 }
13264 }
13265
13266 if (amdgpu_dm_connector->dc_link)
13267 as_type = dm_get_adaptive_sync_support_type(amdgpu_dm_connector->dc_link);
13268
13269 if (as_type == FREESYNC_TYPE_PCON_IN_WHITELIST) {
13270 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
13271 if (i >= 0 && vsdb_info.freesync_supported && vsdb_info.amd_vsdb_version > 0) {
13272
13273 amdgpu_dm_connector->pack_sdp_v1_3 = true;
13274 amdgpu_dm_connector->as_type = as_type;
13275 amdgpu_dm_connector->vsdb_info = vsdb_info;
13276
13277 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz;
13278 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz;
13279 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
13280 freesync_capable = true;
13281
13282 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz;
13283 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz;
13284 }
13285 }
13286
13287 update:
13288 if (dm_con_state)
13289 dm_con_state->freesync_capable = freesync_capable;
13290
13291 if (connector->state && amdgpu_dm_connector->dc_link && !freesync_capable &&
13292 amdgpu_dm_connector->dc_link->replay_settings.config.replay_supported) {
13293 amdgpu_dm_connector->dc_link->replay_settings.config.replay_supported = false;
13294 amdgpu_dm_connector->dc_link->replay_settings.replay_feature_enabled = false;
13295 }
13296
13297 if (connector->vrr_capable_property)
13298 drm_connector_set_vrr_capable_property(connector,
13299 freesync_capable);
13300 }
13301
amdgpu_dm_trigger_timing_sync(struct drm_device * dev)13302 void amdgpu_dm_trigger_timing_sync(struct drm_device *dev)
13303 {
13304 struct amdgpu_device *adev = drm_to_adev(dev);
13305 struct dc *dc = adev->dm.dc;
13306 int i;
13307
13308 mutex_lock(&adev->dm.dc_lock);
13309 if (dc->current_state) {
13310 for (i = 0; i < dc->current_state->stream_count; ++i)
13311 dc->current_state->streams[i]
13312 ->triggered_crtc_reset.enabled =
13313 adev->dm.force_timing_sync;
13314
13315 dm_enable_per_frame_crtc_master_sync(dc->current_state);
13316 dc_trigger_sync(dc, dc->current_state);
13317 }
13318 mutex_unlock(&adev->dm.dc_lock);
13319 }
13320
amdgpu_dm_exit_ips_for_hw_access(struct dc * dc)13321 static inline void amdgpu_dm_exit_ips_for_hw_access(struct dc *dc)
13322 {
13323 if (dc->ctx->dmub_srv && !dc->ctx->dmub_srv->idle_exit_counter)
13324 dc_exit_ips_for_hw_access(dc);
13325 }
13326
dm_write_reg_func(const struct dc_context * ctx,uint32_t address,u32 value,const char * func_name)13327 void dm_write_reg_func(const struct dc_context *ctx, uint32_t address,
13328 u32 value, const char *func_name)
13329 {
13330 #ifdef DM_CHECK_ADDR_0
13331 if (address == 0) {
13332 drm_err(adev_to_drm(ctx->driver_context),
13333 "invalid register write. address = 0");
13334 return;
13335 }
13336 #endif
13337
13338 amdgpu_dm_exit_ips_for_hw_access(ctx->dc);
13339 cgs_write_register(ctx->cgs_device, address, value);
13340 trace_amdgpu_dc_wreg(&ctx->perf_trace->write_count, address, value);
13341 }
13342
dm_read_reg_func(const struct dc_context * ctx,uint32_t address,const char * func_name)13343 uint32_t dm_read_reg_func(const struct dc_context *ctx, uint32_t address,
13344 const char *func_name)
13345 {
13346 u32 value;
13347 #ifdef DM_CHECK_ADDR_0
13348 if (address == 0) {
13349 drm_err(adev_to_drm(ctx->driver_context),
13350 "invalid register read; address = 0\n");
13351 return 0;
13352 }
13353 #endif
13354
13355 if (ctx->dmub_srv &&
13356 ctx->dmub_srv->reg_helper_offload.gather_in_progress &&
13357 !ctx->dmub_srv->reg_helper_offload.should_burst_write) {
13358 ASSERT(false);
13359 return 0;
13360 }
13361
13362 amdgpu_dm_exit_ips_for_hw_access(ctx->dc);
13363
13364 value = cgs_read_register(ctx->cgs_device, address);
13365
13366 trace_amdgpu_dc_rreg(&ctx->perf_trace->read_count, address, value);
13367
13368 return value;
13369 }
13370
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)13371 int amdgpu_dm_process_dmub_aux_transfer_sync(
13372 struct dc_context *ctx,
13373 unsigned int link_index,
13374 struct aux_payload *payload,
13375 enum aux_return_code_type *operation_result)
13376 {
13377 struct amdgpu_device *adev = ctx->driver_context;
13378 struct dmub_notification *p_notify = adev->dm.dmub_notify;
13379 int ret = -1;
13380
13381 mutex_lock(&adev->dm.dpia_aux_lock);
13382 if (!dc_process_dmub_aux_transfer_async(ctx->dc, link_index, payload)) {
13383 *operation_result = AUX_RET_ERROR_ENGINE_ACQUIRE;
13384 goto out;
13385 }
13386
13387 if (!wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ)) {
13388 drm_err(adev_to_drm(adev), "wait_for_completion_timeout timeout!");
13389 *operation_result = AUX_RET_ERROR_TIMEOUT;
13390 goto out;
13391 }
13392
13393 if (p_notify->result != AUX_RET_SUCCESS) {
13394 /*
13395 * Transient states before tunneling is enabled could
13396 * lead to this error. We can ignore this for now.
13397 */
13398 if (p_notify->result == AUX_RET_ERROR_PROTOCOL_ERROR) {
13399 drm_warn(adev_to_drm(adev), "DPIA AUX failed on 0x%x(%d), error %d\n",
13400 payload->address, payload->length,
13401 p_notify->result);
13402 }
13403 *operation_result = p_notify->result;
13404 goto out;
13405 }
13406
13407 payload->reply[0] = adev->dm.dmub_notify->aux_reply.command & 0xF;
13408 if (adev->dm.dmub_notify->aux_reply.command & 0xF0)
13409 /* The reply is stored in the top nibble of the command. */
13410 payload->reply[0] = (adev->dm.dmub_notify->aux_reply.command >> 4) & 0xF;
13411
13412 /*write req may receive a byte indicating partially written number as well*/
13413 if (p_notify->aux_reply.length)
13414 memcpy(payload->data, p_notify->aux_reply.data,
13415 p_notify->aux_reply.length);
13416
13417 /* success */
13418 ret = p_notify->aux_reply.length;
13419 *operation_result = p_notify->result;
13420 out:
13421 reinit_completion(&adev->dm.dmub_aux_transfer_done);
13422 mutex_unlock(&adev->dm.dpia_aux_lock);
13423 return ret;
13424 }
13425
abort_fused_io(struct dc_context * ctx,const struct dmub_cmd_fused_request * request)13426 static void abort_fused_io(
13427 struct dc_context *ctx,
13428 const struct dmub_cmd_fused_request *request
13429 )
13430 {
13431 union dmub_rb_cmd command = { 0 };
13432 struct dmub_rb_cmd_fused_io *io = &command.fused_io;
13433
13434 io->header.type = DMUB_CMD__FUSED_IO;
13435 io->header.sub_type = DMUB_CMD__FUSED_IO_ABORT;
13436 io->header.payload_bytes = sizeof(*io) - sizeof(io->header);
13437 io->request = *request;
13438 dm_execute_dmub_cmd(ctx, &command, DM_DMUB_WAIT_TYPE_NO_WAIT);
13439 }
13440
execute_fused_io(struct amdgpu_device * dev,struct dc_context * ctx,union dmub_rb_cmd * commands,uint8_t count,uint32_t timeout_us)13441 static bool execute_fused_io(
13442 struct amdgpu_device *dev,
13443 struct dc_context *ctx,
13444 union dmub_rb_cmd *commands,
13445 uint8_t count,
13446 uint32_t timeout_us
13447 )
13448 {
13449 const uint8_t ddc_line = commands[0].fused_io.request.u.aux.ddc_line;
13450
13451 if (ddc_line >= ARRAY_SIZE(dev->dm.fused_io))
13452 return false;
13453
13454 struct fused_io_sync *sync = &dev->dm.fused_io[ddc_line];
13455 struct dmub_rb_cmd_fused_io *first = &commands[0].fused_io;
13456 const bool result = dm_execute_dmub_cmd_list(ctx, count, commands, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY)
13457 && first->header.ret_status
13458 && first->request.status == FUSED_REQUEST_STATUS_SUCCESS;
13459
13460 if (!result)
13461 return false;
13462
13463 while (wait_for_completion_timeout(&sync->replied, usecs_to_jiffies(timeout_us))) {
13464 reinit_completion(&sync->replied);
13465
13466 struct dmub_cmd_fused_request *reply = (struct dmub_cmd_fused_request *) sync->reply_data;
13467
13468 static_assert(sizeof(*reply) <= sizeof(sync->reply_data), "Size mismatch");
13469
13470 if (reply->identifier == first->request.identifier) {
13471 first->request = *reply;
13472 return true;
13473 }
13474 }
13475
13476 reinit_completion(&sync->replied);
13477 first->request.status = FUSED_REQUEST_STATUS_TIMEOUT;
13478 abort_fused_io(ctx, &first->request);
13479 return false;
13480 }
13481
amdgpu_dm_execute_fused_io(struct amdgpu_device * dev,struct dc_link * link,union dmub_rb_cmd * commands,uint8_t count,uint32_t timeout_us)13482 bool amdgpu_dm_execute_fused_io(
13483 struct amdgpu_device *dev,
13484 struct dc_link *link,
13485 union dmub_rb_cmd *commands,
13486 uint8_t count,
13487 uint32_t timeout_us)
13488 {
13489 struct amdgpu_display_manager *dm = &dev->dm;
13490
13491 mutex_lock(&dm->dpia_aux_lock);
13492
13493 const bool result = execute_fused_io(dev, link->ctx, commands, count, timeout_us);
13494
13495 mutex_unlock(&dm->dpia_aux_lock);
13496 return result;
13497 }
13498
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)13499 int amdgpu_dm_process_dmub_set_config_sync(
13500 struct dc_context *ctx,
13501 unsigned int link_index,
13502 struct set_config_cmd_payload *payload,
13503 enum set_config_status *operation_result)
13504 {
13505 struct amdgpu_device *adev = ctx->driver_context;
13506 bool is_cmd_complete;
13507 int ret;
13508
13509 mutex_lock(&adev->dm.dpia_aux_lock);
13510 is_cmd_complete = dc_process_dmub_set_config_async(ctx->dc,
13511 link_index, payload, adev->dm.dmub_notify);
13512
13513 if (is_cmd_complete || wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ)) {
13514 ret = 0;
13515 *operation_result = adev->dm.dmub_notify->sc_status;
13516 } else {
13517 drm_err(adev_to_drm(adev), "wait_for_completion_timeout timeout!");
13518 ret = -1;
13519 *operation_result = SET_CONFIG_UNKNOWN_ERROR;
13520 }
13521
13522 if (!is_cmd_complete)
13523 reinit_completion(&adev->dm.dmub_aux_transfer_done);
13524 mutex_unlock(&adev->dm.dpia_aux_lock);
13525 return ret;
13526 }
13527
dm_execute_dmub_cmd(const struct dc_context * ctx,union dmub_rb_cmd * cmd,enum dm_dmub_wait_type wait_type)13528 bool dm_execute_dmub_cmd(const struct dc_context *ctx, union dmub_rb_cmd *cmd, enum dm_dmub_wait_type wait_type)
13529 {
13530 return dc_dmub_srv_cmd_run(ctx->dmub_srv, cmd, wait_type);
13531 }
13532
dm_execute_dmub_cmd_list(const struct dc_context * ctx,unsigned int count,union dmub_rb_cmd * cmd,enum dm_dmub_wait_type wait_type)13533 bool dm_execute_dmub_cmd_list(const struct dc_context *ctx, unsigned int count, union dmub_rb_cmd *cmd, enum dm_dmub_wait_type wait_type)
13534 {
13535 return dc_dmub_srv_cmd_run_list(ctx->dmub_srv, count, cmd, wait_type);
13536 }
13537
dm_acpi_process_phy_transition_interlock(const struct dc_context * ctx,struct dm_process_phy_transition_init_params process_phy_transition_init_params)13538 void dm_acpi_process_phy_transition_interlock(
13539 const struct dc_context *ctx,
13540 struct dm_process_phy_transition_init_params process_phy_transition_init_params)
13541 {
13542 // Not yet implemented
13543 }
13544