hpet.c 27 KB

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