1 /*- 2 * SPDX-License-Identifier: GPL-2.0 or Linux-OpenIB 3 * 4 * Copyright (c) 2015 - 2023 Intel Corporation 5 * 6 * This software is available to you under a choice of one of two 7 * licenses. You may choose to be licensed under the terms of the GNU 8 * General Public License (GPL) Version 2, available from the file 9 * COPYING in the main directory of this source tree, or the 10 * OpenFabrics.org BSD license below: 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above 17 * copyright notice, this list of conditions and the following 18 * disclaimer. 19 * 20 * - Redistributions in binary form must reproduce the above 21 * copyright notice, this list of conditions and the following 22 * disclaimer in the documentation and/or other materials 23 * provided with the distribution. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 * SOFTWARE. 33 */ 34 35 #ifndef IRDMA_PBLE_H 36 #define IRDMA_PBLE_H 37 38 #define PBLE_SHIFT 6 39 #define PBLE_PER_PAGE 512 40 #define HMC_PAGED_BP_SHIFT 12 41 #define PBLE_512_SHIFT 9 42 #define PBLE_INVALID_IDX 0xffffffff 43 44 enum irdma_pble_level { 45 PBLE_LEVEL_0 = 0, 46 PBLE_LEVEL_1 = 1, 47 PBLE_LEVEL_2 = 2, 48 }; 49 50 enum irdma_alloc_type { 51 PBLE_NO_ALLOC = 0, 52 PBLE_SD_CONTIGOUS = 1, 53 PBLE_SD_PAGED = 2, 54 }; 55 56 struct irdma_chunk; 57 58 struct irdma_pble_chunkinfo { 59 struct irdma_chunk *pchunk; 60 u64 bit_idx; 61 u64 bits_used; 62 }; 63 64 struct irdma_pble_info { 65 u64 *addr; 66 u32 idx; 67 u32 cnt; 68 struct irdma_pble_chunkinfo chunkinfo; 69 }; 70 71 struct irdma_pble_level2 { 72 struct irdma_pble_info root; 73 struct irdma_pble_info *leaf; 74 struct irdma_virt_mem leafmem; 75 u32 leaf_cnt; 76 }; 77 78 struct irdma_pble_alloc { 79 u32 total_cnt; 80 enum irdma_pble_level level; 81 union { 82 struct irdma_pble_info level1; 83 struct irdma_pble_level2 level2; 84 }; 85 }; 86 87 struct sd_pd_idx { 88 u32 sd_idx; 89 u32 pd_idx; 90 u32 rel_pd_idx; 91 }; 92 93 struct irdma_add_page_info { 94 struct irdma_chunk *chunk; 95 struct irdma_hmc_sd_entry *sd_entry; 96 struct irdma_hmc_info *hmc_info; 97 struct sd_pd_idx idx; 98 u32 pages; 99 }; 100 101 struct irdma_chunk { 102 struct list_head list; 103 struct irdma_dma_info dmainfo; 104 unsigned long *bitmapbuf; 105 106 u32 sizeofbitmap; 107 u64 size; 108 void *vaddr; 109 u64 fpm_addr; 110 u32 pg_cnt; 111 enum irdma_alloc_type type; 112 struct irdma_sc_dev *dev; 113 struct irdma_virt_mem chunkmem; 114 }; 115 116 struct irdma_pble_prm { 117 struct list_head clist; 118 spinlock_t prm_lock; /* protect prm bitmap */ 119 u64 total_pble_alloc; 120 u64 free_pble_cnt; 121 u8 pble_shift; 122 }; 123 124 struct irdma_hmc_pble_rsrc { 125 u32 unallocated_pble; 126 struct mutex pble_mutex_lock; /* protect PBLE resource */ 127 struct irdma_sc_dev *dev; 128 u64 fpm_base_addr; 129 u64 next_fpm_addr; 130 struct irdma_pble_prm pinfo; 131 u64 allocdpbles; 132 u64 freedpbles; 133 u32 stats_direct_sds; 134 u32 stats_paged_sds; 135 u64 stats_alloc_ok; 136 u64 stats_alloc_fail; 137 u64 stats_alloc_freed; 138 u64 stats_lvl1; 139 u64 stats_lvl2; 140 }; 141 142 void irdma_destroy_pble_prm(struct irdma_hmc_pble_rsrc *pble_rsrc); 143 int irdma_hmc_init_pble(struct irdma_sc_dev *dev, 144 struct irdma_hmc_pble_rsrc *pble_rsrc); 145 void irdma_free_pble(struct irdma_hmc_pble_rsrc *pble_rsrc, 146 struct irdma_pble_alloc *palloc); 147 int irdma_get_pble(struct irdma_hmc_pble_rsrc *pble_rsrc, 148 struct irdma_pble_alloc *palloc, u32 pble_cnt, 149 u8 lvl); 150 int irdma_prm_add_pble_mem(struct irdma_pble_prm *pprm, 151 struct irdma_chunk *pchunk); 152 int irdma_prm_get_pbles(struct irdma_pble_prm *pprm, 153 struct irdma_pble_chunkinfo *chunkinfo, u64 mem_size, 154 u64 **vaddr, u64 *fpm_addr); 155 void irdma_prm_return_pbles(struct irdma_pble_prm *pprm, 156 struct irdma_pble_chunkinfo *chunkinfo); 157 void irdma_pble_acquire_lock(struct irdma_hmc_pble_rsrc *pble_rsrc, 158 unsigned long *flags); 159 void irdma_pble_release_lock(struct irdma_hmc_pble_rsrc *pble_rsrc, 160 unsigned long *flags); 161 void irdma_pble_free_paged_mem(struct irdma_chunk *chunk); 162 int irdma_pble_get_paged_mem(struct irdma_chunk *chunk, u32 pg_cnt); 163 #endif /* IRDMA_PBLE_H */ 164