1 /*
2  * Copyright(c) 2020 Cornelis Networks, Inc.
3  * Copyright(c) 2016 Intel Corporation.
4  *
5  * This file is provided under a dual BSD/GPLv2 license.  When using or
6  * redistributing this file, you may do so under either license.
7  *
8  * GPL LICENSE SUMMARY
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * BSD LICENSE
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
24  *
25  *  - Redistributions of source code must retain the above copyright
26  *    notice, this list of conditions and the following disclaimer.
27  *  - Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in
29  *    the documentation and/or other materials provided with the
30  *    distribution.
31  *  - Neither the name of Intel Corporation nor the names of its
32  *    contributors may be used to endorse or promote products derived
33  *    from this software without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  */
48 #ifndef _HFI1_MMU_RB_H
49 #define _HFI1_MMU_RB_H
50 
51 #include "hfi.h"
52 
53 struct mmu_rb_node {
54 	unsigned long addr;
55 	unsigned long len;
56 	unsigned long __last;
57 	struct rb_node node;
58 	struct mmu_rb_handler *handler;
59 	struct list_head list;
60 };
61 
62 /*
63  * NOTE: filter, insert, invalidate, and evict must not sleep.  Only remove is
64  * allowed to sleep.
65  */
66 struct mmu_rb_ops {
67 	bool (*filter)(struct mmu_rb_node *node, unsigned long addr,
68 		       unsigned long len);
69 	int (*insert)(void *ops_arg, struct mmu_rb_node *mnode);
70 	void (*remove)(void *ops_arg, struct mmu_rb_node *mnode);
71 	int (*invalidate)(void *ops_arg, struct mmu_rb_node *node);
72 	int (*evict)(void *ops_arg, struct mmu_rb_node *mnode,
73 		     void *evict_arg, bool *stop);
74 };
75 
76 struct mmu_rb_handler {
77 	struct mmu_notifier mn;
78 	struct rb_root_cached root;
79 	void *ops_arg;
80 	spinlock_t lock;        /* protect the RB tree */
81 	struct mmu_rb_ops *ops;
82 	struct list_head lru_list;
83 	struct work_struct del_work;
84 	struct list_head del_list;
85 	struct workqueue_struct *wq;
86 };
87 
88 int hfi1_mmu_rb_register(void *ops_arg,
89 			 struct mmu_rb_ops *ops,
90 			 struct workqueue_struct *wq,
91 			 struct mmu_rb_handler **handler);
92 void hfi1_mmu_rb_unregister(struct mmu_rb_handler *handler);
93 int hfi1_mmu_rb_insert(struct mmu_rb_handler *handler,
94 		       struct mmu_rb_node *mnode);
95 void hfi1_mmu_rb_evict(struct mmu_rb_handler *handler, void *evict_arg);
96 void hfi1_mmu_rb_remove(struct mmu_rb_handler *handler,
97 			struct mmu_rb_node *mnode);
98 bool hfi1_mmu_rb_remove_unless_exact(struct mmu_rb_handler *handler,
99 				     unsigned long addr, unsigned long len,
100 				     struct mmu_rb_node **rb_node);
101 
102 #endif /* _HFI1_MMU_RB_H */
103