prep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 "hw/hw.h"
  26. #include "hw/pci/pci.h"
  27. #include "hw/pci/pci_bus.h"
  28. #include "hw/pci/pci_host.h"
  29. #include "hw/i386/pc.h"
  30. #include "hw/loader.h"
  31. #include "exec/address-spaces.h"
  32. #include "elf.h"
  33. #define TYPE_RAVEN_PCI_DEVICE "raven"
  34. #define TYPE_RAVEN_PCI_HOST_BRIDGE "raven-pcihost"
  35. #define RAVEN_PCI_DEVICE(obj) \
  36. OBJECT_CHECK(RavenPCIState, (obj), TYPE_RAVEN_PCI_DEVICE)
  37. typedef struct RavenPCIState {
  38. PCIDevice dev;
  39. uint32_t elf_machine;
  40. char *bios_name;
  41. MemoryRegion bios;
  42. } RavenPCIState;
  43. #define RAVEN_PCI_HOST_BRIDGE(obj) \
  44. OBJECT_CHECK(PREPPCIState, (obj), TYPE_RAVEN_PCI_HOST_BRIDGE)
  45. typedef struct PRePPCIState {
  46. PCIHostState parent_obj;
  47. qemu_irq irq[PCI_NUM_PINS];
  48. PCIBus pci_bus;
  49. AddressSpace pci_io_as;
  50. MemoryRegion pci_io;
  51. MemoryRegion pci_io_non_contiguous;
  52. MemoryRegion pci_memory;
  53. MemoryRegion pci_intack;
  54. MemoryRegion bm;
  55. MemoryRegion bm_ram_alias;
  56. MemoryRegion bm_pci_memory_alias;
  57. AddressSpace bm_as;
  58. RavenPCIState pci_dev;
  59. int contiguous_map;
  60. } PREPPCIState;
  61. #define BIOS_SIZE (1024 * 1024)
  62. static inline uint32_t raven_pci_io_config(hwaddr addr)
  63. {
  64. int i;
  65. for (i = 0; i < 11; i++) {
  66. if ((addr & (1 << (11 + i))) != 0) {
  67. break;
  68. }
  69. }
  70. return (addr & 0x7ff) | (i << 11);
  71. }
  72. static void raven_pci_io_write(void *opaque, hwaddr addr,
  73. uint64_t val, unsigned int size)
  74. {
  75. PREPPCIState *s = opaque;
  76. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  77. pci_data_write(phb->bus, raven_pci_io_config(addr), val, size);
  78. }
  79. static uint64_t raven_pci_io_read(void *opaque, hwaddr addr,
  80. unsigned int size)
  81. {
  82. PREPPCIState *s = opaque;
  83. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  84. return pci_data_read(phb->bus, raven_pci_io_config(addr), size);
  85. }
  86. static const MemoryRegionOps raven_pci_io_ops = {
  87. .read = raven_pci_io_read,
  88. .write = raven_pci_io_write,
  89. .endianness = DEVICE_LITTLE_ENDIAN,
  90. };
  91. static uint64_t raven_intack_read(void *opaque, hwaddr addr,
  92. unsigned int size)
  93. {
  94. return pic_read_irq(isa_pic);
  95. }
  96. static const MemoryRegionOps raven_intack_ops = {
  97. .read = raven_intack_read,
  98. .valid = {
  99. .max_access_size = 1,
  100. },
  101. };
  102. static inline hwaddr raven_io_address(PREPPCIState *s,
  103. hwaddr addr)
  104. {
  105. if (s->contiguous_map == 0) {
  106. /* 64 KB contiguous space for IOs */
  107. addr &= 0xFFFF;
  108. } else {
  109. /* 8 MB non-contiguous space for IOs */
  110. addr = (addr & 0x1F) | ((addr & 0x007FFF000) >> 7);
  111. }
  112. /* FIXME: handle endianness switch */
  113. return addr;
  114. }
  115. static uint64_t raven_io_read(void *opaque, hwaddr addr,
  116. unsigned int size)
  117. {
  118. PREPPCIState *s = opaque;
  119. uint8_t buf[4];
  120. addr = raven_io_address(s, addr);
  121. address_space_read(&s->pci_io_as, addr + 0x80000000, buf, size);
  122. if (size == 1) {
  123. return buf[0];
  124. } else if (size == 2) {
  125. return lduw_le_p(buf);
  126. } else if (size == 4) {
  127. return ldl_le_p(buf);
  128. } else {
  129. g_assert_not_reached();
  130. }
  131. }
  132. static void raven_io_write(void *opaque, hwaddr addr,
  133. uint64_t val, unsigned int size)
  134. {
  135. PREPPCIState *s = opaque;
  136. uint8_t buf[4];
  137. addr = raven_io_address(s, addr);
  138. if (size == 1) {
  139. buf[0] = val;
  140. } else if (size == 2) {
  141. stw_le_p(buf, val);
  142. } else if (size == 4) {
  143. stl_le_p(buf, val);
  144. } else {
  145. g_assert_not_reached();
  146. }
  147. address_space_write(&s->pci_io_as, addr + 0x80000000, buf, size);
  148. }
  149. static const MemoryRegionOps raven_io_ops = {
  150. .read = raven_io_read,
  151. .write = raven_io_write,
  152. .endianness = DEVICE_LITTLE_ENDIAN,
  153. .impl.max_access_size = 4,
  154. .valid.unaligned = true,
  155. };
  156. static int raven_map_irq(PCIDevice *pci_dev, int irq_num)
  157. {
  158. return (irq_num + (pci_dev->devfn >> 3)) & 1;
  159. }
  160. static void raven_set_irq(void *opaque, int irq_num, int level)
  161. {
  162. qemu_irq *pic = opaque;
  163. qemu_set_irq(pic[irq_num] , level);
  164. }
  165. static AddressSpace *raven_pcihost_set_iommu(PCIBus *bus, void *opaque,
  166. int devfn)
  167. {
  168. PREPPCIState *s = opaque;
  169. return &s->bm_as;
  170. }
  171. static void raven_change_gpio(void *opaque, int n, int level)
  172. {
  173. PREPPCIState *s = opaque;
  174. s->contiguous_map = level;
  175. }
  176. static void raven_pcihost_realizefn(DeviceState *d, Error **errp)
  177. {
  178. SysBusDevice *dev = SYS_BUS_DEVICE(d);
  179. PCIHostState *h = PCI_HOST_BRIDGE(dev);
  180. PREPPCIState *s = RAVEN_PCI_HOST_BRIDGE(dev);
  181. MemoryRegion *address_space_mem = get_system_memory();
  182. int i;
  183. for (i = 0; i < PCI_NUM_PINS; i++) {
  184. sysbus_init_irq(dev, &s->irq[i]);
  185. }
  186. qdev_init_gpio_in(d, raven_change_gpio, 1);
  187. pci_bus_irqs(&s->pci_bus, raven_set_irq, raven_map_irq, s->irq,
  188. PCI_NUM_PINS);
  189. memory_region_init_io(&h->conf_mem, OBJECT(h), &pci_host_conf_le_ops, s,
  190. "pci-conf-idx", 4);
  191. memory_region_add_subregion(&s->pci_io, 0xcf8, &h->conf_mem);
  192. memory_region_init_io(&h->data_mem, OBJECT(h), &pci_host_data_le_ops, s,
  193. "pci-conf-data", 4);
  194. memory_region_add_subregion(&s->pci_io, 0xcfc, &h->data_mem);
  195. memory_region_init_io(&h->mmcfg, OBJECT(s), &raven_pci_io_ops, s,
  196. "pciio", 0x00400000);
  197. memory_region_add_subregion(address_space_mem, 0x80800000, &h->mmcfg);
  198. memory_region_init_io(&s->pci_intack, OBJECT(s), &raven_intack_ops, s,
  199. "pci-intack", 1);
  200. memory_region_add_subregion(address_space_mem, 0xbffffff0, &s->pci_intack);
  201. /* TODO Remove once realize propagates to child devices. */
  202. object_property_set_bool(OBJECT(&s->pci_dev), true, "realized", errp);
  203. }
  204. static void raven_pcihost_initfn(Object *obj)
  205. {
  206. PCIHostState *h = PCI_HOST_BRIDGE(obj);
  207. PREPPCIState *s = RAVEN_PCI_HOST_BRIDGE(obj);
  208. MemoryRegion *address_space_mem = get_system_memory();
  209. DeviceState *pci_dev;
  210. memory_region_init(&s->pci_io, obj, "pci-io", 0x3f800000);
  211. memory_region_init_io(&s->pci_io_non_contiguous, obj, &raven_io_ops, s,
  212. "pci-io-non-contiguous", 0x00800000);
  213. /* Open Hack'Ware hack: real size should be only 0x3f000000 bytes */
  214. memory_region_init(&s->pci_memory, obj, "pci-memory",
  215. 0x3f000000 + 0xc0000000ULL);
  216. address_space_init(&s->pci_io_as, &s->pci_io, "raven-io");
  217. /* CPU address space */
  218. memory_region_add_subregion(address_space_mem, 0x80000000, &s->pci_io);
  219. memory_region_add_subregion_overlap(address_space_mem, 0x80000000,
  220. &s->pci_io_non_contiguous, 1);
  221. memory_region_add_subregion(address_space_mem, 0xc0000000, &s->pci_memory);
  222. pci_bus_new_inplace(&s->pci_bus, sizeof(s->pci_bus), DEVICE(obj), NULL,
  223. &s->pci_memory, &s->pci_io, 0, TYPE_PCI_BUS);
  224. /* Bus master address space */
  225. memory_region_init(&s->bm, obj, "bm-raven", UINT32_MAX);
  226. memory_region_init_alias(&s->bm_pci_memory_alias, obj, "bm-pci-memory",
  227. &s->pci_memory, 0,
  228. memory_region_size(&s->pci_memory));
  229. memory_region_init_alias(&s->bm_ram_alias, obj, "bm-system",
  230. get_system_memory(), 0, 0x80000000);
  231. memory_region_add_subregion(&s->bm, 0 , &s->bm_pci_memory_alias);
  232. memory_region_add_subregion(&s->bm, 0x80000000, &s->bm_ram_alias);
  233. address_space_init(&s->bm_as, &s->bm, "raven-bm");
  234. pci_setup_iommu(&s->pci_bus, raven_pcihost_set_iommu, s);
  235. h->bus = &s->pci_bus;
  236. object_initialize(&s->pci_dev, sizeof(s->pci_dev), TYPE_RAVEN_PCI_DEVICE);
  237. pci_dev = DEVICE(&s->pci_dev);
  238. qdev_set_parent_bus(pci_dev, BUS(&s->pci_bus));
  239. object_property_set_int(OBJECT(&s->pci_dev), PCI_DEVFN(0, 0), "addr",
  240. NULL);
  241. qdev_prop_set_bit(pci_dev, "multifunction", false);
  242. }
  243. static int raven_init(PCIDevice *d)
  244. {
  245. RavenPCIState *s = RAVEN_PCI_DEVICE(d);
  246. char *filename;
  247. int bios_size = -1;
  248. d->config[0x0C] = 0x08; // cache_line_size
  249. d->config[0x0D] = 0x10; // latency_timer
  250. d->config[0x34] = 0x00; // capabilities_pointer
  251. memory_region_init_ram(&s->bios, OBJECT(s), "bios", BIOS_SIZE);
  252. memory_region_set_readonly(&s->bios, true);
  253. memory_region_add_subregion(get_system_memory(), (uint32_t)(-BIOS_SIZE),
  254. &s->bios);
  255. vmstate_register_ram_global(&s->bios);
  256. if (s->bios_name) {
  257. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, s->bios_name);
  258. if (filename) {
  259. if (s->elf_machine != EM_NONE) {
  260. bios_size = load_elf(filename, NULL, NULL, NULL,
  261. NULL, NULL, 1, s->elf_machine, 0);
  262. }
  263. if (bios_size < 0) {
  264. bios_size = get_image_size(filename);
  265. if (bios_size > 0 && bios_size <= BIOS_SIZE) {
  266. hwaddr bios_addr;
  267. bios_size = (bios_size + 0xfff) & ~0xfff;
  268. bios_addr = (uint32_t)(-BIOS_SIZE);
  269. bios_size = load_image_targphys(filename, bios_addr,
  270. bios_size);
  271. }
  272. }
  273. }
  274. if (bios_size < 0 || bios_size > BIOS_SIZE) {
  275. hw_error("qemu: could not load bios image '%s'\n", s->bios_name);
  276. }
  277. if (filename) {
  278. g_free(filename);
  279. }
  280. }
  281. return 0;
  282. }
  283. static const VMStateDescription vmstate_raven = {
  284. .name = "raven",
  285. .version_id = 0,
  286. .minimum_version_id = 0,
  287. .fields = (VMStateField[]) {
  288. VMSTATE_PCI_DEVICE(dev, RavenPCIState),
  289. VMSTATE_END_OF_LIST()
  290. },
  291. };
  292. static void raven_class_init(ObjectClass *klass, void *data)
  293. {
  294. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  295. DeviceClass *dc = DEVICE_CLASS(klass);
  296. k->init = raven_init;
  297. k->vendor_id = PCI_VENDOR_ID_MOTOROLA;
  298. k->device_id = PCI_DEVICE_ID_MOTOROLA_RAVEN;
  299. k->revision = 0x00;
  300. k->class_id = PCI_CLASS_BRIDGE_HOST;
  301. dc->desc = "PReP Host Bridge - Motorola Raven";
  302. dc->vmsd = &vmstate_raven;
  303. /*
  304. * PCI-facing part of the host bridge, not usable without the
  305. * host-facing part, which can't be device_add'ed, yet.
  306. */
  307. dc->cannot_instantiate_with_device_add_yet = true;
  308. }
  309. static const TypeInfo raven_info = {
  310. .name = TYPE_RAVEN_PCI_DEVICE,
  311. .parent = TYPE_PCI_DEVICE,
  312. .instance_size = sizeof(RavenPCIState),
  313. .class_init = raven_class_init,
  314. };
  315. static Property raven_pcihost_properties[] = {
  316. DEFINE_PROP_UINT32("elf-machine", PREPPCIState, pci_dev.elf_machine,
  317. EM_NONE),
  318. DEFINE_PROP_STRING("bios-name", PREPPCIState, pci_dev.bios_name),
  319. DEFINE_PROP_END_OF_LIST()
  320. };
  321. static void raven_pcihost_class_init(ObjectClass *klass, void *data)
  322. {
  323. DeviceClass *dc = DEVICE_CLASS(klass);
  324. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  325. dc->realize = raven_pcihost_realizefn;
  326. dc->props = raven_pcihost_properties;
  327. dc->fw_name = "pci";
  328. }
  329. static const TypeInfo raven_pcihost_info = {
  330. .name = TYPE_RAVEN_PCI_HOST_BRIDGE,
  331. .parent = TYPE_PCI_HOST_BRIDGE,
  332. .instance_size = sizeof(PREPPCIState),
  333. .instance_init = raven_pcihost_initfn,
  334. .class_init = raven_pcihost_class_init,
  335. };
  336. static void raven_register_types(void)
  337. {
  338. type_register_static(&raven_pcihost_info);
  339. type_register_static(&raven_info);
  340. }
  341. type_init(raven_register_types)