ACPI P-State CPU driver: shorten function name
[dragonfly.git] / sys / platform / pc32 / acpica5 / acpi_pstate_machdep.c
1 /*
2  * Copyright (c) 2009 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/systm.h>
37 #include <sys/globaldata.h>
38
39 #include <machine/md_var.h>
40 #include <machine/cpufunc.h>
41 #include <machine/cpufreq.h>
42
43 #include "acpi.h"
44 #include "acpi_cpu_pstate.h"
45
46 #define AMD_APMI_HWPSTATE               0x80
47
48 #define AMD_MSR_PSTATE_CSR_MASK         0x7ULL
49 #define AMD1XH_MSR_PSTATE_CTL           0xc0010062
50 #define AMD1XH_MSR_PSTATE_ST            0xc0010063
51
52 #define AMD_MSR_PSTATE_EN               0x8000000000000000ULL
53
54 #define AMD10H_MSR_PSTATE_START         0xc0010064
55 #define AMD10H_MSR_PSTATE_COUNT         5
56
57 #define AMD0F_PST_CTL_FID(cval)         (((cval) >> 0)  & 0x3f)
58 #define AMD0F_PST_CTL_VID(cval)         (((cval) >> 6)  & 0x1f)
59 #define AMD0F_PST_CTL_VST(cval)         (((cval) >> 11) & 0x7f)
60 #define AMD0F_PST_CTL_MVS(cval)         (((cval) >> 18) & 0x3)
61 #define AMD0F_PST_CTL_PLLTIME(cval)     (((cval) >> 20) & 0x7f)
62 #define AMD0F_PST_CTL_RVO(cval)         (((cval) >> 28) & 0x3)
63 #define AMD0F_PST_CTL_IRT(cval)         (((cval) >> 30) & 0x3)
64
65 #define AMD0F_PST_ST_FID(sval)          (((sval) >> 0) & 0x3f)
66 #define AMD0F_PST_ST_VID(sval)          (((sval) >> 6) & 0x3f)
67
68 static const struct acpi_pst_md *
69                 acpi_pst_amd_probe(void);
70 static int      acpi_pst_amd_check_csr(const ACPI_RESOURCE_GENERIC_REGISTER *,
71                     const ACPI_RESOURCE_GENERIC_REGISTER *);
72 static int      acpi_pst_amd1x_check_pstates(const struct acpi_pstate *, int,
73                     uint32_t, uint32_t);
74 static int      acpi_pst_amd10_check_pstates(const struct acpi_pstate *, int);
75 static int      acpi_pst_amd0f_check_pstates(const struct acpi_pstate *, int);
76 static int      acpi_pst_amd1x_set_pstate(
77                     const ACPI_RESOURCE_GENERIC_REGISTER *,
78                     const ACPI_RESOURCE_GENERIC_REGISTER *,
79                     const struct acpi_pstate *);
80 static int      acpi_pst_amd0f_set_pstate(
81                     const ACPI_RESOURCE_GENERIC_REGISTER *,
82                     const ACPI_RESOURCE_GENERIC_REGISTER *,
83                     const struct acpi_pstate *);
84 static const struct acpi_pstate *
85                 acpi_pst_amd1x_get_pstate(
86                     const ACPI_RESOURCE_GENERIC_REGISTER *,
87                     const struct acpi_pstate *, int);
88 static const struct acpi_pstate *
89                 acpi_pst_amd0f_get_pstate(
90                     const ACPI_RESOURCE_GENERIC_REGISTER *,
91                     const struct acpi_pstate *, int);
92
93 static const struct acpi_pst_md acpi_pst_amd10h = {
94         .pmd_check_csr          = acpi_pst_amd_check_csr,
95         .pmd_check_pstates      = acpi_pst_amd10_check_pstates,
96         .pmd_set_pstate         = acpi_pst_amd1x_set_pstate,
97         .pmd_get_pstate         = acpi_pst_amd1x_get_pstate
98 };
99
100 static const struct acpi_pst_md acpi_pst_amd0fh = {
101         .pmd_check_csr          = acpi_pst_amd_check_csr,
102         .pmd_check_pstates      = acpi_pst_amd0f_check_pstates,
103         .pmd_set_pstate         = acpi_pst_amd0f_set_pstate,
104         .pmd_get_pstate         = acpi_pst_amd0f_get_pstate
105 };
106
107 const struct acpi_pst_md *
108 acpi_pst_md_probe(void)
109 {
110         if (strcmp(cpu_vendor, "AuthenticAMD") == 0)
111                 return acpi_pst_amd_probe();
112         return NULL;
113 }
114
115 static const struct acpi_pst_md *
116 acpi_pst_amd_probe(void)
117 {
118         uint32_t regs[4], ext_family;
119
120         if ((cpu_id & 0x00000f00) != 0x00000f00)
121                 return NULL;
122
123         /* Check whether APMI exists */
124         do_cpuid(0x80000000, regs);
125         if (regs[0] < 0x80000007)
126                 return NULL;
127
128         /* Fetch APMI */
129         do_cpuid(0x80000007, regs);
130
131         ext_family = cpu_id & 0x0ff00000;
132         switch (ext_family) {
133         case 0x00000000:        /* Family 0fh */
134                 if ((regs[3] & 0x06) == 0x06)
135                         return &acpi_pst_amd0fh;
136                 break;
137
138         case 0x00100000:        /* Family 10h */
139                 if (regs[3] & 0x80)
140                         return &acpi_pst_amd10h;
141                 break;
142
143         default:
144                 break;
145         }
146         return NULL;
147 }
148
149 static int
150 acpi_pst_amd_check_csr(const ACPI_RESOURCE_GENERIC_REGISTER *ctrl,
151                        const ACPI_RESOURCE_GENERIC_REGISTER *status)
152 {
153         if (ctrl->SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE) {
154                 kprintf("cpu%d: Invalid P-State control register\n", mycpuid);
155                 return EINVAL;
156         }
157         if (status->SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE) {
158                 kprintf("cpu%d: Invalid P-State status register\n", mycpuid);
159                 return EINVAL;
160         }
161         return 0;
162 }
163
164 static int
165 acpi_pst_amd1x_check_pstates(const struct acpi_pstate *pstates, int npstates,
166                              uint32_t msr_start, uint32_t msr_end)
167 {
168         int i;
169
170         /*
171          * Make sure that related MSR P-State registers are enabled.
172          *
173          * NOTE:
174          * We don't check status register value here;
175          * it will not be used.
176          */
177         for (i = 0; i < npstates; ++i) {
178                 uint64_t pstate;
179                 uint32_t msr;
180
181                 msr = msr_start +
182                       (pstates[i].st_cval & AMD_MSR_PSTATE_CSR_MASK);
183                 if (msr >= msr_end) {
184                         kprintf("cpu%d: MSR P-State register %#08x "
185                                 "does not exist\n", mycpuid, msr);
186                         return EINVAL;
187                 }
188
189                 pstate = rdmsr(msr);
190                 if ((pstate & AMD_MSR_PSTATE_EN) == 0) {
191                         kprintf("cpu%d: MSR P-State register %#08x "
192                                 "is not enabled\n", mycpuid, msr);
193                         return EINVAL;
194                 }
195         }
196         return 0;
197 }
198
199 static int
200 acpi_pst_amd10_check_pstates(const struct acpi_pstate *pstates, int npstates)
201 {
202         /* Only P0-P4 are supported */
203         if (npstates > AMD10H_MSR_PSTATE_COUNT) {
204                 kprintf("cpu%d: only P0-P4 is allowed\n", mycpuid);
205                 return EINVAL;
206         }
207
208         return acpi_pst_amd1x_check_pstates(pstates, npstates,
209                         AMD10H_MSR_PSTATE_START,
210                         AMD10H_MSR_PSTATE_START + AMD10H_MSR_PSTATE_COUNT);
211 }
212
213 static int
214 acpi_pst_amd1x_set_pstate(const ACPI_RESOURCE_GENERIC_REGISTER *ctrl __unused,
215                           const ACPI_RESOURCE_GENERIC_REGISTER *status __unused,
216                           const struct acpi_pstate *pstate)
217 {
218         uint64_t cval;
219
220         cval = pstate->st_cval & AMD_MSR_PSTATE_CSR_MASK;
221         wrmsr(AMD1XH_MSR_PSTATE_CTL, cval);
222
223         /*
224          * Don't check AMD1XH_MSR_PSTATE_ST here, since it is
225          * affected by various P-State limits.
226          *
227          * For details:
228          * AMD Family 10h Processor BKDG Rev 3.20 (#31116)
229          * 2.4.2.4 P-state Transition Behavior
230          */
231
232         return 0;
233 }
234
235 static const struct acpi_pstate *
236 acpi_pst_amd1x_get_pstate(const ACPI_RESOURCE_GENERIC_REGISTER *status __unused,
237                           const struct acpi_pstate *pstates, int npstates)
238 {
239         uint64_t sval;
240         int i;
241
242         sval = rdmsr(AMD1XH_MSR_PSTATE_ST) & AMD_MSR_PSTATE_CSR_MASK;
243         for (i = 0; i < npstates; ++i) {
244                 if ((pstates[i].st_sval & AMD_MSR_PSTATE_CSR_MASK) == sval)
245                         return &pstates[i];
246         }
247         return NULL;
248 }
249
250 static int
251 acpi_pst_amd0f_check_pstates(const struct acpi_pstate *pstates, int npstates)
252 {
253         struct amd0f_fidvid fv_max, fv_min;
254         int i;
255
256         amd0f_fidvid_limit(&fv_min, &fv_max);
257
258         for (i = 0; i < npstates; ++i) {
259                 const struct acpi_pstate *p = &pstates[i];
260                 uint32_t fid, vid, mvs, rvo;
261                 int mvs_mv, rvo_mv;
262
263                 fid = AMD0F_PST_CTL_FID(p->st_cval);
264                 vid = AMD0F_PST_CTL_VID(p->st_cval);
265
266                 if (fid > fv_max.fid || fid < fv_min.fid) {
267                         kprintf("cpu%d: Invalid FID %#x [%#x, %#x]\n",
268                                 mycpuid, fid, fv_min.fid, fv_max.fid);
269                         return EINVAL;
270                 }
271                 if (vid < fv_max.vid || vid > fv_min.vid) {
272                         kprintf("cpu%d: Invalid VID %#x [%#x, %#x]\n",
273                                 mycpuid, vid, fv_max.vid, fv_min.fid);
274                         return EINVAL;
275                 }
276
277                 mvs = AMD0F_PST_CTL_MVS(p->st_cval);
278                 rvo = AMD0F_PST_CTL_RVO(p->st_cval);
279
280                 /* Only 0 is allowed, i.e. 25mV stepping */
281                 if (mvs != 0) {
282                         kprintf("cpu%d: Invalid MVS %#x\n", mycpuid, mvs);
283                         return EINVAL;
284                 }
285
286                 /* -> mV */
287                 mvs_mv = 25 * (1 << mvs);
288                 rvo_mv = 25 * rvo;
289                 if (rvo_mv % mvs_mv != 0) {
290                         kprintf("cpu%d: Invalid MVS/RVO (%#x/%#x)\n",
291                                 mycpuid, mvs, rvo);
292                         return EINVAL;
293                 }
294         }
295         return 0;
296 }
297
298 static int
299 acpi_pst_amd0f_set_pstate(const ACPI_RESOURCE_GENERIC_REGISTER *ctrl __unused,
300                           const ACPI_RESOURCE_GENERIC_REGISTER *status __unused,
301                           const struct acpi_pstate *pstate)
302 {
303         struct amd0f_fidvid fv;
304         struct amd0f_xsit xsit;
305
306         fv.fid = AMD0F_PST_CTL_FID(pstate->st_cval);
307         fv.vid = AMD0F_PST_CTL_VID(pstate->st_cval);
308
309         xsit.rvo = AMD0F_PST_CTL_RVO(pstate->st_cval);
310         xsit.mvs = AMD0F_PST_CTL_MVS(pstate->st_cval);
311         xsit.vst = AMD0F_PST_CTL_VST(pstate->st_cval);
312         xsit.pll_time = AMD0F_PST_CTL_PLLTIME(pstate->st_cval);
313         xsit.irt = AMD0F_PST_CTL_IRT(pstate->st_cval);
314
315         return amd0f_set_fidvid(&fv, &xsit);
316 }
317
318 static const struct acpi_pstate *
319 acpi_pst_amd0f_get_pstate(const ACPI_RESOURCE_GENERIC_REGISTER * ctrl __unused,
320                           const struct acpi_pstate *pstates, int npstates)
321 {
322         struct amd0f_fidvid fv;
323         int error, i;
324
325         error = amd0f_get_fidvid(&fv);
326         if (error)
327                 return NULL;
328
329         for (i = 0; i < npstates; ++i) {
330                 const struct acpi_pstate *p = &pstates[i];
331
332                 if (fv.fid == AMD0F_PST_ST_FID(p->st_sval) &&
333                     fv.vid == AMD0F_PST_ST_VID(p->st_sval))
334                         return p;
335         }
336         return NULL;
337 }