blob: 6090933d51716506aefda0cde991c5defc9f3b62 [file] [log] [blame]
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001/*
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07002 * Copyright(c) 2015-2017 Intel Corporation.
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05003 *
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 Haralanovf727a0c2016-02-05 11:57:46 -05009 * 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 Haralanovf727a0c2016-02-05 11:57:46 -050020 * 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>
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -080048#include <linux/string.h>
Mitko Haralanovf727a0c2016-02-05 11:57:46 -050049
50#include "user_exp_rcv.h"
51#include "trace.h"
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080052#include "mmu_rb.h"
Mitko Haralanovf727a0c2016-02-05 11:57:46 -050053
Mitko Haralanovb8abe342016-02-05 11:57:51 -050054struct tid_group {
55 struct list_head list;
56 unsigned base;
57 u8 size;
58 u8 used;
59 u8 map;
60};
61
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080062struct tid_rb_node {
63 struct mmu_rb_node mmu;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -050064 unsigned long phys;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -050065 struct tid_group *grp;
66 u32 rcventry;
67 dma_addr_t dma_addr;
68 bool freed;
69 unsigned npages;
70 struct page *pages[0];
71};
72
Mitko Haralanovf88e0c82016-02-05 11:57:52 -050073struct tid_pageset {
74 u16 idx;
75 u16 count;
76};
77
Mitko Haralanovb8abe342016-02-05 11:57:51 -050078#define EXP_TID_SET_EMPTY(set) (set.count == 0 && list_empty(&set.list))
79
Mitko Haralanov3abb33a2016-02-05 11:57:54 -050080#define num_user_pages(vaddr, len) \
81 (1 + (((((unsigned long)(vaddr) + \
82 (unsigned long)(len) - 1) & PAGE_MASK) - \
83 ((unsigned long)vaddr & PAGE_MASK)) >> PAGE_SHIFT))
84
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070085static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt,
86 struct exp_tid_set *set,
87 struct hfi1_filedata *fd);
88static u32 find_phys_blocks(struct page **pages, unsigned npages,
89 struct tid_pageset *list);
90static int set_rcvarray_entry(struct file *fp, unsigned long vaddr,
91 u32 rcventry, struct tid_group *grp,
92 struct page **pages, unsigned npages);
93static int tid_rb_insert(void *arg, struct mmu_rb_node *node);
Ira Weiny2677a762016-07-28 15:21:26 -040094static void cacheless_tid_rb_remove(struct hfi1_filedata *fdata,
95 struct tid_rb_node *tnode);
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070096static void tid_rb_remove(void *arg, struct mmu_rb_node *node);
97static int tid_rb_invalidate(void *arg, struct mmu_rb_node *mnode);
98static int program_rcvarray(struct file *fp, unsigned long vaddr,
99 struct tid_group *grp,
100 struct tid_pageset *sets,
101 unsigned start, u16 count, struct page **pages,
102 u32 *tidlist, unsigned *tididx, unsigned *pmapped);
103static int unprogram_rcvarray(struct file *fp, u32 tidinfo,
104 struct tid_group **grp);
Ira Weiny2677a762016-07-28 15:21:26 -0400105static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800106
107static struct mmu_rb_ops tid_rb_ops = {
Dean Luicka7cd2dc2016-07-28 12:27:37 -0400108 .insert = tid_rb_insert,
109 .remove = tid_rb_remove,
110 .invalidate = tid_rb_invalidate
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800111};
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500112
113static inline u32 rcventry2tidinfo(u32 rcventry)
114{
115 u32 pair = rcventry & ~0x1;
116
117 return EXP_TID_SET(IDX, pair >> 1) |
118 EXP_TID_SET(CTRL, 1 << (rcventry - pair));
119}
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500120
Mitko Haralanovb8abe342016-02-05 11:57:51 -0500121static inline void exp_tid_group_init(struct exp_tid_set *set)
122{
123 INIT_LIST_HEAD(&set->list);
124 set->count = 0;
125}
126
127static inline void tid_group_remove(struct tid_group *grp,
128 struct exp_tid_set *set)
129{
130 list_del_init(&grp->list);
131 set->count--;
132}
133
134static inline void tid_group_add_tail(struct tid_group *grp,
135 struct exp_tid_set *set)
136{
137 list_add_tail(&grp->list, &set->list);
138 set->count++;
139}
140
141static inline struct tid_group *tid_group_pop(struct exp_tid_set *set)
142{
143 struct tid_group *grp =
144 list_first_entry(&set->list, struct tid_group, list);
145 list_del_init(&grp->list);
146 set->count--;
147 return grp;
148}
149
150static inline void tid_group_move(struct tid_group *group,
151 struct exp_tid_set *s1,
152 struct exp_tid_set *s2)
153{
154 tid_group_remove(group, s1);
155 tid_group_add_tail(group, s2);
156}
157
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500158/*
159 * Initialize context and file private data needed for Expected
160 * receive caching. This needs to be done after the context has
161 * been configured with the eager/expected RcvEntry counts.
162 */
163int hfi1_user_exp_rcv_init(struct file *fp)
164{
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500165 struct hfi1_filedata *fd = fp->private_data;
166 struct hfi1_ctxtdata *uctxt = fd->uctxt;
167 struct hfi1_devdata *dd = uctxt->dd;
168 unsigned tidbase;
169 int i, ret = 0;
170
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500171 spin_lock_init(&fd->tid_lock);
172 spin_lock_init(&fd->invalid_lock);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500173
174 if (!uctxt->subctxt_cnt || !fd->subctxt) {
175 exp_tid_group_init(&uctxt->tid_group_list);
176 exp_tid_group_init(&uctxt->tid_used_list);
177 exp_tid_group_init(&uctxt->tid_full_list);
178
179 tidbase = uctxt->expected_base;
180 for (i = 0; i < uctxt->expected_count /
181 dd->rcv_entries.group_size; i++) {
182 struct tid_group *grp;
183
184 grp = kzalloc(sizeof(*grp), GFP_KERNEL);
185 if (!grp) {
186 /*
187 * If we fail here, the groups already
188 * allocated will be freed by the close
189 * call.
190 */
191 ret = -ENOMEM;
192 goto done;
193 }
194 grp->size = dd->rcv_entries.group_size;
195 grp->base = tidbase;
196 tid_group_add_tail(grp, &uctxt->tid_group_list);
197 tidbase += dd->rcv_entries.group_size;
198 }
199 }
200
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800201 fd->entry_to_rb = kcalloc(uctxt->expected_count,
202 sizeof(struct rb_node *),
203 GFP_KERNEL);
204 if (!fd->entry_to_rb)
205 return -ENOMEM;
206
Dean Luick622c2022016-07-28 15:21:21 -0400207 if (!HFI1_CAP_UGET_MASK(uctxt->flags, TID_UNMAP)) {
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500208 fd->invalid_tid_idx = 0;
Markus Elfring4076e512017-02-09 15:30:53 +0100209 fd->invalid_tids = kcalloc(uctxt->expected_count,
210 sizeof(*fd->invalid_tids),
211 GFP_KERNEL);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500212 if (!fd->invalid_tids) {
213 ret = -ENOMEM;
214 goto done;
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800215 }
216
217 /*
218 * Register MMU notifier callbacks. If the registration
Dean Luick622c2022016-07-28 15:21:21 -0400219 * fails, continue without TID caching for this context.
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800220 */
Dean Luickb85ced92016-07-28 15:21:24 -0400221 ret = hfi1_mmu_rb_register(fd, fd->mm, &tid_rb_ops,
222 dd->pport->hfi1_wq,
223 &fd->handler);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800224 if (ret) {
225 dd_dev_info(dd,
226 "Failed MMU notifier registration %d\n",
227 ret);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800228 ret = 0;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500229 }
230 }
231
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500232 /*
233 * PSM does not have a good way to separate, count, and
234 * effectively enforce a limit on RcvArray entries used by
235 * subctxts (when context sharing is used) when TID caching
236 * is enabled. To help with that, we calculate a per-process
237 * RcvArray entry share and enforce that.
238 * If TID caching is not in use, PSM deals with usage on its
239 * own. In that case, we allow any subctxt to take all of the
240 * entries.
241 *
242 * Make sure that we set the tid counts only after successful
243 * init.
244 */
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500245 spin_lock(&fd->tid_lock);
Dean Luick622c2022016-07-28 15:21:21 -0400246 if (uctxt->subctxt_cnt && fd->handler) {
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500247 u16 remainder;
248
249 fd->tid_limit = uctxt->expected_count / uctxt->subctxt_cnt;
250 remainder = uctxt->expected_count % uctxt->subctxt_cnt;
251 if (remainder && fd->subctxt < remainder)
252 fd->tid_limit++;
253 } else {
254 fd->tid_limit = uctxt->expected_count;
255 }
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500256 spin_unlock(&fd->tid_lock);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500257done:
258 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500259}
260
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700261void hfi1_user_exp_rcv_grp_free(struct hfi1_ctxtdata *uctxt)
262{
263 struct tid_group *grp, *gptr;
264
265 list_for_each_entry_safe(grp, gptr, &uctxt->tid_group_list.list,
266 list) {
267 list_del_init(&grp->list);
268 kfree(grp);
269 }
270 hfi1_clear_tids(uctxt);
271}
272
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500273int hfi1_user_exp_rcv_free(struct hfi1_filedata *fd)
274{
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500275 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500276
277 /*
278 * The notifier would have been removed when the process'es mm
279 * was freed.
280 */
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700281 if (fd->handler) {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400282 hfi1_mmu_rb_unregister(fd->handler);
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700283 } else {
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500284 if (!EXP_TID_SET_EMPTY(uctxt->tid_full_list))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400285 unlock_exp_tids(uctxt, &uctxt->tid_full_list, fd);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500286 if (!EXP_TID_SET_EMPTY(uctxt->tid_used_list))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400287 unlock_exp_tids(uctxt, &uctxt->tid_used_list, fd);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500288 }
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800289
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700290 kfree(fd->invalid_tids);
291 fd->invalid_tids = NULL;
292
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800293 kfree(fd->entry_to_rb);
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700294 fd->entry_to_rb = NULL;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500295 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500296}
297
Mitko Haralanovb8abe342016-02-05 11:57:51 -0500298/*
299 * Write an "empty" RcvArray entry.
300 * This function exists so the TID registaration code can use it
301 * to write to unused/unneeded entries and still take advantage
302 * of the WC performance improvements. The HFI will ignore this
303 * write to the RcvArray entry.
304 */
305static inline void rcv_array_wc_fill(struct hfi1_devdata *dd, u32 index)
306{
307 /*
308 * Doing the WC fill writes only makes sense if the device is
309 * present and the RcvArray has been mapped as WC memory.
310 */
311 if ((dd->flags & HFI1_PRESENT) && dd->rcvarray_wc)
312 writeq(0, dd->rcvarray_wc + (index * 8));
313}
314
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500315/*
316 * RcvArray entry allocation for Expected Receives is done by the
317 * following algorithm:
318 *
319 * The context keeps 3 lists of groups of RcvArray entries:
320 * 1. List of empty groups - tid_group_list
321 * This list is created during user context creation and
322 * contains elements which describe sets (of 8) of empty
323 * RcvArray entries.
324 * 2. List of partially used groups - tid_used_list
325 * This list contains sets of RcvArray entries which are
326 * not completely used up. Another mapping request could
327 * use some of all of the remaining entries.
328 * 3. List of full groups - tid_full_list
329 * This is the list where sets that are completely used
330 * up go.
331 *
332 * An attempt to optimize the usage of RcvArray entries is
333 * made by finding all sets of physically contiguous pages in a
334 * user's buffer.
335 * These physically contiguous sets are further split into
336 * sizes supported by the receive engine of the HFI. The
337 * resulting sets of pages are stored in struct tid_pageset,
338 * which describes the sets as:
339 * * .count - number of pages in this set
340 * * .idx - starting index into struct page ** array
341 * of this set
342 *
343 * From this point on, the algorithm deals with the page sets
344 * described above. The number of pagesets is divided by the
345 * RcvArray group size to produce the number of full groups
346 * needed.
347 *
348 * Groups from the 3 lists are manipulated using the following
349 * rules:
350 * 1. For each set of 8 pagesets, a complete group from
351 * tid_group_list is taken, programmed, and moved to
352 * the tid_full_list list.
353 * 2. For all remaining pagesets:
354 * 2.1 If the tid_used_list is empty and the tid_group_list
355 * is empty, stop processing pageset and return only
356 * what has been programmed up to this point.
357 * 2.2 If the tid_used_list is empty and the tid_group_list
358 * is not empty, move a group from tid_group_list to
359 * tid_used_list.
360 * 2.3 For each group is tid_used_group, program as much as
361 * can fit into the group. If the group becomes fully
362 * used, move it to tid_full_list.
363 */
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500364int hfi1_user_exp_rcv_setup(struct file *fp, struct hfi1_tid_info *tinfo)
365{
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500366 int ret = 0, need_group = 0, pinned;
367 struct hfi1_filedata *fd = fp->private_data;
368 struct hfi1_ctxtdata *uctxt = fd->uctxt;
369 struct hfi1_devdata *dd = uctxt->dd;
370 unsigned npages, ngroups, pageidx = 0, pageset_count, npagesets,
371 tididx = 0, mapped, mapped_pages = 0;
372 unsigned long vaddr = tinfo->vaddr;
373 struct page **pages = NULL;
374 u32 *tidlist = NULL;
375 struct tid_pageset *pagesets = NULL;
376
377 /* Get the number of pages the user buffer spans */
378 npages = num_user_pages(vaddr, tinfo->length);
379 if (!npages)
380 return -EINVAL;
381
382 if (npages > uctxt->expected_count) {
383 dd_dev_err(dd, "Expected buffer too big\n");
384 return -EINVAL;
385 }
386
387 /* Verify that access is OK for the user buffer */
388 if (!access_ok(VERIFY_WRITE, (void __user *)vaddr,
389 npages * PAGE_SIZE)) {
390 dd_dev_err(dd, "Fail vaddr %p, %u pages, !access_ok\n",
391 (void *)vaddr, npages);
392 return -EFAULT;
393 }
394
395 pagesets = kcalloc(uctxt->expected_count, sizeof(*pagesets),
396 GFP_KERNEL);
397 if (!pagesets)
398 return -ENOMEM;
399
400 /* Allocate the array of struct page pointers needed for pinning */
401 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
402 if (!pages) {
403 ret = -ENOMEM;
404 goto bail;
405 }
406
407 /*
408 * Pin all the pages of the user buffer. If we can't pin all the
409 * pages, accept the amount pinned so far and program only that.
410 * User space knows how to deal with partially programmed buffers.
411 */
Ira Weiny3faa3d92016-07-28 15:21:19 -0400412 if (!hfi1_can_pin_pages(dd, fd->mm, fd->tid_n_pinned, npages)) {
Mitko Haralanov0ad2d3d2016-04-12 10:46:29 -0700413 ret = -ENOMEM;
414 goto bail;
415 }
416
Ira Weiny3faa3d92016-07-28 15:21:19 -0400417 pinned = hfi1_acquire_user_pages(fd->mm, vaddr, npages, true, pages);
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500418 if (pinned <= 0) {
419 ret = pinned;
420 goto bail;
421 }
Mitko Haralanova7922f72016-03-08 11:15:39 -0800422 fd->tid_n_pinned += npages;
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500423
424 /* Find sets of physically contiguous pages */
425 npagesets = find_phys_blocks(pages, pinned, pagesets);
426
427 /*
428 * We don't need to access this under a lock since tid_used is per
429 * process and the same process cannot be in hfi1_user_exp_rcv_clear()
430 * and hfi1_user_exp_rcv_setup() at the same time.
431 */
432 spin_lock(&fd->tid_lock);
433 if (fd->tid_used + npagesets > fd->tid_limit)
434 pageset_count = fd->tid_limit - fd->tid_used;
435 else
436 pageset_count = npagesets;
437 spin_unlock(&fd->tid_lock);
438
439 if (!pageset_count)
440 goto bail;
441
442 ngroups = pageset_count / dd->rcv_entries.group_size;
443 tidlist = kcalloc(pageset_count, sizeof(*tidlist), GFP_KERNEL);
444 if (!tidlist) {
445 ret = -ENOMEM;
446 goto nomem;
447 }
448
449 tididx = 0;
450
451 /*
452 * From this point on, we are going to be using shared (between master
453 * and subcontexts) context resources. We need to take the lock.
454 */
455 mutex_lock(&uctxt->exp_lock);
456 /*
457 * The first step is to program the RcvArray entries which are complete
458 * groups.
459 */
460 while (ngroups && uctxt->tid_group_list.count) {
461 struct tid_group *grp =
462 tid_group_pop(&uctxt->tid_group_list);
463
464 ret = program_rcvarray(fp, vaddr, grp, pagesets,
465 pageidx, dd->rcv_entries.group_size,
466 pages, tidlist, &tididx, &mapped);
467 /*
468 * If there was a failure to program the RcvArray
469 * entries for the entire group, reset the grp fields
470 * and add the grp back to the free group list.
471 */
472 if (ret <= 0) {
473 tid_group_add_tail(grp, &uctxt->tid_group_list);
474 hfi1_cdbg(TID,
475 "Failed to program RcvArray group %d", ret);
476 goto unlock;
477 }
478
479 tid_group_add_tail(grp, &uctxt->tid_full_list);
480 ngroups--;
481 pageidx += ret;
482 mapped_pages += mapped;
483 }
484
485 while (pageidx < pageset_count) {
486 struct tid_group *grp, *ptr;
487 /*
488 * If we don't have any partially used tid groups, check
489 * if we have empty groups. If so, take one from there and
490 * put in the partially used list.
491 */
492 if (!uctxt->tid_used_list.count || need_group) {
493 if (!uctxt->tid_group_list.count)
494 goto unlock;
495
496 grp = tid_group_pop(&uctxt->tid_group_list);
497 tid_group_add_tail(grp, &uctxt->tid_used_list);
498 need_group = 0;
499 }
500 /*
501 * There is an optimization opportunity here - instead of
502 * fitting as many page sets as we can, check for a group
503 * later on in the list that could fit all of them.
504 */
505 list_for_each_entry_safe(grp, ptr, &uctxt->tid_used_list.list,
506 list) {
507 unsigned use = min_t(unsigned, pageset_count - pageidx,
508 grp->size - grp->used);
509
510 ret = program_rcvarray(fp, vaddr, grp, pagesets,
511 pageidx, use, pages, tidlist,
512 &tididx, &mapped);
513 if (ret < 0) {
514 hfi1_cdbg(TID,
515 "Failed to program RcvArray entries %d",
516 ret);
517 ret = -EFAULT;
518 goto unlock;
519 } else if (ret > 0) {
520 if (grp->used == grp->size)
521 tid_group_move(grp,
522 &uctxt->tid_used_list,
523 &uctxt->tid_full_list);
524 pageidx += ret;
525 mapped_pages += mapped;
526 need_group = 0;
527 /* Check if we are done so we break out early */
528 if (pageidx >= pageset_count)
529 break;
530 } else if (WARN_ON(ret == 0)) {
531 /*
532 * If ret is 0, we did not program any entries
533 * into this group, which can only happen if
534 * we've screwed up the accounting somewhere.
535 * Warn and try to continue.
536 */
537 need_group = 1;
538 }
539 }
540 }
541unlock:
542 mutex_unlock(&uctxt->exp_lock);
543nomem:
544 hfi1_cdbg(TID, "total mapped: tidpairs:%u pages:%u (%d)", tididx,
545 mapped_pages, ret);
546 if (tididx) {
547 spin_lock(&fd->tid_lock);
548 fd->tid_used += tididx;
549 spin_unlock(&fd->tid_lock);
550 tinfo->tidcnt = tididx;
551 tinfo->length = mapped_pages * PAGE_SIZE;
552
553 if (copy_to_user((void __user *)(unsigned long)tinfo->tidlist,
554 tidlist, sizeof(tidlist[0]) * tididx)) {
555 /*
556 * On failure to copy to the user level, we need to undo
557 * everything done so far so we don't leak resources.
558 */
559 tinfo->tidlist = (unsigned long)&tidlist;
560 hfi1_user_exp_rcv_clear(fp, tinfo);
561 tinfo->tidlist = 0;
562 ret = -EFAULT;
563 goto bail;
564 }
565 }
566
567 /*
568 * If not everything was mapped (due to insufficient RcvArray entries,
569 * for example), unpin all unmapped pages so we can pin them nex time.
570 */
Mitko Haralanova7922f72016-03-08 11:15:39 -0800571 if (mapped_pages != pinned) {
Ira Weiny3faa3d92016-07-28 15:21:19 -0400572 hfi1_release_user_pages(fd->mm, &pages[mapped_pages],
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500573 pinned - mapped_pages,
574 false);
Mitko Haralanova7922f72016-03-08 11:15:39 -0800575 fd->tid_n_pinned -= pinned - mapped_pages;
576 }
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500577bail:
578 kfree(pagesets);
579 kfree(pages);
580 kfree(tidlist);
581 return ret > 0 ? 0 : ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500582}
583
584int hfi1_user_exp_rcv_clear(struct file *fp, struct hfi1_tid_info *tinfo)
585{
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500586 int ret = 0;
587 struct hfi1_filedata *fd = fp->private_data;
588 struct hfi1_ctxtdata *uctxt = fd->uctxt;
589 u32 *tidinfo;
590 unsigned tididx;
591
Michael J. Ruhldb730892017-04-09 10:16:03 -0700592 if (unlikely(tinfo->tidcnt > fd->tid_used))
593 return -EINVAL;
594
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800595 tidinfo = memdup_user((void __user *)(unsigned long)tinfo->tidlist,
596 sizeof(tidinfo[0]) * tinfo->tidcnt);
597 if (IS_ERR(tidinfo))
598 return PTR_ERR(tidinfo);
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500599
600 mutex_lock(&uctxt->exp_lock);
601 for (tididx = 0; tididx < tinfo->tidcnt; tididx++) {
602 ret = unprogram_rcvarray(fp, tidinfo[tididx], NULL);
603 if (ret) {
604 hfi1_cdbg(TID, "Failed to unprogram rcv array %d",
605 ret);
606 break;
607 }
608 }
609 spin_lock(&fd->tid_lock);
610 fd->tid_used -= tididx;
611 spin_unlock(&fd->tid_lock);
612 tinfo->tidcnt = tididx;
613 mutex_unlock(&uctxt->exp_lock);
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800614
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500615 kfree(tidinfo);
616 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500617}
618
619int hfi1_user_exp_rcv_invalid(struct file *fp, struct hfi1_tid_info *tinfo)
620{
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500621 struct hfi1_filedata *fd = fp->private_data;
622 struct hfi1_ctxtdata *uctxt = fd->uctxt;
623 unsigned long *ev = uctxt->dd->events +
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700624 (((uctxt->ctxt - uctxt->dd->first_dyn_alloc_ctxt) *
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500625 HFI1_MAX_SHARED_CTXTS) + fd->subctxt);
626 u32 *array;
627 int ret = 0;
628
629 if (!fd->invalid_tids)
630 return -EINVAL;
631
632 /*
633 * copy_to_user() can sleep, which will leave the invalid_lock
634 * locked and cause the MMU notifier to be blocked on the lock
635 * for a long time.
636 * Copy the data to a local buffer so we can release the lock.
637 */
638 array = kcalloc(uctxt->expected_count, sizeof(*array), GFP_KERNEL);
639 if (!array)
640 return -EFAULT;
641
642 spin_lock(&fd->invalid_lock);
643 if (fd->invalid_tid_idx) {
644 memcpy(array, fd->invalid_tids, sizeof(*array) *
645 fd->invalid_tid_idx);
646 memset(fd->invalid_tids, 0, sizeof(*fd->invalid_tids) *
647 fd->invalid_tid_idx);
648 tinfo->tidcnt = fd->invalid_tid_idx;
649 fd->invalid_tid_idx = 0;
650 /*
651 * Reset the user flag while still holding the lock.
652 * Otherwise, PSM can miss events.
653 */
654 clear_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
655 } else {
656 tinfo->tidcnt = 0;
657 }
658 spin_unlock(&fd->invalid_lock);
659
660 if (tinfo->tidcnt) {
661 if (copy_to_user((void __user *)tinfo->tidlist,
662 array, sizeof(*array) * tinfo->tidcnt))
663 ret = -EFAULT;
664 }
665 kfree(array);
666
667 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500668}
669
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500670static u32 find_phys_blocks(struct page **pages, unsigned npages,
671 struct tid_pageset *list)
672{
673 unsigned pagecount, pageidx, setcount = 0, i;
674 unsigned long pfn, this_pfn;
675
676 if (!npages)
677 return 0;
678
679 /*
680 * Look for sets of physically contiguous pages in the user buffer.
681 * This will allow us to optimize Expected RcvArray entry usage by
682 * using the bigger supported sizes.
683 */
684 pfn = page_to_pfn(pages[0]);
685 for (pageidx = 0, pagecount = 1, i = 1; i <= npages; i++) {
686 this_pfn = i < npages ? page_to_pfn(pages[i]) : 0;
687
688 /*
689 * If the pfn's are not sequential, pages are not physically
690 * contiguous.
691 */
692 if (this_pfn != ++pfn) {
693 /*
694 * At this point we have to loop over the set of
695 * physically contiguous pages and break them down it
696 * sizes supported by the HW.
697 * There are two main constraints:
698 * 1. The max buffer size is MAX_EXPECTED_BUFFER.
699 * If the total set size is bigger than that
700 * program only a MAX_EXPECTED_BUFFER chunk.
701 * 2. The buffer size has to be a power of two. If
702 * it is not, round down to the closes power of
703 * 2 and program that size.
704 */
705 while (pagecount) {
706 int maxpages = pagecount;
707 u32 bufsize = pagecount * PAGE_SIZE;
708
709 if (bufsize > MAX_EXPECTED_BUFFER)
710 maxpages =
711 MAX_EXPECTED_BUFFER >>
712 PAGE_SHIFT;
713 else if (!is_power_of_2(bufsize))
714 maxpages =
715 rounddown_pow_of_two(bufsize) >>
716 PAGE_SHIFT;
717
718 list[setcount].idx = pageidx;
719 list[setcount].count = maxpages;
720 pagecount -= maxpages;
721 pageidx += maxpages;
722 setcount++;
723 }
724 pageidx = i;
725 pagecount = 1;
726 pfn = this_pfn;
727 } else {
728 pagecount++;
729 }
730 }
731 return setcount;
732}
733
734/**
735 * program_rcvarray() - program an RcvArray group with receive buffers
736 * @fp: file pointer
737 * @vaddr: starting user virtual address
738 * @grp: RcvArray group
739 * @sets: array of struct tid_pageset holding information on physically
740 * contiguous chunks from the user buffer
741 * @start: starting index into sets array
742 * @count: number of struct tid_pageset's to program
743 * @pages: an array of struct page * for the user buffer
744 * @tidlist: the array of u32 elements when the information about the
745 * programmed RcvArray entries is to be encoded.
746 * @tididx: starting offset into tidlist
747 * @pmapped: (output parameter) number of pages programmed into the RcvArray
748 * entries.
749 *
750 * This function will program up to 'count' number of RcvArray entries from the
751 * group 'grp'. To make best use of write-combining writes, the function will
752 * perform writes to the unused RcvArray entries which will be ignored by the
753 * HW. Each RcvArray entry will be programmed with a physically contiguous
754 * buffer chunk from the user's virtual buffer.
755 *
756 * Return:
757 * -EINVAL if the requested count is larger than the size of the group,
758 * -ENOMEM or -EFAULT on error from set_rcvarray_entry(), or
759 * number of RcvArray entries programmed.
760 */
761static int program_rcvarray(struct file *fp, unsigned long vaddr,
762 struct tid_group *grp,
763 struct tid_pageset *sets,
764 unsigned start, u16 count, struct page **pages,
765 u32 *tidlist, unsigned *tididx, unsigned *pmapped)
766{
767 struct hfi1_filedata *fd = fp->private_data;
768 struct hfi1_ctxtdata *uctxt = fd->uctxt;
769 struct hfi1_devdata *dd = uctxt->dd;
770 u16 idx;
771 u32 tidinfo = 0, rcventry, useidx = 0;
772 int mapped = 0;
773
774 /* Count should never be larger than the group size */
775 if (count > grp->size)
776 return -EINVAL;
777
778 /* Find the first unused entry in the group */
779 for (idx = 0; idx < grp->size; idx++) {
780 if (!(grp->map & (1 << idx))) {
781 useidx = idx;
782 break;
783 }
784 rcv_array_wc_fill(dd, grp->base + idx);
785 }
786
787 idx = 0;
788 while (idx < count) {
789 u16 npages, pageidx, setidx = start + idx;
790 int ret = 0;
791
792 /*
793 * If this entry in the group is used, move to the next one.
794 * If we go past the end of the group, exit the loop.
795 */
796 if (useidx >= grp->size) {
797 break;
798 } else if (grp->map & (1 << useidx)) {
799 rcv_array_wc_fill(dd, grp->base + useidx);
800 useidx++;
801 continue;
802 }
803
804 rcventry = grp->base + useidx;
805 npages = sets[setidx].count;
806 pageidx = sets[setidx].idx;
807
808 ret = set_rcvarray_entry(fp, vaddr + (pageidx * PAGE_SIZE),
809 rcventry, grp, pages + pageidx,
810 npages);
811 if (ret)
812 return ret;
813 mapped += npages;
814
815 tidinfo = rcventry2tidinfo(rcventry - uctxt->expected_base) |
816 EXP_TID_SET(LEN, npages);
817 tidlist[(*tididx)++] = tidinfo;
818 grp->used++;
819 grp->map |= 1 << useidx++;
820 idx++;
821 }
822
823 /* Fill the rest of the group with "blank" writes */
824 for (; useidx < grp->size; useidx++)
825 rcv_array_wc_fill(dd, grp->base + useidx);
826 *pmapped = mapped;
827 return idx;
828}
829
830static int set_rcvarray_entry(struct file *fp, unsigned long vaddr,
831 u32 rcventry, struct tid_group *grp,
832 struct page **pages, unsigned npages)
833{
834 int ret;
835 struct hfi1_filedata *fd = fp->private_data;
836 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800837 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500838 struct hfi1_devdata *dd = uctxt->dd;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500839 dma_addr_t phys;
840
841 /*
842 * Allocate the node first so we can handle a potential
843 * failure before we've programmed anything.
844 */
845 node = kzalloc(sizeof(*node) + (sizeof(struct page *) * npages),
846 GFP_KERNEL);
847 if (!node)
848 return -ENOMEM;
849
850 phys = pci_map_single(dd->pcidev,
851 __va(page_to_phys(pages[0])),
852 npages * PAGE_SIZE, PCI_DMA_FROMDEVICE);
853 if (dma_mapping_error(&dd->pcidev->dev, phys)) {
854 dd_dev_err(dd, "Failed to DMA map Exp Rcv pages 0x%llx\n",
855 phys);
856 kfree(node);
857 return -EFAULT;
858 }
859
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800860 node->mmu.addr = vaddr;
861 node->mmu.len = npages * PAGE_SIZE;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500862 node->phys = page_to_phys(pages[0]);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500863 node->npages = npages;
864 node->rcventry = rcventry;
865 node->dma_addr = phys;
866 node->grp = grp;
867 node->freed = false;
868 memcpy(node->pages, pages, sizeof(struct page *) * npages);
869
Dean Luick622c2022016-07-28 15:21:21 -0400870 if (!fd->handler)
Dean Luicke0b09ac2016-07-28 15:21:20 -0400871 ret = tid_rb_insert(fd, &node->mmu);
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800872 else
Dean Luicke0b09ac2016-07-28 15:21:20 -0400873 ret = hfi1_mmu_rb_insert(fd->handler, &node->mmu);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500874
875 if (ret) {
876 hfi1_cdbg(TID, "Failed to insert RB node %u 0x%lx, 0x%lx %d",
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800877 node->rcventry, node->mmu.addr, node->phys, ret);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500878 pci_unmap_single(dd->pcidev, phys, npages * PAGE_SIZE,
879 PCI_DMA_FROMDEVICE);
880 kfree(node);
881 return -EFAULT;
882 }
883 hfi1_put_tid(dd, rcventry, PT_EXPECTED, phys, ilog2(npages) + 1);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800884 trace_hfi1_exp_tid_reg(uctxt->ctxt, fd->subctxt, rcventry, npages,
885 node->mmu.addr, node->phys, phys);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500886 return 0;
887}
888
889static int unprogram_rcvarray(struct file *fp, u32 tidinfo,
890 struct tid_group **grp)
891{
892 struct hfi1_filedata *fd = fp->private_data;
893 struct hfi1_ctxtdata *uctxt = fd->uctxt;
894 struct hfi1_devdata *dd = uctxt->dd;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800895 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500896 u8 tidctrl = EXP_TID_GET(tidinfo, CTRL);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800897 u32 tididx = EXP_TID_GET(tidinfo, IDX) << 1, rcventry;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500898
899 if (tididx >= uctxt->expected_count) {
900 dd_dev_err(dd, "Invalid RcvArray entry (%u) index for ctxt %u\n",
901 tididx, uctxt->ctxt);
902 return -EINVAL;
903 }
904
905 if (tidctrl == 0x3)
906 return -EINVAL;
907
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800908 rcventry = tididx + (tidctrl - 1);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500909
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800910 node = fd->entry_to_rb[rcventry];
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800911 if (!node || node->rcventry != (uctxt->expected_base + rcventry))
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500912 return -EBADF;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800913
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500914 if (grp)
915 *grp = node->grp;
Ira Weiny2677a762016-07-28 15:21:26 -0400916
917 if (!fd->handler)
918 cacheless_tid_rb_remove(fd, node);
919 else
920 hfi1_mmu_rb_remove(fd->handler, &node->mmu);
921
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500922 return 0;
923}
924
Ira Weiny5ed3b152016-07-28 12:27:32 -0400925static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500926{
927 struct hfi1_ctxtdata *uctxt = fd->uctxt;
928 struct hfi1_devdata *dd = uctxt->dd;
929
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500930 trace_hfi1_exp_tid_unreg(uctxt->ctxt, fd->subctxt, node->rcventry,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800931 node->npages, node->mmu.addr, node->phys,
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500932 node->dma_addr);
933
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500934 hfi1_put_tid(dd, node->rcventry, PT_INVALID, 0, 0);
935 /*
936 * Make sure device has seen the write before we unpin the
937 * pages.
938 */
939 flush_wc();
940
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800941 pci_unmap_single(dd->pcidev, node->dma_addr, node->mmu.len,
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500942 PCI_DMA_FROMDEVICE);
Ira Weiny3faa3d92016-07-28 15:21:19 -0400943 hfi1_release_user_pages(fd->mm, node->pages, node->npages, true);
Mitko Haralanova7922f72016-03-08 11:15:39 -0800944 fd->tid_n_pinned -= node->npages;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500945
946 node->grp->used--;
947 node->grp->map &= ~(1 << (node->rcventry - node->grp->base));
948
949 if (node->grp->used == node->grp->size - 1)
950 tid_group_move(node->grp, &uctxt->tid_full_list,
951 &uctxt->tid_used_list);
952 else if (!node->grp->used)
953 tid_group_move(node->grp, &uctxt->tid_used_list,
954 &uctxt->tid_group_list);
955 kfree(node);
956}
957
Ira Weiny2677a762016-07-28 15:21:26 -0400958/*
959 * As a simple helper for hfi1_user_exp_rcv_free, this function deals with
960 * clearing nodes in the non-cached case.
961 */
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500962static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt,
Dean Luicke0b09ac2016-07-28 15:21:20 -0400963 struct exp_tid_set *set,
964 struct hfi1_filedata *fd)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500965{
966 struct tid_group *grp, *ptr;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500967 int i;
968
969 list_for_each_entry_safe(grp, ptr, &set->list, list) {
970 list_del_init(&grp->list);
971
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500972 for (i = 0; i < grp->size; i++) {
973 if (grp->map & (1 << i)) {
974 u16 rcventry = grp->base + i;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800975 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500976
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800977 node = fd->entry_to_rb[rcventry -
978 uctxt->expected_base];
979 if (!node || node->rcventry != rcventry)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500980 continue;
Ira Weiny2677a762016-07-28 15:21:26 -0400981
982 cacheless_tid_rb_remove(fd, node);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500983 }
984 }
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500985 }
986}
987
Ira Weiny2677a762016-07-28 15:21:26 -0400988/*
989 * Always return 0 from this function. A non-zero return indicates that the
990 * remove operation will be called and that memory should be unpinned.
991 * However, the driver cannot unpin out from under PSM. Instead, retain the
992 * memory (by returning 0) and inform PSM that the memory is going away. PSM
993 * will call back later when it has removed the memory from its list.
994 */
Dean Luicke0b09ac2016-07-28 15:21:20 -0400995static int tid_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500996{
Dean Luicke0b09ac2016-07-28 15:21:20 -0400997 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800998 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
999 struct tid_rb_node *node =
1000 container_of(mnode, struct tid_rb_node, mmu);
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001001
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001002 if (node->freed)
1003 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001004
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001005 trace_hfi1_exp_tid_inval(uctxt->ctxt, fdata->subctxt, node->mmu.addr,
1006 node->rcventry, node->npages, node->dma_addr);
1007 node->freed = true;
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001008
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001009 spin_lock(&fdata->invalid_lock);
1010 if (fdata->invalid_tid_idx < uctxt->expected_count) {
1011 fdata->invalid_tids[fdata->invalid_tid_idx] =
1012 rcventry2tidinfo(node->rcventry - uctxt->expected_base);
1013 fdata->invalid_tids[fdata->invalid_tid_idx] |=
1014 EXP_TID_SET(LEN, node->npages);
1015 if (!fdata->invalid_tid_idx) {
1016 unsigned long *ev;
Mitko Haralanov0b091fb2016-02-05 11:57:58 -05001017
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001018 /*
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001019 * hfi1_set_uevent_bits() sets a user event flag
1020 * for all processes. Because calling into the
1021 * driver to process TID cache invalidations is
1022 * expensive and TID cache invalidations are
1023 * handled on a per-process basis, we can
1024 * optimize this to set the flag only for the
1025 * process in question.
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001026 */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001027 ev = uctxt->dd->events +
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001028 (((uctxt->ctxt - uctxt->dd->first_dyn_alloc_ctxt) *
1029 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001030 set_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001031 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001032 fdata->invalid_tid_idx++;
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001033 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001034 spin_unlock(&fdata->invalid_lock);
1035 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001036}
1037
Dean Luicke0b09ac2016-07-28 15:21:20 -04001038static int tid_rb_insert(void *arg, struct mmu_rb_node *node)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001039{
Dean Luicke0b09ac2016-07-28 15:21:20 -04001040 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001041 struct tid_rb_node *tnode =
1042 container_of(node, struct tid_rb_node, mmu);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001043 u32 base = fdata->uctxt->expected_base;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001044
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001045 fdata->entry_to_rb[tnode->rcventry - base] = tnode;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001046 return 0;
1047}
1048
Ira Weiny2677a762016-07-28 15:21:26 -04001049static void cacheless_tid_rb_remove(struct hfi1_filedata *fdata,
1050 struct tid_rb_node *tnode)
1051{
1052 u32 base = fdata->uctxt->expected_base;
1053
1054 fdata->entry_to_rb[tnode->rcventry - base] = NULL;
1055 clear_tid_node(fdata, tnode);
1056}
1057
Dean Luick082b3532016-07-28 15:21:25 -04001058static void tid_rb_remove(void *arg, struct mmu_rb_node *node)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001059{
Dean Luicke0b09ac2016-07-28 15:21:20 -04001060 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001061 struct tid_rb_node *tnode =
1062 container_of(node, struct tid_rb_node, mmu);
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001063
Ira Weiny2677a762016-07-28 15:21:26 -04001064 cacheless_tid_rb_remove(fdata, tnode);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001065}