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