Skip to content

Commit

Permalink
hal/ia32: Add HPET
Browse files Browse the repository at this point in the history
JIRA: RTOS-530
  • Loading branch information
astalke committed Nov 14, 2023
1 parent 2d138ba commit 2ef32db
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 84 deletions.
81 changes: 81 additions & 0 deletions hal/ia32/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ static int _hal_acpiInit(hal_config_t *config)
if (syspage->hs.fadt != 0) {
hal_config.fadt = _hal_configMapObjectBeforeStack(pdir, syspage->hs.fadt, syspage->hs.fadtLength, PGHD_WRITE);
}
if (syspage->hs.hpet != 0) {
hal_config.hpet = _hal_configMapObjectBeforeStack(pdir, syspage->hs.hpet, syspage->hs.hpetLength, PGHD_WRITE);
}

if (hal_config.madt != NULL) {
config->localApicAddr = _hal_configMapDevice(pdir, hal_config.madt->localApicAddr, SIZE_PAGE, PGHD_WRITE);
Expand Down Expand Up @@ -200,6 +203,83 @@ static inline void _hal_configMemoryInit(void)
}


void _hal_gasAllocDevice(const hal_gas_t *gas, hal_gasMapped_t *mgas, size_t size)
{
addr_t *pdir = (addr_t *)(VADDR_KERNEL + syspage->hs.pdir);
mgas->addressSpaceId = gas->addressSpaceId;
mgas->registerWidth = gas->registerWidth;
mgas->registerOffset = gas->registerOffset;
mgas->accessSize = gas->accessSize;

switch (gas->addressSpaceId) {
case GAS_ADDRESS_SPACE_ID_MEMORY:
mgas->address = _hal_configMapDevice(pdir, (addr_t)gas->address, size, PGHD_WRITE);
break;
default:
mgas->address = (void *)((u32)gas->address);
break;
}
}


int _hal_gasWrite32(hal_gasMapped_t *gas, u32 offset, u32 val)
{
int ret;
switch (gas->addressSpaceId) {
case GAS_ADDRESS_SPACE_ID_MEMORY:
*(volatile u32 *)(gas->address + offset) = val;
ret = 0;
break;
case GAS_ADDRESS_SPACE_ID_IOPORT:
hal_outl(gas->address + offset, val);
ret = 0;
break;
case GAS_ADDRESS_SPACE_ID_PCI:
/* TODO */
ret = 1;
break;
case GAS_ADDRESS_SPACE_ID_PCIBAR:
/* TODO */
ret = 1;
break;
default:
/* Unspecified */
ret = 1;
break;
}
return ret;
}


int _hal_gasRead32(hal_gasMapped_t *gas, u32 offset, u32 *val)
{
int ret;
switch (gas->addressSpaceId) {
case GAS_ADDRESS_SPACE_ID_MEMORY:
*val = *(volatile u32 *)(gas->address + offset);
ret = 0;
break;
case GAS_ADDRESS_SPACE_ID_IOPORT:
*val = hal_inl(gas->address + offset);
ret = 0;
break;
case GAS_ADDRESS_SPACE_ID_PCI:
/* TODO */
ret = 1;
break;
case GAS_ADDRESS_SPACE_ID_PCIBAR:
/* TODO */
ret = 1;
break;
default:
/* Unspecified */
ret = 1;
break;
}
return ret;
}


void _hal_configInit(syspage_t *s)
{
unsigned int ra, rb, rc, rd;
Expand All @@ -215,6 +295,7 @@ void _hal_configInit(syspage_t *s)
hal_config.ptable = NULL;
hal_config.madt = NULL;
hal_config.fadt = NULL;
hal_config.hpet = NULL;
hal_config.devices = MMIO_DEVICES_VIRT_ADDR;
hal_config.memMap.count = 0;

Expand Down
98 changes: 73 additions & 25 deletions hal/ia32/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,42 @@ typedef struct {
} __attribute__((packed)) sdt_header_t;


#define GAS_ADDRESS_SPACE_ID_MEMORY 0x0
#define GAS_ADDRESS_SPACE_ID_IOPORT 0x1
#define GAS_ADDRESS_SPACE_ID_PCI 0x2
#define GAS_ADDRESS_SPACE_ID_EMBEDD 0x03 /* EMBEDDED_CONTROLLER */
#define GAS_ADDRESS_SPACE_ID_SMBUS 0x04
#define GAS_ADDRESS_SPACE_ID_CMOS 0x05
#define GAS_ADDRESS_SPACE_ID_PCIBAR 0x06 /* PCI_BAR_TARGET */
#define GAS_ADDRESS_SPACE_ID_IPMI 0x07
#define GAS_ADDRESS_SPACE_ID_GPIO 0x08
#define GAS_ADDRESS_SPACE_ID_GSB 0x09 /* Generic Serial Bus*/
#define GAS_ADDRESS_SPACE_ID_PCC 0x0A /* Platform Communications Channel */
#define GAS_ADDRESS_SPACE_ID_PRM 0x0B /* Platform Runtime Mechanism */

#define GAS_ACCESS_SIZE_UNDEFINED 0
#define GAS_ACCESS_SIZE_BYTE 1
#define GAS_ACCESS_SIZE_WORD 2
#define GAS_ACCESS_SIZE_DWORD 3
#define GAS_ACCESS_SIZE_QWORD 4

typedef struct {
u8 addressSpaceId;
u8 registerWidth;
u8 registerOffset;
u8 accessSize;
u64 address;
} __attribute__ ((packed)) hal_gas_t;

typedef struct {
u8 addressSpaceId;
u8 registerWidth;
u8 registerOffset;
u8 accessSize;
void *address;
} hal_gasMapped_t;


typedef struct {
sdt_header_t header;
addr_t sdt[];
Expand Down Expand Up @@ -80,15 +116,18 @@ typedef struct {
addr_t localApicAddr;
u32 flags;
u8 entries[]; /* It is an array of variable length elements */
} __attribute__ ((packed)) madt_header_t;
} __attribute__ ((packed)) hal_madtHeader_t;


typedef struct {
u8 addressSpaceId;
u8 registerWidth;
u8 registerOffset;
u8 accessSize;
u64 address;
} __attribute__ ((packed)) generic_address_structure_t;
sdt_header_t header;
u32 eventTimerBlockID;
hal_gas_t baseAddress;
u8 hpetNumber;
u16 minPeriodicClockTick;
u8 pageProtection;
} __attribute__ ((packed)) hal_hpetHeader_t;


typedef struct {
sdt_header_t header;
Expand Down Expand Up @@ -130,25 +169,25 @@ typedef struct {
u16 iapcBootArch; /* IAPC_BOOT_ARCH */
u8 reserved2;
u32 flags;
generic_address_structure_t resetReg; /* RESET_REG */
u8 resetValue; /* RESET_VALUE */
u16 armBootArch; /* ARM_BOOT_ARCH */
hal_gas_t resetReg; /* RESET_REG */
u8 resetValue; /* RESET_VALUE */
u16 armBootArch; /* ARM_BOOT_ARCH */
u8 fadtMinorVersion;
u64 xFirmwareCtrl; /* X_FIRMWARE_CTRL */
u64 xDsdt; /* X_DSDT */
generic_address_structure_t xPm1aEvtBlk; /* X_PM1a_EVT_BLK */
generic_address_structure_t xPm1bEvtBlk; /* X_PM1b_EVT_BLK */
generic_address_structure_t xPm1aCntBlk; /* X_PM1a_CNT_BLK */
generic_address_structure_t xPm1bCntBlk; /* X_PM1b_CNT_BLK */
generic_address_structure_t xPm2CntBlk; /* X_PM2_CNT_BLK */
generic_address_structure_t xPmTmrBlk; /* X_PM_TMR_BLK */
generic_address_structure_t xGpe0Blk; /* X_GPE0_BLK */
generic_address_structure_t xGpe1Blk; /* X_GPE1_BLK */
generic_address_structure_t sleepControlReg; /* SLEEP_CONTROL_REG */
generic_address_structure_t sleepStatusReg; /* SLEEP_STATUS_REG */
u64 xFirmwareCtrl; /* X_FIRMWARE_CTRL */
u64 xDsdt; /* X_DSDT */
hal_gas_t xPm1aEvtBlk; /* X_PM1a_EVT_BLK */
hal_gas_t xPm1bEvtBlk; /* X_PM1b_EVT_BLK */
hal_gas_t xPm1aCntBlk; /* X_PM1a_CNT_BLK */
hal_gas_t xPm1bCntBlk; /* X_PM1b_CNT_BLK */
hal_gas_t xPm2CntBlk; /* X_PM2_CNT_BLK */
hal_gas_t xPmTmrBlk; /* X_PM_TMR_BLK */
hal_gas_t xGpe0Blk; /* X_GPE0_BLK */
hal_gas_t xGpe1Blk; /* X_GPE1_BLK */
hal_gas_t sleepControlReg; /* SLEEP_CONTROL_REG */
hal_gas_t sleepStatusReg; /* SLEEP_STATUS_REG */
u64 hypervisorVendorIdentity;

} __attribute__ ((packed)) fadt_header_t;
} __attribute__ ((packed)) hal_fadtHeader_t;


typedef struct {
Expand All @@ -167,8 +206,9 @@ typedef struct {
addr_t maxAddr;
void *heapStart;
addr_t *ptable;
madt_header_t *madt;
fadt_header_t *fadt;
hal_madtHeader_t *madt;
hal_fadtHeader_t *fadt;
hal_hpetHeader_t *hpet;
void *devices; /* Address space, where memory mapped devices go */
struct {
u32 count;
Expand Down Expand Up @@ -204,4 +244,12 @@ void _hal_configInit(syspage_t *s);
void *_hal_configMapDevice(u32 *pdir, addr_t start, size_t size, int attr);


void _hal_gasAllocDevice(const hal_gas_t *gas, hal_gasMapped_t *mgas, size_t size);


int _hal_gasWrite32(hal_gasMapped_t *gas, u32 offset, u32 val);


int _hal_gasRead32(hal_gasMapped_t *gas, u32 offset, u32 *val);

#endif
2 changes: 1 addition & 1 deletion hal/ia32/interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ static int _hal_ioapicInit(void)
u32 flags;
} __attribute__((packed)) *localApic;

madt_header_t *madt = hal_config.madt;
hal_madtHeader_t *madt = hal_config.madt;
size_t i;
u32 high, low, n;
void *ptr;
Expand Down
Loading

0 comments on commit 2ef32db

Please sign in to comment.