xen_platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * XEN platform pci device, formerly known as the event channel device
  3. *
  4. * Copyright (c) 2003-2004 Intel Corp.
  5. * Copyright (c) 2006 XenSource
  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 <assert.h>
  26. #include "hw.h"
  27. #include "pc.h"
  28. #include "pci.h"
  29. #include "irq.h"
  30. #include "xen_common.h"
  31. #include "net.h"
  32. #include "xen_backend.h"
  33. #include "trace.h"
  34. #include "exec-memory.h"
  35. #include <xenguest.h>
  36. //#define DEBUG_PLATFORM
  37. #ifdef DEBUG_PLATFORM
  38. #define DPRINTF(fmt, ...) do { \
  39. fprintf(stderr, "xen_platform: " fmt, ## __VA_ARGS__); \
  40. } while (0)
  41. #else
  42. #define DPRINTF(fmt, ...) do { } while (0)
  43. #endif
  44. #define PFFLAG_ROM_LOCK 1 /* Sets whether ROM memory area is RW or RO */
  45. typedef struct PCIXenPlatformState {
  46. PCIDevice pci_dev;
  47. MemoryRegion fixed_io;
  48. MemoryRegion bar;
  49. MemoryRegion mmio_bar;
  50. uint8_t flags; /* used only for version_id == 2 */
  51. int drivers_blacklisted;
  52. uint16_t driver_product_version;
  53. /* Log from guest drivers */
  54. char log_buffer[4096];
  55. int log_buffer_off;
  56. } PCIXenPlatformState;
  57. #define XEN_PLATFORM_IOPORT 0x10
  58. /* Send bytes to syslog */
  59. static void log_writeb(PCIXenPlatformState *s, char val)
  60. {
  61. if (val == '\n' || s->log_buffer_off == sizeof(s->log_buffer) - 1) {
  62. /* Flush buffer */
  63. s->log_buffer[s->log_buffer_off] = 0;
  64. trace_xen_platform_log(s->log_buffer);
  65. s->log_buffer_off = 0;
  66. } else {
  67. s->log_buffer[s->log_buffer_off++] = val;
  68. }
  69. }
  70. /* Xen Platform, Fixed IOPort */
  71. #define UNPLUG_ALL_IDE_DISKS 1
  72. #define UNPLUG_ALL_NICS 2
  73. #define UNPLUG_AUX_IDE_DISKS 4
  74. static void unplug_nic(PCIBus *b, PCIDevice *d, void *o)
  75. {
  76. if (pci_get_word(d->config + PCI_CLASS_DEVICE) ==
  77. PCI_CLASS_NETWORK_ETHERNET) {
  78. qdev_free(&d->qdev);
  79. }
  80. }
  81. static void pci_unplug_nics(PCIBus *bus)
  82. {
  83. pci_for_each_device(bus, 0, unplug_nic, NULL);
  84. }
  85. static void unplug_disks(PCIBus *b, PCIDevice *d, void *o)
  86. {
  87. if (pci_get_word(d->config + PCI_CLASS_DEVICE) ==
  88. PCI_CLASS_STORAGE_IDE) {
  89. qdev_unplug(&(d->qdev), NULL);
  90. }
  91. }
  92. static void pci_unplug_disks(PCIBus *bus)
  93. {
  94. pci_for_each_device(bus, 0, unplug_disks, NULL);
  95. }
  96. static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
  97. {
  98. PCIXenPlatformState *s = opaque;
  99. switch (addr) {
  100. case 0:
  101. /* Unplug devices. Value is a bitmask of which devices to
  102. unplug, with bit 0 the IDE devices, bit 1 the network
  103. devices, and bit 2 the non-primary-master IDE devices. */
  104. if (val & UNPLUG_ALL_IDE_DISKS) {
  105. DPRINTF("unplug disks\n");
  106. bdrv_drain_all();
  107. bdrv_flush_all();
  108. pci_unplug_disks(s->pci_dev.bus);
  109. }
  110. if (val & UNPLUG_ALL_NICS) {
  111. DPRINTF("unplug nics\n");
  112. pci_unplug_nics(s->pci_dev.bus);
  113. }
  114. if (val & UNPLUG_AUX_IDE_DISKS) {
  115. DPRINTF("unplug auxiliary disks not supported\n");
  116. }
  117. break;
  118. case 2:
  119. switch (val) {
  120. case 1:
  121. DPRINTF("Citrix Windows PV drivers loaded in guest\n");
  122. break;
  123. case 0:
  124. DPRINTF("Guest claimed to be running PV product 0?\n");
  125. break;
  126. default:
  127. DPRINTF("Unknown PV product %d loaded in guest\n", val);
  128. break;
  129. }
  130. s->driver_product_version = val;
  131. break;
  132. }
  133. }
  134. static void platform_fixed_ioport_writel(void *opaque, uint32_t addr,
  135. uint32_t val)
  136. {
  137. switch (addr) {
  138. case 0:
  139. /* PV driver version */
  140. break;
  141. }
  142. }
  143. static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
  144. {
  145. PCIXenPlatformState *s = opaque;
  146. switch (addr) {
  147. case 0: /* Platform flags */ {
  148. hvmmem_type_t mem_type = (val & PFFLAG_ROM_LOCK) ?
  149. HVMMEM_ram_ro : HVMMEM_ram_rw;
  150. if (xc_hvm_set_mem_type(xen_xc, xen_domid, mem_type, 0xc0, 0x40)) {
  151. DPRINTF("unable to change ro/rw state of ROM memory area!\n");
  152. } else {
  153. s->flags = val & PFFLAG_ROM_LOCK;
  154. DPRINTF("changed ro/rw state of ROM memory area. now is %s state.\n",
  155. (mem_type == HVMMEM_ram_ro ? "ro":"rw"));
  156. }
  157. break;
  158. }
  159. case 2:
  160. log_writeb(s, val);
  161. break;
  162. }
  163. }
  164. static uint32_t platform_fixed_ioport_readw(void *opaque, uint32_t addr)
  165. {
  166. PCIXenPlatformState *s = opaque;
  167. switch (addr) {
  168. case 0:
  169. if (s->drivers_blacklisted) {
  170. /* The drivers will recognise this magic number and refuse
  171. * to do anything. */
  172. return 0xd249;
  173. } else {
  174. /* Magic value so that you can identify the interface. */
  175. return 0x49d2;
  176. }
  177. default:
  178. return 0xffff;
  179. }
  180. }
  181. static uint32_t platform_fixed_ioport_readb(void *opaque, uint32_t addr)
  182. {
  183. PCIXenPlatformState *s = opaque;
  184. switch (addr) {
  185. case 0:
  186. /* Platform flags */
  187. return s->flags;
  188. case 2:
  189. /* Version number */
  190. return 1;
  191. default:
  192. return 0xff;
  193. }
  194. }
  195. static void platform_fixed_ioport_reset(void *opaque)
  196. {
  197. PCIXenPlatformState *s = opaque;
  198. platform_fixed_ioport_writeb(s, 0, 0);
  199. }
  200. const MemoryRegionPortio xen_platform_ioport[] = {
  201. { 0, 16, 4, .write = platform_fixed_ioport_writel, },
  202. { 0, 16, 2, .write = platform_fixed_ioport_writew, },
  203. { 0, 16, 1, .write = platform_fixed_ioport_writeb, },
  204. { 0, 16, 2, .read = platform_fixed_ioport_readw, },
  205. { 0, 16, 1, .read = platform_fixed_ioport_readb, },
  206. PORTIO_END_OF_LIST()
  207. };
  208. static const MemoryRegionOps platform_fixed_io_ops = {
  209. .old_portio = xen_platform_ioport,
  210. .endianness = DEVICE_NATIVE_ENDIAN,
  211. };
  212. static void platform_fixed_ioport_init(PCIXenPlatformState* s)
  213. {
  214. memory_region_init_io(&s->fixed_io, &platform_fixed_io_ops, s,
  215. "xen-fixed", 16);
  216. memory_region_add_subregion(get_system_io(), XEN_PLATFORM_IOPORT,
  217. &s->fixed_io);
  218. }
  219. /* Xen Platform PCI Device */
  220. static uint32_t xen_platform_ioport_readb(void *opaque, uint32_t addr)
  221. {
  222. if (addr == 0) {
  223. return platform_fixed_ioport_readb(opaque, 0);
  224. } else {
  225. return ~0u;
  226. }
  227. }
  228. static void xen_platform_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
  229. {
  230. PCIXenPlatformState *s = opaque;
  231. switch (addr) {
  232. case 0: /* Platform flags */
  233. platform_fixed_ioport_writeb(opaque, 0, val);
  234. break;
  235. case 8:
  236. log_writeb(s, val);
  237. break;
  238. default:
  239. break;
  240. }
  241. }
  242. static MemoryRegionPortio xen_pci_portio[] = {
  243. { 0, 0x100, 1, .read = xen_platform_ioport_readb, },
  244. { 0, 0x100, 1, .write = xen_platform_ioport_writeb, },
  245. PORTIO_END_OF_LIST()
  246. };
  247. static const MemoryRegionOps xen_pci_io_ops = {
  248. .old_portio = xen_pci_portio,
  249. };
  250. static void platform_ioport_bar_setup(PCIXenPlatformState *d)
  251. {
  252. memory_region_init_io(&d->bar, &xen_pci_io_ops, d, "xen-pci", 0x100);
  253. }
  254. static uint64_t platform_mmio_read(void *opaque, target_phys_addr_t addr,
  255. unsigned size)
  256. {
  257. DPRINTF("Warning: attempted read from physical address "
  258. "0x" TARGET_FMT_plx " in xen platform mmio space\n", addr);
  259. return 0;
  260. }
  261. static void platform_mmio_write(void *opaque, target_phys_addr_t addr,
  262. uint64_t val, unsigned size)
  263. {
  264. DPRINTF("Warning: attempted write of 0x%"PRIx64" to physical "
  265. "address 0x" TARGET_FMT_plx " in xen platform mmio space\n",
  266. val, addr);
  267. }
  268. static const MemoryRegionOps platform_mmio_handler = {
  269. .read = &platform_mmio_read,
  270. .write = &platform_mmio_write,
  271. .endianness = DEVICE_NATIVE_ENDIAN,
  272. };
  273. static void platform_mmio_setup(PCIXenPlatformState *d)
  274. {
  275. memory_region_init_io(&d->mmio_bar, &platform_mmio_handler, d,
  276. "xen-mmio", 0x1000000);
  277. }
  278. static int xen_platform_post_load(void *opaque, int version_id)
  279. {
  280. PCIXenPlatformState *s = opaque;
  281. platform_fixed_ioport_writeb(s, 0, s->flags);
  282. return 0;
  283. }
  284. static const VMStateDescription vmstate_xen_platform = {
  285. .name = "platform",
  286. .version_id = 4,
  287. .minimum_version_id = 4,
  288. .minimum_version_id_old = 4,
  289. .post_load = xen_platform_post_load,
  290. .fields = (VMStateField []) {
  291. VMSTATE_PCI_DEVICE(pci_dev, PCIXenPlatformState),
  292. VMSTATE_UINT8(flags, PCIXenPlatformState),
  293. VMSTATE_END_OF_LIST()
  294. }
  295. };
  296. static int xen_platform_initfn(PCIDevice *dev)
  297. {
  298. PCIXenPlatformState *d = DO_UPCAST(PCIXenPlatformState, pci_dev, dev);
  299. uint8_t *pci_conf;
  300. pci_conf = d->pci_dev.config;
  301. pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
  302. pci_config_set_prog_interface(pci_conf, 0);
  303. pci_conf[PCI_INTERRUPT_PIN] = 1;
  304. platform_ioport_bar_setup(d);
  305. pci_register_bar(&d->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &d->bar);
  306. /* reserve 16MB mmio address for share memory*/
  307. platform_mmio_setup(d);
  308. pci_register_bar(&d->pci_dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
  309. &d->mmio_bar);
  310. platform_fixed_ioport_init(d);
  311. return 0;
  312. }
  313. static void platform_reset(DeviceState *dev)
  314. {
  315. PCIXenPlatformState *s = DO_UPCAST(PCIXenPlatformState, pci_dev.qdev, dev);
  316. platform_fixed_ioport_reset(s);
  317. }
  318. static void xen_platform_class_init(ObjectClass *klass, void *data)
  319. {
  320. DeviceClass *dc = DEVICE_CLASS(klass);
  321. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  322. k->init = xen_platform_initfn;
  323. k->vendor_id = PCI_VENDOR_ID_XEN;
  324. k->device_id = PCI_DEVICE_ID_XEN_PLATFORM;
  325. k->class_id = PCI_CLASS_OTHERS << 8 | 0x80;
  326. k->subsystem_vendor_id = PCI_VENDOR_ID_XEN;
  327. k->subsystem_id = PCI_DEVICE_ID_XEN_PLATFORM;
  328. k->revision = 1;
  329. dc->desc = "XEN platform pci device";
  330. dc->reset = platform_reset;
  331. dc->vmsd = &vmstate_xen_platform;
  332. }
  333. static TypeInfo xen_platform_info = {
  334. .name = "xen-platform",
  335. .parent = TYPE_PCI_DEVICE,
  336. .instance_size = sizeof(PCIXenPlatformState),
  337. .class_init = xen_platform_class_init,
  338. };
  339. static void xen_platform_register_types(void)
  340. {
  341. type_register_static(&xen_platform_info);
  342. }
  343. type_init(xen_platform_register_types)