162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (c) 2014 Mellanox Technologies. All rights reserved. 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci#ifndef IB_UMEM_ODP_H 762306a36Sopenharmony_ci#define IB_UMEM_ODP_H 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <rdma/ib_umem.h> 1062306a36Sopenharmony_ci#include <rdma/ib_verbs.h> 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_cistruct ib_umem_odp { 1362306a36Sopenharmony_ci struct ib_umem umem; 1462306a36Sopenharmony_ci struct mmu_interval_notifier notifier; 1562306a36Sopenharmony_ci struct pid *tgid; 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci /* An array of the pfns included in the on-demand paging umem. */ 1862306a36Sopenharmony_ci unsigned long *pfn_list; 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci /* 2162306a36Sopenharmony_ci * An array with DMA addresses mapped for pfns in pfn_list. 2262306a36Sopenharmony_ci * The lower two bits designate access permissions. 2362306a36Sopenharmony_ci * See ODP_READ_ALLOWED_BIT and ODP_WRITE_ALLOWED_BIT. 2462306a36Sopenharmony_ci */ 2562306a36Sopenharmony_ci dma_addr_t *dma_list; 2662306a36Sopenharmony_ci /* 2762306a36Sopenharmony_ci * The umem_mutex protects the page_list and dma_list fields of an ODP 2862306a36Sopenharmony_ci * umem, allowing only a single thread to map/unmap pages. The mutex 2962306a36Sopenharmony_ci * also protects access to the mmu notifier counters. 3062306a36Sopenharmony_ci */ 3162306a36Sopenharmony_ci struct mutex umem_mutex; 3262306a36Sopenharmony_ci void *private; /* for the HW driver to use. */ 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci int npages; 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci /* 3762306a36Sopenharmony_ci * An implicit odp umem cannot be DMA mapped, has 0 length, and serves 3862306a36Sopenharmony_ci * only as an anchor for the driver to hold onto the per_mm. FIXME: 3962306a36Sopenharmony_ci * This should be removed and drivers should work with the per_mm 4062306a36Sopenharmony_ci * directly. 4162306a36Sopenharmony_ci */ 4262306a36Sopenharmony_ci bool is_implicit_odp; 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci unsigned int page_shift; 4562306a36Sopenharmony_ci}; 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_cistatic inline struct ib_umem_odp *to_ib_umem_odp(struct ib_umem *umem) 4862306a36Sopenharmony_ci{ 4962306a36Sopenharmony_ci return container_of(umem, struct ib_umem_odp, umem); 5062306a36Sopenharmony_ci} 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci/* Returns the first page of an ODP umem. */ 5362306a36Sopenharmony_cistatic inline unsigned long ib_umem_start(struct ib_umem_odp *umem_odp) 5462306a36Sopenharmony_ci{ 5562306a36Sopenharmony_ci return umem_odp->notifier.interval_tree.start; 5662306a36Sopenharmony_ci} 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci/* Returns the address of the page after the last one of an ODP umem. */ 5962306a36Sopenharmony_cistatic inline unsigned long ib_umem_end(struct ib_umem_odp *umem_odp) 6062306a36Sopenharmony_ci{ 6162306a36Sopenharmony_ci return umem_odp->notifier.interval_tree.last + 1; 6262306a36Sopenharmony_ci} 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_cistatic inline size_t ib_umem_odp_num_pages(struct ib_umem_odp *umem_odp) 6562306a36Sopenharmony_ci{ 6662306a36Sopenharmony_ci return (ib_umem_end(umem_odp) - ib_umem_start(umem_odp)) >> 6762306a36Sopenharmony_ci umem_odp->page_shift; 6862306a36Sopenharmony_ci} 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci/* 7162306a36Sopenharmony_ci * The lower 2 bits of the DMA address signal the R/W permissions for 7262306a36Sopenharmony_ci * the entry. To upgrade the permissions, provide the appropriate 7362306a36Sopenharmony_ci * bitmask to the map_dma_pages function. 7462306a36Sopenharmony_ci * 7562306a36Sopenharmony_ci * Be aware that upgrading a mapped address might result in change of 7662306a36Sopenharmony_ci * the DMA address for the page. 7762306a36Sopenharmony_ci */ 7862306a36Sopenharmony_ci#define ODP_READ_ALLOWED_BIT (1<<0ULL) 7962306a36Sopenharmony_ci#define ODP_WRITE_ALLOWED_BIT (1<<1ULL) 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci#define ODP_DMA_ADDR_MASK (~(ODP_READ_ALLOWED_BIT | ODP_WRITE_ALLOWED_BIT)) 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_cistruct ib_umem_odp * 8662306a36Sopenharmony_ciib_umem_odp_get(struct ib_device *device, unsigned long addr, size_t size, 8762306a36Sopenharmony_ci int access, const struct mmu_interval_notifier_ops *ops); 8862306a36Sopenharmony_cistruct ib_umem_odp *ib_umem_odp_alloc_implicit(struct ib_device *device, 8962306a36Sopenharmony_ci int access); 9062306a36Sopenharmony_cistruct ib_umem_odp * 9162306a36Sopenharmony_ciib_umem_odp_alloc_child(struct ib_umem_odp *root_umem, unsigned long addr, 9262306a36Sopenharmony_ci size_t size, 9362306a36Sopenharmony_ci const struct mmu_interval_notifier_ops *ops); 9462306a36Sopenharmony_civoid ib_umem_odp_release(struct ib_umem_odp *umem_odp); 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ciint ib_umem_odp_map_dma_and_lock(struct ib_umem_odp *umem_odp, u64 start_offset, 9762306a36Sopenharmony_ci u64 bcnt, u64 access_mask, bool fault); 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_civoid ib_umem_odp_unmap_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset, 10062306a36Sopenharmony_ci u64 bound); 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci#else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */ 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_cistatic inline struct ib_umem_odp * 10562306a36Sopenharmony_ciib_umem_odp_get(struct ib_device *device, unsigned long addr, size_t size, 10662306a36Sopenharmony_ci int access, const struct mmu_interval_notifier_ops *ops) 10762306a36Sopenharmony_ci{ 10862306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 10962306a36Sopenharmony_ci} 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_cistatic inline void ib_umem_odp_release(struct ib_umem_odp *umem_odp) {} 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci#endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */ 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci#endif /* IB_UMEM_ODP_H */ 116