hpet.c 27 KB

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