apb_timer.c 21 KB

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