File Index
Files and directories
Sign in
FILE

gtdt.c

File preview with syntax highlighting.

linux-6.19.14/drivers/acpi/arm64/gtdt.c C
  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 */
 31struct acpi_gtdt_descriptor {
 32	struct acpi_table_gtdt *gtdt;
 33	void *gtdt_end;
 34	void *platform_timer;
 35};
 36
 37static struct acpi_gtdt_descriptor acpi_gtdt_desc __initdata;
 38
 39static __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
 49static __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
 60static 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
 67static 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
 78static 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 */
101int __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 */
130bool __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 */
161int __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	u32 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		cnt = min(cnt, gtdt->platform_timer_count);
192		pr_err(FW_BUG "limiting Platform Timer count to %d\n", cnt);
193	}
194
195	if (!cnt) {
196		acpi_gtdt_desc.platform_timer = NULL;
197		return 0;
198	}
199
200	if (platform_timer_count)
201		*platform_timer_count = cnt;
202
203	return 0;
204}
205
206static int __init gtdt_parse_timer_block(struct acpi_gtdt_timer_block *block,
207					 struct arch_timer_mem *timer_mem)
208{
209	int i;
210	struct arch_timer_mem_frame *frame;
211	struct acpi_gtdt_timer_entry *gtdt_frame;
212
213	if (!block->timer_count) {
214		pr_err(FW_BUG "GT block present, but frame count is zero.\n");
215		return -ENODEV;
216	}
217
218	if (block->timer_count > ARCH_TIMER_MEM_MAX_FRAMES) {
219		pr_err(FW_BUG "GT block lists %d frames, ACPI spec only allows 8\n",
220		       block->timer_count);
221		return -EINVAL;
222	}
223
224	timer_mem->cntctlbase = (phys_addr_t)block->block_address;
225	/*
226	 * The CNTCTLBase frame is 4KB (register offsets 0x000 - 0xFFC).
227	 * See ARM DDI 0487A.k_iss10775, page I1-5129, Table I1-3
228	 * "CNTCTLBase memory map".
229	 */
230	timer_mem->size = SZ_4K;
231
232	gtdt_frame = (void *)block + block->timer_offset;
233	if (gtdt_frame + block->timer_count != (void *)block + block->header.length)
234		return -EINVAL;
235
236	/*
237	 * Get the GT timer Frame data for every GT Block Timer
238	 */
239	for (i = 0; i < block->timer_count; i++, gtdt_frame++) {
240		if (gtdt_frame->common_flags & ACPI_GTDT_GT_IS_SECURE_TIMER)
241			continue;
242		if (gtdt_frame->frame_number >= ARCH_TIMER_MEM_MAX_FRAMES ||
243		    !gtdt_frame->base_address || !gtdt_frame->timer_interrupt)
244			goto error;
245
246		frame = &timer_mem->frame[gtdt_frame->frame_number];
247
248		/* duplicate frame */
249		if (frame->valid)
250			goto error;
251
252		frame->phys_irq = map_gt_gsi(gtdt_frame->timer_interrupt,
253					     gtdt_frame->timer_flags);
254		if (frame->phys_irq <= 0) {
255			pr_warn("failed to map physical timer irq in frame %d.\n",
256				gtdt_frame->frame_number);
257			goto error;
258		}
259
260		if (gtdt_frame->virtual_timer_interrupt) {
261			frame->virt_irq =
262				map_gt_gsi(gtdt_frame->virtual_timer_interrupt,
263					   gtdt_frame->virtual_timer_flags);
264			if (frame->virt_irq <= 0) {
265				pr_warn("failed to map virtual timer irq in frame %d.\n",
266					gtdt_frame->frame_number);
267				goto error;
268			}
269		} else {
270			pr_debug("virtual timer in frame %d not implemented.\n",
271				 gtdt_frame->frame_number);
272		}
273
274		frame->cntbase = gtdt_frame->base_address;
275		/*
276		 * The CNTBaseN frame is 4KB (register offsets 0x000 - 0xFFC).
277		 * See ARM DDI 0487A.k_iss10775, page I1-5130, Table I1-4
278		 * "CNTBaseN memory map".
279		 */
280		frame->size = SZ_4K;
281		frame->valid = true;
282	}
283
284	return 0;
285
286error:
287	do {
288		if (gtdt_frame->common_flags & ACPI_GTDT_GT_IS_SECURE_TIMER ||
289		    gtdt_frame->frame_number >= ARCH_TIMER_MEM_MAX_FRAMES)
290			continue;
291
292		frame = &timer_mem->frame[gtdt_frame->frame_number];
293
294		if (frame->phys_irq > 0)
295			acpi_unregister_gsi(gtdt_frame->timer_interrupt);
296		frame->phys_irq = 0;
297
298		if (frame->virt_irq > 0)
299			acpi_unregister_gsi(gtdt_frame->virtual_timer_interrupt);
300		frame->virt_irq = 0;
301	} while (i-- > 0 && gtdt_frame--);
302
303	return -EINVAL;
304}
305
306/*
307 * Initialize a SBSA generic Watchdog platform device info from GTDT
308 */
309static int __init gtdt_import_sbsa_gwdt(struct acpi_gtdt_watchdog *wd,
310					int index)
311{
312	struct platform_device *pdev;
313	int irq;
314
315	/*
316	 * According to SBSA specification the size of refresh and control
317	 * frames of SBSA Generic Watchdog is SZ_4K(Offset 0x000 – 0xFFF).
318	 */
319	struct resource res[] = {
320		DEFINE_RES_MEM(wd->control_frame_address, SZ_4K),
321		DEFINE_RES_MEM(wd->refresh_frame_address, SZ_4K),
322		{},
323	};
324	int nr_res = ARRAY_SIZE(res);
325
326	pr_debug("found a Watchdog (0x%llx/0x%llx gsi:%u flags:0x%x).\n",
327		 wd->refresh_frame_address, wd->control_frame_address,
328		 wd->timer_interrupt, wd->timer_flags);
329
330	if (!(wd->refresh_frame_address && wd->control_frame_address)) {
331		pr_err(FW_BUG "failed to get the Watchdog base address.\n");
332		return -EINVAL;
333	}
334
335	irq = map_gt_gsi(wd->timer_interrupt, wd->timer_flags);
336	res[2] = DEFINE_RES_IRQ(irq);
337	if (irq <= 0) {
338		pr_warn("failed to map the Watchdog interrupt.\n");
339		nr_res--;
340	}
341
342	/*
343	 * Add a platform device named "sbsa-gwdt" to match the platform driver.
344	 * "sbsa-gwdt": SBSA(Server Base System Architecture) Generic Watchdog
345	 * The platform driver can get device info below by matching this name.
346	 */
347	pdev = platform_device_register_simple("sbsa-gwdt", index, res, nr_res);
348	if (IS_ERR(pdev)) {
349		if (irq > 0)
350			acpi_unregister_gsi(wd->timer_interrupt);
351		return PTR_ERR(pdev);
352	}
353
354	return 0;
355}
356
357static int __init gtdt_platform_timer_init(void)
358{
359	void *platform_timer;
360	struct acpi_table_header *table;
361	int ret, timer_count, gwdt_count = 0, mmio_timer_count = 0;
362
363	if (acpi_disabled)
364		return 0;
365
366	if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_GTDT, 0, &table)))
367		return -EINVAL;
368
369	/*
370	 * Note: Even though the global variable acpi_gtdt_desc has been
371	 * initialized by acpi_gtdt_init() while initializing the arch timers,
372	 * when we call this function to get SBSA watchdogs info from GTDT, the
373	 * pointers stashed in it are stale (since they are early temporary
374	 * mappings carried out before acpi_permanent_mmap is set) and we need
375	 * to re-initialize them with permanent mapped pointer values to let the
376	 * GTDT parsing possible.
377	 */
378	ret = acpi_gtdt_init(table, &timer_count);
379	if (ret || !timer_count)
380		goto out_put_gtdt;
381
382	for_each_platform_timer(platform_timer) {
383		ret = 0;
384
385		if (is_non_secure_watchdog(platform_timer)) {
386			ret = gtdt_import_sbsa_gwdt(platform_timer, gwdt_count);
387			if (ret)
388				continue;
389			gwdt_count++;
390		} else 	if (is_timer_block(platform_timer)) {
391			struct arch_timer_mem atm = {};
392			struct platform_device *pdev;
393
394			ret = gtdt_parse_timer_block(platform_timer, &atm);
395			if (ret)
396				continue;
397
398			pdev = platform_device_register_data(NULL, "gtdt-arm-mmio-timer",
399							     mmio_timer_count, &atm,
400							     sizeof(atm));
401			if (IS_ERR(pdev)) {
402				pr_err("Can't register timer %d\n", mmio_timer_count);
403				continue;
404			}
405
406			mmio_timer_count++;
407		}
408	}
409
410	if (gwdt_count)
411		pr_info("found %d SBSA generic Watchdog(s).\n", gwdt_count);
412	if (mmio_timer_count)
413		pr_info("found %d Generic MMIO timer(s).\n", mmio_timer_count);
414
415out_put_gtdt:
416	acpi_put_table(table);
417	return ret;
418}
419
420device_initcall(gtdt_platform_timer_init);
~/ Read-only file preview.