hpet.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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. if (io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT,
  225. PAGE_SIZE, vma->vm_page_prot)) {
  226. printk(KERN_ERR "remap_pfn_range failed in hpet.c\n");
  227. return -EAGAIN;
  228. }
  229. return 0;
  230. #else
  231. return -ENOSYS;
  232. #endif
  233. }
  234. static int hpet_fasync(int fd, struct file *file, int on)
  235. {
  236. struct hpet_dev *devp;
  237. devp = file->private_data;
  238. if (fasync_helper(fd, file, on, &devp->hd_async_queue) >= 0)
  239. return 0;
  240. else
  241. return -EIO;
  242. }
  243. static int hpet_release(struct inode *inode, struct file *file)
  244. {
  245. struct hpet_dev *devp;
  246. struct hpet_timer __iomem *timer;
  247. int irq = 0;
  248. devp = file->private_data;
  249. timer = devp->hd_timer;
  250. spin_lock_irq(&hpet_lock);
  251. writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
  252. &timer->hpet_config);
  253. irq = devp->hd_irq;
  254. devp->hd_irq = 0;
  255. devp->hd_ireqfreq = 0;
  256. if (devp->hd_flags & HPET_PERIODIC
  257. && readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
  258. unsigned long v;
  259. v = readq(&timer->hpet_config);
  260. v ^= Tn_TYPE_CNF_MASK;
  261. writeq(v, &timer->hpet_config);
  262. }
  263. devp->hd_flags &= ~(HPET_OPEN | HPET_IE | HPET_PERIODIC);
  264. spin_unlock_irq(&hpet_lock);
  265. if (irq)
  266. free_irq(irq, devp);
  267. if (file->f_flags & FASYNC)
  268. hpet_fasync(-1, file, 0);
  269. file->private_data = NULL;
  270. return 0;
  271. }
  272. static int hpet_ioctl_common(struct hpet_dev *, int, unsigned long, int);
  273. static int
  274. hpet_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  275. unsigned long arg)
  276. {
  277. struct hpet_dev *devp;
  278. devp = file->private_data;
  279. return hpet_ioctl_common(devp, cmd, arg, 0);
  280. }
  281. static int hpet_ioctl_ieon(struct hpet_dev *devp)
  282. {
  283. struct hpet_timer __iomem *timer;
  284. struct hpet __iomem *hpet;
  285. struct hpets *hpetp;
  286. int irq;
  287. unsigned long g, v, t, m;
  288. unsigned long flags, isr;
  289. timer = devp->hd_timer;
  290. hpet = devp->hd_hpet;
  291. hpetp = devp->hd_hpets;
  292. v = readq(&timer->hpet_config);
  293. spin_lock_irq(&hpet_lock);
  294. if (devp->hd_flags & HPET_IE) {
  295. spin_unlock_irq(&hpet_lock);
  296. return -EBUSY;
  297. }
  298. devp->hd_flags |= HPET_IE;
  299. spin_unlock_irq(&hpet_lock);
  300. t = readq(&timer->hpet_config);
  301. irq = devp->hd_hdwirq;
  302. if (irq) {
  303. sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
  304. if (request_irq
  305. (irq, hpet_interrupt, SA_INTERRUPT, devp->hd_name, (void *)devp)) {
  306. printk(KERN_ERR "hpet: IRQ %d is not free\n", irq);
  307. irq = 0;
  308. }
  309. }
  310. if (irq == 0) {
  311. spin_lock_irq(&hpet_lock);
  312. devp->hd_flags ^= HPET_IE;
  313. spin_unlock_irq(&hpet_lock);
  314. return -EIO;
  315. }
  316. devp->hd_irq = irq;
  317. t = devp->hd_ireqfreq;
  318. v = readq(&timer->hpet_config);
  319. g = v | Tn_INT_ENB_CNF_MASK;
  320. if (devp->hd_flags & HPET_PERIODIC) {
  321. write_counter(t, &timer->hpet_compare);
  322. g |= Tn_TYPE_CNF_MASK;
  323. v |= Tn_TYPE_CNF_MASK;
  324. writeq(v, &timer->hpet_config);
  325. v |= Tn_VAL_SET_CNF_MASK;
  326. writeq(v, &timer->hpet_config);
  327. local_irq_save(flags);
  328. m = read_counter(&hpet->hpet_mc);
  329. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  330. } else {
  331. local_irq_save(flags);
  332. m = read_counter(&hpet->hpet_mc);
  333. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  334. }
  335. isr = (1 << (devp - hpets->hp_dev));
  336. writeq(isr, &hpet->hpet_isr);
  337. writeq(g, &timer->hpet_config);
  338. local_irq_restore(flags);
  339. return 0;
  340. }
  341. static inline unsigned long hpet_time_div(unsigned long dis)
  342. {
  343. unsigned long long m = 1000000000000000ULL;
  344. do_div(m, dis);
  345. return (unsigned long)m;
  346. }
  347. static int
  348. hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel)
  349. {
  350. struct hpet_timer __iomem *timer;
  351. struct hpet __iomem *hpet;
  352. struct hpets *hpetp;
  353. int err;
  354. unsigned long v;
  355. switch (cmd) {
  356. case HPET_IE_OFF:
  357. case HPET_INFO:
  358. case HPET_EPI:
  359. case HPET_DPI:
  360. case HPET_IRQFREQ:
  361. timer = devp->hd_timer;
  362. hpet = devp->hd_hpet;
  363. hpetp = devp->hd_hpets;
  364. break;
  365. case HPET_IE_ON:
  366. return hpet_ioctl_ieon(devp);
  367. default:
  368. return -EINVAL;
  369. }
  370. err = 0;
  371. switch (cmd) {
  372. case HPET_IE_OFF:
  373. if ((devp->hd_flags & HPET_IE) == 0)
  374. break;
  375. v = readq(&timer->hpet_config);
  376. v &= ~Tn_INT_ENB_CNF_MASK;
  377. writeq(v, &timer->hpet_config);
  378. if (devp->hd_irq) {
  379. free_irq(devp->hd_irq, devp);
  380. devp->hd_irq = 0;
  381. }
  382. devp->hd_flags ^= HPET_IE;
  383. break;
  384. case HPET_INFO:
  385. {
  386. struct hpet_info info;
  387. info.hi_ireqfreq = hpet_time_div(hpetp->hp_period *
  388. devp->hd_ireqfreq);
  389. info.hi_flags =
  390. readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK;
  391. info.hi_hpet = devp->hd_hpets->hp_which;
  392. info.hi_timer = devp - devp->hd_hpets->hp_dev;
  393. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  394. err = -EFAULT;
  395. break;
  396. }
  397. case HPET_EPI:
  398. v = readq(&timer->hpet_config);
  399. if ((v & Tn_PER_INT_CAP_MASK) == 0) {
  400. err = -ENXIO;
  401. break;
  402. }
  403. devp->hd_flags |= HPET_PERIODIC;
  404. break;
  405. case HPET_DPI:
  406. v = readq(&timer->hpet_config);
  407. if ((v & Tn_PER_INT_CAP_MASK) == 0) {
  408. err = -ENXIO;
  409. break;
  410. }
  411. if (devp->hd_flags & HPET_PERIODIC &&
  412. readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
  413. v = readq(&timer->hpet_config);
  414. v ^= Tn_TYPE_CNF_MASK;
  415. writeq(v, &timer->hpet_config);
  416. }
  417. devp->hd_flags &= ~HPET_PERIODIC;
  418. break;
  419. case HPET_IRQFREQ:
  420. if (!kernel && (arg > hpet_max_freq) &&
  421. !capable(CAP_SYS_RESOURCE)) {
  422. err = -EACCES;
  423. break;
  424. }
  425. if (arg & (arg - 1)) {
  426. err = -EINVAL;
  427. break;
  428. }
  429. devp->hd_ireqfreq = hpet_time_div(hpetp->hp_period * arg);
  430. }
  431. return err;
  432. }
  433. static struct file_operations hpet_fops = {
  434. .owner = THIS_MODULE,
  435. .llseek = no_llseek,
  436. .read = hpet_read,
  437. .poll = hpet_poll,
  438. .ioctl = hpet_ioctl,
  439. .open = hpet_open,
  440. .release = hpet_release,
  441. .fasync = hpet_fasync,
  442. .mmap = hpet_mmap,
  443. };
  444. EXPORT_SYMBOL(hpet_alloc);
  445. EXPORT_SYMBOL(hpet_register);
  446. EXPORT_SYMBOL(hpet_unregister);
  447. EXPORT_SYMBOL(hpet_control);
  448. int hpet_register(struct hpet_task *tp, int periodic)
  449. {
  450. unsigned int i;
  451. u64 mask;
  452. struct hpet_timer __iomem *timer;
  453. struct hpet_dev *devp;
  454. struct hpets *hpetp;
  455. switch (periodic) {
  456. case 1:
  457. mask = Tn_PER_INT_CAP_MASK;
  458. break;
  459. case 0:
  460. mask = 0;
  461. break;
  462. default:
  463. return -EINVAL;
  464. }
  465. spin_lock_irq(&hpet_task_lock);
  466. spin_lock(&hpet_lock);
  467. for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
  468. for (timer = hpetp->hp_hpet->hpet_timers, i = 0;
  469. i < hpetp->hp_ntimer; i++, timer++) {
  470. if ((readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK)
  471. != mask)
  472. continue;
  473. devp = &hpetp->hp_dev[i];
  474. if (devp->hd_flags & HPET_OPEN || devp->hd_task) {
  475. devp = NULL;
  476. continue;
  477. }
  478. tp->ht_opaque = devp;
  479. devp->hd_task = tp;
  480. break;
  481. }
  482. spin_unlock(&hpet_lock);
  483. spin_unlock_irq(&hpet_task_lock);
  484. if (tp->ht_opaque)
  485. return 0;
  486. else
  487. return -EBUSY;
  488. }
  489. static inline int hpet_tpcheck(struct hpet_task *tp)
  490. {
  491. struct hpet_dev *devp;
  492. struct hpets *hpetp;
  493. devp = tp->ht_opaque;
  494. if (!devp)
  495. return -ENXIO;
  496. for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
  497. if (devp >= hpetp->hp_dev
  498. && devp < (hpetp->hp_dev + hpetp->hp_ntimer)
  499. && devp->hd_hpet == hpetp->hp_hpet)
  500. return 0;
  501. return -ENXIO;
  502. }
  503. int hpet_unregister(struct hpet_task *tp)
  504. {
  505. struct hpet_dev *devp;
  506. struct hpet_timer __iomem *timer;
  507. int err;
  508. if ((err = hpet_tpcheck(tp)))
  509. return err;
  510. spin_lock_irq(&hpet_task_lock);
  511. spin_lock(&hpet_lock);
  512. devp = tp->ht_opaque;
  513. if (devp->hd_task != tp) {
  514. spin_unlock(&hpet_lock);
  515. spin_unlock_irq(&hpet_task_lock);
  516. return -ENXIO;
  517. }
  518. timer = devp->hd_timer;
  519. writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
  520. &timer->hpet_config);
  521. devp->hd_flags &= ~(HPET_IE | HPET_PERIODIC);
  522. devp->hd_task = NULL;
  523. spin_unlock(&hpet_lock);
  524. spin_unlock_irq(&hpet_task_lock);
  525. return 0;
  526. }
  527. int hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg)
  528. {
  529. struct hpet_dev *devp;
  530. int err;
  531. if ((err = hpet_tpcheck(tp)))
  532. return err;
  533. spin_lock_irq(&hpet_lock);
  534. devp = tp->ht_opaque;
  535. if (devp->hd_task != tp) {
  536. spin_unlock_irq(&hpet_lock);
  537. return -ENXIO;
  538. }
  539. spin_unlock_irq(&hpet_lock);
  540. return hpet_ioctl_common(devp, cmd, arg, 1);
  541. }
  542. static ctl_table hpet_table[] = {
  543. {
  544. .ctl_name = 1,
  545. .procname = "max-user-freq",
  546. .data = &hpet_max_freq,
  547. .maxlen = sizeof(int),
  548. .mode = 0644,
  549. .proc_handler = &proc_dointvec,
  550. },
  551. {.ctl_name = 0}
  552. };
  553. static ctl_table hpet_root[] = {
  554. {
  555. .ctl_name = 1,
  556. .procname = "hpet",
  557. .maxlen = 0,
  558. .mode = 0555,
  559. .child = hpet_table,
  560. },
  561. {.ctl_name = 0}
  562. };
  563. static ctl_table dev_root[] = {
  564. {
  565. .ctl_name = CTL_DEV,
  566. .procname = "dev",
  567. .maxlen = 0,
  568. .mode = 0555,
  569. .child = hpet_root,
  570. },
  571. {.ctl_name = 0}
  572. };
  573. static struct ctl_table_header *sysctl_header;
  574. static void hpet_register_interpolator(struct hpets *hpetp)
  575. {
  576. #ifdef CONFIG_TIME_INTERPOLATION
  577. struct time_interpolator *ti;
  578. ti = kmalloc(sizeof(*ti), GFP_KERNEL);
  579. if (!ti)
  580. return;
  581. memset(ti, 0, sizeof(*ti));
  582. ti->source = TIME_SOURCE_MMIO64;
  583. ti->shift = 10;
  584. ti->addr = &hpetp->hp_hpet->hpet_mc;
  585. ti->frequency = hpet_time_div(hpets->hp_period);
  586. ti->drift = HPET_DRIFT;
  587. ti->mask = -1;
  588. hpetp->hp_interpolator = ti;
  589. register_time_interpolator(ti);
  590. #endif
  591. }
  592. /*
  593. * Adjustment for when arming the timer with
  594. * initial conditions. That is, main counter
  595. * ticks expired before interrupts are enabled.
  596. */
  597. #define TICK_CALIBRATE (1000UL)
  598. static unsigned long hpet_calibrate(struct hpets *hpetp)
  599. {
  600. struct hpet_timer __iomem *timer = NULL;
  601. unsigned long t, m, count, i, flags, start;
  602. struct hpet_dev *devp;
  603. int j;
  604. struct hpet __iomem *hpet;
  605. for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
  606. if ((devp->hd_flags & HPET_OPEN) == 0) {
  607. timer = devp->hd_timer;
  608. break;
  609. }
  610. if (!timer)
  611. return 0;
  612. hpet = hpets->hp_hpet;
  613. t = read_counter(&timer->hpet_compare);
  614. i = 0;
  615. count = hpet_time_div(hpetp->hp_period * TICK_CALIBRATE);
  616. local_irq_save(flags);
  617. start = read_counter(&hpet->hpet_mc);
  618. do {
  619. m = read_counter(&hpet->hpet_mc);
  620. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  621. } while (i++, (m - start) < count);
  622. local_irq_restore(flags);
  623. return (m - start) / i;
  624. }
  625. int hpet_alloc(struct hpet_data *hdp)
  626. {
  627. u64 cap, mcfg;
  628. struct hpet_dev *devp;
  629. u32 i, ntimer;
  630. struct hpets *hpetp;
  631. size_t siz;
  632. struct hpet __iomem *hpet;
  633. static struct hpets *last = (struct hpets *)0;
  634. unsigned long ns;
  635. /*
  636. * hpet_alloc can be called by platform dependent code.
  637. * if platform dependent code has allocated the hpet
  638. * ACPI also reports hpet, then we catch it here.
  639. */
  640. for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
  641. if (hpetp->hp_hpet == hdp->hd_address)
  642. return 0;
  643. siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
  644. sizeof(struct hpet_dev));
  645. hpetp = kmalloc(siz, GFP_KERNEL);
  646. if (!hpetp)
  647. return -ENOMEM;
  648. memset(hpetp, 0, siz);
  649. hpetp->hp_which = hpet_nhpet++;
  650. hpetp->hp_hpet = hdp->hd_address;
  651. hpetp->hp_hpet_phys = hdp->hd_phys_address;
  652. hpetp->hp_ntimer = hdp->hd_nirqs;
  653. for (i = 0; i < hdp->hd_nirqs; i++)
  654. hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
  655. hpet = hpetp->hp_hpet;
  656. cap = readq(&hpet->hpet_cap);
  657. ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
  658. if (hpetp->hp_ntimer != ntimer) {
  659. printk(KERN_WARNING "hpet: number irqs doesn't agree"
  660. " with number of timers\n");
  661. kfree(hpetp);
  662. return -ENODEV;
  663. }
  664. if (last)
  665. last->hp_next = hpetp;
  666. else
  667. hpets = hpetp;
  668. last = hpetp;
  669. hpetp->hp_period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
  670. HPET_COUNTER_CLK_PERIOD_SHIFT;
  671. printk(KERN_INFO "hpet%d: at MMIO 0x%lx, IRQ%s",
  672. hpetp->hp_which, hdp->hd_phys_address,
  673. hpetp->hp_ntimer > 1 ? "s" : "");
  674. for (i = 0; i < hpetp->hp_ntimer; i++)
  675. printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]);
  676. printk("\n");
  677. ns = hpetp->hp_period; /* femptoseconds, 10^-15 */
  678. ns /= 1000000; /* convert to nanoseconds, 10^-9 */
  679. printk(KERN_INFO "hpet%d: %ldns tick, %d %d-bit timers\n",
  680. hpetp->hp_which, ns, hpetp->hp_ntimer,
  681. cap & HPET_COUNTER_SIZE_MASK ? 64 : 32);
  682. mcfg = readq(&hpet->hpet_config);
  683. if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
  684. write_counter(0L, &hpet->hpet_mc);
  685. mcfg |= HPET_ENABLE_CNF_MASK;
  686. writeq(mcfg, &hpet->hpet_config);
  687. }
  688. for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer;
  689. i++, hpet_ntimer++, devp++) {
  690. unsigned long v;
  691. struct hpet_timer __iomem *timer;
  692. timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
  693. v = readq(&timer->hpet_config);
  694. devp->hd_hpets = hpetp;
  695. devp->hd_hpet = hpet;
  696. devp->hd_timer = timer;
  697. /*
  698. * If the timer was reserved by platform code,
  699. * then make timer unavailable for opens.
  700. */
  701. if (hdp->hd_state & (1 << i)) {
  702. devp->hd_flags = HPET_OPEN;
  703. continue;
  704. }
  705. init_waitqueue_head(&devp->hd_waitqueue);
  706. }
  707. hpetp->hp_delta = hpet_calibrate(hpetp);
  708. hpet_register_interpolator(hpetp);
  709. return 0;
  710. }
  711. static acpi_status hpet_resources(struct acpi_resource *res, void *data)
  712. {
  713. struct hpet_data *hdp;
  714. acpi_status status;
  715. struct acpi_resource_address64 addr;
  716. struct hpets *hpetp;
  717. hdp = data;
  718. status = acpi_resource_to_address64(res, &addr);
  719. if (ACPI_SUCCESS(status)) {
  720. unsigned long size;
  721. size = addr.max_address_range - addr.min_address_range + 1;
  722. hdp->hd_phys_address = addr.min_address_range;
  723. hdp->hd_address = ioremap(addr.min_address_range, size);
  724. for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
  725. if (hpetp->hp_hpet == hdp->hd_address)
  726. return -EBUSY;
  727. } else if (res->id == ACPI_RSTYPE_EXT_IRQ) {
  728. struct acpi_resource_ext_irq *irqp;
  729. int i;
  730. irqp = &res->data.extended_irq;
  731. if (irqp->number_of_interrupts > 0) {
  732. hdp->hd_nirqs = irqp->number_of_interrupts;
  733. for (i = 0; i < hdp->hd_nirqs; i++) {
  734. int rc =
  735. acpi_register_gsi(irqp->interrupts[i],
  736. irqp->edge_level,
  737. irqp->active_high_low);
  738. if (rc < 0)
  739. return AE_ERROR;
  740. hdp->hd_irq[i] = rc;
  741. }
  742. }
  743. }
  744. return AE_OK;
  745. }
  746. static int hpet_acpi_add(struct acpi_device *device)
  747. {
  748. acpi_status result;
  749. struct hpet_data data;
  750. memset(&data, 0, sizeof(data));
  751. result =
  752. acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  753. hpet_resources, &data);
  754. if (ACPI_FAILURE(result))
  755. return -ENODEV;
  756. if (!data.hd_address || !data.hd_nirqs) {
  757. printk("%s: no address or irqs in _CRS\n", __FUNCTION__);
  758. return -ENODEV;
  759. }
  760. return hpet_alloc(&data);
  761. }
  762. static int hpet_acpi_remove(struct acpi_device *device, int type)
  763. {
  764. /* XXX need to unregister interpolator, dealloc mem, etc */
  765. return -EINVAL;
  766. }
  767. static struct acpi_driver hpet_acpi_driver = {
  768. .name = "hpet",
  769. .ids = "PNP0103",
  770. .ops = {
  771. .add = hpet_acpi_add,
  772. .remove = hpet_acpi_remove,
  773. },
  774. };
  775. static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
  776. static int __init hpet_init(void)
  777. {
  778. int result;
  779. result = misc_register(&hpet_misc);
  780. if (result < 0)
  781. return -ENODEV;
  782. sysctl_header = register_sysctl_table(dev_root, 0);
  783. result = acpi_bus_register_driver(&hpet_acpi_driver);
  784. if (result < 0) {
  785. if (sysctl_header)
  786. unregister_sysctl_table(sysctl_header);
  787. misc_deregister(&hpet_misc);
  788. return result;
  789. }
  790. return 0;
  791. }
  792. static void __exit hpet_exit(void)
  793. {
  794. acpi_bus_unregister_driver(&hpet_acpi_driver);
  795. if (sysctl_header)
  796. unregister_sysctl_table(sysctl_header);
  797. misc_deregister(&hpet_misc);
  798. return;
  799. }
  800. module_init(hpet_init);
  801. module_exit(hpet_exit);
  802. MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
  803. MODULE_LICENSE("GPL");