xref: /openbmc/linux/drivers/misc/sgi-xp/xpc_partition.c (revision 64d032ba434ad41586460811148f01511e5612f9)
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/uncached.h>
2445d9ca49SDean Nelson #include <asm/sn/bte.h>
2545d9ca49SDean Nelson #include <asm/sn/intr.h>
2645d9ca49SDean Nelson #include <asm/sn/sn_sal.h>
2745d9ca49SDean Nelson #include <asm/sn/nodepda.h>
2845d9ca49SDean Nelson #include <asm/sn/addrs.h>
2945d9ca49SDean Nelson #include "xpc.h"
3045d9ca49SDean Nelson 
3145d9ca49SDean Nelson /* XPC is exiting flag */
3245d9ca49SDean Nelson int xpc_exiting;
3345d9ca49SDean Nelson 
3445d9ca49SDean Nelson /* SH_IPI_ACCESS shub register value on startup */
3545d9ca49SDean Nelson static u64 xpc_sh1_IPI_access;
3645d9ca49SDean Nelson static u64 xpc_sh2_IPI_access0;
3745d9ca49SDean Nelson static u64 xpc_sh2_IPI_access1;
3845d9ca49SDean Nelson static u64 xpc_sh2_IPI_access2;
3945d9ca49SDean Nelson static u64 xpc_sh2_IPI_access3;
4045d9ca49SDean Nelson 
4145d9ca49SDean Nelson /* original protection values for each node */
4245d9ca49SDean Nelson u64 xpc_prot_vec[MAX_NUMNODES];
4345d9ca49SDean Nelson 
4445d9ca49SDean Nelson /* this partition's reserved page pointers */
4545d9ca49SDean Nelson struct xpc_rsvd_page *xpc_rsvd_page;
4645d9ca49SDean Nelson static u64 *xpc_part_nasids;
4745d9ca49SDean Nelson static u64 *xpc_mach_nasids;
4845d9ca49SDean Nelson struct xpc_vars *xpc_vars;
4945d9ca49SDean Nelson struct xpc_vars_part *xpc_vars_part;
5045d9ca49SDean Nelson 
5145d9ca49SDean Nelson static int xp_nasid_mask_bytes;	/* actual size in bytes of nasid mask */
5245d9ca49SDean Nelson static int xp_nasid_mask_words;	/* actual size in words of nasid mask */
5345d9ca49SDean Nelson 
5445d9ca49SDean Nelson /*
5545d9ca49SDean Nelson  * For performance reasons, each entry of xpc_partitions[] is cacheline
5645d9ca49SDean Nelson  * aligned. And xpc_partitions[] is padded with an additional entry at the
5745d9ca49SDean Nelson  * end so that the last legitimate entry doesn't share its cacheline with
5845d9ca49SDean Nelson  * another variable.
5945d9ca49SDean Nelson  */
6045d9ca49SDean Nelson struct xpc_partition xpc_partitions[XP_MAX_PARTITIONS + 1];
6145d9ca49SDean Nelson 
6245d9ca49SDean Nelson /*
6345d9ca49SDean Nelson  * Generic buffer used to store a local copy of portions of a remote
6445d9ca49SDean Nelson  * partition's reserved page (either its header and part_nasids mask,
6545d9ca49SDean Nelson  * or its vars).
6645d9ca49SDean Nelson  */
6745d9ca49SDean Nelson char *xpc_remote_copy_buffer;
6845d9ca49SDean Nelson void *xpc_remote_copy_buffer_base;
6945d9ca49SDean Nelson 
7045d9ca49SDean Nelson /*
7145d9ca49SDean Nelson  * Guarantee that the kmalloc'd memory is cacheline aligned.
7245d9ca49SDean Nelson  */
7345d9ca49SDean Nelson void *
7445d9ca49SDean Nelson xpc_kmalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
7545d9ca49SDean Nelson {
7645d9ca49SDean Nelson 	/* see if kmalloc will give us cachline aligned memory by default */
7745d9ca49SDean Nelson 	*base = kmalloc(size, flags);
782c2b94f9SDean Nelson 	if (*base == NULL)
7945d9ca49SDean Nelson 		return NULL;
802c2b94f9SDean Nelson 
812c2b94f9SDean Nelson 	if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
8245d9ca49SDean Nelson 		return *base;
832c2b94f9SDean Nelson 
8445d9ca49SDean Nelson 	kfree(*base);
8545d9ca49SDean Nelson 
8645d9ca49SDean Nelson 	/* nope, we'll have to do it ourselves */
8745d9ca49SDean Nelson 	*base = kmalloc(size + L1_CACHE_BYTES, flags);
882c2b94f9SDean Nelson 	if (*base == NULL)
8945d9ca49SDean Nelson 		return NULL;
902c2b94f9SDean Nelson 
9145d9ca49SDean Nelson 	return (void *)L1_CACHE_ALIGN((u64)*base);
9245d9ca49SDean Nelson }
9345d9ca49SDean Nelson 
9445d9ca49SDean Nelson /*
9545d9ca49SDean Nelson  * Given a nasid, get the physical address of the  partition's reserved page
9645d9ca49SDean Nelson  * for that nasid. This function returns 0 on any error.
9745d9ca49SDean Nelson  */
9845d9ca49SDean Nelson static u64
9945d9ca49SDean Nelson xpc_get_rsvd_page_pa(int nasid)
10045d9ca49SDean Nelson {
10145d9ca49SDean Nelson 	bte_result_t bte_res;
10245d9ca49SDean Nelson 	s64 status;
10345d9ca49SDean Nelson 	u64 cookie = 0;
10445d9ca49SDean Nelson 	u64 rp_pa = nasid;	/* seed with nasid */
10545d9ca49SDean Nelson 	u64 len = 0;
10645d9ca49SDean Nelson 	u64 buf = buf;
10745d9ca49SDean Nelson 	u64 buf_len = 0;
10845d9ca49SDean Nelson 	void *buf_base = NULL;
10945d9ca49SDean Nelson 
11045d9ca49SDean Nelson 	while (1) {
11145d9ca49SDean Nelson 
11245d9ca49SDean Nelson 		status = sn_partition_reserved_page_pa(buf, &cookie, &rp_pa,
11345d9ca49SDean Nelson 						       &len);
11445d9ca49SDean Nelson 
11545d9ca49SDean Nelson 		dev_dbg(xpc_part, "SAL returned with status=%li, cookie="
11645d9ca49SDean Nelson 			"0x%016lx, address=0x%016lx, len=0x%016lx\n",
11745d9ca49SDean Nelson 			status, cookie, rp_pa, len);
11845d9ca49SDean Nelson 
1192c2b94f9SDean Nelson 		if (status != SALRET_MORE_PASSES)
12045d9ca49SDean Nelson 			break;
12145d9ca49SDean Nelson 
12245d9ca49SDean Nelson 		if (L1_CACHE_ALIGN(len) > buf_len) {
12345d9ca49SDean Nelson 			kfree(buf_base);
12445d9ca49SDean Nelson 			buf_len = L1_CACHE_ALIGN(len);
12545d9ca49SDean Nelson 			buf = (u64)xpc_kmalloc_cacheline_aligned(buf_len,
1264a3ad2ddSDean Nelson 								 GFP_KERNEL,
1274a3ad2ddSDean Nelson 								 &buf_base);
12845d9ca49SDean Nelson 			if (buf_base == NULL) {
12945d9ca49SDean Nelson 				dev_err(xpc_part, "unable to kmalloc "
13045d9ca49SDean Nelson 					"len=0x%016lx\n", buf_len);
13145d9ca49SDean Nelson 				status = SALRET_ERROR;
13245d9ca49SDean Nelson 				break;
13345d9ca49SDean Nelson 			}
13445d9ca49SDean Nelson 		}
13545d9ca49SDean Nelson 
13645d9ca49SDean Nelson 		bte_res = xp_bte_copy(rp_pa, buf, buf_len,
13745d9ca49SDean Nelson 				      (BTE_NOTIFY | BTE_WACQUIRE), NULL);
13845d9ca49SDean Nelson 		if (bte_res != BTE_SUCCESS) {
13945d9ca49SDean Nelson 			dev_dbg(xpc_part, "xp_bte_copy failed %i\n", bte_res);
14045d9ca49SDean Nelson 			status = SALRET_ERROR;
14145d9ca49SDean Nelson 			break;
14245d9ca49SDean Nelson 		}
14345d9ca49SDean Nelson 	}
14445d9ca49SDean Nelson 
14545d9ca49SDean Nelson 	kfree(buf_base);
14645d9ca49SDean Nelson 
1472c2b94f9SDean Nelson 	if (status != SALRET_OK)
14845d9ca49SDean Nelson 		rp_pa = 0;
1492c2b94f9SDean Nelson 
15045d9ca49SDean Nelson 	dev_dbg(xpc_part, "reserved page at phys address 0x%016lx\n", rp_pa);
15145d9ca49SDean Nelson 	return rp_pa;
15245d9ca49SDean Nelson }
15345d9ca49SDean Nelson 
15445d9ca49SDean Nelson /*
15545d9ca49SDean Nelson  * Fill the partition reserved page with the information needed by
15645d9ca49SDean Nelson  * other partitions to discover we are alive and establish initial
15745d9ca49SDean Nelson  * communications.
15845d9ca49SDean Nelson  */
15945d9ca49SDean Nelson struct xpc_rsvd_page *
16045d9ca49SDean Nelson xpc_rsvd_page_init(void)
16145d9ca49SDean Nelson {
16245d9ca49SDean Nelson 	struct xpc_rsvd_page *rp;
16345d9ca49SDean Nelson 	AMO_t *amos_page;
16445d9ca49SDean Nelson 	u64 rp_pa, nasid_array = 0;
16545d9ca49SDean Nelson 	int i, ret;
16645d9ca49SDean Nelson 
16745d9ca49SDean Nelson 	/* get the local reserved page's address */
16845d9ca49SDean Nelson 
16945d9ca49SDean Nelson 	preempt_disable();
17045d9ca49SDean Nelson 	rp_pa = xpc_get_rsvd_page_pa(cpuid_to_nasid(smp_processor_id()));
17145d9ca49SDean Nelson 	preempt_enable();
17245d9ca49SDean Nelson 	if (rp_pa == 0) {
17345d9ca49SDean Nelson 		dev_err(xpc_part, "SAL failed to locate the reserved page\n");
17445d9ca49SDean Nelson 		return NULL;
17545d9ca49SDean Nelson 	}
17645d9ca49SDean Nelson 	rp = (struct xpc_rsvd_page *)__va(rp_pa);
17745d9ca49SDean Nelson 
17845d9ca49SDean Nelson 	if (rp->partid != sn_partition_id) {
17945d9ca49SDean Nelson 		dev_err(xpc_part, "the reserved page's partid of %d should be "
18045d9ca49SDean Nelson 			"%d\n", rp->partid, sn_partition_id);
18145d9ca49SDean Nelson 		return NULL;
18245d9ca49SDean Nelson 	}
18345d9ca49SDean Nelson 
18445d9ca49SDean Nelson 	rp->version = XPC_RP_VERSION;
18545d9ca49SDean Nelson 
18645d9ca49SDean Nelson 	/* establish the actual sizes of the nasid masks */
18745d9ca49SDean Nelson 	if (rp->SAL_version == 1) {
18845d9ca49SDean Nelson 		/* SAL_version 1 didn't set the nasids_size field */
18945d9ca49SDean Nelson 		rp->nasids_size = 128;
19045d9ca49SDean Nelson 	}
19145d9ca49SDean Nelson 	xp_nasid_mask_bytes = rp->nasids_size;
19245d9ca49SDean Nelson 	xp_nasid_mask_words = xp_nasid_mask_bytes / 8;
19345d9ca49SDean Nelson 
19445d9ca49SDean Nelson 	/* setup the pointers to the various items in the reserved page */
19545d9ca49SDean Nelson 	xpc_part_nasids = XPC_RP_PART_NASIDS(rp);
19645d9ca49SDean Nelson 	xpc_mach_nasids = XPC_RP_MACH_NASIDS(rp);
19745d9ca49SDean Nelson 	xpc_vars = XPC_RP_VARS(rp);
19845d9ca49SDean Nelson 	xpc_vars_part = XPC_RP_VARS_PART(rp);
19945d9ca49SDean Nelson 
20045d9ca49SDean Nelson 	/*
20145d9ca49SDean Nelson 	 * Before clearing xpc_vars, see if a page of AMOs had been previously
20245d9ca49SDean Nelson 	 * allocated. If not we'll need to allocate one and set permissions
20345d9ca49SDean Nelson 	 * so that cross-partition AMOs are allowed.
20445d9ca49SDean Nelson 	 *
20545d9ca49SDean Nelson 	 * The allocated AMO page needs MCA reporting to remain disabled after
20645d9ca49SDean Nelson 	 * XPC has unloaded.  To make this work, we keep a copy of the pointer
20745d9ca49SDean Nelson 	 * to this page (i.e., amos_page) in the struct xpc_vars structure,
20845d9ca49SDean Nelson 	 * which is pointed to by the reserved page, and re-use that saved copy
20945d9ca49SDean Nelson 	 * on subsequent loads of XPC. This AMO page is never freed, and its
21045d9ca49SDean Nelson 	 * memory protections are never restricted.
21145d9ca49SDean Nelson 	 */
2122c2b94f9SDean Nelson 	amos_page = xpc_vars->amos_page;
2132c2b94f9SDean Nelson 	if (amos_page == NULL) {
214e4a064dfSDean Nelson 		amos_page = (AMO_t *)TO_AMO(uncached_alloc_page(0, 1));
21545d9ca49SDean Nelson 		if (amos_page == NULL) {
21645d9ca49SDean Nelson 			dev_err(xpc_part, "can't allocate page of AMOs\n");
21745d9ca49SDean Nelson 			return NULL;
21845d9ca49SDean Nelson 		}
21945d9ca49SDean Nelson 
22045d9ca49SDean Nelson 		/*
22145d9ca49SDean Nelson 		 * Open up AMO-R/W to cpu.  This is done for Shub 1.1 systems
22245d9ca49SDean Nelson 		 * when xpc_allow_IPI_ops() is called via xpc_hb_init().
22345d9ca49SDean Nelson 		 */
22445d9ca49SDean Nelson 		if (!enable_shub_wars_1_1()) {
22545d9ca49SDean Nelson 			ret = sn_change_memprotect(ia64_tpa((u64)amos_page),
2264a3ad2ddSDean Nelson 						   PAGE_SIZE,
2274a3ad2ddSDean Nelson 						   SN_MEMPROT_ACCESS_CLASS_1,
22845d9ca49SDean Nelson 						   &nasid_array);
22945d9ca49SDean Nelson 			if (ret != 0) {
23045d9ca49SDean Nelson 				dev_err(xpc_part, "can't change memory "
23145d9ca49SDean Nelson 					"protections\n");
23245d9ca49SDean Nelson 				uncached_free_page(__IA64_UNCACHED_OFFSET |
233e4a064dfSDean Nelson 						   TO_PHYS((u64)amos_page), 1);
23445d9ca49SDean Nelson 				return NULL;
23545d9ca49SDean Nelson 			}
23645d9ca49SDean Nelson 		}
23745d9ca49SDean Nelson 	} else if (!IS_AMO_ADDRESS((u64)amos_page)) {
23845d9ca49SDean Nelson 		/*
23945d9ca49SDean Nelson 		 * EFI's XPBOOT can also set amos_page in the reserved page,
24045d9ca49SDean Nelson 		 * but it happens to leave it as an uncached physical address
24145d9ca49SDean Nelson 		 * and we need it to be an uncached virtual, so we'll have to
24245d9ca49SDean Nelson 		 * convert it.
24345d9ca49SDean Nelson 		 */
24445d9ca49SDean Nelson 		if (!IS_AMO_PHYS_ADDRESS((u64)amos_page)) {
24545d9ca49SDean Nelson 			dev_err(xpc_part, "previously used amos_page address "
24645d9ca49SDean Nelson 				"is bad = 0x%p\n", (void *)amos_page);
24745d9ca49SDean Nelson 			return NULL;
24845d9ca49SDean Nelson 		}
24945d9ca49SDean Nelson 		amos_page = (AMO_t *)TO_AMO((u64)amos_page);
25045d9ca49SDean Nelson 	}
25145d9ca49SDean Nelson 
25245d9ca49SDean Nelson 	/* clear xpc_vars */
25345d9ca49SDean Nelson 	memset(xpc_vars, 0, sizeof(struct xpc_vars));
25445d9ca49SDean Nelson 
25545d9ca49SDean Nelson 	xpc_vars->version = XPC_V_VERSION;
25645d9ca49SDean Nelson 	xpc_vars->act_nasid = cpuid_to_nasid(0);
25745d9ca49SDean Nelson 	xpc_vars->act_phys_cpuid = cpu_physical_id(0);
25845d9ca49SDean Nelson 	xpc_vars->vars_part_pa = __pa(xpc_vars_part);
25945d9ca49SDean Nelson 	xpc_vars->amos_page_pa = ia64_tpa((u64)amos_page);
26045d9ca49SDean Nelson 	xpc_vars->amos_page = amos_page;	/* save for next load of XPC */
26145d9ca49SDean Nelson 
26245d9ca49SDean Nelson 	/* clear xpc_vars_part */
26345d9ca49SDean Nelson 	memset((u64 *)xpc_vars_part, 0, sizeof(struct xpc_vars_part) *
26445d9ca49SDean Nelson 	       XP_MAX_PARTITIONS);
26545d9ca49SDean Nelson 
26645d9ca49SDean Nelson 	/* initialize the activate IRQ related AMO variables */
2672c2b94f9SDean Nelson 	for (i = 0; i < xp_nasid_mask_words; i++)
26845d9ca49SDean Nelson 		(void)xpc_IPI_init(XPC_ACTIVATE_IRQ_AMOS + i);
26945d9ca49SDean Nelson 
27045d9ca49SDean Nelson 	/* initialize the engaged remote partitions related AMO variables */
27145d9ca49SDean Nelson 	(void)xpc_IPI_init(XPC_ENGAGED_PARTITIONS_AMO);
27245d9ca49SDean Nelson 	(void)xpc_IPI_init(XPC_DISENGAGE_REQUEST_AMO);
27345d9ca49SDean Nelson 
27445d9ca49SDean Nelson 	/* timestamp of when reserved page was setup by XPC */
27545d9ca49SDean Nelson 	rp->stamp = CURRENT_TIME;
27645d9ca49SDean Nelson 
27745d9ca49SDean Nelson 	/*
27845d9ca49SDean Nelson 	 * This signifies to the remote partition that our reserved
27945d9ca49SDean Nelson 	 * page is initialized.
28045d9ca49SDean Nelson 	 */
28145d9ca49SDean Nelson 	rp->vars_pa = __pa(xpc_vars);
28245d9ca49SDean Nelson 
28345d9ca49SDean Nelson 	return rp;
28445d9ca49SDean Nelson }
28545d9ca49SDean Nelson 
28645d9ca49SDean Nelson /*
28745d9ca49SDean Nelson  * Change protections to allow IPI operations (and AMO operations on
28845d9ca49SDean Nelson  * Shub 1.1 systems).
28945d9ca49SDean Nelson  */
29045d9ca49SDean Nelson void
29145d9ca49SDean Nelson xpc_allow_IPI_ops(void)
29245d9ca49SDean Nelson {
29345d9ca49SDean Nelson 	int node;
29445d9ca49SDean Nelson 	int nasid;
29545d9ca49SDean Nelson 
2962c2b94f9SDean Nelson 	/* >>> Change SH_IPI_ACCESS code to use SAL call once it is available */
29745d9ca49SDean Nelson 
29845d9ca49SDean Nelson 	if (is_shub2()) {
29945d9ca49SDean Nelson 		xpc_sh2_IPI_access0 =
30045d9ca49SDean Nelson 		    (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS0));
30145d9ca49SDean Nelson 		xpc_sh2_IPI_access1 =
30245d9ca49SDean Nelson 		    (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS1));
30345d9ca49SDean Nelson 		xpc_sh2_IPI_access2 =
30445d9ca49SDean Nelson 		    (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS2));
30545d9ca49SDean Nelson 		xpc_sh2_IPI_access3 =
30645d9ca49SDean Nelson 		    (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS3));
30745d9ca49SDean Nelson 
30845d9ca49SDean Nelson 		for_each_online_node(node) {
30945d9ca49SDean Nelson 			nasid = cnodeid_to_nasid(node);
31045d9ca49SDean Nelson 			HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
31145d9ca49SDean Nelson 			      -1UL);
31245d9ca49SDean Nelson 			HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
31345d9ca49SDean Nelson 			      -1UL);
31445d9ca49SDean Nelson 			HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
31545d9ca49SDean Nelson 			      -1UL);
31645d9ca49SDean Nelson 			HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
31745d9ca49SDean Nelson 			      -1UL);
31845d9ca49SDean Nelson 		}
31945d9ca49SDean Nelson 
32045d9ca49SDean Nelson 	} else {
32145d9ca49SDean Nelson 		xpc_sh1_IPI_access =
32245d9ca49SDean Nelson 		    (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH1_IPI_ACCESS));
32345d9ca49SDean Nelson 
32445d9ca49SDean Nelson 		for_each_online_node(node) {
32545d9ca49SDean Nelson 			nasid = cnodeid_to_nasid(node);
32645d9ca49SDean Nelson 			HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
32745d9ca49SDean Nelson 			      -1UL);
32845d9ca49SDean Nelson 
32945d9ca49SDean Nelson 			/*
33045d9ca49SDean Nelson 			 * Since the BIST collides with memory operations on
33145d9ca49SDean Nelson 			 * SHUB 1.1 sn_change_memprotect() cannot be used.
33245d9ca49SDean Nelson 			 */
33345d9ca49SDean Nelson 			if (enable_shub_wars_1_1()) {
33445d9ca49SDean Nelson 				/* open up everything */
33545d9ca49SDean Nelson 				xpc_prot_vec[node] = (u64)HUB_L((u64 *)
3364a3ad2ddSDean Nelson 								GLOBAL_MMR_ADDR
3374a3ad2ddSDean Nelson 								(nasid,
33845d9ca49SDean Nelson 						  SH1_MD_DQLP_MMR_DIR_PRIVEC0));
3394a3ad2ddSDean Nelson 				HUB_S((u64 *)
3404a3ad2ddSDean Nelson 				      GLOBAL_MMR_ADDR(nasid,
34145d9ca49SDean Nelson 						   SH1_MD_DQLP_MMR_DIR_PRIVEC0),
34245d9ca49SDean Nelson 				      -1UL);
3434a3ad2ddSDean Nelson 				HUB_S((u64 *)
3444a3ad2ddSDean Nelson 				      GLOBAL_MMR_ADDR(nasid,
34545d9ca49SDean Nelson 						   SH1_MD_DQRP_MMR_DIR_PRIVEC0),
34645d9ca49SDean Nelson 				      -1UL);
34745d9ca49SDean Nelson 			}
34845d9ca49SDean Nelson 		}
34945d9ca49SDean Nelson 	}
35045d9ca49SDean Nelson }
35145d9ca49SDean Nelson 
35245d9ca49SDean Nelson /*
35345d9ca49SDean Nelson  * Restrict protections to disallow IPI operations (and AMO operations on
35445d9ca49SDean Nelson  * Shub 1.1 systems).
35545d9ca49SDean Nelson  */
35645d9ca49SDean Nelson void
35745d9ca49SDean Nelson xpc_restrict_IPI_ops(void)
35845d9ca49SDean Nelson {
35945d9ca49SDean Nelson 	int node;
36045d9ca49SDean Nelson 	int nasid;
36145d9ca49SDean Nelson 
3622c2b94f9SDean Nelson 	/* >>> Change SH_IPI_ACCESS code to use SAL call once it is available */
36345d9ca49SDean Nelson 
36445d9ca49SDean Nelson 	if (is_shub2()) {
36545d9ca49SDean Nelson 
36645d9ca49SDean Nelson 		for_each_online_node(node) {
36745d9ca49SDean Nelson 			nasid = cnodeid_to_nasid(node);
36845d9ca49SDean Nelson 			HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
36945d9ca49SDean Nelson 			      xpc_sh2_IPI_access0);
37045d9ca49SDean Nelson 			HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
37145d9ca49SDean Nelson 			      xpc_sh2_IPI_access1);
37245d9ca49SDean Nelson 			HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
37345d9ca49SDean Nelson 			      xpc_sh2_IPI_access2);
37445d9ca49SDean Nelson 			HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
37545d9ca49SDean Nelson 			      xpc_sh2_IPI_access3);
37645d9ca49SDean Nelson 		}
37745d9ca49SDean Nelson 
37845d9ca49SDean Nelson 	} else {
37945d9ca49SDean Nelson 
38045d9ca49SDean Nelson 		for_each_online_node(node) {
38145d9ca49SDean Nelson 			nasid = cnodeid_to_nasid(node);
38245d9ca49SDean Nelson 			HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
38345d9ca49SDean Nelson 			      xpc_sh1_IPI_access);
38445d9ca49SDean Nelson 
38545d9ca49SDean Nelson 			if (enable_shub_wars_1_1()) {
38645d9ca49SDean Nelson 				HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
38745d9ca49SDean Nelson 						   SH1_MD_DQLP_MMR_DIR_PRIVEC0),
38845d9ca49SDean Nelson 				      xpc_prot_vec[node]);
38945d9ca49SDean Nelson 				HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
39045d9ca49SDean Nelson 						   SH1_MD_DQRP_MMR_DIR_PRIVEC0),
39145d9ca49SDean Nelson 				      xpc_prot_vec[node]);
39245d9ca49SDean Nelson 			}
39345d9ca49SDean Nelson 		}
39445d9ca49SDean Nelson 	}
39545d9ca49SDean Nelson }
39645d9ca49SDean Nelson 
39745d9ca49SDean Nelson /*
39845d9ca49SDean Nelson  * At periodic intervals, scan through all active partitions and ensure
39945d9ca49SDean Nelson  * their heartbeat is still active.  If not, the partition is deactivated.
40045d9ca49SDean Nelson  */
40145d9ca49SDean Nelson void
40245d9ca49SDean Nelson xpc_check_remote_hb(void)
40345d9ca49SDean Nelson {
40445d9ca49SDean Nelson 	struct xpc_vars *remote_vars;
40545d9ca49SDean Nelson 	struct xpc_partition *part;
406*64d032baSDean Nelson 	short partid;
40745d9ca49SDean Nelson 	bte_result_t bres;
40845d9ca49SDean Nelson 
40945d9ca49SDean Nelson 	remote_vars = (struct xpc_vars *)xpc_remote_copy_buffer;
41045d9ca49SDean Nelson 
41145d9ca49SDean Nelson 	for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
41245d9ca49SDean Nelson 
4132c2b94f9SDean Nelson 		if (xpc_exiting)
41445d9ca49SDean Nelson 			break;
41545d9ca49SDean Nelson 
4162c2b94f9SDean Nelson 		if (partid == sn_partition_id)
41745d9ca49SDean Nelson 			continue;
41845d9ca49SDean Nelson 
41945d9ca49SDean Nelson 		part = &xpc_partitions[partid];
42045d9ca49SDean Nelson 
42145d9ca49SDean Nelson 		if (part->act_state == XPC_P_INACTIVE ||
42245d9ca49SDean Nelson 		    part->act_state == XPC_P_DEACTIVATING) {
42345d9ca49SDean Nelson 			continue;
42445d9ca49SDean Nelson 		}
42545d9ca49SDean Nelson 
42645d9ca49SDean Nelson 		/* pull the remote_hb cache line */
42745d9ca49SDean Nelson 		bres = xp_bte_copy(part->remote_vars_pa,
42845d9ca49SDean Nelson 				   (u64)remote_vars,
42945d9ca49SDean Nelson 				   XPC_RP_VARS_SIZE,
43045d9ca49SDean Nelson 				   (BTE_NOTIFY | BTE_WACQUIRE), NULL);
43145d9ca49SDean Nelson 		if (bres != BTE_SUCCESS) {
43245d9ca49SDean Nelson 			XPC_DEACTIVATE_PARTITION(part,
43345d9ca49SDean Nelson 						 xpc_map_bte_errors(bres));
43445d9ca49SDean Nelson 			continue;
43545d9ca49SDean Nelson 		}
43645d9ca49SDean Nelson 
43745d9ca49SDean Nelson 		dev_dbg(xpc_part, "partid = %d, heartbeat = %ld, last_heartbeat"
43845d9ca49SDean Nelson 			" = %ld, heartbeat_offline = %ld, HB_mask = 0x%lx\n",
43945d9ca49SDean Nelson 			partid, remote_vars->heartbeat, part->last_heartbeat,
44045d9ca49SDean Nelson 			remote_vars->heartbeat_offline,
44145d9ca49SDean Nelson 			remote_vars->heartbeating_to_mask);
44245d9ca49SDean Nelson 
44345d9ca49SDean Nelson 		if (((remote_vars->heartbeat == part->last_heartbeat) &&
44445d9ca49SDean Nelson 		     (remote_vars->heartbeat_offline == 0)) ||
44545d9ca49SDean Nelson 		    !xpc_hb_allowed(sn_partition_id, remote_vars)) {
44645d9ca49SDean Nelson 
44765c17b80SDean Nelson 			XPC_DEACTIVATE_PARTITION(part, xpNoHeartbeat);
44845d9ca49SDean Nelson 			continue;
44945d9ca49SDean Nelson 		}
45045d9ca49SDean Nelson 
45145d9ca49SDean Nelson 		part->last_heartbeat = remote_vars->heartbeat;
45245d9ca49SDean Nelson 	}
45345d9ca49SDean Nelson }
45445d9ca49SDean Nelson 
45545d9ca49SDean Nelson /*
45645d9ca49SDean Nelson  * Get a copy of a portion of the remote partition's rsvd page.
45745d9ca49SDean Nelson  *
45845d9ca49SDean Nelson  * remote_rp points to a buffer that is cacheline aligned for BTE copies and
45945d9ca49SDean Nelson  * is large enough to contain a copy of their reserved page header and
46045d9ca49SDean Nelson  * part_nasids mask.
46145d9ca49SDean Nelson  */
46265c17b80SDean Nelson static enum xp_retval
46345d9ca49SDean Nelson xpc_get_remote_rp(int nasid, u64 *discovered_nasids,
46445d9ca49SDean Nelson 		  struct xpc_rsvd_page *remote_rp, u64 *remote_rp_pa)
46545d9ca49SDean Nelson {
46645d9ca49SDean Nelson 	int bres, i;
46745d9ca49SDean Nelson 
46845d9ca49SDean Nelson 	/* get the reserved page's physical address */
46945d9ca49SDean Nelson 
47045d9ca49SDean Nelson 	*remote_rp_pa = xpc_get_rsvd_page_pa(nasid);
4712c2b94f9SDean Nelson 	if (*remote_rp_pa == 0)
47265c17b80SDean Nelson 		return xpNoRsvdPageAddr;
47345d9ca49SDean Nelson 
47445d9ca49SDean Nelson 	/* pull over the reserved page header and part_nasids mask */
47545d9ca49SDean Nelson 	bres = xp_bte_copy(*remote_rp_pa, (u64)remote_rp,
47645d9ca49SDean Nelson 			   XPC_RP_HEADER_SIZE + xp_nasid_mask_bytes,
47745d9ca49SDean Nelson 			   (BTE_NOTIFY | BTE_WACQUIRE), NULL);
4782c2b94f9SDean Nelson 	if (bres != BTE_SUCCESS)
47945d9ca49SDean Nelson 		return xpc_map_bte_errors(bres);
48045d9ca49SDean Nelson 
48145d9ca49SDean Nelson 	if (discovered_nasids != NULL) {
48245d9ca49SDean Nelson 		u64 *remote_part_nasids = XPC_RP_PART_NASIDS(remote_rp);
48345d9ca49SDean Nelson 
4842c2b94f9SDean Nelson 		for (i = 0; i < xp_nasid_mask_words; i++)
48545d9ca49SDean Nelson 			discovered_nasids[i] |= remote_part_nasids[i];
48645d9ca49SDean Nelson 	}
48745d9ca49SDean Nelson 
48845d9ca49SDean Nelson 	/* check that the partid is for another partition */
48945d9ca49SDean Nelson 
49045d9ca49SDean Nelson 	if (remote_rp->partid < 1 ||
49145d9ca49SDean Nelson 	    remote_rp->partid > (XP_MAX_PARTITIONS - 1)) {
49265c17b80SDean Nelson 		return xpInvalidPartid;
49345d9ca49SDean Nelson 	}
49445d9ca49SDean Nelson 
4952c2b94f9SDean Nelson 	if (remote_rp->partid == sn_partition_id)
49665c17b80SDean Nelson 		return xpLocalPartid;
49745d9ca49SDean Nelson 
49845d9ca49SDean Nelson 	if (XPC_VERSION_MAJOR(remote_rp->version) !=
49945d9ca49SDean Nelson 	    XPC_VERSION_MAJOR(XPC_RP_VERSION)) {
50065c17b80SDean Nelson 		return xpBadVersion;
50145d9ca49SDean Nelson 	}
50245d9ca49SDean Nelson 
50365c17b80SDean Nelson 	return xpSuccess;
50445d9ca49SDean Nelson }
50545d9ca49SDean Nelson 
50645d9ca49SDean Nelson /*
50745d9ca49SDean Nelson  * Get a copy of the remote partition's XPC variables from the reserved page.
50845d9ca49SDean Nelson  *
50945d9ca49SDean Nelson  * remote_vars points to a buffer that is cacheline aligned for BTE copies and
51045d9ca49SDean Nelson  * assumed to be of size XPC_RP_VARS_SIZE.
51145d9ca49SDean Nelson  */
51265c17b80SDean Nelson static enum xp_retval
51345d9ca49SDean Nelson xpc_get_remote_vars(u64 remote_vars_pa, struct xpc_vars *remote_vars)
51445d9ca49SDean Nelson {
51545d9ca49SDean Nelson 	int bres;
51645d9ca49SDean Nelson 
5172c2b94f9SDean Nelson 	if (remote_vars_pa == 0)
51865c17b80SDean Nelson 		return xpVarsNotSet;
51945d9ca49SDean Nelson 
52045d9ca49SDean Nelson 	/* pull over the cross partition variables */
52145d9ca49SDean Nelson 	bres = xp_bte_copy(remote_vars_pa, (u64)remote_vars, XPC_RP_VARS_SIZE,
52245d9ca49SDean Nelson 			   (BTE_NOTIFY | BTE_WACQUIRE), NULL);
5232c2b94f9SDean Nelson 	if (bres != BTE_SUCCESS)
52445d9ca49SDean Nelson 		return xpc_map_bte_errors(bres);
52545d9ca49SDean Nelson 
52645d9ca49SDean Nelson 	if (XPC_VERSION_MAJOR(remote_vars->version) !=
52745d9ca49SDean Nelson 	    XPC_VERSION_MAJOR(XPC_V_VERSION)) {
52865c17b80SDean Nelson 		return xpBadVersion;
52945d9ca49SDean Nelson 	}
53045d9ca49SDean Nelson 
53165c17b80SDean Nelson 	return xpSuccess;
53245d9ca49SDean Nelson }
53345d9ca49SDean Nelson 
53445d9ca49SDean Nelson /*
53545d9ca49SDean Nelson  * Update the remote partition's info.
53645d9ca49SDean Nelson  */
53745d9ca49SDean Nelson static void
53845d9ca49SDean Nelson xpc_update_partition_info(struct xpc_partition *part, u8 remote_rp_version,
53945d9ca49SDean Nelson 			  struct timespec *remote_rp_stamp, u64 remote_rp_pa,
54045d9ca49SDean Nelson 			  u64 remote_vars_pa, struct xpc_vars *remote_vars)
54145d9ca49SDean Nelson {
54245d9ca49SDean Nelson 	part->remote_rp_version = remote_rp_version;
54345d9ca49SDean Nelson 	dev_dbg(xpc_part, "  remote_rp_version = 0x%016x\n",
54445d9ca49SDean Nelson 		part->remote_rp_version);
54545d9ca49SDean Nelson 
54645d9ca49SDean Nelson 	part->remote_rp_stamp = *remote_rp_stamp;
54745d9ca49SDean Nelson 	dev_dbg(xpc_part, "  remote_rp_stamp (tv_sec = 0x%lx tv_nsec = 0x%lx\n",
54845d9ca49SDean Nelson 		part->remote_rp_stamp.tv_sec, part->remote_rp_stamp.tv_nsec);
54945d9ca49SDean Nelson 
55045d9ca49SDean Nelson 	part->remote_rp_pa = remote_rp_pa;
55145d9ca49SDean Nelson 	dev_dbg(xpc_part, "  remote_rp_pa = 0x%016lx\n", part->remote_rp_pa);
55245d9ca49SDean Nelson 
55345d9ca49SDean Nelson 	part->remote_vars_pa = remote_vars_pa;
55445d9ca49SDean Nelson 	dev_dbg(xpc_part, "  remote_vars_pa = 0x%016lx\n",
55545d9ca49SDean Nelson 		part->remote_vars_pa);
55645d9ca49SDean Nelson 
55745d9ca49SDean Nelson 	part->last_heartbeat = remote_vars->heartbeat;
55845d9ca49SDean Nelson 	dev_dbg(xpc_part, "  last_heartbeat = 0x%016lx\n",
55945d9ca49SDean Nelson 		part->last_heartbeat);
56045d9ca49SDean Nelson 
56145d9ca49SDean Nelson 	part->remote_vars_part_pa = remote_vars->vars_part_pa;
56245d9ca49SDean Nelson 	dev_dbg(xpc_part, "  remote_vars_part_pa = 0x%016lx\n",
56345d9ca49SDean Nelson 		part->remote_vars_part_pa);
56445d9ca49SDean Nelson 
56545d9ca49SDean Nelson 	part->remote_act_nasid = remote_vars->act_nasid;
56645d9ca49SDean Nelson 	dev_dbg(xpc_part, "  remote_act_nasid = 0x%x\n",
56745d9ca49SDean Nelson 		part->remote_act_nasid);
56845d9ca49SDean Nelson 
56945d9ca49SDean Nelson 	part->remote_act_phys_cpuid = remote_vars->act_phys_cpuid;
57045d9ca49SDean Nelson 	dev_dbg(xpc_part, "  remote_act_phys_cpuid = 0x%x\n",
57145d9ca49SDean Nelson 		part->remote_act_phys_cpuid);
57245d9ca49SDean Nelson 
57345d9ca49SDean Nelson 	part->remote_amos_page_pa = remote_vars->amos_page_pa;
57445d9ca49SDean Nelson 	dev_dbg(xpc_part, "  remote_amos_page_pa = 0x%lx\n",
57545d9ca49SDean Nelson 		part->remote_amos_page_pa);
57645d9ca49SDean Nelson 
57745d9ca49SDean Nelson 	part->remote_vars_version = remote_vars->version;
57845d9ca49SDean Nelson 	dev_dbg(xpc_part, "  remote_vars_version = 0x%x\n",
57945d9ca49SDean Nelson 		part->remote_vars_version);
58045d9ca49SDean Nelson }
58145d9ca49SDean Nelson 
58245d9ca49SDean Nelson /*
58345d9ca49SDean Nelson  * Prior code has determined the nasid which generated an IPI.  Inspect
58445d9ca49SDean Nelson  * that nasid to determine if its partition needs to be activated or
58545d9ca49SDean Nelson  * deactivated.
58645d9ca49SDean Nelson  *
58745d9ca49SDean Nelson  * A partition is consider "awaiting activation" if our partition
58845d9ca49SDean Nelson  * flags indicate it is not active and it has a heartbeat.  A
58945d9ca49SDean Nelson  * partition is considered "awaiting deactivation" if our partition
59045d9ca49SDean Nelson  * flags indicate it is active but it has no heartbeat or it is not
59145d9ca49SDean Nelson  * sending its heartbeat to us.
59245d9ca49SDean Nelson  *
59345d9ca49SDean Nelson  * To determine the heartbeat, the remote nasid must have a properly
59445d9ca49SDean Nelson  * initialized reserved page.
59545d9ca49SDean Nelson  */
59645d9ca49SDean Nelson static void
59745d9ca49SDean Nelson xpc_identify_act_IRQ_req(int nasid)
59845d9ca49SDean Nelson {
59945d9ca49SDean Nelson 	struct xpc_rsvd_page *remote_rp;
60045d9ca49SDean Nelson 	struct xpc_vars *remote_vars;
60145d9ca49SDean Nelson 	u64 remote_rp_pa;
60245d9ca49SDean Nelson 	u64 remote_vars_pa;
60345d9ca49SDean Nelson 	int remote_rp_version;
60445d9ca49SDean Nelson 	int reactivate = 0;
60545d9ca49SDean Nelson 	int stamp_diff;
60645d9ca49SDean Nelson 	struct timespec remote_rp_stamp = { 0, 0 };
607*64d032baSDean Nelson 	short partid;
60845d9ca49SDean Nelson 	struct xpc_partition *part;
60965c17b80SDean Nelson 	enum xp_retval ret;
61045d9ca49SDean Nelson 
61145d9ca49SDean Nelson 	/* pull over the reserved page structure */
61245d9ca49SDean Nelson 
61345d9ca49SDean Nelson 	remote_rp = (struct xpc_rsvd_page *)xpc_remote_copy_buffer;
61445d9ca49SDean Nelson 
61545d9ca49SDean Nelson 	ret = xpc_get_remote_rp(nasid, NULL, remote_rp, &remote_rp_pa);
61665c17b80SDean Nelson 	if (ret != xpSuccess) {
61745d9ca49SDean Nelson 		dev_warn(xpc_part, "unable to get reserved page from nasid %d, "
61845d9ca49SDean Nelson 			 "which sent interrupt, reason=%d\n", nasid, ret);
61945d9ca49SDean Nelson 		return;
62045d9ca49SDean Nelson 	}
62145d9ca49SDean Nelson 
62245d9ca49SDean Nelson 	remote_vars_pa = remote_rp->vars_pa;
62345d9ca49SDean Nelson 	remote_rp_version = remote_rp->version;
6242c2b94f9SDean Nelson 	if (XPC_SUPPORTS_RP_STAMP(remote_rp_version))
62545d9ca49SDean Nelson 		remote_rp_stamp = remote_rp->stamp;
6262c2b94f9SDean Nelson 
62745d9ca49SDean Nelson 	partid = remote_rp->partid;
62845d9ca49SDean Nelson 	part = &xpc_partitions[partid];
62945d9ca49SDean Nelson 
63045d9ca49SDean Nelson 	/* pull over the cross partition variables */
63145d9ca49SDean Nelson 
63245d9ca49SDean Nelson 	remote_vars = (struct xpc_vars *)xpc_remote_copy_buffer;
63345d9ca49SDean Nelson 
63445d9ca49SDean Nelson 	ret = xpc_get_remote_vars(remote_vars_pa, remote_vars);
63565c17b80SDean Nelson 	if (ret != xpSuccess) {
63645d9ca49SDean Nelson 
63745d9ca49SDean Nelson 		dev_warn(xpc_part, "unable to get XPC variables from nasid %d, "
63845d9ca49SDean Nelson 			 "which sent interrupt, reason=%d\n", nasid, ret);
63945d9ca49SDean Nelson 
64045d9ca49SDean Nelson 		XPC_DEACTIVATE_PARTITION(part, ret);
64145d9ca49SDean Nelson 		return;
64245d9ca49SDean Nelson 	}
64345d9ca49SDean Nelson 
64445d9ca49SDean Nelson 	part->act_IRQ_rcvd++;
64545d9ca49SDean Nelson 
64645d9ca49SDean Nelson 	dev_dbg(xpc_part, "partid for nasid %d is %d; IRQs = %d; HB = "
64745d9ca49SDean Nelson 		"%ld:0x%lx\n", (int)nasid, (int)partid, part->act_IRQ_rcvd,
64845d9ca49SDean Nelson 		remote_vars->heartbeat, remote_vars->heartbeating_to_mask);
64945d9ca49SDean Nelson 
6502c2b94f9SDean Nelson 	if (xpc_partition_disengaged(part) &&
6512c2b94f9SDean Nelson 	    part->act_state == XPC_P_INACTIVE) {
65245d9ca49SDean Nelson 
65345d9ca49SDean Nelson 		xpc_update_partition_info(part, remote_rp_version,
65445d9ca49SDean Nelson 					  &remote_rp_stamp, remote_rp_pa,
65545d9ca49SDean Nelson 					  remote_vars_pa, remote_vars);
65645d9ca49SDean Nelson 
65745d9ca49SDean Nelson 		if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) {
65845d9ca49SDean Nelson 			if (xpc_partition_disengage_requested(1UL << partid)) {
65945d9ca49SDean Nelson 				/*
66045d9ca49SDean Nelson 				 * Other side is waiting on us to disengage,
66145d9ca49SDean Nelson 				 * even though we already have.
66245d9ca49SDean Nelson 				 */
66345d9ca49SDean Nelson 				return;
66445d9ca49SDean Nelson 			}
66545d9ca49SDean Nelson 		} else {
66645d9ca49SDean Nelson 			/* other side doesn't support disengage requests */
66745d9ca49SDean Nelson 			xpc_clear_partition_disengage_request(1UL << partid);
66845d9ca49SDean Nelson 		}
66945d9ca49SDean Nelson 
67045d9ca49SDean Nelson 		xpc_activate_partition(part);
67145d9ca49SDean Nelson 		return;
67245d9ca49SDean Nelson 	}
67345d9ca49SDean Nelson 
67445d9ca49SDean Nelson 	DBUG_ON(part->remote_rp_version == 0);
67545d9ca49SDean Nelson 	DBUG_ON(part->remote_vars_version == 0);
67645d9ca49SDean Nelson 
67745d9ca49SDean Nelson 	if (!XPC_SUPPORTS_RP_STAMP(part->remote_rp_version)) {
67845d9ca49SDean Nelson 		DBUG_ON(XPC_SUPPORTS_DISENGAGE_REQUEST(part->
67945d9ca49SDean Nelson 						       remote_vars_version));
68045d9ca49SDean Nelson 
68145d9ca49SDean Nelson 		if (!XPC_SUPPORTS_RP_STAMP(remote_rp_version)) {
68245d9ca49SDean Nelson 			DBUG_ON(XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->
68345d9ca49SDean Nelson 							       version));
68445d9ca49SDean Nelson 			/* see if the other side rebooted */
68545d9ca49SDean Nelson 			if (part->remote_amos_page_pa ==
68645d9ca49SDean Nelson 			    remote_vars->amos_page_pa &&
6874a3ad2ddSDean Nelson 			    xpc_hb_allowed(sn_partition_id, remote_vars)) {
68845d9ca49SDean Nelson 				/* doesn't look that way, so ignore the IPI */
68945d9ca49SDean Nelson 				return;
69045d9ca49SDean Nelson 			}
69145d9ca49SDean Nelson 		}
69245d9ca49SDean Nelson 
69345d9ca49SDean Nelson 		/*
69445d9ca49SDean Nelson 		 * Other side rebooted and previous XPC didn't support the
69545d9ca49SDean Nelson 		 * disengage request, so we don't need to do anything special.
69645d9ca49SDean Nelson 		 */
69745d9ca49SDean Nelson 
69845d9ca49SDean Nelson 		xpc_update_partition_info(part, remote_rp_version,
69945d9ca49SDean Nelson 					  &remote_rp_stamp, remote_rp_pa,
70045d9ca49SDean Nelson 					  remote_vars_pa, remote_vars);
70145d9ca49SDean Nelson 		part->reactivate_nasid = nasid;
70265c17b80SDean Nelson 		XPC_DEACTIVATE_PARTITION(part, xpReactivating);
70345d9ca49SDean Nelson 		return;
70445d9ca49SDean Nelson 	}
70545d9ca49SDean Nelson 
70645d9ca49SDean Nelson 	DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version));
70745d9ca49SDean Nelson 
70845d9ca49SDean Nelson 	if (!XPC_SUPPORTS_RP_STAMP(remote_rp_version)) {
70945d9ca49SDean Nelson 		DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->version));
71045d9ca49SDean Nelson 
71145d9ca49SDean Nelson 		/*
71245d9ca49SDean Nelson 		 * Other side rebooted and previous XPC did support the
71345d9ca49SDean Nelson 		 * disengage request, but the new one doesn't.
71445d9ca49SDean Nelson 		 */
71545d9ca49SDean Nelson 
71645d9ca49SDean Nelson 		xpc_clear_partition_engaged(1UL << partid);
71745d9ca49SDean Nelson 		xpc_clear_partition_disengage_request(1UL << partid);
71845d9ca49SDean Nelson 
71945d9ca49SDean Nelson 		xpc_update_partition_info(part, remote_rp_version,
72045d9ca49SDean Nelson 					  &remote_rp_stamp, remote_rp_pa,
72145d9ca49SDean Nelson 					  remote_vars_pa, remote_vars);
72245d9ca49SDean Nelson 		reactivate = 1;
72345d9ca49SDean Nelson 
72445d9ca49SDean Nelson 	} else {
72545d9ca49SDean Nelson 		DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->version));
72645d9ca49SDean Nelson 
72745d9ca49SDean Nelson 		stamp_diff = xpc_compare_stamps(&part->remote_rp_stamp,
72845d9ca49SDean Nelson 						&remote_rp_stamp);
72945d9ca49SDean Nelson 		if (stamp_diff != 0) {
73045d9ca49SDean Nelson 			DBUG_ON(stamp_diff >= 0);
73145d9ca49SDean Nelson 
73245d9ca49SDean Nelson 			/*
73345d9ca49SDean Nelson 			 * Other side rebooted and the previous XPC did support
73445d9ca49SDean Nelson 			 * the disengage request, as does the new one.
73545d9ca49SDean Nelson 			 */
73645d9ca49SDean Nelson 
73745d9ca49SDean Nelson 			DBUG_ON(xpc_partition_engaged(1UL << partid));
73845d9ca49SDean Nelson 			DBUG_ON(xpc_partition_disengage_requested(1UL <<
73945d9ca49SDean Nelson 								  partid));
74045d9ca49SDean Nelson 
74145d9ca49SDean Nelson 			xpc_update_partition_info(part, remote_rp_version,
7424a3ad2ddSDean Nelson 						  &remote_rp_stamp,
7434a3ad2ddSDean Nelson 						  remote_rp_pa, remote_vars_pa,
7444a3ad2ddSDean Nelson 						  remote_vars);
74545d9ca49SDean Nelson 			reactivate = 1;
74645d9ca49SDean Nelson 		}
74745d9ca49SDean Nelson 	}
74845d9ca49SDean Nelson 
74945d9ca49SDean Nelson 	if (part->disengage_request_timeout > 0 &&
75045d9ca49SDean Nelson 	    !xpc_partition_disengaged(part)) {
75145d9ca49SDean Nelson 		/* still waiting on other side to disengage from us */
75245d9ca49SDean Nelson 		return;
75345d9ca49SDean Nelson 	}
75445d9ca49SDean Nelson 
75545d9ca49SDean Nelson 	if (reactivate) {
75645d9ca49SDean Nelson 		part->reactivate_nasid = nasid;
75765c17b80SDean Nelson 		XPC_DEACTIVATE_PARTITION(part, xpReactivating);
75845d9ca49SDean Nelson 
75945d9ca49SDean Nelson 	} else if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version) &&
76045d9ca49SDean Nelson 		   xpc_partition_disengage_requested(1UL << partid)) {
76165c17b80SDean Nelson 		XPC_DEACTIVATE_PARTITION(part, xpOtherGoingDown);
76245d9ca49SDean Nelson 	}
76345d9ca49SDean Nelson }
76445d9ca49SDean Nelson 
76545d9ca49SDean Nelson /*
76645d9ca49SDean Nelson  * Loop through the activation AMO variables and process any bits
76745d9ca49SDean Nelson  * which are set.  Each bit indicates a nasid sending a partition
76845d9ca49SDean Nelson  * activation or deactivation request.
76945d9ca49SDean Nelson  *
77045d9ca49SDean Nelson  * Return #of IRQs detected.
77145d9ca49SDean Nelson  */
77245d9ca49SDean Nelson int
77345d9ca49SDean Nelson xpc_identify_act_IRQ_sender(void)
77445d9ca49SDean Nelson {
77545d9ca49SDean Nelson 	int word, bit;
77645d9ca49SDean Nelson 	u64 nasid_mask;
77745d9ca49SDean Nelson 	u64 nasid;		/* remote nasid */
77845d9ca49SDean Nelson 	int n_IRQs_detected = 0;
77945d9ca49SDean Nelson 	AMO_t *act_amos;
78045d9ca49SDean Nelson 
78145d9ca49SDean Nelson 	act_amos = xpc_vars->amos_page + XPC_ACTIVATE_IRQ_AMOS;
78245d9ca49SDean Nelson 
78345d9ca49SDean Nelson 	/* scan through act AMO variable looking for non-zero entries */
78445d9ca49SDean Nelson 	for (word = 0; word < xp_nasid_mask_words; word++) {
78545d9ca49SDean Nelson 
7862c2b94f9SDean Nelson 		if (xpc_exiting)
78745d9ca49SDean Nelson 			break;
78845d9ca49SDean Nelson 
78945d9ca49SDean Nelson 		nasid_mask = xpc_IPI_receive(&act_amos[word]);
79045d9ca49SDean Nelson 		if (nasid_mask == 0) {
79145d9ca49SDean Nelson 			/* no IRQs from nasids in this variable */
79245d9ca49SDean Nelson 			continue;
79345d9ca49SDean Nelson 		}
79445d9ca49SDean Nelson 
79545d9ca49SDean Nelson 		dev_dbg(xpc_part, "AMO[%d] gave back 0x%lx\n", word,
79645d9ca49SDean Nelson 			nasid_mask);
79745d9ca49SDean Nelson 
79845d9ca49SDean Nelson 		/*
79945d9ca49SDean Nelson 		 * If this nasid has been added to the machine since
80045d9ca49SDean Nelson 		 * our partition was reset, this will retain the
80145d9ca49SDean Nelson 		 * remote nasid in our reserved pages machine mask.
80245d9ca49SDean Nelson 		 * This is used in the event of module reload.
80345d9ca49SDean Nelson 		 */
80445d9ca49SDean Nelson 		xpc_mach_nasids[word] |= nasid_mask;
80545d9ca49SDean Nelson 
80645d9ca49SDean Nelson 		/* locate the nasid(s) which sent interrupts */
80745d9ca49SDean Nelson 
80845d9ca49SDean Nelson 		for (bit = 0; bit < (8 * sizeof(u64)); bit++) {
80945d9ca49SDean Nelson 			if (nasid_mask & (1UL << bit)) {
81045d9ca49SDean Nelson 				n_IRQs_detected++;
81145d9ca49SDean Nelson 				nasid = XPC_NASID_FROM_W_B(word, bit);
81245d9ca49SDean Nelson 				dev_dbg(xpc_part, "interrupt from nasid %ld\n",
81345d9ca49SDean Nelson 					nasid);
81445d9ca49SDean Nelson 				xpc_identify_act_IRQ_req(nasid);
81545d9ca49SDean Nelson 			}
81645d9ca49SDean Nelson 		}
81745d9ca49SDean Nelson 	}
81845d9ca49SDean Nelson 	return n_IRQs_detected;
81945d9ca49SDean Nelson }
82045d9ca49SDean Nelson 
82145d9ca49SDean Nelson /*
82245d9ca49SDean Nelson  * See if the other side has responded to a partition disengage request
82345d9ca49SDean Nelson  * from us.
82445d9ca49SDean Nelson  */
82545d9ca49SDean Nelson int
82645d9ca49SDean Nelson xpc_partition_disengaged(struct xpc_partition *part)
82745d9ca49SDean Nelson {
828*64d032baSDean Nelson 	short partid = XPC_PARTID(part);
82945d9ca49SDean Nelson 	int disengaged;
83045d9ca49SDean Nelson 
83145d9ca49SDean Nelson 	disengaged = (xpc_partition_engaged(1UL << partid) == 0);
83245d9ca49SDean Nelson 	if (part->disengage_request_timeout) {
83345d9ca49SDean Nelson 		if (!disengaged) {
8342c2b94f9SDean Nelson 			if (time_before(jiffies,
8352c2b94f9SDean Nelson 			    part->disengage_request_timeout)) {
83645d9ca49SDean Nelson 				/* timelimit hasn't been reached yet */
83745d9ca49SDean Nelson 				return 0;
83845d9ca49SDean Nelson 			}
83945d9ca49SDean Nelson 
84045d9ca49SDean Nelson 			/*
84145d9ca49SDean Nelson 			 * Other side hasn't responded to our disengage
84245d9ca49SDean Nelson 			 * request in a timely fashion, so assume it's dead.
84345d9ca49SDean Nelson 			 */
84445d9ca49SDean Nelson 
84545d9ca49SDean Nelson 			dev_info(xpc_part, "disengage from remote partition %d "
84645d9ca49SDean Nelson 				 "timed out\n", partid);
84745d9ca49SDean Nelson 			xpc_disengage_request_timedout = 1;
84845d9ca49SDean Nelson 			xpc_clear_partition_engaged(1UL << partid);
84945d9ca49SDean Nelson 			disengaged = 1;
85045d9ca49SDean Nelson 		}
85145d9ca49SDean Nelson 		part->disengage_request_timeout = 0;
85245d9ca49SDean Nelson 
85345d9ca49SDean Nelson 		/* cancel the timer function, provided it's not us */
85445d9ca49SDean Nelson 		if (!in_interrupt()) {
85545d9ca49SDean Nelson 			del_singleshot_timer_sync(&part->
85645d9ca49SDean Nelson 						  disengage_request_timer);
85745d9ca49SDean Nelson 		}
85845d9ca49SDean Nelson 
85945d9ca49SDean Nelson 		DBUG_ON(part->act_state != XPC_P_DEACTIVATING &&
86045d9ca49SDean Nelson 			part->act_state != XPC_P_INACTIVE);
8612c2b94f9SDean Nelson 		if (part->act_state != XPC_P_INACTIVE)
86245d9ca49SDean Nelson 			xpc_wakeup_channel_mgr(part);
86345d9ca49SDean Nelson 
8642c2b94f9SDean Nelson 		if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version))
86545d9ca49SDean Nelson 			xpc_cancel_partition_disengage_request(part);
86645d9ca49SDean Nelson 	}
86745d9ca49SDean Nelson 	return disengaged;
86845d9ca49SDean Nelson }
86945d9ca49SDean Nelson 
87045d9ca49SDean Nelson /*
87145d9ca49SDean Nelson  * Mark specified partition as active.
87245d9ca49SDean Nelson  */
87365c17b80SDean Nelson enum xp_retval
87445d9ca49SDean Nelson xpc_mark_partition_active(struct xpc_partition *part)
87545d9ca49SDean Nelson {
87645d9ca49SDean Nelson 	unsigned long irq_flags;
87765c17b80SDean Nelson 	enum xp_retval ret;
87845d9ca49SDean Nelson 
87945d9ca49SDean Nelson 	dev_dbg(xpc_part, "setting partition %d to ACTIVE\n", XPC_PARTID(part));
88045d9ca49SDean Nelson 
88145d9ca49SDean Nelson 	spin_lock_irqsave(&part->act_lock, irq_flags);
88245d9ca49SDean Nelson 	if (part->act_state == XPC_P_ACTIVATING) {
88345d9ca49SDean Nelson 		part->act_state = XPC_P_ACTIVE;
88465c17b80SDean Nelson 		ret = xpSuccess;
88545d9ca49SDean Nelson 	} else {
88665c17b80SDean Nelson 		DBUG_ON(part->reason == xpSuccess);
88745d9ca49SDean Nelson 		ret = part->reason;
88845d9ca49SDean Nelson 	}
88945d9ca49SDean Nelson 	spin_unlock_irqrestore(&part->act_lock, irq_flags);
89045d9ca49SDean Nelson 
89145d9ca49SDean Nelson 	return ret;
89245d9ca49SDean Nelson }
89345d9ca49SDean Nelson 
89445d9ca49SDean Nelson /*
89545d9ca49SDean Nelson  * Notify XPC that the partition is down.
89645d9ca49SDean Nelson  */
89745d9ca49SDean Nelson void
89845d9ca49SDean Nelson xpc_deactivate_partition(const int line, struct xpc_partition *part,
89965c17b80SDean Nelson 			 enum xp_retval reason)
90045d9ca49SDean Nelson {
90145d9ca49SDean Nelson 	unsigned long irq_flags;
90245d9ca49SDean Nelson 
90345d9ca49SDean Nelson 	spin_lock_irqsave(&part->act_lock, irq_flags);
90445d9ca49SDean Nelson 
90545d9ca49SDean Nelson 	if (part->act_state == XPC_P_INACTIVE) {
90645d9ca49SDean Nelson 		XPC_SET_REASON(part, reason, line);
90745d9ca49SDean Nelson 		spin_unlock_irqrestore(&part->act_lock, irq_flags);
90865c17b80SDean Nelson 		if (reason == xpReactivating) {
90945d9ca49SDean Nelson 			/* we interrupt ourselves to reactivate partition */
91045d9ca49SDean Nelson 			xpc_IPI_send_reactivate(part);
91145d9ca49SDean Nelson 		}
91245d9ca49SDean Nelson 		return;
91345d9ca49SDean Nelson 	}
91445d9ca49SDean Nelson 	if (part->act_state == XPC_P_DEACTIVATING) {
91565c17b80SDean Nelson 		if ((part->reason == xpUnloading && reason != xpUnloading) ||
91665c17b80SDean Nelson 		    reason == xpReactivating) {
91745d9ca49SDean Nelson 			XPC_SET_REASON(part, reason, line);
91845d9ca49SDean Nelson 		}
91945d9ca49SDean Nelson 		spin_unlock_irqrestore(&part->act_lock, irq_flags);
92045d9ca49SDean Nelson 		return;
92145d9ca49SDean Nelson 	}
92245d9ca49SDean Nelson 
92345d9ca49SDean Nelson 	part->act_state = XPC_P_DEACTIVATING;
92445d9ca49SDean Nelson 	XPC_SET_REASON(part, reason, line);
92545d9ca49SDean Nelson 
92645d9ca49SDean Nelson 	spin_unlock_irqrestore(&part->act_lock, irq_flags);
92745d9ca49SDean Nelson 
92845d9ca49SDean Nelson 	if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) {
92945d9ca49SDean Nelson 		xpc_request_partition_disengage(part);
93045d9ca49SDean Nelson 		xpc_IPI_send_disengage(part);
93145d9ca49SDean Nelson 
93245d9ca49SDean Nelson 		/* set a timelimit on the disengage request */
93345d9ca49SDean Nelson 		part->disengage_request_timeout = jiffies +
93445d9ca49SDean Nelson 		    (xpc_disengage_request_timelimit * HZ);
93545d9ca49SDean Nelson 		part->disengage_request_timer.expires =
93645d9ca49SDean Nelson 		    part->disengage_request_timeout;
93745d9ca49SDean Nelson 		add_timer(&part->disengage_request_timer);
93845d9ca49SDean Nelson 	}
93945d9ca49SDean Nelson 
94045d9ca49SDean Nelson 	dev_dbg(xpc_part, "bringing partition %d down, reason = %d\n",
94145d9ca49SDean Nelson 		XPC_PARTID(part), reason);
94245d9ca49SDean Nelson 
94345d9ca49SDean Nelson 	xpc_partition_going_down(part, reason);
94445d9ca49SDean Nelson }
94545d9ca49SDean Nelson 
94645d9ca49SDean Nelson /*
94745d9ca49SDean Nelson  * Mark specified partition as inactive.
94845d9ca49SDean Nelson  */
94945d9ca49SDean Nelson void
95045d9ca49SDean Nelson xpc_mark_partition_inactive(struct xpc_partition *part)
95145d9ca49SDean Nelson {
95245d9ca49SDean Nelson 	unsigned long irq_flags;
95345d9ca49SDean Nelson 
95445d9ca49SDean Nelson 	dev_dbg(xpc_part, "setting partition %d to INACTIVE\n",
95545d9ca49SDean Nelson 		XPC_PARTID(part));
95645d9ca49SDean Nelson 
95745d9ca49SDean Nelson 	spin_lock_irqsave(&part->act_lock, irq_flags);
95845d9ca49SDean Nelson 	part->act_state = XPC_P_INACTIVE;
95945d9ca49SDean Nelson 	spin_unlock_irqrestore(&part->act_lock, irq_flags);
96045d9ca49SDean Nelson 	part->remote_rp_pa = 0;
96145d9ca49SDean Nelson }
96245d9ca49SDean Nelson 
96345d9ca49SDean Nelson /*
96445d9ca49SDean Nelson  * SAL has provided a partition and machine mask.  The partition mask
96545d9ca49SDean Nelson  * contains a bit for each even nasid in our partition.  The machine
96645d9ca49SDean Nelson  * mask contains a bit for each even nasid in the entire machine.
96745d9ca49SDean Nelson  *
96845d9ca49SDean Nelson  * Using those two bit arrays, we can determine which nasids are
96945d9ca49SDean Nelson  * known in the machine.  Each should also have a reserved page
97045d9ca49SDean Nelson  * initialized if they are available for partitioning.
97145d9ca49SDean Nelson  */
97245d9ca49SDean Nelson void
97345d9ca49SDean Nelson xpc_discovery(void)
97445d9ca49SDean Nelson {
97545d9ca49SDean Nelson 	void *remote_rp_base;
97645d9ca49SDean Nelson 	struct xpc_rsvd_page *remote_rp;
97745d9ca49SDean Nelson 	struct xpc_vars *remote_vars;
97845d9ca49SDean Nelson 	u64 remote_rp_pa;
97945d9ca49SDean Nelson 	u64 remote_vars_pa;
98045d9ca49SDean Nelson 	int region;
98145d9ca49SDean Nelson 	int region_size;
98245d9ca49SDean Nelson 	int max_regions;
98345d9ca49SDean Nelson 	int nasid;
98445d9ca49SDean Nelson 	struct xpc_rsvd_page *rp;
985*64d032baSDean Nelson 	short partid;
98645d9ca49SDean Nelson 	struct xpc_partition *part;
98745d9ca49SDean Nelson 	u64 *discovered_nasids;
98865c17b80SDean Nelson 	enum xp_retval ret;
98945d9ca49SDean Nelson 
99045d9ca49SDean Nelson 	remote_rp = xpc_kmalloc_cacheline_aligned(XPC_RP_HEADER_SIZE +
99145d9ca49SDean Nelson 						  xp_nasid_mask_bytes,
99245d9ca49SDean Nelson 						  GFP_KERNEL, &remote_rp_base);
9932c2b94f9SDean Nelson 	if (remote_rp == NULL)
99445d9ca49SDean Nelson 		return;
9952c2b94f9SDean Nelson 
99645d9ca49SDean Nelson 	remote_vars = (struct xpc_vars *)remote_rp;
99745d9ca49SDean Nelson 
99845d9ca49SDean Nelson 	discovered_nasids = kzalloc(sizeof(u64) * xp_nasid_mask_words,
99945d9ca49SDean Nelson 				    GFP_KERNEL);
100045d9ca49SDean Nelson 	if (discovered_nasids == NULL) {
100145d9ca49SDean Nelson 		kfree(remote_rp_base);
100245d9ca49SDean Nelson 		return;
100345d9ca49SDean Nelson 	}
100445d9ca49SDean Nelson 
100545d9ca49SDean Nelson 	rp = (struct xpc_rsvd_page *)xpc_rsvd_page;
100645d9ca49SDean Nelson 
100745d9ca49SDean Nelson 	/*
100845d9ca49SDean Nelson 	 * The term 'region' in this context refers to the minimum number of
100945d9ca49SDean Nelson 	 * nodes that can comprise an access protection grouping. The access
101045d9ca49SDean Nelson 	 * protection is in regards to memory, IOI and IPI.
101145d9ca49SDean Nelson 	 */
101245d9ca49SDean Nelson 	max_regions = 64;
101345d9ca49SDean Nelson 	region_size = sn_region_size;
101445d9ca49SDean Nelson 
101545d9ca49SDean Nelson 	switch (region_size) {
101645d9ca49SDean Nelson 	case 128:
101745d9ca49SDean Nelson 		max_regions *= 2;
101845d9ca49SDean Nelson 	case 64:
101945d9ca49SDean Nelson 		max_regions *= 2;
102045d9ca49SDean Nelson 	case 32:
102145d9ca49SDean Nelson 		max_regions *= 2;
102245d9ca49SDean Nelson 		region_size = 16;
102345d9ca49SDean Nelson 		DBUG_ON(!is_shub2());
102445d9ca49SDean Nelson 	}
102545d9ca49SDean Nelson 
102645d9ca49SDean Nelson 	for (region = 0; region < max_regions; region++) {
102745d9ca49SDean Nelson 
10282c2b94f9SDean Nelson 		if (xpc_exiting)
102945d9ca49SDean Nelson 			break;
103045d9ca49SDean Nelson 
103145d9ca49SDean Nelson 		dev_dbg(xpc_part, "searching region %d\n", region);
103245d9ca49SDean Nelson 
103345d9ca49SDean Nelson 		for (nasid = (region * region_size * 2);
10344a3ad2ddSDean Nelson 		     nasid < ((region + 1) * region_size * 2); nasid += 2) {
103545d9ca49SDean Nelson 
10362c2b94f9SDean Nelson 			if (xpc_exiting)
103745d9ca49SDean Nelson 				break;
103845d9ca49SDean Nelson 
103945d9ca49SDean Nelson 			dev_dbg(xpc_part, "checking nasid %d\n", nasid);
104045d9ca49SDean Nelson 
104145d9ca49SDean Nelson 			if (XPC_NASID_IN_ARRAY(nasid, xpc_part_nasids)) {
104245d9ca49SDean Nelson 				dev_dbg(xpc_part, "PROM indicates Nasid %d is "
104345d9ca49SDean Nelson 					"part of the local partition; skipping "
104445d9ca49SDean Nelson 					"region\n", nasid);
104545d9ca49SDean Nelson 				break;
104645d9ca49SDean Nelson 			}
104745d9ca49SDean Nelson 
104845d9ca49SDean Nelson 			if (!(XPC_NASID_IN_ARRAY(nasid, xpc_mach_nasids))) {
104945d9ca49SDean Nelson 				dev_dbg(xpc_part, "PROM indicates Nasid %d was "
105045d9ca49SDean Nelson 					"not on Numa-Link network at reset\n",
105145d9ca49SDean Nelson 					nasid);
105245d9ca49SDean Nelson 				continue;
105345d9ca49SDean Nelson 			}
105445d9ca49SDean Nelson 
105545d9ca49SDean Nelson 			if (XPC_NASID_IN_ARRAY(nasid, discovered_nasids)) {
105645d9ca49SDean Nelson 				dev_dbg(xpc_part, "Nasid %d is part of a "
105745d9ca49SDean Nelson 					"partition which was previously "
105845d9ca49SDean Nelson 					"discovered\n", nasid);
105945d9ca49SDean Nelson 				continue;
106045d9ca49SDean Nelson 			}
106145d9ca49SDean Nelson 
106245d9ca49SDean Nelson 			/* pull over the reserved page structure */
106345d9ca49SDean Nelson 
106445d9ca49SDean Nelson 			ret = xpc_get_remote_rp(nasid, discovered_nasids,
106545d9ca49SDean Nelson 						remote_rp, &remote_rp_pa);
106665c17b80SDean Nelson 			if (ret != xpSuccess) {
106745d9ca49SDean Nelson 				dev_dbg(xpc_part, "unable to get reserved page "
106845d9ca49SDean Nelson 					"from nasid %d, reason=%d\n", nasid,
106945d9ca49SDean Nelson 					ret);
107045d9ca49SDean Nelson 
107165c17b80SDean Nelson 				if (ret == xpLocalPartid)
107245d9ca49SDean Nelson 					break;
10732c2b94f9SDean Nelson 
107445d9ca49SDean Nelson 				continue;
107545d9ca49SDean Nelson 			}
107645d9ca49SDean Nelson 
107745d9ca49SDean Nelson 			remote_vars_pa = remote_rp->vars_pa;
107845d9ca49SDean Nelson 
107945d9ca49SDean Nelson 			partid = remote_rp->partid;
108045d9ca49SDean Nelson 			part = &xpc_partitions[partid];
108145d9ca49SDean Nelson 
108245d9ca49SDean Nelson 			/* pull over the cross partition variables */
108345d9ca49SDean Nelson 
108445d9ca49SDean Nelson 			ret = xpc_get_remote_vars(remote_vars_pa, remote_vars);
108565c17b80SDean Nelson 			if (ret != xpSuccess) {
108645d9ca49SDean Nelson 				dev_dbg(xpc_part, "unable to get XPC variables "
108745d9ca49SDean Nelson 					"from nasid %d, reason=%d\n", nasid,
108845d9ca49SDean Nelson 					ret);
108945d9ca49SDean Nelson 
109045d9ca49SDean Nelson 				XPC_DEACTIVATE_PARTITION(part, ret);
109145d9ca49SDean Nelson 				continue;
109245d9ca49SDean Nelson 			}
109345d9ca49SDean Nelson 
109445d9ca49SDean Nelson 			if (part->act_state != XPC_P_INACTIVE) {
109545d9ca49SDean Nelson 				dev_dbg(xpc_part, "partition %d on nasid %d is "
109645d9ca49SDean Nelson 					"already activating\n", partid, nasid);
109745d9ca49SDean Nelson 				break;
109845d9ca49SDean Nelson 			}
109945d9ca49SDean Nelson 
110045d9ca49SDean Nelson 			/*
110145d9ca49SDean Nelson 			 * Register the remote partition's AMOs with SAL so it
110245d9ca49SDean Nelson 			 * can handle and cleanup errors within that address
110345d9ca49SDean Nelson 			 * range should the remote partition go down. We don't
110445d9ca49SDean Nelson 			 * unregister this range because it is difficult to
110545d9ca49SDean Nelson 			 * tell when outstanding writes to the remote partition
110645d9ca49SDean Nelson 			 * are finished and thus when it is thus safe to
110745d9ca49SDean Nelson 			 * unregister. This should not result in wasted space
110845d9ca49SDean Nelson 			 * in the SAL xp_addr_region table because we should
110945d9ca49SDean Nelson 			 * get the same page for remote_act_amos_pa after
111045d9ca49SDean Nelson 			 * module reloads and system reboots.
111145d9ca49SDean Nelson 			 */
11124a3ad2ddSDean Nelson 			if (sn_register_xp_addr_region
11134a3ad2ddSDean Nelson 			    (remote_vars->amos_page_pa, PAGE_SIZE, 1) < 0) {
11144a3ad2ddSDean Nelson 				dev_dbg(xpc_part,
11154a3ad2ddSDean Nelson 					"partition %d failed to "
111645d9ca49SDean Nelson 					"register xp_addr region 0x%016lx\n",
111745d9ca49SDean Nelson 					partid, remote_vars->amos_page_pa);
111845d9ca49SDean Nelson 
111965c17b80SDean Nelson 				XPC_SET_REASON(part, xpPhysAddrRegFailed,
112045d9ca49SDean Nelson 					       __LINE__);
112145d9ca49SDean Nelson 				break;
112245d9ca49SDean Nelson 			}
112345d9ca49SDean Nelson 
112445d9ca49SDean Nelson 			/*
112545d9ca49SDean Nelson 			 * The remote nasid is valid and available.
112645d9ca49SDean Nelson 			 * Send an interrupt to that nasid to notify
112745d9ca49SDean Nelson 			 * it that we are ready to begin activation.
112845d9ca49SDean Nelson 			 */
112945d9ca49SDean Nelson 			dev_dbg(xpc_part, "sending an interrupt to AMO 0x%lx, "
113045d9ca49SDean Nelson 				"nasid %d, phys_cpuid 0x%x\n",
113145d9ca49SDean Nelson 				remote_vars->amos_page_pa,
113245d9ca49SDean Nelson 				remote_vars->act_nasid,
113345d9ca49SDean Nelson 				remote_vars->act_phys_cpuid);
113445d9ca49SDean Nelson 
113545d9ca49SDean Nelson 			if (XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->
113645d9ca49SDean Nelson 							   version)) {
113745d9ca49SDean Nelson 				part->remote_amos_page_pa =
113845d9ca49SDean Nelson 				    remote_vars->amos_page_pa;
113945d9ca49SDean Nelson 				xpc_mark_partition_disengaged(part);
114045d9ca49SDean Nelson 				xpc_cancel_partition_disengage_request(part);
114145d9ca49SDean Nelson 			}
114245d9ca49SDean Nelson 			xpc_IPI_send_activate(remote_vars);
114345d9ca49SDean Nelson 		}
114445d9ca49SDean Nelson 	}
114545d9ca49SDean Nelson 
114645d9ca49SDean Nelson 	kfree(discovered_nasids);
114745d9ca49SDean Nelson 	kfree(remote_rp_base);
114845d9ca49SDean Nelson }
114945d9ca49SDean Nelson 
115045d9ca49SDean Nelson /*
115145d9ca49SDean Nelson  * Given a partid, get the nasids owned by that partition from the
115245d9ca49SDean Nelson  * remote partition's reserved page.
115345d9ca49SDean Nelson  */
115465c17b80SDean Nelson enum xp_retval
1155*64d032baSDean Nelson xpc_initiate_partid_to_nasids(short partid, void *nasid_mask)
115645d9ca49SDean Nelson {
115745d9ca49SDean Nelson 	struct xpc_partition *part;
115845d9ca49SDean Nelson 	u64 part_nasid_pa;
115945d9ca49SDean Nelson 	int bte_res;
116045d9ca49SDean Nelson 
116145d9ca49SDean Nelson 	part = &xpc_partitions[partid];
11622c2b94f9SDean Nelson 	if (part->remote_rp_pa == 0)
116365c17b80SDean Nelson 		return xpPartitionDown;
116445d9ca49SDean Nelson 
116545d9ca49SDean Nelson 	memset(nasid_mask, 0, XP_NASID_MASK_BYTES);
116645d9ca49SDean Nelson 
116745d9ca49SDean Nelson 	part_nasid_pa = (u64)XPC_RP_PART_NASIDS(part->remote_rp_pa);
116845d9ca49SDean Nelson 
116945d9ca49SDean Nelson 	bte_res = xp_bte_copy(part_nasid_pa, (u64)nasid_mask,
11704a3ad2ddSDean Nelson 			      xp_nasid_mask_bytes, (BTE_NOTIFY | BTE_WACQUIRE),
11714a3ad2ddSDean Nelson 			      NULL);
117245d9ca49SDean Nelson 
117345d9ca49SDean Nelson 	return xpc_map_bte_errors(bte_res);
117445d9ca49SDean Nelson }
1175