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
53struct 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	struct kref refcount;
61};
62
63/*
64 * NOTE: filter, insert, invalidate, and evict must not sleep.  Only remove is
65 * allowed to sleep.
66 */
67struct mmu_rb_ops {
68	bool (*filter)(struct mmu_rb_node *node, unsigned long addr,
69		       unsigned long len);
70	int (*insert)(void *ops_arg, struct mmu_rb_node *mnode);
71	void (*remove)(void *ops_arg, struct mmu_rb_node *mnode);
72	int (*invalidate)(void *ops_arg, struct mmu_rb_node *node);
73	int (*evict)(void *ops_arg, struct mmu_rb_node *mnode,
74		     void *evict_arg, bool *stop);
75};
76
77struct mmu_rb_handler {
78	struct mmu_notifier mn;
79	struct rb_root_cached root;
80	void *ops_arg;
81	spinlock_t lock;        /* protect the RB tree */
82	struct mmu_rb_ops *ops;
83	struct list_head lru_list;
84	struct work_struct del_work;
85	struct list_head del_list;
86	struct workqueue_struct *wq;
87};
88
89int hfi1_mmu_rb_register(void *ops_arg,
90			 struct mmu_rb_ops *ops,
91			 struct workqueue_struct *wq,
92			 struct mmu_rb_handler **handler);
93void hfi1_mmu_rb_unregister(struct mmu_rb_handler *handler);
94int hfi1_mmu_rb_insert(struct mmu_rb_handler *handler,
95		       struct mmu_rb_node *mnode);
96void hfi1_mmu_rb_release(struct kref *refcount);
97
98void hfi1_mmu_rb_evict(struct mmu_rb_handler *handler, void *evict_arg);
99struct mmu_rb_node *hfi1_mmu_rb_get_first(struct mmu_rb_handler *handler,
100					  unsigned long addr,
101					  unsigned long len);
102
103#endif /* _HFI1_MMU_RB_H */
104