uv_mmtimer.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <asm/genapic.h>
  26. #include <asm/uv/uv_hub.h>
  27. #include <asm/uv/bios.h>
  28. #include <asm/uv/uv.h>
  29. MODULE_AUTHOR("Dimitri Sivanich <sivanich@sgi.com>");
  30. MODULE_DESCRIPTION("SGI UV Memory Mapped RTC Timer");
  31. MODULE_LICENSE("GPL");
  32. /* name of the device, usually in /dev */
  33. #define UV_MMTIMER_NAME "mmtimer"
  34. #define UV_MMTIMER_DESC "SGI UV Memory Mapped RTC Timer"
  35. #define UV_MMTIMER_VERSION "1.0"
  36. static long uv_mmtimer_ioctl(struct file *file, unsigned int cmd,
  37. unsigned long arg);
  38. static int uv_mmtimer_mmap(struct file *file, struct vm_area_struct *vma);
  39. /*
  40. * Period in femtoseconds (10^-15 s)
  41. */
  42. static unsigned long uv_mmtimer_femtoperiod;
  43. static const struct file_operations uv_mmtimer_fops = {
  44. .owner = THIS_MODULE,
  45. .mmap = uv_mmtimer_mmap,
  46. .unlocked_ioctl = uv_mmtimer_ioctl,
  47. .llseek = noop_llseek,
  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. * Starting with HUB rev 2.0, the UV RTC register is
  84. * replicated across all cachelines of it's own page.
  85. * This allows faster simultaneous reads from a given socket.
  86. *
  87. * The offset returned is in 64 bit units.
  88. */
  89. if (uv_get_min_hub_revision_id() == 1)
  90. ret = 0;
  91. else
  92. ret = ((uv_blade_processor_id() * L1_CACHE_BYTES) %
  93. PAGE_SIZE) / 8;
  94. break;
  95. case MMTIMER_GETRES: /* resolution of the clock in 10^-15 s */
  96. if (copy_to_user((unsigned long __user *)arg,
  97. &uv_mmtimer_femtoperiod, sizeof(unsigned long)))
  98. ret = -EFAULT;
  99. break;
  100. case MMTIMER_GETFREQ: /* frequency in Hz */
  101. if (copy_to_user((unsigned long __user *)arg,
  102. &sn_rtc_cycles_per_second,
  103. sizeof(unsigned long)))
  104. ret = -EFAULT;
  105. break;
  106. case MMTIMER_GETBITS: /* number of bits in the clock */
  107. ret = hweight64(UVH_RTC_REAL_TIME_CLOCK_MASK);
  108. break;
  109. case MMTIMER_MMAPAVAIL:
  110. ret = 1;
  111. break;
  112. case MMTIMER_GETCOUNTER:
  113. if (copy_to_user((unsigned long __user *)arg,
  114. (unsigned long *)uv_local_mmr_address(UVH_RTC),
  115. sizeof(unsigned long)))
  116. ret = -EFAULT;
  117. break;
  118. default:
  119. ret = -ENOTTY;
  120. break;
  121. }
  122. return ret;
  123. }
  124. /**
  125. * uv_mmtimer_mmap - maps the clock's registers into userspace
  126. * @file: file structure for the device
  127. * @vma: VMA to map the registers into
  128. *
  129. * Calls remap_pfn_range() to map the clock's registers into
  130. * the calling process' address space.
  131. */
  132. static int uv_mmtimer_mmap(struct file *file, struct vm_area_struct *vma)
  133. {
  134. unsigned long uv_mmtimer_addr;
  135. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  136. return -EINVAL;
  137. if (vma->vm_flags & VM_WRITE)
  138. return -EPERM;
  139. if (PAGE_SIZE > (1 << 16))
  140. return -ENOSYS;
  141. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  142. uv_mmtimer_addr = UV_LOCAL_MMR_BASE | UVH_RTC;
  143. uv_mmtimer_addr &= ~(PAGE_SIZE - 1);
  144. uv_mmtimer_addr &= 0xfffffffffffffffUL;
  145. if (remap_pfn_range(vma, vma->vm_start, uv_mmtimer_addr >> PAGE_SHIFT,
  146. PAGE_SIZE, vma->vm_page_prot)) {
  147. printk(KERN_ERR "remap_pfn_range failed in uv_mmtimer_mmap\n");
  148. return -EAGAIN;
  149. }
  150. return 0;
  151. }
  152. static struct miscdevice uv_mmtimer_miscdev = {
  153. MISC_DYNAMIC_MINOR,
  154. UV_MMTIMER_NAME,
  155. &uv_mmtimer_fops
  156. };
  157. /**
  158. * uv_mmtimer_init - device initialization routine
  159. *
  160. * Does initial setup for the uv_mmtimer device.
  161. */
  162. static int __init uv_mmtimer_init(void)
  163. {
  164. if (!is_uv_system()) {
  165. printk(KERN_ERR "%s: Hardware unsupported\n", UV_MMTIMER_NAME);
  166. return -1;
  167. }
  168. /*
  169. * Sanity check the cycles/sec variable
  170. */
  171. if (sn_rtc_cycles_per_second < 100000) {
  172. printk(KERN_ERR "%s: unable to determine clock frequency\n",
  173. UV_MMTIMER_NAME);
  174. return -1;
  175. }
  176. uv_mmtimer_femtoperiod = ((unsigned long)1E15 +
  177. sn_rtc_cycles_per_second / 2) /
  178. sn_rtc_cycles_per_second;
  179. if (misc_register(&uv_mmtimer_miscdev)) {
  180. printk(KERN_ERR "%s: failed to register device\n",
  181. UV_MMTIMER_NAME);
  182. return -1;
  183. }
  184. printk(KERN_INFO "%s: v%s, %ld MHz\n", UV_MMTIMER_DESC,
  185. UV_MMTIMER_VERSION,
  186. sn_rtc_cycles_per_second/(unsigned long)1E6);
  187. return 0;
  188. }
  189. module_init(uv_mmtimer_init);