RM-autogen.py: Fix help string
[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 = 5
17 COL_RES_START = 6
18 COL_HOST_START = 7
20 ROW_HOST_ID = 0
21 ROW_RES_START = 2
23 comments = {}
25 def gen_rmcfg_data(sharing):
26         global comments
27         rmcfg = []
28         for res in range(ROW_RES_START, sheet.nrows):
30                 comment = sheet.cell_value(res, COL_COMMENT)
31                 restype = sheet.cell_value(res, COL_RES_TYPE)
32                 subtype = sheet.cell_value(res, COL_SUB_TYPE)
33                 count = sheet.cell_value(res, COL_RES_COUNT)
34                 start = sheet.cell_value(res, COL_RES_START)
35                 if (restype == '' or subtype == '' or start == '' or count == ''):
36                         continue
37                 start = int(start)
38                 count = int(count)
39                 comments[(restype, subtype)] = comment
41                 if (args.allow_all):
42                         rmcfg.append((start, count, restype, subtype, "HOST_ID_ALL"))
43                         continue
45                 for host in range(COL_HOST_START, sheet.ncols):
47                         #print ("##v(%d, %d) = '%s'" % (res, host, sheet.cell_value(res, host)))
48                         host_id = sheet.cell_value(ROW_HOST_ID, host).split('\n')[0]
49                         if (re.match("HOST_ID_.*", host_id) == None):
50                                 continue
52                         num = sheet.cell_value(res, host)
53                         if (num == '' or int(num) == 0):
54                                 continue
55                         num = int(num)
57                         rmcfg.append((start, num, restype, subtype, host_id))
59                         for pair in sharing:
60                                 if (host_id != pair[0]):
61                                         continue
63                                 shared_host = pair[1]
64                                 rmcfg.append((start, num, restype, subtype, shared_host))
66                         start += int(num)
68         return rmcfg
70 def print_rmcfg(rmcfg):
71         comment_templ = '''
72                 /* %s */\n'''
73         rmconfig_templ = '''\
74                 {
75                         .start_resource = %d,
76                         .num_resource = %d,
77                         .type = RESASG_UTYPE (%s,
78                                         %s),
79                         .host_id = %s,
80                 },\n'''
81         output = ""
83         def custom_key(entry):
84                 (start, num, restype, subtype, host) = entry
85                 restype = soc.const_values[restype]
86                 subtype = soc.const_values[subtype]
87                 host = soc.const_values[host]
88                 utype = (restype << soc.RESASG_TYPE_SHIFT) | (subtype << soc.RESASG_SUBTYPE_SHIFT)
89                 val = (utype << 24) | (start << 8) | (host << 0)
90                 return val
92         sorted_rmcfg = sorted(rmcfg, key=custom_key)
94         comment = None
95         for entry in sorted_rmcfg:
96                 (start, num, restype, subtype, host) = entry
98                 if (comment != comments[(restype, subtype)]):
99                         comment = comments[(restype, subtype)]
100                         output += comment_templ % comment
101                 output += rmconfig_templ % (start, num, restype, subtype, host)
102         return output
104 ################################################################################
105 ##                          Main program starts here                          ##
106 ################################################################################
108 parser = argparse.ArgumentParser(prog='RM-autogen.py', formatter_class=argparse.RawTextHelpFormatter,
109         description='RM-autogen.py - Auto generate the Resource Management data')
111 parser.add_argument('-s', '--soc', required=True, dest='soc',
112         action='store', choices=['j721e', 'am65x'],
113         help='SoC name')
115 parser.add_argument('-o', '--output', required=True, dest='output',
116         action='store',
117         help='output file name')
119 parser.add_argument('-f', '--format', required=True, dest='format',
120         action='store', choices=['boardconfig', 'jailhouse_cell_config'],
121         help='format to select the output file')
123 parser.add_argument('--share', dest='share', default=[],
124         action='append', nargs=2, metavar=('HOST_ID_A', 'HOST_ID_B'),
125         help='Share resource with HOST_ID_A for HOST_ID_B')
127 parser.add_argument('--allow_all', dest='allow_all',
128         action='store_true',
129         help='Create the minimal boardconfig to allow all hosts to access all resources')
131 parser.add_argument('workbook', help='Input excel sheet with assigned resources')
133 args = parser.parse_args()
134 print(args)
136 soc = __import__(args.soc)
137 workbook = xlrd.open_workbook(args.workbook)
138 sheet = workbook.sheet_by_name(args.soc)
140 #sheet.nrows = 9
141 if (args.format == 'boardconfig'):
142         boardconfig = gen_rmcfg_data(args.share)
143         print ("Total entries = %d" % len(boardconfig))
144         data = print_rmcfg(boardconfig)
145 else:
146         print ("ERROR: format %s not supported")
147         exit(1)
150 ofile = open(args.output, "w")
151 ofile.write(data)
152 ofile.close()
154 # END OF FILE