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 086a5b9 commit 4c25065
Show file tree
Hide file tree
Showing 4 changed files with 320 additions and 62 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 generic_address_structure_t *gas, mapped_gas_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(mapped_gas_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(mapped_gas_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
60 changes: 54 additions & 6 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)) generic_address_structure_t;

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


typedef struct {
sdt_header_t header;
addr_t sdt[];
Expand Down Expand Up @@ -82,13 +118,16 @@ typedef struct {
u8 entries[]; /* It is an array of variable length elements */
} __attribute__ ((packed)) madt_header_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;
generic_address_structure_t baseAddress;
u8 hpetNumber;
u16 minPeriodicClockTick;
u8 pageProtection;
} __attribute__ ((packed)) hpet_header_t;


typedef struct {
sdt_header_t header;
Expand Down Expand Up @@ -169,6 +208,7 @@ typedef struct {
addr_t *ptable;
madt_header_t *madt;
fadt_header_t *fadt;
hpet_header_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 generic_address_structure_t *gas, mapped_gas_t *mgas, size_t size);


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


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

#endif
Loading

0 comments on commit 4c25065

Please sign in to comment.