Debian Codegraph @

src/device/pci_device.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 /*
4 * Originally based on the Linux kernel (drivers/pci/pci.c).
5 * PCI Bus Services, see include/linux/pci.h for further explanation.
6 */
7
8 #include <acpi/acpi.h>
9 #include <assert.h>
10 #include <cbmem.h>
11 #include <device/pci_ops.h>
12 #include <bootmode.h>
13 #include <console/console.h>
14 #include <cpu/cpu.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <delay.h>
18 #include <device/cardbus.h>
19 #include <device/device.h>
20 #include <device/pci.h>
21 #include <device/pci_ids.h>
22 #include <device/pcix.h>
23 #include <device/pciexp.h>
24 #include <lib.h>
25 #include <pc80/i8259.h>
26 #include <security/vboot/vbnv.h>
27 #include <timestamp.h>
28 #include <types.h>
29
30 u8 pci_moving_config8(struct device *dev, unsigned int reg)
31 {
32 u8 value, ones, zeroes;
33
34 value = pci_read_config8(dev, reg);
35
36 pci_write_config8(dev, reg, 0xff);
37 ones = pci_read_config8(dev, reg);
38
39 pci_write_config8(dev, reg, 0x00);
40 zeroes = pci_read_config8(dev, reg);
41
42 pci_write_config8(dev, reg, value);
43
44 return ones ^ zeroes;
45 }
46
47 u16 pci_moving_config16(struct device *dev, unsigned int reg)
48 {
49 u16 value, ones, zeroes;
50
51 value = pci_read_config16(dev, reg);
52
53 pci_write_config16(dev, reg, 0xffff);
54 ones = pci_read_config16(dev, reg);
55
56 pci_write_config16(dev, reg, 0x0000);
57 zeroes = pci_read_config16(dev, reg);
58
59 pci_write_config16(dev, reg, value);
60
61 return ones ^ zeroes;
62 }
63
64 u32 pci_moving_config32(struct device *dev, unsigned int reg)
65 {
66 u32 value, ones, zeroes;
67
68 value = pci_read_config32(dev, reg);
69
70 pci_write_config32(dev, reg, 0xffffffff);
71 ones = pci_read_config32(dev, reg);
72
73 pci_write_config32(dev, reg, 0x00000000);
74 zeroes = pci_read_config32(dev, reg);
75
76 pci_write_config32(dev, reg, value);
77
78 return ones ^ zeroes;
79 }
80
81 /**
82 * Given a device and register, read the size of the BAR for that register.
83 *
84 * @param dev Pointer to the device structure.
85 * @param index Address of the PCI configuration register.
86 * @return TODO
87 */
88 struct resource *pci_get_resource(struct device *dev, unsigned long index)
89 {
90 struct resource *resource;
91 unsigned long value, attr;
92 resource_t moving, limit;
93
94 /* Initialize the resources to nothing. */
95 resource = new_resource(dev, index);
96
97 /* Get the initial value. */
98 value = pci_read_config32(dev, index);
99
100 /* See which bits move. */
101 moving = pci_moving_config32(dev, index);
102
103 /* Initialize attr to the bits that do not move. */
104 attr = value & ~moving;
105
106 /* If it is a 64bit resource look at the high half as well. */
107 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
108 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
109 PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
110 /* Find the high bits that move. */
111 moving |=
112 ((resource_t)pci_moving_config32(dev, index + 4)) << 32;
113 }
114
115 /* Find the resource constraints.
116 * Start by finding the bits that move. From there:
117 * - Size is the least significant bit of the bits that move.
118 * - Limit is all of the bits that move plus all of the lower bits.
119 * See PCI Spec 6.2.5.1.
120 */
121 limit = 0;
122 if (moving) {
123 resource->size = 1;
124 resource->align = resource->gran = 0;
125 while (!(moving & resource->size)) {
126 resource->size <<= 1;
127 resource->align += 1;
128 resource->gran += 1;
129 }
130 resource->limit = limit = moving | (resource->size - 1);
131
132 if (pci_base_address_is_memory_space(attr)) {
133 /* Page-align to allow individual mapping of devices. */
134 if (resource->align < 12)
135 resource->align = 12;
136 }
137 }
138
139 /*
140 * Some broken hardware has read-only registers that do not
141 * really size correctly.
142 *
143 * Example: the Acer M7229 has BARs 1-4 normally read-only,
144 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
145 * by writing 0xffffffff to it, it will read back as 0x1f1 -- which
146 * is a violation of the spec.
147 *
148 * We catch this case and ignore it by observing which bits move.
149 *
150 * This also catches the common case of unimplemented registers
151 * that always read back as 0.
152 */
153 if (moving == 0) {
154 if (value != 0) {
155 printk(BIOS_DEBUG, "%s register %02lx(%08lx), read-only ignoring it\n",
156 dev_path(dev), index, value);
157 }
158 resource->flags = 0;
159 } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
160 /* An I/O mapped base address. */
161 resource->flags |= IORESOURCE_IO;
162 /* I don't want to deal with 32bit I/O resources. */
163 resource->limit = 0xffff;
164 } else {
165 /* A Memory mapped base address. */
166 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
167 resource->flags |= IORESOURCE_MEM;
168 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
169 resource->flags |= IORESOURCE_PREFETCH;
170 if (CONFIG(PCIEXP_HOTPLUG_PREFETCH_MEM_ABOVE_4G)
171 && dev_path_hotplug(dev))
172 resource->flags |= IORESOURCE_ABOVE_4G;
173 }
174 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
175 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
176 /* 32bit limit. */
177 resource->limit = 0xffffffffUL;
178 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
179 /* 1MB limit. */
180 resource->limit = 0x000fffffUL;
181 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
182 /* 64bit limit. */
183 resource->limit = 0xffffffffffffffffULL;
184 resource->flags |= IORESOURCE_PCI64;
185 } else {
186 /* Invalid value. */
187 printk(BIOS_ERR, "Broken BAR with value %lx\n", attr);
188 printk(BIOS_ERR, " on dev %s at index %02lx\n",
189 dev_path(dev), index);
190 resource->flags = 0;
191 }
192 }
193
194 /* Don't let the limit exceed which bits can move. */
195 if (resource->limit > limit)
196 resource->limit = limit;
197
198 return resource;
199 }
200
201 /**
202 * Given a device and an index, read the size of the BAR for that register.
203 *
204 * @param dev Pointer to the device structure.
205 * @param index Address of the PCI configuration register.
206 */
207 static void pci_get_rom_resource(struct device *dev, unsigned long index)
208 {
209 struct resource *resource;
210 unsigned long value;
211 resource_t moving;
212
213 /* Initialize the resources to nothing. */
214 resource = new_resource(dev, index);
215
216 /* Get the initial value. */
217 value = pci_read_config32(dev, index);
218
219 /* See which bits move. */
220 moving = pci_moving_config32(dev, index);
221
222 /* Clear the Enable bit. */
223 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
224
225 /* Find the resource constraints.
226 * Start by finding the bits that move. From there:
227 * - Size is the least significant bit of the bits that move.
228 * - Limit is all of the bits that move plus all of the lower bits.
229 * See PCI Spec 6.2.5.1.
230 */
231 if (moving) {
232 resource->size = 1;
233 resource->align = resource->gran = 0;
234 while (!(moving & resource->size)) {
235 resource->size <<= 1;
236 resource->align += 1;
237 resource->gran += 1;
238 }
239 resource->limit = moving | (resource->size - 1);
240 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
241 } else {
242 if (value != 0) {
243 printk(BIOS_DEBUG, "%s register %02lx(%08lx), read-only ignoring it\n",
244 dev_path(dev), index, value);
245 }
246 resource->flags = 0;
247 }
248 compact_resources(dev);
249 }
250
251 /**
252 * Given a device, read the size of the MSI-X table.
253 *
254 * @param dev Pointer to the device structure.
255 * @return MSI-X table size or 0 if not MSI-X capable device
256 */
257 size_t pci_msix_table_size(struct device *dev)
258 {
259 const size_t pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
260 if (!pos)
261 return 0;
262
263 const u16 control = pci_read_config16(dev, pos + PCI_MSIX_FLAGS);
264 return (control & PCI_MSIX_FLAGS_QSIZE) + 1;
265 }
266
267 /**
268 * Given a device, return the table offset and bar the MSI-X tables resides in.
269 *
270 * @param dev Pointer to the device structure.
271 * @param offset Returned value gives the offset in bytes inside the PCI BAR.
272 * @param idx The returned value is the index of the PCI_BASE_ADDRESS register
273 * the MSI-X table is located in.
274 * @return Zero on success
275 */
276 int pci_msix_table_bar(struct device *dev, u32 *offset, u8 *idx)
277 {
278 const size_t pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
279 if (!pos || !offset || !idx)
280 return 1;
281
282 *offset = pci_read_config32(dev, pos + PCI_MSIX_TABLE);
283 *idx = (u8)(*offset & PCI_MSIX_PBA_BIR);
284 *offset &= PCI_MSIX_PBA_OFFSET;
285
286 return 0;
287 }
288
289 /**
290 * Given a device, return a msix_entry pointer or NULL if no table was found.
291 *
292 * @param dev Pointer to the device structure.
293 *
294 * @return NULL on error
295 */
296 struct msix_entry *pci_msix_get_table(struct device *dev)
297 {
298 struct resource *res;
299 u32 offset;
300 u8 idx;
301
302 if (pci_msix_table_bar(dev, &offset, &idx))
303 return NULL;
304
305 if (idx > 5)
306 return NULL;
307
308 res = probe_resource(dev, idx * 4 + PCI_BASE_ADDRESS_0);
309 if (!res || !res->base || offset >= res->size)
310 return NULL;
311
312 if ((res->flags & IORESOURCE_PCI64) &&
313 (uintptr_t)res->base != res->base)
314 return NULL;
315
316 return (struct msix_entry *)((uintptr_t)res->base + offset);
317 }
318
319 static unsigned int get_rebar_offset(const struct device *dev, unsigned long index)
320 {
321 uint32_t offset = pciexp_find_extended_cap(dev, PCIE_EXT_CAP_RESIZABLE_BAR, 0);
322 if (!offset)
323 return 0;
324
325 /* Convert PCI_BASE_ADDRESS_0, ..._1, ..._2 into 0, 1, 2... */
326 const unsigned int find_bar_idx = (index - PCI_BASE_ADDRESS_0) /
327 sizeof(uint32_t);
328
329 /* Although all of the Resizable BAR Control Registers contain an
330 "NBARs" field, it is only valid in the Control Register for BAR 0 */
331 const uint32_t rebar_ctrl0 = pci_read_config32(dev, offset + PCI_REBAR_CTRL_OFFSET);
332 const unsigned int nbars = (rebar_ctrl0 & PCI_REBAR_CTRL_NBARS_MASK) >>
333 PCI_REBAR_CTRL_NBARS_SHIFT;
334
335 for (unsigned int i = 0; i < nbars; i++, offset += sizeof(uint64_t)) {
336 const uint32_t rebar_ctrl = pci_read_config32(
337 dev, offset + PCI_REBAR_CTRL_OFFSET);
338 const uint32_t bar_idx = rebar_ctrl & PCI_REBAR_CTRL_IDX_MASK;
339 if (bar_idx == find_bar_idx)
340 return offset;
341 }
342
343 return 0;
344 }
345
346 /* Bit 20 = 1 MiB, bit 21 = 2 MiB, bit 22 = 4 MiB, ... bit 63 = 8 EiB */
347 static uint64_t get_rebar_sizes_mask(const struct device *dev,
348 unsigned long index)
349 {
350 uint64_t size_mask = 0ULL;
351 const uint32_t offset = get_rebar_offset(dev, index);
352 if (!offset)
353 return 0;
354
355 /* Get 1 MB - 128 TB support from CAP register */
356 const uint32_t cap = pci_read_config32(dev, offset + PCI_REBAR_CAP_OFFSET);
357 /* Shift the bits from 4-31 to 0-27 (i.e., down by 4 bits) */
358 size_mask |= ((cap & PCI_REBAR_CAP_SIZE_MASK) >> 4);
359
360 /* Get 256 TB - 8 EB support from CTRL register and store it in bits 28-43 */
361 const uint64_t ctrl = pci_read_config32(dev, offset + PCI_REBAR_CTRL_OFFSET);
362 /* Shift ctrl mask from bit 16 to bit 28, so that the two
363 masks (fom cap and ctrl) form a contiguous bitmask when
364 concatenated (i.e., up by 12 bits). */
365 size_mask |= ((ctrl & PCI_REBAR_CTRL_SIZE_MASK) << 12);
366
367 /* Now that the mask occupies bits 0-43, shift it up to 20-63, so they
368 represent the actual powers of 2. */
369 return size_mask << 20;
370 }
371
372 static void pci_store_rebar_size(const struct device *dev,
373 const struct resource *resource)
374 {
375 const unsigned int num_bits = __fls64(resource->size);
376 const uint32_t offset = get_rebar_offset(dev, resource->index);
377 if (!offset)
378 return;
379
380 pci_update_config32(dev, offset + PCI_REBAR_CTRL_OFFSET,
381 ~PCI_REBAR_CTRL_SIZE_MASK,
382 num_bits << PCI_REBAR_CTRL_SIZE_SHIFT);
383 }
384
385 static void configure_adjustable_base(const struct device *dev,
386 unsigned long index,
387 struct resource *res)
388 {
389 /*
390 * Excerpt from an implementation note from the PCIe spec:
391 *
392 * System software uses this capability in place of the above mentioned
393 * method of determining the resource size[0], and prior to assigning
394 * the base address to the BAR. Potential usable resource sizes are
395 * reported by the Function via its Resizable BAR Capability and Control
396 * registers. It is intended that the software allocate the largest of
397 * the reported sizes that it can, since allocating less address space
398 * than the largest reported size can result in lower
399 * performance. Software then writes the size to the Resizable BAR
400 * Control register for the appropriate BAR for the Function. Following
401 * this, the base address is written to the BAR.
402 *
403 * [0] Referring to using the moving bits in the BAR to determine the
404 * requested size of the MMIO region
405 */
406 const uint64_t size_mask = get_rebar_sizes_mask(dev, index);
407 if (!size_mask)
408 return;
409
410 int max_requested_bits = __fls64(size_mask);
411 if (max_requested_bits > CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS) {
412 printk(BIOS_WARNING, "Device %s requests a BAR with"
413 " %u bits of address space, which coreboot is not"
414 " configured to hand out, truncating to %u bits\n",
415 dev_path(dev), max_requested_bits,
416 CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS);
417 max_requested_bits = CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS;
418 }
419
420 if (!(res->flags & IORESOURCE_PCI64) && max_requested_bits > 32) {
421 printk(BIOS_ERR, "Resizable BAR requested"
422 " above 32 bits, but PCI function reported a"
423 " 32-bit BAR.");
424 return;
425 }
426
427 /* Configure the resource parameters for the adjustable BAR */
428 res->size = 1ULL << max_requested_bits;
429 res->align = max_requested_bits;
430 res->gran = max_requested_bits;
431 res->limit = (res->flags & IORESOURCE_PCI64) ? UINT64_MAX : UINT32_MAX;
432 res->flags |= (res->flags & IORESOURCE_PCI64) ?
433 IORESOURCE_PCIE_RESIZABLE_BAR | IORESOURCE_ABOVE_4G :
434 IORESOURCE_PCIE_RESIZABLE_BAR;
435
436 printk(BIOS_INFO, "%s: Adjusting resource index %lu: base: %llx size: %llx "
437 "align: %d gran: %d limit: %llx\n",
438 dev_path(dev), res->index, res->base, res->size,
439 res->align, res->gran, res->limit);
440 }
441
442 /**
443 * Read the base address registers for a given device.
444 *
445 * @param dev Pointer to the dev structure.
446 * @param howmany How many registers to read (6 for device, 2 for bridge).
447 */
448 static void pci_read_bases(struct device *dev, unsigned int howmany)
449 {
450 unsigned long index;
451
452 for (index = PCI_BASE_ADDRESS_0;
453 (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
454 struct resource *resource;
455 resource = pci_get_resource(dev, index);
456
457 const bool is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE) != 0;
458 if (CONFIG(PCIEXP_SUPPORT_RESIZABLE_BARS) && is_pcie)
459 configure_adjustable_base(dev, index, resource);
460
461 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
462 }
463
464 compact_resources(dev);
465 }
466
467 static void pci_record_bridge_resource(struct device *dev, resource_t moving,
468 unsigned int index, unsigned long type)
469 {
470 struct resource *resource;
471 unsigned long gran;
472 resource_t step;
473
474 resource = NULL;
475
476 if (!moving)
477 return;
478
479 /* Initialize the constraints on the current bus. */
480 resource = new_resource(dev, index);
481 resource->size = 0;
482 gran = 0;
483 step = 1;
484 while ((moving & step) == 0) {
485 gran += 1;
486 step <<= 1;
487 }
488 resource->gran = gran;
489 resource->align = gran;
490 resource->limit = moving | (step - 1);
491 resource->flags = type | IORESOURCE_PCI_BRIDGE |
492 IORESOURCE_BRIDGE;
493 }
494
495 static void pci_bridge_read_bases(struct device *dev)
496 {
497 resource_t moving_base, moving_limit, moving;
498
499 /* See if the bridge I/O resources are implemented. */
500 moving_base = ((u32)pci_moving_config8(dev, PCI_IO_BASE)) << 8;
501 moving_base |=
502 ((u32)pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
503
504 moving_limit = ((u32)pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
505 moving_limit |=
506 ((u32)pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
507
508 moving = moving_base & moving_limit;
509
510 /* Initialize the I/O space constraints on the current bus. */
511 pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
512
513 /* See if the bridge prefmem resources are implemented. */
514 moving_base =
515 ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
516 moving_base |=
517 ((resource_t)pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
518
519 moving_limit =
520 ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
521 moving_limit |=
522 ((resource_t)pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
523
524 moving = moving_base & moving_limit;
525 /* Initialize the prefetchable memory constraints on the current bus. */
526 pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
527 IORESOURCE_MEM | IORESOURCE_PREFETCH);
528
529 /* See if the bridge mem resources are implemented. */
530 moving_base = ((u32)pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
531 moving_limit = ((u32)pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
532
533 moving = moving_base & moving_limit;
534
535 /* Initialize the memory resources on the current bus. */
536 pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
537 IORESOURCE_MEM);
538
539 compact_resources(dev);
540 }
541
542 void pci_dev_read_resources(struct device *dev)
543 {
544 pci_read_bases(dev, 6);
545 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
546
547 #if CONFIG(PCIEXP_PLUGIN_SUPPORT)
548 /* Check for SR-IOV BARs if we have PCIe support */
549 pciexp_dev_read_resources(dev);
550 #endif
551 }
552
553 void pci_bus_read_resources(struct device *dev)
554 {
555 pci_bridge_read_bases(dev);
556 pci_read_bases(dev, 2);
557 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
558 }
559
560 void pci_domain_read_resources(struct device *dev)
561 {
562 struct resource *res;
563
564 /* Initialize the system-wide I/O space constraints. */
565 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
566 res->limit = 0xffffUL;
567 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
568 IORESOURCE_ASSIGNED;
569
570 /*
571 * Initialize 32-bit memory resource constraints.
572 *
573 * There are often undeclared chipset resources in lower memory
574 * and memory right below the 4G barrier. Hence, only allow
575 * one big range from cbmem_top to the configured limit.
576 */
577 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
578 res->base = cbmem_top();
579 res->limit = CONFIG_DOMAIN_RESOURCE_32BIT_LIMIT - 1;
580 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
581 IORESOURCE_ASSIGNED;
582
583 /* Initialize 64-bit memory resource constraints above 4G. */
584 res = new_resource(dev, IOINDEX_SUBTRACTIVE(2, 0));
585 res->base = 4ULL * GiB;
586 res->limit = (1ULL << soc_phys_address_size()) - 1;
587 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
588 IORESOURCE_ASSIGNED;
589 }
590
591 void pci_domain_set_resources(struct device *dev)
592 {
593 assign_resources(dev->downstream);
594 }
595
596 static void pci_store_resource(const struct device *const dev,
597 const struct resource *const resource)
598 {
599 unsigned long base_lo, base_hi;
600
601 base_lo = resource->base & 0xffffffff;
602 base_hi = (resource->base >> 32) & 0xffffffff;
603
604 /*
605 * Some chipsets allow us to set/clear the I/O bit
606 * (e.g. VIA 82C686A). So set it to be safe.
607 */
608 if (resource->flags & IORESOURCE_IO)
609 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
610
611 pci_write_config32(dev, resource->index, base_lo);
612 if (resource->flags & IORESOURCE_PCI64)
613 pci_write_config32(dev, resource->index + 4, base_hi);
614 }
615
616 static void pci_store_bridge_resource(const struct device *const dev,
617 struct resource *const resource)
618 {
619 resource_t base, end;
620
621 /*
622 * PCI bridges have no enable bit. They are disabled if the base of
623 * the range is greater than the limit. If the size is zero, disable
624 * by setting the base = limit and end = limit - 2^gran.
625 */
626 if (resource->size == 0) {
627 base = resource->limit;
628 end = resource->limit - (1 << resource->gran);
629 resource->base = base;
630 } else {
631 base = resource->base;
632 end = resource_end(resource);
633 }
634
635 if (resource->index == PCI_IO_BASE) {
636 /* Set the I/O ranges. */
637 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
638 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
639 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
640 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
641 } else if (resource->index == PCI_MEMORY_BASE) {
642 /* Set the memory range. */
643 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
644 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
645 } else if (resource->index == PCI_PREF_MEMORY_BASE) {
646 /* Set the prefetchable memory range. */
647 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
648 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
649 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
650 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
651 } else {
652 /* Don't let me think I stored the resource. */
653 resource->flags &= ~IORESOURCE_STORED;
654 printk(BIOS_ERR, "invalid resource->index %lx\n", resource->index);
655 }
656 }
657
658 static void pci_set_resource(struct device *dev, struct resource *resource)
659 {
660 /* Make certain the resource has actually been assigned a value. */
661 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
662 if (resource->flags & IORESOURCE_BRIDGE) {
663 /* If a bridge resource has no value assigned,
664 we can treat it like an empty resource. */
665 resource->size = 0;
666 } else {
667 printk(BIOS_ERR, "%s %02lx %s size: 0x%010llx not assigned\n",
668 dev_path(dev), resource->index,
669 resource_type(resource), resource->size);
670 return;
671 }
672 }
673
674 /* If this resource is fixed don't worry about it. */
675 if (resource->flags & IORESOURCE_FIXED)
676 return;
677
678 /* If I have already stored this resource don't worry about it. */
679 if (resource->flags & IORESOURCE_STORED)
680 return;
681
682 /* If the resource is subtractive don't worry about it. */
683 if (resource->flags & IORESOURCE_SUBTRACTIVE)
684 return;
685
686 /* Only handle PCI memory and I/O resources for now. */
687 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
688 return;
689
690 /* Enable the resources in the command register. */
691 if (resource->size) {
692 if (resource->flags & IORESOURCE_MEM)
693 dev->command |= PCI_COMMAND_MEMORY;
694 if (resource->flags & IORESOURCE_IO)
695 dev->command |= PCI_COMMAND_IO;
696 if (resource->flags & IORESOURCE_PCI_BRIDGE &&
697 CONFIG(PCI_SET_BUS_MASTER_PCI_BRIDGES))
698 dev->command |= PCI_COMMAND_MASTER;
699 }
700
701 /* Now store the resource. */
702 resource->flags |= IORESOURCE_STORED;
703
704 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
705 if (CONFIG(PCIEXP_SUPPORT_RESIZABLE_BARS) &&
706 (resource->flags & IORESOURCE_PCIE_RESIZABLE_BAR))
707 pci_store_rebar_size(dev, resource);
708
709 pci_store_resource(dev, resource);
710
711 } else {
712 pci_store_bridge_resource(dev, resource);
713 }
714
715 report_resource_stored(dev, resource, "");
716 }
717
718 void pci_dev_set_resources(struct device *dev)
719 {
720 struct resource *res;
721 u8 line;
722
723 for (res = dev->resource_list; res; res = res->next)
724 pci_set_resource(dev, res);
725
726 if (dev->downstream && dev->downstream->children)
727 assign_resources(dev->downstream);
728
729 /* Set a default latency timer. */
730 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
731
732 /* Set a default secondary latency timer. */
733 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE)
734 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
735
736 /* Zero the IRQ settings. */
737 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
738 if (line)
739 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
740
741 /* Set the cache line size, so far 64 bytes is good for everyone. */
742 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
743 }
744
745 void pci_dev_enable_resources(struct device *dev)
746 {
747 const struct pci_operations *ops = NULL;
748 u16 command;
749
750 /* Set the subsystem vendor and device ID for mainboard devices. */
751 if (dev->ops)
752 ops = dev->ops->ops_pci;
753 if (dev->on_mainboard && ops && ops->set_subsystem) {
754 if (CONFIG_SUBSYSTEM_VENDOR_ID)
755 dev->subsystem_vendor = CONFIG_SUBSYSTEM_VENDOR_ID;
756 else if (!dev->subsystem_vendor)
757 dev->subsystem_vendor = pci_read_config16(dev,
758 PCI_VENDOR_ID);
759 if (CONFIG_SUBSYSTEM_DEVICE_ID)
760 dev->subsystem_device = CONFIG_SUBSYSTEM_DEVICE_ID;
761 else if (!dev->subsystem_device)
762 dev->subsystem_device = pci_read_config16(dev,
763 PCI_DEVICE_ID);
764
765 printk(BIOS_DEBUG, "%s subsystem <- %04x/%04x\n",
766 dev_path(dev), dev->subsystem_vendor,
767 dev->subsystem_device);
768 ops->set_subsystem(dev, dev->subsystem_vendor,
769 dev->subsystem_device);
770 }
771 command = pci_read_config16(dev, PCI_COMMAND);
772 command |= dev->command;
773
774 /* v3 has
775 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
776 */
777
778 printk(BIOS_DEBUG, "%s cmd <- %02x\n", dev_path(dev), command);
779 pci_write_config16(dev, PCI_COMMAND, command);
780 }
781
782 void pci_bus_enable_resources(struct device *dev)
783 {
784 u16 ctrl;
785
786 /*
787 * Enable I/O in command register if there is VGA card
788 * connected with (even it does not claim I/O resource).
789 */
790 if (dev->downstream->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
791 dev->command |= PCI_COMMAND_IO;
792 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
793 ctrl |= dev->downstream->bridge_ctrl;
794 ctrl |= (PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR); /* Error check. */
795 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
796 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
797
798 pci_dev_enable_resources(dev);
799 }
800
801 void pci_bus_reset(struct bus *bus)
802 {
803 u16 ctl;
804
805 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
806 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
807 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
808 mdelay(10);
809
810 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
811 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
812 delay(1);
813 }
814
815 void pci_dev_set_subsystem(struct device *dev, unsigned int vendor,
816 unsigned int device)
817 {
818 uint8_t offset;
819
820 /* Header type */
821 switch (dev->hdr_type & 0x7f) {
822 case PCI_HEADER_TYPE_NORMAL:
823 offset = PCI_SUBSYSTEM_VENDOR_ID;
824 break;
825 case PCI_HEADER_TYPE_BRIDGE:
826 offset = pci_find_capability(dev, PCI_CAP_ID_SSVID);
827 if (!offset)
828 return;
829 offset += 4; /* Vendor ID at offset 4 */
830 break;
831 case PCI_HEADER_TYPE_CARDBUS:
832 offset = PCI_CB_SUBSYSTEM_VENDOR_ID;
833 break;
834 default:
835 return;
836 }
837
838 if (!vendor || !device) {
839 pci_write_config32(dev, offset,
840 pci_read_config32(dev, PCI_VENDOR_ID));
841 } else {
842 pci_write_config32(dev, offset,
843 ((device & 0xffff) << 16) | (vendor & 0xffff));
844 }
845 }
846
847 /** Default handler: only runs the relevant PCI BIOS. */
848 void pci_dev_init(struct device *dev)
849 {
850 if (CONFIG(VGA_ROM_RUN))
851 pci_rom_run(dev);
852 }
853
854 /** Default device operation for PCI devices */
855 struct pci_operations pci_dev_ops_pci = {
856 .set_subsystem = pci_dev_set_subsystem,
857 };
858
859 struct device_operations default_pci_ops_dev = {
860 .read_resources = pci_dev_read_resources,
861 .set_resources = pci_dev_set_resources,
862 .enable_resources = pci_dev_enable_resources,
863 #if CONFIG(HAVE_ACPI_TABLES)
864 .write_acpi_tables = pci_rom_write_acpi_tables,
865 .acpi_fill_ssdt = pci_rom_ssdt,
866 #endif
867 .init = pci_dev_init,
868 .ops_pci = &pci_dev_ops_pci,
869 };
870
871 /** Default device operations for PCI bridges */
872 struct device_operations default_pci_ops_bus = {
873 .read_resources = pci_bus_read_resources,
874 .set_resources = pci_dev_set_resources,
875 .enable_resources = pci_bus_enable_resources,
876 .scan_bus = pci_scan_bridge,
877 .reset_bus = pci_bus_reset,
878 };
879
880 /** Default device operations for PCI devices marked 'hidden' */
881 static struct device_operations default_hidden_pci_ops_dev = {
882 .read_resources = noop_read_resources,
883 .set_resources = noop_set_resources,
884 .scan_bus = scan_static_bus,
885 };
886
887 /**
888 * Check for compatibility to route legacy VGA cycles through a bridge.
889 *
890 * Originally, when decoding i/o ports for legacy VGA cycles, bridges
891 * should only consider the 10 least significant bits of the port address.
892 * This means all VGA registers were aliased every 1024 ports!
893 * e.g. 0x3b0 was also decoded as 0x7b0, 0xbb0 etc.
894 *
895 * To avoid this mess, a bridge control bit (VGA16) was introduced in
896 * 2003 to enable decoding of 16-bit port addresses. As we don't want
897 * to make this any more complex for now, we use this bit if possible
898 * and only warn if it's not supported (in set_vga_bridge_bits()).
899 */
900 static void pci_bridge_vga_compat(struct bus *const bus)
901 {
902 uint16_t bridge_ctrl;
903
904 bridge_ctrl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
905
906 /* Ensure VGA decoding is disabled during probing (it should
907 be by default, but we run blobs nowadays) */
908 bridge_ctrl &= ~PCI_BRIDGE_CTL_VGA;
909 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, bridge_ctrl);
910
911 /* If the upstream bridge doesn't support VGA16, we don't have to check */
912 bus->no_vga16 |= bus->dev->upstream->no_vga16;
913 if (bus->no_vga16)
914 return;
915
916 /* Test if we can enable 16-bit decoding */
917 bridge_ctrl |= PCI_BRIDGE_CTL_VGA16;
918 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, bridge_ctrl);
919 bridge_ctrl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
920
921 bus->no_vga16 = !(bridge_ctrl & PCI_BRIDGE_CTL_VGA16);
922 }
923
924 /**
925 * Detect the type of downstream bridge.
926 *
927 * This function is a heuristic to detect which type of bus is downstream
928 * of a PCI-to-PCI bridge. This functions by looking for various capability
929 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
930 * Hypertransport all seem to have appropriate capabilities.
931 *
932 * When only a PCI-Express capability is found the type is examined to see
933 * which type of bridge we have.
934 *
935 * @param dev Pointer to the device structure of the bridge.
936 * @return Appropriate bridge operations.
937 */
938 static struct device_operations *get_pci_bridge_ops(struct device *dev)
939 {
940 #if CONFIG(PCIX_PLUGIN_SUPPORT)
941 unsigned int pcixpos;
942 pcixpos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
943 if (pcixpos) {
944 printk(BIOS_DEBUG, "%s subordinate bus PCI-X\n", dev_path(dev));
945 return &default_pcix_ops_bus;
946 }
947 #endif
948 #if CONFIG(PCIEXP_PLUGIN_SUPPORT)
949 unsigned int pciexpos;
950 pciexpos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
951 if (pciexpos) {
952 u16 flags;
953 flags = pci_read_config16(dev, pciexpos + PCI_EXP_FLAGS);
954 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
955 case PCI_EXP_TYPE_ROOT_PORT:
956 case PCI_EXP_TYPE_UPSTREAM:
957 case PCI_EXP_TYPE_DOWNSTREAM:
958 printk(BIOS_DEBUG, "%s subordinate bus PCI Express\n",
959 dev_path(dev));
960 if (CONFIG(PCIEXP_HOTPLUG)) {
961 u16 sltcap;
962 sltcap = pci_read_config16(dev, pciexpos + PCI_EXP_SLTCAP);
963 if (sltcap & PCI_EXP_SLTCAP_HPC) {
964 printk(BIOS_DEBUG, "%s hot-plug capable\n",
965 dev_path(dev));
966 return &default_pciexp_hotplug_ops_bus;
967 }
968 }
969 return &default_pciexp_ops_bus;
970 case PCI_EXP_TYPE_PCI_BRIDGE:
971 printk(BIOS_DEBUG, "%s subordinate PCI\n",
972 dev_path(dev));
973 return &default_pci_ops_bus;
974 default:
975 break;
976 }
977 }
978 #endif
979 return &default_pci_ops_bus;
980 }
981
982 /**
983 * Check if a device id matches a PCI driver entry.
984 *
985 * The driver entry can either point at a zero terminated array of acceptable
986 * device IDs, or include a single device ID.
987 *
988 * @param driver pointer to the PCI driver entry being checked
989 * @param device_id PCI device ID of the device being matched
990 */
991 static int device_id_match(struct pci_driver *driver, unsigned short device_id)
992 {
993 if (driver->devices) {
994 unsigned short check_id;
995 const unsigned short *device_list = driver->devices;
996 while ((check_id = *device_list++) != 0)
997 if (check_id == device_id)
998 return 1;
999 }
1000
1001 return (driver->device == device_id);
1002 }
1003
1004 /**
1005 * Set up PCI device operation.
1006 *
1007 * Check if it already has a driver. If not, use find_device_operations(),
1008 * or set to a default based on type.
1009 *
1010 * @param dev Pointer to the device whose pci_ops you want to set.
1011 * @see pci_drivers
1012 */
1013 static void set_pci_ops(struct device *dev)
1014 {
1015 struct pci_driver *driver;
1016
1017 if (dev->ops)
1018 return;
1019
1020 /*
1021 * Look through the list of setup drivers and find one for
1022 * this PCI device.
1023 */
1024 for (driver = &_pci_drivers[0]; driver != &_epci_drivers[0]; driver++) {
1025 if ((driver->vendor == dev->vendor) &&
1026 device_id_match(driver, dev->device)) {
1027 dev->ops = (struct device_operations *)driver->ops;
1028 break;
1029 }
1030 }
1031
1032 if (dev->ops) {
1033 printk(BIOS_SPEW, "%s [%04x/%04x] %sops\n", dev_path(dev),
1034 driver->vendor, driver->device, (driver->ops->scan_bus ? "bus " : ""));
1035 return;
1036 }
1037
1038 /* If I don't have a specific driver use the default operations. */
1039 switch (dev->hdr_type & 0x7f) { /* Header type */
1040 case PCI_HEADER_TYPE_NORMAL:
1041 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
1042 goto bad;
1043 dev->ops = &default_pci_ops_dev;
1044 break;
1045 case PCI_HEADER_TYPE_BRIDGE:
1046 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1047 goto bad;
1048 dev->ops = get_pci_bridge_ops(dev);
1049 break;
1050 #if CONFIG(CARDBUS_PLUGIN_SUPPORT)
1051 case PCI_HEADER_TYPE_CARDBUS:
1052 dev->ops = &default_cardbus_ops_bus;
1053 break;
1054 #endif
1055 default:
1056 bad:
1057 if (dev->enabled) {
1058 printk(BIOS_ERR,
1059 "%s [%04x/%04x/%06x] has unknown header type %02x, ignoring.\n",
1060 dev_path(dev), dev->vendor, dev->device,
1061 dev->class >> 8, dev->hdr_type);
1062 }
1063 }
1064 }
1065
1066 /**
1067 * See if we have already allocated a device structure for a given devfn.
1068 *
1069 * Given a PCI bus structure and a devfn number, find the device structure
1070 * corresponding to the devfn, if present. Then move the device structure
1071 * as the last child on the bus.
1072 *
1073 * @param bus Pointer to the bus structure.
1074 * @param devfn A device/function number.
1075 * @return Pointer to the device structure found or NULL if we have not
1076 * allocated a device for this devfn yet.
1077 */
1078 static struct device *pci_scan_get_dev(struct bus *bus, unsigned int devfn)
1079 {
1080 struct device *dev, **prev;
1081
1082 prev = &bus->children;
1083 for (dev = bus->children; dev; dev = dev->sibling) {
1084 if (dev->path.type == DEVICE_PATH_PCI && dev->path.pci.devfn == devfn) {
1085 /* Unlink from the list. */
1086 *prev = dev->sibling;
1087 dev->sibling = NULL;
1088 break;
1089 }
1090 prev = &dev->sibling;
1091 }
1092
1093 /*
1094 * Just like alloc_dev() add the device to the list of devices on the
1095 * bus. When the list of devices was formed we removed all of the
1096 * parents children, and now we are interleaving static and dynamic
1097 * devices in order on the bus.
1098 */
1099 if (dev) {
1100 struct device *child;
1101
1102 /* Find the last child on the bus. */
1103 for (child = bus->children; child && child->sibling;)
1104 child = child->sibling;
1105
1106 /* Place the device as last on the bus. */
1107 if (child)
1108 child->sibling = dev;
1109 else
1110 bus->children = dev;
1111 }
1112
1113 return dev;
1114 }
1115
1116 /**
1117 * Scan a PCI bus.
1118 *
1119 * Determine the existence of a given PCI device. Allocate a new struct device
1120 * if dev==NULL was passed in and the device exists in hardware.
1121 *
1122 * @param dev Pointer to the dev structure.
1123 * @param bus Pointer to the bus structure.
1124 * @param devfn A device/function number to look at.
1125 * @return The device structure for the device (if found), NULL otherwise.
1126 */
1127 struct device *pci_probe_dev(struct device *dev, struct bus *bus,
1128 unsigned int devfn)
1129 {
1130 u32 id, class;
1131 u8 hdr_type;
1132
1133 /* Detect if a device is present. */
1134 if (!dev) {
1135 struct device dummy;
1136
1137 dummy.upstream = bus;
1138 dummy.path.type = DEVICE_PATH_PCI;
1139 dummy.path.pci.devfn = devfn;
1140
1141 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
1142 /*
1143 * Have we found something? Some broken boards return 0 if a
1144 * slot is empty, but the expected answer is 0xffffffff.
1145 */
1146 if (id == 0xffffffff)
1147 return NULL;
1148
1149 if ((id == 0x00000000) || (id == 0x0000ffff) ||
1150 (id == 0xffff0000)) {
1151 printk(BIOS_SPEW, "%s, bad id 0x%x\n",
1152 dev_path(&dummy), id);
1153 return NULL;
1154 }
1155 dev = alloc_dev(bus, &dummy.path);
1156 } else {
1157 /*
1158 * Enable/disable the device. Once we have found the device-
1159 * specific operations this operations we will disable the
1160 * device with those as well.
1161 *
1162 * This is geared toward devices that have subfunctions
1163 * that do not show up by default.
1164 *
1165 * If a device is a stuff option on the motherboard
1166 * it may be absent and enable_dev() must cope.
1167 */
1168 /* Run the magic enable sequence for the device. */
1169 if (dev->chip_ops && dev->chip_ops->enable_dev)
1170 dev->chip_ops->enable_dev(dev);
1171
1172 /* Now read the vendor and device ID. */
1173 id = pci_read_config32(dev, PCI_VENDOR_ID);
1174
1175 /*
1176 * If the device does not have a PCI ID disable it. Possibly
1177 * this is because we have already disabled the device. But
1178 * this also handles optional devices that may not always
1179 * show up.
1180 */
1181 /* If the chain is fully enumerated quit */
1182 if ((id == 0xffffffff) || (id == 0x00000000) ||
1183 (id == 0x0000ffff) || (id == 0xffff0000)) {
1184 if (dev->enabled) {
1185 printk(BIOS_INFO,
1186 "PCI: Static device %s not found, disabling it.\n",
1187 dev_path(dev));
1188 dev->enabled = 0;
1189 }
1190 return dev;
1191 }
1192 }
1193
1194 /* Read the rest of the PCI configuration information. */
1195 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
1196 class = pci_read_config32(dev, PCI_CLASS_REVISION);
1197
1198 /* Store the interesting information in the device structure. */
1199 dev->vendor = id & 0xffff;
1200 dev->device = (id >> 16) & 0xffff;
1201 dev->hdr_type = hdr_type;
1202
1203 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
1204 dev->class = class >> 8;
1205
1206 /* Architectural/System devices always need to be bus masters. */
1207 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM &&
1208 CONFIG(PCI_ALLOW_BUS_MASTER_ANY_DEVICE))
1209 dev->command |= PCI_COMMAND_MASTER;
1210
1211 /*
1212 * Look at the vendor and device ID, or at least the header type and
1213 * class and figure out which set of configuration methods to use.
1214 * Unless we already have some PCI ops.
1215 */
1216 set_pci_ops(dev);
1217
1218 /* Now run the magic enable/disable sequence for the device. */
1219 if (dev->ops && dev->ops->enable)
1220 dev->ops->enable(dev);
1221
1222 /* Display the device. */
1223 printk(BIOS_DEBUG, "%s [%04x/%04x] %s%s\n", dev_path(dev),
1224 dev->vendor, dev->device, dev->enabled ? "enabled" : "disabled",
1225 dev->ops ? "" : " No operations");
1226
1227 return dev;
1228 }
1229
1230 /**
1231 * Test for match between romstage and ramstage device instance.
1232 *
1233 * @param dev Pointer to the device structure.
1234 * @param sdev Simple device model identifier, created with PCI_DEV().
1235 * @return Non-zero if bus:dev.fn of device matches.
1236 */
1237 unsigned int pci_match_simple_dev(struct device *dev, pci_devfn_t sdev)
1238 {
1239 return dev->upstream->secondary == PCI_DEV2BUS(sdev) &&
1240 dev->upstream->segment_group == PCI_DEV2SEG(sdev) &&
1241 dev->path.pci.devfn == PCI_DEV2DEVFN(sdev);
1242 }
1243
1244 /**
1245 * Test whether a capability is available along the whole path from the given
1246 * device to the host bridge.
1247 *
1248 * @param dev Pointer to the device structure.
1249 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
1250 * @return The next matching capability of the given device, if it is available
1251 * along the whole path, or zero if not.
1252 */
1253 uint16_t pci_find_cap_recursive(const struct device *dev, uint16_t cap)
1254 {
1255 assert(dev->upstream);
1256 uint16_t pos = pci_find_capability(dev, cap);
1257 const struct device *bridge = dev->upstream->dev;
1258 while (bridge && (bridge->path.type == DEVICE_PATH_PCI)) {
1259 assert(bridge->upstream);
1260 if (!pci_find_capability(bridge, cap))
1261 return 0;
1262 bridge = bridge->upstream->dev;
1263 }
1264 return pos;
1265 }
1266
1267 /**
1268 * Returns if the device support PMEs.
1269 *
1270 * @param dev Pointer to the device structure.
1271 * @return Returns true when the device support PMEs. The PME generation can be
1272 * disabled though.
1273 */
1274 bool pci_has_pme_pin(const struct device *dev)
1275 {
1276 const uint16_t cap = pci_find_capability(dev, PCI_CAP_ID_PM);
1277 if (!cap)
1278 return false;
1279
1280 const uint16_t pmecap = pci_read_config16(dev, cap + PCI_PM_PMC);
1281
1282 return !!(pmecap & PCI_PM_CAP_PME);
1283 }
1284
1285 /**
1286 * PCI devices that are marked as "hidden" do not get probed. However, the same
1287 * initialization logic is still performed as if it were. This is useful when
1288 * devices would like to be described in the devicetree.cb file, and/or present
1289 * static PCI resources to the allocator, but the platform firmware hides the
1290 * device (makes the device invisible to PCI enumeration) before PCI enumeration
1291 * takes place.
1292 *
1293 * The expected semantics of PCI devices marked as 'hidden':
1294 * 1) The device is actually present under the specified BDF
1295 * 2) The device config space can still be accessed somehow, but the Vendor ID
1296 * indicates there is no device there (it reads as 0xffffffff).
1297 * 3) The device may still consume PCI resources. Typically, these would have
1298 * been hardcoded elsewhere.
1299 *
1300 * @param dev Pointer to the device structure.
1301 */
1302 static void pci_scan_hidden_device(struct device *dev)
1303 {
1304 if (dev->chip_ops && dev->chip_ops->enable_dev)
1305 dev->chip_ops->enable_dev(dev);
1306
1307 /*
1308 * If chip_ops->enable_dev did not set dev->ops, then set to a default
1309 * .ops, because PCI enumeration is effectively being skipped, therefore
1310 * no PCI driver will bind to this device. However, children may want to
1311 * be enumerated, so this provides scan_static_bus for the .scan_bus
1312 * callback.
1313 */
1314 if (dev->ops == NULL)
1315 dev->ops = &default_hidden_pci_ops_dev;
1316
1317 if (dev->ops->enable)
1318 dev->ops->enable(dev);
1319
1320 /* Display the device almost as if it were probed normally */
1321 printk(BIOS_DEBUG, "%s [0000/%04x] hidden%s\n", dev_path(dev),
1322 dev->device, dev->ops ? "" : " No operations");
1323 }
1324
1325 /**
1326 * A PCIe Downstream Port normally leads to a Link with only Device 0 on it
1327 * (PCIe spec r5.0, sec 7.3.1). As an optimization, scan only for Device 0 in
1328 * that situation.
1329 *
1330 * @param bus Pointer to the bus structure.
1331 */
1332 static bool pci_bus_only_one_child(struct bus *bus)
1333 {
1334 struct device *bridge = bus->dev;
1335 u16 pcie_pos, pcie_flags_reg;
1336 int pcie_type;
1337
1338 if (!bridge)
1339 return false;
1340
1341 if (bridge->path.type != DEVICE_PATH_PCI)
1342 return false;
1343
1344 pcie_pos = pci_find_capability(bridge, PCI_CAP_ID_PCIE);
1345 if (!pcie_pos)
1346 return false;
1347
1348 pcie_flags_reg = pci_read_config16(bridge, pcie_pos + PCI_EXP_FLAGS);
1349
1350 pcie_type = (pcie_flags_reg & PCI_EXP_FLAGS_TYPE) >> 4;
1351
1352 return pciexp_is_downstream_port(pcie_type);
1353 }
1354
1355 /**
1356 * Scan a PCI bus.
1357 *
1358 * Determine the existence of devices and bridges on a PCI bus. If there are
1359 * bridges on the bus, recursively scan the buses behind the bridges.
1360 *
1361 * @param bus Pointer to the bus structure.
1362 * @param min_devfn Minimum devfn to look at in the scan, usually 0x00.
1363 * @param max_devfn Maximum devfn to look at in the scan, usually 0xff.
1364 */
1365 void pci_scan_bus(struct bus *bus, unsigned int min_devfn,
1366 unsigned int max_devfn)
1367 {
1368 unsigned int devfn;
1369 struct device *dev, **prev;
1370 int once = 0;
1371
1372 printk(BIOS_DEBUG, "PCI: %s for segment group %02x bus %02x\n", __func__,
1373 bus->segment_group, bus->secondary);
1374
1375 /* Maximum sane devfn is 0xFF. */
1376 if (max_devfn > 0xff) {
1377 printk(BIOS_ERR, "PCI: %s limits devfn %x - devfn %x\n",
1378 __func__, min_devfn, max_devfn);
1379 printk(BIOS_ERR, "PCI: %s upper limit too big. Using 0xff.\n", __func__);
1380 max_devfn=0xff;
1381 }
1382
1383 post_code(POSTCODE_ENTER_PCI_SCAN_BUS);
1384
1385 if (pci_bus_only_one_child(bus))
1386 max_devfn = MIN(max_devfn, 0x07);
1387
1388 /*
1389 * Probe all devices/functions on this bus with some optimization for
1390 * non-existence and single function devices.
1391 */
1392 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
1393 if (CONFIG(MINIMAL_PCI_SCANNING)) {
1394 dev = pcidev_path_behind(bus, devfn);
1395 if (!dev || !dev->mandatory)
1396 continue;
1397 }
1398
1399 /* First thing setup the device structure. */
1400 dev = pci_scan_get_dev(bus, devfn);
1401
1402 /* Devices marked 'hidden' do not get probed */
1403 if (dev && dev->hidden) {
1404 pci_scan_hidden_device(dev);
1405
1406 /* Skip pci_probe_dev, go to next devfn */
1407 continue;
1408 }
1409
1410 /* See if a device is present and setup the device structure. */
1411 dev = pci_probe_dev(dev, bus, devfn);
1412
1413 /*
1414 * If this is not a multi function device, or the device is
1415 * not present don't waste time probing another function.
1416 * Skip to next device.
1417 */
1418 if ((PCI_FUNC(devfn) == 0x00) && (!dev
1419 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
1420 devfn += 0x07;
1421 }
1422 }
1423
1424 /*
1425 * Warn if any leftover static devices are found.
1426 * There's probably a problem in devicetree.cb.
1427 */
1428
1429 prev = &bus->children;
1430 for (dev = bus->children; dev; dev = dev->sibling) {
1431 /*
1432 * If static device is not PCI then enable it here and don't
1433 * treat it as a leftover device.
1434 */
1435 if (dev->path.type != DEVICE_PATH_PCI) {
1436 enable_static_device(dev);
1437 continue;
1438 }
1439
1440 /*
1441 * The device is only considered leftover if it is not hidden
1442 * and it has a Vendor ID of 0 (the default for a device that
1443 * could not be probed).
1444 */
1445 if (dev->vendor != 0 || dev->hidden) {
1446 prev = &dev->sibling;
1447 continue;
1448 }
1449
1450 /* Unlink it from list. */
1451 *prev = dev->sibling;
1452
1453 if (!once++)
1454 printk(BIOS_WARNING, "PCI: Leftover static devices:\n");
1455 printk(BIOS_WARNING, "%s\n", dev_path(dev));
1456 }
1457
1458 if (once)
1459 printk(BIOS_WARNING, "PCI: Check your devicetree.cb.\n");
1460
1461 /*
1462 * For all children that implement scan_bus() (i.e. bridges)
1463 * scan the bus behind that child.
1464 */
1465
1466 scan_bridges(bus);
1467
1468 /*
1469 * We've scanned the bus and so we know all about what's on the other
1470 * side of any bridges that may be on this bus plus any devices.
1471 * Return how far we've got finding sub-buses.
1472 */
1473 post_code(POSTCODE_EXIT_PCI_SCAN_BUS);
1474 }
1475
1476 typedef enum {
1477 PCI_ROUTE_CLOSE,
1478 PCI_ROUTE_SCAN,
1479 PCI_ROUTE_FINAL,
1480 } scan_state;
1481
1482 static void pci_bridge_route(struct bus *link, scan_state state)
1483 {
1484 struct device *dev = link->dev;
1485 struct bus *parent = dev->upstream;
1486 uint8_t primary, secondary, subordinate;
1487
1488 if (state == PCI_ROUTE_SCAN) {
1489 link->secondary = parent->subordinate + 1;
1490 link->subordinate = link->secondary + dev->hotplug_buses;
1491 link->max_subordinate = parent->max_subordinate
1492 ? parent->max_subordinate
1493 : (PCI_BUSES_PER_SEGMENT_GROUP - 1);
1494 link->segment_group = parent->segment_group;
1495 }
1496
1497 if (link->secondary > link->max_subordinate)
1498 die("%s: No more busses available!\n", __func__);
1499
1500 /* This ought to only happen with hotplug buses. */
1501 if (link->subordinate > link->max_subordinate) {
1502 printk(BIOS_WARNING, "%s: Limiting subordinate busses\n", __func__);
1503 link->subordinate = link->max_subordinate;
1504 }
1505
1506 if (state == PCI_ROUTE_CLOSE) {
1507 primary = 0;
1508 secondary = 0xff;
1509 subordinate = 0xfe;
1510 } else if (state == PCI_ROUTE_SCAN) {
1511 primary = parent->secondary;
1512 secondary = link->secondary;
1513 subordinate = link->max_subordinate;
1514 } else if (state == PCI_ROUTE_FINAL) {
1515 primary = parent->secondary;
1516 secondary = link->secondary;
1517 subordinate = link->subordinate;
1518 } else {
1519 return;
1520 }
1521
1522 if (state == PCI_ROUTE_SCAN) {
1523 /* Clear all status bits and turn off memory, I/O and master enables. */
1524 link->bridge_cmd = pci_read_config16(dev, PCI_COMMAND);
1525 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1526 pci_write_config16(dev, PCI_STATUS, 0xffff);
1527 }
1528
1529 /*
1530 * Configure the bus numbers for this bridge: the configuration
1531 * transactions will not be propagated by the bridge if it is not
1532 * correctly configured.
1533 */
1534 pci_write_config8(dev, PCI_PRIMARY_BUS, primary);
1535 pci_write_config8(dev, PCI_SECONDARY_BUS, secondary);
1536 pci_write_config8(dev, PCI_SUBORDINATE_BUS, subordinate);
1537
1538 if (state == PCI_ROUTE_FINAL) {
1539 pci_write_config16(dev, PCI_COMMAND, link->bridge_cmd);
1540 parent->subordinate = link->subordinate;
1541 }
1542 }
1543
1544 /**
1545 * Scan a PCI bridge and the buses behind the bridge.
1546 *
1547 * Determine the existence of buses behind the bridge. Set up the bridge
1548 * according to the result of the scan.
1549 *
1550 * This function is the default scan_bus() method for PCI bridge devices.
1551 *
1552 * @param dev Pointer to the bridge device.
1553 * @param do_scan_bus TODO
1554 */
1555 void do_pci_scan_bridge(struct device *dev,
1556 void (*do_scan_bus) (struct bus * bus,
1557 unsigned int min_devfn,
1558 unsigned int max_devfn))
1559 {
1560 struct bus *bus;
1561
1562 printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(dev));
1563
1564 if (dev->downstream == NULL) {
1565 struct bus *link;
1566 link = malloc(sizeof(*link));
1567 if (link == NULL)
1568 die("Couldn't allocate a link!\n");
1569 memset(link, 0, sizeof(*link));
1570 link->dev = dev;
1571 dev->downstream = link;
1572 }
1573
1574 bus = dev->downstream;
1575
1576 pci_bridge_vga_compat(bus);
1577
1578 pci_bridge_route(bus, PCI_ROUTE_SCAN);
1579
1580 do_scan_bus(bus, 0x00, 0xff);
1581
1582 pci_bridge_route(bus, PCI_ROUTE_FINAL);
1583 }
1584
1585 /**
1586 * Scan a PCI bridge and the buses behind the bridge.
1587 *
1588 * Determine the existence of buses behind the bridge. Set up the bridge
1589 * according to the result of the scan.
1590 *
1591 * This function is the default scan_bus() method for PCI bridge devices.
1592 *
1593 * @param dev Pointer to the bridge device.
1594 */
1595 void pci_scan_bridge(struct device *dev)
1596 {
1597 do_pci_scan_bridge(dev, pci_scan_bus);
1598 }
1599
1600 /**
1601 * Scan a PCI domain.
1602 *
1603 * This function is the default scan_bus() method for PCI domains.
1604 *
1605 * @param dev Pointer to the domain.
1606 */
1607 void pci_host_bridge_scan_bus(struct device *dev)
1608 {
1609 struct bus *link = dev->downstream;
1610 pci_scan_bus(link, PCI_DEVFN(0, 0), 0xff);
1611 }
1612
1613 void pci_dev_disable_bus_master(const struct device *dev)
1614 {
1615 pci_update_config16(dev, PCI_COMMAND, ~PCI_COMMAND_MASTER, 0x0);
1616 }
1617
1618 /**
1619 * Take an INT_PIN number (0, 1 - 4) and convert
1620 * it to a string ("NO PIN", "PIN A" - "PIN D")
1621 *
1622 * @param pin PCI Interrupt Pin number (0, 1 - 4)
1623 * @return A string corresponding to the pin number or "Invalid"
1624 */
1625 const char *pin_to_str(int pin)
1626 {
1627 const char *str[5] = {
1628 "NO PIN",
1629 "PIN A",
1630 "PIN B",
1631 "PIN C",
1632 "PIN D",
1633 };
1634
1635 if (pin >= 0 && pin <= 4)
1636 return str[pin];
1637 else
1638 return "Invalid PIN, not 0 - 4";
1639 }
1640
1641 /**
1642 * Get the PCI INT_PIN swizzle for a device defined as:
1643 * pin_parent = (pin_child + devn_child) % 4 + 1
1644 * where PIN A = 1 ... PIN_D = 4
1645 *
1646 * Given a PCI device structure 'dev', find the interrupt pin
1647 * that will be triggered on its parent bridge device when
1648 * generating an interrupt. For example: Device 1:3.2 may
1649 * use INT_PIN A but will trigger PIN D on its parent bridge
1650 * device. In this case, this function will return 4 (PIN D).
1651 *
1652 * @param dev A PCI device structure to swizzle interrupt pins for
1653 * @param *parent_bridge The PCI device structure for the bridge
1654 * device 'dev' is attached to
1655 * @return The interrupt pin number (1 - 4) that 'dev' will
1656 * trigger when generating an interrupt
1657 */
1658 static int swizzle_irq_pins(struct device *dev, struct device **parent_bridge)
1659 {
1660 struct device *parent; /* Our current device's parent device */
1661 struct device *child; /* The child device of the parent */
1662 uint8_t parent_bus = 0; /* Parent Bus number */
1663 uint16_t parent_devfn = 0; /* Parent Device and Function number */
1664 uint16_t child_devfn = 0; /* Child Device and Function number */
1665 uint8_t swizzled_pin = 0; /* Pin swizzled across a bridge */
1666
1667 /* Start with PIN A = 0 ... D = 3 */
1668 swizzled_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN) - 1;
1669
1670 /* While our current device has parent devices */
1671 child = dev;
1672 for (parent = child->upstream->dev; parent; parent = parent->upstream->dev) {
1673 parent_bus = parent->upstream->secondary;
1674 parent_devfn = parent->path.pci.devfn;
1675 child_devfn = child->path.pci.devfn;
1676
1677 /* Swizzle the INT_PIN for any bridges not on root bus */
1678 swizzled_pin = (PCI_SLOT(child_devfn) + swizzled_pin) % 4;
1679 printk(BIOS_SPEW, "\tWith INT_PIN swizzled to %s\n"
1680 "\tAttached to bridge device %01X:%02Xh.%02Xh\n",
1681 pin_to_str(swizzled_pin + 1), parent_bus,
1682 PCI_SLOT(parent_devfn), PCI_FUNC(parent_devfn));
1683
1684 /* Continue until we find the root bus */
1685 if (parent_bus > 0) {
1686 /*
1687 * We will go on to the next parent so this parent
1688 * becomes the child
1689 */
1690 child = parent;
1691 continue;
1692 } else {
1693 /*
1694 * Found the root bridge device,
1695 * fill in the structure and exit
1696 */
1697 *parent_bridge = parent;
1698 break;
1699 }
1700 }
1701
1702 /* End with PIN A = 1 ... D = 4 */
1703 return swizzled_pin + 1;
1704 }
1705
1706 /**
1707 * Given a device structure 'dev', find its interrupt pin
1708 * and its parent bridge 'parent_bdg' device structure.
1709 * If it is behind a bridge, it will return the interrupt
1710 * pin number (1 - 4) of the parent bridge that the device
1711 * interrupt pin has been swizzled to, otherwise it will
1712 * return the interrupt pin that is programmed into the
1713 * PCI config space of the target device. If 'dev' is
1714 * behind a bridge, it will fill in 'parent_bdg' with the
1715 * device structure of the bridge it is behind, otherwise
1716 * it will copy 'dev' into 'parent_bdg'.
1717 *
1718 * @param dev A PCI device structure to get interrupt pins for.
1719 * @param *parent_bdg The PCI device structure for the bridge
1720 * device 'dev' is attached to.
1721 * @return The interrupt pin number (1 - 4) that 'dev' will
1722 * trigger when generating an interrupt.
1723 * Errors: -1 is returned if the device is not enabled
1724 * -2 is returned if a parent bridge could not be found.
1725 */
1726 int get_pci_irq_pins(struct device *dev, struct device **parent_bdg)
1727 {
1728 uint8_t bus = 0; /* The bus this device is on */
1729 uint16_t devfn = 0; /* This device's device and function numbers */
1730 uint8_t int_pin = 0; /* Interrupt pin used by the device */
1731 uint8_t target_pin = 0; /* Interrupt pin we want to assign an IRQ to */
1732
1733 /* Make sure this device is enabled */
1734 if (!(dev->enabled && (dev->path.type == DEVICE_PATH_PCI)))
1735 return -1;
1736
1737 bus = dev->upstream->secondary;
1738 devfn = dev->path.pci.devfn;
1739
1740 /* Get and validate the interrupt pin used. Only 1-4 are allowed */
1741 int_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN);
1742 if (int_pin < 1 || int_pin > 4)
1743 return -1;
1744
1745 printk(BIOS_SPEW, "PCI IRQ: Found device %01X:%02X.%02X using %s\n",
1746 bus, PCI_SLOT(devfn), PCI_FUNC(devfn), pin_to_str(int_pin));
1747
1748 /* If this device is on a bridge, swizzle its INT_PIN */
1749 if (bus) {
1750 /* Swizzle its INT_PINs */
1751 target_pin = swizzle_irq_pins(dev, parent_bdg);
1752
1753 /* Make sure the swizzle returned valid structures */
1754 if (parent_bdg == NULL) {
1755 printk(BIOS_WARNING, "Could not find parent bridge for this device!\n");
1756 return -2;
1757 }
1758 } else { /* Device is not behind a bridge */
1759 target_pin = int_pin; /* Return its own interrupt pin */
1760 *parent_bdg = dev; /* Return its own structure */
1761 }
1762
1763 /* Target pin is the interrupt pin we want to assign an IRQ to */
1764 return target_pin;
1765 }
1766
1767 #if CONFIG(PC80_SYSTEM)
1768 /**
1769 * Assign IRQ numbers.
1770 *
1771 * This function assigns IRQs for all functions contained within the indicated
1772 * device address. If the device does not exist or does not require interrupts
1773 * then this function has no effect.
1774 *
1775 * This function should be called for each PCI slot in your system.
1776 *
1777 * @param dev Pointer to dev structure.
1778 * @param pIntAtoD An array of IRQ #s that are assigned to PINTA through PINTD
1779 * of this slot. The particular IRQ #s that are passed in depend on the
1780 * routing inside your southbridge and on your board.
1781 */
1782 void pci_assign_irqs(struct device *dev, const unsigned char pIntAtoD[4])
1783 {
1784 u8 slot, line, irq;
1785
1786 /* Each device may contain up to eight functions. */
1787 slot = dev->path.pci.devfn >> 3;
1788
1789 for (; dev ; dev = dev->sibling) {
1790 if (dev->path.pci.devfn >> 3 != slot)
1791 break;
1792
1793 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
1794
1795 /* PCI spec says all values except 1..4 are reserved. */
1796 if ((line < 1) || (line > 4))
1797 continue;
1798
1799 irq = pIntAtoD[line - 1];
1800
1801 printk(BIOS_DEBUG, "Assigning IRQ %d to %s\n", irq, dev_path(dev));
1802
1803 pci_write_config8(dev, PCI_INTERRUPT_LINE, irq);
1804
1805 /* Change to level triggered. */
1806 i8259_configure_irq_trigger(irq, IRQ_LEVEL_TRIGGERED);
1807 }
1808 }
1809 #endif