hpet.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. #include <linux/clocksource.h>
  2. #include <linux/clockchips.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/sysdev.h>
  5. #include <linux/delay.h>
  6. #include <linux/errno.h>
  7. #include <linux/hpet.h>
  8. #include <linux/init.h>
  9. #include <linux/cpu.h>
  10. #include <linux/pm.h>
  11. #include <linux/io.h>
  12. #include <asm/fixmap.h>
  13. #include <asm/i8253.h>
  14. #include <asm/hpet.h>
  15. #define HPET_MASK CLOCKSOURCE_MASK(32)
  16. #define HPET_SHIFT 22
  17. /* FSEC = 10^-15
  18. NSEC = 10^-9 */
  19. #define FSEC_PER_NSEC 1000000L
  20. #define HPET_DEV_USED_BIT 2
  21. #define HPET_DEV_USED (1 << HPET_DEV_USED_BIT)
  22. #define HPET_DEV_VALID 0x8
  23. #define HPET_DEV_FSB_CAP 0x1000
  24. #define HPET_DEV_PERI_CAP 0x2000
  25. #define EVT_TO_HPET_DEV(evt) container_of(evt, struct hpet_dev, evt)
  26. /*
  27. * HPET address is set in acpi/boot.c, when an ACPI entry exists
  28. */
  29. unsigned long hpet_address;
  30. #ifdef CONFIG_PCI_MSI
  31. static unsigned long hpet_num_timers;
  32. #endif
  33. static void __iomem *hpet_virt_address;
  34. struct hpet_dev {
  35. struct clock_event_device evt;
  36. unsigned int num;
  37. int cpu;
  38. unsigned int irq;
  39. unsigned int flags;
  40. char name[10];
  41. };
  42. unsigned long hpet_readl(unsigned long a)
  43. {
  44. return readl(hpet_virt_address + a);
  45. }
  46. static inline void hpet_writel(unsigned long d, unsigned long a)
  47. {
  48. writel(d, hpet_virt_address + a);
  49. }
  50. #ifdef CONFIG_X86_64
  51. #include <asm/pgtable.h>
  52. #endif
  53. static inline void hpet_set_mapping(void)
  54. {
  55. hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
  56. #ifdef CONFIG_X86_64
  57. __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
  58. #endif
  59. }
  60. static inline void hpet_clear_mapping(void)
  61. {
  62. iounmap(hpet_virt_address);
  63. hpet_virt_address = NULL;
  64. }
  65. /*
  66. * HPET command line enable / disable
  67. */
  68. static int boot_hpet_disable;
  69. int hpet_force_user;
  70. static int __init hpet_setup(char *str)
  71. {
  72. if (str) {
  73. if (!strncmp("disable", str, 7))
  74. boot_hpet_disable = 1;
  75. if (!strncmp("force", str, 5))
  76. hpet_force_user = 1;
  77. }
  78. return 1;
  79. }
  80. __setup("hpet=", hpet_setup);
  81. static int __init disable_hpet(char *str)
  82. {
  83. boot_hpet_disable = 1;
  84. return 1;
  85. }
  86. __setup("nohpet", disable_hpet);
  87. static inline int is_hpet_capable(void)
  88. {
  89. return !boot_hpet_disable && hpet_address;
  90. }
  91. /*
  92. * HPET timer interrupt enable / disable
  93. */
  94. static int hpet_legacy_int_enabled;
  95. /**
  96. * is_hpet_enabled - check whether the hpet timer interrupt is enabled
  97. */
  98. int is_hpet_enabled(void)
  99. {
  100. return is_hpet_capable() && hpet_legacy_int_enabled;
  101. }
  102. EXPORT_SYMBOL_GPL(is_hpet_enabled);
  103. /*
  104. * When the hpet driver (/dev/hpet) is enabled, we need to reserve
  105. * timer 0 and timer 1 in case of RTC emulation.
  106. */
  107. #ifdef CONFIG_HPET
  108. static void hpet_reserve_msi_timers(struct hpet_data *hd);
  109. static void hpet_reserve_platform_timers(unsigned long id)
  110. {
  111. struct hpet __iomem *hpet = hpet_virt_address;
  112. struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
  113. unsigned int nrtimers, i;
  114. struct hpet_data hd;
  115. nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
  116. memset(&hd, 0, sizeof(hd));
  117. hd.hd_phys_address = hpet_address;
  118. hd.hd_address = hpet;
  119. hd.hd_nirqs = nrtimers;
  120. hpet_reserve_timer(&hd, 0);
  121. #ifdef CONFIG_HPET_EMULATE_RTC
  122. hpet_reserve_timer(&hd, 1);
  123. #endif
  124. /*
  125. * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
  126. * is wrong for i8259!) not the output IRQ. Many BIOS writers
  127. * don't bother configuring *any* comparator interrupts.
  128. */
  129. hd.hd_irq[0] = HPET_LEGACY_8254;
  130. hd.hd_irq[1] = HPET_LEGACY_RTC;
  131. for (i = 2; i < nrtimers; timer++, i++) {
  132. hd.hd_irq[i] = (readl(&timer->hpet_config) &
  133. Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
  134. }
  135. hpet_reserve_msi_timers(&hd);
  136. hpet_alloc(&hd);
  137. }
  138. #else
  139. static void hpet_reserve_platform_timers(unsigned long id) { }
  140. #endif
  141. /*
  142. * Common hpet info
  143. */
  144. static unsigned long hpet_period;
  145. static void hpet_legacy_set_mode(enum clock_event_mode mode,
  146. struct clock_event_device *evt);
  147. static int hpet_legacy_next_event(unsigned long delta,
  148. struct clock_event_device *evt);
  149. /*
  150. * The hpet clock event device
  151. */
  152. static struct clock_event_device hpet_clockevent = {
  153. .name = "hpet",
  154. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  155. .set_mode = hpet_legacy_set_mode,
  156. .set_next_event = hpet_legacy_next_event,
  157. .shift = 32,
  158. .irq = 0,
  159. .rating = 50,
  160. };
  161. static void hpet_start_counter(void)
  162. {
  163. unsigned long cfg = hpet_readl(HPET_CFG);
  164. cfg &= ~HPET_CFG_ENABLE;
  165. hpet_writel(cfg, HPET_CFG);
  166. hpet_writel(0, HPET_COUNTER);
  167. hpet_writel(0, HPET_COUNTER + 4);
  168. cfg |= HPET_CFG_ENABLE;
  169. hpet_writel(cfg, HPET_CFG);
  170. }
  171. static void hpet_resume_device(void)
  172. {
  173. force_hpet_resume();
  174. }
  175. static void hpet_restart_counter(void)
  176. {
  177. hpet_resume_device();
  178. hpet_start_counter();
  179. }
  180. static void hpet_enable_legacy_int(void)
  181. {
  182. unsigned long cfg = hpet_readl(HPET_CFG);
  183. cfg |= HPET_CFG_LEGACY;
  184. hpet_writel(cfg, HPET_CFG);
  185. hpet_legacy_int_enabled = 1;
  186. }
  187. static void hpet_legacy_clockevent_register(void)
  188. {
  189. /* Start HPET legacy interrupts */
  190. hpet_enable_legacy_int();
  191. /*
  192. * The mult factor is defined as (include/linux/clockchips.h)
  193. * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h)
  194. * hpet_period is in units of femtoseconds (per cycle), so
  195. * mult/2^shift = cyc/ns = 10^6/hpet_period
  196. * mult = (10^6 * 2^shift)/hpet_period
  197. * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period
  198. */
  199. hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
  200. hpet_period, hpet_clockevent.shift);
  201. /* Calculate the min / max delta */
  202. hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
  203. &hpet_clockevent);
  204. /* 5 usec minimum reprogramming delta. */
  205. hpet_clockevent.min_delta_ns = 5000;
  206. /*
  207. * Start hpet with the boot cpu mask and make it
  208. * global after the IO_APIC has been initialized.
  209. */
  210. hpet_clockevent.cpumask = cpumask_of(smp_processor_id());
  211. clockevents_register_device(&hpet_clockevent);
  212. global_clock_event = &hpet_clockevent;
  213. printk(KERN_DEBUG "hpet clockevent registered\n");
  214. }
  215. static int hpet_setup_msi_irq(unsigned int irq);
  216. static void hpet_set_mode(enum clock_event_mode mode,
  217. struct clock_event_device *evt, int timer)
  218. {
  219. unsigned long cfg, cmp, now;
  220. uint64_t delta;
  221. switch (mode) {
  222. case CLOCK_EVT_MODE_PERIODIC:
  223. delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
  224. delta >>= evt->shift;
  225. now = hpet_readl(HPET_COUNTER);
  226. cmp = now + (unsigned long) delta;
  227. cfg = hpet_readl(HPET_Tn_CFG(timer));
  228. cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
  229. HPET_TN_SETVAL | HPET_TN_32BIT;
  230. hpet_writel(cfg, HPET_Tn_CFG(timer));
  231. /*
  232. * The first write after writing TN_SETVAL to the
  233. * config register sets the counter value, the second
  234. * write sets the period.
  235. */
  236. hpet_writel(cmp, HPET_Tn_CMP(timer));
  237. udelay(1);
  238. hpet_writel((unsigned long) delta, HPET_Tn_CMP(timer));
  239. break;
  240. case CLOCK_EVT_MODE_ONESHOT:
  241. cfg = hpet_readl(HPET_Tn_CFG(timer));
  242. cfg &= ~HPET_TN_PERIODIC;
  243. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  244. hpet_writel(cfg, HPET_Tn_CFG(timer));
  245. break;
  246. case CLOCK_EVT_MODE_UNUSED:
  247. case CLOCK_EVT_MODE_SHUTDOWN:
  248. cfg = hpet_readl(HPET_Tn_CFG(timer));
  249. cfg &= ~HPET_TN_ENABLE;
  250. hpet_writel(cfg, HPET_Tn_CFG(timer));
  251. break;
  252. case CLOCK_EVT_MODE_RESUME:
  253. if (timer == 0) {
  254. hpet_enable_legacy_int();
  255. } else {
  256. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  257. hpet_setup_msi_irq(hdev->irq);
  258. disable_irq(hdev->irq);
  259. irq_set_affinity(hdev->irq, cpumask_of(hdev->cpu));
  260. enable_irq(hdev->irq);
  261. }
  262. break;
  263. }
  264. }
  265. static int hpet_next_event(unsigned long delta,
  266. struct clock_event_device *evt, int timer)
  267. {
  268. u32 cnt;
  269. cnt = hpet_readl(HPET_COUNTER);
  270. cnt += (u32) delta;
  271. hpet_writel(cnt, HPET_Tn_CMP(timer));
  272. /*
  273. * We need to read back the CMP register to make sure that
  274. * what we wrote hit the chip before we compare it to the
  275. * counter.
  276. */
  277. WARN_ON_ONCE((u32)hpet_readl(HPET_Tn_CMP(timer)) != cnt);
  278. return (s32)((u32)hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0;
  279. }
  280. static void hpet_legacy_set_mode(enum clock_event_mode mode,
  281. struct clock_event_device *evt)
  282. {
  283. hpet_set_mode(mode, evt, 0);
  284. }
  285. static int hpet_legacy_next_event(unsigned long delta,
  286. struct clock_event_device *evt)
  287. {
  288. return hpet_next_event(delta, evt, 0);
  289. }
  290. /*
  291. * HPET MSI Support
  292. */
  293. #ifdef CONFIG_PCI_MSI
  294. static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
  295. static struct hpet_dev *hpet_devs;
  296. void hpet_msi_unmask(unsigned int irq)
  297. {
  298. struct hpet_dev *hdev = get_irq_data(irq);
  299. unsigned long cfg;
  300. /* unmask it */
  301. cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
  302. cfg |= HPET_TN_FSB;
  303. hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
  304. }
  305. void hpet_msi_mask(unsigned int irq)
  306. {
  307. unsigned long cfg;
  308. struct hpet_dev *hdev = get_irq_data(irq);
  309. /* mask it */
  310. cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
  311. cfg &= ~HPET_TN_FSB;
  312. hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
  313. }
  314. void hpet_msi_write(unsigned int irq, struct msi_msg *msg)
  315. {
  316. struct hpet_dev *hdev = get_irq_data(irq);
  317. hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
  318. hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
  319. }
  320. void hpet_msi_read(unsigned int irq, struct msi_msg *msg)
  321. {
  322. struct hpet_dev *hdev = get_irq_data(irq);
  323. msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
  324. msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
  325. msg->address_hi = 0;
  326. }
  327. static void hpet_msi_set_mode(enum clock_event_mode mode,
  328. struct clock_event_device *evt)
  329. {
  330. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  331. hpet_set_mode(mode, evt, hdev->num);
  332. }
  333. static int hpet_msi_next_event(unsigned long delta,
  334. struct clock_event_device *evt)
  335. {
  336. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  337. return hpet_next_event(delta, evt, hdev->num);
  338. }
  339. static int hpet_setup_msi_irq(unsigned int irq)
  340. {
  341. if (arch_setup_hpet_msi(irq)) {
  342. destroy_irq(irq);
  343. return -EINVAL;
  344. }
  345. return 0;
  346. }
  347. static int hpet_assign_irq(struct hpet_dev *dev)
  348. {
  349. unsigned int irq;
  350. irq = create_irq();
  351. if (!irq)
  352. return -EINVAL;
  353. set_irq_data(irq, dev);
  354. if (hpet_setup_msi_irq(irq))
  355. return -EINVAL;
  356. dev->irq = irq;
  357. return 0;
  358. }
  359. static irqreturn_t hpet_interrupt_handler(int irq, void *data)
  360. {
  361. struct hpet_dev *dev = (struct hpet_dev *)data;
  362. struct clock_event_device *hevt = &dev->evt;
  363. if (!hevt->event_handler) {
  364. printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
  365. dev->num);
  366. return IRQ_HANDLED;
  367. }
  368. hevt->event_handler(hevt);
  369. return IRQ_HANDLED;
  370. }
  371. static int hpet_setup_irq(struct hpet_dev *dev)
  372. {
  373. if (request_irq(dev->irq, hpet_interrupt_handler,
  374. IRQF_DISABLED|IRQF_NOBALANCING, dev->name, dev))
  375. return -1;
  376. disable_irq(dev->irq);
  377. irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
  378. enable_irq(dev->irq);
  379. printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
  380. dev->name, dev->irq);
  381. return 0;
  382. }
  383. /* This should be called in specific @cpu */
  384. static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
  385. {
  386. struct clock_event_device *evt = &hdev->evt;
  387. uint64_t hpet_freq;
  388. WARN_ON(cpu != smp_processor_id());
  389. if (!(hdev->flags & HPET_DEV_VALID))
  390. return;
  391. if (hpet_setup_msi_irq(hdev->irq))
  392. return;
  393. hdev->cpu = cpu;
  394. per_cpu(cpu_hpet_dev, cpu) = hdev;
  395. evt->name = hdev->name;
  396. hpet_setup_irq(hdev);
  397. evt->irq = hdev->irq;
  398. evt->rating = 110;
  399. evt->features = CLOCK_EVT_FEAT_ONESHOT;
  400. if (hdev->flags & HPET_DEV_PERI_CAP)
  401. evt->features |= CLOCK_EVT_FEAT_PERIODIC;
  402. evt->set_mode = hpet_msi_set_mode;
  403. evt->set_next_event = hpet_msi_next_event;
  404. evt->shift = 32;
  405. /*
  406. * The period is a femto seconds value. We need to calculate the
  407. * scaled math multiplication factor for nanosecond to hpet tick
  408. * conversion.
  409. */
  410. hpet_freq = 1000000000000000ULL;
  411. do_div(hpet_freq, hpet_period);
  412. evt->mult = div_sc((unsigned long) hpet_freq,
  413. NSEC_PER_SEC, evt->shift);
  414. /* Calculate the max delta */
  415. evt->max_delta_ns = clockevent_delta2ns(0x7FFFFFFF, evt);
  416. /* 5 usec minimum reprogramming delta. */
  417. evt->min_delta_ns = 5000;
  418. evt->cpumask = cpumask_of(hdev->cpu);
  419. clockevents_register_device(evt);
  420. }
  421. #ifdef CONFIG_HPET
  422. /* Reserve at least one timer for userspace (/dev/hpet) */
  423. #define RESERVE_TIMERS 1
  424. #else
  425. #define RESERVE_TIMERS 0
  426. #endif
  427. static void hpet_msi_capability_lookup(unsigned int start_timer)
  428. {
  429. unsigned int id;
  430. unsigned int num_timers;
  431. unsigned int num_timers_used = 0;
  432. int i;
  433. id = hpet_readl(HPET_ID);
  434. num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
  435. num_timers++; /* Value read out starts from 0 */
  436. hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
  437. if (!hpet_devs)
  438. return;
  439. hpet_num_timers = num_timers;
  440. for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
  441. struct hpet_dev *hdev = &hpet_devs[num_timers_used];
  442. unsigned long cfg = hpet_readl(HPET_Tn_CFG(i));
  443. /* Only consider HPET timer with MSI support */
  444. if (!(cfg & HPET_TN_FSB_CAP))
  445. continue;
  446. hdev->flags = 0;
  447. if (cfg & HPET_TN_PERIODIC_CAP)
  448. hdev->flags |= HPET_DEV_PERI_CAP;
  449. hdev->num = i;
  450. sprintf(hdev->name, "hpet%d", i);
  451. if (hpet_assign_irq(hdev))
  452. continue;
  453. hdev->flags |= HPET_DEV_FSB_CAP;
  454. hdev->flags |= HPET_DEV_VALID;
  455. num_timers_used++;
  456. if (num_timers_used == num_possible_cpus())
  457. break;
  458. }
  459. printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
  460. num_timers, num_timers_used);
  461. }
  462. #ifdef CONFIG_HPET
  463. static void hpet_reserve_msi_timers(struct hpet_data *hd)
  464. {
  465. int i;
  466. if (!hpet_devs)
  467. return;
  468. for (i = 0; i < hpet_num_timers; i++) {
  469. struct hpet_dev *hdev = &hpet_devs[i];
  470. if (!(hdev->flags & HPET_DEV_VALID))
  471. continue;
  472. hd->hd_irq[hdev->num] = hdev->irq;
  473. hpet_reserve_timer(hd, hdev->num);
  474. }
  475. }
  476. #endif
  477. static struct hpet_dev *hpet_get_unused_timer(void)
  478. {
  479. int i;
  480. if (!hpet_devs)
  481. return NULL;
  482. for (i = 0; i < hpet_num_timers; i++) {
  483. struct hpet_dev *hdev = &hpet_devs[i];
  484. if (!(hdev->flags & HPET_DEV_VALID))
  485. continue;
  486. if (test_and_set_bit(HPET_DEV_USED_BIT,
  487. (unsigned long *)&hdev->flags))
  488. continue;
  489. return hdev;
  490. }
  491. return NULL;
  492. }
  493. struct hpet_work_struct {
  494. struct delayed_work work;
  495. struct completion complete;
  496. };
  497. static void hpet_work(struct work_struct *w)
  498. {
  499. struct hpet_dev *hdev;
  500. int cpu = smp_processor_id();
  501. struct hpet_work_struct *hpet_work;
  502. hpet_work = container_of(w, struct hpet_work_struct, work.work);
  503. hdev = hpet_get_unused_timer();
  504. if (hdev)
  505. init_one_hpet_msi_clockevent(hdev, cpu);
  506. complete(&hpet_work->complete);
  507. }
  508. static int hpet_cpuhp_notify(struct notifier_block *n,
  509. unsigned long action, void *hcpu)
  510. {
  511. unsigned long cpu = (unsigned long)hcpu;
  512. struct hpet_work_struct work;
  513. struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
  514. switch (action & 0xf) {
  515. case CPU_ONLINE:
  516. INIT_DELAYED_WORK(&work.work, hpet_work);
  517. init_completion(&work.complete);
  518. /* FIXME: add schedule_work_on() */
  519. schedule_delayed_work_on(cpu, &work.work, 0);
  520. wait_for_completion(&work.complete);
  521. break;
  522. case CPU_DEAD:
  523. if (hdev) {
  524. free_irq(hdev->irq, hdev);
  525. hdev->flags &= ~HPET_DEV_USED;
  526. per_cpu(cpu_hpet_dev, cpu) = NULL;
  527. }
  528. break;
  529. }
  530. return NOTIFY_OK;
  531. }
  532. #else
  533. static int hpet_setup_msi_irq(unsigned int irq)
  534. {
  535. return 0;
  536. }
  537. static void hpet_msi_capability_lookup(unsigned int start_timer)
  538. {
  539. return;
  540. }
  541. #ifdef CONFIG_HPET
  542. static void hpet_reserve_msi_timers(struct hpet_data *hd)
  543. {
  544. return;
  545. }
  546. #endif
  547. static int hpet_cpuhp_notify(struct notifier_block *n,
  548. unsigned long action, void *hcpu)
  549. {
  550. return NOTIFY_OK;
  551. }
  552. #endif
  553. /*
  554. * Clock source related code
  555. */
  556. static cycle_t read_hpet(void)
  557. {
  558. return (cycle_t)hpet_readl(HPET_COUNTER);
  559. }
  560. #ifdef CONFIG_X86_64
  561. static cycle_t __vsyscall_fn vread_hpet(void)
  562. {
  563. return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
  564. }
  565. #endif
  566. static struct clocksource clocksource_hpet = {
  567. .name = "hpet",
  568. .rating = 250,
  569. .read = read_hpet,
  570. .mask = HPET_MASK,
  571. .shift = HPET_SHIFT,
  572. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  573. .resume = hpet_restart_counter,
  574. #ifdef CONFIG_X86_64
  575. .vread = vread_hpet,
  576. #endif
  577. };
  578. static int hpet_clocksource_register(void)
  579. {
  580. u64 start, now;
  581. cycle_t t1;
  582. /* Start the counter */
  583. hpet_start_counter();
  584. /* Verify whether hpet counter works */
  585. t1 = read_hpet();
  586. rdtscll(start);
  587. /*
  588. * We don't know the TSC frequency yet, but waiting for
  589. * 200000 TSC cycles is safe:
  590. * 4 GHz == 50us
  591. * 1 GHz == 200us
  592. */
  593. do {
  594. rep_nop();
  595. rdtscll(now);
  596. } while ((now - start) < 200000UL);
  597. if (t1 == read_hpet()) {
  598. printk(KERN_WARNING
  599. "HPET counter not counting. HPET disabled\n");
  600. return -ENODEV;
  601. }
  602. /*
  603. * The definition of mult is (include/linux/clocksource.h)
  604. * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
  605. * so we first need to convert hpet_period to ns/cyc units:
  606. * mult/2^shift = ns/cyc = hpet_period/10^6
  607. * mult = (hpet_period * 2^shift)/10^6
  608. * mult = (hpet_period << shift)/FSEC_PER_NSEC
  609. */
  610. clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
  611. clocksource_register(&clocksource_hpet);
  612. return 0;
  613. }
  614. /**
  615. * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
  616. */
  617. int __init hpet_enable(void)
  618. {
  619. unsigned long id;
  620. int i;
  621. if (!is_hpet_capable())
  622. return 0;
  623. hpet_set_mapping();
  624. /*
  625. * Read the period and check for a sane value:
  626. */
  627. hpet_period = hpet_readl(HPET_PERIOD);
  628. /*
  629. * AMD SB700 based systems with spread spectrum enabled use a
  630. * SMM based HPET emulation to provide proper frequency
  631. * setting. The SMM code is initialized with the first HPET
  632. * register access and takes some time to complete. During
  633. * this time the config register reads 0xffffffff. We check
  634. * for max. 1000 loops whether the config register reads a non
  635. * 0xffffffff value to make sure that HPET is up and running
  636. * before we go further. A counting loop is safe, as the HPET
  637. * access takes thousands of CPU cycles. On non SB700 based
  638. * machines this check is only done once and has no side
  639. * effects.
  640. */
  641. for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
  642. if (i == 1000) {
  643. printk(KERN_WARNING
  644. "HPET config register value = 0xFFFFFFFF. "
  645. "Disabling HPET\n");
  646. goto out_nohpet;
  647. }
  648. }
  649. if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
  650. goto out_nohpet;
  651. /*
  652. * Read the HPET ID register to retrieve the IRQ routing
  653. * information and the number of channels
  654. */
  655. id = hpet_readl(HPET_ID);
  656. #ifdef CONFIG_HPET_EMULATE_RTC
  657. /*
  658. * The legacy routing mode needs at least two channels, tick timer
  659. * and the rtc emulation channel.
  660. */
  661. if (!(id & HPET_ID_NUMBER))
  662. goto out_nohpet;
  663. #endif
  664. if (hpet_clocksource_register())
  665. goto out_nohpet;
  666. if (id & HPET_ID_LEGSUP) {
  667. hpet_legacy_clockevent_register();
  668. hpet_msi_capability_lookup(2);
  669. return 1;
  670. }
  671. hpet_msi_capability_lookup(0);
  672. return 0;
  673. out_nohpet:
  674. hpet_clear_mapping();
  675. hpet_address = 0;
  676. return 0;
  677. }
  678. /*
  679. * Needs to be late, as the reserve_timer code calls kalloc !
  680. *
  681. * Not a problem on i386 as hpet_enable is called from late_time_init,
  682. * but on x86_64 it is necessary !
  683. */
  684. static __init int hpet_late_init(void)
  685. {
  686. int cpu;
  687. if (boot_hpet_disable)
  688. return -ENODEV;
  689. if (!hpet_address) {
  690. if (!force_hpet_address)
  691. return -ENODEV;
  692. hpet_address = force_hpet_address;
  693. hpet_enable();
  694. }
  695. if (!hpet_virt_address)
  696. return -ENODEV;
  697. hpet_reserve_platform_timers(hpet_readl(HPET_ID));
  698. for_each_online_cpu(cpu) {
  699. hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
  700. }
  701. /* This notifier should be called after workqueue is ready */
  702. hotcpu_notifier(hpet_cpuhp_notify, -20);
  703. return 0;
  704. }
  705. fs_initcall(hpet_late_init);
  706. void hpet_disable(void)
  707. {
  708. if (is_hpet_capable()) {
  709. unsigned long cfg = hpet_readl(HPET_CFG);
  710. if (hpet_legacy_int_enabled) {
  711. cfg &= ~HPET_CFG_LEGACY;
  712. hpet_legacy_int_enabled = 0;
  713. }
  714. cfg &= ~HPET_CFG_ENABLE;
  715. hpet_writel(cfg, HPET_CFG);
  716. }
  717. }
  718. #ifdef CONFIG_HPET_EMULATE_RTC
  719. /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
  720. * is enabled, we support RTC interrupt functionality in software.
  721. * RTC has 3 kinds of interrupts:
  722. * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
  723. * is updated
  724. * 2) Alarm Interrupt - generate an interrupt at a specific time of day
  725. * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
  726. * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
  727. * (1) and (2) above are implemented using polling at a frequency of
  728. * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
  729. * overhead. (DEFAULT_RTC_INT_FREQ)
  730. * For (3), we use interrupts at 64Hz or user specified periodic
  731. * frequency, whichever is higher.
  732. */
  733. #include <linux/mc146818rtc.h>
  734. #include <linux/rtc.h>
  735. #include <asm/rtc.h>
  736. #define DEFAULT_RTC_INT_FREQ 64
  737. #define DEFAULT_RTC_SHIFT 6
  738. #define RTC_NUM_INTS 1
  739. static unsigned long hpet_rtc_flags;
  740. static int hpet_prev_update_sec;
  741. static struct rtc_time hpet_alarm_time;
  742. static unsigned long hpet_pie_count;
  743. static unsigned long hpet_t1_cmp;
  744. static unsigned long hpet_default_delta;
  745. static unsigned long hpet_pie_delta;
  746. static unsigned long hpet_pie_limit;
  747. static rtc_irq_handler irq_handler;
  748. /*
  749. * Registers a IRQ handler.
  750. */
  751. int hpet_register_irq_handler(rtc_irq_handler handler)
  752. {
  753. if (!is_hpet_enabled())
  754. return -ENODEV;
  755. if (irq_handler)
  756. return -EBUSY;
  757. irq_handler = handler;
  758. return 0;
  759. }
  760. EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
  761. /*
  762. * Deregisters the IRQ handler registered with hpet_register_irq_handler()
  763. * and does cleanup.
  764. */
  765. void hpet_unregister_irq_handler(rtc_irq_handler handler)
  766. {
  767. if (!is_hpet_enabled())
  768. return;
  769. irq_handler = NULL;
  770. hpet_rtc_flags = 0;
  771. }
  772. EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
  773. /*
  774. * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
  775. * is not supported by all HPET implementations for timer 1.
  776. *
  777. * hpet_rtc_timer_init() is called when the rtc is initialized.
  778. */
  779. int hpet_rtc_timer_init(void)
  780. {
  781. unsigned long cfg, cnt, delta, flags;
  782. if (!is_hpet_enabled())
  783. return 0;
  784. if (!hpet_default_delta) {
  785. uint64_t clc;
  786. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  787. clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
  788. hpet_default_delta = (unsigned long) clc;
  789. }
  790. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  791. delta = hpet_default_delta;
  792. else
  793. delta = hpet_pie_delta;
  794. local_irq_save(flags);
  795. cnt = delta + hpet_readl(HPET_COUNTER);
  796. hpet_writel(cnt, HPET_T1_CMP);
  797. hpet_t1_cmp = cnt;
  798. cfg = hpet_readl(HPET_T1_CFG);
  799. cfg &= ~HPET_TN_PERIODIC;
  800. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  801. hpet_writel(cfg, HPET_T1_CFG);
  802. local_irq_restore(flags);
  803. return 1;
  804. }
  805. EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
  806. /*
  807. * The functions below are called from rtc driver.
  808. * Return 0 if HPET is not being used.
  809. * Otherwise do the necessary changes and return 1.
  810. */
  811. int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
  812. {
  813. if (!is_hpet_enabled())
  814. return 0;
  815. hpet_rtc_flags &= ~bit_mask;
  816. return 1;
  817. }
  818. EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
  819. int hpet_set_rtc_irq_bit(unsigned long bit_mask)
  820. {
  821. unsigned long oldbits = hpet_rtc_flags;
  822. if (!is_hpet_enabled())
  823. return 0;
  824. hpet_rtc_flags |= bit_mask;
  825. if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
  826. hpet_prev_update_sec = -1;
  827. if (!oldbits)
  828. hpet_rtc_timer_init();
  829. return 1;
  830. }
  831. EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
  832. int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
  833. unsigned char sec)
  834. {
  835. if (!is_hpet_enabled())
  836. return 0;
  837. hpet_alarm_time.tm_hour = hrs;
  838. hpet_alarm_time.tm_min = min;
  839. hpet_alarm_time.tm_sec = sec;
  840. return 1;
  841. }
  842. EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
  843. int hpet_set_periodic_freq(unsigned long freq)
  844. {
  845. uint64_t clc;
  846. if (!is_hpet_enabled())
  847. return 0;
  848. if (freq <= DEFAULT_RTC_INT_FREQ)
  849. hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
  850. else {
  851. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  852. do_div(clc, freq);
  853. clc >>= hpet_clockevent.shift;
  854. hpet_pie_delta = (unsigned long) clc;
  855. }
  856. return 1;
  857. }
  858. EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
  859. int hpet_rtc_dropped_irq(void)
  860. {
  861. return is_hpet_enabled();
  862. }
  863. EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
  864. static void hpet_rtc_timer_reinit(void)
  865. {
  866. unsigned long cfg, delta;
  867. int lost_ints = -1;
  868. if (unlikely(!hpet_rtc_flags)) {
  869. cfg = hpet_readl(HPET_T1_CFG);
  870. cfg &= ~HPET_TN_ENABLE;
  871. hpet_writel(cfg, HPET_T1_CFG);
  872. return;
  873. }
  874. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  875. delta = hpet_default_delta;
  876. else
  877. delta = hpet_pie_delta;
  878. /*
  879. * Increment the comparator value until we are ahead of the
  880. * current count.
  881. */
  882. do {
  883. hpet_t1_cmp += delta;
  884. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  885. lost_ints++;
  886. } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
  887. if (lost_ints) {
  888. if (hpet_rtc_flags & RTC_PIE)
  889. hpet_pie_count += lost_ints;
  890. if (printk_ratelimit())
  891. printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
  892. lost_ints);
  893. }
  894. }
  895. irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
  896. {
  897. struct rtc_time curr_time;
  898. unsigned long rtc_int_flag = 0;
  899. hpet_rtc_timer_reinit();
  900. memset(&curr_time, 0, sizeof(struct rtc_time));
  901. if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
  902. get_rtc_time(&curr_time);
  903. if (hpet_rtc_flags & RTC_UIE &&
  904. curr_time.tm_sec != hpet_prev_update_sec) {
  905. if (hpet_prev_update_sec >= 0)
  906. rtc_int_flag = RTC_UF;
  907. hpet_prev_update_sec = curr_time.tm_sec;
  908. }
  909. if (hpet_rtc_flags & RTC_PIE &&
  910. ++hpet_pie_count >= hpet_pie_limit) {
  911. rtc_int_flag |= RTC_PF;
  912. hpet_pie_count = 0;
  913. }
  914. if (hpet_rtc_flags & RTC_AIE &&
  915. (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
  916. (curr_time.tm_min == hpet_alarm_time.tm_min) &&
  917. (curr_time.tm_hour == hpet_alarm_time.tm_hour))
  918. rtc_int_flag |= RTC_AF;
  919. if (rtc_int_flag) {
  920. rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
  921. if (irq_handler)
  922. irq_handler(rtc_int_flag, dev_id);
  923. }
  924. return IRQ_HANDLED;
  925. }
  926. EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
  927. #endif