smp.c 9.6 KB
Newer Older
1
/*
2
 * IPI management based on arch/arm/kernel/smp.c (Copyright 2002 ARM Limited)
3
 *
4 5
 * Copyright 2007-2009 Analog Devices Inc.
 *                         Philippe Gerum <rpm@xenomai.org>
6
 *
7
 * Licensed under the GPL-2.
8 9 10 11 12 13 14 15 16
 */

#include <linux/module.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/cache.h>
B
Bob Liu 已提交
17
#include <linux/clockchips.h>
18 19 20 21 22
#include <linux/profile.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/cpu.h>
#include <linux/smp.h>
23
#include <linux/cpumask.h>
24 25
#include <linux/seq_file.h>
#include <linux/irq.h>
26
#include <linux/slab.h>
A
Arun Sharma 已提交
27
#include <linux/atomic.h>
28
#include <asm/cacheflush.h>
29
#include <asm/irq_handler.h>
30 31 32 33 34 35
#include <asm/mmu_context.h>
#include <asm/pgtable.h>
#include <asm/pgalloc.h>
#include <asm/processor.h>
#include <asm/ptrace.h>
#include <asm/cpu.h>
36
#include <asm/time.h>
37 38
#include <linux/err.h>

G
Graf Yang 已提交
39 40 41 42
/*
 * Anomaly notes:
 * 05000120 - we always define corelock as 32-bit integer in L2
 */
43 44
struct corelock_slot corelock __attribute__ ((__section__(".l2.bss")));

45 46 47 48
#ifdef CONFIG_ICACHE_FLUSH_L1
unsigned long blackfin_iflush_l1_entry[NR_CPUS];
#endif

49
struct blackfin_initial_pda initial_pda_coreb;
50

51
enum ipi_message_type {
S
Steven Miao 已提交
52
	BFIN_IPI_NONE,
53 54 55 56 57
	BFIN_IPI_TIMER,
	BFIN_IPI_RESCHEDULE,
	BFIN_IPI_CALL_FUNC,
	BFIN_IPI_CPU_STOP,
};
58 59 60 61 62 63 64 65 66 67 68 69

struct blackfin_flush_data {
	unsigned long start;
	unsigned long end;
};

void *secondary_stack;

static struct blackfin_flush_data smp_flush_data;

static DEFINE_SPINLOCK(stop_lock);

70 71 72 73
/* A magic number - stress test shows this is safe for common cases */
#define BFIN_IPI_MSGQ_LEN 5

/* Simple FIFO buffer, overflow leads to panic */
74
struct ipi_data {
S
Steven Miao 已提交
75 76
	atomic_t count;
	atomic_t bits;
77 78
};

79
static DEFINE_PER_CPU(struct ipi_data, bfin_ipi);
80 81 82 83 84 85 86 87

static void ipi_cpu_stop(unsigned int cpu)
{
	spin_lock(&stop_lock);
	printk(KERN_CRIT "CPU%u: stopping\n", cpu);
	dump_stack();
	spin_unlock(&stop_lock);

88
	set_cpu_online(cpu, false);
89 90 91 92 93 94 95 96 97 98 99 100

	local_irq_disable();

	while (1)
		SSYNC();
}

static void ipi_flush_icache(void *info)
{
	struct blackfin_flush_data *fdata = info;

	/* Invalidate the memory holding the bounds of the flushed region. */
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	blackfin_dcache_invalidate_range((unsigned long)fdata,
					 (unsigned long)fdata + sizeof(*fdata));

	/* Make sure all write buffers in the data side of the core
	 * are flushed before trying to invalidate the icache.  This
	 * needs to be after the data flush and before the icache
	 * flush so that the SSYNC does the right thing in preventing
	 * the instruction prefetcher from hitting things in cached
	 * memory at the wrong time -- it runs much further ahead than
	 * the pipeline.
	 */
	SSYNC();

	/* ipi_flaush_icache is invoked by generic flush_icache_range,
	 * so call blackfin arch icache flush directly here.
	 */
	blackfin_icache_flush_range(fdata->start, fdata->end);
118 119
}

120 121 122 123 124 125 126 127 128 129 130
/* Use IRQ_SUPPLE_0 to request reschedule.
 * When returning from interrupt to user space,
 * there is chance to reschedule */
static irqreturn_t ipi_handler_int0(int irq, void *dev_instance)
{
	unsigned int cpu = smp_processor_id();

	platform_clear_ipi(cpu, IRQ_SUPPLE_0);
	return IRQ_HANDLED;
}

B
Bob Liu 已提交
131 132 133 134 135 136 137 138
DECLARE_PER_CPU(struct clock_event_device, coretmr_events);
void ipi_timer(void)
{
	int cpu = smp_processor_id();
	struct clock_event_device *evt = &per_cpu(coretmr_events, cpu);
	evt->event_handler(evt);
}

139
static irqreturn_t ipi_handler_int1(int irq, void *dev_instance)
140
{
141
	struct ipi_data *bfin_ipi_data;
142
	unsigned int cpu = smp_processor_id();
143 144
	unsigned long pending;
	unsigned long msg;
145

146
	platform_clear_ipi(cpu, IRQ_SUPPLE_1);
147

148
	smp_rmb();
149
	bfin_ipi_data = this_cpu_ptr(&bfin_ipi);
150
	while ((pending = atomic_xchg(&bfin_ipi_data->bits, 0)) != 0) {
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		msg = 0;
		do {
			msg = find_next_bit(&pending, BITS_PER_LONG, msg + 1);
			switch (msg) {
			case BFIN_IPI_TIMER:
				ipi_timer();
				break;
			case BFIN_IPI_RESCHEDULE:
				scheduler_ipi();
				break;
			case BFIN_IPI_CALL_FUNC:
				generic_smp_call_function_interrupt();
				break;
			case BFIN_IPI_CPU_STOP:
				ipi_cpu_stop(cpu);
				break;
167 168
			default:
				goto out;
169
			}
S
Steven Miao 已提交
170
			atomic_dec(&bfin_ipi_data->count);
171
		} while (msg < BITS_PER_LONG);
172

173
	}
174
out:
175 176 177
	return IRQ_HANDLED;
}

178
static void bfin_ipi_init(void)
179 180
{
	unsigned int cpu;
181
	struct ipi_data *bfin_ipi_data;
182
	for_each_possible_cpu(cpu) {
183
		bfin_ipi_data = &per_cpu(bfin_ipi, cpu);
184 185
		atomic_set(&bfin_ipi_data->bits, 0);
		atomic_set(&bfin_ipi_data->count, 0);
186 187 188
	}
}

189
void send_ipi(const struct cpumask *cpumask, enum ipi_message_type msg)
190 191
{
	unsigned int cpu;
192 193 194 195 196 197
	struct ipi_data *bfin_ipi_data;
	unsigned long flags;

	local_irq_save(flags);
	for_each_cpu(cpu, cpumask) {
		bfin_ipi_data = &per_cpu(bfin_ipi, cpu);
198
		atomic_or((1 << msg), &bfin_ipi_data->bits);
S
Steven Miao 已提交
199
		atomic_inc(&bfin_ipi_data->count);
200
	}
201
	local_irq_restore(flags);
202 203 204
	smp_wmb();
	for_each_cpu(cpu, cpumask)
		platform_send_ipi_cpu(cpu, IRQ_SUPPLE_1);
205 206
}

207
void arch_send_call_function_single_ipi(int cpu)
208
{
209
	send_ipi(cpumask_of(cpu), BFIN_IPI_CALL_FUNC);
210 211
}

212
void arch_send_call_function_ipi_mask(const struct cpumask *mask)
213
{
214
	send_ipi(mask, BFIN_IPI_CALL_FUNC);
215 216 217 218
}

void smp_send_reschedule(int cpu)
{
219
	send_ipi(cpumask_of(cpu), BFIN_IPI_RESCHEDULE);
220 221 222 223

	return;
}

B
Bob Liu 已提交
224 225
void smp_send_msg(const struct cpumask *mask, unsigned long type)
{
226
	send_ipi(mask, type);
B
Bob Liu 已提交
227 228 229 230 231 232 233
}

void smp_timer_broadcast(const struct cpumask *mask)
{
	smp_send_msg(mask, BFIN_IPI_TIMER);
}

234 235 236 237
void smp_send_stop(void)
{
	cpumask_t callmap;

238
	preempt_disable();
239 240 241
	cpumask_copy(&callmap, cpu_online_mask);
	cpumask_clear_cpu(smp_processor_id(), &callmap);
	if (!cpumask_empty(&callmap))
242
		send_ipi(&callmap, BFIN_IPI_CPU_STOP);
243

244
	preempt_enable();
245 246 247 248

	return;
}

249
int __cpu_up(unsigned int cpu, struct task_struct *idle)
250 251
{
	int ret;
252

253 254 255 256 257 258 259 260 261
	secondary_stack = task_stack_page(idle) + THREAD_SIZE;

	ret = platform_boot_secondary(cpu, idle);

	secondary_stack = NULL;

	return ret;
}

262
static void setup_secondary(unsigned int cpu)
263 264 265 266 267 268 269 270 271 272 273 274
{
	unsigned long ilat;

	bfin_write_IMASK(0);
	CSYNC();
	ilat = bfin_read_ILAT();
	CSYNC();
	bfin_write_ILAT(ilat);
	CSYNC();

	/* Enable interrupt levels IVG7-15. IARs have been already
	 * programmed by the boot CPU.  */
275
	bfin_irq_flags |= IMASK_IVG15 |
276 277 278 279
	    IMASK_IVG14 | IMASK_IVG13 | IMASK_IVG12 | IMASK_IVG11 |
	    IMASK_IVG10 | IMASK_IVG9 | IMASK_IVG8 | IMASK_IVG7 | IMASK_IVGHW;
}

280
void secondary_start_kernel(void)
281 282 283 284 285 286 287
{
	unsigned int cpu = smp_processor_id();
	struct mm_struct *mm = &init_mm;

	if (_bfin_swrst & SWRST_DBL_FAULT_B) {
		printk(KERN_EMERG "CoreB Recovering from DOUBLE FAULT event\n");
#ifdef CONFIG_DEBUG_DOUBLEFAULT
288 289 290 291 292 293 294
		printk(KERN_EMERG " While handling exception (EXCAUSE = %#x) at %pF\n",
			initial_pda_coreb.seqstat_doublefault & SEQSTAT_EXCAUSE,
			initial_pda_coreb.retx_doublefault);
		printk(KERN_NOTICE "   DCPLB_FAULT_ADDR: %pF\n",
			initial_pda_coreb.dcplb_doublefault_addr);
		printk(KERN_NOTICE "   ICPLB_FAULT_ADDR: %pF\n",
			initial_pda_coreb.icplb_doublefault_addr);
295 296
#endif
		printk(KERN_NOTICE " The instruction at %pF caused a double exception\n",
297
			initial_pda_coreb.retx);
298 299 300 301 302 303 304 305 306 307 308 309
	}

	/*
	 * We want the D-cache to be enabled early, in case the atomic
	 * support code emulates cache coherence (see
	 * __ARCH_SYNC_CORE_DCACHE).
	 */
	init_exception_vectors();

	local_irq_disable();

	/* Attach the new idle task to the global mm. */
V
Vegard Nossum 已提交
310
	mmget(mm);
V
Vegard Nossum 已提交
311
	mmgrab(mm);
312 313 314 315 316 317
	current->active_mm = mm;

	preempt_disable();

	setup_secondary(cpu);

318
	platform_secondary_init(cpu);
319 320 321
	/* setup local core timer */
	bfin_local_timer_setup();

322 323
	local_irq_enable();

324 325
	bfin_setup_caches(cpu);

B
Bob Liu 已提交
326
	notify_cpu_starting(cpu);
327 328 329 330 331 332
	/*
	 * Calibrate loops per jiffy value.
	 * IRQs need to be enabled here - D-cache can be invalidated
	 * in timer irq handler, so core B can read correct jiffies.
	 */
	calibrate_delay();
333

S
Steven Miao 已提交
334 335
	/* We are done with local CPU inits, unblock the boot CPU. */
	set_cpu_online(cpu, true);
336
	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
337 338 339 340 341 342 343 344 345
}

void __init smp_prepare_boot_cpu(void)
{
}

void __init smp_prepare_cpus(unsigned int max_cpus)
{
	platform_prepare_cpus(max_cpus);
346
	bfin_ipi_init();
347 348
	platform_request_ipi(IRQ_SUPPLE_0, ipi_handler_int0);
	platform_request_ipi(IRQ_SUPPLE_1, ipi_handler_int1);
349 350 351 352 353 354 355 356
}

void __init smp_cpus_done(unsigned int max_cpus)
{
	unsigned long bogosum = 0;
	unsigned int cpu;

	for_each_online_cpu(cpu)
357
		bogosum += loops_per_jiffy;
358 359 360 361 362 363 364 365 366 367 368 369 370

	printk(KERN_INFO "SMP: Total of %d processors activated "
	       "(%lu.%02lu BogoMIPS).\n",
	       num_online_cpus(),
	       bogosum / (500000/HZ),
	       (bogosum / (5000/HZ)) % 100);
}

void smp_icache_flush_range_others(unsigned long start, unsigned long end)
{
	smp_flush_data.start = start;
	smp_flush_data.end = end;

371 372
	preempt_disable();
	if (smp_call_function(&ipi_flush_icache, &smp_flush_data, 1))
373
		printk(KERN_WARNING "SMP: failed to run I-cache flush request on other CPUs\n");
374
	preempt_enable();
375 376 377
}
EXPORT_SYMBOL_GPL(smp_icache_flush_range_others);

378
#ifdef __ARCH_SYNC_CORE_ICACHE
379
unsigned long icache_invld_count[NR_CPUS];
380 381 382 383
void resync_core_icache(void)
{
	unsigned int cpu = get_cpu();
	blackfin_invalidate_entire_icache();
384
	icache_invld_count[cpu]++;
385 386 387 388 389
	put_cpu();
}
EXPORT_SYMBOL(resync_core_icache);
#endif

390
#ifdef __ARCH_SYNC_CORE_DCACHE
391
unsigned long dcache_invld_count[NR_CPUS];
392 393 394 395 396 397
unsigned long barrier_mask __attribute__ ((__section__(".l2.bss")));

void resync_core_dcache(void)
{
	unsigned int cpu = get_cpu();
	blackfin_invalidate_entire_dcache();
398
	dcache_invld_count[cpu]++;
399 400 401 402
	put_cpu();
}
EXPORT_SYMBOL(resync_core_dcache);
#endif
403 404

#ifdef CONFIG_HOTPLUG_CPU
405
int __cpu_disable(void)
406 407 408 409 410 411 412 413 414 415
{
	unsigned int cpu = smp_processor_id();

	if (cpu == 0)
		return -EPERM;

	set_cpu_online(cpu, false);
	return 0;
}

416
int __cpu_die(unsigned int cpu)
417
{
418
	return cpu_wait_death(cpu, 5);
419 420 421 422
}

void cpu_die(void)
{
423
	(void)cpu_report_death();
424 425 426 427 428 429 430 431

	atomic_dec(&init_mm.mm_users);
	atomic_dec(&init_mm.mm_count);

	local_irq_disable();
	platform_cpu_die();
}
#endif