Skip to contentSkip to conversation

ppc: qdev-ify CPU creation

RFC In review Andreas Färber 4 participants 8 messages Dec 21, 2010 – Jul 25, 2011

lore
Share & tools
lore
#0 lore raw

RFC 1/1 ppc: qdev-ify CPU creation

From: Hervé Poussineau <[email protected]>

v1:
* Coding style fixes.

---

Hello Alex,

Seeing the discussions about Leon3, is this the way to go for ppc? Is ppc.[hc] right?

The unconditional use of 6xx looks suspicious to me, no? Should we rename cpu_device_irq_request() to cpu_device_irq_request_6xx()?

Regards,
Andreas

 hw/ppc.c            |   75 +++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/ppc.h            |    2 +
 target-ppc/cpu.h    |    1 +
 target-ppc/helper.c |   21 +++++++++++---
 4 files changed, 94 insertions(+), 5 deletions(-)
hw/ppc.c +75 -0
index 968aec1..0927326 100644
@@ -30,6 +30,8 @@
 #include "loader.h"
 #include "kvm.h"
 #include "kvm_ppc.h"
+#include "hw/qdev.h"
+#include "hw/sysbus.h"
 
 //#define PPC_DEBUG_IRQ
 //#define PPC_DEBUG_TB
@@ -1286,3 +1288,76 @@ int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size,
 
     return 0;
 }
+
+DeviceState *cpu_ppc_create_simple(const char *cpu_model)
+{
+    DeviceState *dev;
+
+    dev = qdev_create(NULL, "cpu-ppc");
+    if (!dev) {
+        return NULL;
+    }
+    qdev_prop_set_string(dev, "model", qemu_strdup(cpu_model));
+    if (qdev_init(dev) < 0) {
+        return NULL;
+    }
+    return dev;
+}
+
+typedef struct CPUPPC {
+    SysBusDevice busdev;
+    char *model;
+    CPUPPCState state;
+} CPUPPC;
+
+static void cpu_device_irq_request(void *opaque, int pin, int level)
+{
+    CPUPPC* cpu = opaque;
+    CPUPPCState* env = &cpu->state;
+    ppc6xx_set_irq(env, pin, level);
+}
+
+static int cpu_device_init(SysBusDevice *dev)
+{
+    CPUPPC* cpu = FROM_SYSBUS(CPUPPC, dev);
+    CPUPPCState* env = &cpu->state;
+
+    if (cpu_ppc_init_inplace(env, cpu->model) < 0) {
+        return -1;
+    }
+
+    if (env->flags & POWERPC_FLAG_RTC_CLK) {
+        /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
+        cpu_ppc_tb_init(env, 7812500UL);
+    } else {
+        /* Set time-base frequency to 100 Mhz */
+        cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
+    }
+
+    qdev_init_gpio_in(&dev->qdev, cpu_device_irq_request, PPC6xx_INPUT_NB);
+    return 0;
+}
+
+static void cpu_device_reset(DeviceState *d)
+{
+    CPUPPC *s = FROM_SYSBUS(CPUPPC, sysbus_from_qdev(d));
+    cpu_reset(&s->state);
+}
+
+static SysBusDeviceInfo cpu_device_info = {
+    .qdev.name = "cpu-ppc",
+    .qdev.size = sizeof(CPUPPC),
+    .qdev.reset = cpu_device_reset,
+    .init = cpu_device_init,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_STRING("model", CPUPPC, model),
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};
+
+static void ppc_register_devices(void)
+{
+    sysbus_register_withprop(&cpu_device_info);
+}
+
+device_init(ppc_register_devices)
hw/ppc.h +2 -0
index 34f54cf..ae8dd97 100644
@@ -37,6 +37,8 @@ void ppce500_irq_init (CPUState *env);
 void ppc6xx_irq_init (CPUState *env);
 void ppc970_irq_init (CPUState *env);
 
+DeviceState *cpu_ppc_create_simple(const char *cpu_model);
+
 /* PPC machines for OpenBIOS */
 enum {
     ARCH_PREP = 0,
target-ppc/cpu.h +1 -0
index deb8d7c..0f56d45 100644
@@ -721,6 +721,7 @@ struct mmu_ctx_t {
 
 /*****************************************************************************/
 CPUPPCState *cpu_ppc_init (const char *cpu_model);
+int cpu_ppc_init_inplace(CPUPPCState *env, const char *cpu_model);
 void ppc_translate_init(void);
 int cpu_ppc_exec (CPUPPCState *s);
 void cpu_ppc_close (CPUPPCState *s);
target-ppc/helper.c +16 -5
index 4b49101..99af1f6 100644
@@ -2794,22 +2794,33 @@ void cpu_reset(CPUPPCState *env)
     tlb_flush(env, 1);
 }
 
-CPUPPCState *cpu_ppc_init (const char *cpu_model)
+int cpu_ppc_init_inplace(CPUPPCState *env, const char *cpu_model)
 {
-    CPUPPCState *env;
     const ppc_def_t *def;
 
     def = cpu_ppc_find_by_name(cpu_model);
-    if (!def)
-        return NULL;
+    if (!def) {
+        return -1;
+    }
 
-    env = qemu_mallocz(sizeof(CPUPPCState));
     cpu_exec_init(env);
     ppc_translate_init();
     env->cpu_model_str = cpu_model;
     cpu_ppc_register_internal(env, def);
 
     qemu_init_vcpu(env);
+    return 0;
+}
+
+CPUPPCState *cpu_ppc_init(const char *cpu_model)
+{
+    CPUPPCState *env;
+
+    env = qemu_mallocz(sizeof(CPUPPCState));
+    if (cpu_ppc_init_inplace(env, cpu_model) < 0) {
+        qemu_free(env);
+        return NULL;
+    }
 
     return env;
 }
Signature · 1 line
1.7.3.4
#1 lore raw

Am 21.12.2010 um 21:01 schrieb Andreas Färber:

Quoted 12 lines

From: Hervé Poussineau <[email protected]>

v1:
* Coding style fixes.

---

Hello Alex,

Quoted 7 lines

Seeing the discussions about Leon3, is this the way to go for ppc? Is ppc.[hc] right?

The unconditional use of 6xx looks suspicious to me, no? Should we rename cpu_device_irq_request() to cpu_device_irq_request_6xx()?

Regards,
Andreas

Ping? Any guidance on how to proceed with this?

Andreas

Quoted 169 lines

hw/ppc.c | 75 ++++++++++++++++++++++++++++++++++++++++++
+++++++++

hw/ppc.h            |    2 +
target-ppc/cpu.h    |    1 +
target-ppc/helper.c |   21 +++++++++++---
4 files changed, 94 insertions(+), 5 deletions(-)
hw/ppc.c +2 -0
index 968aec1..0927326 100644
@@ -30,6 +30,8 @@
 #include "loader.h"
 #include "kvm.h"
 #include "kvm_ppc.h"
+#include "hw/qdev.h"
+#include "hw/sysbus.h"
 
 //#define PPC_DEBUG_IRQ
 //#define PPC_DEBUG_TB
@@ -1286,3 +1288,76 @@ int PPC_NVRAM_set_params (nvram_t *nvram,  
 uint16_t NVRAM_size,
 
    return 0;

}

hw/ppc.c +47 -0
+
+DeviceState *cpu_ppc_create_simple(const char *cpu_model)
+{
+    DeviceState *dev;
+
+    dev = qdev_create(NULL, "cpu-ppc");
+    if (!dev) {
+        return NULL;
+    }
+    qdev_prop_set_string(dev, "model", qemu_strdup(cpu_model));
+    if (qdev_init(dev) < 0) {
+        return NULL;
+    }
+    return dev;
+}
+
+typedef struct CPUPPC {
+    SysBusDevice busdev;
+    char *model;
+    CPUPPCState state;
+} CPUPPC;
+
+static void cpu_device_irq_request(void *opaque, int pin, int level)
+{
+    CPUPPC* cpu = opaque;
+    CPUPPCState* env = &cpu->state;
+    ppc6xx_set_irq(env, pin, level);
+}
+
+static int cpu_device_init(SysBusDevice *dev)
+{
+    CPUPPC* cpu = FROM_SYSBUS(CPUPPC, dev);
+    CPUPPCState* env = &cpu->state;
+
+    if (cpu_ppc_init_inplace(env, cpu->model) < 0) {
+        return -1;
+    }
+
+    if (env->flags & POWERPC_FLAG_RTC_CLK) {
+        /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
+        cpu_ppc_tb_init(env, 7812500UL);
+    } else {
+        /* Set time-base frequency to 100 Mhz */
+        cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
+    }
+
+    qdev_init_gpio_in(&dev->qdev, cpu_device_irq_request,  

PPC6xx_INPUT_NB);

hw/ppc.c +26 -0
+    return 0;
+}
+
+static void cpu_device_reset(DeviceState *d)
+{
+    CPUPPC *s = FROM_SYSBUS(CPUPPC, sysbus_from_qdev(d));
+    cpu_reset(&s->state);
+}
+
+static SysBusDeviceInfo cpu_device_info = {
+    .qdev.name = "cpu-ppc",
+    .qdev.size = sizeof(CPUPPC),
+    .qdev.reset = cpu_device_reset,
+    .init = cpu_device_init,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_STRING("model", CPUPPC, model),
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};
+
+static void ppc_register_devices(void)
+{
+    sysbus_register_withprop(&cpu_device_info);
+}
+
+device_init(ppc_register_devices)
hw/ppc.h +2 -0
index 34f54cf..ae8dd97 100644
@@ -37,6 +37,8 @@ void ppce500_irq_init (CPUState *env);
 void ppc6xx_irq_init (CPUState *env);
 void ppc970_irq_init (CPUState *env);
 
+DeviceState *cpu_ppc_create_simple(const char *cpu_model);
+
 /* PPC machines for OpenBIOS */
 enum {
    ARCH_PREP = 0,
target-ppc/cpu.h +1 -0
index deb8d7c..0f56d45 100644
@@ -721,6 +721,7 @@ struct mmu_ctx_t {
 
 /*****************************************************************************/
 CPUPPCState *cpu_ppc_init (const char *cpu_model);
+int cpu_ppc_init_inplace(CPUPPCState *env, const char *cpu_model);
 void ppc_translate_init(void);
 int cpu_ppc_exec (CPUPPCState *s);
 void cpu_ppc_close (CPUPPCState *s);
target-ppc/helper.c +16 -5
index 4b49101..99af1f6 100644
@@ -2794,22 +2794,33 @@ void cpu_reset(CPUPPCState *env)
    tlb_flush(env, 1);
 }
 
-CPUPPCState *cpu_ppc_init (const char *cpu_model)
+int cpu_ppc_init_inplace(CPUPPCState *env, const char *cpu_model)
 {
-    CPUPPCState *env;
    const ppc_def_t *def;
 
    def = cpu_ppc_find_by_name(cpu_model);
-    if (!def)
-        return NULL;
+    if (!def) {
+        return -1;
+    }
 
-    env = qemu_mallocz(sizeof(CPUPPCState));
    cpu_exec_init(env);
    ppc_translate_init();
    env->cpu_model_str = cpu_model;
    cpu_ppc_register_internal(env, def);
 
    qemu_init_vcpu(env);
+    return 0;
+}
+
+CPUPPCState *cpu_ppc_init(const char *cpu_model)
+{
+    CPUPPCState *env;
+
+    env = qemu_mallocz(sizeof(CPUPPCState));
+    if (cpu_ppc_init_inplace(env, cpu_model) < 0) {
+        qemu_free(env);
+        return NULL;
+    }
 
    return env;
 }
Signature · 1 line
1.7.3.4
#2 lore raw
Quoted 17 lines · Andreas Färber

Am 21.12.2010 um 21:01 schrieb Andreas Färber:

Quoted 15 lines

From: Hervé Poussineau <[email protected]>

v1:
* Coding style fixes.

---

Hello Alex,

Seeing the discussions about Leon3, is this the way to go for ppc? Is ppc.[hc] right?

Quoted 7 lines
Quoted 5 lines

The unconditional use of 6xx looks suspicious to me, no? Should we rename cpu_device_irq_request() to cpu_device_irq_request_6xx()?

Regards,
Andreas

Ping? Any guidance on how to proceed with this?

The patch looks OK, though the qdev method is not used.

Ideally after the patch, the devices and even the board level shouldn't use CPUState but DeviceState.

#3 lore raw
In reply to Andreas Färber
Quoted 51 lines · Andreas Färber

From: Hervé Poussineau <[email protected]>

v1:
* Coding style fixes.

---

Hello Alex,

Seeing the discussions about Leon3, is this the way to go for ppc? Is ppc.[hc] right?

The unconditional use of 6xx looks suspicious to me, no? Should we rename cpu_device_irq_request() to cpu_device_irq_request_6xx()?

Regards,
Andreas

hw/ppc.c            |   75 +++++++++++++++++++++++++++++++++++++++++++++++++++
hw/ppc.h            |    2 +
target-ppc/cpu.h    |    1 +
target-ppc/helper.c |   21 +++++++++++---
4 files changed, 94 insertions(+), 5 deletions(-)
hw/ppc.c +10 -0
index 968aec1..0927326 100644
@@ -30,6 +30,8 @@
 #include "loader.h"
 #include "kvm.h"
 #include "kvm_ppc.h"
+#include "hw/qdev.h"
+#include "hw/sysbus.h"
 
 //#define PPC_DEBUG_IRQ
 //#define PPC_DEBUG_TB
@@ -1286,3 +1288,76 @@ int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size,
 
    return 0;
 }
+
+DeviceState *cpu_ppc_create_simple(const char *cpu_model)
+{
+    DeviceState *dev;
+
+    dev = qdev_create(NULL, "cpu-ppc");
+    if (!dev) {
+        return NULL;

hw/ppc.c int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size, · line 1308

Quoted 10 lines
hw/ppc.c +10 -0
+    }
+    qdev_prop_set_string(dev, "model", qemu_strdup(cpu_model));
+    if (qdev_init(dev) < 0) {
+        return NULL;
+    }
+    return dev;
+}
+
+typedef struct CPUPPC {
+    SysBusDevice busdev;

I'm not sure we really want CPUs on the sysbus. They belong to their own CPU bus. Basically, I think we should try to model our bus topology so that it reflects the bus topology in the device tree 1:1. Then generating a device tree from the bug information and some device specific callbacks would be possible.

Quoted 11 lines
hw/ppc.c +11 -0
+    char *model;
+    CPUPPCState state;
+} CPUPPC;
+
+static void cpu_device_irq_request(void *opaque, int pin, int level)
+{
+    CPUPPC* cpu = opaque;
+    CPUPPCState* env = &cpu->state;
+    ppc6xx_set_irq(env, pin, level);
+}
+

hw/ppc.c int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size, · line 1329

Quoted 10 lines
hw/ppc.c +10 -0
+static int cpu_device_init(SysBusDevice *dev)
+{
+    CPUPPC* cpu = FROM_SYSBUS(CPUPPC, dev);
+    CPUPPCState* env = &cpu->state;
+
+    if (cpu_ppc_init_inplace(env, cpu->model) < 0) {
+        return -1;
+    }
+
+    if (env->flags & POWERPC_FLAG_RTC_CLK) {

Where does this flag suddenly come from? Is this related to qdev'ification?

hw/ppc.c int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size, · line 1334

Quoted 5 lines
hw/ppc.c +5 -0
+        /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
+        cpu_ppc_tb_init(env, 7812500UL);
+    } else {
+        /* Set time-base frequency to 100 Mhz */
+        cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);

Usually we have a TB frequency of 400Mhz in our board/devtrees hardcoded in the TCG case. How about a qdev property that the creator could just modify to its needs? We won't need the special 601 flag then either - just move that into the PREP code.

Quoted 81 lines
hw/ppc.c +29 -0
+    }
+
+    qdev_init_gpio_in(&dev->qdev, cpu_device_irq_request, PPC6xx_INPUT_NB);
+    return 0;
+}
+
+static void cpu_device_reset(DeviceState *d)
+{
+    CPUPPC *s = FROM_SYSBUS(CPUPPC, sysbus_from_qdev(d));
+    cpu_reset(&s->state);
+}
+
+static SysBusDeviceInfo cpu_device_info = {
+    .qdev.name = "cpu-ppc",
+    .qdev.size = sizeof(CPUPPC),
+    .qdev.reset = cpu_device_reset,
+    .init = cpu_device_init,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_STRING("model", CPUPPC, model),
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};
+
+static void ppc_register_devices(void)
+{
+    sysbus_register_withprop(&cpu_device_info);
+}
+
+device_init(ppc_register_devices)
hw/ppc.h +2 -0
index 34f54cf..ae8dd97 100644
@@ -37,6 +37,8 @@ void ppce500_irq_init (CPUState *env);
 void ppc6xx_irq_init (CPUState *env);
 void ppc970_irq_init (CPUState *env);
 
+DeviceState *cpu_ppc_create_simple(const char *cpu_model);
+
 /* PPC machines for OpenBIOS */
 enum {
    ARCH_PREP = 0,
target-ppc/cpu.h +1 -0
index deb8d7c..0f56d45 100644
@@ -721,6 +721,7 @@ struct mmu_ctx_t {
 
 /*****************************************************************************/
 CPUPPCState *cpu_ppc_init (const char *cpu_model);
+int cpu_ppc_init_inplace(CPUPPCState *env, const char *cpu_model);
 void ppc_translate_init(void);
 int cpu_ppc_exec (CPUPPCState *s);
 void cpu_ppc_close (CPUPPCState *s);
target-ppc/helper.c +4 -5
index 4b49101..99af1f6 100644
@@ -2794,22 +2794,33 @@ void cpu_reset(CPUPPCState *env)
    tlb_flush(env, 1);
 }
 
-CPUPPCState *cpu_ppc_init (const char *cpu_model)
+int cpu_ppc_init_inplace(CPUPPCState *env, const char *cpu_model)
 {
-    CPUPPCState *env;
    const ppc_def_t *def;
 
    def = cpu_ppc_find_by_name(cpu_model);
-    if (!def)
-        return NULL;
+    if (!def) {
+        return -1;
+    }
 
-    env = qemu_mallocz(sizeof(CPUPPCState));
    cpu_exec_init(env);
    ppc_translate_init();
    env->cpu_model_str = cpu_model;
    cpu_ppc_register_internal(env, def);
 

target-ppc/helper.c void cpu_reset(CPUPPCState *env) · line 2820

Quoted 10 lines
target-ppc/helper.c +9 -0
    qemu_init_vcpu(env);
+    return 0;
+}
+
+CPUPPCState *cpu_ppc_init(const char *cpu_model)
+{
+    CPUPPCState *env;
+
+    env = qemu_mallocz(sizeof(CPUPPCState));
+    if (cpu_ppc_init_inplace(env, cpu_model) < 0) {

Why would we need this function again if the CPUs are qdev'ified?

Overall, I really like the idea of moving CPUs to qdev though. Makes things a lot more structured.

Alex

#4 lore raw
Quoted 59 lines · Alexander Graf
Quoted 56 lines · Andreas Färber

From: Hervé Poussineau <[email protected]>

v1:
* Coding style fixes.

---

Hello Alex,

Seeing the discussions about Leon3, is this the way to go for ppc? Is ppc.[hc] right?

The unconditional use of 6xx looks suspicious to me, no? Should we rename cpu_device_irq_request() to cpu_device_irq_request_6xx()?

Regards,
Andreas

hw/ppc.c            |   75 +++++++++++++++++++++++++++++++++++++++++++++++++++
hw/ppc.h            |    2 +
target-ppc/cpu.h    |    1 +
target-ppc/helper.c |   21 +++++++++++---
4 files changed, 94 insertions(+), 5 deletions(-)
hw/ppc.c +15 -0
index 968aec1..0927326 100644
@@ -30,6 +30,8 @@
 #include "loader.h"
 #include "kvm.h"
 #include "kvm_ppc.h"
+#include "hw/qdev.h"
+#include "hw/sysbus.h"
 
 //#define PPC_DEBUG_IRQ
 //#define PPC_DEBUG_TB
@@ -1286,3 +1288,76 @@ int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size,
 
    return 0;
 }
+
+DeviceState *cpu_ppc_create_simple(const char *cpu_model)
+{
+    DeviceState *dev;
+
+    dev = qdev_create(NULL, "cpu-ppc");
+    if (!dev) {
+        return NULL;
+    }
+    qdev_prop_set_string(dev, "model", qemu_strdup(cpu_model));
+    if (qdev_init(dev) < 0) {
+        return NULL;
+    }
Quoted 10 lines

hw/ppc.c int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size, · line 1308

Quoted 6 lines
hw/ppc.c +5 -0
+    return dev;
+}
+
+typedef struct CPUPPC {
+    SysBusDevice busdev;

I'm not sure we really want CPUs on the sysbus. They belong to their own CPU bus. Basically, I think we should try to model our bus topology so that it reflects the bus topology in the device tree 1:1. Then generating a device tree from the bug information and some device specific callbacks would be possible.

CPUs don't need a bus with specific capabilities, so I used the most simple existing one, ie SysBus.

Quoted 16 lines
Quoted 16 lines
hw/ppc.c +16 -0
+    char *model;
+    CPUPPCState state;
+} CPUPPC;
+
+static void cpu_device_irq_request(void *opaque, int pin, int level)
+{
+    CPUPPC* cpu = opaque;
+    CPUPPCState* env = &cpu->state;
+    ppc6xx_set_irq(env, pin, level);
+}
+
+static int cpu_device_init(SysBusDevice *dev)
+{
+    CPUPPC* cpu = FROM_SYSBUS(CPUPPC, dev);
+    CPUPPCState* env = &cpu->state;
+
Quoted 10 lines

hw/ppc.c int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size, · line 1329

Quoted 6 lines
hw/ppc.c +5 -0
+    if (cpu_ppc_init_inplace(env, cpu->model) < 0) {
+        return -1;
+    }
+
+    if (env->flags & POWERPC_FLAG_RTC_CLK) {

Where does this flag suddenly come from? Is this related to qdev'ification?

It's not related to qdev'ification. It is already set in CPU definitions since a long time.

Quoted 10 lines

hw/ppc.c int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size, · line 1334

Quoted 6 lines
hw/ppc.c +5 -0
+        /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
+        cpu_ppc_tb_init(env, 7812500UL);
+    } else {
+        /* Set time-base frequency to 100 Mhz */
+        cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);

Usually we have a TB frequency of 400Mhz in our board/devtrees hardcoded in the TCG case. How about a qdev property that the creator could just modify to its needs? We won't need the special 601 flag then either - just move that into the PREP code.

This code has been extracted from ppc_prep.c ; a qdev property is also fine.

Quoted 81 lines
Quoted 81 lines
hw/ppc.c +29 -0
+    }
+
+    qdev_init_gpio_in(&dev->qdev, cpu_device_irq_request, PPC6xx_INPUT_NB);
+    return 0;
+}
+
+static void cpu_device_reset(DeviceState *d)
+{
+    CPUPPC *s = FROM_SYSBUS(CPUPPC, sysbus_from_qdev(d));
+    cpu_reset(&s->state);
+}
+
+static SysBusDeviceInfo cpu_device_info = {
+    .qdev.name = "cpu-ppc",
+    .qdev.size = sizeof(CPUPPC),
+    .qdev.reset = cpu_device_reset,
+    .init = cpu_device_init,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_STRING("model", CPUPPC, model),
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};
+
+static void ppc_register_devices(void)
+{
+    sysbus_register_withprop(&cpu_device_info);
+}
+
+device_init(ppc_register_devices)
hw/ppc.h +2 -0
index 34f54cf..ae8dd97 100644
@@ -37,6 +37,8 @@ void ppce500_irq_init (CPUState *env);
 void ppc6xx_irq_init (CPUState *env);
 void ppc970_irq_init (CPUState *env);
 
+DeviceState *cpu_ppc_create_simple(const char *cpu_model);
+
 /* PPC machines for OpenBIOS */
 enum {
    ARCH_PREP = 0,
target-ppc/cpu.h +1 -0
index deb8d7c..0f56d45 100644
@@ -721,6 +721,7 @@ struct mmu_ctx_t {
 
 /*****************************************************************************/
 CPUPPCState *cpu_ppc_init (const char *cpu_model);
+int cpu_ppc_init_inplace(CPUPPCState *env, const char *cpu_model);
 void ppc_translate_init(void);
 int cpu_ppc_exec (CPUPPCState *s);
 void cpu_ppc_close (CPUPPCState *s);
target-ppc/helper.c +4 -5
index 4b49101..99af1f6 100644
@@ -2794,22 +2794,33 @@ void cpu_reset(CPUPPCState *env)
    tlb_flush(env, 1);
 }
 
-CPUPPCState *cpu_ppc_init (const char *cpu_model)
+int cpu_ppc_init_inplace(CPUPPCState *env, const char *cpu_model)
 {
-    CPUPPCState *env;
    const ppc_def_t *def;
 
    def = cpu_ppc_find_by_name(cpu_model);
-    if (!def)
-        return NULL;
+    if (!def) {
+        return -1;
+    }
 
-    env = qemu_mallocz(sizeof(CPUPPCState));
    cpu_exec_init(env);
    ppc_translate_init();
    env->cpu_model_str = cpu_model;
    cpu_ppc_register_internal(env, def);
 
Quoted 14 lines

target-ppc/helper.c void cpu_reset(CPUPPCState *env) · line 2824

Quoted 11 lines
target-ppc/helper.c +9 -0
    qemu_init_vcpu(env);
+    return 0;
+}
+
+CPUPPCState *cpu_ppc_init(const char *cpu_model)
+{
+    CPUPPCState *env;
+
+    env = qemu_mallocz(sizeof(CPUPPCState));
+    if (cpu_ppc_init_inplace(env, cpu_model) < 0) {
 

Why would we need this function again if the CPUs are qdev'ified?

This function is not added ; it is already an existing one (see 25 lines before). I kept it to not put in the same patch the CPU qdev'ification and the change of all the callers. Indeed, a second patch may be created to change all callers to use cpu_ppc_create_simple() and to remove this function.

Quoted 2 lines

Overall, I really like the idea of moving CPUs to qdev though. Makes things a lot more structured.

Thanks

Hervé

#5 lore raw
Quoted 70 lines · Hervé Poussineau
Quoted 59 lines · Alexander Graf
Quoted 56 lines · Andreas Färber

From: Hervé Poussineau <[email protected]>

v1:
* Coding style fixes.

---

Hello Alex,

Seeing the discussions about Leon3, is this the way to go for ppc? Is ppc.[hc] right?

The unconditional use of 6xx looks suspicious to me, no? Should we rename cpu_device_irq_request() to cpu_device_irq_request_6xx()?

Regards,
Andreas

hw/ppc.c            |   75 +++++++++++++++++++++++++++++++++++++++++++++++++++
hw/ppc.h            |    2 +
target-ppc/cpu.h    |    1 +
target-ppc/helper.c |   21 +++++++++++---
4 files changed, 94 insertions(+), 5 deletions(-)
hw/ppc.c +15 -0
index 968aec1..0927326 100644
@@ -30,6 +30,8 @@
 #include "loader.h"
 #include "kvm.h"
 #include "kvm_ppc.h"
+#include "hw/qdev.h"
+#include "hw/sysbus.h"
 
 //#define PPC_DEBUG_IRQ
 //#define PPC_DEBUG_TB
@@ -1286,3 +1288,76 @@ int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size,
 
   return 0;
 }
+
+DeviceState *cpu_ppc_create_simple(const char *cpu_model)
+{
+    DeviceState *dev;
+
+    dev = qdev_create(NULL, "cpu-ppc");
+    if (!dev) {
+        return NULL;
+    }
+    qdev_prop_set_string(dev, "model", qemu_strdup(cpu_model));
+    if (qdev_init(dev) < 0) {
+        return NULL;
+    }
Quoted 10 lines

hw/ppc.c int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size, · line 1308

Quoted 6 lines
hw/ppc.c +5 -0
+    return dev;
+}
+
+typedef struct CPUPPC {
+    SysBusDevice busdev;

I'm not sure we really want CPUs on the sysbus. They belong to their own CPU bus. Basically, I think we should try to model our bus topology so that it reflects the bus topology in the device tree 1:1. Then generating a device tree from the bug information and some device specific callbacks would be possible.

Quoted 1 line

CPUs don't need a bus with specific capabilities, so I used the most simple existing one, ie SysBus.

Yeah, that's nice and simple from the implementation POV, but I have this funny idea that we might be able to generate a device tree from qemu's bus topology. And at that point we'll have to have a separate CPU bus.

Quoted 26 lines
Quoted 16 lines
Quoted 16 lines
hw/ppc.c +16 -0
+    char *model;
+    CPUPPCState state;
+} CPUPPC;
+
+static void cpu_device_irq_request(void *opaque, int pin, int level)
+{
+    CPUPPC* cpu = opaque;
+    CPUPPCState* env = &cpu->state;
+    ppc6xx_set_irq(env, pin, level);
+}
+
+static int cpu_device_init(SysBusDevice *dev)
+{
+    CPUPPC* cpu = FROM_SYSBUS(CPUPPC, dev);
+    CPUPPCState* env = &cpu->state;
+
Quoted 10 lines

hw/ppc.c int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size, · line 1329

Quoted 6 lines
hw/ppc.c +5 -0
+    if (cpu_ppc_init_inplace(env, cpu->model) < 0) {
+        return -1;
+    }
+
+    if (env->flags & POWERPC_FLAG_RTC_CLK) {

Where does this flag suddenly come from? Is this related to qdev'ification?

Quoted 1 line

It's not related to qdev'ification. It is already set in CPU definitions since a long time.

Hrm. So where is it usually read out then? Shouldn't that code go away?

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
hw/ppc.c +5 -0
+        /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
+        cpu_ppc_tb_init(env, 7812500UL);
+    } else {
+        /* Set time-base frequency to 100 Mhz */
+        cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
Quoted 4 lines
Quoted 3 lines

Usually we have a TB frequency of 400Mhz in our board/devtrees hardcoded in the TCG case. How about a qdev property that the creator could just modify to its needs? We won't need the special 601 flag then either - just move that into the PREP code.

This code has been extracted from ppc_prep.c ; a qdev property is also fine.

Yes, please :)

Quoted 95 lines
Quoted 81 lines
Quoted 81 lines
hw/ppc.c +29 -0
+    }
+
+    qdev_init_gpio_in(&dev->qdev, cpu_device_irq_request, PPC6xx_INPUT_NB);
+    return 0;
+}
+
+static void cpu_device_reset(DeviceState *d)
+{
+    CPUPPC *s = FROM_SYSBUS(CPUPPC, sysbus_from_qdev(d));
+    cpu_reset(&s->state);
+}
+
+static SysBusDeviceInfo cpu_device_info = {
+    .qdev.name = "cpu-ppc",
+    .qdev.size = sizeof(CPUPPC),
+    .qdev.reset = cpu_device_reset,
+    .init = cpu_device_init,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_STRING("model", CPUPPC, model),
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};
+
+static void ppc_register_devices(void)
+{
+    sysbus_register_withprop(&cpu_device_info);
+}
+
+device_init(ppc_register_devices)
hw/ppc.h +2 -0
index 34f54cf..ae8dd97 100644
@@ -37,6 +37,8 @@ void ppce500_irq_init (CPUState *env);
 void ppc6xx_irq_init (CPUState *env);
 void ppc970_irq_init (CPUState *env);
 
+DeviceState *cpu_ppc_create_simple(const char *cpu_model);
+
 /* PPC machines for OpenBIOS */
 enum {
   ARCH_PREP = 0,
target-ppc/cpu.h +1 -0
index deb8d7c..0f56d45 100644
@@ -721,6 +721,7 @@ struct mmu_ctx_t {
 
 /*****************************************************************************/
 CPUPPCState *cpu_ppc_init (const char *cpu_model);
+int cpu_ppc_init_inplace(CPUPPCState *env, const char *cpu_model);
 void ppc_translate_init(void);
 int cpu_ppc_exec (CPUPPCState *s);
 void cpu_ppc_close (CPUPPCState *s);
target-ppc/helper.c +4 -5
index 4b49101..99af1f6 100644
@@ -2794,22 +2794,33 @@ void cpu_reset(CPUPPCState *env)
   tlb_flush(env, 1);
 }
 
-CPUPPCState *cpu_ppc_init (const char *cpu_model)
+int cpu_ppc_init_inplace(CPUPPCState *env, const char *cpu_model)
 {
-    CPUPPCState *env;
   const ppc_def_t *def;
 
   def = cpu_ppc_find_by_name(cpu_model);
-    if (!def)
-        return NULL;
+    if (!def) {
+        return -1;
+    }
 
-    env = qemu_mallocz(sizeof(CPUPPCState));
   cpu_exec_init(env);
   ppc_translate_init();
   env->cpu_model_str = cpu_model;
   cpu_ppc_register_internal(env, def);
 
Quoted 14 lines

target-ppc/helper.c void cpu_reset(CPUPPCState *env) · line 2824

Quoted 11 lines
target-ppc/helper.c +9 -0
   qemu_init_vcpu(env);
+    return 0;
+}
+
+CPUPPCState *cpu_ppc_init(const char *cpu_model)
+{
+    CPUPPCState *env;
+
+    env = qemu_mallocz(sizeof(CPUPPCState));
+    if (cpu_ppc_init_inplace(env, cpu_model) < 0) {
 

Why would we need this function again if the CPUs are qdev'ified?

Quoted 2 lines

This function is not added ; it is already an existing one (see 25 lines before). I kept it to not put in the same patch the CPU qdev'ification and the change of all the callers. Indeed, a second patch may be created to change all callers to use cpu_ppc_create_simple() and to remove this function.

Ah, I see. Please write up a patch set that goes through the bits of fully converting it to Qdev :)

Alex

#6 lore raw

Am 25.07.2011 um 12:09 schrieb Alexander Graf:

Quoted 24 lines · Hervé Poussineau
Quoted 23 lines
Quoted 21 lines · Alexander Graf
Quoted 19 lines · Andreas Färber

From: Hervé Poussineau <[email protected]>

v1:
* Coding style fixes.

---

Hello Alex,

Seeing the discussions about Leon3, is this the way to go for ppc? Is ppc.[hc] right?

The unconditional use of 6xx looks suspicious to me, no? Should we rename cpu_device_irq_request() to cpu_device_irq_request_6xx()?

Regards,
Andreas

[snip]

Quoted 9 lines
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

+ if (env->flags & POWERPC_FLAG_RTC_CLK) {

Where does this flag suddenly come from? Is this related to qdev'ification?

It's not related to qdev'ification. It is already set in CPU definitions since a long time.

Hrm. So where is it usually read out then? Shouldn't that code go away?

This is code moved 1:1 from PReP code to general ppc code, which I referred to above. I fear this is wrong.

Quoted 25 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

+ /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz
*/

hw/ppc.c +4 -0
+        cpu_ppc_tb_init(env, 7812500UL);
+    } else {
+        /* Set time-base frequency to 100 Mhz */
+        cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
Quoted 4 lines
Quoted 3 lines

Usually we have a TB frequency of 400Mhz in our board/devtrees hardcoded in the TCG case. How about a qdev property that the creator could just modify to its needs? We won't need the special 601 flag then either - just move that into the PREP code.

This code has been extracted from ppc_prep.c ; a qdev property is also fine.

Yes, please :)

Quoted 10 lines
Quoted 10 lines

target-ppc/helper.c void cpu_reset(CPUPPCState *env) · line 2820

Quoted 7 lines
target-ppc/helper.c +6 -0
+CPUPPCState *cpu_ppc_init(const char *cpu_model)
+{
+    CPUPPCState *env;
+
+    env = qemu_mallocz(sizeof(CPUPPCState));
+    if (cpu_ppc_init_inplace(env, cpu_model) < 0) {

Why would we need this function again if the CPUs are qdev'ified?

Quoted 4 lines
Quoted 2 lines

This function is not added ; it is already an existing one (see 25 lines before). I kept it to not put in the same patch the CPU qdev'ification and the change of all the callers. Indeed, a second patch may be created to change all callers to use cpu_ppc_create_simple() and to remove this function.

Ah, I see. Please write up a patch set that goes through the bits of fully converting it to Qdev :)

Could you please consider pulling my more recent patch of a PReP-local static function for shared CPU initialization into ppc-next? That way we can share code between PReP machines now and easily convert it in one central place when CPU qdev'ification has been completed.

Andreas

#7 lore raw
Quoted 14 lines · Andreas Färber

Am 25.07.2011 um 12:09 schrieb Alexander Graf:

Quoted 12 lines · Hervé Poussineau
Quoted 11 lines
Quoted 11 lines

target-ppc/helper.c void cpu_reset(CPUPPCState *env) · line 2820

Quoted 8 lines
target-ppc/helper.c +6 -0
+CPUPPCState *cpu_ppc_init(const char *cpu_model)
+{
+    CPUPPCState *env;
+
+    env = qemu_mallocz(sizeof(CPUPPCState));
+    if (cpu_ppc_init_inplace(env, cpu_model) < 0) {

Why would we need this function again if the CPUs are qdev'ified?

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

This function is not added ; it is already an existing one (see 25 lines before). I kept it to not put in the same patch the CPU qdev'ification and the change of all the callers. Indeed, a second patch may be created to change all callers to use cpu_ppc_create_simple() and to remove this function.

Ah, I see. Please write up a patch set that goes through the bits of fully converting it to Qdev :)

Could you please consider pulling my more recent patch of a PReP-local static function for shared CPU initialization into ppc-next? That way we can share code between PReP machines now and easily convert it in one central place when CPU qdev'ification has been completed.

Got a subject line for me? :)

Alex