vmware_vga.c 36 KB

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