Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.56.4.1
      1  1.56.4.1  christos /*	$NetBSD: pci_machdep.c,v 1.56.4.1 2019/06/10 22:05:58 christos Exp $	*/
      2       1.1       leo 
      3       1.1       leo /*
      4       1.1       leo  * Copyright (c) 1996 Leo Weppelman.  All rights reserved.
      5       1.7       cgd  * Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.
      6      1.13   mycroft  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
      7       1.1       leo  *
      8       1.1       leo  * Redistribution and use in source and binary forms, with or without
      9       1.1       leo  * modification, are permitted provided that the following conditions
     10       1.1       leo  * are met:
     11       1.1       leo  * 1. Redistributions of source code must retain the above copyright
     12       1.1       leo  *    notice, this list of conditions and the following disclaimer.
     13       1.1       leo  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1       leo  *    notice, this list of conditions and the following disclaimer in the
     15       1.1       leo  *    documentation and/or other materials provided with the distribution.
     16       1.1       leo  * 3. All advertising materials mentioning features or use of this software
     17       1.1       leo  *    must display the following acknowledgement:
     18      1.13   mycroft  *	This product includes software developed by Charles M. Hannum.
     19       1.1       leo  * 4. The name of the author may not be used to endorse or promote products
     20       1.1       leo  *    derived from this software without specific prior written permission.
     21       1.1       leo  *
     22       1.1       leo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23       1.1       leo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24       1.1       leo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25       1.1       leo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26       1.1       leo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27       1.1       leo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28       1.1       leo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29       1.1       leo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30       1.1       leo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31       1.1       leo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32       1.1       leo  */
     33       1.1       leo 
     34      1.40     lukem #include <sys/cdefs.h>
     35  1.56.4.1  christos __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.56.4.1 2019/06/10 22:05:58 christos Exp $");
     36      1.40     lukem 
     37      1.31       leo #include "opt_mbtype.h"
     38      1.40     lukem 
     39       1.1       leo #include <sys/types.h>
     40       1.1       leo #include <sys/param.h>
     41       1.1       leo #include <sys/time.h>
     42       1.1       leo #include <sys/systm.h>
     43       1.1       leo #include <sys/errno.h>
     44       1.1       leo #include <sys/device.h>
     45      1.14    thomas #include <sys/malloc.h>
     46       1.1       leo 
     47      1.30       leo #define _ATARI_BUS_DMA_PRIVATE
     48      1.52    dyoung #include <sys/bus.h>
     49       1.1       leo 
     50       1.1       leo #include <dev/pci/pcivar.h>
     51       1.1       leo #include <dev/pci/pcireg.h>
     52      1.55   tsutsui #include <dev/pci/pcidevs.h>
     53       1.1       leo 
     54      1.30       leo #include <uvm/uvm_extern.h>
     55      1.30       leo 
     56       1.1       leo #include <machine/cpu.h>
     57       1.1       leo #include <machine/iomap.h>
     58       1.9       leo #include <machine/mfp.h>
     59      1.10       leo 
     60       1.1       leo #include <atari/atari/device.h>
     61      1.17       leo #include <atari/pci/pci_vga.h>
     62       1.1       leo 
     63       1.9       leo /*
     64      1.14    thomas  * Sizes of pci memory and I/O area.
     65       1.9       leo  */
     66      1.14    thomas #define PCI_MEM_END     0x10000000      /* 256 MByte */
     67      1.14    thomas #define PCI_IO_END      0x10000000      /* 256 MByte */
     68      1.14    thomas 
     69      1.14    thomas /*
     70      1.14    thomas  * We preserve some space at the begin of the pci area for 32BIT_1M
     71      1.14    thomas  * devices and standard vga.
     72      1.14    thomas  */
     73      1.14    thomas #define PCI_MEM_START   0x00100000      /*   1 MByte */
     74      1.15    thomas #define PCI_IO_START    0x00004000      /*  16 kByte (some PCI cards allow only
     75      1.25       leo 					    I/O addresses up to 0xffff) */
     76      1.20    thomas 
     77      1.20    thomas /*
     78      1.20    thomas  * PCI memory and IO should be aligned acording to this masks
     79      1.20    thomas  */
     80      1.20    thomas #define PCI_MACHDEP_IO_ALIGN_MASK	0xffffff00
     81      1.20    thomas #define PCI_MACHDEP_MEM_ALIGN_MASK	0xfffff000
     82      1.20    thomas 
     83      1.19       leo /*
     84      1.19       leo  * Convert a PCI 'device' number to a slot number.
     85      1.19       leo  */
     86      1.19       leo #define	DEV2SLOT(dev)	(3 - dev)
     87      1.14    thomas 
     88      1.14    thomas /*
     89      1.14    thomas  * Struct to hold the memory and I/O datas of the pci devices
     90      1.14    thomas  */
     91      1.14    thomas struct pci_memreg {
     92  1.56.4.1  christos 	LIST_ENTRY(pci_memreg) link;
     93  1.56.4.1  christos 	int dev;
     94  1.56.4.1  christos 	pcitag_t tag;
     95  1.56.4.1  christos 	pcireg_t reg, address, mask;
     96  1.56.4.1  christos 	uint32_t size;
     97  1.56.4.1  christos 	uint32_t csr;
     98      1.14    thomas };
     99      1.14    thomas 
    100      1.14    thomas typedef LIST_HEAD(pci_memreg_head, pci_memreg) PCI_MEMREG;
    101       1.9       leo 
    102      1.30       leo /*
    103      1.30       leo  * Entry points for PCI DMA.  Use only the 'standard' functions.
    104      1.30       leo  */
    105      1.44       dsl int	_bus_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
    106      1.44       dsl 	    bus_size_t, int, bus_dmamap_t *);
    107      1.30       leo struct atari_bus_dma_tag pci_bus_dma_tag = {
    108      1.30       leo 	0,
    109      1.33       leo #if defined(_ATARIHW_)
    110      1.31       leo 	0x80000000, /* On the Hades, CPU memory starts here PCI-wise */
    111      1.31       leo #else
    112      1.31       leo 	0,
    113      1.31       leo #endif
    114      1.30       leo 	_bus_dmamap_create,
    115      1.30       leo 	_bus_dmamap_destroy,
    116      1.30       leo 	_bus_dmamap_load,
    117      1.30       leo 	_bus_dmamap_load_mbuf,
    118      1.30       leo 	_bus_dmamap_load_uio,
    119      1.30       leo 	_bus_dmamap_load_raw,
    120      1.30       leo 	_bus_dmamap_unload,
    121      1.30       leo 	_bus_dmamap_sync,
    122      1.30       leo };
    123      1.30       leo 
    124      1.51   tsutsui int	ataripcibusprint(void *, const char *);
    125      1.51   tsutsui int	pcibusmatch(device_t, cfdata_t, void *);
    126      1.51   tsutsui void	pcibusattach(device_t, device_t, void *);
    127      1.44       dsl 
    128      1.44       dsl static void enable_pci_devices(void);
    129      1.44       dsl static void insert_into_list(PCI_MEMREG *head, struct pci_memreg *elem);
    130      1.44       dsl static int overlap_pci_areas(struct pci_memreg *p,
    131  1.56.4.1  christos     struct pci_memreg *self, u_int addr, u_int size, u_int what);
    132       1.1       leo 
    133      1.51   tsutsui CFATTACH_DECL_NEW(pcib, 0,
    134      1.38   thorpej     pcibusmatch, pcibusattach, NULL, NULL);
    135       1.1       leo 
    136      1.27       leo /*
    137      1.27       leo  * We need some static storage to probe pci-busses for VGA cards during
    138      1.27       leo  * early console init.
    139      1.27       leo  */
    140      1.27       leo static struct atari_bus_space	bs_storage[2];	/* 1 iot, 1 memt */
    141      1.27       leo 
    142       1.1       leo int
    143      1.51   tsutsui pcibusmatch(device_t parent, cfdata_t cf, void *aux)
    144       1.1       leo {
    145  1.56.4.1  christos 	static int nmatched = 0;
    146      1.26       leo 
    147      1.51   tsutsui 	if (strcmp((char *)aux, "pcib"))
    148      1.48   tsutsui 		return 0;	/* Wrong number... */
    149      1.26       leo 
    150      1.48   tsutsui 	if (atari_realconfig == 0)
    151      1.48   tsutsui 		return 1;
    152      1.27       leo 
    153  1.56.4.1  christos 	if ((machineid & (ATARI_HADES|ATARI_MILAN)) != 0) {
    154      1.26       leo 		/*
    155      1.31       leo 		 * Both Hades and Milan have only one pci bus
    156      1.26       leo 		 */
    157      1.26       leo 		if (nmatched)
    158      1.48   tsutsui 			return 0;
    159      1.26       leo 		nmatched++;
    160      1.48   tsutsui 		return 1;
    161      1.26       leo 	}
    162      1.48   tsutsui 	return 0;
    163       1.1       leo }
    164       1.1       leo 
    165       1.1       leo void
    166      1.51   tsutsui pcibusattach(device_t parent, device_t self, void *aux)
    167       1.1       leo {
    168  1.56.4.1  christos 	struct pcibus_attach_args pba;
    169       1.1       leo 
    170       1.4       leo 	pba.pba_pc      = NULL;
    171       1.1       leo 	pba.pba_bus     = 0;
    172      1.35   thorpej 	pba.pba_bridgetag = NULL;
    173      1.50    dyoung 	pba.pba_flags	= PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY;
    174      1.30       leo 	pba.pba_dmat	= &pci_bus_dma_tag;
    175      1.27       leo 	pba.pba_iot     = leb_alloc_bus_space_tag(&bs_storage[0]);
    176      1.29       leo 	pba.pba_memt    = leb_alloc_bus_space_tag(&bs_storage[1]);
    177      1.11       leo 	if ((pba.pba_iot == NULL) || (pba.pba_memt == NULL)) {
    178      1.11       leo 		printf("leb_alloc_bus_space_tag failed!\n");
    179      1.11       leo 		return;
    180      1.11       leo 	}
    181      1.11       leo 	pba.pba_iot->base  = PCI_IO_PHYS;
    182      1.11       leo 	pba.pba_memt->base = PCI_MEM_PHYS;
    183       1.6       leo 
    184      1.51   tsutsui 	if (self == NULL) {
    185      1.27       leo 		/*
    186      1.27       leo 		 * Scan the bus for a VGA-card that we support. If we
    187      1.27       leo 		 * find one, try to initialize it to a 'standard' text
    188      1.27       leo 		 * mode (80x25).
    189      1.27       leo 		 */
    190      1.32       leo 		check_for_vga(pba.pba_iot, pba.pba_memt);
    191      1.27       leo 		return;
    192      1.27       leo 	}
    193      1.27       leo 
    194      1.27       leo 	enable_pci_devices();
    195      1.27       leo 
    196      1.31       leo #if defined(_ATARIHW_)
    197       1.9       leo 	MFP2->mf_aer &= ~(0x27); /* PCI interrupts: HIGH -> LOW */
    198      1.31       leo #endif
    199       1.9       leo 
    200       1.6       leo 	printf("\n");
    201       1.1       leo 
    202      1.51   tsutsui 	config_found_ia(self, "pcibus", &pba, ataripcibusprint);
    203       1.1       leo }
    204       1.1       leo 
    205       1.1       leo int
    206      1.51   tsutsui ataripcibusprint(void *aux, const char *name)
    207       1.1       leo {
    208      1.48   tsutsui 
    209      1.48   tsutsui 	if (name == NULL)
    210      1.48   tsutsui 		return UNCONF;
    211      1.48   tsutsui 	return QUIET;
    212       1.1       leo }
    213       1.1       leo 
    214       1.1       leo void
    215      1.51   tsutsui pci_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba)
    216       1.1       leo {
    217       1.1       leo }
    218       1.1       leo 
    219       1.1       leo /*
    220       1.9       leo  * Initialize the PCI-bus. The Atari-BIOS does not do this, so....
    221      1.14    thomas  * We only disable all devices here. Memory and I/O enabling is done
    222      1.14    thomas  * later at pcibusattach.
    223       1.9       leo  */
    224       1.9       leo void
    225      1.47    cegger init_pci_bus(void)
    226       1.9       leo {
    227       1.9       leo 	pci_chipset_tag_t	pc = NULL; /* XXX */
    228       1.9       leo 	pcitag_t		tag;
    229      1.14    thomas 	pcireg_t		csr;
    230  1.56.4.1  christos 	int			device, maxndevs;
    231  1.56.4.1  christos 	uint32_t		id;
    232       1.9       leo 
    233      1.14    thomas 	tag   = 0;
    234      1.14    thomas 	id    = 0;
    235       1.9       leo 
    236       1.9       leo 	maxndevs = pci_bus_maxdevs(pc, 0);
    237       1.9       leo 
    238       1.9       leo 	for (device = 0; device < maxndevs; device++) {
    239       1.9       leo 
    240       1.9       leo 		tag = pci_make_tag(pc, 0, device, 0);
    241       1.9       leo 		id  = pci_conf_read(pc, tag, PCI_ID_REG);
    242       1.9       leo 		if (id == 0 || id == 0xffffffff)
    243       1.9       leo 			continue;
    244       1.9       leo 
    245      1.14    thomas 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    246      1.14    thomas 		csr &= ~(PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_IO_ENABLE);
    247      1.14    thomas 		csr &= ~PCI_COMMAND_MASTER_ENABLE;
    248      1.14    thomas 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    249      1.14    thomas 	}
    250      1.14    thomas }
    251      1.14    thomas 
    252      1.14    thomas /*
    253      1.14    thomas  * insert a new element in an existing list that the ID's (size in struct
    254      1.14    thomas  * pci_memreg) are sorted.
    255      1.14    thomas  */
    256      1.14    thomas static void
    257      1.45       dsl insert_into_list(PCI_MEMREG *head, struct pci_memreg *elem)
    258      1.14    thomas {
    259  1.56.4.1  christos 	struct pci_memreg *p, *q;
    260      1.14    thomas 
    261  1.56.4.1  christos 	p = LIST_FIRST(head);
    262  1.56.4.1  christos 	q = NULL;
    263      1.14    thomas 
    264  1.56.4.1  christos 	for (; p != NULL && p->size < elem->size;
    265  1.56.4.1  christos 	    q = p, p = LIST_NEXT(p, link))
    266  1.56.4.1  christos 		;
    267  1.56.4.1  christos 
    268  1.56.4.1  christos 	if (q == NULL) {
    269  1.56.4.1  christos 		LIST_INSERT_HEAD(head, elem, link);
    270  1.56.4.1  christos 	} else {
    271  1.56.4.1  christos 		LIST_INSERT_AFTER(q, elem, link);
    272  1.56.4.1  christos 	}
    273      1.14    thomas }
    274      1.14    thomas 
    275      1.14    thomas /*
    276      1.14    thomas  * Test if a new selected area overlaps with an already (probably preselected)
    277      1.14    thomas  * pci area.
    278      1.14    thomas  */
    279      1.14    thomas static int
    280  1.56.4.1  christos overlap_pci_areas(struct pci_memreg *p, struct pci_memreg *self, u_int addr,
    281  1.56.4.1  christos     u_int size, u_int what)
    282      1.14    thomas {
    283  1.56.4.1  christos 	struct pci_memreg *q;
    284      1.14    thomas 
    285  1.56.4.1  christos 	if (p == NULL)
    286  1.56.4.1  christos 		return 0;
    287      1.14    thomas 
    288  1.56.4.1  christos 	q = p;
    289  1.56.4.1  christos 	while (q != NULL) {
    290  1.56.4.1  christos 		if ((q != self) && (q->csr & what)) {
    291  1.56.4.1  christos 			if ((addr >= q->address) &&
    292  1.56.4.1  christos 			    (addr < (q->address + q->size))) {
    293      1.14    thomas #ifdef DEBUG_PCI_MACHDEP
    294  1.56.4.1  christos 				printf("\noverlap area dev %d reg 0x%02x "
    295  1.56.4.1  christos 				    "with dev %d reg 0x%02x",
    296  1.56.4.1  christos 				    self->dev, self->reg, q->dev, q->reg);
    297      1.14    thomas #endif
    298  1.56.4.1  christos 				return 1;
    299  1.56.4.1  christos 			}
    300  1.56.4.1  christos 			if ((q->address >= addr) &&
    301  1.56.4.1  christos 			    (q->address < (addr + size))) {
    302      1.14    thomas #ifdef DEBUG_PCI_MACHDEP
    303  1.56.4.1  christos 				printf("\noverlap area dev %d reg 0x%02x "
    304  1.56.4.1  christos 				    "with dev %d reg 0x%02x",
    305  1.56.4.1  christos 				    self->dev, self->reg, q->dev, q->reg);
    306      1.14    thomas #endif
    307  1.56.4.1  christos 				return 1;
    308  1.56.4.1  christos 			}
    309  1.56.4.1  christos 		}
    310  1.56.4.1  christos 		q = LIST_NEXT(q, link);
    311      1.14    thomas 	}
    312  1.56.4.1  christos 	return 0;
    313      1.14    thomas }
    314      1.14    thomas 
    315      1.14    thomas /*
    316      1.14    thomas  * Enable memory and I/O on pci devices. Care about already enabled devices
    317      1.14    thomas  * (probabaly by the console driver).
    318      1.14    thomas  *
    319      1.14    thomas  * The idea behind the following code is:
    320      1.14    thomas  * We build a by sizes sorted list of the requirements of the different
    321      1.14    thomas  * pci devices. After that we choose the start addresses of that areas
    322      1.14    thomas  * in such a way that they are placed as closed as possible together.
    323      1.14    thomas  */
    324      1.14    thomas static void
    325      1.47    cegger enable_pci_devices(void)
    326      1.14    thomas {
    327  1.56.4.1  christos 	PCI_MEMREG memlist;
    328  1.56.4.1  christos 	PCI_MEMREG iolist;
    329  1.56.4.1  christos 	struct pci_memreg *p, *q;
    330  1.56.4.1  christos 	int dev, reg;
    331  1.56.4.1  christos 	uint32_t id, class;
    332  1.56.4.1  christos 	pcitag_t tag;
    333  1.56.4.1  christos 	pcireg_t csr, address, mask;
    334  1.56.4.1  christos 	pci_chipset_tag_t pc;
    335  1.56.4.1  christos 	int sizecnt, membase_1m;
    336  1.56.4.1  christos 
    337  1.56.4.1  christos 	pc = 0;
    338  1.56.4.1  christos 	csr = 0;
    339  1.56.4.1  christos 	tag = 0;
    340      1.14    thomas 
    341  1.56.4.1  christos 	LIST_INIT(&memlist);
    342  1.56.4.1  christos 	LIST_INIT(&iolist);
    343      1.14    thomas 
    344      1.14    thomas 	/*
    345  1.56.4.1  christos 	 * first step: go through all devices and gather memory and I/O
    346  1.56.4.1  christos 	 * sizes
    347      1.14    thomas 	 */
    348  1.56.4.1  christos 	for (dev = 0; dev < pci_bus_maxdevs(pc,0); dev++) {
    349  1.56.4.1  christos 
    350  1.56.4.1  christos 		tag = pci_make_tag(pc, 0, dev, 0);
    351  1.56.4.1  christos 		id  = pci_conf_read(pc, tag, PCI_ID_REG);
    352  1.56.4.1  christos 		if (id == 0 || id == 0xffffffff)
    353  1.56.4.1  christos 			continue;
    354      1.14    thomas 
    355  1.56.4.1  christos 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    356      1.14    thomas 
    357      1.14    thomas 		/*
    358  1.56.4.1  christos 		 * special case: if a display card is found and memory is
    359  1.56.4.1  christos 		 * enabled preserve 128k at 0xa0000 as vga memory.
    360  1.56.4.1  christos 		 * XXX: if a display card is found without being enabled,
    361  1.56.4.1  christos 		 * leave it alone! You will usually only create conflicts
    362  1.56.4.1  christos 		 * by enabeling it.
    363      1.20    thomas 		 */
    364  1.56.4.1  christos 		class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    365  1.56.4.1  christos 		switch (PCI_CLASS(class)) {
    366  1.56.4.1  christos 		case PCI_CLASS_PREHISTORIC:
    367  1.56.4.1  christos 		case PCI_CLASS_DISPLAY:
    368  1.56.4.1  christos 			if (csr & (PCI_COMMAND_MEM_ENABLE |
    369  1.56.4.1  christos 			    PCI_COMMAND_MASTER_ENABLE)) {
    370  1.56.4.1  christos 				p = malloc(sizeof(struct pci_memreg),
    371  1.56.4.1  christos 				    M_TEMP, M_WAITOK);
    372  1.56.4.1  christos 				memset(p, 0, sizeof(struct pci_memreg));
    373  1.56.4.1  christos 				p->dev = dev;
    374  1.56.4.1  christos 				p->csr = csr;
    375  1.56.4.1  christos 				p->tag = tag;
    376  1.56.4.1  christos 				p->reg = 0;	/* there is no register
    377  1.56.4.1  christos 						   about this */
    378  1.56.4.1  christos 				p->size = 0x20000; /* 128kByte */
    379  1.56.4.1  christos 				p->mask = 0xfffe0000;
    380  1.56.4.1  christos 				p->address = 0xa0000;
    381  1.56.4.1  christos 
    382  1.56.4.1  christos 				insert_into_list(&memlist, p);
    383  1.56.4.1  christos 			} else
    384  1.56.4.1  christos 				continue;
    385      1.20    thomas 		}
    386      1.20    thomas 
    387  1.56.4.1  christos 		for (reg = PCI_MAPREG_START; reg < PCI_MAPREG_END; reg += 4) {
    388  1.56.4.1  christos 			address = pci_conf_read(pc, tag, reg);
    389  1.56.4.1  christos 			pci_conf_write(pc, tag, reg, 0xffffffff);
    390  1.56.4.1  christos 			mask    = pci_conf_read(pc, tag, reg);
    391  1.56.4.1  christos 			pci_conf_write(pc, tag, reg, address);
    392  1.56.4.1  christos 			if (mask == 0)
    393  1.56.4.1  christos 				continue; /* Register unused */
    394  1.56.4.1  christos 
    395  1.56.4.1  christos 			p = malloc(sizeof(struct pci_memreg),
    396  1.56.4.1  christos 			    M_TEMP, M_WAITOK);
    397  1.56.4.1  christos 			memset(p, 0, sizeof(struct pci_memreg));
    398  1.56.4.1  christos 			p->dev = dev;
    399  1.56.4.1  christos 			p->csr = csr;
    400  1.56.4.1  christos 			p->tag = tag;
    401  1.56.4.1  christos 			p->reg = reg;
    402  1.56.4.1  christos 			p->mask = mask;
    403  1.56.4.1  christos 			p->address = 0;
    404  1.56.4.1  christos 
    405  1.56.4.1  christos 			if ((mask & PCI_MAPREG_TYPE_IO) != 0) {
    406  1.56.4.1  christos 				p->size = PCI_MAPREG_IO_SIZE(mask);
    407  1.56.4.1  christos 
    408  1.56.4.1  christos 				/*
    409  1.56.4.1  christos 				 * Align IO if necessary
    410  1.56.4.1  christos 				 */
    411  1.56.4.1  christos 				if (p->size < PCI_MAPREG_IO_SIZE(
    412  1.56.4.1  christos 				    PCI_MACHDEP_IO_ALIGN_MASK)) {
    413  1.56.4.1  christos 					p->mask = PCI_MACHDEP_IO_ALIGN_MASK;
    414  1.56.4.1  christos 					p->size = PCI_MAPREG_IO_SIZE(p->mask);
    415  1.56.4.1  christos 				}
    416  1.56.4.1  christos 
    417  1.56.4.1  christos 				/*
    418  1.56.4.1  christos 				 * if I/O is already enabled
    419  1.56.4.1  christos 				 * (probably by the console driver)
    420  1.56.4.1  christos 				 * save the address in order to take care
    421  1.56.4.1  christos 				 * about it later.
    422  1.56.4.1  christos 				 */
    423  1.56.4.1  christos 				if ((csr & PCI_COMMAND_IO_ENABLE) != 0)
    424  1.56.4.1  christos 					p->address = address;
    425  1.56.4.1  christos 
    426  1.56.4.1  christos 				insert_into_list(&iolist, p);
    427  1.56.4.1  christos 			} else {
    428  1.56.4.1  christos 				p->size = PCI_MAPREG_MEM_SIZE(mask);
    429  1.56.4.1  christos 
    430  1.56.4.1  christos 				/*
    431  1.56.4.1  christos 				 * Align memory if necessary
    432  1.56.4.1  christos 				 */
    433  1.56.4.1  christos 				if (p->size < PCI_MAPREG_IO_SIZE(
    434  1.56.4.1  christos 				    PCI_MACHDEP_MEM_ALIGN_MASK)) {
    435  1.56.4.1  christos 					p->mask = PCI_MACHDEP_MEM_ALIGN_MASK;
    436  1.56.4.1  christos 					p->size = PCI_MAPREG_MEM_SIZE(p->mask);
    437  1.56.4.1  christos 				}
    438  1.56.4.1  christos 
    439  1.56.4.1  christos 				/*
    440  1.56.4.1  christos 				 * if memory is already enabled
    441  1.56.4.1  christos 				 * (probably by the console driver)
    442  1.56.4.1  christos 				 * save the address in order to take care
    443  1.56.4.1  christos 				 * about it later.
    444  1.56.4.1  christos 				 */
    445  1.56.4.1  christos 				if ((csr & PCI_COMMAND_MEM_ENABLE) != 0)
    446  1.56.4.1  christos 					p->address = address;
    447  1.56.4.1  christos 
    448  1.56.4.1  christos 				insert_into_list(&memlist, p);
    449  1.56.4.1  christos 
    450  1.56.4.1  christos 				if (PCI_MAPREG_MEM_TYPE(mask) ==
    451  1.56.4.1  christos 				    PCI_MAPREG_MEM_TYPE_64BIT)
    452  1.56.4.1  christos 					reg++;
    453  1.56.4.1  christos 			}
    454      1.20    thomas 		}
    455      1.20    thomas 
    456  1.56.4.1  christos #if defined(_ATARIHW_)
    457      1.20    thomas 		/*
    458  1.56.4.1  christos 		 * Both interrupt pin & line are set to the device (== slot)
    459  1.56.4.1  christos 		 * number. This makes sense on the atari Hades because the
    460  1.56.4.1  christos 		 * individual slots are hard-wired to a specific MFP-pin.
    461      1.14    thomas 		 */
    462  1.56.4.1  christos 		csr  = (DEV2SLOT(dev) << PCI_INTERRUPT_PIN_SHIFT);
    463  1.56.4.1  christos 		csr |= (DEV2SLOT(dev) << PCI_INTERRUPT_LINE_SHIFT);
    464  1.56.4.1  christos 		pci_conf_write(pc, tag, PCI_INTERRUPT_REG, csr);
    465      1.33       leo #else
    466      1.56   tsutsui 		/*
    467  1.56.4.1  christos 		 * On the Milan, we accept the BIOS's choice.
    468      1.56   tsutsui 		 */
    469  1.56.4.1  christos 		/*
    470  1.56.4.1  christos 		 * ..except the secondary IDE interrupt that
    471  1.56.4.1  christos 		 * the BIOS doesn't setup.
    472  1.56.4.1  christos 		 */
    473  1.56.4.1  christos #define PIIX_PCIB_MBIRQ0	0x70
    474  1.56.4.1  christos 		if ((PCI_VENDOR(id) == PCI_VENDOR_INTEL) &&
    475  1.56.4.1  christos 		    (PCI_PRODUCT(id) == PCI_PRODUCT_INTEL_82371FB_ISA)) {
    476  1.56.4.1  christos 			/*
    477  1.56.4.1  christos 			 * Set Interrupt Routing for MBIRQ0 to IRQ15.
    478  1.56.4.1  christos 			 * Note Milan's ROM bootloader v1.2 and v1.4
    479  1.56.4.1  christos 			 * incorrectly set MBIRQ0 to IRQ14 (not 15)
    480  1.56.4.1  christos 			 * and unused MBIRQ1 to IRQ 15,
    481  1.56.4.1  christos 			 * so explicitly disable MBIRQ1.
    482  1.56.4.1  christos 			 */
    483  1.56.4.1  christos 			csr = pci_conf_read(pc, tag, PIIX_PCIB_MBIRQ0);
    484  1.56.4.1  christos 			csr &= ~0x0000ffff;
    485  1.56.4.1  christos 			/* MBIRQ1: disable, MBIRQ0: IRQ15 */
    486  1.56.4.1  christos 			csr |=  0x0000800f;
    487  1.56.4.1  christos 			pci_conf_write(pc, tag, PIIX_PCIB_MBIRQ0, csr);
    488      1.55   tsutsui #ifdef DEBUG_PCI_MACHDEP
    489  1.56.4.1  christos 			printf("\npcib0: enable and route MBIRQ0 to irq 15\n");
    490      1.55   tsutsui #endif
    491  1.56.4.1  christos 		}
    492      1.33       leo #endif
    493  1.56.4.1  christos 	}
    494      1.14    thomas 
    495  1.56.4.1  christos 	/*
    496  1.56.4.1  christos 	 * second step: calculate the memory and I/O addresses beginning from
    497  1.56.4.1  christos 	 * PCI_MEM_START and PCI_IO_START. Care about already mapped areas.
    498  1.56.4.1  christos 	 *
    499  1.56.4.1  christos 	 * begin with memory list
    500  1.56.4.1  christos 	 */
    501       1.9       leo 
    502  1.56.4.1  christos 	address = PCI_MEM_START;
    503  1.56.4.1  christos 	sizecnt = 0;
    504  1.56.4.1  christos 	membase_1m = 0;
    505  1.56.4.1  christos 	p = LIST_FIRST(&memlist);
    506  1.56.4.1  christos 	while (p != NULL) {
    507  1.56.4.1  christos 		if ((p->csr & PCI_COMMAND_MEM_ENABLE) == 0) {
    508  1.56.4.1  christos 			if (PCI_MAPREG_MEM_TYPE(p->mask) ==
    509  1.56.4.1  christos 			    PCI_MAPREG_MEM_TYPE_32BIT_1M) {
    510  1.56.4.1  christos 				if (p->size > membase_1m)
    511  1.56.4.1  christos 					membase_1m = p->size;
    512  1.56.4.1  christos 				do {
    513  1.56.4.1  christos 					p->address = membase_1m;
    514  1.56.4.1  christos 					membase_1m += p->size;
    515  1.56.4.1  christos 				} while (overlap_pci_areas(LIST_FIRST(&memlist),
    516  1.56.4.1  christos 				    p, p->address, p->size,
    517  1.56.4.1  christos 				    PCI_COMMAND_MEM_ENABLE));
    518  1.56.4.1  christos 				if (membase_1m > 0x00100000) {
    519  1.56.4.1  christos 					/*
    520  1.56.4.1  christos 					 * Should we panic here?
    521  1.56.4.1  christos 					 */
    522  1.56.4.1  christos 					printf("\npcibus0: dev %d reg %d:"
    523  1.56.4.1  christos 					    " memory not configured",
    524  1.56.4.1  christos 					    p->dev, p->reg);
    525  1.56.4.1  christos 					p->reg = 0;
    526  1.56.4.1  christos 				}
    527  1.56.4.1  christos 			} else {
    528  1.56.4.1  christos 				if (sizecnt && (p->size > sizecnt))
    529  1.56.4.1  christos 					sizecnt =
    530  1.56.4.1  christos 					    ((p->size + sizecnt) & p->mask) &
    531  1.56.4.1  christos 					    PCI_MAPREG_MEM_ADDR_MASK;
    532  1.56.4.1  christos 				if (sizecnt > address) {
    533  1.56.4.1  christos 					address = sizecnt;
    534  1.56.4.1  christos 					sizecnt = 0;
    535  1.56.4.1  christos 				}
    536  1.56.4.1  christos 
    537  1.56.4.1  christos 				do {
    538  1.56.4.1  christos 					p->address = address + sizecnt;
    539  1.56.4.1  christos 					sizecnt += p->size;
    540  1.56.4.1  christos 				} while (overlap_pci_areas(LIST_FIRST(&memlist),
    541  1.56.4.1  christos 				    p, p->address, p->size,
    542  1.56.4.1  christos 				    PCI_COMMAND_MEM_ENABLE));
    543  1.56.4.1  christos 
    544  1.56.4.1  christos 				if ((address + sizecnt) > PCI_MEM_END) {
    545  1.56.4.1  christos 					/*
    546  1.56.4.1  christos 					 * Should we panic here?
    547  1.56.4.1  christos 					 */
    548  1.56.4.1  christos 					printf("\npcibus0: dev %d reg %d:"
    549  1.56.4.1  christos 					    " memory not configured",
    550  1.56.4.1  christos 					    p->dev, p->reg);
    551  1.56.4.1  christos 					p->reg = 0;
    552  1.56.4.1  christos 				}
    553  1.56.4.1  christos 			}
    554  1.56.4.1  christos 			if (p->reg > 0) {
    555  1.56.4.1  christos 				pci_conf_write(pc, p->tag, p->reg, p->address);
    556  1.56.4.1  christos 				csr = pci_conf_read(pc, p->tag,
    557  1.56.4.1  christos 				    PCI_COMMAND_STATUS_REG);
    558  1.56.4.1  christos 				csr |= PCI_COMMAND_MEM_ENABLE |
    559  1.56.4.1  christos 				    PCI_COMMAND_MASTER_ENABLE;
    560  1.56.4.1  christos 				pci_conf_write(pc, p->tag,
    561  1.56.4.1  christos 				    PCI_COMMAND_STATUS_REG, csr);
    562  1.56.4.1  christos 				p->csr = csr;
    563  1.56.4.1  christos 			}
    564      1.14    thomas 		}
    565  1.56.4.1  christos 		p = LIST_NEXT(p, link);
    566  1.56.4.1  christos 	}
    567       1.9       leo 
    568  1.56.4.1  christos 	/*
    569  1.56.4.1  christos 	 * now the I/O list
    570  1.56.4.1  christos 	 */
    571       1.9       leo 
    572  1.56.4.1  christos 	address = PCI_IO_START;
    573  1.56.4.1  christos 	sizecnt = 0;
    574  1.56.4.1  christos 	p = LIST_FIRST(&iolist);
    575  1.56.4.1  christos 	while (p != NULL) {
    576  1.56.4.1  christos 		if (!(p->csr & PCI_COMMAND_IO_ENABLE)) {
    577  1.56.4.1  christos 
    578  1.56.4.1  christos 			if (sizecnt && (p->size > sizecnt))
    579  1.56.4.1  christos 				sizecnt = ((p->size + sizecnt) & p->mask) &
    580  1.56.4.1  christos 				    PCI_MAPREG_IO_ADDR_MASK;
    581  1.56.4.1  christos 			if (sizecnt > address) {
    582  1.56.4.1  christos 				address = sizecnt;
    583  1.56.4.1  christos 				sizecnt = 0;
    584  1.56.4.1  christos 			}
    585  1.56.4.1  christos 
    586  1.56.4.1  christos 			do {
    587  1.56.4.1  christos 				p->address = address + sizecnt;
    588  1.56.4.1  christos 				sizecnt += p->size;
    589  1.56.4.1  christos 			} while (overlap_pci_areas(LIST_FIRST(&iolist), p,
    590  1.56.4.1  christos 			    p->address, p->size, PCI_COMMAND_IO_ENABLE));
    591  1.56.4.1  christos 
    592  1.56.4.1  christos 			if ((address + sizecnt) > PCI_IO_END) {
    593  1.56.4.1  christos 				/*
    594  1.56.4.1  christos 				 * Should we panic here?
    595  1.56.4.1  christos 				 */
    596  1.56.4.1  christos 				printf("\npcibus0: dev %d reg %d:"
    597  1.56.4.1  christos 				    " io not configured",
    598  1.56.4.1  christos 				    p->dev, p->reg);
    599  1.56.4.1  christos 			} else {
    600  1.56.4.1  christos 				pci_conf_write(pc, p->tag, p->reg, p->address);
    601  1.56.4.1  christos 				csr = pci_conf_read(pc, p->tag,
    602  1.56.4.1  christos 				    PCI_COMMAND_STATUS_REG);
    603  1.56.4.1  christos 				csr |= PCI_COMMAND_IO_ENABLE |
    604  1.56.4.1  christos 				    PCI_COMMAND_MASTER_ENABLE;
    605  1.56.4.1  christos 				pci_conf_write(pc, p->tag,
    606  1.56.4.1  christos 				    PCI_COMMAND_STATUS_REG, csr);
    607  1.56.4.1  christos 				p->csr = csr;
    608  1.56.4.1  christos 			}
    609  1.56.4.1  christos 		}
    610  1.56.4.1  christos 		p = LIST_NEXT(p, link);
    611       1.9       leo 	}
    612      1.14    thomas 
    613      1.14    thomas #ifdef DEBUG_PCI_MACHDEP
    614  1.56.4.1  christos 	printf("\nI/O List:\n");
    615  1.56.4.1  christos 	p = LIST_FIRST(&iolist);
    616      1.14    thomas 
    617  1.56.4.1  christos 	while (p != NULL) {
    618  1.56.4.1  christos 		printf("\ndev: %d, reg: 0x%02x, size: 0x%08x, addr: 0x%08x",
    619  1.56.4.1  christos 		    p->dev, p->reg, p->size, p->address);
    620  1.56.4.1  christos 		p = LIST_NEXT(p, link);
    621  1.56.4.1  christos 	}
    622  1.56.4.1  christos 	printf("\nMemlist:");
    623  1.56.4.1  christos 	p = LIST_FIRST(&memlist);
    624  1.56.4.1  christos 
    625  1.56.4.1  christos 	while (p != NULL) {
    626  1.56.4.1  christos 		printf("\ndev: %d, reg: 0x%02x, size: 0x%08x, addr: 0x%08x",
    627  1.56.4.1  christos 		    p->dev, p->reg, p->size, p->address);
    628  1.56.4.1  christos 		p = LIST_NEXT(p, link);
    629  1.56.4.1  christos 	}
    630      1.14    thomas #endif
    631      1.14    thomas 
    632  1.56.4.1  christos 	/*
    633  1.56.4.1  christos 	 * Free the lists
    634  1.56.4.1  christos 	 */
    635      1.14    thomas 	p = LIST_FIRST(&iolist);
    636  1.56.4.1  christos 	while (p != NULL) {
    637  1.56.4.1  christos 		q = p;
    638  1.56.4.1  christos 		LIST_REMOVE(q, link);
    639  1.56.4.1  christos 		free(p, M_WAITOK);
    640  1.56.4.1  christos 		p = LIST_FIRST(&iolist);
    641  1.56.4.1  christos 	}
    642      1.14    thomas 	p = LIST_FIRST(&memlist);
    643  1.56.4.1  christos 	while (p != NULL) {
    644  1.56.4.1  christos 		q = p;
    645  1.56.4.1  christos 		LIST_REMOVE(q, link);
    646  1.56.4.1  christos 		free(p, M_WAITOK);
    647  1.56.4.1  christos 		p = LIST_FIRST(&memlist);
    648  1.56.4.1  christos 	}
    649       1.9       leo }
    650       1.9       leo 
    651       1.1       leo pcitag_t
    652      1.46       dsl pci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
    653       1.1       leo {
    654      1.48   tsutsui 
    655      1.48   tsutsui 	return (bus << 16) | (device << 11) | (function << 8);
    656      1.34   thorpej }
    657      1.34   thorpej 
    658      1.34   thorpej void
    659      1.46       dsl pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag, int *bp, int *dp, int *fp)
    660      1.34   thorpej {
    661      1.34   thorpej 
    662      1.34   thorpej 	if (bp != NULL)
    663      1.34   thorpej 		*bp = (tag >> 16) & 0xff;
    664      1.34   thorpej 	if (dp != NULL)
    665      1.34   thorpej 		*dp = (tag >> 11) & 0x1f;
    666      1.34   thorpej 	if (fp != NULL)
    667      1.34   thorpej 		*fp = (tag >> 8) & 0x7;
    668       1.1       leo }
    669       1.1       leo 
    670       1.1       leo int
    671      1.49    dyoung pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    672       1.1       leo {
    673      1.28  sommerfe 	int line = pa->pa_intrline;
    674      1.28  sommerfe 
    675      1.33       leo #if defined(_MILANHW_)
    676      1.33       leo 	/*
    677      1.33       leo 	 * On the Hades, the 'pin' info is useless.
    678      1.33       leo 	 */
    679      1.33       leo 	{
    680      1.33       leo 		int pin = pa->pa_intrpin;
    681      1.33       leo 
    682      1.33       leo 		if (pin == 0) {
    683      1.33       leo 			/* No IRQ used. */
    684      1.33       leo 			goto bad;
    685      1.33       leo 		}
    686      1.33       leo 		if (pin > PCI_INTERRUPT_PIN_MAX) {
    687      1.33       leo 			printf("pci_intr_map: bad interrupt pin %d\n", pin);
    688      1.33       leo 			goto bad;
    689      1.33       leo 		}
    690      1.33       leo 	}
    691      1.33       leo #endif /* _MILANHW_ */
    692      1.33       leo 
    693       1.9       leo 	/*
    694       1.9       leo 	 * According to the PCI-spec, 255 means `unknown' or `no connection'.
    695       1.9       leo 	 * Interpret this as 'no interrupt assigned'.
    696       1.9       leo 	 */
    697      1.33       leo 	if (line == 255)
    698      1.33       leo 		goto bad;
    699       1.9       leo 
    700       1.9       leo 	/*
    701      1.31       leo 	 * Values are pretty useless on the Hades since all interrupt
    702      1.31       leo 	 * lines for a card are tied together and hardwired to a
    703      1.31       leo 	 * specific TT-MFP I/O port.
    704      1.33       leo 	 * On the Milan, they are tied to the ICU.
    705       1.9       leo 	 */
    706      1.33       leo #if defined(_MILANHW_)
    707      1.33       leo 	if (line >= 16) {
    708      1.33       leo 		printf("pci_intr_map: bad interrupt line %d\n", line);
    709      1.33       leo 		goto bad;
    710      1.33       leo 	}
    711      1.33       leo 	if (line == 2) {
    712      1.33       leo 		printf("pci_intr_map: changed line 2 to line 9\n");
    713      1.33       leo 		line = 9;
    714      1.33       leo 	}
    715      1.33       leo 	/* Assume line == 0 means unassigned */
    716      1.33       leo 	if (line == 0)
    717      1.33       leo 		goto bad;
    718      1.33       leo #endif
    719       1.9       leo 	*ihp = line;
    720       1.9       leo 	return 0;
    721      1.33       leo 
    722      1.33       leo bad:
    723      1.33       leo 	*ihp = -1;
    724      1.33       leo 	return 1;
    725       1.1       leo }
    726       1.1       leo 
    727       1.1       leo const char *
    728  1.56.4.1  christos pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih, char *buf,
    729  1.56.4.1  christos     size_t len)
    730       1.1       leo {
    731  1.56.4.1  christos 
    732       1.9       leo 	if (ih == -1)
    733      1.36    provos 		panic("pci_intr_string: bogus handle 0x%x", ih);
    734       1.1       leo 
    735      1.54  christos 	snprintf(buf, len, "irq %d", ih);
    736      1.54  christos 	return buf;
    737       1.1       leo 
    738      1.22       cgd }
    739      1.22       cgd 
    740      1.22       cgd const struct evcnt *
    741      1.45       dsl pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
    742      1.22       cgd {
    743      1.22       cgd 
    744      1.22       cgd 	/* XXX for now, no evcnt parent reported */
    745      1.22       cgd 	return NULL;
    746       1.1       leo }
    747