uv_mmtimer.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Timer device implementation for SGI UV platform.
  3. *
  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
  6. * for more details.
  7. *
  8. * Copyright (c) 2009 Silicon Graphics, Inc. All rights reserved.
  9. *
  10. */
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/ioctl.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/errno.h>
  17. #include <linux/mm.h>
  18. #include <linux/fs.h>
  19. #include <linux/mmtimer.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/posix-timers.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/time.h>
  24. #include <linux/math64.h>
  25. #include <linux/smp_lock.h>
  26. #include <asm/genapic.h>
  27. #include <asm/uv/uv_hub.h>
  28. #include <asm/uv/bios.h>
  29. #include <asm/uv/uv.h>
  30. MODULE_AUTHOR("Dimitri Sivanich <sivanich@sgi.com>");
  31. MODULE_DESCRIPTION("SGI UV Memory Mapped RTC Timer");
  32. MODULE_LICENSE("GPL");
  33. /* name of the device, usually in /dev */
  34. #define UV_MMTIMER_NAME "mmtimer"
  35. #define UV_MMTIMER_DESC "SGI UV Memory Mapped RTC Timer"
  36. #define UV_MMTIMER_VERSION "1.0"
  37. static long uv_mmtimer_ioctl(struct file *file, unsigned int cmd,
  38. unsigned long arg);
  39. static int uv_mmtimer_mmap(struct file *file, struct vm_area_struct *vma);
  40. /*
  41. * Period in femtoseconds (10^-15 s)
  42. */
  43. static unsigned long uv_mmtimer_femtoperiod;
  44. static const struct file_operations uv_mmtimer_fops = {
  45. .owner = THIS_MODULE,
  46. .mmap = uv_mmtimer_mmap,
  47. .unlocked_ioctl = uv_mmtimer_ioctl,
  48. };
  49. /**
  50. * uv_mmtimer_ioctl - ioctl interface for /dev/uv_mmtimer
  51. * @file: file structure for the device
  52. * @cmd: command to execute
  53. * @arg: optional argument to command
  54. *
  55. * Executes the command specified by @cmd. Returns 0 for success, < 0 for
  56. * failure.
  57. *
  58. * Valid commands:
  59. *
  60. * %MMTIMER_GETOFFSET - Should return the offset (relative to the start
  61. * of the page where the registers are mapped) for the counter in question.
  62. *
  63. * %MMTIMER_GETRES - Returns the resolution of the clock in femto (10^-15)
  64. * seconds
  65. *
  66. * %MMTIMER_GETFREQ - Copies the frequency of the clock in Hz to the address
  67. * specified by @arg
  68. *
  69. * %MMTIMER_GETBITS - Returns the number of bits in the clock's counter
  70. *
  71. * %MMTIMER_MMAPAVAIL - Returns 1 if registers can be mmap'd into userspace
  72. *
  73. * %MMTIMER_GETCOUNTER - Gets the current value in the counter and places it
  74. * in the address specified by @arg.
  75. */
  76. static long uv_mmtimer_ioctl(struct file *file, unsigned int cmd,
  77. unsigned long arg)
  78. {
  79. int ret = 0;
  80. switch (cmd) {
  81. case MMTIMER_GETOFFSET: /* offset of the counter */
  82. /*
  83. * UV RTC register is on its own page
  84. */
  85. if (PAGE_SIZE <= (1 << 16))
  86. ret = ((UV_LOCAL_MMR_BASE | UVH_RTC) & (PAGE_SIZE-1))
  87. / 8;
  88. else
  89. ret = -ENOSYS;
  90. break;
  91. case MMTIMER_GETRES: /* resolution of the clock in 10^-15 s */
  92. if (copy_to_user((unsigned long __user *)arg,
  93. &uv_mmtimer_femtoperiod, sizeof(unsigned long)))
  94. ret = -EFAULT;
  95. break;
  96. case MMTIMER_GETFREQ: /* frequency in Hz */
  97. if (copy_to_user((unsigned long __user *)arg,
  98. &sn_rtc_cycles_per_second,
  99. sizeof(unsigned long)))
  100. ret = -EFAULT;
  101. break;
  102. case MMTIMER_GETBITS: /* number of bits in the clock */
  103. ret = hweight64(UVH_RTC_REAL_TIME_CLOCK_MASK);
  104. break;
  105. case MMTIMER_MMAPAVAIL: /* can we mmap the clock into userspace? */
  106. ret = (PAGE_SIZE <= (1 << 16)) ? 1 : 0;
  107. break;
  108. case MMTIMER_GETCOUNTER:
  109. if (copy_to_user((unsigned long __user *)arg,
  110. (unsigned long *)uv_local_mmr_address(UVH_RTC),
  111. sizeof(unsigned long)))
  112. ret = -EFAULT;
  113. break;
  114. default:
  115. ret = -ENOTTY;
  116. break;
  117. }
  118. return ret;
  119. }
  120. /**
  121. * uv_mmtimer_mmap - maps the clock's registers into userspace
  122. * @file: file structure for the device
  123. * @vma: VMA to map the registers into
  124. *
  125. * Calls remap_pfn_range() to map the clock's registers into
  126. * the calling process' address space.
  127. */
  128. static int uv_mmtimer_mmap(struct file *file, struct vm_area_struct *vma)
  129. {
  130. unsigned long uv_mmtimer_addr;
  131. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  132. return -EINVAL;
  133. if (vma->vm_flags & VM_WRITE)
  134. return -EPERM;
  135. if (PAGE_SIZE > (1 << 16))
  136. return -ENOSYS;
  137. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  138. uv_mmtimer_addr = UV_LOCAL_MMR_BASE | UVH_RTC;
  139. uv_mmtimer_addr &= ~(PAGE_SIZE - 1);
  140. uv_mmtimer_addr &= 0xfffffffffffffffUL;
  141. if (remap_pfn_range(vma, vma->vm_start, uv_mmtimer_addr >> PAGE_SHIFT,
  142. PAGE_SIZE, vma->vm_page_prot)) {
  143. printk(KERN_ERR "remap_pfn_range failed in uv_mmtimer_mmap\n");
  144. return -EAGAIN;
  145. }
  146. return 0;
  147. }
  148. static struct miscdevice uv_mmtimer_miscdev = {
  149. MISC_DYNAMIC_MINOR,
  150. UV_MMTIMER_NAME,
  151. &uv_mmtimer_fops
  152. };
  153. /**
  154. * uv_mmtimer_init - device initialization routine
  155. *
  156. * Does initial setup for the uv_mmtimer device.
  157. */
  158. static int __init uv_mmtimer_init(void)
  159. {
  160. if (!is_uv_system()) {
  161. printk(KERN_ERR "%s: Hardware unsupported\n", UV_MMTIMER_NAME);
  162. return -1;
  163. }
  164. /*
  165. * Sanity check the cycles/sec variable
  166. */
  167. if (sn_rtc_cycles_per_second < 100000) {
  168. printk(KERN_ERR "%s: unable to determine clock frequency\n",
  169. UV_MMTIMER_NAME);
  170. return -1;
  171. }
  172. uv_mmtimer_femtoperiod = ((unsigned long)1E15 +
  173. sn_rtc_cycles_per_second / 2) /
  174. sn_rtc_cycles_per_second;
  175. if (misc_register(&uv_mmtimer_miscdev)) {
  176. printk(KERN_ERR "%s: failed to register device\n",
  177. UV_MMTIMER_NAME);
  178. return -1;
  179. }
  180. printk(KERN_INFO "%s: v%s, %ld MHz\n", UV_MMTIMER_DESC,
  181. UV_MMTIMER_VERSION,
  182. sn_rtc_cycles_per_second/(unsigned long)1E6);
  183. return 0;
  184. }
  185. module_init(uv_mmtimer_init);