hpet.c 25 KB

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