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