hpet.c 28 KB

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