Commit | Line | Data |
---|---|---|
400e64df OBC |
1 | Remote Processor Framework |
2 | ||
3 | 1. Introduction | |
4 | ||
5 | Modern SoCs typically have heterogeneous remote processor devices in asymmetric | |
6 | multiprocessing (AMP) configurations, which may be running different instances | |
7 | of operating system, whether it's Linux or any other flavor of real-time OS. | |
8 | ||
9 | OMAP4, for example, has dual Cortex-A9, dual Cortex-M3 and a C64x+ DSP. | |
10 | In a typical configuration, the dual cortex-A9 is running Linux in a SMP | |
11 | configuration, and each of the other three cores (two M3 cores and a DSP) | |
12 | is running its own instance of RTOS in an AMP configuration. | |
13 | ||
14 | The remoteproc framework allows different platforms/architectures to | |
15 | control (power on, load firmware, power off) those remote processors while | |
16 | abstracting the hardware differences, so the entire driver doesn't need to be | |
17 | duplicated. In addition, this framework also adds rpmsg virtio devices | |
18 | for remote processors that supports this kind of communication. This way, | |
19 | platform-specific remoteproc drivers only need to provide a few low-level | |
20 | handlers, and then all rpmsg drivers will then just work | |
21 | (for more information about the virtio-based rpmsg bus and its drivers, | |
22 | please read Documentation/rpmsg.txt). | |
7a186941 OBC |
23 | Registration of other types of virtio devices is now also possible. Firmwares |
24 | just need to publish what kind of virtio devices do they support, and then | |
25 | remoteproc will add those devices. This makes it possible to reuse the | |
26 | existing virtio drivers with remote processor backends at a minimal development | |
27 | cost. | |
400e64df OBC |
28 | |
29 | 2. User API | |
30 | ||
31 | int rproc_boot(struct rproc *rproc) | |
32 | - Boot a remote processor (i.e. load its firmware, power it on, ...). | |
33 | If the remote processor is already powered on, this function immediately | |
34 | returns (successfully). | |
35 | Returns 0 on success, and an appropriate error value otherwise. | |
36 | Note: to use this function you should already have a valid rproc | |
37 | handle. There are several ways to achieve that cleanly (devres, pdata, | |
38 | the way remoteproc_rpmsg.c does this, or, if this becomes prevalent, we | |
40e575b1 | 39 | might also consider using dev_archdata for this). |
400e64df OBC |
40 | |
41 | void rproc_shutdown(struct rproc *rproc) | |
42 | - Power off a remote processor (previously booted with rproc_boot()). | |
43 | In case @rproc is still being used by an additional user(s), then | |
44 | this function will just decrement the power refcount and exit, | |
45 | without really powering off the device. | |
46 | Every call to rproc_boot() must (eventually) be accompanied by a call | |
47 | to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug. | |
48 | Notes: | |
49 | - we're not decrementing the rproc's refcount, only the power refcount. | |
50 | which means that the @rproc handle stays valid even after | |
51 | rproc_shutdown() returns, and users can still use it with a subsequent | |
52 | rproc_boot(), if needed. | |
400e64df OBC |
53 | |
54 | 3. Typical usage | |
55 | ||
56 | #include <linux/remoteproc.h> | |
57 | ||
58 | /* in case we were given a valid 'rproc' handle */ | |
59 | int dummy_rproc_example(struct rproc *my_rproc) | |
60 | { | |
61 | int ret; | |
62 | ||
63 | /* let's power on and boot our remote processor */ | |
64 | ret = rproc_boot(my_rproc); | |
65 | if (ret) { | |
66 | /* | |
67 | * something went wrong. handle it and leave. | |
68 | */ | |
69 | } | |
70 | ||
71 | /* | |
72 | * our remote processor is now powered on... give it some work | |
73 | */ | |
74 | ||
75 | /* let's shut it down now */ | |
76 | rproc_shutdown(my_rproc); | |
77 | } | |
78 | ||
79 | 4. API for implementors | |
80 | ||
81 | struct rproc *rproc_alloc(struct device *dev, const char *name, | |
82 | const struct rproc_ops *ops, | |
83 | const char *firmware, int len) | |
84 | - Allocate a new remote processor handle, but don't register | |
85 | it yet. Required parameters are the underlying device, the | |
86 | name of this remote processor, platform-specific ops handlers, | |
87 | the name of the firmware to boot this rproc with, and the | |
88 | length of private data needed by the allocating rproc driver (in bytes). | |
89 | ||
90 | This function should be used by rproc implementations during | |
91 | initialization of the remote processor. | |
92 | After creating an rproc handle using this function, and when ready, | |
160e7c84 | 93 | implementations should then call rproc_add() to complete |
400e64df OBC |
94 | the registration of the remote processor. |
95 | On success, the new rproc is returned, and on failure, NULL. | |
96 | ||
97 | Note: _never_ directly deallocate @rproc, even if it was not registered | |
160e7c84 | 98 | yet. Instead, when you need to unroll rproc_alloc(), use rproc_put(). |
400e64df | 99 | |
160e7c84 | 100 | void rproc_put(struct rproc *rproc) |
400e64df | 101 | - Free an rproc handle that was allocated by rproc_alloc. |
c6b5a276 OBC |
102 | This function essentially unrolls rproc_alloc(), by decrementing the |
103 | rproc's refcount. It doesn't directly free rproc; that would happen | |
104 | only if there are no other references to rproc and its refcount now | |
105 | dropped to zero. | |
400e64df | 106 | |
160e7c84 | 107 | int rproc_add(struct rproc *rproc) |
400e64df OBC |
108 | - Register @rproc with the remoteproc framework, after it has been |
109 | allocated with rproc_alloc(). | |
110 | This is called by the platform-specific rproc implementation, whenever | |
111 | a new remote processor device is probed. | |
112 | Returns 0 on success and an appropriate error code otherwise. | |
113 | Note: this function initiates an asynchronous firmware loading | |
114 | context, which will look for virtio devices supported by the rproc's | |
115 | firmware. | |
116 | If found, those virtio devices will be created and added, so as a result | |
117 | of registering this remote processor, additional virtio drivers might get | |
118 | probed. | |
400e64df | 119 | |
160e7c84 OBC |
120 | int rproc_del(struct rproc *rproc) |
121 | - Unroll rproc_add(). | |
400e64df OBC |
122 | This function should be called when the platform specific rproc |
123 | implementation decides to remove the rproc device. it should | |
160e7c84 | 124 | _only_ be called if a previous invocation of rproc_add() |
400e64df OBC |
125 | has completed successfully. |
126 | ||
160e7c84 OBC |
127 | After rproc_del() returns, @rproc is still valid, and its |
128 | last refcount should be decremented by calling rproc_put(). | |
400e64df OBC |
129 | |
130 | Returns 0 on success and -EINVAL if @rproc isn't valid. | |
131 | ||
132 | 5. Implementation callbacks | |
133 | ||
134 | These callbacks should be provided by platform-specific remoteproc | |
135 | drivers: | |
136 | ||
137 | /** | |
138 | * struct rproc_ops - platform-specific device handlers | |
139 | * @start: power on the device and boot it | |
140 | * @stop: power off the device | |
141 | * @kick: kick a virtqueue (virtqueue id given as a parameter) | |
142 | */ | |
143 | struct rproc_ops { | |
144 | int (*start)(struct rproc *rproc); | |
145 | int (*stop)(struct rproc *rproc); | |
146 | void (*kick)(struct rproc *rproc, int vqid); | |
147 | }; | |
148 | ||
149 | Every remoteproc implementation should at least provide the ->start and ->stop | |
7a186941 | 150 | handlers. If rpmsg/virtio functionality is also desired, then the ->kick handler |
400e64df OBC |
151 | should be provided as well. |
152 | ||
153 | The ->start() handler takes an rproc handle and should then power on the | |
154 | device and boot it (use rproc->priv to access platform-specific private data). | |
155 | The boot address, in case needed, can be found in rproc->bootaddr (remoteproc | |
156 | core puts there the ELF entry point). | |
157 | On success, 0 should be returned, and on failure, an appropriate error code. | |
158 | ||
159 | The ->stop() handler takes an rproc handle and powers the device down. | |
160 | On success, 0 is returned, and on failure, an appropriate error code. | |
161 | ||
162 | The ->kick() handler takes an rproc handle, and an index of a virtqueue | |
163 | where new message was placed in. Implementations should interrupt the remote | |
164 | processor and let it know it has pending messages. Notifying remote processors | |
165 | the exact virtqueue index to look in is optional: it is easy (and not | |
166 | too expensive) to go through the existing virtqueues and look for new buffers | |
167 | in the used rings. | |
168 | ||
169 | 6. Binary Firmware Structure | |
170 | ||
171 | At this point remoteproc only supports ELF32 firmware binaries. However, | |
172 | it is quite expected that other platforms/devices which we'd want to | |
173 | support with this framework will be based on different binary formats. | |
174 | ||
175 | When those use cases show up, we will have to decouple the binary format | |
176 | from the framework core, so we can support several binary formats without | |
177 | duplicating common code. | |
178 | ||
179 | When the firmware is parsed, its various segments are loaded to memory | |
180 | according to the specified device address (might be a physical address | |
181 | if the remote processor is accessing memory directly). | |
182 | ||
183 | In addition to the standard ELF segments, most remote processors would | |
184 | also include a special section which we call "the resource table". | |
185 | ||
186 | The resource table contains system resources that the remote processor | |
187 | requires before it should be powered on, such as allocation of physically | |
188 | contiguous memory, or iommu mapping of certain on-chip peripherals. | |
189 | Remotecore will only power up the device after all the resource table's | |
190 | requirement are met. | |
191 | ||
192 | In addition to system resources, the resource table may also contain | |
193 | resource entries that publish the existence of supported features | |
194 | or configurations by the remote processor, such as trace buffers and | |
195 | supported virtio devices (and their configurations). | |
196 | ||
fd2c15ec | 197 | The resource table begins with this header: |
400e64df OBC |
198 | |
199 | /** | |
fd2c15ec OBC |
200 | * struct resource_table - firmware resource table header |
201 | * @ver: version number | |
202 | * @num: number of resource entries | |
203 | * @reserved: reserved (must be zero) | |
204 | * @offset: array of offsets pointing at the various resource entries | |
205 | * | |
206 | * The header of the resource table, as expressed by this structure, | |
207 | * contains a version number (should we need to change this format in the | |
208 | * future), the number of available resource entries, and their offsets | |
209 | * in the table. | |
210 | */ | |
211 | struct resource_table { | |
212 | u32 ver; | |
213 | u32 num; | |
214 | u32 reserved[2]; | |
215 | u32 offset[0]; | |
216 | } __packed; | |
217 | ||
218 | Immediately following this header are the resource entries themselves, | |
219 | each of which begins with the following resource entry header: | |
220 | ||
221 | /** | |
222 | * struct fw_rsc_hdr - firmware resource entry header | |
400e64df | 223 | * @type: resource type |
fd2c15ec OBC |
224 | * @data: resource data |
225 | * | |
226 | * Every resource entry begins with a 'struct fw_rsc_hdr' header providing | |
227 | * its @type. The content of the entry itself will immediately follow | |
228 | * this header, and it should be parsed according to the resource type. | |
400e64df | 229 | */ |
fd2c15ec | 230 | struct fw_rsc_hdr { |
400e64df | 231 | u32 type; |
fd2c15ec | 232 | u8 data[0]; |
400e64df OBC |
233 | } __packed; |
234 | ||
235 | Some resources entries are mere announcements, where the host is informed | |
236 | of specific remoteproc configuration. Other entries require the host to | |
fd2c15ec OBC |
237 | do something (e.g. allocate a system resource). Sometimes a negotiation |
238 | is expected, where the firmware requests a resource, and once allocated, | |
239 | the host should provide back its details (e.g. address of an allocated | |
240 | memory region). | |
400e64df | 241 | |
fd2c15ec | 242 | Here are the various resource types that are currently supported: |
400e64df OBC |
243 | |
244 | /** | |
245 | * enum fw_resource_type - types of resource entries | |
246 | * | |
247 | * @RSC_CARVEOUT: request for allocation of a physically contiguous | |
248 | * memory region. | |
249 | * @RSC_DEVMEM: request to iommu_map a memory-based peripheral. | |
250 | * @RSC_TRACE: announces the availability of a trace buffer into which | |
fd2c15ec OBC |
251 | * the remote processor will be writing logs. |
252 | * @RSC_VDEV: declare support for a virtio device, and serve as its | |
253 | * virtio header. | |
254 | * @RSC_LAST: just keep this one at the end | |
255 | * | |
256 | * Please note that these values are used as indices to the rproc_handle_rsc | |
257 | * lookup table, so please keep them sane. Moreover, @RSC_LAST is used to | |
258 | * check the validity of an index before the lookup table is accessed, so | |
259 | * please update it as needed. | |
400e64df OBC |
260 | */ |
261 | enum fw_resource_type { | |
262 | RSC_CARVEOUT = 0, | |
263 | RSC_DEVMEM = 1, | |
264 | RSC_TRACE = 2, | |
fd2c15ec OBC |
265 | RSC_VDEV = 3, |
266 | RSC_LAST = 4, | |
400e64df OBC |
267 | }; |
268 | ||
fd2c15ec OBC |
269 | For more details regarding a specific resource type, please see its |
270 | dedicated structure in include/linux/remoteproc.h. | |
400e64df OBC |
271 | |
272 | We also expect that platform-specific resource entries will show up | |
fd2c15ec | 273 | at some point. When that happens, we could easily add a new RSC_PLATFORM |
400e64df OBC |
274 | type, and hand those resources to the platform-specific rproc driver to handle. |
275 | ||
276 | 7. Virtio and remoteproc | |
277 | ||
278 | The firmware should provide remoteproc information about virtio devices | |
fd2c15ec OBC |
279 | that it supports, and their configurations: a RSC_VDEV resource entry |
280 | should specify the virtio device id (as in virtio_ids.h), virtio features, | |
281 | virtio config space, vrings information, etc. | |
282 | ||
283 | When a new remote processor is registered, the remoteproc framework | |
284 | will look for its resource table and will register the virtio devices | |
285 | it supports. A firmware may support any number of virtio devices, and | |
286 | of any type (a single remote processor can also easily support several | |
287 | rpmsg virtio devices this way, if desired). | |
288 | ||
289 | Of course, RSC_VDEV resource entries are only good enough for static | |
400e64df OBC |
290 | allocation of virtio devices. Dynamic allocations will also be made possible |
291 | using the rpmsg bus (similar to how we already do dynamic allocations of | |
292 | rpmsg channels; read more about it in rpmsg.txt). |