hpet.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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(boot_cpu_data.cpu_index);
  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)
  292. {
  293. hpet_enable_legacy_int();
  294. hpet_print_config();
  295. return 0;
  296. }
  297. static int hpet_next_event(unsigned long delta,
  298. struct clock_event_device *evt, int timer)
  299. {
  300. u32 cnt;
  301. s32 res;
  302. cnt = hpet_readl(HPET_COUNTER);
  303. cnt += (u32) delta;
  304. hpet_writel(cnt, HPET_Tn_CMP(timer));
  305. /*
  306. * HPETs are a complete disaster. The compare register is
  307. * based on a equal comparison and neither provides a less
  308. * than or equal functionality (which would require to take
  309. * the wraparound into account) nor a simple count down event
  310. * mode. Further the write to the comparator register is
  311. * delayed internally up to two HPET clock cycles in certain
  312. * chipsets (ATI, ICH9,10). Some newer AMD chipsets have even
  313. * longer delays. We worked around that by reading back the
  314. * compare register, but that required another workaround for
  315. * ICH9,10 chips where the first readout after write can
  316. * return the old stale value. We already had a minimum
  317. * programming delta of 5us enforced, but a NMI or SMI hitting
  318. * between the counter readout and the comparator write can
  319. * move us behind that point easily. Now instead of reading
  320. * the compare register back several times, we make the ETIME
  321. * decision based on the following: Return ETIME if the
  322. * counter value after the write is less than HPET_MIN_CYCLES
  323. * away from the event or if the counter is already ahead of
  324. * the event. The minimum programming delta for the generic
  325. * clockevents code is set to 1.5 * HPET_MIN_CYCLES.
  326. */
  327. res = (s32)(cnt - hpet_readl(HPET_COUNTER));
  328. return res < HPET_MIN_CYCLES ? -ETIME : 0;
  329. }
  330. static int hpet_legacy_shutdown(struct clock_event_device *evt)
  331. {
  332. return hpet_shutdown(evt, 0);
  333. }
  334. static int hpet_legacy_set_oneshot(struct clock_event_device *evt)
  335. {
  336. return hpet_set_oneshot(evt, 0);
  337. }
  338. static int hpet_legacy_set_periodic(struct clock_event_device *evt)
  339. {
  340. return hpet_set_periodic(evt, 0);
  341. }
  342. static int hpet_legacy_resume(struct clock_event_device *evt)
  343. {
  344. return hpet_resume(evt);
  345. }
  346. static int hpet_legacy_next_event(unsigned long delta,
  347. struct clock_event_device *evt)
  348. {
  349. return hpet_next_event(delta, evt, 0);
  350. }
  351. /*
  352. * The hpet clock event device
  353. */
  354. static struct clock_event_device hpet_clockevent = {
  355. .name = "hpet",
  356. .features = CLOCK_EVT_FEAT_PERIODIC |
  357. CLOCK_EVT_FEAT_ONESHOT,
  358. .set_state_periodic = hpet_legacy_set_periodic,
  359. .set_state_oneshot = hpet_legacy_set_oneshot,
  360. .set_state_shutdown = hpet_legacy_shutdown,
  361. .tick_resume = hpet_legacy_resume,
  362. .set_next_event = hpet_legacy_next_event,
  363. .irq = 0,
  364. .rating = 50,
  365. };
  366. /*
  367. * HPET MSI Support
  368. */
  369. #ifdef CONFIG_PCI_MSI
  370. static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
  371. static struct hpet_dev *hpet_devs;
  372. static struct irq_domain *hpet_domain;
  373. void hpet_msi_unmask(struct irq_data *data)
  374. {
  375. struct hpet_dev *hdev = irq_data_get_irq_handler_data(data);
  376. unsigned int cfg;
  377. /* unmask it */
  378. cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
  379. cfg |= HPET_TN_ENABLE | HPET_TN_FSB;
  380. hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
  381. }
  382. void hpet_msi_mask(struct irq_data *data)
  383. {
  384. struct hpet_dev *hdev = irq_data_get_irq_handler_data(data);
  385. unsigned int cfg;
  386. /* mask it */
  387. cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
  388. cfg &= ~(HPET_TN_ENABLE | HPET_TN_FSB);
  389. hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
  390. }
  391. void hpet_msi_write(struct hpet_dev *hdev, struct msi_msg *msg)
  392. {
  393. hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
  394. hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
  395. }
  396. void hpet_msi_read(struct hpet_dev *hdev, struct msi_msg *msg)
  397. {
  398. msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
  399. msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
  400. msg->address_hi = 0;
  401. }
  402. static int hpet_msi_shutdown(struct clock_event_device *evt)
  403. {
  404. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  405. return hpet_shutdown(evt, hdev->num);
  406. }
  407. static int hpet_msi_set_oneshot(struct clock_event_device *evt)
  408. {
  409. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  410. return hpet_set_oneshot(evt, hdev->num);
  411. }
  412. static int hpet_msi_set_periodic(struct clock_event_device *evt)
  413. {
  414. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  415. return hpet_set_periodic(evt, hdev->num);
  416. }
  417. static int hpet_msi_resume(struct clock_event_device *evt)
  418. {
  419. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  420. struct irq_data *data = irq_get_irq_data(hdev->irq);
  421. struct msi_msg msg;
  422. /* Restore the MSI msg and unmask the interrupt */
  423. irq_chip_compose_msi_msg(data, &msg);
  424. hpet_msi_write(hdev, &msg);
  425. hpet_msi_unmask(data);
  426. return 0;
  427. }
  428. static int hpet_msi_next_event(unsigned long delta,
  429. struct clock_event_device *evt)
  430. {
  431. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  432. return hpet_next_event(delta, evt, hdev->num);
  433. }
  434. static irqreturn_t hpet_interrupt_handler(int irq, void *data)
  435. {
  436. struct hpet_dev *dev = (struct hpet_dev *)data;
  437. struct clock_event_device *hevt = &dev->evt;
  438. if (!hevt->event_handler) {
  439. printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
  440. dev->num);
  441. return IRQ_HANDLED;
  442. }
  443. hevt->event_handler(hevt);
  444. return IRQ_HANDLED;
  445. }
  446. static int hpet_setup_irq(struct hpet_dev *dev)
  447. {
  448. if (request_irq(dev->irq, hpet_interrupt_handler,
  449. IRQF_TIMER | IRQF_NOBALANCING,
  450. dev->name, dev))
  451. return -1;
  452. disable_irq(dev->irq);
  453. irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
  454. enable_irq(dev->irq);
  455. printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
  456. dev->name, dev->irq);
  457. return 0;
  458. }
  459. /* This should be called in specific @cpu */
  460. static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
  461. {
  462. struct clock_event_device *evt = &hdev->evt;
  463. WARN_ON(cpu != smp_processor_id());
  464. if (!(hdev->flags & HPET_DEV_VALID))
  465. return;
  466. hdev->cpu = cpu;
  467. per_cpu(cpu_hpet_dev, cpu) = hdev;
  468. evt->name = hdev->name;
  469. hpet_setup_irq(hdev);
  470. evt->irq = hdev->irq;
  471. evt->rating = 110;
  472. evt->features = CLOCK_EVT_FEAT_ONESHOT;
  473. if (hdev->flags & HPET_DEV_PERI_CAP) {
  474. evt->features |= CLOCK_EVT_FEAT_PERIODIC;
  475. evt->set_state_periodic = hpet_msi_set_periodic;
  476. }
  477. evt->set_state_shutdown = hpet_msi_shutdown;
  478. evt->set_state_oneshot = hpet_msi_set_oneshot;
  479. evt->tick_resume = hpet_msi_resume;
  480. evt->set_next_event = hpet_msi_next_event;
  481. evt->cpumask = cpumask_of(hdev->cpu);
  482. clockevents_config_and_register(evt, hpet_freq, HPET_MIN_PROG_DELTA,
  483. 0x7FFFFFFF);
  484. }
  485. #ifdef CONFIG_HPET
  486. /* Reserve at least one timer for userspace (/dev/hpet) */
  487. #define RESERVE_TIMERS 1
  488. #else
  489. #define RESERVE_TIMERS 0
  490. #endif
  491. static void hpet_msi_capability_lookup(unsigned int start_timer)
  492. {
  493. unsigned int id;
  494. unsigned int num_timers;
  495. unsigned int num_timers_used = 0;
  496. int i, irq;
  497. if (hpet_msi_disable)
  498. return;
  499. if (boot_cpu_has(X86_FEATURE_ARAT))
  500. return;
  501. id = hpet_readl(HPET_ID);
  502. num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
  503. num_timers++; /* Value read out starts from 0 */
  504. hpet_print_config();
  505. hpet_domain = hpet_create_irq_domain(hpet_blockid);
  506. if (!hpet_domain)
  507. return;
  508. hpet_devs = kcalloc(num_timers, sizeof(struct hpet_dev), GFP_KERNEL);
  509. if (!hpet_devs)
  510. return;
  511. hpet_num_timers = num_timers;
  512. for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
  513. struct hpet_dev *hdev = &hpet_devs[num_timers_used];
  514. unsigned int cfg = hpet_readl(HPET_Tn_CFG(i));
  515. /* Only consider HPET timer with MSI support */
  516. if (!(cfg & HPET_TN_FSB_CAP))
  517. continue;
  518. hdev->flags = 0;
  519. if (cfg & HPET_TN_PERIODIC_CAP)
  520. hdev->flags |= HPET_DEV_PERI_CAP;
  521. sprintf(hdev->name, "hpet%d", i);
  522. hdev->num = i;
  523. irq = hpet_assign_irq(hpet_domain, hdev, hdev->num);
  524. if (irq <= 0)
  525. continue;
  526. hdev->irq = irq;
  527. hdev->flags |= HPET_DEV_FSB_CAP;
  528. hdev->flags |= HPET_DEV_VALID;
  529. num_timers_used++;
  530. if (num_timers_used == num_possible_cpus())
  531. break;
  532. }
  533. printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
  534. num_timers, num_timers_used);
  535. }
  536. #ifdef CONFIG_HPET
  537. static void hpet_reserve_msi_timers(struct hpet_data *hd)
  538. {
  539. int i;
  540. if (!hpet_devs)
  541. return;
  542. for (i = 0; i < hpet_num_timers; i++) {
  543. struct hpet_dev *hdev = &hpet_devs[i];
  544. if (!(hdev->flags & HPET_DEV_VALID))
  545. continue;
  546. hd->hd_irq[hdev->num] = hdev->irq;
  547. hpet_reserve_timer(hd, hdev->num);
  548. }
  549. }
  550. #endif
  551. static struct hpet_dev *hpet_get_unused_timer(void)
  552. {
  553. int i;
  554. if (!hpet_devs)
  555. return NULL;
  556. for (i = 0; i < hpet_num_timers; i++) {
  557. struct hpet_dev *hdev = &hpet_devs[i];
  558. if (!(hdev->flags & HPET_DEV_VALID))
  559. continue;
  560. if (test_and_set_bit(HPET_DEV_USED_BIT,
  561. (unsigned long *)&hdev->flags))
  562. continue;
  563. return hdev;
  564. }
  565. return NULL;
  566. }
  567. struct hpet_work_struct {
  568. struct delayed_work work;
  569. struct completion complete;
  570. };
  571. static void hpet_work(struct work_struct *w)
  572. {
  573. struct hpet_dev *hdev;
  574. int cpu = smp_processor_id();
  575. struct hpet_work_struct *hpet_work;
  576. hpet_work = container_of(w, struct hpet_work_struct, work.work);
  577. hdev = hpet_get_unused_timer();
  578. if (hdev)
  579. init_one_hpet_msi_clockevent(hdev, cpu);
  580. complete(&hpet_work->complete);
  581. }
  582. static int hpet_cpuhp_online(unsigned int cpu)
  583. {
  584. struct hpet_work_struct work;
  585. INIT_DELAYED_WORK_ONSTACK(&work.work, hpet_work);
  586. init_completion(&work.complete);
  587. /* FIXME: add schedule_work_on() */
  588. schedule_delayed_work_on(cpu, &work.work, 0);
  589. wait_for_completion(&work.complete);
  590. destroy_delayed_work_on_stack(&work.work);
  591. return 0;
  592. }
  593. static int hpet_cpuhp_dead(unsigned int cpu)
  594. {
  595. struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
  596. if (!hdev)
  597. return 0;
  598. free_irq(hdev->irq, hdev);
  599. hdev->flags &= ~HPET_DEV_USED;
  600. per_cpu(cpu_hpet_dev, cpu) = NULL;
  601. return 0;
  602. }
  603. #else
  604. static void hpet_msi_capability_lookup(unsigned int start_timer)
  605. {
  606. return;
  607. }
  608. #ifdef CONFIG_HPET
  609. static void hpet_reserve_msi_timers(struct hpet_data *hd)
  610. {
  611. return;
  612. }
  613. #endif
  614. #define hpet_cpuhp_online NULL
  615. #define hpet_cpuhp_dead NULL
  616. #endif
  617. /*
  618. * Clock source related code
  619. */
  620. #if defined(CONFIG_SMP) && defined(CONFIG_64BIT)
  621. /*
  622. * Reading the HPET counter is a very slow operation. If a large number of
  623. * CPUs are trying to access the HPET counter simultaneously, it can cause
  624. * massive delay and slow down system performance dramatically. This may
  625. * happen when HPET is the default clock source instead of TSC. For a
  626. * really large system with hundreds of CPUs, the slowdown may be so
  627. * severe that it may actually crash the system because of a NMI watchdog
  628. * soft lockup, for example.
  629. *
  630. * If multiple CPUs are trying to access the HPET counter at the same time,
  631. * we don't actually need to read the counter multiple times. Instead, the
  632. * other CPUs can use the counter value read by the first CPU in the group.
  633. *
  634. * This special feature is only enabled on x86-64 systems. It is unlikely
  635. * that 32-bit x86 systems will have enough CPUs to require this feature
  636. * with its associated locking overhead. And we also need 64-bit atomic
  637. * read.
  638. *
  639. * The lock and the hpet value are stored together and can be read in a
  640. * single atomic 64-bit read. It is explicitly assumed that arch_spinlock_t
  641. * is 32 bits in size.
  642. */
  643. union hpet_lock {
  644. struct {
  645. arch_spinlock_t lock;
  646. u32 value;
  647. };
  648. u64 lockval;
  649. };
  650. static union hpet_lock hpet __cacheline_aligned = {
  651. { .lock = __ARCH_SPIN_LOCK_UNLOCKED, },
  652. };
  653. static u64 read_hpet(struct clocksource *cs)
  654. {
  655. unsigned long flags;
  656. union hpet_lock old, new;
  657. BUILD_BUG_ON(sizeof(union hpet_lock) != 8);
  658. /*
  659. * Read HPET directly if in NMI.
  660. */
  661. if (in_nmi())
  662. return (u64)hpet_readl(HPET_COUNTER);
  663. /*
  664. * Read the current state of the lock and HPET value atomically.
  665. */
  666. old.lockval = READ_ONCE(hpet.lockval);
  667. if (arch_spin_is_locked(&old.lock))
  668. goto contended;
  669. local_irq_save(flags);
  670. if (arch_spin_trylock(&hpet.lock)) {
  671. new.value = hpet_readl(HPET_COUNTER);
  672. /*
  673. * Use WRITE_ONCE() to prevent store tearing.
  674. */
  675. WRITE_ONCE(hpet.value, new.value);
  676. arch_spin_unlock(&hpet.lock);
  677. local_irq_restore(flags);
  678. return (u64)new.value;
  679. }
  680. local_irq_restore(flags);
  681. contended:
  682. /*
  683. * Contended case
  684. * --------------
  685. * Wait until the HPET value change or the lock is free to indicate
  686. * its value is up-to-date.
  687. *
  688. * It is possible that old.value has already contained the latest
  689. * HPET value while the lock holder was in the process of releasing
  690. * the lock. Checking for lock state change will enable us to return
  691. * the value immediately instead of waiting for the next HPET reader
  692. * to come along.
  693. */
  694. do {
  695. cpu_relax();
  696. new.lockval = READ_ONCE(hpet.lockval);
  697. } while ((new.value == old.value) && arch_spin_is_locked(&new.lock));
  698. return (u64)new.value;
  699. }
  700. #else
  701. /*
  702. * For UP or 32-bit.
  703. */
  704. static u64 read_hpet(struct clocksource *cs)
  705. {
  706. return (u64)hpet_readl(HPET_COUNTER);
  707. }
  708. #endif
  709. static struct clocksource clocksource_hpet = {
  710. .name = "hpet",
  711. .rating = 250,
  712. .read = read_hpet,
  713. .mask = HPET_MASK,
  714. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  715. .resume = hpet_resume_counter,
  716. };
  717. static int hpet_clocksource_register(void)
  718. {
  719. u64 start, now;
  720. u64 t1;
  721. /* Start the counter */
  722. hpet_restart_counter();
  723. /* Verify whether hpet counter works */
  724. t1 = hpet_readl(HPET_COUNTER);
  725. start = rdtsc();
  726. /*
  727. * We don't know the TSC frequency yet, but waiting for
  728. * 200000 TSC cycles is safe:
  729. * 4 GHz == 50us
  730. * 1 GHz == 200us
  731. */
  732. do {
  733. rep_nop();
  734. now = rdtsc();
  735. } while ((now - start) < 200000UL);
  736. if (t1 == hpet_readl(HPET_COUNTER)) {
  737. printk(KERN_WARNING
  738. "HPET counter not counting. HPET disabled\n");
  739. return -ENODEV;
  740. }
  741. clocksource_register_hz(&clocksource_hpet, (u32)hpet_freq);
  742. return 0;
  743. }
  744. static u32 *hpet_boot_cfg;
  745. /**
  746. * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
  747. */
  748. int __init hpet_enable(void)
  749. {
  750. u32 hpet_period, cfg, id;
  751. u64 freq;
  752. unsigned int i, last;
  753. if (!is_hpet_capable())
  754. return 0;
  755. hpet_set_mapping();
  756. /*
  757. * Read the period and check for a sane value:
  758. */
  759. hpet_period = hpet_readl(HPET_PERIOD);
  760. /*
  761. * AMD SB700 based systems with spread spectrum enabled use a
  762. * SMM based HPET emulation to provide proper frequency
  763. * setting. The SMM code is initialized with the first HPET
  764. * register access and takes some time to complete. During
  765. * this time the config register reads 0xffffffff. We check
  766. * for max. 1000 loops whether the config register reads a non
  767. * 0xffffffff value to make sure that HPET is up and running
  768. * before we go further. A counting loop is safe, as the HPET
  769. * access takes thousands of CPU cycles. On non SB700 based
  770. * machines this check is only done once and has no side
  771. * effects.
  772. */
  773. for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
  774. if (i == 1000) {
  775. printk(KERN_WARNING
  776. "HPET config register value = 0xFFFFFFFF. "
  777. "Disabling HPET\n");
  778. goto out_nohpet;
  779. }
  780. }
  781. if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
  782. goto out_nohpet;
  783. /*
  784. * The period is a femto seconds value. Convert it to a
  785. * frequency.
  786. */
  787. freq = FSEC_PER_SEC;
  788. do_div(freq, hpet_period);
  789. hpet_freq = freq;
  790. /*
  791. * Read the HPET ID register to retrieve the IRQ routing
  792. * information and the number of channels
  793. */
  794. id = hpet_readl(HPET_ID);
  795. hpet_print_config();
  796. last = (id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT;
  797. #ifdef CONFIG_HPET_EMULATE_RTC
  798. /*
  799. * The legacy routing mode needs at least two channels, tick timer
  800. * and the rtc emulation channel.
  801. */
  802. if (!last)
  803. goto out_nohpet;
  804. #endif
  805. cfg = hpet_readl(HPET_CFG);
  806. hpet_boot_cfg = kmalloc_array(last + 2, sizeof(*hpet_boot_cfg),
  807. GFP_KERNEL);
  808. if (hpet_boot_cfg)
  809. *hpet_boot_cfg = cfg;
  810. else
  811. pr_warn("HPET initial state will not be saved\n");
  812. cfg &= ~(HPET_CFG_ENABLE | HPET_CFG_LEGACY);
  813. hpet_writel(cfg, HPET_CFG);
  814. if (cfg)
  815. pr_warn("Unrecognized bits %#x set in global cfg\n", cfg);
  816. for (i = 0; i <= last; ++i) {
  817. cfg = hpet_readl(HPET_Tn_CFG(i));
  818. if (hpet_boot_cfg)
  819. hpet_boot_cfg[i + 1] = cfg;
  820. cfg &= ~(HPET_TN_ENABLE | HPET_TN_LEVEL | HPET_TN_FSB);
  821. hpet_writel(cfg, HPET_Tn_CFG(i));
  822. cfg &= ~(HPET_TN_PERIODIC | HPET_TN_PERIODIC_CAP
  823. | HPET_TN_64BIT_CAP | HPET_TN_32BIT | HPET_TN_ROUTE
  824. | HPET_TN_FSB | HPET_TN_FSB_CAP);
  825. if (cfg)
  826. pr_warn("Unrecognized bits %#x set in cfg#%u\n",
  827. cfg, i);
  828. }
  829. hpet_print_config();
  830. if (hpet_clocksource_register())
  831. goto out_nohpet;
  832. if (id & HPET_ID_LEGSUP) {
  833. hpet_legacy_clockevent_register();
  834. return 1;
  835. }
  836. return 0;
  837. out_nohpet:
  838. hpet_clear_mapping();
  839. hpet_address = 0;
  840. return 0;
  841. }
  842. /*
  843. * Needs to be late, as the reserve_timer code calls kalloc !
  844. *
  845. * Not a problem on i386 as hpet_enable is called from late_time_init,
  846. * but on x86_64 it is necessary !
  847. */
  848. static __init int hpet_late_init(void)
  849. {
  850. int ret;
  851. if (boot_hpet_disable)
  852. return -ENODEV;
  853. if (!hpet_address) {
  854. if (!force_hpet_address)
  855. return -ENODEV;
  856. hpet_address = force_hpet_address;
  857. hpet_enable();
  858. }
  859. if (!hpet_virt_address)
  860. return -ENODEV;
  861. if (hpet_readl(HPET_ID) & HPET_ID_LEGSUP)
  862. hpet_msi_capability_lookup(2);
  863. else
  864. hpet_msi_capability_lookup(0);
  865. hpet_reserve_platform_timers(hpet_readl(HPET_ID));
  866. hpet_print_config();
  867. if (hpet_msi_disable)
  868. return 0;
  869. if (boot_cpu_has(X86_FEATURE_ARAT))
  870. return 0;
  871. /* This notifier should be called after workqueue is ready */
  872. ret = cpuhp_setup_state(CPUHP_AP_X86_HPET_ONLINE, "x86/hpet:online",
  873. hpet_cpuhp_online, NULL);
  874. if (ret)
  875. return ret;
  876. ret = cpuhp_setup_state(CPUHP_X86_HPET_DEAD, "x86/hpet:dead", NULL,
  877. hpet_cpuhp_dead);
  878. if (ret)
  879. goto err_cpuhp;
  880. return 0;
  881. err_cpuhp:
  882. cpuhp_remove_state(CPUHP_AP_X86_HPET_ONLINE);
  883. return ret;
  884. }
  885. fs_initcall(hpet_late_init);
  886. void hpet_disable(void)
  887. {
  888. if (is_hpet_capable() && hpet_virt_address) {
  889. unsigned int cfg = hpet_readl(HPET_CFG), id, last;
  890. if (hpet_boot_cfg)
  891. cfg = *hpet_boot_cfg;
  892. else if (hpet_legacy_int_enabled) {
  893. cfg &= ~HPET_CFG_LEGACY;
  894. hpet_legacy_int_enabled = false;
  895. }
  896. cfg &= ~HPET_CFG_ENABLE;
  897. hpet_writel(cfg, HPET_CFG);
  898. if (!hpet_boot_cfg)
  899. return;
  900. id = hpet_readl(HPET_ID);
  901. last = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
  902. for (id = 0; id <= last; ++id)
  903. hpet_writel(hpet_boot_cfg[id + 1], HPET_Tn_CFG(id));
  904. if (*hpet_boot_cfg & HPET_CFG_ENABLE)
  905. hpet_writel(*hpet_boot_cfg, HPET_CFG);
  906. }
  907. }
  908. #ifdef CONFIG_HPET_EMULATE_RTC
  909. /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
  910. * is enabled, we support RTC interrupt functionality in software.
  911. * RTC has 3 kinds of interrupts:
  912. * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
  913. * is updated
  914. * 2) Alarm Interrupt - generate an interrupt at a specific time of day
  915. * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
  916. * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
  917. * (1) and (2) above are implemented using polling at a frequency of
  918. * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
  919. * overhead. (DEFAULT_RTC_INT_FREQ)
  920. * For (3), we use interrupts at 64Hz or user specified periodic
  921. * frequency, whichever is higher.
  922. */
  923. #include <linux/mc146818rtc.h>
  924. #include <linux/rtc.h>
  925. #define DEFAULT_RTC_INT_FREQ 64
  926. #define DEFAULT_RTC_SHIFT 6
  927. #define RTC_NUM_INTS 1
  928. static unsigned long hpet_rtc_flags;
  929. static int hpet_prev_update_sec;
  930. static struct rtc_time hpet_alarm_time;
  931. static unsigned long hpet_pie_count;
  932. static u32 hpet_t1_cmp;
  933. static u32 hpet_default_delta;
  934. static u32 hpet_pie_delta;
  935. static unsigned long hpet_pie_limit;
  936. static rtc_irq_handler irq_handler;
  937. /*
  938. * Check that the hpet counter c1 is ahead of the c2
  939. */
  940. static inline int hpet_cnt_ahead(u32 c1, u32 c2)
  941. {
  942. return (s32)(c2 - c1) < 0;
  943. }
  944. /*
  945. * Registers a IRQ handler.
  946. */
  947. int hpet_register_irq_handler(rtc_irq_handler handler)
  948. {
  949. if (!is_hpet_enabled())
  950. return -ENODEV;
  951. if (irq_handler)
  952. return -EBUSY;
  953. irq_handler = handler;
  954. return 0;
  955. }
  956. EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
  957. /*
  958. * Deregisters the IRQ handler registered with hpet_register_irq_handler()
  959. * and does cleanup.
  960. */
  961. void hpet_unregister_irq_handler(rtc_irq_handler handler)
  962. {
  963. if (!is_hpet_enabled())
  964. return;
  965. irq_handler = NULL;
  966. hpet_rtc_flags = 0;
  967. }
  968. EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
  969. /*
  970. * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
  971. * is not supported by all HPET implementations for timer 1.
  972. *
  973. * hpet_rtc_timer_init() is called when the rtc is initialized.
  974. */
  975. int hpet_rtc_timer_init(void)
  976. {
  977. unsigned int cfg, cnt, delta;
  978. unsigned long flags;
  979. if (!is_hpet_enabled())
  980. return 0;
  981. if (!hpet_default_delta) {
  982. uint64_t clc;
  983. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  984. clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
  985. hpet_default_delta = clc;
  986. }
  987. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  988. delta = hpet_default_delta;
  989. else
  990. delta = hpet_pie_delta;
  991. local_irq_save(flags);
  992. cnt = delta + hpet_readl(HPET_COUNTER);
  993. hpet_writel(cnt, HPET_T1_CMP);
  994. hpet_t1_cmp = cnt;
  995. cfg = hpet_readl(HPET_T1_CFG);
  996. cfg &= ~HPET_TN_PERIODIC;
  997. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  998. hpet_writel(cfg, HPET_T1_CFG);
  999. local_irq_restore(flags);
  1000. return 1;
  1001. }
  1002. EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
  1003. static void hpet_disable_rtc_channel(void)
  1004. {
  1005. u32 cfg = hpet_readl(HPET_T1_CFG);
  1006. cfg &= ~HPET_TN_ENABLE;
  1007. hpet_writel(cfg, HPET_T1_CFG);
  1008. }
  1009. /*
  1010. * The functions below are called from rtc driver.
  1011. * Return 0 if HPET is not being used.
  1012. * Otherwise do the necessary changes and return 1.
  1013. */
  1014. int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
  1015. {
  1016. if (!is_hpet_enabled())
  1017. return 0;
  1018. hpet_rtc_flags &= ~bit_mask;
  1019. if (unlikely(!hpet_rtc_flags))
  1020. hpet_disable_rtc_channel();
  1021. return 1;
  1022. }
  1023. EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
  1024. int hpet_set_rtc_irq_bit(unsigned long bit_mask)
  1025. {
  1026. unsigned long oldbits = hpet_rtc_flags;
  1027. if (!is_hpet_enabled())
  1028. return 0;
  1029. hpet_rtc_flags |= bit_mask;
  1030. if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
  1031. hpet_prev_update_sec = -1;
  1032. if (!oldbits)
  1033. hpet_rtc_timer_init();
  1034. return 1;
  1035. }
  1036. EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
  1037. int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
  1038. unsigned char sec)
  1039. {
  1040. if (!is_hpet_enabled())
  1041. return 0;
  1042. hpet_alarm_time.tm_hour = hrs;
  1043. hpet_alarm_time.tm_min = min;
  1044. hpet_alarm_time.tm_sec = sec;
  1045. return 1;
  1046. }
  1047. EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
  1048. int hpet_set_periodic_freq(unsigned long freq)
  1049. {
  1050. uint64_t clc;
  1051. if (!is_hpet_enabled())
  1052. return 0;
  1053. if (freq <= DEFAULT_RTC_INT_FREQ)
  1054. hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
  1055. else {
  1056. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  1057. do_div(clc, freq);
  1058. clc >>= hpet_clockevent.shift;
  1059. hpet_pie_delta = clc;
  1060. hpet_pie_limit = 0;
  1061. }
  1062. return 1;
  1063. }
  1064. EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
  1065. int hpet_rtc_dropped_irq(void)
  1066. {
  1067. return is_hpet_enabled();
  1068. }
  1069. EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
  1070. static void hpet_rtc_timer_reinit(void)
  1071. {
  1072. unsigned int delta;
  1073. int lost_ints = -1;
  1074. if (unlikely(!hpet_rtc_flags))
  1075. hpet_disable_rtc_channel();
  1076. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  1077. delta = hpet_default_delta;
  1078. else
  1079. delta = hpet_pie_delta;
  1080. /*
  1081. * Increment the comparator value until we are ahead of the
  1082. * current count.
  1083. */
  1084. do {
  1085. hpet_t1_cmp += delta;
  1086. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  1087. lost_ints++;
  1088. } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
  1089. if (lost_ints) {
  1090. if (hpet_rtc_flags & RTC_PIE)
  1091. hpet_pie_count += lost_ints;
  1092. if (printk_ratelimit())
  1093. printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
  1094. lost_ints);
  1095. }
  1096. }
  1097. irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
  1098. {
  1099. struct rtc_time curr_time;
  1100. unsigned long rtc_int_flag = 0;
  1101. hpet_rtc_timer_reinit();
  1102. memset(&curr_time, 0, sizeof(struct rtc_time));
  1103. if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
  1104. mc146818_get_time(&curr_time);
  1105. if (hpet_rtc_flags & RTC_UIE &&
  1106. curr_time.tm_sec != hpet_prev_update_sec) {
  1107. if (hpet_prev_update_sec >= 0)
  1108. rtc_int_flag = RTC_UF;
  1109. hpet_prev_update_sec = curr_time.tm_sec;
  1110. }
  1111. if (hpet_rtc_flags & RTC_PIE &&
  1112. ++hpet_pie_count >= hpet_pie_limit) {
  1113. rtc_int_flag |= RTC_PF;
  1114. hpet_pie_count = 0;
  1115. }
  1116. if (hpet_rtc_flags & RTC_AIE &&
  1117. (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
  1118. (curr_time.tm_min == hpet_alarm_time.tm_min) &&
  1119. (curr_time.tm_hour == hpet_alarm_time.tm_hour))
  1120. rtc_int_flag |= RTC_AF;
  1121. if (rtc_int_flag) {
  1122. rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
  1123. if (irq_handler)
  1124. irq_handler(rtc_int_flag, dev_id);
  1125. }
  1126. return IRQ_HANDLED;
  1127. }
  1128. EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
  1129. #endif