hpet.c 29 KB

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