1 /* $NetBSD: vbus.c,v 1.3 2017/02/17 20:53:17 palle Exp $ */ 2 /* $OpenBSD: vbus.c,v 1.8 2015/09/27 11:29:20 kettenis Exp $ */ 3 /* 4 * Copyright (c) 2008 Mark Kettenis 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/device.h> 21 #include <sys/malloc.h> 22 #include <sys/systm.h> 23 24 #include <machine/autoconf.h> 25 #include <machine/hypervisor.h> 26 #include <machine/openfirm.h> 27 28 #include <sparc64/dev/vbusvar.h> 29 30 #include <sparc64/dev/iommureg.h> 31 32 #include <dev/clock_subr.h> 33 extern todr_chip_handle_t todr_handle; 34 35 #ifdef DEBUG 36 #define VBUS_INTR 0x01 37 int vbus_debug = 0x00|VBUS_INTR; 38 #define DPRINTF(l, s) do { if (vbus_debug & l) printf s; } while (0) 39 #else 40 #define DPRINTF(l, s) 41 #endif 42 43 struct vbus_softc { 44 device_t sc_dv; 45 bus_space_tag_t sc_bustag; 46 bus_dma_tag_t sc_dmatag; 47 }; 48 int vbus_cmp_cells(int *, int *, int *, int); 49 int vbus_match(device_t, cfdata_t, void *); 50 void vbus_attach(device_t, device_t, void *); 51 int vbus_print(void *, const char *); 52 53 CFATTACH_DECL_NEW(vbus, sizeof(struct vbus_softc), 54 vbus_match, vbus_attach, NULL, NULL); 55 56 void *vbus_intr_establish(bus_space_tag_t, int, int, 57 int (*)(void *), void *, void (*)(void)); 58 void vbus_intr_ack(struct intrhand *); 59 bus_space_tag_t vbus_alloc_bus_tag(struct vbus_softc *, bus_space_tag_t); 60 61 int 62 vbus_match(device_t parent, cfdata_t match, void *aux) 63 { 64 struct mainbus_attach_args *ma = aux; 65 66 if (strcmp(ma->ma_name, "virtual-devices") == 0) 67 return (1); 68 69 return (0); 70 } 71 72 void 73 vbus_attach(device_t parent, device_t self, void *aux) 74 { 75 struct vbus_softc *sc = device_private(self); 76 struct mainbus_attach_args *ma = aux; 77 int node; 78 79 sc->sc_bustag = vbus_alloc_bus_tag(sc, ma->ma_bustag); 80 sc->sc_dmatag = ma->ma_dmatag; 81 printf("\n"); 82 83 for (node = OF_child(ma->ma_node); node; node = OF_peer(node)) { 84 struct vbus_attach_args va; 85 char buf[32]; 86 87 bzero(&va, sizeof(va)); 88 va.va_node = node; 89 if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0) 90 continue; 91 va.va_name = buf; 92 va.va_bustag = sc->sc_bustag; 93 va.va_dmatag = sc->sc_dmatag; 94 prom_getprop(node, "reg", sizeof(*va.va_reg), 95 &va.va_nreg, (void **)&va.va_reg); 96 prom_getprop(node, "interrupts", sizeof(*va.va_intr), 97 &va.va_nintr, (void **)&va.va_intr); 98 config_found(self, &va, vbus_print); 99 } 100 101 struct vbus_attach_args va; 102 bzero(&va, sizeof(va)); 103 va.va_name = "rtc"; 104 config_found(self, &va, vbus_print); 105 106 } 107 108 int 109 vbus_print(void *aux, const char *name) 110 { 111 struct vbus_attach_args *va = aux; 112 113 if (name) 114 printf("\"%s\" at %s", va->va_name, name); 115 return (UNCONF); 116 } 117 118 /* 119 * Compare a sequence of cells with a mask, return 1 if they match and 120 * 0 if they don't. 121 */ 122 int 123 vbus_cmp_cells(int *cell1, int *cell2, int *mask, int ncells) 124 { 125 int i; 126 127 for (i = 0; i < ncells; i++) { 128 if (((cell1[i] ^ cell2[i]) & mask[i]) != 0) 129 return (0); 130 } 131 return (1); 132 } 133 134 int 135 vbus_intr_map(int node, int ino, uint64_t *sysino) 136 { 137 int *imap = NULL, nimap; 138 int *reg = NULL, nreg; 139 int *imap_mask; 140 int parent; 141 int address_cells, interrupt_cells; 142 uint64_t devhandle; 143 uint64_t devino; 144 int len; 145 int err; 146 147 DPRINTF(VBUS_INTR, ("vbus_intr_map(): ino 0x%x\n", ino)); 148 149 parent = OF_parent(node); 150 151 address_cells = prom_getpropint(parent, "#address-cells", 2); 152 interrupt_cells = prom_getpropint(parent, "#interrupt-cells", 1); 153 KASSERT(interrupt_cells == 1); 154 155 len = OF_getproplen(parent, "interrupt-map-mask"); 156 if (len < (address_cells + interrupt_cells) * sizeof(int)) 157 return (-1); 158 imap_mask = malloc(len, M_DEVBUF, M_NOWAIT); 159 if (imap_mask == NULL) 160 return (-1); 161 if (OF_getprop(parent, "interrupt-map-mask", imap_mask, len) != len) 162 goto out; 163 164 if (prom_getprop(parent, "interrupt-map", sizeof(int), &nimap, (void **)&imap)) 165 panic("vbus: can't get interrupt-map"); 166 167 if (prom_getprop(node, "reg", sizeof(*reg), &nreg, (void **)®)) 168 panic("vbus: can't get reg"); 169 if (nreg < address_cells) 170 goto out; 171 172 while (nimap >= address_cells + interrupt_cells + 2) { 173 if (vbus_cmp_cells(imap, reg, imap_mask, address_cells) && 174 vbus_cmp_cells(&imap[address_cells], &ino, 175 &imap_mask[address_cells], interrupt_cells)) { 176 node = imap[address_cells + interrupt_cells]; 177 devino = imap[address_cells + interrupt_cells + 1]; 178 179 free(reg, M_DEVBUF); 180 reg = NULL; 181 182 if (prom_getprop(node, "reg", sizeof(*reg), &nreg, (void **)®)) 183 panic("vbus: can't get reg"); 184 185 devhandle = reg[0] & 0x0fffffff; 186 187 err = hv_intr_devino_to_sysino(devhandle, devino, sysino); 188 if (err != H_EOK) 189 goto out; 190 191 KASSERT(*sysino == INTVEC(*sysino)); 192 free(imap_mask, M_DEVBUF); 193 return (0); 194 } 195 imap += address_cells + interrupt_cells + 2; 196 nimap -= address_cells + interrupt_cells + 2; 197 } 198 199 out: 200 free(imap_mask, M_DEVBUF); 201 return (-1); 202 } 203 204 void * 205 vbus_intr_establish(bus_space_tag_t t, int ihandle, int level, 206 int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */) 207 { 208 uint64_t sysino = INTVEC(ihandle); 209 struct intrhand *ih; 210 int ino; 211 int err; 212 213 DPRINTF(VBUS_INTR, ("vbus_intr_establish()\n")); 214 215 ino = INTINO(ihandle); 216 217 ih = intrhand_alloc(); 218 219 ih->ih_ivec = ihandle; 220 ih->ih_fun = handler; 221 ih->ih_arg = arg; 222 ih->ih_pil = level; 223 ih->ih_number = ino; 224 ih->ih_pending = 0; 225 226 intr_establish(ih->ih_pil, level != IPL_VM, ih); 227 ih->ih_ack = vbus_intr_ack; 228 229 err = hv_intr_settarget(sysino, cpus->ci_cpuid); 230 if (err != H_EOK) { 231 printf("hv_intr_settarget(%lu, %u) failed - err = %d\n", 232 (long unsigned int)sysino, cpus->ci_cpuid, err); 233 return (NULL); 234 } 235 236 /* Clear pending interrupts. */ 237 err = hv_intr_setstate(sysino, INTR_IDLE); 238 if (err != H_EOK) { 239 printf("hv_intr_setstate(%lu, INTR_IDLE) failed - err = %d\n", 240 (long unsigned int)sysino, err); 241 return (NULL); 242 } 243 244 err = hv_intr_setenabled(sysino, INTR_ENABLED); 245 if (err != H_EOK) { 246 printf("hv_intr_setenabled(%lu) failed - err = %d\n", 247 (long unsigned int)sysino, err); 248 return (NULL); 249 } 250 251 return (ih); 252 } 253 254 void 255 vbus_intr_ack(struct intrhand *ih) 256 { 257 DPRINTF(VBUS_INTR, ("vbus_intr_ack()\n")); 258 hv_intr_setstate(ih->ih_number, INTR_IDLE); 259 } 260 261 bus_space_tag_t 262 vbus_alloc_bus_tag(struct vbus_softc *sc, bus_space_tag_t parent) 263 { 264 struct sparc_bus_space_tag *bt; 265 266 bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO); 267 if (bt == NULL) 268 panic("could not allocate vbus bus tag"); 269 270 bt->cookie = sc; 271 bt->parent = parent; 272 bt->sparc_bus_map = parent->sparc_bus_map; 273 bt->sparc_intr_establish = vbus_intr_establish; 274 275 return (bt); 276 } 277