hyperv_common.c revision 1.6 1 1.6 nonaka /* $NetBSD: hyperv_common.c,v 1.6 2022/05/20 13:55:17 nonaka Exp $ */
2 1.1 nonaka
3 1.1 nonaka /*-
4 1.1 nonaka * Copyright (c) 2009-2012,2016-2017 Microsoft Corp.
5 1.1 nonaka * Copyright (c) 2012 NetApp Inc.
6 1.1 nonaka * Copyright (c) 2012 Citrix Inc.
7 1.1 nonaka * All rights reserved.
8 1.1 nonaka *
9 1.1 nonaka * Redistribution and use in source and binary forms, with or without
10 1.1 nonaka * modification, are permitted provided that the following conditions
11 1.1 nonaka * are met:
12 1.1 nonaka * 1. Redistributions of source code must retain the above copyright
13 1.1 nonaka * notice unmodified, this list of conditions, and the following
14 1.1 nonaka * disclaimer.
15 1.1 nonaka * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 nonaka * notice, this list of conditions and the following disclaimer in the
17 1.1 nonaka * documentation and/or other materials provided with the distribution.
18 1.1 nonaka *
19 1.1 nonaka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 nonaka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 nonaka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 nonaka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 nonaka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 nonaka * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 nonaka * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 nonaka * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 nonaka * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1 nonaka * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 nonaka */
30 1.1 nonaka
31 1.1 nonaka #include <sys/cdefs.h>
32 1.6 nonaka __KERNEL_RCSID(0, "$NetBSD: hyperv_common.c,v 1.6 2022/05/20 13:55:17 nonaka Exp $");
33 1.1 nonaka
34 1.1 nonaka #include "hyperv.h"
35 1.1 nonaka
36 1.1 nonaka #include <sys/param.h>
37 1.1 nonaka #include <sys/systm.h>
38 1.1 nonaka #include <sys/kernel.h>
39 1.1 nonaka #include <sys/bus.h>
40 1.1 nonaka #include <sys/kmem.h>
41 1.1 nonaka
42 1.1 nonaka #include <dev/hyperv/hypervreg.h>
43 1.1 nonaka #include <dev/hyperv/hypervvar.h>
44 1.1 nonaka
45 1.6 nonaka u_int hyperv_ver_major;
46 1.1 nonaka hyperv_tc64_t hyperv_tc64;
47 1.1 nonaka
48 1.1 nonaka int hyperv_nullop(void);
49 1.1 nonaka void hyperv_voidop(void);
50 1.1 nonaka uint64_t hyperv_hypercall_error(uint64_t, paddr_t, paddr_t);
51 1.1 nonaka
52 1.1 nonaka __weak_alias(hyperv_hypercall, hyperv_hypercall_error);
53 1.1 nonaka __weak_alias(hyperv_hypercall_enabled, hyperv_nullop);
54 1.1 nonaka __weak_alias(hyperv_synic_supported, hyperv_nullop);
55 1.2 nonaka __weak_alias(hyperv_is_gen1, hyperv_nullop);
56 1.1 nonaka __weak_alias(hyperv_set_event_proc, hyperv_voidop);
57 1.1 nonaka __weak_alias(hyperv_set_message_proc, hyperv_voidop);
58 1.1 nonaka __weak_alias(hyperv_send_eom, hyperv_voidop);
59 1.1 nonaka __weak_alias(hyperv_intr, hyperv_voidop);
60 1.4 nonaka __weak_alias(hyperv_get_vcpuid, hyperv_nullop);
61 1.1 nonaka __weak_alias(vmbus_init_interrupts_md, hyperv_voidop);
62 1.1 nonaka __weak_alias(vmbus_deinit_interrupts_md, hyperv_voidop);
63 1.1 nonaka __weak_alias(vmbus_init_synic_md, hyperv_voidop);
64 1.1 nonaka __weak_alias(vmbus_deinit_synic_md, hyperv_voidop);
65 1.1 nonaka
66 1.1 nonaka int
67 1.1 nonaka hyperv_nullop(void)
68 1.1 nonaka {
69 1.1 nonaka return 0;
70 1.1 nonaka }
71 1.1 nonaka
72 1.1 nonaka void
73 1.1 nonaka hyperv_voidop(void)
74 1.1 nonaka {
75 1.1 nonaka }
76 1.1 nonaka
77 1.1 nonaka uint64_t
78 1.1 nonaka hyperv_hypercall_error(uint64_t control, paddr_t in_paddr, paddr_t out_paddr)
79 1.1 nonaka {
80 1.1 nonaka return ~HYPERCALL_STATUS_SUCCESS;
81 1.1 nonaka }
82 1.1 nonaka
83 1.1 nonaka uint64_t
84 1.1 nonaka hyperv_hypercall_post_message(paddr_t msg)
85 1.1 nonaka {
86 1.1 nonaka
87 1.1 nonaka return hyperv_hypercall(HYPERCALL_POST_MESSAGE, msg, 0);
88 1.1 nonaka }
89 1.1 nonaka
90 1.1 nonaka uint64_t
91 1.1 nonaka hyperv_hypercall_signal_event(paddr_t monprm)
92 1.1 nonaka {
93 1.1 nonaka
94 1.1 nonaka return hyperv_hypercall(HYPERCALL_SIGNAL_EVENT, monprm, 0);
95 1.1 nonaka }
96 1.1 nonaka
97 1.1 nonaka int
98 1.1 nonaka hyperv_guid2str(const struct hyperv_guid *guid, char *buf, size_t sz)
99 1.1 nonaka {
100 1.1 nonaka const uint8_t *d = guid->hv_guid;
101 1.1 nonaka
102 1.1 nonaka return snprintf(buf, sz, "%02x%02x%02x%02x-"
103 1.1 nonaka "%02x%02x-%02x%02x-%02x%02x-"
104 1.1 nonaka "%02x%02x%02x%02x%02x%02x",
105 1.1 nonaka d[3], d[2], d[1], d[0],
106 1.1 nonaka d[5], d[4], d[7], d[6], d[8], d[9],
107 1.1 nonaka d[10], d[11], d[12], d[13], d[14], d[15]);
108 1.1 nonaka }
109 1.1 nonaka
110 1.1 nonaka /*
111 1.1 nonaka * Hyper-V bus_dma utilities.
112 1.1 nonaka */
113 1.1 nonaka void *
114 1.1 nonaka hyperv_dma_alloc(bus_dma_tag_t dmat, struct hyperv_dma *dma, bus_size_t size,
115 1.6 nonaka bus_size_t alignment, bus_size_t boundary, int nsegs)
116 1.1 nonaka {
117 1.1 nonaka int rseg, error;
118 1.1 nonaka
119 1.1 nonaka KASSERT(dma != NULL);
120 1.1 nonaka KASSERT(dma->segs == NULL);
121 1.1 nonaka KASSERT(nsegs > 0);
122 1.1 nonaka
123 1.6 nonaka dma->segs = kmem_zalloc(sizeof(*dma->segs) * nsegs, KM_SLEEP);
124 1.1 nonaka dma->nsegs = nsegs;
125 1.1 nonaka
126 1.1 nonaka error = bus_dmamem_alloc(dmat, size, alignment, boundary, dma->segs,
127 1.6 nonaka nsegs, &rseg, BUS_DMA_WAITOK);
128 1.1 nonaka if (error) {
129 1.1 nonaka printf("%s: bus_dmamem_alloc failed: error=%d\n",
130 1.1 nonaka __func__, error);
131 1.1 nonaka goto fail1;
132 1.1 nonaka }
133 1.1 nonaka error = bus_dmamem_map(dmat, dma->segs, rseg, size, &dma->addr,
134 1.6 nonaka BUS_DMA_WAITOK);
135 1.1 nonaka if (error) {
136 1.1 nonaka printf("%s: bus_dmamem_map failed: error=%d\n",
137 1.1 nonaka __func__, error);
138 1.1 nonaka goto fail2;
139 1.1 nonaka }
140 1.6 nonaka error = bus_dmamap_create(dmat, size, rseg, size, boundary,
141 1.6 nonaka BUS_DMA_WAITOK, &dma->map);
142 1.1 nonaka if (error) {
143 1.1 nonaka printf("%s: bus_dmamap_create failed: error=%d\n",
144 1.1 nonaka __func__, error);
145 1.1 nonaka goto fail3;
146 1.1 nonaka }
147 1.1 nonaka error = bus_dmamap_load(dmat, dma->map, dma->addr, size, NULL,
148 1.6 nonaka BUS_DMA_READ | BUS_DMA_WRITE | BUS_DMA_WAITOK);
149 1.1 nonaka if (error) {
150 1.1 nonaka printf("%s: bus_dmamap_load failed: error=%d\n",
151 1.1 nonaka __func__, error);
152 1.1 nonaka goto fail4;
153 1.1 nonaka }
154 1.1 nonaka
155 1.3 nonaka memset(dma->addr, 0, dma->map->dm_mapsize);
156 1.3 nonaka bus_dmamap_sync(dmat, dma->map, 0, dma->map->dm_mapsize,
157 1.3 nonaka BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
158 1.3 nonaka
159 1.1 nonaka return dma->addr;
160 1.1 nonaka
161 1.1 nonaka fail4: bus_dmamap_destroy(dmat, dma->map);
162 1.1 nonaka fail3: bus_dmamem_unmap(dmat, dma->addr, size);
163 1.1 nonaka dma->addr = NULL;
164 1.1 nonaka fail2: bus_dmamem_free(dmat, dma->segs, rseg);
165 1.1 nonaka fail1: kmem_free(dma->segs, sizeof(*dma->segs) * nsegs);
166 1.1 nonaka dma->segs = NULL;
167 1.1 nonaka dma->nsegs = 0;
168 1.1 nonaka return NULL;
169 1.1 nonaka }
170 1.1 nonaka
171 1.1 nonaka void
172 1.1 nonaka hyperv_dma_free(bus_dma_tag_t dmat, struct hyperv_dma *dma)
173 1.1 nonaka {
174 1.1 nonaka bus_size_t size = dma->map->dm_mapsize;
175 1.1 nonaka int rsegs = dma->map->dm_nsegs;
176 1.1 nonaka
177 1.1 nonaka bus_dmamap_unload(dmat, dma->map);
178 1.1 nonaka bus_dmamap_destroy(dmat, dma->map);
179 1.1 nonaka bus_dmamem_unmap(dmat, dma->addr, size);
180 1.1 nonaka dma->addr = NULL;
181 1.1 nonaka bus_dmamem_free(dmat, dma->segs, rsegs);
182 1.1 nonaka kmem_free(dma->segs, sizeof(*dma->segs) * dma->nsegs);
183 1.1 nonaka dma->segs = NULL;
184 1.1 nonaka dma->nsegs = 0;
185 1.1 nonaka }
186