smp.c 8.5 KB
Newer Older
V
Vineet Gupta 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * RajeshwarR: Dec 11, 2007
 *   -- Added support for Inter Processor Interrupts
 *
 * Vineetg: Nov 1st, 2007
 *    -- Initial Write (Borrowed heavily from ARM)
 */

#include <linux/spinlock.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/profile.h>
#include <linux/mm.h>
#include <linux/cpu.h>
#include <linux/irq.h>
#include <linux/atomic.h>
#include <linux/cpumask.h>
#include <linux/reboot.h>
#include <asm/processor.h>
#include <asm/setup.h>
27
#include <asm/mach_desc.h>
V
Vineet Gupta 已提交
28

29
#ifndef CONFIG_ARC_HAS_LLSC
V
Vineet Gupta 已提交
30 31
arch_spinlock_t smp_atomic_ops_lock = __ARCH_SPIN_LOCK_UNLOCKED;
arch_spinlock_t smp_bitops_lock = __ARCH_SPIN_LOCK_UNLOCKED;
32
#endif
V
Vineet Gupta 已提交
33

34
struct plat_smp_ops  __weak plat_smp_ops;
35

V
Vineet Gupta 已提交
36 37 38 39 40 41 42 43 44
/* XXX: per cpu ? Only needed once in early seconday boot */
struct task_struct *secondary_idle_tsk;

/* Called from start_kernel */
void __init smp_prepare_boot_cpu(void)
{
}

/*
45 46 47 48 49 50 51
 * Called from setup_arch() before calling setup_processor()
 *
 * - Initialise the CPU possible map early - this describes the CPUs
 *   which may be present or become present in the system.
 * - Call early smp init hook. This can initialize a specific multi-core
 *   IP which is say common to several platforms (hence not part of
 *   platform specific int_early() hook)
V
Vineet Gupta 已提交
52 53 54 55 56 57 58
 */
void __init smp_init_cpus(void)
{
	unsigned int i;

	for (i = 0; i < NR_CPUS; i++)
		set_cpu_possible(i, true);
59 60 61

	if (plat_smp_ops.init_early_smp)
		plat_smp_ops.init_early_smp();
V
Vineet Gupta 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
}

/* called from init ( ) =>  process 1 */
void __init smp_prepare_cpus(unsigned int max_cpus)
{
	int i;

	/*
	 * Initialise the present map, which describes the set of CPUs
	 * actually populated at the present time.
	 */
	for (i = 0; i < max_cpus; i++)
		set_cpu_present(i, true);
}

void __init smp_cpus_done(unsigned int max_cpus)
{

}

/*
83 84 85 86
 * Default smp boot helper for Run-on-reset case where all cores start off
 * together. Non-masters need to wait for Master to start running.
 * This is implemented using a flag in memory, which Non-masters spin-wait on.
 * Master sets it to cpu-id of core to "ungate" it.
V
Vineet Gupta 已提交
87
 */
88 89 90
static volatile int wake_flag;

static void arc_default_smp_cpu_kick(int cpu, unsigned long pc)
V
Vineet Gupta 已提交
91
{
92 93 94 95 96 97 98 99 100 101 102
	BUG_ON(cpu == 0);
	wake_flag = cpu;
}

void arc_platform_smp_wait_to_boot(int cpu)
{
	while (wake_flag != cpu)
		;

	wake_flag = 0;
	__asm__ __volatile__("j @first_lines_of_secondary	\n");
V
Vineet Gupta 已提交
103 104
}

105

106 107
const char *arc_platform_smp_cpuinfo(void)
{
108
	return plat_smp_ops.info ? : "";
109 110
}

V
Vineet Gupta 已提交
111 112 113 114 115
/*
 * The very first "C" code executed by secondary
 * Called from asm stub in head.S
 * "current"/R25 already setup by low level boot code
 */
116
void start_kernel_secondary(void)
V
Vineet Gupta 已提交
117 118 119 120 121 122 123 124 125 126
{
	struct mm_struct *mm = &init_mm;
	unsigned int cpu = smp_processor_id();

	/* MMU, Caches, Vector Table, Interrupts etc */
	setup_processor();

	atomic_inc(&mm->mm_users);
	atomic_inc(&mm->mm_count);
	current->active_mm = mm;
V
Vineet Gupta 已提交
127
	cpumask_set_cpu(cpu, mm_cpumask(mm));
V
Vineet Gupta 已提交
128 129 130 131 132 133

	notify_cpu_starting(cpu);
	set_cpu_online(cpu, true);

	pr_info("## CPU%u LIVE ##: Executing Code...\n", cpu);

134
	/* Some SMP H/w setup - for each cpu */
135 136
	if (plat_smp_ops.init_per_cpu)
		plat_smp_ops.init_per_cpu(cpu);
137

138 139
	if (machine_desc->init_per_cpu)
		machine_desc->init_per_cpu(cpu);
V
Vineet Gupta 已提交
140 141 142

	local_irq_enable();
	preempt_disable();
143
	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
V
Vineet Gupta 已提交
144 145 146 147 148 149 150 151 152 153 154 155
}

/*
 * Called from kernel_init( ) -> smp_init( ) - for each CPU
 *
 * At this point, Secondary Processor  is "HALT"ed:
 *  -It booted, but was halted in head.S
 *  -It was configured to halt-on-reset
 *  So need to wake it up.
 *
 * Essential requirements being where to run from (PC) and stack (SP)
*/
156
int __cpu_up(unsigned int cpu, struct task_struct *idle)
V
Vineet Gupta 已提交
157 158 159 160 161 162 163 164
{
	unsigned long wait_till;

	secondary_idle_tsk = idle;

	pr_info("Idle Task [%d] %p", cpu, idle);
	pr_info("Trying to bring up CPU%u ...\n", cpu);

165 166
	if (plat_smp_ops.cpu_kick)
		plat_smp_ops.cpu_kick(cpu,
V
Vineet Gupta 已提交
167
				(unsigned long)first_lines_of_secondary);
168 169
	else
		arc_default_smp_cpu_kick(cpu, (unsigned long)NULL);
V
Vineet Gupta 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

	/* wait for 1 sec after kicking the secondary */
	wait_till = jiffies + HZ;
	while (time_before(jiffies, wait_till)) {
		if (cpu_online(cpu))
			break;
	}

	if (!cpu_online(cpu)) {
		pr_info("Timeout: CPU%u FAILED to comeup !!!\n", cpu);
		return -1;
	}

	secondary_idle_tsk = NULL;

	return 0;
}

/*
 * not supported here
 */
191
int setup_profiling_timer(unsigned int multiplier)
V
Vineet Gupta 已提交
192 193 194 195 196 197 198 199 200
{
	return -EINVAL;
}

/*****************************************************************************/
/*              Inter Processor Interrupt Handling                           */
/*****************************************************************************/

enum ipi_msg_type {
V
Vineet Gupta 已提交
201
	IPI_EMPTY = 0,
V
Vineet Gupta 已提交
202 203
	IPI_RESCHEDULE = 1,
	IPI_CALL_FUNC,
V
Vineet Gupta 已提交
204
	IPI_CPU_STOP,
V
Vineet Gupta 已提交
205 206
};

V
Vineet Gupta 已提交
207 208 209 210 211
/*
 * In arches with IRQ for each msg type (above), receiver can use IRQ-id  to
 * figure out what msg was sent. For those which don't (ARC has dedicated IPI
 * IRQ), the msg-type needs to be conveyed via per-cpu data
 */
V
Vineet Gupta 已提交
212

V
Vineet Gupta 已提交
213
static DEFINE_PER_CPU(unsigned long, ipi_data);
V
Vineet Gupta 已提交
214

215
static void ipi_send_msg_one(int cpu, enum ipi_msg_type msg)
V
Vineet Gupta 已提交
216
{
V
Vineet Gupta 已提交
217
	unsigned long __percpu *ipi_data_ptr = per_cpu_ptr(&ipi_data, cpu);
218
	unsigned long old, new;
V
Vineet Gupta 已提交
219 220
	unsigned long flags;

V
Vineet Gupta 已提交
221 222
	pr_debug("%d Sending msg [%d] to %d\n", smp_processor_id(), msg, cpu);

V
Vineet Gupta 已提交
223 224
	local_irq_save(flags);

225 226 227 228 229
	/*
	 * Atomically write new msg bit (in case others are writing too),
	 * and read back old value
	 */
	do {
230
		new = old = ACCESS_ONCE(*ipi_data_ptr);
231 232
		new |= 1U << msg;
	} while (cmpxchg(ipi_data_ptr, old, new) != old);
V
Vineet Gupta 已提交
233

234 235 236 237 238 239 240 241 242
	/*
	 * Call the platform specific IPI kick function, but avoid if possible:
	 * Only do so if there's no pending msg from other concurrent sender(s).
	 * Otherwise, recevier will see this msg as well when it takes the
	 * IPI corresponding to that msg. This is true, even if it is already in
	 * IPI handler, because !@old means it has not yet dequeued the msg(s)
	 * so @new msg can be a free-loader
	 */
	if (plat_smp_ops.ipi_send && !old)
243
		plat_smp_ops.ipi_send(cpu);
V
Vineet Gupta 已提交
244 245 246 247

	local_irq_restore(flags);
}

248 249 250 251 252 253 254 255
static void ipi_send_msg(const struct cpumask *callmap, enum ipi_msg_type msg)
{
	unsigned int cpu;

	for_each_cpu(cpu, callmap)
		ipi_send_msg_one(cpu, msg);
}

V
Vineet Gupta 已提交
256 257
void smp_send_reschedule(int cpu)
{
258
	ipi_send_msg_one(cpu, IPI_RESCHEDULE);
V
Vineet Gupta 已提交
259 260 261 262 263 264 265 266 267 268 269 270
}

void smp_send_stop(void)
{
	struct cpumask targets;
	cpumask_copy(&targets, cpu_online_mask);
	cpumask_clear_cpu(smp_processor_id(), &targets);
	ipi_send_msg(&targets, IPI_CPU_STOP);
}

void arch_send_call_function_single_ipi(int cpu)
{
271
	ipi_send_msg_one(cpu, IPI_CALL_FUNC);
V
Vineet Gupta 已提交
272 273 274 275 276 277 278 279 280 281
}

void arch_send_call_function_ipi_mask(const struct cpumask *mask)
{
	ipi_send_msg(mask, IPI_CALL_FUNC);
}

/*
 * ipi_cpu_stop - handle IPI from smp_send_stop()
 */
282
static void ipi_cpu_stop(void)
V
Vineet Gupta 已提交
283 284 285 286
{
	machine_halt();
}

287
static inline int __do_IPI(unsigned long msg)
V
Vineet Gupta 已提交
288
{
289 290
	int rc = 0;

291 292 293 294
	switch (msg) {
	case IPI_RESCHEDULE:
		scheduler_ipi();
		break;
V
Vineet Gupta 已提交
295

296 297 298
	case IPI_CALL_FUNC:
		generic_smp_call_function_interrupt();
		break;
V
Vineet Gupta 已提交
299

300 301 302
	case IPI_CPU_STOP:
		ipi_cpu_stop();
		break;
V
Vineet Gupta 已提交
303

304
	default:
305
		rc = 1;
V
Vineet Gupta 已提交
306
	}
307 308

	return rc;
V
Vineet Gupta 已提交
309 310 311 312 313 314 315 316
}

/*
 * arch-common ISR to handle for inter-processor interrupts
 * Has hooks for platform specific IPI
 */
irqreturn_t do_IPI(int irq, void *dev_id)
{
V
Vineet Gupta 已提交
317
	unsigned long pending;
318
	unsigned long __maybe_unused copy;
V
Vineet Gupta 已提交
319 320 321

	pr_debug("IPI [%ld] received on cpu %d\n",
		 *this_cpu_ptr(&ipi_data), smp_processor_id());
V
Vineet Gupta 已提交
322

323
	if (plat_smp_ops.ipi_clear)
324
		plat_smp_ops.ipi_clear(irq);
V
Vineet Gupta 已提交
325 326

	/*
327 328
	 * "dequeue" the msg corresponding to this IPI (and possibly other
	 * piggybacked msg from elided IPIs: see ipi_send_msg_one() above)
V
Vineet Gupta 已提交
329
	 */
330
	copy = pending = xchg(this_cpu_ptr(&ipi_data), 0);
331 332 333

	do {
		unsigned long msg = __ffs(pending);
334 335 336 337 338
		int rc;

		rc = __do_IPI(msg);
		if (rc)
			pr_info("IPI with bogus msg %ld in %ld\n", msg, copy);
339 340
		pending &= ~(1U << msg);
	} while (pending);
V
Vineet Gupta 已提交
341 342 343 344 345 346

	return IRQ_HANDLED;
}

/*
 * API called by platform code to hookup arch-common ISR to their IPI IRQ
347 348 349 350
 *
 * Note: If IPI is provided by platform (vs. say ARC MCIP), their intc setup/map
 * function needs to call call irq_set_percpu_devid() for IPI IRQ, otherwise
 * request_percpu_irq() below will fail
V
Vineet Gupta 已提交
351 352
 */
static DEFINE_PER_CPU(int, ipi_dev);
N
Noam Camus 已提交
353

V
Vineet Gupta 已提交
354 355
int smp_ipi_irq_setup(int cpu, int irq)
{
356 357
	int *dev = per_cpu_ptr(&ipi_dev, cpu);

358 359 360 361 362 363 364 365 366 367
	/* Boot cpu calls request, all call enable */
	if (!cpu) {
		int rc;

		rc = request_percpu_irq(irq, do_IPI, "IPI Interrupt", dev);
		if (rc)
			panic("Percpu IRQ request failed for %d\n", irq);
	}

	enable_percpu_irq(irq, 0);
N
Noam Camus 已提交
368 369

	return 0;
V
Vineet Gupta 已提交
370
}