1*3944ff70Sthorpej /* $NetBSD: vbus.c,v 1.9 2022/01/22 11:49:17 thorpej Exp $ */
282352513Spalle /* $OpenBSD: vbus.c,v 1.8 2015/09/27 11:29:20 kettenis Exp $ */
382352513Spalle /*
482352513Spalle * Copyright (c) 2008 Mark Kettenis
582352513Spalle *
682352513Spalle * Permission to use, copy, modify, and distribute this software for any
782352513Spalle * purpose with or without fee is hereby granted, provided that the above
882352513Spalle * copyright notice and this permission notice appear in all copies.
982352513Spalle *
1082352513Spalle * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1182352513Spalle * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1282352513Spalle * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1382352513Spalle * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1482352513Spalle * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1582352513Spalle * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1682352513Spalle * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1782352513Spalle */
1882352513Spalle
1982352513Spalle #include <sys/param.h>
2082352513Spalle #include <sys/device.h>
2182352513Spalle #include <sys/malloc.h>
22fc256c4aSthorpej #include <sys/kmem.h>
2382352513Spalle #include <sys/systm.h>
2482352513Spalle
2582352513Spalle #include <machine/autoconf.h>
2682352513Spalle #include <machine/hypervisor.h>
2782352513Spalle #include <machine/openfirm.h>
2882352513Spalle
2982352513Spalle #include <sparc64/dev/vbusvar.h>
3082352513Spalle
3182352513Spalle #include <sparc64/dev/iommureg.h>
3282352513Spalle
3382352513Spalle #include <dev/clock_subr.h>
3482352513Spalle extern todr_chip_handle_t todr_handle;
3582352513Spalle
3682352513Spalle #ifdef DEBUG
3782352513Spalle #define VBUS_INTR 0x01
3882352513Spalle int vbus_debug = 0x00|VBUS_INTR;
3982352513Spalle #define DPRINTF(l, s) do { if (vbus_debug & l) printf s; } while (0)
4082352513Spalle #else
4182352513Spalle #define DPRINTF(l, s)
4282352513Spalle #endif
4382352513Spalle
4482352513Spalle struct vbus_softc {
4582352513Spalle device_t sc_dv;
4682352513Spalle bus_space_tag_t sc_bustag;
4782352513Spalle bus_dma_tag_t sc_dmatag;
4882352513Spalle };
4982352513Spalle int vbus_cmp_cells(int *, int *, int *, int);
5082352513Spalle int vbus_match(device_t, cfdata_t, void *);
5182352513Spalle void vbus_attach(device_t, device_t, void *);
5282352513Spalle int vbus_print(void *, const char *);
5382352513Spalle
5482352513Spalle CFATTACH_DECL_NEW(vbus, sizeof(struct vbus_softc),
5582352513Spalle vbus_match, vbus_attach, NULL, NULL);
5682352513Spalle
5782352513Spalle void *vbus_intr_establish(bus_space_tag_t, int, int,
5882352513Spalle int (*)(void *), void *, void (*)(void));
5982352513Spalle void vbus_intr_ack(struct intrhand *);
6082352513Spalle bus_space_tag_t vbus_alloc_bus_tag(struct vbus_softc *, bus_space_tag_t);
6182352513Spalle
6282352513Spalle int
vbus_match(device_t parent,cfdata_t match,void * aux)6382352513Spalle vbus_match(device_t parent, cfdata_t match, void *aux)
6482352513Spalle {
6582352513Spalle struct mainbus_attach_args *ma = aux;
6682352513Spalle
6782352513Spalle if (strcmp(ma->ma_name, "virtual-devices") == 0)
6882352513Spalle return (1);
6982352513Spalle
7082352513Spalle return (0);
7182352513Spalle }
7282352513Spalle
7382352513Spalle void
vbus_attach(device_t parent,device_t self,void * aux)7482352513Spalle vbus_attach(device_t parent, device_t self, void *aux)
7582352513Spalle {
763eb86195Spalle struct vbus_softc *sc = device_private(self);
7782352513Spalle struct mainbus_attach_args *ma = aux;
7882352513Spalle int node;
7982352513Spalle
8082352513Spalle sc->sc_bustag = vbus_alloc_bus_tag(sc, ma->ma_bustag);
8182352513Spalle sc->sc_dmatag = ma->ma_dmatag;
8282352513Spalle printf("\n");
8382352513Spalle
84*3944ff70Sthorpej devhandle_t selfh = device_handle(self);
8582352513Spalle for (node = OF_child(ma->ma_node); node; node = OF_peer(node)) {
8682352513Spalle struct vbus_attach_args va;
8782352513Spalle char buf[32];
8882352513Spalle
8982352513Spalle bzero(&va, sizeof(va));
9082352513Spalle va.va_node = node;
9182352513Spalle if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0)
9282352513Spalle continue;
9382352513Spalle va.va_name = buf;
9482352513Spalle va.va_bustag = sc->sc_bustag;
9582352513Spalle va.va_dmatag = sc->sc_dmatag;
9682352513Spalle prom_getprop(node, "reg", sizeof(*va.va_reg),
9782352513Spalle &va.va_nreg, (void **)&va.va_reg);
9882352513Spalle prom_getprop(node, "interrupts", sizeof(*va.va_intr),
9982352513Spalle &va.va_nintr, (void **)&va.va_intr);
10065c738d1Sthorpej config_found(self, &va, vbus_print,
101*3944ff70Sthorpej CFARGS(.devhandle = prom_node_to_devhandle(selfh,
102*3944ff70Sthorpej va.va_node)));
10382352513Spalle }
10482352513Spalle
10582352513Spalle struct vbus_attach_args va;
10682352513Spalle bzero(&va, sizeof(va));
10782352513Spalle va.va_name = "rtc";
108c7fb772bSthorpej config_found(self, &va, vbus_print, CFARGS_NONE);
10982352513Spalle
11082352513Spalle }
11182352513Spalle
11282352513Spalle int
vbus_print(void * aux,const char * name)11382352513Spalle vbus_print(void *aux, const char *name)
11482352513Spalle {
11582352513Spalle struct vbus_attach_args *va = aux;
11682352513Spalle
11782352513Spalle if (name)
11882352513Spalle printf("\"%s\" at %s", va->va_name, name);
11982352513Spalle return (UNCONF);
12082352513Spalle }
12182352513Spalle
12282352513Spalle /*
12382352513Spalle * Compare a sequence of cells with a mask, return 1 if they match and
12482352513Spalle * 0 if they don't.
12582352513Spalle */
12682352513Spalle int
vbus_cmp_cells(int * cell1,int * cell2,int * mask,int ncells)12782352513Spalle vbus_cmp_cells(int *cell1, int *cell2, int *mask, int ncells)
12882352513Spalle {
12982352513Spalle int i;
13082352513Spalle
13182352513Spalle for (i = 0; i < ncells; i++) {
13282352513Spalle if (((cell1[i] ^ cell2[i]) & mask[i]) != 0)
13382352513Spalle return (0);
13482352513Spalle }
13582352513Spalle return (1);
13682352513Spalle }
13782352513Spalle
13882352513Spalle int
vbus_intr_map(int node,int ino,uint64_t * sysino)13982352513Spalle vbus_intr_map(int node, int ino, uint64_t *sysino)
14082352513Spalle {
14182352513Spalle int *imap = NULL, nimap;
14282352513Spalle int *reg = NULL, nreg;
14382352513Spalle int *imap_mask;
14482352513Spalle int parent;
14582352513Spalle int address_cells, interrupt_cells;
14682352513Spalle uint64_t devhandle;
14782352513Spalle uint64_t devino;
14882352513Spalle int len;
14982352513Spalle int err;
15082352513Spalle
15182352513Spalle DPRINTF(VBUS_INTR, ("vbus_intr_map(): ino 0x%x\n", ino));
15282352513Spalle
15382352513Spalle parent = OF_parent(node);
15482352513Spalle
15582352513Spalle address_cells = prom_getpropint(parent, "#address-cells", 2);
15682352513Spalle interrupt_cells = prom_getpropint(parent, "#interrupt-cells", 1);
15782352513Spalle KASSERT(interrupt_cells == 1);
15882352513Spalle
15982352513Spalle len = OF_getproplen(parent, "interrupt-map-mask");
16082352513Spalle if (len < (address_cells + interrupt_cells) * sizeof(int))
16182352513Spalle return (-1);
162d47bcd29Schs imap_mask = malloc(len, M_DEVBUF, M_WAITOK);
16382352513Spalle if (OF_getprop(parent, "interrupt-map-mask", imap_mask, len) != len)
164a3a1c0fbSpalle goto out;
16582352513Spalle
16682352513Spalle if (prom_getprop(parent, "interrupt-map", sizeof(int), &nimap, (void **)&imap))
16782352513Spalle panic("vbus: can't get interrupt-map");
16882352513Spalle
16982352513Spalle if (prom_getprop(node, "reg", sizeof(*reg), &nreg, (void **)®))
17082352513Spalle panic("vbus: can't get reg");
17182352513Spalle if (nreg < address_cells)
172a3a1c0fbSpalle goto out;
17382352513Spalle
17482352513Spalle while (nimap >= address_cells + interrupt_cells + 2) {
17582352513Spalle if (vbus_cmp_cells(imap, reg, imap_mask, address_cells) &&
17682352513Spalle vbus_cmp_cells(&imap[address_cells], &ino,
17782352513Spalle &imap_mask[address_cells], interrupt_cells)) {
17882352513Spalle node = imap[address_cells + interrupt_cells];
17982352513Spalle devino = imap[address_cells + interrupt_cells + 1];
18082352513Spalle
18182352513Spalle free(reg, M_DEVBUF);
18282352513Spalle reg = NULL;
18382352513Spalle
18482352513Spalle if (prom_getprop(node, "reg", sizeof(*reg), &nreg, (void **)®))
18582352513Spalle panic("vbus: can't get reg");
18682352513Spalle
18782352513Spalle devhandle = reg[0] & 0x0fffffff;
18882352513Spalle
18982352513Spalle err = hv_intr_devino_to_sysino(devhandle, devino, sysino);
19082352513Spalle if (err != H_EOK)
191a3a1c0fbSpalle goto out;
19282352513Spalle
19382352513Spalle KASSERT(*sysino == INTVEC(*sysino));
194a3a1c0fbSpalle free(imap_mask, M_DEVBUF);
19582352513Spalle return (0);
19682352513Spalle }
19782352513Spalle imap += address_cells + interrupt_cells + 2;
19882352513Spalle nimap -= address_cells + interrupt_cells + 2;
19982352513Spalle }
20082352513Spalle
201a3a1c0fbSpalle out:
202a3a1c0fbSpalle free(imap_mask, M_DEVBUF);
20382352513Spalle return (-1);
20482352513Spalle }
20582352513Spalle
20682352513Spalle void *
vbus_intr_establish(bus_space_tag_t t,int ihandle,int level,int (* handler)(void *),void * arg,void (* fastvec)(void))20782352513Spalle vbus_intr_establish(bus_space_tag_t t, int ihandle, int level,
20882352513Spalle int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
20982352513Spalle {
21082352513Spalle uint64_t sysino = INTVEC(ihandle);
21182352513Spalle struct intrhand *ih;
21282352513Spalle int ino;
21382352513Spalle int err;
21482352513Spalle
21582352513Spalle DPRINTF(VBUS_INTR, ("vbus_intr_establish()\n"));
21682352513Spalle
21782352513Spalle ino = INTINO(ihandle);
21882352513Spalle
21982352513Spalle ih = intrhand_alloc();
22082352513Spalle
22182352513Spalle ih->ih_ivec = ihandle;
22282352513Spalle ih->ih_fun = handler;
22382352513Spalle ih->ih_arg = arg;
22482352513Spalle ih->ih_pil = level;
22582352513Spalle ih->ih_number = ino;
22682352513Spalle ih->ih_pending = 0;
22782352513Spalle
22882352513Spalle intr_establish(ih->ih_pil, level != IPL_VM, ih);
22982352513Spalle ih->ih_ack = vbus_intr_ack;
23082352513Spalle
23182352513Spalle err = hv_intr_settarget(sysino, cpus->ci_cpuid);
23282352513Spalle if (err != H_EOK) {
23382352513Spalle printf("hv_intr_settarget(%lu, %u) failed - err = %d\n",
23482352513Spalle (long unsigned int)sysino, cpus->ci_cpuid, err);
23582352513Spalle return (NULL);
23682352513Spalle }
23782352513Spalle
23882352513Spalle /* Clear pending interrupts. */
23982352513Spalle err = hv_intr_setstate(sysino, INTR_IDLE);
24082352513Spalle if (err != H_EOK) {
24182352513Spalle printf("hv_intr_setstate(%lu, INTR_IDLE) failed - err = %d\n",
24282352513Spalle (long unsigned int)sysino, err);
24382352513Spalle return (NULL);
24482352513Spalle }
24582352513Spalle
24682352513Spalle err = hv_intr_setenabled(sysino, INTR_ENABLED);
24782352513Spalle if (err != H_EOK) {
24882352513Spalle printf("hv_intr_setenabled(%lu) failed - err = %d\n",
24982352513Spalle (long unsigned int)sysino, err);
25082352513Spalle return (NULL);
25182352513Spalle }
25282352513Spalle
25382352513Spalle return (ih);
25482352513Spalle }
25582352513Spalle
25682352513Spalle void
vbus_intr_ack(struct intrhand * ih)25782352513Spalle vbus_intr_ack(struct intrhand *ih)
25882352513Spalle {
25982352513Spalle DPRINTF(VBUS_INTR, ("vbus_intr_ack()\n"));
26082352513Spalle hv_intr_setstate(ih->ih_number, INTR_IDLE);
26182352513Spalle }
26282352513Spalle
26382352513Spalle bus_space_tag_t
vbus_alloc_bus_tag(struct vbus_softc * sc,bus_space_tag_t parent)26482352513Spalle vbus_alloc_bus_tag(struct vbus_softc *sc, bus_space_tag_t parent)
26582352513Spalle {
26682352513Spalle struct sparc_bus_space_tag *bt;
26782352513Spalle
268fc256c4aSthorpej bt = kmem_zalloc(sizeof(*bt), KM_SLEEP);
26982352513Spalle bt->cookie = sc;
27082352513Spalle bt->parent = parent;
27182352513Spalle bt->sparc_bus_map = parent->sparc_bus_map;
27282352513Spalle bt->sparc_intr_establish = vbus_intr_establish;
27382352513Spalle
27482352513Spalle return (bt);
27582352513Spalle }
276