2
0

xen_platform.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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/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. /* We have to ignore passthrough devices */
  77. if (pci_get_word(d->config + PCI_CLASS_DEVICE) ==
  78. PCI_CLASS_NETWORK_ETHERNET
  79. && strcmp(d->name, "xen-pci-passthrough") != 0) {
  80. qdev_free(&d->qdev);
  81. }
  82. }
  83. static void pci_unplug_nics(PCIBus *bus)
  84. {
  85. pci_for_each_device(bus, 0, unplug_nic, NULL);
  86. }
  87. static void unplug_disks(PCIBus *b, PCIDevice *d, void *o)
  88. {
  89. /* We have to ignore passthrough devices */
  90. if (pci_get_word(d->config + PCI_CLASS_DEVICE) ==
  91. PCI_CLASS_STORAGE_IDE
  92. && strcmp(d->name, "xen-pci-passthrough") != 0) {
  93. qdev_unplug(&(d->qdev), NULL);
  94. }
  95. }
  96. static void pci_unplug_disks(PCIBus *bus)
  97. {
  98. pci_for_each_device(bus, 0, unplug_disks, NULL);
  99. }
  100. static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
  101. {
  102. PCIXenPlatformState *s = opaque;
  103. switch (addr) {
  104. case 0:
  105. /* Unplug devices. Value is a bitmask of which devices to
  106. unplug, with bit 0 the IDE devices, bit 1 the network
  107. devices, and bit 2 the non-primary-master IDE devices. */
  108. if (val & UNPLUG_ALL_IDE_DISKS) {
  109. DPRINTF("unplug disks\n");
  110. bdrv_drain_all();
  111. bdrv_flush_all();
  112. pci_unplug_disks(s->pci_dev.bus);
  113. }
  114. if (val & UNPLUG_ALL_NICS) {
  115. DPRINTF("unplug nics\n");
  116. pci_unplug_nics(s->pci_dev.bus);
  117. }
  118. if (val & UNPLUG_AUX_IDE_DISKS) {
  119. DPRINTF("unplug auxiliary disks not supported\n");
  120. }
  121. break;
  122. case 2:
  123. switch (val) {
  124. case 1:
  125. DPRINTF("Citrix Windows PV drivers loaded in guest\n");
  126. break;
  127. case 0:
  128. DPRINTF("Guest claimed to be running PV product 0?\n");
  129. break;
  130. default:
  131. DPRINTF("Unknown PV product %d loaded in guest\n", val);
  132. break;
  133. }
  134. s->driver_product_version = val;
  135. break;
  136. }
  137. }
  138. static void platform_fixed_ioport_writel(void *opaque, uint32_t addr,
  139. uint32_t val)
  140. {
  141. switch (addr) {
  142. case 0:
  143. /* PV driver version */
  144. break;
  145. }
  146. }
  147. static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
  148. {
  149. PCIXenPlatformState *s = opaque;
  150. switch (addr) {
  151. case 0: /* Platform flags */ {
  152. hvmmem_type_t mem_type = (val & PFFLAG_ROM_LOCK) ?
  153. HVMMEM_ram_ro : HVMMEM_ram_rw;
  154. if (xc_hvm_set_mem_type(xen_xc, xen_domid, mem_type, 0xc0, 0x40)) {
  155. DPRINTF("unable to change ro/rw state of ROM memory area!\n");
  156. } else {
  157. s->flags = val & PFFLAG_ROM_LOCK;
  158. DPRINTF("changed ro/rw state of ROM memory area. now is %s state.\n",
  159. (mem_type == HVMMEM_ram_ro ? "ro":"rw"));
  160. }
  161. break;
  162. }
  163. case 2:
  164. log_writeb(s, val);
  165. break;
  166. }
  167. }
  168. static uint32_t platform_fixed_ioport_readw(void *opaque, uint32_t addr)
  169. {
  170. PCIXenPlatformState *s = opaque;
  171. switch (addr) {
  172. case 0:
  173. if (s->drivers_blacklisted) {
  174. /* The drivers will recognise this magic number and refuse
  175. * to do anything. */
  176. return 0xd249;
  177. } else {
  178. /* Magic value so that you can identify the interface. */
  179. return 0x49d2;
  180. }
  181. default:
  182. return 0xffff;
  183. }
  184. }
  185. static uint32_t platform_fixed_ioport_readb(void *opaque, uint32_t addr)
  186. {
  187. PCIXenPlatformState *s = opaque;
  188. switch (addr) {
  189. case 0:
  190. /* Platform flags */
  191. return s->flags;
  192. case 2:
  193. /* Version number */
  194. return 1;
  195. default:
  196. return 0xff;
  197. }
  198. }
  199. static void platform_fixed_ioport_reset(void *opaque)
  200. {
  201. PCIXenPlatformState *s = opaque;
  202. platform_fixed_ioport_writeb(s, 0, 0);
  203. }
  204. static uint64_t platform_fixed_ioport_read(void *opaque,
  205. hwaddr addr,
  206. unsigned size)
  207. {
  208. switch (size) {
  209. case 1:
  210. return platform_fixed_ioport_readb(opaque, addr);
  211. case 2:
  212. return platform_fixed_ioport_readw(opaque, addr);
  213. default:
  214. return -1;
  215. }
  216. }
  217. static void platform_fixed_ioport_write(void *opaque, hwaddr addr,
  218. uint64_t val, unsigned size)
  219. {
  220. switch (size) {
  221. case 1:
  222. platform_fixed_ioport_writeb(opaque, addr, val);
  223. break;
  224. case 2:
  225. platform_fixed_ioport_writew(opaque, addr, val);
  226. break;
  227. case 4:
  228. platform_fixed_ioport_writel(opaque, addr, val);
  229. break;
  230. }
  231. }
  232. static const MemoryRegionOps platform_fixed_io_ops = {
  233. .read = platform_fixed_ioport_read,
  234. .write = platform_fixed_ioport_write,
  235. .impl = {
  236. .min_access_size = 1,
  237. .max_access_size = 4,
  238. },
  239. .endianness = DEVICE_LITTLE_ENDIAN,
  240. };
  241. static void platform_fixed_ioport_init(PCIXenPlatformState* s)
  242. {
  243. memory_region_init_io(&s->fixed_io, &platform_fixed_io_ops, s,
  244. "xen-fixed", 16);
  245. memory_region_add_subregion(get_system_io(), XEN_PLATFORM_IOPORT,
  246. &s->fixed_io);
  247. }
  248. /* Xen Platform PCI Device */
  249. static uint32_t xen_platform_ioport_readb(void *opaque, uint32_t addr)
  250. {
  251. if (addr == 0) {
  252. return platform_fixed_ioport_readb(opaque, 0);
  253. } else {
  254. return ~0u;
  255. }
  256. }
  257. static void xen_platform_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
  258. {
  259. PCIXenPlatformState *s = opaque;
  260. switch (addr) {
  261. case 0: /* Platform flags */
  262. platform_fixed_ioport_writeb(opaque, 0, val);
  263. break;
  264. case 8:
  265. log_writeb(s, val);
  266. break;
  267. default:
  268. break;
  269. }
  270. }
  271. static MemoryRegionPortio xen_pci_portio[] = {
  272. { 0, 0x100, 1, .read = xen_platform_ioport_readb, },
  273. { 0, 0x100, 1, .write = xen_platform_ioport_writeb, },
  274. PORTIO_END_OF_LIST()
  275. };
  276. static const MemoryRegionOps xen_pci_io_ops = {
  277. .old_portio = xen_pci_portio,
  278. };
  279. static void platform_ioport_bar_setup(PCIXenPlatformState *d)
  280. {
  281. memory_region_init_io(&d->bar, &xen_pci_io_ops, d, "xen-pci", 0x100);
  282. }
  283. static uint64_t platform_mmio_read(void *opaque, hwaddr addr,
  284. unsigned size)
  285. {
  286. DPRINTF("Warning: attempted read from physical address "
  287. "0x" TARGET_FMT_plx " in xen platform mmio space\n", addr);
  288. return 0;
  289. }
  290. static void platform_mmio_write(void *opaque, hwaddr addr,
  291. uint64_t val, unsigned size)
  292. {
  293. DPRINTF("Warning: attempted write of 0x%"PRIx64" to physical "
  294. "address 0x" TARGET_FMT_plx " in xen platform mmio space\n",
  295. val, addr);
  296. }
  297. static const MemoryRegionOps platform_mmio_handler = {
  298. .read = &platform_mmio_read,
  299. .write = &platform_mmio_write,
  300. .endianness = DEVICE_NATIVE_ENDIAN,
  301. };
  302. static void platform_mmio_setup(PCIXenPlatformState *d)
  303. {
  304. memory_region_init_io(&d->mmio_bar, &platform_mmio_handler, d,
  305. "xen-mmio", 0x1000000);
  306. }
  307. static int xen_platform_post_load(void *opaque, int version_id)
  308. {
  309. PCIXenPlatformState *s = opaque;
  310. platform_fixed_ioport_writeb(s, 0, s->flags);
  311. return 0;
  312. }
  313. static const VMStateDescription vmstate_xen_platform = {
  314. .name = "platform",
  315. .version_id = 4,
  316. .minimum_version_id = 4,
  317. .minimum_version_id_old = 4,
  318. .post_load = xen_platform_post_load,
  319. .fields = (VMStateField []) {
  320. VMSTATE_PCI_DEVICE(pci_dev, PCIXenPlatformState),
  321. VMSTATE_UINT8(flags, PCIXenPlatformState),
  322. VMSTATE_END_OF_LIST()
  323. }
  324. };
  325. static int xen_platform_initfn(PCIDevice *dev)
  326. {
  327. PCIXenPlatformState *d = DO_UPCAST(PCIXenPlatformState, pci_dev, dev);
  328. uint8_t *pci_conf;
  329. pci_conf = d->pci_dev.config;
  330. pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
  331. pci_config_set_prog_interface(pci_conf, 0);
  332. pci_conf[PCI_INTERRUPT_PIN] = 1;
  333. platform_ioport_bar_setup(d);
  334. pci_register_bar(&d->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &d->bar);
  335. /* reserve 16MB mmio address for share memory*/
  336. platform_mmio_setup(d);
  337. pci_register_bar(&d->pci_dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
  338. &d->mmio_bar);
  339. platform_fixed_ioport_init(d);
  340. return 0;
  341. }
  342. static void platform_reset(DeviceState *dev)
  343. {
  344. PCIXenPlatformState *s = DO_UPCAST(PCIXenPlatformState, pci_dev.qdev, dev);
  345. platform_fixed_ioport_reset(s);
  346. }
  347. static void xen_platform_class_init(ObjectClass *klass, void *data)
  348. {
  349. DeviceClass *dc = DEVICE_CLASS(klass);
  350. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  351. k->init = xen_platform_initfn;
  352. k->vendor_id = PCI_VENDOR_ID_XEN;
  353. k->device_id = PCI_DEVICE_ID_XEN_PLATFORM;
  354. k->class_id = PCI_CLASS_OTHERS << 8 | 0x80;
  355. k->subsystem_vendor_id = PCI_VENDOR_ID_XEN;
  356. k->subsystem_id = PCI_DEVICE_ID_XEN_PLATFORM;
  357. k->revision = 1;
  358. dc->desc = "XEN platform pci device";
  359. dc->reset = platform_reset;
  360. dc->vmsd = &vmstate_xen_platform;
  361. }
  362. static TypeInfo xen_platform_info = {
  363. .name = "xen-platform",
  364. .parent = TYPE_PCI_DEVICE,
  365. .instance_size = sizeof(PCIXenPlatformState),
  366. .class_init = xen_platform_class_init,
  367. };
  368. static void xen_platform_register_types(void)
  369. {
  370. type_register_static(&xen_platform_info);
  371. }
  372. type_init(xen_platform_register_types)