xref: /openbmc/linux/drivers/misc/sgi-xp/xpc_partition.c (revision 81fe7883d2c8a80a7145ad22f8cd8514d05412b9)
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 /* this partition's reserved page pointers */
3345d9ca49SDean Nelson struct xpc_rsvd_page *xpc_rsvd_page;
3404de7418SDean Nelson static unsigned long *xpc_part_nasids;
3504de7418SDean Nelson unsigned long *xpc_mach_nasids;
3645d9ca49SDean Nelson 
3704de7418SDean Nelson static int xpc_nasid_mask_nbytes;	/* #of bytes in nasid mask */
3804de7418SDean Nelson int xpc_nasid_mask_nlongs;	/* #of longs in nasid mask */
3945d9ca49SDean Nelson 
40bc63d387SDean Nelson struct xpc_partition *xpc_partitions;
4145d9ca49SDean Nelson 
4245d9ca49SDean Nelson /*
4345d9ca49SDean Nelson  * Guarantee that the kmalloc'd memory is cacheline aligned.
4445d9ca49SDean Nelson  */
4545d9ca49SDean Nelson void *
4645d9ca49SDean Nelson xpc_kmalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
4745d9ca49SDean Nelson {
4845d9ca49SDean Nelson 	/* see if kmalloc will give us cachline aligned memory by default */
4945d9ca49SDean Nelson 	*base = kmalloc(size, flags);
502c2b94f9SDean Nelson 	if (*base == NULL)
5145d9ca49SDean Nelson 		return NULL;
522c2b94f9SDean Nelson 
532c2b94f9SDean Nelson 	if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
5445d9ca49SDean Nelson 		return *base;
552c2b94f9SDean Nelson 
5645d9ca49SDean Nelson 	kfree(*base);
5745d9ca49SDean Nelson 
5845d9ca49SDean Nelson 	/* nope, we'll have to do it ourselves */
5945d9ca49SDean Nelson 	*base = kmalloc(size + L1_CACHE_BYTES, flags);
602c2b94f9SDean Nelson 	if (*base == NULL)
6145d9ca49SDean Nelson 		return NULL;
622c2b94f9SDean Nelson 
6345d9ca49SDean Nelson 	return (void *)L1_CACHE_ALIGN((u64)*base);
6445d9ca49SDean Nelson }
6545d9ca49SDean Nelson 
6645d9ca49SDean Nelson /*
6745d9ca49SDean Nelson  * Given a nasid, get the physical address of the  partition's reserved page
6845d9ca49SDean Nelson  * for that nasid. This function returns 0 on any error.
6945d9ca49SDean Nelson  */
7045d9ca49SDean Nelson static u64
7145d9ca49SDean Nelson xpc_get_rsvd_page_pa(int nasid)
7245d9ca49SDean Nelson {
73908787dbSDean Nelson 	enum xp_retval ret;
7445d9ca49SDean Nelson 	s64 status;
7545d9ca49SDean Nelson 	u64 cookie = 0;
7645d9ca49SDean Nelson 	u64 rp_pa = nasid;	/* seed with nasid */
7745d9ca49SDean Nelson 	u64 len = 0;
7845d9ca49SDean Nelson 	u64 buf = buf;
7945d9ca49SDean Nelson 	u64 buf_len = 0;
8045d9ca49SDean Nelson 	void *buf_base = NULL;
8145d9ca49SDean Nelson 
8245d9ca49SDean Nelson 	while (1) {
8345d9ca49SDean Nelson 
8445d9ca49SDean Nelson 		status = sn_partition_reserved_page_pa(buf, &cookie, &rp_pa,
8545d9ca49SDean Nelson 						       &len);
8645d9ca49SDean Nelson 
8745d9ca49SDean Nelson 		dev_dbg(xpc_part, "SAL returned with status=%li, cookie="
8845d9ca49SDean Nelson 			"0x%016lx, address=0x%016lx, len=0x%016lx\n",
8945d9ca49SDean Nelson 			status, cookie, rp_pa, len);
9045d9ca49SDean Nelson 
912c2b94f9SDean Nelson 		if (status != SALRET_MORE_PASSES)
9245d9ca49SDean Nelson 			break;
9345d9ca49SDean Nelson 
94ea57f80cSDean Nelson 		/* !!! L1_CACHE_ALIGN() is only a sn2-bte_copy requirement */
9545d9ca49SDean Nelson 		if (L1_CACHE_ALIGN(len) > buf_len) {
9645d9ca49SDean Nelson 			kfree(buf_base);
9745d9ca49SDean Nelson 			buf_len = L1_CACHE_ALIGN(len);
9845d9ca49SDean Nelson 			buf = (u64)xpc_kmalloc_cacheline_aligned(buf_len,
994a3ad2ddSDean Nelson 								 GFP_KERNEL,
1004a3ad2ddSDean Nelson 								 &buf_base);
10145d9ca49SDean Nelson 			if (buf_base == NULL) {
10245d9ca49SDean Nelson 				dev_err(xpc_part, "unable to kmalloc "
10345d9ca49SDean Nelson 					"len=0x%016lx\n", buf_len);
10445d9ca49SDean Nelson 				status = SALRET_ERROR;
10545d9ca49SDean Nelson 				break;
10645d9ca49SDean Nelson 			}
10745d9ca49SDean Nelson 		}
10845d9ca49SDean Nelson 
109908787dbSDean Nelson 		ret = xp_remote_memcpy((void *)buf, (void *)rp_pa, buf_len);
110908787dbSDean Nelson 		if (ret != xpSuccess) {
111908787dbSDean Nelson 			dev_dbg(xpc_part, "xp_remote_memcpy failed %d\n", ret);
11245d9ca49SDean Nelson 			status = SALRET_ERROR;
11345d9ca49SDean Nelson 			break;
11445d9ca49SDean Nelson 		}
11545d9ca49SDean Nelson 	}
11645d9ca49SDean Nelson 
11745d9ca49SDean Nelson 	kfree(buf_base);
11845d9ca49SDean Nelson 
1192c2b94f9SDean Nelson 	if (status != SALRET_OK)
12045d9ca49SDean Nelson 		rp_pa = 0;
1212c2b94f9SDean Nelson 
12245d9ca49SDean Nelson 	dev_dbg(xpc_part, "reserved page at phys address 0x%016lx\n", rp_pa);
12345d9ca49SDean Nelson 	return rp_pa;
12445d9ca49SDean Nelson }
12545d9ca49SDean Nelson 
12645d9ca49SDean Nelson /*
12745d9ca49SDean Nelson  * Fill the partition reserved page with the information needed by
12845d9ca49SDean Nelson  * other partitions to discover we are alive and establish initial
12945d9ca49SDean Nelson  * communications.
13045d9ca49SDean Nelson  */
13145d9ca49SDean Nelson struct xpc_rsvd_page *
13294bd2708SDean Nelson xpc_setup_rsvd_page(void)
13345d9ca49SDean Nelson {
13445d9ca49SDean Nelson 	struct xpc_rsvd_page *rp;
13594bd2708SDean Nelson 	u64 rp_pa;
136*81fe7883SDean Nelson 	unsigned long new_ts_jiffies;
13745d9ca49SDean Nelson 
13845d9ca49SDean Nelson 	/* get the local reserved page's address */
13945d9ca49SDean Nelson 
14045d9ca49SDean Nelson 	preempt_disable();
14145d9ca49SDean Nelson 	rp_pa = xpc_get_rsvd_page_pa(cpuid_to_nasid(smp_processor_id()));
14245d9ca49SDean Nelson 	preempt_enable();
14345d9ca49SDean Nelson 	if (rp_pa == 0) {
14445d9ca49SDean Nelson 		dev_err(xpc_part, "SAL failed to locate the reserved page\n");
14545d9ca49SDean Nelson 		return NULL;
14645d9ca49SDean Nelson 	}
14745d9ca49SDean Nelson 	rp = (struct xpc_rsvd_page *)__va(rp_pa);
14845d9ca49SDean Nelson 
14994bd2708SDean Nelson 	if (rp->SAL_version < 3) {
15094bd2708SDean Nelson 		/* SAL_versions < 3 had a SAL_partid defined as a u8 */
15194bd2708SDean Nelson 		rp->SAL_partid &= 0xff;
15294bd2708SDean Nelson 	}
15394bd2708SDean Nelson 	BUG_ON(rp->SAL_partid != sn_partition_id);
15494bd2708SDean Nelson 
15594bd2708SDean Nelson 	if (rp->SAL_partid < 0 || rp->SAL_partid >= xp_max_npartitions) {
15694bd2708SDean Nelson 		dev_err(xpc_part, "the reserved page's partid of %d is outside "
15794bd2708SDean Nelson 			"supported range (< 0 || >= %d)\n", rp->SAL_partid,
15894bd2708SDean Nelson 			xp_max_npartitions);
15945d9ca49SDean Nelson 		return NULL;
16045d9ca49SDean Nelson 	}
16145d9ca49SDean Nelson 
16245d9ca49SDean Nelson 	rp->version = XPC_RP_VERSION;
16394bd2708SDean Nelson 	rp->max_npartitions = xp_max_npartitions;
16445d9ca49SDean Nelson 
16545d9ca49SDean Nelson 	/* establish the actual sizes of the nasid masks */
16645d9ca49SDean Nelson 	if (rp->SAL_version == 1) {
16745d9ca49SDean Nelson 		/* SAL_version 1 didn't set the nasids_size field */
16894bd2708SDean Nelson 		rp->SAL_nasids_size = 128;
16945d9ca49SDean Nelson 	}
17004de7418SDean Nelson 	xpc_nasid_mask_nbytes = rp->SAL_nasids_size;
17104de7418SDean Nelson 	xpc_nasid_mask_nlongs = BITS_TO_LONGS(rp->SAL_nasids_size *
17204de7418SDean Nelson 					      BITS_PER_BYTE);
17345d9ca49SDean Nelson 
17445d9ca49SDean Nelson 	/* setup the pointers to the various items in the reserved page */
17545d9ca49SDean Nelson 	xpc_part_nasids = XPC_RP_PART_NASIDS(rp);
17645d9ca49SDean Nelson 	xpc_mach_nasids = XPC_RP_MACH_NASIDS(rp);
17745d9ca49SDean Nelson 
17894bd2708SDean Nelson 	if (xpc_rsvd_page_init(rp) != xpSuccess)
17945d9ca49SDean Nelson 		return NULL;
18045d9ca49SDean Nelson 
18145d9ca49SDean Nelson 	/*
18294bd2708SDean Nelson 	 * Set timestamp of when reserved page was setup by XPC.
18345d9ca49SDean Nelson 	 * This signifies to the remote partition that our reserved
18445d9ca49SDean Nelson 	 * page is initialized.
18545d9ca49SDean Nelson 	 */
186*81fe7883SDean Nelson 	new_ts_jiffies = jiffies;
187*81fe7883SDean Nelson 	if (new_ts_jiffies == 0 || new_ts_jiffies == rp->ts_jiffies)
188*81fe7883SDean Nelson 		new_ts_jiffies++;
189*81fe7883SDean Nelson 	rp->ts_jiffies = new_ts_jiffies;
19045d9ca49SDean Nelson 
19145d9ca49SDean Nelson 	return rp;
19245d9ca49SDean Nelson }
19345d9ca49SDean Nelson 
19445d9ca49SDean Nelson /*
19545d9ca49SDean Nelson  * Get a copy of a portion of the remote partition's rsvd page.
19645d9ca49SDean Nelson  *
19745d9ca49SDean Nelson  * remote_rp points to a buffer that is cacheline aligned for BTE copies and
19845d9ca49SDean Nelson  * is large enough to contain a copy of their reserved page header and
19945d9ca49SDean Nelson  * part_nasids mask.
20045d9ca49SDean Nelson  */
20133ba3c77SDean Nelson enum xp_retval
20204de7418SDean Nelson xpc_get_remote_rp(int nasid, unsigned long *discovered_nasids,
20345d9ca49SDean Nelson 		  struct xpc_rsvd_page *remote_rp, u64 *remote_rp_pa)
20445d9ca49SDean Nelson {
20504de7418SDean Nelson 	int l;
206908787dbSDean Nelson 	enum xp_retval ret;
20745d9ca49SDean Nelson 
20845d9ca49SDean Nelson 	/* get the reserved page's physical address */
20945d9ca49SDean Nelson 
21045d9ca49SDean Nelson 	*remote_rp_pa = xpc_get_rsvd_page_pa(nasid);
2112c2b94f9SDean Nelson 	if (*remote_rp_pa == 0)
21265c17b80SDean Nelson 		return xpNoRsvdPageAddr;
21345d9ca49SDean Nelson 
21445d9ca49SDean Nelson 	/* pull over the reserved page header and part_nasids mask */
215908787dbSDean Nelson 	ret = xp_remote_memcpy(remote_rp, (void *)*remote_rp_pa,
21604de7418SDean Nelson 			       XPC_RP_HEADER_SIZE + xpc_nasid_mask_nbytes);
217908787dbSDean Nelson 	if (ret != xpSuccess)
218908787dbSDean Nelson 		return ret;
21945d9ca49SDean Nelson 
22045d9ca49SDean Nelson 	if (discovered_nasids != NULL) {
22104de7418SDean Nelson 		unsigned long *remote_part_nasids =
22204de7418SDean Nelson 		    XPC_RP_PART_NASIDS(remote_rp);
22345d9ca49SDean Nelson 
22404de7418SDean Nelson 		for (l = 0; l < xpc_nasid_mask_nlongs; l++)
22504de7418SDean Nelson 			discovered_nasids[l] |= remote_part_nasids[l];
22645d9ca49SDean Nelson 	}
22745d9ca49SDean Nelson 
228*81fe7883SDean Nelson 	/* zero timestamp indicates the reserved page has not been setup */
229*81fe7883SDean Nelson 	if (remote_rp->ts_jiffies == 0)
23094bd2708SDean Nelson 		return xpRsvdPageNotSet;
23194bd2708SDean Nelson 
23245d9ca49SDean Nelson 	if (XPC_VERSION_MAJOR(remote_rp->version) !=
23345d9ca49SDean Nelson 	    XPC_VERSION_MAJOR(XPC_RP_VERSION)) {
23465c17b80SDean Nelson 		return xpBadVersion;
23545d9ca49SDean Nelson 	}
23645d9ca49SDean Nelson 
237a47d5dacSDean Nelson 	/* check that both remote and local partids are valid for each side */
238aaa3cd69SDean Nelson 	if (remote_rp->SAL_partid < 0 ||
239aaa3cd69SDean Nelson 	    remote_rp->SAL_partid >= xp_max_npartitions ||
240aaa3cd69SDean Nelson 	    remote_rp->max_npartitions <= sn_partition_id) {
24194bd2708SDean Nelson 		return xpInvalidPartid;
242aaa3cd69SDean Nelson 	}
243aaa3cd69SDean Nelson 
244aaa3cd69SDean Nelson 	if (remote_rp->SAL_partid == sn_partition_id)
245aaa3cd69SDean Nelson 		return xpLocalPartid;
24694bd2708SDean Nelson 
24765c17b80SDean Nelson 	return xpSuccess;
24845d9ca49SDean Nelson }
24945d9ca49SDean Nelson 
25045d9ca49SDean Nelson /*
251a47d5dacSDean Nelson  * See if the other side has responded to a partition deactivate request
252a47d5dacSDean Nelson  * from us. Though we requested the remote partition to deactivate with regard
253a47d5dacSDean Nelson  * to us, we really only need to wait for the other side to disengage from us.
25445d9ca49SDean Nelson  */
25545d9ca49SDean Nelson int
25645d9ca49SDean Nelson xpc_partition_disengaged(struct xpc_partition *part)
25745d9ca49SDean Nelson {
25864d032baSDean Nelson 	short partid = XPC_PARTID(part);
25945d9ca49SDean Nelson 	int disengaged;
26045d9ca49SDean Nelson 
261a47d5dacSDean Nelson 	disengaged = !xpc_partition_engaged(partid);
262a47d5dacSDean Nelson 	if (part->disengage_timeout) {
26345d9ca49SDean Nelson 		if (!disengaged) {
264a47d5dacSDean Nelson 			if (time_is_after_jiffies(part->disengage_timeout)) {
26545d9ca49SDean Nelson 				/* timelimit hasn't been reached yet */
26645d9ca49SDean Nelson 				return 0;
26745d9ca49SDean Nelson 			}
26845d9ca49SDean Nelson 
26945d9ca49SDean Nelson 			/*
270a47d5dacSDean Nelson 			 * Other side hasn't responded to our deactivate
27145d9ca49SDean Nelson 			 * request in a timely fashion, so assume it's dead.
27245d9ca49SDean Nelson 			 */
27345d9ca49SDean Nelson 
274a47d5dacSDean Nelson 			dev_info(xpc_part, "deactivate request to remote "
275a47d5dacSDean Nelson 				 "partition %d timed out\n", partid);
276a47d5dacSDean Nelson 			xpc_disengage_timedout = 1;
277a47d5dacSDean Nelson 			xpc_assume_partition_disengaged(partid);
27845d9ca49SDean Nelson 			disengaged = 1;
27945d9ca49SDean Nelson 		}
280a47d5dacSDean Nelson 		part->disengage_timeout = 0;
28145d9ca49SDean Nelson 
28245d9ca49SDean Nelson 		/* cancel the timer function, provided it's not us */
283a47d5dacSDean Nelson 		if (!in_interrupt())
284a47d5dacSDean Nelson 			del_singleshot_timer_sync(&part->disengage_timer);
28545d9ca49SDean Nelson 
28645d9ca49SDean Nelson 		DBUG_ON(part->act_state != XPC_P_DEACTIVATING &&
28745d9ca49SDean Nelson 			part->act_state != XPC_P_INACTIVE);
2882c2b94f9SDean Nelson 		if (part->act_state != XPC_P_INACTIVE)
28945d9ca49SDean Nelson 			xpc_wakeup_channel_mgr(part);
29045d9ca49SDean Nelson 
291a47d5dacSDean Nelson 		xpc_cancel_partition_deactivation_request(part);
29245d9ca49SDean Nelson 	}
29345d9ca49SDean Nelson 	return disengaged;
29445d9ca49SDean Nelson }
29545d9ca49SDean Nelson 
29645d9ca49SDean Nelson /*
29745d9ca49SDean Nelson  * Mark specified partition as active.
29845d9ca49SDean Nelson  */
29965c17b80SDean Nelson enum xp_retval
30045d9ca49SDean Nelson xpc_mark_partition_active(struct xpc_partition *part)
30145d9ca49SDean Nelson {
30245d9ca49SDean Nelson 	unsigned long irq_flags;
30365c17b80SDean Nelson 	enum xp_retval ret;
30445d9ca49SDean Nelson 
30545d9ca49SDean Nelson 	dev_dbg(xpc_part, "setting partition %d to ACTIVE\n", XPC_PARTID(part));
30645d9ca49SDean Nelson 
30745d9ca49SDean Nelson 	spin_lock_irqsave(&part->act_lock, irq_flags);
30845d9ca49SDean Nelson 	if (part->act_state == XPC_P_ACTIVATING) {
30945d9ca49SDean Nelson 		part->act_state = XPC_P_ACTIVE;
31065c17b80SDean Nelson 		ret = xpSuccess;
31145d9ca49SDean Nelson 	} else {
31265c17b80SDean Nelson 		DBUG_ON(part->reason == xpSuccess);
31345d9ca49SDean Nelson 		ret = part->reason;
31445d9ca49SDean Nelson 	}
31545d9ca49SDean Nelson 	spin_unlock_irqrestore(&part->act_lock, irq_flags);
31645d9ca49SDean Nelson 
31745d9ca49SDean Nelson 	return ret;
31845d9ca49SDean Nelson }
31945d9ca49SDean Nelson 
32045d9ca49SDean Nelson /*
321a47d5dacSDean Nelson  * Start the process of deactivating the specified partition.
32245d9ca49SDean Nelson  */
32345d9ca49SDean Nelson void
32445d9ca49SDean Nelson xpc_deactivate_partition(const int line, struct xpc_partition *part,
32565c17b80SDean Nelson 			 enum xp_retval reason)
32645d9ca49SDean Nelson {
32745d9ca49SDean Nelson 	unsigned long irq_flags;
32845d9ca49SDean Nelson 
32945d9ca49SDean Nelson 	spin_lock_irqsave(&part->act_lock, irq_flags);
33045d9ca49SDean Nelson 
33145d9ca49SDean Nelson 	if (part->act_state == XPC_P_INACTIVE) {
33245d9ca49SDean Nelson 		XPC_SET_REASON(part, reason, line);
33345d9ca49SDean Nelson 		spin_unlock_irqrestore(&part->act_lock, irq_flags);
33465c17b80SDean Nelson 		if (reason == xpReactivating) {
33545d9ca49SDean Nelson 			/* we interrupt ourselves to reactivate partition */
336a47d5dacSDean Nelson 			xpc_request_partition_reactivation(part);
33745d9ca49SDean Nelson 		}
33845d9ca49SDean Nelson 		return;
33945d9ca49SDean Nelson 	}
34045d9ca49SDean Nelson 	if (part->act_state == XPC_P_DEACTIVATING) {
34165c17b80SDean Nelson 		if ((part->reason == xpUnloading && reason != xpUnloading) ||
34265c17b80SDean Nelson 		    reason == xpReactivating) {
34345d9ca49SDean Nelson 			XPC_SET_REASON(part, reason, line);
34445d9ca49SDean Nelson 		}
34545d9ca49SDean Nelson 		spin_unlock_irqrestore(&part->act_lock, irq_flags);
34645d9ca49SDean Nelson 		return;
34745d9ca49SDean Nelson 	}
34845d9ca49SDean Nelson 
34945d9ca49SDean Nelson 	part->act_state = XPC_P_DEACTIVATING;
35045d9ca49SDean Nelson 	XPC_SET_REASON(part, reason, line);
35145d9ca49SDean Nelson 
35245d9ca49SDean Nelson 	spin_unlock_irqrestore(&part->act_lock, irq_flags);
35345d9ca49SDean Nelson 
354a47d5dacSDean Nelson 	/* ask remote partition to deactivate with regard to us */
355a47d5dacSDean Nelson 	xpc_request_partition_deactivation(part);
35645d9ca49SDean Nelson 
357a47d5dacSDean Nelson 	/* set a timelimit on the disengage phase of the deactivation request */
358a47d5dacSDean Nelson 	part->disengage_timeout = jiffies + (xpc_disengage_timelimit * HZ);
359a47d5dacSDean Nelson 	part->disengage_timer.expires = part->disengage_timeout;
360a47d5dacSDean Nelson 	add_timer(&part->disengage_timer);
36145d9ca49SDean Nelson 
36245d9ca49SDean Nelson 	dev_dbg(xpc_part, "bringing partition %d down, reason = %d\n",
36345d9ca49SDean Nelson 		XPC_PARTID(part), reason);
36445d9ca49SDean Nelson 
36545d9ca49SDean Nelson 	xpc_partition_going_down(part, reason);
36645d9ca49SDean Nelson }
36745d9ca49SDean Nelson 
36845d9ca49SDean Nelson /*
36945d9ca49SDean Nelson  * Mark specified partition as inactive.
37045d9ca49SDean Nelson  */
37145d9ca49SDean Nelson void
37245d9ca49SDean Nelson xpc_mark_partition_inactive(struct xpc_partition *part)
37345d9ca49SDean Nelson {
37445d9ca49SDean Nelson 	unsigned long irq_flags;
37545d9ca49SDean Nelson 
37645d9ca49SDean Nelson 	dev_dbg(xpc_part, "setting partition %d to INACTIVE\n",
37745d9ca49SDean Nelson 		XPC_PARTID(part));
37845d9ca49SDean Nelson 
37945d9ca49SDean Nelson 	spin_lock_irqsave(&part->act_lock, irq_flags);
38045d9ca49SDean Nelson 	part->act_state = XPC_P_INACTIVE;
38145d9ca49SDean Nelson 	spin_unlock_irqrestore(&part->act_lock, irq_flags);
38245d9ca49SDean Nelson 	part->remote_rp_pa = 0;
38345d9ca49SDean Nelson }
38445d9ca49SDean Nelson 
38545d9ca49SDean Nelson /*
38645d9ca49SDean Nelson  * SAL has provided a partition and machine mask.  The partition mask
38745d9ca49SDean Nelson  * contains a bit for each even nasid in our partition.  The machine
38845d9ca49SDean Nelson  * mask contains a bit for each even nasid in the entire machine.
38945d9ca49SDean Nelson  *
39045d9ca49SDean Nelson  * Using those two bit arrays, we can determine which nasids are
39145d9ca49SDean Nelson  * known in the machine.  Each should also have a reserved page
39245d9ca49SDean Nelson  * initialized if they are available for partitioning.
39345d9ca49SDean Nelson  */
39445d9ca49SDean Nelson void
39545d9ca49SDean Nelson xpc_discovery(void)
39645d9ca49SDean Nelson {
39745d9ca49SDean Nelson 	void *remote_rp_base;
39845d9ca49SDean Nelson 	struct xpc_rsvd_page *remote_rp;
39945d9ca49SDean Nelson 	u64 remote_rp_pa;
40045d9ca49SDean Nelson 	int region;
40145d9ca49SDean Nelson 	int region_size;
40245d9ca49SDean Nelson 	int max_regions;
40345d9ca49SDean Nelson 	int nasid;
40445d9ca49SDean Nelson 	struct xpc_rsvd_page *rp;
40504de7418SDean Nelson 	unsigned long *discovered_nasids;
40665c17b80SDean Nelson 	enum xp_retval ret;
40745d9ca49SDean Nelson 
40845d9ca49SDean Nelson 	remote_rp = xpc_kmalloc_cacheline_aligned(XPC_RP_HEADER_SIZE +
40904de7418SDean Nelson 						  xpc_nasid_mask_nbytes,
41045d9ca49SDean Nelson 						  GFP_KERNEL, &remote_rp_base);
4112c2b94f9SDean Nelson 	if (remote_rp == NULL)
41245d9ca49SDean Nelson 		return;
4132c2b94f9SDean Nelson 
41404de7418SDean Nelson 	discovered_nasids = kzalloc(sizeof(long) * xpc_nasid_mask_nlongs,
41545d9ca49SDean Nelson 				    GFP_KERNEL);
41645d9ca49SDean Nelson 	if (discovered_nasids == NULL) {
41745d9ca49SDean Nelson 		kfree(remote_rp_base);
41845d9ca49SDean Nelson 		return;
41945d9ca49SDean Nelson 	}
42045d9ca49SDean Nelson 
42145d9ca49SDean Nelson 	rp = (struct xpc_rsvd_page *)xpc_rsvd_page;
42245d9ca49SDean Nelson 
42345d9ca49SDean Nelson 	/*
42445d9ca49SDean Nelson 	 * The term 'region' in this context refers to the minimum number of
42545d9ca49SDean Nelson 	 * nodes that can comprise an access protection grouping. The access
42645d9ca49SDean Nelson 	 * protection is in regards to memory, IOI and IPI.
42745d9ca49SDean Nelson 	 */
42845d9ca49SDean Nelson 	max_regions = 64;
42945d9ca49SDean Nelson 	region_size = sn_region_size;
43045d9ca49SDean Nelson 
43145d9ca49SDean Nelson 	switch (region_size) {
43245d9ca49SDean Nelson 	case 128:
43345d9ca49SDean Nelson 		max_regions *= 2;
43445d9ca49SDean Nelson 	case 64:
43545d9ca49SDean Nelson 		max_regions *= 2;
43645d9ca49SDean Nelson 	case 32:
43745d9ca49SDean Nelson 		max_regions *= 2;
43845d9ca49SDean Nelson 		region_size = 16;
43945d9ca49SDean Nelson 		DBUG_ON(!is_shub2());
44045d9ca49SDean Nelson 	}
44145d9ca49SDean Nelson 
44245d9ca49SDean Nelson 	for (region = 0; region < max_regions; region++) {
44345d9ca49SDean Nelson 
4442c2b94f9SDean Nelson 		if (xpc_exiting)
44545d9ca49SDean Nelson 			break;
44645d9ca49SDean Nelson 
44745d9ca49SDean Nelson 		dev_dbg(xpc_part, "searching region %d\n", region);
44845d9ca49SDean Nelson 
44945d9ca49SDean Nelson 		for (nasid = (region * region_size * 2);
4504a3ad2ddSDean Nelson 		     nasid < ((region + 1) * region_size * 2); nasid += 2) {
45145d9ca49SDean Nelson 
4522c2b94f9SDean Nelson 			if (xpc_exiting)
45345d9ca49SDean Nelson 				break;
45445d9ca49SDean Nelson 
45545d9ca49SDean Nelson 			dev_dbg(xpc_part, "checking nasid %d\n", nasid);
45645d9ca49SDean Nelson 
45704de7418SDean Nelson 			if (test_bit(nasid / 2, xpc_part_nasids)) {
45845d9ca49SDean Nelson 				dev_dbg(xpc_part, "PROM indicates Nasid %d is "
45945d9ca49SDean Nelson 					"part of the local partition; skipping "
46045d9ca49SDean Nelson 					"region\n", nasid);
46145d9ca49SDean Nelson 				break;
46245d9ca49SDean Nelson 			}
46345d9ca49SDean Nelson 
46404de7418SDean Nelson 			if (!(test_bit(nasid / 2, xpc_mach_nasids))) {
46545d9ca49SDean Nelson 				dev_dbg(xpc_part, "PROM indicates Nasid %d was "
46645d9ca49SDean Nelson 					"not on Numa-Link network at reset\n",
46745d9ca49SDean Nelson 					nasid);
46845d9ca49SDean Nelson 				continue;
46945d9ca49SDean Nelson 			}
47045d9ca49SDean Nelson 
47104de7418SDean Nelson 			if (test_bit(nasid / 2, discovered_nasids)) {
47245d9ca49SDean Nelson 				dev_dbg(xpc_part, "Nasid %d is part of a "
47345d9ca49SDean Nelson 					"partition which was previously "
47445d9ca49SDean Nelson 					"discovered\n", nasid);
47545d9ca49SDean Nelson 				continue;
47645d9ca49SDean Nelson 			}
47745d9ca49SDean Nelson 
47833ba3c77SDean Nelson 			/* pull over the rsvd page header & part_nasids mask */
47945d9ca49SDean Nelson 
48045d9ca49SDean Nelson 			ret = xpc_get_remote_rp(nasid, discovered_nasids,
48145d9ca49SDean Nelson 						remote_rp, &remote_rp_pa);
48265c17b80SDean Nelson 			if (ret != xpSuccess) {
48345d9ca49SDean Nelson 				dev_dbg(xpc_part, "unable to get reserved page "
48445d9ca49SDean Nelson 					"from nasid %d, reason=%d\n", nasid,
48545d9ca49SDean Nelson 					ret);
48645d9ca49SDean Nelson 
48765c17b80SDean Nelson 				if (ret == xpLocalPartid)
48845d9ca49SDean Nelson 					break;
4892c2b94f9SDean Nelson 
49045d9ca49SDean Nelson 				continue;
49145d9ca49SDean Nelson 			}
49245d9ca49SDean Nelson 
493a47d5dacSDean Nelson 			xpc_request_partition_activation(remote_rp,
49433ba3c77SDean Nelson 							 remote_rp_pa, nasid);
49545d9ca49SDean Nelson 		}
49645d9ca49SDean Nelson 	}
49745d9ca49SDean Nelson 
49845d9ca49SDean Nelson 	kfree(discovered_nasids);
49945d9ca49SDean Nelson 	kfree(remote_rp_base);
50045d9ca49SDean Nelson }
50145d9ca49SDean Nelson 
50245d9ca49SDean Nelson /*
50345d9ca49SDean Nelson  * Given a partid, get the nasids owned by that partition from the
50445d9ca49SDean Nelson  * remote partition's reserved page.
50545d9ca49SDean Nelson  */
50665c17b80SDean Nelson enum xp_retval
50764d032baSDean Nelson xpc_initiate_partid_to_nasids(short partid, void *nasid_mask)
50845d9ca49SDean Nelson {
50945d9ca49SDean Nelson 	struct xpc_partition *part;
51045d9ca49SDean Nelson 	u64 part_nasid_pa;
51145d9ca49SDean Nelson 
51245d9ca49SDean Nelson 	part = &xpc_partitions[partid];
5132c2b94f9SDean Nelson 	if (part->remote_rp_pa == 0)
51465c17b80SDean Nelson 		return xpPartitionDown;
51545d9ca49SDean Nelson 
51604de7418SDean Nelson 	memset(nasid_mask, 0, xpc_nasid_mask_nbytes);
51745d9ca49SDean Nelson 
51845d9ca49SDean Nelson 	part_nasid_pa = (u64)XPC_RP_PART_NASIDS(part->remote_rp_pa);
51945d9ca49SDean Nelson 
520908787dbSDean Nelson 	return xp_remote_memcpy(nasid_mask, (void *)part_nasid_pa,
52104de7418SDean Nelson 				xpc_nasid_mask_nbytes);
52245d9ca49SDean Nelson }
523