@@ -35,7 +35,7 @@ struct meson_sm_chip {
3535 struct meson_sm_cmd cmd [];
3636};
3737
38- struct meson_sm_chip gxbb_chip = {
38+ static const struct meson_sm_chip gxbb_chip = {
3939 .shmem_size = SZ_4K ,
4040 .cmd_shmem_in_base = 0x82000020 ,
4141 .cmd_shmem_out_base = 0x82000021 ,
@@ -54,8 +54,6 @@ struct meson_sm_firmware {
5454 void __iomem * sm_shmem_out_base ;
5555};
5656
57- static struct meson_sm_firmware fw ;
58-
5957static u32 meson_sm_get_cmd (const struct meson_sm_chip * chip ,
6058 unsigned int cmd_index )
6159{
@@ -90,6 +88,7 @@ static void __iomem *meson_sm_map_shmem(u32 cmd_shmem, unsigned int size)
9088/**
9189 * meson_sm_call - generic SMC32 call to the secure-monitor
9290 *
91+ * @fw: Pointer to secure-monitor firmware
9392 * @cmd_index: Index of the SMC32 function ID
9493 * @ret: Returned value
9594 * @arg0: SMC32 Argument 0
@@ -100,15 +99,15 @@ static void __iomem *meson_sm_map_shmem(u32 cmd_shmem, unsigned int size)
10099 *
101100 * Return: 0 on success, a negative value on error
102101 */
103- int meson_sm_call (unsigned int cmd_index , u32 * ret , u32 arg0 ,
104- u32 arg1 , u32 arg2 , u32 arg3 , u32 arg4 )
102+ int meson_sm_call (struct meson_sm_firmware * fw , unsigned int cmd_index ,
103+ u32 * ret , u32 arg0 , u32 arg1 , u32 arg2 , u32 arg3 , u32 arg4 )
105104{
106105 u32 cmd , lret ;
107106
108- if (!fw . chip )
107+ if (!fw -> chip )
109108 return - ENOENT ;
110109
111- cmd = meson_sm_get_cmd (fw . chip , cmd_index );
110+ cmd = meson_sm_get_cmd (fw -> chip , cmd_index );
112111 if (!cmd )
113112 return - EINVAL ;
114113
@@ -124,6 +123,7 @@ EXPORT_SYMBOL(meson_sm_call);
124123/**
125124 * meson_sm_call_read - retrieve data from secure-monitor
126125 *
126+ * @fw: Pointer to secure-monitor firmware
127127 * @buffer: Buffer to store the retrieved data
128128 * @bsize: Size of the buffer
129129 * @cmd_index: Index of the SMC32 function ID
@@ -137,22 +137,23 @@ EXPORT_SYMBOL(meson_sm_call);
137137 * When 0 is returned there is no guarantee about the amount of
138138 * data read and bsize bytes are copied in buffer.
139139 */
140- int meson_sm_call_read (void * buffer , unsigned int bsize , unsigned int cmd_index ,
141- u32 arg0 , u32 arg1 , u32 arg2 , u32 arg3 , u32 arg4 )
140+ int meson_sm_call_read (struct meson_sm_firmware * fw , void * buffer ,
141+ unsigned int bsize , unsigned int cmd_index , u32 arg0 ,
142+ u32 arg1 , u32 arg2 , u32 arg3 , u32 arg4 )
142143{
143144 u32 size ;
144145 int ret ;
145146
146- if (!fw . chip )
147+ if (!fw -> chip )
147148 return - ENOENT ;
148149
149- if (!fw . chip -> cmd_shmem_out_base )
150+ if (!fw -> chip -> cmd_shmem_out_base )
150151 return - EINVAL ;
151152
152- if (bsize > fw . chip -> shmem_size )
153+ if (bsize > fw -> chip -> shmem_size )
153154 return - EINVAL ;
154155
155- if (meson_sm_call (cmd_index , & size , arg0 , arg1 , arg2 , arg3 , arg4 ) < 0 )
156+ if (meson_sm_call (fw , cmd_index , & size , arg0 , arg1 , arg2 , arg3 , arg4 ) < 0 )
156157 return - EINVAL ;
157158
158159 if (size > bsize )
@@ -164,7 +165,7 @@ int meson_sm_call_read(void *buffer, unsigned int bsize, unsigned int cmd_index,
164165 size = bsize ;
165166
166167 if (buffer )
167- memcpy (buffer , fw . sm_shmem_out_base , size );
168+ memcpy (buffer , fw -> sm_shmem_out_base , size );
168169
169170 return ret ;
170171}
@@ -173,6 +174,7 @@ EXPORT_SYMBOL(meson_sm_call_read);
173174/**
174175 * meson_sm_call_write - send data to secure-monitor
175176 *
177+ * @fw: Pointer to secure-monitor firmware
176178 * @buffer: Buffer containing data to send
177179 * @size: Size of the data to send
178180 * @cmd_index: Index of the SMC32 function ID
@@ -184,23 +186,24 @@ EXPORT_SYMBOL(meson_sm_call_read);
184186 *
185187 * Return: size of sent data on success, a negative value on error
186188 */
187- int meson_sm_call_write (void * buffer , unsigned int size , unsigned int cmd_index ,
188- u32 arg0 , u32 arg1 , u32 arg2 , u32 arg3 , u32 arg4 )
189+ int meson_sm_call_write (struct meson_sm_firmware * fw , void * buffer ,
190+ unsigned int size , unsigned int cmd_index , u32 arg0 ,
191+ u32 arg1 , u32 arg2 , u32 arg3 , u32 arg4 )
189192{
190193 u32 written ;
191194
192- if (!fw . chip )
195+ if (!fw -> chip )
193196 return - ENOENT ;
194197
195- if (size > fw . chip -> shmem_size )
198+ if (size > fw -> chip -> shmem_size )
196199 return - EINVAL ;
197200
198- if (!fw . chip -> cmd_shmem_in_base )
201+ if (!fw -> chip -> cmd_shmem_in_base )
199202 return - EINVAL ;
200203
201- memcpy (fw . sm_shmem_in_base , buffer , size );
204+ memcpy (fw -> sm_shmem_in_base , buffer , size );
202205
203- if (meson_sm_call (cmd_index , & written , arg0 , arg1 , arg2 , arg3 , arg4 ) < 0 )
206+ if (meson_sm_call (fw , cmd_index , & written , arg0 , arg1 , arg2 , arg3 , arg4 ) < 0 )
204207 return - EINVAL ;
205208
206209 if (!written )
@@ -210,40 +213,50 @@ int meson_sm_call_write(void *buffer, unsigned int size, unsigned int cmd_index,
210213}
211214EXPORT_SYMBOL (meson_sm_call_write );
212215
216+ /**
217+ * meson_sm_get - get pointer to meson_sm_firmware structure.
218+ *
219+ * @sm_node: Pointer to the secure-monitor Device Tree node.
220+ *
221+ * Return: NULL is the secure-monitor device is not ready.
222+ */
223+ struct meson_sm_firmware * meson_sm_get (struct device_node * sm_node )
224+ {
225+ struct platform_device * pdev = of_find_device_by_node (sm_node );
226+
227+ if (!pdev )
228+ return NULL ;
229+
230+ return platform_get_drvdata (pdev );
231+ }
232+ EXPORT_SYMBOL_GPL (meson_sm_get );
233+
213234#define SM_CHIP_ID_LENGTH 119
214235#define SM_CHIP_ID_OFFSET 4
215236#define SM_CHIP_ID_SIZE 12
216237
217238static ssize_t serial_show (struct device * dev , struct device_attribute * attr ,
218239 char * buf )
219240{
241+ struct platform_device * pdev = to_platform_device (dev );
242+ struct meson_sm_firmware * fw ;
220243 uint8_t * id_buf ;
221244 int ret ;
222245
246+ fw = platform_get_drvdata (pdev );
247+
223248 id_buf = kmalloc (SM_CHIP_ID_LENGTH , GFP_KERNEL );
224249 if (!id_buf )
225250 return - ENOMEM ;
226251
227- ret = meson_sm_call_read (id_buf , SM_CHIP_ID_LENGTH , SM_GET_CHIP_ID ,
252+ ret = meson_sm_call_read (fw , id_buf , SM_CHIP_ID_LENGTH , SM_GET_CHIP_ID ,
228253 0 , 0 , 0 , 0 , 0 );
229254 if (ret < 0 ) {
230255 kfree (id_buf );
231256 return ret ;
232257 }
233258
234- ret = sprintf (buf , "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n" ,
235- id_buf [SM_CHIP_ID_OFFSET + 0 ],
236- id_buf [SM_CHIP_ID_OFFSET + 1 ],
237- id_buf [SM_CHIP_ID_OFFSET + 2 ],
238- id_buf [SM_CHIP_ID_OFFSET + 3 ],
239- id_buf [SM_CHIP_ID_OFFSET + 4 ],
240- id_buf [SM_CHIP_ID_OFFSET + 5 ],
241- id_buf [SM_CHIP_ID_OFFSET + 6 ],
242- id_buf [SM_CHIP_ID_OFFSET + 7 ],
243- id_buf [SM_CHIP_ID_OFFSET + 8 ],
244- id_buf [SM_CHIP_ID_OFFSET + 9 ],
245- id_buf [SM_CHIP_ID_OFFSET + 10 ],
246- id_buf [SM_CHIP_ID_OFFSET + 11 ]);
259+ ret = sprintf (buf , "%12phN\n" , & id_buf [SM_CHIP_ID_OFFSET ]);
247260
248261 kfree (id_buf );
249262
@@ -268,25 +281,34 @@ static const struct of_device_id meson_sm_ids[] = {
268281
269282static int __init meson_sm_probe (struct platform_device * pdev )
270283{
284+ struct device * dev = & pdev -> dev ;
271285 const struct meson_sm_chip * chip ;
286+ struct meson_sm_firmware * fw ;
287+
288+ fw = devm_kzalloc (dev , sizeof (* fw ), GFP_KERNEL );
289+ if (!fw )
290+ return - ENOMEM ;
272291
273- chip = of_match_device (meson_sm_ids , & pdev -> dev )-> data ;
292+ chip = of_match_device (meson_sm_ids , dev )-> data ;
274293
275294 if (chip -> cmd_shmem_in_base ) {
276- fw . sm_shmem_in_base = meson_sm_map_shmem (chip -> cmd_shmem_in_base ,
277- chip -> shmem_size );
278- if (WARN_ON (!fw . sm_shmem_in_base ))
295+ fw -> sm_shmem_in_base = meson_sm_map_shmem (chip -> cmd_shmem_in_base ,
296+ chip -> shmem_size );
297+ if (WARN_ON (!fw -> sm_shmem_in_base ))
279298 goto out ;
280299 }
281300
282301 if (chip -> cmd_shmem_out_base ) {
283- fw . sm_shmem_out_base = meson_sm_map_shmem (chip -> cmd_shmem_out_base ,
284- chip -> shmem_size );
285- if (WARN_ON (!fw . sm_shmem_out_base ))
302+ fw -> sm_shmem_out_base = meson_sm_map_shmem (chip -> cmd_shmem_out_base ,
303+ chip -> shmem_size );
304+ if (WARN_ON (!fw -> sm_shmem_out_base ))
286305 goto out_in_base ;
287306 }
288307
289- fw .chip = chip ;
308+ fw -> chip = chip ;
309+
310+ platform_set_drvdata (pdev , fw );
311+
290312 pr_info ("secure-monitor enabled\n" );
291313
292314 if (sysfs_create_group (& pdev -> dev .kobj , & meson_sm_sysfs_attr_group ))
@@ -295,7 +317,7 @@ static int __init meson_sm_probe(struct platform_device *pdev)
295317 return 0 ;
296318
297319out_in_base :
298- iounmap (fw . sm_shmem_in_base );
320+ iounmap (fw -> sm_shmem_in_base );
299321out :
300322 return - EINVAL ;
301323}
0 commit comments