VirtualBox

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

Last change on this file since 6290 was 6156, checked in by vboxsync, 17 years ago

implemented the ACPI sleep button

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