fdt_intr.c revision 1.3.2.2 1 /* $NetBSD: fdt_intr.c,v 1.3.2.2 2015/12/27 12:09:49 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: fdt_intr.c,v 1.3.2.2 2015/12/27 12:09:49 skrll Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kmem.h>
35
36 #include <libfdt.h>
37 #include <dev/fdt/fdtvar.h>
38
39 struct fdtbus_interrupt_controller {
40 device_t ic_dev;
41 int ic_phandle;
42 const struct fdtbus_interrupt_controller_func *ic_funcs;
43
44 struct fdtbus_interrupt_controller *ic_next;
45 };
46
47 static struct fdtbus_interrupt_controller *fdtbus_ic = NULL;
48
49 static int
50 fdtbus_get_interrupt_parent(int phandle)
51 {
52 u_int interrupt_parent;
53
54 while (phandle >= 0) {
55 if (of_getprop_uint32(phandle, "interrupt-parent",
56 &interrupt_parent) == 0) {
57 break;
58 }
59 if (phandle == 0) {
60 return -1;
61 }
62 phandle = OF_parent(phandle);
63 }
64 if (phandle < 0) {
65 return -1;
66 }
67
68 const void *data = fdtbus_get_data();
69 const int off = fdt_node_offset_by_phandle(data, interrupt_parent);
70 if (off < 0) {
71 return -1;
72 }
73
74 return fdtbus_offset2phandle(off);
75 }
76
77 static struct fdtbus_interrupt_controller *
78 fdtbus_get_interrupt_controller(int phandle)
79 {
80 struct fdtbus_interrupt_controller *ic;
81
82 const int ic_phandle = fdtbus_get_interrupt_parent(phandle);
83 if (ic_phandle < 0) {
84 return NULL;
85 }
86
87 for (ic = fdtbus_ic; ic; ic = ic->ic_next) {
88 if (ic->ic_phandle == ic_phandle) {
89 return ic;
90 }
91 }
92
93 return NULL;
94 }
95
96 int
97 fdtbus_register_interrupt_controller(device_t dev, int phandle,
98 const struct fdtbus_interrupt_controller_func *funcs)
99 {
100 struct fdtbus_interrupt_controller *ic;
101
102 ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
103 ic->ic_dev = dev;
104 ic->ic_phandle = phandle;
105 ic->ic_funcs = funcs;
106
107 ic->ic_next = fdtbus_ic;
108 fdtbus_ic = ic;
109
110 return 0;
111 }
112
113 void *
114 fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
115 int (*func)(void *), void *arg)
116 {
117 struct fdtbus_interrupt_controller *ic;
118
119 ic = fdtbus_get_interrupt_controller(phandle);
120 if (ic == NULL)
121 return NULL;
122
123 return ic->ic_funcs->establish(ic->ic_dev, phandle, index, ipl,
124 flags, func, arg);
125 }
126
127 void
128 fdtbus_intr_disestablish(int phandle, void *ih)
129 {
130 struct fdtbus_interrupt_controller *ic;
131
132 ic = fdtbus_get_interrupt_controller(phandle);
133 KASSERT(ic != NULL);
134
135 return ic->ic_funcs->disestablish(ic->ic_dev, ih);
136 }
137
138 bool
139 fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
140 {
141 struct fdtbus_interrupt_controller *ic;
142
143 ic = fdtbus_get_interrupt_controller(phandle);
144 if (ic == NULL)
145 return false;
146
147 return ic->ic_funcs->intrstr(ic->ic_dev, phandle, index, buf, buflen);
148 }
149