hpet.c 24 KB

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