hpet.c 22 KB

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