Path: blob/master/drivers/misc/sgi-xp/xpc_partition.c
15111 views
/*1* This file is subject to the terms and conditions of the GNU General Public2* License. See the file "COPYING" in the main directory of this archive3* for more details.4*5* Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.6*/78/*9* Cross Partition Communication (XPC) partition support.10*11* This is the part of XPC that detects the presence/absence of12* other partitions. It provides a heartbeat and monitors the13* heartbeats of other partitions.14*15*/1617#include <linux/device.h>18#include <linux/hardirq.h>19#include <linux/slab.h>20#include "xpc.h"21#include <asm/uv/uv_hub.h>2223/* XPC is exiting flag */24int xpc_exiting;2526/* this partition's reserved page pointers */27struct xpc_rsvd_page *xpc_rsvd_page;28static unsigned long *xpc_part_nasids;29unsigned long *xpc_mach_nasids;3031static int xpc_nasid_mask_nbytes; /* #of bytes in nasid mask */32int xpc_nasid_mask_nlongs; /* #of longs in nasid mask */3334struct xpc_partition *xpc_partitions;3536/*37* Guarantee that the kmalloc'd memory is cacheline aligned.38*/39void *40xpc_kmalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)41{42/* see if kmalloc will give us cachline aligned memory by default */43*base = kmalloc(size, flags);44if (*base == NULL)45return NULL;4647if ((u64)*base == L1_CACHE_ALIGN((u64)*base))48return *base;4950kfree(*base);5152/* nope, we'll have to do it ourselves */53*base = kmalloc(size + L1_CACHE_BYTES, flags);54if (*base == NULL)55return NULL;5657return (void *)L1_CACHE_ALIGN((u64)*base);58}5960/*61* Given a nasid, get the physical address of the partition's reserved page62* for that nasid. This function returns 0 on any error.63*/64static unsigned long65xpc_get_rsvd_page_pa(int nasid)66{67enum xp_retval ret;68u64 cookie = 0;69unsigned long rp_pa = nasid; /* seed with nasid */70size_t len = 0;71size_t buf_len = 0;72void *buf = buf;73void *buf_base = NULL;74enum xp_retval (*get_partition_rsvd_page_pa)75(void *, u64 *, unsigned long *, size_t *) =76xpc_arch_ops.get_partition_rsvd_page_pa;7778while (1) {7980/* !!! rp_pa will need to be _gpa on UV.81* ??? So do we save it into the architecture specific parts82* ??? of the xpc_partition structure? Do we rename this83* ??? function or have two versions? Rename rp_pa for UV to84* ??? rp_gpa?85*/86ret = get_partition_rsvd_page_pa(buf, &cookie, &rp_pa, &len);8788dev_dbg(xpc_part, "SAL returned with ret=%d, cookie=0x%016lx, "89"address=0x%016lx, len=0x%016lx\n", ret,90(unsigned long)cookie, rp_pa, len);9192if (ret != xpNeedMoreInfo)93break;9495/* !!! L1_CACHE_ALIGN() is only a sn2-bte_copy requirement */96if (is_shub())97len = L1_CACHE_ALIGN(len);9899if (len > buf_len) {100if (buf_base != NULL)101kfree(buf_base);102buf_len = L1_CACHE_ALIGN(len);103buf = xpc_kmalloc_cacheline_aligned(buf_len, GFP_KERNEL,104&buf_base);105if (buf_base == NULL) {106dev_err(xpc_part, "unable to kmalloc "107"len=0x%016lx\n", buf_len);108ret = xpNoMemory;109break;110}111}112113ret = xp_remote_memcpy(xp_pa(buf), rp_pa, len);114if (ret != xpSuccess) {115dev_dbg(xpc_part, "xp_remote_memcpy failed %d\n", ret);116break;117}118}119120kfree(buf_base);121122if (ret != xpSuccess)123rp_pa = 0;124125dev_dbg(xpc_part, "reserved page at phys address 0x%016lx\n", rp_pa);126return rp_pa;127}128129/*130* Fill the partition reserved page with the information needed by131* other partitions to discover we are alive and establish initial132* communications.133*/134int135xpc_setup_rsvd_page(void)136{137int ret;138struct xpc_rsvd_page *rp;139unsigned long rp_pa;140unsigned long new_ts_jiffies;141142/* get the local reserved page's address */143144preempt_disable();145rp_pa = xpc_get_rsvd_page_pa(xp_cpu_to_nasid(smp_processor_id()));146preempt_enable();147if (rp_pa == 0) {148dev_err(xpc_part, "SAL failed to locate the reserved page\n");149return -ESRCH;150}151rp = (struct xpc_rsvd_page *)__va(xp_socket_pa(rp_pa));152153if (rp->SAL_version < 3) {154/* SAL_versions < 3 had a SAL_partid defined as a u8 */155rp->SAL_partid &= 0xff;156}157BUG_ON(rp->SAL_partid != xp_partition_id);158159if (rp->SAL_partid < 0 || rp->SAL_partid >= xp_max_npartitions) {160dev_err(xpc_part, "the reserved page's partid of %d is outside "161"supported range (< 0 || >= %d)\n", rp->SAL_partid,162xp_max_npartitions);163return -EINVAL;164}165166rp->version = XPC_RP_VERSION;167rp->max_npartitions = xp_max_npartitions;168169/* establish the actual sizes of the nasid masks */170if (rp->SAL_version == 1) {171/* SAL_version 1 didn't set the nasids_size field */172rp->SAL_nasids_size = 128;173}174xpc_nasid_mask_nbytes = rp->SAL_nasids_size;175xpc_nasid_mask_nlongs = BITS_TO_LONGS(rp->SAL_nasids_size *176BITS_PER_BYTE);177178/* setup the pointers to the various items in the reserved page */179xpc_part_nasids = XPC_RP_PART_NASIDS(rp);180xpc_mach_nasids = XPC_RP_MACH_NASIDS(rp);181182ret = xpc_arch_ops.setup_rsvd_page(rp);183if (ret != 0)184return ret;185186/*187* Set timestamp of when reserved page was setup by XPC.188* This signifies to the remote partition that our reserved189* page is initialized.190*/191new_ts_jiffies = jiffies;192if (new_ts_jiffies == 0 || new_ts_jiffies == rp->ts_jiffies)193new_ts_jiffies++;194rp->ts_jiffies = new_ts_jiffies;195196xpc_rsvd_page = rp;197return 0;198}199200void201xpc_teardown_rsvd_page(void)202{203/* a zero timestamp indicates our rsvd page is not initialized */204xpc_rsvd_page->ts_jiffies = 0;205}206207/*208* Get a copy of a portion of the remote partition's rsvd page.209*210* remote_rp points to a buffer that is cacheline aligned for BTE copies and211* is large enough to contain a copy of their reserved page header and212* part_nasids mask.213*/214enum xp_retval215xpc_get_remote_rp(int nasid, unsigned long *discovered_nasids,216struct xpc_rsvd_page *remote_rp, unsigned long *remote_rp_pa)217{218int l;219enum xp_retval ret;220221/* get the reserved page's physical address */222223*remote_rp_pa = xpc_get_rsvd_page_pa(nasid);224if (*remote_rp_pa == 0)225return xpNoRsvdPageAddr;226227/* pull over the reserved page header and part_nasids mask */228ret = xp_remote_memcpy(xp_pa(remote_rp), *remote_rp_pa,229XPC_RP_HEADER_SIZE + xpc_nasid_mask_nbytes);230if (ret != xpSuccess)231return ret;232233if (discovered_nasids != NULL) {234unsigned long *remote_part_nasids =235XPC_RP_PART_NASIDS(remote_rp);236237for (l = 0; l < xpc_nasid_mask_nlongs; l++)238discovered_nasids[l] |= remote_part_nasids[l];239}240241/* zero timestamp indicates the reserved page has not been setup */242if (remote_rp->ts_jiffies == 0)243return xpRsvdPageNotSet;244245if (XPC_VERSION_MAJOR(remote_rp->version) !=246XPC_VERSION_MAJOR(XPC_RP_VERSION)) {247return xpBadVersion;248}249250/* check that both remote and local partids are valid for each side */251if (remote_rp->SAL_partid < 0 ||252remote_rp->SAL_partid >= xp_max_npartitions ||253remote_rp->max_npartitions <= xp_partition_id) {254return xpInvalidPartid;255}256257if (remote_rp->SAL_partid == xp_partition_id)258return xpLocalPartid;259260return xpSuccess;261}262263/*264* See if the other side has responded to a partition deactivate request265* from us. Though we requested the remote partition to deactivate with regard266* to us, we really only need to wait for the other side to disengage from us.267*/268int269xpc_partition_disengaged(struct xpc_partition *part)270{271short partid = XPC_PARTID(part);272int disengaged;273274disengaged = !xpc_arch_ops.partition_engaged(partid);275if (part->disengage_timeout) {276if (!disengaged) {277if (time_is_after_jiffies(part->disengage_timeout)) {278/* timelimit hasn't been reached yet */279return 0;280}281282/*283* Other side hasn't responded to our deactivate284* request in a timely fashion, so assume it's dead.285*/286287dev_info(xpc_part, "deactivate request to remote "288"partition %d timed out\n", partid);289xpc_disengage_timedout = 1;290xpc_arch_ops.assume_partition_disengaged(partid);291disengaged = 1;292}293part->disengage_timeout = 0;294295/* cancel the timer function, provided it's not us */296if (!in_interrupt())297del_singleshot_timer_sync(&part->disengage_timer);298299DBUG_ON(part->act_state != XPC_P_AS_DEACTIVATING &&300part->act_state != XPC_P_AS_INACTIVE);301if (part->act_state != XPC_P_AS_INACTIVE)302xpc_wakeup_channel_mgr(part);303304xpc_arch_ops.cancel_partition_deactivation_request(part);305}306return disengaged;307}308309/*310* Mark specified partition as active.311*/312enum xp_retval313xpc_mark_partition_active(struct xpc_partition *part)314{315unsigned long irq_flags;316enum xp_retval ret;317318dev_dbg(xpc_part, "setting partition %d to ACTIVE\n", XPC_PARTID(part));319320spin_lock_irqsave(&part->act_lock, irq_flags);321if (part->act_state == XPC_P_AS_ACTIVATING) {322part->act_state = XPC_P_AS_ACTIVE;323ret = xpSuccess;324} else {325DBUG_ON(part->reason == xpSuccess);326ret = part->reason;327}328spin_unlock_irqrestore(&part->act_lock, irq_flags);329330return ret;331}332333/*334* Start the process of deactivating the specified partition.335*/336void337xpc_deactivate_partition(const int line, struct xpc_partition *part,338enum xp_retval reason)339{340unsigned long irq_flags;341342spin_lock_irqsave(&part->act_lock, irq_flags);343344if (part->act_state == XPC_P_AS_INACTIVE) {345XPC_SET_REASON(part, reason, line);346spin_unlock_irqrestore(&part->act_lock, irq_flags);347if (reason == xpReactivating) {348/* we interrupt ourselves to reactivate partition */349xpc_arch_ops.request_partition_reactivation(part);350}351return;352}353if (part->act_state == XPC_P_AS_DEACTIVATING) {354if ((part->reason == xpUnloading && reason != xpUnloading) ||355reason == xpReactivating) {356XPC_SET_REASON(part, reason, line);357}358spin_unlock_irqrestore(&part->act_lock, irq_flags);359return;360}361362part->act_state = XPC_P_AS_DEACTIVATING;363XPC_SET_REASON(part, reason, line);364365spin_unlock_irqrestore(&part->act_lock, irq_flags);366367/* ask remote partition to deactivate with regard to us */368xpc_arch_ops.request_partition_deactivation(part);369370/* set a timelimit on the disengage phase of the deactivation request */371part->disengage_timeout = jiffies + (xpc_disengage_timelimit * HZ);372part->disengage_timer.expires = part->disengage_timeout;373add_timer(&part->disengage_timer);374375dev_dbg(xpc_part, "bringing partition %d down, reason = %d\n",376XPC_PARTID(part), reason);377378xpc_partition_going_down(part, reason);379}380381/*382* Mark specified partition as inactive.383*/384void385xpc_mark_partition_inactive(struct xpc_partition *part)386{387unsigned long irq_flags;388389dev_dbg(xpc_part, "setting partition %d to INACTIVE\n",390XPC_PARTID(part));391392spin_lock_irqsave(&part->act_lock, irq_flags);393part->act_state = XPC_P_AS_INACTIVE;394spin_unlock_irqrestore(&part->act_lock, irq_flags);395part->remote_rp_pa = 0;396}397398/*399* SAL has provided a partition and machine mask. The partition mask400* contains a bit for each even nasid in our partition. The machine401* mask contains a bit for each even nasid in the entire machine.402*403* Using those two bit arrays, we can determine which nasids are404* known in the machine. Each should also have a reserved page405* initialized if they are available for partitioning.406*/407void408xpc_discovery(void)409{410void *remote_rp_base;411struct xpc_rsvd_page *remote_rp;412unsigned long remote_rp_pa;413int region;414int region_size;415int max_regions;416int nasid;417struct xpc_rsvd_page *rp;418unsigned long *discovered_nasids;419enum xp_retval ret;420421remote_rp = xpc_kmalloc_cacheline_aligned(XPC_RP_HEADER_SIZE +422xpc_nasid_mask_nbytes,423GFP_KERNEL, &remote_rp_base);424if (remote_rp == NULL)425return;426427discovered_nasids = kzalloc(sizeof(long) * xpc_nasid_mask_nlongs,428GFP_KERNEL);429if (discovered_nasids == NULL) {430kfree(remote_rp_base);431return;432}433434rp = (struct xpc_rsvd_page *)xpc_rsvd_page;435436/*437* The term 'region' in this context refers to the minimum number of438* nodes that can comprise an access protection grouping. The access439* protection is in regards to memory, IOI and IPI.440*/441region_size = xp_region_size;442443if (is_uv())444max_regions = 256;445else {446max_regions = 64;447448switch (region_size) {449case 128:450max_regions *= 2;451case 64:452max_regions *= 2;453case 32:454max_regions *= 2;455region_size = 16;456DBUG_ON(!is_shub2());457}458}459460for (region = 0; region < max_regions; region++) {461462if (xpc_exiting)463break;464465dev_dbg(xpc_part, "searching region %d\n", region);466467for (nasid = (region * region_size * 2);468nasid < ((region + 1) * region_size * 2); nasid += 2) {469470if (xpc_exiting)471break;472473dev_dbg(xpc_part, "checking nasid %d\n", nasid);474475if (test_bit(nasid / 2, xpc_part_nasids)) {476dev_dbg(xpc_part, "PROM indicates Nasid %d is "477"part of the local partition; skipping "478"region\n", nasid);479break;480}481482if (!(test_bit(nasid / 2, xpc_mach_nasids))) {483dev_dbg(xpc_part, "PROM indicates Nasid %d was "484"not on Numa-Link network at reset\n",485nasid);486continue;487}488489if (test_bit(nasid / 2, discovered_nasids)) {490dev_dbg(xpc_part, "Nasid %d is part of a "491"partition which was previously "492"discovered\n", nasid);493continue;494}495496/* pull over the rsvd page header & part_nasids mask */497498ret = xpc_get_remote_rp(nasid, discovered_nasids,499remote_rp, &remote_rp_pa);500if (ret != xpSuccess) {501dev_dbg(xpc_part, "unable to get reserved page "502"from nasid %d, reason=%d\n", nasid,503ret);504505if (ret == xpLocalPartid)506break;507508continue;509}510511xpc_arch_ops.request_partition_activation(remote_rp,512remote_rp_pa, nasid);513}514}515516kfree(discovered_nasids);517kfree(remote_rp_base);518}519520/*521* Given a partid, get the nasids owned by that partition from the522* remote partition's reserved page.523*/524enum xp_retval525xpc_initiate_partid_to_nasids(short partid, void *nasid_mask)526{527struct xpc_partition *part;528unsigned long part_nasid_pa;529530part = &xpc_partitions[partid];531if (part->remote_rp_pa == 0)532return xpPartitionDown;533534memset(nasid_mask, 0, xpc_nasid_mask_nbytes);535536part_nasid_pa = (unsigned long)XPC_RP_PART_NASIDS(part->remote_rp_pa);537538return xp_remote_memcpy(xp_pa(nasid_mask), part_nasid_pa,539xpc_nasid_mask_nbytes);540}541542543