1/* $NetBSD: nvmm.h,v 1.10 2019/05/11 07:31:56 maxv Exp $ */
2
3/*
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Maxime Villard.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef _NVMM_H_
33#define _NVMM_H_
34
35#include <sys/types.h>
36
37#ifndef _KERNEL
38#include <stdbool.h>
39#endif
40
41typedef uint64_t gpaddr_t;
42typedef uint64_t gvaddr_t;
43
44typedef uint32_t nvmm_machid_t;
45typedef uint32_t nvmm_cpuid_t;
46
47#ifdef __x86_64__
48#include <dev/nvmm/x86/nvmm_x86.h>
49#endif
50
51#define NVMM_EXIT_NONE 0x0000000000000000ULL
52#define NVMM_EXIT_MEMORY 0x0000000000000001ULL
53#define NVMM_EXIT_IO 0x0000000000000002ULL
54#define NVMM_EXIT_MSR 0x0000000000000003ULL /* x86 only? */
55#define NVMM_EXIT_INT_READY 0x0000000000000004ULL
56#define NVMM_EXIT_NMI_READY 0x0000000000000005ULL
57#define NVMM_EXIT_HALTED 0x0000000000000006ULL
58#define NVMM_EXIT_SHUTDOWN 0x0000000000000007ULL
59/* Range 0x1000-0x10000 is MD. */
60#define NVMM_EXIT_INVALID 0xFFFFFFFFFFFFFFFFULL
61
62struct nvmm_exit {
63 uint64_t reason;
64 union nvmm_exit_md u;
65 uint64_t exitstate[8];
66};
67
68enum nvmm_event_type {
69 NVMM_EVENT_INTERRUPT_HW,
70 NVMM_EVENT_INTERRUPT_SW,
71 NVMM_EVENT_EXCEPTION
72};
73
74struct nvmm_event {
75 enum nvmm_event_type type;
76 uint64_t vector;
77 union {
78 /* NVMM_EVENT_INTERRUPT_HW */
79 uint8_t prio;
80
81 /* NVMM_EVENT_EXCEPTION */
82 uint64_t error;
83 } u;
84};
85
86#define NVMM_CAPABILITY_VERSION 1
87
88struct nvmm_capability {
89 uint64_t version;
90 uint64_t state_size;
91 uint64_t max_machines;
92 uint64_t max_vcpus;
93 uint64_t max_ram;
94 struct nvmm_cap_md arch;
95};
96
97/* Configuration slots. */
98#define NVMM_MACH_CONF_LIBNVMM_BEGIN 0
99#define NVMM_MACH_CONF_MI_BEGIN 100
100#define NVMM_MACH_CONF_MD_BEGIN 200
101
102#define NVMM_MACH_CONF_MD(op) (op - NVMM_MACH_CONF_MD_BEGIN)
103
104struct nvmm_comm_page {
105 /* State. */
106 uint64_t state_wanted;
107 uint64_t state_cached;
108 uint64_t state_commit;
109 struct nvmm_vcpu_state state;
110
111 /* Event. */
112 bool event_commit;
113 struct nvmm_event event;
114};
115
116/*
117 * Bits 20:27 -> machid
118 * Bits 12:19 -> cpuid
119 */
120#define NVMM_COMM_OFF(machid, cpuid) \
121 ((((uint64_t)machid & 0xFFULL) << 20) | (((uint64_t)cpuid & 0xFFULL) << 12))
122
123#define NVMM_COMM_MACHID(off) \
124 ((off >> 20) & 0xFF)
125
126#define NVMM_COMM_CPUID(off) \
127 ((off >> 12) & 0xFF)
128
129#endif
130