1 /* 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> 37 #include <rdma/ib_verbs.h> 38 #include <linux/interval_tree.h> 39 40 struct ib_umem_odp { 41 struct ib_umem umem; 42 struct ib_ucontext_per_mm *per_mm; 43 44 /* 45 * An array of the pages included in the on-demand paging umem. 46 * Indices of pages that are currently not mapped into the device will 47 * contain NULL. 48 */ 49 struct page **page_list; 50 /* 51 * An array of the same size as page_list, with DMA addresses mapped 52 * for pages the pages in page_list. The lower two bits designate 53 * access permissions. See ODP_READ_ALLOWED_BIT and 54 * ODP_WRITE_ALLOWED_BIT. 55 */ 56 dma_addr_t *dma_list; 57 /* 58 * The umem_mutex protects the page_list and dma_list fields of an ODP 59 * umem, allowing only a single thread to map/unmap pages. The mutex 60 * also protects access to the mmu notifier counters. 61 */ 62 struct mutex umem_mutex; 63 void *private; /* for the HW driver to use. */ 64 65 int notifiers_seq; 66 int notifiers_count; 67 int npages; 68 69 /* Tree tracking */ 70 struct interval_tree_node interval_tree; 71 72 /* 73 * An implicit odp umem cannot be DMA mapped, has 0 length, and serves 74 * only as an anchor for the driver to hold onto the per_mm. FIXME: 75 * This should be removed and drivers should work with the per_mm 76 * directly. 77 */ 78 bool is_implicit_odp; 79 80 struct completion notifier_completion; 81 int dying; 82 unsigned int page_shift; 83 struct work_struct work; 84 }; 85 86 static inline struct ib_umem_odp *to_ib_umem_odp(struct ib_umem *umem) 87 { 88 return container_of(umem, struct ib_umem_odp, umem); 89 } 90 91 /* Returns the first page of an ODP umem. */ 92 static inline unsigned long ib_umem_start(struct ib_umem_odp *umem_odp) 93 { 94 return umem_odp->interval_tree.start; 95 } 96 97 /* Returns the address of the page after the last one of an ODP umem. */ 98 static inline unsigned long ib_umem_end(struct ib_umem_odp *umem_odp) 99 { 100 return umem_odp->interval_tree.last + 1; 101 } 102 103 static inline size_t ib_umem_odp_num_pages(struct ib_umem_odp *umem_odp) 104 { 105 return (ib_umem_end(umem_odp) - ib_umem_start(umem_odp)) >> 106 umem_odp->page_shift; 107 } 108 109 /* 110 * The lower 2 bits of the DMA address signal the R/W permissions for 111 * the entry. To upgrade the permissions, provide the appropriate 112 * bitmask to the map_dma_pages function. 113 * 114 * Be aware that upgrading a mapped address might result in change of 115 * the DMA address for the page. 116 */ 117 #define ODP_READ_ALLOWED_BIT (1<<0ULL) 118 #define ODP_WRITE_ALLOWED_BIT (1<<1ULL) 119 120 #define ODP_DMA_ADDR_MASK (~(ODP_READ_ALLOWED_BIT | ODP_WRITE_ALLOWED_BIT)) 121 122 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 123 124 struct ib_ucontext_per_mm { 125 struct ib_ucontext *context; 126 struct mm_struct *mm; 127 struct pid *tgid; 128 bool active; 129 130 struct rb_root_cached umem_tree; 131 /* Protects umem_tree */ 132 struct rw_semaphore umem_rwsem; 133 134 struct mmu_notifier mn; 135 unsigned int odp_mrs_count; 136 137 struct list_head ucontext_list; 138 struct rcu_head rcu; 139 }; 140 141 struct ib_umem_odp *ib_umem_odp_get(struct ib_udata *udata, unsigned long addr, 142 size_t size, int access); 143 struct ib_umem_odp *ib_umem_odp_alloc_implicit(struct ib_udata *udata, 144 int access); 145 struct ib_umem_odp *ib_umem_odp_alloc_child(struct ib_umem_odp *root_umem, 146 unsigned long addr, size_t size); 147 void ib_umem_odp_release(struct ib_umem_odp *umem_odp); 148 149 int ib_umem_odp_map_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset, 150 u64 bcnt, u64 access_mask, 151 unsigned long current_seq); 152 153 void ib_umem_odp_unmap_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset, 154 u64 bound); 155 156 typedef int (*umem_call_back)(struct ib_umem_odp *item, u64 start, u64 end, 157 void *cookie); 158 /* 159 * Call the callback on each ib_umem in the range. Returns the logical or of 160 * the return values of the functions called. 161 */ 162 int rbt_ib_umem_for_each_in_range(struct rb_root_cached *root, 163 u64 start, u64 end, 164 umem_call_back cb, 165 bool blockable, void *cookie); 166 167 /* 168 * Find first region intersecting with address range. 169 * Return NULL if not found 170 */ 171 static inline struct ib_umem_odp * 172 rbt_ib_umem_lookup(struct rb_root_cached *root, u64 addr, u64 length) 173 { 174 struct interval_tree_node *node; 175 176 node = interval_tree_iter_first(root, addr, addr + length - 1); 177 if (!node) 178 return NULL; 179 return container_of(node, struct ib_umem_odp, interval_tree); 180 181 } 182 183 static inline int ib_umem_mmu_notifier_retry(struct ib_umem_odp *umem_odp, 184 unsigned long mmu_seq) 185 { 186 /* 187 * This code is strongly based on the KVM code from 188 * mmu_notifier_retry. Should be called with 189 * the relevant locks taken (umem_odp->umem_mutex 190 * and the ucontext umem_mutex semaphore locked for read). 191 */ 192 193 if (unlikely(umem_odp->notifiers_count)) 194 return 1; 195 if (umem_odp->notifiers_seq != mmu_seq) 196 return 1; 197 return 0; 198 } 199 200 #else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */ 201 202 static inline struct ib_umem_odp *ib_umem_odp_get(struct ib_udata *udata, 203 unsigned long addr, 204 size_t size, int access) 205 { 206 return ERR_PTR(-EINVAL); 207 } 208 209 static inline void ib_umem_odp_release(struct ib_umem_odp *umem_odp) {} 210 211 #endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */ 212 213 #endif /* IB_UMEM_ODP_H */ 214