Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 1 | /* |
Jubin John | 05d6ac1 | 2016-02-14 20:22:17 -0800 | [diff] [blame] | 2 | * Copyright(c) 2015, 2016 Intel Corporation. |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 3 | * |
| 4 | * This file is provided under a dual BSD/GPLv2 license. When using or |
| 5 | * redistributing this file, you may do so under either license. |
| 6 | * |
| 7 | * GPL LICENSE SUMMARY |
| 8 | * |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of version 2 of the GNU General Public License as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * BSD LICENSE |
| 19 | * |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 20 | * Redistribution and use in source and binary forms, with or without |
| 21 | * modification, are permitted provided that the following conditions |
| 22 | * are met: |
| 23 | * |
| 24 | * - Redistributions of source code must retain the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer. |
| 26 | * - Redistributions in binary form must reproduce the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer in |
| 28 | * the documentation and/or other materials provided with the |
| 29 | * distribution. |
| 30 | * - Neither the name of Intel Corporation nor the names of its |
| 31 | * contributors may be used to endorse or promote products derived |
| 32 | * from this software without specific prior written permission. |
| 33 | * |
| 34 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 35 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 36 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 37 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 38 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 39 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 40 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 41 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 42 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 43 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 44 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 45 | * |
| 46 | */ |
| 47 | #include <asm/page.h> |
| 48 | |
| 49 | #include "user_exp_rcv.h" |
| 50 | #include "trace.h" |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 51 | #include "mmu_rb.h" |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 52 | |
Mitko Haralanov | b8abe34 | 2016-02-05 11:57:51 -0500 | [diff] [blame] | 53 | struct tid_group { |
| 54 | struct list_head list; |
| 55 | unsigned base; |
| 56 | u8 size; |
| 57 | u8 used; |
| 58 | u8 map; |
| 59 | }; |
| 60 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 61 | struct tid_rb_node { |
| 62 | struct mmu_rb_node mmu; |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 63 | unsigned long phys; |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 64 | struct tid_group *grp; |
| 65 | u32 rcventry; |
| 66 | dma_addr_t dma_addr; |
| 67 | bool freed; |
| 68 | unsigned npages; |
| 69 | struct page *pages[0]; |
| 70 | }; |
| 71 | |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 72 | struct tid_pageset { |
| 73 | u16 idx; |
| 74 | u16 count; |
| 75 | }; |
| 76 | |
Mitko Haralanov | b8abe34 | 2016-02-05 11:57:51 -0500 | [diff] [blame] | 77 | #define EXP_TID_SET_EMPTY(set) (set.count == 0 && list_empty(&set.list)) |
| 78 | |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 79 | #define num_user_pages(vaddr, len) \ |
| 80 | (1 + (((((unsigned long)(vaddr) + \ |
| 81 | (unsigned long)(len) - 1) & PAGE_MASK) - \ |
| 82 | ((unsigned long)vaddr & PAGE_MASK)) >> PAGE_SHIFT)) |
| 83 | |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 84 | static void unlock_exp_tids(struct hfi1_ctxtdata *, struct exp_tid_set *, |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 85 | struct hfi1_filedata *); |
Mitko Haralanov | 7e7a436e | 2016-02-05 11:57:57 -0500 | [diff] [blame] | 86 | static u32 find_phys_blocks(struct page **, unsigned, struct tid_pageset *); |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 87 | static int set_rcvarray_entry(struct file *, unsigned long, u32, |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 88 | struct tid_group *, struct page **, unsigned); |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 89 | static int tid_rb_insert(void *, struct mmu_rb_node *); |
Dean Luick | 082b353 | 2016-07-28 15:21:25 -0400 | [diff] [blame^] | 90 | static void tid_rb_remove(void *, struct mmu_rb_node *); |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 91 | static int tid_rb_invalidate(void *, struct mmu_rb_node *); |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 92 | static int program_rcvarray(struct file *, unsigned long, struct tid_group *, |
| 93 | struct tid_pageset *, unsigned, u16, struct page **, |
Mitko Haralanov | 7e7a436e | 2016-02-05 11:57:57 -0500 | [diff] [blame] | 94 | u32 *, unsigned *, unsigned *); |
Mitko Haralanov | 455d7f1 | 2016-02-05 11:57:56 -0500 | [diff] [blame] | 95 | static int unprogram_rcvarray(struct file *, u32, struct tid_group **); |
Ira Weiny | 5ed3b15 | 2016-07-28 12:27:32 -0400 | [diff] [blame] | 96 | static void clear_tid_node(struct hfi1_filedata *, struct tid_rb_node *); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 97 | |
| 98 | static struct mmu_rb_ops tid_rb_ops = { |
Dean Luick | a7cd2dc | 2016-07-28 12:27:37 -0400 | [diff] [blame] | 99 | .insert = tid_rb_insert, |
| 100 | .remove = tid_rb_remove, |
| 101 | .invalidate = tid_rb_invalidate |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 102 | }; |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 103 | |
| 104 | static inline u32 rcventry2tidinfo(u32 rcventry) |
| 105 | { |
| 106 | u32 pair = rcventry & ~0x1; |
| 107 | |
| 108 | return EXP_TID_SET(IDX, pair >> 1) | |
| 109 | EXP_TID_SET(CTRL, 1 << (rcventry - pair)); |
| 110 | } |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 111 | |
Mitko Haralanov | b8abe34 | 2016-02-05 11:57:51 -0500 | [diff] [blame] | 112 | static inline void exp_tid_group_init(struct exp_tid_set *set) |
| 113 | { |
| 114 | INIT_LIST_HEAD(&set->list); |
| 115 | set->count = 0; |
| 116 | } |
| 117 | |
| 118 | static inline void tid_group_remove(struct tid_group *grp, |
| 119 | struct exp_tid_set *set) |
| 120 | { |
| 121 | list_del_init(&grp->list); |
| 122 | set->count--; |
| 123 | } |
| 124 | |
| 125 | static inline void tid_group_add_tail(struct tid_group *grp, |
| 126 | struct exp_tid_set *set) |
| 127 | { |
| 128 | list_add_tail(&grp->list, &set->list); |
| 129 | set->count++; |
| 130 | } |
| 131 | |
| 132 | static inline struct tid_group *tid_group_pop(struct exp_tid_set *set) |
| 133 | { |
| 134 | struct tid_group *grp = |
| 135 | list_first_entry(&set->list, struct tid_group, list); |
| 136 | list_del_init(&grp->list); |
| 137 | set->count--; |
| 138 | return grp; |
| 139 | } |
| 140 | |
| 141 | static inline void tid_group_move(struct tid_group *group, |
| 142 | struct exp_tid_set *s1, |
| 143 | struct exp_tid_set *s2) |
| 144 | { |
| 145 | tid_group_remove(group, s1); |
| 146 | tid_group_add_tail(group, s2); |
| 147 | } |
| 148 | |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 149 | /* |
| 150 | * Initialize context and file private data needed for Expected |
| 151 | * receive caching. This needs to be done after the context has |
| 152 | * been configured with the eager/expected RcvEntry counts. |
| 153 | */ |
| 154 | int hfi1_user_exp_rcv_init(struct file *fp) |
| 155 | { |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 156 | struct hfi1_filedata *fd = fp->private_data; |
| 157 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
| 158 | struct hfi1_devdata *dd = uctxt->dd; |
| 159 | unsigned tidbase; |
| 160 | int i, ret = 0; |
| 161 | |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 162 | spin_lock_init(&fd->tid_lock); |
| 163 | spin_lock_init(&fd->invalid_lock); |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 164 | |
| 165 | if (!uctxt->subctxt_cnt || !fd->subctxt) { |
| 166 | exp_tid_group_init(&uctxt->tid_group_list); |
| 167 | exp_tid_group_init(&uctxt->tid_used_list); |
| 168 | exp_tid_group_init(&uctxt->tid_full_list); |
| 169 | |
| 170 | tidbase = uctxt->expected_base; |
| 171 | for (i = 0; i < uctxt->expected_count / |
| 172 | dd->rcv_entries.group_size; i++) { |
| 173 | struct tid_group *grp; |
| 174 | |
| 175 | grp = kzalloc(sizeof(*grp), GFP_KERNEL); |
| 176 | if (!grp) { |
| 177 | /* |
| 178 | * If we fail here, the groups already |
| 179 | * allocated will be freed by the close |
| 180 | * call. |
| 181 | */ |
| 182 | ret = -ENOMEM; |
| 183 | goto done; |
| 184 | } |
| 185 | grp->size = dd->rcv_entries.group_size; |
| 186 | grp->base = tidbase; |
| 187 | tid_group_add_tail(grp, &uctxt->tid_group_list); |
| 188 | tidbase += dd->rcv_entries.group_size; |
| 189 | } |
| 190 | } |
| 191 | |
Mitko Haralanov | a92ba6d | 2016-02-03 14:34:41 -0800 | [diff] [blame] | 192 | fd->entry_to_rb = kcalloc(uctxt->expected_count, |
| 193 | sizeof(struct rb_node *), |
| 194 | GFP_KERNEL); |
| 195 | if (!fd->entry_to_rb) |
| 196 | return -ENOMEM; |
| 197 | |
Dean Luick | 622c202 | 2016-07-28 15:21:21 -0400 | [diff] [blame] | 198 | if (!HFI1_CAP_UGET_MASK(uctxt->flags, TID_UNMAP)) { |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 199 | fd->invalid_tid_idx = 0; |
| 200 | fd->invalid_tids = kzalloc(uctxt->expected_count * |
| 201 | sizeof(u32), GFP_KERNEL); |
| 202 | if (!fd->invalid_tids) { |
| 203 | ret = -ENOMEM; |
| 204 | goto done; |
Mitko Haralanov | a92ba6d | 2016-02-03 14:34:41 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Register MMU notifier callbacks. If the registration |
Dean Luick | 622c202 | 2016-07-28 15:21:21 -0400 | [diff] [blame] | 209 | * fails, continue without TID caching for this context. |
Mitko Haralanov | a92ba6d | 2016-02-03 14:34:41 -0800 | [diff] [blame] | 210 | */ |
Dean Luick | b85ced9 | 2016-07-28 15:21:24 -0400 | [diff] [blame] | 211 | ret = hfi1_mmu_rb_register(fd, fd->mm, &tid_rb_ops, |
| 212 | dd->pport->hfi1_wq, |
| 213 | &fd->handler); |
Mitko Haralanov | a92ba6d | 2016-02-03 14:34:41 -0800 | [diff] [blame] | 214 | if (ret) { |
| 215 | dd_dev_info(dd, |
| 216 | "Failed MMU notifier registration %d\n", |
| 217 | ret); |
Mitko Haralanov | a92ba6d | 2016-02-03 14:34:41 -0800 | [diff] [blame] | 218 | ret = 0; |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 222 | /* |
| 223 | * PSM does not have a good way to separate, count, and |
| 224 | * effectively enforce a limit on RcvArray entries used by |
| 225 | * subctxts (when context sharing is used) when TID caching |
| 226 | * is enabled. To help with that, we calculate a per-process |
| 227 | * RcvArray entry share and enforce that. |
| 228 | * If TID caching is not in use, PSM deals with usage on its |
| 229 | * own. In that case, we allow any subctxt to take all of the |
| 230 | * entries. |
| 231 | * |
| 232 | * Make sure that we set the tid counts only after successful |
| 233 | * init. |
| 234 | */ |
Mitko Haralanov | 455d7f1 | 2016-02-05 11:57:56 -0500 | [diff] [blame] | 235 | spin_lock(&fd->tid_lock); |
Dean Luick | 622c202 | 2016-07-28 15:21:21 -0400 | [diff] [blame] | 236 | if (uctxt->subctxt_cnt && fd->handler) { |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 237 | u16 remainder; |
| 238 | |
| 239 | fd->tid_limit = uctxt->expected_count / uctxt->subctxt_cnt; |
| 240 | remainder = uctxt->expected_count % uctxt->subctxt_cnt; |
| 241 | if (remainder && fd->subctxt < remainder) |
| 242 | fd->tid_limit++; |
| 243 | } else { |
| 244 | fd->tid_limit = uctxt->expected_count; |
| 245 | } |
Mitko Haralanov | 455d7f1 | 2016-02-05 11:57:56 -0500 | [diff] [blame] | 246 | spin_unlock(&fd->tid_lock); |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 247 | done: |
| 248 | return ret; |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | int hfi1_user_exp_rcv_free(struct hfi1_filedata *fd) |
| 252 | { |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 253 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
| 254 | struct tid_group *grp, *gptr; |
| 255 | |
Mitko Haralanov | 9415844 | 2016-04-20 06:05:36 -0700 | [diff] [blame] | 256 | if (!test_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags)) |
| 257 | return 0; |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 258 | /* |
| 259 | * The notifier would have been removed when the process'es mm |
| 260 | * was freed. |
| 261 | */ |
Dean Luick | 622c202 | 2016-07-28 15:21:21 -0400 | [diff] [blame] | 262 | if (fd->handler) |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 263 | hfi1_mmu_rb_unregister(fd->handler); |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 264 | |
| 265 | kfree(fd->invalid_tids); |
| 266 | |
| 267 | if (!uctxt->cnt) { |
| 268 | if (!EXP_TID_SET_EMPTY(uctxt->tid_full_list)) |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 269 | unlock_exp_tids(uctxt, &uctxt->tid_full_list, fd); |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 270 | if (!EXP_TID_SET_EMPTY(uctxt->tid_used_list)) |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 271 | unlock_exp_tids(uctxt, &uctxt->tid_used_list, fd); |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 272 | list_for_each_entry_safe(grp, gptr, &uctxt->tid_group_list.list, |
| 273 | list) { |
| 274 | list_del_init(&grp->list); |
| 275 | kfree(grp); |
| 276 | } |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 277 | hfi1_clear_tids(uctxt); |
| 278 | } |
Mitko Haralanov | a92ba6d | 2016-02-03 14:34:41 -0800 | [diff] [blame] | 279 | |
| 280 | kfree(fd->entry_to_rb); |
Mitko Haralanov | 3abb33a | 2016-02-05 11:57:54 -0500 | [diff] [blame] | 281 | return 0; |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Mitko Haralanov | b8abe34 | 2016-02-05 11:57:51 -0500 | [diff] [blame] | 284 | /* |
| 285 | * Write an "empty" RcvArray entry. |
| 286 | * This function exists so the TID registaration code can use it |
| 287 | * to write to unused/unneeded entries and still take advantage |
| 288 | * of the WC performance improvements. The HFI will ignore this |
| 289 | * write to the RcvArray entry. |
| 290 | */ |
| 291 | static inline void rcv_array_wc_fill(struct hfi1_devdata *dd, u32 index) |
| 292 | { |
| 293 | /* |
| 294 | * Doing the WC fill writes only makes sense if the device is |
| 295 | * present and the RcvArray has been mapped as WC memory. |
| 296 | */ |
| 297 | if ((dd->flags & HFI1_PRESENT) && dd->rcvarray_wc) |
| 298 | writeq(0, dd->rcvarray_wc + (index * 8)); |
| 299 | } |
| 300 | |
Mitko Haralanov | 7e7a436e | 2016-02-05 11:57:57 -0500 | [diff] [blame] | 301 | /* |
| 302 | * RcvArray entry allocation for Expected Receives is done by the |
| 303 | * following algorithm: |
| 304 | * |
| 305 | * The context keeps 3 lists of groups of RcvArray entries: |
| 306 | * 1. List of empty groups - tid_group_list |
| 307 | * This list is created during user context creation and |
| 308 | * contains elements which describe sets (of 8) of empty |
| 309 | * RcvArray entries. |
| 310 | * 2. List of partially used groups - tid_used_list |
| 311 | * This list contains sets of RcvArray entries which are |
| 312 | * not completely used up. Another mapping request could |
| 313 | * use some of all of the remaining entries. |
| 314 | * 3. List of full groups - tid_full_list |
| 315 | * This is the list where sets that are completely used |
| 316 | * up go. |
| 317 | * |
| 318 | * An attempt to optimize the usage of RcvArray entries is |
| 319 | * made by finding all sets of physically contiguous pages in a |
| 320 | * user's buffer. |
| 321 | * These physically contiguous sets are further split into |
| 322 | * sizes supported by the receive engine of the HFI. The |
| 323 | * resulting sets of pages are stored in struct tid_pageset, |
| 324 | * which describes the sets as: |
| 325 | * * .count - number of pages in this set |
| 326 | * * .idx - starting index into struct page ** array |
| 327 | * of this set |
| 328 | * |
| 329 | * From this point on, the algorithm deals with the page sets |
| 330 | * described above. The number of pagesets is divided by the |
| 331 | * RcvArray group size to produce the number of full groups |
| 332 | * needed. |
| 333 | * |
| 334 | * Groups from the 3 lists are manipulated using the following |
| 335 | * rules: |
| 336 | * 1. For each set of 8 pagesets, a complete group from |
| 337 | * tid_group_list is taken, programmed, and moved to |
| 338 | * the tid_full_list list. |
| 339 | * 2. For all remaining pagesets: |
| 340 | * 2.1 If the tid_used_list is empty and the tid_group_list |
| 341 | * is empty, stop processing pageset and return only |
| 342 | * what has been programmed up to this point. |
| 343 | * 2.2 If the tid_used_list is empty and the tid_group_list |
| 344 | * is not empty, move a group from tid_group_list to |
| 345 | * tid_used_list. |
| 346 | * 2.3 For each group is tid_used_group, program as much as |
| 347 | * can fit into the group. If the group becomes fully |
| 348 | * used, move it to tid_full_list. |
| 349 | */ |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 350 | int hfi1_user_exp_rcv_setup(struct file *fp, struct hfi1_tid_info *tinfo) |
| 351 | { |
Mitko Haralanov | 7e7a436e | 2016-02-05 11:57:57 -0500 | [diff] [blame] | 352 | int ret = 0, need_group = 0, pinned; |
| 353 | struct hfi1_filedata *fd = fp->private_data; |
| 354 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
| 355 | struct hfi1_devdata *dd = uctxt->dd; |
| 356 | unsigned npages, ngroups, pageidx = 0, pageset_count, npagesets, |
| 357 | tididx = 0, mapped, mapped_pages = 0; |
| 358 | unsigned long vaddr = tinfo->vaddr; |
| 359 | struct page **pages = NULL; |
| 360 | u32 *tidlist = NULL; |
| 361 | struct tid_pageset *pagesets = NULL; |
| 362 | |
| 363 | /* Get the number of pages the user buffer spans */ |
| 364 | npages = num_user_pages(vaddr, tinfo->length); |
| 365 | if (!npages) |
| 366 | return -EINVAL; |
| 367 | |
| 368 | if (npages > uctxt->expected_count) { |
| 369 | dd_dev_err(dd, "Expected buffer too big\n"); |
| 370 | return -EINVAL; |
| 371 | } |
| 372 | |
| 373 | /* Verify that access is OK for the user buffer */ |
| 374 | if (!access_ok(VERIFY_WRITE, (void __user *)vaddr, |
| 375 | npages * PAGE_SIZE)) { |
| 376 | dd_dev_err(dd, "Fail vaddr %p, %u pages, !access_ok\n", |
| 377 | (void *)vaddr, npages); |
| 378 | return -EFAULT; |
| 379 | } |
| 380 | |
| 381 | pagesets = kcalloc(uctxt->expected_count, sizeof(*pagesets), |
| 382 | GFP_KERNEL); |
| 383 | if (!pagesets) |
| 384 | return -ENOMEM; |
| 385 | |
| 386 | /* Allocate the array of struct page pointers needed for pinning */ |
| 387 | pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL); |
| 388 | if (!pages) { |
| 389 | ret = -ENOMEM; |
| 390 | goto bail; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Pin all the pages of the user buffer. If we can't pin all the |
| 395 | * pages, accept the amount pinned so far and program only that. |
| 396 | * User space knows how to deal with partially programmed buffers. |
| 397 | */ |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame] | 398 | if (!hfi1_can_pin_pages(dd, fd->mm, fd->tid_n_pinned, npages)) { |
Mitko Haralanov | 0ad2d3d | 2016-04-12 10:46:29 -0700 | [diff] [blame] | 399 | ret = -ENOMEM; |
| 400 | goto bail; |
| 401 | } |
| 402 | |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame] | 403 | pinned = hfi1_acquire_user_pages(fd->mm, vaddr, npages, true, pages); |
Mitko Haralanov | 7e7a436e | 2016-02-05 11:57:57 -0500 | [diff] [blame] | 404 | if (pinned <= 0) { |
| 405 | ret = pinned; |
| 406 | goto bail; |
| 407 | } |
Mitko Haralanov | a7922f7 | 2016-03-08 11:15:39 -0800 | [diff] [blame] | 408 | fd->tid_n_pinned += npages; |
Mitko Haralanov | 7e7a436e | 2016-02-05 11:57:57 -0500 | [diff] [blame] | 409 | |
| 410 | /* Find sets of physically contiguous pages */ |
| 411 | npagesets = find_phys_blocks(pages, pinned, pagesets); |
| 412 | |
| 413 | /* |
| 414 | * We don't need to access this under a lock since tid_used is per |
| 415 | * process and the same process cannot be in hfi1_user_exp_rcv_clear() |
| 416 | * and hfi1_user_exp_rcv_setup() at the same time. |
| 417 | */ |
| 418 | spin_lock(&fd->tid_lock); |
| 419 | if (fd->tid_used + npagesets > fd->tid_limit) |
| 420 | pageset_count = fd->tid_limit - fd->tid_used; |
| 421 | else |
| 422 | pageset_count = npagesets; |
| 423 | spin_unlock(&fd->tid_lock); |
| 424 | |
| 425 | if (!pageset_count) |
| 426 | goto bail; |
| 427 | |
| 428 | ngroups = pageset_count / dd->rcv_entries.group_size; |
| 429 | tidlist = kcalloc(pageset_count, sizeof(*tidlist), GFP_KERNEL); |
| 430 | if (!tidlist) { |
| 431 | ret = -ENOMEM; |
| 432 | goto nomem; |
| 433 | } |
| 434 | |
| 435 | tididx = 0; |
| 436 | |
| 437 | /* |
| 438 | * From this point on, we are going to be using shared (between master |
| 439 | * and subcontexts) context resources. We need to take the lock. |
| 440 | */ |
| 441 | mutex_lock(&uctxt->exp_lock); |
| 442 | /* |
| 443 | * The first step is to program the RcvArray entries which are complete |
| 444 | * groups. |
| 445 | */ |
| 446 | while (ngroups && uctxt->tid_group_list.count) { |
| 447 | struct tid_group *grp = |
| 448 | tid_group_pop(&uctxt->tid_group_list); |
| 449 | |
| 450 | ret = program_rcvarray(fp, vaddr, grp, pagesets, |
| 451 | pageidx, dd->rcv_entries.group_size, |
| 452 | pages, tidlist, &tididx, &mapped); |
| 453 | /* |
| 454 | * If there was a failure to program the RcvArray |
| 455 | * entries for the entire group, reset the grp fields |
| 456 | * and add the grp back to the free group list. |
| 457 | */ |
| 458 | if (ret <= 0) { |
| 459 | tid_group_add_tail(grp, &uctxt->tid_group_list); |
| 460 | hfi1_cdbg(TID, |
| 461 | "Failed to program RcvArray group %d", ret); |
| 462 | goto unlock; |
| 463 | } |
| 464 | |
| 465 | tid_group_add_tail(grp, &uctxt->tid_full_list); |
| 466 | ngroups--; |
| 467 | pageidx += ret; |
| 468 | mapped_pages += mapped; |
| 469 | } |
| 470 | |
| 471 | while (pageidx < pageset_count) { |
| 472 | struct tid_group *grp, *ptr; |
| 473 | /* |
| 474 | * If we don't have any partially used tid groups, check |
| 475 | * if we have empty groups. If so, take one from there and |
| 476 | * put in the partially used list. |
| 477 | */ |
| 478 | if (!uctxt->tid_used_list.count || need_group) { |
| 479 | if (!uctxt->tid_group_list.count) |
| 480 | goto unlock; |
| 481 | |
| 482 | grp = tid_group_pop(&uctxt->tid_group_list); |
| 483 | tid_group_add_tail(grp, &uctxt->tid_used_list); |
| 484 | need_group = 0; |
| 485 | } |
| 486 | /* |
| 487 | * There is an optimization opportunity here - instead of |
| 488 | * fitting as many page sets as we can, check for a group |
| 489 | * later on in the list that could fit all of them. |
| 490 | */ |
| 491 | list_for_each_entry_safe(grp, ptr, &uctxt->tid_used_list.list, |
| 492 | list) { |
| 493 | unsigned use = min_t(unsigned, pageset_count - pageidx, |
| 494 | grp->size - grp->used); |
| 495 | |
| 496 | ret = program_rcvarray(fp, vaddr, grp, pagesets, |
| 497 | pageidx, use, pages, tidlist, |
| 498 | &tididx, &mapped); |
| 499 | if (ret < 0) { |
| 500 | hfi1_cdbg(TID, |
| 501 | "Failed to program RcvArray entries %d", |
| 502 | ret); |
| 503 | ret = -EFAULT; |
| 504 | goto unlock; |
| 505 | } else if (ret > 0) { |
| 506 | if (grp->used == grp->size) |
| 507 | tid_group_move(grp, |
| 508 | &uctxt->tid_used_list, |
| 509 | &uctxt->tid_full_list); |
| 510 | pageidx += ret; |
| 511 | mapped_pages += mapped; |
| 512 | need_group = 0; |
| 513 | /* Check if we are done so we break out early */ |
| 514 | if (pageidx >= pageset_count) |
| 515 | break; |
| 516 | } else if (WARN_ON(ret == 0)) { |
| 517 | /* |
| 518 | * If ret is 0, we did not program any entries |
| 519 | * into this group, which can only happen if |
| 520 | * we've screwed up the accounting somewhere. |
| 521 | * Warn and try to continue. |
| 522 | */ |
| 523 | need_group = 1; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | unlock: |
| 528 | mutex_unlock(&uctxt->exp_lock); |
| 529 | nomem: |
| 530 | hfi1_cdbg(TID, "total mapped: tidpairs:%u pages:%u (%d)", tididx, |
| 531 | mapped_pages, ret); |
| 532 | if (tididx) { |
| 533 | spin_lock(&fd->tid_lock); |
| 534 | fd->tid_used += tididx; |
| 535 | spin_unlock(&fd->tid_lock); |
| 536 | tinfo->tidcnt = tididx; |
| 537 | tinfo->length = mapped_pages * PAGE_SIZE; |
| 538 | |
| 539 | if (copy_to_user((void __user *)(unsigned long)tinfo->tidlist, |
| 540 | tidlist, sizeof(tidlist[0]) * tididx)) { |
| 541 | /* |
| 542 | * On failure to copy to the user level, we need to undo |
| 543 | * everything done so far so we don't leak resources. |
| 544 | */ |
| 545 | tinfo->tidlist = (unsigned long)&tidlist; |
| 546 | hfi1_user_exp_rcv_clear(fp, tinfo); |
| 547 | tinfo->tidlist = 0; |
| 548 | ret = -EFAULT; |
| 549 | goto bail; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | * If not everything was mapped (due to insufficient RcvArray entries, |
| 555 | * for example), unpin all unmapped pages so we can pin them nex time. |
| 556 | */ |
Mitko Haralanov | a7922f7 | 2016-03-08 11:15:39 -0800 | [diff] [blame] | 557 | if (mapped_pages != pinned) { |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame] | 558 | hfi1_release_user_pages(fd->mm, &pages[mapped_pages], |
Mitko Haralanov | 7e7a436e | 2016-02-05 11:57:57 -0500 | [diff] [blame] | 559 | pinned - mapped_pages, |
| 560 | false); |
Mitko Haralanov | a7922f7 | 2016-03-08 11:15:39 -0800 | [diff] [blame] | 561 | fd->tid_n_pinned -= pinned - mapped_pages; |
| 562 | } |
Mitko Haralanov | 7e7a436e | 2016-02-05 11:57:57 -0500 | [diff] [blame] | 563 | bail: |
| 564 | kfree(pagesets); |
| 565 | kfree(pages); |
| 566 | kfree(tidlist); |
| 567 | return ret > 0 ? 0 : ret; |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | int hfi1_user_exp_rcv_clear(struct file *fp, struct hfi1_tid_info *tinfo) |
| 571 | { |
Mitko Haralanov | 455d7f1 | 2016-02-05 11:57:56 -0500 | [diff] [blame] | 572 | int ret = 0; |
| 573 | struct hfi1_filedata *fd = fp->private_data; |
| 574 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
| 575 | u32 *tidinfo; |
| 576 | unsigned tididx; |
| 577 | |
| 578 | tidinfo = kcalloc(tinfo->tidcnt, sizeof(*tidinfo), GFP_KERNEL); |
| 579 | if (!tidinfo) |
| 580 | return -ENOMEM; |
| 581 | |
| 582 | if (copy_from_user(tidinfo, (void __user *)(unsigned long) |
| 583 | tinfo->tidlist, sizeof(tidinfo[0]) * |
| 584 | tinfo->tidcnt)) { |
| 585 | ret = -EFAULT; |
| 586 | goto done; |
| 587 | } |
| 588 | |
| 589 | mutex_lock(&uctxt->exp_lock); |
| 590 | for (tididx = 0; tididx < tinfo->tidcnt; tididx++) { |
| 591 | ret = unprogram_rcvarray(fp, tidinfo[tididx], NULL); |
| 592 | if (ret) { |
| 593 | hfi1_cdbg(TID, "Failed to unprogram rcv array %d", |
| 594 | ret); |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | spin_lock(&fd->tid_lock); |
| 599 | fd->tid_used -= tididx; |
| 600 | spin_unlock(&fd->tid_lock); |
| 601 | tinfo->tidcnt = tididx; |
| 602 | mutex_unlock(&uctxt->exp_lock); |
| 603 | done: |
| 604 | kfree(tidinfo); |
| 605 | return ret; |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | int hfi1_user_exp_rcv_invalid(struct file *fp, struct hfi1_tid_info *tinfo) |
| 609 | { |
Mitko Haralanov | 455d7f1 | 2016-02-05 11:57:56 -0500 | [diff] [blame] | 610 | struct hfi1_filedata *fd = fp->private_data; |
| 611 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
| 612 | unsigned long *ev = uctxt->dd->events + |
| 613 | (((uctxt->ctxt - uctxt->dd->first_user_ctxt) * |
| 614 | HFI1_MAX_SHARED_CTXTS) + fd->subctxt); |
| 615 | u32 *array; |
| 616 | int ret = 0; |
| 617 | |
| 618 | if (!fd->invalid_tids) |
| 619 | return -EINVAL; |
| 620 | |
| 621 | /* |
| 622 | * copy_to_user() can sleep, which will leave the invalid_lock |
| 623 | * locked and cause the MMU notifier to be blocked on the lock |
| 624 | * for a long time. |
| 625 | * Copy the data to a local buffer so we can release the lock. |
| 626 | */ |
| 627 | array = kcalloc(uctxt->expected_count, sizeof(*array), GFP_KERNEL); |
| 628 | if (!array) |
| 629 | return -EFAULT; |
| 630 | |
| 631 | spin_lock(&fd->invalid_lock); |
| 632 | if (fd->invalid_tid_idx) { |
| 633 | memcpy(array, fd->invalid_tids, sizeof(*array) * |
| 634 | fd->invalid_tid_idx); |
| 635 | memset(fd->invalid_tids, 0, sizeof(*fd->invalid_tids) * |
| 636 | fd->invalid_tid_idx); |
| 637 | tinfo->tidcnt = fd->invalid_tid_idx; |
| 638 | fd->invalid_tid_idx = 0; |
| 639 | /* |
| 640 | * Reset the user flag while still holding the lock. |
| 641 | * Otherwise, PSM can miss events. |
| 642 | */ |
| 643 | clear_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev); |
| 644 | } else { |
| 645 | tinfo->tidcnt = 0; |
| 646 | } |
| 647 | spin_unlock(&fd->invalid_lock); |
| 648 | |
| 649 | if (tinfo->tidcnt) { |
| 650 | if (copy_to_user((void __user *)tinfo->tidlist, |
| 651 | array, sizeof(*array) * tinfo->tidcnt)) |
| 652 | ret = -EFAULT; |
| 653 | } |
| 654 | kfree(array); |
| 655 | |
| 656 | return ret; |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 657 | } |
| 658 | |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 659 | static u32 find_phys_blocks(struct page **pages, unsigned npages, |
| 660 | struct tid_pageset *list) |
| 661 | { |
| 662 | unsigned pagecount, pageidx, setcount = 0, i; |
| 663 | unsigned long pfn, this_pfn; |
| 664 | |
| 665 | if (!npages) |
| 666 | return 0; |
| 667 | |
| 668 | /* |
| 669 | * Look for sets of physically contiguous pages in the user buffer. |
| 670 | * This will allow us to optimize Expected RcvArray entry usage by |
| 671 | * using the bigger supported sizes. |
| 672 | */ |
| 673 | pfn = page_to_pfn(pages[0]); |
| 674 | for (pageidx = 0, pagecount = 1, i = 1; i <= npages; i++) { |
| 675 | this_pfn = i < npages ? page_to_pfn(pages[i]) : 0; |
| 676 | |
| 677 | /* |
| 678 | * If the pfn's are not sequential, pages are not physically |
| 679 | * contiguous. |
| 680 | */ |
| 681 | if (this_pfn != ++pfn) { |
| 682 | /* |
| 683 | * At this point we have to loop over the set of |
| 684 | * physically contiguous pages and break them down it |
| 685 | * sizes supported by the HW. |
| 686 | * There are two main constraints: |
| 687 | * 1. The max buffer size is MAX_EXPECTED_BUFFER. |
| 688 | * If the total set size is bigger than that |
| 689 | * program only a MAX_EXPECTED_BUFFER chunk. |
| 690 | * 2. The buffer size has to be a power of two. If |
| 691 | * it is not, round down to the closes power of |
| 692 | * 2 and program that size. |
| 693 | */ |
| 694 | while (pagecount) { |
| 695 | int maxpages = pagecount; |
| 696 | u32 bufsize = pagecount * PAGE_SIZE; |
| 697 | |
| 698 | if (bufsize > MAX_EXPECTED_BUFFER) |
| 699 | maxpages = |
| 700 | MAX_EXPECTED_BUFFER >> |
| 701 | PAGE_SHIFT; |
| 702 | else if (!is_power_of_2(bufsize)) |
| 703 | maxpages = |
| 704 | rounddown_pow_of_two(bufsize) >> |
| 705 | PAGE_SHIFT; |
| 706 | |
| 707 | list[setcount].idx = pageidx; |
| 708 | list[setcount].count = maxpages; |
| 709 | pagecount -= maxpages; |
| 710 | pageidx += maxpages; |
| 711 | setcount++; |
| 712 | } |
| 713 | pageidx = i; |
| 714 | pagecount = 1; |
| 715 | pfn = this_pfn; |
| 716 | } else { |
| 717 | pagecount++; |
| 718 | } |
| 719 | } |
| 720 | return setcount; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * program_rcvarray() - program an RcvArray group with receive buffers |
| 725 | * @fp: file pointer |
| 726 | * @vaddr: starting user virtual address |
| 727 | * @grp: RcvArray group |
| 728 | * @sets: array of struct tid_pageset holding information on physically |
| 729 | * contiguous chunks from the user buffer |
| 730 | * @start: starting index into sets array |
| 731 | * @count: number of struct tid_pageset's to program |
| 732 | * @pages: an array of struct page * for the user buffer |
| 733 | * @tidlist: the array of u32 elements when the information about the |
| 734 | * programmed RcvArray entries is to be encoded. |
| 735 | * @tididx: starting offset into tidlist |
| 736 | * @pmapped: (output parameter) number of pages programmed into the RcvArray |
| 737 | * entries. |
| 738 | * |
| 739 | * This function will program up to 'count' number of RcvArray entries from the |
| 740 | * group 'grp'. To make best use of write-combining writes, the function will |
| 741 | * perform writes to the unused RcvArray entries which will be ignored by the |
| 742 | * HW. Each RcvArray entry will be programmed with a physically contiguous |
| 743 | * buffer chunk from the user's virtual buffer. |
| 744 | * |
| 745 | * Return: |
| 746 | * -EINVAL if the requested count is larger than the size of the group, |
| 747 | * -ENOMEM or -EFAULT on error from set_rcvarray_entry(), or |
| 748 | * number of RcvArray entries programmed. |
| 749 | */ |
| 750 | static int program_rcvarray(struct file *fp, unsigned long vaddr, |
| 751 | struct tid_group *grp, |
| 752 | struct tid_pageset *sets, |
| 753 | unsigned start, u16 count, struct page **pages, |
| 754 | u32 *tidlist, unsigned *tididx, unsigned *pmapped) |
| 755 | { |
| 756 | struct hfi1_filedata *fd = fp->private_data; |
| 757 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
| 758 | struct hfi1_devdata *dd = uctxt->dd; |
| 759 | u16 idx; |
| 760 | u32 tidinfo = 0, rcventry, useidx = 0; |
| 761 | int mapped = 0; |
| 762 | |
| 763 | /* Count should never be larger than the group size */ |
| 764 | if (count > grp->size) |
| 765 | return -EINVAL; |
| 766 | |
| 767 | /* Find the first unused entry in the group */ |
| 768 | for (idx = 0; idx < grp->size; idx++) { |
| 769 | if (!(grp->map & (1 << idx))) { |
| 770 | useidx = idx; |
| 771 | break; |
| 772 | } |
| 773 | rcv_array_wc_fill(dd, grp->base + idx); |
| 774 | } |
| 775 | |
| 776 | idx = 0; |
| 777 | while (idx < count) { |
| 778 | u16 npages, pageidx, setidx = start + idx; |
| 779 | int ret = 0; |
| 780 | |
| 781 | /* |
| 782 | * If this entry in the group is used, move to the next one. |
| 783 | * If we go past the end of the group, exit the loop. |
| 784 | */ |
| 785 | if (useidx >= grp->size) { |
| 786 | break; |
| 787 | } else if (grp->map & (1 << useidx)) { |
| 788 | rcv_array_wc_fill(dd, grp->base + useidx); |
| 789 | useidx++; |
| 790 | continue; |
| 791 | } |
| 792 | |
| 793 | rcventry = grp->base + useidx; |
| 794 | npages = sets[setidx].count; |
| 795 | pageidx = sets[setidx].idx; |
| 796 | |
| 797 | ret = set_rcvarray_entry(fp, vaddr + (pageidx * PAGE_SIZE), |
| 798 | rcventry, grp, pages + pageidx, |
| 799 | npages); |
| 800 | if (ret) |
| 801 | return ret; |
| 802 | mapped += npages; |
| 803 | |
| 804 | tidinfo = rcventry2tidinfo(rcventry - uctxt->expected_base) | |
| 805 | EXP_TID_SET(LEN, npages); |
| 806 | tidlist[(*tididx)++] = tidinfo; |
| 807 | grp->used++; |
| 808 | grp->map |= 1 << useidx++; |
| 809 | idx++; |
| 810 | } |
| 811 | |
| 812 | /* Fill the rest of the group with "blank" writes */ |
| 813 | for (; useidx < grp->size; useidx++) |
| 814 | rcv_array_wc_fill(dd, grp->base + useidx); |
| 815 | *pmapped = mapped; |
| 816 | return idx; |
| 817 | } |
| 818 | |
| 819 | static int set_rcvarray_entry(struct file *fp, unsigned long vaddr, |
| 820 | u32 rcventry, struct tid_group *grp, |
| 821 | struct page **pages, unsigned npages) |
| 822 | { |
| 823 | int ret; |
| 824 | struct hfi1_filedata *fd = fp->private_data; |
| 825 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 826 | struct tid_rb_node *node; |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 827 | struct hfi1_devdata *dd = uctxt->dd; |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 828 | dma_addr_t phys; |
| 829 | |
| 830 | /* |
| 831 | * Allocate the node first so we can handle a potential |
| 832 | * failure before we've programmed anything. |
| 833 | */ |
| 834 | node = kzalloc(sizeof(*node) + (sizeof(struct page *) * npages), |
| 835 | GFP_KERNEL); |
| 836 | if (!node) |
| 837 | return -ENOMEM; |
| 838 | |
| 839 | phys = pci_map_single(dd->pcidev, |
| 840 | __va(page_to_phys(pages[0])), |
| 841 | npages * PAGE_SIZE, PCI_DMA_FROMDEVICE); |
| 842 | if (dma_mapping_error(&dd->pcidev->dev, phys)) { |
| 843 | dd_dev_err(dd, "Failed to DMA map Exp Rcv pages 0x%llx\n", |
| 844 | phys); |
| 845 | kfree(node); |
| 846 | return -EFAULT; |
| 847 | } |
| 848 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 849 | node->mmu.addr = vaddr; |
| 850 | node->mmu.len = npages * PAGE_SIZE; |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 851 | node->phys = page_to_phys(pages[0]); |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 852 | node->npages = npages; |
| 853 | node->rcventry = rcventry; |
| 854 | node->dma_addr = phys; |
| 855 | node->grp = grp; |
| 856 | node->freed = false; |
| 857 | memcpy(node->pages, pages, sizeof(struct page *) * npages); |
| 858 | |
Dean Luick | 622c202 | 2016-07-28 15:21:21 -0400 | [diff] [blame] | 859 | if (!fd->handler) |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 860 | ret = tid_rb_insert(fd, &node->mmu); |
Mitko Haralanov | 368f2b5 | 2016-03-08 11:14:42 -0800 | [diff] [blame] | 861 | else |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 862 | ret = hfi1_mmu_rb_insert(fd->handler, &node->mmu); |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 863 | |
| 864 | if (ret) { |
| 865 | hfi1_cdbg(TID, "Failed to insert RB node %u 0x%lx, 0x%lx %d", |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 866 | node->rcventry, node->mmu.addr, node->phys, ret); |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 867 | pci_unmap_single(dd->pcidev, phys, npages * PAGE_SIZE, |
| 868 | PCI_DMA_FROMDEVICE); |
| 869 | kfree(node); |
| 870 | return -EFAULT; |
| 871 | } |
| 872 | hfi1_put_tid(dd, rcventry, PT_EXPECTED, phys, ilog2(npages) + 1); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 873 | trace_hfi1_exp_tid_reg(uctxt->ctxt, fd->subctxt, rcventry, npages, |
| 874 | node->mmu.addr, node->phys, phys); |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | static int unprogram_rcvarray(struct file *fp, u32 tidinfo, |
| 879 | struct tid_group **grp) |
| 880 | { |
| 881 | struct hfi1_filedata *fd = fp->private_data; |
| 882 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
| 883 | struct hfi1_devdata *dd = uctxt->dd; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 884 | struct tid_rb_node *node; |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 885 | u8 tidctrl = EXP_TID_GET(tidinfo, CTRL); |
Mitko Haralanov | a92ba6d | 2016-02-03 14:34:41 -0800 | [diff] [blame] | 886 | u32 tididx = EXP_TID_GET(tidinfo, IDX) << 1, rcventry; |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 887 | |
| 888 | if (tididx >= uctxt->expected_count) { |
| 889 | dd_dev_err(dd, "Invalid RcvArray entry (%u) index for ctxt %u\n", |
| 890 | tididx, uctxt->ctxt); |
| 891 | return -EINVAL; |
| 892 | } |
| 893 | |
| 894 | if (tidctrl == 0x3) |
| 895 | return -EINVAL; |
| 896 | |
Mitko Haralanov | a92ba6d | 2016-02-03 14:34:41 -0800 | [diff] [blame] | 897 | rcventry = tididx + (tidctrl - 1); |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 898 | |
Mitko Haralanov | a92ba6d | 2016-02-03 14:34:41 -0800 | [diff] [blame] | 899 | node = fd->entry_to_rb[rcventry]; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 900 | if (!node || node->rcventry != (uctxt->expected_base + rcventry)) |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 901 | return -EBADF; |
Dean Luick | 622c202 | 2016-07-28 15:21:21 -0400 | [diff] [blame] | 902 | if (!fd->handler) |
Dean Luick | 082b353 | 2016-07-28 15:21:25 -0400 | [diff] [blame^] | 903 | tid_rb_remove(fd, &node->mmu); |
Mitko Haralanov | 368f2b5 | 2016-03-08 11:14:42 -0800 | [diff] [blame] | 904 | else |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 905 | hfi1_mmu_rb_remove(fd->handler, &node->mmu); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 906 | |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 907 | if (grp) |
| 908 | *grp = node->grp; |
Ira Weiny | 5ed3b15 | 2016-07-28 12:27:32 -0400 | [diff] [blame] | 909 | clear_tid_node(fd, node); |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 910 | return 0; |
| 911 | } |
| 912 | |
Ira Weiny | 5ed3b15 | 2016-07-28 12:27:32 -0400 | [diff] [blame] | 913 | static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node) |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 914 | { |
| 915 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
| 916 | struct hfi1_devdata *dd = uctxt->dd; |
| 917 | |
Mitko Haralanov | 0b091fb | 2016-02-05 11:57:58 -0500 | [diff] [blame] | 918 | trace_hfi1_exp_tid_unreg(uctxt->ctxt, fd->subctxt, node->rcventry, |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 919 | node->npages, node->mmu.addr, node->phys, |
Mitko Haralanov | 0b091fb | 2016-02-05 11:57:58 -0500 | [diff] [blame] | 920 | node->dma_addr); |
| 921 | |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 922 | hfi1_put_tid(dd, node->rcventry, PT_INVALID, 0, 0); |
| 923 | /* |
| 924 | * Make sure device has seen the write before we unpin the |
| 925 | * pages. |
| 926 | */ |
| 927 | flush_wc(); |
| 928 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 929 | pci_unmap_single(dd->pcidev, node->dma_addr, node->mmu.len, |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 930 | PCI_DMA_FROMDEVICE); |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame] | 931 | hfi1_release_user_pages(fd->mm, node->pages, node->npages, true); |
Mitko Haralanov | a7922f7 | 2016-03-08 11:15:39 -0800 | [diff] [blame] | 932 | fd->tid_n_pinned -= node->npages; |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 933 | |
| 934 | node->grp->used--; |
| 935 | node->grp->map &= ~(1 << (node->rcventry - node->grp->base)); |
| 936 | |
| 937 | if (node->grp->used == node->grp->size - 1) |
| 938 | tid_group_move(node->grp, &uctxt->tid_full_list, |
| 939 | &uctxt->tid_used_list); |
| 940 | else if (!node->grp->used) |
| 941 | tid_group_move(node->grp, &uctxt->tid_used_list, |
| 942 | &uctxt->tid_group_list); |
| 943 | kfree(node); |
| 944 | } |
| 945 | |
| 946 | static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt, |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 947 | struct exp_tid_set *set, |
| 948 | struct hfi1_filedata *fd) |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 949 | { |
| 950 | struct tid_group *grp, *ptr; |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 951 | int i; |
| 952 | |
| 953 | list_for_each_entry_safe(grp, ptr, &set->list, list) { |
| 954 | list_del_init(&grp->list); |
| 955 | |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 956 | for (i = 0; i < grp->size; i++) { |
| 957 | if (grp->map & (1 << i)) { |
| 958 | u16 rcventry = grp->base + i; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 959 | struct tid_rb_node *node; |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 960 | |
Mitko Haralanov | a92ba6d | 2016-02-03 14:34:41 -0800 | [diff] [blame] | 961 | node = fd->entry_to_rb[rcventry - |
| 962 | uctxt->expected_base]; |
| 963 | if (!node || node->rcventry != rcventry) |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 964 | continue; |
Dean Luick | 622c202 | 2016-07-28 15:21:21 -0400 | [diff] [blame] | 965 | if (!fd->handler) |
Dean Luick | 082b353 | 2016-07-28 15:21:25 -0400 | [diff] [blame^] | 966 | tid_rb_remove(fd, &node->mmu); |
Mitko Haralanov | 368f2b5 | 2016-03-08 11:14:42 -0800 | [diff] [blame] | 967 | else |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 968 | hfi1_mmu_rb_remove(fd->handler, |
Mitko Haralanov | 368f2b5 | 2016-03-08 11:14:42 -0800 | [diff] [blame] | 969 | &node->mmu); |
Ira Weiny | 5ed3b15 | 2016-07-28 12:27:32 -0400 | [diff] [blame] | 970 | clear_tid_node(fd, node); |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 971 | } |
| 972 | } |
Mitko Haralanov | f88e0c8 | 2016-02-05 11:57:52 -0500 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 976 | static int tid_rb_invalidate(void *arg, struct mmu_rb_node *mnode) |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 977 | { |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 978 | struct hfi1_filedata *fdata = arg; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 979 | struct hfi1_ctxtdata *uctxt = fdata->uctxt; |
| 980 | struct tid_rb_node *node = |
| 981 | container_of(mnode, struct tid_rb_node, mmu); |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 982 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 983 | if (node->freed) |
| 984 | return 0; |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 985 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 986 | trace_hfi1_exp_tid_inval(uctxt->ctxt, fdata->subctxt, node->mmu.addr, |
| 987 | node->rcventry, node->npages, node->dma_addr); |
| 988 | node->freed = true; |
Mitko Haralanov | b5eb3b2 | 2016-02-05 11:57:55 -0500 | [diff] [blame] | 989 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 990 | spin_lock(&fdata->invalid_lock); |
| 991 | if (fdata->invalid_tid_idx < uctxt->expected_count) { |
| 992 | fdata->invalid_tids[fdata->invalid_tid_idx] = |
| 993 | rcventry2tidinfo(node->rcventry - uctxt->expected_base); |
| 994 | fdata->invalid_tids[fdata->invalid_tid_idx] |= |
| 995 | EXP_TID_SET(LEN, node->npages); |
| 996 | if (!fdata->invalid_tid_idx) { |
| 997 | unsigned long *ev; |
Mitko Haralanov | 0b091fb | 2016-02-05 11:57:58 -0500 | [diff] [blame] | 998 | |
Mitko Haralanov | b5eb3b2 | 2016-02-05 11:57:55 -0500 | [diff] [blame] | 999 | /* |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 1000 | * hfi1_set_uevent_bits() sets a user event flag |
| 1001 | * for all processes. Because calling into the |
| 1002 | * driver to process TID cache invalidations is |
| 1003 | * expensive and TID cache invalidations are |
| 1004 | * handled on a per-process basis, we can |
| 1005 | * optimize this to set the flag only for the |
| 1006 | * process in question. |
Mitko Haralanov | b5eb3b2 | 2016-02-05 11:57:55 -0500 | [diff] [blame] | 1007 | */ |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 1008 | ev = uctxt->dd->events + |
| 1009 | (((uctxt->ctxt - uctxt->dd->first_user_ctxt) * |
| 1010 | HFI1_MAX_SHARED_CTXTS) + fdata->subctxt); |
| 1011 | set_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev); |
Mitko Haralanov | b5eb3b2 | 2016-02-05 11:57:55 -0500 | [diff] [blame] | 1012 | } |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 1013 | fdata->invalid_tid_idx++; |
Mitko Haralanov | b5eb3b2 | 2016-02-05 11:57:55 -0500 | [diff] [blame] | 1014 | } |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 1015 | spin_unlock(&fdata->invalid_lock); |
| 1016 | return 0; |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 1017 | } |
| 1018 | |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 1019 | static int tid_rb_insert(void *arg, struct mmu_rb_node *node) |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 1020 | { |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 1021 | struct hfi1_filedata *fdata = arg; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 1022 | struct tid_rb_node *tnode = |
| 1023 | container_of(node, struct tid_rb_node, mmu); |
Mitko Haralanov | a92ba6d | 2016-02-03 14:34:41 -0800 | [diff] [blame] | 1024 | u32 base = fdata->uctxt->expected_base; |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 1025 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 1026 | fdata->entry_to_rb[tnode->rcventry - base] = tnode; |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 1027 | return 0; |
| 1028 | } |
| 1029 | |
Dean Luick | 082b353 | 2016-07-28 15:21:25 -0400 | [diff] [blame^] | 1030 | static void tid_rb_remove(void *arg, struct mmu_rb_node *node) |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 1031 | { |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 1032 | struct hfi1_filedata *fdata = arg; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 1033 | struct tid_rb_node *tnode = |
| 1034 | container_of(node, struct tid_rb_node, mmu); |
Mitko Haralanov | a92ba6d | 2016-02-03 14:34:41 -0800 | [diff] [blame] | 1035 | u32 base = fdata->uctxt->expected_base; |
Mitko Haralanov | f727a0c | 2016-02-05 11:57:46 -0500 | [diff] [blame] | 1036 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 1037 | fdata->entry_to_rb[tnode->rcventry - base] = NULL; |
Mitko Haralanov | a92ba6d | 2016-02-03 14:34:41 -0800 | [diff] [blame] | 1038 | } |