xref: /dflybsd-src/sys/dev/drm/amd/include/amd_pcie_helpers.h (revision b843c749addef9340ee7d4e250b09fdd492602a1)
1d78d3a22SFrançois Tigeot /*
2d78d3a22SFrançois Tigeot  * Copyright 2015 Advanced Micro Devices, Inc.
3d78d3a22SFrançois Tigeot  *
4d78d3a22SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5d78d3a22SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6d78d3a22SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7d78d3a22SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8d78d3a22SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9d78d3a22SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10d78d3a22SFrançois Tigeot  *
11d78d3a22SFrançois Tigeot  * The above copyright notice and this permission notice shall be included in
12d78d3a22SFrançois Tigeot  * all copies or substantial portions of the Software.
13d78d3a22SFrançois Tigeot  *
14d78d3a22SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15d78d3a22SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16d78d3a22SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17d78d3a22SFrançois Tigeot  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18d78d3a22SFrançois Tigeot  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19d78d3a22SFrançois Tigeot  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20d78d3a22SFrançois Tigeot  * OTHER DEALINGS IN THE SOFTWARE.
21d78d3a22SFrançois Tigeot  */
22d78d3a22SFrançois Tigeot 
23d78d3a22SFrançois Tigeot #ifndef __AMD_PCIE_HELPERS_H__
24d78d3a22SFrançois Tigeot #define __AMD_PCIE_HELPERS_H__
25d78d3a22SFrançois Tigeot 
26d78d3a22SFrançois Tigeot #include "amd_pcie.h"
27d78d3a22SFrançois Tigeot 
is_pcie_gen3_supported(uint32_t pcie_link_speed_cap)28d78d3a22SFrançois Tigeot static inline bool is_pcie_gen3_supported(uint32_t pcie_link_speed_cap)
29d78d3a22SFrançois Tigeot {
30d78d3a22SFrançois Tigeot 	if (pcie_link_speed_cap & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
31d78d3a22SFrançois Tigeot 		return true;
32d78d3a22SFrançois Tigeot 
33d78d3a22SFrançois Tigeot 	return false;
34d78d3a22SFrançois Tigeot }
35d78d3a22SFrançois Tigeot 
is_pcie_gen2_supported(uint32_t pcie_link_speed_cap)36d78d3a22SFrançois Tigeot static inline bool is_pcie_gen2_supported(uint32_t pcie_link_speed_cap)
37d78d3a22SFrançois Tigeot {
38d78d3a22SFrançois Tigeot 	if (pcie_link_speed_cap & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)
39d78d3a22SFrançois Tigeot 		return true;
40d78d3a22SFrançois Tigeot 
41d78d3a22SFrançois Tigeot 	return false;
42d78d3a22SFrançois Tigeot }
43d78d3a22SFrançois Tigeot 
44d78d3a22SFrançois Tigeot /* Get the new PCIE speed given the ASIC PCIE Cap and the NewState's requested PCIE speed*/
get_pcie_gen_support(uint32_t pcie_link_speed_cap,uint16_t ns_pcie_gen)45d78d3a22SFrançois Tigeot static inline uint16_t get_pcie_gen_support(uint32_t pcie_link_speed_cap,
46d78d3a22SFrançois Tigeot 					    uint16_t ns_pcie_gen)
47d78d3a22SFrançois Tigeot {
48d78d3a22SFrançois Tigeot 	uint32_t asic_pcie_link_speed_cap = (pcie_link_speed_cap &
49d78d3a22SFrançois Tigeot 		CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_MASK);
50d78d3a22SFrançois Tigeot 	uint32_t sys_pcie_link_speed_cap  = (pcie_link_speed_cap &
51d78d3a22SFrançois Tigeot 		CAIL_PCIE_LINK_SPEED_SUPPORT_MASK);
52d78d3a22SFrançois Tigeot 
53d78d3a22SFrançois Tigeot 	switch (asic_pcie_link_speed_cap) {
54d78d3a22SFrançois Tigeot 	case CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1:
55d78d3a22SFrançois Tigeot 		return PP_PCIEGen1;
56d78d3a22SFrançois Tigeot 
57d78d3a22SFrançois Tigeot 	case CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2:
58d78d3a22SFrançois Tigeot 		return PP_PCIEGen2;
59d78d3a22SFrançois Tigeot 
60d78d3a22SFrançois Tigeot 	case CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3:
61d78d3a22SFrançois Tigeot 		return PP_PCIEGen3;
62d78d3a22SFrançois Tigeot 
63d78d3a22SFrançois Tigeot 	default:
64d78d3a22SFrançois Tigeot 		if (is_pcie_gen3_supported(sys_pcie_link_speed_cap) &&
65d78d3a22SFrançois Tigeot 			(ns_pcie_gen == PP_PCIEGen3)) {
66d78d3a22SFrançois Tigeot 			return PP_PCIEGen3;
67d78d3a22SFrançois Tigeot 		} else if (is_pcie_gen2_supported(sys_pcie_link_speed_cap) &&
68d78d3a22SFrançois Tigeot 			((ns_pcie_gen == PP_PCIEGen3) || (ns_pcie_gen == PP_PCIEGen2))) {
69d78d3a22SFrançois Tigeot 			return PP_PCIEGen2;
70d78d3a22SFrançois Tigeot 		}
71d78d3a22SFrançois Tigeot 	}
72d78d3a22SFrançois Tigeot 
73d78d3a22SFrançois Tigeot 	return PP_PCIEGen1;
74d78d3a22SFrançois Tigeot }
75d78d3a22SFrançois Tigeot 
get_pcie_lane_support(uint32_t pcie_lane_width_cap,uint16_t ns_pcie_lanes)76d78d3a22SFrançois Tigeot static inline uint16_t get_pcie_lane_support(uint32_t pcie_lane_width_cap,
77d78d3a22SFrançois Tigeot 					     uint16_t ns_pcie_lanes)
78d78d3a22SFrançois Tigeot {
79d78d3a22SFrançois Tigeot 	int i, j;
80d78d3a22SFrançois Tigeot 	uint16_t new_pcie_lanes = ns_pcie_lanes;
81d78d3a22SFrançois Tigeot 	uint16_t pcie_lanes[7] = {1, 2, 4, 8, 12, 16, 32};
82d78d3a22SFrançois Tigeot 
83d78d3a22SFrançois Tigeot 	switch (pcie_lane_width_cap) {
84d78d3a22SFrançois Tigeot 	case 0:
85*b843c749SSergey Zigachev 		pr_err("No valid PCIE lane width reported\n");
86d78d3a22SFrançois Tigeot 		break;
87d78d3a22SFrançois Tigeot 	case CAIL_PCIE_LINK_WIDTH_SUPPORT_X1:
88d78d3a22SFrançois Tigeot 		new_pcie_lanes = 1;
89d78d3a22SFrançois Tigeot 		break;
90d78d3a22SFrançois Tigeot 	case CAIL_PCIE_LINK_WIDTH_SUPPORT_X2:
91d78d3a22SFrançois Tigeot 		new_pcie_lanes = 2;
92d78d3a22SFrançois Tigeot 		break;
93d78d3a22SFrançois Tigeot 	case CAIL_PCIE_LINK_WIDTH_SUPPORT_X4:
94d78d3a22SFrançois Tigeot 		new_pcie_lanes = 4;
95d78d3a22SFrançois Tigeot 		break;
96d78d3a22SFrançois Tigeot 	case CAIL_PCIE_LINK_WIDTH_SUPPORT_X8:
97d78d3a22SFrançois Tigeot 		new_pcie_lanes = 8;
98d78d3a22SFrançois Tigeot 		break;
99d78d3a22SFrançois Tigeot 	case CAIL_PCIE_LINK_WIDTH_SUPPORT_X12:
100d78d3a22SFrançois Tigeot 		new_pcie_lanes = 12;
101d78d3a22SFrançois Tigeot 		break;
102d78d3a22SFrançois Tigeot 	case CAIL_PCIE_LINK_WIDTH_SUPPORT_X16:
103d78d3a22SFrançois Tigeot 		new_pcie_lanes = 16;
104d78d3a22SFrançois Tigeot 		break;
105d78d3a22SFrançois Tigeot 	case CAIL_PCIE_LINK_WIDTH_SUPPORT_X32:
106d78d3a22SFrançois Tigeot 		new_pcie_lanes = 32;
107d78d3a22SFrançois Tigeot 		break;
108d78d3a22SFrançois Tigeot 	default:
109d78d3a22SFrançois Tigeot 		for (i = 0; i < 7; i++) {
110d78d3a22SFrançois Tigeot 			if (ns_pcie_lanes == pcie_lanes[i]) {
111d78d3a22SFrançois Tigeot 				if (pcie_lane_width_cap & (0x10000 << i)) {
112d78d3a22SFrançois Tigeot 					break;
113d78d3a22SFrançois Tigeot 				} else {
114d78d3a22SFrançois Tigeot 					for (j = i - 1; j >= 0; j--) {
115d78d3a22SFrançois Tigeot 						if (pcie_lane_width_cap & (0x10000 << j)) {
116d78d3a22SFrançois Tigeot 							new_pcie_lanes = pcie_lanes[j];
117d78d3a22SFrançois Tigeot 							break;
118d78d3a22SFrançois Tigeot 						}
119d78d3a22SFrançois Tigeot 					}
120d78d3a22SFrançois Tigeot 
121d78d3a22SFrançois Tigeot 					if (j < 0) {
122d78d3a22SFrançois Tigeot 						for (j = i + 1; j < 7; j++) {
123d78d3a22SFrançois Tigeot 							if (pcie_lane_width_cap & (0x10000 << j)) {
124d78d3a22SFrançois Tigeot 								new_pcie_lanes = pcie_lanes[j];
125d78d3a22SFrançois Tigeot 								break;
126d78d3a22SFrançois Tigeot 							}
127d78d3a22SFrançois Tigeot 						}
128d78d3a22SFrançois Tigeot 						if (j > 7)
129*b843c749SSergey Zigachev 							pr_err("Cannot find a valid PCIE lane width!\n");
130d78d3a22SFrançois Tigeot 					}
131d78d3a22SFrançois Tigeot 				}
132d78d3a22SFrançois Tigeot 				break;
133d78d3a22SFrançois Tigeot 			}
134d78d3a22SFrançois Tigeot 		}
135d78d3a22SFrançois Tigeot 		break;
136d78d3a22SFrançois Tigeot 	}
137d78d3a22SFrançois Tigeot 
138d78d3a22SFrançois Tigeot 	return new_pcie_lanes;
139d78d3a22SFrançois Tigeot }
140d78d3a22SFrançois Tigeot 
141d78d3a22SFrançois Tigeot #endif
142