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