blob: 9daf5d1af9f190015b8b9be646aac14cbe6c2c44 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -060023
24#include <asm/uv/uv_mmrs.h>
25#include <asm/uv/uv_hub.h>
26#include <asm/uv/bios.h>
27#include <asm/uv/uv.h>
Dimitri Sivanich1400b3f2009-03-04 16:02:46 -060028#include <asm/apic.h>
29#include <asm/cpu.h>
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -060030
31#define RTC_NAME "sgi_rtc"
32
Coly Lic5428e92009-04-22 23:21:56 +080033static cycle_t uv_read_rtc(struct clocksource *cs);
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -060034static int uv_rtc_next_event(unsigned long, struct clock_event_device *);
35static void uv_rtc_timer_setup(enum clock_event_mode,
36 struct clock_event_device *);
37
38static struct clocksource clocksource_uv = {
39 .name = RTC_NAME,
40 .rating = 400,
41 .read = uv_read_rtc,
42 .mask = (cycle_t)UVH_RTC_REAL_TIME_CLOCK_MASK,
43 .shift = 10,
44 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
45};
46
47static struct clock_event_device clock_event_device_uv = {
48 .name = RTC_NAME,
49 .features = CLOCK_EVT_FEAT_ONESHOT,
50 .shift = 20,
51 .rating = 400,
52 .irq = -1,
53 .set_next_event = uv_rtc_next_event,
54 .set_mode = uv_rtc_timer_setup,
55 .event_handler = NULL,
56};
57
58static DEFINE_PER_CPU(struct clock_event_device, cpu_ced);
59
60/* There is one of these allocated per node */
61struct uv_rtc_timer_head {
62 spinlock_t lock;
63 /* next cpu waiting for timer, local node relative: */
64 int next_cpu;
65 /* number of cpus on this node: */
66 int ncpus;
67 struct {
68 int lcpu; /* systemwide logical cpu number */
69 u64 expires; /* next timer expiration for this cpu */
70 } cpu[1];
71};
72
73/*
74 * Access to uv_rtc_timer_head via blade id.
75 */
76static struct uv_rtc_timer_head **blade_info __read_mostly;
77
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);
Dimitri Sivanich8191c9f2010-11-16 16:23:52 -060092 apicid |= uv_apicid_hibits;
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -060093 val = (1UL << UVH_IPI_INT_SEND_SHFT) |
94 (apicid << UVH_IPI_INT_APIC_ID_SHFT) |
Dimitri Sivanich4a4de9c2009-10-14 09:22:57 -050095 (X86_PLATFORM_IPI_VECTOR << UVH_IPI_INT_VECTOR_SHFT);
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -060096
97 uv_write_global_mmr64(pnode, UVH_IPI_INT, val);
98}
99
100/* Check for an RTC interrupt pending */
101static int uv_intr_pending(int pnode)
102{
103 return uv_read_global_mmr64(pnode, UVH_EVENT_OCCURRED0) &
104 UVH_EVENT_OCCURRED0_RTC1_MASK;
105}
106
107/* Setup interrupt and return non-zero if early expiration occurred. */
108static int uv_setup_intr(int cpu, u64 expires)
109{
110 u64 val;
Dimitri Sivanich8191c9f2010-11-16 16:23:52 -0600111 unsigned long apicid = cpu_physical_id(cpu) | uv_apicid_hibits;
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600112 int pnode = uv_cpu_to_pnode(cpu);
113
114 uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG,
115 UVH_RTC1_INT_CONFIG_M_MASK);
116 uv_write_global_mmr64(pnode, UVH_INT_CMPB, -1L);
117
118 uv_write_global_mmr64(pnode, UVH_EVENT_OCCURRED0_ALIAS,
119 UVH_EVENT_OCCURRED0_RTC1_MASK);
120
Dimitri Sivanich4a4de9c2009-10-14 09:22:57 -0500121 val = (X86_PLATFORM_IPI_VECTOR << UVH_RTC1_INT_CONFIG_VECTOR_SHFT) |
Dimitri Sivanich8191c9f2010-11-16 16:23:52 -0600122 ((u64)apicid << UVH_RTC1_INT_CONFIG_APIC_ID_SHFT);
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600123
124 /* Set configuration */
125 uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG, val);
126 /* Initialize comparator value */
127 uv_write_global_mmr64(pnode, UVH_INT_CMPB, expires);
128
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500129 if (uv_read_rtc(NULL) <= expires)
130 return 0;
131
132 return !uv_intr_pending(pnode);
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600133}
134
135/*
136 * Per-cpu timer tracking routines
137 */
138
139static __init void uv_rtc_deallocate_timers(void)
140{
141 int bid;
142
143 for_each_possible_blade(bid) {
144 kfree(blade_info[bid]);
145 }
146 kfree(blade_info);
147}
148
149/* Allocate per-node list of cpu timer expiration times. */
150static __init int uv_rtc_allocate_timers(void)
151{
152 int cpu;
153
154 blade_info = kmalloc(uv_possible_blades * sizeof(void *), GFP_KERNEL);
155 if (!blade_info)
156 return -ENOMEM;
157 memset(blade_info, 0, uv_possible_blades * sizeof(void *));
158
159 for_each_present_cpu(cpu) {
160 int nid = cpu_to_node(cpu);
161 int bid = uv_cpu_to_blade_id(cpu);
162 int bcpu = uv_cpu_hub_info(cpu)->blade_processor_id;
163 struct uv_rtc_timer_head *head = blade_info[bid];
164
165 if (!head) {
166 head = kmalloc_node(sizeof(struct uv_rtc_timer_head) +
167 (uv_blade_nr_possible_cpus(bid) *
168 2 * sizeof(u64)),
169 GFP_KERNEL, nid);
170 if (!head) {
171 uv_rtc_deallocate_timers();
172 return -ENOMEM;
173 }
174 spin_lock_init(&head->lock);
175 head->ncpus = uv_blade_nr_possible_cpus(bid);
176 head->next_cpu = -1;
177 blade_info[bid] = head;
178 }
179
180 head->cpu[bcpu].lcpu = cpu;
181 head->cpu[bcpu].expires = ULLONG_MAX;
182 }
183
184 return 0;
185}
186
187/* Find and set the next expiring timer. */
188static void uv_rtc_find_next_timer(struct uv_rtc_timer_head *head, int pnode)
189{
190 u64 lowest = ULLONG_MAX;
191 int c, bcpu = -1;
192
193 head->next_cpu = -1;
194 for (c = 0; c < head->ncpus; c++) {
195 u64 exp = head->cpu[c].expires;
196 if (exp < lowest) {
197 bcpu = c;
198 lowest = exp;
199 }
200 }
201 if (bcpu >= 0) {
202 head->next_cpu = bcpu;
203 c = head->cpu[bcpu].lcpu;
204 if (uv_setup_intr(c, lowest))
205 /* If we didn't set it up in time, trigger */
206 uv_rtc_send_IPI(c);
207 } else {
208 uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG,
209 UVH_RTC1_INT_CONFIG_M_MASK);
210 }
211}
212
213/*
214 * Set expiration time for current cpu.
215 *
216 * Returns 1 if we missed the expiration time.
217 */
218static int uv_rtc_set_timer(int cpu, u64 expires)
219{
220 int pnode = uv_cpu_to_pnode(cpu);
221 int bid = uv_cpu_to_blade_id(cpu);
222 struct uv_rtc_timer_head *head = blade_info[bid];
223 int bcpu = uv_cpu_hub_info(cpu)->blade_processor_id;
224 u64 *t = &head->cpu[bcpu].expires;
225 unsigned long flags;
226 int next_cpu;
227
228 spin_lock_irqsave(&head->lock, flags);
229
230 next_cpu = head->next_cpu;
231 *t = expires;
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500232
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600233 /* Will this one be next to go off? */
234 if (next_cpu < 0 || bcpu == next_cpu ||
235 expires < head->cpu[next_cpu].expires) {
236 head->next_cpu = bcpu;
237 if (uv_setup_intr(cpu, expires)) {
238 *t = ULLONG_MAX;
239 uv_rtc_find_next_timer(head, pnode);
240 spin_unlock_irqrestore(&head->lock, flags);
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500241 return -ETIME;
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600242 }
243 }
244
245 spin_unlock_irqrestore(&head->lock, flags);
246 return 0;
247}
248
249/*
250 * Unset expiration time for current cpu.
251 *
252 * Returns 1 if this timer was pending.
253 */
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500254static int uv_rtc_unset_timer(int cpu, int force)
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600255{
256 int pnode = uv_cpu_to_pnode(cpu);
257 int bid = uv_cpu_to_blade_id(cpu);
258 struct uv_rtc_timer_head *head = blade_info[bid];
259 int bcpu = uv_cpu_hub_info(cpu)->blade_processor_id;
260 u64 *t = &head->cpu[bcpu].expires;
261 unsigned long flags;
262 int rc = 0;
263
264 spin_lock_irqsave(&head->lock, flags);
265
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500266 if ((head->next_cpu == bcpu && uv_read_rtc(NULL) >= *t) || force)
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600267 rc = 1;
268
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500269 if (rc) {
270 *t = ULLONG_MAX;
271 /* Was the hardware setup for this timer? */
272 if (head->next_cpu == bcpu)
273 uv_rtc_find_next_timer(head, pnode);
274 }
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600275
276 spin_unlock_irqrestore(&head->lock, flags);
277
278 return rc;
279}
280
281
282/*
283 * Kernel interface routines.
284 */
285
286/*
287 * Read the RTC.
Dimitri Sivanichaca3bb52010-01-22 09:41:40 -0600288 *
289 * Starting with HUB rev 2.0, the UV RTC register is replicated across all
290 * cachelines of it's own page. This allows faster simultaneous reads
291 * from a given socket.
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600292 */
Coly Lic5428e92009-04-22 23:21:56 +0800293static cycle_t uv_read_rtc(struct clocksource *cs)
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600294{
Dimitri Sivanichaca3bb52010-01-22 09:41:40 -0600295 unsigned long offset;
296
297 if (uv_get_min_hub_revision_id() == 1)
298 offset = 0;
299 else
300 offset = (uv_blade_processor_id() * L1_CACHE_BYTES) % PAGE_SIZE;
301
302 return (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600303}
304
305/*
306 * Program the next event, relative to now
307 */
308static int uv_rtc_next_event(unsigned long delta,
309 struct clock_event_device *ced)
310{
311 int ced_cpu = cpumask_first(ced->cpumask);
312
Coly Lic5428e92009-04-22 23:21:56 +0800313 return uv_rtc_set_timer(ced_cpu, delta + uv_read_rtc(NULL));
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600314}
315
316/*
317 * Setup the RTC timer in oneshot mode
318 */
319static void uv_rtc_timer_setup(enum clock_event_mode mode,
320 struct clock_event_device *evt)
321{
322 int ced_cpu = cpumask_first(evt->cpumask);
323
324 switch (mode) {
325 case CLOCK_EVT_MODE_PERIODIC:
326 case CLOCK_EVT_MODE_ONESHOT:
327 case CLOCK_EVT_MODE_RESUME:
328 /* Nothing to do here yet */
329 break;
330 case CLOCK_EVT_MODE_UNUSED:
331 case CLOCK_EVT_MODE_SHUTDOWN:
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500332 uv_rtc_unset_timer(ced_cpu, 1);
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600333 break;
334 }
335}
336
337static void uv_rtc_interrupt(void)
338{
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600339 int cpu = smp_processor_id();
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500340 struct clock_event_device *ced = &per_cpu(cpu_ced, cpu);
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600341
342 if (!ced || !ced->event_handler)
343 return;
344
Dimitri Sivaniche47938b2009-10-14 09:16:30 -0500345 if (uv_rtc_unset_timer(cpu, 0) != 1)
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600346 return;
347
348 ced->event_handler(ced);
349}
350
Dimitri Sivanich8c28de42009-10-14 09:18:48 -0500351static int __init uv_enable_evt_rtc(char *str)
352{
353 uv_rtc_evt_enable = 1;
354
355 return 1;
356}
357__setup("uvrtcevt", uv_enable_evt_rtc);
358
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600359static __init void uv_rtc_register_clockevents(struct work_struct *dummy)
360{
361 struct clock_event_device *ced = &__get_cpu_var(cpu_ced);
362
363 *ced = clock_event_device_uv;
364 ced->cpumask = cpumask_of(smp_processor_id());
365 clockevents_register_device(ced);
366}
367
368static __init int uv_rtc_setup_clock(void)
369{
370 int rc;
371
Dimitri Sivanich581f2022009-11-20 15:48:26 -0600372 if (!is_uv_system())
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600373 return -ENODEV;
374
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600375 clocksource_uv.mult = clocksource_hz2mult(sn_rtc_cycles_per_second,
376 clocksource_uv.shift);
377
Dimitri Sivanich581f2022009-11-20 15:48:26 -0600378 /* If single blade, prefer tsc */
379 if (uv_num_possible_blades() == 1)
380 clocksource_uv.rating = 250;
381
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600382 rc = clocksource_register(&clocksource_uv);
Dimitri Sivanich8c28de42009-10-14 09:18:48 -0500383 if (rc)
384 printk(KERN_INFO "UV RTC clocksource failed rc %d\n", rc);
385 else
386 printk(KERN_INFO "UV RTC clocksource registered freq %lu MHz\n",
387 sn_rtc_cycles_per_second/(unsigned long)1E6);
388
Dimitri Sivanich581f2022009-11-20 15:48:26 -0600389 if (rc || !uv_rtc_evt_enable || x86_platform_ipi_callback)
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600390 return rc;
Dimitri Sivanich8c28de42009-10-14 09:18:48 -0500391
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600392 /* Setup and register clockevents */
393 rc = uv_rtc_allocate_timers();
Dimitri Sivanichd5991ff2009-10-14 09:21:03 -0500394 if (rc)
395 goto error;
396
Dimitri Sivanich4a4de9c2009-10-14 09:22:57 -0500397 x86_platform_ipi_callback = uv_rtc_interrupt;
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600398
399 clock_event_device_uv.mult = div_sc(sn_rtc_cycles_per_second,
400 NSEC_PER_SEC, clock_event_device_uv.shift);
401
402 clock_event_device_uv.min_delta_ns = NSEC_PER_SEC /
403 sn_rtc_cycles_per_second;
404
405 clock_event_device_uv.max_delta_ns = clocksource_uv.mask *
406 (NSEC_PER_SEC / sn_rtc_cycles_per_second);
407
408 rc = schedule_on_each_cpu(uv_rtc_register_clockevents);
409 if (rc) {
Dimitri Sivanich4a4de9c2009-10-14 09:22:57 -0500410 x86_platform_ipi_callback = NULL;
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600411 uv_rtc_deallocate_timers();
Dimitri Sivanichd5991ff2009-10-14 09:21:03 -0500412 goto error;
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600413 }
414
Dimitri Sivanichd5991ff2009-10-14 09:21:03 -0500415 printk(KERN_INFO "UV RTC clockevents registered\n");
416
417 return 0;
418
419error:
420 clocksource_unregister(&clocksource_uv);
421 printk(KERN_INFO "UV RTC clockevents failed rc %d\n", rc);
422
Dimitri Sivanich5ab5ab32009-03-04 12:59:18 -0600423 return rc;
424}
425arch_initcall(uv_rtc_setup_clock);