aboutsummaryrefslogtreecommitdiff
path: root/lib/fdtwalk/fdtwalk.c
blob: 2ca506af693aea3ab266e9022fe621e207994f5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright (c) 2020 Travis Geiselbrecht
 *
 * Use of this source code is governed by a MIT-style
 * license that can be found in the LICENSE file or at
 * https://opensource.org/licenses/MIT
 */
#include <lib/fdtwalk.h>

#include <assert.h>
#include <libfdt.h>
#include <stdio.h>
#include <lk/err.h>
#include <lk/trace.h>
#include <sys/types.h>

#define LOCAL_TRACE 0
#define MAX_DEPTH 16

/* read the #address-cells and #size-cells properties at the current node to
 * see if there are any overriding sizes at this level. It's okay to not
 * find the properties.
 */
static void read_address_size_cells(const void *fdt, int offset, int depth,
                                    uint32_t *address_cells, uint32_t *size_cells) {
    LTRACEF_LEVEL(3, "fdt %p, offset %d depth %d\n", fdt, offset, depth);

    DEBUG_ASSERT(depth >= 0 && depth < MAX_DEPTH);

    int len;
    const void *prop_ptr = fdt_getprop(fdt, offset, "#address-cells", &len);
    LTRACEF_LEVEL(3, "%p, len %d\n", prop_ptr, len);
    if (prop_ptr && len == 4) {
        address_cells[depth] = fdt32_to_cpu(*(const uint32_t *)prop_ptr);
    }

    prop_ptr = fdt_getprop(fdt, offset, "#size-cells", &len);
    LTRACEF_LEVEL(3, "%p, len %d\n", prop_ptr, len);
    if (prop_ptr && len == 4) {
        size_cells[depth] = fdt32_to_cpu(*(const uint32_t *)prop_ptr);
    }

    LTRACEF_LEVEL(3, "address-cells %u size-cells %u\n", address_cells[depth], size_cells[depth]);
}

static status_t read_base_len_pair(const uint8_t *prop_ptr, size_t prop_len,
                                   size_t address_cell_size, size_t size_cell_size,
                                   uint64_t *base, uint64_t *len) {
    *base = 0;
    *len = 0;

    /* we're looking at a memory descriptor */
    if (address_cell_size == 2 && prop_len >= 8) {
        *base = fdt64_to_cpu(*(const uint64_t *)prop_ptr);
        prop_ptr += 8;
        prop_len -= 8;
    } else if (address_cell_size == 1 && prop_len >= 4) {
        *base = fdt32_to_cpu(*(const uint32_t *)prop_ptr);
        prop_ptr += 4;
        prop_len -= 4;
    } else {
        return ERR_NOT_IMPLEMENTED;
    }

    if (size_cell_size == 2 && prop_len >= 8) {
        *len = fdt64_to_cpu(*((const uint64_t *)prop_ptr));
        prop_ptr += 8;
        prop_len -= 8;
    } else if (size_cell_size == 1 && prop_len >= 4) {
        *len = fdt32_to_cpu(*(const uint32_t *)prop_ptr);
        prop_ptr += 4;
        prop_len -= 4;
    } else {
        return ERR_NOT_IMPLEMENTED;
    }

    return NO_ERROR;
}

status_t fdt_walk(const void *fdt, const struct fdt_walk_callbacks *cb) {
    int err = fdt_check_header(fdt);
    if (err != 0) {
        return ERR_NOT_FOUND;
    }

    /* walk the nodes */
    int depth = 0;
    int offset = 0;
    uint32_t address_cells[MAX_DEPTH];
    uint32_t size_cells[MAX_DEPTH];

    /* if >= 0, we're inside /reserved-memory */
    int reserved_memory_depth = -1;

    /* read the address/size cells properties at the root, if present */
    address_cells[0] = 2;
    size_cells[0] = 1;
    read_address_size_cells(fdt, offset, 0, address_cells, size_cells);

    for (;;) {
        offset = fdt_next_node(fdt, offset, &depth);
        if (offset < 0 || depth < 0) {
            break;
        }

        LTRACEF_LEVEL(3, "fdt_next node offset %d, depth %d\n", offset, depth);

        if (depth >= MAX_DEPTH) {
            printf("FDTWALK: exceeded max depth %d\n", MAX_DEPTH);
            return ERR_NO_MEMORY;
        }

        /* copy the address/size cells from the parent depth and then see if we
         * have local properties to override it. */
        if (depth > 0) {
            address_cells[depth] = address_cells[depth - 1];
            size_cells[depth] = size_cells[depth - 1];
        }
        read_address_size_cells(fdt, offset, depth, address_cells, size_cells);

        /* get the name */
        const char *name = fdt_get_name(fdt, offset, NULL);
        if (!name)
            continue;

        LTRACEF_LEVEL(2, "name '%s', depth %d, address cells %u, size cells %u\n",
                      name, depth, address_cells[depth], size_cells[depth]);

        /* look for the 'memory@*' property */
        if (cb->mem && strncmp(name, "memory@", 7) == 0 && depth == 1) {
            int lenp;
            const uint8_t *prop_ptr = fdt_getprop(fdt, offset, "reg", &lenp);
            if (prop_ptr) {
                LTRACEF_LEVEL(2, "found '%s' reg prop len %d, ac %u, sc %u\n", name, lenp,
                              address_cells[depth], size_cells[depth]);
                /* we're looking at a memory descriptor */
                uint64_t base;
                uint64_t len;
                err = read_base_len_pair(prop_ptr, lenp, address_cells[depth], size_cells[depth], &base, &len);
                if (err != NO_ERROR) {
                    TRACEF("error reading base/length from memory@ node\n");
                    /* continue on */
                } else {
                    LTRACEF("calling mem callback with base %#llx len %#llx\n", base, len);
                    cb->mem(base, len, cb->memcookie);
                }
            }
        }

        /* look for the 'reserved-memory' tree */
        if (cb->reserved_memory) {
            /* once we see the reserved-memory first level node, track that we are inside
             * it until we step out to a node at the same or higher depth.
             */
            if (strncmp(name, "reserved-memory", 15) == 0 && depth == 1) {
                LTRACEF_LEVEL(2, "found reserved memory node\n");

                reserved_memory_depth = depth;
            } else if (reserved_memory_depth >= 0) {
                if (depth <= reserved_memory_depth) {
                    /* we have exited the reserved memory tree, so clear our tracking depth */
                    LTRACEF_LEVEL(2, "exiting reserved memory node\n");
                    reserved_memory_depth = -1;
                } else {
                    /* if we're inside the reserved meory tree, so this node must
                     * be a reserved memory region */
                    int lenp;
                    const uint8_t *prop_ptr = fdt_getprop(fdt, offset, "reg", &lenp);
                    if (prop_ptr) {
                        LTRACEF_LEVEL(2, "found '%s' reg prop len %d, ac %u, sc %u\n", name, lenp,
                                      address_cells[depth], size_cells[depth]);
                        /* we're looking at a memory descriptor */
                        uint64_t base;
                        uint64_t len;
                        err = read_base_len_pair(prop_ptr, lenp, address_cells[depth], size_cells[depth], &base, &len);
                        if (err != NO_ERROR) {
                            TRACEF("error reading base/length from reserved-memory node\n");
                            /* continue on */
                        } else {
                            LTRACEF("calling reserved memory callback with base %#llx len %#llx\n", base, len);
                            cb->reserved_memory(base, len, cb->reserved_memory_cookie);
                        }
                    }
                }
            }
        }

        /* look for a cpu leaf and count the number of cpus */
        if (cb->cpu && strncmp(name, "cpu@", 4) == 0 && depth == 2) {
            int lenp;
            const uint8_t *prop_ptr = fdt_getprop(fdt, offset, "reg", &lenp);
            LTRACEF("%p, lenp %u\n", prop_ptr, lenp);
            if (prop_ptr) {
                LTRACEF_LEVEL(2, "found '%s' reg prop len %d, ac %u, sc %u\n", name, lenp,
                              address_cells[depth], size_cells[depth]);
                uint32_t id = 0;
                if (address_cells[depth] == 1 && lenp >= 4) {
                    id = fdt32_to_cpu(*(const uint32_t *)prop_ptr);
                    prop_ptr += 4;
                    lenp -= 4;
                } else {
                    PANIC_UNIMPLEMENTED;
                }

                LTRACEF("calling cpu callback with id %#x\n", id);
                cb->cpu(id, cb->cpucookie);
            }
        }

        /* look for a pcie leaf and pass the address of the ecam and other info to the callback */
        if (cb->pcie && (strncmp(name, "pcie@", 5) == 0 || strncmp(name, "pci@", 4) == 0)) {
            struct fdt_walk_pcie_info info = {0};

            /* find the range of the ecam */
            int lenp;
            const uint8_t *prop_ptr = fdt_getprop(fdt, offset, "reg", &lenp);
            LTRACEF("%p, lenp %u\n", prop_ptr, lenp);
            if (prop_ptr) {
                LTRACEF_LEVEL(2, "found '%s' prop 'reg' len %d, ac %u, sc %u\n", name, lenp,
                              address_cells[depth], size_cells[depth]);

                /* seems to always be full address cells 2, size cells 2, despite it being 3/2 */
                info.ecam_base = fdt64_to_cpu(*(const uint64_t *)prop_ptr);
                prop_ptr += 8;
                info.ecam_len = fdt64_to_cpu(*(const uint64_t *)prop_ptr);
            }

            /* find which bus range the ecam covers */
            prop_ptr = fdt_getprop(fdt, offset, "bus-range", &lenp);
            LTRACEF("%p, lenp %u\n", prop_ptr, lenp);
            if (prop_ptr) {
                LTRACEF_LEVEL(2, "found '%s' prop 'bus-range' len %d, ac %u, sc %u\n", name, lenp,
                              address_cells[depth], size_cells[depth]);

                if (lenp == 8) {
                    info.bus_start = fdt32_to_cpu(*(const uint32_t *)prop_ptr);
                    prop_ptr += 4;
                    info.bus_end = fdt32_to_cpu(*(const uint32_t *)prop_ptr);
                }
            }

            prop_ptr = fdt_getprop(fdt, offset, "ranges", &lenp);
            LTRACEF("%p, lenp %u\n", prop_ptr, lenp);
            if (prop_ptr) {
                LTRACEF_LEVEL(2, "found '%s' prop 'ranges' len %d, ac %u, sc %u\n", name, lenp,
                              address_cells[depth], size_cells[depth]);

                /* iterate this packed property */
                const uint8_t *prop_end = prop_ptr + lenp;
                while (prop_ptr < prop_end) {
                    uint32_t type = fdt32_to_cpu(*(const uint32_t *)(prop_ptr));
                    prop_ptr += 4;

                    /* read 3 64bit values */
                    uint64_t base1, base2, size;
                    base1 = fdt64_to_cpu(*(const uint64_t *)prop_ptr);
                    prop_ptr += 8;
                    base2 = fdt64_to_cpu(*(const uint64_t *)prop_ptr);
                    prop_ptr += 8;
                    size = fdt64_to_cpu(*(const uint64_t *)prop_ptr);
                    prop_ptr += 8;

                    switch (type) {
                        case 0x1000000: // io range
                            LTRACEF_LEVEL(2, "io range\n");
                            info.io_base = base1;
                            info.io_base_mmio = base2;
                            info.io_len = size;
                            break;
                        case 0x2000000: // mmio range
                            LTRACEF_LEVEL(2, "mmio range\n");
                            info.mmio_base = base1;
                            info.mmio_len = size;
                            break;
                        case 0x3000000: // mmio range (64bit)
                            LTRACEF_LEVEL(2, "mmio range (64bit)\n");
                            info.mmio64_base = base1;
                            info.mmio64_len = size;
                            break;
                        default:
                            LTRACEF_LEVEL(2, "unhandled type %#x\n", type);
                    }

                    LTRACEF_LEVEL(2, "base %#llx base2 %#llx size %#llx\n", base1, base2, size);
                }
            }

            if (info.ecam_len > 0) {
                LTRACEF("calling pci callback with ecam base %#llx size %#llx\n", info.ecam_base, info.ecam_len);
                cb->pcie(&info, cb->pciecookie);
            }
        }

    }

    return NO_ERROR;
}