rumpdev_pci.c revision 1.4
11.4Spooka/*      $NetBSD: rumpdev_pci.c,v 1.4 2014/08/26 10:58:13 pooka Exp $	*/
21.1Spooka
31.1Spooka/*
41.1Spooka * Copyright (c) 2013 Antti Kantee.  All Rights Reserved.
51.1Spooka *
61.1Spooka * Redistribution and use in source and binary forms, with or without
71.1Spooka * modification, are permitted provided that the following conditions
81.1Spooka * are met:
91.1Spooka * 1. Redistributions of source code must retain the above copyright
101.1Spooka *    notice, this list of conditions and the following disclaimer.
111.1Spooka * 2. Redistributions in binary form must reproduce the above copyright
121.1Spooka *    notice, this list of conditions and the following disclaimer in the
131.1Spooka *    documentation and/or other materials provided with the distribution.
141.1Spooka *
151.1Spooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
161.1Spooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
171.1Spooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
181.1Spooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191.1Spooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201.1Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
211.1Spooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221.1Spooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231.1Spooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241.1Spooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251.1Spooka * SUCH DAMAGE.
261.1Spooka */
271.1Spooka
281.1Spooka#include <sys/cdefs.h>
291.4Spooka__KERNEL_RCSID(0, "$NetBSD: rumpdev_pci.c,v 1.4 2014/08/26 10:58:13 pooka Exp $");
301.1Spooka
311.1Spooka#include <sys/cdefs.h>
321.1Spooka#include <sys/param.h>
331.2Spooka#include <sys/atomic.h>
341.1Spooka
351.1Spooka#include <dev/pci/pcivar.h>
361.1Spooka
371.1Spooka#include "pci_user.h"
381.1Spooka
391.1Spookavoid
401.1Spookapci_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba)
411.1Spooka{
421.1Spooka
431.1Spooka	/* nada */
441.1Spooka}
451.1Spooka
461.1Spookaint
471.1Spookapci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
481.1Spooka{
491.1Spooka
501.1Spooka	return 32;
511.1Spooka}
521.1Spooka
531.1Spookapcitag_t
541.1Spookapci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
551.1Spooka{
561.1Spooka	pcitag_t pt;
571.1Spooka	int *tag;
581.1Spooka	unsigned csr;
591.1Spooka	int rv;
601.1Spooka
611.1Spooka	CTASSERT(sizeof(pt) >= sizeof(int));
621.1Spooka
631.1Spooka	/* a "bit" ugly, but keeps us MI */
641.1Spooka	tag = (int *)&pt;
651.1Spooka	*tag = (bus << 16) | (device << 8) | (function << 0);
661.1Spooka
671.1Spooka	/*
681.1Spooka	 * On Xen, we need to enable the device io/mem space.
691.1Spooka	 * Doesn't really belong here, but we need to do it somewhere.
701.1Spooka	 */
711.1Spooka	rv = rumpcomp_pci_confread(bus, device, function,
721.1Spooka	    PCI_COMMAND_STATUS_REG, &csr);
731.1Spooka	if (rv == 0 && (csr & PCI_COMMAND_MEM_ENABLE) == 0) {
741.1Spooka		rumpcomp_pci_confwrite(bus, device, function,
751.1Spooka		    PCI_COMMAND_STATUS_REG, csr | PCI_COMMAND_MEM_ENABLE);
761.1Spooka	}
771.1Spooka
781.1Spooka	return pt;
791.1Spooka}
801.1Spooka
811.1Spookapcireg_t
821.1Spookapci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
831.1Spooka{
841.1Spooka	unsigned int rv;
851.1Spooka	int bus, device, fun;
861.1Spooka
871.1Spooka	pci_decompose_tag(pc, tag, &bus, &device, &fun);
881.1Spooka	rumpcomp_pci_confread(bus, device, fun, reg, &rv);
891.1Spooka	return rv;
901.1Spooka}
911.1Spooka
921.1Spookavoid
931.1Spookapci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
941.1Spooka{
951.1Spooka	int bus, device, fun;
961.1Spooka
971.1Spooka	pci_decompose_tag(pc, tag, &bus, &device, &fun);
981.1Spooka	rumpcomp_pci_confwrite(bus, device, fun, reg, data);
991.1Spooka}
1001.1Spooka
1011.1Spookavoid
1021.1Spookapci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag,
1031.1Spooka	int *bp, int *dp, int *fp)
1041.1Spooka{
1051.1Spooka	int *t = (int *)&tag;
1061.1Spooka
1071.1Spooka	*bp = (*t >> 16) & 0xff;
1081.1Spooka	*dp = (*t >> 8)  & 0xff;
1091.1Spooka	*fp = (*t >> 0)  & 0xff;
1101.1Spooka}
1111.1Spooka
1121.2Spooka/*
1131.2Spooka * Well, yay, deal with the wonders of weird_t.  We'll just
1141.2Spooka * assume it's an integral type (which, btw, isn't universally true).
1151.2Spooka * The hypercall will map "cookie" to its internal structure.
1161.2Spooka * Dial _t for a good time.
1171.2Spooka */
1181.1Spookaint
1191.1Spookapci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
1201.1Spooka{
1211.2Spooka	static unsigned int intrhandle;
1221.2Spooka	unsigned cookie;
1231.2Spooka	int rv;
1241.1Spooka
1251.2Spooka	cookie = atomic_inc_uint_nv(&intrhandle);
1261.2Spooka	rv = rumpcomp_pci_irq_map(pa->pa_bus,
1271.2Spooka	    pa->pa_device, pa->pa_function, pa->pa_intrline, cookie);
1281.2Spooka	if (rv == 0)
1291.2Spooka		*ih = cookie;
1301.1Spooka	return 0;
1311.1Spooka}
1321.1Spooka
1331.1Spookaconst char *
1341.3Spookapci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih,
1351.3Spooka	char *buf, size_t buflen)
1361.1Spooka{
1371.1Spooka
1381.3Spooka	snprintf(buf, buflen, "pausebreak");
1391.3Spooka	return buf;
1401.1Spooka}
1411.1Spooka
1421.1Spookavoid *
1431.1Spookapci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih,
1441.1Spooka	int level, int (*func)(void *), void *arg)
1451.1Spooka{
1461.1Spooka
1471.1Spooka	return rumpcomp_pci_irq_establish(ih, func, arg);
1481.1Spooka}
1491.1Spooka
1501.4Spookaint
1511.4Spookapci_intr_setattr(pci_chipset_tag_t pc, pci_intr_handle_t *ih,
1521.4Spooka	int attr, uint64_t data)
1531.4Spooka{
1541.4Spooka
1551.4Spooka	switch (attr) {
1561.4Spooka	case PCI_INTR_MPSAFE:
1571.4Spooka		return 0;
1581.4Spooka	default:
1591.4Spooka		return ENODEV;
1601.4Spooka	}
1611.4Spooka}
1621.4Spooka
1631.1Spookavoid
1641.2Spookapci_intr_disestablish(pci_chipset_tag_t pc, void *not_your_above_ih)
1651.1Spooka{
1661.1Spooka
1671.1Spooka	panic("%s: unimplemented", __func__);
1681.1Spooka}
169