<commit_before>/*	$OpenBSD: cbus.c,v 1.11 2013/07/16 19:45:37 kettenis Exp $	*/
/*
 * Copyright (c) 2008 <NAME>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/systm.h>

#include <machine/autoconf.h>
#include <machine/hypervisor.h>
#include <machine/mdesc.h>
#include <machine/openfirm.h>

#include <sparc64/dev/cbusvar.h>
#include <sparc64/dev/vbusvar.h>

#define CBUS_HANDLE(x) ((x) & ~0xff)
#define CBUS_INO(x) ((x) & 0xff)

struct cbus_softc {
	struct device		sc_dv;
	bus_space_tag_t		sc_bustag;
	bus_dma_tag_t		sc_dmatag;

	/* Machine description. */
	int			sc_idx;
};

int	cbus_match(struct device *, void *, void *);
void	cbus_attach(struct device *, struct device *, void *);
int	cbus_print(void *, const char *);

struct cfattach cbus_ca = {
	sizeof(struct cbus_softc), cbus_match, cbus_attach
};

struct cfdriver cbus_cd = {
	NULL, "cbus", DV_DULL
};

void	*cbus_intr_establish(bus_space_tag_t, bus_space_tag_t, int, int, int,
    int (*)(void *), void *, const char *);
void	cbus_intr_ack(struct intrhand *);
bus_space_tag_t cbus_alloc_bus_tag(struct cbus_softc *, bus_space_tag_t);

int	cbus_get_channel_endpoint(struct cbus_softc *,
	    struct cbus_attach_args *);

int
cbus_match(struct device *parent, void *match, void *aux)
{
	struct vbus_attach_args *va = aux;

	if (strcmp(va->va_name, "channel-devices") == 0)
		return (1);

	return (0);
}

void
cbus_attach(struct device *parent, struct device *self, void *aux)
{
	struct cbus_softc *sc = (struct cbus_softc *)self;
	struct vbus_attach_args *va = aux;
	int node;

	sc->sc_bustag = cbus_alloc_bus_tag(sc, va->va_bustag);
	sc->sc_dmatag = va->va_dmatag;

	printf("\n");

	sc->sc_idx = mdesc_find(va->va_name, va->va_reg[0]);
	if (sc->sc_idx == -1)
		return;

	for (node = OF_child(va->va_node); node; node = OF_peer(node)) {
		struct cbus_attach_args ca;
		char buf[32];

		bzero(&ca, sizeof(ca));
		ca.ca_node = node;
		if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0)
			continue;
		ca.ca_name = buf;
		ca.ca_bustag = sc->sc_bustag;
		ca.ca_dmatag = sc->sc_dmatag;
		getprop(node, "reg", sizeof(*ca.ca_reg),
		    &ca.ca_nreg, (void **)&ca.ca_reg);
		if (cbus_get_channel_endpoint(sc, &ca) != 0)
			continue;

		config_found(self, &ca, cbus_print);
	}
}

int
cbus_print(void *aux, const char *name)
{
	struct cbus_attach_args *ca = aux;

	if (name)
		printf("\"%s\" at %s", ca->ca_name, name);
	if (ca->ca_id != -1)
		printf(" chan 0x%llx", ca->ca_id);
	return (UNCONF);
}

int
cbus_intr_map(int node, int ino, uint64_t *sysino)
{
	int parent;
	int reg;
	int err;

	parent = OF_parent(node);
	if (OF_getprop(parent, "reg", &reg, sizeof(reg)) != sizeof(reg))
		return (-1);

	*sysino = CBUS_HANDLE(reg) | CBUS_INO(ino);
	err = hv_vintr_setcookie(reg, ino, *sysino);
	if (err != H_EOK)
		return (-1);

	return (0);
}

int
cbus_intr_setstate(uint64_t sysino, uint64_t state)
{
	uint64_t devhandle = CBUS_HANDLE(sysino);
	uint64_t devino = CBUS_INO(sysino);
	int err;

	err = hv_vintr_setstate(devhandle, devino, state);
	if (err != H_EOK)
		return (-1);

	return (0);
}

int
cbus_intr_setenabled(uint64_t sysino, uint64_t enabled)
{
	uint64_t devhandle = CBUS_HANDLE(sysino);
	uint64_t devino = CBUS_INO(sysino);
	int err;

	err = hv_vintr_setenabled(devhandle, devino, enabled);
	if (err != H_EOK)
		return (-1);

	return (0);
}

void *
cbus_intr_establish(bus_space_tag_t t, bus_space_tag_t t0, int ihandle,
    int level, int flags, int (*handler)(void *), void *arg, const char *what)
{
	uint64_t devhandle = CBUS_HANDLE(ihandle);
	uint64_t devino = CBUS_INO(ihandle);
	struct intrhand *ih;
	int err;

	ih = bus_intr_allocate(t0, handler, arg, ihandle, level,
	    NULL, NULL, what);
	if (ih == NULL)
		return (NULL);

	err = hv_vintr_setenabled(devhandle, devino, INTR_DISABLED);
	if (err != H_EOK) {
		printf("hv_vintr_setenabled: %d\n", err);
		return (NULL);
	}

	if (flags & BUS_INTR_ESTABLISH_MPSAFE)<commit_msg>Use the pointer to "struct intrhand" as the vintr cookie.<commit_after>/*	$OpenBSD: cbus.c,v 1.12 2014/11/24 10:58:37 kettenis Exp $	*/
/*
 * Copyright (c) 2008 <NAME>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/systm.h>

#include <machine/autoconf.h>
#include <machine/hypervisor.h>
#include <machine/mdesc.h>
#include <machine/openfirm.h>

#include <sparc64/dev/cbusvar.h>
#include <sparc64/dev/vbusvar.h>

#define CBUS_HANDLE(x) ((x) & ~0xff)
#define CBUS_INO(x) ((x) & 0xff)

struct cbus_softc {
	struct device		sc_dv;
	bus_space_tag_t		sc_bustag;
	bus_dma_tag_t		sc_dmatag;

	/* Machine description. */
	int			sc_idx;
};

int	cbus_match(struct device *, void *, void *);
void	cbus_attach(struct device *, struct device *, void *);
int	cbus_print(void *, const char *);

struct cfattach cbus_ca = {
	sizeof(struct cbus_softc), cbus_match, cbus_attach
};

struct cfdriver cbus_cd = {
	NULL, "cbus", DV_DULL
};

void	*cbus_intr_establish(bus_space_tag_t, bus_space_tag_t, int, int, int,
    int (*)(void *), void *, const char *);
void	cbus_intr_ack(struct intrhand *);
bus_space_tag_t cbus_alloc_bus_tag(struct cbus_softc *, bus_space_tag_t);

int	cbus_get_channel_endpoint(struct cbus_softc *,
	    struct cbus_attach_args *);

int
cbus_match(struct device *parent, void *match, void *aux)
{
	struct vbus_attach_args *va = aux;

	if (strcmp(va->va_name, "channel-devices") == 0)
		return (1);

	return (0);
}

void
cbus_attach(struct device *parent, struct device *self, void *aux)
{
	struct cbus_softc *sc = (struct cbus_softc *)self;
	struct vbus_attach_args *va = aux;
	int node;

	sc->sc_bustag = cbus_alloc_bus_tag(sc, va->va_bustag);
	sc->sc_dmatag = va->va_dmatag;

	printf("\n");

	sc->sc_idx = mdesc_find(va->va_name, va->va_reg[0]);
	if (sc->sc_idx == -1)
		return;

	for (node = OF_child(va->va_node); node; node = OF_peer(node)) {
		struct cbus_attach_args ca;
		char buf[32];

		bzero(&ca, sizeof(ca));
		ca.ca_node = node;
		if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0)
			continue;
		ca.ca_name = buf;
		ca.ca_bustag = sc->sc_bustag;
		ca.ca_dmatag = sc->sc_dmatag;
		getprop(node, "reg", sizeof(*ca.ca_reg),
		    &ca.ca_nreg, (void **)&ca.ca_reg);
		if (cbus_get_channel_endpoint(sc, &ca) != 0)
			continue;

		config_found(self, &ca, cbus_print);
	}
}

int
cbus_print(void *aux, const char *name)
{
	struct cbus_attach_args *ca = aux;

	if (name)
		printf("\"%s\" at %s", ca->ca_name, name);
	if (ca->ca_id != -1)
		printf(" chan 0x%llx", ca->ca_id);
	return (UNCONF);
}

int
cbus_intr_map(int node, int ino, uint64_t *sysino)
{
	int parent;
	int reg;

	parent = OF_parent(node);
	if (OF_getprop(parent, "reg", &reg, sizeof(reg)) != sizeof(reg))
		return (-1);

	*sysino = CBUS_HANDLE(reg) | CBUS_INO(ino);
	return (0);
}

int
cbus_intr_setstate(uint64_t sysino, uint64_t state)
{
	uint64_t devhandle = CBUS_HANDLE(sysino);
	uint64_t devino = CBUS_INO(sysino);
	int err;

	err = hv_vintr_setstate(devhandle, devino, state);
	if (err != H_EOK)
		return (-1);

	return (0);
}

int
cbus_intr_setenabled(uint64_t sysino, uint64_t enabled)
{
	uint64_t devhandle = CBUS_HANDLE(sysino);
	uint64_t devino = CBUS_INO(sysino);
	int err;

	err = hv_vintr_setenabled(devhandle, devino, enabled);
	if (err != H_EOK)
		return (-1);

	return (0);
}

void *
cbus_intr_establish(bus_space_tag_t t, bus_space_tag_t t0, int ihandle,
    int level, int flags, int (*handler)(void *), void *arg, const char *what)
{
	uint64_t devhandle = CBUS_HANDLE(ihandle);
	uint64_t devino = CBUS_INO(ihandle);
	struct intrhand *ih;
	int err;

	ih = bus_intr_allocate(t0, handler, arg, ihandle, level,
	    NULL, NULL, what);
	if (ih == NULL)
		return (NULL);

	err = hv_vintr_setenabled(devhandle, devino, INTR_DISABLED);
	if (err != H_EOK) {
		printf("hv_vintr_setenabled: %d\n", err);
		return (NULL);
	}

	err = hv_vintr_setcookie(devhandle, devino, (vaddr_t)ih);
	if (err != H_EOK) {
		printf("hv_vintr_setcookie: %d\n", err);
		return (NULL);
	}

	if (flags & BUS_INTR_ESTABLISH_MPSAFE)
{'fraction_non_alphanumeric': 0.13543323788312653, 'fraction_numerical': 0.014529642592003393, 'mean_word_length': 4.593119810201661, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '319530', 'n_tokens_mistral': 3981, 'n_tokens_neox': 3720, 'n_words': 969}