blob: 6fefb09af7c34ddc550debf429e6769e77c2cd40 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Dan Williams9476df72016-01-15 16:56:19 -08002#ifndef _LINUX_MEMREMAP_H_
3#define _LINUX_MEMREMAP_H_
Dan Williams5c2c2582016-01-15 16:56:49 -08004#include <linux/ioport.h>
5#include <linux/percpu-refcount.h>
Dan Williams9476df72016-01-15 16:56:19 -08006
7struct resource;
8struct device;
Dan Williams4b94ffd2016-01-15 16:56:22 -08009
10/**
11 * struct vmem_altmap - pre-allocated storage for vmemmap_populate
12 * @base_pfn: base of the entire dev_pagemap mapping
13 * @reserve: pages mapped, but reserved for driver use (relative to @base)
14 * @free: free pages set aside in the mapping for memmap storage
15 * @align: pages reserved to meet allocation alignments
16 * @alloc: track pages consumed, private to vmemmap_populate()
17 */
18struct vmem_altmap {
19 const unsigned long base_pfn;
Aneesh Kumar K.Vcf387d92019-09-10 11:58:25 +053020 const unsigned long end_pfn;
Dan Williams4b94ffd2016-01-15 16:56:22 -080021 const unsigned long reserve;
22 unsigned long free;
23 unsigned long align;
24 unsigned long alloc;
25};
26
Jérôme Glisse5042db42017-09-08 16:11:43 -070027/*
28 * Specialize ZONE_DEVICE memory into multiple types each having differents
29 * usage.
30 *
Jérôme Glisse5042db42017-09-08 16:11:43 -070031 * MEMORY_DEVICE_PRIVATE:
32 * Device memory that is not directly addressable by the CPU: CPU can neither
33 * read nor write private memory. In this case, we do still have struct pages
34 * backing the device memory. Doing so simplifies the implementation, but it is
35 * important to remember that there are certain points at which the struct page
36 * must be treated as an opaque object, rather than a "normal" struct page.
37 *
38 * A more complete discussion of unaddressable memory may be found in
Mike Rapoportad56b732018-03-21 21:22:47 +020039 * include/linux/hmm.h and Documentation/vm/hmm.rst.
Jérôme Glissedf6ad692017-09-08 16:12:24 -070040 *
Dan Williamse76384882018-05-16 11:46:08 -070041 * MEMORY_DEVICE_FS_DAX:
42 * Host memory that has similar access semantics as System RAM i.e. DMA
43 * coherent and supports page pinning. In support of coordinating page
44 * pinning vs other operations MEMORY_DEVICE_FS_DAX arranges for a
45 * wakeup event whenever a page is unpinned and becomes idle. This
46 * wakeup is used to coordinate physical address space management (ex:
47 * fs truncate/hole punch) vs pinned pages (ex: device dma).
Logan Gunthorpe52916982018-10-04 15:27:35 -060048 *
Christoph Hellwig3ed2dcd2019-06-26 14:27:07 +020049 * MEMORY_DEVICE_DEVDAX:
50 * Host memory that has similar access semantics as System RAM i.e. DMA
51 * coherent and supports page pinning. In contrast to
52 * MEMORY_DEVICE_FS_DAX, this memory is access via a device-dax
53 * character device.
54 *
Logan Gunthorpe52916982018-10-04 15:27:35 -060055 * MEMORY_DEVICE_PCI_P2PDMA:
56 * Device memory residing in a PCI BAR intended for use with Peer-to-Peer
57 * transactions.
Jérôme Glisse5042db42017-09-08 16:11:43 -070058 */
59enum memory_type {
Christoph Hellwig3ed2dcd2019-06-26 14:27:07 +020060 /* 0 is reserved to catch uninitialized type fields */
Dan Williamse76384882018-05-16 11:46:08 -070061 MEMORY_DEVICE_PRIVATE = 1,
Dan Williamse76384882018-05-16 11:46:08 -070062 MEMORY_DEVICE_FS_DAX,
Christoph Hellwig3ed2dcd2019-06-26 14:27:07 +020063 MEMORY_DEVICE_DEVDAX,
Logan Gunthorpe52916982018-10-04 15:27:35 -060064 MEMORY_DEVICE_PCI_P2PDMA,
Jérôme Glisse5042db42017-09-08 16:11:43 -070065};
66
Christoph Hellwig1e240e82019-06-26 14:27:08 +020067struct dev_pagemap_ops {
68 /*
69 * Called once the page refcount reaches 1. (ZONE_DEVICE pages never
70 * reach 0 refcount unless there is a refcount bug. This allows the
71 * device driver to implement its own memory management.)
72 */
Christoph Hellwig80a72d02019-06-26 14:27:12 +020073 void (*page_free)(struct page *page);
Christoph Hellwig1e240e82019-06-26 14:27:08 +020074
75 /*
76 * Transition the refcount in struct dev_pagemap to the dead state.
77 */
Christoph Hellwigd8668bb2019-06-26 14:27:09 +020078 void (*kill)(struct dev_pagemap *pgmap);
Christoph Hellwig1e240e82019-06-26 14:27:08 +020079
80 /*
81 * Wait for refcount in struct dev_pagemap to be idle and reap it.
82 */
Christoph Hellwigd8668bb2019-06-26 14:27:09 +020083 void (*cleanup)(struct dev_pagemap *pgmap);
Christoph Hellwig897e6362019-06-26 14:27:11 +020084
85 /*
86 * Used for private (un-addressable) device memory only. Must migrate
87 * the page back to a CPU accessible page.
88 */
89 vm_fault_t (*migrate_to_ram)(struct vm_fault *vmf);
Christoph Hellwig1e240e82019-06-26 14:27:08 +020090};
Jérôme Glisse5042db42017-09-08 16:11:43 -070091
Christoph Hellwig514caf22019-06-26 14:27:13 +020092#define PGMAP_ALTMAP_VALID (1 << 0)
93
Dan Williams9476df72016-01-15 16:56:19 -080094/**
95 * struct dev_pagemap - metadata for ZONE_DEVICE mappings
Dan Williams4b94ffd2016-01-15 16:56:22 -080096 * @altmap: pre-allocated/reserved memory for vmemmap allocations
Dan Williams5c2c2582016-01-15 16:56:49 -080097 * @res: physical address range covered by @ref
98 * @ref: reference count that pins the devm_memremap_pages() mapping
Christoph Hellwig24917f62019-06-26 14:27:14 +020099 * @internal_ref: internal reference if @ref is not provided by the caller
100 * @done: completion for @internal_ref
Dan Williams9476df72016-01-15 16:56:19 -0800101 * @dev: host device of the mapping for debug
Jérôme Glisse5042db42017-09-08 16:11:43 -0700102 * @data: private data pointer for page_free()
103 * @type: memory type: see MEMORY_* in memory_hotplug.h
Christoph Hellwig514caf22019-06-26 14:27:13 +0200104 * @flags: PGMAP_* flags to specify defailed behavior
Christoph Hellwig1e240e82019-06-26 14:27:08 +0200105 * @ops: method table
Dan Williams9476df72016-01-15 16:56:19 -0800106 */
107struct dev_pagemap {
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100108 struct vmem_altmap altmap;
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100109 struct resource res;
Dan Williams5c2c2582016-01-15 16:56:49 -0800110 struct percpu_ref *ref;
Christoph Hellwig24917f62019-06-26 14:27:14 +0200111 struct percpu_ref internal_ref;
112 struct completion done;
Jérôme Glisse5042db42017-09-08 16:11:43 -0700113 enum memory_type type;
Christoph Hellwig514caf22019-06-26 14:27:13 +0200114 unsigned int flags;
Christoph Hellwig1e240e82019-06-26 14:27:08 +0200115 const struct dev_pagemap_ops *ops;
Dan Williams9476df72016-01-15 16:56:19 -0800116};
117
Christoph Hellwig514caf22019-06-26 14:27:13 +0200118static inline struct vmem_altmap *pgmap_altmap(struct dev_pagemap *pgmap)
119{
120 if (pgmap->flags & PGMAP_ALTMAP_VALID)
121 return &pgmap->altmap;
122 return NULL;
123}
124
Dan Williams9476df72016-01-15 16:56:19 -0800125#ifdef CONFIG_ZONE_DEVICE
Christoph Hellwig6869b7b2019-08-18 11:05:57 +0200126void *memremap_pages(struct dev_pagemap *pgmap, int nid);
127void memunmap_pages(struct dev_pagemap *pgmap);
Christoph Hellwige8d51342017-12-29 08:54:05 +0100128void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap);
Dan Williams2e3f1392019-06-13 15:56:21 -0700129void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap);
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100130struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
131 struct dev_pagemap *pgmap);
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700132
Christoph Hellwig8e37d002017-12-29 08:53:50 +0100133unsigned long vmem_altmap_offset(struct vmem_altmap *altmap);
134void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns);
Dan Williams9476df72016-01-15 16:56:19 -0800135#else
136static inline void *devm_memremap_pages(struct device *dev,
Christoph Hellwige8d51342017-12-29 08:54:05 +0100137 struct dev_pagemap *pgmap)
Dan Williams9476df72016-01-15 16:56:19 -0800138{
139 /*
140 * Fail attempts to call devm_memremap_pages() without
141 * ZONE_DEVICE support enabled, this requires callers to fall
142 * back to plain devm_memremap() based on config
143 */
144 WARN_ON_ONCE(1);
145 return ERR_PTR(-ENXIO);
146}
147
Dan Williams2e3f1392019-06-13 15:56:21 -0700148static inline void devm_memunmap_pages(struct device *dev,
149 struct dev_pagemap *pgmap)
150{
151}
152
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100153static inline struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
154 struct dev_pagemap *pgmap)
Dan Williams9476df72016-01-15 16:56:19 -0800155{
156 return NULL;
157}
Christoph Hellwig8e37d002017-12-29 08:53:50 +0100158
159static inline unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
160{
161 return 0;
162}
163
164static inline void vmem_altmap_free(struct vmem_altmap *altmap,
165 unsigned long nr_pfns)
166{
167}
168#endif /* CONFIG_ZONE_DEVICE */
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700169
Dan Williams5c2c2582016-01-15 16:56:49 -0800170static inline void put_dev_pagemap(struct dev_pagemap *pgmap)
171{
172 if (pgmap)
173 percpu_ref_put(pgmap->ref);
174}
Dan Williams9476df72016-01-15 16:56:19 -0800175#endif /* _LINUX_MEMREMAP_H_ */