sabre.c 16 KB

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