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