blob: 5c39b2183f80fa7e9d33c22cfe673d4e10d33370 [file] [log] [blame]
Damjan Marion94100532020-11-06 23:25:57 +01001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#ifndef included_clib_interrupt_h
17#define included_clib_interrupt_h
18
19#include <vppinfra/clib.h>
Damjan Marion94100532020-11-06 23:25:57 +010020#include <vppinfra/vec.h>
21
22typedef struct
23{
24 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
25 int n_int;
26 uword n_uword_alloc;
27} clib_interrupt_header_t;
28
29void clib_interrupt_init (void **data, uword n_interrupts);
30void clib_interrupt_resize (void **data, uword n_interrupts);
31
32static_always_inline void
33clib_interrupt_free (void **data)
34{
35 if (data[0])
36 {
37 clib_mem_free (data[0]);
38 data[0] = 0;
39 }
40}
41
42static_always_inline int
43clib_interrupt_get_n_int (void *d)
44{
45 clib_interrupt_header_t *h = d;
46 if (h)
47 return h->n_int;
48 return 0;
49}
50
51static_always_inline uword *
52clib_interrupt_get_bitmap (void *d)
53{
54 return d + sizeof (clib_interrupt_header_t);
55}
56
57static_always_inline uword *
58clib_interrupt_get_atomic_bitmap (void *d)
59{
60 clib_interrupt_header_t *h = d;
61 return clib_interrupt_get_bitmap (d) + h->n_uword_alloc;
62}
63
64static_always_inline void
65clib_interrupt_set (void *in, int int_num)
66{
67 uword *bmp = clib_interrupt_get_bitmap (in);
68 uword mask = 1ULL << (int_num & (uword_bits - 1));
69 bmp += int_num >> log2_uword_bits;
70
71 ASSERT (int_num < ((clib_interrupt_header_t *) in)->n_int);
72
73 *bmp |= mask;
74}
75
76static_always_inline void
77clib_interrupt_set_atomic (void *in, int int_num)
78{
79 uword *bmp = clib_interrupt_get_atomic_bitmap (in);
80 uword mask = 1ULL << (int_num & (uword_bits - 1));
81 bmp += int_num >> log2_uword_bits;
82
83 ASSERT (int_num < ((clib_interrupt_header_t *) in)->n_int);
84
85 __atomic_fetch_or (bmp, mask, __ATOMIC_RELAXED);
86}
87
88static_always_inline void
89clib_interrupt_clear (void *in, int int_num)
90{
91 uword *bmp = clib_interrupt_get_bitmap (in);
92 uword *abm = clib_interrupt_get_atomic_bitmap (in);
93 uword mask = 1ULL << (int_num & (uword_bits - 1));
94 uword off = int_num >> log2_uword_bits;
95
96 ASSERT (int_num < ((clib_interrupt_header_t *) in)->n_int);
97
98 bmp[off] |= __atomic_exchange_n (abm + off, 0, __ATOMIC_SEQ_CST);
99 bmp[off] &= ~mask;
100}
101
102static_always_inline int
103clib_interrupt_get_next (void *in, int last)
104{
105 uword *bmp = clib_interrupt_get_bitmap (in);
106 uword *abm = clib_interrupt_get_atomic_bitmap (in);
107 clib_interrupt_header_t *h = in;
108 uword bmp_uword, off;
109
110 ASSERT (last >= -1 && last < h->n_int);
111
112 off = (last + 1) >> log2_uword_bits;
Paul Atkinsdfc43162022-04-06 14:51:21 +0100113 if (off > h->n_int >> log2_uword_bits || off >= h->n_uword_alloc)
114 return -1;
Damjan Marion94100532020-11-06 23:25:57 +0100115
116 last -= off << log2_uword_bits;
117 bmp[off] |= __atomic_exchange_n (abm + off, 0, __ATOMIC_SEQ_CST);
118 bmp_uword = bmp[off] & ~pow2_mask (last + 1);
119
120next:
121 if (bmp_uword)
122 return (off << log2_uword_bits) + count_trailing_zeros (bmp_uword);
123
124 off++;
125
Paul Atkinsdfc43162022-04-06 14:51:21 +0100126 if (off > h->n_int >> log2_uword_bits || off >= h->n_uword_alloc)
Damjan Marion94100532020-11-06 23:25:57 +0100127 return -1;
128
129 bmp[off] |= __atomic_exchange_n (abm + off, 0, __ATOMIC_SEQ_CST);
130 bmp_uword = bmp[off];
131
132 goto next;
133}
134
135#endif /* included_clib_interrupt_h */
136
137/*
138 * fd.io coding-style-patch-verification: ON
139 *
140 * Local Variables:
141 * eval: (c-set-style "gnu")
142 * End:
143 */