blob: 65877bc5edaae9e67dd391b665de91670556fd20 [file] [log] [blame]
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
Dean Nelson45d9ca42008-04-22 14:46:56 -05006 * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07007 */
8
Dean Nelson89eb8eb2005-03-23 19:50:00 -07009/*
10 * Cross Partition Communication (XPC) partition support.
11 *
12 * This is the part of XPC that detects the presence/absence of
13 * other partitions. It provides a heartbeat and monitors the
14 * heartbeats of other partitions.
15 *
16 */
17
Dean Nelson261f3b42008-07-29 22:34:16 -070018#include <linux/device.h>
19#include <linux/hardirq.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050020#include "xpc.h"
Dean Nelson89eb8eb2005-03-23 19:50:00 -070021
Dean Nelson89eb8eb2005-03-23 19:50:00 -070022/* XPC is exiting flag */
23int xpc_exiting;
24
Dean Nelson4b38fcd2005-10-25 14:09:51 -050025/* this partition's reserved page pointers */
Dean Nelson89eb8eb2005-03-23 19:50:00 -070026struct xpc_rsvd_page *xpc_rsvd_page;
Dean Nelson04de7412008-07-29 22:34:14 -070027static unsigned long *xpc_part_nasids;
28unsigned long *xpc_mach_nasids;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070029
Dean Nelson04de7412008-07-29 22:34:14 -070030static int xpc_nasid_mask_nbytes; /* #of bytes in nasid mask */
31int xpc_nasid_mask_nlongs; /* #of longs in nasid mask */
Dean Nelson4b38fcd2005-10-25 14:09:51 -050032
Dean Nelsonbc63d382008-07-29 22:34:04 -070033struct xpc_partition *xpc_partitions;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070034
Dean Nelson89eb8eb2005-03-23 19:50:00 -070035/*
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050036 * Guarantee that the kmalloc'd memory is cacheline aligned.
37 */
Dean Nelson7682a4c2006-08-08 15:03:29 -050038void *
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050039xpc_kmalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
40{
41 /* see if kmalloc will give us cachline aligned memory by default */
42 *base = kmalloc(size, flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -050043 if (*base == NULL)
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050044 return NULL;
Dean Nelson2c2b94f2008-04-22 14:50:17 -050045
46 if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050047 return *base;
Dean Nelson2c2b94f2008-04-22 14:50:17 -050048
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050049 kfree(*base);
50
51 /* nope, we'll have to do it ourselves */
52 *base = kmalloc(size + L1_CACHE_BYTES, flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -050053 if (*base == NULL)
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050054 return NULL;
Dean Nelson2c2b94f2008-04-22 14:50:17 -050055
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050056 return (void *)L1_CACHE_ALIGN((u64)*base);
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050057}
58
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050059/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -070060 * Given a nasid, get the physical address of the partition's reserved page
61 * for that nasid. This function returns 0 on any error.
62 */
Dean Nelsona812dcc2008-07-29 22:34:16 -070063static unsigned long
Dean Nelson27929022005-10-25 14:11:53 -050064xpc_get_rsvd_page_pa(int nasid)
Dean Nelson89eb8eb2005-03-23 19:50:00 -070065{
Dean Nelson908787d2008-07-29 22:34:05 -070066 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070067 u64 cookie = 0;
Dean Nelsona812dcc2008-07-29 22:34:16 -070068 unsigned long rp_pa = nasid; /* seed with nasid */
Dean Nelson261f3b42008-07-29 22:34:16 -070069 size_t len = 0;
Dean Nelsona812dcc2008-07-29 22:34:16 -070070 size_t buf_len = 0;
71 void *buf = buf;
Dean Nelson27929022005-10-25 14:11:53 -050072 void *buf_base = NULL;
Robin Holta7665b02009-04-13 14:40:19 -070073 enum xp_retval (*get_partition_rsvd_page_pa)
74 (void *, u64 *, unsigned long *, size_t *) =
75 xpc_arch_ops.get_partition_rsvd_page_pa;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070076
Dean Nelson89eb8eb2005-03-23 19:50:00 -070077 while (1) {
78
Dean Nelson5b8669d2008-07-29 22:34:18 -070079 /* !!! rp_pa will need to be _gpa on UV.
80 * ??? So do we save it into the architecture specific parts
81 * ??? of the xpc_partition structure? Do we rename this
82 * ??? function or have two versions? Rename rp_pa for UV to
83 * ??? rp_gpa?
84 */
Robin Holta7665b02009-04-13 14:40:19 -070085 ret = get_partition_rsvd_page_pa(buf, &cookie, &rp_pa, &len);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070086
Dean Nelson261f3b42008-07-29 22:34:16 -070087 dev_dbg(xpc_part, "SAL returned with ret=%d, cookie=0x%016lx, "
88 "address=0x%016lx, len=0x%016lx\n", ret,
Dean Nelsona812dcc2008-07-29 22:34:16 -070089 (unsigned long)cookie, rp_pa, len);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070090
Dean Nelson261f3b42008-07-29 22:34:16 -070091 if (ret != xpNeedMoreInfo)
Dean Nelson89eb8eb2005-03-23 19:50:00 -070092 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070093
Dean Nelsonea57f802008-07-29 22:34:14 -070094 /* !!! L1_CACHE_ALIGN() is only a sn2-bte_copy requirement */
Dean Nelson27929022005-10-25 14:11:53 -050095 if (L1_CACHE_ALIGN(len) > buf_len) {
Jesper Juhlcbf283c2006-04-20 10:11:09 -070096 kfree(buf_base);
Dean Nelson27929022005-10-25 14:11:53 -050097 buf_len = L1_CACHE_ALIGN(len);
Dean Nelsona812dcc2008-07-29 22:34:16 -070098 buf = xpc_kmalloc_cacheline_aligned(buf_len, GFP_KERNEL,
99 &buf_base);
Dean Nelson27929022005-10-25 14:11:53 -0500100 if (buf_base == NULL) {
101 dev_err(xpc_part, "unable to kmalloc "
Dean Nelsona812dcc2008-07-29 22:34:16 -0700102 "len=0x%016lx\n", buf_len);
Dean Nelson261f3b42008-07-29 22:34:16 -0700103 ret = xpNoMemory;
Dean Nelson27929022005-10-25 14:11:53 -0500104 break;
105 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700106 }
107
Dean Nelsona812dcc2008-07-29 22:34:16 -0700108 ret = xp_remote_memcpy(xp_pa(buf), rp_pa, buf_len);
Dean Nelson908787d2008-07-29 22:34:05 -0700109 if (ret != xpSuccess) {
110 dev_dbg(xpc_part, "xp_remote_memcpy failed %d\n", ret);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700111 break;
112 }
113 }
114
Jesper Juhlcbf283c2006-04-20 10:11:09 -0700115 kfree(buf_base);
Dean Nelson27929022005-10-25 14:11:53 -0500116
Dean Nelson261f3b42008-07-29 22:34:16 -0700117 if (ret != xpSuccess)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700118 rp_pa = 0;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500119
Dean Nelsona812dcc2008-07-29 22:34:16 -0700120 dev_dbg(xpc_part, "reserved page at phys address 0x%016lx\n", rp_pa);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700121 return rp_pa;
122}
123
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700124/*
125 * Fill the partition reserved page with the information needed by
126 * other partitions to discover we are alive and establish initial
127 * communications.
128 */
Dean Nelson5b8669d2008-07-29 22:34:18 -0700129int
Dean Nelson94bd2702008-07-29 22:34:05 -0700130xpc_setup_rsvd_page(void)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700131{
Dean Nelson5b8669d2008-07-29 22:34:18 -0700132 int ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700133 struct xpc_rsvd_page *rp;
Dean Nelsona812dcc2008-07-29 22:34:16 -0700134 unsigned long rp_pa;
Dean Nelson81fe7882008-07-29 22:34:15 -0700135 unsigned long new_ts_jiffies;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700136
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700137 /* get the local reserved page's address */
138
Dean Nelson27929022005-10-25 14:11:53 -0500139 preempt_disable();
Dean Nelson261f3b42008-07-29 22:34:16 -0700140 rp_pa = xpc_get_rsvd_page_pa(xp_cpu_to_nasid(smp_processor_id()));
Dean Nelson27929022005-10-25 14:11:53 -0500141 preempt_enable();
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700142 if (rp_pa == 0) {
143 dev_err(xpc_part, "SAL failed to locate the reserved page\n");
Dean Nelson5b8669d2008-07-29 22:34:18 -0700144 return -ESRCH;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700145 }
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500146 rp = (struct xpc_rsvd_page *)__va(rp_pa);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700147
Dean Nelson94bd2702008-07-29 22:34:05 -0700148 if (rp->SAL_version < 3) {
149 /* SAL_versions < 3 had a SAL_partid defined as a u8 */
150 rp->SAL_partid &= 0xff;
151 }
Dean Nelson261f3b42008-07-29 22:34:16 -0700152 BUG_ON(rp->SAL_partid != xp_partition_id);
Dean Nelson94bd2702008-07-29 22:34:05 -0700153
154 if (rp->SAL_partid < 0 || rp->SAL_partid >= xp_max_npartitions) {
155 dev_err(xpc_part, "the reserved page's partid of %d is outside "
156 "supported range (< 0 || >= %d)\n", rp->SAL_partid,
157 xp_max_npartitions);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700158 return -EINVAL;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700159 }
160
161 rp->version = XPC_RP_VERSION;
Dean Nelson94bd2702008-07-29 22:34:05 -0700162 rp->max_npartitions = xp_max_npartitions;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700163
Dean Nelson4b38fcd2005-10-25 14:09:51 -0500164 /* establish the actual sizes of the nasid masks */
165 if (rp->SAL_version == 1) {
166 /* SAL_version 1 didn't set the nasids_size field */
Dean Nelson94bd2702008-07-29 22:34:05 -0700167 rp->SAL_nasids_size = 128;
Dean Nelson4b38fcd2005-10-25 14:09:51 -0500168 }
Dean Nelson04de7412008-07-29 22:34:14 -0700169 xpc_nasid_mask_nbytes = rp->SAL_nasids_size;
170 xpc_nasid_mask_nlongs = BITS_TO_LONGS(rp->SAL_nasids_size *
171 BITS_PER_BYTE);
Dean Nelson4b38fcd2005-10-25 14:09:51 -0500172
173 /* setup the pointers to the various items in the reserved page */
174 xpc_part_nasids = XPC_RP_PART_NASIDS(rp);
175 xpc_mach_nasids = XPC_RP_MACH_NASIDS(rp);
Dean Nelson94bd2702008-07-29 22:34:05 -0700176
Robin Holta7665b02009-04-13 14:40:19 -0700177 ret = xpc_arch_ops.setup_rsvd_page(rp);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700178 if (ret != 0)
179 return ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700180
181 /*
Dean Nelson94bd2702008-07-29 22:34:05 -0700182 * Set timestamp of when reserved page was setup by XPC.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700183 * This signifies to the remote partition that our reserved
184 * page is initialized.
185 */
Dean Nelson81fe7882008-07-29 22:34:15 -0700186 new_ts_jiffies = jiffies;
187 if (new_ts_jiffies == 0 || new_ts_jiffies == rp->ts_jiffies)
188 new_ts_jiffies++;
189 rp->ts_jiffies = new_ts_jiffies;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700190
Dean Nelson5b8669d2008-07-29 22:34:18 -0700191 xpc_rsvd_page = rp;
192 return 0;
193}
194
195void
196xpc_teardown_rsvd_page(void)
197{
198 /* a zero timestamp indicates our rsvd page is not initialized */
199 xpc_rsvd_page->ts_jiffies = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700200}
201
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700202/*
Dean Nelson4b38fcd2005-10-25 14:09:51 -0500203 * Get a copy of a portion of the remote partition's rsvd page.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700204 *
205 * remote_rp points to a buffer that is cacheline aligned for BTE copies and
Dean Nelson4b38fcd2005-10-25 14:09:51 -0500206 * is large enough to contain a copy of their reserved page header and
207 * part_nasids mask.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700208 */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700209enum xp_retval
Dean Nelson04de7412008-07-29 22:34:14 -0700210xpc_get_remote_rp(int nasid, unsigned long *discovered_nasids,
Dean Nelsona812dcc2008-07-29 22:34:16 -0700211 struct xpc_rsvd_page *remote_rp, unsigned long *remote_rp_pa)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700212{
Dean Nelson04de7412008-07-29 22:34:14 -0700213 int l;
Dean Nelson908787d2008-07-29 22:34:05 -0700214 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700215
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700216 /* get the reserved page's physical address */
217
Dean Nelson27929022005-10-25 14:11:53 -0500218 *remote_rp_pa = xpc_get_rsvd_page_pa(nasid);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500219 if (*remote_rp_pa == 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700220 return xpNoRsvdPageAddr;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700221
Dean Nelson4b38fcd2005-10-25 14:09:51 -0500222 /* pull over the reserved page header and part_nasids mask */
Dean Nelsona812dcc2008-07-29 22:34:16 -0700223 ret = xp_remote_memcpy(xp_pa(remote_rp), *remote_rp_pa,
Dean Nelson04de7412008-07-29 22:34:14 -0700224 XPC_RP_HEADER_SIZE + xpc_nasid_mask_nbytes);
Dean Nelson908787d2008-07-29 22:34:05 -0700225 if (ret != xpSuccess)
226 return ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700227
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700228 if (discovered_nasids != NULL) {
Dean Nelson04de7412008-07-29 22:34:14 -0700229 unsigned long *remote_part_nasids =
230 XPC_RP_PART_NASIDS(remote_rp);
Dean Nelson4b38fcd2005-10-25 14:09:51 -0500231
Dean Nelson04de7412008-07-29 22:34:14 -0700232 for (l = 0; l < xpc_nasid_mask_nlongs; l++)
233 discovered_nasids[l] |= remote_part_nasids[l];
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700234 }
235
Dean Nelson81fe7882008-07-29 22:34:15 -0700236 /* zero timestamp indicates the reserved page has not been setup */
237 if (remote_rp->ts_jiffies == 0)
Dean Nelson94bd2702008-07-29 22:34:05 -0700238 return xpRsvdPageNotSet;
239
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700240 if (XPC_VERSION_MAJOR(remote_rp->version) !=
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500241 XPC_VERSION_MAJOR(XPC_RP_VERSION)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700242 return xpBadVersion;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700243 }
244
Dean Nelsona47d5da2008-07-29 22:34:09 -0700245 /* check that both remote and local partids are valid for each side */
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700246 if (remote_rp->SAL_partid < 0 ||
247 remote_rp->SAL_partid >= xp_max_npartitions ||
Dean Nelson261f3b42008-07-29 22:34:16 -0700248 remote_rp->max_npartitions <= xp_partition_id) {
Dean Nelson94bd2702008-07-29 22:34:05 -0700249 return xpInvalidPartid;
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700250 }
251
Dean Nelson261f3b42008-07-29 22:34:16 -0700252 if (remote_rp->SAL_partid == xp_partition_id)
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700253 return xpLocalPartid;
Dean Nelson94bd2702008-07-29 22:34:05 -0700254
Dean Nelson65c17b82008-05-12 14:02:02 -0700255 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700256}
257
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700258/*
Dean Nelsona47d5da2008-07-29 22:34:09 -0700259 * See if the other side has responded to a partition deactivate request
260 * from us. Though we requested the remote partition to deactivate with regard
261 * to us, we really only need to wait for the other side to disengage from us.
Dean Nelsona607c382005-09-01 14:01:37 -0500262 */
263int
264xpc_partition_disengaged(struct xpc_partition *part)
265{
Dean Nelson64d032b2008-05-12 14:02:03 -0700266 short partid = XPC_PARTID(part);
Dean Nelsona607c382005-09-01 14:01:37 -0500267 int disengaged;
268
Robin Holta7665b02009-04-13 14:40:19 -0700269 disengaged = !xpc_arch_ops.partition_engaged(partid);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700270 if (part->disengage_timeout) {
Dean Nelsona607c382005-09-01 14:01:37 -0500271 if (!disengaged) {
Dean Nelsona47d5da2008-07-29 22:34:09 -0700272 if (time_is_after_jiffies(part->disengage_timeout)) {
Dean Nelsona607c382005-09-01 14:01:37 -0500273 /* timelimit hasn't been reached yet */
274 return 0;
275 }
276
277 /*
Dean Nelsona47d5da2008-07-29 22:34:09 -0700278 * Other side hasn't responded to our deactivate
Dean Nelsona607c382005-09-01 14:01:37 -0500279 * request in a timely fashion, so assume it's dead.
280 */
281
Dean Nelsona47d5da2008-07-29 22:34:09 -0700282 dev_info(xpc_part, "deactivate request to remote "
283 "partition %d timed out\n", partid);
284 xpc_disengage_timedout = 1;
Robin Holta7665b02009-04-13 14:40:19 -0700285 xpc_arch_ops.assume_partition_disengaged(partid);
Dean Nelsona607c382005-09-01 14:01:37 -0500286 disengaged = 1;
287 }
Dean Nelsona47d5da2008-07-29 22:34:09 -0700288 part->disengage_timeout = 0;
Dean Nelsona607c382005-09-01 14:01:37 -0500289
290 /* cancel the timer function, provided it's not us */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700291 if (!in_interrupt())
292 del_singleshot_timer_sync(&part->disengage_timer);
Dean Nelsona607c382005-09-01 14:01:37 -0500293
Dean Nelson83469b52008-07-29 22:34:18 -0700294 DBUG_ON(part->act_state != XPC_P_AS_DEACTIVATING &&
295 part->act_state != XPC_P_AS_INACTIVE);
296 if (part->act_state != XPC_P_AS_INACTIVE)
Dean Nelsona607c382005-09-01 14:01:37 -0500297 xpc_wakeup_channel_mgr(part);
Dean Nelsona607c382005-09-01 14:01:37 -0500298
Robin Holta7665b02009-04-13 14:40:19 -0700299 xpc_arch_ops.cancel_partition_deactivation_request(part);
Dean Nelsona607c382005-09-01 14:01:37 -0500300 }
301 return disengaged;
302}
303
Dean Nelsona607c382005-09-01 14:01:37 -0500304/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700305 * Mark specified partition as active.
306 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700307enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700308xpc_mark_partition_active(struct xpc_partition *part)
309{
310 unsigned long irq_flags;
Dean Nelson65c17b82008-05-12 14:02:02 -0700311 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700312
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700313 dev_dbg(xpc_part, "setting partition %d to ACTIVE\n", XPC_PARTID(part));
314
315 spin_lock_irqsave(&part->act_lock, irq_flags);
Dean Nelson83469b52008-07-29 22:34:18 -0700316 if (part->act_state == XPC_P_AS_ACTIVATING) {
317 part->act_state = XPC_P_AS_ACTIVE;
Dean Nelson65c17b82008-05-12 14:02:02 -0700318 ret = xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700319 } else {
Dean Nelson65c17b82008-05-12 14:02:02 -0700320 DBUG_ON(part->reason == xpSuccess);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700321 ret = part->reason;
322 }
323 spin_unlock_irqrestore(&part->act_lock, irq_flags);
324
325 return ret;
326}
327
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700328/*
Dean Nelsona47d5da2008-07-29 22:34:09 -0700329 * Start the process of deactivating the specified partition.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700330 */
331void
332xpc_deactivate_partition(const int line, struct xpc_partition *part,
Dean Nelson65c17b82008-05-12 14:02:02 -0700333 enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700334{
335 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700336
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700337 spin_lock_irqsave(&part->act_lock, irq_flags);
338
Dean Nelson83469b52008-07-29 22:34:18 -0700339 if (part->act_state == XPC_P_AS_INACTIVE) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700340 XPC_SET_REASON(part, reason, line);
341 spin_unlock_irqrestore(&part->act_lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700342 if (reason == xpReactivating) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700343 /* we interrupt ourselves to reactivate partition */
Robin Holta7665b02009-04-13 14:40:19 -0700344 xpc_arch_ops.request_partition_reactivation(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700345 }
346 return;
347 }
Dean Nelson83469b52008-07-29 22:34:18 -0700348 if (part->act_state == XPC_P_AS_DEACTIVATING) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700349 if ((part->reason == xpUnloading && reason != xpUnloading) ||
350 reason == xpReactivating) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700351 XPC_SET_REASON(part, reason, line);
352 }
353 spin_unlock_irqrestore(&part->act_lock, irq_flags);
354 return;
355 }
356
Dean Nelson83469b52008-07-29 22:34:18 -0700357 part->act_state = XPC_P_AS_DEACTIVATING;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700358 XPC_SET_REASON(part, reason, line);
359
360 spin_unlock_irqrestore(&part->act_lock, irq_flags);
361
Dean Nelsona47d5da2008-07-29 22:34:09 -0700362 /* ask remote partition to deactivate with regard to us */
Robin Holta7665b02009-04-13 14:40:19 -0700363 xpc_arch_ops.request_partition_deactivation(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700364
Dean Nelsona47d5da2008-07-29 22:34:09 -0700365 /* set a timelimit on the disengage phase of the deactivation request */
366 part->disengage_timeout = jiffies + (xpc_disengage_timelimit * HZ);
367 part->disengage_timer.expires = part->disengage_timeout;
368 add_timer(&part->disengage_timer);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700369
Dean Nelsone54af722005-10-25 14:07:43 -0500370 dev_dbg(xpc_part, "bringing partition %d down, reason = %d\n",
371 XPC_PARTID(part), reason);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700372
Dean Nelsona607c382005-09-01 14:01:37 -0500373 xpc_partition_going_down(part, reason);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700374}
375
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700376/*
Dean Nelsona607c382005-09-01 14:01:37 -0500377 * Mark specified partition as inactive.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700378 */
379void
380xpc_mark_partition_inactive(struct xpc_partition *part)
381{
382 unsigned long irq_flags;
383
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700384 dev_dbg(xpc_part, "setting partition %d to INACTIVE\n",
385 XPC_PARTID(part));
386
387 spin_lock_irqsave(&part->act_lock, irq_flags);
Dean Nelson83469b52008-07-29 22:34:18 -0700388 part->act_state = XPC_P_AS_INACTIVE;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700389 spin_unlock_irqrestore(&part->act_lock, irq_flags);
390 part->remote_rp_pa = 0;
391}
392
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700393/*
394 * SAL has provided a partition and machine mask. The partition mask
395 * contains a bit for each even nasid in our partition. The machine
396 * mask contains a bit for each even nasid in the entire machine.
397 *
398 * Using those two bit arrays, we can determine which nasids are
399 * known in the machine. Each should also have a reserved page
400 * initialized if they are available for partitioning.
401 */
402void
403xpc_discovery(void)
404{
405 void *remote_rp_base;
406 struct xpc_rsvd_page *remote_rp;
Dean Nelsona812dcc2008-07-29 22:34:16 -0700407 unsigned long remote_rp_pa;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700408 int region;
Dean Nelson4b38fcd2005-10-25 14:09:51 -0500409 int region_size;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700410 int max_regions;
411 int nasid;
412 struct xpc_rsvd_page *rp;
Dean Nelson04de7412008-07-29 22:34:14 -0700413 unsigned long *discovered_nasids;
Dean Nelson65c17b82008-05-12 14:02:02 -0700414 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700415
Dean Nelson4b38fcd2005-10-25 14:09:51 -0500416 remote_rp = xpc_kmalloc_cacheline_aligned(XPC_RP_HEADER_SIZE +
Dean Nelson04de7412008-07-29 22:34:14 -0700417 xpc_nasid_mask_nbytes,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500418 GFP_KERNEL, &remote_rp_base);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500419 if (remote_rp == NULL)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700420 return;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500421
Dean Nelson04de7412008-07-29 22:34:14 -0700422 discovered_nasids = kzalloc(sizeof(long) * xpc_nasid_mask_nlongs,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500423 GFP_KERNEL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700424 if (discovered_nasids == NULL) {
425 kfree(remote_rp_base);
426 return;
427 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700428
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500429 rp = (struct xpc_rsvd_page *)xpc_rsvd_page;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700430
431 /*
432 * The term 'region' in this context refers to the minimum number of
433 * nodes that can comprise an access protection grouping. The access
434 * protection is in regards to memory, IOI and IPI.
435 */
Dean Nelson4b38fcd2005-10-25 14:09:51 -0500436 max_regions = 64;
Dean Nelson261f3b42008-07-29 22:34:16 -0700437 region_size = xp_region_size;
Dean Nelson4b38fcd2005-10-25 14:09:51 -0500438
439 switch (region_size) {
440 case 128:
441 max_regions *= 2;
442 case 64:
443 max_regions *= 2;
444 case 32:
445 max_regions *= 2;
446 region_size = 16;
447 DBUG_ON(!is_shub2());
448 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700449
450 for (region = 0; region < max_regions; region++) {
451
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500452 if (xpc_exiting)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700453 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700454
455 dev_dbg(xpc_part, "searching region %d\n", region);
456
Dean Nelson4b38fcd2005-10-25 14:09:51 -0500457 for (nasid = (region * region_size * 2);
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500458 nasid < ((region + 1) * region_size * 2); nasid += 2) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700459
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500460 if (xpc_exiting)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700461 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700462
463 dev_dbg(xpc_part, "checking nasid %d\n", nasid);
464
Dean Nelson04de7412008-07-29 22:34:14 -0700465 if (test_bit(nasid / 2, xpc_part_nasids)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700466 dev_dbg(xpc_part, "PROM indicates Nasid %d is "
467 "part of the local partition; skipping "
468 "region\n", nasid);
469 break;
470 }
471
Dean Nelson04de7412008-07-29 22:34:14 -0700472 if (!(test_bit(nasid / 2, xpc_mach_nasids))) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700473 dev_dbg(xpc_part, "PROM indicates Nasid %d was "
474 "not on Numa-Link network at reset\n",
475 nasid);
476 continue;
477 }
478
Dean Nelson04de7412008-07-29 22:34:14 -0700479 if (test_bit(nasid / 2, discovered_nasids)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700480 dev_dbg(xpc_part, "Nasid %d is part of a "
481 "partition which was previously "
482 "discovered\n", nasid);
483 continue;
484 }
485
Dean Nelson33ba3c72008-07-29 22:34:07 -0700486 /* pull over the rsvd page header & part_nasids mask */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700487
488 ret = xpc_get_remote_rp(nasid, discovered_nasids,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500489 remote_rp, &remote_rp_pa);
Dean Nelson65c17b82008-05-12 14:02:02 -0700490 if (ret != xpSuccess) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700491 dev_dbg(xpc_part, "unable to get reserved page "
492 "from nasid %d, reason=%d\n", nasid,
493 ret);
494
Dean Nelson65c17b82008-05-12 14:02:02 -0700495 if (ret == xpLocalPartid)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700496 break;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500497
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700498 continue;
499 }
500
Robin Holta7665b02009-04-13 14:40:19 -0700501 xpc_arch_ops.request_partition_activation(remote_rp,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700502 remote_rp_pa, nasid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700503 }
504 }
505
506 kfree(discovered_nasids);
507 kfree(remote_rp_base);
508}
509
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700510/*
511 * Given a partid, get the nasids owned by that partition from the
Dean Nelson3a7d5552005-04-04 13:14:00 -0700512 * remote partition's reserved page.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700513 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700514enum xp_retval
Dean Nelson64d032b2008-05-12 14:02:03 -0700515xpc_initiate_partid_to_nasids(short partid, void *nasid_mask)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700516{
517 struct xpc_partition *part;
Dean Nelsona812dcc2008-07-29 22:34:16 -0700518 unsigned long part_nasid_pa;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700519
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700520 part = &xpc_partitions[partid];
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500521 if (part->remote_rp_pa == 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700522 return xpPartitionDown;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700523
Dean Nelson04de7412008-07-29 22:34:14 -0700524 memset(nasid_mask, 0, xpc_nasid_mask_nbytes);
Dean Nelson4b38fcd2005-10-25 14:09:51 -0500525
Dean Nelsona812dcc2008-07-29 22:34:16 -0700526 part_nasid_pa = (unsigned long)XPC_RP_PART_NASIDS(part->remote_rp_pa);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700527
Dean Nelsona812dcc2008-07-29 22:34:16 -0700528 return xp_remote_memcpy(xp_pa(nasid_mask), part_nasid_pa,
Dean Nelson04de7412008-07-29 22:34:14 -0700529 xpc_nasid_mask_nbytes);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700530}