Home | History | Annotate | Line # | Download | only in pci
pci_map.c revision 1.8
      1  1.8   thorpej /*	$NetBSD: pci_map.c,v 1.8 2000/06/28 17:32:48 thorpej Exp $	*/
      2  1.1   mycroft 
      3  1.5   mycroft /*-
      4  1.7   thorpej  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5  1.5   mycroft  * All rights reserved.
      6  1.5   mycroft  *
      7  1.5   mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8  1.7   thorpej  * by Charles M. Hannum; by William R. Studenmund; by Jason R. Thorpe.
      9  1.1   mycroft  *
     10  1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     11  1.1   mycroft  * modification, are permitted provided that the following conditions
     12  1.1   mycroft  * are met:
     13  1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     14  1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     15  1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17  1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     18  1.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     19  1.1   mycroft  *    must display the following acknowledgement:
     20  1.5   mycroft  *        This product includes software developed by the NetBSD
     21  1.5   mycroft  *        Foundation, Inc. and its contributors.
     22  1.5   mycroft  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.5   mycroft  *    contributors may be used to endorse or promote products derived
     24  1.5   mycroft  *    from this software without specific prior written permission.
     25  1.1   mycroft  *
     26  1.5   mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.5   mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.5   mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.5   mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.5   mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.5   mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.5   mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.5   mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.5   mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.5   mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.5   mycroft  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1   mycroft  */
     38  1.1   mycroft 
     39  1.1   mycroft /*
     40  1.1   mycroft  * PCI device mapping.
     41  1.1   mycroft  */
     42  1.1   mycroft 
     43  1.1   mycroft #include <sys/param.h>
     44  1.1   mycroft #include <sys/systm.h>
     45  1.1   mycroft #include <sys/device.h>
     46  1.1   mycroft 
     47  1.1   mycroft #include <dev/pci/pcireg.h>
     48  1.1   mycroft #include <dev/pci/pcivar.h>
     49  1.1   mycroft 
     50  1.1   mycroft static int pci_io_find __P((pci_chipset_tag_t, pcitag_t, int, pcireg_t,
     51  1.1   mycroft     bus_addr_t *, bus_size_t *, int *));
     52  1.1   mycroft static int pci_mem_find __P((pci_chipset_tag_t, pcitag_t, int, pcireg_t,
     53  1.1   mycroft     bus_addr_t *, bus_size_t *, int *));
     54  1.1   mycroft 
     55  1.1   mycroft static int
     56  1.1   mycroft pci_io_find(pc, tag, reg, type, basep, sizep, flagsp)
     57  1.1   mycroft 	pci_chipset_tag_t pc;
     58  1.1   mycroft 	pcitag_t tag;
     59  1.1   mycroft 	int reg;
     60  1.1   mycroft 	pcireg_t type;
     61  1.1   mycroft 	bus_addr_t *basep;
     62  1.1   mycroft 	bus_size_t *sizep;
     63  1.1   mycroft 	int *flagsp;
     64  1.1   mycroft {
     65  1.1   mycroft 	pcireg_t address, mask;
     66  1.1   mycroft 	int s;
     67  1.1   mycroft 
     68  1.8   thorpej 	if (reg < PCI_MAPREG_START ||
     69  1.8   thorpej #if 0
     70  1.8   thorpej 	    /*
     71  1.8   thorpej 	     * Can't do this check; some devices have mapping registers
     72  1.8   thorpej 	     * way out in left field.
     73  1.8   thorpej 	     */
     74  1.8   thorpej 	    reg >= PCI_MAPREG_END ||
     75  1.8   thorpej #endif
     76  1.8   thorpej 	    (reg & 3))
     77  1.1   mycroft 		panic("pci_io_find: bad request");
     78  1.1   mycroft 
     79  1.1   mycroft 	/*
     80  1.1   mycroft 	 * Section 6.2.5.1, `Address Maps', tells us that:
     81  1.1   mycroft 	 *
     82  1.1   mycroft 	 * 1) The builtin software should have already mapped the device in a
     83  1.1   mycroft 	 * reasonable way.
     84  1.1   mycroft 	 *
     85  1.1   mycroft 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
     86  1.1   mycroft 	 * n bits of the address to 0.  As recommended, we write all 1s and see
     87  1.1   mycroft 	 * what we get back.
     88  1.1   mycroft 	 */
     89  1.1   mycroft 	s = splhigh();
     90  1.1   mycroft 	address = pci_conf_read(pc, tag, reg);
     91  1.1   mycroft 	pci_conf_write(pc, tag, reg, 0xffffffff);
     92  1.1   mycroft 	mask = pci_conf_read(pc, tag, reg);
     93  1.1   mycroft 	pci_conf_write(pc, tag, reg, address);
     94  1.1   mycroft 	splx(s);
     95  1.1   mycroft 
     96  1.1   mycroft 	if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
     97  1.1   mycroft 		printf("pci_io_find: expected type i/o, found mem\n");
     98  1.1   mycroft 		return (1);
     99  1.1   mycroft 	}
    100  1.1   mycroft 
    101  1.2   mycroft 	if (PCI_MAPREG_IO_SIZE(mask) == 0) {
    102  1.2   mycroft 		printf("pci_io_find: void region\n");
    103  1.2   mycroft 		return (1);
    104  1.2   mycroft 	}
    105  1.2   mycroft 
    106  1.1   mycroft 	if (basep != 0)
    107  1.1   mycroft 		*basep = PCI_MAPREG_IO_ADDR(address);
    108  1.1   mycroft 	if (sizep != 0)
    109  1.1   mycroft 		*sizep = PCI_MAPREG_IO_SIZE(mask);
    110  1.1   mycroft 	if (flagsp != 0)
    111  1.1   mycroft 		*flagsp = 0;
    112  1.1   mycroft 
    113  1.1   mycroft 	return (0);
    114  1.1   mycroft }
    115  1.1   mycroft 
    116  1.1   mycroft static int
    117  1.1   mycroft pci_mem_find(pc, tag, reg, type, basep, sizep, flagsp)
    118  1.1   mycroft 	pci_chipset_tag_t pc;
    119  1.1   mycroft 	pcitag_t tag;
    120  1.1   mycroft 	int reg;
    121  1.1   mycroft 	pcireg_t type;
    122  1.1   mycroft 	bus_addr_t *basep;
    123  1.1   mycroft 	bus_size_t *sizep;
    124  1.1   mycroft 	int *flagsp;
    125  1.1   mycroft {
    126  1.7   thorpej 	pcireg_t address, mask, address1 = 0, mask1 = 0xffffffff;
    127  1.7   thorpej 	u_int64_t waddress, wmask;
    128  1.7   thorpej 	int s, is64bit;
    129  1.7   thorpej 
    130  1.7   thorpej 	is64bit = (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT);
    131  1.1   mycroft 
    132  1.8   thorpej 	if (reg < PCI_MAPREG_START ||
    133  1.8   thorpej #if 0
    134  1.8   thorpej 	    /*
    135  1.8   thorpej 	     * Can't do this check; some devices have mapping registers
    136  1.8   thorpej 	     * way out in left field.
    137  1.8   thorpej 	     */
    138  1.8   thorpej 	    reg >= PCI_MAPREG_END ||
    139  1.8   thorpej #endif
    140  1.8   thorpej 	    (reg & 3))
    141  1.7   thorpej 		panic("pci_mem_find: bad request");
    142  1.7   thorpej 
    143  1.7   thorpej 	if (is64bit && (reg + 4) >= PCI_MAPREG_END)
    144  1.7   thorpej 		panic("pci_mem_find: bad 64-bit request");
    145  1.1   mycroft 
    146  1.1   mycroft 	/*
    147  1.1   mycroft 	 * Section 6.2.5.1, `Address Maps', tells us that:
    148  1.1   mycroft 	 *
    149  1.1   mycroft 	 * 1) The builtin software should have already mapped the device in a
    150  1.1   mycroft 	 * reasonable way.
    151  1.1   mycroft 	 *
    152  1.1   mycroft 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
    153  1.1   mycroft 	 * n bits of the address to 0.  As recommended, we write all 1s and see
    154  1.1   mycroft 	 * what we get back.
    155  1.1   mycroft 	 */
    156  1.1   mycroft 	s = splhigh();
    157  1.1   mycroft 	address = pci_conf_read(pc, tag, reg);
    158  1.1   mycroft 	pci_conf_write(pc, tag, reg, 0xffffffff);
    159  1.1   mycroft 	mask = pci_conf_read(pc, tag, reg);
    160  1.1   mycroft 	pci_conf_write(pc, tag, reg, address);
    161  1.7   thorpej 	if (is64bit) {
    162  1.7   thorpej 		address1 = pci_conf_read(pc, tag, reg + 4);
    163  1.7   thorpej 		pci_conf_write(pc, tag, reg + 4, 0xffffffff);
    164  1.7   thorpej 		mask1 = pci_conf_read(pc, tag, reg + 4);
    165  1.7   thorpej 		pci_conf_write(pc, tag, reg + 4, address1);
    166  1.7   thorpej 	}
    167  1.1   mycroft 	splx(s);
    168  1.1   mycroft 
    169  1.1   mycroft 	if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
    170  1.1   mycroft 		printf("pci_mem_find: expected type mem, found i/o\n");
    171  1.1   mycroft 		return (1);
    172  1.1   mycroft 	}
    173  1.1   mycroft 	if (PCI_MAPREG_MEM_TYPE(address) != PCI_MAPREG_MEM_TYPE(type)) {
    174  1.1   mycroft 		printf("pci_mem_find: expected mem type %08x, found %08x\n",
    175  1.1   mycroft 		    PCI_MAPREG_MEM_TYPE(type),
    176  1.1   mycroft 		    PCI_MAPREG_MEM_TYPE(address));
    177  1.2   mycroft 		return (1);
    178  1.2   mycroft 	}
    179  1.2   mycroft 
    180  1.7   thorpej 	waddress = (u_int64_t)address1 << 32UL | address;
    181  1.7   thorpej 	wmask = (u_int64_t)mask1 << 32UL | mask;
    182  1.7   thorpej 
    183  1.7   thorpej 	if (PCI_MAPREG_MEM64_SIZE(wmask) == 0) {
    184  1.2   mycroft 		printf("pci_mem_find: void region\n");
    185  1.1   mycroft 		return (1);
    186  1.1   mycroft 	}
    187  1.1   mycroft 
    188  1.1   mycroft 	switch (PCI_MAPREG_MEM_TYPE(address)) {
    189  1.1   mycroft 	case PCI_MAPREG_MEM_TYPE_32BIT:
    190  1.1   mycroft 	case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    191  1.1   mycroft 		break;
    192  1.1   mycroft 	case PCI_MAPREG_MEM_TYPE_64BIT:
    193  1.7   thorpej 		/*
    194  1.7   thorpej 		 * Handle the case of a 64-bit memory register on a
    195  1.7   thorpej 		 * platform with 32-bit addressing.  Make sure that
    196  1.7   thorpej 		 * the address assigned and the device's memory size
    197  1.7   thorpej 		 * fit in 32 bits.  We implicitly assume that if
    198  1.7   thorpej 		 * bus_addr_t is 64-bit, then so is bus_size_t.
    199  1.7   thorpej 		 */
    200  1.7   thorpej 		if (sizeof(u_int64_t) > sizeof(bus_addr_t) &&
    201  1.7   thorpej 		    (address1 != 0 || mask1 != 0xffffffff)) {
    202  1.7   thorpej 			printf("pci_mem_find: 64-bit memory map which is "
    203  1.7   thorpej 			    "inaccessible on a 32-bit platform\n");
    204  1.7   thorpej 			return (1);
    205  1.7   thorpej 		}
    206  1.7   thorpej 		break;
    207  1.1   mycroft 	default:
    208  1.1   mycroft 		printf("pci_mem_find: reserved mapping register type\n");
    209  1.1   mycroft 		return (1);
    210  1.1   mycroft 	}
    211  1.1   mycroft 
    212  1.7   thorpej 	if (sizeof(u_int64_t) > sizeof(bus_addr_t)) {
    213  1.7   thorpej 		if (basep != 0)
    214  1.7   thorpej 			*basep = PCI_MAPREG_MEM_ADDR(address);
    215  1.7   thorpej 		if (sizep != 0)
    216  1.7   thorpej 			*sizep = PCI_MAPREG_MEM_SIZE(mask);
    217  1.7   thorpej 	} else {
    218  1.7   thorpej 		if (basep != 0)
    219  1.7   thorpej 			*basep = PCI_MAPREG_MEM64_ADDR(waddress);
    220  1.7   thorpej 		if (sizep != 0)
    221  1.7   thorpej 			*sizep = PCI_MAPREG_MEM64_SIZE(wmask);
    222  1.7   thorpej 	}
    223  1.1   mycroft 	if (flagsp != 0)
    224  1.6  drochner 		*flagsp = PCI_MAPREG_MEM_PREFETCHABLE(address) ?
    225  1.6  drochner 		    BUS_SPACE_MAP_PREFETCHABLE : 0;
    226  1.1   mycroft 
    227  1.1   mycroft 	return (0);
    228  1.7   thorpej }
    229  1.7   thorpej 
    230  1.7   thorpej pcireg_t
    231  1.7   thorpej pci_mapreg_type(pc, tag, reg)
    232  1.7   thorpej 	pci_chipset_tag_t pc;
    233  1.7   thorpej 	pcitag_t tag;
    234  1.7   thorpej 	int reg;
    235  1.7   thorpej {
    236  1.7   thorpej 	pcireg_t rv;
    237  1.7   thorpej 
    238  1.7   thorpej 	rv = pci_conf_read(pc, tag, reg);
    239  1.7   thorpej 	if (PCI_MAPREG_TYPE(rv) == PCI_MAPREG_TYPE_IO)
    240  1.7   thorpej 		rv &= PCI_MAPREG_TYPE_MASK;
    241  1.7   thorpej 	else
    242  1.7   thorpej 		rv &= PCI_MAPREG_TYPE_MASK|PCI_MAPREG_MEM_TYPE_MASK;
    243  1.7   thorpej 	return (rv);
    244  1.1   mycroft }
    245  1.1   mycroft 
    246  1.1   mycroft int
    247  1.4   thorpej pci_mapreg_info(pc, tag, reg, type, basep, sizep, flagsp)
    248  1.4   thorpej 	pci_chipset_tag_t pc;
    249  1.4   thorpej 	pcitag_t tag;
    250  1.1   mycroft 	int reg;
    251  1.1   mycroft 	pcireg_t type;
    252  1.1   mycroft 	bus_addr_t *basep;
    253  1.1   mycroft 	bus_size_t *sizep;
    254  1.4   thorpej 	int *flagsp;
    255  1.1   mycroft {
    256  1.1   mycroft 
    257  1.1   mycroft 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO)
    258  1.4   thorpej 		return (pci_io_find(pc, tag, reg, type, basep, sizep,
    259  1.4   thorpej 		    flagsp));
    260  1.1   mycroft 	else
    261  1.4   thorpej 		return (pci_mem_find(pc, tag, reg, type, basep, sizep,
    262  1.4   thorpej 		    flagsp));
    263  1.1   mycroft }
    264  1.1   mycroft 
    265  1.1   mycroft int
    266  1.1   mycroft pci_mapreg_map(pa, reg, type, busflags, tagp, handlep, basep, sizep)
    267  1.1   mycroft 	struct pci_attach_args *pa;
    268  1.1   mycroft 	int reg, busflags;
    269  1.1   mycroft 	pcireg_t type;
    270  1.1   mycroft 	bus_space_tag_t *tagp;
    271  1.1   mycroft 	bus_space_handle_t *handlep;
    272  1.1   mycroft 	bus_addr_t *basep;
    273  1.1   mycroft 	bus_size_t *sizep;
    274  1.1   mycroft {
    275  1.1   mycroft 	bus_space_tag_t tag;
    276  1.1   mycroft 	bus_space_handle_t handle;
    277  1.1   mycroft 	bus_addr_t base;
    278  1.1   mycroft 	bus_size_t size;
    279  1.1   mycroft 	int flags;
    280  1.1   mycroft 
    281  1.1   mycroft 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
    282  1.1   mycroft 		if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0)
    283  1.1   mycroft 			return (1);
    284  1.1   mycroft 		if (pci_io_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
    285  1.1   mycroft 		    &size, &flags))
    286  1.1   mycroft 			return (1);
    287  1.1   mycroft 		tag = pa->pa_iot;
    288  1.1   mycroft 	} else {
    289  1.1   mycroft 		if ((pa->pa_flags & PCI_FLAGS_MEM_ENABLED) == 0)
    290  1.1   mycroft 			return (1);
    291  1.1   mycroft 		if (pci_mem_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
    292  1.1   mycroft 		    &size, &flags))
    293  1.1   mycroft 			return (1);
    294  1.1   mycroft 		tag = pa->pa_memt;
    295  1.1   mycroft 	}
    296  1.1   mycroft 
    297  1.1   mycroft 	if (bus_space_map(tag, base, size, busflags | flags, &handle))
    298  1.1   mycroft 		return (1);
    299  1.1   mycroft 
    300  1.1   mycroft 	if (tagp != 0)
    301  1.1   mycroft 		*tagp = tag;
    302  1.1   mycroft 	if (handlep != 0)
    303  1.1   mycroft 		*handlep = handle;
    304  1.1   mycroft 	if (basep != 0)
    305  1.1   mycroft 		*basep = base;
    306  1.1   mycroft 	if (sizep != 0)
    307  1.1   mycroft 		*sizep = size;
    308  1.1   mycroft 
    309  1.1   mycroft 	return (0);
    310  1.1   mycroft }
    311