vmware_vga.c 34 KB

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