blob: 3da7b1d8bfd391e57a1038facb48dd69fc7f8b27 [file] [log] [blame]
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -06001/*
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
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>
Dimitri Sivanich1400b3f2009-03-04 16:02:46 -060027#include <asm/apic.h>
28#include <asm/cpu.h>
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -060029
30#define RTC_NAME "sgi_rtc"
31
Coly Lic5428e92009-04-22 23:21:56 +080032static cycle_t uv_read_rtc(struct clocksource *cs);
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -060033static int uv_rtc_next_event(unsigned long, struct clock_event_device *);
34static void uv_rtc_timer_setup(enum clock_event_mode,
35 struct clock_event_device *);
36
37static struct clocksource clocksource_uv = {
38 .name = RTC_NAME,
39 .rating = 400,
40 .read = uv_read_rtc,
41 .mask = (cycle_t)UVH_RTC_REAL_TIME_CLOCK_MASK,
42 .shift = 10,
43 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
44};
45
46static struct clock_event_device clock_event_device_uv = {
47 .name = RTC_NAME,
48 .features = CLOCK_EVT_FEAT_ONESHOT,
49 .shift = 20,
50 .rating = 400,
51 .irq = -1,
52 .set_next_event = uv_rtc_next_event,
53 .set_mode = uv_rtc_timer_setup,
54 .event_handler = NULL,
55};
56
57static DEFINE_PER_CPU(struct clock_event_device, cpu_ced);
58
59/* There is one of these allocated per node */
60struct uv_rtc_timer_head {
61 spinlock_t lock;
62 /* next cpu waiting for timer, local node relative: */
63 int next_cpu;
64 /* number of cpus on this node: */
65 int ncpus;
66 struct {
67 int lcpu; /* systemwide logical cpu number */
68 u64 expires; /* next timer expiration for this cpu */
69 } cpu[1];
70};
71
72/*
73 * Access to uv_rtc_timer_head via blade id.
74 */
75static struct uv_rtc_timer_head **blade_info __read_mostly;
76
77static int uv_rtc_enable;
Dimitri Sivanich8c28de42009-10-14 09:18:48 -050078static int uv_rtc_evt_enable;
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -060079
80/*
81 * Hardware interface routines
82 */
83
84/* Send IPIs to another node */
85static void uv_rtc_send_IPI(int cpu)
86{
87 unsigned long apicid, val;
88 int pnode;
89
Dimitri Sivanich1400b3f2009-03-04 16:02:46 -060090 apicid = cpu_physical_id(cpu);
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -060091 pnode = uv_apicid_to_pnode(apicid);
92 val = (1UL << UVH_IPI_INT_SEND_SHFT) |
93 (apicid << UVH_IPI_INT_APIC_ID_SHFT) |
Dimitri Sivanich4a4de9c2009-10-14 09:22:57 -050094 (X86_PLATFORM_IPI_VECTOR << UVH_IPI_INT_VECTOR_SHFT);
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -060095
96 uv_write_global_mmr64(pnode, UVH_IPI_INT, val);
97}
98
99/* Check for an RTC interrupt pending */
100static int uv_intr_pending(int pnode)
101{
102 return uv_read_global_mmr64(pnode, UVH_EVENT_OCCURRED0) &
103 UVH_EVENT_OCCURRED0_RTC1_MASK;
104}
105
106/* Setup interrupt and return non-zero if early expiration occurred. */
107static int uv_setup_intr(int cpu, u64 expires)
108{
109 u64 val;
110 int pnode = uv_cpu_to_pnode(cpu);
111
112 uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG,
113 UVH_RTC1_INT_CONFIG_M_MASK);
114 uv_write_global_mmr64(pnode, UVH_INT_CMPB, -1L);
115
116 uv_write_global_mmr64(pnode, UVH_EVENT_OCCURRED0_ALIAS,
117 UVH_EVENT_OCCURRED0_RTC1_MASK);
118
Dimitri Sivanich4a4de9c2009-10-14 09:22:57 -0500119 val = (X86_PLATFORM_IPI_VECTOR << UVH_RTC1_INT_CONFIG_VECTOR_SHFT) |
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600120 ((u64)cpu_physical_id(cpu) << UVH_RTC1_INT_CONFIG_APIC_ID_SHFT);
121
122 /* Set configuration */
123 uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG, val);
124 /* Initialize comparator value */
125 uv_write_global_mmr64(pnode, UVH_INT_CMPB, expires);
126
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500127 if (uv_read_rtc(NULL) <= expires)
128 return 0;
129
130 return !uv_intr_pending(pnode);
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600131}
132
133/*
134 * Per-cpu timer tracking routines
135 */
136
137static __init void uv_rtc_deallocate_timers(void)
138{
139 int bid;
140
141 for_each_possible_blade(bid) {
142 kfree(blade_info[bid]);
143 }
144 kfree(blade_info);
145}
146
147/* Allocate per-node list of cpu timer expiration times. */
148static __init int uv_rtc_allocate_timers(void)
149{
150 int cpu;
151
152 blade_info = kmalloc(uv_possible_blades * sizeof(void *), GFP_KERNEL);
153 if (!blade_info)
154 return -ENOMEM;
155 memset(blade_info, 0, uv_possible_blades * sizeof(void *));
156
157 for_each_present_cpu(cpu) {
158 int nid = cpu_to_node(cpu);
159 int bid = uv_cpu_to_blade_id(cpu);
160 int bcpu = uv_cpu_hub_info(cpu)->blade_processor_id;
161 struct uv_rtc_timer_head *head = blade_info[bid];
162
163 if (!head) {
164 head = kmalloc_node(sizeof(struct uv_rtc_timer_head) +
165 (uv_blade_nr_possible_cpus(bid) *
166 2 * sizeof(u64)),
167 GFP_KERNEL, nid);
168 if (!head) {
169 uv_rtc_deallocate_timers();
170 return -ENOMEM;
171 }
172 spin_lock_init(&head->lock);
173 head->ncpus = uv_blade_nr_possible_cpus(bid);
174 head->next_cpu = -1;
175 blade_info[bid] = head;
176 }
177
178 head->cpu[bcpu].lcpu = cpu;
179 head->cpu[bcpu].expires = ULLONG_MAX;
180 }
181
182 return 0;
183}
184
185/* Find and set the next expiring timer. */
186static void uv_rtc_find_next_timer(struct uv_rtc_timer_head *head, int pnode)
187{
188 u64 lowest = ULLONG_MAX;
189 int c, bcpu = -1;
190
191 head->next_cpu = -1;
192 for (c = 0; c < head->ncpus; c++) {
193 u64 exp = head->cpu[c].expires;
194 if (exp < lowest) {
195 bcpu = c;
196 lowest = exp;
197 }
198 }
199 if (bcpu >= 0) {
200 head->next_cpu = bcpu;
201 c = head->cpu[bcpu].lcpu;
202 if (uv_setup_intr(c, lowest))
203 /* If we didn't set it up in time, trigger */
204 uv_rtc_send_IPI(c);
205 } else {
206 uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG,
207 UVH_RTC1_INT_CONFIG_M_MASK);
208 }
209}
210
211/*
212 * Set expiration time for current cpu.
213 *
214 * Returns 1 if we missed the expiration time.
215 */
216static int uv_rtc_set_timer(int cpu, u64 expires)
217{
218 int pnode = uv_cpu_to_pnode(cpu);
219 int bid = uv_cpu_to_blade_id(cpu);
220 struct uv_rtc_timer_head *head = blade_info[bid];
221 int bcpu = uv_cpu_hub_info(cpu)->blade_processor_id;
222 u64 *t = &head->cpu[bcpu].expires;
223 unsigned long flags;
224 int next_cpu;
225
226 spin_lock_irqsave(&head->lock, flags);
227
228 next_cpu = head->next_cpu;
229 *t = expires;
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500230
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600231 /* Will this one be next to go off? */
232 if (next_cpu < 0 || bcpu == next_cpu ||
233 expires < head->cpu[next_cpu].expires) {
234 head->next_cpu = bcpu;
235 if (uv_setup_intr(cpu, expires)) {
236 *t = ULLONG_MAX;
237 uv_rtc_find_next_timer(head, pnode);
238 spin_unlock_irqrestore(&head->lock, flags);
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500239 return -ETIME;
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600240 }
241 }
242
243 spin_unlock_irqrestore(&head->lock, flags);
244 return 0;
245}
246
247/*
248 * Unset expiration time for current cpu.
249 *
250 * Returns 1 if this timer was pending.
251 */
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500252static int uv_rtc_unset_timer(int cpu, int force)
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600253{
254 int pnode = uv_cpu_to_pnode(cpu);
255 int bid = uv_cpu_to_blade_id(cpu);
256 struct uv_rtc_timer_head *head = blade_info[bid];
257 int bcpu = uv_cpu_hub_info(cpu)->blade_processor_id;
258 u64 *t = &head->cpu[bcpu].expires;
259 unsigned long flags;
260 int rc = 0;
261
262 spin_lock_irqsave(&head->lock, flags);
263
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500264 if ((head->next_cpu == bcpu && uv_read_rtc(NULL) >= *t) || force)
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600265 rc = 1;
266
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500267 if (rc) {
268 *t = ULLONG_MAX;
269 /* Was the hardware setup for this timer? */
270 if (head->next_cpu == bcpu)
271 uv_rtc_find_next_timer(head, pnode);
272 }
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600273
274 spin_unlock_irqrestore(&head->lock, flags);
275
276 return rc;
277}
278
279
280/*
281 * Kernel interface routines.
282 */
283
284/*
285 * Read the RTC.
286 */
Coly Lic5428e92009-04-22 23:21:56 +0800287static cycle_t uv_read_rtc(struct clocksource *cs)
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600288{
289 return (cycle_t)uv_read_local_mmr(UVH_RTC);
290}
291
292/*
293 * Program the next event, relative to now
294 */
295static int uv_rtc_next_event(unsigned long delta,
296 struct clock_event_device *ced)
297{
298 int ced_cpu = cpumask_first(ced->cpumask);
299
Coly Lic5428e92009-04-22 23:21:56 +0800300 return uv_rtc_set_timer(ced_cpu, delta + uv_read_rtc(NULL));
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600301}
302
303/*
304 * Setup the RTC timer in oneshot mode
305 */
306static void uv_rtc_timer_setup(enum clock_event_mode mode,
307 struct clock_event_device *evt)
308{
309 int ced_cpu = cpumask_first(evt->cpumask);
310
311 switch (mode) {
312 case CLOCK_EVT_MODE_PERIODIC:
313 case CLOCK_EVT_MODE_ONESHOT:
314 case CLOCK_EVT_MODE_RESUME:
315 /* Nothing to do here yet */
316 break;
317 case CLOCK_EVT_MODE_UNUSED:
318 case CLOCK_EVT_MODE_SHUTDOWN:
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500319 uv_rtc_unset_timer(ced_cpu, 1);
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600320 break;
321 }
322}
323
324static void uv_rtc_interrupt(void)
325{
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600326 int cpu = smp_processor_id();
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500327 struct clock_event_device *ced = &per_cpu(cpu_ced, cpu);
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600328
329 if (!ced || !ced->event_handler)
330 return;
331
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500332 if (uv_rtc_unset_timer(cpu, 0) != 1)
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600333 return;
334
335 ced->event_handler(ced);
336}
337
338static int __init uv_enable_rtc(char *str)
339{
340 uv_rtc_enable = 1;
341
342 return 1;
343}
344__setup("uvrtc", uv_enable_rtc);
345
Dimitri Sivanich8c28de42009-10-14 09:18:48 -0500346static int __init uv_enable_evt_rtc(char *str)
347{
348 uv_rtc_evt_enable = 1;
349
350 return 1;
351}
352__setup("uvrtcevt", uv_enable_evt_rtc);
353
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600354static __init void uv_rtc_register_clockevents(struct work_struct *dummy)
355{
356 struct clock_event_device *ced = &__get_cpu_var(cpu_ced);
357
358 *ced = clock_event_device_uv;
359 ced->cpumask = cpumask_of(smp_processor_id());
360 clockevents_register_device(ced);
361}
362
363static __init int uv_rtc_setup_clock(void)
364{
365 int rc;
366
Dimitri Sivanich4a4de9c2009-10-14 09:22:57 -0500367 if (!uv_rtc_enable || !is_uv_system() || x86_platform_ipi_callback)
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600368 return -ENODEV;
369
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600370 clocksource_uv.mult = clocksource_hz2mult(sn_rtc_cycles_per_second,
371 clocksource_uv.shift);
372
373 rc = clocksource_register(&clocksource_uv);
Dimitri Sivanich8c28de42009-10-14 09:18:48 -0500374 if (rc)
375 printk(KERN_INFO "UV RTC clocksource failed rc %d\n", rc);
376 else
377 printk(KERN_INFO "UV RTC clocksource registered freq %lu MHz\n",
378 sn_rtc_cycles_per_second/(unsigned long)1E6);
379
380 if (rc || !uv_rtc_evt_enable)
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600381 return rc;
Dimitri Sivanich8c28de42009-10-14 09:18:48 -0500382
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600383 /* Setup and register clockevents */
384 rc = uv_rtc_allocate_timers();
Dimitri Sivanichd5991ff2009-10-14 09:21:03 -0500385 if (rc)
386 goto error;
387
Dimitri Sivanich4a4de9c2009-10-14 09:22:57 -0500388 x86_platform_ipi_callback = uv_rtc_interrupt;
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600389
390 clock_event_device_uv.mult = div_sc(sn_rtc_cycles_per_second,
391 NSEC_PER_SEC, clock_event_device_uv.shift);
392
393 clock_event_device_uv.min_delta_ns = NSEC_PER_SEC /
394 sn_rtc_cycles_per_second;
395
396 clock_event_device_uv.max_delta_ns = clocksource_uv.mask *
397 (NSEC_PER_SEC / sn_rtc_cycles_per_second);
398
399 rc = schedule_on_each_cpu(uv_rtc_register_clockevents);
400 if (rc) {
Dimitri Sivanich4a4de9c2009-10-14 09:22:57 -0500401 x86_platform_ipi_callback = NULL;
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600402 uv_rtc_deallocate_timers();
Dimitri Sivanichd5991ff2009-10-14 09:21:03 -0500403 goto error;
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600404 }
405
Dimitri Sivanichd5991ff2009-10-14 09:21:03 -0500406 printk(KERN_INFO "UV RTC clockevents registered\n");
407
408 return 0;
409
410error:
411 clocksource_unregister(&clocksource_uv);
412 printk(KERN_INFO "UV RTC clockevents failed rc %d\n", rc);
413
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600414 return rc;
415}
416arch_initcall(uv_rtc_setup_clock);