PIIX4 SMBus host, EEPROM device emulation
PATCH 1/1 PIIX4 SMBus host, EEPROM device emulation
The attached patch adds SMBus host support to the emulated PIIX4 power management device (acpi.c), and adds an emulated serial EEPROM device accessible via the SMBus interface.
I tried to follow the Intel 82371AB spec for the SMBus support; the interface should be generic enough to implement a variety of SMBus-compliant devices.
The EEPROM device can use a file for persistent storage (smbus_eeprom.bin in the BIOS directory). If this file does not exist, a temporary buffer is used instead.
I tested the devices with Linux using the i2c-piix4 host driver and the eeprom chip driver. I have no idea what will happen on other OSes.
Comments and suggestions welcome.
--Ed
hw/acpi.c +173 -3
@@ -27,6 +27,7 @@#define PM_IO_BASE 0xb000#define SMI_CMD_IO_ADDR 0xb040#define ACPI_DBG_IO_ADDR 0xb044 +#define SMB_IO_BASE 0xb100 typedef struct PIIX4PMState { PCIDevice dev;
@@ -35,6 +36,15 @@ typedef struct PIIX4PMState {uint16_t pmcntrl;QEMUTimer *tmr_timer;int64_t tmr_overflow_time; +SMBusDevice *smb_dev[128 ]; +uint8_t smb_stat; +uint8_t smb_ctl; +uint8_t smb_cmd; +uint8_t smb_addr; +uint8_t smb_data0; +uint8_t smb_data1; +uint8_t smb_data[32 ]; +uint8_t smb_index; } PIIX4PMState;#define RTC_EN (1 << 10)
@@ -46,8 +56,6 @@ typedef struct PIIX4PMState {#define SUS_EN (1 << 13) -/* Note: only used for ACPI bios init. Could be deleted when ACPI init -is integrated in Bochs BIOS */ static PIIX4PMState *piix4_pm_state;static uint32_t get_pmtmr (PIIX4PMState *s)
@@ -218,13 +226,163 @@ static void acpi_dbg_writel(void *opaque#endif } +static void smb_transaction (PIIX4PMState *s) +{ +uint8_t prot = (s->smb_ctl >>2 ) &0x07 ; +uint8_t read = s->smb_addr &0x01 ; +uint8_t cmd = s->smb_cmd; +uint8_t addr = s->smb_addr >>1 ; +SMBusDevice *dev = s->smb_dev[addr]; + +#ifdef DEBUG +printf ("SMBus trans addr=0x%02x prot=0x%02x\n" , addr, prot); +#endif +if (!dev) goto error; + +switch (prot) { +case 0x0 : +if (!dev->quick_cmd) goto error; + (*dev->quick_cmd)(dev, read); +break ; +case 0x1 : +if (read) { +if (!dev->receive_byte) goto error; + s->smb_data0 = (*dev->receive_byte)(dev); + } +else { +if (!dev->send_byte) goto error; + (*dev->send_byte)(dev, cmd); + } +break ; +case 0x2 : +if (read) { +if (!dev->read_byte) goto error; + s->smb_data0 = (*dev->read_byte)(dev, cmd); + } +else { +if (!dev->write_byte) goto error; + (*dev->write_byte)(dev, cmd, s->smb_data0); + } +break ; +case 0x3 : +if (read) { +uint16_t val; +if (!dev->read_word) goto error; + val = (*dev->read_word)(dev, cmd); + s->smb_data0 = val; + s->smb_data1 = val >>8 ; + } +else { +if (!dev->write_word) goto error; + (*dev->write_word)(dev, cmd, (s->smb_data1 <<8 ) | s->smb_data0); + } +break ; +case 0x5 : +if (read) { +if (!dev->read_block) goto error; + s->smb_data0 = (*dev->read_block)(dev, cmd, s->smb_data); + } +else { +if (!dev->write_block) goto error; + (*dev->write_block)(dev, cmd, s->smb_data0, s->smb_data); + } +break ; +default : + goto error; + } +return ; + + error: + s->smb_stat |=0x04 ; +} + +static void smb_ioport_writeb (void *opaque,uint32_t addr,uint32_t val) +{ +PIIX4PMState *s = opaque; + addr &=0x3f ; +#ifdef DEBUG +printf ("SMB writeb port=0x%04x val=0x%02x\n" , addr, val); +#endif +switch (addr) { +case 0x00 :/* SMBHSTSTS */ + s->smb_stat =0 ; + s->smb_index =0 ; +break ; +case 0x02 :/* SMBHSTCNT */ + s->smb_ctl = val; +if (val &0x40 ) +smb_transaction (s); +break ; +case 0x03 :/* SMBHSTCMD */ + s->smb_cmd = val; +break ; +case 0x04 :/* SMBHSTADD */ + s->smb_addr = val; +break ; +case 0x05 :/* SMBHSTDAT0 */ + s->smb_data0 = val; +break ; +case 0x06 :/* SMBHSTDAT1 */ + s->smb_data1 = val; +break ; +case 0x07 :/* SMBBLKDAT */ + s->smb_data[s->smb_index++] = val; +if (s->smb_index >31 ) + s->smb_index =0 ; +break ; +default : +break ; + } +} + +static uint32_t smb_ioport_readb (void *opaque,uint32_t addr) +{ +PIIX4PMState *s = opaque; +uint32_t val; + + addr &=0x3f ; +switch (addr) { +case 0x00 :/* SMBHSTSTS */ + val = s->smb_stat; +break ; +case 0x02 :/* SMBHSTCNT */ + s->smb_index =0 ; + val = s->smb_ctl &0x1f ; +break ; +case 0x03 :/* SMBHSTCMD */ + val = s->smb_cmd; +break ; +case 0x04 :/* SMBHSTADD */ + val = s->smb_addr; +break ; +case 0x05 :/* SMBHSTDAT0 */ + val = s->smb_data0; +break ; +case 0x06 :/* SMBHSTDAT1 */ + val = s->smb_data1; +break ; +case 0x07 :/* SMBBLKDAT */ + val = s->smb_data[s->smb_index++]; +if (s->smb_index >31 ) + s->smb_index =0 ; +break ; +default : + val =0 ; +break ; + } +#ifdef DEBUG +printf ("SMB readb port=0x%04x val=0x%02x\n" , addr, val); +#endif +return val; +} +/* XXX: we still add it to the PIIX3 and we count on the fact that OSes are smart enough to accept this strange configuration */ void piix4_pm_init (PCIBus *bus,int devfn) {PIIX4PMState *s;uint8_t *pci_conf; -uint32_t pm_io_base; +uint32_t pm_io_base, smb_io_base; s = (PIIX4PMState *)pci_register_device(bus, "PM", sizeof(PIIX4PMState),
@@ -259,10 +417,22 @@ void piix4_pm_init(PCIBus *bus, int devf pci_conf[0x67 ] = (serial_hds[0 ] !=NULL ?0x08 :0 ) | (serial_hds[1 ] !=NULL ?0x90 :0 ); + smb_io_base =SMB_IO_BASE ; + pci_conf[0x90 ] = smb_io_base |1 ; + pci_conf[0x91 ] = smb_io_base >>8 ; + pci_conf[0xd2 ] =0x09 ; +register_ioport_write (smb_io_base,64 ,1 , smb_ioport_writeb, s); +register_ioport_read (smb_io_base,64 ,1 , smb_ioport_readb, s); + s->tmr_timer =qemu_new_timer (vm_clock, pm_tmr_timer, s); piix4_pm_state = s; } +void piix4_smbus_register_device (SMBusDevice *dev,uint8_t addr) +{ + piix4_pm_state->smb_dev[addr] = dev; +} +/* ACPI tables */ /* XXX: move them in the Bochs BIOS ? */
hw/smbus.h new file +38 -0
@@ -0,0 +1,38 @@ +/* +* QEMU SMBus API +* +* Copyright (c) 2007 Arastra, Inc. +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +typedef struct SMBusDevice SMBusDevice ; + +struct SMBusDevice { +uint8_t addr; +void (*quick_cmd)(SMBusDevice *dev,uint8_t read); +void (*send_byte)(SMBusDevice *dev,uint8_t val); +uint8_t (*receive_byte)(SMBusDevice *dev); +void (*write_byte)(SMBusDevice *dev,uint8_t cmd,uint8_t val); +uint8_t (*read_byte)(SMBusDevice *dev,uint8_t cmd); +void (*write_word)(SMBusDevice *dev,uint8_t cmd,uint16_t val); +uint16_t (*read_word)(SMBusDevice *dev,uint8_t cmd); +void (*write_block)(SMBusDevice *dev,uint8_t cmd,uint8_t len,uint8_t *buf); +uint8_t (*read_block)(SMBusDevice *dev,uint8_t cmd,uint8_t *buf); +};
hw/smbus_eeprom.c new file +94 -0
94 lines not on this page · raw message
vl.h +6 -0
@@ -917,11 +917,17 @@ int pit_get_out(PITState *pit, int channvoid pcspk_init (PITState *);int pcspk_audio_init (AudioState *); +#include "hw/smbus.h" +/* acpi.c */ extern int acpi_enabled;void piix4_pm_init (PCIBus *bus,int devfn); +void piix4_smbus_register_device (SMBusDevice *dev,uint8_t addr);void acpi_bios_init (void ); +/* smbus_eeprom.c */ +SMBusDevice *smbus_eeprom_device_init (uint8_t addr,uint8_t *buf); +/* pc.c */ extern QEMUMachine pc_machine;extern QEMUMachine isapc_machine;
hw/pc.c +17 -0
@@ -24,6 +24,7 @@#include "vl.h" #include <dlfcn.h> /* For dlopen of hw plugin shared libraries */ +#include <sys/mman.h> /* output Bochs bios info messages */ //#define DEBUG_BIOS
@@ -32,6 +33,7 @@#define VGABIOS_FILENAME "vgabios.bin"#define VGABIOS_CIRRUS_FILENAME "vgabios-cirrus.bin"#define LINUX_BOOT_FILENAME "linux_boot.bin" +#define SMBUS_EEPROM_FILENAME "smbus_eeprom.bin"#define KERNEL_LOAD_ADDR 0x00100000#define INITRD_LOAD_ADDR 0x00600000
@@ -863,7 +865,22 @@ static void pc_init1(int ram_size, int v }if (pci_enabled && acpi_enabled) { +int f; +uint8_t *d =MAP_FAILED ;piix4_pm_init (pci_bus, piix3_devfn +3 ); +snprintf (buf,sizeof (buf),"%s/%s" , bios_dir,SMBUS_EEPROM_FILENAME ); + f =open (buf,O_RDWR ); +if (f >=0 ) { +ftruncate (f,8 *256 ); + d =mmap (0 ,8 *256 ,PROT_READ |PROT_WRITE ,MAP_SHARED , f,0 ); + } +if (d ==MAP_FAILED ) + d =qemu_mallocz (8 *256 ); +for (i =0 ; i <8 ; i++) { +SMBusDevice *eeprom =smbus_eeprom_device_init (0x50 + i, + d + (i *256 )); +piix4_smbus_register_device (eeprom,0x50 + i); + } }#if 0
Makefile.target +1 -1
@@ -338,7 +338,7 @@ ifeq ($(TARGET_BASE_ARCH), i386) VL_OBJS+= ide.o pckbd.o ps2.o vga.o $(SOUND_HW) dma.o $(AUDIODRV) VL_OBJS+= fdc.o mc146818rtc.o serial.o i8259.o i8254.o pcspk.o pc.o VL_OBJS+= cirrus_vga.o mixeng.o apic.o parallel.o acpi.o piix_pci.o -VL_OBJS+= usb-uhci.o +VL_OBJS+= usb-uhci.o smbus_eeprom.o DEFINES += -DHAS_AUDIO endif ifeq ($(TARGET_BASE_ARCH), ppc)
Quoted 12 lines · Ed Swierk
The attached patch adds SMBus host support to the emulated PIIX4 power management device (
acpi.c), and adds an emulated serial EEPROM device accessible via the SMBus interface.I tried to follow the Intel 82371AB spec for the SMBus support; the interface should be generic enough to implement a variety of SMBus-compliant devices.
The EEPROM device can use a file for persistent storage (smbus_eeprom.bin in the BIOS directory). If this file does not exist, a temporary buffer is used instead.
Quoted 5 lines
I tested the devices with Linux using the i2c-piix4 host driver and the eeprom chip driver. I have no idea what will happen on other OSes.
Comments and suggestions welcome.
[...]
OK, but avoid using mmap() in the device code. Moreover, files in the BIOS directory are not writable.
Regards,
Fabrice.
Quoted 2 lines · Fabrice Bellard
OK, but avoid using
mmap()in the device code. Moreover, files in the BIOS directory are not writable.
OK. Would it be better to do the following:
- add a command-line option -seeprom that sets the file to use as backing store for the smbus_eeprom device
- in main(), open and mmap the file if the -seeprom option is specified, otherwise allocate a temporary buffer
- add a seeprom buffer argument to each machine's init(), ignored except for pc
--Ed
Here's a revised patch with the mmap stuff removed. I'll refine the persistence support, but in the meantime the EEPROM device is usable even if it forgets its contents when qemu exits.
--Ed
hw/acpi.c +173 -3
@@ -27,6 +27,7 @@#define PM_IO_BASE 0xb000#define SMI_CMD_IO_ADDR 0xb040#define ACPI_DBG_IO_ADDR 0xb044 +#define SMB_IO_BASE 0xb100 typedef struct PIIX4PMState { PCIDevice dev;
@@ -35,6 +36,15 @@ typedef struct PIIX4PMState {uint16_t pmcntrl;QEMUTimer *tmr_timer;int64_t tmr_overflow_time; +SMBusDevice *smb_dev[128 ]; +uint8_t smb_stat; +uint8_t smb_ctl; +uint8_t smb_cmd; +uint8_t smb_addr; +uint8_t smb_data0; +uint8_t smb_data1; +uint8_t smb_data[32 ]; +uint8_t smb_index; } PIIX4PMState;#define RTC_EN (1 << 10)
@@ -46,8 +56,6 @@ typedef struct PIIX4PMState {#define SUS_EN (1 << 13) -/* Note: only used for ACPI bios init. Could be deleted when ACPI init -is integrated in Bochs BIOS */ static PIIX4PMState *piix4_pm_state;static uint32_t get_pmtmr (PIIX4PMState *s)
@@ -218,13 +226,163 @@ static void acpi_dbg_writel(void *opaque#endif } +static void smb_transaction (PIIX4PMState *s) +{ +uint8_t prot = (s->smb_ctl >>2 ) &0x07 ; +uint8_t read = s->smb_addr &0x01 ; +uint8_t cmd = s->smb_cmd; +uint8_t addr = s->smb_addr >>1 ; +SMBusDevice *dev = s->smb_dev[addr]; + +#ifdef DEBUG +printf ("SMBus trans addr=0x%02x prot=0x%02x\n" , addr, prot); +#endif +if (!dev) goto error; + +switch (prot) { +case 0x0 : +if (!dev->quick_cmd) goto error; + (*dev->quick_cmd)(dev, read); +break ; +case 0x1 : +if (read) { +if (!dev->receive_byte) goto error; + s->smb_data0 = (*dev->receive_byte)(dev); + } +else { +if (!dev->send_byte) goto error; + (*dev->send_byte)(dev, cmd); + } +break ; +case 0x2 : +if (read) { +if (!dev->read_byte) goto error; + s->smb_data0 = (*dev->read_byte)(dev, cmd); + } +else { +if (!dev->write_byte) goto error; + (*dev->write_byte)(dev, cmd, s->smb_data0); + } +break ; +case 0x3 : +if (read) { +uint16_t val; +if (!dev->read_word) goto error; + val = (*dev->read_word)(dev, cmd); + s->smb_data0 = val; + s->smb_data1 = val >>8 ; + } +else { +if (!dev->write_word) goto error; + (*dev->write_word)(dev, cmd, (s->smb_data1 <<8 ) | s->smb_data0); + } +break ; +case 0x5 : +if (read) { +if (!dev->read_block) goto error; + s->smb_data0 = (*dev->read_block)(dev, cmd, s->smb_data); + } +else { +if (!dev->write_block) goto error; + (*dev->write_block)(dev, cmd, s->smb_data0, s->smb_data); + } +break ; +default : + goto error; + } +return ; + + error: + s->smb_stat |=0x04 ; +} + +static void smb_ioport_writeb (void *opaque,uint32_t addr,uint32_t val) +{ +PIIX4PMState *s = opaque; + addr &=0x3f ; +#ifdef DEBUG +printf ("SMB writeb port=0x%04x val=0x%02x\n" , addr, val); +#endif +switch (addr) { +case 0x00 :/* SMBHSTSTS */ + s->smb_stat =0 ; + s->smb_index =0 ; +break ; +case 0x02 :/* SMBHSTCNT */ + s->smb_ctl = val; +if (val &0x40 ) +smb_transaction (s); +break ; +case 0x03 :/* SMBHSTCMD */ + s->smb_cmd = val; +break ; +case 0x04 :/* SMBHSTADD */ + s->smb_addr = val; +break ; +case 0x05 :/* SMBHSTDAT0 */ + s->smb_data0 = val; +break ; +case 0x06 :/* SMBHSTDAT1 */ + s->smb_data1 = val; +break ; +case 0x07 :/* SMBBLKDAT */ + s->smb_data[s->smb_index++] = val; +if (s->smb_index >31 ) + s->smb_index =0 ; +break ; +default : +break ; + } +} + +static uint32_t smb_ioport_readb (void *opaque,uint32_t addr) +{ +PIIX4PMState *s = opaque; +uint32_t val; + + addr &=0x3f ; +switch (addr) { +case 0x00 :/* SMBHSTSTS */ + val = s->smb_stat; +break ; +case 0x02 :/* SMBHSTCNT */ + s->smb_index =0 ; + val = s->smb_ctl &0x1f ; +break ; +case 0x03 :/* SMBHSTCMD */ + val = s->smb_cmd; +break ; +case 0x04 :/* SMBHSTADD */ + val = s->smb_addr; +break ; +case 0x05 :/* SMBHSTDAT0 */ + val = s->smb_data0; +break ; +case 0x06 :/* SMBHSTDAT1 */ + val = s->smb_data1; +break ; +case 0x07 :/* SMBBLKDAT */ + val = s->smb_data[s->smb_index++]; +if (s->smb_index >31 ) + s->smb_index =0 ; +break ; +default : + val =0 ; +break ; + } +#ifdef DEBUG +printf ("SMB readb port=0x%04x val=0x%02x\n" , addr, val); +#endif +return val; +} +/* XXX: we still add it to the PIIX3 and we count on the fact that OSes are smart enough to accept this strange configuration */ void piix4_pm_init (PCIBus *bus,int devfn) {PIIX4PMState *s;uint8_t *pci_conf; -uint32_t pm_io_base; +uint32_t pm_io_base, smb_io_base; s = (PIIX4PMState *)pci_register_device(bus, "PM", sizeof(PIIX4PMState),
@@ -259,10 +417,22 @@ void piix4_pm_init(PCIBus *bus, int devf pci_conf[0x67 ] = (serial_hds[0 ] !=NULL ?0x08 :0 ) | (serial_hds[1 ] !=NULL ?0x90 :0 ); + smb_io_base =SMB_IO_BASE ; + pci_conf[0x90 ] = smb_io_base |1 ; + pci_conf[0x91 ] = smb_io_base >>8 ; + pci_conf[0xd2 ] =0x09 ; +register_ioport_write (smb_io_base,64 ,1 , smb_ioport_writeb, s); +register_ioport_read (smb_io_base,64 ,1 , smb_ioport_readb, s); + s->tmr_timer =qemu_new_timer (vm_clock, pm_tmr_timer, s); piix4_pm_state = s; } +void piix4_smbus_register_device (SMBusDevice *dev,uint8_t addr) +{ + piix4_pm_state->smb_dev[addr] = dev; +} +/* ACPI tables */ /* XXX: move them in the Bochs BIOS ? */
hw/smbus.h new file +38 -0
@@ -0,0 +1,38 @@ +/* +* QEMU SMBus API +* +* Copyright (c) 2007 Arastra, Inc. +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +typedef struct SMBusDevice SMBusDevice ; + +struct SMBusDevice { +uint8_t addr; +void (*quick_cmd)(SMBusDevice *dev,uint8_t read); +void (*send_byte)(SMBusDevice *dev,uint8_t val); +uint8_t (*receive_byte)(SMBusDevice *dev); +void (*write_byte)(SMBusDevice *dev,uint8_t cmd,uint8_t val); +uint8_t (*read_byte)(SMBusDevice *dev,uint8_t cmd); +void (*write_word)(SMBusDevice *dev,uint8_t cmd,uint16_t val); +uint16_t (*read_word)(SMBusDevice *dev,uint8_t cmd); +void (*write_block)(SMBusDevice *dev,uint8_t cmd,uint8_t len,uint8_t *buf); +uint8_t (*read_block)(SMBusDevice *dev,uint8_t cmd,uint8_t *buf); +};
hw/smbus_eeprom.c new file +94 -0
94 lines not on this page · raw message
vl.h +6 -0
@@ -917,11 +917,17 @@ int pit_get_out(PITState *pit, int channvoid pcspk_init (PITState *);int pcspk_audio_init (AudioState *); +#include "hw/smbus.h" +/* acpi.c */ extern int acpi_enabled;void piix4_pm_init (PCIBus *bus,int devfn); +void piix4_smbus_register_device (SMBusDevice *dev,uint8_t addr);void acpi_bios_init (void ); +/* smbus_eeprom.c */ +SMBusDevice *smbus_eeprom_device_init (uint8_t addr,uint8_t *buf); +/* pc.c */ extern QEMUMachine pc_machine;extern QEMUMachine isapc_machine;
hw/pc.c +6 -0
@@ -863,7 +863,13 @@ static void pc_init1(int ram_size, int v }if (pci_enabled && acpi_enabled) { +uint8_t *eeprom_buf =qemu_mallocz (8 *256 );/* XXX: make this persistent */ piix4_pm_init (pci_bus, piix3_devfn +3 ); +for (i =0 ; i <8 ; i++) { +SMBusDevice *eeprom =smbus_eeprom_device_init (0x50 + i, + eeprom_buf + (i *256 )); +piix4_smbus_register_device (eeprom,0x50 + i); + } }#if 0
Makefile.target +1 -1
@@ -338,7 +338,7 @@ ifeq ($(TARGET_BASE_ARCH), i386) VL_OBJS+= ide.o pckbd.o ps2.o vga.o $(SOUND_HW) dma.o $(AUDIODRV) VL_OBJS+= fdc.o mc146818rtc.o serial.o i8259.o i8254.o pcspk.o pc.o VL_OBJS+= cirrus_vga.o mixeng.o apic.o parallel.o acpi.o piix_pci.o -VL_OBJS+= usb-uhci.o +VL_OBJS+= usb-uhci.o smbus_eeprom.o DEFINES += -DHAS_AUDIO endif ifeq ($(TARGET_BASE_ARCH), ppc)
Windows 2000 boots and PC Wizard 2007 displays the following information under the Mainboard category:
Quoted 3 lines
SMBus/i2c Bus : Yes
Quoted 1 line
General Information
Device : 82371AB/EB/MB PIIX4/E/M Power Management Controller Revision : 0
Frequency : 16 KHz
Address : 0xB100
Quoted 1 line
Quoted 1 line
Device Capabilities (PCI)
I/O Access : Yes
Memory Access : Yes
Bus Master Capable : Yes
Special Cycle Recognition : No
Memory Write & Invalidate : No
VGA Palette Snoop : No
Parity Error Response : No
Cycle Wait : No
System Error Line : No
Fast Back-to-Back : No
Detects Parity Errors : No
User Defined Format : No
PCI 66Mhz Bus Support : No
New Capability List : No
which hopefully means the patch doesn't break anything.
--Ed
Quoted 3 lines · Ed Swierk
Here's a revised patch with the mmap stuff removed. I'll refine the persistence support, but in the meantime the EEPROM device is usable even if it forgets its contents when qemu exits.
The patch is OK now.
Fabrice.
Quoted 3 lines · Ed Swierk
Here's a revised patch with the mmap stuff removed. I'll refine the persistence support, but in the meantime the EEPROM device is usable even if it forgets its contents when qemu exits.
It doesn't apply for current CVS. Could you please update the patch? I would also prefer to have enums instead of raw constants in all those case statements.
Thiemo
Quoted 3 lines · Thiemo Seufer
It doesn't apply for current CVS. Could you please update the patch? I would also prefer to have enums instead of raw constants in all those case statements.
Here you go.
--Ed
hw/acpi.c +185 -0
@@ -24,6 +24,7 @@#define PM_FREQ 3579545#define ACPI_DBG_IO_ADDR 0xb044 +#define SMB_IO_BASE 0xb100 typedef struct PIIX4PMState { PCIDevice dev;
@@ -34,6 +35,15 @@uint8_t apms;QEMUTimer *tmr_timer;int64_t tmr_overflow_time; +SMBusDevice *smb_dev[128 ]; +uint8_t smb_stat; +uint8_t smb_ctl; +uint8_t smb_cmd; +uint8_t smb_addr; +uint8_t smb_data0; +uint8_t smb_data1; +uint8_t smb_data[32 ]; +uint8_t smb_index; } PIIX4PMState;#define RTC_EN (1 << 10)
@@ -45,6 +55,17 @@#define SUS_EN (1 << 13) +#define SMBHSTSTS 0x00 +#define SMBHSTCNT 0x02 +#define SMBHSTCMD 0x03 +#define SMBHSTADD 0x04 +#define SMBHSTDAT0 0x05 +#define SMBHSTDAT1 0x06 +#define SMBBLKDAT 0x07 + +/* Note: only used for piix4_smbus_register_device */ +static PIIX4PMState *piix4_pm_state; +static uint32_t get_pmtmr (PIIX4PMState *s) {uint32_t d;
@@ -231,6 +252,156 @@#endif } +static void smb_transaction (PIIX4PMState *s) +{ +uint8_t prot = (s->smb_ctl >>2 ) &0x07 ; +uint8_t read = s->smb_addr &0x01 ; +uint8_t cmd = s->smb_cmd; +uint8_t addr = s->smb_addr >>1 ; +SMBusDevice *dev = s->smb_dev[addr]; + +#ifdef DEBUG +printf ("SMBus trans addr=0x%02x prot=0x%02x\n" , addr, prot); +#endif +if (!dev) goto error; + +switch (prot) { +case 0x0 : +if (!dev->quick_cmd) goto error; + (*dev->quick_cmd)(dev, read); +break ; +case 0x1 : +if (read) { +if (!dev->receive_byte) goto error; + s->smb_data0 = (*dev->receive_byte)(dev); + } +else { +if (!dev->send_byte) goto error; + (*dev->send_byte)(dev, cmd); + } +break ; +case 0x2 : +if (read) { +if (!dev->read_byte) goto error; + s->smb_data0 = (*dev->read_byte)(dev, cmd); + } +else { +if (!dev->write_byte) goto error; + (*dev->write_byte)(dev, cmd, s->smb_data0); + } +break ; +case 0x3 : +if (read) { +uint16_t val; +if (!dev->read_word) goto error; + val = (*dev->read_word)(dev, cmd); + s->smb_data0 = val; + s->smb_data1 = val >>8 ; + } +else { +if (!dev->write_word) goto error; + (*dev->write_word)(dev, cmd, (s->smb_data1 <<8 ) | s->smb_data0); + } +break ; +case 0x5 : +if (read) { +if (!dev->read_block) goto error; + s->smb_data0 = (*dev->read_block)(dev, cmd, s->smb_data); + } +else { +if (!dev->write_block) goto error; + (*dev->write_block)(dev, cmd, s->smb_data0, s->smb_data); + } +break ; +default : + goto error; + } +return ; + + error: + s->smb_stat |=0x04 ; +} + +static void smb_ioport_writeb (void *opaque,uint32_t addr,uint32_t val) +{ +PIIX4PMState *s = opaque; + addr &=0x3f ; +#ifdef DEBUG +printf ("SMB writeb port=0x%04x val=0x%02x\n" , addr, val); +#endif +switch (addr) { +case SMBHSTSTS : + s->smb_stat =0 ; + s->smb_index =0 ; +break ; +case SMBHSTCNT : + s->smb_ctl = val; +if (val &0x40 ) +smb_transaction (s); +break ; +case SMBHSTCMD : + s->smb_cmd = val; +break ; +case SMBHSTADD : + s->smb_addr = val; +break ; +case SMBHSTDAT0 : + s->smb_data0 = val; +break ; +case SMBHSTDAT1 : + s->smb_data1 = val; +break ; +case SMBBLKDAT : + s->smb_data[s->smb_index++] = val; +if (s->smb_index >31 ) + s->smb_index =0 ; +break ; +default : +break ; + } +} + +static uint32_t smb_ioport_readb (void *opaque,uint32_t addr) +{ +PIIX4PMState *s = opaque; +uint32_t val; + + addr &=0x3f ; +switch (addr) { +case SMBHSTSTS : + val = s->smb_stat; +break ; +case SMBHSTCNT : + s->smb_index =0 ; + val = s->smb_ctl &0x1f ; +break ; +case SMBHSTCMD : + val = s->smb_cmd; +break ; +case SMBHSTADD : + val = s->smb_addr; +break ; +case SMBHSTDAT0 : + val = s->smb_data0; +break ; +case SMBHSTDAT1 : + val = s->smb_data1; +break ; +case SMBBLKDAT : + val = s->smb_data[s->smb_index++]; +if (s->smb_index >31 ) + s->smb_index =0 ; +break ; +default : + val =0 ; +break ; + } +#ifdef DEBUG +printf ("SMB readb port=0x%04x val=0x%02x\n" , addr, val); +#endif +return val; +} +static void pm_io_space_update (PIIX4PMState *s) {uint32_t pm_io_base;
@@ -302,6 +473,7 @@ {PIIX4PMState *s;uint8_t *pci_conf; +uint32_t pm_io_base, smb_io_base; s = (PIIX4PMState *)pci_register_device(bus, "PM", sizeof(PIIX4PMState),
@@ -332,7 +504,20 @@ pci_conf[0x67 ] = (serial_hds[0 ] !=NULL ?0x08 :0 ) | (serial_hds[1 ] !=NULL ?0x90 :0 ); + smb_io_base =SMB_IO_BASE ; + pci_conf[0x90 ] = smb_io_base |1 ; + pci_conf[0x91 ] = smb_io_base >>8 ; + pci_conf[0xd2 ] =0x09 ; +register_ioport_write (smb_io_base,64 ,1 , smb_ioport_writeb, s); +register_ioport_read (smb_io_base,64 ,1 , smb_ioport_readb, s); + s->tmr_timer =qemu_new_timer (vm_clock, pm_tmr_timer, s);register_savevm ("piix4_pm" ,0 ,1 , pm_save, pm_load, s); + piix4_pm_state = s; +} + +void piix4_smbus_register_device (SMBusDevice *dev,uint8_t addr) +{ + piix4_pm_state->smb_dev[addr] = dev; }
hw/pc.c +6 -0
@@ -699,7 +699,13 @@ }if (pci_enabled && acpi_enabled) { +uint8_t *eeprom_buf =qemu_mallocz (8 *256 );/* XXX: make this persistent */ piix4_pm_init (pci_bus, piix3_devfn +3 ); +for (i =0 ; i <8 ; i++) { +SMBusDevice *eeprom =smbus_eeprom_device_init (0x50 + i, + eeprom_buf + (i *256 )); +piix4_smbus_register_device (eeprom,0x50 + i); + } }if (i440fx_state) {
hw/smbus_eeprom.c +94 -0
94 lines not on this page · raw message
hw/smbus.h +38 -0
@@ -0,0 +1,38 @@ +/* +* QEMU SMBus API +* +* Copyright (c) 2007 Arastra, Inc. +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +typedef struct SMBusDevice SMBusDevice ; + +struct SMBusDevice { +uint8_t addr; +void (*quick_cmd)(SMBusDevice *dev,uint8_t read); +void (*send_byte)(SMBusDevice *dev,uint8_t val); +uint8_t (*receive_byte)(SMBusDevice *dev); +void (*write_byte)(SMBusDevice *dev,uint8_t cmd,uint8_t val); +uint8_t (*read_byte)(SMBusDevice *dev,uint8_t cmd); +void (*write_word)(SMBusDevice *dev,uint8_t cmd,uint16_t val); +uint16_t (*read_word)(SMBusDevice *dev,uint8_t cmd); +void (*write_block)(SMBusDevice *dev,uint8_t cmd,uint8_t len,uint8_t *buf); +uint8_t (*read_block)(SMBusDevice *dev,uint8_t cmd,uint8_t *buf); +};
Makefile.target +1 -1
@@ -365,7 +365,7 @@ VL_OBJS+= ide.o pckbd.o ps2.o vga.o $(SOUND_HW) dma.o $(AUDIODRV) VL_OBJS+= fdc.o mc146818rtc.o serial.o i8259.o i8254.o pcspk.o pc.o VL_OBJS+= cirrus_vga.o mixeng.o apic.o parallel.o acpi.o piix_pci.o -VL_OBJS+= usb-uhci.o +VL_OBJS+= usb-uhci.o smbus_eeprom.o CPPFLAGS += -DHAS_AUDIO endif ifeq ($(TARGET_BASE_ARCH), ppc)
vl.h +6 -0
@@ -1043,11 +1043,17 @@void pcspk_init (PITState *);int pcspk_audio_init (AudioState *); +#include "hw/smbus.h" +/* acpi.c */ extern int acpi_enabled;void piix4_pm_init (PCIBus *bus,int devfn); +void piix4_smbus_register_device (SMBusDevice *dev,uint8_t addr);void acpi_bios_init (void ); +/* smbus_eeprom.c */ +SMBusDevice *smbus_eeprom_device_init (uint8_t addr,uint8_t *buf); +/* pc.c */ extern QEMUMachine pc_machine;extern QEMUMachine isapc_machine;