Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_MEMREMAP_H_
0003 #define _LINUX_MEMREMAP_H_
0004 
0005 #include <linux/mmzone.h>
0006 #include <linux/range.h>
0007 #include <linux/ioport.h>
0008 #include <linux/percpu-refcount.h>
0009 
0010 struct resource;
0011 struct device;
0012 
0013 /**
0014  * struct vmem_altmap - pre-allocated storage for vmemmap_populate
0015  * @base_pfn: base of the entire dev_pagemap mapping
0016  * @reserve: pages mapped, but reserved for driver use (relative to @base)
0017  * @free: free pages set aside in the mapping for memmap storage
0018  * @align: pages reserved to meet allocation alignments
0019  * @alloc: track pages consumed, private to vmemmap_populate()
0020  */
0021 struct vmem_altmap {
0022     unsigned long base_pfn;
0023     const unsigned long end_pfn;
0024     const unsigned long reserve;
0025     unsigned long free;
0026     unsigned long align;
0027     unsigned long alloc;
0028 };
0029 
0030 /*
0031  * Specialize ZONE_DEVICE memory into multiple types each has a different
0032  * usage.
0033  *
0034  * MEMORY_DEVICE_PRIVATE:
0035  * Device memory that is not directly addressable by the CPU: CPU can neither
0036  * read nor write private memory. In this case, we do still have struct pages
0037  * backing the device memory. Doing so simplifies the implementation, but it is
0038  * important to remember that there are certain points at which the struct page
0039  * must be treated as an opaque object, rather than a "normal" struct page.
0040  *
0041  * A more complete discussion of unaddressable memory may be found in
0042  * include/linux/hmm.h and Documentation/mm/hmm.rst.
0043  *
0044  * MEMORY_DEVICE_COHERENT:
0045  * Device memory that is cache coherent from device and CPU point of view. This
0046  * is used on platforms that have an advanced system bus (like CAPI or CXL). A
0047  * driver can hotplug the device memory using ZONE_DEVICE and with that memory
0048  * type. Any page of a process can be migrated to such memory. However no one
0049  * should be allowed to pin such memory so that it can always be evicted.
0050  *
0051  * MEMORY_DEVICE_FS_DAX:
0052  * Host memory that has similar access semantics as System RAM i.e. DMA
0053  * coherent and supports page pinning. In support of coordinating page
0054  * pinning vs other operations MEMORY_DEVICE_FS_DAX arranges for a
0055  * wakeup event whenever a page is unpinned and becomes idle. This
0056  * wakeup is used to coordinate physical address space management (ex:
0057  * fs truncate/hole punch) vs pinned pages (ex: device dma).
0058  *
0059  * MEMORY_DEVICE_GENERIC:
0060  * Host memory that has similar access semantics as System RAM i.e. DMA
0061  * coherent and supports page pinning. This is for example used by DAX devices
0062  * that expose memory using a character device.
0063  *
0064  * MEMORY_DEVICE_PCI_P2PDMA:
0065  * Device memory residing in a PCI BAR intended for use with Peer-to-Peer
0066  * transactions.
0067  */
0068 enum memory_type {
0069     /* 0 is reserved to catch uninitialized type fields */
0070     MEMORY_DEVICE_PRIVATE = 1,
0071     MEMORY_DEVICE_COHERENT,
0072     MEMORY_DEVICE_FS_DAX,
0073     MEMORY_DEVICE_GENERIC,
0074     MEMORY_DEVICE_PCI_P2PDMA,
0075 };
0076 
0077 struct dev_pagemap_ops {
0078     /*
0079      * Called once the page refcount reaches 0.  The reference count will be
0080      * reset to one by the core code after the method is called to prepare
0081      * for handing out the page again.
0082      */
0083     void (*page_free)(struct page *page);
0084 
0085     /*
0086      * Used for private (un-addressable) device memory only.  Must migrate
0087      * the page back to a CPU accessible page.
0088      */
0089     vm_fault_t (*migrate_to_ram)(struct vm_fault *vmf);
0090 
0091     /*
0092      * Handle the memory failure happens on a range of pfns.  Notify the
0093      * processes who are using these pfns, and try to recover the data on
0094      * them if necessary.  The mf_flags is finally passed to the recover
0095      * function through the whole notify routine.
0096      *
0097      * When this is not implemented, or it returns -EOPNOTSUPP, the caller
0098      * will fall back to a common handler called mf_generic_kill_procs().
0099      */
0100     int (*memory_failure)(struct dev_pagemap *pgmap, unsigned long pfn,
0101                   unsigned long nr_pages, int mf_flags);
0102 };
0103 
0104 #define PGMAP_ALTMAP_VALID  (1 << 0)
0105 
0106 /**
0107  * struct dev_pagemap - metadata for ZONE_DEVICE mappings
0108  * @altmap: pre-allocated/reserved memory for vmemmap allocations
0109  * @ref: reference count that pins the devm_memremap_pages() mapping
0110  * @done: completion for @ref
0111  * @type: memory type: see MEMORY_* in memory_hotplug.h
0112  * @flags: PGMAP_* flags to specify defailed behavior
0113  * @vmemmap_shift: structural definition of how the vmemmap page metadata
0114  *      is populated, specifically the metadata page order.
0115  *  A zero value (default) uses base pages as the vmemmap metadata
0116  *  representation. A bigger value will set up compound struct pages
0117  *  of the requested order value.
0118  * @ops: method table
0119  * @owner: an opaque pointer identifying the entity that manages this
0120  *  instance.  Used by various helpers to make sure that no
0121  *  foreign ZONE_DEVICE memory is accessed.
0122  * @nr_range: number of ranges to be mapped
0123  * @range: range to be mapped when nr_range == 1
0124  * @ranges: array of ranges to be mapped when nr_range > 1
0125  */
0126 struct dev_pagemap {
0127     struct vmem_altmap altmap;
0128     struct percpu_ref ref;
0129     struct completion done;
0130     enum memory_type type;
0131     unsigned int flags;
0132     unsigned long vmemmap_shift;
0133     const struct dev_pagemap_ops *ops;
0134     void *owner;
0135     int nr_range;
0136     union {
0137         struct range range;
0138         struct range ranges[0];
0139     };
0140 };
0141 
0142 static inline bool pgmap_has_memory_failure(struct dev_pagemap *pgmap)
0143 {
0144     return pgmap->ops && pgmap->ops->memory_failure;
0145 }
0146 
0147 static inline struct vmem_altmap *pgmap_altmap(struct dev_pagemap *pgmap)
0148 {
0149     if (pgmap->flags & PGMAP_ALTMAP_VALID)
0150         return &pgmap->altmap;
0151     return NULL;
0152 }
0153 
0154 static inline unsigned long pgmap_vmemmap_nr(struct dev_pagemap *pgmap)
0155 {
0156     return 1 << pgmap->vmemmap_shift;
0157 }
0158 
0159 static inline bool is_device_private_page(const struct page *page)
0160 {
0161     return IS_ENABLED(CONFIG_DEVICE_PRIVATE) &&
0162         is_zone_device_page(page) &&
0163         page->pgmap->type == MEMORY_DEVICE_PRIVATE;
0164 }
0165 
0166 static inline bool folio_is_device_private(const struct folio *folio)
0167 {
0168     return is_device_private_page(&folio->page);
0169 }
0170 
0171 static inline bool is_pci_p2pdma_page(const struct page *page)
0172 {
0173     return IS_ENABLED(CONFIG_PCI_P2PDMA) &&
0174         is_zone_device_page(page) &&
0175         page->pgmap->type == MEMORY_DEVICE_PCI_P2PDMA;
0176 }
0177 
0178 static inline bool is_device_coherent_page(const struct page *page)
0179 {
0180     return is_zone_device_page(page) &&
0181         page->pgmap->type == MEMORY_DEVICE_COHERENT;
0182 }
0183 
0184 static inline bool folio_is_device_coherent(const struct folio *folio)
0185 {
0186     return is_device_coherent_page(&folio->page);
0187 }
0188 
0189 #ifdef CONFIG_ZONE_DEVICE
0190 void *memremap_pages(struct dev_pagemap *pgmap, int nid);
0191 void memunmap_pages(struct dev_pagemap *pgmap);
0192 void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap);
0193 void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap);
0194 struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
0195         struct dev_pagemap *pgmap);
0196 bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn);
0197 
0198 unsigned long vmem_altmap_offset(struct vmem_altmap *altmap);
0199 void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns);
0200 unsigned long memremap_compat_align(void);
0201 #else
0202 static inline void *devm_memremap_pages(struct device *dev,
0203         struct dev_pagemap *pgmap)
0204 {
0205     /*
0206      * Fail attempts to call devm_memremap_pages() without
0207      * ZONE_DEVICE support enabled, this requires callers to fall
0208      * back to plain devm_memremap() based on config
0209      */
0210     WARN_ON_ONCE(1);
0211     return ERR_PTR(-ENXIO);
0212 }
0213 
0214 static inline void devm_memunmap_pages(struct device *dev,
0215         struct dev_pagemap *pgmap)
0216 {
0217 }
0218 
0219 static inline struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
0220         struct dev_pagemap *pgmap)
0221 {
0222     return NULL;
0223 }
0224 
0225 static inline bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn)
0226 {
0227     return false;
0228 }
0229 
0230 static inline unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
0231 {
0232     return 0;
0233 }
0234 
0235 static inline void vmem_altmap_free(struct vmem_altmap *altmap,
0236         unsigned long nr_pfns)
0237 {
0238 }
0239 
0240 /* when memremap_pages() is disabled all archs can remap a single page */
0241 static inline unsigned long memremap_compat_align(void)
0242 {
0243     return PAGE_SIZE;
0244 }
0245 #endif /* CONFIG_ZONE_DEVICE */
0246 
0247 static inline void put_dev_pagemap(struct dev_pagemap *pgmap)
0248 {
0249     if (pgmap)
0250         percpu_ref_put(&pgmap->ref);
0251 }
0252 
0253 #endif /* _LINUX_MEMREMAP_H_ */