prep.c 14 KB

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