162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright(c) 2020 Cornelis Networks, Inc.
462306a36Sopenharmony_ci * Copyright(c) 2016 Intel Corporation.
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef _HFI1_MMU_RB_H
862306a36Sopenharmony_ci#define _HFI1_MMU_RB_H
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include "hfi.h"
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_cistruct mmu_rb_node {
1362306a36Sopenharmony_ci	unsigned long addr;
1462306a36Sopenharmony_ci	unsigned long len;
1562306a36Sopenharmony_ci	unsigned long __last;
1662306a36Sopenharmony_ci	struct rb_node node;
1762306a36Sopenharmony_ci	struct mmu_rb_handler *handler;
1862306a36Sopenharmony_ci	struct list_head list;
1962306a36Sopenharmony_ci	struct kref refcount;
2062306a36Sopenharmony_ci};
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci/* filter and evict must not sleep. Only remove is allowed to sleep. */
2362306a36Sopenharmony_cistruct mmu_rb_ops {
2462306a36Sopenharmony_ci	bool (*filter)(struct mmu_rb_node *node, unsigned long addr,
2562306a36Sopenharmony_ci		       unsigned long len);
2662306a36Sopenharmony_ci	void (*remove)(void *ops_arg, struct mmu_rb_node *mnode);
2762306a36Sopenharmony_ci	int (*evict)(void *ops_arg, struct mmu_rb_node *mnode,
2862306a36Sopenharmony_ci		     void *evict_arg, bool *stop);
2962306a36Sopenharmony_ci};
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cistruct mmu_rb_handler {
3262306a36Sopenharmony_ci	/*
3362306a36Sopenharmony_ci	 * struct mmu_notifier is 56 bytes, and spinlock_t is 4 bytes, so
3462306a36Sopenharmony_ci	 * they fit together in one cache line.  mn is relatively rarely
3562306a36Sopenharmony_ci	 * accessed, so co-locating the spinlock with it achieves much of
3662306a36Sopenharmony_ci	 * the cacheline contention reduction of giving the spinlock its own
3762306a36Sopenharmony_ci	 * cacheline without the overhead of doing so.
3862306a36Sopenharmony_ci	 */
3962306a36Sopenharmony_ci	struct mmu_notifier mn;
4062306a36Sopenharmony_ci	spinlock_t lock;        /* protect the RB tree */
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	/* Begin on a new cachline boundary here */
4362306a36Sopenharmony_ci	struct rb_root_cached root ____cacheline_aligned_in_smp;
4462306a36Sopenharmony_ci	void *ops_arg;
4562306a36Sopenharmony_ci	struct mmu_rb_ops *ops;
4662306a36Sopenharmony_ci	struct list_head lru_list;
4762306a36Sopenharmony_ci	struct work_struct del_work;
4862306a36Sopenharmony_ci	struct list_head del_list;
4962306a36Sopenharmony_ci	struct workqueue_struct *wq;
5062306a36Sopenharmony_ci	void *free_ptr;
5162306a36Sopenharmony_ci};
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ciint hfi1_mmu_rb_register(void *ops_arg,
5462306a36Sopenharmony_ci			 struct mmu_rb_ops *ops,
5562306a36Sopenharmony_ci			 struct workqueue_struct *wq,
5662306a36Sopenharmony_ci			 struct mmu_rb_handler **handler);
5762306a36Sopenharmony_civoid hfi1_mmu_rb_unregister(struct mmu_rb_handler *handler);
5862306a36Sopenharmony_ciint hfi1_mmu_rb_insert(struct mmu_rb_handler *handler,
5962306a36Sopenharmony_ci		       struct mmu_rb_node *mnode);
6062306a36Sopenharmony_civoid hfi1_mmu_rb_release(struct kref *refcount);
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_civoid hfi1_mmu_rb_evict(struct mmu_rb_handler *handler, void *evict_arg);
6362306a36Sopenharmony_cistruct mmu_rb_node *hfi1_mmu_rb_get_first(struct mmu_rb_handler *handler,
6462306a36Sopenharmony_ci					  unsigned long addr,
6562306a36Sopenharmony_ci					  unsigned long len);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci#endif /* _HFI1_MMU_RB_H */
68