hpet.c 27 KB

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