1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Remote Processor Framework
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright(c) 2011 Texas Instruments, Inc.
5*4882a593Smuzhiyun * Copyright(c) 2011 Google, Inc.
6*4882a593Smuzhiyun * All rights reserved.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
9*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
10*4882a593Smuzhiyun * are met:
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * * Redistributions of source code must retain the above copyright
13*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
14*4882a593Smuzhiyun * * Redistributions in binary form must reproduce the above copyright
15*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in
16*4882a593Smuzhiyun * the documentation and/or other materials provided with the
17*4882a593Smuzhiyun * distribution.
18*4882a593Smuzhiyun * * Neither the name Texas Instruments nor the names of its
19*4882a593Smuzhiyun * contributors may be used to endorse or promote products derived
20*4882a593Smuzhiyun * from this software without specific prior written permission.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25*4882a593Smuzhiyun * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26*4882a593Smuzhiyun * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27*4882a593Smuzhiyun * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28*4882a593Smuzhiyun * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29*4882a593Smuzhiyun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30*4882a593Smuzhiyun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32*4882a593Smuzhiyun * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #ifndef REMOTEPROC_H
36*4882a593Smuzhiyun #define REMOTEPROC_H
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #include <linux/types.h>
39*4882a593Smuzhiyun #include <linux/mutex.h>
40*4882a593Smuzhiyun #include <linux/virtio.h>
41*4882a593Smuzhiyun #include <linux/cdev.h>
42*4882a593Smuzhiyun #include <linux/completion.h>
43*4882a593Smuzhiyun #include <linux/idr.h>
44*4882a593Smuzhiyun #include <linux/of.h>
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /**
47*4882a593Smuzhiyun * struct resource_table - firmware resource table header
48*4882a593Smuzhiyun * @ver: version number
49*4882a593Smuzhiyun * @num: number of resource entries
50*4882a593Smuzhiyun * @reserved: reserved (must be zero)
51*4882a593Smuzhiyun * @offset: array of offsets pointing at the various resource entries
52*4882a593Smuzhiyun *
53*4882a593Smuzhiyun * A resource table is essentially a list of system resources required
54*4882a593Smuzhiyun * by the remote processor. It may also include configuration entries.
55*4882a593Smuzhiyun * If needed, the remote processor firmware should contain this table
56*4882a593Smuzhiyun * as a dedicated ".resource_table" ELF section.
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun * Some resources entries are mere announcements, where the host is informed
59*4882a593Smuzhiyun * of specific remoteproc configuration. Other entries require the host to
60*4882a593Smuzhiyun * do something (e.g. allocate a system resource). Sometimes a negotiation
61*4882a593Smuzhiyun * is expected, where the firmware requests a resource, and once allocated,
62*4882a593Smuzhiyun * the host should provide back its details (e.g. address of an allocated
63*4882a593Smuzhiyun * memory region).
64*4882a593Smuzhiyun *
65*4882a593Smuzhiyun * The header of the resource table, as expressed by this structure,
66*4882a593Smuzhiyun * contains a version number (should we need to change this format in the
67*4882a593Smuzhiyun * future), the number of available resource entries, and their offsets
68*4882a593Smuzhiyun * in the table.
69*4882a593Smuzhiyun *
70*4882a593Smuzhiyun * Immediately following this header are the resource entries themselves,
71*4882a593Smuzhiyun * each of which begins with a resource entry header (as described below).
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun struct resource_table {
74*4882a593Smuzhiyun u32 ver;
75*4882a593Smuzhiyun u32 num;
76*4882a593Smuzhiyun u32 reserved[2];
77*4882a593Smuzhiyun u32 offset[];
78*4882a593Smuzhiyun } __packed;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /**
81*4882a593Smuzhiyun * struct fw_rsc_hdr - firmware resource entry header
82*4882a593Smuzhiyun * @type: resource type
83*4882a593Smuzhiyun * @data: resource data
84*4882a593Smuzhiyun *
85*4882a593Smuzhiyun * Every resource entry begins with a 'struct fw_rsc_hdr' header providing
86*4882a593Smuzhiyun * its @type. The content of the entry itself will immediately follow
87*4882a593Smuzhiyun * this header, and it should be parsed according to the resource type.
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun struct fw_rsc_hdr {
90*4882a593Smuzhiyun u32 type;
91*4882a593Smuzhiyun u8 data[];
92*4882a593Smuzhiyun } __packed;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /**
95*4882a593Smuzhiyun * enum fw_resource_type - types of resource entries
96*4882a593Smuzhiyun *
97*4882a593Smuzhiyun * @RSC_CARVEOUT: request for allocation of a physically contiguous
98*4882a593Smuzhiyun * memory region.
99*4882a593Smuzhiyun * @RSC_DEVMEM: request to iommu_map a memory-based peripheral.
100*4882a593Smuzhiyun * @RSC_TRACE: announces the availability of a trace buffer into which
101*4882a593Smuzhiyun * the remote processor will be writing logs.
102*4882a593Smuzhiyun * @RSC_VDEV: declare support for a virtio device, and serve as its
103*4882a593Smuzhiyun * virtio header.
104*4882a593Smuzhiyun * @RSC_LAST: just keep this one at the end of standard resources
105*4882a593Smuzhiyun * @RSC_VENDOR_START: start of the vendor specific resource types range
106*4882a593Smuzhiyun * @RSC_VENDOR_END: end of the vendor specific resource types range
107*4882a593Smuzhiyun *
108*4882a593Smuzhiyun * For more details regarding a specific resource type, please see its
109*4882a593Smuzhiyun * dedicated structure below.
110*4882a593Smuzhiyun *
111*4882a593Smuzhiyun * Please note that these values are used as indices to the rproc_handle_rsc
112*4882a593Smuzhiyun * lookup table, so please keep them sane. Moreover, @RSC_LAST is used to
113*4882a593Smuzhiyun * check the validity of an index before the lookup table is accessed, so
114*4882a593Smuzhiyun * please update it as needed.
115*4882a593Smuzhiyun */
116*4882a593Smuzhiyun enum fw_resource_type {
117*4882a593Smuzhiyun RSC_CARVEOUT = 0,
118*4882a593Smuzhiyun RSC_DEVMEM = 1,
119*4882a593Smuzhiyun RSC_TRACE = 2,
120*4882a593Smuzhiyun RSC_VDEV = 3,
121*4882a593Smuzhiyun RSC_LAST = 4,
122*4882a593Smuzhiyun RSC_VENDOR_START = 128,
123*4882a593Smuzhiyun RSC_VENDOR_END = 512,
124*4882a593Smuzhiyun };
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun #define FW_RSC_ADDR_ANY (-1)
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /**
129*4882a593Smuzhiyun * struct fw_rsc_carveout - physically contiguous memory request
130*4882a593Smuzhiyun * @da: device address
131*4882a593Smuzhiyun * @pa: physical address
132*4882a593Smuzhiyun * @len: length (in bytes)
133*4882a593Smuzhiyun * @flags: iommu protection flags
134*4882a593Smuzhiyun * @reserved: reserved (must be zero)
135*4882a593Smuzhiyun * @name: human-readable name of the requested memory region
136*4882a593Smuzhiyun *
137*4882a593Smuzhiyun * This resource entry requests the host to allocate a physically contiguous
138*4882a593Smuzhiyun * memory region.
139*4882a593Smuzhiyun *
140*4882a593Smuzhiyun * These request entries should precede other firmware resource entries,
141*4882a593Smuzhiyun * as other entries might request placing other data objects inside
142*4882a593Smuzhiyun * these memory regions (e.g. data/code segments, trace resource entries, ...).
143*4882a593Smuzhiyun *
144*4882a593Smuzhiyun * Allocating memory this way helps utilizing the reserved physical memory
145*4882a593Smuzhiyun * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
146*4882a593Smuzhiyun * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
147*4882a593Smuzhiyun * pressure is important; it may have a substantial impact on performance.
148*4882a593Smuzhiyun *
149*4882a593Smuzhiyun * If the firmware is compiled with static addresses, then @da should specify
150*4882a593Smuzhiyun * the expected device address of this memory region. If @da is set to
151*4882a593Smuzhiyun * FW_RSC_ADDR_ANY, then the host will dynamically allocate it, and then
152*4882a593Smuzhiyun * overwrite @da with the dynamically allocated address.
153*4882a593Smuzhiyun *
154*4882a593Smuzhiyun * We will always use @da to negotiate the device addresses, even if it
155*4882a593Smuzhiyun * isn't using an iommu. In that case, though, it will obviously contain
156*4882a593Smuzhiyun * physical addresses.
157*4882a593Smuzhiyun *
158*4882a593Smuzhiyun * Some remote processors needs to know the allocated physical address
159*4882a593Smuzhiyun * even if they do use an iommu. This is needed, e.g., if they control
160*4882a593Smuzhiyun * hardware accelerators which access the physical memory directly (this
161*4882a593Smuzhiyun * is the case with OMAP4 for instance). In that case, the host will
162*4882a593Smuzhiyun * overwrite @pa with the dynamically allocated physical address.
163*4882a593Smuzhiyun * Generally we don't want to expose physical addresses if we don't have to
164*4882a593Smuzhiyun * (remote processors are generally _not_ trusted), so we might want to
165*4882a593Smuzhiyun * change this to happen _only_ when explicitly required by the hardware.
166*4882a593Smuzhiyun *
167*4882a593Smuzhiyun * @flags is used to provide IOMMU protection flags, and @name should
168*4882a593Smuzhiyun * (optionally) contain a human readable name of this carveout region
169*4882a593Smuzhiyun * (mainly for debugging purposes).
170*4882a593Smuzhiyun */
171*4882a593Smuzhiyun struct fw_rsc_carveout {
172*4882a593Smuzhiyun u32 da;
173*4882a593Smuzhiyun u32 pa;
174*4882a593Smuzhiyun u32 len;
175*4882a593Smuzhiyun u32 flags;
176*4882a593Smuzhiyun u32 reserved;
177*4882a593Smuzhiyun u8 name[32];
178*4882a593Smuzhiyun } __packed;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /**
181*4882a593Smuzhiyun * struct fw_rsc_devmem - iommu mapping request
182*4882a593Smuzhiyun * @da: device address
183*4882a593Smuzhiyun * @pa: physical address
184*4882a593Smuzhiyun * @len: length (in bytes)
185*4882a593Smuzhiyun * @flags: iommu protection flags
186*4882a593Smuzhiyun * @reserved: reserved (must be zero)
187*4882a593Smuzhiyun * @name: human-readable name of the requested region to be mapped
188*4882a593Smuzhiyun *
189*4882a593Smuzhiyun * This resource entry requests the host to iommu map a physically contiguous
190*4882a593Smuzhiyun * memory region. This is needed in case the remote processor requires
191*4882a593Smuzhiyun * access to certain memory-based peripherals; _never_ use it to access
192*4882a593Smuzhiyun * regular memory.
193*4882a593Smuzhiyun *
194*4882a593Smuzhiyun * This is obviously only needed if the remote processor is accessing memory
195*4882a593Smuzhiyun * via an iommu.
196*4882a593Smuzhiyun *
197*4882a593Smuzhiyun * @da should specify the required device address, @pa should specify
198*4882a593Smuzhiyun * the physical address we want to map, @len should specify the size of
199*4882a593Smuzhiyun * the mapping and @flags is the IOMMU protection flags. As always, @name may
200*4882a593Smuzhiyun * (optionally) contain a human readable name of this mapping (mainly for
201*4882a593Smuzhiyun * debugging purposes).
202*4882a593Smuzhiyun *
203*4882a593Smuzhiyun * Note: at this point we just "trust" those devmem entries to contain valid
204*4882a593Smuzhiyun * physical addresses, but this isn't safe and will be changed: eventually we
205*4882a593Smuzhiyun * want remoteproc implementations to provide us ranges of physical addresses
206*4882a593Smuzhiyun * the firmware is allowed to request, and not allow firmwares to request
207*4882a593Smuzhiyun * access to physical addresses that are outside those ranges.
208*4882a593Smuzhiyun */
209*4882a593Smuzhiyun struct fw_rsc_devmem {
210*4882a593Smuzhiyun u32 da;
211*4882a593Smuzhiyun u32 pa;
212*4882a593Smuzhiyun u32 len;
213*4882a593Smuzhiyun u32 flags;
214*4882a593Smuzhiyun u32 reserved;
215*4882a593Smuzhiyun u8 name[32];
216*4882a593Smuzhiyun } __packed;
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /**
219*4882a593Smuzhiyun * struct fw_rsc_trace - trace buffer declaration
220*4882a593Smuzhiyun * @da: device address
221*4882a593Smuzhiyun * @len: length (in bytes)
222*4882a593Smuzhiyun * @reserved: reserved (must be zero)
223*4882a593Smuzhiyun * @name: human-readable name of the trace buffer
224*4882a593Smuzhiyun *
225*4882a593Smuzhiyun * This resource entry provides the host information about a trace buffer
226*4882a593Smuzhiyun * into which the remote processor will write log messages.
227*4882a593Smuzhiyun *
228*4882a593Smuzhiyun * @da specifies the device address of the buffer, @len specifies
229*4882a593Smuzhiyun * its size, and @name may contain a human readable name of the trace buffer.
230*4882a593Smuzhiyun *
231*4882a593Smuzhiyun * After booting the remote processor, the trace buffers are exposed to the
232*4882a593Smuzhiyun * user via debugfs entries (called trace0, trace1, etc..).
233*4882a593Smuzhiyun */
234*4882a593Smuzhiyun struct fw_rsc_trace {
235*4882a593Smuzhiyun u32 da;
236*4882a593Smuzhiyun u32 len;
237*4882a593Smuzhiyun u32 reserved;
238*4882a593Smuzhiyun u8 name[32];
239*4882a593Smuzhiyun } __packed;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun /**
242*4882a593Smuzhiyun * struct fw_rsc_vdev_vring - vring descriptor entry
243*4882a593Smuzhiyun * @da: device address
244*4882a593Smuzhiyun * @align: the alignment between the consumer and producer parts of the vring
245*4882a593Smuzhiyun * @num: num of buffers supported by this vring (must be power of two)
246*4882a593Smuzhiyun * @notifyid is a unique rproc-wide notify index for this vring. This notify
247*4882a593Smuzhiyun * index is used when kicking a remote processor, to let it know that this
248*4882a593Smuzhiyun * vring is triggered.
249*4882a593Smuzhiyun * @pa: physical address
250*4882a593Smuzhiyun *
251*4882a593Smuzhiyun * This descriptor is not a resource entry by itself; it is part of the
252*4882a593Smuzhiyun * vdev resource type (see below).
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * Note that @da should either contain the device address where
255*4882a593Smuzhiyun * the remote processor is expecting the vring, or indicate that
256*4882a593Smuzhiyun * dynamically allocation of the vring's device address is supported.
257*4882a593Smuzhiyun */
258*4882a593Smuzhiyun struct fw_rsc_vdev_vring {
259*4882a593Smuzhiyun u32 da;
260*4882a593Smuzhiyun u32 align;
261*4882a593Smuzhiyun u32 num;
262*4882a593Smuzhiyun u32 notifyid;
263*4882a593Smuzhiyun u32 pa;
264*4882a593Smuzhiyun } __packed;
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun /**
267*4882a593Smuzhiyun * struct fw_rsc_vdev - virtio device header
268*4882a593Smuzhiyun * @id: virtio device id (as in virtio_ids.h)
269*4882a593Smuzhiyun * @notifyid is a unique rproc-wide notify index for this vdev. This notify
270*4882a593Smuzhiyun * index is used when kicking a remote processor, to let it know that the
271*4882a593Smuzhiyun * status/features of this vdev have changes.
272*4882a593Smuzhiyun * @dfeatures specifies the virtio device features supported by the firmware
273*4882a593Smuzhiyun * @gfeatures is a place holder used by the host to write back the
274*4882a593Smuzhiyun * negotiated features that are supported by both sides.
275*4882a593Smuzhiyun * @config_len is the size of the virtio config space of this vdev. The config
276*4882a593Smuzhiyun * space lies in the resource table immediate after this vdev header.
277*4882a593Smuzhiyun * @status is a place holder where the host will indicate its virtio progress.
278*4882a593Smuzhiyun * @num_of_vrings indicates how many vrings are described in this vdev header
279*4882a593Smuzhiyun * @reserved: reserved (must be zero)
280*4882a593Smuzhiyun * @vring is an array of @num_of_vrings entries of 'struct fw_rsc_vdev_vring'.
281*4882a593Smuzhiyun *
282*4882a593Smuzhiyun * This resource is a virtio device header: it provides information about
283*4882a593Smuzhiyun * the vdev, and is then used by the host and its peer remote processors
284*4882a593Smuzhiyun * to negotiate and share certain virtio properties.
285*4882a593Smuzhiyun *
286*4882a593Smuzhiyun * By providing this resource entry, the firmware essentially asks remoteproc
287*4882a593Smuzhiyun * to statically allocate a vdev upon registration of the rproc (dynamic vdev
288*4882a593Smuzhiyun * allocation is not yet supported).
289*4882a593Smuzhiyun *
290*4882a593Smuzhiyun * Note: unlike virtualization systems, the term 'host' here means
291*4882a593Smuzhiyun * the Linux side which is running remoteproc to control the remote
292*4882a593Smuzhiyun * processors. We use the name 'gfeatures' to comply with virtio's terms,
293*4882a593Smuzhiyun * though there isn't really any virtualized guest OS here: it's the host
294*4882a593Smuzhiyun * which is responsible for negotiating the final features.
295*4882a593Smuzhiyun * Yeah, it's a bit confusing.
296*4882a593Smuzhiyun *
297*4882a593Smuzhiyun * Note: immediately following this structure is the virtio config space for
298*4882a593Smuzhiyun * this vdev (which is specific to the vdev; for more info, read the virtio
299*4882a593Smuzhiyun * spec). the size of the config space is specified by @config_len.
300*4882a593Smuzhiyun */
301*4882a593Smuzhiyun struct fw_rsc_vdev {
302*4882a593Smuzhiyun u32 id;
303*4882a593Smuzhiyun u32 notifyid;
304*4882a593Smuzhiyun u32 dfeatures;
305*4882a593Smuzhiyun u32 gfeatures;
306*4882a593Smuzhiyun u32 config_len;
307*4882a593Smuzhiyun u8 status;
308*4882a593Smuzhiyun u8 num_of_vrings;
309*4882a593Smuzhiyun u8 reserved[2];
310*4882a593Smuzhiyun struct fw_rsc_vdev_vring vring[];
311*4882a593Smuzhiyun } __packed;
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun struct rproc;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /**
316*4882a593Smuzhiyun * struct rproc_mem_entry - memory entry descriptor
317*4882a593Smuzhiyun * @va: virtual address
318*4882a593Smuzhiyun * @is_iomem: io memory
319*4882a593Smuzhiyun * @dma: dma address
320*4882a593Smuzhiyun * @len: length, in bytes
321*4882a593Smuzhiyun * @da: device address
322*4882a593Smuzhiyun * @release: release associated memory
323*4882a593Smuzhiyun * @priv: associated data
324*4882a593Smuzhiyun * @name: associated memory region name (optional)
325*4882a593Smuzhiyun * @node: list node
326*4882a593Smuzhiyun * @rsc_offset: offset in resource table
327*4882a593Smuzhiyun * @flags: iommu protection flags
328*4882a593Smuzhiyun * @of_resm_idx: reserved memory phandle index
329*4882a593Smuzhiyun * @alloc: specific memory allocator function
330*4882a593Smuzhiyun */
331*4882a593Smuzhiyun struct rproc_mem_entry {
332*4882a593Smuzhiyun void *va;
333*4882a593Smuzhiyun bool is_iomem;
334*4882a593Smuzhiyun dma_addr_t dma;
335*4882a593Smuzhiyun size_t len;
336*4882a593Smuzhiyun u32 da;
337*4882a593Smuzhiyun void *priv;
338*4882a593Smuzhiyun char name[32];
339*4882a593Smuzhiyun struct list_head node;
340*4882a593Smuzhiyun u32 rsc_offset;
341*4882a593Smuzhiyun u32 flags;
342*4882a593Smuzhiyun u32 of_resm_idx;
343*4882a593Smuzhiyun int (*alloc)(struct rproc *rproc, struct rproc_mem_entry *mem);
344*4882a593Smuzhiyun int (*release)(struct rproc *rproc, struct rproc_mem_entry *mem);
345*4882a593Smuzhiyun };
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun struct firmware;
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun /**
350*4882a593Smuzhiyun * enum rsc_handling_status - return status of rproc_ops handle_rsc hook
351*4882a593Smuzhiyun * @RSC_HANDLED: resource was handled
352*4882a593Smuzhiyun * @RSC_IGNORED: resource was ignored
353*4882a593Smuzhiyun */
354*4882a593Smuzhiyun enum rsc_handling_status {
355*4882a593Smuzhiyun RSC_HANDLED = 0,
356*4882a593Smuzhiyun RSC_IGNORED = 1,
357*4882a593Smuzhiyun };
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun /**
360*4882a593Smuzhiyun * struct rproc_ops - platform-specific device handlers
361*4882a593Smuzhiyun * @prepare: prepare device for code loading
362*4882a593Smuzhiyun * @unprepare: unprepare device after stop
363*4882a593Smuzhiyun * @start: power on the device and boot it
364*4882a593Smuzhiyun * @stop: power off the device
365*4882a593Smuzhiyun * @attach: attach to a device that his already powered up
366*4882a593Smuzhiyun * @kick: kick a virtqueue (virtqueue id given as a parameter)
367*4882a593Smuzhiyun * @da_to_va: optional platform hook to perform address translations
368*4882a593Smuzhiyun * @parse_fw: parse firmware to extract information (e.g. resource table)
369*4882a593Smuzhiyun * @handle_rsc: optional platform hook to handle vendor resources. Should return
370*4882a593Smuzhiyun * RSC_HANDLED if resource was handled, RSC_IGNORED if not handled and a
371*4882a593Smuzhiyun * negative value on error
372*4882a593Smuzhiyun * @load_rsc_table: load resource table from firmware image
373*4882a593Smuzhiyun * @find_loaded_rsc_table: find the loaded resouce table
374*4882a593Smuzhiyun * @load: load firmware to memory, where the remote processor
375*4882a593Smuzhiyun * expects to find it
376*4882a593Smuzhiyun * @sanity_check: sanity check the fw image
377*4882a593Smuzhiyun * @get_boot_addr: get boot address to entry point specified in firmware
378*4882a593Smuzhiyun * @panic: optional callback to react to system panic, core will delay
379*4882a593Smuzhiyun * panic at least the returned number of milliseconds
380*4882a593Smuzhiyun * @coredump: collect firmware dump after the subsystem is shutdown
381*4882a593Smuzhiyun */
382*4882a593Smuzhiyun struct rproc_ops {
383*4882a593Smuzhiyun int (*prepare)(struct rproc *rproc);
384*4882a593Smuzhiyun int (*unprepare)(struct rproc *rproc);
385*4882a593Smuzhiyun int (*start)(struct rproc *rproc);
386*4882a593Smuzhiyun int (*stop)(struct rproc *rproc);
387*4882a593Smuzhiyun int (*attach)(struct rproc *rproc);
388*4882a593Smuzhiyun void (*kick)(struct rproc *rproc, int vqid);
389*4882a593Smuzhiyun void * (*da_to_va)(struct rproc *rproc, u64 da, size_t len, bool *is_iomem);
390*4882a593Smuzhiyun int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
391*4882a593Smuzhiyun int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
392*4882a593Smuzhiyun int offset, int avail);
393*4882a593Smuzhiyun struct resource_table *(*find_loaded_rsc_table)(
394*4882a593Smuzhiyun struct rproc *rproc, const struct firmware *fw);
395*4882a593Smuzhiyun int (*load)(struct rproc *rproc, const struct firmware *fw);
396*4882a593Smuzhiyun int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
397*4882a593Smuzhiyun u64 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
398*4882a593Smuzhiyun unsigned long (*panic)(struct rproc *rproc);
399*4882a593Smuzhiyun void (*coredump)(struct rproc *rproc);
400*4882a593Smuzhiyun };
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun /**
403*4882a593Smuzhiyun * enum rproc_state - remote processor states
404*4882a593Smuzhiyun * @RPROC_OFFLINE: device is powered off
405*4882a593Smuzhiyun * @RPROC_SUSPENDED: device is suspended; needs to be woken up to receive
406*4882a593Smuzhiyun * a message.
407*4882a593Smuzhiyun * @RPROC_RUNNING: device is up and running
408*4882a593Smuzhiyun * @RPROC_CRASHED: device has crashed; need to start recovery
409*4882a593Smuzhiyun * @RPROC_DELETED: device is deleted
410*4882a593Smuzhiyun * @RPROC_DETACHED: device has been booted by another entity and waiting
411*4882a593Smuzhiyun * for the core to attach to it
412*4882a593Smuzhiyun * @RPROC_LAST: just keep this one at the end
413*4882a593Smuzhiyun *
414*4882a593Smuzhiyun * Please note that the values of these states are used as indices
415*4882a593Smuzhiyun * to rproc_state_string, a state-to-name lookup table,
416*4882a593Smuzhiyun * so please keep the two synchronized. @RPROC_LAST is used to check
417*4882a593Smuzhiyun * the validity of an index before the lookup table is accessed, so
418*4882a593Smuzhiyun * please update it as needed too.
419*4882a593Smuzhiyun */
420*4882a593Smuzhiyun enum rproc_state {
421*4882a593Smuzhiyun RPROC_OFFLINE = 0,
422*4882a593Smuzhiyun RPROC_SUSPENDED = 1,
423*4882a593Smuzhiyun RPROC_RUNNING = 2,
424*4882a593Smuzhiyun RPROC_CRASHED = 3,
425*4882a593Smuzhiyun RPROC_DELETED = 4,
426*4882a593Smuzhiyun RPROC_DETACHED = 5,
427*4882a593Smuzhiyun RPROC_LAST = 6,
428*4882a593Smuzhiyun };
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun /**
431*4882a593Smuzhiyun * enum rproc_crash_type - remote processor crash types
432*4882a593Smuzhiyun * @RPROC_MMUFAULT: iommu fault
433*4882a593Smuzhiyun * @RPROC_WATCHDOG: watchdog bite
434*4882a593Smuzhiyun * @RPROC_FATAL_ERROR fatal error
435*4882a593Smuzhiyun *
436*4882a593Smuzhiyun * Each element of the enum is used as an array index. So that, the value of
437*4882a593Smuzhiyun * the elements should be always something sane.
438*4882a593Smuzhiyun *
439*4882a593Smuzhiyun * Feel free to add more types when needed.
440*4882a593Smuzhiyun */
441*4882a593Smuzhiyun enum rproc_crash_type {
442*4882a593Smuzhiyun RPROC_MMUFAULT,
443*4882a593Smuzhiyun RPROC_WATCHDOG,
444*4882a593Smuzhiyun RPROC_FATAL_ERROR,
445*4882a593Smuzhiyun };
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun /**
448*4882a593Smuzhiyun * enum rproc_dump_mechanism - Coredump options for core
449*4882a593Smuzhiyun * @RPROC_COREDUMP_DISABLED: Don't perform any dump
450*4882a593Smuzhiyun * @RPROC_COREDUMP_ENABLED: Copy dump to separate buffer and carry on with
451*4882a593Smuzhiyun recovery
452*4882a593Smuzhiyun * @RPROC_COREDUMP_INLINE: Read segments directly from device memory. Stall
453*4882a593Smuzhiyun recovery until all segments are read
454*4882a593Smuzhiyun */
455*4882a593Smuzhiyun enum rproc_dump_mechanism {
456*4882a593Smuzhiyun RPROC_COREDUMP_DISABLED,
457*4882a593Smuzhiyun RPROC_COREDUMP_ENABLED,
458*4882a593Smuzhiyun RPROC_COREDUMP_INLINE,
459*4882a593Smuzhiyun };
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun /**
462*4882a593Smuzhiyun * struct rproc_dump_segment - segment info from ELF header
463*4882a593Smuzhiyun * @node: list node related to the rproc segment list
464*4882a593Smuzhiyun * @da: device address of the segment
465*4882a593Smuzhiyun * @size: size of the segment
466*4882a593Smuzhiyun * @priv: private data associated with the dump_segment
467*4882a593Smuzhiyun * @dump: custom dump function to fill device memory segment associated
468*4882a593Smuzhiyun * with coredump
469*4882a593Smuzhiyun */
470*4882a593Smuzhiyun struct rproc_dump_segment {
471*4882a593Smuzhiyun struct list_head node;
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun dma_addr_t da;
474*4882a593Smuzhiyun size_t size;
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun void *priv;
477*4882a593Smuzhiyun void (*dump)(struct rproc *rproc, struct rproc_dump_segment *segment,
478*4882a593Smuzhiyun void *dest, size_t offset, size_t size);
479*4882a593Smuzhiyun loff_t offset;
480*4882a593Smuzhiyun };
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun /**
483*4882a593Smuzhiyun * struct rproc - represents a physical remote processor device
484*4882a593Smuzhiyun * @node: list node of this rproc object
485*4882a593Smuzhiyun * @domain: iommu domain
486*4882a593Smuzhiyun * @name: human readable name of the rproc
487*4882a593Smuzhiyun * @firmware: name of firmware file to be loaded
488*4882a593Smuzhiyun * @priv: private data which belongs to the platform-specific rproc module
489*4882a593Smuzhiyun * @ops: platform-specific start/stop rproc handlers
490*4882a593Smuzhiyun * @dev: virtual device for refcounting and common remoteproc behavior
491*4882a593Smuzhiyun * @power: refcount of users who need this rproc powered up
492*4882a593Smuzhiyun * @state: state of the device
493*4882a593Smuzhiyun * @dump_conf: Currently selected coredump configuration
494*4882a593Smuzhiyun * @lock: lock which protects concurrent manipulations of the rproc
495*4882a593Smuzhiyun * @dbg_dir: debugfs directory of this rproc device
496*4882a593Smuzhiyun * @traces: list of trace buffers
497*4882a593Smuzhiyun * @num_traces: number of trace buffers
498*4882a593Smuzhiyun * @carveouts: list of physically contiguous memory allocations
499*4882a593Smuzhiyun * @mappings: list of iommu mappings we initiated, needed on shutdown
500*4882a593Smuzhiyun * @bootaddr: address of first instruction to boot rproc with (optional)
501*4882a593Smuzhiyun * @rvdevs: list of remote virtio devices
502*4882a593Smuzhiyun * @subdevs: list of subdevices, to following the running state
503*4882a593Smuzhiyun * @notifyids: idr for dynamically assigning rproc-wide unique notify ids
504*4882a593Smuzhiyun * @index: index of this rproc device
505*4882a593Smuzhiyun * @crash_handler: workqueue for handling a crash
506*4882a593Smuzhiyun * @crash_cnt: crash counter
507*4882a593Smuzhiyun * @recovery_disabled: flag that state if recovery was disabled
508*4882a593Smuzhiyun * @max_notifyid: largest allocated notify id.
509*4882a593Smuzhiyun * @table_ptr: pointer to the resource table in effect
510*4882a593Smuzhiyun * @cached_table: copy of the resource table
511*4882a593Smuzhiyun * @table_sz: size of @cached_table
512*4882a593Smuzhiyun * @has_iommu: flag to indicate if remote processor is behind an MMU
513*4882a593Smuzhiyun * @auto_boot: flag to indicate if remote processor should be auto-started
514*4882a593Smuzhiyun * @autonomous: true if an external entity has booted the remote processor
515*4882a593Smuzhiyun * @dump_segments: list of segments in the firmware
516*4882a593Smuzhiyun * @nb_vdev: number of vdev currently handled by rproc
517*4882a593Smuzhiyun * @char_dev: character device of the rproc
518*4882a593Smuzhiyun * @cdev_put_on_release: flag to indicate if remoteproc should be shutdown on @char_dev release
519*4882a593Smuzhiyun */
520*4882a593Smuzhiyun struct rproc {
521*4882a593Smuzhiyun struct list_head node;
522*4882a593Smuzhiyun struct iommu_domain *domain;
523*4882a593Smuzhiyun const char *name;
524*4882a593Smuzhiyun const char *firmware;
525*4882a593Smuzhiyun void *priv;
526*4882a593Smuzhiyun struct rproc_ops *ops;
527*4882a593Smuzhiyun struct device dev;
528*4882a593Smuzhiyun atomic_t power;
529*4882a593Smuzhiyun unsigned int state;
530*4882a593Smuzhiyun enum rproc_dump_mechanism dump_conf;
531*4882a593Smuzhiyun struct mutex lock;
532*4882a593Smuzhiyun struct dentry *dbg_dir;
533*4882a593Smuzhiyun struct list_head traces;
534*4882a593Smuzhiyun int num_traces;
535*4882a593Smuzhiyun struct list_head carveouts;
536*4882a593Smuzhiyun struct list_head mappings;
537*4882a593Smuzhiyun u64 bootaddr;
538*4882a593Smuzhiyun struct list_head rvdevs;
539*4882a593Smuzhiyun struct list_head subdevs;
540*4882a593Smuzhiyun struct idr notifyids;
541*4882a593Smuzhiyun int index;
542*4882a593Smuzhiyun struct work_struct crash_handler;
543*4882a593Smuzhiyun unsigned int crash_cnt;
544*4882a593Smuzhiyun bool recovery_disabled;
545*4882a593Smuzhiyun int max_notifyid;
546*4882a593Smuzhiyun struct resource_table *table_ptr;
547*4882a593Smuzhiyun struct resource_table *cached_table;
548*4882a593Smuzhiyun size_t table_sz;
549*4882a593Smuzhiyun bool has_iommu;
550*4882a593Smuzhiyun bool auto_boot;
551*4882a593Smuzhiyun bool autonomous;
552*4882a593Smuzhiyun struct list_head dump_segments;
553*4882a593Smuzhiyun int nb_vdev;
554*4882a593Smuzhiyun u8 elf_class;
555*4882a593Smuzhiyun u16 elf_machine;
556*4882a593Smuzhiyun struct cdev cdev;
557*4882a593Smuzhiyun bool cdev_put_on_release;
558*4882a593Smuzhiyun };
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun /**
561*4882a593Smuzhiyun * struct rproc_subdev - subdevice tied to a remoteproc
562*4882a593Smuzhiyun * @node: list node related to the rproc subdevs list
563*4882a593Smuzhiyun * @prepare: prepare function, called before the rproc is started
564*4882a593Smuzhiyun * @start: start function, called after the rproc has been started
565*4882a593Smuzhiyun * @stop: stop function, called before the rproc is stopped; the @crashed
566*4882a593Smuzhiyun * parameter indicates if this originates from a recovery
567*4882a593Smuzhiyun * @unprepare: unprepare function, called after the rproc has been stopped
568*4882a593Smuzhiyun */
569*4882a593Smuzhiyun struct rproc_subdev {
570*4882a593Smuzhiyun struct list_head node;
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun int (*prepare)(struct rproc_subdev *subdev);
573*4882a593Smuzhiyun int (*start)(struct rproc_subdev *subdev);
574*4882a593Smuzhiyun void (*stop)(struct rproc_subdev *subdev, bool crashed);
575*4882a593Smuzhiyun void (*unprepare)(struct rproc_subdev *subdev);
576*4882a593Smuzhiyun };
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun /* we currently support only two vrings per rvdev */
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun #define RVDEV_NUM_VRINGS 2
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun /**
583*4882a593Smuzhiyun * struct rproc_vring - remoteproc vring state
584*4882a593Smuzhiyun * @va: virtual address
585*4882a593Smuzhiyun * @len: length, in bytes
586*4882a593Smuzhiyun * @da: device address
587*4882a593Smuzhiyun * @align: vring alignment
588*4882a593Smuzhiyun * @notifyid: rproc-specific unique vring index
589*4882a593Smuzhiyun * @rvdev: remote vdev
590*4882a593Smuzhiyun * @vq: the virtqueue of this vring
591*4882a593Smuzhiyun */
592*4882a593Smuzhiyun struct rproc_vring {
593*4882a593Smuzhiyun void *va;
594*4882a593Smuzhiyun int len;
595*4882a593Smuzhiyun u32 da;
596*4882a593Smuzhiyun u32 align;
597*4882a593Smuzhiyun int notifyid;
598*4882a593Smuzhiyun struct rproc_vdev *rvdev;
599*4882a593Smuzhiyun struct virtqueue *vq;
600*4882a593Smuzhiyun };
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun /**
603*4882a593Smuzhiyun * struct rproc_vdev - remoteproc state for a supported virtio device
604*4882a593Smuzhiyun * @refcount: reference counter for the vdev and vring allocations
605*4882a593Smuzhiyun * @subdev: handle for registering the vdev as a rproc subdevice
606*4882a593Smuzhiyun * @id: virtio device id (as in virtio_ids.h)
607*4882a593Smuzhiyun * @node: list node
608*4882a593Smuzhiyun * @rproc: the rproc handle
609*4882a593Smuzhiyun * @vdev: the virio device
610*4882a593Smuzhiyun * @vring: the vrings for this vdev
611*4882a593Smuzhiyun * @rsc_offset: offset of the vdev's resource entry
612*4882a593Smuzhiyun * @index: vdev position versus other vdev declared in resource table
613*4882a593Smuzhiyun */
614*4882a593Smuzhiyun struct rproc_vdev {
615*4882a593Smuzhiyun struct kref refcount;
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun struct rproc_subdev subdev;
618*4882a593Smuzhiyun struct device dev;
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun unsigned int id;
621*4882a593Smuzhiyun struct list_head node;
622*4882a593Smuzhiyun struct rproc *rproc;
623*4882a593Smuzhiyun struct rproc_vring vring[RVDEV_NUM_VRINGS];
624*4882a593Smuzhiyun u32 rsc_offset;
625*4882a593Smuzhiyun u32 index;
626*4882a593Smuzhiyun };
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun struct rproc *rproc_get_by_phandle(phandle phandle);
629*4882a593Smuzhiyun struct rproc *rproc_get_by_child(struct device *dev);
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun struct rproc *rproc_alloc(struct device *dev, const char *name,
632*4882a593Smuzhiyun const struct rproc_ops *ops,
633*4882a593Smuzhiyun const char *firmware, int len);
634*4882a593Smuzhiyun void rproc_put(struct rproc *rproc);
635*4882a593Smuzhiyun int rproc_add(struct rproc *rproc);
636*4882a593Smuzhiyun int rproc_del(struct rproc *rproc);
637*4882a593Smuzhiyun void rproc_free(struct rproc *rproc);
638*4882a593Smuzhiyun void rproc_resource_cleanup(struct rproc *rproc);
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun struct rproc *devm_rproc_alloc(struct device *dev, const char *name,
641*4882a593Smuzhiyun const struct rproc_ops *ops,
642*4882a593Smuzhiyun const char *firmware, int len);
643*4882a593Smuzhiyun int devm_rproc_add(struct device *dev, struct rproc *rproc);
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem);
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun struct rproc_mem_entry *
648*4882a593Smuzhiyun rproc_mem_entry_init(struct device *dev,
649*4882a593Smuzhiyun void *va, dma_addr_t dma, size_t len, u32 da,
650*4882a593Smuzhiyun int (*alloc)(struct rproc *, struct rproc_mem_entry *),
651*4882a593Smuzhiyun int (*release)(struct rproc *, struct rproc_mem_entry *),
652*4882a593Smuzhiyun const char *name, ...);
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun struct rproc_mem_entry *
655*4882a593Smuzhiyun rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, size_t len,
656*4882a593Smuzhiyun u32 da, const char *name, ...);
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun int rproc_boot(struct rproc *rproc);
659*4882a593Smuzhiyun void rproc_shutdown(struct rproc *rproc);
660*4882a593Smuzhiyun int rproc_set_firmware(struct rproc *rproc, const char *fw_name);
661*4882a593Smuzhiyun void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type);
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun /* from remoteproc_coredump.c */
664*4882a593Smuzhiyun void rproc_coredump_cleanup(struct rproc *rproc);
665*4882a593Smuzhiyun void rproc_coredump(struct rproc *rproc);
666*4882a593Smuzhiyun void rproc_coredump_using_sections(struct rproc *rproc);
667*4882a593Smuzhiyun int rproc_coredump_add_segment(struct rproc *rproc, dma_addr_t da, size_t size);
668*4882a593Smuzhiyun int rproc_coredump_add_custom_segment(struct rproc *rproc,
669*4882a593Smuzhiyun dma_addr_t da, size_t size,
670*4882a593Smuzhiyun void (*dumpfn)(struct rproc *rproc,
671*4882a593Smuzhiyun struct rproc_dump_segment *segment,
672*4882a593Smuzhiyun void *dest, size_t offset,
673*4882a593Smuzhiyun size_t size),
674*4882a593Smuzhiyun void *priv);
675*4882a593Smuzhiyun int rproc_coredump_set_elf_info(struct rproc *rproc, u8 class, u16 machine);
676*4882a593Smuzhiyun
vdev_to_rvdev(struct virtio_device * vdev)677*4882a593Smuzhiyun static inline struct rproc_vdev *vdev_to_rvdev(struct virtio_device *vdev)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun return container_of(vdev->dev.parent, struct rproc_vdev, dev);
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun
vdev_to_rproc(struct virtio_device * vdev)682*4882a593Smuzhiyun static inline struct rproc *vdev_to_rproc(struct virtio_device *vdev)
683*4882a593Smuzhiyun {
684*4882a593Smuzhiyun struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun return rvdev->rproc;
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev);
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev);
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun #endif /* REMOTEPROC_H */
694