hpet.c 28 KB

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