@@ -3,6 +3,7 @@
config GUNYAH
tristate "Gunyah Virtualization drivers"
depends on ARM64
+ select GUNYAH_PLATFORM_HOOKS
help
The Gunyah drivers are the helper interfaces that run in a guest VM
such as basic inter-VM IPC and signaling mechanisms, and higher level
@@ -10,3 +11,6 @@ config GUNYAH
Say Y/M here to enable the drivers needed to interact in a Gunyah
virtual environment.
+
+config GUNYAH_PLATFORM_HOOKS
+ tristate
@@ -3,3 +3,4 @@
gunyah_rsc_mgr-y += rsc_mgr.o rsc_mgr_rpc.o vm_mgr.o vm_mgr_mem.o
obj-$(CONFIG_GUNYAH) += gunyah.o gunyah_rsc_mgr.o gunyah_vcpu.o
+obj-$(CONFIG_GUNYAH_PLATFORM_HOOKS) += gunyah_platform_hooks.o
new file mode 100644
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
+ */
+
+#include <linux/device.h>
+#include <linux/gunyah.h>
+#include <linux/module.h>
+#include <linux/rwsem.h>
+
+#include "rsc_mgr.h"
+
+static const struct gunyah_rm_platform_ops *rm_platform_ops;
+static DECLARE_RWSEM(rm_platform_ops_lock);
+
+int gunyah_rm_platform_pre_mem_share(struct gunyah_rm *rm,
+ struct gunyah_rm_mem_parcel *mem_parcel)
+{
+ int ret = 0;
+
+ down_read(&rm_platform_ops_lock);
+ if (rm_platform_ops && rm_platform_ops->pre_mem_share)
+ ret = rm_platform_ops->pre_mem_share(rm, mem_parcel);
+ up_read(&rm_platform_ops_lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(gunyah_rm_platform_pre_mem_share);
+
+int gunyah_rm_platform_post_mem_reclaim(struct gunyah_rm *rm,
+ struct gunyah_rm_mem_parcel *mem_parcel)
+{
+ int ret = 0;
+
+ down_read(&rm_platform_ops_lock);
+ if (rm_platform_ops && rm_platform_ops->post_mem_reclaim)
+ ret = rm_platform_ops->post_mem_reclaim(rm, mem_parcel);
+ up_read(&rm_platform_ops_lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(gunyah_rm_platform_post_mem_reclaim);
+
+int gunyah_rm_platform_pre_demand_page(struct gunyah_rm *rm, u16 vmid,
+ u32 flags, struct folio *folio)
+{
+ int ret = 0;
+
+ down_read(&rm_platform_ops_lock);
+ if (rm_platform_ops && rm_platform_ops->pre_demand_page)
+ ret = rm_platform_ops->pre_demand_page(rm, vmid, flags, folio);
+ up_read(&rm_platform_ops_lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(gunyah_rm_platform_pre_demand_page);
+
+int gunyah_rm_platform_reclaim_demand_page(struct gunyah_rm *rm, u16 vmid,
+ u32 flags, struct folio *folio)
+{
+ int ret = 0;
+
+ down_read(&rm_platform_ops_lock);
+ if (rm_platform_ops && rm_platform_ops->pre_demand_page)
+ ret = rm_platform_ops->release_demand_page(rm, vmid, flags,
+ folio);
+ up_read(&rm_platform_ops_lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(gunyah_rm_platform_reclaim_demand_page);
+
+int gunyah_rm_register_platform_ops(
+ const struct gunyah_rm_platform_ops *platform_ops)
+{
+ int ret = 0;
+
+ down_write(&rm_platform_ops_lock);
+ if (!rm_platform_ops)
+ rm_platform_ops = platform_ops;
+ else
+ ret = -EEXIST;
+ up_write(&rm_platform_ops_lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(gunyah_rm_register_platform_ops);
+
+void gunyah_rm_unregister_platform_ops(
+ const struct gunyah_rm_platform_ops *platform_ops)
+{
+ down_write(&rm_platform_ops_lock);
+ if (rm_platform_ops == platform_ops)
+ rm_platform_ops = NULL;
+ up_write(&rm_platform_ops_lock);
+}
+EXPORT_SYMBOL_GPL(gunyah_rm_unregister_platform_ops);
+
+static void _devm_gunyah_rm_unregister_platform_ops(void *data)
+{
+ gunyah_rm_unregister_platform_ops(
+ (const struct gunyah_rm_platform_ops *)data);
+}
+
+int devm_gunyah_rm_register_platform_ops(
+ struct device *dev, const struct gunyah_rm_platform_ops *ops)
+{
+ int ret;
+
+ ret = gunyah_rm_register_platform_ops(ops);
+ if (ret)
+ return ret;
+
+ return devm_add_action(dev, _devm_gunyah_rm_unregister_platform_ops,
+ (void *)ops);
+}
+EXPORT_SYMBOL_GPL(devm_gunyah_rm_register_platform_ops);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Gunyah Platform Hooks");
@@ -117,4 +117,14 @@ int gunyah_rm_call(struct gunyah_rm *rsc_mgr, u32 message_id,
const void *req_buf, size_t req_buf_size, void **resp_buf,
size_t *resp_buf_size);
+int gunyah_rm_platform_pre_mem_share(struct gunyah_rm *rm,
+ struct gunyah_rm_mem_parcel *mem_parcel);
+int gunyah_rm_platform_post_mem_reclaim(
+ struct gunyah_rm *rm, struct gunyah_rm_mem_parcel *mem_parcel);
+
+int gunyah_rm_platform_pre_demand_page(struct gunyah_rm *rm, u16 vmid,
+ u32 flags, struct folio *folio);
+int gunyah_rm_platform_reclaim_demand_page(struct gunyah_rm *rm, u16 vmid,
+ u32 flags, struct folio *folio);
+
#endif
@@ -206,6 +206,12 @@ int gunyah_rm_mem_share(struct gunyah_rm *rm, struct gunyah_rm_mem_parcel *p)
if (!msg)
return -ENOMEM;
+ ret = gunyah_rm_platform_pre_mem_share(rm, p);
+ if (ret) {
+ kfree(msg);
+ return ret;
+ }
+
req_header = msg;
acl = (void *)req_header + sizeof(*req_header);
mem = (void *)acl + acl_size;
@@ -231,8 +237,10 @@ int gunyah_rm_mem_share(struct gunyah_rm *rm, struct gunyah_rm_mem_parcel *p)
&resp_size);
kfree(msg);
- if (ret)
+ if (ret) {
+ gunyah_rm_platform_post_mem_reclaim(rm, p);
return ret;
+ }
p->mem_handle = le32_to_cpu(*resp);
kfree(resp);
@@ -263,9 +271,15 @@ int gunyah_rm_mem_reclaim(struct gunyah_rm *rm,
struct gunyah_rm_mem_release_req req = {
.mem_handle = cpu_to_le32(parcel->mem_handle),
};
+ int ret;
- return gunyah_rm_call(rm, GUNYAH_RM_RPC_MEM_RECLAIM, &req, sizeof(req),
- NULL, NULL);
+ ret = gunyah_rm_call(rm, GUNYAH_RM_RPC_MEM_RECLAIM, &req, sizeof(req),
+ NULL, NULL);
+ /* Only call platform mem reclaim hooks if we reclaimed the memory */
+ if (ret)
+ return ret;
+
+ return gunyah_rm_platform_post_mem_reclaim(rm, parcel);
}
/**
@@ -9,6 +9,7 @@
#include <linux/mm.h>
#include <linux/pagemap.h>
+#include "rsc_mgr.h"
#include "vm_mgr.h"
#define WRITE_TAG (1 << 0)
@@ -84,7 +85,7 @@ int gunyah_vm_provide_folio(struct gunyah_vm *ghvm, struct folio *folio,
size_t size = folio_size(folio);
enum gunyah_error gunyah_error;
unsigned long tag = 0;
- int ret;
+ int ret, tmp;
/* clang-format off */
if (share) {
@@ -127,6 +128,11 @@ int gunyah_vm_provide_folio(struct gunyah_vm *ghvm, struct folio *folio,
else /* !share && !write */
access = GUNYAH_PAGETABLE_ACCESS_RX;
+ ret = gunyah_rm_platform_pre_demand_page(ghvm->rm, ghvm->vmid, access,
+ folio);
+ if (ret)
+ goto remove;
+
gunyah_error = gunyah_hypercall_memextent_donate(donate_flags(share),
host_extent->capid,
guest_extent->capid,
@@ -135,7 +141,7 @@ int gunyah_vm_provide_folio(struct gunyah_vm *ghvm, struct folio *folio,
pr_err("Failed to donate memory for guest address 0x%016llx: %d\n",
gpa, gunyah_error);
ret = gunyah_error_remap(gunyah_error);
- goto remove;
+ goto platform_release;
}
extent_attrs =
@@ -166,6 +172,14 @@ int gunyah_vm_provide_folio(struct gunyah_vm *ghvm, struct folio *folio,
if (gunyah_error != GUNYAH_ERROR_OK)
pr_err("Failed to reclaim memory donation for guest address 0x%016llx: %d\n",
gpa, gunyah_error);
+platform_release:
+ tmp = gunyah_rm_platform_reclaim_demand_page(ghvm->rm, ghvm->vmid,
+ access, folio);
+ if (tmp) {
+ pr_err("Platform failed to reclaim memory for guest address 0x%016llx: %d",
+ gpa, tmp);
+ return ret;
+ }
remove:
mtree_erase(&ghvm->mm, gfn);
return ret;
@@ -243,14 +257,12 @@ static int __gunyah_vm_reclaim_folio_locked(struct gunyah_vm *ghvm, void *entry,
else /* !share && !write */
access = GUNYAH_PAGETABLE_ACCESS_RX;
- gunyah_error = gunyah_hypercall_memextent_donate(donate_flags(share),
- guest_extent->capid,
- host_extent->capid, pa,
- size);
- if (gunyah_error != GUNYAH_ERROR_OK) {
- pr_err("Failed to reclaim memory donation for guest address 0x%016llx: %d\n",
- gfn << PAGE_SHIFT, gunyah_error);
- ret = gunyah_error_remap(gunyah_error);
+ ret = gunyah_rm_platform_reclaim_demand_page(ghvm->rm, ghvm->vmid,
+ access, folio);
+ if (ret) {
+ pr_err_ratelimited(
+ "Platform failed to reclaim memory for guest address 0x%016llx: %d",
+ gunyah_gfn_to_gpa(gfn), ret);
goto err;
}
@@ -199,6 +199,43 @@ struct gunyah_rm_mem_parcel {
u32 mem_handle;
};
+struct gunyah_rm_platform_ops {
+ int (*pre_mem_share)(struct gunyah_rm *rm,
+ struct gunyah_rm_mem_parcel *mem_parcel);
+ int (*post_mem_reclaim)(struct gunyah_rm *rm,
+ struct gunyah_rm_mem_parcel *mem_parcel);
+
+ int (*pre_demand_page)(struct gunyah_rm *rm, u16 vmid, u32 flags,
+ struct folio *folio);
+ int (*release_demand_page)(struct gunyah_rm *rm, u16 vmid, u32 flags,
+ struct folio *folio);
+};
+
+#if IS_ENABLED(CONFIG_GUNYAH_PLATFORM_HOOKS)
+int gunyah_rm_register_platform_ops(
+ const struct gunyah_rm_platform_ops *platform_ops);
+void gunyah_rm_unregister_platform_ops(
+ const struct gunyah_rm_platform_ops *platform_ops);
+int devm_gunyah_rm_register_platform_ops(
+ struct device *dev, const struct gunyah_rm_platform_ops *ops);
+#else
+static inline int gunyah_rm_register_platform_ops(
+ const struct gunyah_rm_platform_ops *platform_ops)
+{
+ return 0;
+}
+static inline void gunyah_rm_unregister_platform_ops(
+ const struct gunyah_rm_platform_ops *platform_ops)
+{
+}
+static inline int
+devm_gunyah_rm_register_platform_ops(struct device *dev,
+ const struct gunyah_rm_platform_ops *ops)
+{
+ return 0;
+}
+#endif
+
/******************************************************************************/
/* Common arch-independent definitions for Gunyah hypercalls */
#define GUNYAH_CAPID_INVAL U64_MAX