sgi-xp: support runtime selection of xp_max_npartitions
[safe/jmp/linux-2.6] / drivers / misc / sgi-xp / xpc_partition.c
1 /*
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  *
6  * Copyright (c) 2004-2008 Silicon Graphics, Inc.  All Rights Reserved.
7  */
8
9 /*
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
18 #include <linux/kernel.h>
19 #include <linux/sysctl.h>
20 #include <linux/cache.h>
21 #include <linux/mmzone.h>
22 #include <linux/nodemask.h>
23 #include <asm/uncached.h>
24 #include <asm/sn/bte.h>
25 #include <asm/sn/intr.h>
26 #include <asm/sn/sn_sal.h>
27 #include <asm/sn/nodepda.h>
28 #include <asm/sn/addrs.h>
29 #include "xpc.h"
30
31 /* XPC is exiting flag */
32 int xpc_exiting;
33
34 /* SH_IPI_ACCESS shub register value on startup */
35 static u64 xpc_sh1_IPI_access;
36 static u64 xpc_sh2_IPI_access0;
37 static u64 xpc_sh2_IPI_access1;
38 static u64 xpc_sh2_IPI_access2;
39 static u64 xpc_sh2_IPI_access3;
40
41 /* original protection values for each node */
42 u64 xpc_prot_vec[MAX_NUMNODES];
43
44 /* this partition's reserved page pointers */
45 struct xpc_rsvd_page *xpc_rsvd_page;
46 static u64 *xpc_part_nasids;
47 static u64 *xpc_mach_nasids;
48 struct xpc_vars *xpc_vars;
49 struct xpc_vars_part *xpc_vars_part;
50
51 static int xp_nasid_mask_bytes; /* actual size in bytes of nasid mask */
52 static int xp_nasid_mask_words; /* actual size in words of nasid mask */
53
54 struct xpc_partition *xpc_partitions;
55
56 /*
57  * Generic buffer used to store a local copy of portions of a remote
58  * partition's reserved page (either its header and part_nasids mask,
59  * or its vars).
60  */
61 char *xpc_remote_copy_buffer;
62 void *xpc_remote_copy_buffer_base;
63
64 /*
65  * Guarantee that the kmalloc'd memory is cacheline aligned.
66  */
67 void *
68 xpc_kmalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
69 {
70         /* see if kmalloc will give us cachline aligned memory by default */
71         *base = kmalloc(size, flags);
72         if (*base == NULL)
73                 return NULL;
74
75         if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
76                 return *base;
77
78         kfree(*base);
79
80         /* nope, we'll have to do it ourselves */
81         *base = kmalloc(size + L1_CACHE_BYTES, flags);
82         if (*base == NULL)
83                 return NULL;
84
85         return (void *)L1_CACHE_ALIGN((u64)*base);
86 }
87
88 /*
89  * Given a nasid, get the physical address of the  partition's reserved page
90  * for that nasid. This function returns 0 on any error.
91  */
92 static u64
93 xpc_get_rsvd_page_pa(int nasid)
94 {
95         bte_result_t bte_res;
96         s64 status;
97         u64 cookie = 0;
98         u64 rp_pa = nasid;      /* seed with nasid */
99         u64 len = 0;
100         u64 buf = buf;
101         u64 buf_len = 0;
102         void *buf_base = NULL;
103
104         while (1) {
105
106                 status = sn_partition_reserved_page_pa(buf, &cookie, &rp_pa,
107                                                        &len);
108
109                 dev_dbg(xpc_part, "SAL returned with status=%li, cookie="
110                         "0x%016lx, address=0x%016lx, len=0x%016lx\n",
111                         status, cookie, rp_pa, len);
112
113                 if (status != SALRET_MORE_PASSES)
114                         break;
115
116                 if (L1_CACHE_ALIGN(len) > buf_len) {
117                         kfree(buf_base);
118                         buf_len = L1_CACHE_ALIGN(len);
119                         buf = (u64)xpc_kmalloc_cacheline_aligned(buf_len,
120                                                                  GFP_KERNEL,
121                                                                  &buf_base);
122                         if (buf_base == NULL) {
123                                 dev_err(xpc_part, "unable to kmalloc "
124                                         "len=0x%016lx\n", buf_len);
125                                 status = SALRET_ERROR;
126                                 break;
127                         }
128                 }
129
130                 bte_res = xp_bte_copy(rp_pa, buf, buf_len,
131                                       (BTE_NOTIFY | BTE_WACQUIRE), NULL);
132                 if (bte_res != BTE_SUCCESS) {
133                         dev_dbg(xpc_part, "xp_bte_copy failed %i\n", bte_res);
134                         status = SALRET_ERROR;
135                         break;
136                 }
137         }
138
139         kfree(buf_base);
140
141         if (status != SALRET_OK)
142                 rp_pa = 0;
143
144         dev_dbg(xpc_part, "reserved page at phys address 0x%016lx\n", rp_pa);
145         return rp_pa;
146 }
147
148 /*
149  * Fill the partition reserved page with the information needed by
150  * other partitions to discover we are alive and establish initial
151  * communications.
152  */
153 struct xpc_rsvd_page *
154 xpc_rsvd_page_init(void)
155 {
156         struct xpc_rsvd_page *rp;
157         AMO_t *amos_page;
158         u64 rp_pa, nasid_array = 0;
159         int i, ret;
160
161         /* get the local reserved page's address */
162
163         preempt_disable();
164         rp_pa = xpc_get_rsvd_page_pa(cpuid_to_nasid(smp_processor_id()));
165         preempt_enable();
166         if (rp_pa == 0) {
167                 dev_err(xpc_part, "SAL failed to locate the reserved page\n");
168                 return NULL;
169         }
170         rp = (struct xpc_rsvd_page *)__va(rp_pa);
171
172         if (rp->partid != sn_partition_id) {
173                 dev_err(xpc_part, "the reserved page's partid of %d should be "
174                         "%d\n", rp->partid, sn_partition_id);
175                 return NULL;
176         }
177
178         rp->version = XPC_RP_VERSION;
179
180         /* establish the actual sizes of the nasid masks */
181         if (rp->SAL_version == 1) {
182                 /* SAL_version 1 didn't set the nasids_size field */
183                 rp->nasids_size = 128;
184         }
185         xp_nasid_mask_bytes = rp->nasids_size;
186         xp_nasid_mask_words = xp_nasid_mask_bytes / 8;
187
188         /* setup the pointers to the various items in the reserved page */
189         xpc_part_nasids = XPC_RP_PART_NASIDS(rp);
190         xpc_mach_nasids = XPC_RP_MACH_NASIDS(rp);
191         xpc_vars = XPC_RP_VARS(rp);
192         xpc_vars_part = XPC_RP_VARS_PART(rp);
193
194         /*
195          * Before clearing xpc_vars, see if a page of AMOs had been previously
196          * allocated. If not we'll need to allocate one and set permissions
197          * so that cross-partition AMOs are allowed.
198          *
199          * The allocated AMO page needs MCA reporting to remain disabled after
200          * XPC has unloaded.  To make this work, we keep a copy of the pointer
201          * to this page (i.e., amos_page) in the struct xpc_vars structure,
202          * which is pointed to by the reserved page, and re-use that saved copy
203          * on subsequent loads of XPC. This AMO page is never freed, and its
204          * memory protections are never restricted.
205          */
206         amos_page = xpc_vars->amos_page;
207         if (amos_page == NULL) {
208                 amos_page = (AMO_t *)TO_AMO(uncached_alloc_page(0, 1));
209                 if (amos_page == NULL) {
210                         dev_err(xpc_part, "can't allocate page of AMOs\n");
211                         return NULL;
212                 }
213
214                 /*
215                  * Open up AMO-R/W to cpu.  This is done for Shub 1.1 systems
216                  * when xpc_allow_IPI_ops() is called via xpc_hb_init().
217                  */
218                 if (!enable_shub_wars_1_1()) {
219                         ret = sn_change_memprotect(ia64_tpa((u64)amos_page),
220                                                    PAGE_SIZE,
221                                                    SN_MEMPROT_ACCESS_CLASS_1,
222                                                    &nasid_array);
223                         if (ret != 0) {
224                                 dev_err(xpc_part, "can't change memory "
225                                         "protections\n");
226                                 uncached_free_page(__IA64_UNCACHED_OFFSET |
227                                                    TO_PHYS((u64)amos_page), 1);
228                                 return NULL;
229                         }
230                 }
231         } else if (!IS_AMO_ADDRESS((u64)amos_page)) {
232                 /*
233                  * EFI's XPBOOT can also set amos_page in the reserved page,
234                  * but it happens to leave it as an uncached physical address
235                  * and we need it to be an uncached virtual, so we'll have to
236                  * convert it.
237                  */
238                 if (!IS_AMO_PHYS_ADDRESS((u64)amos_page)) {
239                         dev_err(xpc_part, "previously used amos_page address "
240                                 "is bad = 0x%p\n", (void *)amos_page);
241                         return NULL;
242                 }
243                 amos_page = (AMO_t *)TO_AMO((u64)amos_page);
244         }
245
246         /* clear xpc_vars */
247         memset(xpc_vars, 0, sizeof(struct xpc_vars));
248
249         xpc_vars->version = XPC_V_VERSION;
250         xpc_vars->act_nasid = cpuid_to_nasid(0);
251         xpc_vars->act_phys_cpuid = cpu_physical_id(0);
252         xpc_vars->vars_part_pa = __pa(xpc_vars_part);
253         xpc_vars->amos_page_pa = ia64_tpa((u64)amos_page);
254         xpc_vars->amos_page = amos_page;        /* save for next load of XPC */
255
256         /* clear xpc_vars_part */
257         memset((u64 *)xpc_vars_part, 0, sizeof(struct xpc_vars_part) *
258                xp_max_npartitions);
259
260         /* initialize the activate IRQ related AMO variables */
261         for (i = 0; i < xp_nasid_mask_words; i++)
262                 (void)xpc_IPI_init(XPC_ACTIVATE_IRQ_AMOS + i);
263
264         /* initialize the engaged remote partitions related AMO variables */
265         (void)xpc_IPI_init(XPC_ENGAGED_PARTITIONS_AMO);
266         (void)xpc_IPI_init(XPC_DISENGAGE_REQUEST_AMO);
267
268         /* timestamp of when reserved page was setup by XPC */
269         rp->stamp = CURRENT_TIME;
270
271         /*
272          * This signifies to the remote partition that our reserved
273          * page is initialized.
274          */
275         rp->vars_pa = __pa(xpc_vars);
276
277         return rp;
278 }
279
280 /*
281  * Change protections to allow IPI operations (and AMO operations on
282  * Shub 1.1 systems).
283  */
284 void
285 xpc_allow_IPI_ops(void)
286 {
287         int node;
288         int nasid;
289
290         /* >>> Change SH_IPI_ACCESS code to use SAL call once it is available */
291
292         if (is_shub2()) {
293                 xpc_sh2_IPI_access0 =
294                     (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS0));
295                 xpc_sh2_IPI_access1 =
296                     (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS1));
297                 xpc_sh2_IPI_access2 =
298                     (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS2));
299                 xpc_sh2_IPI_access3 =
300                     (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS3));
301
302                 for_each_online_node(node) {
303                         nasid = cnodeid_to_nasid(node);
304                         HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
305                               -1UL);
306                         HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
307                               -1UL);
308                         HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
309                               -1UL);
310                         HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
311                               -1UL);
312                 }
313
314         } else {
315                 xpc_sh1_IPI_access =
316                     (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH1_IPI_ACCESS));
317
318                 for_each_online_node(node) {
319                         nasid = cnodeid_to_nasid(node);
320                         HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
321                               -1UL);
322
323                         /*
324                          * Since the BIST collides with memory operations on
325                          * SHUB 1.1 sn_change_memprotect() cannot be used.
326                          */
327                         if (enable_shub_wars_1_1()) {
328                                 /* open up everything */
329                                 xpc_prot_vec[node] = (u64)HUB_L((u64 *)
330                                                                 GLOBAL_MMR_ADDR
331                                                                 (nasid,
332                                                   SH1_MD_DQLP_MMR_DIR_PRIVEC0));
333                                 HUB_S((u64 *)
334                                       GLOBAL_MMR_ADDR(nasid,
335                                                    SH1_MD_DQLP_MMR_DIR_PRIVEC0),
336                                       -1UL);
337                                 HUB_S((u64 *)
338                                       GLOBAL_MMR_ADDR(nasid,
339                                                    SH1_MD_DQRP_MMR_DIR_PRIVEC0),
340                                       -1UL);
341                         }
342                 }
343         }
344 }
345
346 /*
347  * Restrict protections to disallow IPI operations (and AMO operations on
348  * Shub 1.1 systems).
349  */
350 void
351 xpc_restrict_IPI_ops(void)
352 {
353         int node;
354         int nasid;
355
356         /* >>> Change SH_IPI_ACCESS code to use SAL call once it is available */
357
358         if (is_shub2()) {
359
360                 for_each_online_node(node) {
361                         nasid = cnodeid_to_nasid(node);
362                         HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
363                               xpc_sh2_IPI_access0);
364                         HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
365                               xpc_sh2_IPI_access1);
366                         HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
367                               xpc_sh2_IPI_access2);
368                         HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
369                               xpc_sh2_IPI_access3);
370                 }
371
372         } else {
373
374                 for_each_online_node(node) {
375                         nasid = cnodeid_to_nasid(node);
376                         HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
377                               xpc_sh1_IPI_access);
378
379                         if (enable_shub_wars_1_1()) {
380                                 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
381                                                    SH1_MD_DQLP_MMR_DIR_PRIVEC0),
382                                       xpc_prot_vec[node]);
383                                 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
384                                                    SH1_MD_DQRP_MMR_DIR_PRIVEC0),
385                                       xpc_prot_vec[node]);
386                         }
387                 }
388         }
389 }
390
391 /*
392  * At periodic intervals, scan through all active partitions and ensure
393  * their heartbeat is still active.  If not, the partition is deactivated.
394  */
395 void
396 xpc_check_remote_hb(void)
397 {
398         struct xpc_vars *remote_vars;
399         struct xpc_partition *part;
400         short partid;
401         bte_result_t bres;
402
403         remote_vars = (struct xpc_vars *)xpc_remote_copy_buffer;
404
405         for (partid = 0; partid < xp_max_npartitions; partid++) {
406
407                 if (xpc_exiting)
408                         break;
409
410                 if (partid == sn_partition_id)
411                         continue;
412
413                 part = &xpc_partitions[partid];
414
415                 if (part->act_state == XPC_P_INACTIVE ||
416                     part->act_state == XPC_P_DEACTIVATING) {
417                         continue;
418                 }
419
420                 /* pull the remote_hb cache line */
421                 bres = xp_bte_copy(part->remote_vars_pa,
422                                    (u64)remote_vars,
423                                    XPC_RP_VARS_SIZE,
424                                    (BTE_NOTIFY | BTE_WACQUIRE), NULL);
425                 if (bres != BTE_SUCCESS) {
426                         XPC_DEACTIVATE_PARTITION(part,
427                                                  xpc_map_bte_errors(bres));
428                         continue;
429                 }
430
431                 dev_dbg(xpc_part, "partid = %d, heartbeat = %ld, last_heartbeat"
432                         " = %ld, heartbeat_offline = %ld, HB_mask = 0x%lx\n",
433                         partid, remote_vars->heartbeat, part->last_heartbeat,
434                         remote_vars->heartbeat_offline,
435                         remote_vars->heartbeating_to_mask);
436
437                 if (((remote_vars->heartbeat == part->last_heartbeat) &&
438                      (remote_vars->heartbeat_offline == 0)) ||
439                     !xpc_hb_allowed(sn_partition_id, remote_vars)) {
440
441                         XPC_DEACTIVATE_PARTITION(part, xpNoHeartbeat);
442                         continue;
443                 }
444
445                 part->last_heartbeat = remote_vars->heartbeat;
446         }
447 }
448
449 /*
450  * Get a copy of a portion of the remote partition's rsvd page.
451  *
452  * remote_rp points to a buffer that is cacheline aligned for BTE copies and
453  * is large enough to contain a copy of their reserved page header and
454  * part_nasids mask.
455  */
456 static enum xp_retval
457 xpc_get_remote_rp(int nasid, u64 *discovered_nasids,
458                   struct xpc_rsvd_page *remote_rp, u64 *remote_rp_pa)
459 {
460         int bres, i;
461
462         /* get the reserved page's physical address */
463
464         *remote_rp_pa = xpc_get_rsvd_page_pa(nasid);
465         if (*remote_rp_pa == 0)
466                 return xpNoRsvdPageAddr;
467
468         /* pull over the reserved page header and part_nasids mask */
469         bres = xp_bte_copy(*remote_rp_pa, (u64)remote_rp,
470                            XPC_RP_HEADER_SIZE + xp_nasid_mask_bytes,
471                            (BTE_NOTIFY | BTE_WACQUIRE), NULL);
472         if (bres != BTE_SUCCESS)
473                 return xpc_map_bte_errors(bres);
474
475         if (discovered_nasids != NULL) {
476                 u64 *remote_part_nasids = XPC_RP_PART_NASIDS(remote_rp);
477
478                 for (i = 0; i < xp_nasid_mask_words; i++)
479                         discovered_nasids[i] |= remote_part_nasids[i];
480         }
481
482         /* check that the partid is for another partition */
483
484         if (remote_rp->partid < 0 || remote_rp->partid >= xp_max_npartitions)
485                 return xpInvalidPartid;
486
487         if (remote_rp->partid == sn_partition_id)
488                 return xpLocalPartid;
489
490         if (XPC_VERSION_MAJOR(remote_rp->version) !=
491             XPC_VERSION_MAJOR(XPC_RP_VERSION)) {
492                 return xpBadVersion;
493         }
494
495         return xpSuccess;
496 }
497
498 /*
499  * Get a copy of the remote partition's XPC variables from the reserved page.
500  *
501  * remote_vars points to a buffer that is cacheline aligned for BTE copies and
502  * assumed to be of size XPC_RP_VARS_SIZE.
503  */
504 static enum xp_retval
505 xpc_get_remote_vars(u64 remote_vars_pa, struct xpc_vars *remote_vars)
506 {
507         int bres;
508
509         if (remote_vars_pa == 0)
510                 return xpVarsNotSet;
511
512         /* pull over the cross partition variables */
513         bres = xp_bte_copy(remote_vars_pa, (u64)remote_vars, XPC_RP_VARS_SIZE,
514                            (BTE_NOTIFY | BTE_WACQUIRE), NULL);
515         if (bres != BTE_SUCCESS)
516                 return xpc_map_bte_errors(bres);
517
518         if (XPC_VERSION_MAJOR(remote_vars->version) !=
519             XPC_VERSION_MAJOR(XPC_V_VERSION)) {
520                 return xpBadVersion;
521         }
522
523         return xpSuccess;
524 }
525
526 /*
527  * Update the remote partition's info.
528  */
529 static void
530 xpc_update_partition_info(struct xpc_partition *part, u8 remote_rp_version,
531                           struct timespec *remote_rp_stamp, u64 remote_rp_pa,
532                           u64 remote_vars_pa, struct xpc_vars *remote_vars)
533 {
534         part->remote_rp_version = remote_rp_version;
535         dev_dbg(xpc_part, "  remote_rp_version = 0x%016x\n",
536                 part->remote_rp_version);
537
538         part->remote_rp_stamp = *remote_rp_stamp;
539         dev_dbg(xpc_part, "  remote_rp_stamp (tv_sec = 0x%lx tv_nsec = 0x%lx\n",
540                 part->remote_rp_stamp.tv_sec, part->remote_rp_stamp.tv_nsec);
541
542         part->remote_rp_pa = remote_rp_pa;
543         dev_dbg(xpc_part, "  remote_rp_pa = 0x%016lx\n", part->remote_rp_pa);
544
545         part->remote_vars_pa = remote_vars_pa;
546         dev_dbg(xpc_part, "  remote_vars_pa = 0x%016lx\n",
547                 part->remote_vars_pa);
548
549         part->last_heartbeat = remote_vars->heartbeat;
550         dev_dbg(xpc_part, "  last_heartbeat = 0x%016lx\n",
551                 part->last_heartbeat);
552
553         part->remote_vars_part_pa = remote_vars->vars_part_pa;
554         dev_dbg(xpc_part, "  remote_vars_part_pa = 0x%016lx\n",
555                 part->remote_vars_part_pa);
556
557         part->remote_act_nasid = remote_vars->act_nasid;
558         dev_dbg(xpc_part, "  remote_act_nasid = 0x%x\n",
559                 part->remote_act_nasid);
560
561         part->remote_act_phys_cpuid = remote_vars->act_phys_cpuid;
562         dev_dbg(xpc_part, "  remote_act_phys_cpuid = 0x%x\n",
563                 part->remote_act_phys_cpuid);
564
565         part->remote_amos_page_pa = remote_vars->amos_page_pa;
566         dev_dbg(xpc_part, "  remote_amos_page_pa = 0x%lx\n",
567                 part->remote_amos_page_pa);
568
569         part->remote_vars_version = remote_vars->version;
570         dev_dbg(xpc_part, "  remote_vars_version = 0x%x\n",
571                 part->remote_vars_version);
572 }
573
574 /*
575  * Prior code has determined the nasid which generated an IPI.  Inspect
576  * that nasid to determine if its partition needs to be activated or
577  * deactivated.
578  *
579  * A partition is consider "awaiting activation" if our partition
580  * flags indicate it is not active and it has a heartbeat.  A
581  * partition is considered "awaiting deactivation" if our partition
582  * flags indicate it is active but it has no heartbeat or it is not
583  * sending its heartbeat to us.
584  *
585  * To determine the heartbeat, the remote nasid must have a properly
586  * initialized reserved page.
587  */
588 static void
589 xpc_identify_act_IRQ_req(int nasid)
590 {
591         struct xpc_rsvd_page *remote_rp;
592         struct xpc_vars *remote_vars;
593         u64 remote_rp_pa;
594         u64 remote_vars_pa;
595         int remote_rp_version;
596         int reactivate = 0;
597         int stamp_diff;
598         struct timespec remote_rp_stamp = { 0, 0 };
599         short partid;
600         struct xpc_partition *part;
601         enum xp_retval ret;
602
603         /* pull over the reserved page structure */
604
605         remote_rp = (struct xpc_rsvd_page *)xpc_remote_copy_buffer;
606
607         ret = xpc_get_remote_rp(nasid, NULL, remote_rp, &remote_rp_pa);
608         if (ret != xpSuccess) {
609                 dev_warn(xpc_part, "unable to get reserved page from nasid %d, "
610                          "which sent interrupt, reason=%d\n", nasid, ret);
611                 return;
612         }
613
614         remote_vars_pa = remote_rp->vars_pa;
615         remote_rp_version = remote_rp->version;
616         if (XPC_SUPPORTS_RP_STAMP(remote_rp_version))
617                 remote_rp_stamp = remote_rp->stamp;
618
619         partid = remote_rp->partid;
620         part = &xpc_partitions[partid];
621
622         /* pull over the cross partition variables */
623
624         remote_vars = (struct xpc_vars *)xpc_remote_copy_buffer;
625
626         ret = xpc_get_remote_vars(remote_vars_pa, remote_vars);
627         if (ret != xpSuccess) {
628
629                 dev_warn(xpc_part, "unable to get XPC variables from nasid %d, "
630                          "which sent interrupt, reason=%d\n", nasid, ret);
631
632                 XPC_DEACTIVATE_PARTITION(part, ret);
633                 return;
634         }
635
636         part->act_IRQ_rcvd++;
637
638         dev_dbg(xpc_part, "partid for nasid %d is %d; IRQs = %d; HB = "
639                 "%ld:0x%lx\n", (int)nasid, (int)partid, part->act_IRQ_rcvd,
640                 remote_vars->heartbeat, remote_vars->heartbeating_to_mask);
641
642         if (xpc_partition_disengaged(part) &&
643             part->act_state == XPC_P_INACTIVE) {
644
645                 xpc_update_partition_info(part, remote_rp_version,
646                                           &remote_rp_stamp, remote_rp_pa,
647                                           remote_vars_pa, remote_vars);
648
649                 if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) {
650                         if (xpc_partition_disengage_requested(1UL << partid)) {
651                                 /*
652                                  * Other side is waiting on us to disengage,
653                                  * even though we already have.
654                                  */
655                                 return;
656                         }
657                 } else {
658                         /* other side doesn't support disengage requests */
659                         xpc_clear_partition_disengage_request(1UL << partid);
660                 }
661
662                 xpc_activate_partition(part);
663                 return;
664         }
665
666         DBUG_ON(part->remote_rp_version == 0);
667         DBUG_ON(part->remote_vars_version == 0);
668
669         if (!XPC_SUPPORTS_RP_STAMP(part->remote_rp_version)) {
670                 DBUG_ON(XPC_SUPPORTS_DISENGAGE_REQUEST(part->
671                                                        remote_vars_version));
672
673                 if (!XPC_SUPPORTS_RP_STAMP(remote_rp_version)) {
674                         DBUG_ON(XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->
675                                                                version));
676                         /* see if the other side rebooted */
677                         if (part->remote_amos_page_pa ==
678                             remote_vars->amos_page_pa &&
679                             xpc_hb_allowed(sn_partition_id, remote_vars)) {
680                                 /* doesn't look that way, so ignore the IPI */
681                                 return;
682                         }
683                 }
684
685                 /*
686                  * Other side rebooted and previous XPC didn't support the
687                  * disengage request, so we don't need to do anything special.
688                  */
689
690                 xpc_update_partition_info(part, remote_rp_version,
691                                           &remote_rp_stamp, remote_rp_pa,
692                                           remote_vars_pa, remote_vars);
693                 part->reactivate_nasid = nasid;
694                 XPC_DEACTIVATE_PARTITION(part, xpReactivating);
695                 return;
696         }
697
698         DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version));
699
700         if (!XPC_SUPPORTS_RP_STAMP(remote_rp_version)) {
701                 DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->version));
702
703                 /*
704                  * Other side rebooted and previous XPC did support the
705                  * disengage request, but the new one doesn't.
706                  */
707
708                 xpc_clear_partition_engaged(1UL << partid);
709                 xpc_clear_partition_disengage_request(1UL << partid);
710
711                 xpc_update_partition_info(part, remote_rp_version,
712                                           &remote_rp_stamp, remote_rp_pa,
713                                           remote_vars_pa, remote_vars);
714                 reactivate = 1;
715
716         } else {
717                 DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->version));
718
719                 stamp_diff = xpc_compare_stamps(&part->remote_rp_stamp,
720                                                 &remote_rp_stamp);
721                 if (stamp_diff != 0) {
722                         DBUG_ON(stamp_diff >= 0);
723
724                         /*
725                          * Other side rebooted and the previous XPC did support
726                          * the disengage request, as does the new one.
727                          */
728
729                         DBUG_ON(xpc_partition_engaged(1UL << partid));
730                         DBUG_ON(xpc_partition_disengage_requested(1UL <<
731                                                                   partid));
732
733                         xpc_update_partition_info(part, remote_rp_version,
734                                                   &remote_rp_stamp,
735                                                   remote_rp_pa, remote_vars_pa,
736                                                   remote_vars);
737                         reactivate = 1;
738                 }
739         }
740
741         if (part->disengage_request_timeout > 0 &&
742             !xpc_partition_disengaged(part)) {
743                 /* still waiting on other side to disengage from us */
744                 return;
745         }
746
747         if (reactivate) {
748                 part->reactivate_nasid = nasid;
749                 XPC_DEACTIVATE_PARTITION(part, xpReactivating);
750
751         } else if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version) &&
752                    xpc_partition_disengage_requested(1UL << partid)) {
753                 XPC_DEACTIVATE_PARTITION(part, xpOtherGoingDown);
754         }
755 }
756
757 /*
758  * Loop through the activation AMO variables and process any bits
759  * which are set.  Each bit indicates a nasid sending a partition
760  * activation or deactivation request.
761  *
762  * Return #of IRQs detected.
763  */
764 int
765 xpc_identify_act_IRQ_sender(void)
766 {
767         int word, bit;
768         u64 nasid_mask;
769         u64 nasid;              /* remote nasid */
770         int n_IRQs_detected = 0;
771         AMO_t *act_amos;
772
773         act_amos = xpc_vars->amos_page + XPC_ACTIVATE_IRQ_AMOS;
774
775         /* scan through act AMO variable looking for non-zero entries */
776         for (word = 0; word < xp_nasid_mask_words; word++) {
777
778                 if (xpc_exiting)
779                         break;
780
781                 nasid_mask = xpc_IPI_receive(&act_amos[word]);
782                 if (nasid_mask == 0) {
783                         /* no IRQs from nasids in this variable */
784                         continue;
785                 }
786
787                 dev_dbg(xpc_part, "AMO[%d] gave back 0x%lx\n", word,
788                         nasid_mask);
789
790                 /*
791                  * If this nasid has been added to the machine since
792                  * our partition was reset, this will retain the
793                  * remote nasid in our reserved pages machine mask.
794                  * This is used in the event of module reload.
795                  */
796                 xpc_mach_nasids[word] |= nasid_mask;
797
798                 /* locate the nasid(s) which sent interrupts */
799
800                 for (bit = 0; bit < (8 * sizeof(u64)); bit++) {
801                         if (nasid_mask & (1UL << bit)) {
802                                 n_IRQs_detected++;
803                                 nasid = XPC_NASID_FROM_W_B(word, bit);
804                                 dev_dbg(xpc_part, "interrupt from nasid %ld\n",
805                                         nasid);
806                                 xpc_identify_act_IRQ_req(nasid);
807                         }
808                 }
809         }
810         return n_IRQs_detected;
811 }
812
813 /*
814  * See if the other side has responded to a partition disengage request
815  * from us.
816  */
817 int
818 xpc_partition_disengaged(struct xpc_partition *part)
819 {
820         short partid = XPC_PARTID(part);
821         int disengaged;
822
823         disengaged = (xpc_partition_engaged(1UL << partid) == 0);
824         if (part->disengage_request_timeout) {
825                 if (!disengaged) {
826                         if (time_before(jiffies,
827                             part->disengage_request_timeout)) {
828                                 /* timelimit hasn't been reached yet */
829                                 return 0;
830                         }
831
832                         /*
833                          * Other side hasn't responded to our disengage
834                          * request in a timely fashion, so assume it's dead.
835                          */
836
837                         dev_info(xpc_part, "disengage from remote partition %d "
838                                  "timed out\n", partid);
839                         xpc_disengage_request_timedout = 1;
840                         xpc_clear_partition_engaged(1UL << partid);
841                         disengaged = 1;
842                 }
843                 part->disengage_request_timeout = 0;
844
845                 /* cancel the timer function, provided it's not us */
846                 if (!in_interrupt()) {
847                         del_singleshot_timer_sync(&part->
848                                                   disengage_request_timer);
849                 }
850
851                 DBUG_ON(part->act_state != XPC_P_DEACTIVATING &&
852                         part->act_state != XPC_P_INACTIVE);
853                 if (part->act_state != XPC_P_INACTIVE)
854                         xpc_wakeup_channel_mgr(part);
855
856                 if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version))
857                         xpc_cancel_partition_disengage_request(part);
858         }
859         return disengaged;
860 }
861
862 /*
863  * Mark specified partition as active.
864  */
865 enum xp_retval
866 xpc_mark_partition_active(struct xpc_partition *part)
867 {
868         unsigned long irq_flags;
869         enum xp_retval ret;
870
871         dev_dbg(xpc_part, "setting partition %d to ACTIVE\n", XPC_PARTID(part));
872
873         spin_lock_irqsave(&part->act_lock, irq_flags);
874         if (part->act_state == XPC_P_ACTIVATING) {
875                 part->act_state = XPC_P_ACTIVE;
876                 ret = xpSuccess;
877         } else {
878                 DBUG_ON(part->reason == xpSuccess);
879                 ret = part->reason;
880         }
881         spin_unlock_irqrestore(&part->act_lock, irq_flags);
882
883         return ret;
884 }
885
886 /*
887  * Notify XPC that the partition is down.
888  */
889 void
890 xpc_deactivate_partition(const int line, struct xpc_partition *part,
891                          enum xp_retval reason)
892 {
893         unsigned long irq_flags;
894
895         spin_lock_irqsave(&part->act_lock, irq_flags);
896
897         if (part->act_state == XPC_P_INACTIVE) {
898                 XPC_SET_REASON(part, reason, line);
899                 spin_unlock_irqrestore(&part->act_lock, irq_flags);
900                 if (reason == xpReactivating) {
901                         /* we interrupt ourselves to reactivate partition */
902                         xpc_IPI_send_reactivate(part);
903                 }
904                 return;
905         }
906         if (part->act_state == XPC_P_DEACTIVATING) {
907                 if ((part->reason == xpUnloading && reason != xpUnloading) ||
908                     reason == xpReactivating) {
909                         XPC_SET_REASON(part, reason, line);
910                 }
911                 spin_unlock_irqrestore(&part->act_lock, irq_flags);
912                 return;
913         }
914
915         part->act_state = XPC_P_DEACTIVATING;
916         XPC_SET_REASON(part, reason, line);
917
918         spin_unlock_irqrestore(&part->act_lock, irq_flags);
919
920         if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) {
921                 xpc_request_partition_disengage(part);
922                 xpc_IPI_send_disengage(part);
923
924                 /* set a timelimit on the disengage request */
925                 part->disengage_request_timeout = jiffies +
926                     (xpc_disengage_request_timelimit * HZ);
927                 part->disengage_request_timer.expires =
928                     part->disengage_request_timeout;
929                 add_timer(&part->disengage_request_timer);
930         }
931
932         dev_dbg(xpc_part, "bringing partition %d down, reason = %d\n",
933                 XPC_PARTID(part), reason);
934
935         xpc_partition_going_down(part, reason);
936 }
937
938 /*
939  * Mark specified partition as inactive.
940  */
941 void
942 xpc_mark_partition_inactive(struct xpc_partition *part)
943 {
944         unsigned long irq_flags;
945
946         dev_dbg(xpc_part, "setting partition %d to INACTIVE\n",
947                 XPC_PARTID(part));
948
949         spin_lock_irqsave(&part->act_lock, irq_flags);
950         part->act_state = XPC_P_INACTIVE;
951         spin_unlock_irqrestore(&part->act_lock, irq_flags);
952         part->remote_rp_pa = 0;
953 }
954
955 /*
956  * SAL has provided a partition and machine mask.  The partition mask
957  * contains a bit for each even nasid in our partition.  The machine
958  * mask contains a bit for each even nasid in the entire machine.
959  *
960  * Using those two bit arrays, we can determine which nasids are
961  * known in the machine.  Each should also have a reserved page
962  * initialized if they are available for partitioning.
963  */
964 void
965 xpc_discovery(void)
966 {
967         void *remote_rp_base;
968         struct xpc_rsvd_page *remote_rp;
969         struct xpc_vars *remote_vars;
970         u64 remote_rp_pa;
971         u64 remote_vars_pa;
972         int region;
973         int region_size;
974         int max_regions;
975         int nasid;
976         struct xpc_rsvd_page *rp;
977         short partid;
978         struct xpc_partition *part;
979         u64 *discovered_nasids;
980         enum xp_retval ret;
981
982         remote_rp = xpc_kmalloc_cacheline_aligned(XPC_RP_HEADER_SIZE +
983                                                   xp_nasid_mask_bytes,
984                                                   GFP_KERNEL, &remote_rp_base);
985         if (remote_rp == NULL)
986                 return;
987
988         remote_vars = (struct xpc_vars *)remote_rp;
989
990         discovered_nasids = kzalloc(sizeof(u64) * xp_nasid_mask_words,
991                                     GFP_KERNEL);
992         if (discovered_nasids == NULL) {
993                 kfree(remote_rp_base);
994                 return;
995         }
996
997         rp = (struct xpc_rsvd_page *)xpc_rsvd_page;
998
999         /*
1000          * The term 'region' in this context refers to the minimum number of
1001          * nodes that can comprise an access protection grouping. The access
1002          * protection is in regards to memory, IOI and IPI.
1003          */
1004         max_regions = 64;
1005         region_size = sn_region_size;
1006
1007         switch (region_size) {
1008         case 128:
1009                 max_regions *= 2;
1010         case 64:
1011                 max_regions *= 2;
1012         case 32:
1013                 max_regions *= 2;
1014                 region_size = 16;
1015                 DBUG_ON(!is_shub2());
1016         }
1017
1018         for (region = 0; region < max_regions; region++) {
1019
1020                 if (xpc_exiting)
1021                         break;
1022
1023                 dev_dbg(xpc_part, "searching region %d\n", region);
1024
1025                 for (nasid = (region * region_size * 2);
1026                      nasid < ((region + 1) * region_size * 2); nasid += 2) {
1027
1028                         if (xpc_exiting)
1029                                 break;
1030
1031                         dev_dbg(xpc_part, "checking nasid %d\n", nasid);
1032
1033                         if (XPC_NASID_IN_ARRAY(nasid, xpc_part_nasids)) {
1034                                 dev_dbg(xpc_part, "PROM indicates Nasid %d is "
1035                                         "part of the local partition; skipping "
1036                                         "region\n", nasid);
1037                                 break;
1038                         }
1039
1040                         if (!(XPC_NASID_IN_ARRAY(nasid, xpc_mach_nasids))) {
1041                                 dev_dbg(xpc_part, "PROM indicates Nasid %d was "
1042                                         "not on Numa-Link network at reset\n",
1043                                         nasid);
1044                                 continue;
1045                         }
1046
1047                         if (XPC_NASID_IN_ARRAY(nasid, discovered_nasids)) {
1048                                 dev_dbg(xpc_part, "Nasid %d is part of a "
1049                                         "partition which was previously "
1050                                         "discovered\n", nasid);
1051                                 continue;
1052                         }
1053
1054                         /* pull over the reserved page structure */
1055
1056                         ret = xpc_get_remote_rp(nasid, discovered_nasids,
1057                                                 remote_rp, &remote_rp_pa);
1058                         if (ret != xpSuccess) {
1059                                 dev_dbg(xpc_part, "unable to get reserved page "
1060                                         "from nasid %d, reason=%d\n", nasid,
1061                                         ret);
1062
1063                                 if (ret == xpLocalPartid)
1064                                         break;
1065
1066                                 continue;
1067                         }
1068
1069                         remote_vars_pa = remote_rp->vars_pa;
1070
1071                         partid = remote_rp->partid;
1072                         part = &xpc_partitions[partid];
1073
1074                         /* pull over the cross partition variables */
1075
1076                         ret = xpc_get_remote_vars(remote_vars_pa, remote_vars);
1077                         if (ret != xpSuccess) {
1078                                 dev_dbg(xpc_part, "unable to get XPC variables "
1079                                         "from nasid %d, reason=%d\n", nasid,
1080                                         ret);
1081
1082                                 XPC_DEACTIVATE_PARTITION(part, ret);
1083                                 continue;
1084                         }
1085
1086                         if (part->act_state != XPC_P_INACTIVE) {
1087                                 dev_dbg(xpc_part, "partition %d on nasid %d is "
1088                                         "already activating\n", partid, nasid);
1089                                 break;
1090                         }
1091
1092                         /*
1093                          * Register the remote partition's AMOs with SAL so it
1094                          * can handle and cleanup errors within that address
1095                          * range should the remote partition go down. We don't
1096                          * unregister this range because it is difficult to
1097                          * tell when outstanding writes to the remote partition
1098                          * are finished and thus when it is thus safe to
1099                          * unregister. This should not result in wasted space
1100                          * in the SAL xp_addr_region table because we should
1101                          * get the same page for remote_act_amos_pa after
1102                          * module reloads and system reboots.
1103                          */
1104                         if (sn_register_xp_addr_region
1105                             (remote_vars->amos_page_pa, PAGE_SIZE, 1) < 0) {
1106                                 dev_dbg(xpc_part,
1107                                         "partition %d failed to "
1108                                         "register xp_addr region 0x%016lx\n",
1109                                         partid, remote_vars->amos_page_pa);
1110
1111                                 XPC_SET_REASON(part, xpPhysAddrRegFailed,
1112                                                __LINE__);
1113                                 break;
1114                         }
1115
1116                         /*
1117                          * The remote nasid is valid and available.
1118                          * Send an interrupt to that nasid to notify
1119                          * it that we are ready to begin activation.
1120                          */
1121                         dev_dbg(xpc_part, "sending an interrupt to AMO 0x%lx, "
1122                                 "nasid %d, phys_cpuid 0x%x\n",
1123                                 remote_vars->amos_page_pa,
1124                                 remote_vars->act_nasid,
1125                                 remote_vars->act_phys_cpuid);
1126
1127                         if (XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->
1128                                                            version)) {
1129                                 part->remote_amos_page_pa =
1130                                     remote_vars->amos_page_pa;
1131                                 xpc_mark_partition_disengaged(part);
1132                                 xpc_cancel_partition_disengage_request(part);
1133                         }
1134                         xpc_IPI_send_activate(remote_vars);
1135                 }
1136         }
1137
1138         kfree(discovered_nasids);
1139         kfree(remote_rp_base);
1140 }
1141
1142 /*
1143  * Given a partid, get the nasids owned by that partition from the
1144  * remote partition's reserved page.
1145  */
1146 enum xp_retval
1147 xpc_initiate_partid_to_nasids(short partid, void *nasid_mask)
1148 {
1149         struct xpc_partition *part;
1150         u64 part_nasid_pa;
1151         int bte_res;
1152
1153         part = &xpc_partitions[partid];
1154         if (part->remote_rp_pa == 0)
1155                 return xpPartitionDown;
1156
1157         memset(nasid_mask, 0, XP_NASID_MASK_BYTES);
1158
1159         part_nasid_pa = (u64)XPC_RP_PART_NASIDS(part->remote_rp_pa);
1160
1161         bte_res = xp_bte_copy(part_nasid_pa, (u64)nasid_mask,
1162                               xp_nasid_mask_bytes, (BTE_NOTIFY | BTE_WACQUIRE),
1163                               NULL);
1164
1165         return xpc_map_bte_errors(bte_res);
1166 }