hpet.c 27 KB

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