From 5876d0ade10d53abe4e9c6666c5117ca73a211f6 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Thu, 12 Sep 2024 22:37:29 +0200 Subject: kernel/dtm: Add an example on the kernel DT API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This comes in the form of a simple kernel module which iterates over CPUs and prints the contents of some device tree nodes. Signed-off-by: Miquel Sabaté Solà --- kernel/dtm/dtm.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 kernel/dtm/dtm.c (limited to 'kernel/dtm/dtm.c') diff --git a/kernel/dtm/dtm.c b/kernel/dtm/dtm.c new file mode 100644 index 0000000..adda89a --- /dev/null +++ b/kernel/dtm/dtm.c @@ -0,0 +1,114 @@ +#include +#include +#include +#include + +MODULE_DESCRIPTION("Kernel module to show off the DeviceTree Kernel API"); +MODULE_AUTHOR("Miquel Sabaté Solà"); +MODULE_LICENSE("GPL"); +MODULE_VERSION("0.0.1"); + +// `compatible` value for QEMU/KVM. +static const char *const qemu[] = { + "riscv-virtio", + NULL, +}; + +// `compatible` values that will match on Starfive VisionFive2 v1.3b (plus a +// boogie value). +static const char *const vf2[] = { + "starfive,visionfive-2-v1.3b", + "starfive,jh7110", + "this is bullshit but it won't interfere", + NULL, +}; + +static int __init dtm_init(void) +{ + unsigned int cpu; + struct device_node *node, *parent; + const char *isa; + int rc; + + /* + * `of_compatible_match` returns true if there is _one_ element of the given + * array which matches a string on the `compatible` file. That is, not all + * of them have to match. + */ + if (of_machine_compatible_match(qemu)) { + pr_info("Inside of QEMU\n"); + } else if (of_machine_compatible_match(vf2)) { + pr_info("Inside of VisionFive2\n"); + } else { + pr_info("Somewhere over the rainbow\n"); + } + + /* + * The `for_each_possible_cpu` is a function-like macro provided by the + * Kernel which allows `cpu` to be an iterator over CPU device nodes. + */ + for_each_possible_cpu(cpu) { + /* isa = NULL; */ + + /* + * Given a CPU identifier, `of_cpu_device_node_get` returns a `struct + * device_node` related to the specified CPU (or NULL on error). + */ + node = of_cpu_device_node_get(cpu); + if (!node) { + pr_warn("Unable to find cpu node\n"); + continue; + } + + /* + * `of_property_present` returns a boolean on whether the given node + * property is present for the given device node. + */ + if (!of_property_present(node, "riscv,isa")) { + pr_warn("Unable to find 'riscv,isa' for this node\n"); + continue; + } + + /* + * `of_property_read_string` initializes the given `isa` string with the + * contents of the property we are trying to fetch. Returns 0 on + * success, and a kernel error otherwise. Note that the previous + * `of_property_present` call was actually not needed, we would've get + * here an `-EINVAL` error eitherway. + */ + rc = of_property_read_string(node, "riscv,isa", &isa); + if (rc) { + pr_warn("Unable to find \"riscv,isa\" devicetree entry\n"); + continue; + } + pr_info("RISC-V ISA for '%s': %s\n", node->full_name, isa); + + /* + * `of_get_parent` returns the parent device node for the one provided + * or NULL if there's none. + */ + parent = of_get_parent(node); + if (!node) { + pr_warn("No parent for the given node!\n"); + continue; + } + pr_info("parent: %s\n", node->full_name); + + /* + * `of_node_put` is used to decrement the reference counter on device + * nodes. We are done using these instances, so we should decrement + * their life times. + */ + of_node_put(node); + of_node_put(parent); + } + return 0; +} + +static void __exit dtm_exit(void) +{ + pr_info("Exiting from DTM module\n"); +} + +module_init(dtm_init); +module_exit(dtm_exit); -- cgit v1.2.3