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 
124 	return 0;
125 }
126 
127 int cdx_mcdi_reset_device(struct cdx_mcdi *cdx, u8 bus_num, u8 dev_num)
128 {
129 	MCDI_DECLARE_BUF(inbuf, MC_CMD_CDX_DEVICE_RESET_IN_LEN);
130 	int ret;
131 
132 	MCDI_SET_DWORD(inbuf, CDX_DEVICE_RESET_IN_BUS, bus_num);
133 	MCDI_SET_DWORD(inbuf, CDX_DEVICE_RESET_IN_DEVICE, dev_num);
134 
135 	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_DEVICE_RESET, inbuf, sizeof(inbuf),
136 			   NULL, 0, NULL);
137 
138 	return ret;
139 }
140