1 /*
2  * Remote Processor Framework
3  *
4  * Copyright(c) 2011 Texas Instruments, Inc.
5  * Copyright(c) 2011-2015 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 REMOTEPROC_H
36 #define REMOTEPROC_H
37 
38 #include <linux/types.h>
39 
40 #ifdef __KERNEL__
41 #include <linux/klist.h>
42 #include <linux/mutex.h>
43 #include <linux/virtio.h>
44 #include <linux/completion.h>
45 #include <linux/idr.h>
46 #endif /* __KERNEL__  */
47 
48 /**
49  * struct resource_table - firmware resource table header
50  * @ver: version number
51  * @num: number of resource entries
52  * @reserved: reserved (must be zero)
53  * @offset: array of offsets pointing at the various resource entries
54  *
55  * A resource table is essentially a list of system resources required
56  * by the remote processor. It may also include configuration entries.
57  * If needed, the remote processor firmware should contain this table
58  * as a dedicated ".resource_table" ELF section.
59  *
60  * Some resources entries are mere announcements, where the host is informed
61  * of specific remoteproc configuration. Other entries require the host to
62  * do something (e.g. allocate a system resource). Sometimes a negotiation
63  * is expected, where the firmware requests a resource, and once allocated,
64  * the host should provide back its details (e.g. address of an allocated
65  * memory region).
66  *
67  * The header of the resource table, as expressed by this structure,
68  * contains a version number (should we need to change this format in the
69  * future), the number of available resource entries, and their offsets
70  * in the table.
71  *
72  * Immediately following this header are the resource entries themselves,
73  * each of which begins with a resource entry header (as described below).
74  */
75 struct resource_table {
76 	u32 ver;
77 	u32 num;
78 	u32 reserved[2];
79 	u32 offset[0];
80 } __packed;
81 
82 /**
83  * struct fw_rsc_hdr - firmware resource entry header
84  * @type: resource type
85  * @data: resource data
86  *
87  * Every resource entry begins with a 'struct fw_rsc_hdr' header providing
88  * its @type. The content of the entry itself will immediately follow
89  * this header, and it should be parsed according to the resource type.
90  */
91 struct fw_rsc_hdr {
92 	u32 type;
93 	u8 data[0];
94 } __packed;
95 
96 /**
97  * enum fw_resource_type - types of resource entries
98  *
99  * @RSC_CARVEOUT:   request for allocation of a physically contiguous
100  *		    memory region.
101  * @RSC_DEVMEM:     request to iommu_map a memory-based peripheral.
102  * @RSC_TRACE:	    announces the availability of a trace buffer into which
103  *		    the remote processor will be writing logs.
104  * @RSC_VDEV:       declare support for a virtio device, and serve as its
105  *		    virtio header.
106  * @RSC_LAST:       just keep this one at the end
107  *
108  * For more details regarding a specific resource type, please see its
109  * dedicated structure below.
110  *
111  * Please note that these values are used as indices to the rproc_handle_rsc
112  * lookup table, so please keep them sane. Moreover, @RSC_LAST is used to
113  * check the validity of an index before the lookup table is accessed, so
114  * please update it as needed.
115  */
116 enum fw_resource_type {
117 	RSC_CARVEOUT	= 0,
118 	RSC_DEVMEM	= 1,
119 	RSC_TRACE	= 2,
120 	RSC_VDEV	= 3,
121 	RSC_LAST	= 4,
122 };
123 
124 #define FW_RSC_ADDR_ANY (0xFFFFFFFFFFFFFFFF)
125 
126 /**
127  * struct fw_rsc_carveout - physically contiguous memory request
128  * @da: device address
129  * @pa: physical address
130  * @len: length (in bytes)
131  * @flags: iommu protection flags
132  * @reserved: reserved (must be zero)
133  * @name: human-readable name of the requested memory region
134  *
135  * This resource entry requests the host to allocate a physically contiguous
136  * memory region.
137  *
138  * These request entries should precede other firmware resource entries,
139  * as other entries might request placing other data objects inside
140  * these memory regions (e.g. data/code segments, trace resource entries, ...).
141  *
142  * Allocating memory this way helps utilizing the reserved physical memory
143  * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
144  * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
145  * pressure is important; it may have a substantial impact on performance.
146  *
147  * If the firmware is compiled with static addresses, then @da should specify
148  * the expected device address of this memory region. If @da is set to
149  * FW_RSC_ADDR_ANY, then the host will dynamically allocate it, and then
150  * overwrite @da with the dynamically allocated address.
151  *
152  * We will always use @da to negotiate the device addresses, even if it
153  * isn't using an iommu. In that case, though, it will obviously contain
154  * physical addresses.
155  *
156  * Some remote processors needs to know the allocated physical address
157  * even if they do use an iommu. This is needed, e.g., if they control
158  * hardware accelerators which access the physical memory directly (this
159  * is the case with OMAP4 for instance). In that case, the host will
160  * overwrite @pa with the dynamically allocated physical address.
161  * Generally we don't want to expose physical addresses if we don't have to
162  * (remote processors are generally _not_ trusted), so we might want to
163  * change this to happen _only_ when explicitly required by the hardware.
164  *
165  * @flags is used to provide IOMMU protection flags, and @name should
166  * (optionally) contain a human readable name of this carveout region
167  * (mainly for debugging purposes).
168  */
169 struct fw_rsc_carveout {
170 	u32 da;
171 	u32 pa;
172 	u32 len;
173 	u32 flags;
174 	u32 reserved;
175 	u8 name[32];
176 } __packed;
177 
178 /**
179  * struct fw_rsc_devmem - iommu mapping request
180  * @da: device address
181  * @pa: physical address
182  * @len: length (in bytes)
183  * @flags: iommu protection flags
184  * @reserved: reserved (must be zero)
185  * @name: human-readable name of the requested region to be mapped
186  *
187  * This resource entry requests the host to iommu map a physically contiguous
188  * memory region. This is needed in case the remote processor requires
189  * access to certain memory-based peripherals; _never_ use it to access
190  * regular memory.
191  *
192  * This is obviously only needed if the remote processor is accessing memory
193  * via an iommu.
194  *
195  * @da should specify the required device address, @pa should specify
196  * the physical address we want to map, @len should specify the size of
197  * the mapping and @flags is the IOMMU protection flags. As always, @name may
198  * (optionally) contain a human readable name of this mapping (mainly for
199  * debugging purposes).
200  *
201  * Note: at this point we just "trust" those devmem entries to contain valid
202  * physical addresses, but this isn't safe and will be changed: eventually we
203  * want remoteproc implementations to provide us ranges of physical addresses
204  * the firmware is allowed to request, and not allow firmwares to request
205  * access to physical addresses that are outside those ranges.
206  */
207 struct fw_rsc_devmem {
208 	u32 da;
209 	u32 pa;
210 	u32 len;
211 	u32 flags;
212 	u32 reserved;
213 	u8 name[32];
214 } __packed;
215 
216 /**
217  * struct fw_rsc_trace - trace buffer declaration
218  * @da: device address
219  * @len: length (in bytes)
220  * @reserved: reserved (must be zero)
221  * @name: human-readable name of the trace buffer
222  *
223  * This resource entry provides the host information about a trace buffer
224  * into which the remote processor will write log messages.
225  *
226  * @da specifies the device address of the buffer, @len specifies
227  * its size, and @name may contain a human readable name of the trace buffer.
228  *
229  * After booting the remote processor, the trace buffers are exposed to the
230  * user via debugfs entries (called trace0, trace1, etc..).
231  */
232 struct fw_rsc_trace {
233 	u32 da;
234 	u32 len;
235 	u32 reserved;
236 	u8 name[32];
237 } __packed;
238 
239 /**
240  * struct fw_rsc_vdev_vring - vring descriptor entry
241  * @da: device address
242  * @align: the alignment between the consumer and producer parts of the vring
243  * @num: num of buffers supported by this vring (must be power of two)
244  * @notifyid is a unique rproc-wide notify index for this vring. This notify
245  * index is used when kicking a remote processor, to let it know that this
246  * vring is triggered.
247  * @reserved: reserved (must be zero)
248  *
249  * This descriptor is not a resource entry by itself; it is part of the
250  * vdev resource type (see below).
251  *
252  * Note that @da should either contain the device address where
253  * the remote processor is expecting the vring, or indicate that
254  * dynamically allocation of the vring's device address is supported.
255  */
256 struct fw_rsc_vdev_vring {
257 	u32 da;
258 	u32 align;
259 	u32 num;
260 	u32 notifyid;
261 	u32 reserved;
262 } __packed;
263 
264 /**
265  * struct fw_rsc_vdev - virtio device header
266  * @id: virtio device id (as in virtio_ids.h)
267  * @notifyid is a unique rproc-wide notify index for this vdev. This notify
268  * index is used when kicking a remote processor, to let it know that the
269  * status/features of this vdev have changes.
270  * @dfeatures specifies the virtio device features supported by the firmware
271  * @gfeatures is a place holder used by the host to write back the
272  * negotiated features that are supported by both sides.
273  * @config_len is the size of the virtio config space of this vdev. The config
274  * space lies in the resource table immediate after this vdev header.
275  * @status is a place holder where the host will indicate its virtio progress.
276  * @num_of_vrings indicates how many vrings are described in this vdev header
277  * @reserved: reserved (must be zero)
278  * @vring is an array of @num_of_vrings entries of 'struct fw_rsc_vdev_vring'.
279  *
280  * This resource is a virtio device header: it provides information about
281  * the vdev, and is then used by the host and its peer remote processors
282  * to negotiate and share certain virtio properties.
283  *
284  * By providing this resource entry, the firmware essentially asks remoteproc
285  * to statically allocate a vdev upon registration of the rproc (dynamic vdev
286  * allocation is not yet supported).
287  *
288  * Note: unlike virtualization systems, the term 'host' here means
289  * the Linux side which is running remoteproc to control the remote
290  * processors. We use the name 'gfeatures' to comply with virtio's terms,
291  * though there isn't really any virtualized guest OS here: it's the host
292  * which is responsible for negotiating the final features.
293  * Yeah, it's a bit confusing.
294  *
295  * Note: immediately following this structure is the virtio config space for
296  * this vdev (which is specific to the vdev; for more info, read the virtio
297  * spec). the size of the config space is specified by @config_len.
298  */
299 struct fw_rsc_vdev {
300 	u32 id;
301 	u32 notifyid;
302 	u32 dfeatures;
303 	u32 gfeatures;
304 	u32 config_len;
305 	u8 status;
306 	u8 num_of_vrings;
307 	u8 reserved[2];
308 	struct fw_rsc_vdev_vring vring[0];
309 } __packed;
310 
311 
312 #ifdef __KERNEL__
313 
314 /**
315  * struct rproc_mem_entry - memory entry descriptor
316  * @va:	virtual address
317  * @dma: dma address
318  * @len: length, in bytes
319  * @da: device address
320  * @priv: associated data
321  * @node: list node
322  */
323 struct rproc_mem_entry {
324 	void *va;
325 	dma_addr_t dma;
326 	int len;
327 	u32 da;
328 	void *priv;
329 	struct list_head node;
330 };
331 
332 struct rproc;
333 
334 /**
335  * struct rproc_ops - platform-specific device handlers
336  * @start:	power on the device and boot it
337  * @stop:	power off the device
338  * @kick:	kick a virtqueue (virtqueue id given as a parameter)
339  */
340 struct rproc_ops {
341 	int (*start)(struct rproc *rproc);
342 	int (*stop)(struct rproc *rproc);
343 	void (*kick)(struct rproc *rproc, int vqid);
344 };
345 
346 /**
347  * enum rproc_state - remote processor states
348  * @RPROC_OFFLINE:	device is powered off
349  * @RPROC_SUSPENDED:	device is suspended; needs to be woken up to receive
350  *			a message.
351  * @RPROC_RUNNING:	device is up and running
352  * @RPROC_CRASHED:	device has crashed; need to start recovery
353  * @RPROC_LAST:		just keep this one at the end
354  *
355  * Please note that the values of these states are used as indices
356  * to rproc_state_string, a state-to-name lookup table,
357  * so please keep the two synchronized. @RPROC_LAST is used to check
358  * the validity of an index before the lookup table is accessed, so
359  * please update it as needed too.
360  */
361 enum rproc_state {
362 	RPROC_OFFLINE	= 0,
363 	RPROC_SUSPENDED	= 1,
364 	RPROC_RUNNING	= 2,
365 	RPROC_CRASHED	= 3,
366 	RPROC_LAST	= 4,
367 };
368 
369 /**
370  * enum rproc_crash_type - remote processor crash types
371  * @RPROC_MMUFAULT:	iommu fault
372  *
373  * Each element of the enum is used as an array index. So that, the value of
374  * the elements should be always something sane.
375  *
376  * Feel free to add more types when needed.
377  */
378 enum rproc_crash_type {
379 	RPROC_MMUFAULT,
380 };
381 
382 /**
383  * struct rproc - represents a physical remote processor device
384  * @node: klist node of this rproc object
385  * @domain: iommu domain
386  * @name: human readable name of the rproc
387  * @firmware: name of firmware file to be loaded
388  * @priv: private data which belongs to the platform-specific rproc module
389  * @ops: platform-specific start/stop rproc handlers
390  * @dev: virtual device for refcounting and common remoteproc behavior
391  * @fw_ops: firmware-specific handlers
392  * @power: refcount of users who need this rproc powered up
393  * @state: state of the device
394  * @lock: lock which protects concurrent manipulations of the rproc
395  * @dbg_dir: debugfs directory of this rproc device
396  * @traces: list of trace buffers
397  * @num_traces: number of trace buffers
398  * @carveouts: list of physically contiguous memory allocations
399  * @mappings: list of iommu mappings we initiated, needed on shutdown
400  * @firmware_loading_complete: marks e/o asynchronous firmware loading
401  * @bootaddr: address of first instruction to boot rproc with (optional)
402  * @rvdevs: list of remote virtio devices
403  * @notifyids: idr for dynamically assigning rproc-wide unique notify ids
404  * @index: index of this rproc device
405  * @crash_handler: workqueue for handling a crash
406  * @crash_cnt: crash counter
407  * @crash_comp: completion used to sync crash handler and the rproc reload
408  * @recovery_disabled: flag that state if recovery was disabled
409  * @max_notifyid: largest allocated notify id.
410  * @table_ptr: pointer to the resource table in effect
411  * @cached_table: copy of the resource table
412  * @table_csum: checksum of the resource table
413  */
414 struct rproc {
415 	struct klist_node node;
416 	struct iommu_domain *domain;
417 	const char *name;
418 	const char *firmware;
419 	void *priv;
420 	const struct rproc_ops *ops;
421 	struct device dev;
422 	const struct rproc_fw_ops *fw_ops;
423 	atomic_t power;
424 	unsigned int state;
425 	struct mutex lock;
426 	struct dentry *dbg_dir;
427 	struct list_head traces;
428 	int num_traces;
429 	struct list_head carveouts;
430 	struct list_head mappings;
431 	struct completion firmware_loading_complete;
432 	u32 bootaddr;
433 	struct list_head rvdevs;
434 	struct idr notifyids;
435 	int index;
436 	struct work_struct crash_handler;
437 	unsigned crash_cnt;
438 	struct completion crash_comp;
439 	bool recovery_disabled;
440 	int max_notifyid;
441 	struct resource_table *table_ptr;
442 	struct resource_table *cached_table;
443 	u32 table_csum;
444 };
445 
446 /* we currently support only two vrings per rvdev */
447 
448 #define RVDEV_NUM_VRINGS 2
449 
450 /**
451  * struct rproc_vring - remoteproc vring state
452  * @va:	virtual address
453  * @dma: dma address
454  * @len: length, in bytes
455  * @da: device address
456  * @align: vring alignment
457  * @notifyid: rproc-specific unique vring index
458  * @rvdev: remote vdev
459  * @vq: the virtqueue of this vring
460  */
461 struct rproc_vring {
462 	void *va;
463 	dma_addr_t dma;
464 	int len;
465 	u32 da;
466 	u32 align;
467 	int notifyid;
468 	struct rproc_vdev *rvdev;
469 	struct virtqueue *vq;
470 };
471 
472 /**
473  * struct rproc_vdev - remoteproc state for a supported virtio device
474  * @node: list node
475  * @rproc: the rproc handle
476  * @vdev: the virio device
477  * @vring: the vrings for this vdev
478  * @rsc_offset: offset of the vdev's resource entry
479  */
480 struct rproc_vdev {
481 	struct list_head node;
482 	struct rproc *rproc;
483 	struct virtio_device vdev;
484 	struct rproc_vring vring[RVDEV_NUM_VRINGS];
485 	u32 rsc_offset;
486 };
487 
488 struct rproc *rproc_alloc(struct device *dev, const char *name,
489 				const struct rproc_ops *ops,
490 				const char *firmware, int len);
491 void rproc_put(struct rproc *rproc);
492 int rproc_add(struct rproc *rproc);
493 int rproc_del(struct rproc *rproc);
494 
495 int rproc_boot(struct rproc *rproc);
496 void rproc_shutdown(struct rproc *rproc);
497 void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type);
498 
vdev_to_rvdev(struct virtio_device * vdev)499 static inline struct rproc_vdev *vdev_to_rvdev(struct virtio_device *vdev)
500 {
501 	return container_of(vdev, struct rproc_vdev, vdev);
502 }
503 
vdev_to_rproc(struct virtio_device * vdev)504 static inline struct rproc *vdev_to_rproc(struct virtio_device *vdev)
505 {
506 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
507 
508 	return rvdev->rproc;
509 }
510 
511 #endif /* __KERNEL__ */
512 
513 #endif /* REMOTEPROC_H */
514