1/* $NetBSD: pci_map.c,v 1.28 2011/04/04 20:37:56 dyoung 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.28 2011/04/04 20:37:56 dyoung 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 46static int pci_mapreg_submap(const struct pci_attach_args *, int, pcireg_t, int, 47 bus_size_t, bus_size_t, bus_space_tag_t *, bus_space_handle_t *, 48 bus_addr_t *, bus_size_t *); 49 50static int 51pci_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 aprint_debug("pci_io_find: expected type i/o, found mem\n"); 87 return 1; 88 } 89 90 if (PCI_MAPREG_IO_SIZE(mask) == 0) { 91 aprint_debug("pci_io_find: void region\n"); 92 return 1; 93 } 94 95 if (basep != NULL) 96 *basep = PCI_MAPREG_IO_ADDR(address); 97 if (sizep != NULL) 98 *sizep = PCI_MAPREG_IO_SIZE(mask); 99 if (flagsp != NULL) 100 *flagsp = 0; 101 102 return 0; 103} 104 105static int 106pci_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, isrom; 112 113 is64bit = (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT); 114 isrom = (reg == PCI_MAPREG_ROM); 115 116 if ((!isrom) && (reg < PCI_MAPREG_START || 117#if 0 118 /* 119 * Can't do this check; some devices have mapping registers 120 * way out in left field. 121 */ 122 reg >= PCI_MAPREG_END || 123#endif 124 (reg & 3))) 125 panic("pci_mem_find: bad request"); 126 127 if (is64bit && (reg + 4) >= PCI_MAPREG_END) 128 panic("pci_mem_find: bad 64-bit request"); 129 130 /* 131 * Section 6.2.5.1, `Address Maps', tells us that: 132 * 133 * 1) The builtin software should have already mapped the device in a 134 * reasonable way. 135 * 136 * 2) A device which wants 2^n bytes of memory will hardwire the bottom 137 * n bits of the address to 0. As recommended, we write all 1s and see 138 * what we get back. 139 */ 140 s = splhigh(); 141 address = pci_conf_read(pc, tag, reg); 142 pci_conf_write(pc, tag, reg, 0xffffffff); 143 mask = pci_conf_read(pc, tag, reg); 144 pci_conf_write(pc, tag, reg, address); 145 if (is64bit) { 146 address1 = pci_conf_read(pc, tag, reg + 4); 147 pci_conf_write(pc, tag, reg + 4, 0xffffffff); 148 mask1 = pci_conf_read(pc, tag, reg + 4); 149 pci_conf_write(pc, tag, reg + 4, address1); 150 } 151 splx(s); 152 153 if (!isrom) { 154 /* 155 * roms should have an enable bit instead of a memory 156 * type decoder bit. For normal BARs, make sure that 157 * the address decoder type matches what we asked for. 158 */ 159 if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) { 160 printf("pci_mem_find: expected type mem, found i/o\n"); 161 return 1; 162 } 163 /* XXX Allow 64bit bars for 32bit requests.*/ 164 if (PCI_MAPREG_MEM_TYPE(address) != 165 PCI_MAPREG_MEM_TYPE(type) && 166 PCI_MAPREG_MEM_TYPE(address) != 167 PCI_MAPREG_MEM_TYPE_64BIT) { 168 printf("pci_mem_find: " 169 "expected mem type %08x, found %08x\n", 170 PCI_MAPREG_MEM_TYPE(type), 171 PCI_MAPREG_MEM_TYPE(address)); 172 return 1; 173 } 174 } 175 176 waddress = (u_int64_t)address1 << 32UL | address; 177 wmask = (u_int64_t)mask1 << 32UL | mask; 178 179 if ((is64bit && PCI_MAPREG_MEM64_SIZE(wmask) == 0) || 180 (!is64bit && PCI_MAPREG_MEM_SIZE(mask) == 0)) { 181 aprint_debug("pci_mem_find: void region\n"); 182 return 1; 183 } 184 185 switch (PCI_MAPREG_MEM_TYPE(address)) { 186 case PCI_MAPREG_MEM_TYPE_32BIT: 187 case PCI_MAPREG_MEM_TYPE_32BIT_1M: 188 break; 189 case PCI_MAPREG_MEM_TYPE_64BIT: 190 /* 191 * Handle the case of a 64-bit memory register on a 192 * platform with 32-bit addressing. Make sure that 193 * the address assigned and the device's memory size 194 * fit in 32 bits. We implicitly assume that if 195 * bus_addr_t is 64-bit, then so is bus_size_t. 196 */ 197 if (sizeof(u_int64_t) > sizeof(bus_addr_t) && 198 (address1 != 0 || mask1 != 0xffffffff)) { 199 printf("pci_mem_find: 64-bit memory map which is " 200 "inaccessible on a 32-bit platform\n"); 201 return 1; 202 } 203 break; 204 default: 205 printf("pci_mem_find: reserved mapping register type\n"); 206 return 1; 207 } 208 209 if (sizeof(u_int64_t) > sizeof(bus_addr_t)) { 210 if (basep != NULL) 211 *basep = PCI_MAPREG_MEM_ADDR(address); 212 if (sizep != NULL) 213 *sizep = PCI_MAPREG_MEM_SIZE(mask); 214 } else { 215 if (basep != NULL) 216 *basep = PCI_MAPREG_MEM64_ADDR(waddress); 217 if (sizep != NULL) 218 *sizep = PCI_MAPREG_MEM64_SIZE(wmask); 219 } 220 if (flagsp != NULL) 221 *flagsp = (isrom || PCI_MAPREG_MEM_PREFETCHABLE(address)) ? 222 BUS_SPACE_MAP_PREFETCHABLE : 0; 223 224 return 0; 225} 226 227#define _PCI_MAPREG_TYPEBITS(reg) \ 228 (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO ? \ 229 reg & PCI_MAPREG_TYPE_MASK : \ 230 reg & (PCI_MAPREG_TYPE_MASK|PCI_MAPREG_MEM_TYPE_MASK)) 231 232pcireg_t 233pci_mapreg_type(pci_chipset_tag_t pc, pcitag_t tag, int reg) 234{ 235 236 return _PCI_MAPREG_TYPEBITS(pci_conf_read(pc, tag, reg)); 237} 238 239int 240pci_mapreg_probe(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *typep) 241{ 242 pcireg_t address, mask; 243 int s; 244 245 s = splhigh(); 246 address = pci_conf_read(pc, tag, reg); 247 pci_conf_write(pc, tag, reg, 0xffffffff); 248 mask = pci_conf_read(pc, tag, reg); 249 pci_conf_write(pc, tag, reg, address); 250 splx(s); 251 252 if (mask == 0) /* unimplemented mapping register */ 253 return 0; 254 255 if (typep != NULL) 256 *typep = _PCI_MAPREG_TYPEBITS(address); 257 return 1; 258} 259 260int 261pci_mapreg_info(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type, 262 bus_addr_t *basep, bus_size_t *sizep, int *flagsp) 263{ 264 265 if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) 266 return pci_io_find(pc, tag, reg, type, basep, sizep, 267 flagsp); 268 else 269 return pci_mem_find(pc, tag, reg, type, basep, sizep, 270 flagsp); 271} 272 273int 274pci_mapreg_map(const struct pci_attach_args *pa, int reg, pcireg_t type, 275 int busflags, bus_space_tag_t *tagp, bus_space_handle_t *handlep, 276 bus_addr_t *basep, bus_size_t *sizep) 277{ 278 return pci_mapreg_submap(pa, reg, type, busflags, 0, 0, tagp, 279 handlep, basep, sizep); 280} 281 282static int 283pci_mapreg_submap(const struct pci_attach_args *pa, int reg, pcireg_t type, 284 int busflags, bus_size_t maxsize, bus_size_t offset, bus_space_tag_t *tagp, 285 bus_space_handle_t *handlep, bus_addr_t *basep, bus_size_t *sizep) 286{ 287 bus_space_tag_t tag; 288 bus_space_handle_t handle; 289 bus_addr_t base; 290 bus_size_t size; 291 int flags; 292 293 if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) { 294 if ((pa->pa_flags & PCI_FLAGS_IO_OKAY) == 0) 295 return 1; 296 if (pci_io_find(pa->pa_pc, pa->pa_tag, reg, type, &base, 297 &size, &flags)) 298 return 1; 299 tag = pa->pa_iot; 300 } else { 301 if ((pa->pa_flags & PCI_FLAGS_MEM_OKAY) == 0) 302 return 1; 303 if (pci_mem_find(pa->pa_pc, pa->pa_tag, reg, type, &base, 304 &size, &flags)) 305 return 1; 306 tag = pa->pa_memt; 307 } 308 309 if (reg == PCI_MAPREG_ROM) { 310 pcireg_t mask; 311 int s; 312 /* we have to enable the ROM address decoder... */ 313 s = splhigh(); 314 mask = pci_conf_read(pa->pa_pc, pa->pa_tag, reg); 315 mask |= PCI_MAPREG_ROM_ENABLE; 316 pci_conf_write(pa->pa_pc, pa->pa_tag, reg, mask); 317 splx(s); 318 } 319 320 /* If we're called with maxsize/offset of 0, behave like 321 * pci_mapreg_map. 322 */ 323 324 maxsize = (maxsize && offset) ? maxsize : size; 325 base += offset; 326 327 if ((maxsize < size && offset + maxsize <= size) || offset != 0) 328 return 1; 329 330 if (bus_space_map(tag, base, maxsize, busflags | flags, &handle)) 331 return 1; 332 333 if (tagp != NULL) 334 *tagp = tag; 335 if (handlep != NULL) 336 *handlep = handle; 337 if (basep != NULL) 338 *basep = base; 339 if (sizep != NULL) 340 *sizep = maxsize; 341 342 return 0; 343} 344 345int 346pci_find_rom(const struct pci_attach_args *pa, bus_space_tag_t bst, 347 bus_space_handle_t bsh, int type, bus_space_handle_t *romh, bus_size_t *sz) 348{ 349 bus_size_t romsz, offset = 0, imagesz; 350 uint16_t ptr; 351 int done = 0; 352 353 if (pci_mem_find(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM, 354 PCI_MAPREG_TYPE_ROM, NULL, &romsz, NULL)) 355 return 1; 356 357 /* 358 * no upper bound check; i cannot imagine a 4GB ROM, but 359 * it appears the spec would allow it! 360 */ 361 if (romsz < 1024) 362 return 1; 363 364 while (offset < romsz && !done){ 365 struct pci_rom_header hdr; 366 struct pci_rom rom; 367 368 hdr.romh_magic = bus_space_read_2(bst, bsh, 369 offset + offsetof (struct pci_rom_header, romh_magic)); 370 hdr.romh_data_ptr = bus_space_read_2(bst, bsh, 371 offset + offsetof (struct pci_rom_header, romh_data_ptr)); 372 373 /* no warning: quite possibly ROM is simply not populated */ 374 if (hdr.romh_magic != PCI_ROM_HEADER_MAGIC) 375 return 1; 376 377 ptr = offset + hdr.romh_data_ptr; 378 379 if (ptr > romsz) { 380 printf("pci_find_rom: rom data ptr out of range\n"); 381 return 1; 382 } 383 384 rom.rom_signature = bus_space_read_4(bst, bsh, ptr); 385 rom.rom_vendor = bus_space_read_2(bst, bsh, ptr + 386 offsetof(struct pci_rom, rom_vendor)); 387 rom.rom_product = bus_space_read_2(bst, bsh, ptr + 388 offsetof(struct pci_rom, rom_product)); 389 rom.rom_class = bus_space_read_1(bst, bsh, 390 ptr + offsetof (struct pci_rom, rom_class)); 391 rom.rom_subclass = bus_space_read_1(bst, bsh, 392 ptr + offsetof (struct pci_rom, rom_subclass)); 393 rom.rom_interface = bus_space_read_1(bst, bsh, 394 ptr + offsetof (struct pci_rom, rom_interface)); 395 rom.rom_len = bus_space_read_2(bst, bsh, 396 ptr + offsetof (struct pci_rom, rom_len)); 397 rom.rom_code_type = bus_space_read_1(bst, bsh, 398 ptr + offsetof (struct pci_rom, rom_code_type)); 399 rom.rom_indicator = bus_space_read_1(bst, bsh, 400 ptr + offsetof (struct pci_rom, rom_indicator)); 401 402 if (rom.rom_signature != PCI_ROM_SIGNATURE) { 403 printf("pci_find_rom: bad rom data signature\n"); 404 return 1; 405 } 406 407 imagesz = rom.rom_len * 512; 408 409 if ((rom.rom_vendor == PCI_VENDOR(pa->pa_id)) && 410 (rom.rom_product == PCI_PRODUCT(pa->pa_id)) && 411 (rom.rom_class == PCI_CLASS(pa->pa_class)) && 412 (rom.rom_subclass == PCI_SUBCLASS(pa->pa_class)) && 413 (rom.rom_interface == PCI_INTERFACE(pa->pa_class)) && 414 (rom.rom_code_type == type)) { 415 *sz = imagesz; 416 bus_space_subregion(bst, bsh, offset, imagesz, romh); 417 return 0; 418 } 419 420 /* last image check */ 421 if (rom.rom_indicator & PCI_ROM_INDICATOR_LAST) 422 return 1; 423 424 /* offset by size */ 425 offset += imagesz; 426 } 427 return 1; 428} 429