xref: /openbmc/linux/drivers/acpi/arm64/gtdt.c (revision 2359ccdd)
1 /*
2  * ARM Specific GTDT table Support
3  *
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>
8  *
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.
12  */
13 
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>
19 
20 #include <clocksource/arm_arch_timer.h>
21 
22 #undef pr_fmt
23 #define pr_fmt(fmt) "ACPI GTDT: " fmt
24 
25 /**
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
30  *
31  * The struct store the key info of GTDT table, it should be initialized by
32  * acpi_gtdt_init.
33  */
34 struct acpi_gtdt_descriptor {
35 	struct acpi_table_gtdt *gtdt;
36 	void *gtdt_end;
37 	void *platform_timer;
38 };
39 
40 static struct acpi_gtdt_descriptor acpi_gtdt_desc __initdata;
41 
42 static inline void *next_platform_timer(void *platform_timer)
43 {
44 	struct acpi_gtdt_header *gh = platform_timer;
45 
46 	platform_timer += gh->length;
47 	if (platform_timer < acpi_gtdt_desc.gtdt_end)
48 		return platform_timer;
49 
50 	return NULL;
51 }
52 
53 #define for_each_platform_timer(_g)				\
54 	for (_g = acpi_gtdt_desc.platform_timer; _g;	\
55 	     _g = next_platform_timer(_g))
56 
57 static inline bool is_timer_block(void *platform_timer)
58 {
59 	struct acpi_gtdt_header *gh = platform_timer;
60 
61 	return gh->type == ACPI_GTDT_TYPE_TIMER_BLOCK;
62 }
63 
64 static inline bool is_non_secure_watchdog(void *platform_timer)
65 {
66 	struct acpi_gtdt_header *gh = platform_timer;
67 	struct acpi_gtdt_watchdog *wd = platform_timer;
68 
69 	if (gh->type != ACPI_GTDT_TYPE_WATCHDOG)
70 		return false;
71 
72 	return !(wd->timer_flags & ACPI_GTDT_WATCHDOG_SECURE);
73 }
74 
75 static int __init map_gt_gsi(u32 interrupt, u32 flags)
76 {
77 	int trigger, polarity;
78 
79 	trigger = (flags & ACPI_GTDT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
80 			: ACPI_LEVEL_SENSITIVE;
81 
82 	polarity = (flags & ACPI_GTDT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
83 			: ACPI_ACTIVE_HIGH;
84 
85 	return acpi_register_gsi(NULL, interrupt, trigger, polarity);
86 }
87 
88 /**
89  * acpi_gtdt_map_ppi() - Map the PPIs of per-cpu arch_timer.
90  * @type:	the type of PPI.
91  *
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.
95  *
96  * Return: the mapped PPI value, 0 if error.
97  */
98 int __init acpi_gtdt_map_ppi(int type)
99 {
100 	struct acpi_table_gtdt *gtdt = acpi_gtdt_desc.gtdt;
101 
102 	switch (type) {
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);
109 
110 	case ARCH_TIMER_HYP_PPI:
111 		return map_gt_gsi(gtdt->non_secure_el2_interrupt,
112 				  gtdt->non_secure_el2_flags);
113 	default:
114 		pr_err("Failed to map timer interrupt: invalid type.\n");
115 	}
116 
117 	return 0;
118 }
119 
120 /**
121  * acpi_gtdt_c3stop() - Got c3stop info from GTDT according to the type of PPI.
122  * @type:	the type of PPI.
123  *
124  * Return: true if the timer HW state is lost when a CPU enters an idle state,
125  * false otherwise
126  */
127 bool __init acpi_gtdt_c3stop(int type)
128 {
129 	struct acpi_table_gtdt *gtdt = acpi_gtdt_desc.gtdt;
130 
131 	switch (type) {
132 	case ARCH_TIMER_PHYS_NONSECURE_PPI:
133 		return !(gtdt->non_secure_el1_flags & ACPI_GTDT_ALWAYS_ON);
134 
135 	case ARCH_TIMER_VIRT_PPI:
136 		return !(gtdt->virtual_timer_flags & ACPI_GTDT_ALWAYS_ON);
137 
138 	case ARCH_TIMER_HYP_PPI:
139 		return !(gtdt->non_secure_el2_flags & ACPI_GTDT_ALWAYS_ON);
140 
141 	default:
142 		pr_err("Failed to get c3stop info: invalid type.\n");
143 	}
144 
145 	return false;
146 }
147 
148 /**
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.
155  *
156  * Return: 0 if success, -EINVAL if error.
157  */
158 int __init acpi_gtdt_init(struct acpi_table_header *table,
159 			  int *platform_timer_count)
160 {
161 	void *platform_timer;
162 	struct acpi_table_gtdt *gtdt;
163 
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;
170 
171 	if (table->revision < 2) {
172 		pr_warn("Revision:%d doesn't support Platform Timers.\n",
173 			table->revision);
174 		return 0;
175 	}
176 
177 	if (!gtdt->platform_timer_count) {
178 		pr_debug("No Platform Timer.\n");
179 		return 0;
180 	}
181 
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");
185 		return -EINVAL;
186 	}
187 	acpi_gtdt_desc.platform_timer = platform_timer;
188 	if (platform_timer_count)
189 		*platform_timer_count = gtdt->platform_timer_count;
190 
191 	return 0;
192 }
193 
194 static int __init gtdt_parse_timer_block(struct acpi_gtdt_timer_block *block,
195 					 struct arch_timer_mem *timer_mem)
196 {
197 	int i;
198 	struct arch_timer_mem_frame *frame;
199 	struct acpi_gtdt_timer_entry *gtdt_frame;
200 
201 	if (!block->timer_count) {
202 		pr_err(FW_BUG "GT block present, but frame count is zero.\n");
203 		return -ENODEV;
204 	}
205 
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",
208 		       block->timer_count);
209 		return -EINVAL;
210 	}
211 
212 	timer_mem->cntctlbase = (phys_addr_t)block->block_address;
213 	/*
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".
217 	 */
218 	timer_mem->size = SZ_4K;
219 
220 	gtdt_frame = (void *)block + block->timer_offset;
221 	if (gtdt_frame + block->timer_count != (void *)block + block->header.length)
222 		return -EINVAL;
223 
224 	/*
225 	 * Get the GT timer Frame data for every GT Block Timer
226 	 */
227 	for (i = 0; i < block->timer_count; i++, gtdt_frame++) {
228 		if (gtdt_frame->common_flags & ACPI_GTDT_GT_IS_SECURE_TIMER)
229 			continue;
230 		if (gtdt_frame->frame_number >= ARCH_TIMER_MEM_MAX_FRAMES ||
231 		    !gtdt_frame->base_address || !gtdt_frame->timer_interrupt)
232 			goto error;
233 
234 		frame = &timer_mem->frame[gtdt_frame->frame_number];
235 
236 		/* duplicate frame */
237 		if (frame->valid)
238 			goto error;
239 
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);
245 			goto error;
246 		}
247 
248 		if (gtdt_frame->virtual_timer_interrupt) {
249 			frame->virt_irq =
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);
255 				goto error;
256 			}
257 		} else {
258 			pr_debug("virtual timer in frame %d not implemented.\n",
259 				 gtdt_frame->frame_number);
260 		}
261 
262 		frame->cntbase = gtdt_frame->base_address;
263 		/*
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".
267 		 */
268 		frame->size = SZ_4K;
269 		frame->valid = true;
270 	}
271 
272 	return 0;
273 
274 error:
275 	do {
276 		if (gtdt_frame->common_flags & ACPI_GTDT_GT_IS_SECURE_TIMER ||
277 		    gtdt_frame->frame_number >= ARCH_TIMER_MEM_MAX_FRAMES)
278 			continue;
279 
280 		frame = &timer_mem->frame[gtdt_frame->frame_number];
281 
282 		if (frame->phys_irq > 0)
283 			acpi_unregister_gsi(gtdt_frame->timer_interrupt);
284 		frame->phys_irq = 0;
285 
286 		if (frame->virt_irq > 0)
287 			acpi_unregister_gsi(gtdt_frame->virtual_timer_interrupt);
288 		frame->virt_irq = 0;
289 	} while (i-- >= 0 && gtdt_frame--);
290 
291 	return -EINVAL;
292 }
293 
294 /**
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.
301  *
302  * Return: 0 if success, -EINVAL/-ENODEV if error.
303  */
304 int __init acpi_arch_timer_mem_init(struct arch_timer_mem *timer_mem,
305 				    int *timer_count)
306 {
307 	int ret;
308 	void *platform_timer;
309 
310 	*timer_count = 0;
311 	for_each_platform_timer(platform_timer) {
312 		if (is_timer_block(platform_timer)) {
313 			ret = gtdt_parse_timer_block(platform_timer, timer_mem);
314 			if (ret)
315 				return ret;
316 			timer_mem++;
317 			(*timer_count)++;
318 		}
319 	}
320 
321 	if (*timer_count)
322 		pr_info("found %d memory-mapped timer block(s).\n",
323 			*timer_count);
324 
325 	return 0;
326 }
327 
328 /*
329  * Initialize a SBSA generic Watchdog platform device info from GTDT
330  */
331 static int __init gtdt_import_sbsa_gwdt(struct acpi_gtdt_watchdog *wd,
332 					int index)
333 {
334 	struct platform_device *pdev;
335 	int irq = map_gt_gsi(wd->timer_interrupt, wd->timer_flags);
336 
337 	/*
338 	 * According to SBSA specification the size of refresh and control
339 	 * frames of SBSA Generic Watchdog is SZ_4K(Offset 0x000 – 0xFFF).
340 	 */
341 	struct resource res[] = {
342 		DEFINE_RES_MEM(wd->control_frame_address, SZ_4K),
343 		DEFINE_RES_MEM(wd->refresh_frame_address, SZ_4K),
344 		DEFINE_RES_IRQ(irq),
345 	};
346 	int nr_res = ARRAY_SIZE(res);
347 
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);
351 
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);
355 		return -EINVAL;
356 	}
357 
358 	if (irq <= 0) {
359 		pr_warn("failed to map the Watchdog interrupt.\n");
360 		nr_res--;
361 	}
362 
363 	/*
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.
367 	 */
368 	pdev = platform_device_register_simple("sbsa-gwdt", index, res, nr_res);
369 	if (IS_ERR(pdev)) {
370 		acpi_unregister_gsi(wd->timer_interrupt);
371 		return PTR_ERR(pdev);
372 	}
373 
374 	return 0;
375 }
376 
377 static int __init gtdt_sbsa_gwdt_init(void)
378 {
379 	void *platform_timer;
380 	struct acpi_table_header *table;
381 	int ret, timer_count, gwdt_count = 0;
382 
383 	if (acpi_disabled)
384 		return 0;
385 
386 	if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_GTDT, 0, &table)))
387 		return -EINVAL;
388 
389 	/*
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.
397 	 */
398 	ret = acpi_gtdt_init(table, &timer_count);
399 	if (ret || !timer_count)
400 		return ret;
401 
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);
405 			if (ret)
406 				break;
407 			gwdt_count++;
408 		}
409 	}
410 
411 	if (gwdt_count)
412 		pr_info("found %d SBSA generic Watchdog(s).\n", gwdt_count);
413 
414 	return ret;
415 }
416 
417 device_initcall(gtdt_sbsa_gwdt_init);
418