xref: /openbmc/u-boot/arch/sandbox/include/asm/state.h (revision 0c41e59a37fbd5b10d4837ae30c288a084997465)
183d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */
26fb62078SSimon Glass /*
36fb62078SSimon Glass  * Copyright (c) 2011-2012 The Chromium OS Authors.
46fb62078SSimon Glass  */
56fb62078SSimon Glass 
66fb62078SSimon Glass #ifndef __SANDBOX_STATE_H
76fb62078SSimon Glass #define __SANDBOX_STATE_H
86fb62078SSimon Glass 
970db4212SSimon Glass #include <config.h>
1011636258SStephen Warren #include <sysreset.h>
11c5a62d4aSSimon Glass #include <stdbool.h>
12428aa0caSSimon Glass #include <linux/list.h>
131209e272SSimon Glass #include <linux/stringify.h>
1470db4212SSimon Glass 
15ffb87905SSimon Glass /**
16ffb87905SSimon Glass  * Selects the behavior of the serial terminal.
17ffb87905SSimon Glass  *
18ffb87905SSimon Glass  * If Ctrl-C is processed by U-Boot, then the only way to quit sandbox is with
19ffb87905SSimon Glass  * the 'reset' command, or equivalent.
20ffb87905SSimon Glass  *
21ffb87905SSimon Glass  * If the terminal is cooked, then Ctrl-C will terminate U-Boot, and the
22ffb87905SSimon Glass  * command line will not be quite such a faithful emulation.
23ffb87905SSimon Glass  *
24ffb87905SSimon Glass  * Options are:
25ffb87905SSimon Glass  *
26ffb87905SSimon Glass  *	raw-with-sigs		- Raw, but allow signals (Ctrl-C will quit)
27ffb87905SSimon Glass  *	raw			- Terminal is always raw
28ffb87905SSimon Glass  *	cooked			- Terminal is always cooked
29ffb87905SSimon Glass  */
30ffb87905SSimon Glass enum state_terminal_raw {
31ffb87905SSimon Glass 	STATE_TERM_RAW_WITH_SIGS,	/* Default */
32ffb87905SSimon Glass 	STATE_TERM_RAW,
33ffb87905SSimon Glass 	STATE_TERM_COOKED,
34ffb87905SSimon Glass 
35ffb87905SSimon Glass 	STATE_TERM_COUNT,
36ffb87905SSimon Glass };
37ffb87905SSimon Glass 
386122813fSMike Frysinger struct sandbox_spi_info {
3949b5d6e6SSimon Glass 	struct udevice *emul;
406122813fSMike Frysinger };
416122813fSMike Frysinger 
420753bc2dSmaxims@google.com struct sandbox_wdt_info {
430753bc2dSmaxims@google.com 	unsigned long long counter;
440753bc2dSmaxims@google.com 	uint reset_count;
450753bc2dSmaxims@google.com 	bool running;
460753bc2dSmaxims@google.com };
470753bc2dSmaxims@google.com 
48428aa0caSSimon Glass /**
49428aa0caSSimon Glass  * struct sandbox_mapmem_entry - maps pointers to/from U-Boot addresses
50428aa0caSSimon Glass  *
51428aa0caSSimon Glass  * When map_to_sysmem() is called with an address outside sandbox's emulated
52428aa0caSSimon Glass  * RAM, a record is created with a tag that can be used to reference that
53428aa0caSSimon Glass  * pointer. When map_sysmem() is called later with that tag, the pointer will
54428aa0caSSimon Glass  * be returned, just as it would for a normal sandbox address.
55428aa0caSSimon Glass  *
56428aa0caSSimon Glass  * @tag: Address tag (a value which U-Boot uses to refer to the address)
57428aa0caSSimon Glass  * @ptr: Associated pointer for that tag
58428aa0caSSimon Glass  */
59428aa0caSSimon Glass struct sandbox_mapmem_entry {
60428aa0caSSimon Glass 	ulong tag;
61428aa0caSSimon Glass 	void *ptr;
62428aa0caSSimon Glass 	struct list_head sibling_node;
63428aa0caSSimon Glass };
64428aa0caSSimon Glass 
656fb62078SSimon Glass /* The complete state of the test system */
666fb62078SSimon Glass struct sandbox_state {
676fb62078SSimon Glass 	const char *cmd;		/* Command to execute */
68c5a62d4aSSimon Glass 	bool interactive;		/* Enable cmdline after execute */
69ebaa832eSSjoerd Simons 	bool run_distro_boot;		/* Automatically run distro bootcommands */
70f828bf25SSimon Glass 	const char *fdt_fname;		/* Filename of FDT binary */
7170db4212SSimon Glass 	const char *parse_err;		/* Error to report from parsing */
7270db4212SSimon Glass 	int argc;			/* Program arguments */
73bda7773fSSimon Glass 	char **argv;			/* Command line arguments */
74ab839dc3SSimon Glass 	const char *jumped_fname;	/* Jumped from previous U_Boot */
755c2859cdSSimon Glass 	uint8_t *ram_buf;		/* Emulated RAM buffer */
765c2859cdSSimon Glass 	unsigned int ram_size;		/* Size of RAM buffer */
775c2859cdSSimon Glass 	const char *ram_buf_fname;	/* Filename to use for RAM buffer */
78ab839dc3SSimon Glass 	bool ram_buf_rm;		/* Remove RAM buffer file after read */
795c2859cdSSimon Glass 	bool write_ram_buf;		/* Write RAM buffer on exit */
801209e272SSimon Glass 	const char *state_fname;	/* File containing sandbox state */
811209e272SSimon Glass 	void *state_fdt;		/* Holds saved state for sandbox */
821209e272SSimon Glass 	bool read_state;		/* Read sandbox state on startup */
831209e272SSimon Glass 	bool write_state;		/* Write sandbox state on exit */
841209e272SSimon Glass 	bool ignore_missing_state_on_read;	/* No error if state missing */
857d95f2a3SSimon Glass 	bool show_lcd;			/* Show LCD on start-up */
8611636258SStephen Warren 	enum sysreset_t last_sysreset;	/* Last system reset type */
8711636258SStephen Warren 	bool sysreset_allowed[SYSRESET_COUNT];	/* Allowed system reset types */
88ffb87905SSimon Glass 	enum state_terminal_raw term_raw;	/* Terminal raw/cooked */
899723563aSSimon Glass 	bool skip_delays;		/* Ignore any time delays (for test) */
909ce8b402SSimon Glass 	bool show_test_output;		/* Don't suppress stdout in tests */
912b1dc29aSSimon Glass 	int default_log_level;		/* Default log level for sandbox */
921ca910beSSimon Glass 	bool show_of_platdata;		/* Show of-platdata in SPL */
93a65d1a06SSimon Glass 	bool ram_buf_read;		/* true if we read the RAM buffer */
946122813fSMike Frysinger 
956122813fSMike Frysinger 	/* Pointer to information for each SPI bus/cs */
966122813fSMike Frysinger 	struct sandbox_spi_info spi[CONFIG_SANDBOX_SPI_MAX_BUS]
976122813fSMike Frysinger 					[CONFIG_SANDBOX_SPI_MAX_CS];
980753bc2dSmaxims@google.com 
990753bc2dSmaxims@google.com 	/* Information about Watchdog */
1000753bc2dSmaxims@google.com 	struct sandbox_wdt_info wdt;
101428aa0caSSimon Glass 
102428aa0caSSimon Glass 	ulong next_tag;			/* Next address tag to allocate */
103428aa0caSSimon Glass 	struct list_head mapmem_head;	/* struct sandbox_mapmem_entry */
1047f84fc67SBenjamin Gaignard 	bool hwspinlock;		/* Hardware Spinlock status */
105*c6d84a30SSimon Glass 
106*c6d84a30SSimon Glass 	/*
107*c6d84a30SSimon Glass 	 * This struct is getting large.
108*c6d84a30SSimon Glass 	 *
109*c6d84a30SSimon Glass 	 * Consider putting test data in driver-private structs, like
110*c6d84a30SSimon Glass 	 * sandbox_pch.c.
111*c6d84a30SSimon Glass 	 *
112*c6d84a30SSimon Glass 	 * If you add new members, please put them above this comment.
113*c6d84a30SSimon Glass 	 */
1146fb62078SSimon Glass };
1156fb62078SSimon Glass 
1161209e272SSimon Glass /* Minimum space we guarantee in the state FDT when calling read/write*/
1171209e272SSimon Glass #define SANDBOX_STATE_MIN_SPACE		0x1000
1181209e272SSimon Glass 
1191209e272SSimon Glass /**
1201209e272SSimon Glass  * struct sandbox_state_io - methods to saved/restore sandbox state
1211209e272SSimon Glass  * @name: Name of of the device tree node, also the name of the variable
1221209e272SSimon Glass  *	holding this data so it should be an identifier (use underscore
1231209e272SSimon Glass  *	instead of minus)
1241209e272SSimon Glass  * @compat: Compatible string for the node containing this state
1251209e272SSimon Glass  *
1261209e272SSimon Glass  * @read: Function to read state from FDT
1271209e272SSimon Glass  *	If data is available, then blob and node will provide access to it. If
1281209e272SSimon Glass  *	not (blob == NULL and node == -1) this function should set up an empty
1291209e272SSimon Glass  *	data set for start-of-day.
1301209e272SSimon Glass  *	@param blob: Pointer to device tree blob, or NULL if no data to read
1311209e272SSimon Glass  *	@param node: Node offset to read from
1321209e272SSimon Glass  *	@return 0 if OK, -ve on error
1331209e272SSimon Glass  *
1341209e272SSimon Glass  * @write: Function to write state to FDT
1351209e272SSimon Glass  *	The caller will ensure that there is a node ready for the state. The
1361209e272SSimon Glass  *	node may already contain the old state, in which case it should be
1371209e272SSimon Glass  *	overridden. There is guaranteed to be SANDBOX_STATE_MIN_SPACE bytes
1381209e272SSimon Glass  *	of free space, so error checking is not required for fdt_setprop...()
1391209e272SSimon Glass  *	calls which add up to less than this much space.
1401209e272SSimon Glass  *
1411209e272SSimon Glass  *	For adding larger properties, use state_setprop().
1421209e272SSimon Glass  *
1431209e272SSimon Glass  * @param blob: Device tree blob holding state
1441209e272SSimon Glass  * @param node: Node to write our state into
1451209e272SSimon Glass  *
1461209e272SSimon Glass  * Note that it is possible to save data as large blobs or as individual
1471209e272SSimon Glass  * hierarchical properties. However, unless you intend to keep state files
1481209e272SSimon Glass  * around for a long time and be able to run an old state file on a new
1491209e272SSimon Glass  * sandbox, it might not be worth using individual properties for everything.
1501209e272SSimon Glass  * This is certainly supported, it is just a matter of the effort you wish
1511209e272SSimon Glass  * to put into the state read/write feature.
1521209e272SSimon Glass  */
1531209e272SSimon Glass struct sandbox_state_io {
1541209e272SSimon Glass 	const char *name;
1551209e272SSimon Glass 	const char *compat;
1561209e272SSimon Glass 	int (*write)(void *blob, int node);
1571209e272SSimon Glass 	int (*read)(const void *blob, int node);
1581209e272SSimon Glass };
1591209e272SSimon Glass 
1601209e272SSimon Glass /**
1611209e272SSimon Glass  * SANDBOX_STATE_IO - Declare sandbox state to read/write
1621209e272SSimon Glass  *
1631209e272SSimon Glass  * Sandbox permits saving state from one run and restoring it in another. This
1641209e272SSimon Glass  * allows the test system to retain state between runs and thus better
1651209e272SSimon Glass  * emulate a real system. Examples of state that might be useful to save are
1661209e272SSimon Glass  * the emulated GPIOs pin settings, flash memory contents and TPM private
1671209e272SSimon Glass  * data. U-Boot memory contents is dealth with separately since it is large
1681209e272SSimon Glass  * and it is not normally useful to save it (since a normal system does not
1691209e272SSimon Glass  * preserve DRAM between runs). See the '-m' option for this.
1701209e272SSimon Glass  *
1711209e272SSimon Glass  * See struct sandbox_state_io above for member documentation.
1721209e272SSimon Glass  */
1731209e272SSimon Glass #define SANDBOX_STATE_IO(_name, _compat, _read, _write) \
1741209e272SSimon Glass 	ll_entry_declare(struct sandbox_state_io, _name, state_io) = { \
1751209e272SSimon Glass 		.name = __stringify(_name), \
1761209e272SSimon Glass 		.read = _read, \
1771209e272SSimon Glass 		.write = _write, \
1781209e272SSimon Glass 		.compat = _compat, \
1791209e272SSimon Glass 	}
1801209e272SSimon Glass 
1816fb62078SSimon Glass /**
1826fb62078SSimon Glass  * Gets a pointer to the current state.
1836fb62078SSimon Glass  *
1846fb62078SSimon Glass  * @return pointer to state
1856fb62078SSimon Glass  */
1866fb62078SSimon Glass struct sandbox_state *state_get_current(void);
1876fb62078SSimon Glass 
1886fb62078SSimon Glass /**
1891209e272SSimon Glass  * Read the sandbox state from the supplied device tree file
1901209e272SSimon Glass  *
1911209e272SSimon Glass  * This calls all registered state handlers to read in the sandbox state
1921209e272SSimon Glass  * from a previous test run.
1931209e272SSimon Glass  *
1941209e272SSimon Glass  * @param state		Sandbox state to update
1951209e272SSimon Glass  * @param fname		Filename of device tree file to read from
1961209e272SSimon Glass  * @return 0 if OK, -ve on error
1971209e272SSimon Glass  */
1981209e272SSimon Glass int sandbox_read_state(struct sandbox_state *state, const char *fname);
1991209e272SSimon Glass 
2001209e272SSimon Glass /**
2011209e272SSimon Glass  * Write the sandbox state to the supplied device tree file
2021209e272SSimon Glass  *
2031209e272SSimon Glass  * This calls all registered state handlers to write out the sandbox state
2041209e272SSimon Glass  * so that it can be preserved for a future test run.
2051209e272SSimon Glass  *
2061209e272SSimon Glass  * If the file exists it is overwritten.
2071209e272SSimon Glass  *
2081209e272SSimon Glass  * @param state		Sandbox state to update
2091209e272SSimon Glass  * @param fname		Filename of device tree file to write to
2101209e272SSimon Glass  * @return 0 if OK, -ve on error
2111209e272SSimon Glass  */
2121209e272SSimon Glass int sandbox_write_state(struct sandbox_state *state, const char *fname);
2131209e272SSimon Glass 
2141209e272SSimon Glass /**
2151209e272SSimon Glass  * Add a property to a sandbox state node
2161209e272SSimon Glass  *
2171209e272SSimon Glass  * This is equivalent to fdt_setprop except that it automatically enlarges
2181209e272SSimon Glass  * the device tree if necessary. That means it is safe to write any amount
2191209e272SSimon Glass  * of data here.
2201209e272SSimon Glass  *
2211209e272SSimon Glass  * This function can only be called from within struct sandbox_state_io's
2221209e272SSimon Glass  * ->write method, i.e. within state I/O drivers.
2231209e272SSimon Glass  *
2241209e272SSimon Glass  * @param node		Device tree node to write to
2251209e272SSimon Glass  * @param prop_name	Property to write
2261209e272SSimon Glass  * @param data		Data to write into property
2271209e272SSimon Glass  * @param size		Size of data to write into property
2281209e272SSimon Glass  */
2291209e272SSimon Glass int state_setprop(int node, const char *prop_name, const void *data, int size);
2301209e272SSimon Glass 
2311209e272SSimon Glass /**
2329723563aSSimon Glass  * Control skipping of time delays
2339723563aSSimon Glass  *
2349723563aSSimon Glass  * Some tests have unnecessay time delays (e.g. USB). Allow these to be
2359723563aSSimon Glass  * skipped to speed up testing
2369723563aSSimon Glass  *
2379723563aSSimon Glass  * @param skip_delays	true to skip delays from now on, false to honour delay
2389723563aSSimon Glass  *			requests
2399723563aSSimon Glass  */
2409723563aSSimon Glass void state_set_skip_delays(bool skip_delays);
2419723563aSSimon Glass 
2429723563aSSimon Glass /**
2439723563aSSimon Glass  * See if delays should be skipped
2449723563aSSimon Glass  *
2459723563aSSimon Glass  * @return true if delays should be skipped, false if they should be honoured
2469723563aSSimon Glass  */
2479723563aSSimon Glass bool state_get_skip_delays(void);
2489723563aSSimon Glass 
2499723563aSSimon Glass /**
25034b744beSSimon Glass  * state_reset_for_test() - Reset ready to re-run tests
25134b744beSSimon Glass  *
25234b744beSSimon Glass  * This clears out any test state ready for another test run.
25334b744beSSimon Glass  */
25434b744beSSimon Glass void state_reset_for_test(struct sandbox_state *state);
25534b744beSSimon Glass 
25634b744beSSimon Glass /**
257d66ddafaSSimon Glass  * state_show() - Show information about the sandbox state
258d66ddafaSSimon Glass  *
259d66ddafaSSimon Glass  * @param state		Sandbox state to show
260d66ddafaSSimon Glass  */
261d66ddafaSSimon Glass void state_show(struct sandbox_state *state);
262d66ddafaSSimon Glass 
263d66ddafaSSimon Glass /**
2646fb62078SSimon Glass  * Initialize the test system state
2656fb62078SSimon Glass  */
2666fb62078SSimon Glass int state_init(void);
2676fb62078SSimon Glass 
2685c2859cdSSimon Glass /**
2695c2859cdSSimon Glass  * Uninitialize the test system state, writing out state if configured to
2705c2859cdSSimon Glass  * do so.
2715c2859cdSSimon Glass  *
2725c2859cdSSimon Glass  * @return 0 if OK, -ve on error
2735c2859cdSSimon Glass  */
2745c2859cdSSimon Glass int state_uninit(void);
2755c2859cdSSimon Glass 
2766fb62078SSimon Glass #endif
277