2 * The PCI Library -- Virtual Emulated Config Space Access Functions
4 * Copyright (c) 2022 Pali Rohár
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
14 ioflg_to_pciflg(pciaddr_t ioflg)
18 if ((ioflg & PCI_IORESOURCE_TYPE_BITS) == PCI_IORESOURCE_IO)
19 flg = PCI_BASE_ADDRESS_SPACE_IO;
20 else if ((ioflg & PCI_IORESOURCE_TYPE_BITS) == PCI_IORESOURCE_MEM)
22 flg = PCI_BASE_ADDRESS_SPACE_MEMORY;
23 if (ioflg & PCI_IORESOURCE_MEM_64)
24 flg |= PCI_BASE_ADDRESS_MEM_TYPE_64;
26 flg |= PCI_BASE_ADDRESS_MEM_TYPE_32;
27 if (ioflg & PCI_IORESOURCE_PREFETCH)
28 flg |= PCI_BASE_ADDRESS_MEM_PREFETCH;
35 baseres_to_pcires(pciaddr_t addr, pciaddr_t ioflg, int *have_sec, u32 *sec_val)
37 u32 val = ioflg_to_pciflg(ioflg);
42 if ((val & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO && addr <= 0xffffffff)
43 val |= addr & PCI_BASE_ADDRESS_IO_MASK;
44 else if ((val & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY)
46 val |= addr & PCI_BASE_ADDRESS_MEM_MASK;
47 if ((val & PCI_BASE_ADDRESS_MEM_TYPE_64) && have_sec)
50 *sec_val = addr >> 32;
58 even_baseres_to_pcires(pciaddr_t addr, pciaddr_t ioflg)
60 return baseres_to_pcires(addr, ioflg, NULL, NULL);
64 odd_baseres_to_pcires(pciaddr_t addr0, pciaddr_t ioflg0, pciaddr_t addr, pciaddr_t ioflg)
68 baseres_to_pcires(addr0, ioflg0, &have_sec, &val);
70 val = baseres_to_pcires(addr, ioflg, NULL, NULL);
75 pci_emulated_read(struct pci_dev *d, int pos, byte *buf, int len)
77 u32 ht = PCI_HEADER_TYPE_NORMAL;
85 return pci_generic_block_read(d, pos, buf, len);
87 if (d->device_class == PCI_CLASS_BRIDGE_PCI)
88 ht = PCI_HEADER_TYPE_BRIDGE;
89 else if (d->device_class == PCI_CLASS_BRIDGE_CARDBUS)
90 ht = PCI_HEADER_TYPE_CARDBUS;
95 for (i = 0; i < 6; i++)
99 if ((d->flags[i] & PCI_IORESOURCE_TYPE_BITS) == PCI_IORESOURCE_IO)
100 val |= PCI_COMMAND_IO;
101 else if ((d->flags[i] & PCI_IORESOURCE_TYPE_BITS) == PCI_IORESOURCE_MEM)
102 val |= PCI_COMMAND_MEMORY;
106 val = (d->device_id << 16) | d->vendor_id;
108 case PCI_CLASS_REVISION:
109 val = (d->device_class << 16) | (d->prog_if << 8) | d->rev_id;
111 case PCI_CACHE_LINE_SIZE:
114 case PCI_BASE_ADDRESS_0:
115 val = even_baseres_to_pcires(d->base_addr[0], d->flags[0]);
117 case PCI_INTERRUPT_LINE:
118 val = (d->irq >= 0 && d->irq <= 0xff) ? d->irq : 0;
122 if ((pos & ~3) == PCI_BASE_ADDRESS_1 && (ht == PCI_HEADER_TYPE_NORMAL || ht == PCI_HEADER_TYPE_BRIDGE))
123 val = odd_baseres_to_pcires(d->base_addr[0], d->flags[0], d->base_addr[1], d->flags[1]);
125 if (ht == PCI_HEADER_TYPE_NORMAL)
128 case PCI_BASE_ADDRESS_2:
129 val = even_baseres_to_pcires(d->base_addr[2], d->flags[2]);
131 case PCI_BASE_ADDRESS_3:
132 val = odd_baseres_to_pcires(d->base_addr[2], d->flags[2], d->base_addr[3], d->flags[3]);
134 case PCI_BASE_ADDRESS_4:
135 val = even_baseres_to_pcires(d->base_addr[4], d->flags[4]);
137 case PCI_BASE_ADDRESS_5:
138 val = odd_baseres_to_pcires(d->base_addr[4], d->flags[4], d->base_addr[5], d->flags[5]);
140 case PCI_SUBSYSTEM_VENDOR_ID:
141 val = (d->subsys_id << 16) | d->subsys_vendor_id;
143 case PCI_ROM_ADDRESS:
144 val = d->rom_base_addr & PCI_ROM_ADDRESS_MASK;
146 val |= PCI_ROM_ADDRESS_ENABLE;
149 else if (ht == PCI_HEADER_TYPE_BRIDGE)
153 if (d->bridge_size[0])
154 val |= PCI_COMMAND_IO;
155 if (d->bridge_size[1] || d->bridge_size[2])
156 val |= PCI_COMMAND_MEMORY;
158 case PCI_PRIMARY_BUS:
162 if (d->bridge_size[0])
164 val = (((((d->bridge_base_addr[0] + d->bridge_size[0] - 1) >> 8) & PCI_IO_RANGE_MASK) << 8) & 0xff00) |
165 (((d->bridge_base_addr[0] >> 8) & PCI_IO_RANGE_MASK) & 0x00ff);
166 if ((d->bridge_flags[0] & PCI_IORESOURCE_IO_16BIT_ADDR) &&
167 d->bridge_base_addr[0] + d->bridge_size[0] - 1 <= 0xffff)
168 val |= (PCI_IO_RANGE_TYPE_16 << 8) | PCI_IO_RANGE_TYPE_16;
170 val |= (PCI_IO_RANGE_TYPE_32 << 8) | PCI_IO_RANGE_TYPE_32;
173 val = 0xff & PCI_IO_RANGE_MASK;
175 case PCI_MEMORY_BASE:
176 if (d->bridge_size[1])
177 val = (((((d->bridge_base_addr[1] + d->bridge_size[1] - 1) >> 16) & PCI_MEMORY_RANGE_MASK) << 16) & 0xffff0000) |
178 (((d->bridge_base_addr[1] >> 16) & PCI_MEMORY_RANGE_MASK) & 0x0000ffff);
180 val = 0xffff & PCI_MEMORY_RANGE_MASK;
182 case PCI_PREF_MEMORY_BASE:
183 if (d->bridge_size[2])
185 val = (((((d->bridge_base_addr[2] + d->bridge_size[2] - 1) >> 16) & PCI_PREF_RANGE_MASK) << 16) & 0xffff0000) |
186 (((d->bridge_base_addr[2] >> 16) & PCI_PREF_RANGE_MASK) & 0x0000ffff);
187 if ((d->bridge_flags[2] & PCI_IORESOURCE_MEM_64) ||
188 d->bridge_base_addr[2] + d->bridge_size[2] - 1 > 0xffffffff)
189 val |= (PCI_PREF_RANGE_TYPE_64 << 16) | PCI_PREF_RANGE_TYPE_64;
191 val |= (PCI_PREF_RANGE_TYPE_32 << 16) | PCI_PREF_RANGE_TYPE_32;
194 val = 0xffff & PCI_PREF_RANGE_MASK;
196 case PCI_PREF_BASE_UPPER32:
197 if (d->bridge_size[2])
198 val = d->bridge_base_addr[2] >> 32;
200 case PCI_PREF_LIMIT_UPPER32:
201 if (d->bridge_size[2])
202 val = (d->bridge_base_addr[2] + d->bridge_size[2] - 1) >> 32;
204 case PCI_IO_BASE_UPPER16:
205 if (d->bridge_size[0])
206 val = ((((d->bridge_base_addr[0] + d->bridge_size[0] - 1) >> 16) << 16) & 0xffff0000) |
207 ((d->bridge_base_addr[0] >> 16) & 0x0000ffff);
209 case PCI_ROM_ADDRESS1:
210 val = d->rom_base_addr & PCI_ROM_ADDRESS_MASK;
212 val |= PCI_ROM_ADDRESS_ENABLE;
215 else if (ht == PCI_HEADER_TYPE_CARDBUS)
219 if (d->bridge_size[0] || d->bridge_size[1])
220 val |= PCI_COMMAND_MEMORY;
221 if (d->bridge_size[2] || d->bridge_size[3])
222 val |= PCI_COMMAND_IO;
224 case PCI_CB_PRIMARY_BUS:
227 case PCI_CB_MEMORY_BASE_0:
228 if (d->bridge_size[0])
229 val = d->bridge_base_addr[0] & ~0xfff;
231 val = 0xffffffff & ~0xfff;
233 case PCI_CB_MEMORY_LIMIT_0:
234 if (d->bridge_size[0])
235 val = (d->bridge_base_addr[0] + d->bridge_size[0] - 1) & ~0xfff;
237 case PCI_CB_MEMORY_BASE_1:
238 if (d->bridge_size[1])
239 val = d->bridge_base_addr[1] & ~0xfff;
241 val = 0xffffffff & ~0xfff;
243 case PCI_CB_MEMORY_LIMIT_1:
244 if (d->bridge_size[1])
245 val = (d->bridge_base_addr[1] + d->bridge_size[1] - 1) & ~0xfff;
247 case PCI_CB_IO_BASE_0:
248 if (d->bridge_size[2])
250 val = d->bridge_base_addr[2] & PCI_CB_IO_RANGE_MASK;
251 if ((d->bridge_flags[2] & PCI_IORESOURCE_IO_16BIT_ADDR) ||
252 d->bridge_base_addr[2] + d->bridge_size[2] - 1 <= 0xffff)
253 val |= PCI_IO_RANGE_TYPE_16;
255 val |= PCI_IO_RANGE_TYPE_32;
258 val = 0x0000ffff & PCI_CB_IO_RANGE_MASK;
260 case PCI_CB_IO_LIMIT_0:
261 if (d->bridge_size[2])
262 val = (d->bridge_base_addr[2] + d->bridge_size[2] - 1) & PCI_CB_IO_RANGE_MASK;
264 case PCI_CB_IO_BASE_1:
265 if (d->bridge_size[3])
267 val = d->bridge_base_addr[3] & PCI_CB_IO_RANGE_MASK;
268 if ((d->bridge_flags[3] & PCI_IORESOURCE_IO_16BIT_ADDR) ||
269 d->bridge_base_addr[3] + d->bridge_size[3] - 1 <= 0xffff)
270 val |= PCI_IO_RANGE_TYPE_16;
272 val |= PCI_IO_RANGE_TYPE_32;
275 val = 0x0000ffff & PCI_CB_IO_RANGE_MASK;
277 case PCI_CB_IO_LIMIT_1:
278 if (d->bridge_size[3])
279 val = (d->bridge_base_addr[3] + d->bridge_size[3] - 1) & PCI_CB_IO_RANGE_MASK;
281 case PCI_CB_BRIDGE_CONTROL:
282 if (d->bridge_flags[0] & PCI_IORESOURCE_PREFETCH)
283 val |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
284 if (d->bridge_flags[1] & PCI_IORESOURCE_PREFETCH)
285 val |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
287 case PCI_CB_SUBSYSTEM_VENDOR_ID:
288 val = (d->subsys_id << 16) | d->subsys_vendor_id;
293 val = (val >> (8 * (pos & 3))) & ((1 << (len * 8)) - 1);
297 *(buf++) = val & 0xff;