// SPDX-License-Identifier: GPL-2.0-only /* * Remote Processor Framework * * Copyright (C) 2011 Texas Instruments, Inc. * Copyright (C) 2011 Google, Inc. * * Ohad Ben-Cohen <[email protected]> * Brian Swetland <[email protected]> * Mark Grosen <[email protected]> * Fernando Guzman Lugo <[email protected]> * Suman Anna <[email protected]> * Robert Tivy <[email protected]> * Armando Uribe De Leon <[email protected]> */ #define pr_fmt(fmt) … #include <linux/delay.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/device.h> #include <linux/panic_notifier.h> #include <linux/slab.h> #include <linux/mutex.h> #include <linux/dma-mapping.h> #include <linux/firmware.h> #include <linux/string.h> #include <linux/debugfs.h> #include <linux/rculist.h> #include <linux/remoteproc.h> #include <linux/iommu.h> #include <linux/idr.h> #include <linux/elf.h> #include <linux/crc32.h> #include <linux/of_platform.h> #include <linux/of_reserved_mem.h> #include <linux/virtio_ids.h> #include <linux/virtio_ring.h> #include <asm/byteorder.h> #include <linux/platform_device.h> #include "remoteproc_internal.h" #define HIGH_BITS_MASK … static DEFINE_MUTEX(rproc_list_mutex); static LIST_HEAD(rproc_list); static struct notifier_block rproc_panic_nb; rproc_handle_resource_t; static int rproc_alloc_carveout(struct rproc *rproc, struct rproc_mem_entry *mem); static int rproc_release_carveout(struct rproc *rproc, struct rproc_mem_entry *mem); /* Unique indices for remoteproc devices */ static DEFINE_IDA(rproc_dev_index); static struct workqueue_struct *rproc_recovery_wq; static const char * const rproc_crash_names[] = …; /* translate rproc_crash_type to string */ static const char *rproc_crash_to_string(enum rproc_crash_type type) { … } /* * This is the IOMMU fault handler we register with the IOMMU API * (when relevant; not all remote processors access memory through * an IOMMU). * * IOMMU core will invoke this handler whenever the remote processor * will try to access an unmapped device address. */ static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev, unsigned long iova, int flags, void *token) { … } static int rproc_enable_iommu(struct rproc *rproc) { … } static void rproc_disable_iommu(struct rproc *rproc) { … } phys_addr_t rproc_va_to_pa(void *cpu_addr) { … } EXPORT_SYMBOL(…); /** * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address * @rproc: handle of a remote processor * @da: remoteproc device address to translate * @len: length of the memory region @da is pointing to * @is_iomem: optional pointer filled in to indicate if @da is iomapped memory * * Some remote processors will ask us to allocate them physically contiguous * memory regions (which we call "carveouts"), and map them to specific * device addresses (which are hardcoded in the firmware). They may also have * dedicated memory regions internal to the processors, and use them either * exclusively or alongside carveouts. * * They may then ask us to copy objects into specific device addresses (e.g. * code/data sections) or expose us certain symbols in other device address * (e.g. their trace buffer). * * This function is a helper function with which we can go over the allocated * carveouts and translate specific device addresses to kernel virtual addresses * so we can access the referenced memory. This function also allows to perform * translations on the internal remoteproc memory regions through a platform * implementation specific da_to_va ops, if present. * * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too, * but only on kernel direct mapped RAM memory. Instead, we're just using * here the output of the DMA API for the carveouts, which should be more * correct. * * Return: a valid kernel address on success or NULL on failure */ void *rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem) { … } EXPORT_SYMBOL(…); /** * rproc_find_carveout_by_name() - lookup the carveout region by a name * @rproc: handle of a remote processor * @name: carveout name to find (format string) * @...: optional parameters matching @name string * * Platform driver has the capability to register some pre-allacoted carveout * (physically contiguous memory regions) before rproc firmware loading and * associated resource table analysis. These regions may be dedicated memory * regions internal to the coprocessor or specified DDR region with specific * attributes * * This function is a helper function with which we can go over the * allocated carveouts and return associated region characteristics like * coprocessor address, length or processor virtual address. * * Return: a valid pointer on carveout entry on success or NULL on failure. */ __printf(2, 3) struct rproc_mem_entry * rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...) { … } /** * rproc_check_carveout_da() - Check specified carveout da configuration * @rproc: handle of a remote processor * @mem: pointer on carveout to check * @da: area device address * @len: associated area size * * This function is a helper function to verify requested device area (couple * da, len) is part of specified carveout. * If da is not set (defined as FW_RSC_ADDR_ANY), only requested length is * checked. * * Return: 0 if carveout matches request else error */ static int rproc_check_carveout_da(struct rproc *rproc, struct rproc_mem_entry *mem, u32 da, u32 len) { … } int rproc_alloc_vring(struct rproc_vdev *rvdev, int i) { … } int rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i) { … } void rproc_free_vring(struct rproc_vring *rvring) { … } void rproc_add_rvdev(struct rproc *rproc, struct rproc_vdev *rvdev) { … } void rproc_remove_rvdev(struct rproc_vdev *rvdev) { … } /** * rproc_handle_vdev() - handle a vdev fw resource * @rproc: the remote processor * @ptr: the vring resource descriptor * @offset: offset of the resource entry * @avail: size of available data (for sanity checking the image) * * This resource entry requests the host to statically register a virtio * device (vdev), and setup everything needed to support it. It contains * everything needed to make it possible: the virtio device id, virtio * device features, vrings information, virtio config space, etc... * * Before registering the vdev, the vrings are allocated from non-cacheable * physically contiguous memory. Currently we only support two vrings per * remote processor (temporary limitation). We might also want to consider * doing the vring allocation only later when ->find_vqs() is invoked, and * then release them upon ->del_vqs(). * * Note: @da is currently not really handled correctly: we dynamically * allocate it using the DMA API, ignoring requested hard coded addresses, * and we don't take care of any required IOMMU programming. This is all * going to be taken care of when the generic iommu-based DMA API will be * merged. Meanwhile, statically-addressed iommu-based firmware images should * use RSC_DEVMEM resource entries to map their required @da to the physical * address of their base CMA region (ouch, hacky!). * * Return: 0 on success, or an appropriate error code otherwise */ static int rproc_handle_vdev(struct rproc *rproc, void *ptr, int offset, int avail) { … } /** * rproc_handle_trace() - handle a shared trace buffer resource * @rproc: the remote processor * @ptr: the trace resource descriptor * @offset: offset of the resource entry * @avail: size of available data (for sanity checking the image) * * In case the remote processor dumps trace logs into memory, * export it via debugfs. * * Currently, the 'da' member of @rsc should contain the device address * where the remote processor is dumping the traces. Later we could also * support dynamically allocating this address using the generic * DMA API (but currently there isn't a use case for that). * * Return: 0 on success, or an appropriate error code otherwise */ static int rproc_handle_trace(struct rproc *rproc, void *ptr, int offset, int avail) { … } /** * rproc_handle_devmem() - handle devmem resource entry * @rproc: remote processor handle * @ptr: the devmem resource entry * @offset: offset of the resource entry * @avail: size of available data (for sanity checking the image) * * Remote processors commonly need to access certain on-chip peripherals. * * Some of these remote processors access memory via an iommu device, * and might require us to configure their iommu before they can access * the on-chip peripherals they need. * * This resource entry is a request to map such a peripheral device. * * These devmem entries will contain the physical address of the device in * the 'pa' member. If a specific device address is expected, then 'da' will * contain it (currently this is the only use case supported). 'len' will * contain the size of the physical region we need to map. * * Currently we just "trust" those devmem entries to contain valid physical * addresses, but this is going to change: we want the implementations to * tell us ranges of physical addresses the firmware is allowed to request, * and not allow firmwares to request access to physical addresses that * are outside those ranges. * * Return: 0 on success, or an appropriate error code otherwise */ static int rproc_handle_devmem(struct rproc *rproc, void *ptr, int offset, int avail) { … } /** * rproc_alloc_carveout() - allocated specified carveout * @rproc: rproc handle * @mem: the memory entry to allocate * * This function allocate specified memory entry @mem using * dma_alloc_coherent() as default allocator * * Return: 0 on success, or an appropriate error code otherwise */ static int rproc_alloc_carveout(struct rproc *rproc, struct rproc_mem_entry *mem) { … } /** * rproc_release_carveout() - release acquired carveout * @rproc: rproc handle * @mem: the memory entry to release * * This function releases specified memory entry @mem allocated via * rproc_alloc_carveout() function by @rproc. * * Return: 0 on success, or an appropriate error code otherwise */ static int rproc_release_carveout(struct rproc *rproc, struct rproc_mem_entry *mem) { … } /** * rproc_handle_carveout() - handle phys contig memory allocation requests * @rproc: rproc handle * @ptr: the resource entry * @offset: offset of the resource entry * @avail: size of available data (for image validation) * * This function will handle firmware requests for allocation of physically * contiguous memory regions. * * These request entries should come first in the firmware's resource table, * as other firmware entries might request placing other data objects inside * these memory regions (e.g. data/code segments, trace resource entries, ...). * * Allocating memory this way helps utilizing the reserved physical memory * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB * pressure is important; it may have a substantial impact on performance. * * Return: 0 on success, or an appropriate error code otherwise */ static int rproc_handle_carveout(struct rproc *rproc, void *ptr, int offset, int avail) { … } /** * rproc_add_carveout() - register an allocated carveout region * @rproc: rproc handle * @mem: memory entry to register * * This function registers specified memory entry in @rproc carveouts list. * Specified carveout should have been allocated before registering. */ void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem) { … } EXPORT_SYMBOL(…); /** * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct * @dev: pointer on device struct * @va: virtual address * @dma: dma address * @len: memory carveout length * @da: device address * @alloc: memory carveout allocation function * @release: memory carveout release function * @name: carveout name * * This function allocates a rproc_mem_entry struct and fill it with parameters * provided by client. * * Return: a valid pointer on success, or NULL on failure */ __printf(8, 9) struct rproc_mem_entry * rproc_mem_entry_init(struct device *dev, void *va, dma_addr_t dma, size_t len, u32 da, int (*alloc)(struct rproc *, struct rproc_mem_entry *), int (*release)(struct rproc *, struct rproc_mem_entry *), const char *name, ...) { … } EXPORT_SYMBOL(…); /** * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct * from a reserved memory phandle * @dev: pointer on device struct * @of_resm_idx: reserved memory phandle index in "memory-region" * @len: memory carveout length * @da: device address * @name: carveout name * * This function allocates a rproc_mem_entry struct and fill it with parameters * provided by client. * * Return: a valid pointer on success, or NULL on failure */ __printf(5, 6) struct rproc_mem_entry * rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, size_t len, u32 da, const char *name, ...) { … } EXPORT_SYMBOL(…); /** * rproc_of_parse_firmware() - parse and return the firmware-name * @dev: pointer on device struct representing a rproc * @index: index to use for the firmware-name retrieval * @fw_name: pointer to a character string, in which the firmware * name is returned on success and unmodified otherwise. * * This is an OF helper function that parses a device's DT node for * the "firmware-name" property and returns the firmware name pointer * in @fw_name on success. * * Return: 0 on success, or an appropriate failure. */ int rproc_of_parse_firmware(struct device *dev, int index, const char **fw_name) { … } EXPORT_SYMBOL(…); /* * A lookup table for resource handlers. The indices are defined in * enum fw_resource_type. */ static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = …; /* handle firmware resource entries before booting the remote processor */ static int rproc_handle_resources(struct rproc *rproc, rproc_handle_resource_t handlers[RSC_LAST]) { … } static int rproc_prepare_subdevices(struct rproc *rproc) { … } static int rproc_start_subdevices(struct rproc *rproc) { … } static void rproc_stop_subdevices(struct rproc *rproc, bool crashed) { … } static void rproc_unprepare_subdevices(struct rproc *rproc) { … } /** * rproc_alloc_registered_carveouts() - allocate all carveouts registered * in the list * @rproc: the remote processor handle * * This function parses registered carveout list, performs allocation * if alloc() ops registered and updates resource table information * if rsc_offset set. * * Return: 0 on success */ static int rproc_alloc_registered_carveouts(struct rproc *rproc) { … } /** * rproc_resource_cleanup() - clean up and free all acquired resources * @rproc: rproc handle * * This function will free all resources acquired for @rproc, and it * is called whenever @rproc either shuts down or fails to boot. */ void rproc_resource_cleanup(struct rproc *rproc) { … } EXPORT_SYMBOL(…); static int rproc_start(struct rproc *rproc, const struct firmware *fw) { … } static int __rproc_attach(struct rproc *rproc) { … } /* * take a firmware and boot a remote processor with it. */ static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw) { … } static int rproc_set_rsc_table(struct rproc *rproc) { … } static int rproc_reset_rsc_table_on_detach(struct rproc *rproc) { … } static int rproc_reset_rsc_table_on_stop(struct rproc *rproc) { … } /* * Attach to remote processor - similar to rproc_fw_boot() but without * the steps that deal with the firmware image. */ static int rproc_attach(struct rproc *rproc) { … } /* * take a firmware and boot it up. * * Note: this function is called asynchronously upon registration of the * remote processor (so we must wait until it completes before we try * to unregister the device. one other option is just to use kref here, * that might be cleaner). */ static void rproc_auto_boot_callback(const struct firmware *fw, void *context) { … } static int rproc_trigger_auto_boot(struct rproc *rproc) { … } static int rproc_stop(struct rproc *rproc, bool crashed) { … } /* * __rproc_detach(): Does the opposite of __rproc_attach() */ static int __rproc_detach(struct rproc *rproc) { … } static int rproc_attach_recovery(struct rproc *rproc) { … } static int rproc_boot_recovery(struct rproc *rproc) { … } /** * rproc_trigger_recovery() - recover a remoteproc * @rproc: the remote processor * * The recovery is done by resetting all the virtio devices, that way all the * rpmsg drivers will be reseted along with the remote processor making the * remoteproc functional again. * * This function can sleep, so it cannot be called from atomic context. * * Return: 0 on success or a negative value upon failure */ int rproc_trigger_recovery(struct rproc *rproc) { … } /** * rproc_crash_handler_work() - handle a crash * @work: work treating the crash * * This function needs to handle everything related to a crash, like cpu * registers and stack dump, information to help to debug the fatal error, etc. */ static void rproc_crash_handler_work(struct work_struct *work) { … } /** * rproc_boot() - boot a remote processor * @rproc: handle of a remote processor * * Boot a remote processor (i.e. load its firmware, power it on, ...). * * If the remote processor is already powered on, this function immediately * returns (successfully). * * Return: 0 on success, and an appropriate error value otherwise */ int rproc_boot(struct rproc *rproc) { … } EXPORT_SYMBOL(…); /** * rproc_shutdown() - power off the remote processor * @rproc: the remote processor * * Power off a remote processor (previously booted with rproc_boot()). * * In case @rproc is still being used by an additional user(s), then * this function will just decrement the power refcount and exit, * without really powering off the device. * * Every call to rproc_boot() must (eventually) be accompanied by a call * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug. * * Notes: * - we're not decrementing the rproc's refcount, only the power refcount. * which means that the @rproc handle stays valid even after rproc_shutdown() * returns, and users can still use it with a subsequent rproc_boot(), if * needed. * * Return: 0 on success, and an appropriate error value otherwise */ int rproc_shutdown(struct rproc *rproc) { … } EXPORT_SYMBOL(…); /** * rproc_detach() - Detach the remote processor from the * remoteproc core * * @rproc: the remote processor * * Detach a remote processor (previously attached to with rproc_attach()). * * In case @rproc is still being used by an additional user(s), then * this function will just decrement the power refcount and exit, * without disconnecting the device. * * Function rproc_detach() calls __rproc_detach() in order to let a remote * processor know that services provided by the application processor are * no longer available. From there it should be possible to remove the * platform driver and even power cycle the application processor (if the HW * supports it) without needing to switch off the remote processor. * * Return: 0 on success, and an appropriate error value otherwise */ int rproc_detach(struct rproc *rproc) { … } EXPORT_SYMBOL(…); /** * rproc_get_by_phandle() - find a remote processor by phandle * @phandle: phandle to the rproc * * Finds an rproc handle using the remote processor's phandle, and then * return a handle to the rproc. * * This function increments the remote processor's refcount, so always * use rproc_put() to decrement it back once rproc isn't needed anymore. * * Return: rproc handle on success, and NULL on failure */ #ifdef CONFIG_OF struct rproc *rproc_get_by_phandle(phandle phandle) { … } #else struct rproc *rproc_get_by_phandle(phandle phandle) { return NULL; } #endif EXPORT_SYMBOL(…); /** * rproc_set_firmware() - assign a new firmware * @rproc: rproc handle to which the new firmware is being assigned * @fw_name: new firmware name to be assigned * * This function allows remoteproc drivers or clients to configure a custom * firmware name that is different from the default name used during remoteproc * registration. The function does not trigger a remote processor boot, * only sets the firmware name used for a subsequent boot. This function * should also be called only when the remote processor is offline. * * This allows either the userspace to configure a different name through * sysfs or a kernel-level remoteproc or a remoteproc client driver to set * a specific firmware when it is controlling the boot and shutdown of the * remote processor. * * Return: 0 on success or a negative value upon failure */ int rproc_set_firmware(struct rproc *rproc, const char *fw_name) { … } EXPORT_SYMBOL(…); static int rproc_validate(struct rproc *rproc) { … } /** * rproc_add() - register a remote processor * @rproc: the remote processor handle to register * * Registers @rproc with the remoteproc framework, after it has been * allocated with rproc_alloc(). * * This is called by the platform-specific rproc implementation, whenever * a new remote processor device is probed. * * Note: this function initiates an asynchronous firmware loading * context, which will look for virtio devices supported by the rproc's * firmware. * * If found, those virtio devices will be created and added, so as a result * of registering this remote processor, additional virtio drivers might be * probed. * * Return: 0 on success and an appropriate error code otherwise */ int rproc_add(struct rproc *rproc) { … } EXPORT_SYMBOL(…); static void devm_rproc_remove(void *rproc) { … } /** * devm_rproc_add() - resource managed rproc_add() * @dev: the underlying device * @rproc: the remote processor handle to register * * This function performs like rproc_add() but the registered rproc device will * automatically be removed on driver detach. * * Return: 0 on success, negative errno on failure */ int devm_rproc_add(struct device *dev, struct rproc *rproc) { … } EXPORT_SYMBOL(…); /** * rproc_type_release() - release a remote processor instance * @dev: the rproc's device * * This function should _never_ be called directly. * * It will be called by the driver core when no one holds a valid pointer * to @dev anymore. */ static void rproc_type_release(struct device *dev) { … } static const struct device_type rproc_type = …; static int rproc_alloc_firmware(struct rproc *rproc, const char *name, const char *firmware) { … } static int rproc_alloc_ops(struct rproc *rproc, const struct rproc_ops *ops) { … } /** * rproc_alloc() - allocate a remote processor handle * @dev: the underlying device * @name: name of this remote processor * @ops: platform-specific handlers (mainly start/stop) * @firmware: name of firmware file to load, can be NULL * @len: length of private data needed by the rproc driver (in bytes) * * Allocates a new remote processor handle, but does not register * it yet. if @firmware is NULL, a default name is used. * * This function should be used by rproc implementations during initialization * of the remote processor. * * After creating an rproc handle using this function, and when ready, * implementations should then call rproc_add() to complete * the registration of the remote processor. * * Note: _never_ directly deallocate @rproc, even if it was not registered * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free(). * * Return: new rproc pointer on success, and NULL on failure */ struct rproc *rproc_alloc(struct device *dev, const char *name, const struct rproc_ops *ops, const char *firmware, int len) { … } EXPORT_SYMBOL(…); /** * rproc_free() - unroll rproc_alloc() * @rproc: the remote processor handle * * This function decrements the rproc dev refcount. * * If no one holds any reference to rproc anymore, then its refcount would * now drop to zero, and it would be freed. */ void rproc_free(struct rproc *rproc) { … } EXPORT_SYMBOL(…); /** * rproc_put() - release rproc reference * @rproc: the remote processor handle * * This function decrements the rproc dev refcount. * * If no one holds any reference to rproc anymore, then its refcount would * now drop to zero, and it would be freed. */ void rproc_put(struct rproc *rproc) { … } EXPORT_SYMBOL(…); /** * rproc_del() - unregister a remote processor * @rproc: rproc handle to unregister * * This function should be called when the platform specific rproc * implementation decides to remove the rproc device. it should * _only_ be called if a previous invocation of rproc_add() * has completed successfully. * * After rproc_del() returns, @rproc isn't freed yet, because * of the outstanding reference created by rproc_alloc. To decrement that * one last refcount, one still needs to call rproc_free(). * * Return: 0 on success and -EINVAL if @rproc isn't valid */ int rproc_del(struct rproc *rproc) { … } EXPORT_SYMBOL(…); static void devm_rproc_free(struct device *dev, void *res) { … } /** * devm_rproc_alloc() - resource managed rproc_alloc() * @dev: the underlying device * @name: name of this remote processor * @ops: platform-specific handlers (mainly start/stop) * @firmware: name of firmware file to load, can be NULL * @len: length of private data needed by the rproc driver (in bytes) * * This function performs like rproc_alloc() but the acquired rproc device will * automatically be released on driver detach. * * Return: new rproc instance, or NULL on failure */ struct rproc *devm_rproc_alloc(struct device *dev, const char *name, const struct rproc_ops *ops, const char *firmware, int len) { … } EXPORT_SYMBOL(…); /** * rproc_add_subdev() - add a subdevice to a remoteproc * @rproc: rproc handle to add the subdevice to * @subdev: subdev handle to register * * Caller is responsible for populating optional subdevice function pointers. */ void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev) { … } EXPORT_SYMBOL(…); /** * rproc_remove_subdev() - remove a subdevice from a remoteproc * @rproc: rproc handle to remove the subdevice from * @subdev: subdev handle, previously registered with rproc_add_subdev() */ void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev) { … } EXPORT_SYMBOL(…); /** * rproc_get_by_child() - acquire rproc handle of @dev's ancestor * @dev: child device to find ancestor of * * Return: the ancestor rproc instance, or NULL if not found */ struct rproc *rproc_get_by_child(struct device *dev) { … } EXPORT_SYMBOL(…); /** * rproc_report_crash() - rproc crash reporter function * @rproc: remote processor * @type: crash type * * This function must be called every time a crash is detected by the low-level * drivers implementing a specific remoteproc. This should not be called from a * non-remoteproc driver. * * This function can be called from atomic/interrupt context. */ void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type) { … } EXPORT_SYMBOL(…); static int rproc_panic_handler(struct notifier_block *nb, unsigned long event, void *ptr) { … } static void __init rproc_init_panic(void) { … } static void __exit rproc_exit_panic(void) { … } static int __init remoteproc_init(void) { … } subsys_initcall(remoteproc_init); static void __exit remoteproc_exit(void) { … } module_exit(remoteproc_exit); MODULE_DESCRIPTION(…) …;