blob: 35c6e7ec8ad6beb311b9e8177a09c40ff82b351d [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
Mitko Haralanovf88e0c82016-02-05 11:57:52 -050085static void unlock_exp_tids(struct hfi1_ctxtdata *, struct exp_tid_set *,
Dean Luicke0b09ac2016-07-28 15:21:20 -040086 struct hfi1_filedata *);
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -050087static u32 find_phys_blocks(struct page **, unsigned, struct tid_pageset *);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -050088static int set_rcvarray_entry(struct file *, unsigned long, u32,
Mitko Haralanov3abb33a2016-02-05 11:57:54 -050089 struct tid_group *, struct page **, unsigned);
Dean Luicke0b09ac2016-07-28 15:21:20 -040090static int tid_rb_insert(void *, struct mmu_rb_node *);
Ira Weiny2677a762016-07-28 15:21:26 -040091static void cacheless_tid_rb_remove(struct hfi1_filedata *fdata,
92 struct tid_rb_node *tnode);
Dean Luick082b3532016-07-28 15:21:25 -040093static void tid_rb_remove(void *, struct mmu_rb_node *);
Dean Luicke0b09ac2016-07-28 15:21:20 -040094static int tid_rb_invalidate(void *, struct mmu_rb_node *);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -050095static int program_rcvarray(struct file *, unsigned long, struct tid_group *,
96 struct tid_pageset *, unsigned, u16, struct page **,
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -050097 u32 *, unsigned *, unsigned *);
Mitko Haralanov455d7f12016-02-05 11:57:56 -050098static int unprogram_rcvarray(struct file *, u32, struct tid_group **);
Ira Weiny2677a762016-07-28 15:21:26 -040099static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800100
101static struct mmu_rb_ops tid_rb_ops = {
Dean Luicka7cd2dc2016-07-28 12:27:37 -0400102 .insert = tid_rb_insert,
103 .remove = tid_rb_remove,
104 .invalidate = tid_rb_invalidate
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800105};
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500106
107static inline u32 rcventry2tidinfo(u32 rcventry)
108{
109 u32 pair = rcventry & ~0x1;
110
111 return EXP_TID_SET(IDX, pair >> 1) |
112 EXP_TID_SET(CTRL, 1 << (rcventry - pair));
113}
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500114
Mitko Haralanovb8abe342016-02-05 11:57:51 -0500115static inline void exp_tid_group_init(struct exp_tid_set *set)
116{
117 INIT_LIST_HEAD(&set->list);
118 set->count = 0;
119}
120
121static inline void tid_group_remove(struct tid_group *grp,
122 struct exp_tid_set *set)
123{
124 list_del_init(&grp->list);
125 set->count--;
126}
127
128static inline void tid_group_add_tail(struct tid_group *grp,
129 struct exp_tid_set *set)
130{
131 list_add_tail(&grp->list, &set->list);
132 set->count++;
133}
134
135static inline struct tid_group *tid_group_pop(struct exp_tid_set *set)
136{
137 struct tid_group *grp =
138 list_first_entry(&set->list, struct tid_group, list);
139 list_del_init(&grp->list);
140 set->count--;
141 return grp;
142}
143
144static inline void tid_group_move(struct tid_group *group,
145 struct exp_tid_set *s1,
146 struct exp_tid_set *s2)
147{
148 tid_group_remove(group, s1);
149 tid_group_add_tail(group, s2);
150}
151
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500152/*
153 * Initialize context and file private data needed for Expected
154 * receive caching. This needs to be done after the context has
155 * been configured with the eager/expected RcvEntry counts.
156 */
157int hfi1_user_exp_rcv_init(struct file *fp)
158{
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500159 struct hfi1_filedata *fd = fp->private_data;
160 struct hfi1_ctxtdata *uctxt = fd->uctxt;
161 struct hfi1_devdata *dd = uctxt->dd;
162 unsigned tidbase;
163 int i, ret = 0;
164
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500165 spin_lock_init(&fd->tid_lock);
166 spin_lock_init(&fd->invalid_lock);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500167
168 if (!uctxt->subctxt_cnt || !fd->subctxt) {
169 exp_tid_group_init(&uctxt->tid_group_list);
170 exp_tid_group_init(&uctxt->tid_used_list);
171 exp_tid_group_init(&uctxt->tid_full_list);
172
173 tidbase = uctxt->expected_base;
174 for (i = 0; i < uctxt->expected_count /
175 dd->rcv_entries.group_size; i++) {
176 struct tid_group *grp;
177
178 grp = kzalloc(sizeof(*grp), GFP_KERNEL);
179 if (!grp) {
180 /*
181 * If we fail here, the groups already
182 * allocated will be freed by the close
183 * call.
184 */
185 ret = -ENOMEM;
186 goto done;
187 }
188 grp->size = dd->rcv_entries.group_size;
189 grp->base = tidbase;
190 tid_group_add_tail(grp, &uctxt->tid_group_list);
191 tidbase += dd->rcv_entries.group_size;
192 }
193 }
194
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800195 fd->entry_to_rb = kcalloc(uctxt->expected_count,
196 sizeof(struct rb_node *),
197 GFP_KERNEL);
198 if (!fd->entry_to_rb)
199 return -ENOMEM;
200
Dean Luick622c2022016-07-28 15:21:21 -0400201 if (!HFI1_CAP_UGET_MASK(uctxt->flags, TID_UNMAP)) {
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500202 fd->invalid_tid_idx = 0;
Markus Elfring4076e512017-02-09 15:30:53 +0100203 fd->invalid_tids = kcalloc(uctxt->expected_count,
204 sizeof(*fd->invalid_tids),
205 GFP_KERNEL);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500206 if (!fd->invalid_tids) {
207 ret = -ENOMEM;
208 goto done;
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800209 }
210
211 /*
212 * Register MMU notifier callbacks. If the registration
Dean Luick622c2022016-07-28 15:21:21 -0400213 * fails, continue without TID caching for this context.
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800214 */
Dean Luickb85ced92016-07-28 15:21:24 -0400215 ret = hfi1_mmu_rb_register(fd, fd->mm, &tid_rb_ops,
216 dd->pport->hfi1_wq,
217 &fd->handler);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800218 if (ret) {
219 dd_dev_info(dd,
220 "Failed MMU notifier registration %d\n",
221 ret);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800222 ret = 0;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500223 }
224 }
225
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500226 /*
227 * PSM does not have a good way to separate, count, and
228 * effectively enforce a limit on RcvArray entries used by
229 * subctxts (when context sharing is used) when TID caching
230 * is enabled. To help with that, we calculate a per-process
231 * RcvArray entry share and enforce that.
232 * If TID caching is not in use, PSM deals with usage on its
233 * own. In that case, we allow any subctxt to take all of the
234 * entries.
235 *
236 * Make sure that we set the tid counts only after successful
237 * init.
238 */
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500239 spin_lock(&fd->tid_lock);
Dean Luick622c2022016-07-28 15:21:21 -0400240 if (uctxt->subctxt_cnt && fd->handler) {
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500241 u16 remainder;
242
243 fd->tid_limit = uctxt->expected_count / uctxt->subctxt_cnt;
244 remainder = uctxt->expected_count % uctxt->subctxt_cnt;
245 if (remainder && fd->subctxt < remainder)
246 fd->tid_limit++;
247 } else {
248 fd->tid_limit = uctxt->expected_count;
249 }
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500250 spin_unlock(&fd->tid_lock);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500251done:
252 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500253}
254
255int hfi1_user_exp_rcv_free(struct hfi1_filedata *fd)
256{
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500257 struct hfi1_ctxtdata *uctxt = fd->uctxt;
258 struct tid_group *grp, *gptr;
259
Mitko Haralanov94158442016-04-20 06:05:36 -0700260 if (!test_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags))
261 return 0;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500262 /*
263 * The notifier would have been removed when the process'es mm
264 * was freed.
265 */
Dean Luick622c2022016-07-28 15:21:21 -0400266 if (fd->handler)
Dean Luicke0b09ac2016-07-28 15:21:20 -0400267 hfi1_mmu_rb_unregister(fd->handler);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500268
269 kfree(fd->invalid_tids);
270
271 if (!uctxt->cnt) {
272 if (!EXP_TID_SET_EMPTY(uctxt->tid_full_list))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400273 unlock_exp_tids(uctxt, &uctxt->tid_full_list, fd);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500274 if (!EXP_TID_SET_EMPTY(uctxt->tid_used_list))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400275 unlock_exp_tids(uctxt, &uctxt->tid_used_list, fd);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500276 list_for_each_entry_safe(grp, gptr, &uctxt->tid_group_list.list,
277 list) {
278 list_del_init(&grp->list);
279 kfree(grp);
280 }
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500281 hfi1_clear_tids(uctxt);
282 }
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800283
284 kfree(fd->entry_to_rb);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500285 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500286}
287
Mitko Haralanovb8abe342016-02-05 11:57:51 -0500288/*
289 * Write an "empty" RcvArray entry.
290 * This function exists so the TID registaration code can use it
291 * to write to unused/unneeded entries and still take advantage
292 * of the WC performance improvements. The HFI will ignore this
293 * write to the RcvArray entry.
294 */
295static inline void rcv_array_wc_fill(struct hfi1_devdata *dd, u32 index)
296{
297 /*
298 * Doing the WC fill writes only makes sense if the device is
299 * present and the RcvArray has been mapped as WC memory.
300 */
301 if ((dd->flags & HFI1_PRESENT) && dd->rcvarray_wc)
302 writeq(0, dd->rcvarray_wc + (index * 8));
303}
304
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500305/*
306 * RcvArray entry allocation for Expected Receives is done by the
307 * following algorithm:
308 *
309 * The context keeps 3 lists of groups of RcvArray entries:
310 * 1. List of empty groups - tid_group_list
311 * This list is created during user context creation and
312 * contains elements which describe sets (of 8) of empty
313 * RcvArray entries.
314 * 2. List of partially used groups - tid_used_list
315 * This list contains sets of RcvArray entries which are
316 * not completely used up. Another mapping request could
317 * use some of all of the remaining entries.
318 * 3. List of full groups - tid_full_list
319 * This is the list where sets that are completely used
320 * up go.
321 *
322 * An attempt to optimize the usage of RcvArray entries is
323 * made by finding all sets of physically contiguous pages in a
324 * user's buffer.
325 * These physically contiguous sets are further split into
326 * sizes supported by the receive engine of the HFI. The
327 * resulting sets of pages are stored in struct tid_pageset,
328 * which describes the sets as:
329 * * .count - number of pages in this set
330 * * .idx - starting index into struct page ** array
331 * of this set
332 *
333 * From this point on, the algorithm deals with the page sets
334 * described above. The number of pagesets is divided by the
335 * RcvArray group size to produce the number of full groups
336 * needed.
337 *
338 * Groups from the 3 lists are manipulated using the following
339 * rules:
340 * 1. For each set of 8 pagesets, a complete group from
341 * tid_group_list is taken, programmed, and moved to
342 * the tid_full_list list.
343 * 2. For all remaining pagesets:
344 * 2.1 If the tid_used_list is empty and the tid_group_list
345 * is empty, stop processing pageset and return only
346 * what has been programmed up to this point.
347 * 2.2 If the tid_used_list is empty and the tid_group_list
348 * is not empty, move a group from tid_group_list to
349 * tid_used_list.
350 * 2.3 For each group is tid_used_group, program as much as
351 * can fit into the group. If the group becomes fully
352 * used, move it to tid_full_list.
353 */
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500354int hfi1_user_exp_rcv_setup(struct file *fp, struct hfi1_tid_info *tinfo)
355{
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500356 int ret = 0, need_group = 0, pinned;
357 struct hfi1_filedata *fd = fp->private_data;
358 struct hfi1_ctxtdata *uctxt = fd->uctxt;
359 struct hfi1_devdata *dd = uctxt->dd;
360 unsigned npages, ngroups, pageidx = 0, pageset_count, npagesets,
361 tididx = 0, mapped, mapped_pages = 0;
362 unsigned long vaddr = tinfo->vaddr;
363 struct page **pages = NULL;
364 u32 *tidlist = NULL;
365 struct tid_pageset *pagesets = NULL;
366
367 /* Get the number of pages the user buffer spans */
368 npages = num_user_pages(vaddr, tinfo->length);
369 if (!npages)
370 return -EINVAL;
371
372 if (npages > uctxt->expected_count) {
373 dd_dev_err(dd, "Expected buffer too big\n");
374 return -EINVAL;
375 }
376
377 /* Verify that access is OK for the user buffer */
378 if (!access_ok(VERIFY_WRITE, (void __user *)vaddr,
379 npages * PAGE_SIZE)) {
380 dd_dev_err(dd, "Fail vaddr %p, %u pages, !access_ok\n",
381 (void *)vaddr, npages);
382 return -EFAULT;
383 }
384
385 pagesets = kcalloc(uctxt->expected_count, sizeof(*pagesets),
386 GFP_KERNEL);
387 if (!pagesets)
388 return -ENOMEM;
389
390 /* Allocate the array of struct page pointers needed for pinning */
391 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
392 if (!pages) {
393 ret = -ENOMEM;
394 goto bail;
395 }
396
397 /*
398 * Pin all the pages of the user buffer. If we can't pin all the
399 * pages, accept the amount pinned so far and program only that.
400 * User space knows how to deal with partially programmed buffers.
401 */
Ira Weiny3faa3d92016-07-28 15:21:19 -0400402 if (!hfi1_can_pin_pages(dd, fd->mm, fd->tid_n_pinned, npages)) {
Mitko Haralanov0ad2d3d2016-04-12 10:46:29 -0700403 ret = -ENOMEM;
404 goto bail;
405 }
406
Ira Weiny3faa3d92016-07-28 15:21:19 -0400407 pinned = hfi1_acquire_user_pages(fd->mm, vaddr, npages, true, pages);
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500408 if (pinned <= 0) {
409 ret = pinned;
410 goto bail;
411 }
Mitko Haralanova7922f72016-03-08 11:15:39 -0800412 fd->tid_n_pinned += npages;
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500413
414 /* Find sets of physically contiguous pages */
415 npagesets = find_phys_blocks(pages, pinned, pagesets);
416
417 /*
418 * We don't need to access this under a lock since tid_used is per
419 * process and the same process cannot be in hfi1_user_exp_rcv_clear()
420 * and hfi1_user_exp_rcv_setup() at the same time.
421 */
422 spin_lock(&fd->tid_lock);
423 if (fd->tid_used + npagesets > fd->tid_limit)
424 pageset_count = fd->tid_limit - fd->tid_used;
425 else
426 pageset_count = npagesets;
427 spin_unlock(&fd->tid_lock);
428
429 if (!pageset_count)
430 goto bail;
431
432 ngroups = pageset_count / dd->rcv_entries.group_size;
433 tidlist = kcalloc(pageset_count, sizeof(*tidlist), GFP_KERNEL);
434 if (!tidlist) {
435 ret = -ENOMEM;
436 goto nomem;
437 }
438
439 tididx = 0;
440
441 /*
442 * From this point on, we are going to be using shared (between master
443 * and subcontexts) context resources. We need to take the lock.
444 */
445 mutex_lock(&uctxt->exp_lock);
446 /*
447 * The first step is to program the RcvArray entries which are complete
448 * groups.
449 */
450 while (ngroups && uctxt->tid_group_list.count) {
451 struct tid_group *grp =
452 tid_group_pop(&uctxt->tid_group_list);
453
454 ret = program_rcvarray(fp, vaddr, grp, pagesets,
455 pageidx, dd->rcv_entries.group_size,
456 pages, tidlist, &tididx, &mapped);
457 /*
458 * If there was a failure to program the RcvArray
459 * entries for the entire group, reset the grp fields
460 * and add the grp back to the free group list.
461 */
462 if (ret <= 0) {
463 tid_group_add_tail(grp, &uctxt->tid_group_list);
464 hfi1_cdbg(TID,
465 "Failed to program RcvArray group %d", ret);
466 goto unlock;
467 }
468
469 tid_group_add_tail(grp, &uctxt->tid_full_list);
470 ngroups--;
471 pageidx += ret;
472 mapped_pages += mapped;
473 }
474
475 while (pageidx < pageset_count) {
476 struct tid_group *grp, *ptr;
477 /*
478 * If we don't have any partially used tid groups, check
479 * if we have empty groups. If so, take one from there and
480 * put in the partially used list.
481 */
482 if (!uctxt->tid_used_list.count || need_group) {
483 if (!uctxt->tid_group_list.count)
484 goto unlock;
485
486 grp = tid_group_pop(&uctxt->tid_group_list);
487 tid_group_add_tail(grp, &uctxt->tid_used_list);
488 need_group = 0;
489 }
490 /*
491 * There is an optimization opportunity here - instead of
492 * fitting as many page sets as we can, check for a group
493 * later on in the list that could fit all of them.
494 */
495 list_for_each_entry_safe(grp, ptr, &uctxt->tid_used_list.list,
496 list) {
497 unsigned use = min_t(unsigned, pageset_count - pageidx,
498 grp->size - grp->used);
499
500 ret = program_rcvarray(fp, vaddr, grp, pagesets,
501 pageidx, use, pages, tidlist,
502 &tididx, &mapped);
503 if (ret < 0) {
504 hfi1_cdbg(TID,
505 "Failed to program RcvArray entries %d",
506 ret);
507 ret = -EFAULT;
508 goto unlock;
509 } else if (ret > 0) {
510 if (grp->used == grp->size)
511 tid_group_move(grp,
512 &uctxt->tid_used_list,
513 &uctxt->tid_full_list);
514 pageidx += ret;
515 mapped_pages += mapped;
516 need_group = 0;
517 /* Check if we are done so we break out early */
518 if (pageidx >= pageset_count)
519 break;
520 } else if (WARN_ON(ret == 0)) {
521 /*
522 * If ret is 0, we did not program any entries
523 * into this group, which can only happen if
524 * we've screwed up the accounting somewhere.
525 * Warn and try to continue.
526 */
527 need_group = 1;
528 }
529 }
530 }
531unlock:
532 mutex_unlock(&uctxt->exp_lock);
533nomem:
534 hfi1_cdbg(TID, "total mapped: tidpairs:%u pages:%u (%d)", tididx,
535 mapped_pages, ret);
536 if (tididx) {
537 spin_lock(&fd->tid_lock);
538 fd->tid_used += tididx;
539 spin_unlock(&fd->tid_lock);
540 tinfo->tidcnt = tididx;
541 tinfo->length = mapped_pages * PAGE_SIZE;
542
543 if (copy_to_user((void __user *)(unsigned long)tinfo->tidlist,
544 tidlist, sizeof(tidlist[0]) * tididx)) {
545 /*
546 * On failure to copy to the user level, we need to undo
547 * everything done so far so we don't leak resources.
548 */
549 tinfo->tidlist = (unsigned long)&tidlist;
550 hfi1_user_exp_rcv_clear(fp, tinfo);
551 tinfo->tidlist = 0;
552 ret = -EFAULT;
553 goto bail;
554 }
555 }
556
557 /*
558 * If not everything was mapped (due to insufficient RcvArray entries,
559 * for example), unpin all unmapped pages so we can pin them nex time.
560 */
Mitko Haralanova7922f72016-03-08 11:15:39 -0800561 if (mapped_pages != pinned) {
Ira Weiny3faa3d92016-07-28 15:21:19 -0400562 hfi1_release_user_pages(fd->mm, &pages[mapped_pages],
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500563 pinned - mapped_pages,
564 false);
Mitko Haralanova7922f72016-03-08 11:15:39 -0800565 fd->tid_n_pinned -= pinned - mapped_pages;
566 }
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500567bail:
568 kfree(pagesets);
569 kfree(pages);
570 kfree(tidlist);
571 return ret > 0 ? 0 : ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500572}
573
574int hfi1_user_exp_rcv_clear(struct file *fp, struct hfi1_tid_info *tinfo)
575{
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500576 int ret = 0;
577 struct hfi1_filedata *fd = fp->private_data;
578 struct hfi1_ctxtdata *uctxt = fd->uctxt;
579 u32 *tidinfo;
580 unsigned tididx;
581
Michael J. Ruhldb730892017-04-09 10:16:03 -0700582 if (unlikely(tinfo->tidcnt > fd->tid_used))
583 return -EINVAL;
584
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800585 tidinfo = memdup_user((void __user *)(unsigned long)tinfo->tidlist,
586 sizeof(tidinfo[0]) * tinfo->tidcnt);
587 if (IS_ERR(tidinfo))
588 return PTR_ERR(tidinfo);
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500589
590 mutex_lock(&uctxt->exp_lock);
591 for (tididx = 0; tididx < tinfo->tidcnt; tididx++) {
592 ret = unprogram_rcvarray(fp, tidinfo[tididx], NULL);
593 if (ret) {
594 hfi1_cdbg(TID, "Failed to unprogram rcv array %d",
595 ret);
596 break;
597 }
598 }
599 spin_lock(&fd->tid_lock);
600 fd->tid_used -= tididx;
601 spin_unlock(&fd->tid_lock);
602 tinfo->tidcnt = tididx;
603 mutex_unlock(&uctxt->exp_lock);
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800604
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500605 kfree(tidinfo);
606 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500607}
608
609int hfi1_user_exp_rcv_invalid(struct file *fp, struct hfi1_tid_info *tinfo)
610{
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500611 struct hfi1_filedata *fd = fp->private_data;
612 struct hfi1_ctxtdata *uctxt = fd->uctxt;
613 unsigned long *ev = uctxt->dd->events +
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700614 (((uctxt->ctxt - uctxt->dd->first_dyn_alloc_ctxt) *
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500615 HFI1_MAX_SHARED_CTXTS) + fd->subctxt);
616 u32 *array;
617 int ret = 0;
618
619 if (!fd->invalid_tids)
620 return -EINVAL;
621
622 /*
623 * copy_to_user() can sleep, which will leave the invalid_lock
624 * locked and cause the MMU notifier to be blocked on the lock
625 * for a long time.
626 * Copy the data to a local buffer so we can release the lock.
627 */
628 array = kcalloc(uctxt->expected_count, sizeof(*array), GFP_KERNEL);
629 if (!array)
630 return -EFAULT;
631
632 spin_lock(&fd->invalid_lock);
633 if (fd->invalid_tid_idx) {
634 memcpy(array, fd->invalid_tids, sizeof(*array) *
635 fd->invalid_tid_idx);
636 memset(fd->invalid_tids, 0, sizeof(*fd->invalid_tids) *
637 fd->invalid_tid_idx);
638 tinfo->tidcnt = fd->invalid_tid_idx;
639 fd->invalid_tid_idx = 0;
640 /*
641 * Reset the user flag while still holding the lock.
642 * Otherwise, PSM can miss events.
643 */
644 clear_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
645 } else {
646 tinfo->tidcnt = 0;
647 }
648 spin_unlock(&fd->invalid_lock);
649
650 if (tinfo->tidcnt) {
651 if (copy_to_user((void __user *)tinfo->tidlist,
652 array, sizeof(*array) * tinfo->tidcnt))
653 ret = -EFAULT;
654 }
655 kfree(array);
656
657 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500658}
659
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500660static u32 find_phys_blocks(struct page **pages, unsigned npages,
661 struct tid_pageset *list)
662{
663 unsigned pagecount, pageidx, setcount = 0, i;
664 unsigned long pfn, this_pfn;
665
666 if (!npages)
667 return 0;
668
669 /*
670 * Look for sets of physically contiguous pages in the user buffer.
671 * This will allow us to optimize Expected RcvArray entry usage by
672 * using the bigger supported sizes.
673 */
674 pfn = page_to_pfn(pages[0]);
675 for (pageidx = 0, pagecount = 1, i = 1; i <= npages; i++) {
676 this_pfn = i < npages ? page_to_pfn(pages[i]) : 0;
677
678 /*
679 * If the pfn's are not sequential, pages are not physically
680 * contiguous.
681 */
682 if (this_pfn != ++pfn) {
683 /*
684 * At this point we have to loop over the set of
685 * physically contiguous pages and break them down it
686 * sizes supported by the HW.
687 * There are two main constraints:
688 * 1. The max buffer size is MAX_EXPECTED_BUFFER.
689 * If the total set size is bigger than that
690 * program only a MAX_EXPECTED_BUFFER chunk.
691 * 2. The buffer size has to be a power of two. If
692 * it is not, round down to the closes power of
693 * 2 and program that size.
694 */
695 while (pagecount) {
696 int maxpages = pagecount;
697 u32 bufsize = pagecount * PAGE_SIZE;
698
699 if (bufsize > MAX_EXPECTED_BUFFER)
700 maxpages =
701 MAX_EXPECTED_BUFFER >>
702 PAGE_SHIFT;
703 else if (!is_power_of_2(bufsize))
704 maxpages =
705 rounddown_pow_of_two(bufsize) >>
706 PAGE_SHIFT;
707
708 list[setcount].idx = pageidx;
709 list[setcount].count = maxpages;
710 pagecount -= maxpages;
711 pageidx += maxpages;
712 setcount++;
713 }
714 pageidx = i;
715 pagecount = 1;
716 pfn = this_pfn;
717 } else {
718 pagecount++;
719 }
720 }
721 return setcount;
722}
723
724/**
725 * program_rcvarray() - program an RcvArray group with receive buffers
726 * @fp: file pointer
727 * @vaddr: starting user virtual address
728 * @grp: RcvArray group
729 * @sets: array of struct tid_pageset holding information on physically
730 * contiguous chunks from the user buffer
731 * @start: starting index into sets array
732 * @count: number of struct tid_pageset's to program
733 * @pages: an array of struct page * for the user buffer
734 * @tidlist: the array of u32 elements when the information about the
735 * programmed RcvArray entries is to be encoded.
736 * @tididx: starting offset into tidlist
737 * @pmapped: (output parameter) number of pages programmed into the RcvArray
738 * entries.
739 *
740 * This function will program up to 'count' number of RcvArray entries from the
741 * group 'grp'. To make best use of write-combining writes, the function will
742 * perform writes to the unused RcvArray entries which will be ignored by the
743 * HW. Each RcvArray entry will be programmed with a physically contiguous
744 * buffer chunk from the user's virtual buffer.
745 *
746 * Return:
747 * -EINVAL if the requested count is larger than the size of the group,
748 * -ENOMEM or -EFAULT on error from set_rcvarray_entry(), or
749 * number of RcvArray entries programmed.
750 */
751static int program_rcvarray(struct file *fp, unsigned long vaddr,
752 struct tid_group *grp,
753 struct tid_pageset *sets,
754 unsigned start, u16 count, struct page **pages,
755 u32 *tidlist, unsigned *tididx, unsigned *pmapped)
756{
757 struct hfi1_filedata *fd = fp->private_data;
758 struct hfi1_ctxtdata *uctxt = fd->uctxt;
759 struct hfi1_devdata *dd = uctxt->dd;
760 u16 idx;
761 u32 tidinfo = 0, rcventry, useidx = 0;
762 int mapped = 0;
763
764 /* Count should never be larger than the group size */
765 if (count > grp->size)
766 return -EINVAL;
767
768 /* Find the first unused entry in the group */
769 for (idx = 0; idx < grp->size; idx++) {
770 if (!(grp->map & (1 << idx))) {
771 useidx = idx;
772 break;
773 }
774 rcv_array_wc_fill(dd, grp->base + idx);
775 }
776
777 idx = 0;
778 while (idx < count) {
779 u16 npages, pageidx, setidx = start + idx;
780 int ret = 0;
781
782 /*
783 * If this entry in the group is used, move to the next one.
784 * If we go past the end of the group, exit the loop.
785 */
786 if (useidx >= grp->size) {
787 break;
788 } else if (grp->map & (1 << useidx)) {
789 rcv_array_wc_fill(dd, grp->base + useidx);
790 useidx++;
791 continue;
792 }
793
794 rcventry = grp->base + useidx;
795 npages = sets[setidx].count;
796 pageidx = sets[setidx].idx;
797
798 ret = set_rcvarray_entry(fp, vaddr + (pageidx * PAGE_SIZE),
799 rcventry, grp, pages + pageidx,
800 npages);
801 if (ret)
802 return ret;
803 mapped += npages;
804
805 tidinfo = rcventry2tidinfo(rcventry - uctxt->expected_base) |
806 EXP_TID_SET(LEN, npages);
807 tidlist[(*tididx)++] = tidinfo;
808 grp->used++;
809 grp->map |= 1 << useidx++;
810 idx++;
811 }
812
813 /* Fill the rest of the group with "blank" writes */
814 for (; useidx < grp->size; useidx++)
815 rcv_array_wc_fill(dd, grp->base + useidx);
816 *pmapped = mapped;
817 return idx;
818}
819
820static int set_rcvarray_entry(struct file *fp, unsigned long vaddr,
821 u32 rcventry, struct tid_group *grp,
822 struct page **pages, unsigned npages)
823{
824 int ret;
825 struct hfi1_filedata *fd = fp->private_data;
826 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800827 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500828 struct hfi1_devdata *dd = uctxt->dd;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500829 dma_addr_t phys;
830
831 /*
832 * Allocate the node first so we can handle a potential
833 * failure before we've programmed anything.
834 */
835 node = kzalloc(sizeof(*node) + (sizeof(struct page *) * npages),
836 GFP_KERNEL);
837 if (!node)
838 return -ENOMEM;
839
840 phys = pci_map_single(dd->pcidev,
841 __va(page_to_phys(pages[0])),
842 npages * PAGE_SIZE, PCI_DMA_FROMDEVICE);
843 if (dma_mapping_error(&dd->pcidev->dev, phys)) {
844 dd_dev_err(dd, "Failed to DMA map Exp Rcv pages 0x%llx\n",
845 phys);
846 kfree(node);
847 return -EFAULT;
848 }
849
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800850 node->mmu.addr = vaddr;
851 node->mmu.len = npages * PAGE_SIZE;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500852 node->phys = page_to_phys(pages[0]);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500853 node->npages = npages;
854 node->rcventry = rcventry;
855 node->dma_addr = phys;
856 node->grp = grp;
857 node->freed = false;
858 memcpy(node->pages, pages, sizeof(struct page *) * npages);
859
Dean Luick622c2022016-07-28 15:21:21 -0400860 if (!fd->handler)
Dean Luicke0b09ac2016-07-28 15:21:20 -0400861 ret = tid_rb_insert(fd, &node->mmu);
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800862 else
Dean Luicke0b09ac2016-07-28 15:21:20 -0400863 ret = hfi1_mmu_rb_insert(fd->handler, &node->mmu);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500864
865 if (ret) {
866 hfi1_cdbg(TID, "Failed to insert RB node %u 0x%lx, 0x%lx %d",
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800867 node->rcventry, node->mmu.addr, node->phys, ret);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500868 pci_unmap_single(dd->pcidev, phys, npages * PAGE_SIZE,
869 PCI_DMA_FROMDEVICE);
870 kfree(node);
871 return -EFAULT;
872 }
873 hfi1_put_tid(dd, rcventry, PT_EXPECTED, phys, ilog2(npages) + 1);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800874 trace_hfi1_exp_tid_reg(uctxt->ctxt, fd->subctxt, rcventry, npages,
875 node->mmu.addr, node->phys, phys);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500876 return 0;
877}
878
879static int unprogram_rcvarray(struct file *fp, u32 tidinfo,
880 struct tid_group **grp)
881{
882 struct hfi1_filedata *fd = fp->private_data;
883 struct hfi1_ctxtdata *uctxt = fd->uctxt;
884 struct hfi1_devdata *dd = uctxt->dd;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800885 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500886 u8 tidctrl = EXP_TID_GET(tidinfo, CTRL);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800887 u32 tididx = EXP_TID_GET(tidinfo, IDX) << 1, rcventry;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500888
889 if (tididx >= uctxt->expected_count) {
890 dd_dev_err(dd, "Invalid RcvArray entry (%u) index for ctxt %u\n",
891 tididx, uctxt->ctxt);
892 return -EINVAL;
893 }
894
895 if (tidctrl == 0x3)
896 return -EINVAL;
897
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800898 rcventry = tididx + (tidctrl - 1);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500899
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800900 node = fd->entry_to_rb[rcventry];
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800901 if (!node || node->rcventry != (uctxt->expected_base + rcventry))
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500902 return -EBADF;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800903
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500904 if (grp)
905 *grp = node->grp;
Ira Weiny2677a762016-07-28 15:21:26 -0400906
907 if (!fd->handler)
908 cacheless_tid_rb_remove(fd, node);
909 else
910 hfi1_mmu_rb_remove(fd->handler, &node->mmu);
911
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500912 return 0;
913}
914
Ira Weiny5ed3b152016-07-28 12:27:32 -0400915static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500916{
917 struct hfi1_ctxtdata *uctxt = fd->uctxt;
918 struct hfi1_devdata *dd = uctxt->dd;
919
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500920 trace_hfi1_exp_tid_unreg(uctxt->ctxt, fd->subctxt, node->rcventry,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800921 node->npages, node->mmu.addr, node->phys,
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500922 node->dma_addr);
923
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500924 hfi1_put_tid(dd, node->rcventry, PT_INVALID, 0, 0);
925 /*
926 * Make sure device has seen the write before we unpin the
927 * pages.
928 */
929 flush_wc();
930
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800931 pci_unmap_single(dd->pcidev, node->dma_addr, node->mmu.len,
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500932 PCI_DMA_FROMDEVICE);
Ira Weiny3faa3d92016-07-28 15:21:19 -0400933 hfi1_release_user_pages(fd->mm, node->pages, node->npages, true);
Mitko Haralanova7922f72016-03-08 11:15:39 -0800934 fd->tid_n_pinned -= node->npages;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500935
936 node->grp->used--;
937 node->grp->map &= ~(1 << (node->rcventry - node->grp->base));
938
939 if (node->grp->used == node->grp->size - 1)
940 tid_group_move(node->grp, &uctxt->tid_full_list,
941 &uctxt->tid_used_list);
942 else if (!node->grp->used)
943 tid_group_move(node->grp, &uctxt->tid_used_list,
944 &uctxt->tid_group_list);
945 kfree(node);
946}
947
Ira Weiny2677a762016-07-28 15:21:26 -0400948/*
949 * As a simple helper for hfi1_user_exp_rcv_free, this function deals with
950 * clearing nodes in the non-cached case.
951 */
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500952static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt,
Dean Luicke0b09ac2016-07-28 15:21:20 -0400953 struct exp_tid_set *set,
954 struct hfi1_filedata *fd)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500955{
956 struct tid_group *grp, *ptr;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500957 int i;
958
959 list_for_each_entry_safe(grp, ptr, &set->list, list) {
960 list_del_init(&grp->list);
961
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500962 for (i = 0; i < grp->size; i++) {
963 if (grp->map & (1 << i)) {
964 u16 rcventry = grp->base + i;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800965 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500966
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800967 node = fd->entry_to_rb[rcventry -
968 uctxt->expected_base];
969 if (!node || node->rcventry != rcventry)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500970 continue;
Ira Weiny2677a762016-07-28 15:21:26 -0400971
972 cacheless_tid_rb_remove(fd, node);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500973 }
974 }
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500975 }
976}
977
Ira Weiny2677a762016-07-28 15:21:26 -0400978/*
979 * Always return 0 from this function. A non-zero return indicates that the
980 * remove operation will be called and that memory should be unpinned.
981 * However, the driver cannot unpin out from under PSM. Instead, retain the
982 * memory (by returning 0) and inform PSM that the memory is going away. PSM
983 * will call back later when it has removed the memory from its list.
984 */
Dean Luicke0b09ac2016-07-28 15:21:20 -0400985static int tid_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500986{
Dean Luicke0b09ac2016-07-28 15:21:20 -0400987 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800988 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
989 struct tid_rb_node *node =
990 container_of(mnode, struct tid_rb_node, mmu);
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500991
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800992 if (node->freed)
993 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500994
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800995 trace_hfi1_exp_tid_inval(uctxt->ctxt, fdata->subctxt, node->mmu.addr,
996 node->rcventry, node->npages, node->dma_addr);
997 node->freed = true;
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -0500998
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800999 spin_lock(&fdata->invalid_lock);
1000 if (fdata->invalid_tid_idx < uctxt->expected_count) {
1001 fdata->invalid_tids[fdata->invalid_tid_idx] =
1002 rcventry2tidinfo(node->rcventry - uctxt->expected_base);
1003 fdata->invalid_tids[fdata->invalid_tid_idx] |=
1004 EXP_TID_SET(LEN, node->npages);
1005 if (!fdata->invalid_tid_idx) {
1006 unsigned long *ev;
Mitko Haralanov0b091fb2016-02-05 11:57:58 -05001007
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001008 /*
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001009 * hfi1_set_uevent_bits() sets a user event flag
1010 * for all processes. Because calling into the
1011 * driver to process TID cache invalidations is
1012 * expensive and TID cache invalidations are
1013 * handled on a per-process basis, we can
1014 * optimize this to set the flag only for the
1015 * process in question.
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001016 */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001017 ev = uctxt->dd->events +
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001018 (((uctxt->ctxt - uctxt->dd->first_dyn_alloc_ctxt) *
1019 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001020 set_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001021 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001022 fdata->invalid_tid_idx++;
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001023 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001024 spin_unlock(&fdata->invalid_lock);
1025 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001026}
1027
Dean Luicke0b09ac2016-07-28 15:21:20 -04001028static int tid_rb_insert(void *arg, struct mmu_rb_node *node)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001029{
Dean Luicke0b09ac2016-07-28 15:21:20 -04001030 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001031 struct tid_rb_node *tnode =
1032 container_of(node, struct tid_rb_node, mmu);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001033 u32 base = fdata->uctxt->expected_base;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001034
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001035 fdata->entry_to_rb[tnode->rcventry - base] = tnode;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001036 return 0;
1037}
1038
Ira Weiny2677a762016-07-28 15:21:26 -04001039static void cacheless_tid_rb_remove(struct hfi1_filedata *fdata,
1040 struct tid_rb_node *tnode)
1041{
1042 u32 base = fdata->uctxt->expected_base;
1043
1044 fdata->entry_to_rb[tnode->rcventry - base] = NULL;
1045 clear_tid_node(fdata, tnode);
1046}
1047
Dean Luick082b3532016-07-28 15:21:25 -04001048static void tid_rb_remove(void *arg, struct mmu_rb_node *node)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001049{
Dean Luicke0b09ac2016-07-28 15:21:20 -04001050 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001051 struct tid_rb_node *tnode =
1052 container_of(node, struct tid_rb_node, mmu);
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001053
Ira Weiny2677a762016-07-28 15:21:26 -04001054 cacheless_tid_rb_remove(fdata, tnode);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001055}