hpet.c 29 KB

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