1 /* $NetBSD: fdt_intr.c,v 1.24 2019/12/31 20:47:05 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2015-2018 Jared McNeill <jmcneill@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.24 2019/12/31 20:47:05 jmcneill Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/kmem.h> 35 #include <sys/queue.h> 36 37 #include <libfdt.h> 38 #include <dev/fdt/fdtvar.h> 39 40 struct fdtbus_interrupt_controller { 41 device_t ic_dev; 42 int ic_phandle; 43 const struct fdtbus_interrupt_controller_func *ic_funcs; 44 45 LIST_ENTRY(fdtbus_interrupt_controller) ic_next; 46 }; 47 48 static LIST_HEAD(, fdtbus_interrupt_controller) fdtbus_interrupt_controllers = 49 LIST_HEAD_INITIALIZER(fdtbus_interrupt_controllers); 50 51 struct fdtbus_interrupt_cookie { 52 struct fdtbus_interrupt_controller *c_ic; 53 void *c_ih; 54 55 LIST_ENTRY(fdtbus_interrupt_cookie) c_next; 56 }; 57 58 static LIST_HEAD(, fdtbus_interrupt_cookie) fdtbus_interrupt_cookies = 59 LIST_HEAD_INITIALIZER(fdtbus_interrupt_cookies); 60 61 static const u_int * get_specifier_by_index(int, int, int *); 62 static const u_int * get_specifier_from_map(int, const u_int *, int *); 63 64 /* 65 * Find the interrupt controller for a given node. This function will either 66 * return the phandle of the interrupt controller for this node, or the phandle 67 * of a node containing an interrupt-map table that can be used to find the 68 * real interrupt controller. 69 */ 70 static int 71 fdtbus_get_interrupt_parent(int phandle) 72 { 73 int iparent = phandle; 74 75 do { 76 /* 77 * If the node is an interrupt-controller, we are done. Note that 78 * a node cannot be an interrupt-controller for itself, so we skip 79 * the leaf node here. 80 */ 81 if (phandle != iparent && of_hasprop(iparent, "interrupt-controller")) 82 return iparent; 83 84 /* 85 * If the node has an explicit interrupt-parent, follow the reference. 86 */ 87 if (of_hasprop(iparent, "interrupt-parent")) 88 return fdtbus_get_phandle(iparent, "interrupt-parent"); 89 90 /* 91 * If the node has an interrupt-map, use it. The caller is responsible 92 * for parsing the interrupt-map and finding the real interrupt parent. 93 */ 94 if (phandle != iparent && of_hasprop(iparent, "interrupt-map")) 95 return iparent; 96 97 /* 98 * Continue searching up the tree. 99 */ 100 iparent = OF_parent(iparent); 101 } while (iparent > 0); 102 103 return 0; 104 } 105 106 static struct fdtbus_interrupt_controller * 107 fdtbus_get_interrupt_controller(int phandle) 108 { 109 struct fdtbus_interrupt_controller * ic; 110 LIST_FOREACH(ic, &fdtbus_interrupt_controllers, ic_next) { 111 if (ic->ic_phandle == phandle) 112 return ic; 113 } 114 return NULL; 115 } 116 117 int 118 fdtbus_register_interrupt_controller(device_t dev, int phandle, 119 const struct fdtbus_interrupt_controller_func *funcs) 120 { 121 struct fdtbus_interrupt_controller *ic; 122 123 ic = kmem_alloc(sizeof(*ic), KM_SLEEP); 124 ic->ic_dev = dev; 125 ic->ic_phandle = phandle; 126 ic->ic_funcs = funcs; 127 128 LIST_INSERT_HEAD(&fdtbus_interrupt_controllers, ic, ic_next); 129 130 return 0; 131 } 132 133 void * 134 fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags, 135 int (*func)(void *), void *arg) 136 { 137 const u_int *specifier; 138 int ihandle; 139 140 specifier = get_specifier_by_index(phandle, index, &ihandle); 141 if (specifier == NULL) 142 return NULL; 143 144 return fdtbus_intr_establish_raw(ihandle, specifier, ipl, 145 flags, func, arg); 146 } 147 148 void * 149 fdtbus_intr_establish_byname(int phandle, const char *name, int ipl, 150 int flags, int (*func)(void *), void *arg) 151 { 152 u_int index; 153 int err; 154 155 err = fdtbus_get_index(phandle, "interrupt-names", name, &index); 156 if (err != 0) 157 return NULL; 158 159 return fdtbus_intr_establish(phandle, index, ipl, flags, func, arg); 160 } 161 162 void * 163 fdtbus_intr_establish_raw(int ihandle, const u_int *specifier, int ipl, 164 int flags, int (*func)(void *), void *arg) 165 { 166 struct fdtbus_interrupt_controller *ic; 167 struct fdtbus_interrupt_cookie *c; 168 void *ih; 169 170 ic = fdtbus_get_interrupt_controller(ihandle); 171 if (ic == NULL) { 172 printf("%s: ihandle %d is not a controller\n",__func__,ihandle); 173 return NULL; 174 } 175 176 ih = ic->ic_funcs->establish(ic->ic_dev, __UNCONST(specifier), 177 ipl, flags, func, arg); 178 if (ih != NULL) { 179 c = kmem_alloc(sizeof(*c), KM_SLEEP); 180 c->c_ic = ic; 181 c->c_ih = ih; 182 LIST_INSERT_HEAD(&fdtbus_interrupt_cookies, c, c_next); 183 } 184 185 return ih; 186 } 187 188 void 189 fdtbus_intr_disestablish(int phandle, void *cookie) 190 { 191 struct fdtbus_interrupt_controller *ic = NULL; 192 struct fdtbus_interrupt_cookie *c; 193 194 LIST_FOREACH(c, &fdtbus_interrupt_cookies, c_next) { 195 if (c->c_ih == cookie) { 196 ic = c->c_ic; 197 LIST_REMOVE(c, c_next); 198 kmem_free(c, sizeof(*c)); 199 break; 200 } 201 } 202 203 if (ic == NULL) 204 panic("%s: interrupt handle not valid", __func__); 205 206 return ic->ic_funcs->disestablish(ic->ic_dev, cookie); 207 } 208 209 bool 210 fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen) 211 { 212 const u_int *specifier; 213 int ihandle; 214 215 specifier = get_specifier_by_index(phandle, index, &ihandle); 216 if (specifier == NULL) 217 return false; 218 219 return fdtbus_intr_str_raw(ihandle, specifier, buf, buflen); 220 } 221 222 bool 223 fdtbus_intr_str_raw(int ihandle, const u_int *specifier, char *buf, size_t buflen) 224 { 225 struct fdtbus_interrupt_controller *ic; 226 227 ic = fdtbus_get_interrupt_controller(ihandle); 228 if (ic == NULL) 229 return false; 230 231 return ic->ic_funcs->intrstr(ic->ic_dev, __UNCONST(specifier), buf, buflen); 232 } 233 234 static int 235 find_address_cells(int phandle) 236 { 237 uint32_t cells; 238 239 if (of_getprop_uint32(phandle, "#address-cells", &cells) != 0) 240 cells = 2; 241 242 return cells; 243 } 244 245 static int 246 find_interrupt_cells(int phandle) 247 { 248 uint32_t cells; 249 250 while (phandle > 0) { 251 if (of_getprop_uint32(phandle, "#interrupt-cells", &cells) == 0) 252 return cells; 253 phandle = OF_parent(phandle); 254 } 255 return 0; 256 } 257 258 static const u_int * 259 get_specifier_from_map(int phandle, const u_int *interrupt_spec, int *piphandle) 260 { 261 const u_int *result = NULL; 262 int len, resid; 263 264 const u_int *data = fdtbus_get_prop(phandle, "interrupt-map", &len); 265 if (data == NULL || len <= 0) 266 return NULL; 267 resid = len; 268 269 /* child unit address: #address-cells prop of child bus node */ 270 const int cua_cells = find_address_cells(phandle); 271 /* child interrupt specifier: #interrupt-cells of the nexus node */ 272 const int cis_cells = find_interrupt_cells(phandle); 273 274 /* Offset (in cells) from map entry to child unit address specifier */ 275 const u_int cua_off = 0; 276 /* Offset (in cells) from map entry to child interrupt specifier */ 277 const u_int cis_off = cua_off + cua_cells; 278 /* Offset (in cells) from map entry to interrupt parent phandle */ 279 const u_int ip_off = cis_off + cis_cells; 280 /* Offset (in cells) from map entry to parent unit specifier */ 281 const u_int pus_off = ip_off + 1; 282 283 const u_int *p = (const u_int *)data; 284 while (resid > 0) { 285 /* Interrupt parent phandle */ 286 const u_int iparent = fdtbus_get_phandle_from_native(be32toh(p[ip_off])); 287 288 /* parent unit specifier: #address-cells of the interrupt parent */ 289 const u_int pus_cells = find_address_cells(iparent); 290 /* parent interrupt specifier: #interrupt-cells of the interrupt parent */ 291 const u_int pis_cells = find_interrupt_cells(iparent); 292 293 /* Offset (in cells) from map entry to parent interrupt specifier */ 294 const u_int pis_off = pus_off + pus_cells; 295 296 #ifdef FDT_INTR_DEBUG 297 printf(" intr map (len %d):", pis_off + pis_cells); 298 for (int i = 0; i < pis_off + pis_cells; i++) 299 printf(" %08x", p[i]); 300 printf("\n"); 301 #endif 302 303 if (memcmp(&p[cis_off], interrupt_spec, cis_cells * 4) == 0) { 304 #ifdef FDT_INTR_DEBUG 305 const int slen = pus_cells + pis_cells; 306 printf(" intr map match iparent %08x slen %d:", iparent, slen); 307 for (int i = 0; i < slen; i++) 308 printf(" %08x", p[pus_off + i]); 309 printf("\n"); 310 #endif 311 result = &p[pus_off]; 312 *piphandle = iparent; 313 goto done; 314 } 315 /* Determine the length of the entry and skip that many 316 * 32 bit words 317 */ 318 const u_int reclen = pis_off + pis_cells; 319 resid -= reclen * sizeof(u_int); 320 p += reclen; 321 } 322 323 done: 324 return result; 325 } 326 327 328 static const u_int * 329 get_specifier_from_extended(int phandle, int pindex, int *piphandle) 330 { 331 const u_int *result = NULL; 332 struct fdt_phandle_data data; 333 334 if (fdtbus_get_phandle_with_data(phandle, "interrupts-extended", 335 "#interrupt-cells", pindex, &data) == 0) { 336 *piphandle = data.phandle; 337 result = data.values; 338 } 339 340 return result; 341 } 342 343 static const u_int * 344 get_specifier_by_index(int phandle, int pindex, int *piphandle) 345 { 346 const u_int *node_specifier; 347 int interrupt_parent, interrupt_cells, len; 348 349 if (of_hasprop(phandle, "interrupts-extended")) 350 return get_specifier_from_extended(phandle, pindex, piphandle); 351 352 interrupt_parent = fdtbus_get_interrupt_parent(phandle); 353 if (interrupt_parent <= 0) 354 return NULL; 355 356 node_specifier = fdtbus_get_prop(phandle, "interrupts", &len); 357 if (node_specifier == NULL) 358 return NULL; 359 360 interrupt_cells = find_interrupt_cells(interrupt_parent); 361 if (interrupt_cells <= 0) 362 return NULL; 363 364 const u_int spec_length = len / 4; 365 const u_int nintr = spec_length / interrupt_cells; 366 if (pindex >= nintr) 367 return NULL; 368 369 node_specifier += (interrupt_cells * pindex); 370 371 if (of_hasprop(interrupt_parent, "interrupt-map")) 372 return get_specifier_from_map(interrupt_parent, node_specifier, piphandle); 373 374 *piphandle = interrupt_parent; 375 376 return node_specifier; 377 } 378