gtdt.c 11 KB

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