162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */ 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci/* Authors: Bernard Metzler <bmt@zurich.ibm.com> */ 462306a36Sopenharmony_ci/* Copyright (c) 2008-2019, IBM Corporation */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci#ifndef _SIW_MEM_H 762306a36Sopenharmony_ci#define _SIW_MEM_H 862306a36Sopenharmony_ci 962306a36Sopenharmony_cistruct siw_umem *siw_umem_get(u64 start, u64 len, bool writable); 1062306a36Sopenharmony_civoid siw_umem_release(struct siw_umem *umem, bool dirty); 1162306a36Sopenharmony_cistruct siw_pbl *siw_pbl_alloc(u32 num_buf); 1262306a36Sopenharmony_cidma_addr_t siw_pbl_get_buffer(struct siw_pbl *pbl, u64 off, int *len, int *idx); 1362306a36Sopenharmony_cistruct siw_mem *siw_mem_id2obj(struct siw_device *sdev, int stag_index); 1462306a36Sopenharmony_ciint siw_mem_add(struct siw_device *sdev, struct siw_mem *m); 1562306a36Sopenharmony_ciint siw_invalidate_stag(struct ib_pd *pd, u32 stag); 1662306a36Sopenharmony_ciint siw_check_mem(struct ib_pd *pd, struct siw_mem *mem, u64 addr, 1762306a36Sopenharmony_ci enum ib_access_flags perms, int len); 1862306a36Sopenharmony_ciint siw_check_sge(struct ib_pd *pd, struct siw_sge *sge, 1962306a36Sopenharmony_ci struct siw_mem *mem[], enum ib_access_flags perms, 2062306a36Sopenharmony_ci u32 off, int len); 2162306a36Sopenharmony_civoid siw_wqe_put_mem(struct siw_wqe *wqe, enum siw_opcode op); 2262306a36Sopenharmony_ciint siw_mr_add_mem(struct siw_mr *mr, struct ib_pd *pd, void *mem_obj, 2362306a36Sopenharmony_ci u64 start, u64 len, int rights); 2462306a36Sopenharmony_civoid siw_mr_drop_mem(struct siw_mr *mr); 2562306a36Sopenharmony_civoid siw_free_mem(struct kref *ref); 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_cistatic inline void siw_mem_put(struct siw_mem *mem) 2862306a36Sopenharmony_ci{ 2962306a36Sopenharmony_ci kref_put(&mem->ref, siw_free_mem); 3062306a36Sopenharmony_ci} 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_cistatic inline void siw_unref_mem_sgl(struct siw_mem **mem, unsigned int num_sge) 3362306a36Sopenharmony_ci{ 3462306a36Sopenharmony_ci while (num_sge) { 3562306a36Sopenharmony_ci if (*mem == NULL) 3662306a36Sopenharmony_ci break; 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci siw_mem_put(*mem); 3962306a36Sopenharmony_ci *mem = NULL; 4062306a36Sopenharmony_ci mem++; 4162306a36Sopenharmony_ci num_sge--; 4262306a36Sopenharmony_ci } 4362306a36Sopenharmony_ci} 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci#define CHUNK_SHIFT 9 /* sets number of pages per chunk */ 4662306a36Sopenharmony_ci#define PAGES_PER_CHUNK (_AC(1, UL) << CHUNK_SHIFT) 4762306a36Sopenharmony_ci#define CHUNK_MASK (~(PAGES_PER_CHUNK - 1)) 4862306a36Sopenharmony_ci#define PAGE_CHUNK_SIZE (PAGES_PER_CHUNK * sizeof(struct page *)) 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci/* 5162306a36Sopenharmony_ci * siw_get_upage() 5262306a36Sopenharmony_ci * 5362306a36Sopenharmony_ci * Get page pointer for address on given umem. 5462306a36Sopenharmony_ci * 5562306a36Sopenharmony_ci * @umem: two dimensional list of page pointers 5662306a36Sopenharmony_ci * @addr: user virtual address 5762306a36Sopenharmony_ci */ 5862306a36Sopenharmony_cistatic inline struct page *siw_get_upage(struct siw_umem *umem, u64 addr) 5962306a36Sopenharmony_ci{ 6062306a36Sopenharmony_ci unsigned int page_idx = (addr - umem->fp_addr) >> PAGE_SHIFT, 6162306a36Sopenharmony_ci chunk_idx = page_idx >> CHUNK_SHIFT, 6262306a36Sopenharmony_ci page_in_chunk = page_idx & ~CHUNK_MASK; 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci if (likely(page_idx < umem->num_pages)) 6562306a36Sopenharmony_ci return umem->page_chunk[chunk_idx].plist[page_in_chunk]; 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci return NULL; 6862306a36Sopenharmony_ci} 6962306a36Sopenharmony_ci#endif 70