xref: /titanic_44/usr/src/uts/sun4v/os/mach_descrip.c (revision bd335c6465ddbafe543900df4b03247bfa288eff)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/machsystm.h>
30 #include <sys/vm.h>
31 #include <sys/cpu.h>
32 #include <sys/intreg.h>
33 #include <sys/machcpuvar.h>
34 #include <sys/machparam.h>
35 #include <vm/hat_sfmmu.h>
36 #include <vm/seg_kmem.h>
37 #include <sys/error.h>
38 #include <sys/hypervisor_api.h>
39 #include <sys/types.h>
40 #include <sys/kstat.h>
41 #ifdef MACH_DESC_DEBUG
42 #include <sys/promif.h>		/* for prom_printf */
43 #endif
44 #include <sys/sysmacros.h>
45 #include <sys/mach_descrip.h>
46 
47 /*
48  * Basic code to pull in the machine description from the Hypervisor
49  * An equivalent to this should really be available from mlsetup
50  * for really early info, but for the time being we are content to
51  * invoke this from startup_end once the VM system has been initialised.
52  * To do this we use the intrq allocator which means that
53  * this function should be called after intrq_init();
54  * We try and do this early enough however that it is useful to other
55  * components within the kernel.
56  * Also, user-level entities can grab the machine description via
57  * kstat and/or the mdesc device driver.
58  */
59 
60 
61 machine_descrip_t machine_descrip;
62 
63 
64 #ifdef MACH_DESC_DEBUG
65 #define	MDP(ARGS)	prom_printf ARGS
66 static void
67 dump_buf(uint8_t *bufp, int size)
68 {
69 	int i;
70 	for (i = 0; i < size; i += 16) {
71 		int j;
72 		prom_printf("0x%04x :", i);
73 		for (j = 0; j < 16 && (i+j) < size; j++)
74 			prom_printf(" %02x", bufp[i+j]);
75 		prom_printf("\n");
76 	}
77 }
78 #else
79 #define	MDP(x)
80 #endif
81 
82 
83 
84 
85 
86 void
87 mach_descrip_init(void)
88 {
89 	uint64_t md_size, ret;
90 
91 	MDP(("MD: Requesting buffer size\n"));
92 
93 	md_size = 0LL;
94 	(void) hv_mach_desc((uint64_t)0, &md_size);
95 	MDP(("MD: buffer size is %d\n", md_size));
96 
97 		/* Round allocated space to nearest page */
98 	machine_descrip.space = P2ROUNDUP(md_size, PAGESIZE);
99 	MDP(("MD: allocated space is %d\n", machine_descrip.space));
100 	machine_descrip.va = contig_mem_alloc(machine_descrip.space);
101 	if (machine_descrip.va == NULL)
102 		cmn_err(CE_PANIC, "Allocation for machine description failed");
103 
104 	MDP(("MD: allocated va = 0x%p (size 0x%llx)\n",
105 		machine_descrip.va, machine_descrip.space));
106 
107 	machine_descrip.pa = va_to_pa(machine_descrip.va);
108 
109 	MDP(("MD: allocated pa = 0x%llx\n", machine_descrip.pa));
110 
111 	ret = hv_mach_desc(machine_descrip.pa, &md_size);
112 	MDP(("MD: HV return code = %ld\n", ret));
113 
114 	if (ret != H_EOK) {
115 		MDP(("MD: Failed with code %ld from HV\n", ret));
116 
117 		machine_descrip.size = 0;
118 
119 	} else {
120 		MDP(("MD: Grabbed %d bytes from HV\n", md_size));
121 #ifdef	MACH_DESC_DEBUG
122 		dump_buf((uint8_t *)machine_descrip.va, md_size);
123 #endif	/* MACH_DESC_DEBUG */
124 
125 		machine_descrip.size = md_size;
126 
127 			/*
128 			 * Allocate the kstat to get at the data
129 			 */
130 		machine_descrip.ksp = kstat_create("unix", 0, "machdesc",
131 		    "misc",
132 		    KSTAT_TYPE_RAW,
133 		    (uint_t)machine_descrip.size,
134 		    KSTAT_FLAG_VIRTUAL);
135 
136 		if (machine_descrip.ksp == NULL) {
137 			cmn_err(CE_PANIC,
138 			    "Failed to create kstat for machine description");
139 		} else {
140 			machine_descrip.ksp->ks_data = machine_descrip.va;
141 			kstat_install(machine_descrip.ksp);
142 		}
143 	}
144 }
145