Home | History | Annotate | Line # | Download | only in pci
pci_map.c revision 1.13
      1 /*	$NetBSD: pci_map.c,v 1.13 2005/02/27 00:27:33 perry 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/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: pci_map.c,v 1.13 2005/02/27 00:27:33 perry Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/device.h>
     49 
     50 #include <dev/pci/pcireg.h>
     51 #include <dev/pci/pcivar.h>
     52 
     53 static int
     54 pci_io_find(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
     55     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
     56 {
     57 	pcireg_t address, mask;
     58 	int s;
     59 
     60 	if (reg < PCI_MAPREG_START ||
     61 #if 0
     62 	    /*
     63 	     * Can't do this check; some devices have mapping registers
     64 	     * way out in left field.
     65 	     */
     66 	    reg >= PCI_MAPREG_END ||
     67 #endif
     68 	    (reg & 3))
     69 		panic("pci_io_find: bad request");
     70 
     71 	/*
     72 	 * Section 6.2.5.1, `Address Maps', tells us that:
     73 	 *
     74 	 * 1) The builtin software should have already mapped the device in a
     75 	 * reasonable way.
     76 	 *
     77 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
     78 	 * n bits of the address to 0.  As recommended, we write all 1s and see
     79 	 * what we get back.
     80 	 */
     81 	s = splhigh();
     82 	address = pci_conf_read(pc, tag, reg);
     83 	pci_conf_write(pc, tag, reg, 0xffffffff);
     84 	mask = pci_conf_read(pc, tag, reg);
     85 	pci_conf_write(pc, tag, reg, address);
     86 	splx(s);
     87 
     88 	if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
     89 		printf("pci_io_find: expected type i/o, found mem\n");
     90 		return (1);
     91 	}
     92 
     93 	if (PCI_MAPREG_IO_SIZE(mask) == 0) {
     94 		printf("pci_io_find: void region\n");
     95 		return (1);
     96 	}
     97 
     98 	if (basep != 0)
     99 		*basep = PCI_MAPREG_IO_ADDR(address);
    100 	if (sizep != 0)
    101 		*sizep = PCI_MAPREG_IO_SIZE(mask);
    102 	if (flagsp != 0)
    103 		*flagsp = 0;
    104 
    105 	return (0);
    106 }
    107 
    108 static int
    109 pci_mem_find(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
    110     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
    111 {
    112 	pcireg_t address, mask, address1 = 0, mask1 = 0xffffffff;
    113 	u_int64_t waddress, wmask;
    114 	int s, is64bit;
    115 
    116 	is64bit = (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT);
    117 
    118 	if (reg < PCI_MAPREG_START ||
    119 #if 0
    120 	    /*
    121 	     * Can't do this check; some devices have mapping registers
    122 	     * way out in left field.
    123 	     */
    124 	    reg >= PCI_MAPREG_END ||
    125 #endif
    126 	    (reg & 3))
    127 		panic("pci_mem_find: bad request");
    128 
    129 	if (is64bit && (reg + 4) >= PCI_MAPREG_END)
    130 		panic("pci_mem_find: bad 64-bit request");
    131 
    132 	/*
    133 	 * Section 6.2.5.1, `Address Maps', tells us that:
    134 	 *
    135 	 * 1) The builtin software should have already mapped the device in a
    136 	 * reasonable way.
    137 	 *
    138 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
    139 	 * n bits of the address to 0.  As recommended, we write all 1s and see
    140 	 * what we get back.
    141 	 */
    142 	s = splhigh();
    143 	address = pci_conf_read(pc, tag, reg);
    144 	pci_conf_write(pc, tag, reg, 0xffffffff);
    145 	mask = pci_conf_read(pc, tag, reg);
    146 	pci_conf_write(pc, tag, reg, address);
    147 	if (is64bit) {
    148 		address1 = pci_conf_read(pc, tag, reg + 4);
    149 		pci_conf_write(pc, tag, reg + 4, 0xffffffff);
    150 		mask1 = pci_conf_read(pc, tag, reg + 4);
    151 		pci_conf_write(pc, tag, reg + 4, address1);
    152 	}
    153 	splx(s);
    154 
    155 	if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
    156 		printf("pci_mem_find: expected type mem, found i/o\n");
    157 		return (1);
    158 	}
    159 	if (PCI_MAPREG_MEM_TYPE(address) != PCI_MAPREG_MEM_TYPE(type)) {
    160 		printf("pci_mem_find: expected mem type %08x, found %08x\n",
    161 		    PCI_MAPREG_MEM_TYPE(type),
    162 		    PCI_MAPREG_MEM_TYPE(address));
    163 		return (1);
    164 	}
    165 
    166 	waddress = (u_int64_t)address1 << 32UL | address;
    167 	wmask = (u_int64_t)mask1 << 32UL | mask;
    168 
    169 	if ((is64bit && PCI_MAPREG_MEM64_SIZE(wmask) == 0) ||
    170 	    (!is64bit && PCI_MAPREG_MEM_SIZE(mask) == 0)) {
    171 		printf("pci_mem_find: void region\n");
    172 		return (1);
    173 	}
    174 
    175 	switch (PCI_MAPREG_MEM_TYPE(address)) {
    176 	case PCI_MAPREG_MEM_TYPE_32BIT:
    177 	case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    178 		break;
    179 	case PCI_MAPREG_MEM_TYPE_64BIT:
    180 		/*
    181 		 * Handle the case of a 64-bit memory register on a
    182 		 * platform with 32-bit addressing.  Make sure that
    183 		 * the address assigned and the device's memory size
    184 		 * fit in 32 bits.  We implicitly assume that if
    185 		 * bus_addr_t is 64-bit, then so is bus_size_t.
    186 		 */
    187 		if (sizeof(u_int64_t) > sizeof(bus_addr_t) &&
    188 		    (address1 != 0 || mask1 != 0xffffffff)) {
    189 			printf("pci_mem_find: 64-bit memory map which is "
    190 			    "inaccessible on a 32-bit platform\n");
    191 			return (1);
    192 		}
    193 		break;
    194 	default:
    195 		printf("pci_mem_find: reserved mapping register type\n");
    196 		return (1);
    197 	}
    198 
    199 	if (sizeof(u_int64_t) > sizeof(bus_addr_t)) {
    200 		if (basep != 0)
    201 			*basep = PCI_MAPREG_MEM_ADDR(address);
    202 		if (sizep != 0)
    203 			*sizep = PCI_MAPREG_MEM_SIZE(mask);
    204 	} else {
    205 		if (basep != 0)
    206 			*basep = PCI_MAPREG_MEM64_ADDR(waddress);
    207 		if (sizep != 0)
    208 			*sizep = PCI_MAPREG_MEM64_SIZE(wmask);
    209 	}
    210 	if (flagsp != 0)
    211 		*flagsp = PCI_MAPREG_MEM_PREFETCHABLE(address) ?
    212 		    BUS_SPACE_MAP_PREFETCHABLE : 0;
    213 
    214 	return (0);
    215 }
    216 
    217 #define _PCI_MAPREG_TYPEBITS(reg) \
    218 	(PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO ? \
    219 	reg & PCI_MAPREG_TYPE_MASK : \
    220 	reg & (PCI_MAPREG_TYPE_MASK|PCI_MAPREG_MEM_TYPE_MASK))
    221 
    222 pcireg_t
    223 pci_mapreg_type(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    224 {
    225 
    226 	return (_PCI_MAPREG_TYPEBITS(pci_conf_read(pc, tag, reg)));
    227 }
    228 
    229 int
    230 pci_mapreg_probe(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *typep)
    231 {
    232 	pcireg_t address, mask;
    233 	int s;
    234 
    235 	s = splhigh();
    236 	address = pci_conf_read(pc, tag, reg);
    237 	pci_conf_write(pc, tag, reg, 0xffffffff);
    238 	mask = pci_conf_read(pc, tag, reg);
    239 	pci_conf_write(pc, tag, reg, address);
    240 	splx(s);
    241 
    242 	if (mask == 0) /* unimplemented mapping register */
    243 		return (0);
    244 
    245 	if (typep)
    246 		*typep = _PCI_MAPREG_TYPEBITS(address);
    247 	return (1);
    248 }
    249 
    250 int
    251 pci_mapreg_info(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
    252     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
    253 {
    254 
    255 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO)
    256 		return (pci_io_find(pc, tag, reg, type, basep, sizep,
    257 		    flagsp));
    258 	else
    259 		return (pci_mem_find(pc, tag, reg, type, basep, sizep,
    260 		    flagsp));
    261 }
    262 
    263 int
    264 pci_mapreg_map(struct pci_attach_args *pa, int reg, pcireg_t type,
    265     int busflags, bus_space_tag_t *tagp, bus_space_handle_t *handlep,
    266     bus_addr_t *basep, bus_size_t *sizep)
    267 {
    268 	bus_space_tag_t tag;
    269 	bus_space_handle_t handle;
    270 	bus_addr_t base;
    271 	bus_size_t size;
    272 	int flags;
    273 
    274 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
    275 		if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0)
    276 			return (1);
    277 		if (pci_io_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
    278 		    &size, &flags))
    279 			return (1);
    280 		tag = pa->pa_iot;
    281 	} else {
    282 		if ((pa->pa_flags & PCI_FLAGS_MEM_ENABLED) == 0)
    283 			return (1);
    284 		if (pci_mem_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
    285 		    &size, &flags))
    286 			return (1);
    287 		tag = pa->pa_memt;
    288 	}
    289 
    290 	if (bus_space_map(tag, base, size, busflags | flags, &handle))
    291 		return (1);
    292 
    293 	if (tagp != 0)
    294 		*tagp = tag;
    295 	if (handlep != 0)
    296 		*handlep = handle;
    297 	if (basep != 0)
    298 		*basep = base;
    299 	if (sizep != 0)
    300 		*sizep = size;
    301 
    302 	return (0);
    303 }
    304