hpet.c 27 KB

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