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