2
0

xen_platform.c 12 KB

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