pci_host.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * pci_host.c
  3. *
  4. * Copyright (c) 2009 Isaku Yamahata <yamahata at valinux co jp>
  5. * VA Linux Systems Japan K.K.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "hw/pci/pci.h"
  19. #include "hw/pci/pci_host.h"
  20. #include "trace.h"
  21. /* debug PCI */
  22. //#define DEBUG_PCI
  23. #ifdef DEBUG_PCI
  24. #define PCI_DPRINTF(fmt, ...) \
  25. do { printf("pci_host_data: " fmt , ## __VA_ARGS__); } while (0)
  26. #else
  27. #define PCI_DPRINTF(fmt, ...)
  28. #endif
  29. /*
  30. * PCI address
  31. * bit 16 - 24: bus number
  32. * bit 8 - 15: devfun number
  33. * bit 0 - 7: offset in configuration space of a given pci device
  34. */
  35. /* the helper function to get a PCIDevice* for a given pci address */
  36. static inline PCIDevice *pci_dev_find_by_addr(PCIBus *bus, uint32_t addr)
  37. {
  38. uint8_t bus_num = addr >> 16;
  39. uint8_t devfn = addr >> 8;
  40. return pci_find_device(bus, bus_num, devfn);
  41. }
  42. void pci_host_config_write_common(PCIDevice *pci_dev, uint32_t addr,
  43. uint32_t limit, uint32_t val, uint32_t len)
  44. {
  45. assert(len <= 4);
  46. trace_pci_cfg_write(pci_dev->name, PCI_SLOT(pci_dev->devfn),
  47. PCI_FUNC(pci_dev->devfn), addr, val);
  48. pci_dev->config_write(pci_dev, addr, val, MIN(len, limit - addr));
  49. }
  50. uint32_t pci_host_config_read_common(PCIDevice *pci_dev, uint32_t addr,
  51. uint32_t limit, uint32_t len)
  52. {
  53. uint32_t ret;
  54. assert(len <= 4);
  55. ret = pci_dev->config_read(pci_dev, addr, MIN(len, limit - addr));
  56. trace_pci_cfg_read(pci_dev->name, PCI_SLOT(pci_dev->devfn),
  57. PCI_FUNC(pci_dev->devfn), addr, ret);
  58. return ret;
  59. }
  60. void pci_data_write(PCIBus *s, uint32_t addr, uint32_t val, int len)
  61. {
  62. PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
  63. uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
  64. if (!pci_dev) {
  65. return;
  66. }
  67. PCI_DPRINTF("%s: %s: addr=%02" PRIx32 " val=%08" PRIx32 " len=%d\n",
  68. __func__, pci_dev->name, config_addr, val, len);
  69. pci_host_config_write_common(pci_dev, config_addr, PCI_CONFIG_SPACE_SIZE,
  70. val, len);
  71. }
  72. uint32_t pci_data_read(PCIBus *s, uint32_t addr, int len)
  73. {
  74. PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
  75. uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
  76. uint32_t val;
  77. if (!pci_dev) {
  78. return ~0x0;
  79. }
  80. val = pci_host_config_read_common(pci_dev, config_addr,
  81. PCI_CONFIG_SPACE_SIZE, len);
  82. PCI_DPRINTF("%s: %s: addr=%02"PRIx32" val=%08"PRIx32" len=%d\n",
  83. __func__, pci_dev->name, config_addr, val, len);
  84. return val;
  85. }
  86. static void pci_host_config_write(void *opaque, hwaddr addr,
  87. uint64_t val, unsigned len)
  88. {
  89. PCIHostState *s = opaque;
  90. PCI_DPRINTF("%s addr " TARGET_FMT_plx " len %d val %"PRIx64"\n",
  91. __func__, addr, len, val);
  92. if (addr != 0 || len != 4) {
  93. return;
  94. }
  95. s->config_reg = val;
  96. }
  97. static uint64_t pci_host_config_read(void *opaque, hwaddr addr,
  98. unsigned len)
  99. {
  100. PCIHostState *s = opaque;
  101. uint32_t val = s->config_reg;
  102. PCI_DPRINTF("%s addr " TARGET_FMT_plx " len %d val %"PRIx32"\n",
  103. __func__, addr, len, val);
  104. return val;
  105. }
  106. static void pci_host_data_write(void *opaque, hwaddr addr,
  107. uint64_t val, unsigned len)
  108. {
  109. PCIHostState *s = opaque;
  110. PCI_DPRINTF("write addr " TARGET_FMT_plx " len %d val %x\n",
  111. addr, len, (unsigned)val);
  112. if (s->config_reg & (1u << 31))
  113. pci_data_write(s->bus, s->config_reg | (addr & 3), val, len);
  114. }
  115. static uint64_t pci_host_data_read(void *opaque,
  116. hwaddr addr, unsigned len)
  117. {
  118. PCIHostState *s = opaque;
  119. uint32_t val;
  120. if (!(s->config_reg & (1U << 31))) {
  121. return 0xffffffff;
  122. }
  123. val = pci_data_read(s->bus, s->config_reg | (addr & 3), len);
  124. PCI_DPRINTF("read addr " TARGET_FMT_plx " len %d val %x\n",
  125. addr, len, val);
  126. return val;
  127. }
  128. const MemoryRegionOps pci_host_conf_le_ops = {
  129. .read = pci_host_config_read,
  130. .write = pci_host_config_write,
  131. .endianness = DEVICE_LITTLE_ENDIAN,
  132. };
  133. const MemoryRegionOps pci_host_conf_be_ops = {
  134. .read = pci_host_config_read,
  135. .write = pci_host_config_write,
  136. .endianness = DEVICE_BIG_ENDIAN,
  137. };
  138. const MemoryRegionOps pci_host_data_le_ops = {
  139. .read = pci_host_data_read,
  140. .write = pci_host_data_write,
  141. .endianness = DEVICE_LITTLE_ENDIAN,
  142. };
  143. const MemoryRegionOps pci_host_data_be_ops = {
  144. .read = pci_host_data_read,
  145. .write = pci_host_data_write,
  146. .endianness = DEVICE_BIG_ENDIAN,
  147. };
  148. static const TypeInfo pci_host_type_info = {
  149. .name = TYPE_PCI_HOST_BRIDGE,
  150. .parent = TYPE_SYS_BUS_DEVICE,
  151. .abstract = true,
  152. .class_size = sizeof(PCIHostBridgeClass),
  153. .instance_size = sizeof(PCIHostState),
  154. };
  155. static void pci_host_register_types(void)
  156. {
  157. type_register_static(&pci_host_type_info);
  158. }
  159. type_init(pci_host_register_types)