xref: /openbmc/linux/drivers/misc/mei/pxp/mei_pxp.c (revision f2d8e15b)
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 static const struct i915_pxp_component_ops mei_pxp_ops = {
81 	.owner = THIS_MODULE,
82 	.send = mei_pxp_send_message,
83 	.recv = mei_pxp_receive_message,
84 };
85 
86 static int mei_component_master_bind(struct device *dev)
87 {
88 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
89 	struct i915_pxp_component *comp_master = mei_cldev_get_drvdata(cldev);
90 	int ret;
91 
92 	comp_master->ops = &mei_pxp_ops;
93 	comp_master->tee_dev = dev;
94 	ret = component_bind_all(dev, comp_master);
95 	if (ret < 0)
96 		return ret;
97 
98 	return 0;
99 }
100 
101 static void mei_component_master_unbind(struct device *dev)
102 {
103 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
104 	struct i915_pxp_component *comp_master = mei_cldev_get_drvdata(cldev);
105 
106 	component_unbind_all(dev, comp_master);
107 }
108 
109 static const struct component_master_ops mei_component_master_ops = {
110 	.bind = mei_component_master_bind,
111 	.unbind = mei_component_master_unbind,
112 };
113 
114 /**
115  * mei_pxp_component_match - compare function for matching mei pxp.
116  *
117  *    The function checks if the driver is i915, the subcomponent is PXP
118  *    and the grand parent of pxp and the parent of i915 are the same
119  *    PCH device.
120  *
121  * @dev: master device
122  * @subcomponent: subcomponent to match (I915_COMPONENT_PXP)
123  * @data: compare data (mei pxp device)
124  *
125  * Return:
126  * * 1 - if components match
127  * * 0 - otherwise
128  */
129 static int mei_pxp_component_match(struct device *dev, int subcomponent,
130 				   void *data)
131 {
132 	struct device *base = data;
133 
134 	if (!dev->driver || strcmp(dev->driver->name, "i915") ||
135 	    subcomponent != I915_COMPONENT_PXP)
136 		return 0;
137 
138 	base = base->parent;
139 	if (!base)
140 		return 0;
141 
142 	base = base->parent;
143 	dev = dev->parent;
144 
145 	return (base && dev && dev == base);
146 }
147 
148 static int mei_pxp_probe(struct mei_cl_device *cldev,
149 			 const struct mei_cl_device_id *id)
150 {
151 	struct i915_pxp_component *comp_master;
152 	struct component_match *master_match;
153 	int ret;
154 
155 	ret = mei_cldev_enable(cldev);
156 	if (ret < 0) {
157 		dev_err(&cldev->dev, "mei_cldev_enable Failed. %d\n", ret);
158 		goto enable_err_exit;
159 	}
160 
161 	comp_master = kzalloc(sizeof(*comp_master), GFP_KERNEL);
162 	if (!comp_master) {
163 		ret = -ENOMEM;
164 		goto err_exit;
165 	}
166 
167 	master_match = NULL;
168 	component_match_add_typed(&cldev->dev, &master_match,
169 				  mei_pxp_component_match, &cldev->dev);
170 	if (IS_ERR_OR_NULL(master_match)) {
171 		ret = -ENOMEM;
172 		goto err_exit;
173 	}
174 
175 	mei_cldev_set_drvdata(cldev, comp_master);
176 	ret = component_master_add_with_match(&cldev->dev,
177 					      &mei_component_master_ops,
178 					      master_match);
179 	if (ret < 0) {
180 		dev_err(&cldev->dev, "Master comp add failed %d\n", ret);
181 		goto err_exit;
182 	}
183 
184 	return 0;
185 
186 err_exit:
187 	mei_cldev_set_drvdata(cldev, NULL);
188 	kfree(comp_master);
189 	mei_cldev_disable(cldev);
190 enable_err_exit:
191 	return ret;
192 }
193 
194 static void mei_pxp_remove(struct mei_cl_device *cldev)
195 {
196 	struct i915_pxp_component *comp_master = mei_cldev_get_drvdata(cldev);
197 	int ret;
198 
199 	component_master_del(&cldev->dev, &mei_component_master_ops);
200 	kfree(comp_master);
201 	mei_cldev_set_drvdata(cldev, NULL);
202 
203 	ret = mei_cldev_disable(cldev);
204 	if (ret)
205 		dev_warn(&cldev->dev, "mei_cldev_disable() failed\n");
206 }
207 
208 /* fbf6fcf1-96cf-4e2e-a6a6-1bab8cbe36b1 : PAVP GUID*/
209 #define MEI_GUID_PXP GUID_INIT(0xfbf6fcf1, 0x96cf, 0x4e2e, 0xA6, \
210 			       0xa6, 0x1b, 0xab, 0x8c, 0xbe, 0x36, 0xb1)
211 
212 static struct mei_cl_device_id mei_pxp_tbl[] = {
213 	{ .uuid = MEI_GUID_PXP, .version = MEI_CL_VERSION_ANY },
214 	{ }
215 };
216 MODULE_DEVICE_TABLE(mei, mei_pxp_tbl);
217 
218 static struct mei_cl_driver mei_pxp_driver = {
219 	.id_table = mei_pxp_tbl,
220 	.name = KBUILD_MODNAME,
221 	.probe = mei_pxp_probe,
222 	.remove	= mei_pxp_remove,
223 };
224 
225 module_mei_cl_driver(mei_pxp_driver);
226 
227 MODULE_AUTHOR("Intel Corporation");
228 MODULE_LICENSE("GPL");
229 MODULE_DESCRIPTION("MEI PXP");
230