hpet.c 25 KB

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