⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smp.c

📁 ARM 嵌入式 系统 设计与实例开发 实验教材 二源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *	linux/arch/alpha/kernel/smp.c * *      2001-07-09 Phil Ezolt (Phillip.Ezolt@compaq.com) *            Renamed modified smp_call_function to smp_call_function_on_cpu() *            Created an function that conforms to the old calling convention *            of smp_call_function(). * *            This is helpful for DCPI. * */#include <linux/errno.h>#include <linux/kernel.h>#include <linux/kernel_stat.h>#include <linux/sched.h>#include <linux/mm.h>#include <linux/threads.h>#include <linux/smp.h>#include <linux/smp_lock.h>#include <linux/interrupt.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/spinlock.h>#include <linux/irq.h>#include <linux/cache.h>#include <asm/hwrpb.h>#include <asm/ptrace.h>#include <asm/atomic.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/bitops.h>#include <asm/pgtable.h>#include <asm/pgalloc.h>#include <asm/hardirq.h>#include <asm/softirq.h>#include <asm/mmu_context.h>#define __KERNEL_SYSCALLS__#include <asm/unistd.h>#include "proto.h"#include "irq_impl.h"#define DEBUG_SMP 0#if DEBUG_SMP#define DBGS(args)	printk args#else#define DBGS(args)#endif/* A collection of per-processor data.  */struct cpuinfo_alpha cpu_data[NR_CPUS];/* A collection of single bit ipi messages.  */static struct {	unsigned long bits ____cacheline_aligned;} ipi_data[NR_CPUS] __cacheline_aligned;enum ipi_message_type {	IPI_RESCHEDULE,	IPI_CALL_FUNC,	IPI_CPU_STOP,};spinlock_t kernel_flag __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;/* Set to a secondary's cpuid when it comes online.  */static unsigned long smp_secondary_alive;/* Which cpus ids came online.  */unsigned long cpu_present_mask;/* cpus reported in the hwrpb */static unsigned long hwrpb_cpu_present_mask __initdata = 0;static int max_cpus = -1;	/* Command-line limitation.  */int smp_num_probed;		/* Internal processor count */int smp_num_cpus = 1;		/* Number that came online.  */int smp_threads_ready;		/* True once the per process idle is forked. */cycles_t cacheflush_time;int __cpu_number_map[NR_CPUS];int __cpu_logical_map[NR_CPUS];extern void calibrate_delay(void);extern asmlinkage void entInt(void);static int __init nosmp(char *str){	max_cpus = 0;	return 1;}__setup("nosmp", nosmp);static int __init maxcpus(char *str){	get_option(&str, &max_cpus);	return 1;}__setup("maxcpus", maxcpus);/* * Called by both boot and secondaries to move global data into *  per-processor storage. */static inline void __initsmp_store_cpu_info(int cpuid){	cpu_data[cpuid].loops_per_jiffy = loops_per_jiffy;	cpu_data[cpuid].last_asn = ASN_FIRST_VERSION;	cpu_data[cpuid].need_new_asn = 0;	cpu_data[cpuid].asn_lock = 0;	local_irq_count(cpuid) = 0;	local_bh_count(cpuid) = 0;}/* * Ideally sets up per-cpu profiling hooks.  Doesn't do much now... */static inline void __initsmp_setup_percpu_timer(int cpuid){	cpu_data[cpuid].prof_counter = 1;	cpu_data[cpuid].prof_multiplier = 1;}static void __initwait_boot_cpu_to_stop(int cpuid){	long stop = jiffies + 10*HZ;	while (time_before(jiffies, stop)) {	        if (!smp_secondary_alive)			return;		barrier();	}	printk("wait_boot_cpu_to_stop: FAILED on CPU %d, hanging now\n", cpuid);	for (;;)		barrier();}/* * Where secondaries begin a life of C. */void __initsmp_callin(void){	int cpuid = hard_smp_processor_id();	if (current != init_tasks[cpu_number_map(cpuid)]) {		printk("BUG: smp_calling: cpu %d current %p init_tasks[cpu_number_map(cpuid)] %p\n",		       cpuid, current, init_tasks[cpu_number_map(cpuid)]);	}	DBGS(("CALLIN %d state 0x%lx\n", cpuid, current->state));	/* Turn on machine checks.  */	wrmces(7);	/* Set trap vectors.  */	trap_init();	/* Set interrupt vector.  */	wrent(entInt, 0);	/* Get our local ticker going. */	smp_setup_percpu_timer(cpuid);	/* Must have completely accurate bogos.  */	__sti();	/*	 * Wait boot CPU to stop with irq enabled before	 * running calibrate_delay().	 */	wait_boot_cpu_to_stop(cpuid);	mb();	calibrate_delay();	smp_store_cpu_info(cpuid);	/*	 * Allow master to continue only after we written	 * the loops_per_jiffy.	 */	wmb();	smp_secondary_alive = 1;	/* Wait for the go code.  */	while (!smp_threads_ready)		barrier();	DBGS(("smp_callin: commencing CPU %d current %p\n",	      cpuid, current));	/* Setup the scheduler for this processor.  */	init_idle();	/* ??? This should be in init_idle.  */	atomic_inc(&init_mm.mm_count);	current->active_mm = &init_mm;	/* Do nothing.  */	cpu_idle();}/* * Rough estimation for SMP scheduling, this is the number of cycles it * takes for a fully memory-limited process to flush the SMP-local cache. * * We are not told how much cache there is, so we have to guess. */static void __initsmp_tune_scheduling (int cpuid){	struct percpu_struct *cpu;	unsigned long on_chip_cache;	unsigned long freq;	cpu = (struct percpu_struct*)((char*)hwrpb + hwrpb->processor_offset				      + cpuid * hwrpb->processor_size);	switch (cpu->type)	{	case EV45_CPU:		on_chip_cache = 16 + 16;		break;	case EV5_CPU:	case EV56_CPU:		on_chip_cache = 8 + 8 + 96;		break;	case PCA56_CPU:		on_chip_cache = 16 + 8;		break;	case EV6_CPU:	case EV67_CPU:		on_chip_cache = 64 + 64;		break;	default:		on_chip_cache = 8 + 8;		break;	}	freq = hwrpb->cycle_freq ? : est_cycle_freq;#if 0	/* Magic estimation stolen from x86 port.  */	cacheflush_time = freq / 1024L * on_chip_cache / 5000L;        printk("Using heuristic of %d cycles.\n",               cacheflush_time);#else	/* Magic value to force potential preemption of other CPUs.  */	cacheflush_time = INT_MAX;        printk("Using heuristic of %d cycles.\n",               cacheflush_time);#endif}/* * Send a message to a secondary's console.  "START" is one such * interesting message.  ;-) */static voidsend_secondary_console_msg(char *str, int cpuid){	struct percpu_struct *cpu;	register char *cp1, *cp2;	unsigned long cpumask;	size_t len;	long timeout;	cpu = (struct percpu_struct *)		((char*)hwrpb		 + hwrpb->processor_offset		 + cpuid * hwrpb->processor_size);	cpumask = (1UL << cpuid);	if (hwrpb->txrdy & cpumask)		goto delay1;	ready1:	cp2 = str;	len = strlen(cp2);	*(unsigned int *)&cpu->ipc_buffer[0] = len;	cp1 = (char *) &cpu->ipc_buffer[1];	memcpy(cp1, cp2, len);	/* atomic test and set */	wmb();	set_bit(cpuid, &hwrpb->rxrdy);	if (hwrpb->txrdy & cpumask)		goto delay2;	ready2:	return;delay1:	/* Wait 10 seconds.  Note that jiffies aren't ticking yet.  */	for (timeout = 1000000; timeout > 0; --timeout) {		if (!(hwrpb->txrdy & cpumask))			goto ready1;		udelay(10);		barrier();	}	goto timeout;delay2:	/* Wait 10 seconds.  */	for (timeout = 1000000; timeout > 0; --timeout) {		if (!(hwrpb->txrdy & cpumask))			goto ready2;		udelay(10);		barrier();	}	goto timeout;timeout:	printk("Processor %x not ready\n", cpuid);	return;}/* * A secondary console wants to send a message.  Receive it. */static voidrecv_secondary_console_msg(void){	int mycpu, i, cnt;	unsigned long txrdy = hwrpb->txrdy;	char *cp1, *cp2, buf[80];	struct percpu_struct *cpu;	DBGS(("recv_secondary_console_msg: TXRDY 0x%lx.\n", txrdy));	mycpu = hard_smp_processor_id();	for (i = 0; i < NR_CPUS; i++) {		if (!(txrdy & (1UL << i)))			continue;		DBGS(("recv_secondary_console_msg: "		      "TXRDY contains CPU %d.\n", i));		cpu = (struct percpu_struct *)		  ((char*)hwrpb		   + hwrpb->processor_offset		   + i * hwrpb->processor_size); 		DBGS(("recv_secondary_console_msg: on %d from %d"		      " HALT_REASON 0x%lx FLAGS 0x%lx\n",		      mycpu, i, cpu->halt_reason, cpu->flags));		cnt = cpu->ipc_buffer[0] >> 32;		if (cnt <= 0 || cnt >= 80)			strcpy(buf, "<<< BOGUS MSG >>>");		else {			cp1 = (char *) &cpu->ipc_buffer[11];			cp2 = buf;			strcpy(cp2, cp1);						while ((cp2 = strchr(cp2, '\r')) != 0) {				*cp2 = ' ';				if (cp2[1] == '\n')					cp2[1] = ' ';			}		}		DBGS((KERN_INFO "recv_secondary_console_msg: on %d "		      "message is '%s'\n", mycpu, buf));	}	hwrpb->txrdy = 0;}/* * Convince the console to have a secondary cpu begin execution. */static int __initsecondary_cpu_start(int cpuid, struct task_struct *idle){	struct percpu_struct *cpu;	struct pcb_struct *hwpcb;	long timeout;	  	cpu = (struct percpu_struct *)		((char*)hwrpb		 + hwrpb->processor_offset		 + cpuid * hwrpb->processor_size);	hwpcb = (struct pcb_struct *) cpu->hwpcb;	/* Initialize the CPU's HWPCB to something just good enough for	   us to get started.  Immediately after starting, we'll swpctx	   to the target idle task's ptb.  Reuse the stack in the mean	   time.  Precalculate the target PCBB.  */	hwpcb->ksp = (unsigned long) idle + sizeof(union task_union) - 16;	hwpcb->usp = 0;	hwpcb->ptbr = idle->thread.ptbr;	hwpcb->pcc = 0;	hwpcb->asn = 0;	hwpcb->unique = virt_to_phys(&idle->thread);	hwpcb->flags = idle->thread.pal_flags;	hwpcb->res1 = hwpcb->res2 = 0;#if 0	DBGS(("KSP 0x%lx PTBR 0x%lx VPTBR 0x%lx UNIQUE 0x%lx\n",	      hwpcb->ksp, hwpcb->ptbr, hwrpb->vptb, hwpcb->unique));#endif	DBGS(("Starting secondary cpu %d: state 0x%lx pal_flags 0x%lx\n",	      cpuid, idle->state, idle->thread.pal_flags));	/* Setup HWRPB fields that SRM uses to activate secondary CPU */	hwrpb->CPU_restart = __smp_callin;	hwrpb->CPU_restart_data = (unsigned long) __smp_callin;	/* Recalculate and update the HWRPB checksum */	hwrpb_update_checksum(hwrpb);	/*	 * Send a "start" command to the specified processor.	 */	/* SRM III 3.4.1.3 */	cpu->flags |= 0x22;	/* turn on Context Valid and Restart Capable */	cpu->flags &= ~1;	/* turn off Bootstrap In Progress */	wmb();	send_secondary_console_msg("START\r\n", cpuid);	/* Wait 10 seconds for an ACK from the console.  Note that jiffies 	   aren't ticking yet.  */	for (timeout = 1000000; timeout > 0; timeout--) {		if (cpu->flags & 1)			goto started;		udelay(10);		barrier();	}	printk(KERN_ERR "SMP: Processor %d failed to start.\n", cpuid);	return -1;started:	DBGS(("secondary_cpu_start: SUCCESS for CPU %d!!!\n", cpuid));	return 0;}static int __init fork_by_hand(void){	struct pt_regs regs;	/*	 * don't care about the regs settings since	 * we'll never reschedule the forked task.	 */	return do_fork(CLONE_VM|CLONE_PID, 0, &regs, 0);}/* * Bring one cpu online. */static int __initsmp_boot_one_cpu(int cpuid, int cpunum){	struct task_struct *idle;	long timeout;	/* Cook up an idler for this guy.  Note that the address we give	   to kernel_thread is irrelevant -- it's going to start where	   HWRPB.CPU_restart says to start.  But this gets all the other	   task-y sort of data structures set up like we wish.  */	/*	 * We can't use kernel_thread since we must avoid to	 * reschedule the child.	 */	if (fork_by_hand() < 0)		panic("failed fork for CPU %d", cpuid);	idle = init_task.prev_task;	if (!idle)		panic("No idle process for CPU %d", cpuid);	if (idle == &init_task)		panic("idle process is init_task for CPU %d", cpuid);	idle->processor = cpuid;	idle->cpus_runnable = 1 << cpuid; /* we schedule the first task manually */	__cpu_logical_map[cpunum] = cpuid;	__cpu_number_map[cpuid] = cpunum; 	del_from_runqueue(idle);	unhash_process(idle);	init_tasks[cpunum] = idle;	DBGS(("smp_boot_one_cpu: CPU %d state 0x%lx flags 0x%lx\n",	      cpuid, idle->state, idle->flags));	/* The secondary will change this once it is happy.  Note that	   secondary_cpu_start contains the necessary memory barrier.  */	smp_secondary_alive = -1;	/* Whirrr, whirrr, whirrrrrrrrr... */	if (secondary_cpu_start(cpuid, idle))		return -1;	mb();	/* Notify the secondary CPU it can run calibrate_delay() */	smp_secondary_alive = 0;	/* We've been acked by the console; wait one second for the task	   to start up for real.  Note that jiffies aren't ticking yet.  */	for (timeout = 0; timeout < 1000000; timeout++) {		if (smp_secondary_alive == 1)			goto alive;		udelay(10);		barrier();	}	/* we must invalidate our stuff as we failed to boot the CPU */	__cpu_logical_map[cpunum] = -1;	__cpu_number_map[cpuid] = -1;	/* the idle task is local to us so free it as we don't use it */	free_task_struct(idle);	printk(KERN_ERR "SMP: Processor %d is stuck.\n", cpuid);	return -1;alive:	/* Another "Red Snapper". */	return 0;}/* * Called from setup_arch.  Detect an SMP system and which processors * are present. */void __initsetup_smp(void){	struct percpu_struct *cpubase, *cpu;	int i;	if (boot_cpuid != 0) {		printk(KERN_WARNING "SMP: Booting off cpu %d instead of 0?\n",		       boot_cpuid);	}	if (hwrpb->nr_processors > 1) {		int boot_cpu_palrev;		DBGS(("setup_smp: nr_processors %ld\n",		      hwrpb->nr_processors));		cpubase = (struct percpu_struct *)			((char*)hwrpb + hwrpb->processor_offset);		boot_cpu_palrev = cpubase->pal_revision;		for (i = 0; i < hwrpb->nr_processors; i++ ) {			cpu = (struct percpu_struct *)				((char *)cpubase + i*hwrpb->processor_size);			if ((cpu->flags & 0x1cc) == 0x1cc) {				smp_num_probed++;				/* Assume here that "whami" == index */				hwrpb_cpu_present_mask |= (1UL << i);				cpu->pal_revision = boot_cpu_palrev;			}			DBGS(("setup_smp: CPU %d: flags 0x%lx type 0x%lx\n",			      i, cpu->flags, cpu->type));			DBGS(("setup_smp: CPU %d: PAL rev 0x%lx\n",			      i, cpu->pal_revision));		}	} else {		smp_num_probed = 1;		hwrpb_cpu_present_mask = (1UL << boot_cpuid);	}	cpu_present_mask = 1UL << boot_cpuid;	printk(KERN_INFO "SMP: %d CPUs probed -- cpu_present_mask = %lx\n",	       smp_num_probed, hwrpb_cpu_present_mask);}/* * Called by smp_init bring all the secondaries online and hold them. */void __initsmp_boot_cpus(void){	int cpu_count, i;	unsigned long bogosum;	/* Take care of some initial bookkeeping.  */	memset(__cpu_number_map, -1, sizeof(__cpu_number_map));	memset(__cpu_logical_map, -1, sizeof(__cpu_logical_map));	memset(ipi_data, 0, sizeof(ipi_data));	__cpu_number_map[boot_cpuid] = 0;	__cpu_logical_map[0] = boot_cpuid;	current->processor = boot_cpuid;	smp_store_cpu_info(boot_cpuid);	smp_tune_scheduling(boot_cpuid);	smp_setup_percpu_timer(boot_cpuid);	init_idle();	/* ??? This should be in init_idle.  */	atomic_inc(&init_mm.mm_count);	current->active_mm = &init_mm;	/* Nothing to do on a UP box, or when told not to.  */	if (smp_num_probed == 1 || max_cpus == 0) {		printk(KERN_INFO "SMP mode deactivated.\n");		return;	}	printk(KERN_INFO "SMP starting up secondaries.\n");	cpu_count = 1;	for (i = 0; i < NR_CPUS; i++) {		if (i == boot_cpuid)			continue;		if (((hwrpb_cpu_present_mask >> i) & 1) == 0)

⌨️ 快捷键说明

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