hpet.c 29 KB

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