gtdt.c 11 KB

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