124 lines
3.5 KiB
Diff
124 lines
3.5 KiB
Diff
|
|
From 884c4d6bc101454f0e0f3c779bc1155024b056c3 Mon Sep 17 00:00:00 2001
|
||
|
|
From: xiongmengbiao <xiongmengbiao@hygon.cn>
|
||
|
|
Date: Wed, 29 May 2024 15:18:55 +0800
|
||
|
|
Subject: [PATCH] hw/misc: support tkm use mem2 memory
|
||
|
|
|
||
|
|
Signed-off-by: xiongmengbiao <xiongmengbiao@hygon.cn>
|
||
|
|
---
|
||
|
|
hw/misc/psp.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++-
|
||
|
|
1 file changed, 63 insertions(+), 1 deletion(-)
|
||
|
|
|
||
|
|
diff --git a/hw/misc/psp.c b/hw/misc/psp.c
|
||
|
|
index 6ff2ceec10..4eb5ca0e0b 100644
|
||
|
|
--- a/hw/misc/psp.c
|
||
|
|
+++ b/hw/misc/psp.c
|
||
|
|
@@ -15,6 +15,9 @@
|
||
|
|
#include "migration/vmstate.h"
|
||
|
|
#include "hw/qdev-properties.h"
|
||
|
|
#include "sysemu/runstate.h"
|
||
|
|
+#include "exec/memory.h"
|
||
|
|
+#include "exec/address-spaces.h"
|
||
|
|
+#include "hw/i386/e820_memory_layout.h"
|
||
|
|
#include <sys/ioctl.h>
|
||
|
|
|
||
|
|
#define TYPE_PSP_DEV "psp"
|
||
|
|
@@ -46,14 +49,24 @@ struct PSPDevState {
|
||
|
|
enum VPSP_DEV_CTRL_OPCODE {
|
||
|
|
VPSP_OP_VID_ADD,
|
||
|
|
VPSP_OP_VID_DEL,
|
||
|
|
+ VPSP_OP_SET_DEFAULT_VID_PERMISSION,
|
||
|
|
+ VPSP_OP_GET_DEFAULT_VID_PERMISSION,
|
||
|
|
+ VPSP_OP_SET_GPA,
|
||
|
|
};
|
||
|
|
|
||
|
|
struct psp_dev_ctrl {
|
||
|
|
unsigned char op;
|
||
|
|
+ unsigned char resv[3];
|
||
|
|
union {
|
||
|
|
unsigned int vid;
|
||
|
|
+ // Set or check the permissions for the default VID
|
||
|
|
+ unsigned int def_vid_perm;
|
||
|
|
+ struct {
|
||
|
|
+ uint64_t gpa_start;
|
||
|
|
+ uint64_t gpa_end;
|
||
|
|
+ } gpa;
|
||
|
|
unsigned char reserved[128];
|
||
|
|
- } data;
|
||
|
|
+ } __attribute__ ((packed)) data;
|
||
|
|
};
|
||
|
|
|
||
|
|
static void psp_dev_destroy(PSPDevState *state)
|
||
|
|
@@ -86,10 +99,32 @@ static void psp_dev_shutdown_notify(Notifier *notifier, void *data)
|
||
|
|
psp_dev_destroy(state);
|
||
|
|
}
|
||
|
|
|
||
|
|
+static MemoryRegion *find_memory_region_by_name(MemoryRegion *root, const char *name) {
|
||
|
|
+ MemoryRegion *subregion;
|
||
|
|
+ MemoryRegion *result;
|
||
|
|
+
|
||
|
|
+ if (strcmp(root->name, name) == 0)
|
||
|
|
+ return root;
|
||
|
|
+
|
||
|
|
+ QTAILQ_FOREACH(subregion, &root->subregions, subregions_link) {
|
||
|
|
+ result = find_memory_region_by_name(subregion, name);
|
||
|
|
+ if (result) {
|
||
|
|
+ return result;
|
||
|
|
+ }
|
||
|
|
+ }
|
||
|
|
+
|
||
|
|
+ return NULL;
|
||
|
|
+}
|
||
|
|
+
|
||
|
|
static void psp_dev_realize(DeviceState *dev, Error **errp)
|
||
|
|
{
|
||
|
|
+ int i;
|
||
|
|
+ char mr_name[128] = {0};
|
||
|
|
struct psp_dev_ctrl ctrl = { 0 };
|
||
|
|
PSPDevState *state = PSP_DEV(dev);
|
||
|
|
+ MemoryRegion *root_mr = get_system_memory();
|
||
|
|
+ MemoryRegion *find_mr = NULL;
|
||
|
|
+ uint64_t ram2_start = 0, ram2_end = 0;
|
||
|
|
|
||
|
|
state->dev_fd = qemu_open_old(PSP_DEV_PATH, O_RDWR);
|
||
|
|
if (state->dev_fd < 0) {
|
||
|
|
@@ -104,9 +139,36 @@ static void psp_dev_realize(DeviceState *dev, Error **errp)
|
||
|
|
goto end;
|
||
|
|
}
|
||
|
|
|
||
|
|
+ for (i = 0 ;; ++i) {
|
||
|
|
+ sprintf(mr_name, "mem2-%d", i);
|
||
|
|
+ find_mr = find_memory_region_by_name(root_mr, mr_name);
|
||
|
|
+ if (!find_mr)
|
||
|
|
+ break;
|
||
|
|
+
|
||
|
|
+ if (!ram2_start)
|
||
|
|
+ ram2_start = find_mr->addr;
|
||
|
|
+ ram2_end = find_mr->addr + find_mr->size - 1;
|
||
|
|
+ }
|
||
|
|
+
|
||
|
|
+ if (ram2_start != ram2_end) {
|
||
|
|
+ ctrl.op = VPSP_OP_SET_GPA;
|
||
|
|
+ ctrl.data.gpa.gpa_start = ram2_start;
|
||
|
|
+ ctrl.data.gpa.gpa_end = ram2_end;
|
||
|
|
+ if (ioctl(state->dev_fd, PSP_IOC_VPSP_OPT, &ctrl) < 0) {
|
||
|
|
+ error_setg(errp, "psp_dev_realize VPSP_OP_SET_GPA (start 0x%lx, end 0x%lx), return %d",
|
||
|
|
+ ram2_start, ram2_end, -errno);
|
||
|
|
+ goto del_vid;
|
||
|
|
+ }
|
||
|
|
+ }
|
||
|
|
+
|
||
|
|
state->enabled = true;
|
||
|
|
state->shutdown_notifier.notify = psp_dev_shutdown_notify;
|
||
|
|
qemu_register_shutdown_notifier(&state->shutdown_notifier);
|
||
|
|
+
|
||
|
|
+ return;
|
||
|
|
+del_vid:
|
||
|
|
+ ctrl.op = VPSP_OP_VID_DEL;
|
||
|
|
+ ioctl(state->dev_fd, PSP_IOC_VPSP_OPT, &ctrl);
|
||
|
|
end:
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
--
|
||
|
|
2.41.0.windows.1
|
||
|
|
|