xref: /linux/drivers/cdx/controller/mcdi_functions.c (revision 3d0fe49454652117522f60bfbefb978ba0e5300b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
4  */
5 
6 #include <linux/module.h>
7 
8 #include "mcdi.h"
9 #include "mcdi_functions.h"
10 
11 int cdx_mcdi_get_num_buses(struct cdx_mcdi *cdx)
12 {
13 	MCDI_DECLARE_BUF(outbuf, MC_CMD_CDX_BUS_ENUM_BUSES_OUT_LEN);
14 	size_t outlen;
15 	int ret;
16 
17 	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_BUS_ENUM_BUSES, NULL, 0,
18 			   outbuf, sizeof(outbuf), &outlen);
19 	if (ret)
20 		return ret;
21 
22 	if (outlen != MC_CMD_CDX_BUS_ENUM_BUSES_OUT_LEN)
23 		return -EIO;
24 
25 	return MCDI_DWORD(outbuf, CDX_BUS_ENUM_BUSES_OUT_BUS_COUNT);
26 }
27 
28 int cdx_mcdi_get_num_devs(struct cdx_mcdi *cdx, int bus_num)
29 {
30 	MCDI_DECLARE_BUF(outbuf, MC_CMD_CDX_BUS_ENUM_DEVICES_OUT_LEN);
31 	MCDI_DECLARE_BUF(inbuf, MC_CMD_CDX_BUS_ENUM_DEVICES_IN_LEN);
32 	size_t outlen;
33 	int ret;
34 
35 	MCDI_SET_DWORD(inbuf, CDX_BUS_ENUM_DEVICES_IN_BUS, bus_num);
36 
37 	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_BUS_ENUM_DEVICES, inbuf, sizeof(inbuf),
38 			   outbuf, sizeof(outbuf), &outlen);
39 	if (ret)
40 		return ret;
41 
42 	if (outlen != MC_CMD_CDX_BUS_ENUM_DEVICES_OUT_LEN)
43 		return -EIO;
44 
45 	return MCDI_DWORD(outbuf, CDX_BUS_ENUM_DEVICES_OUT_DEVICE_COUNT);
46 }
47 
48 int cdx_mcdi_get_dev_config(struct cdx_mcdi *cdx,
49 			    u8 bus_num, u8 dev_num,
50 			    struct cdx_dev_params *dev_params)
51 {
52 	MCDI_DECLARE_BUF(outbuf, MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_LEN);
53 	MCDI_DECLARE_BUF(inbuf, MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_IN_LEN);
54 	struct resource *res = &dev_params->res[0];
55 	size_t outlen;
56 	u32 req_id;
57 	int ret;
58 
59 	MCDI_SET_DWORD(inbuf, CDX_BUS_GET_DEVICE_CONFIG_IN_BUS, bus_num);
60 	MCDI_SET_DWORD(inbuf, CDX_BUS_GET_DEVICE_CONFIG_IN_DEVICE, dev_num);
61 
62 	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_BUS_GET_DEVICE_CONFIG, inbuf, sizeof(inbuf),
63 			   outbuf, sizeof(outbuf), &outlen);
64 	if (ret)
65 		return ret;
66 
67 	if (outlen != MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_LEN)
68 		return -EIO;
69 
70 	dev_params->bus_num = bus_num;
71 	dev_params->dev_num = dev_num;
72 
73 	req_id = MCDI_DWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_REQUESTER_ID);
74 	dev_params->req_id = req_id;
75 
76 	dev_params->res_count = 0;
77 	if (MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_SIZE) != 0) {
78 		res[dev_params->res_count].start =
79 			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_BASE);
80 		res[dev_params->res_count].end =
81 			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_BASE) +
82 				   MCDI_QWORD(outbuf,
83 					      CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_SIZE) - 1;
84 		res[dev_params->res_count].flags = IORESOURCE_MEM;
85 		dev_params->res_count++;
86 	}
87 
88 	if (MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_SIZE) != 0) {
89 		res[dev_params->res_count].start =
90 			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_BASE);
91 		res[dev_params->res_count].end =
92 			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_BASE) +
93 				   MCDI_QWORD(outbuf,
94 					      CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_SIZE) - 1;
95 		res[dev_params->res_count].flags = IORESOURCE_MEM;
96 		dev_params->res_count++;
97 	}
98 
99 	if (MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_SIZE) != 0) {
100 		res[dev_params->res_count].start =
101 			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_BASE);
102 		res[dev_params->res_count].end =
103 			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_BASE) +
104 				   MCDI_QWORD(outbuf,
105 					      CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_SIZE) - 1;
106 		res[dev_params->res_count].flags = IORESOURCE_MEM;
107 		dev_params->res_count++;
108 	}
109 
110 	if (MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_SIZE) != 0) {
111 		res[dev_params->res_count].start =
112 			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_BASE);
113 		res[dev_params->res_count].end =
114 			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_BASE) +
115 				   MCDI_QWORD(outbuf,
116 					      CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_SIZE) - 1;
117 		res[dev_params->res_count].flags = IORESOURCE_MEM;
118 		dev_params->res_count++;
119 	}
120 
121 	dev_params->vendor = MCDI_WORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_VENDOR_ID);
122 	dev_params->device = MCDI_WORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_DEVICE_ID);
123 	dev_params->subsys_vendor = MCDI_WORD(outbuf,
124 					      CDX_BUS_GET_DEVICE_CONFIG_OUT_SUBSYS_VENDOR_ID);
125 	dev_params->subsys_device = MCDI_WORD(outbuf,
126 					      CDX_BUS_GET_DEVICE_CONFIG_OUT_SUBSYS_DEVICE_ID);
127 	dev_params->class = MCDI_DWORD(outbuf,
128 				       CDX_BUS_GET_DEVICE_CONFIG_OUT_DEVICE_CLASS) & 0xFFFFFF;
129 	dev_params->revision = MCDI_BYTE(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_DEVICE_REVISION);
130 
131 	return 0;
132 }
133 
134 int cdx_mcdi_bus_enable(struct cdx_mcdi *cdx, u8 bus_num)
135 {
136 	MCDI_DECLARE_BUF(inbuf, MC_CMD_CDX_BUS_UP_IN_LEN);
137 	int ret;
138 
139 	MCDI_SET_DWORD(inbuf, CDX_BUS_UP_IN_BUS, bus_num);
140 	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_BUS_UP, inbuf, sizeof(inbuf),
141 			   NULL, 0, NULL);
142 
143 	return ret;
144 }
145 
146 int cdx_mcdi_bus_disable(struct cdx_mcdi *cdx, u8 bus_num)
147 {
148 	MCDI_DECLARE_BUF(inbuf, MC_CMD_CDX_BUS_DOWN_IN_LEN);
149 	int ret;
150 
151 	MCDI_SET_DWORD(inbuf, CDX_BUS_DOWN_IN_BUS, bus_num);
152 	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_BUS_DOWN, inbuf, sizeof(inbuf),
153 			   NULL, 0, NULL);
154 
155 	return ret;
156 }
157 
158 int cdx_mcdi_reset_device(struct cdx_mcdi *cdx, u8 bus_num, u8 dev_num)
159 {
160 	MCDI_DECLARE_BUF(inbuf, MC_CMD_CDX_DEVICE_RESET_IN_LEN);
161 	int ret;
162 
163 	MCDI_SET_DWORD(inbuf, CDX_DEVICE_RESET_IN_BUS, bus_num);
164 	MCDI_SET_DWORD(inbuf, CDX_DEVICE_RESET_IN_DEVICE, dev_num);
165 
166 	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_DEVICE_RESET, inbuf, sizeof(inbuf),
167 			   NULL, 0, NULL);
168 
169 	return ret;
170 }
171 
172 static int cdx_mcdi_ctrl_flag_get(struct cdx_mcdi *cdx, u8 bus_num,
173 				  u8 dev_num, u32 *flags)
174 {
175 	MCDI_DECLARE_BUF(inbuf, MC_CMD_CDX_DEVICE_CONTROL_GET_IN_LEN);
176 	MCDI_DECLARE_BUF(outbuf, MC_CMD_CDX_DEVICE_CONTROL_GET_OUT_LEN);
177 	size_t outlen;
178 	int ret;
179 
180 	MCDI_SET_DWORD(inbuf, CDX_DEVICE_CONTROL_GET_IN_BUS, bus_num);
181 	MCDI_SET_DWORD(inbuf, CDX_DEVICE_CONTROL_GET_IN_DEVICE, dev_num);
182 	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_DEVICE_CONTROL_GET, inbuf,
183 			   sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
184 	if (ret)
185 		return ret;
186 
187 	if (outlen != MC_CMD_CDX_DEVICE_CONTROL_GET_OUT_LEN)
188 		return -EIO;
189 
190 	*flags = MCDI_DWORD(outbuf, CDX_DEVICE_CONTROL_GET_OUT_FLAGS);
191 
192 	return 0;
193 }
194 
195 static int cdx_mcdi_ctrl_flag_set(struct cdx_mcdi *cdx, u8 bus_num,
196 				  u8 dev_num, bool enable, int bit_pos)
197 {
198 	MCDI_DECLARE_BUF(inbuf, MC_CMD_CDX_DEVICE_CONTROL_SET_IN_LEN);
199 	u32 flags;
200 	int ret;
201 
202 	/*
203 	 * Get flags and then set/reset bit at bit_pos according to
204 	 * the input params.
205 	 */
206 	ret = cdx_mcdi_ctrl_flag_get(cdx, bus_num, dev_num, &flags);
207 	if (ret)
208 		return ret;
209 
210 	flags = flags & (u32)(~(BIT(bit_pos)));
211 	if (enable)
212 		flags |= (1 << bit_pos);
213 
214 	MCDI_SET_DWORD(inbuf, CDX_DEVICE_CONTROL_SET_IN_BUS, bus_num);
215 	MCDI_SET_DWORD(inbuf, CDX_DEVICE_CONTROL_SET_IN_DEVICE, dev_num);
216 	MCDI_SET_DWORD(inbuf, CDX_DEVICE_CONTROL_SET_IN_FLAGS, flags);
217 	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_DEVICE_CONTROL_SET, inbuf,
218 			   sizeof(inbuf), NULL, 0, NULL);
219 
220 	return ret;
221 }
222 
223 int cdx_mcdi_bus_master_enable(struct cdx_mcdi *cdx, u8 bus_num,
224 			       u8 dev_num, bool enable)
225 {
226 	return cdx_mcdi_ctrl_flag_set(cdx, bus_num, dev_num, enable,
227 			MC_CMD_CDX_DEVICE_CONTROL_SET_IN_BUS_MASTER_ENABLE_LBN);
228 }
229