hpet.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Intel & MS High Precision Event Timer Implementation.
  4. *
  5. * Copyright (C) 2003 Intel Corporation
  6. * Venki Pallipadi
  7. * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
  8. * Bob Picco <robert.picco@hp.com>
  9. */
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/miscdevice.h>
  14. #include <linux/major.h>
  15. #include <linux/ioport.h>
  16. #include <linux/fcntl.h>
  17. #include <linux/init.h>
  18. #include <linux/io-64-nonatomic-lo-hi.h>
  19. #include <linux/poll.h>
  20. #include <linux/mm.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/sysctl.h>
  24. #include <linux/wait.h>
  25. #include <linux/sched/signal.h>
  26. #include <linux/bcd.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/bitops.h>
  29. #include <linux/compat.h>
  30. #include <linux/clocksource.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/slab.h>
  33. #include <linux/io.h>
  34. #include <linux/acpi.h>
  35. #include <linux/hpet.h>
  36. #include <asm/current.h>
  37. #include <asm/irq.h>
  38. #include <asm/div64.h>
  39. /*
  40. * The High Precision Event Timer driver.
  41. * This driver is closely modelled after the rtc.c driver.
  42. * See HPET spec revision 1.
  43. */
  44. #define HPET_USER_FREQ (64)
  45. #define HPET_DRIFT (500)
  46. #define HPET_RANGE_SIZE 1024 /* from HPET spec */
  47. /* WARNING -- don't get confused. These macros are never used
  48. * to write the (single) counter, and rarely to read it.
  49. * They're badly named; to fix, someday.
  50. */
  51. #if BITS_PER_LONG == 64
  52. #define write_counter(V, MC) writeq(V, MC)
  53. #define read_counter(MC) readq(MC)
  54. #else
  55. #define write_counter(V, MC) writel(V, MC)
  56. #define read_counter(MC) readl(MC)
  57. #endif
  58. static DEFINE_MUTEX(hpet_mutex); /* replaces BKL */
  59. static u32 hpet_nhpet, hpet_max_freq = HPET_USER_FREQ;
  60. /* A lock for concurrent access by app and isr hpet activity. */
  61. static DEFINE_SPINLOCK(hpet_lock);
  62. #define HPET_DEV_NAME (7)
  63. struct hpet_dev {
  64. struct hpets *hd_hpets;
  65. struct hpet __iomem *hd_hpet;
  66. struct hpet_timer __iomem *hd_timer;
  67. unsigned long hd_ireqfreq;
  68. unsigned long hd_irqdata;
  69. wait_queue_head_t hd_waitqueue;
  70. struct fasync_struct *hd_async_queue;
  71. unsigned int hd_flags;
  72. unsigned int hd_irq;
  73. unsigned int hd_hdwirq;
  74. char hd_name[HPET_DEV_NAME];
  75. };
  76. struct hpets {
  77. struct hpets *hp_next;
  78. struct hpet __iomem *hp_hpet;
  79. unsigned long hp_hpet_phys;
  80. unsigned long long hp_tick_freq;
  81. unsigned long hp_delta;
  82. unsigned int hp_ntimer;
  83. unsigned int hp_which;
  84. struct hpet_dev hp_dev[] __counted_by(hp_ntimer);
  85. };
  86. static struct hpets *hpets;
  87. #define HPET_OPEN 0x0001
  88. #define HPET_IE 0x0002 /* interrupt enabled */
  89. #define HPET_PERIODIC 0x0004
  90. #define HPET_SHARED_IRQ 0x0008
  91. static irqreturn_t hpet_interrupt(int irq, void *data)
  92. {
  93. struct hpet_dev *devp;
  94. unsigned long isr;
  95. devp = data;
  96. isr = 1 << (devp - devp->hd_hpets->hp_dev);
  97. if ((devp->hd_flags & HPET_SHARED_IRQ) &&
  98. !(isr & readl(&devp->hd_hpet->hpet_isr)))
  99. return IRQ_NONE;
  100. spin_lock(&hpet_lock);
  101. devp->hd_irqdata++;
  102. /*
  103. * For non-periodic timers, increment the accumulator.
  104. * This has the effect of treating non-periodic like periodic.
  105. */
  106. if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) {
  107. unsigned long t, mc, base, k;
  108. struct hpet __iomem *hpet = devp->hd_hpet;
  109. struct hpets *hpetp = devp->hd_hpets;
  110. t = devp->hd_ireqfreq;
  111. read_counter(&devp->hd_timer->hpet_compare);
  112. mc = read_counter(&hpet->hpet_mc);
  113. /* The time for the next interrupt would logically be t + m,
  114. * however, if we are very unlucky and the interrupt is delayed
  115. * for longer than t then we will completely miss the next
  116. * interrupt if we set t + m and an application will hang.
  117. * Therefore we need to make a more complex computation assuming
  118. * that there exists a k for which the following is true:
  119. * k * t + base < mc + delta
  120. * (k + 1) * t + base > mc + delta
  121. * where t is the interval in hpet ticks for the given freq,
  122. * base is the theoretical start value 0 < base < t,
  123. * mc is the main counter value at the time of the interrupt,
  124. * delta is the time it takes to write the a value to the
  125. * comparator.
  126. * k may then be computed as (mc - base + delta) / t .
  127. */
  128. base = mc % t;
  129. k = (mc - base + hpetp->hp_delta) / t;
  130. write_counter(t * (k + 1) + base,
  131. &devp->hd_timer->hpet_compare);
  132. }
  133. if (devp->hd_flags & HPET_SHARED_IRQ)
  134. writel(isr, &devp->hd_hpet->hpet_isr);
  135. spin_unlock(&hpet_lock);
  136. wake_up_interruptible(&devp->hd_waitqueue);
  137. kill_fasync(&devp->hd_async_queue, SIGIO, POLL_IN);
  138. return IRQ_HANDLED;
  139. }
  140. static void hpet_timer_set_irq(struct hpet_dev *devp)
  141. {
  142. const unsigned int nr_irqs = irq_get_nr_irqs();
  143. unsigned long v;
  144. int irq, gsi;
  145. struct hpet_timer __iomem *timer;
  146. spin_lock_irq(&hpet_lock);
  147. if (devp->hd_hdwirq) {
  148. spin_unlock_irq(&hpet_lock);
  149. return;
  150. }
  151. timer = devp->hd_timer;
  152. /* we prefer level triggered mode */
  153. v = readl(&timer->hpet_config);
  154. if (!(v & Tn_INT_TYPE_CNF_MASK)) {
  155. v |= Tn_INT_TYPE_CNF_MASK;
  156. writel(v, &timer->hpet_config);
  157. }
  158. spin_unlock_irq(&hpet_lock);
  159. v = (readq(&timer->hpet_config) & Tn_INT_ROUTE_CAP_MASK) >>
  160. Tn_INT_ROUTE_CAP_SHIFT;
  161. /*
  162. * In PIC mode, skip IRQ0-4, IRQ6-9, IRQ12-15 which is always used by
  163. * legacy device. In IO APIC mode, we skip all the legacy IRQS.
  164. */
  165. if (acpi_irq_model == ACPI_IRQ_MODEL_PIC)
  166. v &= ~0xf3df;
  167. else
  168. v &= ~0xffff;
  169. for_each_set_bit(irq, &v, HPET_MAX_IRQ) {
  170. if (irq >= nr_irqs) {
  171. irq = HPET_MAX_IRQ;
  172. break;
  173. }
  174. gsi = acpi_register_gsi(NULL, irq, ACPI_LEVEL_SENSITIVE,
  175. ACPI_ACTIVE_LOW);
  176. if (gsi > 0)
  177. break;
  178. /* FIXME: Setup interrupt source table */
  179. }
  180. if (irq < HPET_MAX_IRQ) {
  181. spin_lock_irq(&hpet_lock);
  182. v = readl(&timer->hpet_config);
  183. v |= irq << Tn_INT_ROUTE_CNF_SHIFT;
  184. writel(v, &timer->hpet_config);
  185. devp->hd_hdwirq = gsi;
  186. spin_unlock_irq(&hpet_lock);
  187. }
  188. return;
  189. }
  190. static int hpet_open(struct inode *inode, struct file *file)
  191. {
  192. struct hpet_dev *devp;
  193. struct hpets *hpetp;
  194. int i;
  195. if (file->f_mode & FMODE_WRITE)
  196. return -EINVAL;
  197. mutex_lock(&hpet_mutex);
  198. spin_lock_irq(&hpet_lock);
  199. for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
  200. for (i = 0; i < hpetp->hp_ntimer; i++)
  201. if (hpetp->hp_dev[i].hd_flags & HPET_OPEN) {
  202. continue;
  203. } else {
  204. devp = &hpetp->hp_dev[i];
  205. break;
  206. }
  207. if (!devp) {
  208. spin_unlock_irq(&hpet_lock);
  209. mutex_unlock(&hpet_mutex);
  210. return -EBUSY;
  211. }
  212. file->private_data = devp;
  213. devp->hd_irqdata = 0;
  214. devp->hd_flags |= HPET_OPEN;
  215. spin_unlock_irq(&hpet_lock);
  216. mutex_unlock(&hpet_mutex);
  217. hpet_timer_set_irq(devp);
  218. return 0;
  219. }
  220. static ssize_t
  221. hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
  222. {
  223. DECLARE_WAITQUEUE(wait, current);
  224. unsigned long data;
  225. ssize_t retval;
  226. struct hpet_dev *devp;
  227. devp = file->private_data;
  228. if (!devp->hd_ireqfreq)
  229. return -EIO;
  230. if (in_compat_syscall()) {
  231. if (count < sizeof(compat_ulong_t))
  232. return -EINVAL;
  233. } else {
  234. if (count < sizeof(unsigned long))
  235. return -EINVAL;
  236. }
  237. add_wait_queue(&devp->hd_waitqueue, &wait);
  238. for ( ; ; ) {
  239. set_current_state(TASK_INTERRUPTIBLE);
  240. spin_lock_irq(&hpet_lock);
  241. data = devp->hd_irqdata;
  242. devp->hd_irqdata = 0;
  243. spin_unlock_irq(&hpet_lock);
  244. if (data) {
  245. break;
  246. } else if (file->f_flags & O_NONBLOCK) {
  247. retval = -EAGAIN;
  248. goto out;
  249. } else if (signal_pending(current)) {
  250. retval = -ERESTARTSYS;
  251. goto out;
  252. }
  253. schedule();
  254. }
  255. if (in_compat_syscall()) {
  256. retval = put_user(data, (compat_ulong_t __user *)buf);
  257. if (!retval)
  258. retval = sizeof(compat_ulong_t);
  259. } else {
  260. retval = put_user(data, (unsigned long __user *)buf);
  261. if (!retval)
  262. retval = sizeof(unsigned long);
  263. }
  264. out:
  265. __set_current_state(TASK_RUNNING);
  266. remove_wait_queue(&devp->hd_waitqueue, &wait);
  267. return retval;
  268. }
  269. static __poll_t hpet_poll(struct file *file, poll_table * wait)
  270. {
  271. unsigned long v;
  272. struct hpet_dev *devp;
  273. devp = file->private_data;
  274. if (!devp->hd_ireqfreq)
  275. return 0;
  276. poll_wait(file, &devp->hd_waitqueue, wait);
  277. spin_lock_irq(&hpet_lock);
  278. v = devp->hd_irqdata;
  279. spin_unlock_irq(&hpet_lock);
  280. if (v != 0)
  281. return EPOLLIN | EPOLLRDNORM;
  282. return 0;
  283. }
  284. #ifdef CONFIG_HPET_MMAP
  285. #ifdef CONFIG_HPET_MMAP_DEFAULT
  286. static int hpet_mmap_enabled = 1;
  287. #else
  288. static int hpet_mmap_enabled = 0;
  289. #endif
  290. static __init int hpet_mmap_enable(char *str)
  291. {
  292. get_option(&str, &hpet_mmap_enabled);
  293. pr_info("HPET mmap %s\n", hpet_mmap_enabled ? "enabled" : "disabled");
  294. return 1;
  295. }
  296. __setup("hpet_mmap=", hpet_mmap_enable);
  297. static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
  298. {
  299. struct hpet_dev *devp;
  300. unsigned long addr;
  301. if (!hpet_mmap_enabled)
  302. return -EACCES;
  303. devp = file->private_data;
  304. addr = devp->hd_hpets->hp_hpet_phys;
  305. if (addr & (PAGE_SIZE - 1))
  306. return -ENOSYS;
  307. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  308. return vm_iomap_memory(vma, addr, PAGE_SIZE);
  309. }
  310. #else
  311. static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
  312. {
  313. return -ENOSYS;
  314. }
  315. #endif
  316. static int hpet_fasync(int fd, struct file *file, int on)
  317. {
  318. struct hpet_dev *devp;
  319. devp = file->private_data;
  320. if (fasync_helper(fd, file, on, &devp->hd_async_queue) >= 0)
  321. return 0;
  322. else
  323. return -EIO;
  324. }
  325. static int hpet_release(struct inode *inode, struct file *file)
  326. {
  327. struct hpet_dev *devp;
  328. struct hpet_timer __iomem *timer;
  329. int irq = 0;
  330. devp = file->private_data;
  331. timer = devp->hd_timer;
  332. spin_lock_irq(&hpet_lock);
  333. writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
  334. &timer->hpet_config);
  335. irq = devp->hd_irq;
  336. devp->hd_irq = 0;
  337. devp->hd_ireqfreq = 0;
  338. if (devp->hd_flags & HPET_PERIODIC
  339. && readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
  340. unsigned long v;
  341. v = readq(&timer->hpet_config);
  342. v ^= Tn_TYPE_CNF_MASK;
  343. writeq(v, &timer->hpet_config);
  344. }
  345. devp->hd_flags &= ~(HPET_OPEN | HPET_IE | HPET_PERIODIC);
  346. spin_unlock_irq(&hpet_lock);
  347. if (irq)
  348. free_irq(irq, devp);
  349. file->private_data = NULL;
  350. return 0;
  351. }
  352. static int hpet_ioctl_ieon(struct hpet_dev *devp)
  353. {
  354. struct hpet_timer __iomem *timer;
  355. struct hpet __iomem *hpet;
  356. struct hpets *hpetp;
  357. int irq;
  358. unsigned long g, v, t, m;
  359. unsigned long flags, isr;
  360. timer = devp->hd_timer;
  361. hpet = devp->hd_hpet;
  362. hpetp = devp->hd_hpets;
  363. if (!devp->hd_ireqfreq)
  364. return -EIO;
  365. spin_lock_irq(&hpet_lock);
  366. if (devp->hd_flags & HPET_IE) {
  367. spin_unlock_irq(&hpet_lock);
  368. return -EBUSY;
  369. }
  370. devp->hd_flags |= HPET_IE;
  371. if (readl(&timer->hpet_config) & Tn_INT_TYPE_CNF_MASK)
  372. devp->hd_flags |= HPET_SHARED_IRQ;
  373. spin_unlock_irq(&hpet_lock);
  374. irq = devp->hd_hdwirq;
  375. if (irq) {
  376. unsigned long irq_flags;
  377. if (devp->hd_flags & HPET_SHARED_IRQ) {
  378. /*
  379. * To prevent the interrupt handler from seeing an
  380. * unwanted interrupt status bit, program the timer
  381. * so that it will not fire in the near future ...
  382. */
  383. writel(readl(&timer->hpet_config) & ~Tn_TYPE_CNF_MASK,
  384. &timer->hpet_config);
  385. write_counter(read_counter(&hpet->hpet_mc),
  386. &timer->hpet_compare);
  387. /* ... and clear any left-over status. */
  388. isr = 1 << (devp - devp->hd_hpets->hp_dev);
  389. writel(isr, &hpet->hpet_isr);
  390. }
  391. sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
  392. irq_flags = devp->hd_flags & HPET_SHARED_IRQ ? IRQF_SHARED : 0;
  393. if (request_irq(irq, hpet_interrupt, irq_flags,
  394. devp->hd_name, (void *)devp)) {
  395. printk(KERN_ERR "hpet: IRQ %d is not free\n", irq);
  396. irq = 0;
  397. }
  398. }
  399. if (irq == 0) {
  400. spin_lock_irq(&hpet_lock);
  401. devp->hd_flags ^= HPET_IE;
  402. spin_unlock_irq(&hpet_lock);
  403. return -EIO;
  404. }
  405. devp->hd_irq = irq;
  406. t = devp->hd_ireqfreq;
  407. v = readq(&timer->hpet_config);
  408. /* 64-bit comparators are not yet supported through the ioctls,
  409. * so force this into 32-bit mode if it supports both modes
  410. */
  411. g = v | Tn_32MODE_CNF_MASK | Tn_INT_ENB_CNF_MASK;
  412. if (devp->hd_flags & HPET_PERIODIC) {
  413. g |= Tn_TYPE_CNF_MASK;
  414. v |= Tn_TYPE_CNF_MASK | Tn_VAL_SET_CNF_MASK;
  415. writeq(v, &timer->hpet_config);
  416. local_irq_save(flags);
  417. /*
  418. * NOTE: First we modify the hidden accumulator
  419. * register supported by periodic-capable comparators.
  420. * We never want to modify the (single) counter; that
  421. * would affect all the comparators. The value written
  422. * is the counter value when the first interrupt is due.
  423. */
  424. m = read_counter(&hpet->hpet_mc);
  425. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  426. /*
  427. * Then we modify the comparator, indicating the period
  428. * for subsequent interrupt.
  429. */
  430. write_counter(t, &timer->hpet_compare);
  431. } else {
  432. local_irq_save(flags);
  433. m = read_counter(&hpet->hpet_mc);
  434. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  435. }
  436. if (devp->hd_flags & HPET_SHARED_IRQ) {
  437. isr = 1 << (devp - devp->hd_hpets->hp_dev);
  438. writel(isr, &hpet->hpet_isr);
  439. }
  440. writeq(g, &timer->hpet_config);
  441. local_irq_restore(flags);
  442. return 0;
  443. }
  444. /* converts Hz to number of timer ticks */
  445. static inline unsigned long hpet_time_div(struct hpets *hpets,
  446. unsigned long dis)
  447. {
  448. unsigned long long m;
  449. m = hpets->hp_tick_freq + (dis >> 1);
  450. return div64_ul(m, dis);
  451. }
  452. static int
  453. hpet_ioctl_common(struct hpet_dev *devp, unsigned int cmd, unsigned long arg,
  454. struct hpet_info *info)
  455. {
  456. struct hpet_timer __iomem *timer;
  457. struct hpets *hpetp;
  458. int err;
  459. unsigned long v;
  460. switch (cmd) {
  461. case HPET_IE_OFF:
  462. case HPET_INFO:
  463. case HPET_EPI:
  464. case HPET_DPI:
  465. case HPET_IRQFREQ:
  466. timer = devp->hd_timer;
  467. hpetp = devp->hd_hpets;
  468. break;
  469. case HPET_IE_ON:
  470. return hpet_ioctl_ieon(devp);
  471. default:
  472. return -EINVAL;
  473. }
  474. err = 0;
  475. switch (cmd) {
  476. case HPET_IE_OFF:
  477. if ((devp->hd_flags & HPET_IE) == 0)
  478. break;
  479. v = readq(&timer->hpet_config);
  480. v &= ~Tn_INT_ENB_CNF_MASK;
  481. writeq(v, &timer->hpet_config);
  482. if (devp->hd_irq) {
  483. free_irq(devp->hd_irq, devp);
  484. devp->hd_irq = 0;
  485. }
  486. devp->hd_flags ^= HPET_IE;
  487. break;
  488. case HPET_INFO:
  489. {
  490. memset(info, 0, sizeof(*info));
  491. if (devp->hd_ireqfreq)
  492. info->hi_ireqfreq =
  493. hpet_time_div(hpetp, devp->hd_ireqfreq);
  494. info->hi_flags =
  495. readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK;
  496. info->hi_hpet = hpetp->hp_which;
  497. info->hi_timer = devp - hpetp->hp_dev;
  498. break;
  499. }
  500. case HPET_EPI:
  501. v = readq(&timer->hpet_config);
  502. if ((v & Tn_PER_INT_CAP_MASK) == 0) {
  503. err = -ENXIO;
  504. break;
  505. }
  506. devp->hd_flags |= HPET_PERIODIC;
  507. break;
  508. case HPET_DPI:
  509. v = readq(&timer->hpet_config);
  510. if ((v & Tn_PER_INT_CAP_MASK) == 0) {
  511. err = -ENXIO;
  512. break;
  513. }
  514. if (devp->hd_flags & HPET_PERIODIC &&
  515. readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
  516. v = readq(&timer->hpet_config);
  517. v ^= Tn_TYPE_CNF_MASK;
  518. writeq(v, &timer->hpet_config);
  519. }
  520. devp->hd_flags &= ~HPET_PERIODIC;
  521. break;
  522. case HPET_IRQFREQ:
  523. if ((arg > hpet_max_freq) &&
  524. !capable(CAP_SYS_RESOURCE)) {
  525. err = -EACCES;
  526. break;
  527. }
  528. if (!arg) {
  529. err = -EINVAL;
  530. break;
  531. }
  532. devp->hd_ireqfreq = hpet_time_div(hpetp, arg);
  533. }
  534. return err;
  535. }
  536. static long
  537. hpet_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  538. {
  539. struct hpet_info info;
  540. int err;
  541. mutex_lock(&hpet_mutex);
  542. err = hpet_ioctl_common(file->private_data, cmd, arg, &info);
  543. mutex_unlock(&hpet_mutex);
  544. if ((cmd == HPET_INFO) && !err &&
  545. (copy_to_user((void __user *)arg, &info, sizeof(info))))
  546. err = -EFAULT;
  547. return err;
  548. }
  549. #ifdef CONFIG_COMPAT
  550. struct compat_hpet_info {
  551. compat_ulong_t hi_ireqfreq; /* Hz */
  552. compat_ulong_t hi_flags; /* information */
  553. unsigned short hi_hpet;
  554. unsigned short hi_timer;
  555. };
  556. /* 32-bit types would lead to different command codes which should be
  557. * translated into 64-bit ones before passed to hpet_ioctl_common
  558. */
  559. #define COMPAT_HPET_INFO _IOR('h', 0x03, struct compat_hpet_info)
  560. #define COMPAT_HPET_IRQFREQ _IOW('h', 0x6, compat_ulong_t)
  561. static long
  562. hpet_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  563. {
  564. struct hpet_info info;
  565. int err;
  566. if (cmd == COMPAT_HPET_INFO)
  567. cmd = HPET_INFO;
  568. if (cmd == COMPAT_HPET_IRQFREQ)
  569. cmd = HPET_IRQFREQ;
  570. mutex_lock(&hpet_mutex);
  571. err = hpet_ioctl_common(file->private_data, cmd, arg, &info);
  572. mutex_unlock(&hpet_mutex);
  573. if ((cmd == HPET_INFO) && !err) {
  574. struct compat_hpet_info __user *u = compat_ptr(arg);
  575. if (put_user(info.hi_ireqfreq, &u->hi_ireqfreq) ||
  576. put_user(info.hi_flags, &u->hi_flags) ||
  577. put_user(info.hi_hpet, &u->hi_hpet) ||
  578. put_user(info.hi_timer, &u->hi_timer))
  579. err = -EFAULT;
  580. }
  581. return err;
  582. }
  583. #endif
  584. static const struct file_operations hpet_fops = {
  585. .owner = THIS_MODULE,
  586. .read = hpet_read,
  587. .poll = hpet_poll,
  588. .unlocked_ioctl = hpet_ioctl,
  589. #ifdef CONFIG_COMPAT
  590. .compat_ioctl = hpet_compat_ioctl,
  591. #endif
  592. .open = hpet_open,
  593. .release = hpet_release,
  594. .fasync = hpet_fasync,
  595. .mmap = hpet_mmap,
  596. };
  597. static int hpet_is_known(struct hpet_data *hdp)
  598. {
  599. struct hpets *hpetp;
  600. for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
  601. if (hpetp->hp_hpet_phys == hdp->hd_phys_address)
  602. return 1;
  603. return 0;
  604. }
  605. static const struct ctl_table hpet_table[] = {
  606. {
  607. .procname = "max-user-freq",
  608. .data = &hpet_max_freq,
  609. .maxlen = sizeof(int),
  610. .mode = 0644,
  611. .proc_handler = proc_dointvec,
  612. },
  613. };
  614. static struct ctl_table_header *sysctl_header;
  615. /*
  616. * Adjustment for when arming the timer with
  617. * initial conditions. That is, main counter
  618. * ticks expired before interrupts are enabled.
  619. */
  620. #define TICK_CALIBRATE (1000UL)
  621. static unsigned long __hpet_calibrate(struct hpets *hpetp)
  622. {
  623. struct hpet_timer __iomem *timer = NULL;
  624. unsigned long t, m, count, i, flags, start;
  625. struct hpet_dev *devp;
  626. int j;
  627. struct hpet __iomem *hpet;
  628. for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
  629. if ((devp->hd_flags & HPET_OPEN) == 0) {
  630. timer = devp->hd_timer;
  631. break;
  632. }
  633. if (!timer)
  634. return 0;
  635. hpet = hpetp->hp_hpet;
  636. t = read_counter(&timer->hpet_compare);
  637. i = 0;
  638. count = hpet_time_div(hpetp, TICK_CALIBRATE);
  639. local_irq_save(flags);
  640. start = read_counter(&hpet->hpet_mc);
  641. do {
  642. m = read_counter(&hpet->hpet_mc);
  643. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  644. } while (i++, (m - start) < count);
  645. local_irq_restore(flags);
  646. return (m - start) / i;
  647. }
  648. static unsigned long hpet_calibrate(struct hpets *hpetp)
  649. {
  650. unsigned long ret = ~0UL;
  651. unsigned long tmp;
  652. /*
  653. * Try to calibrate until return value becomes stable small value.
  654. * If SMI interruption occurs in calibration loop, the return value
  655. * will be big. This avoids its impact.
  656. */
  657. for ( ; ; ) {
  658. tmp = __hpet_calibrate(hpetp);
  659. if (ret <= tmp)
  660. break;
  661. ret = tmp;
  662. }
  663. return ret;
  664. }
  665. int hpet_alloc(struct hpet_data *hdp)
  666. {
  667. u64 cap, mcfg;
  668. struct hpet_dev *devp;
  669. u32 i, ntimer;
  670. struct hpets *hpetp;
  671. struct hpet __iomem *hpet;
  672. static struct hpets *last;
  673. u32 period;
  674. unsigned long long temp;
  675. u32 remainder;
  676. /*
  677. * hpet_alloc can be called by platform dependent code.
  678. * If platform dependent code has allocated the hpet that
  679. * ACPI has also reported, then we catch it here.
  680. */
  681. if (hpet_is_known(hdp)) {
  682. printk(KERN_DEBUG "%s: duplicate HPET ignored\n",
  683. __func__);
  684. return 0;
  685. }
  686. hpetp = kzalloc_flex(*hpetp, hp_dev, hdp->hd_nirqs);
  687. if (!hpetp)
  688. return -ENOMEM;
  689. hpetp->hp_which = hpet_nhpet++;
  690. hpetp->hp_hpet = hdp->hd_address;
  691. hpetp->hp_hpet_phys = hdp->hd_phys_address;
  692. hpetp->hp_ntimer = hdp->hd_nirqs;
  693. for (i = 0; i < hdp->hd_nirqs; i++)
  694. hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
  695. hpet = hpetp->hp_hpet;
  696. cap = readq(&hpet->hpet_cap);
  697. ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
  698. if (hpetp->hp_ntimer != ntimer) {
  699. printk(KERN_WARNING "hpet: number irqs doesn't agree"
  700. " with number of timers\n");
  701. kfree(hpetp);
  702. return -ENODEV;
  703. }
  704. if (last)
  705. last->hp_next = hpetp;
  706. else
  707. hpets = hpetp;
  708. last = hpetp;
  709. period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
  710. HPET_COUNTER_CLK_PERIOD_SHIFT; /* fs, 10^-15 */
  711. temp = 1000000000000000uLL; /* 10^15 femtoseconds per second */
  712. temp += period >> 1; /* round */
  713. do_div(temp, period);
  714. hpetp->hp_tick_freq = temp; /* ticks per second */
  715. printk(KERN_INFO "hpet%u: at MMIO 0x%lx, IRQ%s",
  716. hpetp->hp_which, hdp->hd_phys_address,
  717. str_plural(hpetp->hp_ntimer));
  718. for (i = 0; i < hpetp->hp_ntimer; i++)
  719. printk(KERN_CONT "%s %u", i > 0 ? "," : "", hdp->hd_irq[i]);
  720. printk(KERN_CONT "\n");
  721. temp = hpetp->hp_tick_freq;
  722. remainder = do_div(temp, 1000000);
  723. printk(KERN_INFO
  724. "hpet%u: %u comparators, %d-bit %u.%06u MHz counter\n",
  725. hpetp->hp_which, hpetp->hp_ntimer,
  726. cap & HPET_COUNTER_SIZE_MASK ? 64 : 32,
  727. (unsigned) temp, remainder);
  728. mcfg = readq(&hpet->hpet_config);
  729. if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
  730. write_counter(0L, &hpet->hpet_mc);
  731. mcfg |= HPET_ENABLE_CNF_MASK;
  732. writeq(mcfg, &hpet->hpet_config);
  733. }
  734. for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer; i++, devp++) {
  735. struct hpet_timer __iomem *timer;
  736. timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
  737. devp->hd_hpets = hpetp;
  738. devp->hd_hpet = hpet;
  739. devp->hd_timer = timer;
  740. /*
  741. * If the timer was reserved by platform code,
  742. * then make timer unavailable for opens.
  743. */
  744. if (hdp->hd_state & (1 << i)) {
  745. devp->hd_flags = HPET_OPEN;
  746. continue;
  747. }
  748. init_waitqueue_head(&devp->hd_waitqueue);
  749. }
  750. hpetp->hp_delta = hpet_calibrate(hpetp);
  751. return 0;
  752. }
  753. static acpi_status hpet_resources(struct acpi_resource *res, void *data)
  754. {
  755. struct hpet_data *hdp;
  756. acpi_status status;
  757. struct acpi_resource_address64 addr;
  758. hdp = data;
  759. status = acpi_resource_to_address64(res, &addr);
  760. if (ACPI_SUCCESS(status)) {
  761. hdp->hd_phys_address = addr.address.minimum;
  762. hdp->hd_address = ioremap(addr.address.minimum, addr.address.address_length);
  763. if (!hdp->hd_address)
  764. return AE_ERROR;
  765. if (hpet_is_known(hdp)) {
  766. iounmap(hdp->hd_address);
  767. return AE_ALREADY_EXISTS;
  768. }
  769. } else if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
  770. struct acpi_resource_fixed_memory32 *fixmem32;
  771. fixmem32 = &res->data.fixed_memory32;
  772. hdp->hd_phys_address = fixmem32->address;
  773. hdp->hd_address = ioremap(fixmem32->address,
  774. HPET_RANGE_SIZE);
  775. if (!hdp->hd_address)
  776. return AE_ERROR;
  777. if (hpet_is_known(hdp)) {
  778. iounmap(hdp->hd_address);
  779. return AE_ALREADY_EXISTS;
  780. }
  781. } else if (res->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
  782. struct acpi_resource_extended_irq *irqp;
  783. int i, irq;
  784. irqp = &res->data.extended_irq;
  785. for (i = 0; i < irqp->interrupt_count; i++) {
  786. if (hdp->hd_nirqs >= HPET_MAX_TIMERS)
  787. break;
  788. irq = acpi_register_gsi(NULL, irqp->interrupts[i],
  789. irqp->triggering,
  790. irqp->polarity);
  791. if (irq < 0)
  792. return AE_ERROR;
  793. hdp->hd_irq[hdp->hd_nirqs] = irq;
  794. hdp->hd_nirqs++;
  795. }
  796. }
  797. return AE_OK;
  798. }
  799. static int hpet_acpi_add(struct acpi_device *device)
  800. {
  801. acpi_status result;
  802. struct hpet_data data;
  803. memset(&data, 0, sizeof(data));
  804. result =
  805. acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  806. hpet_resources, &data);
  807. if (ACPI_FAILURE(result))
  808. return -ENODEV;
  809. if (!data.hd_address || !data.hd_nirqs) {
  810. if (data.hd_address)
  811. iounmap(data.hd_address);
  812. printk("%s: no address or irqs in _CRS\n", __func__);
  813. return -ENODEV;
  814. }
  815. return hpet_alloc(&data);
  816. }
  817. static const struct acpi_device_id hpet_device_ids[] = {
  818. {"PNP0103", 0},
  819. {"", 0},
  820. };
  821. static struct acpi_driver hpet_acpi_driver = {
  822. .name = "hpet",
  823. .ids = hpet_device_ids,
  824. .ops = {
  825. .add = hpet_acpi_add,
  826. },
  827. };
  828. static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
  829. static int __init hpet_init(void)
  830. {
  831. int result;
  832. result = misc_register(&hpet_misc);
  833. if (result < 0)
  834. return -ENODEV;
  835. sysctl_header = register_sysctl("dev/hpet", hpet_table);
  836. result = acpi_bus_register_driver(&hpet_acpi_driver);
  837. if (result < 0) {
  838. unregister_sysctl_table(sysctl_header);
  839. misc_deregister(&hpet_misc);
  840. return result;
  841. }
  842. return 0;
  843. }
  844. device_initcall(hpet_init);
  845. /*
  846. MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
  847. MODULE_LICENSE("GPL");
  848. */