apb_timer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * apb_timer.c: Driver for Langwell APB timers
  3. *
  4. * (C) Copyright 2009 Intel Corporation
  5. * Author: Jacob Pan (jacob.jun.pan@intel.com)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; version 2
  10. * of the License.
  11. *
  12. * Note:
  13. * Langwell is the south complex of Intel Moorestown MID platform. There are
  14. * eight external timers in total that can be used by the operating system.
  15. * The timer information, such as frequency and addresses, is provided to the
  16. * OS via SFI tables.
  17. * Timer interrupts are routed via FW/HW emulated IOAPIC independently via
  18. * individual redirection table entries (RTE).
  19. * Unlike HPET, there is no master counter, therefore one of the timers are
  20. * used as clocksource. The overall allocation looks like:
  21. * - timer 0 - NR_CPUs for per cpu timer
  22. * - one timer for clocksource
  23. * - one timer for watchdog driver.
  24. * It is also worth notice that APB timer does not support true one-shot mode,
  25. * free-running mode will be used here to emulate one-shot mode.
  26. * APB timer can also be used as broadcast timer along with per cpu local APIC
  27. * timer, but by default APB timer has higher rating than local APIC timers.
  28. */
  29. #include <linux/delay.h>
  30. #include <linux/dw_apb_timer.h>
  31. #include <linux/errno.h>
  32. #include <linux/init.h>
  33. #include <linux/slab.h>
  34. #include <linux/pm.h>
  35. #include <linux/sfi.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/cpu.h>
  38. #include <linux/irq.h>
  39. #include <asm/fixmap.h>
  40. #include <asm/apb_timer.h>
  41. #include <asm/mrst.h>
  42. #include <asm/time.h>
  43. #define APBT_CLOCKEVENT_RATING 110
  44. #define APBT_CLOCKSOURCE_RATING 250
  45. #define APBT_CLOCKEVENT0_NUM (0)
  46. #define APBT_CLOCKSOURCE_NUM (2)
  47. static phys_addr_t apbt_address;
  48. static int apb_timer_block_enabled;
  49. static void __iomem *apbt_virt_address;
  50. /*
  51. * Common DW APB timer info
  52. */
  53. static unsigned long apbt_freq;
  54. struct apbt_dev {
  55. struct dw_apb_clock_event_device *timer;
  56. unsigned int num;
  57. int cpu;
  58. unsigned int irq;
  59. char name[10];
  60. };
  61. static struct dw_apb_clocksource *clocksource_apbt;
  62. static inline void __iomem *adev_virt_addr(struct apbt_dev *adev)
  63. {
  64. return apbt_virt_address + adev->num * APBTMRS_REG_SIZE;
  65. }
  66. static DEFINE_PER_CPU(struct apbt_dev, cpu_apbt_dev);
  67. #ifdef CONFIG_SMP
  68. static unsigned int apbt_num_timers_used;
  69. #endif
  70. static inline void apbt_set_mapping(void)
  71. {
  72. struct sfi_timer_table_entry *mtmr;
  73. int phy_cs_timer_id = 0;
  74. if (apbt_virt_address) {
  75. pr_debug("APBT base already mapped\n");
  76. return;
  77. }
  78. mtmr = sfi_get_mtmr(APBT_CLOCKEVENT0_NUM);
  79. if (mtmr == NULL) {
  80. printk(KERN_ERR "Failed to get MTMR %d from SFI\n",
  81. APBT_CLOCKEVENT0_NUM);
  82. return;
  83. }
  84. apbt_address = (phys_addr_t)mtmr->phys_addr;
  85. if (!apbt_address) {
  86. printk(KERN_WARNING "No timer base from SFI, use default\n");
  87. apbt_address = APBT_DEFAULT_BASE;
  88. }
  89. apbt_virt_address = ioremap_nocache(apbt_address, APBT_MMAP_SIZE);
  90. if (!apbt_virt_address) {
  91. pr_debug("Failed mapping APBT phy address at %lu\n",\
  92. (unsigned long)apbt_address);
  93. goto panic_noapbt;
  94. }
  95. apbt_freq = mtmr->freq_hz;
  96. sfi_free_mtmr(mtmr);
  97. /* Now figure out the physical timer id for clocksource device */
  98. mtmr = sfi_get_mtmr(APBT_CLOCKSOURCE_NUM);
  99. if (mtmr == NULL)
  100. goto panic_noapbt;
  101. /* Now figure out the physical timer id */
  102. pr_debug("Use timer %d for clocksource\n",
  103. (int)(mtmr->phys_addr & 0xff) / APBTMRS_REG_SIZE);
  104. phy_cs_timer_id = (unsigned int)(mtmr->phys_addr & 0xff) /
  105. APBTMRS_REG_SIZE;
  106. clocksource_apbt = dw_apb_clocksource_init(APBT_CLOCKSOURCE_RATING,
  107. "apbt0", apbt_virt_address + phy_cs_timer_id *
  108. APBTMRS_REG_SIZE, apbt_freq);
  109. return;
  110. panic_noapbt:
  111. panic("Failed to setup APB system timer\n");
  112. }
  113. static inline void apbt_clear_mapping(void)
  114. {
  115. iounmap(apbt_virt_address);
  116. apbt_virt_address = NULL;
  117. }
  118. /*
  119. * APBT timer interrupt enable / disable
  120. */
  121. static inline int is_apbt_capable(void)
  122. {
  123. return apbt_virt_address ? 1 : 0;
  124. }
  125. static int __init apbt_clockevent_register(void)
  126. {
  127. struct sfi_timer_table_entry *mtmr;
  128. struct apbt_dev *adev = &__get_cpu_var(cpu_apbt_dev);
  129. mtmr = sfi_get_mtmr(APBT_CLOCKEVENT0_NUM);
  130. if (mtmr == NULL) {
  131. printk(KERN_ERR "Failed to get MTMR %d from SFI\n",
  132. APBT_CLOCKEVENT0_NUM);
  133. return -ENODEV;
  134. }
  135. adev->num = smp_processor_id();
  136. adev->timer = dw_apb_clockevent_init(smp_processor_id(), "apbt0",
  137. mrst_timer_options == MRST_TIMER_LAPIC_APBT ?
  138. APBT_CLOCKEVENT_RATING - 100 : APBT_CLOCKEVENT_RATING,
  139. adev_virt_addr(adev), 0, apbt_freq);
  140. /* Firmware does EOI handling for us. */
  141. adev->timer->eoi = NULL;
  142. if (mrst_timer_options == MRST_TIMER_LAPIC_APBT) {
  143. global_clock_event = &adev->timer->ced;
  144. printk(KERN_DEBUG "%s clockevent registered as global\n",
  145. global_clock_event->name);
  146. }
  147. dw_apb_clockevent_register(adev->timer);
  148. sfi_free_mtmr(mtmr);
  149. return 0;
  150. }
  151. #ifdef CONFIG_SMP
  152. static void apbt_setup_irq(struct apbt_dev *adev)
  153. {
  154. /* timer0 irq has been setup early */
  155. if (adev->irq == 0)
  156. return;
  157. irq_modify_status(adev->irq, 0, IRQ_MOVE_PCNTXT);
  158. irq_set_affinity(adev->irq, cpumask_of(adev->cpu));
  159. /* APB timer irqs are set up as mp_irqs, timer is edge type */
  160. __irq_set_handler(adev->irq, handle_edge_irq, 0, "edge");
  161. }
  162. /* Should be called with per cpu */
  163. void apbt_setup_secondary_clock(void)
  164. {
  165. struct apbt_dev *adev;
  166. int cpu;
  167. /* Don't register boot CPU clockevent */
  168. cpu = smp_processor_id();
  169. if (!cpu)
  170. return;
  171. adev = &__get_cpu_var(cpu_apbt_dev);
  172. if (!adev->timer) {
  173. adev->timer = dw_apb_clockevent_init(cpu, adev->name,
  174. APBT_CLOCKEVENT_RATING, adev_virt_addr(adev),
  175. adev->irq, apbt_freq);
  176. adev->timer->eoi = NULL;
  177. } else {
  178. dw_apb_clockevent_resume(adev->timer);
  179. }
  180. printk(KERN_INFO "Registering CPU %d clockevent device %s, cpu %08x\n",
  181. cpu, adev->name, adev->cpu);
  182. apbt_setup_irq(adev);
  183. dw_apb_clockevent_register(adev->timer);
  184. return;
  185. }
  186. /*
  187. * this notify handler process CPU hotplug events. in case of S0i3, nonboot
  188. * cpus are disabled/enabled frequently, for performance reasons, we keep the
  189. * per cpu timer irq registered so that we do need to do free_irq/request_irq.
  190. *
  191. * TODO: it might be more reliable to directly disable percpu clockevent device
  192. * without the notifier chain. currently, cpu 0 may get interrupts from other
  193. * cpu timers during the offline process due to the ordering of notification.
  194. * the extra interrupt is harmless.
  195. */
  196. static int apbt_cpuhp_notify(struct notifier_block *n,
  197. unsigned long action, void *hcpu)
  198. {
  199. unsigned long cpu = (unsigned long)hcpu;
  200. struct apbt_dev *adev = &per_cpu(cpu_apbt_dev, cpu);
  201. switch (action & 0xf) {
  202. case CPU_DEAD:
  203. dw_apb_clockevent_pause(adev->timer);
  204. if (system_state == SYSTEM_RUNNING) {
  205. pr_debug("skipping APBT CPU %lu offline\n", cpu);
  206. } else if (adev) {
  207. pr_debug("APBT clockevent for cpu %lu offline\n", cpu);
  208. dw_apb_clockevent_stop(adev->timer);
  209. }
  210. break;
  211. default:
  212. pr_debug("APBT notified %lu, no action\n", action);
  213. }
  214. return NOTIFY_OK;
  215. }
  216. static __init int apbt_late_init(void)
  217. {
  218. if (mrst_timer_options == MRST_TIMER_LAPIC_APBT ||
  219. !apb_timer_block_enabled)
  220. return 0;
  221. /* This notifier should be called after workqueue is ready */
  222. hotcpu_notifier(apbt_cpuhp_notify, -20);
  223. return 0;
  224. }
  225. fs_initcall(apbt_late_init);
  226. #else
  227. void apbt_setup_secondary_clock(void) {}
  228. #endif /* CONFIG_SMP */
  229. static int apbt_clocksource_register(void)
  230. {
  231. u64 start, now;
  232. cycle_t t1;
  233. /* Start the counter, use timer 2 as source, timer 0/1 for event */
  234. dw_apb_clocksource_start(clocksource_apbt);
  235. /* Verify whether apbt counter works */
  236. t1 = dw_apb_clocksource_read(clocksource_apbt);
  237. rdtscll(start);
  238. /*
  239. * We don't know the TSC frequency yet, but waiting for
  240. * 200000 TSC cycles is safe:
  241. * 4 GHz == 50us
  242. * 1 GHz == 200us
  243. */
  244. do {
  245. rep_nop();
  246. rdtscll(now);
  247. } while ((now - start) < 200000UL);
  248. /* APBT is the only always on clocksource, it has to work! */
  249. if (t1 == dw_apb_clocksource_read(clocksource_apbt))
  250. panic("APBT counter not counting. APBT disabled\n");
  251. dw_apb_clocksource_register(clocksource_apbt);
  252. return 0;
  253. }
  254. /*
  255. * Early setup the APBT timer, only use timer 0 for booting then switch to
  256. * per CPU timer if possible.
  257. * returns 1 if per cpu apbt is setup
  258. * returns 0 if no per cpu apbt is chosen
  259. * panic if set up failed, this is the only platform timer on Moorestown.
  260. */
  261. void __init apbt_time_init(void)
  262. {
  263. #ifdef CONFIG_SMP
  264. int i;
  265. struct sfi_timer_table_entry *p_mtmr;
  266. unsigned int percpu_timer;
  267. struct apbt_dev *adev;
  268. #endif
  269. if (apb_timer_block_enabled)
  270. return;
  271. apbt_set_mapping();
  272. if (!apbt_virt_address)
  273. goto out_noapbt;
  274. /*
  275. * Read the frequency and check for a sane value, for ESL model
  276. * we extend the possible clock range to allow time scaling.
  277. */
  278. if (apbt_freq < APBT_MIN_FREQ || apbt_freq > APBT_MAX_FREQ) {
  279. pr_debug("APBT has invalid freq 0x%lx\n", apbt_freq);
  280. goto out_noapbt;
  281. }
  282. if (apbt_clocksource_register()) {
  283. pr_debug("APBT has failed to register clocksource\n");
  284. goto out_noapbt;
  285. }
  286. if (!apbt_clockevent_register())
  287. apb_timer_block_enabled = 1;
  288. else {
  289. pr_debug("APBT has failed to register clockevent\n");
  290. goto out_noapbt;
  291. }
  292. #ifdef CONFIG_SMP
  293. /* kernel cmdline disable apb timer, so we will use lapic timers */
  294. if (mrst_timer_options == MRST_TIMER_LAPIC_APBT) {
  295. printk(KERN_INFO "apbt: disabled per cpu timer\n");
  296. return;
  297. }
  298. pr_debug("%s: %d CPUs online\n", __func__, num_online_cpus());
  299. if (num_possible_cpus() <= sfi_mtimer_num) {
  300. percpu_timer = 1;
  301. apbt_num_timers_used = num_possible_cpus();
  302. } else {
  303. percpu_timer = 0;
  304. apbt_num_timers_used = 1;
  305. }
  306. pr_debug("%s: %d APB timers used\n", __func__, apbt_num_timers_used);
  307. /* here we set up per CPU timer data structure */
  308. for (i = 0; i < apbt_num_timers_used; i++) {
  309. adev = &per_cpu(cpu_apbt_dev, i);
  310. adev->num = i;
  311. adev->cpu = i;
  312. p_mtmr = sfi_get_mtmr(i);
  313. if (p_mtmr)
  314. adev->irq = p_mtmr->irq;
  315. else
  316. printk(KERN_ERR "Failed to get timer for cpu %d\n", i);
  317. snprintf(adev->name, sizeof(adev->name) - 1, "apbt%d", i);
  318. }
  319. #endif
  320. return;
  321. out_noapbt:
  322. apbt_clear_mapping();
  323. apb_timer_block_enabled = 0;
  324. panic("failed to enable APB timer\n");
  325. }
  326. /* called before apb_timer_enable, use early map */
  327. unsigned long apbt_quick_calibrate(void)
  328. {
  329. int i, scale;
  330. u64 old, new;
  331. cycle_t t1, t2;
  332. unsigned long khz = 0;
  333. u32 loop, shift;
  334. apbt_set_mapping();
  335. dw_apb_clocksource_start(clocksource_apbt);
  336. /* check if the timer can count down, otherwise return */
  337. old = dw_apb_clocksource_read(clocksource_apbt);
  338. i = 10000;
  339. while (--i) {
  340. if (old != dw_apb_clocksource_read(clocksource_apbt))
  341. break;
  342. }
  343. if (!i)
  344. goto failed;
  345. /* count 16 ms */
  346. loop = (apbt_freq / 1000) << 4;
  347. /* restart the timer to ensure it won't get to 0 in the calibration */
  348. dw_apb_clocksource_start(clocksource_apbt);
  349. old = dw_apb_clocksource_read(clocksource_apbt);
  350. old += loop;
  351. t1 = __native_read_tsc();
  352. do {
  353. new = dw_apb_clocksource_read(clocksource_apbt);
  354. } while (new < old);
  355. t2 = __native_read_tsc();
  356. shift = 5;
  357. if (unlikely(loop >> shift == 0)) {
  358. printk(KERN_INFO
  359. "APBT TSC calibration failed, not enough resolution\n");
  360. return 0;
  361. }
  362. scale = (int)div_u64((t2 - t1), loop >> shift);
  363. khz = (scale * (apbt_freq / 1000)) >> shift;
  364. printk(KERN_INFO "TSC freq calculated by APB timer is %lu khz\n", khz);
  365. return khz;
  366. failed:
  367. return 0;
  368. }