Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / acpi / arm64 / gtdt.c
blobf2d0e5915dab5975a37dec195dcd1538a0657721
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
9 */
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>
19 #undef pr_fmt
20 #define pr_fmt(fmt) "ACPI GTDT: " fmt
22 /**
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
29 * acpi_gtdt_init.
31 struct acpi_gtdt_descriptor {
32 struct acpi_table_gtdt *gtdt;
33 void *gtdt_end;
34 void *platform_timer;
37 static struct acpi_gtdt_descriptor acpi_gtdt_desc __initdata;
39 static inline void *next_platform_timer(void *platform_timer)
41 struct acpi_gtdt_header *gh = platform_timer;
43 platform_timer += gh->length;
44 if (platform_timer < acpi_gtdt_desc.gtdt_end)
45 return platform_timer;
47 return NULL;
50 #define for_each_platform_timer(_g) \
51 for (_g = acpi_gtdt_desc.platform_timer; _g; \
52 _g = next_platform_timer(_g))
54 static inline bool is_timer_block(void *platform_timer)
56 struct acpi_gtdt_header *gh = platform_timer;
58 return gh->type == ACPI_GTDT_TYPE_TIMER_BLOCK;
61 static inline bool is_non_secure_watchdog(void *platform_timer)
63 struct acpi_gtdt_header *gh = platform_timer;
64 struct acpi_gtdt_watchdog *wd = platform_timer;
66 if (gh->type != ACPI_GTDT_TYPE_WATCHDOG)
67 return false;
69 return !(wd->timer_flags & ACPI_GTDT_WATCHDOG_SECURE);
72 static int __init map_gt_gsi(u32 interrupt, u32 flags)
74 int trigger, polarity;
76 trigger = (flags & ACPI_GTDT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
77 : ACPI_LEVEL_SENSITIVE;
79 polarity = (flags & ACPI_GTDT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
80 : ACPI_ACTIVE_HIGH;
82 return acpi_register_gsi(NULL, interrupt, trigger, polarity);
85 /**
86 * acpi_gtdt_map_ppi() - Map the PPIs of per-cpu arch_timer.
87 * @type: the type of PPI.
89 * Note: Secure state is not managed by the kernel on ARM64 systems.
90 * So we only handle the non-secure timer PPIs,
91 * ARCH_TIMER_PHYS_SECURE_PPI is treated as invalid type.
93 * Return: the mapped PPI value, 0 if error.
95 int __init acpi_gtdt_map_ppi(int type)
97 struct acpi_table_gtdt *gtdt = acpi_gtdt_desc.gtdt;
99 switch (type) {
100 case ARCH_TIMER_PHYS_NONSECURE_PPI:
101 return map_gt_gsi(gtdt->non_secure_el1_interrupt,
102 gtdt->non_secure_el1_flags);
103 case ARCH_TIMER_VIRT_PPI:
104 return map_gt_gsi(gtdt->virtual_timer_interrupt,
105 gtdt->virtual_timer_flags);
107 case ARCH_TIMER_HYP_PPI:
108 return map_gt_gsi(gtdt->non_secure_el2_interrupt,
109 gtdt->non_secure_el2_flags);
110 default:
111 pr_err("Failed to map timer interrupt: invalid type.\n");
114 return 0;
118 * acpi_gtdt_c3stop() - Got c3stop info from GTDT according to the type of PPI.
119 * @type: the type of PPI.
121 * Return: true if the timer HW state is lost when a CPU enters an idle state,
122 * false otherwise
124 bool __init acpi_gtdt_c3stop(int type)
126 struct acpi_table_gtdt *gtdt = acpi_gtdt_desc.gtdt;
128 switch (type) {
129 case ARCH_TIMER_PHYS_NONSECURE_PPI:
130 return !(gtdt->non_secure_el1_flags & ACPI_GTDT_ALWAYS_ON);
132 case ARCH_TIMER_VIRT_PPI:
133 return !(gtdt->virtual_timer_flags & ACPI_GTDT_ALWAYS_ON);
135 case ARCH_TIMER_HYP_PPI:
136 return !(gtdt->non_secure_el2_flags & ACPI_GTDT_ALWAYS_ON);
138 default:
139 pr_err("Failed to get c3stop info: invalid type.\n");
142 return false;
146 * acpi_gtdt_init() - Get the info of GTDT table to prepare for further init.
147 * @table: The pointer to GTDT table.
148 * @platform_timer_count: It points to a integer variable which is used
149 * for storing the number of platform timers.
150 * This pointer could be NULL, if the caller
151 * doesn't need this info.
153 * Return: 0 if success, -EINVAL if error.
155 int __init acpi_gtdt_init(struct acpi_table_header *table,
156 int *platform_timer_count)
158 void *platform_timer;
159 struct acpi_table_gtdt *gtdt;
161 gtdt = container_of(table, struct acpi_table_gtdt, header);
162 acpi_gtdt_desc.gtdt = gtdt;
163 acpi_gtdt_desc.gtdt_end = (void *)table + table->length;
164 acpi_gtdt_desc.platform_timer = NULL;
165 if (platform_timer_count)
166 *platform_timer_count = 0;
168 if (table->revision < 2) {
169 pr_warn("Revision:%d doesn't support Platform Timers.\n",
170 table->revision);
171 return 0;
174 if (!gtdt->platform_timer_count) {
175 pr_debug("No Platform Timer.\n");
176 return 0;
179 platform_timer = (void *)gtdt + gtdt->platform_timer_offset;
180 if (platform_timer < (void *)table + sizeof(struct acpi_table_gtdt)) {
181 pr_err(FW_BUG "invalid timer data.\n");
182 return -EINVAL;
184 acpi_gtdt_desc.platform_timer = platform_timer;
185 if (platform_timer_count)
186 *platform_timer_count = gtdt->platform_timer_count;
188 return 0;
191 static int __init gtdt_parse_timer_block(struct acpi_gtdt_timer_block *block,
192 struct arch_timer_mem *timer_mem)
194 int i;
195 struct arch_timer_mem_frame *frame;
196 struct acpi_gtdt_timer_entry *gtdt_frame;
198 if (!block->timer_count) {
199 pr_err(FW_BUG "GT block present, but frame count is zero.\n");
200 return -ENODEV;
203 if (block->timer_count > ARCH_TIMER_MEM_MAX_FRAMES) {
204 pr_err(FW_BUG "GT block lists %d frames, ACPI spec only allows 8\n",
205 block->timer_count);
206 return -EINVAL;
209 timer_mem->cntctlbase = (phys_addr_t)block->block_address;
211 * The CNTCTLBase frame is 4KB (register offsets 0x000 - 0xFFC).
212 * See ARM DDI 0487A.k_iss10775, page I1-5129, Table I1-3
213 * "CNTCTLBase memory map".
215 timer_mem->size = SZ_4K;
217 gtdt_frame = (void *)block + block->timer_offset;
218 if (gtdt_frame + block->timer_count != (void *)block + block->header.length)
219 return -EINVAL;
222 * Get the GT timer Frame data for every GT Block Timer
224 for (i = 0; i < block->timer_count; i++, gtdt_frame++) {
225 if (gtdt_frame->common_flags & ACPI_GTDT_GT_IS_SECURE_TIMER)
226 continue;
227 if (gtdt_frame->frame_number >= ARCH_TIMER_MEM_MAX_FRAMES ||
228 !gtdt_frame->base_address || !gtdt_frame->timer_interrupt)
229 goto error;
231 frame = &timer_mem->frame[gtdt_frame->frame_number];
233 /* duplicate frame */
234 if (frame->valid)
235 goto error;
237 frame->phys_irq = map_gt_gsi(gtdt_frame->timer_interrupt,
238 gtdt_frame->timer_flags);
239 if (frame->phys_irq <= 0) {
240 pr_warn("failed to map physical timer irq in frame %d.\n",
241 gtdt_frame->frame_number);
242 goto error;
245 if (gtdt_frame->virtual_timer_interrupt) {
246 frame->virt_irq =
247 map_gt_gsi(gtdt_frame->virtual_timer_interrupt,
248 gtdt_frame->virtual_timer_flags);
249 if (frame->virt_irq <= 0) {
250 pr_warn("failed to map virtual timer irq in frame %d.\n",
251 gtdt_frame->frame_number);
252 goto error;
254 } else {
255 pr_debug("virtual timer in frame %d not implemented.\n",
256 gtdt_frame->frame_number);
259 frame->cntbase = gtdt_frame->base_address;
261 * The CNTBaseN frame is 4KB (register offsets 0x000 - 0xFFC).
262 * See ARM DDI 0487A.k_iss10775, page I1-5130, Table I1-4
263 * "CNTBaseN memory map".
265 frame->size = SZ_4K;
266 frame->valid = true;
269 return 0;
271 error:
272 do {
273 if (gtdt_frame->common_flags & ACPI_GTDT_GT_IS_SECURE_TIMER ||
274 gtdt_frame->frame_number >= ARCH_TIMER_MEM_MAX_FRAMES)
275 continue;
277 frame = &timer_mem->frame[gtdt_frame->frame_number];
279 if (frame->phys_irq > 0)
280 acpi_unregister_gsi(gtdt_frame->timer_interrupt);
281 frame->phys_irq = 0;
283 if (frame->virt_irq > 0)
284 acpi_unregister_gsi(gtdt_frame->virtual_timer_interrupt);
285 frame->virt_irq = 0;
286 } while (i-- >= 0 && gtdt_frame--);
288 return -EINVAL;
292 * acpi_arch_timer_mem_init() - Get the info of all GT blocks in GTDT table.
293 * @timer_mem: The pointer to the array of struct arch_timer_mem for returning
294 * the result of parsing. The element number of this array should
295 * be platform_timer_count(the total number of platform timers).
296 * @timer_count: It points to a integer variable which is used for storing the
297 * number of GT blocks we have parsed.
299 * Return: 0 if success, -EINVAL/-ENODEV if error.
301 int __init acpi_arch_timer_mem_init(struct arch_timer_mem *timer_mem,
302 int *timer_count)
304 int ret;
305 void *platform_timer;
307 *timer_count = 0;
308 for_each_platform_timer(platform_timer) {
309 if (is_timer_block(platform_timer)) {
310 ret = gtdt_parse_timer_block(platform_timer, timer_mem);
311 if (ret)
312 return ret;
313 timer_mem++;
314 (*timer_count)++;
318 if (*timer_count)
319 pr_info("found %d memory-mapped timer block(s).\n",
320 *timer_count);
322 return 0;
326 * Initialize a SBSA generic Watchdog platform device info from GTDT
328 static int __init gtdt_import_sbsa_gwdt(struct acpi_gtdt_watchdog *wd,
329 int index)
331 struct platform_device *pdev;
332 int irq = map_gt_gsi(wd->timer_interrupt, wd->timer_flags);
335 * According to SBSA specification the size of refresh and control
336 * frames of SBSA Generic Watchdog is SZ_4K(Offset 0x000 – 0xFFF).
338 struct resource res[] = {
339 DEFINE_RES_MEM(wd->control_frame_address, SZ_4K),
340 DEFINE_RES_MEM(wd->refresh_frame_address, SZ_4K),
341 DEFINE_RES_IRQ(irq),
343 int nr_res = ARRAY_SIZE(res);
345 pr_debug("found a Watchdog (0x%llx/0x%llx gsi:%u flags:0x%x).\n",
346 wd->refresh_frame_address, wd->control_frame_address,
347 wd->timer_interrupt, wd->timer_flags);
349 if (!(wd->refresh_frame_address && wd->control_frame_address)) {
350 pr_err(FW_BUG "failed to get the Watchdog base address.\n");
351 acpi_unregister_gsi(wd->timer_interrupt);
352 return -EINVAL;
355 if (irq <= 0) {
356 pr_warn("failed to map the Watchdog interrupt.\n");
357 nr_res--;
361 * Add a platform device named "sbsa-gwdt" to match the platform driver.
362 * "sbsa-gwdt": SBSA(Server Base System Architecture) Generic Watchdog
363 * The platform driver can get device info below by matching this name.
365 pdev = platform_device_register_simple("sbsa-gwdt", index, res, nr_res);
366 if (IS_ERR(pdev)) {
367 acpi_unregister_gsi(wd->timer_interrupt);
368 return PTR_ERR(pdev);
371 return 0;
374 static int __init gtdt_sbsa_gwdt_init(void)
376 void *platform_timer;
377 struct acpi_table_header *table;
378 int ret, timer_count, gwdt_count = 0;
380 if (acpi_disabled)
381 return 0;
383 if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_GTDT, 0, &table)))
384 return -EINVAL;
387 * Note: Even though the global variable acpi_gtdt_desc has been
388 * initialized by acpi_gtdt_init() while initializing the arch timers,
389 * when we call this function to get SBSA watchdogs info from GTDT, the
390 * pointers stashed in it are stale (since they are early temporary
391 * mappings carried out before acpi_permanent_mmap is set) and we need
392 * to re-initialize them with permanent mapped pointer values to let the
393 * GTDT parsing possible.
395 ret = acpi_gtdt_init(table, &timer_count);
396 if (ret || !timer_count)
397 goto out_put_gtdt;
399 for_each_platform_timer(platform_timer) {
400 if (is_non_secure_watchdog(platform_timer)) {
401 ret = gtdt_import_sbsa_gwdt(platform_timer, gwdt_count);
402 if (ret)
403 break;
404 gwdt_count++;
408 if (gwdt_count)
409 pr_info("found %d SBSA generic Watchdog(s).\n", gwdt_count);
411 out_put_gtdt:
412 acpi_put_table(table);
413 return ret;
416 device_initcall(gtdt_sbsa_gwdt_init);