1 /* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */
2 /*
3  * Copyright(c) 2020 Cornelis Networks, Inc.
4  * Copyright(c) 2016 Intel Corporation.
5  */
6 
7 #ifndef _HFI1_MMU_RB_H
8 #define _HFI1_MMU_RB_H
9 
10 #include "hfi.h"
11 
12 struct mmu_rb_node {
13 	unsigned long addr;
14 	unsigned long len;
15 	unsigned long __last;
16 	struct rb_node node;
17 	struct mmu_rb_handler *handler;
18 	struct list_head list;
19 };
20 
21 /*
22  * NOTE: filter, insert, invalidate, and evict must not sleep.  Only remove is
23  * allowed to sleep.
24  */
25 struct mmu_rb_ops {
26 	bool (*filter)(struct mmu_rb_node *node, unsigned long addr,
27 		       unsigned long len);
28 	int (*insert)(void *ops_arg, struct mmu_rb_node *mnode);
29 	void (*remove)(void *ops_arg, struct mmu_rb_node *mnode);
30 	int (*invalidate)(void *ops_arg, struct mmu_rb_node *node);
31 	int (*evict)(void *ops_arg, struct mmu_rb_node *mnode,
32 		     void *evict_arg, bool *stop);
33 };
34 
35 struct mmu_rb_handler {
36 	/*
37 	 * struct mmu_notifier is 56 bytes, and spinlock_t is 4 bytes, so
38 	 * they fit together in one cache line.  mn is relatively rarely
39 	 * accessed, so co-locating the spinlock with it achieves much of
40 	 * the cacheline contention reduction of giving the spinlock its own
41 	 * cacheline without the overhead of doing so.
42 	 */
43 	struct mmu_notifier mn;
44 	spinlock_t lock;        /* protect the RB tree */
45 
46 	/* Begin on a new cachline boundary here */
47 	struct rb_root_cached root ____cacheline_aligned_in_smp;
48 	void *ops_arg;
49 	struct mmu_rb_ops *ops;
50 	struct list_head lru_list;
51 	struct work_struct del_work;
52 	struct list_head del_list;
53 	struct workqueue_struct *wq;
54 	void *free_ptr;
55 };
56 
57 int hfi1_mmu_rb_register(void *ops_arg,
58 			 struct mmu_rb_ops *ops,
59 			 struct workqueue_struct *wq,
60 			 struct mmu_rb_handler **handler);
61 void hfi1_mmu_rb_unregister(struct mmu_rb_handler *handler);
62 int hfi1_mmu_rb_insert(struct mmu_rb_handler *handler,
63 		       struct mmu_rb_node *mnode);
64 void hfi1_mmu_rb_evict(struct mmu_rb_handler *handler, void *evict_arg);
65 struct mmu_rb_node *hfi1_mmu_rb_get_first(struct mmu_rb_handler *handler,
66 					  unsigned long addr,
67 					  unsigned long len);
68 
69 #endif /* _HFI1_MMU_RB_H */
70