RM-autogen*.py: Update ROW/COL constants
[glsdk/host-tools.git] / respart / RM-autogen.py
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 in resasg):
61                                 resasg[key].append(value)
62                         else:
63                                 resasg[key] = [value]
64                         rmcfg.append((start, num, restype, subtype, host_id))
66                         for pair in sharing:
67                                 if (host_id != pair[0]):
68                                         continue
70                                 shared_host = pair[1]
71                                 rmcfg.append((start, num, restype, subtype, shared_host))
73                         start += int(num)
75         return rmcfg
77 def print_rmcfg(rmcfg):
78         comment_templ = '''
79                 /* %s */\n'''
80         rmconfig_templ = '''\
81                 {
82                         .start_resource = %d,
83                         .num_resource = %d,
84                         .type = RESASG_UTYPE (%s,
85                                         %s),
86                         .host_id = %s,
87                 },\n'''
88         output = ""
90         def custom_key(entry):
91                 (start, num, restype, subtype, host) = entry
92                 restype = soc.const_values[restype]
93                 subtype = soc.const_values[subtype]
94                 host = soc.const_values[host]
95                 utype = (restype << soc.RESASG_TYPE_SHIFT) | (subtype << soc.RESASG_SUBTYPE_SHIFT)
96                 val = (utype << 24) | (start << 8) | (host << 0)
97                 return val
99         sorted_rmcfg = sorted(rmcfg, key=custom_key)
101         comment = None
102         for entry in sorted_rmcfg:
103                 (start, num, restype, subtype, host) = entry
105                 if (comment != comments[(restype, subtype)]):
106                         comment = comments[(restype, subtype)]
107                         output += comment_templ % comment
108                 output += rmconfig_templ % (start, num, restype, subtype, host)
109         return output
111 ################################################################################
112 ##                          Main program starts here                          ##
113 ################################################################################
115 parser = argparse.ArgumentParser(prog='RM-autogen.py', formatter_class=argparse.RawTextHelpFormatter,
116         description='RM-autogen.py - Auto generate the Resource Management data')
118 parser.add_argument('-s', '--soc', required=True, dest='soc',
119         action='store', choices=['j721e', 'am65x'],
120         help='SoC name')
122 parser.add_argument('-o', '--output', required=True, dest='output',
123         action='store',
124         help='output file name')
126 parser.add_argument('-f', '--format', required=True, dest='format',
127         action='store', choices=['boardconfig', 'jailhouse_cell_config'],
128         help='format to select the output file')
130 parser.add_argument('--share', dest='share', default=[],
131         action='append', nargs=2, metavar=('HOST_ID_A', 'HOST_ID_B'),
132         help='Share resource with HOST_ID_A for HOST_ID_B')
134 parser.add_argument('--allow_all', dest='allow_all',
135         action='store_true',
136         help='Create the minimal boardconfig to allow all hosts to access all resources')
138 parser.add_argument('workbook', help='Input excel sheet with assigned resources')
140 args = parser.parse_args()
141 print(args)
143 soc = __import__(args.soc)
144 workbook = xlrd.open_workbook(args.workbook)
145 sheet = workbook.sheet_by_name(args.soc)
147 #sheet.nrows = 9
148 if (args.format == 'boardconfig'):
149         boardconfig = gen_rmcfg_data(args.share)
150         print ("Total entries = %d" % len(boardconfig))
151         data = print_rmcfg(boardconfig)
152 else:
153         print ("ERROR: format %s not supported")
154         exit(1)
157 ofile = open(args.output, "w")
158 ofile.write(data)
159 ofile.close()
161 # END OF FILE