hpet.c 25 KB

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