Subversion Repositories HelenOS

Rev

Rev 3607 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
885 decky 1
/*
2071 jermar 2
 * Copyright (c) 2005 Martin Decky
885 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
 
1789 jermar 29
#include <ofw.h>
30
#include <ofwarch.h>
1764 jermar 31
#include <printf.h>
32
#include <asm.h>
1782 jermar 33
#include <types.h>
885 decky 34
 
1783 jermar 35
uintptr_t ofw_cif;
964 decky 36
 
885 decky 37
phandle ofw_chosen;
1146 decky 38
ihandle ofw_stdout;
39
phandle ofw_root;
40
ihandle ofw_mmu;
2250 jermar 41
ihandle ofw_memory_prop;
1146 decky 42
phandle ofw_memory;
1130 decky 43
phandle ofw_aliases;
885 decky 44
 
1789 jermar 45
void ofw_init(void)
46
{
47
    ofw_chosen = ofw_find_device("/chosen");
48
    if (ofw_chosen == -1)
49
        halt();
50
 
2250 jermar 51
    if (ofw_get_property(ofw_chosen, "stdout", &ofw_stdout, sizeof(ofw_stdout)) <= 0)
1789 jermar 52
        ofw_stdout = 0;
53
 
54
    ofw_root = ofw_find_device("/");
55
    if (ofw_root == -1) {
56
        puts("\r\nError: Unable to find / device, halted.\r\n");
57
        halt();
58
    }
59
 
2250 jermar 60
    if (ofw_get_property(ofw_chosen, "mmu", &ofw_mmu, sizeof(ofw_mmu)) <= 0) {
1789 jermar 61
        puts("\r\nError: Unable to get mmu property, halted.\r\n");
62
        halt();
63
    }
2250 jermar 64
    if (ofw_get_property(ofw_chosen, "memory", &ofw_memory_prop, sizeof(ofw_memory_prop)) <= 0) {
65
        puts("\r\nError: Unable to get memory property, halted.\r\n");
66
        halt();
67
    }
1789 jermar 68
 
69
    ofw_memory = ofw_find_device("/memory");
70
    if (ofw_memory == -1) {
71
        puts("\r\nError: Unable to find /memory device, halted.\r\n");
72
        halt();
73
    }
74
 
75
    ofw_aliases = ofw_find_device("/aliases");
76
    if (ofw_aliases == -1) {
77
        puts("\r\nError: Unable to find /aliases device, halted.\r\n");
78
        halt();
79
    }
80
}
81
 
1790 jermar 82
/** Perform a call to OpenFirmware client interface.
83
 *
84
 * @param service String identifying the service requested.
85
 * @param nargs Number of input arguments.
86
 * @param nret Number of output arguments. This includes the return value.
87
 * @param rets Buffer for output arguments or NULL. The buffer must accommodate nret - 1 items.
88
 *
89
 * @return Return value returned by the client interface.
90
 */
1899 jermar 91
unsigned long ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets, ...)
885 decky 92
{
93
    va_list list;
94
    ofw_args_t args;
95
    int i;
96
 
1790 jermar 97
    args.service = (ofw_arg_t) service;
885 decky 98
    args.nargs = nargs;
99
    args.nret = nret;
100
 
1146 decky 101
    va_start(list, rets);
885 decky 102
    for (i = 0; i < nargs; i++)
103
        args.args[i] = va_arg(list, ofw_arg_t);
104
    va_end(list);
105
 
106
    for (i = 0; i < nret; i++)
107
        args.args[i + nargs] = 0;
108
 
1783 jermar 109
    (void) ofw(&args);
110
 
1146 decky 111
    for (i = 1; i < nret; i++)
112
        rets[i - 1] = args.args[i + nargs];
1783 jermar 113
 
956 decky 114
    return args.args[nargs];
885 decky 115
}
116
 
1771 jermar 117
phandle ofw_find_device(const char *name)
953 decky 118
{
1146 decky 119
    return ofw_call("finddevice", 1, 1, NULL, name);
964 decky 120
}
121
 
1895 jermar 122
int ofw_get_property(const phandle device, const char *name, void *buf, const int buflen)
964 decky 123
{
1146 decky 124
    return ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen);
964 decky 125
}
126
 
1893 jermar 127
int ofw_get_proplen(const phandle device, const char *name)
128
{
129
    return ofw_call("getproplen", 2, 1, NULL, device, name);
130
}
1146 decky 131
 
1893 jermar 132
int ofw_next_property(const phandle device, char *previous, char *buf)
133
{
134
    return ofw_call("nextprop", 3, 1, NULL, device, previous, buf);
135
}
136
 
1895 jermar 137
int ofw_package_to_path(const phandle device, char *buf, const int buflen)
138
{
139
    return ofw_call("package-to-path", 3, 1, NULL, device, buf, buflen);
140
}
141
 
1789 jermar 142
unsigned int ofw_get_address_cells(const phandle device)
1146 decky 143
{
1789 jermar 144
    unsigned int ret = 1;
1146 decky 145
 
146
    if (ofw_get_property(device, "#address-cells", &ret, sizeof(ret)) <= 0)
147
        if (ofw_get_property(ofw_root, "#address-cells", &ret, sizeof(ret)) <= 0)
1789 jermar 148
            ret = OFW_ADDRESS_CELLS;
1146 decky 149
 
150
    return ret;
151
}
152
 
153
 
1789 jermar 154
unsigned int ofw_get_size_cells(const phandle device)
1146 decky 155
{
156
    unsigned int ret;
157
 
158
    if (ofw_get_property(device, "#size-cells", &ret, sizeof(ret)) <= 0)
159
        if (ofw_get_property(ofw_root, "#size-cells", &ret, sizeof(ret)) <= 0)
1789 jermar 160
            ret = OFW_SIZE_CELLS;
1146 decky 161
 
162
    return ret;
163
}
164
 
1881 jermar 165
phandle ofw_get_child_node(const phandle node)
166
{
167
    return ofw_call("child", 1, 1, NULL, node);
168
}
1146 decky 169
 
1881 jermar 170
phandle ofw_get_peer_node(const phandle node)
171
{
172
    return ofw_call("peer", 1, 1, NULL, node);
173
}
174
 
964 decky 175
static ihandle ofw_open(const char *name)
176
{
1146 decky 177
    return ofw_call("open", 1, 1, NULL, name);
953 decky 178
}
179
 
180
 
964 decky 181
void ofw_write(const char *str, const int len)
885 decky 182
{
964 decky 183
    if (ofw_stdout == 0)
184
        return;
185
 
1146 decky 186
    ofw_call("write", 3, 1, NULL, ofw_stdout, str, len);
885 decky 187
}
188
 
189
 
956 decky 190
void *ofw_translate(const void *virt)
885 decky 191
{
1782 jermar 192
    ofw_arg_t result[4];
193
    int shift;
194
 
195
    if (ofw_call("call-method", 3, 5, result, "translate", ofw_mmu, virt) != 0) {
1146 decky 196
        puts("Error: MMU method translate() failed, halting.\n");
197
        halt();
198
    }
1782 jermar 199
 
1783 jermar 200
    if (ofw_translate_failed(result[0]))
201
        return NULL;
202
 
1782 jermar 203
    if (sizeof(unative_t) == 8)
204
        shift = 32;
205
    else
206
        shift = 0;
1789 jermar 207
 
2250 jermar 208
    return (void *) ((result[2] << shift) | result[3]);
885 decky 209
}
953 decky 210
 
2250 jermar 211
void *ofw_claim_virt(const void *virt, const int len)
1783 jermar 212
{
213
    ofw_arg_t retaddr;
953 decky 214
 
1783 jermar 215
    if (ofw_call("call-method", 5, 2, &retaddr, "claim", ofw_mmu, 0, len, virt) != 0) {
216
        puts("Error: MMU method claim() failed, halting.\n");
217
        halt();
218
    }
219
 
220
    return (void *) retaddr;
221
}
222
 
2250 jermar 223
void *ofw_claim_phys(const void *phys, const int len)
224
{
225
    ofw_arg_t retaddr[2];
226
    int shift;
227
 
228
    if (sizeof(unative_t) == 8) {
229
        shift = 32;
230
        if (ofw_call("call-method", 6, 3, retaddr, "claim",
231
            ofw_memory_prop, 0, len, ((uintptr_t) phys) >> shift,
232
            ((uintptr_t) phys) & ((uint32_t) -1)) != 0) {
233
            /*
234
             * Note that this will help us to discover
235
             * conflicts between OpenFirmware allocations
236
             * and our use of physical memory.
237
             * It is better to detect collisions here
238
             * than to cope with weird errors later.
239
             *
240
             * So this is really not to make the loader
241
             * more generic; it is here for debugging
242
             * purposes.
243
             */
244
            puts("Error: memory method claim() failed, halting.\n");
245
            halt();
246
        }
247
    } else {
248
        shift = 0;
249
        /*
250
         * FIXME: the number of arguments is probably different...
251
         */
252
        puts("Error: 32-bit ofw_claim_phys not implemented.\n");
253
        halt();
254
    }
255
 
256
    return (void *) ((retaddr[0] << shift) | retaddr[1]);
257
}
258
 
1075 decky 259
int ofw_map(const void *phys, const void *virt, const int size, const int mode)
260
{
1782 jermar 261
    uintptr_t phys_hi, phys_lo;
262
 
263
    if (sizeof(unative_t) == 8) {
264
        int shift = 32;
265
        phys_hi = (uintptr_t) phys >> shift;
266
        phys_lo = (uintptr_t) phys & 0xffffffff;
267
    } else {
268
        phys_hi = 0;
269
        phys_lo = (uintptr_t) phys;
270
    }
271
 
272
    return ofw_call("call-method", 7, 1, NULL, "map", ofw_mmu, mode, size, virt,
2250 jermar 273
        phys_hi, phys_lo);
1075 decky 274
}
275
 
2048 jermar 276
/** Save OpenFirmware physical memory map.
277
 *
278
 * @param map Memory map structure where the map will be saved.
279
 *
280
 * @return Zero on failure, non-zero on success.
281
 */
964 decky 282
int ofw_memmap(memmap_t *map)
283
{
3607 rimsky 284
    unsigned int ac = ofw_get_address_cells(ofw_memory) /
285
        (sizeof(uintptr_t) / sizeof(uint32_t));
286
    unsigned int sc = ofw_get_size_cells(ofw_memory) /
287
        (sizeof(uintptr_t) / sizeof(uint32_t));
288
    printf("address cells: %d, size cells: %d. ", ac, sc);
1789 jermar 289
 
3607 rimsky 290
    uintptr_t buf[((ac + sc) * MEMMAP_MAX_RECORDS)];
1771 jermar 291
    int ret = ofw_get_property(ofw_memory, "reg", buf, sizeof(buf));
1789 jermar 292
    if (ret <= 0)       /* ret is the number of written bytes */
964 decky 293
        return false;
1789 jermar 294
 
1146 decky 295
    int pos;
964 decky 296
    map->total = 0;
297
    map->count = 0;
3607 rimsky 298
    for (pos = 0; (pos < ret / sizeof(uintptr_t)) &&
2250 jermar 299
        (map->count < MEMMAP_MAX_RECORDS); pos += ac + sc) {
3607 rimsky 300
        void *start = (void *) (buf[pos + ac - 1]);
1146 decky 301
        unsigned int size = buf[pos + ac + sc - 1];
3607 rimsky 302
 
303
        /*
3618 rimsky 304
         * This is a hot fix of the issue which occurs on machines
305
         * where there are holes in the physical memory (such as
306
         * SunBlade 1500). Should we detect a hole in the physical
307
         * memory, we will ignore any memory detected behind
3607 rimsky 308
         * the hole and pretend the hole does not exist.
309
         */
310
        if ((map->count > 0) &&
311
            (map->zones[map->count - 1].start + map->zones[map->count - 1].size
312
                < start))
313
            break;
314
 
1146 decky 315
        if (size > 0) {
316
            map->zones[map->count].start = start;
317
            map->zones[map->count].size = size;
318
            map->count++;
319
            map->total += size;
320
        }
964 decky 321
    }
1789 jermar 322
 
323
    return true;
964 decky 324
}
1130 decky 325
 
326
int ofw_screen(screen_t *screen)
327
{
1146 decky 328
    char device_name[BUF_SIZE];
1789 jermar 329
    uint32_t virtaddr;
1130 decky 330
 
1771 jermar 331
    if (ofw_get_property(ofw_aliases, "screen", device_name, sizeof(device_name)) <= 0)
1130 decky 332
        return false;
333
 
334
    phandle device = ofw_find_device(device_name);
335
    if (device == -1)
336
        return false;
337
 
1789 jermar 338
    if (ofw_get_property(device, "address", &virtaddr, sizeof(virtaddr)) <= 0)
1130 decky 339
        return false;
1789 jermar 340
 
341
    screen->addr = (void *) ((uintptr_t) virtaddr);
342
 
1130 decky 343
    if (ofw_get_property(device, "width", &screen->width, sizeof(screen->width)) <= 0)
344
        return false;
345
 
346
    if (ofw_get_property(device, "height", &screen->height, sizeof(screen->height)) <= 0)
347
        return false;
348
 
349
    if (ofw_get_property(device, "depth", &screen->bpp, sizeof(screen->bpp)) <= 0)
350
        return false;
351
 
352
    if (ofw_get_property(device, "linebytes", &screen->scanline, sizeof(screen->scanline)) <= 0)
353
        return false;
354
 
355
    return true;
356
}
1947 decky 357
 
358
 
359
void ofw_quiesce(void)
360
{
1977 jermar 361
    ofw_call("quiesce", 0, 0, NULL);
1947 decky 362
}