hpet.c 24 KB

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