1010#include <linux/errno.h>
1111#include <linux/io.h>
1212#include <linux/module.h>
13+ #include <linux/rpmb.h>
1314#include <linux/slab.h>
1415#include <linux/string.h>
1516#include <linux/tee_core.h>
1617#include <linux/types.h>
1718#include "optee_private.h"
1819
20+ struct blocking_notifier_head optee_rpmb_intf_added =
21+ BLOCKING_NOTIFIER_INIT (optee_rpmb_intf_added );
22+
23+ static int rpmb_add_dev (struct device * dev )
24+ {
25+ blocking_notifier_call_chain (& optee_rpmb_intf_added , 0 ,
26+ to_rpmb_dev (dev ));
27+
28+ return 0 ;
29+ }
30+
31+ static struct class_interface rpmb_class_intf = {
32+ .add_dev = rpmb_add_dev ,
33+ };
34+
35+ void optee_bus_scan_rpmb (struct work_struct * work )
36+ {
37+ struct optee * optee = container_of (work , struct optee ,
38+ rpmb_scan_bus_work );
39+ int ret ;
40+
41+ if (!optee -> rpmb_scan_bus_done ) {
42+ ret = optee_enumerate_devices (PTA_CMD_GET_DEVICES_RPMB );
43+ optee -> rpmb_scan_bus_done = !ret ;
44+ if (ret && ret != - ENODEV )
45+ pr_info ("Scanning for RPMB device: ret %d\n" , ret );
46+ }
47+ }
48+
49+ int optee_rpmb_intf_rdev (struct notifier_block * intf , unsigned long action ,
50+ void * data )
51+ {
52+ struct optee * optee = container_of (intf , struct optee , rpmb_intf );
53+
54+ schedule_work (& optee -> rpmb_scan_bus_work );
55+
56+ return 0 ;
57+ }
58+
1959static void optee_bus_scan (struct work_struct * work )
2060{
2161 WARN_ON (optee_enumerate_devices (PTA_CMD_GET_DEVICES_SUPP ));
2262}
2363
64+ static ssize_t rpmb_routing_model_show (struct device * dev ,
65+ struct device_attribute * attr , char * buf )
66+ {
67+ struct optee * optee = dev_get_drvdata (dev );
68+ const char * s ;
69+
70+ if (optee -> in_kernel_rpmb_routing )
71+ s = "kernel" ;
72+ else
73+ s = "user" ;
74+
75+ return scnprintf (buf , PAGE_SIZE , "%s\n" , s );
76+ }
77+ static DEVICE_ATTR_RO (rpmb_routing_model );
78+
79+ static struct attribute * optee_dev_attrs [] = {
80+ & dev_attr_rpmb_routing_model .attr ,
81+ NULL
82+ };
83+
84+ ATTRIBUTE_GROUPS (optee_dev );
85+
86+ void optee_set_dev_group (struct optee * optee )
87+ {
88+ tee_device_set_dev_groups (optee -> teedev , optee_dev_groups );
89+ tee_device_set_dev_groups (optee -> supp_teedev , optee_dev_groups );
90+ }
91+
2492int optee_open (struct tee_context * ctx , bool cap_memref_null )
2593{
2694 struct optee_context_data * ctxdata ;
@@ -97,6 +165,9 @@ void optee_release_supp(struct tee_context *ctx)
97165
98166void optee_remove_common (struct optee * optee )
99167{
168+ blocking_notifier_chain_unregister (& optee_rpmb_intf_added ,
169+ & optee -> rpmb_intf );
170+ cancel_work_sync (& optee -> rpmb_scan_bus_work );
100171 /* Unregister OP-TEE specific client devices on TEE bus */
101172 optee_unregister_devices ();
102173
@@ -113,13 +184,18 @@ void optee_remove_common(struct optee *optee)
113184 tee_shm_pool_free (optee -> pool );
114185 optee_supp_uninit (& optee -> supp );
115186 mutex_destroy (& optee -> call_queue .mutex );
187+ rpmb_dev_put (optee -> rpmb_dev );
188+ mutex_destroy (& optee -> rpmb_dev_mutex );
116189}
117190
118191static int smc_abi_rc ;
119192static int ffa_abi_rc ;
193+ static bool intf_is_regged ;
120194
121195static int __init optee_core_init (void )
122196{
197+ int rc ;
198+
123199 /*
124200 * The kernel may have crashed at the same time that all available
125201 * secure world threads were suspended and we cannot reschedule the
@@ -130,18 +206,36 @@ static int __init optee_core_init(void)
130206 if (is_kdump_kernel ())
131207 return - ENODEV ;
132208
209+ if (IS_REACHABLE (CONFIG_RPMB )) {
210+ rc = rpmb_interface_register (& rpmb_class_intf );
211+ if (rc )
212+ return rc ;
213+ intf_is_regged = true;
214+ }
215+
133216 smc_abi_rc = optee_smc_abi_register ();
134217 ffa_abi_rc = optee_ffa_abi_register ();
135218
136219 /* If both failed there's no point with this module */
137- if (smc_abi_rc && ffa_abi_rc )
220+ if (smc_abi_rc && ffa_abi_rc ) {
221+ if (IS_REACHABLE (CONFIG_RPMB )) {
222+ rpmb_interface_unregister (& rpmb_class_intf );
223+ intf_is_regged = false;
224+ }
138225 return smc_abi_rc ;
226+ }
227+
139228 return 0 ;
140229}
141230module_init (optee_core_init );
142231
143232static void __exit optee_core_exit (void )
144233{
234+ if (IS_REACHABLE (CONFIG_RPMB ) && intf_is_regged ) {
235+ rpmb_interface_unregister (& rpmb_class_intf );
236+ intf_is_regged = false;
237+ }
238+
145239 if (!smc_abi_rc )
146240 optee_smc_abi_unregister ();
147241 if (!ffa_abi_rc )
0 commit comments