1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef _LINUX_MEMREMAP_H_ 3#define _LINUX_MEMREMAP_H_ 4#include <linux/range.h> 5#include <linux/ioport.h> 6#include <linux/percpu-refcount.h> 7#include <linux/mm_types.h> /* needed in RHEL8 for dev_pagemap & vm_fault_t */ 8 9struct resource; 10struct 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 */ 20struct 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 RH_KABI_EXTEND(const unsigned long end_pfn) 27}; 28 29/* 30 * Specialize ZONE_DEVICE memory into multiple types each having differents 31 * usage. 32 * 33 * MEMORY_DEVICE_PRIVATE: 34 * Device memory that is not directly addressable by the CPU: CPU can neither 35 * read nor write private memory. In this case, we do still have struct pages 36 * backing the device memory. Doing so simplifies the implementation, but it is 37 * important to remember that there are certain points at which the struct page 38 * must be treated as an opaque object, rather than a "normal" struct page. 39 * 40 * A more complete discussion of unaddressable memory may be found in 41 * include/linux/hmm.h and Documentation/vm/hmm.rst. 42 * 43 * MEMORY_DEVICE_PUBLIC: 44 * Device memory that is cache coherent from device and CPU point of view. This 45 * is use on platform that have an advance system bus (like CAPI or CCIX). A 46 * driver can hotplug the device memory using ZONE_DEVICE and with that memory 47 * type. Any page of a process can be migrated to such memory. However no one 48 * should be allow to pin such memory so that it can always be evicted. 49 * 50 * MEMORY_DEVICE_FS_DAX: 51 * Host memory that has similar access semantics as System RAM i.e. DMA 52 * coherent and supports page pinning. In support of coordinating page 53 * pinning vs other operations MEMORY_DEVICE_FS_DAX arranges for a 54 * wakeup event whenever a page is unpinned and becomes idle. This 55 * wakeup is used to coordinate physical address space management (ex: 56 * fs truncate/hole punch) vs pinned pages (ex: device dma). 57 * 58 * MEMORY_DEVICE_GENERIC: 59 * Host memory that has similar access semantics as System RAM i.e. DMA 60 * coherent and supports page pinning. This is for example used by DAX devices 61 * that expose memory using a character device. 62 * 63 * MEMORY_DEVICE_PCI_P2PDMA: 64 * Device memory residing in a PCI BAR intended for use with Peer-to-Peer 65 * transactions. 66 */ 67enum memory_type { 68 /* 0 is reserved to catch uninitialized type fields */ 69 MEMORY_DEVICE_PRIVATE = 1, 70 MEMORY_DEVICE_PUBLIC, 71 MEMORY_DEVICE_FS_DAX, 72 MEMORY_DEVICE_PCI_P2PDMA, 73#ifndef __GENKSYMS__ 74 MEMORY_DEVICE_GENERIC, 75#endif /* __GENKSYMS__ */ 76}; 77 78typedef int (*dev_page_fault_t)(struct vm_area_struct *vma, 79 unsigned long addr, 80 const struct page *page, 81 unsigned int flags, 82 pmd_t *pmdp); 83typedef void (*dev_page_free_t)(struct page *page, void *data); 84struct dev_pagemap_ops { 85 /* 86 * Called once the page refcount reaches 1. (ZONE_DEVICE pages never 87 * reach 0 refcount unless there is a refcount bug. This allows the 88 * device driver to implement its own memory management.) 89 */ 90 void (*page_free)(struct page *page); 91 92 /* 93 * Transition the refcount in struct dev_pagemap to the dead state. 94 */ 95 void (*kill)(struct dev_pagemap *pgmap); 96 97 /* 98 * Wait for refcount in struct dev_pagemap to be idle and reap it. 99 */ 100 void (*cleanup)(struct dev_pagemap *pgmap); 101 102 /* 103 * Used for private (un-addressable) device memory only. Must migrate 104 * the page back to a CPU accessible page. 105 */ 106 vm_fault_t (*migrate_to_ram)(struct vm_fault *vmf); 107}; 108 109#define PGMAP_ALTMAP_VALID (1 << 0) 110 111/** 112 * struct dev_pagemap - metadata for ZONE_DEVICE mappings 113 * @altmap: pre-allocated/reserved memory for vmemmap allocations 114 * @ref: reference count that pins the devm_memremap_pages() mapping 115 * @internal_ref: internal reference if @ref is not provided by the caller 116 * @done: completion for @internal_ref 117 * @type: memory type: see MEMORY_* in memory_hotplug.h 118 * @flags: PGMAP_* flags to specify defailed behavior 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 */ 127struct dev_pagemap { 128 RH_KABI_DEPRECATE(dev_page_fault_t, page_fault) 129 RH_KABI_DEPRECATE(dev_page_free_t, page_free) 130 struct vmem_altmap altmap; 131 RH_KABI_DEPRECATE(bool, altmap_valid) 132 RH_KABI_DEPRECATE(struct resource, res) 133 struct percpu_ref *ref; 134 RH_KABI_DEPRECATE(struct device *, dev) 135 RH_KABI_DEPRECATE(void *, data) 136 enum memory_type type; 137 RH_KABI_DEPRECATE(u64, pci_p2pdma_bus_offset) 138 RH_KABI_EXTEND(const struct dev_pagemap_ops *ops) 139 RH_KABI_EXTEND(unsigned int flags) 140 RH_KABI_EXTEND(struct percpu_ref internal_ref) 141 RH_KABI_EXTEND(struct completion done) 142 RH_KABI_EXTEND(void *owner) 143 RH_KABI_EXTEND(int nr_range) 144 RH_KABI_BROKEN_INSERT_BLOCK( 145 union { 146 struct range range; 147 struct range ranges[0]; 148 }; 149 ) /* RH_KABI_BROKEN_INSERT_BLOCK */ 150}; 151 152static inline struct vmem_altmap *pgmap_altmap(struct dev_pagemap *pgmap) 153{ 154 if (pgmap->flags & PGMAP_ALTMAP_VALID) 155 return &pgmap->altmap; 156 return NULL; 157} 158 159#ifdef CONFIG_ZONE_DEVICE 160void *memremap_pages(struct dev_pagemap *pgmap, int nid); 161void memunmap_pages(struct dev_pagemap *pgmap); 162void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap); 163void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap); 164struct dev_pagemap *get_dev_pagemap(unsigned long pfn, 165 struct dev_pagemap *pgmap); 166 167unsigned long vmem_altmap_offset(struct vmem_altmap *altmap); 168void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns); 169unsigned long memremap_compat_align(void); 170#else 171static inline void *devm_memremap_pages(struct device *dev, 172 struct dev_pagemap *pgmap) 173{ 174 /* 175 * Fail attempts to call devm_memremap_pages() without 176 * ZONE_DEVICE support enabled, this requires callers to fall 177 * back to plain devm_memremap() based on config 178 */ 179 WARN_ON_ONCE(1); 180 return ERR_PTR(-ENXIO); 181} 182 183static inline void devm_memunmap_pages(struct device *dev, 184 struct dev_pagemap *pgmap) 185{ 186} 187 188static inline struct dev_pagemap *get_dev_pagemap(unsigned long pfn, 189 struct dev_pagemap *pgmap) 190{ 191 return NULL; 192} 193 194static inline unsigned long vmem_altmap_offset(struct vmem_altmap *altmap) 195{ 196 return 0; 197} 198 199static inline void vmem_altmap_free(struct vmem_altmap *altmap, 200 unsigned long nr_pfns) 201{ 202} 203 204/* when memremap_pages() is disabled all archs can remap a single page */ 205static inline unsigned long memremap_compat_align(void) 206{ 207 return PAGE_SIZE; 208} 209#endif /* CONFIG_ZONE_DEVICE */ 210 211static inline void put_dev_pagemap(struct dev_pagemap *pgmap) 212{ 213 if (pgmap) 214 percpu_ref_put(pgmap->ref); 215} 216 217#endif /* _LINUX_MEMREMAP_H_ */ 218