bcm53xx_pax.c revision 1.10 1 /*-
2 * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Matt Thomas of 3am Software Foundry.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #define _ARM32_BUS_DMA_PRIVATE
31 #define PCIE_PRIVATE
32
33 #include "locators.h"
34
35 #include <sys/cdefs.h>
36
37 __KERNEL_RCSID(1, "$NetBSD: bcm53xx_pax.c,v 1.10 2013/04/21 19:59:39 msaitoh Exp $");
38
39 #include <sys/bus.h>
40 #include <sys/device.h>
41 #include <sys/extent.h>
42 #include <sys/intr.h>
43 #include <sys/kmem.h>
44 #include <sys/systm.h>
45
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pciconf.h>
49
50 #include <arm/broadcom/bcm53xx_reg.h>
51 #include <arm/broadcom/bcm53xx_var.h>
52
53 #ifndef __HAVE_PCI_CONF_HOOK
54 #error __HAVE_PCI_CONF_HOOK must be defined
55 #endif
56
57 static const struct {
58 paddr_t owin_base;
59 psize_t owin_size;
60 } bcmpax_owins[] = {
61 [0] = { BCM53XX_PCIE0_OWIN_PBASE, BCM53XX_PCIE0_OWIN_SIZE },
62 [1] = { BCM53XX_PCIE1_OWIN_PBASE, BCM53XX_PCIE1_OWIN_SIZE },
63 [2] = { BCM53XX_PCIE2_OWIN_PBASE, BCM53XX_PCIE2_OWIN_SIZE },
64 };
65
66 static int bcmpax_ccb_match(device_t, cfdata_t, void *);
67 static void bcmpax_ccb_attach(device_t, device_t, void *);
68
69 struct bcmpax_intrhand {
70 TAILQ_ENTRY(bcmpax_intrhand) ih_link;
71 int (*ih_func)(void *);
72 void *ih_arg;
73 int ih_ipl;
74 };
75
76 TAILQ_HEAD(bcmpax_ihqh, bcmpax_intrhand);
77
78 struct bcmpax_softc {
79 device_t sc_dev;
80 bus_space_tag_t sc_bst;
81 bus_space_handle_t sc_bsh;
82 bus_dma_tag_t sc_dmat;
83 kmutex_t *sc_lock;
84 kmutex_t *sc_cfg_lock;
85 bool sc_linkup;
86 int sc_pba_flags;
87 uint32_t sc_intrgen;
88 struct arm32_pci_chipset sc_pc;
89 struct bcmpax_ihqh sc_intrs;
90 void *sc_ih[6];
91 int sc_port;
92 char sc_intrstring[4][32];
93 };
94
95 static inline uint32_t
96 bcmpax_read_4(struct bcmpax_softc *sc, bus_size_t o)
97 {
98 return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
99 }
100
101 static inline void
102 bcmpax_write_4(struct bcmpax_softc *sc, bus_size_t o, uint32_t v)
103 {
104 bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
105 }
106
107 static void bcmpax_attach_hook(device_t, device_t, struct pcibus_attach_args *);
108 static int bcmpax_bus_maxdevs(void *, int);
109 static pcitag_t bcmpax_make_tag(void *, int, int, int);
110 static void bcmpax_decompose_tag(void *, pcitag_t, int *, int *, int *);
111 static pcireg_t bcmpax_conf_read(void *, pcitag_t, int);
112 static void bcmpax_conf_write(void *, pcitag_t, int, pcireg_t);
113
114 static int bcmpax_intr_map(const struct pci_attach_args *, pci_intr_handle_t *);
115 static const char *bcmpax_intr_string(void *, pci_intr_handle_t);
116 static const struct evcnt *bcmpax_intr_evcnt(void *, pci_intr_handle_t);
117 static void *bcmpax_intr_establish(void *, pci_intr_handle_t, int,
118 int (*)(void *), void *);
119 static void bcmpax_intr_disestablish(void *, void *);
120
121 static int bcmpax_conf_hook(void *, int, int, int, pcireg_t);
122 static void bcmpax_conf_interrupt(void *, int, int, int, int, int *);
123
124 static int bcmpax_intr(void *);
125
126 CFATTACH_DECL_NEW(bcmpax_ccb, sizeof(struct bcmpax_softc),
127 bcmpax_ccb_match, bcmpax_ccb_attach, NULL, NULL);
128
129 static int
130 bcmpax_ccb_match(device_t parent, cfdata_t cf, void *aux)
131 {
132 struct bcmccb_attach_args * const ccbaa = aux;
133 const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
134
135 if (strcmp(cf->cf_name, loc->loc_name))
136 return 0;
137
138 #ifdef DIAGNOSTIC
139 const int port = cf->cf_loc[BCMCCBCF_PORT];
140 #endif
141 KASSERT(port == BCMCCBCF_PORT_DEFAULT || port == loc->loc_port);
142
143 return 1;
144 }
145
146 static int
147 bcmpax_iwin_init(struct bcmpax_softc *sc)
148 {
149 #if 0
150 uint32_t megs = (physical_end + 0xfffff - physical_start) >> 20;
151 uint32_t iwin_megs = min(256, megs);
152 #if 1
153 bus_addr_t iwin1_start = physical_start;
154 #else
155 bus_addr_t iwin1_start = 0;
156 #endif
157 #if 1
158 bcmpax_write_4(sc, PCIE_IARR_1_LOWER, iwin1_start | min(megs, 128));
159 bcmpax_write_4(sc, PCIE_FUNC0_IMAP1, iwin1_start | 1);
160 #else
161 bcmpax_write_4(sc, PCIE_FUNC0_IMAP1, iwin1_start | min(megs, 128));
162 bcmpax_write_4(sc, PCIE_IARR_1_LOWER, iwin1_start | 1);
163 #endif
164 bcmpax_conf_write(sc, 0, PCI_MAPREG_START+4, iwin1_start);
165 if (iwin_megs > 128) {
166 bus_addr_t iwin2_start = iwin1_start + 128*1024*1024;
167 #if 1
168 bcmpax_write_4(sc, PCIE_IARR_2_LOWER, iwin2_start | min(megs - 128, 128));
169 bcmpax_write_4(sc, PCIE_FUNC0_IMAP2, iwin2_start | 1);
170 #else
171 bcmpax_write_4(sc, PCIE_FUNC0_IMAP2, iwin2_start | min(megs - 128, 128));
172 bcmpax_write_4(sc, PCIE_IARR_2_LOWER, iwin2_start | 1);
173 #endif
174 bcmpax_conf_write(sc, 0, PCI_MAPREG_START+8, iwin2_start);
175 }
176
177 if (megs <= iwin_megs) {
178 /*
179 * We could can DMA to all of memory so we don't need to subregion!
180 */
181 return 0;
182 }
183
184 return bus_dmatag_subregion(sc->sc_dmat, physical_start,
185 physical_start + (iwin_megs << 20) - 1, &sc->sc_dmat, 0);
186 #else
187 bcmpax_write_4(sc, PCIE_IARR_1_LOWER, 0);
188 bcmpax_write_4(sc, PCIE_FUNC0_IMAP1, 0);
189 bcmpax_write_4(sc, PCIE_IARR_2_LOWER, 0);
190 bcmpax_write_4(sc, PCIE_FUNC0_IMAP2, 0);
191 return 0;
192 #endif
193 }
194
195 static void
196 bcmpax_ccb_attach(device_t parent, device_t self, void *aux)
197 {
198 struct bcmpax_softc * const sc = device_private(self);
199 struct bcmccb_attach_args * const ccbaa = aux;
200 const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
201 const char * const xname = device_xname(self);
202 cfdata_t cf = device_cfdata(self);
203
204 sc->sc_dev = self;
205 sc->sc_dmat = &bcm53xx_coherent_dma_tag;
206 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
207 if (cf->cf_flags & 2) {
208 sc->sc_dmat = &bcm53xx_bounce_dma_tag;
209 }
210 #endif
211
212 for (u_int i = 0; i < 4; i++) {
213 snprintf(sc->sc_intrstring[i], sizeof(sc->sc_intrstring[i]),
214 "%s int%c", xname, 'a' + i);
215 }
216
217 sc->sc_bst = ccbaa->ccbaa_ccb_bst;
218 bus_space_subregion(sc->sc_bst, ccbaa->ccbaa_ccb_bsh,
219 loc->loc_offset, loc->loc_size, &sc->sc_bsh);
220
221 /*
222 * Kick the hardware into RC mode.
223 */
224 bcmpax_write_4(sc, PCIE_CLK_CONTROL, 3);
225 delay(250);
226 bcmpax_write_4(sc, PCIE_CLK_CONTROL, 1);
227
228 uint32_t v = bcmpax_read_4(sc, PCIE_STRAP_STATUS);
229 const bool enabled = (v & STRAP_PCIE_IF_ENABLE) != 0;
230 const bool is_v2_p = (v & STRAP_PCIE_USER_FOR_CE_GEN1) == 0;
231 const bool is_x2_p = (v & STRAP_PCIE_USER_FOR_CE_1LANE) == 0;
232 const bool is_rc_p = (v & STRAP_PCIE_USER_RC_MODE) != 0;
233
234 aprint_naive("\n");
235 aprint_normal(": PCI Express V%u %u-lane %s Controller%s\n",
236 is_v2_p ? 2 : 1,
237 is_x2_p ? 2 : 1,
238 is_rc_p ? "RC" : "EP",
239 enabled ? "" : "(disabled)");
240 if (!enabled || !is_rc_p)
241 return;
242
243 sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_VM);
244 sc->sc_cfg_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_VM);
245
246 TAILQ_INIT(&sc->sc_intrs);
247
248 sc->sc_pc.pc_conf_v = sc;
249 sc->sc_pc.pc_attach_hook = bcmpax_attach_hook;
250 sc->sc_pc.pc_bus_maxdevs = bcmpax_bus_maxdevs;
251 sc->sc_pc.pc_make_tag = bcmpax_make_tag;
252 sc->sc_pc.pc_decompose_tag = bcmpax_decompose_tag;
253 sc->sc_pc.pc_conf_read = bcmpax_conf_read;
254 sc->sc_pc.pc_conf_write = bcmpax_conf_write;
255
256 sc->sc_pc.pc_intr_v = sc;
257 sc->sc_pc.pc_intr_map = bcmpax_intr_map;
258 sc->sc_pc.pc_intr_string = bcmpax_intr_string;
259 sc->sc_pc.pc_intr_evcnt = bcmpax_intr_evcnt;
260 sc->sc_pc.pc_intr_establish = bcmpax_intr_establish;
261 sc->sc_pc.pc_intr_disestablish = bcmpax_intr_disestablish;
262
263 sc->sc_pc.pc_conf_hook = bcmpax_conf_hook;
264 sc->sc_pc.pc_conf_interrupt = bcmpax_conf_interrupt;
265
266 sc->sc_pba_flags |= PCI_FLAGS_MRL_OKAY;
267 sc->sc_pba_flags |= PCI_FLAGS_MRM_OKAY;
268 sc->sc_pba_flags |= PCI_FLAGS_MWI_OKAY;
269 // sc->sc_pba_flags |= PCI_FLAGS_MSI_OKAY;
270 // sc->sc_pba_flags |= PCI_FLAGS_MSIX_OKAY;
271
272 for (size_t i = 0; i < loc->loc_nintrs; i++) {
273 sc->sc_ih[i] = intr_establish(loc->loc_intrs[0] + i, IPL_VM,
274 IST_LEVEL, bcmpax_intr, sc);
275 if (sc->sc_ih[i] == NULL) {
276 aprint_error_dev(self,
277 "failed to establish interrupt #%zu (%zu)\n", i,
278 loc->loc_intrs[0] + i);
279 while (i-- > 0) {
280 intr_disestablish(sc->sc_ih[i]);
281 }
282 return;
283 }
284 }
285 aprint_normal_dev(self, "interrupting on irqs %d-%d\n",
286 loc->loc_intrs[0], loc->loc_intrs[0] + loc->loc_nintrs - 1);
287
288 /*
289 * Enable INTA-INTD
290 */
291 bcmpax_write_4(sc, PCIE_SYS_RC_INTX_EN, 0x0f);
292
293 int offset;
294 const bool ok = pci_get_capability(&sc->sc_pc, 0, PCI_CAP_PCIEXPRESS,
295 &offset, NULL);
296 KASSERT(ok);
297
298 /*
299 * This will force the device to negotiate to a max of gen1.
300 */
301 if (cf->cf_flags & 1) {
302 bcmpax_conf_write(sc, 0, offset + PCIE_LCSR2, 1);
303 }
304
305 /*
306 * Now we wait (.25 sec) for the link to come up.
307 */
308 offset += PCIE_LCSR;
309 for (size_t timo = 0;; timo++) {
310 const pcireg_t lcsr = bcmpax_conf_read(sc, 0, offset);
311 sc->sc_linkup = __SHIFTOUT(lcsr, PCIE_LCSR_NLW) != 0
312 && (1 || (lcsr & PCIE_LCSR_DLACTIVE) != 0);
313 if (sc->sc_linkup || timo == 250) {
314 aprint_debug_dev(self,
315 "lcsr=%#x nlw=%jd linkup=%d, timo=%zu\n",
316 lcsr, __SHIFTOUT(lcsr, PCIE_LCSR_NLW),
317 sc->sc_linkup, timo);
318 break;
319 }
320 DELAY(1000);
321 }
322
323 if (sc->sc_linkup) {
324 /*
325 * Enable the inbound (device->memory) map.
326 */
327 int error = bcmpax_iwin_init(sc);
328 if (error) {
329 aprint_error_dev(sc->sc_dev,
330 "failed to subregion dma tag: %d\n", error);
331 return;
332 }
333
334 aprint_normal_dev(self, "iwin[1]=%#x/%#x iwin[2]=%#x/%#x\n",
335 bcmpax_read_4(sc, PCIE_FUNC0_IMAP1),
336 bcmpax_read_4(sc, PCIE_IARR_1_LOWER),
337 bcmpax_read_4(sc, PCIE_FUNC0_IMAP2),
338 bcmpax_read_4(sc, PCIE_IARR_2_LOWER));
339
340 paddr_t base = bcmpax_owins[loc->loc_port].owin_base;
341 psize_t size = bcmpax_owins[loc->loc_port].owin_size;
342 KASSERT((size & ~PCIE_OARR_ADDR) == 0);
343 if (size > 0) {
344 bcmpax_write_4(sc, PCIE_OARR_0, base);
345 bcmpax_write_4(sc, PCIE_OMAP_0_LOWER, base | 1);
346 }
347 if (size > __LOWEST_SET_BIT(PCIE_OARR_ADDR)) {
348 paddr_t base1 = base + __LOWEST_SET_BIT(PCIE_OARR_ADDR);
349 bcmpax_write_4(sc, PCIE_OARR_1, base1);
350 bcmpax_write_4(sc, PCIE_OMAP_1_LOWER, base1 | 1);
351 }
352
353 struct extent *memext = extent_create("pcimem", base,
354 base + size, NULL, 0, EX_NOWAIT);
355
356 error = pci_configure_bus(&sc->sc_pc,
357 NULL, memext, NULL, 0, arm_pcache.dcache_line_size);
358
359 extent_destroy(memext);
360
361 if (error) {
362 aprint_normal_dev(self, "configuration failed\n");
363 return;
364 }
365 }
366
367 struct pcibus_attach_args pba;
368 memset(&pba, 0, sizeof(pba));
369
370 pba.pba_flags = sc->sc_pba_flags;
371 pba.pba_flags |= PCI_FLAGS_MEM_OKAY;
372 pba.pba_memt = sc->sc_bst;
373 pba.pba_dmat = sc->sc_dmat;
374 pba.pba_pc = &sc->sc_pc;
375 pba.pba_bus = 0;
376
377 config_found_ia(self, "pcibus", &pba, pcibusprint);
378 }
379
380 static void
381 bcmpax_attach_hook(device_t parent, device_t self,
382 struct pcibus_attach_args *pba)
383 {
384 }
385
386 static int
387 bcmpax_bus_maxdevs(void *v, int bus)
388 {
389 struct bcmpax_softc * const sc = v;
390
391 if (__predict_true(sc->sc_linkup))
392 return bus > 1 ? 32 : 1;
393
394 return bus ? 0 : 1;
395 }
396
397 static void
398 bcmpax_decompose_tag(void *v, pcitag_t tag, int *busp, int *devp, int *funcp)
399 {
400 if (busp)
401 *busp = __SHIFTOUT(tag, CFG_ADDR_BUS);
402 if (devp)
403 *devp = __SHIFTOUT(tag, CFG_ADDR_DEV);
404 if (funcp)
405 *funcp = __SHIFTOUT(tag, CFG_ADDR_FUNC);
406 }
407
408 static pcitag_t
409 bcmpax_make_tag(void *v, int bus, int dev, int func)
410 {
411 return __SHIFTIN(bus, CFG_ADDR_BUS)
412 | __SHIFTIN(dev, CFG_ADDR_DEV)
413 | __SHIFTIN(func, CFG_ADDR_FUNC)
414 | (bus == 0 ? CFG_ADDR_TYPE0 : CFG_ADDR_TYPE1);
415 }
416
417 static inline bus_size_t
418 bcmpax_conf_addr_write(struct bcmpax_softc *sc, pcitag_t tag)
419 {
420 if ((tag & (CFG_ADDR_BUS|CFG_ADDR_DEV)) == 0) {
421 uint32_t reg = __SHIFTOUT(tag, CFG_ADDR_REG);
422 uint32_t func = __SHIFTOUT(tag, CFG_ADDR_FUNC);
423 bcmpax_write_4(sc, PCIE_CFG_IND_ADDR,
424 __SHIFTIN(func, CFG_IND_ADDR_FUNC)
425 | __SHIFTIN(reg, CFG_IND_ADDR_REG));
426 __asm __volatile("dsb");
427 return PCIE_CFG_IND_DATA;
428 }
429 if (sc->sc_linkup) {
430 bcmpax_write_4(sc, PCIE_CFG_ADDR, tag);
431 __asm __volatile("dsb");
432 return PCIE_CFG_DATA;
433 }
434 return 0;
435 }
436
437 static pcireg_t
438 bcmpax_conf_read(void *v, pcitag_t tag, int reg)
439 {
440 struct bcmpax_softc * const sc = v;
441
442 /*
443 * Even in RC mode, the PCI Express Root Complex return itself
444 * as BCM Ethernet Controller!. We could change ppb.c to match it
445 * but we'll just lie and say we are a PPB bridge.
446 */
447 if ((tag & (CFG_ADDR_BUS|CFG_ADDR_DEV|CFG_ADDR_FUNC)) == 0
448 && reg == PCI_CLASS_REG) {
449 return PCI_CLASS_CODE(PCI_CLASS_BRIDGE,
450 PCI_SUBCLASS_BRIDGE_PCI, 0);
451 }
452
453 //printf("%s: tag %#lx reg %#x:", __func__, tag, reg);
454
455 mutex_enter(sc->sc_cfg_lock);
456 bus_size_t data_reg = bcmpax_conf_addr_write(sc, tag | reg);
457
458 //printf(" [from %#lx]:\n", data_reg);
459
460 pcireg_t rv;
461 if (data_reg)
462 rv = bcmpax_read_4(sc, data_reg);
463 else
464 rv = 0xffffffff;
465
466 mutex_exit(sc->sc_cfg_lock);
467
468 //printf(" %#x\n", rv);
469
470 return rv;
471 }
472
473 static void
474 bcmpax_conf_write(void *v, pcitag_t tag, int reg, pcireg_t val)
475 {
476 struct bcmpax_softc * const sc = v;
477
478 mutex_enter(sc->sc_cfg_lock);
479 bus_size_t data_reg = bcmpax_conf_addr_write(sc, tag | reg);
480
481 //printf("%s: tag %#lx reg %#x:", __func__, tag, reg);
482
483 if (data_reg) {
484 //printf(" [to %#lx]:\n", data_reg);
485 bcmpax_write_4(sc, data_reg, val);
486 //printf(" %#x\n", val);
487 }
488
489 mutex_exit(sc->sc_cfg_lock);
490 }
491
492 static void
493 bcmpax_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *ilinep)
494 {
495 *ilinep = 5; /* (ipin + swiz) & 3; */
496 }
497
498 static int
499 bcmpax_conf_hook(void *v, int bus, int dev, int func, pcireg_t id)
500 {
501 if (func > 0)
502 return 0;
503
504 return PCI_CONF_ENABLE_MEM | PCI_CONF_MAP_MEM | PCI_CONF_ENABLE_BM;
505 }
506
507 static int
508 bcmpax_intr(void *v)
509 {
510 struct bcmpax_softc * const sc = v;
511
512 while (bcmpax_read_4(sc, PCIE_SYS_RC_INTX_CSR)) {
513 struct bcmpax_intrhand *ih;
514 mutex_enter(sc->sc_lock);
515 const uint32_t lastgen = sc->sc_intrgen;
516 TAILQ_FOREACH(ih, &sc->sc_intrs, ih_link) {
517 int (* const func)(void *) = ih->ih_func;
518 void * const arg = ih->ih_arg;
519 mutex_exit(sc->sc_lock);
520 int rv = (*func)(arg);
521 if (rv) {
522 return rv;
523 }
524 mutex_enter(sc->sc_lock);
525 /*
526 * Check to see if the interrupt list changed.
527 * If so, restart from the beginning.
528 */
529 if (lastgen != sc->sc_intrgen)
530 break;
531 }
532 mutex_exit(sc->sc_lock);
533 }
534
535 return 0;
536 }
537
538 static int
539 bcmpax_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *pihp)
540 {
541 if (pa->pa_intrpin == 0)
542 return EINVAL;
543
544 *pihp = pa->pa_intrpin;
545 return 0;
546 }
547
548 static const char *
549 bcmpax_intr_string(void *v, pci_intr_handle_t pih)
550 {
551 struct bcmpax_softc * const sc = v;
552
553 if (pih)
554 return sc->sc_intrstring[pih - PCI_INTERRUPT_PIN_A];
555
556 return NULL;
557 }
558
559 static const struct evcnt *
560 bcmpax_intr_evcnt(void *v, pci_intr_handle_t pih)
561 {
562 return NULL;
563 }
564
565 static void *
566 bcmpax_intr_establish(void *v, pci_intr_handle_t pih, int ipl,
567 int (*func)(void *), void *arg)
568 {
569 struct bcmpax_softc * const sc = v;
570
571 KASSERT(!cpu_intr_p());
572 KASSERT(!cpu_softintr_p());
573 KASSERT(ipl == IPL_VM);
574 KASSERT(func != NULL);
575 KASSERT(arg != NULL);
576
577 if (pih == 0)
578 return NULL;
579
580 struct bcmpax_intrhand * const ih = kmem_alloc(sizeof(*ih), KM_SLEEP);
581
582 ih->ih_func = func;
583 ih->ih_arg = arg;
584
585 mutex_enter(sc->sc_lock);
586 TAILQ_INSERT_TAIL(&sc->sc_intrs, ih, ih_link);
587 mutex_exit(sc->sc_lock);
588
589 return ih;
590 }
591
592 static void
593 bcmpax_intr_disestablish(void *v, void *vih)
594 {
595 struct bcmpax_softc * const sc = v;
596 struct bcmpax_intrhand * const ih = vih;
597
598 mutex_enter(sc->sc_lock);
599 TAILQ_REMOVE(&sc->sc_intrs, ih, ih_link);
600 sc->sc_intrgen++;
601 mutex_exit(sc->sc_lock);
602
603 kmem_free(ih, sizeof(*ih));
604 }
605