vmware_vga.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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 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. fprintf(stderr, "%s: x was < 0 (%d)\n", name, x);
  271. return false;
  272. }
  273. if (x > SVGA_MAX_WIDTH) {
  274. fprintf(stderr, "%s: x was > %d (%d)\n", name, SVGA_MAX_WIDTH, x);
  275. return false;
  276. }
  277. if (w < 0) {
  278. fprintf(stderr, "%s: w was < 0 (%d)\n", name, w);
  279. return false;
  280. }
  281. if (w > SVGA_MAX_WIDTH) {
  282. fprintf(stderr, "%s: w was > %d (%d)\n", name, SVGA_MAX_WIDTH, w);
  283. return false;
  284. }
  285. if (x + w > surface_width(surface)) {
  286. fprintf(stderr, "%s: width was > %d (x: %d, w: %d)\n",
  287. name, surface_width(surface), x, w);
  288. return false;
  289. }
  290. if (y < 0) {
  291. fprintf(stderr, "%s: y was < 0 (%d)\n", name, y);
  292. return false;
  293. }
  294. if (y > SVGA_MAX_HEIGHT) {
  295. fprintf(stderr, "%s: y was > %d (%d)\n", name, SVGA_MAX_HEIGHT, y);
  296. return false;
  297. }
  298. if (h < 0) {
  299. fprintf(stderr, "%s: h was < 0 (%d)\n", name, h);
  300. return false;
  301. }
  302. if (h > SVGA_MAX_HEIGHT) {
  303. fprintf(stderr, "%s: h was > %d (%d)\n", name, SVGA_MAX_HEIGHT, h);
  304. return false;
  305. }
  306. if (y + h > surface_height(surface)) {
  307. fprintf(stderr, "%s: update height > %d (y: %d, h: %d)\n",
  308. name, surface_height(surface), y, h);
  309. return false;
  310. }
  311. return true;
  312. }
  313. static inline void vmsvga_update_rect(struct vmsvga_state_s *s,
  314. int x, int y, int w, int h)
  315. {
  316. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  317. int line;
  318. int bypl;
  319. int width;
  320. int start;
  321. uint8_t *src;
  322. uint8_t *dst;
  323. if (!vmsvga_verify_rect(surface, __func__, x, y, w, h)) {
  324. /* go for a fullscreen update as fallback */
  325. x = 0;
  326. y = 0;
  327. w = surface_width(surface);
  328. h = surface_height(surface);
  329. }
  330. bypl = surface_stride(surface);
  331. width = surface_bytes_per_pixel(surface) * w;
  332. start = surface_bytes_per_pixel(surface) * x + bypl * y;
  333. src = s->vga.vram_ptr + start;
  334. dst = surface_data(surface) + start;
  335. for (line = h; line > 0; line--, src += bypl, dst += bypl) {
  336. memcpy(dst, src, width);
  337. }
  338. dpy_gfx_update(s->vga.con, x, y, w, h);
  339. }
  340. static inline void vmsvga_update_rect_delayed(struct vmsvga_state_s *s,
  341. int x, int y, int w, int h)
  342. {
  343. struct vmsvga_rect_s *rect = &s->redraw_fifo[s->redraw_fifo_last++];
  344. s->redraw_fifo_last &= REDRAW_FIFO_LEN - 1;
  345. rect->x = x;
  346. rect->y = y;
  347. rect->w = w;
  348. rect->h = h;
  349. }
  350. static inline void vmsvga_update_rect_flush(struct vmsvga_state_s *s)
  351. {
  352. struct vmsvga_rect_s *rect;
  353. if (s->invalidated) {
  354. s->redraw_fifo_first = s->redraw_fifo_last;
  355. return;
  356. }
  357. /* Overlapping region updates can be optimised out here - if someone
  358. * knows a smart algorithm to do that, please share. */
  359. while (s->redraw_fifo_first != s->redraw_fifo_last) {
  360. rect = &s->redraw_fifo[s->redraw_fifo_first++];
  361. s->redraw_fifo_first &= REDRAW_FIFO_LEN - 1;
  362. vmsvga_update_rect(s, rect->x, rect->y, rect->w, rect->h);
  363. }
  364. }
  365. #ifdef HW_RECT_ACCEL
  366. static inline int vmsvga_copy_rect(struct vmsvga_state_s *s,
  367. int x0, int y0, int x1, int y1, int w, int h)
  368. {
  369. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  370. uint8_t *vram = s->vga.vram_ptr;
  371. int bypl = surface_stride(surface);
  372. int bypp = surface_bytes_per_pixel(surface);
  373. int width = bypp * w;
  374. int line = h;
  375. uint8_t *ptr[2];
  376. if (!vmsvga_verify_rect(surface, "vmsvga_copy_rect/src", x0, y0, w, h)) {
  377. return -1;
  378. }
  379. if (!vmsvga_verify_rect(surface, "vmsvga_copy_rect/dst", x1, y1, w, h)) {
  380. return -1;
  381. }
  382. if (y1 > y0) {
  383. ptr[0] = vram + bypp * x0 + bypl * (y0 + h - 1);
  384. ptr[1] = vram + bypp * x1 + bypl * (y1 + h - 1);
  385. for (; line > 0; line --, ptr[0] -= bypl, ptr[1] -= bypl) {
  386. memmove(ptr[1], ptr[0], width);
  387. }
  388. } else {
  389. ptr[0] = vram + bypp * x0 + bypl * y0;
  390. ptr[1] = vram + bypp * x1 + bypl * y1;
  391. for (; line > 0; line --, ptr[0] += bypl, ptr[1] += bypl) {
  392. memmove(ptr[1], ptr[0], width);
  393. }
  394. }
  395. vmsvga_update_rect_delayed(s, x1, y1, w, h);
  396. return 0;
  397. }
  398. #endif
  399. #ifdef HW_FILL_ACCEL
  400. static inline int vmsvga_fill_rect(struct vmsvga_state_s *s,
  401. uint32_t c, int x, int y, int w, int h)
  402. {
  403. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  404. int bypl = surface_stride(surface);
  405. int width = surface_bytes_per_pixel(surface) * w;
  406. int line = h;
  407. int column;
  408. uint8_t *fst;
  409. uint8_t *dst;
  410. uint8_t *src;
  411. uint8_t col[4];
  412. if (!vmsvga_verify_rect(surface, __func__, x, y, w, h)) {
  413. return -1;
  414. }
  415. col[0] = c;
  416. col[1] = c >> 8;
  417. col[2] = c >> 16;
  418. col[3] = c >> 24;
  419. fst = s->vga.vram_ptr + surface_bytes_per_pixel(surface) * x + bypl * y;
  420. if (line--) {
  421. dst = fst;
  422. src = col;
  423. for (column = width; column > 0; column--) {
  424. *(dst++) = *(src++);
  425. if (src - col == surface_bytes_per_pixel(surface)) {
  426. src = col;
  427. }
  428. }
  429. dst = fst;
  430. for (; line > 0; line--) {
  431. dst += bypl;
  432. memcpy(dst, fst, width);
  433. }
  434. }
  435. vmsvga_update_rect_delayed(s, x, y, w, h);
  436. return 0;
  437. }
  438. #endif
  439. struct vmsvga_cursor_definition_s {
  440. int width;
  441. int height;
  442. int id;
  443. int bpp;
  444. int hot_x;
  445. int hot_y;
  446. uint32_t mask[1024];
  447. uint32_t image[4096];
  448. };
  449. #define SVGA_BITMAP_SIZE(w, h) ((((w) + 31) >> 5) * (h))
  450. #define SVGA_PIXMAP_SIZE(w, h, bpp) (((((w) * (bpp)) + 31) >> 5) * (h))
  451. #ifdef HW_MOUSE_ACCEL
  452. static inline void vmsvga_cursor_define(struct vmsvga_state_s *s,
  453. struct vmsvga_cursor_definition_s *c)
  454. {
  455. QEMUCursor *qc;
  456. int i, pixels;
  457. qc = cursor_alloc(c->width, c->height);
  458. qc->hot_x = c->hot_x;
  459. qc->hot_y = c->hot_y;
  460. switch (c->bpp) {
  461. case 1:
  462. cursor_set_mono(qc, 0xffffff, 0x000000, (void *)c->image,
  463. 1, (void *)c->mask);
  464. #ifdef DEBUG
  465. cursor_print_ascii_art(qc, "vmware/mono");
  466. #endif
  467. break;
  468. case 32:
  469. /* fill alpha channel from mask, set color to zero */
  470. cursor_set_mono(qc, 0x000000, 0x000000, (void *)c->mask,
  471. 1, (void *)c->mask);
  472. /* add in rgb values */
  473. pixels = c->width * c->height;
  474. for (i = 0; i < pixels; i++) {
  475. qc->data[i] |= c->image[i] & 0xffffff;
  476. }
  477. #ifdef DEBUG
  478. cursor_print_ascii_art(qc, "vmware/32bit");
  479. #endif
  480. break;
  481. default:
  482. fprintf(stderr, "%s: unhandled bpp %d, using fallback cursor\n",
  483. __func__, c->bpp);
  484. cursor_put(qc);
  485. qc = cursor_builtin_left_ptr();
  486. }
  487. dpy_cursor_define(s->vga.con, qc);
  488. cursor_put(qc);
  489. }
  490. #endif
  491. #define CMD(f) le32_to_cpu(s->cmd->f)
  492. static inline int vmsvga_fifo_length(struct vmsvga_state_s *s)
  493. {
  494. int num;
  495. if (!s->config || !s->enable) {
  496. return 0;
  497. }
  498. num = CMD(next_cmd) - CMD(stop);
  499. if (num < 0) {
  500. num += CMD(max) - CMD(min);
  501. }
  502. return num >> 2;
  503. }
  504. static inline uint32_t vmsvga_fifo_read_raw(struct vmsvga_state_s *s)
  505. {
  506. uint32_t cmd = s->fifo[CMD(stop) >> 2];
  507. s->cmd->stop = cpu_to_le32(CMD(stop) + 4);
  508. if (CMD(stop) >= CMD(max)) {
  509. s->cmd->stop = s->cmd->min;
  510. }
  511. return cmd;
  512. }
  513. static inline uint32_t vmsvga_fifo_read(struct vmsvga_state_s *s)
  514. {
  515. return le32_to_cpu(vmsvga_fifo_read_raw(s));
  516. }
  517. static void vmsvga_fifo_run(struct vmsvga_state_s *s)
  518. {
  519. uint32_t cmd, colour;
  520. int args, len;
  521. int x, y, dx, dy, width, height;
  522. struct vmsvga_cursor_definition_s cursor;
  523. uint32_t cmd_start;
  524. len = vmsvga_fifo_length(s);
  525. while (len > 0) {
  526. /* May need to go back to the start of the command if incomplete */
  527. cmd_start = s->cmd->stop;
  528. switch (cmd = vmsvga_fifo_read(s)) {
  529. case SVGA_CMD_UPDATE:
  530. case SVGA_CMD_UPDATE_VERBOSE:
  531. len -= 5;
  532. if (len < 0) {
  533. goto rewind;
  534. }
  535. x = vmsvga_fifo_read(s);
  536. y = vmsvga_fifo_read(s);
  537. width = vmsvga_fifo_read(s);
  538. height = vmsvga_fifo_read(s);
  539. vmsvga_update_rect_delayed(s, x, y, width, height);
  540. break;
  541. case SVGA_CMD_RECT_FILL:
  542. len -= 6;
  543. if (len < 0) {
  544. goto rewind;
  545. }
  546. colour = vmsvga_fifo_read(s);
  547. x = vmsvga_fifo_read(s);
  548. y = vmsvga_fifo_read(s);
  549. width = vmsvga_fifo_read(s);
  550. height = vmsvga_fifo_read(s);
  551. #ifdef HW_FILL_ACCEL
  552. if (vmsvga_fill_rect(s, colour, x, y, width, height) == 0) {
  553. break;
  554. }
  555. #endif
  556. args = 0;
  557. goto badcmd;
  558. case SVGA_CMD_RECT_COPY:
  559. len -= 7;
  560. if (len < 0) {
  561. goto rewind;
  562. }
  563. x = vmsvga_fifo_read(s);
  564. y = vmsvga_fifo_read(s);
  565. dx = vmsvga_fifo_read(s);
  566. dy = vmsvga_fifo_read(s);
  567. width = vmsvga_fifo_read(s);
  568. height = vmsvga_fifo_read(s);
  569. #ifdef HW_RECT_ACCEL
  570. if (vmsvga_copy_rect(s, x, y, dx, dy, width, height) == 0) {
  571. break;
  572. }
  573. #endif
  574. args = 0;
  575. goto badcmd;
  576. case SVGA_CMD_DEFINE_CURSOR:
  577. len -= 8;
  578. if (len < 0) {
  579. goto rewind;
  580. }
  581. cursor.id = vmsvga_fifo_read(s);
  582. cursor.hot_x = vmsvga_fifo_read(s);
  583. cursor.hot_y = vmsvga_fifo_read(s);
  584. cursor.width = x = vmsvga_fifo_read(s);
  585. cursor.height = y = vmsvga_fifo_read(s);
  586. vmsvga_fifo_read(s);
  587. cursor.bpp = vmsvga_fifo_read(s);
  588. args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
  589. if (SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
  590. SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
  591. goto badcmd;
  592. }
  593. len -= args;
  594. if (len < 0) {
  595. goto rewind;
  596. }
  597. for (args = 0; args < SVGA_BITMAP_SIZE(x, y); args++) {
  598. cursor.mask[args] = vmsvga_fifo_read_raw(s);
  599. }
  600. for (args = 0; args < SVGA_PIXMAP_SIZE(x, y, cursor.bpp); args++) {
  601. cursor.image[args] = vmsvga_fifo_read_raw(s);
  602. }
  603. #ifdef HW_MOUSE_ACCEL
  604. vmsvga_cursor_define(s, &cursor);
  605. break;
  606. #else
  607. args = 0;
  608. goto badcmd;
  609. #endif
  610. /*
  611. * Other commands that we at least know the number of arguments
  612. * for so we can avoid FIFO desync if driver uses them illegally.
  613. */
  614. case SVGA_CMD_DEFINE_ALPHA_CURSOR:
  615. len -= 6;
  616. if (len < 0) {
  617. goto rewind;
  618. }
  619. vmsvga_fifo_read(s);
  620. vmsvga_fifo_read(s);
  621. vmsvga_fifo_read(s);
  622. x = vmsvga_fifo_read(s);
  623. y = vmsvga_fifo_read(s);
  624. args = x * y;
  625. goto badcmd;
  626. case SVGA_CMD_RECT_ROP_FILL:
  627. args = 6;
  628. goto badcmd;
  629. case SVGA_CMD_RECT_ROP_COPY:
  630. args = 7;
  631. goto badcmd;
  632. case SVGA_CMD_DRAW_GLYPH_CLIPPED:
  633. len -= 4;
  634. if (len < 0) {
  635. goto rewind;
  636. }
  637. vmsvga_fifo_read(s);
  638. vmsvga_fifo_read(s);
  639. args = 7 + (vmsvga_fifo_read(s) >> 2);
  640. goto badcmd;
  641. case SVGA_CMD_SURFACE_ALPHA_BLEND:
  642. args = 12;
  643. goto badcmd;
  644. /*
  645. * Other commands that are not listed as depending on any
  646. * CAPABILITIES bits, but are not described in the README either.
  647. */
  648. case SVGA_CMD_SURFACE_FILL:
  649. case SVGA_CMD_SURFACE_COPY:
  650. case SVGA_CMD_FRONT_ROP_FILL:
  651. case SVGA_CMD_FENCE:
  652. case SVGA_CMD_INVALID_CMD:
  653. break; /* Nop */
  654. default:
  655. args = 0;
  656. badcmd:
  657. len -= args;
  658. if (len < 0) {
  659. goto rewind;
  660. }
  661. while (args--) {
  662. vmsvga_fifo_read(s);
  663. }
  664. printf("%s: Unknown command 0x%02x in SVGA command FIFO\n",
  665. __func__, cmd);
  666. break;
  667. rewind:
  668. s->cmd->stop = cmd_start;
  669. break;
  670. }
  671. }
  672. s->syncing = 0;
  673. }
  674. static uint32_t vmsvga_index_read(void *opaque, uint32_t address)
  675. {
  676. struct vmsvga_state_s *s = opaque;
  677. return s->index;
  678. }
  679. static void vmsvga_index_write(void *opaque, uint32_t address, uint32_t index)
  680. {
  681. struct vmsvga_state_s *s = opaque;
  682. s->index = index;
  683. }
  684. static uint32_t vmsvga_value_read(void *opaque, uint32_t address)
  685. {
  686. uint32_t caps;
  687. struct vmsvga_state_s *s = opaque;
  688. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  689. PixelFormat pf;
  690. uint32_t ret;
  691. switch (s->index) {
  692. case SVGA_REG_ID:
  693. ret = s->svgaid;
  694. break;
  695. case SVGA_REG_ENABLE:
  696. ret = s->enable;
  697. break;
  698. case SVGA_REG_WIDTH:
  699. ret = s->new_width ? s->new_width : surface_width(surface);
  700. break;
  701. case SVGA_REG_HEIGHT:
  702. ret = s->new_height ? s->new_height : surface_height(surface);
  703. break;
  704. case SVGA_REG_MAX_WIDTH:
  705. ret = SVGA_MAX_WIDTH;
  706. break;
  707. case SVGA_REG_MAX_HEIGHT:
  708. ret = SVGA_MAX_HEIGHT;
  709. break;
  710. case SVGA_REG_DEPTH:
  711. ret = (s->new_depth == 32) ? 24 : s->new_depth;
  712. break;
  713. case SVGA_REG_BITS_PER_PIXEL:
  714. case SVGA_REG_HOST_BITS_PER_PIXEL:
  715. ret = s->new_depth;
  716. break;
  717. case SVGA_REG_PSEUDOCOLOR:
  718. ret = 0x0;
  719. break;
  720. case SVGA_REG_RED_MASK:
  721. pf = qemu_default_pixelformat(s->new_depth);
  722. ret = pf.rmask;
  723. break;
  724. case SVGA_REG_GREEN_MASK:
  725. pf = qemu_default_pixelformat(s->new_depth);
  726. ret = pf.gmask;
  727. break;
  728. case SVGA_REG_BLUE_MASK:
  729. pf = qemu_default_pixelformat(s->new_depth);
  730. ret = pf.bmask;
  731. break;
  732. case SVGA_REG_BYTES_PER_LINE:
  733. if (s->new_width) {
  734. ret = (s->new_depth * s->new_width) / 8;
  735. } else {
  736. ret = surface_stride(surface);
  737. }
  738. break;
  739. case SVGA_REG_FB_START: {
  740. struct pci_vmsvga_state_s *pci_vmsvga
  741. = container_of(s, struct pci_vmsvga_state_s, chip);
  742. ret = pci_get_bar_addr(PCI_DEVICE(pci_vmsvga), 1);
  743. break;
  744. }
  745. case SVGA_REG_FB_OFFSET:
  746. ret = 0x0;
  747. break;
  748. case SVGA_REG_VRAM_SIZE:
  749. ret = s->vga.vram_size; /* No physical VRAM besides the framebuffer */
  750. break;
  751. case SVGA_REG_FB_SIZE:
  752. ret = s->vga.vram_size;
  753. break;
  754. case SVGA_REG_CAPABILITIES:
  755. caps = SVGA_CAP_NONE;
  756. #ifdef HW_RECT_ACCEL
  757. caps |= SVGA_CAP_RECT_COPY;
  758. #endif
  759. #ifdef HW_FILL_ACCEL
  760. caps |= SVGA_CAP_RECT_FILL;
  761. #endif
  762. #ifdef HW_MOUSE_ACCEL
  763. if (dpy_cursor_define_supported(s->vga.con)) {
  764. caps |= SVGA_CAP_CURSOR | SVGA_CAP_CURSOR_BYPASS_2 |
  765. SVGA_CAP_CURSOR_BYPASS;
  766. }
  767. #endif
  768. ret = caps;
  769. break;
  770. case SVGA_REG_MEM_START: {
  771. struct pci_vmsvga_state_s *pci_vmsvga
  772. = container_of(s, struct pci_vmsvga_state_s, chip);
  773. ret = pci_get_bar_addr(PCI_DEVICE(pci_vmsvga), 2);
  774. break;
  775. }
  776. case SVGA_REG_MEM_SIZE:
  777. ret = s->fifo_size;
  778. break;
  779. case SVGA_REG_CONFIG_DONE:
  780. ret = s->config;
  781. break;
  782. case SVGA_REG_SYNC:
  783. case SVGA_REG_BUSY:
  784. ret = s->syncing;
  785. break;
  786. case SVGA_REG_GUEST_ID:
  787. ret = s->guest;
  788. break;
  789. case SVGA_REG_CURSOR_ID:
  790. ret = s->cursor.id;
  791. break;
  792. case SVGA_REG_CURSOR_X:
  793. ret = s->cursor.x;
  794. break;
  795. case SVGA_REG_CURSOR_Y:
  796. ret = s->cursor.y;
  797. break;
  798. case SVGA_REG_CURSOR_ON:
  799. ret = s->cursor.on;
  800. break;
  801. case SVGA_REG_SCRATCH_SIZE:
  802. ret = s->scratch_size;
  803. break;
  804. case SVGA_REG_MEM_REGS:
  805. case SVGA_REG_NUM_DISPLAYS:
  806. case SVGA_REG_PITCHLOCK:
  807. case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
  808. ret = 0;
  809. break;
  810. default:
  811. if (s->index >= SVGA_SCRATCH_BASE &&
  812. s->index < SVGA_SCRATCH_BASE + s->scratch_size) {
  813. ret = s->scratch[s->index - SVGA_SCRATCH_BASE];
  814. break;
  815. }
  816. printf("%s: Bad register %02x\n", __func__, s->index);
  817. ret = 0;
  818. break;
  819. }
  820. if (s->index >= SVGA_SCRATCH_BASE) {
  821. trace_vmware_scratch_read(s->index, ret);
  822. } else if (s->index >= SVGA_PALETTE_BASE) {
  823. trace_vmware_palette_read(s->index, ret);
  824. } else {
  825. trace_vmware_value_read(s->index, ret);
  826. }
  827. return ret;
  828. }
  829. static void vmsvga_value_write(void *opaque, uint32_t address, uint32_t value)
  830. {
  831. struct vmsvga_state_s *s = opaque;
  832. if (s->index >= SVGA_SCRATCH_BASE) {
  833. trace_vmware_scratch_write(s->index, value);
  834. } else if (s->index >= SVGA_PALETTE_BASE) {
  835. trace_vmware_palette_write(s->index, value);
  836. } else {
  837. trace_vmware_value_write(s->index, value);
  838. }
  839. switch (s->index) {
  840. case SVGA_REG_ID:
  841. if (value == SVGA_ID_2 || value == SVGA_ID_1 || value == SVGA_ID_0) {
  842. s->svgaid = value;
  843. }
  844. break;
  845. case SVGA_REG_ENABLE:
  846. s->enable = !!value;
  847. s->invalidated = 1;
  848. s->vga.hw_ops->invalidate(&s->vga);
  849. if (s->enable && s->config) {
  850. vga_dirty_log_stop(&s->vga);
  851. } else {
  852. vga_dirty_log_start(&s->vga);
  853. }
  854. break;
  855. case SVGA_REG_WIDTH:
  856. if (value <= SVGA_MAX_WIDTH) {
  857. s->new_width = value;
  858. s->invalidated = 1;
  859. } else {
  860. printf("%s: Bad width: %i\n", __func__, value);
  861. }
  862. break;
  863. case SVGA_REG_HEIGHT:
  864. if (value <= SVGA_MAX_HEIGHT) {
  865. s->new_height = value;
  866. s->invalidated = 1;
  867. } else {
  868. printf("%s: Bad height: %i\n", __func__, value);
  869. }
  870. break;
  871. case SVGA_REG_BITS_PER_PIXEL:
  872. if (value != 32) {
  873. printf("%s: Bad bits per pixel: %i bits\n", __func__, value);
  874. s->config = 0;
  875. s->invalidated = 1;
  876. }
  877. break;
  878. case SVGA_REG_CONFIG_DONE:
  879. if (value) {
  880. s->fifo = (uint32_t *) s->fifo_ptr;
  881. /* Check range and alignment. */
  882. if ((CMD(min) | CMD(max) | CMD(next_cmd) | CMD(stop)) & 3) {
  883. break;
  884. }
  885. if (CMD(min) < (uint8_t *) s->cmd->fifo - (uint8_t *) s->fifo) {
  886. break;
  887. }
  888. if (CMD(max) > SVGA_FIFO_SIZE) {
  889. break;
  890. }
  891. if (CMD(max) < CMD(min) + 10 * 1024) {
  892. break;
  893. }
  894. vga_dirty_log_stop(&s->vga);
  895. }
  896. s->config = !!value;
  897. break;
  898. case SVGA_REG_SYNC:
  899. s->syncing = 1;
  900. vmsvga_fifo_run(s); /* Or should we just wait for update_display? */
  901. break;
  902. case SVGA_REG_GUEST_ID:
  903. s->guest = value;
  904. #ifdef VERBOSE
  905. if (value >= GUEST_OS_BASE && value < GUEST_OS_BASE +
  906. ARRAY_SIZE(vmsvga_guest_id)) {
  907. printf("%s: guest runs %s.\n", __func__,
  908. vmsvga_guest_id[value - GUEST_OS_BASE]);
  909. }
  910. #endif
  911. break;
  912. case SVGA_REG_CURSOR_ID:
  913. s->cursor.id = value;
  914. break;
  915. case SVGA_REG_CURSOR_X:
  916. s->cursor.x = value;
  917. break;
  918. case SVGA_REG_CURSOR_Y:
  919. s->cursor.y = value;
  920. break;
  921. case SVGA_REG_CURSOR_ON:
  922. s->cursor.on |= (value == SVGA_CURSOR_ON_SHOW);
  923. s->cursor.on &= (value != SVGA_CURSOR_ON_HIDE);
  924. #ifdef HW_MOUSE_ACCEL
  925. if (value <= SVGA_CURSOR_ON_SHOW) {
  926. dpy_mouse_set(s->vga.con, s->cursor.x, s->cursor.y, s->cursor.on);
  927. }
  928. #endif
  929. break;
  930. case SVGA_REG_DEPTH:
  931. case SVGA_REG_MEM_REGS:
  932. case SVGA_REG_NUM_DISPLAYS:
  933. case SVGA_REG_PITCHLOCK:
  934. case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
  935. break;
  936. default:
  937. if (s->index >= SVGA_SCRATCH_BASE &&
  938. s->index < SVGA_SCRATCH_BASE + s->scratch_size) {
  939. s->scratch[s->index - SVGA_SCRATCH_BASE] = value;
  940. break;
  941. }
  942. printf("%s: Bad register %02x\n", __func__, s->index);
  943. }
  944. }
  945. static uint32_t vmsvga_bios_read(void *opaque, uint32_t address)
  946. {
  947. printf("%s: what are we supposed to return?\n", __func__);
  948. return 0xcafe;
  949. }
  950. static void vmsvga_bios_write(void *opaque, uint32_t address, uint32_t data)
  951. {
  952. printf("%s: what are we supposed to do with (%08x)?\n", __func__, data);
  953. }
  954. static inline void vmsvga_check_size(struct vmsvga_state_s *s)
  955. {
  956. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  957. if (s->new_width != surface_width(surface) ||
  958. s->new_height != surface_height(surface) ||
  959. s->new_depth != surface_bits_per_pixel(surface)) {
  960. int stride = (s->new_depth * s->new_width) / 8;
  961. trace_vmware_setmode(s->new_width, s->new_height, s->new_depth);
  962. surface = qemu_create_displaysurface_from(s->new_width, s->new_height,
  963. s->new_depth, stride,
  964. s->vga.vram_ptr, false);
  965. dpy_gfx_replace_surface(s->vga.con, surface);
  966. s->invalidated = 1;
  967. }
  968. }
  969. static void vmsvga_update_display(void *opaque)
  970. {
  971. struct vmsvga_state_s *s = opaque;
  972. DisplaySurface *surface;
  973. bool dirty = false;
  974. if (!s->enable) {
  975. s->vga.hw_ops->gfx_update(&s->vga);
  976. return;
  977. }
  978. vmsvga_check_size(s);
  979. surface = qemu_console_surface(s->vga.con);
  980. vmsvga_fifo_run(s);
  981. vmsvga_update_rect_flush(s);
  982. /*
  983. * Is it more efficient to look at vram VGA-dirty bits or wait
  984. * for the driver to issue SVGA_CMD_UPDATE?
  985. */
  986. if (memory_region_is_logging(&s->vga.vram)) {
  987. vga_sync_dirty_bitmap(&s->vga);
  988. dirty = memory_region_get_dirty(&s->vga.vram, 0,
  989. surface_stride(surface) * surface_height(surface),
  990. DIRTY_MEMORY_VGA);
  991. }
  992. if (s->invalidated || dirty) {
  993. s->invalidated = 0;
  994. dpy_gfx_update(s->vga.con, 0, 0,
  995. surface_width(surface), surface_height(surface));
  996. }
  997. if (dirty) {
  998. memory_region_reset_dirty(&s->vga.vram, 0,
  999. surface_stride(surface) * surface_height(surface),
  1000. DIRTY_MEMORY_VGA);
  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),
  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. vmstate_register_ram_global(&s->fifo_ram);
  1092. s->fifo_ptr = memory_region_get_ram_ptr(&s->fifo_ram);
  1093. vga_common_init(&s->vga, OBJECT(dev), true);
  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 int pci_vmsvga_initfn(PCIDevice *dev)
  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, NULL, &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. if (!dev->rom_bar) {
  1154. /* compatibility with pc-0.13 and older */
  1155. vga_init_vbe(&s->chip.vga, OBJECT(dev), pci_address_space(dev));
  1156. }
  1157. return 0;
  1158. }
  1159. static Property vga_vmware_properties[] = {
  1160. DEFINE_PROP_UINT32("vgamem_mb", struct pci_vmsvga_state_s,
  1161. chip.vga.vram_size_mb, 16),
  1162. DEFINE_PROP_END_OF_LIST(),
  1163. };
  1164. static void vmsvga_class_init(ObjectClass *klass, void *data)
  1165. {
  1166. DeviceClass *dc = DEVICE_CLASS(klass);
  1167. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  1168. k->init = pci_vmsvga_initfn;
  1169. k->romfile = "vgabios-vmware.bin";
  1170. k->vendor_id = PCI_VENDOR_ID_VMWARE;
  1171. k->device_id = SVGA_PCI_DEVICE_ID;
  1172. k->class_id = PCI_CLASS_DISPLAY_VGA;
  1173. k->subsystem_vendor_id = PCI_VENDOR_ID_VMWARE;
  1174. k->subsystem_id = SVGA_PCI_DEVICE_ID;
  1175. dc->reset = vmsvga_reset;
  1176. dc->vmsd = &vmstate_vmware_vga;
  1177. dc->props = vga_vmware_properties;
  1178. dc->hotpluggable = false;
  1179. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  1180. }
  1181. static const TypeInfo vmsvga_info = {
  1182. .name = TYPE_VMWARE_SVGA,
  1183. .parent = TYPE_PCI_DEVICE,
  1184. .instance_size = sizeof(struct pci_vmsvga_state_s),
  1185. .class_init = vmsvga_class_init,
  1186. };
  1187. static void vmsvga_register_types(void)
  1188. {
  1189. type_register_static(&vmsvga_info);
  1190. }
  1191. type_init(vmsvga_register_types)