hpet.c 27 KB

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