xref: /openbmc/linux/include/linux/hmm.h (revision 04b3c795)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright 2013 Red Hat Inc.
4  *
5  * Authors: Jérôme Glisse <jglisse@redhat.com>
6  *
7  * See Documentation/vm/hmm.rst for reasons and overview of what HMM is.
8  */
9 #ifndef LINUX_HMM_H
10 #define LINUX_HMM_H
11 
12 #include <linux/kconfig.h>
13 #include <linux/pgtable.h>
14 
15 #include <linux/device.h>
16 #include <linux/migrate.h>
17 #include <linux/memremap.h>
18 #include <linux/completion.h>
19 #include <linux/mmu_notifier.h>
20 
21 /*
22  * On output:
23  * 0             - The page is faultable and a future call with
24  *                 HMM_PFN_REQ_FAULT could succeed.
25  * HMM_PFN_VALID - the pfn field points to a valid PFN. This PFN is at
26  *                 least readable. If dev_private_owner is !NULL then this could
27  *                 point at a DEVICE_PRIVATE page.
28  * HMM_PFN_WRITE - if the page memory can be written to (requires HMM_PFN_VALID)
29  * HMM_PFN_ERROR - accessing the pfn is impossible and the device should
30  *                 fail. ie poisoned memory, special pages, no vma, etc
31  *
32  * On input:
33  * 0                 - Return the current state of the page, do not fault it.
34  * HMM_PFN_REQ_FAULT - The output must have HMM_PFN_VALID or hmm_range_fault()
35  *                     will fail
36  * HMM_PFN_REQ_WRITE - The output must have HMM_PFN_WRITE or hmm_range_fault()
37  *                     will fail. Must be combined with HMM_PFN_REQ_FAULT.
38  */
39 enum hmm_pfn_flags {
40 	/* Output flags */
41 	HMM_PFN_VALID = 1UL << (BITS_PER_LONG - 1),
42 	HMM_PFN_WRITE = 1UL << (BITS_PER_LONG - 2),
43 	HMM_PFN_ERROR = 1UL << (BITS_PER_LONG - 3),
44 
45 	/* Input flags */
46 	HMM_PFN_REQ_FAULT = HMM_PFN_VALID,
47 	HMM_PFN_REQ_WRITE = HMM_PFN_WRITE,
48 
49 	HMM_PFN_FLAGS = HMM_PFN_VALID | HMM_PFN_WRITE | HMM_PFN_ERROR,
50 };
51 
52 /*
53  * hmm_pfn_to_page() - return struct page pointed to by a device entry
54  *
55  * This must be called under the caller 'user_lock' after a successful
56  * mmu_interval_read_begin(). The caller must have tested for HMM_PFN_VALID
57  * already.
58  */
59 static inline struct page *hmm_pfn_to_page(unsigned long hmm_pfn)
60 {
61 	return pfn_to_page(hmm_pfn & ~HMM_PFN_FLAGS);
62 }
63 
64 /*
65  * struct hmm_range - track invalidation lock on virtual address range
66  *
67  * @notifier: a mmu_interval_notifier that includes the start/end
68  * @notifier_seq: result of mmu_interval_read_begin()
69  * @start: range virtual start address (inclusive)
70  * @end: range virtual end address (exclusive)
71  * @hmm_pfns: array of pfns (big enough for the range)
72  * @default_flags: default flags for the range (write, read, ... see hmm doc)
73  * @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter
74  * @dev_private_owner: owner of device private pages
75  */
76 struct hmm_range {
77 	struct mmu_interval_notifier *notifier;
78 	unsigned long		notifier_seq;
79 	unsigned long		start;
80 	unsigned long		end;
81 	unsigned long		*hmm_pfns;
82 	unsigned long		default_flags;
83 	unsigned long		pfn_flags_mask;
84 	void			*dev_private_owner;
85 };
86 
87 /*
88  * Please see Documentation/vm/hmm.rst for how to use the range API.
89  */
90 int hmm_range_fault(struct hmm_range *range);
91 
92 /*
93  * HMM_RANGE_DEFAULT_TIMEOUT - default timeout (ms) when waiting for a range
94  *
95  * When waiting for mmu notifiers we need some kind of time out otherwise we
96  * could potentialy wait for ever, 1000ms ie 1s sounds like a long time to
97  * wait already.
98  */
99 #define HMM_RANGE_DEFAULT_TIMEOUT 1000
100 
101 #endif /* LINUX_HMM_H */
102