hpet.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. #include <linux/clocksource.h>
  2. #include <linux/clockchips.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/irq.h>
  5. #include <linux/export.h>
  6. #include <linux/delay.h>
  7. #include <linux/errno.h>
  8. #include <linux/i8253.h>
  9. #include <linux/slab.h>
  10. #include <linux/hpet.h>
  11. #include <linux/init.h>
  12. #include <linux/cpu.h>
  13. #include <linux/pm.h>
  14. #include <linux/io.h>
  15. #include <asm/cpufeature.h>
  16. #include <asm/irqdomain.h>
  17. #include <asm/fixmap.h>
  18. #include <asm/hpet.h>
  19. #include <asm/time.h>
  20. #define HPET_MASK CLOCKSOURCE_MASK(32)
  21. /* FSEC = 10^-15
  22. NSEC = 10^-9 */
  23. #define FSEC_PER_NSEC 1000000L
  24. #define HPET_DEV_USED_BIT 2
  25. #define HPET_DEV_USED (1 << HPET_DEV_USED_BIT)
  26. #define HPET_DEV_VALID 0x8
  27. #define HPET_DEV_FSB_CAP 0x1000
  28. #define HPET_DEV_PERI_CAP 0x2000
  29. #define HPET_MIN_CYCLES 128
  30. #define HPET_MIN_PROG_DELTA (HPET_MIN_CYCLES + (HPET_MIN_CYCLES >> 1))
  31. /*
  32. * HPET address is set in acpi/boot.c, when an ACPI entry exists
  33. */
  34. unsigned long hpet_address;
  35. u8 hpet_blockid; /* OS timer block num */
  36. bool hpet_msi_disable;
  37. #ifdef CONFIG_PCI_MSI
  38. static unsigned int hpet_num_timers;
  39. #endif
  40. static void __iomem *hpet_virt_address;
  41. struct hpet_dev {
  42. struct clock_event_device evt;
  43. unsigned int num;
  44. int cpu;
  45. unsigned int irq;
  46. unsigned int flags;
  47. char name[10];
  48. };
  49. static inline struct hpet_dev *EVT_TO_HPET_DEV(struct clock_event_device *evtdev)
  50. {
  51. return container_of(evtdev, struct hpet_dev, evt);
  52. }
  53. inline unsigned int hpet_readl(unsigned int a)
  54. {
  55. return readl(hpet_virt_address + a);
  56. }
  57. static inline void hpet_writel(unsigned int d, unsigned int a)
  58. {
  59. writel(d, hpet_virt_address + a);
  60. }
  61. #ifdef CONFIG_X86_64
  62. #include <asm/pgtable.h>
  63. #endif
  64. static inline void hpet_set_mapping(void)
  65. {
  66. hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
  67. }
  68. static inline void hpet_clear_mapping(void)
  69. {
  70. iounmap(hpet_virt_address);
  71. hpet_virt_address = NULL;
  72. }
  73. /*
  74. * HPET command line enable / disable
  75. */
  76. bool boot_hpet_disable;
  77. bool hpet_force_user;
  78. static bool hpet_verbose;
  79. static int __init hpet_setup(char *str)
  80. {
  81. while (str) {
  82. char *next = strchr(str, ',');
  83. if (next)
  84. *next++ = 0;
  85. if (!strncmp("disable", str, 7))
  86. boot_hpet_disable = true;
  87. if (!strncmp("force", str, 5))
  88. hpet_force_user = true;
  89. if (!strncmp("verbose", str, 7))
  90. hpet_verbose = true;
  91. str = next;
  92. }
  93. return 1;
  94. }
  95. __setup("hpet=", hpet_setup);
  96. static int __init disable_hpet(char *str)
  97. {
  98. boot_hpet_disable = true;
  99. return 1;
  100. }
  101. __setup("nohpet", disable_hpet);
  102. static inline int is_hpet_capable(void)
  103. {
  104. return !boot_hpet_disable && hpet_address;
  105. }
  106. /*
  107. * HPET timer interrupt enable / disable
  108. */
  109. static bool hpet_legacy_int_enabled;
  110. /**
  111. * is_hpet_enabled - check whether the hpet timer interrupt is enabled
  112. */
  113. int is_hpet_enabled(void)
  114. {
  115. return is_hpet_capable() && hpet_legacy_int_enabled;
  116. }
  117. EXPORT_SYMBOL_GPL(is_hpet_enabled);
  118. static void _hpet_print_config(const char *function, int line)
  119. {
  120. u32 i, timers, l, h;
  121. printk(KERN_INFO "hpet: %s(%d):\n", function, line);
  122. l = hpet_readl(HPET_ID);
  123. h = hpet_readl(HPET_PERIOD);
  124. timers = ((l & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
  125. printk(KERN_INFO "hpet: ID: 0x%x, PERIOD: 0x%x\n", l, h);
  126. l = hpet_readl(HPET_CFG);
  127. h = hpet_readl(HPET_STATUS);
  128. printk(KERN_INFO "hpet: CFG: 0x%x, STATUS: 0x%x\n", l, h);
  129. l = hpet_readl(HPET_COUNTER);
  130. h = hpet_readl(HPET_COUNTER+4);
  131. printk(KERN_INFO "hpet: COUNTER_l: 0x%x, COUNTER_h: 0x%x\n", l, h);
  132. for (i = 0; i < timers; i++) {
  133. l = hpet_readl(HPET_Tn_CFG(i));
  134. h = hpet_readl(HPET_Tn_CFG(i)+4);
  135. printk(KERN_INFO "hpet: T%d: CFG_l: 0x%x, CFG_h: 0x%x\n",
  136. i, l, h);
  137. l = hpet_readl(HPET_Tn_CMP(i));
  138. h = hpet_readl(HPET_Tn_CMP(i)+4);
  139. printk(KERN_INFO "hpet: T%d: CMP_l: 0x%x, CMP_h: 0x%x\n",
  140. i, l, h);
  141. l = hpet_readl(HPET_Tn_ROUTE(i));
  142. h = hpet_readl(HPET_Tn_ROUTE(i)+4);
  143. printk(KERN_INFO "hpet: T%d ROUTE_l: 0x%x, ROUTE_h: 0x%x\n",
  144. i, l, h);
  145. }
  146. }
  147. #define hpet_print_config() \
  148. do { \
  149. if (hpet_verbose) \
  150. _hpet_print_config(__func__, __LINE__); \
  151. } while (0)
  152. /*
  153. * When the hpet driver (/dev/hpet) is enabled, we need to reserve
  154. * timer 0 and timer 1 in case of RTC emulation.
  155. */
  156. #ifdef CONFIG_HPET
  157. static void hpet_reserve_msi_timers(struct hpet_data *hd);
  158. static void hpet_reserve_platform_timers(unsigned int id)
  159. {
  160. struct hpet __iomem *hpet = hpet_virt_address;
  161. struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
  162. unsigned int nrtimers, i;
  163. struct hpet_data hd;
  164. nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
  165. memset(&hd, 0, sizeof(hd));
  166. hd.hd_phys_address = hpet_address;
  167. hd.hd_address = hpet;
  168. hd.hd_nirqs = nrtimers;
  169. hpet_reserve_timer(&hd, 0);
  170. #ifdef CONFIG_HPET_EMULATE_RTC
  171. hpet_reserve_timer(&hd, 1);
  172. #endif
  173. /*
  174. * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
  175. * is wrong for i8259!) not the output IRQ. Many BIOS writers
  176. * don't bother configuring *any* comparator interrupts.
  177. */
  178. hd.hd_irq[0] = HPET_LEGACY_8254;
  179. hd.hd_irq[1] = HPET_LEGACY_RTC;
  180. for (i = 2; i < nrtimers; timer++, i++) {
  181. hd.hd_irq[i] = (readl(&timer->hpet_config) &
  182. Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
  183. }
  184. hpet_reserve_msi_timers(&hd);
  185. hpet_alloc(&hd);
  186. }
  187. #else
  188. static void hpet_reserve_platform_timers(unsigned int id) { }
  189. #endif
  190. /*
  191. * Common hpet info
  192. */
  193. static unsigned long hpet_freq;
  194. static struct clock_event_device hpet_clockevent;
  195. static void hpet_stop_counter(void)
  196. {
  197. u32 cfg = hpet_readl(HPET_CFG);
  198. cfg &= ~HPET_CFG_ENABLE;
  199. hpet_writel(cfg, HPET_CFG);
  200. }
  201. static void hpet_reset_counter(void)
  202. {
  203. hpet_writel(0, HPET_COUNTER);
  204. hpet_writel(0, HPET_COUNTER + 4);
  205. }
  206. static void hpet_start_counter(void)
  207. {
  208. unsigned int cfg = hpet_readl(HPET_CFG);
  209. cfg |= HPET_CFG_ENABLE;
  210. hpet_writel(cfg, HPET_CFG);
  211. }
  212. static void hpet_restart_counter(void)
  213. {
  214. hpet_stop_counter();
  215. hpet_reset_counter();
  216. hpet_start_counter();
  217. }
  218. static void hpet_resume_device(void)
  219. {
  220. force_hpet_resume();
  221. }
  222. static void hpet_resume_counter(struct clocksource *cs)
  223. {
  224. hpet_resume_device();
  225. hpet_restart_counter();
  226. }
  227. static void hpet_enable_legacy_int(void)
  228. {
  229. unsigned int cfg = hpet_readl(HPET_CFG);
  230. cfg |= HPET_CFG_LEGACY;
  231. hpet_writel(cfg, HPET_CFG);
  232. hpet_legacy_int_enabled = true;
  233. }
  234. static void hpet_legacy_clockevent_register(void)
  235. {
  236. /* Start HPET legacy interrupts */
  237. hpet_enable_legacy_int();
  238. /*
  239. * Start hpet with the boot cpu mask and make it
  240. * global after the IO_APIC has been initialized.
  241. */
  242. hpet_clockevent.cpumask = cpumask_of(boot_cpu_data.cpu_index);
  243. clockevents_config_and_register(&hpet_clockevent, hpet_freq,
  244. HPET_MIN_PROG_DELTA, 0x7FFFFFFF);
  245. global_clock_event = &hpet_clockevent;
  246. printk(KERN_DEBUG "hpet clockevent registered\n");
  247. }
  248. static int hpet_set_periodic(struct clock_event_device *evt, int timer)
  249. {
  250. unsigned int cfg, cmp, now;
  251. uint64_t delta;
  252. hpet_stop_counter();
  253. delta = ((uint64_t)(NSEC_PER_SEC / HZ)) * evt->mult;
  254. delta >>= evt->shift;
  255. now = hpet_readl(HPET_COUNTER);
  256. cmp = now + (unsigned int)delta;
  257. cfg = hpet_readl(HPET_Tn_CFG(timer));
  258. cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC | HPET_TN_SETVAL |
  259. HPET_TN_32BIT;
  260. hpet_writel(cfg, HPET_Tn_CFG(timer));
  261. hpet_writel(cmp, HPET_Tn_CMP(timer));
  262. udelay(1);
  263. /*
  264. * HPET on AMD 81xx needs a second write (with HPET_TN_SETVAL
  265. * cleared) to T0_CMP to set the period. The HPET_TN_SETVAL
  266. * bit is automatically cleared after the first write.
  267. * (See AMD-8111 HyperTransport I/O Hub Data Sheet,
  268. * Publication # 24674)
  269. */
  270. hpet_writel((unsigned int)delta, HPET_Tn_CMP(timer));
  271. hpet_start_counter();
  272. hpet_print_config();
  273. return 0;
  274. }
  275. static int hpet_set_oneshot(struct clock_event_device *evt, int timer)
  276. {
  277. unsigned int cfg;
  278. cfg = hpet_readl(HPET_Tn_CFG(timer));
  279. cfg &= ~HPET_TN_PERIODIC;
  280. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  281. hpet_writel(cfg, HPET_Tn_CFG(timer));
  282. return 0;
  283. }
  284. static int hpet_shutdown(struct clock_event_device *evt, int timer)
  285. {
  286. unsigned int cfg;
  287. cfg = hpet_readl(HPET_Tn_CFG(timer));
  288. cfg &= ~HPET_TN_ENABLE;
  289. hpet_writel(cfg, HPET_Tn_CFG(timer));
  290. return 0;
  291. }
  292. static int hpet_resume(struct clock_event_device *evt)
  293. {
  294. hpet_enable_legacy_int();
  295. hpet_print_config();
  296. return 0;
  297. }
  298. static int hpet_next_event(unsigned long delta,
  299. struct clock_event_device *evt, int timer)
  300. {
  301. u32 cnt;
  302. s32 res;
  303. cnt = hpet_readl(HPET_COUNTER);
  304. cnt += (u32) delta;
  305. hpet_writel(cnt, HPET_Tn_CMP(timer));
  306. /*
  307. * HPETs are a complete disaster. The compare register is
  308. * based on a equal comparison and neither provides a less
  309. * than or equal functionality (which would require to take
  310. * the wraparound into account) nor a simple count down event
  311. * mode. Further the write to the comparator register is
  312. * delayed internally up to two HPET clock cycles in certain
  313. * chipsets (ATI, ICH9,10). Some newer AMD chipsets have even
  314. * longer delays. We worked around that by reading back the
  315. * compare register, but that required another workaround for
  316. * ICH9,10 chips where the first readout after write can
  317. * return the old stale value. We already had a minimum
  318. * programming delta of 5us enforced, but a NMI or SMI hitting
  319. * between the counter readout and the comparator write can
  320. * move us behind that point easily. Now instead of reading
  321. * the compare register back several times, we make the ETIME
  322. * decision based on the following: Return ETIME if the
  323. * counter value after the write is less than HPET_MIN_CYCLES
  324. * away from the event or if the counter is already ahead of
  325. * the event. The minimum programming delta for the generic
  326. * clockevents code is set to 1.5 * HPET_MIN_CYCLES.
  327. */
  328. res = (s32)(cnt - hpet_readl(HPET_COUNTER));
  329. return res < HPET_MIN_CYCLES ? -ETIME : 0;
  330. }
  331. static int hpet_legacy_shutdown(struct clock_event_device *evt)
  332. {
  333. return hpet_shutdown(evt, 0);
  334. }
  335. static int hpet_legacy_set_oneshot(struct clock_event_device *evt)
  336. {
  337. return hpet_set_oneshot(evt, 0);
  338. }
  339. static int hpet_legacy_set_periodic(struct clock_event_device *evt)
  340. {
  341. return hpet_set_periodic(evt, 0);
  342. }
  343. static int hpet_legacy_resume(struct clock_event_device *evt)
  344. {
  345. return hpet_resume(evt);
  346. }
  347. static int hpet_legacy_next_event(unsigned long delta,
  348. struct clock_event_device *evt)
  349. {
  350. return hpet_next_event(delta, evt, 0);
  351. }
  352. /*
  353. * The hpet clock event device
  354. */
  355. static struct clock_event_device hpet_clockevent = {
  356. .name = "hpet",
  357. .features = CLOCK_EVT_FEAT_PERIODIC |
  358. CLOCK_EVT_FEAT_ONESHOT,
  359. .set_state_periodic = hpet_legacy_set_periodic,
  360. .set_state_oneshot = hpet_legacy_set_oneshot,
  361. .set_state_shutdown = hpet_legacy_shutdown,
  362. .tick_resume = hpet_legacy_resume,
  363. .set_next_event = hpet_legacy_next_event,
  364. .irq = 0,
  365. .rating = 50,
  366. };
  367. /*
  368. * HPET MSI Support
  369. */
  370. #ifdef CONFIG_PCI_MSI
  371. static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
  372. static struct hpet_dev *hpet_devs;
  373. static struct irq_domain *hpet_domain;
  374. void hpet_msi_unmask(struct irq_data *data)
  375. {
  376. struct hpet_dev *hdev = irq_data_get_irq_handler_data(data);
  377. unsigned int cfg;
  378. /* unmask it */
  379. cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
  380. cfg |= HPET_TN_ENABLE | HPET_TN_FSB;
  381. hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
  382. }
  383. void hpet_msi_mask(struct irq_data *data)
  384. {
  385. struct hpet_dev *hdev = irq_data_get_irq_handler_data(data);
  386. unsigned int cfg;
  387. /* mask it */
  388. cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
  389. cfg &= ~(HPET_TN_ENABLE | HPET_TN_FSB);
  390. hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
  391. }
  392. void hpet_msi_write(struct hpet_dev *hdev, struct msi_msg *msg)
  393. {
  394. hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
  395. hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
  396. }
  397. void hpet_msi_read(struct hpet_dev *hdev, struct msi_msg *msg)
  398. {
  399. msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
  400. msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
  401. msg->address_hi = 0;
  402. }
  403. static int hpet_msi_shutdown(struct clock_event_device *evt)
  404. {
  405. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  406. return hpet_shutdown(evt, hdev->num);
  407. }
  408. static int hpet_msi_set_oneshot(struct clock_event_device *evt)
  409. {
  410. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  411. return hpet_set_oneshot(evt, hdev->num);
  412. }
  413. static int hpet_msi_set_periodic(struct clock_event_device *evt)
  414. {
  415. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  416. return hpet_set_periodic(evt, hdev->num);
  417. }
  418. static int hpet_msi_resume(struct clock_event_device *evt)
  419. {
  420. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  421. struct irq_data *data = irq_get_irq_data(hdev->irq);
  422. struct msi_msg msg;
  423. /* Restore the MSI msg and unmask the interrupt */
  424. irq_chip_compose_msi_msg(data, &msg);
  425. hpet_msi_write(hdev, &msg);
  426. hpet_msi_unmask(data);
  427. return 0;
  428. }
  429. static int hpet_msi_next_event(unsigned long delta,
  430. struct clock_event_device *evt)
  431. {
  432. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  433. return hpet_next_event(delta, evt, hdev->num);
  434. }
  435. static irqreturn_t hpet_interrupt_handler(int irq, void *data)
  436. {
  437. struct hpet_dev *dev = (struct hpet_dev *)data;
  438. struct clock_event_device *hevt = &dev->evt;
  439. if (!hevt->event_handler) {
  440. printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
  441. dev->num);
  442. return IRQ_HANDLED;
  443. }
  444. hevt->event_handler(hevt);
  445. return IRQ_HANDLED;
  446. }
  447. static int hpet_setup_irq(struct hpet_dev *dev)
  448. {
  449. if (request_irq(dev->irq, hpet_interrupt_handler,
  450. IRQF_TIMER | IRQF_NOBALANCING,
  451. dev->name, dev))
  452. return -1;
  453. disable_irq(dev->irq);
  454. irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
  455. enable_irq(dev->irq);
  456. printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
  457. dev->name, dev->irq);
  458. return 0;
  459. }
  460. /* This should be called in specific @cpu */
  461. static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
  462. {
  463. struct clock_event_device *evt = &hdev->evt;
  464. WARN_ON(cpu != smp_processor_id());
  465. if (!(hdev->flags & HPET_DEV_VALID))
  466. return;
  467. hdev->cpu = cpu;
  468. per_cpu(cpu_hpet_dev, cpu) = hdev;
  469. evt->name = hdev->name;
  470. hpet_setup_irq(hdev);
  471. evt->irq = hdev->irq;
  472. evt->rating = 110;
  473. evt->features = CLOCK_EVT_FEAT_ONESHOT;
  474. if (hdev->flags & HPET_DEV_PERI_CAP) {
  475. evt->features |= CLOCK_EVT_FEAT_PERIODIC;
  476. evt->set_state_periodic = hpet_msi_set_periodic;
  477. }
  478. evt->set_state_shutdown = hpet_msi_shutdown;
  479. evt->set_state_oneshot = hpet_msi_set_oneshot;
  480. evt->tick_resume = hpet_msi_resume;
  481. evt->set_next_event = hpet_msi_next_event;
  482. evt->cpumask = cpumask_of(hdev->cpu);
  483. clockevents_config_and_register(evt, hpet_freq, HPET_MIN_PROG_DELTA,
  484. 0x7FFFFFFF);
  485. }
  486. #ifdef CONFIG_HPET
  487. /* Reserve at least one timer for userspace (/dev/hpet) */
  488. #define RESERVE_TIMERS 1
  489. #else
  490. #define RESERVE_TIMERS 0
  491. #endif
  492. static void hpet_msi_capability_lookup(unsigned int start_timer)
  493. {
  494. unsigned int id;
  495. unsigned int num_timers;
  496. unsigned int num_timers_used = 0;
  497. int i, irq;
  498. if (hpet_msi_disable)
  499. return;
  500. if (boot_cpu_has(X86_FEATURE_ARAT))
  501. return;
  502. id = hpet_readl(HPET_ID);
  503. num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
  504. num_timers++; /* Value read out starts from 0 */
  505. hpet_print_config();
  506. hpet_domain = hpet_create_irq_domain(hpet_blockid);
  507. if (!hpet_domain)
  508. return;
  509. hpet_devs = kcalloc(num_timers, sizeof(struct hpet_dev), GFP_KERNEL);
  510. if (!hpet_devs)
  511. return;
  512. hpet_num_timers = num_timers;
  513. for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
  514. struct hpet_dev *hdev = &hpet_devs[num_timers_used];
  515. unsigned int cfg = hpet_readl(HPET_Tn_CFG(i));
  516. /* Only consider HPET timer with MSI support */
  517. if (!(cfg & HPET_TN_FSB_CAP))
  518. continue;
  519. hdev->flags = 0;
  520. if (cfg & HPET_TN_PERIODIC_CAP)
  521. hdev->flags |= HPET_DEV_PERI_CAP;
  522. sprintf(hdev->name, "hpet%d", i);
  523. hdev->num = i;
  524. irq = hpet_assign_irq(hpet_domain, hdev, hdev->num);
  525. if (irq <= 0)
  526. continue;
  527. hdev->irq = irq;
  528. hdev->flags |= HPET_DEV_FSB_CAP;
  529. hdev->flags |= HPET_DEV_VALID;
  530. num_timers_used++;
  531. if (num_timers_used == num_possible_cpus())
  532. break;
  533. }
  534. printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
  535. num_timers, num_timers_used);
  536. }
  537. #ifdef CONFIG_HPET
  538. static void hpet_reserve_msi_timers(struct hpet_data *hd)
  539. {
  540. int i;
  541. if (!hpet_devs)
  542. return;
  543. for (i = 0; i < hpet_num_timers; i++) {
  544. struct hpet_dev *hdev = &hpet_devs[i];
  545. if (!(hdev->flags & HPET_DEV_VALID))
  546. continue;
  547. hd->hd_irq[hdev->num] = hdev->irq;
  548. hpet_reserve_timer(hd, hdev->num);
  549. }
  550. }
  551. #endif
  552. static struct hpet_dev *hpet_get_unused_timer(void)
  553. {
  554. int i;
  555. if (!hpet_devs)
  556. return NULL;
  557. for (i = 0; i < hpet_num_timers; i++) {
  558. struct hpet_dev *hdev = &hpet_devs[i];
  559. if (!(hdev->flags & HPET_DEV_VALID))
  560. continue;
  561. if (test_and_set_bit(HPET_DEV_USED_BIT,
  562. (unsigned long *)&hdev->flags))
  563. continue;
  564. return hdev;
  565. }
  566. return NULL;
  567. }
  568. struct hpet_work_struct {
  569. struct delayed_work work;
  570. struct completion complete;
  571. };
  572. static void hpet_work(struct work_struct *w)
  573. {
  574. struct hpet_dev *hdev;
  575. int cpu = smp_processor_id();
  576. struct hpet_work_struct *hpet_work;
  577. hpet_work = container_of(w, struct hpet_work_struct, work.work);
  578. hdev = hpet_get_unused_timer();
  579. if (hdev)
  580. init_one_hpet_msi_clockevent(hdev, cpu);
  581. complete(&hpet_work->complete);
  582. }
  583. static int hpet_cpuhp_online(unsigned int cpu)
  584. {
  585. struct hpet_work_struct work;
  586. INIT_DELAYED_WORK_ONSTACK(&work.work, hpet_work);
  587. init_completion(&work.complete);
  588. /* FIXME: add schedule_work_on() */
  589. schedule_delayed_work_on(cpu, &work.work, 0);
  590. wait_for_completion(&work.complete);
  591. destroy_delayed_work_on_stack(&work.work);
  592. return 0;
  593. }
  594. static int hpet_cpuhp_dead(unsigned int cpu)
  595. {
  596. struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
  597. if (!hdev)
  598. return 0;
  599. free_irq(hdev->irq, hdev);
  600. hdev->flags &= ~HPET_DEV_USED;
  601. per_cpu(cpu_hpet_dev, cpu) = NULL;
  602. return 0;
  603. }
  604. #else
  605. static void hpet_msi_capability_lookup(unsigned int start_timer)
  606. {
  607. return;
  608. }
  609. #ifdef CONFIG_HPET
  610. static void hpet_reserve_msi_timers(struct hpet_data *hd)
  611. {
  612. return;
  613. }
  614. #endif
  615. #define hpet_cpuhp_online NULL
  616. #define hpet_cpuhp_dead NULL
  617. #endif
  618. /*
  619. * Clock source related code
  620. */
  621. #if defined(CONFIG_SMP) && defined(CONFIG_64BIT)
  622. /*
  623. * Reading the HPET counter is a very slow operation. If a large number of
  624. * CPUs are trying to access the HPET counter simultaneously, it can cause
  625. * massive delay and slow down system performance dramatically. This may
  626. * happen when HPET is the default clock source instead of TSC. For a
  627. * really large system with hundreds of CPUs, the slowdown may be so
  628. * severe that it may actually crash the system because of a NMI watchdog
  629. * soft lockup, for example.
  630. *
  631. * If multiple CPUs are trying to access the HPET counter at the same time,
  632. * we don't actually need to read the counter multiple times. Instead, the
  633. * other CPUs can use the counter value read by the first CPU in the group.
  634. *
  635. * This special feature is only enabled on x86-64 systems. It is unlikely
  636. * that 32-bit x86 systems will have enough CPUs to require this feature
  637. * with its associated locking overhead. And we also need 64-bit atomic
  638. * read.
  639. *
  640. * The lock and the hpet value are stored together and can be read in a
  641. * single atomic 64-bit read. It is explicitly assumed that arch_spinlock_t
  642. * is 32 bits in size.
  643. */
  644. union hpet_lock {
  645. struct {
  646. arch_spinlock_t lock;
  647. u32 value;
  648. };
  649. u64 lockval;
  650. };
  651. static union hpet_lock hpet __cacheline_aligned = {
  652. { .lock = __ARCH_SPIN_LOCK_UNLOCKED, },
  653. };
  654. static u64 read_hpet(struct clocksource *cs)
  655. {
  656. unsigned long flags;
  657. union hpet_lock old, new;
  658. BUILD_BUG_ON(sizeof(union hpet_lock) != 8);
  659. /*
  660. * Read HPET directly if in NMI.
  661. */
  662. if (in_nmi())
  663. return (u64)hpet_readl(HPET_COUNTER);
  664. /*
  665. * Read the current state of the lock and HPET value atomically.
  666. */
  667. old.lockval = READ_ONCE(hpet.lockval);
  668. if (arch_spin_is_locked(&old.lock))
  669. goto contended;
  670. local_irq_save(flags);
  671. if (arch_spin_trylock(&hpet.lock)) {
  672. new.value = hpet_readl(HPET_COUNTER);
  673. /*
  674. * Use WRITE_ONCE() to prevent store tearing.
  675. */
  676. WRITE_ONCE(hpet.value, new.value);
  677. arch_spin_unlock(&hpet.lock);
  678. local_irq_restore(flags);
  679. return (u64)new.value;
  680. }
  681. local_irq_restore(flags);
  682. contended:
  683. /*
  684. * Contended case
  685. * --------------
  686. * Wait until the HPET value change or the lock is free to indicate
  687. * its value is up-to-date.
  688. *
  689. * It is possible that old.value has already contained the latest
  690. * HPET value while the lock holder was in the process of releasing
  691. * the lock. Checking for lock state change will enable us to return
  692. * the value immediately instead of waiting for the next HPET reader
  693. * to come along.
  694. */
  695. do {
  696. cpu_relax();
  697. new.lockval = READ_ONCE(hpet.lockval);
  698. } while ((new.value == old.value) && arch_spin_is_locked(&new.lock));
  699. return (u64)new.value;
  700. }
  701. #else
  702. /*
  703. * For UP or 32-bit.
  704. */
  705. static u64 read_hpet(struct clocksource *cs)
  706. {
  707. return (u64)hpet_readl(HPET_COUNTER);
  708. }
  709. #endif
  710. static struct clocksource clocksource_hpet = {
  711. .name = "hpet",
  712. .rating = 250,
  713. .read = read_hpet,
  714. .mask = HPET_MASK,
  715. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  716. .resume = hpet_resume_counter,
  717. };
  718. static int hpet_clocksource_register(void)
  719. {
  720. u64 start, now;
  721. u64 t1;
  722. /* Start the counter */
  723. hpet_restart_counter();
  724. /* Verify whether hpet counter works */
  725. t1 = hpet_readl(HPET_COUNTER);
  726. start = rdtsc();
  727. /*
  728. * We don't know the TSC frequency yet, but waiting for
  729. * 200000 TSC cycles is safe:
  730. * 4 GHz == 50us
  731. * 1 GHz == 200us
  732. */
  733. do {
  734. rep_nop();
  735. now = rdtsc();
  736. } while ((now - start) < 200000UL);
  737. if (t1 == hpet_readl(HPET_COUNTER)) {
  738. printk(KERN_WARNING
  739. "HPET counter not counting. HPET disabled\n");
  740. return -ENODEV;
  741. }
  742. clocksource_register_hz(&clocksource_hpet, (u32)hpet_freq);
  743. return 0;
  744. }
  745. static u32 *hpet_boot_cfg;
  746. /**
  747. * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
  748. */
  749. int __init hpet_enable(void)
  750. {
  751. u32 hpet_period, cfg, id;
  752. u64 freq;
  753. unsigned int i, last;
  754. if (!is_hpet_capable())
  755. return 0;
  756. hpet_set_mapping();
  757. if (!hpet_virt_address)
  758. return 0;
  759. /*
  760. * Read the period and check for a sane value:
  761. */
  762. hpet_period = hpet_readl(HPET_PERIOD);
  763. /*
  764. * AMD SB700 based systems with spread spectrum enabled use a
  765. * SMM based HPET emulation to provide proper frequency
  766. * setting. The SMM code is initialized with the first HPET
  767. * register access and takes some time to complete. During
  768. * this time the config register reads 0xffffffff. We check
  769. * for max. 1000 loops whether the config register reads a non
  770. * 0xffffffff value to make sure that HPET is up and running
  771. * before we go further. A counting loop is safe, as the HPET
  772. * access takes thousands of CPU cycles. On non SB700 based
  773. * machines this check is only done once and has no side
  774. * effects.
  775. */
  776. for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
  777. if (i == 1000) {
  778. printk(KERN_WARNING
  779. "HPET config register value = 0xFFFFFFFF. "
  780. "Disabling HPET\n");
  781. goto out_nohpet;
  782. }
  783. }
  784. if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
  785. goto out_nohpet;
  786. /*
  787. * The period is a femto seconds value. Convert it to a
  788. * frequency.
  789. */
  790. freq = FSEC_PER_SEC;
  791. do_div(freq, hpet_period);
  792. hpet_freq = freq;
  793. /*
  794. * Read the HPET ID register to retrieve the IRQ routing
  795. * information and the number of channels
  796. */
  797. id = hpet_readl(HPET_ID);
  798. hpet_print_config();
  799. last = (id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT;
  800. #ifdef CONFIG_HPET_EMULATE_RTC
  801. /*
  802. * The legacy routing mode needs at least two channels, tick timer
  803. * and the rtc emulation channel.
  804. */
  805. if (!last)
  806. goto out_nohpet;
  807. #endif
  808. cfg = hpet_readl(HPET_CFG);
  809. hpet_boot_cfg = kmalloc_array(last + 2, sizeof(*hpet_boot_cfg),
  810. GFP_KERNEL);
  811. if (hpet_boot_cfg)
  812. *hpet_boot_cfg = cfg;
  813. else
  814. pr_warn("HPET initial state will not be saved\n");
  815. cfg &= ~(HPET_CFG_ENABLE | HPET_CFG_LEGACY);
  816. hpet_writel(cfg, HPET_CFG);
  817. if (cfg)
  818. pr_warn("Unrecognized bits %#x set in global cfg\n", 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("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