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