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