prep.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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-common.h"
  27. #include "qemu/units.h"
  28. #include "qapi/error.h"
  29. #include "hw/pci/pci.h"
  30. #include "hw/pci/pci_bus.h"
  31. #include "hw/pci/pci_host.h"
  32. #include "hw/qdev-properties.h"
  33. #include "migration/vmstate.h"
  34. #include "hw/intc/i8259.h"
  35. #include "hw/irq.h"
  36. #include "hw/loader.h"
  37. #include "hw/or-irq.h"
  38. #include "exec/address-spaces.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. qemu_or_irq *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. static inline uint32_t raven_pci_io_config(hwaddr addr)
  73. {
  74. int i;
  75. for (i = 0; i < 11; i++) {
  76. if ((addr & (1 << (11 + i))) != 0) {
  77. break;
  78. }
  79. }
  80. return (addr & 0x7ff) | (i << 11);
  81. }
  82. static void raven_pci_io_write(void *opaque, hwaddr addr,
  83. uint64_t val, unsigned int size)
  84. {
  85. PREPPCIState *s = opaque;
  86. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  87. pci_data_write(phb->bus, raven_pci_io_config(addr), val, size);
  88. }
  89. static uint64_t raven_pci_io_read(void *opaque, hwaddr addr,
  90. unsigned int size)
  91. {
  92. PREPPCIState *s = opaque;
  93. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  94. return pci_data_read(phb->bus, raven_pci_io_config(addr), size);
  95. }
  96. static const MemoryRegionOps raven_pci_io_ops = {
  97. .read = raven_pci_io_read,
  98. .write = raven_pci_io_write,
  99. .endianness = DEVICE_LITTLE_ENDIAN,
  100. };
  101. static uint64_t raven_intack_read(void *opaque, hwaddr addr,
  102. unsigned int size)
  103. {
  104. return pic_read_irq(isa_pic);
  105. }
  106. static const MemoryRegionOps raven_intack_ops = {
  107. .read = raven_intack_read,
  108. .valid = {
  109. .max_access_size = 1,
  110. },
  111. };
  112. static inline hwaddr raven_io_address(PREPPCIState *s,
  113. hwaddr addr)
  114. {
  115. if (s->contiguous_map == 0) {
  116. /* 64 KB contiguous space for IOs */
  117. addr &= 0xFFFF;
  118. } else {
  119. /* 8 MB non-contiguous space for IOs */
  120. addr = (addr & 0x1F) | ((addr & 0x007FFF000) >> 7);
  121. }
  122. /* FIXME: handle endianness switch */
  123. return addr;
  124. }
  125. static uint64_t raven_io_read(void *opaque, hwaddr addr,
  126. unsigned int size)
  127. {
  128. PREPPCIState *s = opaque;
  129. uint8_t buf[4];
  130. addr = raven_io_address(s, addr);
  131. address_space_read(&s->pci_io_as, addr + 0x80000000,
  132. MEMTXATTRS_UNSPECIFIED, buf, size);
  133. if (size == 1) {
  134. return buf[0];
  135. } else if (size == 2) {
  136. return lduw_le_p(buf);
  137. } else if (size == 4) {
  138. return ldl_le_p(buf);
  139. } else {
  140. g_assert_not_reached();
  141. }
  142. }
  143. static void raven_io_write(void *opaque, hwaddr addr,
  144. uint64_t val, unsigned int size)
  145. {
  146. PREPPCIState *s = opaque;
  147. uint8_t buf[4];
  148. addr = raven_io_address(s, addr);
  149. if (size == 1) {
  150. buf[0] = val;
  151. } else if (size == 2) {
  152. stw_le_p(buf, val);
  153. } else if (size == 4) {
  154. stl_le_p(buf, val);
  155. } else {
  156. g_assert_not_reached();
  157. }
  158. address_space_write(&s->pci_io_as, addr + 0x80000000,
  159. MEMTXATTRS_UNSPECIFIED, buf, size);
  160. }
  161. static const MemoryRegionOps raven_io_ops = {
  162. .read = raven_io_read,
  163. .write = raven_io_write,
  164. .endianness = DEVICE_LITTLE_ENDIAN,
  165. .impl.max_access_size = 4,
  166. .valid.unaligned = true,
  167. };
  168. static int raven_map_irq(PCIDevice *pci_dev, int irq_num)
  169. {
  170. return (irq_num + (pci_dev->devfn >> 3)) & 1;
  171. }
  172. static void raven_set_irq(void *opaque, int irq_num, int level)
  173. {
  174. PREPPCIState *s = opaque;
  175. qemu_set_irq(s->pci_irqs[irq_num], level);
  176. }
  177. static AddressSpace *raven_pcihost_set_iommu(PCIBus *bus, void *opaque,
  178. int devfn)
  179. {
  180. PREPPCIState *s = opaque;
  181. return &s->bm_as;
  182. }
  183. static void raven_change_gpio(void *opaque, int n, int level)
  184. {
  185. PREPPCIState *s = opaque;
  186. s->contiguous_map = level;
  187. }
  188. static void raven_pcihost_realizefn(DeviceState *d, Error **errp)
  189. {
  190. SysBusDevice *dev = SYS_BUS_DEVICE(d);
  191. PCIHostState *h = PCI_HOST_BRIDGE(dev);
  192. PREPPCIState *s = RAVEN_PCI_HOST_BRIDGE(dev);
  193. MemoryRegion *address_space_mem = get_system_memory();
  194. int i;
  195. if (s->is_legacy_prep) {
  196. for (i = 0; i < PCI_NUM_PINS; i++) {
  197. sysbus_init_irq(dev, &s->pci_irqs[i]);
  198. }
  199. } else {
  200. /* According to PReP specification section 6.1.6 "System Interrupt
  201. * Assignments", all PCI interrupts are routed via IRQ 15 */
  202. s->or_irq = OR_IRQ(object_new(TYPE_OR_IRQ));
  203. object_property_set_int(OBJECT(s->or_irq), "num-lines", PCI_NUM_PINS,
  204. &error_fatal);
  205. qdev_realize(DEVICE(s->or_irq), NULL, &error_fatal);
  206. sysbus_init_irq(dev, &s->or_irq->out_irq);
  207. for (i = 0; i < PCI_NUM_PINS; i++) {
  208. s->pci_irqs[i] = qdev_get_gpio_in(DEVICE(s->or_irq), i);
  209. }
  210. }
  211. qdev_init_gpio_in(d, raven_change_gpio, 1);
  212. pci_bus_irqs(&s->pci_bus, raven_set_irq, raven_map_irq, s, PCI_NUM_PINS);
  213. memory_region_init_io(&h->conf_mem, OBJECT(h), &pci_host_conf_le_ops, s,
  214. "pci-conf-idx", 4);
  215. memory_region_add_subregion(&s->pci_io, 0xcf8, &h->conf_mem);
  216. memory_region_init_io(&h->data_mem, OBJECT(h), &pci_host_data_le_ops, s,
  217. "pci-conf-data", 4);
  218. memory_region_add_subregion(&s->pci_io, 0xcfc, &h->data_mem);
  219. memory_region_init_io(&h->mmcfg, OBJECT(s), &raven_pci_io_ops, s,
  220. "pciio", 0x00400000);
  221. memory_region_add_subregion(address_space_mem, 0x80800000, &h->mmcfg);
  222. memory_region_init_io(&s->pci_intack, OBJECT(s), &raven_intack_ops, s,
  223. "pci-intack", 1);
  224. memory_region_add_subregion(address_space_mem, 0xbffffff0, &s->pci_intack);
  225. /* TODO Remove once realize propagates to child devices. */
  226. qdev_realize(DEVICE(&s->pci_dev), BUS(&s->pci_bus), errp);
  227. }
  228. static void raven_pcihost_initfn(Object *obj)
  229. {
  230. PCIHostState *h = PCI_HOST_BRIDGE(obj);
  231. PREPPCIState *s = RAVEN_PCI_HOST_BRIDGE(obj);
  232. MemoryRegion *address_space_mem = get_system_memory();
  233. DeviceState *pci_dev;
  234. memory_region_init(&s->pci_io, obj, "pci-io", 0x3f800000);
  235. memory_region_init_io(&s->pci_io_non_contiguous, obj, &raven_io_ops, s,
  236. "pci-io-non-contiguous", 0x00800000);
  237. memory_region_init(&s->pci_memory, obj, "pci-memory", 0x3f000000);
  238. address_space_init(&s->pci_io_as, &s->pci_io, "raven-io");
  239. /* CPU address space */
  240. memory_region_add_subregion(address_space_mem, 0x80000000, &s->pci_io);
  241. memory_region_add_subregion_overlap(address_space_mem, 0x80000000,
  242. &s->pci_io_non_contiguous, 1);
  243. memory_region_add_subregion(address_space_mem, 0xc0000000, &s->pci_memory);
  244. pci_root_bus_new_inplace(&s->pci_bus, sizeof(s->pci_bus), DEVICE(obj), NULL,
  245. &s->pci_memory, &s->pci_io, 0, TYPE_PCI_BUS);
  246. /* Bus master address space */
  247. memory_region_init(&s->bm, obj, "bm-raven", 4 * GiB);
  248. memory_region_init_alias(&s->bm_pci_memory_alias, obj, "bm-pci-memory",
  249. &s->pci_memory, 0,
  250. memory_region_size(&s->pci_memory));
  251. memory_region_init_alias(&s->bm_ram_alias, obj, "bm-system",
  252. get_system_memory(), 0, 0x80000000);
  253. memory_region_add_subregion(&s->bm, 0 , &s->bm_pci_memory_alias);
  254. memory_region_add_subregion(&s->bm, 0x80000000, &s->bm_ram_alias);
  255. address_space_init(&s->bm_as, &s->bm, "raven-bm");
  256. pci_setup_iommu(&s->pci_bus, raven_pcihost_set_iommu, s);
  257. h->bus = &s->pci_bus;
  258. object_initialize(&s->pci_dev, sizeof(s->pci_dev), TYPE_RAVEN_PCI_DEVICE);
  259. pci_dev = DEVICE(&s->pci_dev);
  260. object_property_set_int(OBJECT(&s->pci_dev), "addr", PCI_DEVFN(0, 0),
  261. NULL);
  262. qdev_prop_set_bit(pci_dev, "multifunction", false);
  263. }
  264. static void raven_realize(PCIDevice *d, Error **errp)
  265. {
  266. RavenPCIState *s = RAVEN_PCI_DEVICE(d);
  267. char *filename;
  268. int bios_size = -1;
  269. d->config[0x0C] = 0x08; // cache_line_size
  270. d->config[0x0D] = 0x10; // latency_timer
  271. d->config[0x34] = 0x00; // capabilities_pointer
  272. memory_region_init_rom_nomigrate(&s->bios, OBJECT(s), "bios", BIOS_SIZE,
  273. &error_fatal);
  274. memory_region_add_subregion(get_system_memory(), (uint32_t)(-BIOS_SIZE),
  275. &s->bios);
  276. if (s->bios_name) {
  277. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, s->bios_name);
  278. if (filename) {
  279. if (s->elf_machine != EM_NONE) {
  280. bios_size = load_elf(filename, NULL, NULL, NULL, NULL,
  281. NULL, NULL, NULL, 1, s->elf_machine,
  282. 0, 0);
  283. }
  284. if (bios_size < 0) {
  285. bios_size = get_image_size(filename);
  286. if (bios_size > 0 && bios_size <= BIOS_SIZE) {
  287. hwaddr bios_addr;
  288. bios_size = (bios_size + 0xfff) & ~0xfff;
  289. bios_addr = (uint32_t)(-BIOS_SIZE);
  290. bios_size = load_image_targphys(filename, bios_addr,
  291. bios_size);
  292. }
  293. }
  294. }
  295. g_free(filename);
  296. if (bios_size < 0 || bios_size > BIOS_SIZE) {
  297. memory_region_del_subregion(get_system_memory(), &s->bios);
  298. error_setg(errp, "Could not load bios image '%s'", s->bios_name);
  299. return;
  300. }
  301. }
  302. vmstate_register_ram_global(&s->bios);
  303. }
  304. static const VMStateDescription vmstate_raven = {
  305. .name = "raven",
  306. .version_id = 0,
  307. .minimum_version_id = 0,
  308. .fields = (VMStateField[]) {
  309. VMSTATE_PCI_DEVICE(dev, RavenPCIState),
  310. VMSTATE_END_OF_LIST()
  311. },
  312. };
  313. static void raven_class_init(ObjectClass *klass, void *data)
  314. {
  315. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  316. DeviceClass *dc = DEVICE_CLASS(klass);
  317. k->realize = raven_realize;
  318. k->vendor_id = PCI_VENDOR_ID_MOTOROLA;
  319. k->device_id = PCI_DEVICE_ID_MOTOROLA_RAVEN;
  320. k->revision = 0x00;
  321. k->class_id = PCI_CLASS_BRIDGE_HOST;
  322. dc->desc = "PReP Host Bridge - Motorola Raven";
  323. dc->vmsd = &vmstate_raven;
  324. /*
  325. * Reason: PCI-facing part of the host bridge, not usable without
  326. * the host-facing part, which can't be device_add'ed, yet.
  327. */
  328. dc->user_creatable = false;
  329. }
  330. static const TypeInfo raven_info = {
  331. .name = TYPE_RAVEN_PCI_DEVICE,
  332. .parent = TYPE_PCI_DEVICE,
  333. .instance_size = sizeof(RavenPCIState),
  334. .class_init = raven_class_init,
  335. .interfaces = (InterfaceInfo[]) {
  336. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  337. { },
  338. },
  339. };
  340. static Property raven_pcihost_properties[] = {
  341. DEFINE_PROP_UINT32("elf-machine", PREPPCIState, pci_dev.elf_machine,
  342. EM_NONE),
  343. DEFINE_PROP_STRING("bios-name", PREPPCIState, pci_dev.bios_name),
  344. /* Temporary workaround until legacy prep machine is removed */
  345. DEFINE_PROP_BOOL("is-legacy-prep", PREPPCIState, is_legacy_prep,
  346. false),
  347. DEFINE_PROP_END_OF_LIST()
  348. };
  349. static void raven_pcihost_class_init(ObjectClass *klass, void *data)
  350. {
  351. DeviceClass *dc = DEVICE_CLASS(klass);
  352. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  353. dc->realize = raven_pcihost_realizefn;
  354. device_class_set_props(dc, raven_pcihost_properties);
  355. dc->fw_name = "pci";
  356. }
  357. static const TypeInfo raven_pcihost_info = {
  358. .name = TYPE_RAVEN_PCI_HOST_BRIDGE,
  359. .parent = TYPE_PCI_HOST_BRIDGE,
  360. .instance_size = sizeof(PREPPCIState),
  361. .instance_init = raven_pcihost_initfn,
  362. .class_init = raven_pcihost_class_init,
  363. };
  364. static void raven_register_types(void)
  365. {
  366. type_register_static(&raven_pcihost_info);
  367. type_register_static(&raven_info);
  368. }
  369. type_init(raven_register_types)