2 * Copyright(c) 2015, 2016 Intel Corporation.
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.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
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.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
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
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.
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.
49 #include "user_exp_rcv.h"
54 struct list_head list;
62 struct mmu_rb_node mmu;
64 struct tid_group *grp;
69 struct page *pages[0];
77 #define EXP_TID_SET_EMPTY(set) (set.count == 0 && list_empty(&set.list))
79 #define num_user_pages(vaddr, len) \
80 (1 + (((((unsigned long)(vaddr) + \
81 (unsigned long)(len) - 1) & PAGE_MASK) - \
82 ((unsigned long)vaddr & PAGE_MASK)) >> PAGE_SHIFT))
84 static void unlock_exp_tids(struct hfi1_ctxtdata *, struct exp_tid_set *,
85 struct hfi1_filedata *);
86 static u32 find_phys_blocks(struct page **, unsigned, struct tid_pageset *);
87 static int set_rcvarray_entry(struct file *, unsigned long, u32,
88 struct tid_group *, struct page **, unsigned);
89 static int tid_rb_insert(void *, struct mmu_rb_node *);
90 static void cacheless_tid_rb_remove(struct hfi1_filedata *fdata,
91 struct tid_rb_node *tnode);
92 static void tid_rb_remove(void *, struct mmu_rb_node *);
93 static int tid_rb_invalidate(void *, struct mmu_rb_node *);
94 static int program_rcvarray(struct file *, unsigned long, struct tid_group *,
95 struct tid_pageset *, unsigned, u16, struct page **,
96 u32 *, unsigned *, unsigned *);
97 static int unprogram_rcvarray(struct file *, u32, struct tid_group **);
98 static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node);
100 static struct mmu_rb_ops tid_rb_ops = {
101 .insert = tid_rb_insert,
102 .remove = tid_rb_remove,
103 .invalidate = tid_rb_invalidate
106 static inline u32 rcventry2tidinfo(u32 rcventry)
108 u32 pair = rcventry & ~0x1;
110 return EXP_TID_SET(IDX, pair >> 1) |
111 EXP_TID_SET(CTRL, 1 << (rcventry - pair));
114 static inline void exp_tid_group_init(struct exp_tid_set *set)
116 INIT_LIST_HEAD(&set->list);
120 static inline void tid_group_remove(struct tid_group *grp,
121 struct exp_tid_set *set)
123 list_del_init(&grp->list);
127 static inline void tid_group_add_tail(struct tid_group *grp,
128 struct exp_tid_set *set)
130 list_add_tail(&grp->list, &set->list);
134 static inline struct tid_group *tid_group_pop(struct exp_tid_set *set)
136 struct tid_group *grp =
137 list_first_entry(&set->list, struct tid_group, list);
138 list_del_init(&grp->list);
143 static inline void tid_group_move(struct tid_group *group,
144 struct exp_tid_set *s1,
145 struct exp_tid_set *s2)
147 tid_group_remove(group, s1);
148 tid_group_add_tail(group, s2);
152 * Initialize context and file private data needed for Expected
153 * receive caching. This needs to be done after the context has
154 * been configured with the eager/expected RcvEntry counts.
156 int hfi1_user_exp_rcv_init(struct file *fp)
158 struct hfi1_filedata *fd = fp->private_data;
159 struct hfi1_ctxtdata *uctxt = fd->uctxt;
160 struct hfi1_devdata *dd = uctxt->dd;
164 spin_lock_init(&fd->tid_lock);
165 spin_lock_init(&fd->invalid_lock);
167 if (!uctxt->subctxt_cnt || !fd->subctxt) {
168 exp_tid_group_init(&uctxt->tid_group_list);
169 exp_tid_group_init(&uctxt->tid_used_list);
170 exp_tid_group_init(&uctxt->tid_full_list);
172 tidbase = uctxt->expected_base;
173 for (i = 0; i < uctxt->expected_count /
174 dd->rcv_entries.group_size; i++) {
175 struct tid_group *grp;
177 grp = kzalloc(sizeof(*grp), GFP_KERNEL);
180 * If we fail here, the groups already
181 * allocated will be freed by the close
187 grp->size = dd->rcv_entries.group_size;
189 tid_group_add_tail(grp, &uctxt->tid_group_list);
190 tidbase += dd->rcv_entries.group_size;
194 fd->entry_to_rb = kcalloc(uctxt->expected_count,
195 sizeof(struct rb_node *),
197 if (!fd->entry_to_rb)
200 if (!HFI1_CAP_UGET_MASK(uctxt->flags, TID_UNMAP)) {
201 fd->invalid_tid_idx = 0;
202 fd->invalid_tids = kzalloc(uctxt->expected_count *
203 sizeof(u32), GFP_KERNEL);
204 if (!fd->invalid_tids) {
210 * Register MMU notifier callbacks. If the registration
211 * fails, continue without TID caching for this context.
213 ret = hfi1_mmu_rb_register(fd, fd->mm, &tid_rb_ops,
218 "Failed MMU notifier registration %d\n",
225 * PSM does not have a good way to separate, count, and
226 * effectively enforce a limit on RcvArray entries used by
227 * subctxts (when context sharing is used) when TID caching
228 * is enabled. To help with that, we calculate a per-process
229 * RcvArray entry share and enforce that.
230 * If TID caching is not in use, PSM deals with usage on its
231 * own. In that case, we allow any subctxt to take all of the
234 * Make sure that we set the tid counts only after successful
237 spin_lock(&fd->tid_lock);
238 if (uctxt->subctxt_cnt && fd->handler) {
241 fd->tid_limit = uctxt->expected_count / uctxt->subctxt_cnt;
242 remainder = uctxt->expected_count % uctxt->subctxt_cnt;
243 if (remainder && fd->subctxt < remainder)
246 fd->tid_limit = uctxt->expected_count;
248 spin_unlock(&fd->tid_lock);
253 void hfi1_user_exp_rcv_grp_free(struct hfi1_ctxtdata *uctxt)
255 struct tid_group *grp, *gptr;
257 list_for_each_entry_safe(grp, gptr, &uctxt->tid_group_list.list,
259 list_del_init(&grp->list);
262 hfi1_clear_tids(uctxt);
265 int hfi1_user_exp_rcv_free(struct hfi1_filedata *fd)
267 struct hfi1_ctxtdata *uctxt = fd->uctxt;
270 * The notifier would have been removed when the process'es mm
274 hfi1_mmu_rb_unregister(fd->handler);
276 if (!EXP_TID_SET_EMPTY(uctxt->tid_full_list))
277 unlock_exp_tids(uctxt, &uctxt->tid_full_list, fd);
278 if (!EXP_TID_SET_EMPTY(uctxt->tid_used_list))
279 unlock_exp_tids(uctxt, &uctxt->tid_used_list, fd);
282 kfree(fd->invalid_tids);
283 fd->invalid_tids = NULL;
285 kfree(fd->entry_to_rb);
286 fd->entry_to_rb = NULL;
291 * Write an "empty" RcvArray entry.
292 * This function exists so the TID registaration code can use it
293 * to write to unused/unneeded entries and still take advantage
294 * of the WC performance improvements. The HFI will ignore this
295 * write to the RcvArray entry.
297 static inline void rcv_array_wc_fill(struct hfi1_devdata *dd, u32 index)
300 * Doing the WC fill writes only makes sense if the device is
301 * present and the RcvArray has been mapped as WC memory.
303 if ((dd->flags & HFI1_PRESENT) && dd->rcvarray_wc)
304 writeq(0, dd->rcvarray_wc + (index * 8));
308 * RcvArray entry allocation for Expected Receives is done by the
309 * following algorithm:
311 * The context keeps 3 lists of groups of RcvArray entries:
312 * 1. List of empty groups - tid_group_list
313 * This list is created during user context creation and
314 * contains elements which describe sets (of 8) of empty
316 * 2. List of partially used groups - tid_used_list
317 * This list contains sets of RcvArray entries which are
318 * not completely used up. Another mapping request could
319 * use some of all of the remaining entries.
320 * 3. List of full groups - tid_full_list
321 * This is the list where sets that are completely used
324 * An attempt to optimize the usage of RcvArray entries is
325 * made by finding all sets of physically contiguous pages in a
327 * These physically contiguous sets are further split into
328 * sizes supported by the receive engine of the HFI. The
329 * resulting sets of pages are stored in struct tid_pageset,
330 * which describes the sets as:
331 * * .count - number of pages in this set
332 * * .idx - starting index into struct page ** array
335 * From this point on, the algorithm deals with the page sets
336 * described above. The number of pagesets is divided by the
337 * RcvArray group size to produce the number of full groups
340 * Groups from the 3 lists are manipulated using the following
342 * 1. For each set of 8 pagesets, a complete group from
343 * tid_group_list is taken, programmed, and moved to
344 * the tid_full_list list.
345 * 2. For all remaining pagesets:
346 * 2.1 If the tid_used_list is empty and the tid_group_list
347 * is empty, stop processing pageset and return only
348 * what has been programmed up to this point.
349 * 2.2 If the tid_used_list is empty and the tid_group_list
350 * is not empty, move a group from tid_group_list to
352 * 2.3 For each group is tid_used_group, program as much as
353 * can fit into the group. If the group becomes fully
354 * used, move it to tid_full_list.
356 int hfi1_user_exp_rcv_setup(struct file *fp, struct hfi1_tid_info *tinfo)
358 int ret = 0, need_group = 0, pinned;
359 struct hfi1_filedata *fd = fp->private_data;
360 struct hfi1_ctxtdata *uctxt = fd->uctxt;
361 struct hfi1_devdata *dd = uctxt->dd;
362 unsigned npages, ngroups, pageidx = 0, pageset_count, npagesets,
363 tididx = 0, mapped, mapped_pages = 0;
364 unsigned long vaddr = tinfo->vaddr;
365 struct page **pages = NULL;
367 struct tid_pageset *pagesets = NULL;
369 /* Get the number of pages the user buffer spans */
370 npages = num_user_pages(vaddr, tinfo->length);
374 if (npages > uctxt->expected_count) {
375 dd_dev_err(dd, "Expected buffer too big\n");
379 /* Verify that access is OK for the user buffer */
380 if (!access_ok(VERIFY_WRITE, (void __user *)vaddr,
381 npages * PAGE_SIZE)) {
382 dd_dev_err(dd, "Fail vaddr %p, %u pages, !access_ok\n",
383 (void *)vaddr, npages);
387 pagesets = kcalloc(uctxt->expected_count, sizeof(*pagesets),
392 /* Allocate the array of struct page pointers needed for pinning */
393 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
400 * Pin all the pages of the user buffer. If we can't pin all the
401 * pages, accept the amount pinned so far and program only that.
402 * User space knows how to deal with partially programmed buffers.
404 if (!hfi1_can_pin_pages(dd, fd->mm, fd->tid_n_pinned, npages)) {
409 pinned = hfi1_acquire_user_pages(fd->mm, vaddr, npages, true, pages);
414 fd->tid_n_pinned += npages;
416 /* Find sets of physically contiguous pages */
417 npagesets = find_phys_blocks(pages, pinned, pagesets);
420 * We don't need to access this under a lock since tid_used is per
421 * process and the same process cannot be in hfi1_user_exp_rcv_clear()
422 * and hfi1_user_exp_rcv_setup() at the same time.
424 spin_lock(&fd->tid_lock);
425 if (fd->tid_used + npagesets > fd->tid_limit)
426 pageset_count = fd->tid_limit - fd->tid_used;
428 pageset_count = npagesets;
429 spin_unlock(&fd->tid_lock);
434 ngroups = pageset_count / dd->rcv_entries.group_size;
435 tidlist = kcalloc(pageset_count, sizeof(*tidlist), GFP_KERNEL);
444 * From this point on, we are going to be using shared (between master
445 * and subcontexts) context resources. We need to take the lock.
447 mutex_lock(&uctxt->exp_lock);
449 * The first step is to program the RcvArray entries which are complete
452 while (ngroups && uctxt->tid_group_list.count) {
453 struct tid_group *grp =
454 tid_group_pop(&uctxt->tid_group_list);
456 ret = program_rcvarray(fp, vaddr, grp, pagesets,
457 pageidx, dd->rcv_entries.group_size,
458 pages, tidlist, &tididx, &mapped);
460 * If there was a failure to program the RcvArray
461 * entries for the entire group, reset the grp fields
462 * and add the grp back to the free group list.
465 tid_group_add_tail(grp, &uctxt->tid_group_list);
467 "Failed to program RcvArray group %d", ret);
471 tid_group_add_tail(grp, &uctxt->tid_full_list);
474 mapped_pages += mapped;
477 while (pageidx < pageset_count) {
478 struct tid_group *grp, *ptr;
480 * If we don't have any partially used tid groups, check
481 * if we have empty groups. If so, take one from there and
482 * put in the partially used list.
484 if (!uctxt->tid_used_list.count || need_group) {
485 if (!uctxt->tid_group_list.count)
488 grp = tid_group_pop(&uctxt->tid_group_list);
489 tid_group_add_tail(grp, &uctxt->tid_used_list);
493 * There is an optimization opportunity here - instead of
494 * fitting as many page sets as we can, check for a group
495 * later on in the list that could fit all of them.
497 list_for_each_entry_safe(grp, ptr, &uctxt->tid_used_list.list,
499 unsigned use = min_t(unsigned, pageset_count - pageidx,
500 grp->size - grp->used);
502 ret = program_rcvarray(fp, vaddr, grp, pagesets,
503 pageidx, use, pages, tidlist,
507 "Failed to program RcvArray entries %d",
511 } else if (ret > 0) {
512 if (grp->used == grp->size)
514 &uctxt->tid_used_list,
515 &uctxt->tid_full_list);
517 mapped_pages += mapped;
519 /* Check if we are done so we break out early */
520 if (pageidx >= pageset_count)
522 } else if (WARN_ON(ret == 0)) {
524 * If ret is 0, we did not program any entries
525 * into this group, which can only happen if
526 * we've screwed up the accounting somewhere.
527 * Warn and try to continue.
534 mutex_unlock(&uctxt->exp_lock);
536 hfi1_cdbg(TID, "total mapped: tidpairs:%u pages:%u (%d)", tididx,
539 spin_lock(&fd->tid_lock);
540 fd->tid_used += tididx;
541 spin_unlock(&fd->tid_lock);
542 tinfo->tidcnt = tididx;
543 tinfo->length = mapped_pages * PAGE_SIZE;
545 if (copy_to_user((void __user *)(unsigned long)tinfo->tidlist,
546 tidlist, sizeof(tidlist[0]) * tididx)) {
548 * On failure to copy to the user level, we need to undo
549 * everything done so far so we don't leak resources.
551 tinfo->tidlist = (unsigned long)&tidlist;
552 hfi1_user_exp_rcv_clear(fp, tinfo);
560 * If not everything was mapped (due to insufficient RcvArray entries,
561 * for example), unpin all unmapped pages so we can pin them nex time.
563 if (mapped_pages != pinned) {
564 hfi1_release_user_pages(fd->mm, &pages[mapped_pages],
565 pinned - mapped_pages,
567 fd->tid_n_pinned -= pinned - mapped_pages;
573 return ret > 0 ? 0 : ret;
576 int hfi1_user_exp_rcv_clear(struct file *fp, struct hfi1_tid_info *tinfo)
579 struct hfi1_filedata *fd = fp->private_data;
580 struct hfi1_ctxtdata *uctxt = fd->uctxt;
584 tidinfo = kcalloc(tinfo->tidcnt, sizeof(*tidinfo), GFP_KERNEL);
588 if (copy_from_user(tidinfo, (void __user *)(unsigned long)
589 tinfo->tidlist, sizeof(tidinfo[0]) *
595 mutex_lock(&uctxt->exp_lock);
596 for (tididx = 0; tididx < tinfo->tidcnt; tididx++) {
597 ret = unprogram_rcvarray(fp, tidinfo[tididx], NULL);
599 hfi1_cdbg(TID, "Failed to unprogram rcv array %d",
604 spin_lock(&fd->tid_lock);
605 fd->tid_used -= tididx;
606 spin_unlock(&fd->tid_lock);
607 tinfo->tidcnt = tididx;
608 mutex_unlock(&uctxt->exp_lock);
614 int hfi1_user_exp_rcv_invalid(struct file *fp, struct hfi1_tid_info *tinfo)
616 struct hfi1_filedata *fd = fp->private_data;
617 struct hfi1_ctxtdata *uctxt = fd->uctxt;
618 unsigned long *ev = uctxt->dd->events +
619 (((uctxt->ctxt - uctxt->dd->first_user_ctxt) *
620 HFI1_MAX_SHARED_CTXTS) + fd->subctxt);
624 if (!fd->invalid_tids)
628 * copy_to_user() can sleep, which will leave the invalid_lock
629 * locked and cause the MMU notifier to be blocked on the lock
631 * Copy the data to a local buffer so we can release the lock.
633 array = kcalloc(uctxt->expected_count, sizeof(*array), GFP_KERNEL);
637 spin_lock(&fd->invalid_lock);
638 if (fd->invalid_tid_idx) {
639 memcpy(array, fd->invalid_tids, sizeof(*array) *
640 fd->invalid_tid_idx);
641 memset(fd->invalid_tids, 0, sizeof(*fd->invalid_tids) *
642 fd->invalid_tid_idx);
643 tinfo->tidcnt = fd->invalid_tid_idx;
644 fd->invalid_tid_idx = 0;
646 * Reset the user flag while still holding the lock.
647 * Otherwise, PSM can miss events.
649 clear_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
653 spin_unlock(&fd->invalid_lock);
656 if (copy_to_user((void __user *)tinfo->tidlist,
657 array, sizeof(*array) * tinfo->tidcnt))
665 static u32 find_phys_blocks(struct page **pages, unsigned npages,
666 struct tid_pageset *list)
668 unsigned pagecount, pageidx, setcount = 0, i;
669 unsigned long pfn, this_pfn;
675 * Look for sets of physically contiguous pages in the user buffer.
676 * This will allow us to optimize Expected RcvArray entry usage by
677 * using the bigger supported sizes.
679 pfn = page_to_pfn(pages[0]);
680 for (pageidx = 0, pagecount = 1, i = 1; i <= npages; i++) {
681 this_pfn = i < npages ? page_to_pfn(pages[i]) : 0;
684 * If the pfn's are not sequential, pages are not physically
687 if (this_pfn != ++pfn) {
689 * At this point we have to loop over the set of
690 * physically contiguous pages and break them down it
691 * sizes supported by the HW.
692 * There are two main constraints:
693 * 1. The max buffer size is MAX_EXPECTED_BUFFER.
694 * If the total set size is bigger than that
695 * program only a MAX_EXPECTED_BUFFER chunk.
696 * 2. The buffer size has to be a power of two. If
697 * it is not, round down to the closes power of
698 * 2 and program that size.
701 int maxpages = pagecount;
702 u32 bufsize = pagecount * PAGE_SIZE;
704 if (bufsize > MAX_EXPECTED_BUFFER)
706 MAX_EXPECTED_BUFFER >>
708 else if (!is_power_of_2(bufsize))
710 rounddown_pow_of_two(bufsize) >>
713 list[setcount].idx = pageidx;
714 list[setcount].count = maxpages;
715 pagecount -= maxpages;
730 * program_rcvarray() - program an RcvArray group with receive buffers
732 * @vaddr: starting user virtual address
733 * @grp: RcvArray group
734 * @sets: array of struct tid_pageset holding information on physically
735 * contiguous chunks from the user buffer
736 * @start: starting index into sets array
737 * @count: number of struct tid_pageset's to program
738 * @pages: an array of struct page * for the user buffer
739 * @tidlist: the array of u32 elements when the information about the
740 * programmed RcvArray entries is to be encoded.
741 * @tididx: starting offset into tidlist
742 * @pmapped: (output parameter) number of pages programmed into the RcvArray
745 * This function will program up to 'count' number of RcvArray entries from the
746 * group 'grp'. To make best use of write-combining writes, the function will
747 * perform writes to the unused RcvArray entries which will be ignored by the
748 * HW. Each RcvArray entry will be programmed with a physically contiguous
749 * buffer chunk from the user's virtual buffer.
752 * -EINVAL if the requested count is larger than the size of the group,
753 * -ENOMEM or -EFAULT on error from set_rcvarray_entry(), or
754 * number of RcvArray entries programmed.
756 static int program_rcvarray(struct file *fp, unsigned long vaddr,
757 struct tid_group *grp,
758 struct tid_pageset *sets,
759 unsigned start, u16 count, struct page **pages,
760 u32 *tidlist, unsigned *tididx, unsigned *pmapped)
762 struct hfi1_filedata *fd = fp->private_data;
763 struct hfi1_ctxtdata *uctxt = fd->uctxt;
764 struct hfi1_devdata *dd = uctxt->dd;
766 u32 tidinfo = 0, rcventry, useidx = 0;
769 /* Count should never be larger than the group size */
770 if (count > grp->size)
773 /* Find the first unused entry in the group */
774 for (idx = 0; idx < grp->size; idx++) {
775 if (!(grp->map & (1 << idx))) {
779 rcv_array_wc_fill(dd, grp->base + idx);
783 while (idx < count) {
784 u16 npages, pageidx, setidx = start + idx;
788 * If this entry in the group is used, move to the next one.
789 * If we go past the end of the group, exit the loop.
791 if (useidx >= grp->size) {
793 } else if (grp->map & (1 << useidx)) {
794 rcv_array_wc_fill(dd, grp->base + useidx);
799 rcventry = grp->base + useidx;
800 npages = sets[setidx].count;
801 pageidx = sets[setidx].idx;
803 ret = set_rcvarray_entry(fp, vaddr + (pageidx * PAGE_SIZE),
804 rcventry, grp, pages + pageidx,
810 tidinfo = rcventry2tidinfo(rcventry - uctxt->expected_base) |
811 EXP_TID_SET(LEN, npages);
812 tidlist[(*tididx)++] = tidinfo;
814 grp->map |= 1 << useidx++;
818 /* Fill the rest of the group with "blank" writes */
819 for (; useidx < grp->size; useidx++)
820 rcv_array_wc_fill(dd, grp->base + useidx);
825 static int set_rcvarray_entry(struct file *fp, unsigned long vaddr,
826 u32 rcventry, struct tid_group *grp,
827 struct page **pages, unsigned npages)
830 struct hfi1_filedata *fd = fp->private_data;
831 struct hfi1_ctxtdata *uctxt = fd->uctxt;
832 struct tid_rb_node *node;
833 struct hfi1_devdata *dd = uctxt->dd;
837 * Allocate the node first so we can handle a potential
838 * failure before we've programmed anything.
840 node = kzalloc(sizeof(*node) + (sizeof(struct page *) * npages),
845 phys = pci_map_single(dd->pcidev,
846 __va(page_to_phys(pages[0])),
847 npages * PAGE_SIZE, PCI_DMA_FROMDEVICE);
848 if (dma_mapping_error(&dd->pcidev->dev, phys)) {
849 dd_dev_err(dd, "Failed to DMA map Exp Rcv pages 0x%llx\n",
855 node->mmu.addr = vaddr;
856 node->mmu.len = npages * PAGE_SIZE;
857 node->phys = page_to_phys(pages[0]);
858 node->npages = npages;
859 node->rcventry = rcventry;
860 node->dma_addr = phys;
863 memcpy(node->pages, pages, sizeof(struct page *) * npages);
866 ret = tid_rb_insert(fd, &node->mmu);
868 ret = hfi1_mmu_rb_insert(fd->handler, &node->mmu);
871 hfi1_cdbg(TID, "Failed to insert RB node %u 0x%lx, 0x%lx %d",
872 node->rcventry, node->mmu.addr, node->phys, ret);
873 pci_unmap_single(dd->pcidev, phys, npages * PAGE_SIZE,
878 hfi1_put_tid(dd, rcventry, PT_EXPECTED, phys, ilog2(npages) + 1);
879 trace_hfi1_exp_tid_reg(uctxt->ctxt, fd->subctxt, rcventry, npages,
880 node->mmu.addr, node->phys, phys);
884 static int unprogram_rcvarray(struct file *fp, u32 tidinfo,
885 struct tid_group **grp)
887 struct hfi1_filedata *fd = fp->private_data;
888 struct hfi1_ctxtdata *uctxt = fd->uctxt;
889 struct hfi1_devdata *dd = uctxt->dd;
890 struct tid_rb_node *node;
891 u8 tidctrl = EXP_TID_GET(tidinfo, CTRL);
892 u32 tididx = EXP_TID_GET(tidinfo, IDX) << 1, rcventry;
894 if (tididx >= uctxt->expected_count) {
895 dd_dev_err(dd, "Invalid RcvArray entry (%u) index for ctxt %u\n",
896 tididx, uctxt->ctxt);
903 rcventry = tididx + (tidctrl - 1);
905 node = fd->entry_to_rb[rcventry];
906 if (!node || node->rcventry != (uctxt->expected_base + rcventry))
913 cacheless_tid_rb_remove(fd, node);
915 hfi1_mmu_rb_remove(fd->handler, &node->mmu);
920 static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node)
922 struct hfi1_ctxtdata *uctxt = fd->uctxt;
923 struct hfi1_devdata *dd = uctxt->dd;
925 trace_hfi1_exp_tid_unreg(uctxt->ctxt, fd->subctxt, node->rcventry,
926 node->npages, node->mmu.addr, node->phys,
929 hfi1_put_tid(dd, node->rcventry, PT_INVALID, 0, 0);
931 * Make sure device has seen the write before we unpin the
936 pci_unmap_single(dd->pcidev, node->dma_addr, node->mmu.len,
938 hfi1_release_user_pages(fd->mm, node->pages, node->npages, true);
939 fd->tid_n_pinned -= node->npages;
942 node->grp->map &= ~(1 << (node->rcventry - node->grp->base));
944 if (node->grp->used == node->grp->size - 1)
945 tid_group_move(node->grp, &uctxt->tid_full_list,
946 &uctxt->tid_used_list);
947 else if (!node->grp->used)
948 tid_group_move(node->grp, &uctxt->tid_used_list,
949 &uctxt->tid_group_list);
954 * As a simple helper for hfi1_user_exp_rcv_free, this function deals with
955 * clearing nodes in the non-cached case.
957 static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt,
958 struct exp_tid_set *set,
959 struct hfi1_filedata *fd)
961 struct tid_group *grp, *ptr;
964 list_for_each_entry_safe(grp, ptr, &set->list, list) {
965 list_del_init(&grp->list);
967 for (i = 0; i < grp->size; i++) {
968 if (grp->map & (1 << i)) {
969 u16 rcventry = grp->base + i;
970 struct tid_rb_node *node;
972 node = fd->entry_to_rb[rcventry -
973 uctxt->expected_base];
974 if (!node || node->rcventry != rcventry)
977 cacheless_tid_rb_remove(fd, node);
984 * Always return 0 from this function. A non-zero return indicates that the
985 * remove operation will be called and that memory should be unpinned.
986 * However, the driver cannot unpin out from under PSM. Instead, retain the
987 * memory (by returning 0) and inform PSM that the memory is going away. PSM
988 * will call back later when it has removed the memory from its list.
990 static int tid_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
992 struct hfi1_filedata *fdata = arg;
993 struct hfi1_ctxtdata *uctxt = fdata->uctxt;
994 struct tid_rb_node *node =
995 container_of(mnode, struct tid_rb_node, mmu);
1000 trace_hfi1_exp_tid_inval(uctxt->ctxt, fdata->subctxt, node->mmu.addr,
1001 node->rcventry, node->npages, node->dma_addr);
1004 spin_lock(&fdata->invalid_lock);
1005 if (fdata->invalid_tid_idx < uctxt->expected_count) {
1006 fdata->invalid_tids[fdata->invalid_tid_idx] =
1007 rcventry2tidinfo(node->rcventry - uctxt->expected_base);
1008 fdata->invalid_tids[fdata->invalid_tid_idx] |=
1009 EXP_TID_SET(LEN, node->npages);
1010 if (!fdata->invalid_tid_idx) {
1014 * hfi1_set_uevent_bits() sets a user event flag
1015 * for all processes. Because calling into the
1016 * driver to process TID cache invalidations is
1017 * expensive and TID cache invalidations are
1018 * handled on a per-process basis, we can
1019 * optimize this to set the flag only for the
1020 * process in question.
1022 ev = uctxt->dd->events +
1023 (((uctxt->ctxt - uctxt->dd->first_user_ctxt) *
1024 HFI1_MAX_SHARED_CTXTS) + fdata->subctxt);
1025 set_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
1027 fdata->invalid_tid_idx++;
1029 spin_unlock(&fdata->invalid_lock);
1033 static int tid_rb_insert(void *arg, struct mmu_rb_node *node)
1035 struct hfi1_filedata *fdata = arg;
1036 struct tid_rb_node *tnode =
1037 container_of(node, struct tid_rb_node, mmu);
1038 u32 base = fdata->uctxt->expected_base;
1040 fdata->entry_to_rb[tnode->rcventry - base] = tnode;
1044 static void cacheless_tid_rb_remove(struct hfi1_filedata *fdata,
1045 struct tid_rb_node *tnode)
1047 u32 base = fdata->uctxt->expected_base;
1049 fdata->entry_to_rb[tnode->rcventry - base] = NULL;
1050 clear_tid_node(fdata, tnode);
1053 static void tid_rb_remove(void *arg, struct mmu_rb_node *node)
1055 struct hfi1_filedata *fdata = arg;
1056 struct tid_rb_node *tnode =
1057 container_of(node, struct tid_rb_node, mmu);
1059 cacheless_tid_rb_remove(fdata, tnode);