hb.c revision 1.22 1 /* $NetBSD: hb.c,v 1.22 2021/04/24 23:36:44 thorpej Exp $ */
2
3 #include <sys/cdefs.h>
4 __KERNEL_RCSID(0, "$NetBSD: hb.c,v 1.22 2021/04/24 23:36:44 thorpej Exp $");
5
6 #define __INTR_PRIVATE
7 #include <sys/param.h>
8 #include <sys/systm.h>
9 #include <sys/device.h>
10 #include <sys/kmem.h>
11 #include <sys/intr.h>
12
13 #include <machine/autoconf.h>
14
15 #include <newsmips/dev/hbvar.h>
16
17 #include "ioconf.h"
18
19 static int hb_match(device_t, cfdata_t, void *);
20 static void hb_attach(device_t, device_t, void *);
21 static int hb_search(device_t, cfdata_t, const int *, void *);
22 static int hb_print(void *, const char *);
23
24 CFATTACH_DECL_NEW(hb, 0,
25 hb_match, hb_attach, NULL, NULL);
26
27 #define NLEVEL 4
28 static struct newsmips_intr hbintr_tab[NLEVEL];
29
30 static int
31 hb_match(device_t parent, cfdata_t cf, void *aux)
32 {
33 struct confargs *ca = aux;
34
35 if (strcmp(ca->ca_name, hb_cd.cd_name) != 0)
36 return 0;
37
38 return 1;
39 }
40
41 static void
42 hb_attach(device_t parent, device_t self, void *aux)
43 {
44 struct hb_attach_args ha;
45 struct newsmips_intr *ip;
46 int i;
47
48 aprint_normal("\n");
49
50 memset(&ha, 0, sizeof(ha));
51 for (i = 0; i < NLEVEL; i++) {
52 ip = &hbintr_tab[i];
53 LIST_INIT(&ip->intr_q);
54 }
55
56 config_search(self, &ha,
57 CFARG_SEARCH, hb_search,
58 CFARG_EOL);
59 }
60
61 static int
62 hb_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
63 {
64 struct hb_attach_args *ha = aux;
65
66 ha->ha_name = cf->cf_name;
67 ha->ha_addr = cf->cf_addr;
68 ha->ha_level = cf->cf_level;
69
70 if (config_probe(parent, cf, ha))
71 config_attach(parent, cf, ha, hb_print, CFARG_EOL);
72
73 return 0;
74 }
75
76 /*
77 * Print out the confargs. The (parent) name is non-NULL
78 * when there was no match found by config_found().
79 */
80 static int
81 hb_print(void *args, const char *name)
82 {
83 struct hb_attach_args *ha = args;
84
85 /* Be quiet about empty HB locations. */
86 if (name)
87 return QUIET;
88
89 if (ha->ha_addr != -1)
90 aprint_normal(" addr 0x%x", ha->ha_addr);
91
92 return UNCONF;
93 }
94
95 void *
96 hb_intr_establish(int level, int mask, int priority, int (*func)(void *),
97 void *arg)
98 {
99 struct newsmips_intr *ip;
100 struct newsmips_intrhand *ih, *curih;
101
102 ip = &hbintr_tab[level];
103
104 ih = kmem_alloc(sizeof(*ih), KM_SLEEP);
105 ih->ih_func = func;
106 ih->ih_arg = arg;
107 ih->ih_level = level;
108 ih->ih_mask = mask;
109 ih->ih_priority = priority;
110
111 if (LIST_EMPTY(&ip->intr_q)) {
112 LIST_INSERT_HEAD(&ip->intr_q, ih, ih_q);
113 goto done;
114 }
115
116 for (curih = LIST_FIRST(&ip->intr_q);
117 LIST_NEXT(curih, ih_q) != NULL;
118 curih = LIST_NEXT(curih, ih_q)) {
119 if (ih->ih_priority > curih->ih_priority) {
120 LIST_INSERT_BEFORE(curih, ih, ih_q);
121 goto done;
122 }
123 }
124
125 LIST_INSERT_AFTER(curih, ih, ih_q);
126
127 done:
128 return ih;
129 }
130
131 void
132 hb_intr_dispatch(int level, int stat)
133 {
134 struct newsmips_intr *ip;
135 struct newsmips_intrhand *ih;
136
137 ip = &hbintr_tab[level];
138
139 LIST_FOREACH(ih, &ip->intr_q, ih_q) {
140 if (ih->ih_mask & stat)
141 (*ih->ih_func)(ih->ih_arg);
142 }
143 }
144