2 * Copyright 2021 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 #include <inc/core_status.h>
28 #include <inc/link_hwss.h>
29 #include <inc/link_dpcd.h>
30 #include "drm/drm_dp_helper.h"
31 #include <dc_dp_types.h>
32 #include "dm_helpers.h"
34 #define END_ADDRESS(start, size) (start + size - 1)
35 #define ADDRESS_RANGE_SIZE(start, end) (end - start + 1)
36 struct dpcd_address_range {
41 static enum dc_status internal_link_read_dpcd(
47 if (!link->aux_access_disabled &&
48 !dm_helpers_dp_read_dpcd(link->ctx,
49 link, address, data, size)) {
50 return DC_ERROR_UNEXPECTED;
56 static enum dc_status internal_link_write_dpcd(
62 if (!link->aux_access_disabled &&
63 !dm_helpers_dp_write_dpcd(link->ctx,
64 link, address, data, size)) {
65 return DC_ERROR_UNEXPECTED;
72 * Partition the entire DPCD address space
73 * XXX: This partitioning must cover the entire DPCD address space,
74 * and must contain no gaps or overlapping address ranges.
76 static const struct dpcd_address_range mandatory_dpcd_partitions[] = {
77 { 0, DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR1) - 1},
78 { DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR1), DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR2) - 1 },
79 { DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR2), DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR3) - 1 },
80 { DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR3), DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR4) - 1 },
81 { DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR4), DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR5) - 1 },
82 { DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR5), DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR6) - 1 },
83 { DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR6), DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR7) - 1 },
84 { DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR7), DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR8) - 1 },
85 { DP_TRAINING_PATTERN_SET_PHY_REPEATER(DP_PHY_LTTPR8), DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR1) - 1 },
87 * The FEC registers are contiguous
89 { DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR1), DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR1) - 1 },
90 { DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR2), DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR2) - 1 },
91 { DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR3), DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR3) - 1 },
92 { DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR4), DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR4) - 1 },
93 { DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR5), DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR5) - 1 },
94 { DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR6), DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR6) - 1 },
95 { DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR7), DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR7) - 1 },
96 { DP_FEC_STATUS_PHY_REPEATER(DP_PHY_LTTPR8), DP_LTTPR_MAX_ADD },
97 /* all remaining DPCD addresses */
98 { DP_LTTPR_MAX_ADD + 1, DP_DPCD_MAX_ADD } };
100 static inline bool do_addresses_intersect_with_range(
101 const struct dpcd_address_range *range,
102 const uint32_t start_address,
103 const uint32_t end_address)
105 return start_address <= range->end && end_address >= range->start;
108 static uint32_t dpcd_get_next_partition_size(const uint32_t address, const uint32_t size)
110 const uint32_t end_address = END_ADDRESS(address, size);
111 uint32_t partition_iterator = 0;
114 * find current partition
115 * this loop spins forever if partition map above is not surjective
117 while (!do_addresses_intersect_with_range(&mandatory_dpcd_partitions[partition_iterator],
118 address, end_address))
119 partition_iterator++;
120 if (end_address < mandatory_dpcd_partitions[partition_iterator].end)
122 return ADDRESS_RANGE_SIZE(address, mandatory_dpcd_partitions[partition_iterator].end);
126 * Ranges of DPCD addresses that must be read in a single transaction
127 * XXX: Do not allow any two address ranges in this array to overlap
129 static const struct dpcd_address_range mandatory_dpcd_blocks[] = {
130 { DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV, DP_PHY_REPEATER_EXTENDED_WAIT_TIMEOUT }};
133 * extend addresses to read all mandatory blocks together
135 static void dpcd_extend_address_range(
136 const uint32_t in_address,
137 uint8_t * const in_data,
138 const uint32_t in_size,
139 uint32_t *out_address,
143 const uint32_t end_address = END_ADDRESS(in_address, in_size);
144 const struct dpcd_address_range *addr_range;
145 struct dpcd_address_range new_addr_range;
148 new_addr_range.start = in_address;
149 new_addr_range.end = end_address;
150 for (i = 0; i < ARRAY_SIZE(mandatory_dpcd_blocks); i++) {
151 addr_range = &mandatory_dpcd_blocks[i];
152 if (addr_range->start <= in_address && addr_range->end >= in_address)
153 new_addr_range.start = addr_range->start;
155 if (addr_range->start <= end_address && addr_range->end >= end_address)
156 new_addr_range.end = addr_range->end;
158 *out_address = in_address;
161 if (new_addr_range.start != in_address || new_addr_range.end != end_address) {
162 *out_address = new_addr_range.start;
163 *out_size = ADDRESS_RANGE_SIZE(new_addr_range.start, new_addr_range.end);
164 *out_data = kzalloc(*out_size * sizeof(**out_data), GFP_KERNEL);
169 * Reduce the AUX reply down to the values the caller requested
171 static void dpcd_reduce_address_range(
172 const uint32_t extended_address,
173 uint8_t * const extended_data,
174 const uint32_t extended_size,
175 const uint32_t reduced_address,
176 uint8_t * const reduced_data,
177 const uint32_t reduced_size)
179 const uint32_t offset = reduced_address - extended_address;
182 * If the address is same, address was not extended.
183 * So we do not need to free any memory.
184 * The data is in original buffer(reduced_data).
186 if (extended_data == reduced_data)
189 memcpy(&extended_data[offset], reduced_data, reduced_size);
190 kfree(extended_data);
193 enum dc_status core_link_read_dpcd(
194 struct dc_link *link,
199 uint32_t extended_address;
200 uint32_t partitioned_address;
201 uint8_t *extended_data;
202 uint32_t extended_size;
203 /* size of the remaining partitioned address space */
204 uint32_t size_left_to_read;
205 enum dc_status status;
206 /* size of the next partition to be read from */
207 uint32_t partition_size;
208 uint32_t data_index = 0;
210 dpcd_extend_address_range(address, data, size, &extended_address, &extended_data, &extended_size);
211 partitioned_address = extended_address;
212 size_left_to_read = extended_size;
213 while (size_left_to_read) {
214 partition_size = dpcd_get_next_partition_size(partitioned_address, size_left_to_read);
215 status = internal_link_read_dpcd(link, partitioned_address, &extended_data[data_index], partition_size);
218 partitioned_address += partition_size;
219 data_index += partition_size;
220 size_left_to_read -= partition_size;
222 dpcd_reduce_address_range(extended_address, extended_data, extended_size, address, data, size);
226 enum dc_status core_link_write_dpcd(
227 struct dc_link *link,
232 uint32_t partition_size;
233 uint32_t data_index = 0;
234 enum dc_status status;
237 partition_size = dpcd_get_next_partition_size(address, size);
238 status = internal_link_write_dpcd(link, address, &data[data_index], partition_size);
241 address += partition_size;
242 data_index += partition_size;
243 size -= partition_size;