2 * Copyright (c) 2011 Advanced Computing Technologies LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * Support APIs for Host to PCI bridge drivers and drivers that
33 * provide PCI domains.
36 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <dev/pci/pcireg.h>
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcib_private.h>
46 * Try to read the bus number of a host-PCI bridge using appropriate config
50 host_pcib_get_busno(pci_read_config_fn read_config, int bus, int slot, int func,
55 id = read_config(bus, slot, func, PCIR_DEVVENDOR, 4);
62 /* XXX This is a guess */
63 /* *busnum = read_config(bus, slot, func, 0x41, 1); */
67 /* Intel 82454KX/GX (Orion) */
68 *busnum = read_config(bus, slot, func, 0x4a, 1);
72 * For the 450nx chipset, there is a whole bundle of
73 * things pretending to be host bridges. The MIOC will
74 * be seen first and isn't really a pci bridge (the
75 * actual busses are attached to the PXB's). We need to
76 * read the registers of the MIOC to figure out the
77 * bus numbers for the PXB channels.
79 * Since the MIOC doesn't have a pci bus attached, we
80 * pretend it wasn't there.
86 /* Intel 82454NX PXB#0, Bus#A */
87 *busnum = read_config(bus, 0x10, func, 0xd0, 1);
90 /* Intel 82454NX PXB#0, Bus#B */
91 *busnum = read_config(bus, 0x10, func, 0xd1, 1) + 1;
94 /* Intel 82454NX PXB#1, Bus#A */
95 *busnum = read_config(bus, 0x10, func, 0xd3, 1);
98 /* Intel 82454NX PXB#1, Bus#B */
99 *busnum = read_config(bus, 0x10, func, 0xd4, 1) + 1;
104 /* ServerWorks -- vendor 0x1166 */
118 *busnum = read_config(bus, slot, func, 0x44, 1);
121 /* Compaq/HP -- vendor 0x0e11 */
123 *busnum = read_config(bus, slot, func, 0xc8, 1);
126 /* Don't know how to read bus number. */
135 * Return a pointer to a pretty name for a PCI device. If the device
136 * has a driver attached, the device's name is used, otherwise a name
137 * is generated from the device's PCI address.
140 pcib_child_name(device_t child)
144 if (device_get_nameunit(child) != NULL)
145 return (device_get_nameunit(child));
146 snprintf(buf, sizeof(buf), "pci%d:%d:%d:%d", pci_get_domain(child),
147 pci_get_bus(child), pci_get_slot(child), pci_get_function(child));
152 * Some Host-PCI bridge drivers know which resource ranges they can
153 * decode and should only allocate subranges to child PCI devices.
154 * This API provides a way to manage this. The bridge drive should
155 * initialize this structure during attach and call
156 * pcib_host_res_decodes() on each resource range it decodes. It can
157 * then use pcib_host_res_alloc() and pcib_host_res_adjust() as helper
158 * routines for BUS_ALLOC_RESOURCE() and BUS_ADJUST_RESOURCE(). This
159 * API assumes that resources for any decoded ranges can be safely
160 * allocated from the parent via bus_generic_alloc_resource().
163 pcib_host_res_init(device_t pcib, struct pcib_host_resources *hr)
167 resource_list_init(&hr->hr_rl);
172 pcib_host_res_free(device_t pcib, struct pcib_host_resources *hr)
175 resource_list_free(&hr->hr_rl);
180 pcib_host_res_decodes(struct pcib_host_resources *hr, int type, u_long start,
181 u_long end, u_int flags)
183 struct resource_list_entry *rle;
187 device_printf(hr->hr_pcib, "decoding %d %srange %#lx-%#lx\n",
188 type, flags & RF_PREFETCHABLE ? "prefetchable ": "", start,
190 rid = resource_list_add_next(&hr->hr_rl, type, start, end,
192 if (flags & RF_PREFETCHABLE) {
193 KASSERT(type == SYS_RES_MEMORY,
194 ("only memory is prefetchable"));
195 rle = resource_list_find(&hr->hr_rl, type, rid);
196 rle->flags = RLE_PREFETCH;
202 pcib_host_res_alloc(struct pcib_host_resources *hr, device_t dev, int type,
203 int *rid, u_long start, u_long end, u_long count, u_int flags)
205 struct resource_list_entry *rle;
207 u_long new_start, new_end;
209 if (flags & RF_PREFETCHABLE)
210 KASSERT(type == SYS_RES_MEMORY,
211 ("only memory is prefetchable"));
213 rle = resource_list_find(&hr->hr_rl, type, 0);
216 * No decoding ranges for this resource type, just pass
217 * the request up to the parent.
219 return (bus_generic_alloc_resource(hr->hr_pcib, dev, type, rid,
220 start, end, count, flags));
224 /* Try to allocate from each decoded range. */
225 for (; rle != NULL; rle = STAILQ_NEXT(rle, link)) {
226 if (rle->type != type)
228 if (((flags & RF_PREFETCHABLE) != 0) !=
229 ((rle->flags & RLE_PREFETCH) != 0))
231 new_start = ulmax(start, rle->start);
232 new_end = ulmin(end, rle->end);
233 if (new_start > new_end ||
234 new_start + count - 1 > new_end ||
235 new_start + count < new_start)
237 r = bus_generic_alloc_resource(hr->hr_pcib, dev, type, rid,
238 new_start, new_end, count, flags);
241 device_printf(hr->hr_pcib,
242 "allocated type %d (%#lx-%#lx) for rid %x of %s\n",
243 type, rman_get_start(r), rman_get_end(r),
244 *rid, pcib_child_name(dev));
250 * If we failed to find a prefetch range for a memory
251 * resource, try again without prefetch.
253 if (flags & RF_PREFETCHABLE) {
254 flags &= ~RF_PREFETCHABLE;
255 rle = resource_list_find(&hr->hr_rl, type, 0);
262 pcib_host_res_adjust(struct pcib_host_resources *hr, device_t dev, int type,
263 struct resource *r, u_long start, u_long end)
265 struct resource_list_entry *rle;
267 rle = resource_list_find(&hr->hr_rl, type, 0);
270 * No decoding ranges for this resource type, just pass
271 * the request up to the parent.
273 return (bus_generic_adjust_resource(hr->hr_pcib, dev, type, r,
277 /* Only allow adjustments that stay within a decoded range. */
278 for (; rle != NULL; rle = STAILQ_NEXT(rle, link)) {
279 if (rle->start <= start && rle->end >= end)
280 return (bus_generic_adjust_resource(hr->hr_pcib, dev,
281 type, r, start, end));
285 #endif /* NEW_PCIB */