vmware_vga.c 38 KB

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