apb_pci.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * QEMU Ultrasparc APB PCI host
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. /* XXX This file and most of its contents are somewhat misnamed. The
  25. Ultrasparc PCI host is called the PCI Bus Module (PBM). The APB is
  26. the secondary PCI bridge. */
  27. #include "hw.h"
  28. #include "pci.h"
  29. /* debug APB */
  30. //#define DEBUG_APB
  31. #ifdef DEBUG_APB
  32. #define APB_DPRINTF(fmt, args...) \
  33. do { printf("APB: " fmt , ##args); } while (0)
  34. #else
  35. #define APB_DPRINTF(fmt, args...)
  36. #endif
  37. typedef target_phys_addr_t pci_addr_t;
  38. #include "pci_host.h"
  39. typedef PCIHostState APBState;
  40. static void pci_apb_config_writel (void *opaque, target_phys_addr_t addr,
  41. uint32_t val)
  42. {
  43. APBState *s = opaque;
  44. #ifdef TARGET_WORDS_BIGENDIAN
  45. val = bswap32(val);
  46. #endif
  47. APB_DPRINTF("config_writel addr " TARGET_FMT_plx " val %x\n", addr,
  48. val);
  49. s->config_reg = val;
  50. }
  51. static uint32_t pci_apb_config_readl (void *opaque,
  52. target_phys_addr_t addr)
  53. {
  54. APBState *s = opaque;
  55. uint32_t val;
  56. val = s->config_reg;
  57. #ifdef TARGET_WORDS_BIGENDIAN
  58. val = bswap32(val);
  59. #endif
  60. APB_DPRINTF("config_readl addr " TARGET_FMT_plx " val %x\n", addr,
  61. val);
  62. return val;
  63. }
  64. static CPUWriteMemoryFunc *pci_apb_config_write[] = {
  65. &pci_apb_config_writel,
  66. &pci_apb_config_writel,
  67. &pci_apb_config_writel,
  68. };
  69. static CPUReadMemoryFunc *pci_apb_config_read[] = {
  70. &pci_apb_config_readl,
  71. &pci_apb_config_readl,
  72. &pci_apb_config_readl,
  73. };
  74. static void apb_config_writel (void *opaque, target_phys_addr_t addr,
  75. uint32_t val)
  76. {
  77. //PCIBus *s = opaque;
  78. switch (addr & 0x3f) {
  79. case 0x00: // Control/Status
  80. case 0x10: // AFSR
  81. case 0x18: // AFAR
  82. case 0x20: // Diagnostic
  83. case 0x28: // Target address space
  84. // XXX
  85. default:
  86. break;
  87. }
  88. }
  89. static uint32_t apb_config_readl (void *opaque,
  90. target_phys_addr_t addr)
  91. {
  92. //PCIBus *s = opaque;
  93. uint32_t val;
  94. switch (addr & 0x3f) {
  95. case 0x00: // Control/Status
  96. case 0x10: // AFSR
  97. case 0x18: // AFAR
  98. case 0x20: // Diagnostic
  99. case 0x28: // Target address space
  100. // XXX
  101. default:
  102. val = 0;
  103. break;
  104. }
  105. return val;
  106. }
  107. static CPUWriteMemoryFunc *apb_config_write[] = {
  108. &apb_config_writel,
  109. &apb_config_writel,
  110. &apb_config_writel,
  111. };
  112. static CPUReadMemoryFunc *apb_config_read[] = {
  113. &apb_config_readl,
  114. &apb_config_readl,
  115. &apb_config_readl,
  116. };
  117. static CPUWriteMemoryFunc *pci_apb_write[] = {
  118. &pci_host_data_writeb,
  119. &pci_host_data_writew,
  120. &pci_host_data_writel,
  121. };
  122. static CPUReadMemoryFunc *pci_apb_read[] = {
  123. &pci_host_data_readb,
  124. &pci_host_data_readw,
  125. &pci_host_data_readl,
  126. };
  127. static void pci_apb_iowriteb (void *opaque, target_phys_addr_t addr,
  128. uint32_t val)
  129. {
  130. cpu_outb(NULL, addr & 0xffff, val);
  131. }
  132. static void pci_apb_iowritew (void *opaque, target_phys_addr_t addr,
  133. uint32_t val)
  134. {
  135. cpu_outw(NULL, addr & 0xffff, val);
  136. }
  137. static void pci_apb_iowritel (void *opaque, target_phys_addr_t addr,
  138. uint32_t val)
  139. {
  140. cpu_outl(NULL, addr & 0xffff, val);
  141. }
  142. static uint32_t pci_apb_ioreadb (void *opaque, target_phys_addr_t addr)
  143. {
  144. uint32_t val;
  145. val = cpu_inb(NULL, addr & 0xffff);
  146. return val;
  147. }
  148. static uint32_t pci_apb_ioreadw (void *opaque, target_phys_addr_t addr)
  149. {
  150. uint32_t val;
  151. val = cpu_inw(NULL, addr & 0xffff);
  152. return val;
  153. }
  154. static uint32_t pci_apb_ioreadl (void *opaque, target_phys_addr_t addr)
  155. {
  156. uint32_t val;
  157. val = cpu_inl(NULL, addr & 0xffff);
  158. return val;
  159. }
  160. static CPUWriteMemoryFunc *pci_apb_iowrite[] = {
  161. &pci_apb_iowriteb,
  162. &pci_apb_iowritew,
  163. &pci_apb_iowritel,
  164. };
  165. static CPUReadMemoryFunc *pci_apb_ioread[] = {
  166. &pci_apb_ioreadb,
  167. &pci_apb_ioreadw,
  168. &pci_apb_ioreadl,
  169. };
  170. /* The APB host has an IRQ line for each IRQ line of each slot. */
  171. static int pci_apb_map_irq(PCIDevice *pci_dev, int irq_num)
  172. {
  173. return ((pci_dev->devfn & 0x18) >> 1) + irq_num;
  174. }
  175. static int pci_pbm_map_irq(PCIDevice *pci_dev, int irq_num)
  176. {
  177. int bus_offset;
  178. if (pci_dev->devfn & 1)
  179. bus_offset = 16;
  180. else
  181. bus_offset = 0;
  182. return bus_offset + irq_num;
  183. }
  184. static void pci_apb_set_irq(qemu_irq *pic, int irq_num, int level)
  185. {
  186. /* PCI IRQ map onto the first 32 INO. */
  187. qemu_set_irq(pic[irq_num], level);
  188. }
  189. PCIBus *pci_apb_init(target_phys_addr_t special_base,
  190. target_phys_addr_t mem_base,
  191. qemu_irq *pic, PCIBus **bus2, PCIBus **bus3)
  192. {
  193. APBState *s;
  194. PCIDevice *d;
  195. int pci_mem_config, pci_mem_data, apb_config, pci_ioport;
  196. s = qemu_mallocz(sizeof(APBState));
  197. /* Ultrasparc PBM main bus */
  198. s->bus = pci_register_bus(pci_apb_set_irq, pci_pbm_map_irq, pic, 0, 32);
  199. pci_mem_config = cpu_register_io_memory(0, pci_apb_config_read,
  200. pci_apb_config_write, s);
  201. apb_config = cpu_register_io_memory(0, apb_config_read,
  202. apb_config_write, s);
  203. pci_mem_data = cpu_register_io_memory(0, pci_apb_read,
  204. pci_apb_write, s);
  205. pci_ioport = cpu_register_io_memory(0, pci_apb_ioread,
  206. pci_apb_iowrite, s);
  207. cpu_register_physical_memory(special_base + 0x2000ULL, 0x40, apb_config);
  208. cpu_register_physical_memory(special_base + 0x1000000ULL, 0x10,
  209. pci_mem_config);
  210. cpu_register_physical_memory(special_base + 0x2000000ULL, 0x10000,
  211. pci_ioport);
  212. cpu_register_physical_memory(mem_base, 0x10000000,
  213. pci_mem_data); // XXX size should be 4G-prom
  214. d = pci_register_device(s->bus, "Advanced PCI Bus", sizeof(PCIDevice),
  215. 0, NULL, NULL);
  216. pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_SUN);
  217. pci_config_set_device_id(d->config, PCI_DEVICE_ID_SUN_SABRE);
  218. d->config[0x04] = 0x06; // command = bus master, pci mem
  219. d->config[0x05] = 0x00;
  220. d->config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
  221. d->config[0x07] = 0x03; // status = medium devsel
  222. d->config[0x08] = 0x00; // revision
  223. d->config[0x09] = 0x00; // programming i/f
  224. pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST);
  225. d->config[0x0D] = 0x10; // latency_timer
  226. d->config[0x0E] = 0x00; // header_type
  227. /* APB secondary busses */
  228. *bus2 = pci_bridge_init(s->bus, 8, PCI_VENDOR_ID_SUN,
  229. PCI_DEVICE_ID_SUN_SIMBA, pci_apb_map_irq,
  230. "Advanced PCI Bus secondary bridge 1");
  231. *bus3 = pci_bridge_init(s->bus, 9, PCI_VENDOR_ID_SUN,
  232. PCI_DEVICE_ID_SUN_SIMBA, pci_apb_map_irq,
  233. "Advanced PCI Bus secondary bridge 2");
  234. return s->bus;
  235. }