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