hpet.c 27 KB

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