vmware_vga.c 37 KB

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