uv_time.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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-2013 Silicon Graphics, Inc. All Rights Reserved.
  19. * Copyright (c) Dimitri Sivanich
  20. */
  21. #include <linux/clockchips.h>
  22. #include <linux/slab.h>
  23. #include <asm/uv/uv_mmrs.h>
  24. #include <asm/uv/uv_hub.h>
  25. #include <asm/uv/bios.h>
  26. #include <asm/uv/uv.h>
  27. #include <asm/apic.h>
  28. #include <asm/cpu.h>
  29. #define RTC_NAME "sgi_rtc"
  30. static cycle_t uv_read_rtc(struct clocksource *cs);
  31. static int uv_rtc_next_event(unsigned long, struct clock_event_device *);
  32. static int uv_rtc_shutdown(struct clock_event_device *evt);
  33. static struct clocksource clocksource_uv = {
  34. .name = RTC_NAME,
  35. .rating = 299,
  36. .read = uv_read_rtc,
  37. .mask = (cycle_t)UVH_RTC_REAL_TIME_CLOCK_MASK,
  38. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  39. };
  40. static struct clock_event_device clock_event_device_uv = {
  41. .name = RTC_NAME,
  42. .features = CLOCK_EVT_FEAT_ONESHOT,
  43. .shift = 20,
  44. .rating = 400,
  45. .irq = -1,
  46. .set_next_event = uv_rtc_next_event,
  47. .set_state_shutdown = uv_rtc_shutdown,
  48. .event_handler = NULL,
  49. };
  50. static DEFINE_PER_CPU(struct clock_event_device, cpu_ced);
  51. /* There is one of these allocated per node */
  52. struct uv_rtc_timer_head {
  53. spinlock_t lock;
  54. /* next cpu waiting for timer, local node relative: */
  55. int next_cpu;
  56. /* number of cpus on this node: */
  57. int ncpus;
  58. struct {
  59. int lcpu; /* systemwide logical cpu number */
  60. u64 expires; /* next timer expiration for this cpu */
  61. } cpu[1];
  62. };
  63. /*
  64. * Access to uv_rtc_timer_head via blade id.
  65. */
  66. static struct uv_rtc_timer_head **blade_info __read_mostly;
  67. static int uv_rtc_evt_enable;
  68. /*
  69. * Hardware interface routines
  70. */
  71. /* Send IPIs to another node */
  72. static void uv_rtc_send_IPI(int cpu)
  73. {
  74. unsigned long apicid, val;
  75. int pnode;
  76. apicid = cpu_physical_id(cpu);
  77. pnode = uv_apicid_to_pnode(apicid);
  78. apicid |= uv_apicid_hibits;
  79. val = (1UL << UVH_IPI_INT_SEND_SHFT) |
  80. (apicid << UVH_IPI_INT_APIC_ID_SHFT) |
  81. (X86_PLATFORM_IPI_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. if (is_uv1_hub())
  88. return uv_read_global_mmr64(pnode, UVH_EVENT_OCCURRED0) &
  89. UV1H_EVENT_OCCURRED0_RTC1_MASK;
  90. else if (is_uvx_hub())
  91. return uv_read_global_mmr64(pnode, UVXH_EVENT_OCCURRED2) &
  92. UVXH_EVENT_OCCURRED2_RTC_1_MASK;
  93. return 0;
  94. }
  95. /* Setup interrupt and return non-zero if early expiration occurred. */
  96. static int uv_setup_intr(int cpu, u64 expires)
  97. {
  98. u64 val;
  99. unsigned long apicid = cpu_physical_id(cpu) | uv_apicid_hibits;
  100. int pnode = uv_cpu_to_pnode(cpu);
  101. uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG,
  102. UVH_RTC1_INT_CONFIG_M_MASK);
  103. uv_write_global_mmr64(pnode, UVH_INT_CMPB, -1L);
  104. if (is_uv1_hub())
  105. uv_write_global_mmr64(pnode, UVH_EVENT_OCCURRED0_ALIAS,
  106. UV1H_EVENT_OCCURRED0_RTC1_MASK);
  107. else
  108. uv_write_global_mmr64(pnode, UVXH_EVENT_OCCURRED2_ALIAS,
  109. UVXH_EVENT_OCCURRED2_RTC_1_MASK);
  110. val = (X86_PLATFORM_IPI_VECTOR << UVH_RTC1_INT_CONFIG_VECTOR_SHFT) |
  111. ((u64)apicid << UVH_RTC1_INT_CONFIG_APIC_ID_SHFT);
  112. /* Set configuration */
  113. uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG, val);
  114. /* Initialize comparator value */
  115. uv_write_global_mmr64(pnode, UVH_INT_CMPB, expires);
  116. if (uv_read_rtc(NULL) <= expires)
  117. return 0;
  118. return !uv_intr_pending(pnode);
  119. }
  120. /*
  121. * Per-cpu timer tracking routines
  122. */
  123. static __init void uv_rtc_deallocate_timers(void)
  124. {
  125. int bid;
  126. for_each_possible_blade(bid) {
  127. kfree(blade_info[bid]);
  128. }
  129. kfree(blade_info);
  130. }
  131. /* Allocate per-node list of cpu timer expiration times. */
  132. static __init int uv_rtc_allocate_timers(void)
  133. {
  134. int cpu;
  135. blade_info = kzalloc(uv_possible_blades * sizeof(void *), GFP_KERNEL);
  136. if (!blade_info)
  137. return -ENOMEM;
  138. for_each_present_cpu(cpu) {
  139. int nid = cpu_to_node(cpu);
  140. int bid = uv_cpu_to_blade_id(cpu);
  141. int bcpu = uv_cpu_blade_processor_id(cpu);
  142. struct uv_rtc_timer_head *head = blade_info[bid];
  143. if (!head) {
  144. head = kmalloc_node(sizeof(struct uv_rtc_timer_head) +
  145. (uv_blade_nr_possible_cpus(bid) *
  146. 2 * sizeof(u64)),
  147. GFP_KERNEL, nid);
  148. if (!head) {
  149. uv_rtc_deallocate_timers();
  150. return -ENOMEM;
  151. }
  152. spin_lock_init(&head->lock);
  153. head->ncpus = uv_blade_nr_possible_cpus(bid);
  154. head->next_cpu = -1;
  155. blade_info[bid] = head;
  156. }
  157. head->cpu[bcpu].lcpu = cpu;
  158. head->cpu[bcpu].expires = ULLONG_MAX;
  159. }
  160. return 0;
  161. }
  162. /* Find and set the next expiring timer. */
  163. static void uv_rtc_find_next_timer(struct uv_rtc_timer_head *head, int pnode)
  164. {
  165. u64 lowest = ULLONG_MAX;
  166. int c, bcpu = -1;
  167. head->next_cpu = -1;
  168. for (c = 0; c < head->ncpus; c++) {
  169. u64 exp = head->cpu[c].expires;
  170. if (exp < lowest) {
  171. bcpu = c;
  172. lowest = exp;
  173. }
  174. }
  175. if (bcpu >= 0) {
  176. head->next_cpu = bcpu;
  177. c = head->cpu[bcpu].lcpu;
  178. if (uv_setup_intr(c, lowest))
  179. /* If we didn't set it up in time, trigger */
  180. uv_rtc_send_IPI(c);
  181. } else {
  182. uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG,
  183. UVH_RTC1_INT_CONFIG_M_MASK);
  184. }
  185. }
  186. /*
  187. * Set expiration time for current cpu.
  188. *
  189. * Returns 1 if we missed the expiration time.
  190. */
  191. static int uv_rtc_set_timer(int cpu, u64 expires)
  192. {
  193. int pnode = uv_cpu_to_pnode(cpu);
  194. int bid = uv_cpu_to_blade_id(cpu);
  195. struct uv_rtc_timer_head *head = blade_info[bid];
  196. int bcpu = uv_cpu_blade_processor_id(cpu);
  197. u64 *t = &head->cpu[bcpu].expires;
  198. unsigned long flags;
  199. int next_cpu;
  200. spin_lock_irqsave(&head->lock, flags);
  201. next_cpu = head->next_cpu;
  202. *t = expires;
  203. /* Will this one be next to go off? */
  204. if (next_cpu < 0 || bcpu == next_cpu ||
  205. expires < head->cpu[next_cpu].expires) {
  206. head->next_cpu = bcpu;
  207. if (uv_setup_intr(cpu, expires)) {
  208. *t = ULLONG_MAX;
  209. uv_rtc_find_next_timer(head, pnode);
  210. spin_unlock_irqrestore(&head->lock, flags);
  211. return -ETIME;
  212. }
  213. }
  214. spin_unlock_irqrestore(&head->lock, flags);
  215. return 0;
  216. }
  217. /*
  218. * Unset expiration time for current cpu.
  219. *
  220. * Returns 1 if this timer was pending.
  221. */
  222. static int uv_rtc_unset_timer(int cpu, int force)
  223. {
  224. int pnode = uv_cpu_to_pnode(cpu);
  225. int bid = uv_cpu_to_blade_id(cpu);
  226. struct uv_rtc_timer_head *head = blade_info[bid];
  227. int bcpu = uv_cpu_blade_processor_id(cpu);
  228. u64 *t = &head->cpu[bcpu].expires;
  229. unsigned long flags;
  230. int rc = 0;
  231. spin_lock_irqsave(&head->lock, flags);
  232. if ((head->next_cpu == bcpu && uv_read_rtc(NULL) >= *t) || force)
  233. rc = 1;
  234. if (rc) {
  235. *t = ULLONG_MAX;
  236. /* Was the hardware setup for this timer? */
  237. if (head->next_cpu == bcpu)
  238. uv_rtc_find_next_timer(head, pnode);
  239. }
  240. spin_unlock_irqrestore(&head->lock, flags);
  241. return rc;
  242. }
  243. /*
  244. * Kernel interface routines.
  245. */
  246. /*
  247. * Read the RTC.
  248. *
  249. * Starting with HUB rev 2.0, the UV RTC register is replicated across all
  250. * cachelines of it's own page. This allows faster simultaneous reads
  251. * from a given socket.
  252. */
  253. static cycle_t uv_read_rtc(struct clocksource *cs)
  254. {
  255. unsigned long offset;
  256. if (uv_get_min_hub_revision_id() == 1)
  257. offset = 0;
  258. else
  259. offset = (uv_blade_processor_id() * L1_CACHE_BYTES) % PAGE_SIZE;
  260. return (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  261. }
  262. /*
  263. * Program the next event, relative to now
  264. */
  265. static int uv_rtc_next_event(unsigned long delta,
  266. struct clock_event_device *ced)
  267. {
  268. int ced_cpu = cpumask_first(ced->cpumask);
  269. return uv_rtc_set_timer(ced_cpu, delta + uv_read_rtc(NULL));
  270. }
  271. /*
  272. * Shutdown the RTC timer
  273. */
  274. static int uv_rtc_shutdown(struct clock_event_device *evt)
  275. {
  276. int ced_cpu = cpumask_first(evt->cpumask);
  277. uv_rtc_unset_timer(ced_cpu, 1);
  278. return 0;
  279. }
  280. static void uv_rtc_interrupt(void)
  281. {
  282. int cpu = smp_processor_id();
  283. struct clock_event_device *ced = &per_cpu(cpu_ced, cpu);
  284. if (!ced || !ced->event_handler)
  285. return;
  286. if (uv_rtc_unset_timer(cpu, 0) != 1)
  287. return;
  288. ced->event_handler(ced);
  289. }
  290. static int __init uv_enable_evt_rtc(char *str)
  291. {
  292. uv_rtc_evt_enable = 1;
  293. return 1;
  294. }
  295. __setup("uvrtcevt", uv_enable_evt_rtc);
  296. static __init void uv_rtc_register_clockevents(struct work_struct *dummy)
  297. {
  298. struct clock_event_device *ced = this_cpu_ptr(&cpu_ced);
  299. *ced = clock_event_device_uv;
  300. ced->cpumask = cpumask_of(smp_processor_id());
  301. clockevents_register_device(ced);
  302. }
  303. static __init int uv_rtc_setup_clock(void)
  304. {
  305. int rc;
  306. if (!is_uv_system())
  307. return -ENODEV;
  308. rc = clocksource_register_hz(&clocksource_uv, sn_rtc_cycles_per_second);
  309. if (rc)
  310. printk(KERN_INFO "UV RTC clocksource failed rc %d\n", rc);
  311. else
  312. printk(KERN_INFO "UV RTC clocksource registered freq %lu MHz\n",
  313. sn_rtc_cycles_per_second/(unsigned long)1E6);
  314. if (rc || !uv_rtc_evt_enable || x86_platform_ipi_callback)
  315. return rc;
  316. /* Setup and register clockevents */
  317. rc = uv_rtc_allocate_timers();
  318. if (rc)
  319. goto error;
  320. x86_platform_ipi_callback = uv_rtc_interrupt;
  321. clock_event_device_uv.mult = div_sc(sn_rtc_cycles_per_second,
  322. NSEC_PER_SEC, clock_event_device_uv.shift);
  323. clock_event_device_uv.min_delta_ns = NSEC_PER_SEC /
  324. sn_rtc_cycles_per_second;
  325. clock_event_device_uv.max_delta_ns = clocksource_uv.mask *
  326. (NSEC_PER_SEC / sn_rtc_cycles_per_second);
  327. rc = schedule_on_each_cpu(uv_rtc_register_clockevents);
  328. if (rc) {
  329. x86_platform_ipi_callback = NULL;
  330. uv_rtc_deallocate_timers();
  331. goto error;
  332. }
  333. printk(KERN_INFO "UV RTC clockevents registered\n");
  334. return 0;
  335. error:
  336. clocksource_unregister(&clocksource_uv);
  337. printk(KERN_INFO "UV RTC clockevents failed rc %d\n", rc);
  338. return rc;
  339. }
  340. arch_initcall(uv_rtc_setup_clock);