smp.c 11.0 KB
Newer Older
1 2 3 4
/*
 * Author: Andy Fleming <afleming@freescale.com>
 * 	   Kumar Gala <galak@kernel.crashing.org>
 *
5
 * Copyright 2006-2008, 2011-2012, 2015 Freescale Semiconductor Inc.
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 */

#include <linux/stddef.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/of.h>
18
#include <linux/kexec.h>
19
#include <linux/highmem.h>
20
#include <linux/cpu.h>
21
#include <linux/fsl/guts.h>
22 23 24 25 26 27

#include <asm/machdep.h>
#include <asm/pgtable.h>
#include <asm/page.h>
#include <asm/mpic.h>
#include <asm/cacheflush.h>
28
#include <asm/dbell.h>
29
#include <asm/code-patching.h>
30
#include <asm/cputhreads.h>
31
#include <asm/fsl_pm.h>
32 33

#include <sysdev/fsl_soc.h>
34
#include <sysdev/mpic.h>
35
#include "smp.h"
36

37 38 39 40 41 42 43 44
struct epapr_spin_table {
	u32	addr_h;
	u32	addr_l;
	u32	r3_h;
	u32	r3_l;
	u32	reserved;
	u32	pir;
};
45

46
#ifdef CONFIG_HOTPLUG_CPU
47 48 49 50 51 52 53 54 55
static u64 timebase;
static int tb_req;
static int tb_valid;

static void mpc85xx_give_timebase(void)
{
	unsigned long flags;

	local_irq_save(flags);
56
	hard_irq_disable();
57 58 59 60 61

	while (!tb_req)
		barrier();
	tb_req = 0;

62
	qoriq_pm_ops->freeze_time_base(true);
63 64 65 66 67 68 69 70 71 72 73 74 75 76
#ifdef CONFIG_PPC64
	/*
	 * e5500/e6500 have a workaround for erratum A-006958 in place
	 * that will reread the timebase until TBL is non-zero.
	 * That would be a bad thing when the timebase is frozen.
	 *
	 * Thus, we read it manually, and instead of checking that
	 * TBL is non-zero, we ensure that TB does not change.  We don't
	 * do that for the main mftb implementation, because it requires
	 * a scratch register
	 */
	{
		u64 prev;

77 78
		asm volatile("mfspr %0, %1" : "=r" (timebase) :
			     "i" (SPRN_TBRL));
79 80 81

		do {
			prev = timebase;
82 83
			asm volatile("mfspr %0, %1" : "=r" (timebase) :
				     "i" (SPRN_TBRL));
84 85 86
		} while (prev != timebase);
	}
#else
87
	timebase = get_tb();
88
#endif
89 90 91 92 93 94
	mb();
	tb_valid = 1;

	while (tb_valid)
		barrier();

95
	qoriq_pm_ops->freeze_time_base(false);
96 97 98 99 100 101 102 103 104

	local_irq_restore(flags);
}

static void mpc85xx_take_timebase(void)
{
	unsigned long flags;

	local_irq_save(flags);
105
	hard_irq_disable();
106 107 108 109 110 111 112 113 114 115 116 117

	tb_req = 1;
	while (!tb_valid)
		barrier();

	set_tb(timebase >> 32, timebase & 0xffffffff);
	isync();
	tb_valid = 0;

	local_irq_restore(flags);
}

118
static void smp_85xx_mach_cpu_die(void)
119 120 121 122
{
	unsigned int cpu = smp_processor_id();

	local_irq_disable();
123 124 125 126
	hard_irq_disable();
	/* mask all irqs to prevent cpu wakeup */
	qoriq_pm_ops->irq_mask(cpu);

127 128 129
	idle_task_exit();

	mtspr(SPRN_TCR, 0);
130
	mtspr(SPRN_TSR, mfspr(SPRN_TSR));
131

132
	generic_set_cpu_dead(cpu);
133

134
	cur_cpu_spec->cpu_down_flush();
135

136
	qoriq_pm_ops->cpu_die(cpu);
137 138 139 140

	while (1)
		;
}
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

static void qoriq_cpu_kill(unsigned int cpu)
{
	int i;

	for (i = 0; i < 500; i++) {
		if (is_cpu_dead(cpu)) {
#ifdef CONFIG_PPC64
			paca[cpu].cpu_start = 0;
#endif
			return;
		}
		msleep(20);
	}
	pr_err("CPU%d didn't die...\n", cpu);
}
157 158
#endif

159 160 161 162 163 164 165
/*
 * To keep it compatible with old boot program which uses
 * cache-inhibit spin table, we need to flush the cache
 * before accessing spin table to invalidate any staled data.
 * We also need to flush the cache after writing to spin
 * table to push data out.
 */
166 167 168 169 170 171 172 173 174 175 176 177 178
static inline void flush_spin_table(void *spin_table)
{
	flush_dcache_range((ulong)spin_table,
		(ulong)spin_table + sizeof(struct epapr_spin_table));
}

static inline u32 read_spin_table_addr_l(void *spin_table)
{
	flush_dcache_range((ulong)spin_table,
		(ulong)spin_table + sizeof(struct epapr_spin_table));
	return in_be32(&((struct epapr_spin_table *)spin_table)->addr_l);
}

179 180 181 182
#ifdef CONFIG_PPC64
static void wake_hw_thread(void *info)
{
	void fsl_secondary_thread_init(void);
183
	unsigned long imsr, inia;
184 185
	int nr = *(const int *)info;

186 187 188 189 190 191 192 193 194 195 196 197 198
	imsr = MSR_KERNEL;
	inia = *(unsigned long *)fsl_secondary_thread_init;

	if (cpu_thread_in_core(nr) == 0) {
		/* For when we boot on a secondary thread with kdump */
		mttmr(TMRN_IMSR0, imsr);
		mttmr(TMRN_INIA0, inia);
		mtspr(SPRN_TENS, TEN_THREAD(0));
	} else {
		mttmr(TMRN_IMSR1, imsr);
		mttmr(TMRN_INIA1, inia);
		mtspr(SPRN_TENS, TEN_THREAD(1));
	}
199 200 201 202 203

	smp_generic_kick_cpu(nr);
}
#endif

204
static int smp_85xx_start_cpu(int cpu)
205
{
206
	int ret = 0;
207
	struct device_node *np;
208 209
	const u64 *cpu_rel_addr;
	unsigned long flags;
210
	int ioremappable;
211 212
	int hw_cpu = get_hard_smp_processor_id(cpu);
	struct epapr_spin_table __iomem *spin_table;
213

214
	np = of_get_cpu_node(cpu, NULL);
215
	cpu_rel_addr = of_get_property(np, "cpu-release-addr", NULL);
216 217
	if (!cpu_rel_addr) {
		pr_err("No cpu-release-addr for cpu %d\n", cpu);
218
		return -ENOENT;
219 220
	}

221 222 223 224 225 226 227 228
	/*
	 * A secondary core could be in a spinloop in the bootpage
	 * (0xfffff000), somewhere in highmem, or somewhere in lowmem.
	 * The bootpage and highmem can be accessed via ioremap(), but
	 * we need to directly access the spinloop if its in lowmem.
	 */
	ioremappable = *cpu_rel_addr > virt_to_phys(high_memory);

229
	/* Map the spin table */
230
	if (ioremappable)
231 232
		spin_table = ioremap_prot(*cpu_rel_addr,
			sizeof(struct epapr_spin_table), _PAGE_COHERENT);
233
	else
234
		spin_table = phys_to_virt(*cpu_rel_addr);
235

236
	local_irq_save(flags);
237
	hard_irq_disable();
238

239 240
	if (qoriq_pm_ops)
		qoriq_pm_ops->cpu_up_prepare(cpu);
241

242 243
	/* if cpu is not spinning, reset it */
	if (read_spin_table_addr_l(spin_table) != 1) {
244 245 246 247
		/*
		 * We don't set the BPTR register here since it already points
		 * to the boot page properly.
		 */
248
		mpic_reset_core(cpu);
249

250 251 252 253 254 255 256 257
		/*
		 * wait until core is ready...
		 * We need to invalidate the stale data, in case the boot
		 * loader uses a cache-inhibited spin table.
		 */
		if (!spin_event_timeout(
				read_spin_table_addr_l(spin_table) == 1,
				10000, 100)) {
258 259 260 261
			pr_err("timeout waiting for cpu %d to reset\n",
				hw_cpu);
			ret = -EAGAIN;
			goto err;
262 263
		}
	}
264

265
	flush_spin_table(spin_table);
266
	out_be32(&spin_table->pir, hw_cpu);
267
#ifdef CONFIG_PPC64
268
	out_be64((u64 *)(&spin_table->addr_h),
269
		__pa(ppc_function_entry(generic_secondary_smp_init)));
270 271
#else
	out_be32(&spin_table->addr_l, __pa(__early_start));
272
#endif
273 274
	flush_spin_table(spin_table);
err:
275 276
	local_irq_restore(flags);

277
	if (ioremappable)
278
		iounmap(spin_table);
279

280
	return ret;
281 282
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
static int smp_85xx_kick_cpu(int nr)
{
	int ret = 0;
#ifdef CONFIG_PPC64
	int primary = nr;
#endif

	WARN_ON(nr < 0 || nr >= num_possible_cpus());

	pr_debug("kick CPU #%d\n", nr);

#ifdef CONFIG_PPC64
	/* Threads don't use the spin table */
	if (cpu_thread_in_core(nr) != 0) {
		int primary = cpu_first_thread_sibling(nr);

		if (WARN_ON_ONCE(!cpu_has_feature(CPU_FTR_SMT)))
			return -ENOENT;

		if (cpu_thread_in_core(nr) != 1) {
			pr_err("%s: cpu %d: invalid hw thread %d\n",
			       __func__, nr, cpu_thread_in_core(nr));
			return -ENOENT;
		}

		if (!cpu_online(primary)) {
			pr_err("%s: cpu %d: primary %d not online\n",
			       __func__, nr, primary);
			return -ENOENT;
		}

		smp_call_function_single(primary, wake_hw_thread, &nr, 0);
		return 0;
	}

	ret = smp_85xx_start_cpu(primary);
	if (ret)
		return ret;

	paca[nr].cpu_start = 1;
	generic_set_cpu_up(nr);

	return ret;
#else
	ret = smp_85xx_start_cpu(nr);
	if (ret)
		return ret;

	generic_set_cpu_up(nr);

	return ret;
#endif
}

337 338
struct smp_ops_t smp_85xx_ops = {
	.kick_cpu = smp_85xx_kick_cpu,
339
	.cpu_bootable = smp_generic_cpu_bootable,
340 341 342 343
#ifdef CONFIG_HOTPLUG_CPU
	.cpu_disable	= generic_cpu_disable,
	.cpu_die	= generic_cpu_die,
#endif
344
#if defined(CONFIG_KEXEC) && !defined(CONFIG_PPC64)
345 346 347
	.give_timebase	= smp_generic_give_timebase,
	.take_timebase	= smp_generic_take_timebase,
#endif
348 349
};

350
#ifdef CONFIG_KEXEC
351
#ifdef CONFIG_PPC32
352
atomic_t kexec_down_cpus = ATOMIC_INIT(0);
353 354 355

void mpc85xx_smp_kexec_cpu_down(int crash_shutdown, int secondary)
{
356
	local_irq_disable();
357

358
	if (secondary) {
359
		cur_cpu_spec->cpu_down_flush();
360 361
		atomic_inc(&kexec_down_cpus);
		/* loop forever */
362 363 364 365 366 367 368 369 370
		while (1);
	}
}

static void mpc85xx_smp_kexec_down(void *arg)
{
	if (ppc_md.kexec_cpu_down)
		ppc_md.kexec_cpu_down(0,1);
}
371 372 373
#else
void mpc85xx_smp_kexec_cpu_down(int crash_shutdown, int secondary)
{
374 375 376 377 378 379 380 381
	int cpu = smp_processor_id();
	int sibling = cpu_last_thread_sibling(cpu);
	bool notified = false;
	int disable_cpu;
	int disable_threadbit = 0;
	long start = mftb();
	long now;

382 383 384
	local_irq_disable();
	hard_irq_disable();
	mpic_teardown_this_cpu(secondary);
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

	if (cpu == crashing_cpu && cpu_thread_in_core(cpu) != 0) {
		/*
		 * We enter the crash kernel on whatever cpu crashed,
		 * even if it's a secondary thread.  If that's the case,
		 * disable the corresponding primary thread.
		 */
		disable_threadbit = 1;
		disable_cpu = cpu_first_thread_sibling(cpu);
	} else if (sibling != crashing_cpu &&
		   cpu_thread_in_core(cpu) == 0 &&
		   cpu_thread_in_core(sibling) != 0) {
		disable_threadbit = 2;
		disable_cpu = sibling;
	}

	if (disable_threadbit) {
		while (paca[disable_cpu].kexec_state < KEXEC_STATE_REAL_MODE) {
			barrier();
			now = mftb();
			if (!notified && now - start > 1000000) {
				pr_info("%s/%d: waiting for cpu %d to enter KEXEC_STATE_REAL_MODE (%d)\n",
					__func__, smp_processor_id(),
					disable_cpu,
					paca[disable_cpu].kexec_state);
				notified = true;
			}
		}

		if (notified) {
			pr_info("%s: cpu %d done waiting\n",
				__func__, disable_cpu);
		}

		mtspr(SPRN_TENC, disable_threadbit);
		while (mfspr(SPRN_TENSR) & disable_threadbit)
			cpu_relax();
	}
423 424
}
#endif
425 426 427

static void mpc85xx_smp_machine_kexec(struct kimage *image)
{
428
#ifdef CONFIG_PPC32
429 430
	int timeout = INT_MAX;
	int i, num_cpus = num_present_cpus();
431

432 433
	if (image->type == KEXEC_TYPE_DEFAULT)
		smp_call_function(mpc85xx_smp_kexec_down, NULL, 0);
434

435
	while ( (atomic_read(&kexec_down_cpus) != (num_cpus - 1)) &&
436 437 438 439 440 441 442 443
		( timeout > 0 ) )
	{
		timeout--;
	}

	if ( !timeout )
		printk(KERN_ERR "Unable to bring down secondary cpu(s)");

444
	for_each_online_cpu(i)
445 446 447 448
	{
		if ( i == smp_processor_id() ) continue;
		mpic_reset_core(i);
	}
449
#endif
450 451 452 453 454

	default_machine_kexec(image);
}
#endif /* CONFIG_KEXEC */

455
static void smp_85xx_basic_setup(int cpu_nr)
456 457 458 459 460
{
	if (cpu_has_feature(CPU_FTR_DBELL))
		doorbell_setup_this_cpu();
}

461 462 463 464 465 466
static void smp_85xx_setup_cpu(int cpu_nr)
{
	mpic_setup_this_cpu();
	smp_85xx_basic_setup(cpu_nr);
}

467 468 469 470
void __init mpc85xx_smp_init(void)
{
	struct device_node *np;

471

472 473 474
	np = of_find_node_by_type(NULL, "open-pic");
	if (np) {
		smp_85xx_ops.probe = smp_mpic_probe;
475
		smp_85xx_ops.setup_cpu = smp_85xx_setup_cpu;
476
		smp_85xx_ops.message_pass = smp_mpic_message_pass;
477 478
	} else
		smp_85xx_ops.setup_cpu = smp_85xx_basic_setup;
479

480
	if (cpu_has_feature(CPU_FTR_DBELL)) {
481 482 483 484
		/*
		 * If left NULL, .message_pass defaults to
		 * smp_muxed_ipi_message_pass
		 */
485
		smp_85xx_ops.message_pass = NULL;
486
		smp_85xx_ops.cause_ipi = doorbell_cause_ipi;
487
		smp_85xx_ops.probe = NULL;
488
	}
489

490
#ifdef CONFIG_HOTPLUG_CPU
491 492 493 494
#ifdef CONFIG_FSL_CORENET_RCPM
	fsl_rcpm_init();
#endif

495 496 497 498
#ifdef CONFIG_FSL_PMC
	mpc85xx_setup_pmc();
#endif
	if (qoriq_pm_ops) {
499 500
		smp_85xx_ops.give_timebase = mpc85xx_give_timebase;
		smp_85xx_ops.take_timebase = mpc85xx_take_timebase;
501
		ppc_md.cpu_die = smp_85xx_mach_cpu_die;
502
		smp_85xx_ops.cpu_die = qoriq_cpu_kill;
503
	}
504
#endif
505
	smp_ops = &smp_85xx_ops;
506 507 508 509 510

#ifdef CONFIG_KEXEC
	ppc_md.kexec_cpu_down = mpc85xx_smp_kexec_cpu_down;
	ppc_md.machine_kexec = mpc85xx_smp_machine_kexec;
#endif
511
}