145d9ca49SDean Nelson /* 245d9ca49SDean Nelson * This file is subject to the terms and conditions of the GNU General Public 345d9ca49SDean Nelson * License. See the file "COPYING" in the main directory of this archive 445d9ca49SDean Nelson * for more details. 545d9ca49SDean Nelson * 645d9ca49SDean Nelson * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved. 745d9ca49SDean Nelson */ 845d9ca49SDean Nelson 945d9ca49SDean Nelson /* 1045d9ca49SDean Nelson * Cross Partition Communication (XPC) partition support. 1145d9ca49SDean Nelson * 1245d9ca49SDean Nelson * This is the part of XPC that detects the presence/absence of 1345d9ca49SDean Nelson * other partitions. It provides a heartbeat and monitors the 1445d9ca49SDean Nelson * heartbeats of other partitions. 1545d9ca49SDean Nelson * 1645d9ca49SDean Nelson */ 1745d9ca49SDean Nelson 1845d9ca49SDean Nelson #include <linux/kernel.h> 1945d9ca49SDean Nelson #include <linux/sysctl.h> 2045d9ca49SDean Nelson #include <linux/cache.h> 2145d9ca49SDean Nelson #include <linux/mmzone.h> 2245d9ca49SDean Nelson #include <linux/nodemask.h> 2345d9ca49SDean Nelson #include <asm/sn/intr.h> 2445d9ca49SDean Nelson #include <asm/sn/sn_sal.h> 2545d9ca49SDean Nelson #include <asm/sn/nodepda.h> 2645d9ca49SDean Nelson #include <asm/sn/addrs.h> 2745d9ca49SDean Nelson #include "xpc.h" 2845d9ca49SDean Nelson 2945d9ca49SDean Nelson /* XPC is exiting flag */ 3045d9ca49SDean Nelson int xpc_exiting; 3145d9ca49SDean Nelson 3245d9ca49SDean Nelson /* SH_IPI_ACCESS shub register value on startup */ 3345d9ca49SDean Nelson static u64 xpc_sh1_IPI_access; 3445d9ca49SDean Nelson static u64 xpc_sh2_IPI_access0; 3545d9ca49SDean Nelson static u64 xpc_sh2_IPI_access1; 3645d9ca49SDean Nelson static u64 xpc_sh2_IPI_access2; 3745d9ca49SDean Nelson static u64 xpc_sh2_IPI_access3; 3845d9ca49SDean Nelson 3945d9ca49SDean Nelson /* original protection values for each node */ 4045d9ca49SDean Nelson u64 xpc_prot_vec[MAX_NUMNODES]; 4145d9ca49SDean Nelson 4245d9ca49SDean Nelson /* this partition's reserved page pointers */ 4345d9ca49SDean Nelson struct xpc_rsvd_page *xpc_rsvd_page; 4445d9ca49SDean Nelson static u64 *xpc_part_nasids; 4545d9ca49SDean Nelson static u64 *xpc_mach_nasids; 4645d9ca49SDean Nelson 47*94bd2708SDean Nelson /* >>> next two variables should be 'xpc_' if they remain here */ 48*94bd2708SDean Nelson static int xp_sizeof_nasid_mask; /* actual size in bytes of nasid mask */ 49*94bd2708SDean Nelson int xp_nasid_mask_words; /* actual size in words of nasid mask */ 5045d9ca49SDean Nelson 51bc63d387SDean Nelson struct xpc_partition *xpc_partitions; 5245d9ca49SDean Nelson 5345d9ca49SDean Nelson /* 5445d9ca49SDean Nelson * Generic buffer used to store a local copy of portions of a remote 5545d9ca49SDean Nelson * partition's reserved page (either its header and part_nasids mask, 5645d9ca49SDean Nelson * or its vars). 5745d9ca49SDean Nelson */ 5845d9ca49SDean Nelson char *xpc_remote_copy_buffer; 5945d9ca49SDean Nelson void *xpc_remote_copy_buffer_base; 6045d9ca49SDean Nelson 6145d9ca49SDean Nelson /* 6245d9ca49SDean Nelson * Guarantee that the kmalloc'd memory is cacheline aligned. 6345d9ca49SDean Nelson */ 6445d9ca49SDean Nelson void * 6545d9ca49SDean Nelson xpc_kmalloc_cacheline_aligned(size_t size, gfp_t flags, void **base) 6645d9ca49SDean Nelson { 6745d9ca49SDean Nelson /* see if kmalloc will give us cachline aligned memory by default */ 6845d9ca49SDean Nelson *base = kmalloc(size, flags); 692c2b94f9SDean Nelson if (*base == NULL) 7045d9ca49SDean Nelson return NULL; 712c2b94f9SDean Nelson 722c2b94f9SDean Nelson if ((u64)*base == L1_CACHE_ALIGN((u64)*base)) 7345d9ca49SDean Nelson return *base; 742c2b94f9SDean Nelson 7545d9ca49SDean Nelson kfree(*base); 7645d9ca49SDean Nelson 7745d9ca49SDean Nelson /* nope, we'll have to do it ourselves */ 7845d9ca49SDean Nelson *base = kmalloc(size + L1_CACHE_BYTES, flags); 792c2b94f9SDean Nelson if (*base == NULL) 8045d9ca49SDean Nelson return NULL; 812c2b94f9SDean Nelson 8245d9ca49SDean Nelson return (void *)L1_CACHE_ALIGN((u64)*base); 8345d9ca49SDean Nelson } 8445d9ca49SDean Nelson 8545d9ca49SDean Nelson /* 8645d9ca49SDean Nelson * Given a nasid, get the physical address of the partition's reserved page 8745d9ca49SDean Nelson * for that nasid. This function returns 0 on any error. 8845d9ca49SDean Nelson */ 8945d9ca49SDean Nelson static u64 9045d9ca49SDean Nelson xpc_get_rsvd_page_pa(int nasid) 9145d9ca49SDean Nelson { 92908787dbSDean Nelson enum xp_retval ret; 9345d9ca49SDean Nelson s64 status; 9445d9ca49SDean Nelson u64 cookie = 0; 9545d9ca49SDean Nelson u64 rp_pa = nasid; /* seed with nasid */ 9645d9ca49SDean Nelson u64 len = 0; 9745d9ca49SDean Nelson u64 buf = buf; 9845d9ca49SDean Nelson u64 buf_len = 0; 9945d9ca49SDean Nelson void *buf_base = NULL; 10045d9ca49SDean Nelson 10145d9ca49SDean Nelson while (1) { 10245d9ca49SDean Nelson 10345d9ca49SDean Nelson status = sn_partition_reserved_page_pa(buf, &cookie, &rp_pa, 10445d9ca49SDean Nelson &len); 10545d9ca49SDean Nelson 10645d9ca49SDean Nelson dev_dbg(xpc_part, "SAL returned with status=%li, cookie=" 10745d9ca49SDean Nelson "0x%016lx, address=0x%016lx, len=0x%016lx\n", 10845d9ca49SDean Nelson status, cookie, rp_pa, len); 10945d9ca49SDean Nelson 1102c2b94f9SDean Nelson if (status != SALRET_MORE_PASSES) 11145d9ca49SDean Nelson break; 11245d9ca49SDean Nelson 113908787dbSDean Nelson /* >>> L1_CACHE_ALIGN() is only a sn2-bte_copy requirement */ 11445d9ca49SDean Nelson if (L1_CACHE_ALIGN(len) > buf_len) { 11545d9ca49SDean Nelson kfree(buf_base); 11645d9ca49SDean Nelson buf_len = L1_CACHE_ALIGN(len); 11745d9ca49SDean Nelson buf = (u64)xpc_kmalloc_cacheline_aligned(buf_len, 1184a3ad2ddSDean Nelson GFP_KERNEL, 1194a3ad2ddSDean Nelson &buf_base); 12045d9ca49SDean Nelson if (buf_base == NULL) { 12145d9ca49SDean Nelson dev_err(xpc_part, "unable to kmalloc " 12245d9ca49SDean Nelson "len=0x%016lx\n", buf_len); 12345d9ca49SDean Nelson status = SALRET_ERROR; 12445d9ca49SDean Nelson break; 12545d9ca49SDean Nelson } 12645d9ca49SDean Nelson } 12745d9ca49SDean Nelson 128908787dbSDean Nelson ret = xp_remote_memcpy((void *)buf, (void *)rp_pa, buf_len); 129908787dbSDean Nelson if (ret != xpSuccess) { 130908787dbSDean Nelson dev_dbg(xpc_part, "xp_remote_memcpy failed %d\n", ret); 13145d9ca49SDean Nelson status = SALRET_ERROR; 13245d9ca49SDean Nelson break; 13345d9ca49SDean Nelson } 13445d9ca49SDean Nelson } 13545d9ca49SDean Nelson 13645d9ca49SDean Nelson kfree(buf_base); 13745d9ca49SDean Nelson 1382c2b94f9SDean Nelson if (status != SALRET_OK) 13945d9ca49SDean Nelson rp_pa = 0; 1402c2b94f9SDean Nelson 14145d9ca49SDean Nelson dev_dbg(xpc_part, "reserved page at phys address 0x%016lx\n", rp_pa); 14245d9ca49SDean Nelson return rp_pa; 14345d9ca49SDean Nelson } 14445d9ca49SDean Nelson 14545d9ca49SDean Nelson /* 14645d9ca49SDean Nelson * Fill the partition reserved page with the information needed by 14745d9ca49SDean Nelson * other partitions to discover we are alive and establish initial 14845d9ca49SDean Nelson * communications. 14945d9ca49SDean Nelson */ 15045d9ca49SDean Nelson struct xpc_rsvd_page * 151*94bd2708SDean Nelson xpc_setup_rsvd_page(void) 15245d9ca49SDean Nelson { 15345d9ca49SDean Nelson struct xpc_rsvd_page *rp; 154*94bd2708SDean Nelson u64 rp_pa; 15545d9ca49SDean Nelson 15645d9ca49SDean Nelson /* get the local reserved page's address */ 15745d9ca49SDean Nelson 15845d9ca49SDean Nelson preempt_disable(); 15945d9ca49SDean Nelson rp_pa = xpc_get_rsvd_page_pa(cpuid_to_nasid(smp_processor_id())); 16045d9ca49SDean Nelson preempt_enable(); 16145d9ca49SDean Nelson if (rp_pa == 0) { 16245d9ca49SDean Nelson dev_err(xpc_part, "SAL failed to locate the reserved page\n"); 16345d9ca49SDean Nelson return NULL; 16445d9ca49SDean Nelson } 16545d9ca49SDean Nelson rp = (struct xpc_rsvd_page *)__va(rp_pa); 16645d9ca49SDean Nelson 167*94bd2708SDean Nelson if (rp->SAL_version < 3) { 168*94bd2708SDean Nelson /* SAL_versions < 3 had a SAL_partid defined as a u8 */ 169*94bd2708SDean Nelson rp->SAL_partid &= 0xff; 170*94bd2708SDean Nelson } 171*94bd2708SDean Nelson BUG_ON(rp->SAL_partid != sn_partition_id); 172*94bd2708SDean Nelson 173*94bd2708SDean Nelson if (rp->SAL_partid < 0 || rp->SAL_partid >= xp_max_npartitions) { 174*94bd2708SDean Nelson dev_err(xpc_part, "the reserved page's partid of %d is outside " 175*94bd2708SDean Nelson "supported range (< 0 || >= %d)\n", rp->SAL_partid, 176*94bd2708SDean Nelson xp_max_npartitions); 17745d9ca49SDean Nelson return NULL; 17845d9ca49SDean Nelson } 17945d9ca49SDean Nelson 18045d9ca49SDean Nelson rp->version = XPC_RP_VERSION; 181*94bd2708SDean Nelson rp->max_npartitions = xp_max_npartitions; 18245d9ca49SDean Nelson 18345d9ca49SDean Nelson /* establish the actual sizes of the nasid masks */ 18445d9ca49SDean Nelson if (rp->SAL_version == 1) { 18545d9ca49SDean Nelson /* SAL_version 1 didn't set the nasids_size field */ 186*94bd2708SDean Nelson rp->SAL_nasids_size = 128; 18745d9ca49SDean Nelson } 188*94bd2708SDean Nelson xp_sizeof_nasid_mask = rp->SAL_nasids_size; 189*94bd2708SDean Nelson xp_nasid_mask_words = DIV_ROUND_UP(xp_sizeof_nasid_mask, 190*94bd2708SDean Nelson BYTES_PER_WORD); 19145d9ca49SDean Nelson 19245d9ca49SDean Nelson /* setup the pointers to the various items in the reserved page */ 19345d9ca49SDean Nelson xpc_part_nasids = XPC_RP_PART_NASIDS(rp); 19445d9ca49SDean Nelson xpc_mach_nasids = XPC_RP_MACH_NASIDS(rp); 19545d9ca49SDean Nelson 196*94bd2708SDean Nelson if (xpc_rsvd_page_init(rp) != xpSuccess) 19745d9ca49SDean Nelson return NULL; 19845d9ca49SDean Nelson 19945d9ca49SDean Nelson /* 200*94bd2708SDean Nelson * Set timestamp of when reserved page was setup by XPC. 20145d9ca49SDean Nelson * This signifies to the remote partition that our reserved 20245d9ca49SDean Nelson * page is initialized. 20345d9ca49SDean Nelson */ 204*94bd2708SDean Nelson rp->stamp = CURRENT_TIME; 20545d9ca49SDean Nelson 20645d9ca49SDean Nelson return rp; 20745d9ca49SDean Nelson } 20845d9ca49SDean Nelson 20945d9ca49SDean Nelson /* 21045d9ca49SDean Nelson * Change protections to allow IPI operations (and AMO operations on 21145d9ca49SDean Nelson * Shub 1.1 systems). 21245d9ca49SDean Nelson */ 21345d9ca49SDean Nelson void 21445d9ca49SDean Nelson xpc_allow_IPI_ops(void) 21545d9ca49SDean Nelson { 21645d9ca49SDean Nelson int node; 21745d9ca49SDean Nelson int nasid; 21845d9ca49SDean Nelson 2192c2b94f9SDean Nelson /* >>> Change SH_IPI_ACCESS code to use SAL call once it is available */ 22045d9ca49SDean Nelson 22145d9ca49SDean Nelson if (is_shub2()) { 22245d9ca49SDean Nelson xpc_sh2_IPI_access0 = 22345d9ca49SDean Nelson (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS0)); 22445d9ca49SDean Nelson xpc_sh2_IPI_access1 = 22545d9ca49SDean Nelson (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS1)); 22645d9ca49SDean Nelson xpc_sh2_IPI_access2 = 22745d9ca49SDean Nelson (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS2)); 22845d9ca49SDean Nelson xpc_sh2_IPI_access3 = 22945d9ca49SDean Nelson (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS3)); 23045d9ca49SDean Nelson 23145d9ca49SDean Nelson for_each_online_node(node) { 23245d9ca49SDean Nelson nasid = cnodeid_to_nasid(node); 23345d9ca49SDean Nelson HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0), 23445d9ca49SDean Nelson -1UL); 23545d9ca49SDean Nelson HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1), 23645d9ca49SDean Nelson -1UL); 23745d9ca49SDean Nelson HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2), 23845d9ca49SDean Nelson -1UL); 23945d9ca49SDean Nelson HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3), 24045d9ca49SDean Nelson -1UL); 24145d9ca49SDean Nelson } 24245d9ca49SDean Nelson 24345d9ca49SDean Nelson } else { 24445d9ca49SDean Nelson xpc_sh1_IPI_access = 24545d9ca49SDean Nelson (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH1_IPI_ACCESS)); 24645d9ca49SDean Nelson 24745d9ca49SDean Nelson for_each_online_node(node) { 24845d9ca49SDean Nelson nasid = cnodeid_to_nasid(node); 24945d9ca49SDean Nelson HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS), 25045d9ca49SDean Nelson -1UL); 25145d9ca49SDean Nelson 25245d9ca49SDean Nelson /* 25345d9ca49SDean Nelson * Since the BIST collides with memory operations on 25445d9ca49SDean Nelson * SHUB 1.1 sn_change_memprotect() cannot be used. 25545d9ca49SDean Nelson */ 25645d9ca49SDean Nelson if (enable_shub_wars_1_1()) { 25745d9ca49SDean Nelson /* open up everything */ 25845d9ca49SDean Nelson xpc_prot_vec[node] = (u64)HUB_L((u64 *) 2594a3ad2ddSDean Nelson GLOBAL_MMR_ADDR 2604a3ad2ddSDean Nelson (nasid, 26145d9ca49SDean Nelson SH1_MD_DQLP_MMR_DIR_PRIVEC0)); 2624a3ad2ddSDean Nelson HUB_S((u64 *) 2634a3ad2ddSDean Nelson GLOBAL_MMR_ADDR(nasid, 26445d9ca49SDean Nelson SH1_MD_DQLP_MMR_DIR_PRIVEC0), 26545d9ca49SDean Nelson -1UL); 2664a3ad2ddSDean Nelson HUB_S((u64 *) 2674a3ad2ddSDean Nelson GLOBAL_MMR_ADDR(nasid, 26845d9ca49SDean Nelson SH1_MD_DQRP_MMR_DIR_PRIVEC0), 26945d9ca49SDean Nelson -1UL); 27045d9ca49SDean Nelson } 27145d9ca49SDean Nelson } 27245d9ca49SDean Nelson } 27345d9ca49SDean Nelson } 27445d9ca49SDean Nelson 27545d9ca49SDean Nelson /* 27645d9ca49SDean Nelson * Restrict protections to disallow IPI operations (and AMO operations on 27745d9ca49SDean Nelson * Shub 1.1 systems). 27845d9ca49SDean Nelson */ 27945d9ca49SDean Nelson void 28045d9ca49SDean Nelson xpc_restrict_IPI_ops(void) 28145d9ca49SDean Nelson { 28245d9ca49SDean Nelson int node; 28345d9ca49SDean Nelson int nasid; 28445d9ca49SDean Nelson 2852c2b94f9SDean Nelson /* >>> Change SH_IPI_ACCESS code to use SAL call once it is available */ 28645d9ca49SDean Nelson 28745d9ca49SDean Nelson if (is_shub2()) { 28845d9ca49SDean Nelson 28945d9ca49SDean Nelson for_each_online_node(node) { 29045d9ca49SDean Nelson nasid = cnodeid_to_nasid(node); 29145d9ca49SDean Nelson HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0), 29245d9ca49SDean Nelson xpc_sh2_IPI_access0); 29345d9ca49SDean Nelson HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1), 29445d9ca49SDean Nelson xpc_sh2_IPI_access1); 29545d9ca49SDean Nelson HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2), 29645d9ca49SDean Nelson xpc_sh2_IPI_access2); 29745d9ca49SDean Nelson HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3), 29845d9ca49SDean Nelson xpc_sh2_IPI_access3); 29945d9ca49SDean Nelson } 30045d9ca49SDean Nelson 30145d9ca49SDean Nelson } else { 30245d9ca49SDean Nelson 30345d9ca49SDean Nelson for_each_online_node(node) { 30445d9ca49SDean Nelson nasid = cnodeid_to_nasid(node); 30545d9ca49SDean Nelson HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS), 30645d9ca49SDean Nelson xpc_sh1_IPI_access); 30745d9ca49SDean Nelson 30845d9ca49SDean Nelson if (enable_shub_wars_1_1()) { 30945d9ca49SDean Nelson HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, 31045d9ca49SDean Nelson SH1_MD_DQLP_MMR_DIR_PRIVEC0), 31145d9ca49SDean Nelson xpc_prot_vec[node]); 31245d9ca49SDean Nelson HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, 31345d9ca49SDean Nelson SH1_MD_DQRP_MMR_DIR_PRIVEC0), 31445d9ca49SDean Nelson xpc_prot_vec[node]); 31545d9ca49SDean Nelson } 31645d9ca49SDean Nelson } 31745d9ca49SDean Nelson } 31845d9ca49SDean Nelson } 31945d9ca49SDean Nelson 32045d9ca49SDean Nelson /* 32145d9ca49SDean Nelson * At periodic intervals, scan through all active partitions and ensure 32245d9ca49SDean Nelson * their heartbeat is still active. If not, the partition is deactivated. 32345d9ca49SDean Nelson */ 32445d9ca49SDean Nelson void 32545d9ca49SDean Nelson xpc_check_remote_hb(void) 32645d9ca49SDean Nelson { 32745d9ca49SDean Nelson struct xpc_vars *remote_vars; 32845d9ca49SDean Nelson struct xpc_partition *part; 32964d032baSDean Nelson short partid; 330908787dbSDean Nelson enum xp_retval ret; 33145d9ca49SDean Nelson 33245d9ca49SDean Nelson remote_vars = (struct xpc_vars *)xpc_remote_copy_buffer; 33345d9ca49SDean Nelson 334bc63d387SDean Nelson for (partid = 0; partid < xp_max_npartitions; partid++) { 33545d9ca49SDean Nelson 3362c2b94f9SDean Nelson if (xpc_exiting) 33745d9ca49SDean Nelson break; 33845d9ca49SDean Nelson 3392c2b94f9SDean Nelson if (partid == sn_partition_id) 34045d9ca49SDean Nelson continue; 34145d9ca49SDean Nelson 34245d9ca49SDean Nelson part = &xpc_partitions[partid]; 34345d9ca49SDean Nelson 34445d9ca49SDean Nelson if (part->act_state == XPC_P_INACTIVE || 34545d9ca49SDean Nelson part->act_state == XPC_P_DEACTIVATING) { 34645d9ca49SDean Nelson continue; 34745d9ca49SDean Nelson } 34845d9ca49SDean Nelson 34945d9ca49SDean Nelson /* pull the remote_hb cache line */ 350908787dbSDean Nelson ret = xp_remote_memcpy(remote_vars, 351908787dbSDean Nelson (void *)part->remote_vars_pa, 352908787dbSDean Nelson XPC_RP_VARS_SIZE); 353908787dbSDean Nelson if (ret != xpSuccess) { 354908787dbSDean Nelson XPC_DEACTIVATE_PARTITION(part, ret); 35545d9ca49SDean Nelson continue; 35645d9ca49SDean Nelson } 35745d9ca49SDean Nelson 35845d9ca49SDean Nelson dev_dbg(xpc_part, "partid = %d, heartbeat = %ld, last_heartbeat" 35945d9ca49SDean Nelson " = %ld, heartbeat_offline = %ld, HB_mask = 0x%lx\n", 36045d9ca49SDean Nelson partid, remote_vars->heartbeat, part->last_heartbeat, 36145d9ca49SDean Nelson remote_vars->heartbeat_offline, 36245d9ca49SDean Nelson remote_vars->heartbeating_to_mask); 36345d9ca49SDean Nelson 36445d9ca49SDean Nelson if (((remote_vars->heartbeat == part->last_heartbeat) && 36545d9ca49SDean Nelson (remote_vars->heartbeat_offline == 0)) || 36645d9ca49SDean Nelson !xpc_hb_allowed(sn_partition_id, remote_vars)) { 36745d9ca49SDean Nelson 36865c17b80SDean Nelson XPC_DEACTIVATE_PARTITION(part, xpNoHeartbeat); 36945d9ca49SDean Nelson continue; 37045d9ca49SDean Nelson } 37145d9ca49SDean Nelson 37245d9ca49SDean Nelson part->last_heartbeat = remote_vars->heartbeat; 37345d9ca49SDean Nelson } 37445d9ca49SDean Nelson } 37545d9ca49SDean Nelson 37645d9ca49SDean Nelson /* 37745d9ca49SDean Nelson * Get a copy of a portion of the remote partition's rsvd page. 37845d9ca49SDean Nelson * 37945d9ca49SDean Nelson * remote_rp points to a buffer that is cacheline aligned for BTE copies and 38045d9ca49SDean Nelson * is large enough to contain a copy of their reserved page header and 38145d9ca49SDean Nelson * part_nasids mask. 38245d9ca49SDean Nelson */ 38365c17b80SDean Nelson static enum xp_retval 38445d9ca49SDean Nelson xpc_get_remote_rp(int nasid, u64 *discovered_nasids, 38545d9ca49SDean Nelson struct xpc_rsvd_page *remote_rp, u64 *remote_rp_pa) 38645d9ca49SDean Nelson { 387908787dbSDean Nelson int i; 388908787dbSDean Nelson enum xp_retval ret; 38945d9ca49SDean Nelson 39045d9ca49SDean Nelson /* get the reserved page's physical address */ 39145d9ca49SDean Nelson 39245d9ca49SDean Nelson *remote_rp_pa = xpc_get_rsvd_page_pa(nasid); 3932c2b94f9SDean Nelson if (*remote_rp_pa == 0) 39465c17b80SDean Nelson return xpNoRsvdPageAddr; 39545d9ca49SDean Nelson 39645d9ca49SDean Nelson /* pull over the reserved page header and part_nasids mask */ 397908787dbSDean Nelson ret = xp_remote_memcpy(remote_rp, (void *)*remote_rp_pa, 398*94bd2708SDean Nelson XPC_RP_HEADER_SIZE + xp_sizeof_nasid_mask); 399908787dbSDean Nelson if (ret != xpSuccess) 400908787dbSDean Nelson return ret; 40145d9ca49SDean Nelson 40245d9ca49SDean Nelson if (discovered_nasids != NULL) { 40345d9ca49SDean Nelson u64 *remote_part_nasids = XPC_RP_PART_NASIDS(remote_rp); 40445d9ca49SDean Nelson 4052c2b94f9SDean Nelson for (i = 0; i < xp_nasid_mask_words; i++) 40645d9ca49SDean Nelson discovered_nasids[i] |= remote_part_nasids[i]; 40745d9ca49SDean Nelson } 40845d9ca49SDean Nelson 409*94bd2708SDean Nelson /* check that the partid is valid and is for another partition */ 41045d9ca49SDean Nelson 411*94bd2708SDean Nelson if (remote_rp->SAL_partid < 0 || 412*94bd2708SDean Nelson remote_rp->SAL_partid >= xp_max_npartitions) { 41365c17b80SDean Nelson return xpInvalidPartid; 414*94bd2708SDean Nelson } 41545d9ca49SDean Nelson 416*94bd2708SDean Nelson if (remote_rp->SAL_partid == sn_partition_id) 41765c17b80SDean Nelson return xpLocalPartid; 41845d9ca49SDean Nelson 419*94bd2708SDean Nelson /* see if the rest of the reserved page has been set up by XPC */ 420*94bd2708SDean Nelson if (timespec_equal(&remote_rp->stamp, &ZERO_STAMP)) 421*94bd2708SDean Nelson return xpRsvdPageNotSet; 422*94bd2708SDean Nelson 42345d9ca49SDean Nelson if (XPC_VERSION_MAJOR(remote_rp->version) != 42445d9ca49SDean Nelson XPC_VERSION_MAJOR(XPC_RP_VERSION)) { 42565c17b80SDean Nelson return xpBadVersion; 42645d9ca49SDean Nelson } 42745d9ca49SDean Nelson 428*94bd2708SDean Nelson if (remote_rp->max_npartitions <= sn_partition_id) 429*94bd2708SDean Nelson return xpInvalidPartid; 430*94bd2708SDean Nelson 43165c17b80SDean Nelson return xpSuccess; 43245d9ca49SDean Nelson } 43345d9ca49SDean Nelson 43445d9ca49SDean Nelson /* 43545d9ca49SDean Nelson * Get a copy of the remote partition's XPC variables from the reserved page. 43645d9ca49SDean Nelson * 43745d9ca49SDean Nelson * remote_vars points to a buffer that is cacheline aligned for BTE copies and 43845d9ca49SDean Nelson * assumed to be of size XPC_RP_VARS_SIZE. 43945d9ca49SDean Nelson */ 44065c17b80SDean Nelson static enum xp_retval 44145d9ca49SDean Nelson xpc_get_remote_vars(u64 remote_vars_pa, struct xpc_vars *remote_vars) 44245d9ca49SDean Nelson { 443908787dbSDean Nelson enum xp_retval ret; 44445d9ca49SDean Nelson 4452c2b94f9SDean Nelson if (remote_vars_pa == 0) 44665c17b80SDean Nelson return xpVarsNotSet; 44745d9ca49SDean Nelson 44845d9ca49SDean Nelson /* pull over the cross partition variables */ 449908787dbSDean Nelson ret = xp_remote_memcpy(remote_vars, (void *)remote_vars_pa, 450908787dbSDean Nelson XPC_RP_VARS_SIZE); 451908787dbSDean Nelson if (ret != xpSuccess) 452908787dbSDean Nelson return ret; 45345d9ca49SDean Nelson 45445d9ca49SDean Nelson if (XPC_VERSION_MAJOR(remote_vars->version) != 45545d9ca49SDean Nelson XPC_VERSION_MAJOR(XPC_V_VERSION)) { 45665c17b80SDean Nelson return xpBadVersion; 45745d9ca49SDean Nelson } 45845d9ca49SDean Nelson 45965c17b80SDean Nelson return xpSuccess; 46045d9ca49SDean Nelson } 46145d9ca49SDean Nelson 46245d9ca49SDean Nelson /* 46345d9ca49SDean Nelson * Update the remote partition's info. 46445d9ca49SDean Nelson */ 46545d9ca49SDean Nelson static void 46645d9ca49SDean Nelson xpc_update_partition_info(struct xpc_partition *part, u8 remote_rp_version, 46745d9ca49SDean Nelson struct timespec *remote_rp_stamp, u64 remote_rp_pa, 46845d9ca49SDean Nelson u64 remote_vars_pa, struct xpc_vars *remote_vars) 46945d9ca49SDean Nelson { 47045d9ca49SDean Nelson part->remote_rp_version = remote_rp_version; 47145d9ca49SDean Nelson dev_dbg(xpc_part, " remote_rp_version = 0x%016x\n", 47245d9ca49SDean Nelson part->remote_rp_version); 47345d9ca49SDean Nelson 47445d9ca49SDean Nelson part->remote_rp_stamp = *remote_rp_stamp; 47545d9ca49SDean Nelson dev_dbg(xpc_part, " remote_rp_stamp (tv_sec = 0x%lx tv_nsec = 0x%lx\n", 47645d9ca49SDean Nelson part->remote_rp_stamp.tv_sec, part->remote_rp_stamp.tv_nsec); 47745d9ca49SDean Nelson 47845d9ca49SDean Nelson part->remote_rp_pa = remote_rp_pa; 47945d9ca49SDean Nelson dev_dbg(xpc_part, " remote_rp_pa = 0x%016lx\n", part->remote_rp_pa); 48045d9ca49SDean Nelson 48145d9ca49SDean Nelson part->remote_vars_pa = remote_vars_pa; 48245d9ca49SDean Nelson dev_dbg(xpc_part, " remote_vars_pa = 0x%016lx\n", 48345d9ca49SDean Nelson part->remote_vars_pa); 48445d9ca49SDean Nelson 48545d9ca49SDean Nelson part->last_heartbeat = remote_vars->heartbeat; 48645d9ca49SDean Nelson dev_dbg(xpc_part, " last_heartbeat = 0x%016lx\n", 48745d9ca49SDean Nelson part->last_heartbeat); 48845d9ca49SDean Nelson 48945d9ca49SDean Nelson part->remote_vars_part_pa = remote_vars->vars_part_pa; 49045d9ca49SDean Nelson dev_dbg(xpc_part, " remote_vars_part_pa = 0x%016lx\n", 49145d9ca49SDean Nelson part->remote_vars_part_pa); 49245d9ca49SDean Nelson 49345d9ca49SDean Nelson part->remote_act_nasid = remote_vars->act_nasid; 49445d9ca49SDean Nelson dev_dbg(xpc_part, " remote_act_nasid = 0x%x\n", 49545d9ca49SDean Nelson part->remote_act_nasid); 49645d9ca49SDean Nelson 49745d9ca49SDean Nelson part->remote_act_phys_cpuid = remote_vars->act_phys_cpuid; 49845d9ca49SDean Nelson dev_dbg(xpc_part, " remote_act_phys_cpuid = 0x%x\n", 49945d9ca49SDean Nelson part->remote_act_phys_cpuid); 50045d9ca49SDean Nelson 50145d9ca49SDean Nelson part->remote_amos_page_pa = remote_vars->amos_page_pa; 50245d9ca49SDean Nelson dev_dbg(xpc_part, " remote_amos_page_pa = 0x%lx\n", 50345d9ca49SDean Nelson part->remote_amos_page_pa); 50445d9ca49SDean Nelson 50545d9ca49SDean Nelson part->remote_vars_version = remote_vars->version; 50645d9ca49SDean Nelson dev_dbg(xpc_part, " remote_vars_version = 0x%x\n", 50745d9ca49SDean Nelson part->remote_vars_version); 50845d9ca49SDean Nelson } 50945d9ca49SDean Nelson 51045d9ca49SDean Nelson /* 51145d9ca49SDean Nelson * Prior code has determined the nasid which generated an IPI. Inspect 51245d9ca49SDean Nelson * that nasid to determine if its partition needs to be activated or 51345d9ca49SDean Nelson * deactivated. 51445d9ca49SDean Nelson * 51545d9ca49SDean Nelson * A partition is consider "awaiting activation" if our partition 51645d9ca49SDean Nelson * flags indicate it is not active and it has a heartbeat. A 51745d9ca49SDean Nelson * partition is considered "awaiting deactivation" if our partition 51845d9ca49SDean Nelson * flags indicate it is active but it has no heartbeat or it is not 51945d9ca49SDean Nelson * sending its heartbeat to us. 52045d9ca49SDean Nelson * 52145d9ca49SDean Nelson * To determine the heartbeat, the remote nasid must have a properly 52245d9ca49SDean Nelson * initialized reserved page. 52345d9ca49SDean Nelson */ 52445d9ca49SDean Nelson static void 52545d9ca49SDean Nelson xpc_identify_act_IRQ_req(int nasid) 52645d9ca49SDean Nelson { 52745d9ca49SDean Nelson struct xpc_rsvd_page *remote_rp; 52845d9ca49SDean Nelson struct xpc_vars *remote_vars; 52945d9ca49SDean Nelson u64 remote_rp_pa; 53045d9ca49SDean Nelson u64 remote_vars_pa; 53145d9ca49SDean Nelson int remote_rp_version; 53245d9ca49SDean Nelson int reactivate = 0; 53345d9ca49SDean Nelson int stamp_diff; 534*94bd2708SDean Nelson struct timespec remote_rp_stamp = { 0, 0 }; /*>>> ZERO_STAMP */ 53564d032baSDean Nelson short partid; 53645d9ca49SDean Nelson struct xpc_partition *part; 53765c17b80SDean Nelson enum xp_retval ret; 53845d9ca49SDean Nelson 53945d9ca49SDean Nelson /* pull over the reserved page structure */ 54045d9ca49SDean Nelson 54145d9ca49SDean Nelson remote_rp = (struct xpc_rsvd_page *)xpc_remote_copy_buffer; 54245d9ca49SDean Nelson 54345d9ca49SDean Nelson ret = xpc_get_remote_rp(nasid, NULL, remote_rp, &remote_rp_pa); 54465c17b80SDean Nelson if (ret != xpSuccess) { 54545d9ca49SDean Nelson dev_warn(xpc_part, "unable to get reserved page from nasid %d, " 54645d9ca49SDean Nelson "which sent interrupt, reason=%d\n", nasid, ret); 54745d9ca49SDean Nelson return; 54845d9ca49SDean Nelson } 54945d9ca49SDean Nelson 550*94bd2708SDean Nelson remote_vars_pa = remote_rp->sn.vars_pa; 55145d9ca49SDean Nelson remote_rp_version = remote_rp->version; 5522c2b94f9SDean Nelson if (XPC_SUPPORTS_RP_STAMP(remote_rp_version)) 55345d9ca49SDean Nelson remote_rp_stamp = remote_rp->stamp; 5542c2b94f9SDean Nelson 555*94bd2708SDean Nelson partid = remote_rp->SAL_partid; 55645d9ca49SDean Nelson part = &xpc_partitions[partid]; 55745d9ca49SDean Nelson 55845d9ca49SDean Nelson /* pull over the cross partition variables */ 55945d9ca49SDean Nelson 56045d9ca49SDean Nelson remote_vars = (struct xpc_vars *)xpc_remote_copy_buffer; 56145d9ca49SDean Nelson 56245d9ca49SDean Nelson ret = xpc_get_remote_vars(remote_vars_pa, remote_vars); 56365c17b80SDean Nelson if (ret != xpSuccess) { 56445d9ca49SDean Nelson 56545d9ca49SDean Nelson dev_warn(xpc_part, "unable to get XPC variables from nasid %d, " 56645d9ca49SDean Nelson "which sent interrupt, reason=%d\n", nasid, ret); 56745d9ca49SDean Nelson 56845d9ca49SDean Nelson XPC_DEACTIVATE_PARTITION(part, ret); 56945d9ca49SDean Nelson return; 57045d9ca49SDean Nelson } 57145d9ca49SDean Nelson 57245d9ca49SDean Nelson part->act_IRQ_rcvd++; 57345d9ca49SDean Nelson 57445d9ca49SDean Nelson dev_dbg(xpc_part, "partid for nasid %d is %d; IRQs = %d; HB = " 57545d9ca49SDean Nelson "%ld:0x%lx\n", (int)nasid, (int)partid, part->act_IRQ_rcvd, 57645d9ca49SDean Nelson remote_vars->heartbeat, remote_vars->heartbeating_to_mask); 57745d9ca49SDean Nelson 5782c2b94f9SDean Nelson if (xpc_partition_disengaged(part) && 5792c2b94f9SDean Nelson part->act_state == XPC_P_INACTIVE) { 58045d9ca49SDean Nelson 58145d9ca49SDean Nelson xpc_update_partition_info(part, remote_rp_version, 58245d9ca49SDean Nelson &remote_rp_stamp, remote_rp_pa, 58345d9ca49SDean Nelson remote_vars_pa, remote_vars); 58445d9ca49SDean Nelson 58545d9ca49SDean Nelson if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) { 58645d9ca49SDean Nelson if (xpc_partition_disengage_requested(1UL << partid)) { 58745d9ca49SDean Nelson /* 58845d9ca49SDean Nelson * Other side is waiting on us to disengage, 58945d9ca49SDean Nelson * even though we already have. 59045d9ca49SDean Nelson */ 59145d9ca49SDean Nelson return; 59245d9ca49SDean Nelson } 59345d9ca49SDean Nelson } else { 59445d9ca49SDean Nelson /* other side doesn't support disengage requests */ 59545d9ca49SDean Nelson xpc_clear_partition_disengage_request(1UL << partid); 59645d9ca49SDean Nelson } 59745d9ca49SDean Nelson 59845d9ca49SDean Nelson xpc_activate_partition(part); 59945d9ca49SDean Nelson return; 60045d9ca49SDean Nelson } 60145d9ca49SDean Nelson 60245d9ca49SDean Nelson DBUG_ON(part->remote_rp_version == 0); 60345d9ca49SDean Nelson DBUG_ON(part->remote_vars_version == 0); 60445d9ca49SDean Nelson 60545d9ca49SDean Nelson if (!XPC_SUPPORTS_RP_STAMP(part->remote_rp_version)) { 60645d9ca49SDean Nelson DBUG_ON(XPC_SUPPORTS_DISENGAGE_REQUEST(part-> 60745d9ca49SDean Nelson remote_vars_version)); 60845d9ca49SDean Nelson 60945d9ca49SDean Nelson if (!XPC_SUPPORTS_RP_STAMP(remote_rp_version)) { 61045d9ca49SDean Nelson DBUG_ON(XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars-> 61145d9ca49SDean Nelson version)); 61245d9ca49SDean Nelson /* see if the other side rebooted */ 61345d9ca49SDean Nelson if (part->remote_amos_page_pa == 61445d9ca49SDean Nelson remote_vars->amos_page_pa && 6154a3ad2ddSDean Nelson xpc_hb_allowed(sn_partition_id, remote_vars)) { 61645d9ca49SDean Nelson /* doesn't look that way, so ignore the IPI */ 61745d9ca49SDean Nelson return; 61845d9ca49SDean Nelson } 61945d9ca49SDean Nelson } 62045d9ca49SDean Nelson 62145d9ca49SDean Nelson /* 62245d9ca49SDean Nelson * Other side rebooted and previous XPC didn't support the 62345d9ca49SDean Nelson * disengage request, so we don't need to do anything special. 62445d9ca49SDean Nelson */ 62545d9ca49SDean Nelson 62645d9ca49SDean Nelson xpc_update_partition_info(part, remote_rp_version, 62745d9ca49SDean Nelson &remote_rp_stamp, remote_rp_pa, 62845d9ca49SDean Nelson remote_vars_pa, remote_vars); 62945d9ca49SDean Nelson part->reactivate_nasid = nasid; 63065c17b80SDean Nelson XPC_DEACTIVATE_PARTITION(part, xpReactivating); 63145d9ca49SDean Nelson return; 63245d9ca49SDean Nelson } 63345d9ca49SDean Nelson 63445d9ca49SDean Nelson DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)); 63545d9ca49SDean Nelson 63645d9ca49SDean Nelson if (!XPC_SUPPORTS_RP_STAMP(remote_rp_version)) { 63745d9ca49SDean Nelson DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->version)); 63845d9ca49SDean Nelson 63945d9ca49SDean Nelson /* 64045d9ca49SDean Nelson * Other side rebooted and previous XPC did support the 64145d9ca49SDean Nelson * disengage request, but the new one doesn't. 64245d9ca49SDean Nelson */ 64345d9ca49SDean Nelson 64445d9ca49SDean Nelson xpc_clear_partition_engaged(1UL << partid); 64545d9ca49SDean Nelson xpc_clear_partition_disengage_request(1UL << partid); 64645d9ca49SDean Nelson 64745d9ca49SDean Nelson xpc_update_partition_info(part, remote_rp_version, 64845d9ca49SDean Nelson &remote_rp_stamp, remote_rp_pa, 64945d9ca49SDean Nelson remote_vars_pa, remote_vars); 65045d9ca49SDean Nelson reactivate = 1; 65145d9ca49SDean Nelson 65245d9ca49SDean Nelson } else { 65345d9ca49SDean Nelson DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->version)); 65445d9ca49SDean Nelson 65545d9ca49SDean Nelson stamp_diff = xpc_compare_stamps(&part->remote_rp_stamp, 65645d9ca49SDean Nelson &remote_rp_stamp); 65745d9ca49SDean Nelson if (stamp_diff != 0) { 65845d9ca49SDean Nelson DBUG_ON(stamp_diff >= 0); 65945d9ca49SDean Nelson 66045d9ca49SDean Nelson /* 66145d9ca49SDean Nelson * Other side rebooted and the previous XPC did support 66245d9ca49SDean Nelson * the disengage request, as does the new one. 66345d9ca49SDean Nelson */ 66445d9ca49SDean Nelson 66545d9ca49SDean Nelson DBUG_ON(xpc_partition_engaged(1UL << partid)); 66645d9ca49SDean Nelson DBUG_ON(xpc_partition_disengage_requested(1UL << 66745d9ca49SDean Nelson partid)); 66845d9ca49SDean Nelson 66945d9ca49SDean Nelson xpc_update_partition_info(part, remote_rp_version, 6704a3ad2ddSDean Nelson &remote_rp_stamp, 6714a3ad2ddSDean Nelson remote_rp_pa, remote_vars_pa, 6724a3ad2ddSDean Nelson remote_vars); 67345d9ca49SDean Nelson reactivate = 1; 67445d9ca49SDean Nelson } 67545d9ca49SDean Nelson } 67645d9ca49SDean Nelson 67745d9ca49SDean Nelson if (part->disengage_request_timeout > 0 && 67845d9ca49SDean Nelson !xpc_partition_disengaged(part)) { 67945d9ca49SDean Nelson /* still waiting on other side to disengage from us */ 68045d9ca49SDean Nelson return; 68145d9ca49SDean Nelson } 68245d9ca49SDean Nelson 68345d9ca49SDean Nelson if (reactivate) { 68445d9ca49SDean Nelson part->reactivate_nasid = nasid; 68565c17b80SDean Nelson XPC_DEACTIVATE_PARTITION(part, xpReactivating); 68645d9ca49SDean Nelson 68745d9ca49SDean Nelson } else if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version) && 68845d9ca49SDean Nelson xpc_partition_disengage_requested(1UL << partid)) { 68965c17b80SDean Nelson XPC_DEACTIVATE_PARTITION(part, xpOtherGoingDown); 69045d9ca49SDean Nelson } 69145d9ca49SDean Nelson } 69245d9ca49SDean Nelson 69345d9ca49SDean Nelson /* 69445d9ca49SDean Nelson * Loop through the activation AMO variables and process any bits 69545d9ca49SDean Nelson * which are set. Each bit indicates a nasid sending a partition 69645d9ca49SDean Nelson * activation or deactivation request. 69745d9ca49SDean Nelson * 69845d9ca49SDean Nelson * Return #of IRQs detected. 69945d9ca49SDean Nelson */ 70045d9ca49SDean Nelson int 70145d9ca49SDean Nelson xpc_identify_act_IRQ_sender(void) 70245d9ca49SDean Nelson { 70345d9ca49SDean Nelson int word, bit; 70445d9ca49SDean Nelson u64 nasid_mask; 70545d9ca49SDean Nelson u64 nasid; /* remote nasid */ 70645d9ca49SDean Nelson int n_IRQs_detected = 0; 70745d9ca49SDean Nelson AMO_t *act_amos; 70845d9ca49SDean Nelson 70945d9ca49SDean Nelson act_amos = xpc_vars->amos_page + XPC_ACTIVATE_IRQ_AMOS; 71045d9ca49SDean Nelson 71145d9ca49SDean Nelson /* scan through act AMO variable looking for non-zero entries */ 71245d9ca49SDean Nelson for (word = 0; word < xp_nasid_mask_words; word++) { 71345d9ca49SDean Nelson 7142c2b94f9SDean Nelson if (xpc_exiting) 71545d9ca49SDean Nelson break; 71645d9ca49SDean Nelson 71745d9ca49SDean Nelson nasid_mask = xpc_IPI_receive(&act_amos[word]); 71845d9ca49SDean Nelson if (nasid_mask == 0) { 71945d9ca49SDean Nelson /* no IRQs from nasids in this variable */ 72045d9ca49SDean Nelson continue; 72145d9ca49SDean Nelson } 72245d9ca49SDean Nelson 72345d9ca49SDean Nelson dev_dbg(xpc_part, "AMO[%d] gave back 0x%lx\n", word, 72445d9ca49SDean Nelson nasid_mask); 72545d9ca49SDean Nelson 72645d9ca49SDean Nelson /* 72745d9ca49SDean Nelson * If this nasid has been added to the machine since 72845d9ca49SDean Nelson * our partition was reset, this will retain the 72945d9ca49SDean Nelson * remote nasid in our reserved pages machine mask. 73045d9ca49SDean Nelson * This is used in the event of module reload. 73145d9ca49SDean Nelson */ 73245d9ca49SDean Nelson xpc_mach_nasids[word] |= nasid_mask; 73345d9ca49SDean Nelson 73445d9ca49SDean Nelson /* locate the nasid(s) which sent interrupts */ 73545d9ca49SDean Nelson 73645d9ca49SDean Nelson for (bit = 0; bit < (8 * sizeof(u64)); bit++) { 73745d9ca49SDean Nelson if (nasid_mask & (1UL << bit)) { 73845d9ca49SDean Nelson n_IRQs_detected++; 73945d9ca49SDean Nelson nasid = XPC_NASID_FROM_W_B(word, bit); 74045d9ca49SDean Nelson dev_dbg(xpc_part, "interrupt from nasid %ld\n", 74145d9ca49SDean Nelson nasid); 74245d9ca49SDean Nelson xpc_identify_act_IRQ_req(nasid); 74345d9ca49SDean Nelson } 74445d9ca49SDean Nelson } 74545d9ca49SDean Nelson } 74645d9ca49SDean Nelson return n_IRQs_detected; 74745d9ca49SDean Nelson } 74845d9ca49SDean Nelson 74945d9ca49SDean Nelson /* 75045d9ca49SDean Nelson * See if the other side has responded to a partition disengage request 75145d9ca49SDean Nelson * from us. 75245d9ca49SDean Nelson */ 75345d9ca49SDean Nelson int 75445d9ca49SDean Nelson xpc_partition_disengaged(struct xpc_partition *part) 75545d9ca49SDean Nelson { 75664d032baSDean Nelson short partid = XPC_PARTID(part); 75745d9ca49SDean Nelson int disengaged; 75845d9ca49SDean Nelson 75945d9ca49SDean Nelson disengaged = (xpc_partition_engaged(1UL << partid) == 0); 76045d9ca49SDean Nelson if (part->disengage_request_timeout) { 76145d9ca49SDean Nelson if (!disengaged) { 7622c2b94f9SDean Nelson if (time_before(jiffies, 7632c2b94f9SDean Nelson part->disengage_request_timeout)) { 76445d9ca49SDean Nelson /* timelimit hasn't been reached yet */ 76545d9ca49SDean Nelson return 0; 76645d9ca49SDean Nelson } 76745d9ca49SDean Nelson 76845d9ca49SDean Nelson /* 76945d9ca49SDean Nelson * Other side hasn't responded to our disengage 77045d9ca49SDean Nelson * request in a timely fashion, so assume it's dead. 77145d9ca49SDean Nelson */ 77245d9ca49SDean Nelson 77345d9ca49SDean Nelson dev_info(xpc_part, "disengage from remote partition %d " 77445d9ca49SDean Nelson "timed out\n", partid); 77545d9ca49SDean Nelson xpc_disengage_request_timedout = 1; 77645d9ca49SDean Nelson xpc_clear_partition_engaged(1UL << partid); 77745d9ca49SDean Nelson disengaged = 1; 77845d9ca49SDean Nelson } 77945d9ca49SDean Nelson part->disengage_request_timeout = 0; 78045d9ca49SDean Nelson 78145d9ca49SDean Nelson /* cancel the timer function, provided it's not us */ 78245d9ca49SDean Nelson if (!in_interrupt()) { 78345d9ca49SDean Nelson del_singleshot_timer_sync(&part-> 78445d9ca49SDean Nelson disengage_request_timer); 78545d9ca49SDean Nelson } 78645d9ca49SDean Nelson 78745d9ca49SDean Nelson DBUG_ON(part->act_state != XPC_P_DEACTIVATING && 78845d9ca49SDean Nelson part->act_state != XPC_P_INACTIVE); 7892c2b94f9SDean Nelson if (part->act_state != XPC_P_INACTIVE) 79045d9ca49SDean Nelson xpc_wakeup_channel_mgr(part); 79145d9ca49SDean Nelson 7922c2b94f9SDean Nelson if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) 79345d9ca49SDean Nelson xpc_cancel_partition_disengage_request(part); 79445d9ca49SDean Nelson } 79545d9ca49SDean Nelson return disengaged; 79645d9ca49SDean Nelson } 79745d9ca49SDean Nelson 79845d9ca49SDean Nelson /* 79945d9ca49SDean Nelson * Mark specified partition as active. 80045d9ca49SDean Nelson */ 80165c17b80SDean Nelson enum xp_retval 80245d9ca49SDean Nelson xpc_mark_partition_active(struct xpc_partition *part) 80345d9ca49SDean Nelson { 80445d9ca49SDean Nelson unsigned long irq_flags; 80565c17b80SDean Nelson enum xp_retval ret; 80645d9ca49SDean Nelson 80745d9ca49SDean Nelson dev_dbg(xpc_part, "setting partition %d to ACTIVE\n", XPC_PARTID(part)); 80845d9ca49SDean Nelson 80945d9ca49SDean Nelson spin_lock_irqsave(&part->act_lock, irq_flags); 81045d9ca49SDean Nelson if (part->act_state == XPC_P_ACTIVATING) { 81145d9ca49SDean Nelson part->act_state = XPC_P_ACTIVE; 81265c17b80SDean Nelson ret = xpSuccess; 81345d9ca49SDean Nelson } else { 81465c17b80SDean Nelson DBUG_ON(part->reason == xpSuccess); 81545d9ca49SDean Nelson ret = part->reason; 81645d9ca49SDean Nelson } 81745d9ca49SDean Nelson spin_unlock_irqrestore(&part->act_lock, irq_flags); 81845d9ca49SDean Nelson 81945d9ca49SDean Nelson return ret; 82045d9ca49SDean Nelson } 82145d9ca49SDean Nelson 82245d9ca49SDean Nelson /* 82345d9ca49SDean Nelson * Notify XPC that the partition is down. 82445d9ca49SDean Nelson */ 82545d9ca49SDean Nelson void 82645d9ca49SDean Nelson xpc_deactivate_partition(const int line, struct xpc_partition *part, 82765c17b80SDean Nelson enum xp_retval reason) 82845d9ca49SDean Nelson { 82945d9ca49SDean Nelson unsigned long irq_flags; 83045d9ca49SDean Nelson 83145d9ca49SDean Nelson spin_lock_irqsave(&part->act_lock, irq_flags); 83245d9ca49SDean Nelson 83345d9ca49SDean Nelson if (part->act_state == XPC_P_INACTIVE) { 83445d9ca49SDean Nelson XPC_SET_REASON(part, reason, line); 83545d9ca49SDean Nelson spin_unlock_irqrestore(&part->act_lock, irq_flags); 83665c17b80SDean Nelson if (reason == xpReactivating) { 83745d9ca49SDean Nelson /* we interrupt ourselves to reactivate partition */ 83845d9ca49SDean Nelson xpc_IPI_send_reactivate(part); 83945d9ca49SDean Nelson } 84045d9ca49SDean Nelson return; 84145d9ca49SDean Nelson } 84245d9ca49SDean Nelson if (part->act_state == XPC_P_DEACTIVATING) { 84365c17b80SDean Nelson if ((part->reason == xpUnloading && reason != xpUnloading) || 84465c17b80SDean Nelson reason == xpReactivating) { 84545d9ca49SDean Nelson XPC_SET_REASON(part, reason, line); 84645d9ca49SDean Nelson } 84745d9ca49SDean Nelson spin_unlock_irqrestore(&part->act_lock, irq_flags); 84845d9ca49SDean Nelson return; 84945d9ca49SDean Nelson } 85045d9ca49SDean Nelson 85145d9ca49SDean Nelson part->act_state = XPC_P_DEACTIVATING; 85245d9ca49SDean Nelson XPC_SET_REASON(part, reason, line); 85345d9ca49SDean Nelson 85445d9ca49SDean Nelson spin_unlock_irqrestore(&part->act_lock, irq_flags); 85545d9ca49SDean Nelson 85645d9ca49SDean Nelson if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) { 85745d9ca49SDean Nelson xpc_request_partition_disengage(part); 85845d9ca49SDean Nelson xpc_IPI_send_disengage(part); 85945d9ca49SDean Nelson 86045d9ca49SDean Nelson /* set a timelimit on the disengage request */ 86145d9ca49SDean Nelson part->disengage_request_timeout = jiffies + 86245d9ca49SDean Nelson (xpc_disengage_request_timelimit * HZ); 86345d9ca49SDean Nelson part->disengage_request_timer.expires = 86445d9ca49SDean Nelson part->disengage_request_timeout; 86545d9ca49SDean Nelson add_timer(&part->disengage_request_timer); 86645d9ca49SDean Nelson } 86745d9ca49SDean Nelson 86845d9ca49SDean Nelson dev_dbg(xpc_part, "bringing partition %d down, reason = %d\n", 86945d9ca49SDean Nelson XPC_PARTID(part), reason); 87045d9ca49SDean Nelson 87145d9ca49SDean Nelson xpc_partition_going_down(part, reason); 87245d9ca49SDean Nelson } 87345d9ca49SDean Nelson 87445d9ca49SDean Nelson /* 87545d9ca49SDean Nelson * Mark specified partition as inactive. 87645d9ca49SDean Nelson */ 87745d9ca49SDean Nelson void 87845d9ca49SDean Nelson xpc_mark_partition_inactive(struct xpc_partition *part) 87945d9ca49SDean Nelson { 88045d9ca49SDean Nelson unsigned long irq_flags; 88145d9ca49SDean Nelson 88245d9ca49SDean Nelson dev_dbg(xpc_part, "setting partition %d to INACTIVE\n", 88345d9ca49SDean Nelson XPC_PARTID(part)); 88445d9ca49SDean Nelson 88545d9ca49SDean Nelson spin_lock_irqsave(&part->act_lock, irq_flags); 88645d9ca49SDean Nelson part->act_state = XPC_P_INACTIVE; 88745d9ca49SDean Nelson spin_unlock_irqrestore(&part->act_lock, irq_flags); 88845d9ca49SDean Nelson part->remote_rp_pa = 0; 88945d9ca49SDean Nelson } 89045d9ca49SDean Nelson 89145d9ca49SDean Nelson /* 89245d9ca49SDean Nelson * SAL has provided a partition and machine mask. The partition mask 89345d9ca49SDean Nelson * contains a bit for each even nasid in our partition. The machine 89445d9ca49SDean Nelson * mask contains a bit for each even nasid in the entire machine. 89545d9ca49SDean Nelson * 89645d9ca49SDean Nelson * Using those two bit arrays, we can determine which nasids are 89745d9ca49SDean Nelson * known in the machine. Each should also have a reserved page 89845d9ca49SDean Nelson * initialized if they are available for partitioning. 89945d9ca49SDean Nelson */ 90045d9ca49SDean Nelson void 90145d9ca49SDean Nelson xpc_discovery(void) 90245d9ca49SDean Nelson { 90345d9ca49SDean Nelson void *remote_rp_base; 90445d9ca49SDean Nelson struct xpc_rsvd_page *remote_rp; 90545d9ca49SDean Nelson struct xpc_vars *remote_vars; 90645d9ca49SDean Nelson u64 remote_rp_pa; 90745d9ca49SDean Nelson u64 remote_vars_pa; 90845d9ca49SDean Nelson int region; 90945d9ca49SDean Nelson int region_size; 91045d9ca49SDean Nelson int max_regions; 91145d9ca49SDean Nelson int nasid; 91245d9ca49SDean Nelson struct xpc_rsvd_page *rp; 91364d032baSDean Nelson short partid; 91445d9ca49SDean Nelson struct xpc_partition *part; 91545d9ca49SDean Nelson u64 *discovered_nasids; 91665c17b80SDean Nelson enum xp_retval ret; 91745d9ca49SDean Nelson 91845d9ca49SDean Nelson remote_rp = xpc_kmalloc_cacheline_aligned(XPC_RP_HEADER_SIZE + 919*94bd2708SDean Nelson xp_sizeof_nasid_mask, 92045d9ca49SDean Nelson GFP_KERNEL, &remote_rp_base); 9212c2b94f9SDean Nelson if (remote_rp == NULL) 92245d9ca49SDean Nelson return; 9232c2b94f9SDean Nelson 92445d9ca49SDean Nelson remote_vars = (struct xpc_vars *)remote_rp; 92545d9ca49SDean Nelson 92645d9ca49SDean Nelson discovered_nasids = kzalloc(sizeof(u64) * xp_nasid_mask_words, 92745d9ca49SDean Nelson GFP_KERNEL); 92845d9ca49SDean Nelson if (discovered_nasids == NULL) { 92945d9ca49SDean Nelson kfree(remote_rp_base); 93045d9ca49SDean Nelson return; 93145d9ca49SDean Nelson } 93245d9ca49SDean Nelson 93345d9ca49SDean Nelson rp = (struct xpc_rsvd_page *)xpc_rsvd_page; 93445d9ca49SDean Nelson 93545d9ca49SDean Nelson /* 93645d9ca49SDean Nelson * The term 'region' in this context refers to the minimum number of 93745d9ca49SDean Nelson * nodes that can comprise an access protection grouping. The access 93845d9ca49SDean Nelson * protection is in regards to memory, IOI and IPI. 93945d9ca49SDean Nelson */ 94045d9ca49SDean Nelson max_regions = 64; 94145d9ca49SDean Nelson region_size = sn_region_size; 94245d9ca49SDean Nelson 94345d9ca49SDean Nelson switch (region_size) { 94445d9ca49SDean Nelson case 128: 94545d9ca49SDean Nelson max_regions *= 2; 94645d9ca49SDean Nelson case 64: 94745d9ca49SDean Nelson max_regions *= 2; 94845d9ca49SDean Nelson case 32: 94945d9ca49SDean Nelson max_regions *= 2; 95045d9ca49SDean Nelson region_size = 16; 95145d9ca49SDean Nelson DBUG_ON(!is_shub2()); 95245d9ca49SDean Nelson } 95345d9ca49SDean Nelson 95445d9ca49SDean Nelson for (region = 0; region < max_regions; region++) { 95545d9ca49SDean Nelson 9562c2b94f9SDean Nelson if (xpc_exiting) 95745d9ca49SDean Nelson break; 95845d9ca49SDean Nelson 95945d9ca49SDean Nelson dev_dbg(xpc_part, "searching region %d\n", region); 96045d9ca49SDean Nelson 96145d9ca49SDean Nelson for (nasid = (region * region_size * 2); 9624a3ad2ddSDean Nelson nasid < ((region + 1) * region_size * 2); nasid += 2) { 96345d9ca49SDean Nelson 9642c2b94f9SDean Nelson if (xpc_exiting) 96545d9ca49SDean Nelson break; 96645d9ca49SDean Nelson 96745d9ca49SDean Nelson dev_dbg(xpc_part, "checking nasid %d\n", nasid); 96845d9ca49SDean Nelson 96945d9ca49SDean Nelson if (XPC_NASID_IN_ARRAY(nasid, xpc_part_nasids)) { 97045d9ca49SDean Nelson dev_dbg(xpc_part, "PROM indicates Nasid %d is " 97145d9ca49SDean Nelson "part of the local partition; skipping " 97245d9ca49SDean Nelson "region\n", nasid); 97345d9ca49SDean Nelson break; 97445d9ca49SDean Nelson } 97545d9ca49SDean Nelson 97645d9ca49SDean Nelson if (!(XPC_NASID_IN_ARRAY(nasid, xpc_mach_nasids))) { 97745d9ca49SDean Nelson dev_dbg(xpc_part, "PROM indicates Nasid %d was " 97845d9ca49SDean Nelson "not on Numa-Link network at reset\n", 97945d9ca49SDean Nelson nasid); 98045d9ca49SDean Nelson continue; 98145d9ca49SDean Nelson } 98245d9ca49SDean Nelson 98345d9ca49SDean Nelson if (XPC_NASID_IN_ARRAY(nasid, discovered_nasids)) { 98445d9ca49SDean Nelson dev_dbg(xpc_part, "Nasid %d is part of a " 98545d9ca49SDean Nelson "partition which was previously " 98645d9ca49SDean Nelson "discovered\n", nasid); 98745d9ca49SDean Nelson continue; 98845d9ca49SDean Nelson } 98945d9ca49SDean Nelson 99045d9ca49SDean Nelson /* pull over the reserved page structure */ 99145d9ca49SDean Nelson 99245d9ca49SDean Nelson ret = xpc_get_remote_rp(nasid, discovered_nasids, 99345d9ca49SDean Nelson remote_rp, &remote_rp_pa); 99465c17b80SDean Nelson if (ret != xpSuccess) { 99545d9ca49SDean Nelson dev_dbg(xpc_part, "unable to get reserved page " 99645d9ca49SDean Nelson "from nasid %d, reason=%d\n", nasid, 99745d9ca49SDean Nelson ret); 99845d9ca49SDean Nelson 99965c17b80SDean Nelson if (ret == xpLocalPartid) 100045d9ca49SDean Nelson break; 10012c2b94f9SDean Nelson 100245d9ca49SDean Nelson continue; 100345d9ca49SDean Nelson } 100445d9ca49SDean Nelson 1005*94bd2708SDean Nelson remote_vars_pa = remote_rp->sn.vars_pa; 100645d9ca49SDean Nelson 1007*94bd2708SDean Nelson partid = remote_rp->SAL_partid; 100845d9ca49SDean Nelson part = &xpc_partitions[partid]; 100945d9ca49SDean Nelson 101045d9ca49SDean Nelson /* pull over the cross partition variables */ 101145d9ca49SDean Nelson 101245d9ca49SDean Nelson ret = xpc_get_remote_vars(remote_vars_pa, remote_vars); 101365c17b80SDean Nelson if (ret != xpSuccess) { 101445d9ca49SDean Nelson dev_dbg(xpc_part, "unable to get XPC variables " 101545d9ca49SDean Nelson "from nasid %d, reason=%d\n", nasid, 101645d9ca49SDean Nelson ret); 101745d9ca49SDean Nelson 101845d9ca49SDean Nelson XPC_DEACTIVATE_PARTITION(part, ret); 101945d9ca49SDean Nelson continue; 102045d9ca49SDean Nelson } 102145d9ca49SDean Nelson 102245d9ca49SDean Nelson if (part->act_state != XPC_P_INACTIVE) { 102345d9ca49SDean Nelson dev_dbg(xpc_part, "partition %d on nasid %d is " 102445d9ca49SDean Nelson "already activating\n", partid, nasid); 102545d9ca49SDean Nelson break; 102645d9ca49SDean Nelson } 102745d9ca49SDean Nelson 102845d9ca49SDean Nelson /* 102945d9ca49SDean Nelson * Register the remote partition's AMOs with SAL so it 103045d9ca49SDean Nelson * can handle and cleanup errors within that address 103145d9ca49SDean Nelson * range should the remote partition go down. We don't 103245d9ca49SDean Nelson * unregister this range because it is difficult to 103345d9ca49SDean Nelson * tell when outstanding writes to the remote partition 103445d9ca49SDean Nelson * are finished and thus when it is thus safe to 103545d9ca49SDean Nelson * unregister. This should not result in wasted space 103645d9ca49SDean Nelson * in the SAL xp_addr_region table because we should 103745d9ca49SDean Nelson * get the same page for remote_act_amos_pa after 103845d9ca49SDean Nelson * module reloads and system reboots. 103945d9ca49SDean Nelson */ 10404a3ad2ddSDean Nelson if (sn_register_xp_addr_region 10414a3ad2ddSDean Nelson (remote_vars->amos_page_pa, PAGE_SIZE, 1) < 0) { 10424a3ad2ddSDean Nelson dev_dbg(xpc_part, 10434a3ad2ddSDean Nelson "partition %d failed to " 104445d9ca49SDean Nelson "register xp_addr region 0x%016lx\n", 104545d9ca49SDean Nelson partid, remote_vars->amos_page_pa); 104645d9ca49SDean Nelson 104765c17b80SDean Nelson XPC_SET_REASON(part, xpPhysAddrRegFailed, 104845d9ca49SDean Nelson __LINE__); 104945d9ca49SDean Nelson break; 105045d9ca49SDean Nelson } 105145d9ca49SDean Nelson 105245d9ca49SDean Nelson /* 105345d9ca49SDean Nelson * The remote nasid is valid and available. 105445d9ca49SDean Nelson * Send an interrupt to that nasid to notify 105545d9ca49SDean Nelson * it that we are ready to begin activation. 105645d9ca49SDean Nelson */ 105745d9ca49SDean Nelson dev_dbg(xpc_part, "sending an interrupt to AMO 0x%lx, " 105845d9ca49SDean Nelson "nasid %d, phys_cpuid 0x%x\n", 105945d9ca49SDean Nelson remote_vars->amos_page_pa, 106045d9ca49SDean Nelson remote_vars->act_nasid, 106145d9ca49SDean Nelson remote_vars->act_phys_cpuid); 106245d9ca49SDean Nelson 106345d9ca49SDean Nelson if (XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars-> 106445d9ca49SDean Nelson version)) { 106545d9ca49SDean Nelson part->remote_amos_page_pa = 106645d9ca49SDean Nelson remote_vars->amos_page_pa; 106745d9ca49SDean Nelson xpc_mark_partition_disengaged(part); 106845d9ca49SDean Nelson xpc_cancel_partition_disengage_request(part); 106945d9ca49SDean Nelson } 107045d9ca49SDean Nelson xpc_IPI_send_activate(remote_vars); 107145d9ca49SDean Nelson } 107245d9ca49SDean Nelson } 107345d9ca49SDean Nelson 107445d9ca49SDean Nelson kfree(discovered_nasids); 107545d9ca49SDean Nelson kfree(remote_rp_base); 107645d9ca49SDean Nelson } 107745d9ca49SDean Nelson 107845d9ca49SDean Nelson /* 107945d9ca49SDean Nelson * Given a partid, get the nasids owned by that partition from the 108045d9ca49SDean Nelson * remote partition's reserved page. 108145d9ca49SDean Nelson */ 108265c17b80SDean Nelson enum xp_retval 108364d032baSDean Nelson xpc_initiate_partid_to_nasids(short partid, void *nasid_mask) 108445d9ca49SDean Nelson { 108545d9ca49SDean Nelson struct xpc_partition *part; 108645d9ca49SDean Nelson u64 part_nasid_pa; 108745d9ca49SDean Nelson 108845d9ca49SDean Nelson part = &xpc_partitions[partid]; 10892c2b94f9SDean Nelson if (part->remote_rp_pa == 0) 109065c17b80SDean Nelson return xpPartitionDown; 109145d9ca49SDean Nelson 109245d9ca49SDean Nelson memset(nasid_mask, 0, XP_NASID_MASK_BYTES); 109345d9ca49SDean Nelson 109445d9ca49SDean Nelson part_nasid_pa = (u64)XPC_RP_PART_NASIDS(part->remote_rp_pa); 109545d9ca49SDean Nelson 1096908787dbSDean Nelson return xp_remote_memcpy(nasid_mask, (void *)part_nasid_pa, 1097*94bd2708SDean Nelson xp_sizeof_nasid_mask); 109845d9ca49SDean Nelson } 1099