xref: /openbmc/linux/drivers/misc/sgi-xp/xpc_partition.c (revision a47d5dac9d8481766382f8cf1483dd581df38b99)
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;
3445d9ca49SDean Nelson static u64 *xpc_part_nasids;
3533ba3c77SDean Nelson u64 *xpc_mach_nasids;
3645d9ca49SDean Nelson 
3794bd2708SDean Nelson /* >>> next two variables should be 'xpc_' if they remain here */
3894bd2708SDean Nelson static int xp_sizeof_nasid_mask;	/* actual size in bytes of nasid mask */
3994bd2708SDean Nelson int xp_nasid_mask_words;	/* actual size in words of nasid mask */
4045d9ca49SDean Nelson 
41bc63d387SDean Nelson struct xpc_partition *xpc_partitions;
4245d9ca49SDean Nelson 
4345d9ca49SDean Nelson /*
4445d9ca49SDean Nelson  * Generic buffer used to store a local copy of portions of a remote
4545d9ca49SDean Nelson  * partition's reserved page (either its header and part_nasids mask,
4645d9ca49SDean Nelson  * or its vars).
4745d9ca49SDean Nelson  */
4845d9ca49SDean Nelson char *xpc_remote_copy_buffer;
4945d9ca49SDean Nelson void *xpc_remote_copy_buffer_base;
5045d9ca49SDean Nelson 
5145d9ca49SDean Nelson /*
5245d9ca49SDean Nelson  * Guarantee that the kmalloc'd memory is cacheline aligned.
5345d9ca49SDean Nelson  */
5445d9ca49SDean Nelson void *
5545d9ca49SDean Nelson xpc_kmalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
5645d9ca49SDean Nelson {
5745d9ca49SDean Nelson 	/* see if kmalloc will give us cachline aligned memory by default */
5845d9ca49SDean Nelson 	*base = kmalloc(size, flags);
592c2b94f9SDean Nelson 	if (*base == NULL)
6045d9ca49SDean Nelson 		return NULL;
612c2b94f9SDean Nelson 
622c2b94f9SDean Nelson 	if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
6345d9ca49SDean Nelson 		return *base;
642c2b94f9SDean Nelson 
6545d9ca49SDean Nelson 	kfree(*base);
6645d9ca49SDean Nelson 
6745d9ca49SDean Nelson 	/* nope, we'll have to do it ourselves */
6845d9ca49SDean Nelson 	*base = kmalloc(size + L1_CACHE_BYTES, flags);
692c2b94f9SDean Nelson 	if (*base == NULL)
7045d9ca49SDean Nelson 		return NULL;
712c2b94f9SDean Nelson 
7245d9ca49SDean Nelson 	return (void *)L1_CACHE_ALIGN((u64)*base);
7345d9ca49SDean Nelson }
7445d9ca49SDean Nelson 
7545d9ca49SDean Nelson /*
7645d9ca49SDean Nelson  * Given a nasid, get the physical address of the  partition's reserved page
7745d9ca49SDean Nelson  * for that nasid. This function returns 0 on any error.
7845d9ca49SDean Nelson  */
7945d9ca49SDean Nelson static u64
8045d9ca49SDean Nelson xpc_get_rsvd_page_pa(int nasid)
8145d9ca49SDean Nelson {
82908787dbSDean Nelson 	enum xp_retval ret;
8345d9ca49SDean Nelson 	s64 status;
8445d9ca49SDean Nelson 	u64 cookie = 0;
8545d9ca49SDean Nelson 	u64 rp_pa = nasid;	/* seed with nasid */
8645d9ca49SDean Nelson 	u64 len = 0;
8745d9ca49SDean Nelson 	u64 buf = buf;
8845d9ca49SDean Nelson 	u64 buf_len = 0;
8945d9ca49SDean Nelson 	void *buf_base = NULL;
9045d9ca49SDean Nelson 
9145d9ca49SDean Nelson 	while (1) {
9245d9ca49SDean Nelson 
9345d9ca49SDean Nelson 		status = sn_partition_reserved_page_pa(buf, &cookie, &rp_pa,
9445d9ca49SDean Nelson 						       &len);
9545d9ca49SDean Nelson 
9645d9ca49SDean Nelson 		dev_dbg(xpc_part, "SAL returned with status=%li, cookie="
9745d9ca49SDean Nelson 			"0x%016lx, address=0x%016lx, len=0x%016lx\n",
9845d9ca49SDean Nelson 			status, cookie, rp_pa, len);
9945d9ca49SDean Nelson 
1002c2b94f9SDean Nelson 		if (status != SALRET_MORE_PASSES)
10145d9ca49SDean Nelson 			break;
10245d9ca49SDean Nelson 
103908787dbSDean Nelson 		/* >>> L1_CACHE_ALIGN() is only a sn2-bte_copy requirement */
10445d9ca49SDean Nelson 		if (L1_CACHE_ALIGN(len) > buf_len) {
10545d9ca49SDean Nelson 			kfree(buf_base);
10645d9ca49SDean Nelson 			buf_len = L1_CACHE_ALIGN(len);
10745d9ca49SDean Nelson 			buf = (u64)xpc_kmalloc_cacheline_aligned(buf_len,
1084a3ad2ddSDean Nelson 								 GFP_KERNEL,
1094a3ad2ddSDean Nelson 								 &buf_base);
11045d9ca49SDean Nelson 			if (buf_base == NULL) {
11145d9ca49SDean Nelson 				dev_err(xpc_part, "unable to kmalloc "
11245d9ca49SDean Nelson 					"len=0x%016lx\n", buf_len);
11345d9ca49SDean Nelson 				status = SALRET_ERROR;
11445d9ca49SDean Nelson 				break;
11545d9ca49SDean Nelson 			}
11645d9ca49SDean Nelson 		}
11745d9ca49SDean Nelson 
118908787dbSDean Nelson 		ret = xp_remote_memcpy((void *)buf, (void *)rp_pa, buf_len);
119908787dbSDean Nelson 		if (ret != xpSuccess) {
120908787dbSDean Nelson 			dev_dbg(xpc_part, "xp_remote_memcpy failed %d\n", ret);
12145d9ca49SDean Nelson 			status = SALRET_ERROR;
12245d9ca49SDean Nelson 			break;
12345d9ca49SDean Nelson 		}
12445d9ca49SDean Nelson 	}
12545d9ca49SDean Nelson 
12645d9ca49SDean Nelson 	kfree(buf_base);
12745d9ca49SDean Nelson 
1282c2b94f9SDean Nelson 	if (status != SALRET_OK)
12945d9ca49SDean Nelson 		rp_pa = 0;
1302c2b94f9SDean Nelson 
13145d9ca49SDean Nelson 	dev_dbg(xpc_part, "reserved page at phys address 0x%016lx\n", rp_pa);
13245d9ca49SDean Nelson 	return rp_pa;
13345d9ca49SDean Nelson }
13445d9ca49SDean Nelson 
13545d9ca49SDean Nelson /*
13645d9ca49SDean Nelson  * Fill the partition reserved page with the information needed by
13745d9ca49SDean Nelson  * other partitions to discover we are alive and establish initial
13845d9ca49SDean Nelson  * communications.
13945d9ca49SDean Nelson  */
14045d9ca49SDean Nelson struct xpc_rsvd_page *
14194bd2708SDean Nelson xpc_setup_rsvd_page(void)
14245d9ca49SDean Nelson {
14345d9ca49SDean Nelson 	struct xpc_rsvd_page *rp;
14494bd2708SDean Nelson 	u64 rp_pa;
145aaa3cd69SDean Nelson 	unsigned long new_stamp;
14645d9ca49SDean Nelson 
14745d9ca49SDean Nelson 	/* get the local reserved page's address */
14845d9ca49SDean Nelson 
14945d9ca49SDean Nelson 	preempt_disable();
15045d9ca49SDean Nelson 	rp_pa = xpc_get_rsvd_page_pa(cpuid_to_nasid(smp_processor_id()));
15145d9ca49SDean Nelson 	preempt_enable();
15245d9ca49SDean Nelson 	if (rp_pa == 0) {
15345d9ca49SDean Nelson 		dev_err(xpc_part, "SAL failed to locate the reserved page\n");
15445d9ca49SDean Nelson 		return NULL;
15545d9ca49SDean Nelson 	}
15645d9ca49SDean Nelson 	rp = (struct xpc_rsvd_page *)__va(rp_pa);
15745d9ca49SDean Nelson 
15894bd2708SDean Nelson 	if (rp->SAL_version < 3) {
15994bd2708SDean Nelson 		/* SAL_versions < 3 had a SAL_partid defined as a u8 */
16094bd2708SDean Nelson 		rp->SAL_partid &= 0xff;
16194bd2708SDean Nelson 	}
16294bd2708SDean Nelson 	BUG_ON(rp->SAL_partid != sn_partition_id);
16394bd2708SDean Nelson 
16494bd2708SDean Nelson 	if (rp->SAL_partid < 0 || rp->SAL_partid >= xp_max_npartitions) {
16594bd2708SDean Nelson 		dev_err(xpc_part, "the reserved page's partid of %d is outside "
16694bd2708SDean Nelson 			"supported range (< 0 || >= %d)\n", rp->SAL_partid,
16794bd2708SDean Nelson 			xp_max_npartitions);
16845d9ca49SDean Nelson 		return NULL;
16945d9ca49SDean Nelson 	}
17045d9ca49SDean Nelson 
17145d9ca49SDean Nelson 	rp->version = XPC_RP_VERSION;
17294bd2708SDean Nelson 	rp->max_npartitions = xp_max_npartitions;
17345d9ca49SDean Nelson 
17445d9ca49SDean Nelson 	/* establish the actual sizes of the nasid masks */
17545d9ca49SDean Nelson 	if (rp->SAL_version == 1) {
17645d9ca49SDean Nelson 		/* SAL_version 1 didn't set the nasids_size field */
17794bd2708SDean Nelson 		rp->SAL_nasids_size = 128;
17845d9ca49SDean Nelson 	}
17994bd2708SDean Nelson 	xp_sizeof_nasid_mask = rp->SAL_nasids_size;
18094bd2708SDean Nelson 	xp_nasid_mask_words = DIV_ROUND_UP(xp_sizeof_nasid_mask,
18194bd2708SDean Nelson 					   BYTES_PER_WORD);
18245d9ca49SDean Nelson 
18345d9ca49SDean Nelson 	/* setup the pointers to the various items in the reserved page */
18445d9ca49SDean Nelson 	xpc_part_nasids = XPC_RP_PART_NASIDS(rp);
18545d9ca49SDean Nelson 	xpc_mach_nasids = XPC_RP_MACH_NASIDS(rp);
18645d9ca49SDean Nelson 
18794bd2708SDean Nelson 	if (xpc_rsvd_page_init(rp) != xpSuccess)
18845d9ca49SDean Nelson 		return NULL;
18945d9ca49SDean Nelson 
19045d9ca49SDean Nelson 	/*
19194bd2708SDean Nelson 	 * Set timestamp of when reserved page was setup by XPC.
19245d9ca49SDean Nelson 	 * This signifies to the remote partition that our reserved
19345d9ca49SDean Nelson 	 * page is initialized.
19445d9ca49SDean Nelson 	 */
195aaa3cd69SDean Nelson 	new_stamp = jiffies;
196aaa3cd69SDean Nelson 	if (new_stamp == 0 || new_stamp == rp->stamp)
197aaa3cd69SDean Nelson 		new_stamp++;
198aaa3cd69SDean Nelson 	rp->stamp = new_stamp;
19945d9ca49SDean Nelson 
20045d9ca49SDean Nelson 	return rp;
20145d9ca49SDean Nelson }
20245d9ca49SDean Nelson 
20345d9ca49SDean Nelson /*
20445d9ca49SDean Nelson  * Get a copy of a portion of the remote partition's rsvd page.
20545d9ca49SDean Nelson  *
20645d9ca49SDean Nelson  * remote_rp points to a buffer that is cacheline aligned for BTE copies and
20745d9ca49SDean Nelson  * is large enough to contain a copy of their reserved page header and
20845d9ca49SDean Nelson  * part_nasids mask.
20945d9ca49SDean Nelson  */
21033ba3c77SDean Nelson enum xp_retval
21145d9ca49SDean Nelson xpc_get_remote_rp(int nasid, u64 *discovered_nasids,
21245d9ca49SDean Nelson 		  struct xpc_rsvd_page *remote_rp, u64 *remote_rp_pa)
21345d9ca49SDean Nelson {
214908787dbSDean Nelson 	int i;
215908787dbSDean Nelson 	enum xp_retval ret;
21645d9ca49SDean Nelson 
21745d9ca49SDean Nelson 	/* get the reserved page's physical address */
21845d9ca49SDean Nelson 
21945d9ca49SDean Nelson 	*remote_rp_pa = xpc_get_rsvd_page_pa(nasid);
2202c2b94f9SDean Nelson 	if (*remote_rp_pa == 0)
22165c17b80SDean Nelson 		return xpNoRsvdPageAddr;
22245d9ca49SDean Nelson 
22345d9ca49SDean Nelson 	/* pull over the reserved page header and part_nasids mask */
224908787dbSDean Nelson 	ret = xp_remote_memcpy(remote_rp, (void *)*remote_rp_pa,
22594bd2708SDean Nelson 			       XPC_RP_HEADER_SIZE + xp_sizeof_nasid_mask);
226908787dbSDean Nelson 	if (ret != xpSuccess)
227908787dbSDean Nelson 		return ret;
22845d9ca49SDean Nelson 
22945d9ca49SDean Nelson 	if (discovered_nasids != NULL) {
23045d9ca49SDean Nelson 		u64 *remote_part_nasids = XPC_RP_PART_NASIDS(remote_rp);
23145d9ca49SDean Nelson 
2322c2b94f9SDean Nelson 		for (i = 0; i < xp_nasid_mask_words; i++)
23345d9ca49SDean Nelson 			discovered_nasids[i] |= remote_part_nasids[i];
23445d9ca49SDean Nelson 	}
23545d9ca49SDean Nelson 
236aaa3cd69SDean Nelson 	/* see if the reserved page has been set up by XPC */
237aaa3cd69SDean Nelson 	if (remote_rp->stamp == 0)
23894bd2708SDean Nelson 		return xpRsvdPageNotSet;
23994bd2708SDean Nelson 
24045d9ca49SDean Nelson 	if (XPC_VERSION_MAJOR(remote_rp->version) !=
24145d9ca49SDean Nelson 	    XPC_VERSION_MAJOR(XPC_RP_VERSION)) {
24265c17b80SDean Nelson 		return xpBadVersion;
24345d9ca49SDean Nelson 	}
24445d9ca49SDean Nelson 
245*a47d5dacSDean Nelson 	/* check that both remote and local partids are valid for each side */
246aaa3cd69SDean Nelson 	if (remote_rp->SAL_partid < 0 ||
247aaa3cd69SDean Nelson 	    remote_rp->SAL_partid >= xp_max_npartitions ||
248aaa3cd69SDean Nelson 	    remote_rp->max_npartitions <= sn_partition_id) {
24994bd2708SDean Nelson 		return xpInvalidPartid;
250aaa3cd69SDean Nelson 	}
251aaa3cd69SDean Nelson 
252aaa3cd69SDean Nelson 	if (remote_rp->SAL_partid == sn_partition_id)
253aaa3cd69SDean Nelson 		return xpLocalPartid;
25494bd2708SDean Nelson 
25565c17b80SDean Nelson 	return xpSuccess;
25645d9ca49SDean Nelson }
25745d9ca49SDean Nelson 
25845d9ca49SDean Nelson /*
259*a47d5dacSDean Nelson  * See if the other side has responded to a partition deactivate request
260*a47d5dacSDean Nelson  * from us. Though we requested the remote partition to deactivate with regard
261*a47d5dacSDean Nelson  * to us, we really only need to wait for the other side to disengage from us.
26245d9ca49SDean Nelson  */
26345d9ca49SDean Nelson int
26445d9ca49SDean Nelson xpc_partition_disengaged(struct xpc_partition *part)
26545d9ca49SDean Nelson {
26664d032baSDean Nelson 	short partid = XPC_PARTID(part);
26745d9ca49SDean Nelson 	int disengaged;
26845d9ca49SDean Nelson 
269*a47d5dacSDean Nelson 	disengaged = !xpc_partition_engaged(partid);
270*a47d5dacSDean Nelson 	if (part->disengage_timeout) {
27145d9ca49SDean Nelson 		if (!disengaged) {
272*a47d5dacSDean Nelson 			if (time_is_after_jiffies(part->disengage_timeout)) {
27345d9ca49SDean Nelson 				/* timelimit hasn't been reached yet */
27445d9ca49SDean Nelson 				return 0;
27545d9ca49SDean Nelson 			}
27645d9ca49SDean Nelson 
27745d9ca49SDean Nelson 			/*
278*a47d5dacSDean Nelson 			 * Other side hasn't responded to our deactivate
27945d9ca49SDean Nelson 			 * request in a timely fashion, so assume it's dead.
28045d9ca49SDean Nelson 			 */
28145d9ca49SDean Nelson 
282*a47d5dacSDean Nelson 			dev_info(xpc_part, "deactivate request to remote "
283*a47d5dacSDean Nelson 				 "partition %d timed out\n", partid);
284*a47d5dacSDean Nelson 			xpc_disengage_timedout = 1;
285*a47d5dacSDean Nelson 			xpc_assume_partition_disengaged(partid);
28645d9ca49SDean Nelson 			disengaged = 1;
28745d9ca49SDean Nelson 		}
288*a47d5dacSDean Nelson 		part->disengage_timeout = 0;
28945d9ca49SDean Nelson 
29045d9ca49SDean Nelson 		/* cancel the timer function, provided it's not us */
291*a47d5dacSDean Nelson 		if (!in_interrupt())
292*a47d5dacSDean Nelson 			del_singleshot_timer_sync(&part->disengage_timer);
29345d9ca49SDean Nelson 
29445d9ca49SDean Nelson 		DBUG_ON(part->act_state != XPC_P_DEACTIVATING &&
29545d9ca49SDean Nelson 			part->act_state != XPC_P_INACTIVE);
2962c2b94f9SDean Nelson 		if (part->act_state != XPC_P_INACTIVE)
29745d9ca49SDean Nelson 			xpc_wakeup_channel_mgr(part);
29845d9ca49SDean Nelson 
299*a47d5dacSDean Nelson 		xpc_cancel_partition_deactivation_request(part);
30045d9ca49SDean Nelson 	}
30145d9ca49SDean Nelson 	return disengaged;
30245d9ca49SDean Nelson }
30345d9ca49SDean Nelson 
30445d9ca49SDean Nelson /*
30545d9ca49SDean Nelson  * Mark specified partition as active.
30645d9ca49SDean Nelson  */
30765c17b80SDean Nelson enum xp_retval
30845d9ca49SDean Nelson xpc_mark_partition_active(struct xpc_partition *part)
30945d9ca49SDean Nelson {
31045d9ca49SDean Nelson 	unsigned long irq_flags;
31165c17b80SDean Nelson 	enum xp_retval ret;
31245d9ca49SDean Nelson 
31345d9ca49SDean Nelson 	dev_dbg(xpc_part, "setting partition %d to ACTIVE\n", XPC_PARTID(part));
31445d9ca49SDean Nelson 
31545d9ca49SDean Nelson 	spin_lock_irqsave(&part->act_lock, irq_flags);
31645d9ca49SDean Nelson 	if (part->act_state == XPC_P_ACTIVATING) {
31745d9ca49SDean Nelson 		part->act_state = XPC_P_ACTIVE;
31865c17b80SDean Nelson 		ret = xpSuccess;
31945d9ca49SDean Nelson 	} else {
32065c17b80SDean Nelson 		DBUG_ON(part->reason == xpSuccess);
32145d9ca49SDean Nelson 		ret = part->reason;
32245d9ca49SDean Nelson 	}
32345d9ca49SDean Nelson 	spin_unlock_irqrestore(&part->act_lock, irq_flags);
32445d9ca49SDean Nelson 
32545d9ca49SDean Nelson 	return ret;
32645d9ca49SDean Nelson }
32745d9ca49SDean Nelson 
32845d9ca49SDean Nelson /*
329*a47d5dacSDean Nelson  * Start the process of deactivating the specified partition.
33045d9ca49SDean Nelson  */
33145d9ca49SDean Nelson void
33245d9ca49SDean Nelson xpc_deactivate_partition(const int line, struct xpc_partition *part,
33365c17b80SDean Nelson 			 enum xp_retval reason)
33445d9ca49SDean Nelson {
33545d9ca49SDean Nelson 	unsigned long irq_flags;
33645d9ca49SDean Nelson 
33745d9ca49SDean Nelson 	spin_lock_irqsave(&part->act_lock, irq_flags);
33845d9ca49SDean Nelson 
33945d9ca49SDean Nelson 	if (part->act_state == XPC_P_INACTIVE) {
34045d9ca49SDean Nelson 		XPC_SET_REASON(part, reason, line);
34145d9ca49SDean Nelson 		spin_unlock_irqrestore(&part->act_lock, irq_flags);
34265c17b80SDean Nelson 		if (reason == xpReactivating) {
34345d9ca49SDean Nelson 			/* we interrupt ourselves to reactivate partition */
344*a47d5dacSDean Nelson 			xpc_request_partition_reactivation(part);
34545d9ca49SDean Nelson 		}
34645d9ca49SDean Nelson 		return;
34745d9ca49SDean Nelson 	}
34845d9ca49SDean Nelson 	if (part->act_state == XPC_P_DEACTIVATING) {
34965c17b80SDean Nelson 		if ((part->reason == xpUnloading && reason != xpUnloading) ||
35065c17b80SDean Nelson 		    reason == xpReactivating) {
35145d9ca49SDean Nelson 			XPC_SET_REASON(part, reason, line);
35245d9ca49SDean Nelson 		}
35345d9ca49SDean Nelson 		spin_unlock_irqrestore(&part->act_lock, irq_flags);
35445d9ca49SDean Nelson 		return;
35545d9ca49SDean Nelson 	}
35645d9ca49SDean Nelson 
35745d9ca49SDean Nelson 	part->act_state = XPC_P_DEACTIVATING;
35845d9ca49SDean Nelson 	XPC_SET_REASON(part, reason, line);
35945d9ca49SDean Nelson 
36045d9ca49SDean Nelson 	spin_unlock_irqrestore(&part->act_lock, irq_flags);
36145d9ca49SDean Nelson 
362*a47d5dacSDean Nelson 	/* ask remote partition to deactivate with regard to us */
363*a47d5dacSDean Nelson 	xpc_request_partition_deactivation(part);
36445d9ca49SDean Nelson 
365*a47d5dacSDean Nelson 	/* set a timelimit on the disengage phase of the deactivation request */
366*a47d5dacSDean Nelson 	part->disengage_timeout = jiffies + (xpc_disengage_timelimit * HZ);
367*a47d5dacSDean Nelson 	part->disengage_timer.expires = part->disengage_timeout;
368*a47d5dacSDean Nelson 	add_timer(&part->disengage_timer);
36945d9ca49SDean Nelson 
37045d9ca49SDean Nelson 	dev_dbg(xpc_part, "bringing partition %d down, reason = %d\n",
37145d9ca49SDean Nelson 		XPC_PARTID(part), reason);
37245d9ca49SDean Nelson 
37345d9ca49SDean Nelson 	xpc_partition_going_down(part, reason);
37445d9ca49SDean Nelson }
37545d9ca49SDean Nelson 
37645d9ca49SDean Nelson /*
37745d9ca49SDean Nelson  * Mark specified partition as inactive.
37845d9ca49SDean Nelson  */
37945d9ca49SDean Nelson void
38045d9ca49SDean Nelson xpc_mark_partition_inactive(struct xpc_partition *part)
38145d9ca49SDean Nelson {
38245d9ca49SDean Nelson 	unsigned long irq_flags;
38345d9ca49SDean Nelson 
38445d9ca49SDean Nelson 	dev_dbg(xpc_part, "setting partition %d to INACTIVE\n",
38545d9ca49SDean Nelson 		XPC_PARTID(part));
38645d9ca49SDean Nelson 
38745d9ca49SDean Nelson 	spin_lock_irqsave(&part->act_lock, irq_flags);
38845d9ca49SDean Nelson 	part->act_state = XPC_P_INACTIVE;
38945d9ca49SDean Nelson 	spin_unlock_irqrestore(&part->act_lock, irq_flags);
39045d9ca49SDean Nelson 	part->remote_rp_pa = 0;
39145d9ca49SDean Nelson }
39245d9ca49SDean Nelson 
39345d9ca49SDean Nelson /*
39445d9ca49SDean Nelson  * SAL has provided a partition and machine mask.  The partition mask
39545d9ca49SDean Nelson  * contains a bit for each even nasid in our partition.  The machine
39645d9ca49SDean Nelson  * mask contains a bit for each even nasid in the entire machine.
39745d9ca49SDean Nelson  *
39845d9ca49SDean Nelson  * Using those two bit arrays, we can determine which nasids are
39945d9ca49SDean Nelson  * known in the machine.  Each should also have a reserved page
40045d9ca49SDean Nelson  * initialized if they are available for partitioning.
40145d9ca49SDean Nelson  */
40245d9ca49SDean Nelson void
40345d9ca49SDean Nelson xpc_discovery(void)
40445d9ca49SDean Nelson {
40545d9ca49SDean Nelson 	void *remote_rp_base;
40645d9ca49SDean Nelson 	struct xpc_rsvd_page *remote_rp;
40745d9ca49SDean Nelson 	u64 remote_rp_pa;
40845d9ca49SDean Nelson 	int region;
40945d9ca49SDean Nelson 	int region_size;
41045d9ca49SDean Nelson 	int max_regions;
41145d9ca49SDean Nelson 	int nasid;
41245d9ca49SDean Nelson 	struct xpc_rsvd_page *rp;
41345d9ca49SDean Nelson 	u64 *discovered_nasids;
41465c17b80SDean Nelson 	enum xp_retval ret;
41545d9ca49SDean Nelson 
41645d9ca49SDean Nelson 	remote_rp = xpc_kmalloc_cacheline_aligned(XPC_RP_HEADER_SIZE +
41794bd2708SDean Nelson 						  xp_sizeof_nasid_mask,
41845d9ca49SDean Nelson 						  GFP_KERNEL, &remote_rp_base);
4192c2b94f9SDean Nelson 	if (remote_rp == NULL)
42045d9ca49SDean Nelson 		return;
4212c2b94f9SDean Nelson 
42245d9ca49SDean Nelson 	discovered_nasids = kzalloc(sizeof(u64) * xp_nasid_mask_words,
42345d9ca49SDean Nelson 				    GFP_KERNEL);
42445d9ca49SDean Nelson 	if (discovered_nasids == NULL) {
42545d9ca49SDean Nelson 		kfree(remote_rp_base);
42645d9ca49SDean Nelson 		return;
42745d9ca49SDean Nelson 	}
42845d9ca49SDean Nelson 
42945d9ca49SDean Nelson 	rp = (struct xpc_rsvd_page *)xpc_rsvd_page;
43045d9ca49SDean Nelson 
43145d9ca49SDean Nelson 	/*
43245d9ca49SDean Nelson 	 * The term 'region' in this context refers to the minimum number of
43345d9ca49SDean Nelson 	 * nodes that can comprise an access protection grouping. The access
43445d9ca49SDean Nelson 	 * protection is in regards to memory, IOI and IPI.
43545d9ca49SDean Nelson 	 */
43645d9ca49SDean Nelson 	max_regions = 64;
43745d9ca49SDean Nelson 	region_size = sn_region_size;
43845d9ca49SDean Nelson 
43945d9ca49SDean Nelson 	switch (region_size) {
44045d9ca49SDean Nelson 	case 128:
44145d9ca49SDean Nelson 		max_regions *= 2;
44245d9ca49SDean Nelson 	case 64:
44345d9ca49SDean Nelson 		max_regions *= 2;
44445d9ca49SDean Nelson 	case 32:
44545d9ca49SDean Nelson 		max_regions *= 2;
44645d9ca49SDean Nelson 		region_size = 16;
44745d9ca49SDean Nelson 		DBUG_ON(!is_shub2());
44845d9ca49SDean Nelson 	}
44945d9ca49SDean Nelson 
45045d9ca49SDean Nelson 	for (region = 0; region < max_regions; region++) {
45145d9ca49SDean Nelson 
4522c2b94f9SDean Nelson 		if (xpc_exiting)
45345d9ca49SDean Nelson 			break;
45445d9ca49SDean Nelson 
45545d9ca49SDean Nelson 		dev_dbg(xpc_part, "searching region %d\n", region);
45645d9ca49SDean Nelson 
45745d9ca49SDean Nelson 		for (nasid = (region * region_size * 2);
4584a3ad2ddSDean Nelson 		     nasid < ((region + 1) * region_size * 2); nasid += 2) {
45945d9ca49SDean Nelson 
4602c2b94f9SDean Nelson 			if (xpc_exiting)
46145d9ca49SDean Nelson 				break;
46245d9ca49SDean Nelson 
46345d9ca49SDean Nelson 			dev_dbg(xpc_part, "checking nasid %d\n", nasid);
46445d9ca49SDean Nelson 
46545d9ca49SDean Nelson 			if (XPC_NASID_IN_ARRAY(nasid, xpc_part_nasids)) {
46645d9ca49SDean Nelson 				dev_dbg(xpc_part, "PROM indicates Nasid %d is "
46745d9ca49SDean Nelson 					"part of the local partition; skipping "
46845d9ca49SDean Nelson 					"region\n", nasid);
46945d9ca49SDean Nelson 				break;
47045d9ca49SDean Nelson 			}
47145d9ca49SDean Nelson 
47245d9ca49SDean Nelson 			if (!(XPC_NASID_IN_ARRAY(nasid, xpc_mach_nasids))) {
47345d9ca49SDean Nelson 				dev_dbg(xpc_part, "PROM indicates Nasid %d was "
47445d9ca49SDean Nelson 					"not on Numa-Link network at reset\n",
47545d9ca49SDean Nelson 					nasid);
47645d9ca49SDean Nelson 				continue;
47745d9ca49SDean Nelson 			}
47845d9ca49SDean Nelson 
47945d9ca49SDean Nelson 			if (XPC_NASID_IN_ARRAY(nasid, discovered_nasids)) {
48045d9ca49SDean Nelson 				dev_dbg(xpc_part, "Nasid %d is part of a "
48145d9ca49SDean Nelson 					"partition which was previously "
48245d9ca49SDean Nelson 					"discovered\n", nasid);
48345d9ca49SDean Nelson 				continue;
48445d9ca49SDean Nelson 			}
48545d9ca49SDean Nelson 
48633ba3c77SDean Nelson 			/* pull over the rsvd page header & part_nasids mask */
48745d9ca49SDean Nelson 
48845d9ca49SDean Nelson 			ret = xpc_get_remote_rp(nasid, discovered_nasids,
48945d9ca49SDean Nelson 						remote_rp, &remote_rp_pa);
49065c17b80SDean Nelson 			if (ret != xpSuccess) {
49145d9ca49SDean Nelson 				dev_dbg(xpc_part, "unable to get reserved page "
49245d9ca49SDean Nelson 					"from nasid %d, reason=%d\n", nasid,
49345d9ca49SDean Nelson 					ret);
49445d9ca49SDean Nelson 
49565c17b80SDean Nelson 				if (ret == xpLocalPartid)
49645d9ca49SDean Nelson 					break;
4972c2b94f9SDean Nelson 
49845d9ca49SDean Nelson 				continue;
49945d9ca49SDean Nelson 			}
50045d9ca49SDean Nelson 
501*a47d5dacSDean Nelson 			xpc_request_partition_activation(remote_rp,
50233ba3c77SDean Nelson 							 remote_rp_pa, nasid);
50345d9ca49SDean Nelson 		}
50445d9ca49SDean Nelson 	}
50545d9ca49SDean Nelson 
50645d9ca49SDean Nelson 	kfree(discovered_nasids);
50745d9ca49SDean Nelson 	kfree(remote_rp_base);
50845d9ca49SDean Nelson }
50945d9ca49SDean Nelson 
51045d9ca49SDean Nelson /*
51145d9ca49SDean Nelson  * Given a partid, get the nasids owned by that partition from the
51245d9ca49SDean Nelson  * remote partition's reserved page.
51345d9ca49SDean Nelson  */
51465c17b80SDean Nelson enum xp_retval
51564d032baSDean Nelson xpc_initiate_partid_to_nasids(short partid, void *nasid_mask)
51645d9ca49SDean Nelson {
51745d9ca49SDean Nelson 	struct xpc_partition *part;
51845d9ca49SDean Nelson 	u64 part_nasid_pa;
51945d9ca49SDean Nelson 
52045d9ca49SDean Nelson 	part = &xpc_partitions[partid];
5212c2b94f9SDean Nelson 	if (part->remote_rp_pa == 0)
52265c17b80SDean Nelson 		return xpPartitionDown;
52345d9ca49SDean Nelson 
52445d9ca49SDean Nelson 	memset(nasid_mask, 0, XP_NASID_MASK_BYTES);
52545d9ca49SDean Nelson 
52645d9ca49SDean Nelson 	part_nasid_pa = (u64)XPC_RP_PART_NASIDS(part->remote_rp_pa);
52745d9ca49SDean Nelson 
528908787dbSDean Nelson 	return xp_remote_memcpy(nasid_mask, (void *)part_nasid_pa,
52994bd2708SDean Nelson 				xp_sizeof_nasid_mask);
53045d9ca49SDean Nelson }
531