hpet.c 29 KB

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