xref: /openbmc/linux/include/rdma/ib_umem_odp.h (revision 52cdded0)
1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /*
3  * Copyright (c) 2014 Mellanox Technologies. All rights reserved.
4  */
5 
6 #ifndef IB_UMEM_ODP_H
7 #define IB_UMEM_ODP_H
8 
9 #include <rdma/ib_umem.h>
10 #include <rdma/ib_verbs.h>
11 
12 struct ib_umem_odp {
13 	struct ib_umem umem;
14 	struct mmu_interval_notifier notifier;
15 	struct pid *tgid;
16 
17 	/*
18 	 * An array of the pages included in the on-demand paging umem.
19 	 * Indices of pages that are currently not mapped into the device will
20 	 * contain NULL.
21 	 */
22 	struct page		**page_list;
23 	/*
24 	 * An array of the same size as page_list, with DMA addresses mapped
25 	 * for pages the pages in page_list. The lower two bits designate
26 	 * access permissions. See ODP_READ_ALLOWED_BIT and
27 	 * ODP_WRITE_ALLOWED_BIT.
28 	 */
29 	dma_addr_t		*dma_list;
30 	/*
31 	 * The umem_mutex protects the page_list and dma_list fields of an ODP
32 	 * umem, allowing only a single thread to map/unmap pages. The mutex
33 	 * also protects access to the mmu notifier counters.
34 	 */
35 	struct mutex		umem_mutex;
36 	void			*private; /* for the HW driver to use. */
37 
38 	int npages;
39 
40 	/*
41 	 * An implicit odp umem cannot be DMA mapped, has 0 length, and serves
42 	 * only as an anchor for the driver to hold onto the per_mm. FIXME:
43 	 * This should be removed and drivers should work with the per_mm
44 	 * directly.
45 	 */
46 	bool is_implicit_odp;
47 
48 	unsigned int		page_shift;
49 };
50 
51 static inline struct ib_umem_odp *to_ib_umem_odp(struct ib_umem *umem)
52 {
53 	return container_of(umem, struct ib_umem_odp, umem);
54 }
55 
56 /* Returns the first page of an ODP umem. */
57 static inline unsigned long ib_umem_start(struct ib_umem_odp *umem_odp)
58 {
59 	return umem_odp->notifier.interval_tree.start;
60 }
61 
62 /* Returns the address of the page after the last one of an ODP umem. */
63 static inline unsigned long ib_umem_end(struct ib_umem_odp *umem_odp)
64 {
65 	return umem_odp->notifier.interval_tree.last + 1;
66 }
67 
68 static inline size_t ib_umem_odp_num_pages(struct ib_umem_odp *umem_odp)
69 {
70 	return (ib_umem_end(umem_odp) - ib_umem_start(umem_odp)) >>
71 	       umem_odp->page_shift;
72 }
73 
74 /*
75  * The lower 2 bits of the DMA address signal the R/W permissions for
76  * the entry. To upgrade the permissions, provide the appropriate
77  * bitmask to the map_dma_pages function.
78  *
79  * Be aware that upgrading a mapped address might result in change of
80  * the DMA address for the page.
81  */
82 #define ODP_READ_ALLOWED_BIT  (1<<0ULL)
83 #define ODP_WRITE_ALLOWED_BIT (1<<1ULL)
84 
85 #define ODP_DMA_ADDR_MASK (~(ODP_READ_ALLOWED_BIT | ODP_WRITE_ALLOWED_BIT))
86 
87 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
88 
89 struct ib_umem_odp *
90 ib_umem_odp_get(struct ib_device *device, unsigned long addr, size_t size,
91 		int access, const struct mmu_interval_notifier_ops *ops);
92 struct ib_umem_odp *ib_umem_odp_alloc_implicit(struct ib_device *device,
93 					       int access);
94 struct ib_umem_odp *
95 ib_umem_odp_alloc_child(struct ib_umem_odp *root_umem, unsigned long addr,
96 			size_t size,
97 			const struct mmu_interval_notifier_ops *ops);
98 void ib_umem_odp_release(struct ib_umem_odp *umem_odp);
99 
100 int ib_umem_odp_map_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset,
101 			      u64 bcnt, u64 access_mask,
102 			      unsigned long current_seq);
103 
104 void ib_umem_odp_unmap_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset,
105 				 u64 bound);
106 
107 #else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
108 
109 static inline struct ib_umem_odp *
110 ib_umem_odp_get(struct ib_device *device, unsigned long addr, size_t size,
111 		int access, const struct mmu_interval_notifier_ops *ops)
112 {
113 	return ERR_PTR(-EINVAL);
114 }
115 
116 static inline void ib_umem_odp_release(struct ib_umem_odp *umem_odp) {}
117 
118 #endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
119 
120 #endif /* IB_UMEM_ODP_H */
121