summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/interrupt.h
blob: b0d7dde272a1d67348f430f673f468bc181558bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* SPDX-License-Identifier: Apache-2.0
 * Copyright (c) 2023 Cisco Systems, Inc.
 */

#ifndef included_clib_interrupt_h
#define included_clib_interrupt_h

#include <vppinfra/clib.h>
#include <vppinfra/vec.h>

typedef struct
{
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
  u32 n_int;
  u32 uwords_allocated;
  u32 uwords_used;
  uword *local;
  uword *remote;
} clib_interrupt_header_t;

void clib_interrupt_init (void **data, u32 n_interrupts);
void clib_interrupt_resize (void **data, u32 n_interrupts);

static_always_inline void
clib_interrupt_free (void **data)
{
  if (data[0])
    {
      clib_mem_free (data[0]);
      data[0] = 0;
    }
}

static_always_inline int
clib_interrupt_get_n_int (void *d)
{
  clib_interrupt_header_t *h = d;
  if (h)
    return h->n_int;
  return 0;
}

static_always_inline void
clib_interrupt_set (void *in, int int_num)
{
  clib_interrupt_header_t *h = in;
  u32 off = int_num >> log2_uword_bits;
  uword bit = 1ULL << (int_num & pow2_mask (log2_uword_bits));

  ASSERT (int_num < h->n_int);

  h->local[off] |= bit;
}

static_always_inline void
clib_interrupt_set_atomic (void *in, int int_num)
{
  clib_interrupt_header_t *h = in;
  u32 off = int_num >> log2_uword_bits;
  uword bit = 1ULL << (int_num & pow2_mask (log2_uword_bits));

  ASSERT (int_num < h->n_int);

  __atomic_fetch_or (h->remote + off, bit, __ATOMIC_RELAXED);
}

static_always_inline void
clib_interrupt_clear (void *in, int int_num)
{
  clib_interrupt_header_t *h = in;
  u32 off = int_num >> log2_uword_bits;
  uword bit = 1ULL << (int_num & pow2_mask (log2_uword_bits));
  uword *loc = h->local;
  uword *rem = h->remote;
  uword v;

  ASSERT (int_num < h->n_int);

  v = loc[off] | __atomic_exchange_n (rem + off, 0, __ATOMIC_SEQ_CST);
  loc[off] = v & ~bit;
}

static_always_inline int
clib_interrupt_get_next_and_clear (void *in, int last)
{
  clib_interrupt_header_t *h = in;
  uword bit, v;
  uword *loc = h->local;
  uword *rem = h->remote;
  u32 off, n_uwords = h->uwords_used;

  ASSERT (last >= -1 && last < (int) h->n_int);

  off = (last + 1) >> log2_uword_bits;

  if (off >= n_uwords)
    return -1;

  v = loc[off] | __atomic_exchange_n (rem + off, 0, __ATOMIC_SEQ_CST);
  loc[off] = v;

  v &= ~pow2_mask ((last + 1) & pow2_mask (log2_uword_bits));

  while (v == 0)
    {
      if (++off == n_uwords)
	return -1;

      v = loc[off] | __atomic_exchange_n (rem + off, 0, __ATOMIC_SEQ_CST);
      loc[off] = v;
    }

  bit = get_lowest_set_bit (v);
  loc[off] &= ~bit;
  return get_lowest_set_bit_index (bit) + (int) (off << log2_uword_bits);
}

static_always_inline int
clib_interrupt_is_any_pending (void *in)
{
  clib_interrupt_header_t *h = in;
  u32 n_uwords = h->uwords_used;
  uword *loc = h->local;
  uword *rem = h->remote;

  for (u32 i = 0; i < n_uwords; i++)
    if (loc[i])
      return 1;

  for (u32 i = 0; i < n_uwords; i++)
    if (rem[i])
      return 1;

  return 0;
}

#endif /* included_clib_interrupt_h */