1 // SPDX-License-Identifier: GPL-2.0-only
3 * ARM Specific GTDT table Support
5 * Copyright (C) 2016, Linaro Ltd.
6 * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
7 * Fu Wei <fu.wei@linaro.org>
8 * Hanjun Guo <hanjun.guo@linaro.org>
11 #include <linux/acpi.h>
12 #include <linux/init.h>
13 #include <linux/irqdomain.h>
14 #include <linux/kernel.h>
15 #include <linux/platform_device.h>
17 #include <clocksource/arm_arch_timer.h>
20 #define pr_fmt(fmt) "ACPI GTDT: " fmt
23 * struct acpi_gtdt_descriptor - Store the key info of GTDT for all functions
24 * @gtdt: The pointer to the struct acpi_table_gtdt of GTDT table.
25 * @gtdt_end: The pointer to the end of GTDT table.
26 * @platform_timer: The pointer to the start of Platform Timer Structure
28 * The struct store the key info of GTDT table, it should be initialized by
31 struct acpi_gtdt_descriptor
{
32 struct acpi_table_gtdt
*gtdt
;
37 static struct acpi_gtdt_descriptor acpi_gtdt_desc __initdata
;
39 static __init
bool platform_timer_valid(void *platform_timer
)
41 struct acpi_gtdt_header
*gh
= platform_timer
;
43 return (platform_timer
>= (void *)(acpi_gtdt_desc
.gtdt
+ 1) &&
44 platform_timer
< acpi_gtdt_desc
.gtdt_end
&&
46 platform_timer
+ gh
->length
<= acpi_gtdt_desc
.gtdt_end
);
49 static __init
void *next_platform_timer(void *platform_timer
)
51 struct acpi_gtdt_header
*gh
= platform_timer
;
53 return platform_timer
+ gh
->length
;
56 #define for_each_platform_timer(_g) \
57 for (_g = acpi_gtdt_desc.platform_timer; platform_timer_valid(_g);\
58 _g = next_platform_timer(_g))
60 static inline bool is_timer_block(void *platform_timer
)
62 struct acpi_gtdt_header
*gh
= platform_timer
;
64 return gh
->type
== ACPI_GTDT_TYPE_TIMER_BLOCK
;
67 static inline bool is_non_secure_watchdog(void *platform_timer
)
69 struct acpi_gtdt_header
*gh
= platform_timer
;
70 struct acpi_gtdt_watchdog
*wd
= platform_timer
;
72 if (gh
->type
!= ACPI_GTDT_TYPE_WATCHDOG
)
75 return !(wd
->timer_flags
& ACPI_GTDT_WATCHDOG_SECURE
);
78 static int __init
map_gt_gsi(u32 interrupt
, u32 flags
)
80 int trigger
, polarity
;
82 trigger
= (flags
& ACPI_GTDT_INTERRUPT_MODE
) ? ACPI_EDGE_SENSITIVE
83 : ACPI_LEVEL_SENSITIVE
;
85 polarity
= (flags
& ACPI_GTDT_INTERRUPT_POLARITY
) ? ACPI_ACTIVE_LOW
88 return acpi_register_gsi(NULL
, interrupt
, trigger
, polarity
);
92 * acpi_gtdt_map_ppi() - Map the PPIs of per-cpu arch_timer.
93 * @type: the type of PPI.
95 * Note: Secure state is not managed by the kernel on ARM64 systems.
96 * So we only handle the non-secure timer PPIs,
97 * ARCH_TIMER_PHYS_SECURE_PPI is treated as invalid type.
99 * Return: the mapped PPI value, 0 if error.
101 int __init
acpi_gtdt_map_ppi(int type
)
103 struct acpi_table_gtdt
*gtdt
= acpi_gtdt_desc
.gtdt
;
106 case ARCH_TIMER_PHYS_NONSECURE_PPI
:
107 return map_gt_gsi(gtdt
->non_secure_el1_interrupt
,
108 gtdt
->non_secure_el1_flags
);
109 case ARCH_TIMER_VIRT_PPI
:
110 return map_gt_gsi(gtdt
->virtual_timer_interrupt
,
111 gtdt
->virtual_timer_flags
);
113 case ARCH_TIMER_HYP_PPI
:
114 return map_gt_gsi(gtdt
->non_secure_el2_interrupt
,
115 gtdt
->non_secure_el2_flags
);
117 pr_err("Failed to map timer interrupt: invalid type.\n");
124 * acpi_gtdt_c3stop() - Got c3stop info from GTDT according to the type of PPI.
125 * @type: the type of PPI.
127 * Return: true if the timer HW state is lost when a CPU enters an idle state,
130 bool __init
acpi_gtdt_c3stop(int type
)
132 struct acpi_table_gtdt
*gtdt
= acpi_gtdt_desc
.gtdt
;
135 case ARCH_TIMER_PHYS_NONSECURE_PPI
:
136 return !(gtdt
->non_secure_el1_flags
& ACPI_GTDT_ALWAYS_ON
);
138 case ARCH_TIMER_VIRT_PPI
:
139 return !(gtdt
->virtual_timer_flags
& ACPI_GTDT_ALWAYS_ON
);
141 case ARCH_TIMER_HYP_PPI
:
142 return !(gtdt
->non_secure_el2_flags
& ACPI_GTDT_ALWAYS_ON
);
145 pr_err("Failed to get c3stop info: invalid type.\n");
152 * acpi_gtdt_init() - Get the info of GTDT table to prepare for further init.
153 * @table: The pointer to GTDT table.
154 * @platform_timer_count: It points to a integer variable which is used
155 * for storing the number of platform timers.
156 * This pointer could be NULL, if the caller
157 * doesn't need this info.
159 * Return: 0 if success, -EINVAL if error.
161 int __init
acpi_gtdt_init(struct acpi_table_header
*table
,
162 int *platform_timer_count
)
164 void *platform_timer
;
165 struct acpi_table_gtdt
*gtdt
;
168 gtdt
= container_of(table
, struct acpi_table_gtdt
, header
);
169 acpi_gtdt_desc
.gtdt
= gtdt
;
170 acpi_gtdt_desc
.gtdt_end
= (void *)table
+ table
->length
;
171 acpi_gtdt_desc
.platform_timer
= NULL
;
172 if (platform_timer_count
)
173 *platform_timer_count
= 0;
175 if (table
->revision
< 2) {
176 pr_warn("Revision:%d doesn't support Platform Timers.\n",
181 if (!gtdt
->platform_timer_count
) {
182 pr_debug("No Platform Timer.\n");
186 acpi_gtdt_desc
.platform_timer
= (void *)gtdt
+ gtdt
->platform_timer_offset
;
187 for_each_platform_timer(platform_timer
)
190 if (cnt
!= gtdt
->platform_timer_count
) {
191 acpi_gtdt_desc
.platform_timer
= NULL
;
192 pr_err(FW_BUG
"invalid timer data.\n");
196 if (platform_timer_count
)
197 *platform_timer_count
= gtdt
->platform_timer_count
;
202 static int __init
gtdt_parse_timer_block(struct acpi_gtdt_timer_block
*block
,
203 struct arch_timer_mem
*timer_mem
)
206 struct arch_timer_mem_frame
*frame
;
207 struct acpi_gtdt_timer_entry
*gtdt_frame
;
209 if (!block
->timer_count
) {
210 pr_err(FW_BUG
"GT block present, but frame count is zero.\n");
214 if (block
->timer_count
> ARCH_TIMER_MEM_MAX_FRAMES
) {
215 pr_err(FW_BUG
"GT block lists %d frames, ACPI spec only allows 8\n",
220 timer_mem
->cntctlbase
= (phys_addr_t
)block
->block_address
;
222 * The CNTCTLBase frame is 4KB (register offsets 0x000 - 0xFFC).
223 * See ARM DDI 0487A.k_iss10775, page I1-5129, Table I1-3
224 * "CNTCTLBase memory map".
226 timer_mem
->size
= SZ_4K
;
228 gtdt_frame
= (void *)block
+ block
->timer_offset
;
229 if (gtdt_frame
+ block
->timer_count
!= (void *)block
+ block
->header
.length
)
233 * Get the GT timer Frame data for every GT Block Timer
235 for (i
= 0; i
< block
->timer_count
; i
++, gtdt_frame
++) {
236 if (gtdt_frame
->common_flags
& ACPI_GTDT_GT_IS_SECURE_TIMER
)
238 if (gtdt_frame
->frame_number
>= ARCH_TIMER_MEM_MAX_FRAMES
||
239 !gtdt_frame
->base_address
|| !gtdt_frame
->timer_interrupt
)
242 frame
= &timer_mem
->frame
[gtdt_frame
->frame_number
];
244 /* duplicate frame */
248 frame
->phys_irq
= map_gt_gsi(gtdt_frame
->timer_interrupt
,
249 gtdt_frame
->timer_flags
);
250 if (frame
->phys_irq
<= 0) {
251 pr_warn("failed to map physical timer irq in frame %d.\n",
252 gtdt_frame
->frame_number
);
256 if (gtdt_frame
->virtual_timer_interrupt
) {
258 map_gt_gsi(gtdt_frame
->virtual_timer_interrupt
,
259 gtdt_frame
->virtual_timer_flags
);
260 if (frame
->virt_irq
<= 0) {
261 pr_warn("failed to map virtual timer irq in frame %d.\n",
262 gtdt_frame
->frame_number
);
266 pr_debug("virtual timer in frame %d not implemented.\n",
267 gtdt_frame
->frame_number
);
270 frame
->cntbase
= gtdt_frame
->base_address
;
272 * The CNTBaseN frame is 4KB (register offsets 0x000 - 0xFFC).
273 * See ARM DDI 0487A.k_iss10775, page I1-5130, Table I1-4
274 * "CNTBaseN memory map".
284 if (gtdt_frame
->common_flags
& ACPI_GTDT_GT_IS_SECURE_TIMER
||
285 gtdt_frame
->frame_number
>= ARCH_TIMER_MEM_MAX_FRAMES
)
288 frame
= &timer_mem
->frame
[gtdt_frame
->frame_number
];
290 if (frame
->phys_irq
> 0)
291 acpi_unregister_gsi(gtdt_frame
->timer_interrupt
);
294 if (frame
->virt_irq
> 0)
295 acpi_unregister_gsi(gtdt_frame
->virtual_timer_interrupt
);
297 } while (i
-- > 0 && gtdt_frame
--);
303 * acpi_arch_timer_mem_init() - Get the info of all GT blocks in GTDT table.
304 * @timer_mem: The pointer to the array of struct arch_timer_mem for returning
305 * the result of parsing. The element number of this array should
306 * be platform_timer_count(the total number of platform timers).
307 * @timer_count: It points to a integer variable which is used for storing the
308 * number of GT blocks we have parsed.
310 * Return: 0 if success, -EINVAL/-ENODEV if error.
312 int __init
acpi_arch_timer_mem_init(struct arch_timer_mem
*timer_mem
,
316 void *platform_timer
;
319 for_each_platform_timer(platform_timer
) {
320 if (is_timer_block(platform_timer
)) {
321 ret
= gtdt_parse_timer_block(platform_timer
, timer_mem
);
330 pr_info("found %d memory-mapped timer block(s).\n",
337 * Initialize a SBSA generic Watchdog platform device info from GTDT
339 static int __init
gtdt_import_sbsa_gwdt(struct acpi_gtdt_watchdog
*wd
,
342 struct platform_device
*pdev
;
346 * According to SBSA specification the size of refresh and control
347 * frames of SBSA Generic Watchdog is SZ_4K(Offset 0x000 – 0xFFF).
349 struct resource res
[] = {
350 DEFINE_RES_MEM(wd
->control_frame_address
, SZ_4K
),
351 DEFINE_RES_MEM(wd
->refresh_frame_address
, SZ_4K
),
354 int nr_res
= ARRAY_SIZE(res
);
356 pr_debug("found a Watchdog (0x%llx/0x%llx gsi:%u flags:0x%x).\n",
357 wd
->refresh_frame_address
, wd
->control_frame_address
,
358 wd
->timer_interrupt
, wd
->timer_flags
);
360 if (!(wd
->refresh_frame_address
&& wd
->control_frame_address
)) {
361 pr_err(FW_BUG
"failed to get the Watchdog base address.\n");
365 irq
= map_gt_gsi(wd
->timer_interrupt
, wd
->timer_flags
);
366 res
[2] = DEFINE_RES_IRQ(irq
);
368 pr_warn("failed to map the Watchdog interrupt.\n");
373 * Add a platform device named "sbsa-gwdt" to match the platform driver.
374 * "sbsa-gwdt": SBSA(Server Base System Architecture) Generic Watchdog
375 * The platform driver can get device info below by matching this name.
377 pdev
= platform_device_register_simple("sbsa-gwdt", index
, res
, nr_res
);
380 acpi_unregister_gsi(wd
->timer_interrupt
);
381 return PTR_ERR(pdev
);
387 static int __init
gtdt_sbsa_gwdt_init(void)
389 void *platform_timer
;
390 struct acpi_table_header
*table
;
391 int ret
, timer_count
, gwdt_count
= 0;
396 if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_GTDT
, 0, &table
)))
400 * Note: Even though the global variable acpi_gtdt_desc has been
401 * initialized by acpi_gtdt_init() while initializing the arch timers,
402 * when we call this function to get SBSA watchdogs info from GTDT, the
403 * pointers stashed in it are stale (since they are early temporary
404 * mappings carried out before acpi_permanent_mmap is set) and we need
405 * to re-initialize them with permanent mapped pointer values to let the
406 * GTDT parsing possible.
408 ret
= acpi_gtdt_init(table
, &timer_count
);
409 if (ret
|| !timer_count
)
412 for_each_platform_timer(platform_timer
) {
413 if (is_non_secure_watchdog(platform_timer
)) {
414 ret
= gtdt_import_sbsa_gwdt(platform_timer
, gwdt_count
);
422 pr_info("found %d SBSA generic Watchdog(s).\n", gwdt_count
);
425 acpi_put_table(table
);
429 device_initcall(gtdt_sbsa_gwdt_init
);