1 /* 2 * Copyright (c) 2014 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Sepherosa Ziehau <sepherosa@gmail.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/kernel.h> 37 #include <sys/systm.h> 38 #include <sys/globaldata.h> 39 40 #include <machine/md_var.h> 41 #include <machine/cpufunc.h> 42 #include <machine/cpufreq.h> 43 #include <machine/cputypes.h> 44 #include <machine/specialreg.h> 45 46 #include "acpi.h" 47 #include "acpi_cpu_cstate.h" 48 49 /* GAS.BitWidth */ 50 #define ACPI_GAS_INTEL_VENDOR 1 51 52 /* GAS.BitOffset */ 53 #define ACPI_GAS_INTEL_CLASS_C1_IO_HALT 1 54 #define ACPI_GAS_INTEL_CLASS_CX_NATIVE 2 55 56 /* GAS.AccessWidth */ 57 #define ACPI_GAS_INTEL_ARG1_HWCOORD 0x1 58 #define ACPI_GAS_INTEL_ARG1_BM_STS 0x2 59 60 /* GAS.Address */ 61 #define ACPI_GAS_INTEL_ARG0_MWAIT_HINTMASK 0xffffffff 62 63 static int acpi_cst_cx_mwait_setup(struct acpi_cst_cx *); 64 static void acpi_cst_cx_mwait_enter(const struct acpi_cst_cx *); 65 66 int 67 acpi_cst_md_cx_setup(struct acpi_cst_cx *cx) 68 { 69 int error; 70 71 if (cpu_vendor_id != CPU_VENDOR_INTEL) { 72 /* 73 * No optimization for non-Intel CPUs so far. 74 * 75 * Hardware fixed resouce is not supported for 76 * C1+ state yet. 77 */ 78 if (cx->type == ACPI_STATE_C1 && 79 cx->gas.SpaceId == ACPI_ADR_SPACE_FIXED_HARDWARE) 80 return 0; 81 if (cx->gas.SpaceId != ACPI_ADR_SPACE_SYSTEM_IO && 82 cx->gas.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY) { 83 kprintf("C%d: invalid SpaceId %d\n", cx->type, 84 cx->gas.SpaceId); 85 return EINVAL; 86 } 87 return 0; 88 } 89 90 switch (cx->gas.SpaceId) { 91 case ACPI_ADR_SPACE_SYSTEM_IO: 92 case ACPI_ADR_SPACE_SYSTEM_MEMORY: 93 break; 94 95 case ACPI_ADR_SPACE_FIXED_HARDWARE: 96 error = acpi_cst_cx_mwait_setup(cx); 97 if (error) 98 return error; 99 break; 100 101 default: 102 kprintf("C%d: invalid SpaceId %d\n", cx->type, cx->gas.SpaceId); 103 return EINVAL; 104 } 105 106 if (cx->type >= ACPI_STATE_C3) { 107 if (CPUID_TO_FAMILY(cpu_id) > 0xf || 108 (CPUID_TO_FAMILY(cpu_id) == 0x6 && 109 CPUID_TO_MODEL(cpu_id) >= 0xf)) { 110 /* 111 * Pentium dual-core, Core 2 and beyond do not 112 * need any additional activities to enter C3(+). 113 */ 114 cx->preamble = ACPI_CST_CX_PREAMBLE_NONE; 115 } else if ((acpi_cst_quirks & ACPI_CST_QUIRK_NO_BM) == 0) { 116 /* 117 * Intel CPUs support bus master operation for 118 * entering C3(+) even on MP system. 119 */ 120 cx->preamble = ACPI_CST_CX_PREAMBLE_BM_ARB; 121 } 122 } 123 return 0; 124 } 125 126 static int 127 acpi_cst_cx_mwait_setup(struct acpi_cst_cx *cx) 128 { 129 uint32_t eax_hint; 130 131 if (bootverbose) { 132 kprintf("C%d: BitWidth(vendor) %d, BitOffset(class) %d, " 133 "Address(arg0) 0x%jx, AccessWidth(arg1) 0x%x\n", cx->type, 134 cx->gas.BitWidth, cx->gas.BitOffset, 135 (uintmax_t)cx->gas.Address, cx->gas.AccessWidth); 136 } 137 138 if (cx->type == ACPI_STATE_C1) { 139 /* XXX mwait */ 140 /* XXX I/O then halt */ 141 return 0; 142 } 143 144 if (cx->gas.BitOffset != ACPI_GAS_INTEL_CLASS_CX_NATIVE) 145 return EINVAL; 146 147 if ((cpu_feature2 & CPUID2_MON) == 0) 148 return EOPNOTSUPP; 149 if ((cpu_mwait_feature & (CPUID_MWAIT_EXT | CPUID_MWAIT_INTBRK)) != 150 (CPUID_MWAIT_EXT | CPUID_MWAIT_INTBRK)) 151 return EOPNOTSUPP; 152 153 eax_hint = cx->gas.Address & ACPI_GAS_INTEL_ARG0_MWAIT_HINTMASK; 154 if (bootverbose) { 155 kprintf("C%d -> cpu specific C%d sub state %d\n", cx->type, 156 MWAIT_EAX_TO_CX(eax_hint), MWAIT_EAX_TO_CX_SUB(eax_hint)); 157 } 158 159 if (!cpu_mwait_hint_valid(eax_hint)) { 160 kprintf("C%d: invalid mwait hint 0x%08x\n", cx->type, eax_hint); 161 return EINVAL; 162 } 163 164 cx->md_arg0 = eax_hint; 165 cx->enter = acpi_cst_cx_mwait_enter; 166 167 if ((cx->gas.AccessWidth & ACPI_GAS_INTEL_ARG1_BM_STS) == 0) { 168 cpu_mwait_cx_no_bmsts(); 169 if (cx->type >= ACPI_STATE_C3) 170 cx->flags &= ~ACPI_CST_CX_FLAG_BM_STS; 171 } 172 173 return 0; 174 } 175 176 static void 177 acpi_cst_cx_mwait_enter(const struct acpi_cst_cx *cx) 178 { 179 struct globaldata *gd = mycpu; 180 int reqflags; 181 182 reqflags = gd->gd_reqflags; 183 if ((reqflags & RQF_IDLECHECK_WK_MASK) == 0) { 184 cpu_mmw_pause_int(&gd->gd_reqflags, reqflags, cx->md_arg0, 185 MWAIT_ECX_INTBRK); 186 } 187 } 188