hpet.c 25 KB

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