Merge remote-tracking branches 'asoc/topic/fsl-easi', 'asoc/topic/fsl-sai', 'asoc...
[linux-2.6-block.git] / arch / tile / kernel / smp.c
CommitLineData
867e359b
CM
1/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 *
14 * TILE SMP support routines.
15 */
16
17#include <linux/smp.h>
fb702b94
CM
18#include <linux/interrupt.h>
19#include <linux/io.h>
867e359b 20#include <linux/irq.h>
fb702b94 21#include <linux/module.h>
867e359b 22#include <asm/cacheflush.h>
3fa17c39 23#include <asm/homecache.h>
867e359b 24
ce61cdc2
CM
25/*
26 * We write to width and height with a single store in head_NN.S,
27 * so make the variable aligned to "long".
28 */
29HV_Topology smp_topology __write_once __aligned(sizeof(long));
fb702b94
CM
30EXPORT_SYMBOL(smp_topology);
31
32#if CHIP_HAS_IPI()
33static unsigned long __iomem *ipi_mappings[NR_CPUS];
34#endif
867e359b
CM
35
36
37/*
38 * Top-level send_IPI*() functions to send messages to other cpus.
39 */
40
41/* Set by smp_send_stop() to avoid recursive panics. */
42static int stopping_cpus;
43
bbeee4b2
CM
44static void __send_IPI_many(HV_Recipient *recip, int nrecip, int tag)
45{
46 int sent = 0;
47 while (sent < nrecip) {
48 int rc = hv_send_message(recip, nrecip,
49 (HV_VirtAddr)&tag, sizeof(tag));
50 if (rc < 0) {
51 if (!stopping_cpus) /* avoid recursive panic */
52 panic("hv_send_message returned %d", rc);
53 break;
54 }
55 WARN_ONCE(rc == 0, "hv_send_message() returned zero\n");
56 sent += rc;
57 }
58}
59
867e359b
CM
60void send_IPI_single(int cpu, int tag)
61{
62 HV_Recipient recip = {
63 .y = cpu / smp_width,
64 .x = cpu % smp_width,
65 .state = HV_TO_BE_SENT
66 };
bbeee4b2 67 __send_IPI_many(&recip, 1, tag);
867e359b
CM
68}
69
70void send_IPI_many(const struct cpumask *mask, int tag)
71{
72 HV_Recipient recip[NR_CPUS];
bbeee4b2 73 int cpu;
867e359b
CM
74 int nrecip = 0;
75 int my_cpu = smp_processor_id();
76 for_each_cpu(cpu, mask) {
77 HV_Recipient *r;
78 BUG_ON(cpu == my_cpu);
79 r = &recip[nrecip++];
80 r->y = cpu / smp_width;
81 r->x = cpu % smp_width;
82 r->state = HV_TO_BE_SENT;
83 }
bbeee4b2 84 __send_IPI_many(recip, nrecip, tag);
867e359b
CM
85}
86
87void send_IPI_allbutself(int tag)
88{
89 struct cpumask mask;
90 cpumask_copy(&mask, cpu_online_mask);
91 cpumask_clear_cpu(smp_processor_id(), &mask);
92 send_IPI_many(&mask, tag);
93}
94
867e359b
CM
95/*
96 * Functions related to starting/stopping cpus.
97 */
98
99/* Handler to start the current cpu. */
100static void smp_start_cpu_interrupt(void)
101{
867e359b
CM
102 get_irq_regs()->pc = start_cpu_function_addr;
103}
104
105/* Handler to stop the current cpu. */
106static void smp_stop_cpu_interrupt(void)
107{
5d966115 108 arch_local_irq_disable_all();
bc1a298f 109 set_cpu_online(smp_processor_id(), 0);
867e359b 110 for (;;)
8c92ba6c 111 asm("nap; nop");
867e359b
CM
112}
113
114/* This function calls the 'stop' function on all other CPUs in the system. */
115void smp_send_stop(void)
116{
117 stopping_cpus = 1;
118 send_IPI_allbutself(MSG_TAG_STOP_CPU);
119}
120
cb210ee3
CM
121/* On panic, just wait; we may get an smp_send_stop() later on. */
122void panic_smp_self_stop(void)
123{
124 while (1)
125 asm("nap; nop");
126}
867e359b
CM
127
128/*
129 * Dispatch code called from hv_message_intr() for HV_MSG_TILE hv messages.
130 */
131void evaluate_message(int tag)
132{
133 switch (tag) {
134 case MSG_TAG_START_CPU: /* Start up a cpu */
135 smp_start_cpu_interrupt();
136 break;
137
138 case MSG_TAG_STOP_CPU: /* Sent to shut down slave CPU's */
139 smp_stop_cpu_interrupt();
140 break;
141
142 case MSG_TAG_CALL_FUNCTION_MANY: /* Call function on cpumask */
143 generic_smp_call_function_interrupt();
144 break;
145
146 case MSG_TAG_CALL_FUNCTION_SINGLE: /* Call function on one other CPU */
147 generic_smp_call_function_single_interrupt();
148 break;
149
150 default:
151 panic("Unknown IPI message tag %d", tag);
152 break;
153 }
154}
155
156
157/*
158 * flush_icache_range() code uses smp_call_function().
159 */
160
161struct ipi_flush {
162 unsigned long start;
163 unsigned long end;
164};
165
166static void ipi_flush_icache_range(void *info)
167{
168 struct ipi_flush *flush = (struct ipi_flush *) info;
169 __flush_icache_range(flush->start, flush->end);
170}
171
172void flush_icache_range(unsigned long start, unsigned long end)
173{
174 struct ipi_flush flush = { start, end };
3fa17c39
TL
175
176 /* If invoked with irqs disabled, we can not issue IPIs. */
177 if (irqs_disabled())
178 flush_remote(0, HV_FLUSH_EVICT_L1I, NULL, 0, 0, 0,
179 NULL, NULL, 0);
180 else {
181 preempt_disable();
182 on_each_cpu(ipi_flush_icache_range, &flush, 1);
183 preempt_enable();
184 }
867e359b 185}
e3560305 186EXPORT_SYMBOL(flush_icache_range);
867e359b
CM
187
188
fb702b94
CM
189/* Called when smp_send_reschedule() triggers IRQ_RESCHEDULE. */
190static irqreturn_t handle_reschedule_ipi(int irq, void *token)
867e359b 191{
867e359b 192 __get_cpu_var(irq_stat).irq_resched_count++;
184748cc 193 scheduler_ipi();
867e359b
CM
194
195 return IRQ_HANDLED;
196}
197
fb702b94
CM
198static struct irqaction resched_action = {
199 .handler = handle_reschedule_ipi,
200 .name = "resched",
201 .dev_id = handle_reschedule_ipi /* unique token */,
202};
203
204void __init ipi_init(void)
205{
206#if CHIP_HAS_IPI()
207 int cpu;
208 /* Map IPI trigger MMIO addresses. */
209 for_each_possible_cpu(cpu) {
210 HV_Coord tile;
211 HV_PTE pte;
212 unsigned long offset;
213
214 tile.x = cpu_x(cpu);
215 tile.y = cpu_y(cpu);
a78c942d 216 if (hv_get_ipi_pte(tile, KERNEL_PL, &pte) != 0)
fb702b94
CM
217 panic("Failed to initialize IPI for cpu %d\n", cpu);
218
d5d14ed6 219 offset = PFN_PHYS(pte_pfn(pte));
fb702b94
CM
220 ipi_mappings[cpu] = ioremap_prot(offset, PAGE_SIZE, pte);
221 }
222#endif
223
224 /* Bind handle_reschedule_ipi() to IRQ_RESCHEDULE. */
225 tile_irq_activate(IRQ_RESCHEDULE, TILE_IRQ_PERCPU);
226 BUG_ON(setup_irq(IRQ_RESCHEDULE, &resched_action));
227}
228
229#if CHIP_HAS_IPI()
230
231void smp_send_reschedule(int cpu)
232{
233 WARN_ON(cpu_is_offline(cpu));
234
235 /*
236 * We just want to do an MMIO store. The traditional writeq()
237 * functions aren't really correct here, since they're always
238 * directed at the PCI shim. For now, just do a raw store,
239 * casting away the __iomem attribute.
240 */
241 ((unsigned long __force *)ipi_mappings[cpu])[IRQ_RESCHEDULE] = 0;
242}
243
244#else
245
867e359b
CM
246void smp_send_reschedule(int cpu)
247{
248 HV_Coord coord;
249
250 WARN_ON(cpu_is_offline(cpu));
fb702b94
CM
251
252 coord.y = cpu_y(cpu);
253 coord.x = cpu_x(cpu);
867e359b
CM
254 hv_trigger_ipi(coord, IRQ_RESCHEDULE);
255}
fb702b94
CM
256
257#endif /* CHIP_HAS_IPI() */