18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
38c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
48c2ecf20Sopenharmony_ci * for more details.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (c) 2008-2009 Silicon Graphics, Inc.  All Rights Reserved.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci/*
108c2ecf20Sopenharmony_ci * Cross Partition Communication (XPC) uv-based functions.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *     Architecture specific implementation of common functions.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/kernel.h>
178c2ecf20Sopenharmony_ci#include <linux/mm.h>
188c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
198c2ecf20Sopenharmony_ci#include <linux/delay.h>
208c2ecf20Sopenharmony_ci#include <linux/device.h>
218c2ecf20Sopenharmony_ci#include <linux/cpu.h>
228c2ecf20Sopenharmony_ci#include <linux/module.h>
238c2ecf20Sopenharmony_ci#include <linux/err.h>
248c2ecf20Sopenharmony_ci#include <linux/slab.h>
258c2ecf20Sopenharmony_ci#include <linux/numa.h>
268c2ecf20Sopenharmony_ci#include <asm/uv/uv_hub.h>
278c2ecf20Sopenharmony_ci#if defined CONFIG_X86_64
288c2ecf20Sopenharmony_ci#include <asm/uv/bios.h>
298c2ecf20Sopenharmony_ci#include <asm/uv/uv_irq.h>
308c2ecf20Sopenharmony_ci#elif defined CONFIG_IA64_SGI_UV
318c2ecf20Sopenharmony_ci#include <asm/sn/intr.h>
328c2ecf20Sopenharmony_ci#include <asm/sn/sn_sal.h>
338c2ecf20Sopenharmony_ci#endif
348c2ecf20Sopenharmony_ci#include "../sgi-gru/gru.h"
358c2ecf20Sopenharmony_ci#include "../sgi-gru/grukservices.h"
368c2ecf20Sopenharmony_ci#include "xpc.h"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#if defined CONFIG_IA64_SGI_UV
398c2ecf20Sopenharmony_cistruct uv_IO_APIC_route_entry {
408c2ecf20Sopenharmony_ci	__u64	vector		:  8,
418c2ecf20Sopenharmony_ci		delivery_mode	:  3,
428c2ecf20Sopenharmony_ci		dest_mode	:  1,
438c2ecf20Sopenharmony_ci		delivery_status	:  1,
448c2ecf20Sopenharmony_ci		polarity	:  1,
458c2ecf20Sopenharmony_ci		__reserved_1	:  1,
468c2ecf20Sopenharmony_ci		trigger		:  1,
478c2ecf20Sopenharmony_ci		mask		:  1,
488c2ecf20Sopenharmony_ci		__reserved_2	: 15,
498c2ecf20Sopenharmony_ci		dest		: 32;
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define sn_partition_id 0
538c2ecf20Sopenharmony_ci#endif
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic struct xpc_heartbeat_uv *xpc_heartbeat_uv;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define XPC_ACTIVATE_MSG_SIZE_UV	(1 * GRU_CACHE_LINE_BYTES)
588c2ecf20Sopenharmony_ci#define XPC_ACTIVATE_MQ_SIZE_UV		(4 * XP_MAX_NPARTITIONS_UV * \
598c2ecf20Sopenharmony_ci					 XPC_ACTIVATE_MSG_SIZE_UV)
608c2ecf20Sopenharmony_ci#define XPC_ACTIVATE_IRQ_NAME		"xpc_activate"
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define XPC_NOTIFY_MSG_SIZE_UV		(2 * GRU_CACHE_LINE_BYTES)
638c2ecf20Sopenharmony_ci#define XPC_NOTIFY_MQ_SIZE_UV		(4 * XP_MAX_NPARTITIONS_UV * \
648c2ecf20Sopenharmony_ci					 XPC_NOTIFY_MSG_SIZE_UV)
658c2ecf20Sopenharmony_ci#define XPC_NOTIFY_IRQ_NAME		"xpc_notify"
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic int xpc_mq_node = NUMA_NO_NODE;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic struct xpc_gru_mq_uv *xpc_activate_mq_uv;
708c2ecf20Sopenharmony_cistatic struct xpc_gru_mq_uv *xpc_notify_mq_uv;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic int
738c2ecf20Sopenharmony_cixpc_setup_partitions_uv(void)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	short partid;
768c2ecf20Sopenharmony_ci	struct xpc_partition_uv *part_uv;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	for (partid = 0; partid < XP_MAX_NPARTITIONS_UV; partid++) {
798c2ecf20Sopenharmony_ci		part_uv = &xpc_partitions[partid].sn.uv;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci		mutex_init(&part_uv->cached_activate_gru_mq_desc_mutex);
828c2ecf20Sopenharmony_ci		spin_lock_init(&part_uv->flags_lock);
838c2ecf20Sopenharmony_ci		part_uv->remote_act_state = XPC_P_AS_INACTIVE;
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci	return 0;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic void
898c2ecf20Sopenharmony_cixpc_teardown_partitions_uv(void)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	short partid;
928c2ecf20Sopenharmony_ci	struct xpc_partition_uv *part_uv;
938c2ecf20Sopenharmony_ci	unsigned long irq_flags;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	for (partid = 0; partid < XP_MAX_NPARTITIONS_UV; partid++) {
968c2ecf20Sopenharmony_ci		part_uv = &xpc_partitions[partid].sn.uv;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci		if (part_uv->cached_activate_gru_mq_desc != NULL) {
998c2ecf20Sopenharmony_ci			mutex_lock(&part_uv->cached_activate_gru_mq_desc_mutex);
1008c2ecf20Sopenharmony_ci			spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
1018c2ecf20Sopenharmony_ci			part_uv->flags &= ~XPC_P_CACHED_ACTIVATE_GRU_MQ_DESC_UV;
1028c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
1038c2ecf20Sopenharmony_ci			kfree(part_uv->cached_activate_gru_mq_desc);
1048c2ecf20Sopenharmony_ci			part_uv->cached_activate_gru_mq_desc = NULL;
1058c2ecf20Sopenharmony_ci			mutex_unlock(&part_uv->
1068c2ecf20Sopenharmony_ci				     cached_activate_gru_mq_desc_mutex);
1078c2ecf20Sopenharmony_ci		}
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic int
1128c2ecf20Sopenharmony_cixpc_get_gru_mq_irq_uv(struct xpc_gru_mq_uv *mq, int cpu, char *irq_name)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	int mmr_pnode = uv_blade_to_pnode(mq->mmr_blade);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci#if defined CONFIG_X86_64
1178c2ecf20Sopenharmony_ci	mq->irq = uv_setup_irq(irq_name, cpu, mq->mmr_blade, mq->mmr_offset,
1188c2ecf20Sopenharmony_ci			UV_AFFINITY_CPU);
1198c2ecf20Sopenharmony_ci	if (mq->irq < 0)
1208c2ecf20Sopenharmony_ci		return mq->irq;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	mq->mmr_value = uv_read_global_mmr64(mmr_pnode, mq->mmr_offset);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci#elif defined CONFIG_IA64_SGI_UV
1258c2ecf20Sopenharmony_ci	if (strcmp(irq_name, XPC_ACTIVATE_IRQ_NAME) == 0)
1268c2ecf20Sopenharmony_ci		mq->irq = SGI_XPC_ACTIVATE;
1278c2ecf20Sopenharmony_ci	else if (strcmp(irq_name, XPC_NOTIFY_IRQ_NAME) == 0)
1288c2ecf20Sopenharmony_ci		mq->irq = SGI_XPC_NOTIFY;
1298c2ecf20Sopenharmony_ci	else
1308c2ecf20Sopenharmony_ci		return -EINVAL;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	mq->mmr_value = (unsigned long)cpu_physical_id(cpu) << 32 | mq->irq;
1338c2ecf20Sopenharmony_ci	uv_write_global_mmr64(mmr_pnode, mq->mmr_offset, mq->mmr_value);
1348c2ecf20Sopenharmony_ci#else
1358c2ecf20Sopenharmony_ci	#error not a supported configuration
1368c2ecf20Sopenharmony_ci#endif
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	return 0;
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic void
1428c2ecf20Sopenharmony_cixpc_release_gru_mq_irq_uv(struct xpc_gru_mq_uv *mq)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci#if defined CONFIG_X86_64
1458c2ecf20Sopenharmony_ci	uv_teardown_irq(mq->irq);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci#elif defined CONFIG_IA64_SGI_UV
1488c2ecf20Sopenharmony_ci	int mmr_pnode;
1498c2ecf20Sopenharmony_ci	unsigned long mmr_value;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	mmr_pnode = uv_blade_to_pnode(mq->mmr_blade);
1528c2ecf20Sopenharmony_ci	mmr_value = 1UL << 16;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	uv_write_global_mmr64(mmr_pnode, mq->mmr_offset, mmr_value);
1558c2ecf20Sopenharmony_ci#else
1568c2ecf20Sopenharmony_ci	#error not a supported configuration
1578c2ecf20Sopenharmony_ci#endif
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic int
1618c2ecf20Sopenharmony_cixpc_gru_mq_watchlist_alloc_uv(struct xpc_gru_mq_uv *mq)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	int ret;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci#if defined CONFIG_IA64_SGI_UV
1668c2ecf20Sopenharmony_ci	int mmr_pnode = uv_blade_to_pnode(mq->mmr_blade);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	ret = sn_mq_watchlist_alloc(mmr_pnode, (void *)uv_gpa(mq->address),
1698c2ecf20Sopenharmony_ci				    mq->order, &mq->mmr_offset);
1708c2ecf20Sopenharmony_ci	if (ret < 0) {
1718c2ecf20Sopenharmony_ci		dev_err(xpc_part, "sn_mq_watchlist_alloc() failed, ret=%d\n",
1728c2ecf20Sopenharmony_ci			ret);
1738c2ecf20Sopenharmony_ci		return -EBUSY;
1748c2ecf20Sopenharmony_ci	}
1758c2ecf20Sopenharmony_ci#elif defined CONFIG_X86_64
1768c2ecf20Sopenharmony_ci	ret = uv_bios_mq_watchlist_alloc(uv_gpa(mq->address),
1778c2ecf20Sopenharmony_ci					 mq->order, &mq->mmr_offset);
1788c2ecf20Sopenharmony_ci	if (ret < 0) {
1798c2ecf20Sopenharmony_ci		dev_err(xpc_part, "uv_bios_mq_watchlist_alloc() failed, "
1808c2ecf20Sopenharmony_ci			"ret=%d\n", ret);
1818c2ecf20Sopenharmony_ci		return ret;
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci#else
1848c2ecf20Sopenharmony_ci	#error not a supported configuration
1858c2ecf20Sopenharmony_ci#endif
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	mq->watchlist_num = ret;
1888c2ecf20Sopenharmony_ci	return 0;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic void
1928c2ecf20Sopenharmony_cixpc_gru_mq_watchlist_free_uv(struct xpc_gru_mq_uv *mq)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	int ret;
1958c2ecf20Sopenharmony_ci	int mmr_pnode = uv_blade_to_pnode(mq->mmr_blade);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci#if defined CONFIG_X86_64
1988c2ecf20Sopenharmony_ci	ret = uv_bios_mq_watchlist_free(mmr_pnode, mq->watchlist_num);
1998c2ecf20Sopenharmony_ci	BUG_ON(ret != BIOS_STATUS_SUCCESS);
2008c2ecf20Sopenharmony_ci#elif defined CONFIG_IA64_SGI_UV
2018c2ecf20Sopenharmony_ci	ret = sn_mq_watchlist_free(mmr_pnode, mq->watchlist_num);
2028c2ecf20Sopenharmony_ci	BUG_ON(ret != SALRET_OK);
2038c2ecf20Sopenharmony_ci#else
2048c2ecf20Sopenharmony_ci	#error not a supported configuration
2058c2ecf20Sopenharmony_ci#endif
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic struct xpc_gru_mq_uv *
2098c2ecf20Sopenharmony_cixpc_create_gru_mq_uv(unsigned int mq_size, int cpu, char *irq_name,
2108c2ecf20Sopenharmony_ci		     irq_handler_t irq_handler)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	enum xp_retval xp_ret;
2138c2ecf20Sopenharmony_ci	int ret;
2148c2ecf20Sopenharmony_ci	int nid;
2158c2ecf20Sopenharmony_ci	int nasid;
2168c2ecf20Sopenharmony_ci	int pg_order;
2178c2ecf20Sopenharmony_ci	struct page *page;
2188c2ecf20Sopenharmony_ci	struct xpc_gru_mq_uv *mq;
2198c2ecf20Sopenharmony_ci	struct uv_IO_APIC_route_entry *mmr_value;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	mq = kmalloc(sizeof(struct xpc_gru_mq_uv), GFP_KERNEL);
2228c2ecf20Sopenharmony_ci	if (mq == NULL) {
2238c2ecf20Sopenharmony_ci		dev_err(xpc_part, "xpc_create_gru_mq_uv() failed to kmalloc() "
2248c2ecf20Sopenharmony_ci			"a xpc_gru_mq_uv structure\n");
2258c2ecf20Sopenharmony_ci		ret = -ENOMEM;
2268c2ecf20Sopenharmony_ci		goto out_0;
2278c2ecf20Sopenharmony_ci	}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	mq->gru_mq_desc = kzalloc(sizeof(struct gru_message_queue_desc),
2308c2ecf20Sopenharmony_ci				  GFP_KERNEL);
2318c2ecf20Sopenharmony_ci	if (mq->gru_mq_desc == NULL) {
2328c2ecf20Sopenharmony_ci		dev_err(xpc_part, "xpc_create_gru_mq_uv() failed to kmalloc() "
2338c2ecf20Sopenharmony_ci			"a gru_message_queue_desc structure\n");
2348c2ecf20Sopenharmony_ci		ret = -ENOMEM;
2358c2ecf20Sopenharmony_ci		goto out_1;
2368c2ecf20Sopenharmony_ci	}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	pg_order = get_order(mq_size);
2398c2ecf20Sopenharmony_ci	mq->order = pg_order + PAGE_SHIFT;
2408c2ecf20Sopenharmony_ci	mq_size = 1UL << mq->order;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	mq->mmr_blade = uv_cpu_to_blade_id(cpu);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	nid = cpu_to_node(cpu);
2458c2ecf20Sopenharmony_ci	page = __alloc_pages_node(nid,
2468c2ecf20Sopenharmony_ci				      GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE,
2478c2ecf20Sopenharmony_ci				      pg_order);
2488c2ecf20Sopenharmony_ci	if (page == NULL) {
2498c2ecf20Sopenharmony_ci		dev_err(xpc_part, "xpc_create_gru_mq_uv() failed to alloc %d "
2508c2ecf20Sopenharmony_ci			"bytes of memory on nid=%d for GRU mq\n", mq_size, nid);
2518c2ecf20Sopenharmony_ci		ret = -ENOMEM;
2528c2ecf20Sopenharmony_ci		goto out_2;
2538c2ecf20Sopenharmony_ci	}
2548c2ecf20Sopenharmony_ci	mq->address = page_address(page);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	/* enable generation of irq when GRU mq operation occurs to this mq */
2578c2ecf20Sopenharmony_ci	ret = xpc_gru_mq_watchlist_alloc_uv(mq);
2588c2ecf20Sopenharmony_ci	if (ret != 0)
2598c2ecf20Sopenharmony_ci		goto out_3;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	ret = xpc_get_gru_mq_irq_uv(mq, cpu, irq_name);
2628c2ecf20Sopenharmony_ci	if (ret != 0)
2638c2ecf20Sopenharmony_ci		goto out_4;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	ret = request_irq(mq->irq, irq_handler, 0, irq_name, NULL);
2668c2ecf20Sopenharmony_ci	if (ret != 0) {
2678c2ecf20Sopenharmony_ci		dev_err(xpc_part, "request_irq(irq=%d) returned error=%d\n",
2688c2ecf20Sopenharmony_ci			mq->irq, -ret);
2698c2ecf20Sopenharmony_ci		goto out_5;
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	nasid = UV_PNODE_TO_NASID(uv_cpu_to_pnode(cpu));
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	mmr_value = (struct uv_IO_APIC_route_entry *)&mq->mmr_value;
2758c2ecf20Sopenharmony_ci	ret = gru_create_message_queue(mq->gru_mq_desc, mq->address, mq_size,
2768c2ecf20Sopenharmony_ci				     nasid, mmr_value->vector, mmr_value->dest);
2778c2ecf20Sopenharmony_ci	if (ret != 0) {
2788c2ecf20Sopenharmony_ci		dev_err(xpc_part, "gru_create_message_queue() returned "
2798c2ecf20Sopenharmony_ci			"error=%d\n", ret);
2808c2ecf20Sopenharmony_ci		ret = -EINVAL;
2818c2ecf20Sopenharmony_ci		goto out_6;
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/* allow other partitions to access this GRU mq */
2858c2ecf20Sopenharmony_ci	xp_ret = xp_expand_memprotect(xp_pa(mq->address), mq_size);
2868c2ecf20Sopenharmony_ci	if (xp_ret != xpSuccess) {
2878c2ecf20Sopenharmony_ci		ret = -EACCES;
2888c2ecf20Sopenharmony_ci		goto out_6;
2898c2ecf20Sopenharmony_ci	}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	return mq;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	/* something went wrong */
2948c2ecf20Sopenharmony_ciout_6:
2958c2ecf20Sopenharmony_ci	free_irq(mq->irq, NULL);
2968c2ecf20Sopenharmony_ciout_5:
2978c2ecf20Sopenharmony_ci	xpc_release_gru_mq_irq_uv(mq);
2988c2ecf20Sopenharmony_ciout_4:
2998c2ecf20Sopenharmony_ci	xpc_gru_mq_watchlist_free_uv(mq);
3008c2ecf20Sopenharmony_ciout_3:
3018c2ecf20Sopenharmony_ci	free_pages((unsigned long)mq->address, pg_order);
3028c2ecf20Sopenharmony_ciout_2:
3038c2ecf20Sopenharmony_ci	kfree(mq->gru_mq_desc);
3048c2ecf20Sopenharmony_ciout_1:
3058c2ecf20Sopenharmony_ci	kfree(mq);
3068c2ecf20Sopenharmony_ciout_0:
3078c2ecf20Sopenharmony_ci	return ERR_PTR(ret);
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic void
3118c2ecf20Sopenharmony_cixpc_destroy_gru_mq_uv(struct xpc_gru_mq_uv *mq)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	unsigned int mq_size;
3148c2ecf20Sopenharmony_ci	int pg_order;
3158c2ecf20Sopenharmony_ci	int ret;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	/* disallow other partitions to access GRU mq */
3188c2ecf20Sopenharmony_ci	mq_size = 1UL << mq->order;
3198c2ecf20Sopenharmony_ci	ret = xp_restrict_memprotect(xp_pa(mq->address), mq_size);
3208c2ecf20Sopenharmony_ci	BUG_ON(ret != xpSuccess);
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	/* unregister irq handler and release mq irq/vector mapping */
3238c2ecf20Sopenharmony_ci	free_irq(mq->irq, NULL);
3248c2ecf20Sopenharmony_ci	xpc_release_gru_mq_irq_uv(mq);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	/* disable generation of irq when GRU mq op occurs to this mq */
3278c2ecf20Sopenharmony_ci	xpc_gru_mq_watchlist_free_uv(mq);
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	pg_order = mq->order - PAGE_SHIFT;
3308c2ecf20Sopenharmony_ci	free_pages((unsigned long)mq->address, pg_order);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	kfree(mq);
3338c2ecf20Sopenharmony_ci}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic enum xp_retval
3368c2ecf20Sopenharmony_cixpc_send_gru_msg(struct gru_message_queue_desc *gru_mq_desc, void *msg,
3378c2ecf20Sopenharmony_ci		 size_t msg_size)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	enum xp_retval xp_ret;
3408c2ecf20Sopenharmony_ci	int ret;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	while (1) {
3438c2ecf20Sopenharmony_ci		ret = gru_send_message_gpa(gru_mq_desc, msg, msg_size);
3448c2ecf20Sopenharmony_ci		if (ret == MQE_OK) {
3458c2ecf20Sopenharmony_ci			xp_ret = xpSuccess;
3468c2ecf20Sopenharmony_ci			break;
3478c2ecf20Sopenharmony_ci		}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci		if (ret == MQE_QUEUE_FULL) {
3508c2ecf20Sopenharmony_ci			dev_dbg(xpc_chan, "gru_send_message_gpa() returned "
3518c2ecf20Sopenharmony_ci				"error=MQE_QUEUE_FULL\n");
3528c2ecf20Sopenharmony_ci			/* !!! handle QLimit reached; delay & try again */
3538c2ecf20Sopenharmony_ci			/* ??? Do we add a limit to the number of retries? */
3548c2ecf20Sopenharmony_ci			(void)msleep_interruptible(10);
3558c2ecf20Sopenharmony_ci		} else if (ret == MQE_CONGESTION) {
3568c2ecf20Sopenharmony_ci			dev_dbg(xpc_chan, "gru_send_message_gpa() returned "
3578c2ecf20Sopenharmony_ci				"error=MQE_CONGESTION\n");
3588c2ecf20Sopenharmony_ci			/* !!! handle LB Overflow; simply try again */
3598c2ecf20Sopenharmony_ci			/* ??? Do we add a limit to the number of retries? */
3608c2ecf20Sopenharmony_ci		} else {
3618c2ecf20Sopenharmony_ci			/* !!! Currently this is MQE_UNEXPECTED_CB_ERR */
3628c2ecf20Sopenharmony_ci			dev_err(xpc_chan, "gru_send_message_gpa() returned "
3638c2ecf20Sopenharmony_ci				"error=%d\n", ret);
3648c2ecf20Sopenharmony_ci			xp_ret = xpGruSendMqError;
3658c2ecf20Sopenharmony_ci			break;
3668c2ecf20Sopenharmony_ci		}
3678c2ecf20Sopenharmony_ci	}
3688c2ecf20Sopenharmony_ci	return xp_ret;
3698c2ecf20Sopenharmony_ci}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic void
3728c2ecf20Sopenharmony_cixpc_process_activate_IRQ_rcvd_uv(void)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	unsigned long irq_flags;
3758c2ecf20Sopenharmony_ci	short partid;
3768c2ecf20Sopenharmony_ci	struct xpc_partition *part;
3778c2ecf20Sopenharmony_ci	u8 act_state_req;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	DBUG_ON(xpc_activate_IRQ_rcvd == 0);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
3828c2ecf20Sopenharmony_ci	for (partid = 0; partid < XP_MAX_NPARTITIONS_UV; partid++) {
3838c2ecf20Sopenharmony_ci		part = &xpc_partitions[partid];
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci		if (part->sn.uv.act_state_req == 0)
3868c2ecf20Sopenharmony_ci			continue;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci		xpc_activate_IRQ_rcvd--;
3898c2ecf20Sopenharmony_ci		BUG_ON(xpc_activate_IRQ_rcvd < 0);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci		act_state_req = part->sn.uv.act_state_req;
3928c2ecf20Sopenharmony_ci		part->sn.uv.act_state_req = 0;
3938c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci		if (act_state_req == XPC_P_ASR_ACTIVATE_UV) {
3968c2ecf20Sopenharmony_ci			if (part->act_state == XPC_P_AS_INACTIVE)
3978c2ecf20Sopenharmony_ci				xpc_activate_partition(part);
3988c2ecf20Sopenharmony_ci			else if (part->act_state == XPC_P_AS_DEACTIVATING)
3998c2ecf20Sopenharmony_ci				XPC_DEACTIVATE_PARTITION(part, xpReactivating);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci		} else if (act_state_req == XPC_P_ASR_REACTIVATE_UV) {
4028c2ecf20Sopenharmony_ci			if (part->act_state == XPC_P_AS_INACTIVE)
4038c2ecf20Sopenharmony_ci				xpc_activate_partition(part);
4048c2ecf20Sopenharmony_ci			else
4058c2ecf20Sopenharmony_ci				XPC_DEACTIVATE_PARTITION(part, xpReactivating);
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci		} else if (act_state_req == XPC_P_ASR_DEACTIVATE_UV) {
4088c2ecf20Sopenharmony_ci			XPC_DEACTIVATE_PARTITION(part, part->sn.uv.reason);
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci		} else {
4118c2ecf20Sopenharmony_ci			BUG();
4128c2ecf20Sopenharmony_ci		}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci		spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
4158c2ecf20Sopenharmony_ci		if (xpc_activate_IRQ_rcvd == 0)
4168c2ecf20Sopenharmony_ci			break;
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_cistatic void
4238c2ecf20Sopenharmony_cixpc_handle_activate_mq_msg_uv(struct xpc_partition *part,
4248c2ecf20Sopenharmony_ci			      struct xpc_activate_mq_msghdr_uv *msg_hdr,
4258c2ecf20Sopenharmony_ci			      int part_setup,
4268c2ecf20Sopenharmony_ci			      int *wakeup_hb_checker)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	unsigned long irq_flags;
4298c2ecf20Sopenharmony_ci	struct xpc_partition_uv *part_uv = &part->sn.uv;
4308c2ecf20Sopenharmony_ci	struct xpc_openclose_args *args;
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	part_uv->remote_act_state = msg_hdr->act_state;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	switch (msg_hdr->type) {
4358c2ecf20Sopenharmony_ci	case XPC_ACTIVATE_MQ_MSG_SYNC_ACT_STATE_UV:
4368c2ecf20Sopenharmony_ci		/* syncing of remote_act_state was just done above */
4378c2ecf20Sopenharmony_ci		break;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	case XPC_ACTIVATE_MQ_MSG_ACTIVATE_REQ_UV: {
4408c2ecf20Sopenharmony_ci		struct xpc_activate_mq_msg_activate_req_uv *msg;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci		/*
4438c2ecf20Sopenharmony_ci		 * ??? Do we deal here with ts_jiffies being different
4448c2ecf20Sopenharmony_ci		 * ??? if act_state != XPC_P_AS_INACTIVE instead of
4458c2ecf20Sopenharmony_ci		 * ??? below?
4468c2ecf20Sopenharmony_ci		 */
4478c2ecf20Sopenharmony_ci		msg = container_of(msg_hdr, struct
4488c2ecf20Sopenharmony_ci				   xpc_activate_mq_msg_activate_req_uv, hdr);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci		spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
4518c2ecf20Sopenharmony_ci		if (part_uv->act_state_req == 0)
4528c2ecf20Sopenharmony_ci			xpc_activate_IRQ_rcvd++;
4538c2ecf20Sopenharmony_ci		part_uv->act_state_req = XPC_P_ASR_ACTIVATE_UV;
4548c2ecf20Sopenharmony_ci		part->remote_rp_pa = msg->rp_gpa; /* !!! _pa is _gpa */
4558c2ecf20Sopenharmony_ci		part->remote_rp_ts_jiffies = msg_hdr->rp_ts_jiffies;
4568c2ecf20Sopenharmony_ci		part_uv->heartbeat_gpa = msg->heartbeat_gpa;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci		if (msg->activate_gru_mq_desc_gpa !=
4598c2ecf20Sopenharmony_ci		    part_uv->activate_gru_mq_desc_gpa) {
4608c2ecf20Sopenharmony_ci			spin_lock(&part_uv->flags_lock);
4618c2ecf20Sopenharmony_ci			part_uv->flags &= ~XPC_P_CACHED_ACTIVATE_GRU_MQ_DESC_UV;
4628c2ecf20Sopenharmony_ci			spin_unlock(&part_uv->flags_lock);
4638c2ecf20Sopenharmony_ci			part_uv->activate_gru_mq_desc_gpa =
4648c2ecf20Sopenharmony_ci			    msg->activate_gru_mq_desc_gpa;
4658c2ecf20Sopenharmony_ci		}
4668c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci		(*wakeup_hb_checker)++;
4698c2ecf20Sopenharmony_ci		break;
4708c2ecf20Sopenharmony_ci	}
4718c2ecf20Sopenharmony_ci	case XPC_ACTIVATE_MQ_MSG_DEACTIVATE_REQ_UV: {
4728c2ecf20Sopenharmony_ci		struct xpc_activate_mq_msg_deactivate_req_uv *msg;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci		msg = container_of(msg_hdr, struct
4758c2ecf20Sopenharmony_ci				   xpc_activate_mq_msg_deactivate_req_uv, hdr);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci		spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
4788c2ecf20Sopenharmony_ci		if (part_uv->act_state_req == 0)
4798c2ecf20Sopenharmony_ci			xpc_activate_IRQ_rcvd++;
4808c2ecf20Sopenharmony_ci		part_uv->act_state_req = XPC_P_ASR_DEACTIVATE_UV;
4818c2ecf20Sopenharmony_ci		part_uv->reason = msg->reason;
4828c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci		(*wakeup_hb_checker)++;
4858c2ecf20Sopenharmony_ci		return;
4868c2ecf20Sopenharmony_ci	}
4878c2ecf20Sopenharmony_ci	case XPC_ACTIVATE_MQ_MSG_CHCTL_CLOSEREQUEST_UV: {
4888c2ecf20Sopenharmony_ci		struct xpc_activate_mq_msg_chctl_closerequest_uv *msg;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci		if (!part_setup)
4918c2ecf20Sopenharmony_ci			break;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci		msg = container_of(msg_hdr, struct
4948c2ecf20Sopenharmony_ci				   xpc_activate_mq_msg_chctl_closerequest_uv,
4958c2ecf20Sopenharmony_ci				   hdr);
4968c2ecf20Sopenharmony_ci		args = &part->remote_openclose_args[msg->ch_number];
4978c2ecf20Sopenharmony_ci		args->reason = msg->reason;
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci		spin_lock_irqsave(&part->chctl_lock, irq_flags);
5008c2ecf20Sopenharmony_ci		part->chctl.flags[msg->ch_number] |= XPC_CHCTL_CLOSEREQUEST;
5018c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci		xpc_wakeup_channel_mgr(part);
5048c2ecf20Sopenharmony_ci		break;
5058c2ecf20Sopenharmony_ci	}
5068c2ecf20Sopenharmony_ci	case XPC_ACTIVATE_MQ_MSG_CHCTL_CLOSEREPLY_UV: {
5078c2ecf20Sopenharmony_ci		struct xpc_activate_mq_msg_chctl_closereply_uv *msg;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci		if (!part_setup)
5108c2ecf20Sopenharmony_ci			break;
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci		msg = container_of(msg_hdr, struct
5138c2ecf20Sopenharmony_ci				   xpc_activate_mq_msg_chctl_closereply_uv,
5148c2ecf20Sopenharmony_ci				   hdr);
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci		spin_lock_irqsave(&part->chctl_lock, irq_flags);
5178c2ecf20Sopenharmony_ci		part->chctl.flags[msg->ch_number] |= XPC_CHCTL_CLOSEREPLY;
5188c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci		xpc_wakeup_channel_mgr(part);
5218c2ecf20Sopenharmony_ci		break;
5228c2ecf20Sopenharmony_ci	}
5238c2ecf20Sopenharmony_ci	case XPC_ACTIVATE_MQ_MSG_CHCTL_OPENREQUEST_UV: {
5248c2ecf20Sopenharmony_ci		struct xpc_activate_mq_msg_chctl_openrequest_uv *msg;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci		if (!part_setup)
5278c2ecf20Sopenharmony_ci			break;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci		msg = container_of(msg_hdr, struct
5308c2ecf20Sopenharmony_ci				   xpc_activate_mq_msg_chctl_openrequest_uv,
5318c2ecf20Sopenharmony_ci				   hdr);
5328c2ecf20Sopenharmony_ci		args = &part->remote_openclose_args[msg->ch_number];
5338c2ecf20Sopenharmony_ci		args->entry_size = msg->entry_size;
5348c2ecf20Sopenharmony_ci		args->local_nentries = msg->local_nentries;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci		spin_lock_irqsave(&part->chctl_lock, irq_flags);
5378c2ecf20Sopenharmony_ci		part->chctl.flags[msg->ch_number] |= XPC_CHCTL_OPENREQUEST;
5388c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci		xpc_wakeup_channel_mgr(part);
5418c2ecf20Sopenharmony_ci		break;
5428c2ecf20Sopenharmony_ci	}
5438c2ecf20Sopenharmony_ci	case XPC_ACTIVATE_MQ_MSG_CHCTL_OPENREPLY_UV: {
5448c2ecf20Sopenharmony_ci		struct xpc_activate_mq_msg_chctl_openreply_uv *msg;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci		if (!part_setup)
5478c2ecf20Sopenharmony_ci			break;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci		msg = container_of(msg_hdr, struct
5508c2ecf20Sopenharmony_ci				   xpc_activate_mq_msg_chctl_openreply_uv, hdr);
5518c2ecf20Sopenharmony_ci		args = &part->remote_openclose_args[msg->ch_number];
5528c2ecf20Sopenharmony_ci		args->remote_nentries = msg->remote_nentries;
5538c2ecf20Sopenharmony_ci		args->local_nentries = msg->local_nentries;
5548c2ecf20Sopenharmony_ci		args->local_msgqueue_pa = msg->notify_gru_mq_desc_gpa;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci		spin_lock_irqsave(&part->chctl_lock, irq_flags);
5578c2ecf20Sopenharmony_ci		part->chctl.flags[msg->ch_number] |= XPC_CHCTL_OPENREPLY;
5588c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci		xpc_wakeup_channel_mgr(part);
5618c2ecf20Sopenharmony_ci		break;
5628c2ecf20Sopenharmony_ci	}
5638c2ecf20Sopenharmony_ci	case XPC_ACTIVATE_MQ_MSG_CHCTL_OPENCOMPLETE_UV: {
5648c2ecf20Sopenharmony_ci		struct xpc_activate_mq_msg_chctl_opencomplete_uv *msg;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci		if (!part_setup)
5678c2ecf20Sopenharmony_ci			break;
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci		msg = container_of(msg_hdr, struct
5708c2ecf20Sopenharmony_ci				xpc_activate_mq_msg_chctl_opencomplete_uv, hdr);
5718c2ecf20Sopenharmony_ci		spin_lock_irqsave(&part->chctl_lock, irq_flags);
5728c2ecf20Sopenharmony_ci		part->chctl.flags[msg->ch_number] |= XPC_CHCTL_OPENCOMPLETE;
5738c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci		xpc_wakeup_channel_mgr(part);
5768c2ecf20Sopenharmony_ci	}
5778c2ecf20Sopenharmony_ci		fallthrough;
5788c2ecf20Sopenharmony_ci	case XPC_ACTIVATE_MQ_MSG_MARK_ENGAGED_UV:
5798c2ecf20Sopenharmony_ci		spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
5808c2ecf20Sopenharmony_ci		part_uv->flags |= XPC_P_ENGAGED_UV;
5818c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
5828c2ecf20Sopenharmony_ci		break;
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	case XPC_ACTIVATE_MQ_MSG_MARK_DISENGAGED_UV:
5858c2ecf20Sopenharmony_ci		spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
5868c2ecf20Sopenharmony_ci		part_uv->flags &= ~XPC_P_ENGAGED_UV;
5878c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
5888c2ecf20Sopenharmony_ci		break;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	default:
5918c2ecf20Sopenharmony_ci		dev_err(xpc_part, "received unknown activate_mq msg type=%d "
5928c2ecf20Sopenharmony_ci			"from partition=%d\n", msg_hdr->type, XPC_PARTID(part));
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci		/* get hb checker to deactivate from the remote partition */
5958c2ecf20Sopenharmony_ci		spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
5968c2ecf20Sopenharmony_ci		if (part_uv->act_state_req == 0)
5978c2ecf20Sopenharmony_ci			xpc_activate_IRQ_rcvd++;
5988c2ecf20Sopenharmony_ci		part_uv->act_state_req = XPC_P_ASR_DEACTIVATE_UV;
5998c2ecf20Sopenharmony_ci		part_uv->reason = xpBadMsgType;
6008c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci		(*wakeup_hb_checker)++;
6038c2ecf20Sopenharmony_ci		return;
6048c2ecf20Sopenharmony_ci	}
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	if (msg_hdr->rp_ts_jiffies != part->remote_rp_ts_jiffies &&
6078c2ecf20Sopenharmony_ci	    part->remote_rp_ts_jiffies != 0) {
6088c2ecf20Sopenharmony_ci		/*
6098c2ecf20Sopenharmony_ci		 * ??? Does what we do here need to be sensitive to
6108c2ecf20Sopenharmony_ci		 * ??? act_state or remote_act_state?
6118c2ecf20Sopenharmony_ci		 */
6128c2ecf20Sopenharmony_ci		spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
6138c2ecf20Sopenharmony_ci		if (part_uv->act_state_req == 0)
6148c2ecf20Sopenharmony_ci			xpc_activate_IRQ_rcvd++;
6158c2ecf20Sopenharmony_ci		part_uv->act_state_req = XPC_P_ASR_REACTIVATE_UV;
6168c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci		(*wakeup_hb_checker)++;
6198c2ecf20Sopenharmony_ci	}
6208c2ecf20Sopenharmony_ci}
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_cistatic irqreturn_t
6238c2ecf20Sopenharmony_cixpc_handle_activate_IRQ_uv(int irq, void *dev_id)
6248c2ecf20Sopenharmony_ci{
6258c2ecf20Sopenharmony_ci	struct xpc_activate_mq_msghdr_uv *msg_hdr;
6268c2ecf20Sopenharmony_ci	short partid;
6278c2ecf20Sopenharmony_ci	struct xpc_partition *part;
6288c2ecf20Sopenharmony_ci	int wakeup_hb_checker = 0;
6298c2ecf20Sopenharmony_ci	int part_referenced;
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	while (1) {
6328c2ecf20Sopenharmony_ci		msg_hdr = gru_get_next_message(xpc_activate_mq_uv->gru_mq_desc);
6338c2ecf20Sopenharmony_ci		if (msg_hdr == NULL)
6348c2ecf20Sopenharmony_ci			break;
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci		partid = msg_hdr->partid;
6378c2ecf20Sopenharmony_ci		if (partid < 0 || partid >= XP_MAX_NPARTITIONS_UV) {
6388c2ecf20Sopenharmony_ci			dev_err(xpc_part, "xpc_handle_activate_IRQ_uv() "
6398c2ecf20Sopenharmony_ci				"received invalid partid=0x%x in message\n",
6408c2ecf20Sopenharmony_ci				partid);
6418c2ecf20Sopenharmony_ci		} else {
6428c2ecf20Sopenharmony_ci			part = &xpc_partitions[partid];
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci			part_referenced = xpc_part_ref(part);
6458c2ecf20Sopenharmony_ci			xpc_handle_activate_mq_msg_uv(part, msg_hdr,
6468c2ecf20Sopenharmony_ci						      part_referenced,
6478c2ecf20Sopenharmony_ci						      &wakeup_hb_checker);
6488c2ecf20Sopenharmony_ci			if (part_referenced)
6498c2ecf20Sopenharmony_ci				xpc_part_deref(part);
6508c2ecf20Sopenharmony_ci		}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci		gru_free_message(xpc_activate_mq_uv->gru_mq_desc, msg_hdr);
6538c2ecf20Sopenharmony_ci	}
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	if (wakeup_hb_checker)
6568c2ecf20Sopenharmony_ci		wake_up_interruptible(&xpc_activate_IRQ_wq);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
6598c2ecf20Sopenharmony_ci}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_cistatic enum xp_retval
6628c2ecf20Sopenharmony_cixpc_cache_remote_gru_mq_desc_uv(struct gru_message_queue_desc *gru_mq_desc,
6638c2ecf20Sopenharmony_ci				unsigned long gru_mq_desc_gpa)
6648c2ecf20Sopenharmony_ci{
6658c2ecf20Sopenharmony_ci	enum xp_retval ret;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	ret = xp_remote_memcpy(uv_gpa(gru_mq_desc), gru_mq_desc_gpa,
6688c2ecf20Sopenharmony_ci			       sizeof(struct gru_message_queue_desc));
6698c2ecf20Sopenharmony_ci	if (ret == xpSuccess)
6708c2ecf20Sopenharmony_ci		gru_mq_desc->mq = NULL;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	return ret;
6738c2ecf20Sopenharmony_ci}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_cistatic enum xp_retval
6768c2ecf20Sopenharmony_cixpc_send_activate_IRQ_uv(struct xpc_partition *part, void *msg, size_t msg_size,
6778c2ecf20Sopenharmony_ci			 int msg_type)
6788c2ecf20Sopenharmony_ci{
6798c2ecf20Sopenharmony_ci	struct xpc_activate_mq_msghdr_uv *msg_hdr = msg;
6808c2ecf20Sopenharmony_ci	struct xpc_partition_uv *part_uv = &part->sn.uv;
6818c2ecf20Sopenharmony_ci	struct gru_message_queue_desc *gru_mq_desc;
6828c2ecf20Sopenharmony_ci	unsigned long irq_flags;
6838c2ecf20Sopenharmony_ci	enum xp_retval ret;
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	DBUG_ON(msg_size > XPC_ACTIVATE_MSG_SIZE_UV);
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	msg_hdr->type = msg_type;
6888c2ecf20Sopenharmony_ci	msg_hdr->partid = xp_partition_id;
6898c2ecf20Sopenharmony_ci	msg_hdr->act_state = part->act_state;
6908c2ecf20Sopenharmony_ci	msg_hdr->rp_ts_jiffies = xpc_rsvd_page->ts_jiffies;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	mutex_lock(&part_uv->cached_activate_gru_mq_desc_mutex);
6938c2ecf20Sopenharmony_ciagain:
6948c2ecf20Sopenharmony_ci	if (!(part_uv->flags & XPC_P_CACHED_ACTIVATE_GRU_MQ_DESC_UV)) {
6958c2ecf20Sopenharmony_ci		gru_mq_desc = part_uv->cached_activate_gru_mq_desc;
6968c2ecf20Sopenharmony_ci		if (gru_mq_desc == NULL) {
6978c2ecf20Sopenharmony_ci			gru_mq_desc = kmalloc(sizeof(struct
6988c2ecf20Sopenharmony_ci					      gru_message_queue_desc),
6998c2ecf20Sopenharmony_ci					      GFP_ATOMIC);
7008c2ecf20Sopenharmony_ci			if (gru_mq_desc == NULL) {
7018c2ecf20Sopenharmony_ci				ret = xpNoMemory;
7028c2ecf20Sopenharmony_ci				goto done;
7038c2ecf20Sopenharmony_ci			}
7048c2ecf20Sopenharmony_ci			part_uv->cached_activate_gru_mq_desc = gru_mq_desc;
7058c2ecf20Sopenharmony_ci		}
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci		ret = xpc_cache_remote_gru_mq_desc_uv(gru_mq_desc,
7088c2ecf20Sopenharmony_ci						      part_uv->
7098c2ecf20Sopenharmony_ci						      activate_gru_mq_desc_gpa);
7108c2ecf20Sopenharmony_ci		if (ret != xpSuccess)
7118c2ecf20Sopenharmony_ci			goto done;
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci		spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
7148c2ecf20Sopenharmony_ci		part_uv->flags |= XPC_P_CACHED_ACTIVATE_GRU_MQ_DESC_UV;
7158c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
7168c2ecf20Sopenharmony_ci	}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	/* ??? Is holding a spin_lock (ch->lock) during this call a bad idea? */
7198c2ecf20Sopenharmony_ci	ret = xpc_send_gru_msg(part_uv->cached_activate_gru_mq_desc, msg,
7208c2ecf20Sopenharmony_ci			       msg_size);
7218c2ecf20Sopenharmony_ci	if (ret != xpSuccess) {
7228c2ecf20Sopenharmony_ci		smp_rmb();	/* ensure a fresh copy of part_uv->flags */
7238c2ecf20Sopenharmony_ci		if (!(part_uv->flags & XPC_P_CACHED_ACTIVATE_GRU_MQ_DESC_UV))
7248c2ecf20Sopenharmony_ci			goto again;
7258c2ecf20Sopenharmony_ci	}
7268c2ecf20Sopenharmony_cidone:
7278c2ecf20Sopenharmony_ci	mutex_unlock(&part_uv->cached_activate_gru_mq_desc_mutex);
7288c2ecf20Sopenharmony_ci	return ret;
7298c2ecf20Sopenharmony_ci}
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_cistatic void
7328c2ecf20Sopenharmony_cixpc_send_activate_IRQ_part_uv(struct xpc_partition *part, void *msg,
7338c2ecf20Sopenharmony_ci			      size_t msg_size, int msg_type)
7348c2ecf20Sopenharmony_ci{
7358c2ecf20Sopenharmony_ci	enum xp_retval ret;
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	ret = xpc_send_activate_IRQ_uv(part, msg, msg_size, msg_type);
7388c2ecf20Sopenharmony_ci	if (unlikely(ret != xpSuccess))
7398c2ecf20Sopenharmony_ci		XPC_DEACTIVATE_PARTITION(part, ret);
7408c2ecf20Sopenharmony_ci}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_cistatic void
7438c2ecf20Sopenharmony_cixpc_send_activate_IRQ_ch_uv(struct xpc_channel *ch, unsigned long *irq_flags,
7448c2ecf20Sopenharmony_ci			 void *msg, size_t msg_size, int msg_type)
7458c2ecf20Sopenharmony_ci{
7468c2ecf20Sopenharmony_ci	struct xpc_partition *part = &xpc_partitions[ch->partid];
7478c2ecf20Sopenharmony_ci	enum xp_retval ret;
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	ret = xpc_send_activate_IRQ_uv(part, msg, msg_size, msg_type);
7508c2ecf20Sopenharmony_ci	if (unlikely(ret != xpSuccess)) {
7518c2ecf20Sopenharmony_ci		if (irq_flags != NULL)
7528c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&ch->lock, *irq_flags);
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci		XPC_DEACTIVATE_PARTITION(part, ret);
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci		if (irq_flags != NULL)
7578c2ecf20Sopenharmony_ci			spin_lock_irqsave(&ch->lock, *irq_flags);
7588c2ecf20Sopenharmony_ci	}
7598c2ecf20Sopenharmony_ci}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_cistatic void
7628c2ecf20Sopenharmony_cixpc_send_local_activate_IRQ_uv(struct xpc_partition *part, int act_state_req)
7638c2ecf20Sopenharmony_ci{
7648c2ecf20Sopenharmony_ci	unsigned long irq_flags;
7658c2ecf20Sopenharmony_ci	struct xpc_partition_uv *part_uv = &part->sn.uv;
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	/*
7688c2ecf20Sopenharmony_ci	 * !!! Make our side think that the remote partition sent an activate
7698c2ecf20Sopenharmony_ci	 * !!! mq message our way by doing what the activate IRQ handler would
7708c2ecf20Sopenharmony_ci	 * !!! do had one really been sent.
7718c2ecf20Sopenharmony_ci	 */
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
7748c2ecf20Sopenharmony_ci	if (part_uv->act_state_req == 0)
7758c2ecf20Sopenharmony_ci		xpc_activate_IRQ_rcvd++;
7768c2ecf20Sopenharmony_ci	part_uv->act_state_req = act_state_req;
7778c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	wake_up_interruptible(&xpc_activate_IRQ_wq);
7808c2ecf20Sopenharmony_ci}
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_cistatic enum xp_retval
7838c2ecf20Sopenharmony_cixpc_get_partition_rsvd_page_pa_uv(void *buf, u64 *cookie, unsigned long *rp_pa,
7848c2ecf20Sopenharmony_ci				  size_t *len)
7858c2ecf20Sopenharmony_ci{
7868c2ecf20Sopenharmony_ci	s64 status;
7878c2ecf20Sopenharmony_ci	enum xp_retval ret;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci#if defined CONFIG_X86_64
7908c2ecf20Sopenharmony_ci	status = uv_bios_reserved_page_pa((u64)buf, cookie, (u64 *)rp_pa,
7918c2ecf20Sopenharmony_ci					  (u64 *)len);
7928c2ecf20Sopenharmony_ci	if (status == BIOS_STATUS_SUCCESS)
7938c2ecf20Sopenharmony_ci		ret = xpSuccess;
7948c2ecf20Sopenharmony_ci	else if (status == BIOS_STATUS_MORE_PASSES)
7958c2ecf20Sopenharmony_ci		ret = xpNeedMoreInfo;
7968c2ecf20Sopenharmony_ci	else
7978c2ecf20Sopenharmony_ci		ret = xpBiosError;
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci#elif defined CONFIG_IA64_SGI_UV
8008c2ecf20Sopenharmony_ci	status = sn_partition_reserved_page_pa((u64)buf, cookie, rp_pa, len);
8018c2ecf20Sopenharmony_ci	if (status == SALRET_OK)
8028c2ecf20Sopenharmony_ci		ret = xpSuccess;
8038c2ecf20Sopenharmony_ci	else if (status == SALRET_MORE_PASSES)
8048c2ecf20Sopenharmony_ci		ret = xpNeedMoreInfo;
8058c2ecf20Sopenharmony_ci	else
8068c2ecf20Sopenharmony_ci		ret = xpSalError;
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci#else
8098c2ecf20Sopenharmony_ci	#error not a supported configuration
8108c2ecf20Sopenharmony_ci#endif
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	return ret;
8138c2ecf20Sopenharmony_ci}
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_cistatic int
8168c2ecf20Sopenharmony_cixpc_setup_rsvd_page_uv(struct xpc_rsvd_page *rp)
8178c2ecf20Sopenharmony_ci{
8188c2ecf20Sopenharmony_ci	xpc_heartbeat_uv =
8198c2ecf20Sopenharmony_ci	    &xpc_partitions[sn_partition_id].sn.uv.cached_heartbeat;
8208c2ecf20Sopenharmony_ci	rp->sn.uv.heartbeat_gpa = uv_gpa(xpc_heartbeat_uv);
8218c2ecf20Sopenharmony_ci	rp->sn.uv.activate_gru_mq_desc_gpa =
8228c2ecf20Sopenharmony_ci	    uv_gpa(xpc_activate_mq_uv->gru_mq_desc);
8238c2ecf20Sopenharmony_ci	return 0;
8248c2ecf20Sopenharmony_ci}
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_cistatic void
8278c2ecf20Sopenharmony_cixpc_allow_hb_uv(short partid)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_cistatic void
8328c2ecf20Sopenharmony_cixpc_disallow_hb_uv(short partid)
8338c2ecf20Sopenharmony_ci{
8348c2ecf20Sopenharmony_ci}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_cistatic void
8378c2ecf20Sopenharmony_cixpc_disallow_all_hbs_uv(void)
8388c2ecf20Sopenharmony_ci{
8398c2ecf20Sopenharmony_ci}
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_cistatic void
8428c2ecf20Sopenharmony_cixpc_increment_heartbeat_uv(void)
8438c2ecf20Sopenharmony_ci{
8448c2ecf20Sopenharmony_ci	xpc_heartbeat_uv->value++;
8458c2ecf20Sopenharmony_ci}
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_cistatic void
8488c2ecf20Sopenharmony_cixpc_offline_heartbeat_uv(void)
8498c2ecf20Sopenharmony_ci{
8508c2ecf20Sopenharmony_ci	xpc_increment_heartbeat_uv();
8518c2ecf20Sopenharmony_ci	xpc_heartbeat_uv->offline = 1;
8528c2ecf20Sopenharmony_ci}
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_cistatic void
8558c2ecf20Sopenharmony_cixpc_online_heartbeat_uv(void)
8568c2ecf20Sopenharmony_ci{
8578c2ecf20Sopenharmony_ci	xpc_increment_heartbeat_uv();
8588c2ecf20Sopenharmony_ci	xpc_heartbeat_uv->offline = 0;
8598c2ecf20Sopenharmony_ci}
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_cistatic void
8628c2ecf20Sopenharmony_cixpc_heartbeat_init_uv(void)
8638c2ecf20Sopenharmony_ci{
8648c2ecf20Sopenharmony_ci	xpc_heartbeat_uv->value = 1;
8658c2ecf20Sopenharmony_ci	xpc_heartbeat_uv->offline = 0;
8668c2ecf20Sopenharmony_ci}
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_cistatic void
8698c2ecf20Sopenharmony_cixpc_heartbeat_exit_uv(void)
8708c2ecf20Sopenharmony_ci{
8718c2ecf20Sopenharmony_ci	xpc_offline_heartbeat_uv();
8728c2ecf20Sopenharmony_ci}
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_cistatic enum xp_retval
8758c2ecf20Sopenharmony_cixpc_get_remote_heartbeat_uv(struct xpc_partition *part)
8768c2ecf20Sopenharmony_ci{
8778c2ecf20Sopenharmony_ci	struct xpc_partition_uv *part_uv = &part->sn.uv;
8788c2ecf20Sopenharmony_ci	enum xp_retval ret;
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	ret = xp_remote_memcpy(uv_gpa(&part_uv->cached_heartbeat),
8818c2ecf20Sopenharmony_ci			       part_uv->heartbeat_gpa,
8828c2ecf20Sopenharmony_ci			       sizeof(struct xpc_heartbeat_uv));
8838c2ecf20Sopenharmony_ci	if (ret != xpSuccess)
8848c2ecf20Sopenharmony_ci		return ret;
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	if (part_uv->cached_heartbeat.value == part->last_heartbeat &&
8878c2ecf20Sopenharmony_ci	    !part_uv->cached_heartbeat.offline) {
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci		ret = xpNoHeartbeat;
8908c2ecf20Sopenharmony_ci	} else {
8918c2ecf20Sopenharmony_ci		part->last_heartbeat = part_uv->cached_heartbeat.value;
8928c2ecf20Sopenharmony_ci	}
8938c2ecf20Sopenharmony_ci	return ret;
8948c2ecf20Sopenharmony_ci}
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_cistatic void
8978c2ecf20Sopenharmony_cixpc_request_partition_activation_uv(struct xpc_rsvd_page *remote_rp,
8988c2ecf20Sopenharmony_ci				    unsigned long remote_rp_gpa, int nasid)
8998c2ecf20Sopenharmony_ci{
9008c2ecf20Sopenharmony_ci	short partid = remote_rp->SAL_partid;
9018c2ecf20Sopenharmony_ci	struct xpc_partition *part = &xpc_partitions[partid];
9028c2ecf20Sopenharmony_ci	struct xpc_activate_mq_msg_activate_req_uv msg;
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	part->remote_rp_pa = remote_rp_gpa; /* !!! _pa here is really _gpa */
9058c2ecf20Sopenharmony_ci	part->remote_rp_ts_jiffies = remote_rp->ts_jiffies;
9068c2ecf20Sopenharmony_ci	part->sn.uv.heartbeat_gpa = remote_rp->sn.uv.heartbeat_gpa;
9078c2ecf20Sopenharmony_ci	part->sn.uv.activate_gru_mq_desc_gpa =
9088c2ecf20Sopenharmony_ci	    remote_rp->sn.uv.activate_gru_mq_desc_gpa;
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	/*
9118c2ecf20Sopenharmony_ci	 * ??? Is it a good idea to make this conditional on what is
9128c2ecf20Sopenharmony_ci	 * ??? potentially stale state information?
9138c2ecf20Sopenharmony_ci	 */
9148c2ecf20Sopenharmony_ci	if (part->sn.uv.remote_act_state == XPC_P_AS_INACTIVE) {
9158c2ecf20Sopenharmony_ci		msg.rp_gpa = uv_gpa(xpc_rsvd_page);
9168c2ecf20Sopenharmony_ci		msg.heartbeat_gpa = xpc_rsvd_page->sn.uv.heartbeat_gpa;
9178c2ecf20Sopenharmony_ci		msg.activate_gru_mq_desc_gpa =
9188c2ecf20Sopenharmony_ci		    xpc_rsvd_page->sn.uv.activate_gru_mq_desc_gpa;
9198c2ecf20Sopenharmony_ci		xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
9208c2ecf20Sopenharmony_ci					   XPC_ACTIVATE_MQ_MSG_ACTIVATE_REQ_UV);
9218c2ecf20Sopenharmony_ci	}
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	if (part->act_state == XPC_P_AS_INACTIVE)
9248c2ecf20Sopenharmony_ci		xpc_send_local_activate_IRQ_uv(part, XPC_P_ASR_ACTIVATE_UV);
9258c2ecf20Sopenharmony_ci}
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_cistatic void
9288c2ecf20Sopenharmony_cixpc_request_partition_reactivation_uv(struct xpc_partition *part)
9298c2ecf20Sopenharmony_ci{
9308c2ecf20Sopenharmony_ci	xpc_send_local_activate_IRQ_uv(part, XPC_P_ASR_ACTIVATE_UV);
9318c2ecf20Sopenharmony_ci}
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_cistatic void
9348c2ecf20Sopenharmony_cixpc_request_partition_deactivation_uv(struct xpc_partition *part)
9358c2ecf20Sopenharmony_ci{
9368c2ecf20Sopenharmony_ci	struct xpc_activate_mq_msg_deactivate_req_uv msg;
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	/*
9398c2ecf20Sopenharmony_ci	 * ??? Is it a good idea to make this conditional on what is
9408c2ecf20Sopenharmony_ci	 * ??? potentially stale state information?
9418c2ecf20Sopenharmony_ci	 */
9428c2ecf20Sopenharmony_ci	if (part->sn.uv.remote_act_state != XPC_P_AS_DEACTIVATING &&
9438c2ecf20Sopenharmony_ci	    part->sn.uv.remote_act_state != XPC_P_AS_INACTIVE) {
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci		msg.reason = part->reason;
9468c2ecf20Sopenharmony_ci		xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
9478c2ecf20Sopenharmony_ci					 XPC_ACTIVATE_MQ_MSG_DEACTIVATE_REQ_UV);
9488c2ecf20Sopenharmony_ci	}
9498c2ecf20Sopenharmony_ci}
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_cistatic void
9528c2ecf20Sopenharmony_cixpc_cancel_partition_deactivation_request_uv(struct xpc_partition *part)
9538c2ecf20Sopenharmony_ci{
9548c2ecf20Sopenharmony_ci	/* nothing needs to be done */
9558c2ecf20Sopenharmony_ci	return;
9568c2ecf20Sopenharmony_ci}
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_cistatic void
9598c2ecf20Sopenharmony_cixpc_init_fifo_uv(struct xpc_fifo_head_uv *head)
9608c2ecf20Sopenharmony_ci{
9618c2ecf20Sopenharmony_ci	head->first = NULL;
9628c2ecf20Sopenharmony_ci	head->last = NULL;
9638c2ecf20Sopenharmony_ci	spin_lock_init(&head->lock);
9648c2ecf20Sopenharmony_ci	head->n_entries = 0;
9658c2ecf20Sopenharmony_ci}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_cistatic void *
9688c2ecf20Sopenharmony_cixpc_get_fifo_entry_uv(struct xpc_fifo_head_uv *head)
9698c2ecf20Sopenharmony_ci{
9708c2ecf20Sopenharmony_ci	unsigned long irq_flags;
9718c2ecf20Sopenharmony_ci	struct xpc_fifo_entry_uv *first;
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci	spin_lock_irqsave(&head->lock, irq_flags);
9748c2ecf20Sopenharmony_ci	first = head->first;
9758c2ecf20Sopenharmony_ci	if (head->first != NULL) {
9768c2ecf20Sopenharmony_ci		head->first = first->next;
9778c2ecf20Sopenharmony_ci		if (head->first == NULL)
9788c2ecf20Sopenharmony_ci			head->last = NULL;
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci		head->n_entries--;
9818c2ecf20Sopenharmony_ci		BUG_ON(head->n_entries < 0);
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci		first->next = NULL;
9848c2ecf20Sopenharmony_ci	}
9858c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&head->lock, irq_flags);
9868c2ecf20Sopenharmony_ci	return first;
9878c2ecf20Sopenharmony_ci}
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_cistatic void
9908c2ecf20Sopenharmony_cixpc_put_fifo_entry_uv(struct xpc_fifo_head_uv *head,
9918c2ecf20Sopenharmony_ci		      struct xpc_fifo_entry_uv *last)
9928c2ecf20Sopenharmony_ci{
9938c2ecf20Sopenharmony_ci	unsigned long irq_flags;
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	last->next = NULL;
9968c2ecf20Sopenharmony_ci	spin_lock_irqsave(&head->lock, irq_flags);
9978c2ecf20Sopenharmony_ci	if (head->last != NULL)
9988c2ecf20Sopenharmony_ci		head->last->next = last;
9998c2ecf20Sopenharmony_ci	else
10008c2ecf20Sopenharmony_ci		head->first = last;
10018c2ecf20Sopenharmony_ci	head->last = last;
10028c2ecf20Sopenharmony_ci	head->n_entries++;
10038c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&head->lock, irq_flags);
10048c2ecf20Sopenharmony_ci}
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_cistatic int
10078c2ecf20Sopenharmony_cixpc_n_of_fifo_entries_uv(struct xpc_fifo_head_uv *head)
10088c2ecf20Sopenharmony_ci{
10098c2ecf20Sopenharmony_ci	return head->n_entries;
10108c2ecf20Sopenharmony_ci}
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci/*
10138c2ecf20Sopenharmony_ci * Setup the channel structures that are uv specific.
10148c2ecf20Sopenharmony_ci */
10158c2ecf20Sopenharmony_cistatic enum xp_retval
10168c2ecf20Sopenharmony_cixpc_setup_ch_structures_uv(struct xpc_partition *part)
10178c2ecf20Sopenharmony_ci{
10188c2ecf20Sopenharmony_ci	struct xpc_channel_uv *ch_uv;
10198c2ecf20Sopenharmony_ci	int ch_number;
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
10228c2ecf20Sopenharmony_ci		ch_uv = &part->channels[ch_number].sn.uv;
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci		xpc_init_fifo_uv(&ch_uv->msg_slot_free_list);
10258c2ecf20Sopenharmony_ci		xpc_init_fifo_uv(&ch_uv->recv_msg_list);
10268c2ecf20Sopenharmony_ci	}
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	return xpSuccess;
10298c2ecf20Sopenharmony_ci}
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci/*
10328c2ecf20Sopenharmony_ci * Teardown the channel structures that are uv specific.
10338c2ecf20Sopenharmony_ci */
10348c2ecf20Sopenharmony_cistatic void
10358c2ecf20Sopenharmony_cixpc_teardown_ch_structures_uv(struct xpc_partition *part)
10368c2ecf20Sopenharmony_ci{
10378c2ecf20Sopenharmony_ci	/* nothing needs to be done */
10388c2ecf20Sopenharmony_ci	return;
10398c2ecf20Sopenharmony_ci}
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_cistatic enum xp_retval
10428c2ecf20Sopenharmony_cixpc_make_first_contact_uv(struct xpc_partition *part)
10438c2ecf20Sopenharmony_ci{
10448c2ecf20Sopenharmony_ci	struct xpc_activate_mq_msg_uv msg;
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci	/*
10478c2ecf20Sopenharmony_ci	 * We send a sync msg to get the remote partition's remote_act_state
10488c2ecf20Sopenharmony_ci	 * updated to our current act_state which at this point should
10498c2ecf20Sopenharmony_ci	 * be XPC_P_AS_ACTIVATING.
10508c2ecf20Sopenharmony_ci	 */
10518c2ecf20Sopenharmony_ci	xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
10528c2ecf20Sopenharmony_ci				      XPC_ACTIVATE_MQ_MSG_SYNC_ACT_STATE_UV);
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_ci	while (!((part->sn.uv.remote_act_state == XPC_P_AS_ACTIVATING) ||
10558c2ecf20Sopenharmony_ci		 (part->sn.uv.remote_act_state == XPC_P_AS_ACTIVE))) {
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci		dev_dbg(xpc_part, "waiting to make first contact with "
10588c2ecf20Sopenharmony_ci			"partition %d\n", XPC_PARTID(part));
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci		/* wait a 1/4 of a second or so */
10618c2ecf20Sopenharmony_ci		(void)msleep_interruptible(250);
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci		if (part->act_state == XPC_P_AS_DEACTIVATING)
10648c2ecf20Sopenharmony_ci			return part->reason;
10658c2ecf20Sopenharmony_ci	}
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci	return xpSuccess;
10688c2ecf20Sopenharmony_ci}
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_cistatic u64
10718c2ecf20Sopenharmony_cixpc_get_chctl_all_flags_uv(struct xpc_partition *part)
10728c2ecf20Sopenharmony_ci{
10738c2ecf20Sopenharmony_ci	unsigned long irq_flags;
10748c2ecf20Sopenharmony_ci	union xpc_channel_ctl_flags chctl;
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci	spin_lock_irqsave(&part->chctl_lock, irq_flags);
10778c2ecf20Sopenharmony_ci	chctl = part->chctl;
10788c2ecf20Sopenharmony_ci	if (chctl.all_flags != 0)
10798c2ecf20Sopenharmony_ci		part->chctl.all_flags = 0;
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
10828c2ecf20Sopenharmony_ci	return chctl.all_flags;
10838c2ecf20Sopenharmony_ci}
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_cistatic enum xp_retval
10868c2ecf20Sopenharmony_cixpc_allocate_send_msg_slot_uv(struct xpc_channel *ch)
10878c2ecf20Sopenharmony_ci{
10888c2ecf20Sopenharmony_ci	struct xpc_channel_uv *ch_uv = &ch->sn.uv;
10898c2ecf20Sopenharmony_ci	struct xpc_send_msg_slot_uv *msg_slot;
10908c2ecf20Sopenharmony_ci	unsigned long irq_flags;
10918c2ecf20Sopenharmony_ci	int nentries;
10928c2ecf20Sopenharmony_ci	int entry;
10938c2ecf20Sopenharmony_ci	size_t nbytes;
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ci	for (nentries = ch->local_nentries; nentries > 0; nentries--) {
10968c2ecf20Sopenharmony_ci		nbytes = nentries * sizeof(struct xpc_send_msg_slot_uv);
10978c2ecf20Sopenharmony_ci		ch_uv->send_msg_slots = kzalloc(nbytes, GFP_KERNEL);
10988c2ecf20Sopenharmony_ci		if (ch_uv->send_msg_slots == NULL)
10998c2ecf20Sopenharmony_ci			continue;
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci		for (entry = 0; entry < nentries; entry++) {
11028c2ecf20Sopenharmony_ci			msg_slot = &ch_uv->send_msg_slots[entry];
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci			msg_slot->msg_slot_number = entry;
11058c2ecf20Sopenharmony_ci			xpc_put_fifo_entry_uv(&ch_uv->msg_slot_free_list,
11068c2ecf20Sopenharmony_ci					      &msg_slot->next);
11078c2ecf20Sopenharmony_ci		}
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ch->lock, irq_flags);
11108c2ecf20Sopenharmony_ci		if (nentries < ch->local_nentries)
11118c2ecf20Sopenharmony_ci			ch->local_nentries = nentries;
11128c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ch->lock, irq_flags);
11138c2ecf20Sopenharmony_ci		return xpSuccess;
11148c2ecf20Sopenharmony_ci	}
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ci	return xpNoMemory;
11178c2ecf20Sopenharmony_ci}
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_cistatic enum xp_retval
11208c2ecf20Sopenharmony_cixpc_allocate_recv_msg_slot_uv(struct xpc_channel *ch)
11218c2ecf20Sopenharmony_ci{
11228c2ecf20Sopenharmony_ci	struct xpc_channel_uv *ch_uv = &ch->sn.uv;
11238c2ecf20Sopenharmony_ci	struct xpc_notify_mq_msg_uv *msg_slot;
11248c2ecf20Sopenharmony_ci	unsigned long irq_flags;
11258c2ecf20Sopenharmony_ci	int nentries;
11268c2ecf20Sopenharmony_ci	int entry;
11278c2ecf20Sopenharmony_ci	size_t nbytes;
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
11308c2ecf20Sopenharmony_ci		nbytes = nentries * ch->entry_size;
11318c2ecf20Sopenharmony_ci		ch_uv->recv_msg_slots = kzalloc(nbytes, GFP_KERNEL);
11328c2ecf20Sopenharmony_ci		if (ch_uv->recv_msg_slots == NULL)
11338c2ecf20Sopenharmony_ci			continue;
11348c2ecf20Sopenharmony_ci
11358c2ecf20Sopenharmony_ci		for (entry = 0; entry < nentries; entry++) {
11368c2ecf20Sopenharmony_ci			msg_slot = ch_uv->recv_msg_slots +
11378c2ecf20Sopenharmony_ci			    entry * ch->entry_size;
11388c2ecf20Sopenharmony_ci
11398c2ecf20Sopenharmony_ci			msg_slot->hdr.msg_slot_number = entry;
11408c2ecf20Sopenharmony_ci		}
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ch->lock, irq_flags);
11438c2ecf20Sopenharmony_ci		if (nentries < ch->remote_nentries)
11448c2ecf20Sopenharmony_ci			ch->remote_nentries = nentries;
11458c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ch->lock, irq_flags);
11468c2ecf20Sopenharmony_ci		return xpSuccess;
11478c2ecf20Sopenharmony_ci	}
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci	return xpNoMemory;
11508c2ecf20Sopenharmony_ci}
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci/*
11538c2ecf20Sopenharmony_ci * Allocate msg_slots associated with the channel.
11548c2ecf20Sopenharmony_ci */
11558c2ecf20Sopenharmony_cistatic enum xp_retval
11568c2ecf20Sopenharmony_cixpc_setup_msg_structures_uv(struct xpc_channel *ch)
11578c2ecf20Sopenharmony_ci{
11588c2ecf20Sopenharmony_ci	static enum xp_retval ret;
11598c2ecf20Sopenharmony_ci	struct xpc_channel_uv *ch_uv = &ch->sn.uv;
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	DBUG_ON(ch->flags & XPC_C_SETUP);
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	ch_uv->cached_notify_gru_mq_desc = kmalloc(sizeof(struct
11648c2ecf20Sopenharmony_ci						   gru_message_queue_desc),
11658c2ecf20Sopenharmony_ci						   GFP_KERNEL);
11668c2ecf20Sopenharmony_ci	if (ch_uv->cached_notify_gru_mq_desc == NULL)
11678c2ecf20Sopenharmony_ci		return xpNoMemory;
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_ci	ret = xpc_allocate_send_msg_slot_uv(ch);
11708c2ecf20Sopenharmony_ci	if (ret == xpSuccess) {
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci		ret = xpc_allocate_recv_msg_slot_uv(ch);
11738c2ecf20Sopenharmony_ci		if (ret != xpSuccess) {
11748c2ecf20Sopenharmony_ci			kfree(ch_uv->send_msg_slots);
11758c2ecf20Sopenharmony_ci			xpc_init_fifo_uv(&ch_uv->msg_slot_free_list);
11768c2ecf20Sopenharmony_ci		}
11778c2ecf20Sopenharmony_ci	}
11788c2ecf20Sopenharmony_ci	return ret;
11798c2ecf20Sopenharmony_ci}
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_ci/*
11828c2ecf20Sopenharmony_ci * Free up msg_slots and clear other stuff that were setup for the specified
11838c2ecf20Sopenharmony_ci * channel.
11848c2ecf20Sopenharmony_ci */
11858c2ecf20Sopenharmony_cistatic void
11868c2ecf20Sopenharmony_cixpc_teardown_msg_structures_uv(struct xpc_channel *ch)
11878c2ecf20Sopenharmony_ci{
11888c2ecf20Sopenharmony_ci	struct xpc_channel_uv *ch_uv = &ch->sn.uv;
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci	lockdep_assert_held(&ch->lock);
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	kfree(ch_uv->cached_notify_gru_mq_desc);
11938c2ecf20Sopenharmony_ci	ch_uv->cached_notify_gru_mq_desc = NULL;
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci	if (ch->flags & XPC_C_SETUP) {
11968c2ecf20Sopenharmony_ci		xpc_init_fifo_uv(&ch_uv->msg_slot_free_list);
11978c2ecf20Sopenharmony_ci		kfree(ch_uv->send_msg_slots);
11988c2ecf20Sopenharmony_ci		xpc_init_fifo_uv(&ch_uv->recv_msg_list);
11998c2ecf20Sopenharmony_ci		kfree(ch_uv->recv_msg_slots);
12008c2ecf20Sopenharmony_ci	}
12018c2ecf20Sopenharmony_ci}
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_cistatic void
12048c2ecf20Sopenharmony_cixpc_send_chctl_closerequest_uv(struct xpc_channel *ch, unsigned long *irq_flags)
12058c2ecf20Sopenharmony_ci{
12068c2ecf20Sopenharmony_ci	struct xpc_activate_mq_msg_chctl_closerequest_uv msg;
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci	msg.ch_number = ch->number;
12098c2ecf20Sopenharmony_ci	msg.reason = ch->reason;
12108c2ecf20Sopenharmony_ci	xpc_send_activate_IRQ_ch_uv(ch, irq_flags, &msg, sizeof(msg),
12118c2ecf20Sopenharmony_ci				    XPC_ACTIVATE_MQ_MSG_CHCTL_CLOSEREQUEST_UV);
12128c2ecf20Sopenharmony_ci}
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_cistatic void
12158c2ecf20Sopenharmony_cixpc_send_chctl_closereply_uv(struct xpc_channel *ch, unsigned long *irq_flags)
12168c2ecf20Sopenharmony_ci{
12178c2ecf20Sopenharmony_ci	struct xpc_activate_mq_msg_chctl_closereply_uv msg;
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci	msg.ch_number = ch->number;
12208c2ecf20Sopenharmony_ci	xpc_send_activate_IRQ_ch_uv(ch, irq_flags, &msg, sizeof(msg),
12218c2ecf20Sopenharmony_ci				    XPC_ACTIVATE_MQ_MSG_CHCTL_CLOSEREPLY_UV);
12228c2ecf20Sopenharmony_ci}
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_cistatic void
12258c2ecf20Sopenharmony_cixpc_send_chctl_openrequest_uv(struct xpc_channel *ch, unsigned long *irq_flags)
12268c2ecf20Sopenharmony_ci{
12278c2ecf20Sopenharmony_ci	struct xpc_activate_mq_msg_chctl_openrequest_uv msg;
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_ci	msg.ch_number = ch->number;
12308c2ecf20Sopenharmony_ci	msg.entry_size = ch->entry_size;
12318c2ecf20Sopenharmony_ci	msg.local_nentries = ch->local_nentries;
12328c2ecf20Sopenharmony_ci	xpc_send_activate_IRQ_ch_uv(ch, irq_flags, &msg, sizeof(msg),
12338c2ecf20Sopenharmony_ci				    XPC_ACTIVATE_MQ_MSG_CHCTL_OPENREQUEST_UV);
12348c2ecf20Sopenharmony_ci}
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_cistatic void
12378c2ecf20Sopenharmony_cixpc_send_chctl_openreply_uv(struct xpc_channel *ch, unsigned long *irq_flags)
12388c2ecf20Sopenharmony_ci{
12398c2ecf20Sopenharmony_ci	struct xpc_activate_mq_msg_chctl_openreply_uv msg;
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	msg.ch_number = ch->number;
12428c2ecf20Sopenharmony_ci	msg.local_nentries = ch->local_nentries;
12438c2ecf20Sopenharmony_ci	msg.remote_nentries = ch->remote_nentries;
12448c2ecf20Sopenharmony_ci	msg.notify_gru_mq_desc_gpa = uv_gpa(xpc_notify_mq_uv->gru_mq_desc);
12458c2ecf20Sopenharmony_ci	xpc_send_activate_IRQ_ch_uv(ch, irq_flags, &msg, sizeof(msg),
12468c2ecf20Sopenharmony_ci				    XPC_ACTIVATE_MQ_MSG_CHCTL_OPENREPLY_UV);
12478c2ecf20Sopenharmony_ci}
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_cistatic void
12508c2ecf20Sopenharmony_cixpc_send_chctl_opencomplete_uv(struct xpc_channel *ch, unsigned long *irq_flags)
12518c2ecf20Sopenharmony_ci{
12528c2ecf20Sopenharmony_ci	struct xpc_activate_mq_msg_chctl_opencomplete_uv msg;
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci	msg.ch_number = ch->number;
12558c2ecf20Sopenharmony_ci	xpc_send_activate_IRQ_ch_uv(ch, irq_flags, &msg, sizeof(msg),
12568c2ecf20Sopenharmony_ci				    XPC_ACTIVATE_MQ_MSG_CHCTL_OPENCOMPLETE_UV);
12578c2ecf20Sopenharmony_ci}
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_cistatic void
12608c2ecf20Sopenharmony_cixpc_send_chctl_local_msgrequest_uv(struct xpc_partition *part, int ch_number)
12618c2ecf20Sopenharmony_ci{
12628c2ecf20Sopenharmony_ci	unsigned long irq_flags;
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci	spin_lock_irqsave(&part->chctl_lock, irq_flags);
12658c2ecf20Sopenharmony_ci	part->chctl.flags[ch_number] |= XPC_CHCTL_MSGREQUEST;
12668c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci	xpc_wakeup_channel_mgr(part);
12698c2ecf20Sopenharmony_ci}
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_cistatic enum xp_retval
12728c2ecf20Sopenharmony_cixpc_save_remote_msgqueue_pa_uv(struct xpc_channel *ch,
12738c2ecf20Sopenharmony_ci			       unsigned long gru_mq_desc_gpa)
12748c2ecf20Sopenharmony_ci{
12758c2ecf20Sopenharmony_ci	struct xpc_channel_uv *ch_uv = &ch->sn.uv;
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci	DBUG_ON(ch_uv->cached_notify_gru_mq_desc == NULL);
12788c2ecf20Sopenharmony_ci	return xpc_cache_remote_gru_mq_desc_uv(ch_uv->cached_notify_gru_mq_desc,
12798c2ecf20Sopenharmony_ci					       gru_mq_desc_gpa);
12808c2ecf20Sopenharmony_ci}
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_cistatic void
12838c2ecf20Sopenharmony_cixpc_indicate_partition_engaged_uv(struct xpc_partition *part)
12848c2ecf20Sopenharmony_ci{
12858c2ecf20Sopenharmony_ci	struct xpc_activate_mq_msg_uv msg;
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_ci	xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
12888c2ecf20Sopenharmony_ci				      XPC_ACTIVATE_MQ_MSG_MARK_ENGAGED_UV);
12898c2ecf20Sopenharmony_ci}
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_cistatic void
12928c2ecf20Sopenharmony_cixpc_indicate_partition_disengaged_uv(struct xpc_partition *part)
12938c2ecf20Sopenharmony_ci{
12948c2ecf20Sopenharmony_ci	struct xpc_activate_mq_msg_uv msg;
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
12978c2ecf20Sopenharmony_ci				      XPC_ACTIVATE_MQ_MSG_MARK_DISENGAGED_UV);
12988c2ecf20Sopenharmony_ci}
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_cistatic void
13018c2ecf20Sopenharmony_cixpc_assume_partition_disengaged_uv(short partid)
13028c2ecf20Sopenharmony_ci{
13038c2ecf20Sopenharmony_ci	struct xpc_partition_uv *part_uv = &xpc_partitions[partid].sn.uv;
13048c2ecf20Sopenharmony_ci	unsigned long irq_flags;
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci	spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
13078c2ecf20Sopenharmony_ci	part_uv->flags &= ~XPC_P_ENGAGED_UV;
13088c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
13098c2ecf20Sopenharmony_ci}
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_cistatic int
13128c2ecf20Sopenharmony_cixpc_partition_engaged_uv(short partid)
13138c2ecf20Sopenharmony_ci{
13148c2ecf20Sopenharmony_ci	return (xpc_partitions[partid].sn.uv.flags & XPC_P_ENGAGED_UV) != 0;
13158c2ecf20Sopenharmony_ci}
13168c2ecf20Sopenharmony_ci
13178c2ecf20Sopenharmony_cistatic int
13188c2ecf20Sopenharmony_cixpc_any_partition_engaged_uv(void)
13198c2ecf20Sopenharmony_ci{
13208c2ecf20Sopenharmony_ci	struct xpc_partition_uv *part_uv;
13218c2ecf20Sopenharmony_ci	short partid;
13228c2ecf20Sopenharmony_ci
13238c2ecf20Sopenharmony_ci	for (partid = 0; partid < XP_MAX_NPARTITIONS_UV; partid++) {
13248c2ecf20Sopenharmony_ci		part_uv = &xpc_partitions[partid].sn.uv;
13258c2ecf20Sopenharmony_ci		if ((part_uv->flags & XPC_P_ENGAGED_UV) != 0)
13268c2ecf20Sopenharmony_ci			return 1;
13278c2ecf20Sopenharmony_ci	}
13288c2ecf20Sopenharmony_ci	return 0;
13298c2ecf20Sopenharmony_ci}
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_cistatic enum xp_retval
13328c2ecf20Sopenharmony_cixpc_allocate_msg_slot_uv(struct xpc_channel *ch, u32 flags,
13338c2ecf20Sopenharmony_ci			 struct xpc_send_msg_slot_uv **address_of_msg_slot)
13348c2ecf20Sopenharmony_ci{
13358c2ecf20Sopenharmony_ci	enum xp_retval ret;
13368c2ecf20Sopenharmony_ci	struct xpc_send_msg_slot_uv *msg_slot;
13378c2ecf20Sopenharmony_ci	struct xpc_fifo_entry_uv *entry;
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci	while (1) {
13408c2ecf20Sopenharmony_ci		entry = xpc_get_fifo_entry_uv(&ch->sn.uv.msg_slot_free_list);
13418c2ecf20Sopenharmony_ci		if (entry != NULL)
13428c2ecf20Sopenharmony_ci			break;
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci		if (flags & XPC_NOWAIT)
13458c2ecf20Sopenharmony_ci			return xpNoWait;
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_ci		ret = xpc_allocate_msg_wait(ch);
13488c2ecf20Sopenharmony_ci		if (ret != xpInterrupted && ret != xpTimeout)
13498c2ecf20Sopenharmony_ci			return ret;
13508c2ecf20Sopenharmony_ci	}
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ci	msg_slot = container_of(entry, struct xpc_send_msg_slot_uv, next);
13538c2ecf20Sopenharmony_ci	*address_of_msg_slot = msg_slot;
13548c2ecf20Sopenharmony_ci	return xpSuccess;
13558c2ecf20Sopenharmony_ci}
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_cistatic void
13588c2ecf20Sopenharmony_cixpc_free_msg_slot_uv(struct xpc_channel *ch,
13598c2ecf20Sopenharmony_ci		     struct xpc_send_msg_slot_uv *msg_slot)
13608c2ecf20Sopenharmony_ci{
13618c2ecf20Sopenharmony_ci	xpc_put_fifo_entry_uv(&ch->sn.uv.msg_slot_free_list, &msg_slot->next);
13628c2ecf20Sopenharmony_ci
13638c2ecf20Sopenharmony_ci	/* wakeup anyone waiting for a free msg slot */
13648c2ecf20Sopenharmony_ci	if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
13658c2ecf20Sopenharmony_ci		wake_up(&ch->msg_allocate_wq);
13668c2ecf20Sopenharmony_ci}
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_cistatic void
13698c2ecf20Sopenharmony_cixpc_notify_sender_uv(struct xpc_channel *ch,
13708c2ecf20Sopenharmony_ci		     struct xpc_send_msg_slot_uv *msg_slot,
13718c2ecf20Sopenharmony_ci		     enum xp_retval reason)
13728c2ecf20Sopenharmony_ci{
13738c2ecf20Sopenharmony_ci	xpc_notify_func func = msg_slot->func;
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci	if (func != NULL && cmpxchg(&msg_slot->func, func, NULL) == func) {
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci		atomic_dec(&ch->n_to_notify);
13788c2ecf20Sopenharmony_ci
13798c2ecf20Sopenharmony_ci		dev_dbg(xpc_chan, "msg_slot->func() called, msg_slot=0x%p "
13808c2ecf20Sopenharmony_ci			"msg_slot_number=%d partid=%d channel=%d\n", msg_slot,
13818c2ecf20Sopenharmony_ci			msg_slot->msg_slot_number, ch->partid, ch->number);
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_ci		func(reason, ch->partid, ch->number, msg_slot->key);
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ci		dev_dbg(xpc_chan, "msg_slot->func() returned, msg_slot=0x%p "
13868c2ecf20Sopenharmony_ci			"msg_slot_number=%d partid=%d channel=%d\n", msg_slot,
13878c2ecf20Sopenharmony_ci			msg_slot->msg_slot_number, ch->partid, ch->number);
13888c2ecf20Sopenharmony_ci	}
13898c2ecf20Sopenharmony_ci}
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_cistatic void
13928c2ecf20Sopenharmony_cixpc_handle_notify_mq_ack_uv(struct xpc_channel *ch,
13938c2ecf20Sopenharmony_ci			    struct xpc_notify_mq_msg_uv *msg)
13948c2ecf20Sopenharmony_ci{
13958c2ecf20Sopenharmony_ci	struct xpc_send_msg_slot_uv *msg_slot;
13968c2ecf20Sopenharmony_ci	int entry = msg->hdr.msg_slot_number % ch->local_nentries;
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci	msg_slot = &ch->sn.uv.send_msg_slots[entry];
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci	BUG_ON(msg_slot->msg_slot_number != msg->hdr.msg_slot_number);
14018c2ecf20Sopenharmony_ci	msg_slot->msg_slot_number += ch->local_nentries;
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_ci	if (msg_slot->func != NULL)
14048c2ecf20Sopenharmony_ci		xpc_notify_sender_uv(ch, msg_slot, xpMsgDelivered);
14058c2ecf20Sopenharmony_ci
14068c2ecf20Sopenharmony_ci	xpc_free_msg_slot_uv(ch, msg_slot);
14078c2ecf20Sopenharmony_ci}
14088c2ecf20Sopenharmony_ci
14098c2ecf20Sopenharmony_cistatic void
14108c2ecf20Sopenharmony_cixpc_handle_notify_mq_msg_uv(struct xpc_partition *part,
14118c2ecf20Sopenharmony_ci			    struct xpc_notify_mq_msg_uv *msg)
14128c2ecf20Sopenharmony_ci{
14138c2ecf20Sopenharmony_ci	struct xpc_partition_uv *part_uv = &part->sn.uv;
14148c2ecf20Sopenharmony_ci	struct xpc_channel *ch;
14158c2ecf20Sopenharmony_ci	struct xpc_channel_uv *ch_uv;
14168c2ecf20Sopenharmony_ci	struct xpc_notify_mq_msg_uv *msg_slot;
14178c2ecf20Sopenharmony_ci	unsigned long irq_flags;
14188c2ecf20Sopenharmony_ci	int ch_number = msg->hdr.ch_number;
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	if (unlikely(ch_number >= part->nchannels)) {
14218c2ecf20Sopenharmony_ci		dev_err(xpc_part, "xpc_handle_notify_IRQ_uv() received invalid "
14228c2ecf20Sopenharmony_ci			"channel number=0x%x in message from partid=%d\n",
14238c2ecf20Sopenharmony_ci			ch_number, XPC_PARTID(part));
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_ci		/* get hb checker to deactivate from the remote partition */
14268c2ecf20Sopenharmony_ci		spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
14278c2ecf20Sopenharmony_ci		if (part_uv->act_state_req == 0)
14288c2ecf20Sopenharmony_ci			xpc_activate_IRQ_rcvd++;
14298c2ecf20Sopenharmony_ci		part_uv->act_state_req = XPC_P_ASR_DEACTIVATE_UV;
14308c2ecf20Sopenharmony_ci		part_uv->reason = xpBadChannelNumber;
14318c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci		wake_up_interruptible(&xpc_activate_IRQ_wq);
14348c2ecf20Sopenharmony_ci		return;
14358c2ecf20Sopenharmony_ci	}
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_ci	ch = &part->channels[ch_number];
14388c2ecf20Sopenharmony_ci	xpc_msgqueue_ref(ch);
14398c2ecf20Sopenharmony_ci
14408c2ecf20Sopenharmony_ci	if (!(ch->flags & XPC_C_CONNECTED)) {
14418c2ecf20Sopenharmony_ci		xpc_msgqueue_deref(ch);
14428c2ecf20Sopenharmony_ci		return;
14438c2ecf20Sopenharmony_ci	}
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_ci	/* see if we're really dealing with an ACK for a previously sent msg */
14468c2ecf20Sopenharmony_ci	if (msg->hdr.size == 0) {
14478c2ecf20Sopenharmony_ci		xpc_handle_notify_mq_ack_uv(ch, msg);
14488c2ecf20Sopenharmony_ci		xpc_msgqueue_deref(ch);
14498c2ecf20Sopenharmony_ci		return;
14508c2ecf20Sopenharmony_ci	}
14518c2ecf20Sopenharmony_ci
14528c2ecf20Sopenharmony_ci	/* we're dealing with a normal message sent via the notify_mq */
14538c2ecf20Sopenharmony_ci	ch_uv = &ch->sn.uv;
14548c2ecf20Sopenharmony_ci
14558c2ecf20Sopenharmony_ci	msg_slot = ch_uv->recv_msg_slots +
14568c2ecf20Sopenharmony_ci	    (msg->hdr.msg_slot_number % ch->remote_nentries) * ch->entry_size;
14578c2ecf20Sopenharmony_ci
14588c2ecf20Sopenharmony_ci	BUG_ON(msg_slot->hdr.size != 0);
14598c2ecf20Sopenharmony_ci
14608c2ecf20Sopenharmony_ci	memcpy(msg_slot, msg, msg->hdr.size);
14618c2ecf20Sopenharmony_ci
14628c2ecf20Sopenharmony_ci	xpc_put_fifo_entry_uv(&ch_uv->recv_msg_list, &msg_slot->hdr.u.next);
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci	if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) {
14658c2ecf20Sopenharmony_ci		/*
14668c2ecf20Sopenharmony_ci		 * If there is an existing idle kthread get it to deliver
14678c2ecf20Sopenharmony_ci		 * the payload, otherwise we'll have to get the channel mgr
14688c2ecf20Sopenharmony_ci		 * for this partition to create a kthread to do the delivery.
14698c2ecf20Sopenharmony_ci		 */
14708c2ecf20Sopenharmony_ci		if (atomic_read(&ch->kthreads_idle) > 0)
14718c2ecf20Sopenharmony_ci			wake_up_nr(&ch->idle_wq, 1);
14728c2ecf20Sopenharmony_ci		else
14738c2ecf20Sopenharmony_ci			xpc_send_chctl_local_msgrequest_uv(part, ch->number);
14748c2ecf20Sopenharmony_ci	}
14758c2ecf20Sopenharmony_ci	xpc_msgqueue_deref(ch);
14768c2ecf20Sopenharmony_ci}
14778c2ecf20Sopenharmony_ci
14788c2ecf20Sopenharmony_cistatic irqreturn_t
14798c2ecf20Sopenharmony_cixpc_handle_notify_IRQ_uv(int irq, void *dev_id)
14808c2ecf20Sopenharmony_ci{
14818c2ecf20Sopenharmony_ci	struct xpc_notify_mq_msg_uv *msg;
14828c2ecf20Sopenharmony_ci	short partid;
14838c2ecf20Sopenharmony_ci	struct xpc_partition *part;
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci	while ((msg = gru_get_next_message(xpc_notify_mq_uv->gru_mq_desc)) !=
14868c2ecf20Sopenharmony_ci	       NULL) {
14878c2ecf20Sopenharmony_ci
14888c2ecf20Sopenharmony_ci		partid = msg->hdr.partid;
14898c2ecf20Sopenharmony_ci		if (partid < 0 || partid >= XP_MAX_NPARTITIONS_UV) {
14908c2ecf20Sopenharmony_ci			dev_err(xpc_part, "xpc_handle_notify_IRQ_uv() received "
14918c2ecf20Sopenharmony_ci				"invalid partid=0x%x in message\n", partid);
14928c2ecf20Sopenharmony_ci		} else {
14938c2ecf20Sopenharmony_ci			part = &xpc_partitions[partid];
14948c2ecf20Sopenharmony_ci
14958c2ecf20Sopenharmony_ci			if (xpc_part_ref(part)) {
14968c2ecf20Sopenharmony_ci				xpc_handle_notify_mq_msg_uv(part, msg);
14978c2ecf20Sopenharmony_ci				xpc_part_deref(part);
14988c2ecf20Sopenharmony_ci			}
14998c2ecf20Sopenharmony_ci		}
15008c2ecf20Sopenharmony_ci
15018c2ecf20Sopenharmony_ci		gru_free_message(xpc_notify_mq_uv->gru_mq_desc, msg);
15028c2ecf20Sopenharmony_ci	}
15038c2ecf20Sopenharmony_ci
15048c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
15058c2ecf20Sopenharmony_ci}
15068c2ecf20Sopenharmony_ci
15078c2ecf20Sopenharmony_cistatic int
15088c2ecf20Sopenharmony_cixpc_n_of_deliverable_payloads_uv(struct xpc_channel *ch)
15098c2ecf20Sopenharmony_ci{
15108c2ecf20Sopenharmony_ci	return xpc_n_of_fifo_entries_uv(&ch->sn.uv.recv_msg_list);
15118c2ecf20Sopenharmony_ci}
15128c2ecf20Sopenharmony_ci
15138c2ecf20Sopenharmony_cistatic void
15148c2ecf20Sopenharmony_cixpc_process_msg_chctl_flags_uv(struct xpc_partition *part, int ch_number)
15158c2ecf20Sopenharmony_ci{
15168c2ecf20Sopenharmony_ci	struct xpc_channel *ch = &part->channels[ch_number];
15178c2ecf20Sopenharmony_ci	int ndeliverable_payloads;
15188c2ecf20Sopenharmony_ci
15198c2ecf20Sopenharmony_ci	xpc_msgqueue_ref(ch);
15208c2ecf20Sopenharmony_ci
15218c2ecf20Sopenharmony_ci	ndeliverable_payloads = xpc_n_of_deliverable_payloads_uv(ch);
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_ci	if (ndeliverable_payloads > 0 &&
15248c2ecf20Sopenharmony_ci	    (ch->flags & XPC_C_CONNECTED) &&
15258c2ecf20Sopenharmony_ci	    (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE)) {
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_ci		xpc_activate_kthreads(ch, ndeliverable_payloads);
15288c2ecf20Sopenharmony_ci	}
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_ci	xpc_msgqueue_deref(ch);
15318c2ecf20Sopenharmony_ci}
15328c2ecf20Sopenharmony_ci
15338c2ecf20Sopenharmony_cistatic enum xp_retval
15348c2ecf20Sopenharmony_cixpc_send_payload_uv(struct xpc_channel *ch, u32 flags, void *payload,
15358c2ecf20Sopenharmony_ci		    u16 payload_size, u8 notify_type, xpc_notify_func func,
15368c2ecf20Sopenharmony_ci		    void *key)
15378c2ecf20Sopenharmony_ci{
15388c2ecf20Sopenharmony_ci	enum xp_retval ret = xpSuccess;
15398c2ecf20Sopenharmony_ci	struct xpc_send_msg_slot_uv *msg_slot = NULL;
15408c2ecf20Sopenharmony_ci	struct xpc_notify_mq_msg_uv *msg;
15418c2ecf20Sopenharmony_ci	u8 msg_buffer[XPC_NOTIFY_MSG_SIZE_UV];
15428c2ecf20Sopenharmony_ci	size_t msg_size;
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_ci	DBUG_ON(notify_type != XPC_N_CALL);
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ci	msg_size = sizeof(struct xpc_notify_mq_msghdr_uv) + payload_size;
15478c2ecf20Sopenharmony_ci	if (msg_size > ch->entry_size)
15488c2ecf20Sopenharmony_ci		return xpPayloadTooBig;
15498c2ecf20Sopenharmony_ci
15508c2ecf20Sopenharmony_ci	xpc_msgqueue_ref(ch);
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_ci	if (ch->flags & XPC_C_DISCONNECTING) {
15538c2ecf20Sopenharmony_ci		ret = ch->reason;
15548c2ecf20Sopenharmony_ci		goto out_1;
15558c2ecf20Sopenharmony_ci	}
15568c2ecf20Sopenharmony_ci	if (!(ch->flags & XPC_C_CONNECTED)) {
15578c2ecf20Sopenharmony_ci		ret = xpNotConnected;
15588c2ecf20Sopenharmony_ci		goto out_1;
15598c2ecf20Sopenharmony_ci	}
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_ci	ret = xpc_allocate_msg_slot_uv(ch, flags, &msg_slot);
15628c2ecf20Sopenharmony_ci	if (ret != xpSuccess)
15638c2ecf20Sopenharmony_ci		goto out_1;
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_ci	if (func != NULL) {
15668c2ecf20Sopenharmony_ci		atomic_inc(&ch->n_to_notify);
15678c2ecf20Sopenharmony_ci
15688c2ecf20Sopenharmony_ci		msg_slot->key = key;
15698c2ecf20Sopenharmony_ci		smp_wmb(); /* a non-NULL func must hit memory after the key */
15708c2ecf20Sopenharmony_ci		msg_slot->func = func;
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci		if (ch->flags & XPC_C_DISCONNECTING) {
15738c2ecf20Sopenharmony_ci			ret = ch->reason;
15748c2ecf20Sopenharmony_ci			goto out_2;
15758c2ecf20Sopenharmony_ci		}
15768c2ecf20Sopenharmony_ci	}
15778c2ecf20Sopenharmony_ci
15788c2ecf20Sopenharmony_ci	msg = (struct xpc_notify_mq_msg_uv *)&msg_buffer;
15798c2ecf20Sopenharmony_ci	msg->hdr.partid = xp_partition_id;
15808c2ecf20Sopenharmony_ci	msg->hdr.ch_number = ch->number;
15818c2ecf20Sopenharmony_ci	msg->hdr.size = msg_size;
15828c2ecf20Sopenharmony_ci	msg->hdr.msg_slot_number = msg_slot->msg_slot_number;
15838c2ecf20Sopenharmony_ci	memcpy(&msg->payload, payload, payload_size);
15848c2ecf20Sopenharmony_ci
15858c2ecf20Sopenharmony_ci	ret = xpc_send_gru_msg(ch->sn.uv.cached_notify_gru_mq_desc, msg,
15868c2ecf20Sopenharmony_ci			       msg_size);
15878c2ecf20Sopenharmony_ci	if (ret == xpSuccess)
15888c2ecf20Sopenharmony_ci		goto out_1;
15898c2ecf20Sopenharmony_ci
15908c2ecf20Sopenharmony_ci	XPC_DEACTIVATE_PARTITION(&xpc_partitions[ch->partid], ret);
15918c2ecf20Sopenharmony_ciout_2:
15928c2ecf20Sopenharmony_ci	if (func != NULL) {
15938c2ecf20Sopenharmony_ci		/*
15948c2ecf20Sopenharmony_ci		 * Try to NULL the msg_slot's func field. If we fail, then
15958c2ecf20Sopenharmony_ci		 * xpc_notify_senders_of_disconnect_uv() beat us to it, in which
15968c2ecf20Sopenharmony_ci		 * case we need to pretend we succeeded to send the message
15978c2ecf20Sopenharmony_ci		 * since the user will get a callout for the disconnect error
15988c2ecf20Sopenharmony_ci		 * by xpc_notify_senders_of_disconnect_uv(), and to also get an
15998c2ecf20Sopenharmony_ci		 * error returned here will confuse them. Additionally, since
16008c2ecf20Sopenharmony_ci		 * in this case the channel is being disconnected we don't need
16018c2ecf20Sopenharmony_ci		 * to put the the msg_slot back on the free list.
16028c2ecf20Sopenharmony_ci		 */
16038c2ecf20Sopenharmony_ci		if (cmpxchg(&msg_slot->func, func, NULL) != func) {
16048c2ecf20Sopenharmony_ci			ret = xpSuccess;
16058c2ecf20Sopenharmony_ci			goto out_1;
16068c2ecf20Sopenharmony_ci		}
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_ci		msg_slot->key = NULL;
16098c2ecf20Sopenharmony_ci		atomic_dec(&ch->n_to_notify);
16108c2ecf20Sopenharmony_ci	}
16118c2ecf20Sopenharmony_ci	xpc_free_msg_slot_uv(ch, msg_slot);
16128c2ecf20Sopenharmony_ciout_1:
16138c2ecf20Sopenharmony_ci	xpc_msgqueue_deref(ch);
16148c2ecf20Sopenharmony_ci	return ret;
16158c2ecf20Sopenharmony_ci}
16168c2ecf20Sopenharmony_ci
16178c2ecf20Sopenharmony_ci/*
16188c2ecf20Sopenharmony_ci * Tell the callers of xpc_send_notify() that the status of their payloads
16198c2ecf20Sopenharmony_ci * is unknown because the channel is now disconnecting.
16208c2ecf20Sopenharmony_ci *
16218c2ecf20Sopenharmony_ci * We don't worry about putting these msg_slots on the free list since the
16228c2ecf20Sopenharmony_ci * msg_slots themselves are about to be kfree'd.
16238c2ecf20Sopenharmony_ci */
16248c2ecf20Sopenharmony_cistatic void
16258c2ecf20Sopenharmony_cixpc_notify_senders_of_disconnect_uv(struct xpc_channel *ch)
16268c2ecf20Sopenharmony_ci{
16278c2ecf20Sopenharmony_ci	struct xpc_send_msg_slot_uv *msg_slot;
16288c2ecf20Sopenharmony_ci	int entry;
16298c2ecf20Sopenharmony_ci
16308c2ecf20Sopenharmony_ci	DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
16318c2ecf20Sopenharmony_ci
16328c2ecf20Sopenharmony_ci	for (entry = 0; entry < ch->local_nentries; entry++) {
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_ci		if (atomic_read(&ch->n_to_notify) == 0)
16358c2ecf20Sopenharmony_ci			break;
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_ci		msg_slot = &ch->sn.uv.send_msg_slots[entry];
16388c2ecf20Sopenharmony_ci		if (msg_slot->func != NULL)
16398c2ecf20Sopenharmony_ci			xpc_notify_sender_uv(ch, msg_slot, ch->reason);
16408c2ecf20Sopenharmony_ci	}
16418c2ecf20Sopenharmony_ci}
16428c2ecf20Sopenharmony_ci
16438c2ecf20Sopenharmony_ci/*
16448c2ecf20Sopenharmony_ci * Get the next deliverable message's payload.
16458c2ecf20Sopenharmony_ci */
16468c2ecf20Sopenharmony_cistatic void *
16478c2ecf20Sopenharmony_cixpc_get_deliverable_payload_uv(struct xpc_channel *ch)
16488c2ecf20Sopenharmony_ci{
16498c2ecf20Sopenharmony_ci	struct xpc_fifo_entry_uv *entry;
16508c2ecf20Sopenharmony_ci	struct xpc_notify_mq_msg_uv *msg;
16518c2ecf20Sopenharmony_ci	void *payload = NULL;
16528c2ecf20Sopenharmony_ci
16538c2ecf20Sopenharmony_ci	if (!(ch->flags & XPC_C_DISCONNECTING)) {
16548c2ecf20Sopenharmony_ci		entry = xpc_get_fifo_entry_uv(&ch->sn.uv.recv_msg_list);
16558c2ecf20Sopenharmony_ci		if (entry != NULL) {
16568c2ecf20Sopenharmony_ci			msg = container_of(entry, struct xpc_notify_mq_msg_uv,
16578c2ecf20Sopenharmony_ci					   hdr.u.next);
16588c2ecf20Sopenharmony_ci			payload = &msg->payload;
16598c2ecf20Sopenharmony_ci		}
16608c2ecf20Sopenharmony_ci	}
16618c2ecf20Sopenharmony_ci	return payload;
16628c2ecf20Sopenharmony_ci}
16638c2ecf20Sopenharmony_ci
16648c2ecf20Sopenharmony_cistatic void
16658c2ecf20Sopenharmony_cixpc_received_payload_uv(struct xpc_channel *ch, void *payload)
16668c2ecf20Sopenharmony_ci{
16678c2ecf20Sopenharmony_ci	struct xpc_notify_mq_msg_uv *msg;
16688c2ecf20Sopenharmony_ci	enum xp_retval ret;
16698c2ecf20Sopenharmony_ci
16708c2ecf20Sopenharmony_ci	msg = container_of(payload, struct xpc_notify_mq_msg_uv, payload);
16718c2ecf20Sopenharmony_ci
16728c2ecf20Sopenharmony_ci	/* return an ACK to the sender of this message */
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_ci	msg->hdr.partid = xp_partition_id;
16758c2ecf20Sopenharmony_ci	msg->hdr.size = 0;	/* size of zero indicates this is an ACK */
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ci	ret = xpc_send_gru_msg(ch->sn.uv.cached_notify_gru_mq_desc, msg,
16788c2ecf20Sopenharmony_ci			       sizeof(struct xpc_notify_mq_msghdr_uv));
16798c2ecf20Sopenharmony_ci	if (ret != xpSuccess)
16808c2ecf20Sopenharmony_ci		XPC_DEACTIVATE_PARTITION(&xpc_partitions[ch->partid], ret);
16818c2ecf20Sopenharmony_ci}
16828c2ecf20Sopenharmony_ci
16838c2ecf20Sopenharmony_cistatic const struct xpc_arch_operations xpc_arch_ops_uv = {
16848c2ecf20Sopenharmony_ci	.setup_partitions = xpc_setup_partitions_uv,
16858c2ecf20Sopenharmony_ci	.teardown_partitions = xpc_teardown_partitions_uv,
16868c2ecf20Sopenharmony_ci	.process_activate_IRQ_rcvd = xpc_process_activate_IRQ_rcvd_uv,
16878c2ecf20Sopenharmony_ci	.get_partition_rsvd_page_pa = xpc_get_partition_rsvd_page_pa_uv,
16888c2ecf20Sopenharmony_ci	.setup_rsvd_page = xpc_setup_rsvd_page_uv,
16898c2ecf20Sopenharmony_ci
16908c2ecf20Sopenharmony_ci	.allow_hb = xpc_allow_hb_uv,
16918c2ecf20Sopenharmony_ci	.disallow_hb = xpc_disallow_hb_uv,
16928c2ecf20Sopenharmony_ci	.disallow_all_hbs = xpc_disallow_all_hbs_uv,
16938c2ecf20Sopenharmony_ci	.increment_heartbeat = xpc_increment_heartbeat_uv,
16948c2ecf20Sopenharmony_ci	.offline_heartbeat = xpc_offline_heartbeat_uv,
16958c2ecf20Sopenharmony_ci	.online_heartbeat = xpc_online_heartbeat_uv,
16968c2ecf20Sopenharmony_ci	.heartbeat_init = xpc_heartbeat_init_uv,
16978c2ecf20Sopenharmony_ci	.heartbeat_exit = xpc_heartbeat_exit_uv,
16988c2ecf20Sopenharmony_ci	.get_remote_heartbeat = xpc_get_remote_heartbeat_uv,
16998c2ecf20Sopenharmony_ci
17008c2ecf20Sopenharmony_ci	.request_partition_activation =
17018c2ecf20Sopenharmony_ci		xpc_request_partition_activation_uv,
17028c2ecf20Sopenharmony_ci	.request_partition_reactivation =
17038c2ecf20Sopenharmony_ci		xpc_request_partition_reactivation_uv,
17048c2ecf20Sopenharmony_ci	.request_partition_deactivation =
17058c2ecf20Sopenharmony_ci		xpc_request_partition_deactivation_uv,
17068c2ecf20Sopenharmony_ci	.cancel_partition_deactivation_request =
17078c2ecf20Sopenharmony_ci		xpc_cancel_partition_deactivation_request_uv,
17088c2ecf20Sopenharmony_ci
17098c2ecf20Sopenharmony_ci	.setup_ch_structures = xpc_setup_ch_structures_uv,
17108c2ecf20Sopenharmony_ci	.teardown_ch_structures = xpc_teardown_ch_structures_uv,
17118c2ecf20Sopenharmony_ci
17128c2ecf20Sopenharmony_ci	.make_first_contact = xpc_make_first_contact_uv,
17138c2ecf20Sopenharmony_ci
17148c2ecf20Sopenharmony_ci	.get_chctl_all_flags = xpc_get_chctl_all_flags_uv,
17158c2ecf20Sopenharmony_ci	.send_chctl_closerequest = xpc_send_chctl_closerequest_uv,
17168c2ecf20Sopenharmony_ci	.send_chctl_closereply = xpc_send_chctl_closereply_uv,
17178c2ecf20Sopenharmony_ci	.send_chctl_openrequest = xpc_send_chctl_openrequest_uv,
17188c2ecf20Sopenharmony_ci	.send_chctl_openreply = xpc_send_chctl_openreply_uv,
17198c2ecf20Sopenharmony_ci	.send_chctl_opencomplete = xpc_send_chctl_opencomplete_uv,
17208c2ecf20Sopenharmony_ci	.process_msg_chctl_flags = xpc_process_msg_chctl_flags_uv,
17218c2ecf20Sopenharmony_ci
17228c2ecf20Sopenharmony_ci	.save_remote_msgqueue_pa = xpc_save_remote_msgqueue_pa_uv,
17238c2ecf20Sopenharmony_ci
17248c2ecf20Sopenharmony_ci	.setup_msg_structures = xpc_setup_msg_structures_uv,
17258c2ecf20Sopenharmony_ci	.teardown_msg_structures = xpc_teardown_msg_structures_uv,
17268c2ecf20Sopenharmony_ci
17278c2ecf20Sopenharmony_ci	.indicate_partition_engaged = xpc_indicate_partition_engaged_uv,
17288c2ecf20Sopenharmony_ci	.indicate_partition_disengaged = xpc_indicate_partition_disengaged_uv,
17298c2ecf20Sopenharmony_ci	.assume_partition_disengaged = xpc_assume_partition_disengaged_uv,
17308c2ecf20Sopenharmony_ci	.partition_engaged = xpc_partition_engaged_uv,
17318c2ecf20Sopenharmony_ci	.any_partition_engaged = xpc_any_partition_engaged_uv,
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_ci	.n_of_deliverable_payloads = xpc_n_of_deliverable_payloads_uv,
17348c2ecf20Sopenharmony_ci	.send_payload = xpc_send_payload_uv,
17358c2ecf20Sopenharmony_ci	.get_deliverable_payload = xpc_get_deliverable_payload_uv,
17368c2ecf20Sopenharmony_ci	.received_payload = xpc_received_payload_uv,
17378c2ecf20Sopenharmony_ci	.notify_senders_of_disconnect = xpc_notify_senders_of_disconnect_uv,
17388c2ecf20Sopenharmony_ci};
17398c2ecf20Sopenharmony_ci
17408c2ecf20Sopenharmony_cistatic int
17418c2ecf20Sopenharmony_cixpc_init_mq_node(int nid)
17428c2ecf20Sopenharmony_ci{
17438c2ecf20Sopenharmony_ci	int cpu;
17448c2ecf20Sopenharmony_ci
17458c2ecf20Sopenharmony_ci	get_online_cpus();
17468c2ecf20Sopenharmony_ci
17478c2ecf20Sopenharmony_ci	for_each_cpu(cpu, cpumask_of_node(nid)) {
17488c2ecf20Sopenharmony_ci		xpc_activate_mq_uv =
17498c2ecf20Sopenharmony_ci			xpc_create_gru_mq_uv(XPC_ACTIVATE_MQ_SIZE_UV, nid,
17508c2ecf20Sopenharmony_ci					     XPC_ACTIVATE_IRQ_NAME,
17518c2ecf20Sopenharmony_ci					     xpc_handle_activate_IRQ_uv);
17528c2ecf20Sopenharmony_ci		if (!IS_ERR(xpc_activate_mq_uv))
17538c2ecf20Sopenharmony_ci			break;
17548c2ecf20Sopenharmony_ci	}
17558c2ecf20Sopenharmony_ci	if (IS_ERR(xpc_activate_mq_uv)) {
17568c2ecf20Sopenharmony_ci		put_online_cpus();
17578c2ecf20Sopenharmony_ci		return PTR_ERR(xpc_activate_mq_uv);
17588c2ecf20Sopenharmony_ci	}
17598c2ecf20Sopenharmony_ci
17608c2ecf20Sopenharmony_ci	for_each_cpu(cpu, cpumask_of_node(nid)) {
17618c2ecf20Sopenharmony_ci		xpc_notify_mq_uv =
17628c2ecf20Sopenharmony_ci			xpc_create_gru_mq_uv(XPC_NOTIFY_MQ_SIZE_UV, nid,
17638c2ecf20Sopenharmony_ci					     XPC_NOTIFY_IRQ_NAME,
17648c2ecf20Sopenharmony_ci					     xpc_handle_notify_IRQ_uv);
17658c2ecf20Sopenharmony_ci		if (!IS_ERR(xpc_notify_mq_uv))
17668c2ecf20Sopenharmony_ci			break;
17678c2ecf20Sopenharmony_ci	}
17688c2ecf20Sopenharmony_ci	if (IS_ERR(xpc_notify_mq_uv)) {
17698c2ecf20Sopenharmony_ci		xpc_destroy_gru_mq_uv(xpc_activate_mq_uv);
17708c2ecf20Sopenharmony_ci		put_online_cpus();
17718c2ecf20Sopenharmony_ci		return PTR_ERR(xpc_notify_mq_uv);
17728c2ecf20Sopenharmony_ci	}
17738c2ecf20Sopenharmony_ci
17748c2ecf20Sopenharmony_ci	put_online_cpus();
17758c2ecf20Sopenharmony_ci	return 0;
17768c2ecf20Sopenharmony_ci}
17778c2ecf20Sopenharmony_ci
17788c2ecf20Sopenharmony_ciint
17798c2ecf20Sopenharmony_cixpc_init_uv(void)
17808c2ecf20Sopenharmony_ci{
17818c2ecf20Sopenharmony_ci	int nid;
17828c2ecf20Sopenharmony_ci	int ret = 0;
17838c2ecf20Sopenharmony_ci
17848c2ecf20Sopenharmony_ci	xpc_arch_ops = xpc_arch_ops_uv;
17858c2ecf20Sopenharmony_ci
17868c2ecf20Sopenharmony_ci	if (sizeof(struct xpc_notify_mq_msghdr_uv) > XPC_MSG_HDR_MAX_SIZE) {
17878c2ecf20Sopenharmony_ci		dev_err(xpc_part, "xpc_notify_mq_msghdr_uv is larger than %d\n",
17888c2ecf20Sopenharmony_ci			XPC_MSG_HDR_MAX_SIZE);
17898c2ecf20Sopenharmony_ci		return -E2BIG;
17908c2ecf20Sopenharmony_ci	}
17918c2ecf20Sopenharmony_ci
17928c2ecf20Sopenharmony_ci	if (xpc_mq_node < 0)
17938c2ecf20Sopenharmony_ci		for_each_online_node(nid) {
17948c2ecf20Sopenharmony_ci			ret = xpc_init_mq_node(nid);
17958c2ecf20Sopenharmony_ci
17968c2ecf20Sopenharmony_ci			if (!ret)
17978c2ecf20Sopenharmony_ci				break;
17988c2ecf20Sopenharmony_ci		}
17998c2ecf20Sopenharmony_ci	else
18008c2ecf20Sopenharmony_ci		ret = xpc_init_mq_node(xpc_mq_node);
18018c2ecf20Sopenharmony_ci
18028c2ecf20Sopenharmony_ci	if (ret < 0)
18038c2ecf20Sopenharmony_ci		dev_err(xpc_part, "xpc_init_mq_node() returned error=%d\n",
18048c2ecf20Sopenharmony_ci			-ret);
18058c2ecf20Sopenharmony_ci
18068c2ecf20Sopenharmony_ci	return ret;
18078c2ecf20Sopenharmony_ci}
18088c2ecf20Sopenharmony_ci
18098c2ecf20Sopenharmony_civoid
18108c2ecf20Sopenharmony_cixpc_exit_uv(void)
18118c2ecf20Sopenharmony_ci{
18128c2ecf20Sopenharmony_ci	xpc_destroy_gru_mq_uv(xpc_notify_mq_uv);
18138c2ecf20Sopenharmony_ci	xpc_destroy_gru_mq_uv(xpc_activate_mq_uv);
18148c2ecf20Sopenharmony_ci}
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_cimodule_param(xpc_mq_node, int, 0);
18178c2ecf20Sopenharmony_ciMODULE_PARM_DESC(xpc_mq_node, "Node number on which to allocate message queues.");
1818