Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 93335fd | 2015-11-26 19:51:23 -0700 | [diff] [blame] | 2 | /* |
| 3 | * PCI auto-configuration library |
| 4 | * |
| 5 | * Author: Matt Porter <mporter@mvista.com> |
| 6 | * |
| 7 | * Copyright 2000 MontaVista Software Inc. |
| 8 | * |
| 9 | * Modifications for driver model: |
| 10 | * Copyright 2015 Google, Inc |
| 11 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 93335fd | 2015-11-26 19:51:23 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 14 | #include <common.h> |
Simon Glass | 93335fd | 2015-11-26 19:51:23 -0700 | [diff] [blame] | 15 | #include <dm.h> |
| 16 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 17 | #include <log.h> |
Simon Glass | 93335fd | 2015-11-26 19:51:23 -0700 | [diff] [blame] | 18 | #include <pci.h> |
| 19 | |
| 20 | void pciauto_region_init(struct pci_region *res) |
| 21 | { |
| 22 | /* |
| 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 |
Bin Meng | f95cfeb | 2019-06-05 07:26:44 -0700 | [diff] [blame] | 25 | * drivers to fail. Use a reasonable starting value of 0x1000 instead |
| 26 | * if the bus start address is below 0x1000. |
Simon Glass | 93335fd | 2015-11-26 19:51:23 -0700 | [diff] [blame] | 27 | */ |
Bin Meng | f95cfeb | 2019-06-05 07:26:44 -0700 | [diff] [blame] | 28 | res->bus_lower = res->bus_start < 0x1000 ? 0x1000 : res->bus_start; |
Simon Glass | 93335fd | 2015-11-26 19:51:23 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void pciauto_region_align(struct pci_region *res, pci_size_t size) |
| 32 | { |
| 33 | res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1; |
| 34 | } |
| 35 | |
| 36 | int pciauto_region_allocate(struct pci_region *res, pci_size_t size, |
Tuomas Tynkkynen | f20b718 | 2018-05-14 19:38:13 +0300 | [diff] [blame] | 37 | pci_addr_t *bar, bool supports_64bit) |
Simon Glass | 93335fd | 2015-11-26 19:51:23 -0700 | [diff] [blame] | 38 | { |
| 39 | pci_addr_t addr; |
| 40 | |
| 41 | if (!res) { |
Tuomas Tynkkynen | 928aec7 | 2018-05-14 19:38:12 +0300 | [diff] [blame] | 42 | debug("No resource\n"); |
Simon Glass | 93335fd | 2015-11-26 19:51:23 -0700 | [diff] [blame] | 43 | goto error; |
| 44 | } |
| 45 | |
| 46 | addr = ((res->bus_lower - 1) | (size - 1)) + 1; |
| 47 | |
| 48 | if (addr - res->bus_start + size > res->size) { |
Simon Glass | b7be808 | 2019-09-25 08:56:15 -0600 | [diff] [blame] | 49 | debug("No room in resource, avail start=%llx / size=%llx, " |
| 50 | "need=%llx\n", (unsigned long long)res->bus_lower, |
| 51 | (unsigned long long)res->size, (unsigned long long)size); |
Simon Glass | 93335fd | 2015-11-26 19:51:23 -0700 | [diff] [blame] | 52 | goto error; |
| 53 | } |
| 54 | |
Tuomas Tynkkynen | f20b718 | 2018-05-14 19:38:13 +0300 | [diff] [blame] | 55 | if (upper_32_bits(addr) && !supports_64bit) { |
| 56 | debug("Cannot assign 64-bit address to 32-bit-only resource\n"); |
| 57 | goto error; |
| 58 | } |
| 59 | |
Simon Glass | 93335fd | 2015-11-26 19:51:23 -0700 | [diff] [blame] | 60 | res->bus_lower = addr + size; |
| 61 | |
Tuomas Tynkkynen | 928aec7 | 2018-05-14 19:38:12 +0300 | [diff] [blame] | 62 | debug("address=0x%llx bus_lower=0x%llx\n", (unsigned long long)addr, |
Simon Glass | 93335fd | 2015-11-26 19:51:23 -0700 | [diff] [blame] | 63 | (unsigned long long)res->bus_lower); |
| 64 | |
| 65 | *bar = addr; |
| 66 | return 0; |
| 67 | |
| 68 | error: |
| 69 | *bar = (pci_addr_t)-1; |
| 70 | return -1; |
| 71 | } |
| 72 | |
Simon Glass | 8186aa0 | 2016-02-29 15:25:35 -0700 | [diff] [blame] | 73 | static void pciauto_show_region(const char *name, struct pci_region *region) |
| 74 | { |
| 75 | pciauto_region_init(region); |
| 76 | debug("PCI Autoconfig: Bus %s region: [%llx-%llx],\n" |
Pali Rohár | 14e5d38 | 2021-10-08 17:01:24 +0200 | [diff] [blame] | 77 | "\t\tPhysical Memory [%llx-%llx]\n", name, |
Simon Glass | 8186aa0 | 2016-02-29 15:25:35 -0700 | [diff] [blame] | 78 | (unsigned long long)region->bus_start, |
| 79 | (unsigned long long)(region->bus_start + region->size - 1), |
| 80 | (unsigned long long)region->phys_start, |
| 81 | (unsigned long long)(region->phys_start + region->size - 1)); |
| 82 | } |
| 83 | |
Simon Glass | 93335fd | 2015-11-26 19:51:23 -0700 | [diff] [blame] | 84 | void pciauto_config_init(struct pci_controller *hose) |
| 85 | { |
| 86 | int i; |
| 87 | |
| 88 | hose->pci_io = NULL; |
| 89 | hose->pci_mem = NULL; |
| 90 | hose->pci_prefetch = NULL; |
| 91 | |
| 92 | for (i = 0; i < hose->region_count; i++) { |
| 93 | switch (hose->regions[i].flags) { |
| 94 | case PCI_REGION_IO: |
| 95 | if (!hose->pci_io || |
| 96 | hose->pci_io->size < hose->regions[i].size) |
| 97 | hose->pci_io = hose->regions + i; |
| 98 | break; |
| 99 | case PCI_REGION_MEM: |
| 100 | if (!hose->pci_mem || |
| 101 | hose->pci_mem->size < hose->regions[i].size) |
| 102 | hose->pci_mem = hose->regions + i; |
| 103 | break; |
| 104 | case (PCI_REGION_MEM | PCI_REGION_PREFETCH): |
| 105 | if (!hose->pci_prefetch || |
| 106 | hose->pci_prefetch->size < hose->regions[i].size) |
| 107 | hose->pci_prefetch = hose->regions + i; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
Simon Glass | 93335fd | 2015-11-26 19:51:23 -0700 | [diff] [blame] | 112 | |
Simon Glass | 8186aa0 | 2016-02-29 15:25:35 -0700 | [diff] [blame] | 113 | if (hose->pci_mem) |
| 114 | pciauto_show_region("Memory", hose->pci_mem); |
| 115 | if (hose->pci_prefetch) |
| 116 | pciauto_show_region("Prefetchable Mem", hose->pci_prefetch); |
| 117 | if (hose->pci_io) |
| 118 | pciauto_show_region("I/O", hose->pci_io); |
Simon Glass | 93335fd | 2015-11-26 19:51:23 -0700 | [diff] [blame] | 119 | } |