|
| 1 | +/* |
| 2 | + * olpcpci.c - Low-level PCI config space access for OLPC systems |
| 3 | + * without the VSA PCI virtualization software. |
| 4 | + * |
| 5 | + * The AMD Geode chipset (GX2 processor, cs5536 I/O companion device) |
| 6 | + * has some I/O functions (display, southbridge, sound, USB HCIs, etc) |
| 7 | + * that more or less behave like PCI devices, but the hardware doesn't |
| 8 | + * directly implement the PCI configuration space headers. AMD provides |
| 9 | + * "VSA" (Virtual System Architecture) software that emulates PCI config |
| 10 | + * space for these devices, by trapping I/O accesses to PCI config register |
| 11 | + * (CF8/CFC) and running some code in System Management Mode interrupt state. |
| 12 | + * On the OLPC platform, we don't want to use that VSA code because |
| 13 | + * (a) it slows down suspend/resume, and (b) recompiling it requires special |
| 14 | + * compilers that are hard to get. So instead of letting the complex VSA |
| 15 | + * code simulate the PCI config registers for the on-chip devices, we |
| 16 | + * just simulate them the easy way, by inserting the code into the |
| 17 | + * pci_write_config and pci_read_config path. Most of the config registers |
| 18 | + * are read-only anyway, so the bulk of the simulation is just table lookup. |
| 19 | + */ |
| 20 | + |
| 21 | +//#include <linux/pci.h> |
| 22 | +//#include <linux/init.h> |
| 23 | +//#include <asm/olpc.h> |
| 24 | +//#include <asm/geode.h> |
| 25 | +//#include "pci.h" |
| 26 | + |
| 27 | +/* INCLUDES ******************************************************************/ |
| 28 | + |
| 29 | +#include <hal.h> |
| 30 | +#define NDEBUG |
| 31 | +#include <debug.h> |
| 32 | + |
| 33 | +/* GLOBALS *******************************************************************/ |
| 34 | + |
| 35 | +static int is_lx = FALSE; //FIXME: Support LX too! |
| 36 | + |
| 37 | +#define PCI_SLOT(dev, fn) ((((fn) & 0x7) << 5) | ((dev) & 0x1F)) |
| 38 | + |
| 39 | +/* |
| 40 | + * In the tables below, the first two line (8 longwords) are the |
| 41 | + * size masks that are used when the higher level PCI code determines |
| 42 | + * the size of the region by writing ~0 to a base address register |
| 43 | + * and reading back the result. |
| 44 | + * |
| 45 | + * The following lines are the values that are read during normal |
| 46 | + * PCI config access cycles, i.e. not after just having written |
| 47 | + * ~0 to a base address register. |
| 48 | + */ |
| 49 | + |
| 50 | +static const ULONG lxnb_hdr[] = { /* dev 1 function 0 - devfn = 8 */ |
| 51 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 52 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 53 | + |
| 54 | + 0x281022 , 0x2200005 , 0x6000021 , 0x80f808 , /* AMD Vendor ID */ |
| 55 | + 0x0 , 0x0 , 0x0 , 0x0 , /* No virtual registers, hence no BAR for them */ |
| 56 | + 0x0 , 0x0 , 0x0 , 0x28100b , |
| 57 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 58 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 59 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 60 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 61 | +}; |
| 62 | + |
| 63 | +static const ULONG gxnb_hdr[] = { /* dev 1 function 0 - devfn = 8 */ |
| 64 | + 0xfffffffd , 0x0 , 0x0 , 0x0 , |
| 65 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 66 | + |
| 67 | + 0x28100b , 0x2200005 , 0x6000021 , 0x80f808 , /* NSC Vendor ID */ |
| 68 | + 0xac1d , 0x0 , 0x0 , 0x0 , /* I/O BAR - base of virtual registers */ |
| 69 | + 0x0 , 0x0 , 0x0 , 0x28100b , |
| 70 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 71 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 72 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 73 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 74 | +}; |
| 75 | + |
| 76 | +static const ULONG lxfb_hdr[] = { /* dev 1 function 1 - devfn = 9 */ |
| 77 | + 0xff800008 , 0xffffc000 , 0xffffc000 , 0xffffc000 , |
| 78 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 79 | + |
| 80 | + 0x20811022 , 0x2200003 , 0x3000000 , 0x0 , /* AMD Vendor ID */ |
| 81 | + 0xfd000000 , 0xfe000000 , 0xfe004000 , 0xfe008000 , /* FB, GP, VG, DF */ |
| 82 | + 0xfe00c000 , 0x0 , 0x0 , 0x30100b , /* VIP */ |
| 83 | + 0x0 , 0x0 , 0x0 , 0x10e , /* INTA, IRQ14 for graphics accel */ |
| 84 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 85 | + 0x3d0 , 0x3c0 , 0xa0000 , 0x0 , /* VG IO, VG IO, EGA FB, MONO FB */ |
| 86 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 87 | +}; |
| 88 | + |
| 89 | +static const ULONG gxfb_hdr[] = { /* dev 1 function 1 - devfn = 9 */ |
| 90 | + 0xff800008 , 0xffffc000 , 0xffffc000 , 0xffffc000 , |
| 91 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 92 | + |
| 93 | + 0x30100b , 0x2200003 , 0x3000000 , 0x0 , /* NSC Vendor ID */ |
| 94 | + 0xfd000000 , 0xfe000000 , 0xfe004000 , 0xfe008000 , /* FB, GP, VG, DF */ |
| 95 | + 0x0 , 0x0 , 0x0 , 0x30100b , |
| 96 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 97 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 98 | + 0x3d0 , 0x3c0 , 0xa0000 , 0x0 , /* VG IO, VG IO, EGA FB, MONO FB */ |
| 99 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 100 | +}; |
| 101 | + |
| 102 | +static const ULONG aes_hdr[] = { /* dev 1 function 2 - devfn = 0xa */ |
| 103 | + 0xffffc000 , 0x0 , 0x0 , 0x0 , |
| 104 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 105 | + |
| 106 | + 0x20821022 , 0x2a00006 , 0x10100000 , 0x8 , /* NSC Vendor ID */ |
| 107 | + 0xfe010000 , 0x0 , 0x0 , 0x0 , /* AES registers */ |
| 108 | + 0x0 , 0x0 , 0x0 , 0x20821022 , |
| 109 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 110 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 111 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 112 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 113 | +}; |
| 114 | + |
| 115 | + |
| 116 | +static const ULONG isa_hdr[] = { /* dev f function 0 - devfn = 78 */ |
| 117 | + 0xfffffff9 , 0xffffff01 , 0xffffffc1 , 0xffffffe1 , |
| 118 | + 0xffffff81 , 0xffffffc1 , 0x0 , 0x0 , |
| 119 | + |
| 120 | + 0x20901022 , 0x2a00049 , 0x6010003 , 0x802000 , |
| 121 | + 0x18b1 , 0x1001 , 0x1801 , 0x1881 , /* SMB-8 GPIO-256 MFGPT-64 IRQ-32 */ |
| 122 | + 0x1401 , 0x1841 , 0x0 , 0x20901022 , /* PMS-128 ACPI-64 */ |
| 123 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 124 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 125 | + 0x0 , 0x0 , 0x0 , 0xaa5b , /* interrupt steering */ |
| 126 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 127 | +}; |
| 128 | + |
| 129 | +static const ULONG ac97_hdr[] = { /* dev f function 3 - devfn = 7b */ |
| 130 | + 0xffffff81 , 0x0 , 0x0 , 0x0 , |
| 131 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 132 | + |
| 133 | + 0x20931022 , 0x2a00041 , 0x4010001 , 0x0 , |
| 134 | + 0x1481 , 0x0 , 0x0 , 0x0 , /* I/O BAR-128 */ |
| 135 | + 0x0 , 0x0 , 0x0 , 0x20931022 , |
| 136 | + 0x0 , 0x0 , 0x0 , 0x205 , /* IntB , IRQ5 */ |
| 137 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 138 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 139 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 140 | +}; |
| 141 | + |
| 142 | +static const ULONG ohci_hdr[] = { /* dev f function 4 - devfn = 7c */ |
| 143 | + 0xfffff000 , 0x0 , 0x0 , 0x0 , |
| 144 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 145 | + |
| 146 | + 0x20941022 , 0x2300006 , 0xc031002 , 0x0 , |
| 147 | + 0xfe01a000 , 0x0 , 0x0 , 0x0 , /* MEMBAR-1000 */ |
| 148 | + 0x0 , 0x0 , 0x0 , 0x20941022 , |
| 149 | + 0x0 , 0x40 , 0x0 , 0x40a , /* CapPtr INT-D, IRQ A */ |
| 150 | + 0xc8020001 , 0x0 , 0x0 , 0x0 , /* Capabilities - 40 is R/O, 44 is mask 8103 (power control) */ |
| 151 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 152 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 153 | +}; |
| 154 | + |
| 155 | +static const ULONG ehci_hdr[] = { /* dev f function 5 - devfn = 7d */ |
| 156 | + 0xfffff000 , 0x0 , 0x0 , 0x0 , |
| 157 | + 0x0 , 0x0 , 0x0 , 0x0 , |
| 158 | + |
| 159 | + 0x20951022 , 0x2300006 , 0xc032002 , 0x0 , |
| 160 | + 0xfe01b000 , 0x0 , 0x0 , 0x0 , /* MEMBAR-1000 */ |
| 161 | + 0x0 , 0x0 , 0x0 , 0x20951022 , |
| 162 | + 0x0 , 0x40 , 0x0 , 0x40a , /* CapPtr INT-D, IRQ A */ |
| 163 | + 0xc8020001 , 0x0 , 0x0 , 0x0 , /* Capabilities - 40 is R/O, 44 is mask 8103 (power control) */ |
| 164 | +#if 0 |
| 165 | + 0x1 , 0x40080000 , 0x0 , 0x0 , /* EECP - see section 2.1.7 of EHCI spec */ |
| 166 | +#endif |
| 167 | + 0x01000001 , 0x00000000 , 0x0 , 0x0 , /* EECP - see section 2.1.7 of EHCI spec */ |
| 168 | + 0x2020 , 0x0 , 0x0 , 0x0 , /* (EHCI page 8) 60 SBRN (R/O), 61 FLADJ (R/W), PORTWAKECAP */ |
| 169 | +}; |
| 170 | + |
| 171 | +static ULONG ff_loc = ~0; |
| 172 | +static ULONG zero_loc = 0; |
| 173 | + |
| 174 | +static int bar_probing = 0; /* Set after a write of ~0 to a BAR */ |
| 175 | + |
| 176 | +static ULONG *hdr_addr(const ULONG *hdr, int reg) |
| 177 | +{ |
| 178 | + ULONG addr; |
| 179 | + |
| 180 | + /* |
| 181 | + * This is a little bit tricky. The header maps consist of |
| 182 | + * 0x20 bytes of size masks, followed by 0x70 bytes of header data. |
| 183 | + * In the normal case, when not probing a BAR's size, we want |
| 184 | + * to access the header data, so we add 0x20 to the reg offset, |
| 185 | + * thus skipping the size mask area. |
| 186 | + * In the BAR probing case, we want to access the size mask for |
| 187 | + * the BAR, so we subtract 0x10 (the config header offset for |
| 188 | + * BAR0), and don't skip the size mask area. |
| 189 | + */ |
| 190 | + |
| 191 | + addr = (ULONG)hdr + reg + (bar_probing ? -0x10 : 0x20); |
| 192 | + |
| 193 | + if ( ((ULONG)addr - (ULONG)hdr) >= 0x90 ) |
| 194 | + { |
| 195 | + DPRINT1("WARNING: out of bounds access: 0x%x, bar_probing %d, offset 0x%x\n", |
| 196 | + (ULONG)addr - (ULONG)hdr, bar_probing, reg); |
| 197 | + } |
| 198 | + |
| 199 | + bar_probing = 0; |
| 200 | + return (ULONG *)addr; |
| 201 | +} |
| 202 | + |
| 203 | +VOID NTAPI |
| 204 | +pci_olpc_read(ULONG bus, PCI_SLOT_NUMBER devfn, ULONG reg, ULONG len, PUCHAR value) |
| 205 | +{ |
| 206 | + ULONG *addr; |
| 207 | + |
| 208 | + /* |
| 209 | + * No device has config registers past 0x70, so we save table space |
| 210 | + * by not storing entries for the nonexistent registers |
| 211 | + */ |
| 212 | + if (reg >= 0x70) |
| 213 | + { |
| 214 | + addr = &zero_loc; |
| 215 | + } |
| 216 | + else |
| 217 | + { |
| 218 | + if (devfn.u.bits.DeviceNumber == 1) |
| 219 | + { |
| 220 | + switch (devfn.u.bits.FunctionNumber) |
| 221 | + { |
| 222 | + case 0 /* 0x8*/: |
| 223 | + addr = hdr_addr(is_lx ? lxnb_hdr : gxnb_hdr, reg); |
| 224 | + break; |
| 225 | + case 1 /*0x9*/: |
| 226 | + addr = hdr_addr(is_lx ? lxfb_hdr : gxfb_hdr, reg); |
| 227 | + break; |
| 228 | + case 2 /* 0xa*/: |
| 229 | + addr = is_lx ? hdr_addr(aes_hdr, reg) : &ff_loc; |
| 230 | + break; |
| 231 | + default: |
| 232 | + addr = &ff_loc; |
| 233 | + break; |
| 234 | + } |
| 235 | + } |
| 236 | + else |
| 237 | + if (devfn.u.bits.DeviceNumber == 0xF) |
| 238 | + { |
| 239 | + switch (devfn.u.bits.FunctionNumber) |
| 240 | + { |
| 241 | + case 0 /*0x78*/: |
| 242 | + addr = hdr_addr(isa_hdr, reg); |
| 243 | + break; |
| 244 | + case 3 /*0x7b*/: |
| 245 | + addr = hdr_addr(ac97_hdr, reg); |
| 246 | + break; |
| 247 | + case 4 /*0x7c*/: |
| 248 | + addr = hdr_addr(ohci_hdr, reg); |
| 249 | + break; |
| 250 | + case 5 /*0x7d*/: |
| 251 | + addr = hdr_addr(ehci_hdr, reg); |
| 252 | + break; |
| 253 | + default: |
| 254 | + addr = &zero_loc; |
| 255 | + break; |
| 256 | + } |
| 257 | + } |
| 258 | + else |
| 259 | + { |
| 260 | + addr = &ff_loc; |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + ASSERT(len == 1 || len == 2 || len == 4) |
| 265 | + RtlCopyMemory(value, addr, len); |
| 266 | +} |
| 267 | + |
| 268 | +VOID NTAPI |
| 269 | +pci_olpc_write(ULONG bus, PCI_SLOT_NUMBER devfn, ULONG reg, ULONG len, PUCHAR value) |
| 270 | +{ |
| 271 | + /* XXX we may want to extend this to simulate EHCI power management */ |
| 272 | + |
| 273 | + /* |
| 274 | + * Mostly we just discard writes, but if the write is a size probe |
| 275 | + * (i.e. writing ~0 to a BAR), we remember it and arrange to return |
| 276 | + * the appropriate size mask on the next read. This is cheating |
| 277 | + * to some extent, because it depends on the fact that the next |
| 278 | + * access after such a write will always be a read to the same BAR. |
| 279 | + */ |
| 280 | + |
| 281 | + if ((reg >= 0x10) && (reg < 0x2c)) { |
| 282 | + /* Write is to a BAR */ |
| 283 | + if (*(PULONG)value == ~0) |
| 284 | + bar_probing = 1; |
| 285 | + } else { |
| 286 | + /* |
| 287 | + * No warning on writes to ROM BAR, CMD, LATENCY_TIMER, |
| 288 | + * CACHE_LINE_SIZE, or PM registers. |
| 289 | + */ |
| 290 | + if ((reg != 0x30) && (reg != 0x04) && (reg != 0x0d) && |
| 291 | + (reg != 0x0c) && (reg != 0x44)) |
| 292 | + DbgPrint("OLPC PCI: Config write to devfn %x reg %x value %x\n", devfn, reg, *value); |
| 293 | + } |
| 294 | +} |
0 commit comments