Sun Jun 11 12:56:37 2017 UTC ()
Fix an issue with interrupt controller lookup wrt cascading interrupt
controllers.


(jmcneill)
diff -r1.10 -r1.11 src/sys/dev/fdt/fdt_intr.c

cvs diff -r1.10 -r1.11 src/sys/dev/fdt/fdt_intr.c (expand / switch to unified diff)

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