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