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