summaryrefslogtreecommitdiff
path: root/sys/rump/dev/lib/libpci/rumpdev_pci.c
blob: 249076ab3a1eb4dc2d1b7961807cc75251ee7fe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*      $NetBSD: rumpdev_pci.c,v 1.5 2015/05/17 13:51:31 pooka Exp $	*/

/*
 * Copyright (c) 2013 Antti Kantee.  All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: rumpdev_pci.c,v 1.5 2015/05/17 13:51:31 pooka Exp $");

#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/atomic.h>

#include <dev/pci/pcivar.h>

#include "pci_user.h"

void
pci_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba)
{

	/* nada */
}

int
pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
{

	return 32;
}

pcitag_t
pci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
{
	pcitag_t pt;
	int *tag;
	unsigned csr;
	int rv;

	CTASSERT(sizeof(pt) >= sizeof(int));

	/* a "bit" ugly, but keeps us MI */
	tag = (int *)&pt;
	*tag = (bus << 16) | (device << 8) | (function << 0);

	/*
	 * On Xen, we need to enable the device io/mem space.
	 * Doesn't really belong here, but we need to do it somewhere.
	 */
	rv = rumpcomp_pci_confread(bus, device, function,
	    PCI_COMMAND_STATUS_REG, &csr);
	if (rv == 0 && (csr & PCI_COMMAND_MEM_ENABLE) == 0) {
		rumpcomp_pci_confwrite(bus, device, function,
		    PCI_COMMAND_STATUS_REG, csr | PCI_COMMAND_MEM_ENABLE);
	}

	return pt;
}

pcireg_t
pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
{
	unsigned int rv;
	int bus, device, fun;

	pci_decompose_tag(pc, tag, &bus, &device, &fun);
	rumpcomp_pci_confread(bus, device, fun, reg, &rv);
	return rv;
}

void
pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
{
	int bus, device, fun;

	pci_decompose_tag(pc, tag, &bus, &device, &fun);
	rumpcomp_pci_confwrite(bus, device, fun, reg, data);
}

void
pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag,
	int *bp, int *dp, int *fp)
{
	int *t = (int *)&tag;

	*bp = (*t >> 16) & 0xff;
	*dp = (*t >> 8)  & 0xff;
	*fp = (*t >> 0)  & 0xff;
}

/*
 * Well, yay, deal with the wonders of weird_t.  We'll just
 * assume it's an integral type (which, btw, isn't universally true).
 * The hypercall will map "cookie" to its internal structure.
 * Dial _t for a good time.
 */
int
pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
{
	static unsigned int intrhandle;
	unsigned cookie;
	int rv;

	cookie = atomic_inc_uint_nv(&intrhandle);
	rv = rumpcomp_pci_irq_map(pa->pa_bus,
	    pa->pa_device, pa->pa_function, pa->pa_intrline, cookie);
	if (rv == 0)
		*ih = cookie;
	return 0;
}

const char *
pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih,
	char *buf, size_t buflen)
{

	snprintf(buf, buflen, "pausebreak");
	return buf;
}

void *
pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih,
	int level, int (*func)(void *), void *arg)
{

	return rumpcomp_pci_irq_establish(ih, func, arg);
}

int
pci_intr_setattr(pci_chipset_tag_t pc, pci_intr_handle_t *ih,
	int attr, uint64_t data)
{

	switch (attr) {
	case PCI_INTR_MPSAFE:
		return 0;
	default:
		return ENODEV;
	}
}

void
pci_intr_disestablish(pci_chipset_tag_t pc, void *not_your_above_ih)
{

	panic("%s: unimplemented", __func__);
}

#ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pciidereg.h>
#include <dev/pci/pciidevar.h>

void *
pciide_machdep_compat_intr_establish(device_t dev,
	const struct pci_attach_args *pa, int chan,
	int (*func)(void *), void *arg)
{
	pci_intr_handle_t ih;
	struct pci_attach_args mypa = *pa;

	mypa.pa_intrline = PCIIDE_COMPAT_IRQ(chan);
	if (pci_intr_map(&mypa, &ih) != 0)
		return NULL;
	return rumpcomp_pci_irq_establish(ih, func, arg);
}

__strong_alias(pciide_machdep_compat_intr_disestablish,pci_intr_disestablish);
#endif /* __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH */