fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / sparc / kernel / smp.c
index cb2399e..6b5f26b 100644 (file)
 #include <asm/pgalloc.h>
 #include <asm/pgtable.h>
 #include <asm/oplib.h>
-#include <asm/hardirq.h>
 #include <asm/cacheflush.h>
 #include <asm/tlbflush.h>
 #include <asm/cpudata.h>
 
-volatile int smp_processors_ready = 0;
 int smp_num_cpus = 1;
-int smp_threads_ready=0;
 volatile unsigned long cpu_callin_map[NR_CPUS] __initdata = {0,};
 unsigned char boot_cpu_id = 0;
 unsigned char boot_cpu_id4 = 0; /* boot_cpu_id << 2 */
 int smp_activated = 0;
 volatile int __cpu_number_map[NR_CPUS];
 volatile int __cpu_logical_map[NR_CPUS];
-cycles_t cacheflush_time = 0; /* XXX */
 
 cpumask_t cpu_online_map = CPU_MASK_NONE;
 cpumask_t phys_cpu_present_map = CPU_MASK_NONE;
+cpumask_t smp_commenced_mask = CPU_MASK_NONE;
 
 /* The only guaranteed locking primitive available on all Sparc
  * processors is 'ldstub [%reg + immediate], %dest_reg' which atomically
@@ -60,12 +57,7 @@ cpumask_t phys_cpu_present_map = CPU_MASK_NONE;
 /* Used to make bitops atomic */
 unsigned char bitops_spinlock = 0;
 
-volatile unsigned long ipi_count;
-
-volatile int smp_process_available=0;
-volatile int smp_commenced = 0;
-
-void __init smp_store_cpu_info(int id)
+void __cpuinit smp_store_cpu_info(int id)
 {
        int cpu_node;
 
@@ -76,12 +68,66 @@ void __init smp_store_cpu_info(int id)
                                                     "clock-frequency", 0);
        cpu_data(id).prom_node = cpu_node;
        cpu_data(id).mid = cpu_get_hwmid(cpu_node);
+
+       /* this is required to tune the scheduler correctly */
+       /* is it possible to have CPUs with different cache sizes? */
+       if (id == boot_cpu_id) {
+               int cache_line,cache_nlines;
+               cache_line = 0x20;
+               cache_line = prom_getintdefault(cpu_node, "ecache-line-size", cache_line);
+               cache_nlines = 0x8000;
+               cache_nlines = prom_getintdefault(cpu_node, "ecache-nlines", cache_nlines);
+               max_cache_size = cache_line * cache_nlines;
+       }
        if (cpu_data(id).mid < 0)
                panic("No MID found for CPU%d at node 0x%08d", id, cpu_node);
 }
 
 void __init smp_cpus_done(unsigned int max_cpus)
 {
+       extern void smp4m_smp_done(void);
+       extern void smp4d_smp_done(void);
+       unsigned long bogosum = 0;
+       int cpu, num;
+
+       for (cpu = 0, num = 0; cpu < NR_CPUS; cpu++)
+               if (cpu_online(cpu)) {
+                       num++;
+                       bogosum += cpu_data(cpu).udelay_val;
+               }
+
+       printk("Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
+               num, bogosum/(500000/HZ),
+               (bogosum/(5000/HZ))%100);
+
+       switch(sparc_cpu_model) {
+       case sun4:
+               printk("SUN4\n");
+               BUG();
+               break;
+       case sun4c:
+               printk("SUN4C\n");
+               BUG();
+               break;
+       case sun4m:
+               smp4m_smp_done();
+               break;
+       case sun4d:
+               smp4d_smp_done();
+               break;
+       case sun4e:
+               printk("SUN4E\n");
+               BUG();
+               break;
+       case sun4u:
+               printk("SUN4U\n");
+               BUG();
+               break;
+       default:
+               printk("UNKNOWN!\n");
+               BUG();
+               break;
+       };
 }
 
 void cpu_panic(void)
@@ -92,17 +138,6 @@ void cpu_panic(void)
 
 struct linux_prom_registers smp_penguin_ctable __initdata = { 0 };
 
-void __init smp_boot_cpus(void)
-{
-       extern void smp4m_boot_cpus(void);
-       extern void smp4d_boot_cpus(void);
-       
-       if (sparc_cpu_model == sun4m)
-               smp4m_boot_cpus();
-       else
-               smp4d_boot_cpus();
-}
-
 void smp_send_reschedule(int cpu)
 {
        /* See sparc64 */
@@ -127,7 +162,9 @@ void smp_flush_tlb_all(void)
 void smp_flush_cache_mm(struct mm_struct *mm)
 {
        if(mm->context != NO_CONTEXT) {
-               if(mm->cpu_vm_mask != (1 << smp_processor_id()))
+               cpumask_t cpu_mask = mm->cpu_vm_mask;
+               cpu_clear(smp_processor_id(), cpu_mask);
+               if (!cpus_empty(cpu_mask))
                        xc1((smpfunc_t) BTFIXUP_CALL(local_flush_cache_mm), (unsigned long) mm);
                local_flush_cache_mm(mm);
        }
@@ -136,10 +173,12 @@ void smp_flush_cache_mm(struct mm_struct *mm)
 void smp_flush_tlb_mm(struct mm_struct *mm)
 {
        if(mm->context != NO_CONTEXT) {
-               if(mm->cpu_vm_mask != (1 << smp_processor_id())) {
+               cpumask_t cpu_mask = mm->cpu_vm_mask;
+               cpu_clear(smp_processor_id(), cpu_mask);
+               if (!cpus_empty(cpu_mask)) {
                        xc1((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_mm), (unsigned long) mm);
                        if(atomic_read(&mm->mm_users) == 1 && current->active_mm == mm)
-                               mm->cpu_vm_mask = (1 << smp_processor_id());
+                               mm->cpu_vm_mask = cpumask_of_cpu(smp_processor_id());
                }
                local_flush_tlb_mm(mm);
        }
@@ -151,7 +190,9 @@ void smp_flush_cache_range(struct vm_area_struct *vma, unsigned long start,
        struct mm_struct *mm = vma->vm_mm;
 
        if (mm->context != NO_CONTEXT) {
-               if(mm->cpu_vm_mask != (1 << smp_processor_id()))
+               cpumask_t cpu_mask = mm->cpu_vm_mask;
+               cpu_clear(smp_processor_id(), cpu_mask);
+               if (!cpus_empty(cpu_mask))
                        xc3((smpfunc_t) BTFIXUP_CALL(local_flush_cache_range), (unsigned long) vma, start, end);
                local_flush_cache_range(vma, start, end);
        }
@@ -163,7 +204,9 @@ void smp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
        struct mm_struct *mm = vma->vm_mm;
 
        if (mm->context != NO_CONTEXT) {
-               if(mm->cpu_vm_mask != (1 << smp_processor_id()))
+               cpumask_t cpu_mask = mm->cpu_vm_mask;
+               cpu_clear(smp_processor_id(), cpu_mask);
+               if (!cpus_empty(cpu_mask))
                        xc3((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_range), (unsigned long) vma, start, end);
                local_flush_tlb_range(vma, start, end);
        }
@@ -174,7 +217,9 @@ void smp_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
        struct mm_struct *mm = vma->vm_mm;
 
        if(mm->context != NO_CONTEXT) {
-               if(mm->cpu_vm_mask != (1 << smp_processor_id()))
+               cpumask_t cpu_mask = mm->cpu_vm_mask;
+               cpu_clear(smp_processor_id(), cpu_mask);
+               if (!cpus_empty(cpu_mask))
                        xc2((smpfunc_t) BTFIXUP_CALL(local_flush_cache_page), (unsigned long) vma, page);
                local_flush_cache_page(vma, page);
        }
@@ -185,12 +230,19 @@ void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
        struct mm_struct *mm = vma->vm_mm;
 
        if(mm->context != NO_CONTEXT) {
-               if(mm->cpu_vm_mask != (1 << smp_processor_id()))
+               cpumask_t cpu_mask = mm->cpu_vm_mask;
+               cpu_clear(smp_processor_id(), cpu_mask);
+               if (!cpus_empty(cpu_mask))
                        xc2((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_page), (unsigned long) vma, page);
                local_flush_tlb_page(vma, page);
        }
 }
 
+void smp_reschedule_irq(void)
+{
+       set_need_resched();
+}
+
 void smp_flush_page_to_ram(unsigned long page)
 {
        /* Current theory is that those who call this are the one's
@@ -207,7 +259,9 @@ void smp_flush_page_to_ram(unsigned long page)
 
 void smp_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
 {
-       if(mm->cpu_vm_mask != (1 << smp_processor_id()))
+       cpumask_t cpu_mask = mm->cpu_vm_mask;
+       cpu_clear(smp_processor_id(), cpu_mask);
+       if (!cpus_empty(cpu_mask))
                xc2((smpfunc_t) BTFIXUP_CALL(local_flush_sig_insns), (unsigned long) mm, insn_addr);
        local_flush_sig_insns(mm, insn_addr);
 }
@@ -215,7 +269,7 @@ void smp_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
 extern unsigned int lvl14_resolution;
 
 /* /proc/profile writes can call this, don't __init it please. */
-static spinlock_t prof_setup_lock = SPIN_LOCK_UNLOCKED;
+static DEFINE_SPINLOCK(prof_setup_lock);
 
 int setup_profiling_timer(unsigned int multiplier)
 {
@@ -227,9 +281,8 @@ int setup_profiling_timer(unsigned int multiplier)
                return -EINVAL;
 
        spin_lock_irqsave(&prof_setup_lock, flags);
-       for(i = 0; i < NR_CPUS; i++) {
-               if (cpu_possible(i))
-                       load_profile_irq(i, lvl14_resolution / multiplier);
+       for_each_possible_cpu(i) {
+               load_profile_irq(i, lvl14_resolution / multiplier);
                prof_multiplier(i) = multiplier;
        }
        spin_unlock_irqrestore(&prof_setup_lock, flags);
@@ -237,33 +290,142 @@ int setup_profiling_timer(unsigned int multiplier)
        return 0;
 }
 
-void __init smp_prepare_cpus(unsigned int maxcpus)
+void __init smp_prepare_cpus(unsigned int max_cpus)
+{
+       extern void __init smp4m_boot_cpus(void);
+       extern void __init smp4d_boot_cpus(void);
+       int i, cpuid, extra;
+
+       printk("Entering SMP Mode...\n");
+
+       extra = 0;
+       for (i = 0; !cpu_find_by_instance(i, NULL, &cpuid); i++) {
+               if (cpuid >= NR_CPUS)
+                       extra++;
+       }
+       /* i = number of cpus */
+       if (extra && max_cpus > i - extra)
+               printk("Warning: NR_CPUS is too low to start all cpus\n");
+
+       smp_store_cpu_info(boot_cpu_id);
+
+       switch(sparc_cpu_model) {
+       case sun4:
+               printk("SUN4\n");
+               BUG();
+               break;
+       case sun4c:
+               printk("SUN4C\n");
+               BUG();
+               break;
+       case sun4m:
+               smp4m_boot_cpus();
+               break;
+       case sun4d:
+               smp4d_boot_cpus();
+               break;
+       case sun4e:
+               printk("SUN4E\n");
+               BUG();
+               break;
+       case sun4u:
+               printk("SUN4U\n");
+               BUG();
+               break;
+       default:
+               printk("UNKNOWN!\n");
+               BUG();
+               break;
+       };
+}
+
+/* Set this up early so that things like the scheduler can init
+ * properly.  We use the same cpu mask for both the present and
+ * possible cpu map.
+ */
+void __init smp_setup_cpu_possible_map(void)
 {
+       int instance, mid;
+
+       instance = 0;
+       while (!cpu_find_by_instance(instance, NULL, &mid)) {
+               if (mid < NR_CPUS) {
+                       cpu_set(mid, phys_cpu_present_map);
+                       cpu_set(mid, cpu_present_map);
+               }
+               instance++;
+       }
 }
 
-void __devinit smp_prepare_boot_cpu(void)
+void __init smp_prepare_boot_cpu(void)
 {
-       current_thread_info()->cpu = hard_smp_processor_id();
-       cpu_set(smp_processor_id(), cpu_online_map);
-       cpu_set(smp_processor_id(), phys_cpu_present_map);
+       int cpuid = hard_smp_processor_id();
+
+       if (cpuid >= NR_CPUS) {
+               prom_printf("Serious problem, boot cpu id >= NR_CPUS\n");
+               prom_halt();
+       }
+       if (cpuid != 0)
+               printk("boot cpu id != 0, this could work but is untested\n");
+
+       current_thread_info()->cpu = cpuid;
+       cpu_set(cpuid, cpu_online_map);
+       cpu_set(cpuid, phys_cpu_present_map);
 }
 
-int __devinit __cpu_up(unsigned int cpu)
+int __cpuinit __cpu_up(unsigned int cpu)
 {
-       panic("smp doesn't work\n");
+       extern int __cpuinit smp4m_boot_one_cpu(int);
+       extern int __cpuinit smp4d_boot_one_cpu(int);
+       int ret=0;
+
+       switch(sparc_cpu_model) {
+       case sun4:
+               printk("SUN4\n");
+               BUG();
+               break;
+       case sun4c:
+               printk("SUN4C\n");
+               BUG();
+               break;
+       case sun4m:
+               ret = smp4m_boot_one_cpu(cpu);
+               break;
+       case sun4d:
+               ret = smp4d_boot_one_cpu(cpu);
+               break;
+       case sun4e:
+               printk("SUN4E\n");
+               BUG();
+               break;
+       case sun4u:
+               printk("SUN4U\n");
+               BUG();
+               break;
+       default:
+               printk("UNKNOWN!\n");
+               BUG();
+               break;
+       };
+
+       if (!ret) {
+               cpu_set(cpu, smp_commenced_mask);
+               while (!cpu_online(cpu))
+                       mb();
+       }
+       return ret;
 }
 
 void smp_bogo(struct seq_file *m)
 {
        int i;
        
-       for (i = 0; i < NR_CPUS; i++) {
-               if (cpu_online(i))
-                       seq_printf(m,
-                                  "Cpu%dBogo\t: %lu.%02lu\n", 
-                                  i,
-                                  cpu_data(i).udelay_val/(500000/HZ),
-                                  (cpu_data(i).udelay_val/(5000/HZ))%100);
+       for_each_online_cpu(i) {
+               seq_printf(m,
+                          "Cpu%dBogo\t: %lu.%02lu\n",
+                          i,
+                          cpu_data(i).udelay_val/(500000/HZ),
+                          (cpu_data(i).udelay_val/(5000/HZ))%100);
        }
 }
 
@@ -272,8 +434,6 @@ void smp_info(struct seq_file *m)
        int i;
 
        seq_printf(m, "State:\n");
-       for (i = 0; i < NR_CPUS; i++) {
-               if (cpu_online(i))
-                       seq_printf(m, "CPU%d\t\t: online\n", i);
-       }
+       for_each_online_cpu(i)
+               seq_printf(m, "CPU%d\t\t: online\n", i);
 }