device: iterable supported devices

Add supported device information to the device `handles` array. This
enables API's to iterate over supported devices for power management
purposes.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
This commit is contained in:
Jordan Yates 2021-08-20 18:31:18 +10:00 committed by Carles Cufí
commit 106f710c7d
2 changed files with 70 additions and 11 deletions

View file

@ -683,6 +683,26 @@ static inline bool device_is_ready(const struct device *dev)
* in a distinct pass1 section (which will be replaced by
* postprocessing).
*
* Before processing in gen_handles.py, the array format is:
* {
* DEVICE_ORDINAL (or DEVICE_HANDLE_NULL if not a devicetree node),
* List of devicetree dependency ordinals (if any),
* DEVICE_HANDLE_SEP,
* List of injected dependency ordinals (if any),
* DEVICE_HANDLE_SEP,
* List of devicetree supporting ordinals (if any),
* }
*
* After processing in gen_handles.py, the format is updated to:
* {
* List of existing devicetree dependency handles (if any),
* DEVICE_HANDLE_SEP,
* List of injected dependency ordinals (if any),
* DEVICE_HANDLE_SEP,
* List of existing devicetree support handles (if any),
* DEVICE_HANDLE_NULL
* }
*
* It is also (experimentally) necessary to provide explicit alignment
* on each object. Otherwise x86-64 builds will introduce padding
* between objects in the same input section in individual object
@ -711,6 +731,9 @@ BUILD_ASSERT(sizeof(device_handle_t) == 2, "fix the linker scripts");
)) \
DEVICE_HANDLE_SEP, \
Z_DEVICE_EXTRA_HANDLES(__VA_ARGS__) \
DEVICE_HANDLE_SEP, \
COND_CODE_1(DT_NODE_EXISTS(node_id), \
(DT_SUPPORTS_DEP_ORDS(node_id)), ()) \
};
#define Z_DEVICE_DEFINE_INIT(node_id, dev_name) \

View file

@ -246,15 +246,19 @@ def main():
hvi = 1
handle.dev_deps = []
handle.ext_deps = []
deps = handle.dev_deps
handle.dev_sups = []
hdls = handle.dev_deps
while hvi < len(hv):
h = hv[hvi]
if h == DEVICE_HANDLE_ENDS:
break
if h == DEVICE_HANDLE_SEP:
deps = handle.ext_deps
if hdls == handle.dev_deps:
hdls = handle.ext_deps
else:
hdls = handle.dev_sups
else:
deps.append(h)
hdls.append(h)
n = edt
hvi += 1
@ -264,22 +268,36 @@ def main():
root = edt.dep_ord2node[0]
assert root not in used_nodes
for sn in used_nodes:
for n in used_nodes:
# Where we're storing the final set of nodes: these are all used
sn.__depends = set()
n.__depends = set()
n.__supports = set()
deps = set(sn.depends_on)
debug("\nNode: %s\nOrig deps:\n\t%s" % (sn.path, "\n\t".join([dn.path for dn in deps])))
deps = set(n.depends_on)
debug("\nNode: %s\nOrig deps:\n\t%s" % (n.path, "\n\t".join([dn.path for dn in deps])))
while len(deps) > 0:
dn = deps.pop()
if dn in used_nodes:
# this is used
sn.__depends.add(dn)
n.__depends.add(dn)
elif dn != root:
# forward the dependency up one level
for ddn in dn.depends_on:
deps.add(ddn)
debug("final deps:\n\t%s\n" % ("\n\t".join([ _dn.path for _dn in sn.__depends])))
debug("Final deps:\n\t%s\n" % ("\n\t".join([ _dn.path for _dn in n.__depends])))
sups = set(n.required_by)
debug("\nOrig sups:\n\t%s" % ("\n\t".join([dn.path for dn in sups])))
while len(sups) > 0:
sn = sups.pop()
if sn in used_nodes:
# this is used
n.__supports.add(sn)
else:
# forward the support down one level
for ssn in sn.required_by:
sups.add(ssn)
debug("\nFinal sups:\n\t%s" % ("\n\t".join([_sn.path for _sn in n.__supports])))
with open(args.output_source, "w") as fp:
fp.write('#include <device.h>\n')
@ -290,6 +308,7 @@ def main():
assert hs, "no hs for %s" % (dev.sym.name,)
dep_paths = []
ext_paths = []
sup_paths = []
hdls = []
sn = hs.node
@ -300,12 +319,24 @@ def main():
dep_paths.append(dn.path)
else:
dep_paths.append('(%s)' % dn.path)
# Force separator to signal start of injected dependencies
hdls.append(DEVICE_HANDLE_SEP)
if len(hs.ext_deps) > 0:
# TODO: map these to something smaller?
ext_paths.extend(map(str, hs.ext_deps))
hdls.append(DEVICE_HANDLE_SEP)
hdls.extend(hs.ext_deps)
# Force separator to signal start of supported devices
hdls.append(DEVICE_HANDLE_SEP)
if len(hs.dev_sups) > 0:
for dn in sn.required_by:
if dn in sn.__supports:
sup_paths.append(dn.path)
else:
sup_paths.append('(%s)' % dn.path)
hdls.extend(dn.__device.dev_handle for dn in sn.__supports)
# Terminate the array with the end symbol
hdls.append(DEVICE_HANDLE_ENDS)
@ -315,9 +346,14 @@ def main():
]
if len(dep_paths) > 0:
lines.append(' * - %s' % ('\n * - '.join(dep_paths)))
lines.append(' * Direct Dependencies:')
lines.append(' * - %s' % ('\n * - '.join(dep_paths)))
if len(ext_paths) > 0:
lines.append(' * + %s' % ('\n * + '.join(ext_paths)))
lines.append(' * Injected Dependencies:')
lines.append(' * - %s' % ('\n * - '.join(ext_paths)))
if len(sup_paths) > 0:
lines.append(' * Supported:')
lines.append(' * - %s' % ('\n * - '.join(sup_paths)))
lines.extend([
' */',