162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2007 Cisco Systems.  All rights reserved.
462306a36Sopenharmony_ci * Copyright (c) 2020 Intel Corporation.  All rights reserved.
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef IB_UMEM_H
862306a36Sopenharmony_ci#define IB_UMEM_H
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/list.h>
1162306a36Sopenharmony_ci#include <linux/scatterlist.h>
1262306a36Sopenharmony_ci#include <linux/workqueue.h>
1362306a36Sopenharmony_ci#include <rdma/ib_verbs.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_cistruct ib_ucontext;
1662306a36Sopenharmony_cistruct ib_umem_odp;
1762306a36Sopenharmony_cistruct dma_buf_attach_ops;
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistruct ib_umem {
2062306a36Sopenharmony_ci	struct ib_device       *ibdev;
2162306a36Sopenharmony_ci	struct mm_struct       *owning_mm;
2262306a36Sopenharmony_ci	u64 iova;
2362306a36Sopenharmony_ci	size_t			length;
2462306a36Sopenharmony_ci	unsigned long		address;
2562306a36Sopenharmony_ci	u32 writable : 1;
2662306a36Sopenharmony_ci	u32 is_odp : 1;
2762306a36Sopenharmony_ci	u32 is_dmabuf : 1;
2862306a36Sopenharmony_ci	struct sg_append_table sgt_append;
2962306a36Sopenharmony_ci};
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cistruct ib_umem_dmabuf {
3262306a36Sopenharmony_ci	struct ib_umem umem;
3362306a36Sopenharmony_ci	struct dma_buf_attachment *attach;
3462306a36Sopenharmony_ci	struct sg_table *sgt;
3562306a36Sopenharmony_ci	struct scatterlist *first_sg;
3662306a36Sopenharmony_ci	struct scatterlist *last_sg;
3762306a36Sopenharmony_ci	unsigned long first_sg_offset;
3862306a36Sopenharmony_ci	unsigned long last_sg_trim;
3962306a36Sopenharmony_ci	void *private;
4062306a36Sopenharmony_ci	u8 pinned : 1;
4162306a36Sopenharmony_ci};
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistatic inline struct ib_umem_dmabuf *to_ib_umem_dmabuf(struct ib_umem *umem)
4462306a36Sopenharmony_ci{
4562306a36Sopenharmony_ci	return container_of(umem, struct ib_umem_dmabuf, umem);
4662306a36Sopenharmony_ci}
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/* Returns the offset of the umem start relative to the first page. */
4962306a36Sopenharmony_cistatic inline int ib_umem_offset(struct ib_umem *umem)
5062306a36Sopenharmony_ci{
5162306a36Sopenharmony_ci	return umem->address & ~PAGE_MASK;
5262306a36Sopenharmony_ci}
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_cistatic inline unsigned long ib_umem_dma_offset(struct ib_umem *umem,
5562306a36Sopenharmony_ci					       unsigned long pgsz)
5662306a36Sopenharmony_ci{
5762306a36Sopenharmony_ci	return (sg_dma_address(umem->sgt_append.sgt.sgl) + ib_umem_offset(umem)) &
5862306a36Sopenharmony_ci	       (pgsz - 1);
5962306a36Sopenharmony_ci}
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_cistatic inline size_t ib_umem_num_dma_blocks(struct ib_umem *umem,
6262306a36Sopenharmony_ci					    unsigned long pgsz)
6362306a36Sopenharmony_ci{
6462306a36Sopenharmony_ci	return (size_t)((ALIGN(umem->iova + umem->length, pgsz) -
6562306a36Sopenharmony_ci			 ALIGN_DOWN(umem->iova, pgsz))) /
6662306a36Sopenharmony_ci	       pgsz;
6762306a36Sopenharmony_ci}
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_cistatic inline size_t ib_umem_num_pages(struct ib_umem *umem)
7062306a36Sopenharmony_ci{
7162306a36Sopenharmony_ci	return ib_umem_num_dma_blocks(umem, PAGE_SIZE);
7262306a36Sopenharmony_ci}
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_cistatic inline void __rdma_umem_block_iter_start(struct ib_block_iter *biter,
7562306a36Sopenharmony_ci						struct ib_umem *umem,
7662306a36Sopenharmony_ci						unsigned long pgsz)
7762306a36Sopenharmony_ci{
7862306a36Sopenharmony_ci	__rdma_block_iter_start(biter, umem->sgt_append.sgt.sgl,
7962306a36Sopenharmony_ci				umem->sgt_append.sgt.nents, pgsz);
8062306a36Sopenharmony_ci	biter->__sg_advance = ib_umem_offset(umem) & ~(pgsz - 1);
8162306a36Sopenharmony_ci	biter->__sg_numblocks = ib_umem_num_dma_blocks(umem, pgsz);
8262306a36Sopenharmony_ci}
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_cistatic inline bool __rdma_umem_block_iter_next(struct ib_block_iter *biter)
8562306a36Sopenharmony_ci{
8662306a36Sopenharmony_ci	return __rdma_block_iter_next(biter) && biter->__sg_numblocks--;
8762306a36Sopenharmony_ci}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci/**
9062306a36Sopenharmony_ci * rdma_umem_for_each_dma_block - iterate over contiguous DMA blocks of the umem
9162306a36Sopenharmony_ci * @umem: umem to iterate over
9262306a36Sopenharmony_ci * @pgsz: Page size to split the list into
9362306a36Sopenharmony_ci *
9462306a36Sopenharmony_ci * pgsz must be <= PAGE_SIZE or computed by ib_umem_find_best_pgsz(). The
9562306a36Sopenharmony_ci * returned DMA blocks will be aligned to pgsz and span the range:
9662306a36Sopenharmony_ci * ALIGN_DOWN(umem->address, pgsz) to ALIGN(umem->address + umem->length, pgsz)
9762306a36Sopenharmony_ci *
9862306a36Sopenharmony_ci * Performs exactly ib_umem_num_dma_blocks() iterations.
9962306a36Sopenharmony_ci */
10062306a36Sopenharmony_ci#define rdma_umem_for_each_dma_block(umem, biter, pgsz)                        \
10162306a36Sopenharmony_ci	for (__rdma_umem_block_iter_start(biter, umem, pgsz);                  \
10262306a36Sopenharmony_ci	     __rdma_umem_block_iter_next(biter);)
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci#ifdef CONFIG_INFINIBAND_USER_MEM
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistruct ib_umem *ib_umem_get(struct ib_device *device, unsigned long addr,
10762306a36Sopenharmony_ci			    size_t size, int access);
10862306a36Sopenharmony_civoid ib_umem_release(struct ib_umem *umem);
10962306a36Sopenharmony_ciint ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
11062306a36Sopenharmony_ci		      size_t length);
11162306a36Sopenharmony_ciunsigned long ib_umem_find_best_pgsz(struct ib_umem *umem,
11262306a36Sopenharmony_ci				     unsigned long pgsz_bitmap,
11362306a36Sopenharmony_ci				     unsigned long virt);
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci/**
11662306a36Sopenharmony_ci * ib_umem_find_best_pgoff - Find best HW page size
11762306a36Sopenharmony_ci *
11862306a36Sopenharmony_ci * @umem: umem struct
11962306a36Sopenharmony_ci * @pgsz_bitmap bitmap of HW supported page sizes
12062306a36Sopenharmony_ci * @pgoff_bitmask: Mask of bits that can be represented with an offset
12162306a36Sopenharmony_ci *
12262306a36Sopenharmony_ci * This is very similar to ib_umem_find_best_pgsz() except instead of accepting
12362306a36Sopenharmony_ci * an IOVA it accepts a bitmask specifying what address bits can be represented
12462306a36Sopenharmony_ci * with a page offset.
12562306a36Sopenharmony_ci *
12662306a36Sopenharmony_ci * For instance if the HW has multiple page sizes, requires 64 byte alignemnt,
12762306a36Sopenharmony_ci * and can support aligned offsets up to 4032 then pgoff_bitmask would be
12862306a36Sopenharmony_ci * "111111000000".
12962306a36Sopenharmony_ci *
13062306a36Sopenharmony_ci * If the pgoff_bitmask requires either alignment in the low bit or an
13162306a36Sopenharmony_ci * unavailable page size for the high bits, this function returns 0.
13262306a36Sopenharmony_ci */
13362306a36Sopenharmony_cistatic inline unsigned long ib_umem_find_best_pgoff(struct ib_umem *umem,
13462306a36Sopenharmony_ci						    unsigned long pgsz_bitmap,
13562306a36Sopenharmony_ci						    u64 pgoff_bitmask)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	struct scatterlist *sg = umem->sgt_append.sgt.sgl;
13862306a36Sopenharmony_ci	dma_addr_t dma_addr;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	dma_addr = sg_dma_address(sg) + (umem->address & ~PAGE_MASK);
14162306a36Sopenharmony_ci	return ib_umem_find_best_pgsz(umem, pgsz_bitmap,
14262306a36Sopenharmony_ci				      dma_addr & pgoff_bitmask);
14362306a36Sopenharmony_ci}
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_cistruct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device,
14662306a36Sopenharmony_ci					  unsigned long offset, size_t size,
14762306a36Sopenharmony_ci					  int fd, int access,
14862306a36Sopenharmony_ci					  const struct dma_buf_attach_ops *ops);
14962306a36Sopenharmony_cistruct ib_umem_dmabuf *ib_umem_dmabuf_get_pinned(struct ib_device *device,
15062306a36Sopenharmony_ci						 unsigned long offset,
15162306a36Sopenharmony_ci						 size_t size, int fd,
15262306a36Sopenharmony_ci						 int access);
15362306a36Sopenharmony_ciint ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf *umem_dmabuf);
15462306a36Sopenharmony_civoid ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf *umem_dmabuf);
15562306a36Sopenharmony_civoid ib_umem_dmabuf_release(struct ib_umem_dmabuf *umem_dmabuf);
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci#else /* CONFIG_INFINIBAND_USER_MEM */
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci#include <linux/err.h>
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_cistatic inline struct ib_umem *ib_umem_get(struct ib_device *device,
16262306a36Sopenharmony_ci					  unsigned long addr, size_t size,
16362306a36Sopenharmony_ci					  int access)
16462306a36Sopenharmony_ci{
16562306a36Sopenharmony_ci	return ERR_PTR(-EOPNOTSUPP);
16662306a36Sopenharmony_ci}
16762306a36Sopenharmony_cistatic inline void ib_umem_release(struct ib_umem *umem) { }
16862306a36Sopenharmony_cistatic inline int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
16962306a36Sopenharmony_ci		      		    size_t length) {
17062306a36Sopenharmony_ci	return -EOPNOTSUPP;
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_cistatic inline unsigned long ib_umem_find_best_pgsz(struct ib_umem *umem,
17362306a36Sopenharmony_ci						   unsigned long pgsz_bitmap,
17462306a36Sopenharmony_ci						   unsigned long virt)
17562306a36Sopenharmony_ci{
17662306a36Sopenharmony_ci	return 0;
17762306a36Sopenharmony_ci}
17862306a36Sopenharmony_cistatic inline unsigned long ib_umem_find_best_pgoff(struct ib_umem *umem,
17962306a36Sopenharmony_ci						    unsigned long pgsz_bitmap,
18062306a36Sopenharmony_ci						    u64 pgoff_bitmask)
18162306a36Sopenharmony_ci{
18262306a36Sopenharmony_ci	return 0;
18362306a36Sopenharmony_ci}
18462306a36Sopenharmony_cistatic inline
18562306a36Sopenharmony_cistruct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device,
18662306a36Sopenharmony_ci					  unsigned long offset,
18762306a36Sopenharmony_ci					  size_t size, int fd,
18862306a36Sopenharmony_ci					  int access,
18962306a36Sopenharmony_ci					  struct dma_buf_attach_ops *ops)
19062306a36Sopenharmony_ci{
19162306a36Sopenharmony_ci	return ERR_PTR(-EOPNOTSUPP);
19262306a36Sopenharmony_ci}
19362306a36Sopenharmony_cistatic inline struct ib_umem_dmabuf *
19462306a36Sopenharmony_ciib_umem_dmabuf_get_pinned(struct ib_device *device, unsigned long offset,
19562306a36Sopenharmony_ci			  size_t size, int fd, int access)
19662306a36Sopenharmony_ci{
19762306a36Sopenharmony_ci	return ERR_PTR(-EOPNOTSUPP);
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_cistatic inline int ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf *umem_dmabuf)
20062306a36Sopenharmony_ci{
20162306a36Sopenharmony_ci	return -EOPNOTSUPP;
20262306a36Sopenharmony_ci}
20362306a36Sopenharmony_cistatic inline void ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf *umem_dmabuf) { }
20462306a36Sopenharmony_cistatic inline void ib_umem_dmabuf_release(struct ib_umem_dmabuf *umem_dmabuf) { }
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci#endif /* CONFIG_INFINIBAND_USER_MEM */
20762306a36Sopenharmony_ci#endif /* IB_UMEM_H */
208