xref: /openbmc/linux/include/rdma/ib_umem_odp.h (revision 8bfafde0)
16bf9d8f6SLeon Romanovsky /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
28ada2c1cSShachar Raindel /*
38ada2c1cSShachar Raindel  * Copyright (c) 2014 Mellanox Technologies. All rights reserved.
48ada2c1cSShachar Raindel  */
58ada2c1cSShachar Raindel 
68ada2c1cSShachar Raindel #ifndef IB_UMEM_ODP_H
78ada2c1cSShachar Raindel #define IB_UMEM_ODP_H
88ada2c1cSShachar Raindel 
98ada2c1cSShachar Raindel #include <rdma/ib_umem.h>
10882214e2SHaggai Eran #include <rdma/ib_verbs.h>
11882214e2SHaggai Eran 
128ada2c1cSShachar Raindel struct ib_umem_odp {
1341b4deeaSJason Gunthorpe 	struct ib_umem umem;
14f25a546eSJason Gunthorpe 	struct mmu_interval_notifier notifier;
15f25a546eSJason Gunthorpe 	struct pid *tgid;
16c9990ab3SJason Gunthorpe 
1736f30e48SYishai Hadas 	/* An array of the pfns included in the on-demand paging umem. */
1836f30e48SYishai Hadas 	unsigned long *pfn_list;
1936f30e48SYishai Hadas 
208ada2c1cSShachar Raindel 	/*
2136f30e48SYishai Hadas 	 * An array with DMA addresses mapped for pfns in pfn_list.
2236f30e48SYishai Hadas 	 * The lower two bits designate access permissions.
2336f30e48SYishai Hadas 	 * See ODP_READ_ALLOWED_BIT and ODP_WRITE_ALLOWED_BIT.
248ada2c1cSShachar Raindel 	 */
258ada2c1cSShachar Raindel 	dma_addr_t		*dma_list;
268ada2c1cSShachar Raindel 	/*
278ada2c1cSShachar Raindel 	 * The umem_mutex protects the page_list and dma_list fields of an ODP
28882214e2SHaggai Eran 	 * umem, allowing only a single thread to map/unmap pages. The mutex
29882214e2SHaggai Eran 	 * also protects access to the mmu notifier counters.
308ada2c1cSShachar Raindel 	 */
318ada2c1cSShachar Raindel 	struct mutex		umem_mutex;
328ada2c1cSShachar Raindel 	void			*private; /* for the HW driver to use. */
33882214e2SHaggai Eran 
34d10bcf94SShiraz Saleem 	int npages;
35882214e2SHaggai Eran 
36fd7dbf03SJason Gunthorpe 	/*
37fd7dbf03SJason Gunthorpe 	 * An implicit odp umem cannot be DMA mapped, has 0 length, and serves
38fd7dbf03SJason Gunthorpe 	 * only as an anchor for the driver to hold onto the per_mm. FIXME:
39fd7dbf03SJason Gunthorpe 	 * This should be removed and drivers should work with the per_mm
40fd7dbf03SJason Gunthorpe 	 * directly.
41fd7dbf03SJason Gunthorpe 	 */
42fd7dbf03SJason Gunthorpe 	bool is_implicit_odp;
43fd7dbf03SJason Gunthorpe 
44d2183c6fSJason Gunthorpe 	unsigned int		page_shift;
458ada2c1cSShachar Raindel };
468ada2c1cSShachar Raindel 
to_ib_umem_odp(struct ib_umem * umem)47b5231b01SJason Gunthorpe static inline struct ib_umem_odp *to_ib_umem_odp(struct ib_umem *umem)
48b5231b01SJason Gunthorpe {
4941b4deeaSJason Gunthorpe 	return container_of(umem, struct ib_umem_odp, umem);
50b5231b01SJason Gunthorpe }
51b5231b01SJason Gunthorpe 
52d2183c6fSJason Gunthorpe /* Returns the first page of an ODP umem. */
ib_umem_start(struct ib_umem_odp * umem_odp)53d2183c6fSJason Gunthorpe static inline unsigned long ib_umem_start(struct ib_umem_odp *umem_odp)
54d2183c6fSJason Gunthorpe {
55f25a546eSJason Gunthorpe 	return umem_odp->notifier.interval_tree.start;
56d2183c6fSJason Gunthorpe }
57d2183c6fSJason Gunthorpe 
58d2183c6fSJason Gunthorpe /* Returns the address of the page after the last one of an ODP umem. */
ib_umem_end(struct ib_umem_odp * umem_odp)59d2183c6fSJason Gunthorpe static inline unsigned long ib_umem_end(struct ib_umem_odp *umem_odp)
60d2183c6fSJason Gunthorpe {
61f25a546eSJason Gunthorpe 	return umem_odp->notifier.interval_tree.last + 1;
62d2183c6fSJason Gunthorpe }
63d2183c6fSJason Gunthorpe 
ib_umem_odp_num_pages(struct ib_umem_odp * umem_odp)64d2183c6fSJason Gunthorpe static inline size_t ib_umem_odp_num_pages(struct ib_umem_odp *umem_odp)
65d2183c6fSJason Gunthorpe {
66d2183c6fSJason Gunthorpe 	return (ib_umem_end(umem_odp) - ib_umem_start(umem_odp)) >>
67d2183c6fSJason Gunthorpe 	       umem_odp->page_shift;
68d2183c6fSJason Gunthorpe }
69d2183c6fSJason Gunthorpe 
7013859d5dSLeon Romanovsky /*
7113859d5dSLeon Romanovsky  * The lower 2 bits of the DMA address signal the R/W permissions for
7213859d5dSLeon Romanovsky  * the entry. To upgrade the permissions, provide the appropriate
7313859d5dSLeon Romanovsky  * bitmask to the map_dma_pages function.
7413859d5dSLeon Romanovsky  *
7513859d5dSLeon Romanovsky  * Be aware that upgrading a mapped address might result in change of
7613859d5dSLeon Romanovsky  * the DMA address for the page.
7713859d5dSLeon Romanovsky  */
7813859d5dSLeon Romanovsky #define ODP_READ_ALLOWED_BIT  (1<<0ULL)
7913859d5dSLeon Romanovsky #define ODP_WRITE_ALLOWED_BIT (1<<1ULL)
8013859d5dSLeon Romanovsky 
8113859d5dSLeon Romanovsky #define ODP_DMA_ADDR_MASK (~(ODP_READ_ALLOWED_BIT | ODP_WRITE_ALLOWED_BIT))
8213859d5dSLeon Romanovsky 
838ada2c1cSShachar Raindel #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
848ada2c1cSShachar Raindel 
85f25a546eSJason Gunthorpe struct ib_umem_odp *
86c320e527SMoni Shoua ib_umem_odp_get(struct ib_device *device, unsigned long addr, size_t size,
87f25a546eSJason Gunthorpe 		int access, const struct mmu_interval_notifier_ops *ops);
88c320e527SMoni Shoua struct ib_umem_odp *ib_umem_odp_alloc_implicit(struct ib_device *device,
89f20bef6aSJason Gunthorpe 					       int access);
90f25a546eSJason Gunthorpe struct ib_umem_odp *
91f25a546eSJason Gunthorpe ib_umem_odp_alloc_child(struct ib_umem_odp *root_umem, unsigned long addr,
92f25a546eSJason Gunthorpe 			size_t size,
93f25a546eSJason Gunthorpe 			const struct mmu_interval_notifier_ops *ops);
94b5231b01SJason Gunthorpe void ib_umem_odp_release(struct ib_umem_odp *umem_odp);
958ada2c1cSShachar Raindel 
9636f30e48SYishai Hadas int ib_umem_odp_map_dma_and_lock(struct ib_umem_odp *umem_odp, u64 start_offset,
978bfafde0SYishai Hadas 				 u64 bcnt, u64 access_mask, bool fault);
988ada2c1cSShachar Raindel 
99b5231b01SJason Gunthorpe void ib_umem_odp_unmap_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset,
1008ada2c1cSShachar Raindel 				 u64 bound);
1018ada2c1cSShachar Raindel 
1028ada2c1cSShachar Raindel #else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
1038ada2c1cSShachar Raindel 
104f25a546eSJason Gunthorpe static inline struct ib_umem_odp *
ib_umem_odp_get(struct ib_device * device,unsigned long addr,size_t size,int access,const struct mmu_interval_notifier_ops * ops)105c320e527SMoni Shoua ib_umem_odp_get(struct ib_device *device, unsigned long addr, size_t size,
106f25a546eSJason Gunthorpe 		int access, const struct mmu_interval_notifier_ops *ops)
1078ada2c1cSShachar Raindel {
108261dc53fSJason Gunthorpe 	return ERR_PTR(-EINVAL);
1098ada2c1cSShachar Raindel }
1108ada2c1cSShachar Raindel 
ib_umem_odp_release(struct ib_umem_odp * umem_odp)111b5231b01SJason Gunthorpe static inline void ib_umem_odp_release(struct ib_umem_odp *umem_odp) {}
1128ada2c1cSShachar Raindel 
1138ada2c1cSShachar Raindel #endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
1148ada2c1cSShachar Raindel 
1158ada2c1cSShachar Raindel #endif /* IB_UMEM_ODP_H */
116