xref: /openbmc/linux/arch/mips/sgi-ip27/ip27-klnuma.c (revision b78e9d63)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Ported from IRIX to Linux by Kanoj Sarcar, 06/08/00.
4  * Copyright 2000 - 2001 Silicon Graphics, Inc.
5  * Copyright 2000 - 2001 Kanoj Sarcar (kanoj@sgi.com)
6  */
7 #include <linux/init.h>
8 #include <linux/mm.h>
9 #include <linux/mmzone.h>
10 #include <linux/kernel.h>
11 #include <linux/nodemask.h>
12 #include <linux/string.h>
13 
14 #include <asm/page.h>
15 #include <asm/sections.h>
16 #include <asm/sn/types.h>
17 #include <asm/sn/arch.h>
18 #include <asm/sn/gda.h>
19 #include <asm/sn/mapped_kernel.h>
20 #include <asm/sn/sn_private.h>
21 
22 static nodemask_t ktext_repmask;
23 
24 /*
25  * XXX - This needs to be much smarter about where it puts copies of the
26  * kernel.  For example, we should never put a copy on a headless node,
27  * and we should respect the topology of the machine.
28  */
29 void __init setup_replication_mask(void)
30 {
31 	/* Set only the master cnode's bit.  The master cnode is always 0. */
32 	nodes_clear(ktext_repmask);
33 	node_set(0, ktext_repmask);
34 
35 #ifdef CONFIG_REPLICATE_KTEXT
36 #ifndef CONFIG_MAPPED_KERNEL
37 #error Kernel replication works with mapped kernel support. No calias support.
38 #endif
39 	{
40 		nasid_t nasid;
41 
42 		for_each_online_node(nasid) {
43 			if (nasid == 0)
44 				continue;
45 			/* Advertise that we have a copy of the kernel */
46 			node_set(nasid, ktext_repmask);
47 		}
48 	}
49 #endif
50 	/* Set up a GDA pointer to the replication mask. */
51 	GDA->g_ktext_repmask = &ktext_repmask;
52 }
53 
54 
55 static __init void set_ktext_source(nasid_t client_nasid, nasid_t server_nasid)
56 {
57 	kern_vars_t *kvp;
58 
59 	kvp = &hub_data(client_nasid)->kern_vars;
60 
61 	KERN_VARS_ADDR(client_nasid) = (unsigned long)kvp;
62 
63 	kvp->kv_magic = KV_MAGIC;
64 	kvp->kv_ro_nasid = server_nasid;
65 	kvp->kv_rw_nasid = master_nasid;
66 	kvp->kv_ro_baseaddr = NODE_CAC_BASE(server_nasid);
67 	kvp->kv_rw_baseaddr = NODE_CAC_BASE(master_nasid);
68 	printk("REPLICATION: ON nasid %d, ktext from nasid %d, kdata from nasid %d\n", client_nasid, server_nasid, master_nasid);
69 }
70 
71 /* XXX - When the BTE works, we should use it instead of this. */
72 static __init void copy_kernel(nasid_t dest_nasid)
73 {
74 	unsigned long dest_kern_start, source_start, source_end, kern_size;
75 
76 	source_start = (unsigned long) _stext;
77 	source_end = (unsigned long) _etext;
78 	kern_size = source_end - source_start;
79 
80 	dest_kern_start = CHANGE_ADDR_NASID(MAPPED_KERN_RO_TO_K0(source_start),
81 					    dest_nasid);
82 	memcpy((void *)dest_kern_start, (void *)source_start, kern_size);
83 }
84 
85 void __init replicate_kernel_text(void)
86 {
87 	nasid_t client_nasid;
88 	nasid_t server_nasid;
89 
90 	server_nasid = master_nasid;
91 
92 	/* Record where the master node should get its kernel text */
93 	set_ktext_source(master_nasid, master_nasid);
94 
95 	for_each_online_node(client_nasid) {
96 		if (client_nasid == 0)
97 			continue;
98 
99 		/* Check if this node should get a copy of the kernel */
100 		if (node_isset(client_nasid, ktext_repmask)) {
101 			server_nasid = client_nasid;
102 			copy_kernel(server_nasid);
103 		}
104 
105 		/* Record where this node should get its kernel text */
106 		set_ktext_source(client_nasid, server_nasid);
107 	}
108 }
109 
110 /*
111  * Return pfn of first free page of memory on a node. PROM may allocate
112  * data structures on the first couple of pages of the first slot of each
113  * node. If this is the case, getfirstfree(node) > getslotstart(node, 0).
114  */
115 unsigned long node_getfirstfree(nasid_t nasid)
116 {
117 	unsigned long loadbase = REP_BASE;
118 	unsigned long offset;
119 
120 #ifdef CONFIG_MAPPED_KERNEL
121 	loadbase += 16777216;
122 #endif
123 	offset = PAGE_ALIGN((unsigned long)(&_end)) - loadbase;
124 	if ((nasid == 0) || (node_isset(nasid, ktext_repmask)))
125 		return TO_NODE(nasid, offset) >> PAGE_SHIFT;
126 	else
127 		return KDM_TO_PHYS(PAGE_ALIGN(SYMMON_STK_ADDR(nasid, 0))) >> PAGE_SHIFT;
128 }
129