hpet.c 27 KB

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