Back to home page

OSCL-LXR

 
 

    


0001 ==========================
0002 Remote Processor Framework
0003 ==========================
0004 
0005 Introduction
0006 ============
0007 
0008 Modern SoCs typically have heterogeneous remote processor devices in asymmetric
0009 multiprocessing (AMP) configurations, which may be running different instances
0010 of operating system, whether it's Linux or any other flavor of real-time OS.
0011 
0012 OMAP4, for example, has dual Cortex-A9, dual Cortex-M3 and a C64x+ DSP.
0013 In a typical configuration, the dual cortex-A9 is running Linux in a SMP
0014 configuration, and each of the other three cores (two M3 cores and a DSP)
0015 is running its own instance of RTOS in an AMP configuration.
0016 
0017 The remoteproc framework allows different platforms/architectures to
0018 control (power on, load firmware, power off) those remote processors while
0019 abstracting the hardware differences, so the entire driver doesn't need to be
0020 duplicated. In addition, this framework also adds rpmsg virtio devices
0021 for remote processors that supports this kind of communication. This way,
0022 platform-specific remoteproc drivers only need to provide a few low-level
0023 handlers, and then all rpmsg drivers will then just work
0024 (for more information about the virtio-based rpmsg bus and its drivers,
0025 please read Documentation/staging/rpmsg.rst).
0026 Registration of other types of virtio devices is now also possible. Firmwares
0027 just need to publish what kind of virtio devices do they support, and then
0028 remoteproc will add those devices. This makes it possible to reuse the
0029 existing virtio drivers with remote processor backends at a minimal development
0030 cost.
0031 
0032 User API
0033 ========
0034 
0035 ::
0036 
0037   int rproc_boot(struct rproc *rproc)
0038 
0039 Boot a remote processor (i.e. load its firmware, power it on, ...).
0040 
0041 If the remote processor is already powered on, this function immediately
0042 returns (successfully).
0043 
0044 Returns 0 on success, and an appropriate error value otherwise.
0045 Note: to use this function you should already have a valid rproc
0046 handle. There are several ways to achieve that cleanly (devres, pdata,
0047 the way remoteproc_rpmsg.c does this, or, if this becomes prevalent, we
0048 might also consider using dev_archdata for this).
0049 
0050 ::
0051 
0052   int rproc_shutdown(struct rproc *rproc)
0053 
0054 Power off a remote processor (previously booted with rproc_boot()).
0055 In case @rproc is still being used by an additional user(s), then
0056 this function will just decrement the power refcount and exit,
0057 without really powering off the device.
0058 
0059 Returns 0 on success, and an appropriate error value otherwise.
0060 Every call to rproc_boot() must (eventually) be accompanied by a call
0061 to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
0062 
0063 .. note::
0064 
0065   we're not decrementing the rproc's refcount, only the power refcount.
0066   which means that the @rproc handle stays valid even after
0067   rproc_shutdown() returns, and users can still use it with a subsequent
0068   rproc_boot(), if needed.
0069 
0070 ::
0071 
0072   struct rproc *rproc_get_by_phandle(phandle phandle)
0073 
0074 Find an rproc handle using a device tree phandle. Returns the rproc
0075 handle on success, and NULL on failure. This function increments
0076 the remote processor's refcount, so always use rproc_put() to
0077 decrement it back once rproc isn't needed anymore.
0078 
0079 Typical usage
0080 =============
0081 
0082 ::
0083 
0084   #include <linux/remoteproc.h>
0085 
0086   /* in case we were given a valid 'rproc' handle */
0087   int dummy_rproc_example(struct rproc *my_rproc)
0088   {
0089         int ret;
0090 
0091         /* let's power on and boot our remote processor */
0092         ret = rproc_boot(my_rproc);
0093         if (ret) {
0094                 /*
0095                  * something went wrong. handle it and leave.
0096                  */
0097         }
0098 
0099         /*
0100          * our remote processor is now powered on... give it some work
0101          */
0102 
0103         /* let's shut it down now */
0104         rproc_shutdown(my_rproc);
0105   }
0106 
0107 API for implementors
0108 ====================
0109 
0110 ::
0111 
0112   struct rproc *rproc_alloc(struct device *dev, const char *name,
0113                                 const struct rproc_ops *ops,
0114                                 const char *firmware, int len)
0115 
0116 Allocate a new remote processor handle, but don't register
0117 it yet. Required parameters are the underlying device, the
0118 name of this remote processor, platform-specific ops handlers,
0119 the name of the firmware to boot this rproc with, and the
0120 length of private data needed by the allocating rproc driver (in bytes).
0121 
0122 This function should be used by rproc implementations during
0123 initialization of the remote processor.
0124 
0125 After creating an rproc handle using this function, and when ready,
0126 implementations should then call rproc_add() to complete
0127 the registration of the remote processor.
0128 
0129 On success, the new rproc is returned, and on failure, NULL.
0130 
0131 .. note::
0132 
0133   **never** directly deallocate @rproc, even if it was not registered
0134   yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().
0135 
0136 ::
0137 
0138   void rproc_free(struct rproc *rproc)
0139 
0140 Free an rproc handle that was allocated by rproc_alloc.
0141 
0142 This function essentially unrolls rproc_alloc(), by decrementing the
0143 rproc's refcount. It doesn't directly free rproc; that would happen
0144 only if there are no other references to rproc and its refcount now
0145 dropped to zero.
0146 
0147 ::
0148 
0149   int rproc_add(struct rproc *rproc)
0150 
0151 Register @rproc with the remoteproc framework, after it has been
0152 allocated with rproc_alloc().
0153 
0154 This is called by the platform-specific rproc implementation, whenever
0155 a new remote processor device is probed.
0156 
0157 Returns 0 on success and an appropriate error code otherwise.
0158 Note: this function initiates an asynchronous firmware loading
0159 context, which will look for virtio devices supported by the rproc's
0160 firmware.
0161 
0162 If found, those virtio devices will be created and added, so as a result
0163 of registering this remote processor, additional virtio drivers might get
0164 probed.
0165 
0166 ::
0167 
0168   int rproc_del(struct rproc *rproc)
0169 
0170 Unroll rproc_add().
0171 
0172 This function should be called when the platform specific rproc
0173 implementation decides to remove the rproc device. it should
0174 _only_ be called if a previous invocation of rproc_add()
0175 has completed successfully.
0176 
0177 After rproc_del() returns, @rproc is still valid, and its
0178 last refcount should be decremented by calling rproc_free().
0179 
0180 Returns 0 on success and -EINVAL if @rproc isn't valid.
0181 
0182 ::
0183 
0184   void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
0185 
0186 Report a crash in a remoteproc
0187 
0188 This function must be called every time a crash is detected by the
0189 platform specific rproc implementation. This should not be called from a
0190 non-remoteproc driver. This function can be called from atomic/interrupt
0191 context.
0192 
0193 Implementation callbacks
0194 ========================
0195 
0196 These callbacks should be provided by platform-specific remoteproc
0197 drivers::
0198 
0199   /**
0200    * struct rproc_ops - platform-specific device handlers
0201    * @start:    power on the device and boot it
0202    * @stop:     power off the device
0203    * @kick:     kick a virtqueue (virtqueue id given as a parameter)
0204    */
0205   struct rproc_ops {
0206         int (*start)(struct rproc *rproc);
0207         int (*stop)(struct rproc *rproc);
0208         void (*kick)(struct rproc *rproc, int vqid);
0209   };
0210 
0211 Every remoteproc implementation should at least provide the ->start and ->stop
0212 handlers. If rpmsg/virtio functionality is also desired, then the ->kick handler
0213 should be provided as well.
0214 
0215 The ->start() handler takes an rproc handle and should then power on the
0216 device and boot it (use rproc->priv to access platform-specific private data).
0217 The boot address, in case needed, can be found in rproc->bootaddr (remoteproc
0218 core puts there the ELF entry point).
0219 On success, 0 should be returned, and on failure, an appropriate error code.
0220 
0221 The ->stop() handler takes an rproc handle and powers the device down.
0222 On success, 0 is returned, and on failure, an appropriate error code.
0223 
0224 The ->kick() handler takes an rproc handle, and an index of a virtqueue
0225 where new message was placed in. Implementations should interrupt the remote
0226 processor and let it know it has pending messages. Notifying remote processors
0227 the exact virtqueue index to look in is optional: it is easy (and not
0228 too expensive) to go through the existing virtqueues and look for new buffers
0229 in the used rings.
0230 
0231 Binary Firmware Structure
0232 =========================
0233 
0234 At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
0235 it is quite expected that other platforms/devices which we'd want to
0236 support with this framework will be based on different binary formats.
0237 
0238 When those use cases show up, we will have to decouple the binary format
0239 from the framework core, so we can support several binary formats without
0240 duplicating common code.
0241 
0242 When the firmware is parsed, its various segments are loaded to memory
0243 according to the specified device address (might be a physical address
0244 if the remote processor is accessing memory directly).
0245 
0246 In addition to the standard ELF segments, most remote processors would
0247 also include a special section which we call "the resource table".
0248 
0249 The resource table contains system resources that the remote processor
0250 requires before it should be powered on, such as allocation of physically
0251 contiguous memory, or iommu mapping of certain on-chip peripherals.
0252 Remotecore will only power up the device after all the resource table's
0253 requirement are met.
0254 
0255 In addition to system resources, the resource table may also contain
0256 resource entries that publish the existence of supported features
0257 or configurations by the remote processor, such as trace buffers and
0258 supported virtio devices (and their configurations).
0259 
0260 The resource table begins with this header::
0261 
0262   /**
0263    * struct resource_table - firmware resource table header
0264    * @ver: version number
0265    * @num: number of resource entries
0266    * @reserved: reserved (must be zero)
0267    * @offset: array of offsets pointing at the various resource entries
0268    *
0269    * The header of the resource table, as expressed by this structure,
0270    * contains a version number (should we need to change this format in the
0271    * future), the number of available resource entries, and their offsets
0272    * in the table.
0273    */
0274   struct resource_table {
0275         u32 ver;
0276         u32 num;
0277         u32 reserved[2];
0278         u32 offset[0];
0279   } __packed;
0280 
0281 Immediately following this header are the resource entries themselves,
0282 each of which begins with the following resource entry header::
0283 
0284   /**
0285    * struct fw_rsc_hdr - firmware resource entry header
0286    * @type: resource type
0287    * @data: resource data
0288    *
0289    * Every resource entry begins with a 'struct fw_rsc_hdr' header providing
0290    * its @type. The content of the entry itself will immediately follow
0291    * this header, and it should be parsed according to the resource type.
0292    */
0293   struct fw_rsc_hdr {
0294         u32 type;
0295         u8 data[0];
0296   } __packed;
0297 
0298 Some resources entries are mere announcements, where the host is informed
0299 of specific remoteproc configuration. Other entries require the host to
0300 do something (e.g. allocate a system resource). Sometimes a negotiation
0301 is expected, where the firmware requests a resource, and once allocated,
0302 the host should provide back its details (e.g. address of an allocated
0303 memory region).
0304 
0305 Here are the various resource types that are currently supported::
0306 
0307   /**
0308    * enum fw_resource_type - types of resource entries
0309    *
0310    * @RSC_CARVEOUT:   request for allocation of a physically contiguous
0311    *                memory region.
0312    * @RSC_DEVMEM:     request to iommu_map a memory-based peripheral.
0313    * @RSC_TRACE:            announces the availability of a trace buffer into which
0314    *                the remote processor will be writing logs.
0315    * @RSC_VDEV:       declare support for a virtio device, and serve as its
0316    *                virtio header.
0317    * @RSC_LAST:       just keep this one at the end
0318    * @RSC_VENDOR_START: start of the vendor specific resource types range
0319    * @RSC_VENDOR_END:   end of the vendor specific resource types range
0320    *
0321    * Please note that these values are used as indices to the rproc_handle_rsc
0322    * lookup table, so please keep them sane. Moreover, @RSC_LAST is used to
0323    * check the validity of an index before the lookup table is accessed, so
0324    * please update it as needed.
0325    */
0326   enum fw_resource_type {
0327         RSC_CARVEOUT            = 0,
0328         RSC_DEVMEM              = 1,
0329         RSC_TRACE               = 2,
0330         RSC_VDEV                = 3,
0331         RSC_LAST                = 4,
0332         RSC_VENDOR_START        = 128,
0333         RSC_VENDOR_END          = 512,
0334   };
0335 
0336 For more details regarding a specific resource type, please see its
0337 dedicated structure in include/linux/remoteproc.h.
0338 
0339 We also expect that platform-specific resource entries will show up
0340 at some point. When that happens, we could easily add a new RSC_PLATFORM
0341 type, and hand those resources to the platform-specific rproc driver to handle.
0342 
0343 Virtio and remoteproc
0344 =====================
0345 
0346 The firmware should provide remoteproc information about virtio devices
0347 that it supports, and their configurations: a RSC_VDEV resource entry
0348 should specify the virtio device id (as in virtio_ids.h), virtio features,
0349 virtio config space, vrings information, etc.
0350 
0351 When a new remote processor is registered, the remoteproc framework
0352 will look for its resource table and will register the virtio devices
0353 it supports. A firmware may support any number of virtio devices, and
0354 of any type (a single remote processor can also easily support several
0355 rpmsg virtio devices this way, if desired).
0356 
0357 Of course, RSC_VDEV resource entries are only good enough for static
0358 allocation of virtio devices. Dynamic allocations will also be made possible
0359 using the rpmsg bus (similar to how we already do dynamic allocations of
0360 rpmsg channels; read more about it in rpmsg.txt).