blob: 853e9e1acb504b78bc5ef08b72303e6388a64828 [file] [log] [blame]
Soren Brinkmann76fcae32016-03-06 20:16:27 -08001/*
Michal Simek2a47faa2023-04-14 08:43:51 +02002 * Copyright (c) 2013-2018, Arm Limited and Contributors. All rights reserved.
Jay Buddhabhatti26e138a2022-12-21 23:03:35 -08003 * Copyright (c) 2022-2023, Advanced Micro Devices, Inc. All rights reserved.
Soren Brinkmann76fcae32016-03-06 20:16:27 -08004 *
dp-armfa3cf0b2017-05-03 09:38:09 +01005 * SPDX-License-Identifier: BSD-3-Clause
Soren Brinkmann76fcae32016-03-06 20:16:27 -08006 */
7
8/*
9 * APU specific definition of processors in the subsystem as well as functions
10 * for getting information about and changing state of the APU.
11 */
12
Filip Drazic4c0765a2016-07-26 12:11:33 +020013#include <assert.h>
Filip Drazic4c0765a2016-07-26 12:11:33 +020014#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015
16#include <common/bl_common.h>
17#include <drivers/arm/gic_common.h>
18#include <drivers/arm/gicv2.h>
19#include <lib/bakery_lock.h>
20#include <lib/mmio.h>
21#include <lib/utils.h>
22
Jolly Shah259afe52019-01-09 12:37:57 -080023#include <plat_ipi.h>
Jolly Shah16fe5ab2019-01-08 11:16:16 -080024#include <zynqmp_def.h>
Soren Brinkmann76fcae32016-03-06 20:16:27 -080025#include "pm_client.h"
26#include "pm_ipi.h"
Jay Buddhabhatti26e138a2022-12-21 23:03:35 -080027#include "zynqmp_pm_api_sys.h"
Soren Brinkmann76fcae32016-03-06 20:16:27 -080028
HariBabu Gattemb0c70f52022-09-29 23:59:11 -070029#define IRQ_MAX 84U
30#define NUM_GICD_ISENABLER ((IRQ_MAX >> 5U) + 1U)
31#define UNDEFINED_CPUID (~0U)
Filip Drazic0bd9d0c2016-07-20 17:17:39 +020032
HariBabu Gattemb0c70f52022-09-29 23:59:11 -070033#define PM_SUSPEND_MODE_STD 0U
34#define PM_SUSPEND_MODE_POWER_OFF 1U
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +053035
Stefan Krsmanovic2b783662016-05-20 15:51:09 +020036DEFINE_BAKERY_LOCK(pm_client_secure_lock);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080037
Michal Simek7b6eaba2023-04-25 12:40:41 +020038static const struct pm_ipi apu_ipi = {
Michal Simek3a63f052023-04-25 14:21:20 +020039 .local_ipi_id = IPI_LOCAL_ID,
40 .remote_ipi_id = IPI_REMOTE_ID,
Michal Simek9a8da502023-04-25 14:04:02 +020041 .buffer_base = IPI_BUFFER_LOCAL_BASE,
Jolly Shah259afe52019-01-09 12:37:57 -080042};
43
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +053044static uint32_t suspend_mode = PM_SUSPEND_MODE_STD;
45
Soren Brinkmann76fcae32016-03-06 20:16:27 -080046/* Order in pm_procs_all array must match cpu ids */
Soren Brinkmann4ae19c42017-07-01 20:24:47 -070047static const struct pm_proc pm_procs_all[] = {
Soren Brinkmann76fcae32016-03-06 20:16:27 -080048 {
49 .node_id = NODE_APU_0,
50 .pwrdn_mask = APU_0_PWRCTL_CPUPWRDWNREQ_MASK,
51 .ipi = &apu_ipi,
52 },
53 {
54 .node_id = NODE_APU_1,
55 .pwrdn_mask = APU_1_PWRCTL_CPUPWRDWNREQ_MASK,
56 .ipi = &apu_ipi,
57 },
58 {
59 .node_id = NODE_APU_2,
60 .pwrdn_mask = APU_2_PWRCTL_CPUPWRDWNREQ_MASK,
61 .ipi = &apu_ipi,
62 },
63 {
64 .node_id = NODE_APU_3,
65 .pwrdn_mask = APU_3_PWRCTL_CPUPWRDWNREQ_MASK,
66 .ipi = &apu_ipi,
67 },
68};
69
Filip Drazic4c0765a2016-07-26 12:11:33 +020070/* Interrupt to PM node ID map */
HariBabu Gattemb0c70f52022-09-29 23:59:11 -070071static enum pm_node_id irq_node_map[IRQ_MAX + 1U] = {
Filip Drazic4c0765a2016-07-26 12:11:33 +020072 NODE_UNKNOWN,
73 NODE_UNKNOWN,
74 NODE_UNKNOWN,
75 NODE_UNKNOWN, /* 3 */
76 NODE_UNKNOWN,
77 NODE_UNKNOWN,
78 NODE_UNKNOWN,
79 NODE_UNKNOWN, /* 7 */
80 NODE_UNKNOWN,
81 NODE_UNKNOWN,
82 NODE_UNKNOWN,
83 NODE_UNKNOWN, /* 11 */
84 NODE_UNKNOWN,
85 NODE_UNKNOWN,
86 NODE_NAND,
87 NODE_QSPI, /* 15 */
88 NODE_GPIO,
89 NODE_I2C_0,
90 NODE_I2C_1,
91 NODE_SPI_0, /* 19 */
92 NODE_SPI_1,
93 NODE_UART_0,
94 NODE_UART_1,
95 NODE_CAN_0, /* 23 */
96 NODE_CAN_1,
97 NODE_UNKNOWN,
98 NODE_RTC,
99 NODE_RTC, /* 27 */
100 NODE_UNKNOWN,
101 NODE_UNKNOWN,
102 NODE_UNKNOWN,
103 NODE_UNKNOWN, /* 31 */
104 NODE_UNKNOWN,
105 NODE_UNKNOWN,
106 NODE_UNKNOWN,
107 NODE_UNKNOWN, /* 35, NODE_IPI_APU */
108 NODE_TTC_0,
109 NODE_TTC_0,
110 NODE_TTC_0,
111 NODE_TTC_1, /* 39 */
112 NODE_TTC_1,
113 NODE_TTC_1,
114 NODE_TTC_2,
115 NODE_TTC_2, /* 43 */
116 NODE_TTC_2,
117 NODE_TTC_3,
118 NODE_TTC_3,
119 NODE_TTC_3, /* 47 */
120 NODE_SD_0,
121 NODE_SD_1,
122 NODE_SD_0,
123 NODE_SD_1, /* 51 */
124 NODE_UNKNOWN,
125 NODE_UNKNOWN,
126 NODE_UNKNOWN,
127 NODE_UNKNOWN, /* 55 */
128 NODE_UNKNOWN,
129 NODE_ETH_0,
130 NODE_ETH_0,
131 NODE_ETH_1, /* 59 */
132 NODE_ETH_1,
133 NODE_ETH_2,
134 NODE_ETH_2,
135 NODE_ETH_3, /* 63 */
136 NODE_ETH_3,
137 NODE_USB_0,
138 NODE_USB_0,
139 NODE_USB_0, /* 67 */
140 NODE_USB_0,
141 NODE_USB_0,
142 NODE_USB_1,
143 NODE_USB_1, /* 71 */
144 NODE_USB_1,
145 NODE_USB_1,
146 NODE_USB_1,
147 NODE_USB_0, /* 75 */
148 NODE_USB_0,
149 NODE_ADMA,
150 NODE_ADMA,
151 NODE_ADMA, /* 79 */
152 NODE_ADMA,
153 NODE_ADMA,
154 NODE_ADMA,
155 NODE_ADMA, /* 83 */
156 NODE_ADMA,
157};
158
159/**
160 * irq_to_pm_node - Get PM node ID corresponding to the interrupt number
161 * @irq: Interrupt number
162 *
163 * Return: PM node ID corresponding to the specified interrupt
164 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530165static enum pm_node_id irq_to_pm_node(uint32_t irq)
Filip Drazic4c0765a2016-07-26 12:11:33 +0200166{
167 assert(irq <= IRQ_MAX);
168 return irq_node_map[irq];
169}
170
171/**
172 * pm_client_set_wakeup_sources - Set all slaves with enabled interrupts as wake
173 * sources in the PMU firmware
174 */
175static void pm_client_set_wakeup_sources(void)
176{
177 uint32_t reg_num;
HariBabu Gattemaa811712022-10-07 00:07:49 -0700178 uint8_t pm_wakeup_nodes_set[NODE_MAX] = { 0 };
HariBabu Gattemb0c70f52022-09-29 23:59:11 -0700179 uintptr_t isenabler1 = BASE_GICD_BASE + GICD_ISENABLER + 4U;
Filip Drazic4c0765a2016-07-26 12:11:33 +0200180
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +0530181 /* In case of power-off suspend, only NODE_EXTERN must be set */
182 if (suspend_mode == PM_SUSPEND_MODE_POWER_OFF) {
183 enum pm_ret_status ret;
184
HariBabu Gattemb0c70f52022-09-29 23:59:11 -0700185 ret = pm_set_wakeup_source(NODE_APU, NODE_EXTERN, 1U);
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +0530186 /**
187 * If NODE_EXTERN could not be set as wake source, proceed with
188 * standard suspend (no one will wake the system otherwise)
189 */
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530190 if (ret == PM_RET_SUCCESS) {
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +0530191 return;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530192 }
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +0530193 }
194
Douglas Raillarda8954fc2017-01-26 15:54:44 +0000195 zeromem(&pm_wakeup_nodes_set, sizeof(pm_wakeup_nodes_set));
Filip Drazic4c0765a2016-07-26 12:11:33 +0200196
HariBabu Gattemb0c70f52022-09-29 23:59:11 -0700197 for (reg_num = 0U; reg_num < NUM_GICD_ISENABLER; reg_num++) {
Filip Drazic4c0765a2016-07-26 12:11:33 +0200198 uint32_t base_irq = reg_num << ISENABLER_SHIFT;
HariBabu Gattemb0c70f52022-09-29 23:59:11 -0700199 uint32_t reg = mmio_read_32(isenabler1 + (reg_num << 2U));
Filip Drazic4c0765a2016-07-26 12:11:33 +0200200
HariBabu Gattemb0c70f52022-09-29 23:59:11 -0700201 if (reg == 0) {
Filip Drazic4c0765a2016-07-26 12:11:33 +0200202 continue;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530203 }
Filip Drazic4c0765a2016-07-26 12:11:33 +0200204
205 while (reg) {
206 enum pm_node_id node;
207 uint32_t idx, ret, irq, lowest_set = reg & (-reg);
208
209 idx = __builtin_ctz(lowest_set);
210 irq = base_irq + idx;
211
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530212 if (irq > IRQ_MAX) {
Filip Drazic4c0765a2016-07-26 12:11:33 +0200213 break;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530214 }
Filip Drazic4c0765a2016-07-26 12:11:33 +0200215
216 node = irq_to_pm_node(irq);
217 reg &= ~lowest_set;
218
Ronak Jainf8414672022-05-11 02:48:52 -0700219 if (node > NODE_UNKNOWN && node < NODE_MAX) {
220 if (pm_wakeup_nodes_set[node] == 0U) {
221 ret = pm_set_wakeup_source(NODE_APU, node, 1U);
222 pm_wakeup_nodes_set[node] = (ret == PM_RET_SUCCESS) ? 1U : 0U;
223 }
Filip Drazic4c0765a2016-07-26 12:11:33 +0200224 }
225 }
226 }
227}
228
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800229/**
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800230 * pm_get_proc() - returns pointer to the proc structure
231 * @cpuid: id of the cpu whose proc struct pointer should be returned
232 *
233 * Return: pointer to a proc structure if proc is found, otherwise NULL
234 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530235const struct pm_proc *pm_get_proc(uint32_t cpuid)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800236{
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530237 if (cpuid < ARRAY_SIZE(pm_procs_all)) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800238 return &pm_procs_all[cpuid];
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530239 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800240
241 return NULL;
242}
243
244/**
245 * pm_get_proc_by_node() - returns pointer to the proc structure
246 * @nid: node id of the processor
247 *
248 * Return: pointer to a proc structure if proc is found, otherwise NULL
249 */
250const struct pm_proc *pm_get_proc_by_node(enum pm_node_id nid)
251{
252 for (size_t i = 0; i < ARRAY_SIZE(pm_procs_all); i++) {
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530253 if (nid == pm_procs_all[i].node_id) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800254 return &pm_procs_all[i];
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530255 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800256 }
257 return NULL;
258}
259
260/**
261 * pm_get_cpuid() - get the local cpu ID for a global node ID
262 * @nid: node id of the processor
263 *
264 * Return: the cpu ID (starting from 0) for the subsystem
265 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530266static uint32_t pm_get_cpuid(enum pm_node_id nid)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800267{
268 for (size_t i = 0; i < ARRAY_SIZE(pm_procs_all); i++) {
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530269 if (pm_procs_all[i].node_id == nid) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800270 return i;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530271 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800272 }
273 return UNDEFINED_CPUID;
274}
275
276const struct pm_proc *primary_proc = &pm_procs_all[0];
277
278/**
279 * pm_client_suspend() - Client-specific suspend actions
280 *
281 * This function should contain any PU-specific actions
282 * required prior to sending suspend request to PMU
Filip Drazic4c0765a2016-07-26 12:11:33 +0200283 * Actions taken depend on the state system is suspending to.
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800284 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530285void pm_client_suspend(const struct pm_proc *proc, uint32_t state)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800286{
Stefan Krsmanovic2b783662016-05-20 15:51:09 +0200287 bakery_lock_get(&pm_client_secure_lock);
288
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530289 if (state == PM_STATE_SUSPEND_TO_RAM) {
Filip Drazic4c0765a2016-07-26 12:11:33 +0200290 pm_client_set_wakeup_sources();
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530291 }
Filip Drazic4c0765a2016-07-26 12:11:33 +0200292
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800293 /* Set powerdown request */
294 mmio_write_32(APU_PWRCTL, mmio_read_32(APU_PWRCTL) | proc->pwrdn_mask);
Stefan Krsmanovic2b783662016-05-20 15:51:09 +0200295
296 bakery_lock_release(&pm_client_secure_lock);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800297}
298
299
300/**
301 * pm_client_abort_suspend() - Client-specific abort-suspend actions
302 *
303 * This function should contain any PU-specific actions
304 * required for aborting a prior suspend request
305 */
306void pm_client_abort_suspend(void)
307{
308 /* Enable interrupts at processor level (for current cpu) */
309 gicv2_cpuif_enable();
Stefan Krsmanovic2b783662016-05-20 15:51:09 +0200310
311 bakery_lock_get(&pm_client_secure_lock);
312
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800313 /* Clear powerdown request */
314 mmio_write_32(APU_PWRCTL,
315 mmio_read_32(APU_PWRCTL) & ~primary_proc->pwrdn_mask);
Stefan Krsmanovic2b783662016-05-20 15:51:09 +0200316
317 bakery_lock_release(&pm_client_secure_lock);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800318}
319
320/**
321 * pm_client_wakeup() - Client-specific wakeup actions
322 *
323 * This function should contain any PU-specific actions
324 * required for waking up another APU core
325 */
326void pm_client_wakeup(const struct pm_proc *proc)
327{
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530328 uint32_t cpuid = pm_get_cpuid(proc->node_id);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800329
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530330 if (cpuid == UNDEFINED_CPUID) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800331 return;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530332 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800333
Stefan Krsmanovic2b783662016-05-20 15:51:09 +0200334 bakery_lock_get(&pm_client_secure_lock);
335
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800336 /* clear powerdown bit for affected cpu */
337 uint32_t val = mmio_read_32(APU_PWRCTL);
338 val &= ~(proc->pwrdn_mask);
339 mmio_write_32(APU_PWRCTL, val);
Stefan Krsmanovic2b783662016-05-20 15:51:09 +0200340
341 bakery_lock_release(&pm_client_secure_lock);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800342}
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +0530343
344enum pm_ret_status pm_set_suspend_mode(uint32_t mode)
345{
346 if ((mode != PM_SUSPEND_MODE_STD) &&
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530347 (mode != PM_SUSPEND_MODE_POWER_OFF)) {
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +0530348 return PM_RET_ERROR_ARGS;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530349 }
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +0530350
351 suspend_mode = mode;
352 return PM_RET_SUCCESS;
353}