hpet.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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_cpu(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_cpu(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_cpu(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_cpu(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. boot_hpet_disable = 1;
  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. if (!hpet_virt_address)
  695. return -ENODEV;
  696. }
  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