xref: /linux/drivers/char/tpm/tpm_crb_ffa.c (revision 677042afb97ac5057e1d2900139f123bb15ba6e6)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2024 Arm Ltd.
4  *
5  * This device driver implements the TPM CRB start method
6  * as defined in the TPM Service Command Response Buffer
7  * Interface Over FF-A (DEN0138).
8  */
9 
10 #define pr_fmt(fmt) "CRB_FFA: " fmt
11 
12 #include <linux/arm_ffa.h>
13 #include <linux/delay.h>
14 #include <linux/moduleparam.h>
15 #include "tpm_crb_ffa.h"
16 
17 static unsigned int busy_timeout_ms = 2000;
18 
19 module_param(busy_timeout_ms, uint, 0644);
20 MODULE_PARM_DESC(busy_timeout_ms,
21 		 "Maximum time in ms to retry before giving up on busy");
22 
23 /* TPM service function status codes */
24 #define CRB_FFA_OK			0x05000001
25 #define CRB_FFA_OK_RESULTS_RETURNED	0x05000002
26 #define CRB_FFA_NOFUNC			0x8e000001
27 #define CRB_FFA_NOTSUP			0x8e000002
28 #define CRB_FFA_INVARG			0x8e000005
29 #define CRB_FFA_INV_CRB_CTRL_DATA	0x8e000006
30 #define CRB_FFA_ALREADY			0x8e000009
31 #define CRB_FFA_DENIED			0x8e00000a
32 #define CRB_FFA_NOMEM			0x8e00000b
33 
34 #define CRB_FFA_VERSION_MAJOR	1
35 #define CRB_FFA_VERSION_MINOR	0
36 
37 /* version encoding */
38 #define CRB_FFA_MAJOR_VERSION_MASK  GENMASK(30, 16)
39 #define CRB_FFA_MINOR_VERSION_MASK  GENMASK(15, 0)
40 #define CRB_FFA_MAJOR_VERSION(x)    ((u16)(FIELD_GET(CRB_FFA_MAJOR_VERSION_MASK, (x))))
41 #define CRB_FFA_MINOR_VERSION(x)    ((u16)(FIELD_GET(CRB_FFA_MINOR_VERSION_MASK, (x))))
42 
43 /*
44  * Normal world sends requests with FFA_MSG_SEND_DIRECT_REQ and
45  * responses are returned with FFA_MSG_SEND_DIRECT_RESP for normal
46  * messages.
47  *
48  * All requests with FFA_MSG_SEND_DIRECT_REQ and FFA_MSG_SEND_DIRECT_RESP
49  * are using the AArch32 or AArch64 SMC calling convention with register usage
50  * as defined in FF-A specification:
51  * w0:    Function ID
52  *          -for 32-bit: 0x8400006F or 0x84000070
53  *          -for 64-bit: 0xC400006F or 0xC4000070
54  * w1:    Source/Destination IDs
55  * w2:    Reserved (MBZ)
56  * w3-w7: Implementation defined, free to be used below
57  */
58 
59 /*
60  * Returns the version of the interface that is available
61  * Call register usage:
62  * w3:    Not used (MBZ)
63  * w4:    TPM service function ID, CRB_FFA_GET_INTERFACE_VERSION
64  * w5-w7: Reserved (MBZ)
65  *
66  * Return register usage:
67  * w3:    Not used (MBZ)
68  * w4:    TPM service function status
69  * w5:    TPM service interface version
70  *        Bits[31:16]: major version
71  *        Bits[15:0]: minor version
72  * w6-w7: Reserved (MBZ)
73  *
74  * Possible function status codes in register w4:
75  *     CRB_FFA_OK_RESULTS_RETURNED: The version of the interface has been
76  *                                  returned.
77  */
78 #define CRB_FFA_GET_INTERFACE_VERSION 0x0f000001
79 
80 /*
81  * Notifies the TPM service that a TPM command or TPM locality request is
82  * ready to be processed, and allows the TPM service to process it.
83  * Call register usage:
84  * w3:    Not used (MBZ)
85  * w4:    TPM service function ID, CRB_FFA_START
86  * w5:    Start function qualifier
87  *            Bits[31:8] (MBZ)
88  *            Bits[7:0]
89  *              0: Notifies TPM that a command is ready to be processed
90  *              1: Notifies TPM that a locality request is ready to be processed
91  * w6:    TPM locality, one of 0..4
92  *            -If the start function qualifier is 0, identifies the locality
93  *             from where the command originated.
94  *            -If the start function qualifier is 1, identifies the locality
95  *             of the locality request
96  * w6-w7: Reserved (MBZ)
97  *
98  * Return register usage:
99  * w3:    Not used (MBZ)
100  * w4:    TPM service function status
101  * w5-w7: Reserved (MBZ)
102  *
103  * Possible function status codes in register w4:
104  *     CRB_FFA_OK: the TPM service has been notified successfully
105  *     CRB_FFA_INVARG: one or more arguments are not valid
106  *     CRB_FFA_INV_CRB_CTRL_DATA: CRB control data or locality control
107  *         data at the given TPM locality is not valid
108  *     CRB_FFA_DENIED: the TPM has previously disabled locality requests and
109  *         command processing at the given locality
110  */
111 #define CRB_FFA_START 0x0f000201
112 
113 struct tpm_crb_ffa {
114 	struct ffa_device *ffa_dev;
115 	u16 major_version;
116 	u16 minor_version;
117 	/* lock to protect sending of FF-A messages: */
118 	struct mutex msg_data_lock;
119 	union {
120 		struct ffa_send_direct_data direct_msg_data;
121 		struct ffa_send_direct_data2 direct_msg_data2;
122 	};
123 };
124 
125 static struct tpm_crb_ffa *tpm_crb_ffa;
126 static struct ffa_driver tpm_crb_ffa_driver;
127 
128 static int tpm_crb_ffa_to_linux_errno(int errno)
129 {
130 	int rc;
131 
132 	switch (errno) {
133 	case CRB_FFA_OK:
134 		rc = 0;
135 		break;
136 	case CRB_FFA_OK_RESULTS_RETURNED:
137 		rc = 0;
138 		break;
139 	case CRB_FFA_NOFUNC:
140 		rc = -ENOENT;
141 		break;
142 	case CRB_FFA_NOTSUP:
143 		rc = -EPERM;
144 		break;
145 	case CRB_FFA_INVARG:
146 		rc = -EINVAL;
147 		break;
148 	case CRB_FFA_INV_CRB_CTRL_DATA:
149 		rc = -ENOEXEC;
150 		break;
151 	case CRB_FFA_ALREADY:
152 		rc = -EEXIST;
153 		break;
154 	case CRB_FFA_DENIED:
155 		rc = -EACCES;
156 		break;
157 	case CRB_FFA_NOMEM:
158 		rc = -ENOMEM;
159 		break;
160 	default:
161 		rc = -EINVAL;
162 	}
163 
164 	return rc;
165 }
166 
167 /**
168  * tpm_crb_ffa_init - called by the CRB driver to do any needed initialization
169  *
170  * This function is called by the tpm_crb driver during the tpm_crb
171  * driver's initialization. If the tpm_crb_ffa has not been probed
172  * yet, returns -ENOENT in order to force a retry.  If th ffa_crb
173  * driver had been probed  but failed with an error, returns -ENODEV
174  * in order to prevent further retries.
175  *
176  * Return: 0 on success, negative error code on failure.
177  */
178 int tpm_crb_ffa_init(void)
179 {
180 	if (!tpm_crb_ffa)
181 		return -ENOENT;
182 
183 	if (IS_ERR_VALUE(tpm_crb_ffa))
184 		return -ENODEV;
185 
186 	return 0;
187 }
188 EXPORT_SYMBOL_GPL(tpm_crb_ffa_init);
189 
190 static int __tpm_crb_ffa_try_send_receive(unsigned long func_id,
191 					  unsigned long a0, unsigned long a1,
192 					  unsigned long a2)
193 {
194 	const struct ffa_msg_ops *msg_ops;
195 	int ret;
196 
197 	msg_ops = tpm_crb_ffa->ffa_dev->ops->msg_ops;
198 
199 	if (ffa_partition_supports_direct_req2_recv(tpm_crb_ffa->ffa_dev)) {
200 		tpm_crb_ffa->direct_msg_data2 = (struct ffa_send_direct_data2){
201 			.data = { func_id, a0, a1, a2 },
202 		};
203 
204 		ret = msg_ops->sync_send_receive2(tpm_crb_ffa->ffa_dev,
205 				&tpm_crb_ffa->direct_msg_data2);
206 		if (!ret)
207 			ret = tpm_crb_ffa_to_linux_errno(tpm_crb_ffa->direct_msg_data2.data[0]);
208 	} else {
209 		tpm_crb_ffa->direct_msg_data = (struct ffa_send_direct_data){
210 			.data1 = func_id,
211 			.data2 = a0,
212 			.data3 = a1,
213 			.data4 = a2,
214 		};
215 
216 		ret = msg_ops->sync_send_receive(tpm_crb_ffa->ffa_dev,
217 				&tpm_crb_ffa->direct_msg_data);
218 		if (!ret)
219 			ret = tpm_crb_ffa_to_linux_errno(tpm_crb_ffa->direct_msg_data.data1);
220 	}
221 
222 	return ret;
223 }
224 
225 static int __tpm_crb_ffa_send_receive(unsigned long func_id, unsigned long a0,
226 				      unsigned long a1, unsigned long a2)
227 {
228 	ktime_t start, stop;
229 	int ret;
230 
231 	if (!tpm_crb_ffa)
232 		return -ENOENT;
233 
234 	start = ktime_get();
235 	stop = ktime_add(start, ms_to_ktime(busy_timeout_ms));
236 
237 	for (;;) {
238 		ret = __tpm_crb_ffa_try_send_receive(func_id, a0, a1, a2);
239 		if (ret != -EBUSY)
240 			break;
241 
242 		usleep_range(50, 100);
243 		if (ktime_after(ktime_get(), stop)) {
244 			dev_warn(&tpm_crb_ffa->ffa_dev->dev,
245 				 "Busy retry timed out\n");
246 			break;
247 		}
248 	}
249 
250 	return ret;
251 }
252 
253 /**
254  * tpm_crb_ffa_get_interface_version() - gets the ABI version of the TPM service
255  * @major: Pointer to caller-allocated buffer to hold the major version
256  *         number the ABI
257  * @minor: Pointer to caller-allocated buffer to hold the minor version
258  *         number the ABI
259  *
260  * Returns the major and minor version of the ABI of the FF-A based TPM.
261  * Allows the caller to evaluate its compatibility with the version of
262  * the ABI.
263  *
264  * Return: 0 on success, negative error code on failure.
265  */
266 static int tpm_crb_ffa_get_interface_version(u16 *major, u16 *minor)
267 {
268 	int rc;
269 
270 	if (!tpm_crb_ffa)
271 		return -ENOENT;
272 
273 	if (IS_ERR_VALUE(tpm_crb_ffa))
274 		return -ENODEV;
275 
276 	if (!major || !minor)
277 		return -EINVAL;
278 
279 	guard(mutex)(&tpm_crb_ffa->msg_data_lock);
280 
281 	rc = __tpm_crb_ffa_send_receive(CRB_FFA_GET_INTERFACE_VERSION, 0x00, 0x00, 0x00);
282 	if (!rc) {
283 		if (ffa_partition_supports_direct_req2_recv(tpm_crb_ffa->ffa_dev)) {
284 			*major = CRB_FFA_MAJOR_VERSION(tpm_crb_ffa->direct_msg_data2.data[1]);
285 			*minor = CRB_FFA_MINOR_VERSION(tpm_crb_ffa->direct_msg_data2.data[1]);
286 		} else {
287 			*major = CRB_FFA_MAJOR_VERSION(tpm_crb_ffa->direct_msg_data.data2);
288 			*minor = CRB_FFA_MINOR_VERSION(tpm_crb_ffa->direct_msg_data.data2);
289 		}
290 	}
291 
292 	return rc;
293 }
294 
295 /**
296  * tpm_crb_ffa_start() - signals the TPM that a field has changed in the CRB
297  * @request_type: Identifies whether the change to the CRB is in the command
298  *                fields or locality fields.
299  * @locality: Specifies the locality number.
300  *
301  * Used by the CRB driver
302  * that might be useful to those using or modifying it.  Begins with
303  * empty comment line, and may include additional embedded empty
304  * comment lines.
305  *
306  * Return: 0 on success, negative error code on failure.
307  */
308 int tpm_crb_ffa_start(int request_type, int locality)
309 {
310 	if (!tpm_crb_ffa)
311 		return -ENOENT;
312 
313 	if (IS_ERR_VALUE(tpm_crb_ffa))
314 		return -ENODEV;
315 
316 	guard(mutex)(&tpm_crb_ffa->msg_data_lock);
317 
318 	return __tpm_crb_ffa_send_receive(CRB_FFA_START, request_type, locality, 0x00);
319 }
320 EXPORT_SYMBOL_GPL(tpm_crb_ffa_start);
321 
322 static int tpm_crb_ffa_probe(struct ffa_device *ffa_dev)
323 {
324 	struct tpm_crb_ffa *p;
325 	int rc;
326 
327 	/* only one instance of a TPM partition is supported */
328 	if (tpm_crb_ffa && !IS_ERR_VALUE(tpm_crb_ffa))
329 		return -EEXIST;
330 
331 	tpm_crb_ffa = ERR_PTR(-ENODEV); // set tpm_crb_ffa so we can detect probe failure
332 
333 	if (!ffa_partition_supports_direct_recv(ffa_dev) &&
334 	    !ffa_partition_supports_direct_req2_recv(ffa_dev)) {
335 		dev_warn(&ffa_dev->dev, "partition doesn't support direct message receive.\n");
336 		return -EINVAL;
337 	}
338 
339 	p = kzalloc_obj(*tpm_crb_ffa);
340 	if (!p)
341 		return -ENOMEM;
342 	tpm_crb_ffa = p;
343 
344 	mutex_init(&tpm_crb_ffa->msg_data_lock);
345 	tpm_crb_ffa->ffa_dev = ffa_dev;
346 	ffa_dev_set_drvdata(ffa_dev, tpm_crb_ffa);
347 
348 	/* if TPM is aarch32 use 32-bit SMCs */
349 	if (!ffa_partition_check_property(ffa_dev, FFA_PARTITION_AARCH64_EXEC))
350 		ffa_dev->ops->msg_ops->mode_32bit_set(ffa_dev);
351 
352 	/* verify compatibility of TPM service version number */
353 	rc = tpm_crb_ffa_get_interface_version(&tpm_crb_ffa->major_version,
354 					       &tpm_crb_ffa->minor_version);
355 	if (rc) {
356 		dev_err(&ffa_dev->dev, "failed to get crb interface version. rc:%d\n", rc);
357 		goto out;
358 	}
359 
360 	dev_info(&ffa_dev->dev, "ABI version %u.%u\n", tpm_crb_ffa->major_version,
361 		tpm_crb_ffa->minor_version);
362 
363 	if (tpm_crb_ffa->major_version != CRB_FFA_VERSION_MAJOR ||
364 	    (tpm_crb_ffa->minor_version > 0 &&
365 	    tpm_crb_ffa->minor_version < CRB_FFA_VERSION_MINOR)) {
366 		dev_warn(&ffa_dev->dev, "Incompatible ABI version\n");
367 		goto out;
368 	}
369 
370 	return 0;
371 
372 out:
373 	kfree(tpm_crb_ffa);
374 	tpm_crb_ffa = ERR_PTR(-ENODEV);
375 	return -EINVAL;
376 }
377 
378 static void tpm_crb_ffa_remove(struct ffa_device *ffa_dev)
379 {
380 	kfree(tpm_crb_ffa);
381 	tpm_crb_ffa = NULL;
382 }
383 
384 static const struct ffa_device_id tpm_crb_ffa_device_id[] = {
385 	/* 17b862a4-1806-4faf-86b3-089a58353861 */
386 	{ UUID_INIT(0x17b862a4, 0x1806, 0x4faf,
387 		    0x86, 0xb3, 0x08, 0x9a, 0x58, 0x35, 0x38, 0x61) },
388 	{}
389 };
390 
391 static struct ffa_driver tpm_crb_ffa_driver = {
392 	.name = "ffa-crb",
393 	.probe = tpm_crb_ffa_probe,
394 	.remove = tpm_crb_ffa_remove,
395 	.id_table = tpm_crb_ffa_device_id,
396 };
397 
398 module_ffa_driver(tpm_crb_ffa_driver);
399 
400 MODULE_AUTHOR("Arm");
401 MODULE_DESCRIPTION("TPM CRB FFA driver");
402 MODULE_LICENSE("GPL");
403