hpet.c 29 KB

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