~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to xen/arch/x86/acpi/cpu_idle.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * cpu_idle - xen idle state module derived from Linux 
 
3
 *            drivers/acpi/processor_idle.c & 
 
4
 *            arch/x86/kernel/acpi/cstate.c
 
5
 *
 
6
 *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
 
7
 *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
 
8
 *  Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de>
 
9
 *  Copyright (C) 2004  Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
 
10
 *                      - Added processor hotplug support
 
11
 *  Copyright (C) 2005  Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
 
12
 *                      - Added support for C3 on SMP
 
13
 *  Copyright (C) 2007, 2008 Intel Corporation
 
14
 *
 
15
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
16
 *
 
17
 *  This program is free software; you can redistribute it and/or modify
 
18
 *  it under the terms of the GNU General Public License as published by
 
19
 *  the Free Software Foundation; either version 2 of the License, or (at
 
20
 *  your option) any later version.
 
21
 *
 
22
 *  This program is distributed in the hope that it will be useful, but
 
23
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 
24
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
25
 *  General Public License for more details.
 
26
 *
 
27
 *  You should have received a copy of the GNU General Public License along
 
28
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 
29
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 
30
 *
 
31
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
32
 */
 
33
 
 
34
#include <xen/config.h>
 
35
#include <xen/errno.h>
 
36
#include <xen/lib.h>
 
37
#include <xen/types.h>
 
38
#include <xen/acpi.h>
 
39
#include <xen/smp.h>
 
40
#include <xen/guest_access.h>
 
41
#include <xen/keyhandler.h>
 
42
#include <xen/cpuidle.h>
 
43
#include <xen/trace.h>
 
44
#include <xen/sched-if.h>
 
45
#include <asm/cache.h>
 
46
#include <asm/io.h>
 
47
#include <asm/hpet.h>
 
48
#include <asm/processor.h>
 
49
#include <xen/pmstat.h>
 
50
#include <public/platform.h>
 
51
#include <public/sysctl.h>
 
52
#include <acpi/cpufreq/cpufreq.h>
 
53
 
 
54
/*#define DEBUG_PM_CX*/
 
55
 
 
56
static void lapic_timer_nop(void) { }
 
57
static void (*lapic_timer_off)(void);
 
58
static void (*lapic_timer_on)(void);
 
59
 
 
60
extern void (*pm_idle) (void);
 
61
extern void (*dead_idle) (void);
 
62
extern void menu_get_trace_data(u32 *expected, u32 *pred);
 
63
 
 
64
static void (*pm_idle_save) (void) __read_mostly;
 
65
unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER - 1;
 
66
integer_param("max_cstate", max_cstate);
 
67
static int local_apic_timer_c2_ok __read_mostly = 0;
 
68
boolean_param("lapic_timer_c2_ok", local_apic_timer_c2_ok);
 
69
 
 
70
static struct acpi_processor_power *__read_mostly processor_powers[NR_CPUS];
 
71
 
 
72
static void print_acpi_power(uint32_t cpu, struct acpi_processor_power *power)
 
73
{
 
74
    uint32_t i, idle_usage = 0;
 
75
    uint64_t res, idle_res = 0;
 
76
 
 
77
    printk("==cpu%d==\n", cpu);
 
78
    printk("active state:\t\tC%d\n",
 
79
           power->last_state ? power->last_state->idx : -1);
 
80
    printk("max_cstate:\t\tC%d\n", max_cstate);
 
81
    printk("states:\n");
 
82
    
 
83
    for ( i = 1; i < power->count; i++ )
 
84
    {
 
85
        res = acpi_pm_tick_to_ns(power->states[i].time);
 
86
        idle_usage += power->states[i].usage;
 
87
        idle_res += res;
 
88
 
 
89
        printk((power->last_state && power->last_state->idx == i) ?
 
90
               "   *" : "    ");
 
91
        printk("C%d:\t", i);
 
92
        printk("type[C%d] ", power->states[i].type);
 
93
        printk("latency[%03d] ", power->states[i].latency);
 
94
        printk("usage[%08d] ", power->states[i].usage);
 
95
        printk("duration[%"PRId64"]\n", res);
 
96
    }
 
97
    printk("    C0:\tusage[%08d] duration[%"PRId64"]\n",
 
98
           idle_usage, NOW() - idle_res);
 
99
 
 
100
}
 
101
 
 
102
static void dump_cx(unsigned char key)
 
103
{
 
104
    unsigned int cpu;
 
105
 
 
106
    printk("'%c' pressed -> printing ACPI Cx structures\n", key);
 
107
    for_each_online_cpu ( cpu )
 
108
        if (processor_powers[cpu])
 
109
            print_acpi_power(cpu, processor_powers[cpu]);
 
110
}
 
111
 
 
112
static struct keyhandler dump_cx_keyhandler = {
 
113
    .diagnostic = 1,
 
114
    .u.fn = dump_cx,
 
115
    .desc = "dump ACPI Cx structures"
 
116
};
 
117
 
 
118
static int __init cpu_idle_key_init(void)
 
119
{
 
120
    register_keyhandler('c', &dump_cx_keyhandler);
 
121
    return 0;
 
122
}
 
123
__initcall(cpu_idle_key_init);
 
124
 
 
125
static inline u32 ticks_elapsed(u32 t1, u32 t2)
 
126
{
 
127
    if ( t2 >= t1 )
 
128
        return (t2 - t1);
 
129
    else if ( !(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER) )
 
130
        return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
 
131
    else
 
132
        return ((0xFFFFFFFF - t1) + t2);
 
133
}
 
134
 
 
135
static void acpi_safe_halt(void)
 
136
{
 
137
    smp_mb__after_clear_bit();
 
138
    safe_halt();
 
139
}
 
140
 
 
141
#define MWAIT_ECX_INTERRUPT_BREAK   (0x1)
 
142
 
 
143
static void mwait_idle_with_hints(unsigned long eax, unsigned long ecx)
 
144
{
 
145
    __monitor((void *)current, 0, 0);
 
146
    smp_mb();
 
147
    __mwait(eax, ecx);
 
148
}
 
149
 
 
150
static void acpi_processor_ffh_cstate_enter(struct acpi_processor_cx *cx)
 
151
{
 
152
    mwait_idle_with_hints(cx->address, MWAIT_ECX_INTERRUPT_BREAK);
 
153
}
 
154
 
 
155
static void acpi_idle_do_entry(struct acpi_processor_cx *cx)
 
156
{
 
157
    int unused;
 
158
 
 
159
    switch ( cx->entry_method )
 
160
    {
 
161
    case ACPI_CSTATE_EM_FFH:
 
162
        /* Call into architectural FFH based C-state */
 
163
        acpi_processor_ffh_cstate_enter(cx);
 
164
        return;
 
165
    case ACPI_CSTATE_EM_SYSIO:
 
166
        /* IO port based C-state */
 
167
        inb(cx->address);
 
168
        /* Dummy wait op - must do something useless after P_LVL2 read
 
169
           because chipsets cannot guarantee that STPCLK# signal
 
170
           gets asserted in time to freeze execution properly. */
 
171
        unused = inl(pmtmr_ioport);
 
172
        return;
 
173
    case ACPI_CSTATE_EM_HALT:
 
174
        acpi_safe_halt();
 
175
        local_irq_disable();
 
176
        return;
 
177
    }
 
178
}
 
179
 
 
180
static int acpi_idle_bm_check(void)
 
181
{
 
182
    u32 bm_status = 0;
 
183
 
 
184
    acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
 
185
    if ( bm_status )
 
186
        acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
 
187
    /*
 
188
     * TBD: PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
 
189
     * the true state of bus mastering activity; forcing us to
 
190
     * manually check the BMIDEA bit of each IDE channel.
 
191
     */
 
192
    return bm_status;
 
193
}
 
194
 
 
195
static struct {
 
196
    spinlock_t lock;
 
197
    unsigned int count;
 
198
} c3_cpu_status = { .lock = SPIN_LOCK_UNLOCKED };
 
199
 
 
200
static inline void trace_exit_reason(u32 *irq_traced)
 
201
{
 
202
    if ( unlikely(tb_init_done) )
 
203
    {
 
204
        int i, curbit;
 
205
        u32 irr_status[8] = { 0 };
 
206
 
 
207
        /* Get local apic IRR register */
 
208
        for ( i = 0; i < 8; i++ )
 
209
            irr_status[i] = apic_read(APIC_IRR + (i << 4));
 
210
        i = 0;
 
211
        curbit = find_first_bit((const unsigned long *)irr_status, 256);
 
212
        while ( i < 4 && curbit < 256 )
 
213
        {
 
214
            irq_traced[i++] = curbit;
 
215
            curbit = find_next_bit((const unsigned long *)irr_status, 256, curbit + 1);
 
216
        }
 
217
    }
 
218
}
 
219
 
 
220
/* vcpu is urgent if vcpu is polling event channel
 
221
 *
 
222
 * if urgent vcpu exists, CPU should not enter deep C state
 
223
 */
 
224
static int sched_has_urgent_vcpu(void)
 
225
{
 
226
    return atomic_read(&this_cpu(schedule_data).urgent_count);
 
227
}
 
228
 
 
229
static void acpi_processor_idle(void)
 
230
{
 
231
    struct acpi_processor_power *power = processor_powers[smp_processor_id()];
 
232
    struct acpi_processor_cx *cx = NULL;
 
233
    int next_state;
 
234
    int sleep_ticks = 0;
 
235
    u32 t1, t2 = 0;
 
236
    u32 exp = 0, pred = 0;
 
237
    u32 irq_traced[4] = { 0 };
 
238
 
 
239
    if ( max_cstate > 0 && power && !sched_has_urgent_vcpu() &&
 
240
         (next_state = cpuidle_current_governor->select(power)) > 0 )
 
241
    {
 
242
        cx = &power->states[next_state];
 
243
        if ( power->flags.bm_check && acpi_idle_bm_check()
 
244
             && cx->type == ACPI_STATE_C3 )
 
245
            cx = power->safe_state;
 
246
        if ( cx->idx > max_cstate )
 
247
            cx = &power->states[max_cstate];
 
248
        menu_get_trace_data(&exp, &pred);
 
249
    }
 
250
    if ( !cx )
 
251
    {
 
252
        if ( pm_idle_save )
 
253
            pm_idle_save();
 
254
        else
 
255
            acpi_safe_halt();
 
256
        return;
 
257
    }
 
258
 
 
259
    cpufreq_dbs_timer_suspend();
 
260
 
 
261
    sched_tick_suspend();
 
262
    /* sched_tick_suspend() can raise TIMER_SOFTIRQ. Process it now. */
 
263
    process_pending_softirqs();
 
264
 
 
265
    /*
 
266
     * Interrupts must be disabled during bus mastering calculations and
 
267
     * for C2/C3 transitions.
 
268
     */
 
269
    local_irq_disable();
 
270
 
 
271
    if ( softirq_pending(smp_processor_id()) ||
 
272
         cpu_is_offline(smp_processor_id()) )
 
273
    {
 
274
        local_irq_enable();
 
275
        sched_tick_resume();
 
276
        cpufreq_dbs_timer_resume();
 
277
        return;
 
278
    }
 
279
 
 
280
    power->last_state = cx;
 
281
 
 
282
    /*
 
283
     * Sleep:
 
284
     * ------
 
285
     * Invoke the current Cx state to put the processor to sleep.
 
286
     */
 
287
    switch ( cx->type )
 
288
    {
 
289
    case ACPI_STATE_C1:
 
290
    case ACPI_STATE_C2:
 
291
        if ( cx->type == ACPI_STATE_C1 || local_apic_timer_c2_ok )
 
292
        {
 
293
            /* Get start time (ticks) */
 
294
            t1 = inl(pmtmr_ioport);
 
295
            /* Trace cpu idle entry */
 
296
            TRACE_4D(TRC_PM_IDLE_ENTRY, cx->idx, t1, exp, pred);
 
297
            /* Invoke C2 */
 
298
            acpi_idle_do_entry(cx);
 
299
            /* Get end time (ticks) */
 
300
            t2 = inl(pmtmr_ioport);
 
301
            trace_exit_reason(irq_traced);
 
302
            /* Trace cpu idle exit */
 
303
            TRACE_6D(TRC_PM_IDLE_EXIT, cx->idx, t2,
 
304
                     irq_traced[0], irq_traced[1], irq_traced[2], irq_traced[3]);
 
305
            /* Re-enable interrupts */
 
306
            local_irq_enable();
 
307
            /* Compute time (ticks) that we were actually asleep */
 
308
            sleep_ticks = ticks_elapsed(t1, t2);
 
309
            break;
 
310
        }
 
311
 
 
312
    case ACPI_STATE_C3:
 
313
        /*
 
314
         * disable bus master
 
315
         * bm_check implies we need ARB_DIS
 
316
         * !bm_check implies we need cache flush
 
317
         * bm_control implies whether we can do ARB_DIS
 
318
         *
 
319
         * That leaves a case where bm_check is set and bm_control is
 
320
         * not set. In that case we cannot do much, we enter C3
 
321
         * without doing anything.
 
322
         */
 
323
        if ( power->flags.bm_check && power->flags.bm_control )
 
324
        {
 
325
            spin_lock(&c3_cpu_status.lock);
 
326
            if ( ++c3_cpu_status.count == num_online_cpus() )
 
327
            {
 
328
                /*
 
329
                 * All CPUs are trying to go to C3
 
330
                 * Disable bus master arbitration
 
331
                 */
 
332
                acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
 
333
            }
 
334
            spin_unlock(&c3_cpu_status.lock);
 
335
        }
 
336
        else if ( !power->flags.bm_check )
 
337
        {
 
338
            /* SMP with no shared cache... Invalidate cache  */
 
339
            ACPI_FLUSH_CPU_CACHE();
 
340
        }
 
341
 
 
342
        /*
 
343
         * Before invoking C3, be aware that TSC/APIC timer may be 
 
344
         * stopped by H/W. Without carefully handling of TSC/APIC stop issues,
 
345
         * deep C state can't work correctly.
 
346
         */
 
347
        /* preparing APIC stop */
 
348
        lapic_timer_off();
 
349
 
 
350
        /* Get start time (ticks) */
 
351
        t1 = inl(pmtmr_ioport);
 
352
        /* Trace cpu idle entry */
 
353
        TRACE_4D(TRC_PM_IDLE_ENTRY, cx->idx, t1, exp, pred);
 
354
        /* Invoke C3 */
 
355
        acpi_idle_do_entry(cx);
 
356
        /* Get end time (ticks) */
 
357
        t2 = inl(pmtmr_ioport);
 
358
 
 
359
        /* recovering TSC */
 
360
        cstate_restore_tsc();
 
361
        trace_exit_reason(irq_traced);
 
362
        /* Trace cpu idle exit */
 
363
        TRACE_6D(TRC_PM_IDLE_EXIT, cx->idx, t2,
 
364
                 irq_traced[0], irq_traced[1], irq_traced[2], irq_traced[3]);
 
365
 
 
366
        if ( power->flags.bm_check && power->flags.bm_control )
 
367
        {
 
368
            /* Enable bus master arbitration */
 
369
            spin_lock(&c3_cpu_status.lock);
 
370
            if ( c3_cpu_status.count-- == num_online_cpus() )
 
371
                acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
 
372
            spin_unlock(&c3_cpu_status.lock);
 
373
        }
 
374
 
 
375
        /* Re-enable interrupts */
 
376
        local_irq_enable();
 
377
        /* recovering APIC */
 
378
        lapic_timer_on();
 
379
        /* Compute time (ticks) that we were actually asleep */
 
380
        sleep_ticks = ticks_elapsed(t1, t2);
 
381
 
 
382
        break;
 
383
 
 
384
    default:
 
385
        local_irq_enable();
 
386
        sched_tick_resume();
 
387
        cpufreq_dbs_timer_resume();
 
388
        return;
 
389
    }
 
390
 
 
391
    cx->usage++;
 
392
    if ( sleep_ticks > 0 )
 
393
    {
 
394
        power->last_residency = acpi_pm_tick_to_ns(sleep_ticks) / 1000UL;
 
395
        cx->time += sleep_ticks;
 
396
    }
 
397
 
 
398
    sched_tick_resume();
 
399
    cpufreq_dbs_timer_resume();
 
400
 
 
401
    if ( cpuidle_current_governor->reflect )
 
402
        cpuidle_current_governor->reflect(power);
 
403
}
 
404
 
 
405
static void acpi_dead_idle(void)
 
406
{
 
407
    struct acpi_processor_power *power;
 
408
    struct acpi_processor_cx *cx;
 
409
    int unused;
 
410
 
 
411
    if ( (power = processor_powers[smp_processor_id()]) == NULL )
 
412
        goto default_halt;
 
413
 
 
414
    if ( (cx = &power->states[power->count-1]) == NULL )
 
415
        goto default_halt;
 
416
 
 
417
    for ( ; ; )
 
418
    {
 
419
        if ( !power->flags.bm_check && cx->type == ACPI_STATE_C3 )
 
420
            ACPI_FLUSH_CPU_CACHE();
 
421
 
 
422
        switch ( cx->entry_method )
 
423
        {
 
424
            case ACPI_CSTATE_EM_FFH:
 
425
                /* Not treat interrupt as break event */
 
426
                mwait_idle_with_hints(cx->address, 0);
 
427
                break;
 
428
            case ACPI_CSTATE_EM_SYSIO:
 
429
                inb(cx->address);
 
430
                unused = inl(pmtmr_ioport);
 
431
                break;
 
432
            default:
 
433
                goto default_halt;
 
434
        }
 
435
    }
 
436
 
 
437
default_halt:
 
438
    for ( ; ; )
 
439
        halt();
 
440
}
 
441
 
 
442
static int init_cx_pminfo(struct acpi_processor_power *acpi_power)
 
443
{
 
444
    int i;
 
445
 
 
446
    memset(acpi_power, 0, sizeof(*acpi_power));
 
447
 
 
448
    for ( i = 0; i < ACPI_PROCESSOR_MAX_POWER; i++ )
 
449
        acpi_power->states[i].idx = i;
 
450
 
 
451
    acpi_power->states[ACPI_STATE_C1].type = ACPI_STATE_C1;
 
452
    acpi_power->states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_EM_HALT;
 
453
 
 
454
    acpi_power->states[ACPI_STATE_C0].valid = 1;
 
455
    acpi_power->states[ACPI_STATE_C1].valid = 1;
 
456
 
 
457
    acpi_power->count = 2;
 
458
    acpi_power->safe_state = &acpi_power->states[ACPI_STATE_C1];
 
459
 
 
460
    return 0;
 
461
}
 
462
 
 
463
#define CPUID_MWAIT_LEAF (5)
 
464
#define CPUID5_ECX_EXTENSIONS_SUPPORTED (0x1)
 
465
#define CPUID5_ECX_INTERRUPT_BREAK      (0x2)
 
466
 
 
467
#define MWAIT_ECX_INTERRUPT_BREAK       (0x1)
 
468
 
 
469
#define MWAIT_SUBSTATE_MASK (0xf)
 
470
#define MWAIT_SUBSTATE_SIZE (4)
 
471
 
 
472
static int acpi_processor_ffh_cstate_probe(xen_processor_cx_t *cx)
 
473
{
 
474
    struct cpuinfo_x86 *c = &current_cpu_data;
 
475
    unsigned int eax, ebx, ecx, edx;
 
476
    unsigned int edx_part;
 
477
    unsigned int cstate_type; /* C-state type and not ACPI C-state type */
 
478
    unsigned int num_cstate_subtype;
 
479
 
 
480
    if ( c->cpuid_level < CPUID_MWAIT_LEAF )
 
481
    {
 
482
        printk(XENLOG_INFO "MWAIT leaf not supported by cpuid\n");
 
483
        return -EFAULT;
 
484
    }
 
485
 
 
486
    cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
 
487
    printk(XENLOG_DEBUG "cpuid.MWAIT[.eax=%x, .ebx=%x, .ecx=%x, .edx=%x]\n",
 
488
           eax, ebx, ecx, edx);
 
489
 
 
490
    /* Check whether this particular cx_type (in CST) is supported or not */
 
491
    cstate_type = (cx->reg.address >> MWAIT_SUBSTATE_SIZE) + 1;
 
492
    edx_part = edx >> (cstate_type * MWAIT_SUBSTATE_SIZE);
 
493
    num_cstate_subtype = edx_part & MWAIT_SUBSTATE_MASK;
 
494
 
 
495
    if ( num_cstate_subtype < (cx->reg.address & MWAIT_SUBSTATE_MASK) )
 
496
        return -EFAULT;
 
497
 
 
498
    /* mwait ecx extensions INTERRUPT_BREAK should be supported for C2/C3 */
 
499
    if ( !(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
 
500
         !(ecx & CPUID5_ECX_INTERRUPT_BREAK) )
 
501
        return -EFAULT;
 
502
 
 
503
    printk(XENLOG_INFO "Monitor-Mwait will be used to enter C-%d state\n", cx->type);
 
504
    return 0;
 
505
}
 
506
 
 
507
/*
 
508
 * Initialize bm_flags based on the CPU cache properties
 
509
 * On SMP it depends on cache configuration
 
510
 * - When cache is not shared among all CPUs, we flush cache
 
511
 *   before entering C3.
 
512
 * - When cache is shared among all CPUs, we use bm_check
 
513
 *   mechanism as in UP case
 
514
 *
 
515
 * This routine is called only after all the CPUs are online
 
516
 */
 
517
static void acpi_processor_power_init_bm_check(struct acpi_processor_flags *flags)
 
518
{
 
519
    struct cpuinfo_x86 *c = &current_cpu_data;
 
520
 
 
521
    flags->bm_check = 0;
 
522
    if ( num_online_cpus() == 1 )
 
523
        flags->bm_check = 1;
 
524
    else if ( c->x86_vendor == X86_VENDOR_INTEL )
 
525
    {
 
526
        /*
 
527
         * Today all MP CPUs that support C3 share cache.
 
528
         * And caches should not be flushed by software while
 
529
         * entering C3 type state.
 
530
         */
 
531
        flags->bm_check = 1;
 
532
    }
 
533
 
 
534
    /*
 
535
     * On all recent platforms, ARB_DISABLE is a nop.
 
536
     * So, set bm_control to zero to indicate that ARB_DISABLE
 
537
     * is not required while entering C3 type state on
 
538
     * P4, Core and beyond CPUs
 
539
     */
 
540
    if ( c->x86_vendor == X86_VENDOR_INTEL &&
 
541
        (c->x86 > 0x6 || (c->x86 == 6 && c->x86_model >= 14)) )
 
542
            flags->bm_control = 0;
 
543
}
 
544
 
 
545
#define VENDOR_INTEL                   (1)
 
546
#define NATIVE_CSTATE_BEYOND_HALT      (2)
 
547
 
 
548
static int check_cx(struct acpi_processor_power *power, xen_processor_cx_t *cx)
 
549
{
 
550
    static int bm_check_flag = -1;
 
551
    static int bm_control_flag = -1;
 
552
 
 
553
    switch ( cx->reg.space_id )
 
554
    {
 
555
    case ACPI_ADR_SPACE_SYSTEM_IO:
 
556
        if ( cx->reg.address == 0 )
 
557
            return -EINVAL;
 
558
        break;
 
559
 
 
560
    case ACPI_ADR_SPACE_FIXED_HARDWARE:
 
561
        if ( cx->reg.bit_width != VENDOR_INTEL || 
 
562
             cx->reg.bit_offset != NATIVE_CSTATE_BEYOND_HALT )
 
563
            return -EINVAL;
 
564
 
 
565
        /* assume all logical cpu has the same support for mwait */
 
566
        if ( acpi_processor_ffh_cstate_probe(cx) )
 
567
            return -EINVAL;
 
568
        break;
 
569
 
 
570
    default:
 
571
        return -ENODEV;
 
572
    }
 
573
 
 
574
    switch ( cx->type )
 
575
    {
 
576
    case ACPI_STATE_C2:
 
577
        if ( local_apic_timer_c2_ok )
 
578
            break;
 
579
    case ACPI_STATE_C3:
 
580
        if ( boot_cpu_has(X86_FEATURE_ARAT) )
 
581
        {
 
582
            lapic_timer_off = lapic_timer_nop;
 
583
            lapic_timer_on = lapic_timer_nop;
 
584
        }
 
585
        else if ( hpet_broadcast_is_available() )
 
586
        {
 
587
            lapic_timer_off = hpet_broadcast_enter;
 
588
            lapic_timer_on = hpet_broadcast_exit;
 
589
        }
 
590
        else if ( pit_broadcast_is_available() )
 
591
        {
 
592
            lapic_timer_off = pit_broadcast_enter;
 
593
            lapic_timer_on = pit_broadcast_exit;
 
594
        }
 
595
        else
 
596
        {
 
597
            return -EINVAL;
 
598
        }
 
599
 
 
600
        /* All the logic here assumes flags.bm_check is same across all CPUs */
 
601
        if ( bm_check_flag == -1 )
 
602
        {
 
603
            /* Determine whether bm_check is needed based on CPU  */
 
604
            acpi_processor_power_init_bm_check(&(power->flags));
 
605
            bm_check_flag = power->flags.bm_check;
 
606
            bm_control_flag = power->flags.bm_control;
 
607
        }
 
608
        else
 
609
        {
 
610
            power->flags.bm_check = bm_check_flag;
 
611
            power->flags.bm_control = bm_control_flag;
 
612
        }
 
613
 
 
614
        if ( power->flags.bm_check )
 
615
        {
 
616
            if ( !power->flags.bm_control )
 
617
            {
 
618
                if ( power->flags.has_cst != 1 )
 
619
                {
 
620
                    /* bus mastering control is necessary */
 
621
                    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
622
                        "C3 support requires BM control\n"));
 
623
                    return -EINVAL;
 
624
                }
 
625
                else
 
626
                {
 
627
                    /* Here we enter C3 without bus mastering */
 
628
                    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
629
                        "C3 support without BM control\n"));
 
630
                }
 
631
            }
 
632
            /*
 
633
             * On older chipsets, BM_RLD needs to be set
 
634
             * in order for Bus Master activity to wake the
 
635
             * system from C3.  Newer chipsets handle DMA
 
636
             * during C3 automatically and BM_RLD is a NOP.
 
637
             * In either case, the proper way to
 
638
             * handle BM_RLD is to set it and leave it set.
 
639
             */
 
640
            acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
 
641
        }
 
642
        else
 
643
        {
 
644
            /*
 
645
             * WBINVD should be set in fadt, for C3 state to be
 
646
             * supported on when bm_check is not required.
 
647
             */
 
648
            if ( !(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD) )
 
649
            {
 
650
                ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
651
                          "Cache invalidation should work properly"
 
652
                          " for C3 to be enabled on SMP systems\n"));
 
653
                return -EINVAL;
 
654
            }
 
655
            acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
 
656
        }
 
657
 
 
658
        break;
 
659
    }
 
660
 
 
661
    return 0;
 
662
}
 
663
 
 
664
static unsigned int latency_factor = 2;
 
665
integer_param("idle_latency_factor", latency_factor);
 
666
 
 
667
static void set_cx(
 
668
    struct acpi_processor_power *acpi_power,
 
669
    xen_processor_cx_t *xen_cx)
 
670
{
 
671
    struct acpi_processor_cx *cx;
 
672
 
 
673
    if ( check_cx(acpi_power, xen_cx) != 0 )
 
674
        return;
 
675
 
 
676
    if ( xen_cx->type == ACPI_STATE_C1 )
 
677
        cx = &acpi_power->states[1];
 
678
    else
 
679
        cx = &acpi_power->states[acpi_power->count];
 
680
 
 
681
    if ( !cx->valid )
 
682
        acpi_power->count++;
 
683
 
 
684
    cx->valid    = 1;
 
685
    cx->type     = xen_cx->type;
 
686
    cx->address  = xen_cx->reg.address;
 
687
 
 
688
    switch ( xen_cx->reg.space_id )
 
689
    {
 
690
    case ACPI_ADR_SPACE_FIXED_HARDWARE:
 
691
        if ( xen_cx->reg.bit_width == VENDOR_INTEL &&
 
692
             xen_cx->reg.bit_offset == NATIVE_CSTATE_BEYOND_HALT )
 
693
            cx->entry_method = ACPI_CSTATE_EM_FFH;
 
694
        else
 
695
            cx->entry_method = ACPI_CSTATE_EM_HALT;
 
696
        break;
 
697
    case ACPI_ADR_SPACE_SYSTEM_IO:
 
698
        cx->entry_method = ACPI_CSTATE_EM_SYSIO;
 
699
        break;
 
700
    default:
 
701
        cx->entry_method = ACPI_CSTATE_EM_NONE;
 
702
    }
 
703
 
 
704
    cx->latency  = xen_cx->latency;
 
705
    cx->power    = xen_cx->power;
 
706
    
 
707
    cx->latency_ticks = ns_to_acpi_pm_tick(cx->latency * 1000UL);
 
708
    cx->target_residency = cx->latency * latency_factor;
 
709
    if ( cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2 )
 
710
        acpi_power->safe_state = cx;
 
711
}
 
712
 
 
713
int get_cpu_id(u8 acpi_id)
 
714
{
 
715
    int i;
 
716
    u8 apic_id;
 
717
 
 
718
    apic_id = x86_acpiid_to_apicid[acpi_id];
 
719
    if ( apic_id == 0xff )
 
720
        return -1;
 
721
 
 
722
    for ( i = 0; i < NR_CPUS; i++ )
 
723
    {
 
724
        if ( apic_id == x86_cpu_to_apicid[i] )
 
725
            return i;
 
726
    }
 
727
 
 
728
    return -1;
 
729
}
 
730
 
 
731
#ifdef DEBUG_PM_CX
 
732
static void print_cx_pminfo(uint32_t cpu, struct xen_processor_power *power)
 
733
{
 
734
    XEN_GUEST_HANDLE(xen_processor_cx_t) states;
 
735
    xen_processor_cx_t  state;
 
736
    XEN_GUEST_HANDLE(xen_processor_csd_t) csd;
 
737
    xen_processor_csd_t dp;
 
738
    uint32_t i;
 
739
 
 
740
    printk("cpu%d cx acpi info:\n", cpu);
 
741
    printk("\tcount = %d\n", power->count);
 
742
    printk("\tflags: bm_cntl[%d], bm_chk[%d], has_cst[%d],\n"
 
743
           "\t       pwr_setup_done[%d], bm_rld_set[%d]\n",
 
744
           power->flags.bm_control, power->flags.bm_check, power->flags.has_cst,
 
745
           power->flags.power_setup_done, power->flags.bm_rld_set);
 
746
    
 
747
    states = power->states;
 
748
    
 
749
    for ( i = 0; i < power->count; i++ )
 
750
    {
 
751
        if ( unlikely(copy_from_guest_offset(&state, states, i, 1)) )
 
752
            return;
 
753
        
 
754
        printk("\tstates[%d]:\n", i);
 
755
        printk("\t\treg.space_id = 0x%x\n", state.reg.space_id);
 
756
        printk("\t\treg.bit_width = 0x%x\n", state.reg.bit_width);
 
757
        printk("\t\treg.bit_offset = 0x%x\n", state.reg.bit_offset);
 
758
        printk("\t\treg.access_size = 0x%x\n", state.reg.access_size);
 
759
        printk("\t\treg.address = 0x%"PRIx64"\n", state.reg.address);
 
760
        printk("\t\ttype    = %d\n", state.type);
 
761
        printk("\t\tlatency = %d\n", state.latency);
 
762
        printk("\t\tpower   = %d\n", state.power);
 
763
 
 
764
        csd = state.dp;
 
765
        printk("\t\tdp(@0x%p)\n", csd.p);
 
766
        
 
767
        if ( csd.p != NULL )
 
768
        {
 
769
            if ( unlikely(copy_from_guest(&dp, csd, 1)) )
 
770
                return;
 
771
            printk("\t\t\tdomain = %d\n", dp.domain);
 
772
            printk("\t\t\tcoord_type   = %d\n", dp.coord_type);
 
773
            printk("\t\t\tnum = %d\n", dp.num);
 
774
        }
 
775
    }
 
776
}
 
777
#else
 
778
#define print_cx_pminfo(c, p)
 
779
#endif
 
780
 
 
781
long set_cx_pminfo(uint32_t cpu, struct xen_processor_power *power)
 
782
{
 
783
    XEN_GUEST_HANDLE(xen_processor_cx_t) states;
 
784
    xen_processor_cx_t xen_cx;
 
785
    struct acpi_processor_power *acpi_power;
 
786
    int cpu_id, i;
 
787
 
 
788
    if ( unlikely(!guest_handle_okay(power->states, power->count)) )
 
789
        return -EFAULT;
 
790
 
 
791
    print_cx_pminfo(cpu, power);
 
792
 
 
793
    /* map from acpi_id to cpu_id */
 
794
    cpu_id = get_cpu_id((u8)cpu);
 
795
    if ( cpu_id == -1 )
 
796
    {
 
797
        printk(XENLOG_ERR "no cpu_id for acpi_id %d\n", cpu);
 
798
        return -EFAULT;
 
799
    }
 
800
 
 
801
    acpi_power = processor_powers[cpu_id];
 
802
    if ( !acpi_power )
 
803
    {
 
804
        acpi_power = xmalloc(struct acpi_processor_power);
 
805
        if ( !acpi_power )
 
806
            return -ENOMEM;
 
807
        memset(acpi_power, 0, sizeof(*acpi_power));
 
808
        processor_powers[cpu_id] = acpi_power;
 
809
    }
 
810
 
 
811
    init_cx_pminfo(acpi_power);
 
812
 
 
813
    acpi_power->cpu = cpu_id;
 
814
    acpi_power->flags.bm_check = power->flags.bm_check;
 
815
    acpi_power->flags.bm_control = power->flags.bm_control;
 
816
    acpi_power->flags.has_cst = power->flags.has_cst;
 
817
 
 
818
    states = power->states;
 
819
 
 
820
    for ( i = 0; i < power->count; i++ )
 
821
    {
 
822
        if ( unlikely(copy_from_guest_offset(&xen_cx, states, i, 1)) )
 
823
            return -EFAULT;
 
824
 
 
825
        set_cx(acpi_power, &xen_cx);
 
826
    }
 
827
 
 
828
    if ( cpuidle_current_governor->enable &&
 
829
         cpuidle_current_governor->enable(acpi_power) )
 
830
        return -EFAULT;
 
831
 
 
832
    /* FIXME: C-state dependency is not supported by far */
 
833
 
 
834
    /*print_acpi_power(cpu_id, acpi_power);*/
 
835
 
 
836
    if ( cpu_id == 0 && pm_idle_save == NULL )
 
837
    {
 
838
        pm_idle_save = pm_idle;
 
839
        pm_idle = acpi_processor_idle;
 
840
    }
 
841
 
 
842
    if ( cpu_id == 0 )
 
843
    {
 
844
        dead_idle = acpi_dead_idle;
 
845
    }
 
846
        
 
847
    return 0;
 
848
}
 
849
 
 
850
uint32_t pmstat_get_cx_nr(uint32_t cpuid)
 
851
{
 
852
    return processor_powers[cpuid] ? processor_powers[cpuid]->count : 0;
 
853
}
 
854
 
 
855
int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat)
 
856
{
 
857
    const struct acpi_processor_power *power = processor_powers[cpuid];
 
858
    uint64_t usage, res, idle_usage = 0, idle_res = 0;
 
859
    int i;
 
860
 
 
861
    if ( power == NULL )
 
862
    {
 
863
        stat->last = 0;
 
864
        stat->nr = 0;
 
865
        stat->idle_time = 0;
 
866
        return 0;
 
867
    }
 
868
 
 
869
    stat->last = power->last_state ? power->last_state->idx : 0;
 
870
    stat->nr = power->count;
 
871
    stat->idle_time = get_cpu_idle_time(cpuid);
 
872
 
 
873
    for ( i = power->count - 1; i >= 0; i-- )
 
874
    {
 
875
        if ( i != 0 )
 
876
        {
 
877
            usage = power->states[i].usage;
 
878
            res = acpi_pm_tick_to_ns(power->states[i].time);
 
879
            idle_usage += usage;
 
880
            idle_res += res;
 
881
        }
 
882
        else
 
883
        {
 
884
            usage = idle_usage;
 
885
            res = NOW() - idle_res;
 
886
        }
 
887
        if ( copy_to_guest_offset(stat->triggers, i, &usage, 1) ||
 
888
             copy_to_guest_offset(stat->residencies, i, &res, 1) )
 
889
            return -EFAULT;
 
890
    }
 
891
 
 
892
    return 0;
 
893
}
 
894
 
 
895
int pmstat_reset_cx_stat(uint32_t cpuid)
 
896
{
 
897
    return 0;
 
898
}
 
899
 
 
900
void cpuidle_disable_deep_cstate(void)
 
901
{
 
902
    if ( max_cstate > 1 )
 
903
    {
 
904
        if ( local_apic_timer_c2_ok )
 
905
            max_cstate = 2;
 
906
        else
 
907
            max_cstate = 1;
 
908
    }
 
909
 
 
910
    mb();
 
911
 
 
912
    hpet_disable_legacy_broadcast();
 
913
}
 
914