pcie_host.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * pcie_host.c
  3. * utility functions for pci express host bridge.
  4. *
  5. * Copyright (c) 2009 Isaku Yamahata <yamahata at valinux co jp>
  6. * VA Linux Systems Japan K.K.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "hw.h"
  20. #include "pci.h"
  21. #include "pcie_host.h"
  22. #include "exec-memory.h"
  23. /*
  24. * PCI express mmcfig address
  25. * bit 20 - 28: bus number
  26. * bit 15 - 19: device number
  27. * bit 12 - 14: function number
  28. * bit 0 - 11: offset in configuration space of a given device
  29. */
  30. #define PCIE_MMCFG_SIZE_MAX (1ULL << 28)
  31. #define PCIE_MMCFG_SIZE_MIN (1ULL << 20)
  32. #define PCIE_MMCFG_BUS_BIT 20
  33. #define PCIE_MMCFG_BUS_MASK 0x1ff
  34. #define PCIE_MMCFG_DEVFN_BIT 12
  35. #define PCIE_MMCFG_DEVFN_MASK 0xff
  36. #define PCIE_MMCFG_CONFOFFSET_MASK 0xfff
  37. #define PCIE_MMCFG_BUS(addr) (((addr) >> PCIE_MMCFG_BUS_BIT) & \
  38. PCIE_MMCFG_BUS_MASK)
  39. #define PCIE_MMCFG_DEVFN(addr) (((addr) >> PCIE_MMCFG_DEVFN_BIT) & \
  40. PCIE_MMCFG_DEVFN_MASK)
  41. #define PCIE_MMCFG_CONFOFFSET(addr) ((addr) & PCIE_MMCFG_CONFOFFSET_MASK)
  42. /* a helper function to get a PCIDevice for a given mmconfig address */
  43. static inline PCIDevice *pcie_dev_find_by_mmcfg_addr(PCIBus *s,
  44. uint32_t mmcfg_addr)
  45. {
  46. return pci_find_device(s, PCIE_MMCFG_BUS(mmcfg_addr),
  47. PCIE_MMCFG_DEVFN(mmcfg_addr));
  48. }
  49. static void pcie_mmcfg_data_write(void *opaque, target_phys_addr_t mmcfg_addr,
  50. uint64_t val, unsigned len)
  51. {
  52. PCIExpressHost *e = opaque;
  53. PCIBus *s = e->pci.bus;
  54. PCIDevice *pci_dev = pcie_dev_find_by_mmcfg_addr(s, mmcfg_addr);
  55. uint32_t addr;
  56. uint32_t limit;
  57. if (!pci_dev) {
  58. return;
  59. }
  60. addr = PCIE_MMCFG_CONFOFFSET(mmcfg_addr);
  61. limit = pci_config_size(pci_dev);
  62. if (limit <= addr) {
  63. /* conventional pci device can be behind pcie-to-pci bridge.
  64. 256 <= addr < 4K has no effects. */
  65. return;
  66. }
  67. pci_host_config_write_common(pci_dev, addr, limit, val, len);
  68. }
  69. static uint64_t pcie_mmcfg_data_read(void *opaque,
  70. target_phys_addr_t mmcfg_addr,
  71. unsigned len)
  72. {
  73. PCIExpressHost *e = opaque;
  74. PCIBus *s = e->pci.bus;
  75. PCIDevice *pci_dev = pcie_dev_find_by_mmcfg_addr(s, mmcfg_addr);
  76. uint32_t addr;
  77. uint32_t limit;
  78. if (!pci_dev) {
  79. return ~0x0;
  80. }
  81. addr = PCIE_MMCFG_CONFOFFSET(mmcfg_addr);
  82. limit = pci_config_size(pci_dev);
  83. if (limit <= addr) {
  84. /* conventional pci device can be behind pcie-to-pci bridge.
  85. 256 <= addr < 4K has no effects. */
  86. return ~0x0;
  87. }
  88. return pci_host_config_read_common(pci_dev, addr, limit, len);
  89. }
  90. static const MemoryRegionOps pcie_mmcfg_ops = {
  91. .read = pcie_mmcfg_data_read,
  92. .write = pcie_mmcfg_data_write,
  93. .endianness = DEVICE_NATIVE_ENDIAN,
  94. };
  95. /* pcie_host::base_addr == PCIE_BASE_ADDR_UNMAPPED when it isn't mapped. */
  96. #define PCIE_BASE_ADDR_UNMAPPED ((target_phys_addr_t)-1ULL)
  97. int pcie_host_init(PCIExpressHost *e, uint32_t size)
  98. {
  99. assert(!(size & (size - 1))); /* power of 2 */
  100. assert(size >= PCIE_MMCFG_SIZE_MIN);
  101. assert(size <= PCIE_MMCFG_SIZE_MAX);
  102. e->base_addr = PCIE_BASE_ADDR_UNMAPPED;
  103. e->size = size;
  104. memory_region_init_io(&e->mmio, &pcie_mmcfg_ops, e, "pcie-mmcfg", e->size);
  105. return 0;
  106. }
  107. void pcie_host_mmcfg_unmap(PCIExpressHost *e)
  108. {
  109. if (e->base_addr != PCIE_BASE_ADDR_UNMAPPED) {
  110. memory_region_del_subregion(get_system_memory(), &e->mmio);
  111. e->base_addr = PCIE_BASE_ADDR_UNMAPPED;
  112. }
  113. }
  114. void pcie_host_mmcfg_map(PCIExpressHost *e, target_phys_addr_t addr)
  115. {
  116. e->base_addr = addr;
  117. memory_region_add_subregion(get_system_memory(), e->base_addr, &e->mmio);
  118. }
  119. void pcie_host_mmcfg_update(PCIExpressHost *e,
  120. int enable,
  121. target_phys_addr_t addr)
  122. {
  123. pcie_host_mmcfg_unmap(e);
  124. if (enable) {
  125. pcie_host_mmcfg_map(e, addr);
  126. }
  127. }