hpet.c 21 KB

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