hpet.c 28 KB

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