aboutsummaryrefslogtreecommitdiff
path: root/kernel/dtm/dtm.c
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mikisabate@gmail.com>2024-09-12 22:37:29 +0200
committerMiquel Sabaté Solà <mikisabate@gmail.com>2024-12-05 16:02:50 +0100
commit5876d0ade10d53abe4e9c6666c5117ca73a211f6 (patch)
tree533837fc527858e7540c5e064a3acec91f0e27b5 /kernel/dtm/dtm.c
parent432436797bedf396ad698477a19f9d2e13147621 (diff)
downloadfarga-5876d0ade10d53abe4e9c6666c5117ca73a211f6.tar.gz
farga-5876d0ade10d53abe4e9c6666c5117ca73a211f6.zip
kernel/dtm: Add an example on the kernel DT API
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à <mikisabate@gmail.com>
Diffstat (limited to 'kernel/dtm/dtm.c')
-rw-r--r--kernel/dtm/dtm.c114
1 files changed, 114 insertions, 0 deletions
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 <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/of.h>
+
+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);