smp.c

来自「linux 内核源代码」· C语言 代码 · 共 1,454 行 · 第 1/3 页

C
1,454
字号
/* smp.c: Sparc64 SMP support. * * Copyright (C) 1997, 2007 David S. Miller (davem@davemloft.net) */#include <linux/module.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/mm.h>#include <linux/pagemap.h>#include <linux/threads.h>#include <linux/smp.h>#include <linux/interrupt.h>#include <linux/kernel_stat.h>#include <linux/delay.h>#include <linux/init.h>#include <linux/spinlock.h>#include <linux/fs.h>#include <linux/seq_file.h>#include <linux/cache.h>#include <linux/jiffies.h>#include <linux/profile.h>#include <linux/bootmem.h>#include <asm/head.h>#include <asm/ptrace.h>#include <asm/atomic.h>#include <asm/tlbflush.h>#include <asm/mmu_context.h>#include <asm/cpudata.h>#include <asm/hvtramp.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/irq_regs.h>#include <asm/page.h>#include <asm/pgtable.h>#include <asm/oplib.h>#include <asm/uaccess.h>#include <asm/timer.h>#include <asm/starfire.h>#include <asm/tlb.h>#include <asm/sections.h>#include <asm/prom.h>#include <asm/mdesc.h>#include <asm/ldc.h>#include <asm/hypervisor.h>extern void calibrate_delay(void);int sparc64_multi_core __read_mostly;cpumask_t cpu_possible_map __read_mostly = CPU_MASK_NONE;cpumask_t cpu_online_map __read_mostly = CPU_MASK_NONE;DEFINE_PER_CPU(cpumask_t, cpu_sibling_map) = CPU_MASK_NONE;cpumask_t cpu_core_map[NR_CPUS] __read_mostly =	{ [0 ... NR_CPUS-1] = CPU_MASK_NONE };EXPORT_SYMBOL(cpu_possible_map);EXPORT_SYMBOL(cpu_online_map);EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);EXPORT_SYMBOL(cpu_core_map);static cpumask_t smp_commenced_mask;void smp_info(struct seq_file *m){	int i;		seq_printf(m, "State:\n");	for_each_online_cpu(i)		seq_printf(m, "CPU%d:\t\tonline\n", i);}void smp_bogo(struct seq_file *m){	int i;		for_each_online_cpu(i)		seq_printf(m,			   "Cpu%dClkTck\t: %016lx\n",			   i, cpu_data(i).clock_tick);}static __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_lock);extern void setup_sparc64_timer(void);static volatile unsigned long callin_flag = 0;void __devinit smp_callin(void){	int cpuid = hard_smp_processor_id();	__local_per_cpu_offset = __per_cpu_offset(cpuid);	if (tlb_type == hypervisor)		sun4v_ktsb_register();	__flush_tlb_all();	setup_sparc64_timer();	if (cheetah_pcache_forced_on)		cheetah_enable_pcache();	local_irq_enable();	callin_flag = 1;	__asm__ __volatile__("membar #Sync\n\t"			     "flush  %%g6" : : : "memory");	/* Clear this or we will die instantly when we	 * schedule back to this idler...	 */	current_thread_info()->new_child = 0;	/* Attach to the address space of init_task. */	atomic_inc(&init_mm.mm_count);	current->active_mm = &init_mm;	while (!cpu_isset(cpuid, smp_commenced_mask))		rmb();	spin_lock(&call_lock);	cpu_set(cpuid, cpu_online_map);	spin_unlock(&call_lock);	/* idle thread is expected to have preempt disabled */	preempt_disable();}void cpu_panic(void){	printk("CPU[%d]: Returns from cpu_idle!\n", smp_processor_id());	panic("SMP bolixed\n");}/* This tick register synchronization scheme is taken entirely from * the ia64 port, see arch/ia64/kernel/smpboot.c for details and credit. * * The only change I've made is to rework it so that the master * initiates the synchonization instead of the slave. -DaveM */#define MASTER	0#define SLAVE	(SMP_CACHE_BYTES/sizeof(unsigned long))#define NUM_ROUNDS	64	/* magic value */#define NUM_ITERS	5	/* likewise */static DEFINE_SPINLOCK(itc_sync_lock);static unsigned long go[SLAVE + 1];#define DEBUG_TICK_SYNC	0static inline long get_delta (long *rt, long *master){	unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;	unsigned long tcenter, t0, t1, tm;	unsigned long i;	for (i = 0; i < NUM_ITERS; i++) {		t0 = tick_ops->get_tick();		go[MASTER] = 1;		membar_storeload();		while (!(tm = go[SLAVE]))			rmb();		go[SLAVE] = 0;		wmb();		t1 = tick_ops->get_tick();		if (t1 - t0 < best_t1 - best_t0)			best_t0 = t0, best_t1 = t1, best_tm = tm;	}	*rt = best_t1 - best_t0;	*master = best_tm - best_t0;	/* average best_t0 and best_t1 without overflow: */	tcenter = (best_t0/2 + best_t1/2);	if (best_t0 % 2 + best_t1 % 2 == 2)		tcenter++;	return tcenter - best_tm;}void smp_synchronize_tick_client(void){	long i, delta, adj, adjust_latency = 0, done = 0;	unsigned long flags, rt, master_time_stamp, bound;#if DEBUG_TICK_SYNC	struct {		long rt;	/* roundtrip time */		long master;	/* master's timestamp */		long diff;	/* difference between midpoint and master's timestamp */		long lat;	/* estimate of itc adjustment latency */	} t[NUM_ROUNDS];#endif	go[MASTER] = 1;	while (go[MASTER])		rmb();	local_irq_save(flags);	{		for (i = 0; i < NUM_ROUNDS; i++) {			delta = get_delta(&rt, &master_time_stamp);			if (delta == 0) {				done = 1;	/* let's lock on to this... */				bound = rt;			}			if (!done) {				if (i > 0) {					adjust_latency += -delta;					adj = -delta + adjust_latency/4;				} else					adj = -delta;				tick_ops->add_tick(adj);			}#if DEBUG_TICK_SYNC			t[i].rt = rt;			t[i].master = master_time_stamp;			t[i].diff = delta;			t[i].lat = adjust_latency/4;#endif		}	}	local_irq_restore(flags);#if DEBUG_TICK_SYNC	for (i = 0; i < NUM_ROUNDS; i++)		printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n",		       t[i].rt, t[i].master, t[i].diff, t[i].lat);#endif	printk(KERN_INFO "CPU %d: synchronized TICK with master CPU "	       "(last diff %ld cycles, maxerr %lu cycles)\n",	       smp_processor_id(), delta, rt);}static void smp_start_sync_tick_client(int cpu);static void smp_synchronize_one_tick(int cpu){	unsigned long flags, i;	go[MASTER] = 0;	smp_start_sync_tick_client(cpu);	/* wait for client to be ready */	while (!go[MASTER])		rmb();	/* now let the client proceed into his loop */	go[MASTER] = 0;	membar_storeload();	spin_lock_irqsave(&itc_sync_lock, flags);	{		for (i = 0; i < NUM_ROUNDS*NUM_ITERS; i++) {			while (!go[MASTER])				rmb();			go[MASTER] = 0;			wmb();			go[SLAVE] = tick_ops->get_tick();			membar_storeload();		}	}	spin_unlock_irqrestore(&itc_sync_lock, flags);}#if defined(CONFIG_SUN_LDOMS) && defined(CONFIG_HOTPLUG_CPU)/* XXX Put this in some common place. XXX */static unsigned long kimage_addr_to_ra(void *p){	unsigned long val = (unsigned long) p;	return kern_base + (val - KERNBASE);}static void ldom_startcpu_cpuid(unsigned int cpu, unsigned long thread_reg){	extern unsigned long sparc64_ttable_tl0;	extern unsigned long kern_locked_tte_data;	extern int bigkernel;	struct hvtramp_descr *hdesc;	unsigned long trampoline_ra;	struct trap_per_cpu *tb;	u64 tte_vaddr, tte_data;	unsigned long hv_err;	hdesc = kzalloc(sizeof(*hdesc), GFP_KERNEL);	if (!hdesc) {		printk(KERN_ERR "ldom_startcpu_cpuid: Cannot allocate "		       "hvtramp_descr.\n");		return;	}	hdesc->cpu = cpu;	hdesc->num_mappings = (bigkernel ? 2 : 1);	tb = &trap_block[cpu];	tb->hdesc = hdesc;	hdesc->fault_info_va = (unsigned long) &tb->fault_info;	hdesc->fault_info_pa = kimage_addr_to_ra(&tb->fault_info);	hdesc->thread_reg = thread_reg;	tte_vaddr = (unsigned long) KERNBASE;	tte_data = kern_locked_tte_data;	hdesc->maps[0].vaddr = tte_vaddr;	hdesc->maps[0].tte   = tte_data;	if (bigkernel) {		tte_vaddr += 0x400000;		tte_data  += 0x400000;		hdesc->maps[1].vaddr = tte_vaddr;		hdesc->maps[1].tte   = tte_data;	}	trampoline_ra = kimage_addr_to_ra(hv_cpu_startup);	hv_err = sun4v_cpu_start(cpu, trampoline_ra,				 kimage_addr_to_ra(&sparc64_ttable_tl0),				 __pa(hdesc));	if (hv_err)		printk(KERN_ERR "ldom_startcpu_cpuid: sun4v_cpu_start() "		       "gives error %lu\n", hv_err);}#endifextern unsigned long sparc64_cpu_startup;/* The OBP cpu startup callback truncates the 3rd arg cookie to * 32-bits (I think) so to be safe we have it read the pointer * contained here so we work on >4GB machines. -DaveM */static struct thread_info *cpu_new_thread = NULL;static int __devinit smp_boot_one_cpu(unsigned int cpu){	struct trap_per_cpu *tb = &trap_block[cpu];	unsigned long entry =		(unsigned long)(&sparc64_cpu_startup);	unsigned long cookie =		(unsigned long)(&cpu_new_thread);	struct task_struct *p;	int timeout, ret;	p = fork_idle(cpu);	if (IS_ERR(p))		return PTR_ERR(p);	callin_flag = 0;	cpu_new_thread = task_thread_info(p);	if (tlb_type == hypervisor) {#if defined(CONFIG_SUN_LDOMS) && defined(CONFIG_HOTPLUG_CPU)		if (ldom_domaining_enabled)			ldom_startcpu_cpuid(cpu,					    (unsigned long) cpu_new_thread);		else#endif			prom_startcpu_cpuid(cpu, entry, cookie);	} else {		struct device_node *dp = of_find_node_by_cpuid(cpu);		prom_startcpu(dp->node, entry, cookie);	}	for (timeout = 0; timeout < 50000; timeout++) {		if (callin_flag)			break;		udelay(100);	}	if (callin_flag) {		ret = 0;	} else {		printk("Processor %d is stuck.\n", cpu);		ret = -ENODEV;	}	cpu_new_thread = NULL;	if (tb->hdesc) {		kfree(tb->hdesc);		tb->hdesc = NULL;	}	return ret;}static void spitfire_xcall_helper(u64 data0, u64 data1, u64 data2, u64 pstate, unsigned long cpu){	u64 result, target;	int stuck, tmp;	if (this_is_starfire) {		/* map to real upaid */		cpu = (((cpu & 0x3c) << 1) |			((cpu & 0x40) >> 4) |			(cpu & 0x3));	}	target = (cpu << 14) | 0x70;again:	/* Ok, this is the real Spitfire Errata #54.	 * One must read back from a UDB internal register	 * after writes to the UDB interrupt dispatch, but	 * before the membar Sync for that write.	 * So we use the high UDB control register (ASI 0x7f,	 * ADDR 0x20) for the dummy read. -DaveM	 */	tmp = 0x40;	__asm__ __volatile__(	"wrpr	%1, %2, %%pstate\n\t"	"stxa	%4, [%0] %3\n\t"	"stxa	%5, [%0+%8] %3\n\t"	"add	%0, %8, %0\n\t"	"stxa	%6, [%0+%8] %3\n\t"	"membar	#Sync\n\t"	"stxa	%%g0, [%7] %3\n\t"	"membar	#Sync\n\t"	"mov	0x20, %%g1\n\t"	"ldxa	[%%g1] 0x7f, %%g0\n\t"	"membar	#Sync"	: "=r" (tmp)	: "r" (pstate), "i" (PSTATE_IE), "i" (ASI_INTR_W),	  "r" (data0), "r" (data1), "r" (data2), "r" (target),	  "r" (0x10), "0" (tmp)        : "g1");	/* NOTE: PSTATE_IE is still clear. */	stuck = 100000;	do {		__asm__ __volatile__("ldxa [%%g0] %1, %0"			: "=r" (result)			: "i" (ASI_INTR_DISPATCH_STAT));		if (result == 0) {			__asm__ __volatile__("wrpr %0, 0x0, %%pstate"					     : : "r" (pstate));			return;		}		stuck -= 1;		if (stuck == 0)			break;	} while (result & 0x1);	__asm__ __volatile__("wrpr %0, 0x0, %%pstate"			     : : "r" (pstate));	if (stuck == 0) {		printk("CPU[%d]: mondo stuckage result[%016lx]\n",		       smp_processor_id(), result);	} else {		udelay(2);		goto again;	}}static inline void spitfire_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask){	u64 pstate;	int i;	__asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));	for_each_cpu_mask(i, mask)		spitfire_xcall_helper(data0, data1, data2, pstate, i);}/* Cheetah now allows to send the whole 64-bytes of data in the interrupt * packet, but we have no use for that.  However we do take advantage of * the new pipelining feature (ie. dispatch to multiple cpus simultaneously). */static void cheetah_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask){	u64 pstate, ver, busy_mask;	int nack_busy_id, is_jbus, need_more;	if (cpus_empty(mask))		return;	/* Unfortunately, someone at Sun had the brilliant idea to make the

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?