Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 1 | #ifndef _LINUX_MEMREMAP_H_ |
| 2 | #define _LINUX_MEMREMAP_H_ |
| 3 | #include <linux/mm.h> |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 4 | #include <linux/ioport.h> |
| 5 | #include <linux/percpu-refcount.h> |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 6 | |
Jérôme Glisse | 5042db4 | 2017-09-08 16:11:43 -0700 | [diff] [blame^] | 7 | #include <asm/pgtable.h> |
| 8 | |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 9 | struct resource; |
| 10 | struct device; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 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 | unsigned long vmem_altmap_offset(struct vmem_altmap *altmap); |
| 29 | void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns); |
| 30 | |
Dan Williams | 11db048 | 2016-07-28 15:48:11 -0700 | [diff] [blame] | 31 | #ifdef CONFIG_ZONE_DEVICE |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 32 | struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start); |
| 33 | #else |
| 34 | static inline struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start) |
| 35 | { |
| 36 | return NULL; |
| 37 | } |
| 38 | #endif |
| 39 | |
Jérôme Glisse | 5042db4 | 2017-09-08 16:11:43 -0700 | [diff] [blame^] | 40 | /* |
| 41 | * Specialize ZONE_DEVICE memory into multiple types each having differents |
| 42 | * usage. |
| 43 | * |
| 44 | * MEMORY_DEVICE_HOST: |
| 45 | * Persistent device memory (pmem): struct page might be allocated in different |
| 46 | * memory and architecture might want to perform special actions. It is similar |
| 47 | * to regular memory, in that the CPU can access it transparently. However, |
| 48 | * it is likely to have different bandwidth and latency than regular memory. |
| 49 | * See Documentation/nvdimm/nvdimm.txt for more information. |
| 50 | * |
| 51 | * MEMORY_DEVICE_PRIVATE: |
| 52 | * Device memory that is not directly addressable by the CPU: CPU can neither |
| 53 | * read nor write private memory. In this case, we do still have struct pages |
| 54 | * backing the device memory. Doing so simplifies the implementation, but it is |
| 55 | * important to remember that there are certain points at which the struct page |
| 56 | * must be treated as an opaque object, rather than a "normal" struct page. |
| 57 | * |
| 58 | * A more complete discussion of unaddressable memory may be found in |
| 59 | * include/linux/hmm.h and Documentation/vm/hmm.txt. |
| 60 | */ |
| 61 | enum memory_type { |
| 62 | MEMORY_DEVICE_HOST = 0, |
| 63 | MEMORY_DEVICE_PRIVATE, |
| 64 | }; |
| 65 | |
| 66 | /* |
| 67 | * For MEMORY_DEVICE_PRIVATE we use ZONE_DEVICE and extend it with two |
| 68 | * callbacks: |
| 69 | * page_fault() |
| 70 | * page_free() |
| 71 | * |
| 72 | * Additional notes about MEMORY_DEVICE_PRIVATE may be found in |
| 73 | * include/linux/hmm.h and Documentation/vm/hmm.txt. There is also a brief |
| 74 | * explanation in include/linux/memory_hotplug.h. |
| 75 | * |
| 76 | * The page_fault() callback must migrate page back, from device memory to |
| 77 | * system memory, so that the CPU can access it. This might fail for various |
| 78 | * reasons (device issues, device have been unplugged, ...). When such error |
| 79 | * conditions happen, the page_fault() callback must return VM_FAULT_SIGBUS and |
| 80 | * set the CPU page table entry to "poisoned". |
| 81 | * |
| 82 | * Note that because memory cgroup charges are transferred to the device memory, |
| 83 | * this should never fail due to memory restrictions. However, allocation |
| 84 | * of a regular system page might still fail because we are out of memory. If |
| 85 | * that happens, the page_fault() callback must return VM_FAULT_OOM. |
| 86 | * |
| 87 | * The page_fault() callback can also try to migrate back multiple pages in one |
| 88 | * chunk, as an optimization. It must, however, prioritize the faulting address |
| 89 | * over all the others. |
| 90 | * |
| 91 | * |
| 92 | * The page_free() callback is called once the page refcount reaches 1 |
| 93 | * (ZONE_DEVICE pages never reach 0 refcount unless there is a refcount bug. |
| 94 | * This allows the device driver to implement its own memory management.) |
| 95 | */ |
| 96 | typedef int (*dev_page_fault_t)(struct vm_area_struct *vma, |
| 97 | unsigned long addr, |
| 98 | const struct page *page, |
| 99 | unsigned int flags, |
| 100 | pmd_t *pmdp); |
| 101 | typedef void (*dev_page_free_t)(struct page *page, void *data); |
| 102 | |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 103 | /** |
| 104 | * struct dev_pagemap - metadata for ZONE_DEVICE mappings |
Jérôme Glisse | 5042db4 | 2017-09-08 16:11:43 -0700 | [diff] [blame^] | 105 | * @page_fault: callback when CPU fault on an unaddressable device page |
| 106 | * @page_free: free page callback when page refcount reaches 1 |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 107 | * @altmap: pre-allocated/reserved memory for vmemmap allocations |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 108 | * @res: physical address range covered by @ref |
| 109 | * @ref: reference count that pins the devm_memremap_pages() mapping |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 110 | * @dev: host device of the mapping for debug |
Jérôme Glisse | 5042db4 | 2017-09-08 16:11:43 -0700 | [diff] [blame^] | 111 | * @data: private data pointer for page_free() |
| 112 | * @type: memory type: see MEMORY_* in memory_hotplug.h |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 113 | */ |
| 114 | struct dev_pagemap { |
Jérôme Glisse | 5042db4 | 2017-09-08 16:11:43 -0700 | [diff] [blame^] | 115 | dev_page_fault_t page_fault; |
| 116 | dev_page_free_t page_free; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 117 | struct vmem_altmap *altmap; |
| 118 | const struct resource *res; |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 119 | struct percpu_ref *ref; |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 120 | struct device *dev; |
Jérôme Glisse | 5042db4 | 2017-09-08 16:11:43 -0700 | [diff] [blame^] | 121 | void *data; |
| 122 | enum memory_type type; |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | #ifdef CONFIG_ZONE_DEVICE |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 126 | void *devm_memremap_pages(struct device *dev, struct resource *res, |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 127 | struct percpu_ref *ref, struct vmem_altmap *altmap); |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 128 | struct dev_pagemap *find_dev_pagemap(resource_size_t phys); |
| 129 | #else |
| 130 | static inline void *devm_memremap_pages(struct device *dev, |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 131 | struct resource *res, struct percpu_ref *ref, |
| 132 | struct vmem_altmap *altmap) |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 133 | { |
| 134 | /* |
| 135 | * Fail attempts to call devm_memremap_pages() without |
| 136 | * ZONE_DEVICE support enabled, this requires callers to fall |
| 137 | * back to plain devm_memremap() based on config |
| 138 | */ |
| 139 | WARN_ON_ONCE(1); |
| 140 | return ERR_PTR(-ENXIO); |
| 141 | } |
| 142 | |
| 143 | static inline struct dev_pagemap *find_dev_pagemap(resource_size_t phys) |
| 144 | { |
| 145 | return NULL; |
| 146 | } |
| 147 | #endif |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * get_dev_pagemap() - take a new live reference on the dev_pagemap for @pfn |
| 151 | * @pfn: page frame number to lookup page_map |
| 152 | * @pgmap: optional known pgmap that already has a reference |
| 153 | * |
| 154 | * @pgmap allows the overhead of a lookup to be bypassed when @pfn lands in the |
| 155 | * same mapping. |
| 156 | */ |
| 157 | static inline struct dev_pagemap *get_dev_pagemap(unsigned long pfn, |
| 158 | struct dev_pagemap *pgmap) |
| 159 | { |
| 160 | const struct resource *res = pgmap ? pgmap->res : NULL; |
| 161 | resource_size_t phys = PFN_PHYS(pfn); |
| 162 | |
| 163 | /* |
| 164 | * In the cached case we're already holding a live reference so |
| 165 | * we can simply do a blind increment |
| 166 | */ |
| 167 | if (res && phys >= res->start && phys <= res->end) { |
| 168 | percpu_ref_get(pgmap->ref); |
| 169 | return pgmap; |
| 170 | } |
| 171 | |
| 172 | /* fall back to slow path lookup */ |
| 173 | rcu_read_lock(); |
| 174 | pgmap = find_dev_pagemap(phys); |
| 175 | if (pgmap && !percpu_ref_tryget_live(pgmap->ref)) |
| 176 | pgmap = NULL; |
| 177 | rcu_read_unlock(); |
| 178 | |
| 179 | return pgmap; |
| 180 | } |
| 181 | |
| 182 | static inline void put_dev_pagemap(struct dev_pagemap *pgmap) |
| 183 | { |
| 184 | if (pgmap) |
| 185 | percpu_ref_put(pgmap->ref); |
| 186 | } |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 187 | #endif /* _LINUX_MEMREMAP_H_ */ |