raven.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. .valid.unaligned = true,
  174. };
  175. static int raven_map_irq(PCIDevice *pci_dev, int irq_num)
  176. {
  177. return (irq_num + (pci_dev->devfn >> 3)) & 1;
  178. }
  179. static void raven_set_irq(void *opaque, int irq_num, int level)
  180. {
  181. PREPPCIState *s = opaque;
  182. qemu_set_irq(s->pci_irqs[irq_num], level);
  183. }
  184. static AddressSpace *raven_pcihost_set_iommu(PCIBus *bus, void *opaque,
  185. int devfn)
  186. {
  187. PREPPCIState *s = opaque;
  188. return &s->bm_as;
  189. }
  190. static void raven_change_gpio(void *opaque, int n, int level)
  191. {
  192. PREPPCIState *s = opaque;
  193. s->contiguous_map = level;
  194. }
  195. static void raven_pcihost_realizefn(DeviceState *d, Error **errp)
  196. {
  197. SysBusDevice *dev = SYS_BUS_DEVICE(d);
  198. PCIHostState *h = PCI_HOST_BRIDGE(dev);
  199. PREPPCIState *s = RAVEN_PCI_HOST_BRIDGE(dev);
  200. MemoryRegion *address_space_mem = get_system_memory();
  201. int i;
  202. if (s->is_legacy_prep) {
  203. for (i = 0; i < PCI_NUM_PINS; i++) {
  204. sysbus_init_irq(dev, &s->pci_irqs[i]);
  205. }
  206. } else {
  207. /* According to PReP specification section 6.1.6 "System Interrupt
  208. * Assignments", all PCI interrupts are routed via IRQ 15 */
  209. s->or_irq = OR_IRQ(object_new(TYPE_OR_IRQ));
  210. object_property_set_int(OBJECT(s->or_irq), "num-lines", PCI_NUM_PINS,
  211. &error_fatal);
  212. qdev_realize(DEVICE(s->or_irq), NULL, &error_fatal);
  213. sysbus_init_irq(dev, &s->or_irq->out_irq);
  214. for (i = 0; i < PCI_NUM_PINS; i++) {
  215. s->pci_irqs[i] = qdev_get_gpio_in(DEVICE(s->or_irq), i);
  216. }
  217. }
  218. qdev_init_gpio_in(d, raven_change_gpio, 1);
  219. pci_bus_irqs(&s->pci_bus, raven_set_irq, s, PCI_NUM_PINS);
  220. pci_bus_map_irqs(&s->pci_bus, raven_map_irq);
  221. memory_region_init_io(&h->conf_mem, OBJECT(h), &pci_host_conf_le_ops, s,
  222. "pci-conf-idx", 4);
  223. memory_region_add_subregion(&s->pci_io, 0xcf8, &h->conf_mem);
  224. memory_region_init_io(&h->data_mem, OBJECT(h), &pci_host_data_le_ops, s,
  225. "pci-conf-data", 4);
  226. memory_region_add_subregion(&s->pci_io, 0xcfc, &h->data_mem);
  227. memory_region_init_io(&h->mmcfg, OBJECT(s), &raven_pci_io_ops, s,
  228. "pciio", 0x00400000);
  229. memory_region_add_subregion(address_space_mem, 0x80800000, &h->mmcfg);
  230. memory_region_init_io(&s->pci_intack, OBJECT(s), &raven_intack_ops, s,
  231. "pci-intack", 1);
  232. memory_region_add_subregion(address_space_mem, 0xbffffff0, &s->pci_intack);
  233. /* TODO Remove once realize propagates to child devices. */
  234. qdev_realize(DEVICE(&s->pci_dev), BUS(&s->pci_bus), errp);
  235. }
  236. static void raven_pcihost_initfn(Object *obj)
  237. {
  238. PCIHostState *h = PCI_HOST_BRIDGE(obj);
  239. PREPPCIState *s = RAVEN_PCI_HOST_BRIDGE(obj);
  240. MemoryRegion *address_space_mem = get_system_memory();
  241. DeviceState *pci_dev;
  242. memory_region_init(&s->pci_io, obj, "pci-io", 0x3f800000);
  243. memory_region_init_io(&s->pci_io_non_contiguous, obj, &raven_io_ops, s,
  244. "pci-io-non-contiguous", 0x00800000);
  245. memory_region_init(&s->pci_memory, obj, "pci-memory", 0x3f000000);
  246. address_space_init(&s->pci_io_as, &s->pci_io, "raven-io");
  247. /* CPU address space */
  248. memory_region_add_subregion(address_space_mem, PCI_IO_BASE_ADDR,
  249. &s->pci_io);
  250. memory_region_add_subregion_overlap(address_space_mem, PCI_IO_BASE_ADDR,
  251. &s->pci_io_non_contiguous, 1);
  252. memory_region_add_subregion(address_space_mem, 0xc0000000, &s->pci_memory);
  253. pci_root_bus_init(&s->pci_bus, sizeof(s->pci_bus), DEVICE(obj), NULL,
  254. &s->pci_memory, &s->pci_io, 0, TYPE_PCI_BUS);
  255. /* Bus master address space */
  256. memory_region_init(&s->bm, obj, "bm-raven", 4 * GiB);
  257. memory_region_init_alias(&s->bm_pci_memory_alias, obj, "bm-pci-memory",
  258. &s->pci_memory, 0,
  259. memory_region_size(&s->pci_memory));
  260. memory_region_init_alias(&s->bm_ram_alias, obj, "bm-system",
  261. get_system_memory(), 0, 0x80000000);
  262. memory_region_add_subregion(&s->bm, 0 , &s->bm_pci_memory_alias);
  263. memory_region_add_subregion(&s->bm, 0x80000000, &s->bm_ram_alias);
  264. address_space_init(&s->bm_as, &s->bm, "raven-bm");
  265. pci_setup_iommu(&s->pci_bus, raven_pcihost_set_iommu, s);
  266. h->bus = &s->pci_bus;
  267. object_initialize(&s->pci_dev, sizeof(s->pci_dev), TYPE_RAVEN_PCI_DEVICE);
  268. pci_dev = DEVICE(&s->pci_dev);
  269. object_property_set_int(OBJECT(&s->pci_dev), "addr", PCI_DEVFN(0, 0),
  270. NULL);
  271. qdev_prop_set_bit(pci_dev, "multifunction", false);
  272. }
  273. static void raven_realize(PCIDevice *d, Error **errp)
  274. {
  275. RavenPCIState *s = RAVEN_PCI_DEVICE(d);
  276. char *filename;
  277. int bios_size = -1;
  278. d->config[PCI_CACHE_LINE_SIZE] = 0x08;
  279. d->config[PCI_LATENCY_TIMER] = 0x10;
  280. d->config[PCI_CAPABILITY_LIST] = 0x00;
  281. memory_region_init_rom_nomigrate(&s->bios, OBJECT(s), "bios", BIOS_SIZE,
  282. &error_fatal);
  283. memory_region_add_subregion(get_system_memory(), (uint32_t)(-BIOS_SIZE),
  284. &s->bios);
  285. if (s->bios_name) {
  286. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, s->bios_name);
  287. if (filename) {
  288. if (s->elf_machine != EM_NONE) {
  289. bios_size = load_elf(filename, NULL, NULL, NULL, NULL,
  290. NULL, NULL, NULL, 1, s->elf_machine,
  291. 0, 0);
  292. }
  293. if (bios_size < 0) {
  294. bios_size = get_image_size(filename);
  295. if (bios_size > 0 && bios_size <= BIOS_SIZE) {
  296. hwaddr bios_addr;
  297. bios_size = (bios_size + 0xfff) & ~0xfff;
  298. bios_addr = (uint32_t)(-BIOS_SIZE);
  299. bios_size = load_image_targphys(filename, bios_addr,
  300. bios_size);
  301. }
  302. }
  303. }
  304. g_free(filename);
  305. if (bios_size < 0 || bios_size > BIOS_SIZE) {
  306. memory_region_del_subregion(get_system_memory(), &s->bios);
  307. error_setg(errp, "Could not load bios image '%s'", s->bios_name);
  308. return;
  309. }
  310. }
  311. vmstate_register_ram_global(&s->bios);
  312. }
  313. static const VMStateDescription vmstate_raven = {
  314. .name = "raven",
  315. .version_id = 0,
  316. .minimum_version_id = 0,
  317. .fields = (VMStateField[]) {
  318. VMSTATE_PCI_DEVICE(dev, RavenPCIState),
  319. VMSTATE_END_OF_LIST()
  320. },
  321. };
  322. static void raven_class_init(ObjectClass *klass, void *data)
  323. {
  324. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  325. DeviceClass *dc = DEVICE_CLASS(klass);
  326. k->realize = raven_realize;
  327. k->vendor_id = PCI_VENDOR_ID_MOTOROLA;
  328. k->device_id = PCI_DEVICE_ID_MOTOROLA_RAVEN;
  329. k->revision = 0x00;
  330. k->class_id = PCI_CLASS_BRIDGE_HOST;
  331. dc->desc = "PReP Host Bridge - Motorola Raven";
  332. dc->vmsd = &vmstate_raven;
  333. /*
  334. * Reason: PCI-facing part of the host bridge, not usable without
  335. * the host-facing part, which can't be device_add'ed, yet.
  336. */
  337. dc->user_creatable = false;
  338. }
  339. static const TypeInfo raven_info = {
  340. .name = TYPE_RAVEN_PCI_DEVICE,
  341. .parent = TYPE_PCI_DEVICE,
  342. .instance_size = sizeof(RavenPCIState),
  343. .class_init = raven_class_init,
  344. .interfaces = (InterfaceInfo[]) {
  345. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  346. { },
  347. },
  348. };
  349. static Property raven_pcihost_properties[] = {
  350. DEFINE_PROP_UINT32("elf-machine", PREPPCIState, pci_dev.elf_machine,
  351. EM_NONE),
  352. DEFINE_PROP_STRING("bios-name", PREPPCIState, pci_dev.bios_name),
  353. /* Temporary workaround until legacy prep machine is removed */
  354. DEFINE_PROP_BOOL("is-legacy-prep", PREPPCIState, is_legacy_prep,
  355. false),
  356. DEFINE_PROP_END_OF_LIST()
  357. };
  358. static void raven_pcihost_class_init(ObjectClass *klass, void *data)
  359. {
  360. DeviceClass *dc = DEVICE_CLASS(klass);
  361. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  362. dc->realize = raven_pcihost_realizefn;
  363. device_class_set_props(dc, raven_pcihost_properties);
  364. dc->fw_name = "pci";
  365. }
  366. static const TypeInfo raven_pcihost_info = {
  367. .name = TYPE_RAVEN_PCI_HOST_BRIDGE,
  368. .parent = TYPE_PCI_HOST_BRIDGE,
  369. .instance_size = sizeof(PREPPCIState),
  370. .instance_init = raven_pcihost_initfn,
  371. .class_init = raven_pcihost_class_init,
  372. };
  373. static void raven_register_types(void)
  374. {
  375. type_register_static(&raven_pcihost_info);
  376. type_register_static(&raven_info);
  377. }
  378. type_init(raven_register_types)