xref: /linux/drivers/misc/mei/pxp/mei_pxp.c (revision 9c93c0b44be36fd5267fb79ae33453f989fbe909)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright © 2020 - 2021 Intel Corporation
4  */
5 
6 /**
7  * DOC: MEI_PXP Client Driver
8  *
9  * The mei_pxp driver acts as a translation layer between PXP
10  * protocol  implementer (I915) and ME FW by translating PXP
11  * negotiation messages to ME FW command payloads and vice versa.
12  */
13 
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/slab.h>
18 #include <linux/mei.h>
19 #include <linux/mei_cl_bus.h>
20 #include <linux/component.h>
21 #include <drm/drm_connector.h>
22 #include <drm/i915_component.h>
23 #include <drm/i915_pxp_tee_interface.h>
24 
25 #include "mei_pxp.h"
26 
27 static inline int mei_pxp_reenable(const struct device *dev, struct mei_cl_device *cldev)
28 {
29 	int ret;
30 
31 	dev_warn(dev, "Trying to reset the channel...\n");
32 	ret = mei_cldev_disable(cldev);
33 	if (ret < 0)
34 		dev_warn(dev, "mei_cldev_disable failed. %d\n", ret);
35 	/*
36 	 * Explicitly ignoring disable failure,
37 	 * enable may fix the states and succeed
38 	 */
39 	ret = mei_cldev_enable(cldev);
40 	if (ret < 0)
41 		dev_err(dev, "mei_cldev_enable failed. %d\n", ret);
42 	return ret;
43 }
44 
45 /**
46  * mei_pxp_send_message() - Sends a PXP message to ME FW.
47  * @dev: device corresponding to the mei_cl_device
48  * @message: a message buffer to send
49  * @size: size of the message
50  * @timeout_ms: timeout in milliseconds, zero means wait indefinitely.
51  *
52  * Returns: 0 on Success, <0 on Failure with the following defined failures.
53  *         -ENODEV: Client was not connected.
54  *                  Caller may attempt to try again immediately.
55  *         -ENOMEM: Internal memory allocation failure experienced.
56  *                  Caller may sleep to allow kernel reclaim before retrying.
57  *         -EINTR : Calling thread received a signal. Caller may choose
58  *                  to abandon with the same thread id.
59  *         -ETIME : Request is timed out.
60  *                  Caller may attempt to try again immediately.
61  */
62 static int
63 mei_pxp_send_message(struct device *dev, const void *message, size_t size, unsigned long timeout_ms)
64 {
65 	struct mei_cl_device *cldev;
66 	ssize_t byte;
67 	int ret;
68 
69 	if (!dev || !message)
70 		return -EINVAL;
71 
72 	cldev = to_mei_cl_device(dev);
73 
74 	byte = mei_cldev_send_timeout(cldev, message, size, timeout_ms);
75 	if (byte < 0) {
76 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
77 		switch (byte) {
78 		case -ENOMEM:
79 			fallthrough;
80 		case -ENODEV:
81 			fallthrough;
82 		case -ETIME:
83 			ret = mei_pxp_reenable(dev, cldev);
84 			if (ret)
85 				byte = ret;
86 			break;
87 		}
88 		return byte;
89 	}
90 
91 	return 0;
92 }
93 
94 /**
95  * mei_pxp_receive_message() - Receives a PXP message from ME FW.
96  * @dev: device corresponding to the mei_cl_device
97  * @buffer: a message buffer to contain the received message
98  * @size: size of the buffer
99  * @timeout_ms: timeout in milliseconds, zero means wait indefinitely.
100  *
101  * Returns: number of bytes send on Success, <0 on Failure with the following defined failures.
102  *         -ENODEV: Client was not connected.
103  *                  Caller may attempt to try again from send immediately.
104  *         -ENOMEM: Internal memory allocation failure experienced.
105  *                  Caller may sleep to allow kernel reclaim before retrying.
106  *         -EINTR : Calling thread received a signal. Caller will need to repeat calling
107  *                  (with a different owning thread) to retrieve existing unclaimed response
108  *                  (and may discard it).
109  *         -ETIME : Request is timed out.
110  *                  Caller may attempt to try again from send immediately.
111  */
112 static int
113 mei_pxp_receive_message(struct device *dev, void *buffer, size_t size, unsigned long timeout_ms)
114 {
115 	struct mei_cl_device *cldev;
116 	ssize_t byte;
117 	bool retry = false;
118 	int ret;
119 
120 	if (!dev || !buffer)
121 		return -EINVAL;
122 
123 	cldev = to_mei_cl_device(dev);
124 
125 retry:
126 	byte = mei_cldev_recv_timeout(cldev, buffer, size, timeout_ms);
127 	if (byte < 0) {
128 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
129 		switch (byte) {
130 		case -ENOMEM:
131 			/* Retry the read when pages are reclaimed */
132 			msleep(20);
133 			if (!retry) {
134 				retry = true;
135 				goto retry;
136 			}
137 			fallthrough;
138 		case -ENODEV:
139 			fallthrough;
140 		case -ETIME:
141 			ret = mei_pxp_reenable(dev, cldev);
142 			if (ret)
143 				byte = ret;
144 			break;
145 		}
146 	}
147 
148 	return byte;
149 }
150 
151 /**
152  * mei_pxp_gsc_command() - sends a gsc command, by sending
153  * a sgl mei message to gsc and receiving reply from gsc
154  *
155  * @dev: device corresponding to the mei_cl_device
156  * @client_id: client id to send the command to
157  * @fence_id: fence id to send the command to
158  * @sg_in: scatter gather list containing addresses for rx message buffer
159  * @total_in_len: total length of data in 'in' sg, can be less than the sum of buffers sizes
160  * @sg_out: scatter gather list containing addresses for tx message buffer
161  *
162  * Return: bytes sent on Success, <0 on Failure
163  */
164 static ssize_t mei_pxp_gsc_command(struct device *dev, u8 client_id, u32 fence_id,
165 				   struct scatterlist *sg_in, size_t total_in_len,
166 				   struct scatterlist *sg_out)
167 {
168 	struct mei_cl_device *cldev;
169 
170 	cldev = to_mei_cl_device(dev);
171 
172 	return mei_cldev_send_gsc_command(cldev, client_id, fence_id, sg_in, total_in_len, sg_out);
173 }
174 
175 static const struct i915_pxp_component_ops mei_pxp_ops = {
176 	.owner = THIS_MODULE,
177 	.send = mei_pxp_send_message,
178 	.recv = mei_pxp_receive_message,
179 	.gsc_command = mei_pxp_gsc_command,
180 };
181 
182 static int mei_component_master_bind(struct device *dev)
183 {
184 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
185 	struct i915_pxp_component *comp_master = mei_cldev_get_drvdata(cldev);
186 	int ret;
187 
188 	comp_master->ops = &mei_pxp_ops;
189 	comp_master->tee_dev = dev;
190 	ret = component_bind_all(dev, comp_master);
191 	if (ret < 0)
192 		return ret;
193 
194 	return 0;
195 }
196 
197 static void mei_component_master_unbind(struct device *dev)
198 {
199 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
200 	struct i915_pxp_component *comp_master = mei_cldev_get_drvdata(cldev);
201 
202 	component_unbind_all(dev, comp_master);
203 }
204 
205 static const struct component_master_ops mei_component_master_ops = {
206 	.bind = mei_component_master_bind,
207 	.unbind = mei_component_master_unbind,
208 };
209 
210 /**
211  * mei_pxp_component_match - compare function for matching mei pxp.
212  *
213  *    The function checks if the driver is i915, the subcomponent is PXP
214  *    and the grand parent of pxp and the parent of i915 are the same
215  *    PCH device.
216  *
217  * @dev: master device
218  * @subcomponent: subcomponent to match (I915_COMPONENT_PXP)
219  * @data: compare data (mei pxp device)
220  *
221  * Return:
222  * * 1 - if components match
223  * * 0 - otherwise
224  */
225 static int mei_pxp_component_match(struct device *dev, int subcomponent,
226 				   void *data)
227 {
228 	struct device *base = data;
229 	struct pci_dev *pdev;
230 
231 	if (!dev)
232 		return 0;
233 
234 	if (!dev_is_pci(dev))
235 		return 0;
236 
237 	pdev = to_pci_dev(dev);
238 
239 	if (pdev->class != (PCI_CLASS_DISPLAY_VGA << 8) ||
240 	    pdev->vendor != PCI_VENDOR_ID_INTEL)
241 		return 0;
242 
243 	if (subcomponent != I915_COMPONENT_PXP)
244 		return 0;
245 
246 	base = base->parent;
247 	if (!base) /* mei device */
248 		return 0;
249 
250 	base = base->parent; /* pci device */
251 	/* for dgfx */
252 	if (base && dev == base)
253 		return 1;
254 
255 	/* for pch */
256 	dev = dev->parent;
257 	return (base && dev && dev == base);
258 }
259 
260 static int mei_pxp_probe(struct mei_cl_device *cldev,
261 			 const struct mei_cl_device_id *id)
262 {
263 	struct i915_pxp_component *comp_master;
264 	struct component_match *master_match;
265 	int ret;
266 
267 	ret = mei_cldev_enable(cldev);
268 	if (ret < 0) {
269 		dev_err(&cldev->dev, "mei_cldev_enable Failed. %d\n", ret);
270 		goto enable_err_exit;
271 	}
272 
273 	comp_master = kzalloc(sizeof(*comp_master), GFP_KERNEL);
274 	if (!comp_master) {
275 		ret = -ENOMEM;
276 		goto err_exit;
277 	}
278 
279 	master_match = NULL;
280 	component_match_add_typed(&cldev->dev, &master_match,
281 				  mei_pxp_component_match, &cldev->dev);
282 	if (IS_ERR_OR_NULL(master_match)) {
283 		ret = -ENOMEM;
284 		goto err_exit;
285 	}
286 
287 	mei_cldev_set_drvdata(cldev, comp_master);
288 	ret = component_master_add_with_match(&cldev->dev,
289 					      &mei_component_master_ops,
290 					      master_match);
291 	if (ret < 0) {
292 		dev_err(&cldev->dev, "Master comp add failed %d\n", ret);
293 		goto err_exit;
294 	}
295 
296 	return 0;
297 
298 err_exit:
299 	mei_cldev_set_drvdata(cldev, NULL);
300 	kfree(comp_master);
301 	mei_cldev_disable(cldev);
302 enable_err_exit:
303 	return ret;
304 }
305 
306 static void mei_pxp_remove(struct mei_cl_device *cldev)
307 {
308 	struct i915_pxp_component *comp_master = mei_cldev_get_drvdata(cldev);
309 	int ret;
310 
311 	component_master_del(&cldev->dev, &mei_component_master_ops);
312 	kfree(comp_master);
313 	mei_cldev_set_drvdata(cldev, NULL);
314 
315 	ret = mei_cldev_disable(cldev);
316 	if (ret)
317 		dev_warn(&cldev->dev, "mei_cldev_disable() failed\n");
318 }
319 
320 /* fbf6fcf1-96cf-4e2e-a6a6-1bab8cbe36b1 : PAVP GUID*/
321 #define MEI_GUID_PXP UUID_LE(0xfbf6fcf1, 0x96cf, 0x4e2e, 0xA6, \
322 			     0xa6, 0x1b, 0xab, 0x8c, 0xbe, 0x36, 0xb1)
323 
324 static struct mei_cl_device_id mei_pxp_tbl[] = {
325 	{ .uuid = MEI_GUID_PXP, .version = MEI_CL_VERSION_ANY },
326 	{ }
327 };
328 MODULE_DEVICE_TABLE(mei, mei_pxp_tbl);
329 
330 static struct mei_cl_driver mei_pxp_driver = {
331 	.id_table = mei_pxp_tbl,
332 	.name = KBUILD_MODNAME,
333 	.probe = mei_pxp_probe,
334 	.remove	= mei_pxp_remove,
335 };
336 
337 module_mei_cl_driver(mei_pxp_driver);
338 
339 MODULE_AUTHOR("Intel Corporation");
340 MODULE_LICENSE("GPL");
341 MODULE_DESCRIPTION("MEI PXP");
342