xref: /openbmc/linux/drivers/misc/mei/pxp/mei_pxp.c (revision ecefa105)
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/module.h>
15 #include <linux/slab.h>
16 #include <linux/uuid.h>
17 #include <linux/mei_cl_bus.h>
18 #include <linux/component.h>
19 #include <drm/drm_connector.h>
20 #include <drm/i915_component.h>
21 #include <drm/i915_pxp_tee_interface.h>
22 
23 #include "mei_pxp.h"
24 
25 /**
26  * mei_pxp_send_message() - Sends a PXP message to ME FW.
27  * @dev: device corresponding to the mei_cl_device
28  * @message: a message buffer to send
29  * @size: size of the message
30  * Return: 0 on Success, <0 on Failure
31  */
32 static int
33 mei_pxp_send_message(struct device *dev, const void *message, size_t size)
34 {
35 	struct mei_cl_device *cldev;
36 	ssize_t byte;
37 
38 	if (!dev || !message)
39 		return -EINVAL;
40 
41 	cldev = to_mei_cl_device(dev);
42 
43 	/* temporary drop const qualifier till the API is fixed */
44 	byte = mei_cldev_send(cldev, (u8 *)message, size);
45 	if (byte < 0) {
46 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
47 		return byte;
48 	}
49 
50 	return 0;
51 }
52 
53 /**
54  * mei_pxp_receive_message() - Receives a PXP message from ME FW.
55  * @dev: device corresponding to the mei_cl_device
56  * @buffer: a message buffer to contain the received message
57  * @size: size of the buffer
58  * Return: bytes sent on Success, <0 on Failure
59  */
60 static int
61 mei_pxp_receive_message(struct device *dev, void *buffer, size_t size)
62 {
63 	struct mei_cl_device *cldev;
64 	ssize_t byte;
65 
66 	if (!dev || !buffer)
67 		return -EINVAL;
68 
69 	cldev = to_mei_cl_device(dev);
70 
71 	byte = mei_cldev_recv(cldev, buffer, size);
72 	if (byte < 0) {
73 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
74 		return byte;
75 	}
76 
77 	return byte;
78 }
79 
80 /**
81  * mei_pxp_gsc_command() - sends a gsc command, by sending
82  * a sgl mei message to gsc and receiving reply from gsc
83  *
84  * @dev: device corresponding to the mei_cl_device
85  * @client_id: client id to send the command to
86  * @fence_id: fence id to send the command to
87  * @sg_in: scatter gather list containing addresses for rx message buffer
88  * @total_in_len: total length of data in 'in' sg, can be less than the sum of buffers sizes
89  * @sg_out: scatter gather list containing addresses for tx message buffer
90  *
91  * Return: bytes sent on Success, <0 on Failure
92  */
93 static ssize_t mei_pxp_gsc_command(struct device *dev, u8 client_id, u32 fence_id,
94 				   struct scatterlist *sg_in, size_t total_in_len,
95 				   struct scatterlist *sg_out)
96 {
97 	struct mei_cl_device *cldev;
98 
99 	cldev = to_mei_cl_device(dev);
100 
101 	return mei_cldev_send_gsc_command(cldev, client_id, fence_id, sg_in, total_in_len, sg_out);
102 }
103 
104 static const struct i915_pxp_component_ops mei_pxp_ops = {
105 	.owner = THIS_MODULE,
106 	.send = mei_pxp_send_message,
107 	.recv = mei_pxp_receive_message,
108 	.gsc_command = mei_pxp_gsc_command,
109 };
110 
111 static int mei_component_master_bind(struct device *dev)
112 {
113 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
114 	struct i915_pxp_component *comp_master = mei_cldev_get_drvdata(cldev);
115 	int ret;
116 
117 	comp_master->ops = &mei_pxp_ops;
118 	comp_master->tee_dev = dev;
119 	ret = component_bind_all(dev, comp_master);
120 	if (ret < 0)
121 		return ret;
122 
123 	return 0;
124 }
125 
126 static void mei_component_master_unbind(struct device *dev)
127 {
128 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
129 	struct i915_pxp_component *comp_master = mei_cldev_get_drvdata(cldev);
130 
131 	component_unbind_all(dev, comp_master);
132 }
133 
134 static const struct component_master_ops mei_component_master_ops = {
135 	.bind = mei_component_master_bind,
136 	.unbind = mei_component_master_unbind,
137 };
138 
139 /**
140  * mei_pxp_component_match - compare function for matching mei pxp.
141  *
142  *    The function checks if the driver is i915, the subcomponent is PXP
143  *    and the grand parent of pxp and the parent of i915 are the same
144  *    PCH device.
145  *
146  * @dev: master device
147  * @subcomponent: subcomponent to match (I915_COMPONENT_PXP)
148  * @data: compare data (mei pxp device)
149  *
150  * Return:
151  * * 1 - if components match
152  * * 0 - otherwise
153  */
154 static int mei_pxp_component_match(struct device *dev, int subcomponent,
155 				   void *data)
156 {
157 	struct device *base = data;
158 
159 	if (!dev)
160 		return 0;
161 
162 	if (!dev->driver || strcmp(dev->driver->name, "i915") ||
163 	    subcomponent != I915_COMPONENT_PXP)
164 		return 0;
165 
166 	base = base->parent;
167 	if (!base) /* mei device */
168 		return 0;
169 
170 	base = base->parent; /* pci device */
171 	/* for dgfx */
172 	if (base && dev == base)
173 		return 1;
174 
175 	/* for pch */
176 	dev = dev->parent;
177 	return (base && dev && dev == base);
178 }
179 
180 static int mei_pxp_probe(struct mei_cl_device *cldev,
181 			 const struct mei_cl_device_id *id)
182 {
183 	struct i915_pxp_component *comp_master;
184 	struct component_match *master_match;
185 	int ret;
186 
187 	ret = mei_cldev_enable(cldev);
188 	if (ret < 0) {
189 		dev_err(&cldev->dev, "mei_cldev_enable Failed. %d\n", ret);
190 		goto enable_err_exit;
191 	}
192 
193 	comp_master = kzalloc(sizeof(*comp_master), GFP_KERNEL);
194 	if (!comp_master) {
195 		ret = -ENOMEM;
196 		goto err_exit;
197 	}
198 
199 	master_match = NULL;
200 	component_match_add_typed(&cldev->dev, &master_match,
201 				  mei_pxp_component_match, &cldev->dev);
202 	if (IS_ERR_OR_NULL(master_match)) {
203 		ret = -ENOMEM;
204 		goto err_exit;
205 	}
206 
207 	mei_cldev_set_drvdata(cldev, comp_master);
208 	ret = component_master_add_with_match(&cldev->dev,
209 					      &mei_component_master_ops,
210 					      master_match);
211 	if (ret < 0) {
212 		dev_err(&cldev->dev, "Master comp add failed %d\n", ret);
213 		goto err_exit;
214 	}
215 
216 	return 0;
217 
218 err_exit:
219 	mei_cldev_set_drvdata(cldev, NULL);
220 	kfree(comp_master);
221 	mei_cldev_disable(cldev);
222 enable_err_exit:
223 	return ret;
224 }
225 
226 static void mei_pxp_remove(struct mei_cl_device *cldev)
227 {
228 	struct i915_pxp_component *comp_master = mei_cldev_get_drvdata(cldev);
229 	int ret;
230 
231 	component_master_del(&cldev->dev, &mei_component_master_ops);
232 	kfree(comp_master);
233 	mei_cldev_set_drvdata(cldev, NULL);
234 
235 	ret = mei_cldev_disable(cldev);
236 	if (ret)
237 		dev_warn(&cldev->dev, "mei_cldev_disable() failed\n");
238 }
239 
240 /* fbf6fcf1-96cf-4e2e-a6a6-1bab8cbe36b1 : PAVP GUID*/
241 #define MEI_GUID_PXP UUID_LE(0xfbf6fcf1, 0x96cf, 0x4e2e, 0xA6, \
242 			     0xa6, 0x1b, 0xab, 0x8c, 0xbe, 0x36, 0xb1)
243 
244 static struct mei_cl_device_id mei_pxp_tbl[] = {
245 	{ .uuid = MEI_GUID_PXP, .version = MEI_CL_VERSION_ANY },
246 	{ }
247 };
248 MODULE_DEVICE_TABLE(mei, mei_pxp_tbl);
249 
250 static struct mei_cl_driver mei_pxp_driver = {
251 	.id_table = mei_pxp_tbl,
252 	.name = KBUILD_MODNAME,
253 	.probe = mei_pxp_probe,
254 	.remove	= mei_pxp_remove,
255 };
256 
257 module_mei_cl_driver(mei_pxp_driver);
258 
259 MODULE_AUTHOR("Intel Corporation");
260 MODULE_LICENSE("GPL");
261 MODULE_DESCRIPTION("MEI PXP");
262