xref: /kvm-unit-tests/lib/x86/fwcfg.c (revision 4363f1d9a646a5c7ea673bee8fc33ca6f2cddbd8)
1 #include "fwcfg.h"
2 #include "smp.h"
3 
4 static struct spinlock lock;
5 
6 static uint64_t fwcfg_get_u(uint16_t index, int bytes)
7 {
8     uint64_t r = 0;
9     uint8_t b;
10     int i;
11 
12     spin_lock(&lock);
13     asm volatile ("out %0, %1" : : "a"(index), "d"((uint16_t)BIOS_CFG_IOPORT));
14     for (i = 0; i < bytes; ++i) {
15         asm volatile ("in %1, %0" : "=a"(b) : "d"((uint16_t)(BIOS_CFG_IOPORT + 1)));
16         r |= (uint64_t)b << (i * 8);
17     }
18     spin_unlock(&lock);
19     return r;
20 }
21 
22 uint8_t fwcfg_get_u8(unsigned index)
23 {
24     return fwcfg_get_u(index, 1);
25 }
26 
27 uint16_t fwcfg_get_u16(unsigned index)
28 {
29     return fwcfg_get_u(index, 2);
30 }
31 
32 uint32_t fwcfg_get_u32(unsigned index)
33 {
34     return fwcfg_get_u(index, 4);
35 }
36 
37 uint64_t fwcfg_get_u64(unsigned index)
38 {
39     return fwcfg_get_u(index, 8);
40 }
41 
42 unsigned fwcfg_get_nb_cpus(void)
43 {
44     return fwcfg_get_u16(FW_CFG_NB_CPUS);
45 }
46