hpet.c 22 KB

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