raven.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * QEMU PREP PCI host
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. * Copyright (c) 2011-2013 Andreas Färber
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu/datadir.h"
  27. #include "qemu/units.h"
  28. #include "qemu/log.h"
  29. #include "qapi/error.h"
  30. #include "hw/pci/pci_device.h"
  31. #include "hw/pci/pci_bus.h"
  32. #include "hw/pci/pci_host.h"
  33. #include "hw/qdev-properties.h"
  34. #include "migration/vmstate.h"
  35. #include "hw/intc/i8259.h"
  36. #include "hw/irq.h"
  37. #include "hw/loader.h"
  38. #include "hw/or-irq.h"
  39. #include "elf.h"
  40. #include "qom/object.h"
  41. #define TYPE_RAVEN_PCI_DEVICE "raven"
  42. #define TYPE_RAVEN_PCI_HOST_BRIDGE "raven-pcihost"
  43. OBJECT_DECLARE_SIMPLE_TYPE(RavenPCIState, RAVEN_PCI_DEVICE)
  44. struct RavenPCIState {
  45. PCIDevice dev;
  46. uint32_t elf_machine;
  47. char *bios_name;
  48. MemoryRegion bios;
  49. };
  50. typedef struct PRePPCIState PREPPCIState;
  51. DECLARE_INSTANCE_CHECKER(PREPPCIState, RAVEN_PCI_HOST_BRIDGE,
  52. TYPE_RAVEN_PCI_HOST_BRIDGE)
  53. struct PRePPCIState {
  54. PCIHostState parent_obj;
  55. OrIRQState *or_irq;
  56. qemu_irq pci_irqs[PCI_NUM_PINS];
  57. PCIBus pci_bus;
  58. AddressSpace pci_io_as;
  59. MemoryRegion pci_io;
  60. MemoryRegion pci_io_non_contiguous;
  61. MemoryRegion pci_memory;
  62. MemoryRegion pci_intack;
  63. MemoryRegion bm;
  64. MemoryRegion bm_ram_alias;
  65. MemoryRegion bm_pci_memory_alias;
  66. AddressSpace bm_as;
  67. RavenPCIState pci_dev;
  68. int contiguous_map;
  69. bool is_legacy_prep;
  70. };
  71. #define BIOS_SIZE (1 * MiB)
  72. #define PCI_IO_BASE_ADDR 0x80000000 /* Physical address on main bus */
  73. static inline uint32_t raven_pci_io_config(hwaddr addr)
  74. {
  75. int i;
  76. for (i = 0; i < 11; i++) {
  77. if ((addr & (1 << (11 + i))) != 0) {
  78. break;
  79. }
  80. }
  81. return (addr & 0x7ff) | (i << 11);
  82. }
  83. static void raven_pci_io_write(void *opaque, hwaddr addr,
  84. uint64_t val, unsigned int size)
  85. {
  86. PREPPCIState *s = opaque;
  87. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  88. pci_data_write(phb->bus, raven_pci_io_config(addr), val, size);
  89. }
  90. static uint64_t raven_pci_io_read(void *opaque, hwaddr addr,
  91. unsigned int size)
  92. {
  93. PREPPCIState *s = opaque;
  94. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  95. return pci_data_read(phb->bus, raven_pci_io_config(addr), size);
  96. }
  97. static const MemoryRegionOps raven_pci_io_ops = {
  98. .read = raven_pci_io_read,
  99. .write = raven_pci_io_write,
  100. .endianness = DEVICE_LITTLE_ENDIAN,
  101. };
  102. static uint64_t raven_intack_read(void *opaque, hwaddr addr,
  103. unsigned int size)
  104. {
  105. return pic_read_irq(isa_pic);
  106. }
  107. static void raven_intack_write(void *opaque, hwaddr addr,
  108. uint64_t data, unsigned size)
  109. {
  110. qemu_log_mask(LOG_UNIMP, "%s not implemented\n", __func__);
  111. }
  112. static const MemoryRegionOps raven_intack_ops = {
  113. .read = raven_intack_read,
  114. .write = raven_intack_write,
  115. .valid = {
  116. .max_access_size = 1,
  117. },
  118. };
  119. static inline hwaddr raven_io_address(PREPPCIState *s,
  120. hwaddr addr)
  121. {
  122. if (s->contiguous_map == 0) {
  123. /* 64 KB contiguous space for IOs */
  124. addr &= 0xFFFF;
  125. } else {
  126. /* 8 MB non-contiguous space for IOs */
  127. addr = (addr & 0x1F) | ((addr & 0x007FFF000) >> 7);
  128. }
  129. /* FIXME: handle endianness switch */
  130. return addr;
  131. }
  132. static uint64_t raven_io_read(void *opaque, hwaddr addr,
  133. unsigned int size)
  134. {
  135. PREPPCIState *s = opaque;
  136. uint8_t buf[4];
  137. addr = raven_io_address(s, addr);
  138. address_space_read(&s->pci_io_as, addr + PCI_IO_BASE_ADDR,
  139. MEMTXATTRS_UNSPECIFIED, buf, size);
  140. if (size == 1) {
  141. return buf[0];
  142. } else if (size == 2) {
  143. return lduw_le_p(buf);
  144. } else if (size == 4) {
  145. return ldl_le_p(buf);
  146. } else {
  147. g_assert_not_reached();
  148. }
  149. }
  150. static void raven_io_write(void *opaque, hwaddr addr,
  151. uint64_t val, unsigned int size)
  152. {
  153. PREPPCIState *s = opaque;
  154. uint8_t buf[4];
  155. addr = raven_io_address(s, addr);
  156. if (size == 1) {
  157. buf[0] = val;
  158. } else if (size == 2) {
  159. stw_le_p(buf, val);
  160. } else if (size == 4) {
  161. stl_le_p(buf, val);
  162. } else {
  163. g_assert_not_reached();
  164. }
  165. address_space_write(&s->pci_io_as, addr + PCI_IO_BASE_ADDR,
  166. MEMTXATTRS_UNSPECIFIED, buf, size);
  167. }
  168. static const MemoryRegionOps raven_io_ops = {
  169. .read = raven_io_read,
  170. .write = raven_io_write,
  171. .endianness = DEVICE_LITTLE_ENDIAN,
  172. .impl.max_access_size = 4,
  173. .impl.unaligned = true,
  174. .valid.unaligned = true,
  175. };
  176. static int raven_map_irq(PCIDevice *pci_dev, int irq_num)
  177. {
  178. return (irq_num + (pci_dev->devfn >> 3)) & 1;
  179. }
  180. static void raven_set_irq(void *opaque, int irq_num, int level)
  181. {
  182. PREPPCIState *s = opaque;
  183. qemu_set_irq(s->pci_irqs[irq_num], level);
  184. }
  185. static AddressSpace *raven_pcihost_set_iommu(PCIBus *bus, void *opaque,
  186. int devfn)
  187. {
  188. PREPPCIState *s = opaque;
  189. return &s->bm_as;
  190. }
  191. static const PCIIOMMUOps raven_iommu_ops = {
  192. .get_address_space = raven_pcihost_set_iommu,
  193. };
  194. static void raven_change_gpio(void *opaque, int n, int level)
  195. {
  196. PREPPCIState *s = opaque;
  197. s->contiguous_map = level;
  198. }
  199. static void raven_pcihost_realizefn(DeviceState *d, Error **errp)
  200. {
  201. SysBusDevice *dev = SYS_BUS_DEVICE(d);
  202. PCIHostState *h = PCI_HOST_BRIDGE(dev);
  203. PREPPCIState *s = RAVEN_PCI_HOST_BRIDGE(dev);
  204. MemoryRegion *address_space_mem = get_system_memory();
  205. int i;
  206. if (s->is_legacy_prep) {
  207. for (i = 0; i < PCI_NUM_PINS; i++) {
  208. sysbus_init_irq(dev, &s->pci_irqs[i]);
  209. }
  210. } else {
  211. /* According to PReP specification section 6.1.6 "System Interrupt
  212. * Assignments", all PCI interrupts are routed via IRQ 15 */
  213. s->or_irq = OR_IRQ(object_new(TYPE_OR_IRQ));
  214. object_property_set_int(OBJECT(s->or_irq), "num-lines", PCI_NUM_PINS,
  215. &error_fatal);
  216. qdev_realize(DEVICE(s->or_irq), NULL, &error_fatal);
  217. sysbus_init_irq(dev, &s->or_irq->out_irq);
  218. for (i = 0; i < PCI_NUM_PINS; i++) {
  219. s->pci_irqs[i] = qdev_get_gpio_in(DEVICE(s->or_irq), i);
  220. }
  221. }
  222. qdev_init_gpio_in(d, raven_change_gpio, 1);
  223. pci_bus_irqs(&s->pci_bus, raven_set_irq, s, PCI_NUM_PINS);
  224. pci_bus_map_irqs(&s->pci_bus, raven_map_irq);
  225. memory_region_init_io(&h->conf_mem, OBJECT(h), &pci_host_conf_le_ops, s,
  226. "pci-conf-idx", 4);
  227. memory_region_add_subregion(&s->pci_io, 0xcf8, &h->conf_mem);
  228. memory_region_init_io(&h->data_mem, OBJECT(h), &pci_host_data_le_ops, s,
  229. "pci-conf-data", 4);
  230. memory_region_add_subregion(&s->pci_io, 0xcfc, &h->data_mem);
  231. memory_region_init_io(&h->mmcfg, OBJECT(s), &raven_pci_io_ops, s,
  232. "pciio", 0x00400000);
  233. memory_region_add_subregion(address_space_mem, 0x80800000, &h->mmcfg);
  234. memory_region_init_io(&s->pci_intack, OBJECT(s), &raven_intack_ops, s,
  235. "pci-intack", 1);
  236. memory_region_add_subregion(address_space_mem, 0xbffffff0, &s->pci_intack);
  237. /* TODO Remove once realize propagates to child devices. */
  238. qdev_realize(DEVICE(&s->pci_dev), BUS(&s->pci_bus), errp);
  239. }
  240. static void raven_pcihost_initfn(Object *obj)
  241. {
  242. PCIHostState *h = PCI_HOST_BRIDGE(obj);
  243. PREPPCIState *s = RAVEN_PCI_HOST_BRIDGE(obj);
  244. MemoryRegion *address_space_mem = get_system_memory();
  245. DeviceState *pci_dev;
  246. memory_region_init(&s->pci_io, obj, "pci-io", 0x3f800000);
  247. memory_region_init_io(&s->pci_io_non_contiguous, obj, &raven_io_ops, s,
  248. "pci-io-non-contiguous", 0x00800000);
  249. memory_region_init(&s->pci_memory, obj, "pci-memory", 0x3f000000);
  250. address_space_init(&s->pci_io_as, &s->pci_io, "raven-io");
  251. /*
  252. * Raven's raven_io_ops use the address-space API to access pci-conf-idx
  253. * (which is also owned by the raven device). As such, mark the
  254. * pci_io_non_contiguous as re-entrancy safe.
  255. */
  256. s->pci_io_non_contiguous.disable_reentrancy_guard = true;
  257. /* CPU address space */
  258. memory_region_add_subregion(address_space_mem, PCI_IO_BASE_ADDR,
  259. &s->pci_io);
  260. memory_region_add_subregion_overlap(address_space_mem, PCI_IO_BASE_ADDR,
  261. &s->pci_io_non_contiguous, 1);
  262. memory_region_add_subregion(address_space_mem, 0xc0000000, &s->pci_memory);
  263. pci_root_bus_init(&s->pci_bus, sizeof(s->pci_bus), DEVICE(obj), NULL,
  264. &s->pci_memory, &s->pci_io, 0, TYPE_PCI_BUS);
  265. /* Bus master address space */
  266. memory_region_init(&s->bm, obj, "bm-raven", 4 * GiB);
  267. memory_region_init_alias(&s->bm_pci_memory_alias, obj, "bm-pci-memory",
  268. &s->pci_memory, 0,
  269. memory_region_size(&s->pci_memory));
  270. memory_region_init_alias(&s->bm_ram_alias, obj, "bm-system",
  271. get_system_memory(), 0, 0x80000000);
  272. memory_region_add_subregion(&s->bm, 0 , &s->bm_pci_memory_alias);
  273. memory_region_add_subregion(&s->bm, 0x80000000, &s->bm_ram_alias);
  274. address_space_init(&s->bm_as, &s->bm, "raven-bm");
  275. pci_setup_iommu(&s->pci_bus, &raven_iommu_ops, s);
  276. h->bus = &s->pci_bus;
  277. object_initialize(&s->pci_dev, sizeof(s->pci_dev), TYPE_RAVEN_PCI_DEVICE);
  278. pci_dev = DEVICE(&s->pci_dev);
  279. object_property_set_int(OBJECT(&s->pci_dev), "addr", PCI_DEVFN(0, 0),
  280. NULL);
  281. qdev_prop_set_bit(pci_dev, "multifunction", false);
  282. }
  283. static void raven_realize(PCIDevice *d, Error **errp)
  284. {
  285. RavenPCIState *s = RAVEN_PCI_DEVICE(d);
  286. char *filename;
  287. int bios_size = -1;
  288. d->config[PCI_CACHE_LINE_SIZE] = 0x08;
  289. d->config[PCI_LATENCY_TIMER] = 0x10;
  290. d->config[PCI_CAPABILITY_LIST] = 0x00;
  291. if (!memory_region_init_rom_nomigrate(&s->bios, OBJECT(s), "bios",
  292. BIOS_SIZE, errp)) {
  293. return;
  294. }
  295. memory_region_add_subregion(get_system_memory(), (uint32_t)(-BIOS_SIZE),
  296. &s->bios);
  297. if (s->bios_name) {
  298. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, s->bios_name);
  299. if (filename) {
  300. if (s->elf_machine != EM_NONE) {
  301. bios_size = load_elf(filename, NULL, NULL, NULL, NULL,
  302. NULL, NULL, NULL,
  303. ELFDATA2MSB, s->elf_machine, 0, 0);
  304. }
  305. if (bios_size < 0) {
  306. bios_size = get_image_size(filename);
  307. if (bios_size > 0 && bios_size <= BIOS_SIZE) {
  308. hwaddr bios_addr;
  309. bios_size = (bios_size + 0xfff) & ~0xfff;
  310. bios_addr = (uint32_t)(-BIOS_SIZE);
  311. bios_size = load_image_targphys(filename, bios_addr,
  312. bios_size);
  313. }
  314. }
  315. }
  316. g_free(filename);
  317. if (bios_size < 0 || bios_size > BIOS_SIZE) {
  318. memory_region_del_subregion(get_system_memory(), &s->bios);
  319. error_setg(errp, "Could not load bios image '%s'", s->bios_name);
  320. return;
  321. }
  322. }
  323. vmstate_register_ram_global(&s->bios);
  324. }
  325. static const VMStateDescription vmstate_raven = {
  326. .name = "raven",
  327. .version_id = 0,
  328. .minimum_version_id = 0,
  329. .fields = (const VMStateField[]) {
  330. VMSTATE_PCI_DEVICE(dev, RavenPCIState),
  331. VMSTATE_END_OF_LIST()
  332. },
  333. };
  334. static void raven_class_init(ObjectClass *klass, void *data)
  335. {
  336. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  337. DeviceClass *dc = DEVICE_CLASS(klass);
  338. k->realize = raven_realize;
  339. k->vendor_id = PCI_VENDOR_ID_MOTOROLA;
  340. k->device_id = PCI_DEVICE_ID_MOTOROLA_RAVEN;
  341. k->revision = 0x00;
  342. k->class_id = PCI_CLASS_BRIDGE_HOST;
  343. dc->desc = "PReP Host Bridge - Motorola Raven";
  344. dc->vmsd = &vmstate_raven;
  345. /*
  346. * Reason: PCI-facing part of the host bridge, not usable without
  347. * the host-facing part, which can't be device_add'ed, yet.
  348. */
  349. dc->user_creatable = false;
  350. }
  351. static const TypeInfo raven_info = {
  352. .name = TYPE_RAVEN_PCI_DEVICE,
  353. .parent = TYPE_PCI_DEVICE,
  354. .instance_size = sizeof(RavenPCIState),
  355. .class_init = raven_class_init,
  356. .interfaces = (InterfaceInfo[]) {
  357. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  358. { },
  359. },
  360. };
  361. static const Property raven_pcihost_properties[] = {
  362. DEFINE_PROP_UINT32("elf-machine", PREPPCIState, pci_dev.elf_machine,
  363. EM_NONE),
  364. DEFINE_PROP_STRING("bios-name", PREPPCIState, pci_dev.bios_name),
  365. /* Temporary workaround until legacy prep machine is removed */
  366. DEFINE_PROP_BOOL("is-legacy-prep", PREPPCIState, is_legacy_prep,
  367. false),
  368. };
  369. static void raven_pcihost_class_init(ObjectClass *klass, void *data)
  370. {
  371. DeviceClass *dc = DEVICE_CLASS(klass);
  372. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  373. dc->realize = raven_pcihost_realizefn;
  374. device_class_set_props(dc, raven_pcihost_properties);
  375. dc->fw_name = "pci";
  376. }
  377. static const TypeInfo raven_pcihost_info = {
  378. .name = TYPE_RAVEN_PCI_HOST_BRIDGE,
  379. .parent = TYPE_PCI_HOST_BRIDGE,
  380. .instance_size = sizeof(PREPPCIState),
  381. .instance_init = raven_pcihost_initfn,
  382. .class_init = raven_pcihost_class_init,
  383. };
  384. static void raven_register_types(void)
  385. {
  386. type_register_static(&raven_pcihost_info);
  387. type_register_static(&raven_info);
  388. }
  389. type_init(raven_register_types)