18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2003-2011 NetLogic Microsystems, Inc. (NetLogic). All rights
38c2ecf20Sopenharmony_ci * reserved.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
68c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
78c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
88c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the NetLogic
98c2ecf20Sopenharmony_ci * license below:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
128c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
138c2ecf20Sopenharmony_ci * are met:
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
168c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
178c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
188c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in
198c2ecf20Sopenharmony_ci *    the documentation and/or other materials provided with the
208c2ecf20Sopenharmony_ci *    distribution.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY NETLOGIC ``AS IS'' AND ANY EXPRESS OR
238c2ecf20Sopenharmony_ci * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
248c2ecf20Sopenharmony_ci * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
258c2ecf20Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL NETLOGIC OR CONTRIBUTORS BE LIABLE
268c2ecf20Sopenharmony_ci * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
278c2ecf20Sopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
288c2ecf20Sopenharmony_ci * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
298c2ecf20Sopenharmony_ci * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
308c2ecf20Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
318c2ecf20Sopenharmony_ci * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
328c2ecf20Sopenharmony_ci * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <linux/kernel.h>
368c2ecf20Sopenharmony_ci#include <linux/delay.h>
378c2ecf20Sopenharmony_ci#include <linux/init.h>
388c2ecf20Sopenharmony_ci#include <linux/sched/task_stack.h>
398c2ecf20Sopenharmony_ci#include <linux/smp.h>
408c2ecf20Sopenharmony_ci#include <linux/irq.h>
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#include <asm/mmu_context.h>
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#include <asm/netlogic/interrupt.h>
458c2ecf20Sopenharmony_ci#include <asm/netlogic/mips-extns.h>
468c2ecf20Sopenharmony_ci#include <asm/netlogic/haldefs.h>
478c2ecf20Sopenharmony_ci#include <asm/netlogic/common.h>
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#if defined(CONFIG_CPU_XLP)
508c2ecf20Sopenharmony_ci#include <asm/netlogic/xlp-hal/iomap.h>
518c2ecf20Sopenharmony_ci#include <asm/netlogic/xlp-hal/xlp.h>
528c2ecf20Sopenharmony_ci#include <asm/netlogic/xlp-hal/pic.h>
538c2ecf20Sopenharmony_ci#elif defined(CONFIG_CPU_XLR)
548c2ecf20Sopenharmony_ci#include <asm/netlogic/xlr/iomap.h>
558c2ecf20Sopenharmony_ci#include <asm/netlogic/xlr/pic.h>
568c2ecf20Sopenharmony_ci#include <asm/netlogic/xlr/xlr.h>
578c2ecf20Sopenharmony_ci#else
588c2ecf20Sopenharmony_ci#error "Unknown CPU"
598c2ecf20Sopenharmony_ci#endif
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_civoid nlm_send_ipi_single(int logical_cpu, unsigned int action)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	unsigned int hwtid;
648c2ecf20Sopenharmony_ci	uint64_t picbase;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	/* node id is part of hwtid, and needed for send_ipi */
678c2ecf20Sopenharmony_ci	hwtid = cpu_logical_map(logical_cpu);
688c2ecf20Sopenharmony_ci	picbase = nlm_get_node(nlm_hwtid_to_node(hwtid))->picbase;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	if (action & SMP_CALL_FUNCTION)
718c2ecf20Sopenharmony_ci		nlm_pic_send_ipi(picbase, hwtid, IRQ_IPI_SMP_FUNCTION, 0);
728c2ecf20Sopenharmony_ci	if (action & SMP_RESCHEDULE_YOURSELF)
738c2ecf20Sopenharmony_ci		nlm_pic_send_ipi(picbase, hwtid, IRQ_IPI_SMP_RESCHEDULE, 0);
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_civoid nlm_send_ipi_mask(const struct cpumask *mask, unsigned int action)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	int cpu;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	for_each_cpu(cpu, mask) {
818c2ecf20Sopenharmony_ci		nlm_send_ipi_single(cpu, action);
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/* IRQ_IPI_SMP_FUNCTION Handler */
868c2ecf20Sopenharmony_civoid nlm_smp_function_ipi_handler(struct irq_desc *desc)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	unsigned int irq = irq_desc_get_irq(desc);
898c2ecf20Sopenharmony_ci	clear_c0_eimr(irq);
908c2ecf20Sopenharmony_ci	ack_c0_eirr(irq);
918c2ecf20Sopenharmony_ci	generic_smp_call_function_interrupt();
928c2ecf20Sopenharmony_ci	set_c0_eimr(irq);
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci/* IRQ_IPI_SMP_RESCHEDULE  handler */
968c2ecf20Sopenharmony_civoid nlm_smp_resched_ipi_handler(struct irq_desc *desc)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	unsigned int irq = irq_desc_get_irq(desc);
998c2ecf20Sopenharmony_ci	clear_c0_eimr(irq);
1008c2ecf20Sopenharmony_ci	ack_c0_eirr(irq);
1018c2ecf20Sopenharmony_ci	scheduler_ipi();
1028c2ecf20Sopenharmony_ci	set_c0_eimr(irq);
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci/*
1068c2ecf20Sopenharmony_ci * Called before going into mips code, early cpu init
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_civoid nlm_early_init_secondary(int cpu)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	change_c0_config(CONF_CM_CMASK, 0x3);
1118c2ecf20Sopenharmony_ci#ifdef CONFIG_CPU_XLP
1128c2ecf20Sopenharmony_ci	xlp_mmu_init();
1138c2ecf20Sopenharmony_ci#endif
1148c2ecf20Sopenharmony_ci	write_c0_ebase(nlm_current_node()->ebase);
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/*
1188c2ecf20Sopenharmony_ci * Code to run on secondary just after probing the CPU
1198c2ecf20Sopenharmony_ci */
1208c2ecf20Sopenharmony_cistatic void nlm_init_secondary(void)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	int hwtid;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	hwtid = hard_smp_processor_id();
1258c2ecf20Sopenharmony_ci	cpu_set_core(&current_cpu_data, hwtid / NLM_THREADS_PER_CORE);
1268c2ecf20Sopenharmony_ci	current_cpu_data.package = nlm_nodeid();
1278c2ecf20Sopenharmony_ci	nlm_percpu_init(hwtid);
1288c2ecf20Sopenharmony_ci	nlm_smp_irq_init(hwtid);
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_civoid nlm_prepare_cpus(unsigned int max_cpus)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	/* declare we are SMT capable */
1348c2ecf20Sopenharmony_ci	smp_num_siblings = nlm_threads_per_core;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_civoid nlm_smp_finish(void)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	local_irq_enable();
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/*
1438c2ecf20Sopenharmony_ci * Boot all other cpus in the system, initialize them, and bring them into
1448c2ecf20Sopenharmony_ci * the boot function
1458c2ecf20Sopenharmony_ci */
1468c2ecf20Sopenharmony_ciunsigned long nlm_next_gp;
1478c2ecf20Sopenharmony_ciunsigned long nlm_next_sp;
1488c2ecf20Sopenharmony_cistatic cpumask_t phys_cpu_present_mask;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ciint nlm_boot_secondary(int logical_cpu, struct task_struct *idle)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	uint64_t picbase;
1538c2ecf20Sopenharmony_ci	int hwtid;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	hwtid = cpu_logical_map(logical_cpu);
1568c2ecf20Sopenharmony_ci	picbase = nlm_get_node(nlm_hwtid_to_node(hwtid))->picbase;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	nlm_next_sp = (unsigned long)__KSTK_TOS(idle);
1598c2ecf20Sopenharmony_ci	nlm_next_gp = (unsigned long)task_thread_info(idle);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	/* barrier for sp/gp store above */
1628c2ecf20Sopenharmony_ci	__sync();
1638c2ecf20Sopenharmony_ci	nlm_pic_send_ipi(picbase, hwtid, 1, 1);  /* NMI */
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	return 0;
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_civoid __init nlm_smp_setup(void)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	unsigned int boot_cpu;
1718c2ecf20Sopenharmony_ci	int num_cpus, i, ncore, node;
1728c2ecf20Sopenharmony_ci	volatile u32 *cpu_ready = nlm_get_boot_data(BOOT_CPU_READY);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	boot_cpu = hard_smp_processor_id();
1758c2ecf20Sopenharmony_ci	cpumask_clear(&phys_cpu_present_mask);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	cpumask_set_cpu(boot_cpu, &phys_cpu_present_mask);
1788c2ecf20Sopenharmony_ci	__cpu_number_map[boot_cpu] = 0;
1798c2ecf20Sopenharmony_ci	__cpu_logical_map[0] = boot_cpu;
1808c2ecf20Sopenharmony_ci	set_cpu_possible(0, true);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	num_cpus = 1;
1838c2ecf20Sopenharmony_ci	for (i = 0; i < NR_CPUS; i++) {
1848c2ecf20Sopenharmony_ci		/*
1858c2ecf20Sopenharmony_ci		 * cpu_ready array is not set for the boot_cpu,
1868c2ecf20Sopenharmony_ci		 * it is only set for ASPs (see smpboot.S)
1878c2ecf20Sopenharmony_ci		 */
1888c2ecf20Sopenharmony_ci		if (cpu_ready[i]) {
1898c2ecf20Sopenharmony_ci			cpumask_set_cpu(i, &phys_cpu_present_mask);
1908c2ecf20Sopenharmony_ci			__cpu_number_map[i] = num_cpus;
1918c2ecf20Sopenharmony_ci			__cpu_logical_map[num_cpus] = i;
1928c2ecf20Sopenharmony_ci			set_cpu_possible(num_cpus, true);
1938c2ecf20Sopenharmony_ci			node = nlm_hwtid_to_node(i);
1948c2ecf20Sopenharmony_ci			cpumask_set_cpu(num_cpus, &nlm_get_node(node)->cpumask);
1958c2ecf20Sopenharmony_ci			++num_cpus;
1968c2ecf20Sopenharmony_ci		}
1978c2ecf20Sopenharmony_ci	}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	pr_info("Physical CPU mask: %*pb\n",
2008c2ecf20Sopenharmony_ci		cpumask_pr_args(&phys_cpu_present_mask));
2018c2ecf20Sopenharmony_ci	pr_info("Possible CPU mask: %*pb\n",
2028c2ecf20Sopenharmony_ci		cpumask_pr_args(cpu_possible_mask));
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	/* check with the cores we have woken up */
2058c2ecf20Sopenharmony_ci	for (ncore = 0, i = 0; i < NLM_NR_NODES; i++)
2068c2ecf20Sopenharmony_ci		ncore += hweight32(nlm_get_node(i)->coremask);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	pr_info("Detected (%dc%dt) %d Slave CPU(s)\n", ncore,
2098c2ecf20Sopenharmony_ci		nlm_threads_per_core, num_cpus);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	/* switch NMI handler to boot CPUs */
2128c2ecf20Sopenharmony_ci	nlm_set_nmi_handler(nlm_boot_secondary_cpus);
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic int nlm_parse_cpumask(cpumask_t *wakeup_mask)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	uint32_t core0_thr_mask, core_thr_mask;
2188c2ecf20Sopenharmony_ci	int threadmode, i, j;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	core0_thr_mask = 0;
2218c2ecf20Sopenharmony_ci	for (i = 0; i < NLM_THREADS_PER_CORE; i++)
2228c2ecf20Sopenharmony_ci		if (cpumask_test_cpu(i, wakeup_mask))
2238c2ecf20Sopenharmony_ci			core0_thr_mask |= (1 << i);
2248c2ecf20Sopenharmony_ci	switch (core0_thr_mask) {
2258c2ecf20Sopenharmony_ci	case 1:
2268c2ecf20Sopenharmony_ci		nlm_threads_per_core = 1;
2278c2ecf20Sopenharmony_ci		threadmode = 0;
2288c2ecf20Sopenharmony_ci		break;
2298c2ecf20Sopenharmony_ci	case 3:
2308c2ecf20Sopenharmony_ci		nlm_threads_per_core = 2;
2318c2ecf20Sopenharmony_ci		threadmode = 2;
2328c2ecf20Sopenharmony_ci		break;
2338c2ecf20Sopenharmony_ci	case 0xf:
2348c2ecf20Sopenharmony_ci		nlm_threads_per_core = 4;
2358c2ecf20Sopenharmony_ci		threadmode = 3;
2368c2ecf20Sopenharmony_ci		break;
2378c2ecf20Sopenharmony_ci	default:
2388c2ecf20Sopenharmony_ci		goto unsupp;
2398c2ecf20Sopenharmony_ci	}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	/* Verify other cores CPU masks */
2428c2ecf20Sopenharmony_ci	for (i = 0; i < NR_CPUS; i += NLM_THREADS_PER_CORE) {
2438c2ecf20Sopenharmony_ci		core_thr_mask = 0;
2448c2ecf20Sopenharmony_ci		for (j = 0; j < NLM_THREADS_PER_CORE; j++)
2458c2ecf20Sopenharmony_ci			if (cpumask_test_cpu(i + j, wakeup_mask))
2468c2ecf20Sopenharmony_ci				core_thr_mask |= (1 << j);
2478c2ecf20Sopenharmony_ci		if (core_thr_mask != 0 && core_thr_mask != core0_thr_mask)
2488c2ecf20Sopenharmony_ci				goto unsupp;
2498c2ecf20Sopenharmony_ci	}
2508c2ecf20Sopenharmony_ci	return threadmode;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ciunsupp:
2538c2ecf20Sopenharmony_ci	panic("Unsupported CPU mask %*pb", cpumask_pr_args(wakeup_mask));
2548c2ecf20Sopenharmony_ci	return 0;
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ciint nlm_wakeup_secondary_cpus(void)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	u32 *reset_data;
2608c2ecf20Sopenharmony_ci	int threadmode;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	/* verify the mask and setup core config variables */
2638c2ecf20Sopenharmony_ci	threadmode = nlm_parse_cpumask(&nlm_cpumask);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	/* Setup CPU init parameters */
2668c2ecf20Sopenharmony_ci	reset_data = nlm_get_boot_data(BOOT_THREAD_MODE);
2678c2ecf20Sopenharmony_ci	*reset_data = threadmode;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci#ifdef CONFIG_CPU_XLP
2708c2ecf20Sopenharmony_ci	xlp_wakeup_secondary_cpus();
2718c2ecf20Sopenharmony_ci#else
2728c2ecf20Sopenharmony_ci	xlr_wakeup_secondary_cpus();
2738c2ecf20Sopenharmony_ci#endif
2748c2ecf20Sopenharmony_ci	return 0;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ciconst struct plat_smp_ops nlm_smp_ops = {
2788c2ecf20Sopenharmony_ci	.send_ipi_single	= nlm_send_ipi_single,
2798c2ecf20Sopenharmony_ci	.send_ipi_mask		= nlm_send_ipi_mask,
2808c2ecf20Sopenharmony_ci	.init_secondary		= nlm_init_secondary,
2818c2ecf20Sopenharmony_ci	.smp_finish		= nlm_smp_finish,
2828c2ecf20Sopenharmony_ci	.boot_secondary		= nlm_boot_secondary,
2838c2ecf20Sopenharmony_ci	.smp_setup		= nlm_smp_setup,
2848c2ecf20Sopenharmony_ci	.prepare_cpus		= nlm_prepare_cpus,
2858c2ecf20Sopenharmony_ci};
286