uv_time.c 9.3 KB

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