VirtualBox

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

Last change on this file since 13384 was 13079, checked in by vboxsync, 16 years ago

added getGuestEnteredACPIMode()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 68.7 KB
Line 
1/* $Id: DevACPI.cpp 13079 2008-10-08 13:40:19Z 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
901/**
902 * Check if the ACPI power button event was handled.
903 *
904 * @returns VBox status code
905 * @param pInterface Pointer to the interface structure containing the called function pointer.
906 * @param pfHandled Return true if the power button event was handled by the guest.
907 */
908static DECLCALLBACK(int) acpiGetPowerButtonHandled(PPDMIACPIPORT pInterface, bool *pfHandled)
909{
910 ACPIState *s = IACPIPORT_2_ACPISTATE(pInterface);
911 *pfHandled = s->fPowerButtonHandled;
912 return VINF_SUCCESS;
913}
914
915/**
916 * Check if the Guest entered into G0 (working) or G1 (sleeping).
917 *
918 * @returns VBox status code
919 * @param pInterface Pointer to the interface structure containing the called function pointer.
920 * @param pfEntered Return true if the guest entered the ACPI mode.
921 */
922static DECLCALLBACK(int) acpiGetGuestEnteredACPIMode(PPDMIACPIPORT pInterface, bool *pfEntered)
923{
924 ACPIState *s = IACPIPORT_2_ACPISTATE(pInterface);
925 *pfEntered = (s->pm1a_ctl & SCI_EN) != 0;
926 return VINF_SUCCESS;
927}
928
929/**
930 * Send an ACPI sleep button event.
931 *
932 * @returns VBox status code
933 * @param pInterface Pointer to the interface structure containing the called function pointer.
934 */
935static DECLCALLBACK(int) acpiSleepButtonPress(PPDMIACPIPORT pInterface)
936{
937 ACPIState *s = IACPIPORT_2_ACPISTATE(pInterface);
938 update_pm1a (s, s->pm1a_sts | SLPBTN_STS, s->pm1a_en);
939 return VINF_SUCCESS;
940}
941
942/* PM1a_EVT_BLK enable */
943static uint32_t acpiPm1aEnReadw (ACPIState *s, uint32_t addr)
944{
945 uint16_t val = s->pm1a_en;
946 Log (("acpi: acpiPm1aEnReadw -> %#x\n", val));
947 return val;
948}
949
950static void acpiPM1aEnWritew (ACPIState *s, uint32_t addr, uint32_t val)
951{
952 Log (("acpi: acpiPM1aEnWritew <- %#x (%#x)\n", val, val & ~(RSR_EN | IGN_EN)));
953 val &= ~(RSR_EN | IGN_EN);
954 update_pm1a (s, s->pm1a_sts, val);
955}
956
957/* PM1a_EVT_BLK status */
958static uint32_t acpiPm1aStsReadw (ACPIState *s, uint32_t addr)
959{
960 uint16_t val = s->pm1a_sts;
961 Log (("acpi: acpiPm1aStsReadw -> %#x\n", val));
962 return val;
963}
964
965static void acpiPM1aStsWritew (ACPIState *s, uint32_t addr, uint32_t val)
966{
967 Log (("acpi: acpiPM1aStsWritew <- %#x (%#x)\n", val, val & ~(RSR_STS | IGN_STS)));
968 if (val & PWRBTN_STS)
969 s->fPowerButtonHandled = true; /* Remember that the guest handled the last power button event */
970 val = s->pm1a_sts & ~(val & ~(RSR_STS | IGN_STS));
971 update_pm1a (s, val, s->pm1a_en);
972}
973
974/* PM1a_CTL_BLK */
975static uint32_t acpiPm1aCtlReadw (ACPIState *s, uint32_t addr)
976{
977 uint16_t val = s->pm1a_ctl;
978 Log (("acpi: acpiPm1aCtlReadw -> %#x\n", val));
979 return val;
980}
981
982static int acpiPM1aCtlWritew (ACPIState *s, uint32_t addr, uint32_t val)
983{
984 uint32_t uSleepState;
985
986 Log (("acpi: acpiPM1aCtlWritew <- %#x (%#x)\n", val, val & ~(RSR_CNT | IGN_CNT)));
987 s->pm1a_ctl = val & ~(RSR_CNT | IGN_CNT);
988
989 uSleepState = (s->pm1a_ctl >> SLP_TYPx_SHIFT) & SLP_TYPx_MASK;
990 if (uSleepState != s->uSleepState)
991 {
992 s->uSleepState = uSleepState;
993 switch (uSleepState)
994 {
995 case 0x00: /* S0 */
996 break;
997 case 0x05: /* S5 */
998 LogRel (("Entering S5 (power down)\n"));
999 return acpiPowerDown (s);
1000 default:
1001 AssertMsgFailed (("Unknown sleep state %#x\n", uSleepState));
1002 break;
1003 }
1004 }
1005 return VINF_SUCCESS;
1006}
1007
1008/* GPE0_BLK */
1009static uint32_t acpiGpe0EnReadb (ACPIState *s, uint32_t addr)
1010{
1011 uint8_t val = s->gpe0_en;
1012 Log (("acpi: acpiGpe0EnReadl -> %#x\n", val));
1013 return val;
1014}
1015
1016static void acpiGpe0EnWriteb (ACPIState *s, uint32_t addr, uint32_t val)
1017{
1018 Log (("acpi: acpiGpe0EnWritel <- %#x\n", val));
1019 update_gpe0 (s, s->gpe0_sts, val);
1020}
1021
1022static uint32_t acpiGpe0StsReadb (ACPIState *s, uint32_t addr)
1023{
1024 uint8_t val = s->gpe0_sts;
1025 Log (("acpi: acpiGpe0StsReadl -> %#x\n", val));
1026 return val;
1027}
1028
1029static void acpiGpe0StsWriteb (ACPIState *s, uint32_t addr, uint32_t val)
1030{
1031 val = s->gpe0_sts & ~val;
1032 update_gpe0 (s, val, s->gpe0_en);
1033 Log (("acpi: acpiGpe0StsWritel <- %#x\n", val));
1034}
1035
1036static int acpiResetWriteU8(ACPIState *s, uint32_t addr, uint32_t val)
1037{
1038 int rc = VINF_SUCCESS;
1039
1040 Log(("ACPI: acpiResetWriteU8: %x %x\n", addr, val));
1041 if (val == ACPI_RESET_REG_VAL)
1042 {
1043# ifndef IN_RING3
1044 rc = VINF_IOM_HC_IOPORT_WRITE;
1045# else /* IN_RING3 */
1046 rc = PDMDevHlpVMReset(s->pDevIns);
1047# endif /* !IN_RING3 */
1048 }
1049 return rc;
1050}
1051
1052/* SMI */
1053static void acpiSmiWriteU8 (ACPIState *s, uint32_t addr, uint32_t val)
1054{
1055 Log (("acpi: acpiSmiWriteU8 %#x\n", val));
1056 if (val == ACPI_ENABLE)
1057 s->pm1a_ctl |= SCI_EN;
1058 else if (val == ACPI_DISABLE)
1059 s->pm1a_ctl &= ~SCI_EN;
1060 else
1061 Log (("acpi: acpiSmiWriteU8 %#x <- unknown value\n", val));
1062}
1063
1064static uint32_t find_rsdp_space (void)
1065{
1066 return 0xe0000;
1067}
1068
1069static void acpiPMTimerReset (ACPIState *s)
1070{
1071 uint64_t interval, freq;
1072
1073 freq = TMTimerGetFreq (s->CTX_SUFF(ts));
1074 interval = ASMMultU64ByU32DivByU32 (0xffffffff, freq, PM_TMR_FREQ);
1075 Log (("interval = %RU64\n", interval));
1076 TMTimerSet (s->CTX_SUFF(ts), TMTimerGet (s->CTX_SUFF(ts)) + interval);
1077}
1078
1079static DECLCALLBACK(void) acpiTimer (PPDMDEVINS pDevIns, PTMTIMER pTimer)
1080{
1081 ACPIState *s = PDMINS_2_DATA (pDevIns, ACPIState *);
1082
1083 Log (("acpi: pm timer sts %#x (%d), en %#x (%d)\n",
1084 s->pm1a_sts, (s->pm1a_sts & TMR_STS) != 0,
1085 s->pm1a_en, (s->pm1a_en & TMR_EN) != 0));
1086
1087 update_pm1a (s, s->pm1a_sts | TMR_STS, s->pm1a_en);
1088 acpiPMTimerReset (s);
1089}
1090
1091/**
1092 * _BST method.
1093 */
1094static void acpiFetchBatteryStatus (ACPIState *s)
1095{
1096 uint32_t *p = s->au8BatteryInfo;
1097 bool fPresent; /* battery present? */
1098 PDMACPIBATCAPACITY hostRemainingCapacity; /* 0..100 */
1099 PDMACPIBATSTATE hostBatteryState; /* bitfield */
1100 uint32_t hostPresentRate; /* 0..1000 */
1101 int rc;
1102
1103 if (!s->pDrv)
1104 return;
1105 rc = s->pDrv->pfnQueryBatteryStatus (s->pDrv, &fPresent, &hostRemainingCapacity,
1106 &hostBatteryState, &hostPresentRate);
1107 AssertRC (rc);
1108
1109 /* default values */
1110 p[BAT_STATUS_STATE] = hostBatteryState;
1111 p[BAT_STATUS_PRESENT_RATE] = hostPresentRate == ~0U ? 0xFFFFFFFF
1112 : hostPresentRate * 50; /* mW */
1113 p[BAT_STATUS_REMAINING_CAPACITY] = 50000; /* mWh */
1114 p[BAT_STATUS_PRESENT_VOLTAGE] = 10000; /* mV */
1115
1116 /* did we get a valid battery state? */
1117 if (hostRemainingCapacity != PDM_ACPI_BAT_CAPACITY_UNKNOWN)
1118 p[BAT_STATUS_REMAINING_CAPACITY] = hostRemainingCapacity * 500; /* mWh */
1119 if (hostBatteryState == PDM_ACPI_BAT_STATE_CHARGED)
1120 p[BAT_STATUS_PRESENT_RATE] = 0; /* mV */
1121}
1122
1123/**
1124 * _BIF method.
1125 */
1126static void acpiFetchBatteryInfo (ACPIState *s)
1127{
1128 uint32_t *p = s->au8BatteryInfo;
1129
1130 p[BAT_INFO_UNITS] = 0; /* mWh */
1131 p[BAT_INFO_DESIGN_CAPACITY] = 50000; /* mWh */
1132 p[BAT_INFO_LAST_FULL_CHARGE_CAPACITY] = 50000; /* mWh */
1133 p[BAT_INFO_TECHNOLOGY] = BAT_TECH_PRIMARY;
1134 p[BAT_INFO_DESIGN_VOLTAGE] = 10000; /* mV */
1135 p[BAT_INFO_DESIGN_CAPACITY_OF_WARNING] = 100; /* mWh */
1136 p[BAT_INFO_DESIGN_CAPACITY_OF_LOW] = 50; /* mWh */
1137 p[BAT_INFO_CAPACITY_GRANULARITY_1] = 1; /* mWh */
1138 p[BAT_INFO_CAPACITY_GRANULARITY_2] = 1; /* mWh */
1139}
1140
1141/**
1142 * _STA method.
1143 */
1144static uint32_t acpiGetBatteryDeviceStatus (ACPIState *s)
1145{
1146 bool fPresent; /* battery present? */
1147 PDMACPIBATCAPACITY hostRemainingCapacity; /* 0..100 */
1148 PDMACPIBATSTATE hostBatteryState; /* bitfield */
1149 uint32_t hostPresentRate; /* 0..1000 */
1150 int rc;
1151
1152 if (!s->pDrv)
1153 return 0;
1154 rc = s->pDrv->pfnQueryBatteryStatus (s->pDrv, &fPresent, &hostRemainingCapacity,
1155 &hostBatteryState, &hostPresentRate);
1156 AssertRC (rc);
1157
1158 return fPresent
1159 ? STA_DEVICE_PRESENT_MASK /* present */
1160 | STA_DEVICE_ENABLED_MASK /* enabled and decodes its resources */
1161 | STA_DEVICE_SHOW_IN_UI_MASK /* should be shown in UI */
1162 | STA_DEVICE_FUNCTIONING_PROPERLY_MASK /* functioning properly */
1163 | STA_BATTERY_PRESENT_MASK /* battery is present */
1164 : 0; /* device not present */
1165}
1166
1167static uint32_t acpiGetPowerSource (ACPIState *s)
1168{
1169 PDMACPIPOWERSOURCE ps;
1170
1171 /* query the current power source from the host driver */
1172 if (!s->pDrv)
1173 return AC_ONLINE;
1174 int rc = s->pDrv->pfnQueryPowerSource (s->pDrv, &ps);
1175 AssertRC (rc);
1176 return ps == PDM_ACPI_POWER_SOURCE_BATTERY ? AC_OFFLINE : AC_ONLINE;
1177}
1178
1179IO_WRITE_PROTO (acpiBatIndexWrite)
1180{
1181 ACPIState *s = (ACPIState *)pvUser;
1182
1183 switch (cb)
1184 {
1185 case 4:
1186 u32 >>= s->u8IndexShift;
1187 /* see comment at the declaration of u8IndexShift */
1188 if (s->u8IndexShift == 0 && u32 == (BAT_DEVICE_STATUS << 2))
1189 {
1190 s->u8IndexShift = 2;
1191 u32 >>= 2;
1192 }
1193 Assert (u32 < BAT_INDEX_LAST);
1194 s->uBatteryIndex = u32;
1195 break;
1196 default:
1197 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1198 break;
1199 }
1200 return VINF_SUCCESS;
1201}
1202
1203IO_READ_PROTO (acpiBatDataRead)
1204{
1205 ACPIState *s = (ACPIState *)pvUser;
1206
1207 switch (cb)
1208 {
1209 case 4:
1210 switch (s->uBatteryIndex)
1211 {
1212 case BAT_STATUS_STATE:
1213 acpiFetchBatteryStatus(s);
1214 case BAT_STATUS_PRESENT_RATE:
1215 case BAT_STATUS_REMAINING_CAPACITY:
1216 case BAT_STATUS_PRESENT_VOLTAGE:
1217 *pu32 = s->au8BatteryInfo[s->uBatteryIndex];
1218 break;
1219
1220 case BAT_INFO_UNITS:
1221 acpiFetchBatteryInfo(s);
1222 case BAT_INFO_DESIGN_CAPACITY:
1223 case BAT_INFO_LAST_FULL_CHARGE_CAPACITY:
1224 case BAT_INFO_TECHNOLOGY:
1225 case BAT_INFO_DESIGN_VOLTAGE:
1226 case BAT_INFO_DESIGN_CAPACITY_OF_WARNING:
1227 case BAT_INFO_DESIGN_CAPACITY_OF_LOW:
1228 case BAT_INFO_CAPACITY_GRANULARITY_1:
1229 case BAT_INFO_CAPACITY_GRANULARITY_2:
1230 *pu32 = s->au8BatteryInfo[s->uBatteryIndex];
1231 break;
1232
1233 case BAT_DEVICE_STATUS:
1234 *pu32 = acpiGetBatteryDeviceStatus(s);
1235 break;
1236
1237 case BAT_POWER_SOURCE:
1238 *pu32 = acpiGetPowerSource(s);
1239 break;
1240
1241 default:
1242 AssertMsgFailed (("Invalid battery index %d\n", s->uBatteryIndex));
1243 break;
1244 }
1245 break;
1246 default:
1247 return VERR_IOM_IOPORT_UNUSED;
1248 }
1249 return VINF_SUCCESS;
1250}
1251
1252IO_READ_PROTO (acpiFdcStatusRead)
1253{
1254 ACPIState *s = (ACPIState *)pvUser;
1255
1256 switch (cb)
1257 {
1258 case 4:
1259 *pu32 = s->u8UseFdc
1260 ? STA_DEVICE_PRESENT_MASK /* present */
1261 | STA_DEVICE_ENABLED_MASK /* enabled and decodes its resources */
1262 | STA_DEVICE_SHOW_IN_UI_MASK /* should be shown in UI */
1263 | STA_DEVICE_FUNCTIONING_PROPERLY_MASK /* functioning properly */
1264 : 0; /* device not present */
1265 break;
1266 default:
1267 return VERR_IOM_IOPORT_UNUSED;
1268 }
1269 return VINF_SUCCESS;
1270}
1271
1272IO_WRITE_PROTO (acpiSysInfoIndexWrite)
1273{
1274 ACPIState *s = (ACPIState *)pvUser;
1275
1276 Log(("system_index = %d, %d\n", u32, u32 >> 2));
1277 switch (cb) {
1278 case 4:
1279 if (u32 == SYSTEM_INFO_INDEX_VALID || u32 == SYSTEM_INFO_INDEX_INVALID)
1280 s->uSystemInfoIndex = u32;
1281 else
1282 {
1283 u32 >>= s->u8IndexShift;
1284 Assert (u32 < SYSTEM_INFO_INDEX_LAST);
1285 s->uSystemInfoIndex = u32;
1286 }
1287 break;
1288
1289 default:
1290 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1291 break;
1292 }
1293 return VINF_SUCCESS;
1294}
1295
1296IO_READ_PROTO (acpiSysInfoDataRead)
1297{
1298 ACPIState *s = (ACPIState *)pvUser;
1299
1300 switch (cb)
1301 {
1302 case 4:
1303 switch (s->uSystemInfoIndex)
1304 {
1305 case SYSTEM_INFO_INDEX_MEMORY_LENGTH:
1306 *pu32 = s->u64RamSize;
1307 break;
1308
1309 case SYSTEM_INFO_INDEX_USE_IOAPIC:
1310 *pu32 = s->u8UseIOApic;
1311 break;
1312
1313 default:
1314 AssertMsgFailed (("Invalid system info index %d\n", s->uSystemInfoIndex));
1315 break;
1316 }
1317 break;
1318
1319 default:
1320 return VERR_IOM_IOPORT_UNUSED;
1321 }
1322
1323 Log(("index %d val %d\n", s->uSystemInfoIndex, *pu32));
1324 return VINF_SUCCESS;
1325}
1326
1327IO_WRITE_PROTO (acpiSysInfoDataWrite)
1328{
1329 ACPIState *s = (ACPIState *)pvUser;
1330
1331 Log(("addr=%#x cb=%d u32=%#x si=%#x\n", Port, cb, u32, s->uSystemInfoIndex));
1332
1333 if (cb == 4 && u32 == 0xbadc0de)
1334 {
1335 switch (s->uSystemInfoIndex)
1336 {
1337 case SYSTEM_INFO_INDEX_INVALID:
1338 s->u8IndexShift = 0;
1339 break;
1340
1341 case SYSTEM_INFO_INDEX_VALID:
1342 s->u8IndexShift = 2;
1343 break;
1344
1345 default:
1346 AssertMsgFailed(("Port=%#x cb=%d u32=%#x system_index=%#x\n",
1347 Port, cb, u32, s->uSystemInfoIndex));
1348 break;
1349 }
1350 }
1351 else
1352 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1353 return VINF_SUCCESS;
1354}
1355
1356/* IO Helpers */
1357IO_READ_PROTO (acpiPm1aEnRead)
1358{
1359 switch (cb)
1360 {
1361 case 2:
1362 *pu32 = acpiPm1aEnReadw ((ACPIState*)pvUser, Port);
1363 break;
1364 default:
1365 return VERR_IOM_IOPORT_UNUSED;
1366 }
1367 return VINF_SUCCESS;
1368}
1369
1370IO_READ_PROTO (acpiPm1aStsRead)
1371{
1372 switch (cb)
1373 {
1374 case 2:
1375 *pu32 = acpiPm1aStsReadw ((ACPIState*)pvUser, Port);
1376 break;
1377 default:
1378 return VERR_IOM_IOPORT_UNUSED;
1379 }
1380 return VINF_SUCCESS;
1381}
1382
1383IO_READ_PROTO (acpiPm1aCtlRead)
1384{
1385 switch (cb)
1386 {
1387 case 2:
1388 *pu32 = acpiPm1aCtlReadw ((ACPIState*)pvUser, Port);
1389 break;
1390 default:
1391 return VERR_IOM_IOPORT_UNUSED;
1392 }
1393 return VINF_SUCCESS;
1394}
1395
1396IO_WRITE_PROTO (acpiPM1aEnWrite)
1397{
1398 switch (cb)
1399 {
1400 case 2:
1401 acpiPM1aEnWritew ((ACPIState*)pvUser, Port, u32);
1402 break;
1403 default:
1404 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1405 break;
1406 }
1407 return VINF_SUCCESS;
1408}
1409
1410IO_WRITE_PROTO (acpiPM1aStsWrite)
1411{
1412 switch (cb)
1413 {
1414 case 2:
1415 acpiPM1aStsWritew ((ACPIState*)pvUser, Port, u32);
1416 break;
1417 default:
1418 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1419 break;
1420 }
1421 return VINF_SUCCESS;
1422}
1423
1424IO_WRITE_PROTO (acpiPM1aCtlWrite)
1425{
1426 switch (cb)
1427 {
1428 case 2:
1429 return acpiPM1aCtlWritew ((ACPIState*)pvUser, Port, u32);
1430 default:
1431 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1432 break;
1433 }
1434 return VINF_SUCCESS;
1435}
1436
1437#endif /* IN_RING3 */
1438
1439/**
1440 * PMTMR readable from host/guest.
1441 */
1442IO_READ_PROTO (acpiPMTmrRead)
1443{
1444 if (cb == 4)
1445 {
1446 ACPIState *s = PDMINS_2_DATA (pDevIns, ACPIState *);
1447 int64_t now = TMTimerGet (s->CTX_SUFF(ts));
1448 int64_t elapsed = now - s->pm_timer_initial;
1449
1450 *pu32 = ASMMultU64ByU32DivByU32 (elapsed, PM_TMR_FREQ, TMTimerGetFreq (s->CTX_SUFF(ts)));
1451 Log (("acpi: acpiPMTmrRead -> %#x\n", *pu32));
1452 return VINF_SUCCESS;
1453 }
1454 return VERR_IOM_IOPORT_UNUSED;
1455}
1456
1457#ifdef IN_RING3
1458
1459IO_READ_PROTO (acpiGpe0StsRead)
1460{
1461 switch (cb)
1462 {
1463 case 1:
1464 *pu32 = acpiGpe0StsReadb ((ACPIState*)pvUser, Port);
1465 break;
1466 default:
1467 return VERR_IOM_IOPORT_UNUSED;
1468 }
1469 return VINF_SUCCESS;
1470}
1471
1472IO_READ_PROTO (acpiGpe0EnRead)
1473{
1474 switch (cb)
1475 {
1476 case 1:
1477 *pu32 = acpiGpe0EnReadb ((ACPIState*)pvUser, Port);
1478 break;
1479 default:
1480 return VERR_IOM_IOPORT_UNUSED;
1481 }
1482 return VINF_SUCCESS;
1483}
1484
1485IO_WRITE_PROTO (acpiGpe0StsWrite)
1486{
1487 switch (cb)
1488 {
1489 case 1:
1490 acpiGpe0StsWriteb ((ACPIState*)pvUser, Port, u32);
1491 break;
1492 default:
1493 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1494 break;
1495 }
1496 return VINF_SUCCESS;
1497}
1498
1499IO_WRITE_PROTO (acpiGpe0EnWrite)
1500{
1501 switch (cb)
1502 {
1503 case 1:
1504 acpiGpe0EnWriteb ((ACPIState*)pvUser, Port, u32);
1505 break;
1506 default:
1507 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1508 break;
1509 }
1510 return VINF_SUCCESS;
1511}
1512
1513IO_WRITE_PROTO (acpiSmiWrite)
1514{
1515 switch (cb)
1516 {
1517 case 1:
1518 acpiSmiWriteU8 ((ACPIState*)pvUser, Port, u32);
1519 break;
1520 default:
1521 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1522 break;
1523 }
1524 return VINF_SUCCESS;
1525}
1526
1527IO_WRITE_PROTO (acpiResetWrite)
1528{
1529 switch (cb)
1530 {
1531 case 1:
1532 return acpiResetWriteU8 ((ACPIState*)pvUser, Port, u32);
1533 default:
1534 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1535 break;
1536 }
1537 return VINF_SUCCESS;
1538}
1539
1540#ifdef DEBUG_ACPI
1541
1542IO_WRITE_PROTO (acpiDhexWrite)
1543{
1544 switch (cb)
1545 {
1546 case 1:
1547 Log (("%#x\n", u32 & 0xff));
1548 break;
1549 case 2:
1550 Log (("%#6x\n", u32 & 0xffff));
1551 case 4:
1552 Log (("%#10x\n", u32));
1553 break;
1554 default:
1555 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1556 break;
1557 }
1558 return VINF_SUCCESS;
1559}
1560
1561IO_WRITE_PROTO (acpiDchrWrite)
1562{
1563 switch (cb)
1564 {
1565 case 1:
1566 Log (("%c", u32 & 0xff));
1567 break;
1568 default:
1569 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1570 break;
1571 }
1572 return VINF_SUCCESS;
1573}
1574
1575#endif /* DEBUG_ACPI */
1576
1577
1578/**
1579 * Saved state structure description.
1580 */
1581static const SSMFIELD g_AcpiSavedStateFields[] =
1582{
1583 SSMFIELD_ENTRY (ACPIState, pm1a_en),
1584 SSMFIELD_ENTRY (ACPIState, pm1a_sts),
1585 SSMFIELD_ENTRY (ACPIState, pm1a_ctl),
1586 SSMFIELD_ENTRY (ACPIState, pm_timer_initial),
1587 SSMFIELD_ENTRY (ACPIState, gpe0_en),
1588 SSMFIELD_ENTRY (ACPIState, gpe0_sts),
1589 SSMFIELD_ENTRY (ACPIState, uBatteryIndex),
1590 SSMFIELD_ENTRY (ACPIState, uSystemInfoIndex),
1591 SSMFIELD_ENTRY (ACPIState, u64RamSize),
1592 SSMFIELD_ENTRY (ACPIState, u8IndexShift),
1593 SSMFIELD_ENTRY (ACPIState, u8UseIOApic),
1594 SSMFIELD_ENTRY (ACPIState, uSleepState),
1595 SSMFIELD_ENTRY_TERM ()
1596};
1597
1598static DECLCALLBACK(int) acpi_save_state (PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
1599{
1600 ACPIState *s = PDMINS_2_DATA (pDevIns, ACPIState *);
1601 return SSMR3PutStruct (pSSMHandle, s, &g_AcpiSavedStateFields[0]);
1602}
1603
1604static DECLCALLBACK(int) acpi_load_state (PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle,
1605 uint32_t u32Version)
1606{
1607 ACPIState *s = PDMINS_2_DATA (pDevIns, ACPIState *);
1608 int rc;
1609
1610 if (u32Version != 4)
1611 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1612
1613 rc = SSMR3GetStruct (pSSMHandle, s, &g_AcpiSavedStateFields[0]);
1614 if (RT_SUCCESS (rc))
1615 {
1616 acpiFetchBatteryStatus (s);
1617 acpiFetchBatteryInfo (s);
1618 acpiPMTimerReset (s);
1619 }
1620 return rc;
1621}
1622
1623/**
1624 * Queries an interface to the driver.
1625 *
1626 * @returns Pointer to interface.
1627 * @returns NULL if the interface was not supported by the driver.
1628 * @param pInterface Pointer to this interface structure.
1629 * @param enmInterface The requested interface identification.
1630 * @thread Any thread.
1631 */
1632static DECLCALLBACK(void *) acpiQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
1633{
1634 ACPIState *pThis = (ACPIState*)((uintptr_t)pInterface - RT_OFFSETOF(ACPIState, IBase));
1635 switch (enmInterface)
1636 {
1637 case PDMINTERFACE_BASE:
1638 return &pThis->IBase;
1639 case PDMINTERFACE_ACPI_PORT:
1640 return &pThis->IACPIPort;
1641 default:
1642 return NULL;
1643 }
1644}
1645
1646/**
1647 * Create the ACPI tables.
1648 */
1649static int acpiPlantTables (ACPIState *s)
1650{
1651 int rc;
1652 RTGCPHYS32 rsdt_addr, xsdt_addr, fadt_addr, facs_addr, dsdt_addr, last_addr, apic_addr = 0;
1653 uint32_t addend = 0;
1654 RTGCPHYS32 rsdt_addrs[4];
1655 uint32_t cAddr;
1656 size_t rsdt_tbl_len = sizeof(ACPITBLHEADER);
1657 size_t xsdt_tbl_len = sizeof(ACPITBLHEADER);
1658
1659 cAddr = 1; /* FADT */
1660 if (s->u8UseIOApic)
1661 cAddr++; /* MADT */
1662
1663 rsdt_tbl_len += cAddr*4; /* each entry: 32 bits phys. address. */
1664 xsdt_tbl_len += cAddr*8; /* each entry: 64 bits phys. address. */
1665
1666 rc = CFGMR3QueryU64 (s->pDevIns->pCfgHandle, "RamSize", &s->u64RamSize);
1667 if (RT_FAILURE (rc))
1668 return PDMDEV_SET_ERROR(s->pDevIns, rc,
1669 N_("Configuration error: Querying "
1670 "\"RamSize\" as integer failed"));
1671
1672 if (s->u64RamSize > (0xffffffff - 0x10000))
1673 return PDMDEV_SET_ERROR(s->pDevIns, VERR_OUT_OF_RANGE,
1674 N_("Configuration error: Invalid \"RamSize\", maximum allowed "
1675 "value is 4095MB"));
1676 rsdt_addr = 0;
1677 xsdt_addr = RT_ALIGN_32 (rsdt_addr + rsdt_tbl_len, 16);
1678 fadt_addr = RT_ALIGN_32 (xsdt_addr + xsdt_tbl_len, 16);
1679 facs_addr = RT_ALIGN_32 (fadt_addr + sizeof(ACPITBLFADT), 16);
1680 if (s->u8UseIOApic)
1681 {
1682 apic_addr = RT_ALIGN_32 (facs_addr + sizeof(ACPITBLFACS), 16);
1683#ifdef VBOX_WITH_SMP_GUESTS
1684 /**
1685 * @todo nike: maybe some refactoring needed to compute tables layout,
1686 * but as this code is executed only once it doesn't make sense to optimize much
1687 */
1688 dsdt_addr = RT_ALIGN_32 (apic_addr + AcpiTableMADT::sizeFor(s), 16);
1689#else
1690 dsdt_addr = RT_ALIGN_32 (apic_addr + sizeof(ACPITBLMADT), 16);
1691#endif
1692 }
1693 else
1694 {
1695 dsdt_addr = RT_ALIGN_32 (facs_addr + sizeof(ACPITBLFACS), 16);
1696 }
1697
1698 last_addr = RT_ALIGN_32 (dsdt_addr + sizeof(AmlCode), 16);
1699 if (last_addr > 0x10000)
1700 return PDMDEV_SET_ERROR(s->pDevIns, VERR_TOO_MUCH_DATA,
1701 N_("Error: ACPI tables > 64KB"));
1702
1703 Log(("RSDP 0x%08X\n", find_rsdp_space()));
1704 addend = (uint32_t) s->u64RamSize - 0x10000;
1705 Log(("RSDT 0x%08X XSDT 0x%08X\n", rsdt_addr + addend, xsdt_addr + addend));
1706 Log(("FACS 0x%08X FADT 0x%08X\n", facs_addr + addend, fadt_addr + addend));
1707 Log(("DSDT 0x%08X\n", dsdt_addr + addend));
1708 acpiSetupRSDP ((ACPITBLRSDP*)s->au8RSDPPage, rsdt_addr + addend, xsdt_addr + addend);
1709 acpiSetupDSDT (s, dsdt_addr + addend);
1710 acpiSetupFACS (s, facs_addr + addend);
1711 acpiSetupFADT (s, fadt_addr + addend, facs_addr + addend, dsdt_addr + addend);
1712
1713 rsdt_addrs[0] = fadt_addr + addend;
1714 if (s->u8UseIOApic)
1715 {
1716 acpiSetupMADT (s, apic_addr + addend);
1717 rsdt_addrs[1] = apic_addr + addend;
1718 }
1719
1720 rc = acpiSetupRSDT (s, rsdt_addr + addend, cAddr, rsdt_addrs);
1721 if (RT_FAILURE(rc))
1722 return rc;
1723 return acpiSetupXSDT (s, xsdt_addr + addend, cAddr, rsdt_addrs);
1724}
1725
1726/**
1727 * Construct a device instance for a VM.
1728 *
1729 * @returns VBox status.
1730 * @param pDevIns The device instance data.
1731 * If the registration structure is needed, pDevIns->pDevReg points to it.
1732 * @param iInstance Instance number. Use this to figure out which registers and such to use.
1733 * The device number is also found in pDevIns->iInstance, but since it's
1734 * likely to be freqently used PDM passes it as parameter.
1735 * @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
1736 * of the device instance. It's also found in pDevIns->pCfgHandle, but like
1737 * iInstance it's expected to be used a bit in this function.
1738 */
1739static DECLCALLBACK(int) acpiConstruct (PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
1740{
1741 int rc;
1742 ACPIState *s = PDMINS_2_DATA (pDevIns, ACPIState *);
1743 uint32_t rsdp_addr;
1744 PCIDevice *dev;
1745 bool fGCEnabled;
1746 bool fR0Enabled;
1747
1748 /* Validate and read the configuration. */
1749 if (!CFGMR3AreValuesValid (pCfgHandle,
1750 "RamSize\0"
1751 "IOAPIC\0"
1752 "NumCPUs\0"
1753 "GCEnabled\0"
1754 "R0Enabled\0"
1755 "FdcEnabled\0"))
1756 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
1757 N_("Configuration error: Invalid config key for ACPI device"));
1758
1759 s->pDevIns = pDevIns;
1760
1761 /* query whether we are supposed to present an IOAPIC */
1762 rc = CFGMR3QueryU8 (pCfgHandle, "IOAPIC", &s->u8UseIOApic);
1763 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1764 s->u8UseIOApic = 1;
1765 else if (RT_FAILURE (rc))
1766 return PDMDEV_SET_ERROR(pDevIns, rc,
1767 N_("Configuration error: Failed to read \"IOAPIC\""));
1768
1769 rc = CFGMR3QueryU16Def(pCfgHandle, "NumCPUs", &s->cCpus, 1);
1770 if (RT_FAILURE(rc))
1771 return PDMDEV_SET_ERROR(pDevIns, rc,
1772 N_("Configuration error: Querying \"NumCPUs\" as integer failed"));
1773
1774 /* query whether we are supposed to present an FDC controller */
1775 rc = CFGMR3QueryU8 (pCfgHandle, "FdcEnabled", &s->u8UseFdc);
1776 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1777 s->u8UseFdc = 1;
1778 else if (RT_FAILURE (rc))
1779 return PDMDEV_SET_ERROR(pDevIns, rc,
1780 N_("Configuration error: Failed to read \"FdcEnabled\""));
1781
1782 rc = CFGMR3QueryBool (pCfgHandle, "GCEnabled", &fGCEnabled);
1783 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1784 fGCEnabled = true;
1785 else if (RT_FAILURE (rc))
1786 return PDMDEV_SET_ERROR(pDevIns, rc,
1787 N_("Configuration error: Failed to read \"GCEnabled\""));
1788
1789 rc = CFGMR3QueryBool(pCfgHandle, "R0Enabled", &fR0Enabled);
1790 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1791 fR0Enabled = true;
1792 else if (RT_FAILURE(rc))
1793 return PDMDEV_SET_ERROR(pDevIns, rc,
1794 N_("configuration error: failed to read R0Enabled as boolean"));
1795
1796 /* */
1797 rsdp_addr = find_rsdp_space ();
1798 if (!rsdp_addr)
1799 return PDMDEV_SET_ERROR(pDevIns, VERR_NO_MEMORY,
1800 N_("Can not find space for RSDP. ACPI is disabled"));
1801
1802 rc = acpiPlantTables (s);
1803 if (RT_FAILURE (rc))
1804 return rc;
1805
1806 rc = PDMDevHlpROMRegister (pDevIns, rsdp_addr, 0x1000, s->au8RSDPPage, false /* fShadow */, "ACPI RSDP");
1807 if (RT_FAILURE (rc))
1808 return rc;
1809
1810#define R(addr, cnt, writer, reader, description) \
1811 do { \
1812 rc = PDMDevHlpIOPortRegister (pDevIns, addr, cnt, s, writer, reader, \
1813 NULL, NULL, description); \
1814 if (RT_FAILURE (rc)) \
1815 return rc; \
1816 } while (0)
1817#define L (GPE0_BLK_LEN / 2)
1818
1819 R (PM1a_EVT_BLK+2, 1, acpiPM1aEnWrite, acpiPm1aEnRead, "ACPI PM1a Enable");
1820 R (PM1a_EVT_BLK, 1, acpiPM1aStsWrite, acpiPm1aStsRead, "ACPI PM1a Status");
1821 R (PM1a_CTL_BLK, 1, acpiPM1aCtlWrite, acpiPm1aCtlRead, "ACPI PM1a Control");
1822 R (PM_TMR_BLK, 1, NULL, acpiPMTmrRead, "ACPI PM Timer");
1823 R (SMI_CMD, 1, acpiSmiWrite, NULL, "ACPI SMI");
1824#ifdef DEBUG_ACPI
1825 R (DEBUG_HEX, 1, acpiDhexWrite, NULL, "ACPI Debug hex");
1826 R (DEBUG_CHR, 1, acpiDchrWrite, NULL, "ACPI Debug char");
1827#endif
1828 R (BAT_INDEX, 1, acpiBatIndexWrite, NULL, "ACPI Battery status index");
1829 R (BAT_DATA, 1, NULL, acpiBatDataRead, "ACPI Battery status data");
1830 R (SYSI_INDEX, 1, acpiSysInfoIndexWrite, NULL, "ACPI system info index");
1831 R (SYSI_DATA, 1, acpiSysInfoDataWrite, acpiSysInfoDataRead, "ACPI system info data");
1832 R (FDC_STATUS, 1, NULL, acpiFdcStatusRead, "ACPI FDC status index");
1833 R (GPE0_BLK + L, L, acpiGpe0EnWrite, acpiGpe0EnRead, "ACPI GPE0 Enable");
1834 R (GPE0_BLK, L, acpiGpe0StsWrite, acpiGpe0StsRead, "ACPI GPE0 Status");
1835 R (ACPI_RESET_BLK, 1, acpiResetWrite, NULL, "ACPI Reset");
1836#undef L
1837#undef R
1838
1839 /* register GC stuff */
1840 if (fGCEnabled)
1841 {
1842 rc = PDMDevHlpIOPortRegisterGC (pDevIns, PM_TMR_BLK, 1, 0, NULL, "acpiPMTmrRead",
1843 NULL, NULL, "ACPI PM Timer");
1844 AssertRCReturn(rc, rc);
1845 }
1846
1847 /* register R0 stuff */
1848 if (fR0Enabled)
1849 {
1850 rc = PDMDevHlpIOPortRegisterR0 (pDevIns, PM_TMR_BLK, 1, 0, NULL, "acpiPMTmrRead",
1851 NULL, NULL, "ACPI PM Timer");
1852 AssertRCReturn(rc, rc);
1853 }
1854
1855 rc = PDMDevHlpTMTimerCreate (pDevIns, TMCLOCK_VIRTUAL_SYNC, acpiTimer, "ACPI Timer", &s->tsR3);
1856 if (RT_FAILURE(rc))
1857 {
1858 AssertMsgFailed(("pfnTMTimerCreate -> %Rrc\n", rc));
1859 return rc;
1860 }
1861
1862 s->tsR0 = TMTimerR0Ptr (s->tsR3);
1863 s->tsRC = TMTimerRCPtr (s->tsR3);
1864 s->pm_timer_initial = TMTimerGet (s->tsR3);
1865 acpiPMTimerReset (s);
1866
1867 dev = &s->dev;
1868 dev->config[0x00] = 0x86;
1869 dev->config[0x01] = 0x80;
1870
1871 dev->config[0x02] = 0x13;
1872 dev->config[0x03] = 0x71;
1873
1874 dev->config[0x04] = 0x01;
1875 dev->config[0x05] = 0x00;
1876
1877 dev->config[0x06] = 0x80;
1878 dev->config[0x07] = 0x02;
1879 dev->config[0x08] = 0x08;
1880 dev->config[0x09] = 0x00;
1881
1882 dev->config[0x0a] = 0x80;
1883 dev->config[0x0b] = 0x06;
1884
1885 dev->config[0x0e] = 0x80;
1886 dev->config[0x0f] = 0x00;
1887
1888#if 0 /* The ACPI controller usually has no subsystem ID. */
1889 dev->config[0x2c] = 0x86;
1890 dev->config[0x2d] = 0x80;
1891 dev->config[0x2e] = 0x00;
1892 dev->config[0x2f] = 0x00;
1893#endif
1894 dev->config[0x3c] = SCI_INT;
1895
1896 rc = PDMDevHlpPCIRegister (pDevIns, dev);
1897 if (RT_FAILURE (rc))
1898 return rc;
1899
1900 rc = PDMDevHlpSSMRegister (pDevIns, pDevIns->pDevReg->szDeviceName, iInstance, 4, sizeof(*s),
1901 NULL, acpi_save_state, NULL, NULL, acpi_load_state, NULL);
1902 if (RT_FAILURE(rc))
1903 return rc;
1904
1905 /*
1906 * Interfaces
1907 */
1908 /* IBase */
1909 s->IBase.pfnQueryInterface = acpiQueryInterface;
1910 /* IACPIPort */
1911 s->IACPIPort.pfnSleepButtonPress = acpiSleepButtonPress;
1912 s->IACPIPort.pfnPowerButtonPress = acpiPowerButtonPress;
1913 s->IACPIPort.pfnGetPowerButtonHandled = acpiGetPowerButtonHandled;
1914 s->IACPIPort.pfnGetGuestEnteredACPIMode = acpiGetGuestEnteredACPIMode;
1915
1916 /*
1917 * Get the corresponding connector interface
1918 */
1919 rc = PDMDevHlpDriverAttach (pDevIns, 0, &s->IBase, &s->pDrvBase, "ACPI Driver Port");
1920 if (RT_SUCCESS (rc))
1921 {
1922 s->pDrv = (PPDMIACPICONNECTOR)s->pDrvBase->pfnQueryInterface (s->pDrvBase,
1923 PDMINTERFACE_ACPI_CONNECTOR);
1924 if (!s->pDrv)
1925 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_MISSING_INTERFACE,
1926 N_("LUN #0 doesn't have an ACPI connector interface"));
1927 }
1928 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
1929 {
1930 Log (("acpi: %s/%d: warning: no driver attached to LUN #0!\n",
1931 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
1932 rc = VINF_SUCCESS;
1933 }
1934 else
1935 return PDMDEV_SET_ERROR(pDevIns, rc,
1936 N_("Failed to attach LUN #0"));
1937
1938 return rc;
1939}
1940
1941/**
1942 * Relocates the GC pointer members.
1943 */
1944static DECLCALLBACK(void) acpiRelocate (PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
1945{
1946 ACPIState *s = PDMINS_2_DATA (pDevIns, ACPIState *);
1947 s->tsRC = TMTimerRCPtr (s->CTX_SUFF(ts));
1948}
1949
1950static DECLCALLBACK(void) acpiReset (PPDMDEVINS pDevIns)
1951{
1952 ACPIState *s = PDMINS_2_DATA (pDevIns, ACPIState *);
1953
1954 s->pm1a_en = 0;
1955 s->pm1a_sts = 0;
1956 s->pm1a_ctl = 0;
1957 s->pm_timer_initial = TMTimerGet (s->CTX_SUFF(ts));
1958 acpiPMTimerReset(s);
1959 s->uBatteryIndex = 0;
1960 s->uSystemInfoIndex = 0;
1961 s->gpe0_en = 0;
1962 s->gpe0_sts = 0;
1963 s->uSleepState = 0;
1964
1965 acpiPlantTables(s);
1966}
1967
1968/**
1969 * The device registration structure.
1970 */
1971const PDMDEVREG g_DeviceACPI =
1972{
1973 /* u32Version */
1974 PDM_DEVREG_VERSION,
1975 /* szDeviceName */
1976 "acpi",
1977 /* szRCMod */
1978 "VBoxDDGC.gc",
1979 /* szR0Mod */
1980 "VBoxDDR0.r0",
1981 /* pszDescription */
1982 "Advanced Configuration and Power Interface",
1983 /* fFlags */
1984 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
1985 /* fClass */
1986 PDM_DEVREG_CLASS_ACPI,
1987 /* cMaxInstances */
1988 ~0,
1989 /* cbInstance */
1990 sizeof(ACPIState),
1991 /* pfnConstruct */
1992 acpiConstruct,
1993 /* pfnDestruct */
1994 NULL,
1995 /* pfnRelocate */
1996 acpiRelocate,
1997 /* pfnIOCtl */
1998 NULL,
1999 /* pfnPowerOn */
2000 NULL,
2001 /* pfnReset */
2002 acpiReset,
2003 /* pfnSuspend */
2004 NULL,
2005 /* pfnResume */
2006 NULL,
2007 /* pfnAttach */
2008 NULL,
2009 /* pfnDetach */
2010 NULL,
2011 /* pfnQueryInterface. */
2012 NULL,
2013 /* pfnInitComplete */
2014 NULL,
2015 /* pfnPowerOff */
2016 NULL,
2017 /* pfnSoftReset */
2018 NULL,
2019 /* u32VersionEnd */
2020 PDM_DEVREG_VERSION
2021};
2022
2023#endif /* IN_RING3 */
2024#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
2025
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