Prepare v2023.10
[platform/kernel/u-boot.git] / drivers / firmware / firmware-zynqmp.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Xilinx Zynq MPSoC Firmware driver
4  *
5  * Copyright (C) 2018-2019 Xilinx, Inc.
6  */
7
8 #include <common.h>
9 #include <cpu_func.h>
10 #include <dm.h>
11 #include <dm/lists.h>
12 #include <log.h>
13 #include <zynqmp_firmware.h>
14 #include <asm/cache.h>
15 #include <asm/ptrace.h>
16
17 #if defined(CONFIG_ZYNQMP_IPI)
18 #include <mailbox.h>
19 #include <asm/arch/sys_proto.h>
20
21 #define PMUFW_PAYLOAD_ARG_CNT   8
22
23 #define XST_PM_NO_ACCESS        2002L
24 #define XST_PM_ALREADY_CONFIGURED       2009L
25
26 static struct zynqmp_power {
27         struct mbox_chan tx_chan;
28         struct mbox_chan rx_chan;
29 } zynqmp_power __section(".data");
30
31 #define NODE_ID_LOCATION        5
32
33 static unsigned int xpm_configobject[] = {
34         /**********************************************************************/
35         /* HEADER */
36         2,      /* Number of remaining words in the header */
37         1,      /* Number of sections included in config object */
38         PM_CONFIG_OBJECT_TYPE_OVERLAY,  /* Type of Config object as overlay */
39         /**********************************************************************/
40         /* SLAVE SECTION */
41
42         PM_CONFIG_SLAVE_SECTION_ID,     /* Section ID */
43         1,                              /* Number of slaves */
44
45         0, /* Node ID which will be changed below */
46         PM_SLAVE_FLAG_IS_SHAREABLE,
47         PM_CONFIG_IPI_PSU_CORTEXA53_0_MASK |
48         PM_CONFIG_IPI_PSU_CORTEXR5_0_MASK |
49         PM_CONFIG_IPI_PSU_CORTEXR5_1_MASK, /* IPI Mask */
50 };
51
52 static unsigned int xpm_configobject_close[] = {
53         /**********************************************************************/
54         /* HEADER */
55         2,      /* Number of remaining words in the header */
56         1,      /* Number of sections included in config object */
57         PM_CONFIG_OBJECT_TYPE_OVERLAY,  /* Type of Config object as overlay */
58         /**********************************************************************/
59         /* SET CONFIG SECTION */
60         PM_CONFIG_SET_CONFIG_SECTION_ID,
61         0U,     /* Loading permission to Overlay config object */
62 };
63
64 int zynqmp_pmufw_config_close(void)
65 {
66         return zynqmp_pmufw_load_config_object(xpm_configobject_close,
67                                                sizeof(xpm_configobject_close));
68 }
69
70 int zynqmp_pmufw_node(u32 id)
71 {
72         static bool check = true;
73         static bool permission = true;
74
75         if (check) {
76                 check = false;
77
78                 if (zynqmp_pmufw_node(NODE_OCM_BANK_0) == -EACCES) {
79                         printf("PMUFW:  No permission to change config object\n");
80                         permission = false;
81                 }
82         }
83
84         if (!permission)
85                 return 0;
86
87         /* Record power domain id */
88         xpm_configobject[NODE_ID_LOCATION] = id;
89
90         return zynqmp_pmufw_load_config_object(xpm_configobject,
91                                                sizeof(xpm_configobject));
92 }
93
94 static int do_pm_probe(void)
95 {
96         struct udevice *dev;
97         int ret;
98
99         ret = uclass_get_device_by_driver(UCLASS_FIRMWARE,
100                                           DM_DRIVER_GET(zynqmp_power),
101                                           &dev);
102         if (ret)
103                 debug("%s: Probing device failed: %d\n", __func__, ret);
104
105         return ret;
106 }
107
108 static int ipi_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
109 {
110         struct zynqmp_ipi_msg msg;
111         int ret;
112         u32 buffer[PAYLOAD_ARG_CNT];
113
114         if (!res)
115                 res = buffer;
116
117         if (req_len > PMUFW_PAYLOAD_ARG_CNT ||
118             res_maxlen > PMUFW_PAYLOAD_ARG_CNT)
119                 return -EINVAL;
120
121         if (!(zynqmp_power.tx_chan.dev) || !(zynqmp_power.rx_chan.dev)) {
122                 ret = do_pm_probe();
123                 if (ret)
124                         return ret;
125         }
126
127         debug("%s, Sending IPI message with ID: 0x%0x\n", __func__, req[0]);
128         msg.buf = (u32 *)req;
129         msg.len = req_len;
130         ret = mbox_send(&zynqmp_power.tx_chan, &msg);
131         if (ret) {
132                 debug("%s: Sending message failed\n", __func__);
133                 return ret;
134         }
135
136         msg.buf = res;
137         msg.len = res_maxlen;
138         ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100);
139         if (ret)
140                 debug("%s: Receiving message failed\n", __func__);
141
142         return ret;
143 }
144
145 unsigned int zynqmp_firmware_version(void)
146 {
147         int ret;
148         u32 ret_payload[PAYLOAD_ARG_CNT];
149         static u32 pm_api_version = ZYNQMP_PM_VERSION_INVALID;
150
151         /*
152          * Get PMU version only once and later
153          * just return stored values instead of
154          * asking PMUFW again.
155          **/
156         if (pm_api_version == ZYNQMP_PM_VERSION_INVALID) {
157
158                 ret = xilinx_pm_request(PM_GET_API_VERSION, 0, 0, 0, 0,
159                                         ret_payload);
160                 if (ret)
161                         panic("PMUFW is not found - Please load it!\n");
162
163                 pm_api_version = ret_payload[1];
164                 if (pm_api_version < ZYNQMP_PM_VERSION)
165                         panic("PMUFW version error. Expected: v%d.%d\n",
166                               ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR);
167         }
168
169         return pm_api_version;
170 };
171
172 int zynqmp_pm_set_gem_config(u32 node, enum pm_gem_config_type config, u32 value)
173 {
174         int ret;
175
176         ret = xilinx_pm_request(PM_IOCTL, node, IOCTL_SET_GEM_CONFIG,
177                                 config, value, NULL);
178         if (ret)
179                 printf("%s: node %d: set_gem_config %d failed\n",
180                        __func__, node, config);
181
182         return ret;
183 }
184
185 int zynqmp_pm_set_sd_config(u32 node, enum pm_sd_config_type config, u32 value)
186 {
187         int ret;
188
189         ret = xilinx_pm_request(PM_IOCTL, node, IOCTL_SET_SD_CONFIG,
190                                 config, value, NULL);
191         if (ret)
192                 printf("%s: node %d: set_sd_config %d failed\n",
193                        __func__, node, config);
194
195         return ret;
196 }
197
198 int zynqmp_pm_is_function_supported(const u32 api_id, const u32 id)
199 {
200         int ret;
201         u32 *bit_mask;
202         u32 ret_payload[PAYLOAD_ARG_CNT];
203
204         /* Input arguments validation */
205         if (id >= 64 || (api_id != PM_IOCTL && api_id != PM_QUERY_DATA))
206                 return -EINVAL;
207
208         /* Check feature check API version */
209         ret = xilinx_pm_request(PM_FEATURE_CHECK, PM_FEATURE_CHECK, 0, 0, 0,
210                                 ret_payload);
211         if (ret)
212                 return ret;
213
214         /* Check if feature check version 2 is supported or not */
215         if ((ret_payload[1] & FIRMWARE_VERSION_MASK) == PM_API_VERSION_2) {
216                 /*
217                  * Call feature check for IOCTL/QUERY API to get IOCTL ID or
218                  * QUERY ID feature status.
219                  */
220
221                 ret = xilinx_pm_request(PM_FEATURE_CHECK, api_id, 0, 0, 0,
222                                         ret_payload);
223                 if (ret)
224                         return ret;
225
226                 bit_mask = &ret_payload[2];
227                 if ((bit_mask[(id / 32)] & BIT((id % 32))) == 0)
228                         return -EOPNOTSUPP;
229         } else {
230                 return -ENODATA;
231         }
232
233         return 0;
234 }
235
236 /**
237  * Send a configuration object to the PMU firmware.
238  *
239  * @cfg_obj: Pointer to the configuration object
240  * @size:    Size of @cfg_obj in bytes
241  * Return:   0 on success otherwise negative errno.
242  */
243 int zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size)
244 {
245         int err;
246         u32 ret_payload[PAYLOAD_ARG_CNT];
247
248         if (IS_ENABLED(CONFIG_SPL_BUILD))
249                 printf("Loading new PMUFW cfg obj (%ld bytes)\n", size);
250
251         flush_dcache_range((ulong)cfg_obj, (ulong)(cfg_obj + size));
252
253         err = xilinx_pm_request(PM_SET_CONFIGURATION, (u32)(u64)cfg_obj, 0, 0,
254                                 0, ret_payload);
255         if (err == XST_PM_NO_ACCESS) {
256                 return -EACCES;
257         }
258
259         if (err == XST_PM_ALREADY_CONFIGURED) {
260                 debug("PMUFW Node is already configured\n");
261                 return -ENODEV;
262         }
263
264         if (err)
265                 printf("Cannot load PMUFW configuration object (%d)\n", err);
266
267         if (ret_payload[0])
268                 printf("PMUFW returned 0x%08x status!\n", ret_payload[0]);
269
270         if ((err || ret_payload[0]) && IS_ENABLED(CONFIG_SPL_BUILD))
271                 panic("PMUFW config object loading failed in EL3\n");
272
273         return 0;
274 }
275
276 static int zynqmp_power_probe(struct udevice *dev)
277 {
278         int ret;
279
280         debug("%s, (dev=%p)\n", __func__, dev);
281
282         ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan);
283         if (ret) {
284                 debug("%s: Cannot find tx mailbox\n", __func__);
285                 return ret;
286         }
287
288         ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan);
289         if (ret) {
290                 debug("%s: Cannot find rx mailbox\n", __func__);
291                 return ret;
292         }
293
294         ret = zynqmp_firmware_version();
295         printf("PMUFW:\tv%d.%d\n",
296                ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
297                ret & ZYNQMP_PM_VERSION_MINOR_MASK);
298
299         return 0;
300 };
301
302 static const struct udevice_id zynqmp_power_ids[] = {
303         { .compatible = "xlnx,zynqmp-power" },
304         { }
305 };
306
307 U_BOOT_DRIVER(zynqmp_power) = {
308         .name = "zynqmp_power",
309         .id = UCLASS_FIRMWARE,
310         .of_match = zynqmp_power_ids,
311         .probe = zynqmp_power_probe,
312 };
313 #endif
314
315 int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
316                                      u32 arg3, u32 *ret_payload)
317 {
318         debug("%s at EL%d, API ID: 0x%0x, 0x%0x, 0x%0x, 0x%0x, 0x%0x\n",
319               __func__, current_el(), api_id, arg0, arg1, arg2, arg3);
320
321         if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
322 #if defined(CONFIG_ZYNQMP_IPI)
323                 /*
324                  * Use fixed payload and arg size as the EL2 call. The firmware
325                  * is capable to handle PMUFW_PAYLOAD_ARG_CNT bytes but the
326                  * firmware API is limited by the SMC call size
327                  */
328                 u32 regs[] = {api_id, arg0, arg1, arg2, arg3};
329                 int ret;
330
331                 if (api_id == PM_FPGA_LOAD) {
332                         /* Swap addr_hi/low because of incompatibility */
333                         u32 temp = regs[1];
334
335                         regs[1] = regs[2];
336                         regs[2] = temp;
337                 }
338
339                 ret = ipi_req(regs, PAYLOAD_ARG_CNT, ret_payload,
340                               PAYLOAD_ARG_CNT);
341                 if (ret)
342                         return ret;
343 #else
344                 return -EPERM;
345 #endif
346         } else {
347                 /*
348                  * Added SIP service call Function Identifier
349                  * Make sure to stay in x0 register
350                  */
351                 struct pt_regs regs;
352
353                 regs.regs[0] = PM_SIP_SVC | api_id;
354                 regs.regs[1] = ((u64)arg1 << 32) | arg0;
355                 regs.regs[2] = ((u64)arg3 << 32) | arg2;
356
357                 smc_call(&regs);
358
359                 if (ret_payload) {
360                         ret_payload[0] = (u32)regs.regs[0];
361                         ret_payload[1] = upper_32_bits(regs.regs[0]);
362                         ret_payload[2] = (u32)regs.regs[1];
363                         ret_payload[3] = upper_32_bits(regs.regs[1]);
364                         ret_payload[4] = (u32)regs.regs[2];
365                 }
366
367         }
368         return (ret_payload) ? ret_payload[0] : 0;
369 }
370
371 static const struct udevice_id zynqmp_firmware_ids[] = {
372         { .compatible = "xlnx,zynqmp-firmware" },
373         { .compatible = "xlnx,versal-firmware"},
374         { .compatible = "xlnx,versal-net-firmware"},
375         { }
376 };
377
378 static int zynqmp_firmware_bind(struct udevice *dev)
379 {
380         int ret;
381         struct udevice *child;
382
383         if ((IS_ENABLED(CONFIG_SPL_BUILD) &&
384              IS_ENABLED(CONFIG_SPL_POWER_DOMAIN) &&
385              IS_ENABLED(CONFIG_ZYNQMP_POWER_DOMAIN)) ||
386              (!IS_ENABLED(CONFIG_SPL_BUILD) &&
387               IS_ENABLED(CONFIG_ZYNQMP_POWER_DOMAIN))) {
388                 ret = device_bind_driver_to_node(dev, "zynqmp_power_domain",
389                                                  "zynqmp_power_domain",
390                                                  dev_ofnode(dev), &child);
391                 if (ret) {
392                         printf("zynqmp power domain driver is not bound: %d\n", ret);
393                         return ret;
394                 }
395         }
396
397         return 0;
398 }
399
400 U_BOOT_DRIVER(zynqmp_firmware) = {
401         .id = UCLASS_FIRMWARE,
402         .name = "zynqmp_firmware",
403         .of_match = zynqmp_firmware_ids,
404         .bind = zynqmp_firmware_bind,
405 };