1 | /*
|
---|
2 | * Copyright (c) 2005 Martin Decky
|
---|
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 | #include <ofw.h>
|
---|
30 | #include <ofwarch.h>
|
---|
31 | #include <printf.h>
|
---|
32 | #include <asm.h>
|
---|
33 | #include <types.h>
|
---|
34 | #include <string.h>
|
---|
35 |
|
---|
36 | #define RED(i) (((i) >> 5) & ((1 << 3) - 1))
|
---|
37 | #define GREEN(i) (((i) >> 3) & ((1 << 2) - 1))
|
---|
38 | #define BLUE(i) ((i) & ((1 << 3) - 1))
|
---|
39 | #define CLIP(i) ((i) <= 255 ? (i) : 255)
|
---|
40 |
|
---|
41 | uintptr_t ofw_cif;
|
---|
42 |
|
---|
43 | phandle ofw_chosen;
|
---|
44 | ihandle ofw_stdout;
|
---|
45 | phandle ofw_root;
|
---|
46 | ihandle ofw_mmu;
|
---|
47 | ihandle ofw_memory_prop;
|
---|
48 | phandle ofw_memory;
|
---|
49 |
|
---|
50 | void ofw_init(void)
|
---|
51 | {
|
---|
52 | ofw_chosen = ofw_find_device("/chosen");
|
---|
53 | if (ofw_chosen == -1)
|
---|
54 | halt();
|
---|
55 |
|
---|
56 | if (ofw_get_property(ofw_chosen, "stdout", &ofw_stdout,
|
---|
57 | sizeof(ofw_stdout)) <= 0)
|
---|
58 | ofw_stdout = 0;
|
---|
59 |
|
---|
60 | ofw_root = ofw_find_device("/");
|
---|
61 | if (ofw_root == -1) {
|
---|
62 | puts("\r\nError: Unable to find / device, halted.\r\n");
|
---|
63 | halt();
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (ofw_get_property(ofw_chosen, "mmu", &ofw_mmu,
|
---|
67 | sizeof(ofw_mmu)) <= 0) {
|
---|
68 | puts("\r\nError: Unable to get mmu property, halted.\r\n");
|
---|
69 | halt();
|
---|
70 | }
|
---|
71 | if (ofw_get_property(ofw_chosen, "memory", &ofw_memory_prop,
|
---|
72 | sizeof(ofw_memory_prop)) <= 0) {
|
---|
73 | puts("\r\nError: Unable to get memory property, halted.\r\n");
|
---|
74 | halt();
|
---|
75 | }
|
---|
76 |
|
---|
77 | ofw_memory = ofw_find_device("/memory");
|
---|
78 | if (ofw_memory == -1) {
|
---|
79 | puts("\r\nError: Unable to find /memory device, halted.\r\n");
|
---|
80 | halt();
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Perform a call to OpenFirmware client interface.
|
---|
85 | *
|
---|
86 | * @param service String identifying the service requested.
|
---|
87 | * @param nargs Number of input arguments.
|
---|
88 | * @param nret Number of output arguments. This includes the return
|
---|
89 | * value.
|
---|
90 | * @param rets Buffer for output arguments or NULL. The buffer must
|
---|
91 | * accommodate nret - 1 items.
|
---|
92 | *
|
---|
93 | * @return Return value returned by the client interface.
|
---|
94 | *
|
---|
95 | */
|
---|
96 | unsigned long
|
---|
97 | ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets,
|
---|
98 | ...)
|
---|
99 | {
|
---|
100 | va_list list;
|
---|
101 | ofw_args_t args;
|
---|
102 | int i;
|
---|
103 |
|
---|
104 | args.service = (ofw_arg_t) service;
|
---|
105 | args.nargs = nargs;
|
---|
106 | args.nret = nret;
|
---|
107 |
|
---|
108 | va_start(list, rets);
|
---|
109 | for (i = 0; i < nargs; i++)
|
---|
110 | args.args[i] = va_arg(list, ofw_arg_t);
|
---|
111 | va_end(list);
|
---|
112 |
|
---|
113 | for (i = 0; i < nret; i++)
|
---|
114 | args.args[i + nargs] = 0;
|
---|
115 |
|
---|
116 | (void) ofw(&args);
|
---|
117 |
|
---|
118 | for (i = 1; i < nret; i++)
|
---|
119 | rets[i - 1] = args.args[i + nargs];
|
---|
120 |
|
---|
121 | return args.args[nargs];
|
---|
122 | }
|
---|
123 |
|
---|
124 | phandle ofw_find_device(const char *name)
|
---|
125 | {
|
---|
126 | return ofw_call("finddevice", 1, 1, NULL, name);
|
---|
127 | }
|
---|
128 |
|
---|
129 | int ofw_get_property(const phandle device, const char *name, void *buf,
|
---|
130 | const int buflen)
|
---|
131 | {
|
---|
132 | return ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen);
|
---|
133 | }
|
---|
134 |
|
---|
135 | int ofw_get_proplen(const phandle device, const char *name)
|
---|
136 | {
|
---|
137 | return ofw_call("getproplen", 2, 1, NULL, device, name);
|
---|
138 | }
|
---|
139 |
|
---|
140 | int ofw_next_property(const phandle device, char *previous, char *buf)
|
---|
141 | {
|
---|
142 | return ofw_call("nextprop", 3, 1, NULL, device, previous, buf);
|
---|
143 | }
|
---|
144 |
|
---|
145 | int ofw_package_to_path(const phandle device, char *buf, const int buflen)
|
---|
146 | {
|
---|
147 | return ofw_call("package-to-path", 3, 1, NULL, device, buf, buflen);
|
---|
148 | }
|
---|
149 |
|
---|
150 | unsigned int ofw_get_address_cells(const phandle device)
|
---|
151 | {
|
---|
152 | unsigned int ret = 1;
|
---|
153 |
|
---|
154 | if (ofw_get_property(device, "#address-cells", &ret, sizeof(ret)) <= 0)
|
---|
155 | if (ofw_get_property(ofw_root, "#address-cells", &ret,
|
---|
156 | sizeof(ret)) <= 0)
|
---|
157 | ret = OFW_ADDRESS_CELLS;
|
---|
158 |
|
---|
159 | return ret;
|
---|
160 | }
|
---|
161 |
|
---|
162 | unsigned int ofw_get_size_cells(const phandle device)
|
---|
163 | {
|
---|
164 | unsigned int ret;
|
---|
165 |
|
---|
166 | if (ofw_get_property(device, "#size-cells", &ret, sizeof(ret)) <= 0)
|
---|
167 | if (ofw_get_property(ofw_root, "#size-cells", &ret,
|
---|
168 | sizeof(ret)) <= 0)
|
---|
169 | ret = OFW_SIZE_CELLS;
|
---|
170 |
|
---|
171 | return ret;
|
---|
172 | }
|
---|
173 |
|
---|
174 | phandle ofw_get_child_node(const phandle node)
|
---|
175 | {
|
---|
176 | return ofw_call("child", 1, 1, NULL, node);
|
---|
177 | }
|
---|
178 |
|
---|
179 | phandle ofw_get_peer_node(const phandle node)
|
---|
180 | {
|
---|
181 | return ofw_call("peer", 1, 1, NULL, node);
|
---|
182 | }
|
---|
183 |
|
---|
184 | static ihandle ofw_open(const char *name)
|
---|
185 | {
|
---|
186 | return ofw_call("open", 1, 1, NULL, name);
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | void ofw_write(const char *str, const int len)
|
---|
191 | {
|
---|
192 | if (ofw_stdout == 0)
|
---|
193 | return;
|
---|
194 |
|
---|
195 | ofw_call("write", 3, 1, NULL, ofw_stdout, str, len);
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 | void *ofw_translate(const void *virt)
|
---|
200 | {
|
---|
201 | ofw_arg_t result[4];
|
---|
202 | int shift;
|
---|
203 |
|
---|
204 | if (ofw_call("call-method", 4, 5, result, "translate", ofw_mmu,
|
---|
205 | virt, 0) != 0) {
|
---|
206 | puts("Error: MMU method translate() failed, halting.\n");
|
---|
207 | halt();
|
---|
208 | }
|
---|
209 |
|
---|
210 | if (ofw_translate_failed(result[0]))
|
---|
211 | return NULL;
|
---|
212 |
|
---|
213 | if (sizeof(unative_t) == 8)
|
---|
214 | shift = 32;
|
---|
215 | else
|
---|
216 | shift = 0;
|
---|
217 |
|
---|
218 | return (void *) ((result[2] << shift) | result[3]);
|
---|
219 | }
|
---|
220 |
|
---|
221 | void *ofw_claim_virt(const void *virt, const unsigned int len)
|
---|
222 | {
|
---|
223 | ofw_arg_t retaddr;
|
---|
224 |
|
---|
225 | if (ofw_call("call-method", 5, 2, &retaddr, "claim", ofw_mmu, 0, len,
|
---|
226 | virt) != 0) {
|
---|
227 | puts("Error: MMU method claim() failed, halting.\n");
|
---|
228 | halt();
|
---|
229 | }
|
---|
230 |
|
---|
231 | return (void *) retaddr;
|
---|
232 | }
|
---|
233 |
|
---|
234 | static void *ofw_claim_phys_internal(const void *phys, const unsigned int len, const unsigned int alignment)
|
---|
235 | {
|
---|
236 | /*
|
---|
237 | * Note that the return value check will help
|
---|
238 | * us to discover conflicts between OpenFirmware
|
---|
239 | * allocations and our use of physical memory.
|
---|
240 | * It is better to detect collisions here
|
---|
241 | * than to cope with weird errors later.
|
---|
242 | *
|
---|
243 | * So this is really not to make the loader
|
---|
244 | * more generic; it is here for debugging
|
---|
245 | * purposes.
|
---|
246 | */
|
---|
247 |
|
---|
248 | if (sizeof(unative_t) == 8) {
|
---|
249 | ofw_arg_t retaddr[2];
|
---|
250 | int shift = 32;
|
---|
251 |
|
---|
252 | if (ofw_call("call-method", 6, 3, retaddr, "claim",
|
---|
253 | ofw_memory_prop, alignment, len, ((uintptr_t) phys) >> shift,
|
---|
254 | ((uintptr_t) phys) & ((uint32_t) -1)) != 0) {
|
---|
255 | puts("Error: memory method claim() failed, halting.\n");
|
---|
256 | halt();
|
---|
257 | }
|
---|
258 |
|
---|
259 | return (void *) ((retaddr[0] << shift) | retaddr[1]);
|
---|
260 | } else {
|
---|
261 | ofw_arg_t retaddr[1];
|
---|
262 |
|
---|
263 | if (ofw_call("call-method", 5, 2, retaddr, "claim",
|
---|
264 | ofw_memory_prop, alignment, len, (uintptr_t) phys) != 0) {
|
---|
265 | puts("Error: memory method claim() failed, halting.\n");
|
---|
266 | halt();
|
---|
267 | }
|
---|
268 |
|
---|
269 | return (void *) retaddr[0];
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | void *ofw_claim_phys(const void *phys, const unsigned int len)
|
---|
274 | {
|
---|
275 | return ofw_claim_phys_internal(phys, len, 0);
|
---|
276 | }
|
---|
277 |
|
---|
278 | void *ofw_claim_phys_any(const unsigned int len, const unsigned int alignment)
|
---|
279 | {
|
---|
280 | return ofw_claim_phys_internal(NULL, len, alignment);
|
---|
281 | }
|
---|
282 |
|
---|
283 | int ofw_map(const void *phys, const void *virt, const unsigned int size, const int mode)
|
---|
284 | {
|
---|
285 | uintptr_t phys_hi, phys_lo;
|
---|
286 |
|
---|
287 | if (sizeof(unative_t) == 8) {
|
---|
288 | int shift = 32;
|
---|
289 | phys_hi = (uintptr_t) phys >> shift;
|
---|
290 | phys_lo = (uintptr_t) phys & 0xffffffff;
|
---|
291 | } else {
|
---|
292 | phys_hi = 0;
|
---|
293 | phys_lo = (uintptr_t) phys;
|
---|
294 | }
|
---|
295 |
|
---|
296 | return ofw_call("call-method", 7, 1, NULL, "map", ofw_mmu, mode, size,
|
---|
297 | virt, phys_hi, phys_lo);
|
---|
298 | }
|
---|
299 |
|
---|
300 | /** Save OpenFirmware physical memory map.
|
---|
301 | *
|
---|
302 | * @param map Memory map structure where the map will be saved.
|
---|
303 | *
|
---|
304 | * @return Zero on failure, non-zero on success.
|
---|
305 | */
|
---|
306 | int ofw_memmap(memmap_t *map)
|
---|
307 | {
|
---|
308 | unsigned int ac = ofw_get_address_cells(ofw_memory) /
|
---|
309 | (sizeof(uintptr_t) / sizeof(uint32_t));
|
---|
310 | unsigned int sc = ofw_get_size_cells(ofw_memory) /
|
---|
311 | (sizeof(uintptr_t) / sizeof(uint32_t));
|
---|
312 |
|
---|
313 | uintptr_t buf[((ac + sc) * MEMMAP_MAX_RECORDS)];
|
---|
314 | int ret = ofw_get_property(ofw_memory, "reg", buf, sizeof(buf));
|
---|
315 | if (ret <= 0) /* ret is the number of written bytes */
|
---|
316 | return false;
|
---|
317 |
|
---|
318 | int pos;
|
---|
319 | map->total = 0;
|
---|
320 | map->count = 0;
|
---|
321 | for (pos = 0; (pos < ret / sizeof(uintptr_t)) &&
|
---|
322 | (map->count < MEMMAP_MAX_RECORDS); pos += ac + sc) {
|
---|
323 | void *start = (void *) (buf[pos + ac - 1]);
|
---|
324 | unsigned int size = buf[pos + ac + sc - 1];
|
---|
325 |
|
---|
326 | /*
|
---|
327 | * This is a hot fix of the issue which occurs on machines
|
---|
328 | * where there are holes in the physical memory (such as
|
---|
329 | * SunBlade 1500). Should we detect a hole in the physical
|
---|
330 | * memory, we will ignore any memory detected behind
|
---|
331 | * the hole and pretend the hole does not exist.
|
---|
332 | */
|
---|
333 | if ((map->count > 0) && (map->zones[map->count - 1].start +
|
---|
334 | map->zones[map->count - 1].size < start))
|
---|
335 | break;
|
---|
336 |
|
---|
337 | if (size > 0) {
|
---|
338 | map->zones[map->count].start = start;
|
---|
339 | map->zones[map->count].size = size;
|
---|
340 | map->count++;
|
---|
341 | map->total += size;
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | return true;
|
---|
346 | }
|
---|
347 |
|
---|
348 | static void ofw_setup_screen(phandle handle)
|
---|
349 | {
|
---|
350 | /* Check for device type */
|
---|
351 | char device_type[OFW_TREE_PROPERTY_MAX_VALUELEN];
|
---|
352 | if (ofw_get_property(handle, "device_type", device_type, OFW_TREE_PROPERTY_MAX_VALUELEN) <= 0)
|
---|
353 | return;
|
---|
354 |
|
---|
355 | device_type[OFW_TREE_PROPERTY_MAX_VALUELEN - 1] = '\0';
|
---|
356 | if (strcmp(device_type, "display") != 0)
|
---|
357 | return;
|
---|
358 |
|
---|
359 | /* Check for 8 bit depth */
|
---|
360 | uint32_t depth;
|
---|
361 | if (ofw_get_property(handle, "depth", &depth, sizeof(uint32_t)) <= 0)
|
---|
362 | depth = 0;
|
---|
363 |
|
---|
364 | /* Get device path */
|
---|
365 | static char path[OFW_TREE_PATH_MAX_LEN + 1];
|
---|
366 | size_t len = ofw_package_to_path(handle, path, OFW_TREE_PATH_MAX_LEN);
|
---|
367 | if (len == -1)
|
---|
368 | return;
|
---|
369 |
|
---|
370 | path[len] = '\0';
|
---|
371 |
|
---|
372 | /* Open the display to initialize it */
|
---|
373 | ihandle screen = ofw_open(path);
|
---|
374 | if (screen == -1)
|
---|
375 | return;
|
---|
376 |
|
---|
377 | if (depth == 8) {
|
---|
378 | /* Setup the palette so that the (inverted) 3:2:3 scheme is usable */
|
---|
379 | unsigned int i;
|
---|
380 | for (i = 0; i < 256; i++) {
|
---|
381 | ofw_call("call-method", 6, 1, NULL, "color!", screen,
|
---|
382 | 255 - i, CLIP(BLUE(i) * 37), GREEN(i) * 85, CLIP(RED(i) * 37));
|
---|
383 | }
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | static void ofw_setup_screens_internal(phandle current)
|
---|
388 | {
|
---|
389 | while ((current != 0) && (current != -1)) {
|
---|
390 | ofw_setup_screen(current);
|
---|
391 |
|
---|
392 | /*
|
---|
393 | * Recursively process the potential child node.
|
---|
394 | */
|
---|
395 | phandle child = ofw_get_child_node(current);
|
---|
396 | if ((child != 0) && (child != -1))
|
---|
397 | ofw_setup_screens_internal(child);
|
---|
398 |
|
---|
399 | /*
|
---|
400 | * Iteratively process the next peer node.
|
---|
401 | * Note that recursion is a bad idea here.
|
---|
402 | * Due to the topology of the OpenFirmware device tree,
|
---|
403 | * the nesting of peer nodes could be to wide and the
|
---|
404 | * risk of overflowing the stack is too real.
|
---|
405 | */
|
---|
406 | phandle peer = ofw_get_peer_node(current);
|
---|
407 | if ((peer != 0) && (peer != -1)) {
|
---|
408 | current = peer;
|
---|
409 | /*
|
---|
410 | * Process the peer in next iteration.
|
---|
411 | */
|
---|
412 | continue;
|
---|
413 | }
|
---|
414 |
|
---|
415 | /*
|
---|
416 | * No more peers on this level.
|
---|
417 | */
|
---|
418 | break;
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 | /** Setup all screens which can be detected.
|
---|
423 | *
|
---|
424 | * Open all screens which can be detected and set up the palette for the 8-bit
|
---|
425 | * color depth configuration so that the 3:2:3 color scheme can be used.
|
---|
426 | * Check that setting the palette makes sense (the color depth is not greater
|
---|
427 | * than 8).
|
---|
428 | *
|
---|
429 | */
|
---|
430 | void ofw_setup_screens(void)
|
---|
431 | {
|
---|
432 | ofw_setup_screens_internal(ofw_root);
|
---|
433 | }
|
---|
434 |
|
---|
435 | void ofw_quiesce(void)
|
---|
436 | {
|
---|
437 | ofw_call("quiesce", 0, 0, NULL);
|
---|
438 | }
|
---|