preprocessor cleanup: __xpv
[unleashed.git] / arch / x86 / kernel / platform / i86pc / os / mp_implfuncs.c
blob86b0a835d3240b56db5cc8089fd111ee2b536f9b
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 #define PSMI_1_7
27 #include <sys/vmem.h>
28 #include <vm/hat.h>
29 #include <sys/modctl.h>
30 #include <vm/seg_kmem.h>
31 #include <sys/psm.h>
32 #include <sys/psm_modctl.h>
33 #include <sys/smp_impldefs.h>
34 #include <sys/reboot.h>
37 * External reference functions
39 extern void *get_next_mach(void *, char *);
40 extern void close_mach_list(void);
41 extern void open_mach_list(void);
44 * from startup.c - kernel VA range allocator for device mappings
46 extern void *device_arena_alloc(size_t size, int vm_flag);
47 extern void device_arena_free(void * vaddr, size_t size);
49 void psm_modloadonly(void);
50 void psm_install(void);
53 * Local Function Prototypes
55 static struct modlinkage *psm_modlinkage_alloc(struct psm_info *infop);
56 static void psm_modlinkage_free(struct modlinkage *mlinkp);
58 static char *psm_get_impl_module(int first);
60 static int mod_installpsm(struct modlpsm *modl, struct modlinkage *modlp);
61 static int mod_removepsm(struct modlpsm *modl, struct modlinkage *modlp);
62 static int mod_infopsm(struct modlpsm *modl, struct modlinkage *modlp, int *p0);
63 struct mod_ops mod_psmops = {
64 mod_installpsm, mod_removepsm, mod_infopsm
67 static struct psm_sw psm_swtab = {
68 &psm_swtab, &psm_swtab, NULL, 0
71 kmutex_t psmsw_lock; /* lock accesses to psmsw */
72 struct psm_sw *psmsw = &psm_swtab; /* start of all psm_sw */
74 static struct modlinkage *
75 psm_modlinkage_alloc(struct psm_info *infop)
77 int memsz;
78 struct modlinkage *mlinkp;
79 struct modlpsm *mlpsmp;
80 struct psm_sw *swp;
82 memsz = sizeof (struct modlinkage) + sizeof (struct modlpsm) +
83 sizeof (struct psm_sw);
84 mlinkp = kmem_zalloc(memsz, KM_NOSLEEP);
85 if (!mlinkp) {
86 cmn_err(CE_WARN, "!psm_mod_init: Cannot install %s",
87 infop->p_mach_idstring);
88 return (NULL);
90 mlpsmp = (struct modlpsm *)(mlinkp + 1);
91 swp = (struct psm_sw *)(mlpsmp + 1);
93 mlinkp->ml_rev = MODREV_1;
94 mlinkp->ml_linkage[0] = (void *)mlpsmp;
95 mlinkp->ml_linkage[1] = NULL;
97 mlpsmp->psm_modops = &mod_psmops;
98 mlpsmp->psm_linkinfo = infop->p_mach_desc;
99 mlpsmp->psm_swp = swp;
101 swp->psw_infop = infop;
103 return (mlinkp);
106 static void
107 psm_modlinkage_free(struct modlinkage *mlinkp)
109 if (!mlinkp)
110 return;
112 (void) kmem_free(mlinkp, (sizeof (struct modlinkage) +
113 sizeof (struct modlpsm) + sizeof (struct psm_sw)));
117 psm_mod_init(void **handlepp, struct psm_info *infop)
119 struct modlinkage **modlpp = (struct modlinkage **)handlepp;
120 int status;
121 struct modlinkage *mlinkp;
123 if (!*modlpp) {
124 mlinkp = psm_modlinkage_alloc(infop);
125 if (!mlinkp)
126 return (ENOSPC);
127 } else
128 mlinkp = *modlpp;
130 status = mod_install(mlinkp);
131 if (status) {
132 psm_modlinkage_free(mlinkp);
133 *modlpp = NULL;
134 } else
135 *modlpp = mlinkp;
137 return (status);
140 /*ARGSUSED1*/
142 psm_mod_fini(void **handlepp, struct psm_info *infop)
144 struct modlinkage **modlpp = (struct modlinkage **)handlepp;
145 int status;
147 status = mod_remove(*modlpp);
148 if (status == 0) {
149 psm_modlinkage_free(*modlpp);
150 *modlpp = NULL;
152 return (status);
156 psm_mod_info(void **handlepp, struct psm_info *infop, struct modinfo *modinfop)
158 struct modlinkage **modlpp = (struct modlinkage **)handlepp;
159 int status;
160 struct modlinkage *mlinkp;
162 if (!*modlpp) {
163 mlinkp = psm_modlinkage_alloc(infop);
164 if (!mlinkp)
165 return (0);
166 } else
167 mlinkp = *modlpp;
169 status = mod_info(mlinkp, modinfop);
171 if (!status) {
172 psm_modlinkage_free(mlinkp);
173 *modlpp = NULL;
174 } else
175 *modlpp = mlinkp;
177 return (status);
181 psm_add_intr(int lvl, avfunc xxintr, char *name, int vect, caddr_t arg)
183 return (add_avintr(NULL, lvl, xxintr, name, vect,
184 arg, NULL, NULL, NULL));
188 psm_add_nmintr(int lvl, avfunc xxintr, char *name, caddr_t arg)
190 return (add_nmintr(lvl, xxintr, name, arg));
193 processorid_t
194 psm_get_cpu_id(void)
196 return (CPU->cpu_id);
199 caddr_t
200 psm_map_phys_new(paddr_t addr, size_t len, int prot)
202 uint_t pgoffset;
203 paddr_t base;
204 pgcnt_t npages;
205 caddr_t cvaddr;
207 if (len == 0)
208 return (0);
210 pgoffset = addr & MMU_PAGEOFFSET;
211 base = addr;
212 npages = mmu_btopr(len + pgoffset);
213 cvaddr = device_arena_alloc(ptob(npages), VM_NOSLEEP);
214 if (cvaddr == NULL)
215 return (0);
216 hat_devload(kas.a_hat, cvaddr, mmu_ptob(npages), mmu_btop(base),
217 prot, HAT_LOAD_LOCK);
218 return (cvaddr + pgoffset);
221 void
222 psm_unmap_phys(caddr_t addr, size_t len)
224 uint_t pgoffset;
225 caddr_t base;
226 pgcnt_t npages;
228 if (len == 0)
229 return;
231 pgoffset = (uintptr_t)addr & MMU_PAGEOFFSET;
232 base = addr - pgoffset;
233 npages = mmu_btopr(len + pgoffset);
234 hat_unload(kas.a_hat, base, ptob(npages), HAT_UNLOAD_UNLOCK);
235 device_arena_free(base, ptob(npages));
238 caddr_t
239 psm_map_new(paddr_t addr, size_t len, int prot)
241 int phys_prot = PROT_READ;
243 ASSERT(prot == (prot & (PSM_PROT_WRITE | PSM_PROT_READ)));
244 if (prot & PSM_PROT_WRITE)
245 phys_prot |= PROT_WRITE;
247 return (psm_map_phys(addr, len, phys_prot));
250 #undef psm_map_phys
251 #undef psm_map
253 caddr_t
254 psm_map_phys(uint32_t addr, size_t len, int prot)
256 return (psm_map_phys_new((paddr_t)(addr & 0xffffffff), len, prot));
259 caddr_t
260 psm_map(uint32_t addr, size_t len, int prot)
262 return (psm_map_new((paddr_t)(addr & 0xffffffff), len, prot));
265 void
266 psm_unmap(caddr_t addr, size_t len)
268 uint_t pgoffset;
269 caddr_t base;
270 pgcnt_t npages;
272 if (len == 0)
273 return;
275 pgoffset = (uintptr_t)addr & MMU_PAGEOFFSET;
276 base = addr - pgoffset;
277 npages = mmu_btopr(len + pgoffset);
278 hat_unload(kas.a_hat, base, ptob(npages), HAT_UNLOAD_UNLOCK);
279 device_arena_free(base, ptob(npages));
282 /*ARGSUSED1*/
283 static int
284 mod_installpsm(struct modlpsm *modl, struct modlinkage *modlp)
286 struct psm_sw *swp;
288 swp = modl->psm_swp;
289 mutex_enter(&psmsw_lock);
290 psmsw->psw_back->psw_forw = swp;
291 swp->psw_back = psmsw->psw_back;
292 swp->psw_forw = psmsw;
293 psmsw->psw_back = swp;
294 swp->psw_flag |= PSM_MOD_INSTALL;
295 mutex_exit(&psmsw_lock);
296 return (0);
299 /*ARGSUSED1*/
300 static int
301 mod_removepsm(struct modlpsm *modl, struct modlinkage *modlp)
303 struct psm_sw *swp;
305 swp = modl->psm_swp;
306 mutex_enter(&psmsw_lock);
307 if (swp->psw_flag & PSM_MOD_IDENTIFY) {
308 mutex_exit(&psmsw_lock);
309 return (EBUSY);
311 if (!(swp->psw_flag & PSM_MOD_INSTALL)) {
312 mutex_exit(&psmsw_lock);
313 return (0);
316 swp->psw_back->psw_forw = swp->psw_forw;
317 swp->psw_forw->psw_back = swp->psw_back;
318 mutex_exit(&psmsw_lock);
319 return (0);
322 /*ARGSUSED1*/
323 static int
324 mod_infopsm(struct modlpsm *modl, struct modlinkage *modlp, int *p0)
326 *p0 = (int)modl->psm_swp->psw_infop->p_owner;
327 return (0);
330 #define DEFAULT_PSM_MODULE "uppc"
332 static char *
333 psm_get_impl_module(int first)
335 static char **pnamep;
336 static char *psm_impl_module_list[] = {
337 DEFAULT_PSM_MODULE,
338 (char *)0
340 static void *mhdl = NULL;
341 static char machname[MAXNAMELEN];
343 if (first)
344 pnamep = psm_impl_module_list;
346 if (*pnamep != NULL)
347 return (*pnamep++);
349 mhdl = get_next_mach(mhdl, machname);
350 if (mhdl)
351 return (machname);
352 return (NULL);
355 void
356 psm_modload(void)
358 char *this;
360 mutex_init(&psmsw_lock, NULL, MUTEX_DEFAULT, NULL);
361 open_mach_list();
363 for (this = psm_get_impl_module(1); this != NULL;
364 this = psm_get_impl_module(0)) {
365 if (modload("mach", this) == -1)
366 cmn_err(CE_CONT, "!Skipping psm: %s\n", this);
368 close_mach_list();
371 #define NOTSUP_MSG "This version of Solaris does not support this hardware"
373 void
374 psm_install(void)
376 struct psm_sw *swp, *cswp;
377 struct psm_ops *opsp;
378 char machstring[15];
379 int err, psmcnt = 0;
381 mutex_enter(&psmsw_lock);
382 for (swp = psmsw->psw_forw; swp != psmsw; ) {
383 opsp = swp->psw_infop->p_ops;
384 if (opsp->psm_probe) {
385 if ((*opsp->psm_probe)() == PSM_SUCCESS) {
386 psmcnt++;
387 swp->psw_flag |= PSM_MOD_IDENTIFY;
388 swp = swp->psw_forw;
389 continue;
392 /* remove the unsuccessful psm modules */
393 cswp = swp;
394 swp = swp->psw_forw;
396 mutex_exit(&psmsw_lock);
397 (void) strcpy(&machstring[0], cswp->psw_infop->p_mach_idstring);
398 err = mod_remove_by_name(cswp->psw_infop->p_mach_idstring);
399 if (err)
400 cmn_err(CE_WARN, "!%s: mod_remove_by_name failed %d",
401 &machstring[0], err);
402 mutex_enter(&psmsw_lock);
404 mutex_exit(&psmsw_lock);
405 if (psmcnt == 0)
406 halt(NOTSUP_MSG);
407 (*psminitf)();
411 * Return 1 if kernel debugger is present, and 0 if not.
414 psm_debugger(void)
416 return ((boothowto & RB_DEBUG) != 0);