devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nícolas F. R. A. Prado" <nfraprado@collabora.com>
To: Rob Herring <robh+dt@kernel.org>,
	Frank Rowand <frowand.list@gmail.com>,
	Shuah Khan <shuah@kernel.org>
Cc: cocci@inria.fr, "Mark Brown" <broonie@kernel.org>,
	"Nicolas Palix" <nicolas.palix@imag.fr>,
	kernelci@lists.linux.dev, "Julia Lawall" <Julia.Lawall@inria.fr>,
	"Bjorn Andersson" <andersson@kernel.org>,
	kernel@collabora.com, "Guenter Roeck" <groeck@chromium.org>,
	"Nícolas F. R. A. Prado" <nfraprado@collabora.com>,
	devicetree@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH 1/2] scripts/dtc: Add script to extract matchable DT compatibles
Date: Thu, 10 Aug 2023 16:23:50 -0400	[thread overview]
Message-ID: <20230810202413.1780286-2-nfraprado@collabora.com> (raw)
In-Reply-To: <20230810202413.1780286-1-nfraprado@collabora.com>

Introduce a script to extract all compatibles that can match a device
described on the Devicetree to a driver.

The compatible extraction is done by running Coccinelle with a newly
introduced Coccinelle file that detects compatibles listed inside a
of_device_id table which is referenced by the of_match_table of a
driver struct.

Signed-off-by: Nícolas F. R. A. Prado <nfraprado@collabora.com>

---

 scripts/dtc/extract-matchable-dt-compatibles | 33 +++++++++++
 scripts/dtc/matchable_dt_compatibles.cocci   | 58 ++++++++++++++++++++
 2 files changed, 91 insertions(+)
 create mode 100755 scripts/dtc/extract-matchable-dt-compatibles
 create mode 100644 scripts/dtc/matchable_dt_compatibles.cocci

diff --git a/scripts/dtc/extract-matchable-dt-compatibles b/scripts/dtc/extract-matchable-dt-compatibles
new file mode 100755
index 000000000000..b5e96c7ba42e
--- /dev/null
+++ b/scripts/dtc/extract-matchable-dt-compatibles
@@ -0,0 +1,33 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Copyright (c) 2023 Collabora Ltd
+#
+# Based on coccicheck script.
+#
+# This script uses Coccinelle to extract all compatibles that can be matched by
+# Devicetree devices from the kernel source.
+
+DIR="$(dirname $(readlink -f $0))"
+SPATCH=`which ${SPATCH:=spatch}`
+COCCI_FILE=$DIR/matchable_dt_compatibles.cocci
+
+if [ ! -x "$SPATCH" ]; then
+    echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
+    exit 1
+fi
+
+# Use only one thread per core by default if hyperthreading is enabled
+THREADS_PER_CORE=$(LANG=C lscpu | grep "Thread(s) per core: " | tr -cd "[:digit:]")
+if [ -z "$J" ]; then
+	NPROC=$(getconf _NPROCESSORS_ONLN)
+	if [ $THREADS_PER_CORE -gt 1 -a $NPROC -gt 4 ] ; then
+		NPROC=$((NPROC/2))
+	fi
+else
+	NPROC="$J"
+fi
+
+"$SPATCH" --cocci-file "$COCCI_FILE" --dir .  --jobs "$NPROC" --chunksize 1 \
+	--no-show-diff --no-includes --include-headers --very-quiet \
+	| grep '"' | sed -e 's/"//g;s/^ *\([^ ]*\) *$/\1/'
diff --git a/scripts/dtc/matchable_dt_compatibles.cocci b/scripts/dtc/matchable_dt_compatibles.cocci
new file mode 100644
index 000000000000..ecd3705681aa
--- /dev/null
+++ b/scripts/dtc/matchable_dt_compatibles.cocci
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright: (C) 2023 Collabora Ltd
+@rule1@
+identifier table;
+type drv;
+identifier drvname;
+identifier drvfield;
+identifier drvfield1;
+identifier drvfield2;
+@@
+
+(
+drv drvname = {
+	.drvfield = {
+		.of_match_table = table,
+	},
+};
+|
+drv drvname = {
+	.drvfield = {
+		.of_match_table = of_match_ptr(table),
+	},
+};
+|
+// Accounts for mdio and spimem drivers
+drv drvname = {
+	.drvfield1 = {
+		.drvfield2 = {
+			.of_match_table = table,
+		},
+	},
+};
+|
+drv drvname = {
+	.drvfield1 = {
+		.drvfield2 = {
+			.of_match_table = of_match_ptr(table),
+		},
+	},
+};
+)
+
+@rule2@
+identifier rule1.table;
+expression compat;
+@@
+
+struct of_device_id table[] = {
+...,
+{ .compatible = compat, },
+...
+};
+
+@script:python@
+compat << rule2.compat;
+@@
+
+print("%s" % compat)
-- 
2.41.0


  reply	other threads:[~2023-08-10 20:24 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-10 20:23 [RFC PATCH 0/2] Add a test to catch unprobed Devicetree devices Nícolas F. R. A. Prado
2023-08-10 20:23 ` Nícolas F. R. A. Prado [this message]
2023-08-10 21:43 ` Rob Herring
2023-08-10 21:54   ` Rob Herring
2023-08-11 13:17   ` Nícolas F. R. A. Prado

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230810202413.1780286-2-nfraprado@collabora.com \
    --to=nfraprado@collabora.com \
    --cc=Julia.Lawall@inria.fr \
    --cc=andersson@kernel.org \
    --cc=broonie@kernel.org \
    --cc=cocci@inria.fr \
    --cc=devicetree@vger.kernel.org \
    --cc=frowand.list@gmail.com \
    --cc=groeck@chromium.org \
    --cc=kernel@collabora.com \
    --cc=kernelci@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nicolas.palix@imag.fr \
    --cc=robh+dt@kernel.org \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).