VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/DevACPI.cpp@ 12630

Last change on this file since 12630 was 12431, checked in by vboxsync, 16 years ago

DevACPI: r=bird: C++ is ring-3 only. class names are not all uppercase. doxygen comments starts with / and are used whereever possible.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 67.7 KB
Line 
1/* $Id: DevACPI.cpp 12431 2008-09-12 15:29:01Z vboxsync $ */
2/** @file
3 * DevACPI - Advanced Configuration and Power Interface (ACPI) Device.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#define LOG_GROUP LOG_GROUP_DEV_ACPI
23#include <VBox/pdmdev.h>
24#include <VBox/log.h>
25#include <iprt/assert.h>
26#include <iprt/asm.h>
27#ifdef IN_RING3
28# include <iprt/alloc.h>
29# include <iprt/string.h>
30#endif /* IN_RING3 */
31
32#include "../Builtins.h"
33
34#ifdef LOG_ENABLED
35# define DEBUG_ACPI
36#endif
37
38/* the compiled DSL */
39#if defined(IN_RING3) && !defined(VBOX_DEVICE_STRUCT_TESTCASE)
40#include <vboxaml.hex>
41#endif /* !IN_RING3 */
42
43#define IO_READ_PROTO(name) \
44 PDMBOTHCBDECL(int) name (PPDMDEVINS pDevIns, void *pvUser, \
45 RTIOPORT Port, uint32_t *pu32, unsigned cb)
46
47#define IO_WRITE_PROTO(name) \
48 PDMBOTHCBDECL(int) name (PPDMDEVINS pDevIns, void *pvUser, \
49 RTIOPORT Port, uint32_t u32, unsigned cb)
50
51#define DEBUG_HEX 0x3000
52#define DEBUG_CHR 0x3001
53
54#define PM_TMR_FREQ 3579545
55#define PM1a_EVT_BLK 0x00004000
56#define PM1b_EVT_BLK 0x00000000 /**< not supported */
57#define PM1a_CTL_BLK 0x00004004
58#define PM1b_CTL_BLK 0x00000000 /**< not supported */
59#define PM2_CTL_BLK 0x00000000 /**< not supported */
60#define PM_TMR_BLK 0x00004008
61#define GPE0_BLK 0x00004020
62#define GPE1_BLK 0x00000000 /**< not supported */
63#define BAT_INDEX 0x00004040
64#define BAT_DATA 0x00004044
65#define SYSI_INDEX 0x00004048
66#define SYSI_DATA 0x0000404c
67#define ACPI_RESET_BLK 0x00004050
68#define FDC_STATUS 0x00004054
69
70/* PM1x status register bits */
71#define TMR_STS RT_BIT(0)
72#define RSR1_STS (RT_BIT(1) | RT_BIT(2) | RT_BIT(3))
73#define BM_STS RT_BIT(4)
74#define GBL_STS RT_BIT(5)
75#define RSR2_STS (RT_BIT(6) | RT_BIT(7))
76#define PWRBTN_STS RT_BIT(8)
77#define SLPBTN_STS RT_BIT(9)
78#define RTC_STS RT_BIT(10)
79#define IGN_STS RT_BIT(11)
80#define RSR3_STS (RT_BIT(12) | RT_BIT(13) | RT_BIT(14))
81#define WAK_STS RT_BIT(15)
82#define RSR_STS (RSR1_STS | RSR2_STS | RSR3_STS)
83
84/* PM1x enable register bits */
85#define TMR_EN RT_BIT(0)
86#define RSR1_EN (RT_BIT(1) | RT_BIT(2) | RT_BIT(3) | RT_BIT(4))
87#define GBL_EN RT_BIT(5)
88#define RSR2_EN (RT_BIT(6) | RT_BIT(7))
89#define PWRBTN_EN RT_BIT(8)
90#define SLPBTN_EN RT_BIT(9)
91#define RTC_EN RT_BIT(10)
92#define RSR3_EN (RT_BIT(11) | RT_BIT(12) | RT_BIT(13) | RT_BIT(14) | RT_BIT(15))
93#define RSR_EN (RSR1_EN | RSR2_EN | RSR3_EN)
94#define IGN_EN 0
95
96/* PM1x control register bits */
97#define SCI_EN RT_BIT(0)
98#define BM_RLD RT_BIT(1)
99#define GBL_RLS RT_BIT(2)
100#define RSR1_CNT (RT_BIT(3) | RT_BIT(4) | RT_BIT(5) | RT_BIT(6) | RT_BIT(7) | RT_BIT(8))
101#define IGN_CNT RT_BIT(9)
102#define SLP_TYPx_SHIFT 10
103#define SLP_TYPx_MASK 7
104#define SLP_EN RT_BIT(13)
105#define RSR2_CNT (RT_BIT(14) | RT_BIT(15))
106#define RSR_CNT (RSR1_CNT | RSR2_CNT)
107
108#define GPE0_BATTERY_INFO_CHANGED RT_BIT(0)
109
110enum
111{
112 BAT_STATUS_STATE = 0x00, /**< BST battery state */
113 BAT_STATUS_PRESENT_RATE = 0x01, /**< BST battery present rate */
114 BAT_STATUS_REMAINING_CAPACITY = 0x02, /**< BST battery remaining capacity */
115 BAT_STATUS_PRESENT_VOLTAGE = 0x03, /**< BST battery present voltage */
116 BAT_INFO_UNITS = 0x04, /**< BIF power unit */
117 BAT_INFO_DESIGN_CAPACITY = 0x05, /**< BIF design capacity */
118 BAT_INFO_LAST_FULL_CHARGE_CAPACITY = 0x06, /**< BIF last full charge capacity */
119 BAT_INFO_TECHNOLOGY = 0x07, /**< BIF battery technology */
120 BAT_INFO_DESIGN_VOLTAGE = 0x08, /**< BIF design voltage */
121 BAT_INFO_DESIGN_CAPACITY_OF_WARNING = 0x09, /**< BIF design capacity of warning */
122 BAT_INFO_DESIGN_CAPACITY_OF_LOW = 0x0A, /**< BIF design capacity of low */
123 BAT_INFO_CAPACITY_GRANULARITY_1 = 0x0B, /**< BIF battery capacity granularity 1 */
124 BAT_INFO_CAPACITY_GRANULARITY_2 = 0x0C, /**< BIF battery capacity granularity 2 */
125 BAT_DEVICE_STATUS = 0x0D, /**< STA device status */
126 BAT_POWER_SOURCE = 0x0E, /**< PSR power source */
127 BAT_INDEX_LAST
128};
129
130enum
131{
132 SYSTEM_INFO_INDEX_MEMORY_LENGTH = 0,
133 SYSTEM_INFO_INDEX_USE_IOAPIC = 1,
134 SYSTEM_INFO_INDEX_LAST = 2,
135 SYSTEM_INFO_INDEX_INVALID = 0x80,
136 SYSTEM_INFO_INDEX_VALID = 0x200
137};
138
139#define AC_OFFLINE 0
140#define AC_ONLINE 1
141
142#define BAT_TECH_PRIMARY 1
143#define BAT_TECH_SECONDARY 2
144
145#define BAT_STATUS_DISCHARGING_MASK RT_BIT(0)
146#define BAT_STATUS_CHARGING_MASK RT_BIT(1)
147#define BAT_STATUS_CRITICAL_MASK RT_BIT(2)
148
149#define STA_DEVICE_PRESENT_MASK RT_BIT(0)
150#define STA_DEVICE_ENABLED_MASK RT_BIT(1)
151#define STA_DEVICE_SHOW_IN_UI_MASK RT_BIT(2)
152#define STA_DEVICE_FUNCTIONING_PROPERLY_MASK RT_BIT(3)
153#define STA_BATTERY_PRESENT_MASK RT_BIT(4)
154
155struct ACPIState
156{
157 PCIDevice dev;
158 uint16_t pm1a_en;
159 uint16_t pm1a_sts;
160 uint16_t pm1a_ctl;
161 /** Number of logical CPUs in guest */
162 uint16_t cCpus;
163 int64_t pm_timer_initial;
164 PTMTIMERR3 tsR3;
165 PTMTIMERR0 tsR0;
166 PTMTIMERRC tsRC;
167
168 uint32_t gpe0_en;
169 uint32_t gpe0_sts;
170
171 unsigned int uBatteryIndex;
172 uint32_t au8BatteryInfo[13];
173
174 unsigned int uSystemInfoIndex;
175 uint64_t u64RamSize;
176
177 /** Current ACPI S* state. We support S0 and S5 */
178 uint32_t uSleepState;
179 uint8_t au8RSDPPage[0x1000];
180 /** This is a workaround for incorrect index field handling by Intels ACPICA.
181 * The system info _INI method writes to offset 0x200. We either observe a
182 * write request to index 0x80 (in that case we don't change the index) or a
183 * write request to offset 0x200 (in that case we divide the index value by
184 * 4. Note that the _STA method is sometimes called prior to the _INI method
185 * (ACPI spec 6.3.7, _STA). See the special case for BAT_DEVICE_STATUS in
186 * acpiBatIndexWrite() for handling this. */
187 uint8_t u8IndexShift;
188 uint8_t u8UseIOApic;
189 uint8_t u8UseFdc;
190 bool fPowerButtonHandled;
191
192 /** ACPI port base interface. */
193 PDMIBASE IBase;
194 /** ACPI port interface. */
195 PDMIACPIPORT IACPIPort;
196 /** Pointer to the device instance. */
197 PPDMDEVINSR3 pDevIns;
198 /** Pointer to the driver base interface */
199 R3PTRTYPE(PPDMIBASE) pDrvBase;
200 /** Pointer to the driver connector interface */
201 R3PTRTYPE(PPDMIACPICONNECTOR) pDrv;
202};
203
204#pragma pack(1)
205
206/** Generic Address Structure (see ACPIspec 3.0, 5.2.3.1) */
207struct ACPIGENADDR
208{
209 uint8_t u8AddressSpaceId; /**< 0=sys, 1=IO, 2=PCICfg, 3=emb, 4=SMBus */
210 uint8_t u8RegisterBitWidth; /**< size in bits of the given register */
211 uint8_t u8RegisterBitOffset; /**< bit offset of register */
212 uint8_t u8AccessSize; /**< 1=byte, 2=word, 3=dword, 4=qword */
213 uint64_t u64Address; /**< 64-bit address of register */
214};
215AssertCompileSize(ACPIGENADDR, 12);
216
217/** Root System Description Pointer */
218struct ACPITBLRSDP
219{
220 uint8_t au8Signature[8]; /**< 'RSD PTR ' */
221 uint8_t u8Checksum; /**< checksum for the first 20 bytes */
222 uint8_t au8OemId[6]; /**< OEM-supplied identifier */
223 uint8_t u8Revision; /**< revision number, currently 2 */
224#define ACPI_REVISION 2 /**< ACPI 3.0 */
225 uint32_t u32RSDT; /**< phys addr of RSDT */
226 uint32_t u32Length; /**< bytes of this table */
227 uint64_t u64XSDT; /**< 64-bit phys addr of XSDT */
228 uint8_t u8ExtChecksum; /**< checksum of entire table */
229 uint8_t u8Reserved[3]; /**< reserved */
230};
231AssertCompileSize(ACPITBLRSDP, 36);
232
233/** System Description Table Header */
234struct ACPITBLHEADER
235{
236 uint8_t au8Signature[4]; /**< table identifier */
237 uint32_t u32Length; /**< length of the table including header */
238 uint8_t u8Revision; /**< revision number */
239 uint8_t u8Checksum; /**< all fields inclusive this add to zero */
240 uint8_t au8OemId[6]; /**< OEM-supplied string */
241 uint8_t au8OemTabId[8]; /**< to identify the particular data table */
242 uint32_t u32OemRevision; /**< OEM-supplied revision number */
243 uint8_t au8CreatorId[4]; /**< ID for the ASL compiler */
244 uint32_t u32CreatorRev; /**< revision for the ASL compiler */
245};
246AssertCompileSize(ACPITBLHEADER, 36);
247
248/** Root System Description Table */
249struct ACPITBLRSDT
250{
251 ACPITBLHEADER header;
252 uint32_t u32Entry[1]; /**< array of phys. addresses to other tables */
253};
254AssertCompileSize(ACPITBLRSDT, 40);
255
256/** Extended System Description Table */
257struct ACPITBLXSDT
258{
259 ACPITBLHEADER header;
260 uint64_t u64Entry[1]; /**< array of phys. addresses to other tables */
261};
262AssertCompileSize(ACPITBLXSDT, 44);
263
264/** Fixed ACPI Description Table */
265struct ACPITBLFADT
266{
267 ACPITBLHEADER header;
268 uint32_t u32FACS; /**< phys. address of FACS */
269 uint32_t u32DSDT; /**< phys. address of DSDT */
270 uint8_t u8IntModel; /**< was eleminated in ACPI 2.0 */
271#define INT_MODEL_DUAL_PIC 1 /**< for ACPI 2+ */
272#define INT_MODEL_MULTIPLE_APIC 2
273 uint8_t u8PreferredPMProfile; /**< preferred power management profile */
274 uint16_t u16SCIInt; /**< system vector the SCI is wired in 8259 mode */
275#define SCI_INT 9
276 uint32_t u32SMICmd; /**< system port address of SMI command port */
277#define SMI_CMD 0x0000442e
278 uint8_t u8AcpiEnable; /**< SMICmd val to disable ownship of ACPIregs */
279#define ACPI_ENABLE 0xa1
280 uint8_t u8AcpiDisable; /**< SMICmd val to re-enable ownship of ACPIregs */
281#define ACPI_DISABLE 0xa0
282 uint8_t u8S4BIOSReq; /**< SMICmd val to enter S4BIOS state */
283 uint8_t u8PStateCnt; /**< SMICmd val to assume processor performance
284 state control responsibility */
285 uint32_t u32PM1aEVTBLK; /**< port addr of PM1a event regs block */
286 uint32_t u32PM1bEVTBLK; /**< port addr of PM1b event regs block */
287 uint32_t u32PM1aCTLBLK; /**< port addr of PM1a control regs block */
288 uint32_t u32PM1bCTLBLK; /**< port addr of PM1b control regs block */
289 uint32_t u32PM2CTLBLK; /**< port addr of PM2 control regs block */
290 uint32_t u32PMTMRBLK; /**< port addr of PMTMR regs block */
291 uint32_t u32GPE0BLK; /**< port addr of gen-purp event 0 regs block */
292 uint32_t u32GPE1BLK; /**< port addr of gen-purp event 1 regs block */
293 uint8_t u8PM1EVTLEN; /**< bytes decoded by PM1a_EVT_BLK. >= 4 */
294 uint8_t u8PM1CTLLEN; /**< bytes decoded by PM1b_CNT_BLK. >= 2 */
295 uint8_t u8PM2CTLLEN; /**< bytes decoded by PM2_CNT_BLK. >= 1 or 0 */
296 uint8_t u8PMTMLEN; /**< bytes decoded by PM_TMR_BLK. ==4 */
297 uint8_t u8GPE0BLKLEN; /**< bytes decoded by GPE0_BLK. %2==0 */
298#define GPE0_BLK_LEN 2
299 uint8_t u8GPE1BLKLEN; /**< bytes decoded by GPE1_BLK. %2==0 */
300#define GPE1_BLK_LEN 0
301 uint8_t u8GPE1BASE; /**< offset of GPE1 based events */
302#define GPE1_BASE 0
303 uint8_t u8CSTCNT; /**< SMICmd val to indicate OS supp for C states */
304 uint16_t u16PLVL2LAT; /**< us to enter/exit C2. >100 => unsupported */
305#define P_LVL2_LAT 101 /**< C2 state not supported */
306 uint16_t u16PLVL3LAT; /**< us to enter/exit C3. >1000 => unsupported */
307#define P_LVL3_LAT 1001 /**< C3 state not supported */
308 uint16_t u16FlushSize; /**< # of flush strides to read to flush dirty
309 lines from any processors memory caches */
310#define FLUSH_SIZE 0 /**< Ignored if WBVIND set in FADT_FLAGS */
311 uint16_t u16FlushStride; /**< cache line width */
312#define FLUSH_STRIDE 0 /**< Ignored if WBVIND set in FADT_FLAGS */
313 uint8_t u8DutyOffset;
314 uint8_t u8DutyWidth;
315 uint8_t u8DayAlarm; /**< RTC CMOS RAM index of day-of-month alarm */
316 uint8_t u8MonAlarm; /**< RTC CMOS RAM index of month-of-year alarm */
317 uint8_t u8Century; /**< RTC CMOS RAM index of century */
318 uint16_t u16IAPCBOOTARCH; /**< IA-PC boot architecture flags */
319#define IAPC_BOOT_ARCH_LEGACY_DEV RT_BIT(0) /**< legacy devices present such as LPT
320 (COM too?) */
321#define IAPC_BOOT_ARCH_8042 RT_BIT(1) /**< legacy keyboard device present */
322#define IAPC_BOOT_ARCH_NO_VGA RT_BIT(2) /**< VGA not present */
323 uint8_t u8Must0_0; /**< must be 0 */
324 uint32_t u32Flags; /**< fixed feature flags */
325#define FADT_FL_WBINVD RT_BIT(0) /**< emulation of WBINVD available */
326#define FADT_FL_WBINVD_FLUSH RT_BIT(1)
327#define FADT_FL_PROC_C1 RT_BIT(2) /**< 1=C1 supported on all processors */
328#define FADT_FL_P_LVL2_UP RT_BIT(3) /**< 1=C2 works on SMP and UNI systems */
329#define FADT_FL_PWR_BUTTON RT_BIT(4) /**< 1=power button handled as ctrl method dev */
330#define FADT_FL_SLP_BUTTON RT_BIT(5) /**< 1=sleep button handled as ctrl method dev */
331#define FADT_FL_FIX_RTC RT_BIT(6) /**< 0=RTC wake status in fixed register */
332#define FADT_FL_RTC_S4 RT_BIT(7) /**< 1=RTC can wake system from S4 */
333#define FADT_FL_TMR_VAL_EXT RT_BIT(8) /**< 1=TMR_VAL implemented as 32 bit */
334#define FADT_FL_DCK_CAP RT_BIT(9) /**< 0=system cannot support docking */
335#define FADT_FL_RESET_REG_SUP RT_BIT(10) /**< 1=system supports system resets */
336#define FADT_FL_SEALED_CASE RT_BIT(11) /**< 1=case is sealed */
337#define FADT_FL_HEADLESS RT_BIT(12) /**< 1=system cannot detect moni/keyb/mouse */
338#define FADT_FL_CPU_SW_SLP RT_BIT(13)
339#define FADT_FL_PCI_EXT_WAK RT_BIT(14) /**< 1=system supports PCIEXP_WAKE_STS */
340#define FADT_FL_USE_PLATFORM_CLOCK RT_BIT(15) /**< 1=system has ACPI PM timer */
341#define FADT_FL_S4_RTC_STS_VALID RT_BIT(16) /**< 1=RTC_STS flag is valid when waking from S4 */
342#define FADT_FL_REMOVE_POWER_ON_CAPABLE RT_BIT(17) /**< 1=platform can remote power on */
343#define FADT_FL_FORCE_APIC_CLUSTER_MODEL RT_BIT(18)
344#define FADT_FL_FORCE_APIC_PHYS_DEST_MODE RT_BIT(19)
345 ACPIGENADDR ResetReg; /**< ext addr of reset register */
346 uint8_t u8ResetVal; /**< ResetReg value to reset the system */
347#define ACPI_RESET_REG_VAL 0x10
348 uint8_t au8Must0_1[3]; /**< must be 0 */
349 uint64_t u64XFACS; /**< 64-bit phys address of FACS */
350 uint64_t u64XDSDT; /**< 64-bit phys address of DSDT */
351 ACPIGENADDR X_PM1aEVTBLK; /**< ext addr of PM1a event regs block */
352 ACPIGENADDR X_PM1bEVTBLK; /**< ext addr of PM1b event regs block */
353 ACPIGENADDR X_PM1aCTLBLK; /**< ext addr of PM1a control regs block */
354 ACPIGENADDR X_PM1bCTLBLK; /**< ext addr of PM1b control regs block */
355 ACPIGENADDR X_PM2CTLBLK; /**< ext addr of PM2 control regs block */
356 ACPIGENADDR X_PMTMRBLK; /**< ext addr of PMTMR control regs block */
357 ACPIGENADDR X_GPE0BLK; /**< ext addr of GPE1 regs block */
358 ACPIGENADDR X_GPE1BLK; /**< ext addr of GPE1 regs block */
359};
360AssertCompileSize(ACPITBLFADT, 244);
361
362/** Firmware ACPI Control Structure */
363struct ACPITBLFACS
364{
365 uint8_t au8Signature[4]; /**< 'FACS' */
366 uint32_t u32Length; /**< bytes of entire FACS structure >= 64 */
367 uint32_t u32HWSignature; /**< systems HW signature at last boot */
368 uint32_t u32FWVector; /**< address of waking vector */
369 uint32_t u32GlobalLock; /**< global lock to sync HW/SW */
370 uint32_t u32Flags; /**< FACS flags */
371 uint64_t u64X_FWVector; /**< 64-bit waking vector */
372 uint8_t u8Version; /**< version of this table */
373 uint8_t au8Reserved[31]; /**< zero */
374};
375AssertCompileSize(ACPITBLFACS, 64);
376
377/** Processor Local APIC Structure */
378struct ACPITBLLAPIC
379{
380 uint8_t u8Type; /**< 0 = LAPIC */
381 uint8_t u8Length; /**< 8 */
382 uint8_t u8ProcId; /**< processor ID */
383 uint8_t u8ApicId; /**< local APIC ID */
384 uint32_t u32Flags; /**< Flags */
385#define LAPIC_ENABLED 0x1
386};
387AssertCompileSize(ACPITBLLAPIC, 8);
388
389/** I/O APIC Structure */
390struct ACPITBLIOAPIC
391{
392 uint8_t u8Type; /**< 1 == I/O APIC */
393 uint8_t u8Length; /**< 12 */
394 uint8_t u8IOApicId; /**< I/O APIC ID */
395 uint8_t u8Reserved; /**< 0 */
396 uint32_t u32Address; /**< phys address to access I/O APIC */
397 uint32_t u32GSIB; /**< global system interrupt number to start */
398};
399AssertCompileSize(ACPITBLIOAPIC, 12);
400
401#ifdef VBOX_WITH_SMP_GUESTS
402#ifdef IN_RING3 /**@todo r=bird: Move this down to where it's used. */
403
404# define PCAT_COMPAT 0x1 /**< system has also a dual-8259 setup */
405
406/**
407 * Multiple APIC Description Table.
408 *
409 * This structure looks somewhat convoluted due layout of MADT table in MP case.
410 * There extpected to be multiple LAPIC records for each CPU, thus we cannot
411 * use regular C structure and proxy to raw memory instead.
412 */
413class AcpiTableMADT
414{
415 /**
416 * All actual data stored in dynamically allocated memory pointed by this field.
417 */
418 uint8_t* pData;
419 /**
420 * Number of CPU entries in this MADT.
421 */
422 uint32_t cCpus;
423
424 public:
425 /**
426 * Address of ACPI header
427 */
428 inline ACPITBLHEADER* header_addr() const
429 {
430 return (ACPITBLHEADER*)pData;
431 }
432
433 /**
434 * Address of local APIC for each CPU. Note that different CPUs address different LAPICs,
435 * although address is the same for all of them.
436 */
437 inline uint32_t* u32LAPIC_addr() const
438 {
439 return (uint32_t*)(header_addr() + 1);
440 }
441
442 /**
443 * Address of APIC flags
444 */
445 inline uint32_t* u32Flags_addr() const
446 {
447 return (uint32_t*)(u32LAPIC_addr() + 1);
448 }
449
450 /**
451 * Address of per-CPU LAPIC descriptions
452 */
453 inline ACPITBLLAPIC* LApics_addr() const
454 {
455 return (ACPITBLLAPIC*)(u32Flags_addr() + 1);
456 }
457
458 /**
459 * Address of IO APIC description
460 */
461 inline ACPITBLIOAPIC* IOApic_addr() const
462 {
463 return (ACPITBLIOAPIC*)(LApics_addr() + cCpus);
464 }
465
466 /**
467 * Size of MADT.
468 * Note that this function assumes IOApic to be the last field in structure.
469 */
470 inline uint32_t size() const
471 {
472 return (uint8_t*)(IOApic_addr() + 1)-(uint8_t*)header_addr();
473 }
474
475 /**
476 * Raw data of MADT.
477 */
478 inline const uint8_t* data() const
479 {
480 return pData;
481 }
482
483 /**
484 * Size of MADT for given ACPI config, useful to compute layout.
485 */
486 static uint32_t sizeFor(ACPIState *s)
487 {
488 return AcpiTableMADT(s->cCpus).size();
489 }
490
491 /*
492 * Constructor, only works in Ring 3, doesn't look like a big deal.
493 */
494 AcpiTableMADT(uint16_t cpus)
495 {
496 cCpus = cpus;
497 pData = 0;
498 uint32_t sSize = size();
499 pData = (uint8_t*)RTMemAllocZ(sSize);
500 }
501
502 ~AcpiTableMADT()
503 {
504 RTMemFree(pData);
505 }
506};
507#endif /* IN_RING3 */
508
509#else /* !VBOX_WITH_SMP_GUESTS */
510/** Multiple APIC Description Table */
511struct ACPITBLMADT
512{
513 ACPITBLHEADER header;
514 uint32_t u32LAPIC; /**< local APIC address */
515 uint32_t u32Flags; /**< Flags */
516#define PCAT_COMPAT 0x1 /**< system has also a dual-8259 setup */
517 ACPITBLLAPIC LApic;
518 ACPITBLIOAPIC IOApic;
519};
520AssertCompileSize(ACPITBLMADT, 64);
521#endif /* !VBOX_WITH_SMP_GUESTS */
522
523#pragma pack()
524
525
526#ifndef VBOX_DEVICE_STRUCT_TESTCASE
527__BEGIN_DECLS
528IO_READ_PROTO (acpiPMTmrRead);
529#ifdef IN_RING3
530IO_READ_PROTO (acpiPm1aEnRead);
531IO_WRITE_PROTO (acpiPM1aEnWrite);
532IO_READ_PROTO (acpiPm1aStsRead);
533IO_WRITE_PROTO (acpiPM1aStsWrite);
534IO_READ_PROTO (acpiPm1aCtlRead);
535IO_WRITE_PROTO (acpiPM1aCtlWrite);
536IO_WRITE_PROTO (acpiSmiWrite);
537IO_WRITE_PROTO (acpiBatIndexWrite);
538IO_READ_PROTO (acpiBatDataRead);
539IO_READ_PROTO (acpiFdcStatusRead);
540IO_READ_PROTO (acpiSysInfoDataRead);
541IO_WRITE_PROTO (acpiSysInfoDataWrite);
542IO_READ_PROTO (acpiGpe0EnRead);
543IO_WRITE_PROTO (acpiGpe0EnWrite);
544IO_READ_PROTO (acpiGpe0StsRead);
545IO_WRITE_PROTO (acpiGpe0StsWrite);
546IO_WRITE_PROTO (acpiResetWrite);
547# ifdef DEBUG_ACPI
548IO_WRITE_PROTO (acpiDhexWrite);
549IO_WRITE_PROTO (acpiDchrWrite);
550# endif
551#endif
552__END_DECLS
553
554#ifdef IN_RING3
555
556/* Simple acpiChecksum: all the bytes must add up to 0. */
557static uint8_t acpiChecksum (const uint8_t * const data, uint32_t len)
558{
559 uint8_t sum = 0;
560 for (size_t i = 0; i < len; ++i)
561 sum += data[i];
562 return -sum;
563}
564
565static void acpiPrepareHeader (ACPITBLHEADER *header, const char au8Signature[4],
566 uint32_t u32Length, uint8_t u8Revision)
567{
568 memcpy(header->au8Signature, au8Signature, 4);
569 header->u32Length = RT_H2LE_U32(u32Length);
570 header->u8Revision = u8Revision;
571 memcpy(header->au8OemId, "VBOX ", 6);
572 memcpy(header->au8OemTabId, "VBOX", 4);
573 memcpy(header->au8OemTabId+4, au8Signature, 4);
574 header->u32OemRevision = RT_H2LE_U32(1);
575 memcpy(header->au8CreatorId, "ASL ", 4);
576 header->u32CreatorRev = RT_H2LE_U32(0x61);
577}
578
579static void acpiWriteGenericAddr(ACPIGENADDR *g, uint8_t u8AddressSpaceId,
580 uint8_t u8RegisterBitWidth, uint8_t u8RegisterBitOffset,
581 uint8_t u8AccessSize, uint64_t u64Address)
582{
583 g->u8AddressSpaceId = u8AddressSpaceId;
584 g->u8RegisterBitWidth = u8RegisterBitWidth;
585 g->u8RegisterBitOffset = u8RegisterBitOffset;
586 g->u8AccessSize = u8AccessSize;
587 g->u64Address = RT_H2LE_U64(u64Address);
588}
589
590static void acpiPhyscpy (ACPIState *s, RTGCPHYS32 dst, const void * const src, size_t size)
591{
592 PDMDevHlpPhysWrite (s->pDevIns, dst, src, size);
593}
594
595/* Differentiated System Description Table (DSDT) */
596static void acpiSetupDSDT (ACPIState *s, RTGCPHYS32 addr)
597{
598 acpiPhyscpy (s, addr, AmlCode, sizeof(AmlCode));
599}
600
601/* Firmware ACPI Control Structure (FACS) */
602static void acpiSetupFACS (ACPIState *s, RTGCPHYS32 addr)
603{
604 ACPITBLFACS facs;
605
606 memset (&facs, 0, sizeof(facs));
607 memcpy (facs.au8Signature, "FACS", 4);
608 facs.u32Length = RT_H2LE_U32(sizeof(ACPITBLFACS));
609 facs.u32HWSignature = RT_H2LE_U32(0);
610 facs.u32FWVector = RT_H2LE_U32(0);
611 facs.u32GlobalLock = RT_H2LE_U32(0);
612 facs.u32Flags = RT_H2LE_U32(0);
613 facs.u64X_FWVector = RT_H2LE_U64(0);
614 facs.u8Version = 1;
615
616 acpiPhyscpy (s, addr, (const uint8_t*)&facs, sizeof(facs));
617}
618
619/* Fixed ACPI Description Table (FADT aka FACP) */
620static void acpiSetupFADT (ACPIState *s, RTGCPHYS32 addr, uint32_t facs_addr, uint32_t dsdt_addr)
621{
622 ACPITBLFADT fadt;
623
624 memset (&fadt, 0, sizeof(fadt));
625 acpiPrepareHeader (&fadt.header, "FACP", sizeof(fadt), 4);
626 fadt.u32FACS = RT_H2LE_U32(facs_addr);
627 fadt.u32DSDT = RT_H2LE_U32(dsdt_addr);
628 fadt.u8IntModel = INT_MODEL_DUAL_PIC;
629 fadt.u8PreferredPMProfile = 0; /* unspecified */
630 fadt.u16SCIInt = RT_H2LE_U16(SCI_INT);
631 fadt.u32SMICmd = RT_H2LE_U32(SMI_CMD);
632 fadt.u8AcpiEnable = ACPI_ENABLE;
633 fadt.u8AcpiDisable = ACPI_DISABLE;
634 fadt.u8S4BIOSReq = 0;
635 fadt.u8PStateCnt = 0;
636 fadt.u32PM1aEVTBLK = RT_H2LE_U32(PM1a_EVT_BLK);
637 fadt.u32PM1bEVTBLK = RT_H2LE_U32(PM1b_EVT_BLK);
638 fadt.u32PM1aCTLBLK = RT_H2LE_U32(PM1a_CTL_BLK);
639 fadt.u32PM1bCTLBLK = RT_H2LE_U32(PM1b_CTL_BLK);
640 fadt.u32PM2CTLBLK = RT_H2LE_U32(PM2_CTL_BLK);
641 fadt.u32PMTMRBLK = RT_H2LE_U32(PM_TMR_BLK);
642 fadt.u32GPE0BLK = RT_H2LE_U32(GPE0_BLK);
643 fadt.u32GPE1BLK = RT_H2LE_U32(GPE1_BLK);
644 fadt.u8PM1EVTLEN = 4;
645 fadt.u8PM1CTLLEN = 2;
646 fadt.u8PM2CTLLEN = 0;
647 fadt.u8PMTMLEN = 4;
648 fadt.u8GPE0BLKLEN = GPE0_BLK_LEN;
649 fadt.u8GPE1BLKLEN = GPE1_BLK_LEN;
650 fadt.u8GPE1BASE = GPE1_BASE;
651 fadt.u8CSTCNT = 0;
652 fadt.u16PLVL2LAT = RT_H2LE_U16(P_LVL2_LAT);
653 fadt.u16PLVL3LAT = RT_H2LE_U16(P_LVL3_LAT);
654 fadt.u16FlushSize = RT_H2LE_U16(FLUSH_SIZE);
655 fadt.u16FlushStride = RT_H2LE_U16(FLUSH_STRIDE);
656 fadt.u8DutyOffset = 0;
657 fadt.u8DutyWidth = 0;
658 fadt.u8DayAlarm = 0;
659 fadt.u8MonAlarm = 0;
660 fadt.u8Century = 0;
661 fadt.u16IAPCBOOTARCH = RT_H2LE_U16(IAPC_BOOT_ARCH_LEGACY_DEV | IAPC_BOOT_ARCH_8042);
662 /** @note WBINVD is required for ACPI versions newer than 1.0 */
663 fadt.u32Flags = RT_H2LE_U32( FADT_FL_WBINVD
664 | FADT_FL_FIX_RTC
665 | FADT_FL_TMR_VAL_EXT);
666 acpiWriteGenericAddr(&fadt.ResetReg, 1, 8, 0, 1, ACPI_RESET_BLK);
667 fadt.u8ResetVal = ACPI_RESET_REG_VAL;
668 fadt.u64XFACS = RT_H2LE_U64((uint64_t)facs_addr);
669 fadt.u64XDSDT = RT_H2LE_U64((uint64_t)dsdt_addr);
670 acpiWriteGenericAddr(&fadt.X_PM1aEVTBLK, 1, 32, 0, 2, PM1a_EVT_BLK);
671 acpiWriteGenericAddr(&fadt.X_PM1bEVTBLK, 0, 0, 0, 0, PM1b_EVT_BLK);
672 acpiWriteGenericAddr(&fadt.X_PM1aCTLBLK, 1, 16, 0, 2, PM1a_CTL_BLK);
673 acpiWriteGenericAddr(&fadt.X_PM1bCTLBLK, 0, 0, 0, 0, PM1b_CTL_BLK);
674 acpiWriteGenericAddr(&fadt.X_PM2CTLBLK, 0, 0, 0, 0, PM2_CTL_BLK);
675 acpiWriteGenericAddr(&fadt.X_PMTMRBLK, 1, 32, 0, 3, PM_TMR_BLK);
676 acpiWriteGenericAddr(&fadt.X_GPE0BLK, 1, 16, 0, 1, GPE0_BLK);
677 acpiWriteGenericAddr(&fadt.X_GPE1BLK, 0, 0, 0, 0, GPE1_BLK);
678 fadt.header.u8Checksum = acpiChecksum ((uint8_t*)&fadt, sizeof(fadt));
679 acpiPhyscpy (s, addr, &fadt, sizeof(fadt));
680}
681
682/*
683 * Root System Description Table.
684 * The RSDT and XSDT tables are basically identical. The only difference is 32 vs 64 bits
685 * addresses for description headers. RSDT is for ACPI 1.0. XSDT for ACPI 2.0 and up.
686 */
687static int acpiSetupRSDT (ACPIState *s, RTGCPHYS32 addr, unsigned int nb_entries, uint32_t *addrs)
688{
689 ACPITBLRSDT *rsdt;
690 const size_t size = sizeof(ACPITBLHEADER) + nb_entries * sizeof(rsdt->u32Entry[0]);
691
692 rsdt = (ACPITBLRSDT*)RTMemAllocZ (size);
693 if (!rsdt)
694 return PDMDEV_SET_ERROR(s->pDevIns, VERR_NO_TMP_MEMORY, N_("Cannot allocate RSDT"));
695
696 acpiPrepareHeader (&rsdt->header, "RSDT", size, 1);
697 for (unsigned int i = 0; i < nb_entries; ++i)
698 {
699 rsdt->u32Entry[i] = RT_H2LE_U32(addrs[i]);
700 Log(("Setup RSDT: [%d] = %x\n", i, rsdt->u32Entry[i]));
701 }
702 rsdt->header.u8Checksum = acpiChecksum ((uint8_t*)rsdt, size);
703 acpiPhyscpy (s, addr, rsdt, size);
704 RTMemFree (rsdt);
705 return VINF_SUCCESS;
706}
707
708/* Extended System Description Table. */
709static int acpiSetupXSDT (ACPIState *s, RTGCPHYS32 addr, unsigned int nb_entries, uint32_t *addrs)
710{
711 ACPITBLXSDT *xsdt;
712 const size_t size = sizeof(ACPITBLHEADER) + nb_entries * sizeof(xsdt->u64Entry[0]);
713
714 xsdt = (ACPITBLXSDT*)RTMemAllocZ (size);
715 if (!xsdt)
716 return VERR_NO_TMP_MEMORY;
717
718 acpiPrepareHeader (&xsdt->header, "XSDT", size, 1 /* according to ACPI 3.0 specs */);
719 for (unsigned int i = 0; i < nb_entries; ++i)
720 {
721 xsdt->u64Entry[i] = RT_H2LE_U64((uint64_t)addrs[i]);
722 Log(("Setup XSDT: [%d] = %RX64\n", i, xsdt->u64Entry[i]));
723 }
724 xsdt->header.u8Checksum = acpiChecksum ((uint8_t*)xsdt, size);
725 acpiPhyscpy (s, addr, xsdt, size);
726 RTMemFree (xsdt);
727 return VINF_SUCCESS;
728}
729
730/* Root System Description Pointer (RSDP) */
731static void acpiSetupRSDP (ACPITBLRSDP *rsdp, uint32_t rsdt_addr, uint64_t xsdt_addr)
732{
733 memset(rsdp, 0, sizeof(*rsdp));
734
735 /* ACPI 1.0 part (RSDT */
736 memcpy(rsdp->au8Signature, "RSD PTR ", 8);
737 memcpy(rsdp->au8OemId, "VBOX ", 6);
738 rsdp->u8Revision = ACPI_REVISION;
739 rsdp->u32RSDT = RT_H2LE_U32(rsdt_addr);
740 rsdp->u8Checksum = acpiChecksum((uint8_t*)rsdp, RT_OFFSETOF(ACPITBLRSDP, u32Length));
741
742 /* ACPI 2.0 part (XSDT) */
743 rsdp->u32Length = RT_H2LE_U32(sizeof(ACPITBLRSDP));
744 rsdp->u64XSDT = RT_H2LE_U64(xsdt_addr);
745 rsdp->u8ExtChecksum = acpiChecksum ((uint8_t*)rsdp, sizeof(ACPITBLRSDP));
746}
747
748/* Multiple APIC Description Table. */
749/** @todo All hardcoded, should set this up based on the actual VM config!!!!! */
750/** @note APIC without IO-APIC hangs Windows Vista therefore we setup both */
751static void acpiSetupMADT (ACPIState *s, RTGCPHYS32 addr)
752{
753#ifdef VBOX_WITH_SMP_GUESTS
754 uint16_t cpus = s->cCpus;
755 AcpiTableMADT madt(cpus);
756
757 acpiPrepareHeader(madt.header_addr(), "APIC", madt.size(), 2);
758
759 *madt.u32LAPIC_addr() = RT_H2LE_U32(0xfee00000);
760 *madt.u32Flags_addr() = RT_H2LE_U32(PCAT_COMPAT);
761
762 ACPITBLLAPIC* lapic = madt.LApics_addr();
763 for (uint16_t i = 0; i < cpus; i++)
764 {
765 lapic->u8Type = 0;
766 lapic->u8Length = sizeof(ACPITBLLAPIC);
767 lapic->u8ProcId = i;
768 lapic->u8ApicId = i;
769 lapic->u32Flags = RT_H2LE_U32(LAPIC_ENABLED);
770 lapic++;
771 }
772
773 ACPITBLIOAPIC* ioapic = madt.IOApic_addr();
774
775 ioapic->u8Type = 1;
776 ioapic->u8Length = sizeof(ACPITBLIOAPIC);
777 ioapic->u8IOApicId = cpus;
778 ioapic->u8Reserved = 0;
779 ioapic->u32Address = RT_H2LE_U32(0xfec00000);
780 ioapic->u32GSIB = RT_H2LE_U32(0);
781
782 madt.header_addr()->u8Checksum = acpiChecksum (madt.data(), madt.size());
783 acpiPhyscpy (s, addr, madt.data(), madt.size());
784
785#else /* !VBOX_WITH_SMP_GUESTS */
786 ACPITBLMADT madt;
787
788 /* Don't call this function if u8UseIOApic==false! */
789 Assert(s->u8UseIOApic);
790
791 memset(&madt, 0, sizeof(madt));
792 acpiPrepareHeader(&madt.header, "APIC", sizeof(madt), 2);
793
794 madt.u32LAPIC = RT_H2LE_U32(0xfee00000);
795 madt.u32Flags = RT_H2LE_U32(PCAT_COMPAT);
796
797 madt.LApic.u8Type = 0;
798 madt.LApic.u8Length = sizeof(ACPITBLLAPIC);
799 madt.LApic.u8ProcId = 0;
800 madt.LApic.u8ApicId = 0;
801 madt.LApic.u32Flags = RT_H2LE_U32(LAPIC_ENABLED);
802
803 madt.IOApic.u8Type = 1;
804 madt.IOApic.u8Length = sizeof(ACPITBLIOAPIC);
805 madt.IOApic.u8IOApicId = 0;
806 madt.IOApic.u8Reserved = 0;
807 madt.IOApic.u32Address = RT_H2LE_U32(0xfec00000);
808 madt.IOApic.u32GSIB = RT_H2LE_U32(0);
809
810 madt.header.u8Checksum = acpiChecksum ((uint8_t*)&madt, sizeof(madt));
811 acpiPhyscpy (s, addr, &madt, sizeof(madt));
812#endif /* !VBOX_WITH_SMP_GUESTS */
813}
814
815/* SCI IRQ */
816DECLINLINE(void) acpiSetIrq (ACPIState *s, int level)
817{
818 if (s->pm1a_ctl & SCI_EN)
819 PDMDevHlpPCISetIrq (s->pDevIns, -1, level);
820}
821
822DECLINLINE(uint32_t) pm1a_pure_en (uint32_t en)
823{
824 return en & ~(RSR_EN | IGN_EN);
825}
826
827DECLINLINE(uint32_t) pm1a_pure_sts (uint32_t sts)
828{
829 return sts & ~(RSR_STS | IGN_STS);
830}
831
832DECLINLINE(int) pm1a_level (ACPIState *s)
833{
834 return (pm1a_pure_en (s->pm1a_en) & pm1a_pure_sts (s->pm1a_sts)) != 0;
835}
836
837DECLINLINE(int) gpe0_level (ACPIState *s)
838{
839 return (s->gpe0_en & s->gpe0_sts) != 0;
840}
841
842static void update_pm1a (ACPIState *s, uint32_t sts, uint32_t en)
843{
844 int old_level, new_level;
845
846 if (gpe0_level (s))
847 return;
848
849 old_level = pm1a_level (s);
850 new_level = (pm1a_pure_en (en) & pm1a_pure_sts (sts)) != 0;
851
852 s->pm1a_en = en;
853 s->pm1a_sts = sts;
854
855 if (new_level != old_level)
856 acpiSetIrq (s, new_level);
857}
858
859static void update_gpe0 (ACPIState *s, uint32_t sts, uint32_t en)
860{
861 int old_level, new_level;
862
863 if (pm1a_level (s))
864 return;
865
866 old_level = (s->gpe0_en & s->gpe0_sts) != 0;
867 new_level = (en & sts) != 0;
868
869 s->gpe0_en = en;
870 s->gpe0_sts = sts;
871
872 if (new_level != old_level)
873 acpiSetIrq (s, new_level);
874}
875
876static int acpiPowerDown (ACPIState *s)
877{
878 int rc = PDMDevHlpVMPowerOff(s->pDevIns);
879 if (RT_FAILURE (rc))
880 AssertMsgFailed (("Could not power down the VM. rc = %Rrc\n", rc));
881 return rc;
882}
883
884/** Converts a ACPI port interface pointer to an ACPI state pointer. */
885#define IACPIPORT_2_ACPISTATE(pInterface) ( (ACPIState*)((uintptr_t)pInterface - RT_OFFSETOF(ACPIState, IACPIPort)) )
886
887/**
888 * Send an ACPI power off event.
889 *
890 * @returns VBox status code
891 * @param pInterface Pointer to the interface structure containing the called function pointer.
892 */
893static DECLCALLBACK(int) acpiPowerButtonPress(PPDMIACPIPORT pInterface)
894{
895 ACPIState *s = IACPIPORT_2_ACPISTATE(pInterface);
896 s->fPowerButtonHandled = false;
897 update_pm1a (s, s->pm1a_sts | PWRBTN_STS, s->pm1a_en);
898 return VINF_SUCCESS;
899}
900
901static DECLCALLBACK(int) acpiGetPowerButtonHandled(PPDMIACPIPORT pInterface, bool *pfHandled)
902{
903 ACPIState *s = IACPIPORT_2_ACPISTATE(pInterface);
904 *pfHandled = s->fPowerButtonHandled;
905 return VINF_SUCCESS;
906}
907
908/**
909 * Send an ACPI sleep button event.
910 *
911 * @returns VBox status code
912 * @param pInterface Pointer to the interface structure containing the called function pointer.
913 */
914static DECLCALLBACK(int) acpiSleepButtonPress(PPDMIACPIPORT pInterface)
915{
916 ACPIState *s = IACPIPORT_2_ACPISTATE(pInterface);
917 update_pm1a (s, s->pm1a_sts | SLPBTN_STS, s->pm1a_en);
918 return VINF_SUCCESS;
919}
920
921/* PM1a_EVT_BLK enable */
922static uint32_t acpiPm1aEnReadw (ACPIState *s, uint32_t addr)
923{
924 uint16_t val = s->pm1a_en;
925 Log (("acpi: acpiPm1aEnReadw -> %#x\n", val));
926 return val;
927}
928
929static void acpiPM1aEnWritew (ACPIState *s, uint32_t addr, uint32_t val)
930{
931 Log (("acpi: acpiPM1aEnWritew <- %#x (%#x)\n", val, val & ~(RSR_EN | IGN_EN)));
932 val &= ~(RSR_EN | IGN_EN);
933 update_pm1a (s, s->pm1a_sts, val);
934}
935
936/* PM1a_EVT_BLK status */
937static uint32_t acpiPm1aStsReadw (ACPIState *s, uint32_t addr)
938{
939 uint16_t val = s->pm1a_sts;
940 Log (("acpi: acpiPm1aStsReadw -> %#x\n", val));
941 return val;
942}
943
944static void acpiPM1aStsWritew (ACPIState *s, uint32_t addr, uint32_t val)
945{
946 Log (("acpi: acpiPM1aStsWritew <- %#x (%#x)\n", val, val & ~(RSR_STS | IGN_STS)));
947 if (val & PWRBTN_STS)
948 s->fPowerButtonHandled = true; /* Remember that the guest handled the last power button event */
949 val = s->pm1a_sts & ~(val & ~(RSR_STS | IGN_STS));
950 update_pm1a (s, val, s->pm1a_en);
951}
952
953/* PM1a_CTL_BLK */
954static uint32_t acpiPm1aCtlReadw (ACPIState *s, uint32_t addr)
955{
956 uint16_t val = s->pm1a_ctl;
957 Log (("acpi: acpiPm1aCtlReadw -> %#x\n", val));
958 return val;
959}
960
961static int acpiPM1aCtlWritew (ACPIState *s, uint32_t addr, uint32_t val)
962{
963 uint32_t uSleepState;
964
965 Log (("acpi: acpiPM1aCtlWritew <- %#x (%#x)\n", val, val & ~(RSR_CNT | IGN_CNT)));
966 s->pm1a_ctl = val & ~(RSR_CNT | IGN_CNT);
967
968 uSleepState = (s->pm1a_ctl >> SLP_TYPx_SHIFT) & SLP_TYPx_MASK;
969 if (uSleepState != s->uSleepState)
970 {
971 s->uSleepState = uSleepState;
972 switch (uSleepState)
973 {
974 case 0x00: /* S0 */
975 break;
976 case 0x05: /* S5 */
977 LogRel (("Entering S5 (power down)\n"));
978 return acpiPowerDown (s);
979 default:
980 AssertMsgFailed (("Unknown sleep state %#x\n", uSleepState));
981 break;
982 }
983 }
984 return VINF_SUCCESS;
985}
986
987/* GPE0_BLK */
988static uint32_t acpiGpe0EnReadb (ACPIState *s, uint32_t addr)
989{
990 uint8_t val = s->gpe0_en;
991 Log (("acpi: acpiGpe0EnReadl -> %#x\n", val));
992 return val;
993}
994
995static void acpiGpe0EnWriteb (ACPIState *s, uint32_t addr, uint32_t val)
996{
997 Log (("acpi: acpiGpe0EnWritel <- %#x\n", val));
998 update_gpe0 (s, s->gpe0_sts, val);
999}
1000
1001static uint32_t acpiGpe0StsReadb (ACPIState *s, uint32_t addr)
1002{
1003 uint8_t val = s->gpe0_sts;
1004 Log (("acpi: acpiGpe0StsReadl -> %#x\n", val));
1005 return val;
1006}
1007
1008static void acpiGpe0StsWriteb (ACPIState *s, uint32_t addr, uint32_t val)
1009{
1010 val = s->gpe0_sts & ~val;
1011 update_gpe0 (s, val, s->gpe0_en);
1012 Log (("acpi: acpiGpe0StsWritel <- %#x\n", val));
1013}
1014
1015static int acpiResetWriteU8(ACPIState *s, uint32_t addr, uint32_t val)
1016{
1017 int rc = VINF_SUCCESS;
1018
1019 Log(("ACPI: acpiResetWriteU8: %x %x\n", addr, val));
1020 if (val == ACPI_RESET_REG_VAL)
1021 {
1022# ifndef IN_RING3
1023 rc = VINF_IOM_HC_IOPORT_WRITE;
1024# else /* IN_RING3 */
1025 rc = PDMDevHlpVMReset(s->pDevIns);
1026# endif /* !IN_RING3 */
1027 }
1028 return rc;
1029}
1030
1031/* SMI */
1032static void acpiSmiWriteU8 (ACPIState *s, uint32_t addr, uint32_t val)
1033{
1034 Log (("acpi: acpiSmiWriteU8 %#x\n", val));
1035 if (val == ACPI_ENABLE)
1036 s->pm1a_ctl |= SCI_EN;
1037 else if (val == ACPI_DISABLE)
1038 s->pm1a_ctl &= ~SCI_EN;
1039 else
1040 Log (("acpi: acpiSmiWriteU8 %#x <- unknown value\n", val));
1041}
1042
1043static uint32_t find_rsdp_space (void)
1044{
1045 return 0xe0000;
1046}
1047
1048static void acpiPMTimerReset (ACPIState *s)
1049{
1050 uint64_t interval, freq;
1051
1052 freq = TMTimerGetFreq (s->CTX_SUFF(ts));
1053 interval = ASMMultU64ByU32DivByU32 (0xffffffff, freq, PM_TMR_FREQ);
1054 Log (("interval = %RU64\n", interval));
1055 TMTimerSet (s->CTX_SUFF(ts), TMTimerGet (s->CTX_SUFF(ts)) + interval);
1056}
1057
1058static DECLCALLBACK(void) acpiTimer (PPDMDEVINS pDevIns, PTMTIMER pTimer)
1059{
1060 ACPIState *s = PDMINS_2_DATA (pDevIns, ACPIState *);
1061
1062 Log (("acpi: pm timer sts %#x (%d), en %#x (%d)\n",
1063 s->pm1a_sts, (s->pm1a_sts & TMR_STS) != 0,
1064 s->pm1a_en, (s->pm1a_en & TMR_EN) != 0));
1065
1066 update_pm1a (s, s->pm1a_sts | TMR_STS, s->pm1a_en);
1067 acpiPMTimerReset (s);
1068}
1069
1070/**
1071 * _BST method.
1072 */
1073static void acpiFetchBatteryStatus (ACPIState *s)
1074{
1075 uint32_t *p = s->au8BatteryInfo;
1076 bool fPresent; /* battery present? */
1077 PDMACPIBATCAPACITY hostRemainingCapacity; /* 0..100 */
1078 PDMACPIBATSTATE hostBatteryState; /* bitfield */
1079 uint32_t hostPresentRate; /* 0..1000 */
1080 int rc;
1081
1082 if (!s->pDrv)
1083 return;
1084 rc = s->pDrv->pfnQueryBatteryStatus (s->pDrv, &fPresent, &hostRemainingCapacity,
1085 &hostBatteryState, &hostPresentRate);
1086 AssertRC (rc);
1087
1088 /* default values */
1089 p[BAT_STATUS_STATE] = hostBatteryState;
1090 p[BAT_STATUS_PRESENT_RATE] = hostPresentRate == ~0U ? 0xFFFFFFFF
1091 : hostPresentRate * 50; /* mW */
1092 p[BAT_STATUS_REMAINING_CAPACITY] = 50000; /* mWh */
1093 p[BAT_STATUS_PRESENT_VOLTAGE] = 10000; /* mV */
1094
1095 /* did we get a valid battery state? */
1096 if (hostRemainingCapacity != PDM_ACPI_BAT_CAPACITY_UNKNOWN)
1097 p[BAT_STATUS_REMAINING_CAPACITY] = hostRemainingCapacity * 500; /* mWh */
1098 if (hostBatteryState == PDM_ACPI_BAT_STATE_CHARGED)
1099 p[BAT_STATUS_PRESENT_RATE] = 0; /* mV */
1100}
1101
1102/**
1103 * _BIF method.
1104 */
1105static void acpiFetchBatteryInfo (ACPIState *s)
1106{
1107 uint32_t *p = s->au8BatteryInfo;
1108
1109 p[BAT_INFO_UNITS] = 0; /* mWh */
1110 p[BAT_INFO_DESIGN_CAPACITY] = 50000; /* mWh */
1111 p[BAT_INFO_LAST_FULL_CHARGE_CAPACITY] = 50000; /* mWh */
1112 p[BAT_INFO_TECHNOLOGY] = BAT_TECH_PRIMARY;
1113 p[BAT_INFO_DESIGN_VOLTAGE] = 10000; /* mV */
1114 p[BAT_INFO_DESIGN_CAPACITY_OF_WARNING] = 100; /* mWh */
1115 p[BAT_INFO_DESIGN_CAPACITY_OF_LOW] = 50; /* mWh */
1116 p[BAT_INFO_CAPACITY_GRANULARITY_1] = 1; /* mWh */
1117 p[BAT_INFO_CAPACITY_GRANULARITY_2] = 1; /* mWh */
1118}
1119
1120/**
1121 * _STA method.
1122 */
1123static uint32_t acpiGetBatteryDeviceStatus (ACPIState *s)
1124{
1125 bool fPresent; /* battery present? */
1126 PDMACPIBATCAPACITY hostRemainingCapacity; /* 0..100 */
1127 PDMACPIBATSTATE hostBatteryState; /* bitfield */
1128 uint32_t hostPresentRate; /* 0..1000 */
1129 int rc;
1130
1131 if (!s->pDrv)
1132 return 0;
1133 rc = s->pDrv->pfnQueryBatteryStatus (s->pDrv, &fPresent, &hostRemainingCapacity,
1134 &hostBatteryState, &hostPresentRate);
1135 AssertRC (rc);
1136
1137 return fPresent
1138 ? STA_DEVICE_PRESENT_MASK /* present */
1139 | STA_DEVICE_ENABLED_MASK /* enabled and decodes its resources */
1140 | STA_DEVICE_SHOW_IN_UI_MASK /* should be shown in UI */
1141 | STA_DEVICE_FUNCTIONING_PROPERLY_MASK /* functioning properly */
1142 | STA_BATTERY_PRESENT_MASK /* battery is present */
1143 : 0; /* device not present */
1144}
1145
1146static uint32_t acpiGetPowerSource (ACPIState *s)
1147{
1148 PDMACPIPOWERSOURCE ps;
1149
1150 /* query the current power source from the host driver */
1151 if (!s->pDrv)
1152 return AC_ONLINE;
1153 int rc = s->pDrv->pfnQueryPowerSource (s->pDrv, &ps);
1154 AssertRC (rc);
1155 return ps == PDM_ACPI_POWER_SOURCE_BATTERY ? AC_OFFLINE : AC_ONLINE;
1156}
1157
1158IO_WRITE_PROTO (acpiBatIndexWrite)
1159{
1160 ACPIState *s = (ACPIState *)pvUser;
1161
1162 switch (cb)
1163 {
1164 case 4:
1165 u32 >>= s->u8IndexShift;
1166 /* see comment at the declaration of u8IndexShift */
1167 if (s->u8IndexShift == 0 && u32 == (BAT_DEVICE_STATUS << 2))
1168 {
1169 s->u8IndexShift = 2;
1170 u32 >>= 2;
1171 }
1172 Assert (u32 < BAT_INDEX_LAST);
1173 s->uBatteryIndex = u32;
1174 break;
1175 default:
1176 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1177 break;
1178 }
1179 return VINF_SUCCESS;
1180}
1181
1182IO_READ_PROTO (acpiBatDataRead)
1183{
1184 ACPIState *s = (ACPIState *)pvUser;
1185
1186 switch (cb)
1187 {
1188 case 4:
1189 switch (s->uBatteryIndex)
1190 {
1191 case BAT_STATUS_STATE:
1192 acpiFetchBatteryStatus(s);
1193 case BAT_STATUS_PRESENT_RATE:
1194 case BAT_STATUS_REMAINING_CAPACITY:
1195 case BAT_STATUS_PRESENT_VOLTAGE:
1196 *pu32 = s->au8BatteryInfo[s->uBatteryIndex];
1197 break;
1198
1199 case BAT_INFO_UNITS:
1200 acpiFetchBatteryInfo(s);
1201 case BAT_INFO_DESIGN_CAPACITY:
1202 case BAT_INFO_LAST_FULL_CHARGE_CAPACITY:
1203 case BAT_INFO_TECHNOLOGY:
1204 case BAT_INFO_DESIGN_VOLTAGE:
1205 case BAT_INFO_DESIGN_CAPACITY_OF_WARNING:
1206 case BAT_INFO_DESIGN_CAPACITY_OF_LOW:
1207 case BAT_INFO_CAPACITY_GRANULARITY_1:
1208 case BAT_INFO_CAPACITY_GRANULARITY_2:
1209 *pu32 = s->au8BatteryInfo[s->uBatteryIndex];
1210 break;
1211
1212 case BAT_DEVICE_STATUS:
1213 *pu32 = acpiGetBatteryDeviceStatus(s);
1214 break;
1215
1216 case BAT_POWER_SOURCE:
1217 *pu32 = acpiGetPowerSource(s);
1218 break;
1219
1220 default:
1221 AssertMsgFailed (("Invalid battery index %d\n", s->uBatteryIndex));
1222 break;
1223 }
1224 break;
1225 default:
1226 return VERR_IOM_IOPORT_UNUSED;
1227 }
1228 return VINF_SUCCESS;
1229}
1230
1231IO_READ_PROTO (acpiFdcStatusRead)
1232{
1233 ACPIState *s = (ACPIState *)pvUser;
1234
1235 switch (cb)
1236 {
1237 case 4:
1238 *pu32 = s->u8UseFdc
1239 ? STA_DEVICE_PRESENT_MASK /* present */
1240 | STA_DEVICE_ENABLED_MASK /* enabled and decodes its resources */
1241 | STA_DEVICE_SHOW_IN_UI_MASK /* should be shown in UI */
1242 | STA_DEVICE_FUNCTIONING_PROPERLY_MASK /* functioning properly */
1243 : 0; /* device not present */
1244 break;
1245 default:
1246 return VERR_IOM_IOPORT_UNUSED;
1247 }
1248 return VINF_SUCCESS;
1249}
1250
1251IO_WRITE_PROTO (acpiSysInfoIndexWrite)
1252{
1253 ACPIState *s = (ACPIState *)pvUser;
1254
1255 Log(("system_index = %d, %d\n", u32, u32 >> 2));
1256 switch (cb) {
1257 case 4:
1258 if (u32 == SYSTEM_INFO_INDEX_VALID || u32 == SYSTEM_INFO_INDEX_INVALID)
1259 s->uSystemInfoIndex = u32;
1260 else
1261 {
1262 u32 >>= s->u8IndexShift;
1263 Assert (u32 < SYSTEM_INFO_INDEX_LAST);
1264 s->uSystemInfoIndex = u32;
1265 }
1266 break;
1267
1268 default:
1269 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1270 break;
1271 }
1272 return VINF_SUCCESS;
1273}
1274
1275IO_READ_PROTO (acpiSysInfoDataRead)
1276{
1277 ACPIState *s = (ACPIState *)pvUser;
1278
1279 switch (cb)
1280 {
1281 case 4:
1282 switch (s->uSystemInfoIndex)
1283 {
1284 case SYSTEM_INFO_INDEX_MEMORY_LENGTH:
1285 *pu32 = s->u64RamSize;
1286 break;
1287
1288 case SYSTEM_INFO_INDEX_USE_IOAPIC:
1289 *pu32 = s->u8UseIOApic;
1290 break;
1291
1292 default:
1293 AssertMsgFailed (("Invalid system info index %d\n", s->uSystemInfoIndex));
1294 break;
1295 }
1296 break;
1297
1298 default:
1299 return VERR_IOM_IOPORT_UNUSED;
1300 }
1301
1302 Log(("index %d val %d\n", s->uSystemInfoIndex, *pu32));
1303 return VINF_SUCCESS;
1304}
1305
1306IO_WRITE_PROTO (acpiSysInfoDataWrite)
1307{
1308 ACPIState *s = (ACPIState *)pvUser;
1309
1310 Log(("addr=%#x cb=%d u32=%#x si=%#x\n", Port, cb, u32, s->uSystemInfoIndex));
1311
1312 if (cb == 4 && u32 == 0xbadc0de)
1313 {
1314 switch (s->uSystemInfoIndex)
1315 {
1316 case SYSTEM_INFO_INDEX_INVALID:
1317 s->u8IndexShift = 0;
1318 break;
1319
1320 case SYSTEM_INFO_INDEX_VALID:
1321 s->u8IndexShift = 2;
1322 break;
1323
1324 default:
1325 AssertMsgFailed(("Port=%#x cb=%d u32=%#x system_index=%#x\n",
1326 Port, cb, u32, s->uSystemInfoIndex));
1327 break;
1328 }
1329 }
1330 else
1331 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1332 return VINF_SUCCESS;
1333}
1334
1335/* IO Helpers */
1336IO_READ_PROTO (acpiPm1aEnRead)
1337{
1338 switch (cb)
1339 {
1340 case 2:
1341 *pu32 = acpiPm1aEnReadw ((ACPIState*)pvUser, Port);
1342 break;
1343 default:
1344 return VERR_IOM_IOPORT_UNUSED;
1345 }
1346 return VINF_SUCCESS;
1347}
1348
1349IO_READ_PROTO (acpiPm1aStsRead)
1350{
1351 switch (cb)
1352 {
1353 case 2:
1354 *pu32 = acpiPm1aStsReadw ((ACPIState*)pvUser, Port);
1355 break;
1356 default:
1357 return VERR_IOM_IOPORT_UNUSED;
1358 }
1359 return VINF_SUCCESS;
1360}
1361
1362IO_READ_PROTO (acpiPm1aCtlRead)
1363{
1364 switch (cb)
1365 {
1366 case 2:
1367 *pu32 = acpiPm1aCtlReadw ((ACPIState*)pvUser, Port);
1368 break;
1369 default:
1370 return VERR_IOM_IOPORT_UNUSED;
1371 }
1372 return VINF_SUCCESS;
1373}
1374
1375IO_WRITE_PROTO (acpiPM1aEnWrite)
1376{
1377 switch (cb)
1378 {
1379 case 2:
1380 acpiPM1aEnWritew ((ACPIState*)pvUser, Port, u32);
1381 break;
1382 default:
1383 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1384 break;
1385 }
1386 return VINF_SUCCESS;
1387}
1388
1389IO_WRITE_PROTO (acpiPM1aStsWrite)
1390{
1391 switch (cb)
1392 {
1393 case 2:
1394 acpiPM1aStsWritew ((ACPIState*)pvUser, Port, u32);
1395 break;
1396 default:
1397 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1398 break;
1399 }
1400 return VINF_SUCCESS;
1401}
1402
1403IO_WRITE_PROTO (acpiPM1aCtlWrite)
1404{
1405 switch (cb)
1406 {
1407 case 2:
1408 return acpiPM1aCtlWritew ((ACPIState*)pvUser, Port, u32);
1409 default:
1410 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1411 break;
1412 }
1413 return VINF_SUCCESS;
1414}
1415
1416#endif /* IN_RING3 */
1417
1418/**
1419 * PMTMR readable from host/guest.
1420 */
1421IO_READ_PROTO (acpiPMTmrRead)
1422{
1423 if (cb == 4)
1424 {
1425 ACPIState *s = PDMINS_2_DATA (pDevIns, ACPIState *);
1426 int64_t now = TMTimerGet (s->CTX_SUFF(ts));
1427 int64_t elapsed = now - s->pm_timer_initial;
1428
1429 *pu32 = ASMMultU64ByU32DivByU32 (elapsed, PM_TMR_FREQ, TMTimerGetFreq (s->CTX_SUFF(ts)));
1430 Log (("acpi: acpiPMTmrRead -> %#x\n", *pu32));
1431 return VINF_SUCCESS;
1432 }
1433 return VERR_IOM_IOPORT_UNUSED;
1434}
1435
1436#ifdef IN_RING3
1437
1438IO_READ_PROTO (acpiGpe0StsRead)
1439{
1440 switch (cb)
1441 {
1442 case 1:
1443 *pu32 = acpiGpe0StsReadb ((ACPIState*)pvUser, Port);
1444 break;
1445 default:
1446 return VERR_IOM_IOPORT_UNUSED;
1447 }
1448 return VINF_SUCCESS;
1449}
1450
1451IO_READ_PROTO (acpiGpe0EnRead)
1452{
1453 switch (cb)
1454 {
1455 case 1:
1456 *pu32 = acpiGpe0EnReadb ((ACPIState*)pvUser, Port);
1457 break;
1458 default:
1459 return VERR_IOM_IOPORT_UNUSED;
1460 }
1461 return VINF_SUCCESS;
1462}
1463
1464IO_WRITE_PROTO (acpiGpe0StsWrite)
1465{
1466 switch (cb)
1467 {
1468 case 1:
1469 acpiGpe0StsWriteb ((ACPIState*)pvUser, Port, u32);
1470 break;
1471 default:
1472 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1473 break;
1474 }
1475 return VINF_SUCCESS;
1476}
1477
1478IO_WRITE_PROTO (acpiGpe0EnWrite)
1479{
1480 switch (cb)
1481 {
1482 case 1:
1483 acpiGpe0EnWriteb ((ACPIState*)pvUser, Port, u32);
1484 break;
1485 default:
1486 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1487 break;
1488 }
1489 return VINF_SUCCESS;
1490}
1491
1492IO_WRITE_PROTO (acpiSmiWrite)
1493{
1494 switch (cb)
1495 {
1496 case 1:
1497 acpiSmiWriteU8 ((ACPIState*)pvUser, Port, u32);
1498 break;
1499 default:
1500 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1501 break;
1502 }
1503 return VINF_SUCCESS;
1504}
1505
1506IO_WRITE_PROTO (acpiResetWrite)
1507{
1508 switch (cb)
1509 {
1510 case 1:
1511 return acpiResetWriteU8 ((ACPIState*)pvUser, Port, u32);
1512 default:
1513 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1514 break;
1515 }
1516 return VINF_SUCCESS;
1517}
1518
1519#ifdef DEBUG_ACPI
1520
1521IO_WRITE_PROTO (acpiDhexWrite)
1522{
1523 switch (cb)
1524 {
1525 case 1:
1526 Log (("%#x\n", u32 & 0xff));
1527 break;
1528 case 2:
1529 Log (("%#6x\n", u32 & 0xffff));
1530 case 4:
1531 Log (("%#10x\n", u32));
1532 break;
1533 default:
1534 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1535 break;
1536 }
1537 return VINF_SUCCESS;
1538}
1539
1540IO_WRITE_PROTO (acpiDchrWrite)
1541{
1542 switch (cb)
1543 {
1544 case 1:
1545 Log (("%c", u32 & 0xff));
1546 break;
1547 default:
1548 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1549 break;
1550 }
1551 return VINF_SUCCESS;
1552}
1553
1554#endif /* DEBUG_ACPI */
1555
1556
1557/**
1558 * Saved state structure description.
1559 */
1560static const SSMFIELD g_AcpiSavedStateFields[] =
1561{
1562 SSMFIELD_ENTRY (ACPIState, pm1a_en),
1563 SSMFIELD_ENTRY (ACPIState, pm1a_sts),
1564 SSMFIELD_ENTRY (ACPIState, pm1a_ctl),
1565 SSMFIELD_ENTRY (ACPIState, pm_timer_initial),
1566 SSMFIELD_ENTRY (ACPIState, gpe0_en),
1567 SSMFIELD_ENTRY (ACPIState, gpe0_sts),
1568 SSMFIELD_ENTRY (ACPIState, uBatteryIndex),
1569 SSMFIELD_ENTRY (ACPIState, uSystemInfoIndex),
1570 SSMFIELD_ENTRY (ACPIState, u64RamSize),
1571 SSMFIELD_ENTRY (ACPIState, u8IndexShift),
1572 SSMFIELD_ENTRY (ACPIState, u8UseIOApic),
1573 SSMFIELD_ENTRY (ACPIState, uSleepState),
1574 SSMFIELD_ENTRY_TERM ()
1575};
1576
1577static DECLCALLBACK(int) acpi_save_state (PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
1578{
1579 ACPIState *s = PDMINS_2_DATA (pDevIns, ACPIState *);
1580 return SSMR3PutStruct (pSSMHandle, s, &g_AcpiSavedStateFields[0]);
1581}
1582
1583static DECLCALLBACK(int) acpi_load_state (PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle,
1584 uint32_t u32Version)
1585{
1586 ACPIState *s = PDMINS_2_DATA (pDevIns, ACPIState *);
1587 int rc;
1588
1589 if (u32Version != 4)
1590 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1591
1592 rc = SSMR3GetStruct (pSSMHandle, s, &g_AcpiSavedStateFields[0]);
1593 if (RT_SUCCESS (rc))
1594 {
1595 acpiFetchBatteryStatus (s);
1596 acpiFetchBatteryInfo (s);
1597 acpiPMTimerReset (s);
1598 }
1599 return rc;
1600}
1601
1602/**
1603 * Queries an interface to the driver.
1604 *
1605 * @returns Pointer to interface.
1606 * @returns NULL if the interface was not supported by the driver.
1607 * @param pInterface Pointer to this interface structure.
1608 * @param enmInterface The requested interface identification.
1609 * @thread Any thread.
1610 */
1611static DECLCALLBACK(void *) acpiQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
1612{
1613 ACPIState *pThis = (ACPIState*)((uintptr_t)pInterface - RT_OFFSETOF(ACPIState, IBase));
1614 switch (enmInterface)
1615 {
1616 case PDMINTERFACE_BASE:
1617 return &pThis->IBase;
1618 case PDMINTERFACE_ACPI_PORT:
1619 return &pThis->IACPIPort;
1620 default:
1621 return NULL;
1622 }
1623}
1624
1625/**
1626 * Create the ACPI tables.
1627 */
1628static int acpiPlantTables (ACPIState *s)
1629{
1630 int rc;
1631 RTGCPHYS32 rsdt_addr, xsdt_addr, fadt_addr, facs_addr, dsdt_addr, last_addr, apic_addr = 0;
1632 uint32_t addend = 0;
1633 RTGCPHYS32 rsdt_addrs[4];
1634 uint32_t cAddr;
1635 size_t rsdt_tbl_len = sizeof(ACPITBLHEADER);
1636 size_t xsdt_tbl_len = sizeof(ACPITBLHEADER);
1637
1638 cAddr = 1; /* FADT */
1639 if (s->u8UseIOApic)
1640 cAddr++; /* MADT */
1641
1642 rsdt_tbl_len += cAddr*4; /* each entry: 32 bits phys. address. */
1643 xsdt_tbl_len += cAddr*8; /* each entry: 64 bits phys. address. */
1644
1645 rc = CFGMR3QueryU64 (s->pDevIns->pCfgHandle, "RamSize", &s->u64RamSize);
1646 if (RT_FAILURE (rc))
1647 return PDMDEV_SET_ERROR(s->pDevIns, rc,
1648 N_("Configuration error: Querying "
1649 "\"RamSize\" as integer failed"));
1650
1651 if (s->u64RamSize > (0xffffffff - 0x10000))
1652 return PDMDEV_SET_ERROR(s->pDevIns, VERR_OUT_OF_RANGE,
1653 N_("Configuration error: Invalid \"RamSize\", maximum allowed "
1654 "value is 4095MB"));
1655 rsdt_addr = 0;
1656 xsdt_addr = RT_ALIGN_32 (rsdt_addr + rsdt_tbl_len, 16);
1657 fadt_addr = RT_ALIGN_32 (xsdt_addr + xsdt_tbl_len, 16);
1658 facs_addr = RT_ALIGN_32 (fadt_addr + sizeof(ACPITBLFADT), 16);
1659 if (s->u8UseIOApic)
1660 {
1661 apic_addr = RT_ALIGN_32 (facs_addr + sizeof(ACPITBLFACS), 16);
1662#ifdef VBOX_WITH_SMP_GUESTS
1663 /**
1664 * @todo nike: maybe some refactoring needed to compute tables layout,
1665 * but as this code is executed only once it doesn't make sense to optimize much
1666 */
1667 dsdt_addr = RT_ALIGN_32 (apic_addr + AcpiTableMADT::sizeFor(s), 16);
1668#else
1669 dsdt_addr = RT_ALIGN_32 (apic_addr + sizeof(ACPITBLMADT), 16);
1670#endif
1671 }
1672 else
1673 {
1674 dsdt_addr = RT_ALIGN_32 (facs_addr + sizeof(ACPITBLFACS), 16);
1675 }
1676
1677 last_addr = RT_ALIGN_32 (dsdt_addr + sizeof(AmlCode), 16);
1678 if (last_addr > 0x10000)
1679 return PDMDEV_SET_ERROR(s->pDevIns, VERR_TOO_MUCH_DATA,
1680 N_("Error: ACPI tables > 64KB"));
1681
1682 Log(("RSDP 0x%08X\n", find_rsdp_space()));
1683 addend = (uint32_t) s->u64RamSize - 0x10000;
1684 Log(("RSDT 0x%08X XSDT 0x%08X\n", rsdt_addr + addend, xsdt_addr + addend));
1685 Log(("FACS 0x%08X FADT 0x%08X\n", facs_addr + addend, fadt_addr + addend));
1686 Log(("DSDT 0x%08X\n", dsdt_addr + addend));
1687 acpiSetupRSDP ((ACPITBLRSDP*)s->au8RSDPPage, rsdt_addr + addend, xsdt_addr + addend);
1688 acpiSetupDSDT (s, dsdt_addr + addend);
1689 acpiSetupFACS (s, facs_addr + addend);
1690 acpiSetupFADT (s, fadt_addr + addend, facs_addr + addend, dsdt_addr + addend);
1691
1692 rsdt_addrs[0] = fadt_addr + addend;
1693 if (s->u8UseIOApic)
1694 {
1695 acpiSetupMADT (s, apic_addr + addend);
1696 rsdt_addrs[1] = apic_addr + addend;
1697 }
1698
1699 rc = acpiSetupRSDT (s, rsdt_addr + addend, cAddr, rsdt_addrs);
1700 if (RT_FAILURE(rc))
1701 return rc;
1702 return acpiSetupXSDT (s, xsdt_addr + addend, cAddr, rsdt_addrs);
1703}
1704
1705/**
1706 * Construct a device instance for a VM.
1707 *
1708 * @returns VBox status.
1709 * @param pDevIns The device instance data.
1710 * If the registration structure is needed, pDevIns->pDevReg points to it.
1711 * @param iInstance Instance number. Use this to figure out which registers and such to use.
1712 * The device number is also found in pDevIns->iInstance, but since it's
1713 * likely to be freqently used PDM passes it as parameter.
1714 * @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
1715 * of the device instance. It's also found in pDevIns->pCfgHandle, but like
1716 * iInstance it's expected to be used a bit in this function.
1717 */
1718static DECLCALLBACK(int) acpiConstruct (PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
1719{
1720 int rc;
1721 ACPIState *s = PDMINS_2_DATA (pDevIns, ACPIState *);
1722 uint32_t rsdp_addr;
1723 PCIDevice *dev;
1724 bool fGCEnabled;
1725 bool fR0Enabled;
1726
1727 /* Validate and read the configuration. */
1728 if (!CFGMR3AreValuesValid (pCfgHandle,
1729 "RamSize\0"
1730 "IOAPIC\0"
1731 "NumCPUs\0"
1732 "GCEnabled\0"
1733 "R0Enabled\0"
1734 "FdcEnabled\0"))
1735 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
1736 N_("Configuration error: Invalid config key for ACPI device"));
1737
1738 s->pDevIns = pDevIns;
1739
1740 /* query whether we are supposed to present an IOAPIC */
1741 rc = CFGMR3QueryU8 (pCfgHandle, "IOAPIC", &s->u8UseIOApic);
1742 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1743 s->u8UseIOApic = 1;
1744 else if (RT_FAILURE (rc))
1745 return PDMDEV_SET_ERROR(pDevIns, rc,
1746 N_("Configuration error: Failed to read \"IOAPIC\""));
1747
1748 rc = CFGMR3QueryU16Def(pCfgHandle, "NumCPUs", &s->cCpus, 1);
1749 if (RT_FAILURE(rc))
1750 return PDMDEV_SET_ERROR(pDevIns, rc,
1751 N_("Configuration error: Querying \"NumCPUs\" as integer failed"));
1752
1753 /* query whether we are supposed to present an FDC controller */
1754 rc = CFGMR3QueryU8 (pCfgHandle, "FdcEnabled", &s->u8UseFdc);
1755 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1756 s->u8UseFdc = 1;
1757 else if (RT_FAILURE (rc))
1758 return PDMDEV_SET_ERROR(pDevIns, rc,
1759 N_("Configuration error: Failed to read \"FdcEnabled\""));
1760
1761 rc = CFGMR3QueryBool (pCfgHandle, "GCEnabled", &fGCEnabled);
1762 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1763 fGCEnabled = true;
1764 else if (RT_FAILURE (rc))
1765 return PDMDEV_SET_ERROR(pDevIns, rc,
1766 N_("Configuration error: Failed to read \"GCEnabled\""));
1767
1768 rc = CFGMR3QueryBool(pCfgHandle, "R0Enabled", &fR0Enabled);
1769 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1770 fR0Enabled = true;
1771 else if (RT_FAILURE(rc))
1772 return PDMDEV_SET_ERROR(pDevIns, rc,
1773 N_("configuration error: failed to read R0Enabled as boolean"));
1774
1775 /* */
1776 rsdp_addr = find_rsdp_space ();
1777 if (!rsdp_addr)
1778 return PDMDEV_SET_ERROR(pDevIns, VERR_NO_MEMORY,
1779 N_("Can not find space for RSDP. ACPI is disabled"));
1780
1781 rc = acpiPlantTables (s);
1782 if (RT_FAILURE (rc))
1783 return rc;
1784
1785 rc = PDMDevHlpROMRegister (pDevIns, rsdp_addr, 0x1000, s->au8RSDPPage, false /* fShadow */, "ACPI RSDP");
1786 if (RT_FAILURE (rc))
1787 return rc;
1788
1789#define R(addr, cnt, writer, reader, description) \
1790 do { \
1791 rc = PDMDevHlpIOPortRegister (pDevIns, addr, cnt, s, writer, reader, \
1792 NULL, NULL, description); \
1793 if (RT_FAILURE (rc)) \
1794 return rc; \
1795 } while (0)
1796#define L (GPE0_BLK_LEN / 2)
1797
1798 R (PM1a_EVT_BLK+2, 1, acpiPM1aEnWrite, acpiPm1aEnRead, "ACPI PM1a Enable");
1799 R (PM1a_EVT_BLK, 1, acpiPM1aStsWrite, acpiPm1aStsRead, "ACPI PM1a Status");
1800 R (PM1a_CTL_BLK, 1, acpiPM1aCtlWrite, acpiPm1aCtlRead, "ACPI PM1a Control");
1801 R (PM_TMR_BLK, 1, NULL, acpiPMTmrRead, "ACPI PM Timer");
1802 R (SMI_CMD, 1, acpiSmiWrite, NULL, "ACPI SMI");
1803#ifdef DEBUG_ACPI
1804 R (DEBUG_HEX, 1, acpiDhexWrite, NULL, "ACPI Debug hex");
1805 R (DEBUG_CHR, 1, acpiDchrWrite, NULL, "ACPI Debug char");
1806#endif
1807 R (BAT_INDEX, 1, acpiBatIndexWrite, NULL, "ACPI Battery status index");
1808 R (BAT_DATA, 1, NULL, acpiBatDataRead, "ACPI Battery status data");
1809 R (SYSI_INDEX, 1, acpiSysInfoIndexWrite, NULL, "ACPI system info index");
1810 R (SYSI_DATA, 1, acpiSysInfoDataWrite, acpiSysInfoDataRead, "ACPI system info data");
1811 R (FDC_STATUS, 1, NULL, acpiFdcStatusRead, "ACPI FDC status index");
1812 R (GPE0_BLK + L, L, acpiGpe0EnWrite, acpiGpe0EnRead, "ACPI GPE0 Enable");
1813 R (GPE0_BLK, L, acpiGpe0StsWrite, acpiGpe0StsRead, "ACPI GPE0 Status");
1814 R (ACPI_RESET_BLK, 1, acpiResetWrite, NULL, "ACPI Reset");
1815#undef L
1816#undef R
1817
1818 /* register GC stuff */
1819 if (fGCEnabled)
1820 {
1821 rc = PDMDevHlpIOPortRegisterGC (pDevIns, PM_TMR_BLK, 1, 0, NULL, "acpiPMTmrRead",
1822 NULL, NULL, "ACPI PM Timer");
1823 AssertRCReturn(rc, rc);
1824 }
1825
1826 /* register R0 stuff */
1827 if (fR0Enabled)
1828 {
1829 rc = PDMDevHlpIOPortRegisterR0 (pDevIns, PM_TMR_BLK, 1, 0, NULL, "acpiPMTmrRead",
1830 NULL, NULL, "ACPI PM Timer");
1831 AssertRCReturn(rc, rc);
1832 }
1833
1834 rc = PDMDevHlpTMTimerCreate (pDevIns, TMCLOCK_VIRTUAL_SYNC, acpiTimer, "ACPI Timer", &s->tsR3);
1835 if (RT_FAILURE(rc))
1836 {
1837 AssertMsgFailed(("pfnTMTimerCreate -> %Rrc\n", rc));
1838 return rc;
1839 }
1840
1841 s->tsR0 = TMTimerR0Ptr (s->tsR3);
1842 s->tsRC = TMTimerRCPtr (s->tsR3);
1843 s->pm_timer_initial = TMTimerGet (s->tsR3);
1844 acpiPMTimerReset (s);
1845
1846 dev = &s->dev;
1847 dev->config[0x00] = 0x86;
1848 dev->config[0x01] = 0x80;
1849
1850 dev->config[0x02] = 0x13;
1851 dev->config[0x03] = 0x71;
1852
1853 dev->config[0x04] = 0x01;
1854 dev->config[0x05] = 0x00;
1855
1856 dev->config[0x06] = 0x80;
1857 dev->config[0x07] = 0x02;
1858 dev->config[0x08] = 0x08;
1859 dev->config[0x09] = 0x00;
1860
1861 dev->config[0x0a] = 0x80;
1862 dev->config[0x0b] = 0x06;
1863
1864 dev->config[0x0e] = 0x80;
1865 dev->config[0x0f] = 0x00;
1866
1867#if 0 /* The ACPI controller usually has no subsystem ID. */
1868 dev->config[0x2c] = 0x86;
1869 dev->config[0x2d] = 0x80;
1870 dev->config[0x2e] = 0x00;
1871 dev->config[0x2f] = 0x00;
1872#endif
1873 dev->config[0x3c] = SCI_INT;
1874
1875 rc = PDMDevHlpPCIRegister (pDevIns, dev);
1876 if (RT_FAILURE (rc))
1877 return rc;
1878
1879 rc = PDMDevHlpSSMRegister (pDevIns, pDevIns->pDevReg->szDeviceName, iInstance, 4, sizeof(*s),
1880 NULL, acpi_save_state, NULL, NULL, acpi_load_state, NULL);
1881 if (RT_FAILURE(rc))
1882 return rc;
1883
1884 /*
1885 * Interfaces
1886 */
1887 /* IBase */
1888 s->IBase.pfnQueryInterface = acpiQueryInterface;
1889 /* IACPIPort */
1890 s->IACPIPort.pfnSleepButtonPress = acpiSleepButtonPress;
1891 s->IACPIPort.pfnPowerButtonPress = acpiPowerButtonPress;
1892 s->IACPIPort.pfnGetPowerButtonHandled = acpiGetPowerButtonHandled;
1893
1894 /*
1895 * Get the corresponding connector interface
1896 */
1897 rc = PDMDevHlpDriverAttach (pDevIns, 0, &s->IBase, &s->pDrvBase, "ACPI Driver Port");
1898 if (RT_SUCCESS (rc))
1899 {
1900 s->pDrv = (PPDMIACPICONNECTOR)s->pDrvBase->pfnQueryInterface (s->pDrvBase,
1901 PDMINTERFACE_ACPI_CONNECTOR);
1902 if (!s->pDrv)
1903 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_MISSING_INTERFACE,
1904 N_("LUN #0 doesn't have an ACPI connector interface"));
1905 }
1906 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
1907 {
1908 Log (("acpi: %s/%d: warning: no driver attached to LUN #0!\n",
1909 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
1910 rc = VINF_SUCCESS;
1911 }
1912 else
1913 return PDMDEV_SET_ERROR(pDevIns, rc,
1914 N_("Failed to attach LUN #0"));
1915
1916 return rc;
1917}
1918
1919/**
1920 * Relocates the GC pointer members.
1921 */
1922static DECLCALLBACK(void) acpiRelocate (PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
1923{
1924 ACPIState *s = PDMINS_2_DATA (pDevIns, ACPIState *);
1925 s->tsRC = TMTimerRCPtr (s->CTX_SUFF(ts));
1926}
1927
1928static DECLCALLBACK(void) acpiReset (PPDMDEVINS pDevIns)
1929{
1930 ACPIState *s = PDMINS_2_DATA (pDevIns, ACPIState *);
1931
1932 s->pm1a_en = 0;
1933 s->pm1a_sts = 0;
1934 s->pm1a_ctl = 0;
1935 s->pm_timer_initial = TMTimerGet (s->CTX_SUFF(ts));
1936 acpiPMTimerReset(s);
1937 s->uBatteryIndex = 0;
1938 s->uSystemInfoIndex = 0;
1939 s->gpe0_en = 0;
1940 s->gpe0_sts = 0;
1941 s->uSleepState = 0;
1942
1943 acpiPlantTables(s);
1944}
1945
1946/**
1947 * The device registration structure.
1948 */
1949const PDMDEVREG g_DeviceACPI =
1950{
1951 /* u32Version */
1952 PDM_DEVREG_VERSION,
1953 /* szDeviceName */
1954 "acpi",
1955 /* szGCMod */
1956 "VBoxDDGC.gc",
1957 /* szR0Mod */
1958 "VBoxDDR0.r0",
1959 /* pszDescription */
1960 "Advanced Configuration and Power Interface",
1961 /* fFlags */
1962 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GC | PDM_DEVREG_FLAGS_R0,
1963 /* fClass */
1964 PDM_DEVREG_CLASS_ACPI,
1965 /* cMaxInstances */
1966 ~0,
1967 /* cbInstance */
1968 sizeof(ACPIState),
1969 /* pfnConstruct */
1970 acpiConstruct,
1971 /* pfnDestruct */
1972 NULL,
1973 /* pfnRelocate */
1974 acpiRelocate,
1975 /* pfnIOCtl */
1976 NULL,
1977 /* pfnPowerOn */
1978 NULL,
1979 /* pfnReset */
1980 acpiReset,
1981 /* pfnSuspend */
1982 NULL,
1983 /* pfnResume */
1984 NULL,
1985 /* pfnAttach */
1986 NULL,
1987 /* pfnDetach */
1988 NULL,
1989 /* pfnQueryInterface. */
1990 NULL
1991};
1992
1993#endif /* IN_RING3 */
1994#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
1995
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette