apb_pci.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 "sysbus.h"
  28. #include "pci/pci.h"
  29. #include "pci/pci_host.h"
  30. #include "pci/pci_bridge.h"
  31. #include "pci/pci_bus.h"
  32. #include "apb_pci.h"
  33. #include "sysemu/sysemu.h"
  34. #include "exec/address-spaces.h"
  35. /* debug APB */
  36. //#define DEBUG_APB
  37. #ifdef DEBUG_APB
  38. #define APB_DPRINTF(fmt, ...) \
  39. do { printf("APB: " fmt , ## __VA_ARGS__); } while (0)
  40. #else
  41. #define APB_DPRINTF(fmt, ...)
  42. #endif
  43. /*
  44. * Chipset docs:
  45. * PBM: "UltraSPARC IIi User's Manual",
  46. * http://www.sun.com/processors/manuals/805-0087.pdf
  47. *
  48. * APB: "Advanced PCI Bridge (APB) User's Manual",
  49. * http://www.sun.com/processors/manuals/805-1251.pdf
  50. */
  51. #define PBM_PCI_IMR_MASK 0x7fffffff
  52. #define PBM_PCI_IMR_ENABLED 0x80000000
  53. #define POR (1 << 31)
  54. #define SOFT_POR (1 << 30)
  55. #define SOFT_XIR (1 << 29)
  56. #define BTN_POR (1 << 28)
  57. #define BTN_XIR (1 << 27)
  58. #define RESET_MASK 0xf8000000
  59. #define RESET_WCMASK 0x98000000
  60. #define RESET_WMASK 0x60000000
  61. #define MAX_IVEC 0x30
  62. typedef struct APBState {
  63. SysBusDevice busdev;
  64. PCIBus *bus;
  65. MemoryRegion apb_config;
  66. MemoryRegion pci_config;
  67. MemoryRegion pci_mmio;
  68. MemoryRegion pci_ioport;
  69. uint32_t iommu[4];
  70. uint32_t pci_control[16];
  71. uint32_t pci_irq_map[8];
  72. uint32_t obio_irq_map[32];
  73. qemu_irq *pbm_irqs;
  74. qemu_irq *ivec_irqs;
  75. uint32_t reset_control;
  76. unsigned int nr_resets;
  77. } APBState;
  78. static void pci_apb_set_irq(void *opaque, int irq_num, int level);
  79. static void apb_config_writel (void *opaque, hwaddr addr,
  80. uint64_t val, unsigned size)
  81. {
  82. APBState *s = opaque;
  83. APB_DPRINTF("%s: addr " TARGET_FMT_lx " val %" PRIx64 "\n", __func__, addr, val);
  84. switch (addr & 0xffff) {
  85. case 0x30 ... 0x4f: /* DMA error registers */
  86. /* XXX: not implemented yet */
  87. break;
  88. case 0x200 ... 0x20b: /* IOMMU */
  89. s->iommu[(addr & 0xf) >> 2] = val;
  90. break;
  91. case 0x20c ... 0x3ff: /* IOMMU flush */
  92. break;
  93. case 0xc00 ... 0xc3f: /* PCI interrupt control */
  94. if (addr & 4) {
  95. s->pci_irq_map[(addr & 0x3f) >> 3] &= PBM_PCI_IMR_MASK;
  96. s->pci_irq_map[(addr & 0x3f) >> 3] |= val & ~PBM_PCI_IMR_MASK;
  97. }
  98. break;
  99. case 0x1000 ... 0x1080: /* OBIO interrupt control */
  100. if (addr & 4) {
  101. s->obio_irq_map[(addr & 0xff) >> 3] &= PBM_PCI_IMR_MASK;
  102. s->obio_irq_map[(addr & 0xff) >> 3] |= val & ~PBM_PCI_IMR_MASK;
  103. }
  104. break;
  105. case 0x1400 ... 0x143f: /* PCI interrupt clear */
  106. if (addr & 4) {
  107. pci_apb_set_irq(s, (addr & 0x3f) >> 3, 0);
  108. }
  109. break;
  110. case 0x1800 ... 0x1860: /* OBIO interrupt clear */
  111. if (addr & 4) {
  112. pci_apb_set_irq(s, 0x20 | ((addr & 0xff) >> 3), 0);
  113. }
  114. break;
  115. case 0x2000 ... 0x202f: /* PCI control */
  116. s->pci_control[(addr & 0x3f) >> 2] = val;
  117. break;
  118. case 0xf020 ... 0xf027: /* Reset control */
  119. if (addr & 4) {
  120. val &= RESET_MASK;
  121. s->reset_control &= ~(val & RESET_WCMASK);
  122. s->reset_control |= val & RESET_WMASK;
  123. if (val & SOFT_POR) {
  124. s->nr_resets = 0;
  125. qemu_system_reset_request();
  126. } else if (val & SOFT_XIR) {
  127. qemu_system_reset_request();
  128. }
  129. }
  130. break;
  131. case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
  132. case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
  133. case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
  134. case 0xf000 ... 0xf01f: /* FFB config, memory control */
  135. /* we don't care */
  136. default:
  137. break;
  138. }
  139. }
  140. static uint64_t apb_config_readl (void *opaque,
  141. hwaddr addr, unsigned size)
  142. {
  143. APBState *s = opaque;
  144. uint32_t val;
  145. switch (addr & 0xffff) {
  146. case 0x30 ... 0x4f: /* DMA error registers */
  147. val = 0;
  148. /* XXX: not implemented yet */
  149. break;
  150. case 0x200 ... 0x20b: /* IOMMU */
  151. val = s->iommu[(addr & 0xf) >> 2];
  152. break;
  153. case 0x20c ... 0x3ff: /* IOMMU flush */
  154. val = 0;
  155. break;
  156. case 0xc00 ... 0xc3f: /* PCI interrupt control */
  157. if (addr & 4) {
  158. val = s->pci_irq_map[(addr & 0x3f) >> 3];
  159. } else {
  160. val = 0;
  161. }
  162. break;
  163. case 0x1000 ... 0x1080: /* OBIO interrupt control */
  164. if (addr & 4) {
  165. val = s->obio_irq_map[(addr & 0xff) >> 3];
  166. } else {
  167. val = 0;
  168. }
  169. break;
  170. case 0x2000 ... 0x202f: /* PCI control */
  171. val = s->pci_control[(addr & 0x3f) >> 2];
  172. break;
  173. case 0xf020 ... 0xf027: /* Reset control */
  174. if (addr & 4) {
  175. val = s->reset_control;
  176. } else {
  177. val = 0;
  178. }
  179. break;
  180. case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
  181. case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
  182. case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
  183. case 0xf000 ... 0xf01f: /* FFB config, memory control */
  184. /* we don't care */
  185. default:
  186. val = 0;
  187. break;
  188. }
  189. APB_DPRINTF("%s: addr " TARGET_FMT_lx " -> %x\n", __func__, addr, val);
  190. return val;
  191. }
  192. static const MemoryRegionOps apb_config_ops = {
  193. .read = apb_config_readl,
  194. .write = apb_config_writel,
  195. .endianness = DEVICE_NATIVE_ENDIAN,
  196. };
  197. static void apb_pci_config_write(void *opaque, hwaddr addr,
  198. uint64_t val, unsigned size)
  199. {
  200. APBState *s = opaque;
  201. val = qemu_bswap_len(val, size);
  202. APB_DPRINTF("%s: addr " TARGET_FMT_lx " val %" PRIx64 "\n", __func__, addr, val);
  203. pci_data_write(s->bus, addr, val, size);
  204. }
  205. static uint64_t apb_pci_config_read(void *opaque, hwaddr addr,
  206. unsigned size)
  207. {
  208. uint32_t ret;
  209. APBState *s = opaque;
  210. ret = pci_data_read(s->bus, addr, size);
  211. ret = qemu_bswap_len(ret, size);
  212. APB_DPRINTF("%s: addr " TARGET_FMT_lx " -> %x\n", __func__, addr, ret);
  213. return ret;
  214. }
  215. static void pci_apb_iowriteb (void *opaque, hwaddr addr,
  216. uint32_t val)
  217. {
  218. cpu_outb(addr & IOPORTS_MASK, val);
  219. }
  220. static void pci_apb_iowritew (void *opaque, hwaddr addr,
  221. uint32_t val)
  222. {
  223. cpu_outw(addr & IOPORTS_MASK, bswap16(val));
  224. }
  225. static void pci_apb_iowritel (void *opaque, hwaddr addr,
  226. uint32_t val)
  227. {
  228. cpu_outl(addr & IOPORTS_MASK, bswap32(val));
  229. }
  230. static uint32_t pci_apb_ioreadb (void *opaque, hwaddr addr)
  231. {
  232. uint32_t val;
  233. val = cpu_inb(addr & IOPORTS_MASK);
  234. return val;
  235. }
  236. static uint32_t pci_apb_ioreadw (void *opaque, hwaddr addr)
  237. {
  238. uint32_t val;
  239. val = bswap16(cpu_inw(addr & IOPORTS_MASK));
  240. return val;
  241. }
  242. static uint32_t pci_apb_ioreadl (void *opaque, hwaddr addr)
  243. {
  244. uint32_t val;
  245. val = bswap32(cpu_inl(addr & IOPORTS_MASK));
  246. return val;
  247. }
  248. static const MemoryRegionOps pci_ioport_ops = {
  249. .old_mmio = {
  250. .read = { pci_apb_ioreadb, pci_apb_ioreadw, pci_apb_ioreadl },
  251. .write = { pci_apb_iowriteb, pci_apb_iowritew, pci_apb_iowritel, },
  252. },
  253. .endianness = DEVICE_NATIVE_ENDIAN,
  254. };
  255. /* The APB host has an IRQ line for each IRQ line of each slot. */
  256. static int pci_apb_map_irq(PCIDevice *pci_dev, int irq_num)
  257. {
  258. return ((pci_dev->devfn & 0x18) >> 1) + irq_num;
  259. }
  260. static int pci_pbm_map_irq(PCIDevice *pci_dev, int irq_num)
  261. {
  262. int bus_offset;
  263. if (pci_dev->devfn & 1)
  264. bus_offset = 16;
  265. else
  266. bus_offset = 0;
  267. return bus_offset + irq_num;
  268. }
  269. static void pci_apb_set_irq(void *opaque, int irq_num, int level)
  270. {
  271. APBState *s = opaque;
  272. /* PCI IRQ map onto the first 32 INO. */
  273. if (irq_num < 32) {
  274. if (s->pci_irq_map[irq_num >> 2] & PBM_PCI_IMR_ENABLED) {
  275. APB_DPRINTF("%s: set irq %d level %d\n", __func__, irq_num, level);
  276. qemu_set_irq(s->ivec_irqs[irq_num], level);
  277. } else {
  278. APB_DPRINTF("%s: not enabled: lower irq %d\n", __func__, irq_num);
  279. qemu_irq_lower(s->ivec_irqs[irq_num]);
  280. }
  281. } else {
  282. /* OBIO IRQ map onto the next 16 INO. */
  283. if (s->obio_irq_map[irq_num - 32] & PBM_PCI_IMR_ENABLED) {
  284. APB_DPRINTF("%s: set irq %d level %d\n", __func__, irq_num, level);
  285. qemu_set_irq(s->ivec_irqs[irq_num], level);
  286. } else {
  287. APB_DPRINTF("%s: not enabled: lower irq %d\n", __func__, irq_num);
  288. qemu_irq_lower(s->ivec_irqs[irq_num]);
  289. }
  290. }
  291. }
  292. static int apb_pci_bridge_initfn(PCIDevice *dev)
  293. {
  294. int rc;
  295. rc = pci_bridge_initfn(dev);
  296. if (rc < 0) {
  297. return rc;
  298. }
  299. /*
  300. * command register:
  301. * According to PCI bridge spec, after reset
  302. * bus master bit is off
  303. * memory space enable bit is off
  304. * According to manual (805-1251.pdf).
  305. * the reset value should be zero unless the boot pin is tied high
  306. * (which is true) and thus it should be PCI_COMMAND_MEMORY.
  307. */
  308. pci_set_word(dev->config + PCI_COMMAND,
  309. PCI_COMMAND_MEMORY);
  310. pci_set_word(dev->config + PCI_STATUS,
  311. PCI_STATUS_FAST_BACK | PCI_STATUS_66MHZ |
  312. PCI_STATUS_DEVSEL_MEDIUM);
  313. return 0;
  314. }
  315. PCIBus *pci_apb_init(hwaddr special_base,
  316. hwaddr mem_base,
  317. qemu_irq *ivec_irqs, PCIBus **bus2, PCIBus **bus3,
  318. qemu_irq **pbm_irqs)
  319. {
  320. DeviceState *dev;
  321. SysBusDevice *s;
  322. APBState *d;
  323. PCIDevice *pci_dev;
  324. PCIBridge *br;
  325. /* Ultrasparc PBM main bus */
  326. dev = qdev_create(NULL, "pbm");
  327. qdev_init_nofail(dev);
  328. s = SYS_BUS_DEVICE(dev);
  329. /* apb_config */
  330. sysbus_mmio_map(s, 0, special_base);
  331. /* PCI configuration space */
  332. sysbus_mmio_map(s, 1, special_base + 0x1000000ULL);
  333. /* pci_ioport */
  334. sysbus_mmio_map(s, 2, special_base + 0x2000000ULL);
  335. d = FROM_SYSBUS(APBState, s);
  336. memory_region_init(&d->pci_mmio, "pci-mmio", 0x100000000ULL);
  337. memory_region_add_subregion(get_system_memory(), mem_base, &d->pci_mmio);
  338. d->bus = pci_register_bus(&d->busdev.qdev, "pci",
  339. pci_apb_set_irq, pci_pbm_map_irq, d,
  340. &d->pci_mmio,
  341. get_system_io(),
  342. 0, 32);
  343. *pbm_irqs = d->pbm_irqs;
  344. d->ivec_irqs = ivec_irqs;
  345. pci_create_simple(d->bus, 0, "pbm-pci");
  346. /* APB secondary busses */
  347. pci_dev = pci_create_multifunction(d->bus, PCI_DEVFN(1, 0), true,
  348. "pbm-bridge");
  349. br = DO_UPCAST(PCIBridge, dev, pci_dev);
  350. pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 1",
  351. pci_apb_map_irq);
  352. qdev_init_nofail(&pci_dev->qdev);
  353. *bus2 = pci_bridge_get_sec_bus(br);
  354. pci_dev = pci_create_multifunction(d->bus, PCI_DEVFN(1, 1), true,
  355. "pbm-bridge");
  356. br = DO_UPCAST(PCIBridge, dev, pci_dev);
  357. pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 2",
  358. pci_apb_map_irq);
  359. qdev_init_nofail(&pci_dev->qdev);
  360. *bus3 = pci_bridge_get_sec_bus(br);
  361. return d->bus;
  362. }
  363. static void pci_pbm_reset(DeviceState *d)
  364. {
  365. unsigned int i;
  366. APBState *s = container_of(d, APBState, busdev.qdev);
  367. for (i = 0; i < 8; i++) {
  368. s->pci_irq_map[i] &= PBM_PCI_IMR_MASK;
  369. }
  370. for (i = 0; i < 32; i++) {
  371. s->obio_irq_map[i] &= PBM_PCI_IMR_MASK;
  372. }
  373. if (s->nr_resets++ == 0) {
  374. /* Power on reset */
  375. s->reset_control = POR;
  376. }
  377. }
  378. static const MemoryRegionOps pci_config_ops = {
  379. .read = apb_pci_config_read,
  380. .write = apb_pci_config_write,
  381. .endianness = DEVICE_NATIVE_ENDIAN,
  382. };
  383. static int pci_pbm_init_device(SysBusDevice *dev)
  384. {
  385. APBState *s;
  386. unsigned int i;
  387. s = FROM_SYSBUS(APBState, dev);
  388. for (i = 0; i < 8; i++) {
  389. s->pci_irq_map[i] = (0x1f << 6) | (i << 2);
  390. }
  391. for (i = 0; i < 32; i++) {
  392. s->obio_irq_map[i] = ((0x1f << 6) | 0x20) + i;
  393. }
  394. s->pbm_irqs = qemu_allocate_irqs(pci_apb_set_irq, s, MAX_IVEC);
  395. /* apb_config */
  396. memory_region_init_io(&s->apb_config, &apb_config_ops, s, "apb-config",
  397. 0x10000);
  398. /* at region 0 */
  399. sysbus_init_mmio(dev, &s->apb_config);
  400. memory_region_init_io(&s->pci_config, &pci_config_ops, s, "apb-pci-config",
  401. 0x1000000);
  402. /* at region 1 */
  403. sysbus_init_mmio(dev, &s->pci_config);
  404. /* pci_ioport */
  405. memory_region_init_io(&s->pci_ioport, &pci_ioport_ops, s,
  406. "apb-pci-ioport", 0x10000);
  407. /* at region 2 */
  408. sysbus_init_mmio(dev, &s->pci_ioport);
  409. return 0;
  410. }
  411. static int pbm_pci_host_init(PCIDevice *d)
  412. {
  413. pci_set_word(d->config + PCI_COMMAND,
  414. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  415. pci_set_word(d->config + PCI_STATUS,
  416. PCI_STATUS_FAST_BACK | PCI_STATUS_66MHZ |
  417. PCI_STATUS_DEVSEL_MEDIUM);
  418. return 0;
  419. }
  420. static void pbm_pci_host_class_init(ObjectClass *klass, void *data)
  421. {
  422. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  423. k->init = pbm_pci_host_init;
  424. k->vendor_id = PCI_VENDOR_ID_SUN;
  425. k->device_id = PCI_DEVICE_ID_SUN_SABRE;
  426. k->class_id = PCI_CLASS_BRIDGE_HOST;
  427. }
  428. static const TypeInfo pbm_pci_host_info = {
  429. .name = "pbm-pci",
  430. .parent = TYPE_PCI_DEVICE,
  431. .instance_size = sizeof(PCIDevice),
  432. .class_init = pbm_pci_host_class_init,
  433. };
  434. static void pbm_host_class_init(ObjectClass *klass, void *data)
  435. {
  436. DeviceClass *dc = DEVICE_CLASS(klass);
  437. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  438. k->init = pci_pbm_init_device;
  439. dc->reset = pci_pbm_reset;
  440. }
  441. static const TypeInfo pbm_host_info = {
  442. .name = "pbm",
  443. .parent = TYPE_SYS_BUS_DEVICE,
  444. .instance_size = sizeof(APBState),
  445. .class_init = pbm_host_class_init,
  446. };
  447. static void pbm_pci_bridge_class_init(ObjectClass *klass, void *data)
  448. {
  449. DeviceClass *dc = DEVICE_CLASS(klass);
  450. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  451. k->init = apb_pci_bridge_initfn;
  452. k->exit = pci_bridge_exitfn;
  453. k->vendor_id = PCI_VENDOR_ID_SUN;
  454. k->device_id = PCI_DEVICE_ID_SUN_SIMBA;
  455. k->revision = 0x11;
  456. k->config_write = pci_bridge_write_config;
  457. k->is_bridge = 1;
  458. dc->reset = pci_bridge_reset;
  459. dc->vmsd = &vmstate_pci_device;
  460. }
  461. static const TypeInfo pbm_pci_bridge_info = {
  462. .name = "pbm-bridge",
  463. .parent = TYPE_PCI_DEVICE,
  464. .instance_size = sizeof(PCIBridge),
  465. .class_init = pbm_pci_bridge_class_init,
  466. };
  467. static void pbm_register_types(void)
  468. {
  469. type_register_static(&pbm_host_info);
  470. type_register_static(&pbm_pci_host_info);
  471. type_register_static(&pbm_pci_bridge_info);
  472. }
  473. type_init(pbm_register_types)