hpet.c 29 KB

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