blob: 4c66f8d57cc1fb097ce7c9a550e17479baaf1e41 [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;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -070056 u32 base;
Mitko Haralanovb8abe342016-02-05 11:57:51 -050057 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);
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -070090static int set_rcvarray_entry(struct hfi1_filedata *fd, unsigned long vaddr,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070091 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);
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -070098static int program_rcvarray(struct hfi1_filedata *fd, unsigned long vaddr,
99 struct tid_group *grp, struct tid_pageset *sets,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700100 unsigned start, u16 count, struct page **pages,
101 u32 *tidlist, unsigned *tididx, unsigned *pmapped);
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700102static int unprogram_rcvarray(struct hfi1_filedata *fd, u32 tidinfo,
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700103 struct tid_group **grp);
Ira Weiny2677a762016-07-28 15:21:26 -0400104static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800105
106static struct mmu_rb_ops tid_rb_ops = {
Dean Luicka7cd2dc2016-07-28 12:27:37 -0400107 .insert = tid_rb_insert,
108 .remove = tid_rb_remove,
109 .invalidate = tid_rb_invalidate
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800110};
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500111
112static inline u32 rcventry2tidinfo(u32 rcventry)
113{
114 u32 pair = rcventry & ~0x1;
115
116 return EXP_TID_SET(IDX, pair >> 1) |
117 EXP_TID_SET(CTRL, 1 << (rcventry - pair));
118}
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500119
Mitko Haralanovb8abe342016-02-05 11:57:51 -0500120static inline void exp_tid_group_init(struct exp_tid_set *set)
121{
122 INIT_LIST_HEAD(&set->list);
123 set->count = 0;
124}
125
126static inline void tid_group_remove(struct tid_group *grp,
127 struct exp_tid_set *set)
128{
129 list_del_init(&grp->list);
130 set->count--;
131}
132
133static inline void tid_group_add_tail(struct tid_group *grp,
134 struct exp_tid_set *set)
135{
136 list_add_tail(&grp->list, &set->list);
137 set->count++;
138}
139
140static inline struct tid_group *tid_group_pop(struct exp_tid_set *set)
141{
142 struct tid_group *grp =
143 list_first_entry(&set->list, struct tid_group, list);
144 list_del_init(&grp->list);
145 set->count--;
146 return grp;
147}
148
149static inline void tid_group_move(struct tid_group *group,
150 struct exp_tid_set *s1,
151 struct exp_tid_set *s2)
152{
153 tid_group_remove(group, s1);
154 tid_group_add_tail(group, s2);
155}
156
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700157int hfi1_user_exp_rcv_grp_init(struct hfi1_filedata *fd)
158{
159 struct hfi1_ctxtdata *uctxt = fd->uctxt;
160 struct hfi1_devdata *dd = fd->dd;
161 u32 tidbase;
162 u32 i;
163
164 exp_tid_group_init(&uctxt->tid_group_list);
165 exp_tid_group_init(&uctxt->tid_used_list);
166 exp_tid_group_init(&uctxt->tid_full_list);
167
168 tidbase = uctxt->expected_base;
169 for (i = 0; i < uctxt->expected_count /
170 dd->rcv_entries.group_size; i++) {
171 struct tid_group *grp;
172
173 grp = kzalloc(sizeof(*grp), GFP_KERNEL);
174 if (!grp) {
175 /*
176 * If we fail here, the groups already
177 * allocated will be freed by the close
178 * call.
179 */
180 return -ENOMEM;
181 }
182 grp->size = dd->rcv_entries.group_size;
183 grp->base = tidbase;
184 tid_group_add_tail(grp, &uctxt->tid_group_list);
185 tidbase += dd->rcv_entries.group_size;
186 }
187
188 return 0;
189}
190
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500191/*
192 * Initialize context and file private data needed for Expected
193 * receive caching. This needs to be done after the context has
194 * been configured with the eager/expected RcvEntry counts.
195 */
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700196int hfi1_user_exp_rcv_init(struct hfi1_filedata *fd)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500197{
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500198 struct hfi1_ctxtdata *uctxt = fd->uctxt;
199 struct hfi1_devdata *dd = uctxt->dd;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700200 int ret = 0;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500201
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500202 spin_lock_init(&fd->tid_lock);
203 spin_lock_init(&fd->invalid_lock);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500204
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800205 fd->entry_to_rb = kcalloc(uctxt->expected_count,
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700206 sizeof(struct rb_node *),
207 GFP_KERNEL);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800208 if (!fd->entry_to_rb)
209 return -ENOMEM;
210
Dean Luick622c2022016-07-28 15:21:21 -0400211 if (!HFI1_CAP_UGET_MASK(uctxt->flags, TID_UNMAP)) {
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500212 fd->invalid_tid_idx = 0;
Markus Elfring4076e512017-02-09 15:30:53 +0100213 fd->invalid_tids = kcalloc(uctxt->expected_count,
214 sizeof(*fd->invalid_tids),
215 GFP_KERNEL);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700216 /*
217 * NOTE: If this is an error, shouldn't we cleanup enry_to_rb?
218 */
219 if (!fd->invalid_tids)
220 return -ENOMEM;
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800221
222 /*
223 * Register MMU notifier callbacks. If the registration
Dean Luick622c2022016-07-28 15:21:21 -0400224 * fails, continue without TID caching for this context.
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800225 */
Dean Luickb85ced92016-07-28 15:21:24 -0400226 ret = hfi1_mmu_rb_register(fd, fd->mm, &tid_rb_ops,
227 dd->pport->hfi1_wq,
228 &fd->handler);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800229 if (ret) {
230 dd_dev_info(dd,
231 "Failed MMU notifier registration %d\n",
232 ret);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800233 ret = 0;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500234 }
235 }
236
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500237 /*
238 * PSM does not have a good way to separate, count, and
239 * effectively enforce a limit on RcvArray entries used by
240 * subctxts (when context sharing is used) when TID caching
241 * is enabled. To help with that, we calculate a per-process
242 * RcvArray entry share and enforce that.
243 * If TID caching is not in use, PSM deals with usage on its
244 * own. In that case, we allow any subctxt to take all of the
245 * entries.
246 *
247 * Make sure that we set the tid counts only after successful
248 * init.
249 */
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500250 spin_lock(&fd->tid_lock);
Dean Luick622c2022016-07-28 15:21:21 -0400251 if (uctxt->subctxt_cnt && fd->handler) {
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500252 u16 remainder;
253
254 fd->tid_limit = uctxt->expected_count / uctxt->subctxt_cnt;
255 remainder = uctxt->expected_count % uctxt->subctxt_cnt;
256 if (remainder && fd->subctxt < remainder)
257 fd->tid_limit++;
258 } else {
259 fd->tid_limit = uctxt->expected_count;
260 }
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500261 spin_unlock(&fd->tid_lock);
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700262
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500263 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500264}
265
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700266void hfi1_user_exp_rcv_grp_free(struct hfi1_ctxtdata *uctxt)
267{
268 struct tid_group *grp, *gptr;
269
270 list_for_each_entry_safe(grp, gptr, &uctxt->tid_group_list.list,
271 list) {
272 list_del_init(&grp->list);
273 kfree(grp);
274 }
275 hfi1_clear_tids(uctxt);
276}
277
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700278void hfi1_user_exp_rcv_free(struct hfi1_filedata *fd)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500279{
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500280 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500281
282 /*
283 * The notifier would have been removed when the process'es mm
284 * was freed.
285 */
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700286 if (fd->handler) {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400287 hfi1_mmu_rb_unregister(fd->handler);
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700288 } else {
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500289 if (!EXP_TID_SET_EMPTY(uctxt->tid_full_list))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400290 unlock_exp_tids(uctxt, &uctxt->tid_full_list, fd);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500291 if (!EXP_TID_SET_EMPTY(uctxt->tid_used_list))
Dean Luicke0b09ac2016-07-28 15:21:20 -0400292 unlock_exp_tids(uctxt, &uctxt->tid_used_list, fd);
Mitko Haralanov3abb33a2016-02-05 11:57:54 -0500293 }
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800294
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700295 kfree(fd->invalid_tids);
296 fd->invalid_tids = NULL;
297
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800298 kfree(fd->entry_to_rb);
Michael J. Ruhl224d71f2017-05-04 05:14:34 -0700299 fd->entry_to_rb = NULL;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500300}
301
Mitko Haralanovb8abe342016-02-05 11:57:51 -0500302/*
303 * Write an "empty" RcvArray entry.
304 * This function exists so the TID registaration code can use it
305 * to write to unused/unneeded entries and still take advantage
306 * of the WC performance improvements. The HFI will ignore this
307 * write to the RcvArray entry.
308 */
309static inline void rcv_array_wc_fill(struct hfi1_devdata *dd, u32 index)
310{
311 /*
312 * Doing the WC fill writes only makes sense if the device is
313 * present and the RcvArray has been mapped as WC memory.
314 */
315 if ((dd->flags & HFI1_PRESENT) && dd->rcvarray_wc)
316 writeq(0, dd->rcvarray_wc + (index * 8));
317}
318
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500319/*
320 * RcvArray entry allocation for Expected Receives is done by the
321 * following algorithm:
322 *
323 * The context keeps 3 lists of groups of RcvArray entries:
324 * 1. List of empty groups - tid_group_list
325 * This list is created during user context creation and
326 * contains elements which describe sets (of 8) of empty
327 * RcvArray entries.
328 * 2. List of partially used groups - tid_used_list
329 * This list contains sets of RcvArray entries which are
330 * not completely used up. Another mapping request could
331 * use some of all of the remaining entries.
332 * 3. List of full groups - tid_full_list
333 * This is the list where sets that are completely used
334 * up go.
335 *
336 * An attempt to optimize the usage of RcvArray entries is
337 * made by finding all sets of physically contiguous pages in a
338 * user's buffer.
339 * These physically contiguous sets are further split into
340 * sizes supported by the receive engine of the HFI. The
341 * resulting sets of pages are stored in struct tid_pageset,
342 * which describes the sets as:
343 * * .count - number of pages in this set
344 * * .idx - starting index into struct page ** array
345 * of this set
346 *
347 * From this point on, the algorithm deals with the page sets
348 * described above. The number of pagesets is divided by the
349 * RcvArray group size to produce the number of full groups
350 * needed.
351 *
352 * Groups from the 3 lists are manipulated using the following
353 * rules:
354 * 1. For each set of 8 pagesets, a complete group from
355 * tid_group_list is taken, programmed, and moved to
356 * the tid_full_list list.
357 * 2. For all remaining pagesets:
358 * 2.1 If the tid_used_list is empty and the tid_group_list
359 * is empty, stop processing pageset and return only
360 * what has been programmed up to this point.
361 * 2.2 If the tid_used_list is empty and the tid_group_list
362 * is not empty, move a group from tid_group_list to
363 * tid_used_list.
364 * 2.3 For each group is tid_used_group, program as much as
365 * can fit into the group. If the group becomes fully
366 * used, move it to tid_full_list.
367 */
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700368int hfi1_user_exp_rcv_setup(struct hfi1_filedata *fd,
369 struct hfi1_tid_info *tinfo)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500370{
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500371 int ret = 0, need_group = 0, pinned;
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500372 struct hfi1_ctxtdata *uctxt = fd->uctxt;
373 struct hfi1_devdata *dd = uctxt->dd;
374 unsigned npages, ngroups, pageidx = 0, pageset_count, npagesets,
375 tididx = 0, mapped, mapped_pages = 0;
376 unsigned long vaddr = tinfo->vaddr;
377 struct page **pages = NULL;
378 u32 *tidlist = NULL;
379 struct tid_pageset *pagesets = NULL;
380
381 /* Get the number of pages the user buffer spans */
382 npages = num_user_pages(vaddr, tinfo->length);
383 if (!npages)
384 return -EINVAL;
385
386 if (npages > uctxt->expected_count) {
387 dd_dev_err(dd, "Expected buffer too big\n");
388 return -EINVAL;
389 }
390
391 /* Verify that access is OK for the user buffer */
392 if (!access_ok(VERIFY_WRITE, (void __user *)vaddr,
393 npages * PAGE_SIZE)) {
394 dd_dev_err(dd, "Fail vaddr %p, %u pages, !access_ok\n",
395 (void *)vaddr, npages);
396 return -EFAULT;
397 }
398
399 pagesets = kcalloc(uctxt->expected_count, sizeof(*pagesets),
400 GFP_KERNEL);
401 if (!pagesets)
402 return -ENOMEM;
403
404 /* Allocate the array of struct page pointers needed for pinning */
405 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
406 if (!pages) {
407 ret = -ENOMEM;
408 goto bail;
409 }
410
411 /*
412 * Pin all the pages of the user buffer. If we can't pin all the
413 * pages, accept the amount pinned so far and program only that.
414 * User space knows how to deal with partially programmed buffers.
415 */
Ira Weiny3faa3d92016-07-28 15:21:19 -0400416 if (!hfi1_can_pin_pages(dd, fd->mm, fd->tid_n_pinned, npages)) {
Mitko Haralanov0ad2d3d2016-04-12 10:46:29 -0700417 ret = -ENOMEM;
418 goto bail;
419 }
420
Ira Weiny3faa3d92016-07-28 15:21:19 -0400421 pinned = hfi1_acquire_user_pages(fd->mm, vaddr, npages, true, pages);
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500422 if (pinned <= 0) {
423 ret = pinned;
424 goto bail;
425 }
Mitko Haralanova7922f72016-03-08 11:15:39 -0800426 fd->tid_n_pinned += npages;
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500427
428 /* Find sets of physically contiguous pages */
429 npagesets = find_phys_blocks(pages, pinned, pagesets);
430
431 /*
432 * We don't need to access this under a lock since tid_used is per
433 * process and the same process cannot be in hfi1_user_exp_rcv_clear()
434 * and hfi1_user_exp_rcv_setup() at the same time.
435 */
436 spin_lock(&fd->tid_lock);
437 if (fd->tid_used + npagesets > fd->tid_limit)
438 pageset_count = fd->tid_limit - fd->tid_used;
439 else
440 pageset_count = npagesets;
441 spin_unlock(&fd->tid_lock);
442
443 if (!pageset_count)
444 goto bail;
445
446 ngroups = pageset_count / dd->rcv_entries.group_size;
447 tidlist = kcalloc(pageset_count, sizeof(*tidlist), GFP_KERNEL);
448 if (!tidlist) {
449 ret = -ENOMEM;
450 goto nomem;
451 }
452
453 tididx = 0;
454
455 /*
456 * From this point on, we are going to be using shared (between master
457 * and subcontexts) context resources. We need to take the lock.
458 */
459 mutex_lock(&uctxt->exp_lock);
460 /*
461 * The first step is to program the RcvArray entries which are complete
462 * groups.
463 */
464 while (ngroups && uctxt->tid_group_list.count) {
465 struct tid_group *grp =
466 tid_group_pop(&uctxt->tid_group_list);
467
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700468 ret = program_rcvarray(fd, vaddr, grp, pagesets,
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500469 pageidx, dd->rcv_entries.group_size,
470 pages, tidlist, &tididx, &mapped);
471 /*
472 * If there was a failure to program the RcvArray
473 * entries for the entire group, reset the grp fields
474 * and add the grp back to the free group list.
475 */
476 if (ret <= 0) {
477 tid_group_add_tail(grp, &uctxt->tid_group_list);
478 hfi1_cdbg(TID,
479 "Failed to program RcvArray group %d", ret);
480 goto unlock;
481 }
482
483 tid_group_add_tail(grp, &uctxt->tid_full_list);
484 ngroups--;
485 pageidx += ret;
486 mapped_pages += mapped;
487 }
488
489 while (pageidx < pageset_count) {
490 struct tid_group *grp, *ptr;
491 /*
492 * If we don't have any partially used tid groups, check
493 * if we have empty groups. If so, take one from there and
494 * put in the partially used list.
495 */
496 if (!uctxt->tid_used_list.count || need_group) {
497 if (!uctxt->tid_group_list.count)
498 goto unlock;
499
500 grp = tid_group_pop(&uctxt->tid_group_list);
501 tid_group_add_tail(grp, &uctxt->tid_used_list);
502 need_group = 0;
503 }
504 /*
505 * There is an optimization opportunity here - instead of
506 * fitting as many page sets as we can, check for a group
507 * later on in the list that could fit all of them.
508 */
509 list_for_each_entry_safe(grp, ptr, &uctxt->tid_used_list.list,
510 list) {
511 unsigned use = min_t(unsigned, pageset_count - pageidx,
512 grp->size - grp->used);
513
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700514 ret = program_rcvarray(fd, vaddr, grp, pagesets,
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500515 pageidx, use, pages, tidlist,
516 &tididx, &mapped);
517 if (ret < 0) {
518 hfi1_cdbg(TID,
519 "Failed to program RcvArray entries %d",
520 ret);
521 ret = -EFAULT;
522 goto unlock;
523 } else if (ret > 0) {
524 if (grp->used == grp->size)
525 tid_group_move(grp,
526 &uctxt->tid_used_list,
527 &uctxt->tid_full_list);
528 pageidx += ret;
529 mapped_pages += mapped;
530 need_group = 0;
531 /* Check if we are done so we break out early */
532 if (pageidx >= pageset_count)
533 break;
534 } else if (WARN_ON(ret == 0)) {
535 /*
536 * If ret is 0, we did not program any entries
537 * into this group, which can only happen if
538 * we've screwed up the accounting somewhere.
539 * Warn and try to continue.
540 */
541 need_group = 1;
542 }
543 }
544 }
545unlock:
546 mutex_unlock(&uctxt->exp_lock);
547nomem:
548 hfi1_cdbg(TID, "total mapped: tidpairs:%u pages:%u (%d)", tididx,
549 mapped_pages, ret);
550 if (tididx) {
551 spin_lock(&fd->tid_lock);
552 fd->tid_used += tididx;
553 spin_unlock(&fd->tid_lock);
554 tinfo->tidcnt = tididx;
555 tinfo->length = mapped_pages * PAGE_SIZE;
556
557 if (copy_to_user((void __user *)(unsigned long)tinfo->tidlist,
558 tidlist, sizeof(tidlist[0]) * tididx)) {
559 /*
560 * On failure to copy to the user level, we need to undo
561 * everything done so far so we don't leak resources.
562 */
563 tinfo->tidlist = (unsigned long)&tidlist;
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700564 hfi1_user_exp_rcv_clear(fd, tinfo);
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500565 tinfo->tidlist = 0;
566 ret = -EFAULT;
567 goto bail;
568 }
569 }
570
571 /*
572 * If not everything was mapped (due to insufficient RcvArray entries,
573 * for example), unpin all unmapped pages so we can pin them nex time.
574 */
Mitko Haralanova7922f72016-03-08 11:15:39 -0800575 if (mapped_pages != pinned) {
Ira Weiny3faa3d92016-07-28 15:21:19 -0400576 hfi1_release_user_pages(fd->mm, &pages[mapped_pages],
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500577 pinned - mapped_pages,
578 false);
Mitko Haralanova7922f72016-03-08 11:15:39 -0800579 fd->tid_n_pinned -= pinned - mapped_pages;
580 }
Mitko Haralanov7e7a436e2016-02-05 11:57:57 -0500581bail:
582 kfree(pagesets);
583 kfree(pages);
584 kfree(tidlist);
585 return ret > 0 ? 0 : ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500586}
587
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700588int hfi1_user_exp_rcv_clear(struct hfi1_filedata *fd,
589 struct hfi1_tid_info *tinfo)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500590{
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500591 int ret = 0;
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500592 struct hfi1_ctxtdata *uctxt = fd->uctxt;
593 u32 *tidinfo;
594 unsigned tididx;
595
Michael J. Ruhldb730892017-04-09 10:16:03 -0700596 if (unlikely(tinfo->tidcnt > fd->tid_used))
597 return -EINVAL;
598
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800599 tidinfo = memdup_user((void __user *)(unsigned long)tinfo->tidlist,
600 sizeof(tidinfo[0]) * tinfo->tidcnt);
601 if (IS_ERR(tidinfo))
602 return PTR_ERR(tidinfo);
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500603
604 mutex_lock(&uctxt->exp_lock);
605 for (tididx = 0; tididx < tinfo->tidcnt; tididx++) {
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700606 ret = unprogram_rcvarray(fd, tidinfo[tididx], NULL);
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500607 if (ret) {
608 hfi1_cdbg(TID, "Failed to unprogram rcv array %d",
609 ret);
610 break;
611 }
612 }
613 spin_lock(&fd->tid_lock);
614 fd->tid_used -= tididx;
615 spin_unlock(&fd->tid_lock);
616 tinfo->tidcnt = tididx;
617 mutex_unlock(&uctxt->exp_lock);
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800618
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500619 kfree(tidinfo);
620 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500621}
622
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700623int hfi1_user_exp_rcv_invalid(struct hfi1_filedata *fd,
624 struct hfi1_tid_info *tinfo)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500625{
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500626 struct hfi1_ctxtdata *uctxt = fd->uctxt;
627 unsigned long *ev = uctxt->dd->events +
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700628 (((uctxt->ctxt - uctxt->dd->first_dyn_alloc_ctxt) *
Mitko Haralanov455d7f12016-02-05 11:57:56 -0500629 HFI1_MAX_SHARED_CTXTS) + fd->subctxt);
630 u32 *array;
631 int ret = 0;
632
633 if (!fd->invalid_tids)
634 return -EINVAL;
635
636 /*
637 * copy_to_user() can sleep, which will leave the invalid_lock
638 * locked and cause the MMU notifier to be blocked on the lock
639 * for a long time.
640 * Copy the data to a local buffer so we can release the lock.
641 */
642 array = kcalloc(uctxt->expected_count, sizeof(*array), GFP_KERNEL);
643 if (!array)
644 return -EFAULT;
645
646 spin_lock(&fd->invalid_lock);
647 if (fd->invalid_tid_idx) {
648 memcpy(array, fd->invalid_tids, sizeof(*array) *
649 fd->invalid_tid_idx);
650 memset(fd->invalid_tids, 0, sizeof(*fd->invalid_tids) *
651 fd->invalid_tid_idx);
652 tinfo->tidcnt = fd->invalid_tid_idx;
653 fd->invalid_tid_idx = 0;
654 /*
655 * Reset the user flag while still holding the lock.
656 * Otherwise, PSM can miss events.
657 */
658 clear_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
659 } else {
660 tinfo->tidcnt = 0;
661 }
662 spin_unlock(&fd->invalid_lock);
663
664 if (tinfo->tidcnt) {
665 if (copy_to_user((void __user *)tinfo->tidlist,
666 array, sizeof(*array) * tinfo->tidcnt))
667 ret = -EFAULT;
668 }
669 kfree(array);
670
671 return ret;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500672}
673
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500674static u32 find_phys_blocks(struct page **pages, unsigned npages,
675 struct tid_pageset *list)
676{
677 unsigned pagecount, pageidx, setcount = 0, i;
678 unsigned long pfn, this_pfn;
679
680 if (!npages)
681 return 0;
682
683 /*
684 * Look for sets of physically contiguous pages in the user buffer.
685 * This will allow us to optimize Expected RcvArray entry usage by
686 * using the bigger supported sizes.
687 */
688 pfn = page_to_pfn(pages[0]);
689 for (pageidx = 0, pagecount = 1, i = 1; i <= npages; i++) {
690 this_pfn = i < npages ? page_to_pfn(pages[i]) : 0;
691
692 /*
693 * If the pfn's are not sequential, pages are not physically
694 * contiguous.
695 */
696 if (this_pfn != ++pfn) {
697 /*
698 * At this point we have to loop over the set of
699 * physically contiguous pages and break them down it
700 * sizes supported by the HW.
701 * There are two main constraints:
702 * 1. The max buffer size is MAX_EXPECTED_BUFFER.
703 * If the total set size is bigger than that
704 * program only a MAX_EXPECTED_BUFFER chunk.
705 * 2. The buffer size has to be a power of two. If
706 * it is not, round down to the closes power of
707 * 2 and program that size.
708 */
709 while (pagecount) {
710 int maxpages = pagecount;
711 u32 bufsize = pagecount * PAGE_SIZE;
712
713 if (bufsize > MAX_EXPECTED_BUFFER)
714 maxpages =
715 MAX_EXPECTED_BUFFER >>
716 PAGE_SHIFT;
717 else if (!is_power_of_2(bufsize))
718 maxpages =
719 rounddown_pow_of_two(bufsize) >>
720 PAGE_SHIFT;
721
722 list[setcount].idx = pageidx;
723 list[setcount].count = maxpages;
724 pagecount -= maxpages;
725 pageidx += maxpages;
726 setcount++;
727 }
728 pageidx = i;
729 pagecount = 1;
730 pfn = this_pfn;
731 } else {
732 pagecount++;
733 }
734 }
735 return setcount;
736}
737
738/**
739 * program_rcvarray() - program an RcvArray group with receive buffers
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700740 * @fd: filedata pointer
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500741 * @vaddr: starting user virtual address
742 * @grp: RcvArray group
743 * @sets: array of struct tid_pageset holding information on physically
744 * contiguous chunks from the user buffer
745 * @start: starting index into sets array
746 * @count: number of struct tid_pageset's to program
747 * @pages: an array of struct page * for the user buffer
748 * @tidlist: the array of u32 elements when the information about the
749 * programmed RcvArray entries is to be encoded.
750 * @tididx: starting offset into tidlist
751 * @pmapped: (output parameter) number of pages programmed into the RcvArray
752 * entries.
753 *
754 * This function will program up to 'count' number of RcvArray entries from the
755 * group 'grp'. To make best use of write-combining writes, the function will
756 * perform writes to the unused RcvArray entries which will be ignored by the
757 * HW. Each RcvArray entry will be programmed with a physically contiguous
758 * buffer chunk from the user's virtual buffer.
759 *
760 * Return:
761 * -EINVAL if the requested count is larger than the size of the group,
762 * -ENOMEM or -EFAULT on error from set_rcvarray_entry(), or
763 * number of RcvArray entries programmed.
764 */
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700765static int program_rcvarray(struct hfi1_filedata *fd, unsigned long vaddr,
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500766 struct tid_group *grp,
767 struct tid_pageset *sets,
768 unsigned start, u16 count, struct page **pages,
769 u32 *tidlist, unsigned *tididx, unsigned *pmapped)
770{
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500771 struct hfi1_ctxtdata *uctxt = fd->uctxt;
772 struct hfi1_devdata *dd = uctxt->dd;
773 u16 idx;
774 u32 tidinfo = 0, rcventry, useidx = 0;
775 int mapped = 0;
776
777 /* Count should never be larger than the group size */
778 if (count > grp->size)
779 return -EINVAL;
780
781 /* Find the first unused entry in the group */
782 for (idx = 0; idx < grp->size; idx++) {
783 if (!(grp->map & (1 << idx))) {
784 useidx = idx;
785 break;
786 }
787 rcv_array_wc_fill(dd, grp->base + idx);
788 }
789
790 idx = 0;
791 while (idx < count) {
792 u16 npages, pageidx, setidx = start + idx;
793 int ret = 0;
794
795 /*
796 * If this entry in the group is used, move to the next one.
797 * If we go past the end of the group, exit the loop.
798 */
799 if (useidx >= grp->size) {
800 break;
801 } else if (grp->map & (1 << useidx)) {
802 rcv_array_wc_fill(dd, grp->base + useidx);
803 useidx++;
804 continue;
805 }
806
807 rcventry = grp->base + useidx;
808 npages = sets[setidx].count;
809 pageidx = sets[setidx].idx;
810
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700811 ret = set_rcvarray_entry(fd, vaddr + (pageidx * PAGE_SIZE),
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500812 rcventry, grp, pages + pageidx,
813 npages);
814 if (ret)
815 return ret;
816 mapped += npages;
817
818 tidinfo = rcventry2tidinfo(rcventry - uctxt->expected_base) |
819 EXP_TID_SET(LEN, npages);
820 tidlist[(*tididx)++] = tidinfo;
821 grp->used++;
822 grp->map |= 1 << useidx++;
823 idx++;
824 }
825
826 /* Fill the rest of the group with "blank" writes */
827 for (; useidx < grp->size; useidx++)
828 rcv_array_wc_fill(dd, grp->base + useidx);
829 *pmapped = mapped;
830 return idx;
831}
832
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700833static int set_rcvarray_entry(struct hfi1_filedata *fd, unsigned long vaddr,
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500834 u32 rcventry, struct tid_group *grp,
835 struct page **pages, unsigned npages)
836{
837 int ret;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500838 struct hfi1_ctxtdata *uctxt = fd->uctxt;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800839 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500840 struct hfi1_devdata *dd = uctxt->dd;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500841 dma_addr_t phys;
842
843 /*
844 * Allocate the node first so we can handle a potential
845 * failure before we've programmed anything.
846 */
847 node = kzalloc(sizeof(*node) + (sizeof(struct page *) * npages),
848 GFP_KERNEL);
849 if (!node)
850 return -ENOMEM;
851
852 phys = pci_map_single(dd->pcidev,
853 __va(page_to_phys(pages[0])),
854 npages * PAGE_SIZE, PCI_DMA_FROMDEVICE);
855 if (dma_mapping_error(&dd->pcidev->dev, phys)) {
856 dd_dev_err(dd, "Failed to DMA map Exp Rcv pages 0x%llx\n",
857 phys);
858 kfree(node);
859 return -EFAULT;
860 }
861
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800862 node->mmu.addr = vaddr;
863 node->mmu.len = npages * PAGE_SIZE;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500864 node->phys = page_to_phys(pages[0]);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500865 node->npages = npages;
866 node->rcventry = rcventry;
867 node->dma_addr = phys;
868 node->grp = grp;
869 node->freed = false;
870 memcpy(node->pages, pages, sizeof(struct page *) * npages);
871
Dean Luick622c2022016-07-28 15:21:21 -0400872 if (!fd->handler)
Dean Luicke0b09ac2016-07-28 15:21:20 -0400873 ret = tid_rb_insert(fd, &node->mmu);
Mitko Haralanov368f2b52016-03-08 11:14:42 -0800874 else
Dean Luicke0b09ac2016-07-28 15:21:20 -0400875 ret = hfi1_mmu_rb_insert(fd->handler, &node->mmu);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500876
877 if (ret) {
878 hfi1_cdbg(TID, "Failed to insert RB node %u 0x%lx, 0x%lx %d",
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800879 node->rcventry, node->mmu.addr, node->phys, ret);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500880 pci_unmap_single(dd->pcidev, phys, npages * PAGE_SIZE,
881 PCI_DMA_FROMDEVICE);
882 kfree(node);
883 return -EFAULT;
884 }
885 hfi1_put_tid(dd, rcventry, PT_EXPECTED, phys, ilog2(npages) + 1);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800886 trace_hfi1_exp_tid_reg(uctxt->ctxt, fd->subctxt, rcventry, npages,
887 node->mmu.addr, node->phys, phys);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500888 return 0;
889}
890
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700891static int unprogram_rcvarray(struct hfi1_filedata *fd, u32 tidinfo,
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500892 struct tid_group **grp)
893{
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500894 struct hfi1_ctxtdata *uctxt = fd->uctxt;
895 struct hfi1_devdata *dd = uctxt->dd;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800896 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500897 u8 tidctrl = EXP_TID_GET(tidinfo, CTRL);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800898 u32 tididx = EXP_TID_GET(tidinfo, IDX) << 1, rcventry;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500899
900 if (tididx >= uctxt->expected_count) {
901 dd_dev_err(dd, "Invalid RcvArray entry (%u) index for ctxt %u\n",
902 tididx, uctxt->ctxt);
903 return -EINVAL;
904 }
905
906 if (tidctrl == 0x3)
907 return -EINVAL;
908
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800909 rcventry = tididx + (tidctrl - 1);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500910
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800911 node = fd->entry_to_rb[rcventry];
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800912 if (!node || node->rcventry != (uctxt->expected_base + rcventry))
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500913 return -EBADF;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800914
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500915 if (grp)
916 *grp = node->grp;
Ira Weiny2677a762016-07-28 15:21:26 -0400917
918 if (!fd->handler)
919 cacheless_tid_rb_remove(fd, node);
920 else
921 hfi1_mmu_rb_remove(fd->handler, &node->mmu);
922
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500923 return 0;
924}
925
Ira Weiny5ed3b152016-07-28 12:27:32 -0400926static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500927{
928 struct hfi1_ctxtdata *uctxt = fd->uctxt;
929 struct hfi1_devdata *dd = uctxt->dd;
930
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500931 trace_hfi1_exp_tid_unreg(uctxt->ctxt, fd->subctxt, node->rcventry,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800932 node->npages, node->mmu.addr, node->phys,
Mitko Haralanov0b091fb2016-02-05 11:57:58 -0500933 node->dma_addr);
934
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500935 hfi1_put_tid(dd, node->rcventry, PT_INVALID, 0, 0);
936 /*
937 * Make sure device has seen the write before we unpin the
938 * pages.
939 */
940 flush_wc();
941
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800942 pci_unmap_single(dd->pcidev, node->dma_addr, node->mmu.len,
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500943 PCI_DMA_FROMDEVICE);
Ira Weiny3faa3d92016-07-28 15:21:19 -0400944 hfi1_release_user_pages(fd->mm, node->pages, node->npages, true);
Mitko Haralanova7922f72016-03-08 11:15:39 -0800945 fd->tid_n_pinned -= node->npages;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500946
947 node->grp->used--;
948 node->grp->map &= ~(1 << (node->rcventry - node->grp->base));
949
950 if (node->grp->used == node->grp->size - 1)
951 tid_group_move(node->grp, &uctxt->tid_full_list,
952 &uctxt->tid_used_list);
953 else if (!node->grp->used)
954 tid_group_move(node->grp, &uctxt->tid_used_list,
955 &uctxt->tid_group_list);
956 kfree(node);
957}
958
Ira Weiny2677a762016-07-28 15:21:26 -0400959/*
960 * As a simple helper for hfi1_user_exp_rcv_free, this function deals with
961 * clearing nodes in the non-cached case.
962 */
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500963static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt,
Dean Luicke0b09ac2016-07-28 15:21:20 -0400964 struct exp_tid_set *set,
965 struct hfi1_filedata *fd)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500966{
967 struct tid_group *grp, *ptr;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500968 int i;
969
970 list_for_each_entry_safe(grp, ptr, &set->list, list) {
971 list_del_init(&grp->list);
972
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500973 for (i = 0; i < grp->size; i++) {
974 if (grp->map & (1 << i)) {
975 u16 rcventry = grp->base + i;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800976 struct tid_rb_node *node;
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500977
Mitko Haralanova92ba6d2016-02-03 14:34:41 -0800978 node = fd->entry_to_rb[rcventry -
979 uctxt->expected_base];
980 if (!node || node->rcventry != rcventry)
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500981 continue;
Ira Weiny2677a762016-07-28 15:21:26 -0400982
983 cacheless_tid_rb_remove(fd, node);
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500984 }
985 }
Mitko Haralanovf88e0c82016-02-05 11:57:52 -0500986 }
987}
988
Ira Weiny2677a762016-07-28 15:21:26 -0400989/*
990 * Always return 0 from this function. A non-zero return indicates that the
991 * remove operation will be called and that memory should be unpinned.
992 * However, the driver cannot unpin out from under PSM. Instead, retain the
993 * memory (by returning 0) and inform PSM that the memory is going away. PSM
994 * will call back later when it has removed the memory from its list.
995 */
Dean Luicke0b09ac2016-07-28 15:21:20 -0400996static int tid_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -0500997{
Dean Luicke0b09ac2016-07-28 15:21:20 -0400998 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800999 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
1000 struct tid_rb_node *node =
1001 container_of(mnode, struct tid_rb_node, mmu);
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001002
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001003 if (node->freed)
1004 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001005
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001006 trace_hfi1_exp_tid_inval(uctxt->ctxt, fdata->subctxt, node->mmu.addr,
1007 node->rcventry, node->npages, node->dma_addr);
1008 node->freed = true;
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001009
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001010 spin_lock(&fdata->invalid_lock);
1011 if (fdata->invalid_tid_idx < uctxt->expected_count) {
1012 fdata->invalid_tids[fdata->invalid_tid_idx] =
1013 rcventry2tidinfo(node->rcventry - uctxt->expected_base);
1014 fdata->invalid_tids[fdata->invalid_tid_idx] |=
1015 EXP_TID_SET(LEN, node->npages);
1016 if (!fdata->invalid_tid_idx) {
1017 unsigned long *ev;
Mitko Haralanov0b091fb2016-02-05 11:57:58 -05001018
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001019 /*
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001020 * hfi1_set_uevent_bits() sets a user event flag
1021 * for all processes. Because calling into the
1022 * driver to process TID cache invalidations is
1023 * expensive and TID cache invalidations are
1024 * handled on a per-process basis, we can
1025 * optimize this to set the flag only for the
1026 * process in question.
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001027 */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001028 ev = uctxt->dd->events +
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001029 (((uctxt->ctxt - uctxt->dd->first_dyn_alloc_ctxt) *
1030 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001031 set_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001032 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001033 fdata->invalid_tid_idx++;
Mitko Haralanovb5eb3b22016-02-05 11:57:55 -05001034 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001035 spin_unlock(&fdata->invalid_lock);
1036 return 0;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001037}
1038
Dean Luicke0b09ac2016-07-28 15:21:20 -04001039static int tid_rb_insert(void *arg, struct mmu_rb_node *node)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001040{
Dean Luicke0b09ac2016-07-28 15:21:20 -04001041 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001042 struct tid_rb_node *tnode =
1043 container_of(node, struct tid_rb_node, mmu);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001044 u32 base = fdata->uctxt->expected_base;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001045
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001046 fdata->entry_to_rb[tnode->rcventry - base] = tnode;
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001047 return 0;
1048}
1049
Ira Weiny2677a762016-07-28 15:21:26 -04001050static void cacheless_tid_rb_remove(struct hfi1_filedata *fdata,
1051 struct tid_rb_node *tnode)
1052{
1053 u32 base = fdata->uctxt->expected_base;
1054
1055 fdata->entry_to_rb[tnode->rcventry - base] = NULL;
1056 clear_tid_node(fdata, tnode);
1057}
1058
Dean Luick082b3532016-07-28 15:21:25 -04001059static void tid_rb_remove(void *arg, struct mmu_rb_node *node)
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001060{
Dean Luicke0b09ac2016-07-28 15:21:20 -04001061 struct hfi1_filedata *fdata = arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001062 struct tid_rb_node *tnode =
1063 container_of(node, struct tid_rb_node, mmu);
Mitko Haralanovf727a0c2016-02-05 11:57:46 -05001064
Ira Weiny2677a762016-07-28 15:21:26 -04001065 cacheless_tid_rb_remove(fdata, tnode);
Mitko Haralanova92ba6d2016-02-03 14:34:41 -08001066}