Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2021 Marvell.
3 : : */
4 : : #include <fcntl.h>
5 : : #include <pthread.h>
6 : : #include <sys/ioctl.h>
7 : : #include <sys/mman.h>
8 : : #include <sys/queue.h>
9 : : #include <unistd.h>
10 : :
11 : : #include "roc_api.h"
12 : : #include "roc_bphy_irq.h"
13 : :
14 : : struct roc_bphy_irq_usr_data {
15 : : uint64_t isr_base;
16 : : uint64_t sp;
17 : : uint64_t cpu;
18 : : uint64_t irq_num;
19 : : };
20 : :
21 : : struct roc_bphy_irq_stack {
22 : : STAILQ_ENTRY(roc_bphy_irq_stack) entries;
23 : : void *sp_buffer;
24 : : int cpu;
25 : : int inuse;
26 : : };
27 : :
28 : : #define ROC_BPHY_MEMZONE_NAME "roc_bphy_mz"
29 : : #define ROC_BPHY_CTR_DEV_PATH "/dev/otx-bphy-ctr"
30 : :
31 : : #define ROC_BPHY_IOC_MAGIC 0xF3
32 : : #define ROC_BPHY_IOC_SET_BPHY_HANDLER \
33 : : _IOW(ROC_BPHY_IOC_MAGIC, 1, struct roc_bphy_irq_usr_data)
34 : : #define ROC_BPHY_IOC_CLR_BPHY_HANDLER _IO(ROC_BPHY_IOC_MAGIC, 2)
35 : : #define ROC_BPHY_IOC_GET_BPHY_MAX_IRQ _IOR(ROC_BPHY_IOC_MAGIC, 3, uint64_t)
36 : : #define ROC_BPHY_IOC_GET_BPHY_BMASK_IRQ _IOR(ROC_BPHY_IOC_MAGIC, 4, uint64_t)
37 : :
38 : : static STAILQ_HEAD(slisthead, roc_bphy_irq_stack)
39 : : irq_stacks = STAILQ_HEAD_INITIALIZER(irq_stacks);
40 : :
41 : : /* Note: it is assumed that as for now there is no multiprocess support */
42 : : static pthread_mutex_t stacks_mutex = PTHREAD_MUTEX_INITIALIZER;
43 : :
44 : : struct roc_bphy_irq_chip *
45 : 0 : roc_bphy_intr_init(void)
46 : : {
47 : : struct roc_bphy_irq_chip *irq_chip;
48 : : uint64_t max_irq, i, avail_irqs;
49 : : int fd, ret;
50 : :
51 : : fd = open(ROC_BPHY_CTR_DEV_PATH, O_RDWR | O_SYNC);
52 [ # # ]: 0 : if (fd < 0) {
53 : 0 : plt_err("Failed to open %s", ROC_BPHY_CTR_DEV_PATH);
54 : 0 : return NULL;
55 : : }
56 : :
57 : 0 : ret = ioctl(fd, ROC_BPHY_IOC_GET_BPHY_MAX_IRQ, &max_irq);
58 [ # # ]: 0 : if (ret < 0) {
59 : 0 : plt_err("Failed to get max irq number via ioctl");
60 : 0 : goto err_ioctl;
61 : : }
62 : :
63 : 0 : ret = ioctl(fd, ROC_BPHY_IOC_GET_BPHY_BMASK_IRQ, &avail_irqs);
64 [ # # ]: 0 : if (ret < 0) {
65 : 0 : plt_err("Failed to get available irqs bitmask via ioctl");
66 : 0 : goto err_ioctl;
67 : : }
68 : :
69 : 0 : irq_chip = plt_zmalloc(sizeof(*irq_chip), 0);
70 [ # # ]: 0 : if (irq_chip == NULL) {
71 : 0 : plt_err("Failed to alloc irq_chip");
72 : 0 : goto err_alloc_chip;
73 : : }
74 : :
75 : 0 : irq_chip->intfd = fd;
76 : 0 : irq_chip->max_irq = max_irq;
77 : 0 : irq_chip->avail_irq_bmask = avail_irqs;
78 : 0 : irq_chip->irq_vecs =
79 : 0 : plt_zmalloc(irq_chip->max_irq * sizeof(*irq_chip->irq_vecs), 0);
80 [ # # ]: 0 : if (irq_chip->irq_vecs == NULL) {
81 : 0 : plt_err("Failed to alloc irq_chip irq_vecs");
82 : 0 : goto err_alloc_irq;
83 : : }
84 : :
85 : 0 : irq_chip->mz_name = plt_zmalloc(strlen(ROC_BPHY_MEMZONE_NAME) + 1, 0);
86 [ # # ]: 0 : if (irq_chip->mz_name == NULL) {
87 : 0 : plt_err("Failed to alloc irq_chip name");
88 : 0 : goto err_alloc_name;
89 : : }
90 : : plt_strlcpy(irq_chip->mz_name, ROC_BPHY_MEMZONE_NAME,
91 : : strlen(ROC_BPHY_MEMZONE_NAME) + 1);
92 : :
93 [ # # ]: 0 : for (i = 0; i < irq_chip->max_irq; i++) {
94 : 0 : irq_chip->irq_vecs[i].fd = -1;
95 : 0 : irq_chip->irq_vecs[i].handler_cpu = -1;
96 : : }
97 : :
98 : : return irq_chip;
99 : :
100 : : err_alloc_name:
101 : 0 : plt_free(irq_chip->irq_vecs);
102 : :
103 : 0 : err_alloc_irq:
104 : 0 : plt_free(irq_chip);
105 : :
106 : 0 : err_ioctl:
107 : 0 : err_alloc_chip:
108 : 0 : close(fd);
109 : 0 : return NULL;
110 : : }
111 : :
112 : : void
113 : 0 : roc_bphy_intr_fini(struct roc_bphy_irq_chip *irq_chip)
114 : : {
115 [ # # ]: 0 : if (irq_chip == NULL)
116 : : return;
117 : :
118 : 0 : close(irq_chip->intfd);
119 : 0 : plt_free(irq_chip->mz_name);
120 : 0 : plt_free(irq_chip->irq_vecs);
121 : 0 : plt_free(irq_chip);
122 : : }
123 : :
124 : : static void
125 : 0 : roc_bphy_irq_stack_remove(int cpu)
126 : : {
127 : : struct roc_bphy_irq_stack *curr_stack;
128 : :
129 [ # # ]: 0 : if (pthread_mutex_lock(&stacks_mutex))
130 : : return;
131 : :
132 [ # # ]: 0 : STAILQ_FOREACH(curr_stack, &irq_stacks, entries) {
133 [ # # ]: 0 : if (curr_stack->cpu == cpu)
134 : : break;
135 : : }
136 : :
137 [ # # ]: 0 : if (curr_stack == NULL)
138 : 0 : goto leave;
139 : :
140 [ # # ]: 0 : if (curr_stack->inuse > 0)
141 : 0 : curr_stack->inuse--;
142 : :
143 [ # # ]: 0 : if (curr_stack->inuse == 0) {
144 [ # # # # : 0 : STAILQ_REMOVE(&irq_stacks, curr_stack, roc_bphy_irq_stack,
# # # # ]
145 : : entries);
146 : 0 : plt_free(curr_stack->sp_buffer);
147 : 0 : plt_free(curr_stack);
148 : : }
149 : :
150 : 0 : leave:
151 : 0 : pthread_mutex_unlock(&stacks_mutex);
152 : : }
153 : :
154 : : static void *
155 : 0 : roc_bphy_irq_stack_get(int cpu)
156 : : {
157 : : #define ARM_STACK_ALIGNMENT (2 * sizeof(void *))
158 : : #define IRQ_ISR_STACK_SIZE 0x200000
159 : :
160 : : struct roc_bphy_irq_stack *curr_stack;
161 : : void *retval = NULL;
162 : :
163 [ # # ]: 0 : if (pthread_mutex_lock(&stacks_mutex))
164 : : return NULL;
165 : :
166 [ # # ]: 0 : STAILQ_FOREACH(curr_stack, &irq_stacks, entries) {
167 [ # # ]: 0 : if (curr_stack->cpu == cpu) {
168 : 0 : curr_stack->inuse++;
169 : 0 : retval = ((char *)curr_stack->sp_buffer) +
170 : : IRQ_ISR_STACK_SIZE;
171 : 0 : goto found_stack;
172 : : }
173 : : }
174 : :
175 : 0 : curr_stack = plt_zmalloc(sizeof(struct roc_bphy_irq_stack), 0);
176 [ # # ]: 0 : if (curr_stack == NULL)
177 : 0 : goto err_stack;
178 : :
179 : 0 : curr_stack->sp_buffer =
180 : 0 : plt_zmalloc(IRQ_ISR_STACK_SIZE * 2, ARM_STACK_ALIGNMENT);
181 [ # # ]: 0 : if (curr_stack->sp_buffer == NULL)
182 : 0 : goto err_buffer;
183 : :
184 : 0 : curr_stack->cpu = cpu;
185 : 0 : curr_stack->inuse = 0;
186 : 0 : STAILQ_INSERT_TAIL(&irq_stacks, curr_stack, entries);
187 : 0 : retval = ((char *)curr_stack->sp_buffer) + IRQ_ISR_STACK_SIZE;
188 : :
189 : 0 : found_stack:
190 : 0 : pthread_mutex_unlock(&stacks_mutex);
191 : 0 : return retval;
192 : :
193 : : err_buffer:
194 : 0 : plt_free(curr_stack);
195 : :
196 : 0 : err_stack:
197 : 0 : pthread_mutex_unlock(&stacks_mutex);
198 : 0 : return NULL;
199 : : }
200 : :
201 : : void
202 : 0 : roc_bphy_intr_handler(unsigned int irq_num)
203 : : {
204 : : struct roc_bphy_irq_chip *irq_chip;
205 : : const struct plt_memzone *mz;
206 : :
207 : 0 : mz = plt_memzone_lookup(ROC_BPHY_MEMZONE_NAME);
208 [ # # ]: 0 : if (mz == NULL)
209 : : return;
210 : :
211 : 0 : irq_chip = *(struct roc_bphy_irq_chip **)mz->addr;
212 [ # # ]: 0 : if (irq_chip == NULL)
213 : : return;
214 : :
215 [ # # ]: 0 : if (irq_chip->irq_vecs[irq_num].handler != NULL)
216 : 0 : irq_chip->irq_vecs[irq_num].handler(
217 : : (int)irq_num, irq_chip->irq_vecs[irq_num].isr_data);
218 : :
219 : : roc_atf_ret();
220 : : }
221 : :
222 : : static int
223 : 0 : roc_bphy_irq_handler_set(struct roc_bphy_irq_chip *chip, int cpu, int irq_num,
224 : : void (*isr)(int irq_num, void *isr_data),
225 : : void *isr_data)
226 : : {
227 : : struct roc_bphy_irq_usr_data irq_usr;
228 : : const struct plt_memzone *mz;
229 : : int retval, rc;
230 : : char *env;
231 : :
232 : 0 : mz = plt_memzone_lookup(chip->mz_name);
233 [ # # ]: 0 : if (mz == NULL) {
234 : : /* what we want is just a pointer to chip, not object itself */
235 : 0 : mz = plt_memzone_reserve_cache_align(chip->mz_name,
236 : : sizeof(chip));
237 [ # # ]: 0 : if (mz == NULL)
238 : : return -ENOMEM;
239 : : }
240 : :
241 [ # # ]: 0 : if (chip->irq_vecs[irq_num].handler != NULL)
242 : : return -EINVAL;
243 : :
244 : 0 : irq_usr.isr_base = (uint64_t)roc_bphy_intr_handler;
245 : 0 : irq_usr.sp = (uint64_t)roc_bphy_irq_stack_get(cpu);
246 : 0 : irq_usr.cpu = cpu;
247 [ # # ]: 0 : if (irq_usr.sp == 0)
248 : : return -ENOMEM;
249 : :
250 : : /* On simulator memory locking operation takes much time. We want
251 : : * to skip this when running in such an environment.
252 : : */
253 : 0 : env = getenv("BPHY_INTR_MLOCK_DISABLE");
254 [ # # ]: 0 : if (env == NULL) {
255 : 0 : rc = mlockall(MCL_CURRENT | MCL_FUTURE);
256 [ # # ]: 0 : if (rc < 0)
257 : 0 : plt_warn("Failed to lock memory into RAM");
258 : : }
259 : :
260 : 0 : *((struct roc_bphy_irq_chip **)(mz->addr)) = chip;
261 : 0 : irq_usr.irq_num = irq_num;
262 : 0 : chip->irq_vecs[irq_num].handler_cpu = cpu;
263 : 0 : chip->irq_vecs[irq_num].handler = isr;
264 : 0 : chip->irq_vecs[irq_num].isr_data = isr_data;
265 : 0 : retval = ioctl(chip->intfd, ROC_BPHY_IOC_SET_BPHY_HANDLER, &irq_usr);
266 [ # # ]: 0 : if (retval != 0) {
267 : 0 : roc_bphy_irq_stack_remove(cpu);
268 : 0 : chip->irq_vecs[irq_num].handler = NULL;
269 : 0 : chip->irq_vecs[irq_num].handler_cpu = -1;
270 : : } else {
271 : 0 : chip->n_handlers++;
272 : : }
273 : :
274 : : return retval;
275 : : }
276 : :
277 : : bool
278 : 0 : roc_bphy_intr_available(struct roc_bphy_irq_chip *irq_chip, int irq_num)
279 : : {
280 [ # # # # ]: 0 : if (irq_num < 0 || (uint64_t)irq_num >= irq_chip->max_irq)
281 : : return false;
282 : :
283 : 0 : return irq_chip->avail_irq_bmask & BIT(irq_num);
284 : : }
285 : :
286 : : uint64_t
287 : 0 : roc_bphy_intr_max_get(struct roc_bphy_irq_chip *irq_chip)
288 : : {
289 : 0 : return irq_chip->max_irq;
290 : : }
291 : :
292 : : int
293 : 0 : roc_bphy_intr_clear(struct roc_bphy_irq_chip *chip, int irq_num)
294 : : {
295 : : const struct plt_memzone *mz;
296 : : int retval;
297 : :
298 [ # # ]: 0 : if (chip == NULL)
299 : : return -EINVAL;
300 [ # # # # ]: 0 : if ((uint64_t)irq_num >= chip->max_irq || irq_num < 0)
301 : : return -EINVAL;
302 [ # # ]: 0 : if (!roc_bphy_intr_available(chip, irq_num))
303 : : return -ENOTSUP;
304 [ # # ]: 0 : if (chip->irq_vecs[irq_num].handler == NULL)
305 : : return -EINVAL;
306 : 0 : mz = plt_memzone_lookup(chip->mz_name);
307 [ # # ]: 0 : if (mz == NULL)
308 : : return -ENXIO;
309 : :
310 : 0 : retval = ioctl(chip->intfd, ROC_BPHY_IOC_CLR_BPHY_HANDLER, irq_num);
311 [ # # ]: 0 : if (retval == 0) {
312 : 0 : roc_bphy_irq_stack_remove(chip->irq_vecs[irq_num].handler_cpu);
313 : 0 : chip->n_handlers--;
314 : 0 : chip->irq_vecs[irq_num].isr_data = NULL;
315 : 0 : chip->irq_vecs[irq_num].handler = NULL;
316 : 0 : chip->irq_vecs[irq_num].handler_cpu = -1;
317 [ # # ]: 0 : if (chip->n_handlers == 0) {
318 : 0 : retval = plt_memzone_free(mz);
319 [ # # ]: 0 : if (retval < 0)
320 : 0 : plt_err("Failed to free memzone: irq %d",
321 : : irq_num);
322 : : }
323 : : } else {
324 : 0 : plt_err("Failed to clear bphy interrupt handler");
325 : : }
326 : :
327 : : return retval;
328 : : }
329 : :
330 : : int
331 : 0 : roc_bphy_intr_register(struct roc_bphy_irq_chip *irq_chip,
332 : : struct roc_bphy_intr *intr)
333 : : {
334 : : int ret;
335 : :
336 [ # # ]: 0 : if (!roc_bphy_intr_available(irq_chip, intr->irq_num))
337 : : return -ENOTSUP;
338 : :
339 : 0 : ret = roc_bphy_irq_handler_set(irq_chip, intr->cpu, intr->irq_num,
340 : : intr->intr_handler, intr->isr_data);
341 : :
342 : 0 : return ret;
343 : : }
|