hpet.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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/hpet.h>
  8. #include <linux/init.h>
  9. #include <linux/cpu.h>
  10. #include <linux/pm.h>
  11. #include <linux/io.h>
  12. #include <asm/fixmap.h>
  13. #include <asm/i8253.h>
  14. #include <asm/hpet.h>
  15. #define HPET_MASK CLOCKSOURCE_MASK(32)
  16. #define HPET_SHIFT 22
  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. * An erratum on some chipsets (ICH9,..), results in comparator read
  347. * immediately following a write returning old value. Workaround
  348. * for this is to read this value second time, when first
  349. * read returns old value.
  350. */
  351. if (unlikely((u32)hpet_readl(HPET_Tn_CMP(timer)) != cnt)) {
  352. WARN_ONCE(hpet_readl(HPET_Tn_CMP(timer)) != cnt,
  353. KERN_WARNING "hpet: compare register read back failed.\n");
  354. }
  355. return (s32)(hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0;
  356. }
  357. static void hpet_legacy_set_mode(enum clock_event_mode mode,
  358. struct clock_event_device *evt)
  359. {
  360. hpet_set_mode(mode, evt, 0);
  361. }
  362. static int hpet_legacy_next_event(unsigned long delta,
  363. struct clock_event_device *evt)
  364. {
  365. return hpet_next_event(delta, evt, 0);
  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. void hpet_msi_unmask(unsigned int irq)
  374. {
  375. struct hpet_dev *hdev = get_irq_data(irq);
  376. unsigned int cfg;
  377. /* unmask it */
  378. cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
  379. cfg |= HPET_TN_FSB;
  380. hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
  381. }
  382. void hpet_msi_mask(unsigned int irq)
  383. {
  384. unsigned int cfg;
  385. struct hpet_dev *hdev = get_irq_data(irq);
  386. /* mask it */
  387. cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
  388. cfg &= ~HPET_TN_FSB;
  389. hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
  390. }
  391. void hpet_msi_write(unsigned int irq, struct msi_msg *msg)
  392. {
  393. struct hpet_dev *hdev = get_irq_data(irq);
  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(unsigned int irq, struct msi_msg *msg)
  398. {
  399. struct hpet_dev *hdev = get_irq_data(irq);
  400. msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
  401. msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
  402. msg->address_hi = 0;
  403. }
  404. static void hpet_msi_set_mode(enum clock_event_mode mode,
  405. struct clock_event_device *evt)
  406. {
  407. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  408. hpet_set_mode(mode, evt, hdev->num);
  409. }
  410. static int hpet_msi_next_event(unsigned long delta,
  411. struct clock_event_device *evt)
  412. {
  413. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  414. return hpet_next_event(delta, evt, hdev->num);
  415. }
  416. static int hpet_setup_msi_irq(unsigned int irq)
  417. {
  418. if (arch_setup_hpet_msi(irq, hpet_blockid)) {
  419. destroy_irq(irq);
  420. return -EINVAL;
  421. }
  422. return 0;
  423. }
  424. static int hpet_assign_irq(struct hpet_dev *dev)
  425. {
  426. unsigned int irq;
  427. irq = create_irq();
  428. if (!irq)
  429. return -EINVAL;
  430. set_irq_data(irq, dev);
  431. if (hpet_setup_msi_irq(irq))
  432. return -EINVAL;
  433. dev->irq = irq;
  434. return 0;
  435. }
  436. static irqreturn_t hpet_interrupt_handler(int irq, void *data)
  437. {
  438. struct hpet_dev *dev = (struct hpet_dev *)data;
  439. struct clock_event_device *hevt = &dev->evt;
  440. if (!hevt->event_handler) {
  441. printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
  442. dev->num);
  443. return IRQ_HANDLED;
  444. }
  445. hevt->event_handler(hevt);
  446. return IRQ_HANDLED;
  447. }
  448. static int hpet_setup_irq(struct hpet_dev *dev)
  449. {
  450. if (request_irq(dev->irq, hpet_interrupt_handler,
  451. IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
  452. dev->name, dev))
  453. return -1;
  454. disable_irq(dev->irq);
  455. irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
  456. enable_irq(dev->irq);
  457. printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
  458. dev->name, dev->irq);
  459. return 0;
  460. }
  461. /* This should be called in specific @cpu */
  462. static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
  463. {
  464. struct clock_event_device *evt = &hdev->evt;
  465. uint64_t hpet_freq;
  466. WARN_ON(cpu != smp_processor_id());
  467. if (!(hdev->flags & HPET_DEV_VALID))
  468. return;
  469. if (hpet_setup_msi_irq(hdev->irq))
  470. return;
  471. hdev->cpu = cpu;
  472. per_cpu(cpu_hpet_dev, cpu) = hdev;
  473. evt->name = hdev->name;
  474. hpet_setup_irq(hdev);
  475. evt->irq = hdev->irq;
  476. evt->rating = 110;
  477. evt->features = CLOCK_EVT_FEAT_ONESHOT;
  478. if (hdev->flags & HPET_DEV_PERI_CAP)
  479. evt->features |= CLOCK_EVT_FEAT_PERIODIC;
  480. evt->set_mode = hpet_msi_set_mode;
  481. evt->set_next_event = hpet_msi_next_event;
  482. evt->shift = 32;
  483. /*
  484. * The period is a femto seconds value. We need to calculate the
  485. * scaled math multiplication factor for nanosecond to hpet tick
  486. * conversion.
  487. */
  488. hpet_freq = 1000000000000000ULL;
  489. do_div(hpet_freq, hpet_period);
  490. evt->mult = div_sc((unsigned long) hpet_freq,
  491. NSEC_PER_SEC, evt->shift);
  492. /* Calculate the max delta */
  493. evt->max_delta_ns = clockevent_delta2ns(0x7FFFFFFF, evt);
  494. /* 5 usec minimum reprogramming delta. */
  495. evt->min_delta_ns = 5000;
  496. evt->cpumask = cpumask_of(hdev->cpu);
  497. clockevents_register_device(evt);
  498. }
  499. #ifdef CONFIG_HPET
  500. /* Reserve at least one timer for userspace (/dev/hpet) */
  501. #define RESERVE_TIMERS 1
  502. #else
  503. #define RESERVE_TIMERS 0
  504. #endif
  505. static void hpet_msi_capability_lookup(unsigned int start_timer)
  506. {
  507. unsigned int id;
  508. unsigned int num_timers;
  509. unsigned int num_timers_used = 0;
  510. int i;
  511. if (hpet_msi_disable)
  512. return;
  513. if (boot_cpu_has(X86_FEATURE_ARAT))
  514. return;
  515. id = hpet_readl(HPET_ID);
  516. num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
  517. num_timers++; /* Value read out starts from 0 */
  518. hpet_print_config();
  519. hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
  520. if (!hpet_devs)
  521. return;
  522. hpet_num_timers = num_timers;
  523. for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
  524. struct hpet_dev *hdev = &hpet_devs[num_timers_used];
  525. unsigned int cfg = hpet_readl(HPET_Tn_CFG(i));
  526. /* Only consider HPET timer with MSI support */
  527. if (!(cfg & HPET_TN_FSB_CAP))
  528. continue;
  529. hdev->flags = 0;
  530. if (cfg & HPET_TN_PERIODIC_CAP)
  531. hdev->flags |= HPET_DEV_PERI_CAP;
  532. hdev->num = i;
  533. sprintf(hdev->name, "hpet%d", i);
  534. if (hpet_assign_irq(hdev))
  535. continue;
  536. hdev->flags |= HPET_DEV_FSB_CAP;
  537. hdev->flags |= HPET_DEV_VALID;
  538. num_timers_used++;
  539. if (num_timers_used == num_possible_cpus())
  540. break;
  541. }
  542. printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
  543. num_timers, num_timers_used);
  544. }
  545. #ifdef CONFIG_HPET
  546. static void hpet_reserve_msi_timers(struct hpet_data *hd)
  547. {
  548. int i;
  549. if (!hpet_devs)
  550. return;
  551. for (i = 0; i < hpet_num_timers; i++) {
  552. struct hpet_dev *hdev = &hpet_devs[i];
  553. if (!(hdev->flags & HPET_DEV_VALID))
  554. continue;
  555. hd->hd_irq[hdev->num] = hdev->irq;
  556. hpet_reserve_timer(hd, hdev->num);
  557. }
  558. }
  559. #endif
  560. static struct hpet_dev *hpet_get_unused_timer(void)
  561. {
  562. int i;
  563. if (!hpet_devs)
  564. return NULL;
  565. for (i = 0; i < hpet_num_timers; i++) {
  566. struct hpet_dev *hdev = &hpet_devs[i];
  567. if (!(hdev->flags & HPET_DEV_VALID))
  568. continue;
  569. if (test_and_set_bit(HPET_DEV_USED_BIT,
  570. (unsigned long *)&hdev->flags))
  571. continue;
  572. return hdev;
  573. }
  574. return NULL;
  575. }
  576. struct hpet_work_struct {
  577. struct delayed_work work;
  578. struct completion complete;
  579. };
  580. static void hpet_work(struct work_struct *w)
  581. {
  582. struct hpet_dev *hdev;
  583. int cpu = smp_processor_id();
  584. struct hpet_work_struct *hpet_work;
  585. hpet_work = container_of(w, struct hpet_work_struct, work.work);
  586. hdev = hpet_get_unused_timer();
  587. if (hdev)
  588. init_one_hpet_msi_clockevent(hdev, cpu);
  589. complete(&hpet_work->complete);
  590. }
  591. static int hpet_cpuhp_notify(struct notifier_block *n,
  592. unsigned long action, void *hcpu)
  593. {
  594. unsigned long cpu = (unsigned long)hcpu;
  595. struct hpet_work_struct work;
  596. struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
  597. switch (action & 0xf) {
  598. case CPU_ONLINE:
  599. INIT_DELAYED_WORK_ON_STACK(&work.work, hpet_work);
  600. init_completion(&work.complete);
  601. /* FIXME: add schedule_work_on() */
  602. schedule_delayed_work_on(cpu, &work.work, 0);
  603. wait_for_completion(&work.complete);
  604. destroy_timer_on_stack(&work.work.timer);
  605. break;
  606. case CPU_DEAD:
  607. if (hdev) {
  608. free_irq(hdev->irq, hdev);
  609. hdev->flags &= ~HPET_DEV_USED;
  610. per_cpu(cpu_hpet_dev, cpu) = NULL;
  611. }
  612. break;
  613. }
  614. return NOTIFY_OK;
  615. }
  616. #else
  617. static int hpet_setup_msi_irq(unsigned int irq)
  618. {
  619. return 0;
  620. }
  621. static void hpet_msi_capability_lookup(unsigned int start_timer)
  622. {
  623. return;
  624. }
  625. #ifdef CONFIG_HPET
  626. static void hpet_reserve_msi_timers(struct hpet_data *hd)
  627. {
  628. return;
  629. }
  630. #endif
  631. static int hpet_cpuhp_notify(struct notifier_block *n,
  632. unsigned long action, void *hcpu)
  633. {
  634. return NOTIFY_OK;
  635. }
  636. #endif
  637. /*
  638. * Clock source related code
  639. */
  640. static cycle_t read_hpet(struct clocksource *cs)
  641. {
  642. return (cycle_t)hpet_readl(HPET_COUNTER);
  643. }
  644. #ifdef CONFIG_X86_64
  645. static cycle_t __vsyscall_fn vread_hpet(void)
  646. {
  647. return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
  648. }
  649. #endif
  650. static struct clocksource clocksource_hpet = {
  651. .name = "hpet",
  652. .rating = 250,
  653. .read = read_hpet,
  654. .mask = HPET_MASK,
  655. .shift = HPET_SHIFT,
  656. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  657. .resume = hpet_resume_counter,
  658. #ifdef CONFIG_X86_64
  659. .vread = vread_hpet,
  660. #endif
  661. };
  662. static int hpet_clocksource_register(void)
  663. {
  664. u64 start, now;
  665. cycle_t t1;
  666. /* Start the counter */
  667. hpet_restart_counter();
  668. /* Verify whether hpet counter works */
  669. t1 = hpet_readl(HPET_COUNTER);
  670. rdtscll(start);
  671. /*
  672. * We don't know the TSC frequency yet, but waiting for
  673. * 200000 TSC cycles is safe:
  674. * 4 GHz == 50us
  675. * 1 GHz == 200us
  676. */
  677. do {
  678. rep_nop();
  679. rdtscll(now);
  680. } while ((now - start) < 200000UL);
  681. if (t1 == hpet_readl(HPET_COUNTER)) {
  682. printk(KERN_WARNING
  683. "HPET counter not counting. HPET disabled\n");
  684. return -ENODEV;
  685. }
  686. /*
  687. * The definition of mult is (include/linux/clocksource.h)
  688. * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
  689. * so we first need to convert hpet_period to ns/cyc units:
  690. * mult/2^shift = ns/cyc = hpet_period/10^6
  691. * mult = (hpet_period * 2^shift)/10^6
  692. * mult = (hpet_period << shift)/FSEC_PER_NSEC
  693. */
  694. clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
  695. clocksource_register(&clocksource_hpet);
  696. return 0;
  697. }
  698. /**
  699. * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
  700. */
  701. int __init hpet_enable(void)
  702. {
  703. unsigned int id;
  704. int i;
  705. if (!is_hpet_capable())
  706. return 0;
  707. hpet_set_mapping();
  708. /*
  709. * Read the period and check for a sane value:
  710. */
  711. hpet_period = hpet_readl(HPET_PERIOD);
  712. /*
  713. * AMD SB700 based systems with spread spectrum enabled use a
  714. * SMM based HPET emulation to provide proper frequency
  715. * setting. The SMM code is initialized with the first HPET
  716. * register access and takes some time to complete. During
  717. * this time the config register reads 0xffffffff. We check
  718. * for max. 1000 loops whether the config register reads a non
  719. * 0xffffffff value to make sure that HPET is up and running
  720. * before we go further. A counting loop is safe, as the HPET
  721. * access takes thousands of CPU cycles. On non SB700 based
  722. * machines this check is only done once and has no side
  723. * effects.
  724. */
  725. for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
  726. if (i == 1000) {
  727. printk(KERN_WARNING
  728. "HPET config register value = 0xFFFFFFFF. "
  729. "Disabling HPET\n");
  730. goto out_nohpet;
  731. }
  732. }
  733. if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
  734. goto out_nohpet;
  735. /*
  736. * Read the HPET ID register to retrieve the IRQ routing
  737. * information and the number of channels
  738. */
  739. id = hpet_readl(HPET_ID);
  740. hpet_print_config();
  741. #ifdef CONFIG_HPET_EMULATE_RTC
  742. /*
  743. * The legacy routing mode needs at least two channels, tick timer
  744. * and the rtc emulation channel.
  745. */
  746. if (!(id & HPET_ID_NUMBER))
  747. goto out_nohpet;
  748. #endif
  749. if (hpet_clocksource_register())
  750. goto out_nohpet;
  751. if (id & HPET_ID_LEGSUP) {
  752. hpet_legacy_clockevent_register();
  753. return 1;
  754. }
  755. return 0;
  756. out_nohpet:
  757. hpet_clear_mapping();
  758. hpet_address = 0;
  759. return 0;
  760. }
  761. /*
  762. * Needs to be late, as the reserve_timer code calls kalloc !
  763. *
  764. * Not a problem on i386 as hpet_enable is called from late_time_init,
  765. * but on x86_64 it is necessary !
  766. */
  767. static __init int hpet_late_init(void)
  768. {
  769. int cpu;
  770. if (boot_hpet_disable)
  771. return -ENODEV;
  772. if (!hpet_address) {
  773. if (!force_hpet_address)
  774. return -ENODEV;
  775. hpet_address = force_hpet_address;
  776. hpet_enable();
  777. }
  778. if (!hpet_virt_address)
  779. return -ENODEV;
  780. if (hpet_readl(HPET_ID) & HPET_ID_LEGSUP)
  781. hpet_msi_capability_lookup(2);
  782. else
  783. hpet_msi_capability_lookup(0);
  784. hpet_reserve_platform_timers(hpet_readl(HPET_ID));
  785. hpet_print_config();
  786. if (hpet_msi_disable)
  787. return 0;
  788. if (boot_cpu_has(X86_FEATURE_ARAT))
  789. return 0;
  790. for_each_online_cpu(cpu) {
  791. hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
  792. }
  793. /* This notifier should be called after workqueue is ready */
  794. hotcpu_notifier(hpet_cpuhp_notify, -20);
  795. return 0;
  796. }
  797. fs_initcall(hpet_late_init);
  798. void hpet_disable(void)
  799. {
  800. if (is_hpet_capable()) {
  801. unsigned int cfg = hpet_readl(HPET_CFG);
  802. if (hpet_legacy_int_enabled) {
  803. cfg &= ~HPET_CFG_LEGACY;
  804. hpet_legacy_int_enabled = 0;
  805. }
  806. cfg &= ~HPET_CFG_ENABLE;
  807. hpet_writel(cfg, HPET_CFG);
  808. }
  809. }
  810. #ifdef CONFIG_HPET_EMULATE_RTC
  811. /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
  812. * is enabled, we support RTC interrupt functionality in software.
  813. * RTC has 3 kinds of interrupts:
  814. * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
  815. * is updated
  816. * 2) Alarm Interrupt - generate an interrupt at a specific time of day
  817. * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
  818. * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
  819. * (1) and (2) above are implemented using polling at a frequency of
  820. * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
  821. * overhead. (DEFAULT_RTC_INT_FREQ)
  822. * For (3), we use interrupts at 64Hz or user specified periodic
  823. * frequency, whichever is higher.
  824. */
  825. #include <linux/mc146818rtc.h>
  826. #include <linux/rtc.h>
  827. #include <asm/rtc.h>
  828. #define DEFAULT_RTC_INT_FREQ 64
  829. #define DEFAULT_RTC_SHIFT 6
  830. #define RTC_NUM_INTS 1
  831. static unsigned long hpet_rtc_flags;
  832. static int hpet_prev_update_sec;
  833. static struct rtc_time hpet_alarm_time;
  834. static unsigned long hpet_pie_count;
  835. static u32 hpet_t1_cmp;
  836. static u32 hpet_default_delta;
  837. static u32 hpet_pie_delta;
  838. static unsigned long hpet_pie_limit;
  839. static rtc_irq_handler irq_handler;
  840. /*
  841. * Check that the hpet counter c1 is ahead of the c2
  842. */
  843. static inline int hpet_cnt_ahead(u32 c1, u32 c2)
  844. {
  845. return (s32)(c2 - c1) < 0;
  846. }
  847. /*
  848. * Registers a IRQ handler.
  849. */
  850. int hpet_register_irq_handler(rtc_irq_handler handler)
  851. {
  852. if (!is_hpet_enabled())
  853. return -ENODEV;
  854. if (irq_handler)
  855. return -EBUSY;
  856. irq_handler = handler;
  857. return 0;
  858. }
  859. EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
  860. /*
  861. * Deregisters the IRQ handler registered with hpet_register_irq_handler()
  862. * and does cleanup.
  863. */
  864. void hpet_unregister_irq_handler(rtc_irq_handler handler)
  865. {
  866. if (!is_hpet_enabled())
  867. return;
  868. irq_handler = NULL;
  869. hpet_rtc_flags = 0;
  870. }
  871. EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
  872. /*
  873. * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
  874. * is not supported by all HPET implementations for timer 1.
  875. *
  876. * hpet_rtc_timer_init() is called when the rtc is initialized.
  877. */
  878. int hpet_rtc_timer_init(void)
  879. {
  880. unsigned int cfg, cnt, delta;
  881. unsigned long flags;
  882. if (!is_hpet_enabled())
  883. return 0;
  884. if (!hpet_default_delta) {
  885. uint64_t clc;
  886. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  887. clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
  888. hpet_default_delta = clc;
  889. }
  890. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  891. delta = hpet_default_delta;
  892. else
  893. delta = hpet_pie_delta;
  894. local_irq_save(flags);
  895. cnt = delta + hpet_readl(HPET_COUNTER);
  896. hpet_writel(cnt, HPET_T1_CMP);
  897. hpet_t1_cmp = cnt;
  898. cfg = hpet_readl(HPET_T1_CFG);
  899. cfg &= ~HPET_TN_PERIODIC;
  900. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  901. hpet_writel(cfg, HPET_T1_CFG);
  902. local_irq_restore(flags);
  903. return 1;
  904. }
  905. EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
  906. /*
  907. * The functions below are called from rtc driver.
  908. * Return 0 if HPET is not being used.
  909. * Otherwise do the necessary changes and return 1.
  910. */
  911. int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
  912. {
  913. if (!is_hpet_enabled())
  914. return 0;
  915. hpet_rtc_flags &= ~bit_mask;
  916. return 1;
  917. }
  918. EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
  919. int hpet_set_rtc_irq_bit(unsigned long bit_mask)
  920. {
  921. unsigned long oldbits = hpet_rtc_flags;
  922. if (!is_hpet_enabled())
  923. return 0;
  924. hpet_rtc_flags |= bit_mask;
  925. if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
  926. hpet_prev_update_sec = -1;
  927. if (!oldbits)
  928. hpet_rtc_timer_init();
  929. return 1;
  930. }
  931. EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
  932. int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
  933. unsigned char sec)
  934. {
  935. if (!is_hpet_enabled())
  936. return 0;
  937. hpet_alarm_time.tm_hour = hrs;
  938. hpet_alarm_time.tm_min = min;
  939. hpet_alarm_time.tm_sec = sec;
  940. return 1;
  941. }
  942. EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
  943. int hpet_set_periodic_freq(unsigned long freq)
  944. {
  945. uint64_t clc;
  946. if (!is_hpet_enabled())
  947. return 0;
  948. if (freq <= DEFAULT_RTC_INT_FREQ)
  949. hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
  950. else {
  951. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  952. do_div(clc, freq);
  953. clc >>= hpet_clockevent.shift;
  954. hpet_pie_delta = clc;
  955. }
  956. return 1;
  957. }
  958. EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
  959. int hpet_rtc_dropped_irq(void)
  960. {
  961. return is_hpet_enabled();
  962. }
  963. EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
  964. static void hpet_rtc_timer_reinit(void)
  965. {
  966. unsigned int cfg, delta;
  967. int lost_ints = -1;
  968. if (unlikely(!hpet_rtc_flags)) {
  969. cfg = hpet_readl(HPET_T1_CFG);
  970. cfg &= ~HPET_TN_ENABLE;
  971. hpet_writel(cfg, HPET_T1_CFG);
  972. return;
  973. }
  974. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  975. delta = hpet_default_delta;
  976. else
  977. delta = hpet_pie_delta;
  978. /*
  979. * Increment the comparator value until we are ahead of the
  980. * current count.
  981. */
  982. do {
  983. hpet_t1_cmp += delta;
  984. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  985. lost_ints++;
  986. } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
  987. if (lost_ints) {
  988. if (hpet_rtc_flags & RTC_PIE)
  989. hpet_pie_count += lost_ints;
  990. if (printk_ratelimit())
  991. printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
  992. lost_ints);
  993. }
  994. }
  995. irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
  996. {
  997. struct rtc_time curr_time;
  998. unsigned long rtc_int_flag = 0;
  999. hpet_rtc_timer_reinit();
  1000. memset(&curr_time, 0, sizeof(struct rtc_time));
  1001. if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
  1002. get_rtc_time(&curr_time);
  1003. if (hpet_rtc_flags & RTC_UIE &&
  1004. curr_time.tm_sec != hpet_prev_update_sec) {
  1005. if (hpet_prev_update_sec >= 0)
  1006. rtc_int_flag = RTC_UF;
  1007. hpet_prev_update_sec = curr_time.tm_sec;
  1008. }
  1009. if (hpet_rtc_flags & RTC_PIE &&
  1010. ++hpet_pie_count >= hpet_pie_limit) {
  1011. rtc_int_flag |= RTC_PF;
  1012. hpet_pie_count = 0;
  1013. }
  1014. if (hpet_rtc_flags & RTC_AIE &&
  1015. (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
  1016. (curr_time.tm_min == hpet_alarm_time.tm_min) &&
  1017. (curr_time.tm_hour == hpet_alarm_time.tm_hour))
  1018. rtc_int_flag |= RTC_AF;
  1019. if (rtc_int_flag) {
  1020. rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
  1021. if (irq_handler)
  1022. irq_handler(rtc_int_flag, dev_id);
  1023. }
  1024. return IRQ_HANDLED;
  1025. }
  1026. EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
  1027. #endif