1 /*
2 * Copyright (C) 2020-2022 Intel Corporation.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <errno.h>
8 #include <util.h>
9 #include <acrn_hv_defs.h>
10 #include <asm/pgtable.h>
11 #include <asm/guest/vm.h>
12 #include <asm/guest/ept.h>
13 #include <debug/logmsg.h>
14
15
assign_mmio_dev(struct acrn_vm * vm,const struct acrn_mmiodev * mmiodev)16 int32_t assign_mmio_dev(struct acrn_vm *vm, const struct acrn_mmiodev *mmiodev)
17 {
18 int32_t i, ret = 0;
19 const struct acrn_mmiores *res;
20
21 for (i = 0; i < MMIODEV_RES_NUM; i++) {
22 res = &mmiodev->res[i];
23 if (mem_aligned_check(res->user_vm_pa, PAGE_SIZE) &&
24 mem_aligned_check(res->host_pa, PAGE_SIZE) &&
25 mem_aligned_check(res->size, PAGE_SIZE)) {
26 ept_add_mr(vm, (uint64_t *)vm->arch_vm.nworld_eptp, res->host_pa,
27 is_service_vm(vm) ? res->host_pa : res->user_vm_pa,
28 res->size, EPT_RWX | (res->mem_type & EPT_MT_MASK));
29 } else {
30 pr_err("%s invalid mmio res[%d] gpa:0x%lx hpa:0x%lx size:0x%lx",
31 __FUNCTION__, i, res->user_vm_pa, res->host_pa, res->size);
32 ret = -EINVAL;
33 break;
34 }
35 }
36
37 return ret;
38 }
39
deassign_mmio_dev(struct acrn_vm * vm,const struct acrn_mmiodev * mmiodev)40 int32_t deassign_mmio_dev(struct acrn_vm *vm, const struct acrn_mmiodev *mmiodev)
41 {
42 int32_t i, ret = 0;
43 uint64_t gpa;
44 const struct acrn_mmiores *res;
45
46 for (i = 0; i < MMIODEV_RES_NUM; i++) {
47 res = &mmiodev->res[i];
48 gpa = is_service_vm(vm) ? res->host_pa : res->user_vm_pa;
49 if (ept_is_valid_mr(vm, gpa, res->size)) {
50 if (mem_aligned_check(gpa, PAGE_SIZE) &&
51 mem_aligned_check(res->size, PAGE_SIZE)) {
52 ept_del_mr(vm, (uint64_t *)vm->arch_vm.nworld_eptp, gpa, res->size);
53 } else {
54 pr_err("%s invalid mmio res[%d] gpa:0x%lx hpa:0x%lx size:0x%lx",
55 __FUNCTION__, i, res->user_vm_pa, res->host_pa, res->size);
56 ret = -EINVAL;
57 break;
58 }
59 }
60 }
61
62 return ret;
63 }
64