hpet.c 27 KB

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