Home | History | Annotate | Line # | Download | only in pci
pci_map.c revision 1.26
      1 /*	$NetBSD: pci_map.c,v 1.26 2011/02/27 17:31:08 jruoho 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * PCI device mapping.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: pci_map.c,v 1.26 2011/02/27 17:31:08 jruoho Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/device.h>
     42 
     43 #include <dev/pci/pcireg.h>
     44 #include <dev/pci/pcivar.h>
     45 
     46 static int
     47 pci_io_find(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
     48     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
     49 {
     50 	pcireg_t address, mask;
     51 	int s;
     52 
     53 	if (reg < PCI_MAPREG_START ||
     54 #if 0
     55 	    /*
     56 	     * Can't do this check; some devices have mapping registers
     57 	     * way out in left field.
     58 	     */
     59 	    reg >= PCI_MAPREG_END ||
     60 #endif
     61 	    (reg & 3))
     62 		panic("pci_io_find: bad request");
     63 
     64 	/*
     65 	 * Section 6.2.5.1, `Address Maps', tells us that:
     66 	 *
     67 	 * 1) The builtin software should have already mapped the device in a
     68 	 * reasonable way.
     69 	 *
     70 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
     71 	 * n bits of the address to 0.  As recommended, we write all 1s and see
     72 	 * what we get back.
     73 	 */
     74 	s = splhigh();
     75 	address = pci_conf_read(pc, tag, reg);
     76 	pci_conf_write(pc, tag, reg, 0xffffffff);
     77 	mask = pci_conf_read(pc, tag, reg);
     78 	pci_conf_write(pc, tag, reg, address);
     79 	splx(s);
     80 
     81 	if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
     82 		aprint_debug("pci_io_find: expected type i/o, found mem\n");
     83 		return 1;
     84 	}
     85 
     86 	if (PCI_MAPREG_IO_SIZE(mask) == 0) {
     87 		aprint_debug("pci_io_find: void region\n");
     88 		return 1;
     89 	}
     90 
     91 	if (basep != NULL)
     92 		*basep = PCI_MAPREG_IO_ADDR(address);
     93 	if (sizep != NULL)
     94 		*sizep = PCI_MAPREG_IO_SIZE(mask);
     95 	if (flagsp != NULL)
     96 		*flagsp = 0;
     97 
     98 	return 0;
     99 }
    100 
    101 static int
    102 pci_mem_find(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
    103     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
    104 {
    105 	pcireg_t address, mask, address1 = 0, mask1 = 0xffffffff;
    106 	u_int64_t waddress, wmask;
    107 	int s, is64bit, isrom;
    108 
    109 	is64bit = (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT);
    110 	isrom = (reg == PCI_MAPREG_ROM);
    111 
    112 	if ((!isrom) && (reg < PCI_MAPREG_START ||
    113 #if 0
    114 	    /*
    115 	     * Can't do this check; some devices have mapping registers
    116 	     * way out in left field.
    117 	     */
    118 	    reg >= PCI_MAPREG_END ||
    119 #endif
    120 	    (reg & 3)))
    121 		panic("pci_mem_find: bad request");
    122 
    123 	if (is64bit && (reg + 4) >= PCI_MAPREG_END)
    124 		panic("pci_mem_find: bad 64-bit request");
    125 
    126 	/*
    127 	 * Section 6.2.5.1, `Address Maps', tells us that:
    128 	 *
    129 	 * 1) The builtin software should have already mapped the device in a
    130 	 * reasonable way.
    131 	 *
    132 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
    133 	 * n bits of the address to 0.  As recommended, we write all 1s and see
    134 	 * what we get back.
    135 	 */
    136 	s = splhigh();
    137 	address = pci_conf_read(pc, tag, reg);
    138 	pci_conf_write(pc, tag, reg, 0xffffffff);
    139 	mask = pci_conf_read(pc, tag, reg);
    140 	pci_conf_write(pc, tag, reg, address);
    141 	if (is64bit) {
    142 		address1 = pci_conf_read(pc, tag, reg + 4);
    143 		pci_conf_write(pc, tag, reg + 4, 0xffffffff);
    144 		mask1 = pci_conf_read(pc, tag, reg + 4);
    145 		pci_conf_write(pc, tag, reg + 4, address1);
    146 	}
    147 	splx(s);
    148 
    149 	if (!isrom) {
    150 		/*
    151 		 * roms should have an enable bit instead of a memory
    152 		 * type decoder bit.  For normal BARs, make sure that
    153 		 * the address decoder type matches what we asked for.
    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 		/* XXX Allow 64bit bars for 32bit requests.*/
    160 		if (PCI_MAPREG_MEM_TYPE(address) !=
    161 		    PCI_MAPREG_MEM_TYPE(type) &&
    162 		    PCI_MAPREG_MEM_TYPE(address) !=
    163 		    PCI_MAPREG_MEM_TYPE_64BIT) {
    164 			printf("pci_mem_find: "
    165 			    "expected mem type %08x, found %08x\n",
    166 			    PCI_MAPREG_MEM_TYPE(type),
    167 			    PCI_MAPREG_MEM_TYPE(address));
    168 			return 1;
    169 		}
    170 	}
    171 
    172 	waddress = (u_int64_t)address1 << 32UL | address;
    173 	wmask = (u_int64_t)mask1 << 32UL | mask;
    174 
    175 	if ((is64bit && PCI_MAPREG_MEM64_SIZE(wmask) == 0) ||
    176 	    (!is64bit && PCI_MAPREG_MEM_SIZE(mask) == 0)) {
    177 		return 1;
    178 	}
    179 
    180 	switch (PCI_MAPREG_MEM_TYPE(address)) {
    181 	case PCI_MAPREG_MEM_TYPE_32BIT:
    182 	case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    183 		break;
    184 	case PCI_MAPREG_MEM_TYPE_64BIT:
    185 		/*
    186 		 * Handle the case of a 64-bit memory register on a
    187 		 * platform with 32-bit addressing.  Make sure that
    188 		 * the address assigned and the device's memory size
    189 		 * fit in 32 bits.  We implicitly assume that if
    190 		 * bus_addr_t is 64-bit, then so is bus_size_t.
    191 		 */
    192 		if (sizeof(u_int64_t) > sizeof(bus_addr_t) &&
    193 		    (address1 != 0 || mask1 != 0xffffffff)) {
    194 			printf("pci_mem_find: 64-bit memory map which is "
    195 			    "inaccessible on a 32-bit platform\n");
    196 			return 1;
    197 		}
    198 		break;
    199 	default:
    200 		printf("pci_mem_find: reserved mapping register type\n");
    201 		return 1;
    202 	}
    203 
    204 	if (sizeof(u_int64_t) > sizeof(bus_addr_t)) {
    205 		if (basep != NULL)
    206 			*basep = PCI_MAPREG_MEM_ADDR(address);
    207 		if (sizep != NULL)
    208 			*sizep = PCI_MAPREG_MEM_SIZE(mask);
    209 	} else {
    210 		if (basep != NULL)
    211 			*basep = PCI_MAPREG_MEM64_ADDR(waddress);
    212 		if (sizep != NULL)
    213 			*sizep = PCI_MAPREG_MEM64_SIZE(wmask);
    214 	}
    215 	if (flagsp != NULL)
    216 		*flagsp = (isrom || PCI_MAPREG_MEM_PREFETCHABLE(address)) ?
    217 		    BUS_SPACE_MAP_PREFETCHABLE : 0;
    218 
    219 	return 0;
    220 }
    221 
    222 #define _PCI_MAPREG_TYPEBITS(reg) \
    223 	(PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO ? \
    224 	reg & PCI_MAPREG_TYPE_MASK : \
    225 	reg & (PCI_MAPREG_TYPE_MASK|PCI_MAPREG_MEM_TYPE_MASK))
    226 
    227 pcireg_t
    228 pci_mapreg_type(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    229 {
    230 
    231 	return _PCI_MAPREG_TYPEBITS(pci_conf_read(pc, tag, reg));
    232 }
    233 
    234 int
    235 pci_mapreg_probe(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *typep)
    236 {
    237 	pcireg_t address, mask;
    238 	int s;
    239 
    240 	s = splhigh();
    241 	address = pci_conf_read(pc, tag, reg);
    242 	pci_conf_write(pc, tag, reg, 0xffffffff);
    243 	mask = pci_conf_read(pc, tag, reg);
    244 	pci_conf_write(pc, tag, reg, address);
    245 	splx(s);
    246 
    247 	if (mask == 0) /* unimplemented mapping register */
    248 		return 0;
    249 
    250 	if (typep != NULL)
    251 		*typep = _PCI_MAPREG_TYPEBITS(address);
    252 	return 1;
    253 }
    254 
    255 int
    256 pci_mapreg_info(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
    257     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
    258 {
    259 
    260 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO)
    261 		return pci_io_find(pc, tag, reg, type, basep, sizep,
    262 		    flagsp);
    263 	else
    264 		return pci_mem_find(pc, tag, reg, type, basep, sizep,
    265 		    flagsp);
    266 }
    267 
    268 int
    269 pci_mapreg_map(struct pci_attach_args *pa, int reg, pcireg_t type,
    270     int busflags, bus_space_tag_t *tagp, bus_space_handle_t *handlep,
    271     bus_addr_t *basep, bus_size_t *sizep)
    272 {
    273 	return pci_mapreg_submap(pa, reg, type, busflags, 0, 0, tagp,
    274 	    handlep, basep, sizep);
    275 }
    276 
    277 int
    278 pci_mapreg_submap(struct pci_attach_args *pa, int reg, pcireg_t type,
    279     int busflags, bus_size_t maxsize, bus_size_t offset, bus_space_tag_t *tagp,
    280 	bus_space_handle_t *handlep, bus_addr_t *basep, bus_size_t *sizep)
    281 {
    282 	bus_space_tag_t tag;
    283 	bus_space_handle_t handle;
    284 	bus_addr_t base;
    285 	bus_size_t size;
    286 	int flags;
    287 
    288 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
    289 		if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0)
    290 			return 1;
    291 		if (pci_io_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
    292 		    &size, &flags))
    293 			return 1;
    294 		tag = pa->pa_iot;
    295 	} else {
    296 		if ((pa->pa_flags & PCI_FLAGS_MEM_ENABLED) == 0)
    297 			return 1;
    298 		if (pci_mem_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
    299 		    &size, &flags))
    300 			return 1;
    301 		tag = pa->pa_memt;
    302 	}
    303 
    304 	if (reg == PCI_MAPREG_ROM) {
    305 		pcireg_t 	mask;
    306 		int		s;
    307 		/* we have to enable the ROM address decoder... */
    308 		s = splhigh();
    309 		mask = pci_conf_read(pa->pa_pc, pa->pa_tag, reg);
    310 		mask |= PCI_MAPREG_ROM_ENABLE;
    311 		pci_conf_write(pa->pa_pc, pa->pa_tag, reg, mask);
    312 		splx(s);
    313 	}
    314 
    315 	/* If we're called with maxsize/offset of 0, behave like
    316 	 * pci_mapreg_map.
    317 	 */
    318 
    319 	maxsize = (maxsize && offset) ? maxsize : size;
    320 	base += offset;
    321 
    322 	if ((maxsize < size && offset + maxsize <= size) || offset != 0)
    323 		return 1;
    324 
    325 	if (bus_space_map(tag, base, maxsize, busflags | flags, &handle))
    326 		return 1;
    327 
    328 	if (tagp != NULL)
    329 		*tagp = tag;
    330 	if (handlep != NULL)
    331 		*handlep = handle;
    332 	if (basep != NULL)
    333 		*basep = base;
    334 	if (sizep != NULL)
    335 		*sizep = maxsize;
    336 
    337 	return 0;
    338 }
    339 
    340 int
    341 pci_find_rom(struct pci_attach_args *pa, bus_space_tag_t bst,
    342     bus_space_handle_t bsh, int type, bus_space_handle_t *romh, bus_size_t *sz)
    343 {
    344 	bus_size_t	romsz, offset = 0, imagesz;
    345 	uint16_t	ptr;
    346 	int		done = 0;
    347 
    348 	if (pci_mem_find(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM,
    349 	    PCI_MAPREG_TYPE_ROM, NULL, &romsz, NULL))
    350 		return 1;
    351 
    352 	/*
    353 	 * no upper bound check; i cannot imagine a 4GB ROM, but
    354 	 * it appears the spec would allow it!
    355 	 */
    356 	if (romsz < 1024)
    357 		return 1;
    358 
    359 	while (offset < romsz && !done){
    360 		struct pci_rom_header	hdr;
    361 		struct pci_rom		rom;
    362 
    363 		hdr.romh_magic = bus_space_read_2(bst, bsh,
    364 		    offset + offsetof (struct pci_rom_header, romh_magic));
    365 		hdr.romh_data_ptr = bus_space_read_2(bst, bsh,
    366 		    offset + offsetof (struct pci_rom_header, romh_data_ptr));
    367 
    368 		/* no warning: quite possibly ROM is simply not populated */
    369 		if (hdr.romh_magic != PCI_ROM_HEADER_MAGIC)
    370 			return 1;
    371 
    372 		ptr = offset + hdr.romh_data_ptr;
    373 
    374 		if (ptr > romsz) {
    375 			printf("pci_find_rom: rom data ptr out of range\n");
    376 			return 1;
    377 		}
    378 
    379 		rom.rom_signature = bus_space_read_4(bst, bsh, ptr);
    380 		rom.rom_vendor = bus_space_read_2(bst, bsh, ptr +
    381 		    offsetof(struct pci_rom, rom_vendor));
    382 		rom.rom_product = bus_space_read_2(bst, bsh, ptr +
    383 		    offsetof(struct pci_rom, rom_product));
    384 		rom.rom_class = bus_space_read_1(bst, bsh,
    385 		    ptr + offsetof (struct pci_rom, rom_class));
    386 		rom.rom_subclass = bus_space_read_1(bst, bsh,
    387 		    ptr + offsetof (struct pci_rom, rom_subclass));
    388 		rom.rom_interface = bus_space_read_1(bst, bsh,
    389 		    ptr + offsetof (struct pci_rom, rom_interface));
    390 		rom.rom_len = bus_space_read_2(bst, bsh,
    391 		    ptr + offsetof (struct pci_rom, rom_len));
    392 		rom.rom_code_type = bus_space_read_1(bst, bsh,
    393 		    ptr + offsetof (struct pci_rom, rom_code_type));
    394 		rom.rom_indicator = bus_space_read_1(bst, bsh,
    395 		    ptr + offsetof (struct pci_rom, rom_indicator));
    396 
    397 		if (rom.rom_signature != PCI_ROM_SIGNATURE) {
    398 			printf("pci_find_rom: bad rom data signature\n");
    399 			return 1;
    400 		}
    401 
    402 		imagesz = rom.rom_len * 512;
    403 
    404 		if ((rom.rom_vendor == PCI_VENDOR(pa->pa_id)) &&
    405 		    (rom.rom_product == PCI_PRODUCT(pa->pa_id)) &&
    406 		    (rom.rom_class == PCI_CLASS(pa->pa_class)) &&
    407 		    (rom.rom_subclass == PCI_SUBCLASS(pa->pa_class)) &&
    408 		    (rom.rom_interface == PCI_INTERFACE(pa->pa_class)) &&
    409 		    (rom.rom_code_type == type)) {
    410 			*sz = imagesz;
    411 			bus_space_subregion(bst, bsh, offset, imagesz, romh);
    412 			return 0;
    413 		}
    414 
    415 		/* last image check */
    416 		if (rom.rom_indicator & PCI_ROM_INDICATOR_LAST)
    417 			return 1;
    418 
    419 		/* offset by size */
    420 		offset += imagesz;
    421 	}
    422 	return 1;
    423 }
    424