xref: /linux/arch/mips/sgi-ip27/ip27-smp.c (revision d595d423d06071bd7a4892c3c2f16bfe1d5b3a85)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * This file is subject to the terms and conditions of the GNU General
31da177e4SLinus Torvalds  * Public License.  See the file "COPYING" in the main directory of this
41da177e4SLinus Torvalds  * archive for more details.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 2000 - 2001 by Kanoj Sarcar (kanoj@sgi.com)
71da177e4SLinus Torvalds  * Copyright (C) 2000 - 2001 by Silicon Graphics, Inc.
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds #include <linux/init.h>
101da177e4SLinus Torvalds #include <linux/sched.h>
11fc69910fSArnd Bergmann #include <linux/sched/task_stack.h>
12ee6a3d19SIngo Molnar #include <linux/topology.h>
131da177e4SLinus Torvalds #include <linux/nodemask.h>
14fc69910fSArnd Bergmann 
151da177e4SLinus Torvalds #include <asm/page.h>
161da177e4SLinus Torvalds #include <asm/processor.h>
17fc69910fSArnd Bergmann #include <asm/ptrace.h>
181da177e4SLinus Torvalds #include <asm/sn/arch.h>
191da177e4SLinus Torvalds #include <asm/sn/gda.h>
201da177e4SLinus Torvalds #include <asm/sn/intr.h>
211da177e4SLinus Torvalds #include <asm/sn/klconfig.h>
221da177e4SLinus Torvalds #include <asm/sn/launch.h>
231da177e4SLinus Torvalds #include <asm/sn/mapped_kernel.h>
241da177e4SLinus Torvalds #include <asm/sn/sn_private.h>
251da177e4SLinus Torvalds #include <asm/sn/types.h>
261da177e4SLinus Torvalds #include <asm/sn/sn0/hubpi.h>
271da177e4SLinus Torvalds #include <asm/sn/sn0/hubio.h>
281da177e4SLinus Torvalds #include <asm/sn/sn0/ip27.h>
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds /*
311da177e4SLinus Torvalds  * Takes as first input the PROM assigned cpu id, and the kernel
321da177e4SLinus Torvalds  * assigned cpu id as the second.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds static void alloc_cpupda(cpuid_t cpu, int cpunum)
351da177e4SLinus Torvalds {
361da177e4SLinus Torvalds 	cnodeid_t node = get_cpu_cnode(cpu);
371da177e4SLinus Torvalds 	nasid_t nasid = COMPACT_TO_NASID_NODEID(node);
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds 	cputonasid(cpunum) = nasid;
40cc6e8e08SRalf Baechle 	sn_cpu_info[cpunum].p_nodeid = node;
411da177e4SLinus Torvalds 	cputoslice(cpunum) = get_cpu_slice(cpu);
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds static nasid_t get_actual_nasid(lboard_t *brd)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	klhub_t *hub;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	if (!brd)
491da177e4SLinus Torvalds 		return INVALID_NASID;
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	/* find out if we are a completely disabled brd. */
521da177e4SLinus Torvalds 	hub  = (klhub_t *)find_first_component(brd, KLSTRUCT_HUB);
531da177e4SLinus Torvalds 	if (!hub)
541da177e4SLinus Torvalds 		return INVALID_NASID;
551da177e4SLinus Torvalds 	if (!(hub->hub_info.flags & KLINFO_ENABLE))	/* disabled node brd */
561da177e4SLinus Torvalds 		return hub->hub_info.physid;
571da177e4SLinus Torvalds 	else
581da177e4SLinus Torvalds 		return brd->brd_nasid;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static int do_cpumask(cnodeid_t cnode, nasid_t nasid, int highest)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	static int tot_cpus_found = 0;
641da177e4SLinus Torvalds 	lboard_t *brd;
651da177e4SLinus Torvalds 	klcpu_t *acpu;
661da177e4SLinus Torvalds 	int cpus_found = 0;
671da177e4SLinus Torvalds 	cpuid_t cpuid;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	brd = find_lboard((lboard_t *)KL_CONFIG_INFO(nasid), KLTYPE_IP27);
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	do {
721da177e4SLinus Torvalds 		acpu = (klcpu_t *)find_first_component(brd, KLSTRUCT_CPU);
731da177e4SLinus Torvalds 		while (acpu) {
741da177e4SLinus Torvalds 			cpuid = acpu->cpu_info.virtid;
751da177e4SLinus Torvalds 			/* cnode is not valid for completely disabled brds */
761da177e4SLinus Torvalds 			if (get_actual_nasid(brd) == brd->brd_nasid)
771da177e4SLinus Torvalds 				cpuid_to_compact_node[cpuid] = cnode;
781da177e4SLinus Torvalds 			if (cpuid > highest)
791da177e4SLinus Torvalds 				highest = cpuid;
801da177e4SLinus Torvalds 			/* Only let it join in if it's marked enabled */
811da177e4SLinus Torvalds 			if ((acpu->cpu_info.flags & KLINFO_ENABLE) &&
821da177e4SLinus Torvalds 			    (tot_cpus_found != NR_CPUS)) {
830b5f9c00SRusty Russell 				set_cpu_possible(cpuid, true);
841da177e4SLinus Torvalds 				alloc_cpupda(cpuid, tot_cpus_found);
851da177e4SLinus Torvalds 				cpus_found++;
861da177e4SLinus Torvalds 				tot_cpus_found++;
871da177e4SLinus Torvalds 			}
881da177e4SLinus Torvalds 			acpu = (klcpu_t *)find_component(brd, (klinfo_t *)acpu,
891da177e4SLinus Torvalds 								KLSTRUCT_CPU);
901da177e4SLinus Torvalds 		}
911da177e4SLinus Torvalds 		brd = KLCF_NEXT(brd);
921da177e4SLinus Torvalds 		if (!brd)
931da177e4SLinus Torvalds 			break;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 		brd = find_lboard(brd, KLTYPE_IP27);
961da177e4SLinus Torvalds 	} while (brd);
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	return highest;
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds void cpu_node_probe(void)
1021da177e4SLinus Torvalds {
1031da177e4SLinus Torvalds 	int i, highest = 0;
1041da177e4SLinus Torvalds 	gda_t *gdap = GDA;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	/*
1071da177e4SLinus Torvalds 	 * Initialize the arrays to invalid nodeid (-1)
1081da177e4SLinus Torvalds 	 */
1091da177e4SLinus Torvalds 	for (i = 0; i < MAX_COMPACT_NODES; i++)
1101da177e4SLinus Torvalds 		compact_to_nasid_node[i] = INVALID_NASID;
1111da177e4SLinus Torvalds 	for (i = 0; i < MAX_NASIDS; i++)
1121da177e4SLinus Torvalds 		nasid_to_compact_node[i] = INVALID_CNODEID;
1131da177e4SLinus Torvalds 	for (i = 0; i < MAXCPUS; i++)
1141da177e4SLinus Torvalds 		cpuid_to_compact_node[i] = INVALID_CNODEID;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	/*
1171da177e4SLinus Torvalds 	 * MCD - this whole "compact node" stuff can probably be dropped,
1181da177e4SLinus Torvalds 	 * as we can handle sparse numbering now
1191da177e4SLinus Torvalds 	 */
1201da177e4SLinus Torvalds 	nodes_clear(node_online_map);
1211da177e4SLinus Torvalds 	for (i = 0; i < MAX_COMPACT_NODES; i++) {
1221da177e4SLinus Torvalds 		nasid_t nasid = gdap->g_nasidtable[i];
1231da177e4SLinus Torvalds 		if (nasid == INVALID_NASID)
1241da177e4SLinus Torvalds 			break;
1251da177e4SLinus Torvalds 		compact_to_nasid_node[i] = nasid;
1261da177e4SLinus Torvalds 		nasid_to_compact_node[nasid] = i;
1271da177e4SLinus Torvalds 		node_set_online(num_online_nodes());
1281da177e4SLinus Torvalds 		highest = do_cpumask(i, nasid, highest);
1291da177e4SLinus Torvalds 	}
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	printk("Discovered %d cpus on %d nodes\n", highest + 1, num_online_nodes());
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
13439408c6aSRalf Baechle static __init void intr_clear_all(nasid_t nasid)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	int i;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	REMOTE_HUB_S(nasid, PI_INT_MASK0_A, 0);
1391da177e4SLinus Torvalds 	REMOTE_HUB_S(nasid, PI_INT_MASK0_B, 0);
1401da177e4SLinus Torvalds 	REMOTE_HUB_S(nasid, PI_INT_MASK1_A, 0);
1411da177e4SLinus Torvalds 	REMOTE_HUB_S(nasid, PI_INT_MASK1_B, 0);
14239408c6aSRalf Baechle 
14339408c6aSRalf Baechle 	for (i = 0; i < 128; i++)
14439408c6aSRalf Baechle 		REMOTE_HUB_CLR_INTR(nasid, i);
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
14787353d8aSRalf Baechle static void ip27_send_ipi_single(int destid, unsigned int action)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	int irq;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	switch (action) {
1521da177e4SLinus Torvalds 	case SMP_RESCHEDULE_YOURSELF:
1531da177e4SLinus Torvalds 		irq = CPU_RESCHED_A_IRQ;
1541da177e4SLinus Torvalds 		break;
1551da177e4SLinus Torvalds 	case SMP_CALL_FUNCTION:
1561da177e4SLinus Torvalds 		irq = CPU_CALL_A_IRQ;
1571da177e4SLinus Torvalds 		break;
1581da177e4SLinus Torvalds 	default:
1591da177e4SLinus Torvalds 		panic("sendintr");
1601da177e4SLinus Torvalds 	}
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	irq += cputoslice(destid);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	/*
1651da177e4SLinus Torvalds 	 * Convert the compact hub number to the NASID to get the correct
1661da177e4SLinus Torvalds 	 * part of the address space.  Then set the interrupt bit associated
1671da177e4SLinus Torvalds 	 * with the CPU we want to send the interrupt to.
1681da177e4SLinus Torvalds 	 */
1691da177e4SLinus Torvalds 	REMOTE_HUB_SEND_INTR(COMPACT_TO_NASID_NODEID(cpu_to_node(destid)), irq);
1701da177e4SLinus Torvalds }
17187353d8aSRalf Baechle 
172b533e652SRalf Baechle static void ip27_send_ipi_mask(const struct cpumask *mask, unsigned int action)
17387353d8aSRalf Baechle {
17487353d8aSRalf Baechle 	unsigned int i;
17587353d8aSRalf Baechle 
17648a048feSRusty Russell 	for_each_cpu(i, mask)
17787353d8aSRalf Baechle 		ip27_send_ipi_single(i, action);
17887353d8aSRalf Baechle }
17987353d8aSRalf Baechle 
180078a55fcSPaul Gortmaker static void ip27_init_secondary(void)
18187353d8aSRalf Baechle {
18287353d8aSRalf Baechle 	per_cpu_init();
18387353d8aSRalf Baechle }
18487353d8aSRalf Baechle 
185078a55fcSPaul Gortmaker static void ip27_smp_finish(void)
18687353d8aSRalf Baechle {
187b32bb803SThomas Bogendoerfer 	extern void hub_rt_clock_event_init(void);
188b32bb803SThomas Bogendoerfer 
189b32bb803SThomas Bogendoerfer 	hub_rt_clock_event_init();
190b32bb803SThomas Bogendoerfer 	local_irq_enable();
19187353d8aSRalf Baechle }
19287353d8aSRalf Baechle 
19387353d8aSRalf Baechle /*
19487353d8aSRalf Baechle  * Launch a slave into smp_bootstrap().	 It doesn't take an argument, and we
19587353d8aSRalf Baechle  * set sp to the kernel stack of the newly created idle process, gp to the proc
19687353d8aSRalf Baechle  * struct so that current_thread_info() will work.
19787353d8aSRalf Baechle  */
198*d595d423SPaul Burton static int ip27_boot_secondary(int cpu, struct task_struct *idle)
19987353d8aSRalf Baechle {
20087353d8aSRalf Baechle 	unsigned long gp = (unsigned long)task_thread_info(idle);
20187353d8aSRalf Baechle 	unsigned long sp = __KSTK_TOS(idle);
20287353d8aSRalf Baechle 
20387353d8aSRalf Baechle 	LAUNCH_SLAVE(cputonasid(cpu), cputoslice(cpu),
20487353d8aSRalf Baechle 		(launch_proc_t)MAPPED_KERN_RW_TO_K0(smp_bootstrap),
20587353d8aSRalf Baechle 		0, (void *) sp, (void *) gp);
206*d595d423SPaul Burton 	return 0;
20787353d8aSRalf Baechle }
20887353d8aSRalf Baechle 
20987353d8aSRalf Baechle static void __init ip27_smp_setup(void)
21087353d8aSRalf Baechle {
21187353d8aSRalf Baechle 	cnodeid_t	cnode;
21287353d8aSRalf Baechle 
21387353d8aSRalf Baechle 	for_each_online_node(cnode) {
21487353d8aSRalf Baechle 		if (cnode == 0)
21587353d8aSRalf Baechle 			continue;
21687353d8aSRalf Baechle 		intr_clear_all(COMPACT_TO_NASID_NODEID(cnode));
21787353d8aSRalf Baechle 	}
21887353d8aSRalf Baechle 
21987353d8aSRalf Baechle 	replicate_kernel_text();
22087353d8aSRalf Baechle 
22187353d8aSRalf Baechle 	/*
22287353d8aSRalf Baechle 	 * Assumption to be fixed: we're always booted on logical / physical
22387353d8aSRalf Baechle 	 * processor 0.	 While we're always running on logical processor 0
22487353d8aSRalf Baechle 	 * this still means this is physical processor zero; it might for
225877d0310SNick Andrew 	 * example be disabled in the firmware.
22687353d8aSRalf Baechle 	 */
22787353d8aSRalf Baechle 	alloc_cpupda(0, 0);
22887353d8aSRalf Baechle }
22987353d8aSRalf Baechle 
23087353d8aSRalf Baechle static void __init ip27_prepare_cpus(unsigned int max_cpus)
23187353d8aSRalf Baechle {
23287353d8aSRalf Baechle 	/* We already did everything necessary earlier */
23387353d8aSRalf Baechle }
23487353d8aSRalf Baechle 
235ff2c8252SMatt Redfearn const struct plat_smp_ops ip27_smp_ops = {
23687353d8aSRalf Baechle 	.send_ipi_single	= ip27_send_ipi_single,
23787353d8aSRalf Baechle 	.send_ipi_mask		= ip27_send_ipi_mask,
23887353d8aSRalf Baechle 	.init_secondary		= ip27_init_secondary,
23987353d8aSRalf Baechle 	.smp_finish		= ip27_smp_finish,
24087353d8aSRalf Baechle 	.boot_secondary		= ip27_boot_secondary,
24187353d8aSRalf Baechle 	.smp_setup		= ip27_smp_setup,
24287353d8aSRalf Baechle 	.prepare_cpus		= ip27_prepare_cpus,
24387353d8aSRalf Baechle };
244