summaryrefslogtreecommitdiffstats
path: root/sys/sun4v/mdesc/mdesc_init.c
blob: bb2990de94c22f8e654be7cf4d377dea9ce9ba2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*-
 * Copyright (c) 2006 Kip Macy
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <vm/vm.h>
#include <vm/pmap.h>

#include <machine/hypervisorvar.h>
#include <machine/hv_api.h>
#include <machine/cddl/mdesc.h>
#include <machine/cddl/mdesc_impl.h>


static void mdesc_postvm_init(void *);
/*
 * It doesn't really matter when this happens right now
 * it just needs to happen before device initialization 
 * and after VM initialization. Later on we will end up 
 * doing this statically from pmap_bootstrap so that we 
 * can kill off all calls to OBP. OBP removal is not in
 * the critical path for sun4v at this time.
 */
SYSINIT(mdesc_init, SI_SUB_CPU, SI_ORDER_SECOND, mdesc_postvm_init, NULL);


#define UNIMPLEMENTED panic("%s not implemented.", __FUNCTION__)

typedef struct mdesc_memops_ {
        void            *(*mm_buf_alloc)(size_t size, size_t align);
        void            (*mm_buf_free)(void *, size_t size);
        void            *(*mm_meta_alloc)(size_t size);
        void            (*mm_meta_free)(void *, size_t size);
} mdesc_memops_t;

typedef struct mdesc_ {
        uint64_t        md_gen;         /* md-generation# */
        struct mtx      md_lock;       
        void           *md_addr;        /* address of raw MD */
        uint64_t        md_size;        /* size of raw MD */
        uint64_t        md_buf_size;    /* size of buffer allocated for MD */
        int             md_refcount;    /* reference count */
        mdesc_memops_t *md_memops;      /* Memory operations for this MD */
} mdesc_t;



static mdesc_t        *curr_mdesc = NULL;
static struct mtx      curr_mdesc_lock;
static mdesc_memops_t *mdesc_memops;

static void *mdesc_boot_buf_alloc(size_t size, size_t align);
static void mdesc_boot_buf_free(void *buf, size_t align);
static void *mdesc_boot_meta_alloc(size_t size);
static void mdesc_boot_meta_free(void *buf, size_t size);

static void *mdesc_buf_alloc(size_t size, size_t align);
static void mdesc_buf_free(void *buf, size_t align);
static void *mdesc_meta_alloc(size_t size);
static void mdesc_meta_free(void *buf, size_t size);

static mdesc_memops_t mdesc_boot_memops = {
	mdesc_boot_buf_alloc,
	mdesc_boot_buf_free,
	mdesc_boot_meta_alloc,
	mdesc_boot_meta_free,
};

static mdesc_memops_t mdesc_generic_memops = {
	mdesc_buf_alloc,
	mdesc_buf_free,
	mdesc_meta_alloc,
	mdesc_meta_free,
};

static void *
mdesc_boot_buf_alloc(size_t size, size_t align)
{
	UNIMPLEMENTED;
}

static void 
mdesc_boot_buf_free(void *buf, size_t align)
{
	UNIMPLEMENTED;
}

static void *
mdesc_boot_meta_alloc(size_t size)
{
	UNIMPLEMENTED;
}

static void 
mdesc_boot_meta_free(void *buf, size_t size)
{
	UNIMPLEMENTED;
}

static void *
mdesc_buf_alloc(size_t size, size_t align)
{
	return pmap_alloc_zeroed_contig_pages(size/PAGE_SIZE, align);
}

static void 
mdesc_buf_free(void *buf, size_t align)
{
	contigfree(buf, PAGE_SIZE /*fix me*/, M_MDPROP);
}

static void *
mdesc_meta_alloc(size_t size)
{
	return malloc(size, M_MDPROP, M_NOWAIT);
}

static void 
mdesc_meta_free(void *buf, size_t size)
{
	free(buf, M_MDPROP);
}

void 
mdesc_init(void)
{

	mtx_init(&curr_mdesc_lock, "current machine description lock", NULL, MTX_DEF);
	mdesc_memops = &mdesc_boot_memops;
}

static void 
mdesc_postvm_init(void *unused)
{
	mdesc_memops = &mdesc_generic_memops;
}

static mdesc_t *
mdesc_alloc(void)
{
	mdesc_t *mdp;
	
	mdp = (mdesc_t *)(mdesc_memops->mm_meta_alloc)(sizeof(mdesc_t));
	if (mdp != NULL) {
		bzero(mdp, sizeof(*mdp));
		mdp->md_memops = mdesc_memops;
		mtx_init(&mdp->md_lock, "machine descriptor lock", NULL, MTX_DEF);
	}
	
	return (mdp);
}

int 
mdesc_update(void)
{
	uint64_t rv;
	uint64_t mdesc_size, mdesc_buf_size = 0;
	void    *buf = NULL;
#ifdef notyet
	uint64_t gen;
#endif
	do {
		if (buf != NULL)
			(mdesc_memops->mm_buf_free)(buf, mdesc_buf_size);

		mdesc_size = 0LL;
		do {
			rv = hv_mach_desc((uint64_t)0, &mdesc_size);
			if (rv != H_EOK && rv != H_EINVAL)
				printf("retrying to fetch mdesc size\n");
		} while (rv != H_EOK && rv != H_EINVAL); 

		mdesc_size = mdesc_buf_size = round_page(mdesc_size);
		
		if ((buf = (*mdesc_memops->mm_buf_alloc)(mdesc_buf_size, PAGE_SIZE)) == NULL) {
			rv = ENOMEM;
			goto done;
		}
		
		rv = hv_mach_desc(vtophys(buf), &mdesc_size);

		if (rv != H_EOK && rv != H_EINVAL) {
			goto done;
		}
	} while (mdesc_size > mdesc_buf_size);
	
	KASSERT(rv == H_EOK, ("unexpected return from hv_mach_desc"));
	
	/* XXX we ignore the generation count... not all versions may 
	 * support it  
	 */

	if (curr_mdesc->md_refcount == 0) {
		(*mdesc_memops->mm_buf_free) (curr_mdesc->md_addr, curr_mdesc->md_size);
	} else {
		panic("out of date machine description list not implemented");
	}

	mtx_lock(&curr_mdesc_lock);
#ifdef notyet
	curr_mdesc->md_gen = gen;
#endif
	curr_mdesc->md_addr = buf;
	curr_mdesc->md_size = mdesc_size;
	curr_mdesc->md_buf_size = mdesc_buf_size;
	mtx_unlock(&curr_mdesc_lock);
	
	return (0);

 done:
	if (buf != NULL)
		(*mdesc_memops->mm_buf_free)(buf, mdesc_buf_size);
	return (rv);
}

md_t *
md_get(void)
{
	md_t *mdp;
	int rc;
	
	/*
	 * XXX This should actually happen in init
	 */
	if (curr_mdesc == NULL) {
		if ((curr_mdesc = mdesc_alloc()) == NULL)
			panic("machine description allocation failed");
		if ((rc = mdesc_update()) != 0)
			panic("machine description update failed: %d", rc);
	}
		
	mtx_lock(&curr_mdesc_lock);
	curr_mdesc->md_refcount++;
	mdp = md_init_intern(curr_mdesc->md_addr, 
			     curr_mdesc->md_memops->mm_meta_alloc,
			     curr_mdesc->md_memops->mm_meta_free);
	mtx_unlock(&curr_mdesc_lock);

	return (mdp);
}

void
md_put(md_t *ptr)
{
	md_impl_t *mdp;
	
	mdp = (md_impl_t *)ptr;

#ifdef notyet
	mtx_lock(&curr_mdesc_lock)

	/* XXX check against generation */
	if (curr_mdesc->md_gen == mdp->md_gen) {
		curr_mdesc->md_refcount--;
		mtx_unlock(&curr_mdesc_lock)
		goto done;
	}
	mtx_unlock(&curr_mdesc_lock);
	/*
	 * MD is on the out of date list 
	 */

 done:
#endif
	/*
	 * We don't keep an out of date list currently
	 */
	mtx_lock(&curr_mdesc_lock);
	curr_mdesc->md_refcount--;
	mtx_unlock(&curr_mdesc_lock);
	mdp->freep(mdp, sizeof(md_impl_t));
}


OpenPOWER on IntegriCloud