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