acpi/pstate: Add tunable to allow BIOS whacking start/max FID/VID
[dragonfly.git] / sys / platform / pc64 / 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/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/specialreg.h>
44
45 #include "acpi.h"
46 #include "acpi_cpu_pstate.h"
47
48 #define AMD_APMI_HWPSTATE               0x80
49
50 #define AMD_MSR_PSTATE_CSR_MASK         0x7ULL
51 #define AMD1X_MSR_PSTATE_CTL            0xc0010062
52 #define AMD1X_MSR_PSTATE_ST             0xc0010063
53
54 #define AMD_MSR_PSTATE_EN               0x8000000000000000ULL
55
56 #define AMD10_MSR_PSTATE_START          0xc0010064
57 #define AMD10_MSR_PSTATE_COUNT          5
58
59 #define AMD0F_PST_CTL_FID(cval)         (((cval) >> 0)  & 0x3f)
60 #define AMD0F_PST_CTL_VID(cval)         (((cval) >> 6)  & 0x1f)
61 #define AMD0F_PST_CTL_VST(cval)         (((cval) >> 11) & 0x7f)
62 #define AMD0F_PST_CTL_MVS(cval)         (((cval) >> 18) & 0x3)
63 #define AMD0F_PST_CTL_PLLTIME(cval)     (((cval) >> 20) & 0x7f)
64 #define AMD0F_PST_CTL_RVO(cval)         (((cval) >> 28) & 0x3)
65 #define AMD0F_PST_CTL_IRT(cval)         (((cval) >> 30) & 0x3)
66
67 #define AMD0F_PST_ST_FID(sval)          (((sval) >> 0) & 0x3f)
68 #define AMD0F_PST_ST_VID(sval)          (((sval) >> 6) & 0x3f)
69
70 #define INTEL_MSR_MISC_ENABLE           0x1a0
71 #define INTEL_MSR_MISC_EST_EN           0x10000ULL
72
73 #define INTEL_MSR_PERF_STATUS           0x198
74 #define INTEL_MSR_PERF_CTL              0x199
75 #define INTEL_MSR_PERF_MASK             0xffffULL
76
77 static const struct acpi_pst_md *
78                 acpi_pst_amd_probe(void);
79 static int      acpi_pst_amd_check_csr(const struct acpi_pst_res *,
80                     const struct acpi_pst_res *);
81 static int      acpi_pst_amd1x_check_pstates(const struct acpi_pstate *, int,
82                     uint32_t, uint32_t);
83 static int      acpi_pst_amd10_check_pstates(const struct acpi_pstate *, int);
84 static int      acpi_pst_amd0f_check_pstates(const struct acpi_pstate *, int);
85 static int      acpi_pst_amd_init(const struct acpi_pst_res *,
86                     const struct acpi_pst_res *);
87 static int      acpi_pst_amd1x_set_pstate(const struct acpi_pst_res *,
88                     const struct acpi_pst_res *, const struct acpi_pstate *);
89 static int      acpi_pst_amd0f_set_pstate(const struct acpi_pst_res *,
90                     const struct acpi_pst_res *, const struct acpi_pstate *);
91 static const struct acpi_pstate *
92                 acpi_pst_amd1x_get_pstate(const struct acpi_pst_res *,
93                     const struct acpi_pstate *, int);
94 static const struct acpi_pstate *
95                 acpi_pst_amd0f_get_pstate(const struct acpi_pst_res *,
96                     const struct acpi_pstate *, int);
97
98 static const struct acpi_pst_md *
99                 acpi_pst_intel_probe(void);
100 static int      acpi_pst_intel_check_csr(const struct acpi_pst_res *,
101                     const struct acpi_pst_res *);
102 static int      acpi_pst_intel_check_pstates(const struct acpi_pstate *, int);
103 static int      acpi_pst_intel_init(const struct acpi_pst_res *,
104                     const struct acpi_pst_res *);
105 static int      acpi_pst_intel_set_pstate(const struct acpi_pst_res *,
106                     const struct acpi_pst_res *, const struct acpi_pstate *);
107 static const struct acpi_pstate *
108                 acpi_pst_intel_get_pstate(const struct acpi_pst_res *,
109                     const struct acpi_pstate *, int);
110
111 static int      acpi_pst_md_gas_asz(const ACPI_GENERIC_ADDRESS *);
112 static int      acpi_pst_md_gas_verify(const ACPI_GENERIC_ADDRESS *);
113 static uint32_t acpi_pst_md_res_read(const struct acpi_pst_res *);
114 static void     acpi_pst_md_res_write(const struct acpi_pst_res *, uint32_t);
115
116 static const struct acpi_pst_md acpi_pst_amd10 = {
117         .pmd_check_csr          = acpi_pst_amd_check_csr,
118         .pmd_check_pstates      = acpi_pst_amd10_check_pstates,
119         .pmd_init               = acpi_pst_amd_init,
120         .pmd_set_pstate         = acpi_pst_amd1x_set_pstate,
121         .pmd_get_pstate         = acpi_pst_amd1x_get_pstate
122 };
123
124 static const struct acpi_pst_md acpi_pst_amd0f = {
125         .pmd_check_csr          = acpi_pst_amd_check_csr,
126         .pmd_check_pstates      = acpi_pst_amd0f_check_pstates,
127         .pmd_init               = acpi_pst_amd_init,
128         .pmd_set_pstate         = acpi_pst_amd0f_set_pstate,
129         .pmd_get_pstate         = acpi_pst_amd0f_get_pstate
130 };
131
132 static const struct acpi_pst_md acpi_pst_intel = {
133         .pmd_check_csr          = acpi_pst_intel_check_csr,
134         .pmd_check_pstates      = acpi_pst_intel_check_pstates,
135         .pmd_init               = acpi_pst_intel_init,
136         .pmd_set_pstate         = acpi_pst_intel_set_pstate,
137         .pmd_get_pstate         = acpi_pst_intel_get_pstate
138 };
139
140 static int acpi_pst_stringent_check = 1;
141 TUNABLE_INT("hw.acpi.cpu.pstate.strigent_check", &acpi_pst_stringent_check);
142
143 const struct acpi_pst_md *
144 acpi_pst_md_probe(void)
145 {
146         if (strcmp(cpu_vendor, "AuthenticAMD") == 0)
147                 return acpi_pst_amd_probe();
148         else if (strcmp(cpu_vendor, "GenuineIntel") == 0)
149                 return acpi_pst_intel_probe();
150         return NULL;
151 }
152
153 static const struct acpi_pst_md *
154 acpi_pst_amd_probe(void)
155 {
156         uint32_t regs[4], ext_family;
157
158         if ((cpu_id & 0x00000f00) != 0x00000f00)
159                 return NULL;
160
161         /* Check whether APMI exists */
162         do_cpuid(0x80000000, regs);
163         if (regs[0] < 0x80000007)
164                 return NULL;
165
166         /* Fetch APMI */
167         do_cpuid(0x80000007, regs);
168
169         ext_family = cpu_id & 0x0ff00000;
170         switch (ext_family) {
171         case 0x00000000:        /* Family 0fh */
172                 if ((regs[3] & 0x06) == 0x06)
173                         return &acpi_pst_amd0f;
174                 break;
175
176         case 0x00100000:        /* Family 10h */
177                 if (regs[3] & 0x80)
178                         return &acpi_pst_amd10;
179                 break;
180
181         default:
182                 break;
183         }
184         return NULL;
185 }
186
187 static int
188 acpi_pst_amd_check_csr(const struct acpi_pst_res *ctrl,
189                        const struct acpi_pst_res *status)
190 {
191         if (ctrl->pr_gas.SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE) {
192                 kprintf("cpu%d: Invalid P-State control register\n", mycpuid);
193                 return EINVAL;
194         }
195         if (status->pr_gas.SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE) {
196                 kprintf("cpu%d: Invalid P-State status register\n", mycpuid);
197                 return EINVAL;
198         }
199         return 0;
200 }
201
202 static int
203 acpi_pst_amd1x_check_pstates(const struct acpi_pstate *pstates, int npstates,
204                              uint32_t msr_start, uint32_t msr_end)
205 {
206         int i;
207
208         /*
209          * Make sure that related MSR P-State registers are enabled.
210          *
211          * NOTE:
212          * We don't check status register value here;
213          * it will not be used.
214          */
215         for (i = 0; i < npstates; ++i) {
216                 uint64_t pstate;
217                 uint32_t msr;
218
219                 msr = msr_start +
220                       (pstates[i].st_cval & AMD_MSR_PSTATE_CSR_MASK);
221                 if (msr >= msr_end) {
222                         kprintf("cpu%d: MSR P-State register %#08x "
223                                 "does not exist\n", mycpuid, msr);
224                         return EINVAL;
225                 }
226
227                 pstate = rdmsr(msr);
228                 if ((pstate & AMD_MSR_PSTATE_EN) == 0) {
229                         kprintf("cpu%d: MSR P-State register %#08x "
230                                 "is not enabled\n", mycpuid, msr);
231                         return EINVAL;
232                 }
233         }
234         return 0;
235 }
236
237 static int
238 acpi_pst_amd10_check_pstates(const struct acpi_pstate *pstates, int npstates)
239 {
240         /* Only P0-P4 are supported */
241         if (npstates > AMD10_MSR_PSTATE_COUNT) {
242                 kprintf("cpu%d: only P0-P4 is allowed\n", mycpuid);
243                 return EINVAL;
244         }
245
246         return acpi_pst_amd1x_check_pstates(pstates, npstates,
247                         AMD10_MSR_PSTATE_START,
248                         AMD10_MSR_PSTATE_START + AMD10_MSR_PSTATE_COUNT);
249 }
250
251 static int
252 acpi_pst_amd1x_set_pstate(const struct acpi_pst_res *ctrl __unused,
253                           const struct acpi_pst_res *status __unused,
254                           const struct acpi_pstate *pstate)
255 {
256         uint64_t cval;
257
258         cval = pstate->st_cval & AMD_MSR_PSTATE_CSR_MASK;
259         wrmsr(AMD1X_MSR_PSTATE_CTL, cval);
260
261         /*
262          * Don't check AMD1X_MSR_PSTATE_ST here, since it is
263          * affected by various P-State limits.
264          *
265          * For details:
266          * AMD Family 10h Processor BKDG Rev 3.20 (#31116)
267          * 2.4.2.4 P-state Transition Behavior
268          */
269
270         return 0;
271 }
272
273 static const struct acpi_pstate *
274 acpi_pst_amd1x_get_pstate(const struct acpi_pst_res *status __unused,
275                           const struct acpi_pstate *pstates, int npstates)
276 {
277         uint64_t sval;
278         int i;
279
280         sval = rdmsr(AMD1X_MSR_PSTATE_ST) & AMD_MSR_PSTATE_CSR_MASK;
281         for (i = 0; i < npstates; ++i) {
282                 if ((pstates[i].st_sval & AMD_MSR_PSTATE_CSR_MASK) == sval)
283                         return &pstates[i];
284         }
285         return NULL;
286 }
287
288 static int
289 acpi_pst_amd0f_check_pstates(const struct acpi_pstate *pstates, int npstates)
290 {
291         struct amd0f_fidvid fv_max, fv_min;
292         int i;
293
294         amd0f_fidvid_limit(&fv_min, &fv_max);
295
296         if (fv_min.fid == fv_max.fid && fv_min.vid == fv_max.vid) {
297                 kprintf("cpu%d: only one P-State is supported\n", mycpuid);
298                 if (acpi_pst_stringent_check)
299                         return EOPNOTSUPP;
300         }
301
302         for (i = 0; i < npstates; ++i) {
303                 const struct acpi_pstate *p = &pstates[i];
304                 uint32_t fid, vid, mvs, rvo;
305                 int mvs_mv, rvo_mv;
306
307                 fid = AMD0F_PST_CTL_FID(p->st_cval);
308                 vid = AMD0F_PST_CTL_VID(p->st_cval);
309
310                 if (i == 0) {
311                         if (vid != fv_max.vid) {
312                                 kprintf("cpu%d: max VID mismatch "
313                                         "real %u, lim %d\n", mycpuid,
314                                         vid, fv_max.vid);
315                         }
316                         if (fid != fv_max.fid) {
317                                 kprintf("cpu%d: max FID mismatch "
318                                         "real %u, lim %d\n", mycpuid,
319                                         fid, fv_max.fid);
320                         }
321                 } else if (i == npstates - 1) {
322                         if (vid != fv_min.vid) {
323                                 kprintf("cpu%d: min VID mismatch "
324                                         "real %u, lim %d\n", mycpuid,
325                                         vid, fv_min.vid);
326                         }
327                         if (fid != fv_min.fid) {
328                                 kprintf("cpu%d: min FID mismatch "
329                                         "real %u, lim %d\n", mycpuid,
330                                         fid, fv_min.fid);
331                         }
332                 } else {
333                         if (fid >= fv_max.fid || fid < (fv_min.fid + 0x8)) {
334                                 kprintf("cpu%d: Invalid FID %#x, "
335                                         "out [%#x, %#x]\n", mycpuid, fid,
336                                         fv_min.fid + 0x8, fv_max.fid);
337                                 if (acpi_pst_stringent_check)
338                                         return EINVAL;
339                         }
340                         if (vid < fv_max.vid || vid > fv_min.vid) {
341                                 kprintf("cpu%d: Invalid VID %#x, "
342                                         "in [%#x, %#x]\n", mycpuid, vid,
343                                         fv_max.vid, fv_min.vid);
344                                 if (acpi_pst_stringent_check)
345                                         return EINVAL;
346                         }
347                 }
348
349                 mvs = AMD0F_PST_CTL_MVS(p->st_cval);
350                 rvo = AMD0F_PST_CTL_RVO(p->st_cval);
351
352                 /* Only 0 is allowed, i.e. 25mV stepping */
353                 if (mvs != 0) {
354                         kprintf("cpu%d: Invalid MVS %#x\n", mycpuid, mvs);
355                         return EINVAL;
356                 }
357
358                 /* -> mV */
359                 mvs_mv = 25 * (1 << mvs);
360                 rvo_mv = 25 * rvo;
361                 if (rvo_mv % mvs_mv != 0) {
362                         kprintf("cpu%d: Invalid MVS/RVO (%#x/%#x)\n",
363                                 mycpuid, mvs, rvo);
364                         return EINVAL;
365                 }
366         }
367         return 0;
368 }
369
370 static int
371 acpi_pst_amd0f_set_pstate(const struct acpi_pst_res *ctrl __unused,
372                           const struct acpi_pst_res *status __unused,
373                           const struct acpi_pstate *pstate)
374 {
375         struct amd0f_fidvid fv;
376         struct amd0f_xsit xsit;
377
378         fv.fid = AMD0F_PST_CTL_FID(pstate->st_cval);
379         fv.vid = AMD0F_PST_CTL_VID(pstate->st_cval);
380
381         xsit.rvo = AMD0F_PST_CTL_RVO(pstate->st_cval);
382         xsit.mvs = AMD0F_PST_CTL_MVS(pstate->st_cval);
383         xsit.vst = AMD0F_PST_CTL_VST(pstate->st_cval);
384         xsit.pll_time = AMD0F_PST_CTL_PLLTIME(pstate->st_cval);
385         xsit.irt = AMD0F_PST_CTL_IRT(pstate->st_cval);
386
387         return amd0f_set_fidvid(&fv, &xsit);
388 }
389
390 static const struct acpi_pstate *
391 acpi_pst_amd0f_get_pstate(const struct acpi_pst_res *status __unused,
392                           const struct acpi_pstate *pstates, int npstates)
393 {
394         struct amd0f_fidvid fv;
395         int error, i;
396
397         error = amd0f_get_fidvid(&fv);
398         if (error)
399                 return NULL;
400
401         for (i = 0; i < npstates; ++i) {
402                 const struct acpi_pstate *p = &pstates[i];
403
404                 if (fv.fid == AMD0F_PST_ST_FID(p->st_sval) &&
405                     fv.vid == AMD0F_PST_ST_VID(p->st_sval))
406                         return p;
407         }
408         return NULL;
409 }
410
411 static int
412 acpi_pst_amd_init(const struct acpi_pst_res *ctrl __unused,
413                   const struct acpi_pst_res *status __unused)
414 {
415         return 0;
416 }
417
418 static const struct acpi_pst_md *
419 acpi_pst_intel_probe(void)
420 {
421         uint32_t family;
422
423         if ((cpu_feature2 & CPUID2_EST) == 0)
424                 return NULL;
425
426         family = cpu_id & 0xf00;
427         if (family != 0xf00 && family != 0x600)
428                 return NULL;
429         return &acpi_pst_intel;
430 }
431
432 static int
433 acpi_pst_intel_check_csr(const struct acpi_pst_res *ctrl,
434                          const struct acpi_pst_res *status)
435 {
436         int error;
437
438         if (ctrl->pr_gas.SpaceId != status->pr_gas.SpaceId) {
439                 kprintf("cpu%d: P-State control(%d)/status(%d) registers have "
440                         "different SpaceId", mycpuid,
441                         ctrl->pr_gas.SpaceId, status->pr_gas.SpaceId);
442                 return EINVAL;
443         }
444
445         switch (ctrl->pr_gas.SpaceId) {
446         case ACPI_ADR_SPACE_FIXED_HARDWARE:
447                 if (ctrl->pr_res != NULL || status->pr_res != NULL) {
448                         /* XXX should panic() */
449                         kprintf("cpu%d: Allocated resource for fixed hardware "
450                                 "registers\n", mycpuid);
451                         return EINVAL;
452                 }
453                 break;
454
455         case ACPI_ADR_SPACE_SYSTEM_IO:
456                 if (ctrl->pr_res == NULL) {
457                         kprintf("cpu%d: ioport allocation failed for control "
458                                 "register\n", mycpuid);
459                         return ENXIO;
460                 }
461                 error = acpi_pst_md_gas_verify(&ctrl->pr_gas);
462                 if (error) {
463                         kprintf("cpu%d: Invalid control register GAS\n",
464                                 mycpuid);
465                         return error;
466                 }
467
468                 if (status->pr_res == NULL) {
469                         kprintf("cpu%d: ioport allocation failed for status "
470                                 "register\n", mycpuid);
471                         return ENXIO;
472                 }
473                 error = acpi_pst_md_gas_verify(&status->pr_gas);
474                 if (error) {
475                         kprintf("cpu%d: Invalid status register GAS\n",
476                                 mycpuid);
477                         return error;
478                 }
479                 break;
480
481         default:
482                 kprintf("cpu%d: Invalid P-State control/status register "
483                         "SpaceId %d\n", mycpuid, ctrl->pr_gas.SpaceId);
484                 return EOPNOTSUPP;
485         }
486         return 0;
487 }
488
489 static int
490 acpi_pst_intel_check_pstates(const struct acpi_pstate *pstates __unused,
491                              int npstates __unused)
492 {
493         return 0;
494 }
495
496 static int
497 acpi_pst_intel_init(const struct acpi_pst_res *ctrl __unused,
498                     const struct acpi_pst_res *status __unused)
499 {
500         uint32_t family, model;
501         uint64_t misc_enable;
502
503         family = cpu_id & 0xf00;
504         if (family == 0xf00) {
505                 /* EST enable bit is reserved in INTEL_MSR_MISC_ENABLE */
506                 return 0;
507         }
508         KKASSERT(family == 0x600);
509
510         model = ((cpu_id & 0xf0000) >> 12) | ((cpu_id & 0xf0) >> 4);
511         if (model < 0xd) {
512                 /* EST enable bit is reserved in INTEL_MSR_MISC_ENABLE */
513                 return 0;
514         }
515
516         misc_enable = rdmsr(INTEL_MSR_MISC_ENABLE);
517         if ((misc_enable & INTEL_MSR_MISC_EST_EN) == 0) {
518                 misc_enable |= INTEL_MSR_MISC_EST_EN;
519                 wrmsr(INTEL_MSR_MISC_ENABLE, misc_enable);
520
521                 misc_enable = rdmsr(INTEL_MSR_MISC_ENABLE);
522                 if ((misc_enable & INTEL_MSR_MISC_EST_EN) == 0) {
523                         kprintf("cpu%d: Can't enable EST\n", mycpuid);
524                         return EIO;
525                 }
526         }
527         return 0;
528 }
529
530 static int
531 acpi_pst_intel_set_pstate(const struct acpi_pst_res *ctrl,
532                           const struct acpi_pst_res *status __unused,
533                           const struct acpi_pstate *pstate)
534 {
535         if (ctrl->pr_res != NULL) {
536                 acpi_pst_md_res_write(ctrl, pstate->st_cval);
537         } else {
538                 uint64_t ctl;
539
540                 ctl = rdmsr(INTEL_MSR_PERF_CTL);
541                 ctl &= ~INTEL_MSR_PERF_MASK;
542                 ctl |= (pstate->st_cval & INTEL_MSR_PERF_MASK);
543                 wrmsr(INTEL_MSR_PERF_CTL, ctl);
544         }
545         return 0;
546 }
547
548 static const struct acpi_pstate *
549 acpi_pst_intel_get_pstate(const struct acpi_pst_res *status,
550                           const struct acpi_pstate *pstates, int npstates)
551 {
552         int i;
553
554         if (status->pr_res != NULL) {
555                 uint32_t st;
556
557                 st = acpi_pst_md_res_read(status);
558                 for (i = 0; i < npstates; ++i) {
559                         if (pstates[i].st_sval == st)
560                                 return &pstates[i];
561                 }
562         } else {
563                 uint64_t sval;
564
565                 sval = rdmsr(INTEL_MSR_PERF_STATUS) & INTEL_MSR_PERF_MASK;
566                 for (i = 0; i < npstates; ++i) {
567                         if ((pstates[i].st_sval & INTEL_MSR_PERF_MASK) == sval)
568                                 return &pstates[i];
569                 }
570         }
571         return NULL;
572 }
573
574 static int
575 acpi_pst_md_gas_asz(const ACPI_GENERIC_ADDRESS *gas)
576 {
577         int asz;
578
579         if (gas->AccessWidth != 0)
580                 asz = gas->AccessWidth;
581         else
582                 asz = gas->BitWidth / NBBY;
583         switch (asz) {
584         case 1:
585         case 2:
586         case 4:
587                 break;
588         default:
589                 asz = 0;
590                 break;
591         }
592         return asz;
593 }
594
595 static int
596 acpi_pst_md_gas_verify(const ACPI_GENERIC_ADDRESS *gas)
597 {
598         int reg, end, asz;
599
600         if (gas->BitOffset % NBBY != 0)
601                 return EINVAL;
602
603         end = gas->BitWidth / NBBY;
604         reg = gas->BitOffset / NBBY;
605
606         if (reg >= end)
607                 return EINVAL;
608
609         asz = acpi_pst_md_gas_asz(gas);
610         if (asz == 0)
611                 return EINVAL;
612
613         if (reg + asz > end)
614                 return EINVAL;
615         return 0;
616 }
617
618 static uint32_t
619 acpi_pst_md_res_read(const struct acpi_pst_res *res)
620 {
621         int asz, reg;
622
623         KKASSERT(res->pr_res != NULL);
624         asz = acpi_pst_md_gas_asz(&res->pr_gas);
625         reg = res->pr_gas.BitOffset / NBBY;
626
627         switch (asz) {
628         case 1:
629                 return bus_space_read_1(res->pr_bt, res->pr_bh, reg);
630         case 2:
631                 return bus_space_read_2(res->pr_bt, res->pr_bh, reg);
632         case 4:
633                 return bus_space_read_4(res->pr_bt, res->pr_bh, reg);
634         }
635         panic("unsupported access width %d\n", asz);
636
637         /* NEVER REACHED */
638         return 0;
639 }
640
641 static void
642 acpi_pst_md_res_write(const struct acpi_pst_res *res, uint32_t val)
643 {
644         int asz, reg;
645
646         KKASSERT(res->pr_res != NULL);
647         asz = acpi_pst_md_gas_asz(&res->pr_gas);
648         reg = res->pr_gas.BitOffset / NBBY;
649
650         switch (asz) {
651         case 1:
652                 bus_space_write_1(res->pr_bt, res->pr_bh, reg, val);
653                 break;
654         case 2:
655                 bus_space_write_2(res->pr_bt, res->pr_bh, reg, val);
656                 break;
657         case 4:
658                 bus_space_write_4(res->pr_bt, res->pr_bh, reg, val);
659                 break;
660         default:
661                 panic("unsupported access width %d\n", asz);
662         }
663 }