1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2023 Addiva Elektronik
4  * Author: Tobias Waldekranz <tobias@waldekranz.com>
5  */
6 
7 #ifndef _BLKMAP_H
8 #define _BLKMAP_H
9 
10 #include <blk.h>
11 #include <dm/lists.h>
12 
13 /**
14  * struct blkmap - Block map
15  *
16  * Data associated with a blkmap.
17  *
18  * @label: Human readable name of this blkmap
19  * @blk: Underlying block device
20  * @slices: List of slices associated with this blkmap
21  */
22 struct blkmap {
23 	char *label;
24 	struct udevice *blk;
25 	struct list_head slices;
26 };
27 
28 /**
29  * blkmap_map_linear() - Map region of other block device
30  *
31  * @dev: Blkmap to create the mapping on
32  * @blknr: Start block number of the mapping
33  * @blkcnt: Number of blocks to map
34  * @lblk: The target block device of the mapping
35  * @lblknr: The start block number of the target device
36  * Returns: 0 on success, negative error code on failure
37  */
38 int blkmap_map_linear(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
39 		      struct udevice *lblk, lbaint_t lblknr);
40 
41 /**
42  * blkmap_map_mem() - Map region of memory
43  *
44  * @dev: Blkmap to create the mapping on
45  * @blknr: Start block number of the mapping
46  * @blkcnt: Number of blocks to map
47  * @addr: The target memory address of the mapping
48  * Returns: 0 on success, negative error code on failure
49  */
50 int blkmap_map_mem(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
51 		   void *addr);
52 
53 /**
54  * blkmap_map_pmem() - Map region of physical memory
55  *
56  * Ensures that a valid physical to virtual memory mapping for the
57  * requested region is valid for the lifetime of the mapping, on
58  * architectures that require it (sandbox).
59  *
60  * @dev: Blkmap to create the mapping on
61  * @blknr: Start block number of the mapping
62  * @blkcnt: Number of blocks to map
63  * @paddr: The target physical memory address of the mapping
64  * @preserve: Mapping intended to be preserved for subsequent stages,
65  *		like the OS (e.g. ISO installer)
66  * Returns: 0 on success, negative error code on failure
67  */
68 int blkmap_map_pmem(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
69 		    phys_addr_t paddr, bool preserve);
70 
71 /**
72  * blkmap_from_label() - Find blkmap from label
73  *
74  * @label: Label of the requested blkmap
75  * Returns: A pointer to the blkmap on success, NULL on failure
76  */
77 struct udevice *blkmap_from_label(const char *label);
78 
79 /**
80  * blkmap_create() - Create new blkmap
81  *
82  * @label: Label of the new blkmap
83  * @devp: If not NULL, updated with the address of the resulting device
84  * Returns: 0 on success, negative error code on failure
85  */
86 int blkmap_create(const char *label, struct udevice **devp);
87 
88 /**
89  * blkmap_destroy() - Destroy blkmap
90  *
91  * @dev: The blkmap to be destroyed
92  * Returns: 0 on success, negative error code on failure
93  */
94 int blkmap_destroy(struct udevice *dev);
95 
96 /**
97  * blkmap_create_ramdisk() - Create new ramdisk with blkmap
98  *
99  * @label: Label of the new blkmap
100  * @image_addr: Target memory start address of this mapping
101  * @image_size: Target memory size of this mapping
102  * @devp: Updated with the address of the created blkmap device
103  * Returns: 0 on success, negative error code on failure
104  */
105 int blkmap_create_ramdisk(const char *label, ulong image_addr, ulong image_size,
106 			  struct udevice **devp);
107 
108 /**
109  * blkmap_get_preserved_pmem_slices() - Look for memory mapped preserved slices
110  * @cb: Callback function to call for the blkmap slice
111  * @ctx: Argument to be passed to the callback function
112  *
113  * The function is used to iterate through all the blkmap slices, looking
114  * specifically for memory mapped blkmap mapping which has been
115  * created with the preserve attribute. The function looks for such slices
116  * with the relevant attributes and then calls the callback function which
117  * then does additional configuration as needed. The callback function is
118  * invoked for all the discovered slices, unless there is an error returned
119  * by the callback, in which case the function returns that error.
120  *
121  * The callback function has the following arguments
122  * @ctx:	Argument to be passed to the callback function
123  * @addr:	Start address of the memory mapped slice
124  * @size:	Size of the memory mapped slice
125  *
126  * Typically, the callback will perform some configuration needed for the
127  * information passed on to it. An example of this would be setting up the
128  * pmem node in a device-tree(passed through the ctx argument) with the
129  * parameters passed on to the callback.
130  *
131  * Return: 0 on success, negative error on failure
132  */
133 int blkmap_get_preserved_pmem_slices(int (*cb)(void *ctx, u64 addr,
134 					       u64 size), void *ctx);
135 
136 #endif	/* _BLKMAP_H */
137