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