vmware_vga.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  1. /*
  2. * QEMU VMware-SVGA "chipset".
  3. *
  4. * Copyright (c) 2007 Andrzej Zaborowski <balrog@zabor.org>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/module.h"
  26. #include "qemu/units.h"
  27. #include "qapi/error.h"
  28. #include "hw/loader.h"
  29. #include "trace.h"
  30. #include "ui/vnc.h"
  31. #include "hw/pci/pci.h"
  32. #include "hw/qdev-properties.h"
  33. #include "migration/vmstate.h"
  34. #undef VERBOSE
  35. #define HW_RECT_ACCEL
  36. #define HW_FILL_ACCEL
  37. #define HW_MOUSE_ACCEL
  38. #include "vga_int.h"
  39. /* See http://vmware-svga.sf.net/ for some documentation on VMWare SVGA */
  40. struct vmsvga_state_s {
  41. VGACommonState vga;
  42. int invalidated;
  43. int enable;
  44. int config;
  45. struct {
  46. int id;
  47. int x;
  48. int y;
  49. int on;
  50. } cursor;
  51. int index;
  52. int scratch_size;
  53. uint32_t *scratch;
  54. int new_width;
  55. int new_height;
  56. int new_depth;
  57. uint32_t guest;
  58. uint32_t svgaid;
  59. int syncing;
  60. MemoryRegion fifo_ram;
  61. uint8_t *fifo_ptr;
  62. unsigned int fifo_size;
  63. uint32_t *fifo;
  64. uint32_t fifo_min;
  65. uint32_t fifo_max;
  66. uint32_t fifo_next;
  67. uint32_t fifo_stop;
  68. #define REDRAW_FIFO_LEN 512
  69. struct vmsvga_rect_s {
  70. int x, y, w, h;
  71. } redraw_fifo[REDRAW_FIFO_LEN];
  72. int redraw_fifo_first, redraw_fifo_last;
  73. };
  74. #define TYPE_VMWARE_SVGA "vmware-svga"
  75. #define VMWARE_SVGA(obj) \
  76. OBJECT_CHECK(struct pci_vmsvga_state_s, (obj), TYPE_VMWARE_SVGA)
  77. struct pci_vmsvga_state_s {
  78. /*< private >*/
  79. PCIDevice parent_obj;
  80. /*< public >*/
  81. struct vmsvga_state_s chip;
  82. MemoryRegion io_bar;
  83. };
  84. #define SVGA_MAGIC 0x900000UL
  85. #define SVGA_MAKE_ID(ver) (SVGA_MAGIC << 8 | (ver))
  86. #define SVGA_ID_0 SVGA_MAKE_ID(0)
  87. #define SVGA_ID_1 SVGA_MAKE_ID(1)
  88. #define SVGA_ID_2 SVGA_MAKE_ID(2)
  89. #define SVGA_LEGACY_BASE_PORT 0x4560
  90. #define SVGA_INDEX_PORT 0x0
  91. #define SVGA_VALUE_PORT 0x1
  92. #define SVGA_BIOS_PORT 0x2
  93. #define SVGA_VERSION_2
  94. #ifdef SVGA_VERSION_2
  95. # define SVGA_ID SVGA_ID_2
  96. # define SVGA_IO_BASE SVGA_LEGACY_BASE_PORT
  97. # define SVGA_IO_MUL 1
  98. # define SVGA_FIFO_SIZE 0x10000
  99. # define SVGA_PCI_DEVICE_ID PCI_DEVICE_ID_VMWARE_SVGA2
  100. #else
  101. # define SVGA_ID SVGA_ID_1
  102. # define SVGA_IO_BASE SVGA_LEGACY_BASE_PORT
  103. # define SVGA_IO_MUL 4
  104. # define SVGA_FIFO_SIZE 0x10000
  105. # define SVGA_PCI_DEVICE_ID PCI_DEVICE_ID_VMWARE_SVGA
  106. #endif
  107. enum {
  108. /* ID 0, 1 and 2 registers */
  109. SVGA_REG_ID = 0,
  110. SVGA_REG_ENABLE = 1,
  111. SVGA_REG_WIDTH = 2,
  112. SVGA_REG_HEIGHT = 3,
  113. SVGA_REG_MAX_WIDTH = 4,
  114. SVGA_REG_MAX_HEIGHT = 5,
  115. SVGA_REG_DEPTH = 6,
  116. SVGA_REG_BITS_PER_PIXEL = 7, /* Current bpp in the guest */
  117. SVGA_REG_PSEUDOCOLOR = 8,
  118. SVGA_REG_RED_MASK = 9,
  119. SVGA_REG_GREEN_MASK = 10,
  120. SVGA_REG_BLUE_MASK = 11,
  121. SVGA_REG_BYTES_PER_LINE = 12,
  122. SVGA_REG_FB_START = 13,
  123. SVGA_REG_FB_OFFSET = 14,
  124. SVGA_REG_VRAM_SIZE = 15,
  125. SVGA_REG_FB_SIZE = 16,
  126. /* ID 1 and 2 registers */
  127. SVGA_REG_CAPABILITIES = 17,
  128. SVGA_REG_MEM_START = 18, /* Memory for command FIFO */
  129. SVGA_REG_MEM_SIZE = 19,
  130. SVGA_REG_CONFIG_DONE = 20, /* Set when memory area configured */
  131. SVGA_REG_SYNC = 21, /* Write to force synchronization */
  132. SVGA_REG_BUSY = 22, /* Read to check if sync is done */
  133. SVGA_REG_GUEST_ID = 23, /* Set guest OS identifier */
  134. SVGA_REG_CURSOR_ID = 24, /* ID of cursor */
  135. SVGA_REG_CURSOR_X = 25, /* Set cursor X position */
  136. SVGA_REG_CURSOR_Y = 26, /* Set cursor Y position */
  137. SVGA_REG_CURSOR_ON = 27, /* Turn cursor on/off */
  138. SVGA_REG_HOST_BITS_PER_PIXEL = 28, /* Current bpp in the host */
  139. SVGA_REG_SCRATCH_SIZE = 29, /* Number of scratch registers */
  140. SVGA_REG_MEM_REGS = 30, /* Number of FIFO registers */
  141. SVGA_REG_NUM_DISPLAYS = 31, /* Number of guest displays */
  142. SVGA_REG_PITCHLOCK = 32, /* Fixed pitch for all modes */
  143. SVGA_PALETTE_BASE = 1024, /* Base of SVGA color map */
  144. SVGA_PALETTE_END = SVGA_PALETTE_BASE + 767,
  145. SVGA_SCRATCH_BASE = SVGA_PALETTE_BASE + 768,
  146. };
  147. #define SVGA_CAP_NONE 0
  148. #define SVGA_CAP_RECT_FILL (1 << 0)
  149. #define SVGA_CAP_RECT_COPY (1 << 1)
  150. #define SVGA_CAP_RECT_PAT_FILL (1 << 2)
  151. #define SVGA_CAP_LEGACY_OFFSCREEN (1 << 3)
  152. #define SVGA_CAP_RASTER_OP (1 << 4)
  153. #define SVGA_CAP_CURSOR (1 << 5)
  154. #define SVGA_CAP_CURSOR_BYPASS (1 << 6)
  155. #define SVGA_CAP_CURSOR_BYPASS_2 (1 << 7)
  156. #define SVGA_CAP_8BIT_EMULATION (1 << 8)
  157. #define SVGA_CAP_ALPHA_CURSOR (1 << 9)
  158. #define SVGA_CAP_GLYPH (1 << 10)
  159. #define SVGA_CAP_GLYPH_CLIPPING (1 << 11)
  160. #define SVGA_CAP_OFFSCREEN_1 (1 << 12)
  161. #define SVGA_CAP_ALPHA_BLEND (1 << 13)
  162. #define SVGA_CAP_3D (1 << 14)
  163. #define SVGA_CAP_EXTENDED_FIFO (1 << 15)
  164. #define SVGA_CAP_MULTIMON (1 << 16)
  165. #define SVGA_CAP_PITCHLOCK (1 << 17)
  166. /*
  167. * FIFO offsets (seen as an array of 32-bit words)
  168. */
  169. enum {
  170. /*
  171. * The original defined FIFO offsets
  172. */
  173. SVGA_FIFO_MIN = 0,
  174. SVGA_FIFO_MAX, /* The distance from MIN to MAX must be at least 10K */
  175. SVGA_FIFO_NEXT,
  176. SVGA_FIFO_STOP,
  177. /*
  178. * Additional offsets added as of SVGA_CAP_EXTENDED_FIFO
  179. */
  180. SVGA_FIFO_CAPABILITIES = 4,
  181. SVGA_FIFO_FLAGS,
  182. SVGA_FIFO_FENCE,
  183. SVGA_FIFO_3D_HWVERSION,
  184. SVGA_FIFO_PITCHLOCK,
  185. };
  186. #define SVGA_FIFO_CAP_NONE 0
  187. #define SVGA_FIFO_CAP_FENCE (1 << 0)
  188. #define SVGA_FIFO_CAP_ACCELFRONT (1 << 1)
  189. #define SVGA_FIFO_CAP_PITCHLOCK (1 << 2)
  190. #define SVGA_FIFO_FLAG_NONE 0
  191. #define SVGA_FIFO_FLAG_ACCELFRONT (1 << 0)
  192. /* These values can probably be changed arbitrarily. */
  193. #define SVGA_SCRATCH_SIZE 0x8000
  194. #define SVGA_MAX_WIDTH ROUND_UP(2360, VNC_DIRTY_PIXELS_PER_BIT)
  195. #define SVGA_MAX_HEIGHT 1770
  196. #ifdef VERBOSE
  197. # define GUEST_OS_BASE 0x5001
  198. static const char *vmsvga_guest_id[] = {
  199. [0x00] = "Dos",
  200. [0x01] = "Windows 3.1",
  201. [0x02] = "Windows 95",
  202. [0x03] = "Windows 98",
  203. [0x04] = "Windows ME",
  204. [0x05] = "Windows NT",
  205. [0x06] = "Windows 2000",
  206. [0x07] = "Linux",
  207. [0x08] = "OS/2",
  208. [0x09] = "an unknown OS",
  209. [0x0a] = "BSD",
  210. [0x0b] = "Whistler",
  211. [0x0c] = "an unknown OS",
  212. [0x0d] = "an unknown OS",
  213. [0x0e] = "an unknown OS",
  214. [0x0f] = "an unknown OS",
  215. [0x10] = "an unknown OS",
  216. [0x11] = "an unknown OS",
  217. [0x12] = "an unknown OS",
  218. [0x13] = "an unknown OS",
  219. [0x14] = "an unknown OS",
  220. [0x15] = "Windows 2003",
  221. };
  222. #endif
  223. enum {
  224. SVGA_CMD_INVALID_CMD = 0,
  225. SVGA_CMD_UPDATE = 1,
  226. SVGA_CMD_RECT_FILL = 2,
  227. SVGA_CMD_RECT_COPY = 3,
  228. SVGA_CMD_DEFINE_BITMAP = 4,
  229. SVGA_CMD_DEFINE_BITMAP_SCANLINE = 5,
  230. SVGA_CMD_DEFINE_PIXMAP = 6,
  231. SVGA_CMD_DEFINE_PIXMAP_SCANLINE = 7,
  232. SVGA_CMD_RECT_BITMAP_FILL = 8,
  233. SVGA_CMD_RECT_PIXMAP_FILL = 9,
  234. SVGA_CMD_RECT_BITMAP_COPY = 10,
  235. SVGA_CMD_RECT_PIXMAP_COPY = 11,
  236. SVGA_CMD_FREE_OBJECT = 12,
  237. SVGA_CMD_RECT_ROP_FILL = 13,
  238. SVGA_CMD_RECT_ROP_COPY = 14,
  239. SVGA_CMD_RECT_ROP_BITMAP_FILL = 15,
  240. SVGA_CMD_RECT_ROP_PIXMAP_FILL = 16,
  241. SVGA_CMD_RECT_ROP_BITMAP_COPY = 17,
  242. SVGA_CMD_RECT_ROP_PIXMAP_COPY = 18,
  243. SVGA_CMD_DEFINE_CURSOR = 19,
  244. SVGA_CMD_DISPLAY_CURSOR = 20,
  245. SVGA_CMD_MOVE_CURSOR = 21,
  246. SVGA_CMD_DEFINE_ALPHA_CURSOR = 22,
  247. SVGA_CMD_DRAW_GLYPH = 23,
  248. SVGA_CMD_DRAW_GLYPH_CLIPPED = 24,
  249. SVGA_CMD_UPDATE_VERBOSE = 25,
  250. SVGA_CMD_SURFACE_FILL = 26,
  251. SVGA_CMD_SURFACE_COPY = 27,
  252. SVGA_CMD_SURFACE_ALPHA_BLEND = 28,
  253. SVGA_CMD_FRONT_ROP_FILL = 29,
  254. SVGA_CMD_FENCE = 30,
  255. };
  256. /* Legal values for the SVGA_REG_CURSOR_ON register in cursor bypass mode */
  257. enum {
  258. SVGA_CURSOR_ON_HIDE = 0,
  259. SVGA_CURSOR_ON_SHOW = 1,
  260. SVGA_CURSOR_ON_REMOVE_FROM_FB = 2,
  261. SVGA_CURSOR_ON_RESTORE_TO_FB = 3,
  262. };
  263. static inline bool vmsvga_verify_rect(DisplaySurface *surface,
  264. const char *name,
  265. int x, int y, int w, int h)
  266. {
  267. if (x < 0) {
  268. fprintf(stderr, "%s: x was < 0 (%d)\n", name, x);
  269. return false;
  270. }
  271. if (x > SVGA_MAX_WIDTH) {
  272. fprintf(stderr, "%s: x was > %d (%d)\n", name, SVGA_MAX_WIDTH, x);
  273. return false;
  274. }
  275. if (w < 0) {
  276. fprintf(stderr, "%s: w was < 0 (%d)\n", name, w);
  277. return false;
  278. }
  279. if (w > SVGA_MAX_WIDTH) {
  280. fprintf(stderr, "%s: w was > %d (%d)\n", name, SVGA_MAX_WIDTH, w);
  281. return false;
  282. }
  283. if (x + w > surface_width(surface)) {
  284. fprintf(stderr, "%s: width was > %d (x: %d, w: %d)\n",
  285. name, surface_width(surface), x, w);
  286. return false;
  287. }
  288. if (y < 0) {
  289. fprintf(stderr, "%s: y was < 0 (%d)\n", name, y);
  290. return false;
  291. }
  292. if (y > SVGA_MAX_HEIGHT) {
  293. fprintf(stderr, "%s: y was > %d (%d)\n", name, SVGA_MAX_HEIGHT, y);
  294. return false;
  295. }
  296. if (h < 0) {
  297. fprintf(stderr, "%s: h was < 0 (%d)\n", name, h);
  298. return false;
  299. }
  300. if (h > SVGA_MAX_HEIGHT) {
  301. fprintf(stderr, "%s: h was > %d (%d)\n", name, SVGA_MAX_HEIGHT, h);
  302. return false;
  303. }
  304. if (y + h > surface_height(surface)) {
  305. fprintf(stderr, "%s: update height > %d (y: %d, h: %d)\n",
  306. name, surface_height(surface), y, h);
  307. return false;
  308. }
  309. return true;
  310. }
  311. static inline void vmsvga_update_rect(struct vmsvga_state_s *s,
  312. int x, int y, int w, int h)
  313. {
  314. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  315. int line;
  316. int bypl;
  317. int width;
  318. int start;
  319. uint8_t *src;
  320. uint8_t *dst;
  321. if (!vmsvga_verify_rect(surface, __func__, x, y, w, h)) {
  322. /* go for a fullscreen update as fallback */
  323. x = 0;
  324. y = 0;
  325. w = surface_width(surface);
  326. h = surface_height(surface);
  327. }
  328. bypl = surface_stride(surface);
  329. width = surface_bytes_per_pixel(surface) * w;
  330. start = surface_bytes_per_pixel(surface) * x + bypl * y;
  331. src = s->vga.vram_ptr + start;
  332. dst = surface_data(surface) + start;
  333. for (line = h; line > 0; line--, src += bypl, dst += bypl) {
  334. memcpy(dst, src, width);
  335. }
  336. dpy_gfx_update(s->vga.con, x, y, w, h);
  337. }
  338. static inline void vmsvga_update_rect_delayed(struct vmsvga_state_s *s,
  339. int x, int y, int w, int h)
  340. {
  341. struct vmsvga_rect_s *rect = &s->redraw_fifo[s->redraw_fifo_last++];
  342. s->redraw_fifo_last &= REDRAW_FIFO_LEN - 1;
  343. rect->x = x;
  344. rect->y = y;
  345. rect->w = w;
  346. rect->h = h;
  347. }
  348. static inline void vmsvga_update_rect_flush(struct vmsvga_state_s *s)
  349. {
  350. struct vmsvga_rect_s *rect;
  351. if (s->invalidated) {
  352. s->redraw_fifo_first = s->redraw_fifo_last;
  353. return;
  354. }
  355. /* Overlapping region updates can be optimised out here - if someone
  356. * knows a smart algorithm to do that, please share. */
  357. while (s->redraw_fifo_first != s->redraw_fifo_last) {
  358. rect = &s->redraw_fifo[s->redraw_fifo_first++];
  359. s->redraw_fifo_first &= REDRAW_FIFO_LEN - 1;
  360. vmsvga_update_rect(s, rect->x, rect->y, rect->w, rect->h);
  361. }
  362. }
  363. #ifdef HW_RECT_ACCEL
  364. static inline int vmsvga_copy_rect(struct vmsvga_state_s *s,
  365. int x0, int y0, int x1, int y1, int w, int h)
  366. {
  367. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  368. uint8_t *vram = s->vga.vram_ptr;
  369. int bypl = surface_stride(surface);
  370. int bypp = surface_bytes_per_pixel(surface);
  371. int width = bypp * w;
  372. int line = h;
  373. uint8_t *ptr[2];
  374. if (!vmsvga_verify_rect(surface, "vmsvga_copy_rect/src", x0, y0, w, h)) {
  375. return -1;
  376. }
  377. if (!vmsvga_verify_rect(surface, "vmsvga_copy_rect/dst", x1, y1, w, h)) {
  378. return -1;
  379. }
  380. if (y1 > y0) {
  381. ptr[0] = vram + bypp * x0 + bypl * (y0 + h - 1);
  382. ptr[1] = vram + bypp * x1 + bypl * (y1 + h - 1);
  383. for (; line > 0; line --, ptr[0] -= bypl, ptr[1] -= bypl) {
  384. memmove(ptr[1], ptr[0], width);
  385. }
  386. } else {
  387. ptr[0] = vram + bypp * x0 + bypl * y0;
  388. ptr[1] = vram + bypp * x1 + bypl * y1;
  389. for (; line > 0; line --, ptr[0] += bypl, ptr[1] += bypl) {
  390. memmove(ptr[1], ptr[0], width);
  391. }
  392. }
  393. vmsvga_update_rect_delayed(s, x1, y1, w, h);
  394. return 0;
  395. }
  396. #endif
  397. #ifdef HW_FILL_ACCEL
  398. static inline int vmsvga_fill_rect(struct vmsvga_state_s *s,
  399. uint32_t c, int x, int y, int w, int h)
  400. {
  401. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  402. int bypl = surface_stride(surface);
  403. int width = surface_bytes_per_pixel(surface) * w;
  404. int line = h;
  405. int column;
  406. uint8_t *fst;
  407. uint8_t *dst;
  408. uint8_t *src;
  409. uint8_t col[4];
  410. if (!vmsvga_verify_rect(surface, __func__, x, y, w, h)) {
  411. return -1;
  412. }
  413. col[0] = c;
  414. col[1] = c >> 8;
  415. col[2] = c >> 16;
  416. col[3] = c >> 24;
  417. fst = s->vga.vram_ptr + surface_bytes_per_pixel(surface) * x + bypl * y;
  418. if (line--) {
  419. dst = fst;
  420. src = col;
  421. for (column = width; column > 0; column--) {
  422. *(dst++) = *(src++);
  423. if (src - col == surface_bytes_per_pixel(surface)) {
  424. src = col;
  425. }
  426. }
  427. dst = fst;
  428. for (; line > 0; line--) {
  429. dst += bypl;
  430. memcpy(dst, fst, width);
  431. }
  432. }
  433. vmsvga_update_rect_delayed(s, x, y, w, h);
  434. return 0;
  435. }
  436. #endif
  437. struct vmsvga_cursor_definition_s {
  438. uint32_t width;
  439. uint32_t height;
  440. int id;
  441. uint32_t bpp;
  442. int hot_x;
  443. int hot_y;
  444. uint32_t mask[1024];
  445. uint32_t image[4096];
  446. };
  447. #define SVGA_BITMAP_SIZE(w, h) ((((w) + 31) >> 5) * (h))
  448. #define SVGA_PIXMAP_SIZE(w, h, bpp) (((((w) * (bpp)) + 31) >> 5) * (h))
  449. #ifdef HW_MOUSE_ACCEL
  450. static inline void vmsvga_cursor_define(struct vmsvga_state_s *s,
  451. struct vmsvga_cursor_definition_s *c)
  452. {
  453. QEMUCursor *qc;
  454. int i, pixels;
  455. qc = cursor_alloc(c->width, c->height);
  456. qc->hot_x = c->hot_x;
  457. qc->hot_y = c->hot_y;
  458. switch (c->bpp) {
  459. case 1:
  460. cursor_set_mono(qc, 0xffffff, 0x000000, (void *)c->image,
  461. 1, (void *)c->mask);
  462. #ifdef DEBUG
  463. cursor_print_ascii_art(qc, "vmware/mono");
  464. #endif
  465. break;
  466. case 32:
  467. /* fill alpha channel from mask, set color to zero */
  468. cursor_set_mono(qc, 0x000000, 0x000000, (void *)c->mask,
  469. 1, (void *)c->mask);
  470. /* add in rgb values */
  471. pixels = c->width * c->height;
  472. for (i = 0; i < pixels; i++) {
  473. qc->data[i] |= c->image[i] & 0xffffff;
  474. }
  475. #ifdef DEBUG
  476. cursor_print_ascii_art(qc, "vmware/32bit");
  477. #endif
  478. break;
  479. default:
  480. fprintf(stderr, "%s: unhandled bpp %d, using fallback cursor\n",
  481. __func__, c->bpp);
  482. cursor_put(qc);
  483. qc = cursor_builtin_left_ptr();
  484. }
  485. dpy_cursor_define(s->vga.con, qc);
  486. cursor_put(qc);
  487. }
  488. #endif
  489. static inline int vmsvga_fifo_length(struct vmsvga_state_s *s)
  490. {
  491. int num;
  492. if (!s->config || !s->enable) {
  493. return 0;
  494. }
  495. s->fifo_min = le32_to_cpu(s->fifo[SVGA_FIFO_MIN]);
  496. s->fifo_max = le32_to_cpu(s->fifo[SVGA_FIFO_MAX]);
  497. s->fifo_next = le32_to_cpu(s->fifo[SVGA_FIFO_NEXT]);
  498. s->fifo_stop = le32_to_cpu(s->fifo[SVGA_FIFO_STOP]);
  499. /* Check range and alignment. */
  500. if ((s->fifo_min | s->fifo_max | s->fifo_next | s->fifo_stop) & 3) {
  501. return 0;
  502. }
  503. if (s->fifo_min < sizeof(uint32_t) * 4) {
  504. return 0;
  505. }
  506. if (s->fifo_max > SVGA_FIFO_SIZE ||
  507. s->fifo_min >= SVGA_FIFO_SIZE ||
  508. s->fifo_stop >= SVGA_FIFO_SIZE ||
  509. s->fifo_next >= SVGA_FIFO_SIZE) {
  510. return 0;
  511. }
  512. if (s->fifo_max < s->fifo_min + 10 * KiB) {
  513. return 0;
  514. }
  515. num = s->fifo_next - s->fifo_stop;
  516. if (num < 0) {
  517. num += s->fifo_max - s->fifo_min;
  518. }
  519. return num >> 2;
  520. }
  521. static inline uint32_t vmsvga_fifo_read_raw(struct vmsvga_state_s *s)
  522. {
  523. uint32_t cmd = s->fifo[s->fifo_stop >> 2];
  524. s->fifo_stop += 4;
  525. if (s->fifo_stop >= s->fifo_max) {
  526. s->fifo_stop = s->fifo_min;
  527. }
  528. s->fifo[SVGA_FIFO_STOP] = cpu_to_le32(s->fifo_stop);
  529. return cmd;
  530. }
  531. static inline uint32_t vmsvga_fifo_read(struct vmsvga_state_s *s)
  532. {
  533. return le32_to_cpu(vmsvga_fifo_read_raw(s));
  534. }
  535. static void vmsvga_fifo_run(struct vmsvga_state_s *s)
  536. {
  537. uint32_t cmd, colour;
  538. int args, len, maxloop = 1024;
  539. int x, y, dx, dy, width, height;
  540. struct vmsvga_cursor_definition_s cursor;
  541. uint32_t cmd_start;
  542. len = vmsvga_fifo_length(s);
  543. while (len > 0 && --maxloop > 0) {
  544. /* May need to go back to the start of the command if incomplete */
  545. cmd_start = s->fifo_stop;
  546. switch (cmd = vmsvga_fifo_read(s)) {
  547. case SVGA_CMD_UPDATE:
  548. case SVGA_CMD_UPDATE_VERBOSE:
  549. len -= 5;
  550. if (len < 0) {
  551. goto rewind;
  552. }
  553. x = vmsvga_fifo_read(s);
  554. y = vmsvga_fifo_read(s);
  555. width = vmsvga_fifo_read(s);
  556. height = vmsvga_fifo_read(s);
  557. vmsvga_update_rect_delayed(s, x, y, width, height);
  558. break;
  559. case SVGA_CMD_RECT_FILL:
  560. len -= 6;
  561. if (len < 0) {
  562. goto rewind;
  563. }
  564. colour = vmsvga_fifo_read(s);
  565. x = vmsvga_fifo_read(s);
  566. y = vmsvga_fifo_read(s);
  567. width = vmsvga_fifo_read(s);
  568. height = vmsvga_fifo_read(s);
  569. #ifdef HW_FILL_ACCEL
  570. if (vmsvga_fill_rect(s, colour, x, y, width, height) == 0) {
  571. break;
  572. }
  573. #endif
  574. args = 0;
  575. goto badcmd;
  576. case SVGA_CMD_RECT_COPY:
  577. len -= 7;
  578. if (len < 0) {
  579. goto rewind;
  580. }
  581. x = vmsvga_fifo_read(s);
  582. y = vmsvga_fifo_read(s);
  583. dx = vmsvga_fifo_read(s);
  584. dy = vmsvga_fifo_read(s);
  585. width = vmsvga_fifo_read(s);
  586. height = vmsvga_fifo_read(s);
  587. #ifdef HW_RECT_ACCEL
  588. if (vmsvga_copy_rect(s, x, y, dx, dy, width, height) == 0) {
  589. break;
  590. }
  591. #endif
  592. args = 0;
  593. goto badcmd;
  594. case SVGA_CMD_DEFINE_CURSOR:
  595. len -= 8;
  596. if (len < 0) {
  597. goto rewind;
  598. }
  599. cursor.id = vmsvga_fifo_read(s);
  600. cursor.hot_x = vmsvga_fifo_read(s);
  601. cursor.hot_y = vmsvga_fifo_read(s);
  602. cursor.width = x = vmsvga_fifo_read(s);
  603. cursor.height = y = vmsvga_fifo_read(s);
  604. vmsvga_fifo_read(s);
  605. cursor.bpp = vmsvga_fifo_read(s);
  606. args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
  607. if (cursor.width > 256
  608. || cursor.height > 256
  609. || cursor.bpp > 32
  610. || SVGA_BITMAP_SIZE(x, y) > ARRAY_SIZE(cursor.mask)
  611. || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
  612. > ARRAY_SIZE(cursor.image)) {
  613. goto badcmd;
  614. }
  615. len -= args;
  616. if (len < 0) {
  617. goto rewind;
  618. }
  619. for (args = 0; args < SVGA_BITMAP_SIZE(x, y); args++) {
  620. cursor.mask[args] = vmsvga_fifo_read_raw(s);
  621. }
  622. for (args = 0; args < SVGA_PIXMAP_SIZE(x, y, cursor.bpp); args++) {
  623. cursor.image[args] = vmsvga_fifo_read_raw(s);
  624. }
  625. #ifdef HW_MOUSE_ACCEL
  626. vmsvga_cursor_define(s, &cursor);
  627. break;
  628. #else
  629. args = 0;
  630. goto badcmd;
  631. #endif
  632. /*
  633. * Other commands that we at least know the number of arguments
  634. * for so we can avoid FIFO desync if driver uses them illegally.
  635. */
  636. case SVGA_CMD_DEFINE_ALPHA_CURSOR:
  637. len -= 6;
  638. if (len < 0) {
  639. goto rewind;
  640. }
  641. vmsvga_fifo_read(s);
  642. vmsvga_fifo_read(s);
  643. vmsvga_fifo_read(s);
  644. x = vmsvga_fifo_read(s);
  645. y = vmsvga_fifo_read(s);
  646. args = x * y;
  647. goto badcmd;
  648. case SVGA_CMD_RECT_ROP_FILL:
  649. args = 6;
  650. goto badcmd;
  651. case SVGA_CMD_RECT_ROP_COPY:
  652. args = 7;
  653. goto badcmd;
  654. case SVGA_CMD_DRAW_GLYPH_CLIPPED:
  655. len -= 4;
  656. if (len < 0) {
  657. goto rewind;
  658. }
  659. vmsvga_fifo_read(s);
  660. vmsvga_fifo_read(s);
  661. args = 7 + (vmsvga_fifo_read(s) >> 2);
  662. goto badcmd;
  663. case SVGA_CMD_SURFACE_ALPHA_BLEND:
  664. args = 12;
  665. goto badcmd;
  666. /*
  667. * Other commands that are not listed as depending on any
  668. * CAPABILITIES bits, but are not described in the README either.
  669. */
  670. case SVGA_CMD_SURFACE_FILL:
  671. case SVGA_CMD_SURFACE_COPY:
  672. case SVGA_CMD_FRONT_ROP_FILL:
  673. case SVGA_CMD_FENCE:
  674. case SVGA_CMD_INVALID_CMD:
  675. break; /* Nop */
  676. default:
  677. args = 0;
  678. badcmd:
  679. len -= args;
  680. if (len < 0) {
  681. goto rewind;
  682. }
  683. while (args--) {
  684. vmsvga_fifo_read(s);
  685. }
  686. printf("%s: Unknown command 0x%02x in SVGA command FIFO\n",
  687. __func__, cmd);
  688. break;
  689. rewind:
  690. s->fifo_stop = cmd_start;
  691. s->fifo[SVGA_FIFO_STOP] = cpu_to_le32(s->fifo_stop);
  692. break;
  693. }
  694. }
  695. s->syncing = 0;
  696. }
  697. static uint32_t vmsvga_index_read(void *opaque, uint32_t address)
  698. {
  699. struct vmsvga_state_s *s = opaque;
  700. return s->index;
  701. }
  702. static void vmsvga_index_write(void *opaque, uint32_t address, uint32_t index)
  703. {
  704. struct vmsvga_state_s *s = opaque;
  705. s->index = index;
  706. }
  707. static uint32_t vmsvga_value_read(void *opaque, uint32_t address)
  708. {
  709. uint32_t caps;
  710. struct vmsvga_state_s *s = opaque;
  711. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  712. PixelFormat pf;
  713. uint32_t ret;
  714. switch (s->index) {
  715. case SVGA_REG_ID:
  716. ret = s->svgaid;
  717. break;
  718. case SVGA_REG_ENABLE:
  719. ret = s->enable;
  720. break;
  721. case SVGA_REG_WIDTH:
  722. ret = s->new_width ? s->new_width : surface_width(surface);
  723. break;
  724. case SVGA_REG_HEIGHT:
  725. ret = s->new_height ? s->new_height : surface_height(surface);
  726. break;
  727. case SVGA_REG_MAX_WIDTH:
  728. ret = SVGA_MAX_WIDTH;
  729. break;
  730. case SVGA_REG_MAX_HEIGHT:
  731. ret = SVGA_MAX_HEIGHT;
  732. break;
  733. case SVGA_REG_DEPTH:
  734. ret = (s->new_depth == 32) ? 24 : s->new_depth;
  735. break;
  736. case SVGA_REG_BITS_PER_PIXEL:
  737. case SVGA_REG_HOST_BITS_PER_PIXEL:
  738. ret = s->new_depth;
  739. break;
  740. case SVGA_REG_PSEUDOCOLOR:
  741. ret = 0x0;
  742. break;
  743. case SVGA_REG_RED_MASK:
  744. pf = qemu_default_pixelformat(s->new_depth);
  745. ret = pf.rmask;
  746. break;
  747. case SVGA_REG_GREEN_MASK:
  748. pf = qemu_default_pixelformat(s->new_depth);
  749. ret = pf.gmask;
  750. break;
  751. case SVGA_REG_BLUE_MASK:
  752. pf = qemu_default_pixelformat(s->new_depth);
  753. ret = pf.bmask;
  754. break;
  755. case SVGA_REG_BYTES_PER_LINE:
  756. if (s->new_width) {
  757. ret = (s->new_depth * s->new_width) / 8;
  758. } else {
  759. ret = surface_stride(surface);
  760. }
  761. break;
  762. case SVGA_REG_FB_START: {
  763. struct pci_vmsvga_state_s *pci_vmsvga
  764. = container_of(s, struct pci_vmsvga_state_s, chip);
  765. ret = pci_get_bar_addr(PCI_DEVICE(pci_vmsvga), 1);
  766. break;
  767. }
  768. case SVGA_REG_FB_OFFSET:
  769. ret = 0x0;
  770. break;
  771. case SVGA_REG_VRAM_SIZE:
  772. ret = s->vga.vram_size; /* No physical VRAM besides the framebuffer */
  773. break;
  774. case SVGA_REG_FB_SIZE:
  775. ret = s->vga.vram_size;
  776. break;
  777. case SVGA_REG_CAPABILITIES:
  778. caps = SVGA_CAP_NONE;
  779. #ifdef HW_RECT_ACCEL
  780. caps |= SVGA_CAP_RECT_COPY;
  781. #endif
  782. #ifdef HW_FILL_ACCEL
  783. caps |= SVGA_CAP_RECT_FILL;
  784. #endif
  785. #ifdef HW_MOUSE_ACCEL
  786. if (dpy_cursor_define_supported(s->vga.con)) {
  787. caps |= SVGA_CAP_CURSOR | SVGA_CAP_CURSOR_BYPASS_2 |
  788. SVGA_CAP_CURSOR_BYPASS;
  789. }
  790. #endif
  791. ret = caps;
  792. break;
  793. case SVGA_REG_MEM_START: {
  794. struct pci_vmsvga_state_s *pci_vmsvga
  795. = container_of(s, struct pci_vmsvga_state_s, chip);
  796. ret = pci_get_bar_addr(PCI_DEVICE(pci_vmsvga), 2);
  797. break;
  798. }
  799. case SVGA_REG_MEM_SIZE:
  800. ret = s->fifo_size;
  801. break;
  802. case SVGA_REG_CONFIG_DONE:
  803. ret = s->config;
  804. break;
  805. case SVGA_REG_SYNC:
  806. case SVGA_REG_BUSY:
  807. ret = s->syncing;
  808. break;
  809. case SVGA_REG_GUEST_ID:
  810. ret = s->guest;
  811. break;
  812. case SVGA_REG_CURSOR_ID:
  813. ret = s->cursor.id;
  814. break;
  815. case SVGA_REG_CURSOR_X:
  816. ret = s->cursor.x;
  817. break;
  818. case SVGA_REG_CURSOR_Y:
  819. ret = s->cursor.y;
  820. break;
  821. case SVGA_REG_CURSOR_ON:
  822. ret = s->cursor.on;
  823. break;
  824. case SVGA_REG_SCRATCH_SIZE:
  825. ret = s->scratch_size;
  826. break;
  827. case SVGA_REG_MEM_REGS:
  828. case SVGA_REG_NUM_DISPLAYS:
  829. case SVGA_REG_PITCHLOCK:
  830. case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
  831. ret = 0;
  832. break;
  833. default:
  834. if (s->index >= SVGA_SCRATCH_BASE &&
  835. s->index < SVGA_SCRATCH_BASE + s->scratch_size) {
  836. ret = s->scratch[s->index - SVGA_SCRATCH_BASE];
  837. break;
  838. }
  839. printf("%s: Bad register %02x\n", __func__, s->index);
  840. ret = 0;
  841. break;
  842. }
  843. if (s->index >= SVGA_SCRATCH_BASE) {
  844. trace_vmware_scratch_read(s->index, ret);
  845. } else if (s->index >= SVGA_PALETTE_BASE) {
  846. trace_vmware_palette_read(s->index, ret);
  847. } else {
  848. trace_vmware_value_read(s->index, ret);
  849. }
  850. return ret;
  851. }
  852. static void vmsvga_value_write(void *opaque, uint32_t address, uint32_t value)
  853. {
  854. struct vmsvga_state_s *s = opaque;
  855. if (s->index >= SVGA_SCRATCH_BASE) {
  856. trace_vmware_scratch_write(s->index, value);
  857. } else if (s->index >= SVGA_PALETTE_BASE) {
  858. trace_vmware_palette_write(s->index, value);
  859. } else {
  860. trace_vmware_value_write(s->index, value);
  861. }
  862. switch (s->index) {
  863. case SVGA_REG_ID:
  864. if (value == SVGA_ID_2 || value == SVGA_ID_1 || value == SVGA_ID_0) {
  865. s->svgaid = value;
  866. }
  867. break;
  868. case SVGA_REG_ENABLE:
  869. s->enable = !!value;
  870. s->invalidated = 1;
  871. s->vga.hw_ops->invalidate(&s->vga);
  872. if (s->enable && s->config) {
  873. vga_dirty_log_stop(&s->vga);
  874. } else {
  875. vga_dirty_log_start(&s->vga);
  876. }
  877. break;
  878. case SVGA_REG_WIDTH:
  879. if (value <= SVGA_MAX_WIDTH) {
  880. s->new_width = value;
  881. s->invalidated = 1;
  882. } else {
  883. printf("%s: Bad width: %i\n", __func__, value);
  884. }
  885. break;
  886. case SVGA_REG_HEIGHT:
  887. if (value <= SVGA_MAX_HEIGHT) {
  888. s->new_height = value;
  889. s->invalidated = 1;
  890. } else {
  891. printf("%s: Bad height: %i\n", __func__, value);
  892. }
  893. break;
  894. case SVGA_REG_BITS_PER_PIXEL:
  895. if (value != 32) {
  896. printf("%s: Bad bits per pixel: %i bits\n", __func__, value);
  897. s->config = 0;
  898. s->invalidated = 1;
  899. }
  900. break;
  901. case SVGA_REG_CONFIG_DONE:
  902. if (value) {
  903. s->fifo = (uint32_t *) s->fifo_ptr;
  904. vga_dirty_log_stop(&s->vga);
  905. }
  906. s->config = !!value;
  907. break;
  908. case SVGA_REG_SYNC:
  909. s->syncing = 1;
  910. vmsvga_fifo_run(s); /* Or should we just wait for update_display? */
  911. break;
  912. case SVGA_REG_GUEST_ID:
  913. s->guest = value;
  914. #ifdef VERBOSE
  915. if (value >= GUEST_OS_BASE && value < GUEST_OS_BASE +
  916. ARRAY_SIZE(vmsvga_guest_id)) {
  917. printf("%s: guest runs %s.\n", __func__,
  918. vmsvga_guest_id[value - GUEST_OS_BASE]);
  919. }
  920. #endif
  921. break;
  922. case SVGA_REG_CURSOR_ID:
  923. s->cursor.id = value;
  924. break;
  925. case SVGA_REG_CURSOR_X:
  926. s->cursor.x = value;
  927. break;
  928. case SVGA_REG_CURSOR_Y:
  929. s->cursor.y = value;
  930. break;
  931. case SVGA_REG_CURSOR_ON:
  932. s->cursor.on |= (value == SVGA_CURSOR_ON_SHOW);
  933. s->cursor.on &= (value != SVGA_CURSOR_ON_HIDE);
  934. #ifdef HW_MOUSE_ACCEL
  935. if (value <= SVGA_CURSOR_ON_SHOW) {
  936. dpy_mouse_set(s->vga.con, s->cursor.x, s->cursor.y, s->cursor.on);
  937. }
  938. #endif
  939. break;
  940. case SVGA_REG_DEPTH:
  941. case SVGA_REG_MEM_REGS:
  942. case SVGA_REG_NUM_DISPLAYS:
  943. case SVGA_REG_PITCHLOCK:
  944. case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
  945. break;
  946. default:
  947. if (s->index >= SVGA_SCRATCH_BASE &&
  948. s->index < SVGA_SCRATCH_BASE + s->scratch_size) {
  949. s->scratch[s->index - SVGA_SCRATCH_BASE] = value;
  950. break;
  951. }
  952. printf("%s: Bad register %02x\n", __func__, s->index);
  953. }
  954. }
  955. static uint32_t vmsvga_bios_read(void *opaque, uint32_t address)
  956. {
  957. printf("%s: what are we supposed to return?\n", __func__);
  958. return 0xcafe;
  959. }
  960. static void vmsvga_bios_write(void *opaque, uint32_t address, uint32_t data)
  961. {
  962. printf("%s: what are we supposed to do with (%08x)?\n", __func__, data);
  963. }
  964. static inline void vmsvga_check_size(struct vmsvga_state_s *s)
  965. {
  966. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  967. if (s->new_width != surface_width(surface) ||
  968. s->new_height != surface_height(surface) ||
  969. s->new_depth != surface_bits_per_pixel(surface)) {
  970. int stride = (s->new_depth * s->new_width) / 8;
  971. pixman_format_code_t format =
  972. qemu_default_pixman_format(s->new_depth, true);
  973. trace_vmware_setmode(s->new_width, s->new_height, s->new_depth);
  974. surface = qemu_create_displaysurface_from(s->new_width, s->new_height,
  975. format, stride,
  976. s->vga.vram_ptr);
  977. dpy_gfx_replace_surface(s->vga.con, surface);
  978. s->invalidated = 1;
  979. }
  980. }
  981. static void vmsvga_update_display(void *opaque)
  982. {
  983. struct vmsvga_state_s *s = opaque;
  984. if (!s->enable || !s->config) {
  985. /* in standard vga mode */
  986. s->vga.hw_ops->gfx_update(&s->vga);
  987. return;
  988. }
  989. vmsvga_check_size(s);
  990. vmsvga_fifo_run(s);
  991. vmsvga_update_rect_flush(s);
  992. if (s->invalidated) {
  993. s->invalidated = 0;
  994. dpy_gfx_update_full(s->vga.con);
  995. }
  996. }
  997. static void vmsvga_reset(DeviceState *dev)
  998. {
  999. struct pci_vmsvga_state_s *pci = VMWARE_SVGA(dev);
  1000. struct vmsvga_state_s *s = &pci->chip;
  1001. s->index = 0;
  1002. s->enable = 0;
  1003. s->config = 0;
  1004. s->svgaid = SVGA_ID;
  1005. s->cursor.on = 0;
  1006. s->redraw_fifo_first = 0;
  1007. s->redraw_fifo_last = 0;
  1008. s->syncing = 0;
  1009. vga_dirty_log_start(&s->vga);
  1010. }
  1011. static void vmsvga_invalidate_display(void *opaque)
  1012. {
  1013. struct vmsvga_state_s *s = opaque;
  1014. if (!s->enable) {
  1015. s->vga.hw_ops->invalidate(&s->vga);
  1016. return;
  1017. }
  1018. s->invalidated = 1;
  1019. }
  1020. static void vmsvga_text_update(void *opaque, console_ch_t *chardata)
  1021. {
  1022. struct vmsvga_state_s *s = opaque;
  1023. if (s->vga.hw_ops->text_update) {
  1024. s->vga.hw_ops->text_update(&s->vga, chardata);
  1025. }
  1026. }
  1027. static int vmsvga_post_load(void *opaque, int version_id)
  1028. {
  1029. struct vmsvga_state_s *s = opaque;
  1030. s->invalidated = 1;
  1031. if (s->config) {
  1032. s->fifo = (uint32_t *) s->fifo_ptr;
  1033. }
  1034. return 0;
  1035. }
  1036. static const VMStateDescription vmstate_vmware_vga_internal = {
  1037. .name = "vmware_vga_internal",
  1038. .version_id = 0,
  1039. .minimum_version_id = 0,
  1040. .post_load = vmsvga_post_load,
  1041. .fields = (VMStateField[]) {
  1042. VMSTATE_INT32_EQUAL(new_depth, struct vmsvga_state_s, NULL),
  1043. VMSTATE_INT32(enable, struct vmsvga_state_s),
  1044. VMSTATE_INT32(config, struct vmsvga_state_s),
  1045. VMSTATE_INT32(cursor.id, struct vmsvga_state_s),
  1046. VMSTATE_INT32(cursor.x, struct vmsvga_state_s),
  1047. VMSTATE_INT32(cursor.y, struct vmsvga_state_s),
  1048. VMSTATE_INT32(cursor.on, struct vmsvga_state_s),
  1049. VMSTATE_INT32(index, struct vmsvga_state_s),
  1050. VMSTATE_VARRAY_INT32(scratch, struct vmsvga_state_s,
  1051. scratch_size, 0, vmstate_info_uint32, uint32_t),
  1052. VMSTATE_INT32(new_width, struct vmsvga_state_s),
  1053. VMSTATE_INT32(new_height, struct vmsvga_state_s),
  1054. VMSTATE_UINT32(guest, struct vmsvga_state_s),
  1055. VMSTATE_UINT32(svgaid, struct vmsvga_state_s),
  1056. VMSTATE_INT32(syncing, struct vmsvga_state_s),
  1057. VMSTATE_UNUSED(4), /* was fb_size */
  1058. VMSTATE_END_OF_LIST()
  1059. }
  1060. };
  1061. static const VMStateDescription vmstate_vmware_vga = {
  1062. .name = "vmware_vga",
  1063. .version_id = 0,
  1064. .minimum_version_id = 0,
  1065. .fields = (VMStateField[]) {
  1066. VMSTATE_PCI_DEVICE(parent_obj, struct pci_vmsvga_state_s),
  1067. VMSTATE_STRUCT(chip, struct pci_vmsvga_state_s, 0,
  1068. vmstate_vmware_vga_internal, struct vmsvga_state_s),
  1069. VMSTATE_END_OF_LIST()
  1070. }
  1071. };
  1072. static const GraphicHwOps vmsvga_ops = {
  1073. .invalidate = vmsvga_invalidate_display,
  1074. .gfx_update = vmsvga_update_display,
  1075. .text_update = vmsvga_text_update,
  1076. };
  1077. static void vmsvga_init(DeviceState *dev, struct vmsvga_state_s *s,
  1078. MemoryRegion *address_space, MemoryRegion *io)
  1079. {
  1080. s->scratch_size = SVGA_SCRATCH_SIZE;
  1081. s->scratch = g_malloc(s->scratch_size * 4);
  1082. s->vga.con = graphic_console_init(dev, 0, &vmsvga_ops, s);
  1083. s->fifo_size = SVGA_FIFO_SIZE;
  1084. memory_region_init_ram(&s->fifo_ram, NULL, "vmsvga.fifo", s->fifo_size,
  1085. &error_fatal);
  1086. s->fifo_ptr = memory_region_get_ram_ptr(&s->fifo_ram);
  1087. vga_common_init(&s->vga, OBJECT(dev));
  1088. vga_init(&s->vga, OBJECT(dev), address_space, io, true);
  1089. vmstate_register(NULL, 0, &vmstate_vga_common, &s->vga);
  1090. s->new_depth = 32;
  1091. }
  1092. static uint64_t vmsvga_io_read(void *opaque, hwaddr addr, unsigned size)
  1093. {
  1094. struct vmsvga_state_s *s = opaque;
  1095. switch (addr) {
  1096. case SVGA_IO_MUL * SVGA_INDEX_PORT: return vmsvga_index_read(s, addr);
  1097. case SVGA_IO_MUL * SVGA_VALUE_PORT: return vmsvga_value_read(s, addr);
  1098. case SVGA_IO_MUL * SVGA_BIOS_PORT: return vmsvga_bios_read(s, addr);
  1099. default: return -1u;
  1100. }
  1101. }
  1102. static void vmsvga_io_write(void *opaque, hwaddr addr,
  1103. uint64_t data, unsigned size)
  1104. {
  1105. struct vmsvga_state_s *s = opaque;
  1106. switch (addr) {
  1107. case SVGA_IO_MUL * SVGA_INDEX_PORT:
  1108. vmsvga_index_write(s, addr, data);
  1109. break;
  1110. case SVGA_IO_MUL * SVGA_VALUE_PORT:
  1111. vmsvga_value_write(s, addr, data);
  1112. break;
  1113. case SVGA_IO_MUL * SVGA_BIOS_PORT:
  1114. vmsvga_bios_write(s, addr, data);
  1115. break;
  1116. }
  1117. }
  1118. static const MemoryRegionOps vmsvga_io_ops = {
  1119. .read = vmsvga_io_read,
  1120. .write = vmsvga_io_write,
  1121. .endianness = DEVICE_LITTLE_ENDIAN,
  1122. .valid = {
  1123. .min_access_size = 4,
  1124. .max_access_size = 4,
  1125. .unaligned = true,
  1126. },
  1127. .impl = {
  1128. .unaligned = true,
  1129. },
  1130. };
  1131. static void pci_vmsvga_realize(PCIDevice *dev, Error **errp)
  1132. {
  1133. struct pci_vmsvga_state_s *s = VMWARE_SVGA(dev);
  1134. dev->config[PCI_CACHE_LINE_SIZE] = 0x08;
  1135. dev->config[PCI_LATENCY_TIMER] = 0x40;
  1136. dev->config[PCI_INTERRUPT_LINE] = 0xff; /* End */
  1137. memory_region_init_io(&s->io_bar, NULL, &vmsvga_io_ops, &s->chip,
  1138. "vmsvga-io", 0x10);
  1139. memory_region_set_flush_coalesced(&s->io_bar);
  1140. pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
  1141. vmsvga_init(DEVICE(dev), &s->chip,
  1142. pci_address_space(dev), pci_address_space_io(dev));
  1143. pci_register_bar(dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
  1144. &s->chip.vga.vram);
  1145. pci_register_bar(dev, 2, PCI_BASE_ADDRESS_MEM_PREFETCH,
  1146. &s->chip.fifo_ram);
  1147. if (!dev->rom_bar) {
  1148. /* compatibility with pc-0.13 and older */
  1149. vga_init_vbe(&s->chip.vga, OBJECT(dev), pci_address_space(dev));
  1150. }
  1151. }
  1152. static Property vga_vmware_properties[] = {
  1153. DEFINE_PROP_UINT32("vgamem_mb", struct pci_vmsvga_state_s,
  1154. chip.vga.vram_size_mb, 16),
  1155. DEFINE_PROP_BOOL("global-vmstate", struct pci_vmsvga_state_s,
  1156. chip.vga.global_vmstate, false),
  1157. DEFINE_PROP_END_OF_LIST(),
  1158. };
  1159. static void vmsvga_class_init(ObjectClass *klass, void *data)
  1160. {
  1161. DeviceClass *dc = DEVICE_CLASS(klass);
  1162. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  1163. k->realize = pci_vmsvga_realize;
  1164. k->romfile = "vgabios-vmware.bin";
  1165. k->vendor_id = PCI_VENDOR_ID_VMWARE;
  1166. k->device_id = SVGA_PCI_DEVICE_ID;
  1167. k->class_id = PCI_CLASS_DISPLAY_VGA;
  1168. k->subsystem_vendor_id = PCI_VENDOR_ID_VMWARE;
  1169. k->subsystem_id = SVGA_PCI_DEVICE_ID;
  1170. dc->reset = vmsvga_reset;
  1171. dc->vmsd = &vmstate_vmware_vga;
  1172. dc->props = vga_vmware_properties;
  1173. dc->hotpluggable = false;
  1174. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  1175. }
  1176. static const TypeInfo vmsvga_info = {
  1177. .name = TYPE_VMWARE_SVGA,
  1178. .parent = TYPE_PCI_DEVICE,
  1179. .instance_size = sizeof(struct pci_vmsvga_state_s),
  1180. .class_init = vmsvga_class_init,
  1181. .interfaces = (InterfaceInfo[]) {
  1182. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  1183. { },
  1184. },
  1185. };
  1186. static void vmsvga_register_types(void)
  1187. {
  1188. type_register_static(&vmsvga_info);
  1189. }
  1190. type_init(vmsvga_register_types)