xref: /openbmc/linux/drivers/misc/sgi-xp/xpc_partition.c (revision 83469b5525b4a35be40b17cb41d64118d84d9f80)
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 
18261f3b49SDean Nelson #include <linux/device.h>
19261f3b49SDean Nelson #include <linux/hardirq.h>
2045d9ca49SDean Nelson #include "xpc.h"
2145d9ca49SDean Nelson 
2245d9ca49SDean Nelson /* XPC is exiting flag */
2345d9ca49SDean Nelson int xpc_exiting;
2445d9ca49SDean Nelson 
2545d9ca49SDean Nelson /* this partition's reserved page pointers */
2645d9ca49SDean Nelson struct xpc_rsvd_page *xpc_rsvd_page;
2704de7418SDean Nelson static unsigned long *xpc_part_nasids;
2804de7418SDean Nelson unsigned long *xpc_mach_nasids;
2945d9ca49SDean Nelson 
3004de7418SDean Nelson static int xpc_nasid_mask_nbytes;	/* #of bytes in nasid mask */
3104de7418SDean Nelson int xpc_nasid_mask_nlongs;	/* #of longs in nasid mask */
3245d9ca49SDean Nelson 
33bc63d387SDean Nelson struct xpc_partition *xpc_partitions;
3445d9ca49SDean Nelson 
3545d9ca49SDean Nelson /*
3645d9ca49SDean Nelson  * Guarantee that the kmalloc'd memory is cacheline aligned.
3745d9ca49SDean Nelson  */
3845d9ca49SDean Nelson void *
3945d9ca49SDean Nelson xpc_kmalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
4045d9ca49SDean Nelson {
4145d9ca49SDean Nelson 	/* see if kmalloc will give us cachline aligned memory by default */
4245d9ca49SDean Nelson 	*base = kmalloc(size, flags);
432c2b94f9SDean Nelson 	if (*base == NULL)
4445d9ca49SDean Nelson 		return NULL;
452c2b94f9SDean Nelson 
462c2b94f9SDean Nelson 	if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
4745d9ca49SDean Nelson 		return *base;
482c2b94f9SDean Nelson 
4945d9ca49SDean Nelson 	kfree(*base);
5045d9ca49SDean Nelson 
5145d9ca49SDean Nelson 	/* nope, we'll have to do it ourselves */
5245d9ca49SDean Nelson 	*base = kmalloc(size + L1_CACHE_BYTES, flags);
532c2b94f9SDean Nelson 	if (*base == NULL)
5445d9ca49SDean Nelson 		return NULL;
552c2b94f9SDean Nelson 
5645d9ca49SDean Nelson 	return (void *)L1_CACHE_ALIGN((u64)*base);
5745d9ca49SDean Nelson }
5845d9ca49SDean Nelson 
5945d9ca49SDean Nelson /*
6045d9ca49SDean Nelson  * Given a nasid, get the physical address of the  partition's reserved page
6145d9ca49SDean Nelson  * for that nasid. This function returns 0 on any error.
6245d9ca49SDean Nelson  */
63a812dcc3SDean Nelson static unsigned long
6445d9ca49SDean Nelson xpc_get_rsvd_page_pa(int nasid)
6545d9ca49SDean Nelson {
66908787dbSDean Nelson 	enum xp_retval ret;
6745d9ca49SDean Nelson 	u64 cookie = 0;
68a812dcc3SDean Nelson 	unsigned long rp_pa = nasid;	/* seed with nasid */
69261f3b49SDean Nelson 	size_t len = 0;
70a812dcc3SDean Nelson 	size_t buf_len = 0;
71a812dcc3SDean Nelson 	void *buf = buf;
7245d9ca49SDean Nelson 	void *buf_base = NULL;
7345d9ca49SDean Nelson 
7445d9ca49SDean Nelson 	while (1) {
7545d9ca49SDean Nelson 
76261f3b49SDean Nelson 		ret = xpc_get_partition_rsvd_page_pa(buf, &cookie, &rp_pa,
7745d9ca49SDean Nelson 						     &len);
7845d9ca49SDean Nelson 
79261f3b49SDean Nelson 		dev_dbg(xpc_part, "SAL returned with ret=%d, cookie=0x%016lx, "
80261f3b49SDean Nelson 			"address=0x%016lx, len=0x%016lx\n", ret,
81a812dcc3SDean Nelson 			(unsigned long)cookie, rp_pa, len);
8245d9ca49SDean Nelson 
83261f3b49SDean Nelson 		if (ret != xpNeedMoreInfo)
8445d9ca49SDean Nelson 			break;
8545d9ca49SDean Nelson 
86ea57f80cSDean Nelson 		/* !!! L1_CACHE_ALIGN() is only a sn2-bte_copy requirement */
8745d9ca49SDean Nelson 		if (L1_CACHE_ALIGN(len) > buf_len) {
8845d9ca49SDean Nelson 			kfree(buf_base);
8945d9ca49SDean Nelson 			buf_len = L1_CACHE_ALIGN(len);
90a812dcc3SDean Nelson 			buf = xpc_kmalloc_cacheline_aligned(buf_len, GFP_KERNEL,
914a3ad2ddSDean Nelson 							    &buf_base);
9245d9ca49SDean Nelson 			if (buf_base == NULL) {
9345d9ca49SDean Nelson 				dev_err(xpc_part, "unable to kmalloc "
94a812dcc3SDean Nelson 					"len=0x%016lx\n", buf_len);
95261f3b49SDean Nelson 				ret = xpNoMemory;
9645d9ca49SDean Nelson 				break;
9745d9ca49SDean Nelson 			}
9845d9ca49SDean Nelson 		}
9945d9ca49SDean Nelson 
100a812dcc3SDean Nelson 		ret = xp_remote_memcpy(xp_pa(buf), rp_pa, buf_len);
101908787dbSDean Nelson 		if (ret != xpSuccess) {
102908787dbSDean Nelson 			dev_dbg(xpc_part, "xp_remote_memcpy failed %d\n", ret);
10345d9ca49SDean Nelson 			break;
10445d9ca49SDean Nelson 		}
10545d9ca49SDean Nelson 	}
10645d9ca49SDean Nelson 
10745d9ca49SDean Nelson 	kfree(buf_base);
10845d9ca49SDean Nelson 
109261f3b49SDean Nelson 	if (ret != xpSuccess)
11045d9ca49SDean Nelson 		rp_pa = 0;
1112c2b94f9SDean Nelson 
112a812dcc3SDean Nelson 	dev_dbg(xpc_part, "reserved page at phys address 0x%016lx\n", rp_pa);
11345d9ca49SDean Nelson 	return rp_pa;
11445d9ca49SDean Nelson }
11545d9ca49SDean Nelson 
11645d9ca49SDean Nelson /*
11745d9ca49SDean Nelson  * Fill the partition reserved page with the information needed by
11845d9ca49SDean Nelson  * other partitions to discover we are alive and establish initial
11945d9ca49SDean Nelson  * communications.
12045d9ca49SDean Nelson  */
12145d9ca49SDean Nelson struct xpc_rsvd_page *
12294bd2708SDean Nelson xpc_setup_rsvd_page(void)
12345d9ca49SDean Nelson {
12445d9ca49SDean Nelson 	struct xpc_rsvd_page *rp;
125a812dcc3SDean Nelson 	unsigned long rp_pa;
12681fe7883SDean Nelson 	unsigned long new_ts_jiffies;
12745d9ca49SDean Nelson 
12845d9ca49SDean Nelson 	/* get the local reserved page's address */
12945d9ca49SDean Nelson 
13045d9ca49SDean Nelson 	preempt_disable();
131261f3b49SDean Nelson 	rp_pa = xpc_get_rsvd_page_pa(xp_cpu_to_nasid(smp_processor_id()));
13245d9ca49SDean Nelson 	preempt_enable();
13345d9ca49SDean Nelson 	if (rp_pa == 0) {
13445d9ca49SDean Nelson 		dev_err(xpc_part, "SAL failed to locate the reserved page\n");
13545d9ca49SDean Nelson 		return NULL;
13645d9ca49SDean Nelson 	}
13745d9ca49SDean Nelson 	rp = (struct xpc_rsvd_page *)__va(rp_pa);
13845d9ca49SDean Nelson 
13994bd2708SDean Nelson 	if (rp->SAL_version < 3) {
14094bd2708SDean Nelson 		/* SAL_versions < 3 had a SAL_partid defined as a u8 */
14194bd2708SDean Nelson 		rp->SAL_partid &= 0xff;
14294bd2708SDean Nelson 	}
143261f3b49SDean Nelson 	BUG_ON(rp->SAL_partid != xp_partition_id);
14494bd2708SDean Nelson 
14594bd2708SDean Nelson 	if (rp->SAL_partid < 0 || rp->SAL_partid >= xp_max_npartitions) {
14694bd2708SDean Nelson 		dev_err(xpc_part, "the reserved page's partid of %d is outside "
14794bd2708SDean Nelson 			"supported range (< 0 || >= %d)\n", rp->SAL_partid,
14894bd2708SDean Nelson 			xp_max_npartitions);
14945d9ca49SDean Nelson 		return NULL;
15045d9ca49SDean Nelson 	}
15145d9ca49SDean Nelson 
15245d9ca49SDean Nelson 	rp->version = XPC_RP_VERSION;
15394bd2708SDean Nelson 	rp->max_npartitions = xp_max_npartitions;
15445d9ca49SDean Nelson 
15545d9ca49SDean Nelson 	/* establish the actual sizes of the nasid masks */
15645d9ca49SDean Nelson 	if (rp->SAL_version == 1) {
15745d9ca49SDean Nelson 		/* SAL_version 1 didn't set the nasids_size field */
15894bd2708SDean Nelson 		rp->SAL_nasids_size = 128;
15945d9ca49SDean Nelson 	}
16004de7418SDean Nelson 	xpc_nasid_mask_nbytes = rp->SAL_nasids_size;
16104de7418SDean Nelson 	xpc_nasid_mask_nlongs = BITS_TO_LONGS(rp->SAL_nasids_size *
16204de7418SDean Nelson 					      BITS_PER_BYTE);
16345d9ca49SDean Nelson 
16445d9ca49SDean Nelson 	/* setup the pointers to the various items in the reserved page */
16545d9ca49SDean Nelson 	xpc_part_nasids = XPC_RP_PART_NASIDS(rp);
16645d9ca49SDean Nelson 	xpc_mach_nasids = XPC_RP_MACH_NASIDS(rp);
16745d9ca49SDean Nelson 
16894bd2708SDean Nelson 	if (xpc_rsvd_page_init(rp) != xpSuccess)
16945d9ca49SDean Nelson 		return NULL;
17045d9ca49SDean Nelson 
17145d9ca49SDean Nelson 	/*
17294bd2708SDean Nelson 	 * Set timestamp of when reserved page was setup by XPC.
17345d9ca49SDean Nelson 	 * This signifies to the remote partition that our reserved
17445d9ca49SDean Nelson 	 * page is initialized.
17545d9ca49SDean Nelson 	 */
17681fe7883SDean Nelson 	new_ts_jiffies = jiffies;
17781fe7883SDean Nelson 	if (new_ts_jiffies == 0 || new_ts_jiffies == rp->ts_jiffies)
17881fe7883SDean Nelson 		new_ts_jiffies++;
17981fe7883SDean Nelson 	rp->ts_jiffies = new_ts_jiffies;
18045d9ca49SDean Nelson 
18145d9ca49SDean Nelson 	return rp;
18245d9ca49SDean Nelson }
18345d9ca49SDean Nelson 
18445d9ca49SDean Nelson /*
18545d9ca49SDean Nelson  * Get a copy of a portion of the remote partition's rsvd page.
18645d9ca49SDean Nelson  *
18745d9ca49SDean Nelson  * remote_rp points to a buffer that is cacheline aligned for BTE copies and
18845d9ca49SDean Nelson  * is large enough to contain a copy of their reserved page header and
18945d9ca49SDean Nelson  * part_nasids mask.
19045d9ca49SDean Nelson  */
19133ba3c77SDean Nelson enum xp_retval
19204de7418SDean Nelson xpc_get_remote_rp(int nasid, unsigned long *discovered_nasids,
193a812dcc3SDean Nelson 		  struct xpc_rsvd_page *remote_rp, unsigned long *remote_rp_pa)
19445d9ca49SDean Nelson {
19504de7418SDean Nelson 	int l;
196908787dbSDean Nelson 	enum xp_retval ret;
19745d9ca49SDean Nelson 
19845d9ca49SDean Nelson 	/* get the reserved page's physical address */
19945d9ca49SDean Nelson 
20045d9ca49SDean Nelson 	*remote_rp_pa = xpc_get_rsvd_page_pa(nasid);
2012c2b94f9SDean Nelson 	if (*remote_rp_pa == 0)
20265c17b80SDean Nelson 		return xpNoRsvdPageAddr;
20345d9ca49SDean Nelson 
20445d9ca49SDean Nelson 	/* pull over the reserved page header and part_nasids mask */
205a812dcc3SDean Nelson 	ret = xp_remote_memcpy(xp_pa(remote_rp), *remote_rp_pa,
20604de7418SDean Nelson 			       XPC_RP_HEADER_SIZE + xpc_nasid_mask_nbytes);
207908787dbSDean Nelson 	if (ret != xpSuccess)
208908787dbSDean Nelson 		return ret;
20945d9ca49SDean Nelson 
21045d9ca49SDean Nelson 	if (discovered_nasids != NULL) {
21104de7418SDean Nelson 		unsigned long *remote_part_nasids =
21204de7418SDean Nelson 		    XPC_RP_PART_NASIDS(remote_rp);
21345d9ca49SDean Nelson 
21404de7418SDean Nelson 		for (l = 0; l < xpc_nasid_mask_nlongs; l++)
21504de7418SDean Nelson 			discovered_nasids[l] |= remote_part_nasids[l];
21645d9ca49SDean Nelson 	}
21745d9ca49SDean Nelson 
21881fe7883SDean Nelson 	/* zero timestamp indicates the reserved page has not been setup */
21981fe7883SDean Nelson 	if (remote_rp->ts_jiffies == 0)
22094bd2708SDean Nelson 		return xpRsvdPageNotSet;
22194bd2708SDean Nelson 
22245d9ca49SDean Nelson 	if (XPC_VERSION_MAJOR(remote_rp->version) !=
22345d9ca49SDean Nelson 	    XPC_VERSION_MAJOR(XPC_RP_VERSION)) {
22465c17b80SDean Nelson 		return xpBadVersion;
22545d9ca49SDean Nelson 	}
22645d9ca49SDean Nelson 
227a47d5dacSDean Nelson 	/* check that both remote and local partids are valid for each side */
228aaa3cd69SDean Nelson 	if (remote_rp->SAL_partid < 0 ||
229aaa3cd69SDean Nelson 	    remote_rp->SAL_partid >= xp_max_npartitions ||
230261f3b49SDean Nelson 	    remote_rp->max_npartitions <= xp_partition_id) {
23194bd2708SDean Nelson 		return xpInvalidPartid;
232aaa3cd69SDean Nelson 	}
233aaa3cd69SDean Nelson 
234261f3b49SDean Nelson 	if (remote_rp->SAL_partid == xp_partition_id)
235aaa3cd69SDean Nelson 		return xpLocalPartid;
23694bd2708SDean Nelson 
23765c17b80SDean Nelson 	return xpSuccess;
23845d9ca49SDean Nelson }
23945d9ca49SDean Nelson 
24045d9ca49SDean Nelson /*
241a47d5dacSDean Nelson  * See if the other side has responded to a partition deactivate request
242a47d5dacSDean Nelson  * from us. Though we requested the remote partition to deactivate with regard
243a47d5dacSDean Nelson  * to us, we really only need to wait for the other side to disengage from us.
24445d9ca49SDean Nelson  */
24545d9ca49SDean Nelson int
24645d9ca49SDean Nelson xpc_partition_disengaged(struct xpc_partition *part)
24745d9ca49SDean Nelson {
24864d032baSDean Nelson 	short partid = XPC_PARTID(part);
24945d9ca49SDean Nelson 	int disengaged;
25045d9ca49SDean Nelson 
251a47d5dacSDean Nelson 	disengaged = !xpc_partition_engaged(partid);
252a47d5dacSDean Nelson 	if (part->disengage_timeout) {
25345d9ca49SDean Nelson 		if (!disengaged) {
254a47d5dacSDean Nelson 			if (time_is_after_jiffies(part->disengage_timeout)) {
25545d9ca49SDean Nelson 				/* timelimit hasn't been reached yet */
25645d9ca49SDean Nelson 				return 0;
25745d9ca49SDean Nelson 			}
25845d9ca49SDean Nelson 
25945d9ca49SDean Nelson 			/*
260a47d5dacSDean Nelson 			 * Other side hasn't responded to our deactivate
26145d9ca49SDean Nelson 			 * request in a timely fashion, so assume it's dead.
26245d9ca49SDean Nelson 			 */
26345d9ca49SDean Nelson 
264a47d5dacSDean Nelson 			dev_info(xpc_part, "deactivate request to remote "
265a47d5dacSDean Nelson 				 "partition %d timed out\n", partid);
266a47d5dacSDean Nelson 			xpc_disengage_timedout = 1;
267a47d5dacSDean Nelson 			xpc_assume_partition_disengaged(partid);
26845d9ca49SDean Nelson 			disengaged = 1;
26945d9ca49SDean Nelson 		}
270a47d5dacSDean Nelson 		part->disengage_timeout = 0;
27145d9ca49SDean Nelson 
27245d9ca49SDean Nelson 		/* cancel the timer function, provided it's not us */
273a47d5dacSDean Nelson 		if (!in_interrupt())
274a47d5dacSDean Nelson 			del_singleshot_timer_sync(&part->disengage_timer);
27545d9ca49SDean Nelson 
276*83469b55SDean Nelson 		DBUG_ON(part->act_state != XPC_P_AS_DEACTIVATING &&
277*83469b55SDean Nelson 			part->act_state != XPC_P_AS_INACTIVE);
278*83469b55SDean Nelson 		if (part->act_state != XPC_P_AS_INACTIVE)
27945d9ca49SDean Nelson 			xpc_wakeup_channel_mgr(part);
28045d9ca49SDean Nelson 
281a47d5dacSDean Nelson 		xpc_cancel_partition_deactivation_request(part);
28245d9ca49SDean Nelson 	}
28345d9ca49SDean Nelson 	return disengaged;
28445d9ca49SDean Nelson }
28545d9ca49SDean Nelson 
28645d9ca49SDean Nelson /*
28745d9ca49SDean Nelson  * Mark specified partition as active.
28845d9ca49SDean Nelson  */
28965c17b80SDean Nelson enum xp_retval
29045d9ca49SDean Nelson xpc_mark_partition_active(struct xpc_partition *part)
29145d9ca49SDean Nelson {
29245d9ca49SDean Nelson 	unsigned long irq_flags;
29365c17b80SDean Nelson 	enum xp_retval ret;
29445d9ca49SDean Nelson 
29545d9ca49SDean Nelson 	dev_dbg(xpc_part, "setting partition %d to ACTIVE\n", XPC_PARTID(part));
29645d9ca49SDean Nelson 
29745d9ca49SDean Nelson 	spin_lock_irqsave(&part->act_lock, irq_flags);
298*83469b55SDean Nelson 	if (part->act_state == XPC_P_AS_ACTIVATING) {
299*83469b55SDean Nelson 		part->act_state = XPC_P_AS_ACTIVE;
30065c17b80SDean Nelson 		ret = xpSuccess;
30145d9ca49SDean Nelson 	} else {
30265c17b80SDean Nelson 		DBUG_ON(part->reason == xpSuccess);
30345d9ca49SDean Nelson 		ret = part->reason;
30445d9ca49SDean Nelson 	}
30545d9ca49SDean Nelson 	spin_unlock_irqrestore(&part->act_lock, irq_flags);
30645d9ca49SDean Nelson 
30745d9ca49SDean Nelson 	return ret;
30845d9ca49SDean Nelson }
30945d9ca49SDean Nelson 
31045d9ca49SDean Nelson /*
311a47d5dacSDean Nelson  * Start the process of deactivating the specified partition.
31245d9ca49SDean Nelson  */
31345d9ca49SDean Nelson void
31445d9ca49SDean Nelson xpc_deactivate_partition(const int line, struct xpc_partition *part,
31565c17b80SDean Nelson 			 enum xp_retval reason)
31645d9ca49SDean Nelson {
31745d9ca49SDean Nelson 	unsigned long irq_flags;
31845d9ca49SDean Nelson 
31945d9ca49SDean Nelson 	spin_lock_irqsave(&part->act_lock, irq_flags);
32045d9ca49SDean Nelson 
321*83469b55SDean Nelson 	if (part->act_state == XPC_P_AS_INACTIVE) {
32245d9ca49SDean Nelson 		XPC_SET_REASON(part, reason, line);
32345d9ca49SDean Nelson 		spin_unlock_irqrestore(&part->act_lock, irq_flags);
32465c17b80SDean Nelson 		if (reason == xpReactivating) {
32545d9ca49SDean Nelson 			/* we interrupt ourselves to reactivate partition */
326a47d5dacSDean Nelson 			xpc_request_partition_reactivation(part);
32745d9ca49SDean Nelson 		}
32845d9ca49SDean Nelson 		return;
32945d9ca49SDean Nelson 	}
330*83469b55SDean Nelson 	if (part->act_state == XPC_P_AS_DEACTIVATING) {
33165c17b80SDean Nelson 		if ((part->reason == xpUnloading && reason != xpUnloading) ||
33265c17b80SDean Nelson 		    reason == xpReactivating) {
33345d9ca49SDean Nelson 			XPC_SET_REASON(part, reason, line);
33445d9ca49SDean Nelson 		}
33545d9ca49SDean Nelson 		spin_unlock_irqrestore(&part->act_lock, irq_flags);
33645d9ca49SDean Nelson 		return;
33745d9ca49SDean Nelson 	}
33845d9ca49SDean Nelson 
339*83469b55SDean Nelson 	part->act_state = XPC_P_AS_DEACTIVATING;
34045d9ca49SDean Nelson 	XPC_SET_REASON(part, reason, line);
34145d9ca49SDean Nelson 
34245d9ca49SDean Nelson 	spin_unlock_irqrestore(&part->act_lock, irq_flags);
34345d9ca49SDean Nelson 
344a47d5dacSDean Nelson 	/* ask remote partition to deactivate with regard to us */
345a47d5dacSDean Nelson 	xpc_request_partition_deactivation(part);
34645d9ca49SDean Nelson 
347a47d5dacSDean Nelson 	/* set a timelimit on the disengage phase of the deactivation request */
348a47d5dacSDean Nelson 	part->disengage_timeout = jiffies + (xpc_disengage_timelimit * HZ);
349a47d5dacSDean Nelson 	part->disengage_timer.expires = part->disengage_timeout;
350a47d5dacSDean Nelson 	add_timer(&part->disengage_timer);
35145d9ca49SDean Nelson 
35245d9ca49SDean Nelson 	dev_dbg(xpc_part, "bringing partition %d down, reason = %d\n",
35345d9ca49SDean Nelson 		XPC_PARTID(part), reason);
35445d9ca49SDean Nelson 
35545d9ca49SDean Nelson 	xpc_partition_going_down(part, reason);
35645d9ca49SDean Nelson }
35745d9ca49SDean Nelson 
35845d9ca49SDean Nelson /*
35945d9ca49SDean Nelson  * Mark specified partition as inactive.
36045d9ca49SDean Nelson  */
36145d9ca49SDean Nelson void
36245d9ca49SDean Nelson xpc_mark_partition_inactive(struct xpc_partition *part)
36345d9ca49SDean Nelson {
36445d9ca49SDean Nelson 	unsigned long irq_flags;
36545d9ca49SDean Nelson 
36645d9ca49SDean Nelson 	dev_dbg(xpc_part, "setting partition %d to INACTIVE\n",
36745d9ca49SDean Nelson 		XPC_PARTID(part));
36845d9ca49SDean Nelson 
36945d9ca49SDean Nelson 	spin_lock_irqsave(&part->act_lock, irq_flags);
370*83469b55SDean Nelson 	part->act_state = XPC_P_AS_INACTIVE;
37145d9ca49SDean Nelson 	spin_unlock_irqrestore(&part->act_lock, irq_flags);
37245d9ca49SDean Nelson 	part->remote_rp_pa = 0;
37345d9ca49SDean Nelson }
37445d9ca49SDean Nelson 
37545d9ca49SDean Nelson /*
37645d9ca49SDean Nelson  * SAL has provided a partition and machine mask.  The partition mask
37745d9ca49SDean Nelson  * contains a bit for each even nasid in our partition.  The machine
37845d9ca49SDean Nelson  * mask contains a bit for each even nasid in the entire machine.
37945d9ca49SDean Nelson  *
38045d9ca49SDean Nelson  * Using those two bit arrays, we can determine which nasids are
38145d9ca49SDean Nelson  * known in the machine.  Each should also have a reserved page
38245d9ca49SDean Nelson  * initialized if they are available for partitioning.
38345d9ca49SDean Nelson  */
38445d9ca49SDean Nelson void
38545d9ca49SDean Nelson xpc_discovery(void)
38645d9ca49SDean Nelson {
38745d9ca49SDean Nelson 	void *remote_rp_base;
38845d9ca49SDean Nelson 	struct xpc_rsvd_page *remote_rp;
389a812dcc3SDean Nelson 	unsigned long remote_rp_pa;
39045d9ca49SDean Nelson 	int region;
39145d9ca49SDean Nelson 	int region_size;
39245d9ca49SDean Nelson 	int max_regions;
39345d9ca49SDean Nelson 	int nasid;
39445d9ca49SDean Nelson 	struct xpc_rsvd_page *rp;
39504de7418SDean Nelson 	unsigned long *discovered_nasids;
39665c17b80SDean Nelson 	enum xp_retval ret;
39745d9ca49SDean Nelson 
39845d9ca49SDean Nelson 	remote_rp = xpc_kmalloc_cacheline_aligned(XPC_RP_HEADER_SIZE +
39904de7418SDean Nelson 						  xpc_nasid_mask_nbytes,
40045d9ca49SDean Nelson 						  GFP_KERNEL, &remote_rp_base);
4012c2b94f9SDean Nelson 	if (remote_rp == NULL)
40245d9ca49SDean Nelson 		return;
4032c2b94f9SDean Nelson 
40404de7418SDean Nelson 	discovered_nasids = kzalloc(sizeof(long) * xpc_nasid_mask_nlongs,
40545d9ca49SDean Nelson 				    GFP_KERNEL);
40645d9ca49SDean Nelson 	if (discovered_nasids == NULL) {
40745d9ca49SDean Nelson 		kfree(remote_rp_base);
40845d9ca49SDean Nelson 		return;
40945d9ca49SDean Nelson 	}
41045d9ca49SDean Nelson 
41145d9ca49SDean Nelson 	rp = (struct xpc_rsvd_page *)xpc_rsvd_page;
41245d9ca49SDean Nelson 
41345d9ca49SDean Nelson 	/*
41445d9ca49SDean Nelson 	 * The term 'region' in this context refers to the minimum number of
41545d9ca49SDean Nelson 	 * nodes that can comprise an access protection grouping. The access
41645d9ca49SDean Nelson 	 * protection is in regards to memory, IOI and IPI.
41745d9ca49SDean Nelson 	 */
41845d9ca49SDean Nelson 	max_regions = 64;
419261f3b49SDean Nelson 	region_size = xp_region_size;
42045d9ca49SDean Nelson 
42145d9ca49SDean Nelson 	switch (region_size) {
42245d9ca49SDean Nelson 	case 128:
42345d9ca49SDean Nelson 		max_regions *= 2;
42445d9ca49SDean Nelson 	case 64:
42545d9ca49SDean Nelson 		max_regions *= 2;
42645d9ca49SDean Nelson 	case 32:
42745d9ca49SDean Nelson 		max_regions *= 2;
42845d9ca49SDean Nelson 		region_size = 16;
42945d9ca49SDean Nelson 		DBUG_ON(!is_shub2());
43045d9ca49SDean Nelson 	}
43145d9ca49SDean Nelson 
43245d9ca49SDean Nelson 	for (region = 0; region < max_regions; region++) {
43345d9ca49SDean Nelson 
4342c2b94f9SDean Nelson 		if (xpc_exiting)
43545d9ca49SDean Nelson 			break;
43645d9ca49SDean Nelson 
43745d9ca49SDean Nelson 		dev_dbg(xpc_part, "searching region %d\n", region);
43845d9ca49SDean Nelson 
43945d9ca49SDean Nelson 		for (nasid = (region * region_size * 2);
4404a3ad2ddSDean Nelson 		     nasid < ((region + 1) * region_size * 2); nasid += 2) {
44145d9ca49SDean Nelson 
4422c2b94f9SDean Nelson 			if (xpc_exiting)
44345d9ca49SDean Nelson 				break;
44445d9ca49SDean Nelson 
44545d9ca49SDean Nelson 			dev_dbg(xpc_part, "checking nasid %d\n", nasid);
44645d9ca49SDean Nelson 
44704de7418SDean Nelson 			if (test_bit(nasid / 2, xpc_part_nasids)) {
44845d9ca49SDean Nelson 				dev_dbg(xpc_part, "PROM indicates Nasid %d is "
44945d9ca49SDean Nelson 					"part of the local partition; skipping "
45045d9ca49SDean Nelson 					"region\n", nasid);
45145d9ca49SDean Nelson 				break;
45245d9ca49SDean Nelson 			}
45345d9ca49SDean Nelson 
45404de7418SDean Nelson 			if (!(test_bit(nasid / 2, xpc_mach_nasids))) {
45545d9ca49SDean Nelson 				dev_dbg(xpc_part, "PROM indicates Nasid %d was "
45645d9ca49SDean Nelson 					"not on Numa-Link network at reset\n",
45745d9ca49SDean Nelson 					nasid);
45845d9ca49SDean Nelson 				continue;
45945d9ca49SDean Nelson 			}
46045d9ca49SDean Nelson 
46104de7418SDean Nelson 			if (test_bit(nasid / 2, discovered_nasids)) {
46245d9ca49SDean Nelson 				dev_dbg(xpc_part, "Nasid %d is part of a "
46345d9ca49SDean Nelson 					"partition which was previously "
46445d9ca49SDean Nelson 					"discovered\n", nasid);
46545d9ca49SDean Nelson 				continue;
46645d9ca49SDean Nelson 			}
46745d9ca49SDean Nelson 
46833ba3c77SDean Nelson 			/* pull over the rsvd page header & part_nasids mask */
46945d9ca49SDean Nelson 
47045d9ca49SDean Nelson 			ret = xpc_get_remote_rp(nasid, discovered_nasids,
47145d9ca49SDean Nelson 						remote_rp, &remote_rp_pa);
47265c17b80SDean Nelson 			if (ret != xpSuccess) {
47345d9ca49SDean Nelson 				dev_dbg(xpc_part, "unable to get reserved page "
47445d9ca49SDean Nelson 					"from nasid %d, reason=%d\n", nasid,
47545d9ca49SDean Nelson 					ret);
47645d9ca49SDean Nelson 
47765c17b80SDean Nelson 				if (ret == xpLocalPartid)
47845d9ca49SDean Nelson 					break;
4792c2b94f9SDean Nelson 
48045d9ca49SDean Nelson 				continue;
48145d9ca49SDean Nelson 			}
48245d9ca49SDean Nelson 
483a47d5dacSDean Nelson 			xpc_request_partition_activation(remote_rp,
48433ba3c77SDean Nelson 							 remote_rp_pa, nasid);
48545d9ca49SDean Nelson 		}
48645d9ca49SDean Nelson 	}
48745d9ca49SDean Nelson 
48845d9ca49SDean Nelson 	kfree(discovered_nasids);
48945d9ca49SDean Nelson 	kfree(remote_rp_base);
49045d9ca49SDean Nelson }
49145d9ca49SDean Nelson 
49245d9ca49SDean Nelson /*
49345d9ca49SDean Nelson  * Given a partid, get the nasids owned by that partition from the
49445d9ca49SDean Nelson  * remote partition's reserved page.
49545d9ca49SDean Nelson  */
49665c17b80SDean Nelson enum xp_retval
49764d032baSDean Nelson xpc_initiate_partid_to_nasids(short partid, void *nasid_mask)
49845d9ca49SDean Nelson {
49945d9ca49SDean Nelson 	struct xpc_partition *part;
500a812dcc3SDean Nelson 	unsigned long part_nasid_pa;
50145d9ca49SDean Nelson 
50245d9ca49SDean Nelson 	part = &xpc_partitions[partid];
5032c2b94f9SDean Nelson 	if (part->remote_rp_pa == 0)
50465c17b80SDean Nelson 		return xpPartitionDown;
50545d9ca49SDean Nelson 
50604de7418SDean Nelson 	memset(nasid_mask, 0, xpc_nasid_mask_nbytes);
50745d9ca49SDean Nelson 
508a812dcc3SDean Nelson 	part_nasid_pa = (unsigned long)XPC_RP_PART_NASIDS(part->remote_rp_pa);
50945d9ca49SDean Nelson 
510a812dcc3SDean Nelson 	return xp_remote_memcpy(xp_pa(nasid_mask), part_nasid_pa,
51104de7418SDean Nelson 				xpc_nasid_mask_nbytes);
51245d9ca49SDean Nelson }
513