static int i2c_bus_fdt_init(char *node_path, FDTMachineInfo *fdti, void *priv) { Object *parent; DeviceState *dev; char parent_node_path[DT_PATH_LENGTH]; char *node_name = qemu_devtree_get_node_name(fdti->fdt, node_path); DB_PRINT_NP(1, "\n"); /* FIXME: share this code with fdt_generic_util.c/fdt_init_qdev() */ if (qemu_devtree_getparent(fdti->fdt, parent_node_path, node_path)) { abort(); } while (!fdt_init_has_opaque(fdti, parent_node_path)) { fdt_init_yield(fdti); } parent = fdt_init_get_opaque(fdti, parent_node_path); dev = (DeviceState *)object_dynamic_cast(parent, TYPE_DEVICE); if (parent && dev) { while (!dev->realized) { fdt_init_yield(fdti); } DB_PRINT_NP(0, "parenting i2c bus to %s bus %s\n", parent_node_path, node_name); fdt_init_set_opaque(fdti, node_path, qdev_get_child_bus(dev, node_name)); } else { DB_PRINT_NP(0, "orphaning i2c bus\n"); } return 0; }
FDTMachineInfo *fdt_generic_create_machine(void *fdt, qemu_irq *cpu_irq) { char node_path[DT_PATH_LENGTH]; FDTMachineInfo *fdti = fdt_init_new_fdti(fdt); fdti->irq_base = cpu_irq; /* parse the device tree */ if (!qemu_devtree_get_root_node(fdt, node_path)) { memory_region_transaction_begin(); fdt_init_set_opaque(fdti, node_path, NULL); simple_bus_fdt_init(node_path, fdti); while (qemu_co_enter_next(fdti->cq)); fdt_init_all_irqs(fdti); memory_region_transaction_commit(); } else { fprintf(stderr, "FDT: ERROR: cannot get root node from device tree %s\n" , node_path); } DB_PRINT(0, "FDT: Device tree scan complete\n"); FDTMachineInfo *ret = g_malloc0(sizeof(*ret)); return fdti; }
static void cpu_probe(FDTMachineInfo *fdti, const char *node_path, uint32_t offset) { //int i; DeviceState *dev; Error *errp = NULL; Nios2CPU *cpu = cpu_nios2_init("nios2"); qemu_register_reset(main_cpu_reset, cpu); #if 0 /* TODO: Finish off the vectored-interrupt-controller */ int reglen; const void *reg = qemu_devtree_getprop_offset(fdt, node, "reg", ®len); uint32_t irq_addr = qemu_devtree_int_array_index(reg, 0) + offset; int nrIrqLen; const void *nrIrq = qemu_devtree_getprop_offset(fdt, node, "ALTR,num-intr-inputs", &nrIrqLen); uint32_t nrIrqs = qemu_devtree_int_array_index(nrIrq, 0); printf(" IRQ BASE %08X NIRQS %d\n", irq_addr, nrIrqs); fdti->irq_base = nios2_pic_init_cpu(env); dev = altera_vic_create(irq_addr, fdti->irq_base[0], 2); #else /* Internal interrupt controller (IIC) */ fdti->irq_base = nios2_pic_init_cpu(cpu); dev = altera_iic_create(cpu, fdti->irq_base[0], 2); #endif /* TODO: use the entrypoint of the passed in elf file or the device-tree one */ #if 0 cpu->env.reset_addr = qemu_devtree_getprop_cell(fdt, node_path, "ALTR,reset-addr", 0, 0, &errp); #else cpu->env.reset_addr = 0xc0000000; #endif cpu->env.exception_addr = qemu_devtree_getprop_cell(fdti->fdt, node_path, "ALTR,exception-addr", 0, 0, &errp); cpu->env.fast_tlb_miss_addr = qemu_devtree_getprop_cell(fdti->fdt, node_path, "ALTR,fast-tlb-miss-addr", 0, 0, &errp); /* reset again to use the new reset vector */ cpu_reset(CPU(cpu)); fdt_init_set_opaque(fdti, node_path, dev); }
static void fdt_init_node(void *args) { struct FDTInitNodeArgs *a = args; char *node_path = a->node_path; FDTMachineInfo *fdti = a->fdti; g_free(a); char *all_compats = NULL, *compat, *node_name, *next_compat; int compat_len; #ifdef FDT_GENERIC_UTIL_ERR_DEBUG static int entry_index; int this_entry = entry_index++; #endif DB_PRINT("enter %d %s\n", this_entry, node_path); /* try instance binding first */ node_name = qemu_fdt_get_node_name(fdti->fdt, node_path); if (!node_name) { fprintf(stderr, "FDT: ERROR: nameless node: %s\n", node_path); } if (!fdt_init_inst_bind(node_path, fdti, node_name)) { goto exit; } /* fallback to compatibility binding */ all_compats = qemu_fdt_getprop(fdti->fdt, node_path, "compatible", &compat_len, false, NULL); if (!all_compats) { fprintf(stderr, "FDT: ERROR: no compatibility found for node %s/%s\n", node_path, node_name); DB_PRINT("exit %d\n", this_entry); fdti->routinesPending--; return; } compat = all_compats; try_next_compat: if (compat_len == 0) { goto invalidate; } if (!fdt_init_compat(node_path, fdti, compat)) { goto exit; } if (!fdt_init_qdev(node_path, fdti, compat)) { goto exit; } next_compat = rawmemchr(compat, '\0'); compat_len -= (next_compat + 1 - compat); if (compat_len > 0) { *next_compat = ' '; } compat = next_compat+1; goto try_next_compat; invalidate: fprintf(stderr, "FDT: Unsupported peripheral invalidated %s compatibilities %s\n", node_name, all_compats); qemu_fdt_setprop_string(fdti->fdt, node_path, "compatible", "invalidated"); exit: DB_PRINT("exit %d\n", this_entry); if (!fdt_init_has_opaque(fdti, node_path)) { fdt_init_set_opaque(fdti, node_path, NULL); } g_free(node_path); g_free(all_compats); fdti->routinesPending--; return; }
static int fdt_init_qdev(char *node_path, FDTMachineInfo *fdti, char *compat) { int err; qemu_irq irq; hwaddr base; int offset; DeviceState *dev; char *dev_type = NULL; int is_intc; int i; dev = fdt_create_qdev_from_compat(compat, &dev_type); if (!dev) { DB_PRINT("no match found for %s\n", compat); return 1; } /* FIXME: attach to the sysbus instead */ object_property_add_child(container_get(qdev_get_machine(), "/unattached"), qemu_fdt_get_node_name(fdti->fdt, node_path), OBJECT(dev), NULL); fdt_init_set_opaque(fdti, node_path, dev); /* connect nic if appropriate */ static int nics; if (object_property_find(OBJECT(dev), "mac", NULL)) { qdev_set_nic_properties(dev, &nd_table[nics]); if (nd_table[nics].instantiated) { DB_PRINT("NIC instantiated: %s\n", dev_type); nics++; } } offset = fdt_path_offset(fdti->fdt, node_path); for (offset = fdt_first_property_offset(fdti->fdt, offset); offset != -FDT_ERR_NOTFOUND; offset = fdt_next_property_offset(fdti->fdt, offset)) { const char *propname; int len; const void *val = fdt_getprop_by_offset(fdti->fdt, offset, &propname, &len); propname = trim_vendor(propname); ObjectProperty *p = object_property_find(OBJECT(dev), propname, NULL); if (p) { DB_PRINT("matched property: %s of type %s, len %d\n", propname, p->type, len); } if (!p) { continue; } /* FIXME: handle generically using accessors and stuff */ if (!strcmp(p->type, "uint8") || !strcmp(p->type, "uint16") || !strcmp(p->type, "uint32") || !strcmp(p->type, "uint64")) { uint64_t offset = (!strcmp(propname, "reg")) ? fdt_get_parent_base(node_path, fdti) : 0; object_property_set_int(OBJECT(dev), get_int_be(val, len) + offset, propname, &error_abort); DB_PRINT("set property %s to %#llx\n", propname, (long long unsigned int)get_int_be(val, len)); } else if (!strcmp(p->type, "bool")) { object_property_set_bool(OBJECT(dev), !!get_int_be(val, len), propname, &error_abort); DB_PRINT("set property %s to %#llx\n", propname, (long long unsigned int)get_int_be(val, len)); } else if (!strncmp(p->type, "link", 4)) { char target_node_path[DT_PATH_LENGTH]; DeviceState *linked_dev; if (qemu_fdt_get_node_by_phandle(fdti->fdt, target_node_path, get_int_be(val, len))) { abort(); } while (!fdt_init_has_opaque(fdti, target_node_path)) { fdt_init_yield(fdti); } linked_dev = fdt_init_get_opaque(fdti, target_node_path); object_property_set_link(OBJECT(dev), OBJECT(linked_dev), propname, &error_abort); } else if (!strcmp(p->type, "string")) { object_property_set_str(OBJECT(dev), strndup(val, len), propname, &error_abort); } } qdev_init_nofail(dev); /* map slave attachment */ base = qemu_fdt_getprop_cell(fdti->fdt, node_path, "reg", 0, false, &error_abort); base += fdt_get_parent_base(node_path, fdti); sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base); { int len; fdt_get_property(fdti->fdt, fdt_path_offset(fdti->fdt, node_path), "interrupt-controller", &len); is_intc = len >= 0; DB_PRINT("is interrupt controller: %c\n", is_intc ? 'y' : 'n'); } /* connect irq */ for (i = 0; ; ++i) { char irq_info[1024]; irq = fdt_get_irq_info(fdti, node_path, i, &err, irq_info); /* INTCs inferr their top level, if no IRQ connection specified */ if (err && is_intc) { irq = fdti->irq_base; sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, irq); fprintf(stderr, "FDT: (%s) connected top level irq %s\n", dev_type, irq_info); break; } if (!err) { sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, irq); fprintf(stderr, "FDT: (%s) connected irq %s\n", dev_type, irq_info); } else { break; } } if (dev_type) { g_free(dev_type); } return 0; }
static void fdt_init_node(void *args) { struct FDTInitNodeArgs *a = args; char *node_path = a->node_path; FDTMachineInfo *fdti = a->fdti; g_free(a); simple_bus_fdt_init(node_path, fdti); char *all_compats = NULL, *compat, *node_name, *next_compat, *device_type; int compat_len; DB_PRINT_NP(1, "enter\n"); /* try instance binding first */ node_name = qemu_devtree_get_node_name(fdti->fdt, node_path); DB_PRINT_NP(1, "node with name: %s\n", node_name ? node_name : "(none)"); if (!node_name) { printf("FDT: ERROR: nameless node: %s\n", node_path); } if (!fdt_init_inst_bind(node_path, fdti, node_name)) { DB_PRINT_NP(0, "instance bind successful\n"); goto exit; } /* fallback to compatibility binding */ all_compats = qemu_fdt_getprop(fdti->fdt, node_path, "compatible", &compat_len, false, NULL); if (!all_compats) { DB_PRINT_NP(0, "no compatibility found\n"); } for (compat = all_compats; compat && compat_len; compat = next_compat+1) { char *compat_prefixed = g_strdup_printf("compatible:%s", compat); if (!fdt_init_compat(node_path, fdti, compat_prefixed)) { goto exit; } g_free(compat_prefixed); if (!fdt_init_qdev(node_path, fdti, compat)) { goto exit; } next_compat = rawmemchr(compat, '\0'); compat_len -= (next_compat + 1 - compat); if (compat_len > 0) { *next_compat = ' '; } } device_type = qemu_fdt_getprop(fdti->fdt, node_path, "device_type", NULL, false, NULL); device_type = g_strdup_printf("device_type:%s", device_type); if (!fdt_init_compat(node_path, fdti, device_type)) { goto exit; } if (!all_compats) { goto exit; } DB_PRINT_NP(0, "FDT: Unsupported peripheral invalidated - " "compatibilities %s\n", all_compats); qemu_fdt_setprop_string(fdti->fdt, node_path, "compatible", "invalidated"); exit: DB_PRINT_NP(1, "exit\n"); if (!fdt_init_has_opaque(fdti, node_path)) { fdt_init_set_opaque(fdti, node_path, NULL); } g_free(node_path); g_free(all_compats); return; }