2 * ARM Specific GTDT table Support
4 * Copyright (C) 2016, Linaro Ltd.
5 * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
6 * Fu Wei <fu.wei@linaro.org>
7 * Hanjun Guo <hanjun.guo@linaro.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/acpi.h>
15 #include <linux/init.h>
16 #include <linux/irqdomain.h>
17 #include <linux/kernel.h>
18 #include <linux/platform_device.h>
20 #include <clocksource/arm_arch_timer.h>
23 #define pr_fmt(fmt) "ACPI GTDT: " fmt
26 * struct acpi_gtdt_descriptor - Store the key info of GTDT for all functions
27 * @gtdt: The pointer to the struct acpi_table_gtdt of GTDT table.
28 * @gtdt_end: The pointer to the end of GTDT table.
29 * @platform_timer: The pointer to the start of Platform Timer Structure
31 * The struct store the key info of GTDT table, it should be initialized by
34 struct acpi_gtdt_descriptor
{
35 struct acpi_table_gtdt
*gtdt
;
40 static struct acpi_gtdt_descriptor acpi_gtdt_desc __initdata
;
42 static inline void *next_platform_timer(void *platform_timer
)
44 struct acpi_gtdt_header
*gh
= platform_timer
;
46 platform_timer
+= gh
->length
;
47 if (platform_timer
< acpi_gtdt_desc
.gtdt_end
)
48 return platform_timer
;
53 #define for_each_platform_timer(_g) \
54 for (_g = acpi_gtdt_desc.platform_timer; _g; \
55 _g = next_platform_timer(_g))
57 static inline bool is_timer_block(void *platform_timer
)
59 struct acpi_gtdt_header
*gh
= platform_timer
;
61 return gh
->type
== ACPI_GTDT_TYPE_TIMER_BLOCK
;
64 static inline bool is_non_secure_watchdog(void *platform_timer
)
66 struct acpi_gtdt_header
*gh
= platform_timer
;
67 struct acpi_gtdt_watchdog
*wd
= platform_timer
;
69 if (gh
->type
!= ACPI_GTDT_TYPE_WATCHDOG
)
72 return !(wd
->timer_flags
& ACPI_GTDT_WATCHDOG_SECURE
);
75 static int __init
map_gt_gsi(u32 interrupt
, u32 flags
)
77 int trigger
, polarity
;
79 trigger
= (flags
& ACPI_GTDT_INTERRUPT_MODE
) ? ACPI_EDGE_SENSITIVE
80 : ACPI_LEVEL_SENSITIVE
;
82 polarity
= (flags
& ACPI_GTDT_INTERRUPT_POLARITY
) ? ACPI_ACTIVE_LOW
85 return acpi_register_gsi(NULL
, interrupt
, trigger
, polarity
);
89 * acpi_gtdt_map_ppi() - Map the PPIs of per-cpu arch_timer.
90 * @type: the type of PPI.
92 * Note: Secure state is not managed by the kernel on ARM64 systems.
93 * So we only handle the non-secure timer PPIs,
94 * ARCH_TIMER_PHYS_SECURE_PPI is treated as invalid type.
96 * Return: the mapped PPI value, 0 if error.
98 int __init
acpi_gtdt_map_ppi(int type
)
100 struct acpi_table_gtdt
*gtdt
= acpi_gtdt_desc
.gtdt
;
103 case ARCH_TIMER_PHYS_NONSECURE_PPI
:
104 return map_gt_gsi(gtdt
->non_secure_el1_interrupt
,
105 gtdt
->non_secure_el1_flags
);
106 case ARCH_TIMER_VIRT_PPI
:
107 return map_gt_gsi(gtdt
->virtual_timer_interrupt
,
108 gtdt
->virtual_timer_flags
);
110 case ARCH_TIMER_HYP_PPI
:
111 return map_gt_gsi(gtdt
->non_secure_el2_interrupt
,
112 gtdt
->non_secure_el2_flags
);
114 pr_err("Failed to map timer interrupt: invalid type.\n");
121 * acpi_gtdt_c3stop() - Got c3stop info from GTDT according to the type of PPI.
122 * @type: the type of PPI.
124 * Return: true if the timer HW state is lost when a CPU enters an idle state,
127 bool __init
acpi_gtdt_c3stop(int type
)
129 struct acpi_table_gtdt
*gtdt
= acpi_gtdt_desc
.gtdt
;
132 case ARCH_TIMER_PHYS_NONSECURE_PPI
:
133 return !(gtdt
->non_secure_el1_flags
& ACPI_GTDT_ALWAYS_ON
);
135 case ARCH_TIMER_VIRT_PPI
:
136 return !(gtdt
->virtual_timer_flags
& ACPI_GTDT_ALWAYS_ON
);
138 case ARCH_TIMER_HYP_PPI
:
139 return !(gtdt
->non_secure_el2_flags
& ACPI_GTDT_ALWAYS_ON
);
142 pr_err("Failed to get c3stop info: invalid type.\n");
149 * acpi_gtdt_init() - Get the info of GTDT table to prepare for further init.
150 * @table: The pointer to GTDT table.
151 * @platform_timer_count: It points to a integer variable which is used
152 * for storing the number of platform timers.
153 * This pointer could be NULL, if the caller
154 * doesn't need this info.
156 * Return: 0 if success, -EINVAL if error.
158 int __init
acpi_gtdt_init(struct acpi_table_header
*table
,
159 int *platform_timer_count
)
161 void *platform_timer
;
162 struct acpi_table_gtdt
*gtdt
;
164 gtdt
= container_of(table
, struct acpi_table_gtdt
, header
);
165 acpi_gtdt_desc
.gtdt
= gtdt
;
166 acpi_gtdt_desc
.gtdt_end
= (void *)table
+ table
->length
;
167 acpi_gtdt_desc
.platform_timer
= NULL
;
168 if (platform_timer_count
)
169 *platform_timer_count
= 0;
171 if (table
->revision
< 2) {
172 pr_warn("Revision:%d doesn't support Platform Timers.\n",
177 if (!gtdt
->platform_timer_count
) {
178 pr_debug("No Platform Timer.\n");
182 platform_timer
= (void *)gtdt
+ gtdt
->platform_timer_offset
;
183 if (platform_timer
< (void *)table
+ sizeof(struct acpi_table_gtdt
)) {
184 pr_err(FW_BUG
"invalid timer data.\n");
187 acpi_gtdt_desc
.platform_timer
= platform_timer
;
188 if (platform_timer_count
)
189 *platform_timer_count
= gtdt
->platform_timer_count
;
194 static int __init
gtdt_parse_timer_block(struct acpi_gtdt_timer_block
*block
,
195 struct arch_timer_mem
*timer_mem
)
198 struct arch_timer_mem_frame
*frame
;
199 struct acpi_gtdt_timer_entry
*gtdt_frame
;
201 if (!block
->timer_count
) {
202 pr_err(FW_BUG
"GT block present, but frame count is zero.\n");
206 if (block
->timer_count
> ARCH_TIMER_MEM_MAX_FRAMES
) {
207 pr_err(FW_BUG
"GT block lists %d frames, ACPI spec only allows 8\n",
212 timer_mem
->cntctlbase
= (phys_addr_t
)block
->block_address
;
214 * The CNTCTLBase frame is 4KB (register offsets 0x000 - 0xFFC).
215 * See ARM DDI 0487A.k_iss10775, page I1-5129, Table I1-3
216 * "CNTCTLBase memory map".
218 timer_mem
->size
= SZ_4K
;
220 gtdt_frame
= (void *)block
+ block
->timer_offset
;
221 if (gtdt_frame
+ block
->timer_count
!= (void *)block
+ block
->header
.length
)
225 * Get the GT timer Frame data for every GT Block Timer
227 for (i
= 0; i
< block
->timer_count
; i
++, gtdt_frame
++) {
228 if (gtdt_frame
->common_flags
& ACPI_GTDT_GT_IS_SECURE_TIMER
)
230 if (gtdt_frame
->frame_number
>= ARCH_TIMER_MEM_MAX_FRAMES
||
231 !gtdt_frame
->base_address
|| !gtdt_frame
->timer_interrupt
)
234 frame
= &timer_mem
->frame
[gtdt_frame
->frame_number
];
236 /* duplicate frame */
240 frame
->phys_irq
= map_gt_gsi(gtdt_frame
->timer_interrupt
,
241 gtdt_frame
->timer_flags
);
242 if (frame
->phys_irq
<= 0) {
243 pr_warn("failed to map physical timer irq in frame %d.\n",
244 gtdt_frame
->frame_number
);
248 if (gtdt_frame
->virtual_timer_interrupt
) {
250 map_gt_gsi(gtdt_frame
->virtual_timer_interrupt
,
251 gtdt_frame
->virtual_timer_flags
);
252 if (frame
->virt_irq
<= 0) {
253 pr_warn("failed to map virtual timer irq in frame %d.\n",
254 gtdt_frame
->frame_number
);
258 pr_debug("virtual timer in frame %d not implemented.\n",
259 gtdt_frame
->frame_number
);
262 frame
->cntbase
= gtdt_frame
->base_address
;
264 * The CNTBaseN frame is 4KB (register offsets 0x000 - 0xFFC).
265 * See ARM DDI 0487A.k_iss10775, page I1-5130, Table I1-4
266 * "CNTBaseN memory map".
276 if (gtdt_frame
->common_flags
& ACPI_GTDT_GT_IS_SECURE_TIMER
||
277 gtdt_frame
->frame_number
>= ARCH_TIMER_MEM_MAX_FRAMES
)
280 frame
= &timer_mem
->frame
[gtdt_frame
->frame_number
];
282 if (frame
->phys_irq
> 0)
283 acpi_unregister_gsi(gtdt_frame
->timer_interrupt
);
286 if (frame
->virt_irq
> 0)
287 acpi_unregister_gsi(gtdt_frame
->virtual_timer_interrupt
);
289 } while (i
-- >= 0 && gtdt_frame
--);
295 * acpi_arch_timer_mem_init() - Get the info of all GT blocks in GTDT table.
296 * @timer_mem: The pointer to the array of struct arch_timer_mem for returning
297 * the result of parsing. The element number of this array should
298 * be platform_timer_count(the total number of platform timers).
299 * @timer_count: It points to a integer variable which is used for storing the
300 * number of GT blocks we have parsed.
302 * Return: 0 if success, -EINVAL/-ENODEV if error.
304 int __init
acpi_arch_timer_mem_init(struct arch_timer_mem
*timer_mem
,
308 void *platform_timer
;
311 for_each_platform_timer(platform_timer
) {
312 if (is_timer_block(platform_timer
)) {
313 ret
= gtdt_parse_timer_block(platform_timer
, timer_mem
);
322 pr_info("found %d memory-mapped timer block(s).\n",
329 * Initialize a SBSA generic Watchdog platform device info from GTDT
331 static int __init
gtdt_import_sbsa_gwdt(struct acpi_gtdt_watchdog
*wd
,
334 struct platform_device
*pdev
;
335 int irq
= map_gt_gsi(wd
->timer_interrupt
, wd
->timer_flags
);
338 * According to SBSA specification the size of refresh and control
339 * frames of SBSA Generic Watchdog is SZ_4K(Offset 0x000 – 0xFFF).
341 struct resource res
[] = {
342 DEFINE_RES_MEM(wd
->control_frame_address
, SZ_4K
),
343 DEFINE_RES_MEM(wd
->refresh_frame_address
, SZ_4K
),
346 int nr_res
= ARRAY_SIZE(res
);
348 pr_debug("found a Watchdog (0x%llx/0x%llx gsi:%u flags:0x%x).\n",
349 wd
->refresh_frame_address
, wd
->control_frame_address
,
350 wd
->timer_interrupt
, wd
->timer_flags
);
352 if (!(wd
->refresh_frame_address
&& wd
->control_frame_address
)) {
353 pr_err(FW_BUG
"failed to get the Watchdog base address.\n");
354 acpi_unregister_gsi(wd
->timer_interrupt
);
359 pr_warn("failed to map the Watchdog interrupt.\n");
364 * Add a platform device named "sbsa-gwdt" to match the platform driver.
365 * "sbsa-gwdt": SBSA(Server Base System Architecture) Generic Watchdog
366 * The platform driver can get device info below by matching this name.
368 pdev
= platform_device_register_simple("sbsa-gwdt", index
, res
, nr_res
);
370 acpi_unregister_gsi(wd
->timer_interrupt
);
371 return PTR_ERR(pdev
);
377 static int __init
gtdt_sbsa_gwdt_init(void)
379 void *platform_timer
;
380 struct acpi_table_header
*table
;
381 int ret
, timer_count
, gwdt_count
= 0;
386 if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_GTDT
, 0, &table
)))
390 * Note: Even though the global variable acpi_gtdt_desc has been
391 * initialized by acpi_gtdt_init() while initializing the arch timers,
392 * when we call this function to get SBSA watchdogs info from GTDT, the
393 * pointers stashed in it are stale (since they are early temporary
394 * mappings carried out before acpi_permanent_mmap is set) and we need
395 * to re-initialize them with permanent mapped pointer values to let the
396 * GTDT parsing possible.
398 ret
= acpi_gtdt_init(table
, &timer_count
);
399 if (ret
|| !timer_count
)
402 for_each_platform_timer(platform_timer
) {
403 if (is_non_secure_watchdog(platform_timer
)) {
404 ret
= gtdt_import_sbsa_gwdt(platform_timer
, gwdt_count
);
412 pr_info("found %d SBSA generic Watchdog(s).\n", gwdt_count
);
417 device_initcall(gtdt_sbsa_gwdt_init
);