2 * PCI auto-configuration library
4 * Author: Matt Porter <mporter@mvista.com>
6 * Copyright 2000 MontaVista Software Inc.
8 * Modifications for driver model:
9 * Copyright 2015 Google, Inc
10 * Written by Simon Glass <sjg@chromium.org>
12 * SPDX-License-Identifier: GPL-2.0+
20 void pciauto_region_init(struct pci_region *res)
23 * Avoid allocating PCI resources from address 0 -- this is illegal
24 * according to PCI 2.1 and moreover, this is known to cause Linux IDE
25 * drivers to fail. Use a reasonable starting value of 0x1000 instead.
27 res->bus_lower = res->bus_start ? res->bus_start : 0x1000;
30 void pciauto_region_align(struct pci_region *res, pci_size_t size)
32 res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1;
35 int pciauto_region_allocate(struct pci_region *res, pci_size_t size,
45 addr = ((res->bus_lower - 1) | (size - 1)) + 1;
47 if (addr - res->bus_start + size > res->size) {
48 debug("No room in resource");
52 res->bus_lower = addr + size;
54 debug("address=0x%llx bus_lower=0x%llx", (unsigned long long)addr,
55 (unsigned long long)res->bus_lower);
61 *bar = (pci_addr_t)-1;
65 static void pciauto_show_region(const char *name, struct pci_region *region)
67 pciauto_region_init(region);
68 debug("PCI Autoconfig: Bus %s region: [%llx-%llx],\n"
69 "\t\tPhysical Memory [%llx-%llxx]\n", name,
70 (unsigned long long)region->bus_start,
71 (unsigned long long)(region->bus_start + region->size - 1),
72 (unsigned long long)region->phys_start,
73 (unsigned long long)(region->phys_start + region->size - 1));
76 void pciauto_config_init(struct pci_controller *hose)
82 hose->pci_prefetch = NULL;
84 for (i = 0; i < hose->region_count; i++) {
85 switch (hose->regions[i].flags) {
88 hose->pci_io->size < hose->regions[i].size)
89 hose->pci_io = hose->regions + i;
93 hose->pci_mem->size < hose->regions[i].size)
94 hose->pci_mem = hose->regions + i;
96 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
97 if (!hose->pci_prefetch ||
98 hose->pci_prefetch->size < hose->regions[i].size)
99 hose->pci_prefetch = hose->regions + i;
106 pciauto_show_region("Memory", hose->pci_mem);
107 if (hose->pci_prefetch)
108 pciauto_show_region("Prefetchable Mem", hose->pci_prefetch);
110 pciauto_show_region("I/O", hose->pci_io);