hpet.c 25 KB

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