hpet.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. #include <linux/clocksource.h>
  2. #include <linux/clockchips.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/export.h>
  5. #include <linux/delay.h>
  6. #include <linux/errno.h>
  7. #include <linux/i8253.h>
  8. #include <linux/slab.h>
  9. #include <linux/hpet.h>
  10. #include <linux/init.h>
  11. #include <linux/cpu.h>
  12. #include <linux/pm.h>
  13. #include <linux/io.h>
  14. #include <asm/cpufeature.h>
  15. #include <asm/irqdomain.h>
  16. #include <asm/fixmap.h>
  17. #include <asm/hpet.h>
  18. #include <asm/time.h>
  19. #define HPET_MASK CLOCKSOURCE_MASK(32)
  20. /* FSEC = 10^-15
  21. NSEC = 10^-9 */
  22. #define FSEC_PER_NSEC 1000000L
  23. #define HPET_DEV_USED_BIT 2
  24. #define HPET_DEV_USED (1 << HPET_DEV_USED_BIT)
  25. #define HPET_DEV_VALID 0x8
  26. #define HPET_DEV_FSB_CAP 0x1000
  27. #define HPET_DEV_PERI_CAP 0x2000
  28. #define HPET_MIN_CYCLES 128
  29. #define HPET_MIN_PROG_DELTA (HPET_MIN_CYCLES + (HPET_MIN_CYCLES >> 1))
  30. /*
  31. * HPET address is set in acpi/boot.c, when an ACPI entry exists
  32. */
  33. unsigned long hpet_address;
  34. u8 hpet_blockid; /* OS timer block num */
  35. bool hpet_msi_disable;
  36. #ifdef CONFIG_PCI_MSI
  37. static unsigned int hpet_num_timers;
  38. #endif
  39. static void __iomem *hpet_virt_address;
  40. struct hpet_dev {
  41. struct clock_event_device evt;
  42. unsigned int num;
  43. int cpu;
  44. unsigned int irq;
  45. unsigned int flags;
  46. char name[10];
  47. };
  48. static inline struct hpet_dev *EVT_TO_HPET_DEV(struct clock_event_device *evtdev)
  49. {
  50. return container_of(evtdev, struct hpet_dev, evt);
  51. }
  52. inline unsigned int hpet_readl(unsigned int a)
  53. {
  54. return readl(hpet_virt_address + a);
  55. }
  56. static inline void hpet_writel(unsigned int d, unsigned int a)
  57. {
  58. writel(d, hpet_virt_address + a);
  59. }
  60. #ifdef CONFIG_X86_64
  61. #include <asm/pgtable.h>
  62. #endif
  63. static inline void hpet_set_mapping(void)
  64. {
  65. hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
  66. }
  67. static inline void hpet_clear_mapping(void)
  68. {
  69. iounmap(hpet_virt_address);
  70. hpet_virt_address = NULL;
  71. }
  72. /*
  73. * HPET command line enable / disable
  74. */
  75. bool boot_hpet_disable;
  76. bool hpet_force_user;
  77. static bool hpet_verbose;
  78. static int __init hpet_setup(char *str)
  79. {
  80. while (str) {
  81. char *next = strchr(str, ',');
  82. if (next)
  83. *next++ = 0;
  84. if (!strncmp("disable", str, 7))
  85. boot_hpet_disable = true;
  86. if (!strncmp("force", str, 5))
  87. hpet_force_user = true;
  88. if (!strncmp("verbose", str, 7))
  89. hpet_verbose = true;
  90. str = next;
  91. }
  92. return 1;
  93. }
  94. __setup("hpet=", hpet_setup);
  95. static int __init disable_hpet(char *str)
  96. {
  97. boot_hpet_disable = true;
  98. return 1;
  99. }
  100. __setup("nohpet", disable_hpet);
  101. static inline int is_hpet_capable(void)
  102. {
  103. return !boot_hpet_disable && hpet_address;
  104. }
  105. /*
  106. * HPET timer interrupt enable / disable
  107. */
  108. static bool hpet_legacy_int_enabled;
  109. /**
  110. * is_hpet_enabled - check whether the hpet timer interrupt is enabled
  111. */
  112. int is_hpet_enabled(void)
  113. {
  114. return is_hpet_capable() && hpet_legacy_int_enabled;
  115. }
  116. EXPORT_SYMBOL_GPL(is_hpet_enabled);
  117. static void _hpet_print_config(const char *function, int line)
  118. {
  119. u32 i, timers, l, h;
  120. printk(KERN_INFO "hpet: %s(%d):\n", function, line);
  121. l = hpet_readl(HPET_ID);
  122. h = hpet_readl(HPET_PERIOD);
  123. timers = ((l & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
  124. printk(KERN_INFO "hpet: ID: 0x%x, PERIOD: 0x%x\n", l, h);
  125. l = hpet_readl(HPET_CFG);
  126. h = hpet_readl(HPET_STATUS);
  127. printk(KERN_INFO "hpet: CFG: 0x%x, STATUS: 0x%x\n", l, h);
  128. l = hpet_readl(HPET_COUNTER);
  129. h = hpet_readl(HPET_COUNTER+4);
  130. printk(KERN_INFO "hpet: COUNTER_l: 0x%x, COUNTER_h: 0x%x\n", l, h);
  131. for (i = 0; i < timers; i++) {
  132. l = hpet_readl(HPET_Tn_CFG(i));
  133. h = hpet_readl(HPET_Tn_CFG(i)+4);
  134. printk(KERN_INFO "hpet: T%d: CFG_l: 0x%x, CFG_h: 0x%x\n",
  135. i, l, h);
  136. l = hpet_readl(HPET_Tn_CMP(i));
  137. h = hpet_readl(HPET_Tn_CMP(i)+4);
  138. printk(KERN_INFO "hpet: T%d: CMP_l: 0x%x, CMP_h: 0x%x\n",
  139. i, l, h);
  140. l = hpet_readl(HPET_Tn_ROUTE(i));
  141. h = hpet_readl(HPET_Tn_ROUTE(i)+4);
  142. printk(KERN_INFO "hpet: T%d ROUTE_l: 0x%x, ROUTE_h: 0x%x\n",
  143. i, l, h);
  144. }
  145. }
  146. #define hpet_print_config() \
  147. do { \
  148. if (hpet_verbose) \
  149. _hpet_print_config(__func__, __LINE__); \
  150. } while (0)
  151. /*
  152. * When the hpet driver (/dev/hpet) is enabled, we need to reserve
  153. * timer 0 and timer 1 in case of RTC emulation.
  154. */
  155. #ifdef CONFIG_HPET
  156. static void hpet_reserve_msi_timers(struct hpet_data *hd);
  157. static void hpet_reserve_platform_timers(unsigned int id)
  158. {
  159. struct hpet __iomem *hpet = hpet_virt_address;
  160. struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
  161. unsigned int nrtimers, i;
  162. struct hpet_data hd;
  163. nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
  164. memset(&hd, 0, sizeof(hd));
  165. hd.hd_phys_address = hpet_address;
  166. hd.hd_address = hpet;
  167. hd.hd_nirqs = nrtimers;
  168. hpet_reserve_timer(&hd, 0);
  169. #ifdef CONFIG_HPET_EMULATE_RTC
  170. hpet_reserve_timer(&hd, 1);
  171. #endif
  172. /*
  173. * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
  174. * is wrong for i8259!) not the output IRQ. Many BIOS writers
  175. * don't bother configuring *any* comparator interrupts.
  176. */
  177. hd.hd_irq[0] = HPET_LEGACY_8254;
  178. hd.hd_irq[1] = HPET_LEGACY_RTC;
  179. for (i = 2; i < nrtimers; timer++, i++) {
  180. hd.hd_irq[i] = (readl(&timer->hpet_config) &
  181. Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
  182. }
  183. hpet_reserve_msi_timers(&hd);
  184. hpet_alloc(&hd);
  185. }
  186. #else
  187. static void hpet_reserve_platform_timers(unsigned int id) { }
  188. #endif
  189. /*
  190. * Common hpet info
  191. */
  192. static unsigned long hpet_freq;
  193. static struct clock_event_device hpet_clockevent;
  194. static void hpet_stop_counter(void)
  195. {
  196. u32 cfg = hpet_readl(HPET_CFG);
  197. cfg &= ~HPET_CFG_ENABLE;
  198. hpet_writel(cfg, HPET_CFG);
  199. }
  200. static void hpet_reset_counter(void)
  201. {
  202. hpet_writel(0, HPET_COUNTER);
  203. hpet_writel(0, HPET_COUNTER + 4);
  204. }
  205. static void hpet_start_counter(void)
  206. {
  207. unsigned int cfg = hpet_readl(HPET_CFG);
  208. cfg |= HPET_CFG_ENABLE;
  209. hpet_writel(cfg, HPET_CFG);
  210. }
  211. static void hpet_restart_counter(void)
  212. {
  213. hpet_stop_counter();
  214. hpet_reset_counter();
  215. hpet_start_counter();
  216. }
  217. static void hpet_resume_device(void)
  218. {
  219. force_hpet_resume();
  220. }
  221. static void hpet_resume_counter(struct clocksource *cs)
  222. {
  223. hpet_resume_device();
  224. hpet_restart_counter();
  225. }
  226. static void hpet_enable_legacy_int(void)
  227. {
  228. unsigned int cfg = hpet_readl(HPET_CFG);
  229. cfg |= HPET_CFG_LEGACY;
  230. hpet_writel(cfg, HPET_CFG);
  231. hpet_legacy_int_enabled = true;
  232. }
  233. static void hpet_legacy_clockevent_register(void)
  234. {
  235. /* Start HPET legacy interrupts */
  236. hpet_enable_legacy_int();
  237. /*
  238. * Start hpet with the boot cpu mask and make it
  239. * global after the IO_APIC has been initialized.
  240. */
  241. hpet_clockevent.cpumask = cpumask_of(smp_processor_id());
  242. clockevents_config_and_register(&hpet_clockevent, hpet_freq,
  243. HPET_MIN_PROG_DELTA, 0x7FFFFFFF);
  244. global_clock_event = &hpet_clockevent;
  245. printk(KERN_DEBUG "hpet clockevent registered\n");
  246. }
  247. static int hpet_set_periodic(struct clock_event_device *evt, int timer)
  248. {
  249. unsigned int cfg, cmp, now;
  250. uint64_t delta;
  251. hpet_stop_counter();
  252. delta = ((uint64_t)(NSEC_PER_SEC / HZ)) * evt->mult;
  253. delta >>= evt->shift;
  254. now = hpet_readl(HPET_COUNTER);
  255. cmp = now + (unsigned int)delta;
  256. cfg = hpet_readl(HPET_Tn_CFG(timer));
  257. cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC | HPET_TN_SETVAL |
  258. HPET_TN_32BIT;
  259. hpet_writel(cfg, HPET_Tn_CFG(timer));
  260. hpet_writel(cmp, HPET_Tn_CMP(timer));
  261. udelay(1);
  262. /*
  263. * HPET on AMD 81xx needs a second write (with HPET_TN_SETVAL
  264. * cleared) to T0_CMP to set the period. The HPET_TN_SETVAL
  265. * bit is automatically cleared after the first write.
  266. * (See AMD-8111 HyperTransport I/O Hub Data Sheet,
  267. * Publication # 24674)
  268. */
  269. hpet_writel((unsigned int)delta, HPET_Tn_CMP(timer));
  270. hpet_start_counter();
  271. hpet_print_config();
  272. return 0;
  273. }
  274. static int hpet_set_oneshot(struct clock_event_device *evt, int timer)
  275. {
  276. unsigned int cfg;
  277. cfg = hpet_readl(HPET_Tn_CFG(timer));
  278. cfg &= ~HPET_TN_PERIODIC;
  279. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  280. hpet_writel(cfg, HPET_Tn_CFG(timer));
  281. return 0;
  282. }
  283. static int hpet_shutdown(struct clock_event_device *evt, int timer)
  284. {
  285. unsigned int cfg;
  286. cfg = hpet_readl(HPET_Tn_CFG(timer));
  287. cfg &= ~HPET_TN_ENABLE;
  288. hpet_writel(cfg, HPET_Tn_CFG(timer));
  289. return 0;
  290. }
  291. static int hpet_resume(struct clock_event_device *evt, int timer)
  292. {
  293. if (!timer) {
  294. hpet_enable_legacy_int();
  295. } else {
  296. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  297. irq_domain_activate_irq(irq_get_irq_data(hdev->irq));
  298. disable_irq(hdev->irq);
  299. irq_set_affinity(hdev->irq, cpumask_of(hdev->cpu));
  300. enable_irq(hdev->irq);
  301. }
  302. hpet_print_config();
  303. return 0;
  304. }
  305. static int hpet_next_event(unsigned long delta,
  306. struct clock_event_device *evt, int timer)
  307. {
  308. u32 cnt;
  309. s32 res;
  310. cnt = hpet_readl(HPET_COUNTER);
  311. cnt += (u32) delta;
  312. hpet_writel(cnt, HPET_Tn_CMP(timer));
  313. /*
  314. * HPETs are a complete disaster. The compare register is
  315. * based on a equal comparison and neither provides a less
  316. * than or equal functionality (which would require to take
  317. * the wraparound into account) nor a simple count down event
  318. * mode. Further the write to the comparator register is
  319. * delayed internally up to two HPET clock cycles in certain
  320. * chipsets (ATI, ICH9,10). Some newer AMD chipsets have even
  321. * longer delays. We worked around that by reading back the
  322. * compare register, but that required another workaround for
  323. * ICH9,10 chips where the first readout after write can
  324. * return the old stale value. We already had a minimum
  325. * programming delta of 5us enforced, but a NMI or SMI hitting
  326. * between the counter readout and the comparator write can
  327. * move us behind that point easily. Now instead of reading
  328. * the compare register back several times, we make the ETIME
  329. * decision based on the following: Return ETIME if the
  330. * counter value after the write is less than HPET_MIN_CYCLES
  331. * away from the event or if the counter is already ahead of
  332. * the event. The minimum programming delta for the generic
  333. * clockevents code is set to 1.5 * HPET_MIN_CYCLES.
  334. */
  335. res = (s32)(cnt - hpet_readl(HPET_COUNTER));
  336. return res < HPET_MIN_CYCLES ? -ETIME : 0;
  337. }
  338. static int hpet_legacy_shutdown(struct clock_event_device *evt)
  339. {
  340. return hpet_shutdown(evt, 0);
  341. }
  342. static int hpet_legacy_set_oneshot(struct clock_event_device *evt)
  343. {
  344. return hpet_set_oneshot(evt, 0);
  345. }
  346. static int hpet_legacy_set_periodic(struct clock_event_device *evt)
  347. {
  348. return hpet_set_periodic(evt, 0);
  349. }
  350. static int hpet_legacy_resume(struct clock_event_device *evt)
  351. {
  352. return hpet_resume(evt, 0);
  353. }
  354. static int hpet_legacy_next_event(unsigned long delta,
  355. struct clock_event_device *evt)
  356. {
  357. return hpet_next_event(delta, evt, 0);
  358. }
  359. /*
  360. * The hpet clock event device
  361. */
  362. static struct clock_event_device hpet_clockevent = {
  363. .name = "hpet",
  364. .features = CLOCK_EVT_FEAT_PERIODIC |
  365. CLOCK_EVT_FEAT_ONESHOT,
  366. .set_state_periodic = hpet_legacy_set_periodic,
  367. .set_state_oneshot = hpet_legacy_set_oneshot,
  368. .set_state_shutdown = hpet_legacy_shutdown,
  369. .tick_resume = hpet_legacy_resume,
  370. .set_next_event = hpet_legacy_next_event,
  371. .irq = 0,
  372. .rating = 50,
  373. };
  374. /*
  375. * HPET MSI Support
  376. */
  377. #ifdef CONFIG_PCI_MSI
  378. static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
  379. static struct hpet_dev *hpet_devs;
  380. static struct irq_domain *hpet_domain;
  381. void hpet_msi_unmask(struct irq_data *data)
  382. {
  383. struct hpet_dev *hdev = irq_data_get_irq_handler_data(data);
  384. unsigned int cfg;
  385. /* unmask it */
  386. cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
  387. cfg |= HPET_TN_ENABLE | HPET_TN_FSB;
  388. hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
  389. }
  390. void hpet_msi_mask(struct irq_data *data)
  391. {
  392. struct hpet_dev *hdev = irq_data_get_irq_handler_data(data);
  393. unsigned int cfg;
  394. /* mask it */
  395. cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
  396. cfg &= ~(HPET_TN_ENABLE | HPET_TN_FSB);
  397. hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
  398. }
  399. void hpet_msi_write(struct hpet_dev *hdev, struct msi_msg *msg)
  400. {
  401. hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
  402. hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
  403. }
  404. void hpet_msi_read(struct hpet_dev *hdev, struct msi_msg *msg)
  405. {
  406. msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
  407. msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
  408. msg->address_hi = 0;
  409. }
  410. static int hpet_msi_shutdown(struct clock_event_device *evt)
  411. {
  412. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  413. return hpet_shutdown(evt, hdev->num);
  414. }
  415. static int hpet_msi_set_oneshot(struct clock_event_device *evt)
  416. {
  417. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  418. return hpet_set_oneshot(evt, hdev->num);
  419. }
  420. static int hpet_msi_set_periodic(struct clock_event_device *evt)
  421. {
  422. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  423. return hpet_set_periodic(evt, hdev->num);
  424. }
  425. static int hpet_msi_resume(struct clock_event_device *evt)
  426. {
  427. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  428. return hpet_resume(evt, hdev->num);
  429. }
  430. static int hpet_msi_next_event(unsigned long delta,
  431. struct clock_event_device *evt)
  432. {
  433. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  434. return hpet_next_event(delta, evt, hdev->num);
  435. }
  436. static irqreturn_t hpet_interrupt_handler(int irq, void *data)
  437. {
  438. struct hpet_dev *dev = (struct hpet_dev *)data;
  439. struct clock_event_device *hevt = &dev->evt;
  440. if (!hevt->event_handler) {
  441. printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
  442. dev->num);
  443. return IRQ_HANDLED;
  444. }
  445. hevt->event_handler(hevt);
  446. return IRQ_HANDLED;
  447. }
  448. static int hpet_setup_irq(struct hpet_dev *dev)
  449. {
  450. if (request_irq(dev->irq, hpet_interrupt_handler,
  451. IRQF_TIMER | IRQF_NOBALANCING,
  452. dev->name, dev))
  453. return -1;
  454. disable_irq(dev->irq);
  455. irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
  456. enable_irq(dev->irq);
  457. printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
  458. dev->name, dev->irq);
  459. return 0;
  460. }
  461. /* This should be called in specific @cpu */
  462. static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
  463. {
  464. struct clock_event_device *evt = &hdev->evt;
  465. WARN_ON(cpu != smp_processor_id());
  466. if (!(hdev->flags & HPET_DEV_VALID))
  467. return;
  468. hdev->cpu = cpu;
  469. per_cpu(cpu_hpet_dev, cpu) = hdev;
  470. evt->name = hdev->name;
  471. hpet_setup_irq(hdev);
  472. evt->irq = hdev->irq;
  473. evt->rating = 110;
  474. evt->features = CLOCK_EVT_FEAT_ONESHOT;
  475. if (hdev->flags & HPET_DEV_PERI_CAP) {
  476. evt->features |= CLOCK_EVT_FEAT_PERIODIC;
  477. evt->set_state_periodic = hpet_msi_set_periodic;
  478. }
  479. evt->set_state_shutdown = hpet_msi_shutdown;
  480. evt->set_state_oneshot = hpet_msi_set_oneshot;
  481. evt->tick_resume = hpet_msi_resume;
  482. evt->set_next_event = hpet_msi_next_event;
  483. evt->cpumask = cpumask_of(hdev->cpu);
  484. clockevents_config_and_register(evt, hpet_freq, HPET_MIN_PROG_DELTA,
  485. 0x7FFFFFFF);
  486. }
  487. #ifdef CONFIG_HPET
  488. /* Reserve at least one timer for userspace (/dev/hpet) */
  489. #define RESERVE_TIMERS 1
  490. #else
  491. #define RESERVE_TIMERS 0
  492. #endif
  493. static void hpet_msi_capability_lookup(unsigned int start_timer)
  494. {
  495. unsigned int id;
  496. unsigned int num_timers;
  497. unsigned int num_timers_used = 0;
  498. int i, irq;
  499. if (hpet_msi_disable)
  500. return;
  501. if (boot_cpu_has(X86_FEATURE_ARAT))
  502. return;
  503. id = hpet_readl(HPET_ID);
  504. num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
  505. num_timers++; /* Value read out starts from 0 */
  506. hpet_print_config();
  507. hpet_domain = hpet_create_irq_domain(hpet_blockid);
  508. if (!hpet_domain)
  509. return;
  510. hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
  511. if (!hpet_devs)
  512. return;
  513. hpet_num_timers = num_timers;
  514. for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
  515. struct hpet_dev *hdev = &hpet_devs[num_timers_used];
  516. unsigned int cfg = hpet_readl(HPET_Tn_CFG(i));
  517. /* Only consider HPET timer with MSI support */
  518. if (!(cfg & HPET_TN_FSB_CAP))
  519. continue;
  520. hdev->flags = 0;
  521. if (cfg & HPET_TN_PERIODIC_CAP)
  522. hdev->flags |= HPET_DEV_PERI_CAP;
  523. sprintf(hdev->name, "hpet%d", i);
  524. hdev->num = i;
  525. irq = hpet_assign_irq(hpet_domain, hdev, hdev->num);
  526. if (irq <= 0)
  527. continue;
  528. hdev->irq = irq;
  529. hdev->flags |= HPET_DEV_FSB_CAP;
  530. hdev->flags |= HPET_DEV_VALID;
  531. num_timers_used++;
  532. if (num_timers_used == num_possible_cpus())
  533. break;
  534. }
  535. printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
  536. num_timers, num_timers_used);
  537. }
  538. #ifdef CONFIG_HPET
  539. static void hpet_reserve_msi_timers(struct hpet_data *hd)
  540. {
  541. int i;
  542. if (!hpet_devs)
  543. return;
  544. for (i = 0; i < hpet_num_timers; i++) {
  545. struct hpet_dev *hdev = &hpet_devs[i];
  546. if (!(hdev->flags & HPET_DEV_VALID))
  547. continue;
  548. hd->hd_irq[hdev->num] = hdev->irq;
  549. hpet_reserve_timer(hd, hdev->num);
  550. }
  551. }
  552. #endif
  553. static struct hpet_dev *hpet_get_unused_timer(void)
  554. {
  555. int i;
  556. if (!hpet_devs)
  557. return NULL;
  558. for (i = 0; i < hpet_num_timers; i++) {
  559. struct hpet_dev *hdev = &hpet_devs[i];
  560. if (!(hdev->flags & HPET_DEV_VALID))
  561. continue;
  562. if (test_and_set_bit(HPET_DEV_USED_BIT,
  563. (unsigned long *)&hdev->flags))
  564. continue;
  565. return hdev;
  566. }
  567. return NULL;
  568. }
  569. struct hpet_work_struct {
  570. struct delayed_work work;
  571. struct completion complete;
  572. };
  573. static void hpet_work(struct work_struct *w)
  574. {
  575. struct hpet_dev *hdev;
  576. int cpu = smp_processor_id();
  577. struct hpet_work_struct *hpet_work;
  578. hpet_work = container_of(w, struct hpet_work_struct, work.work);
  579. hdev = hpet_get_unused_timer();
  580. if (hdev)
  581. init_one_hpet_msi_clockevent(hdev, cpu);
  582. complete(&hpet_work->complete);
  583. }
  584. static int hpet_cpuhp_online(unsigned int cpu)
  585. {
  586. struct hpet_work_struct work;
  587. INIT_DELAYED_WORK_ONSTACK(&work.work, hpet_work);
  588. init_completion(&work.complete);
  589. /* FIXME: add schedule_work_on() */
  590. schedule_delayed_work_on(cpu, &work.work, 0);
  591. wait_for_completion(&work.complete);
  592. destroy_delayed_work_on_stack(&work.work);
  593. return 0;
  594. }
  595. static int hpet_cpuhp_dead(unsigned int cpu)
  596. {
  597. struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
  598. if (!hdev)
  599. return 0;
  600. free_irq(hdev->irq, hdev);
  601. hdev->flags &= ~HPET_DEV_USED;
  602. per_cpu(cpu_hpet_dev, cpu) = NULL;
  603. return 0;
  604. }
  605. #else
  606. static void hpet_msi_capability_lookup(unsigned int start_timer)
  607. {
  608. return;
  609. }
  610. #ifdef CONFIG_HPET
  611. static void hpet_reserve_msi_timers(struct hpet_data *hd)
  612. {
  613. return;
  614. }
  615. #endif
  616. #define hpet_cpuhp_online NULL
  617. #define hpet_cpuhp_dead NULL
  618. #endif
  619. /*
  620. * Clock source related code
  621. */
  622. #if defined(CONFIG_SMP) && defined(CONFIG_64BIT)
  623. /*
  624. * Reading the HPET counter is a very slow operation. If a large number of
  625. * CPUs are trying to access the HPET counter simultaneously, it can cause
  626. * massive delay and slow down system performance dramatically. This may
  627. * happen when HPET is the default clock source instead of TSC. For a
  628. * really large system with hundreds of CPUs, the slowdown may be so
  629. * severe that it may actually crash the system because of a NMI watchdog
  630. * soft lockup, for example.
  631. *
  632. * If multiple CPUs are trying to access the HPET counter at the same time,
  633. * we don't actually need to read the counter multiple times. Instead, the
  634. * other CPUs can use the counter value read by the first CPU in the group.
  635. *
  636. * This special feature is only enabled on x86-64 systems. It is unlikely
  637. * that 32-bit x86 systems will have enough CPUs to require this feature
  638. * with its associated locking overhead. And we also need 64-bit atomic
  639. * read.
  640. *
  641. * The lock and the hpet value are stored together and can be read in a
  642. * single atomic 64-bit read. It is explicitly assumed that arch_spinlock_t
  643. * is 32 bits in size.
  644. */
  645. union hpet_lock {
  646. struct {
  647. arch_spinlock_t lock;
  648. u32 value;
  649. };
  650. u64 lockval;
  651. };
  652. static union hpet_lock hpet __cacheline_aligned = {
  653. { .lock = __ARCH_SPIN_LOCK_UNLOCKED, },
  654. };
  655. static u64 read_hpet(struct clocksource *cs)
  656. {
  657. unsigned long flags;
  658. union hpet_lock old, new;
  659. BUILD_BUG_ON(sizeof(union hpet_lock) != 8);
  660. /*
  661. * Read HPET directly if in NMI.
  662. */
  663. if (in_nmi())
  664. return (u64)hpet_readl(HPET_COUNTER);
  665. /*
  666. * Read the current state of the lock and HPET value atomically.
  667. */
  668. old.lockval = READ_ONCE(hpet.lockval);
  669. if (arch_spin_is_locked(&old.lock))
  670. goto contended;
  671. local_irq_save(flags);
  672. if (arch_spin_trylock(&hpet.lock)) {
  673. new.value = hpet_readl(HPET_COUNTER);
  674. /*
  675. * Use WRITE_ONCE() to prevent store tearing.
  676. */
  677. WRITE_ONCE(hpet.value, new.value);
  678. arch_spin_unlock(&hpet.lock);
  679. local_irq_restore(flags);
  680. return (u64)new.value;
  681. }
  682. local_irq_restore(flags);
  683. contended:
  684. /*
  685. * Contended case
  686. * --------------
  687. * Wait until the HPET value change or the lock is free to indicate
  688. * its value is up-to-date.
  689. *
  690. * It is possible that old.value has already contained the latest
  691. * HPET value while the lock holder was in the process of releasing
  692. * the lock. Checking for lock state change will enable us to return
  693. * the value immediately instead of waiting for the next HPET reader
  694. * to come along.
  695. */
  696. do {
  697. cpu_relax();
  698. new.lockval = READ_ONCE(hpet.lockval);
  699. } while ((new.value == old.value) && arch_spin_is_locked(&new.lock));
  700. return (u64)new.value;
  701. }
  702. #else
  703. /*
  704. * For UP or 32-bit.
  705. */
  706. static u64 read_hpet(struct clocksource *cs)
  707. {
  708. return (u64)hpet_readl(HPET_COUNTER);
  709. }
  710. #endif
  711. static struct clocksource clocksource_hpet = {
  712. .name = "hpet",
  713. .rating = 250,
  714. .read = read_hpet,
  715. .mask = HPET_MASK,
  716. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  717. .resume = hpet_resume_counter,
  718. };
  719. static int hpet_clocksource_register(void)
  720. {
  721. u64 start, now;
  722. u64 t1;
  723. /* Start the counter */
  724. hpet_restart_counter();
  725. /* Verify whether hpet counter works */
  726. t1 = hpet_readl(HPET_COUNTER);
  727. start = rdtsc();
  728. /*
  729. * We don't know the TSC frequency yet, but waiting for
  730. * 200000 TSC cycles is safe:
  731. * 4 GHz == 50us
  732. * 1 GHz == 200us
  733. */
  734. do {
  735. rep_nop();
  736. now = rdtsc();
  737. } while ((now - start) < 200000UL);
  738. if (t1 == hpet_readl(HPET_COUNTER)) {
  739. printk(KERN_WARNING
  740. "HPET counter not counting. HPET disabled\n");
  741. return -ENODEV;
  742. }
  743. clocksource_register_hz(&clocksource_hpet, (u32)hpet_freq);
  744. return 0;
  745. }
  746. static u32 *hpet_boot_cfg;
  747. /**
  748. * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
  749. */
  750. int __init hpet_enable(void)
  751. {
  752. u32 hpet_period, cfg, id;
  753. u64 freq;
  754. unsigned int i, last;
  755. if (!is_hpet_capable())
  756. return 0;
  757. hpet_set_mapping();
  758. /*
  759. * Read the period and check for a sane value:
  760. */
  761. hpet_period = hpet_readl(HPET_PERIOD);
  762. /*
  763. * AMD SB700 based systems with spread spectrum enabled use a
  764. * SMM based HPET emulation to provide proper frequency
  765. * setting. The SMM code is initialized with the first HPET
  766. * register access and takes some time to complete. During
  767. * this time the config register reads 0xffffffff. We check
  768. * for max. 1000 loops whether the config register reads a non
  769. * 0xffffffff value to make sure that HPET is up and running
  770. * before we go further. A counting loop is safe, as the HPET
  771. * access takes thousands of CPU cycles. On non SB700 based
  772. * machines this check is only done once and has no side
  773. * effects.
  774. */
  775. for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
  776. if (i == 1000) {
  777. printk(KERN_WARNING
  778. "HPET config register value = 0xFFFFFFFF. "
  779. "Disabling HPET\n");
  780. goto out_nohpet;
  781. }
  782. }
  783. if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
  784. goto out_nohpet;
  785. /*
  786. * The period is a femto seconds value. Convert it to a
  787. * frequency.
  788. */
  789. freq = FSEC_PER_SEC;
  790. do_div(freq, hpet_period);
  791. hpet_freq = freq;
  792. /*
  793. * Read the HPET ID register to retrieve the IRQ routing
  794. * information and the number of channels
  795. */
  796. id = hpet_readl(HPET_ID);
  797. hpet_print_config();
  798. last = (id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT;
  799. #ifdef CONFIG_HPET_EMULATE_RTC
  800. /*
  801. * The legacy routing mode needs at least two channels, tick timer
  802. * and the rtc emulation channel.
  803. */
  804. if (!last)
  805. goto out_nohpet;
  806. #endif
  807. cfg = hpet_readl(HPET_CFG);
  808. hpet_boot_cfg = kmalloc((last + 2) * sizeof(*hpet_boot_cfg),
  809. GFP_KERNEL);
  810. if (hpet_boot_cfg)
  811. *hpet_boot_cfg = cfg;
  812. else
  813. pr_warn("HPET initial state will not be saved\n");
  814. cfg &= ~(HPET_CFG_ENABLE | HPET_CFG_LEGACY);
  815. hpet_writel(cfg, HPET_CFG);
  816. if (cfg)
  817. pr_warn("HPET: Unrecognized bits %#x set in global cfg\n",
  818. cfg);
  819. for (i = 0; i <= last; ++i) {
  820. cfg = hpet_readl(HPET_Tn_CFG(i));
  821. if (hpet_boot_cfg)
  822. hpet_boot_cfg[i + 1] = cfg;
  823. cfg &= ~(HPET_TN_ENABLE | HPET_TN_LEVEL | HPET_TN_FSB);
  824. hpet_writel(cfg, HPET_Tn_CFG(i));
  825. cfg &= ~(HPET_TN_PERIODIC | HPET_TN_PERIODIC_CAP
  826. | HPET_TN_64BIT_CAP | HPET_TN_32BIT | HPET_TN_ROUTE
  827. | HPET_TN_FSB | HPET_TN_FSB_CAP);
  828. if (cfg)
  829. pr_warn("HPET: Unrecognized bits %#x set in cfg#%u\n",
  830. cfg, i);
  831. }
  832. hpet_print_config();
  833. if (hpet_clocksource_register())
  834. goto out_nohpet;
  835. if (id & HPET_ID_LEGSUP) {
  836. hpet_legacy_clockevent_register();
  837. return 1;
  838. }
  839. return 0;
  840. out_nohpet:
  841. hpet_clear_mapping();
  842. hpet_address = 0;
  843. return 0;
  844. }
  845. /*
  846. * Needs to be late, as the reserve_timer code calls kalloc !
  847. *
  848. * Not a problem on i386 as hpet_enable is called from late_time_init,
  849. * but on x86_64 it is necessary !
  850. */
  851. static __init int hpet_late_init(void)
  852. {
  853. int ret;
  854. if (boot_hpet_disable)
  855. return -ENODEV;
  856. if (!hpet_address) {
  857. if (!force_hpet_address)
  858. return -ENODEV;
  859. hpet_address = force_hpet_address;
  860. hpet_enable();
  861. }
  862. if (!hpet_virt_address)
  863. return -ENODEV;
  864. if (hpet_readl(HPET_ID) & HPET_ID_LEGSUP)
  865. hpet_msi_capability_lookup(2);
  866. else
  867. hpet_msi_capability_lookup(0);
  868. hpet_reserve_platform_timers(hpet_readl(HPET_ID));
  869. hpet_print_config();
  870. if (hpet_msi_disable)
  871. return 0;
  872. if (boot_cpu_has(X86_FEATURE_ARAT))
  873. return 0;
  874. /* This notifier should be called after workqueue is ready */
  875. ret = cpuhp_setup_state(CPUHP_AP_X86_HPET_ONLINE, "x86/hpet:online",
  876. hpet_cpuhp_online, NULL);
  877. if (ret)
  878. return ret;
  879. ret = cpuhp_setup_state(CPUHP_X86_HPET_DEAD, "x86/hpet:dead", NULL,
  880. hpet_cpuhp_dead);
  881. if (ret)
  882. goto err_cpuhp;
  883. return 0;
  884. err_cpuhp:
  885. cpuhp_remove_state(CPUHP_AP_X86_HPET_ONLINE);
  886. return ret;
  887. }
  888. fs_initcall(hpet_late_init);
  889. void hpet_disable(void)
  890. {
  891. if (is_hpet_capable() && hpet_virt_address) {
  892. unsigned int cfg = hpet_readl(HPET_CFG), id, last;
  893. if (hpet_boot_cfg)
  894. cfg = *hpet_boot_cfg;
  895. else if (hpet_legacy_int_enabled) {
  896. cfg &= ~HPET_CFG_LEGACY;
  897. hpet_legacy_int_enabled = false;
  898. }
  899. cfg &= ~HPET_CFG_ENABLE;
  900. hpet_writel(cfg, HPET_CFG);
  901. if (!hpet_boot_cfg)
  902. return;
  903. id = hpet_readl(HPET_ID);
  904. last = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
  905. for (id = 0; id <= last; ++id)
  906. hpet_writel(hpet_boot_cfg[id + 1], HPET_Tn_CFG(id));
  907. if (*hpet_boot_cfg & HPET_CFG_ENABLE)
  908. hpet_writel(*hpet_boot_cfg, HPET_CFG);
  909. }
  910. }
  911. #ifdef CONFIG_HPET_EMULATE_RTC
  912. /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
  913. * is enabled, we support RTC interrupt functionality in software.
  914. * RTC has 3 kinds of interrupts:
  915. * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
  916. * is updated
  917. * 2) Alarm Interrupt - generate an interrupt at a specific time of day
  918. * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
  919. * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
  920. * (1) and (2) above are implemented using polling at a frequency of
  921. * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
  922. * overhead. (DEFAULT_RTC_INT_FREQ)
  923. * For (3), we use interrupts at 64Hz or user specified periodic
  924. * frequency, whichever is higher.
  925. */
  926. #include <linux/mc146818rtc.h>
  927. #include <linux/rtc.h>
  928. #define DEFAULT_RTC_INT_FREQ 64
  929. #define DEFAULT_RTC_SHIFT 6
  930. #define RTC_NUM_INTS 1
  931. static unsigned long hpet_rtc_flags;
  932. static int hpet_prev_update_sec;
  933. static struct rtc_time hpet_alarm_time;
  934. static unsigned long hpet_pie_count;
  935. static u32 hpet_t1_cmp;
  936. static u32 hpet_default_delta;
  937. static u32 hpet_pie_delta;
  938. static unsigned long hpet_pie_limit;
  939. static rtc_irq_handler irq_handler;
  940. /*
  941. * Check that the hpet counter c1 is ahead of the c2
  942. */
  943. static inline int hpet_cnt_ahead(u32 c1, u32 c2)
  944. {
  945. return (s32)(c2 - c1) < 0;
  946. }
  947. /*
  948. * Registers a IRQ handler.
  949. */
  950. int hpet_register_irq_handler(rtc_irq_handler handler)
  951. {
  952. if (!is_hpet_enabled())
  953. return -ENODEV;
  954. if (irq_handler)
  955. return -EBUSY;
  956. irq_handler = handler;
  957. return 0;
  958. }
  959. EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
  960. /*
  961. * Deregisters the IRQ handler registered with hpet_register_irq_handler()
  962. * and does cleanup.
  963. */
  964. void hpet_unregister_irq_handler(rtc_irq_handler handler)
  965. {
  966. if (!is_hpet_enabled())
  967. return;
  968. irq_handler = NULL;
  969. hpet_rtc_flags = 0;
  970. }
  971. EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
  972. /*
  973. * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
  974. * is not supported by all HPET implementations for timer 1.
  975. *
  976. * hpet_rtc_timer_init() is called when the rtc is initialized.
  977. */
  978. int hpet_rtc_timer_init(void)
  979. {
  980. unsigned int cfg, cnt, delta;
  981. unsigned long flags;
  982. if (!is_hpet_enabled())
  983. return 0;
  984. if (!hpet_default_delta) {
  985. uint64_t clc;
  986. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  987. clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
  988. hpet_default_delta = clc;
  989. }
  990. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  991. delta = hpet_default_delta;
  992. else
  993. delta = hpet_pie_delta;
  994. local_irq_save(flags);
  995. cnt = delta + hpet_readl(HPET_COUNTER);
  996. hpet_writel(cnt, HPET_T1_CMP);
  997. hpet_t1_cmp = cnt;
  998. cfg = hpet_readl(HPET_T1_CFG);
  999. cfg &= ~HPET_TN_PERIODIC;
  1000. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  1001. hpet_writel(cfg, HPET_T1_CFG);
  1002. local_irq_restore(flags);
  1003. return 1;
  1004. }
  1005. EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
  1006. static void hpet_disable_rtc_channel(void)
  1007. {
  1008. u32 cfg = hpet_readl(HPET_T1_CFG);
  1009. cfg &= ~HPET_TN_ENABLE;
  1010. hpet_writel(cfg, HPET_T1_CFG);
  1011. }
  1012. /*
  1013. * The functions below are called from rtc driver.
  1014. * Return 0 if HPET is not being used.
  1015. * Otherwise do the necessary changes and return 1.
  1016. */
  1017. int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
  1018. {
  1019. if (!is_hpet_enabled())
  1020. return 0;
  1021. hpet_rtc_flags &= ~bit_mask;
  1022. if (unlikely(!hpet_rtc_flags))
  1023. hpet_disable_rtc_channel();
  1024. return 1;
  1025. }
  1026. EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
  1027. int hpet_set_rtc_irq_bit(unsigned long bit_mask)
  1028. {
  1029. unsigned long oldbits = hpet_rtc_flags;
  1030. if (!is_hpet_enabled())
  1031. return 0;
  1032. hpet_rtc_flags |= bit_mask;
  1033. if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
  1034. hpet_prev_update_sec = -1;
  1035. if (!oldbits)
  1036. hpet_rtc_timer_init();
  1037. return 1;
  1038. }
  1039. EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
  1040. int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
  1041. unsigned char sec)
  1042. {
  1043. if (!is_hpet_enabled())
  1044. return 0;
  1045. hpet_alarm_time.tm_hour = hrs;
  1046. hpet_alarm_time.tm_min = min;
  1047. hpet_alarm_time.tm_sec = sec;
  1048. return 1;
  1049. }
  1050. EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
  1051. int hpet_set_periodic_freq(unsigned long freq)
  1052. {
  1053. uint64_t clc;
  1054. if (!is_hpet_enabled())
  1055. return 0;
  1056. if (freq <= DEFAULT_RTC_INT_FREQ)
  1057. hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
  1058. else {
  1059. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  1060. do_div(clc, freq);
  1061. clc >>= hpet_clockevent.shift;
  1062. hpet_pie_delta = clc;
  1063. hpet_pie_limit = 0;
  1064. }
  1065. return 1;
  1066. }
  1067. EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
  1068. int hpet_rtc_dropped_irq(void)
  1069. {
  1070. return is_hpet_enabled();
  1071. }
  1072. EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
  1073. static void hpet_rtc_timer_reinit(void)
  1074. {
  1075. unsigned int delta;
  1076. int lost_ints = -1;
  1077. if (unlikely(!hpet_rtc_flags))
  1078. hpet_disable_rtc_channel();
  1079. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  1080. delta = hpet_default_delta;
  1081. else
  1082. delta = hpet_pie_delta;
  1083. /*
  1084. * Increment the comparator value until we are ahead of the
  1085. * current count.
  1086. */
  1087. do {
  1088. hpet_t1_cmp += delta;
  1089. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  1090. lost_ints++;
  1091. } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
  1092. if (lost_ints) {
  1093. if (hpet_rtc_flags & RTC_PIE)
  1094. hpet_pie_count += lost_ints;
  1095. if (printk_ratelimit())
  1096. printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
  1097. lost_ints);
  1098. }
  1099. }
  1100. irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
  1101. {
  1102. struct rtc_time curr_time;
  1103. unsigned long rtc_int_flag = 0;
  1104. hpet_rtc_timer_reinit();
  1105. memset(&curr_time, 0, sizeof(struct rtc_time));
  1106. if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
  1107. mc146818_get_time(&curr_time);
  1108. if (hpet_rtc_flags & RTC_UIE &&
  1109. curr_time.tm_sec != hpet_prev_update_sec) {
  1110. if (hpet_prev_update_sec >= 0)
  1111. rtc_int_flag = RTC_UF;
  1112. hpet_prev_update_sec = curr_time.tm_sec;
  1113. }
  1114. if (hpet_rtc_flags & RTC_PIE &&
  1115. ++hpet_pie_count >= hpet_pie_limit) {
  1116. rtc_int_flag |= RTC_PF;
  1117. hpet_pie_count = 0;
  1118. }
  1119. if (hpet_rtc_flags & RTC_AIE &&
  1120. (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
  1121. (curr_time.tm_min == hpet_alarm_time.tm_min) &&
  1122. (curr_time.tm_hour == hpet_alarm_time.tm_hour))
  1123. rtc_int_flag |= RTC_AF;
  1124. if (rtc_int_flag) {
  1125. rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
  1126. if (irq_handler)
  1127. irq_handler(rtc_int_flag, dev_id);
  1128. }
  1129. return IRQ_HANDLED;
  1130. }
  1131. EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
  1132. #endif