blob: 3da0b167041b477e14a3e731e9de76e85148b46c [file] [log] [blame]
Shachar Raindel8ada2c12014-12-11 17:04:17 +02001/*
2 * Copyright (c) 2014 Mellanox Technologies. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#ifndef IB_UMEM_ODP_H
34#define IB_UMEM_ODP_H
35
36#include <rdma/ib_umem.h>
Haggai Eran882214e2014-12-11 17:04:18 +020037#include <rdma/ib_verbs.h>
38#include <linux/interval_tree.h>
39
40struct umem_odp_node {
41 u64 __subtree_last;
42 struct rb_node rb;
43};
Shachar Raindel8ada2c12014-12-11 17:04:17 +020044
45struct ib_umem_odp {
46 /*
47 * An array of the pages included in the on-demand paging umem.
48 * Indices of pages that are currently not mapped into the device will
49 * contain NULL.
50 */
51 struct page **page_list;
52 /*
53 * An array of the same size as page_list, with DMA addresses mapped
54 * for pages the pages in page_list. The lower two bits designate
55 * access permissions. See ODP_READ_ALLOWED_BIT and
56 * ODP_WRITE_ALLOWED_BIT.
57 */
58 dma_addr_t *dma_list;
59 /*
60 * The umem_mutex protects the page_list and dma_list fields of an ODP
Haggai Eran882214e2014-12-11 17:04:18 +020061 * umem, allowing only a single thread to map/unmap pages. The mutex
62 * also protects access to the mmu notifier counters.
Shachar Raindel8ada2c12014-12-11 17:04:17 +020063 */
64 struct mutex umem_mutex;
65 void *private; /* for the HW driver to use. */
Haggai Eran882214e2014-12-11 17:04:18 +020066
67 /* When false, use the notifier counter in the ucontext struct. */
68 bool mn_counters_active;
69 int notifiers_seq;
70 int notifiers_count;
71
72 /* A linked list of umems that don't have private mmu notifier
73 * counters yet. */
74 struct list_head no_private_counters;
75 struct ib_umem *umem;
76
77 /* Tree tracking */
78 struct umem_odp_node interval_tree;
79
80 struct completion notifier_completion;
81 int dying;
Shachar Raindel8ada2c12014-12-11 17:04:17 +020082};
83
84#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
85
86int ib_umem_odp_get(struct ib_ucontext *context, struct ib_umem *umem);
87
88void ib_umem_odp_release(struct ib_umem *umem);
89
90/*
91 * The lower 2 bits of the DMA address signal the R/W permissions for
92 * the entry. To upgrade the permissions, provide the appropriate
93 * bitmask to the map_dma_pages function.
94 *
95 * Be aware that upgrading a mapped address might result in change of
96 * the DMA address for the page.
97 */
98#define ODP_READ_ALLOWED_BIT (1<<0ULL)
99#define ODP_WRITE_ALLOWED_BIT (1<<1ULL)
100
101#define ODP_DMA_ADDR_MASK (~(ODP_READ_ALLOWED_BIT | ODP_WRITE_ALLOWED_BIT))
102
103int ib_umem_odp_map_dma_pages(struct ib_umem *umem, u64 start_offset, u64 bcnt,
104 u64 access_mask, unsigned long current_seq);
105
106void ib_umem_odp_unmap_dma_pages(struct ib_umem *umem, u64 start_offset,
107 u64 bound);
108
Haggai Eran882214e2014-12-11 17:04:18 +0200109void rbt_ib_umem_insert(struct umem_odp_node *node, struct rb_root *root);
110void rbt_ib_umem_remove(struct umem_odp_node *node, struct rb_root *root);
111typedef int (*umem_call_back)(struct ib_umem *item, u64 start, u64 end,
112 void *cookie);
113/*
114 * Call the callback on each ib_umem in the range. Returns the logical or of
115 * the return values of the functions called.
116 */
117int rbt_ib_umem_for_each_in_range(struct rb_root *root, u64 start, u64 end,
118 umem_call_back cb, void *cookie);
119
120struct umem_odp_node *rbt_ib_umem_iter_first(struct rb_root *root,
121 u64 start, u64 last);
122struct umem_odp_node *rbt_ib_umem_iter_next(struct umem_odp_node *node,
123 u64 start, u64 last);
124
125static inline int ib_umem_mmu_notifier_retry(struct ib_umem *item,
126 unsigned long mmu_seq)
127{
128 /*
129 * This code is strongly based on the KVM code from
130 * mmu_notifier_retry. Should be called with
131 * the relevant locks taken (item->odp_data->umem_mutex
132 * and the ucontext umem_mutex semaphore locked for read).
133 */
134
135 /* Do not allow page faults while the new ib_umem hasn't seen a state
136 * with zero notifiers yet, and doesn't have its own valid set of
137 * private counters. */
138 if (!item->odp_data->mn_counters_active)
139 return 1;
140
141 if (unlikely(item->odp_data->notifiers_count))
142 return 1;
143 if (item->odp_data->notifiers_seq != mmu_seq)
144 return 1;
145 return 0;
146}
147
Shachar Raindel8ada2c12014-12-11 17:04:17 +0200148#else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
149
150static inline int ib_umem_odp_get(struct ib_ucontext *context,
151 struct ib_umem *umem)
152{
153 return -EINVAL;
154}
155
156static inline void ib_umem_odp_release(struct ib_umem *umem) {}
157
158#endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
159
160#endif /* IB_UMEM_ODP_H */