2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef included_clib_interrupt_h
17 #define included_clib_interrupt_h
19 #include <vppinfra/clib.h>
20 #include <vppinfra/bitops.h> /* for count_set_bits */
21 #include <vppinfra/vec.h>
25 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
28 } clib_interrupt_header_t;
30 void clib_interrupt_init (void **data, uword n_interrupts);
31 void clib_interrupt_resize (void **data, uword n_interrupts);
33 static_always_inline void
34 clib_interrupt_free (void **data)
38 clib_mem_free (data[0]);
43 static_always_inline int
44 clib_interrupt_get_n_int (void *d)
46 clib_interrupt_header_t *h = d;
52 static_always_inline uword *
53 clib_interrupt_get_bitmap (void *d)
55 return d + sizeof (clib_interrupt_header_t);
58 static_always_inline uword *
59 clib_interrupt_get_atomic_bitmap (void *d)
61 clib_interrupt_header_t *h = d;
62 return clib_interrupt_get_bitmap (d) + h->n_uword_alloc;
65 static_always_inline void
66 clib_interrupt_set (void *in, int int_num)
68 uword *bmp = clib_interrupt_get_bitmap (in);
69 uword mask = 1ULL << (int_num & (uword_bits - 1));
70 bmp += int_num >> log2_uword_bits;
72 ASSERT (int_num < ((clib_interrupt_header_t *) in)->n_int);
77 static_always_inline void
78 clib_interrupt_set_atomic (void *in, int int_num)
80 uword *bmp = clib_interrupt_get_atomic_bitmap (in);
81 uword mask = 1ULL << (int_num & (uword_bits - 1));
82 bmp += int_num >> log2_uword_bits;
84 ASSERT (int_num < ((clib_interrupt_header_t *) in)->n_int);
86 __atomic_fetch_or (bmp, mask, __ATOMIC_RELAXED);
89 static_always_inline void
90 clib_interrupt_clear (void *in, int int_num)
92 uword *bmp = clib_interrupt_get_bitmap (in);
93 uword *abm = clib_interrupt_get_atomic_bitmap (in);
94 uword mask = 1ULL << (int_num & (uword_bits - 1));
95 uword off = int_num >> log2_uword_bits;
97 ASSERT (int_num < ((clib_interrupt_header_t *) in)->n_int);
99 bmp[off] |= __atomic_exchange_n (abm + off, 0, __ATOMIC_SEQ_CST);
103 static_always_inline int
104 clib_interrupt_get_next (void *in, int last)
106 uword *bmp = clib_interrupt_get_bitmap (in);
107 uword *abm = clib_interrupt_get_atomic_bitmap (in);
108 clib_interrupt_header_t *h = in;
109 uword bmp_uword, off;
111 ASSERT (last >= -1 && last < h->n_int);
113 off = (last + 1) >> log2_uword_bits;
115 last -= off << log2_uword_bits;
116 bmp[off] |= __atomic_exchange_n (abm + off, 0, __ATOMIC_SEQ_CST);
117 bmp_uword = bmp[off] & ~pow2_mask (last + 1);
121 return (off << log2_uword_bits) + count_trailing_zeros (bmp_uword);
125 if (off > h->n_int >> log2_uword_bits)
128 bmp[off] |= __atomic_exchange_n (abm + off, 0, __ATOMIC_SEQ_CST);
129 bmp_uword = bmp[off];
134 #endif /* included_clib_interrupt_h */
137 * fd.io coding-style-patch-verification: ON
140 * eval: (c-set-style "gnu")