2 * Copyright (c) 2006 Kip Macy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/mutex.h>
41 #include <machine/hypervisorvar.h>
42 #include <machine/hv_api.h>
43 #include <machine/cddl/mdesc.h>
44 #include <machine/cddl/mdesc_impl.h>
47 static void mdesc_postvm_init(void *);
49 * It doesn't really matter when this happens right now
50 * it just needs to happen before device initialization
51 * and after VM initialization. Later on we will end up
52 * doing this statically from pmap_bootstrap so that we
53 * can kill off all calls to OBP. OBP removal is not in
54 * the critical path for sun4v at this time.
56 SYSINIT(mdesc_init, SI_SUB_CPU, SI_ORDER_FIRST, mdesc_postvm_init, NULL);
59 #define UNIMPLEMENTED panic("%s not implemented.", __FUNCTION__)
61 typedef struct mdesc_memops_ {
62 void *(*mm_buf_alloc)(size_t size, size_t align);
63 void (*mm_buf_free)(void *, size_t size);
64 void *(*mm_meta_alloc)(size_t size);
65 void (*mm_meta_free)(void *, size_t size);
68 typedef struct mdesc_ {
69 uint64_t md_gen; /* md-generation# */
71 void *md_addr; /* address of raw MD */
72 uint64_t md_size; /* size of raw MD */
73 uint64_t md_buf_size; /* size of buffer allocated for MD */
74 int md_refcount; /* reference count */
75 mdesc_memops_t *md_memops; /* Memory operations for this MD */
80 static mdesc_t *curr_mdesc = NULL;
81 static struct mtx curr_mdesc_lock;
82 static mdesc_memops_t *mdesc_memops;
84 static void *mdesc_boot_buf_alloc(size_t size, size_t align);
85 static void mdesc_boot_buf_free(void *buf, size_t align);
86 static void *mdesc_boot_meta_alloc(size_t size);
87 static void mdesc_boot_meta_free(void *buf, size_t size);
89 static void *mdesc_buf_alloc(size_t size, size_t align);
90 static void mdesc_buf_free(void *buf, size_t align);
91 static void *mdesc_meta_alloc(size_t size);
92 static void mdesc_meta_free(void *buf, size_t size);
94 static mdesc_memops_t mdesc_boot_memops = {
97 mdesc_boot_meta_alloc,
101 static mdesc_memops_t mdesc_generic_memops = {
109 mdesc_boot_buf_alloc(size_t size, size_t align)
115 mdesc_boot_buf_free(void *buf, size_t align)
121 mdesc_boot_meta_alloc(size_t size)
127 mdesc_boot_meta_free(void *buf, size_t size)
133 mdesc_buf_alloc(size_t size, size_t align)
135 return pmap_alloc_zeroed_contig_pages(size/PAGE_SIZE, align);
139 mdesc_buf_free(void *buf, size_t align)
141 contigfree(buf, PAGE_SIZE /*fix me*/, M_MDPROP);
145 mdesc_meta_alloc(size_t size)
147 return malloc(size, M_MDPROP, M_NOWAIT);
151 mdesc_meta_free(void *buf, size_t size)
160 mtx_init(&curr_mdesc_lock, "current machine description lock", NULL, MTX_DEF);
161 mdesc_memops = &mdesc_boot_memops;
165 mdesc_postvm_init(void *unused)
167 mdesc_memops = &mdesc_generic_memops;
175 mdp = (mdesc_t *)(mdesc_memops->mm_meta_alloc)(sizeof(mdesc_t));
177 bzero(mdp, sizeof(*mdp));
178 mdp->md_memops = mdesc_memops;
179 mtx_init(&mdp->md_lock, "machine descriptor lock", NULL, MTX_DEF);
189 uint64_t mdesc_size, mdesc_buf_size = 0;
196 (mdesc_memops->mm_buf_free)(buf, mdesc_buf_size);
200 rv = hv_mach_desc((uint64_t)0, &mdesc_size);
201 if (rv != H_EOK && rv != H_EINVAL)
202 printf("retrying to fetch mdesc size\n");
203 } while (rv != H_EOK && rv != H_EINVAL);
205 mdesc_size = mdesc_buf_size = round_page(mdesc_size);
207 if ((buf = (*mdesc_memops->mm_buf_alloc)(mdesc_buf_size, PAGE_SIZE)) == NULL) {
212 rv = hv_mach_desc(vtophys(buf), &mdesc_size);
214 if (rv != H_EOK && rv != H_EINVAL) {
217 } while (mdesc_size > mdesc_buf_size);
219 KASSERT(rv == H_EOK, ("unexpected return from hv_mach_desc"));
221 /* XXX we ignore the generation count... not all versions may
225 if (curr_mdesc->md_refcount == 0) {
226 (*mdesc_memops->mm_buf_free) (curr_mdesc->md_addr, curr_mdesc->md_size);
228 panic("out of date machine description list not implemented");
231 mtx_lock(&curr_mdesc_lock);
233 curr_mdesc->md_gen = gen;
235 curr_mdesc->md_addr = buf;
236 curr_mdesc->md_size = mdesc_size;
237 curr_mdesc->md_buf_size = mdesc_buf_size;
238 mtx_unlock(&curr_mdesc_lock);
244 (*mdesc_memops->mm_buf_free)(buf, mdesc_buf_size);
255 * XXX This should actually happen in init
257 if (curr_mdesc == NULL) {
258 if ((curr_mdesc = mdesc_alloc()) == NULL)
259 panic("machine description allocation failed");
260 if ((rc = mdesc_update()) != 0)
261 panic("machine description update failed: %d", rc);
264 mtx_lock(&curr_mdesc_lock);
265 curr_mdesc->md_refcount++;
266 mdp = md_init_intern(curr_mdesc->md_addr,
267 curr_mdesc->md_memops->mm_meta_alloc,
268 curr_mdesc->md_memops->mm_meta_free);
269 mtx_unlock(&curr_mdesc_lock);
279 mdp = (md_impl_t *)ptr;
282 mtx_lock(&curr_mdesc_lock)
284 /* XXX check against generation */
285 if (curr_mdesc->md_gen == mdp->md_gen) {
286 curr_mdesc->md_refcount--;
287 mtx_unlock(&curr_mdesc_lock)
290 mtx_unlock(&curr_mdesc_lock);
292 * MD is on the out of date list
298 * We don't keep an out of date list currently
300 mtx_lock(&curr_mdesc_lock);
301 curr_mdesc->md_refcount--;
302 mtx_unlock(&curr_mdesc_lock);
303 mdp->freep(mdp, sizeof(md_impl_t));