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