hpet.c 27 KB

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