blob: 7910d8fc162d740536fef8c755d96ce60ad54f4e [file] [log] [blame]
Tejas Patelc73a90e2018-12-14 00:55:37 -08001/*
Tanmay Shah0b151872022-03-23 12:43:45 -07002 * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved.
Jay Buddhabhatti31b68482023-02-28 01:23:04 -08003 * Copyright (c) 2022-2023, Advanced Micro Devices, Inc. All rights reserved.
Tejas Patelc73a90e2018-12-14 00:55:37 -08004 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
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
Tejas Patel6e82cd82019-02-27 18:44:56 +053013#include <assert.h>
Tejas Patelc73a90e2018-12-14 00:55:37 -080014#include <plat_ipi.h>
15#include <platform_def.h>
16#include <versal_def.h>
17#include <lib/bakery_lock.h>
Tejas Patelfbb32692019-12-08 23:29:44 -080018#include <lib/mmio.h>
Tejas Patel6e82cd82019-02-27 18:44:56 +053019#include <lib/utils.h>
Tejas Patelfbb32692019-12-08 23:29:44 -080020#include <drivers/arm/gicv3.h>
Tejas Patel6e82cd82019-02-27 18:44:56 +053021#include <drivers/arm/gic_common.h>
Tejas Patelfbb32692019-12-08 23:29:44 -080022#include <plat/common/platform.h>
Tejas Patel6e82cd82019-02-27 18:44:56 +053023#include "pm_api_sys.h"
Tejas Patelc73a90e2018-12-14 00:55:37 -080024#include "pm_client.h"
Tanmay Shah0b151872022-03-23 12:43:45 -070025#include "pm_defs.h"
Tejas Patelc73a90e2018-12-14 00:55:37 -080026
Tejas Patel25b1a912019-02-27 18:44:54 +053027#define UNDEFINED_CPUID (~0)
Abhyuday Godhasara5d1c2112021-08-04 23:58:46 -070028#define IRQ_MAX 142U
29#define NUM_GICD_ISENABLER ((IRQ_MAX >> 5U) + 1U)
Tejas Patel25b1a912019-02-27 18:44:54 +053030
Tejas Patelc73a90e2018-12-14 00:55:37 -080031DEFINE_BAKERY_LOCK(pm_client_secure_lock);
32
33static const struct pm_ipi apu_ipi = {
34 .local_ipi_id = IPI_ID_APU,
35 .remote_ipi_id = IPI_ID_PMC,
36 .buffer_base = IPI_BUFFER_APU_BASE,
37};
38
39/* Order in pm_procs_all array must match cpu ids */
40static const struct pm_proc pm_procs_all[] = {
41 {
42 .node_id = XPM_DEVID_ACPU_0,
43 .ipi = &apu_ipi,
Tejas Patelfbb32692019-12-08 23:29:44 -080044 .pwrdn_mask = APU_0_PWRCTL_CPUPWRDWNREQ_MASK,
Tejas Patelc73a90e2018-12-14 00:55:37 -080045 },
46 {
47 .node_id = XPM_DEVID_ACPU_1,
48 .ipi = &apu_ipi,
Tejas Patelfbb32692019-12-08 23:29:44 -080049 .pwrdn_mask = APU_1_PWRCTL_CPUPWRDWNREQ_MASK,
Tejas Patelc73a90e2018-12-14 00:55:37 -080050 }
51};
52
53const struct pm_proc *primary_proc = &pm_procs_all[0];
Tejas Patelfbb32692019-12-08 23:29:44 -080054
Tejas Patel6e82cd82019-02-27 18:44:56 +053055/* Interrupt to PM node index map */
56static enum pm_device_node_idx irq_node_map[IRQ_MAX + 1] = {
57 [13] = XPM_NODEIDX_DEV_GPIO,
58 [14] = XPM_NODEIDX_DEV_I2C_0,
59 [15] = XPM_NODEIDX_DEV_I2C_1,
60 [16] = XPM_NODEIDX_DEV_SPI_0,
61 [17] = XPM_NODEIDX_DEV_SPI_1,
62 [18] = XPM_NODEIDX_DEV_UART_0,
63 [19] = XPM_NODEIDX_DEV_UART_1,
64 [20] = XPM_NODEIDX_DEV_CAN_FD_0,
65 [21] = XPM_NODEIDX_DEV_CAN_FD_1,
66 [22] = XPM_NODEIDX_DEV_USB_0,
67 [23] = XPM_NODEIDX_DEV_USB_0,
68 [24] = XPM_NODEIDX_DEV_USB_0,
69 [25] = XPM_NODEIDX_DEV_USB_0,
70 [26] = XPM_NODEIDX_DEV_USB_0,
71 [37] = XPM_NODEIDX_DEV_TTC_0,
72 [38] = XPM_NODEIDX_DEV_TTC_0,
73 [39] = XPM_NODEIDX_DEV_TTC_0,
74 [40] = XPM_NODEIDX_DEV_TTC_1,
75 [41] = XPM_NODEIDX_DEV_TTC_1,
76 [42] = XPM_NODEIDX_DEV_TTC_1,
77 [43] = XPM_NODEIDX_DEV_TTC_2,
78 [44] = XPM_NODEIDX_DEV_TTC_2,
79 [45] = XPM_NODEIDX_DEV_TTC_2,
80 [46] = XPM_NODEIDX_DEV_TTC_3,
81 [47] = XPM_NODEIDX_DEV_TTC_3,
82 [48] = XPM_NODEIDX_DEV_TTC_3,
83 [56] = XPM_NODEIDX_DEV_GEM_0,
84 [57] = XPM_NODEIDX_DEV_GEM_0,
85 [58] = XPM_NODEIDX_DEV_GEM_1,
86 [59] = XPM_NODEIDX_DEV_GEM_1,
87 [60] = XPM_NODEIDX_DEV_ADMA_0,
88 [61] = XPM_NODEIDX_DEV_ADMA_1,
89 [62] = XPM_NODEIDX_DEV_ADMA_2,
90 [63] = XPM_NODEIDX_DEV_ADMA_3,
91 [64] = XPM_NODEIDX_DEV_ADMA_4,
92 [65] = XPM_NODEIDX_DEV_ADMA_5,
93 [66] = XPM_NODEIDX_DEV_ADMA_6,
94 [67] = XPM_NODEIDX_DEV_ADMA_7,
95 [74] = XPM_NODEIDX_DEV_USB_0,
96 [126] = XPM_NODEIDX_DEV_SDIO_0,
97 [127] = XPM_NODEIDX_DEV_SDIO_0,
98 [128] = XPM_NODEIDX_DEV_SDIO_1,
99 [129] = XPM_NODEIDX_DEV_SDIO_1,
100 [142] = XPM_NODEIDX_DEV_RTC,
101};
102
103/**
104 * irq_to_pm_node_idx - Get PM node index corresponding to the interrupt number
105 * @irq: Interrupt number
106 *
107 * Return: PM node index corresponding to the specified interrupt
108 */
Venkatesh Yadav Abbarapu912b7a62022-05-24 11:11:12 +0530109static enum pm_device_node_idx irq_to_pm_node_idx(uint32_t irq)
Tejas Patel6e82cd82019-02-27 18:44:56 +0530110{
111 assert(irq <= IRQ_MAX);
112 return irq_node_map[irq];
113}
114
115/**
116 * pm_client_set_wakeup_sources - Set all devices with enabled interrupts as
117 * wake sources in the LibPM.
Ravi Patelb05d2792019-07-30 04:10:07 -0700118 * @node_id: Node id of processor
Tejas Patel6e82cd82019-02-27 18:44:56 +0530119 */
Ravi Patelb05d2792019-07-30 04:10:07 -0700120static void pm_client_set_wakeup_sources(uint32_t node_id)
Tejas Patel6e82cd82019-02-27 18:44:56 +0530121{
122 uint32_t reg_num;
123 uint32_t device_id;
Naman Patelcd73d622022-11-16 01:54:23 -0800124 uint8_t pm_wakeup_nodes_set[XPM_NODEIDX_DEV_MAX] = { 0U };
Jay Buddhabhatti31b68482023-02-28 01:23:04 -0800125 uintptr_t isenabler1 = PLAT_GICD_BASE_VALUE + GICD_ISENABLER + 4;
Tejas Patel6e82cd82019-02-27 18:44:56 +0530126
Abhyuday Godhasara5d1c2112021-08-04 23:58:46 -0700127 for (reg_num = 0U; reg_num < NUM_GICD_ISENABLER; reg_num++) {
Tejas Patel6e82cd82019-02-27 18:44:56 +0530128 uint32_t base_irq = reg_num << ISENABLER_SHIFT;
129 uint32_t reg = mmio_read_32(isenabler1 + (reg_num << 2));
130
Abhyuday Godhasara775bf1b2021-08-05 02:59:26 -0700131 if (reg == 0U) {
Tejas Patel6e82cd82019-02-27 18:44:56 +0530132 continue;
Abhyuday Godhasara41567192021-08-05 03:14:17 -0700133 }
Tejas Patel6e82cd82019-02-27 18:44:56 +0530134
Abhyuday Godhasara775bf1b2021-08-05 02:59:26 -0700135 while (reg != 0U) {
Tejas Patel6e82cd82019-02-27 18:44:56 +0530136 enum pm_device_node_idx node_idx;
Abhyuday Godhasarafa98d7f2021-08-05 03:05:22 -0700137 uint32_t idx, irq, lowest_set = reg & (-reg);
138 enum pm_ret_status ret;
Michal Simekbb1768c2022-03-09 08:53:20 +0100139
Tejas Patel6e82cd82019-02-27 18:44:56 +0530140 idx = __builtin_ctz(lowest_set);
141 irq = base_irq + idx;
142
Abhyuday Godhasara41567192021-08-05 03:14:17 -0700143 if (irq > IRQ_MAX) {
Tejas Patel6e82cd82019-02-27 18:44:56 +0530144 break;
Abhyuday Godhasara41567192021-08-05 03:14:17 -0700145 }
Tejas Patel6e82cd82019-02-27 18:44:56 +0530146
147 node_idx = irq_to_pm_node_idx(irq);
148 reg &= ~lowest_set;
149
Tanmay Shah0b151872022-03-23 12:43:45 -0700150 if (node_idx > XPM_NODEIDX_DEV_MIN && node_idx < XPM_NODEIDX_DEV_MAX) {
151 if (pm_wakeup_nodes_set[node_idx] == 0U) {
152 /* Get device ID from node index */
153 device_id = PERIPH_DEVID(node_idx);
154 ret = pm_set_wakeup_source(node_id,
155 device_id, 1,
156 SECURE_FLAG);
157 pm_wakeup_nodes_set[node_idx] = (ret == PM_RET_SUCCESS) ?
158 1 : 0;
159 }
Tejas Patel6e82cd82019-02-27 18:44:56 +0530160 }
161 }
162 }
163}
164
Tejas Patelfbb32692019-12-08 23:29:44 -0800165/**
166 * pm_client_suspend() - Client-specific suspend actions
167 *
168 * This function should contain any PU-specific actions
169 * required prior to sending suspend request to PMU
170 * Actions taken depend on the state system is suspending to.
171 */
Venkatesh Yadav Abbarapu912b7a62022-05-24 11:11:12 +0530172void pm_client_suspend(const struct pm_proc *proc, uint32_t state)
Tejas Patelfbb32692019-12-08 23:29:44 -0800173{
174 bakery_lock_get(&pm_client_secure_lock);
175
Abhyuday Godhasara41567192021-08-05 03:14:17 -0700176 if (state == PM_STATE_SUSPEND_TO_RAM) {
Abhyuday Godhasara5d1c2112021-08-04 23:58:46 -0700177 pm_client_set_wakeup_sources((uint32_t)proc->node_id);
Abhyuday Godhasara41567192021-08-05 03:14:17 -0700178 }
Tejas Patel6e82cd82019-02-27 18:44:56 +0530179
Tejas Patelfbb32692019-12-08 23:29:44 -0800180 /* Set powerdown request */
181 mmio_write_32(FPD_APU_PWRCTL, mmio_read_32(FPD_APU_PWRCTL) |
Abhyuday Godhasara775bf1b2021-08-05 02:59:26 -0700182 (uint32_t)proc->pwrdn_mask);
Tejas Patelfbb32692019-12-08 23:29:44 -0800183
184 bakery_lock_release(&pm_client_secure_lock);
185}
186
187/**
188 * pm_client_abort_suspend() - Client-specific abort-suspend actions
189 *
190 * This function should contain any PU-specific actions
191 * required for aborting a prior suspend request
192 */
193void pm_client_abort_suspend(void)
194{
195 /* Enable interrupts at processor level (for current cpu) */
196 gicv3_cpuif_enable(plat_my_core_pos());
197
198 bakery_lock_get(&pm_client_secure_lock);
199
200 /* Clear powerdown request */
201 mmio_write_32(FPD_APU_PWRCTL, mmio_read_32(FPD_APU_PWRCTL) &
Abhyuday Godhasara775bf1b2021-08-05 02:59:26 -0700202 ~((uint32_t)primary_proc->pwrdn_mask));
Tejas Patelfbb32692019-12-08 23:29:44 -0800203
204 bakery_lock_release(&pm_client_secure_lock);
205}
206
207/**
Tejas Patel25b1a912019-02-27 18:44:54 +0530208 * pm_get_cpuid() - get the local cpu ID for a global node ID
209 * @nid: node id of the processor
210 *
211 * Return: the cpu ID (starting from 0) for the subsystem
212 */
Venkatesh Yadav Abbarapu912b7a62022-05-24 11:11:12 +0530213static uint32_t pm_get_cpuid(uint32_t nid)
Tejas Patel25b1a912019-02-27 18:44:54 +0530214{
Abhyuday Godhasara5d1c2112021-08-04 23:58:46 -0700215 for (size_t i = 0U; i < ARRAY_SIZE(pm_procs_all); i++) {
Abhyuday Godhasara41567192021-08-05 03:14:17 -0700216 if (pm_procs_all[i].node_id == nid) {
Tejas Patel25b1a912019-02-27 18:44:54 +0530217 return i;
Abhyuday Godhasara41567192021-08-05 03:14:17 -0700218 }
Tejas Patel25b1a912019-02-27 18:44:54 +0530219 }
220 return UNDEFINED_CPUID;
221}
222
223/**
224 * pm_client_wakeup() - Client-specific wakeup actions
225 *
226 * This function should contain any PU-specific actions
227 * required for waking up another APU core
228 */
229void pm_client_wakeup(const struct pm_proc *proc)
230{
Venkatesh Yadav Abbarapu912b7a62022-05-24 11:11:12 +0530231 uint32_t cpuid = pm_get_cpuid(proc->node_id);
Tejas Patel25b1a912019-02-27 18:44:54 +0530232
Abhyuday Godhasara41567192021-08-05 03:14:17 -0700233 if (cpuid == UNDEFINED_CPUID) {
Tejas Patel25b1a912019-02-27 18:44:54 +0530234 return;
Abhyuday Godhasara41567192021-08-05 03:14:17 -0700235 }
Tejas Patel25b1a912019-02-27 18:44:54 +0530236
237 bakery_lock_get(&pm_client_secure_lock);
238
239 /* clear powerdown bit for affected cpu */
240 uint32_t val = mmio_read_32(FPD_APU_PWRCTL);
241 val &= ~(proc->pwrdn_mask);
242 mmio_write_32(FPD_APU_PWRCTL, val);
243
244 bakery_lock_release(&pm_client_secure_lock);
245}
246
247/**
Tejas Patelfbb32692019-12-08 23:29:44 -0800248 * pm_get_proc() - returns pointer to the proc structure
249 * @cpuid: id of the cpu whose proc struct pointer should be returned
250 *
251 * Return: pointer to a proc structure if proc is found, otherwise NULL
252 */
Venkatesh Yadav Abbarapu912b7a62022-05-24 11:11:12 +0530253const struct pm_proc *pm_get_proc(uint32_t cpuid)
Tejas Patelfbb32692019-12-08 23:29:44 -0800254{
Abhyuday Godhasara41567192021-08-05 03:14:17 -0700255 if (cpuid < ARRAY_SIZE(pm_procs_all)) {
Tejas Patelfbb32692019-12-08 23:29:44 -0800256 return &pm_procs_all[cpuid];
Abhyuday Godhasara41567192021-08-05 03:14:17 -0700257 }
Tejas Patelfbb32692019-12-08 23:29:44 -0800258
259 return NULL;
260}