Handle tsc rollover.
[seabios.git] / src / util.h
index fbcfb79b8a414fa7a0d518f5c754105c8bcd6e09..f95cdb598e803e39808ac61054f5793d85994b74 100644 (file)
@@ -94,6 +94,25 @@ static inline u32 __ffs(u32 word)
     return word;
 }
 
+static inline void writel(void *addr, u32 val) {
+    *(volatile u32 *)addr = val;
+}
+static inline void writew(void *addr, u16 val) {
+    *(volatile u16 *)addr = val;
+}
+static inline void writeb(void *addr, u8 val) {
+    *(volatile u8 *)addr = val;
+}
+static inline u32 readl(const void *addr) {
+    return *(volatile const u32 *)addr;
+}
+static inline u16 readw(const void *addr) {
+    return *(volatile const u16 *)addr;
+}
+static inline u8 readb(const void *addr) {
+    return *(volatile const u8 *)addr;
+}
+
 // GDT bit manipulation
 #define GDT_BASE(v)  ((((u64)(v) & 0xff000000) << 32)           \
                       | (((u64)(v) & 0x00ffffff) << 16))
@@ -156,6 +175,8 @@ void printf(const char *fmt, ...)
     __attribute__ ((format (printf, 1, 2)));
 void __dprintf(const char *fmt, ...)
     __attribute__ ((format (printf, 1, 2)));
+void snprintf(char *str, size_t size, const char *fmt, ...)
+    __attribute__ ((format (printf, 3, 4)));
 #define dprintf(lvl, fmt, args...) do {                         \
         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
             __dprintf((fmt) , ##args );                         \
@@ -193,11 +214,15 @@ void serial_setup();
 void lpt_setup();
 
 // clock.c
+static inline int check_time(u64 end) {
+    return (s64)(rdtscll() - end) > 0;
+}
 void timer_setup();
 void ndelay(u32 count);
 void udelay(u32 count);
 void mdelay(u32 count);
 u64 calc_future_tsc(u32 msecs);
+u64 calc_future_tsc_usec(u32 usecs);
 void handle_1583(struct bregs *regs);
 void handle_1586(struct bregs *regs);
 
@@ -219,13 +244,11 @@ void smm_init();
 
 // smp.c
 extern u32 CountCPUs;
+extern u32 MaxCountCPUs;
 void wrmsr_smp(u32 index, u64 val);
 void smp_probe(void);
 void smp_probe_setup(void);
 
-// smbios.c
-void smbios_init(void);
-
 // coreboot.c
 struct cbfs_file;
 struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
@@ -260,32 +283,39 @@ void pnp_setup();
 
 // pmm.c
 extern struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
-void *zone_malloc(struct zone_s *zone, u32 size, u32 align);
-void *zone_malloc_low(u32 size, u32 align);
 void malloc_setup();
 void malloc_finalize();
+void *pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align);
+int pmm_free(void *data);
 void pmm_setup();
 void pmm_finalize();
+#define PMM_DEFAULT_HANDLE 0xFFFFFFFF
 // Minimum alignment of malloc'd memory
 #define MALLOC_MIN_ALIGN 16
 // Helper functions for memory allocation.
 static inline void *malloc_low(u32 size) {
-    return zone_malloc_low(size, MALLOC_MIN_ALIGN);
+    return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
 }
 static inline void *malloc_high(u32 size) {
-    return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
+    return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
 }
 static inline void *malloc_fseg(u32 size) {
-    return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
+    return pmm_malloc(&ZoneFSeg, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
 }
 static inline void *malloc_tmphigh(u32 size) {
-    return zone_malloc(&ZoneTmpHigh, size, MALLOC_MIN_ALIGN);
+    return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
 }
-static inline void *memalign_tmphigh(u32 align, u32 size) {
-    return zone_malloc(&ZoneTmpHigh, size, align);
+static inline void *memalign_low(u32 align, u32 size) {
+    return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, align);
 }
 static inline void *memalign_high(u32 align, u32 size) {
-    return zone_malloc(&ZoneHigh, size, align);
+    return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, align);
+}
+static inline void *memalign_tmphigh(u32 align, u32 size) {
+    return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, align);
+}
+static inline void free(void *data) {
+    pmm_free(data);
 }
 
 // mtrr.c
@@ -297,9 +327,6 @@ void reset_vector() __attribute__ ((noreturn));
 // misc.c
 extern u8 BiosChecksum;
 
-// mptable.c
-extern int irq0override;
-
 // version (auto generated file out/version.c)
 extern const char VERSION[];