apb_timer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. #include <asm/mrst.h>
  45. #define APBT_MASK CLOCKSOURCE_MASK(32)
  46. #define APBT_SHIFT 22
  47. #define APBT_CLOCKEVENT_RATING 110
  48. #define APBT_CLOCKSOURCE_RATING 250
  49. #define APBT_MIN_DELTA_USEC 200
  50. #define EVT_TO_APBT_DEV(evt) container_of(evt, struct apbt_dev, evt)
  51. #define APBT_CLOCKEVENT0_NUM (0)
  52. #define APBT_CLOCKEVENT1_NUM (1)
  53. #define APBT_CLOCKSOURCE_NUM (2)
  54. static unsigned long apbt_address;
  55. static int apb_timer_block_enabled;
  56. static void __iomem *apbt_virt_address;
  57. static int phy_cs_timer_id;
  58. /*
  59. * Common DW APB timer info
  60. */
  61. static uint64_t apbt_freq;
  62. static void apbt_set_mode(enum clock_event_mode mode,
  63. struct clock_event_device *evt);
  64. static int apbt_next_event(unsigned long delta,
  65. struct clock_event_device *evt);
  66. static cycle_t apbt_read_clocksource(struct clocksource *cs);
  67. static void apbt_restart_clocksource(struct clocksource *cs);
  68. struct apbt_dev {
  69. struct clock_event_device evt;
  70. unsigned int num;
  71. int cpu;
  72. unsigned int irq;
  73. unsigned int tick;
  74. unsigned int count;
  75. unsigned int flags;
  76. char name[10];
  77. };
  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. .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. * start count down from 0xffff_ffff. this is done by toggling the enable bit
  172. * then load initial load count to ~0.
  173. */
  174. static void apbt_start_counter(int n)
  175. {
  176. unsigned long ctrl = apbt_readl(n, APBTMR_N_CONTROL);
  177. ctrl &= ~APBTMR_CONTROL_ENABLE;
  178. apbt_writel(n, ctrl, APBTMR_N_CONTROL);
  179. apbt_writel(n, ~0, APBTMR_N_LOAD_COUNT);
  180. /* enable, mask interrupt */
  181. ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
  182. ctrl |= (APBTMR_CONTROL_ENABLE | APBTMR_CONTROL_INT);
  183. apbt_writel(n, ctrl, APBTMR_N_CONTROL);
  184. /* read it once to get cached counter value initialized */
  185. apbt_read_clocksource(&clocksource_apbt);
  186. }
  187. static irqreturn_t apbt_interrupt_handler(int irq, void *data)
  188. {
  189. struct apbt_dev *dev = (struct apbt_dev *)data;
  190. struct clock_event_device *aevt = &dev->evt;
  191. if (!aevt->event_handler) {
  192. printk(KERN_INFO "Spurious APBT timer interrupt on %d\n",
  193. dev->num);
  194. return IRQ_NONE;
  195. }
  196. aevt->event_handler(aevt);
  197. return IRQ_HANDLED;
  198. }
  199. static void apbt_restart_clocksource(struct clocksource *cs)
  200. {
  201. apbt_start_counter(phy_cs_timer_id);
  202. }
  203. static void apbt_enable_int(int n)
  204. {
  205. unsigned long ctrl = apbt_readl(n, APBTMR_N_CONTROL);
  206. /* clear pending intr */
  207. apbt_readl(n, APBTMR_N_EOI);
  208. ctrl &= ~APBTMR_CONTROL_INT;
  209. apbt_writel(n, ctrl, APBTMR_N_CONTROL);
  210. }
  211. static void apbt_disable_int(int n)
  212. {
  213. unsigned long ctrl = apbt_readl(n, APBTMR_N_CONTROL);
  214. ctrl |= APBTMR_CONTROL_INT;
  215. apbt_writel(n, ctrl, APBTMR_N_CONTROL);
  216. }
  217. static int __init apbt_clockevent_register(void)
  218. {
  219. struct sfi_timer_table_entry *mtmr;
  220. struct apbt_dev *adev = &__get_cpu_var(cpu_apbt_dev);
  221. mtmr = sfi_get_mtmr(APBT_CLOCKEVENT0_NUM);
  222. if (mtmr == NULL) {
  223. printk(KERN_ERR "Failed to get MTMR %d from SFI\n",
  224. APBT_CLOCKEVENT0_NUM);
  225. return -ENODEV;
  226. }
  227. /*
  228. * We need to calculate the scaled math multiplication factor for
  229. * nanosecond to apbt tick conversion.
  230. * mult = (nsec/cycle)*2^APBT_SHIFT
  231. */
  232. apbt_clockevent.mult = div_sc((unsigned long) mtmr->freq_hz
  233. , NSEC_PER_SEC, APBT_SHIFT);
  234. /* Calculate the min / max delta */
  235. apbt_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
  236. &apbt_clockevent);
  237. apbt_clockevent.min_delta_ns = clockevent_delta2ns(
  238. APBT_MIN_DELTA_USEC*apbt_freq,
  239. &apbt_clockevent);
  240. /*
  241. * Start apbt with the boot cpu mask and make it
  242. * global if not used for per cpu timer.
  243. */
  244. apbt_clockevent.cpumask = cpumask_of(smp_processor_id());
  245. adev->num = smp_processor_id();
  246. memcpy(&adev->evt, &apbt_clockevent, sizeof(struct clock_event_device));
  247. if (mrst_timer_options == MRST_TIMER_LAPIC_APBT) {
  248. adev->evt.rating = APBT_CLOCKEVENT_RATING - 100;
  249. global_clock_event = &adev->evt;
  250. printk(KERN_DEBUG "%s clockevent registered as global\n",
  251. global_clock_event->name);
  252. }
  253. if (request_irq(apbt_clockevent.irq, apbt_interrupt_handler,
  254. IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
  255. apbt_clockevent.name, adev)) {
  256. printk(KERN_ERR "Failed request IRQ for APBT%d\n",
  257. apbt_clockevent.irq);
  258. }
  259. clockevents_register_device(&adev->evt);
  260. /* Start APBT 0 interrupts */
  261. apbt_enable_int(APBT_CLOCKEVENT0_NUM);
  262. sfi_free_mtmr(mtmr);
  263. return 0;
  264. }
  265. #ifdef CONFIG_SMP
  266. static void apbt_setup_irq(struct apbt_dev *adev)
  267. {
  268. /* timer0 irq has been setup early */
  269. if (adev->irq == 0)
  270. return;
  271. irq_modify_status(adev->irq, 0, IRQ_MOVE_PCNTXT);
  272. irq_set_affinity(adev->irq, cpumask_of(adev->cpu));
  273. /* APB timer irqs are set up as mp_irqs, timer is edge type */
  274. __irq_set_handler(adev->irq, handle_edge_irq, 0, "edge");
  275. if (system_state == SYSTEM_BOOTING) {
  276. if (request_irq(adev->irq, apbt_interrupt_handler,
  277. IRQF_TIMER | IRQF_DISABLED |
  278. IRQF_NOBALANCING,
  279. adev->name, adev)) {
  280. printk(KERN_ERR "Failed request IRQ for APBT%d\n",
  281. adev->num);
  282. }
  283. } else
  284. enable_irq(adev->irq);
  285. }
  286. /* Should be called with per cpu */
  287. void apbt_setup_secondary_clock(void)
  288. {
  289. struct apbt_dev *adev;
  290. struct clock_event_device *aevt;
  291. int cpu;
  292. /* Don't register boot CPU clockevent */
  293. cpu = smp_processor_id();
  294. if (!cpu)
  295. return;
  296. /*
  297. * We need to calculate the scaled math multiplication factor for
  298. * nanosecond to apbt tick conversion.
  299. * mult = (nsec/cycle)*2^APBT_SHIFT
  300. */
  301. printk(KERN_INFO "Init per CPU clockevent %d\n", cpu);
  302. adev = &per_cpu(cpu_apbt_dev, cpu);
  303. aevt = &adev->evt;
  304. memcpy(aevt, &apbt_clockevent, sizeof(*aevt));
  305. aevt->cpumask = cpumask_of(cpu);
  306. aevt->name = adev->name;
  307. aevt->mode = CLOCK_EVT_MODE_UNUSED;
  308. printk(KERN_INFO "Registering CPU %d clockevent device %s, mask %08x\n",
  309. cpu, aevt->name, *(u32 *)aevt->cpumask);
  310. apbt_setup_irq(adev);
  311. clockevents_register_device(aevt);
  312. apbt_enable_int(cpu);
  313. return;
  314. }
  315. /*
  316. * this notify handler process CPU hotplug events. in case of S0i3, nonboot
  317. * cpus are disabled/enabled frequently, for performance reasons, we keep the
  318. * per cpu timer irq registered so that we do need to do free_irq/request_irq.
  319. *
  320. * TODO: it might be more reliable to directly disable percpu clockevent device
  321. * without the notifier chain. currently, cpu 0 may get interrupts from other
  322. * cpu timers during the offline process due to the ordering of notification.
  323. * the extra interrupt is harmless.
  324. */
  325. static int apbt_cpuhp_notify(struct notifier_block *n,
  326. unsigned long action, void *hcpu)
  327. {
  328. unsigned long cpu = (unsigned long)hcpu;
  329. struct apbt_dev *adev = &per_cpu(cpu_apbt_dev, cpu);
  330. switch (action & 0xf) {
  331. case CPU_DEAD:
  332. disable_irq(adev->irq);
  333. apbt_disable_int(cpu);
  334. if (system_state == SYSTEM_RUNNING) {
  335. pr_debug("skipping APBT CPU %lu offline\n", cpu);
  336. } else if (adev) {
  337. pr_debug("APBT clockevent for cpu %lu offline\n", cpu);
  338. free_irq(adev->irq, adev);
  339. }
  340. break;
  341. default:
  342. pr_debug("APBT notified %lu, no action\n", action);
  343. }
  344. return NOTIFY_OK;
  345. }
  346. static __init int apbt_late_init(void)
  347. {
  348. if (mrst_timer_options == MRST_TIMER_LAPIC_APBT ||
  349. !apb_timer_block_enabled)
  350. return 0;
  351. /* This notifier should be called after workqueue is ready */
  352. hotcpu_notifier(apbt_cpuhp_notify, -20);
  353. return 0;
  354. }
  355. fs_initcall(apbt_late_init);
  356. #else
  357. void apbt_setup_secondary_clock(void) {}
  358. #endif /* CONFIG_SMP */
  359. static void apbt_set_mode(enum clock_event_mode mode,
  360. struct clock_event_device *evt)
  361. {
  362. unsigned long ctrl;
  363. uint64_t delta;
  364. int timer_num;
  365. struct apbt_dev *adev = EVT_TO_APBT_DEV(evt);
  366. BUG_ON(!apbt_virt_address);
  367. timer_num = adev->num;
  368. pr_debug("%s CPU %d timer %d mode=%d\n",
  369. __func__, first_cpu(*evt->cpumask), timer_num, mode);
  370. switch (mode) {
  371. case CLOCK_EVT_MODE_PERIODIC:
  372. delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * apbt_clockevent.mult;
  373. delta >>= apbt_clockevent.shift;
  374. ctrl = apbt_readl(timer_num, APBTMR_N_CONTROL);
  375. ctrl |= APBTMR_CONTROL_MODE_PERIODIC;
  376. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  377. /*
  378. * DW APB p. 46, have to disable timer before load counter,
  379. * may cause sync problem.
  380. */
  381. ctrl &= ~APBTMR_CONTROL_ENABLE;
  382. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  383. udelay(1);
  384. pr_debug("Setting clock period %d for HZ %d\n", (int)delta, HZ);
  385. apbt_writel(timer_num, delta, APBTMR_N_LOAD_COUNT);
  386. ctrl |= APBTMR_CONTROL_ENABLE;
  387. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  388. break;
  389. /* APB timer does not have one-shot mode, use free running mode */
  390. case CLOCK_EVT_MODE_ONESHOT:
  391. ctrl = apbt_readl(timer_num, APBTMR_N_CONTROL);
  392. /*
  393. * set free running mode, this mode will let timer reload max
  394. * timeout which will give time (3min on 25MHz clock) to rearm
  395. * the next event, therefore emulate the one-shot mode.
  396. */
  397. ctrl &= ~APBTMR_CONTROL_ENABLE;
  398. ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
  399. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  400. /* write again to set free running mode */
  401. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  402. /*
  403. * DW APB p. 46, load counter with all 1s before starting free
  404. * running mode.
  405. */
  406. apbt_writel(timer_num, ~0, APBTMR_N_LOAD_COUNT);
  407. ctrl &= ~APBTMR_CONTROL_INT;
  408. ctrl |= APBTMR_CONTROL_ENABLE;
  409. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  410. break;
  411. case CLOCK_EVT_MODE_UNUSED:
  412. case CLOCK_EVT_MODE_SHUTDOWN:
  413. apbt_disable_int(timer_num);
  414. ctrl = apbt_readl(timer_num, APBTMR_N_CONTROL);
  415. ctrl &= ~APBTMR_CONTROL_ENABLE;
  416. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  417. break;
  418. case CLOCK_EVT_MODE_RESUME:
  419. apbt_enable_int(timer_num);
  420. break;
  421. }
  422. }
  423. static int apbt_next_event(unsigned long delta,
  424. struct clock_event_device *evt)
  425. {
  426. unsigned long ctrl;
  427. int timer_num;
  428. struct apbt_dev *adev = EVT_TO_APBT_DEV(evt);
  429. timer_num = adev->num;
  430. /* Disable timer */
  431. ctrl = apbt_readl(timer_num, APBTMR_N_CONTROL);
  432. ctrl &= ~APBTMR_CONTROL_ENABLE;
  433. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  434. /* write new count */
  435. apbt_writel(timer_num, delta, APBTMR_N_LOAD_COUNT);
  436. ctrl |= APBTMR_CONTROL_ENABLE;
  437. apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
  438. return 0;
  439. }
  440. static cycle_t apbt_read_clocksource(struct clocksource *cs)
  441. {
  442. unsigned long current_count;
  443. current_count = apbt_readl(phy_cs_timer_id, APBTMR_N_CURRENT_VALUE);
  444. return (cycle_t)~current_count;
  445. }
  446. static int apbt_clocksource_register(void)
  447. {
  448. u64 start, now;
  449. cycle_t t1;
  450. /* Start the counter, use timer 2 as source, timer 0/1 for event */
  451. apbt_start_counter(phy_cs_timer_id);
  452. /* Verify whether apbt counter works */
  453. t1 = apbt_read_clocksource(&clocksource_apbt);
  454. rdtscll(start);
  455. /*
  456. * We don't know the TSC frequency yet, but waiting for
  457. * 200000 TSC cycles is safe:
  458. * 4 GHz == 50us
  459. * 1 GHz == 200us
  460. */
  461. do {
  462. rep_nop();
  463. rdtscll(now);
  464. } while ((now - start) < 200000UL);
  465. /* APBT is the only always on clocksource, it has to work! */
  466. if (t1 == apbt_read_clocksource(&clocksource_apbt))
  467. panic("APBT counter not counting. APBT disabled\n");
  468. clocksource_register_khz(&clocksource_apbt, (u32)apbt_freq*1000);
  469. return 0;
  470. }
  471. /*
  472. * Early setup the APBT timer, only use timer 0 for booting then switch to
  473. * per CPU timer if possible.
  474. * returns 1 if per cpu apbt is setup
  475. * returns 0 if no per cpu apbt is chosen
  476. * panic if set up failed, this is the only platform timer on Moorestown.
  477. */
  478. void __init apbt_time_init(void)
  479. {
  480. #ifdef CONFIG_SMP
  481. int i;
  482. struct sfi_timer_table_entry *p_mtmr;
  483. unsigned int percpu_timer;
  484. struct apbt_dev *adev;
  485. #endif
  486. if (apb_timer_block_enabled)
  487. return;
  488. apbt_set_mapping();
  489. if (apbt_virt_address) {
  490. pr_debug("Found APBT version 0x%lx\n",\
  491. apbt_readl_reg(APBTMRS_COMP_VERSION));
  492. } else
  493. goto out_noapbt;
  494. /*
  495. * Read the frequency and check for a sane value, for ESL model
  496. * we extend the possible clock range to allow time scaling.
  497. */
  498. if (apbt_freq < APBT_MIN_FREQ || apbt_freq > APBT_MAX_FREQ) {
  499. pr_debug("APBT has invalid freq 0x%llx\n", apbt_freq);
  500. goto out_noapbt;
  501. }
  502. if (apbt_clocksource_register()) {
  503. pr_debug("APBT has failed to register clocksource\n");
  504. goto out_noapbt;
  505. }
  506. if (!apbt_clockevent_register())
  507. apb_timer_block_enabled = 1;
  508. else {
  509. pr_debug("APBT has failed to register clockevent\n");
  510. goto out_noapbt;
  511. }
  512. #ifdef CONFIG_SMP
  513. /* kernel cmdline disable apb timer, so we will use lapic timers */
  514. if (mrst_timer_options == MRST_TIMER_LAPIC_APBT) {
  515. printk(KERN_INFO "apbt: disabled per cpu timer\n");
  516. return;
  517. }
  518. pr_debug("%s: %d CPUs online\n", __func__, num_online_cpus());
  519. if (num_possible_cpus() <= sfi_mtimer_num) {
  520. percpu_timer = 1;
  521. apbt_num_timers_used = num_possible_cpus();
  522. } else {
  523. percpu_timer = 0;
  524. apbt_num_timers_used = 1;
  525. adev = &per_cpu(cpu_apbt_dev, 0);
  526. adev->flags &= ~APBT_DEV_USED;
  527. }
  528. pr_debug("%s: %d APB timers used\n", __func__, apbt_num_timers_used);
  529. /* here we set up per CPU timer data structure */
  530. apbt_devs = kzalloc(sizeof(struct apbt_dev) * apbt_num_timers_used,
  531. GFP_KERNEL);
  532. if (!apbt_devs) {
  533. printk(KERN_ERR "Failed to allocate APB timer devices\n");
  534. return;
  535. }
  536. for (i = 0; i < apbt_num_timers_used; i++) {
  537. adev = &per_cpu(cpu_apbt_dev, i);
  538. adev->num = i;
  539. adev->cpu = i;
  540. p_mtmr = sfi_get_mtmr(i);
  541. if (p_mtmr) {
  542. adev->tick = p_mtmr->freq_hz;
  543. adev->irq = p_mtmr->irq;
  544. } else
  545. printk(KERN_ERR "Failed to get timer for cpu %d\n", i);
  546. adev->count = 0;
  547. sprintf(adev->name, "apbt%d", i);
  548. }
  549. #endif
  550. return;
  551. out_noapbt:
  552. apbt_clear_mapping();
  553. apb_timer_block_enabled = 0;
  554. panic("failed to enable APB timer\n");
  555. }
  556. static inline void apbt_disable(int n)
  557. {
  558. if (is_apbt_capable()) {
  559. unsigned long ctrl = apbt_readl(n, APBTMR_N_CONTROL);
  560. ctrl &= ~APBTMR_CONTROL_ENABLE;
  561. apbt_writel(n, ctrl, APBTMR_N_CONTROL);
  562. }
  563. }
  564. /* called before apb_timer_enable, use early map */
  565. unsigned long apbt_quick_calibrate()
  566. {
  567. int i, scale;
  568. u64 old, new;
  569. cycle_t t1, t2;
  570. unsigned long khz = 0;
  571. u32 loop, shift;
  572. apbt_set_mapping();
  573. apbt_start_counter(phy_cs_timer_id);
  574. /* check if the timer can count down, otherwise return */
  575. old = apbt_read_clocksource(&clocksource_apbt);
  576. i = 10000;
  577. while (--i) {
  578. if (old != apbt_read_clocksource(&clocksource_apbt))
  579. break;
  580. }
  581. if (!i)
  582. goto failed;
  583. /* count 16 ms */
  584. loop = (apbt_freq * 1000) << 4;
  585. /* restart the timer to ensure it won't get to 0 in the calibration */
  586. apbt_start_counter(phy_cs_timer_id);
  587. old = apbt_read_clocksource(&clocksource_apbt);
  588. old += loop;
  589. t1 = __native_read_tsc();
  590. do {
  591. new = apbt_read_clocksource(&clocksource_apbt);
  592. } while (new < old);
  593. t2 = __native_read_tsc();
  594. shift = 5;
  595. if (unlikely(loop >> shift == 0)) {
  596. printk(KERN_INFO
  597. "APBT TSC calibration failed, not enough resolution\n");
  598. return 0;
  599. }
  600. scale = (int)div_u64((t2 - t1), loop >> shift);
  601. khz = (scale * apbt_freq * 1000) >> shift;
  602. printk(KERN_INFO "TSC freq calculated by APB timer is %lu khz\n", khz);
  603. return khz;
  604. failed:
  605. return 0;
  606. }