1 #include <lunaix/mm/valloc.h>
2 #include <lunaix/syslog.h>
4 #include <klibc/string.h>
10 static struct dt_context dtctx;
13 fdt_itbegin(struct fdt_iter* fdti, struct fdt_header* fdt_hdr)
15 unsigned int off_struct, off_str;
16 struct fdt_token* tok;
19 off_str = le(fdt_hdr->off_dt_strings);
20 off_struct = le(fdt_hdr->off_dt_struct);
22 tok = offset_t(fdt_hdr, struct fdt_token, off_struct);
23 str_blk = offset_t(fdt_hdr, const char, off_str);
25 *fdti = (struct fdt_iter) {
32 fdt_itend(struct fdt_iter* fdti)
38 fdt_itnext(struct fdt_iter* fdti)
40 struct fdt_token *current;
41 struct fdt_prop *prop;
50 if (fdt_nope(current)) {
54 if (fdt_prop(current)) {
55 prop = (struct fdt_prop*) current;
56 current = offset(current, prop->len);
60 if (fdt_node_end(current)) {
68 if (fdti->depth == 1) {
73 while (!fdt_prop(current) && !fdt_node_end(current)) {
77 if (fdt_prop(current)) {
83 } while (fdt_nope(current) && fdti->depth > 0);
85 return fdti->depth > 0;
89 fdt_itnext_at(struct fdt_iter* fdti, int level)
91 while (fdti->depth != level && fdt_itnext(fdti));
93 return fdti->depth == level;
97 fdt_memrsvd_itbegin(struct fdt_memrsvd_iter* rsvdi,
98 struct fdt_header* fdt_hdr)
100 size_t off = le(fdt_hdr->off_mem_rsvmap);
103 offset_t(fdt_hdr, typeof(*rsvdi->block), off);
105 rsvdi->block = &rsvdi->block[-1];
109 fdt_memrsvd_itnext(struct fdt_memrsvd_iter* rsvdi)
111 struct fdt_memrsvd_ent* ent;
120 return ent->addr || ent->size;
124 fdt_memrsvd_itend(struct fdt_memrsvd_iter* rsvdi)
130 propeq(struct fdt_iter* it, const char* key)
132 return streq(fdtit_prop_key(it), key);
136 __mkprop_val32(struct fdt_iter* it, struct dt_prop_val* val)
138 val->u32_val = le(*(u32_t*)&it->prop[1]);
139 val->size = le(it->prop->len);
143 __mkprop_val64(struct fdt_iter* it, struct dt_prop_val* val)
145 val->u64_val = le64(*(u64_t*)&it->prop[1]);
146 val->size = le(it->prop->len);
150 __mkprop_ptr(struct fdt_iter* it, struct dt_prop_val* val)
152 val->ptr_val = __ptr(&it->prop[1]);
153 val->size = le(it->prop->len);
157 __prop_getu32(struct fdt_iter* it)
159 return le(*(u32_t*)&it->prop[1]);
163 __parse_stdbase_prop(struct fdt_iter* it, struct dt_node_base* node)
165 struct fdt_prop* prop;
169 if (propeq(it, "compatible")) {
170 __mkprop_ptr(it, &node->compat);
173 else if (propeq(it, "model")) {
174 node->model = (const char*)&prop[1];
177 else if (propeq(it, "phandle")) {
178 node->phandle = __prop_getu32(it);
179 hashtable_hash_in(dtctx.phnds_table,
180 &node->phnd_link, node->phandle);
183 else if (propeq(it, "#address-cells")) {
184 node->addr_c = (char)__prop_getu32(it);
187 else if (propeq(it, "#size-cells")) {
188 node->sz_c = (char)__prop_getu32(it);
191 else if (propeq(it, "#interrupt-cells")) {
192 node->intr_c = (char)__prop_getu32(it);
195 else if (propeq(it, "status")) {
196 char peek = *(char*)&it->prop[1];
198 node->status = STATUS_OK;
200 else if (peek == 'r') {
201 node->status = STATUS_RSVD;
203 else if (peek == 'd') {
204 node->status = STATUS_DISABLE;
206 else if (peek == 'f') {
207 node->status = STATUS_FAIL;
219 __parse_stdnode_prop(struct fdt_iter* it, struct dt_node* node)
221 if (propeq(it, "reg")) {
222 __mkprop_ptr(it, &node->reg);
225 else if (propeq(it, "virtual-reg")) {
226 __mkprop_ptr(it, &node->vreg);
229 else if (propeq(it, "ranges")) {
230 __mkprop_ptr(it, &node->ranges);
233 else if (propeq(it, "dma-ranges")) {
234 __mkprop_ptr(it, &node->dma_ranges);
245 __parse_stdflags(struct fdt_iter* it, struct dt_node_base* node)
247 if (propeq(it, "dma-coherent")) {
248 node->dma_coherent = true;
251 else if (propeq(it, "dma-noncoherent")) {
252 node->dma_ncoherent = true;
255 else if (propeq(it, "interrupt-controller")) {
256 node->intr_controll = true;
267 __parse_other_prop(struct fdt_iter* it, struct dt_node_base* node)
269 struct dt_prop* prop;
273 prop = valloc(sizeof(*prop));
274 key = fdtit_prop_key(it);
276 prop->key = HSTR(key, strlen(key));
277 __mkprop_ptr(it, &prop->val);
279 hstr_rehash(&prop->key, HSTR_FULL_HASH);
280 hash = prop->key.hash;
282 hashtable_hash_in(node->_op_bucket, &prop->ht, hash);
286 __fill_node(struct fdt_iter* it, struct dt_node* node)
288 if (__parse_stdflags(it, &node->base)) {
292 if (__parse_stdbase_prop(it, &node->base)) {
296 if (__parse_stdnode_prop(it, node)) {
300 if (__parse_stdintr_prop(it, &node->intr)) {
304 __parse_other_prop(it, &node->base);
308 __fill_root(struct fdt_iter* it, struct dt_root* node)
310 if (__parse_stdflags(it, &node->base)) {
314 if (__parse_stdbase_prop(it, &node->base)) {
318 struct fdt_prop* prop;
321 if (propeq(it, "serial-number")) {
322 node->serial = (const char*)&prop[1];
325 else if (propeq(it, "chassis-type")) {
326 node->chassis = (const char*)&prop[1];
329 __parse_other_prop(it, &node->base);
333 __init_node(struct dt_node_base* node)
335 hashtable_init(node->_op_bucket);
336 llist_init_head(&node->children);
339 node->_std = node->parent->_std;
343 __init_node_regular(struct dt_node* node)
345 __init_node(&node->base);
346 node->intr.parent_hnd = PHND_NULL;
350 __expand_extended_intr(struct dt_intr_node* intrupt)
352 struct dt_prop_iter it;
353 struct dt_prop_val arr;
354 struct dt_node *node;
355 struct dt_node *master;
356 struct dt_intr_prop* intr_prop;
358 if (!intrupt->intr.extended) {
362 arr = intrupt->intr.arr;
363 node = INTR_TO_DTNODE(intrupt);
365 llist_init_head(&intrupt->intr.values);
367 dt_decode(&it, &node->base, &arr, 1);
371 phnd = dtprop_to_u32(it.prop_loc);
372 master = dt_resolve_phandle(phnd);
375 WARN("dtb: (intr_extended) malformed phandle: %d", phnd);
379 intr_prop = valloc(sizeof(*intr_prop));
381 intr_prop->master = &master->intr;
382 intr_prop->val = (struct dt_prop_val) {
383 .encoded = it.prop_loc_next,
384 .size = master->base.intr_c
387 llist_append(&intrupt->intr.values, &intr_prop->props);
388 dtprop_next_n(&it, intr_prop->val.size);
390 } while(dtprop_next(&it));
394 __resolve_phnd_references()
396 struct dt_node_base *pos, *n;
397 struct dt_node *node, *parent, *default_parent;
398 struct dt_intr_node* intrupt;
401 llist_for_each(pos, n, &dtctx.nodes, nodes)
403 node = BASE_TO_DTNODE(pos);
404 intrupt = &node->intr;
406 if (!node->base.intr_c) {
410 phnd = intrupt->parent_hnd;
411 default_parent = (struct dt_node*)node->base.parent;
412 parent = default_parent;
414 if (phnd != PHND_NULL) {
415 parent = dt_resolve_phandle(phnd);
419 WARN("dtb: (phnd_resolve) malformed phandle: %d", phnd);
420 parent = default_parent;
423 intrupt->parent = &parent->intr;
425 __expand_extended_intr(intrupt);
430 __resolve_inter_map()
432 struct dt_node_base *pos, *n;
434 llist_for_each(pos, n, &dtctx.nodes, nodes)
436 dt_resolve_interrupt_map(BASE_TO_DTNODE(pos));
441 dt_load(ptr_t dtb_dropoff)
443 dtctx.reloacted_dtb = dtb_dropoff;
445 if (dtctx.fdt->magic != FDT_MAGIC) {
446 ERROR("invalid dtb, unexpected magic: 0x%x", dtctx.fdt->magic);
450 size_t str_off = le(dtctx.fdt->size_dt_strings);
451 dtctx.str_block = offset_t(dtb_dropoff, const char, str_off);
453 llist_init_head(&dtctx.nodes);
454 hashtable_init(dtctx.phnds_table);
457 struct fdt_token* tok;
458 struct dt_node_base *node, *prev;
460 struct dt_node_base* depth[16];
461 bool is_root_level, filled;
465 fdt_itbegin(&it, dtctx.fdt);
467 while (fdt_itnext(&it)) {
468 is_root_level = it.depth == 1;
470 if (it.depth >= 16) {
472 ERROR("strange dtb, too deep to dive.");
476 depth[it.depth] = NULL;
477 node = depth[it.depth - 1];
481 if (unlikely(is_root_level)) {
482 node = vzalloc(sizeof(struct dt_root));
486 node = vzalloc(sizeof(struct dt_node));
487 prev = depth[it.depth - 2];
490 __init_node_regular((struct dt_node*)node);
491 llist_append(&prev->children, &node->siblings);
493 llist_append(&dtctx.nodes, &node->nodes);
496 node->name = (const char*)&it.pos[1];
499 if (unlikely(is_root_level)) {
500 __fill_root(&it, (struct dt_root*)node);
503 __fill_node(&it, (struct dt_node*)node);
509 dtctx.root = (struct dt_root*)depth[0];
511 __resolve_phnd_references();
512 __resolve_inter_map();
514 INFO("device tree loaded");
520 dt_resolve_phandle(dt_phnd_t phandle)
522 struct dt_node_base *pos, *n;
523 hashtable_hash_foreach(dtctx.phnds_table, phandle, pos, n, phnd_link)
525 if (pos->phandle == phandle) {
526 return (struct dt_node*)pos;
534 __byname_predicate(struct dt_node_iter* iter, struct dt_node_base* node)
537 const char* be_matched = node->name;
538 const char* name = (const char*)iter->closure;
540 while (be_matched[i] && name[i])
542 if (be_matched[i] != name[i]) {
553 dt_begin_find_byname(struct dt_node_iter* iter,
554 struct dt_node* node, const char* name)
556 dt_begin_find(iter, node, __byname_predicate, name);
560 dt_begin_find(struct dt_node_iter* iter, struct dt_node* node,
561 node_predicate_t pred, void* closure)
563 node = node ? : (struct dt_node*)dtctx.root;
565 iter->head = &node->base;
566 iter->matched = NULL;
567 iter->closure = closure;
570 struct dt_node_base *pos, *n;
571 llist_for_each(pos, n, &node->base.children, siblings)
573 if (pred(iter, pos)) {
581 dt_find_next(struct dt_node_iter* iter,
582 struct dt_node_base** matched)
584 if (!dt_found_any(iter)) {
588 struct dt_node_base *pos, *head;
594 while (&pos->siblings != &head->children)
596 pos = list_next(pos, struct dt_node_base, siblings);
598 if (!iter->pred(iter, pos)) {
610 dt_getprop(struct dt_node_base* base, const char* name)
612 struct hstr hashed_name;
613 struct dt_prop *pos, *n;
616 hashed_name = HSTR(name, strlen(name));
617 hstr_rehash(&hashed_name, HSTR_FULL_HASH);
618 hash = hashed_name.hash;
620 hashtable_hash_foreach(base->_op_bucket, hash, pos, n, ht)
622 if (HSTR_EQ(&pos->key, &hashed_name)) {