sabre.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 "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. * 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 & 0xffff) {
  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;
  183. switch (addr & 0xffff) {
  184. case 0x30 ... 0x4f: /* DMA error registers */
  185. val = 0;
  186. /* XXX: not implemented yet */
  187. break;
  188. case 0xc00 ... 0xc3f: /* PCI interrupt control */
  189. if (addr & 4) {
  190. val = s->pci_irq_map[(addr & 0x3f) >> 3];
  191. } else {
  192. val = 0;
  193. }
  194. break;
  195. case 0x1000 ... 0x107f: /* OBIO interrupt control */
  196. if (addr & 4) {
  197. val = s->obio_irq_map[(addr & 0xff) >> 3];
  198. } else {
  199. val = 0;
  200. }
  201. break;
  202. case 0x1080 ... 0x108f: /* PCI bus error */
  203. if (addr & 4) {
  204. val = s->pci_err_irq_map[(addr & 0xf) >> 3];
  205. } else {
  206. val = 0;
  207. }
  208. break;
  209. case 0x2000 ... 0x202f: /* PCI control */
  210. val = s->pci_control[(addr & 0x3f) >> 2];
  211. break;
  212. case 0xf020 ... 0xf027: /* Reset control */
  213. if (addr & 4) {
  214. val = s->reset_control;
  215. } else {
  216. val = 0;
  217. }
  218. break;
  219. case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
  220. case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
  221. case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
  222. case 0xf000 ... 0xf01f: /* FFB config, memory control */
  223. /* we don't care */
  224. default:
  225. val = 0;
  226. break;
  227. }
  228. trace_sabre_config_read(addr, val);
  229. return val;
  230. }
  231. static const MemoryRegionOps sabre_config_ops = {
  232. .read = sabre_config_read,
  233. .write = sabre_config_write,
  234. .endianness = DEVICE_BIG_ENDIAN,
  235. };
  236. static void sabre_pci_config_write(void *opaque, hwaddr addr,
  237. uint64_t val, unsigned size)
  238. {
  239. SabreState *s = opaque;
  240. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  241. trace_sabre_pci_config_write(addr, val);
  242. pci_data_write(phb->bus, addr, val, size);
  243. }
  244. static uint64_t sabre_pci_config_read(void *opaque, hwaddr addr,
  245. unsigned size)
  246. {
  247. uint32_t ret;
  248. SabreState *s = opaque;
  249. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  250. ret = pci_data_read(phb->bus, addr, size);
  251. trace_sabre_pci_config_read(addr, ret);
  252. return ret;
  253. }
  254. /* The sabre host has an IRQ line for each IRQ line of each slot. */
  255. static int pci_sabre_map_irq(PCIDevice *pci_dev, int irq_num)
  256. {
  257. /* Return the irq as swizzled by the PBM */
  258. return irq_num;
  259. }
  260. static int pci_simbaA_map_irq(PCIDevice *pci_dev, int irq_num)
  261. {
  262. /* The on-board devices have fixed (legacy) OBIO intnos */
  263. switch (PCI_SLOT(pci_dev->devfn)) {
  264. case 1:
  265. /* Onboard NIC */
  266. return OBIO_NIC_IRQ;
  267. case 3:
  268. /* Onboard IDE */
  269. return OBIO_HDD_IRQ;
  270. default:
  271. /* Normal intno, fall through */
  272. break;
  273. }
  274. return ((PCI_SLOT(pci_dev->devfn) << 2) + irq_num) & 0x1f;
  275. }
  276. static int pci_simbaB_map_irq(PCIDevice *pci_dev, int irq_num)
  277. {
  278. return (0x10 + (PCI_SLOT(pci_dev->devfn) << 2) + irq_num) & 0x1f;
  279. }
  280. static void pci_sabre_set_irq(void *opaque, int irq_num, int level)
  281. {
  282. SabreState *s = opaque;
  283. trace_sabre_pci_set_irq(irq_num, level);
  284. /* PCI IRQ map onto the first 32 INO. */
  285. if (irq_num < 32) {
  286. if (level) {
  287. s->pci_irq_in |= 1ULL << irq_num;
  288. if (s->pci_irq_map[irq_num >> 2] & PBM_PCI_IMR_ENABLED) {
  289. sabre_set_request(s, irq_num);
  290. }
  291. } else {
  292. s->pci_irq_in &= ~(1ULL << irq_num);
  293. }
  294. } else {
  295. /* OBIO IRQ map onto the next 32 INO. */
  296. if (level) {
  297. trace_sabre_pci_set_obio_irq(irq_num, level);
  298. s->pci_irq_in |= 1ULL << irq_num;
  299. if ((s->irq_request == NO_IRQ_REQUEST)
  300. && (s->obio_irq_map[irq_num - 32] & PBM_PCI_IMR_ENABLED)) {
  301. sabre_set_request(s, irq_num);
  302. }
  303. } else {
  304. s->pci_irq_in &= ~(1ULL << irq_num);
  305. }
  306. }
  307. }
  308. static void sabre_reset(DeviceState *d)
  309. {
  310. SabreState *s = SABRE_DEVICE(d);
  311. PCIDevice *pci_dev;
  312. unsigned int i;
  313. uint16_t cmd;
  314. for (i = 0; i < 8; i++) {
  315. s->pci_irq_map[i] &= PBM_PCI_IMR_MASK;
  316. }
  317. for (i = 0; i < 32; i++) {
  318. s->obio_irq_map[i] &= PBM_PCI_IMR_MASK;
  319. }
  320. s->irq_request = NO_IRQ_REQUEST;
  321. s->pci_irq_in = 0ULL;
  322. if (s->nr_resets++ == 0) {
  323. /* Power on reset */
  324. s->reset_control = POR;
  325. }
  326. /* As this is the busA PCI bridge which contains the on-board devices
  327. * attached to the ebus, ensure that we initially allow IO transactions
  328. * so that we get the early serial console until OpenBIOS can properly
  329. * configure the PCI bridge itself */
  330. pci_dev = PCI_DEVICE(s->bridgeA);
  331. cmd = pci_get_word(pci_dev->config + PCI_COMMAND);
  332. pci_set_word(pci_dev->config + PCI_COMMAND, cmd | PCI_COMMAND_IO);
  333. pci_bridge_update_mappings(PCI_BRIDGE(pci_dev));
  334. }
  335. static const MemoryRegionOps pci_config_ops = {
  336. .read = sabre_pci_config_read,
  337. .write = sabre_pci_config_write,
  338. .endianness = DEVICE_LITTLE_ENDIAN,
  339. };
  340. static void sabre_realize(DeviceState *dev, Error **errp)
  341. {
  342. SabreState *s = SABRE_DEVICE(dev);
  343. PCIHostState *phb = PCI_HOST_BRIDGE(dev);
  344. SysBusDevice *sbd = SYS_BUS_DEVICE(s);
  345. PCIDevice *pci_dev;
  346. /* sabre_config */
  347. sysbus_mmio_map(sbd, 0, s->special_base);
  348. /* PCI configuration space */
  349. sysbus_mmio_map(sbd, 1, s->special_base + 0x1000000ULL);
  350. /* pci_ioport */
  351. sysbus_mmio_map(sbd, 2, s->special_base + 0x2000000ULL);
  352. memory_region_init(&s->pci_mmio, OBJECT(s), "pci-mmio", 0x100000000ULL);
  353. memory_region_add_subregion(get_system_memory(), s->mem_base,
  354. &s->pci_mmio);
  355. phb->bus = pci_register_root_bus(dev, "pci",
  356. pci_sabre_set_irq, pci_sabre_map_irq, s,
  357. &s->pci_mmio,
  358. &s->pci_ioport,
  359. 0, 32, TYPE_PCI_BUS);
  360. pci_create_simple(phb->bus, 0, TYPE_SABRE_PCI_DEVICE);
  361. /* IOMMU */
  362. memory_region_add_subregion_overlap(&s->sabre_config, 0x200,
  363. sysbus_mmio_get_region(SYS_BUS_DEVICE(s->iommu), 0), 1);
  364. pci_setup_iommu(phb->bus, sabre_pci_dma_iommu, s->iommu);
  365. /* APB secondary busses */
  366. pci_dev = pci_create_multifunction(phb->bus, PCI_DEVFN(1, 0), true,
  367. TYPE_SIMBA_PCI_BRIDGE);
  368. s->bridgeB = PCI_BRIDGE(pci_dev);
  369. pci_bridge_map_irq(s->bridgeB, "pciB", pci_simbaB_map_irq);
  370. qdev_init_nofail(&pci_dev->qdev);
  371. pci_dev = pci_create_multifunction(phb->bus, PCI_DEVFN(1, 1), true,
  372. TYPE_SIMBA_PCI_BRIDGE);
  373. s->bridgeA = PCI_BRIDGE(pci_dev);
  374. pci_bridge_map_irq(s->bridgeA, "pciA", pci_simbaA_map_irq);
  375. qdev_init_nofail(&pci_dev->qdev);
  376. }
  377. static void sabre_init(Object *obj)
  378. {
  379. SabreState *s = SABRE_DEVICE(obj);
  380. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  381. unsigned int i;
  382. for (i = 0; i < 8; i++) {
  383. s->pci_irq_map[i] = (0x1f << 6) | (i << 2);
  384. }
  385. for (i = 0; i < 2; i++) {
  386. s->pci_err_irq_map[i] = (0x1f << 6) | 0x30;
  387. }
  388. for (i = 0; i < 32; i++) {
  389. s->obio_irq_map[i] = ((0x1f << 6) | 0x20) + i;
  390. }
  391. qdev_init_gpio_in_named(DEVICE(s), pci_sabre_set_irq, "pbm-irq", MAX_IVEC);
  392. qdev_init_gpio_out_named(DEVICE(s), s->ivec_irqs, "ivec-irq", MAX_IVEC);
  393. s->irq_request = NO_IRQ_REQUEST;
  394. s->pci_irq_in = 0ULL;
  395. /* IOMMU */
  396. object_property_add_link(obj, "iommu", TYPE_SUN4U_IOMMU,
  397. (Object **) &s->iommu,
  398. qdev_prop_allow_set_link_before_realize,
  399. 0, NULL);
  400. /* sabre_config */
  401. memory_region_init_io(&s->sabre_config, OBJECT(s), &sabre_config_ops, s,
  402. "sabre-config", 0x10000);
  403. /* at region 0 */
  404. sysbus_init_mmio(sbd, &s->sabre_config);
  405. memory_region_init_io(&s->pci_config, OBJECT(s), &pci_config_ops, s,
  406. "sabre-pci-config", 0x1000000);
  407. /* at region 1 */
  408. sysbus_init_mmio(sbd, &s->pci_config);
  409. /* pci_ioport */
  410. memory_region_init(&s->pci_ioport, OBJECT(s), "sabre-pci-ioport",
  411. 0x1000000);
  412. /* at region 2 */
  413. sysbus_init_mmio(sbd, &s->pci_ioport);
  414. }
  415. static void sabre_pci_realize(PCIDevice *d, Error **errp)
  416. {
  417. pci_set_word(d->config + PCI_COMMAND,
  418. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  419. pci_set_word(d->config + PCI_STATUS,
  420. PCI_STATUS_FAST_BACK | PCI_STATUS_66MHZ |
  421. PCI_STATUS_DEVSEL_MEDIUM);
  422. }
  423. static void sabre_pci_class_init(ObjectClass *klass, void *data)
  424. {
  425. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  426. DeviceClass *dc = DEVICE_CLASS(klass);
  427. k->realize = sabre_pci_realize;
  428. k->vendor_id = PCI_VENDOR_ID_SUN;
  429. k->device_id = PCI_DEVICE_ID_SUN_SABRE;
  430. k->class_id = PCI_CLASS_BRIDGE_HOST;
  431. /*
  432. * PCI-facing part of the host bridge, not usable without the
  433. * host-facing part, which can't be device_add'ed, yet.
  434. */
  435. dc->user_creatable = false;
  436. }
  437. static const TypeInfo sabre_pci_info = {
  438. .name = TYPE_SABRE_PCI_DEVICE,
  439. .parent = TYPE_PCI_DEVICE,
  440. .instance_size = sizeof(SabrePCIState),
  441. .class_init = sabre_pci_class_init,
  442. .interfaces = (InterfaceInfo[]) {
  443. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  444. { },
  445. },
  446. };
  447. static char *sabre_ofw_unit_address(const SysBusDevice *dev)
  448. {
  449. SabreState *s = SABRE_DEVICE(dev);
  450. return g_strdup_printf("%x,%x",
  451. (uint32_t)((s->special_base >> 32) & 0xffffffff),
  452. (uint32_t)(s->special_base & 0xffffffff));
  453. }
  454. static Property sabre_properties[] = {
  455. DEFINE_PROP_UINT64("special-base", SabreState, special_base, 0),
  456. DEFINE_PROP_UINT64("mem-base", SabreState, mem_base, 0),
  457. DEFINE_PROP_END_OF_LIST(),
  458. };
  459. static void sabre_class_init(ObjectClass *klass, void *data)
  460. {
  461. DeviceClass *dc = DEVICE_CLASS(klass);
  462. SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
  463. dc->realize = sabre_realize;
  464. dc->reset = sabre_reset;
  465. dc->props = sabre_properties;
  466. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  467. dc->fw_name = "pci";
  468. sbc->explicit_ofw_unit_address = sabre_ofw_unit_address;
  469. }
  470. static const TypeInfo sabre_info = {
  471. .name = TYPE_SABRE,
  472. .parent = TYPE_PCI_HOST_BRIDGE,
  473. .instance_size = sizeof(SabreState),
  474. .instance_init = sabre_init,
  475. .class_init = sabre_class_init,
  476. };
  477. static void sabre_register_types(void)
  478. {
  479. type_register_static(&sabre_info);
  480. type_register_static(&sabre_pci_info);
  481. }
  482. type_init(sabre_register_types)