uv_time.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * SGI RTC clock/timer routines.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Copyright (c) 2009 Silicon Graphics, Inc. All Rights Reserved.
  19. * Copyright (c) Dimitri Sivanich
  20. */
  21. #include <linux/clockchips.h>
  22. #include <asm/uv/uv_mmrs.h>
  23. #include <asm/uv/uv_hub.h>
  24. #include <asm/uv/bios.h>
  25. #include <asm/uv/uv.h>
  26. #define RTC_NAME "sgi_rtc"
  27. static cycle_t uv_read_rtc(void);
  28. static int uv_rtc_next_event(unsigned long, struct clock_event_device *);
  29. static void uv_rtc_timer_setup(enum clock_event_mode,
  30. struct clock_event_device *);
  31. static struct clocksource clocksource_uv = {
  32. .name = RTC_NAME,
  33. .rating = 400,
  34. .read = uv_read_rtc,
  35. .mask = (cycle_t)UVH_RTC_REAL_TIME_CLOCK_MASK,
  36. .shift = 10,
  37. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  38. };
  39. static struct clock_event_device clock_event_device_uv = {
  40. .name = RTC_NAME,
  41. .features = CLOCK_EVT_FEAT_ONESHOT,
  42. .shift = 20,
  43. .rating = 400,
  44. .irq = -1,
  45. .set_next_event = uv_rtc_next_event,
  46. .set_mode = uv_rtc_timer_setup,
  47. .event_handler = NULL,
  48. };
  49. static DEFINE_PER_CPU(struct clock_event_device, cpu_ced);
  50. /* There is one of these allocated per node */
  51. struct uv_rtc_timer_head {
  52. spinlock_t lock;
  53. /* next cpu waiting for timer, local node relative: */
  54. int next_cpu;
  55. /* number of cpus on this node: */
  56. int ncpus;
  57. struct {
  58. int lcpu; /* systemwide logical cpu number */
  59. u64 expires; /* next timer expiration for this cpu */
  60. } cpu[1];
  61. };
  62. /*
  63. * Access to uv_rtc_timer_head via blade id.
  64. */
  65. static struct uv_rtc_timer_head **blade_info __read_mostly;
  66. static int uv_rtc_enable;
  67. /*
  68. * Hardware interface routines
  69. */
  70. /* Send IPIs to another node */
  71. static void uv_rtc_send_IPI(int cpu)
  72. {
  73. unsigned long apicid, val;
  74. int pnode;
  75. apicid = per_cpu(x86_cpu_to_apicid, cpu);
  76. pnode = uv_apicid_to_pnode(apicid);
  77. val = (1UL << UVH_IPI_INT_SEND_SHFT) |
  78. (apicid << UVH_IPI_INT_APIC_ID_SHFT) |
  79. (GENERIC_INTERRUPT_VECTOR << UVH_IPI_INT_VECTOR_SHFT);
  80. uv_write_global_mmr64(pnode, UVH_IPI_INT, val);
  81. }
  82. /* Check for an RTC interrupt pending */
  83. static int uv_intr_pending(int pnode)
  84. {
  85. return uv_read_global_mmr64(pnode, UVH_EVENT_OCCURRED0) &
  86. UVH_EVENT_OCCURRED0_RTC1_MASK;
  87. }
  88. /* Setup interrupt and return non-zero if early expiration occurred. */
  89. static int uv_setup_intr(int cpu, u64 expires)
  90. {
  91. u64 val;
  92. int pnode = uv_cpu_to_pnode(cpu);
  93. uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG,
  94. UVH_RTC1_INT_CONFIG_M_MASK);
  95. uv_write_global_mmr64(pnode, UVH_INT_CMPB, -1L);
  96. uv_write_global_mmr64(pnode, UVH_EVENT_OCCURRED0_ALIAS,
  97. UVH_EVENT_OCCURRED0_RTC1_MASK);
  98. val = (GENERIC_INTERRUPT_VECTOR << UVH_RTC1_INT_CONFIG_VECTOR_SHFT) |
  99. ((u64)cpu_physical_id(cpu) << UVH_RTC1_INT_CONFIG_APIC_ID_SHFT);
  100. /* Set configuration */
  101. uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG, val);
  102. /* Initialize comparator value */
  103. uv_write_global_mmr64(pnode, UVH_INT_CMPB, expires);
  104. return (expires < uv_read_rtc() && !uv_intr_pending(pnode));
  105. }
  106. /*
  107. * Per-cpu timer tracking routines
  108. */
  109. static __init void uv_rtc_deallocate_timers(void)
  110. {
  111. int bid;
  112. for_each_possible_blade(bid) {
  113. kfree(blade_info[bid]);
  114. }
  115. kfree(blade_info);
  116. }
  117. /* Allocate per-node list of cpu timer expiration times. */
  118. static __init int uv_rtc_allocate_timers(void)
  119. {
  120. int cpu;
  121. blade_info = kmalloc(uv_possible_blades * sizeof(void *), GFP_KERNEL);
  122. if (!blade_info)
  123. return -ENOMEM;
  124. memset(blade_info, 0, uv_possible_blades * sizeof(void *));
  125. for_each_present_cpu(cpu) {
  126. int nid = cpu_to_node(cpu);
  127. int bid = uv_cpu_to_blade_id(cpu);
  128. int bcpu = uv_cpu_hub_info(cpu)->blade_processor_id;
  129. struct uv_rtc_timer_head *head = blade_info[bid];
  130. if (!head) {
  131. head = kmalloc_node(sizeof(struct uv_rtc_timer_head) +
  132. (uv_blade_nr_possible_cpus(bid) *
  133. 2 * sizeof(u64)),
  134. GFP_KERNEL, nid);
  135. if (!head) {
  136. uv_rtc_deallocate_timers();
  137. return -ENOMEM;
  138. }
  139. spin_lock_init(&head->lock);
  140. head->ncpus = uv_blade_nr_possible_cpus(bid);
  141. head->next_cpu = -1;
  142. blade_info[bid] = head;
  143. }
  144. head->cpu[bcpu].lcpu = cpu;
  145. head->cpu[bcpu].expires = ULLONG_MAX;
  146. }
  147. return 0;
  148. }
  149. /* Find and set the next expiring timer. */
  150. static void uv_rtc_find_next_timer(struct uv_rtc_timer_head *head, int pnode)
  151. {
  152. u64 lowest = ULLONG_MAX;
  153. int c, bcpu = -1;
  154. head->next_cpu = -1;
  155. for (c = 0; c < head->ncpus; c++) {
  156. u64 exp = head->cpu[c].expires;
  157. if (exp < lowest) {
  158. bcpu = c;
  159. lowest = exp;
  160. }
  161. }
  162. if (bcpu >= 0) {
  163. head->next_cpu = bcpu;
  164. c = head->cpu[bcpu].lcpu;
  165. if (uv_setup_intr(c, lowest))
  166. /* If we didn't set it up in time, trigger */
  167. uv_rtc_send_IPI(c);
  168. } else {
  169. uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG,
  170. UVH_RTC1_INT_CONFIG_M_MASK);
  171. }
  172. }
  173. /*
  174. * Set expiration time for current cpu.
  175. *
  176. * Returns 1 if we missed the expiration time.
  177. */
  178. static int uv_rtc_set_timer(int cpu, u64 expires)
  179. {
  180. int pnode = uv_cpu_to_pnode(cpu);
  181. int bid = uv_cpu_to_blade_id(cpu);
  182. struct uv_rtc_timer_head *head = blade_info[bid];
  183. int bcpu = uv_cpu_hub_info(cpu)->blade_processor_id;
  184. u64 *t = &head->cpu[bcpu].expires;
  185. unsigned long flags;
  186. int next_cpu;
  187. spin_lock_irqsave(&head->lock, flags);
  188. next_cpu = head->next_cpu;
  189. *t = expires;
  190. /* Will this one be next to go off? */
  191. if (next_cpu < 0 || bcpu == next_cpu ||
  192. expires < head->cpu[next_cpu].expires) {
  193. head->next_cpu = bcpu;
  194. if (uv_setup_intr(cpu, expires)) {
  195. *t = ULLONG_MAX;
  196. uv_rtc_find_next_timer(head, pnode);
  197. spin_unlock_irqrestore(&head->lock, flags);
  198. return 1;
  199. }
  200. }
  201. spin_unlock_irqrestore(&head->lock, flags);
  202. return 0;
  203. }
  204. /*
  205. * Unset expiration time for current cpu.
  206. *
  207. * Returns 1 if this timer was pending.
  208. */
  209. static int uv_rtc_unset_timer(int cpu)
  210. {
  211. int pnode = uv_cpu_to_pnode(cpu);
  212. int bid = uv_cpu_to_blade_id(cpu);
  213. struct uv_rtc_timer_head *head = blade_info[bid];
  214. int bcpu = uv_cpu_hub_info(cpu)->blade_processor_id;
  215. u64 *t = &head->cpu[bcpu].expires;
  216. unsigned long flags;
  217. int rc = 0;
  218. spin_lock_irqsave(&head->lock, flags);
  219. if (head->next_cpu == bcpu && uv_read_rtc() >= *t)
  220. rc = 1;
  221. *t = ULLONG_MAX;
  222. /* Was the hardware setup for this timer? */
  223. if (head->next_cpu == bcpu)
  224. uv_rtc_find_next_timer(head, pnode);
  225. spin_unlock_irqrestore(&head->lock, flags);
  226. return rc;
  227. }
  228. /*
  229. * Kernel interface routines.
  230. */
  231. /*
  232. * Read the RTC.
  233. */
  234. static cycle_t uv_read_rtc(void)
  235. {
  236. return (cycle_t)uv_read_local_mmr(UVH_RTC);
  237. }
  238. /*
  239. * Program the next event, relative to now
  240. */
  241. static int uv_rtc_next_event(unsigned long delta,
  242. struct clock_event_device *ced)
  243. {
  244. int ced_cpu = cpumask_first(ced->cpumask);
  245. return uv_rtc_set_timer(ced_cpu, delta + uv_read_rtc());
  246. }
  247. /*
  248. * Setup the RTC timer in oneshot mode
  249. */
  250. static void uv_rtc_timer_setup(enum clock_event_mode mode,
  251. struct clock_event_device *evt)
  252. {
  253. int ced_cpu = cpumask_first(evt->cpumask);
  254. switch (mode) {
  255. case CLOCK_EVT_MODE_PERIODIC:
  256. case CLOCK_EVT_MODE_ONESHOT:
  257. case CLOCK_EVT_MODE_RESUME:
  258. /* Nothing to do here yet */
  259. break;
  260. case CLOCK_EVT_MODE_UNUSED:
  261. case CLOCK_EVT_MODE_SHUTDOWN:
  262. uv_rtc_unset_timer(ced_cpu);
  263. break;
  264. }
  265. }
  266. static void uv_rtc_interrupt(void)
  267. {
  268. struct clock_event_device *ced = &__get_cpu_var(cpu_ced);
  269. int cpu = smp_processor_id();
  270. if (!ced || !ced->event_handler)
  271. return;
  272. if (uv_rtc_unset_timer(cpu) != 1)
  273. return;
  274. ced->event_handler(ced);
  275. }
  276. static int __init uv_enable_rtc(char *str)
  277. {
  278. uv_rtc_enable = 1;
  279. return 1;
  280. }
  281. __setup("uvrtc", uv_enable_rtc);
  282. static __init void uv_rtc_register_clockevents(struct work_struct *dummy)
  283. {
  284. struct clock_event_device *ced = &__get_cpu_var(cpu_ced);
  285. *ced = clock_event_device_uv;
  286. ced->cpumask = cpumask_of(smp_processor_id());
  287. clockevents_register_device(ced);
  288. }
  289. static __init int uv_rtc_setup_clock(void)
  290. {
  291. int rc;
  292. if (!uv_rtc_enable || !is_uv_system() || generic_interrupt_extension)
  293. return -ENODEV;
  294. generic_interrupt_extension = uv_rtc_interrupt;
  295. clocksource_uv.mult = clocksource_hz2mult(sn_rtc_cycles_per_second,
  296. clocksource_uv.shift);
  297. rc = clocksource_register(&clocksource_uv);
  298. if (rc) {
  299. generic_interrupt_extension = NULL;
  300. return rc;
  301. }
  302. /* Setup and register clockevents */
  303. rc = uv_rtc_allocate_timers();
  304. if (rc) {
  305. clocksource_unregister(&clocksource_uv);
  306. generic_interrupt_extension = NULL;
  307. return rc;
  308. }
  309. clock_event_device_uv.mult = div_sc(sn_rtc_cycles_per_second,
  310. NSEC_PER_SEC, clock_event_device_uv.shift);
  311. clock_event_device_uv.min_delta_ns = NSEC_PER_SEC /
  312. sn_rtc_cycles_per_second;
  313. clock_event_device_uv.max_delta_ns = clocksource_uv.mask *
  314. (NSEC_PER_SEC / sn_rtc_cycles_per_second);
  315. rc = schedule_on_each_cpu(uv_rtc_register_clockevents);
  316. if (rc) {
  317. clocksource_unregister(&clocksource_uv);
  318. generic_interrupt_extension = NULL;
  319. uv_rtc_deallocate_timers();
  320. }
  321. return rc;
  322. }
  323. arch_initcall(uv_rtc_setup_clock);