18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright(c) 2020 Cornelis Networks, Inc.
38c2ecf20Sopenharmony_ci * Copyright(c) 2016 Intel Corporation.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license.  When using or
68c2ecf20Sopenharmony_ci * redistributing this file, you may do so under either license.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * GPL LICENSE SUMMARY
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
118c2ecf20Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as
128c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
158c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
168c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
178c2ecf20Sopenharmony_ci * General Public License for more details.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * BSD LICENSE
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
228c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
238c2ecf20Sopenharmony_ci * are met:
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci *  - Redistributions of source code must retain the above copyright
268c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
278c2ecf20Sopenharmony_ci *  - Redistributions in binary form must reproduce the above copyright
288c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in
298c2ecf20Sopenharmony_ci *    the documentation and/or other materials provided with the
308c2ecf20Sopenharmony_ci *    distribution.
318c2ecf20Sopenharmony_ci *  - Neither the name of Intel Corporation nor the names of its
328c2ecf20Sopenharmony_ci *    contributors may be used to endorse or promote products derived
338c2ecf20Sopenharmony_ci *    from this software without specific prior written permission.
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
368c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
378c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
388c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
398c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
408c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
418c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
428c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
438c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
448c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
458c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_ci#ifndef _HFI1_MMU_RB_H
498c2ecf20Sopenharmony_ci#define _HFI1_MMU_RB_H
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#include "hfi.h"
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistruct mmu_rb_node {
548c2ecf20Sopenharmony_ci	unsigned long addr;
558c2ecf20Sopenharmony_ci	unsigned long len;
568c2ecf20Sopenharmony_ci	unsigned long __last;
578c2ecf20Sopenharmony_ci	struct rb_node node;
588c2ecf20Sopenharmony_ci	struct mmu_rb_handler *handler;
598c2ecf20Sopenharmony_ci	struct list_head list;
608c2ecf20Sopenharmony_ci	struct kref refcount;
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/*
648c2ecf20Sopenharmony_ci * NOTE: filter, insert, invalidate, and evict must not sleep.  Only remove is
658c2ecf20Sopenharmony_ci * allowed to sleep.
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_cistruct mmu_rb_ops {
688c2ecf20Sopenharmony_ci	bool (*filter)(struct mmu_rb_node *node, unsigned long addr,
698c2ecf20Sopenharmony_ci		       unsigned long len);
708c2ecf20Sopenharmony_ci	int (*insert)(void *ops_arg, struct mmu_rb_node *mnode);
718c2ecf20Sopenharmony_ci	void (*remove)(void *ops_arg, struct mmu_rb_node *mnode);
728c2ecf20Sopenharmony_ci	int (*invalidate)(void *ops_arg, struct mmu_rb_node *node);
738c2ecf20Sopenharmony_ci	int (*evict)(void *ops_arg, struct mmu_rb_node *mnode,
748c2ecf20Sopenharmony_ci		     void *evict_arg, bool *stop);
758c2ecf20Sopenharmony_ci};
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistruct mmu_rb_handler {
788c2ecf20Sopenharmony_ci	struct mmu_notifier mn;
798c2ecf20Sopenharmony_ci	struct rb_root_cached root;
808c2ecf20Sopenharmony_ci	void *ops_arg;
818c2ecf20Sopenharmony_ci	spinlock_t lock;        /* protect the RB tree */
828c2ecf20Sopenharmony_ci	struct mmu_rb_ops *ops;
838c2ecf20Sopenharmony_ci	struct list_head lru_list;
848c2ecf20Sopenharmony_ci	struct work_struct del_work;
858c2ecf20Sopenharmony_ci	struct list_head del_list;
868c2ecf20Sopenharmony_ci	struct workqueue_struct *wq;
878c2ecf20Sopenharmony_ci};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ciint hfi1_mmu_rb_register(void *ops_arg,
908c2ecf20Sopenharmony_ci			 struct mmu_rb_ops *ops,
918c2ecf20Sopenharmony_ci			 struct workqueue_struct *wq,
928c2ecf20Sopenharmony_ci			 struct mmu_rb_handler **handler);
938c2ecf20Sopenharmony_civoid hfi1_mmu_rb_unregister(struct mmu_rb_handler *handler);
948c2ecf20Sopenharmony_ciint hfi1_mmu_rb_insert(struct mmu_rb_handler *handler,
958c2ecf20Sopenharmony_ci		       struct mmu_rb_node *mnode);
968c2ecf20Sopenharmony_civoid hfi1_mmu_rb_release(struct kref *refcount);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_civoid hfi1_mmu_rb_evict(struct mmu_rb_handler *handler, void *evict_arg);
998c2ecf20Sopenharmony_cistruct mmu_rb_node *hfi1_mmu_rb_get_first(struct mmu_rb_handler *handler,
1008c2ecf20Sopenharmony_ci					  unsigned long addr,
1018c2ecf20Sopenharmony_ci					  unsigned long len);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#endif /* _HFI1_MMU_RB_H */
104