vmware_vga.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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 "qemu/log.h"
  29. #include "hw/loader.h"
  30. #include "trace.h"
  31. #include "hw/pci/pci_device.h"
  32. #include "hw/qdev-properties.h"
  33. #include "migration/vmstate.h"
  34. #include "qom/object.h"
  35. #include "ui/console.h"
  36. #undef VERBOSE
  37. #define HW_RECT_ACCEL
  38. #define HW_FILL_ACCEL
  39. #define HW_MOUSE_ACCEL
  40. #include "vga_int.h"
  41. /* See http://vmware-svga.sf.net/ for some documentation on VMWare SVGA */
  42. struct vmsvga_state_s {
  43. VGACommonState vga;
  44. int invalidated;
  45. int enable;
  46. int config;
  47. struct {
  48. int id;
  49. int x;
  50. int y;
  51. int on;
  52. } cursor;
  53. int index;
  54. int scratch_size;
  55. uint32_t *scratch;
  56. int new_width;
  57. int new_height;
  58. int new_depth;
  59. uint32_t guest;
  60. uint32_t svgaid;
  61. int syncing;
  62. MemoryRegion fifo_ram;
  63. uint8_t *fifo_ptr;
  64. unsigned int fifo_size;
  65. uint32_t *fifo;
  66. uint32_t fifo_min;
  67. uint32_t fifo_max;
  68. uint32_t fifo_next;
  69. uint32_t fifo_stop;
  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_last;
  75. };
  76. #define TYPE_VMWARE_SVGA "vmware-svga"
  77. DECLARE_INSTANCE_CHECKER(struct pci_vmsvga_state_s, VMWARE_SVGA,
  78. 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,
  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 2368
  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 bool vmsvga_verify_rect(DisplaySurface *surface,
  266. const char *name,
  267. int x, int y, int w, int h)
  268. {
  269. if (x < 0) {
  270. trace_vmware_verify_rect_less_than_zero(name, "x", x);
  271. return false;
  272. }
  273. if (x > SVGA_MAX_WIDTH) {
  274. trace_vmware_verify_rect_greater_than_bound(name, "x", SVGA_MAX_WIDTH,
  275. x);
  276. return false;
  277. }
  278. if (w < 0) {
  279. trace_vmware_verify_rect_less_than_zero(name, "w", w);
  280. return false;
  281. }
  282. if (w > SVGA_MAX_WIDTH) {
  283. trace_vmware_verify_rect_greater_than_bound(name, "w", SVGA_MAX_WIDTH,
  284. w);
  285. return false;
  286. }
  287. if (x + w > surface_width(surface)) {
  288. trace_vmware_verify_rect_surface_bound_exceeded(name, "width",
  289. surface_width(surface),
  290. "x", x, "w", w);
  291. return false;
  292. }
  293. if (y < 0) {
  294. trace_vmware_verify_rect_less_than_zero(name, "y", y);
  295. return false;
  296. }
  297. if (y > SVGA_MAX_HEIGHT) {
  298. trace_vmware_verify_rect_greater_than_bound(name, "y", SVGA_MAX_HEIGHT,
  299. y);
  300. return false;
  301. }
  302. if (h < 0) {
  303. trace_vmware_verify_rect_less_than_zero(name, "h", h);
  304. return false;
  305. }
  306. if (h > SVGA_MAX_HEIGHT) {
  307. trace_vmware_verify_rect_greater_than_bound(name, "y", SVGA_MAX_HEIGHT,
  308. y);
  309. return false;
  310. }
  311. if (y + h > surface_height(surface)) {
  312. trace_vmware_verify_rect_surface_bound_exceeded(name, "height",
  313. surface_height(surface),
  314. "y", y, "h", h);
  315. return false;
  316. }
  317. return true;
  318. }
  319. static inline void vmsvga_update_rect(struct vmsvga_state_s *s,
  320. int x, int y, int w, int h)
  321. {
  322. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  323. int line;
  324. int bypl;
  325. int width;
  326. int start;
  327. uint8_t *src;
  328. uint8_t *dst;
  329. if (!vmsvga_verify_rect(surface, __func__, x, y, w, h)) {
  330. /* go for a fullscreen update as fallback */
  331. x = 0;
  332. y = 0;
  333. w = surface_width(surface);
  334. h = surface_height(surface);
  335. }
  336. bypl = surface_stride(surface);
  337. width = surface_bytes_per_pixel(surface) * w;
  338. start = surface_bytes_per_pixel(surface) * x + bypl * y;
  339. src = s->vga.vram_ptr + start;
  340. dst = surface_data(surface) + start;
  341. for (line = h; line > 0; line--, src += bypl, dst += bypl) {
  342. memcpy(dst, src, width);
  343. }
  344. dpy_gfx_update(s->vga.con, x, y, w, h);
  345. }
  346. static inline void vmsvga_update_rect_flush(struct vmsvga_state_s *s)
  347. {
  348. struct vmsvga_rect_s *rect;
  349. if (s->invalidated) {
  350. s->redraw_fifo_last = 0;
  351. return;
  352. }
  353. /* Overlapping region updates can be optimised out here - if someone
  354. * knows a smart algorithm to do that, please share. */
  355. for (int i = 0; i < s->redraw_fifo_last; i++) {
  356. rect = &s->redraw_fifo[i];
  357. vmsvga_update_rect(s, rect->x, rect->y, rect->w, rect->h);
  358. }
  359. s->redraw_fifo_last = 0;
  360. }
  361. static inline void vmsvga_update_rect_delayed(struct vmsvga_state_s *s,
  362. int x, int y, int w, int h)
  363. {
  364. if (s->redraw_fifo_last >= REDRAW_FIFO_LEN) {
  365. trace_vmware_update_rect_delayed_flush();
  366. vmsvga_update_rect_flush(s);
  367. }
  368. struct vmsvga_rect_s *rect = &s->redraw_fifo[s->redraw_fifo_last++];
  369. rect->x = x;
  370. rect->y = y;
  371. rect->w = w;
  372. rect->h = h;
  373. }
  374. #ifdef HW_RECT_ACCEL
  375. static inline int vmsvga_copy_rect(struct vmsvga_state_s *s,
  376. int x0, int y0, int x1, int y1, int w, int h)
  377. {
  378. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  379. uint8_t *vram = s->vga.vram_ptr;
  380. int bypl = surface_stride(surface);
  381. int bypp = surface_bytes_per_pixel(surface);
  382. int width = bypp * w;
  383. int line = h;
  384. uint8_t *ptr[2];
  385. if (!vmsvga_verify_rect(surface, "vmsvga_copy_rect/src", x0, y0, w, h)) {
  386. return -1;
  387. }
  388. if (!vmsvga_verify_rect(surface, "vmsvga_copy_rect/dst", x1, y1, w, h)) {
  389. return -1;
  390. }
  391. if (y1 > y0) {
  392. ptr[0] = vram + bypp * x0 + bypl * (y0 + h - 1);
  393. ptr[1] = vram + bypp * x1 + bypl * (y1 + h - 1);
  394. for (; line > 0; line --, ptr[0] -= bypl, ptr[1] -= bypl) {
  395. memmove(ptr[1], ptr[0], width);
  396. }
  397. } else {
  398. ptr[0] = vram + bypp * x0 + bypl * y0;
  399. ptr[1] = vram + bypp * x1 + bypl * y1;
  400. for (; line > 0; line --, ptr[0] += bypl, ptr[1] += bypl) {
  401. memmove(ptr[1], ptr[0], width);
  402. }
  403. }
  404. vmsvga_update_rect_delayed(s, x1, y1, w, h);
  405. return 0;
  406. }
  407. #endif
  408. #ifdef HW_FILL_ACCEL
  409. static inline int vmsvga_fill_rect(struct vmsvga_state_s *s,
  410. uint32_t c, int x, int y, int w, int h)
  411. {
  412. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  413. int bypl = surface_stride(surface);
  414. int width = surface_bytes_per_pixel(surface) * w;
  415. int line = h;
  416. int column;
  417. uint8_t *fst;
  418. uint8_t *dst;
  419. uint8_t *src;
  420. uint8_t col[4];
  421. if (!vmsvga_verify_rect(surface, __func__, x, y, w, h)) {
  422. return -1;
  423. }
  424. col[0] = c;
  425. col[1] = c >> 8;
  426. col[2] = c >> 16;
  427. col[3] = c >> 24;
  428. fst = s->vga.vram_ptr + surface_bytes_per_pixel(surface) * x + bypl * y;
  429. if (line--) {
  430. dst = fst;
  431. src = col;
  432. for (column = width; column > 0; column--) {
  433. *(dst++) = *(src++);
  434. if (src - col == surface_bytes_per_pixel(surface)) {
  435. src = col;
  436. }
  437. }
  438. dst = fst;
  439. for (; line > 0; line--) {
  440. dst += bypl;
  441. memcpy(dst, fst, width);
  442. }
  443. }
  444. vmsvga_update_rect_delayed(s, x, y, w, h);
  445. return 0;
  446. }
  447. #endif
  448. struct vmsvga_cursor_definition_s {
  449. uint32_t width;
  450. uint32_t height;
  451. int id;
  452. uint32_t bpp;
  453. int hot_x;
  454. int hot_y;
  455. uint32_t mask[1024];
  456. uint32_t image[4096];
  457. };
  458. #define SVGA_BITMAP_SIZE(w, h) ((((w) + 31) >> 5) * (h))
  459. #define SVGA_PIXMAP_SIZE(w, h, bpp) (((((w) * (bpp)) + 31) >> 5) * (h))
  460. #ifdef HW_MOUSE_ACCEL
  461. static inline void vmsvga_cursor_define(struct vmsvga_state_s *s,
  462. struct vmsvga_cursor_definition_s *c)
  463. {
  464. QEMUCursor *qc;
  465. int i, pixels;
  466. qc = cursor_alloc(c->width, c->height);
  467. assert(qc != NULL);
  468. qc->hot_x = c->hot_x;
  469. qc->hot_y = c->hot_y;
  470. switch (c->bpp) {
  471. case 1:
  472. cursor_set_mono(qc, 0xffffff, 0x000000, (void *)c->image,
  473. 1, (void *)c->mask);
  474. #ifdef DEBUG
  475. cursor_print_ascii_art(qc, "vmware/mono");
  476. #endif
  477. break;
  478. case 32:
  479. /* fill alpha channel from mask, set color to zero */
  480. cursor_set_mono(qc, 0x000000, 0x000000, (void *)c->mask,
  481. 1, (void *)c->mask);
  482. /* add in rgb values */
  483. pixels = c->width * c->height;
  484. for (i = 0; i < pixels; i++) {
  485. qc->data[i] |= c->image[i] & 0xffffff;
  486. }
  487. #ifdef DEBUG
  488. cursor_print_ascii_art(qc, "vmware/32bit");
  489. #endif
  490. break;
  491. default:
  492. fprintf(stderr, "%s: unhandled bpp %d, using fallback cursor\n",
  493. __func__, c->bpp);
  494. cursor_unref(qc);
  495. qc = cursor_builtin_left_ptr();
  496. }
  497. dpy_cursor_define(s->vga.con, qc);
  498. cursor_unref(qc);
  499. }
  500. #endif
  501. static inline int vmsvga_fifo_length(struct vmsvga_state_s *s)
  502. {
  503. int num;
  504. if (!s->config || !s->enable) {
  505. return 0;
  506. }
  507. s->fifo_min = le32_to_cpu(s->fifo[SVGA_FIFO_MIN]);
  508. s->fifo_max = le32_to_cpu(s->fifo[SVGA_FIFO_MAX]);
  509. s->fifo_next = le32_to_cpu(s->fifo[SVGA_FIFO_NEXT]);
  510. s->fifo_stop = le32_to_cpu(s->fifo[SVGA_FIFO_STOP]);
  511. /* Check range and alignment. */
  512. if ((s->fifo_min | s->fifo_max | s->fifo_next | s->fifo_stop) & 3) {
  513. return 0;
  514. }
  515. if (s->fifo_min < sizeof(uint32_t) * 4) {
  516. return 0;
  517. }
  518. if (s->fifo_max > SVGA_FIFO_SIZE ||
  519. s->fifo_min >= SVGA_FIFO_SIZE ||
  520. s->fifo_stop >= SVGA_FIFO_SIZE ||
  521. s->fifo_next >= SVGA_FIFO_SIZE) {
  522. return 0;
  523. }
  524. if (s->fifo_max < s->fifo_min + 10 * KiB) {
  525. return 0;
  526. }
  527. num = s->fifo_next - s->fifo_stop;
  528. if (num < 0) {
  529. num += s->fifo_max - s->fifo_min;
  530. }
  531. return num >> 2;
  532. }
  533. static inline uint32_t vmsvga_fifo_read_raw(struct vmsvga_state_s *s)
  534. {
  535. uint32_t cmd = s->fifo[s->fifo_stop >> 2];
  536. s->fifo_stop += 4;
  537. if (s->fifo_stop >= s->fifo_max) {
  538. s->fifo_stop = s->fifo_min;
  539. }
  540. s->fifo[SVGA_FIFO_STOP] = cpu_to_le32(s->fifo_stop);
  541. return cmd;
  542. }
  543. static inline uint32_t vmsvga_fifo_read(struct vmsvga_state_s *s)
  544. {
  545. return le32_to_cpu(vmsvga_fifo_read_raw(s));
  546. }
  547. static void vmsvga_fifo_run(struct vmsvga_state_s *s)
  548. {
  549. uint32_t cmd, colour;
  550. int args, len, maxloop = 1024;
  551. int x, y, dx, dy, width, height;
  552. struct vmsvga_cursor_definition_s cursor;
  553. uint32_t cmd_start;
  554. len = vmsvga_fifo_length(s);
  555. while (len > 0 && --maxloop > 0) {
  556. /* May need to go back to the start of the command if incomplete */
  557. cmd_start = s->fifo_stop;
  558. switch (cmd = vmsvga_fifo_read(s)) {
  559. case SVGA_CMD_UPDATE:
  560. case SVGA_CMD_UPDATE_VERBOSE:
  561. len -= 5;
  562. if (len < 0) {
  563. goto rewind;
  564. }
  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. vmsvga_update_rect_delayed(s, x, y, width, height);
  570. break;
  571. case SVGA_CMD_RECT_FILL:
  572. len -= 6;
  573. if (len < 0) {
  574. goto rewind;
  575. }
  576. colour = vmsvga_fifo_read(s);
  577. x = vmsvga_fifo_read(s);
  578. y = vmsvga_fifo_read(s);
  579. width = vmsvga_fifo_read(s);
  580. height = vmsvga_fifo_read(s);
  581. #ifdef HW_FILL_ACCEL
  582. if (vmsvga_fill_rect(s, colour, x, y, width, height) == 0) {
  583. break;
  584. }
  585. #endif
  586. args = 0;
  587. goto badcmd;
  588. case SVGA_CMD_RECT_COPY:
  589. len -= 7;
  590. if (len < 0) {
  591. goto rewind;
  592. }
  593. x = vmsvga_fifo_read(s);
  594. y = vmsvga_fifo_read(s);
  595. dx = vmsvga_fifo_read(s);
  596. dy = vmsvga_fifo_read(s);
  597. width = vmsvga_fifo_read(s);
  598. height = vmsvga_fifo_read(s);
  599. #ifdef HW_RECT_ACCEL
  600. if (vmsvga_copy_rect(s, x, y, dx, dy, width, height) == 0) {
  601. break;
  602. }
  603. #endif
  604. args = 0;
  605. goto badcmd;
  606. case SVGA_CMD_DEFINE_CURSOR:
  607. len -= 8;
  608. if (len < 0) {
  609. goto rewind;
  610. }
  611. cursor.id = vmsvga_fifo_read(s);
  612. cursor.hot_x = vmsvga_fifo_read(s);
  613. cursor.hot_y = vmsvga_fifo_read(s);
  614. cursor.width = x = vmsvga_fifo_read(s);
  615. cursor.height = y = vmsvga_fifo_read(s);
  616. vmsvga_fifo_read(s);
  617. cursor.bpp = vmsvga_fifo_read(s);
  618. args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
  619. if (cursor.width > 256
  620. || cursor.height > 256
  621. || cursor.bpp > 32
  622. || SVGA_BITMAP_SIZE(x, y) > ARRAY_SIZE(cursor.mask)
  623. || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
  624. > ARRAY_SIZE(cursor.image)) {
  625. goto badcmd;
  626. }
  627. len -= args;
  628. if (len < 0) {
  629. goto rewind;
  630. }
  631. for (args = 0; args < SVGA_BITMAP_SIZE(x, y); args++) {
  632. cursor.mask[args] = vmsvga_fifo_read_raw(s);
  633. }
  634. for (args = 0; args < SVGA_PIXMAP_SIZE(x, y, cursor.bpp); args++) {
  635. cursor.image[args] = vmsvga_fifo_read_raw(s);
  636. }
  637. #ifdef HW_MOUSE_ACCEL
  638. vmsvga_cursor_define(s, &cursor);
  639. break;
  640. #else
  641. args = 0;
  642. goto badcmd;
  643. #endif
  644. /*
  645. * Other commands that we at least know the number of arguments
  646. * for so we can avoid FIFO desync if driver uses them illegally.
  647. */
  648. case SVGA_CMD_DEFINE_ALPHA_CURSOR:
  649. len -= 6;
  650. if (len < 0) {
  651. goto rewind;
  652. }
  653. vmsvga_fifo_read(s);
  654. vmsvga_fifo_read(s);
  655. vmsvga_fifo_read(s);
  656. x = vmsvga_fifo_read(s);
  657. y = vmsvga_fifo_read(s);
  658. args = x * y;
  659. goto badcmd;
  660. case SVGA_CMD_RECT_ROP_FILL:
  661. args = 6;
  662. goto badcmd;
  663. case SVGA_CMD_RECT_ROP_COPY:
  664. args = 7;
  665. goto badcmd;
  666. case SVGA_CMD_DRAW_GLYPH_CLIPPED:
  667. len -= 4;
  668. if (len < 0) {
  669. goto rewind;
  670. }
  671. vmsvga_fifo_read(s);
  672. vmsvga_fifo_read(s);
  673. args = 7 + (vmsvga_fifo_read(s) >> 2);
  674. goto badcmd;
  675. case SVGA_CMD_SURFACE_ALPHA_BLEND:
  676. args = 12;
  677. goto badcmd;
  678. /*
  679. * Other commands that are not listed as depending on any
  680. * CAPABILITIES bits, but are not described in the README either.
  681. */
  682. case SVGA_CMD_SURFACE_FILL:
  683. case SVGA_CMD_SURFACE_COPY:
  684. case SVGA_CMD_FRONT_ROP_FILL:
  685. case SVGA_CMD_FENCE:
  686. case SVGA_CMD_INVALID_CMD:
  687. break; /* Nop */
  688. default:
  689. args = 0;
  690. badcmd:
  691. len -= args;
  692. if (len < 0) {
  693. goto rewind;
  694. }
  695. while (args--) {
  696. vmsvga_fifo_read(s);
  697. }
  698. printf("%s: Unknown command 0x%02x in SVGA command FIFO\n",
  699. __func__, cmd);
  700. break;
  701. rewind:
  702. s->fifo_stop = cmd_start;
  703. s->fifo[SVGA_FIFO_STOP] = cpu_to_le32(s->fifo_stop);
  704. break;
  705. }
  706. }
  707. s->syncing = 0;
  708. }
  709. static uint32_t vmsvga_index_read(void *opaque, uint32_t address)
  710. {
  711. struct vmsvga_state_s *s = opaque;
  712. return s->index;
  713. }
  714. static void vmsvga_index_write(void *opaque, uint32_t address, uint32_t index)
  715. {
  716. struct vmsvga_state_s *s = opaque;
  717. s->index = index;
  718. }
  719. static uint32_t vmsvga_value_read(void *opaque, uint32_t address)
  720. {
  721. uint32_t caps;
  722. struct vmsvga_state_s *s = opaque;
  723. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  724. PixelFormat pf;
  725. uint32_t ret;
  726. switch (s->index) {
  727. case SVGA_REG_ID:
  728. ret = s->svgaid;
  729. break;
  730. case SVGA_REG_ENABLE:
  731. ret = s->enable;
  732. break;
  733. case SVGA_REG_WIDTH:
  734. ret = s->new_width ? s->new_width : surface_width(surface);
  735. break;
  736. case SVGA_REG_HEIGHT:
  737. ret = s->new_height ? s->new_height : surface_height(surface);
  738. break;
  739. case SVGA_REG_MAX_WIDTH:
  740. ret = SVGA_MAX_WIDTH;
  741. break;
  742. case SVGA_REG_MAX_HEIGHT:
  743. ret = SVGA_MAX_HEIGHT;
  744. break;
  745. case SVGA_REG_DEPTH:
  746. ret = (s->new_depth == 32) ? 24 : s->new_depth;
  747. break;
  748. case SVGA_REG_BITS_PER_PIXEL:
  749. case SVGA_REG_HOST_BITS_PER_PIXEL:
  750. ret = s->new_depth;
  751. break;
  752. case SVGA_REG_PSEUDOCOLOR:
  753. ret = 0x0;
  754. break;
  755. case SVGA_REG_RED_MASK:
  756. pf = qemu_default_pixelformat(s->new_depth);
  757. ret = pf.rmask;
  758. break;
  759. case SVGA_REG_GREEN_MASK:
  760. pf = qemu_default_pixelformat(s->new_depth);
  761. ret = pf.gmask;
  762. break;
  763. case SVGA_REG_BLUE_MASK:
  764. pf = qemu_default_pixelformat(s->new_depth);
  765. ret = pf.bmask;
  766. break;
  767. case SVGA_REG_BYTES_PER_LINE:
  768. if (s->new_width) {
  769. ret = (s->new_depth * s->new_width) / 8;
  770. } else {
  771. ret = surface_stride(surface);
  772. }
  773. break;
  774. case SVGA_REG_FB_START: {
  775. struct pci_vmsvga_state_s *pci_vmsvga
  776. = container_of(s, struct pci_vmsvga_state_s, chip);
  777. ret = pci_get_bar_addr(PCI_DEVICE(pci_vmsvga), 1);
  778. break;
  779. }
  780. case SVGA_REG_FB_OFFSET:
  781. ret = 0x0;
  782. break;
  783. case SVGA_REG_VRAM_SIZE:
  784. ret = s->vga.vram_size; /* No physical VRAM besides the framebuffer */
  785. break;
  786. case SVGA_REG_FB_SIZE:
  787. ret = s->vga.vram_size;
  788. break;
  789. case SVGA_REG_CAPABILITIES:
  790. caps = SVGA_CAP_NONE;
  791. #ifdef HW_RECT_ACCEL
  792. caps |= SVGA_CAP_RECT_COPY;
  793. #endif
  794. #ifdef HW_FILL_ACCEL
  795. caps |= SVGA_CAP_RECT_FILL;
  796. #endif
  797. #ifdef HW_MOUSE_ACCEL
  798. if (dpy_cursor_define_supported(s->vga.con)) {
  799. caps |= SVGA_CAP_CURSOR | SVGA_CAP_CURSOR_BYPASS_2 |
  800. SVGA_CAP_CURSOR_BYPASS;
  801. }
  802. #endif
  803. ret = caps;
  804. break;
  805. case SVGA_REG_MEM_START: {
  806. struct pci_vmsvga_state_s *pci_vmsvga
  807. = container_of(s, struct pci_vmsvga_state_s, chip);
  808. ret = pci_get_bar_addr(PCI_DEVICE(pci_vmsvga), 2);
  809. break;
  810. }
  811. case SVGA_REG_MEM_SIZE:
  812. ret = s->fifo_size;
  813. break;
  814. case SVGA_REG_CONFIG_DONE:
  815. ret = s->config;
  816. break;
  817. case SVGA_REG_SYNC:
  818. case SVGA_REG_BUSY:
  819. ret = s->syncing;
  820. break;
  821. case SVGA_REG_GUEST_ID:
  822. ret = s->guest;
  823. break;
  824. case SVGA_REG_CURSOR_ID:
  825. ret = s->cursor.id;
  826. break;
  827. case SVGA_REG_CURSOR_X:
  828. ret = s->cursor.x;
  829. break;
  830. case SVGA_REG_CURSOR_Y:
  831. ret = s->cursor.y;
  832. break;
  833. case SVGA_REG_CURSOR_ON:
  834. ret = s->cursor.on;
  835. break;
  836. case SVGA_REG_SCRATCH_SIZE:
  837. ret = s->scratch_size;
  838. break;
  839. case SVGA_REG_MEM_REGS:
  840. case SVGA_REG_NUM_DISPLAYS:
  841. case SVGA_REG_PITCHLOCK:
  842. case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
  843. ret = 0;
  844. break;
  845. default:
  846. if (s->index >= SVGA_SCRATCH_BASE &&
  847. s->index < SVGA_SCRATCH_BASE + s->scratch_size) {
  848. ret = s->scratch[s->index - SVGA_SCRATCH_BASE];
  849. break;
  850. }
  851. qemu_log_mask(LOG_GUEST_ERROR,
  852. "%s: Bad register %02x\n", __func__, s->index);
  853. ret = 0;
  854. break;
  855. }
  856. if (s->index >= SVGA_SCRATCH_BASE) {
  857. trace_vmware_scratch_read(s->index, ret);
  858. } else if (s->index >= SVGA_PALETTE_BASE) {
  859. trace_vmware_palette_read(s->index, ret);
  860. } else {
  861. trace_vmware_value_read(s->index, ret);
  862. }
  863. return ret;
  864. }
  865. static void vmsvga_value_write(void *opaque, uint32_t address, uint32_t value)
  866. {
  867. struct vmsvga_state_s *s = opaque;
  868. if (s->index >= SVGA_SCRATCH_BASE) {
  869. trace_vmware_scratch_write(s->index, value);
  870. } else if (s->index >= SVGA_PALETTE_BASE) {
  871. trace_vmware_palette_write(s->index, value);
  872. } else {
  873. trace_vmware_value_write(s->index, value);
  874. }
  875. switch (s->index) {
  876. case SVGA_REG_ID:
  877. if (value == SVGA_ID_2 || value == SVGA_ID_1 || value == SVGA_ID_0) {
  878. s->svgaid = value;
  879. }
  880. break;
  881. case SVGA_REG_ENABLE:
  882. s->enable = !!value;
  883. s->invalidated = 1;
  884. s->vga.hw_ops->invalidate(&s->vga);
  885. if (s->enable && s->config) {
  886. vga_dirty_log_stop(&s->vga);
  887. } else {
  888. vga_dirty_log_start(&s->vga);
  889. }
  890. break;
  891. case SVGA_REG_WIDTH:
  892. if (value <= SVGA_MAX_WIDTH) {
  893. s->new_width = value;
  894. s->invalidated = 1;
  895. } else {
  896. qemu_log_mask(LOG_GUEST_ERROR,
  897. "%s: Bad width: %i\n", __func__, value);
  898. }
  899. break;
  900. case SVGA_REG_HEIGHT:
  901. if (value <= SVGA_MAX_HEIGHT) {
  902. s->new_height = value;
  903. s->invalidated = 1;
  904. } else {
  905. qemu_log_mask(LOG_GUEST_ERROR,
  906. "%s: Bad height: %i\n", __func__, value);
  907. }
  908. break;
  909. case SVGA_REG_BITS_PER_PIXEL:
  910. if (value != 32) {
  911. qemu_log_mask(LOG_GUEST_ERROR,
  912. "%s: Bad bits per pixel: %i bits\n", __func__, value);
  913. s->config = 0;
  914. s->invalidated = 1;
  915. }
  916. break;
  917. case SVGA_REG_CONFIG_DONE:
  918. if (value) {
  919. s->fifo = (uint32_t *) s->fifo_ptr;
  920. vga_dirty_log_stop(&s->vga);
  921. }
  922. s->config = !!value;
  923. break;
  924. case SVGA_REG_SYNC:
  925. s->syncing = 1;
  926. vmsvga_fifo_run(s); /* Or should we just wait for update_display? */
  927. break;
  928. case SVGA_REG_GUEST_ID:
  929. s->guest = value;
  930. #ifdef VERBOSE
  931. if (value >= GUEST_OS_BASE && value < GUEST_OS_BASE +
  932. ARRAY_SIZE(vmsvga_guest_id)) {
  933. printf("%s: guest runs %s.\n", __func__,
  934. vmsvga_guest_id[value - GUEST_OS_BASE]);
  935. }
  936. #endif
  937. break;
  938. case SVGA_REG_CURSOR_ID:
  939. s->cursor.id = value;
  940. break;
  941. case SVGA_REG_CURSOR_X:
  942. s->cursor.x = value;
  943. break;
  944. case SVGA_REG_CURSOR_Y:
  945. s->cursor.y = value;
  946. break;
  947. case SVGA_REG_CURSOR_ON:
  948. s->cursor.on |= (value == SVGA_CURSOR_ON_SHOW);
  949. s->cursor.on &= (value != SVGA_CURSOR_ON_HIDE);
  950. #ifdef HW_MOUSE_ACCEL
  951. if (value <= SVGA_CURSOR_ON_SHOW) {
  952. dpy_mouse_set(s->vga.con, s->cursor.x, s->cursor.y, s->cursor.on);
  953. }
  954. #endif
  955. break;
  956. case SVGA_REG_DEPTH:
  957. case SVGA_REG_MEM_REGS:
  958. case SVGA_REG_NUM_DISPLAYS:
  959. case SVGA_REG_PITCHLOCK:
  960. case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
  961. break;
  962. default:
  963. if (s->index >= SVGA_SCRATCH_BASE &&
  964. s->index < SVGA_SCRATCH_BASE + s->scratch_size) {
  965. s->scratch[s->index - SVGA_SCRATCH_BASE] = value;
  966. break;
  967. }
  968. qemu_log_mask(LOG_GUEST_ERROR,
  969. "%s: Bad register %02x\n", __func__, s->index);
  970. }
  971. }
  972. static uint32_t vmsvga_bios_read(void *opaque, uint32_t address)
  973. {
  974. printf("%s: what are we supposed to return?\n", __func__);
  975. return 0xcafe;
  976. }
  977. static void vmsvga_bios_write(void *opaque, uint32_t address, uint32_t data)
  978. {
  979. printf("%s: what are we supposed to do with (%08x)?\n", __func__, data);
  980. }
  981. static inline void vmsvga_check_size(struct vmsvga_state_s *s)
  982. {
  983. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  984. if (s->new_width != surface_width(surface) ||
  985. s->new_height != surface_height(surface) ||
  986. s->new_depth != surface_bits_per_pixel(surface)) {
  987. int stride = (s->new_depth * s->new_width) / 8;
  988. pixman_format_code_t format =
  989. qemu_default_pixman_format(s->new_depth, true);
  990. trace_vmware_setmode(s->new_width, s->new_height, s->new_depth);
  991. surface = qemu_create_displaysurface_from(s->new_width, s->new_height,
  992. format, stride,
  993. s->vga.vram_ptr);
  994. dpy_gfx_replace_surface(s->vga.con, surface);
  995. s->invalidated = 1;
  996. }
  997. }
  998. static void vmsvga_update_display(void *opaque)
  999. {
  1000. struct vmsvga_state_s *s = opaque;
  1001. if (!s->enable || !s->config) {
  1002. /* in standard vga mode */
  1003. s->vga.hw_ops->gfx_update(&s->vga);
  1004. return;
  1005. }
  1006. vmsvga_check_size(s);
  1007. vmsvga_fifo_run(s);
  1008. vmsvga_update_rect_flush(s);
  1009. if (s->invalidated) {
  1010. s->invalidated = 0;
  1011. dpy_gfx_update_full(s->vga.con);
  1012. }
  1013. }
  1014. static void vmsvga_reset(DeviceState *dev)
  1015. {
  1016. struct pci_vmsvga_state_s *pci = VMWARE_SVGA(dev);
  1017. struct vmsvga_state_s *s = &pci->chip;
  1018. s->index = 0;
  1019. s->enable = 0;
  1020. s->config = 0;
  1021. s->svgaid = SVGA_ID;
  1022. s->cursor.on = 0;
  1023. s->redraw_fifo_last = 0;
  1024. s->syncing = 0;
  1025. vga_dirty_log_start(&s->vga);
  1026. }
  1027. static void vmsvga_invalidate_display(void *opaque)
  1028. {
  1029. struct vmsvga_state_s *s = opaque;
  1030. if (!s->enable) {
  1031. s->vga.hw_ops->invalidate(&s->vga);
  1032. return;
  1033. }
  1034. s->invalidated = 1;
  1035. }
  1036. static void vmsvga_text_update(void *opaque, console_ch_t *chardata)
  1037. {
  1038. struct vmsvga_state_s *s = opaque;
  1039. if (s->vga.hw_ops->text_update) {
  1040. s->vga.hw_ops->text_update(&s->vga, chardata);
  1041. }
  1042. }
  1043. static int vmsvga_post_load(void *opaque, int version_id)
  1044. {
  1045. struct vmsvga_state_s *s = opaque;
  1046. s->invalidated = 1;
  1047. if (s->config) {
  1048. s->fifo = (uint32_t *) s->fifo_ptr;
  1049. }
  1050. return 0;
  1051. }
  1052. static const VMStateDescription vmstate_vmware_vga_internal = {
  1053. .name = "vmware_vga_internal",
  1054. .version_id = 0,
  1055. .minimum_version_id = 0,
  1056. .post_load = vmsvga_post_load,
  1057. .fields = (VMStateField[]) {
  1058. VMSTATE_INT32_EQUAL(new_depth, struct vmsvga_state_s, NULL),
  1059. VMSTATE_INT32(enable, struct vmsvga_state_s),
  1060. VMSTATE_INT32(config, struct vmsvga_state_s),
  1061. VMSTATE_INT32(cursor.id, struct vmsvga_state_s),
  1062. VMSTATE_INT32(cursor.x, struct vmsvga_state_s),
  1063. VMSTATE_INT32(cursor.y, struct vmsvga_state_s),
  1064. VMSTATE_INT32(cursor.on, struct vmsvga_state_s),
  1065. VMSTATE_INT32(index, struct vmsvga_state_s),
  1066. VMSTATE_VARRAY_INT32(scratch, struct vmsvga_state_s,
  1067. scratch_size, 0, vmstate_info_uint32, uint32_t),
  1068. VMSTATE_INT32(new_width, struct vmsvga_state_s),
  1069. VMSTATE_INT32(new_height, struct vmsvga_state_s),
  1070. VMSTATE_UINT32(guest, struct vmsvga_state_s),
  1071. VMSTATE_UINT32(svgaid, struct vmsvga_state_s),
  1072. VMSTATE_INT32(syncing, struct vmsvga_state_s),
  1073. VMSTATE_UNUSED(4), /* was fb_size */
  1074. VMSTATE_END_OF_LIST()
  1075. }
  1076. };
  1077. static const VMStateDescription vmstate_vmware_vga = {
  1078. .name = "vmware_vga",
  1079. .version_id = 0,
  1080. .minimum_version_id = 0,
  1081. .fields = (VMStateField[]) {
  1082. VMSTATE_PCI_DEVICE(parent_obj, struct pci_vmsvga_state_s),
  1083. VMSTATE_STRUCT(chip, struct pci_vmsvga_state_s, 0,
  1084. vmstate_vmware_vga_internal, struct vmsvga_state_s),
  1085. VMSTATE_END_OF_LIST()
  1086. }
  1087. };
  1088. static const GraphicHwOps vmsvga_ops = {
  1089. .invalidate = vmsvga_invalidate_display,
  1090. .gfx_update = vmsvga_update_display,
  1091. .text_update = vmsvga_text_update,
  1092. };
  1093. static void vmsvga_init(DeviceState *dev, struct vmsvga_state_s *s,
  1094. MemoryRegion *address_space, MemoryRegion *io)
  1095. {
  1096. s->scratch_size = SVGA_SCRATCH_SIZE;
  1097. s->scratch = g_malloc(s->scratch_size * 4);
  1098. s->vga.con = graphic_console_init(dev, 0, &vmsvga_ops, s);
  1099. s->fifo_size = SVGA_FIFO_SIZE;
  1100. memory_region_init_ram(&s->fifo_ram, NULL, "vmsvga.fifo", s->fifo_size,
  1101. &error_fatal);
  1102. s->fifo_ptr = memory_region_get_ram_ptr(&s->fifo_ram);
  1103. vga_common_init(&s->vga, OBJECT(dev), &error_fatal);
  1104. vga_init(&s->vga, OBJECT(dev), address_space, io, true);
  1105. vmstate_register(NULL, 0, &vmstate_vga_common, &s->vga);
  1106. s->new_depth = 32;
  1107. }
  1108. static uint64_t vmsvga_io_read(void *opaque, hwaddr addr, unsigned size)
  1109. {
  1110. struct vmsvga_state_s *s = opaque;
  1111. switch (addr) {
  1112. case SVGA_IO_MUL * SVGA_INDEX_PORT: return vmsvga_index_read(s, addr);
  1113. case SVGA_IO_MUL * SVGA_VALUE_PORT: return vmsvga_value_read(s, addr);
  1114. case SVGA_IO_MUL * SVGA_BIOS_PORT: return vmsvga_bios_read(s, addr);
  1115. default: return -1u;
  1116. }
  1117. }
  1118. static void vmsvga_io_write(void *opaque, hwaddr addr,
  1119. uint64_t data, unsigned size)
  1120. {
  1121. struct vmsvga_state_s *s = opaque;
  1122. switch (addr) {
  1123. case SVGA_IO_MUL * SVGA_INDEX_PORT:
  1124. vmsvga_index_write(s, addr, data);
  1125. break;
  1126. case SVGA_IO_MUL * SVGA_VALUE_PORT:
  1127. vmsvga_value_write(s, addr, data);
  1128. break;
  1129. case SVGA_IO_MUL * SVGA_BIOS_PORT:
  1130. vmsvga_bios_write(s, addr, data);
  1131. break;
  1132. }
  1133. }
  1134. static const MemoryRegionOps vmsvga_io_ops = {
  1135. .read = vmsvga_io_read,
  1136. .write = vmsvga_io_write,
  1137. .endianness = DEVICE_LITTLE_ENDIAN,
  1138. .valid = {
  1139. .min_access_size = 4,
  1140. .max_access_size = 4,
  1141. .unaligned = true,
  1142. },
  1143. .impl = {
  1144. .unaligned = true,
  1145. },
  1146. };
  1147. static void pci_vmsvga_realize(PCIDevice *dev, Error **errp)
  1148. {
  1149. struct pci_vmsvga_state_s *s = VMWARE_SVGA(dev);
  1150. dev->config[PCI_CACHE_LINE_SIZE] = 0x08;
  1151. dev->config[PCI_LATENCY_TIMER] = 0x40;
  1152. dev->config[PCI_INTERRUPT_LINE] = 0xff; /* End */
  1153. memory_region_init_io(&s->io_bar, OBJECT(dev), &vmsvga_io_ops, &s->chip,
  1154. "vmsvga-io", 0x10);
  1155. memory_region_set_flush_coalesced(&s->io_bar);
  1156. pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
  1157. vmsvga_init(DEVICE(dev), &s->chip,
  1158. pci_address_space(dev), pci_address_space_io(dev));
  1159. pci_register_bar(dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
  1160. &s->chip.vga.vram);
  1161. pci_register_bar(dev, 2, PCI_BASE_ADDRESS_MEM_PREFETCH,
  1162. &s->chip.fifo_ram);
  1163. }
  1164. static Property vga_vmware_properties[] = {
  1165. DEFINE_PROP_UINT32("vgamem_mb", struct pci_vmsvga_state_s,
  1166. chip.vga.vram_size_mb, 16),
  1167. DEFINE_PROP_BOOL("global-vmstate", struct pci_vmsvga_state_s,
  1168. chip.vga.global_vmstate, false),
  1169. DEFINE_PROP_END_OF_LIST(),
  1170. };
  1171. static void vmsvga_class_init(ObjectClass *klass, void *data)
  1172. {
  1173. DeviceClass *dc = DEVICE_CLASS(klass);
  1174. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  1175. k->realize = pci_vmsvga_realize;
  1176. k->romfile = "vgabios-vmware.bin";
  1177. k->vendor_id = PCI_VENDOR_ID_VMWARE;
  1178. k->device_id = SVGA_PCI_DEVICE_ID;
  1179. k->class_id = PCI_CLASS_DISPLAY_VGA;
  1180. k->subsystem_vendor_id = PCI_VENDOR_ID_VMWARE;
  1181. k->subsystem_id = SVGA_PCI_DEVICE_ID;
  1182. dc->reset = vmsvga_reset;
  1183. dc->vmsd = &vmstate_vmware_vga;
  1184. device_class_set_props(dc, vga_vmware_properties);
  1185. dc->hotpluggable = false;
  1186. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  1187. }
  1188. static const TypeInfo vmsvga_info = {
  1189. .name = TYPE_VMWARE_SVGA,
  1190. .parent = TYPE_PCI_DEVICE,
  1191. .instance_size = sizeof(struct pci_vmsvga_state_s),
  1192. .class_init = vmsvga_class_init,
  1193. .interfaces = (InterfaceInfo[]) {
  1194. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  1195. { },
  1196. },
  1197. };
  1198. static void vmsvga_register_types(void)
  1199. {
  1200. type_register_static(&vmsvga_info);
  1201. }
  1202. type_init(vmsvga_register_types)