pciconf.c revision 1.43 1 1.43 msaitoh /* $NetBSD: pciconf.c,v 1.43 2019/12/05 07:03:01 msaitoh Exp $ */
2 1.1 briggs
3 1.1 briggs /*
4 1.1 briggs * Copyright 2001 Wasabi Systems, Inc.
5 1.1 briggs * All rights reserved.
6 1.1 briggs *
7 1.1 briggs * Written by Allen Briggs for Wasabi Systems, Inc.
8 1.1 briggs *
9 1.1 briggs * Redistribution and use in source and binary forms, with or without
10 1.1 briggs * modification, are permitted provided that the following conditions
11 1.1 briggs * are met:
12 1.1 briggs * 1. Redistributions of source code must retain the above copyright
13 1.1 briggs * notice, this list of conditions and the following disclaimer.
14 1.1 briggs * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 briggs * notice, this list of conditions and the following disclaimer in the
16 1.1 briggs * documentation and/or other materials provided with the distribution.
17 1.1 briggs * 3. All advertising materials mentioning features or use of this software
18 1.1 briggs * must display the following acknowledgement:
19 1.1 briggs * This product includes software developed for the NetBSD Project by
20 1.1 briggs * Wasabi Systems, Inc.
21 1.1 briggs * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 briggs * or promote products derived from this software without specific prior
23 1.1 briggs * written permission.
24 1.1 briggs *
25 1.1 briggs * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 briggs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 briggs * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 briggs * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 briggs * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 briggs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 briggs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 briggs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 briggs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 briggs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 briggs * POSSIBILITY OF SUCH DAMAGE.
36 1.1 briggs */
37 1.1 briggs /*
38 1.1 briggs * Derived in part from code from PMON/2000 (http://pmon.groupbsd.org/).
39 1.1 briggs */
40 1.1 briggs
41 1.2 briggs /*
42 1.2 briggs * To do:
43 1.10 thorpej * - Perform all data structure allocation dynamically, don't have
44 1.10 thorpej * statically-sized arrays ("oops, you lose because you have too
45 1.10 thorpej * many slots filled!")
46 1.7 thorpej * - Do this in 2 passes, with an MD hook to control the behavior:
47 1.7 thorpej * (1) Configure the bus (possibly including expansion
48 1.7 thorpej * ROMs.
49 1.7 thorpej * (2) Another pass to disable expansion ROMs if they're
50 1.7 thorpej * mapped (since you're not supposed to leave them
51 1.7 thorpej * mapped when you're not using them).
52 1.7 thorpej * This would facilitate MD code executing the expansion ROMs
53 1.7 thorpej * if necessary (possibly with an x86 emulator) to configure
54 1.7 thorpej * devices (e.g. VGA cards).
55 1.2 briggs * - Deal with "anything can be hot-plugged" -- i.e., carry configuration
56 1.8 briggs * information around & be able to reconfigure on the fly
57 1.2 briggs * - Deal with segments (See IA64 System Abstraction Layer)
58 1.2 briggs * - Deal with subtractive bridges (& non-spec positive/subtractive decode)
59 1.2 briggs * - Deal with ISA/VGA/VGA palette snooping
60 1.2 briggs * - Deal with device capabilities on bridges
61 1.8 briggs * - Worry about changing a bridge to/from transparency
62 1.8 briggs * From thorpej (05/25/01)
63 1.8 briggs * - Try to handle devices that are already configured (perhaps using that
64 1.8 briggs * as a hint to where we put other devices)
65 1.2 briggs */
66 1.13 lukem
67 1.13 lukem #include <sys/cdefs.h>
68 1.43 msaitoh __KERNEL_RCSID(0, "$NetBSD: pciconf.c,v 1.43 2019/12/05 07:03:01 msaitoh Exp $");
69 1.2 briggs
70 1.1 briggs #include "opt_pci.h"
71 1.1 briggs
72 1.1 briggs #include <sys/param.h>
73 1.1 briggs #include <sys/extent.h>
74 1.1 briggs #include <sys/queue.h>
75 1.1 briggs #include <sys/systm.h>
76 1.1 briggs #include <sys/malloc.h>
77 1.32 matt #include <sys/kmem.h>
78 1.1 briggs
79 1.1 briggs #include <dev/pci/pcivar.h>
80 1.1 briggs #include <dev/pci/pciconf.h>
81 1.1 briggs #include <dev/pci/pcidevs.h>
82 1.22 briggs #include <dev/pci/pccbbreg.h>
83 1.1 briggs
84 1.1 briggs int pci_conf_debug = 0;
85 1.1 briggs
86 1.1 briggs #if !defined(MIN)
87 1.1 briggs #define MIN(a,b) (((a)<(b))?(a):(b))
88 1.1 briggs #define MAX(a,b) (((a)>(b))?(a):(b))
89 1.1 briggs #endif
90 1.1 briggs
91 1.1 briggs /* per-bus constants. */
92 1.10 thorpej #define MAX_CONF_DEV 32 /* Arbitrary */
93 1.1 briggs #define MAX_CONF_MEM (3 * MAX_CONF_DEV) /* Avg. 3 per device -- Arb. */
94 1.8 briggs #define MAX_CONF_IO (3 * MAX_CONF_DEV) /* Avg. 1 per device -- Arb. */
95 1.1 briggs
96 1.1 briggs struct _s_pciconf_bus_t; /* Forward declaration */
97 1.1 briggs
98 1.1 briggs typedef struct _s_pciconf_dev_t {
99 1.1 briggs int ipin;
100 1.1 briggs int iline;
101 1.1 briggs int min_gnt;
102 1.1 briggs int max_lat;
103 1.2 briggs int enable;
104 1.1 briggs pcitag_t tag;
105 1.1 briggs pci_chipset_tag_t pc;
106 1.1 briggs struct _s_pciconf_bus_t *ppb; /* I am really a bridge */
107 1.1 briggs } pciconf_dev_t;
108 1.1 briggs
109 1.1 briggs typedef struct _s_pciconf_win_t {
110 1.1 briggs pciconf_dev_t *dev;
111 1.1 briggs int reg; /* 0 for busses */
112 1.1 briggs int align;
113 1.1 briggs int prefetch;
114 1.39 msaitoh uint64_t size;
115 1.39 msaitoh uint64_t address;
116 1.1 briggs } pciconf_win_t;
117 1.1 briggs
118 1.1 briggs typedef struct _s_pciconf_bus_t {
119 1.1 briggs int busno;
120 1.1 briggs int next_busno;
121 1.1 briggs int last_busno;
122 1.1 briggs int max_mingnt;
123 1.1 briggs int min_maxlat;
124 1.14 thorpej int cacheline_size;
125 1.1 briggs int prefetch;
126 1.1 briggs int fast_b2b;
127 1.1 briggs int freq_66;
128 1.1 briggs int def_ltim;
129 1.1 briggs int max_ltim;
130 1.1 briggs int bandwidth_used;
131 1.1 briggs int swiz;
132 1.2 briggs int io_32bit;
133 1.2 briggs int pmem_64bit;
134 1.36 matt int io_align;
135 1.36 matt int mem_align;
136 1.36 matt int pmem_align;
137 1.1 briggs
138 1.1 briggs int ndevs;
139 1.1 briggs pciconf_dev_t device[MAX_CONF_DEV];
140 1.1 briggs
141 1.1 briggs /* These should be sorted in order of decreasing size */
142 1.1 briggs int nmemwin;
143 1.1 briggs pciconf_win_t pcimemwin[MAX_CONF_MEM];
144 1.1 briggs int niowin;
145 1.1 briggs pciconf_win_t pciiowin[MAX_CONF_IO];
146 1.1 briggs
147 1.1 briggs bus_size_t io_total;
148 1.1 briggs bus_size_t mem_total;
149 1.1 briggs bus_size_t pmem_total;
150 1.1 briggs
151 1.1 briggs struct extent *ioext;
152 1.1 briggs struct extent *memext;
153 1.1 briggs struct extent *pmemext;
154 1.1 briggs
155 1.1 briggs pci_chipset_tag_t pc;
156 1.1 briggs struct _s_pciconf_bus_t *parent_bus;
157 1.1 briggs } pciconf_bus_t;
158 1.1 briggs
159 1.1 briggs static int probe_bus(pciconf_bus_t *);
160 1.1 briggs static void alloc_busno(pciconf_bus_t *, pciconf_bus_t *);
161 1.18 simonb static void set_busreg(pci_chipset_tag_t, pcitag_t, int, int, int);
162 1.4 simonb static int pci_do_device_query(pciconf_bus_t *, pcitag_t, int, int, int);
163 1.1 briggs static int setup_iowins(pciconf_bus_t *);
164 1.1 briggs static int setup_memwins(pciconf_bus_t *);
165 1.1 briggs static int configure_bridge(pciconf_dev_t *);
166 1.1 briggs static int configure_bus(pciconf_bus_t *);
167 1.39 msaitoh static uint64_t pci_allocate_range(struct extent *, uint64_t, int);
168 1.1 briggs static pciconf_win_t *get_io_desc(pciconf_bus_t *, bus_size_t);
169 1.1 briggs static pciconf_win_t *get_mem_desc(pciconf_bus_t *, bus_size_t);
170 1.1 briggs static pciconf_bus_t *query_bus(pciconf_bus_t *, pciconf_dev_t *, int);
171 1.1 briggs
172 1.1 briggs static void print_tag(pci_chipset_tag_t, pcitag_t);
173 1.1 briggs
174 1.1 briggs static void
175 1.1 briggs print_tag(pci_chipset_tag_t pc, pcitag_t tag)
176 1.1 briggs {
177 1.1 briggs int bus, dev, func;
178 1.1 briggs
179 1.1 briggs pci_decompose_tag(pc, tag, &bus, &dev, &func);
180 1.1 briggs printf("PCI: bus %d, device %d, function %d: ", bus, dev, func);
181 1.1 briggs }
182 1.1 briggs
183 1.1 briggs /************************************************************************/
184 1.1 briggs /************************************************************************/
185 1.1 briggs /*********************** Bus probing routines ***********************/
186 1.1 briggs /************************************************************************/
187 1.1 briggs /************************************************************************/
188 1.1 briggs static pciconf_win_t *
189 1.1 briggs get_io_desc(pciconf_bus_t *pb, bus_size_t size)
190 1.1 briggs {
191 1.1 briggs int i, n;
192 1.1 briggs
193 1.1 briggs n = pb->niowin;
194 1.40 msaitoh for (i = n; i > 0 && size > pb->pciiowin[i-1].size; i--)
195 1.1 briggs pb->pciiowin[i] = pb->pciiowin[i-1]; /* struct copy */
196 1.1 briggs return &pb->pciiowin[i];
197 1.1 briggs }
198 1.1 briggs
199 1.1 briggs static pciconf_win_t *
200 1.1 briggs get_mem_desc(pciconf_bus_t *pb, bus_size_t size)
201 1.1 briggs {
202 1.1 briggs int i, n;
203 1.1 briggs
204 1.1 briggs n = pb->nmemwin;
205 1.40 msaitoh for (i = n; i > 0 && size > pb->pcimemwin[i-1].size; i--)
206 1.1 briggs pb->pcimemwin[i] = pb->pcimemwin[i-1]; /* struct copy */
207 1.1 briggs return &pb->pcimemwin[i];
208 1.1 briggs }
209 1.1 briggs
210 1.1 briggs /*
211 1.1 briggs * Set up bus common stuff, then loop over devices & functions.
212 1.1 briggs * If we find something, call pci_do_device_query()).
213 1.1 briggs */
214 1.1 briggs static int
215 1.1 briggs probe_bus(pciconf_bus_t *pb)
216 1.1 briggs {
217 1.33 dyoung int device;
218 1.33 dyoung uint8_t devs[32];
219 1.33 dyoung int i, n;
220 1.1 briggs
221 1.1 briggs pb->ndevs = 0;
222 1.1 briggs pb->niowin = 0;
223 1.1 briggs pb->nmemwin = 0;
224 1.1 briggs pb->freq_66 = 1;
225 1.21 augustss #ifdef PCICONF_NO_FAST_B2B
226 1.21 augustss pb->fast_b2b = 0;
227 1.21 augustss #else
228 1.1 briggs pb->fast_b2b = 1;
229 1.21 augustss #endif
230 1.1 briggs pb->prefetch = 1;
231 1.1 briggs pb->max_mingnt = 0; /* we are looking for the maximum */
232 1.1 briggs pb->min_maxlat = 0x100; /* we are looking for the minimum */
233 1.1 briggs pb->bandwidth_used = 0;
234 1.4 simonb
235 1.33 dyoung n = pci_bus_devorder(pb->pc, pb->busno, devs, __arraycount(devs));
236 1.33 dyoung for (i = 0; i < n; i++) {
237 1.1 briggs pcitag_t tag;
238 1.1 briggs pcireg_t id, bhlcr;
239 1.1 briggs int function, nfunction;
240 1.4 simonb int confmode;
241 1.1 briggs
242 1.33 dyoung device = devs[i];
243 1.33 dyoung
244 1.1 briggs tag = pci_make_tag(pb->pc, pb->busno, device, 0);
245 1.1 briggs if (pci_conf_debug) {
246 1.1 briggs print_tag(pb->pc, tag);
247 1.1 briggs }
248 1.1 briggs id = pci_conf_read(pb->pc, tag, PCI_ID_REG);
249 1.1 briggs
250 1.4 simonb if (pci_conf_debug) {
251 1.4 simonb printf("id=%x: Vendor=%x, Product=%x\n",
252 1.40 msaitoh id, PCI_VENDOR(id), PCI_PRODUCT(id));
253 1.4 simonb }
254 1.1 briggs /* Invalid vendor ID value? */
255 1.1 briggs if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
256 1.1 briggs continue;
257 1.1 briggs
258 1.1 briggs bhlcr = pci_conf_read(pb->pc, tag, PCI_BHLC_REG);
259 1.1 briggs nfunction = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
260 1.40 msaitoh for (function = 0; function < nfunction; function++) {
261 1.1 briggs tag = pci_make_tag(pb->pc, pb->busno, device, function);
262 1.1 briggs id = pci_conf_read(pb->pc, tag, PCI_ID_REG);
263 1.1 briggs if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
264 1.1 briggs continue;
265 1.40 msaitoh if (pb->ndevs + 1 < MAX_CONF_DEV) {
266 1.1 briggs if (pci_conf_debug) {
267 1.1 briggs print_tag(pb->pc, tag);
268 1.3 thorpej printf("Found dev 0x%04x 0x%04x -- "
269 1.3 thorpej "really probing.\n",
270 1.3 thorpej PCI_VENDOR(id), PCI_PRODUCT(id));
271 1.1 briggs }
272 1.4 simonb #ifdef __HAVE_PCI_CONF_HOOK
273 1.4 simonb confmode = pci_conf_hook(pb->pc, pb->busno,
274 1.4 simonb device, function, id);
275 1.4 simonb if (confmode == 0)
276 1.4 simonb continue;
277 1.4 simonb #else
278 1.6 thorpej /*
279 1.6 thorpej * Don't enable expansion ROMS -- some cards
280 1.6 thorpej * share address decoders between the EXPROM
281 1.6 thorpej * and PCI memory space, and enabling the ROM
282 1.6 thorpej * when not needed will cause all sorts of
283 1.6 thorpej * lossage.
284 1.6 thorpej */
285 1.28 gdamore confmode = PCI_CONF_DEFAULT;
286 1.4 simonb #endif
287 1.1 briggs if (pci_do_device_query(pb, tag, device,
288 1.4 simonb function, confmode))
289 1.1 briggs return -1;
290 1.1 briggs pb->ndevs++;
291 1.1 briggs }
292 1.1 briggs }
293 1.1 briggs }
294 1.1 briggs return 0;
295 1.1 briggs }
296 1.1 briggs
297 1.1 briggs static void
298 1.1 briggs alloc_busno(pciconf_bus_t *parent, pciconf_bus_t *pb)
299 1.1 briggs {
300 1.1 briggs pb->busno = parent->next_busno;
301 1.17 augustss pb->next_busno = pb->busno + 1;
302 1.17 augustss }
303 1.17 augustss
304 1.17 augustss static void
305 1.17 augustss set_busreg(pci_chipset_tag_t pc, pcitag_t tag, int prim, int sec, int sub)
306 1.17 augustss {
307 1.17 augustss pcireg_t busreg;
308 1.17 augustss
309 1.41 msaitoh busreg = __SHIFTIN(prim, PCI_BRIDGE_BUS_PRIMARY);
310 1.41 msaitoh busreg |= __SHIFTIN(sec, PCI_BRIDGE_BUS_SECONDARY);
311 1.41 msaitoh busreg |= __SHIFTIN(sub, PCI_BRIDGE_BUS_SUBORDINATE);
312 1.17 augustss pci_conf_write(pc, tag, PCI_BRIDGE_BUS_REG, busreg);
313 1.1 briggs }
314 1.1 briggs
315 1.1 briggs static pciconf_bus_t *
316 1.1 briggs query_bus(pciconf_bus_t *parent, pciconf_dev_t *pd, int dev)
317 1.1 briggs {
318 1.1 briggs pciconf_bus_t *pb;
319 1.17 augustss pcireg_t io, pmem;
320 1.1 briggs pciconf_win_t *pi, *pm;
321 1.1 briggs
322 1.42 chs pb = kmem_zalloc(sizeof (pciconf_bus_t), KM_SLEEP);
323 1.14 thorpej pb->cacheline_size = parent->cacheline_size;
324 1.1 briggs pb->parent_bus = parent;
325 1.1 briggs alloc_busno(parent, pb);
326 1.1 briggs
327 1.36 matt pb->mem_align = 0x100000; /* 1M alignment */
328 1.36 matt pb->pmem_align = 0x100000; /* 1M alignment */
329 1.36 matt pb->io_align = 0x1000; /* 4K alignment */
330 1.36 matt
331 1.17 augustss set_busreg(parent->pc, pd->tag, parent->busno, pb->busno, 0xff);
332 1.1 briggs
333 1.1 briggs pb->swiz = parent->swiz + dev;
334 1.1 briggs
335 1.1 briggs pb->ioext = NULL;
336 1.1 briggs pb->memext = NULL;
337 1.1 briggs pb->pmemext = NULL;
338 1.1 briggs pb->pc = parent->pc;
339 1.1 briggs pb->io_total = pb->mem_total = pb->pmem_total = 0;
340 1.1 briggs
341 1.2 briggs pb->io_32bit = 0;
342 1.2 briggs if (parent->io_32bit) {
343 1.11 thorpej io = pci_conf_read(parent->pc, pd->tag, PCI_BRIDGE_STATIO_REG);
344 1.40 msaitoh if (PCI_BRIDGE_IO_32BITS(io))
345 1.2 briggs pb->io_32bit = 1;
346 1.2 briggs }
347 1.2 briggs
348 1.2 briggs pb->pmem_64bit = 0;
349 1.2 briggs if (parent->pmem_64bit) {
350 1.11 thorpej pmem = pci_conf_read(parent->pc, pd->tag,
351 1.2 briggs PCI_BRIDGE_PREFETCHMEM_REG);
352 1.40 msaitoh if (PCI_BRIDGE_PREFETCHMEM_64BITS(pmem))
353 1.2 briggs pb->pmem_64bit = 1;
354 1.2 briggs }
355 1.2 briggs
356 1.1 briggs if (probe_bus(pb)) {
357 1.1 briggs printf("Failed to probe bus %d\n", pb->busno);
358 1.1 briggs goto err;
359 1.1 briggs }
360 1.1 briggs
361 1.17 augustss /* We have found all subordinate busses now, reprogram busreg. */
362 1.40 msaitoh pb->last_busno = pb->next_busno - 1;
363 1.17 augustss parent->next_busno = pb->next_busno;
364 1.17 augustss set_busreg(parent->pc, pd->tag, parent->busno, pb->busno,
365 1.17 augustss pb->last_busno);
366 1.17 augustss if (pci_conf_debug)
367 1.17 augustss printf("PCI bus bridge (parent %d) covers busses %d-%d\n",
368 1.17 augustss parent->busno, pb->busno, pb->last_busno);
369 1.17 augustss
370 1.1 briggs if (pb->io_total > 0) {
371 1.1 briggs if (parent->niowin >= MAX_CONF_IO) {
372 1.35 matt printf("pciconf: too many (%d) I/O windows\n",
373 1.35 matt parent->niowin);
374 1.1 briggs goto err;
375 1.1 briggs }
376 1.36 matt pb->io_total |= pb->io_align - 1; /* Round up */
377 1.1 briggs pi = get_io_desc(parent, pb->io_total);
378 1.1 briggs pi->dev = pd;
379 1.1 briggs pi->reg = 0;
380 1.1 briggs pi->size = pb->io_total;
381 1.36 matt pi->align = pb->io_align; /* 4K min alignment */
382 1.36 matt if (parent->io_align < pb->io_align)
383 1.36 matt parent->io_align = pb->io_align;
384 1.1 briggs pi->prefetch = 0;
385 1.1 briggs parent->niowin++;
386 1.1 briggs parent->io_total += pb->io_total;
387 1.1 briggs }
388 1.1 briggs
389 1.1 briggs if (pb->mem_total > 0) {
390 1.1 briggs if (parent->nmemwin >= MAX_CONF_MEM) {
391 1.35 matt printf("pciconf: too many (%d) MEM windows\n",
392 1.35 matt parent->nmemwin);
393 1.1 briggs goto err;
394 1.1 briggs }
395 1.40 msaitoh pb->mem_total |= pb->mem_align - 1; /* Round up */
396 1.1 briggs pm = get_mem_desc(parent, pb->mem_total);
397 1.1 briggs pm->dev = pd;
398 1.1 briggs pm->reg = 0;
399 1.1 briggs pm->size = pb->mem_total;
400 1.36 matt pm->align = pb->mem_align; /* 1M min alignment */
401 1.36 matt if (parent->mem_align < pb->mem_align)
402 1.36 matt parent->mem_align = pb->mem_align;
403 1.1 briggs pm->prefetch = 0;
404 1.1 briggs parent->nmemwin++;
405 1.1 briggs parent->mem_total += pb->mem_total;
406 1.1 briggs }
407 1.1 briggs
408 1.1 briggs if (pb->pmem_total > 0) {
409 1.1 briggs if (parent->nmemwin >= MAX_CONF_MEM) {
410 1.10 thorpej printf("pciconf: too many MEM windows\n");
411 1.1 briggs goto err;
412 1.1 briggs }
413 1.40 msaitoh pb->pmem_total |= pb->pmem_align - 1; /* Round up */
414 1.1 briggs pm = get_mem_desc(parent, pb->pmem_total);
415 1.1 briggs pm->dev = pd;
416 1.1 briggs pm->reg = 0;
417 1.1 briggs pm->size = pb->pmem_total;
418 1.36 matt pm->align = pb->pmem_align; /* 1M alignment */
419 1.36 matt if (parent->pmem_align < pb->pmem_align)
420 1.36 matt parent->pmem_align = pb->pmem_align;
421 1.1 briggs pm->prefetch = 1;
422 1.1 briggs parent->nmemwin++;
423 1.1 briggs parent->pmem_total += pb->pmem_total;
424 1.1 briggs }
425 1.1 briggs
426 1.1 briggs return pb;
427 1.1 briggs err:
428 1.32 matt kmem_free(pb, sizeof(*pb));
429 1.1 briggs return NULL;
430 1.1 briggs }
431 1.1 briggs
432 1.1 briggs static int
433 1.39 msaitoh pci_do_device_query(pciconf_bus_t *pb, pcitag_t tag, int dev, int func,
434 1.39 msaitoh int mode)
435 1.1 briggs {
436 1.1 briggs pciconf_dev_t *pd;
437 1.1 briggs pciconf_win_t *pi, *pm;
438 1.39 msaitoh pcireg_t classreg, cmd, icr, bhlc, bar, mask, bar64, mask64,
439 1.39 msaitoh busreg;
440 1.39 msaitoh uint64_t size;
441 1.22 briggs int br, width, reg_start, reg_end;
442 1.1 briggs
443 1.1 briggs pd = &pb->device[pb->ndevs];
444 1.1 briggs pd->pc = pb->pc;
445 1.1 briggs pd->tag = tag;
446 1.1 briggs pd->ppb = NULL;
447 1.4 simonb pd->enable = mode;
448 1.1 briggs
449 1.37 matt classreg = pci_conf_read(pb->pc, tag, PCI_CLASS_REG);
450 1.1 briggs
451 1.1 briggs cmd = pci_conf_read(pb->pc, tag, PCI_COMMAND_STATUS_REG);
452 1.32 matt bhlc = pci_conf_read(pb->pc, tag, PCI_BHLC_REG);
453 1.1 briggs
454 1.37 matt if (PCI_CLASS(classreg) != PCI_CLASS_BRIDGE
455 1.32 matt && PCI_HDRTYPE_TYPE(bhlc) != PCI_HDRTYPE_PPB) {
456 1.1 briggs cmd &= ~(PCI_COMMAND_MASTER_ENABLE |
457 1.1 briggs PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE);
458 1.1 briggs pci_conf_write(pb->pc, tag, PCI_COMMAND_STATUS_REG, cmd);
459 1.3 thorpej } else if (pci_conf_debug) {
460 1.3 thorpej print_tag(pb->pc, tag);
461 1.3 thorpej printf("device is a bridge; not clearing enables\n");
462 1.1 briggs }
463 1.1 briggs
464 1.1 briggs if ((cmd & PCI_STATUS_BACKTOBACK_SUPPORT) == 0)
465 1.1 briggs pb->fast_b2b = 0;
466 1.1 briggs
467 1.1 briggs if ((cmd & PCI_STATUS_66MHZ_SUPPORT) == 0)
468 1.1 briggs pb->freq_66 = 0;
469 1.1 briggs
470 1.22 briggs switch (PCI_HDRTYPE_TYPE(bhlc)) {
471 1.22 briggs case PCI_HDRTYPE_DEVICE:
472 1.22 briggs reg_start = PCI_MAPREG_START;
473 1.22 briggs reg_end = PCI_MAPREG_END;
474 1.22 briggs break;
475 1.22 briggs case PCI_HDRTYPE_PPB:
476 1.1 briggs pd->ppb = query_bus(pb, pd, dev);
477 1.1 briggs if (pd->ppb == NULL)
478 1.1 briggs return -1;
479 1.1 briggs return 0;
480 1.22 briggs case PCI_HDRTYPE_PCB:
481 1.22 briggs reg_start = PCI_MAPREG_START;
482 1.22 briggs reg_end = PCI_MAPREG_PCB_END;
483 1.22 briggs
484 1.22 briggs busreg = pci_conf_read(pb->pc, tag, PCI_BUSNUM);
485 1.22 briggs busreg = (busreg & 0xff000000) |
486 1.41 msaitoh __SHIFTIN(pb->busno, PCI_BRIDGE_BUS_PRIMARY) |
487 1.41 msaitoh __SHIFTIN(pb->next_busno, PCI_BRIDGE_BUS_SECONDARY) |
488 1.41 msaitoh __SHIFTIN(pb->next_busno, PCI_BRIDGE_BUS_SUBORDINATE);
489 1.22 briggs pci_conf_write(pb->pc, tag, PCI_BUSNUM, busreg);
490 1.22 briggs
491 1.24 simonb pb->next_busno++;
492 1.22 briggs break;
493 1.22 briggs default:
494 1.22 briggs return -1;
495 1.1 briggs }
496 1.1 briggs
497 1.1 briggs icr = pci_conf_read(pb->pc, tag, PCI_INTERRUPT_REG);
498 1.1 briggs pd->ipin = PCI_INTERRUPT_PIN(icr);
499 1.1 briggs pd->iline = PCI_INTERRUPT_LINE(icr);
500 1.1 briggs pd->min_gnt = PCI_MIN_GNT(icr);
501 1.1 briggs pd->max_lat = PCI_MAX_LAT(icr);
502 1.1 briggs if (pd->iline || pd->ipin) {
503 1.8 briggs pci_conf_interrupt(pb->pc, pb->busno, dev, pd->ipin, pb->swiz,
504 1.1 briggs &pd->iline);
505 1.1 briggs icr &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
506 1.1 briggs icr |= (pd->iline << PCI_INTERRUPT_LINE_SHIFT);
507 1.1 briggs pci_conf_write(pb->pc, tag, PCI_INTERRUPT_REG, icr);
508 1.1 briggs }
509 1.1 briggs
510 1.1 briggs if (pd->min_gnt != 0 || pd->max_lat != 0) {
511 1.1 briggs if (pd->min_gnt != 0 && pd->min_gnt > pb->max_mingnt)
512 1.1 briggs pb->max_mingnt = pd->min_gnt;
513 1.1 briggs
514 1.1 briggs if (pd->max_lat != 0 && pd->max_lat < pb->min_maxlat)
515 1.1 briggs pb->min_maxlat = pd->max_lat;
516 1.1 briggs
517 1.1 briggs pb->bandwidth_used += pd->min_gnt * 4000000 /
518 1.1 briggs (pd->min_gnt + pd->max_lat);
519 1.1 briggs }
520 1.1 briggs
521 1.1 briggs width = 4;
522 1.22 briggs for (br = reg_start; br < reg_end; br += width) {
523 1.3 thorpej #if 0
524 1.8 briggs /* XXX Should only ignore if IDE not in legacy mode? */
525 1.37 matt if (PCI_CLASS(classreg) == PCI_CLASS_MASS_STORAGE &&
526 1.37 matt PCI_SUBCLASS(classreg) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
527 1.1 briggs break;
528 1.1 briggs }
529 1.3 thorpej #endif
530 1.1 briggs bar = pci_conf_read(pb->pc, tag, br);
531 1.3 thorpej pci_conf_write(pb->pc, tag, br, 0xffffffff);
532 1.1 briggs mask = pci_conf_read(pb->pc, tag, br);
533 1.1 briggs pci_conf_write(pb->pc, tag, br, bar);
534 1.1 briggs width = 4;
535 1.1 briggs
536 1.8 briggs if ( (mode & PCI_CONF_MAP_IO)
537 1.8 briggs && (PCI_MAPREG_TYPE(mask) == PCI_MAPREG_TYPE_IO)) {
538 1.8 briggs /*
539 1.8 briggs * Upper 16 bits must be one. Devices may hardwire
540 1.8 briggs * them to zero, though, per PCI 2.2, 6.2.5.1, p 203.
541 1.8 briggs */
542 1.3 thorpej mask |= 0xffff0000;
543 1.3 thorpej
544 1.3 thorpej size = PCI_MAPREG_IO_SIZE(mask);
545 1.3 thorpej if (size == 0) {
546 1.3 thorpej if (pci_conf_debug) {
547 1.3 thorpej print_tag(pb->pc, tag);
548 1.3 thorpej printf("I/O BAR 0x%x is void\n", br);
549 1.3 thorpej }
550 1.3 thorpej continue;
551 1.3 thorpej }
552 1.1 briggs
553 1.1 briggs if (pb->niowin >= MAX_CONF_IO) {
554 1.10 thorpej printf("pciconf: too many I/O windows\n");
555 1.1 briggs return -1;
556 1.1 briggs }
557 1.1 briggs
558 1.1 briggs pi = get_io_desc(pb, size);
559 1.1 briggs pi->dev = pd;
560 1.1 briggs pi->reg = br;
561 1.43 msaitoh pi->size = (uint64_t)size;
562 1.1 briggs pi->align = 4;
563 1.36 matt if (pb->io_align < pi->size)
564 1.36 matt pb->io_align = pi->size;
565 1.1 briggs pi->prefetch = 0;
566 1.1 briggs if (pci_conf_debug) {
567 1.1 briggs print_tag(pb->pc, tag);
568 1.23 scw printf("Register 0x%x, I/O size %" PRIu64 "\n",
569 1.1 briggs br, pi->size);
570 1.1 briggs }
571 1.1 briggs pb->niowin++;
572 1.1 briggs pb->io_total += size;
573 1.4 simonb } else if ((mode & PCI_CONF_MAP_MEM)
574 1.4 simonb && (PCI_MAPREG_TYPE(mask) == PCI_MAPREG_TYPE_MEM)) {
575 1.1 briggs switch (PCI_MAPREG_MEM_TYPE(mask)) {
576 1.1 briggs case PCI_MAPREG_MEM_TYPE_32BIT:
577 1.1 briggs case PCI_MAPREG_MEM_TYPE_32BIT_1M:
578 1.43 msaitoh size = (uint64_t)PCI_MAPREG_MEM_SIZE(mask);
579 1.1 briggs break;
580 1.1 briggs case PCI_MAPREG_MEM_TYPE_64BIT:
581 1.1 briggs bar64 = pci_conf_read(pb->pc, tag, br + 4);
582 1.1 briggs pci_conf_write(pb->pc, tag, br + 4, 0xffffffff);
583 1.1 briggs mask64 = pci_conf_read(pb->pc, tag, br + 4);
584 1.1 briggs pci_conf_write(pb->pc, tag, br + 4, bar64);
585 1.43 msaitoh size = (uint64_t)PCI_MAPREG_MEM64_SIZE(
586 1.43 msaitoh (((uint64_t)mask64) << 32) | mask);
587 1.1 briggs width = 8;
588 1.16 briggs break;
589 1.1 briggs default:
590 1.1 briggs print_tag(pb->pc, tag);
591 1.1 briggs printf("reserved mapping type 0x%x\n",
592 1.1 briggs PCI_MAPREG_MEM_TYPE(mask));
593 1.1 briggs continue;
594 1.1 briggs }
595 1.1 briggs
596 1.3 thorpej if (size == 0) {
597 1.3 thorpej if (pci_conf_debug) {
598 1.3 thorpej print_tag(pb->pc, tag);
599 1.3 thorpej printf("MEM%d BAR 0x%x is void\n",
600 1.3 thorpej PCI_MAPREG_MEM_TYPE(mask) ==
601 1.3 thorpej PCI_MAPREG_MEM_TYPE_64BIT ?
602 1.3 thorpej 64 : 32, br);
603 1.3 thorpej }
604 1.3 thorpej continue;
605 1.16 briggs } else {
606 1.16 briggs if (pci_conf_debug) {
607 1.16 briggs print_tag(pb->pc, tag);
608 1.36 matt printf("MEM%d BAR 0x%x has size %#lx\n",
609 1.16 briggs PCI_MAPREG_MEM_TYPE(mask) ==
610 1.16 briggs PCI_MAPREG_MEM_TYPE_64BIT ?
611 1.43 msaitoh 64 : 32,
612 1.43 msaitoh br, (unsigned long)size);
613 1.16 briggs }
614 1.3 thorpej }
615 1.3 thorpej
616 1.1 briggs if (pb->nmemwin >= MAX_CONF_MEM) {
617 1.10 thorpej printf("pciconf: too many memory windows\n");
618 1.1 briggs return -1;
619 1.1 briggs }
620 1.1 briggs
621 1.1 briggs pm = get_mem_desc(pb, size);
622 1.1 briggs pm->dev = pd;
623 1.1 briggs pm->reg = br;
624 1.1 briggs pm->size = size;
625 1.1 briggs pm->align = 4;
626 1.1 briggs pm->prefetch = PCI_MAPREG_MEM_PREFETCHABLE(mask);
627 1.1 briggs if (pci_conf_debug) {
628 1.1 briggs print_tag(pb->pc, tag);
629 1.23 scw printf("Register 0x%x, memory size %"
630 1.23 scw PRIu64 "\n", br, pm->size);
631 1.1 briggs }
632 1.1 briggs pb->nmemwin++;
633 1.1 briggs if (pm->prefetch) {
634 1.1 briggs pb->pmem_total += size;
635 1.36 matt if (pb->pmem_align < pm->size)
636 1.36 matt pb->pmem_align = pm->size;
637 1.1 briggs } else {
638 1.1 briggs pb->mem_total += size;
639 1.36 matt if (pb->mem_align < pm->size)
640 1.36 matt pb->mem_align = pm->size;
641 1.1 briggs }
642 1.1 briggs }
643 1.1 briggs }
644 1.1 briggs
645 1.4 simonb if (mode & PCI_CONF_MAP_ROM) {
646 1.4 simonb bar = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
647 1.4 simonb pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM, 0xfffffffe);
648 1.4 simonb mask = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
649 1.4 simonb pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM, bar);
650 1.4 simonb
651 1.4 simonb if (mask != 0 && mask != 0xffffffff) {
652 1.4 simonb if (pb->nmemwin >= MAX_CONF_MEM) {
653 1.10 thorpej printf("pciconf: too many memory windows\n");
654 1.4 simonb return -1;
655 1.4 simonb }
656 1.43 msaitoh size = (uint64_t)PCI_MAPREG_MEM_SIZE(mask);
657 1.1 briggs
658 1.4 simonb pm = get_mem_desc(pb, size);
659 1.4 simonb pm->dev = pd;
660 1.4 simonb pm->reg = PCI_MAPREG_ROM;
661 1.4 simonb pm->size = size;
662 1.4 simonb pm->align = 4;
663 1.4 simonb pm->prefetch = 1;
664 1.4 simonb if (pci_conf_debug) {
665 1.4 simonb print_tag(pb->pc, tag);
666 1.23 scw printf("Expansion ROM memory size %"
667 1.23 scw PRIu64 "\n", pm->size);
668 1.4 simonb }
669 1.4 simonb pb->nmemwin++;
670 1.4 simonb pb->pmem_total += size;
671 1.1 briggs }
672 1.8 briggs } else {
673 1.28 gdamore /* Don't enable ROMs if we aren't going to map them. */
674 1.28 gdamore mode &= ~PCI_CONF_ENABLE_ROM;
675 1.28 gdamore pd->enable &= ~PCI_CONF_ENABLE_ROM;
676 1.28 gdamore }
677 1.28 gdamore
678 1.28 gdamore if (!(mode & PCI_CONF_ENABLE_ROM)) {
679 1.8 briggs /* Ensure ROM is disabled */
680 1.8 briggs bar = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
681 1.8 briggs pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM,
682 1.8 briggs bar & ~PCI_MAPREG_ROM_ENABLE);
683 1.1 briggs }
684 1.1 briggs
685 1.1 briggs return 0;
686 1.1 briggs }
687 1.1 briggs
688 1.1 briggs /************************************************************************/
689 1.1 briggs /************************************************************************/
690 1.1 briggs /******************** Bus configuration routines ********************/
691 1.1 briggs /************************************************************************/
692 1.1 briggs /************************************************************************/
693 1.39 msaitoh static uint64_t
694 1.39 msaitoh pci_allocate_range(struct extent *ex, uint64_t amt, int align)
695 1.1 briggs {
696 1.1 briggs int r;
697 1.1 briggs u_long addr;
698 1.1 briggs
699 1.1 briggs r = extent_alloc(ex, amt, align, 0, EX_NOWAIT, &addr);
700 1.1 briggs if (r) {
701 1.36 matt printf("extent_alloc(%p, %#" PRIx64 ", %#x) returned %d\n",
702 1.4 simonb ex, amt, align, r);
703 1.4 simonb extent_print(ex);
704 1.36 matt return ~0ULL;
705 1.1 briggs }
706 1.36 matt return addr;
707 1.1 briggs }
708 1.1 briggs
709 1.1 briggs static int
710 1.1 briggs setup_iowins(pciconf_bus_t *pb)
711 1.1 briggs {
712 1.1 briggs pciconf_win_t *pi;
713 1.1 briggs pciconf_dev_t *pd;
714 1.1 briggs
715 1.40 msaitoh for (pi = pb->pciiowin; pi < &pb->pciiowin[pb->niowin]; pi++) {
716 1.1 briggs if (pi->size == 0)
717 1.1 briggs continue;
718 1.1 briggs
719 1.1 briggs pd = pi->dev;
720 1.1 briggs pi->address = pci_allocate_range(pb->ioext, pi->size,
721 1.1 briggs pi->align);
722 1.36 matt if (~pi->address == 0) {
723 1.1 briggs print_tag(pd->pc, pd->tag);
724 1.23 scw printf("Failed to allocate PCI I/O space (%"
725 1.23 scw PRIu64 " req)\n", pi->size);
726 1.1 briggs return -1;
727 1.1 briggs }
728 1.1 briggs if (pd->ppb && pi->reg == 0) {
729 1.1 briggs pd->ppb->ioext = extent_create("pciconf", pi->address,
730 1.34 para pi->address + pi->size, NULL, 0,
731 1.1 briggs EX_NOWAIT);
732 1.1 briggs if (pd->ppb->ioext == NULL) {
733 1.1 briggs print_tag(pd->pc, pd->tag);
734 1.1 briggs printf("Failed to alloc I/O ext. for bus %d\n",
735 1.1 briggs pd->ppb->busno);
736 1.1 briggs return -1;
737 1.1 briggs }
738 1.1 briggs continue;
739 1.1 briggs }
740 1.26 tsutsui if (!pb->io_32bit && pi->address > 0xFFFF) {
741 1.26 tsutsui pi->address = 0;
742 1.26 tsutsui pd->enable &= ~PCI_CONF_ENABLE_IO;
743 1.26 tsutsui } else {
744 1.26 tsutsui pd->enable |= PCI_CONF_ENABLE_IO;
745 1.26 tsutsui }
746 1.1 briggs if (pci_conf_debug) {
747 1.1 briggs print_tag(pd->pc, pd->tag);
748 1.23 scw printf("Putting %" PRIu64 " I/O bytes @ %#" PRIx64
749 1.23 scw " (reg %x)\n", pi->size, pi->address, pi->reg);
750 1.1 briggs }
751 1.1 briggs pci_conf_write(pd->pc, pd->tag, pi->reg,
752 1.1 briggs PCI_MAPREG_IO_ADDR(pi->address) | PCI_MAPREG_TYPE_IO);
753 1.1 briggs }
754 1.1 briggs return 0;
755 1.1 briggs }
756 1.1 briggs
757 1.1 briggs static int
758 1.1 briggs setup_memwins(pciconf_bus_t *pb)
759 1.1 briggs {
760 1.1 briggs pciconf_win_t *pm;
761 1.1 briggs pciconf_dev_t *pd;
762 1.1 briggs pcireg_t base;
763 1.1 briggs struct extent *ex;
764 1.1 briggs
765 1.40 msaitoh for (pm = pb->pcimemwin; pm < &pb->pcimemwin[pb->nmemwin]; pm++) {
766 1.1 briggs if (pm->size == 0)
767 1.1 briggs continue;
768 1.1 briggs
769 1.1 briggs pd = pm->dev;
770 1.1 briggs ex = (pm->prefetch) ? pb->pmemext : pb->memext;
771 1.1 briggs pm->address = pci_allocate_range(ex, pm->size, pm->align);
772 1.36 matt if (~pm->address == 0) {
773 1.1 briggs print_tag(pd->pc, pd->tag);
774 1.1 briggs printf(
775 1.23 scw "Failed to allocate PCI memory space (%" PRIu64
776 1.23 scw " req)\n", pm->size);
777 1.1 briggs return -1;
778 1.1 briggs }
779 1.1 briggs if (pd->ppb && pm->reg == 0) {
780 1.1 briggs ex = extent_create("pciconf", pm->address,
781 1.34 para pm->address + pm->size, NULL, 0, EX_NOWAIT);
782 1.1 briggs if (ex == NULL) {
783 1.1 briggs print_tag(pd->pc, pd->tag);
784 1.1 briggs printf("Failed to alloc MEM ext. for bus %d\n",
785 1.1 briggs pd->ppb->busno);
786 1.1 briggs return -1;
787 1.1 briggs }
788 1.39 msaitoh if (pm->prefetch)
789 1.1 briggs pd->ppb->pmemext = ex;
790 1.39 msaitoh else
791 1.1 briggs pd->ppb->memext = ex;
792 1.39 msaitoh
793 1.1 briggs continue;
794 1.1 briggs }
795 1.2 briggs if (pm->prefetch && !pb->pmem_64bit &&
796 1.2 briggs pm->address > 0xFFFFFFFFULL) {
797 1.2 briggs pm->address = 0;
798 1.26 tsutsui pd->enable &= ~PCI_CONF_ENABLE_MEM;
799 1.39 msaitoh } else
800 1.8 briggs pd->enable |= PCI_CONF_ENABLE_MEM;
801 1.39 msaitoh
802 1.1 briggs if (pm->reg != PCI_MAPREG_ROM) {
803 1.1 briggs if (pci_conf_debug) {
804 1.1 briggs print_tag(pd->pc, pd->tag);
805 1.1 briggs printf(
806 1.23 scw "Putting %" PRIu64 " MEM bytes @ %#"
807 1.23 scw PRIx64 " (reg %x)\n", pm->size,
808 1.23 scw pm->address, pm->reg);
809 1.1 briggs }
810 1.1 briggs base = pci_conf_read(pd->pc, pd->tag, pm->reg);
811 1.1 briggs base = PCI_MAPREG_MEM_ADDR(pm->address) |
812 1.1 briggs PCI_MAPREG_MEM_TYPE(base);
813 1.1 briggs pci_conf_write(pd->pc, pd->tag, pm->reg, base);
814 1.1 briggs if (PCI_MAPREG_MEM_TYPE(base) ==
815 1.1 briggs PCI_MAPREG_MEM_TYPE_64BIT) {
816 1.1 briggs base = (pcireg_t)
817 1.1 briggs (PCI_MAPREG_MEM64_ADDR(pm->address) >> 32);
818 1.1 briggs pci_conf_write(pd->pc, pd->tag, pm->reg + 4,
819 1.1 briggs base);
820 1.1 briggs }
821 1.1 briggs }
822 1.1 briggs }
823 1.40 msaitoh for (pm = pb->pcimemwin; pm < &pb->pcimemwin[pb->nmemwin]; pm++) {
824 1.1 briggs if (pm->reg == PCI_MAPREG_ROM && pm->address != -1) {
825 1.1 briggs pd = pm->dev;
826 1.29 gdamore if (!(pd->enable & PCI_CONF_MAP_ROM))
827 1.28 gdamore continue;
828 1.1 briggs if (pci_conf_debug) {
829 1.1 briggs print_tag(pd->pc, pd->tag);
830 1.1 briggs printf(
831 1.23 scw "Putting %" PRIu64 " ROM bytes @ %#"
832 1.23 scw PRIx64 " (reg %x)\n", pm->size,
833 1.23 scw pm->address, pm->reg);
834 1.1 briggs }
835 1.29 gdamore base = (pcireg_t) pm->address;
836 1.29 gdamore if (pd->enable & PCI_CONF_ENABLE_ROM)
837 1.29 gdamore base |= PCI_MAPREG_ROM_ENABLE;
838 1.29 gdamore
839 1.1 briggs pci_conf_write(pd->pc, pd->tag, pm->reg, base);
840 1.1 briggs }
841 1.1 briggs }
842 1.1 briggs return 0;
843 1.1 briggs }
844 1.1 briggs
845 1.1 briggs /*
846 1.1 briggs * Configure I/O, memory, and prefetcable memory spaces, then make
847 1.1 briggs * a call to configure_bus().
848 1.1 briggs */
849 1.1 briggs static int
850 1.1 briggs configure_bridge(pciconf_dev_t *pd)
851 1.1 briggs {
852 1.1 briggs unsigned long io_base, io_limit, mem_base, mem_limit;
853 1.1 briggs pciconf_bus_t *pb;
854 1.1 briggs pcireg_t io, iohigh, mem, cmd;
855 1.1 briggs int rv;
856 1.38 msaitoh bool isprefetchmem64;
857 1.1 briggs
858 1.1 briggs pb = pd->ppb;
859 1.1 briggs /* Configure I/O base & limit*/
860 1.1 briggs if (pb->ioext) {
861 1.1 briggs io_base = pb->ioext->ex_start;
862 1.1 briggs io_limit = pb->ioext->ex_end;
863 1.2 briggs } else {
864 1.2 briggs io_base = 0x1000; /* 4K */
865 1.2 briggs io_limit = 0x0000;
866 1.1 briggs }
867 1.2 briggs if (pb->io_32bit) {
868 1.41 msaitoh iohigh = __SHIFTIN(io_base >> 16, PCI_BRIDGE_IOHIGH_BASE) |
869 1.41 msaitoh __SHIFTIN(io_limit >> 16, PCI_BRIDGE_IOHIGH_LIMIT);
870 1.2 briggs } else {
871 1.2 briggs if (io_limit > 0xFFFF) {
872 1.2 briggs printf("Bus %d bridge does not support 32-bit I/O. ",
873 1.2 briggs pb->busno);
874 1.2 briggs printf("Disabling I/O accesses\n");
875 1.2 briggs io_base = 0x1000; /* 4K */
876 1.2 briggs io_limit = 0x0000;
877 1.2 briggs }
878 1.2 briggs iohigh = 0;
879 1.2 briggs }
880 1.9 briggs io = pci_conf_read(pb->pc, pd->tag, PCI_BRIDGE_STATIO_REG) &
881 1.41 msaitoh PCI_BRIDGE_STATIO_STATUS;
882 1.41 msaitoh io |= __SHIFTIN((io_base >> 8) & PCI_BRIDGE_STATIO_IOADDR,
883 1.41 msaitoh PCI_BRIDGE_STATIO_IOBASE);
884 1.41 msaitoh io |= __SHIFTIN((io_limit >> 8) & PCI_BRIDGE_STATIO_IOADDR,
885 1.41 msaitoh PCI_BRIDGE_STATIO_IOLIMIT);
886 1.2 briggs pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_STATIO_REG, io);
887 1.2 briggs pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_IOHIGH_REG, iohigh);
888 1.1 briggs
889 1.1 briggs /* Configure mem base & limit */
890 1.1 briggs if (pb->memext) {
891 1.1 briggs mem_base = pb->memext->ex_start;
892 1.1 briggs mem_limit = pb->memext->ex_end;
893 1.2 briggs } else {
894 1.2 briggs mem_base = 0x100000; /* 1M */
895 1.2 briggs mem_limit = 0x000000;
896 1.1 briggs }
897 1.19 thorpej #if ULONG_MAX > 0xffffffff
898 1.2 briggs if (mem_limit > 0xFFFFFFFFULL) {
899 1.2 briggs printf("Bus %d bridge MEM range out of range. ", pb->busno);
900 1.2 briggs printf("Disabling MEM accesses\n");
901 1.2 briggs mem_base = 0x100000; /* 1M */
902 1.2 briggs mem_limit = 0x000000;
903 1.2 briggs }
904 1.19 thorpej #endif
905 1.41 msaitoh mem = __SHIFTIN((mem_base >> 16) & PCI_BRIDGE_MEMORY_ADDR,
906 1.41 msaitoh PCI_BRIDGE_MEMORY_BASE);
907 1.41 msaitoh mem |= __SHIFTIN((mem_limit >> 16) & PCI_BRIDGE_MEMORY_ADDR,
908 1.41 msaitoh PCI_BRIDGE_MEMORY_LIMIT);
909 1.2 briggs pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_MEMORY_REG, mem);
910 1.1 briggs
911 1.1 briggs /* Configure prefetchable mem base & limit */
912 1.1 briggs if (pb->pmemext) {
913 1.1 briggs mem_base = pb->pmemext->ex_start;
914 1.1 briggs mem_limit = pb->pmemext->ex_end;
915 1.2 briggs } else {
916 1.2 briggs mem_base = 0x100000; /* 1M */
917 1.2 briggs mem_limit = 0x000000;
918 1.1 briggs }
919 1.2 briggs mem = pci_conf_read(pb->pc, pd->tag, PCI_BRIDGE_PREFETCHMEM_REG);
920 1.38 msaitoh isprefetchmem64 = PCI_BRIDGE_PREFETCHMEM_64BITS(mem);
921 1.19 thorpej #if ULONG_MAX > 0xffffffff
922 1.38 msaitoh if (!isprefetchmem64 && mem_limit > 0xFFFFFFFFULL) {
923 1.2 briggs printf("Bus %d bridge does not support 64-bit PMEM. ",
924 1.2 briggs pb->busno);
925 1.2 briggs printf("Disabling prefetchable-MEM accesses\n");
926 1.2 briggs mem_base = 0x100000; /* 1M */
927 1.2 briggs mem_limit = 0x000000;
928 1.2 briggs }
929 1.19 thorpej #endif
930 1.41 msaitoh mem = __SHIFTIN((mem_base >> 16) & PCI_BRIDGE_PREFETCHMEM_ADDR,
931 1.41 msaitoh PCI_BRIDGE_PREFETCHMEM_BASE);
932 1.41 msaitoh mem |= __SHIFTIN((mem_limit >> 16) & PCI_BRIDGE_PREFETCHMEM_ADDR,
933 1.41 msaitoh PCI_BRIDGE_PREFETCHMEM_LIMIT);
934 1.2 briggs pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_PREFETCHMEM_REG, mem);
935 1.2 briggs /*
936 1.2 briggs * XXX -- 64-bit systems need a lot more than just this...
937 1.2 briggs */
938 1.38 msaitoh if (isprefetchmem64) {
939 1.39 msaitoh mem_base = (uint64_t)mem_base >> 32;
940 1.39 msaitoh mem_limit = (uint64_t)mem_limit >> 32;
941 1.41 msaitoh pci_conf_write(pb->pc, pd->tag,
942 1.41 msaitoh PCI_BRIDGE_PREFETCHBASEUP32_REG, mem_base & 0xffffffff);
943 1.41 msaitoh pci_conf_write(pb->pc, pd->tag,
944 1.41 msaitoh PCI_BRIDGE_PREFETCHLIMITUP32_REG, mem_limit & 0xffffffff);
945 1.32 matt }
946 1.1 briggs
947 1.1 briggs rv = configure_bus(pb);
948 1.1 briggs
949 1.1 briggs if (pb->ioext)
950 1.1 briggs extent_destroy(pb->ioext);
951 1.1 briggs if (pb->memext)
952 1.1 briggs extent_destroy(pb->memext);
953 1.1 briggs if (pb->pmemext)
954 1.1 briggs extent_destroy(pb->pmemext);
955 1.1 briggs if (rv == 0) {
956 1.1 briggs cmd = pci_conf_read(pd->pc, pd->tag, PCI_BRIDGE_CONTROL_REG);
957 1.41 msaitoh cmd &= ~PCI_BRIDGE_CONTROL; /* Clear control bit first */
958 1.41 msaitoh cmd |= PCI_BRIDGE_CONTROL_PERE | PCI_BRIDGE_CONTROL_SERR;
959 1.41 msaitoh if (pb->fast_b2b)
960 1.41 msaitoh cmd |= PCI_BRIDGE_CONTROL_SECFASTB2B;
961 1.41 msaitoh
962 1.1 briggs pci_conf_write(pd->pc, pd->tag, PCI_BRIDGE_CONTROL_REG, cmd);
963 1.1 briggs cmd = pci_conf_read(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG);
964 1.1 briggs cmd |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE;
965 1.1 briggs pci_conf_write(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG, cmd);
966 1.1 briggs }
967 1.1 briggs
968 1.1 briggs return rv;
969 1.1 briggs }
970 1.1 briggs
971 1.1 briggs /*
972 1.1 briggs * Calculate latency values, allocate I/O and MEM segments, then set them
973 1.1 briggs * up. If a PCI-PCI bridge is found, configure the bridge separately,
974 1.1 briggs * which will cause a recursive call back here.
975 1.1 briggs */
976 1.1 briggs static int
977 1.1 briggs configure_bus(pciconf_bus_t *pb)
978 1.1 briggs {
979 1.1 briggs pciconf_dev_t *pd;
980 1.8 briggs int def_ltim, max_ltim, band, bus_mhz;
981 1.1 briggs
982 1.20 simonb if (pb->ndevs == 0) {
983 1.20 simonb if (pci_conf_debug)
984 1.20 simonb printf("PCI bus %d - no devices\n", pb->busno);
985 1.39 msaitoh return 1;
986 1.20 simonb }
987 1.8 briggs bus_mhz = pb->freq_66 ? 66 : 33;
988 1.8 briggs max_ltim = pb->max_mingnt * bus_mhz / 4; /* cvt to cycle count */
989 1.30 briggs band = 4000000; /* 0.25us cycles/sec */
990 1.1 briggs if (band < pb->bandwidth_used) {
991 1.31 gavan printf("PCI bus %d: Warning: Total bandwidth exceeded!? (%d)\n",
992 1.31 gavan pb->busno, pb->bandwidth_used);
993 1.1 briggs def_ltim = -1;
994 1.1 briggs } else {
995 1.1 briggs def_ltim = (band - pb->bandwidth_used) / pb->ndevs;
996 1.1 briggs if (def_ltim > pb->min_maxlat)
997 1.1 briggs def_ltim = pb->min_maxlat;
998 1.8 briggs def_ltim = def_ltim * bus_mhz / 4;
999 1.1 briggs }
1000 1.1 briggs def_ltim = (def_ltim + 7) & ~7;
1001 1.1 briggs max_ltim = (max_ltim + 7) & ~7;
1002 1.1 briggs
1003 1.43 msaitoh pb->def_ltim = MIN(def_ltim, 255);
1004 1.43 msaitoh pb->max_ltim = MIN(MAX(max_ltim, def_ltim), 255);
1005 1.1 briggs
1006 1.1 briggs /*
1007 1.1 briggs * Now we have what we need to initialize the devices.
1008 1.1 briggs * It would probably be better if we could allocate all of these
1009 1.1 briggs * for all busses at once, but "not right now". First, get a list
1010 1.1 briggs * of free memory ranges from the m.d. system.
1011 1.1 briggs */
1012 1.1 briggs if (setup_iowins(pb) || setup_memwins(pb)) {
1013 1.36 matt printf("PCI bus configuration failed: "
1014 1.36 matt "unable to assign all I/O and memory ranges.\n");
1015 1.1 briggs return -1;
1016 1.1 briggs }
1017 1.1 briggs
1018 1.1 briggs /*
1019 1.1 briggs * Configure the latency for the devices, and enable them.
1020 1.1 briggs */
1021 1.40 msaitoh for (pd = pb->device; pd < &pb->device[pb->ndevs]; pd++) {
1022 1.37 matt pcireg_t cmd, classreg, misc;
1023 1.1 briggs int ltim;
1024 1.1 briggs
1025 1.1 briggs if (pci_conf_debug) {
1026 1.1 briggs print_tag(pd->pc, pd->tag);
1027 1.1 briggs printf("Configuring device.\n");
1028 1.1 briggs }
1029 1.37 matt classreg = pci_conf_read(pd->pc, pd->tag, PCI_CLASS_REG);
1030 1.1 briggs misc = pci_conf_read(pd->pc, pd->tag, PCI_BHLC_REG);
1031 1.1 briggs cmd = pci_conf_read(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG);
1032 1.26 tsutsui if (pd->enable & PCI_CONF_ENABLE_PARITY)
1033 1.26 tsutsui cmd |= PCI_COMMAND_PARITY_ENABLE;
1034 1.26 tsutsui if (pd->enable & PCI_CONF_ENABLE_SERR)
1035 1.26 tsutsui cmd |= PCI_COMMAND_SERR_ENABLE;
1036 1.1 briggs if (pb->fast_b2b)
1037 1.1 briggs cmd |= PCI_COMMAND_BACKTOBACK_ENABLE;
1038 1.37 matt if (PCI_CLASS(classreg) != PCI_CLASS_BRIDGE ||
1039 1.37 matt PCI_SUBCLASS(classreg) != PCI_SUBCLASS_BRIDGE_PCI) {
1040 1.8 briggs if (pd->enable & PCI_CONF_ENABLE_IO)
1041 1.8 briggs cmd |= PCI_COMMAND_IO_ENABLE;
1042 1.8 briggs if (pd->enable & PCI_CONF_ENABLE_MEM)
1043 1.8 briggs cmd |= PCI_COMMAND_MEM_ENABLE;
1044 1.8 briggs if (pd->enable & PCI_CONF_ENABLE_BM)
1045 1.8 briggs cmd |= PCI_COMMAND_MASTER_ENABLE;
1046 1.8 briggs ltim = pd->min_gnt * bus_mhz / 4;
1047 1.1 briggs ltim = MIN (MAX (pb->def_ltim, ltim), pb->max_ltim);
1048 1.1 briggs } else {
1049 1.8 briggs cmd |= PCI_COMMAND_MASTER_ENABLE;
1050 1.1 briggs ltim = MIN (pb->def_ltim, pb->max_ltim);
1051 1.1 briggs }
1052 1.26 tsutsui if ((pd->enable &
1053 1.43 msaitoh (PCI_CONF_ENABLE_MEM | PCI_CONF_ENABLE_IO)) == 0) {
1054 1.2 briggs print_tag(pd->pc, pd->tag);
1055 1.2 briggs printf("Disabled due to lack of resources.\n");
1056 1.2 briggs cmd &= ~(PCI_COMMAND_MASTER_ENABLE |
1057 1.2 briggs PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE);
1058 1.2 briggs }
1059 1.1 briggs pci_conf_write(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG, cmd);
1060 1.1 briggs
1061 1.14 thorpej misc &= ~((PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT) |
1062 1.14 thorpej (PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT));
1063 1.14 thorpej misc |= (ltim & PCI_LATTIMER_MASK) << PCI_LATTIMER_SHIFT;
1064 1.15 kleink misc |= ((pb->cacheline_size >> 2) & PCI_CACHELINE_MASK) <<
1065 1.14 thorpej PCI_CACHELINE_SHIFT;
1066 1.1 briggs pci_conf_write(pd->pc, pd->tag, PCI_BHLC_REG, misc);
1067 1.1 briggs
1068 1.1 briggs if (pd->ppb) {
1069 1.1 briggs if (configure_bridge(pd) < 0)
1070 1.1 briggs return -1;
1071 1.1 briggs continue;
1072 1.1 briggs }
1073 1.1 briggs }
1074 1.1 briggs
1075 1.39 msaitoh if (pci_conf_debug)
1076 1.1 briggs printf("PCI bus %d configured\n", pb->busno);
1077 1.1 briggs
1078 1.1 briggs return 0;
1079 1.1 briggs }
1080 1.1 briggs
1081 1.1 briggs /*
1082 1.1 briggs * Let's configure the PCI bus.
1083 1.1 briggs * This consists of basically scanning for all existing devices,
1084 1.1 briggs * identifying their needs, and then making another pass over them
1085 1.1 briggs * to set:
1086 1.1 briggs * 1. I/O addresses
1087 1.1 briggs * 2. Memory addresses (Prefetchable and not)
1088 1.1 briggs * 3. PCI command register
1089 1.1 briggs * 4. The latency part of the PCI BHLC (BIST (Built-In Self Test),
1090 1.1 briggs * Header type, Latency timer, Cache line size) register
1091 1.1 briggs *
1092 1.1 briggs * The command register is set to enable fast back-to-back transactions
1093 1.25 perry * if the host bridge says it can handle it. We also configure
1094 1.1 briggs * Master Enable, SERR enable, parity enable, and (if this is not a
1095 1.1 briggs * PCI-PCI bridge) the I/O and Memory spaces. Apparently some devices
1096 1.1 briggs * will not report some I/O space.
1097 1.1 briggs *
1098 1.1 briggs * The latency is computed to be a "fair share" of the bus bandwidth.
1099 1.1 briggs * The bus bandwidth variable is initialized to the number of PCI cycles
1100 1.1 briggs * in one second. The number of cycles taken for one transaction by each
1101 1.1 briggs * device (MAX_LAT + MIN_GNT) is then subtracted from the bandwidth.
1102 1.1 briggs * Care is taken to ensure that the latency timer won't be set such that
1103 1.1 briggs * it would exceed the critical time for any device.
1104 1.1 briggs *
1105 1.1 briggs * This is complicated somewhat due to the presence of bridges. PCI-PCI
1106 1.1 briggs * bridges are probed and configured recursively.
1107 1.1 briggs */
1108 1.1 briggs int
1109 1.1 briggs pci_configure_bus(pci_chipset_tag_t pc, struct extent *ioext,
1110 1.14 thorpej struct extent *memext, struct extent *pmemext, int firstbus,
1111 1.14 thorpej int cacheline_size)
1112 1.1 briggs {
1113 1.1 briggs pciconf_bus_t *pb;
1114 1.1 briggs int rv;
1115 1.1 briggs
1116 1.42 chs pb = kmem_zalloc(sizeof (pciconf_bus_t), KM_SLEEP);
1117 1.12 thorpej pb->busno = firstbus;
1118 1.1 briggs pb->next_busno = pb->busno + 1;
1119 1.1 briggs pb->last_busno = 255;
1120 1.14 thorpej pb->cacheline_size = cacheline_size;
1121 1.1 briggs pb->parent_bus = NULL;
1122 1.1 briggs pb->swiz = 0;
1123 1.2 briggs pb->io_32bit = 1;
1124 1.2 briggs pb->pmem_64bit = 0;
1125 1.1 briggs pb->ioext = ioext;
1126 1.1 briggs pb->memext = memext;
1127 1.39 msaitoh if (pmemext == NULL)
1128 1.1 briggs pb->pmemext = memext;
1129 1.39 msaitoh else
1130 1.1 briggs pb->pmemext = pmemext;
1131 1.39 msaitoh
1132 1.1 briggs pb->pc = pc;
1133 1.1 briggs pb->io_total = pb->mem_total = pb->pmem_total = 0;
1134 1.1 briggs
1135 1.1 briggs rv = probe_bus(pb);
1136 1.40 msaitoh pb->last_busno = pb->next_busno - 1;
1137 1.39 msaitoh if (rv == 0)
1138 1.1 briggs rv = configure_bus(pb);
1139 1.1 briggs
1140 1.1 briggs /*
1141 1.1 briggs * All done!
1142 1.1 briggs */
1143 1.32 matt kmem_free(pb, sizeof(*pb));
1144 1.1 briggs return rv;
1145 1.1 briggs }
1146