hpet.c 28 KB

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