hpet.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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. /* 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 HPET_MIN_CYCLES 128
  26. #define HPET_MIN_PROG_DELTA (HPET_MIN_CYCLES + (HPET_MIN_CYCLES >> 1))
  27. #define EVT_TO_HPET_DEV(evt) container_of(evt, struct hpet_dev, evt)
  28. /*
  29. * HPET address is set in acpi/boot.c, when an ACPI entry exists
  30. */
  31. unsigned long hpet_address;
  32. u8 hpet_blockid; /* OS timer block num */
  33. u8 hpet_msi_disable;
  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_freq;
  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. .irq = 0,
  199. .rating = 50,
  200. };
  201. static void hpet_stop_counter(void)
  202. {
  203. unsigned long cfg = hpet_readl(HPET_CFG);
  204. cfg &= ~HPET_CFG_ENABLE;
  205. hpet_writel(cfg, HPET_CFG);
  206. }
  207. static void hpet_reset_counter(void)
  208. {
  209. hpet_writel(0, HPET_COUNTER);
  210. hpet_writel(0, HPET_COUNTER + 4);
  211. }
  212. static void hpet_start_counter(void)
  213. {
  214. unsigned int cfg = hpet_readl(HPET_CFG);
  215. cfg |= HPET_CFG_ENABLE;
  216. hpet_writel(cfg, HPET_CFG);
  217. }
  218. static void hpet_restart_counter(void)
  219. {
  220. hpet_stop_counter();
  221. hpet_reset_counter();
  222. hpet_start_counter();
  223. }
  224. static void hpet_resume_device(void)
  225. {
  226. force_hpet_resume();
  227. }
  228. static void hpet_resume_counter(struct clocksource *cs)
  229. {
  230. hpet_resume_device();
  231. hpet_restart_counter();
  232. }
  233. static void hpet_enable_legacy_int(void)
  234. {
  235. unsigned int cfg = hpet_readl(HPET_CFG);
  236. cfg |= HPET_CFG_LEGACY;
  237. hpet_writel(cfg, HPET_CFG);
  238. hpet_legacy_int_enabled = 1;
  239. }
  240. static void hpet_legacy_clockevent_register(void)
  241. {
  242. /* Start HPET legacy interrupts */
  243. hpet_enable_legacy_int();
  244. /*
  245. * Start hpet with the boot cpu mask and make it
  246. * global after the IO_APIC has been initialized.
  247. */
  248. hpet_clockevent.cpumask = cpumask_of(smp_processor_id());
  249. clockevents_config_and_register(&hpet_clockevent, hpet_freq,
  250. HPET_MIN_PROG_DELTA, 0x7FFFFFFF);
  251. global_clock_event = &hpet_clockevent;
  252. printk(KERN_DEBUG "hpet clockevent registered\n");
  253. }
  254. static int hpet_setup_msi_irq(unsigned int irq);
  255. static void hpet_set_mode(enum clock_event_mode mode,
  256. struct clock_event_device *evt, int timer)
  257. {
  258. unsigned int cfg, cmp, now;
  259. uint64_t delta;
  260. switch (mode) {
  261. case CLOCK_EVT_MODE_PERIODIC:
  262. hpet_stop_counter();
  263. delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
  264. delta >>= evt->shift;
  265. now = hpet_readl(HPET_COUNTER);
  266. cmp = now + (unsigned int) delta;
  267. cfg = hpet_readl(HPET_Tn_CFG(timer));
  268. /* Make sure we use edge triggered interrupts */
  269. cfg &= ~HPET_TN_LEVEL;
  270. cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
  271. HPET_TN_SETVAL | HPET_TN_32BIT;
  272. hpet_writel(cfg, HPET_Tn_CFG(timer));
  273. hpet_writel(cmp, HPET_Tn_CMP(timer));
  274. udelay(1);
  275. /*
  276. * HPET on AMD 81xx needs a second write (with HPET_TN_SETVAL
  277. * cleared) to T0_CMP to set the period. The HPET_TN_SETVAL
  278. * bit is automatically cleared after the first write.
  279. * (See AMD-8111 HyperTransport I/O Hub Data Sheet,
  280. * Publication # 24674)
  281. */
  282. hpet_writel((unsigned int) delta, HPET_Tn_CMP(timer));
  283. hpet_start_counter();
  284. hpet_print_config();
  285. break;
  286. case CLOCK_EVT_MODE_ONESHOT:
  287. cfg = hpet_readl(HPET_Tn_CFG(timer));
  288. cfg &= ~HPET_TN_PERIODIC;
  289. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  290. hpet_writel(cfg, HPET_Tn_CFG(timer));
  291. break;
  292. case CLOCK_EVT_MODE_UNUSED:
  293. case CLOCK_EVT_MODE_SHUTDOWN:
  294. cfg = hpet_readl(HPET_Tn_CFG(timer));
  295. cfg &= ~HPET_TN_ENABLE;
  296. hpet_writel(cfg, HPET_Tn_CFG(timer));
  297. break;
  298. case CLOCK_EVT_MODE_RESUME:
  299. if (timer == 0) {
  300. hpet_enable_legacy_int();
  301. } else {
  302. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  303. hpet_setup_msi_irq(hdev->irq);
  304. disable_irq(hdev->irq);
  305. irq_set_affinity(hdev->irq, cpumask_of(hdev->cpu));
  306. enable_irq(hdev->irq);
  307. }
  308. hpet_print_config();
  309. break;
  310. }
  311. }
  312. static int hpet_next_event(unsigned long delta,
  313. struct clock_event_device *evt, int timer)
  314. {
  315. u32 cnt;
  316. s32 res;
  317. cnt = hpet_readl(HPET_COUNTER);
  318. cnt += (u32) delta;
  319. hpet_writel(cnt, HPET_Tn_CMP(timer));
  320. /*
  321. * HPETs are a complete disaster. The compare register is
  322. * based on a equal comparison and neither provides a less
  323. * than or equal functionality (which would require to take
  324. * the wraparound into account) nor a simple count down event
  325. * mode. Further the write to the comparator register is
  326. * delayed internally up to two HPET clock cycles in certain
  327. * chipsets (ATI, ICH9,10). Some newer AMD chipsets have even
  328. * longer delays. We worked around that by reading back the
  329. * compare register, but that required another workaround for
  330. * ICH9,10 chips where the first readout after write can
  331. * return the old stale value. We already had a minimum
  332. * programming delta of 5us enforced, but a NMI or SMI hitting
  333. * between the counter readout and the comparator write can
  334. * move us behind that point easily. Now instead of reading
  335. * the compare register back several times, we make the ETIME
  336. * decision based on the following: Return ETIME if the
  337. * counter value after the write is less than HPET_MIN_CYCLES
  338. * away from the event or if the counter is already ahead of
  339. * the event. The minimum programming delta for the generic
  340. * clockevents code is set to 1.5 * HPET_MIN_CYCLES.
  341. */
  342. res = (s32)(cnt - hpet_readl(HPET_COUNTER));
  343. return res < HPET_MIN_CYCLES ? -ETIME : 0;
  344. }
  345. static void hpet_legacy_set_mode(enum clock_event_mode mode,
  346. struct clock_event_device *evt)
  347. {
  348. hpet_set_mode(mode, evt, 0);
  349. }
  350. static int hpet_legacy_next_event(unsigned long delta,
  351. struct clock_event_device *evt)
  352. {
  353. return hpet_next_event(delta, evt, 0);
  354. }
  355. /*
  356. * HPET MSI Support
  357. */
  358. #ifdef CONFIG_PCI_MSI
  359. static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
  360. static struct hpet_dev *hpet_devs;
  361. void hpet_msi_unmask(struct irq_data *data)
  362. {
  363. struct hpet_dev *hdev = data->handler_data;
  364. unsigned int cfg;
  365. /* unmask it */
  366. cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
  367. cfg |= HPET_TN_FSB;
  368. hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
  369. }
  370. void hpet_msi_mask(struct irq_data *data)
  371. {
  372. struct hpet_dev *hdev = data->handler_data;
  373. unsigned int cfg;
  374. /* mask it */
  375. cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
  376. cfg &= ~HPET_TN_FSB;
  377. hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
  378. }
  379. void hpet_msi_write(struct hpet_dev *hdev, struct msi_msg *msg)
  380. {
  381. hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
  382. hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
  383. }
  384. void hpet_msi_read(struct hpet_dev *hdev, struct msi_msg *msg)
  385. {
  386. msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
  387. msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
  388. msg->address_hi = 0;
  389. }
  390. static void hpet_msi_set_mode(enum clock_event_mode mode,
  391. struct clock_event_device *evt)
  392. {
  393. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  394. hpet_set_mode(mode, evt, hdev->num);
  395. }
  396. static int hpet_msi_next_event(unsigned long delta,
  397. struct clock_event_device *evt)
  398. {
  399. struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
  400. return hpet_next_event(delta, evt, hdev->num);
  401. }
  402. static int hpet_setup_msi_irq(unsigned int irq)
  403. {
  404. if (arch_setup_hpet_msi(irq, hpet_blockid)) {
  405. destroy_irq(irq);
  406. return -EINVAL;
  407. }
  408. return 0;
  409. }
  410. static int hpet_assign_irq(struct hpet_dev *dev)
  411. {
  412. unsigned int irq;
  413. irq = create_irq_nr(0, -1);
  414. if (!irq)
  415. return -EINVAL;
  416. irq_set_handler_data(irq, dev);
  417. if (hpet_setup_msi_irq(irq))
  418. return -EINVAL;
  419. dev->irq = irq;
  420. return 0;
  421. }
  422. static irqreturn_t hpet_interrupt_handler(int irq, void *data)
  423. {
  424. struct hpet_dev *dev = (struct hpet_dev *)data;
  425. struct clock_event_device *hevt = &dev->evt;
  426. if (!hevt->event_handler) {
  427. printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
  428. dev->num);
  429. return IRQ_HANDLED;
  430. }
  431. hevt->event_handler(hevt);
  432. return IRQ_HANDLED;
  433. }
  434. static int hpet_setup_irq(struct hpet_dev *dev)
  435. {
  436. if (request_irq(dev->irq, hpet_interrupt_handler,
  437. IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
  438. dev->name, dev))
  439. return -1;
  440. disable_irq(dev->irq);
  441. irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
  442. enable_irq(dev->irq);
  443. printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
  444. dev->name, dev->irq);
  445. return 0;
  446. }
  447. /* This should be called in specific @cpu */
  448. static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
  449. {
  450. struct clock_event_device *evt = &hdev->evt;
  451. WARN_ON(cpu != smp_processor_id());
  452. if (!(hdev->flags & HPET_DEV_VALID))
  453. return;
  454. if (hpet_setup_msi_irq(hdev->irq))
  455. return;
  456. hdev->cpu = cpu;
  457. per_cpu(cpu_hpet_dev, cpu) = hdev;
  458. evt->name = hdev->name;
  459. hpet_setup_irq(hdev);
  460. evt->irq = hdev->irq;
  461. evt->rating = 110;
  462. evt->features = CLOCK_EVT_FEAT_ONESHOT;
  463. if (hdev->flags & HPET_DEV_PERI_CAP)
  464. evt->features |= CLOCK_EVT_FEAT_PERIODIC;
  465. evt->set_mode = hpet_msi_set_mode;
  466. evt->set_next_event = hpet_msi_next_event;
  467. evt->cpumask = cpumask_of(hdev->cpu);
  468. clockevents_config_and_register(evt, hpet_freq, HPET_MIN_PROG_DELTA,
  469. 0x7FFFFFFF);
  470. }
  471. #ifdef CONFIG_HPET
  472. /* Reserve at least one timer for userspace (/dev/hpet) */
  473. #define RESERVE_TIMERS 1
  474. #else
  475. #define RESERVE_TIMERS 0
  476. #endif
  477. static void hpet_msi_capability_lookup(unsigned int start_timer)
  478. {
  479. unsigned int id;
  480. unsigned int num_timers;
  481. unsigned int num_timers_used = 0;
  482. int i;
  483. if (hpet_msi_disable)
  484. return;
  485. if (boot_cpu_has(X86_FEATURE_ARAT))
  486. return;
  487. id = hpet_readl(HPET_ID);
  488. num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
  489. num_timers++; /* Value read out starts from 0 */
  490. hpet_print_config();
  491. hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
  492. if (!hpet_devs)
  493. return;
  494. hpet_num_timers = num_timers;
  495. for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
  496. struct hpet_dev *hdev = &hpet_devs[num_timers_used];
  497. unsigned int cfg = hpet_readl(HPET_Tn_CFG(i));
  498. /* Only consider HPET timer with MSI support */
  499. if (!(cfg & HPET_TN_FSB_CAP))
  500. continue;
  501. hdev->flags = 0;
  502. if (cfg & HPET_TN_PERIODIC_CAP)
  503. hdev->flags |= HPET_DEV_PERI_CAP;
  504. hdev->num = i;
  505. sprintf(hdev->name, "hpet%d", i);
  506. if (hpet_assign_irq(hdev))
  507. continue;
  508. hdev->flags |= HPET_DEV_FSB_CAP;
  509. hdev->flags |= HPET_DEV_VALID;
  510. num_timers_used++;
  511. if (num_timers_used == num_possible_cpus())
  512. break;
  513. }
  514. printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
  515. num_timers, num_timers_used);
  516. }
  517. #ifdef CONFIG_HPET
  518. static void hpet_reserve_msi_timers(struct hpet_data *hd)
  519. {
  520. int i;
  521. if (!hpet_devs)
  522. return;
  523. for (i = 0; i < hpet_num_timers; i++) {
  524. struct hpet_dev *hdev = &hpet_devs[i];
  525. if (!(hdev->flags & HPET_DEV_VALID))
  526. continue;
  527. hd->hd_irq[hdev->num] = hdev->irq;
  528. hpet_reserve_timer(hd, hdev->num);
  529. }
  530. }
  531. #endif
  532. static struct hpet_dev *hpet_get_unused_timer(void)
  533. {
  534. int i;
  535. if (!hpet_devs)
  536. return NULL;
  537. for (i = 0; i < hpet_num_timers; i++) {
  538. struct hpet_dev *hdev = &hpet_devs[i];
  539. if (!(hdev->flags & HPET_DEV_VALID))
  540. continue;
  541. if (test_and_set_bit(HPET_DEV_USED_BIT,
  542. (unsigned long *)&hdev->flags))
  543. continue;
  544. return hdev;
  545. }
  546. return NULL;
  547. }
  548. struct hpet_work_struct {
  549. struct delayed_work work;
  550. struct completion complete;
  551. };
  552. static void hpet_work(struct work_struct *w)
  553. {
  554. struct hpet_dev *hdev;
  555. int cpu = smp_processor_id();
  556. struct hpet_work_struct *hpet_work;
  557. hpet_work = container_of(w, struct hpet_work_struct, work.work);
  558. hdev = hpet_get_unused_timer();
  559. if (hdev)
  560. init_one_hpet_msi_clockevent(hdev, cpu);
  561. complete(&hpet_work->complete);
  562. }
  563. static int hpet_cpuhp_notify(struct notifier_block *n,
  564. unsigned long action, void *hcpu)
  565. {
  566. unsigned long cpu = (unsigned long)hcpu;
  567. struct hpet_work_struct work;
  568. struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
  569. switch (action & 0xf) {
  570. case CPU_ONLINE:
  571. INIT_DELAYED_WORK_ONSTACK(&work.work, hpet_work);
  572. init_completion(&work.complete);
  573. /* FIXME: add schedule_work_on() */
  574. schedule_delayed_work_on(cpu, &work.work, 0);
  575. wait_for_completion(&work.complete);
  576. destroy_timer_on_stack(&work.work.timer);
  577. break;
  578. case CPU_DEAD:
  579. if (hdev) {
  580. free_irq(hdev->irq, hdev);
  581. hdev->flags &= ~HPET_DEV_USED;
  582. per_cpu(cpu_hpet_dev, cpu) = NULL;
  583. }
  584. break;
  585. }
  586. return NOTIFY_OK;
  587. }
  588. #else
  589. static int hpet_setup_msi_irq(unsigned int irq)
  590. {
  591. return 0;
  592. }
  593. static void hpet_msi_capability_lookup(unsigned int start_timer)
  594. {
  595. return;
  596. }
  597. #ifdef CONFIG_HPET
  598. static void hpet_reserve_msi_timers(struct hpet_data *hd)
  599. {
  600. return;
  601. }
  602. #endif
  603. static int hpet_cpuhp_notify(struct notifier_block *n,
  604. unsigned long action, void *hcpu)
  605. {
  606. return NOTIFY_OK;
  607. }
  608. #endif
  609. /*
  610. * Clock source related code
  611. */
  612. static cycle_t read_hpet(struct clocksource *cs)
  613. {
  614. return (cycle_t)hpet_readl(HPET_COUNTER);
  615. }
  616. #ifdef CONFIG_X86_64
  617. static cycle_t __vsyscall_fn vread_hpet(void)
  618. {
  619. return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
  620. }
  621. #endif
  622. static struct clocksource clocksource_hpet = {
  623. .name = "hpet",
  624. .rating = 250,
  625. .read = read_hpet,
  626. .mask = HPET_MASK,
  627. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  628. .resume = hpet_resume_counter,
  629. #ifdef CONFIG_X86_64
  630. .vread = vread_hpet,
  631. #endif
  632. };
  633. static int hpet_clocksource_register(void)
  634. {
  635. u64 start, now;
  636. cycle_t t1;
  637. /* Start the counter */
  638. hpet_restart_counter();
  639. /* Verify whether hpet counter works */
  640. t1 = hpet_readl(HPET_COUNTER);
  641. rdtscll(start);
  642. /*
  643. * We don't know the TSC frequency yet, but waiting for
  644. * 200000 TSC cycles is safe:
  645. * 4 GHz == 50us
  646. * 1 GHz == 200us
  647. */
  648. do {
  649. rep_nop();
  650. rdtscll(now);
  651. } while ((now - start) < 200000UL);
  652. if (t1 == hpet_readl(HPET_COUNTER)) {
  653. printk(KERN_WARNING
  654. "HPET counter not counting. HPET disabled\n");
  655. return -ENODEV;
  656. }
  657. clocksource_register_hz(&clocksource_hpet, (u32)hpet_freq);
  658. return 0;
  659. }
  660. /**
  661. * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
  662. */
  663. int __init hpet_enable(void)
  664. {
  665. unsigned long hpet_period;
  666. unsigned int id;
  667. u64 freq;
  668. int i;
  669. if (!is_hpet_capable())
  670. return 0;
  671. hpet_set_mapping();
  672. /*
  673. * Read the period and check for a sane value:
  674. */
  675. hpet_period = hpet_readl(HPET_PERIOD);
  676. /*
  677. * AMD SB700 based systems with spread spectrum enabled use a
  678. * SMM based HPET emulation to provide proper frequency
  679. * setting. The SMM code is initialized with the first HPET
  680. * register access and takes some time to complete. During
  681. * this time the config register reads 0xffffffff. We check
  682. * for max. 1000 loops whether the config register reads a non
  683. * 0xffffffff value to make sure that HPET is up and running
  684. * before we go further. A counting loop is safe, as the HPET
  685. * access takes thousands of CPU cycles. On non SB700 based
  686. * machines this check is only done once and has no side
  687. * effects.
  688. */
  689. for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
  690. if (i == 1000) {
  691. printk(KERN_WARNING
  692. "HPET config register value = 0xFFFFFFFF. "
  693. "Disabling HPET\n");
  694. goto out_nohpet;
  695. }
  696. }
  697. if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
  698. goto out_nohpet;
  699. /*
  700. * The period is a femto seconds value. Convert it to a
  701. * frequency.
  702. */
  703. freq = FSEC_PER_SEC;
  704. do_div(freq, hpet_period);
  705. hpet_freq = freq;
  706. /*
  707. * Read the HPET ID register to retrieve the IRQ routing
  708. * information and the number of channels
  709. */
  710. id = hpet_readl(HPET_ID);
  711. hpet_print_config();
  712. #ifdef CONFIG_HPET_EMULATE_RTC
  713. /*
  714. * The legacy routing mode needs at least two channels, tick timer
  715. * and the rtc emulation channel.
  716. */
  717. if (!(id & HPET_ID_NUMBER))
  718. goto out_nohpet;
  719. #endif
  720. if (hpet_clocksource_register())
  721. goto out_nohpet;
  722. if (id & HPET_ID_LEGSUP) {
  723. hpet_legacy_clockevent_register();
  724. return 1;
  725. }
  726. return 0;
  727. out_nohpet:
  728. hpet_clear_mapping();
  729. hpet_address = 0;
  730. return 0;
  731. }
  732. /*
  733. * Needs to be late, as the reserve_timer code calls kalloc !
  734. *
  735. * Not a problem on i386 as hpet_enable is called from late_time_init,
  736. * but on x86_64 it is necessary !
  737. */
  738. static __init int hpet_late_init(void)
  739. {
  740. int cpu;
  741. if (boot_hpet_disable)
  742. return -ENODEV;
  743. if (!hpet_address) {
  744. if (!force_hpet_address)
  745. return -ENODEV;
  746. hpet_address = force_hpet_address;
  747. hpet_enable();
  748. }
  749. if (!hpet_virt_address)
  750. return -ENODEV;
  751. if (hpet_readl(HPET_ID) & HPET_ID_LEGSUP)
  752. hpet_msi_capability_lookup(2);
  753. else
  754. hpet_msi_capability_lookup(0);
  755. hpet_reserve_platform_timers(hpet_readl(HPET_ID));
  756. hpet_print_config();
  757. if (hpet_msi_disable)
  758. return 0;
  759. if (boot_cpu_has(X86_FEATURE_ARAT))
  760. return 0;
  761. for_each_online_cpu(cpu) {
  762. hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
  763. }
  764. /* This notifier should be called after workqueue is ready */
  765. hotcpu_notifier(hpet_cpuhp_notify, -20);
  766. return 0;
  767. }
  768. fs_initcall(hpet_late_init);
  769. void hpet_disable(void)
  770. {
  771. if (is_hpet_capable() && hpet_virt_address) {
  772. unsigned int cfg = hpet_readl(HPET_CFG);
  773. if (hpet_legacy_int_enabled) {
  774. cfg &= ~HPET_CFG_LEGACY;
  775. hpet_legacy_int_enabled = 0;
  776. }
  777. cfg &= ~HPET_CFG_ENABLE;
  778. hpet_writel(cfg, HPET_CFG);
  779. }
  780. }
  781. #ifdef CONFIG_HPET_EMULATE_RTC
  782. /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
  783. * is enabled, we support RTC interrupt functionality in software.
  784. * RTC has 3 kinds of interrupts:
  785. * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
  786. * is updated
  787. * 2) Alarm Interrupt - generate an interrupt at a specific time of day
  788. * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
  789. * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
  790. * (1) and (2) above are implemented using polling at a frequency of
  791. * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
  792. * overhead. (DEFAULT_RTC_INT_FREQ)
  793. * For (3), we use interrupts at 64Hz or user specified periodic
  794. * frequency, whichever is higher.
  795. */
  796. #include <linux/mc146818rtc.h>
  797. #include <linux/rtc.h>
  798. #include <asm/rtc.h>
  799. #define DEFAULT_RTC_INT_FREQ 64
  800. #define DEFAULT_RTC_SHIFT 6
  801. #define RTC_NUM_INTS 1
  802. static unsigned long hpet_rtc_flags;
  803. static int hpet_prev_update_sec;
  804. static struct rtc_time hpet_alarm_time;
  805. static unsigned long hpet_pie_count;
  806. static u32 hpet_t1_cmp;
  807. static u32 hpet_default_delta;
  808. static u32 hpet_pie_delta;
  809. static unsigned long hpet_pie_limit;
  810. static rtc_irq_handler irq_handler;
  811. /*
  812. * Check that the hpet counter c1 is ahead of the c2
  813. */
  814. static inline int hpet_cnt_ahead(u32 c1, u32 c2)
  815. {
  816. return (s32)(c2 - c1) < 0;
  817. }
  818. /*
  819. * Registers a IRQ handler.
  820. */
  821. int hpet_register_irq_handler(rtc_irq_handler handler)
  822. {
  823. if (!is_hpet_enabled())
  824. return -ENODEV;
  825. if (irq_handler)
  826. return -EBUSY;
  827. irq_handler = handler;
  828. return 0;
  829. }
  830. EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
  831. /*
  832. * Deregisters the IRQ handler registered with hpet_register_irq_handler()
  833. * and does cleanup.
  834. */
  835. void hpet_unregister_irq_handler(rtc_irq_handler handler)
  836. {
  837. if (!is_hpet_enabled())
  838. return;
  839. irq_handler = NULL;
  840. hpet_rtc_flags = 0;
  841. }
  842. EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
  843. /*
  844. * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
  845. * is not supported by all HPET implementations for timer 1.
  846. *
  847. * hpet_rtc_timer_init() is called when the rtc is initialized.
  848. */
  849. int hpet_rtc_timer_init(void)
  850. {
  851. unsigned int cfg, cnt, delta;
  852. unsigned long flags;
  853. if (!is_hpet_enabled())
  854. return 0;
  855. if (!hpet_default_delta) {
  856. uint64_t clc;
  857. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  858. clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
  859. hpet_default_delta = clc;
  860. }
  861. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  862. delta = hpet_default_delta;
  863. else
  864. delta = hpet_pie_delta;
  865. local_irq_save(flags);
  866. cnt = delta + hpet_readl(HPET_COUNTER);
  867. hpet_writel(cnt, HPET_T1_CMP);
  868. hpet_t1_cmp = cnt;
  869. cfg = hpet_readl(HPET_T1_CFG);
  870. cfg &= ~HPET_TN_PERIODIC;
  871. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  872. hpet_writel(cfg, HPET_T1_CFG);
  873. local_irq_restore(flags);
  874. return 1;
  875. }
  876. EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
  877. /*
  878. * The functions below are called from rtc driver.
  879. * Return 0 if HPET is not being used.
  880. * Otherwise do the necessary changes and return 1.
  881. */
  882. int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
  883. {
  884. if (!is_hpet_enabled())
  885. return 0;
  886. hpet_rtc_flags &= ~bit_mask;
  887. return 1;
  888. }
  889. EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
  890. int hpet_set_rtc_irq_bit(unsigned long bit_mask)
  891. {
  892. unsigned long oldbits = hpet_rtc_flags;
  893. if (!is_hpet_enabled())
  894. return 0;
  895. hpet_rtc_flags |= bit_mask;
  896. if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
  897. hpet_prev_update_sec = -1;
  898. if (!oldbits)
  899. hpet_rtc_timer_init();
  900. return 1;
  901. }
  902. EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
  903. int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
  904. unsigned char sec)
  905. {
  906. if (!is_hpet_enabled())
  907. return 0;
  908. hpet_alarm_time.tm_hour = hrs;
  909. hpet_alarm_time.tm_min = min;
  910. hpet_alarm_time.tm_sec = sec;
  911. return 1;
  912. }
  913. EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
  914. int hpet_set_periodic_freq(unsigned long freq)
  915. {
  916. uint64_t clc;
  917. if (!is_hpet_enabled())
  918. return 0;
  919. if (freq <= DEFAULT_RTC_INT_FREQ)
  920. hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
  921. else {
  922. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  923. do_div(clc, freq);
  924. clc >>= hpet_clockevent.shift;
  925. hpet_pie_delta = clc;
  926. hpet_pie_limit = 0;
  927. }
  928. return 1;
  929. }
  930. EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
  931. int hpet_rtc_dropped_irq(void)
  932. {
  933. return is_hpet_enabled();
  934. }
  935. EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
  936. static void hpet_rtc_timer_reinit(void)
  937. {
  938. unsigned int cfg, delta;
  939. int lost_ints = -1;
  940. if (unlikely(!hpet_rtc_flags)) {
  941. cfg = hpet_readl(HPET_T1_CFG);
  942. cfg &= ~HPET_TN_ENABLE;
  943. hpet_writel(cfg, HPET_T1_CFG);
  944. return;
  945. }
  946. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  947. delta = hpet_default_delta;
  948. else
  949. delta = hpet_pie_delta;
  950. /*
  951. * Increment the comparator value until we are ahead of the
  952. * current count.
  953. */
  954. do {
  955. hpet_t1_cmp += delta;
  956. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  957. lost_ints++;
  958. } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
  959. if (lost_ints) {
  960. if (hpet_rtc_flags & RTC_PIE)
  961. hpet_pie_count += lost_ints;
  962. if (printk_ratelimit())
  963. printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
  964. lost_ints);
  965. }
  966. }
  967. irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
  968. {
  969. struct rtc_time curr_time;
  970. unsigned long rtc_int_flag = 0;
  971. hpet_rtc_timer_reinit();
  972. memset(&curr_time, 0, sizeof(struct rtc_time));
  973. if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
  974. get_rtc_time(&curr_time);
  975. if (hpet_rtc_flags & RTC_UIE &&
  976. curr_time.tm_sec != hpet_prev_update_sec) {
  977. if (hpet_prev_update_sec >= 0)
  978. rtc_int_flag = RTC_UF;
  979. hpet_prev_update_sec = curr_time.tm_sec;
  980. }
  981. if (hpet_rtc_flags & RTC_PIE &&
  982. ++hpet_pie_count >= hpet_pie_limit) {
  983. rtc_int_flag |= RTC_PF;
  984. hpet_pie_count = 0;
  985. }
  986. if (hpet_rtc_flags & RTC_AIE &&
  987. (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
  988. (curr_time.tm_min == hpet_alarm_time.tm_min) &&
  989. (curr_time.tm_hour == hpet_alarm_time.tm_hour))
  990. rtc_int_flag |= RTC_AF;
  991. if (rtc_int_flag) {
  992. rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
  993. if (irq_handler)
  994. irq_handler(rtc_int_flag, dev_id);
  995. }
  996. return IRQ_HANDLED;
  997. }
  998. EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
  999. #endif