xref: /openbmc/linux/arch/mips/sgi-ip27/ip27-klnuma.c (revision b2441318)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Ported from IRIX to Linux by Kanoj Sarcar, 06/08/00.
41da177e4SLinus Torvalds  * Copyright 2000 - 2001 Silicon Graphics, Inc.
51da177e4SLinus Torvalds  * Copyright 2000 - 2001 Kanoj Sarcar (kanoj@sgi.com)
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds #include <linux/init.h>
827ac792cSAndrea Righi #include <linux/mm.h>
91da177e4SLinus Torvalds #include <linux/mmzone.h>
101da177e4SLinus Torvalds #include <linux/kernel.h>
111da177e4SLinus Torvalds #include <linux/nodemask.h>
121da177e4SLinus Torvalds #include <linux/string.h>
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <asm/page.h>
151da177e4SLinus Torvalds #include <asm/sections.h>
161da177e4SLinus Torvalds #include <asm/sn/types.h>
171da177e4SLinus Torvalds #include <asm/sn/arch.h>
181da177e4SLinus Torvalds #include <asm/sn/gda.h>
191da177e4SLinus Torvalds #include <asm/sn/hub.h>
201da177e4SLinus Torvalds #include <asm/sn/mapped_kernel.h>
211da177e4SLinus Torvalds #include <asm/sn/sn_private.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds static cpumask_t ktext_repmask;
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /*
261da177e4SLinus Torvalds  * XXX - This needs to be much smarter about where it puts copies of the
271da177e4SLinus Torvalds  * kernel.  For example, we should never put a copy on a headless node,
281da177e4SLinus Torvalds  * and we should respect the topology of the machine.
291da177e4SLinus Torvalds  */
30c11b3c1bSRalf Baechle void __init setup_replication_mask(void)
311da177e4SLinus Torvalds {
321da177e4SLinus Torvalds 	/* Set only the master cnode's bit.  The master cnode is always 0. */
338dd92891SRusty Russell 	cpumask_clear(&ktext_repmask);
348dd92891SRusty Russell 	cpumask_set_cpu(0, &ktext_repmask);
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #ifdef CONFIG_REPLICATE_KTEXT
371da177e4SLinus Torvalds #ifndef CONFIG_MAPPED_KERNEL
381da177e4SLinus Torvalds #error Kernel replication works with mapped kernel support. No calias support.
391da177e4SLinus Torvalds #endif
4089c96096SRalf Baechle 	{
4189c96096SRalf Baechle 		cnodeid_t	cnode;
4289c96096SRalf Baechle 
431da177e4SLinus Torvalds 		for_each_online_node(cnode) {
441da177e4SLinus Torvalds 			if (cnode == 0)
451da177e4SLinus Torvalds 				continue;
461da177e4SLinus Torvalds 			/* Advertise that we have a copy of the kernel */
478dd92891SRusty Russell 			cpumask_set_cpu(cnode, &ktext_repmask);
481da177e4SLinus Torvalds 		}
4989c96096SRalf Baechle 	}
501da177e4SLinus Torvalds #endif
511da177e4SLinus Torvalds 	/* Set up a GDA pointer to the replication mask. */
521da177e4SLinus Torvalds 	GDA->g_ktext_repmask = &ktext_repmask;
531da177e4SLinus Torvalds }
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds static __init void set_ktext_source(nasid_t client_nasid, nasid_t server_nasid)
571da177e4SLinus Torvalds {
581da177e4SLinus Torvalds 	kern_vars_t *kvp;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	kvp = &hub_data(client_nasid)->kern_vars;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	KERN_VARS_ADDR(client_nasid) = (unsigned long)kvp;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	kvp->kv_magic = KV_MAGIC;
651da177e4SLinus Torvalds 	kvp->kv_ro_nasid = server_nasid;
661da177e4SLinus Torvalds 	kvp->kv_rw_nasid = master_nasid;
671da177e4SLinus Torvalds 	kvp->kv_ro_baseaddr = NODE_CAC_BASE(server_nasid);
681da177e4SLinus Torvalds 	kvp->kv_rw_baseaddr = NODE_CAC_BASE(master_nasid);
691da177e4SLinus Torvalds 	printk("REPLICATION: ON nasid %d, ktext from nasid %d, kdata from nasid %d\n", client_nasid, server_nasid, master_nasid);
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /* XXX - When the BTE works, we should use it instead of this. */
731da177e4SLinus Torvalds static __init void copy_kernel(nasid_t dest_nasid)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	unsigned long dest_kern_start, source_start, source_end, kern_size;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	source_start = (unsigned long) _stext;
781da177e4SLinus Torvalds 	source_end = (unsigned long) _etext;
791da177e4SLinus Torvalds 	kern_size = source_end - source_start;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	dest_kern_start = CHANGE_ADDR_NASID(MAPPED_KERN_RO_TO_K0(source_start),
821da177e4SLinus Torvalds 					    dest_nasid);
831da177e4SLinus Torvalds 	memcpy((void *)dest_kern_start, (void *)source_start, kern_size);
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
8642b76a1dSArnd Bergmann void __init replicate_kernel_text(void)
871da177e4SLinus Torvalds {
881da177e4SLinus Torvalds 	cnodeid_t cnode;
891da177e4SLinus Torvalds 	nasid_t client_nasid;
901da177e4SLinus Torvalds 	nasid_t server_nasid;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	server_nasid = master_nasid;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	/* Record where the master node should get its kernel text */
951da177e4SLinus Torvalds 	set_ktext_source(master_nasid, master_nasid);
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	for_each_online_node(cnode) {
981da177e4SLinus Torvalds 		if (cnode == 0)
991da177e4SLinus Torvalds 			continue;
1001da177e4SLinus Torvalds 		client_nasid = COMPACT_TO_NASID_NODEID(cnode);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 		/* Check if this node should get a copy of the kernel */
1038dd92891SRusty Russell 		if (cpumask_test_cpu(cnode, &ktext_repmask)) {
1041da177e4SLinus Torvalds 			server_nasid = client_nasid;
1051da177e4SLinus Torvalds 			copy_kernel(server_nasid);
1061da177e4SLinus Torvalds 		}
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 		/* Record where this node should get its kernel text */
1091da177e4SLinus Torvalds 		set_ktext_source(client_nasid, server_nasid);
1101da177e4SLinus Torvalds 	}
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /*
1141da177e4SLinus Torvalds  * Return pfn of first free page of memory on a node. PROM may allocate
1151da177e4SLinus Torvalds  * data structures on the first couple of pages of the first slot of each
1161da177e4SLinus Torvalds  * node. If this is the case, getfirstfree(node) > getslotstart(node, 0).
1171da177e4SLinus Torvalds  */
118fc0460d0SRalf Baechle unsigned long node_getfirstfree(cnodeid_t cnode)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	unsigned long loadbase = REP_BASE;
1211da177e4SLinus Torvalds 	nasid_t nasid = COMPACT_TO_NASID_NODEID(cnode);
1221da177e4SLinus Torvalds 	unsigned long offset;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds #ifdef CONFIG_MAPPED_KERNEL
1251da177e4SLinus Torvalds 	loadbase += 16777216;
1261da177e4SLinus Torvalds #endif
1271da177e4SLinus Torvalds 	offset = PAGE_ALIGN((unsigned long)(&_end)) - loadbase;
1288dd92891SRusty Russell 	if ((cnode == 0) || (cpumask_test_cpu(cnode, &ktext_repmask)))
129635c9907SRalf Baechle 		return TO_NODE(nasid, offset) >> PAGE_SHIFT;
1301da177e4SLinus Torvalds 	else
131635c9907SRalf Baechle 		return KDM_TO_PHYS(PAGE_ALIGN(SYMMON_STK_ADDR(nasid, 0))) >> PAGE_SHIFT;
1321da177e4SLinus Torvalds }
133