2020-07-16 13:45:06 +08:00
|
|
|
/*
|
2022-07-13 09:21:24 +08:00
|
|
|
* Copyright (C) 2020-2022 Intel Corporation.
|
2020-07-16 13:45:06 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <util.h>
|
|
|
|
#include <acrn_hv_defs.h>
|
2021-04-23 15:50:57 +08:00
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/guest/vm.h>
|
|
|
|
#include <asm/guest/ept.h>
|
2021-08-06 10:11:34 +08:00
|
|
|
#include <debug/logmsg.h>
|
|
|
|
|
2020-07-16 13:45:06 +08:00
|
|
|
|
|
|
|
int32_t assign_mmio_dev(struct acrn_vm *vm, const struct acrn_mmiodev *mmiodev)
|
|
|
|
{
|
2021-08-06 10:11:34 +08:00
|
|
|
int32_t i, ret = 0;
|
|
|
|
const struct acrn_mmiores *res;
|
|
|
|
|
|
|
|
for (i = 0; i < MMIODEV_RES_NUM; i++) {
|
|
|
|
res = &mmiodev->res[i];
|
|
|
|
if (mem_aligned_check(res->user_vm_pa, PAGE_SIZE) &&
|
|
|
|
mem_aligned_check(res->host_pa, PAGE_SIZE) &&
|
|
|
|
mem_aligned_check(res->size, PAGE_SIZE)) {
|
|
|
|
ept_add_mr(vm, (uint64_t *)vm->arch_vm.nworld_eptp, res->host_pa,
|
2021-10-19 01:24:54 +08:00
|
|
|
is_service_vm(vm) ? res->host_pa : res->user_vm_pa,
|
2021-08-06 10:11:34 +08:00
|
|
|
res->size, EPT_RWX | (res->mem_type & EPT_MT_MASK));
|
|
|
|
} else {
|
|
|
|
pr_err("%s invalid mmio res[%d] gpa:0x%lx hpa:0x%lx size:0x%lx",
|
|
|
|
__FUNCTION__, i, res->user_vm_pa, res->host_pa, res->size);
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2020-07-16 13:45:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t deassign_mmio_dev(struct acrn_vm *vm, const struct acrn_mmiodev *mmiodev)
|
|
|
|
{
|
2021-08-06 10:11:34 +08:00
|
|
|
int32_t i, ret = 0;
|
|
|
|
uint64_t gpa;
|
|
|
|
const struct acrn_mmiores *res;
|
|
|
|
|
|
|
|
for (i = 0; i < MMIODEV_RES_NUM; i++) {
|
|
|
|
res = &mmiodev->res[i];
|
2021-10-19 01:24:54 +08:00
|
|
|
gpa = is_service_vm(vm) ? res->host_pa : res->user_vm_pa;
|
2021-08-06 10:11:34 +08:00
|
|
|
if (ept_is_valid_mr(vm, gpa, res->size)) {
|
|
|
|
if (mem_aligned_check(gpa, PAGE_SIZE) &&
|
|
|
|
mem_aligned_check(res->size, PAGE_SIZE)) {
|
|
|
|
ept_del_mr(vm, (uint64_t *)vm->arch_vm.nworld_eptp, gpa, res->size);
|
|
|
|
} else {
|
|
|
|
pr_err("%s invalid mmio res[%d] gpa:0x%lx hpa:0x%lx size:0x%lx",
|
|
|
|
__FUNCTION__, i, res->user_vm_pa, res->host_pa, res->size);
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-07-16 13:45:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|