1/* 2 * Copyright 2010 Tilera Corporation. All Rights Reserved. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation, version 2. 7 * 8 * This program is distributed in the hope that it will be useful, but 9 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or 11 * NON INFRINGEMENT. See the GNU General Public License for 12 * more details. 13 */ 14 15#include <linux/module.h> 16#include <linux/init.h> 17#include <linux/kernel.h> 18#include <linux/mm.h> 19#include <linux/sched.h> 20#include <linux/kernel_stat.h> 21#include <linux/smp_lock.h> 22#include <linux/bootmem.h> 23#include <linux/notifier.h> 24#include <linux/cpu.h> 25#include <linux/percpu.h> 26#include <linux/delay.h> 27#include <linux/err.h> 28#include <linux/irq.h> 29#include <asm/mmu_context.h> 30#include <asm/tlbflush.h> 31#include <asm/sections.h> 32 33/* State of each CPU. */ 34static DEFINE_PER_CPU(int, cpu_state) = { 0 }; 35 36/* The messaging code jumps to this pointer during boot-up */ 37unsigned long start_cpu_function_addr; 38 39/* Called very early during startup to mark boot cpu as online */ 40void __init smp_prepare_boot_cpu(void) 41{ 42 int cpu = smp_processor_id(); 43 set_cpu_online(cpu, 1); 44 set_cpu_present(cpu, 1); 45 __get_cpu_var(cpu_state) = CPU_ONLINE; 46 47 init_messaging(); 48} 49 50static void start_secondary(void); 51 52/* 53 * Called at the top of init() to launch all the other CPUs. 54 * They run free to complete their initialization and then wait 55 * until they get an IPI from the boot cpu to come online. 56 */ 57void __init smp_prepare_cpus(unsigned int max_cpus) 58{ 59 long rc; 60 int cpu, cpu_count; 61 int boot_cpu = smp_processor_id(); 62 63 current_thread_info()->cpu = boot_cpu; 64 65 /* 66 * Pin this task to the boot CPU while we bring up the others, 67 * just to make sure we don't uselessly migrate as they come up. 68 */ 69 rc = sched_setaffinity(current->pid, cpumask_of(boot_cpu)); 70 if (rc != 0) 71 pr_err("Couldn't set init affinity to boot cpu (%ld)\n", rc); 72 73 /* Print information about disabled and dataplane cpus. */ 74 print_disabled_cpus(); 75 76 /* 77 * Tell the messaging subsystem how to respond to the 78 * startup message. We use a level of indirection to avoid 79 * confusing the linker with the fact that the messaging 80 * subsystem is calling __init code. 81 */ 82 start_cpu_function_addr = (unsigned long) &online_secondary; 83 84 /* Set up thread context for all new processors. */ 85 cpu_count = 1; 86 for (cpu = 0; cpu < NR_CPUS; ++cpu) { 87 struct task_struct *idle; 88 89 if (cpu == boot_cpu) 90 continue; 91 92 if (!cpu_possible(cpu)) { 93 /* 94 * Make this processor do nothing on boot. 95 * Note that we don't give the boot_pc function 96 * a stack, so it has to be assembly code. 97 */ 98 per_cpu(boot_sp, cpu) = 0; 99 per_cpu(boot_pc, cpu) = (unsigned long) smp_nap; 100 continue; 101 } 102 103 /* Create a new idle thread to run start_secondary() */ 104 idle = fork_idle(cpu); 105 if (IS_ERR(idle)) 106 panic("failed fork for CPU %d", cpu); 107 idle->thread.pc = (unsigned long) start_secondary; 108 109 /* Make this thread the boot thread for this processor */ 110 per_cpu(boot_sp, cpu) = task_ksp0(idle); 111 per_cpu(boot_pc, cpu) = idle->thread.pc; 112 113 ++cpu_count; 114 } 115 BUG_ON(cpu_count > (max_cpus ? max_cpus : 1)); 116 117 /* Fire up the other tiles, if any */ 118 init_cpu_present(cpu_possible_mask); 119 if (cpumask_weight(cpu_present_mask) > 1) { 120 mb(); /* make sure all data is visible to new processors */ 121 hv_start_all_tiles(); 122 } 123} 124 125static __initdata struct cpumask init_affinity; 126 127static __init int reset_init_affinity(void) 128{ 129 long rc = sched_setaffinity(current->pid, &init_affinity); 130 if (rc != 0) 131 pr_warning("couldn't reset init affinity (%ld)\n", 132 rc); 133 return 0; 134} 135late_initcall(reset_init_affinity); 136 137static struct cpumask cpu_started __cpuinitdata; 138 139/* 140 * Activate a secondary processor. Very minimal; don't add anything 141 * to this path without knowing what you're doing, since SMP booting 142 * is pretty fragile. 143 */ 144static void __cpuinit start_secondary(void) 145{ 146 int cpuid = smp_processor_id(); 147 148 /* Set our thread pointer appropriately. */ 149 set_my_cpu_offset(__per_cpu_offset[cpuid]); 150 151 preempt_disable(); 152 153 /* 154 * In large machines even this will slow us down, since we 155 * will be contending for for the printk spinlock. 156 */ 157 /* printk(KERN_DEBUG "Initializing CPU#%d\n", cpuid); */ 158 159 /* Initialize the current asid for our first page table. */ 160 __get_cpu_var(current_asid) = min_asid; 161 162 /* Set up this thread as another owner of the init_mm */ 163 atomic_inc(&init_mm.mm_count); 164 current->active_mm = &init_mm; 165 if (current->mm) 166 BUG(); 167 enter_lazy_tlb(&init_mm, current); 168 169 /* Allow hypervisor messages to be received */ 170 init_messaging(); 171 local_irq_enable(); 172 173 /* Indicate that we're ready to come up. */ 174 /* Must not do this before we're ready to receive messages */ 175 if (cpumask_test_and_set_cpu(cpuid, &cpu_started)) { 176 pr_warning("CPU#%d already started!\n", cpuid); 177 for (;;) 178 local_irq_enable(); 179 } 180 181 smp_nap(); 182} 183 184/* 185 * Bring a secondary processor online. 186 */ 187void __cpuinit online_secondary(void) 188{ 189 /* 190 * low-memory mappings have been cleared, flush them from 191 * the local TLBs too. 192 */ 193 local_flush_tlb(); 194 195 BUG_ON(in_interrupt()); 196 197 /* This must be done before setting cpu_online_mask */ 198 wmb(); 199 200 /* 201 * We need to hold call_lock, so there is no inconsistency 202 * between the time smp_call_function() determines number of 203 * IPI recipients, and the time when the determination is made 204 * for which cpus receive the IPI. Holding this 205 * lock helps us to not include this cpu in a currently in progress 206 * smp_call_function(). 207 */ 208 ipi_call_lock(); 209 set_cpu_online(smp_processor_id(), 1); 210 ipi_call_unlock(); 211 __get_cpu_var(cpu_state) = CPU_ONLINE; 212 213 /* Set up tile-specific state for this cpu. */ 214 setup_cpu(0); 215 216 /* Set up tile-timer clock-event device on this cpu */ 217 setup_tile_timer(); 218 219 preempt_enable(); 220 221 cpu_idle(); 222} 223 224int __cpuinit __cpu_up(unsigned int cpu) 225{ 226 /* Wait 5s total for all CPUs for them to come online */ 227 static int timeout; 228 for (; !cpumask_test_cpu(cpu, &cpu_started); timeout++) { 229 if (timeout >= 50000) { 230 pr_info("skipping unresponsive cpu%d\n", cpu); 231 local_irq_enable(); 232 return -EIO; 233 } 234 udelay(100); 235 } 236 237 local_irq_enable(); 238 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE; 239 240 /* Unleash the CPU! */ 241 send_IPI_single(cpu, MSG_TAG_START_CPU); 242 while (!cpumask_test_cpu(cpu, cpu_online_mask)) 243 cpu_relax(); 244 return 0; 245} 246 247static void panic_start_cpu(void) 248{ 249 panic("Received a MSG_START_CPU IPI after boot finished."); 250} 251 252void __init smp_cpus_done(unsigned int max_cpus) 253{ 254 int cpu, next, rc; 255 256 /* Reset the response to a (now illegal) MSG_START_CPU IPI. */ 257 start_cpu_function_addr = (unsigned long) &panic_start_cpu; 258 259 cpumask_copy(&init_affinity, cpu_online_mask); 260 261 /* 262 * Pin ourselves to a single cpu in the initial affinity set 263 * so that kernel mappings for the rootfs are not in the dataplane, 264 * if set, and to avoid unnecessary migrating during bringup. 265 * Use the last cpu just in case the whole chip has been 266 * isolated from the scheduler, to keep init away from likely 267 * more useful user code. This also ensures that work scheduled 268 * via schedule_delayed_work() in the init routines will land 269 * on this cpu. 270 */ 271 for (cpu = cpumask_first(&init_affinity); 272 (next = cpumask_next(cpu, &init_affinity)) < nr_cpu_ids; 273 cpu = next) 274 ; 275 rc = sched_setaffinity(current->pid, cpumask_of(cpu)); 276 if (rc != 0) 277 pr_err("Couldn't set init affinity to cpu %d (%d)\n", cpu, rc); 278} 279