// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2010, 2011, 2012, Lemote, Inc. * Author: Chen Huacai, chenhc@lemote.com */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "smp.h" DEFINE_PER_CPU(int, cpu_state); static void *ipi_set0_regs[16]; static void *ipi_clear0_regs[16]; static void *ipi_status0_regs[16]; static void *ipi_en0_regs[16]; static void *ipi_mailbox_buf[16]; static uint32_t core0_c0count[NR_CPUS]; /* read a 32bit value from ipi register */ #define loongson3_ipi_read32(addr) readl(addr) /* read a 64bit value from ipi register */ #define loongson3_ipi_read64(addr) readq(addr) /* write a 32bit value to ipi register */ #define loongson3_ipi_write32(action, addr) \ do { \ writel(action, addr); \ __wbflush(); \ } while (0) /* write a 64bit value to ipi register */ #define loongson3_ipi_write64(action, addr) \ do { \ writeq(action, addr); \ __wbflush(); \ } while (0) u32 (*ipi_read_clear)(int cpu); void (*ipi_write_action)(int cpu, u32 action); static u32 csr_ipi_read_clear(int cpu) { u32 action; /* Load the ipi register to figure out what we're supposed to do */ action = csr_readl(LOONGSON_CSR_IPI_STATUS); /* Clear the ipi register to clear the interrupt */ csr_writel(action, LOONGSON_CSR_IPI_CLEAR); return action; } static void csr_ipi_write_action(int cpu, u32 action) { unsigned int irq = 0; while ((irq = ffs(action))) { uint32_t val = CSR_IPI_SEND_BLOCK; val |= (irq - 1); val |= (cpu << CSR_IPI_SEND_CPU_SHIFT); csr_writel(val, LOONGSON_CSR_IPI_SEND); action &= ~BIT(irq - 1); } } static u32 legacy_ipi_read_clear(int cpu) { u32 action; /* Load the ipi register to figure out what we're supposed to do */ action = loongson3_ipi_read32(ipi_status0_regs[cpu_logical_map(cpu)]); /* Clear the ipi register to clear the interrupt */ loongson3_ipi_write32(action, ipi_clear0_regs[cpu_logical_map(cpu)]); return action; } static void legacy_ipi_write_action(int cpu, u32 action) { loongson3_ipi_write32((u32)action, ipi_set0_regs[cpu]); } static void csr_ipi_probe(void) { if (cpu_has_csr() && csr_readl(LOONGSON_CSR_FEATURES) & LOONGSON_CSRF_IPI) { ipi_read_clear = csr_ipi_read_clear; ipi_write_action = csr_ipi_write_action; } else { ipi_read_clear = legacy_ipi_read_clear; ipi_write_action = legacy_ipi_write_action; } } static void ipi_set0_regs_init(void) { ipi_set0_regs[0] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE0_OFFSET + SET0); ipi_set0_regs[1] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE1_OFFSET + SET0); ipi_set0_regs[2] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE2_OFFSET + SET0); ipi_set0_regs[3] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE3_OFFSET + SET0); ipi_set0_regs[4] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE0_OFFSET + SET0); ipi_set0_regs[5] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE1_OFFSET + SET0); ipi_set0_regs[6] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE2_OFFSET + SET0); ipi_set0_regs[7] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE3_OFFSET + SET0); ipi_set0_regs[8] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE0_OFFSET + SET0); ipi_set0_regs[9] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE1_OFFSET + SET0); ipi_set0_regs[10] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE2_OFFSET + SET0); ipi_set0_regs[11] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE3_OFFSET + SET0); ipi_set0_regs[12] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE0_OFFSET + SET0); ipi_set0_regs[13] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE1_OFFSET + SET0); ipi_set0_regs[14] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE2_OFFSET + SET0); ipi_set0_regs[15] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE3_OFFSET + SET0); } static void ipi_clear0_regs_init(void) { ipi_clear0_regs[0] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE0_OFFSET + CLEAR0); ipi_clear0_regs[1] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE1_OFFSET + CLEAR0); ipi_clear0_regs[2] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE2_OFFSET + CLEAR0); ipi_clear0_regs[3] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE3_OFFSET + CLEAR0); ipi_clear0_regs[4] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE0_OFFSET + CLEAR0); ipi_clear0_regs[5] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE1_OFFSET + CLEAR0); ipi_clear0_regs[6] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE2_OFFSET + CLEAR0); ipi_clear0_regs[7] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE3_OFFSET + CLEAR0); ipi_clear0_regs[8] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE0_OFFSET + CLEAR0); ipi_clear0_regs[9] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE1_OFFSET + CLEAR0); ipi_clear0_regs[10] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE2_OFFSET + CLEAR0); ipi_clear0_regs[11] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE3_OFFSET + CLEAR0); ipi_clear0_regs[12] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE0_OFFSET + CLEAR0); ipi_clear0_regs[13] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE1_OFFSET + CLEAR0); ipi_clear0_regs[14] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE2_OFFSET + CLEAR0); ipi_clear0_regs[15] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE3_OFFSET + CLEAR0); } static void ipi_status0_regs_init(void) { ipi_status0_regs[0] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE0_OFFSET + STATUS0); ipi_status0_regs[1] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE1_OFFSET + STATUS0); ipi_status0_regs[2] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE2_OFFSET + STATUS0); ipi_status0_regs[3] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE3_OFFSET + STATUS0); ipi_status0_regs[4] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE0_OFFSET + STATUS0); ipi_status0_regs[5] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE1_OFFSET + STATUS0); ipi_status0_regs[6] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE2_OFFSET + STATUS0); ipi_status0_regs[7] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE3_OFFSET + STATUS0); ipi_status0_regs[8] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE0_OFFSET + STATUS0); ipi_status0_regs[9] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE1_OFFSET + STATUS0); ipi_status0_regs[10] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE2_OFFSET + STATUS0); ipi_status0_regs[11] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE3_OFFSET + STATUS0); ipi_status0_regs[12] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE0_OFFSET + STATUS0); ipi_status0_regs[13] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE1_OFFSET + STATUS0); ipi_status0_regs[14] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE2_OFFSET + STATUS0); ipi_status0_regs[15] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE3_OFFSET + STATUS0); } static void ipi_en0_regs_init(void) { ipi_en0_regs[0] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE0_OFFSET + EN0); ipi_en0_regs[1] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE1_OFFSET + EN0); ipi_en0_regs[2] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE2_OFFSET + EN0); ipi_en0_regs[3] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE3_OFFSET + EN0); ipi_en0_regs[4] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE0_OFFSET + EN0); ipi_en0_regs[5] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE1_OFFSET + EN0); ipi_en0_regs[6] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE2_OFFSET + EN0); ipi_en0_regs[7] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE3_OFFSET + EN0); ipi_en0_regs[8] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE0_OFFSET + EN0); ipi_en0_regs[9] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE1_OFFSET + EN0); ipi_en0_regs[10] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE2_OFFSET + EN0); ipi_en0_regs[11] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE3_OFFSET + EN0); ipi_en0_regs[12] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE0_OFFSET + EN0); ipi_en0_regs[13] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE1_OFFSET + EN0); ipi_en0_regs[14] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE2_OFFSET + EN0); ipi_en0_regs[15] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE3_OFFSET + EN0); } static void ipi_mailbox_buf_init(void) { ipi_mailbox_buf[0] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE0_OFFSET + BUF); ipi_mailbox_buf[1] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE1_OFFSET + BUF); ipi_mailbox_buf[2] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE2_OFFSET + BUF); ipi_mailbox_buf[3] = (void *) (SMP_CORE_GROUP0_BASE + SMP_CORE3_OFFSET + BUF); ipi_mailbox_buf[4] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE0_OFFSET + BUF); ipi_mailbox_buf[5] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE1_OFFSET + BUF); ipi_mailbox_buf[6] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE2_OFFSET + BUF); ipi_mailbox_buf[7] = (void *) (SMP_CORE_GROUP1_BASE + SMP_CORE3_OFFSET + BUF); ipi_mailbox_buf[8] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE0_OFFSET + BUF); ipi_mailbox_buf[9] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE1_OFFSET + BUF); ipi_mailbox_buf[10] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE2_OFFSET + BUF); ipi_mailbox_buf[11] = (void *) (SMP_CORE_GROUP2_BASE + SMP_CORE3_OFFSET + BUF); ipi_mailbox_buf[12] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE0_OFFSET + BUF); ipi_mailbox_buf[13] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE1_OFFSET + BUF); ipi_mailbox_buf[14] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE2_OFFSET + BUF); ipi_mailbox_buf[15] = (void *) (SMP_CORE_GROUP3_BASE + SMP_CORE3_OFFSET + BUF); } /* * Simple enough, just poke the appropriate ipi register */ static void loongson3_send_ipi_single(int cpu, unsigned int action) { ipi_write_action(cpu_logical_map(cpu), (u32)action); } static void loongson3_send_ipi_mask(const struct cpumask *mask, unsigned int action) { unsigned int i; for_each_cpu(i, mask) ipi_write_action(cpu_logical_map(i), (u32)action); } #define IPI_IRQ_OFFSET 6 void loongson3_send_irq_by_ipi(int cpu, int irqs) { ipi_write_action(cpu_logical_map(cpu), irqs << IPI_IRQ_OFFSET); } void loongson3_ipi_interrupt(struct pt_regs *regs) { int i, cpu = smp_processor_id(); unsigned int action, c0count, irqs; action = ipi_read_clear(cpu); irqs = action >> IPI_IRQ_OFFSET; if (action & SMP_RESCHEDULE_YOURSELF) scheduler_ipi(); if (action & SMP_CALL_FUNCTION) { irq_enter(); generic_smp_call_function_interrupt(); irq_exit(); } if (action & SMP_ASK_C0COUNT) { BUG_ON(cpu != 0); c0count = read_c0_count(); c0count = c0count ? c0count : 1; for (i = 1; i < nr_cpu_ids; i++) core0_c0count[i] = c0count; __wbflush(); /* Let others see the result ASAP */ } if (irqs) { int irq; while ((irq = ffs(irqs))) { do_IRQ(irq-1); irqs &= ~(1<<(irq-1)); } } } #define MAX_LOOPS 800 /* * SMP init and finish on secondary CPUs */ static void loongson3_init_secondary(void) { int i; uint32_t initcount; unsigned int cpu = smp_processor_id(); unsigned int imask = STATUSF_IP7 | STATUSF_IP6 | STATUSF_IP3 | STATUSF_IP2; /* Set interrupt mask, but don't enable */ change_c0_status(ST0_IM, imask); for (i = 0; i < num_possible_cpus(); i++) loongson3_ipi_write32(0xffffffff, ipi_en0_regs[cpu_logical_map(i)]); per_cpu(cpu_state, cpu) = CPU_ONLINE; cpu_set_core(&cpu_data[cpu], cpu_logical_map(cpu) % loongson_sysconf.cores_per_package); cpu_data[cpu].package = cpu_logical_map(cpu) / loongson_sysconf.cores_per_package; i = 0; core0_c0count[cpu] = 0; loongson3_send_ipi_single(0, SMP_ASK_C0COUNT); while (!core0_c0count[cpu]) { i++; cpu_relax(); } if (i > MAX_LOOPS) i = MAX_LOOPS; if (cpu_data[cpu].package) initcount = core0_c0count[cpu] + i; else /* Local access is faster for loops */ initcount = core0_c0count[cpu] + i/2; write_c0_count(initcount); } static void loongson3_smp_finish(void) { int cpu = smp_processor_id(); write_c0_compare(read_c0_count() + mips_hpt_frequency/HZ); local_irq_enable(); loongson3_ipi_write64(0, ipi_mailbox_buf[cpu_logical_map(cpu)] + 0x0); pr_info("CPU#%d finished, CP0_ST=%x\n", smp_processor_id(), read_c0_status()); } static void __init loongson3_smp_setup(void) { int i = 0, num = 0; /* i: physical id, num: logical id */ init_cpu_possible(cpu_none_mask); /* For unified kernel, NR_CPUS is the maximum possible value, * loongson_sysconf.nr_cpus is the really present value */ while (i < loongson_sysconf.nr_cpus) { if (loongson_sysconf.reserved_cpus_mask & (1<