ppc.c 37.8 KB
Newer Older
1
/*
2
 * QEMU generic PowerPC hardware System Emulator
3
 *
4
 * Copyright (c) 2003-2007 Jocelyn Mayer
5
 *
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
P
pbrook 已提交
24 25 26 27 28
#include "hw.h"
#include "ppc.h"
#include "qemu-timer.h"
#include "sysemu.h"
#include "nvram.h"
B
blueswir1 已提交
29
#include "qemu-log.h"
B
Blue Swirl 已提交
30
#include "loader.h"
31 32
#include "kvm.h"
#include "kvm_ppc.h"
33

34
//#define PPC_DEBUG_IRQ
J
j_mayer 已提交
35
//#define PPC_DEBUG_TB
36

37
#ifdef PPC_DEBUG_IRQ
38
#  define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
39 40 41 42 43 44
#else
#  define LOG_IRQ(...) do { } while (0)
#endif


#ifdef PPC_DEBUG_TB
45
#  define LOG_TB(...) qemu_log(__VA_ARGS__)
46 47 48 49
#else
#  define LOG_TB(...) do { } while (0)
#endif

J
j_mayer 已提交
50 51 52
static void cpu_ppc_tb_stop (CPUState *env);
static void cpu_ppc_tb_start (CPUState *env);

J
j_mayer 已提交
53
static void ppc_set_irq (CPUState *env, int n_IRQ, int level)
54
{
55 56
    unsigned int old_pending = env->pending_interrupts;

57 58 59 60 61 62 63 64
    if (level) {
        env->pending_interrupts |= 1 << n_IRQ;
        cpu_interrupt(env, CPU_INTERRUPT_HARD);
    } else {
        env->pending_interrupts &= ~(1 << n_IRQ);
        if (env->pending_interrupts == 0)
            cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
    }
65 66 67 68 69 70 71

    if (old_pending != env->pending_interrupts) {
#ifdef CONFIG_KVM
        kvmppc_set_interrupt(env, n_IRQ, level);
#endif
    }

72
    LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32
73
                "req %08x\n", __func__, env, n_IRQ, level,
74
                env->pending_interrupts, env->interrupt_request);
75 76
}

77 78
/* PowerPC 6xx / 7xx internal IRQ controller */
static void ppc6xx_set_irq (void *opaque, int pin, int level)
P
pbrook 已提交
79
{
80 81
    CPUState *env = opaque;
    int cur_level;
P
pbrook 已提交
82

83
    LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
84
                env, pin, level);
85 86
    cur_level = (env->irq_input_state >> pin) & 1;
    /* Don't generate spurious events */
87
    if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
88
        switch (pin) {
J
j_mayer 已提交
89 90
        case PPC6xx_INPUT_TBEN:
            /* Level sensitive - active high */
91
            LOG_IRQ("%s: %s the time base\n",
J
j_mayer 已提交
92 93 94 95 96 97
                        __func__, level ? "start" : "stop");
            if (level) {
                cpu_ppc_tb_start(env);
            } else {
                cpu_ppc_tb_stop(env);
            }
98 99
        case PPC6xx_INPUT_INT:
            /* Level sensitive - active high */
100
            LOG_IRQ("%s: set the external IRQ state to %d\n",
101
                        __func__, level);
102 103
            ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
            break;
104
        case PPC6xx_INPUT_SMI:
105
            /* Level sensitive - active high */
106
            LOG_IRQ("%s: set the SMI IRQ state to %d\n",
107
                        __func__, level);
108 109
            ppc_set_irq(env, PPC_INTERRUPT_SMI, level);
            break;
110
        case PPC6xx_INPUT_MCP:
111 112 113 114 115
            /* Negative edge sensitive */
            /* XXX: TODO: actual reaction may depends on HID0 status
             *            603/604/740/750: check HID0[EMCP]
             */
            if (cur_level == 1 && level == 0) {
116
                LOG_IRQ("%s: raise machine check state\n",
117
                            __func__);
118 119 120
                ppc_set_irq(env, PPC_INTERRUPT_MCK, 1);
            }
            break;
121
        case PPC6xx_INPUT_CKSTP_IN:
122 123
            /* Level sensitive - active low */
            /* XXX: TODO: relay the signal to CKSTP_OUT pin */
124
            /* XXX: Note that the only way to restart the CPU is to reset it */
125
            if (level) {
126
                LOG_IRQ("%s: stop the CPU\n", __func__);
127 128 129
                env->halted = 1;
            }
            break;
130
        case PPC6xx_INPUT_HRESET:
131 132
            /* Level sensitive - active low */
            if (level) {
133
                LOG_IRQ("%s: reset the CPU\n", __func__);
134 135 136
                env->interrupt_request |= CPU_INTERRUPT_EXITTB;
                /* XXX: TOFIX */
#if 0
137
                cpu_reset(env);
138 139
#else
                qemu_system_reset_request();
140 141 142
#endif
            }
            break;
143
        case PPC6xx_INPUT_SRESET:
144
            LOG_IRQ("%s: set the RESET IRQ state to %d\n",
145
                        __func__, level);
146 147 148 149
            ppc_set_irq(env, PPC_INTERRUPT_RESET, level);
            break;
        default:
            /* Unknown pin - do nothing */
150
            LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
151 152 153 154 155 156
            return;
        }
        if (level)
            env->irq_input_state |= 1 << pin;
        else
            env->irq_input_state &= ~(1 << pin);
P
pbrook 已提交
157 158 159
    }
}

160
void ppc6xx_irq_init (CPUState *env)
161
{
162 163
    env->irq_inputs = (void **)qemu_allocate_irqs(&ppc6xx_set_irq, env,
                                                  PPC6xx_INPUT_NB);
164 165
}

J
j_mayer 已提交
166
#if defined(TARGET_PPC64)
167 168 169 170 171 172
/* PowerPC 970 internal IRQ controller */
static void ppc970_set_irq (void *opaque, int pin, int level)
{
    CPUState *env = opaque;
    int cur_level;

173
    LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
174 175 176 177 178 179 180
                env, pin, level);
    cur_level = (env->irq_input_state >> pin) & 1;
    /* Don't generate spurious events */
    if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
        switch (pin) {
        case PPC970_INPUT_INT:
            /* Level sensitive - active high */
181
            LOG_IRQ("%s: set the external IRQ state to %d\n",
182 183 184 185 186
                        __func__, level);
            ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
            break;
        case PPC970_INPUT_THINT:
            /* Level sensitive - active high */
187
            LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__,
188 189 190 191 192 193 194 195 196
                        level);
            ppc_set_irq(env, PPC_INTERRUPT_THERM, level);
            break;
        case PPC970_INPUT_MCP:
            /* Negative edge sensitive */
            /* XXX: TODO: actual reaction may depends on HID0 status
             *            603/604/740/750: check HID0[EMCP]
             */
            if (cur_level == 1 && level == 0) {
197
                LOG_IRQ("%s: raise machine check state\n",
198 199 200 201 202 203 204 205
                            __func__);
                ppc_set_irq(env, PPC_INTERRUPT_MCK, 1);
            }
            break;
        case PPC970_INPUT_CKSTP:
            /* Level sensitive - active low */
            /* XXX: TODO: relay the signal to CKSTP_OUT pin */
            if (level) {
206
                LOG_IRQ("%s: stop the CPU\n", __func__);
207 208
                env->halted = 1;
            } else {
209
                LOG_IRQ("%s: restart the CPU\n", __func__);
210 211 212 213 214 215 216
                env->halted = 0;
            }
            break;
        case PPC970_INPUT_HRESET:
            /* Level sensitive - active low */
            if (level) {
#if 0 // XXX: TOFIX
217
                LOG_IRQ("%s: reset the CPU\n", __func__);
218 219 220 221 222
                cpu_reset(env);
#endif
            }
            break;
        case PPC970_INPUT_SRESET:
223
            LOG_IRQ("%s: set the RESET IRQ state to %d\n",
224 225 226 227
                        __func__, level);
            ppc_set_irq(env, PPC_INTERRUPT_RESET, level);
            break;
        case PPC970_INPUT_TBEN:
228
            LOG_IRQ("%s: set the TBEN state to %d\n", __func__,
229 230 231 232 233
                        level);
            /* XXX: TODO */
            break;
        default:
            /* Unknown pin - do nothing */
234
            LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
235 236 237 238 239 240 241 242 243 244 245
            return;
        }
        if (level)
            env->irq_input_state |= 1 << pin;
        else
            env->irq_input_state &= ~(1 << pin);
    }
}

void ppc970_irq_init (CPUState *env)
{
246 247
    env->irq_inputs = (void **)qemu_allocate_irqs(&ppc970_set_irq, env,
                                                  PPC970_INPUT_NB);
248
}
J
j_mayer 已提交
249
#endif /* defined(TARGET_PPC64) */
250

251 252
/* PowerPC 40x internal IRQ controller */
static void ppc40x_set_irq (void *opaque, int pin, int level)
253 254 255 256
{
    CPUState *env = opaque;
    int cur_level;

257
    LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
258
                env, pin, level);
259 260 261 262
    cur_level = (env->irq_input_state >> pin) & 1;
    /* Don't generate spurious events */
    if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
        switch (pin) {
263
        case PPC40x_INPUT_RESET_SYS:
264
            if (level) {
265
                LOG_IRQ("%s: reset the PowerPC system\n",
266 267 268 269
                            __func__);
                ppc40x_system_reset(env);
            }
            break;
270
        case PPC40x_INPUT_RESET_CHIP:
271
            if (level) {
272
                LOG_IRQ("%s: reset the PowerPC chip\n", __func__);
273 274 275
                ppc40x_chip_reset(env);
            }
            break;
276
        case PPC40x_INPUT_RESET_CORE:
277 278
            /* XXX: TODO: update DBSR[MRR] */
            if (level) {
279
                LOG_IRQ("%s: reset the PowerPC core\n", __func__);
280
                ppc40x_core_reset(env);
281 282
            }
            break;
283
        case PPC40x_INPUT_CINT:
284
            /* Level sensitive - active high */
285
            LOG_IRQ("%s: set the critical IRQ state to %d\n",
286
                        __func__, level);
287
            ppc_set_irq(env, PPC_INTERRUPT_CEXT, level);
288
            break;
289
        case PPC40x_INPUT_INT:
290
            /* Level sensitive - active high */
291
            LOG_IRQ("%s: set the external IRQ state to %d\n",
292
                        __func__, level);
293 294
            ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
            break;
295
        case PPC40x_INPUT_HALT:
296 297
            /* Level sensitive - active low */
            if (level) {
298
                LOG_IRQ("%s: stop the CPU\n", __func__);
299 300
                env->halted = 1;
            } else {
301
                LOG_IRQ("%s: restart the CPU\n", __func__);
302 303 304
                env->halted = 0;
            }
            break;
305
        case PPC40x_INPUT_DEBUG:
306
            /* Level sensitive - active high */
307
            LOG_IRQ("%s: set the debug pin state to %d\n",
308
                        __func__, level);
309
            ppc_set_irq(env, PPC_INTERRUPT_DEBUG, level);
310 311 312
            break;
        default:
            /* Unknown pin - do nothing */
313
            LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
314 315 316 317 318 319 320 321 322
            return;
        }
        if (level)
            env->irq_input_state |= 1 << pin;
        else
            env->irq_input_state &= ~(1 << pin);
    }
}

323
void ppc40x_irq_init (CPUState *env)
324
{
325 326
    env->irq_inputs = (void **)qemu_allocate_irqs(&ppc40x_set_irq,
                                                  env, PPC40x_INPUT_NB);
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
/* PowerPC E500 internal IRQ controller */
static void ppce500_set_irq (void *opaque, int pin, int level)
{
    CPUState *env = opaque;
    int cur_level;

    LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
                env, pin, level);
    cur_level = (env->irq_input_state >> pin) & 1;
    /* Don't generate spurious events */
    if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
        switch (pin) {
        case PPCE500_INPUT_MCK:
            if (level) {
                LOG_IRQ("%s: reset the PowerPC system\n",
                            __func__);
                qemu_system_reset_request();
            }
            break;
        case PPCE500_INPUT_RESET_CORE:
            if (level) {
                LOG_IRQ("%s: reset the PowerPC core\n", __func__);
                ppc_set_irq(env, PPC_INTERRUPT_MCK, level);
            }
            break;
        case PPCE500_INPUT_CINT:
            /* Level sensitive - active high */
            LOG_IRQ("%s: set the critical IRQ state to %d\n",
                        __func__, level);
            ppc_set_irq(env, PPC_INTERRUPT_CEXT, level);
            break;
        case PPCE500_INPUT_INT:
            /* Level sensitive - active high */
            LOG_IRQ("%s: set the core IRQ state to %d\n",
                        __func__, level);
            ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
            break;
        case PPCE500_INPUT_DEBUG:
            /* Level sensitive - active high */
            LOG_IRQ("%s: set the debug pin state to %d\n",
                        __func__, level);
            ppc_set_irq(env, PPC_INTERRUPT_DEBUG, level);
            break;
        default:
            /* Unknown pin - do nothing */
            LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
            return;
        }
        if (level)
            env->irq_input_state |= 1 << pin;
        else
            env->irq_input_state &= ~(1 << pin);
    }
}

void ppce500_irq_init (CPUState *env)
{
    env->irq_inputs = (void **)qemu_allocate_irqs(&ppce500_set_irq,
                                        env, PPCE500_INPUT_NB);
}
389
/*****************************************************************************/
390
/* PowerPC time base and decrementer emulation */
A
Anthony Liguori 已提交
391
struct ppc_tb_t {
392
    /* Time base management */
J
j_mayer 已提交
393 394 395
    int64_t  tb_offset;    /* Compensation                    */
    int64_t  atb_offset;   /* Compensation                    */
    uint32_t tb_freq;      /* TB frequency                    */
396
    /* Decrementer management */
J
j_mayer 已提交
397 398
    uint64_t decr_next;    /* Tick for next decr interrupt    */
    uint32_t decr_freq;    /* decrementer frequency           */
399
    struct QEMUTimer *decr_timer;
400 401 402 403 404
    /* Hypervisor decrementer management */
    uint64_t hdecr_next;    /* Tick for next hdecr interrupt  */
    struct QEMUTimer *hdecr_timer;
    uint64_t purr_load;
    uint64_t purr_start;
405
    void *opaque;
406 407
};

A
Anthony Liguori 已提交
408
static inline uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk,
B
Blue Swirl 已提交
409
                                      int64_t tb_offset)
410 411
{
    /* TB time in tb periods */
412
    return muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec()) + tb_offset;
413 414
}

A
Alexander Graf 已提交
415
uint64_t cpu_ppc_load_tbl (CPUState *env)
416
{
A
Anthony Liguori 已提交
417
    ppc_tb_t *tb_env = env->tb_env;
418 419
    uint64_t tb;

J
j_mayer 已提交
420
    tb = cpu_ppc_get_tb(tb_env, qemu_get_clock(vm_clock), tb_env->tb_offset);
421
    LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
422

A
Alexander Graf 已提交
423
    return tb;
424 425
}

B
Blue Swirl 已提交
426
static inline uint32_t _cpu_ppc_load_tbu(CPUState *env)
427
{
A
Anthony Liguori 已提交
428
    ppc_tb_t *tb_env = env->tb_env;
429 430
    uint64_t tb;

J
j_mayer 已提交
431
    tb = cpu_ppc_get_tb(tb_env, qemu_get_clock(vm_clock), tb_env->tb_offset);
432
    LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
433

434 435 436
    return tb >> 32;
}

437 438 439 440 441
uint32_t cpu_ppc_load_tbu (CPUState *env)
{
    return _cpu_ppc_load_tbu(env);
}

A
Anthony Liguori 已提交
442
static inline void cpu_ppc_store_tb(ppc_tb_t *tb_env, uint64_t vmclk,
B
Blue Swirl 已提交
443
                                    int64_t *tb_offsetp, uint64_t value)
444
{
445
    *tb_offsetp = value - muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec());
446
    LOG_TB("%s: tb %016" PRIx64 " offset %08" PRIx64 "\n",
447
                __func__, value, *tb_offsetp);
448 449
}

450 451
void cpu_ppc_store_tbl (CPUState *env, uint32_t value)
{
A
Anthony Liguori 已提交
452
    ppc_tb_t *tb_env = env->tb_env;
453 454
    uint64_t tb;

J
j_mayer 已提交
455
    tb = cpu_ppc_get_tb(tb_env, qemu_get_clock(vm_clock), tb_env->tb_offset);
456
    tb &= 0xFFFFFFFF00000000ULL;
J
j_mayer 已提交
457 458
    cpu_ppc_store_tb(tb_env, qemu_get_clock(vm_clock),
                     &tb_env->tb_offset, tb | (uint64_t)value);
459 460
}

B
Blue Swirl 已提交
461
static inline void _cpu_ppc_store_tbu(CPUState *env, uint32_t value)
462
{
A
Anthony Liguori 已提交
463
    ppc_tb_t *tb_env = env->tb_env;
464
    uint64_t tb;
465

J
j_mayer 已提交
466
    tb = cpu_ppc_get_tb(tb_env, qemu_get_clock(vm_clock), tb_env->tb_offset);
467
    tb &= 0x00000000FFFFFFFFULL;
J
j_mayer 已提交
468 469
    cpu_ppc_store_tb(tb_env, qemu_get_clock(vm_clock),
                     &tb_env->tb_offset, ((uint64_t)value << 32) | tb);
470 471
}

472 473 474 475 476
void cpu_ppc_store_tbu (CPUState *env, uint32_t value)
{
    _cpu_ppc_store_tbu(env, value);
}

A
Aurelien Jarno 已提交
477
uint64_t cpu_ppc_load_atbl (CPUState *env)
478
{
A
Anthony Liguori 已提交
479
    ppc_tb_t *tb_env = env->tb_env;
480 481
    uint64_t tb;

J
j_mayer 已提交
482
    tb = cpu_ppc_get_tb(tb_env, qemu_get_clock(vm_clock), tb_env->atb_offset);
483
    LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
484

A
Aurelien Jarno 已提交
485
    return tb;
486 487 488 489
}

uint32_t cpu_ppc_load_atbu (CPUState *env)
{
A
Anthony Liguori 已提交
490
    ppc_tb_t *tb_env = env->tb_env;
491 492
    uint64_t tb;

J
j_mayer 已提交
493
    tb = cpu_ppc_get_tb(tb_env, qemu_get_clock(vm_clock), tb_env->atb_offset);
494
    LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
495 496 497 498 499 500

    return tb >> 32;
}

void cpu_ppc_store_atbl (CPUState *env, uint32_t value)
{
A
Anthony Liguori 已提交
501
    ppc_tb_t *tb_env = env->tb_env;
502 503
    uint64_t tb;

J
j_mayer 已提交
504
    tb = cpu_ppc_get_tb(tb_env, qemu_get_clock(vm_clock), tb_env->atb_offset);
505
    tb &= 0xFFFFFFFF00000000ULL;
J
j_mayer 已提交
506 507
    cpu_ppc_store_tb(tb_env, qemu_get_clock(vm_clock),
                     &tb_env->atb_offset, tb | (uint64_t)value);
508 509 510
}

void cpu_ppc_store_atbu (CPUState *env, uint32_t value)
511
{
A
Anthony Liguori 已提交
512
    ppc_tb_t *tb_env = env->tb_env;
513
    uint64_t tb;
514

J
j_mayer 已提交
515
    tb = cpu_ppc_get_tb(tb_env, qemu_get_clock(vm_clock), tb_env->atb_offset);
516
    tb &= 0x00000000FFFFFFFFULL;
J
j_mayer 已提交
517 518 519 520 521 522
    cpu_ppc_store_tb(tb_env, qemu_get_clock(vm_clock),
                     &tb_env->atb_offset, ((uint64_t)value << 32) | tb);
}

static void cpu_ppc_tb_stop (CPUState *env)
{
A
Anthony Liguori 已提交
523
    ppc_tb_t *tb_env = env->tb_env;
J
j_mayer 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
    uint64_t tb, atb, vmclk;

    /* If the time base is already frozen, do nothing */
    if (tb_env->tb_freq != 0) {
        vmclk = qemu_get_clock(vm_clock);
        /* Get the time base */
        tb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->tb_offset);
        /* Get the alternate time base */
        atb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->atb_offset);
        /* Store the time base value (ie compute the current offset) */
        cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb);
        /* Store the alternate time base value (compute the current offset) */
        cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb);
        /* Set the time base frequency to zero */
        tb_env->tb_freq = 0;
        /* Now, the time bases are frozen to tb_offset / atb_offset value */
    }
}

static void cpu_ppc_tb_start (CPUState *env)
{
A
Anthony Liguori 已提交
545
    ppc_tb_t *tb_env = env->tb_env;
J
j_mayer 已提交
546
    uint64_t tb, atb, vmclk;
547

J
j_mayer 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561
    /* If the time base is not frozen, do nothing */
    if (tb_env->tb_freq == 0) {
        vmclk = qemu_get_clock(vm_clock);
        /* Get the time base from tb_offset */
        tb = tb_env->tb_offset;
        /* Get the alternate time base from atb_offset */
        atb = tb_env->atb_offset;
        /* Restore the tb frequency from the decrementer frequency */
        tb_env->tb_freq = tb_env->decr_freq;
        /* Store the time base value */
        cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb);
        /* Store the alternate time base value */
        cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb);
    }
562 563
}

B
Blue Swirl 已提交
564
static inline uint32_t _cpu_ppc_load_decr(CPUState *env, uint64_t next)
565
{
A
Anthony Liguori 已提交
566
    ppc_tb_t *tb_env = env->tb_env;
567
    uint32_t decr;
B
bellard 已提交
568
    int64_t diff;
569

570
    diff = next - qemu_get_clock(vm_clock);
B
bellard 已提交
571
    if (diff >= 0)
572
        decr = muldiv64(diff, tb_env->decr_freq, get_ticks_per_sec());
B
bellard 已提交
573
    else
574
        decr = -muldiv64(-diff, tb_env->decr_freq, get_ticks_per_sec());
575
    LOG_TB("%s: %08" PRIx32 "\n", __func__, decr);
576

577 578 579
    return decr;
}

580 581
uint32_t cpu_ppc_load_decr (CPUState *env)
{
A
Anthony Liguori 已提交
582
    ppc_tb_t *tb_env = env->tb_env;
583

584
    return _cpu_ppc_load_decr(env, tb_env->decr_next);
585 586 587 588
}

uint32_t cpu_ppc_load_hdecr (CPUState *env)
{
A
Anthony Liguori 已提交
589
    ppc_tb_t *tb_env = env->tb_env;
590

591
    return _cpu_ppc_load_decr(env, tb_env->hdecr_next);
592 593 594 595
}

uint64_t cpu_ppc_load_purr (CPUState *env)
{
A
Anthony Liguori 已提交
596
    ppc_tb_t *tb_env = env->tb_env;
597 598 599
    uint64_t diff;

    diff = qemu_get_clock(vm_clock) - tb_env->purr_start;
J
j_mayer 已提交
600

601
    return tb_env->purr_load + muldiv64(diff, tb_env->tb_freq, get_ticks_per_sec());
602 603
}

604 605 606
/* When decrementer expires,
 * all we need to do is generate or queue a CPU exception
 */
B
Blue Swirl 已提交
607
static inline void cpu_ppc_decr_excp(CPUState *env)
608 609
{
    /* Raise it */
610
    LOG_TB("raise decrementer exception\n");
611
    ppc_set_irq(env, PPC_INTERRUPT_DECR, 1);
612 613
}

B
Blue Swirl 已提交
614
static inline void cpu_ppc_hdecr_excp(CPUState *env)
615 616
{
    /* Raise it */
617
    LOG_TB("raise decrementer exception\n");
618 619 620 621
    ppc_set_irq(env, PPC_INTERRUPT_HDECR, 1);
}

static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,
J
j_mayer 已提交
622 623 624 625
                                  struct QEMUTimer *timer,
                                  void (*raise_excp)(CPUState *),
                                  uint32_t decr, uint32_t value,
                                  int is_excp)
626
{
A
Anthony Liguori 已提交
627
    ppc_tb_t *tb_env = env->tb_env;
628 629
    uint64_t now, next;

630
    LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__,
631
                decr, value);
632
    now = qemu_get_clock(vm_clock);
633
    next = now + muldiv64(value, get_ticks_per_sec(), tb_env->decr_freq);
634
    if (is_excp)
635
        next += *nextp - now;
636
    if (next == now)
637
        next++;
638
    *nextp = next;
639
    /* Adjust timer */
640
    qemu_mod_timer(timer, next);
641 642 643 644
    /* If we set a negative value and the decrementer was positive,
     * raise an exception.
     */
    if ((value & 0x80000000) && !(decr & 0x80000000))
645 646 647
        (*raise_excp)(env);
}

B
Blue Swirl 已提交
648 649
static inline void _cpu_ppc_store_decr(CPUState *env, uint32_t decr,
                                       uint32_t value, int is_excp)
650
{
A
Anthony Liguori 已提交
651
    ppc_tb_t *tb_env = env->tb_env;
652 653 654

    __cpu_ppc_store_decr(env, &tb_env->decr_next, tb_env->decr_timer,
                         &cpu_ppc_decr_excp, decr, value, is_excp);
655 656 657 658 659 660 661 662 663 664 665 666
}

void cpu_ppc_store_decr (CPUState *env, uint32_t value)
{
    _cpu_ppc_store_decr(env, cpu_ppc_load_decr(env), value, 0);
}

static void cpu_ppc_decr_cb (void *opaque)
{
    _cpu_ppc_store_decr(opaque, 0x00000000, 0xFFFFFFFF, 1);
}

B
Blue Swirl 已提交
667 668
static inline void _cpu_ppc_store_hdecr(CPUState *env, uint32_t hdecr,
                                        uint32_t value, int is_excp)
669
{
A
Anthony Liguori 已提交
670
    ppc_tb_t *tb_env = env->tb_env;
671

672 673 674 675
    if (tb_env->hdecr_timer != NULL) {
        __cpu_ppc_store_decr(env, &tb_env->hdecr_next, tb_env->hdecr_timer,
                             &cpu_ppc_hdecr_excp, hdecr, value, is_excp);
    }
676 677 678 679 680 681 682 683 684 685 686 687 688 689
}

void cpu_ppc_store_hdecr (CPUState *env, uint32_t value)
{
    _cpu_ppc_store_hdecr(env, cpu_ppc_load_hdecr(env), value, 0);
}

static void cpu_ppc_hdecr_cb (void *opaque)
{
    _cpu_ppc_store_hdecr(opaque, 0x00000000, 0xFFFFFFFF, 1);
}

void cpu_ppc_store_purr (CPUState *env, uint64_t value)
{
A
Anthony Liguori 已提交
690
    ppc_tb_t *tb_env = env->tb_env;
691 692 693 694 695

    tb_env->purr_load = value;
    tb_env->purr_start = qemu_get_clock(vm_clock);
}

696 697 698
static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
{
    CPUState *env = opaque;
A
Anthony Liguori 已提交
699
    ppc_tb_t *tb_env = env->tb_env;
700 701

    tb_env->tb_freq = freq;
J
j_mayer 已提交
702
    tb_env->decr_freq = freq;
703 704 705 706 707
    /* There is a bug in Linux 2.4 kernels:
     * if a decrementer exception is pending when it enables msr_ee at startup,
     * it's not ready to handle it...
     */
    _cpu_ppc_store_decr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0);
708 709
    _cpu_ppc_store_hdecr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0);
    cpu_ppc_store_purr(env, 0x0000000000000000ULL);
710 711
}

712
/* Set up (once) timebase frequency (in Hz) */
713
clk_setup_cb cpu_ppc_tb_init (CPUState *env, uint32_t freq)
714
{
A
Anthony Liguori 已提交
715
    ppc_tb_t *tb_env;
716

A
Anthony Liguori 已提交
717
    tb_env = qemu_mallocz(sizeof(ppc_tb_t));
718
    env->tb_env = tb_env;
719 720
    /* Create new timer */
    tb_env->decr_timer = qemu_new_timer(vm_clock, &cpu_ppc_decr_cb, env);
721 722 723 724 725 726 727
    if (0) {
        /* XXX: find a suitable condition to enable the hypervisor decrementer
         */
        tb_env->hdecr_timer = qemu_new_timer(vm_clock, &cpu_ppc_hdecr_cb, env);
    } else {
        tb_env->hdecr_timer = NULL;
    }
728
    cpu_ppc_set_tb_clk(env, freq);
729

730
    return &cpu_ppc_set_tb_clk;
731 732
}

733
/* Specific helpers for POWER & PowerPC 601 RTC */
734 735
#if 0
static clk_setup_cb cpu_ppc601_rtc_init (CPUState *env)
736 737 738
{
    return cpu_ppc_tb_init(env, 7812500);
}
739
#endif
740 741

void cpu_ppc601_store_rtcu (CPUState *env, uint32_t value)
742 743 744
{
    _cpu_ppc_store_tbu(env, value);
}
745 746

uint32_t cpu_ppc601_load_rtcu (CPUState *env)
747 748 749
{
    return _cpu_ppc_load_tbu(env);
}
750 751 752 753 754 755 756 757 758 759 760

void cpu_ppc601_store_rtcl (CPUState *env, uint32_t value)
{
    cpu_ppc_store_tbl(env, value & 0x3FFFFF80);
}

uint32_t cpu_ppc601_load_rtcl (CPUState *env)
{
    return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
}

J
j_mayer 已提交
761
/*****************************************************************************/
762
/* Embedded PowerPC timers */
J
j_mayer 已提交
763 764

/* PIT, FIT & WDT */
A
Anthony Liguori 已提交
765 766
typedef struct ppcemb_timer_t ppcemb_timer_t;
struct ppcemb_timer_t {
J
j_mayer 已提交
767 768 769 770 771 772
    uint64_t pit_reload;  /* PIT auto-reload value        */
    uint64_t fit_next;    /* Tick for next FIT interrupt  */
    struct QEMUTimer *fit_timer;
    uint64_t wdt_next;    /* Tick for next WDT interrupt  */
    struct QEMUTimer *wdt_timer;
};
773

J
j_mayer 已提交
774 775 776 777
/* Fixed interval timer */
static void cpu_4xx_fit_cb (void *opaque)
{
    CPUState *env;
A
Anthony Liguori 已提交
778 779
    ppc_tb_t *tb_env;
    ppcemb_timer_t *ppcemb_timer;
J
j_mayer 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
    uint64_t now, next;

    env = opaque;
    tb_env = env->tb_env;
    ppcemb_timer = tb_env->opaque;
    now = qemu_get_clock(vm_clock);
    switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) {
    case 0:
        next = 1 << 9;
        break;
    case 1:
        next = 1 << 13;
        break;
    case 2:
        next = 1 << 17;
        break;
    case 3:
        next = 1 << 21;
        break;
    default:
        /* Cannot occur, but makes gcc happy */
        return;
    }
803
    next = now + muldiv64(next, get_ticks_per_sec(), tb_env->tb_freq);
J
j_mayer 已提交
804 805 806 807 808 809
    if (next == now)
        next++;
    qemu_mod_timer(ppcemb_timer->fit_timer, next);
    env->spr[SPR_40x_TSR] |= 1 << 26;
    if ((env->spr[SPR_40x_TCR] >> 23) & 0x1)
        ppc_set_irq(env, PPC_INTERRUPT_FIT, 1);
810 811 812
    LOG_TB("%s: ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__,
           (int)((env->spr[SPR_40x_TCR] >> 23) & 0x1),
           env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
J
j_mayer 已提交
813 814 815
}

/* Programmable interval timer */
A
Anthony Liguori 已提交
816
static void start_stop_pit (CPUState *env, ppc_tb_t *tb_env, int is_excp)
817
{
A
Anthony Liguori 已提交
818
    ppcemb_timer_t *ppcemb_timer;
J
j_mayer 已提交
819 820 821
    uint64_t now, next;

    ppcemb_timer = tb_env->opaque;
J
j_mayer 已提交
822 823 824 825
    if (ppcemb_timer->pit_reload <= 1 ||
        !((env->spr[SPR_40x_TCR] >> 26) & 0x1) ||
        (is_excp && !((env->spr[SPR_40x_TCR] >> 22) & 0x1))) {
        /* Stop PIT */
826
        LOG_TB("%s: stop PIT\n", __func__);
J
j_mayer 已提交
827 828
        qemu_del_timer(tb_env->decr_timer);
    } else {
829
        LOG_TB("%s: start PIT %016" PRIx64 "\n",
J
j_mayer 已提交
830 831
                    __func__, ppcemb_timer->pit_reload);
        now = qemu_get_clock(vm_clock);
J
j_mayer 已提交
832
        next = now + muldiv64(ppcemb_timer->pit_reload,
833
                              get_ticks_per_sec(), tb_env->decr_freq);
J
j_mayer 已提交
834 835
        if (is_excp)
            next += tb_env->decr_next - now;
J
j_mayer 已提交
836 837 838 839 840
        if (next == now)
            next++;
        qemu_mod_timer(tb_env->decr_timer, next);
        tb_env->decr_next = next;
    }
J
j_mayer 已提交
841 842 843 844 845
}

static void cpu_4xx_pit_cb (void *opaque)
{
    CPUState *env;
A
Anthony Liguori 已提交
846 847
    ppc_tb_t *tb_env;
    ppcemb_timer_t *ppcemb_timer;
J
j_mayer 已提交
848 849 850 851

    env = opaque;
    tb_env = env->tb_env;
    ppcemb_timer = tb_env->opaque;
J
j_mayer 已提交
852 853 854
    env->spr[SPR_40x_TSR] |= 1 << 27;
    if ((env->spr[SPR_40x_TCR] >> 26) & 0x1)
        ppc_set_irq(env, PPC_INTERRUPT_PIT, 1);
J
j_mayer 已提交
855
    start_stop_pit(env, tb_env, 1);
856 857 858 859 860 861
    LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx " "
           "%016" PRIx64 "\n", __func__,
           (int)((env->spr[SPR_40x_TCR] >> 22) & 0x1),
           (int)((env->spr[SPR_40x_TCR] >> 26) & 0x1),
           env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR],
           ppcemb_timer->pit_reload);
J
j_mayer 已提交
862 863 864 865 866 867
}

/* Watchdog timer */
static void cpu_4xx_wdt_cb (void *opaque)
{
    CPUState *env;
A
Anthony Liguori 已提交
868 869
    ppc_tb_t *tb_env;
    ppcemb_timer_t *ppcemb_timer;
J
j_mayer 已提交
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
    uint64_t now, next;

    env = opaque;
    tb_env = env->tb_env;
    ppcemb_timer = tb_env->opaque;
    now = qemu_get_clock(vm_clock);
    switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) {
    case 0:
        next = 1 << 17;
        break;
    case 1:
        next = 1 << 21;
        break;
    case 2:
        next = 1 << 25;
        break;
    case 3:
        next = 1 << 29;
        break;
    default:
        /* Cannot occur, but makes gcc happy */
        return;
    }
893
    next = now + muldiv64(next, get_ticks_per_sec(), tb_env->decr_freq);
J
j_mayer 已提交
894 895
    if (next == now)
        next++;
896 897
    LOG_TB("%s: TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__,
           env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
J
j_mayer 已提交
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
    switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) {
    case 0x0:
    case 0x1:
        qemu_mod_timer(ppcemb_timer->wdt_timer, next);
        ppcemb_timer->wdt_next = next;
        env->spr[SPR_40x_TSR] |= 1 << 31;
        break;
    case 0x2:
        qemu_mod_timer(ppcemb_timer->wdt_timer, next);
        ppcemb_timer->wdt_next = next;
        env->spr[SPR_40x_TSR] |= 1 << 30;
        if ((env->spr[SPR_40x_TCR] >> 27) & 0x1)
            ppc_set_irq(env, PPC_INTERRUPT_WDT, 1);
        break;
    case 0x3:
        env->spr[SPR_40x_TSR] &= ~0x30000000;
        env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000;
        switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) {
        case 0x0:
            /* No reset */
            break;
        case 0x1: /* Core reset */
920 921
            ppc40x_core_reset(env);
            break;
J
j_mayer 已提交
922
        case 0x2: /* Chip reset */
923 924
            ppc40x_chip_reset(env);
            break;
J
j_mayer 已提交
925
        case 0x3: /* System reset */
926 927
            ppc40x_system_reset(env);
            break;
J
j_mayer 已提交
928 929
        }
    }
930 931 932 933
}

void store_40x_pit (CPUState *env, target_ulong val)
{
A
Anthony Liguori 已提交
934 935
    ppc_tb_t *tb_env;
    ppcemb_timer_t *ppcemb_timer;
J
j_mayer 已提交
936 937 938

    tb_env = env->tb_env;
    ppcemb_timer = tb_env->opaque;
939
    LOG_TB("%s val" TARGET_FMT_lx "\n", __func__, val);
J
j_mayer 已提交
940
    ppcemb_timer->pit_reload = val;
J
j_mayer 已提交
941
    start_stop_pit(env, tb_env, 0);
942 943
}

J
j_mayer 已提交
944
target_ulong load_40x_pit (CPUState *env)
945
{
J
j_mayer 已提交
946
    return cpu_ppc_load_decr(env);
947 948 949 950
}

void store_booke_tsr (CPUState *env, target_ulong val)
{
951
    LOG_TB("%s: val " TARGET_FMT_lx "\n", __func__, val);
J
j_mayer 已提交
952 953 954
    env->spr[SPR_40x_TSR] &= ~(val & 0xFC000000);
    if (val & 0x80000000)
        ppc_set_irq(env, PPC_INTERRUPT_PIT, 0);
J
j_mayer 已提交
955 956 957 958
}

void store_booke_tcr (CPUState *env, target_ulong val)
{
A
Anthony Liguori 已提交
959
    ppc_tb_t *tb_env;
J
j_mayer 已提交
960 961

    tb_env = env->tb_env;
962
    LOG_TB("%s: val " TARGET_FMT_lx "\n", __func__, val);
J
j_mayer 已提交
963 964
    env->spr[SPR_40x_TCR] = val & 0xFFC00000;
    start_stop_pit(env, tb_env, 1);
965
    cpu_4xx_wdt_cb(env);
J
j_mayer 已提交
966 967
}

J
j_mayer 已提交
968 969 970
static void ppc_emb_set_tb_clk (void *opaque, uint32_t freq)
{
    CPUState *env = opaque;
A
Anthony Liguori 已提交
971
    ppc_tb_t *tb_env = env->tb_env;
J
j_mayer 已提交
972

973
    LOG_TB("%s set new frequency to %" PRIu32 "\n", __func__,
974
                freq);
J
j_mayer 已提交
975
    tb_env->tb_freq = freq;
J
j_mayer 已提交
976
    tb_env->decr_freq = freq;
J
j_mayer 已提交
977 978 979
    /* XXX: we should also update all timers */
}

980
clk_setup_cb ppc_emb_timers_init (CPUState *env, uint32_t freq)
J
j_mayer 已提交
981
{
A
Anthony Liguori 已提交
982 983
    ppc_tb_t *tb_env;
    ppcemb_timer_t *ppcemb_timer;
J
j_mayer 已提交
984

A
Anthony Liguori 已提交
985
    tb_env = qemu_mallocz(sizeof(ppc_tb_t));
986
    env->tb_env = tb_env;
A
Anthony Liguori 已提交
987
    ppcemb_timer = qemu_mallocz(sizeof(ppcemb_timer_t));
988
    tb_env->tb_freq = freq;
J
j_mayer 已提交
989
    tb_env->decr_freq = freq;
J
j_mayer 已提交
990
    tb_env->opaque = ppcemb_timer;
991
    LOG_TB("%s freq %" PRIu32 "\n", __func__, freq);
J
j_mayer 已提交
992 993 994 995 996 997 998 999
    if (ppcemb_timer != NULL) {
        /* We use decr timer for PIT */
        tb_env->decr_timer = qemu_new_timer(vm_clock, &cpu_4xx_pit_cb, env);
        ppcemb_timer->fit_timer =
            qemu_new_timer(vm_clock, &cpu_4xx_fit_cb, env);
        ppcemb_timer->wdt_timer =
            qemu_new_timer(vm_clock, &cpu_4xx_wdt_cb, env);
    }
1000

J
j_mayer 已提交
1001
    return &ppc_emb_set_tb_clk;
1002 1003
}

1004 1005
/*****************************************************************************/
/* Embedded PowerPC Device Control Registers */
A
Anthony Liguori 已提交
1006 1007
typedef struct ppc_dcrn_t ppc_dcrn_t;
struct ppc_dcrn_t {
1008 1009 1010 1011 1012
    dcr_read_cb dcr_read;
    dcr_write_cb dcr_write;
    void *opaque;
};

1013 1014 1015
/* XXX: on 460, DCR addresses are 32 bits wide,
 *      using DCRIPR to get the 22 upper bits of the DCR address
 */
1016
#define DCRN_NB 1024
A
Anthony Liguori 已提交
1017 1018
struct ppc_dcr_t {
    ppc_dcrn_t dcrn[DCRN_NB];
1019 1020 1021 1022
    int (*read_error)(int dcrn);
    int (*write_error)(int dcrn);
};

A
Alexander Graf 已提交
1023
int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp)
1024
{
A
Anthony Liguori 已提交
1025
    ppc_dcrn_t *dcr;
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

    if (dcrn < 0 || dcrn >= DCRN_NB)
        goto error;
    dcr = &dcr_env->dcrn[dcrn];
    if (dcr->dcr_read == NULL)
        goto error;
    *valp = (*dcr->dcr_read)(dcr->opaque, dcrn);

    return 0;

 error:
    if (dcr_env->read_error != NULL)
        return (*dcr_env->read_error)(dcrn);

    return -1;
}

A
Alexander Graf 已提交
1043
int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val)
1044
{
A
Anthony Liguori 已提交
1045
    ppc_dcrn_t *dcr;
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

    if (dcrn < 0 || dcrn >= DCRN_NB)
        goto error;
    dcr = &dcr_env->dcrn[dcrn];
    if (dcr->dcr_write == NULL)
        goto error;
    (*dcr->dcr_write)(dcr->opaque, dcrn, val);

    return 0;

 error:
    if (dcr_env->write_error != NULL)
        return (*dcr_env->write_error)(dcrn);

    return -1;
}

int ppc_dcr_register (CPUState *env, int dcrn, void *opaque,
                      dcr_read_cb dcr_read, dcr_write_cb dcr_write)
{
A
Anthony Liguori 已提交
1066 1067
    ppc_dcr_t *dcr_env;
    ppc_dcrn_t *dcr;
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088

    dcr_env = env->dcr_env;
    if (dcr_env == NULL)
        return -1;
    if (dcrn < 0 || dcrn >= DCRN_NB)
        return -1;
    dcr = &dcr_env->dcrn[dcrn];
    if (dcr->opaque != NULL ||
        dcr->dcr_read != NULL ||
        dcr->dcr_write != NULL)
        return -1;
    dcr->opaque = opaque;
    dcr->dcr_read = dcr_read;
    dcr->dcr_write = dcr_write;

    return 0;
}

int ppc_dcr_init (CPUState *env, int (*read_error)(int dcrn),
                  int (*write_error)(int dcrn))
{
A
Anthony Liguori 已提交
1089
    ppc_dcr_t *dcr_env;
1090

A
Anthony Liguori 已提交
1091
    dcr_env = qemu_mallocz(sizeof(ppc_dcr_t));
1092 1093 1094 1095 1096 1097 1098
    dcr_env->read_error = read_error;
    dcr_env->write_error = write_error;
    env->dcr_env = dcr_env;

    return 0;
}

B
bellard 已提交
1099 1100
/*****************************************************************************/
/* Debug port */
B
bellard 已提交
1101
void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val)
B
bellard 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
{
    addr &= 0xF;
    switch (addr) {
    case 0:
        printf("%c", val);
        break;
    case 1:
        printf("\n");
        fflush(stdout);
        break;
    case 2:
1113
        printf("Set loglevel to %04" PRIx32 "\n", val);
B
bellard 已提交
1114
        cpu_set_log(val | 0x100);
B
bellard 已提交
1115 1116 1117 1118 1119 1120
        break;
    }
}

/*****************************************************************************/
/* NVRAM helpers */
A
Anthony Liguori 已提交
1121
static inline uint32_t nvram_read (nvram_t *nvram, uint32_t addr)
B
bellard 已提交
1122
{
J
j_mayer 已提交
1123
    return (*nvram->read_fn)(nvram->opaque, addr);;
B
bellard 已提交
1124 1125
}

A
Anthony Liguori 已提交
1126
static inline void nvram_write (nvram_t *nvram, uint32_t addr, uint32_t val)
B
bellard 已提交
1127
{
J
j_mayer 已提交
1128
    (*nvram->write_fn)(nvram->opaque, addr, val);
B
bellard 已提交
1129 1130
}

A
Anthony Liguori 已提交
1131
void NVRAM_set_byte (nvram_t *nvram, uint32_t addr, uint8_t value)
B
bellard 已提交
1132
{
J
j_mayer 已提交
1133
    nvram_write(nvram, addr, value);
B
bellard 已提交
1134 1135
}

A
Anthony Liguori 已提交
1136
uint8_t NVRAM_get_byte (nvram_t *nvram, uint32_t addr)
J
j_mayer 已提交
1137 1138 1139 1140
{
    return nvram_read(nvram, addr);
}

A
Anthony Liguori 已提交
1141
void NVRAM_set_word (nvram_t *nvram, uint32_t addr, uint16_t value)
J
j_mayer 已提交
1142 1143 1144 1145 1146
{
    nvram_write(nvram, addr, value >> 8);
    nvram_write(nvram, addr + 1, value & 0xFF);
}

A
Anthony Liguori 已提交
1147
uint16_t NVRAM_get_word (nvram_t *nvram, uint32_t addr)
B
bellard 已提交
1148 1149 1150
{
    uint16_t tmp;

J
j_mayer 已提交
1151 1152 1153
    tmp = nvram_read(nvram, addr) << 8;
    tmp |= nvram_read(nvram, addr + 1);

B
bellard 已提交
1154 1155 1156
    return tmp;
}

A
Anthony Liguori 已提交
1157
void NVRAM_set_lword (nvram_t *nvram, uint32_t addr, uint32_t value)
B
bellard 已提交
1158
{
J
j_mayer 已提交
1159 1160 1161 1162
    nvram_write(nvram, addr, value >> 24);
    nvram_write(nvram, addr + 1, (value >> 16) & 0xFF);
    nvram_write(nvram, addr + 2, (value >> 8) & 0xFF);
    nvram_write(nvram, addr + 3, value & 0xFF);
B
bellard 已提交
1163 1164
}

A
Anthony Liguori 已提交
1165
uint32_t NVRAM_get_lword (nvram_t *nvram, uint32_t addr)
B
bellard 已提交
1166 1167 1168
{
    uint32_t tmp;

J
j_mayer 已提交
1169 1170 1171 1172
    tmp = nvram_read(nvram, addr) << 24;
    tmp |= nvram_read(nvram, addr + 1) << 16;
    tmp |= nvram_read(nvram, addr + 2) << 8;
    tmp |= nvram_read(nvram, addr + 3);
1173

B
bellard 已提交
1174 1175 1176
    return tmp;
}

A
Anthony Liguori 已提交
1177
void NVRAM_set_string (nvram_t *nvram, uint32_t addr,
1178
                       const char *str, uint32_t max)
B
bellard 已提交
1179 1180 1181 1182
{
    int i;

    for (i = 0; i < max && str[i] != '\0'; i++) {
J
j_mayer 已提交
1183
        nvram_write(nvram, addr + i, str[i]);
B
bellard 已提交
1184
    }
J
j_mayer 已提交
1185 1186
    nvram_write(nvram, addr + i, str[i]);
    nvram_write(nvram, addr + max - 1, '\0');
B
bellard 已提交
1187 1188
}

A
Anthony Liguori 已提交
1189
int NVRAM_get_string (nvram_t *nvram, uint8_t *dst, uint16_t addr, int max)
B
bellard 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
{
    int i;

    memset(dst, 0, max);
    for (i = 0; i < max; i++) {
        dst[i] = NVRAM_get_byte(nvram, addr + i);
        if (dst[i] == '\0')
            break;
    }

    return i;
}

static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value)
{
    uint16_t tmp;
    uint16_t pd, pd1, pd2;

    tmp = prev >> 8;
    pd = prev ^ value;
    pd1 = pd & 0x000F;
    pd2 = ((pd >> 4) & 0x000F) ^ pd1;
    tmp ^= (pd1 << 3) | (pd1 << 8);
    tmp ^= pd2 | (pd2 << 7) | (pd2 << 12);

    return tmp;
}

A
Anthony Liguori 已提交
1218
static uint16_t NVRAM_compute_crc (nvram_t *nvram, uint32_t start, uint32_t count)
B
bellard 已提交
1219 1220 1221 1222 1223 1224 1225 1226
{
    uint32_t i;
    uint16_t crc = 0xFFFF;
    int odd;

    odd = count & 1;
    count &= ~1;
    for (i = 0; i != count; i++) {
1227
        crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i));
B
bellard 已提交
1228 1229
    }
    if (odd) {
1230
        crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8);
B
bellard 已提交
1231 1232 1233 1234 1235
    }

    return crc;
}

B
bellard 已提交
1236 1237
#define CMDLINE_ADDR 0x017ff000

A
Anthony Liguori 已提交
1238
int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size,
1239
                          const char *arch,
B
bellard 已提交
1240 1241
                          uint32_t RAM_size, int boot_device,
                          uint32_t kernel_image, uint32_t kernel_size,
B
bellard 已提交
1242
                          const char *cmdline,
B
bellard 已提交
1243
                          uint32_t initrd_image, uint32_t initrd_size,
B
bellard 已提交
1244 1245
                          uint32_t NVRAM_image,
                          int width, int height, int depth)
B
bellard 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
{
    uint16_t crc;

    /* Set parameters for Open Hack'Ware BIOS */
    NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16);
    NVRAM_set_lword(nvram,  0x10, 0x00000002); /* structure v2 */
    NVRAM_set_word(nvram,   0x14, NVRAM_size);
    NVRAM_set_string(nvram, 0x20, arch, 16);
    NVRAM_set_lword(nvram,  0x30, RAM_size);
    NVRAM_set_byte(nvram,   0x34, boot_device);
    NVRAM_set_lword(nvram,  0x38, kernel_image);
    NVRAM_set_lword(nvram,  0x3C, kernel_size);
B
bellard 已提交
1258 1259
    if (cmdline) {
        /* XXX: put the cmdline in NVRAM too ? */
1260
        pstrcpy_targphys("cmdline", CMDLINE_ADDR, RAM_size - CMDLINE_ADDR, cmdline);
B
bellard 已提交
1261 1262 1263 1264 1265 1266
        NVRAM_set_lword(nvram,  0x40, CMDLINE_ADDR);
        NVRAM_set_lword(nvram,  0x44, strlen(cmdline));
    } else {
        NVRAM_set_lword(nvram,  0x40, 0);
        NVRAM_set_lword(nvram,  0x44, 0);
    }
B
bellard 已提交
1267 1268 1269
    NVRAM_set_lword(nvram,  0x48, initrd_image);
    NVRAM_set_lword(nvram,  0x4C, initrd_size);
    NVRAM_set_lword(nvram,  0x50, NVRAM_image);
B
bellard 已提交
1270 1271 1272 1273 1274

    NVRAM_set_word(nvram,   0x54, width);
    NVRAM_set_word(nvram,   0x56, height);
    NVRAM_set_word(nvram,   0x58, depth);
    crc = NVRAM_compute_crc(nvram, 0x00, 0xF8);
J
j_mayer 已提交
1275
    NVRAM_set_word(nvram,   0xFC, crc);
B
bellard 已提交
1276 1277

    return 0;
1278
}