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 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 206 static 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 286 error: 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 * acpi_arch_timer_mem_init() - Get the info of all GT blocks in GTDT table. 308 * @timer_mem: The pointer to the array of struct arch_timer_mem for returning 309 * the result of parsing. The element number of this array should 310 * be platform_timer_count(the total number of platform timers). 311 * @timer_count: It points to a integer variable which is used for storing the 312 * number of GT blocks we have parsed. 313 * 314 * Return: 0 if success, -EINVAL/-ENODEV if error. 315 */ 316 int __init acpi_arch_timer_mem_init(struct arch_timer_mem *timer_mem, 317 int *timer_count) 318 { 319 int ret; 320 void *platform_timer; 321 322 *timer_count = 0; 323 for_each_platform_timer(platform_timer) { 324 if (is_timer_block(platform_timer)) { 325 ret = gtdt_parse_timer_block(platform_timer, timer_mem); 326 if (ret) 327 return ret; 328 timer_mem++; 329 (*timer_count)++; 330 } 331 } 332 333 if (*timer_count) 334 pr_info("found %d memory-mapped timer block(s).\n", 335 *timer_count); 336 337 return 0; 338 } 339 340 /* 341 * Initialize a SBSA generic Watchdog platform device info from GTDT 342 */ 343 static int __init gtdt_import_sbsa_gwdt(struct acpi_gtdt_watchdog *wd, 344 int index) 345 { 346 struct platform_device *pdev; 347 int irq; 348 349 /* 350 * According to SBSA specification the size of refresh and control 351 * frames of SBSA Generic Watchdog is SZ_4K(Offset 0x000 – 0xFFF). 352 */ 353 struct resource res[] = { 354 DEFINE_RES_MEM(wd->control_frame_address, SZ_4K), 355 DEFINE_RES_MEM(wd->refresh_frame_address, SZ_4K), 356 {}, 357 }; 358 int nr_res = ARRAY_SIZE(res); 359 360 pr_debug("found a Watchdog (0x%llx/0x%llx gsi:%u flags:0x%x).\n", 361 wd->refresh_frame_address, wd->control_frame_address, 362 wd->timer_interrupt, wd->timer_flags); 363 364 if (!(wd->refresh_frame_address && wd->control_frame_address)) { 365 pr_err(FW_BUG "failed to get the Watchdog base address.\n"); 366 return -EINVAL; 367 } 368 369 irq = map_gt_gsi(wd->timer_interrupt, wd->timer_flags); 370 res[2] = DEFINE_RES_IRQ(irq); 371 if (irq <= 0) { 372 pr_warn("failed to map the Watchdog interrupt.\n"); 373 nr_res--; 374 } 375 376 /* 377 * Add a platform device named "sbsa-gwdt" to match the platform driver. 378 * "sbsa-gwdt": SBSA(Server Base System Architecture) Generic Watchdog 379 * The platform driver can get device info below by matching this name. 380 */ 381 pdev = platform_device_register_simple("sbsa-gwdt", index, res, nr_res); 382 if (IS_ERR(pdev)) { 383 if (irq > 0) 384 acpi_unregister_gsi(wd->timer_interrupt); 385 return PTR_ERR(pdev); 386 } 387 388 return 0; 389 } 390 391 static int __init gtdt_sbsa_gwdt_init(void) 392 { 393 void *platform_timer; 394 struct acpi_table_header *table; 395 int ret, timer_count, gwdt_count = 0; 396 397 if (acpi_disabled) 398 return 0; 399 400 if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_GTDT, 0, &table))) 401 return -EINVAL; 402 403 /* 404 * Note: Even though the global variable acpi_gtdt_desc has been 405 * initialized by acpi_gtdt_init() while initializing the arch timers, 406 * when we call this function to get SBSA watchdogs info from GTDT, the 407 * pointers stashed in it are stale (since they are early temporary 408 * mappings carried out before acpi_permanent_mmap is set) and we need 409 * to re-initialize them with permanent mapped pointer values to let the 410 * GTDT parsing possible. 411 */ 412 ret = acpi_gtdt_init(table, &timer_count); 413 if (ret || !timer_count) 414 goto out_put_gtdt; 415 416 for_each_platform_timer(platform_timer) { 417 if (is_non_secure_watchdog(platform_timer)) { 418 ret = gtdt_import_sbsa_gwdt(platform_timer, gwdt_count); 419 if (ret) 420 break; 421 gwdt_count++; 422 } 423 } 424 425 if (gwdt_count) 426 pr_info("found %d SBSA generic Watchdog(s).\n", gwdt_count); 427 428 out_put_gtdt: 429 acpi_put_table(table); 430 return ret; 431 } 432 433 device_initcall(gtdt_sbsa_gwdt_init); 434