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