ia64/linux-2.6.18-xen.hg

annotate drivers/char/hpet.c @ 893:f994bfe9b93b

linux/blktap2: reduce TLB flush scope

c/s 885 added very coarse TLB flushing. Since these flushes always
follow single page updates, single page flushes (when available) are
sufficient.

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jun 04 10:32:57 2009 +0100 (2009-06-04)
parents 831230e53067
children
rev   line source
ian@0 1 /*
ian@0 2 * Intel & MS High Precision Event Timer Implementation.
ian@0 3 *
ian@0 4 * Copyright (C) 2003 Intel Corporation
ian@0 5 * Venki Pallipadi
ian@0 6 * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
ian@0 7 * Bob Picco <robert.picco@hp.com>
ian@0 8 *
ian@0 9 * This program is free software; you can redistribute it and/or modify
ian@0 10 * it under the terms of the GNU General Public License version 2 as
ian@0 11 * published by the Free Software Foundation.
ian@0 12 */
ian@0 13
ian@0 14 #include <linux/interrupt.h>
ian@0 15 #include <linux/module.h>
ian@0 16 #include <linux/kernel.h>
ian@0 17 #include <linux/types.h>
ian@0 18 #include <linux/miscdevice.h>
ian@0 19 #include <linux/major.h>
ian@0 20 #include <linux/ioport.h>
ian@0 21 #include <linux/fcntl.h>
ian@0 22 #include <linux/init.h>
ian@0 23 #include <linux/poll.h>
ian@0 24 #include <linux/proc_fs.h>
ian@0 25 #include <linux/spinlock.h>
ian@0 26 #include <linux/sysctl.h>
ian@0 27 #include <linux/wait.h>
ian@0 28 #include <linux/bcd.h>
ian@0 29 #include <linux/seq_file.h>
ian@0 30 #include <linux/bitops.h>
ian@0 31
ian@0 32 #include <asm/current.h>
ian@0 33 #include <asm/uaccess.h>
ian@0 34 #include <asm/system.h>
ian@0 35 #include <asm/io.h>
ian@0 36 #include <asm/irq.h>
ian@0 37 #include <asm/div64.h>
ian@0 38
ian@0 39 #include <linux/acpi.h>
ian@0 40 #include <acpi/acpi_bus.h>
ian@0 41 #include <linux/hpet.h>
ian@0 42
ian@0 43 /*
ian@0 44 * The High Precision Event Timer driver.
ian@0 45 * This driver is closely modelled after the rtc.c driver.
ian@0 46 * http://www.intel.com/hardwaredesign/hpetspec.htm
ian@0 47 */
ian@0 48 #define HPET_USER_FREQ (64)
ian@0 49 #define HPET_DRIFT (500)
ian@0 50
ian@0 51 #define HPET_RANGE_SIZE 1024 /* from HPET spec */
ian@0 52
ian@0 53 static u32 hpet_nhpet, hpet_max_freq = HPET_USER_FREQ;
ian@0 54
ian@0 55 /* A lock for concurrent access by app and isr hpet activity. */
ian@0 56 static DEFINE_SPINLOCK(hpet_lock);
ian@0 57 /* A lock for concurrent intermodule access to hpet and isr hpet activity. */
ian@0 58 static DEFINE_SPINLOCK(hpet_task_lock);
ian@0 59
ian@0 60 #define HPET_DEV_NAME (7)
ian@0 61
ian@0 62 struct hpet_dev {
ian@0 63 struct hpets *hd_hpets;
ian@0 64 struct hpet __iomem *hd_hpet;
ian@0 65 struct hpet_timer __iomem *hd_timer;
ian@0 66 unsigned long hd_ireqfreq;
ian@0 67 unsigned long hd_irqdata;
ian@0 68 wait_queue_head_t hd_waitqueue;
ian@0 69 struct fasync_struct *hd_async_queue;
ian@0 70 struct hpet_task *hd_task;
ian@0 71 unsigned int hd_flags;
ian@0 72 unsigned int hd_irq;
ian@0 73 unsigned int hd_hdwirq;
ian@0 74 char hd_name[HPET_DEV_NAME];
ian@0 75 };
ian@0 76
ian@0 77 struct hpets {
ian@0 78 struct hpets *hp_next;
ian@0 79 struct hpet __iomem *hp_hpet;
ian@0 80 unsigned long hp_hpet_phys;
ian@0 81 struct time_interpolator *hp_interpolator;
ian@0 82 unsigned long long hp_tick_freq;
ian@0 83 unsigned long hp_delta;
ian@0 84 unsigned int hp_ntimer;
ian@0 85 unsigned int hp_which;
ian@0 86 struct hpet_dev hp_dev[1];
ian@0 87 };
ian@0 88
ian@0 89 static struct hpets *hpets;
ian@0 90
ian@0 91 #define HPET_OPEN 0x0001
ian@0 92 #define HPET_IE 0x0002 /* interrupt enabled */
ian@0 93 #define HPET_PERIODIC 0x0004
ian@0 94 #define HPET_SHARED_IRQ 0x0008
ian@0 95
ian@0 96 #if BITS_PER_LONG == 64
ian@0 97 #define write_counter(V, MC) writeq(V, MC)
ian@0 98 #define read_counter(MC) readq(MC)
ian@0 99 #else
ian@0 100 #define write_counter(V, MC) writel(V, MC)
ian@0 101 #define read_counter(MC) readl(MC)
ian@0 102 #endif
ian@0 103
ian@0 104 #ifndef readq
ian@0 105 static inline unsigned long long readq(void __iomem *addr)
ian@0 106 {
ian@0 107 return readl(addr) | (((unsigned long long)readl(addr + 4)) << 32LL);
ian@0 108 }
ian@0 109 #endif
ian@0 110
ian@0 111 #ifndef writeq
ian@0 112 static inline void writeq(unsigned long long v, void __iomem *addr)
ian@0 113 {
ian@0 114 writel(v & 0xffffffff, addr);
ian@0 115 writel(v >> 32, addr + 4);
ian@0 116 }
ian@0 117 #endif
ian@0 118
ian@0 119 static irqreturn_t hpet_interrupt(int irq, void *data, struct pt_regs *regs)
ian@0 120 {
ian@0 121 struct hpet_dev *devp;
ian@0 122 unsigned long isr;
ian@0 123
ian@0 124 devp = data;
ian@0 125 isr = 1 << (devp - devp->hd_hpets->hp_dev);
ian@0 126
ian@0 127 if ((devp->hd_flags & HPET_SHARED_IRQ) &&
ian@0 128 !(isr & readl(&devp->hd_hpet->hpet_isr)))
ian@0 129 return IRQ_NONE;
ian@0 130
ian@0 131 spin_lock(&hpet_lock);
ian@0 132 devp->hd_irqdata++;
ian@0 133
ian@0 134 /*
ian@0 135 * For non-periodic timers, increment the accumulator.
ian@0 136 * This has the effect of treating non-periodic like periodic.
ian@0 137 */
ian@0 138 if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) {
ian@0 139 unsigned long m, t;
ian@0 140
ian@0 141 t = devp->hd_ireqfreq;
ian@0 142 m = read_counter(&devp->hd_hpet->hpet_mc);
ian@0 143 write_counter(t + m + devp->hd_hpets->hp_delta,
ian@0 144 &devp->hd_timer->hpet_compare);
ian@0 145 }
ian@0 146
ian@0 147 if (devp->hd_flags & HPET_SHARED_IRQ)
ian@0 148 writel(isr, &devp->hd_hpet->hpet_isr);
ian@0 149 spin_unlock(&hpet_lock);
ian@0 150
ian@0 151 spin_lock(&hpet_task_lock);
ian@0 152 if (devp->hd_task)
ian@0 153 devp->hd_task->ht_func(devp->hd_task->ht_data);
ian@0 154 spin_unlock(&hpet_task_lock);
ian@0 155
ian@0 156 wake_up_interruptible(&devp->hd_waitqueue);
ian@0 157
ian@0 158 kill_fasync(&devp->hd_async_queue, SIGIO, POLL_IN);
ian@0 159
ian@0 160 return IRQ_HANDLED;
ian@0 161 }
ian@0 162
ian@0 163 static int hpet_open(struct inode *inode, struct file *file)
ian@0 164 {
ian@0 165 struct hpet_dev *devp;
ian@0 166 struct hpets *hpetp;
ian@0 167 int i;
ian@0 168
ian@0 169 if (file->f_mode & FMODE_WRITE)
ian@0 170 return -EINVAL;
ian@0 171
ian@0 172 spin_lock_irq(&hpet_lock);
ian@0 173
ian@0 174 for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
ian@0 175 for (i = 0; i < hpetp->hp_ntimer; i++)
ian@0 176 if (hpetp->hp_dev[i].hd_flags & HPET_OPEN
ian@0 177 || hpetp->hp_dev[i].hd_task)
ian@0 178 continue;
ian@0 179 else {
ian@0 180 devp = &hpetp->hp_dev[i];
ian@0 181 break;
ian@0 182 }
ian@0 183
ian@0 184 if (!devp) {
ian@0 185 spin_unlock_irq(&hpet_lock);
ian@0 186 return -EBUSY;
ian@0 187 }
ian@0 188
ian@0 189 file->private_data = devp;
ian@0 190 devp->hd_irqdata = 0;
ian@0 191 devp->hd_flags |= HPET_OPEN;
ian@0 192 spin_unlock_irq(&hpet_lock);
ian@0 193
ian@0 194 return 0;
ian@0 195 }
ian@0 196
ian@0 197 static ssize_t
ian@0 198 hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
ian@0 199 {
ian@0 200 DECLARE_WAITQUEUE(wait, current);
ian@0 201 unsigned long data;
ian@0 202 ssize_t retval;
ian@0 203 struct hpet_dev *devp;
ian@0 204
ian@0 205 devp = file->private_data;
ian@0 206 if (!devp->hd_ireqfreq)
ian@0 207 return -EIO;
ian@0 208
ian@0 209 if (count < sizeof(unsigned long))
ian@0 210 return -EINVAL;
ian@0 211
ian@0 212 add_wait_queue(&devp->hd_waitqueue, &wait);
ian@0 213
ian@0 214 for ( ; ; ) {
ian@0 215 set_current_state(TASK_INTERRUPTIBLE);
ian@0 216
ian@0 217 spin_lock_irq(&hpet_lock);
ian@0 218 data = devp->hd_irqdata;
ian@0 219 devp->hd_irqdata = 0;
ian@0 220 spin_unlock_irq(&hpet_lock);
ian@0 221
ian@0 222 if (data)
ian@0 223 break;
ian@0 224 else if (file->f_flags & O_NONBLOCK) {
ian@0 225 retval = -EAGAIN;
ian@0 226 goto out;
ian@0 227 } else if (signal_pending(current)) {
ian@0 228 retval = -ERESTARTSYS;
ian@0 229 goto out;
ian@0 230 }
ian@0 231 schedule();
ian@0 232 }
ian@0 233
ian@0 234 retval = put_user(data, (unsigned long __user *)buf);
ian@0 235 if (!retval)
ian@0 236 retval = sizeof(unsigned long);
ian@0 237 out:
ian@0 238 __set_current_state(TASK_RUNNING);
ian@0 239 remove_wait_queue(&devp->hd_waitqueue, &wait);
ian@0 240
ian@0 241 return retval;
ian@0 242 }
ian@0 243
ian@0 244 static unsigned int hpet_poll(struct file *file, poll_table * wait)
ian@0 245 {
ian@0 246 unsigned long v;
ian@0 247 struct hpet_dev *devp;
ian@0 248
ian@0 249 devp = file->private_data;
ian@0 250
ian@0 251 if (!devp->hd_ireqfreq)
ian@0 252 return 0;
ian@0 253
ian@0 254 poll_wait(file, &devp->hd_waitqueue, wait);
ian@0 255
ian@0 256 spin_lock_irq(&hpet_lock);
ian@0 257 v = devp->hd_irqdata;
ian@0 258 spin_unlock_irq(&hpet_lock);
ian@0 259
ian@0 260 if (v != 0)
ian@0 261 return POLLIN | POLLRDNORM;
ian@0 262
ian@0 263 return 0;
ian@0 264 }
ian@0 265
ian@0 266 static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
ian@0 267 {
ian@0 268 #ifdef CONFIG_HPET_MMAP
ian@0 269 struct hpet_dev *devp;
ian@0 270 unsigned long addr;
ian@0 271
ian@0 272 if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff)
ian@0 273 return -EINVAL;
ian@0 274
ian@0 275 devp = file->private_data;
ian@0 276 addr = devp->hd_hpets->hp_hpet_phys;
ian@0 277
ian@0 278 if (addr & (PAGE_SIZE - 1))
ian@0 279 return -ENOSYS;
ian@0 280
ian@0 281 vma->vm_flags |= VM_IO;
ian@0 282 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
ian@0 283
ian@0 284 if (io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT,
ian@0 285 PAGE_SIZE, vma->vm_page_prot)) {
ian@0 286 printk(KERN_ERR "%s: io_remap_pfn_range failed\n",
ian@0 287 __FUNCTION__);
ian@0 288 return -EAGAIN;
ian@0 289 }
ian@0 290
ian@0 291 return 0;
ian@0 292 #else
ian@0 293 return -ENOSYS;
ian@0 294 #endif
ian@0 295 }
ian@0 296
ian@0 297 static int hpet_fasync(int fd, struct file *file, int on)
ian@0 298 {
ian@0 299 struct hpet_dev *devp;
ian@0 300
ian@0 301 devp = file->private_data;
ian@0 302
ian@0 303 if (fasync_helper(fd, file, on, &devp->hd_async_queue) >= 0)
ian@0 304 return 0;
ian@0 305 else
ian@0 306 return -EIO;
ian@0 307 }
ian@0 308
ian@0 309 static int hpet_release(struct inode *inode, struct file *file)
ian@0 310 {
ian@0 311 struct hpet_dev *devp;
ian@0 312 struct hpet_timer __iomem *timer;
ian@0 313 int irq = 0;
ian@0 314
ian@0 315 devp = file->private_data;
ian@0 316 timer = devp->hd_timer;
ian@0 317
ian@0 318 spin_lock_irq(&hpet_lock);
ian@0 319
ian@0 320 writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
ian@0 321 &timer->hpet_config);
ian@0 322
ian@0 323 irq = devp->hd_irq;
ian@0 324 devp->hd_irq = 0;
ian@0 325
ian@0 326 devp->hd_ireqfreq = 0;
ian@0 327
ian@0 328 if (devp->hd_flags & HPET_PERIODIC
ian@0 329 && readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
ian@0 330 unsigned long v;
ian@0 331
ian@0 332 v = readq(&timer->hpet_config);
ian@0 333 v ^= Tn_TYPE_CNF_MASK;
ian@0 334 writeq(v, &timer->hpet_config);
ian@0 335 }
ian@0 336
ian@0 337 devp->hd_flags &= ~(HPET_OPEN | HPET_IE | HPET_PERIODIC);
ian@0 338 spin_unlock_irq(&hpet_lock);
ian@0 339
ian@0 340 if (irq)
ian@0 341 free_irq(irq, devp);
ian@0 342
ian@0 343 if (file->f_flags & FASYNC)
ian@0 344 hpet_fasync(-1, file, 0);
ian@0 345
ian@0 346 file->private_data = NULL;
ian@0 347 return 0;
ian@0 348 }
ian@0 349
ian@0 350 static int hpet_ioctl_common(struct hpet_dev *, int, unsigned long, int);
ian@0 351
ian@0 352 static int
ian@0 353 hpet_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
ian@0 354 unsigned long arg)
ian@0 355 {
ian@0 356 struct hpet_dev *devp;
ian@0 357
ian@0 358 devp = file->private_data;
ian@0 359 return hpet_ioctl_common(devp, cmd, arg, 0);
ian@0 360 }
ian@0 361
ian@0 362 static int hpet_ioctl_ieon(struct hpet_dev *devp)
ian@0 363 {
ian@0 364 struct hpet_timer __iomem *timer;
ian@0 365 struct hpet __iomem *hpet;
ian@0 366 struct hpets *hpetp;
ian@0 367 int irq;
ian@0 368 unsigned long g, v, t, m;
ian@0 369 unsigned long flags, isr;
ian@0 370
ian@0 371 timer = devp->hd_timer;
ian@0 372 hpet = devp->hd_hpet;
ian@0 373 hpetp = devp->hd_hpets;
ian@0 374
ian@0 375 if (!devp->hd_ireqfreq)
ian@0 376 return -EIO;
ian@0 377
ian@0 378 spin_lock_irq(&hpet_lock);
ian@0 379
ian@0 380 if (devp->hd_flags & HPET_IE) {
ian@0 381 spin_unlock_irq(&hpet_lock);
ian@0 382 return -EBUSY;
ian@0 383 }
ian@0 384
ian@0 385 devp->hd_flags |= HPET_IE;
ian@0 386
ian@0 387 if (readl(&timer->hpet_config) & Tn_INT_TYPE_CNF_MASK)
ian@0 388 devp->hd_flags |= HPET_SHARED_IRQ;
ian@0 389 spin_unlock_irq(&hpet_lock);
ian@0 390
ian@0 391 irq = devp->hd_hdwirq;
ian@0 392
ian@0 393 if (irq) {
ian@0 394 unsigned long irq_flags;
ian@0 395
ian@0 396 sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
ian@0 397 irq_flags = devp->hd_flags & HPET_SHARED_IRQ
ian@0 398 ? IRQF_SHARED : IRQF_DISABLED;
ian@0 399 if (request_irq(irq, hpet_interrupt, irq_flags,
ian@0 400 devp->hd_name, (void *)devp)) {
ian@0 401 printk(KERN_ERR "hpet: IRQ %d is not free\n", irq);
ian@0 402 irq = 0;
ian@0 403 }
ian@0 404 }
ian@0 405
ian@0 406 if (irq == 0) {
ian@0 407 spin_lock_irq(&hpet_lock);
ian@0 408 devp->hd_flags ^= HPET_IE;
ian@0 409 spin_unlock_irq(&hpet_lock);
ian@0 410 return -EIO;
ian@0 411 }
ian@0 412
ian@0 413 devp->hd_irq = irq;
ian@0 414 t = devp->hd_ireqfreq;
ian@0 415 v = readq(&timer->hpet_config);
ian@0 416 g = v | Tn_INT_ENB_CNF_MASK;
ian@0 417
ian@0 418 if (devp->hd_flags & HPET_PERIODIC) {
ian@0 419 write_counter(t, &timer->hpet_compare);
ian@0 420 g |= Tn_TYPE_CNF_MASK;
ian@0 421 v |= Tn_TYPE_CNF_MASK;
ian@0 422 writeq(v, &timer->hpet_config);
ian@0 423 v |= Tn_VAL_SET_CNF_MASK;
ian@0 424 writeq(v, &timer->hpet_config);
ian@0 425 local_irq_save(flags);
ian@0 426 m = read_counter(&hpet->hpet_mc);
ian@0 427 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
ian@0 428 } else {
ian@0 429 local_irq_save(flags);
ian@0 430 m = read_counter(&hpet->hpet_mc);
ian@0 431 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
ian@0 432 }
ian@0 433
ian@0 434 if (devp->hd_flags & HPET_SHARED_IRQ) {
ian@0 435 isr = 1 << (devp - devp->hd_hpets->hp_dev);
ian@0 436 writel(isr, &hpet->hpet_isr);
ian@0 437 }
ian@0 438 writeq(g, &timer->hpet_config);
ian@0 439 local_irq_restore(flags);
ian@0 440
ian@0 441 return 0;
ian@0 442 }
ian@0 443
ian@0 444 /* converts Hz to number of timer ticks */
ian@0 445 static inline unsigned long hpet_time_div(struct hpets *hpets,
ian@0 446 unsigned long dis)
ian@0 447 {
ian@0 448 unsigned long long m;
ian@0 449
ian@0 450 m = hpets->hp_tick_freq + (dis >> 1);
ian@0 451 do_div(m, dis);
ian@0 452 return (unsigned long)m;
ian@0 453 }
ian@0 454
ian@0 455 static int
ian@0 456 hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel)
ian@0 457 {
ian@0 458 struct hpet_timer __iomem *timer;
ian@0 459 struct hpet __iomem *hpet;
ian@0 460 struct hpets *hpetp;
ian@0 461 int err;
ian@0 462 unsigned long v;
ian@0 463
ian@0 464 switch (cmd) {
ian@0 465 case HPET_IE_OFF:
ian@0 466 case HPET_INFO:
ian@0 467 case HPET_EPI:
ian@0 468 case HPET_DPI:
ian@0 469 case HPET_IRQFREQ:
ian@0 470 timer = devp->hd_timer;
ian@0 471 hpet = devp->hd_hpet;
ian@0 472 hpetp = devp->hd_hpets;
ian@0 473 break;
ian@0 474 case HPET_IE_ON:
ian@0 475 return hpet_ioctl_ieon(devp);
ian@0 476 default:
ian@0 477 return -EINVAL;
ian@0 478 }
ian@0 479
ian@0 480 err = 0;
ian@0 481
ian@0 482 switch (cmd) {
ian@0 483 case HPET_IE_OFF:
ian@0 484 if ((devp->hd_flags & HPET_IE) == 0)
ian@0 485 break;
ian@0 486 v = readq(&timer->hpet_config);
ian@0 487 v &= ~Tn_INT_ENB_CNF_MASK;
ian@0 488 writeq(v, &timer->hpet_config);
ian@0 489 if (devp->hd_irq) {
ian@0 490 free_irq(devp->hd_irq, devp);
ian@0 491 devp->hd_irq = 0;
ian@0 492 }
ian@0 493 devp->hd_flags ^= HPET_IE;
ian@0 494 break;
ian@0 495 case HPET_INFO:
ian@0 496 {
ian@0 497 struct hpet_info info;
ian@0 498
ian@0 499 if (devp->hd_ireqfreq)
ian@0 500 info.hi_ireqfreq =
ian@0 501 hpet_time_div(hpetp, devp->hd_ireqfreq);
ian@0 502 else
ian@0 503 info.hi_ireqfreq = 0;
ian@0 504 info.hi_flags =
ian@0 505 readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK;
ian@0 506 info.hi_hpet = hpetp->hp_which;
ian@0 507 info.hi_timer = devp - hpetp->hp_dev;
ian@0 508 if (kernel)
ian@0 509 memcpy((void *)arg, &info, sizeof(info));
ian@0 510 else
ian@0 511 if (copy_to_user((void __user *)arg, &info,
ian@0 512 sizeof(info)))
ian@0 513 err = -EFAULT;
ian@0 514 break;
ian@0 515 }
ian@0 516 case HPET_EPI:
ian@0 517 v = readq(&timer->hpet_config);
ian@0 518 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
ian@0 519 err = -ENXIO;
ian@0 520 break;
ian@0 521 }
ian@0 522 devp->hd_flags |= HPET_PERIODIC;
ian@0 523 break;
ian@0 524 case HPET_DPI:
ian@0 525 v = readq(&timer->hpet_config);
ian@0 526 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
ian@0 527 err = -ENXIO;
ian@0 528 break;
ian@0 529 }
ian@0 530 if (devp->hd_flags & HPET_PERIODIC &&
ian@0 531 readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
ian@0 532 v = readq(&timer->hpet_config);
ian@0 533 v ^= Tn_TYPE_CNF_MASK;
ian@0 534 writeq(v, &timer->hpet_config);
ian@0 535 }
ian@0 536 devp->hd_flags &= ~HPET_PERIODIC;
ian@0 537 break;
ian@0 538 case HPET_IRQFREQ:
ian@0 539 if (!kernel && (arg > hpet_max_freq) &&
ian@0 540 !capable(CAP_SYS_RESOURCE)) {
ian@0 541 err = -EACCES;
ian@0 542 break;
ian@0 543 }
ian@0 544
ian@0 545 if (!arg) {
ian@0 546 err = -EINVAL;
ian@0 547 break;
ian@0 548 }
ian@0 549
ian@0 550 devp->hd_ireqfreq = hpet_time_div(hpetp, arg);
ian@0 551 }
ian@0 552
ian@0 553 return err;
ian@0 554 }
ian@0 555
ian@0 556 static const struct file_operations hpet_fops = {
ian@0 557 .owner = THIS_MODULE,
ian@0 558 .llseek = no_llseek,
ian@0 559 .read = hpet_read,
ian@0 560 .poll = hpet_poll,
ian@0 561 .ioctl = hpet_ioctl,
ian@0 562 .open = hpet_open,
ian@0 563 .release = hpet_release,
ian@0 564 .fasync = hpet_fasync,
ian@0 565 .mmap = hpet_mmap,
ian@0 566 };
ian@0 567
ian@0 568 static int hpet_is_known(struct hpet_data *hdp)
ian@0 569 {
ian@0 570 struct hpets *hpetp;
ian@0 571
ian@0 572 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
ian@0 573 if (hpetp->hp_hpet_phys == hdp->hd_phys_address)
ian@0 574 return 1;
ian@0 575
ian@0 576 return 0;
ian@0 577 }
ian@0 578
ian@0 579 EXPORT_SYMBOL(hpet_alloc);
ian@0 580 EXPORT_SYMBOL(hpet_register);
ian@0 581 EXPORT_SYMBOL(hpet_unregister);
ian@0 582 EXPORT_SYMBOL(hpet_control);
ian@0 583
ian@0 584 int hpet_register(struct hpet_task *tp, int periodic)
ian@0 585 {
ian@0 586 unsigned int i;
ian@0 587 u64 mask;
ian@0 588 struct hpet_timer __iomem *timer;
ian@0 589 struct hpet_dev *devp;
ian@0 590 struct hpets *hpetp;
ian@0 591
ian@0 592 switch (periodic) {
ian@0 593 case 1:
ian@0 594 mask = Tn_PER_INT_CAP_MASK;
ian@0 595 break;
ian@0 596 case 0:
ian@0 597 mask = 0;
ian@0 598 break;
ian@0 599 default:
ian@0 600 return -EINVAL;
ian@0 601 }
ian@0 602
ian@0 603 tp->ht_opaque = NULL;
ian@0 604
ian@0 605 spin_lock_irq(&hpet_task_lock);
ian@0 606 spin_lock(&hpet_lock);
ian@0 607
ian@0 608 for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
ian@0 609 for (timer = hpetp->hp_hpet->hpet_timers, i = 0;
ian@0 610 i < hpetp->hp_ntimer; i++, timer++) {
ian@0 611 if ((readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK)
ian@0 612 != mask)
ian@0 613 continue;
ian@0 614
ian@0 615 devp = &hpetp->hp_dev[i];
ian@0 616
ian@0 617 if (devp->hd_flags & HPET_OPEN || devp->hd_task) {
ian@0 618 devp = NULL;
ian@0 619 continue;
ian@0 620 }
ian@0 621
ian@0 622 tp->ht_opaque = devp;
ian@0 623 devp->hd_task = tp;
ian@0 624 break;
ian@0 625 }
ian@0 626
ian@0 627 spin_unlock(&hpet_lock);
ian@0 628 spin_unlock_irq(&hpet_task_lock);
ian@0 629
ian@0 630 if (tp->ht_opaque)
ian@0 631 return 0;
ian@0 632 else
ian@0 633 return -EBUSY;
ian@0 634 }
ian@0 635
ian@0 636 static inline int hpet_tpcheck(struct hpet_task *tp)
ian@0 637 {
ian@0 638 struct hpet_dev *devp;
ian@0 639 struct hpets *hpetp;
ian@0 640
ian@0 641 devp = tp->ht_opaque;
ian@0 642
ian@0 643 if (!devp)
ian@0 644 return -ENXIO;
ian@0 645
ian@0 646 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
ian@0 647 if (devp >= hpetp->hp_dev
ian@0 648 && devp < (hpetp->hp_dev + hpetp->hp_ntimer)
ian@0 649 && devp->hd_hpet == hpetp->hp_hpet)
ian@0 650 return 0;
ian@0 651
ian@0 652 return -ENXIO;
ian@0 653 }
ian@0 654
ian@0 655 int hpet_unregister(struct hpet_task *tp)
ian@0 656 {
ian@0 657 struct hpet_dev *devp;
ian@0 658 struct hpet_timer __iomem *timer;
ian@0 659 int err;
ian@0 660
ian@0 661 if ((err = hpet_tpcheck(tp)))
ian@0 662 return err;
ian@0 663
ian@0 664 spin_lock_irq(&hpet_task_lock);
ian@0 665 spin_lock(&hpet_lock);
ian@0 666
ian@0 667 devp = tp->ht_opaque;
ian@0 668 if (devp->hd_task != tp) {
ian@0 669 spin_unlock(&hpet_lock);
ian@0 670 spin_unlock_irq(&hpet_task_lock);
ian@0 671 return -ENXIO;
ian@0 672 }
ian@0 673
ian@0 674 timer = devp->hd_timer;
ian@0 675 writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
ian@0 676 &timer->hpet_config);
ian@0 677 devp->hd_flags &= ~(HPET_IE | HPET_PERIODIC);
ian@0 678 devp->hd_task = NULL;
ian@0 679 spin_unlock(&hpet_lock);
ian@0 680 spin_unlock_irq(&hpet_task_lock);
ian@0 681
ian@0 682 return 0;
ian@0 683 }
ian@0 684
ian@0 685 int hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg)
ian@0 686 {
ian@0 687 struct hpet_dev *devp;
ian@0 688 int err;
ian@0 689
ian@0 690 if ((err = hpet_tpcheck(tp)))
ian@0 691 return err;
ian@0 692
ian@0 693 spin_lock_irq(&hpet_lock);
ian@0 694 devp = tp->ht_opaque;
ian@0 695 if (devp->hd_task != tp) {
ian@0 696 spin_unlock_irq(&hpet_lock);
ian@0 697 return -ENXIO;
ian@0 698 }
ian@0 699 spin_unlock_irq(&hpet_lock);
ian@0 700 return hpet_ioctl_common(devp, cmd, arg, 1);
ian@0 701 }
ian@0 702
ian@0 703 static ctl_table hpet_table[] = {
ian@0 704 {
ian@0 705 .ctl_name = 1,
ian@0 706 .procname = "max-user-freq",
ian@0 707 .data = &hpet_max_freq,
ian@0 708 .maxlen = sizeof(int),
ian@0 709 .mode = 0644,
ian@0 710 .proc_handler = &proc_dointvec,
ian@0 711 },
ian@0 712 {.ctl_name = 0}
ian@0 713 };
ian@0 714
ian@0 715 static ctl_table hpet_root[] = {
ian@0 716 {
ian@0 717 .ctl_name = 1,
ian@0 718 .procname = "hpet",
ian@0 719 .maxlen = 0,
ian@0 720 .mode = 0555,
ian@0 721 .child = hpet_table,
ian@0 722 },
ian@0 723 {.ctl_name = 0}
ian@0 724 };
ian@0 725
ian@0 726 static ctl_table dev_root[] = {
ian@0 727 {
ian@0 728 .ctl_name = CTL_DEV,
ian@0 729 .procname = "dev",
ian@0 730 .maxlen = 0,
ian@0 731 .mode = 0555,
ian@0 732 .child = hpet_root,
ian@0 733 },
ian@0 734 {.ctl_name = 0}
ian@0 735 };
ian@0 736
ian@0 737 static struct ctl_table_header *sysctl_header;
ian@0 738
ian@0 739 static void hpet_register_interpolator(struct hpets *hpetp)
ian@0 740 {
ian@0 741 #ifdef CONFIG_TIME_INTERPOLATION
ian@0 742 struct time_interpolator *ti;
ian@0 743
ian@0 744 ti = kzalloc(sizeof(*ti), GFP_KERNEL);
ian@0 745 if (!ti)
ian@0 746 return;
ian@0 747
ian@0 748 ti->source = TIME_SOURCE_MMIO64;
ian@0 749 ti->shift = 10;
ian@0 750 ti->addr = &hpetp->hp_hpet->hpet_mc;
ian@0 751 ti->frequency = hpetp->hp_tick_freq;
ian@0 752 ti->drift = HPET_DRIFT;
ian@0 753 ti->mask = -1;
ian@0 754
ian@0 755 hpetp->hp_interpolator = ti;
ian@0 756 register_time_interpolator(ti);
ian@0 757 #endif
ian@0 758 }
ian@0 759
ian@0 760 /*
ian@0 761 * Adjustment for when arming the timer with
ian@0 762 * initial conditions. That is, main counter
ian@0 763 * ticks expired before interrupts are enabled.
ian@0 764 */
ian@0 765 #define TICK_CALIBRATE (1000UL)
ian@0 766
ian@0 767 static unsigned long hpet_calibrate(struct hpets *hpetp)
ian@0 768 {
ian@0 769 struct hpet_timer __iomem *timer = NULL;
ian@0 770 unsigned long t, m, count, i, flags, start;
ian@0 771 struct hpet_dev *devp;
ian@0 772 int j;
ian@0 773 struct hpet __iomem *hpet;
ian@0 774
ian@0 775 for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
ian@0 776 if ((devp->hd_flags & HPET_OPEN) == 0) {
ian@0 777 timer = devp->hd_timer;
ian@0 778 break;
ian@0 779 }
ian@0 780
ian@0 781 if (!timer)
ian@0 782 return 0;
ian@0 783
ian@0 784 hpet = hpetp->hp_hpet;
ian@0 785 t = read_counter(&timer->hpet_compare);
ian@0 786
ian@0 787 i = 0;
ian@0 788 count = hpet_time_div(hpetp, TICK_CALIBRATE);
ian@0 789
ian@0 790 local_irq_save(flags);
ian@0 791
ian@0 792 start = read_counter(&hpet->hpet_mc);
ian@0 793
ian@0 794 do {
ian@0 795 m = read_counter(&hpet->hpet_mc);
ian@0 796 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
ian@0 797 } while (i++, (m - start) < count);
ian@0 798
ian@0 799 local_irq_restore(flags);
ian@0 800
ian@0 801 return (m - start) / i;
ian@0 802 }
ian@0 803
ian@0 804 int hpet_alloc(struct hpet_data *hdp)
ian@0 805 {
ian@0 806 u64 cap, mcfg;
ian@0 807 struct hpet_dev *devp;
ian@0 808 u32 i, ntimer;
ian@0 809 struct hpets *hpetp;
ian@0 810 size_t siz;
ian@0 811 struct hpet __iomem *hpet;
ian@0 812 static struct hpets *last = NULL;
ian@0 813 unsigned long period;
ian@0 814 unsigned long long temp;
ian@0 815
ian@0 816 /*
ian@0 817 * hpet_alloc can be called by platform dependent code.
ian@0 818 * If platform dependent code has allocated the hpet that
ian@0 819 * ACPI has also reported, then we catch it here.
ian@0 820 */
ian@0 821 if (hpet_is_known(hdp)) {
ian@0 822 printk(KERN_DEBUG "%s: duplicate HPET ignored\n",
ian@0 823 __FUNCTION__);
ian@0 824 return 0;
ian@0 825 }
ian@0 826
ian@0 827 siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
ian@0 828 sizeof(struct hpet_dev));
ian@0 829
ian@0 830 hpetp = kzalloc(siz, GFP_KERNEL);
ian@0 831
ian@0 832 if (!hpetp)
ian@0 833 return -ENOMEM;
ian@0 834
ian@0 835 hpetp->hp_which = hpet_nhpet++;
ian@0 836 hpetp->hp_hpet = hdp->hd_address;
ian@0 837 hpetp->hp_hpet_phys = hdp->hd_phys_address;
ian@0 838
ian@0 839 hpetp->hp_ntimer = hdp->hd_nirqs;
ian@0 840
ian@0 841 for (i = 0; i < hdp->hd_nirqs; i++)
ian@0 842 hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
ian@0 843
ian@0 844 hpet = hpetp->hp_hpet;
ian@0 845
ian@0 846 cap = readq(&hpet->hpet_cap);
ian@0 847
ian@0 848 ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
ian@0 849
ian@0 850 if (hpetp->hp_ntimer != ntimer) {
ian@0 851 printk(KERN_WARNING "hpet: number irqs doesn't agree"
ian@0 852 " with number of timers\n");
ian@0 853 kfree(hpetp);
ian@0 854 return -ENODEV;
ian@0 855 }
ian@0 856
ian@0 857 if (last)
ian@0 858 last->hp_next = hpetp;
ian@0 859 else
ian@0 860 hpets = hpetp;
ian@0 861
ian@0 862 last = hpetp;
ian@0 863
ian@0 864 period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
ian@0 865 HPET_COUNTER_CLK_PERIOD_SHIFT; /* fs, 10^-15 */
ian@0 866 temp = 1000000000000000uLL; /* 10^15 femtoseconds per second */
ian@0 867 temp += period >> 1; /* round */
ian@0 868 do_div(temp, period);
ian@0 869 hpetp->hp_tick_freq = temp; /* ticks per second */
ian@0 870
ian@0 871 printk(KERN_INFO "hpet%d: at MMIO 0x%lx (virtual 0x%p), IRQ%s",
ian@0 872 hpetp->hp_which, hdp->hd_phys_address, hdp->hd_address,
ian@0 873 hpetp->hp_ntimer > 1 ? "s" : "");
ian@0 874 for (i = 0; i < hpetp->hp_ntimer; i++)
ian@0 875 printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]);
ian@0 876 printk("\n");
ian@0 877
ian@0 878 printk(KERN_INFO "hpet%u: %u %d-bit timers, %Lu Hz\n",
ian@0 879 hpetp->hp_which, hpetp->hp_ntimer,
ian@0 880 cap & HPET_COUNTER_SIZE_MASK ? 64 : 32, hpetp->hp_tick_freq);
ian@0 881
ian@0 882 mcfg = readq(&hpet->hpet_config);
ian@0 883 if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
ian@0 884 write_counter(0L, &hpet->hpet_mc);
ian@0 885 mcfg |= HPET_ENABLE_CNF_MASK;
ian@0 886 writeq(mcfg, &hpet->hpet_config);
ian@0 887 }
ian@0 888
ian@0 889 for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer; i++, devp++) {
ian@0 890 struct hpet_timer __iomem *timer;
ian@0 891
ian@0 892 timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
ian@0 893
ian@0 894 devp->hd_hpets = hpetp;
ian@0 895 devp->hd_hpet = hpet;
ian@0 896 devp->hd_timer = timer;
ian@0 897
ian@0 898 /*
ian@0 899 * If the timer was reserved by platform code,
ian@0 900 * then make timer unavailable for opens.
ian@0 901 */
ian@0 902 if (hdp->hd_state & (1 << i)) {
ian@0 903 devp->hd_flags = HPET_OPEN;
ian@0 904 continue;
ian@0 905 }
ian@0 906
ian@0 907 init_waitqueue_head(&devp->hd_waitqueue);
ian@0 908 }
ian@0 909
ian@0 910 hpetp->hp_delta = hpet_calibrate(hpetp);
ian@0 911 hpet_register_interpolator(hpetp);
ian@0 912
ian@0 913 return 0;
ian@0 914 }
ian@0 915
ian@0 916 static acpi_status hpet_resources(struct acpi_resource *res, void *data)
ian@0 917 {
ian@0 918 struct hpet_data *hdp;
ian@0 919 acpi_status status;
ian@0 920 struct acpi_resource_address64 addr;
ian@0 921
ian@0 922 hdp = data;
ian@0 923
ian@0 924 status = acpi_resource_to_address64(res, &addr);
ian@0 925
ian@0 926 if (ACPI_SUCCESS(status)) {
ian@0 927 hdp->hd_phys_address = addr.minimum;
ian@0 928 hdp->hd_address = ioremap(addr.minimum, addr.address_length);
ian@0 929
ian@0 930 if (hpet_is_known(hdp)) {
ian@0 931 printk(KERN_DEBUG "%s: 0x%lx is busy\n",
ian@0 932 __FUNCTION__, hdp->hd_phys_address);
ian@0 933 iounmap(hdp->hd_address);
ian@0 934 return -EBUSY;
ian@0 935 }
ian@0 936 } else if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
ian@0 937 struct acpi_resource_fixed_memory32 *fixmem32;
ian@0 938
ian@0 939 fixmem32 = &res->data.fixed_memory32;
ian@0 940 if (!fixmem32)
ian@0 941 return -EINVAL;
ian@0 942
ian@0 943 hdp->hd_phys_address = fixmem32->address;
ian@0 944 hdp->hd_address = ioremap(fixmem32->address,
ian@0 945 HPET_RANGE_SIZE);
ian@0 946
ian@0 947 if (hpet_is_known(hdp)) {
ian@0 948 printk(KERN_DEBUG "%s: 0x%lx is busy\n",
ian@0 949 __FUNCTION__, hdp->hd_phys_address);
ian@0 950 iounmap(hdp->hd_address);
ian@0 951 return -EBUSY;
ian@0 952 }
ian@0 953 } else if (res->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
ian@0 954 struct acpi_resource_extended_irq *irqp;
ian@0 955 int i, irq;
ian@0 956
ian@0 957 irqp = &res->data.extended_irq;
ian@0 958
ian@0 959 for (i = 0; i < irqp->interrupt_count; i++) {
ian@0 960 irq = acpi_register_gsi(irqp->interrupts[i],
ian@0 961 irqp->triggering, irqp->polarity);
ian@0 962 if (irq < 0)
ian@0 963 return AE_ERROR;
ian@0 964
ian@0 965 hdp->hd_irq[hdp->hd_nirqs] = irq;
ian@0 966 hdp->hd_nirqs++;
ian@0 967 }
ian@0 968 }
ian@0 969
ian@0 970 return AE_OK;
ian@0 971 }
ian@0 972
ian@0 973 static int hpet_acpi_add(struct acpi_device *device)
ian@0 974 {
ian@0 975 acpi_status result;
ian@0 976 struct hpet_data data;
ian@0 977
ian@0 978 memset(&data, 0, sizeof(data));
ian@0 979
ian@0 980 result =
ian@0 981 acpi_walk_resources(device->handle, METHOD_NAME__CRS,
ian@0 982 hpet_resources, &data);
ian@0 983
ian@0 984 if (ACPI_FAILURE(result))
ian@0 985 return -ENODEV;
ian@0 986
ian@0 987 if (!data.hd_address || !data.hd_nirqs) {
ian@0 988 printk("%s: no address or irqs in _CRS\n", __FUNCTION__);
ian@0 989 return -ENODEV;
ian@0 990 }
ian@0 991
ian@0 992 return hpet_alloc(&data);
ian@0 993 }
ian@0 994
ian@0 995 static int hpet_acpi_remove(struct acpi_device *device, int type)
ian@0 996 {
ian@0 997 /* XXX need to unregister interpolator, dealloc mem, etc */
ian@0 998 return -EINVAL;
ian@0 999 }
ian@0 1000
ian@0 1001 static struct acpi_driver hpet_acpi_driver = {
ian@0 1002 .name = "hpet",
ian@0 1003 .ids = "PNP0103",
ian@0 1004 .ops = {
ian@0 1005 .add = hpet_acpi_add,
ian@0 1006 .remove = hpet_acpi_remove,
ian@0 1007 },
ian@0 1008 };
ian@0 1009
ian@0 1010 static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
ian@0 1011
ian@0 1012 static int __init hpet_init(void)
ian@0 1013 {
ian@0 1014 int result;
ian@0 1015
ian@0 1016 result = misc_register(&hpet_misc);
ian@0 1017 if (result < 0)
ian@0 1018 return -ENODEV;
ian@0 1019
ian@0 1020 sysctl_header = register_sysctl_table(dev_root, 0);
ian@0 1021
ian@0 1022 result = acpi_bus_register_driver(&hpet_acpi_driver);
ian@0 1023 if (result < 0) {
ian@0 1024 if (sysctl_header)
ian@0 1025 unregister_sysctl_table(sysctl_header);
ian@0 1026 misc_deregister(&hpet_misc);
ian@0 1027 return result;
ian@0 1028 }
ian@0 1029
ian@0 1030 return 0;
ian@0 1031 }
ian@0 1032
ian@0 1033 static void __exit hpet_exit(void)
ian@0 1034 {
ian@0 1035 acpi_bus_unregister_driver(&hpet_acpi_driver);
ian@0 1036
ian@0 1037 if (sysctl_header)
ian@0 1038 unregister_sysctl_table(sysctl_header);
ian@0 1039 misc_deregister(&hpet_misc);
ian@0 1040
ian@0 1041 return;
ian@0 1042 }
ian@0 1043
ian@0 1044 module_init(hpet_init);
ian@0 1045 module_exit(hpet_exit);
ian@0 1046 MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
ian@0 1047 MODULE_LICENSE("GPL");