xref: /openbmc/u-boot/include/smem.h (revision ef64e782)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * The shared memory system is an allocate-only heap structure that
4  * consists of one of more memory areas that can be accessed by the processors
5  * in the SoC.
6  *
7  * Allocation can be done globally for all processors or to an individual processor.
8  * This is controlled by the @host parameter.
9  *
10  * Allocation and management of heap can be implemented in various ways,
11  * The @item parameter should be used as an index/hash to the memory region.
12  *
13  * Copyright (c) 2018 Ramon Fried <ramon.fried@gmail.com>
14  */
15 
16 #ifndef _smemh_
17 #define _smemh_
18 
19 /* struct smem_ops: Operations for the SMEM uclass */
20 struct smem_ops {
21 	/**
22 	 * alloc() - allocate space for a smem item
23 	 *
24 	 * @host:	remote processor id, or -1 for all processors.
25 	 * @item:	smem item handle
26 	 * @size:	number of bytes to be allocated
27 	 * @return 0 if OK, -ve on error
28 	 */
29 	int (*alloc)(unsigned int host,
30 		unsigned int item, size_t size);
31 
32 	/**
33 	 * get() - Resolve ptr of size of a smem item
34 	 *
35 	 * @host:	the remote processor, of -1 for all processors.
36 	 * @item:	smem item handle
37 	 * @size:	pointer to be filled out with the size of the item
38 	 * @return	pointer on success, NULL on error
39 	 */
40 	void *(*get)(unsigned int host,
41 		unsigned int item, size_t *size);
42 
43 	/**
44 	 * get_free_space() - Get free space in smem in bytes
45 	 *
46 	 * @host:   the remote processor identifying a partition, or -1
47 	 *			for all processors.
48 	 * @return	free space, -ve on error
49 	 */
50 	int (*get_free_space)(unsigned int host);
51 };
52 
53 #define smem_get_ops(dev)	((struct smem_ops *)(dev)->driver->ops)
54 
55 /**
56  * smem_alloc() - allocate space for a smem item
57  * @host:	remote processor id, or -1
58  * @item:	smem item handle
59  * @size:	number of bytes to be allocated
60  * @return 0 if OK, -ve on error
61  *
62  * Allocate space for a given smem item of size @size, given that the item is
63  * not yet allocated.
64  */
65 int smem_alloc(struct udevice *dev, unsigned int host, unsigned int item, size_t size);
66 
67 /**
68  * smem_get() - resolve ptr of size of a smem item
69  * @host:	the remote processor, or -1 for all processors.
70  * @item:	smem item handle
71  * @size:	pointer to be filled out with size of the item
72  * @return	pointer on success, NULL on error
73  *
74  * Looks up smem item and returns pointer to it. Size of smem
75  * item is returned in @size.
76  */
77 void *smem_get(struct udevice *dev, unsigned int host, unsigned int item, size_t *size);
78 
79 /**
80  * smem_get_free_space() - retrieve amount of free space in a partition
81  * @host:	the remote processor identifying a partition, or -1
82  *			for all processors.
83  * @return	size in bytes, -ve on error
84  *
85  * To be used by smem clients as a quick way to determine if any new
86  * allocations has been made.
87  */
88 int smem_get_free_space(struct udevice *dev, unsigned int host);
89 
90 #endif /* _smem_h_ */
91 
92