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