]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/hmm.h
Merge tag 'dma-mapping-5.15-1' of git://git.infradead.org/users/hch/dma-mapping
[mirror_ubuntu-jammy-kernel.git] / include / linux / hmm.h
CommitLineData
c942fddf 1/* SPDX-License-Identifier: GPL-2.0-or-later */
133ff0ea
JG
2/*
3 * Copyright 2013 Red Hat Inc.
4 *
f813f219 5 * Authors: Jérôme Glisse <jglisse@redhat.com>
133ff0ea 6 *
f970b977 7 * See Documentation/vm/hmm.rst for reasons and overview of what HMM is.
133ff0ea
JG
8 */
9#ifndef LINUX_HMM_H
10#define LINUX_HMM_H
11
12#include <linux/kconfig.h>
ca5999fd 13#include <linux/pgtable.h>
133ff0ea 14
858b54da 15#include <linux/device.h>
4ef589dc
JG
16#include <linux/migrate.h>
17#include <linux/memremap.h>
18#include <linux/completion.h>
a3e0d41c 19#include <linux/mmu_notifier.h>
4ef589dc 20
133ff0ea 21/*
2733ea14
JG
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
f88a1e90 31 *
2733ea14
JG
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.
f88a1e90 38 */
2733ea14 39enum hmm_pfn_flags {
3b50a6e5 40 /* Output fields and flags */
2733ea14
JG
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),
3b50a6e5 44 HMM_PFN_ORDER_SHIFT = (BITS_PER_LONG - 8),
2733ea14
JG
45
46 /* Input flags */
47 HMM_PFN_REQ_FAULT = HMM_PFN_VALID,
48 HMM_PFN_REQ_WRITE = HMM_PFN_WRITE,
49
3b50a6e5 50 HMM_PFN_FLAGS = 0xFFUL << HMM_PFN_ORDER_SHIFT,
f88a1e90
JG
51};
52
53/*
2733ea14 54 * hmm_pfn_to_page() - return struct page pointed to by a device entry
f88a1e90 55 *
2733ea14
JG
56 * This must be called under the caller 'user_lock' after a successful
57 * mmu_interval_read_begin(). The caller must have tested for HMM_PFN_VALID
58 * already.
133ff0ea 59 */
2733ea14
JG
60static inline struct page *hmm_pfn_to_page(unsigned long hmm_pfn)
61{
62 return pfn_to_page(hmm_pfn & ~HMM_PFN_FLAGS);
63}
f88a1e90 64
3b50a6e5
RC
65/*
66 * hmm_pfn_to_map_order() - return the CPU mapping size order
67 *
68 * This is optionally useful to optimize processing of the pfn result
69 * array. It indicates that the page starts at the order aligned VA and is
70 * 1<<order bytes long. Every pfn within an high order page will have the
71 * same pfn flags, both access protections and the map_order. The caller must
72 * be careful with edge cases as the start and end VA of the given page may
73 * extend past the range used with hmm_range_fault().
74 *
75 * This must be called under the caller 'user_lock' after a successful
76 * mmu_interval_read_begin(). The caller must have tested for HMM_PFN_VALID
77 * already.
78 */
79static inline unsigned int hmm_pfn_to_map_order(unsigned long hmm_pfn)
80{
81 return (hmm_pfn >> HMM_PFN_ORDER_SHIFT) & 0x1F;
82}
83
f88a1e90
JG
84/*
85 * struct hmm_range - track invalidation lock on virtual address range
86 *
a22dd506
JG
87 * @notifier: a mmu_interval_notifier that includes the start/end
88 * @notifier_seq: result of mmu_interval_read_begin()
f88a1e90
JG
89 * @start: range virtual start address (inclusive)
90 * @end: range virtual end address (exclusive)
2733ea14 91 * @hmm_pfns: array of pfns (big enough for the range)
023a019a
JG
92 * @default_flags: default flags for the range (write, read, ... see hmm doc)
93 * @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter
08ddddda 94 * @dev_private_owner: owner of device private pages
f88a1e90
JG
95 */
96struct hmm_range {
04ec32fb
JG
97 struct mmu_interval_notifier *notifier;
98 unsigned long notifier_seq;
f88a1e90
JG
99 unsigned long start;
100 unsigned long end;
2733ea14
JG
101 unsigned long *hmm_pfns;
102 unsigned long default_flags;
103 unsigned long pfn_flags_mask;
08ddddda 104 void *dev_private_owner;
f88a1e90 105};
133ff0ea 106
da4c3c73 107/*
a3e0d41c 108 * Please see Documentation/vm/hmm.rst for how to use the range API.
da4c3c73 109 */
be957c88 110int hmm_range_fault(struct hmm_range *range);
74eee180
JG
111
112/*
a3e0d41c 113 * HMM_RANGE_DEFAULT_TIMEOUT - default timeout (ms) when waiting for a range
74eee180 114 *
a3e0d41c 115 * When waiting for mmu notifiers we need some kind of time out otherwise we
06c88398 116 * could potentially wait for ever, 1000ms ie 1s sounds like a long time to
a3e0d41c 117 * wait already.
74eee180 118 */
a3e0d41c
JG
119#define HMM_RANGE_DEFAULT_TIMEOUT 1000
120
133ff0ea 121#endif /* LINUX_HMM_H */