Statistics
| Branch: | Revision:

root / hw / hpet.c @ 16b29ae1

History | View | Annotate | Download (18.5 kB)

1
/*
2
 *  High Precisition Event Timer emulation
3
 *
4
 *  Copyright (c) 2007 Alexander Graf
5
 *  Copyright (c) 2008 IBM Corporation
6
 *
7
 *  Authors: Beth Kon <bkon@us.ibm.com>
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2 of the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with this library; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 *
23
 * *****************************************************************
24
 *
25
 * This driver attempts to emulate an HPET device in software.
26
 */
27

    
28
#include "hw.h"
29
#include "console.h"
30
#include "qemu-timer.h"
31
#include "hpet_emul.h"
32

    
33
extern void hpet_pit_disable(void);
34
extern void hpet_pit_enable(void);
35

    
36
//#define HPET_DEBUG
37
#ifdef HPET_DEBUG
38
#define dprintf printf
39
#else
40
#define dprintf(...)
41
#endif
42

    
43
static HPETState *hpet_statep;
44

    
45
uint32_t hpet_in_legacy_mode(void)
46
{
47
    if (hpet_statep)
48
        return hpet_statep->config & HPET_CFG_LEGACY;
49
    else
50
        return 0;
51
}
52

    
53
static uint32_t timer_int_route(struct HPETTimer *timer)   
54
{
55
    uint32_t route;
56
    route = (timer->config & HPET_TN_INT_ROUTE_MASK) >> HPET_TN_INT_ROUTE_SHIFT;
57
    return route;
58
}
59

    
60
static uint32_t hpet_enabled(void)
61
{
62
    return hpet_statep->config & HPET_CFG_ENABLE;
63
}
64

    
65
static uint32_t timer_is_periodic(HPETTimer *t)
66
{
67
    return t->config & HPET_TN_PERIODIC;
68
}
69

    
70
static uint32_t timer_enabled(HPETTimer *t)
71
{
72
    return t->config & HPET_TN_ENABLE;
73
}
74

    
75
static uint32_t hpet_time_after(uint64_t a, uint64_t b)
76
{
77
    return ((int32_t)(b) - (int32_t)(a) < 0);
78
}
79

    
80
static uint32_t hpet_time_after64(uint64_t a, uint64_t b)
81
{
82
    return ((int64_t)(b) - (int64_t)(a) < 0);
83
}
84

    
85
static uint64_t ticks_to_ns(uint64_t value) 
86
{
87
    return (muldiv64(value, HPET_CLK_PERIOD, FS_PER_NS));
88
}
89

    
90
static uint64_t ns_to_ticks(uint64_t value) 
91
{
92
    return (muldiv64(value, FS_PER_NS, HPET_CLK_PERIOD));
93
}
94

    
95
static uint64_t hpet_fixup_reg(uint64_t new, uint64_t old, uint64_t mask)
96
{
97
    new &= mask;
98
    new |= old & ~mask;
99
    return new;
100
}
101

    
102
static int activating_bit(uint64_t old, uint64_t new, uint64_t mask)
103
{
104
    return (!(old & mask) && (new & mask)); 
105
}
106

    
107
static int deactivating_bit(uint64_t old, uint64_t new, uint64_t mask)
108
{
109
    return ((old & mask) && !(new & mask)); 
110
}
111

    
112
static uint64_t hpet_get_ticks(void) 
113
{
114
    uint64_t ticks;
115
    ticks = ns_to_ticks(qemu_get_clock(vm_clock) + hpet_statep->hpet_offset);
116
    return ticks;
117
}
118

    
119
/* 
120
 * calculate diff between comparator value and current ticks  
121
 */
122
static inline uint64_t hpet_calculate_diff(HPETTimer *t, uint64_t current)
123
{
124
    
125
    if (t->config & HPET_TN_32BIT) {
126
        uint32_t diff, cmp;
127
        cmp = (uint32_t)t->cmp;
128
        diff = cmp - (uint32_t)current;
129
        diff = (int32_t)diff > 0 ? diff : (uint32_t)0;
130
        return (uint64_t)diff;
131
    } else {
132
        uint64_t diff, cmp;
133
        cmp = t->cmp;
134
        diff = cmp - current;
135
        diff = (int64_t)diff > 0 ? diff : (uint64_t)0;
136
        return diff;
137
    }
138
}
139

    
140
static void update_irq(struct HPETTimer *timer)
141
{
142
    qemu_irq irq;
143
    int route;
144

    
145
    if (timer->tn <= 1 && hpet_in_legacy_mode()) {
146
        /* if LegacyReplacementRoute bit is set, HPET specification requires
147
         * timer0 be routed to IRQ0 in NON-APIC or IRQ2 in the I/O APIC,
148
         * timer1 be routed to IRQ8 in NON-APIC or IRQ8 in the I/O APIC. 
149
         */
150
        if (timer->tn == 0) {
151
            irq=timer->state->irqs[0];
152
        } else
153
            irq=timer->state->irqs[8];
154
    } else {
155
        route=timer_int_route(timer);
156
        irq=timer->state->irqs[route];
157
    }
158
    if (timer_enabled(timer) && hpet_enabled()) {
159
        qemu_irq_pulse(irq);
160
    }
161
}
162

    
163
static void hpet_save(QEMUFile *f, void *opaque)
164
{
165
    HPETState *s = opaque;
166
    int i;
167
    qemu_put_be64s(f, &s->config);
168
    qemu_put_be64s(f, &s->isr);
169
    /* save current counter value */
170
    s->hpet_counter = hpet_get_ticks(); 
171
    qemu_put_be64s(f, &s->hpet_counter);
172

    
173
    for (i = 0; i < HPET_NUM_TIMERS; i++) {
174
        qemu_put_8s(f, &s->timer[i].tn);
175
        qemu_put_be64s(f, &s->timer[i].config);
176
        qemu_put_be64s(f, &s->timer[i].cmp);
177
        qemu_put_be64s(f, &s->timer[i].fsb);
178
        qemu_put_be64s(f, &s->timer[i].period);
179
        qemu_put_8s(f, &s->timer[i].wrap_flag);
180
        if (s->timer[i].qemu_timer) {
181
            qemu_put_timer(f, s->timer[i].qemu_timer);
182
        }
183
    }
184
}
185

    
186
static int hpet_load(QEMUFile *f, void *opaque, int version_id)
187
{
188
    HPETState *s = opaque;
189
    int i;
190
 
191
    if (version_id != 1)
192
        return -EINVAL;
193

    
194
    qemu_get_be64s(f, &s->config);
195
    qemu_get_be64s(f, &s->isr);
196
    qemu_get_be64s(f, &s->hpet_counter);
197
    /* Recalculate the offset between the main counter and guest time */
198
    s->hpet_offset = ticks_to_ns(s->hpet_counter) - qemu_get_clock(vm_clock);
199

    
200
    for (i = 0; i < HPET_NUM_TIMERS; i++) {
201
        qemu_get_8s(f, &s->timer[i].tn);
202
        qemu_get_be64s(f, &s->timer[i].config);
203
        qemu_get_be64s(f, &s->timer[i].cmp);
204
        qemu_get_be64s(f, &s->timer[i].fsb);
205
        qemu_get_be64s(f, &s->timer[i].period);
206
        qemu_get_8s(f, &s->timer[i].wrap_flag);
207
        if (s->timer[i].qemu_timer) {
208
            qemu_get_timer(f, s->timer[i].qemu_timer);
209
        }
210
    }
211
    return 0;
212
}
213

    
214
/* 
215
 * timer expiration callback
216
 */
217
static void hpet_timer(void *opaque)
218
{
219
    HPETTimer *t = (HPETTimer*)opaque;
220
    uint64_t diff;
221

    
222
    uint64_t period = t->period;
223
    uint64_t cur_tick = hpet_get_ticks();
224

    
225
    if (timer_is_periodic(t) && period != 0) {
226
        if (t->config & HPET_TN_32BIT) {
227
            while (hpet_time_after(cur_tick, t->cmp))
228
                t->cmp = (uint32_t)(t->cmp + t->period);
229
        } else
230
            while (hpet_time_after64(cur_tick, t->cmp))
231
                t->cmp += period;
232

    
233
        diff = hpet_calculate_diff(t, cur_tick);
234
        qemu_mod_timer(t->qemu_timer, qemu_get_clock(vm_clock) 
235
                       + (int64_t)ticks_to_ns(diff));
236
    } else if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
237
        if (t->wrap_flag) {
238
            diff = hpet_calculate_diff(t, cur_tick);
239
            qemu_mod_timer(t->qemu_timer, qemu_get_clock(vm_clock) 
240
                           + (int64_t)ticks_to_ns(diff));
241
            t->wrap_flag = 0;
242
        }
243
    }
244
    update_irq(t);
245
}
246

    
247
static void hpet_set_timer(HPETTimer *t)
248
{
249
    uint64_t diff;
250
    uint32_t wrap_diff;  /* how many ticks until we wrap? */
251
    uint64_t cur_tick = hpet_get_ticks();
252
    
253
    /* whenever new timer is being set up, make sure wrap_flag is 0 */
254
    t->wrap_flag = 0;
255
    diff = hpet_calculate_diff(t, cur_tick);
256

    
257
    /* hpet spec says in one-shot 32-bit mode, generate an interrupt when 
258
     * counter wraps in addition to an interrupt with comparator match.
259
     */ 
260
    if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
261
        wrap_diff = 0xffffffff - (uint32_t)cur_tick;
262
        if (wrap_diff < (uint32_t)diff) {
263
            diff = wrap_diff;
264
            t->wrap_flag = 1; 
265
        }
266
    }
267
    qemu_mod_timer(t->qemu_timer, qemu_get_clock(vm_clock) 
268
                   + (int64_t)ticks_to_ns(diff));
269
}
270

    
271
static void hpet_del_timer(HPETTimer *t)
272
{
273
    qemu_del_timer(t->qemu_timer);
274
}
275

    
276
#ifdef HPET_DEBUG
277
static uint32_t hpet_ram_readb(void *opaque, target_phys_addr_t addr)
278
{
279
    printf("qemu: hpet_read b at %" PRIx64 "\n", addr);
280
    return 0;
281
}
282

    
283
static uint32_t hpet_ram_readw(void *opaque, target_phys_addr_t addr)
284
{
285
    printf("qemu: hpet_read w at %" PRIx64 "\n", addr);
286
    return 0;
287
}
288
#endif
289

    
290
static uint32_t hpet_ram_readl(void *opaque, target_phys_addr_t addr)
291
{
292
    HPETState *s = (HPETState *)opaque;
293
    uint64_t cur_tick, index;
294

    
295
    dprintf("qemu: Enter hpet_ram_readl at %" PRIx64 "\n", addr);
296
    index = addr;
297
    /*address range of all TN regs*/
298
    if (index >= 0x100 && index <= 0x3ff) {
299
        uint8_t timer_id = (addr - 0x100) / 0x20;
300
        if (timer_id > HPET_NUM_TIMERS - 1) {
301
            printf("qemu: timer id out of range\n");
302
            return 0;
303
        }
304
        HPETTimer *timer = &s->timer[timer_id];
305

    
306
        switch ((addr - 0x100) % 0x20) {
307
            case HPET_TN_CFG:
308
                return timer->config;
309
            case HPET_TN_CFG + 4: // Interrupt capabilities
310
                return timer->config >> 32;
311
            case HPET_TN_CMP: // comparator register
312
                return timer->cmp;
313
            case HPET_TN_CMP + 4:
314
                return timer->cmp >> 32;
315
            case HPET_TN_ROUTE:
316
                return timer->fsb >> 32;
317
            default:
318
                dprintf("qemu: invalid hpet_ram_readl\n");
319
                break;
320
        }
321
    } else {
322
        switch (index) {
323
            case HPET_ID:
324
                return s->capability;
325
            case HPET_PERIOD:
326
                return s->capability >> 32; 
327
            case HPET_CFG:
328
                return s->config;
329
            case HPET_CFG + 4:
330
                dprintf("qemu: invalid HPET_CFG + 4 hpet_ram_readl \n");
331
                return 0;
332
            case HPET_COUNTER: 
333
                if (hpet_enabled())
334
                    cur_tick = hpet_get_ticks();
335
                else 
336
                    cur_tick = s->hpet_counter;
337
                dprintf("qemu: reading counter  = %" PRIx64 "\n", cur_tick);
338
                return cur_tick;
339
            case HPET_COUNTER + 4:
340
                if (hpet_enabled())
341
                    cur_tick = hpet_get_ticks();
342
                else 
343
                    cur_tick = s->hpet_counter;
344
                dprintf("qemu: reading counter + 4  = %" PRIx64 "\n", cur_tick);
345
                return cur_tick >> 32;
346
            case HPET_STATUS:
347
                return s->isr;
348
            default:
349
                dprintf("qemu: invalid hpet_ram_readl\n");
350
                break;
351
        }
352
    }
353
    return 0;
354
}
355

    
356
#ifdef HPET_DEBUG
357
static void hpet_ram_writeb(void *opaque, target_phys_addr_t addr, 
358
                            uint32_t value)
359
{
360
    printf("qemu: invalid hpet_write b at %" PRIx64 " = %#x\n", 
361
           addr, value);
362
}
363

    
364
static void hpet_ram_writew(void *opaque, target_phys_addr_t addr, 
365
                            uint32_t value)
366
{
367
    printf("qemu: invalid hpet_write w at %" PRIx64 " = %#x\n", 
368
           addr, value);
369
}
370
#endif
371

    
372
static void hpet_ram_writel(void *opaque, target_phys_addr_t addr,
373
                            uint32_t value)
374
{
375
    int i;
376
    HPETState *s = (HPETState *)opaque;
377
    uint64_t old_val, new_val, index;
378

    
379
    dprintf("qemu: Enter hpet_ram_writel at %" PRIx64 " = %#x\n", addr, value);
380
    index = addr;
381
    old_val = hpet_ram_readl(opaque, addr);
382
    new_val = value;
383

    
384
    /*address range of all TN regs*/
385
    if (index >= 0x100 && index <= 0x3ff) {
386
        uint8_t timer_id = (addr - 0x100) / 0x20;
387
        dprintf("qemu: hpet_ram_writel timer_id = %#x \n", timer_id);
388
        HPETTimer *timer = &s->timer[timer_id];
389
        
390
        switch ((addr - 0x100) % 0x20) {
391
            case HPET_TN_CFG:
392
                dprintf("qemu: hpet_ram_writel HPET_TN_CFG\n");
393
                timer->config = hpet_fixup_reg(new_val, old_val, 0x3e4e);
394
                if (new_val & HPET_TN_32BIT) {
395
                    timer->cmp = (uint32_t)timer->cmp;
396
                    timer->period = (uint32_t)timer->period;
397
                }
398
                if (new_val & HPET_TIMER_TYPE_LEVEL) {
399
                    printf("qemu: level-triggered hpet not supported\n");
400
                    exit (-1);
401
                }
402

    
403
                break;
404
            case HPET_TN_CFG + 4: // Interrupt capabilities
405
                dprintf("qemu: invalid HPET_TN_CFG+4 write\n");
406
                break;
407
            case HPET_TN_CMP: // comparator register
408
                dprintf("qemu: hpet_ram_writel HPET_TN_CMP \n");
409
                if (timer->config & HPET_TN_32BIT)
410
                    new_val = (uint32_t)new_val;
411
                if (!timer_is_periodic(timer) ||
412
                           (timer->config & HPET_TN_SETVAL))
413
                    timer->cmp = (timer->cmp & 0xffffffff00000000ULL)
414
                                  | new_val;
415
                else {
416
                    /*
417
                     * FIXME: Clamp period to reasonable min value?
418
                     * Clamp period to reasonable max value
419
                     */
420
                    new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1;
421
                    timer->period = (timer->period & 0xffffffff00000000ULL)
422
                                     | new_val;
423
                }
424
                timer->config &= ~HPET_TN_SETVAL;
425
                if (hpet_enabled())
426
                    hpet_set_timer(timer);
427
                break;
428
            case HPET_TN_CMP + 4: // comparator register high order
429
                dprintf("qemu: hpet_ram_writel HPET_TN_CMP + 4\n");
430
                if (!timer_is_periodic(timer) ||
431
                           (timer->config & HPET_TN_SETVAL))
432
                    timer->cmp = (timer->cmp & 0xffffffffULL)
433
                                  | new_val << 32;
434
                else {
435
                    /*
436
                     * FIXME: Clamp period to reasonable min value?
437
                     * Clamp period to reasonable max value
438
                     */
439
                    new_val &= (timer->config 
440
                                & HPET_TN_32BIT ? ~0u : ~0ull) >> 1;
441
                    timer->period = (timer->period & 0xffffffffULL)
442
                                     | new_val << 32;
443
                }
444
                timer->config &= ~HPET_TN_SETVAL;
445
                if (hpet_enabled())
446
                    hpet_set_timer(timer);
447
                break;
448
            case HPET_TN_ROUTE + 4:
449
                dprintf("qemu: hpet_ram_writel HPET_TN_ROUTE + 4\n");
450
                break;
451
            default:
452
                dprintf("qemu: invalid hpet_ram_writel\n");
453
                break;
454
        }
455
        return;
456
    } else {
457
        switch (index) {
458
            case HPET_ID:
459
                return;
460
            case HPET_CFG:
461
                s->config = hpet_fixup_reg(new_val, old_val, 0x3);
462
                if (activating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
463
                    /* Enable main counter and interrupt generation. */
464
                    s->hpet_offset = ticks_to_ns(s->hpet_counter)
465
                                     - qemu_get_clock(vm_clock);
466
                    for (i = 0; i < HPET_NUM_TIMERS; i++)
467
                        if ((&s->timer[i])->cmp != ~0ULL)
468
                            hpet_set_timer(&s->timer[i]);
469
                }
470
                else if (deactivating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
471
                    /* Halt main counter and disable interrupt generation. */
472
                    s->hpet_counter = hpet_get_ticks(); 
473
                    for (i = 0; i < HPET_NUM_TIMERS; i++)
474
                        hpet_del_timer(&s->timer[i]);
475
                }
476
                /* i8254 and RTC are disabled when HPET is in legacy mode */
477
                if (activating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
478
                    hpet_pit_disable();
479
                } else if (deactivating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
480
                    hpet_pit_enable();
481
                }
482
                break;
483
            case HPET_CFG + 4: 
484
                dprintf("qemu: invalid HPET_CFG+4 write \n");
485
                break;
486
            case HPET_STATUS:
487
                /* FIXME: need to handle level-triggered interrupts */
488
                break;
489
            case HPET_COUNTER:
490
               if (hpet_enabled()) 
491
                   printf("qemu: Writing counter while HPET enabled!\n"); 
492
               s->hpet_counter = (s->hpet_counter & 0xffffffff00000000ULL) 
493
                                  | value;
494
               dprintf("qemu: HPET counter written. ctr = %#x -> %" PRIx64 "\n",
495
                        value, s->hpet_counter);
496
               break;
497
            case HPET_COUNTER + 4:
498
               if (hpet_enabled()) 
499
                   printf("qemu: Writing counter while HPET enabled!\n"); 
500
               s->hpet_counter = (s->hpet_counter & 0xffffffffULL) 
501
                                  | (((uint64_t)value) << 32);
502
               dprintf("qemu: HPET counter + 4 written. ctr = %#x -> %" PRIx64 "\n",
503
                        value, s->hpet_counter);
504
               break;
505
            default:
506
               dprintf("qemu: invalid hpet_ram_writel\n");
507
               break;
508
        }
509
    }
510
}
511

    
512
static CPUReadMemoryFunc *hpet_ram_read[] = {
513
#ifdef HPET_DEBUG
514
    hpet_ram_readb,
515
    hpet_ram_readw,
516
#else
517
    NULL,
518
    NULL,
519
#endif
520
    hpet_ram_readl,
521
};
522

    
523
static CPUWriteMemoryFunc *hpet_ram_write[] = {
524
#ifdef HPET_DEBUG
525
    hpet_ram_writeb,
526
    hpet_ram_writew,
527
#else
528
    NULL,
529
    NULL,
530
#endif
531
    hpet_ram_writel,
532
};
533

    
534
static void hpet_reset(void *opaque) {
535
    HPETState *s = opaque;
536
    int i;
537
    static int count = 0;
538

    
539
    for (i=0; i<HPET_NUM_TIMERS; i++) {
540
        HPETTimer *timer = &s->timer[i];
541
        hpet_del_timer(timer);
542
        timer->tn = i;
543
        timer->cmp = ~0ULL;
544
        timer->config =  HPET_TN_PERIODIC_CAP | HPET_TN_SIZE_CAP;
545
        /* advertise availability of irqs 5,10,11 */
546
        timer->config |=  0x00000c20ULL << 32;
547
        timer->state = s;
548
        timer->period = 0ULL;
549
        timer->wrap_flag = 0;
550
    }
551

    
552
    s->hpet_counter = 0ULL;
553
    s->hpet_offset = 0ULL;
554
    /* 64-bit main counter; 3 timers supported; LegacyReplacementRoute. */
555
    s->capability = 0x8086a201ULL;
556
    s->capability |= ((HPET_CLK_PERIOD) << 32);
557
    if (count > 0)
558
        /* we don't enable pit when hpet_reset is first called (by hpet_init) 
559
         * because hpet is taking over for pit here. On subsequent invocations,
560
         * hpet_reset is called due to system reset. At this point control must
561
         * be returned to pit until SW reenables hpet.                    
562
         */
563
        hpet_pit_enable();
564
    count = 1;
565
}
566

    
567

    
568
void hpet_init(qemu_irq *irq) {
569
    int i, iomemtype;
570
    HPETState *s;
571
    
572
    dprintf ("hpet_init\n");
573

    
574
    s = qemu_mallocz(sizeof(HPETState));
575
    hpet_statep = s;
576
    s->irqs = irq;
577
    for (i=0; i<HPET_NUM_TIMERS; i++) {
578
        HPETTimer *timer = &s->timer[i];
579
        timer->qemu_timer = qemu_new_timer(vm_clock, hpet_timer, timer);
580
    }
581
    hpet_reset(s);
582
    register_savevm("hpet", -1, 1, hpet_save, hpet_load, s);
583
    qemu_register_reset(hpet_reset, s);
584
    /* HPET Area */
585
    iomemtype = cpu_register_io_memory(0, hpet_ram_read,
586
                                       hpet_ram_write, s);
587
    cpu_register_physical_memory(HPET_BASE, 0x400, iomemtype);
588
}