hpet.c 27 KB

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