drm/amd/display: break down dc_link.c
[linux-2.6-microblaze.git] / drivers / gpu / drm / amd / display / dc / dc_link.h
1 /*
2  * Copyright 2012-14 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 #ifndef DC_LINK_H_
27 #define DC_LINK_H_
28
29 #include "dc.h"
30 #include "dc_types.h"
31 #include "grph_object_defs.h"
32
33 struct link_resource;
34 enum aux_return_code_type;
35
36 enum dc_link_fec_state {
37         dc_link_fec_not_ready,
38         dc_link_fec_ready,
39         dc_link_fec_enabled
40 };
41
42 struct dc_link_status {
43         bool link_active;
44         struct dpcd_caps *dpcd_caps;
45 };
46
47 struct dprx_states {
48         bool cable_id_written;
49 };
50
51 /* DP MST stream allocation (payload bandwidth number) */
52 struct link_mst_stream_allocation {
53         /* DIG front */
54         const struct stream_encoder *stream_enc;
55         /* HPO DP Stream Encoder */
56         const struct hpo_dp_stream_encoder *hpo_dp_stream_enc;
57         /* associate DRM payload table with DC stream encoder */
58         uint8_t vcp_id;
59         /* number of slots required for the DP stream in transport packet */
60         uint8_t slot_count;
61 };
62
63 /* DP MST stream allocation table */
64 struct link_mst_stream_allocation_table {
65         /* number of DP video streams */
66         int stream_count;
67         /* array of stream allocations */
68         struct link_mst_stream_allocation stream_allocations[MAX_CONTROLLER_NUM];
69 };
70
71 struct edp_trace_power_timestamps {
72         uint64_t poweroff;
73         uint64_t poweron;
74 };
75
76 struct dp_trace_lt_counts {
77         unsigned int total;
78         unsigned int fail;
79 };
80
81 struct dp_trace_lt {
82         struct dp_trace_lt_counts counts;
83         struct dp_trace_timestamps {
84                 unsigned long long start;
85                 unsigned long long end;
86         } timestamps;
87         enum link_training_result result;
88         bool is_logged;
89 };
90
91 struct dp_trace {
92         struct dp_trace_lt detect_lt_trace;
93         struct dp_trace_lt commit_lt_trace;
94         unsigned int link_loss_count;
95         bool is_initialized;
96         struct edp_trace_power_timestamps edp_trace_power_timestamps;
97 };
98
99 /* PSR feature flags */
100 struct psr_settings {
101         bool psr_feature_enabled;               // PSR is supported by sink
102         bool psr_allow_active;                  // PSR is currently active
103         enum dc_psr_version psr_version;                // Internal PSR version, determined based on DPCD
104         bool psr_vtotal_control_support;        // Vtotal control is supported by sink
105
106         /* These parameters are calculated in Driver,
107          * based on display timing and Sink capabilities.
108          * If VBLANK region is too small and Sink takes a long time
109          * to set up RFB, it may take an extra frame to enter PSR state.
110          */
111         bool psr_frame_capture_indication_req;
112         unsigned int psr_sdp_transmit_line_num_deadline;
113         uint8_t force_ffu_mode;
114         unsigned int psr_power_opt;
115 };
116
117 /* To split out "global" and "per-panel" config settings.
118  * Add a struct dc_panel_config under dc_link
119  */
120 struct dc_panel_config {
121         /* extra panel power sequence parameters */
122         struct pps {
123                 unsigned int extra_t3_ms;
124                 unsigned int extra_t7_ms;
125                 unsigned int extra_delay_backlight_off;
126                 unsigned int extra_post_t7_ms;
127                 unsigned int extra_pre_t11_ms;
128                 unsigned int extra_t12_ms;
129                 unsigned int extra_post_OUI_ms;
130         } pps;
131         /* PSR */
132         struct psr {
133                 bool disable_psr;
134                 bool disallow_psrsu;
135                 bool rc_disable;
136                 bool rc_allow_static_screen;
137                 bool rc_allow_fullscreen_VPB;
138         } psr;
139         /* ABM */
140         struct varib {
141                 unsigned int varibright_feature_enable;
142                 unsigned int def_varibright_level;
143                 unsigned int abm_config_setting;
144         } varib;
145         /* edp DSC */
146         struct dsc {
147                 bool disable_dsc_edp;
148                 unsigned int force_dsc_edp_policy;
149         } dsc;
150         /* eDP ILR */
151         struct ilr {
152                 bool optimize_edp_link_rate; /* eDP ILR */
153         } ilr;
154 };
155
156 /*
157  *  USB4 DPIA BW ALLOCATION STRUCTS
158  */
159 struct dc_dpia_bw_alloc {
160         int sink_verified_bw;  // The Verified BW that sink can allocated and use that has been verified already
161         int sink_allocated_bw; // The Actual Allocated BW that sink currently allocated
162         int sink_max_bw;       // The Max BW that sink can require/support
163         int estimated_bw;      // The estimated available BW for this DPIA
164         int bw_granularity;    // BW Granularity
165         bool bw_alloc_enabled; // The BW Alloc Mode Support is turned ON for all 3:  DP-Tx & Dpia & CM
166         bool response_ready;   // Response ready from the CM side
167 };
168
169 /*
170  * A link contains one or more sinks and their connected status.
171  * The currently active signal type (HDMI, DP-SST, DP-MST) is also reported.
172  */
173 struct dc_link {
174         struct dc_sink *remote_sinks[MAX_SINKS_PER_LINK];
175         unsigned int sink_count;
176         struct dc_sink *local_sink;
177         unsigned int link_index;
178         enum dc_connection_type type;
179         enum signal_type connector_signal;
180         enum dc_irq_source irq_source_hpd;
181         enum dc_irq_source irq_source_hpd_rx;/* aka DP Short Pulse  */
182         bool is_hpd_filter_disabled;
183         bool dp_ss_off;
184
185         /**
186          * @link_state_valid:
187          *
188          * If there is no link and local sink, this variable should be set to
189          * false. Otherwise, it should be set to true; usually, the function
190          * core_link_enable_stream sets this field to true.
191          */
192         bool link_state_valid;
193         bool aux_access_disabled;
194         bool sync_lt_in_progress;
195         bool is_internal_display;
196
197         /* TODO: Rename. Flag an endpoint as having a programmable mapping to a
198          * DIG encoder. */
199         bool is_dig_mapping_flexible;
200         bool hpd_status; /* HPD status of link without physical HPD pin. */
201         bool is_hpd_pending; /* Indicates a new received hpd */
202         bool is_automated; /* Indicates automated testing */
203
204         bool edp_sink_present;
205
206         struct dp_trace dp_trace;
207
208         /* caps is the same as reported_link_cap. link_traing use
209          * reported_link_cap. Will clean up.  TODO
210          */
211         struct dc_link_settings reported_link_cap;
212         struct dc_link_settings verified_link_cap;
213         struct dc_link_settings cur_link_settings;
214         struct dc_lane_settings cur_lane_setting[LANE_COUNT_DP_MAX];
215         struct dc_link_settings preferred_link_setting;
216         /* preferred_training_settings are override values that
217          * come from DM. DM is responsible for the memory
218          * management of the override pointers.
219          */
220         struct dc_link_training_overrides preferred_training_settings;
221         struct dp_audio_test_data audio_test_data;
222
223         uint8_t ddc_hw_inst;
224
225         uint8_t hpd_src;
226
227         uint8_t link_enc_hw_inst;
228         /* DIG link encoder ID. Used as index in link encoder resource pool.
229          * For links with fixed mapping to DIG, this is not changed after dc_link
230          * object creation.
231          */
232         enum engine_id eng_id;
233
234         bool test_pattern_enabled;
235         union compliance_test_state compliance_test_state;
236
237         void *priv;
238
239         struct ddc_service *ddc;
240
241         bool aux_mode;
242
243         /* Private to DC core */
244
245         const struct dc *dc;
246
247         struct dc_context *ctx;
248
249         struct panel_cntl *panel_cntl;
250         struct link_encoder *link_enc;
251         struct graphics_object_id link_id;
252         /* Endpoint type distinguishes display endpoints which do not have entries
253          * in the BIOS connector table from those that do. Helps when tracking link
254          * encoder to display endpoint assignments.
255          */
256         enum display_endpoint_type ep_type;
257         union ddi_channel_mapping ddi_channel_mapping;
258         struct connector_device_tag_info device_tag;
259         struct dpcd_caps dpcd_caps;
260         uint32_t dongle_max_pix_clk;
261         unsigned short chip_caps;
262         unsigned int dpcd_sink_count;
263 #if defined(CONFIG_DRM_AMD_DC_HDCP)
264         struct hdcp_caps hdcp_caps;
265 #endif
266         enum edp_revision edp_revision;
267         union dpcd_sink_ext_caps dpcd_sink_ext_caps;
268
269         struct psr_settings psr_settings;
270
271         /* Drive settings read from integrated info table */
272         struct dc_lane_settings bios_forced_drive_settings;
273
274         /* Vendor specific LTTPR workaround variables */
275         uint8_t vendor_specific_lttpr_link_rate_wa;
276         bool apply_vendor_specific_lttpr_link_rate_wa;
277
278         /* MST record stream using this link */
279         struct link_flags {
280                 bool dp_keep_receiver_powered;
281                 bool dp_skip_DID2;
282                 bool dp_skip_reset_segment;
283                 bool dp_skip_fs_144hz;
284                 bool dp_mot_reset_segment;
285                 /* Some USB4 docks do not handle turning off MST DSC once it has been enabled. */
286                 bool dpia_mst_dsc_always_on;
287                 /* Forced DPIA into TBT3 compatibility mode. */
288                 bool dpia_forced_tbt3_mode;
289                 bool dongle_mode_timing_override;
290         } wa_flags;
291         struct link_mst_stream_allocation_table mst_stream_alloc_table;
292
293         struct dc_link_status link_status;
294         struct dprx_states dprx_states;
295
296         struct gpio *hpd_gpio;
297         enum dc_link_fec_state fec_state;
298         bool link_powered_externally;   // Used to bypass hardware sequencing delays when panel is powered down forcibly
299
300         struct dc_panel_config panel_config;
301         struct phy_state phy_state;
302 };
303
304 const struct dc_link_status *dc_link_get_status(const struct dc_link *dc_link);
305
306 /**
307  * dc_get_link_at_index() - Return an enumerated dc_link.
308  *
309  * dc_link order is constant and determined at
310  * boot time.  They cannot be created or destroyed.
311  * Use dc_get_caps() to get number of links.
312  */
313 static inline struct dc_link *dc_get_link_at_index(struct dc *dc, uint32_t link_index)
314 {
315         return dc->links[link_index];
316 }
317
318 static inline void get_edp_links(const struct dc *dc,
319                 struct dc_link **edp_links,
320                 int *edp_num)
321 {
322         int i;
323
324         *edp_num = 0;
325         for (i = 0; i < dc->link_count; i++) {
326                 // report any eDP links, even unconnected DDI's
327                 if (!dc->links[i])
328                         continue;
329                 if (dc->links[i]->connector_signal == SIGNAL_TYPE_EDP) {
330                         edp_links[*edp_num] = dc->links[i];
331                         if (++(*edp_num) == MAX_NUM_EDP)
332                                 return;
333                 }
334         }
335 }
336
337 static inline bool dc_get_edp_link_panel_inst(const struct dc *dc,
338                 const struct dc_link *link,
339                 unsigned int *inst_out)
340 {
341         struct dc_link *edp_links[MAX_NUM_EDP];
342         int edp_num, i;
343
344         *inst_out = 0;
345         if (link->connector_signal != SIGNAL_TYPE_EDP)
346                 return false;
347         get_edp_links(dc, edp_links, &edp_num);
348         for (i = 0; i < edp_num; i++) {
349                 if (link == edp_links[i])
350                         break;
351                 (*inst_out)++;
352         }
353         return true;
354 }
355
356 /* Set backlight level of an embedded panel (eDP, LVDS).
357  * backlight_pwm_u16_16 is unsigned 32 bit with 16 bit integer
358  * and 16 bit fractional, where 1.0 is max backlight value.
359  */
360 bool dc_link_set_backlight_level(const struct dc_link *dc_link,
361                 uint32_t backlight_pwm_u16_16,
362                 uint32_t frame_ramp);
363
364 /* Set/get nits-based backlight level of an embedded panel (eDP, LVDS). */
365 bool dc_link_set_backlight_level_nits(struct dc_link *link,
366                 bool isHDR,
367                 uint32_t backlight_millinits,
368                 uint32_t transition_time_in_ms);
369
370 bool dc_link_get_backlight_level_nits(struct dc_link *link,
371                 uint32_t *backlight_millinits,
372                 uint32_t *backlight_millinits_peak);
373
374 int dc_link_get_backlight_level(const struct dc_link *dc_link);
375
376 int dc_link_get_target_backlight_pwm(const struct dc_link *link);
377
378 bool dc_link_set_psr_allow_active(struct dc_link *dc_link, const bool *enable,
379                 bool wait, bool force_static, const unsigned int *power_opts);
380
381 bool dc_link_get_psr_state(const struct dc_link *dc_link, enum dc_psr_state *state);
382
383 bool dc_link_setup_psr(struct dc_link *dc_link,
384                 const struct dc_stream_state *stream, struct psr_config *psr_config,
385                 struct psr_context *psr_context);
386
387 /* Request DC to detect if there is a Panel connected.
388  * boot - If this call is during initial boot.
389  * Return false for any type of detection failure or MST detection
390  * true otherwise. True meaning further action is required (status update
391  * and OS notification).
392  */
393 enum dc_detect_reason {
394         DETECT_REASON_BOOT,
395         DETECT_REASON_RESUMEFROMS3S4,
396         DETECT_REASON_HPD,
397         DETECT_REASON_HPDRX,
398         DETECT_REASON_FALLBACK,
399         DETECT_REASON_RETRAIN,
400         DETECT_REASON_TDR,
401 };
402
403 bool dc_link_detect(struct dc_link *dc_link, enum dc_detect_reason reason);
404 bool dc_link_get_hpd_state(struct dc_link *dc_link);
405
406 /* Notify DC about DP RX Interrupt (aka Short Pulse Interrupt).
407  * Return:
408  * true - Downstream port status changed. DM should call DC to do the
409  * detection.
410  * false - no change in Downstream port status. No further action required
411  * from DM. */
412 bool dc_link_handle_hpd_rx_irq(struct dc_link *dc_link,
413                 union hpd_irq_data *hpd_irq_dpcd_data, bool *out_link_loss,
414                 bool defer_handling, bool *has_left_work);
415
416 /*
417  * On eDP links this function call will stall until T12 has elapsed.
418  * If the panel is not in power off state, this function will return
419  * immediately.
420  */
421 bool dc_link_wait_for_t12(struct dc_link *link);
422
423 void dc_link_dp_handle_automated_test(struct dc_link *link);
424 void dc_link_dp_handle_link_loss(struct dc_link *link);
425 bool dc_link_dp_allow_hpd_rx_irq(const struct dc_link *link);
426 bool dc_link_check_link_loss_status(struct dc_link *link,
427                 union hpd_irq_data *hpd_irq_dpcd_data);
428 enum dc_status dc_link_dp_read_hpd_rx_irq_data(
429         struct dc_link *link,
430         union hpd_irq_data *irq_data);
431 struct dc_sink_init_data;
432
433 struct dc_sink *dc_link_add_remote_sink(
434                 struct dc_link *dc_link,
435                 const uint8_t *edid,
436                 int len,
437                 struct dc_sink_init_data *init_data);
438
439 void dc_link_remove_remote_sink(
440         struct dc_link *link,
441         struct dc_sink *sink);
442
443 /* Used by diagnostics for virtual link at the moment */
444
445 bool dc_link_dp_set_test_pattern(
446         struct dc_link *link,
447         enum dp_test_pattern test_pattern,
448         enum dp_test_pattern_color_space test_pattern_color_space,
449         const struct link_training_settings *p_link_settings,
450         const unsigned char *p_custom_pattern,
451         unsigned int cust_pattern_size);
452
453 bool dc_link_dp_get_max_link_enc_cap(const struct dc_link *link, struct dc_link_settings *max_link_enc_cap);
454
455 /**
456  *****************************************************************************
457  *  Function: dc_link_enable_hpd_filter
458  *
459  *  @brief
460  *     If enable is true, programs HPD filter on associated HPD line to default
461  *     values dependent on link->connector_signal
462  *
463  *     If enable is false, programs HPD filter on associated HPD line with no
464  *     delays on connect or disconnect
465  *
466  *  @param [in] link: pointer to the dc link
467  *  @param [in] enable: boolean specifying whether to enable hbd
468  *****************************************************************************
469  */
470 void dc_link_enable_hpd_filter(struct dc_link *link, bool enable);
471
472 bool dc_link_is_dp_sink_present(struct dc_link *link);
473
474 bool dc_link_detect_connection_type(struct dc_link *link, enum dc_connection_type *type);
475 /*
476  * DPCD access interfaces
477  */
478
479 #ifdef CONFIG_DRM_AMD_DC_HDCP
480 bool dc_link_is_hdcp14(struct dc_link *link, enum signal_type signal);
481 bool dc_link_is_hdcp22(struct dc_link *link, enum signal_type signal);
482 #endif
483 void dc_link_set_drive_settings(struct dc *dc,
484                                 struct link_training_settings *lt_settings,
485                                 const struct dc_link *link);
486 void dc_link_set_preferred_link_settings(struct dc *dc,
487                                          struct dc_link_settings *link_setting,
488                                          struct dc_link *link);
489 void dc_link_set_preferred_training_settings(struct dc *dc,
490                                         struct dc_link_settings *link_setting,
491                                         struct dc_link_training_overrides *lt_overrides,
492                                         struct dc_link *link,
493                                         bool skip_immediate_retrain);
494 void dc_link_enable_hpd(const struct dc_link *link);
495 void dc_link_disable_hpd(const struct dc_link *link);
496 void dc_link_set_test_pattern(struct dc_link *link,
497                         enum dp_test_pattern test_pattern,
498                         enum dp_test_pattern_color_space test_pattern_color_space,
499                         const struct link_training_settings *p_link_settings,
500                         const unsigned char *p_custom_pattern,
501                         unsigned int cust_pattern_size);
502 uint32_t dc_link_bandwidth_kbps(
503         const struct dc_link *link,
504         const struct dc_link_settings *link_setting);
505
506 const struct dc_link_settings *dc_link_get_link_cap(
507                 const struct dc_link *link);
508
509 void dc_link_overwrite_extended_receiver_cap(
510                 struct dc_link *link);
511
512 bool dc_is_oem_i2c_device_present(
513         struct dc *dc,
514         size_t slave_address
515 );
516
517 bool dc_submit_i2c(
518                 struct dc *dc,
519                 uint32_t link_index,
520                 struct i2c_command *cmd);
521
522 bool dc_submit_i2c_oem(
523                 struct dc *dc,
524                 struct i2c_command *cmd);
525
526 uint32_t dc_bandwidth_in_kbps_from_timing(
527         const struct dc_crtc_timing *timing);
528
529 bool dc_link_is_fec_supported(const struct dc_link *link);
530 bool dc_link_should_enable_fec(const struct dc_link *link);
531
532 uint32_t dc_link_bw_kbps_from_raw_frl_link_rate_data(uint8_t bw);
533 enum dp_link_encoding dc_link_dp_mst_decide_link_encoding_format(const struct dc_link *link);
534
535 void dc_link_get_cur_link_res(const struct dc_link *link,
536                 struct link_resource *link_res);
537 /* take a snapshot of current link resource allocation state */
538 void dc_get_cur_link_res_map(const struct dc *dc, uint32_t *map);
539 /* restore link resource allocation state from a snapshot */
540 void dc_restore_link_res_map(const struct dc *dc, uint32_t *map);
541 void dc_link_clear_dprx_states(struct dc_link *link);
542 void dp_trace_reset(struct dc_link *link);
543 bool dc_dp_trace_is_initialized(struct dc_link *link);
544 unsigned long long dc_dp_trace_get_lt_end_timestamp(struct dc_link *link,
545                 bool in_detection);
546 void dc_dp_trace_set_is_logged_flag(struct dc_link *link,
547                 bool in_detection,
548                 bool is_logged);
549 bool dc_dp_trace_is_logged(struct dc_link *link,
550                 bool in_detection);
551 struct dp_trace_lt_counts *dc_dp_trace_get_lt_counts(struct dc_link *link,
552                 bool in_detection);
553 unsigned int dc_dp_trace_get_link_loss_count(struct dc_link *link);
554
555 /* Destruct the mst topology of the link and reset the allocated payload table */
556 bool dc_link_reset_cur_dp_mst_topology(struct dc_link *link);
557
558 /* Attempt to transfer the given aux payload. This function does not perform
559  * retries or handle error states. The reply is returned in the payload->reply
560  * and the result through operation_result. Returns the number of bytes
561  * transferred,or -1 on a failure.
562  */
563 int dc_link_aux_transfer_raw(struct ddc_service *ddc,
564                 struct aux_payload *payload,
565                 enum aux_return_code_type *operation_result);
566
567 enum lttpr_mode dc_link_decide_lttpr_mode(struct dc_link *link,
568                 struct dc_link_settings *link_setting);
569 void dc_link_dp_receiver_power_ctrl(struct dc_link *link, bool on);
570 bool dc_link_decide_edp_link_settings(struct dc_link *link,
571                 struct dc_link_settings *link_setting,
572                 uint32_t req_bw);
573 void dc_link_edp_panel_backlight_power_on(struct dc_link *link,
574                 bool wait_for_hpd);
575
576 /*
577  *  USB4 DPIA BW ALLOCATION PUBLIC FUNCTIONS
578  */
579 /*
580  * Send a request from DP-Tx requesting to allocate BW remotely after
581  * allocating it locally. This will get processed by CM and a CB function
582  * will be called.
583  *
584  * @link: pointer to the dc_link struct instance
585  * @req_bw: The requested bw in Kbyte to allocated
586  *
587  * return: none
588  */
589 void dc_link_set_usb4_req_bw_req(struct dc_link *link, int req_bw);
590
591 /*
592  * CB function for when the status of the Req above is complete. We will
593  * find out the result of allocating on CM and update structs accordingly
594  *
595  * @link: pointer to the dc_link struct instance
596  * @bw: Allocated or Estimated BW depending on the result
597  * @result: Response type
598  *
599  * return: none
600  */
601 void dc_link_get_usb4_req_bw_resp(struct dc_link *link, uint8_t bw, uint8_t result);
602
603 /* TODO: this is not meant to be exposed to DM. Should switch to stream update
604  * interface i.e stream_update->dsc_config
605  */
606 bool dc_link_update_dsc_config(struct pipe_ctx *pipe_ctx);
607 #endif /* DC_LINK_H_ */