2 * Timer device implementation for SGI UV platform.
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (c) 2009 Silicon Graphics, Inc. All rights reserved.
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/ioctl.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/errno.h>
20 #include <linux/mmtimer.h>
21 #include <linux/miscdevice.h>
22 #include <linux/posix-timers.h>
23 #include <linux/interrupt.h>
24 #include <linux/time.h>
25 #include <linux/math64.h>
26 #include <linux/smp_lock.h>
28 #include <asm/genapic.h>
29 #include <asm/uv/uv_hub.h>
30 #include <asm/uv/bios.h>
31 #include <asm/uv/uv.h>
33 MODULE_AUTHOR("Dimitri Sivanich <sivanich@sgi.com>");
34 MODULE_DESCRIPTION("SGI UV Memory Mapped RTC Timer");
35 MODULE_LICENSE("GPL");
37 /* name of the device, usually in /dev */
38 #define UV_MMTIMER_NAME "mmtimer"
39 #define UV_MMTIMER_DESC "SGI UV Memory Mapped RTC Timer"
40 #define UV_MMTIMER_VERSION "1.0"
42 static long uv_mmtimer_ioctl(struct file
*file
, unsigned int cmd
,
44 static int uv_mmtimer_mmap(struct file
*file
, struct vm_area_struct
*vma
);
47 * Period in femtoseconds (10^-15 s)
49 static unsigned long uv_mmtimer_femtoperiod
;
51 static const struct file_operations uv_mmtimer_fops
= {
53 .mmap
= uv_mmtimer_mmap
,
54 .unlocked_ioctl
= uv_mmtimer_ioctl
,
55 .llseek
= noop_llseek
,
59 * uv_mmtimer_ioctl - ioctl interface for /dev/uv_mmtimer
60 * @file: file structure for the device
61 * @cmd: command to execute
62 * @arg: optional argument to command
64 * Executes the command specified by @cmd. Returns 0 for success, < 0 for
69 * %MMTIMER_GETOFFSET - Should return the offset (relative to the start
70 * of the page where the registers are mapped) for the counter in question.
72 * %MMTIMER_GETRES - Returns the resolution of the clock in femto (10^-15)
75 * %MMTIMER_GETFREQ - Copies the frequency of the clock in Hz to the address
78 * %MMTIMER_GETBITS - Returns the number of bits in the clock's counter
80 * %MMTIMER_MMAPAVAIL - Returns 1 if registers can be mmap'd into userspace
82 * %MMTIMER_GETCOUNTER - Gets the current value in the counter and places it
83 * in the address specified by @arg.
85 static long uv_mmtimer_ioctl(struct file
*file
, unsigned int cmd
,
91 case MMTIMER_GETOFFSET
: /* offset of the counter */
93 * Starting with HUB rev 2.0, the UV RTC register is
94 * replicated across all cachelines of it's own page.
95 * This allows faster simultaneous reads from a given socket.
97 * The offset returned is in 64 bit units.
99 if (uv_get_min_hub_revision_id() == 1)
102 ret
= ((uv_blade_processor_id() * L1_CACHE_BYTES
) %
106 case MMTIMER_GETRES
: /* resolution of the clock in 10^-15 s */
107 if (copy_to_user((unsigned long __user
*)arg
,
108 &uv_mmtimer_femtoperiod
, sizeof(unsigned long)))
112 case MMTIMER_GETFREQ
: /* frequency in Hz */
113 if (copy_to_user((unsigned long __user
*)arg
,
114 &sn_rtc_cycles_per_second
,
115 sizeof(unsigned long)))
119 case MMTIMER_GETBITS
: /* number of bits in the clock */
120 ret
= hweight64(UVH_RTC_REAL_TIME_CLOCK_MASK
);
123 case MMTIMER_MMAPAVAIL
:
127 case MMTIMER_GETCOUNTER
:
128 if (copy_to_user((unsigned long __user
*)arg
,
129 (unsigned long *)uv_local_mmr_address(UVH_RTC
),
130 sizeof(unsigned long)))
141 * uv_mmtimer_mmap - maps the clock's registers into userspace
142 * @file: file structure for the device
143 * @vma: VMA to map the registers into
145 * Calls remap_pfn_range() to map the clock's registers into
146 * the calling process' address space.
148 static int uv_mmtimer_mmap(struct file
*file
, struct vm_area_struct
*vma
)
150 unsigned long uv_mmtimer_addr
;
152 if (vma
->vm_end
- vma
->vm_start
!= PAGE_SIZE
)
155 if (vma
->vm_flags
& VM_WRITE
)
158 if (PAGE_SIZE
> (1 << 16))
161 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
163 uv_mmtimer_addr
= UV_LOCAL_MMR_BASE
| UVH_RTC
;
164 uv_mmtimer_addr
&= ~(PAGE_SIZE
- 1);
165 uv_mmtimer_addr
&= 0xfffffffffffffffUL
;
167 if (remap_pfn_range(vma
, vma
->vm_start
, uv_mmtimer_addr
>> PAGE_SHIFT
,
168 PAGE_SIZE
, vma
->vm_page_prot
)) {
169 printk(KERN_ERR
"remap_pfn_range failed in uv_mmtimer_mmap\n");
176 static struct miscdevice uv_mmtimer_miscdev
= {
184 * uv_mmtimer_init - device initialization routine
186 * Does initial setup for the uv_mmtimer device.
188 static int __init
uv_mmtimer_init(void)
190 if (!is_uv_system()) {
191 printk(KERN_ERR
"%s: Hardware unsupported\n", UV_MMTIMER_NAME
);
196 * Sanity check the cycles/sec variable
198 if (sn_rtc_cycles_per_second
< 100000) {
199 printk(KERN_ERR
"%s: unable to determine clock frequency\n",
204 uv_mmtimer_femtoperiod
= ((unsigned long)1E15
+
205 sn_rtc_cycles_per_second
/ 2) /
206 sn_rtc_cycles_per_second
;
208 if (misc_register(&uv_mmtimer_miscdev
)) {
209 printk(KERN_ERR
"%s: failed to register device\n",
214 printk(KERN_INFO
"%s: v%s, %ld MHz\n", UV_MMTIMER_DESC
,
216 sn_rtc_cycles_per_second
/(unsigned long)1E6
);
221 module_init(uv_mmtimer_init
);