Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: vbus.c,v 1.9 2022/01/22 11:49:17 thorpej 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/kmem.h>
     23 #include <sys/systm.h>
     24 
     25 #include <machine/autoconf.h>
     26 #include <machine/hypervisor.h>
     27 #include <machine/openfirm.h>
     28 
     29 #include <sparc64/dev/vbusvar.h>
     30 
     31 #include <sparc64/dev/iommureg.h>
     32 
     33 #include <dev/clock_subr.h>
     34 extern todr_chip_handle_t todr_handle;
     35 
     36 #ifdef DEBUG
     37 #define VBUS_INTR             0x01
     38 int vbus_debug = 0x00|VBUS_INTR;
     39 #define DPRINTF(l, s)   do { if (vbus_debug & l) printf s; } while (0)
     40 #else
     41 #define DPRINTF(l, s)
     42 #endif
     43 
     44 struct vbus_softc {
     45 	device_t		sc_dv;
     46 	bus_space_tag_t		sc_bustag;
     47 	bus_dma_tag_t		sc_dmatag;
     48 };
     49 int	vbus_cmp_cells(int *, int *, int *, int);
     50 int	vbus_match(device_t, cfdata_t, void *);
     51 void	vbus_attach(device_t, device_t, void *);
     52 int	vbus_print(void *, const char *);
     53 
     54 CFATTACH_DECL_NEW(vbus, sizeof(struct vbus_softc),
     55     vbus_match, vbus_attach, NULL, NULL);
     56 
     57 void *vbus_intr_establish(bus_space_tag_t, int, int,
     58     int (*)(void *), void *, void (*)(void));
     59 void	vbus_intr_ack(struct intrhand *);
     60 bus_space_tag_t vbus_alloc_bus_tag(struct vbus_softc *, bus_space_tag_t);
     61 
     62 int
     63 vbus_match(device_t parent, cfdata_t match, void *aux)
     64 {
     65 	struct mainbus_attach_args *ma = aux;
     66 
     67 	if (strcmp(ma->ma_name, "virtual-devices") == 0)
     68 		return (1);
     69 
     70 	return (0);
     71 }
     72 
     73 void
     74 vbus_attach(device_t parent, device_t self, void *aux)
     75 {
     76         struct vbus_softc *sc = device_private(self);
     77 	struct mainbus_attach_args *ma = aux;
     78 	int node;
     79 
     80 	sc->sc_bustag = vbus_alloc_bus_tag(sc, ma->ma_bustag);
     81 	sc->sc_dmatag = ma->ma_dmatag;
     82 	printf("\n");
     83 
     84 	devhandle_t selfh = device_handle(self);
     85 	for (node = OF_child(ma->ma_node); node; node = OF_peer(node)) {
     86 		struct vbus_attach_args va;
     87 		char buf[32];
     88 
     89 		bzero(&va, sizeof(va));
     90 		va.va_node = node;
     91 		if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0)
     92 			continue;
     93 		va.va_name = buf;
     94 		va.va_bustag = sc->sc_bustag;
     95 		va.va_dmatag = sc->sc_dmatag;
     96 		prom_getprop(node, "reg", sizeof(*va.va_reg),
     97 			     &va.va_nreg, (void **)&va.va_reg);
     98 		prom_getprop(node, "interrupts", sizeof(*va.va_intr),
     99 			     &va.va_nintr, (void **)&va.va_intr);
    100 		config_found(self, &va, vbus_print,
    101 		    CFARGS(.devhandle = prom_node_to_devhandle(selfh,
    102 							       va.va_node)));
    103 	}
    104 
    105 	struct vbus_attach_args va;
    106 	bzero(&va, sizeof(va));
    107 	va.va_name = "rtc";
    108 	config_found(self, &va, vbus_print, CFARGS_NONE);
    109 
    110 }
    111 
    112 int
    113 vbus_print(void *aux, const char *name)
    114 {
    115 	struct vbus_attach_args *va = aux;
    116 
    117 	if (name)
    118 		printf("\"%s\" at %s", va->va_name, name);
    119 	return (UNCONF);
    120 }
    121 
    122 /*
    123  * Compare a sequence of cells with a mask, return 1 if they match and
    124  * 0 if they don't.
    125  */
    126 int
    127 vbus_cmp_cells(int *cell1, int *cell2, int *mask, int ncells)
    128 {
    129 	int i;
    130 
    131 	for (i = 0; i < ncells; i++) {
    132 		if (((cell1[i] ^ cell2[i]) & mask[i]) != 0)
    133 			return (0);
    134 	}
    135 	return (1);
    136 }
    137 
    138 int
    139 vbus_intr_map(int node, int ino, uint64_t *sysino)
    140 {
    141 	int *imap = NULL, nimap;
    142 	int *reg = NULL, nreg;
    143 	int *imap_mask;
    144 	int parent;
    145 	int address_cells, interrupt_cells;
    146 	uint64_t devhandle;
    147 	uint64_t devino;
    148 	int len;
    149 	int err;
    150 
    151 	DPRINTF(VBUS_INTR, ("vbus_intr_map(): ino 0x%x\n", ino));
    152 
    153 	parent = OF_parent(node);
    154 
    155 	address_cells = prom_getpropint(parent, "#address-cells", 2);
    156 	interrupt_cells = prom_getpropint(parent, "#interrupt-cells", 1);
    157 	KASSERT(interrupt_cells == 1);
    158 
    159 	len = OF_getproplen(parent, "interrupt-map-mask");
    160 	if (len < (address_cells + interrupt_cells) * sizeof(int))
    161 		return (-1);
    162 	imap_mask = malloc(len, M_DEVBUF, M_WAITOK);
    163 	if (OF_getprop(parent, "interrupt-map-mask", imap_mask, len) != len)
    164 		goto out;
    165 
    166 	if (prom_getprop(parent, "interrupt-map", sizeof(int), &nimap, (void **)&imap))
    167 		panic("vbus: can't get interrupt-map");
    168 
    169 	if (prom_getprop(node, "reg", sizeof(*reg), &nreg, (void **)&reg))
    170 		panic("vbus: can't get reg");
    171 	if (nreg < address_cells)
    172 		goto out;
    173 
    174 	while (nimap >= address_cells + interrupt_cells + 2) {
    175 		if (vbus_cmp_cells(imap, reg, imap_mask, address_cells) &&
    176 		    vbus_cmp_cells(&imap[address_cells], &ino,
    177 		    &imap_mask[address_cells], interrupt_cells)) {
    178 			node = imap[address_cells + interrupt_cells];
    179 			devino = imap[address_cells + interrupt_cells + 1];
    180 
    181 			free(reg, M_DEVBUF);
    182 			reg = NULL;
    183 
    184 			if (prom_getprop(node, "reg", sizeof(*reg), &nreg, (void **)&reg))
    185 				panic("vbus: can't get reg");
    186 
    187 			devhandle = reg[0] & 0x0fffffff;
    188 
    189 			err = hv_intr_devino_to_sysino(devhandle, devino, sysino);
    190 			if (err != H_EOK)
    191 				goto out;
    192 
    193 			KASSERT(*sysino == INTVEC(*sysino));
    194 			free(imap_mask, M_DEVBUF);
    195 			return (0);
    196 		}
    197 		imap += address_cells + interrupt_cells + 2;
    198 		nimap -= address_cells + interrupt_cells + 2;
    199 	}
    200 
    201 out:
    202 	free(imap_mask, M_DEVBUF);
    203 	return (-1);
    204 }
    205 
    206 void *
    207 vbus_intr_establish(bus_space_tag_t t, int ihandle, int level,
    208 	int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
    209 {
    210 	uint64_t sysino = INTVEC(ihandle);
    211 	struct intrhand *ih;
    212 	int ino;
    213 	int err;
    214 
    215 	DPRINTF(VBUS_INTR, ("vbus_intr_establish()\n"));
    216 
    217 	ino = INTINO(ihandle);
    218 
    219 	ih = intrhand_alloc();
    220 
    221 	ih->ih_ivec = ihandle;
    222 	ih->ih_fun = handler;
    223 	ih->ih_arg = arg;
    224 	ih->ih_pil = level;
    225 	ih->ih_number = ino;
    226 	ih->ih_pending = 0;
    227 
    228 	intr_establish(ih->ih_pil, level != IPL_VM, ih);
    229 	ih->ih_ack = vbus_intr_ack;
    230 
    231 	err = hv_intr_settarget(sysino, cpus->ci_cpuid);
    232 	if (err != H_EOK) {
    233 		printf("hv_intr_settarget(%lu, %u) failed - err = %d\n",
    234 		       (long unsigned int)sysino, cpus->ci_cpuid, err);
    235 		return (NULL);
    236 	}
    237 
    238 	/* Clear pending interrupts. */
    239 	err = hv_intr_setstate(sysino, INTR_IDLE);
    240 	if (err != H_EOK) {
    241 	  printf("hv_intr_setstate(%lu, INTR_IDLE) failed - err = %d\n",
    242 		 (long unsigned int)sysino, err);
    243 	  return (NULL);
    244 	}
    245 
    246 	err = hv_intr_setenabled(sysino, INTR_ENABLED);
    247 	if (err != H_EOK) {
    248 	  printf("hv_intr_setenabled(%lu) failed - err = %d\n",
    249 		 (long unsigned int)sysino, err);
    250 	  return (NULL);
    251 	}
    252 
    253 	return (ih);
    254 }
    255 
    256 void
    257 vbus_intr_ack(struct intrhand *ih)
    258 {
    259 	DPRINTF(VBUS_INTR, ("vbus_intr_ack()\n"));
    260 	hv_intr_setstate(ih->ih_number, INTR_IDLE);
    261 }
    262 
    263 bus_space_tag_t
    264 vbus_alloc_bus_tag(struct vbus_softc *sc, bus_space_tag_t parent)
    265 {
    266 	struct sparc_bus_space_tag *bt;
    267 
    268 	bt = kmem_zalloc(sizeof(*bt), KM_SLEEP);
    269 	bt->cookie = sc;
    270 	bt->parent = parent;
    271 	bt->sparc_bus_map = parent->sparc_bus_map;
    272 	bt->sparc_intr_establish = vbus_intr_establish;
    273 
    274 	return (bt);
    275 }
    276