aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/vmd/vmd_bus.c
blob: 5eed6176630f390a5c8726a6c71ebce65905671a (plain) (blame)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright 2019 Cisco Systems, Inc.
 * 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 AND CONTRIBUTORS ``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>
__FBSDID("$FreeBSD$");

#include <sys/types.h>
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/systm.h>
#include <sys/malloc.h>

#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>
#include <sys/taskqueue.h>

#include <sys/pciio.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pci_private.h>
#include <dev/pci/pcib_private.h>
#include <dev/pci/pci_host_generic.h>
#include <dev/vmd/vmd.h>

#include "pcib_if.h"
#include "pci_if.h"

static int
vmd_bus_probe(device_t dev)
{
	device_set_desc(dev, "VMD bus");

	return (-1000);
}

/* PCI interface. */

static int
vmd_bus_attach(device_t dev)
{
	struct vmd_softc *sc;
	struct pci_devinfo *dinfo;
	rman_res_t start, end;
	int b, s, f;
	int found;

	sc = device_get_softc(device_get_parent(dev));

	/*
	 * Start at max PCI vmd_domain and work down.  Only VMD
	 * starting bus is connect to VMD device directly.  Scan al
	 * lslots and function connected to starting bus.
	 */

	b = sc->vmd_bus_start;

	found = 0;
	for (s = 0; s < PCI_SLOTMAX; s++) {
		for (f = 0; f < PCI_FUNCMAX; f++) {
			dinfo = pci_read_device(device_get_parent(dev), dev,
			     PCI_DOMAINMAX - device_get_unit(
			     device_get_parent(dev)), b, s, f);
			if (dinfo != NULL) {
				found = 1;
				pci_add_child(dev, dinfo);

				start = rman_get_start(sc->vmd_regs_resource[1]);
				end = rman_get_end(sc->vmd_regs_resource[1]);
				resource_list_add_next(&dinfo->resources,
				    SYS_RES_MEMORY, start, end, end - start + 1);

				start = rman_get_start(sc->vmd_io_resource);
				end = rman_get_end(sc->vmd_io_resource);
				resource_list_add_next(&dinfo->resources,
				    SYS_RES_IOPORT, start, end, end - start + 1);

			}
		}
	}

	if (found) {
		bus_generic_attach(dev);
	}

	return (0);
}

static int
vmd_bus_detach(device_t dev)
{
	struct vmd_softc *sc;
	struct pci_devinfo *dinfo;
	int b, s, f;

	device_delete_children(dev);

	sc = device_get_softc(device_get_parent(dev));
	b = sc->vmd_bus_start;

	for (s = 0; s < PCI_SLOTMAX; s++) {
		for (f = 0; f < PCI_FUNCMAX; f++) {
			dinfo = pci_read_device(device_get_parent(dev), dev,
			     PCI_DOMAINMAX - device_get_unit(
			     device_get_parent(dev)), b, s, f);
			if (dinfo != NULL)
				resource_list_free(&dinfo->resources);
		}
	}
	return (0);
}

static int
vmd_bus_adjust_resource(device_t dev, device_t child, int type,
    struct resource *r, rman_res_t start, rman_res_t end)
{
	struct resource *res = r;
	if (type == SYS_RES_MEMORY) {
		/* VMD device controls this */
		return (0);
	}

	return (bus_generic_adjust_resource(dev, child, type, res, start, end));
}

static int
vmd_bus_release_resource(device_t dev, device_t child, int type, int rid,
    struct resource *r)
{
	if (type == SYS_RES_MEMORY) {
		/* VMD device controls this */
		return (0);
	}

	return (pci_release_resource(dev, child, type, rid, r));
}

static struct resource *
vmd_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
    rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
{
	struct vmd_softc *sc;

	sc = device_get_softc(device_get_parent(dev));

	if (type == SYS_RES_MEMORY) {
		/* remap to VMD resources */
		if (*rid == PCIR_MEMBASE_1) {
			return (sc->vmd_regs_resource[1]);
		} else 	if (*rid == PCIR_PMBASEL_1) {
			return (sc->vmd_regs_resource[2]);
		} else {
			return (sc->vmd_regs_resource[2]);
		}
	}
	return (pci_alloc_resource(dev, child, type, rid, start, end,
	   count, flags));
}

static int
vmd_bus_shutdown(device_t dev)
{
	return (0);
}

static device_method_t vmd_bus_pci_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		vmd_bus_probe),
	DEVMETHOD(device_attach,	vmd_bus_attach),
	DEVMETHOD(device_detach,	vmd_bus_detach),
	DEVMETHOD(device_shutdown,	vmd_bus_shutdown),

	/* Bus interface */
	DEVMETHOD(bus_alloc_resource,	vmd_bus_alloc_resource),
	DEVMETHOD(bus_adjust_resource,	vmd_bus_adjust_resource),
	DEVMETHOD(bus_release_resource,	vmd_bus_release_resource),

	/* pci interface */
	DEVMETHOD(pci_read_config,	pci_read_config_method),
	DEVMETHOD(pci_write_config,	pci_write_config_method),
	DEVMETHOD(pci_alloc_devinfo,	pci_alloc_devinfo_method),

	DEVMETHOD_END
};

static devclass_t vmd_bus_devclass;

DEFINE_CLASS_1(vmd_bus, vmd_bus_pci_driver, vmd_bus_pci_methods,
    sizeof(struct pci_softc), pci_driver);

DRIVER_MODULE(vmd_bus, vmd, vmd_bus_pci_driver, vmd_bus_devclass, NULL, NULL);
MODULE_VERSION(vmd_bus, 1);