xref: /linux/drivers/acpi/arm64/gtdt.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ARM Specific GTDT table Support
4  *
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  */
10 
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>
16 
17 #include <clocksource/arm_arch_timer.h>
18 
19 #undef pr_fmt
20 #define pr_fmt(fmt) "ACPI GTDT: " fmt
21 
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
27  *
28  * The struct store the key info of GTDT table, it should be initialized by
29  * acpi_gtdt_init.
30  */
31 struct acpi_gtdt_descriptor {
32 	struct acpi_table_gtdt *gtdt;
33 	void *gtdt_end;
34 	void *platform_timer;
35 };
36 
37 struct gtdt_v3 {
38 	struct acpi_table_gtdt	gtdt_v2;
39 	struct acpi_gtdt_el2	el2_vtimer;
40 };
41 
42 static struct acpi_gtdt_descriptor acpi_gtdt_desc __initdata;
43 
44 static __init struct acpi_gtdt_el2 *gtdt_to_el2_vtimer(struct acpi_table_gtdt *gtdt)
45 {
46 	if (gtdt->header.revision < 3)
47 		return NULL;
48 
49 	return &container_of(gtdt, struct gtdt_v3, gtdt_v2)->el2_vtimer;
50 }
51 
52 static __init bool platform_timer_valid(void *platform_timer)
53 {
54 	struct acpi_gtdt_header *gh = platform_timer;
55 	void *platform_timer_begin;
56 
57 	if (acpi_gtdt_desc.gtdt->header.revision >= 3)
58 		platform_timer_begin = container_of(acpi_gtdt_desc.gtdt, struct gtdt_v3, gtdt_v2) + 1;
59 	else
60 		platform_timer_begin = acpi_gtdt_desc.gtdt + 1;
61 
62 	return (platform_timer >= platform_timer_begin &&
63 		platform_timer + sizeof(*gh) <= acpi_gtdt_desc.gtdt_end &&
64 		gh->length != 0 &&
65 		platform_timer + gh->length <= acpi_gtdt_desc.gtdt_end);
66 }
67 
68 static __init void *next_platform_timer(void *platform_timer)
69 {
70 	struct acpi_gtdt_header *gh = platform_timer;
71 
72 	return platform_timer + gh->length;
73 }
74 
75 #define for_each_platform_timer(_g)				\
76 	for (_g = acpi_gtdt_desc.platform_timer; platform_timer_valid(_g);\
77 	     _g = next_platform_timer(_g))
78 
79 static inline bool is_timer_block(void *platform_timer)
80 {
81 	struct acpi_gtdt_header *gh = platform_timer;
82 
83 	return gh->type == ACPI_GTDT_TYPE_TIMER_BLOCK;
84 }
85 
86 static inline bool is_non_secure_watchdog(void *platform_timer)
87 {
88 	struct acpi_gtdt_header *gh = platform_timer;
89 	struct acpi_gtdt_watchdog *wd = platform_timer;
90 
91 	if (gh->type != ACPI_GTDT_TYPE_WATCHDOG)
92 		return false;
93 
94 	return !(wd->timer_flags & ACPI_GTDT_WATCHDOG_SECURE);
95 }
96 
97 static int __init map_gt_gsi(u32 interrupt, u32 flags)
98 {
99 	int trigger, polarity;
100 
101 	trigger = (flags & ACPI_GTDT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
102 			: ACPI_LEVEL_SENSITIVE;
103 
104 	polarity = (flags & ACPI_GTDT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
105 			: ACPI_ACTIVE_HIGH;
106 
107 	return acpi_register_gsi(NULL, interrupt, trigger, polarity);
108 }
109 
110 /**
111  * acpi_gtdt_map_ppi() - Map the PPIs of per-cpu arch_timer.
112  * @type:	the type of PPI.
113  *
114  * Note: Secure state is not managed by the kernel on ARM64 systems.
115  * So we only handle the non-secure timer PPIs,
116  * ARCH_TIMER_PHYS_SECURE_PPI is treated as invalid type.
117  *
118  * Return: the mapped PPI value, 0 if error.
119  */
120 int __init acpi_gtdt_map_ppi(int type)
121 {
122 	struct acpi_table_gtdt *gtdt = acpi_gtdt_desc.gtdt;
123 	struct acpi_gtdt_el2 *el2_vtimer = gtdt_to_el2_vtimer(gtdt);
124 
125 	switch (type) {
126 	case ARCH_TIMER_PHYS_NONSECURE_PPI:
127 		return map_gt_gsi(gtdt->non_secure_el1_interrupt,
128 				  gtdt->non_secure_el1_flags);
129 	case ARCH_TIMER_VIRT_PPI:
130 		return map_gt_gsi(gtdt->virtual_timer_interrupt,
131 				  gtdt->virtual_timer_flags);
132 
133 	case ARCH_TIMER_HYP_PPI:
134 		return map_gt_gsi(gtdt->non_secure_el2_interrupt,
135 				  gtdt->non_secure_el2_flags);
136 	case ARCH_TIMER_HYP_VIRT_PPI:
137 		if (el2_vtimer && el2_vtimer->virtual_el2_timer_gsiv)
138 			return map_gt_gsi(el2_vtimer->virtual_el2_timer_gsiv,
139 					  el2_vtimer->virtual_el2_timer_flags);
140 
141 		return 0;
142 	default:
143 		pr_err("Failed to map timer interrupt: invalid type.\n");
144 	}
145 
146 	return 0;
147 }
148 
149 /**
150  * acpi_gtdt_c3stop() - Got c3stop info from GTDT according to the type of PPI.
151  * @type:	the type of PPI.
152  *
153  * Return: true if the timer HW state is lost when a CPU enters an idle state,
154  * false otherwise
155  */
156 bool __init acpi_gtdt_c3stop(int type)
157 {
158 	struct acpi_table_gtdt *gtdt = acpi_gtdt_desc.gtdt;
159 	struct acpi_gtdt_el2 *el2_vtimer = gtdt_to_el2_vtimer(gtdt);
160 
161 	switch (type) {
162 	case ARCH_TIMER_PHYS_NONSECURE_PPI:
163 		return !(gtdt->non_secure_el1_flags & ACPI_GTDT_ALWAYS_ON);
164 
165 	case ARCH_TIMER_VIRT_PPI:
166 		return !(gtdt->virtual_timer_flags & ACPI_GTDT_ALWAYS_ON);
167 
168 	case ARCH_TIMER_HYP_PPI:
169 		return !(gtdt->non_secure_el2_flags & ACPI_GTDT_ALWAYS_ON);
170 
171 	case ARCH_TIMER_HYP_VIRT_PPI:
172 		return el2_vtimer && el2_vtimer->virtual_el2_timer_gsiv &&
173 		       !(el2_vtimer->virtual_el2_timer_flags & ACPI_GTDT_ALWAYS_ON);
174 
175 	default:
176 		pr_err("Failed to get c3stop info: invalid type.\n");
177 	}
178 
179 	return false;
180 }
181 
182 /**
183  * acpi_gtdt_init() - Get the info of GTDT table to prepare for further init.
184  * @table:			The pointer to GTDT table.
185  * @platform_timer_count:	It points to a integer variable which is used
186  *				for storing the number of platform timers.
187  *				This pointer could be NULL, if the caller
188  *				doesn't need this info.
189  *
190  * Return: 0 if success, -EINVAL if error.
191  */
192 int __init acpi_gtdt_init(struct acpi_table_header *table,
193 			  int *platform_timer_count)
194 {
195 	void *platform_timer;
196 	struct acpi_table_gtdt *gtdt;
197 	u32 cnt = 0;
198 
199 	gtdt = container_of(table, struct acpi_table_gtdt, header);
200 
201 	if ((gtdt->header.revision >= 3 && gtdt->header.length < sizeof(struct gtdt_v3)) ||
202 	    (gtdt->header.revision == 2 && gtdt->header.length < sizeof(*gtdt))) {
203 		pr_err(FW_BUG "GTDT with invalid size %d\n", gtdt->header.length);
204 		return -EINVAL;
205 	}
206 
207 	acpi_gtdt_desc.gtdt = gtdt;
208 	acpi_gtdt_desc.gtdt_end = (void *)table + table->length;
209 	acpi_gtdt_desc.platform_timer = NULL;
210 	if (platform_timer_count)
211 		*platform_timer_count = 0;
212 
213 	if (table->revision < 2) {
214 		pr_warn("Revision:%d doesn't support Platform Timers.\n",
215 			table->revision);
216 		return 0;
217 	}
218 
219 	if (!gtdt->platform_timer_count) {
220 		pr_debug("No Platform Timer.\n");
221 		return 0;
222 	}
223 
224 	acpi_gtdt_desc.platform_timer = (void *)gtdt + gtdt->platform_timer_offset;
225 	for_each_platform_timer(platform_timer)
226 		cnt++;
227 
228 	if (cnt != gtdt->platform_timer_count) {
229 		cnt = min(cnt, gtdt->platform_timer_count);
230 		pr_err(FW_BUG "limiting Platform Timer count to %d\n", cnt);
231 	}
232 
233 	if (!cnt) {
234 		acpi_gtdt_desc.platform_timer = NULL;
235 		return 0;
236 	}
237 
238 	if (platform_timer_count)
239 		*platform_timer_count = cnt;
240 
241 	return 0;
242 }
243 
244 static int __init gtdt_parse_timer_block(struct acpi_gtdt_timer_block *block,
245 					 struct arch_timer_mem *timer_mem)
246 {
247 	int i;
248 	struct arch_timer_mem_frame *frame;
249 	struct acpi_gtdt_timer_entry *gtdt_frame;
250 
251 	if (!block->timer_count) {
252 		pr_err(FW_BUG "GT block present, but frame count is zero.\n");
253 		return -ENODEV;
254 	}
255 
256 	if (block->timer_count > ARCH_TIMER_MEM_MAX_FRAMES) {
257 		pr_err(FW_BUG "GT block lists %d frames, ACPI spec only allows 8\n",
258 		       block->timer_count);
259 		return -EINVAL;
260 	}
261 
262 	timer_mem->cntctlbase = (phys_addr_t)block->block_address;
263 	/*
264 	 * The CNTCTLBase frame is 4KB (register offsets 0x000 - 0xFFC).
265 	 * See ARM DDI 0487A.k_iss10775, page I1-5129, Table I1-3
266 	 * "CNTCTLBase memory map".
267 	 */
268 	timer_mem->size = SZ_4K;
269 
270 	gtdt_frame = (void *)block + block->timer_offset;
271 	if (gtdt_frame + block->timer_count != (void *)block + block->header.length)
272 		return -EINVAL;
273 
274 	/*
275 	 * Get the GT timer Frame data for every GT Block Timer
276 	 */
277 	for (i = 0; i < block->timer_count; i++, gtdt_frame++) {
278 		if (gtdt_frame->common_flags & ACPI_GTDT_GT_IS_SECURE_TIMER)
279 			continue;
280 		if (gtdt_frame->frame_number >= ARCH_TIMER_MEM_MAX_FRAMES ||
281 		    !gtdt_frame->base_address || !gtdt_frame->timer_interrupt)
282 			goto error;
283 
284 		frame = &timer_mem->frame[gtdt_frame->frame_number];
285 
286 		/* duplicate frame */
287 		if (frame->valid)
288 			goto error;
289 
290 		frame->phys_irq = map_gt_gsi(gtdt_frame->timer_interrupt,
291 					     gtdt_frame->timer_flags);
292 		if (frame->phys_irq <= 0) {
293 			pr_warn("failed to map physical timer irq in frame %d.\n",
294 				gtdt_frame->frame_number);
295 			goto error;
296 		}
297 
298 		if (gtdt_frame->virtual_timer_interrupt) {
299 			frame->virt_irq =
300 				map_gt_gsi(gtdt_frame->virtual_timer_interrupt,
301 					   gtdt_frame->virtual_timer_flags);
302 			if (frame->virt_irq <= 0) {
303 				pr_warn("failed to map virtual timer irq in frame %d.\n",
304 					gtdt_frame->frame_number);
305 				goto error;
306 			}
307 		} else {
308 			pr_debug("virtual timer in frame %d not implemented.\n",
309 				 gtdt_frame->frame_number);
310 		}
311 
312 		frame->cntbase = gtdt_frame->base_address;
313 		/*
314 		 * The CNTBaseN frame is 4KB (register offsets 0x000 - 0xFFC).
315 		 * See ARM DDI 0487A.k_iss10775, page I1-5130, Table I1-4
316 		 * "CNTBaseN memory map".
317 		 */
318 		frame->size = SZ_4K;
319 		frame->valid = true;
320 	}
321 
322 	return 0;
323 
324 error:
325 	do {
326 		if (gtdt_frame->common_flags & ACPI_GTDT_GT_IS_SECURE_TIMER ||
327 		    gtdt_frame->frame_number >= ARCH_TIMER_MEM_MAX_FRAMES)
328 			continue;
329 
330 		frame = &timer_mem->frame[gtdt_frame->frame_number];
331 
332 		if (frame->phys_irq > 0)
333 			acpi_unregister_gsi(gtdt_frame->timer_interrupt);
334 		frame->phys_irq = 0;
335 
336 		if (frame->virt_irq > 0)
337 			acpi_unregister_gsi(gtdt_frame->virtual_timer_interrupt);
338 		frame->virt_irq = 0;
339 	} while (i-- > 0 && gtdt_frame--);
340 
341 	return -EINVAL;
342 }
343 
344 /*
345  * Initialize a SBSA generic Watchdog platform device info from GTDT
346  */
347 static int __init gtdt_import_sbsa_gwdt(struct acpi_gtdt_watchdog *wd,
348 					int index)
349 {
350 	struct platform_device *pdev;
351 	int irq;
352 
353 	/*
354 	 * According to SBSA specification the size of refresh and control
355 	 * frames of SBSA Generic Watchdog is SZ_4K(Offset 0x000 – 0xFFF).
356 	 */
357 	struct resource res[] = {
358 		DEFINE_RES_MEM(wd->control_frame_address, SZ_4K),
359 		DEFINE_RES_MEM(wd->refresh_frame_address, SZ_4K),
360 		{},
361 	};
362 	int nr_res = ARRAY_SIZE(res);
363 
364 	pr_debug("found a Watchdog (0x%llx/0x%llx gsi:%u flags:0x%x).\n",
365 		 wd->refresh_frame_address, wd->control_frame_address,
366 		 wd->timer_interrupt, wd->timer_flags);
367 
368 	if (!(wd->refresh_frame_address && wd->control_frame_address)) {
369 		pr_err(FW_BUG "failed to get the Watchdog base address.\n");
370 		return -EINVAL;
371 	}
372 
373 	irq = map_gt_gsi(wd->timer_interrupt, wd->timer_flags);
374 	res[2] = DEFINE_RES_IRQ(irq);
375 	if (irq <= 0) {
376 		pr_warn("failed to map the Watchdog interrupt.\n");
377 		nr_res--;
378 	}
379 
380 	/*
381 	 * Add a platform device named "sbsa-gwdt" to match the platform driver.
382 	 * "sbsa-gwdt": SBSA(Server Base System Architecture) Generic Watchdog
383 	 * The platform driver can get device info below by matching this name.
384 	 */
385 	pdev = platform_device_register_simple("sbsa-gwdt", index, res, nr_res);
386 	if (IS_ERR(pdev)) {
387 		if (irq > 0)
388 			acpi_unregister_gsi(wd->timer_interrupt);
389 		return PTR_ERR(pdev);
390 	}
391 
392 	return 0;
393 }
394 
395 static int __init gtdt_platform_timer_init(void)
396 {
397 	void *platform_timer;
398 	struct acpi_table_header *table;
399 	int ret, timer_count, gwdt_count = 0, mmio_timer_count = 0;
400 
401 	if (acpi_disabled)
402 		return 0;
403 
404 	if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_GTDT, 0, &table)))
405 		return -EINVAL;
406 
407 	/*
408 	 * Note: Even though the global variable acpi_gtdt_desc has been
409 	 * initialized by acpi_gtdt_init() while initializing the arch timers,
410 	 * when we call this function to get SBSA watchdogs info from GTDT, the
411 	 * pointers stashed in it are stale (since they are early temporary
412 	 * mappings carried out before acpi_permanent_mmap is set) and we need
413 	 * to re-initialize them with permanent mapped pointer values to let the
414 	 * GTDT parsing possible.
415 	 */
416 	ret = acpi_gtdt_init(table, &timer_count);
417 	if (ret || !timer_count)
418 		goto out_put_gtdt;
419 
420 	for_each_platform_timer(platform_timer) {
421 		ret = 0;
422 
423 		if (is_non_secure_watchdog(platform_timer)) {
424 			ret = gtdt_import_sbsa_gwdt(platform_timer, gwdt_count);
425 			if (ret)
426 				continue;
427 			gwdt_count++;
428 		} else 	if (is_timer_block(platform_timer)) {
429 			struct arch_timer_mem atm = {};
430 			struct platform_device *pdev;
431 
432 			ret = gtdt_parse_timer_block(platform_timer, &atm);
433 			if (ret)
434 				continue;
435 
436 			pdev = platform_device_register_data(NULL, "gtdt-arm-mmio-timer",
437 							     mmio_timer_count, &atm,
438 							     sizeof(atm));
439 			if (IS_ERR(pdev)) {
440 				pr_err("Can't register timer %d\n", mmio_timer_count);
441 				continue;
442 			}
443 
444 			mmio_timer_count++;
445 		}
446 	}
447 
448 	if (gwdt_count)
449 		pr_info("found %d SBSA generic Watchdog(s).\n", gwdt_count);
450 	if (mmio_timer_count)
451 		pr_info("found %d Generic MMIO timer(s).\n", mmio_timer_count);
452 
453 out_put_gtdt:
454 	acpi_put_table(table);
455 	return ret;
456 }
457 
458 device_initcall(gtdt_platform_timer_init);
459