accel/ivpu: Move recovery work to system_unbound_wq
[drm/drm-misc.git] / drivers / acpi / arm64 / gtdt.c
blob3561553eff8b5e4a06ef232db1123e8a1f6c44f2
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 __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 &&
45 gh->length != 0 &&
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)
73 return false;
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
86 : ACPI_ACTIVE_HIGH;
88 return acpi_register_gsi(NULL, interrupt, trigger, polarity);
91 /**
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;
105 switch (type) {
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);
116 default:
117 pr_err("Failed to map timer interrupt: invalid type.\n");
120 return 0;
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,
128 * false otherwise
130 bool __init acpi_gtdt_c3stop(int type)
132 struct acpi_table_gtdt *gtdt = acpi_gtdt_desc.gtdt;
134 switch (type) {
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);
144 default:
145 pr_err("Failed to get c3stop info: invalid type.\n");
148 return false;
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;
166 int cnt = 0;
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",
177 table->revision);
178 return 0;
181 if (!gtdt->platform_timer_count) {
182 pr_debug("No Platform Timer.\n");
183 return 0;
186 acpi_gtdt_desc.platform_timer = (void *)gtdt + gtdt->platform_timer_offset;
187 for_each_platform_timer(platform_timer)
188 cnt++;
190 if (cnt != gtdt->platform_timer_count) {
191 acpi_gtdt_desc.platform_timer = NULL;
192 pr_err(FW_BUG "invalid timer data.\n");
193 return -EINVAL;
196 if (platform_timer_count)
197 *platform_timer_count = gtdt->platform_timer_count;
199 return 0;
202 static int __init gtdt_parse_timer_block(struct acpi_gtdt_timer_block *block,
203 struct arch_timer_mem *timer_mem)
205 int i;
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");
211 return -ENODEV;
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",
216 block->timer_count);
217 return -EINVAL;
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)
230 return -EINVAL;
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)
237 continue;
238 if (gtdt_frame->frame_number >= ARCH_TIMER_MEM_MAX_FRAMES ||
239 !gtdt_frame->base_address || !gtdt_frame->timer_interrupt)
240 goto error;
242 frame = &timer_mem->frame[gtdt_frame->frame_number];
244 /* duplicate frame */
245 if (frame->valid)
246 goto error;
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);
253 goto error;
256 if (gtdt_frame->virtual_timer_interrupt) {
257 frame->virt_irq =
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);
263 goto error;
265 } else {
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".
276 frame->size = SZ_4K;
277 frame->valid = true;
280 return 0;
282 error:
283 do {
284 if (gtdt_frame->common_flags & ACPI_GTDT_GT_IS_SECURE_TIMER ||
285 gtdt_frame->frame_number >= ARCH_TIMER_MEM_MAX_FRAMES)
286 continue;
288 frame = &timer_mem->frame[gtdt_frame->frame_number];
290 if (frame->phys_irq > 0)
291 acpi_unregister_gsi(gtdt_frame->timer_interrupt);
292 frame->phys_irq = 0;
294 if (frame->virt_irq > 0)
295 acpi_unregister_gsi(gtdt_frame->virtual_timer_interrupt);
296 frame->virt_irq = 0;
297 } while (i-- > 0 && gtdt_frame--);
299 return -EINVAL;
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,
313 int *timer_count)
315 int ret;
316 void *platform_timer;
318 *timer_count = 0;
319 for_each_platform_timer(platform_timer) {
320 if (is_timer_block(platform_timer)) {
321 ret = gtdt_parse_timer_block(platform_timer, timer_mem);
322 if (ret)
323 return ret;
324 timer_mem++;
325 (*timer_count)++;
329 if (*timer_count)
330 pr_info("found %d memory-mapped timer block(s).\n",
331 *timer_count);
333 return 0;
337 * Initialize a SBSA generic Watchdog platform device info from GTDT
339 static int __init gtdt_import_sbsa_gwdt(struct acpi_gtdt_watchdog *wd,
340 int index)
342 struct platform_device *pdev;
343 int irq;
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");
362 return -EINVAL;
365 irq = map_gt_gsi(wd->timer_interrupt, wd->timer_flags);
366 res[2] = DEFINE_RES_IRQ(irq);
367 if (irq <= 0) {
368 pr_warn("failed to map the Watchdog interrupt.\n");
369 nr_res--;
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);
378 if (IS_ERR(pdev)) {
379 if (irq > 0)
380 acpi_unregister_gsi(wd->timer_interrupt);
381 return PTR_ERR(pdev);
384 return 0;
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;
393 if (acpi_disabled)
394 return 0;
396 if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_GTDT, 0, &table)))
397 return -EINVAL;
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)
410 goto out_put_gtdt;
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);
415 if (ret)
416 break;
417 gwdt_count++;
421 if (gwdt_count)
422 pr_info("found %d SBSA generic Watchdog(s).\n", gwdt_count);
424 out_put_gtdt:
425 acpi_put_table(table);
426 return ret;
429 device_initcall(gtdt_sbsa_gwdt_init);