uv_mmtimer.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. .llseek = noop_llseek,
  49. };
  50. /**
  51. * uv_mmtimer_ioctl - ioctl interface for /dev/uv_mmtimer
  52. * @file: file structure for the device
  53. * @cmd: command to execute
  54. * @arg: optional argument to command
  55. *
  56. * Executes the command specified by @cmd. Returns 0 for success, < 0 for
  57. * failure.
  58. *
  59. * Valid commands:
  60. *
  61. * %MMTIMER_GETOFFSET - Should return the offset (relative to the start
  62. * of the page where the registers are mapped) for the counter in question.
  63. *
  64. * %MMTIMER_GETRES - Returns the resolution of the clock in femto (10^-15)
  65. * seconds
  66. *
  67. * %MMTIMER_GETFREQ - Copies the frequency of the clock in Hz to the address
  68. * specified by @arg
  69. *
  70. * %MMTIMER_GETBITS - Returns the number of bits in the clock's counter
  71. *
  72. * %MMTIMER_MMAPAVAIL - Returns 1 if registers can be mmap'd into userspace
  73. *
  74. * %MMTIMER_GETCOUNTER - Gets the current value in the counter and places it
  75. * in the address specified by @arg.
  76. */
  77. static long uv_mmtimer_ioctl(struct file *file, unsigned int cmd,
  78. unsigned long arg)
  79. {
  80. int ret = 0;
  81. switch (cmd) {
  82. case MMTIMER_GETOFFSET: /* offset of the counter */
  83. /*
  84. * Starting with HUB rev 2.0, the UV RTC register is
  85. * replicated across all cachelines of it's own page.
  86. * This allows faster simultaneous reads from a given socket.
  87. *
  88. * The offset returned is in 64 bit units.
  89. */
  90. if (uv_get_min_hub_revision_id() == 1)
  91. ret = 0;
  92. else
  93. ret = ((uv_blade_processor_id() * L1_CACHE_BYTES) %
  94. PAGE_SIZE) / 8;
  95. break;
  96. case MMTIMER_GETRES: /* resolution of the clock in 10^-15 s */
  97. if (copy_to_user((unsigned long __user *)arg,
  98. &uv_mmtimer_femtoperiod, sizeof(unsigned long)))
  99. ret = -EFAULT;
  100. break;
  101. case MMTIMER_GETFREQ: /* frequency in Hz */
  102. if (copy_to_user((unsigned long __user *)arg,
  103. &sn_rtc_cycles_per_second,
  104. sizeof(unsigned long)))
  105. ret = -EFAULT;
  106. break;
  107. case MMTIMER_GETBITS: /* number of bits in the clock */
  108. ret = hweight64(UVH_RTC_REAL_TIME_CLOCK_MASK);
  109. break;
  110. case MMTIMER_MMAPAVAIL:
  111. ret = 1;
  112. break;
  113. case MMTIMER_GETCOUNTER:
  114. if (copy_to_user((unsigned long __user *)arg,
  115. (unsigned long *)uv_local_mmr_address(UVH_RTC),
  116. sizeof(unsigned long)))
  117. ret = -EFAULT;
  118. break;
  119. default:
  120. ret = -ENOTTY;
  121. break;
  122. }
  123. return ret;
  124. }
  125. /**
  126. * uv_mmtimer_mmap - maps the clock's registers into userspace
  127. * @file: file structure for the device
  128. * @vma: VMA to map the registers into
  129. *
  130. * Calls remap_pfn_range() to map the clock's registers into
  131. * the calling process' address space.
  132. */
  133. static int uv_mmtimer_mmap(struct file *file, struct vm_area_struct *vma)
  134. {
  135. unsigned long uv_mmtimer_addr;
  136. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  137. return -EINVAL;
  138. if (vma->vm_flags & VM_WRITE)
  139. return -EPERM;
  140. if (PAGE_SIZE > (1 << 16))
  141. return -ENOSYS;
  142. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  143. uv_mmtimer_addr = UV_LOCAL_MMR_BASE | UVH_RTC;
  144. uv_mmtimer_addr &= ~(PAGE_SIZE - 1);
  145. uv_mmtimer_addr &= 0xfffffffffffffffUL;
  146. if (remap_pfn_range(vma, vma->vm_start, uv_mmtimer_addr >> PAGE_SHIFT,
  147. PAGE_SIZE, vma->vm_page_prot)) {
  148. printk(KERN_ERR "remap_pfn_range failed in uv_mmtimer_mmap\n");
  149. return -EAGAIN;
  150. }
  151. return 0;
  152. }
  153. static struct miscdevice uv_mmtimer_miscdev = {
  154. MISC_DYNAMIC_MINOR,
  155. UV_MMTIMER_NAME,
  156. &uv_mmtimer_fops
  157. };
  158. /**
  159. * uv_mmtimer_init - device initialization routine
  160. *
  161. * Does initial setup for the uv_mmtimer device.
  162. */
  163. static int __init uv_mmtimer_init(void)
  164. {
  165. if (!is_uv_system()) {
  166. printk(KERN_ERR "%s: Hardware unsupported\n", UV_MMTIMER_NAME);
  167. return -1;
  168. }
  169. /*
  170. * Sanity check the cycles/sec variable
  171. */
  172. if (sn_rtc_cycles_per_second < 100000) {
  173. printk(KERN_ERR "%s: unable to determine clock frequency\n",
  174. UV_MMTIMER_NAME);
  175. return -1;
  176. }
  177. uv_mmtimer_femtoperiod = ((unsigned long)1E15 +
  178. sn_rtc_cycles_per_second / 2) /
  179. sn_rtc_cycles_per_second;
  180. if (misc_register(&uv_mmtimer_miscdev)) {
  181. printk(KERN_ERR "%s: failed to register device\n",
  182. UV_MMTIMER_NAME);
  183. return -1;
  184. }
  185. printk(KERN_INFO "%s: v%s, %ld MHz\n", UV_MMTIMER_DESC,
  186. UV_MMTIMER_VERSION,
  187. sn_rtc_cycles_per_second/(unsigned long)1E6);
  188. return 0;
  189. }
  190. module_init(uv_mmtimer_init);