pci_auto_common.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: GPL-2.0+
  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>
  12. */
  13. #include <common.h>
  14. #include <dm.h>
  15. #include <errno.h>
  16. #include <log.h>
  17. #include <pci.h>
  18. void pciauto_region_init(struct pci_region *res)
  19. {
  20. /*
  21. * Avoid allocating PCI resources from address 0 -- this is illegal
  22. * according to PCI 2.1 and moreover, this is known to cause Linux IDE
  23. * drivers to fail. Use a reasonable starting value of 0x1000 instead
  24. * if the bus start address is below 0x1000.
  25. */
  26. res->bus_lower = res->bus_start < 0x1000 ? 0x1000 : res->bus_start;
  27. }
  28. void pciauto_region_align(struct pci_region *res, pci_size_t size)
  29. {
  30. res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1;
  31. }
  32. int pciauto_region_allocate(struct pci_region *res, pci_size_t size,
  33. pci_addr_t *bar, bool supports_64bit)
  34. {
  35. pci_addr_t addr;
  36. if (!res) {
  37. debug("No resource\n");
  38. goto error;
  39. }
  40. addr = ((res->bus_lower - 1) | (size - 1)) + 1;
  41. if (addr - res->bus_start + size > res->size) {
  42. debug("No room in resource, avail start=%llx / size=%llx, "
  43. "need=%llx\n", (unsigned long long)res->bus_lower,
  44. (unsigned long long)res->size, (unsigned long long)size);
  45. goto error;
  46. }
  47. if (upper_32_bits(addr) && !supports_64bit) {
  48. debug("Cannot assign 64-bit address to 32-bit-only resource\n");
  49. goto error;
  50. }
  51. res->bus_lower = addr + size;
  52. debug("address=0x%llx bus_lower=0x%llx\n", (unsigned long long)addr,
  53. (unsigned long long)res->bus_lower);
  54. *bar = addr;
  55. return 0;
  56. error:
  57. *bar = (pci_addr_t)-1;
  58. return -1;
  59. }
  60. static void pciauto_show_region(const char *name, struct pci_region *region)
  61. {
  62. pciauto_region_init(region);
  63. debug("PCI Autoconfig: Bus %s region: [%llx-%llx],\n"
  64. "\t\tPhysical Memory [%llx-%llxx]\n", name,
  65. (unsigned long long)region->bus_start,
  66. (unsigned long long)(region->bus_start + region->size - 1),
  67. (unsigned long long)region->phys_start,
  68. (unsigned long long)(region->phys_start + region->size - 1));
  69. }
  70. void pciauto_config_init(struct pci_controller *hose)
  71. {
  72. int i;
  73. hose->pci_io = NULL;
  74. hose->pci_mem = NULL;
  75. hose->pci_prefetch = NULL;
  76. for (i = 0; i < hose->region_count; i++) {
  77. switch (hose->regions[i].flags) {
  78. case PCI_REGION_IO:
  79. if (!hose->pci_io ||
  80. hose->pci_io->size < hose->regions[i].size)
  81. hose->pci_io = hose->regions + i;
  82. break;
  83. case PCI_REGION_MEM:
  84. if (!hose->pci_mem ||
  85. hose->pci_mem->size < hose->regions[i].size)
  86. hose->pci_mem = hose->regions + i;
  87. break;
  88. case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
  89. if (!hose->pci_prefetch ||
  90. hose->pci_prefetch->size < hose->regions[i].size)
  91. hose->pci_prefetch = hose->regions + i;
  92. break;
  93. }
  94. }
  95. if (hose->pci_mem)
  96. pciauto_show_region("Memory", hose->pci_mem);
  97. if (hose->pci_prefetch)
  98. pciauto_show_region("Prefetchable Mem", hose->pci_prefetch);
  99. if (hose->pci_io)
  100. pciauto_show_region("I/O", hose->pci_io);
  101. }