hpet.c 29 KB

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