pci_host.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "pci.h"
  19. #include "pci_host.h"
  20. /* debug PCI */
  21. //#define DEBUG_PCI
  22. #ifdef DEBUG_PCI
  23. #define PCI_DPRINTF(fmt, ...) \
  24. do { printf("pci_host_data: " fmt , ## __VA_ARGS__); } while (0)
  25. #else
  26. #define PCI_DPRINTF(fmt, ...)
  27. #endif
  28. /*
  29. * PCI address
  30. * bit 16 - 24: bus number
  31. * bit 8 - 15: devfun number
  32. * bit 0 - 7: offset in configuration space of a given pci device
  33. */
  34. /* the helper functio to get a PCIDeice* for a given pci address */
  35. static inline PCIDevice *pci_dev_find_by_addr(PCIBus *bus, uint32_t addr)
  36. {
  37. uint8_t bus_num = addr >> 16;
  38. uint8_t devfn = addr >> 8;
  39. return pci_find_device(bus, bus_num, devfn);
  40. }
  41. void pci_data_write(PCIBus *s, uint32_t addr, uint32_t val, int len)
  42. {
  43. PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
  44. uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
  45. if (!pci_dev)
  46. return;
  47. PCI_DPRINTF("%s: %s: addr=%02" PRIx32 " val=%08" PRIx32 " len=%d\n",
  48. __func__, pci_dev->name, config_addr, val, len);
  49. pci_dev->config_write(pci_dev, config_addr, val, len);
  50. }
  51. uint32_t pci_data_read(PCIBus *s, uint32_t addr, int len)
  52. {
  53. PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
  54. uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
  55. uint32_t val;
  56. assert(len == 1 || len == 2 || len == 4);
  57. if (!pci_dev) {
  58. return ~0x0;
  59. }
  60. val = pci_dev->config_read(pci_dev, config_addr, len);
  61. PCI_DPRINTF("%s: %s: addr=%02"PRIx32" val=%08"PRIx32" len=%d\n",
  62. __func__, pci_dev->name, config_addr, val, len);
  63. return val;
  64. }
  65. static void pci_host_config_write(ReadWriteHandler *handler,
  66. pcibus_t addr, uint32_t val, int len)
  67. {
  68. PCIHostState *s = container_of(handler, PCIHostState, conf_handler);
  69. PCI_DPRINTF("%s addr %" FMT_PCIBUS " %d val %"PRIx32"\n",
  70. __func__, addr, len, val);
  71. s->config_reg = val;
  72. }
  73. static uint32_t pci_host_config_read(ReadWriteHandler *handler,
  74. pcibus_t addr, int len)
  75. {
  76. PCIHostState *s = container_of(handler, PCIHostState, conf_handler);
  77. uint32_t val = s->config_reg;
  78. PCI_DPRINTF("%s addr %" FMT_PCIBUS " len %d val %"PRIx32"\n",
  79. __func__, addr, len, val);
  80. return val;
  81. }
  82. static void pci_host_data_write(ReadWriteHandler *handler,
  83. pcibus_t addr, uint32_t val, int len)
  84. {
  85. PCIHostState *s = container_of(handler, PCIHostState, data_handler);
  86. PCI_DPRINTF("write addr %" FMT_PCIBUS " len %d val %x\n",
  87. addr, len, val);
  88. if (s->config_reg & (1u << 31))
  89. pci_data_write(s->bus, s->config_reg | (addr & 3), val, len);
  90. }
  91. static uint32_t pci_host_data_read(ReadWriteHandler *handler,
  92. pcibus_t addr, int len)
  93. {
  94. PCIHostState *s = container_of(handler, PCIHostState, data_handler);
  95. uint32_t val;
  96. if (!(s->config_reg & (1 << 31)))
  97. return 0xffffffff;
  98. val = pci_data_read(s->bus, s->config_reg | (addr & 3), len);
  99. PCI_DPRINTF("read addr %" FMT_PCIBUS " len %d val %x\n",
  100. addr, len, val);
  101. return val;
  102. }
  103. static void pci_host_init(PCIHostState *s)
  104. {
  105. s->conf_handler.write = pci_host_config_write;
  106. s->conf_handler.read = pci_host_config_read;
  107. s->data_handler.write = pci_host_data_write;
  108. s->data_handler.read = pci_host_data_read;
  109. }
  110. int pci_host_conf_register_mmio(PCIHostState *s, int endian)
  111. {
  112. pci_host_init(s);
  113. return cpu_register_io_memory_simple(&s->conf_handler, endian);
  114. }
  115. void pci_host_conf_register_ioport(pio_addr_t ioport, PCIHostState *s)
  116. {
  117. pci_host_init(s);
  118. register_ioport_simple(&s->conf_handler, ioport, 4, 4);
  119. sysbus_init_ioports(&s->busdev, ioport, 4);
  120. }
  121. int pci_host_data_register_mmio(PCIHostState *s, int endian)
  122. {
  123. pci_host_init(s);
  124. return cpu_register_io_memory_simple(&s->data_handler, endian);
  125. }
  126. void pci_host_data_register_ioport(pio_addr_t ioport, PCIHostState *s)
  127. {
  128. pci_host_init(s);
  129. register_ioport_simple(&s->data_handler, ioport, 4, 1);
  130. register_ioport_simple(&s->data_handler, ioport, 4, 2);
  131. register_ioport_simple(&s->data_handler, ioport, 4, 4);
  132. sysbus_init_ioports(&s->busdev, ioport, 4);
  133. }