1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright 2013 Red Hat Inc.
5 * Authors: Jérôme Glisse <jglisse@redhat.com>
7 * See Documentation/mm/hmm.rst for reasons and overview of what HMM is.
14 struct mmu_interval_notifier;
18 * 0 - The page is faultable and a future call with
19 * HMM_PFN_REQ_FAULT could succeed.
20 * HMM_PFN_VALID - the pfn field points to a valid PFN. This PFN is at
21 * least readable. If dev_private_owner is !NULL then this could
22 * point at a DEVICE_PRIVATE page.
23 * HMM_PFN_WRITE - if the page memory can be written to (requires HMM_PFN_VALID)
24 * HMM_PFN_ERROR - accessing the pfn is impossible and the device should
25 * fail. ie poisoned memory, special pages, no vma, etc
26 * HMM_PFN_P2PDMA - P2P page
27 * HMM_PFN_P2PDMA_BUS - Bus mapped P2P transfer
28 * HMM_PFN_DMA_MAPPED - Flag preserved on input-to-output transformation
29 * to mark that page is already DMA mapped
32 * 0 - Return the current state of the page, do not fault it.
33 * HMM_PFN_REQ_FAULT - The output must have HMM_PFN_VALID or hmm_range_fault()
35 * HMM_PFN_REQ_WRITE - The output must have HMM_PFN_WRITE or hmm_range_fault()
36 * will fail. Must be combined with HMM_PFN_REQ_FAULT.
39 /* Output fields and flags */
40 HMM_PFN_VALID = 1UL << (BITS_PER_LONG - 1),
41 HMM_PFN_WRITE = 1UL << (BITS_PER_LONG - 2),
42 HMM_PFN_ERROR = 1UL << (BITS_PER_LONG - 3),
44 * Sticky flags, carried from input to output,
45 * don't forget to update HMM_PFN_INOUT_FLAGS
47 HMM_PFN_DMA_MAPPED = 1UL << (BITS_PER_LONG - 4),
48 HMM_PFN_P2PDMA = 1UL << (BITS_PER_LONG - 5),
49 HMM_PFN_P2PDMA_BUS = 1UL << (BITS_PER_LONG - 6),
51 HMM_PFN_ORDER_SHIFT = (BITS_PER_LONG - 11),
54 HMM_PFN_REQ_FAULT = HMM_PFN_VALID,
55 HMM_PFN_REQ_WRITE = HMM_PFN_WRITE,
57 HMM_PFN_FLAGS = ~((1UL << HMM_PFN_ORDER_SHIFT) - 1),
61 * hmm_pfn_to_page() - return struct page pointed to by a device entry
63 * This must be called under the caller 'user_lock' after a successful
64 * mmu_interval_read_begin(). The caller must have tested for HMM_PFN_VALID
67 static inline struct page *hmm_pfn_to_page(unsigned long hmm_pfn)
69 return pfn_to_page(hmm_pfn & ~HMM_PFN_FLAGS);
73 * hmm_pfn_to_phys() - return physical address pointed to by a device entry
75 static inline phys_addr_t hmm_pfn_to_phys(unsigned long hmm_pfn)
77 return __pfn_to_phys(hmm_pfn & ~HMM_PFN_FLAGS);
81 * hmm_pfn_to_map_order() - return the CPU mapping size order
83 * This is optionally useful to optimize processing of the pfn result
84 * array. It indicates that the page starts at the order aligned VA and is
85 * 1<<order bytes long. Every pfn within an high order page will have the
86 * same pfn flags, both access protections and the map_order. The caller must
87 * be careful with edge cases as the start and end VA of the given page may
88 * extend past the range used with hmm_range_fault().
90 * This must be called under the caller 'user_lock' after a successful
91 * mmu_interval_read_begin(). The caller must have tested for HMM_PFN_VALID
94 static inline unsigned int hmm_pfn_to_map_order(unsigned long hmm_pfn)
96 return (hmm_pfn >> HMM_PFN_ORDER_SHIFT) & 0x1F;
100 * struct hmm_range - track invalidation lock on virtual address range
102 * @notifier: a mmu_interval_notifier that includes the start/end
103 * @notifier_seq: result of mmu_interval_read_begin()
104 * @start: range virtual start address (inclusive)
105 * @end: range virtual end address (exclusive)
106 * @hmm_pfns: array of pfns (big enough for the range)
107 * @default_flags: default flags for the range (write, read, ... see hmm doc)
108 * @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter
109 * @dev_private_owner: owner of device private pages
112 struct mmu_interval_notifier *notifier;
113 unsigned long notifier_seq;
116 unsigned long *hmm_pfns;
117 unsigned long default_flags;
118 unsigned long pfn_flags_mask;
119 void *dev_private_owner;
123 * Please see Documentation/mm/hmm.rst for how to use the range API.
125 int hmm_range_fault(struct hmm_range *range);
128 * HMM_RANGE_DEFAULT_TIMEOUT - default timeout (ms) when waiting for a range
130 * When waiting for mmu notifiers we need some kind of time out otherwise we
131 * could potentially wait for ever, 1000ms ie 1s sounds like a long time to
134 #define HMM_RANGE_DEFAULT_TIMEOUT 1000
136 #endif /* LINUX_HMM_H */