sabre.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * QEMU Ultrasparc Sabre PCI host (PBM)
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. * Copyright (c) 2012,2013 Artyom Tarasenko
  6. * Copyright (c) 2018 Mark Cave-Ayland
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu/osdep.h"
  27. #include "hw/sysbus.h"
  28. #include "hw/pci/pci.h"
  29. #include "hw/pci/pci_host.h"
  30. #include "hw/qdev-properties.h"
  31. #include "hw/pci/pci_bridge.h"
  32. #include "hw/pci/pci_bus.h"
  33. #include "hw/irq.h"
  34. #include "hw/pci-bridge/simba.h"
  35. #include "hw/pci-host/sabre.h"
  36. #include "exec/address-spaces.h"
  37. #include "qapi/error.h"
  38. #include "qemu/log.h"
  39. #include "qemu/module.h"
  40. #include "sysemu/runstate.h"
  41. #include "trace.h"
  42. /*
  43. * Chipset docs:
  44. * PBM: "UltraSPARC IIi User's Manual",
  45. * http://www.sun.com/processors/manuals/805-0087.pdf
  46. */
  47. #define PBM_PCI_IMR_MASK 0x7fffffff
  48. #define PBM_PCI_IMR_ENABLED 0x80000000
  49. #define POR (1U << 31)
  50. #define SOFT_POR (1U << 30)
  51. #define SOFT_XIR (1U << 29)
  52. #define BTN_POR (1U << 28)
  53. #define BTN_XIR (1U << 27)
  54. #define RESET_MASK 0xf8000000
  55. #define RESET_WCMASK 0x98000000
  56. #define RESET_WMASK 0x60000000
  57. #define NO_IRQ_REQUEST (MAX_IVEC + 1)
  58. static inline void sabre_set_request(SabreState *s, unsigned int irq_num)
  59. {
  60. trace_sabre_set_request(irq_num);
  61. s->irq_request = irq_num;
  62. qemu_set_irq(s->ivec_irqs[irq_num], 1);
  63. }
  64. static inline void sabre_check_irqs(SabreState *s)
  65. {
  66. unsigned int i;
  67. /* Previous request is not acknowledged, resubmit */
  68. if (s->irq_request != NO_IRQ_REQUEST) {
  69. sabre_set_request(s, s->irq_request);
  70. return;
  71. }
  72. /* no request pending */
  73. if (s->pci_irq_in == 0ULL) {
  74. return;
  75. }
  76. for (i = 0; i < 32; i++) {
  77. if (s->pci_irq_in & (1ULL << i)) {
  78. if (s->pci_irq_map[i >> 2] & PBM_PCI_IMR_ENABLED) {
  79. sabre_set_request(s, i);
  80. return;
  81. }
  82. }
  83. }
  84. for (i = 32; i < 64; i++) {
  85. if (s->pci_irq_in & (1ULL << i)) {
  86. if (s->obio_irq_map[i - 32] & PBM_PCI_IMR_ENABLED) {
  87. sabre_set_request(s, i);
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. static inline void sabre_clear_request(SabreState *s, unsigned int irq_num)
  94. {
  95. trace_sabre_clear_request(irq_num);
  96. qemu_set_irq(s->ivec_irqs[irq_num], 0);
  97. s->irq_request = NO_IRQ_REQUEST;
  98. }
  99. static AddressSpace *sabre_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
  100. {
  101. IOMMUState *is = opaque;
  102. return &is->iommu_as;
  103. }
  104. static void sabre_config_write(void *opaque, hwaddr addr,
  105. uint64_t val, unsigned size)
  106. {
  107. SabreState *s = opaque;
  108. trace_sabre_config_write(addr, val);
  109. switch (addr & 0xffff) {
  110. case 0x30 ... 0x4f: /* DMA error registers */
  111. /* XXX: not implemented yet */
  112. break;
  113. case 0xc00 ... 0xc3f: /* PCI interrupt control */
  114. if (addr & 4) {
  115. unsigned int ino = (addr & 0x3f) >> 3;
  116. s->pci_irq_map[ino] &= PBM_PCI_IMR_MASK;
  117. s->pci_irq_map[ino] |= val & ~PBM_PCI_IMR_MASK;
  118. if ((s->irq_request == ino) && !(val & ~PBM_PCI_IMR_MASK)) {
  119. sabre_clear_request(s, ino);
  120. }
  121. sabre_check_irqs(s);
  122. }
  123. break;
  124. case 0x1000 ... 0x107f: /* OBIO interrupt control */
  125. if (addr & 4) {
  126. unsigned int ino = ((addr & 0xff) >> 3);
  127. s->obio_irq_map[ino] &= PBM_PCI_IMR_MASK;
  128. s->obio_irq_map[ino] |= val & ~PBM_PCI_IMR_MASK;
  129. if ((s->irq_request == (ino | 0x20))
  130. && !(val & ~PBM_PCI_IMR_MASK)) {
  131. sabre_clear_request(s, ino | 0x20);
  132. }
  133. sabre_check_irqs(s);
  134. }
  135. break;
  136. case 0x1400 ... 0x14ff: /* PCI interrupt clear */
  137. if (addr & 4) {
  138. unsigned int ino = (addr & 0xff) >> 5;
  139. if ((s->irq_request / 4) == ino) {
  140. sabre_clear_request(s, s->irq_request);
  141. sabre_check_irqs(s);
  142. }
  143. }
  144. break;
  145. case 0x1800 ... 0x1860: /* OBIO interrupt clear */
  146. if (addr & 4) {
  147. unsigned int ino = ((addr & 0xff) >> 3) | 0x20;
  148. if (s->irq_request == ino) {
  149. sabre_clear_request(s, ino);
  150. sabre_check_irqs(s);
  151. }
  152. }
  153. break;
  154. case 0x2000 ... 0x202f: /* PCI control */
  155. s->pci_control[(addr & 0x3f) >> 2] = val;
  156. break;
  157. case 0xf020 ... 0xf027: /* Reset control */
  158. if (addr & 4) {
  159. val &= RESET_MASK;
  160. s->reset_control &= ~(val & RESET_WCMASK);
  161. s->reset_control |= val & RESET_WMASK;
  162. if (val & SOFT_POR) {
  163. s->nr_resets = 0;
  164. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  165. } else if (val & SOFT_XIR) {
  166. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  167. }
  168. }
  169. break;
  170. case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
  171. case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
  172. case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
  173. case 0xf000 ... 0xf01f: /* FFB config, memory control */
  174. /* we don't care */
  175. default:
  176. break;
  177. }
  178. }
  179. static uint64_t sabre_config_read(void *opaque,
  180. hwaddr addr, unsigned size)
  181. {
  182. SabreState *s = opaque;
  183. uint32_t val;
  184. switch (addr & 0xffff) {
  185. case 0x30 ... 0x4f: /* DMA error registers */
  186. val = 0;
  187. /* XXX: not implemented yet */
  188. break;
  189. case 0xc00 ... 0xc3f: /* PCI interrupt control */
  190. if (addr & 4) {
  191. val = s->pci_irq_map[(addr & 0x3f) >> 3];
  192. } else {
  193. val = 0;
  194. }
  195. break;
  196. case 0x1000 ... 0x107f: /* OBIO interrupt control */
  197. if (addr & 4) {
  198. val = s->obio_irq_map[(addr & 0xff) >> 3];
  199. } else {
  200. val = 0;
  201. }
  202. break;
  203. case 0x1080 ... 0x108f: /* PCI bus error */
  204. if (addr & 4) {
  205. val = s->pci_err_irq_map[(addr & 0xf) >> 3];
  206. } else {
  207. val = 0;
  208. }
  209. break;
  210. case 0x2000 ... 0x202f: /* PCI control */
  211. val = s->pci_control[(addr & 0x3f) >> 2];
  212. break;
  213. case 0xf020 ... 0xf027: /* Reset control */
  214. if (addr & 4) {
  215. val = s->reset_control;
  216. } else {
  217. val = 0;
  218. }
  219. break;
  220. case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
  221. case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
  222. case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
  223. case 0xf000 ... 0xf01f: /* FFB config, memory control */
  224. /* we don't care */
  225. default:
  226. val = 0;
  227. break;
  228. }
  229. trace_sabre_config_read(addr, val);
  230. return val;
  231. }
  232. static const MemoryRegionOps sabre_config_ops = {
  233. .read = sabre_config_read,
  234. .write = sabre_config_write,
  235. .endianness = DEVICE_BIG_ENDIAN,
  236. };
  237. static void sabre_pci_config_write(void *opaque, hwaddr addr,
  238. uint64_t val, unsigned size)
  239. {
  240. SabreState *s = opaque;
  241. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  242. trace_sabre_pci_config_write(addr, val);
  243. pci_data_write(phb->bus, addr, val, size);
  244. }
  245. static uint64_t sabre_pci_config_read(void *opaque, hwaddr addr,
  246. unsigned size)
  247. {
  248. uint32_t ret;
  249. SabreState *s = opaque;
  250. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  251. ret = pci_data_read(phb->bus, addr, size);
  252. trace_sabre_pci_config_read(addr, ret);
  253. return ret;
  254. }
  255. /* The sabre host has an IRQ line for each IRQ line of each slot. */
  256. static int pci_sabre_map_irq(PCIDevice *pci_dev, int irq_num)
  257. {
  258. /* Return the irq as swizzled by the PBM */
  259. return irq_num;
  260. }
  261. static int pci_simbaA_map_irq(PCIDevice *pci_dev, int irq_num)
  262. {
  263. /* The on-board devices have fixed (legacy) OBIO intnos */
  264. switch (PCI_SLOT(pci_dev->devfn)) {
  265. case 1:
  266. /* Onboard NIC */
  267. return OBIO_NIC_IRQ;
  268. case 3:
  269. /* Onboard IDE */
  270. return OBIO_HDD_IRQ;
  271. default:
  272. /* Normal intno, fall through */
  273. break;
  274. }
  275. return ((PCI_SLOT(pci_dev->devfn) << 2) + irq_num) & 0x1f;
  276. }
  277. static int pci_simbaB_map_irq(PCIDevice *pci_dev, int irq_num)
  278. {
  279. return (0x10 + (PCI_SLOT(pci_dev->devfn) << 2) + irq_num) & 0x1f;
  280. }
  281. static void pci_sabre_set_irq(void *opaque, int irq_num, int level)
  282. {
  283. SabreState *s = opaque;
  284. trace_sabre_pci_set_irq(irq_num, level);
  285. /* PCI IRQ map onto the first 32 INO. */
  286. if (irq_num < 32) {
  287. if (level) {
  288. s->pci_irq_in |= 1ULL << irq_num;
  289. if (s->pci_irq_map[irq_num >> 2] & PBM_PCI_IMR_ENABLED) {
  290. sabre_set_request(s, irq_num);
  291. }
  292. } else {
  293. s->pci_irq_in &= ~(1ULL << irq_num);
  294. }
  295. } else {
  296. /* OBIO IRQ map onto the next 32 INO. */
  297. if (level) {
  298. trace_sabre_pci_set_obio_irq(irq_num, level);
  299. s->pci_irq_in |= 1ULL << irq_num;
  300. if ((s->irq_request == NO_IRQ_REQUEST)
  301. && (s->obio_irq_map[irq_num - 32] & PBM_PCI_IMR_ENABLED)) {
  302. sabre_set_request(s, irq_num);
  303. }
  304. } else {
  305. s->pci_irq_in &= ~(1ULL << irq_num);
  306. }
  307. }
  308. }
  309. static void sabre_reset(DeviceState *d)
  310. {
  311. SabreState *s = SABRE_DEVICE(d);
  312. PCIDevice *pci_dev;
  313. unsigned int i;
  314. uint16_t cmd;
  315. for (i = 0; i < 8; i++) {
  316. s->pci_irq_map[i] &= PBM_PCI_IMR_MASK;
  317. }
  318. for (i = 0; i < 32; i++) {
  319. s->obio_irq_map[i] &= PBM_PCI_IMR_MASK;
  320. }
  321. s->irq_request = NO_IRQ_REQUEST;
  322. s->pci_irq_in = 0ULL;
  323. if (s->nr_resets++ == 0) {
  324. /* Power on reset */
  325. s->reset_control = POR;
  326. }
  327. /* As this is the busA PCI bridge which contains the on-board devices
  328. * attached to the ebus, ensure that we initially allow IO transactions
  329. * so that we get the early serial console until OpenBIOS can properly
  330. * configure the PCI bridge itself */
  331. pci_dev = PCI_DEVICE(s->bridgeA);
  332. cmd = pci_get_word(pci_dev->config + PCI_COMMAND);
  333. pci_set_word(pci_dev->config + PCI_COMMAND, cmd | PCI_COMMAND_IO);
  334. pci_bridge_update_mappings(PCI_BRIDGE(pci_dev));
  335. }
  336. static const MemoryRegionOps pci_config_ops = {
  337. .read = sabre_pci_config_read,
  338. .write = sabre_pci_config_write,
  339. .endianness = DEVICE_LITTLE_ENDIAN,
  340. };
  341. static void sabre_realize(DeviceState *dev, Error **errp)
  342. {
  343. SabreState *s = SABRE_DEVICE(dev);
  344. PCIHostState *phb = PCI_HOST_BRIDGE(dev);
  345. SysBusDevice *sbd = SYS_BUS_DEVICE(s);
  346. PCIDevice *pci_dev;
  347. /* sabre_config */
  348. sysbus_mmio_map(sbd, 0, s->special_base);
  349. /* PCI configuration space */
  350. sysbus_mmio_map(sbd, 1, s->special_base + 0x1000000ULL);
  351. /* pci_ioport */
  352. sysbus_mmio_map(sbd, 2, s->special_base + 0x2000000ULL);
  353. memory_region_init(&s->pci_mmio, OBJECT(s), "pci-mmio", 0x100000000ULL);
  354. memory_region_add_subregion(get_system_memory(), s->mem_base,
  355. &s->pci_mmio);
  356. phb->bus = pci_register_root_bus(dev, "pci",
  357. pci_sabre_set_irq, pci_sabre_map_irq, s,
  358. &s->pci_mmio,
  359. &s->pci_ioport,
  360. 0, 32, TYPE_PCI_BUS);
  361. pci_create_simple(phb->bus, 0, TYPE_SABRE_PCI_DEVICE);
  362. /* IOMMU */
  363. memory_region_add_subregion_overlap(&s->sabre_config, 0x200,
  364. sysbus_mmio_get_region(SYS_BUS_DEVICE(s->iommu), 0), 1);
  365. pci_setup_iommu(phb->bus, sabre_pci_dma_iommu, s->iommu);
  366. /* APB secondary busses */
  367. pci_dev = pci_new_multifunction(PCI_DEVFN(1, 0), true,
  368. TYPE_SIMBA_PCI_BRIDGE);
  369. s->bridgeB = PCI_BRIDGE(pci_dev);
  370. pci_bridge_map_irq(s->bridgeB, "pciB", pci_simbaB_map_irq);
  371. pci_realize_and_unref(pci_dev, phb->bus, &error_fatal);
  372. pci_dev = pci_new_multifunction(PCI_DEVFN(1, 1), true,
  373. TYPE_SIMBA_PCI_BRIDGE);
  374. s->bridgeA = PCI_BRIDGE(pci_dev);
  375. pci_bridge_map_irq(s->bridgeA, "pciA", pci_simbaA_map_irq);
  376. pci_realize_and_unref(pci_dev, phb->bus, &error_fatal);
  377. }
  378. static void sabre_init(Object *obj)
  379. {
  380. SabreState *s = SABRE_DEVICE(obj);
  381. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  382. unsigned int i;
  383. for (i = 0; i < 8; i++) {
  384. s->pci_irq_map[i] = (0x1f << 6) | (i << 2);
  385. }
  386. for (i = 0; i < 2; i++) {
  387. s->pci_err_irq_map[i] = (0x1f << 6) | 0x30;
  388. }
  389. for (i = 0; i < 32; i++) {
  390. s->obio_irq_map[i] = ((0x1f << 6) | 0x20) + i;
  391. }
  392. qdev_init_gpio_in_named(DEVICE(s), pci_sabre_set_irq, "pbm-irq", MAX_IVEC);
  393. qdev_init_gpio_out_named(DEVICE(s), s->ivec_irqs, "ivec-irq", MAX_IVEC);
  394. s->irq_request = NO_IRQ_REQUEST;
  395. s->pci_irq_in = 0ULL;
  396. /* IOMMU */
  397. object_property_add_link(obj, "iommu", TYPE_SUN4U_IOMMU,
  398. (Object **) &s->iommu,
  399. qdev_prop_allow_set_link_before_realize,
  400. 0);
  401. /* sabre_config */
  402. memory_region_init_io(&s->sabre_config, OBJECT(s), &sabre_config_ops, s,
  403. "sabre-config", 0x10000);
  404. /* at region 0 */
  405. sysbus_init_mmio(sbd, &s->sabre_config);
  406. memory_region_init_io(&s->pci_config, OBJECT(s), &pci_config_ops, s,
  407. "sabre-pci-config", 0x1000000);
  408. /* at region 1 */
  409. sysbus_init_mmio(sbd, &s->pci_config);
  410. /* pci_ioport */
  411. memory_region_init(&s->pci_ioport, OBJECT(s), "sabre-pci-ioport",
  412. 0x1000000);
  413. /* at region 2 */
  414. sysbus_init_mmio(sbd, &s->pci_ioport);
  415. }
  416. static void sabre_pci_realize(PCIDevice *d, Error **errp)
  417. {
  418. pci_set_word(d->config + PCI_COMMAND,
  419. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  420. pci_set_word(d->config + PCI_STATUS,
  421. PCI_STATUS_FAST_BACK | PCI_STATUS_66MHZ |
  422. PCI_STATUS_DEVSEL_MEDIUM);
  423. }
  424. static void sabre_pci_class_init(ObjectClass *klass, void *data)
  425. {
  426. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  427. DeviceClass *dc = DEVICE_CLASS(klass);
  428. k->realize = sabre_pci_realize;
  429. k->vendor_id = PCI_VENDOR_ID_SUN;
  430. k->device_id = PCI_DEVICE_ID_SUN_SABRE;
  431. k->class_id = PCI_CLASS_BRIDGE_HOST;
  432. /*
  433. * PCI-facing part of the host bridge, not usable without the
  434. * host-facing part, which can't be device_add'ed, yet.
  435. */
  436. dc->user_creatable = false;
  437. }
  438. static const TypeInfo sabre_pci_info = {
  439. .name = TYPE_SABRE_PCI_DEVICE,
  440. .parent = TYPE_PCI_DEVICE,
  441. .instance_size = sizeof(SabrePCIState),
  442. .class_init = sabre_pci_class_init,
  443. .interfaces = (InterfaceInfo[]) {
  444. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  445. { },
  446. },
  447. };
  448. static char *sabre_ofw_unit_address(const SysBusDevice *dev)
  449. {
  450. SabreState *s = SABRE_DEVICE(dev);
  451. return g_strdup_printf("%x,%x",
  452. (uint32_t)((s->special_base >> 32) & 0xffffffff),
  453. (uint32_t)(s->special_base & 0xffffffff));
  454. }
  455. static Property sabre_properties[] = {
  456. DEFINE_PROP_UINT64("special-base", SabreState, special_base, 0),
  457. DEFINE_PROP_UINT64("mem-base", SabreState, mem_base, 0),
  458. DEFINE_PROP_END_OF_LIST(),
  459. };
  460. static void sabre_class_init(ObjectClass *klass, void *data)
  461. {
  462. DeviceClass *dc = DEVICE_CLASS(klass);
  463. SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
  464. dc->realize = sabre_realize;
  465. dc->reset = sabre_reset;
  466. device_class_set_props(dc, sabre_properties);
  467. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  468. dc->fw_name = "pci";
  469. sbc->explicit_ofw_unit_address = sabre_ofw_unit_address;
  470. }
  471. static const TypeInfo sabre_info = {
  472. .name = TYPE_SABRE,
  473. .parent = TYPE_PCI_HOST_BRIDGE,
  474. .instance_size = sizeof(SabreState),
  475. .instance_init = sabre_init,
  476. .class_init = sabre_class_init,
  477. };
  478. static void sabre_register_types(void)
  479. {
  480. type_register_static(&sabre_info);
  481. type_register_static(&sabre_pci_info);
  482. }
  483. type_init(sabre_register_types)