xref: /linux/include/linux/rsc_table.h (revision 1620ddc0dcefc6ffb85f4718086e880335d1a7f5)
1 /*
2  * Resource table and its types data structure
3  *
4  * Copyright(c) 2011 Texas Instruments, Inc.
5  * Copyright(c) 2011 Google, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  *   notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  * * Neither the name Texas Instruments nor the names of its
19  *   contributors may be used to endorse or promote products derived
20  *   from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef RSC_TABLE_H
36 #define RSC_TABLE_H
37 
38 /**
39  * struct resource_table - firmware resource table header
40  * @ver: version number
41  * @num: number of resource entries
42  * @reserved: reserved (must be zero)
43  * @offset: array of offsets pointing at the various resource entries
44  *
45  * A resource table is essentially a list of system resources required
46  * by the remote processor. It may also include configuration entries.
47  * If needed, the remote processor firmware should contain this table
48  * as a dedicated ".resource_table" ELF section.
49  *
50  * Some resources entries are mere announcements, where the host is informed
51  * of specific remoteproc configuration. Other entries require the host to
52  * do something (e.g. allocate a system resource). Sometimes a negotiation
53  * is expected, where the firmware requests a resource, and once allocated,
54  * the host should provide back its details (e.g. address of an allocated
55  * memory region).
56  *
57  * The header of the resource table, as expressed by this structure,
58  * contains a version number (should we need to change this format in the
59  * future), the number of available resource entries, and their offsets
60  * in the table.
61  *
62  * Immediately following this header are the resource entries themselves,
63  * each of which begins with a resource entry header (as described below).
64  */
65 struct resource_table {
66 	u32 ver;
67 	u32 num;
68 	u32 reserved[2];
69 	u32 offset[];
70 } __packed;
71 
72 /**
73  * struct fw_rsc_hdr - firmware resource entry header
74  * @type: resource type
75  * @data: resource data
76  *
77  * Every resource entry begins with a 'struct fw_rsc_hdr' header providing
78  * its @type. The content of the entry itself will immediately follow
79  * this header, and it should be parsed according to the resource type.
80  */
81 struct fw_rsc_hdr {
82 	u32 type;
83 	u8 data[];
84 } __packed;
85 
86 /**
87  * enum fw_resource_type - types of resource entries
88  *
89  * @RSC_CARVEOUT:   request for allocation of a physically contiguous
90  *		    memory region.
91  * @RSC_DEVMEM:     request to iommu_map a memory-based peripheral.
92  * @RSC_TRACE:	    announces the availability of a trace buffer into which
93  *		    the remote processor will be writing logs.
94  * @RSC_VDEV:       declare support for a virtio device, and serve as its
95  *		    virtio header.
96  * @RSC_LAST:       just keep this one at the end of standard resources
97  * @RSC_VENDOR_START:	start of the vendor specific resource types range
98  * @RSC_VENDOR_END:	end of the vendor specific resource types range
99  *
100  * For more details regarding a specific resource type, please see its
101  * dedicated structure below.
102  *
103  * Please note that these values are used as indices to the rproc_handle_rsc
104  * lookup table, so please keep them sane. Moreover, @RSC_LAST is used to
105  * check the validity of an index before the lookup table is accessed, so
106  * please update it as needed.
107  */
108 enum fw_resource_type {
109 	RSC_CARVEOUT		= 0,
110 	RSC_DEVMEM		= 1,
111 	RSC_TRACE		= 2,
112 	RSC_VDEV		= 3,
113 	RSC_LAST		= 4,
114 	RSC_VENDOR_START	= 128,
115 	RSC_VENDOR_END		= 512,
116 };
117 
118 #define FW_RSC_ADDR_ANY (-1)
119 
120 /**
121  * struct fw_rsc_carveout - physically contiguous memory request
122  * @da: device address
123  * @pa: physical address
124  * @len: length (in bytes)
125  * @flags: iommu protection flags
126  * @reserved: reserved (must be zero)
127  * @name: human-readable name of the requested memory region
128  *
129  * This resource entry requests the host to allocate a physically contiguous
130  * memory region.
131  *
132  * These request entries should precede other firmware resource entries,
133  * as other entries might request placing other data objects inside
134  * these memory regions (e.g. data/code segments, trace resource entries, ...).
135  *
136  * Allocating memory this way helps utilizing the reserved physical memory
137  * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
138  * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
139  * pressure is important; it may have a substantial impact on performance.
140  *
141  * If the firmware is compiled with static addresses, then @da should specify
142  * the expected device address of this memory region. If @da is set to
143  * FW_RSC_ADDR_ANY, then the host will dynamically allocate it, and then
144  * overwrite @da with the dynamically allocated address.
145  *
146  * We will always use @da to negotiate the device addresses, even if it
147  * isn't using an iommu. In that case, though, it will obviously contain
148  * physical addresses.
149  *
150  * Some remote processors needs to know the allocated physical address
151  * even if they do use an iommu. This is needed, e.g., if they control
152  * hardware accelerators which access the physical memory directly (this
153  * is the case with OMAP4 for instance). In that case, the host will
154  * overwrite @pa with the dynamically allocated physical address.
155  * Generally we don't want to expose physical addresses if we don't have to
156  * (remote processors are generally _not_ trusted), so we might want to
157  * change this to happen _only_ when explicitly required by the hardware.
158  *
159  * @flags is used to provide IOMMU protection flags, and @name should
160  * (optionally) contain a human readable name of this carveout region
161  * (mainly for debugging purposes).
162  */
163 struct fw_rsc_carveout {
164 	u32 da;
165 	u32 pa;
166 	u32 len;
167 	u32 flags;
168 	u32 reserved;
169 	u8 name[32];
170 } __packed;
171 
172 /**
173  * struct fw_rsc_devmem - iommu mapping request
174  * @da: device address
175  * @pa: physical address
176  * @len: length (in bytes)
177  * @flags: iommu protection flags
178  * @reserved: reserved (must be zero)
179  * @name: human-readable name of the requested region to be mapped
180  *
181  * This resource entry requests the host to iommu map a physically contiguous
182  * memory region. This is needed in case the remote processor requires
183  * access to certain memory-based peripherals; _never_ use it to access
184  * regular memory.
185  *
186  * This is obviously only needed if the remote processor is accessing memory
187  * via an iommu.
188  *
189  * @da should specify the required device address, @pa should specify
190  * the physical address we want to map, @len should specify the size of
191  * the mapping and @flags is the IOMMU protection flags. As always, @name may
192  * (optionally) contain a human readable name of this mapping (mainly for
193  * debugging purposes).
194  *
195  * Note: at this point we just "trust" those devmem entries to contain valid
196  * physical addresses, but this isn't safe and will be changed: eventually we
197  * want remoteproc implementations to provide us ranges of physical addresses
198  * the firmware is allowed to request, and not allow firmwares to request
199  * access to physical addresses that are outside those ranges.
200  */
201 struct fw_rsc_devmem {
202 	u32 da;
203 	u32 pa;
204 	u32 len;
205 	u32 flags;
206 	u32 reserved;
207 	u8 name[32];
208 } __packed;
209 
210 /**
211  * struct fw_rsc_trace - trace buffer declaration
212  * @da: device address
213  * @len: length (in bytes)
214  * @reserved: reserved (must be zero)
215  * @name: human-readable name of the trace buffer
216  *
217  * This resource entry provides the host information about a trace buffer
218  * into which the remote processor will write log messages.
219  *
220  * @da specifies the device address of the buffer, @len specifies
221  * its size, and @name may contain a human readable name of the trace buffer.
222  *
223  * After booting the remote processor, the trace buffers are exposed to the
224  * user via debugfs entries (called trace0, trace1, etc..).
225  */
226 struct fw_rsc_trace {
227 	u32 da;
228 	u32 len;
229 	u32 reserved;
230 	u8 name[32];
231 } __packed;
232 
233 /**
234  * struct fw_rsc_vdev_vring - vring descriptor entry
235  * @da: device address
236  * @align: the alignment between the consumer and producer parts of the vring
237  * @num: num of buffers supported by this vring (must be power of two)
238  * @notifyid: a unique rproc-wide notify index for this vring. This notify
239  * index is used when kicking a remote processor, to let it know that this
240  * vring is triggered.
241  * @pa: physical address
242  *
243  * This descriptor is not a resource entry by itself; it is part of the
244  * vdev resource type (see below).
245  *
246  * Note that @da should either contain the device address where
247  * the remote processor is expecting the vring, or indicate that
248  * dynamically allocation of the vring's device address is supported.
249  */
250 struct fw_rsc_vdev_vring {
251 	u32 da;
252 	u32 align;
253 	u32 num;
254 	u32 notifyid;
255 	u32 pa;
256 } __packed;
257 
258 /**
259  * struct fw_rsc_vdev - virtio device header
260  * @id: virtio device id (as in virtio_ids.h)
261  * @notifyid: a unique rproc-wide notify index for this vdev. This notify
262  * index is used when kicking a remote processor, to let it know that the
263  * status/features of this vdev have changes.
264  * @dfeatures: specifies the virtio device features supported by the firmware
265  * @gfeatures: a place holder used by the host to write back the
266  * negotiated features that are supported by both sides.
267  * @config_len: the size of the virtio config space of this vdev. The config
268  * space lies in the resource table immediate after this vdev header.
269  * @status: a place holder where the host will indicate its virtio progress.
270  * @num_of_vrings: indicates how many vrings are described in this vdev header
271  * @reserved: reserved (must be zero)
272  * @vring: an array of @num_of_vrings entries of 'struct fw_rsc_vdev_vring'.
273  *
274  * This resource is a virtio device header: it provides information about
275  * the vdev, and is then used by the host and its peer remote processors
276  * to negotiate and share certain virtio properties.
277  *
278  * By providing this resource entry, the firmware essentially asks remoteproc
279  * to statically allocate a vdev upon registration of the rproc (dynamic vdev
280  * allocation is not yet supported).
281  *
282  * Note:
283  * 1. unlike virtualization systems, the term 'host' here means
284  *    the Linux side which is running remoteproc to control the remote
285  *    processors. We use the name 'gfeatures' to comply with virtio's terms,
286  *    though there isn't really any virtualized guest OS here: it's the host
287  *    which is responsible for negotiating the final features.
288  *    Yeah, it's a bit confusing.
289  *
290  * 2. immediately following this structure is the virtio config space for
291  *    this vdev (which is specific to the vdev; for more info, read the virtio
292  *    spec). The size of the config space is specified by @config_len.
293  */
294 struct fw_rsc_vdev {
295 	u32 id;
296 	u32 notifyid;
297 	u32 dfeatures;
298 	u32 gfeatures;
299 	u32 config_len;
300 	u8 status;
301 	u8 num_of_vrings;
302 	u8 reserved[2];
303 	struct fw_rsc_vdev_vring vring[];
304 } __packed;
305 
306 /**
307  * rsc_table_for_each_entry() - iterate over all entries in a resource table
308  * @table:    pointer to the resource table
309  * @table_sz: total size of the table buffer in bytes
310  * @dev:      device used for error logging
311  * @cb:       callback invoked for each entry:
312  *              @type   - value from enum fw_resource_type
313  *              @rsc    - pointer to the entry payload (past struct fw_rsc_hdr)
314  *              @offset - byte offset of the payload within the table; callers
315  *                        that write back into the table (e.g. to record a
316  *                        dynamically allocated address) use this to locate the
317  *                        entry for later update
318  *              @avail  - bytes available in the payload
319  *              @data   - caller-supplied private pointer
320  *            Return 0 to continue iteration, non-zero to stop.
321  * @data:     private pointer forwarded to @cb on every call
322  *
323  * Iterates over every resource entry in @table, performing the standard
324  * truncation check, and invokes @cb for each one. Iteration stops on the
325  * first non-zero return from @cb or on a malformed table.
326  *
327  * Returns 0 after a complete iteration, -EINVAL if the table is truncated,
328  * or the first non-zero value returned by @cb.
329  */
330 static inline int rsc_table_for_each_entry(struct resource_table *table,
331 					   size_t table_sz,
332 					   struct device *dev,
333 					   int (*cb)(u32 type, void *rsc,
334 						     int offset, int avail,
335 						     void *data),
336 					   void *data) {
337 	int i, ret;
338 
339 	for (i = 0; i < table->num; i++) {
340 		int offset = table->offset[i];
341 		struct fw_rsc_hdr *hdr = (void *)table + offset;
342 		int avail = table_sz - offset - sizeof(*hdr);
343 		int rsc_offset = offset + sizeof(*hdr);
344 		void *rsc = (void *)hdr + sizeof(*hdr);
345 
346 		if (avail < 0) {
347 			dev_err(dev, "rsc table is truncated\n");
348 			return -EINVAL;
349 		}
350 
351 		ret = cb(hdr->type, rsc, rsc_offset, avail, data);
352 		if (ret)
353 			return ret;
354 	}
355 
356 	return 0;
357 }
358 
359 #endif /* RSC_TABLE_H */
360