/*1* Copyright 2010 Tilera Corporation. All Rights Reserved.2*3* This program is free software; you can redistribute it and/or4* modify it under the terms of the GNU General Public License5* as published by the Free Software Foundation, version 2.6*7* This program is distributed in the hope that it will be useful, but8* WITHOUT ANY WARRANTY; without even the implied warranty of9* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or10* NON INFRINGEMENT. See the GNU General Public License for11* more details.12*/1314#include <linux/module.h>15#include <linux/init.h>16#include <linux/kernel.h>17#include <linux/mm.h>18#include <linux/sched.h>19#include <linux/kernel_stat.h>20#include <linux/bootmem.h>21#include <linux/notifier.h>22#include <linux/cpu.h>23#include <linux/percpu.h>24#include <linux/delay.h>25#include <linux/err.h>26#include <linux/irq.h>27#include <asm/mmu_context.h>28#include <asm/tlbflush.h>29#include <asm/sections.h>3031/* State of each CPU. */32static DEFINE_PER_CPU(int, cpu_state) = { 0 };3334/* The messaging code jumps to this pointer during boot-up */35unsigned long start_cpu_function_addr;3637/* Called very early during startup to mark boot cpu as online */38void __init smp_prepare_boot_cpu(void)39{40int cpu = smp_processor_id();41set_cpu_online(cpu, 1);42set_cpu_present(cpu, 1);43__get_cpu_var(cpu_state) = CPU_ONLINE;4445init_messaging();46}4748static void start_secondary(void);4950/*51* Called at the top of init() to launch all the other CPUs.52* They run free to complete their initialization and then wait53* until they get an IPI from the boot cpu to come online.54*/55void __init smp_prepare_cpus(unsigned int max_cpus)56{57long rc;58int cpu, cpu_count;59int boot_cpu = smp_processor_id();6061current_thread_info()->cpu = boot_cpu;6263/*64* Pin this task to the boot CPU while we bring up the others,65* just to make sure we don't uselessly migrate as they come up.66*/67rc = sched_setaffinity(current->pid, cpumask_of(boot_cpu));68if (rc != 0)69pr_err("Couldn't set init affinity to boot cpu (%ld)\n", rc);7071/* Print information about disabled and dataplane cpus. */72print_disabled_cpus();7374/*75* Tell the messaging subsystem how to respond to the76* startup message. We use a level of indirection to avoid77* confusing the linker with the fact that the messaging78* subsystem is calling __init code.79*/80start_cpu_function_addr = (unsigned long) &online_secondary;8182/* Set up thread context for all new processors. */83cpu_count = 1;84for (cpu = 0; cpu < NR_CPUS; ++cpu) {85struct task_struct *idle;8687if (cpu == boot_cpu)88continue;8990if (!cpu_possible(cpu)) {91/*92* Make this processor do nothing on boot.93* Note that we don't give the boot_pc function94* a stack, so it has to be assembly code.95*/96per_cpu(boot_sp, cpu) = 0;97per_cpu(boot_pc, cpu) = (unsigned long) smp_nap;98continue;99}100101/* Create a new idle thread to run start_secondary() */102idle = fork_idle(cpu);103if (IS_ERR(idle))104panic("failed fork for CPU %d", cpu);105idle->thread.pc = (unsigned long) start_secondary;106107/* Make this thread the boot thread for this processor */108per_cpu(boot_sp, cpu) = task_ksp0(idle);109per_cpu(boot_pc, cpu) = idle->thread.pc;110111++cpu_count;112}113BUG_ON(cpu_count > (max_cpus ? max_cpus : 1));114115/* Fire up the other tiles, if any */116init_cpu_present(cpu_possible_mask);117if (cpumask_weight(cpu_present_mask) > 1) {118mb(); /* make sure all data is visible to new processors */119hv_start_all_tiles();120}121}122123static __initdata struct cpumask init_affinity;124125static __init int reset_init_affinity(void)126{127long rc = sched_setaffinity(current->pid, &init_affinity);128if (rc != 0)129pr_warning("couldn't reset init affinity (%ld)\n",130rc);131return 0;132}133late_initcall(reset_init_affinity);134135static struct cpumask cpu_started __cpuinitdata;136137/*138* Activate a secondary processor. Very minimal; don't add anything139* to this path without knowing what you're doing, since SMP booting140* is pretty fragile.141*/142static void __cpuinit start_secondary(void)143{144int cpuid = smp_processor_id();145146/* Set our thread pointer appropriately. */147set_my_cpu_offset(__per_cpu_offset[cpuid]);148149preempt_disable();150151/*152* In large machines even this will slow us down, since we153* will be contending for for the printk spinlock.154*/155/* printk(KERN_DEBUG "Initializing CPU#%d\n", cpuid); */156157/* Initialize the current asid for our first page table. */158__get_cpu_var(current_asid) = min_asid;159160/* Set up this thread as another owner of the init_mm */161atomic_inc(&init_mm.mm_count);162current->active_mm = &init_mm;163if (current->mm)164BUG();165enter_lazy_tlb(&init_mm, current);166167/* Allow hypervisor messages to be received */168init_messaging();169local_irq_enable();170171/* Indicate that we're ready to come up. */172/* Must not do this before we're ready to receive messages */173if (cpumask_test_and_set_cpu(cpuid, &cpu_started)) {174pr_warning("CPU#%d already started!\n", cpuid);175for (;;)176local_irq_enable();177}178179smp_nap();180}181182/*183* Bring a secondary processor online.184*/185void __cpuinit online_secondary(void)186{187/*188* low-memory mappings have been cleared, flush them from189* the local TLBs too.190*/191local_flush_tlb();192193BUG_ON(in_interrupt());194195/* This must be done before setting cpu_online_mask */196wmb();197198/*199* We need to hold call_lock, so there is no inconsistency200* between the time smp_call_function() determines number of201* IPI recipients, and the time when the determination is made202* for which cpus receive the IPI. Holding this203* lock helps us to not include this cpu in a currently in progress204* smp_call_function().205*/206ipi_call_lock();207set_cpu_online(smp_processor_id(), 1);208ipi_call_unlock();209__get_cpu_var(cpu_state) = CPU_ONLINE;210211/* Set up tile-specific state for this cpu. */212setup_cpu(0);213214/* Set up tile-timer clock-event device on this cpu */215setup_tile_timer();216217preempt_enable();218219cpu_idle();220}221222int __cpuinit __cpu_up(unsigned int cpu)223{224/* Wait 5s total for all CPUs for them to come online */225static int timeout;226for (; !cpumask_test_cpu(cpu, &cpu_started); timeout++) {227if (timeout >= 50000) {228pr_info("skipping unresponsive cpu%d\n", cpu);229local_irq_enable();230return -EIO;231}232udelay(100);233}234235local_irq_enable();236per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;237238/* Unleash the CPU! */239send_IPI_single(cpu, MSG_TAG_START_CPU);240while (!cpumask_test_cpu(cpu, cpu_online_mask))241cpu_relax();242return 0;243}244245static void panic_start_cpu(void)246{247panic("Received a MSG_START_CPU IPI after boot finished.");248}249250void __init smp_cpus_done(unsigned int max_cpus)251{252int cpu, next, rc;253254/* Reset the response to a (now illegal) MSG_START_CPU IPI. */255start_cpu_function_addr = (unsigned long) &panic_start_cpu;256257cpumask_copy(&init_affinity, cpu_online_mask);258259/*260* Pin ourselves to a single cpu in the initial affinity set261* so that kernel mappings for the rootfs are not in the dataplane,262* if set, and to avoid unnecessary migrating during bringup.263* Use the last cpu just in case the whole chip has been264* isolated from the scheduler, to keep init away from likely265* more useful user code. This also ensures that work scheduled266* via schedule_delayed_work() in the init routines will land267* on this cpu.268*/269for (cpu = cpumask_first(&init_affinity);270(next = cpumask_next(cpu, &init_affinity)) < nr_cpu_ids;271cpu = next)272;273rc = sched_setaffinity(current->pid, cpumask_of(cpu));274if (rc != 0)275pr_err("Couldn't set init affinity to cpu %d (%d)\n", cpu, rc);276}277278279