Skip to content

Commit fe7f2a8

Browse files
kvaneeshctmarinas
authored andcommitted
firmware: smccc: Add an Arm SMCCC bus
SMCCC-discovered firmware services are currently represented by separate platform devices, such as smccc_trng and arm-cca-dev. Those devices do not represent independent DT/ACPI-described platform resources; they are features of the SMCCC firmware interface. Add an Arm SMCCC bus for services discovered through the SMCCC firmware interface. The bus provides SMCCC device and driver registration helpers, function ID based matching, modalias generation, and a sysfs modalias attribute so SMCCC service drivers can bind to discovered firmware services and autoload as modules. Follow-up changes can then register SMCCC firmware services as arm-smccc devices instead of creating independent per-feature platform devices. Based on arm_ffa code Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com> Reviewed-by: Catalin Marinas <catalin.marinas@arm.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: Sudeep Holla <sudeep.holla@kernel.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Lorenzo Pieralisi <lpieralisi@kernel.org> Cc: Sudeep Holla <sudeep.holla@kernel.org> Cc: Nathan Chancellor <nathan@kernel.org> Cc: Nicolas Schier <nsc@kernel.org> Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org> Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent 93f5157 commit fe7f2a8

8 files changed

Lines changed: 240 additions & 1 deletion

File tree

‎MAINTAINERS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24742,7 +24742,9 @@ M: Sudeep Holla <sudeep.holla@kernel.org>
2474224742
L: linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
2474324743
S: Maintained
2474424744
F: drivers/firmware/smccc/
24745+
F: include/linux/arm-smccc-bus.h
2474524746
F: include/linux/arm-smccc.h
24747+
F: include/linux/device-id/arm_smccc.h
2474624748

2474724749
SECURITY CONTACT
2474824750
M: Security Officers <security@kernel.org>

‎drivers/firmware/smccc/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# SPDX-License-Identifier: GPL-2.0
22
#
3-
obj-$(CONFIG_HAVE_ARM_SMCCC_DISCOVERY) += smccc.o kvm_guest.o
3+
obj-$(CONFIG_HAVE_ARM_SMCCC_DISCOVERY) += bus.o smccc.o kvm_guest.o
44
obj-$(CONFIG_ARM_SMCCC_SOC_ID) += soc_id.o

‎drivers/firmware/smccc/bus.c‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2026 Arm Limited
4+
*/
5+
6+
#include <linux/arm-smccc-bus.h>
7+
#include <linux/slab.h>
8+
9+
static int arm_smccc_bus_match(struct device *dev,
10+
const struct device_driver *drv)
11+
{
12+
const struct arm_smccc_device_id *id_table;
13+
const struct arm_smccc_device *smccc_dev = to_arm_smccc_device(dev);
14+
15+
id_table = to_arm_smccc_driver(drv)->id_table;
16+
if (!id_table)
17+
return 0;
18+
19+
while (id_table->func_id) {
20+
if (smccc_dev->func_id == id_table->func_id)
21+
return 1;
22+
id_table++;
23+
}
24+
25+
return 0;
26+
}
27+
28+
static int arm_smccc_bus_probe(struct device *dev)
29+
{
30+
struct arm_smccc_driver *smccc_drv = to_arm_smccc_driver(dev->driver);
31+
32+
return smccc_drv->probe(to_arm_smccc_device(dev));
33+
}
34+
35+
static void arm_smccc_bus_remove(struct device *dev)
36+
{
37+
struct arm_smccc_driver *smcc_drv = to_arm_smccc_driver(dev->driver);
38+
39+
if (smcc_drv->remove)
40+
smcc_drv->remove(to_arm_smccc_device(dev));
41+
}
42+
43+
static int arm_smccc_bus_uevent(const struct device *dev,
44+
struct kobj_uevent_env *env)
45+
{
46+
const struct arm_smccc_device *smccc_dev = to_arm_smccc_device(dev);
47+
48+
return add_uevent_var(env, "MODALIAS=" ARM_SMCCC_MODULE_PREFIX "f%08X",
49+
smccc_dev->func_id);
50+
}
51+
52+
static ssize_t modalias_show(struct device *dev,
53+
struct device_attribute *attr, char *buf)
54+
{
55+
struct arm_smccc_device *smccc_dev = to_arm_smccc_device(dev);
56+
57+
return sysfs_emit(buf, ARM_SMCCC_MODULE_PREFIX "f%08X\n",
58+
smccc_dev->func_id);
59+
}
60+
static DEVICE_ATTR_RO(modalias);
61+
62+
static struct attribute *arm_smccc_device_attrs[] = {
63+
&dev_attr_modalias.attr,
64+
NULL,
65+
};
66+
ATTRIBUTE_GROUPS(arm_smccc_device);
67+
68+
const struct bus_type arm_smccc_bus_type = {
69+
.name = "arm_smccc",
70+
.match = arm_smccc_bus_match,
71+
.probe = arm_smccc_bus_probe,
72+
.remove = arm_smccc_bus_remove,
73+
.uevent = arm_smccc_bus_uevent,
74+
.dev_groups = arm_smccc_device_groups,
75+
};
76+
EXPORT_SYMBOL_GPL(arm_smccc_bus_type);
77+
78+
int arm_smccc_driver_register(struct arm_smccc_driver *driver,
79+
struct module *owner, const char *mod_name)
80+
{
81+
if (!driver->probe || !driver->id_table)
82+
return -EINVAL;
83+
84+
driver->driver.bus = &arm_smccc_bus_type;
85+
driver->driver.name = driver->name;
86+
driver->driver.owner = owner;
87+
driver->driver.mod_name = mod_name;
88+
89+
return driver_register(&driver->driver);
90+
}
91+
EXPORT_SYMBOL_GPL(arm_smccc_driver_register);
92+
93+
void arm_smccc_driver_unregister(struct arm_smccc_driver *driver)
94+
{
95+
driver_unregister(&driver->driver);
96+
}
97+
EXPORT_SYMBOL_GPL(arm_smccc_driver_unregister);
98+
99+
static void arm_smccc_release_device(struct device *dev)
100+
{
101+
struct arm_smccc_device *smccc_dev = to_arm_smccc_device(dev);
102+
103+
kfree(smccc_dev);
104+
}
105+
106+
static struct device *arm_smccc_device_alloc(u32 func_id)
107+
{
108+
struct arm_smccc_device *smccc_dev;
109+
110+
smccc_dev = kzalloc_obj(*smccc_dev);
111+
if (!smccc_dev)
112+
return NULL;
113+
114+
smccc_dev->func_id = func_id;
115+
smccc_dev->dev.bus = &arm_smccc_bus_type;
116+
smccc_dev->dev.release = arm_smccc_release_device;
117+
device_initialize(&smccc_dev->dev);
118+
119+
return &smccc_dev->dev;
120+
}
121+
122+
struct arm_smccc_device *arm_smccc_device_register(const char *name, u32 func_id)
123+
{
124+
int ret;
125+
126+
if (!name)
127+
return ERR_PTR(-EINVAL);
128+
129+
struct device *dev __free(put_device) = arm_smccc_device_alloc(func_id);
130+
if (!dev)
131+
return ERR_PTR(-ENOMEM);
132+
133+
ret = dev_set_name(dev, "%s", name);
134+
if (ret)
135+
return ERR_PTR(ret);
136+
137+
ret = device_add(dev);
138+
if (ret)
139+
return ERR_PTR(ret);
140+
141+
return to_arm_smccc_device(no_free_ptr(dev));
142+
}
143+
EXPORT_SYMBOL_GPL(arm_smccc_device_register);
144+
145+
void arm_smccc_device_unregister(struct arm_smccc_device *smccc_dev)
146+
{
147+
if (!smccc_dev)
148+
return;
149+
150+
device_unregister(&smccc_dev->dev);
151+
}
152+
EXPORT_SYMBOL_GPL(arm_smccc_device_unregister);
153+
154+
static int __init arm_smccc_bus_init(void)
155+
{
156+
return bus_register(&arm_smccc_bus_type);
157+
}
158+
subsys_initcall(arm_smccc_bus_init);

‎include/linux/arm-smccc-bus.h‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (C) 2026 Arm Limited
4+
*/
5+
#ifndef __LINUX_ARM_SMCCC_BUS_H
6+
#define __LINUX_ARM_SMCCC_BUS_H
7+
8+
#include <linux/device.h>
9+
#include <linux/device-id/arm_smccc.h>
10+
#include <linux/module.h>
11+
12+
struct arm_smccc_device {
13+
struct device dev;
14+
u32 func_id;
15+
};
16+
17+
#define to_arm_smccc_device(d) container_of_const(d, struct arm_smccc_device, dev)
18+
19+
struct arm_smccc_driver {
20+
struct device_driver driver;
21+
22+
const char *name;
23+
int (*probe)(struct arm_smccc_device *sdev);
24+
void (*remove)(struct arm_smccc_device *sdev);
25+
const struct arm_smccc_device_id *id_table;
26+
};
27+
28+
#define to_arm_smccc_driver(d) \
29+
container_of_const(d, struct arm_smccc_driver, driver)
30+
31+
int arm_smccc_driver_register(struct arm_smccc_driver *driver,
32+
struct module *owner, const char *mod_name);
33+
void arm_smccc_driver_unregister(struct arm_smccc_driver *driver);
34+
struct arm_smccc_device *arm_smccc_device_register(const char *name, u32 func_id);
35+
void arm_smccc_device_unregister(struct arm_smccc_device *smcc_dev);
36+
37+
#define arm_smccc_register(driver) \
38+
arm_smccc_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
39+
#define arm_smccc_unregister(driver) \
40+
arm_smccc_driver_unregister(driver)
41+
42+
#define module_arm_smccc_driver(__arm_smccc_driver) \
43+
module_driver(__arm_smccc_driver, arm_smccc_register, \
44+
arm_smccc_unregister)
45+
46+
extern const struct bus_type arm_smccc_bus_type;
47+
48+
#endif /* __LINUX_ARM_SMCCC_BUS_H */
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __LINUX_DEVICE_ID_ARM_SMCCC_H
3+
#define __LINUX_DEVICE_ID_ARM_SMCCC_H
4+
5+
#ifdef __KERNEL__
6+
#include <linux/types.h>
7+
#endif
8+
9+
#define ARM_SMCCC_MODULE_PREFIX "arm_smccc:"
10+
11+
/**
12+
* struct arm_smccc_device_id - Arm SMCCC bus device identifier
13+
* @func_id: SMCCC function identifier
14+
*/
15+
struct arm_smccc_device_id {
16+
__u32 func_id;
17+
};
18+
19+
#endif /* __LINUX_DEVICE_ID_ARM_SMCCC_H */

‎include/linux/mod_devicetable.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "device-id/amba.h"
1717
#include "device-id/ap.h"
1818
#include "device-id/apr.h"
19+
#include "device-id/arm_smccc.h"
1920
#include "device-id/auxiliary.h"
2021
#include "device-id/bcma.h"
2122
#include "device-id/ccw.h"

‎scripts/mod/devicetable-offsets.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ int main(void)
254254
DEVID(auxiliary_device_id);
255255
DEVID_FIELD(auxiliary_device_id, name);
256256

257+
DEVID(arm_smccc_device_id);
258+
DEVID_FIELD(arm_smccc_device_id, func_id);
259+
257260
DEVID(ssam_device_id);
258261
DEVID_FIELD(ssam_device_id, match_flags);
259262
DEVID_FIELD(ssam_device_id, domain);

‎scripts/mod/file2alias.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,13 @@ static void do_auxiliary_entry(struct module *mod, void *symval)
13491349
module_alias_printf(mod, false, AUXILIARY_MODULE_PREFIX "%s", *name);
13501350
}
13511351

1352+
static void do_arm_smccc_entry(struct module *mod, void *symval)
1353+
{
1354+
DEF_FIELD(symval, arm_smccc_device_id, func_id);
1355+
1356+
module_alias_printf(mod, false, ARM_SMCCC_MODULE_PREFIX "f%08X", func_id);
1357+
}
1358+
13521359
/*
13531360
* Looks like: ssam:dNcNtNiNfN
13541361
*
@@ -1519,6 +1526,7 @@ static const struct devtable devtable[] = {
15191526
{"mhi", SIZE_mhi_device_id, do_mhi_entry},
15201527
{"mhi_ep", SIZE_mhi_device_id, do_mhi_ep_entry},
15211528
{"auxiliary", SIZE_auxiliary_device_id, do_auxiliary_entry},
1529+
{"arm_smccc", SIZE_arm_smccc_device_id, do_arm_smccc_entry},
15221530
{"ssam", SIZE_ssam_device_id, do_ssam_entry},
15231531
{"dfl", SIZE_dfl_device_id, do_dfl_entry},
15241532
{"ishtp", SIZE_ishtp_device_id, do_ishtp_entry},

0 commit comments

Comments
 (0)