hpet.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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. sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
  374. irq_flags = devp->hd_flags & HPET_SHARED_IRQ
  375. ? IRQF_SHARED : IRQF_DISABLED;
  376. if (request_irq(irq, hpet_interrupt, irq_flags,
  377. devp->hd_name, (void *)devp)) {
  378. printk(KERN_ERR "hpet: IRQ %d is not free\n", irq);
  379. irq = 0;
  380. }
  381. }
  382. if (irq == 0) {
  383. spin_lock_irq(&hpet_lock);
  384. devp->hd_flags ^= HPET_IE;
  385. spin_unlock_irq(&hpet_lock);
  386. return -EIO;
  387. }
  388. devp->hd_irq = irq;
  389. t = devp->hd_ireqfreq;
  390. v = readq(&timer->hpet_config);
  391. /* 64-bit comparators are not yet supported through the ioctls,
  392. * so force this into 32-bit mode if it supports both modes
  393. */
  394. g = v | Tn_32MODE_CNF_MASK | Tn_INT_ENB_CNF_MASK;
  395. if (devp->hd_flags & HPET_PERIODIC) {
  396. g |= Tn_TYPE_CNF_MASK;
  397. v |= Tn_TYPE_CNF_MASK | Tn_VAL_SET_CNF_MASK;
  398. writeq(v, &timer->hpet_config);
  399. local_irq_save(flags);
  400. /*
  401. * NOTE: First we modify the hidden accumulator
  402. * register supported by periodic-capable comparators.
  403. * We never want to modify the (single) counter; that
  404. * would affect all the comparators. The value written
  405. * is the counter value when the first interrupt is due.
  406. */
  407. m = read_counter(&hpet->hpet_mc);
  408. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  409. /*
  410. * Then we modify the comparator, indicating the period
  411. * for subsequent interrupt.
  412. */
  413. write_counter(t, &timer->hpet_compare);
  414. } else {
  415. local_irq_save(flags);
  416. m = read_counter(&hpet->hpet_mc);
  417. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  418. }
  419. if (devp->hd_flags & HPET_SHARED_IRQ) {
  420. isr = 1 << (devp - devp->hd_hpets->hp_dev);
  421. writel(isr, &hpet->hpet_isr);
  422. }
  423. writeq(g, &timer->hpet_config);
  424. local_irq_restore(flags);
  425. return 0;
  426. }
  427. /* converts Hz to number of timer ticks */
  428. static inline unsigned long hpet_time_div(struct hpets *hpets,
  429. unsigned long dis)
  430. {
  431. unsigned long long m;
  432. m = hpets->hp_tick_freq + (dis >> 1);
  433. do_div(m, dis);
  434. return (unsigned long)m;
  435. }
  436. static int
  437. hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg,
  438. struct hpet_info *info)
  439. {
  440. struct hpet_timer __iomem *timer;
  441. struct hpet __iomem *hpet;
  442. struct hpets *hpetp;
  443. int err;
  444. unsigned long v;
  445. switch (cmd) {
  446. case HPET_IE_OFF:
  447. case HPET_INFO:
  448. case HPET_EPI:
  449. case HPET_DPI:
  450. case HPET_IRQFREQ:
  451. timer = devp->hd_timer;
  452. hpet = devp->hd_hpet;
  453. hpetp = devp->hd_hpets;
  454. break;
  455. case HPET_IE_ON:
  456. return hpet_ioctl_ieon(devp);
  457. default:
  458. return -EINVAL;
  459. }
  460. err = 0;
  461. switch (cmd) {
  462. case HPET_IE_OFF:
  463. if ((devp->hd_flags & HPET_IE) == 0)
  464. break;
  465. v = readq(&timer->hpet_config);
  466. v &= ~Tn_INT_ENB_CNF_MASK;
  467. writeq(v, &timer->hpet_config);
  468. if (devp->hd_irq) {
  469. free_irq(devp->hd_irq, devp);
  470. devp->hd_irq = 0;
  471. }
  472. devp->hd_flags ^= HPET_IE;
  473. break;
  474. case HPET_INFO:
  475. {
  476. if (devp->hd_ireqfreq)
  477. info->hi_ireqfreq =
  478. hpet_time_div(hpetp, devp->hd_ireqfreq);
  479. else
  480. info->hi_ireqfreq = 0;
  481. info->hi_flags =
  482. readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK;
  483. info->hi_hpet = hpetp->hp_which;
  484. info->hi_timer = devp - hpetp->hp_dev;
  485. break;
  486. }
  487. case HPET_EPI:
  488. v = readq(&timer->hpet_config);
  489. if ((v & Tn_PER_INT_CAP_MASK) == 0) {
  490. err = -ENXIO;
  491. break;
  492. }
  493. devp->hd_flags |= HPET_PERIODIC;
  494. break;
  495. case HPET_DPI:
  496. v = readq(&timer->hpet_config);
  497. if ((v & Tn_PER_INT_CAP_MASK) == 0) {
  498. err = -ENXIO;
  499. break;
  500. }
  501. if (devp->hd_flags & HPET_PERIODIC &&
  502. readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
  503. v = readq(&timer->hpet_config);
  504. v ^= Tn_TYPE_CNF_MASK;
  505. writeq(v, &timer->hpet_config);
  506. }
  507. devp->hd_flags &= ~HPET_PERIODIC;
  508. break;
  509. case HPET_IRQFREQ:
  510. if ((arg > hpet_max_freq) &&
  511. !capable(CAP_SYS_RESOURCE)) {
  512. err = -EACCES;
  513. break;
  514. }
  515. if (!arg) {
  516. err = -EINVAL;
  517. break;
  518. }
  519. devp->hd_ireqfreq = hpet_time_div(hpetp, arg);
  520. }
  521. return err;
  522. }
  523. static long
  524. hpet_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  525. {
  526. struct hpet_info info;
  527. int err;
  528. mutex_lock(&hpet_mutex);
  529. err = hpet_ioctl_common(file->private_data, cmd, arg, &info);
  530. mutex_unlock(&hpet_mutex);
  531. if ((cmd == HPET_INFO) && !err &&
  532. (copy_to_user((void __user *)arg, &info, sizeof(info))))
  533. err = -EFAULT;
  534. return err;
  535. }
  536. #ifdef CONFIG_COMPAT
  537. struct compat_hpet_info {
  538. compat_ulong_t hi_ireqfreq; /* Hz */
  539. compat_ulong_t hi_flags; /* information */
  540. unsigned short hi_hpet;
  541. unsigned short hi_timer;
  542. };
  543. static long
  544. hpet_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  545. {
  546. struct hpet_info info;
  547. int err;
  548. mutex_lock(&hpet_mutex);
  549. err = hpet_ioctl_common(file->private_data, cmd, arg, &info);
  550. mutex_unlock(&hpet_mutex);
  551. if ((cmd == HPET_INFO) && !err) {
  552. struct compat_hpet_info __user *u = compat_ptr(arg);
  553. if (put_user(info.hi_ireqfreq, &u->hi_ireqfreq) ||
  554. put_user(info.hi_flags, &u->hi_flags) ||
  555. put_user(info.hi_hpet, &u->hi_hpet) ||
  556. put_user(info.hi_timer, &u->hi_timer))
  557. err = -EFAULT;
  558. }
  559. return err;
  560. }
  561. #endif
  562. static const struct file_operations hpet_fops = {
  563. .owner = THIS_MODULE,
  564. .llseek = no_llseek,
  565. .read = hpet_read,
  566. .poll = hpet_poll,
  567. .unlocked_ioctl = hpet_ioctl,
  568. #ifdef CONFIG_COMPAT
  569. .compat_ioctl = hpet_compat_ioctl,
  570. #endif
  571. .open = hpet_open,
  572. .release = hpet_release,
  573. .fasync = hpet_fasync,
  574. .mmap = hpet_mmap,
  575. };
  576. static int hpet_is_known(struct hpet_data *hdp)
  577. {
  578. struct hpets *hpetp;
  579. for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
  580. if (hpetp->hp_hpet_phys == hdp->hd_phys_address)
  581. return 1;
  582. return 0;
  583. }
  584. static ctl_table hpet_table[] = {
  585. {
  586. .procname = "max-user-freq",
  587. .data = &hpet_max_freq,
  588. .maxlen = sizeof(int),
  589. .mode = 0644,
  590. .proc_handler = proc_dointvec,
  591. },
  592. {}
  593. };
  594. static ctl_table hpet_root[] = {
  595. {
  596. .procname = "hpet",
  597. .maxlen = 0,
  598. .mode = 0555,
  599. .child = hpet_table,
  600. },
  601. {}
  602. };
  603. static ctl_table dev_root[] = {
  604. {
  605. .procname = "dev",
  606. .maxlen = 0,
  607. .mode = 0555,
  608. .child = hpet_root,
  609. },
  610. {}
  611. };
  612. static struct ctl_table_header *sysctl_header;
  613. /*
  614. * Adjustment for when arming the timer with
  615. * initial conditions. That is, main counter
  616. * ticks expired before interrupts are enabled.
  617. */
  618. #define TICK_CALIBRATE (1000UL)
  619. static unsigned long __hpet_calibrate(struct hpets *hpetp)
  620. {
  621. struct hpet_timer __iomem *timer = NULL;
  622. unsigned long t, m, count, i, flags, start;
  623. struct hpet_dev *devp;
  624. int j;
  625. struct hpet __iomem *hpet;
  626. for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
  627. if ((devp->hd_flags & HPET_OPEN) == 0) {
  628. timer = devp->hd_timer;
  629. break;
  630. }
  631. if (!timer)
  632. return 0;
  633. hpet = hpetp->hp_hpet;
  634. t = read_counter(&timer->hpet_compare);
  635. i = 0;
  636. count = hpet_time_div(hpetp, TICK_CALIBRATE);
  637. local_irq_save(flags);
  638. start = read_counter(&hpet->hpet_mc);
  639. do {
  640. m = read_counter(&hpet->hpet_mc);
  641. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  642. } while (i++, (m - start) < count);
  643. local_irq_restore(flags);
  644. return (m - start) / i;
  645. }
  646. static unsigned long hpet_calibrate(struct hpets *hpetp)
  647. {
  648. unsigned long ret = -1;
  649. unsigned long tmp;
  650. /*
  651. * Try to calibrate until return value becomes stable small value.
  652. * If SMI interruption occurs in calibration loop, the return value
  653. * will be big. This avoids its impact.
  654. */
  655. for ( ; ; ) {
  656. tmp = __hpet_calibrate(hpetp);
  657. if (ret <= tmp)
  658. break;
  659. ret = tmp;
  660. }
  661. return ret;
  662. }
  663. int hpet_alloc(struct hpet_data *hdp)
  664. {
  665. u64 cap, mcfg;
  666. struct hpet_dev *devp;
  667. u32 i, ntimer;
  668. struct hpets *hpetp;
  669. size_t siz;
  670. struct hpet __iomem *hpet;
  671. static struct hpets *last = NULL;
  672. unsigned long period;
  673. unsigned long long temp;
  674. u32 remainder;
  675. /*
  676. * hpet_alloc can be called by platform dependent code.
  677. * If platform dependent code has allocated the hpet that
  678. * ACPI has also reported, then we catch it here.
  679. */
  680. if (hpet_is_known(hdp)) {
  681. printk(KERN_DEBUG "%s: duplicate HPET ignored\n",
  682. __func__);
  683. return 0;
  684. }
  685. siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
  686. sizeof(struct hpet_dev));
  687. hpetp = kzalloc(siz, GFP_KERNEL);
  688. if (!hpetp)
  689. return -ENOMEM;
  690. hpetp->hp_which = hpet_nhpet++;
  691. hpetp->hp_hpet = hdp->hd_address;
  692. hpetp->hp_hpet_phys = hdp->hd_phys_address;
  693. hpetp->hp_ntimer = hdp->hd_nirqs;
  694. for (i = 0; i < hdp->hd_nirqs; i++)
  695. hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
  696. hpet = hpetp->hp_hpet;
  697. cap = readq(&hpet->hpet_cap);
  698. ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
  699. if (hpetp->hp_ntimer != ntimer) {
  700. printk(KERN_WARNING "hpet: number irqs doesn't agree"
  701. " with number of timers\n");
  702. kfree(hpetp);
  703. return -ENODEV;
  704. }
  705. if (last)
  706. last->hp_next = hpetp;
  707. else
  708. hpets = hpetp;
  709. last = hpetp;
  710. period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
  711. HPET_COUNTER_CLK_PERIOD_SHIFT; /* fs, 10^-15 */
  712. temp = 1000000000000000uLL; /* 10^15 femtoseconds per second */
  713. temp += period >> 1; /* round */
  714. do_div(temp, period);
  715. hpetp->hp_tick_freq = temp; /* ticks per second */
  716. printk(KERN_INFO "hpet%d: at MMIO 0x%lx, IRQ%s",
  717. hpetp->hp_which, hdp->hd_phys_address,
  718. hpetp->hp_ntimer > 1 ? "s" : "");
  719. for (i = 0; i < hpetp->hp_ntimer; i++)
  720. printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]);
  721. printk("\n");
  722. temp = hpetp->hp_tick_freq;
  723. remainder = do_div(temp, 1000000);
  724. printk(KERN_INFO
  725. "hpet%u: %u comparators, %d-bit %u.%06u MHz counter\n",
  726. hpetp->hp_which, hpetp->hp_ntimer,
  727. cap & HPET_COUNTER_SIZE_MASK ? 64 : 32,
  728. (unsigned) temp, remainder);
  729. mcfg = readq(&hpet->hpet_config);
  730. if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
  731. write_counter(0L, &hpet->hpet_mc);
  732. mcfg |= HPET_ENABLE_CNF_MASK;
  733. writeq(mcfg, &hpet->hpet_config);
  734. }
  735. for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer; i++, devp++) {
  736. struct hpet_timer __iomem *timer;
  737. timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
  738. devp->hd_hpets = hpetp;
  739. devp->hd_hpet = hpet;
  740. devp->hd_timer = timer;
  741. /*
  742. * If the timer was reserved by platform code,
  743. * then make timer unavailable for opens.
  744. */
  745. if (hdp->hd_state & (1 << i)) {
  746. devp->hd_flags = HPET_OPEN;
  747. continue;
  748. }
  749. init_waitqueue_head(&devp->hd_waitqueue);
  750. }
  751. hpetp->hp_delta = hpet_calibrate(hpetp);
  752. /* This clocksource driver currently only works on ia64 */
  753. #ifdef CONFIG_IA64
  754. if (!hpet_clocksource) {
  755. hpet_mctr = (void __iomem *)&hpetp->hp_hpet->hpet_mc;
  756. CLKSRC_FSYS_MMIO_SET(clocksource_hpet.fsys_mmio, hpet_mctr);
  757. clocksource_hpet.mult = clocksource_hz2mult(hpetp->hp_tick_freq,
  758. clocksource_hpet.shift);
  759. clocksource_register(&clocksource_hpet);
  760. hpetp->hp_clocksource = &clocksource_hpet;
  761. hpet_clocksource = &clocksource_hpet;
  762. }
  763. #endif
  764. return 0;
  765. }
  766. static acpi_status hpet_resources(struct acpi_resource *res, void *data)
  767. {
  768. struct hpet_data *hdp;
  769. acpi_status status;
  770. struct acpi_resource_address64 addr;
  771. hdp = data;
  772. status = acpi_resource_to_address64(res, &addr);
  773. if (ACPI_SUCCESS(status)) {
  774. hdp->hd_phys_address = addr.minimum;
  775. hdp->hd_address = ioremap(addr.minimum, addr.address_length);
  776. if (hpet_is_known(hdp)) {
  777. iounmap(hdp->hd_address);
  778. return AE_ALREADY_EXISTS;
  779. }
  780. } else if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
  781. struct acpi_resource_fixed_memory32 *fixmem32;
  782. fixmem32 = &res->data.fixed_memory32;
  783. if (!fixmem32)
  784. return AE_NO_MEMORY;
  785. hdp->hd_phys_address = fixmem32->address;
  786. hdp->hd_address = ioremap(fixmem32->address,
  787. HPET_RANGE_SIZE);
  788. if (hpet_is_known(hdp)) {
  789. iounmap(hdp->hd_address);
  790. return AE_ALREADY_EXISTS;
  791. }
  792. } else if (res->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
  793. struct acpi_resource_extended_irq *irqp;
  794. int i, irq;
  795. irqp = &res->data.extended_irq;
  796. for (i = 0; i < irqp->interrupt_count; i++) {
  797. irq = acpi_register_gsi(NULL, irqp->interrupts[i],
  798. irqp->triggering, irqp->polarity);
  799. if (irq < 0)
  800. return AE_ERROR;
  801. hdp->hd_irq[hdp->hd_nirqs] = irq;
  802. hdp->hd_nirqs++;
  803. }
  804. }
  805. return AE_OK;
  806. }
  807. static int hpet_acpi_add(struct acpi_device *device)
  808. {
  809. acpi_status result;
  810. struct hpet_data data;
  811. memset(&data, 0, sizeof(data));
  812. result =
  813. acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  814. hpet_resources, &data);
  815. if (ACPI_FAILURE(result))
  816. return -ENODEV;
  817. if (!data.hd_address || !data.hd_nirqs) {
  818. printk("%s: no address or irqs in _CRS\n", __func__);
  819. return -ENODEV;
  820. }
  821. return hpet_alloc(&data);
  822. }
  823. static int hpet_acpi_remove(struct acpi_device *device, int type)
  824. {
  825. /* XXX need to unregister clocksource, dealloc mem, etc */
  826. return -EINVAL;
  827. }
  828. static const struct acpi_device_id hpet_device_ids[] = {
  829. {"PNP0103", 0},
  830. {"", 0},
  831. };
  832. MODULE_DEVICE_TABLE(acpi, hpet_device_ids);
  833. static struct acpi_driver hpet_acpi_driver = {
  834. .name = "hpet",
  835. .ids = hpet_device_ids,
  836. .ops = {
  837. .add = hpet_acpi_add,
  838. .remove = hpet_acpi_remove,
  839. },
  840. };
  841. static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
  842. static int __init hpet_init(void)
  843. {
  844. int result;
  845. result = misc_register(&hpet_misc);
  846. if (result < 0)
  847. return -ENODEV;
  848. sysctl_header = register_sysctl_table(dev_root);
  849. result = acpi_bus_register_driver(&hpet_acpi_driver);
  850. if (result < 0) {
  851. if (sysctl_header)
  852. unregister_sysctl_table(sysctl_header);
  853. misc_deregister(&hpet_misc);
  854. return result;
  855. }
  856. return 0;
  857. }
  858. static void __exit hpet_exit(void)
  859. {
  860. acpi_bus_unregister_driver(&hpet_acpi_driver);
  861. if (sysctl_header)
  862. unregister_sysctl_table(sysctl_header);
  863. misc_deregister(&hpet_misc);
  864. return;
  865. }
  866. module_init(hpet_init);
  867. module_exit(hpet_exit);
  868. MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
  869. MODULE_LICENSE("GPL");