xref: /freebsd/sys/dev/pci/pci.c (revision 3ff369fed2a08f32dda232c10470b949bef9489f)
1 /*
2  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3  * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4  * Copyright (c) 2000, BSDi
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  *
30  */
31 
32 #include "opt_bus.h"
33 #include "opt_pci.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/linker.h>
40 #include <sys/fcntl.h>
41 #include <sys/conf.h>
42 #include <sys/kernel.h>
43 #include <sys/queue.h>
44 #include <sys/types.h>
45 
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48 #include <vm/vm_extern.h>
49 
50 #include <sys/bus.h>
51 #include <machine/bus.h>
52 #include <sys/rman.h>
53 #include <machine/resource.h>
54 
55 #include <sys/pciio.h>
56 #include <dev/pci/pcireg.h>
57 #include <dev/pci/pcivar.h>
58 #include <dev/pci/pci_private.h>
59 
60 #include "pcib_if.h"
61 #include "pci_if.h"
62 
63 static u_int32_t	pci_mapbase(unsigned mapreg);
64 static int		pci_maptype(unsigned mapreg);
65 static int		pci_mapsize(unsigned testval);
66 static int		pci_maprange(unsigned mapreg);
67 static void		pci_fixancient(pcicfgregs *cfg);
68 static void		pci_hdrtypedata(device_t pcib, int b, int s, int f,
69 					pcicfgregs *cfg);
70 static void		pci_read_extcap(device_t pcib, pcicfgregs *cfg);
71 
72 static int		pci_porten(device_t pcib, int b, int s, int f);
73 static int		pci_memen(device_t pcib, int b, int s, int f);
74 static int		pci_add_map(device_t pcib, int b, int s, int f, int reg,
75 				    struct resource_list *rl);
76 static void		pci_add_resources(device_t pcib, int b, int s, int f,
77 					  device_t dev);
78 static void		pci_add_children(device_t dev, int busno);
79 static int		pci_probe(device_t dev);
80 static int		pci_describe_parse_line(char **ptr, int *vendor,
81 						int *device, char **desc);
82 static char		*pci_describe_device(device_t dev);
83 static int		pci_modevent(module_t mod, int what, void *arg);
84 
85 static device_method_t pci_methods[] = {
86 	/* Device interface */
87 	DEVMETHOD(device_probe,		pci_probe),
88 	DEVMETHOD(device_attach,	bus_generic_attach),
89 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
90 	DEVMETHOD(device_suspend,	bus_generic_suspend),
91 	DEVMETHOD(device_resume,	bus_generic_resume),
92 
93 	/* Bus interface */
94 	DEVMETHOD(bus_print_child,	pci_print_child),
95 	DEVMETHOD(bus_probe_nomatch,	pci_probe_nomatch),
96 	DEVMETHOD(bus_read_ivar,	pci_read_ivar),
97 	DEVMETHOD(bus_write_ivar,	pci_write_ivar),
98 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
99 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
100 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
101 
102 	DEVMETHOD(bus_get_resource_list,pci_get_resource_list),
103 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
104 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
105 	DEVMETHOD(bus_delete_resource,	pci_delete_resource),
106 	DEVMETHOD(bus_alloc_resource,	pci_alloc_resource),
107 	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
108 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
109 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
110 
111 	/* PCI interface */
112 	DEVMETHOD(pci_read_config,	pci_read_config_method),
113 	DEVMETHOD(pci_write_config,	pci_write_config_method),
114 	DEVMETHOD(pci_enable_busmaster,	pci_enable_busmaster_method),
115 	DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method),
116 	DEVMETHOD(pci_enable_io,	pci_enable_io_method),
117 	DEVMETHOD(pci_disable_io,	pci_disable_io_method),
118 	DEVMETHOD(pci_get_powerstate,	pci_get_powerstate_method),
119 	DEVMETHOD(pci_set_powerstate,	pci_set_powerstate_method),
120 
121 	{ 0, 0 }
122 };
123 
124 static driver_t pci_driver = {
125 	"pci",
126 	pci_methods,
127 	0,			/* no softc */
128 };
129 
130 static devclass_t	pci_devclass;
131 DRIVER_MODULE(pci, pcib, pci_driver, pci_devclass, pci_modevent, 0);
132 DRIVER_MODULE(pci, acpi_pcib, pci_driver, pci_devclass, pci_modevent, 0);
133 MODULE_VERSION(pci, 1);
134 
135 static char	*pci_vendordata;
136 static size_t	pci_vendordata_size;
137 
138 
139 struct pci_quirk {
140 	u_int32_t devid;	/* Vendor/device of the card */
141 	int	type;
142 #define PCI_QUIRK_MAP_REG	1 /* PCI map register in weird place */
143 	int	arg1;
144 	int	arg2;
145 };
146 
147 struct pci_quirk pci_quirks[] = {
148 	/* The Intel 82371AB and 82443MX has a map register at offset 0x90. */
149 	{ 0x71138086, PCI_QUIRK_MAP_REG,	0x90,	 0 },
150 	{ 0x719b8086, PCI_QUIRK_MAP_REG,	0x90,	 0 },
151 	/* As does the Serverworks OSB4 (the SMBus mapping register) */
152 	{ 0x02001166, PCI_QUIRK_MAP_REG,	0x90,	 0 },
153 
154 	{ 0 }
155 };
156 
157 /* map register information */
158 #define PCI_MAPMEM	0x01	/* memory map */
159 #define PCI_MAPMEMP	0x02	/* prefetchable memory map */
160 #define PCI_MAPPORT	0x04	/* port map */
161 
162 struct devlist pci_devq;
163 u_int32_t pci_generation;
164 u_int32_t pci_numdevs = 0;
165 
166 /* Find a device_t by bus/slot/function */
167 
168 device_t
169 pci_find_bsf (u_int8_t bus, u_int8_t slot, u_int8_t func)
170 {
171 	struct pci_devinfo *dinfo;
172 
173 	STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
174 		if ((dinfo->cfg.bus == bus) &&
175 		    (dinfo->cfg.slot == slot) &&
176 		    (dinfo->cfg.func == func)) {
177 			return (dinfo->cfg.dev);
178 		}
179 	}
180 
181 	return (NULL);
182 }
183 
184 /* Find a device_t by vendor/device ID */
185 
186 device_t
187 pci_find_device (u_int16_t vendor, u_int16_t device)
188 {
189 	struct pci_devinfo *dinfo;
190 
191 	STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
192 		if ((dinfo->cfg.vendor == vendor) &&
193 		    (dinfo->cfg.device == device)) {
194 			return (dinfo->cfg.dev);
195 		}
196 	}
197 
198 	return (NULL);
199 }
200 
201 /* return base address of memory or port map */
202 
203 static u_int32_t
204 pci_mapbase(unsigned mapreg)
205 {
206 	int mask = 0x03;
207 	if ((mapreg & 0x01) == 0)
208 		mask = 0x0f;
209 	return (mapreg & ~mask);
210 }
211 
212 /* return map type of memory or port map */
213 
214 static int
215 pci_maptype(unsigned mapreg)
216 {
217 	static u_int8_t maptype[0x10] = {
218 		PCI_MAPMEM,		PCI_MAPPORT,
219 		PCI_MAPMEM,		0,
220 		PCI_MAPMEM,		PCI_MAPPORT,
221 		0,			0,
222 		PCI_MAPMEM|PCI_MAPMEMP,	PCI_MAPPORT,
223 		PCI_MAPMEM|PCI_MAPMEMP, 0,
224 		PCI_MAPMEM|PCI_MAPMEMP,	PCI_MAPPORT,
225 		0,			0,
226 	};
227 
228 	return maptype[mapreg & 0x0f];
229 }
230 
231 /* return log2 of map size decoded for memory or port map */
232 
233 static int
234 pci_mapsize(unsigned testval)
235 {
236 	int ln2size;
237 
238 	testval = pci_mapbase(testval);
239 	ln2size = 0;
240 	if (testval != 0) {
241 		while ((testval & 1) == 0)
242 		{
243 			ln2size++;
244 			testval >>= 1;
245 		}
246 	}
247 	return (ln2size);
248 }
249 
250 /* return log2 of address range supported by map register */
251 
252 static int
253 pci_maprange(unsigned mapreg)
254 {
255 	int ln2range = 0;
256 	switch (mapreg & 0x07) {
257 	case 0x00:
258 	case 0x01:
259 	case 0x05:
260 		ln2range = 32;
261 		break;
262 	case 0x02:
263 		ln2range = 20;
264 		break;
265 	case 0x04:
266 		ln2range = 64;
267 		break;
268 	}
269 	return (ln2range);
270 }
271 
272 /* adjust some values from PCI 1.0 devices to match 2.0 standards ... */
273 
274 static void
275 pci_fixancient(pcicfgregs *cfg)
276 {
277 	if (cfg->hdrtype != 0)
278 		return;
279 
280 	/* PCI to PCI bridges use header type 1 */
281 	if (cfg->baseclass == PCIC_BRIDGE && cfg->subclass == PCIS_BRIDGE_PCI)
282 		cfg->hdrtype = 1;
283 }
284 
285 /* extract header type specific config data */
286 
287 static void
288 pci_hdrtypedata(device_t pcib, int b, int s, int f, pcicfgregs *cfg)
289 {
290 #define REG(n, w)	PCIB_READ_CONFIG(pcib, b, s, f, n, w)
291 	switch (cfg->hdrtype) {
292 	case 0:
293 		cfg->subvendor      = REG(PCIR_SUBVEND_0, 2);
294 		cfg->subdevice      = REG(PCIR_SUBDEV_0, 2);
295 		cfg->nummaps	    = PCI_MAXMAPS_0;
296 		break;
297 	case 1:
298 		cfg->subvendor      = REG(PCIR_SUBVEND_1, 2);
299 		cfg->subdevice      = REG(PCIR_SUBDEV_1, 2);
300 		cfg->nummaps	    = PCI_MAXMAPS_1;
301 		break;
302 	case 2:
303 		cfg->subvendor      = REG(PCIR_SUBVEND_2, 2);
304 		cfg->subdevice      = REG(PCIR_SUBDEV_2, 2);
305 		cfg->nummaps	    = PCI_MAXMAPS_2;
306 		break;
307 	}
308 #undef REG
309 }
310 
311 /* read configuration header into pcicfgregs structure */
312 
313 struct pci_devinfo *
314 pci_read_device(device_t pcib, int b, int s, int f, size_t size)
315 {
316 #define REG(n, w)	PCIB_READ_CONFIG(pcib, b, s, f, n, w)
317 	pcicfgregs *cfg = NULL;
318 	struct pci_devinfo *devlist_entry;
319 	struct devlist *devlist_head;
320 
321 	devlist_head = &pci_devq;
322 
323 	devlist_entry = NULL;
324 
325 	if (PCIB_READ_CONFIG(pcib, b, s, f, PCIR_DEVVENDOR, 4) != -1) {
326 		devlist_entry = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
327 		if (devlist_entry == NULL)
328 			return (NULL);
329 
330 		cfg = &devlist_entry->cfg;
331 
332 		cfg->bus		= b;
333 		cfg->slot		= s;
334 		cfg->func		= f;
335 		cfg->vendor		= REG(PCIR_VENDOR, 2);
336 		cfg->device		= REG(PCIR_DEVICE, 2);
337 		cfg->cmdreg		= REG(PCIR_COMMAND, 2);
338 		cfg->statreg		= REG(PCIR_STATUS, 2);
339 		cfg->baseclass		= REG(PCIR_CLASS, 1);
340 		cfg->subclass		= REG(PCIR_SUBCLASS, 1);
341 		cfg->progif		= REG(PCIR_PROGIF, 1);
342 		cfg->revid		= REG(PCIR_REVID, 1);
343 		cfg->hdrtype		= REG(PCIR_HEADERTYPE, 1);
344 		cfg->cachelnsz		= REG(PCIR_CACHELNSZ, 1);
345 		cfg->lattimer		= REG(PCIR_LATTIMER, 1);
346 		cfg->intpin		= REG(PCIR_INTPIN, 1);
347 		cfg->intline		= REG(PCIR_INTLINE, 1);
348 
349 		cfg->mingnt		= REG(PCIR_MINGNT, 1);
350 		cfg->maxlat		= REG(PCIR_MAXLAT, 1);
351 
352 		cfg->mfdev		= (cfg->hdrtype & PCIM_MFDEV) != 0;
353 		cfg->hdrtype		&= ~PCIM_MFDEV;
354 
355 		pci_fixancient(cfg);
356 		pci_hdrtypedata(pcib, b, s, f, cfg);
357 
358 		if (REG(PCIR_STATUS, 2) & PCIM_STATUS_CAPPRESENT)
359 			pci_read_extcap(pcib, cfg);
360 
361 		STAILQ_INSERT_TAIL(devlist_head, devlist_entry, pci_links);
362 
363 		devlist_entry->conf.pc_sel.pc_bus = cfg->bus;
364 		devlist_entry->conf.pc_sel.pc_dev = cfg->slot;
365 		devlist_entry->conf.pc_sel.pc_func = cfg->func;
366 		devlist_entry->conf.pc_hdr = cfg->hdrtype;
367 
368 		devlist_entry->conf.pc_subvendor = cfg->subvendor;
369 		devlist_entry->conf.pc_subdevice = cfg->subdevice;
370 		devlist_entry->conf.pc_vendor = cfg->vendor;
371 		devlist_entry->conf.pc_device = cfg->device;
372 
373 		devlist_entry->conf.pc_class = cfg->baseclass;
374 		devlist_entry->conf.pc_subclass = cfg->subclass;
375 		devlist_entry->conf.pc_progif = cfg->progif;
376 		devlist_entry->conf.pc_revid = cfg->revid;
377 
378 		pci_numdevs++;
379 		pci_generation++;
380 	}
381 	return (devlist_entry);
382 #undef REG
383 }
384 
385 static void
386 pci_read_extcap(device_t pcib, pcicfgregs *cfg)
387 {
388 #define REG(n, w)	PCIB_READ_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, w)
389 	int	ptr, nextptr, ptrptr;
390 
391 	switch (cfg->hdrtype) {
392 	case 0:
393 		ptrptr = 0x34;
394 		break;
395 	case 2:
396 		ptrptr = 0x14;
397 		break;
398 	default:
399 		return;		/* no extended capabilities support */
400 	}
401 	nextptr = REG(ptrptr, 1);	/* sanity check? */
402 
403 	/*
404 	 * Read capability entries.
405 	 */
406 	while (nextptr != 0) {
407 		/* Sanity check */
408 		if (nextptr > 255) {
409 			printf("illegal PCI extended capability offset %d\n",
410 			    nextptr);
411 			return;
412 		}
413 		/* Find the next entry */
414 		ptr = nextptr;
415 		nextptr = REG(ptr + 1, 1);
416 
417 		/* Process this entry */
418 		switch (REG(ptr, 1)) {
419 		case 0x01:		/* PCI power management */
420 			if (cfg->pp_cap == 0) {
421 				cfg->pp_cap = REG(ptr + PCIR_POWER_CAP, 2);
422 				cfg->pp_status = ptr + PCIR_POWER_STATUS;
423 				cfg->pp_pmcsr = ptr + PCIR_POWER_PMCSR;
424 				if ((nextptr - ptr) > PCIR_POWER_DATA)
425 					cfg->pp_data = ptr + PCIR_POWER_DATA;
426 			}
427 			break;
428 		default:
429 			break;
430 		}
431 	}
432 #undef REG
433 }
434 
435 /* free pcicfgregs structure and all depending data structures */
436 
437 int
438 pci_freecfg(struct pci_devinfo *dinfo)
439 {
440 	struct devlist *devlist_head;
441 
442 	devlist_head = &pci_devq;
443 
444 	/* XXX this hasn't been tested */
445 	STAILQ_REMOVE(devlist_head, dinfo, pci_devinfo, pci_links);
446 	free(dinfo, M_DEVBUF);
447 
448 	/* increment the generation count */
449 	pci_generation++;
450 
451 	/* we're losing one device */
452 	pci_numdevs--;
453 	return (0);
454 }
455 
456 /*
457  * PCI power manangement
458  */
459 int
460 pci_set_powerstate_method(device_t dev, device_t child, int state)
461 {
462 	struct pci_devinfo *dinfo = device_get_ivars(child);
463 	pcicfgregs *cfg = &dinfo->cfg;
464 	u_int16_t status;
465 	int result;
466 
467 	if (cfg->pp_cap != 0) {
468 		status = PCI_READ_CONFIG(dev, child, cfg->pp_status, 2) & ~PCIM_PSTAT_DMASK;
469 		result = 0;
470 		switch (state) {
471 		case PCI_POWERSTATE_D0:
472 			status |= PCIM_PSTAT_D0;
473 			break;
474 		case PCI_POWERSTATE_D1:
475 			if (cfg->pp_cap & PCIM_PCAP_D1SUPP) {
476 				status |= PCIM_PSTAT_D1;
477 			} else {
478 				result = EOPNOTSUPP;
479 			}
480 			break;
481 		case PCI_POWERSTATE_D2:
482 			if (cfg->pp_cap & PCIM_PCAP_D2SUPP) {
483 				status |= PCIM_PSTAT_D2;
484 			} else {
485 				result = EOPNOTSUPP;
486 			}
487 			break;
488 		case PCI_POWERSTATE_D3:
489 			status |= PCIM_PSTAT_D3;
490 			break;
491 		default:
492 			result = EINVAL;
493 		}
494 		if (result == 0)
495 			PCI_WRITE_CONFIG(dev, child, cfg->pp_status, status, 2);
496 	} else {
497 		result = ENXIO;
498 	}
499 	return(result);
500 }
501 
502 int
503 pci_get_powerstate_method(device_t dev, device_t child)
504 {
505 	struct pci_devinfo *dinfo = device_get_ivars(child);
506 	pcicfgregs *cfg = &dinfo->cfg;
507 	u_int16_t status;
508 	int result;
509 
510 	if (cfg->pp_cap != 0) {
511 		status = PCI_READ_CONFIG(dev, child, cfg->pp_status, 2);
512 		switch (status & PCIM_PSTAT_DMASK) {
513 		case PCIM_PSTAT_D0:
514 			result = PCI_POWERSTATE_D0;
515 			break;
516 		case PCIM_PSTAT_D1:
517 			result = PCI_POWERSTATE_D1;
518 			break;
519 		case PCIM_PSTAT_D2:
520 			result = PCI_POWERSTATE_D2;
521 			break;
522 		case PCIM_PSTAT_D3:
523 			result = PCI_POWERSTATE_D3;
524 			break;
525 		default:
526 			result = PCI_POWERSTATE_UNKNOWN;
527 			break;
528 		}
529 	} else {
530 		/* No support, device is always at D0 */
531 		result = PCI_POWERSTATE_D0;
532 	}
533 	return(result);
534 }
535 
536 /*
537  * Some convenience functions for PCI device drivers.
538  */
539 
540 static __inline void
541 pci_set_command_bit(device_t dev, device_t child, u_int16_t bit)
542 {
543 	u_int16_t	command;
544 
545 	command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
546 	command |= bit;
547 	PCI_WRITE_CONFIG(dev, child, PCIR_COMMAND, command, 2);
548 }
549 
550 static __inline void
551 pci_clear_command_bit(device_t dev, device_t child, u_int16_t bit)
552 {
553 	u_int16_t	command;
554 
555 	command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
556 	command &= ~bit;
557 	PCI_WRITE_CONFIG(dev, child, PCIR_COMMAND, command, 2);
558 }
559 
560 void
561 pci_enable_busmaster_method(device_t dev, device_t child)
562 {
563 	pci_set_command_bit(dev, child, PCIM_CMD_BUSMASTEREN);
564 }
565 
566 void
567 pci_disable_busmaster_method(device_t dev, device_t child)
568 {
569 	pci_clear_command_bit(dev, child, PCIM_CMD_BUSMASTEREN);
570 }
571 
572 void
573 pci_enable_io_method(device_t dev, device_t child, int space)
574 {
575 	switch(space) {
576 	case SYS_RES_IOPORT:
577 		pci_set_command_bit(dev, child, PCIM_CMD_PORTEN);
578 		break;
579 	case SYS_RES_MEMORY:
580 		pci_set_command_bit(dev, child, PCIM_CMD_MEMEN);
581 		break;
582 	}
583 }
584 
585 void
586 pci_disable_io_method(device_t dev, device_t child, int space)
587 {
588 	switch(space) {
589 	case SYS_RES_IOPORT:
590 		pci_clear_command_bit(dev, child, PCIM_CMD_PORTEN);
591 		break;
592 	case SYS_RES_MEMORY:
593 		pci_clear_command_bit(dev, child, PCIM_CMD_MEMEN);
594 		break;
595 	}
596 }
597 
598 /*
599  * New style pci driver.  Parent device is either a pci-host-bridge or a
600  * pci-pci-bridge.  Both kinds are represented by instances of pcib.
601  */
602 
603 void
604 pci_print_verbose(struct pci_devinfo *dinfo)
605 {
606 	if (bootverbose) {
607 		pcicfgregs *cfg = &dinfo->cfg;
608 
609 		printf("found->\tvendor=0x%04x, dev=0x%04x, revid=0x%02x\n",
610 		    cfg->vendor, cfg->device, cfg->revid);
611 		printf("\tbus=%d, slot=%d, func=%d\n",
612 		    cfg->bus, cfg->slot, cfg->func);
613 		printf("\tclass=%02x-%02x-%02x, hdrtype=0x%02x, mfdev=%d\n",
614 		    cfg->baseclass, cfg->subclass, cfg->progif, cfg->hdrtype,
615 		    cfg->mfdev);
616 #ifdef PCI_DEBUG
617 		printf("\tcmdreg=0x%04x, statreg=0x%04x, cachelnsz=%d (dwords)\n",
618 		    cfg->cmdreg, cfg->statreg, cfg->cachelnsz);
619 		printf("\tlattimer=0x%02x (%d ns), mingnt=0x%02x (%d ns), maxlat=0x%02x (%d ns)\n",
620 		    cfg->lattimer, cfg->lattimer * 30, cfg->mingnt,
621 		    cfg->mingnt * 250, cfg->maxlat, cfg->maxlat * 250);
622 #endif /* PCI_DEBUG */
623 		if (cfg->intpin > 0)
624 			printf("\tintpin=%c, irq=%d\n",
625 			    cfg->intpin +'a' -1, cfg->intline);
626 		if (cfg->pp_cap) {
627 			u_int16_t status;
628 
629 			status = pci_read_config(cfg->dev, cfg->pp_status, 2);
630 			printf("\tpowerspec %d  supports D0%s%s D3  current D%d\n",
631 			    cfg->pp_cap & PCIM_PCAP_SPEC,
632 			    cfg->pp_cap & PCIM_PCAP_D1SUPP ? " D1" : "",
633 			    cfg->pp_cap & PCIM_PCAP_D2SUPP ? " D2" : "",
634 			    status & PCIM_PSTAT_DMASK);
635 		}
636 	}
637 }
638 
639 static int
640 pci_porten(device_t pcib, int b, int s, int f)
641 {
642 	return (PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2)
643 		& PCIM_CMD_PORTEN) != 0;
644 }
645 
646 static int
647 pci_memen(device_t pcib, int b, int s, int f)
648 {
649 	return (PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2)
650 		& PCIM_CMD_MEMEN) != 0;
651 }
652 
653 /*
654  * Add a resource based on a pci map register. Return 1 if the map
655  * register is a 32bit map register or 2 if it is a 64bit register.
656  */
657 static int
658 pci_add_map(device_t pcib, int b, int s, int f, int reg,
659 	    struct resource_list *rl)
660 {
661 	u_int32_t map;
662 	u_int64_t base;
663 	u_int8_t ln2size;
664 	u_int8_t ln2range;
665 	u_int32_t testval;
666 #ifdef PCI_ENABLE_IO_MODES
667 	u_int16_t cmd;
668 #endif
669 	int type;
670 
671 	map = PCIB_READ_CONFIG(pcib, b, s, f, reg, 4);
672 
673 	if (map == 0 || map == 0xffffffff)
674 		return (1); /* skip invalid entry */
675 
676 	PCIB_WRITE_CONFIG(pcib, b, s, f, reg, 0xffffffff, 4);
677 	testval = PCIB_READ_CONFIG(pcib, b, s, f, reg, 4);
678 	PCIB_WRITE_CONFIG(pcib, b, s, f, reg, map, 4);
679 
680 	base = pci_mapbase(map);
681 	if (pci_maptype(map) & PCI_MAPMEM)
682 		type = SYS_RES_MEMORY;
683 	else
684 		type = SYS_RES_IOPORT;
685 	ln2size = pci_mapsize(testval);
686 	ln2range = pci_maprange(testval);
687 	if (ln2range == 64) {
688 		/* Read the other half of a 64bit map register */
689 		base |= (u_int64_t) PCIB_READ_CONFIG(pcib, b, s, f, reg + 4, 4) << 32;
690 	}
691 
692 	if (bootverbose) {
693 		printf("\tmap[%02x]: type %x, range %2d, base %08x, size %2d",
694 		    reg, pci_maptype(map), ln2range,
695 		    (unsigned int) base, ln2size);
696 		if (type == SYS_RES_IOPORT && !pci_porten(pcib, b, s, f))
697 			printf(", port disabled\n");
698 		else if (type == SYS_RES_MEMORY && !pci_memen(pcib, b, s, f))
699 			printf(", memory disabled\n");
700 		else
701 			printf(", enabled\n");
702 	}
703 
704 	/*
705 	 * This code theoretically does the right thing, but has
706 	 * undesirable side effects in some cases where
707 	 * peripherals respond oddly to having these bits
708 	 * enabled.  Leave them alone by default.
709 	 */
710 #ifdef PCI_ENABLE_IO_MODES
711 	/* Turn on resources that have been left off by a lazy BIOS */
712 	if (type == SYS_RES_IOPORT && !pci_porten(pcib, b, s, f)) {
713 		cmd = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2);
714 		cmd |= PCIM_CMD_PORTEN;
715 		PCIB_WRITE_CONFIG(pcib, b, s, f, PCIR_COMMAND, cmd, 2);
716 	}
717 	if (type == SYS_RES_MEMORY && !pci_memen(pcib, b, s, f)) {
718 		cmd = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2);
719 		cmd |= PCIM_CMD_MEMEN;
720 		PCIB_WRITE_CONFIG(pcib, b, s, f, PCIR_COMMAND, cmd, 2);
721 	}
722 #else
723         if (type == SYS_RES_IOPORT && !pci_porten(pcib, b, s, f))
724                 return (1);
725         if (type == SYS_RES_MEMORY && !pci_memen(pcib, b, s, f))
726 		return (1);
727 #endif
728 
729 	resource_list_add(rl, type, reg, base, base + (1 << ln2size) - 1,
730 	    (1 << ln2size));
731 
732 	return ((ln2range == 64) ? 2 : 1);
733 }
734 
735 static void
736 pci_add_resources(device_t pcib, int b, int s, int f, device_t dev)
737 {
738 	struct pci_devinfo *dinfo = device_get_ivars(dev);
739 	pcicfgregs *cfg = &dinfo->cfg;
740 	struct resource_list *rl = &dinfo->resources;
741 	struct pci_quirk *q;
742 	int i;
743 
744 	for (i = 0; i < cfg->nummaps;) {
745 		i += pci_add_map(pcib, b, s, f, PCIR_MAPS + i*4, rl);
746 	}
747 
748 	for (q = &pci_quirks[0]; q->devid; q++) {
749 		if (q->devid == ((cfg->device << 16) | cfg->vendor)
750 		    && q->type == PCI_QUIRK_MAP_REG)
751 			pci_add_map(pcib, b, s, f, q->arg1, rl);
752 	}
753 
754 	if (cfg->intpin > 0 && PCI_INTERRUPT_VALID(cfg->intline)) {
755 #ifdef __ia64__
756 		/*
757 		 * Re-route interrupts on ia64 so that we can get the
758 		 * I/O SAPIC interrupt numbers (the BIOS leaves legacy
759 		 * PIC interrupt numbers in the intline registers).
760 		 */
761 		cfg->intline = PCIB_ROUTE_INTERRUPT(pcib, dev, cfg->intpin);
762 #endif
763 		resource_list_add(rl, SYS_RES_IRQ, 0, cfg->intline,
764 		  cfg->intline, 1);
765 	}
766 }
767 
768 static void
769 pci_add_children(device_t dev, int busno)
770 {
771 	device_t pcib = device_get_parent(dev);
772 	int maxslots;
773 	int s, f;
774 
775 	maxslots = PCIB_MAXSLOTS(pcib);
776 
777 	for (s = 0; s <= maxslots; s++) {
778 		int pcifunchigh = 0;
779 		for (f = 0; f <= pcifunchigh; f++) {
780 			struct pci_devinfo *dinfo = pci_read_device(pcib,
781 			    busno, s, f, sizeof(struct pci_devinfo));
782 			if (dinfo != NULL) {
783 				if (dinfo->cfg.mfdev)
784 					pcifunchigh = PCI_FUNCMAX;
785 
786 				dinfo->cfg.dev = device_add_child(dev, NULL, -1);
787 				device_set_ivars(dinfo->cfg.dev, dinfo);
788 				pci_add_resources(pcib, busno, s, f,
789 						  dinfo->cfg.dev);
790 				pci_print_verbose(dinfo);
791 			}
792 		}
793 	}
794 }
795 
796 static int
797 pci_probe(device_t dev)
798 {
799 	static int once, busno;
800 	caddr_t vendordata, info;
801 
802 	device_set_desc(dev, "PCI bus");
803 
804 	if (bootverbose)
805 		device_printf(dev, "physical bus=%d\n", pcib_get_bus(dev));
806 
807 	/*
808 	 * Since there can be multiple independantly numbered PCI
809 	 * busses on some large alpha systems, we can't use the unit
810 	 * number to decide what bus we are probing. We ask the parent
811 	 * pcib what our bus number is.
812 	 */
813 	busno = pcib_get_bus(dev);
814 	if (busno < 0)
815 		return (ENXIO);
816 	pci_add_children(dev, busno);
817 
818 	if (!once) {
819 		make_dev(&pcicdev, 0, UID_ROOT, GID_WHEEL, 0644, "pci");
820 		if ((vendordata = preload_search_by_type("pci_vendor_data"))
821 		    != NULL) {
822 			info = preload_search_info(vendordata, MODINFO_ADDR);
823 			pci_vendordata = *(char **)info;
824 			info = preload_search_info(vendordata, MODINFO_SIZE);
825 			pci_vendordata_size = *(size_t *)info;
826 			/* terminate the database */
827 			pci_vendordata[pci_vendordata_size] = '\n';
828 		}
829 		once++;
830 	}
831 
832 	return (0);
833 }
834 
835 int
836 pci_print_child(device_t dev, device_t child)
837 {
838 	struct pci_devinfo *dinfo;
839 	struct resource_list *rl;
840 	pcicfgregs *cfg;
841 	int retval = 0;
842 
843 	dinfo = device_get_ivars(child);
844 	cfg = &dinfo->cfg;
845 	rl = &dinfo->resources;
846 
847 	retval += bus_print_child_header(dev, child);
848 
849 	retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
850 	retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
851 	retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
852 	if (device_get_flags(dev))
853 		retval += printf(" flags %#x", device_get_flags(dev));
854 
855 	retval += printf(" at device %d.%d", pci_get_slot(child),
856 	    pci_get_function(child));
857 
858 	retval += bus_print_child_footer(dev, child);
859 
860 	return (retval);
861 }
862 
863 static struct
864 {
865 	int	class;
866 	int	subclass;
867 	char	*desc;
868 } pci_nomatch_tab[] = {
869 	{PCIC_OLD,		-1,			"old"},
870 	{PCIC_OLD,		PCIS_OLD_NONVGA,	"non-VGA display device"},
871 	{PCIC_OLD,		PCIS_OLD_VGA,		"VGA-compatible display device"},
872 	{PCIC_STORAGE,		-1,			"mass storage"},
873 	{PCIC_STORAGE,		PCIS_STORAGE_SCSI,	"SCSI"},
874 	{PCIC_STORAGE,		PCIS_STORAGE_IDE,	"ATA"},
875 	{PCIC_STORAGE,		PCIS_STORAGE_FLOPPY,	"floppy disk"},
876 	{PCIC_STORAGE,		PCIS_STORAGE_IPI,	"IPI"},
877 	{PCIC_STORAGE,		PCIS_STORAGE_RAID,	"RAID"},
878 	{PCIC_NETWORK,		-1,			"network"},
879 	{PCIC_NETWORK,		PCIS_NETWORK_ETHERNET,	"ethernet"},
880 	{PCIC_NETWORK,		PCIS_NETWORK_TOKENRING,	"token ring"},
881 	{PCIC_NETWORK,		PCIS_NETWORK_FDDI,	"fddi"},
882 	{PCIC_NETWORK,		PCIS_NETWORK_ATM,	"ATM"},
883 	{PCIC_DISPLAY,		-1,			"display"},
884 	{PCIC_DISPLAY,		PCIS_DISPLAY_VGA,	"VGA"},
885 	{PCIC_DISPLAY,		PCIS_DISPLAY_XGA,	"XGA"},
886 	{PCIC_MULTIMEDIA,	-1,			"multimedia"},
887 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_VIDEO,	"video"},
888 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_AUDIO,	"audio"},
889 	{PCIC_MEMORY,		-1,			"memory"},
890 	{PCIC_MEMORY,		PCIS_MEMORY_RAM,	"RAM"},
891 	{PCIC_MEMORY,		PCIS_MEMORY_FLASH,	"flash"},
892 	{PCIC_BRIDGE,		-1,			"bridge"},
893 	{PCIC_BRIDGE,		PCIS_BRIDGE_HOST,	"HOST-PCI"},
894 	{PCIC_BRIDGE,		PCIS_BRIDGE_ISA,	"PCI-ISA"},
895 	{PCIC_BRIDGE,		PCIS_BRIDGE_EISA,	"PCI-EISA"},
896 	{PCIC_BRIDGE,		PCIS_BRIDGE_MCA,	"PCI-MCA"},
897 	{PCIC_BRIDGE,		PCIS_BRIDGE_PCI,	"PCI-PCI"},
898 	{PCIC_BRIDGE,		PCIS_BRIDGE_PCMCIA,	"PCI-PCMCIA"},
899 	{PCIC_BRIDGE,		PCIS_BRIDGE_NUBUS,	"PCI-NuBus"},
900 	{PCIC_BRIDGE,		PCIS_BRIDGE_CARDBUS,	"PCI-CardBus"},
901 	{PCIC_BRIDGE,		PCIS_BRIDGE_OTHER,	"PCI-unknown"},
902 	{PCIC_SIMPLECOMM,	-1,			"simple comms"},
903 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_UART,	"UART"},	/* could detect 16550 */
904 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_PAR,	"parallel port"},
905 	{PCIC_BASEPERIPH,	-1,			"base peripheral"},
906 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_PIC,	"interrupt controller"},
907 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_DMA,	"DMA controller"},
908 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_TIMER,	"timer"},
909 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_RTC,	"realtime clock"},
910 	{PCIC_INPUTDEV,		-1,			"input device"},
911 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_KEYBOARD,	"keyboard"},
912 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_DIGITIZER,"digitizer"},
913 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_MOUSE,	"mouse"},
914 	{PCIC_DOCKING,		-1,			"docking station"},
915 	{PCIC_PROCESSOR,	-1,			"processor"},
916 	{PCIC_SERIALBUS,	-1,			"serial bus"},
917 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_FW,	"FireWire"},
918 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_ACCESS,	"AccessBus"},
919 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_SSA,	"SSA"},
920 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_USB,	"USB"},
921 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_FC,	"Fibre Channel"},
922 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_SMBUS,	"SMBus"},
923 	{0, 0,		NULL}
924 };
925 
926 void
927 pci_probe_nomatch(device_t dev, device_t child)
928 {
929 	int	i;
930 	char	*cp, *scp, *device;
931 
932 	/*
933 	 * Look for a listing for this device in a loaded device database.
934 	 */
935 	if ((device = pci_describe_device(child)) != NULL) {
936 		device_printf(dev, "<%s>", device);
937 		free(device, M_DEVBUF);
938 	} else {
939 		/*
940 		 * Scan the class/subclass descriptions for a general
941 		 * description.
942 		 */
943 		cp = "unknown";
944 		scp = NULL;
945 		for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
946 			if (pci_nomatch_tab[i].class == pci_get_class(child)) {
947 				if (pci_nomatch_tab[i].subclass == -1) {
948 					cp = pci_nomatch_tab[i].desc;
949 				} else if (pci_nomatch_tab[i].subclass ==
950 				    pci_get_subclass(child)) {
951 					scp = pci_nomatch_tab[i].desc;
952 				}
953 			}
954 		}
955 		device_printf(dev, "<%s%s%s>",
956 		    cp ? : "", ((cp != NULL) && (scp != NULL)) ? ", " : "",
957 		    scp ? : "");
958 	}
959 	printf(" at device %d.%d (no driver attached)\n",
960 	    pci_get_slot(child), pci_get_function(child));
961 	return;
962 }
963 
964 /*
965  * Parse the PCI device database, if loaded, and return a pointer to a
966  * description of the device.
967  *
968  * The database is flat text formatted as follows:
969  *
970  * Any line not in a valid format is ignored.
971  * Lines are terminated with newline '\n' characters.
972  *
973  * A VENDOR line consists of the 4 digit (hex) vendor code, a TAB, then
974  * the vendor name.
975  *
976  * A DEVICE line is entered immediately below the corresponding VENDOR ID.
977  * - devices cannot be listed without a corresponding VENDOR line.
978  * A DEVICE line consists of a TAB, the 4 digit (hex) device code,
979  * another TAB, then the device name.
980  */
981 
982 /*
983  * Assuming (ptr) points to the beginning of a line in the database,
984  * return the vendor or device and description of the next entry.
985  * The value of (vendor) or (device) inappropriate for the entry type
986  * is set to -1.  Returns nonzero at the end of the database.
987  *
988  * Note that this is slightly unrobust in the face of corrupt data;
989  * we attempt to safeguard against this by spamming the end of the
990  * database with a newline when we initialise.
991  */
992 static int
993 pci_describe_parse_line(char **ptr, int *vendor, int *device, char **desc)
994 {
995 	char	*cp = *ptr;
996 	int	left;
997 
998 	*device = -1;
999 	*vendor = -1;
1000 	**desc = '\0';
1001 	for (;;) {
1002 		left = pci_vendordata_size - (cp - pci_vendordata);
1003 		if (left <= 0) {
1004 			*ptr = cp;
1005 			return(1);
1006 		}
1007 
1008 		/* vendor entry? */
1009 		if (*cp != '\t' &&
1010 		    sscanf(cp, "%x\t%80[^\n]", vendor, *desc) == 2)
1011 			break;
1012 		/* device entry? */
1013 		if (*cp == '\t' &&
1014 		    sscanf(cp, "%x\t%80[^\n]", device, *desc) == 2)
1015 			break;
1016 
1017 		/* skip to next line */
1018 		while (*cp != '\n' && left > 0) {
1019 			cp++;
1020 			left--;
1021 		}
1022 		if (*cp == '\n') {
1023 			cp++;
1024 			left--;
1025 		}
1026 	}
1027 	/* skip to next line */
1028 	while (*cp != '\n' && left > 0) {
1029 		cp++;
1030 		left--;
1031 	}
1032 	if (*cp == '\n' && left > 0)
1033 		cp++;
1034 	*ptr = cp;
1035 	return(0);
1036 }
1037 
1038 static char *
1039 pci_describe_device(device_t dev)
1040 {
1041 	int	vendor, device;
1042 	char	*desc, *vp, *dp, *line;
1043 
1044 	desc = vp = dp = NULL;
1045 
1046 	/*
1047 	 * If we have no vendor data, we can't do anything.
1048 	 */
1049 	if (pci_vendordata == NULL)
1050 		goto out;
1051 
1052 	/*
1053 	 * Scan the vendor data looking for this device
1054 	 */
1055 	line = pci_vendordata;
1056 	if ((vp = malloc(80, M_DEVBUF, M_NOWAIT)) == NULL)
1057 		goto out;
1058 	for (;;) {
1059 		if (pci_describe_parse_line(&line, &vendor, &device, &vp))
1060 			goto out;
1061 		if (vendor == pci_get_vendor(dev))
1062 			break;
1063 	}
1064 	if ((dp = malloc(80, M_DEVBUF, M_NOWAIT)) == NULL)
1065 		goto out;
1066 	for (;;) {
1067 		if (pci_describe_parse_line(&line, &vendor, &device, &dp)) {
1068 			*dp = 0;
1069 			break;
1070 		}
1071 		if (vendor != -1) {
1072 			*dp = 0;
1073 			break;
1074 		}
1075 		if (device == pci_get_device(dev))
1076 			break;
1077 	}
1078 	if (dp[0] == '\0')
1079 		snprintf(dp, 80, "0x%x", pci_get_device(dev));
1080 	if ((desc = malloc(strlen(vp) + strlen(dp) + 3, M_DEVBUF, M_NOWAIT)) !=
1081 	    NULL)
1082 		sprintf(desc, "%s, %s", vp, dp);
1083  out:
1084 	if (vp != NULL)
1085 		free(vp, M_DEVBUF);
1086 	if (dp != NULL)
1087 		free(dp, M_DEVBUF);
1088 	return(desc);
1089 }
1090 
1091 int
1092 pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1093 {
1094 	struct pci_devinfo *dinfo;
1095 	pcicfgregs *cfg;
1096 
1097 	dinfo = device_get_ivars(child);
1098 	cfg = &dinfo->cfg;
1099 
1100 	switch (which) {
1101 	case PCI_IVAR_SUBVENDOR:
1102 		*result = cfg->subvendor;
1103 		break;
1104 	case PCI_IVAR_SUBDEVICE:
1105 		*result = cfg->subdevice;
1106 		break;
1107 	case PCI_IVAR_VENDOR:
1108 		*result = cfg->vendor;
1109 		break;
1110 	case PCI_IVAR_DEVICE:
1111 		*result = cfg->device;
1112 		break;
1113 	case PCI_IVAR_DEVID:
1114 		*result = (cfg->device << 16) | cfg->vendor;
1115 		break;
1116 	case PCI_IVAR_CLASS:
1117 		*result = cfg->baseclass;
1118 		break;
1119 	case PCI_IVAR_SUBCLASS:
1120 		*result = cfg->subclass;
1121 		break;
1122 	case PCI_IVAR_PROGIF:
1123 		*result = cfg->progif;
1124 		break;
1125 	case PCI_IVAR_REVID:
1126 		*result = cfg->revid;
1127 		break;
1128 	case PCI_IVAR_INTPIN:
1129 		*result = cfg->intpin;
1130 		break;
1131 	case PCI_IVAR_IRQ:
1132 		*result = cfg->intline;
1133 		break;
1134 	case PCI_IVAR_BUS:
1135 		*result = cfg->bus;
1136 		break;
1137 	case PCI_IVAR_SLOT:
1138 		*result = cfg->slot;
1139 		break;
1140 	case PCI_IVAR_FUNCTION:
1141 		*result = cfg->func;
1142 		break;
1143 	default:
1144 		return (ENOENT);
1145 	}
1146 	return (0);
1147 }
1148 
1149 int
1150 pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1151 {
1152 	struct pci_devinfo *dinfo;
1153 	pcicfgregs *cfg;
1154 
1155 	dinfo = device_get_ivars(child);
1156 	cfg = &dinfo->cfg;
1157 
1158 	switch (which) {
1159 	case PCI_IVAR_SUBVENDOR:
1160 	case PCI_IVAR_SUBDEVICE:
1161 	case PCI_IVAR_VENDOR:
1162 	case PCI_IVAR_DEVICE:
1163 	case PCI_IVAR_DEVID:
1164 	case PCI_IVAR_CLASS:
1165 	case PCI_IVAR_SUBCLASS:
1166 	case PCI_IVAR_PROGIF:
1167 	case PCI_IVAR_REVID:
1168 	case PCI_IVAR_INTPIN:
1169 	case PCI_IVAR_IRQ:
1170 	case PCI_IVAR_BUS:
1171 	case PCI_IVAR_SLOT:
1172 	case PCI_IVAR_FUNCTION:
1173 		return (EINVAL);	/* disallow for now */
1174 
1175 	default:
1176 		return (ENOENT);
1177 	}
1178 	return (0);
1179 }
1180 
1181 struct resource *
1182 pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
1183 		   u_long start, u_long end, u_long count, u_int flags)
1184 {
1185 	struct pci_devinfo *dinfo = device_get_ivars(child);
1186 	struct resource_list *rl = &dinfo->resources;
1187 	pcicfgregs *cfg = &dinfo->cfg;
1188 
1189 	/*
1190 	 * Perform lazy resource allocation
1191 	 *
1192 	 * XXX add support here for SYS_RES_IOPORT and SYS_RES_MEMORY
1193 	 */
1194 	if (device_get_parent(child) == dev) {
1195 		/*
1196 		 * If device doesn't have an interrupt routed, and is
1197 		 * deserving of  an interrupt, try to assign it one.
1198 		 */
1199 		if ((type == SYS_RES_IRQ) &&
1200 		    !PCI_INTERRUPT_VALID(cfg->intline) &&
1201 		    (cfg->intpin != 0)) {
1202 			cfg->intline = PCIB_ROUTE_INTERRUPT(
1203 				device_get_parent(dev), child, cfg->intpin);
1204 			if (PCI_INTERRUPT_VALID(cfg->intline)) {
1205 				pci_write_config(child, PCIR_INTLINE,
1206 				    cfg->intline, 1);
1207 				resource_list_add(rl, SYS_RES_IRQ, 0,
1208 				    cfg->intline, cfg->intline, 1);
1209 			}
1210 		}
1211 	}
1212 
1213 	return (resource_list_alloc(rl, dev, child, type, rid,
1214 	    start, end, count, flags));
1215 }
1216 
1217 void
1218 pci_delete_resource(device_t dev, device_t child, int type, int rid)
1219 {
1220 	struct pci_devinfo *dinfo;
1221 	struct resource_list *rl;
1222 	struct resource_list_entry *rle;
1223 
1224 	if (device_get_parent(child) != dev)
1225 		return;
1226 
1227 	dinfo = device_get_ivars(child);
1228 	rl = &dinfo->resources;
1229 	rle = resource_list_find(rl, type, rid);
1230 	if (rle) {
1231 		if (rle->res) {
1232 			if (rle->res->r_dev != dev ||
1233 			    rman_get_flags(rle->res) & RF_ACTIVE) {
1234 				device_printf(dev, "delete_resource: "
1235 				    "Resource still owned by child, oops. "
1236 				    "(type=%d, rid=%d, addr=%lx)\n",
1237 				    rle->type, rle->rid,
1238 				    rman_get_start(rle->res));
1239 				return;
1240 			}
1241 			bus_release_resource(dev, type, rid, rle->res);
1242 		}
1243 		resource_list_delete(rl, type, rid);
1244 	}
1245 	/*
1246 	 * Why do we turn off the PCI configuration BAR when we delete a
1247 	 * resource? -- imp
1248 	 */
1249 	pci_write_config(child, rid, 0, 4);
1250 	BUS_DELETE_RESOURCE(device_get_parent(dev), child, type, rid);
1251 }
1252 
1253 struct resource_list *
1254 pci_get_resource_list (device_t dev, device_t child)
1255 {
1256 	struct pci_devinfo *	dinfo = device_get_ivars(child);
1257 	struct resource_list *  rl = &dinfo->resources;
1258 
1259 	if (!rl)
1260 		return (NULL);
1261 
1262 	return (rl);
1263 }
1264 
1265 u_int32_t
1266 pci_read_config_method(device_t dev, device_t child, int reg, int width)
1267 {
1268 	struct pci_devinfo *dinfo = device_get_ivars(child);
1269 	pcicfgregs *cfg = &dinfo->cfg;
1270 
1271 	return (PCIB_READ_CONFIG(device_get_parent(dev),
1272 	    cfg->bus, cfg->slot, cfg->func, reg, width));
1273 }
1274 
1275 void
1276 pci_write_config_method(device_t dev, device_t child, int reg,
1277     u_int32_t val, int width)
1278 {
1279 	struct pci_devinfo *dinfo = device_get_ivars(child);
1280 	pcicfgregs *cfg = &dinfo->cfg;
1281 
1282 	PCIB_WRITE_CONFIG(device_get_parent(dev),
1283 	    cfg->bus, cfg->slot, cfg->func, reg, val, width);
1284 }
1285 
1286 static int
1287 pci_modevent(module_t mod, int what, void *arg)
1288 {
1289 	switch (what) {
1290 	case MOD_LOAD:
1291 		STAILQ_INIT(&pci_devq);
1292 		pci_generation = 0;
1293 		break;
1294 
1295 	case MOD_UNLOAD:
1296 		break;
1297 	}
1298 
1299 	return (0);
1300 }
1301