Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* leon_smp.c: Sparc-Leon SMP support.
0003  *
0004  * based on sun4m_smp.c
0005  * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
0006  * Copyright (C) 2009 Daniel Hellstrom (daniel@gaisler.com) Aeroflex Gaisler AB
0007  * Copyright (C) 2009 Konrad Eisele (konrad@gaisler.com) Aeroflex Gaisler AB
0008  */
0009 
0010 #include <asm/head.h>
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/sched/mm.h>
0014 #include <linux/threads.h>
0015 #include <linux/smp.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/kernel_stat.h>
0018 #include <linux/of.h>
0019 #include <linux/init.h>
0020 #include <linux/spinlock.h>
0021 #include <linux/mm.h>
0022 #include <linux/swap.h>
0023 #include <linux/profile.h>
0024 #include <linux/pm.h>
0025 #include <linux/delay.h>
0026 #include <linux/gfp.h>
0027 #include <linux/cpu.h>
0028 #include <linux/clockchips.h>
0029 
0030 #include <asm/cacheflush.h>
0031 #include <asm/tlbflush.h>
0032 
0033 #include <asm/ptrace.h>
0034 #include <linux/atomic.h>
0035 #include <asm/irq_regs.h>
0036 #include <asm/traps.h>
0037 
0038 #include <asm/delay.h>
0039 #include <asm/irq.h>
0040 #include <asm/page.h>
0041 #include <asm/oplib.h>
0042 #include <asm/cpudata.h>
0043 #include <asm/asi.h>
0044 #include <asm/leon.h>
0045 #include <asm/leon_amba.h>
0046 #include <asm/timer.h>
0047 
0048 #include "kernel.h"
0049 
0050 #include "irq.h"
0051 
0052 extern ctxd_t *srmmu_ctx_table_phys;
0053 static int smp_processors_ready;
0054 extern volatile unsigned long cpu_callin_map[NR_CPUS];
0055 extern cpumask_t smp_commenced_mask;
0056 void leon_configure_cache_smp(void);
0057 static void leon_ipi_init(void);
0058 
0059 /* IRQ number of LEON IPIs */
0060 int leon_ipi_irq = LEON3_IRQ_IPI_DEFAULT;
0061 
0062 static inline unsigned long do_swap(volatile unsigned long *ptr,
0063                     unsigned long val)
0064 {
0065     __asm__ __volatile__("swapa [%2] %3, %0\n\t" : "=&r"(val)
0066                  : "0"(val), "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
0067                  : "memory");
0068     return val;
0069 }
0070 
0071 void leon_cpu_pre_starting(void *arg)
0072 {
0073     leon_configure_cache_smp();
0074 }
0075 
0076 void leon_cpu_pre_online(void *arg)
0077 {
0078     int cpuid = hard_smp_processor_id();
0079 
0080     /* Allow master to continue. The master will then give us the
0081      * go-ahead by setting the smp_commenced_mask and will wait without
0082      * timeouts until our setup is completed fully (signified by
0083      * our bit being set in the cpu_online_mask).
0084      */
0085     do_swap(&cpu_callin_map[cpuid], 1);
0086 
0087     local_ops->cache_all();
0088     local_ops->tlb_all();
0089 
0090     /* Fix idle thread fields. */
0091     __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(&current_set[cpuid])
0092                  : "memory" /* paranoid */);
0093 
0094     /* Attach to the address space of init_task. */
0095     mmgrab(&init_mm);
0096     current->active_mm = &init_mm;
0097 
0098     while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
0099         mb();
0100 }
0101 
0102 /*
0103  *  Cycle through the processors asking the PROM to start each one.
0104  */
0105 
0106 extern struct linux_prom_registers smp_penguin_ctable;
0107 
0108 void leon_configure_cache_smp(void)
0109 {
0110     unsigned long cfg = sparc_leon3_get_dcachecfg();
0111     int me = smp_processor_id();
0112 
0113     if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) {
0114         printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n",
0115              (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg),
0116              (unsigned int)cfg, (unsigned int)me);
0117         sparc_leon3_disable_cache();
0118     } else {
0119         if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) {
0120             sparc_leon3_enable_snooping();
0121         } else {
0122             printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
0123                  me);
0124             sparc_leon3_disable_cache();
0125         }
0126     }
0127 
0128     local_ops->cache_all();
0129     local_ops->tlb_all();
0130 }
0131 
0132 static void leon_smp_setbroadcast(unsigned int mask)
0133 {
0134     int broadcast =
0135         ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
0136           LEON3_IRQMPSTATUS_BROADCAST) & 1);
0137     if (!broadcast) {
0138         prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
0139              leon_smp_nrcpus());
0140         if (leon_smp_nrcpus() > 1) {
0141             BUG();
0142         } else {
0143             prom_printf("continue anyway\n");
0144             return;
0145         }
0146     }
0147     LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
0148 }
0149 
0150 int leon_smp_nrcpus(void)
0151 {
0152     int nrcpu =
0153         ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
0154           LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
0155     return nrcpu;
0156 }
0157 
0158 void __init leon_boot_cpus(void)
0159 {
0160     int nrcpu = leon_smp_nrcpus();
0161     int me = smp_processor_id();
0162 
0163     /* Setup IPI */
0164     leon_ipi_init();
0165 
0166     printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
0167            (unsigned int)nrcpu, (unsigned int)NR_CPUS,
0168            (unsigned int)&(leon3_irqctrl_regs->mpstatus));
0169 
0170     leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me);
0171     leon_enable_irq_cpu(LEON3_IRQ_TICKER, me);
0172     leon_enable_irq_cpu(leon_ipi_irq, me);
0173 
0174     leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
0175 
0176     leon_configure_cache_smp();
0177     local_ops->cache_all();
0178 
0179 }
0180 
0181 int leon_boot_one_cpu(int i, struct task_struct *idle)
0182 {
0183     int timeout;
0184 
0185     current_set[i] = task_thread_info(idle);
0186 
0187     /* See trampoline.S:leon_smp_cpu_startup for details...
0188      * Initialize the contexts table
0189      * Since the call to prom_startcpu() trashes the structure,
0190      * we need to re-initialize it for each cpu
0191      */
0192     smp_penguin_ctable.which_io = 0;
0193     smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
0194     smp_penguin_ctable.reg_size = 0;
0195 
0196     /* whirrr, whirrr, whirrrrrrrrr... */
0197     printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
0198            (unsigned int)&leon3_irqctrl_regs->mpstatus);
0199     local_ops->cache_all();
0200 
0201     /* Make sure all IRQs are of from the start for this new CPU */
0202     LEON_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[i], 0);
0203 
0204     /* Wake one CPU */
0205     LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
0206 
0207     /* wheee... it's going... */
0208     for (timeout = 0; timeout < 10000; timeout++) {
0209         if (cpu_callin_map[i])
0210             break;
0211         udelay(200);
0212     }
0213     printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
0214 
0215     if (!(cpu_callin_map[i])) {
0216         printk(KERN_ERR "Processor %d is stuck.\n", i);
0217         return -ENODEV;
0218     } else {
0219         leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
0220         leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
0221         leon_enable_irq_cpu(leon_ipi_irq, i);
0222     }
0223 
0224     local_ops->cache_all();
0225     return 0;
0226 }
0227 
0228 void __init leon_smp_done(void)
0229 {
0230 
0231     int i, first;
0232     int *prev;
0233 
0234     /* setup cpu list for irq rotation */
0235     first = 0;
0236     prev = &first;
0237     for (i = 0; i < NR_CPUS; i++) {
0238         if (cpu_online(i)) {
0239             *prev = i;
0240             prev = &cpu_data(i).next;
0241         }
0242     }
0243     *prev = first;
0244     local_ops->cache_all();
0245 
0246     /* Free unneeded trap tables */
0247     if (!cpu_present(1)) {
0248         free_reserved_page(virt_to_page(&trapbase_cpu1));
0249     }
0250     if (!cpu_present(2)) {
0251         free_reserved_page(virt_to_page(&trapbase_cpu2));
0252     }
0253     if (!cpu_present(3)) {
0254         free_reserved_page(virt_to_page(&trapbase_cpu3));
0255     }
0256     /* Ok, they are spinning and ready to go. */
0257     smp_processors_ready = 1;
0258 
0259 }
0260 
0261 struct leon_ipi_work {
0262     int single;
0263     int msk;
0264     int resched;
0265 };
0266 
0267 static DEFINE_PER_CPU_SHARED_ALIGNED(struct leon_ipi_work, leon_ipi_work);
0268 
0269 /* Initialize IPIs on the LEON, in order to save IRQ resources only one IRQ
0270  * is used for all three types of IPIs.
0271  */
0272 static void __init leon_ipi_init(void)
0273 {
0274     int cpu, len;
0275     struct leon_ipi_work *work;
0276     struct property *pp;
0277     struct device_node *rootnp;
0278     struct tt_entry *trap_table;
0279     unsigned long flags;
0280 
0281     /* Find IPI IRQ or stick with default value */
0282     rootnp = of_find_node_by_path("/ambapp0");
0283     if (rootnp) {
0284         pp = of_find_property(rootnp, "ipi_num", &len);
0285         if (pp && (*(int *)pp->value))
0286             leon_ipi_irq = *(int *)pp->value;
0287     }
0288     printk(KERN_INFO "leon: SMP IPIs at IRQ %d\n", leon_ipi_irq);
0289 
0290     /* Adjust so that we jump directly to smpleon_ipi */
0291     local_irq_save(flags);
0292     trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_ipi_irq - 1)];
0293     trap_table->inst_three += smpleon_ipi - real_irq_entry;
0294     local_ops->cache_all();
0295     local_irq_restore(flags);
0296 
0297     for_each_possible_cpu(cpu) {
0298         work = &per_cpu(leon_ipi_work, cpu);
0299         work->single = work->msk = work->resched = 0;
0300     }
0301 }
0302 
0303 static void leon_send_ipi(int cpu, int level)
0304 {
0305     unsigned long mask;
0306     mask = leon_get_irqmask(level);
0307     LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
0308 }
0309 
0310 static void leon_ipi_single(int cpu)
0311 {
0312     struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
0313 
0314     /* Mark work */
0315     work->single = 1;
0316 
0317     /* Generate IRQ on the CPU */
0318     leon_send_ipi(cpu, leon_ipi_irq);
0319 }
0320 
0321 static void leon_ipi_mask_one(int cpu)
0322 {
0323     struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
0324 
0325     /* Mark work */
0326     work->msk = 1;
0327 
0328     /* Generate IRQ on the CPU */
0329     leon_send_ipi(cpu, leon_ipi_irq);
0330 }
0331 
0332 static void leon_ipi_resched(int cpu)
0333 {
0334     struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
0335 
0336     /* Mark work */
0337     work->resched = 1;
0338 
0339     /* Generate IRQ on the CPU (any IRQ will cause resched) */
0340     leon_send_ipi(cpu, leon_ipi_irq);
0341 }
0342 
0343 void leonsmp_ipi_interrupt(void)
0344 {
0345     struct leon_ipi_work *work = this_cpu_ptr(&leon_ipi_work);
0346 
0347     if (work->single) {
0348         work->single = 0;
0349         smp_call_function_single_interrupt();
0350     }
0351     if (work->msk) {
0352         work->msk = 0;
0353         smp_call_function_interrupt();
0354     }
0355     if (work->resched) {
0356         work->resched = 0;
0357         smp_resched_interrupt();
0358     }
0359 }
0360 
0361 static struct smp_funcall {
0362     void *func;
0363     unsigned long arg1;
0364     unsigned long arg2;
0365     unsigned long arg3;
0366     unsigned long arg4;
0367     unsigned long arg5;
0368     unsigned long processors_in[NR_CPUS];   /* Set when ipi entered. */
0369     unsigned long processors_out[NR_CPUS];  /* Set when ipi exited. */
0370 } ccall_info __attribute__((aligned(8)));
0371 
0372 static DEFINE_SPINLOCK(cross_call_lock);
0373 
0374 /* Cross calls must be serialized, at least currently. */
0375 static void leon_cross_call(void *func, cpumask_t mask, unsigned long arg1,
0376                 unsigned long arg2, unsigned long arg3,
0377                 unsigned long arg4)
0378 {
0379     if (smp_processors_ready) {
0380         register int high = NR_CPUS - 1;
0381         unsigned long flags;
0382 
0383         spin_lock_irqsave(&cross_call_lock, flags);
0384 
0385         {
0386             /* If you make changes here, make sure gcc generates proper code... */
0387             register void *f asm("i0") = func;
0388             register unsigned long a1 asm("i1") = arg1;
0389             register unsigned long a2 asm("i2") = arg2;
0390             register unsigned long a3 asm("i3") = arg3;
0391             register unsigned long a4 asm("i4") = arg4;
0392             register unsigned long a5 asm("i5") = 0;
0393 
0394             __asm__ __volatile__("std %0, [%6]\n\t"
0395                          "std %2, [%6 + 8]\n\t"
0396                          "std %4, [%6 + 16]\n\t" : :
0397                          "r"(f), "r"(a1), "r"(a2), "r"(a3),
0398                          "r"(a4), "r"(a5),
0399                          "r"(&ccall_info.func));
0400         }
0401 
0402         /* Init receive/complete mapping, plus fire the IPI's off. */
0403         {
0404             register int i;
0405 
0406             cpumask_clear_cpu(smp_processor_id(), &mask);
0407             cpumask_and(&mask, cpu_online_mask, &mask);
0408             for (i = 0; i <= high; i++) {
0409                 if (cpumask_test_cpu(i, &mask)) {
0410                     ccall_info.processors_in[i] = 0;
0411                     ccall_info.processors_out[i] = 0;
0412                     leon_send_ipi(i, LEON3_IRQ_CROSS_CALL);
0413 
0414                 }
0415             }
0416         }
0417 
0418         {
0419             register int i;
0420 
0421             i = 0;
0422             do {
0423                 if (!cpumask_test_cpu(i, &mask))
0424                     continue;
0425 
0426                 while (!ccall_info.processors_in[i])
0427                     barrier();
0428             } while (++i <= high);
0429 
0430             i = 0;
0431             do {
0432                 if (!cpumask_test_cpu(i, &mask))
0433                     continue;
0434 
0435                 while (!ccall_info.processors_out[i])
0436                     barrier();
0437             } while (++i <= high);
0438         }
0439 
0440         spin_unlock_irqrestore(&cross_call_lock, flags);
0441     }
0442 }
0443 
0444 /* Running cross calls. */
0445 void leon_cross_call_irq(void)
0446 {
0447     void (*func)(unsigned long, unsigned long, unsigned long, unsigned long,
0448              unsigned long) = ccall_info.func;
0449     int i = smp_processor_id();
0450 
0451     ccall_info.processors_in[i] = 1;
0452     func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3, ccall_info.arg4,
0453          ccall_info.arg5);
0454     ccall_info.processors_out[i] = 1;
0455 }
0456 
0457 static const struct sparc32_ipi_ops leon_ipi_ops = {
0458     .cross_call = leon_cross_call,
0459     .resched    = leon_ipi_resched,
0460     .single     = leon_ipi_single,
0461     .mask_one   = leon_ipi_mask_one,
0462 };
0463 
0464 void __init leon_init_smp(void)
0465 {
0466     /* Patch ipi15 trap table */
0467     t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
0468 
0469     sparc32_ipi_ops = &leon_ipi_ops;
0470 }