blob: 6cc520d71d2b1bdcadd0d086df2c671cc66ec207 [file] [log] [blame]
Chris Metcalf867e3592010-05-28 23:09:12 -04001/*
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>
Chris Metcalffb702b92010-06-25 16:41:11 -040018#include <linux/interrupt.h>
19#include <linux/io.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040020#include <linux/irq.h>
Chris Metcalffb702b92010-06-25 16:41:11 -040021#include <linux/module.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040022#include <asm/cacheflush.h>
23
24HV_Topology smp_topology __write_once;
Chris Metcalffb702b92010-06-25 16:41:11 -040025EXPORT_SYMBOL(smp_topology);
26
27#if CHIP_HAS_IPI()
28static unsigned long __iomem *ipi_mappings[NR_CPUS];
29#endif
Chris Metcalf867e3592010-05-28 23:09:12 -040030
31
32/*
33 * Top-level send_IPI*() functions to send messages to other cpus.
34 */
35
36/* Set by smp_send_stop() to avoid recursive panics. */
37static int stopping_cpus;
38
Chris Metcalfbbeee4b22011-02-28 13:32:14 -050039static void __send_IPI_many(HV_Recipient *recip, int nrecip, int tag)
40{
41 int sent = 0;
42 while (sent < nrecip) {
43 int rc = hv_send_message(recip, nrecip,
44 (HV_VirtAddr)&tag, sizeof(tag));
45 if (rc < 0) {
46 if (!stopping_cpus) /* avoid recursive panic */
47 panic("hv_send_message returned %d", rc);
48 break;
49 }
50 WARN_ONCE(rc == 0, "hv_send_message() returned zero\n");
51 sent += rc;
52 }
53}
54
Chris Metcalf867e3592010-05-28 23:09:12 -040055void send_IPI_single(int cpu, int tag)
56{
57 HV_Recipient recip = {
58 .y = cpu / smp_width,
59 .x = cpu % smp_width,
60 .state = HV_TO_BE_SENT
61 };
Chris Metcalfbbeee4b22011-02-28 13:32:14 -050062 __send_IPI_many(&recip, 1, tag);
Chris Metcalf867e3592010-05-28 23:09:12 -040063}
64
65void send_IPI_many(const struct cpumask *mask, int tag)
66{
67 HV_Recipient recip[NR_CPUS];
Chris Metcalfbbeee4b22011-02-28 13:32:14 -050068 int cpu;
Chris Metcalf867e3592010-05-28 23:09:12 -040069 int nrecip = 0;
70 int my_cpu = smp_processor_id();
71 for_each_cpu(cpu, mask) {
72 HV_Recipient *r;
73 BUG_ON(cpu == my_cpu);
74 r = &recip[nrecip++];
75 r->y = cpu / smp_width;
76 r->x = cpu % smp_width;
77 r->state = HV_TO_BE_SENT;
78 }
Chris Metcalfbbeee4b22011-02-28 13:32:14 -050079 __send_IPI_many(recip, nrecip, tag);
Chris Metcalf867e3592010-05-28 23:09:12 -040080}
81
82void send_IPI_allbutself(int tag)
83{
84 struct cpumask mask;
85 cpumask_copy(&mask, cpu_online_mask);
86 cpumask_clear_cpu(smp_processor_id(), &mask);
87 send_IPI_many(&mask, tag);
88}
89
Chris Metcalf867e3592010-05-28 23:09:12 -040090/*
91 * Functions related to starting/stopping cpus.
92 */
93
94/* Handler to start the current cpu. */
95static void smp_start_cpu_interrupt(void)
96{
Chris Metcalf867e3592010-05-28 23:09:12 -040097 get_irq_regs()->pc = start_cpu_function_addr;
98}
99
100/* Handler to stop the current cpu. */
101static void smp_stop_cpu_interrupt(void)
102{
Chris Metcalf5d966112010-11-01 15:24:29 -0400103 arch_local_irq_disable_all();
Chris Metcalfbc1a2982013-08-07 11:36:54 -0400104 set_cpu_online(smp_processor_id(), 0);
Chris Metcalf867e3592010-05-28 23:09:12 -0400105 for (;;)
Chris Metcalf8c92ba62012-03-29 15:57:18 -0400106 asm("nap; nop");
Chris Metcalf867e3592010-05-28 23:09:12 -0400107}
108
109/* This function calls the 'stop' function on all other CPUs in the system. */
110void smp_send_stop(void)
111{
112 stopping_cpus = 1;
113 send_IPI_allbutself(MSG_TAG_STOP_CPU);
114}
115
Chris Metcalfcb210ee2012-03-29 15:59:11 -0400116/* On panic, just wait; we may get an smp_send_stop() later on. */
117void panic_smp_self_stop(void)
118{
119 while (1)
120 asm("nap; nop");
121}
Chris Metcalf867e3592010-05-28 23:09:12 -0400122
123/*
124 * Dispatch code called from hv_message_intr() for HV_MSG_TILE hv messages.
125 */
126void evaluate_message(int tag)
127{
128 switch (tag) {
129 case MSG_TAG_START_CPU: /* Start up a cpu */
130 smp_start_cpu_interrupt();
131 break;
132
133 case MSG_TAG_STOP_CPU: /* Sent to shut down slave CPU's */
134 smp_stop_cpu_interrupt();
135 break;
136
137 case MSG_TAG_CALL_FUNCTION_MANY: /* Call function on cpumask */
138 generic_smp_call_function_interrupt();
139 break;
140
141 case MSG_TAG_CALL_FUNCTION_SINGLE: /* Call function on one other CPU */
142 generic_smp_call_function_single_interrupt();
143 break;
144
145 default:
146 panic("Unknown IPI message tag %d", tag);
147 break;
148 }
149}
150
151
152/*
153 * flush_icache_range() code uses smp_call_function().
154 */
155
156struct ipi_flush {
157 unsigned long start;
158 unsigned long end;
159};
160
161static void ipi_flush_icache_range(void *info)
162{
163 struct ipi_flush *flush = (struct ipi_flush *) info;
164 __flush_icache_range(flush->start, flush->end);
165}
166
167void flush_icache_range(unsigned long start, unsigned long end)
168{
169 struct ipi_flush flush = { start, end };
170 preempt_disable();
171 on_each_cpu(ipi_flush_icache_range, &flush, 1);
172 preempt_enable();
173}
174
175
Chris Metcalffb702b92010-06-25 16:41:11 -0400176/* Called when smp_send_reschedule() triggers IRQ_RESCHEDULE. */
177static irqreturn_t handle_reschedule_ipi(int irq, void *token)
Chris Metcalf867e3592010-05-28 23:09:12 -0400178{
Chris Metcalf867e3592010-05-28 23:09:12 -0400179 __get_cpu_var(irq_stat).irq_resched_count++;
Peter Zijlstra184748c2011-04-05 17:23:39 +0200180 scheduler_ipi();
Chris Metcalf867e3592010-05-28 23:09:12 -0400181
182 return IRQ_HANDLED;
183}
184
Chris Metcalffb702b92010-06-25 16:41:11 -0400185static struct irqaction resched_action = {
186 .handler = handle_reschedule_ipi,
187 .name = "resched",
188 .dev_id = handle_reschedule_ipi /* unique token */,
189};
190
191void __init ipi_init(void)
192{
193#if CHIP_HAS_IPI()
194 int cpu;
195 /* Map IPI trigger MMIO addresses. */
196 for_each_possible_cpu(cpu) {
197 HV_Coord tile;
198 HV_PTE pte;
199 unsigned long offset;
200
201 tile.x = cpu_x(cpu);
202 tile.y = cpu_y(cpu);
Chris Metcalfa78c9422010-10-14 16:23:03 -0400203 if (hv_get_ipi_pte(tile, KERNEL_PL, &pte) != 0)
Chris Metcalffb702b92010-06-25 16:41:11 -0400204 panic("Failed to initialize IPI for cpu %d\n", cpu);
205
Chris Metcalfd5d14ed2012-03-29 13:58:43 -0400206 offset = PFN_PHYS(pte_pfn(pte));
Chris Metcalffb702b92010-06-25 16:41:11 -0400207 ipi_mappings[cpu] = ioremap_prot(offset, PAGE_SIZE, pte);
208 }
209#endif
210
211 /* Bind handle_reschedule_ipi() to IRQ_RESCHEDULE. */
212 tile_irq_activate(IRQ_RESCHEDULE, TILE_IRQ_PERCPU);
213 BUG_ON(setup_irq(IRQ_RESCHEDULE, &resched_action));
214}
215
216#if CHIP_HAS_IPI()
217
218void smp_send_reschedule(int cpu)
219{
220 WARN_ON(cpu_is_offline(cpu));
221
222 /*
223 * We just want to do an MMIO store. The traditional writeq()
224 * functions aren't really correct here, since they're always
225 * directed at the PCI shim. For now, just do a raw store,
226 * casting away the __iomem attribute.
227 */
228 ((unsigned long __force *)ipi_mappings[cpu])[IRQ_RESCHEDULE] = 0;
229}
230
231#else
232
Chris Metcalf867e3592010-05-28 23:09:12 -0400233void smp_send_reschedule(int cpu)
234{
235 HV_Coord coord;
236
237 WARN_ON(cpu_is_offline(cpu));
Chris Metcalffb702b92010-06-25 16:41:11 -0400238
239 coord.y = cpu_y(cpu);
240 coord.x = cpu_x(cpu);
Chris Metcalf867e3592010-05-28 23:09:12 -0400241 hv_trigger_ipi(coord, IRQ_RESCHEDULE);
242}
Chris Metcalffb702b92010-06-25 16:41:11 -0400243
244#endif /* CHIP_HAS_IPI() */