blob: ce5e53384ae1ea1cd60cddd6304da84126d9a412 [file] [log] [blame]
Tejas Patel354fe572018-12-14 00:55:37 -08001/*
Tanmay Shahb145e122022-03-23 12:43:45 -07002 * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved.
Tejas Patel354fe572018-12-14 00:55:37 -08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/*
8 * APU specific definition of processors in the subsystem as well as functions
9 * for getting information about and changing state of the APU.
10 */
11
Tejas Patelfcb7c162019-02-27 18:44:56 +053012#include <assert.h>
Tejas Patel354fe572018-12-14 00:55:37 -080013#include <plat_ipi.h>
14#include <platform_def.h>
15#include <versal_def.h>
16#include <lib/bakery_lock.h>
Tejas Patelfe0e10a2019-12-08 23:29:44 -080017#include <lib/mmio.h>
Tejas Patelfcb7c162019-02-27 18:44:56 +053018#include <lib/utils.h>
Tejas Patelfe0e10a2019-12-08 23:29:44 -080019#include <drivers/arm/gicv3.h>
Tejas Patelfcb7c162019-02-27 18:44:56 +053020#include <drivers/arm/gic_common.h>
Tejas Patelfe0e10a2019-12-08 23:29:44 -080021#include <plat/common/platform.h>
Tejas Patelfcb7c162019-02-27 18:44:56 +053022#include "pm_api_sys.h"
Tejas Patel354fe572018-12-14 00:55:37 -080023#include "pm_client.h"
Tanmay Shahb145e122022-03-23 12:43:45 -070024#include "pm_defs.h"
Tejas Patel354fe572018-12-14 00:55:37 -080025
Tejas Patel7029dd82019-02-27 18:44:54 +053026#define UNDEFINED_CPUID (~0)
Abhyuday Godhasaraedc38ae2021-08-04 23:58:46 -070027#define IRQ_MAX 142U
28#define NUM_GICD_ISENABLER ((IRQ_MAX >> 5U) + 1U)
Tejas Patel7029dd82019-02-27 18:44:54 +053029
Tejas Patel354fe572018-12-14 00:55:37 -080030DEFINE_BAKERY_LOCK(pm_client_secure_lock);
31
32static const struct pm_ipi apu_ipi = {
33 .local_ipi_id = IPI_ID_APU,
34 .remote_ipi_id = IPI_ID_PMC,
35 .buffer_base = IPI_BUFFER_APU_BASE,
36};
37
38/* Order in pm_procs_all array must match cpu ids */
39static const struct pm_proc pm_procs_all[] = {
40 {
41 .node_id = XPM_DEVID_ACPU_0,
42 .ipi = &apu_ipi,
Tejas Patelfe0e10a2019-12-08 23:29:44 -080043 .pwrdn_mask = APU_0_PWRCTL_CPUPWRDWNREQ_MASK,
Tejas Patel354fe572018-12-14 00:55:37 -080044 },
45 {
46 .node_id = XPM_DEVID_ACPU_1,
47 .ipi = &apu_ipi,
Tejas Patelfe0e10a2019-12-08 23:29:44 -080048 .pwrdn_mask = APU_1_PWRCTL_CPUPWRDWNREQ_MASK,
Tejas Patel354fe572018-12-14 00:55:37 -080049 }
50};
51
52const struct pm_proc *primary_proc = &pm_procs_all[0];
Tejas Patelfe0e10a2019-12-08 23:29:44 -080053
Tejas Patelfcb7c162019-02-27 18:44:56 +053054/* Interrupt to PM node index map */
55static enum pm_device_node_idx irq_node_map[IRQ_MAX + 1] = {
56 [13] = XPM_NODEIDX_DEV_GPIO,
57 [14] = XPM_NODEIDX_DEV_I2C_0,
58 [15] = XPM_NODEIDX_DEV_I2C_1,
59 [16] = XPM_NODEIDX_DEV_SPI_0,
60 [17] = XPM_NODEIDX_DEV_SPI_1,
61 [18] = XPM_NODEIDX_DEV_UART_0,
62 [19] = XPM_NODEIDX_DEV_UART_1,
63 [20] = XPM_NODEIDX_DEV_CAN_FD_0,
64 [21] = XPM_NODEIDX_DEV_CAN_FD_1,
65 [22] = XPM_NODEIDX_DEV_USB_0,
66 [23] = XPM_NODEIDX_DEV_USB_0,
67 [24] = XPM_NODEIDX_DEV_USB_0,
68 [25] = XPM_NODEIDX_DEV_USB_0,
69 [26] = XPM_NODEIDX_DEV_USB_0,
70 [37] = XPM_NODEIDX_DEV_TTC_0,
71 [38] = XPM_NODEIDX_DEV_TTC_0,
72 [39] = XPM_NODEIDX_DEV_TTC_0,
73 [40] = XPM_NODEIDX_DEV_TTC_1,
74 [41] = XPM_NODEIDX_DEV_TTC_1,
75 [42] = XPM_NODEIDX_DEV_TTC_1,
76 [43] = XPM_NODEIDX_DEV_TTC_2,
77 [44] = XPM_NODEIDX_DEV_TTC_2,
78 [45] = XPM_NODEIDX_DEV_TTC_2,
79 [46] = XPM_NODEIDX_DEV_TTC_3,
80 [47] = XPM_NODEIDX_DEV_TTC_3,
81 [48] = XPM_NODEIDX_DEV_TTC_3,
82 [56] = XPM_NODEIDX_DEV_GEM_0,
83 [57] = XPM_NODEIDX_DEV_GEM_0,
84 [58] = XPM_NODEIDX_DEV_GEM_1,
85 [59] = XPM_NODEIDX_DEV_GEM_1,
86 [60] = XPM_NODEIDX_DEV_ADMA_0,
87 [61] = XPM_NODEIDX_DEV_ADMA_1,
88 [62] = XPM_NODEIDX_DEV_ADMA_2,
89 [63] = XPM_NODEIDX_DEV_ADMA_3,
90 [64] = XPM_NODEIDX_DEV_ADMA_4,
91 [65] = XPM_NODEIDX_DEV_ADMA_5,
92 [66] = XPM_NODEIDX_DEV_ADMA_6,
93 [67] = XPM_NODEIDX_DEV_ADMA_7,
94 [74] = XPM_NODEIDX_DEV_USB_0,
95 [126] = XPM_NODEIDX_DEV_SDIO_0,
96 [127] = XPM_NODEIDX_DEV_SDIO_0,
97 [128] = XPM_NODEIDX_DEV_SDIO_1,
98 [129] = XPM_NODEIDX_DEV_SDIO_1,
99 [142] = XPM_NODEIDX_DEV_RTC,
100};
101
102/**
103 * irq_to_pm_node_idx - Get PM node index corresponding to the interrupt number
104 * @irq: Interrupt number
105 *
106 * Return: PM node index corresponding to the specified interrupt
107 */
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530108static enum pm_device_node_idx irq_to_pm_node_idx(uint32_t irq)
Tejas Patelfcb7c162019-02-27 18:44:56 +0530109{
110 assert(irq <= IRQ_MAX);
111 return irq_node_map[irq];
112}
113
114/**
115 * pm_client_set_wakeup_sources - Set all devices with enabled interrupts as
116 * wake sources in the LibPM.
Ravi Patel023899d2019-07-30 04:10:07 -0700117 * @node_id: Node id of processor
Tejas Patelfcb7c162019-02-27 18:44:56 +0530118 */
Ravi Patel023899d2019-07-30 04:10:07 -0700119static void pm_client_set_wakeup_sources(uint32_t node_id)
Tejas Patelfcb7c162019-02-27 18:44:56 +0530120{
121 uint32_t reg_num;
122 uint32_t device_id;
123 uint8_t pm_wakeup_nodes_set[XPM_NODEIDX_DEV_MAX];
124 uintptr_t isenabler1 = PLAT_VERSAL_GICD_BASE + GICD_ISENABLER + 4;
125
Abhyuday Godhasara5c67ec12021-08-05 03:05:22 -0700126 zeromem(&pm_wakeup_nodes_set, (u_register_t)sizeof(pm_wakeup_nodes_set));
Tejas Patelfcb7c162019-02-27 18:44:56 +0530127
Abhyuday Godhasaraedc38ae2021-08-04 23:58:46 -0700128 for (reg_num = 0U; reg_num < NUM_GICD_ISENABLER; reg_num++) {
Tejas Patelfcb7c162019-02-27 18:44:56 +0530129 uint32_t base_irq = reg_num << ISENABLER_SHIFT;
130 uint32_t reg = mmio_read_32(isenabler1 + (reg_num << 2));
131
Abhyuday Godhasara44877942021-08-05 02:59:26 -0700132 if (reg == 0U) {
Tejas Patelfcb7c162019-02-27 18:44:56 +0530133 continue;
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700134 }
Tejas Patelfcb7c162019-02-27 18:44:56 +0530135
Abhyuday Godhasara44877942021-08-05 02:59:26 -0700136 while (reg != 0U) {
Tejas Patelfcb7c162019-02-27 18:44:56 +0530137 enum pm_device_node_idx node_idx;
Abhyuday Godhasara5c67ec12021-08-05 03:05:22 -0700138 uint32_t idx, irq, lowest_set = reg & (-reg);
139 enum pm_ret_status ret;
Michal Simek08341b72022-03-09 08:53:20 +0100140
Tejas Patelfcb7c162019-02-27 18:44:56 +0530141 idx = __builtin_ctz(lowest_set);
142 irq = base_irq + idx;
143
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700144 if (irq > IRQ_MAX) {
Tejas Patelfcb7c162019-02-27 18:44:56 +0530145 break;
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700146 }
Tejas Patelfcb7c162019-02-27 18:44:56 +0530147
148 node_idx = irq_to_pm_node_idx(irq);
149 reg &= ~lowest_set;
150
Tanmay Shahb145e122022-03-23 12:43:45 -0700151 if (node_idx > XPM_NODEIDX_DEV_MIN && node_idx < XPM_NODEIDX_DEV_MAX) {
152 if (pm_wakeup_nodes_set[node_idx] == 0U) {
153 /* Get device ID from node index */
154 device_id = PERIPH_DEVID(node_idx);
155 ret = pm_set_wakeup_source(node_id,
156 device_id, 1,
157 SECURE_FLAG);
158 pm_wakeup_nodes_set[node_idx] = (ret == PM_RET_SUCCESS) ?
159 1 : 0;
160 }
Tejas Patelfcb7c162019-02-27 18:44:56 +0530161 }
162 }
163 }
164}
165
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800166/**
167 * pm_client_suspend() - Client-specific suspend actions
168 *
169 * This function should contain any PU-specific actions
170 * required prior to sending suspend request to PMU
171 * Actions taken depend on the state system is suspending to.
172 */
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530173void pm_client_suspend(const struct pm_proc *proc, uint32_t state)
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800174{
175 bakery_lock_get(&pm_client_secure_lock);
176
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700177 if (state == PM_STATE_SUSPEND_TO_RAM) {
Abhyuday Godhasaraedc38ae2021-08-04 23:58:46 -0700178 pm_client_set_wakeup_sources((uint32_t)proc->node_id);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700179 }
Tejas Patelfcb7c162019-02-27 18:44:56 +0530180
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800181 /* Set powerdown request */
182 mmio_write_32(FPD_APU_PWRCTL, mmio_read_32(FPD_APU_PWRCTL) |
Abhyuday Godhasara44877942021-08-05 02:59:26 -0700183 (uint32_t)proc->pwrdn_mask);
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800184
185 bakery_lock_release(&pm_client_secure_lock);
186}
187
188/**
189 * pm_client_abort_suspend() - Client-specific abort-suspend actions
190 *
191 * This function should contain any PU-specific actions
192 * required for aborting a prior suspend request
193 */
194void pm_client_abort_suspend(void)
195{
196 /* Enable interrupts at processor level (for current cpu) */
197 gicv3_cpuif_enable(plat_my_core_pos());
198
199 bakery_lock_get(&pm_client_secure_lock);
200
201 /* Clear powerdown request */
202 mmio_write_32(FPD_APU_PWRCTL, mmio_read_32(FPD_APU_PWRCTL) &
Abhyuday Godhasara44877942021-08-05 02:59:26 -0700203 ~((uint32_t)primary_proc->pwrdn_mask));
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800204
205 bakery_lock_release(&pm_client_secure_lock);
206}
207
208/**
Tejas Patel7029dd82019-02-27 18:44:54 +0530209 * pm_get_cpuid() - get the local cpu ID for a global node ID
210 * @nid: node id of the processor
211 *
212 * Return: the cpu ID (starting from 0) for the subsystem
213 */
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530214static uint32_t pm_get_cpuid(uint32_t nid)
Tejas Patel7029dd82019-02-27 18:44:54 +0530215{
Abhyuday Godhasaraedc38ae2021-08-04 23:58:46 -0700216 for (size_t i = 0U; i < ARRAY_SIZE(pm_procs_all); i++) {
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700217 if (pm_procs_all[i].node_id == nid) {
Tejas Patel7029dd82019-02-27 18:44:54 +0530218 return i;
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700219 }
Tejas Patel7029dd82019-02-27 18:44:54 +0530220 }
221 return UNDEFINED_CPUID;
222}
223
224/**
225 * pm_client_wakeup() - Client-specific wakeup actions
226 *
227 * This function should contain any PU-specific actions
228 * required for waking up another APU core
229 */
230void pm_client_wakeup(const struct pm_proc *proc)
231{
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530232 uint32_t cpuid = pm_get_cpuid(proc->node_id);
Tejas Patel7029dd82019-02-27 18:44:54 +0530233
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700234 if (cpuid == UNDEFINED_CPUID) {
Tejas Patel7029dd82019-02-27 18:44:54 +0530235 return;
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700236 }
Tejas Patel7029dd82019-02-27 18:44:54 +0530237
238 bakery_lock_get(&pm_client_secure_lock);
239
240 /* clear powerdown bit for affected cpu */
241 uint32_t val = mmio_read_32(FPD_APU_PWRCTL);
242 val &= ~(proc->pwrdn_mask);
243 mmio_write_32(FPD_APU_PWRCTL, val);
244
245 bakery_lock_release(&pm_client_secure_lock);
246}
247
248/**
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800249 * pm_get_proc() - returns pointer to the proc structure
250 * @cpuid: id of the cpu whose proc struct pointer should be returned
251 *
252 * Return: pointer to a proc structure if proc is found, otherwise NULL
253 */
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530254const struct pm_proc *pm_get_proc(uint32_t cpuid)
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800255{
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700256 if (cpuid < ARRAY_SIZE(pm_procs_all)) {
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800257 return &pm_procs_all[cpuid];
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700258 }
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800259
260 return NULL;
261}