1 | /*
|
---|
2 | * Copyright (c) 2012 Adam Hraska
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup kernel_generic
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief Facility to invoke functions on other cpus via IPIs.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <smp/smp_call.h>
|
---|
39 | #include <barrier.h>
|
---|
40 | #include <arch/asm.h> /* interrupt_disable */
|
---|
41 | #include <arch.h>
|
---|
42 | #include <assert.h>
|
---|
43 | #include <config.h>
|
---|
44 | #include <preemption.h>
|
---|
45 | #include <cpu.h>
|
---|
46 |
|
---|
47 | static void call_start(smp_call_t *call_info, smp_call_func_t func, void *arg);
|
---|
48 | static void call_done(smp_call_t *call_info);
|
---|
49 | static void call_wait(smp_call_t *call_info);
|
---|
50 |
|
---|
51 | /** Init smp_call() on the local cpu. */
|
---|
52 | void smp_call_init(void)
|
---|
53 | {
|
---|
54 | assert(CPU);
|
---|
55 | assert(PREEMPTION_DISABLED || interrupts_disabled());
|
---|
56 |
|
---|
57 | spinlock_initialize(&CPU->smp_calls_lock, "cpu[].smp_calls_lock");
|
---|
58 | list_initialize(&CPU->smp_pending_calls);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /** Invokes a function on a specific cpu and waits for it to complete.
|
---|
62 | *
|
---|
63 | * Calls @a func on the CPU denoted by its logical id @cpu_id .
|
---|
64 | * The function will execute with interrupts disabled. It should
|
---|
65 | * be a quick and simple function and must never block.
|
---|
66 | *
|
---|
67 | * If @a cpu_id is the local CPU, the function will be invoked
|
---|
68 | * directly.
|
---|
69 | *
|
---|
70 | * All memory accesses of prior to smp_call() will be visible
|
---|
71 | * to @a func on cpu @a cpu_id. Similarly, any changes @a func
|
---|
72 | * makes on cpu @a cpu_id will be visible on this cpu once
|
---|
73 | * smp_call() returns.
|
---|
74 | *
|
---|
75 | * Invoking @a func on the destination cpu acts as a memory barrier
|
---|
76 | * on that cpu.
|
---|
77 | *
|
---|
78 | * @param cpu_id Destination CPU's logical id (eg CPU->id)
|
---|
79 | * @param func Function to call.
|
---|
80 | * @param arg Argument to pass to the user supplied function @a func.
|
---|
81 | */
|
---|
82 | void smp_call(unsigned int cpu_id, smp_call_func_t func, void *arg)
|
---|
83 | {
|
---|
84 | smp_call_t call_info;
|
---|
85 | smp_call_async(cpu_id, func, arg, &call_info);
|
---|
86 | smp_call_wait(&call_info);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Invokes a function on a specific cpu asynchronously.
|
---|
90 | *
|
---|
91 | * Calls @a func on the CPU denoted by its logical id @cpu_id .
|
---|
92 | * The function will execute with interrupts disabled. It should
|
---|
93 | * be a quick and simple function and must never block.
|
---|
94 | *
|
---|
95 | * Pass @a call_info to smp_call_wait() in order to wait for
|
---|
96 | * @a func to complete.
|
---|
97 | *
|
---|
98 | * @a call_info must be valid until/after @a func returns. Use
|
---|
99 | * smp_call_wait() to wait until it is safe to free @a call_info.
|
---|
100 | *
|
---|
101 | * If @a cpu_id is the local CPU, the function will be invoked
|
---|
102 | * directly. If the destination cpu id @a cpu_id is invalid
|
---|
103 | * or denotes an inactive cpu, the call is discarded immediately.
|
---|
104 | *
|
---|
105 | * All memory accesses of the caller prior to smp_call_async()
|
---|
106 | * will be made visible to @a func on the other cpu. Similarly,
|
---|
107 | * any changes @a func makes on cpu @a cpu_id will be visible
|
---|
108 | * to this cpu when smp_call_wait() returns.
|
---|
109 | *
|
---|
110 | * Invoking @a func on the destination cpu acts as a memory barrier
|
---|
111 | * on that cpu.
|
---|
112 | *
|
---|
113 | * Interrupts must be enabled. Otherwise you run the risk
|
---|
114 | * of a deadlock.
|
---|
115 | *
|
---|
116 | * @param cpu_id Destination CPU's logical id (eg CPU->id).
|
---|
117 | * @param func Function to call.
|
---|
118 | * @param arg Argument to pass to the user supplied function @a func.
|
---|
119 | * @param call_info Use it to wait for the function to complete. Must
|
---|
120 | * be valid until the function completes.
|
---|
121 | */
|
---|
122 | void smp_call_async(unsigned int cpu_id, smp_call_func_t func, void *arg,
|
---|
123 | smp_call_t *call_info)
|
---|
124 | {
|
---|
125 | /*
|
---|
126 | * Interrupts must not be disabled or you run the risk of a deadlock
|
---|
127 | * if both the destination and source cpus try to send an IPI to each
|
---|
128 | * other with interrupts disabled. Because the interrupts are disabled
|
---|
129 | * the IPIs cannot be delivered and both cpus will forever busy wait
|
---|
130 | * for an acknowledgment of the IPI from the other cpu.
|
---|
131 | */
|
---|
132 | assert(!interrupts_disabled());
|
---|
133 | assert(call_info != NULL);
|
---|
134 |
|
---|
135 | /* Discard invalid calls. */
|
---|
136 | if (config.cpu_count <= cpu_id || !cpus[cpu_id].active) {
|
---|
137 | call_start(call_info, func, arg);
|
---|
138 | call_done(call_info);
|
---|
139 | return;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /* Protect cpu->id against migration. */
|
---|
143 | preemption_disable();
|
---|
144 |
|
---|
145 | call_start(call_info, func, arg);
|
---|
146 |
|
---|
147 | if (cpu_id != CPU->id) {
|
---|
148 | #ifdef CONFIG_SMP
|
---|
149 | spinlock_lock(&cpus[cpu_id].smp_calls_lock);
|
---|
150 | list_append(&call_info->calls_link, &cpus[cpu_id].smp_pending_calls);
|
---|
151 | spinlock_unlock(&cpus[cpu_id].smp_calls_lock);
|
---|
152 |
|
---|
153 | /*
|
---|
154 | * If a platform supports SMP it must implement arch_smp_call_ipi().
|
---|
155 | * It should issue an IPI on cpu_id and invoke smp_call_ipi_recv()
|
---|
156 | * on cpu_id in turn.
|
---|
157 | *
|
---|
158 | * Do not implement as just an empty dummy function. Instead
|
---|
159 | * consider providing a full implementation or at least a version
|
---|
160 | * that panics if invoked. Note that smp_call_async() never
|
---|
161 | * calls arch_smp_call_ipi() on uniprocessors even if CONFIG_SMP.
|
---|
162 | */
|
---|
163 | arch_smp_call_ipi(cpu_id);
|
---|
164 | #endif
|
---|
165 | } else {
|
---|
166 | /* Invoke local smp calls in place. */
|
---|
167 | ipl_t ipl = interrupts_disable();
|
---|
168 | func(arg);
|
---|
169 | interrupts_restore(ipl);
|
---|
170 |
|
---|
171 | call_done(call_info);
|
---|
172 | }
|
---|
173 |
|
---|
174 | preemption_enable();
|
---|
175 | }
|
---|
176 |
|
---|
177 | /** Waits for a function invoked on another CPU asynchronously to complete.
|
---|
178 | *
|
---|
179 | * Does not sleep but rather spins.
|
---|
180 | *
|
---|
181 | * Example usage:
|
---|
182 | * @code
|
---|
183 | * void hello(void *p) {
|
---|
184 | * puts((char*)p);
|
---|
185 | * }
|
---|
186 | *
|
---|
187 | * smp_call_t call_info;
|
---|
188 | * smp_call_async(cpus[2].id, hello, "hi!\n", &call_info);
|
---|
189 | * // Do some work. In the meantime, hello() is executed on cpu2.
|
---|
190 | * smp_call_wait(&call_info);
|
---|
191 | * @endcode
|
---|
192 | *
|
---|
193 | * @param call_info Initialized by smp_call_async().
|
---|
194 | */
|
---|
195 | void smp_call_wait(smp_call_t *call_info)
|
---|
196 | {
|
---|
197 | call_wait(call_info);
|
---|
198 | }
|
---|
199 |
|
---|
200 | #ifdef CONFIG_SMP
|
---|
201 |
|
---|
202 | /** Architecture independent smp call IPI handler.
|
---|
203 | *
|
---|
204 | * Interrupts must be disabled. Tolerates spurious calls.
|
---|
205 | */
|
---|
206 | void smp_call_ipi_recv(void)
|
---|
207 | {
|
---|
208 | assert(interrupts_disabled());
|
---|
209 | assert(CPU);
|
---|
210 |
|
---|
211 | list_t calls_list;
|
---|
212 | list_initialize(&calls_list);
|
---|
213 |
|
---|
214 | /*
|
---|
215 | * Acts as a load memory barrier. Any changes made by the cpu that
|
---|
216 | * added the smp_call to calls_list will be made visible to this cpu.
|
---|
217 | */
|
---|
218 | spinlock_lock(&CPU->smp_calls_lock);
|
---|
219 | list_concat(&calls_list, &CPU->smp_pending_calls);
|
---|
220 | spinlock_unlock(&CPU->smp_calls_lock);
|
---|
221 |
|
---|
222 | /* Walk the list manually, so that we can safely remove list items. */
|
---|
223 | for (link_t *cur = calls_list.head.next, *next = cur->next;
|
---|
224 | !list_empty(&calls_list); cur = next, next = cur->next) {
|
---|
225 |
|
---|
226 | smp_call_t *call_info = list_get_instance(cur, smp_call_t, calls_link);
|
---|
227 | list_remove(cur);
|
---|
228 |
|
---|
229 | call_info->func(call_info->arg);
|
---|
230 | call_done(call_info);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | #endif /* CONFIG_SMP */
|
---|
235 |
|
---|
236 | static void call_start(smp_call_t *call_info, smp_call_func_t func, void *arg)
|
---|
237 | {
|
---|
238 | link_initialize(&call_info->calls_link);
|
---|
239 | call_info->func = func;
|
---|
240 | call_info->arg = arg;
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * We can't use standard spinlocks here because we want to lock
|
---|
244 | * the structure on one cpu and unlock it on another (without
|
---|
245 | * messing up the preemption count).
|
---|
246 | */
|
---|
247 | atomic_store(&call_info->pending, 1);
|
---|
248 |
|
---|
249 | /* Let initialization complete before continuing. */
|
---|
250 | memory_barrier();
|
---|
251 | }
|
---|
252 |
|
---|
253 | static void call_done(smp_call_t *call_info)
|
---|
254 | {
|
---|
255 | /*
|
---|
256 | * Separate memory accesses of the called function from the
|
---|
257 | * announcement of its completion.
|
---|
258 | */
|
---|
259 | memory_barrier();
|
---|
260 | atomic_store(&call_info->pending, 0);
|
---|
261 | }
|
---|
262 |
|
---|
263 | static void call_wait(smp_call_t *call_info)
|
---|
264 | {
|
---|
265 | do {
|
---|
266 | /*
|
---|
267 | * Ensure memory accesses following call_wait() are ordered
|
---|
268 | * after completion of the called function on another cpu.
|
---|
269 | * Also, speed up loading of call_info->pending.
|
---|
270 | */
|
---|
271 | memory_barrier();
|
---|
272 | } while (atomic_load(&call_info->pending));
|
---|
273 | }
|
---|
274 |
|
---|
275 | /** @}
|
---|
276 | */
|
---|