Home | History | Annotate | Line # | Download | only in dev
vbus.c revision 1.2
      1 /*	$NetBSD: vbus.c,v 1.1 2016/06/17 21:59:06 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 		return (-1);
    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 **)&reg))
    168 		  panic("vbus: can't get reg");
    169 	if (nreg < address_cells)
    170 		return (-1);
    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 **)&reg))
    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 			  return (-1);
    190 
    191 			KASSERT(*sysino == INTVEC(*sysino));
    192 			return (0);
    193 		}
    194 		imap += address_cells + interrupt_cells + 2;
    195 		nimap -= address_cells + interrupt_cells + 2;
    196 	}
    197 
    198 	return (-1);
    199 }
    200 
    201 void *
    202 vbus_intr_establish(bus_space_tag_t t, int ihandle, int level,
    203 	int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
    204 {
    205 	uint64_t sysino = INTVEC(ihandle);
    206 	struct intrhand *ih;
    207 	int ino;
    208 	int err;
    209 
    210 	DPRINTF(VBUS_INTR, ("vbus_intr_establish()\n"));
    211 
    212 	ino = INTINO(ihandle);
    213 
    214 	ih = intrhand_alloc();
    215 
    216 	ih->ih_ivec = ihandle;
    217 	ih->ih_fun = handler;
    218 	ih->ih_arg = arg;
    219 	ih->ih_pil = level;
    220 	ih->ih_number = ino;
    221 	ih->ih_pending = 0;
    222 
    223 	intr_establish(ih->ih_pil, level != IPL_VM, ih);
    224 	ih->ih_ack = vbus_intr_ack;
    225 
    226 	err = hv_intr_settarget(sysino, cpus->ci_cpuid);
    227 	if (err != H_EOK) {
    228 		printf("hv_intr_settarget(%lu, %u) failed - err = %d\n",
    229 		       (long unsigned int)sysino, cpus->ci_cpuid, err);
    230 		return (NULL);
    231 	}
    232 
    233 	/* Clear pending interrupts. */
    234 	err = hv_intr_setstate(sysino, INTR_IDLE);
    235 	if (err != H_EOK) {
    236 	  printf("hv_intr_setstate(%lu, INTR_IDLE) failed - err = %d\n",
    237 		 (long unsigned int)sysino, err);
    238 	  return (NULL);
    239 	}
    240 
    241 	err = hv_intr_setenabled(sysino, INTR_ENABLED);
    242 	if (err != H_EOK) {
    243 	  printf("hv_intr_setenabled(%lu) failed - err = %d\n",
    244 		 (long unsigned int)sysino, err);
    245 	  return (NULL);
    246 	}
    247 
    248 	return (ih);
    249 }
    250 
    251 void
    252 vbus_intr_ack(struct intrhand *ih)
    253 {
    254 	DPRINTF(VBUS_INTR, ("vbus_intr_ack()\n"));
    255 	hv_intr_setstate(ih->ih_number, INTR_IDLE);
    256 }
    257 
    258 bus_space_tag_t
    259 vbus_alloc_bus_tag(struct vbus_softc *sc, bus_space_tag_t parent)
    260 {
    261 	struct sparc_bus_space_tag *bt;
    262 
    263 	bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO);
    264 	if (bt == NULL)
    265 		panic("could not allocate vbus bus tag");
    266 
    267 	bt->cookie = sc;
    268 	bt->parent = parent;
    269 	bt->sparc_bus_map = parent->sparc_bus_map;
    270 	bt->sparc_intr_establish = vbus_intr_establish;
    271 
    272 	return (bt);
    273 }
    274