1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MEMREMAP_H_
3 #define _LINUX_MEMREMAP_H_
4 #include <linux/ioport.h>
5 #include <linux/percpu-refcount.h>
6 
7 #include <asm/pgtable.h>
8 
9 struct resource;
10 struct device;
11 
12 /**
13  * struct vmem_altmap - pre-allocated storage for vmemmap_populate
14  * @base_pfn: base of the entire dev_pagemap mapping
15  * @reserve: pages mapped, but reserved for driver use (relative to @base)
16  * @free: free pages set aside in the mapping for memmap storage
17  * @align: pages reserved to meet allocation alignments
18  * @alloc: track pages consumed, private to vmemmap_populate()
19  */
20 struct vmem_altmap {
21 	const unsigned long base_pfn;
22 	const unsigned long reserve;
23 	unsigned long free;
24 	unsigned long align;
25 	unsigned long alloc;
26 };
27 
28 /*
29  * Specialize ZONE_DEVICE memory into multiple types each having differents
30  * usage.
31  *
32  * MEMORY_DEVICE_PRIVATE:
33  * Device memory that is not directly addressable by the CPU: CPU can neither
34  * read nor write private memory. In this case, we do still have struct pages
35  * backing the device memory. Doing so simplifies the implementation, but it is
36  * important to remember that there are certain points at which the struct page
37  * must be treated as an opaque object, rather than a "normal" struct page.
38  *
39  * A more complete discussion of unaddressable memory may be found in
40  * include/linux/hmm.h and Documentation/vm/hmm.rst.
41  *
42  * MEMORY_DEVICE_PUBLIC:
43  * Device memory that is cache coherent from device and CPU point of view. This
44  * is use on platform that have an advance system bus (like CAPI or CCIX). A
45  * driver can hotplug the device memory using ZONE_DEVICE and with that memory
46  * type. Any page of a process can be migrated to such memory. However no one
47  * should be allow to pin such memory so that it can always be evicted.
48  *
49  * MEMORY_DEVICE_FS_DAX:
50  * Host memory that has similar access semantics as System RAM i.e. DMA
51  * coherent and supports page pinning. In support of coordinating page
52  * pinning vs other operations MEMORY_DEVICE_FS_DAX arranges for a
53  * wakeup event whenever a page is unpinned and becomes idle. This
54  * wakeup is used to coordinate physical address space management (ex:
55  * fs truncate/hole punch) vs pinned pages (ex: device dma).
56  */
57 enum memory_type {
58 	MEMORY_DEVICE_PRIVATE = 1,
59 	MEMORY_DEVICE_PUBLIC,
60 	MEMORY_DEVICE_FS_DAX,
61 };
62 
63 /*
64  * For MEMORY_DEVICE_PRIVATE we use ZONE_DEVICE and extend it with two
65  * callbacks:
66  *   page_fault()
67  *   page_free()
68  *
69  * Additional notes about MEMORY_DEVICE_PRIVATE may be found in
70  * include/linux/hmm.h and Documentation/vm/hmm.rst. There is also a brief
71  * explanation in include/linux/memory_hotplug.h.
72  *
73  * The page_fault() callback must migrate page back, from device memory to
74  * system memory, so that the CPU can access it. This might fail for various
75  * reasons (device issues,  device have been unplugged, ...). When such error
76  * conditions happen, the page_fault() callback must return VM_FAULT_SIGBUS and
77  * set the CPU page table entry to "poisoned".
78  *
79  * Note that because memory cgroup charges are transferred to the device memory,
80  * this should never fail due to memory restrictions. However, allocation
81  * of a regular system page might still fail because we are out of memory. If
82  * that happens, the page_fault() callback must return VM_FAULT_OOM.
83  *
84  * The page_fault() callback can also try to migrate back multiple pages in one
85  * chunk, as an optimization. It must, however, prioritize the faulting address
86  * over all the others.
87  *
88  *
89  * The page_free() callback is called once the page refcount reaches 1
90  * (ZONE_DEVICE pages never reach 0 refcount unless there is a refcount bug.
91  * This allows the device driver to implement its own memory management.)
92  *
93  * For MEMORY_DEVICE_PUBLIC only the page_free() callback matter.
94  */
95 typedef int (*dev_page_fault_t)(struct vm_area_struct *vma,
96 				unsigned long addr,
97 				const struct page *page,
98 				unsigned int flags,
99 				pmd_t *pmdp);
100 typedef void (*dev_page_free_t)(struct page *page, void *data);
101 
102 /**
103  * struct dev_pagemap - metadata for ZONE_DEVICE mappings
104  * @page_fault: callback when CPU fault on an unaddressable device page
105  * @page_free: free page callback when page refcount reaches 1
106  * @altmap: pre-allocated/reserved memory for vmemmap allocations
107  * @res: physical address range covered by @ref
108  * @ref: reference count that pins the devm_memremap_pages() mapping
109  * @kill: callback to transition @ref to the dead state
110  * @dev: host device of the mapping for debug
111  * @data: private data pointer for page_free()
112  * @type: memory type: see MEMORY_* in memory_hotplug.h
113  */
114 struct dev_pagemap {
115 	dev_page_fault_t page_fault;
116 	dev_page_free_t page_free;
117 	struct vmem_altmap altmap;
118 	bool altmap_valid;
119 	struct resource res;
120 	struct percpu_ref *ref;
121 	void (*kill)(struct percpu_ref *ref);
122 	struct device *dev;
123 	void *data;
124 	enum memory_type type;
125 };
126 
127 #ifdef CONFIG_ZONE_DEVICE
128 void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap);
129 struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
130 		struct dev_pagemap *pgmap);
131 
132 unsigned long vmem_altmap_offset(struct vmem_altmap *altmap);
133 void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns);
134 #else
devm_memremap_pages(struct device * dev,struct dev_pagemap * pgmap)135 static inline void *devm_memremap_pages(struct device *dev,
136 		struct dev_pagemap *pgmap)
137 {
138 	/*
139 	 * Fail attempts to call devm_memremap_pages() without
140 	 * ZONE_DEVICE support enabled, this requires callers to fall
141 	 * back to plain devm_memremap() based on config
142 	 */
143 	WARN_ON_ONCE(1);
144 	return ERR_PTR(-ENXIO);
145 }
146 
get_dev_pagemap(unsigned long pfn,struct dev_pagemap * pgmap)147 static inline struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
148 		struct dev_pagemap *pgmap)
149 {
150 	return NULL;
151 }
152 
vmem_altmap_offset(struct vmem_altmap * altmap)153 static inline unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
154 {
155 	return 0;
156 }
157 
vmem_altmap_free(struct vmem_altmap * altmap,unsigned long nr_pfns)158 static inline void vmem_altmap_free(struct vmem_altmap *altmap,
159 		unsigned long nr_pfns)
160 {
161 }
162 #endif /* CONFIG_ZONE_DEVICE */
163 
put_dev_pagemap(struct dev_pagemap * pgmap)164 static inline void put_dev_pagemap(struct dev_pagemap *pgmap)
165 {
166 	if (pgmap)
167 		percpu_ref_put(pgmap->ref);
168 }
169 #endif /* _LINUX_MEMREMAP_H_ */
170