File Index
Files and directories
Sign in
FILE

intel_gsc_proxy.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/i915/gt/uc/intel_gsc_proxy.c C
  1// SPDX-License-Identifier: MIT
  2/*
  3 * Copyright © 2023 Intel Corporation
  4 */
  5
  6#include <linux/component.h>
  7
  8#include <drm/intel/i915_component.h>
  9#include <drm/intel/i915_gsc_proxy_mei_interface.h>
 10
 11#include "gt/intel_gt.h"
 12#include "gt/intel_gt_print.h"
 13
 14#include "i915_drv.h"
 15#include "i915_reg.h"
 16#include "i915_wait_util.h"
 17#include "intel_gsc_proxy.h"
 18#include "intel_gsc_uc.h"
 19#include "intel_gsc_uc_heci_cmd_submit.h"
 20
 21/*
 22 * GSC proxy:
 23 * The GSC uC needs to communicate with the CSME to perform certain operations.
 24 * Since the GSC can't perform this communication directly on platforms where it
 25 * is integrated in GT, i915 needs to transfer the messages from GSC to CSME
 26 * and back. i915 must manually start the proxy flow after the GSC is loaded to
 27 * signal to GSC that we're ready to handle its messages and allow it to query
 28 * its init data from CSME; GSC will then trigger an HECI2 interrupt if it needs
 29 * to send messages to CSME again.
 30 * The proxy flow is as follow:
 31 * 1 - i915 submits a request to GSC asking for the message to CSME
 32 * 2 - GSC replies with the proxy header + payload for CSME
 33 * 3 - i915 sends the reply from GSC as-is to CSME via the mei proxy component
 34 * 4 - CSME replies with the proxy header + payload for GSC
 35 * 5 - i915 submits a request to GSC with the reply from CSME
 36 * 6 - GSC replies either with a new header + payload (same as step 2, so we
 37 *     restart from there) or with an end message.
 38 */
 39
 40/*
 41 * The component should load quite quickly in most cases, but it could take
 42 * a bit. Using a very big timeout just to cover the worst case scenario
 43 */
 44#define GSC_PROXY_INIT_TIMEOUT_MS 20000
 45
 46/* the protocol supports up to 32K in each direction */
 47#define GSC_PROXY_BUFFER_SIZE SZ_32K
 48#define GSC_PROXY_CHANNEL_SIZE (GSC_PROXY_BUFFER_SIZE * 2)
 49#define GSC_PROXY_MAX_MSG_SIZE (GSC_PROXY_BUFFER_SIZE - sizeof(struct intel_gsc_mtl_header))
 50
 51/* FW-defined proxy header */
 52struct intel_gsc_proxy_header {
 53	/*
 54	 * hdr:
 55	 * Bits 0-7: type of the proxy message (see enum intel_gsc_proxy_type)
 56	 * Bits 8-15: rsvd
 57	 * Bits 16-31: length in bytes of the payload following the proxy header
 58	 */
 59	u32 hdr;
 60#define GSC_PROXY_TYPE		 GENMASK(7, 0)
 61#define GSC_PROXY_PAYLOAD_LENGTH GENMASK(31, 16)
 62
 63	u32 source;		/* Source of the Proxy message */
 64	u32 destination;	/* Destination of the Proxy message */
 65#define GSC_PROXY_ADDRESSING_KMD  0x10000
 66#define GSC_PROXY_ADDRESSING_GSC  0x20000
 67#define GSC_PROXY_ADDRESSING_CSME 0x30000
 68
 69	u32 status;		/* Command status */
 70} __packed;
 71
 72/* FW-defined proxy types */
 73enum intel_gsc_proxy_type {
 74	GSC_PROXY_MSG_TYPE_PROXY_INVALID = 0,
 75	GSC_PROXY_MSG_TYPE_PROXY_QUERY = 1,
 76	GSC_PROXY_MSG_TYPE_PROXY_PAYLOAD = 2,
 77	GSC_PROXY_MSG_TYPE_PROXY_END = 3,
 78	GSC_PROXY_MSG_TYPE_PROXY_NOTIFICATION = 4,
 79};
 80
 81struct gsc_proxy_msg {
 82	struct intel_gsc_mtl_header header;
 83	struct intel_gsc_proxy_header proxy_header;
 84} __packed;
 85
 86static int proxy_send_to_csme(struct intel_gsc_uc *gsc)
 87{
 88	struct intel_gt *gt = gsc_uc_to_gt(gsc);
 89	struct i915_gsc_proxy_component *comp = gsc->proxy.component;
 90	struct intel_gsc_mtl_header *hdr;
 91	void *in = gsc->proxy.to_csme;
 92	void *out = gsc->proxy.to_gsc;
 93	u32 in_size;
 94	int ret;
 95
 96	/* CSME msg only includes the proxy */
 97	hdr = in;
 98	in += sizeof(struct intel_gsc_mtl_header);
 99	out += sizeof(struct intel_gsc_mtl_header);
100
101	in_size = hdr->message_size - sizeof(struct intel_gsc_mtl_header);
102
103	/* the message must contain at least the proxy header */
104	if (in_size < sizeof(struct intel_gsc_proxy_header) ||
105	    in_size > GSC_PROXY_MAX_MSG_SIZE) {
106		gt_err(gt, "Invalid CSME message size: %u\n", in_size);
107		return -EINVAL;
108	}
109
110	ret = comp->ops->send(comp->mei_dev, in, in_size);
111	if (ret < 0) {
112		gt_err(gt, "Failed to send CSME message\n");
113		return ret;
114	}
115
116	ret = comp->ops->recv(comp->mei_dev, out, GSC_PROXY_MAX_MSG_SIZE);
117	if (ret < 0) {
118		gt_err(gt, "Failed to receive CSME message\n");
119		return ret;
120	}
121
122	return ret;
123}
124
125static int proxy_send_to_gsc(struct intel_gsc_uc *gsc)
126{
127	struct intel_gt *gt = gsc_uc_to_gt(gsc);
128	u32 *marker = gsc->proxy.to_csme; /* first dw of the reply header */
129	u64 addr_in = i915_ggtt_offset(gsc->proxy.vma);
130	u64 addr_out = addr_in + GSC_PROXY_BUFFER_SIZE;
131	u32 size = ((struct gsc_proxy_msg *)gsc->proxy.to_gsc)->header.message_size;
132	int err;
133
134	/* the message must contain at least the gsc and proxy headers */
135	if (size < sizeof(struct gsc_proxy_msg) || size > GSC_PROXY_BUFFER_SIZE) {
136		gt_err(gt, "Invalid GSC proxy message size: %u\n", size);
137		return -EINVAL;
138	}
139
140	/* clear the message marker */
141	*marker = 0;
142
143	/* make sure the marker write is flushed */
144	wmb();
145
146	/* send the request */
147	err = intel_gsc_uc_heci_cmd_submit_packet(gsc, addr_in, size,
148						  addr_out, GSC_PROXY_BUFFER_SIZE);
149
150	if (!err) {
151		/* wait for the reply to show up */
152		err = wait_for(*marker != 0, 300);
153		if (err)
154			gt_err(gt, "Failed to get a proxy reply from gsc\n");
155	}
156
157	return err;
158}
159
160static int validate_proxy_header(struct intel_gsc_proxy_header *header,
161				 u32 source, u32 dest)
162{
163	u32 type = FIELD_GET(GSC_PROXY_TYPE, header->hdr);
164	u32 length = FIELD_GET(GSC_PROXY_PAYLOAD_LENGTH, header->hdr);
165	int ret = 0;
166
167	if (header->destination != dest || header->source != source) {
168		ret = -ENOEXEC;
169		goto fail;
170	}
171
172	switch (type) {
173	case GSC_PROXY_MSG_TYPE_PROXY_PAYLOAD:
174		if (length > 0)
175			break;
176		fallthrough;
177	case GSC_PROXY_MSG_TYPE_PROXY_INVALID:
178		ret = -EIO;
179		goto fail;
180	default:
181		break;
182	}
183
184fail:
185	return ret;
186}
187
188static int proxy_query(struct intel_gsc_uc *gsc)
189{
190	struct intel_gt *gt = gsc_uc_to_gt(gsc);
191	struct gsc_proxy_msg *to_gsc = gsc->proxy.to_gsc;
192	struct gsc_proxy_msg *to_csme = gsc->proxy.to_csme;
193	int ret;
194
195	intel_gsc_uc_heci_cmd_emit_mtl_header(&to_gsc->header,
196					      HECI_MEADDRESS_PROXY,
197					      sizeof(struct gsc_proxy_msg),
198					      0);
199
200	to_gsc->proxy_header.hdr =
201		FIELD_PREP(GSC_PROXY_TYPE, GSC_PROXY_MSG_TYPE_PROXY_QUERY) |
202		FIELD_PREP(GSC_PROXY_PAYLOAD_LENGTH, 0);
203
204	to_gsc->proxy_header.source = GSC_PROXY_ADDRESSING_KMD;
205	to_gsc->proxy_header.destination = GSC_PROXY_ADDRESSING_GSC;
206	to_gsc->proxy_header.status = 0;
207
208	while (1) {
209		/* clear the GSC response header space */
210		memset(gsc->proxy.to_csme, 0, sizeof(struct gsc_proxy_msg));
211
212		/* send proxy message to GSC */
213		ret = proxy_send_to_gsc(gsc);
214		if (ret) {
215			gt_err(gt, "failed to send proxy message to GSC! %d\n", ret);
216			goto proxy_error;
217		}
218
219		/* stop if this was the last message */
220		if (FIELD_GET(GSC_PROXY_TYPE, to_csme->proxy_header.hdr) ==
221				GSC_PROXY_MSG_TYPE_PROXY_END)
222			break;
223
224		/* make sure the GSC-to-CSME proxy header is sane */
225		ret = validate_proxy_header(&to_csme->proxy_header,
226					    GSC_PROXY_ADDRESSING_GSC,
227					    GSC_PROXY_ADDRESSING_CSME);
228		if (ret) {
229			gt_err(gt, "invalid GSC to CSME proxy header! %d\n", ret);
230			goto proxy_error;
231		}
232
233		/* send the GSC message to the CSME */
234		ret = proxy_send_to_csme(gsc);
235		if (ret < 0) {
236			gt_err(gt, "failed to send proxy message to CSME! %d\n", ret);
237			goto proxy_error;
238		}
239
240		/* update the GSC message size with the returned value from CSME */
241		to_gsc->header.message_size = ret + sizeof(struct intel_gsc_mtl_header);
242
243		/* make sure the CSME-to-GSC proxy header is sane */
244		ret = validate_proxy_header(&to_gsc->proxy_header,
245					    GSC_PROXY_ADDRESSING_CSME,
246					    GSC_PROXY_ADDRESSING_GSC);
247		if (ret) {
248			gt_err(gt, "invalid CSME to GSC proxy header! %d\n", ret);
249			goto proxy_error;
250		}
251	}
252
253proxy_error:
254	return ret < 0 ? ret : 0;
255}
256
257int intel_gsc_proxy_request_handler(struct intel_gsc_uc *gsc)
258{
259	struct intel_gt *gt = gsc_uc_to_gt(gsc);
260	int err;
261
262	if (!gsc->proxy.component_added)
263		return -ENODEV;
264
265	assert_rpm_wakelock_held(gt->uncore->rpm);
266
267	/* when GSC is loaded, we can queue this before the component is bound */
268	err = wait_for(gsc->proxy.component, GSC_PROXY_INIT_TIMEOUT_MS);
269	if (err) {
270		gt_err(gt, "GSC proxy component didn't bind within the expected timeout\n");
271		return -EIO;
272	}
273
274	mutex_lock(&gsc->proxy.mutex);
275	if (!gsc->proxy.component) {
276		gt_err(gt, "GSC proxy worker called without the component being bound!\n");
277		err = -EIO;
278	} else {
279		/*
280		 * write the status bit to clear it and allow new proxy
281		 * interrupts to be generated while we handle the current
282		 * request, but be sure not to write the reset bit
283		 */
284		intel_uncore_rmw(gt->uncore, HECI_H_CSR(MTL_GSC_HECI2_BASE),
285				 HECI_H_CSR_RST, HECI_H_CSR_IS);
286		err = proxy_query(gsc);
287	}
288	mutex_unlock(&gsc->proxy.mutex);
289	return err;
290}
291
292void intel_gsc_proxy_irq_handler(struct intel_gsc_uc *gsc, u32 iir)
293{
294	struct intel_gt *gt = gsc_uc_to_gt(gsc);
295
296	if (unlikely(!iir))
297		return;
298
299	lockdep_assert_held(gt->irq_lock);
300
301	if (!gsc->proxy.component) {
302		gt_err(gt, "GSC proxy irq received without the component being bound!\n");
303		return;
304	}
305
306	gsc->gsc_work_actions |= GSC_ACTION_SW_PROXY;
307	queue_work(gsc->wq, &gsc->work);
308}
309
310static int i915_gsc_proxy_component_bind(struct device *i915_kdev,
311					 struct device *mei_kdev, void *data)
312{
313	struct drm_i915_private *i915 = kdev_to_i915(i915_kdev);
314	struct intel_gt *gt = i915->media_gt;
315	struct intel_gsc_uc *gsc = &gt->uc.gsc;
316	intel_wakeref_t wakeref;
317
318	/* enable HECI2 IRQs */
319	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
320		intel_uncore_rmw(gt->uncore, HECI_H_CSR(MTL_GSC_HECI2_BASE),
321				 HECI_H_CSR_RST, HECI_H_CSR_IE);
322
323	mutex_lock(&gsc->proxy.mutex);
324	gsc->proxy.component = data;
325	gsc->proxy.component->mei_dev = mei_kdev;
326	mutex_unlock(&gsc->proxy.mutex);
327	gt_dbg(gt, "GSC proxy mei component bound\n");
328
329	return 0;
330}
331
332static void i915_gsc_proxy_component_unbind(struct device *i915_kdev,
333					    struct device *mei_kdev, void *data)
334{
335	struct drm_i915_private *i915 = kdev_to_i915(i915_kdev);
336	struct intel_gt *gt = i915->media_gt;
337	struct intel_gsc_uc *gsc = &gt->uc.gsc;
338	intel_wakeref_t wakeref;
339
340	mutex_lock(&gsc->proxy.mutex);
341	gsc->proxy.component = NULL;
342	mutex_unlock(&gsc->proxy.mutex);
343
344	/* disable HECI2 IRQs */
345	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
346		intel_uncore_rmw(gt->uncore, HECI_H_CSR(MTL_GSC_HECI2_BASE),
347				 HECI_H_CSR_IE | HECI_H_CSR_RST, 0);
348	gt_dbg(gt, "GSC proxy mei component unbound\n");
349}
350
351static const struct component_ops i915_gsc_proxy_component_ops = {
352	.bind   = i915_gsc_proxy_component_bind,
353	.unbind = i915_gsc_proxy_component_unbind,
354};
355
356static int proxy_channel_alloc(struct intel_gsc_uc *gsc)
357{
358	struct intel_gt *gt = gsc_uc_to_gt(gsc);
359	struct i915_vma *vma;
360	void *vaddr;
361	int err;
362
363	err = intel_guc_allocate_and_map_vma(gt_to_guc(gt),
364					     GSC_PROXY_CHANNEL_SIZE,
365					     &vma, &vaddr);
366	if (err)
367		return err;
368
369	gsc->proxy.vma = vma;
370	gsc->proxy.to_gsc = vaddr;
371	gsc->proxy.to_csme = vaddr + GSC_PROXY_BUFFER_SIZE;
372
373	return 0;
374}
375
376static void proxy_channel_free(struct intel_gsc_uc *gsc)
377{
378	if (!gsc->proxy.vma)
379		return;
380
381	gsc->proxy.to_gsc = NULL;
382	gsc->proxy.to_csme = NULL;
383	i915_vma_unpin_and_release(&gsc->proxy.vma, I915_VMA_RELEASE_MAP);
384}
385
386void intel_gsc_proxy_fini(struct intel_gsc_uc *gsc)
387{
388	struct intel_gt *gt = gsc_uc_to_gt(gsc);
389	struct drm_i915_private *i915 = gt->i915;
390
391	if (fetch_and_zero(&gsc->proxy.component_added))
392		component_del(i915->drm.dev, &i915_gsc_proxy_component_ops);
393
394	proxy_channel_free(gsc);
395}
396
397int intel_gsc_proxy_init(struct intel_gsc_uc *gsc)
398{
399	int err;
400	struct intel_gt *gt = gsc_uc_to_gt(gsc);
401	struct drm_i915_private *i915 = gt->i915;
402
403	mutex_init(&gsc->proxy.mutex);
404
405	if (!IS_ENABLED(CONFIG_INTEL_MEI_GSC_PROXY)) {
406		gt_info(gt, "can't init GSC proxy due to missing mei component\n");
407		return -ENODEV;
408	}
409
410	err = proxy_channel_alloc(gsc);
411	if (err)
412		return err;
413
414	err = component_add_typed(i915->drm.dev, &i915_gsc_proxy_component_ops,
415				  I915_COMPONENT_GSC_PROXY);
416	if (err < 0) {
417		gt_err(gt, "Failed to add GSC_PROXY component (%d)\n", err);
418		goto out_free;
419	}
420
421	gsc->proxy.component_added = true;
422
423	return 0;
424
425out_free:
426	proxy_channel_free(gsc);
427	return err;
428}
429
~/ Read-only file preview.