* INTC Contribution to the O-RAN F Release for O-DU Low
[o-du/phy.git] / fapi_5g / source / api / fapi2phy / p7 / nr5g_fapi_proc_tti_req_common.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2021 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19 #include "nr5g_fapi_framework.h"
20 #include "nr5g_fapi_fapi2phy_p7_pvt_proc.h"
21
22 #define FAPI_EMPTY_RB_BITMAP_MASK (0u)
23 #define FAPI_EMPTY_RBG_INDEX (0u)
24 #define FAPI_MAX_RB_BIT_NUM (273u)
25
26 static uint16_t nr5g_fapi_rb_bitmap_mask(
27     uint8_t rbg_size)
28 {
29     switch (rbg_size) {
30         case 2:
31             return 0x3u;
32         case 4:
33             return 0xFu;
34         case 8:
35             return 0xFFu;
36         case 16:
37             return 0xFFFFu;
38         default:
39             return FAPI_EMPTY_RB_BITMAP_MASK;
40     }
41 }
42
43 uint16_t nr5g_fapi_get_rb_bits_for_rbg(
44     const uint8_t rb_bitmap[FAPI_RB_BITMAP_SIZE],
45     uint32_t nth_rbg_bit,
46     uint8_t rbg_size,
47     uint16_t rb_bitmap_mask)
48 {
49     uint16_t rb_bits = 0u;
50     const uint16_t rb_bits_bit_size = sizeof(rb_bits) * CHAR_BIT;
51     const uint16_t nth_rb_bit = nth_rbg_bit * rbg_size;
52     const uint32_t rb_byte_1 = ( nth_rb_bit / rb_bits_bit_size ) * sizeof(rb_bits);
53     const uint32_t rb_byte_2 = rb_byte_1 + 1u;
54     if (rb_byte_1 < FAPI_RB_BITMAP_SIZE)
55     {
56         rb_bits |= rb_bitmap[rb_byte_1];
57     }
58     if (rb_byte_2 < FAPI_RB_BITMAP_SIZE)
59     {
60         rb_bits |= (rb_bitmap[rb_byte_2] << CHAR_BIT);
61     }
62     const uint32_t local_rbg_idx = nth_rb_bit % rb_bits_bit_size;
63     return (rb_bits >> local_rbg_idx) & rb_bitmap_mask;
64 }
65
66 static bool nr5g_fapi_has_rbg_bits_in_rb_bitmap(
67     const uint8_t rb_bitmap[FAPI_RB_BITMAP_SIZE],
68     const uint16_t rbg_bit,
69     const uint8_t rbg_size,
70     const uint16_t rb_bitmap_mask)
71 {
72     const uint16_t rb_bits_for_rbg = nr5g_fapi_get_rb_bits_for_rbg(
73         rb_bitmap, rbg_bit, rbg_size, rb_bitmap_mask);
74     if (rb_bitmap_mask == rb_bits_for_rbg)
75     {
76         return true;
77     }
78     else if (FAPI_EMPTY_RB_BITMAP_MASK != rb_bits_for_rbg)
79     {
80         NR5G_FAPI_LOG(ERROR_LOG, ("rb_bits don not match rb_bitmap_mask."
81             " rbg_size %u rbg_bit=%u rb_bits_for_rbg=%#X rb_bitmap_mask=%#X",
82             rbg_size, rbg_bit, rb_bits_for_rbg, rb_bitmap_mask));
83     }
84     return false;
85 }
86
87 /** @ingroup group_source_api_p7_fapi2phy_proc
88  *
89  *  @param[in]  rb_bitmap Pointer to FAPI DL resource block bitmap.
90  *  @param[in]  bwp_start Value of bandwidth partition start.
91  *  @param[in]  bwp_size Value of bandwidth partition size.
92  *  @param[in]  rbg_size Value of resource block group size.
93  *  @param[in]  get_rbg_index_mask Function placing bit in rbgIndex (bit order).
94  *
95  *  @return     Returns IAPI nRBGIndex
96  *
97  *  @description
98  *  See TS 138 214 5.1.2.2.1/6.1.2.2.1 for more info.
99  *  IAPI uses bit per Resource Block Group,
100  *  FAPI uses bit per Virtual Resource Block.
101  *  Therefore 1 nRBGIndex bit (IAPI), maps to nRBGSize bits (FAPI)
102  *  Bitmaps mappings representation:
103  *  IAPI: nRBGIndex    = RBG-0................RBG-17 (for PDSCH MSB to LSB,
104  *                                                    for PUSCH LSB to MSB)
105  *  FAPI  RB-0...RB-272:
106  *  FAPI  rbBitmap[i]  = RB-(7+i*8)...........RB-(0+i*8) (MSB to LSB)
107  *
108 **/
109 uint32_t nr5g_fapi_calc_rbg_index(
110     const uint8_t rb_bitmap[FAPI_RB_BITMAP_SIZE],
111     uint16_t bwp_start,
112     uint16_t bwp_size,
113     uint32_t(*get_rbg_index_mask)(uint32_t nth_bit))
114 {
115     const uint8_t rbg_size = nr5g_fapi_calc_n_rbg_size(bwp_size);
116     const uint16_t rb_bitmap_mask = nr5g_fapi_rb_bitmap_mask(rbg_size);
117     if (FAPI_EMPTY_RB_BITMAP_MASK == rb_bitmap_mask)
118     {
119         NR5G_FAPI_LOG(ERROR_LOG, ("Wrong rbg_size=%u. rbg_index set to 0.",
120             rbg_size));
121         return FAPI_EMPTY_RBG_INDEX;
122     }
123     if (bwp_start >= FAPI_MAX_RB_BIT_NUM)
124     {
125         NR5G_FAPI_LOG(ERROR_LOG, ("Wrong bwp_start=%u. rbg_index set to 0.",
126             bwp_start));
127         return FAPI_EMPTY_RBG_INDEX;
128     }
129
130     const uint16_t rbg_bit_begin = bwp_start / rbg_size;
131     const uint16_t rb_bit_end = fmin(FAPI_MAX_RB_BIT_NUM, bwp_start + bwp_size);
132     const uint16_t rbg_bit_last = ceil((double)rb_bit_end / rbg_size) - 1u;
133
134     const uint16_t start_offset = bwp_start % rbg_size;
135     uint16_t rb_bitmap_mask_1st_rbg =
136         rb_bitmap_mask & (rb_bitmap_mask << start_offset);
137     const uint16_t last_rbg_size =
138         (0u == rb_bit_end % rbg_size) ? rbg_size : rb_bit_end % rbg_size;
139     const uint16_t end_offset = rbg_size - last_rbg_size;
140     uint16_t rb_bitmap_mask_last_rbg = rb_bitmap_mask >> end_offset;
141     if (rbg_bit_begin == rbg_bit_last)
142     {
143         const uint16_t mask = rb_bitmap_mask_1st_rbg & rb_bitmap_mask_last_rbg;
144         rb_bitmap_mask_1st_rbg = mask;
145         rb_bitmap_mask_last_rbg = mask;
146     }
147
148     uint32_t result = 0u;
149     // fill 1st rbg
150     if (nr5g_fapi_has_rbg_bits_in_rb_bitmap(
151         rb_bitmap, rbg_bit_begin, rbg_size, rb_bitmap_mask_1st_rbg))
152     {
153         result |= get_rbg_index_mask(rbg_bit_begin);
154     }
155     // fill last rbg
156     if (nr5g_fapi_has_rbg_bits_in_rb_bitmap(
157         rb_bitmap, rbg_bit_last, rbg_size, rb_bitmap_mask_last_rbg))
158     {
159         result |= get_rbg_index_mask(rbg_bit_last);
160     }
161     // fill rest of rbgs
162     uint8_t rbg_bit;
163     for (rbg_bit = rbg_bit_begin + 1u; rbg_bit < rbg_bit_last; rbg_bit++)
164     {
165         if (nr5g_fapi_has_rbg_bits_in_rb_bitmap(
166             rb_bitmap, rbg_bit, rbg_size, rb_bitmap_mask))
167         {
168             result |= get_rbg_index_mask(rbg_bit);
169         }
170     }
171
172     return result;
173 }
174
175  /** @ingroup group_source_api_p7_fapi2phy_proc
176  *
177  *  @param[in]  bwp_size  Variable holding the Bandwidth part size.
178  *
179  *  @return     Returns ::RBG Size.
180  *
181  *  @description
182  *  This functions calculates and return RBG Size from Bandwidth part size
183  *  provided.
184  *
185 **/
186 uint8_t nr5g_fapi_calc_n_rbg_size(
187     uint16_t bwp_size)
188 {
189     uint8_t n_rbg_size = 0;
190     if (bwp_size >= 1 && bwp_size <= 36) {
191         n_rbg_size = 2;
192     } else if (bwp_size >= 37 && bwp_size <= 72) {
193         n_rbg_size = 4;
194     } else if (bwp_size >= 73 && bwp_size <= 144) {
195         n_rbg_size = 8;
196     } else if (bwp_size >= 145 && bwp_size <= 275) {
197         n_rbg_size = 16;
198     } else {
199         n_rbg_size = 0;
200     }
201     return n_rbg_size;
202 }