apb_timer.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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/clocksource.h>
  30. #include <linux/clockchips.h>
  31. #include <linux/delay.h>
  32. #include <linux/errno.h>
  33. #include <linux/init.h>
  34. #include <linux/sysdev.h>
  35. #include <linux/slab.h>
  36. #include <linux/pm.h>
  37. #include <linux/pci.h>
  38. #include <linux/sfi.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/cpu.h>
  41. #include <linux/irq.h>
  42. #include <asm/fixmap.h>
  43. #include <asm/apb_timer.h>
  44. #define APBT_MASK CLOCKSOURCE_MASK(32)
  45. #define APBT_SHIFT 22
  46. #define APBT_CLOCKEVENT_RATING 150
  47. #define APBT_CLOCKSOURCE_RATING 250
  48. #define APBT_MIN_DELTA_USEC 200
  49. #define EVT_TO_APBT_DEV(evt) container_of(evt, struct apbt_dev, evt)
  50. #define APBT_CLOCKEVENT0_NUM (0)
  51. #define APBT_CLOCKEVENT1_NUM (1)
  52. #define APBT_CLOCKSOURCE_NUM (2)
  53. static unsigned long apbt_address;
  54. static int apb_timer_block_enabled;
  55. static void __iomem *apbt_virt_address;
  56. static int phy_cs_timer_id;
  57. /*
  58. * Common DW APB timer info
  59. */
  60. static uint64_t apbt_freq;
  61. static void apbt_set_mode(enum clock_event_mode mode,
  62. struct clock_event_device *evt);
  63. static int apbt_next_event(unsigned long delta,
  64. struct clock_event_device *evt);
  65. static cycle_t apbt_read_clocksource(struct clocksource *cs);
  66. static void apbt_restart_clocksource(struct clocksource *cs);
  67. struct apbt_dev {
  68. struct clock_event_device evt;
  69. unsigned int num;
  70. int cpu;
  71. unsigned int irq;
  72. unsigned int tick;
  73. unsigned int count;
  74. unsigned int flags;
  75. char name[10];
  76. };
  77. int disable_apbt_percpu __cpuinitdata;
  78. static DEFINE_PER_CPU(struct apbt_dev, cpu_apbt_dev);
  79. #ifdef CONFIG_SMP
  80. static unsigned int apbt_num_timers_used;
  81. static struct apbt_dev *apbt_devs;
  82. #endif
  83. static inline unsigned long apbt_readl_reg(unsigned long a)
  84. {
  85. return readl(apbt_virt_address + a);
  86. }
  87. static inline void apbt_writel_reg(unsigned long d, unsigned long a)
  88. {
  89. writel(d, apbt_virt_address + a);
  90. }
  91. static inline unsigned long apbt_readl(int n, unsigned long a)
  92. {
  93. return readl(apbt_virt_address + a + n * APBTMRS_REG_SIZE);
  94. }
  95. static inline void apbt_writel(int n, unsigned long d, unsigned long a)
  96. {
  97. writel(d, apbt_virt_address + a + n * APBTMRS_REG_SIZE);
  98. }
  99. static inline void apbt_set_mapping(void)
  100. {
  101. struct sfi_timer_table_entry *mtmr;
  102. if (apbt_virt_address) {
  103. pr_debug("APBT base already mapped\n");
  104. return;
  105. }
  106. mtmr = sfi_get_mtmr(APBT_CLOCKEVENT0_NUM);
  107. if (mtmr == NULL) {
  108. printk(KERN_ERR "Failed to get MTMR %d from SFI\n",
  109. APBT_CLOCKEVENT0_NUM);
  110. return;
  111. }
  112. apbt_address = (unsigned long)mtmr->phys_addr;
  113. if (!apbt_address) {
  114. printk(KERN_WARNING "No timer base from SFI, use default\n");
  115. apbt_address = APBT_DEFAULT_BASE;
  116. }
  117. apbt_virt_address = ioremap_nocache(apbt_address, APBT_MMAP_SIZE);
  118. if (apbt_virt_address) {
  119. pr_debug("Mapped APBT physical addr %p at virtual addr %p\n",\
  120. (void *)apbt_address, (void *)apbt_virt_address);
  121. } else {
  122. pr_debug("Failed mapping APBT phy address at %p\n",\
  123. (void *)apbt_address);
  124. goto panic_noapbt;
  125. }
  126. apbt_freq = mtmr->freq_hz / USEC_PER_SEC;
  127. sfi_free_mtmr(mtmr);
  128. /* Now figure out the physical timer id for clocksource device */
  129. mtmr = sfi_get_mtmr(APBT_CLOCKSOURCE_NUM);
  130. if (mtmr == NULL)
  131. goto panic_noapbt;
  132. /* Now figure out the physical timer id */
  133. phy_cs_timer_id = (unsigned int)(mtmr->phys_addr & 0xff)
  134. / APBTMRS_REG_SIZE;
  135. pr_debug("Use timer %d for clocksource\n", phy_cs_timer_id);
  136. return;
  137. panic_noapbt:
  138. panic("Failed to setup APB system timer\n");
  139. }
  140. static inline void apbt_clear_mapping(void)
  141. {
  142. iounmap(apbt_virt_address);
  143. apbt_virt_address = NULL;
  144. }
  145. /*
  146. * APBT timer interrupt enable / disable
  147. */
  148. static inline int is_apbt_capable(void)
  149. {
  150. return apbt_virt_address ? 1 : 0;
  151. }
  152. static struct clocksource clocksource_apbt = {
  153. .name = "apbt",
  154. .rating = APBT_CLOCKSOURCE_RATING,
  155. .read = apbt_read_clocksource,
  156. .mask = APBT_MASK,
  157. .shift = APBT_SHIFT,
  158. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  159. .resume = apbt_restart_clocksource,
  160. };
  161. /* boot APB clock event device */
  162. static struct clock_event_device apbt_clockevent = {
  163. .name = "apbt0",
  164. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  165. .set_mode = apbt_set_mode,
  166. .set_next_event = apbt_next_event,
  167. .shift = APBT_SHIFT,
  168. .irq = 0,
  169. .rating = APBT_CLOCKEVENT_RATING,
  170. };
  171. /*
  172. * if user does not want to use per CPU apb timer, just give it a lower rating
  173. * than local apic timer and skip the late per cpu timer init.
  174. */
  175. static inline int __init setup_x86_mrst_timer(char *arg)
  176. {
  177. if (!arg)
  178. return -EINVAL;
  179. if (strcmp("apbt_only", arg) == 0)
  180. disable_apbt_percpu = 0;
  181. else if (strcmp("lapic_and_apbt", arg) == 0)
  182. disable_apbt_percpu = 1;
  183. else {
  184. pr_warning("X86 MRST timer option %s not recognised"
  185. " use x86_mrst_timer=apbt_only or lapic_and_apbt\n",
  186. arg);
  187. return -EINVAL;
  188. }
  189. return 0;
  190. }
  191. __setup("x86_mrst_timer=", setup_x86_mrst_timer);
  192. /*
  193. * start count down from 0xffff_ffff. this is done by toggling the enable bit
  194. * then load initial load count to ~0.
  195. */
  196. static void apbt_start_counter(int n)
  197. {
  198. unsigned long ctrl = apbt_readl(n, APBTMR_N_CONTROL);
  199. ctrl &= ~APBTMR_CONTROL_ENABLE;
  200. apbt_writel(n, ctrl, APBTMR_N_CONTROL);
  201. apbt_writel(n, ~0, APBTMR_N_LOAD_COUNT);
  202. /* enable, mask interrupt */
  203. ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
  204. ctrl |= (APBTMR_CONTROL_ENABLE | APBTMR_CONTROL_INT);
  205. apbt_writel(n, ctrl, APBTMR_N_CONTROL);
  206. /* read it once to get cached counter value initialized */
  207. apbt_read_clocksource(&clocksource_apbt);
  208. }
  209. static irqreturn_t apbt_interrupt_handler(int irq, void *data)
  210. {
  211. struct apbt_dev *dev = (struct apbt_dev *)data;
  212. struct clock_event_device *aevt = &dev->evt;
  213. if (!aevt->event_handler) {
  214. printk(KERN_INFO "Spurious APBT timer interrupt on %d\n",
  215. dev->num);
  216. return IRQ_NONE;
  217. }
  218. aevt->event_handler(aevt);
  219. return IRQ_HANDLED;
  220. }
  221. static void apbt_restart_clocksource(struct clocksource *cs)
  222. {
  223. apbt_start_counter(phy_cs_timer_id);
  224. }
  225. /* Setup IRQ routing via IOAPIC */
  226. #ifdef CONFIG_SMP
  227. static void apbt_setup_irq(struct apbt_dev *adev)
  228. {
  229. struct irq_chip *chip;
  230. struct irq_desc *desc;
  231. /* timer0 irq has been setup early */
  232. if (adev->irq == 0)
  233. return;
  234. desc = irq_to_desc(adev->irq);
  235. chip = get_irq_chip(adev->irq);
  236. disable_irq(adev->irq);
  237. desc->status |= IRQ_MOVE_PCNTXT;
  238. irq_set_affinity(adev->irq, cpumask_of(adev->cpu));
  239. /* APB timer irqs are set up as mp_irqs, timer is edge triggerred */
  240. set_irq_chip_and_handler_name(adev->irq, chip, handle_edge_irq, "edge");
  241. enable_irq(adev->irq);
  242. if (system_state == SYSTEM_BOOTING)
  243. if (request_irq(adev->irq, apbt_interrupt_handler,
  244. IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
  245. adev->name, adev)) {
  246. printk(KERN_ERR "Failed request IRQ for APBT%d\n",
  247. adev->num);
  248. }
  249. }
  250. #endif
  251. static void apbt_enable_int(int n)
  252. {
  253. unsigned long ctrl = apbt_readl(n, APBTMR_N_CONTROL);
  254. /* clear pending intr */
  255. apbt_readl(n, APBTMR_N_EOI);
  256. ctrl &= ~APBTMR_CONTROL_INT;
  257. apbt_writel(n, ctrl, APBTMR_N_CONTROL);
  258. }
  259. static void apbt_disable_int(int n)
  260. {
  261. unsigned long ctrl = apbt_readl(n, APBTMR_N_CONTROL);
  262. ctrl |= APBTMR_CONTROL_INT;
  263. apbt_writel(n, ctrl, APBTMR_N_CONTROL);
  264. }
  265. static int __init apbt_clockevent_register(void)
  266. {
  267. struct sfi_timer_table_entry *mtmr;
  268. struct apbt_dev *adev = &__get_cpu_var(cpu_apbt_dev);
  269. mtmr = sfi_get_mtmr(APBT_CLOCKEVENT0_NUM);
  270. if (mtmr == NULL) {
  271. printk(KERN_ERR "Failed to get MTMR %d from SFI\n",
  272. APBT_CLOCKEVENT0_NUM);
  273. return -ENODEV;
  274. }
  275. /*
  276. * We need to calculate the scaled math multiplication factor for
  277. * nanosecond to apbt tick conversion.
  278. * mult = (nsec/cycle)*2^APBT_SHIFT
  279. */
  280. apbt_clockevent.mult = div_sc((unsigned long) mtmr->freq_hz
  281. , NSEC_PER_SEC, APBT_SHIFT);
  282. /* Calculate the min / max delta */
  283. apbt_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
  284. &apbt_clockevent);
  285. apbt_clockevent.min_delta_ns = clockevent_delta2ns(
  286. APBT_MIN_DELTA_USEC*apbt_freq,
  287. &apbt_clockevent);
  288. /*
  289. * Start apbt with the boot cpu mask and make it
  290. * global if not used for per cpu timer.
  291. */
  292. apbt_clockevent.cpumask = cpumask_of(smp_processor_id());
  293. adev->num = smp_processor_id();
  294. memcpy(&adev->evt, &apbt_clockevent, sizeof(struct clock_event_device));
  295. if (disable_apbt_percpu) {
  296. apbt_clockevent.rating = APBT_CLOCKEVENT_RATING - 100;
  297. global_clock_event = &adev->evt;
  298. printk(KERN_DEBUG "%s clockevent registered as global\n",
  299. global_clock_event->name);
  300. }
  301. if (request_irq(apbt_clockevent.irq, apbt_interrupt_handler,
  302. IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
  303. apbt_clockevent.name, adev)) {
  304. printk(KERN_ERR "Failed request IRQ for APBT%d\n",
  305. apbt_clockevent.irq);
  306. }
  307. clockevents_register_device(&adev->evt);
  308. /* Start APBT 0 interrupts */
  309. apbt_enable_int(APBT_CLOCKEVENT0_NUM);
  310. sfi_free_mtmr(mtmr);
  311. return 0;
  312. }
  313. #ifdef CONFIG_SMP
  314. /* Should be called with per cpu */
  315. void apbt_setup_secondary_clock(void)
  316. {
  317. struct apbt_dev *adev;
  318. struct clock_event_device *aevt;
  319. int cpu;
  320. /* Don't register boot CPU clockevent */
  321. cpu = smp_processor_id();
  322. if (cpu == boot_cpu_id)
  323. return;
  324. /*
  325. * We need to calculate the scaled math multiplication factor for
  326. * nanosecond to apbt tick conversion.
  327. * mult = (nsec/cycle)*2^APBT_SHIFT
  328. */
  329. printk(KERN_INFO "Init per CPU clockevent %d\n", cpu);
  330. adev = &per_cpu(cpu_apbt_dev, cpu);
  331. aevt = &adev->evt;
  332. memcpy(aevt, &apbt_clockevent, sizeof(*aevt));
  333. aevt->cpumask = cpumask_of(cpu);
  334. aevt->name = adev->name;
  335. aevt->mode = CLOCK_EVT_MODE_UNUSED;
  336. printk(KERN_INFO "Registering CPU %d clockevent device %s, mask %08x\n",
  337. cpu, aevt->name, *(u32 *)aevt->cpumask);
  338. apbt_setup_irq(adev);
  339. clockevents_register_device(aevt);
  340. apbt_enable_int(cpu);
  341. return;
  342. }
  343. /*
  344. * this notify handler process CPU hotplug events. in case of S0i3, nonboot
  345. * cpus are disabled/enabled frequently, for performance reasons, we keep the
  346. * per cpu timer irq registered so that we do need to do free_irq/request_irq.
  347. *
  348. * TODO: it might be more reliable to directly disable percpu clockevent device
  349. * without the notifier chain. currently, cpu 0 may get interrupts from other
  350. * cpu timers during the offline process due to the ordering of notification.
  351. * the extra interrupt is harmless.
  352. */
  353. static int apbt_cpuhp_notify(struct notifier_block *n,
  354. unsigned long action, void *hcpu)
  355. {
  356. unsigned long cpu = (unsigned long)hcpu;
  357. struct apbt_dev *adev = &per_cpu(cpu_apbt_dev, cpu);
  358. switch (action & 0xf) {
  359. case CPU_DEAD:
  360. apbt_disable_int(cpu);
  361. if (system_state == SYSTEM_RUNNING)
  362. pr_debug("skipping APBT CPU %lu offline\n", cpu);
  363. else if (adev) {
  364. pr_debug("APBT clockevent for cpu %lu offline\n", cpu);
  365. free_irq(adev->irq, adev);
  366. }
  367. break;
  368. default:
  369. pr_debug(KERN_INFO "APBT notified %lu, no action\n", action);
  370. }
  371. return NOTIFY_OK;
  372. }
  373. static __init int apbt_late_init(void)
  374. {
  375. if (disable_apbt_percpu || !apb_timer_block_enabled)
  376. return 0;
  377. /* This notifier should be called after workqueue is ready */
  378. hotcpu_notifier(apbt_cpuhp_notify, -20);
  379. return 0;
  380. }
  381. fs_initcall(apbt_late_init);
  382. #else
  383. void apbt_setup_secondary_clock(void) {}
  384. #endif /* CONFIG_SMP */
  385. static void apbt_set_mode(enum clock_event_mode mode,
  386. struct clock_event_device *evt)
  387. {
  388. unsigned long ctrl;
  389. uint64_t delta;
  390. int timer_num;
  391. struct apbt_dev *adev = EVT_TO_APBT_DEV(evt);
  392. timer_num = adev->num;
  393. pr_debug("%s CPU %d timer %d mode=%d\n",
  394. __func__, first_cpu(*evt->cpumask), timer_num, mode);
  395. switch (mode) {
  396. case CLOCK_EVT_MODE_PERIODIC:
  397. delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * apbt_clockevent.mult;
  398. delta >>= apbt_clockevent.shift;
  399. ctrl = apbt_readl(timer_num, APBTMR_N_CONTROL);
  400. ctrl |= APBTMR_CONTROL_MODE_PERIODIC;
  401. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  402. /*
  403. * DW APB p. 46, have to disable timer before load counter,
  404. * may cause sync problem.
  405. */
  406. ctrl &= ~APBTMR_CONTROL_ENABLE;
  407. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  408. udelay(1);
  409. pr_debug("Setting clock period %d for HZ %d\n", (int)delta, HZ);
  410. apbt_writel(timer_num, delta, APBTMR_N_LOAD_COUNT);
  411. ctrl |= APBTMR_CONTROL_ENABLE;
  412. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  413. break;
  414. /* APB timer does not have one-shot mode, use free running mode */
  415. case CLOCK_EVT_MODE_ONESHOT:
  416. ctrl = apbt_readl(timer_num, APBTMR_N_CONTROL);
  417. /*
  418. * set free running mode, this mode will let timer reload max
  419. * timeout which will give time (3min on 25MHz clock) to rearm
  420. * the next event, therefore emulate the one-shot mode.
  421. */
  422. ctrl &= ~APBTMR_CONTROL_ENABLE;
  423. ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
  424. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  425. /* write again to set free running mode */
  426. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  427. /*
  428. * DW APB p. 46, load counter with all 1s before starting free
  429. * running mode.
  430. */
  431. apbt_writel(timer_num, ~0, APBTMR_N_LOAD_COUNT);
  432. ctrl &= ~APBTMR_CONTROL_INT;
  433. ctrl |= APBTMR_CONTROL_ENABLE;
  434. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  435. break;
  436. case CLOCK_EVT_MODE_UNUSED:
  437. case CLOCK_EVT_MODE_SHUTDOWN:
  438. apbt_disable_int(timer_num);
  439. ctrl = apbt_readl(timer_num, APBTMR_N_CONTROL);
  440. ctrl &= ~APBTMR_CONTROL_ENABLE;
  441. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  442. break;
  443. case CLOCK_EVT_MODE_RESUME:
  444. apbt_enable_int(timer_num);
  445. break;
  446. }
  447. }
  448. static int apbt_next_event(unsigned long delta,
  449. struct clock_event_device *evt)
  450. {
  451. unsigned long ctrl;
  452. int timer_num;
  453. struct apbt_dev *adev = EVT_TO_APBT_DEV(evt);
  454. timer_num = adev->num;
  455. /* Disable timer */
  456. ctrl = apbt_readl(timer_num, APBTMR_N_CONTROL);
  457. ctrl &= ~APBTMR_CONTROL_ENABLE;
  458. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  459. /* write new count */
  460. apbt_writel(timer_num, delta, APBTMR_N_LOAD_COUNT);
  461. ctrl |= APBTMR_CONTROL_ENABLE;
  462. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  463. return 0;
  464. }
  465. /*
  466. * APB timer clock is not in sync with pclk on Langwell, which translates to
  467. * unreliable read value caused by sampling error. the error does not add up
  468. * overtime and only happens when sampling a 0 as a 1 by mistake. so the time
  469. * would go backwards. the following code is trying to prevent time traveling
  470. * backwards. little bit paranoid.
  471. */
  472. static cycle_t apbt_read_clocksource(struct clocksource *cs)
  473. {
  474. unsigned long t0, t1, t2;
  475. static unsigned long last_read;
  476. bad_count:
  477. t1 = apbt_readl(phy_cs_timer_id,
  478. APBTMR_N_CURRENT_VALUE);
  479. t2 = apbt_readl(phy_cs_timer_id,
  480. APBTMR_N_CURRENT_VALUE);
  481. if (unlikely(t1 < t2)) {
  482. pr_debug("APBT: read current count error %lx:%lx:%lx\n",
  483. t1, t2, t2 - t1);
  484. goto bad_count;
  485. }
  486. /*
  487. * check against cached last read, makes sure time does not go back.
  488. * it could be a normal rollover but we will do tripple check anyway
  489. */
  490. if (unlikely(t2 > last_read)) {
  491. /* check if we have a normal rollover */
  492. unsigned long raw_intr_status =
  493. apbt_readl_reg(APBTMRS_RAW_INT_STATUS);
  494. /*
  495. * cs timer interrupt is masked but raw intr bit is set if
  496. * rollover occurs. then we read EOI reg to clear it.
  497. */
  498. if (raw_intr_status & (1 << phy_cs_timer_id)) {
  499. apbt_readl(phy_cs_timer_id, APBTMR_N_EOI);
  500. goto out;
  501. }
  502. pr_debug("APB CS going back %lx:%lx:%lx ",
  503. t2, last_read, t2 - last_read);
  504. bad_count_x3:
  505. pr_debug(KERN_INFO "tripple check enforced\n");
  506. t0 = apbt_readl(phy_cs_timer_id,
  507. APBTMR_N_CURRENT_VALUE);
  508. udelay(1);
  509. t1 = apbt_readl(phy_cs_timer_id,
  510. APBTMR_N_CURRENT_VALUE);
  511. udelay(1);
  512. t2 = apbt_readl(phy_cs_timer_id,
  513. APBTMR_N_CURRENT_VALUE);
  514. if ((t2 > t1) || (t1 > t0)) {
  515. printk(KERN_ERR "Error: APB CS tripple check failed\n");
  516. goto bad_count_x3;
  517. }
  518. }
  519. out:
  520. last_read = t2;
  521. return (cycle_t)~t2;
  522. }
  523. static int apbt_clocksource_register(void)
  524. {
  525. u64 start, now;
  526. cycle_t t1;
  527. /* Start the counter, use timer 2 as source, timer 0/1 for event */
  528. apbt_start_counter(phy_cs_timer_id);
  529. /* Verify whether apbt counter works */
  530. t1 = apbt_read_clocksource(&clocksource_apbt);
  531. rdtscll(start);
  532. /*
  533. * We don't know the TSC frequency yet, but waiting for
  534. * 200000 TSC cycles is safe:
  535. * 4 GHz == 50us
  536. * 1 GHz == 200us
  537. */
  538. do {
  539. rep_nop();
  540. rdtscll(now);
  541. } while ((now - start) < 200000UL);
  542. /* APBT is the only always on clocksource, it has to work! */
  543. if (t1 == apbt_read_clocksource(&clocksource_apbt))
  544. panic("APBT counter not counting. APBT disabled\n");
  545. /*
  546. * initialize and register APBT clocksource
  547. * convert that to ns/clock cycle
  548. * mult = (ns/c) * 2^APBT_SHIFT
  549. */
  550. clocksource_apbt.mult = div_sc(MSEC_PER_SEC,
  551. (unsigned long) apbt_freq, APBT_SHIFT);
  552. clocksource_register(&clocksource_apbt);
  553. return 0;
  554. }
  555. /*
  556. * Early setup the APBT timer, only use timer 0 for booting then switch to
  557. * per CPU timer if possible.
  558. * returns 1 if per cpu apbt is setup
  559. * returns 0 if no per cpu apbt is chosen
  560. * panic if set up failed, this is the only platform timer on Moorestown.
  561. */
  562. void __init apbt_time_init(void)
  563. {
  564. #ifdef CONFIG_SMP
  565. int i;
  566. struct sfi_timer_table_entry *p_mtmr;
  567. unsigned int percpu_timer;
  568. struct apbt_dev *adev;
  569. #endif
  570. if (apb_timer_block_enabled)
  571. return;
  572. apbt_set_mapping();
  573. if (apbt_virt_address) {
  574. pr_debug("Found APBT version 0x%lx\n",\
  575. apbt_readl_reg(APBTMRS_COMP_VERSION));
  576. } else
  577. goto out_noapbt;
  578. /*
  579. * Read the frequency and check for a sane value, for ESL model
  580. * we extend the possible clock range to allow time scaling.
  581. */
  582. if (apbt_freq < APBT_MIN_FREQ || apbt_freq > APBT_MAX_FREQ) {
  583. pr_debug("APBT has invalid freq 0x%llx\n", apbt_freq);
  584. goto out_noapbt;
  585. }
  586. if (apbt_clocksource_register()) {
  587. pr_debug("APBT has failed to register clocksource\n");
  588. goto out_noapbt;
  589. }
  590. if (!apbt_clockevent_register())
  591. apb_timer_block_enabled = 1;
  592. else {
  593. pr_debug("APBT has failed to register clockevent\n");
  594. goto out_noapbt;
  595. }
  596. #ifdef CONFIG_SMP
  597. /* kernel cmdline disable apb timer, so we will use lapic timers */
  598. if (disable_apbt_percpu) {
  599. printk(KERN_INFO "apbt: disabled per cpu timer\n");
  600. return;
  601. }
  602. pr_debug("%s: %d CPUs online\n", __func__, num_online_cpus());
  603. if (num_possible_cpus() <= sfi_mtimer_num) {
  604. percpu_timer = 1;
  605. apbt_num_timers_used = num_possible_cpus();
  606. } else {
  607. percpu_timer = 0;
  608. apbt_num_timers_used = 1;
  609. adev = &per_cpu(cpu_apbt_dev, 0);
  610. adev->flags &= ~APBT_DEV_USED;
  611. }
  612. pr_debug("%s: %d APB timers used\n", __func__, apbt_num_timers_used);
  613. /* here we set up per CPU timer data structure */
  614. apbt_devs = kzalloc(sizeof(struct apbt_dev) * apbt_num_timers_used,
  615. GFP_KERNEL);
  616. if (!apbt_devs) {
  617. printk(KERN_ERR "Failed to allocate APB timer devices\n");
  618. return;
  619. }
  620. for (i = 0; i < apbt_num_timers_used; i++) {
  621. adev = &per_cpu(cpu_apbt_dev, i);
  622. adev->num = i;
  623. adev->cpu = i;
  624. p_mtmr = sfi_get_mtmr(i);
  625. if (p_mtmr) {
  626. adev->tick = p_mtmr->freq_hz;
  627. adev->irq = p_mtmr->irq;
  628. } else
  629. printk(KERN_ERR "Failed to get timer for cpu %d\n", i);
  630. adev->count = 0;
  631. sprintf(adev->name, "apbt%d", i);
  632. }
  633. #endif
  634. return;
  635. out_noapbt:
  636. apbt_clear_mapping();
  637. apb_timer_block_enabled = 0;
  638. panic("failed to enable APB timer\n");
  639. }
  640. static inline void apbt_disable(int n)
  641. {
  642. if (is_apbt_capable()) {
  643. unsigned long ctrl = apbt_readl(n, APBTMR_N_CONTROL);
  644. ctrl &= ~APBTMR_CONTROL_ENABLE;
  645. apbt_writel(n, ctrl, APBTMR_N_CONTROL);
  646. }
  647. }
  648. /* called before apb_timer_enable, use early map */
  649. unsigned long apbt_quick_calibrate()
  650. {
  651. int i, scale;
  652. u64 old, new;
  653. cycle_t t1, t2;
  654. unsigned long khz = 0;
  655. u32 loop, shift;
  656. apbt_set_mapping();
  657. apbt_start_counter(phy_cs_timer_id);
  658. /* check if the timer can count down, otherwise return */
  659. old = apbt_read_clocksource(&clocksource_apbt);
  660. i = 10000;
  661. while (--i) {
  662. if (old != apbt_read_clocksource(&clocksource_apbt))
  663. break;
  664. }
  665. if (!i)
  666. goto failed;
  667. /* count 16 ms */
  668. loop = (apbt_freq * 1000) << 4;
  669. /* restart the timer to ensure it won't get to 0 in the calibration */
  670. apbt_start_counter(phy_cs_timer_id);
  671. old = apbt_read_clocksource(&clocksource_apbt);
  672. old += loop;
  673. t1 = __native_read_tsc();
  674. do {
  675. new = apbt_read_clocksource(&clocksource_apbt);
  676. } while (new < old);
  677. t2 = __native_read_tsc();
  678. shift = 5;
  679. if (unlikely(loop >> shift == 0)) {
  680. printk(KERN_INFO
  681. "APBT TSC calibration failed, not enough resolution\n");
  682. return 0;
  683. }
  684. scale = (int)div_u64((t2 - t1), loop >> shift);
  685. khz = (scale * apbt_freq * 1000) >> shift;
  686. printk(KERN_INFO "TSC freq calculated by APB timer is %lu khz\n", khz);
  687. return khz;
  688. failed:
  689. return 0;
  690. }