1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef LINUX_KEXEC_HANDOVER_H
3 #define LINUX_KEXEC_HANDOVER_H
4
5 #include <linux/err.h>
6 #include <linux/errno.h>
7 #include <linux/types.h>
8
9 struct kho_scratch {
10 phys_addr_t addr;
11 phys_addr_t size;
12 };
13
14 struct folio;
15 struct page;
16
17 #define DECLARE_KHOSER_PTR(name, type) \
18 union { \
19 phys_addr_t phys; \
20 type ptr; \
21 } name
22 #define KHOSER_STORE_PTR(dest, val) \
23 ({ \
24 typeof(val) v = val; \
25 typecheck(typeof((dest).ptr), v); \
26 (dest).phys = virt_to_phys(v); \
27 })
28 #define KHOSER_LOAD_PTR(src) \
29 ({ \
30 typeof(src) s = src; \
31 (typeof((s).ptr))((s).phys ? phys_to_virt((s).phys) : NULL); \
32 })
33
34 struct kho_vmalloc_chunk;
35 struct kho_vmalloc {
36 DECLARE_KHOSER_PTR(first, struct kho_vmalloc_chunk *);
37 unsigned int total_pages;
38 unsigned short flags;
39 unsigned short order;
40 };
41
42 #ifdef CONFIG_KEXEC_HANDOVER
43 bool kho_is_enabled(void);
44 bool is_kho_boot(void);
45
46 int kho_preserve_folio(struct folio *folio);
47 void kho_unpreserve_folio(struct folio *folio);
48 int kho_preserve_pages(struct page *page, unsigned int nr_pages);
49 void kho_unpreserve_pages(struct page *page, unsigned int nr_pages);
50 int kho_preserve_vmalloc(void *ptr, struct kho_vmalloc *preservation);
51 void kho_unpreserve_vmalloc(struct kho_vmalloc *preservation);
52 void *kho_alloc_preserve(size_t size);
53 void kho_unpreserve_free(void *mem);
54 void kho_restore_free(void *mem);
55 struct folio *kho_restore_folio(phys_addr_t phys);
56 struct page *kho_restore_pages(phys_addr_t phys, unsigned int nr_pages);
57 void *kho_restore_vmalloc(const struct kho_vmalloc *preservation);
58 int kho_add_subtree(const char *name, void *fdt);
59 void kho_remove_subtree(void *fdt);
60 int kho_retrieve_subtree(const char *name, phys_addr_t *phys);
61
62 void kho_memory_init(void);
63
64 void kho_populate(phys_addr_t fdt_phys, u64 fdt_len, phys_addr_t scratch_phys,
65 u64 scratch_len);
66 #else
kho_is_enabled(void)67 static inline bool kho_is_enabled(void)
68 {
69 return false;
70 }
71
is_kho_boot(void)72 static inline bool is_kho_boot(void)
73 {
74 return false;
75 }
76
kho_preserve_folio(struct folio * folio)77 static inline int kho_preserve_folio(struct folio *folio)
78 {
79 return -EOPNOTSUPP;
80 }
81
kho_unpreserve_folio(struct folio * folio)82 static inline void kho_unpreserve_folio(struct folio *folio) { }
83
kho_preserve_pages(struct page * page,unsigned int nr_pages)84 static inline int kho_preserve_pages(struct page *page, unsigned int nr_pages)
85 {
86 return -EOPNOTSUPP;
87 }
88
kho_unpreserve_pages(struct page * page,unsigned int nr_pages)89 static inline void kho_unpreserve_pages(struct page *page, unsigned int nr_pages) { }
90
kho_preserve_vmalloc(void * ptr,struct kho_vmalloc * preservation)91 static inline int kho_preserve_vmalloc(void *ptr,
92 struct kho_vmalloc *preservation)
93 {
94 return -EOPNOTSUPP;
95 }
96
kho_unpreserve_vmalloc(struct kho_vmalloc * preservation)97 static inline void kho_unpreserve_vmalloc(struct kho_vmalloc *preservation) { }
98
kho_alloc_preserve(size_t size)99 static inline void *kho_alloc_preserve(size_t size)
100 {
101 return ERR_PTR(-EOPNOTSUPP);
102 }
103
kho_unpreserve_free(void * mem)104 static inline void kho_unpreserve_free(void *mem) { }
kho_restore_free(void * mem)105 static inline void kho_restore_free(void *mem) { }
106
kho_restore_folio(phys_addr_t phys)107 static inline struct folio *kho_restore_folio(phys_addr_t phys)
108 {
109 return NULL;
110 }
111
kho_restore_pages(phys_addr_t phys,unsigned int nr_pages)112 static inline struct page *kho_restore_pages(phys_addr_t phys,
113 unsigned int nr_pages)
114 {
115 return NULL;
116 }
117
kho_restore_vmalloc(const struct kho_vmalloc * preservation)118 static inline void *kho_restore_vmalloc(const struct kho_vmalloc *preservation)
119 {
120 return NULL;
121 }
122
kho_add_subtree(const char * name,void * fdt)123 static inline int kho_add_subtree(const char *name, void *fdt)
124 {
125 return -EOPNOTSUPP;
126 }
127
kho_remove_subtree(void * fdt)128 static inline void kho_remove_subtree(void *fdt) { }
129
kho_retrieve_subtree(const char * name,phys_addr_t * phys)130 static inline int kho_retrieve_subtree(const char *name, phys_addr_t *phys)
131 {
132 return -EOPNOTSUPP;
133 }
134
kho_memory_init(void)135 static inline void kho_memory_init(void) { }
136
kho_populate(phys_addr_t fdt_phys,u64 fdt_len,phys_addr_t scratch_phys,u64 scratch_len)137 static inline void kho_populate(phys_addr_t fdt_phys, u64 fdt_len,
138 phys_addr_t scratch_phys, u64 scratch_len)
139 {
140 }
141 #endif /* CONFIG_KEXEC_HANDOVER */
142
143 #endif /* LINUX_KEXEC_HANDOVER_H */
144