xref: /openbmc/linux/include/rdma/ib_umem_odp.h (revision d2183c6f1958e6b6dfdde279f4cee04280710e34)
18ada2c1cSShachar Raindel /*
28ada2c1cSShachar Raindel  * Copyright (c) 2014 Mellanox Technologies. All rights reserved.
38ada2c1cSShachar Raindel  *
48ada2c1cSShachar Raindel  * This software is available to you under a choice of one of two
58ada2c1cSShachar Raindel  * licenses.  You may choose to be licensed under the terms of the GNU
68ada2c1cSShachar Raindel  * General Public License (GPL) Version 2, available from the file
78ada2c1cSShachar Raindel  * COPYING in the main directory of this source tree, or the
88ada2c1cSShachar Raindel  * OpenIB.org BSD license below:
98ada2c1cSShachar Raindel  *
108ada2c1cSShachar Raindel  *     Redistribution and use in source and binary forms, with or
118ada2c1cSShachar Raindel  *     without modification, are permitted provided that the following
128ada2c1cSShachar Raindel  *     conditions are met:
138ada2c1cSShachar Raindel  *
148ada2c1cSShachar Raindel  *      - Redistributions of source code must retain the above
158ada2c1cSShachar Raindel  *        copyright notice, this list of conditions and the following
168ada2c1cSShachar Raindel  *        disclaimer.
178ada2c1cSShachar Raindel  *
188ada2c1cSShachar Raindel  *      - Redistributions in binary form must reproduce the above
198ada2c1cSShachar Raindel  *        copyright notice, this list of conditions and the following
208ada2c1cSShachar Raindel  *        disclaimer in the documentation and/or other materials
218ada2c1cSShachar Raindel  *        provided with the distribution.
228ada2c1cSShachar Raindel  *
238ada2c1cSShachar Raindel  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
248ada2c1cSShachar Raindel  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
258ada2c1cSShachar Raindel  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
268ada2c1cSShachar Raindel  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
278ada2c1cSShachar Raindel  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
288ada2c1cSShachar Raindel  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
298ada2c1cSShachar Raindel  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
308ada2c1cSShachar Raindel  * SOFTWARE.
318ada2c1cSShachar Raindel  */
328ada2c1cSShachar Raindel 
338ada2c1cSShachar Raindel #ifndef IB_UMEM_ODP_H
348ada2c1cSShachar Raindel #define IB_UMEM_ODP_H
358ada2c1cSShachar Raindel 
368ada2c1cSShachar Raindel #include <rdma/ib_umem.h>
37882214e2SHaggai Eran #include <rdma/ib_verbs.h>
38882214e2SHaggai Eran #include <linux/interval_tree.h>
39882214e2SHaggai Eran 
40882214e2SHaggai Eran struct umem_odp_node {
41882214e2SHaggai Eran 	u64 __subtree_last;
42882214e2SHaggai Eran 	struct rb_node rb;
43882214e2SHaggai Eran };
448ada2c1cSShachar Raindel 
458ada2c1cSShachar Raindel struct ib_umem_odp {
4641b4deeaSJason Gunthorpe 	struct ib_umem umem;
47c9990ab3SJason Gunthorpe 	struct ib_ucontext_per_mm *per_mm;
48c9990ab3SJason Gunthorpe 
498ada2c1cSShachar Raindel 	/*
508ada2c1cSShachar Raindel 	 * An array of the pages included in the on-demand paging umem.
518ada2c1cSShachar Raindel 	 * Indices of pages that are currently not mapped into the device will
528ada2c1cSShachar Raindel 	 * contain NULL.
538ada2c1cSShachar Raindel 	 */
548ada2c1cSShachar Raindel 	struct page		**page_list;
558ada2c1cSShachar Raindel 	/*
568ada2c1cSShachar Raindel 	 * An array of the same size as page_list, with DMA addresses mapped
578ada2c1cSShachar Raindel 	 * for pages the pages in page_list. The lower two bits designate
588ada2c1cSShachar Raindel 	 * access permissions. See ODP_READ_ALLOWED_BIT and
598ada2c1cSShachar Raindel 	 * ODP_WRITE_ALLOWED_BIT.
608ada2c1cSShachar Raindel 	 */
618ada2c1cSShachar Raindel 	dma_addr_t		*dma_list;
628ada2c1cSShachar Raindel 	/*
638ada2c1cSShachar Raindel 	 * The umem_mutex protects the page_list and dma_list fields of an ODP
64882214e2SHaggai Eran 	 * umem, allowing only a single thread to map/unmap pages. The mutex
65882214e2SHaggai Eran 	 * also protects access to the mmu notifier counters.
668ada2c1cSShachar Raindel 	 */
678ada2c1cSShachar Raindel 	struct mutex		umem_mutex;
688ada2c1cSShachar Raindel 	void			*private; /* for the HW driver to use. */
69882214e2SHaggai Eran 
70882214e2SHaggai Eran 	int notifiers_seq;
71882214e2SHaggai Eran 	int notifiers_count;
72d10bcf94SShiraz Saleem 	int npages;
73882214e2SHaggai Eran 
74882214e2SHaggai Eran 	/* Tree tracking */
75882214e2SHaggai Eran 	struct umem_odp_node	interval_tree;
76882214e2SHaggai Eran 
77882214e2SHaggai Eran 	struct completion	notifier_completion;
78882214e2SHaggai Eran 	int			dying;
79*d2183c6fSJason Gunthorpe 	unsigned int		page_shift;
80d07d1d70SArtemy Kovalyov 	struct work_struct	work;
818ada2c1cSShachar Raindel };
828ada2c1cSShachar Raindel 
83b5231b01SJason Gunthorpe static inline struct ib_umem_odp *to_ib_umem_odp(struct ib_umem *umem)
84b5231b01SJason Gunthorpe {
8541b4deeaSJason Gunthorpe 	return container_of(umem, struct ib_umem_odp, umem);
86b5231b01SJason Gunthorpe }
87b5231b01SJason Gunthorpe 
88*d2183c6fSJason Gunthorpe /* Returns the first page of an ODP umem. */
89*d2183c6fSJason Gunthorpe static inline unsigned long ib_umem_start(struct ib_umem_odp *umem_odp)
90*d2183c6fSJason Gunthorpe {
91*d2183c6fSJason Gunthorpe 	return ALIGN_DOWN(umem_odp->umem.address, 1UL << umem_odp->page_shift);
92*d2183c6fSJason Gunthorpe }
93*d2183c6fSJason Gunthorpe 
94*d2183c6fSJason Gunthorpe /* Returns the address of the page after the last one of an ODP umem. */
95*d2183c6fSJason Gunthorpe static inline unsigned long ib_umem_end(struct ib_umem_odp *umem_odp)
96*d2183c6fSJason Gunthorpe {
97*d2183c6fSJason Gunthorpe 	return ALIGN(umem_odp->umem.address + umem_odp->umem.length,
98*d2183c6fSJason Gunthorpe 		     1UL << umem_odp->page_shift);
99*d2183c6fSJason Gunthorpe }
100*d2183c6fSJason Gunthorpe 
101*d2183c6fSJason Gunthorpe static inline size_t ib_umem_odp_num_pages(struct ib_umem_odp *umem_odp)
102*d2183c6fSJason Gunthorpe {
103*d2183c6fSJason Gunthorpe 	return (ib_umem_end(umem_odp) - ib_umem_start(umem_odp)) >>
104*d2183c6fSJason Gunthorpe 	       umem_odp->page_shift;
105*d2183c6fSJason Gunthorpe }
106*d2183c6fSJason Gunthorpe 
10713859d5dSLeon Romanovsky /*
10813859d5dSLeon Romanovsky  * The lower 2 bits of the DMA address signal the R/W permissions for
10913859d5dSLeon Romanovsky  * the entry. To upgrade the permissions, provide the appropriate
11013859d5dSLeon Romanovsky  * bitmask to the map_dma_pages function.
11113859d5dSLeon Romanovsky  *
11213859d5dSLeon Romanovsky  * Be aware that upgrading a mapped address might result in change of
11313859d5dSLeon Romanovsky  * the DMA address for the page.
11413859d5dSLeon Romanovsky  */
11513859d5dSLeon Romanovsky #define ODP_READ_ALLOWED_BIT  (1<<0ULL)
11613859d5dSLeon Romanovsky #define ODP_WRITE_ALLOWED_BIT (1<<1ULL)
11713859d5dSLeon Romanovsky 
11813859d5dSLeon Romanovsky #define ODP_DMA_ADDR_MASK (~(ODP_READ_ALLOWED_BIT | ODP_WRITE_ALLOWED_BIT))
11913859d5dSLeon Romanovsky 
1208ada2c1cSShachar Raindel #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1218ada2c1cSShachar Raindel 
122f27a0d50SJason Gunthorpe struct ib_ucontext_per_mm {
123f27a0d50SJason Gunthorpe 	struct ib_ucontext *context;
124f27a0d50SJason Gunthorpe 	struct mm_struct *mm;
125f27a0d50SJason Gunthorpe 	struct pid *tgid;
126be7a57b4SJason Gunthorpe 	bool active;
127f27a0d50SJason Gunthorpe 
128f27a0d50SJason Gunthorpe 	struct rb_root_cached umem_tree;
129f27a0d50SJason Gunthorpe 	/* Protects umem_tree */
130f27a0d50SJason Gunthorpe 	struct rw_semaphore umem_rwsem;
131f27a0d50SJason Gunthorpe 
132f27a0d50SJason Gunthorpe 	struct mmu_notifier mn;
133f27a0d50SJason Gunthorpe 	unsigned int odp_mrs_count;
134f27a0d50SJason Gunthorpe 
135f27a0d50SJason Gunthorpe 	struct list_head ucontext_list;
13656ac9dd9SJason Gunthorpe 	struct rcu_head rcu;
137f27a0d50SJason Gunthorpe };
138f27a0d50SJason Gunthorpe 
13941b4deeaSJason Gunthorpe int ib_umem_odp_get(struct ib_umem_odp *umem_odp, int access);
140da6a496aSMoni Shoua struct ib_umem_odp *ib_alloc_odp_umem(struct ib_umem_odp *root_umem,
141b5231b01SJason Gunthorpe 				      unsigned long addr, size_t size);
142b5231b01SJason Gunthorpe void ib_umem_odp_release(struct ib_umem_odp *umem_odp);
1438ada2c1cSShachar Raindel 
144b5231b01SJason Gunthorpe int ib_umem_odp_map_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset,
145b5231b01SJason Gunthorpe 			      u64 bcnt, u64 access_mask,
146b5231b01SJason Gunthorpe 			      unsigned long current_seq);
1478ada2c1cSShachar Raindel 
148b5231b01SJason Gunthorpe void ib_umem_odp_unmap_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset,
1498ada2c1cSShachar Raindel 				 u64 bound);
1508ada2c1cSShachar Raindel 
151b5231b01SJason Gunthorpe typedef int (*umem_call_back)(struct ib_umem_odp *item, u64 start, u64 end,
152882214e2SHaggai Eran 			      void *cookie);
153882214e2SHaggai Eran /*
154882214e2SHaggai Eran  * Call the callback on each ib_umem in the range. Returns the logical or of
155882214e2SHaggai Eran  * the return values of the functions called.
156882214e2SHaggai Eran  */
157f808c13fSDavidlohr Bueso int rbt_ib_umem_for_each_in_range(struct rb_root_cached *root,
158f808c13fSDavidlohr Bueso 				  u64 start, u64 end,
15993065ac7SMichal Hocko 				  umem_call_back cb,
16093065ac7SMichal Hocko 				  bool blockable, void *cookie);
161882214e2SHaggai Eran 
162d07d1d70SArtemy Kovalyov /*
163d07d1d70SArtemy Kovalyov  * Find first region intersecting with address range.
164d07d1d70SArtemy Kovalyov  * Return NULL if not found
165d07d1d70SArtemy Kovalyov  */
166f808c13fSDavidlohr Bueso struct ib_umem_odp *rbt_ib_umem_lookup(struct rb_root_cached *root,
167d07d1d70SArtemy Kovalyov 				       u64 addr, u64 length);
168882214e2SHaggai Eran 
169b5231b01SJason Gunthorpe static inline int ib_umem_mmu_notifier_retry(struct ib_umem_odp *umem_odp,
170882214e2SHaggai Eran 					     unsigned long mmu_seq)
171882214e2SHaggai Eran {
172882214e2SHaggai Eran 	/*
173882214e2SHaggai Eran 	 * This code is strongly based on the KVM code from
174882214e2SHaggai Eran 	 * mmu_notifier_retry. Should be called with
175b5231b01SJason Gunthorpe 	 * the relevant locks taken (umem_odp->umem_mutex
176882214e2SHaggai Eran 	 * and the ucontext umem_mutex semaphore locked for read).
177882214e2SHaggai Eran 	 */
178882214e2SHaggai Eran 
179b5231b01SJason Gunthorpe 	if (unlikely(umem_odp->notifiers_count))
180882214e2SHaggai Eran 		return 1;
181b5231b01SJason Gunthorpe 	if (umem_odp->notifiers_seq != mmu_seq)
182882214e2SHaggai Eran 		return 1;
183882214e2SHaggai Eran 	return 0;
184882214e2SHaggai Eran }
185882214e2SHaggai Eran 
1868ada2c1cSShachar Raindel #else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
1878ada2c1cSShachar Raindel 
18841b4deeaSJason Gunthorpe static inline int ib_umem_odp_get(struct ib_umem_odp *umem_odp, int access)
1898ada2c1cSShachar Raindel {
1908ada2c1cSShachar Raindel 	return -EINVAL;
1918ada2c1cSShachar Raindel }
1928ada2c1cSShachar Raindel 
193b5231b01SJason Gunthorpe static inline void ib_umem_odp_release(struct ib_umem_odp *umem_odp) {}
1948ada2c1cSShachar Raindel 
1958ada2c1cSShachar Raindel #endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
1968ada2c1cSShachar Raindel 
1978ada2c1cSShachar Raindel #endif /* IB_UMEM_ODP_H */
198