2
0

sabre.c 16 KB

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