Skip to content

Commit bf917ae

Browse files
Sainath Grandhiwenlingz
authored andcommitted
acrn-config: Generate info in pci_dev file for Pre-Launched VMs
Pre-Launched VMs need the vbar_base values pre-populated for pass-thru PCI devices. This patch moves the pci parser logic from board_cfg_gen to library so that scenario_cfg_gen scripts can use pci parser routines while generating pci_dev file logical partition scenario. Tracked-On: #4666 Signed-off-by: Sainath Grandhi <sainath.grandhi@intel.com>
1 parent b99de16 commit bf917ae

File tree

3 files changed

+166
-135
lines changed

3 files changed

+166
-135
lines changed

misc/acrn-config/board_config/pci_devices_h.py

Lines changed: 6 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -14,135 +14,6 @@
1414
PCI_END_HEADER = r"""
1515
#endif /* PCI_DEVICES_H_ */"""
1616

17-
18-
HI_MMIO_OFFSET = 0
19-
20-
class Bar_Mem:
21-
def __init__(self):
22-
self.addr = 0
23-
self.remapped = False
24-
25-
26-
class Bar_Attr:
27-
def __init__(self):
28-
self.name = 0
29-
self.remappable = True
30-
31-
32-
class Pci_Dev_Bar_Desc:
33-
def __init__(self):
34-
self.pci_dev_dic = {}
35-
self.pci_bar_dic = {}
36-
37-
PCI_DEV_BAR_DESC = Pci_Dev_Bar_Desc()
38-
39-
40-
def get_value_after_str(line, key):
41-
""" Get the value after cstate string """
42-
idx = 0
43-
line_in_list = line.split()
44-
for idx_key, val in enumerate(line_in_list):
45-
if val == key:
46-
idx = idx_key
47-
break
48-
49-
return line_in_list[idx + 1]
50-
51-
52-
def check_bar_remappable(line):
53-
#TODO: check device BAR remappable per ACPI table
54-
55-
return True
56-
57-
58-
def get_size(line):
59-
60-
# get size string from format, Region n: Memory at x ... [size=NK]
61-
size_str = line.split()[-1].strip(']').split('=')[1]
62-
if 'G' in size_str:
63-
size = int(size_str.strip('G')) * common.SIZE_G
64-
elif 'M' in size_str:
65-
size = int(size_str.strip('M')) * common.SIZE_M
66-
elif 'K' in size_str:
67-
size = int(size_str.strip('K')) * common.SIZE_K
68-
else:
69-
size = int(size_str)
70-
71-
return size
72-
73-
# round up the running bar_addr to the size of the incoming bar "line"
74-
def remap_bar_addr_to_high(bar_addr, line):
75-
"""Generate vbar address"""
76-
global HI_MMIO_OFFSET
77-
size = get_size(line)
78-
cur_addr = common.round_up(bar_addr, size)
79-
HI_MMIO_OFFSET = cur_addr + size
80-
return cur_addr
81-
82-
83-
def parser_pci():
84-
""" Parse PCI lines """
85-
cur_bdf = 0
86-
prev_bdf = 0
87-
tmp_bar_dic = {}
88-
bar_addr = bar_num = '0'
89-
cal_sub_pci_name = []
90-
91-
pci_lines = board_cfg_lib.get_info(
92-
common.BOARD_INFO_FILE, "<PCI_DEVICE>", "</PCI_DEVICE>")
93-
94-
for line in pci_lines:
95-
tmp_bar_mem = Bar_Mem()
96-
# get pci bar information into PCI_DEV_BAR_DESC
97-
if "Region" in line and "Memory at" in line:
98-
#ignore memory region from SR-IOV capabilities
99-
if "size=" not in line:
100-
continue
101-
102-
bar_addr = int(get_value_after_str(line, "at"), 16)
103-
bar_num = line.split()[1].strip(':')
104-
if bar_addr >= common.SIZE_4G or bar_addr < common.SIZE_2G:
105-
if not tmp_bar_attr.remappable:
106-
continue
107-
108-
bar_addr = remap_bar_addr_to_high(HI_MMIO_OFFSET, line)
109-
tmp_bar_mem.remapped = True
110-
111-
tmp_bar_mem.addr = hex(bar_addr)
112-
tmp_bar_dic[int(bar_num)] = tmp_bar_mem
113-
else:
114-
tmp_bar_attr = Bar_Attr()
115-
prev_bdf = cur_bdf
116-
pci_bdf = line.split()[0]
117-
tmp_bar_attr.name = " ".join(line.split(':')[1].split()[1:])
118-
119-
# remove '[*]' in pci subname
120-
if '[' in tmp_bar_attr.name:
121-
tmp_bar_attr.name = tmp_bar_attr.name.rsplit('[', 1)[0]
122-
123-
cal_sub_pci_name.append(tmp_bar_attr.name)
124-
tmp_bar_attr.remappable = check_bar_remappable(line)
125-
PCI_DEV_BAR_DESC.pci_dev_dic[pci_bdf] = tmp_bar_attr
126-
cur_bdf = pci_bdf
127-
128-
if not prev_bdf:
129-
prev_bdf = cur_bdf
130-
131-
if tmp_bar_dic and cur_bdf != prev_bdf:
132-
PCI_DEV_BAR_DESC.pci_bar_dic[prev_bdf] = tmp_bar_dic
133-
134-
# clear the tmp_bar_dic before store the next dic
135-
tmp_bar_dic = {}
136-
137-
# output all the pci device list to pci_device.h
138-
sub_name_count = collections.Counter(cal_sub_pci_name)
139-
140-
if tmp_bar_dic:
141-
PCI_DEV_BAR_DESC.pci_bar_dic[cur_bdf] = tmp_bar_dic
142-
143-
return sub_name_count
144-
145-
14617
def write_pbdf(i_cnt, bdf, bar_attr, config):
14718
"""
14819
Parser and generate pbdf
@@ -160,6 +31,8 @@ def write_pbdf(i_cnt, bdf, bar_attr, config):
16031
else:
16132
tmp_sub_name = "_".join(bar_attr.name.split()).upper() + "_" + str(i_cnt)
16233

34+
board_cfg_lib.PCI_DEV_BAR_DESC.pci_dev_dic[bdf].name_w_i_cnt = tmp_sub_name
35+
16336
bus = int(bdf.split(':')[0], 16)
16437
dev = int(bdf.split(':')[1].split('.')[0], 16)
16538
fun = int(bdf.split('.')[1], 16)
@@ -227,22 +100,22 @@ def generate_file(config):
227100
# write the header into pci
228101
print("{0}".format(PCI_HEADER), file=config)
229102

230-
sub_name_count = parser_pci()
103+
sub_name_count = board_cfg_lib.parser_pci()
231104

232-
print("#define %-32s" % "PTDEV_HI_MMIO_SIZE", " {}UL".format(hex(HI_MMIO_OFFSET)), file=config)
105+
print("#define %-32s" % "PTDEV_HI_MMIO_SIZE", " {}UL".format(hex(board_cfg_lib.HI_MMIO_OFFSET)), file=config)
233106

234107
compared_bdf = []
235108
for cnt_sub_name in sub_name_count.keys():
236109
i_cnt = 0
237-
for bdf, bar_attr in PCI_DEV_BAR_DESC.pci_dev_dic.items():
110+
for bdf, bar_attr in board_cfg_lib.PCI_DEV_BAR_DESC.pci_dev_dic.items():
238111
if cnt_sub_name == bar_attr.name and bdf not in compared_bdf:
239112
compared_bdf.append(bdf)
240113
else:
241114
continue
242115

243116
print("",file=config)
244117
write_pbdf(i_cnt, bdf, bar_attr, config)
245-
write_vbar(i_cnt, bdf, PCI_DEV_BAR_DESC.pci_bar_dic, bar_attr, config)
118+
write_vbar(i_cnt, bdf, board_cfg_lib.PCI_DEV_BAR_DESC.pci_bar_dic, bar_attr, config)
246119

247120
i_cnt += 1
248121

misc/acrn-config/library/board_cfg_lib.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import re
77
import sys
88
import common
9+
import collections
910

1011
BOARD_NAME = ''
1112
BIOS_INFO = ['BIOS Information', 'Vendor:', 'Version:', 'Release Date:', 'BIOS Revision:']
@@ -466,3 +467,129 @@ def get_pci_info(board_info):
466467
pci_bdf_vpid[bdf_str] = vid_pid
467468

468469
return (pci_desc, pci_bdf_vpid)
470+
471+
HI_MMIO_OFFSET = 0
472+
473+
class Bar_Mem:
474+
def __init__(self):
475+
self.addr = 0
476+
self.remapped = False
477+
478+
479+
class Bar_Attr:
480+
def __init__(self):
481+
self.name = 0
482+
self.remappable = True
483+
self.name_w_i_cnt = 0
484+
485+
class Pci_Dev_Bar_Desc:
486+
def __init__(self):
487+
self.pci_dev_dic = {}
488+
self.pci_bar_dic = {}
489+
490+
PCI_DEV_BAR_DESC = Pci_Dev_Bar_Desc()
491+
492+
493+
def get_value_after_str(line, key):
494+
""" Get the value after cstate string """
495+
idx = 0
496+
line_in_list = line.split()
497+
for idx_key, val in enumerate(line_in_list):
498+
if val == key:
499+
idx = idx_key
500+
break
501+
502+
return line_in_list[idx + 1]
503+
504+
505+
def check_bar_remappable(line):
506+
#TODO: check device BAR remappable per ACPI table
507+
508+
return True
509+
510+
511+
def get_size(line):
512+
513+
# get size string from format, Region n: Memory at x ... [size=NK]
514+
size_str = line.split()[-1].strip(']').split('=')[1]
515+
if 'G' in size_str:
516+
size = int(size_str.strip('G')) * common.SIZE_G
517+
elif 'M' in size_str:
518+
size = int(size_str.strip('M')) * common.SIZE_M
519+
elif 'K' in size_str:
520+
size = int(size_str.strip('K')) * common.SIZE_K
521+
else:
522+
size = int(size_str)
523+
524+
return size
525+
526+
# round up the running bar_addr to the size of the incoming bar "line"
527+
def remap_bar_addr_to_high(bar_addr, line):
528+
"""Generate vbar address"""
529+
global HI_MMIO_OFFSET
530+
size = get_size(line)
531+
cur_addr = common.round_up(bar_addr, size)
532+
HI_MMIO_OFFSET = cur_addr + size
533+
return cur_addr
534+
535+
536+
def parser_pci():
537+
""" Parse PCI lines """
538+
cur_bdf = 0
539+
prev_bdf = 0
540+
tmp_bar_dic = {}
541+
bar_addr = bar_num = '0'
542+
cal_sub_pci_name = []
543+
544+
pci_lines = get_info(common.BOARD_INFO_FILE, "<PCI_DEVICE>", "</PCI_DEVICE>")
545+
546+
for line in pci_lines:
547+
tmp_bar_mem = Bar_Mem()
548+
# get pci bar information into board_cfg_lib.PCI_DEV_BAR_DESC
549+
if "Region" in line and "Memory at" in line:
550+
#ignore memory region from SR-IOV capabilities
551+
if "size=" not in line:
552+
continue
553+
554+
bar_addr = int(get_value_after_str(line, "at"), 16)
555+
bar_num = line.split()[1].strip(':')
556+
if bar_addr >= common.SIZE_4G or bar_addr < common.SIZE_2G:
557+
if not tmp_bar_attr.remappable:
558+
continue
559+
560+
bar_addr = remap_bar_addr_to_high(HI_MMIO_OFFSET, line)
561+
tmp_bar_mem.remapped = True
562+
563+
tmp_bar_mem.addr = hex(bar_addr)
564+
tmp_bar_dic[int(bar_num)] = tmp_bar_mem
565+
else:
566+
tmp_bar_attr = Bar_Attr()
567+
prev_bdf = cur_bdf
568+
pci_bdf = line.split()[0]
569+
tmp_bar_attr.name = " ".join(line.split(':')[1].split()[1:])
570+
571+
# remove '[*]' in pci subname
572+
if '[' in tmp_bar_attr.name:
573+
tmp_bar_attr.name = tmp_bar_attr.name.rsplit('[', 1)[0]
574+
575+
cal_sub_pci_name.append(tmp_bar_attr.name)
576+
tmp_bar_attr.remappable = check_bar_remappable(line)
577+
PCI_DEV_BAR_DESC.pci_dev_dic[pci_bdf] = tmp_bar_attr
578+
cur_bdf = pci_bdf
579+
580+
if not prev_bdf:
581+
prev_bdf = cur_bdf
582+
583+
if tmp_bar_dic and cur_bdf != prev_bdf:
584+
PCI_DEV_BAR_DESC.pci_bar_dic[prev_bdf] = tmp_bar_dic
585+
586+
# clear the tmp_bar_dic before store the next dic
587+
tmp_bar_dic = {}
588+
589+
# output all the pci device list to pci_device.h
590+
sub_name_count = collections.Counter(cal_sub_pci_name)
591+
592+
if tmp_bar_dic:
593+
PCI_DEV_BAR_DESC.pci_bar_dic[cur_bdf] = tmp_bar_dic
594+
595+
return sub_name_count

misc/acrn-config/scenario_config/pci_dev_c.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,43 @@
55

66
import common
77
import scenario_cfg_lib
8+
import board_cfg_lib
89

910
PCI_DEV_TYPE = ['PCI_DEV_TYPE_HVEMUL', 'PCI_DEV_TYPE_PTDEV']
1011

12+
def add_instance_to_name(i_cnt, bdf, bar_attr):
13+
if i_cnt == 0 and bar_attr.name.upper() == "HOST BRIDGE":
14+
tmp_sub_name = "_".join(bar_attr.name.split()).upper()
15+
else:
16+
if '-' in bar_attr.name:
17+
tmp_sub_name = common.undline_name(bar_attr.name) + "_" + str(i_cnt)
18+
else:
19+
tmp_sub_name = "_".join(bar_attr.name.split()).upper() + "_" + str(i_cnt)
20+
21+
board_cfg_lib.PCI_DEV_BAR_DESC.pci_dev_dic[bdf].name_w_i_cnt = tmp_sub_name
1122

1223
def generate_file(vm_info, config):
1324
"""
14-
Generate pci_dev.c while logical_partition scenario
25+
Generate pci_dev.c for Pre-Launched VMs in a scenario.
1526
:param config: it is pointer for for file write to
1627
:return: None
1728
"""
29+
sub_name_count = board_cfg_lib.parser_pci()
30+
31+
compared_bdf = []
32+
33+
for cnt_sub_name in sub_name_count.keys():
34+
i_cnt = 0
35+
for bdf, bar_attr in board_cfg_lib.PCI_DEV_BAR_DESC.pci_dev_dic.items():
36+
if cnt_sub_name == bar_attr.name and bdf not in compared_bdf:
37+
compared_bdf.append(bdf)
38+
else:
39+
continue
40+
41+
add_instance_to_name(i_cnt, bdf, bar_attr)
42+
43+
i_cnt += 1
44+
1845
print("{}".format(scenario_cfg_lib.HEADER_LICENSE), file=config)
1946
print("", file=config)
2047
print("#include <vm_config.h>", file=config)
@@ -52,7 +79,11 @@ def generate_file(vm_info, config):
5279
print("\t{", file=config)
5380
print("\t\t.emu_type = {},".format(PCI_DEV_TYPE[1]), file=config)
5481
print("\t\t.vbdf.bits = {{.b = 0x00U, .d = 0x0{}U, .f = 0x00U}},".format(pci_cnt), file=config)
55-
print("\t\t.pbdf.bits = {{.b = 0x{:02X}U, .d = 0x{:02X}U, .f = 0x{:02X}U}},".format(bus, dev, fun), file=config)
82+
for bdf, bar_attr in board_cfg_lib.PCI_DEV_BAR_DESC.pci_dev_dic.items():
83+
if bdf == pci_bdf_dev:
84+
print("\t\t{},".format(board_cfg_lib.PCI_DEV_BAR_DESC.pci_dev_dic[bdf].name_w_i_cnt), file=config)
85+
else:
86+
continue
5687
print("\t},", file=config)
5788
pci_cnt += 1
5889

0 commit comments

Comments
 (0)