1
2
3
4
5
6
7
8
9
10
11
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
27
28
29
30
31
32
33
34struct acpi_gtdt_descriptor {
35 struct acpi_table_gtdt *gtdt;
36 void *gtdt_end;
37 void *platform_timer;
38};
39
40static struct acpi_gtdt_descriptor acpi_gtdt_desc __initdata;
41
42static 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
57static 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
64static 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
75static 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
90
91
92
93
94
95
96
97
98int __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
122
123
124
125
126
127bool __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
150
151
152
153
154
155
156
157
158int __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
194static 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
215
216
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
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
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
265
266
267
268 frame->size = SZ_4K;
269 frame->valid = true;
270 }
271
272 return 0;
273
274error:
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
296
297
298
299
300
301
302
303
304int __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
330
331static 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
339
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
365
366
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
377static 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
391
392
393
394
395
396
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
417device_initcall(gtdt_sbsa_gwdt_init);
418