pci_host.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "qemu/osdep.h"
  19. #include "hw/pci/pci.h"
  20. #include "hw/pci/pci_bridge.h"
  21. #include "hw/pci/pci_host.h"
  22. #include "hw/qdev-properties.h"
  23. #include "qemu/module.h"
  24. #include "hw/pci/pci_bus.h"
  25. #include "migration/vmstate.h"
  26. #include "trace.h"
  27. /* debug PCI */
  28. //#define DEBUG_PCI
  29. #ifdef DEBUG_PCI
  30. #define PCI_DPRINTF(fmt, ...) \
  31. do { printf("pci_host_data: " fmt , ## __VA_ARGS__); } while (0)
  32. #else
  33. #define PCI_DPRINTF(fmt, ...)
  34. #endif
  35. /*
  36. * PCI address
  37. * bit 16 - 24: bus number
  38. * bit 8 - 15: devfun number
  39. * bit 0 - 7: offset in configuration space of a given pci device
  40. */
  41. /* the helper function to get a PCIDevice* for a given pci address */
  42. static inline PCIDevice *pci_dev_find_by_addr(PCIBus *bus, uint32_t addr)
  43. {
  44. uint8_t bus_num = addr >> 16;
  45. uint8_t devfn = addr >> 8;
  46. return pci_find_device(bus, bus_num, devfn);
  47. }
  48. static void pci_adjust_config_limit(PCIBus *bus, uint32_t *limit)
  49. {
  50. if ((*limit > PCI_CONFIG_SPACE_SIZE) &&
  51. !pci_bus_allows_extended_config_space(bus)) {
  52. *limit = PCI_CONFIG_SPACE_SIZE;
  53. }
  54. }
  55. void pci_host_config_write_common(PCIDevice *pci_dev, uint32_t addr,
  56. uint32_t limit, uint32_t val, uint32_t len)
  57. {
  58. pci_adjust_config_limit(pci_get_bus(pci_dev), &limit);
  59. if (limit <= addr) {
  60. return;
  61. }
  62. assert(len <= 4);
  63. /* non-zero functions are only exposed when function 0 is present,
  64. * allowing direct removal of unexposed functions.
  65. */
  66. if ((pci_dev->qdev.hotplugged && !pci_get_function_0(pci_dev)) ||
  67. !pci_dev->has_power) {
  68. return;
  69. }
  70. trace_pci_cfg_write(pci_dev->name, pci_dev_bus_num(pci_dev),
  71. PCI_SLOT(pci_dev->devfn),
  72. PCI_FUNC(pci_dev->devfn), addr, val);
  73. pci_dev->config_write(pci_dev, addr, val, MIN(len, limit - addr));
  74. }
  75. uint32_t pci_host_config_read_common(PCIDevice *pci_dev, uint32_t addr,
  76. uint32_t limit, uint32_t len)
  77. {
  78. uint32_t ret;
  79. pci_adjust_config_limit(pci_get_bus(pci_dev), &limit);
  80. if (limit <= addr) {
  81. return ~0x0;
  82. }
  83. assert(len <= 4);
  84. /* non-zero functions are only exposed when function 0 is present,
  85. * allowing direct removal of unexposed functions.
  86. */
  87. if ((pci_dev->qdev.hotplugged && !pci_get_function_0(pci_dev)) ||
  88. !pci_dev->has_power) {
  89. return ~0x0;
  90. }
  91. ret = pci_dev->config_read(pci_dev, addr, MIN(len, limit - addr));
  92. trace_pci_cfg_read(pci_dev->name, pci_dev_bus_num(pci_dev),
  93. PCI_SLOT(pci_dev->devfn),
  94. PCI_FUNC(pci_dev->devfn), addr, ret);
  95. return ret;
  96. }
  97. void pci_data_write(PCIBus *s, uint32_t addr, uint32_t val, unsigned len)
  98. {
  99. PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
  100. uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
  101. if (!pci_dev) {
  102. trace_pci_cfg_write("empty", extract32(addr, 16, 8),
  103. extract32(addr, 11, 5), extract32(addr, 8, 3),
  104. config_addr, val);
  105. return;
  106. }
  107. pci_host_config_write_common(pci_dev, config_addr, PCI_CONFIG_SPACE_SIZE,
  108. val, len);
  109. }
  110. uint32_t pci_data_read(PCIBus *s, uint32_t addr, unsigned len)
  111. {
  112. PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
  113. uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
  114. if (!pci_dev) {
  115. trace_pci_cfg_read("empty", extract32(addr, 16, 8),
  116. extract32(addr, 11, 5), extract32(addr, 8, 3),
  117. config_addr, ~0x0);
  118. return ~0x0;
  119. }
  120. return pci_host_config_read_common(pci_dev, config_addr,
  121. PCI_CONFIG_SPACE_SIZE, len);
  122. }
  123. static void pci_host_config_write(void *opaque, hwaddr addr,
  124. uint64_t val, unsigned len)
  125. {
  126. PCIHostState *s = opaque;
  127. PCI_DPRINTF("%s addr " HWADDR_FMT_plx " len %d val %"PRIx64"\n",
  128. __func__, addr, len, val);
  129. if (addr != 0 || len != 4) {
  130. return;
  131. }
  132. s->config_reg = val;
  133. }
  134. static uint64_t pci_host_config_read(void *opaque, hwaddr addr,
  135. unsigned len)
  136. {
  137. PCIHostState *s = opaque;
  138. uint32_t val = s->config_reg;
  139. PCI_DPRINTF("%s addr " HWADDR_FMT_plx " len %d val %"PRIx32"\n",
  140. __func__, addr, len, val);
  141. return val;
  142. }
  143. static void pci_host_data_write(void *opaque, hwaddr addr,
  144. uint64_t val, unsigned len)
  145. {
  146. PCIHostState *s = opaque;
  147. if (s->config_reg & (1u << 31))
  148. pci_data_write(s->bus, s->config_reg | (addr & 3), val, len);
  149. }
  150. static uint64_t pci_host_data_read(void *opaque,
  151. hwaddr addr, unsigned len)
  152. {
  153. PCIHostState *s = opaque;
  154. if (!(s->config_reg & (1U << 31))) {
  155. return 0xffffffff;
  156. }
  157. return pci_data_read(s->bus, s->config_reg | (addr & 3), len);
  158. }
  159. const MemoryRegionOps pci_host_conf_le_ops = {
  160. .read = pci_host_config_read,
  161. .write = pci_host_config_write,
  162. .endianness = DEVICE_LITTLE_ENDIAN,
  163. };
  164. const MemoryRegionOps pci_host_conf_be_ops = {
  165. .read = pci_host_config_read,
  166. .write = pci_host_config_write,
  167. .endianness = DEVICE_BIG_ENDIAN,
  168. };
  169. const MemoryRegionOps pci_host_data_le_ops = {
  170. .read = pci_host_data_read,
  171. .write = pci_host_data_write,
  172. .endianness = DEVICE_LITTLE_ENDIAN,
  173. };
  174. const MemoryRegionOps pci_host_data_be_ops = {
  175. .read = pci_host_data_read,
  176. .write = pci_host_data_write,
  177. .endianness = DEVICE_BIG_ENDIAN,
  178. };
  179. static bool pci_host_needed(void *opaque)
  180. {
  181. PCIHostState *s = opaque;
  182. return s->mig_enabled;
  183. }
  184. const VMStateDescription vmstate_pcihost = {
  185. .name = "PCIHost",
  186. .needed = pci_host_needed,
  187. .version_id = 1,
  188. .minimum_version_id = 1,
  189. .fields = (VMStateField[]) {
  190. VMSTATE_UINT32(config_reg, PCIHostState),
  191. VMSTATE_END_OF_LIST()
  192. }
  193. };
  194. static Property pci_host_properties_common[] = {
  195. DEFINE_PROP_BOOL("x-config-reg-migration-enabled", PCIHostState,
  196. mig_enabled, true),
  197. DEFINE_PROP_BOOL("bypass-iommu", PCIHostState, bypass_iommu, false),
  198. DEFINE_PROP_END_OF_LIST(),
  199. };
  200. static void pci_host_class_init(ObjectClass *klass, void *data)
  201. {
  202. DeviceClass *dc = DEVICE_CLASS(klass);
  203. device_class_set_props(dc, pci_host_properties_common);
  204. dc->vmsd = &vmstate_pcihost;
  205. }
  206. static const TypeInfo pci_host_type_info = {
  207. .name = TYPE_PCI_HOST_BRIDGE,
  208. .parent = TYPE_SYS_BUS_DEVICE,
  209. .abstract = true,
  210. .class_size = sizeof(PCIHostBridgeClass),
  211. .instance_size = sizeof(PCIHostState),
  212. .class_init = pci_host_class_init,
  213. };
  214. static void pci_host_register_types(void)
  215. {
  216. type_register_static(&pci_host_type_info);
  217. }
  218. type_init(pci_host_register_types)