xref: /linux/drivers/acpi/arm64/gtdt.c (revision fcb3ad4366b9c810cbb9da34c076a9a52d8aa1e0)
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 static struct acpi_gtdt_descriptor acpi_gtdt_desc __initdata;
38 
39 static __init bool platform_timer_valid(void *platform_timer)
40 {
41 	struct acpi_gtdt_header *gh = platform_timer;
42 
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);
47 }
48 
49 static __init void *next_platform_timer(void *platform_timer)
50 {
51 	struct acpi_gtdt_header *gh = platform_timer;
52 
53 	return platform_timer + gh->length;
54 }
55 
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))
59 
60 static inline bool is_timer_block(void *platform_timer)
61 {
62 	struct acpi_gtdt_header *gh = platform_timer;
63 
64 	return gh->type == ACPI_GTDT_TYPE_TIMER_BLOCK;
65 }
66 
67 static inline bool is_non_secure_watchdog(void *platform_timer)
68 {
69 	struct acpi_gtdt_header *gh = platform_timer;
70 	struct acpi_gtdt_watchdog *wd = platform_timer;
71 
72 	if (gh->type != ACPI_GTDT_TYPE_WATCHDOG)
73 		return false;
74 
75 	return !(wd->timer_flags & ACPI_GTDT_WATCHDOG_SECURE);
76 }
77 
78 static int __init map_gt_gsi(u32 interrupt, u32 flags)
79 {
80 	int trigger, polarity;
81 
82 	trigger = (flags & ACPI_GTDT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
83 			: ACPI_LEVEL_SENSITIVE;
84 
85 	polarity = (flags & ACPI_GTDT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
86 			: ACPI_ACTIVE_HIGH;
87 
88 	return acpi_register_gsi(NULL, interrupt, trigger, polarity);
89 }
90 
91 /**
92  * acpi_gtdt_map_ppi() - Map the PPIs of per-cpu arch_timer.
93  * @type:	the type of PPI.
94  *
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.
98  *
99  * Return: the mapped PPI value, 0 if error.
100  */
101 int __init acpi_gtdt_map_ppi(int type)
102 {
103 	struct acpi_table_gtdt *gtdt = acpi_gtdt_desc.gtdt;
104 
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);
112 
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");
118 	}
119 
120 	return 0;
121 }
122 
123 /**
124  * acpi_gtdt_c3stop() - Got c3stop info from GTDT according to the type of PPI.
125  * @type:	the type of PPI.
126  *
127  * Return: true if the timer HW state is lost when a CPU enters an idle state,
128  * false otherwise
129  */
130 bool __init acpi_gtdt_c3stop(int type)
131 {
132 	struct acpi_table_gtdt *gtdt = acpi_gtdt_desc.gtdt;
133 
134 	switch (type) {
135 	case ARCH_TIMER_PHYS_NONSECURE_PPI:
136 		return !(gtdt->non_secure_el1_flags & ACPI_GTDT_ALWAYS_ON);
137 
138 	case ARCH_TIMER_VIRT_PPI:
139 		return !(gtdt->virtual_timer_flags & ACPI_GTDT_ALWAYS_ON);
140 
141 	case ARCH_TIMER_HYP_PPI:
142 		return !(gtdt->non_secure_el2_flags & ACPI_GTDT_ALWAYS_ON);
143 
144 	default:
145 		pr_err("Failed to get c3stop info: invalid type.\n");
146 	}
147 
148 	return false;
149 }
150 
151 /**
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.
158  *
159  * Return: 0 if success, -EINVAL if error.
160  */
161 int __init acpi_gtdt_init(struct acpi_table_header *table,
162 			  int *platform_timer_count)
163 {
164 	void *platform_timer;
165 	struct acpi_table_gtdt *gtdt;
166 	int cnt = 0;
167 
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;
174 
175 	if (table->revision < 2) {
176 		pr_warn("Revision:%d doesn't support Platform Timers.\n",
177 			table->revision);
178 		return 0;
179 	}
180 
181 	if (!gtdt->platform_timer_count) {
182 		pr_debug("No Platform Timer.\n");
183 		return 0;
184 	}
185 
186 	acpi_gtdt_desc.platform_timer = (void *)gtdt + gtdt->platform_timer_offset;
187 	for_each_platform_timer(platform_timer)
188 		cnt++;
189 
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;
194 	}
195 
196 	if (platform_timer_count)
197 		*platform_timer_count = gtdt->platform_timer_count;
198 
199 	return 0;
200 }
201 
202 static int __init gtdt_parse_timer_block(struct acpi_gtdt_timer_block *block,
203 					 struct arch_timer_mem *timer_mem)
204 {
205 	int i;
206 	struct arch_timer_mem_frame *frame;
207 	struct acpi_gtdt_timer_entry *gtdt_frame;
208 
209 	if (!block->timer_count) {
210 		pr_err(FW_BUG "GT block present, but frame count is zero.\n");
211 		return -ENODEV;
212 	}
213 
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;
218 	}
219 
220 	timer_mem->cntctlbase = (phys_addr_t)block->block_address;
221 	/*
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".
225 	 */
226 	timer_mem->size = SZ_4K;
227 
228 	gtdt_frame = (void *)block + block->timer_offset;
229 	if (gtdt_frame + block->timer_count != (void *)block + block->header.length)
230 		return -EINVAL;
231 
232 	/*
233 	 * Get the GT timer Frame data for every GT Block Timer
234 	 */
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;
241 
242 		frame = &timer_mem->frame[gtdt_frame->frame_number];
243 
244 		/* duplicate frame */
245 		if (frame->valid)
246 			goto error;
247 
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;
254 		}
255 
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;
264 			}
265 		} else {
266 			pr_debug("virtual timer in frame %d not implemented.\n",
267 				 gtdt_frame->frame_number);
268 		}
269 
270 		frame->cntbase = gtdt_frame->base_address;
271 		/*
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".
275 		 */
276 		frame->size = SZ_4K;
277 		frame->valid = true;
278 	}
279 
280 	return 0;
281 
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;
287 
288 		frame = &timer_mem->frame[gtdt_frame->frame_number];
289 
290 		if (frame->phys_irq > 0)
291 			acpi_unregister_gsi(gtdt_frame->timer_interrupt);
292 		frame->phys_irq = 0;
293 
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--);
298 
299 	return -EINVAL;
300 }
301 
302 /**
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.
309  *
310  * Return: 0 if success, -EINVAL/-ENODEV if error.
311  */
312 int __init acpi_arch_timer_mem_init(struct arch_timer_mem *timer_mem,
313 				    int *timer_count)
314 {
315 	int ret;
316 	void *platform_timer;
317 
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)++;
326 		}
327 	}
328 
329 	if (*timer_count)
330 		pr_info("found %d memory-mapped timer block(s).\n",
331 			*timer_count);
332 
333 	return 0;
334 }
335 
336 /*
337  * Initialize a SBSA generic Watchdog platform device info from GTDT
338  */
339 static int __init gtdt_import_sbsa_gwdt(struct acpi_gtdt_watchdog *wd,
340 					int index)
341 {
342 	struct platform_device *pdev;
343 	int irq;
344 
345 	/*
346 	 * According to SBSA specification the size of refresh and control
347 	 * frames of SBSA Generic Watchdog is SZ_4K(Offset 0x000 – 0xFFF).
348 	 */
349 	struct resource res[] = {
350 		DEFINE_RES_MEM(wd->control_frame_address, SZ_4K),
351 		DEFINE_RES_MEM(wd->refresh_frame_address, SZ_4K),
352 		{},
353 	};
354 	int nr_res = ARRAY_SIZE(res);
355 
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);
359 
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;
363 	}
364 
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--;
370 	}
371 
372 	/*
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.
376 	 */
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);
382 	}
383 
384 	return 0;
385 }
386 
387 static int __init gtdt_sbsa_gwdt_init(void)
388 {
389 	void *platform_timer;
390 	struct acpi_table_header *table;
391 	int ret, timer_count, gwdt_count = 0;
392 
393 	if (acpi_disabled)
394 		return 0;
395 
396 	if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_GTDT, 0, &table)))
397 		return -EINVAL;
398 
399 	/*
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.
407 	 */
408 	ret = acpi_gtdt_init(table, &timer_count);
409 	if (ret || !timer_count)
410 		goto out_put_gtdt;
411 
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++;
418 		}
419 	}
420 
421 	if (gwdt_count)
422 		pr_info("found %d SBSA generic Watchdog(s).\n", gwdt_count);
423 
424 out_put_gtdt:
425 	acpi_put_table(table);
426 	return ret;
427 }
428 
429 device_initcall(gtdt_sbsa_gwdt_init);
430