Re: [PATCH 02/15] drm/i915: Embedded microcontroller (uC) firmware loading support

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 06/07/15 15:06, Daniel Vetter wrote:
On Fri, Jul 03, 2015 at 01:30:24PM +0100, Dave Gordon wrote:
Current devices may contain one or more programmable microcontrollers
that need to have a firmware image (aka "binary blob") loaded from an
external medium and transferred to the device's memory.

This file provides common support functions for doing this; they can
then be used by each uC-specific loader, thus reducing code duplication
and testing effort.

v2:
     Local functions should pass dev_priv rather than dev [Chris Wilson]
     Various other improvements per Chris Wilson's review comments

v3:
     Defeatured version without async prefetch [Daniel Vetter]

Signed-off-by: Dave Gordon <david.s.gordon@xxxxxxxxx>
Signed-off-by: Alex Dai <yu.dai@xxxxxxxxx>
---
  drivers/gpu/drm/i915/Makefile          |   3 +
  drivers/gpu/drm/i915/intel_uc_loader.c | 310 +++++++++++++++++++++++++++++++++
  drivers/gpu/drm/i915/intel_uc_loader.h |  92 ++++++++++
  3 files changed, 405 insertions(+)
  create mode 100644 drivers/gpu/drm/i915/intel_uc_loader.c
  create mode 100644 drivers/gpu/drm/i915/intel_uc_loader.h

diff --git a/drivers/gpu/drm/i915/Makefile b/drivers/gpu/drm/i915/Makefile
index de21965..f1f80fc 100644
--- a/drivers/gpu/drm/i915/Makefile
+++ b/drivers/gpu/drm/i915/Makefile
@@ -39,6 +39,9 @@ i915-y += i915_cmd_parser.o \
  	  intel_ringbuffer.o \
  	  intel_uncore.o

+# generic ancilliary microcontroller support
+i915-y += intel_uc_loader.o
+
  # autogenerated null render state
  i915-y += intel_renderstate_gen6.o \
  	  intel_renderstate_gen7.o \
diff --git a/drivers/gpu/drm/i915/intel_uc_loader.c b/drivers/gpu/drm/i915/intel_uc_loader.c
new file mode 100644
index 0000000..a8fc1dd
--- /dev/null
+++ b/drivers/gpu/drm/i915/intel_uc_loader.c
@@ -0,0 +1,310 @@
+/*
+ * Copyright © 2014 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Author:
+ *	Dave Gordon <david.s.gordon@xxxxxxxxx>
+ */
+#include <linux/firmware.h>
+#include "i915_drv.h"
+#include "intel_uc_loader.h"
+
+/**
+ * DOC: Common functions for embedded microcontroller (uC) firmware loading
+ *
+ * The functions in this file provide common support code for loading the
+ * firmware that may be required by an embedded microcontroller (uC).
+ *
+ * The function intel_uc_fw_init() should be called first; it requires no
+ * locking, and can be called even before GEM has been initialised. It just
+ * initialises the tracking data and stores its parameters for the subsequent
+ * call to intel_uc_fw_fetch().
+ *
+ * At some convenient point after GEM initialisation, the driver should call
+ * intel_uc_fw_fetch(). The first time, this will use the Linux kernel's
+ * request_firmware() call to open and read the firmware image into memory.
+ * (On subsequent calls, this is skipped, as either the firmware has already
+ * been fetched into memory, or it is already known that no valid firmware
+ * image could be found).
+ *
+ * The callback() function passed to intel_uc_fw_fetch() can further check
+ * the firmware image before it is saved. This function can reject the image
+ * by returning a negative error code (e.g. -ENOEXEC), or accept it. In the
+ * latter case, it can return INTEL_UC_FW_GOOD (which is also the default if
+ * no callback() is supplied), and the common code here will save the whole
+ * of the firmware image in a (pageable, shmfs-backed) GEM object.
+ *
+ * If saving the whole image unmodified is not appropriate (for example, if
+ * only a small part of the image is needed later, or the data needs to be
+ * reorganised before saving), the callback() function can instead make its
+ * own arrangements for saving the required data in a GEM object or otherwise
+ * and then return INTEL_UC_FW_SAVED.
+ *
+ * (If such a callback does stash (some of) the image data in a GEM object,
+ * it can use the uc_fw_obj and uc_fw_size fields; the common framework will
+ * then handle deallocating the object on failure or finalisation. Otherwise
+ * any allocated resources will have to be managed by the uC-specific code).
+ *
+ * After a successful call to intel_uc_fw_fetch(), the uC-specific code can
+ * transfer the data in the GEM object (or its own alternative) to the uC's
+ * memory (in some uC-specific way, not handled here).
+ *
+ * During driver shutdown, or if driver load is aborted, intel_uc_fw_fini()
+ * should be called to release any remaining resources.
+ */
+
+/* User-friendly representation of an enum */
+const char *intel_uc_fw_status_repr(enum intel_uc_fw_status status)
+{
+	switch (status) {
+	case INTEL_UC_FIRMWARE_FAIL:
+		return "FAIL";
+	case INTEL_UC_FIRMWARE_NONE:
+		return "NONE";
+	case INTEL_UC_FIRMWARE_PENDING:
+		return "PENDING";
+	case INTEL_UC_FIRMWARE_SUCCESS:
+		return "SUCCESS";
+	default:
+		return "UNKNOWN!";
+	}
+};
+
+/*
+ * Called once per uC, late in driver initialisation, after GEM is set up.
+ * First, we fetch the firmware image using request_firmware(), then allow
+ * the optional callback() function to check it. If it's good, and callback()
+ * doesn't say it's already saved the image, we will save it here by copying
+ * the whole thing into a (pageable, shmfs-backed) GEM object,
+ */
+static void
+uc_fw_fetch(struct intel_uc_fw *uc_fw,
+	    int callback(struct intel_uc_fw *))
+{
+	struct drm_device *dev = uc_fw->uc_dev;
+	struct drm_i915_gem_object *obj;
+	const struct firmware *fw;
+	int cb_status = INTEL_UC_FW_GOOD;
+
+	DRM_DEBUG_DRIVER("before waiting: %s fw fetch status %s, fw %p\n",
+		uc_fw->uc_name,
+		intel_uc_fw_status_repr(uc_fw->uc_fw_fetch_status),
+		uc_fw->uc_fw_blob);
+
+	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
+	WARN_ON(uc_fw->uc_fw_fetch_status != INTEL_UC_FIRMWARE_PENDING);
+
+	if (request_firmware(&fw, uc_fw->uc_fw_path, &dev->pdev->dev))
+		goto fail;
+	if (!fw)
+		goto fail;
+
+	DRM_DEBUG_DRIVER("fetch %s fw from %s succeeded, fw %p\n",
+		uc_fw->uc_name, uc_fw->uc_fw_path, fw);
+	uc_fw->uc_fw_blob = fw;
+
+	/* Callback to the optional uC-specific function, if supplied */
+	if (callback)
+		cb_status = callback(uc_fw);
+	if (cb_status < 0)
+		goto fail;
+	switch (cb_status) {
+	default:
+		WARN(1, "Invalid status %d from fw checking callback %pf\n",
+				cb_status, callback);
+		goto fail;
+
+	case INTEL_UC_FW_SAVED:
+		// Good, already saved, nothing to do
+		break;
+
+	case INTEL_UC_FW_GOOD:
+		// Good, save in GEM object
+		obj = i915_gem_object_create_from_data(dev, fw->data, fw->size);
+		if (!obj)
+			goto fail;
+
+		uc_fw->uc_fw_obj = obj;
+		uc_fw->uc_fw_size = fw->size;
+	}
+
+	DRM_DEBUG_DRIVER("%s fw fetch status SUCCESS, cb %d, obj %p\n",
+			uc_fw->uc_name, cb_status, uc_fw->uc_fw_obj);
+
+	release_firmware(fw);
+	uc_fw->uc_fw_blob = NULL;
+	uc_fw->uc_fw_fetch_status = INTEL_UC_FIRMWARE_SUCCESS;
+	return;
+
+fail:
+	DRM_DEBUG_DRIVER("%s fw fetch status FAIL; fw %p, obj %p\n",
+		uc_fw->uc_name, fw, uc_fw->uc_fw_obj);
+	DRM_ERROR("Failed to fetch %s firmware from %s\n",
+		  uc_fw->uc_name, uc_fw->uc_fw_path);
+
+	obj = uc_fw->uc_fw_obj;
+	if (obj)
+		drm_gem_object_unreference(&obj->base);
+	uc_fw->uc_fw_obj = NULL;
+
+	release_firmware(fw);		/* OK even if fw is NULL */
+	uc_fw->uc_fw_blob = NULL;
+	uc_fw->uc_fw_fetch_status = INTEL_UC_FIRMWARE_FAIL;
+}

So looking at this the entire library now boils down to a glorified
version of

	if (request_firmware != 0) {
		DRM_DEBUG_GEM("guc firmware loading failed\n");
		return fail;
	}

	gem_obj_create_from_date(fw)
	relase_firmware

Can I have that inlined (or as a simple static helper function) into the
callsite please? Yes that unfortunately means I ask you to can the really
great kerneldoc you've done (I'd really like all patches to look this
great wrt docs), but I also think that the abstraction really doesn't pay
off. Firmware-loading using request_firmware is not something tricky
enough to justify added abstraction. Yes there was the
synchronization/completion stuff in there before, but as explained in the
previous discussion I don't expect us to be able to share that part at
all, and I'd like any kind of synchronization to be open-coded using
normal kernel primitives (like flush_work or similar) to make it really
stick out to reviewers.

Thanks, Daniel

Well, I /could/ squash these functions into the GuC-specific loader and put a "g" on the front of all the names, but that would just result in the whole lot getting copypasted when we come to do the next letter of the alphabet :(

Or worse still, someone else would write a completely new version from scratch, which would be a waste of effort both for them and for those who would then have to review the new code, spot the similarities and differences, and ask for it all to be rewritten to be more like the by-then-existing GuC code.

It's also not quite as simple as your paraphrase; even without the prefetch code, it also deals with the do-this-once logic, and correctly freeing resources in all error paths; not enormously complicated but why repeat similar code in several places when you can just share the one instance (and there /will/ be other users for this code). And we may well add other common functionality here, should it turn out useful for more than one uC. You're just asking to increase the maintenance effort by duplicating the loader code rather than sharing it :(

Conversely, if you think it adds little functionality, it also adds little overhead. I wouldn't much want to inline the currently-generic functions into their callers also because that would bloat them too much; at the moment, with the split between GuC-specific and common functions, the biggest functions turn out to be about one screenful. If we "open-code" it all we end up with monstrous 300-line-plus functions with any number of error paths, which is just not nice.

So I'd really prefer to keep the utility code and the GuC-specific stuff tidily separated even before we have the other potential users of this support code in upstream.

.Dave.

_______________________________________________
Intel-gfx mailing list
Intel-gfx@xxxxxxxxxxxxxxxxxxxxx
http://lists.freedesktop.org/mailman/listinfo/intel-gfx




[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]
  Powered by Linux