aboutsummaryrefslogtreecommitdiffstats
path: root/xen/common/device_tree.c
blob: 5301d63d9f0aeba6ca697bb1f6422f657023b904 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * Device Tree
 *
 * Copyright (C) 2012 Citrix Systems, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <xen/config.h>
#include <xen/types.h>
#include <xen/init.h>
#include <xen/device_tree.h>
#include <xen/kernel.h>
#include <xen/lib.h>
#include <xen/libfdt/libfdt.h>
#include <xen/mm.h>
#include <xen/stdarg.h>
#include <xen/string.h>
#include <xen/cpumask.h>
#include <asm/early_printk.h>

struct dt_early_info __initdata early_info;
void *device_tree_flattened;

/* Some device tree functions may be called both before and after the
   console is initialized. */
static void (*dt_printk)(const char *fmt, ...) = early_printk;

bool_t device_tree_node_matches(const void *fdt, int node, const char *match)
{
    const char *name;
    size_t match_len;

    name = fdt_get_name(fdt, node, NULL);
    match_len = strlen(match);

    /* Match both "match" and "match@..." patterns but not
       "match-foo". */
    return strncmp(name, match, match_len) == 0
        && (name[match_len] == '@' || name[match_len] == '\0');
}

bool_t device_tree_type_matches(const void *fdt, int node, const char *match)
{
    const void *prop;

    prop = fdt_getprop(fdt, node, "device_type", NULL);
    if ( prop == NULL )
        return 0;

    return !strcmp(prop, match);
}

bool_t device_tree_node_compatible(const void *fdt, int node, const char *match)
{
    int len, l;
    const void *prop;

    prop = fdt_getprop(fdt, node, "compatible", &len);
    if ( prop == NULL )
        return 0;

    while ( len > 0 ) {
        if ( !strcmp(prop, match) )
            return 1;
        l = strlen(prop) + 1;
        prop += l;
        len -= l;
    }

    return 0;
}

static int device_tree_nr_reg_ranges(const struct fdt_property *prop,
        u32 address_cells, u32 size_cells)
{
    u32 reg_cells = address_cells + size_cells;
    return fdt32_to_cpu(prop->len) / (reg_cells * sizeof(u32));
}

static void __init get_val(const u32 **cell, u32 cells, u64 *val)
{
    *val = 0;

    if ( cells > 2 )
        early_panic("dtb value contains > 2 cells\n");

    while ( cells-- )
    {
        *val <<= 32;
        *val |= fdt32_to_cpu(*(*cell)++);
    }
}

void device_tree_get_reg(const u32 **cell, u32 address_cells, u32 size_cells,
                         u64 *start, u64 *size)
{
    get_val(cell, address_cells, start);
    get_val(cell, size_cells, size);
}

static void set_val(u32 **cell, u32 cells, u64 val)
{
    u32 c = cells;

    while ( c-- )
    {
        (*cell)[c] = cpu_to_fdt32(val);
        val >>= 32;
    }
    (*cell) += cells;
}

void device_tree_set_reg(u32 **cell, u32 address_cells, u32 size_cells,
                         u64 start, u64 size)
{
    set_val(cell, address_cells, start);
    set_val(cell, size_cells, size);
}

u32 device_tree_get_u32(const void *fdt, int node, const char *prop_name)
{
    const struct fdt_property *prop;

    prop = fdt_get_property(fdt, node, prop_name, NULL);
    if ( !prop || prop->len < sizeof(u32) )
        return 0; /* default to 0 */

    return fdt32_to_cpu(*(uint32_t*)prop->data);
}

/**
 * device_tree_for_each_node - iterate over all device tree nodes
 * @fdt: flat device tree.
 * @func: function to call for each node.
 * @data: data to pass to @func.
 *
 * Any nodes nested at DEVICE_TREE_MAX_DEPTH or deeper are ignored.
 *
 * Returns 0 if all nodes were iterated over successfully.  If @func
 * returns a value different from 0, that value is returned immediately.
 */
int device_tree_for_each_node(const void *fdt,
                              device_tree_node_func func, void *data)
{
    int node;
    int depth;
    u32 address_cells[DEVICE_TREE_MAX_DEPTH];
    u32 size_cells[DEVICE_TREE_MAX_DEPTH];
    int ret;

    for ( node = 0, depth = 0;
          node >=0 && depth >= 0;
          node = fdt_next_node(fdt, node, &depth) )
    {
        const char *name = fdt_get_name(fdt, node, NULL);

        if ( depth >= DEVICE_TREE_MAX_DEPTH )
        {
            dt_printk("Warning: device tree node `%s' is nested too deep\n",
                      name);
            continue;
        }

        address_cells[depth] = device_tree_get_u32(fdt, node, "#address-cells");
        size_cells[depth] = device_tree_get_u32(fdt, node, "#size-cells");

        ret = func(fdt, node, name, depth,
                   address_cells[depth-1], size_cells[depth-1], data);
        if ( ret != 0 )
            return ret;
    }
    return 0;
}

struct find_compat {
    const char *compatible;
    int found;
    int node;
    int depth;
    u32 address_cells;
    u32 size_cells;
};

static int _find_compatible_node(const void *fdt,
                             int node, const char *name, int depth,
                             u32 address_cells, u32 size_cells,
                             void *data)
{
    struct find_compat *c = (struct find_compat *) data;

    if (  c->found  )
        return 1;

    if ( device_tree_node_compatible(fdt, node, c->compatible) )
    {
        c->found = 1;
        c->node = node;
        c->depth = depth;
        c->address_cells = address_cells;
        c->size_cells = size_cells;
        return 1;
    }
    return 0;
}

int find_compatible_node(const char *compatible, int *node, int *depth,
                u32 *address_cells, u32 *size_cells)
{
    int ret;
    struct find_compat c;
    c.compatible = compatible;
    c.found = 0;

    ret = device_tree_for_each_node(device_tree_flattened, _find_compatible_node, &c);
    if ( !c.found )
        return ret;
    else
    {
        *node = c.node;
        *depth = c.depth;
        *address_cells = c.address_cells;
        *size_cells = c.size_cells;
        return 1;
    }
}

/**
 * device_tree_bootargs - return the bootargs (the Xen command line)
 * @fdt flat device tree.
 */
const char *device_tree_bootargs(const void *fdt)
{
    int node; 
    const struct fdt_property *prop;

    node = fdt_path_offset(fdt, "/chosen");
    if ( node < 0 )
        return NULL;

    prop = fdt_get_property(fdt, node, "bootargs", NULL);
    if ( prop == NULL )
        return NULL;

    return prop->data;
}

static int dump_node(const void *fdt, int node, const char *name, int depth,
                     u32 address_cells, u32 size_cells, void *data)
{
    char prefix[2*DEVICE_TREE_MAX_DEPTH + 1] = "";
    int i;
    int prop;

    for ( i = 0; i < depth; i++ )
        safe_strcat(prefix, "  ");

    if ( name[0] == '\0' )
        name = "/";
    printk("%s%s:\n", prefix, name);

    for ( prop = fdt_first_property_offset(fdt, node);
          prop >= 0;
          prop = fdt_next_property_offset(fdt, prop) )
    {
        const struct fdt_property *p;

        p = fdt_get_property_by_offset(fdt, prop, NULL);

        printk("%s  %s\n", prefix, fdt_string(fdt, fdt32_to_cpu(p->nameoff)));
    }

    return 0;
}

/**
 * device_tree_dump - print a text representation of a device tree
 * @fdt: flat device tree to print
 */
void device_tree_dump(const void *fdt)
{
    device_tree_for_each_node(fdt, dump_node, NULL);
}


static void __init process_memory_node(const void *fdt, int node,
                                       const char *name,
                                       u32 address_cells, u32 size_cells)
{
    const struct fdt_property *prop;
    int i;
    int banks;
    const u32 *cell;
    paddr_t start, size;

    if ( address_cells < 1 || size_cells < 1 )
    {
        early_printk("fdt: node `%s': invalid #address-cells or #size-cells",
                     name);
        return;
    }

    prop = fdt_get_property(fdt, node, "reg", NULL);
    if ( !prop )
    {
        early_printk("fdt: node `%s': missing `reg' property\n", name);
        return;
    }

    cell = (const u32 *)prop->data;
    banks = device_tree_nr_reg_ranges(prop, address_cells, size_cells);

    for ( i = 0; i < banks && early_info.mem.nr_banks < NR_MEM_BANKS; i++ )
    {
        device_tree_get_reg(&cell, address_cells, size_cells, &start, &size);
        early_info.mem.bank[early_info.mem.nr_banks].start = start;
        early_info.mem.bank[early_info.mem.nr_banks].size = size;
        early_info.mem.nr_banks++;
    }
}

static void __init process_cpu_node(const void *fdt, int node,
                                    const char *name,
                                    u32 address_cells, u32 size_cells)
{
    const struct fdt_property *prop;
    const u32 *cell;
    paddr_t start, size;

    if ( address_cells != 1 || size_cells != 0 )
    {
        early_printk("fdt: node `%s': invalid #address-cells or #size-cells",
                     name);
        return;
    }

    prop = fdt_get_property(fdt, node, "reg", NULL);
    if ( !prop )
    {
        early_printk("fdt: node `%s': missing `reg' property\n", name);
        return;
    }

    cell = (const u32 *)prop->data;
    device_tree_get_reg(&cell, address_cells, size_cells, &start, &size);

    cpumask_set_cpu(start, &cpu_possible_map);
}

static void __init process_gic_node(const void *fdt, int node,
                                    const char *name,
                                    u32 address_cells, u32 size_cells)
{
    const struct fdt_property *prop;
    const u32 *cell;
    paddr_t start, size;
    int interfaces;

    if ( address_cells < 1 || size_cells < 1 )
    {
        early_printk("fdt: node `%s': invalid #address-cells or #size-cells",
                     name);
        return;
    }

    prop = fdt_get_property(fdt, node, "reg", NULL);
    if ( !prop )
    {
        early_printk("fdt: node `%s': missing `reg' property\n", name);
        return;
    }

    cell = (const u32 *)prop->data;
    interfaces = device_tree_nr_reg_ranges(prop, address_cells, size_cells);
    if ( interfaces < 4 )
    {
        early_printk("fdt: node `%s': not enough ranges\n", name);
        return;
    }
    device_tree_get_reg(&cell, address_cells, size_cells, &start, &size);
    early_info.gic.gic_dist_addr = start;
    device_tree_get_reg(&cell, address_cells, size_cells, &start, &size);
    early_info.gic.gic_cpu_addr = start;
    device_tree_get_reg(&cell, address_cells, size_cells, &start, &size);
    early_info.gic.gic_hyp_addr = start;
    device_tree_get_reg(&cell, address_cells, size_cells, &start, &size);
    early_info.gic.gic_vcpu_addr = start;
}

static int __init early_scan_node(const void *fdt,
                                  int node, const char *name, int depth,
                                  u32 address_cells, u32 size_cells,
                                  void *data)
{
    if ( device_tree_node_matches(fdt, node, "memory") )
        process_memory_node(fdt, node, name, address_cells, size_cells);
    else if ( device_tree_type_matches(fdt, node, "cpu") )
        process_cpu_node(fdt, node, name, address_cells, size_cells);
    else if ( device_tree_node_compatible(fdt, node, "arm,cortex-a15-gic") )
        process_gic_node(fdt, node, name, address_cells, size_cells);

    return 0;
}

static void __init early_print_info(void)
{
    struct dt_mem_info *mi = &early_info.mem;
    int i;

    for ( i = 0; i < mi->nr_banks; i++ )
        early_printk("RAM: %016llx - %016llx\n",
                     mi->bank[i].start,
                     mi->bank[i].start + mi->bank[i].size - 1);
}

/**
 * device_tree_early_init - initialize early info from a DTB
 * @fdt: flattened device tree binary
 *
 * Returns the size of the DTB.
 */
size_t __init device_tree_early_init(const void *fdt)
{
    int ret;

    ret = fdt_check_header(fdt);
    if ( ret < 0 )
        early_panic("No valid device tree\n");

    device_tree_for_each_node((void *)fdt, early_scan_node, NULL);
    early_print_info();

    dt_printk = printk;

    return fdt_totalsize(fdt);
}

/*
 * Local variables:
 * mode: C
 * c-set-style: "BSD"
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */