1 #!/usr/bin/python
2 #
3 # Author: Nikhil Devshatwar <nikhil.nd@ti.com>
4 # Python script to auto generate the Resource Management data
5 # Parses excel sheet and generates different files which need
6 # to be in sync for correct functionality
7 #
8 import argparse
9 import xlrd
10 import xlwt
11 import re
13 COL_COMMENT = 0
14 COL_RES_TYPE = 1
15 COL_SUB_TYPE = 2
16 COL_RES_FIELD = 3
17 COL_RES_COUNT = 5
18 COL_RES_START = 6
19 COL_HOST_START = 7
21 ROW_HOST_ID = 0
22 ROW_RES_START = 2
24 comments = {}
26 def gen_rmcfg_data(sharing):
27 global comments
28 rmcfg = []
29 for res in range(ROW_RES_START, sheet.nrows):
31 comment = sheet.cell_value(res, COL_COMMENT)
32 restype = sheet.cell_value(res, COL_RES_TYPE)
33 subtype = sheet.cell_value(res, COL_SUB_TYPE)
34 count = sheet.cell_value(res, COL_RES_COUNT)
35 start = sheet.cell_value(res, COL_RES_START)
36 if (restype == '' or subtype == '' or start == '' or count == ''):
37 continue
38 start = int(start)
39 count = int(count)
40 comments[(restype, subtype)] = comment
42 if (args.allow_all):
43 rmcfg.append((start, count, restype, subtype, "HOST_ID_ALL"))
44 continue
46 for host in range(COL_HOST_START, sheet.ncols):
48 #print ("##v(%d, %d) = '%s'" % (res, host, sheet.cell_value(res, host)))
49 host_id = sheet.cell_value(ROW_HOST_ID, host).split('\n')[0]
50 if (re.match("HOST_ID_.*", host_id) == None):
51 continue
53 num = sheet.cell_value(res, host)
54 if (num == '' or int(num) == 0):
55 continue
56 num = int(num)
58 rmcfg.append((start, num, restype, subtype, host_id))
60 for pair in sharing:
61 if (host_id != pair[0]):
62 continue
64 shared_host = pair[1]
65 rmcfg.append((start, num, restype, subtype, shared_host))
67 start += int(num)
69 return rmcfg
71 def print_rmcfg(rmcfg):
72 comment_templ = '''
73 /* %s */\n'''
74 rmconfig_templ = '''\
75 {
76 .start_resource = %d,
77 .num_resource = %d,
78 .type = RESASG_UTYPE (%s,
79 %s),
80 .host_id = %s,
81 },\n'''
82 output = ""
84 def custom_key(entry):
85 (start, num, restype, subtype, host) = entry
86 restype = soc.const_values[restype]
87 subtype = soc.const_values[subtype]
88 host = soc.const_values[host]
89 utype = (restype << soc.RESASG_TYPE_SHIFT) | (subtype << soc.RESASG_SUBTYPE_SHIFT)
90 val = (utype << 24) | (start << 8) | (host << 0)
91 return val
93 sorted_rmcfg = sorted(rmcfg, key=custom_key)
95 comment = None
96 for entry in sorted_rmcfg:
97 (start, num, restype, subtype, host) = entry
99 if (comment != comments[(restype, subtype)]):
100 comment = comments[(restype, subtype)]
101 output += comment_templ % comment
102 output += rmconfig_templ % (start, num, restype, subtype, host)
103 return output
105 ################################################################################
106 ## Main program starts here ##
107 ################################################################################
109 parser = argparse.ArgumentParser(prog='RM-autogen.py', formatter_class=argparse.RawTextHelpFormatter,
110 description='RM-autogen.py - Auto generate the Resource Management data')
112 parser.add_argument('-s', '--soc', required=True, dest='soc',
113 action='store', choices=['j721e', 'am65x'],
114 help='Share resource with HOST_ID_A for HOST_ID_B')
116 parser.add_argument('-o', '--output', required=True, dest='output',
117 action='store',
118 help='output file name')
120 parser.add_argument('-f', '--format', required=True, dest='format',
121 action='store', choices=['boardconfig', 'jailhouse_cell_config'],
122 help='format to select the output file')
124 parser.add_argument('--share', dest='share', default=[],
125 action='append', nargs=2, metavar=('HOST_ID_A', 'HOST_ID_B'),
126 help='Share resource with HOST_ID_A for HOST_ID_B')
128 parser.add_argument('--allow_all', dest='allow_all',
129 action='store_true',
130 help='Create the minimal boardconfig to allow all hosts to access all resources')
132 parser.add_argument('workbook', help='Input excel sheet with assigned resources')
134 args = parser.parse_args()
135 print(args)
137 soc = __import__(args.soc)
138 workbook = xlrd.open_workbook(args.workbook)
139 sheet = workbook.sheet_by_name(args.soc)
141 #sheet.nrows = 9
142 if (args.format == 'boardconfig'):
143 boardconfig = gen_rmcfg_data(args.share)
144 print ("Total entries = %d" % len(boardconfig))
145 data = print_rmcfg(boardconfig)
146 else:
147 print ("ERROR: format %s not supported")
148 exit(1)
151 ofile = open(args.output, "w")
152 ofile.write(data)
153 ofile.close()
155 # END OF FILE