1 1.9 christos /* $NetBSD: pci_ranges.c,v 1.9 2021/06/21 03:01:23 christos Exp $ */ 2 1.1 dyoung 3 1.1 dyoung /*- 4 1.1 dyoung * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 1.1 dyoung * All rights reserved. 6 1.1 dyoung * 7 1.1 dyoung * This code is derived from software contributed to The NetBSD Foundation 8 1.1 dyoung * by David Young <dyoung (at) NetBSD.org>. 9 1.1 dyoung * 10 1.1 dyoung * Redistribution and use in source and binary forms, with or without 11 1.1 dyoung * modification, are permitted provided that the following conditions 12 1.1 dyoung * are met: 13 1.1 dyoung * 1. Redistributions of source code must retain the above copyright 14 1.1 dyoung * notice, this list of conditions and the following disclaimer. 15 1.1 dyoung * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 dyoung * notice, this list of conditions and the following disclaimer in the 17 1.1 dyoung * documentation and/or other materials provided with the distribution. 18 1.1 dyoung * 19 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 dyoung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 dyoung * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 dyoung * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 dyoung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 dyoung * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 dyoung * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 dyoung * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 dyoung * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 dyoung * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 dyoung * POSSIBILITY OF SUCH DAMAGE. 30 1.1 dyoung */ 31 1.1 dyoung 32 1.1 dyoung 33 1.1 dyoung #include <sys/cdefs.h> 34 1.9 christos __KERNEL_RCSID(0, "$NetBSD: pci_ranges.c,v 1.9 2021/06/21 03:01:23 christos Exp $"); 35 1.1 dyoung 36 1.1 dyoung #include <sys/types.h> 37 1.1 dyoung #include <sys/param.h> 38 1.1 dyoung #include <sys/systm.h> 39 1.1 dyoung #include <sys/errno.h> 40 1.1 dyoung #include <sys/bus.h> 41 1.1 dyoung #include <sys/kmem.h> 42 1.1 dyoung 43 1.1 dyoung #include <prop/proplib.h> 44 1.1 dyoung #include <ppath/ppath.h> 45 1.1 dyoung 46 1.1 dyoung #include <dev/pci/pcivar.h> 47 1.1 dyoung #include <dev/pci/pcireg.h> 48 1.1 dyoung #include <dev/pci/pccbbreg.h> 49 1.1 dyoung 50 1.3 dyoung #include <machine/autoconf.h> 51 1.3 dyoung 52 1.1 dyoung typedef enum pci_alloc_regtype { 53 1.1 dyoung PCI_ALLOC_REGTYPE_NONE = 0 54 1.1 dyoung , PCI_ALLOC_REGTYPE_BAR = 1 55 1.1 dyoung , PCI_ALLOC_REGTYPE_WIN = 2 56 1.1 dyoung , PCI_ALLOC_REGTYPE_CBWIN = 3 57 1.1 dyoung , PCI_ALLOC_REGTYPE_VGA_EN = 4 58 1.1 dyoung } pci_alloc_regtype_t; 59 1.1 dyoung 60 1.1 dyoung typedef enum pci_alloc_space { 61 1.1 dyoung PCI_ALLOC_SPACE_IO = 0 62 1.1 dyoung , PCI_ALLOC_SPACE_MEM = 1 63 1.1 dyoung } pci_alloc_space_t; 64 1.1 dyoung 65 1.1 dyoung typedef enum pci_alloc_flags { 66 1.1 dyoung PCI_ALLOC_F_PREFETCHABLE = 0x1 67 1.1 dyoung } pci_alloc_flags_t; 68 1.1 dyoung 69 1.1 dyoung typedef struct pci_alloc { 70 1.1 dyoung TAILQ_ENTRY(pci_alloc) pal_link; 71 1.1 dyoung pcitag_t pal_tag; 72 1.1 dyoung uint64_t pal_addr; 73 1.1 dyoung uint64_t pal_size; 74 1.1 dyoung pci_alloc_regtype_t pal_type; 75 1.1 dyoung struct pci_alloc_reg { 76 1.1 dyoung int r_ofs; 77 1.1 dyoung pcireg_t r_val; 78 1.1 dyoung pcireg_t r_mask; 79 1.1 dyoung } pal_reg[3]; 80 1.1 dyoung pci_alloc_space_t pal_space; 81 1.1 dyoung pci_alloc_flags_t pal_flags; 82 1.1 dyoung } pci_alloc_t; 83 1.1 dyoung 84 1.1 dyoung typedef struct pci_alloc_reg pci_alloc_reg_t; 85 1.1 dyoung 86 1.1 dyoung TAILQ_HEAD(pci_alloc_list, pci_alloc); 87 1.1 dyoung 88 1.1 dyoung typedef struct pci_alloc_list pci_alloc_list_t; 89 1.1 dyoung 90 1.1 dyoung static pci_alloc_t * 91 1.1 dyoung pci_alloc_dup(const pci_alloc_t *pal) 92 1.1 dyoung { 93 1.1 dyoung pci_alloc_t *npal; 94 1.1 dyoung 95 1.7 chs npal = kmem_alloc(sizeof(*npal), KM_SLEEP); 96 1.1 dyoung *npal = *pal; 97 1.1 dyoung return npal; 98 1.1 dyoung } 99 1.1 dyoung 100 1.1 dyoung static bool 101 1.1 dyoung pci_alloc_linkdup(pci_alloc_list_t *pals, const pci_alloc_t *pal) 102 1.1 dyoung { 103 1.1 dyoung pci_alloc_t *npal; 104 1.1 dyoung 105 1.1 dyoung if ((npal = pci_alloc_dup(pal)) == NULL) 106 1.1 dyoung return false; 107 1.1 dyoung 108 1.1 dyoung TAILQ_INSERT_TAIL(pals, npal, pal_link); 109 1.1 dyoung 110 1.1 dyoung return true; 111 1.1 dyoung } 112 1.1 dyoung 113 1.1 dyoung struct range_infer_ctx { 114 1.1 dyoung pci_chipset_tag_t ric_pc; 115 1.1 dyoung pci_alloc_list_t ric_pals; 116 1.1 dyoung bus_addr_t ric_mmio_bottom; 117 1.1 dyoung bus_addr_t ric_mmio_top; 118 1.1 dyoung bus_addr_t ric_io_bottom; 119 1.1 dyoung bus_addr_t ric_io_top; 120 1.1 dyoung }; 121 1.1 dyoung 122 1.1 dyoung static bool 123 1.1 dyoung io_range_extend(struct range_infer_ctx *ric, const pci_alloc_t *pal) 124 1.1 dyoung { 125 1.1 dyoung if (ric->ric_io_bottom > pal->pal_addr) 126 1.1 dyoung ric->ric_io_bottom = pal->pal_addr; 127 1.1 dyoung if (ric->ric_io_top < pal->pal_addr + pal->pal_size) 128 1.1 dyoung ric->ric_io_top = pal->pal_addr + pal->pal_size; 129 1.1 dyoung 130 1.1 dyoung return pci_alloc_linkdup(&ric->ric_pals, pal); 131 1.1 dyoung } 132 1.1 dyoung 133 1.1 dyoung static bool 134 1.1 dyoung io_range_extend_by_bar(struct range_infer_ctx *ric, int bus, int dev, int fun, 135 1.1 dyoung int ofs, pcireg_t curbar, pcireg_t sizebar) 136 1.1 dyoung { 137 1.1 dyoung pci_alloc_reg_t *r; 138 1.1 dyoung pci_alloc_t pal = { 139 1.1 dyoung .pal_flags = 0 140 1.1 dyoung , .pal_space = PCI_ALLOC_SPACE_IO 141 1.1 dyoung , .pal_type = PCI_ALLOC_REGTYPE_BAR 142 1.1 dyoung , .pal_reg = {{ 143 1.1 dyoung .r_mask = ~(pcireg_t)0 144 1.1 dyoung }} 145 1.1 dyoung }; 146 1.1 dyoung 147 1.1 dyoung r = &pal.pal_reg[0]; 148 1.1 dyoung 149 1.1 dyoung pal.pal_tag = pci_make_tag(ric->ric_pc, bus, dev, fun); 150 1.1 dyoung r->r_ofs = ofs; 151 1.1 dyoung r->r_val = curbar; 152 1.1 dyoung 153 1.1 dyoung pal.pal_addr = PCI_MAPREG_IO_ADDR(curbar); 154 1.1 dyoung pal.pal_size = PCI_MAPREG_IO_SIZE(sizebar); 155 1.1 dyoung 156 1.1 dyoung aprint_debug("%s: %d.%d.%d base at %" PRIx64 " size %" PRIx64 "\n", 157 1.1 dyoung __func__, bus, dev, fun, pal.pal_addr, pal.pal_size); 158 1.1 dyoung 159 1.1 dyoung return (pal.pal_size == 0) || io_range_extend(ric, &pal); 160 1.1 dyoung } 161 1.1 dyoung 162 1.1 dyoung static bool 163 1.1 dyoung io_range_extend_by_vga_enable(struct range_infer_ctx *ric, 164 1.1 dyoung int bus, int dev, int fun, pcireg_t csr, pcireg_t bcr) 165 1.1 dyoung { 166 1.1 dyoung pci_alloc_reg_t *r; 167 1.1 dyoung pci_alloc_t tpal = { 168 1.1 dyoung .pal_flags = 0 169 1.1 dyoung , .pal_space = PCI_ALLOC_SPACE_IO 170 1.1 dyoung , .pal_type = PCI_ALLOC_REGTYPE_VGA_EN 171 1.1 dyoung , .pal_reg = {{ 172 1.1 dyoung .r_ofs = PCI_COMMAND_STATUS_REG 173 1.1 dyoung , .r_mask = PCI_COMMAND_IO_ENABLE 174 1.1 dyoung }, { 175 1.1 dyoung .r_ofs = PCI_BRIDGE_CONTROL_REG 176 1.8 msaitoh , .r_mask = PCI_BRIDGE_CONTROL_VGA; 177 1.1 dyoung }} 178 1.1 dyoung }, pal[2]; 179 1.1 dyoung 180 1.1 dyoung aprint_debug("%s: %d.%d.%d enter\n", __func__, bus, dev, fun); 181 1.1 dyoung 182 1.1 dyoung if ((csr & PCI_COMMAND_IO_ENABLE) == 0 || 183 1.8 msaitoh (bcr & PCI_BRIDGE_CONTROL_VGA) == 0) { 184 1.1 dyoung aprint_debug("%s: %d.%d.%d I/O or VGA disabled\n", 185 1.1 dyoung __func__, bus, dev, fun); 186 1.1 dyoung return true; 187 1.1 dyoung } 188 1.1 dyoung 189 1.1 dyoung r = &tpal.pal_reg[0]; 190 1.1 dyoung tpal.pal_tag = pci_make_tag(ric->ric_pc, bus, dev, fun); 191 1.1 dyoung r[0].r_val = csr; 192 1.1 dyoung r[1].r_val = bcr; 193 1.1 dyoung 194 1.1 dyoung pal[0] = pal[1] = tpal; 195 1.1 dyoung 196 1.1 dyoung pal[0].pal_addr = 0x3b0; 197 1.1 dyoung pal[0].pal_size = 0x3bb - 0x3b0 + 1; 198 1.1 dyoung 199 1.1 dyoung pal[1].pal_addr = 0x3c0; 200 1.1 dyoung pal[1].pal_size = 0x3df - 0x3c0 + 1; 201 1.1 dyoung 202 1.1 dyoung /* XXX add aliases for pal[0..1] */ 203 1.1 dyoung 204 1.1 dyoung return io_range_extend(ric, &pal[0]) && io_range_extend(ric, &pal[1]); 205 1.1 dyoung } 206 1.1 dyoung 207 1.1 dyoung static bool 208 1.1 dyoung io_range_extend_by_win(struct range_infer_ctx *ric, 209 1.1 dyoung int bus, int dev, int fun, int ofs, int ofshigh, 210 1.1 dyoung pcireg_t io, pcireg_t iohigh) 211 1.1 dyoung { 212 1.1 dyoung const int fourkb = 4 * 1024; 213 1.1 dyoung pcireg_t baser, limitr; 214 1.1 dyoung pci_alloc_reg_t *r; 215 1.1 dyoung pci_alloc_t pal = { 216 1.1 dyoung .pal_flags = 0 217 1.1 dyoung , .pal_space = PCI_ALLOC_SPACE_IO 218 1.1 dyoung , .pal_type = PCI_ALLOC_REGTYPE_WIN 219 1.1 dyoung , .pal_reg = {{ 220 1.1 dyoung .r_mask = ~(pcireg_t)0 221 1.1 dyoung }} 222 1.1 dyoung }; 223 1.1 dyoung 224 1.1 dyoung r = &pal.pal_reg[0]; 225 1.1 dyoung 226 1.1 dyoung pal.pal_tag = pci_make_tag(ric->ric_pc, bus, dev, fun); 227 1.1 dyoung r[0].r_ofs = ofs; 228 1.1 dyoung r[0].r_val = io; 229 1.1 dyoung 230 1.8 msaitoh baser = __SHIFTOUT(io, PCI_BRIDGE_STATIO_IOBASE) >> 4; 231 1.8 msaitoh limitr = __SHIFTOUT(io, PCI_BRIDGE_STATIO_IOLIMIT) >> 4; 232 1.1 dyoung 233 1.1 dyoung if (PCI_BRIDGE_IO_32BITS(io)) { 234 1.1 dyoung pcireg_t baseh, limith; 235 1.1 dyoung 236 1.1 dyoung r[1].r_mask = ~(pcireg_t)0; 237 1.1 dyoung r[1].r_ofs = ofshigh; 238 1.1 dyoung r[1].r_val = iohigh; 239 1.1 dyoung 240 1.8 msaitoh baseh = __SHIFTOUT(iohigh, PCI_BRIDGE_IOHIGH_BASE); 241 1.8 msaitoh limith = __SHIFTOUT(iohigh, PCI_BRIDGE_IOHIGH_LIMIT); 242 1.1 dyoung 243 1.1 dyoung baser |= baseh << 4; 244 1.1 dyoung limitr |= limith << 4; 245 1.1 dyoung } 246 1.1 dyoung 247 1.1 dyoung /* XXX check with the PCI standard */ 248 1.1 dyoung if (baser > limitr) 249 1.1 dyoung return true; 250 1.1 dyoung 251 1.1 dyoung pal.pal_addr = baser * fourkb; 252 1.1 dyoung pal.pal_size = (limitr - baser + 1) * fourkb; 253 1.1 dyoung 254 1.1 dyoung aprint_debug("%s: %d.%d.%d window at %" PRIx64 " size %" PRIx64 "\n", 255 1.1 dyoung __func__, bus, dev, fun, pal.pal_addr, pal.pal_size); 256 1.1 dyoung 257 1.1 dyoung return io_range_extend(ric, &pal); 258 1.1 dyoung } 259 1.1 dyoung 260 1.1 dyoung static bool 261 1.1 dyoung io_range_extend_by_cbwin(struct range_infer_ctx *ric, 262 1.1 dyoung int bus, int dev, int fun, int ofs, pcireg_t base0, pcireg_t limit0) 263 1.1 dyoung { 264 1.1 dyoung pcireg_t base, limit; 265 1.1 dyoung pci_alloc_reg_t *r; 266 1.1 dyoung pci_alloc_t pal = { 267 1.1 dyoung .pal_flags = 0 268 1.1 dyoung , .pal_space = PCI_ALLOC_SPACE_IO 269 1.1 dyoung , .pal_type = PCI_ALLOC_REGTYPE_CBWIN 270 1.1 dyoung , .pal_reg = {{ 271 1.1 dyoung .r_mask = ~(pcireg_t)0 272 1.1 dyoung }, { 273 1.1 dyoung .r_mask = ~(pcireg_t)0 274 1.1 dyoung }} 275 1.1 dyoung }; 276 1.1 dyoung 277 1.1 dyoung r = &pal.pal_reg[0]; 278 1.1 dyoung 279 1.1 dyoung pal.pal_tag = pci_make_tag(ric->ric_pc, bus, dev, fun); 280 1.1 dyoung r[0].r_ofs = ofs; 281 1.1 dyoung r[0].r_val = base0; 282 1.1 dyoung r[1].r_ofs = ofs + 4; 283 1.1 dyoung r[1].r_val = limit0; 284 1.1 dyoung 285 1.1 dyoung base = base0 & __BITS(31, 2); 286 1.1 dyoung limit = limit0 & __BITS(31, 2); 287 1.1 dyoung 288 1.1 dyoung if (base > limit) 289 1.1 dyoung return true; 290 1.1 dyoung 291 1.1 dyoung pal.pal_addr = base; 292 1.1 dyoung pal.pal_size = limit - base + 4; /* XXX */ 293 1.1 dyoung 294 1.1 dyoung aprint_debug("%s: %d.%d.%d window at %" PRIx64 " size %" PRIx64 "\n", 295 1.1 dyoung __func__, bus, dev, fun, pal.pal_addr, pal.pal_size); 296 1.1 dyoung 297 1.1 dyoung return io_range_extend(ric, &pal); 298 1.1 dyoung } 299 1.1 dyoung 300 1.1 dyoung static void 301 1.1 dyoung io_range_infer(pci_chipset_tag_t pc, pcitag_t tag, void *ctx) 302 1.1 dyoung { 303 1.1 dyoung struct range_infer_ctx *ric = ctx; 304 1.1 dyoung pcireg_t bhlcr, limit, io; 305 1.1 dyoung int bar, bus, dev, fun, hdrtype, nbar; 306 1.1 dyoung bool ok = true; 307 1.1 dyoung 308 1.1 dyoung bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG); 309 1.1 dyoung 310 1.1 dyoung hdrtype = PCI_HDRTYPE_TYPE(bhlcr); 311 1.1 dyoung 312 1.1 dyoung pci_decompose_tag(pc, tag, &bus, &dev, &fun); 313 1.1 dyoung 314 1.1 dyoung switch (hdrtype) { 315 1.1 dyoung case PCI_HDRTYPE_PPB: 316 1.1 dyoung nbar = 2; 317 1.1 dyoung /* Extract I/O windows */ 318 1.1 dyoung ok = ok && io_range_extend_by_win(ric, bus, dev, fun, 319 1.1 dyoung PCI_BRIDGE_STATIO_REG, 320 1.1 dyoung PCI_BRIDGE_IOHIGH_REG, 321 1.1 dyoung pci_conf_read(pc, tag, PCI_BRIDGE_STATIO_REG), 322 1.1 dyoung pci_conf_read(pc, tag, PCI_BRIDGE_IOHIGH_REG)); 323 1.1 dyoung ok = ok && io_range_extend_by_vga_enable(ric, bus, dev, fun, 324 1.1 dyoung pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG), 325 1.1 dyoung pci_conf_read(pc, tag, PCI_BRIDGE_CONTROL_REG)); 326 1.1 dyoung break; 327 1.1 dyoung case PCI_HDRTYPE_PCB: 328 1.1 dyoung /* Extract I/O windows */ 329 1.1 dyoung io = pci_conf_read(pc, tag, PCI_CB_IOBASE0); 330 1.1 dyoung limit = pci_conf_read(pc, tag, PCI_CB_IOLIMIT0); 331 1.1 dyoung ok = ok && io_range_extend_by_cbwin(ric, bus, dev, fun, 332 1.1 dyoung PCI_CB_IOBASE0, io, limit); 333 1.1 dyoung io = pci_conf_read(pc, tag, PCI_CB_IOBASE1); 334 1.1 dyoung limit = pci_conf_read(pc, tag, PCI_CB_IOLIMIT1); 335 1.1 dyoung ok = ok && io_range_extend_by_cbwin(ric, bus, dev, fun, 336 1.1 dyoung PCI_CB_IOBASE1, io, limit); 337 1.1 dyoung nbar = 1; 338 1.1 dyoung break; 339 1.1 dyoung case PCI_HDRTYPE_DEVICE: 340 1.1 dyoung nbar = 6; 341 1.1 dyoung break; 342 1.1 dyoung default: 343 1.1 dyoung aprint_debug("%s: unknown header type %d at %d.%d.%d\n", 344 1.1 dyoung __func__, hdrtype, bus, dev, fun); 345 1.1 dyoung return; 346 1.1 dyoung } 347 1.1 dyoung 348 1.1 dyoung for (bar = 0; bar < nbar; bar++) { 349 1.1 dyoung pcireg_t basebar, sizebar; 350 1.1 dyoung 351 1.1 dyoung basebar = pci_conf_read(pc, tag, PCI_BAR(bar)); 352 1.1 dyoung pci_conf_write(pc, tag, PCI_BAR(bar), 0xffffffff); 353 1.1 dyoung sizebar = pci_conf_read(pc, tag, PCI_BAR(bar)); 354 1.1 dyoung pci_conf_write(pc, tag, PCI_BAR(bar), basebar); 355 1.1 dyoung 356 1.1 dyoung if (sizebar == 0) 357 1.1 dyoung continue; 358 1.1 dyoung if (PCI_MAPREG_TYPE(sizebar) != PCI_MAPREG_TYPE_IO) 359 1.1 dyoung continue; 360 1.1 dyoung 361 1.1 dyoung ok = ok && io_range_extend_by_bar(ric, bus, dev, fun, 362 1.1 dyoung PCI_BAR(bar), basebar, sizebar); 363 1.1 dyoung } 364 1.1 dyoung if (!ok) { 365 1.1 dyoung aprint_verbose("I/O range inference failed at PCI %d.%d.%d\n", 366 1.1 dyoung bus, dev, fun); 367 1.1 dyoung } 368 1.1 dyoung } 369 1.1 dyoung 370 1.1 dyoung static bool 371 1.1 dyoung mmio_range_extend(struct range_infer_ctx *ric, const pci_alloc_t *pal) 372 1.1 dyoung { 373 1.1 dyoung if (ric->ric_mmio_bottom > pal->pal_addr) 374 1.1 dyoung ric->ric_mmio_bottom = pal->pal_addr; 375 1.1 dyoung if (ric->ric_mmio_top < pal->pal_addr + pal->pal_size) 376 1.1 dyoung ric->ric_mmio_top = pal->pal_addr + pal->pal_size; 377 1.1 dyoung 378 1.1 dyoung return pci_alloc_linkdup(&ric->ric_pals, pal); 379 1.1 dyoung } 380 1.1 dyoung 381 1.1 dyoung static bool 382 1.5 msaitoh mmio_range_extend_by_bar(struct range_infer_ctx *ric, int bus, int dev, 383 1.5 msaitoh int fun, int ofs, pcireg_t curbar, pcireg_t sizebar) 384 1.1 dyoung { 385 1.1 dyoung int type; 386 1.1 dyoung bool prefetchable; 387 1.1 dyoung pci_alloc_reg_t *r; 388 1.1 dyoung pci_alloc_t pal = { 389 1.1 dyoung .pal_flags = 0 390 1.1 dyoung , .pal_space = PCI_ALLOC_SPACE_MEM 391 1.1 dyoung , .pal_type = PCI_ALLOC_REGTYPE_BAR 392 1.1 dyoung , .pal_reg = {{ 393 1.1 dyoung .r_mask = ~(pcireg_t)0 394 1.1 dyoung }} 395 1.1 dyoung }; 396 1.1 dyoung 397 1.1 dyoung r = &pal.pal_reg[0]; 398 1.1 dyoung 399 1.1 dyoung pal.pal_tag = pci_make_tag(ric->ric_pc, bus, dev, fun); 400 1.1 dyoung r->r_ofs = ofs; 401 1.1 dyoung r->r_val = curbar; 402 1.1 dyoung 403 1.1 dyoung pal.pal_addr = PCI_MAPREG_MEM_ADDR(curbar); 404 1.1 dyoung 405 1.1 dyoung type = PCI_MAPREG_MEM_TYPE(curbar); 406 1.1 dyoung prefetchable = PCI_MAPREG_MEM_PREFETCHABLE(curbar); 407 1.1 dyoung 408 1.1 dyoung if (prefetchable) 409 1.1 dyoung pal.pal_flags |= PCI_ALLOC_F_PREFETCHABLE; 410 1.1 dyoung 411 1.1 dyoung switch (type) { 412 1.1 dyoung case PCI_MAPREG_MEM_TYPE_32BIT: 413 1.1 dyoung pal.pal_size = PCI_MAPREG_MEM_SIZE(sizebar); 414 1.1 dyoung break; 415 1.1 dyoung case PCI_MAPREG_MEM_TYPE_64BIT: 416 1.1 dyoung pal.pal_size = PCI_MAPREG_MEM64_SIZE(sizebar); 417 1.1 dyoung break; 418 1.1 dyoung case PCI_MAPREG_MEM_TYPE_32BIT_1M: 419 1.1 dyoung default: 420 1.1 dyoung aprint_debug("%s: ignored memory type %d at %d.%d.%d\n", 421 1.1 dyoung __func__, type, bus, dev, fun); 422 1.1 dyoung return false; 423 1.1 dyoung } 424 1.1 dyoung 425 1.1 dyoung aprint_debug("%s: %d.%d.%d base at %" PRIx64 " size %" PRIx64 "\n", 426 1.1 dyoung __func__, bus, dev, fun, pal.pal_addr, pal.pal_size); 427 1.1 dyoung 428 1.1 dyoung return (pal.pal_size == 0) || mmio_range_extend(ric, &pal); 429 1.1 dyoung } 430 1.1 dyoung 431 1.1 dyoung static bool 432 1.1 dyoung mmio_range_extend_by_vga_enable(struct range_infer_ctx *ric, 433 1.1 dyoung int bus, int dev, int fun, pcireg_t csr, pcireg_t bcr) 434 1.1 dyoung { 435 1.1 dyoung pci_alloc_reg_t *r; 436 1.1 dyoung pci_alloc_t tpal = { 437 1.1 dyoung .pal_flags = PCI_ALLOC_F_PREFETCHABLE /* XXX a guess */ 438 1.1 dyoung , .pal_space = PCI_ALLOC_SPACE_MEM 439 1.1 dyoung , .pal_type = PCI_ALLOC_REGTYPE_VGA_EN 440 1.1 dyoung , .pal_reg = {{ 441 1.1 dyoung .r_ofs = PCI_COMMAND_STATUS_REG 442 1.1 dyoung , .r_mask = PCI_COMMAND_MEM_ENABLE 443 1.1 dyoung }, { 444 1.1 dyoung .r_ofs = PCI_BRIDGE_CONTROL_REG 445 1.8 msaitoh , .r_mask = PCI_BRIDGE_CONTROL_VGA 446 1.1 dyoung }} 447 1.1 dyoung }, pal; 448 1.1 dyoung 449 1.1 dyoung aprint_debug("%s: %d.%d.%d enter\n", __func__, bus, dev, fun); 450 1.1 dyoung 451 1.1 dyoung if ((csr & PCI_COMMAND_MEM_ENABLE) == 0 || 452 1.8 msaitoh (bcr & PCI_BRIDGE_CONTROL_VGA) == 0) { 453 1.1 dyoung aprint_debug("%s: %d.%d.%d memory or VGA disabled\n", 454 1.1 dyoung __func__, bus, dev, fun); 455 1.1 dyoung return true; 456 1.1 dyoung } 457 1.1 dyoung 458 1.1 dyoung r = &tpal.pal_reg[0]; 459 1.1 dyoung tpal.pal_tag = pci_make_tag(ric->ric_pc, bus, dev, fun); 460 1.1 dyoung r[0].r_val = csr; 461 1.1 dyoung r[1].r_val = bcr; 462 1.1 dyoung 463 1.1 dyoung pal = tpal; 464 1.1 dyoung 465 1.1 dyoung pal.pal_addr = 0xa0000; 466 1.1 dyoung pal.pal_size = 0xbffff - 0xa0000 + 1; 467 1.1 dyoung 468 1.1 dyoung return mmio_range_extend(ric, &pal); 469 1.1 dyoung } 470 1.1 dyoung 471 1.1 dyoung static bool 472 1.1 dyoung mmio_range_extend_by_win(struct range_infer_ctx *ric, 473 1.1 dyoung int bus, int dev, int fun, int ofs, pcireg_t mem) 474 1.1 dyoung { 475 1.1 dyoung const int onemeg = 1024 * 1024; 476 1.1 dyoung pcireg_t baser, limitr; 477 1.1 dyoung pci_alloc_reg_t *r; 478 1.1 dyoung pci_alloc_t pal = { 479 1.1 dyoung .pal_flags = 0 480 1.1 dyoung , .pal_space = PCI_ALLOC_SPACE_MEM 481 1.1 dyoung , .pal_type = PCI_ALLOC_REGTYPE_WIN 482 1.1 dyoung , .pal_reg = {{ 483 1.1 dyoung .r_mask = ~(pcireg_t)0 484 1.1 dyoung }} 485 1.1 dyoung }; 486 1.1 dyoung 487 1.1 dyoung r = &pal.pal_reg[0]; 488 1.1 dyoung 489 1.1 dyoung pal.pal_tag = pci_make_tag(ric->ric_pc, bus, dev, fun); 490 1.1 dyoung r->r_ofs = ofs; 491 1.1 dyoung r->r_val = mem; 492 1.1 dyoung 493 1.1 dyoung baser = (mem >> PCI_BRIDGE_MEMORY_BASE_SHIFT) & 494 1.1 dyoung PCI_BRIDGE_MEMORY_BASE_MASK; 495 1.1 dyoung limitr = (mem >> PCI_BRIDGE_MEMORY_LIMIT_SHIFT) & 496 1.1 dyoung PCI_BRIDGE_MEMORY_LIMIT_MASK; 497 1.1 dyoung 498 1.1 dyoung /* XXX check with the PCI standard */ 499 1.1 dyoung if (baser > limitr || limitr == 0) 500 1.1 dyoung return true; 501 1.1 dyoung 502 1.1 dyoung pal.pal_addr = baser * onemeg; 503 1.1 dyoung pal.pal_size = (limitr - baser + 1) * onemeg; 504 1.1 dyoung 505 1.1 dyoung aprint_debug("%s: %d.%d.%d window at %" PRIx64 " size %" PRIx64 "\n", 506 1.1 dyoung __func__, bus, dev, fun, pal.pal_addr, pal.pal_size); 507 1.1 dyoung 508 1.1 dyoung return mmio_range_extend(ric, &pal); 509 1.1 dyoung } 510 1.1 dyoung 511 1.1 dyoung static bool 512 1.1 dyoung mmio_range_extend_by_prememwin(struct range_infer_ctx *ric, 513 1.1 dyoung int bus, int dev, int fun, int ofs, pcireg_t mem, 514 1.1 dyoung int hibaseofs, pcireg_t hibase, 515 1.1 dyoung int hilimitofs, pcireg_t hilimit) 516 1.1 dyoung { 517 1.1 dyoung const int onemeg = 1024 * 1024; 518 1.1 dyoung uint64_t baser, limitr; 519 1.1 dyoung pci_alloc_reg_t *r; 520 1.1 dyoung pci_alloc_t pal = { 521 1.1 dyoung .pal_flags = PCI_ALLOC_F_PREFETCHABLE 522 1.1 dyoung , .pal_space = PCI_ALLOC_SPACE_MEM 523 1.1 dyoung , .pal_type = PCI_ALLOC_REGTYPE_WIN 524 1.1 dyoung , .pal_reg = {{ 525 1.1 dyoung .r_mask = ~(pcireg_t)0 526 1.1 dyoung }} 527 1.1 dyoung }; 528 1.1 dyoung 529 1.1 dyoung r = &pal.pal_reg[0]; 530 1.1 dyoung 531 1.1 dyoung pal.pal_tag = pci_make_tag(ric->ric_pc, bus, dev, fun); 532 1.1 dyoung r[0].r_ofs = ofs; 533 1.1 dyoung r[0].r_val = mem; 534 1.1 dyoung 535 1.1 dyoung baser = (mem >> PCI_BRIDGE_PREFETCHMEM_BASE_SHIFT) & 536 1.1 dyoung PCI_BRIDGE_PREFETCHMEM_BASE_MASK; 537 1.1 dyoung limitr = (mem >> PCI_BRIDGE_PREFETCHMEM_LIMIT_SHIFT) & 538 1.1 dyoung PCI_BRIDGE_PREFETCHMEM_LIMIT_MASK; 539 1.1 dyoung 540 1.1 dyoung if (PCI_BRIDGE_PREFETCHMEM_64BITS(mem)) { 541 1.1 dyoung r[1].r_mask = r[2].r_mask = ~(pcireg_t)0; 542 1.1 dyoung r[1].r_ofs = hibaseofs; 543 1.1 dyoung r[1].r_val = hibase; 544 1.1 dyoung r[2].r_ofs = hilimitofs; 545 1.1 dyoung r[2].r_val = hilimit; 546 1.1 dyoung 547 1.1 dyoung baser |= hibase << 12; 548 1.1 dyoung limitr |= hibase << 12; 549 1.1 dyoung } 550 1.1 dyoung 551 1.1 dyoung /* XXX check with the PCI standard */ 552 1.1 dyoung if (baser > limitr || limitr == 0) 553 1.1 dyoung return true; 554 1.1 dyoung 555 1.1 dyoung pal.pal_addr = baser * onemeg; 556 1.1 dyoung pal.pal_size = (limitr - baser + 1) * onemeg; 557 1.1 dyoung 558 1.1 dyoung aprint_debug("%s: %d.%d.%d window at %" PRIx64 " size %" PRIx64 "\n", 559 1.1 dyoung __func__, bus, dev, fun, pal.pal_addr, pal.pal_size); 560 1.1 dyoung 561 1.1 dyoung return mmio_range_extend(ric, &pal); 562 1.1 dyoung } 563 1.1 dyoung 564 1.1 dyoung static bool 565 1.1 dyoung mmio_range_extend_by_cbwin(struct range_infer_ctx *ric, 566 1.1 dyoung int bus, int dev, int fun, int ofs, pcireg_t base, pcireg_t limit, 567 1.1 dyoung bool prefetchable) 568 1.1 dyoung { 569 1.1 dyoung pci_alloc_reg_t *r; 570 1.1 dyoung pci_alloc_t pal = { 571 1.1 dyoung .pal_flags = 0 572 1.1 dyoung , .pal_space = PCI_ALLOC_SPACE_MEM 573 1.1 dyoung , .pal_type = PCI_ALLOC_REGTYPE_CBWIN 574 1.1 dyoung , .pal_reg = {{ 575 1.1 dyoung .r_mask = ~(pcireg_t)0 576 1.1 dyoung }, { 577 1.1 dyoung .r_mask = ~(pcireg_t)0 578 1.1 dyoung }} 579 1.1 dyoung }; 580 1.1 dyoung 581 1.1 dyoung r = &pal.pal_reg[0]; 582 1.1 dyoung 583 1.1 dyoung if (prefetchable) 584 1.1 dyoung pal.pal_flags |= PCI_ALLOC_F_PREFETCHABLE; 585 1.1 dyoung 586 1.1 dyoung pal.pal_tag = pci_make_tag(ric->ric_pc, bus, dev, fun); 587 1.1 dyoung r[0].r_ofs = ofs; 588 1.1 dyoung r[0].r_val = base; 589 1.1 dyoung r[1].r_ofs = ofs + 4; 590 1.1 dyoung r[1].r_val = limit; 591 1.1 dyoung 592 1.1 dyoung if (base > limit) 593 1.1 dyoung return true; 594 1.1 dyoung 595 1.1 dyoung if (limit == 0) 596 1.1 dyoung return true; 597 1.1 dyoung 598 1.1 dyoung pal.pal_addr = base; 599 1.1 dyoung pal.pal_size = limit - base + 4096; 600 1.1 dyoung 601 1.1 dyoung aprint_debug("%s: %d.%d.%d window at %" PRIx64 " size %" PRIx64 "\n", 602 1.1 dyoung __func__, bus, dev, fun, pal.pal_addr, pal.pal_size); 603 1.1 dyoung 604 1.1 dyoung return mmio_range_extend(ric, &pal); 605 1.1 dyoung } 606 1.1 dyoung 607 1.1 dyoung static void 608 1.1 dyoung mmio_range_infer(pci_chipset_tag_t pc, pcitag_t tag, void *ctx) 609 1.1 dyoung { 610 1.1 dyoung struct range_infer_ctx *ric = ctx; 611 1.1 dyoung pcireg_t bcr, bhlcr, limit, mem, premem, hiprebase, hiprelimit; 612 1.1 dyoung int bar, bus, dev, fun, hdrtype, nbar; 613 1.1 dyoung bool ok = true; 614 1.1 dyoung 615 1.1 dyoung bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG); 616 1.1 dyoung 617 1.1 dyoung hdrtype = PCI_HDRTYPE_TYPE(bhlcr); 618 1.1 dyoung 619 1.1 dyoung pci_decompose_tag(pc, tag, &bus, &dev, &fun); 620 1.1 dyoung 621 1.1 dyoung switch (hdrtype) { 622 1.1 dyoung case PCI_HDRTYPE_PPB: 623 1.1 dyoung nbar = 2; 624 1.1 dyoung /* Extract memory windows */ 625 1.1 dyoung ok = ok && mmio_range_extend_by_win(ric, bus, dev, fun, 626 1.1 dyoung PCI_BRIDGE_MEMORY_REG, 627 1.1 dyoung pci_conf_read(pc, tag, PCI_BRIDGE_MEMORY_REG)); 628 1.1 dyoung premem = pci_conf_read(pc, tag, PCI_BRIDGE_PREFETCHMEM_REG); 629 1.1 dyoung if (PCI_BRIDGE_PREFETCHMEM_64BITS(premem)) { 630 1.1 dyoung aprint_debug("%s: 64-bit prefetchable memory window " 631 1.1 dyoung "at %d.%d.%d\n", __func__, bus, dev, fun); 632 1.1 dyoung hiprebase = pci_conf_read(pc, tag, 633 1.1 dyoung PCI_BRIDGE_PREFETCHBASE32_REG); 634 1.1 dyoung hiprelimit = pci_conf_read(pc, tag, 635 1.1 dyoung PCI_BRIDGE_PREFETCHLIMIT32_REG); 636 1.1 dyoung } else 637 1.1 dyoung hiprebase = hiprelimit = 0; 638 1.1 dyoung ok = ok && 639 1.1 dyoung mmio_range_extend_by_prememwin(ric, bus, dev, fun, 640 1.1 dyoung PCI_BRIDGE_PREFETCHMEM_REG, premem, 641 1.1 dyoung PCI_BRIDGE_PREFETCHBASE32_REG, hiprebase, 642 1.1 dyoung PCI_BRIDGE_PREFETCHLIMIT32_REG, hiprelimit) && 643 1.1 dyoung mmio_range_extend_by_vga_enable(ric, bus, dev, fun, 644 1.1 dyoung pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG), 645 1.1 dyoung pci_conf_read(pc, tag, PCI_BRIDGE_CONTROL_REG)); 646 1.1 dyoung break; 647 1.1 dyoung case PCI_HDRTYPE_PCB: 648 1.1 dyoung /* Extract memory windows */ 649 1.1 dyoung bcr = pci_conf_read(pc, tag, PCI_BRIDGE_CONTROL_REG); 650 1.1 dyoung mem = pci_conf_read(pc, tag, PCI_CB_MEMBASE0); 651 1.1 dyoung limit = pci_conf_read(pc, tag, PCI_CB_MEMLIMIT0); 652 1.1 dyoung ok = ok && mmio_range_extend_by_cbwin(ric, bus, dev, fun, 653 1.1 dyoung PCI_CB_MEMBASE0, mem, limit, 654 1.1 dyoung (bcr & CB_BCR_PREFETCH_MEMWIN0) != 0); 655 1.1 dyoung mem = pci_conf_read(pc, tag, PCI_CB_MEMBASE1); 656 1.1 dyoung limit = pci_conf_read(pc, tag, PCI_CB_MEMLIMIT1); 657 1.1 dyoung ok = ok && mmio_range_extend_by_cbwin(ric, bus, dev, fun, 658 1.1 dyoung PCI_CB_MEMBASE1, mem, limit, 659 1.1 dyoung (bcr & CB_BCR_PREFETCH_MEMWIN1) != 0); 660 1.1 dyoung nbar = 1; 661 1.1 dyoung break; 662 1.1 dyoung case PCI_HDRTYPE_DEVICE: 663 1.1 dyoung nbar = 6; 664 1.1 dyoung break; 665 1.1 dyoung default: 666 1.1 dyoung aprint_debug("%s: unknown header type %d at %d.%d.%d\n", 667 1.1 dyoung __func__, hdrtype, bus, dev, fun); 668 1.1 dyoung return; 669 1.1 dyoung } 670 1.1 dyoung 671 1.1 dyoung for (bar = 0; bar < nbar; bar++) { 672 1.1 dyoung pcireg_t basebar, sizebar; 673 1.1 dyoung 674 1.1 dyoung basebar = pci_conf_read(pc, tag, PCI_BAR(bar)); 675 1.1 dyoung pci_conf_write(pc, tag, PCI_BAR(bar), 0xffffffff); 676 1.1 dyoung sizebar = pci_conf_read(pc, tag, PCI_BAR(bar)); 677 1.1 dyoung pci_conf_write(pc, tag, PCI_BAR(bar), basebar); 678 1.1 dyoung 679 1.1 dyoung if (sizebar == 0) 680 1.1 dyoung continue; 681 1.1 dyoung if (PCI_MAPREG_TYPE(sizebar) != PCI_MAPREG_TYPE_MEM) 682 1.1 dyoung continue; 683 1.1 dyoung 684 1.1 dyoung ok = ok && mmio_range_extend_by_bar(ric, bus, dev, fun, 685 1.1 dyoung PCI_BAR(bar), basebar, sizebar); 686 1.1 dyoung } 687 1.1 dyoung if (!ok) { 688 1.1 dyoung aprint_verbose("MMIO range inference failed at PCI %d.%d.%d\n", 689 1.1 dyoung bus, dev, fun); 690 1.1 dyoung } 691 1.1 dyoung } 692 1.1 dyoung 693 1.1 dyoung static const char * 694 1.1 dyoung pci_alloc_regtype_string(const pci_alloc_regtype_t t) 695 1.1 dyoung { 696 1.1 dyoung switch (t) { 697 1.1 dyoung case PCI_ALLOC_REGTYPE_BAR: 698 1.1 dyoung return "bar"; 699 1.1 dyoung case PCI_ALLOC_REGTYPE_WIN: 700 1.1 dyoung case PCI_ALLOC_REGTYPE_CBWIN: 701 1.1 dyoung return "window"; 702 1.1 dyoung case PCI_ALLOC_REGTYPE_VGA_EN: 703 1.1 dyoung return "vga-enable"; 704 1.1 dyoung default: 705 1.1 dyoung return "<unknown>"; 706 1.1 dyoung } 707 1.1 dyoung } 708 1.1 dyoung 709 1.1 dyoung static void 710 1.1 dyoung pci_alloc_print(pci_chipset_tag_t pc, const pci_alloc_t *pal) 711 1.1 dyoung { 712 1.1 dyoung int bus, dev, fun; 713 1.1 dyoung const pci_alloc_reg_t *r; 714 1.1 dyoung 715 1.1 dyoung pci_decompose_tag(pc, pal->pal_tag, &bus, &dev, &fun); 716 1.1 dyoung r = &pal->pal_reg[0]; 717 1.1 dyoung 718 1.1 dyoung aprint_normal("%s range [0x%08" PRIx64 ", 0x%08" PRIx64 ")" 719 1.1 dyoung " at %d.%d.%d %s%s 0x%02x\n", 720 1.1 dyoung (pal->pal_space == PCI_ALLOC_SPACE_IO) ? "IO" : "MMIO", 721 1.1 dyoung pal->pal_addr, pal->pal_addr + pal->pal_size, 722 1.1 dyoung bus, dev, fun, 723 1.1 dyoung (pal->pal_flags & PCI_ALLOC_F_PREFETCHABLE) ? "prefetchable " : "", 724 1.1 dyoung pci_alloc_regtype_string(pal->pal_type), 725 1.1 dyoung r->r_ofs); 726 1.1 dyoung } 727 1.1 dyoung 728 1.1 dyoung prop_dictionary_t pci_rsrc_dict = NULL; 729 1.1 dyoung 730 1.1 dyoung static bool 731 1.1 dyoung pci_range_record(pci_chipset_tag_t pc, prop_array_t rsvns, 732 1.1 dyoung pci_alloc_list_t *pals, pci_alloc_space_t space) 733 1.1 dyoung { 734 1.1 dyoung int bus, dev, fun, i; 735 1.1 dyoung prop_array_t regs; 736 1.1 dyoung prop_dictionary_t reg; 737 1.1 dyoung const pci_alloc_t *pal; 738 1.1 dyoung const pci_alloc_reg_t *r; 739 1.1 dyoung prop_dictionary_t rsvn; 740 1.1 dyoung 741 1.1 dyoung TAILQ_FOREACH(pal, pals, pal_link) { 742 1.1 dyoung bool ok = true; 743 1.1 dyoung 744 1.1 dyoung r = &pal->pal_reg[0]; 745 1.1 dyoung 746 1.1 dyoung if (pal->pal_space != space) 747 1.1 dyoung continue; 748 1.1 dyoung 749 1.1 dyoung if ((rsvn = prop_dictionary_create()) == NULL) 750 1.1 dyoung return false; 751 1.1 dyoung 752 1.1 dyoung if ((regs = prop_array_create()) == NULL) { 753 1.1 dyoung prop_object_release(rsvn); 754 1.1 dyoung return false; 755 1.1 dyoung } 756 1.1 dyoung 757 1.1 dyoung if (!prop_dictionary_set(rsvn, "regs", regs)) { 758 1.1 dyoung prop_object_release(rsvn); 759 1.1 dyoung prop_object_release(regs); 760 1.1 dyoung return false; 761 1.1 dyoung } 762 1.1 dyoung 763 1.1 dyoung for (i = 0; i < __arraycount(pal->pal_reg); i++) { 764 1.1 dyoung r = &pal->pal_reg[i]; 765 1.1 dyoung 766 1.1 dyoung if (r->r_mask == 0) 767 1.1 dyoung break; 768 1.1 dyoung 769 1.1 dyoung ok = (reg = prop_dictionary_create()) != NULL; 770 1.1 dyoung if (!ok) 771 1.1 dyoung break; 772 1.1 dyoung 773 1.1 dyoung ok = prop_dictionary_set_uint16(reg, "offset", 774 1.1 dyoung r->r_ofs) && 775 1.1 dyoung prop_dictionary_set_uint32(reg, "val", r->r_val) && 776 1.1 dyoung prop_dictionary_set_uint32(reg, "mask", 777 1.1 dyoung r->r_mask) && prop_array_add(regs, reg); 778 1.1 dyoung if (!ok) { 779 1.1 dyoung prop_object_release(reg); 780 1.1 dyoung break; 781 1.1 dyoung } 782 1.1 dyoung } 783 1.1 dyoung 784 1.1 dyoung pci_decompose_tag(pc, pal->pal_tag, &bus, &dev, &fun); 785 1.1 dyoung 786 1.1 dyoung ok = ok && 787 1.9 christos prop_dictionary_set_string_nocopy(rsvn, "type", 788 1.1 dyoung pci_alloc_regtype_string(pal->pal_type)) && 789 1.1 dyoung prop_dictionary_set_uint64(rsvn, "address", 790 1.1 dyoung pal->pal_addr) && 791 1.1 dyoung prop_dictionary_set_uint64(rsvn, "size", pal->pal_size) && 792 1.1 dyoung prop_dictionary_set_uint8(rsvn, "bus", bus) && 793 1.1 dyoung prop_dictionary_set_uint8(rsvn, "device", dev) && 794 1.1 dyoung prop_dictionary_set_uint8(rsvn, "function", fun) && 795 1.1 dyoung prop_array_add(rsvns, rsvn); 796 1.1 dyoung prop_object_release(rsvn); 797 1.1 dyoung if (!ok) 798 1.1 dyoung return false; 799 1.1 dyoung } 800 1.1 dyoung return true; 801 1.1 dyoung } 802 1.1 dyoung 803 1.1 dyoung prop_dictionary_t 804 1.1 dyoung pci_rsrc_filter(prop_dictionary_t rsrcs0, 805 1.1 dyoung bool (*predicate)(void *, prop_dictionary_t), void *arg) 806 1.1 dyoung { 807 1.1 dyoung int i, space; 808 1.1 dyoung prop_dictionary_t rsrcs; 809 1.1 dyoung prop_array_t rsvns; 810 1.1 dyoung ppath_t *op, *p; 811 1.1 dyoung 812 1.1 dyoung if ((rsrcs = prop_dictionary_copy(rsrcs0)) == NULL) 813 1.1 dyoung return NULL; 814 1.1 dyoung 815 1.1 dyoung for (space = 0; space < 2; space++) { 816 1.1 dyoung op = p = ppath_create(); 817 1.1 dyoung p = ppath_push_key(p, (space == 0) ? "memory" : "io"); 818 1.1 dyoung p = ppath_push_key(p, "bios-reservations"); 819 1.1 dyoung if (p == NULL) { 820 1.1 dyoung ppath_release(op); 821 1.1 dyoung return NULL; 822 1.1 dyoung } 823 1.1 dyoung if ((rsvns = ppath_lookup(rsrcs0, p)) == NULL) { 824 1.1 dyoung printf("%s: reservations not found\n", __func__); 825 1.1 dyoung ppath_release(p); 826 1.1 dyoung return NULL; 827 1.1 dyoung } 828 1.1 dyoung for (i = prop_array_count(rsvns); --i >= 0; ) { 829 1.1 dyoung prop_dictionary_t rsvn; 830 1.1 dyoung 831 1.1 dyoung if ((p = ppath_push_idx(p, i)) == NULL) { 832 1.1 dyoung printf("%s: ppath_push_idx\n", __func__); 833 1.1 dyoung ppath_release(op); 834 1.1 dyoung prop_object_release(rsrcs); 835 1.1 dyoung return NULL; 836 1.1 dyoung } 837 1.1 dyoung 838 1.1 dyoung rsvn = ppath_lookup(rsrcs0, p); 839 1.1 dyoung 840 1.1 dyoung KASSERT(rsvn != NULL); 841 1.1 dyoung 842 1.1 dyoung if (!(*predicate)(arg, rsvn)) { 843 1.1 dyoung ppath_copydel_object((prop_object_t)rsrcs0, 844 1.1 dyoung (prop_object_t *)&rsrcs, p); 845 1.1 dyoung } 846 1.1 dyoung 847 1.1 dyoung if ((p = ppath_pop(p, NULL)) == NULL) { 848 1.1 dyoung printf("%s: ppath_pop\n", __func__); 849 1.1 dyoung ppath_release(p); 850 1.1 dyoung prop_object_release(rsrcs); 851 1.1 dyoung return NULL; 852 1.1 dyoung } 853 1.1 dyoung } 854 1.1 dyoung ppath_release(op); 855 1.1 dyoung } 856 1.1 dyoung return rsrcs; 857 1.1 dyoung } 858 1.1 dyoung 859 1.1 dyoung void 860 1.1 dyoung pci_ranges_infer(pci_chipset_tag_t pc, int minbus, int maxbus, 861 1.1 dyoung bus_addr_t *iobasep, bus_size_t *iosizep, 862 1.1 dyoung bus_addr_t *membasep, bus_size_t *memsizep) 863 1.1 dyoung { 864 1.1 dyoung prop_dictionary_t iodict = NULL, memdict = NULL; 865 1.1 dyoung prop_array_t iorsvns, memrsvns; 866 1.1 dyoung struct range_infer_ctx ric = { 867 1.1 dyoung .ric_io_bottom = ~((bus_addr_t)0) 868 1.1 dyoung , .ric_io_top = 0 869 1.1 dyoung , .ric_mmio_bottom = ~((bus_addr_t)0) 870 1.1 dyoung , .ric_mmio_top = 0 871 1.1 dyoung , .ric_pals = TAILQ_HEAD_INITIALIZER(ric.ric_pals) 872 1.1 dyoung }; 873 1.1 dyoung const pci_alloc_t *pal; 874 1.1 dyoung 875 1.1 dyoung ric.ric_pc = pc; 876 1.1 dyoung pci_device_foreach_min(pc, minbus, maxbus, mmio_range_infer, &ric); 877 1.1 dyoung pci_device_foreach_min(pc, minbus, maxbus, io_range_infer, &ric); 878 1.1 dyoung if (membasep != NULL) 879 1.1 dyoung *membasep = ric.ric_mmio_bottom; 880 1.1 dyoung if (memsizep != NULL) 881 1.1 dyoung *memsizep = ric.ric_mmio_top - ric.ric_mmio_bottom; 882 1.1 dyoung if (iobasep != NULL) 883 1.1 dyoung *iobasep = ric.ric_io_bottom; 884 1.1 dyoung if (iosizep != NULL) 885 1.1 dyoung *iosizep = ric.ric_io_top - ric.ric_io_bottom; 886 1.1 dyoung aprint_verbose("%s: inferred %" PRIuMAX 887 1.1 dyoung " bytes of memory-mapped PCI space at 0x%" PRIxMAX "\n", __func__, 888 1.1 dyoung (uintmax_t)(ric.ric_mmio_top - ric.ric_mmio_bottom), 889 1.6 msaitoh (uintmax_t)ric.ric_mmio_bottom); 890 1.1 dyoung aprint_verbose("%s: inferred %" PRIuMAX 891 1.1 dyoung " bytes of PCI I/O space at 0x%" PRIxMAX "\n", __func__, 892 1.1 dyoung (uintmax_t)(ric.ric_io_top - ric.ric_io_bottom), 893 1.6 msaitoh (uintmax_t)ric.ric_io_bottom); 894 1.1 dyoung TAILQ_FOREACH(pal, &ric.ric_pals, pal_link) 895 1.1 dyoung pci_alloc_print(pc, pal); 896 1.1 dyoung 897 1.1 dyoung if ((memdict = prop_dictionary_create()) == NULL) { 898 1.1 dyoung aprint_error("%s: could not create PCI MMIO " 899 1.1 dyoung "resources dictionary\n", __func__); 900 1.1 dyoung } else if ((memrsvns = prop_array_create()) == NULL) { 901 1.1 dyoung aprint_error("%s: could not create PCI BIOS memory " 902 1.1 dyoung "reservations array\n", __func__); 903 1.1 dyoung } else if (!prop_dictionary_set(memdict, "bios-reservations", 904 1.1 dyoung memrsvns)) { 905 1.1 dyoung aprint_error("%s: could not record PCI BIOS memory " 906 1.1 dyoung "reservations array\n", __func__); 907 1.1 dyoung } else if (!pci_range_record(pc, memrsvns, &ric.ric_pals, 908 1.1 dyoung PCI_ALLOC_SPACE_MEM)) { 909 1.1 dyoung aprint_error("%s: could not record PCI BIOS memory " 910 1.1 dyoung "reservations\n", __func__); 911 1.1 dyoung } else if (!prop_dictionary_set_uint64(memdict, 912 1.1 dyoung "start", ric.ric_mmio_bottom) || 913 1.1 dyoung !prop_dictionary_set_uint64(memdict, "size", 914 1.1 dyoung ric.ric_mmio_top - ric.ric_mmio_bottom)) { 915 1.1 dyoung aprint_error("%s: could not record PCI memory min & max\n", 916 1.1 dyoung __func__); 917 1.1 dyoung } else if ((iodict = prop_dictionary_create()) == NULL) { 918 1.1 dyoung aprint_error("%s: could not create PCI I/O " 919 1.1 dyoung "resources dictionary\n", __func__); 920 1.1 dyoung } else if ((iorsvns = prop_array_create()) == NULL) { 921 1.1 dyoung aprint_error("%s: could not create PCI BIOS I/O " 922 1.1 dyoung "reservations array\n", __func__); 923 1.1 dyoung } else if (!prop_dictionary_set(iodict, "bios-reservations", 924 1.1 dyoung iorsvns)) { 925 1.1 dyoung aprint_error("%s: could not record PCI BIOS I/O " 926 1.1 dyoung "reservations array\n", __func__); 927 1.1 dyoung } else if (!pci_range_record(pc, iorsvns, &ric.ric_pals, 928 1.1 dyoung PCI_ALLOC_SPACE_IO)) { 929 1.1 dyoung aprint_error("%s: could not record PCI BIOS I/O " 930 1.1 dyoung "reservations\n", __func__); 931 1.1 dyoung } else if (!prop_dictionary_set_uint64(iodict, 932 1.1 dyoung "start", ric.ric_io_bottom) || 933 1.1 dyoung !prop_dictionary_set_uint64(iodict, "size", 934 1.1 dyoung ric.ric_io_top - ric.ric_io_bottom)) { 935 1.1 dyoung aprint_error("%s: could not record PCI I/O min & max\n", 936 1.1 dyoung __func__); 937 1.1 dyoung } else if ((pci_rsrc_dict = prop_dictionary_create()) == NULL) { 938 1.1 dyoung aprint_error("%s: could not create PCI resources dictionary\n", 939 1.1 dyoung __func__); 940 1.1 dyoung } else if (!prop_dictionary_set(pci_rsrc_dict, "memory", memdict) || 941 1.1 dyoung !prop_dictionary_set(pci_rsrc_dict, "io", iodict)) { 942 1.1 dyoung aprint_error("%s: could not record PCI memory- or I/O-" 943 1.1 dyoung "resources dictionary\n", __func__); 944 1.1 dyoung prop_object_release(pci_rsrc_dict); 945 1.1 dyoung pci_rsrc_dict = NULL; 946 1.1 dyoung } 947 1.1 dyoung 948 1.1 dyoung if (iodict != NULL) 949 1.1 dyoung prop_object_release(iodict); 950 1.1 dyoung if (memdict != NULL) 951 1.1 dyoung prop_object_release(memdict); 952 1.1 dyoung /* XXX release iorsvns, memrsvns */ 953 1.1 dyoung } 954 1.3 dyoung 955 1.3 dyoung static bool 956 1.3 dyoung pcibus_rsvn_predicate(void *arg, prop_dictionary_t rsvn) 957 1.3 dyoung { 958 1.3 dyoung struct pcibus_attach_args *pba = arg; 959 1.3 dyoung uint8_t bus; 960 1.3 dyoung 961 1.3 dyoung if (!prop_dictionary_get_uint8(rsvn, "bus", &bus)) 962 1.3 dyoung return false; 963 1.3 dyoung 964 1.3 dyoung return pba->pba_bus <= bus && bus <= pba->pba_sub; 965 1.3 dyoung } 966 1.3 dyoung 967 1.3 dyoung static bool 968 1.3 dyoung pci_rsvn_predicate(void *arg, prop_dictionary_t rsvn) 969 1.3 dyoung { 970 1.3 dyoung struct pci_attach_args *pa = arg; 971 1.3 dyoung uint8_t bus, device, function; 972 1.3 dyoung bool rc; 973 1.3 dyoung 974 1.3 dyoung rc = prop_dictionary_get_uint8(rsvn, "bus", &bus) && 975 1.3 dyoung prop_dictionary_get_uint8(rsvn, "device", &device) && 976 1.3 dyoung prop_dictionary_get_uint8(rsvn, "function", &function); 977 1.3 dyoung 978 1.3 dyoung if (!rc) 979 1.3 dyoung return false; 980 1.3 dyoung 981 1.3 dyoung return pa->pa_bus == bus && pa->pa_device == device && 982 1.3 dyoung pa->pa_function == function; 983 1.3 dyoung } 984 1.3 dyoung 985 1.3 dyoung void 986 1.3 dyoung device_pci_props_register(device_t dev, void *aux) 987 1.3 dyoung { 988 1.3 dyoung cfdata_t cf; 989 1.3 dyoung prop_dictionary_t dict; 990 1.3 dyoung 991 1.3 dyoung cf = (device_parent(dev) != NULL) ? device_cfdata(dev) : NULL; 992 1.3 dyoung #if 0 993 1.3 dyoung aprint_normal_dev(dev, "is%s a pci, parent %p, cf %p, ifattr %s\n", 994 1.3 dyoung device_is_a(dev, "pci") ? "" : " not", 995 1.4 chs device_parent(dev), 996 1.3 dyoung cf, 997 1.3 dyoung cf != NULL ? cfdata_ifattr(cf) : ""); 998 1.3 dyoung #endif 999 1.3 dyoung if (pci_rsrc_dict == NULL) 1000 1.3 dyoung return; 1001 1.3 dyoung 1002 1.3 dyoung if (!device_is_a(dev, "pci") && 1003 1.3 dyoung (cf == NULL || strcmp(cfdata_ifattr(cf), "pci") != 0)) 1004 1.3 dyoung return; 1005 1.3 dyoung 1006 1.3 dyoung dict = pci_rsrc_filter(pci_rsrc_dict, 1007 1.3 dyoung device_is_a(dev, "pci") ? &pcibus_rsvn_predicate 1008 1.3 dyoung : &pci_rsvn_predicate, aux); 1009 1.3 dyoung if (dict == NULL) 1010 1.3 dyoung return; 1011 1.3 dyoung (void)prop_dictionary_set(device_properties(dev), 1012 1.3 dyoung "pci-resources", dict); 1013 1.3 dyoung } 1014