hpet.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. inline unsigned int hpet_readl(unsigned int a)
  43. {
  44. return readl(hpet_virt_address + a);
  45. }
  46. static inline void hpet_writel(unsigned int d, unsigned int 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 int 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 int 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 int 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 int 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 int 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 int) 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 int) 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(hpet_readl(HPET_Tn_CMP(timer)) != cnt);
  336. return (s32)(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 int 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 int 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. if (boot_cpu_has(X86_FEATURE_ARAT))
  493. return;
  494. id = hpet_readl(HPET_ID);
  495. num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
  496. num_timers++; /* Value read out starts from 0 */
  497. hpet_print_config();
  498. hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
  499. if (!hpet_devs)
  500. return;
  501. hpet_num_timers = num_timers;
  502. for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
  503. struct hpet_dev *hdev = &hpet_devs[num_timers_used];
  504. unsigned int cfg = hpet_readl(HPET_Tn_CFG(i));
  505. /* Only consider HPET timer with MSI support */
  506. if (!(cfg & HPET_TN_FSB_CAP))
  507. continue;
  508. hdev->flags = 0;
  509. if (cfg & HPET_TN_PERIODIC_CAP)
  510. hdev->flags |= HPET_DEV_PERI_CAP;
  511. hdev->num = i;
  512. sprintf(hdev->name, "hpet%d", i);
  513. if (hpet_assign_irq(hdev))
  514. continue;
  515. hdev->flags |= HPET_DEV_FSB_CAP;
  516. hdev->flags |= HPET_DEV_VALID;
  517. num_timers_used++;
  518. if (num_timers_used == num_possible_cpus())
  519. break;
  520. }
  521. printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
  522. num_timers, num_timers_used);
  523. }
  524. #ifdef CONFIG_HPET
  525. static void hpet_reserve_msi_timers(struct hpet_data *hd)
  526. {
  527. int i;
  528. if (!hpet_devs)
  529. return;
  530. for (i = 0; i < hpet_num_timers; i++) {
  531. struct hpet_dev *hdev = &hpet_devs[i];
  532. if (!(hdev->flags & HPET_DEV_VALID))
  533. continue;
  534. hd->hd_irq[hdev->num] = hdev->irq;
  535. hpet_reserve_timer(hd, hdev->num);
  536. }
  537. }
  538. #endif
  539. static struct hpet_dev *hpet_get_unused_timer(void)
  540. {
  541. int i;
  542. if (!hpet_devs)
  543. return NULL;
  544. for (i = 0; i < hpet_num_timers; i++) {
  545. struct hpet_dev *hdev = &hpet_devs[i];
  546. if (!(hdev->flags & HPET_DEV_VALID))
  547. continue;
  548. if (test_and_set_bit(HPET_DEV_USED_BIT,
  549. (unsigned long *)&hdev->flags))
  550. continue;
  551. return hdev;
  552. }
  553. return NULL;
  554. }
  555. struct hpet_work_struct {
  556. struct delayed_work work;
  557. struct completion complete;
  558. };
  559. static void hpet_work(struct work_struct *w)
  560. {
  561. struct hpet_dev *hdev;
  562. int cpu = smp_processor_id();
  563. struct hpet_work_struct *hpet_work;
  564. hpet_work = container_of(w, struct hpet_work_struct, work.work);
  565. hdev = hpet_get_unused_timer();
  566. if (hdev)
  567. init_one_hpet_msi_clockevent(hdev, cpu);
  568. complete(&hpet_work->complete);
  569. }
  570. static int hpet_cpuhp_notify(struct notifier_block *n,
  571. unsigned long action, void *hcpu)
  572. {
  573. unsigned long cpu = (unsigned long)hcpu;
  574. struct hpet_work_struct work;
  575. struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
  576. switch (action & 0xf) {
  577. case CPU_ONLINE:
  578. INIT_DELAYED_WORK_ON_STACK(&work.work, hpet_work);
  579. init_completion(&work.complete);
  580. /* FIXME: add schedule_work_on() */
  581. schedule_delayed_work_on(cpu, &work.work, 0);
  582. wait_for_completion(&work.complete);
  583. destroy_timer_on_stack(&work.work.timer);
  584. break;
  585. case CPU_DEAD:
  586. if (hdev) {
  587. free_irq(hdev->irq, hdev);
  588. hdev->flags &= ~HPET_DEV_USED;
  589. per_cpu(cpu_hpet_dev, cpu) = NULL;
  590. }
  591. break;
  592. }
  593. return NOTIFY_OK;
  594. }
  595. #else
  596. static int hpet_setup_msi_irq(unsigned int irq)
  597. {
  598. return 0;
  599. }
  600. static void hpet_msi_capability_lookup(unsigned int start_timer)
  601. {
  602. return;
  603. }
  604. #ifdef CONFIG_HPET
  605. static void hpet_reserve_msi_timers(struct hpet_data *hd)
  606. {
  607. return;
  608. }
  609. #endif
  610. static int hpet_cpuhp_notify(struct notifier_block *n,
  611. unsigned long action, void *hcpu)
  612. {
  613. return NOTIFY_OK;
  614. }
  615. #endif
  616. /*
  617. * Clock source related code
  618. */
  619. static cycle_t read_hpet(struct clocksource *cs)
  620. {
  621. return (cycle_t)hpet_readl(HPET_COUNTER);
  622. }
  623. #ifdef CONFIG_X86_64
  624. static cycle_t __vsyscall_fn vread_hpet(void)
  625. {
  626. return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
  627. }
  628. #endif
  629. static struct clocksource clocksource_hpet = {
  630. .name = "hpet",
  631. .rating = 250,
  632. .read = read_hpet,
  633. .mask = HPET_MASK,
  634. .shift = HPET_SHIFT,
  635. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  636. .resume = hpet_resume_counter,
  637. #ifdef CONFIG_X86_64
  638. .vread = vread_hpet,
  639. #endif
  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. rdtscll(start);
  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. rdtscll(now);
  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. /*
  666. * The definition of mult is (include/linux/clocksource.h)
  667. * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
  668. * so we first need to convert hpet_period to ns/cyc units:
  669. * mult/2^shift = ns/cyc = hpet_period/10^6
  670. * mult = (hpet_period * 2^shift)/10^6
  671. * mult = (hpet_period << shift)/FSEC_PER_NSEC
  672. */
  673. clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
  674. clocksource_register(&clocksource_hpet);
  675. return 0;
  676. }
  677. /**
  678. * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
  679. */
  680. int __init hpet_enable(void)
  681. {
  682. unsigned int id;
  683. int i;
  684. if (!is_hpet_capable())
  685. return 0;
  686. hpet_set_mapping();
  687. /*
  688. * Read the period and check for a sane value:
  689. */
  690. hpet_period = hpet_readl(HPET_PERIOD);
  691. /*
  692. * AMD SB700 based systems with spread spectrum enabled use a
  693. * SMM based HPET emulation to provide proper frequency
  694. * setting. The SMM code is initialized with the first HPET
  695. * register access and takes some time to complete. During
  696. * this time the config register reads 0xffffffff. We check
  697. * for max. 1000 loops whether the config register reads a non
  698. * 0xffffffff value to make sure that HPET is up and running
  699. * before we go further. A counting loop is safe, as the HPET
  700. * access takes thousands of CPU cycles. On non SB700 based
  701. * machines this check is only done once and has no side
  702. * effects.
  703. */
  704. for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
  705. if (i == 1000) {
  706. printk(KERN_WARNING
  707. "HPET config register value = 0xFFFFFFFF. "
  708. "Disabling HPET\n");
  709. goto out_nohpet;
  710. }
  711. }
  712. if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
  713. goto out_nohpet;
  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. #ifdef CONFIG_HPET_EMULATE_RTC
  721. /*
  722. * The legacy routing mode needs at least two channels, tick timer
  723. * and the rtc emulation channel.
  724. */
  725. if (!(id & HPET_ID_NUMBER))
  726. goto out_nohpet;
  727. #endif
  728. if (hpet_clocksource_register())
  729. goto out_nohpet;
  730. if (id & HPET_ID_LEGSUP) {
  731. hpet_legacy_clockevent_register();
  732. return 1;
  733. }
  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. if (hpet_readl(HPET_ID) & HPET_ID_LEGSUP)
  760. hpet_msi_capability_lookup(2);
  761. else
  762. hpet_msi_capability_lookup(0);
  763. hpet_reserve_platform_timers(hpet_readl(HPET_ID));
  764. hpet_print_config();
  765. if (boot_cpu_has(X86_FEATURE_ARAT))
  766. return 0;
  767. for_each_online_cpu(cpu) {
  768. hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
  769. }
  770. /* This notifier should be called after workqueue is ready */
  771. hotcpu_notifier(hpet_cpuhp_notify, -20);
  772. return 0;
  773. }
  774. fs_initcall(hpet_late_init);
  775. void hpet_disable(void)
  776. {
  777. if (is_hpet_capable()) {
  778. unsigned int cfg = hpet_readl(HPET_CFG);
  779. if (hpet_legacy_int_enabled) {
  780. cfg &= ~HPET_CFG_LEGACY;
  781. hpet_legacy_int_enabled = 0;
  782. }
  783. cfg &= ~HPET_CFG_ENABLE;
  784. hpet_writel(cfg, HPET_CFG);
  785. }
  786. }
  787. #ifdef CONFIG_HPET_EMULATE_RTC
  788. /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
  789. * is enabled, we support RTC interrupt functionality in software.
  790. * RTC has 3 kinds of interrupts:
  791. * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
  792. * is updated
  793. * 2) Alarm Interrupt - generate an interrupt at a specific time of day
  794. * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
  795. * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
  796. * (1) and (2) above are implemented using polling at a frequency of
  797. * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
  798. * overhead. (DEFAULT_RTC_INT_FREQ)
  799. * For (3), we use interrupts at 64Hz or user specified periodic
  800. * frequency, whichever is higher.
  801. */
  802. #include <linux/mc146818rtc.h>
  803. #include <linux/rtc.h>
  804. #include <asm/rtc.h>
  805. #define DEFAULT_RTC_INT_FREQ 64
  806. #define DEFAULT_RTC_SHIFT 6
  807. #define RTC_NUM_INTS 1
  808. static unsigned long hpet_rtc_flags;
  809. static int hpet_prev_update_sec;
  810. static struct rtc_time hpet_alarm_time;
  811. static unsigned long hpet_pie_count;
  812. static u32 hpet_t1_cmp;
  813. static u32 hpet_default_delta;
  814. static u32 hpet_pie_delta;
  815. static unsigned long hpet_pie_limit;
  816. static rtc_irq_handler irq_handler;
  817. /*
  818. * Check that the hpet counter c1 is ahead of the c2
  819. */
  820. static inline int hpet_cnt_ahead(u32 c1, u32 c2)
  821. {
  822. return (s32)(c2 - c1) < 0;
  823. }
  824. /*
  825. * Registers a IRQ handler.
  826. */
  827. int hpet_register_irq_handler(rtc_irq_handler handler)
  828. {
  829. if (!is_hpet_enabled())
  830. return -ENODEV;
  831. if (irq_handler)
  832. return -EBUSY;
  833. irq_handler = handler;
  834. return 0;
  835. }
  836. EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
  837. /*
  838. * Deregisters the IRQ handler registered with hpet_register_irq_handler()
  839. * and does cleanup.
  840. */
  841. void hpet_unregister_irq_handler(rtc_irq_handler handler)
  842. {
  843. if (!is_hpet_enabled())
  844. return;
  845. irq_handler = NULL;
  846. hpet_rtc_flags = 0;
  847. }
  848. EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
  849. /*
  850. * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
  851. * is not supported by all HPET implementations for timer 1.
  852. *
  853. * hpet_rtc_timer_init() is called when the rtc is initialized.
  854. */
  855. int hpet_rtc_timer_init(void)
  856. {
  857. unsigned int cfg, cnt, delta;
  858. unsigned long flags;
  859. if (!is_hpet_enabled())
  860. return 0;
  861. if (!hpet_default_delta) {
  862. uint64_t clc;
  863. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  864. clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
  865. hpet_default_delta = clc;
  866. }
  867. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  868. delta = hpet_default_delta;
  869. else
  870. delta = hpet_pie_delta;
  871. local_irq_save(flags);
  872. cnt = delta + hpet_readl(HPET_COUNTER);
  873. hpet_writel(cnt, HPET_T1_CMP);
  874. hpet_t1_cmp = cnt;
  875. cfg = hpet_readl(HPET_T1_CFG);
  876. cfg &= ~HPET_TN_PERIODIC;
  877. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  878. hpet_writel(cfg, HPET_T1_CFG);
  879. local_irq_restore(flags);
  880. return 1;
  881. }
  882. EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
  883. /*
  884. * The functions below are called from rtc driver.
  885. * Return 0 if HPET is not being used.
  886. * Otherwise do the necessary changes and return 1.
  887. */
  888. int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
  889. {
  890. if (!is_hpet_enabled())
  891. return 0;
  892. hpet_rtc_flags &= ~bit_mask;
  893. return 1;
  894. }
  895. EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
  896. int hpet_set_rtc_irq_bit(unsigned long bit_mask)
  897. {
  898. unsigned long oldbits = hpet_rtc_flags;
  899. if (!is_hpet_enabled())
  900. return 0;
  901. hpet_rtc_flags |= bit_mask;
  902. if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
  903. hpet_prev_update_sec = -1;
  904. if (!oldbits)
  905. hpet_rtc_timer_init();
  906. return 1;
  907. }
  908. EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
  909. int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
  910. unsigned char sec)
  911. {
  912. if (!is_hpet_enabled())
  913. return 0;
  914. hpet_alarm_time.tm_hour = hrs;
  915. hpet_alarm_time.tm_min = min;
  916. hpet_alarm_time.tm_sec = sec;
  917. return 1;
  918. }
  919. EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
  920. int hpet_set_periodic_freq(unsigned long freq)
  921. {
  922. uint64_t clc;
  923. if (!is_hpet_enabled())
  924. return 0;
  925. if (freq <= DEFAULT_RTC_INT_FREQ)
  926. hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
  927. else {
  928. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  929. do_div(clc, freq);
  930. clc >>= hpet_clockevent.shift;
  931. hpet_pie_delta = clc;
  932. }
  933. return 1;
  934. }
  935. EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
  936. int hpet_rtc_dropped_irq(void)
  937. {
  938. return is_hpet_enabled();
  939. }
  940. EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
  941. static void hpet_rtc_timer_reinit(void)
  942. {
  943. unsigned int cfg, delta;
  944. int lost_ints = -1;
  945. if (unlikely(!hpet_rtc_flags)) {
  946. cfg = hpet_readl(HPET_T1_CFG);
  947. cfg &= ~HPET_TN_ENABLE;
  948. hpet_writel(cfg, HPET_T1_CFG);
  949. return;
  950. }
  951. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  952. delta = hpet_default_delta;
  953. else
  954. delta = hpet_pie_delta;
  955. /*
  956. * Increment the comparator value until we are ahead of the
  957. * current count.
  958. */
  959. do {
  960. hpet_t1_cmp += delta;
  961. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  962. lost_ints++;
  963. } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
  964. if (lost_ints) {
  965. if (hpet_rtc_flags & RTC_PIE)
  966. hpet_pie_count += lost_ints;
  967. if (printk_ratelimit())
  968. printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
  969. lost_ints);
  970. }
  971. }
  972. irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
  973. {
  974. struct rtc_time curr_time;
  975. unsigned long rtc_int_flag = 0;
  976. hpet_rtc_timer_reinit();
  977. memset(&curr_time, 0, sizeof(struct rtc_time));
  978. if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
  979. get_rtc_time(&curr_time);
  980. if (hpet_rtc_flags & RTC_UIE &&
  981. curr_time.tm_sec != hpet_prev_update_sec) {
  982. if (hpet_prev_update_sec >= 0)
  983. rtc_int_flag = RTC_UF;
  984. hpet_prev_update_sec = curr_time.tm_sec;
  985. }
  986. if (hpet_rtc_flags & RTC_PIE &&
  987. ++hpet_pie_count >= hpet_pie_limit) {
  988. rtc_int_flag |= RTC_PF;
  989. hpet_pie_count = 0;
  990. }
  991. if (hpet_rtc_flags & RTC_AIE &&
  992. (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
  993. (curr_time.tm_min == hpet_alarm_time.tm_min) &&
  994. (curr_time.tm_hour == hpet_alarm_time.tm_hour))
  995. rtc_int_flag |= RTC_AF;
  996. if (rtc_int_flag) {
  997. rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
  998. if (irq_handler)
  999. irq_handler(rtc_int_flag, dev_id);
  1000. }
  1001. return IRQ_HANDLED;
  1002. }
  1003. EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
  1004. #endif