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_COUNT = 3
17 COL_RES_START = 4
18 COL_HOST_START = 5
20 ROW_HOST_ID = 0
21 ROW_RES_START = 1
23 comments = {}
24 resasg = {}
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 key = (restype, subtype, host_id)
59 value = (start, num)
60 if (key not in resasg):
61 resasg[key] = [value]
62 else:
63 print ("WARNING: Ignoring multiple entries for (%s,%s,%s)" % key)
64 #resasg[key].append(value)
65 rmcfg.append((start, num, restype, subtype, host_id))
67 for pair in sharing:
68 if (host_id != pair[0]):
69 continue
71 shared_host = pair[1]
72 rmcfg.append((start, num, restype, subtype, shared_host))
74 start += int(num)
76 return rmcfg
78 def print_rmcfg(rmcfg):
79 comment_templ = '''
80 /* %s */\n'''
81 rmconfig_templ = '''\
82 {
83 .start_resource = %d,
84 .num_resource = %d,
85 .type = RESASG_UTYPE (%s,
86 %s),
87 .host_id = %s,
88 },\n'''
89 output = ""
91 def custom_key(entry):
92 (start, num, restype, subtype, host) = entry
93 restype = soc.const_values[restype]
94 subtype = soc.const_values[subtype]
95 host = soc.const_values[host]
96 utype = (restype << soc.RESASG_TYPE_SHIFT) | (subtype << soc.RESASG_SUBTYPE_SHIFT)
97 val = (utype << 24) | (start << 8) | (host << 0)
98 return val
100 sorted_rmcfg = sorted(rmcfg, key=custom_key)
102 comment = None
103 for entry in sorted_rmcfg:
104 (start, num, restype, subtype, host) = entry
106 if (comment != comments[(restype, subtype)]):
107 comment = comments[(restype, subtype)]
108 output += comment_templ % comment
109 output += rmconfig_templ % (start, num, restype, subtype, host)
110 return output
112 ################################################################################
113 ## Main program starts here ##
114 ################################################################################
116 parser = argparse.ArgumentParser(prog='RM-autogen.py', formatter_class=argparse.RawTextHelpFormatter,
117 description='RM-autogen.py - Auto generate the Resource Management data')
119 parser.add_argument('-s', '--soc', required=True, dest='soc',
120 action='store', choices=['j721e', 'am65x'],
121 help='SoC name')
123 parser.add_argument('-o', '--output', required=True, dest='output',
124 action='store',
125 help='output file name')
127 parser.add_argument('-f', '--format', required=True, dest='format',
128 action='store', choices=['boardconfig', 'jailhouse_cell_config'],
129 help='format to select the output file')
131 parser.add_argument('--share', dest='share', default=[],
132 action='append', nargs=2, metavar=('HOST_ID_A', 'HOST_ID_B'),
133 help='Share resource with HOST_ID_A for HOST_ID_B')
135 parser.add_argument('--allow_all', dest='allow_all',
136 action='store_true',
137 help='Create the minimal boardconfig to allow all hosts to access all resources')
139 parser.add_argument('workbook', help='Input excel sheet with assigned resources')
141 args = parser.parse_args()
142 print(args)
144 soc = __import__(args.soc)
145 workbook = xlrd.open_workbook(args.workbook)
146 sheet = workbook.sheet_by_name(args.soc)
148 #sheet.nrows = 9
149 if (args.format == 'boardconfig'):
150 boardconfig = gen_rmcfg_data(args.share)
151 print ("Total entries = %d" % len(boardconfig))
152 data = print_rmcfg(boardconfig)
153 else:
154 print ("ERROR: format %s not supported")
155 exit(1)
158 ofile = open(args.output, "w")
159 ofile.write(data)
160 ofile.close()
162 # END OF FILE