1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MEMREMAP_H_
3 #define _LINUX_MEMREMAP_H_
4
5 #include <linux/mmzone.h>
6 #include <linux/range.h>
7 #include <linux/ioport.h>
8 #include <linux/percpu-refcount.h>
9
10 struct resource;
11 struct device;
12
13 /**
14 * struct vmem_altmap - pre-allocated storage for vmemmap_populate
15 * @base_pfn: base of the entire dev_pagemap mapping
16 * @reserve: pages mapped, but reserved for driver use (relative to @base)
17 * @free: free pages set aside in the mapping for memmap storage
18 * @align: pages reserved to meet allocation alignments
19 * @alloc: track pages consumed, private to vmemmap_populate()
20 */
21 struct vmem_altmap {
22 unsigned long base_pfn;
23 const unsigned long end_pfn;
24 const unsigned long reserve;
25 unsigned long free;
26 unsigned long align;
27 unsigned long alloc;
28 bool inaccessible;
29 };
30
31 /*
32 * Specialize ZONE_DEVICE memory into multiple types each has a different
33 * usage.
34 *
35 * MEMORY_DEVICE_PRIVATE:
36 * Device memory that is not directly addressable by the CPU: CPU can neither
37 * read nor write private memory. In this case, we do still have struct pages
38 * backing the device memory. Doing so simplifies the implementation, but it is
39 * important to remember that there are certain points at which the struct page
40 * must be treated as an opaque object, rather than a "normal" struct page.
41 *
42 * A more complete discussion of unaddressable memory may be found in
43 * include/linux/hmm.h and Documentation/mm/hmm.rst.
44 *
45 * MEMORY_DEVICE_COHERENT:
46 * Device memory that is cache coherent from device and CPU point of view. This
47 * is used on platforms that have an advanced system bus (like CAPI or CXL). A
48 * driver can hotplug the device memory using ZONE_DEVICE and with that memory
49 * type. Any page of a process can be migrated to such memory. However no one
50 * should be allowed to pin such memory so that it can always be evicted.
51 *
52 * MEMORY_DEVICE_FS_DAX:
53 * Host memory that has similar access semantics as System RAM i.e. DMA
54 * coherent and supports page pinning. In support of coordinating page
55 * pinning vs other operations MEMORY_DEVICE_FS_DAX arranges for a
56 * wakeup event whenever a page is unpinned and becomes idle. This
57 * wakeup is used to coordinate physical address space management (ex:
58 * fs truncate/hole punch) vs pinned pages (ex: device dma).
59 *
60 * MEMORY_DEVICE_GENERIC:
61 * Host memory that has similar access semantics as System RAM i.e. DMA
62 * coherent and supports page pinning. This is for example used by DAX devices
63 * that expose memory using a character device.
64 *
65 * MEMORY_DEVICE_PCI_P2PDMA:
66 * Device memory residing in a PCI BAR intended for use with Peer-to-Peer
67 * transactions.
68 */
69 enum memory_type {
70 /* 0 is reserved to catch uninitialized type fields */
71 MEMORY_DEVICE_PRIVATE = 1,
72 MEMORY_DEVICE_COHERENT,
73 MEMORY_DEVICE_FS_DAX,
74 MEMORY_DEVICE_GENERIC,
75 MEMORY_DEVICE_PCI_P2PDMA,
76 };
77
78 struct dev_pagemap_ops {
79 /*
80 * Called once the page refcount reaches 0. The reference count will be
81 * reset to one by the core code after the method is called to prepare
82 * for handing out the page again.
83 */
84 void (*page_free)(struct page *page);
85
86 /*
87 * Used for private (un-addressable) device memory only. Must migrate
88 * the page back to a CPU accessible page.
89 */
90 vm_fault_t (*migrate_to_ram)(struct vm_fault *vmf);
91
92 /*
93 * Handle the memory failure happens on a range of pfns. Notify the
94 * processes who are using these pfns, and try to recover the data on
95 * them if necessary. The mf_flags is finally passed to the recover
96 * function through the whole notify routine.
97 *
98 * When this is not implemented, or it returns -EOPNOTSUPP, the caller
99 * will fall back to a common handler called mf_generic_kill_procs().
100 */
101 int (*memory_failure)(struct dev_pagemap *pgmap, unsigned long pfn,
102 unsigned long nr_pages, int mf_flags);
103 };
104
105 #define PGMAP_ALTMAP_VALID (1 << 0)
106
107 /**
108 * struct dev_pagemap - metadata for ZONE_DEVICE mappings
109 * @altmap: pre-allocated/reserved memory for vmemmap allocations
110 * @ref: reference count that pins the devm_memremap_pages() mapping
111 * @done: completion for @ref
112 * @type: memory type: see MEMORY_* above in memremap.h
113 * @flags: PGMAP_* flags to specify defailed behavior
114 * @vmemmap_shift: structural definition of how the vmemmap page metadata
115 * is populated, specifically the metadata page order.
116 * A zero value (default) uses base pages as the vmemmap metadata
117 * representation. A bigger value will set up compound struct pages
118 * of the requested order value.
119 * @ops: method table
120 * @owner: an opaque pointer identifying the entity that manages this
121 * instance. Used by various helpers to make sure that no
122 * foreign ZONE_DEVICE memory is accessed.
123 * @nr_range: number of ranges to be mapped
124 * @range: range to be mapped when nr_range == 1
125 * @ranges: array of ranges to be mapped when nr_range > 1
126 */
127 struct dev_pagemap {
128 struct vmem_altmap altmap;
129 struct percpu_ref ref;
130 struct completion done;
131 enum memory_type type;
132 unsigned int flags;
133 unsigned long vmemmap_shift;
134 const struct dev_pagemap_ops *ops;
135 void *owner;
136 int nr_range;
137 union {
138 struct range range;
139 DECLARE_FLEX_ARRAY(struct range, ranges);
140 };
141 };
142
pgmap_has_memory_failure(struct dev_pagemap * pgmap)143 static inline bool pgmap_has_memory_failure(struct dev_pagemap *pgmap)
144 {
145 return pgmap->ops && pgmap->ops->memory_failure;
146 }
147
pgmap_altmap(struct dev_pagemap * pgmap)148 static inline struct vmem_altmap *pgmap_altmap(struct dev_pagemap *pgmap)
149 {
150 if (pgmap->flags & PGMAP_ALTMAP_VALID)
151 return &pgmap->altmap;
152 return NULL;
153 }
154
pgmap_vmemmap_nr(struct dev_pagemap * pgmap)155 static inline unsigned long pgmap_vmemmap_nr(struct dev_pagemap *pgmap)
156 {
157 return 1 << pgmap->vmemmap_shift;
158 }
159
folio_is_device_private(const struct folio * folio)160 static inline bool folio_is_device_private(const struct folio *folio)
161 {
162 return IS_ENABLED(CONFIG_DEVICE_PRIVATE) &&
163 folio_is_zone_device(folio) &&
164 folio->pgmap->type == MEMORY_DEVICE_PRIVATE;
165 }
166
is_device_private_page(const struct page * page)167 static inline bool is_device_private_page(const struct page *page)
168 {
169 return IS_ENABLED(CONFIG_DEVICE_PRIVATE) &&
170 folio_is_device_private(page_folio(page));
171 }
172
folio_is_pci_p2pdma(const struct folio * folio)173 static inline bool folio_is_pci_p2pdma(const struct folio *folio)
174 {
175 return IS_ENABLED(CONFIG_PCI_P2PDMA) &&
176 folio_is_zone_device(folio) &&
177 folio->pgmap->type == MEMORY_DEVICE_PCI_P2PDMA;
178 }
179
is_pci_p2pdma_page(const struct page * page)180 static inline bool is_pci_p2pdma_page(const struct page *page)
181 {
182 return IS_ENABLED(CONFIG_PCI_P2PDMA) &&
183 folio_is_pci_p2pdma(page_folio(page));
184 }
185
folio_is_device_coherent(const struct folio * folio)186 static inline bool folio_is_device_coherent(const struct folio *folio)
187 {
188 return folio_is_zone_device(folio) &&
189 folio->pgmap->type == MEMORY_DEVICE_COHERENT;
190 }
191
is_device_coherent_page(const struct page * page)192 static inline bool is_device_coherent_page(const struct page *page)
193 {
194 return folio_is_device_coherent(page_folio(page));
195 }
196
folio_is_fsdax(const struct folio * folio)197 static inline bool folio_is_fsdax(const struct folio *folio)
198 {
199 return folio_is_zone_device(folio) &&
200 folio->pgmap->type == MEMORY_DEVICE_FS_DAX;
201 }
202
is_fsdax_page(const struct page * page)203 static inline bool is_fsdax_page(const struct page *page)
204 {
205 return folio_is_fsdax(page_folio(page));
206 }
207
208 #ifdef CONFIG_ZONE_DEVICE
209 void zone_device_page_init(struct page *page);
210 void *memremap_pages(struct dev_pagemap *pgmap, int nid);
211 void memunmap_pages(struct dev_pagemap *pgmap);
212 void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap);
213 void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap);
214 struct dev_pagemap *get_dev_pagemap(unsigned long pfn);
215 bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn);
216
217 unsigned long memremap_compat_align(void);
218 #else
devm_memremap_pages(struct device * dev,struct dev_pagemap * pgmap)219 static inline void *devm_memremap_pages(struct device *dev,
220 struct dev_pagemap *pgmap)
221 {
222 /*
223 * Fail attempts to call devm_memremap_pages() without
224 * ZONE_DEVICE support enabled, this requires callers to fall
225 * back to plain devm_memremap() based on config
226 */
227 WARN_ON_ONCE(1);
228 return ERR_PTR(-ENXIO);
229 }
230
devm_memunmap_pages(struct device * dev,struct dev_pagemap * pgmap)231 static inline void devm_memunmap_pages(struct device *dev,
232 struct dev_pagemap *pgmap)
233 {
234 }
235
get_dev_pagemap(unsigned long pfn)236 static inline struct dev_pagemap *get_dev_pagemap(unsigned long pfn)
237 {
238 return NULL;
239 }
240
pgmap_pfn_valid(struct dev_pagemap * pgmap,unsigned long pfn)241 static inline bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn)
242 {
243 return false;
244 }
245
246 /* when memremap_pages() is disabled all archs can remap a single page */
memremap_compat_align(void)247 static inline unsigned long memremap_compat_align(void)
248 {
249 return PAGE_SIZE;
250 }
251 #endif /* CONFIG_ZONE_DEVICE */
252
put_dev_pagemap(struct dev_pagemap * pgmap)253 static inline void put_dev_pagemap(struct dev_pagemap *pgmap)
254 {
255 if (pgmap)
256 percpu_ref_put(&pgmap->ref);
257 }
258
259 #endif /* _LINUX_MEMREMAP_H_ */
260