1 | /*
|
---|
2 | * Copyright (c) 2009 Pavel Rimsky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup sparc64
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <debug.h>
|
---|
36 | #include <panic.h>
|
---|
37 | #include <func.h>
|
---|
38 | #include <print.h>
|
---|
39 | #include <string.h>
|
---|
40 | #include <arch/sun4v/md.h>
|
---|
41 | #include <arch/sun4v/hypercall.h>
|
---|
42 | #include <arch/mm/page.h>
|
---|
43 |
|
---|
44 | /* maximum MD size estimate (in bytes) */
|
---|
45 | #define MD_MAX_SIZE (64 * 1024)
|
---|
46 |
|
---|
47 | /** element types (element tag values) */
|
---|
48 | #define LIST_END 0x0 /**< End of element list */
|
---|
49 | #define NODE 0x4e /**< Start of node definition */
|
---|
50 | #define NODE_END 0x45 /**< End of node definition */
|
---|
51 | #define NOOP 0x20 /**< NOOP list element - to be ignored */
|
---|
52 | #define PROP_ARC 0x61 /**< Node property arc'ing to another node */
|
---|
53 | #define PROP_VAL 0x76 /**< Node property with an integer value */
|
---|
54 | #define PROP_STR 0x73 /**< Node property with a string value */
|
---|
55 | #define PROP_DATA 0x64 /**< Node property with a block of data */
|
---|
56 |
|
---|
57 |
|
---|
58 | /** machine description header */
|
---|
59 | typedef struct {
|
---|
60 | uint32_t transport_version; /**< Transport version number */
|
---|
61 | uint32_t node_blk_sz; /**< Size in bytes of node block */
|
---|
62 | uint32_t name_blk_sz; /**< Size in bytes of name block */
|
---|
63 | uint32_t data_blk_sz; /**< Size in bytes of data block */
|
---|
64 | } __attribute__ ((packed)) md_header_t;
|
---|
65 |
|
---|
66 | /** machine description element (in the node block) */
|
---|
67 | typedef struct {
|
---|
68 | uint8_t tag; /**< Type of element */
|
---|
69 | uint8_t name_len; /**< Length in bytes of element name */
|
---|
70 | uint16_t _reserved_field; /**< reserved field (zeros) */
|
---|
71 | uint32_t name_offset; /**< Location offset of name associated
|
---|
72 | with this element relative to
|
---|
73 | start of name block */
|
---|
74 | union {
|
---|
75 | /** for elements of type “PROP_STR” and of type “PROP_DATA” */
|
---|
76 | struct {
|
---|
77 | /** Length in bytes of data in data block */
|
---|
78 | uint32_t data_len;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Location offset of data associated with this
|
---|
82 | * element relative to start of data block
|
---|
83 | */
|
---|
84 | uint32_t data_offset;
|
---|
85 | } y;
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * 64 bit value for elements of tag type “NODE”, “PROP_VAL”
|
---|
89 | * or “PROP_ARC”
|
---|
90 | */
|
---|
91 | uint64_t val;
|
---|
92 | } d;
|
---|
93 | } __attribute__ ((packed)) md_element_t;
|
---|
94 |
|
---|
95 | /** index of the element within the node block */
|
---|
96 | typedef unsigned int element_idx_t;
|
---|
97 |
|
---|
98 | /** buffer to which the machine description will be saved */
|
---|
99 | static uint8_t mach_desc[MD_MAX_SIZE]
|
---|
100 | __attribute__ ((aligned (16)));
|
---|
101 |
|
---|
102 |
|
---|
103 | /** returns pointer to the element at the given index */
|
---|
104 | static md_element_t *get_element(element_idx_t idx)
|
---|
105 | {
|
---|
106 | return (md_element_t *) (mach_desc +
|
---|
107 | sizeof(md_header_t) + idx * sizeof(md_element_t));
|
---|
108 | }
|
---|
109 |
|
---|
110 | /** returns the name of the element represented by the index */
|
---|
111 | static const char *get_element_name(element_idx_t idx)
|
---|
112 | {
|
---|
113 | md_header_t *md_header = (md_header_t *) mach_desc;
|
---|
114 | uintptr_t name_offset = get_element(idx)->name_offset;
|
---|
115 | return (char *) mach_desc + sizeof(md_header_t) +
|
---|
116 | md_header->node_blk_sz + name_offset;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** finds the name of the node represented by "node" */
|
---|
120 | const char *md_get_node_name(md_node_t node)
|
---|
121 | {
|
---|
122 | return get_element_name(node);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Returns the value of the integer property of the given node.
|
---|
127 | *
|
---|
128 | * @param
|
---|
129 | */
|
---|
130 | bool md_get_integer_property(md_node_t node, const char *key,
|
---|
131 | uint64_t *result)
|
---|
132 | {
|
---|
133 | element_idx_t idx = node;
|
---|
134 |
|
---|
135 | while (get_element(idx)->tag != NODE_END) {
|
---|
136 | idx++;
|
---|
137 | md_element_t *element = get_element(idx);
|
---|
138 | if (element->tag == PROP_VAL &&
|
---|
139 | str_cmp(key, get_element_name(idx)) == 0) {
|
---|
140 | *result = element->d.val;
|
---|
141 | return true;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | return false;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Returns the value of the string property of the given node.
|
---|
150 | *
|
---|
151 | * @param
|
---|
152 | */
|
---|
153 | bool md_get_string_property(md_node_t node, const char *key,
|
---|
154 | const char **result)
|
---|
155 | {
|
---|
156 | md_header_t *md_header = (md_header_t *) mach_desc;
|
---|
157 | element_idx_t idx = node;
|
---|
158 |
|
---|
159 | while (get_element(idx)->tag != NODE_END) {
|
---|
160 | idx++;
|
---|
161 | md_element_t *element = get_element(idx);
|
---|
162 | if (element->tag == PROP_DATA &&
|
---|
163 | str_cmp(key, get_element_name(idx)) == 0) {
|
---|
164 | *result = (char *) mach_desc + sizeof(md_header_t) +
|
---|
165 | md_header->node_blk_sz + md_header->name_blk_sz +
|
---|
166 | element->d.y.data_offset;
|
---|
167 | return true;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | return false;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Moves the child oterator to the next child (following sibling of the node
|
---|
176 | * the oterator currently points to).
|
---|
177 | *
|
---|
178 | * @param it pointer to the iterator to be moved
|
---|
179 | */
|
---|
180 | bool md_next_child(md_child_iter_t *it)
|
---|
181 | {
|
---|
182 | element_idx_t backup = *it;
|
---|
183 |
|
---|
184 | while (get_element(*it)->tag != NODE_END) {
|
---|
185 | (*it)++;
|
---|
186 | md_element_t *element = get_element(*it);
|
---|
187 | if (element->tag == PROP_ARC &&
|
---|
188 | str_cmp("fwd", get_element_name(*it)) == 0) {
|
---|
189 | return true;
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | *it = backup;
|
---|
194 | return false;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Returns the node the iterator point to.
|
---|
199 | */
|
---|
200 | md_node_t md_get_child_node(md_child_iter_t it)
|
---|
201 | {
|
---|
202 | return get_element(it)->d.val;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Helper function used to split a string to a part before the first
|
---|
207 | * slash sign and a part after the slash sign.
|
---|
208 | *
|
---|
209 | * @param str pointer to the string to be split; when the function finishes,
|
---|
210 | * it will contain only the part following the first slash sign of
|
---|
211 | * the original string
|
---|
212 | * @param head pointer to the string which will be set to the part before the
|
---|
213 | * first slash sign
|
---|
214 | */
|
---|
215 | static bool str_parse_head(char **str, char **head)
|
---|
216 | {
|
---|
217 | *head = *str;
|
---|
218 |
|
---|
219 | char *cur = *str;
|
---|
220 | while (*cur != '\0') {
|
---|
221 | if (*cur == '/') {
|
---|
222 | *cur = '\0';
|
---|
223 | *str = cur + 1;
|
---|
224 | return true;
|
---|
225 | }
|
---|
226 | cur++;
|
---|
227 | }
|
---|
228 |
|
---|
229 | return false;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Returns the descendant of the given node. The descendant is identified
|
---|
234 | * by a path where the node names are separated by a slash.
|
---|
235 | *
|
---|
236 | * Ex.: Let there be a node N with path "a/b/c/x/y/z" and let P represent the
|
---|
237 | * node with path "a/b/c". Then md_get_child(P, "x/y/z") will return N.
|
---|
238 | */
|
---|
239 | md_node_t md_get_child(md_node_t node, char *name)
|
---|
240 | {
|
---|
241 | bool more;
|
---|
242 |
|
---|
243 | do {
|
---|
244 | char *head;
|
---|
245 | more = str_parse_head(&name, &head);
|
---|
246 |
|
---|
247 | while (md_next_child(&node)) {
|
---|
248 | element_idx_t child = md_get_child_node(node);
|
---|
249 | if (str_cmp(head, get_element_name(child)) == 0) {
|
---|
250 | node = child;
|
---|
251 | break;
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | } while (more);
|
---|
256 |
|
---|
257 | return node;
|
---|
258 | }
|
---|
259 |
|
---|
260 | /** returns the root node of MD */
|
---|
261 | md_node_t md_get_root(void)
|
---|
262 | {
|
---|
263 | return 0;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Returns the child iterator - a token to be passed to functions iterating
|
---|
268 | * through all the children of a node.
|
---|
269 | *
|
---|
270 | * @param node a node whose children the iterator will be used
|
---|
271 | * to iterate through
|
---|
272 | */
|
---|
273 | md_child_iter_t md_get_child_iterator(md_node_t node)
|
---|
274 | {
|
---|
275 | return node;
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Moves "node" to the node following "node" in the list of all the existing
|
---|
280 | * nodes of the MD whose name is "name".
|
---|
281 | */
|
---|
282 | bool md_next_node(md_node_t *node, const char *name)
|
---|
283 | {
|
---|
284 | md_element_t *element;
|
---|
285 | (*node)++;
|
---|
286 |
|
---|
287 | do {
|
---|
288 | element = get_element(*node);
|
---|
289 |
|
---|
290 | if (element->tag == NODE &&
|
---|
291 | str_cmp(name, get_element_name(*node)) == 0) {
|
---|
292 | return true;
|
---|
293 | }
|
---|
294 |
|
---|
295 | (*node)++;
|
---|
296 | } while (element->tag != LIST_END);
|
---|
297 |
|
---|
298 | return false;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Retrieves the machine description from the hypervisor and saves it to
|
---|
303 | * a kernel buffer.
|
---|
304 | */
|
---|
305 | void md_init(void)
|
---|
306 | {
|
---|
307 | uint64_t retval = __hypercall_fast2(MACH_DESC, KA2PA(mach_desc),
|
---|
308 | MD_MAX_SIZE);
|
---|
309 |
|
---|
310 | retval = retval;
|
---|
311 | if (retval != HV_EOK) {
|
---|
312 | printf("Could not retrieve machine description, error = %d.\n",
|
---|
313 | retval);
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | /** @}
|
---|
318 | */
|
---|