2
0

qxl.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354
  1. /*
  2. * Copyright (C) 2010 Red Hat, Inc.
  3. *
  4. * written by Yaniv Kamay, Izik Eidus, Gerd Hoffmann
  5. * maintained by Gerd Hoffmann <kraxel@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 or
  10. * (at your option) version 3 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <zlib.h>
  21. #include "qemu-common.h"
  22. #include "qemu/timer.h"
  23. #include "qemu/queue.h"
  24. #include "monitor/monitor.h"
  25. #include "sysemu/sysemu.h"
  26. #include "trace.h"
  27. #include "qxl.h"
  28. /*
  29. * NOTE: SPICE_RING_PROD_ITEM accesses memory on the pci bar and as
  30. * such can be changed by the guest, so to avoid a guest trigerrable
  31. * abort we just qxl_set_guest_bug and set the return to NULL. Still
  32. * it may happen as a result of emulator bug as well.
  33. */
  34. #undef SPICE_RING_PROD_ITEM
  35. #define SPICE_RING_PROD_ITEM(qxl, r, ret) { \
  36. uint32_t prod = (r)->prod & SPICE_RING_INDEX_MASK(r); \
  37. if (prod >= ARRAY_SIZE((r)->items)) { \
  38. qxl_set_guest_bug(qxl, "SPICE_RING_PROD_ITEM indices mismatch " \
  39. "%u >= %zu", prod, ARRAY_SIZE((r)->items)); \
  40. ret = NULL; \
  41. } else { \
  42. ret = &(r)->items[prod].el; \
  43. } \
  44. }
  45. #undef SPICE_RING_CONS_ITEM
  46. #define SPICE_RING_CONS_ITEM(qxl, r, ret) { \
  47. uint32_t cons = (r)->cons & SPICE_RING_INDEX_MASK(r); \
  48. if (cons >= ARRAY_SIZE((r)->items)) { \
  49. qxl_set_guest_bug(qxl, "SPICE_RING_CONS_ITEM indices mismatch " \
  50. "%u >= %zu", cons, ARRAY_SIZE((r)->items)); \
  51. ret = NULL; \
  52. } else { \
  53. ret = &(r)->items[cons].el; \
  54. } \
  55. }
  56. #undef ALIGN
  57. #define ALIGN(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
  58. #define PIXEL_SIZE 0.2936875 //1280x1024 is 14.8" x 11.9"
  59. #define QXL_MODE(_x, _y, _b, _o) \
  60. { .x_res = _x, \
  61. .y_res = _y, \
  62. .bits = _b, \
  63. .stride = (_x) * (_b) / 8, \
  64. .x_mili = PIXEL_SIZE * (_x), \
  65. .y_mili = PIXEL_SIZE * (_y), \
  66. .orientation = _o, \
  67. }
  68. #define QXL_MODE_16_32(x_res, y_res, orientation) \
  69. QXL_MODE(x_res, y_res, 16, orientation), \
  70. QXL_MODE(x_res, y_res, 32, orientation)
  71. #define QXL_MODE_EX(x_res, y_res) \
  72. QXL_MODE_16_32(x_res, y_res, 0), \
  73. QXL_MODE_16_32(x_res, y_res, 1)
  74. static QXLMode qxl_modes[] = {
  75. QXL_MODE_EX(640, 480),
  76. QXL_MODE_EX(800, 480),
  77. QXL_MODE_EX(800, 600),
  78. QXL_MODE_EX(832, 624),
  79. QXL_MODE_EX(960, 640),
  80. QXL_MODE_EX(1024, 600),
  81. QXL_MODE_EX(1024, 768),
  82. QXL_MODE_EX(1152, 864),
  83. QXL_MODE_EX(1152, 870),
  84. QXL_MODE_EX(1280, 720),
  85. QXL_MODE_EX(1280, 760),
  86. QXL_MODE_EX(1280, 768),
  87. QXL_MODE_EX(1280, 800),
  88. QXL_MODE_EX(1280, 960),
  89. QXL_MODE_EX(1280, 1024),
  90. QXL_MODE_EX(1360, 768),
  91. QXL_MODE_EX(1366, 768),
  92. QXL_MODE_EX(1400, 1050),
  93. QXL_MODE_EX(1440, 900),
  94. QXL_MODE_EX(1600, 900),
  95. QXL_MODE_EX(1600, 1200),
  96. QXL_MODE_EX(1680, 1050),
  97. QXL_MODE_EX(1920, 1080),
  98. /* these modes need more than 8 MB video memory */
  99. QXL_MODE_EX(1920, 1200),
  100. QXL_MODE_EX(1920, 1440),
  101. QXL_MODE_EX(2048, 1536),
  102. QXL_MODE_EX(2560, 1440),
  103. QXL_MODE_EX(2560, 1600),
  104. /* these modes need more than 16 MB video memory */
  105. QXL_MODE_EX(2560, 2048),
  106. QXL_MODE_EX(2800, 2100),
  107. QXL_MODE_EX(3200, 2400),
  108. };
  109. static PCIQXLDevice *qxl0;
  110. static void qxl_send_events(PCIQXLDevice *d, uint32_t events);
  111. static int qxl_destroy_primary(PCIQXLDevice *d, qxl_async_io async);
  112. static void qxl_reset_memslots(PCIQXLDevice *d);
  113. static void qxl_reset_surfaces(PCIQXLDevice *d);
  114. static void qxl_ring_set_dirty(PCIQXLDevice *qxl);
  115. void qxl_set_guest_bug(PCIQXLDevice *qxl, const char *msg, ...)
  116. {
  117. trace_qxl_set_guest_bug(qxl->id);
  118. qxl_send_events(qxl, QXL_INTERRUPT_ERROR);
  119. qxl->guest_bug = 1;
  120. if (qxl->guestdebug) {
  121. va_list ap;
  122. va_start(ap, msg);
  123. fprintf(stderr, "qxl-%d: guest bug: ", qxl->id);
  124. vfprintf(stderr, msg, ap);
  125. fprintf(stderr, "\n");
  126. va_end(ap);
  127. }
  128. }
  129. static void qxl_clear_guest_bug(PCIQXLDevice *qxl)
  130. {
  131. qxl->guest_bug = 0;
  132. }
  133. void qxl_spice_update_area(PCIQXLDevice *qxl, uint32_t surface_id,
  134. struct QXLRect *area, struct QXLRect *dirty_rects,
  135. uint32_t num_dirty_rects,
  136. uint32_t clear_dirty_region,
  137. qxl_async_io async, struct QXLCookie *cookie)
  138. {
  139. trace_qxl_spice_update_area(qxl->id, surface_id, area->left, area->right,
  140. area->top, area->bottom);
  141. trace_qxl_spice_update_area_rest(qxl->id, num_dirty_rects,
  142. clear_dirty_region);
  143. if (async == QXL_SYNC) {
  144. qxl->ssd.worker->update_area(qxl->ssd.worker, surface_id, area,
  145. dirty_rects, num_dirty_rects, clear_dirty_region);
  146. } else {
  147. assert(cookie != NULL);
  148. spice_qxl_update_area_async(&qxl->ssd.qxl, surface_id, area,
  149. clear_dirty_region, (uintptr_t)cookie);
  150. }
  151. }
  152. static void qxl_spice_destroy_surface_wait_complete(PCIQXLDevice *qxl,
  153. uint32_t id)
  154. {
  155. trace_qxl_spice_destroy_surface_wait_complete(qxl->id, id);
  156. qemu_mutex_lock(&qxl->track_lock);
  157. qxl->guest_surfaces.cmds[id] = 0;
  158. qxl->guest_surfaces.count--;
  159. qemu_mutex_unlock(&qxl->track_lock);
  160. }
  161. static void qxl_spice_destroy_surface_wait(PCIQXLDevice *qxl, uint32_t id,
  162. qxl_async_io async)
  163. {
  164. QXLCookie *cookie;
  165. trace_qxl_spice_destroy_surface_wait(qxl->id, id, async);
  166. if (async) {
  167. cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
  168. QXL_IO_DESTROY_SURFACE_ASYNC);
  169. cookie->u.surface_id = id;
  170. spice_qxl_destroy_surface_async(&qxl->ssd.qxl, id, (uintptr_t)cookie);
  171. } else {
  172. qxl->ssd.worker->destroy_surface_wait(qxl->ssd.worker, id);
  173. qxl_spice_destroy_surface_wait_complete(qxl, id);
  174. }
  175. }
  176. static void qxl_spice_flush_surfaces_async(PCIQXLDevice *qxl)
  177. {
  178. trace_qxl_spice_flush_surfaces_async(qxl->id, qxl->guest_surfaces.count,
  179. qxl->num_free_res);
  180. spice_qxl_flush_surfaces_async(&qxl->ssd.qxl,
  181. (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
  182. QXL_IO_FLUSH_SURFACES_ASYNC));
  183. }
  184. void qxl_spice_loadvm_commands(PCIQXLDevice *qxl, struct QXLCommandExt *ext,
  185. uint32_t count)
  186. {
  187. trace_qxl_spice_loadvm_commands(qxl->id, ext, count);
  188. qxl->ssd.worker->loadvm_commands(qxl->ssd.worker, ext, count);
  189. }
  190. void qxl_spice_oom(PCIQXLDevice *qxl)
  191. {
  192. trace_qxl_spice_oom(qxl->id);
  193. qxl->ssd.worker->oom(qxl->ssd.worker);
  194. }
  195. void qxl_spice_reset_memslots(PCIQXLDevice *qxl)
  196. {
  197. trace_qxl_spice_reset_memslots(qxl->id);
  198. qxl->ssd.worker->reset_memslots(qxl->ssd.worker);
  199. }
  200. static void qxl_spice_destroy_surfaces_complete(PCIQXLDevice *qxl)
  201. {
  202. trace_qxl_spice_destroy_surfaces_complete(qxl->id);
  203. qemu_mutex_lock(&qxl->track_lock);
  204. memset(qxl->guest_surfaces.cmds, 0,
  205. sizeof(qxl->guest_surfaces.cmds) * qxl->ssd.num_surfaces);
  206. qxl->guest_surfaces.count = 0;
  207. qemu_mutex_unlock(&qxl->track_lock);
  208. }
  209. static void qxl_spice_destroy_surfaces(PCIQXLDevice *qxl, qxl_async_io async)
  210. {
  211. trace_qxl_spice_destroy_surfaces(qxl->id, async);
  212. if (async) {
  213. spice_qxl_destroy_surfaces_async(&qxl->ssd.qxl,
  214. (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
  215. QXL_IO_DESTROY_ALL_SURFACES_ASYNC));
  216. } else {
  217. qxl->ssd.worker->destroy_surfaces(qxl->ssd.worker);
  218. qxl_spice_destroy_surfaces_complete(qxl);
  219. }
  220. }
  221. static void qxl_spice_monitors_config_async(PCIQXLDevice *qxl, int replay)
  222. {
  223. trace_qxl_spice_monitors_config(qxl->id);
  224. if (replay) {
  225. /*
  226. * don't use QXL_COOKIE_TYPE_IO:
  227. * - we are not running yet (post_load), we will assert
  228. * in send_events
  229. * - this is not a guest io, but a reply, so async_io isn't set.
  230. */
  231. spice_qxl_monitors_config_async(&qxl->ssd.qxl,
  232. qxl->guest_monitors_config,
  233. MEMSLOT_GROUP_GUEST,
  234. (uintptr_t)qxl_cookie_new(
  235. QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG,
  236. 0));
  237. } else {
  238. qxl->guest_monitors_config = qxl->ram->monitors_config;
  239. spice_qxl_monitors_config_async(&qxl->ssd.qxl,
  240. qxl->ram->monitors_config,
  241. MEMSLOT_GROUP_GUEST,
  242. (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
  243. QXL_IO_MONITORS_CONFIG_ASYNC));
  244. }
  245. }
  246. void qxl_spice_reset_image_cache(PCIQXLDevice *qxl)
  247. {
  248. trace_qxl_spice_reset_image_cache(qxl->id);
  249. qxl->ssd.worker->reset_image_cache(qxl->ssd.worker);
  250. }
  251. void qxl_spice_reset_cursor(PCIQXLDevice *qxl)
  252. {
  253. trace_qxl_spice_reset_cursor(qxl->id);
  254. qxl->ssd.worker->reset_cursor(qxl->ssd.worker);
  255. qemu_mutex_lock(&qxl->track_lock);
  256. qxl->guest_cursor = 0;
  257. qemu_mutex_unlock(&qxl->track_lock);
  258. if (qxl->ssd.cursor) {
  259. cursor_put(qxl->ssd.cursor);
  260. }
  261. qxl->ssd.cursor = cursor_builtin_hidden();
  262. }
  263. static inline uint32_t msb_mask(uint32_t val)
  264. {
  265. uint32_t mask;
  266. do {
  267. mask = ~(val - 1) & val;
  268. val &= ~mask;
  269. } while (mask < val);
  270. return mask;
  271. }
  272. static ram_addr_t qxl_rom_size(void)
  273. {
  274. uint32_t required_rom_size = sizeof(QXLRom) + sizeof(QXLModes) +
  275. sizeof(qxl_modes);
  276. uint32_t rom_size = 8192; /* two pages */
  277. required_rom_size = MAX(required_rom_size, TARGET_PAGE_SIZE);
  278. required_rom_size = msb_mask(required_rom_size * 2 - 1);
  279. assert(required_rom_size <= rom_size);
  280. return rom_size;
  281. }
  282. static void init_qxl_rom(PCIQXLDevice *d)
  283. {
  284. QXLRom *rom = memory_region_get_ram_ptr(&d->rom_bar);
  285. QXLModes *modes = (QXLModes *)(rom + 1);
  286. uint32_t ram_header_size;
  287. uint32_t surface0_area_size;
  288. uint32_t num_pages;
  289. uint32_t fb;
  290. int i, n;
  291. memset(rom, 0, d->rom_size);
  292. rom->magic = cpu_to_le32(QXL_ROM_MAGIC);
  293. rom->id = cpu_to_le32(d->id);
  294. rom->log_level = cpu_to_le32(d->guestdebug);
  295. rom->modes_offset = cpu_to_le32(sizeof(QXLRom));
  296. rom->slot_gen_bits = MEMSLOT_GENERATION_BITS;
  297. rom->slot_id_bits = MEMSLOT_SLOT_BITS;
  298. rom->slots_start = 1;
  299. rom->slots_end = NUM_MEMSLOTS - 1;
  300. rom->n_surfaces = cpu_to_le32(d->ssd.num_surfaces);
  301. for (i = 0, n = 0; i < ARRAY_SIZE(qxl_modes); i++) {
  302. fb = qxl_modes[i].y_res * qxl_modes[i].stride;
  303. if (fb > d->vgamem_size) {
  304. continue;
  305. }
  306. modes->modes[n].id = cpu_to_le32(i);
  307. modes->modes[n].x_res = cpu_to_le32(qxl_modes[i].x_res);
  308. modes->modes[n].y_res = cpu_to_le32(qxl_modes[i].y_res);
  309. modes->modes[n].bits = cpu_to_le32(qxl_modes[i].bits);
  310. modes->modes[n].stride = cpu_to_le32(qxl_modes[i].stride);
  311. modes->modes[n].x_mili = cpu_to_le32(qxl_modes[i].x_mili);
  312. modes->modes[n].y_mili = cpu_to_le32(qxl_modes[i].y_mili);
  313. modes->modes[n].orientation = cpu_to_le32(qxl_modes[i].orientation);
  314. n++;
  315. }
  316. modes->n_modes = cpu_to_le32(n);
  317. ram_header_size = ALIGN(sizeof(QXLRam), 4096);
  318. surface0_area_size = ALIGN(d->vgamem_size, 4096);
  319. num_pages = d->vga.vram_size;
  320. num_pages -= ram_header_size;
  321. num_pages -= surface0_area_size;
  322. num_pages = num_pages / TARGET_PAGE_SIZE;
  323. rom->draw_area_offset = cpu_to_le32(0);
  324. rom->surface0_area_size = cpu_to_le32(surface0_area_size);
  325. rom->pages_offset = cpu_to_le32(surface0_area_size);
  326. rom->num_pages = cpu_to_le32(num_pages);
  327. rom->ram_header_offset = cpu_to_le32(d->vga.vram_size - ram_header_size);
  328. d->shadow_rom = *rom;
  329. d->rom = rom;
  330. d->modes = modes;
  331. }
  332. static void init_qxl_ram(PCIQXLDevice *d)
  333. {
  334. uint8_t *buf;
  335. uint64_t *item;
  336. buf = d->vga.vram_ptr;
  337. d->ram = (QXLRam *)(buf + le32_to_cpu(d->shadow_rom.ram_header_offset));
  338. d->ram->magic = cpu_to_le32(QXL_RAM_MAGIC);
  339. d->ram->int_pending = cpu_to_le32(0);
  340. d->ram->int_mask = cpu_to_le32(0);
  341. d->ram->update_surface = 0;
  342. SPICE_RING_INIT(&d->ram->cmd_ring);
  343. SPICE_RING_INIT(&d->ram->cursor_ring);
  344. SPICE_RING_INIT(&d->ram->release_ring);
  345. SPICE_RING_PROD_ITEM(d, &d->ram->release_ring, item);
  346. assert(item);
  347. *item = 0;
  348. qxl_ring_set_dirty(d);
  349. }
  350. /* can be called from spice server thread context */
  351. static void qxl_set_dirty(MemoryRegion *mr, ram_addr_t addr, ram_addr_t end)
  352. {
  353. memory_region_set_dirty(mr, addr, end - addr);
  354. }
  355. static void qxl_rom_set_dirty(PCIQXLDevice *qxl)
  356. {
  357. qxl_set_dirty(&qxl->rom_bar, 0, qxl->rom_size);
  358. }
  359. /* called from spice server thread context only */
  360. static void qxl_ram_set_dirty(PCIQXLDevice *qxl, void *ptr)
  361. {
  362. void *base = qxl->vga.vram_ptr;
  363. intptr_t offset;
  364. offset = ptr - base;
  365. offset &= ~(TARGET_PAGE_SIZE-1);
  366. assert(offset < qxl->vga.vram_size);
  367. qxl_set_dirty(&qxl->vga.vram, offset, offset + TARGET_PAGE_SIZE);
  368. }
  369. /* can be called from spice server thread context */
  370. static void qxl_ring_set_dirty(PCIQXLDevice *qxl)
  371. {
  372. ram_addr_t addr = qxl->shadow_rom.ram_header_offset;
  373. ram_addr_t end = qxl->vga.vram_size;
  374. qxl_set_dirty(&qxl->vga.vram, addr, end);
  375. }
  376. /*
  377. * keep track of some command state, for savevm/loadvm.
  378. * called from spice server thread context only
  379. */
  380. static int qxl_track_command(PCIQXLDevice *qxl, struct QXLCommandExt *ext)
  381. {
  382. switch (le32_to_cpu(ext->cmd.type)) {
  383. case QXL_CMD_SURFACE:
  384. {
  385. QXLSurfaceCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
  386. if (!cmd) {
  387. return 1;
  388. }
  389. uint32_t id = le32_to_cpu(cmd->surface_id);
  390. if (id >= qxl->ssd.num_surfaces) {
  391. qxl_set_guest_bug(qxl, "QXL_CMD_SURFACE id %d >= %d", id,
  392. qxl->ssd.num_surfaces);
  393. return 1;
  394. }
  395. if (cmd->type == QXL_SURFACE_CMD_CREATE &&
  396. (cmd->u.surface_create.stride & 0x03) != 0) {
  397. qxl_set_guest_bug(qxl, "QXL_CMD_SURFACE stride = %d %% 4 != 0\n",
  398. cmd->u.surface_create.stride);
  399. return 1;
  400. }
  401. qemu_mutex_lock(&qxl->track_lock);
  402. if (cmd->type == QXL_SURFACE_CMD_CREATE) {
  403. qxl->guest_surfaces.cmds[id] = ext->cmd.data;
  404. qxl->guest_surfaces.count++;
  405. if (qxl->guest_surfaces.max < qxl->guest_surfaces.count)
  406. qxl->guest_surfaces.max = qxl->guest_surfaces.count;
  407. }
  408. if (cmd->type == QXL_SURFACE_CMD_DESTROY) {
  409. qxl->guest_surfaces.cmds[id] = 0;
  410. qxl->guest_surfaces.count--;
  411. }
  412. qemu_mutex_unlock(&qxl->track_lock);
  413. break;
  414. }
  415. case QXL_CMD_CURSOR:
  416. {
  417. QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
  418. if (!cmd) {
  419. return 1;
  420. }
  421. if (cmd->type == QXL_CURSOR_SET) {
  422. qemu_mutex_lock(&qxl->track_lock);
  423. qxl->guest_cursor = ext->cmd.data;
  424. qemu_mutex_unlock(&qxl->track_lock);
  425. }
  426. break;
  427. }
  428. }
  429. return 0;
  430. }
  431. /* spice display interface callbacks */
  432. static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
  433. {
  434. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  435. trace_qxl_interface_attach_worker(qxl->id);
  436. qxl->ssd.worker = qxl_worker;
  437. }
  438. static void interface_set_compression_level(QXLInstance *sin, int level)
  439. {
  440. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  441. trace_qxl_interface_set_compression_level(qxl->id, level);
  442. qxl->shadow_rom.compression_level = cpu_to_le32(level);
  443. qxl->rom->compression_level = cpu_to_le32(level);
  444. qxl_rom_set_dirty(qxl);
  445. }
  446. static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
  447. {
  448. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  449. trace_qxl_interface_set_mm_time(qxl->id, mm_time);
  450. qxl->shadow_rom.mm_clock = cpu_to_le32(mm_time);
  451. qxl->rom->mm_clock = cpu_to_le32(mm_time);
  452. qxl_rom_set_dirty(qxl);
  453. }
  454. static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
  455. {
  456. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  457. trace_qxl_interface_get_init_info(qxl->id);
  458. info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
  459. info->memslot_id_bits = MEMSLOT_SLOT_BITS;
  460. info->num_memslots = NUM_MEMSLOTS;
  461. info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
  462. info->internal_groupslot_id = 0;
  463. info->qxl_ram_size = le32_to_cpu(qxl->shadow_rom.num_pages) << TARGET_PAGE_BITS;
  464. info->n_surfaces = qxl->ssd.num_surfaces;
  465. }
  466. static const char *qxl_mode_to_string(int mode)
  467. {
  468. switch (mode) {
  469. case QXL_MODE_COMPAT:
  470. return "compat";
  471. case QXL_MODE_NATIVE:
  472. return "native";
  473. case QXL_MODE_UNDEFINED:
  474. return "undefined";
  475. case QXL_MODE_VGA:
  476. return "vga";
  477. }
  478. return "INVALID";
  479. }
  480. static const char *io_port_to_string(uint32_t io_port)
  481. {
  482. if (io_port >= QXL_IO_RANGE_SIZE) {
  483. return "out of range";
  484. }
  485. static const char *io_port_to_string[QXL_IO_RANGE_SIZE + 1] = {
  486. [QXL_IO_NOTIFY_CMD] = "QXL_IO_NOTIFY_CMD",
  487. [QXL_IO_NOTIFY_CURSOR] = "QXL_IO_NOTIFY_CURSOR",
  488. [QXL_IO_UPDATE_AREA] = "QXL_IO_UPDATE_AREA",
  489. [QXL_IO_UPDATE_IRQ] = "QXL_IO_UPDATE_IRQ",
  490. [QXL_IO_NOTIFY_OOM] = "QXL_IO_NOTIFY_OOM",
  491. [QXL_IO_RESET] = "QXL_IO_RESET",
  492. [QXL_IO_SET_MODE] = "QXL_IO_SET_MODE",
  493. [QXL_IO_LOG] = "QXL_IO_LOG",
  494. [QXL_IO_MEMSLOT_ADD] = "QXL_IO_MEMSLOT_ADD",
  495. [QXL_IO_MEMSLOT_DEL] = "QXL_IO_MEMSLOT_DEL",
  496. [QXL_IO_DETACH_PRIMARY] = "QXL_IO_DETACH_PRIMARY",
  497. [QXL_IO_ATTACH_PRIMARY] = "QXL_IO_ATTACH_PRIMARY",
  498. [QXL_IO_CREATE_PRIMARY] = "QXL_IO_CREATE_PRIMARY",
  499. [QXL_IO_DESTROY_PRIMARY] = "QXL_IO_DESTROY_PRIMARY",
  500. [QXL_IO_DESTROY_SURFACE_WAIT] = "QXL_IO_DESTROY_SURFACE_WAIT",
  501. [QXL_IO_DESTROY_ALL_SURFACES] = "QXL_IO_DESTROY_ALL_SURFACES",
  502. [QXL_IO_UPDATE_AREA_ASYNC] = "QXL_IO_UPDATE_AREA_ASYNC",
  503. [QXL_IO_MEMSLOT_ADD_ASYNC] = "QXL_IO_MEMSLOT_ADD_ASYNC",
  504. [QXL_IO_CREATE_PRIMARY_ASYNC] = "QXL_IO_CREATE_PRIMARY_ASYNC",
  505. [QXL_IO_DESTROY_PRIMARY_ASYNC] = "QXL_IO_DESTROY_PRIMARY_ASYNC",
  506. [QXL_IO_DESTROY_SURFACE_ASYNC] = "QXL_IO_DESTROY_SURFACE_ASYNC",
  507. [QXL_IO_DESTROY_ALL_SURFACES_ASYNC]
  508. = "QXL_IO_DESTROY_ALL_SURFACES_ASYNC",
  509. [QXL_IO_FLUSH_SURFACES_ASYNC] = "QXL_IO_FLUSH_SURFACES_ASYNC",
  510. [QXL_IO_FLUSH_RELEASE] = "QXL_IO_FLUSH_RELEASE",
  511. [QXL_IO_MONITORS_CONFIG_ASYNC] = "QXL_IO_MONITORS_CONFIG_ASYNC",
  512. };
  513. return io_port_to_string[io_port];
  514. }
  515. /* called from spice server thread context only */
  516. static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
  517. {
  518. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  519. SimpleSpiceUpdate *update;
  520. QXLCommandRing *ring;
  521. QXLCommand *cmd;
  522. int notify, ret;
  523. trace_qxl_ring_command_check(qxl->id, qxl_mode_to_string(qxl->mode));
  524. switch (qxl->mode) {
  525. case QXL_MODE_VGA:
  526. ret = false;
  527. qemu_mutex_lock(&qxl->ssd.lock);
  528. update = QTAILQ_FIRST(&qxl->ssd.updates);
  529. if (update != NULL) {
  530. QTAILQ_REMOVE(&qxl->ssd.updates, update, next);
  531. *ext = update->ext;
  532. ret = true;
  533. }
  534. qemu_mutex_unlock(&qxl->ssd.lock);
  535. if (ret) {
  536. trace_qxl_ring_command_get(qxl->id, qxl_mode_to_string(qxl->mode));
  537. qxl_log_command(qxl, "vga", ext);
  538. }
  539. return ret;
  540. case QXL_MODE_COMPAT:
  541. case QXL_MODE_NATIVE:
  542. case QXL_MODE_UNDEFINED:
  543. ring = &qxl->ram->cmd_ring;
  544. if (qxl->guest_bug || SPICE_RING_IS_EMPTY(ring)) {
  545. return false;
  546. }
  547. SPICE_RING_CONS_ITEM(qxl, ring, cmd);
  548. if (!cmd) {
  549. return false;
  550. }
  551. ext->cmd = *cmd;
  552. ext->group_id = MEMSLOT_GROUP_GUEST;
  553. ext->flags = qxl->cmdflags;
  554. SPICE_RING_POP(ring, notify);
  555. qxl_ring_set_dirty(qxl);
  556. if (notify) {
  557. qxl_send_events(qxl, QXL_INTERRUPT_DISPLAY);
  558. }
  559. qxl->guest_primary.commands++;
  560. qxl_track_command(qxl, ext);
  561. qxl_log_command(qxl, "cmd", ext);
  562. trace_qxl_ring_command_get(qxl->id, qxl_mode_to_string(qxl->mode));
  563. return true;
  564. default:
  565. return false;
  566. }
  567. }
  568. /* called from spice server thread context only */
  569. static int interface_req_cmd_notification(QXLInstance *sin)
  570. {
  571. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  572. int wait = 1;
  573. trace_qxl_ring_command_req_notification(qxl->id);
  574. switch (qxl->mode) {
  575. case QXL_MODE_COMPAT:
  576. case QXL_MODE_NATIVE:
  577. case QXL_MODE_UNDEFINED:
  578. SPICE_RING_CONS_WAIT(&qxl->ram->cmd_ring, wait);
  579. qxl_ring_set_dirty(qxl);
  580. break;
  581. default:
  582. /* nothing */
  583. break;
  584. }
  585. return wait;
  586. }
  587. /* called from spice server thread context only */
  588. static inline void qxl_push_free_res(PCIQXLDevice *d, int flush)
  589. {
  590. QXLReleaseRing *ring = &d->ram->release_ring;
  591. uint64_t *item;
  592. int notify;
  593. #define QXL_FREE_BUNCH_SIZE 32
  594. if (ring->prod - ring->cons + 1 == ring->num_items) {
  595. /* ring full -- can't push */
  596. return;
  597. }
  598. if (!flush && d->oom_running) {
  599. /* collect everything from oom handler before pushing */
  600. return;
  601. }
  602. if (!flush && d->num_free_res < QXL_FREE_BUNCH_SIZE) {
  603. /* collect a bit more before pushing */
  604. return;
  605. }
  606. SPICE_RING_PUSH(ring, notify);
  607. trace_qxl_ring_res_push(d->id, qxl_mode_to_string(d->mode),
  608. d->guest_surfaces.count, d->num_free_res,
  609. d->last_release, notify ? "yes" : "no");
  610. trace_qxl_ring_res_push_rest(d->id, ring->prod - ring->cons,
  611. ring->num_items, ring->prod, ring->cons);
  612. if (notify) {
  613. qxl_send_events(d, QXL_INTERRUPT_DISPLAY);
  614. }
  615. SPICE_RING_PROD_ITEM(d, ring, item);
  616. if (!item) {
  617. return;
  618. }
  619. *item = 0;
  620. d->num_free_res = 0;
  621. d->last_release = NULL;
  622. qxl_ring_set_dirty(d);
  623. }
  624. /* called from spice server thread context only */
  625. static void interface_release_resource(QXLInstance *sin,
  626. struct QXLReleaseInfoExt ext)
  627. {
  628. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  629. QXLReleaseRing *ring;
  630. uint64_t *item, id;
  631. if (ext.group_id == MEMSLOT_GROUP_HOST) {
  632. /* host group -> vga mode update request */
  633. qemu_spice_destroy_update(&qxl->ssd, (void *)(intptr_t)ext.info->id);
  634. return;
  635. }
  636. /*
  637. * ext->info points into guest-visible memory
  638. * pci bar 0, $command.release_info
  639. */
  640. ring = &qxl->ram->release_ring;
  641. SPICE_RING_PROD_ITEM(qxl, ring, item);
  642. if (!item) {
  643. return;
  644. }
  645. if (*item == 0) {
  646. /* stick head into the ring */
  647. id = ext.info->id;
  648. ext.info->next = 0;
  649. qxl_ram_set_dirty(qxl, &ext.info->next);
  650. *item = id;
  651. qxl_ring_set_dirty(qxl);
  652. } else {
  653. /* append item to the list */
  654. qxl->last_release->next = ext.info->id;
  655. qxl_ram_set_dirty(qxl, &qxl->last_release->next);
  656. ext.info->next = 0;
  657. qxl_ram_set_dirty(qxl, &ext.info->next);
  658. }
  659. qxl->last_release = ext.info;
  660. qxl->num_free_res++;
  661. trace_qxl_ring_res_put(qxl->id, qxl->num_free_res);
  662. qxl_push_free_res(qxl, 0);
  663. }
  664. /* called from spice server thread context only */
  665. static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
  666. {
  667. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  668. QXLCursorRing *ring;
  669. QXLCommand *cmd;
  670. int notify;
  671. trace_qxl_ring_cursor_check(qxl->id, qxl_mode_to_string(qxl->mode));
  672. switch (qxl->mode) {
  673. case QXL_MODE_COMPAT:
  674. case QXL_MODE_NATIVE:
  675. case QXL_MODE_UNDEFINED:
  676. ring = &qxl->ram->cursor_ring;
  677. if (SPICE_RING_IS_EMPTY(ring)) {
  678. return false;
  679. }
  680. SPICE_RING_CONS_ITEM(qxl, ring, cmd);
  681. if (!cmd) {
  682. return false;
  683. }
  684. ext->cmd = *cmd;
  685. ext->group_id = MEMSLOT_GROUP_GUEST;
  686. ext->flags = qxl->cmdflags;
  687. SPICE_RING_POP(ring, notify);
  688. qxl_ring_set_dirty(qxl);
  689. if (notify) {
  690. qxl_send_events(qxl, QXL_INTERRUPT_CURSOR);
  691. }
  692. qxl->guest_primary.commands++;
  693. qxl_track_command(qxl, ext);
  694. qxl_log_command(qxl, "csr", ext);
  695. if (qxl->id == 0) {
  696. qxl_render_cursor(qxl, ext);
  697. }
  698. trace_qxl_ring_cursor_get(qxl->id, qxl_mode_to_string(qxl->mode));
  699. return true;
  700. default:
  701. return false;
  702. }
  703. }
  704. /* called from spice server thread context only */
  705. static int interface_req_cursor_notification(QXLInstance *sin)
  706. {
  707. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  708. int wait = 1;
  709. trace_qxl_ring_cursor_req_notification(qxl->id);
  710. switch (qxl->mode) {
  711. case QXL_MODE_COMPAT:
  712. case QXL_MODE_NATIVE:
  713. case QXL_MODE_UNDEFINED:
  714. SPICE_RING_CONS_WAIT(&qxl->ram->cursor_ring, wait);
  715. qxl_ring_set_dirty(qxl);
  716. break;
  717. default:
  718. /* nothing */
  719. break;
  720. }
  721. return wait;
  722. }
  723. /* called from spice server thread context */
  724. static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
  725. {
  726. /*
  727. * Called by spice-server as a result of a QXL_CMD_UPDATE which is not in
  728. * use by xf86-video-qxl and is defined out in the qxl windows driver.
  729. * Probably was at some earlier version that is prior to git start (2009),
  730. * and is still guest trigerrable.
  731. */
  732. fprintf(stderr, "%s: deprecated\n", __func__);
  733. }
  734. /* called from spice server thread context only */
  735. static int interface_flush_resources(QXLInstance *sin)
  736. {
  737. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  738. int ret;
  739. ret = qxl->num_free_res;
  740. if (ret) {
  741. qxl_push_free_res(qxl, 1);
  742. }
  743. return ret;
  744. }
  745. static void qxl_create_guest_primary_complete(PCIQXLDevice *d);
  746. /* called from spice server thread context only */
  747. static void interface_async_complete_io(PCIQXLDevice *qxl, QXLCookie *cookie)
  748. {
  749. uint32_t current_async;
  750. qemu_mutex_lock(&qxl->async_lock);
  751. current_async = qxl->current_async;
  752. qxl->current_async = QXL_UNDEFINED_IO;
  753. qemu_mutex_unlock(&qxl->async_lock);
  754. trace_qxl_interface_async_complete_io(qxl->id, current_async, cookie);
  755. if (!cookie) {
  756. fprintf(stderr, "qxl: %s: error, cookie is NULL\n", __func__);
  757. return;
  758. }
  759. if (cookie && current_async != cookie->io) {
  760. fprintf(stderr,
  761. "qxl: %s: error: current_async = %d != %"
  762. PRId64 " = cookie->io\n", __func__, current_async, cookie->io);
  763. }
  764. switch (current_async) {
  765. case QXL_IO_MEMSLOT_ADD_ASYNC:
  766. case QXL_IO_DESTROY_PRIMARY_ASYNC:
  767. case QXL_IO_UPDATE_AREA_ASYNC:
  768. case QXL_IO_FLUSH_SURFACES_ASYNC:
  769. case QXL_IO_MONITORS_CONFIG_ASYNC:
  770. break;
  771. case QXL_IO_CREATE_PRIMARY_ASYNC:
  772. qxl_create_guest_primary_complete(qxl);
  773. break;
  774. case QXL_IO_DESTROY_ALL_SURFACES_ASYNC:
  775. qxl_spice_destroy_surfaces_complete(qxl);
  776. break;
  777. case QXL_IO_DESTROY_SURFACE_ASYNC:
  778. qxl_spice_destroy_surface_wait_complete(qxl, cookie->u.surface_id);
  779. break;
  780. default:
  781. fprintf(stderr, "qxl: %s: unexpected current_async %d\n", __func__,
  782. current_async);
  783. }
  784. qxl_send_events(qxl, QXL_INTERRUPT_IO_CMD);
  785. }
  786. /* called from spice server thread context only */
  787. static void interface_update_area_complete(QXLInstance *sin,
  788. uint32_t surface_id,
  789. QXLRect *dirty, uint32_t num_updated_rects)
  790. {
  791. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  792. int i;
  793. int qxl_i;
  794. qemu_mutex_lock(&qxl->ssd.lock);
  795. if (surface_id != 0 || !qxl->render_update_cookie_num) {
  796. qemu_mutex_unlock(&qxl->ssd.lock);
  797. return;
  798. }
  799. trace_qxl_interface_update_area_complete(qxl->id, surface_id, dirty->left,
  800. dirty->right, dirty->top, dirty->bottom);
  801. trace_qxl_interface_update_area_complete_rest(qxl->id, num_updated_rects);
  802. if (qxl->num_dirty_rects + num_updated_rects > QXL_NUM_DIRTY_RECTS) {
  803. /*
  804. * overflow - treat this as a full update. Not expected to be common.
  805. */
  806. trace_qxl_interface_update_area_complete_overflow(qxl->id,
  807. QXL_NUM_DIRTY_RECTS);
  808. qxl->guest_primary.resized = 1;
  809. }
  810. if (qxl->guest_primary.resized) {
  811. /*
  812. * Don't bother copying or scheduling the bh since we will flip
  813. * the whole area anyway on completion of the update_area async call
  814. */
  815. qemu_mutex_unlock(&qxl->ssd.lock);
  816. return;
  817. }
  818. qxl_i = qxl->num_dirty_rects;
  819. for (i = 0; i < num_updated_rects; i++) {
  820. qxl->dirty[qxl_i++] = dirty[i];
  821. }
  822. qxl->num_dirty_rects += num_updated_rects;
  823. trace_qxl_interface_update_area_complete_schedule_bh(qxl->id,
  824. qxl->num_dirty_rects);
  825. qemu_bh_schedule(qxl->update_area_bh);
  826. qemu_mutex_unlock(&qxl->ssd.lock);
  827. }
  828. /* called from spice server thread context only */
  829. static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
  830. {
  831. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  832. QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
  833. switch (cookie->type) {
  834. case QXL_COOKIE_TYPE_IO:
  835. interface_async_complete_io(qxl, cookie);
  836. g_free(cookie);
  837. break;
  838. case QXL_COOKIE_TYPE_RENDER_UPDATE_AREA:
  839. qxl_render_update_area_done(qxl, cookie);
  840. break;
  841. case QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG:
  842. break;
  843. default:
  844. fprintf(stderr, "qxl: %s: unexpected cookie type %d\n",
  845. __func__, cookie->type);
  846. g_free(cookie);
  847. }
  848. }
  849. /* called from spice server thread context only */
  850. static void interface_set_client_capabilities(QXLInstance *sin,
  851. uint8_t client_present,
  852. uint8_t caps[58])
  853. {
  854. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  855. if (qxl->revision < 4) {
  856. trace_qxl_set_client_capabilities_unsupported_by_revision(qxl->id,
  857. qxl->revision);
  858. return;
  859. }
  860. if (runstate_check(RUN_STATE_INMIGRATE) ||
  861. runstate_check(RUN_STATE_POSTMIGRATE)) {
  862. return;
  863. }
  864. qxl->shadow_rom.client_present = client_present;
  865. memcpy(qxl->shadow_rom.client_capabilities, caps,
  866. sizeof(qxl->shadow_rom.client_capabilities));
  867. qxl->rom->client_present = client_present;
  868. memcpy(qxl->rom->client_capabilities, caps,
  869. sizeof(qxl->rom->client_capabilities));
  870. qxl_rom_set_dirty(qxl);
  871. qxl_send_events(qxl, QXL_INTERRUPT_CLIENT);
  872. }
  873. static uint32_t qxl_crc32(const uint8_t *p, unsigned len)
  874. {
  875. /*
  876. * zlib xors the seed with 0xffffffff, and xors the result
  877. * again with 0xffffffff; Both are not done with linux's crc32,
  878. * which we want to be compatible with, so undo that.
  879. */
  880. return crc32(0xffffffff, p, len) ^ 0xffffffff;
  881. }
  882. /* called from main context only */
  883. static int interface_client_monitors_config(QXLInstance *sin,
  884. VDAgentMonitorsConfig *monitors_config)
  885. {
  886. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  887. QXLRom *rom = memory_region_get_ram_ptr(&qxl->rom_bar);
  888. int i;
  889. if (qxl->revision < 4) {
  890. trace_qxl_client_monitors_config_unsupported_by_device(qxl->id,
  891. qxl->revision);
  892. return 0;
  893. }
  894. /*
  895. * Older windows drivers set int_mask to 0 when their ISR is called,
  896. * then later set it to ~0. So it doesn't relate to the actual interrupts
  897. * handled. However, they are old, so clearly they don't support this
  898. * interrupt
  899. */
  900. if (qxl->ram->int_mask == 0 || qxl->ram->int_mask == ~0 ||
  901. !(qxl->ram->int_mask & QXL_INTERRUPT_CLIENT_MONITORS_CONFIG)) {
  902. trace_qxl_client_monitors_config_unsupported_by_guest(qxl->id,
  903. qxl->ram->int_mask,
  904. monitors_config);
  905. return 0;
  906. }
  907. if (!monitors_config) {
  908. return 1;
  909. }
  910. memset(&rom->client_monitors_config, 0,
  911. sizeof(rom->client_monitors_config));
  912. rom->client_monitors_config.count = monitors_config->num_of_monitors;
  913. /* monitors_config->flags ignored */
  914. if (rom->client_monitors_config.count >=
  915. ARRAY_SIZE(rom->client_monitors_config.heads)) {
  916. trace_qxl_client_monitors_config_capped(qxl->id,
  917. monitors_config->num_of_monitors,
  918. ARRAY_SIZE(rom->client_monitors_config.heads));
  919. rom->client_monitors_config.count =
  920. ARRAY_SIZE(rom->client_monitors_config.heads);
  921. }
  922. for (i = 0 ; i < rom->client_monitors_config.count ; ++i) {
  923. VDAgentMonConfig *monitor = &monitors_config->monitors[i];
  924. QXLURect *rect = &rom->client_monitors_config.heads[i];
  925. /* monitor->depth ignored */
  926. rect->left = monitor->x;
  927. rect->top = monitor->y;
  928. rect->right = monitor->x + monitor->width;
  929. rect->bottom = monitor->y + monitor->height;
  930. }
  931. rom->client_monitors_config_crc = qxl_crc32(
  932. (const uint8_t *)&rom->client_monitors_config,
  933. sizeof(rom->client_monitors_config));
  934. trace_qxl_client_monitors_config_crc(qxl->id,
  935. sizeof(rom->client_monitors_config),
  936. rom->client_monitors_config_crc);
  937. trace_qxl_interrupt_client_monitors_config(qxl->id,
  938. rom->client_monitors_config.count,
  939. rom->client_monitors_config.heads);
  940. qxl_send_events(qxl, QXL_INTERRUPT_CLIENT_MONITORS_CONFIG);
  941. return 1;
  942. }
  943. static const QXLInterface qxl_interface = {
  944. .base.type = SPICE_INTERFACE_QXL,
  945. .base.description = "qxl gpu",
  946. .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
  947. .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
  948. .attache_worker = interface_attach_worker,
  949. .set_compression_level = interface_set_compression_level,
  950. .set_mm_time = interface_set_mm_time,
  951. .get_init_info = interface_get_init_info,
  952. /* the callbacks below are called from spice server thread context */
  953. .get_command = interface_get_command,
  954. .req_cmd_notification = interface_req_cmd_notification,
  955. .release_resource = interface_release_resource,
  956. .get_cursor_command = interface_get_cursor_command,
  957. .req_cursor_notification = interface_req_cursor_notification,
  958. .notify_update = interface_notify_update,
  959. .flush_resources = interface_flush_resources,
  960. .async_complete = interface_async_complete,
  961. .update_area_complete = interface_update_area_complete,
  962. .set_client_capabilities = interface_set_client_capabilities,
  963. .client_monitors_config = interface_client_monitors_config,
  964. };
  965. static void qxl_enter_vga_mode(PCIQXLDevice *d)
  966. {
  967. if (d->mode == QXL_MODE_VGA) {
  968. return;
  969. }
  970. trace_qxl_enter_vga_mode(d->id);
  971. qemu_spice_create_host_primary(&d->ssd);
  972. d->mode = QXL_MODE_VGA;
  973. vga_dirty_log_start(&d->vga);
  974. vga_hw_update();
  975. }
  976. static void qxl_exit_vga_mode(PCIQXLDevice *d)
  977. {
  978. if (d->mode != QXL_MODE_VGA) {
  979. return;
  980. }
  981. trace_qxl_exit_vga_mode(d->id);
  982. vga_dirty_log_stop(&d->vga);
  983. qxl_destroy_primary(d, QXL_SYNC);
  984. }
  985. static void qxl_update_irq(PCIQXLDevice *d)
  986. {
  987. uint32_t pending = le32_to_cpu(d->ram->int_pending);
  988. uint32_t mask = le32_to_cpu(d->ram->int_mask);
  989. int level = !!(pending & mask);
  990. qemu_set_irq(d->pci.irq[0], level);
  991. qxl_ring_set_dirty(d);
  992. }
  993. static void qxl_check_state(PCIQXLDevice *d)
  994. {
  995. QXLRam *ram = d->ram;
  996. int spice_display_running = qemu_spice_display_is_running(&d->ssd);
  997. assert(!spice_display_running || SPICE_RING_IS_EMPTY(&ram->cmd_ring));
  998. assert(!spice_display_running || SPICE_RING_IS_EMPTY(&ram->cursor_ring));
  999. }
  1000. static void qxl_reset_state(PCIQXLDevice *d)
  1001. {
  1002. QXLRom *rom = d->rom;
  1003. qxl_check_state(d);
  1004. d->shadow_rom.update_id = cpu_to_le32(0);
  1005. *rom = d->shadow_rom;
  1006. qxl_rom_set_dirty(d);
  1007. init_qxl_ram(d);
  1008. d->num_free_res = 0;
  1009. d->last_release = NULL;
  1010. memset(&d->ssd.dirty, 0, sizeof(d->ssd.dirty));
  1011. }
  1012. static void qxl_soft_reset(PCIQXLDevice *d)
  1013. {
  1014. trace_qxl_soft_reset(d->id);
  1015. qxl_check_state(d);
  1016. qxl_clear_guest_bug(d);
  1017. d->current_async = QXL_UNDEFINED_IO;
  1018. if (d->id == 0) {
  1019. qxl_enter_vga_mode(d);
  1020. } else {
  1021. d->mode = QXL_MODE_UNDEFINED;
  1022. }
  1023. }
  1024. static void qxl_hard_reset(PCIQXLDevice *d, int loadvm)
  1025. {
  1026. trace_qxl_hard_reset(d->id, loadvm);
  1027. qxl_spice_reset_cursor(d);
  1028. qxl_spice_reset_image_cache(d);
  1029. qxl_reset_surfaces(d);
  1030. qxl_reset_memslots(d);
  1031. /* pre loadvm reset must not touch QXLRam. This lives in
  1032. * device memory, is migrated together with RAM and thus
  1033. * already loaded at this point */
  1034. if (!loadvm) {
  1035. qxl_reset_state(d);
  1036. }
  1037. qemu_spice_create_host_memslot(&d->ssd);
  1038. qxl_soft_reset(d);
  1039. }
  1040. static void qxl_reset_handler(DeviceState *dev)
  1041. {
  1042. PCIQXLDevice *d = DO_UPCAST(PCIQXLDevice, pci.qdev, dev);
  1043. qxl_hard_reset(d, 0);
  1044. }
  1045. static void qxl_vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
  1046. {
  1047. VGACommonState *vga = opaque;
  1048. PCIQXLDevice *qxl = container_of(vga, PCIQXLDevice, vga);
  1049. trace_qxl_io_write_vga(qxl->id, qxl_mode_to_string(qxl->mode), addr, val);
  1050. if (qxl->mode != QXL_MODE_VGA) {
  1051. qxl_destroy_primary(qxl, QXL_SYNC);
  1052. qxl_soft_reset(qxl);
  1053. }
  1054. vga_ioport_write(opaque, addr, val);
  1055. }
  1056. static const MemoryRegionPortio qxl_vga_portio_list[] = {
  1057. { 0x04, 2, 1, .read = vga_ioport_read,
  1058. .write = qxl_vga_ioport_write }, /* 3b4 */
  1059. { 0x0a, 1, 1, .read = vga_ioport_read,
  1060. .write = qxl_vga_ioport_write }, /* 3ba */
  1061. { 0x10, 16, 1, .read = vga_ioport_read,
  1062. .write = qxl_vga_ioport_write }, /* 3c0 */
  1063. { 0x24, 2, 1, .read = vga_ioport_read,
  1064. .write = qxl_vga_ioport_write }, /* 3d4 */
  1065. { 0x2a, 1, 1, .read = vga_ioport_read,
  1066. .write = qxl_vga_ioport_write }, /* 3da */
  1067. PORTIO_END_OF_LIST(),
  1068. };
  1069. static int qxl_add_memslot(PCIQXLDevice *d, uint32_t slot_id, uint64_t delta,
  1070. qxl_async_io async)
  1071. {
  1072. static const int regions[] = {
  1073. QXL_RAM_RANGE_INDEX,
  1074. QXL_VRAM_RANGE_INDEX,
  1075. QXL_VRAM64_RANGE_INDEX,
  1076. };
  1077. uint64_t guest_start;
  1078. uint64_t guest_end;
  1079. int pci_region;
  1080. pcibus_t pci_start;
  1081. pcibus_t pci_end;
  1082. intptr_t virt_start;
  1083. QXLDevMemSlot memslot;
  1084. int i;
  1085. guest_start = le64_to_cpu(d->guest_slots[slot_id].slot.mem_start);
  1086. guest_end = le64_to_cpu(d->guest_slots[slot_id].slot.mem_end);
  1087. trace_qxl_memslot_add_guest(d->id, slot_id, guest_start, guest_end);
  1088. if (slot_id >= NUM_MEMSLOTS) {
  1089. qxl_set_guest_bug(d, "%s: slot_id >= NUM_MEMSLOTS %d >= %d", __func__,
  1090. slot_id, NUM_MEMSLOTS);
  1091. return 1;
  1092. }
  1093. if (guest_start > guest_end) {
  1094. qxl_set_guest_bug(d, "%s: guest_start > guest_end 0x%" PRIx64
  1095. " > 0x%" PRIx64, __func__, guest_start, guest_end);
  1096. return 1;
  1097. }
  1098. for (i = 0; i < ARRAY_SIZE(regions); i++) {
  1099. pci_region = regions[i];
  1100. pci_start = d->pci.io_regions[pci_region].addr;
  1101. pci_end = pci_start + d->pci.io_regions[pci_region].size;
  1102. /* mapped? */
  1103. if (pci_start == -1) {
  1104. continue;
  1105. }
  1106. /* start address in range ? */
  1107. if (guest_start < pci_start || guest_start > pci_end) {
  1108. continue;
  1109. }
  1110. /* end address in range ? */
  1111. if (guest_end > pci_end) {
  1112. continue;
  1113. }
  1114. /* passed */
  1115. break;
  1116. }
  1117. if (i == ARRAY_SIZE(regions)) {
  1118. qxl_set_guest_bug(d, "%s: finished loop without match", __func__);
  1119. return 1;
  1120. }
  1121. switch (pci_region) {
  1122. case QXL_RAM_RANGE_INDEX:
  1123. virt_start = (intptr_t)memory_region_get_ram_ptr(&d->vga.vram);
  1124. break;
  1125. case QXL_VRAM_RANGE_INDEX:
  1126. case 4 /* vram 64bit */:
  1127. virt_start = (intptr_t)memory_region_get_ram_ptr(&d->vram_bar);
  1128. break;
  1129. default:
  1130. /* should not happen */
  1131. qxl_set_guest_bug(d, "%s: pci_region = %d", __func__, pci_region);
  1132. return 1;
  1133. }
  1134. memslot.slot_id = slot_id;
  1135. memslot.slot_group_id = MEMSLOT_GROUP_GUEST; /* guest group */
  1136. memslot.virt_start = virt_start + (guest_start - pci_start);
  1137. memslot.virt_end = virt_start + (guest_end - pci_start);
  1138. memslot.addr_delta = memslot.virt_start - delta;
  1139. memslot.generation = d->rom->slot_generation = 0;
  1140. qxl_rom_set_dirty(d);
  1141. qemu_spice_add_memslot(&d->ssd, &memslot, async);
  1142. d->guest_slots[slot_id].ptr = (void*)memslot.virt_start;
  1143. d->guest_slots[slot_id].size = memslot.virt_end - memslot.virt_start;
  1144. d->guest_slots[slot_id].delta = delta;
  1145. d->guest_slots[slot_id].active = 1;
  1146. return 0;
  1147. }
  1148. static void qxl_del_memslot(PCIQXLDevice *d, uint32_t slot_id)
  1149. {
  1150. qemu_spice_del_memslot(&d->ssd, MEMSLOT_GROUP_HOST, slot_id);
  1151. d->guest_slots[slot_id].active = 0;
  1152. }
  1153. static void qxl_reset_memslots(PCIQXLDevice *d)
  1154. {
  1155. qxl_spice_reset_memslots(d);
  1156. memset(&d->guest_slots, 0, sizeof(d->guest_slots));
  1157. }
  1158. static void qxl_reset_surfaces(PCIQXLDevice *d)
  1159. {
  1160. trace_qxl_reset_surfaces(d->id);
  1161. d->mode = QXL_MODE_UNDEFINED;
  1162. qxl_spice_destroy_surfaces(d, QXL_SYNC);
  1163. }
  1164. /* can be also called from spice server thread context */
  1165. void *qxl_phys2virt(PCIQXLDevice *qxl, QXLPHYSICAL pqxl, int group_id)
  1166. {
  1167. uint64_t phys = le64_to_cpu(pqxl);
  1168. uint32_t slot = (phys >> (64 - 8)) & 0xff;
  1169. uint64_t offset = phys & 0xffffffffffff;
  1170. switch (group_id) {
  1171. case MEMSLOT_GROUP_HOST:
  1172. return (void *)(intptr_t)offset;
  1173. case MEMSLOT_GROUP_GUEST:
  1174. if (slot >= NUM_MEMSLOTS) {
  1175. qxl_set_guest_bug(qxl, "slot too large %d >= %d", slot,
  1176. NUM_MEMSLOTS);
  1177. return NULL;
  1178. }
  1179. if (!qxl->guest_slots[slot].active) {
  1180. qxl_set_guest_bug(qxl, "inactive slot %d\n", slot);
  1181. return NULL;
  1182. }
  1183. if (offset < qxl->guest_slots[slot].delta) {
  1184. qxl_set_guest_bug(qxl,
  1185. "slot %d offset %"PRIu64" < delta %"PRIu64"\n",
  1186. slot, offset, qxl->guest_slots[slot].delta);
  1187. return NULL;
  1188. }
  1189. offset -= qxl->guest_slots[slot].delta;
  1190. if (offset > qxl->guest_slots[slot].size) {
  1191. qxl_set_guest_bug(qxl,
  1192. "slot %d offset %"PRIu64" > size %"PRIu64"\n",
  1193. slot, offset, qxl->guest_slots[slot].size);
  1194. return NULL;
  1195. }
  1196. return qxl->guest_slots[slot].ptr + offset;
  1197. }
  1198. return NULL;
  1199. }
  1200. static void qxl_create_guest_primary_complete(PCIQXLDevice *qxl)
  1201. {
  1202. /* for local rendering */
  1203. qxl_render_resize(qxl);
  1204. }
  1205. static void qxl_create_guest_primary(PCIQXLDevice *qxl, int loadvm,
  1206. qxl_async_io async)
  1207. {
  1208. QXLDevSurfaceCreate surface;
  1209. QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
  1210. int size;
  1211. int requested_height = le32_to_cpu(sc->height);
  1212. int requested_stride = le32_to_cpu(sc->stride);
  1213. size = abs(requested_stride) * requested_height;
  1214. if (size > qxl->vgamem_size) {
  1215. qxl_set_guest_bug(qxl, "%s: requested primary larger then framebuffer"
  1216. " size", __func__);
  1217. return;
  1218. }
  1219. if (qxl->mode == QXL_MODE_NATIVE) {
  1220. qxl_set_guest_bug(qxl, "%s: nop since already in QXL_MODE_NATIVE",
  1221. __func__);
  1222. }
  1223. qxl_exit_vga_mode(qxl);
  1224. surface.format = le32_to_cpu(sc->format);
  1225. surface.height = le32_to_cpu(sc->height);
  1226. surface.mem = le64_to_cpu(sc->mem);
  1227. surface.position = le32_to_cpu(sc->position);
  1228. surface.stride = le32_to_cpu(sc->stride);
  1229. surface.width = le32_to_cpu(sc->width);
  1230. surface.type = le32_to_cpu(sc->type);
  1231. surface.flags = le32_to_cpu(sc->flags);
  1232. trace_qxl_create_guest_primary(qxl->id, sc->width, sc->height, sc->mem,
  1233. sc->format, sc->position);
  1234. trace_qxl_create_guest_primary_rest(qxl->id, sc->stride, sc->type,
  1235. sc->flags);
  1236. if ((surface.stride & 0x3) != 0) {
  1237. qxl_set_guest_bug(qxl, "primary surface stride = %d %% 4 != 0",
  1238. surface.stride);
  1239. return;
  1240. }
  1241. surface.mouse_mode = true;
  1242. surface.group_id = MEMSLOT_GROUP_GUEST;
  1243. if (loadvm) {
  1244. surface.flags |= QXL_SURF_FLAG_KEEP_DATA;
  1245. }
  1246. qxl->mode = QXL_MODE_NATIVE;
  1247. qxl->cmdflags = 0;
  1248. qemu_spice_create_primary_surface(&qxl->ssd, 0, &surface, async);
  1249. if (async == QXL_SYNC) {
  1250. qxl_create_guest_primary_complete(qxl);
  1251. }
  1252. }
  1253. /* return 1 if surface destoy was initiated (in QXL_ASYNC case) or
  1254. * done (in QXL_SYNC case), 0 otherwise. */
  1255. static int qxl_destroy_primary(PCIQXLDevice *d, qxl_async_io async)
  1256. {
  1257. if (d->mode == QXL_MODE_UNDEFINED) {
  1258. return 0;
  1259. }
  1260. trace_qxl_destroy_primary(d->id);
  1261. d->mode = QXL_MODE_UNDEFINED;
  1262. qemu_spice_destroy_primary_surface(&d->ssd, 0, async);
  1263. qxl_spice_reset_cursor(d);
  1264. return 1;
  1265. }
  1266. static void qxl_set_mode(PCIQXLDevice *d, int modenr, int loadvm)
  1267. {
  1268. pcibus_t start = d->pci.io_regions[QXL_RAM_RANGE_INDEX].addr;
  1269. pcibus_t end = d->pci.io_regions[QXL_RAM_RANGE_INDEX].size + start;
  1270. QXLMode *mode = d->modes->modes + modenr;
  1271. uint64_t devmem = d->pci.io_regions[QXL_RAM_RANGE_INDEX].addr;
  1272. QXLMemSlot slot = {
  1273. .mem_start = start,
  1274. .mem_end = end
  1275. };
  1276. QXLSurfaceCreate surface = {
  1277. .width = mode->x_res,
  1278. .height = mode->y_res,
  1279. .stride = -mode->x_res * 4,
  1280. .format = SPICE_SURFACE_FMT_32_xRGB,
  1281. .flags = loadvm ? QXL_SURF_FLAG_KEEP_DATA : 0,
  1282. .mouse_mode = true,
  1283. .mem = devmem + d->shadow_rom.draw_area_offset,
  1284. };
  1285. trace_qxl_set_mode(d->id, modenr, mode->x_res, mode->y_res, mode->bits,
  1286. devmem);
  1287. if (!loadvm) {
  1288. qxl_hard_reset(d, 0);
  1289. }
  1290. d->guest_slots[0].slot = slot;
  1291. assert(qxl_add_memslot(d, 0, devmem, QXL_SYNC) == 0);
  1292. d->guest_primary.surface = surface;
  1293. qxl_create_guest_primary(d, 0, QXL_SYNC);
  1294. d->mode = QXL_MODE_COMPAT;
  1295. d->cmdflags = QXL_COMMAND_FLAG_COMPAT;
  1296. if (mode->bits == 16) {
  1297. d->cmdflags |= QXL_COMMAND_FLAG_COMPAT_16BPP;
  1298. }
  1299. d->shadow_rom.mode = cpu_to_le32(modenr);
  1300. d->rom->mode = cpu_to_le32(modenr);
  1301. qxl_rom_set_dirty(d);
  1302. }
  1303. static void ioport_write(void *opaque, hwaddr addr,
  1304. uint64_t val, unsigned size)
  1305. {
  1306. PCIQXLDevice *d = opaque;
  1307. uint32_t io_port = addr;
  1308. qxl_async_io async = QXL_SYNC;
  1309. uint32_t orig_io_port = io_port;
  1310. if (d->guest_bug && io_port != QXL_IO_RESET) {
  1311. return;
  1312. }
  1313. if (d->revision <= QXL_REVISION_STABLE_V10 &&
  1314. io_port > QXL_IO_FLUSH_RELEASE) {
  1315. qxl_set_guest_bug(d, "unsupported io %d for revision %d\n",
  1316. io_port, d->revision);
  1317. return;
  1318. }
  1319. switch (io_port) {
  1320. case QXL_IO_RESET:
  1321. case QXL_IO_SET_MODE:
  1322. case QXL_IO_MEMSLOT_ADD:
  1323. case QXL_IO_MEMSLOT_DEL:
  1324. case QXL_IO_CREATE_PRIMARY:
  1325. case QXL_IO_UPDATE_IRQ:
  1326. case QXL_IO_LOG:
  1327. case QXL_IO_MEMSLOT_ADD_ASYNC:
  1328. case QXL_IO_CREATE_PRIMARY_ASYNC:
  1329. break;
  1330. default:
  1331. if (d->mode != QXL_MODE_VGA) {
  1332. break;
  1333. }
  1334. trace_qxl_io_unexpected_vga_mode(d->id,
  1335. addr, val, io_port_to_string(io_port));
  1336. /* be nice to buggy guest drivers */
  1337. if (io_port >= QXL_IO_UPDATE_AREA_ASYNC &&
  1338. io_port < QXL_IO_RANGE_SIZE) {
  1339. qxl_send_events(d, QXL_INTERRUPT_IO_CMD);
  1340. }
  1341. return;
  1342. }
  1343. /* we change the io_port to avoid ifdeffery in the main switch */
  1344. orig_io_port = io_port;
  1345. switch (io_port) {
  1346. case QXL_IO_UPDATE_AREA_ASYNC:
  1347. io_port = QXL_IO_UPDATE_AREA;
  1348. goto async_common;
  1349. case QXL_IO_MEMSLOT_ADD_ASYNC:
  1350. io_port = QXL_IO_MEMSLOT_ADD;
  1351. goto async_common;
  1352. case QXL_IO_CREATE_PRIMARY_ASYNC:
  1353. io_port = QXL_IO_CREATE_PRIMARY;
  1354. goto async_common;
  1355. case QXL_IO_DESTROY_PRIMARY_ASYNC:
  1356. io_port = QXL_IO_DESTROY_PRIMARY;
  1357. goto async_common;
  1358. case QXL_IO_DESTROY_SURFACE_ASYNC:
  1359. io_port = QXL_IO_DESTROY_SURFACE_WAIT;
  1360. goto async_common;
  1361. case QXL_IO_DESTROY_ALL_SURFACES_ASYNC:
  1362. io_port = QXL_IO_DESTROY_ALL_SURFACES;
  1363. goto async_common;
  1364. case QXL_IO_FLUSH_SURFACES_ASYNC:
  1365. case QXL_IO_MONITORS_CONFIG_ASYNC:
  1366. async_common:
  1367. async = QXL_ASYNC;
  1368. qemu_mutex_lock(&d->async_lock);
  1369. if (d->current_async != QXL_UNDEFINED_IO) {
  1370. qxl_set_guest_bug(d, "%d async started before last (%d) complete",
  1371. io_port, d->current_async);
  1372. qemu_mutex_unlock(&d->async_lock);
  1373. return;
  1374. }
  1375. d->current_async = orig_io_port;
  1376. qemu_mutex_unlock(&d->async_lock);
  1377. break;
  1378. default:
  1379. break;
  1380. }
  1381. trace_qxl_io_write(d->id, qxl_mode_to_string(d->mode), addr, val, size,
  1382. async);
  1383. switch (io_port) {
  1384. case QXL_IO_UPDATE_AREA:
  1385. {
  1386. QXLCookie *cookie = NULL;
  1387. QXLRect update = d->ram->update_area;
  1388. if (d->ram->update_surface > d->ssd.num_surfaces) {
  1389. qxl_set_guest_bug(d, "QXL_IO_UPDATE_AREA: invalid surface id %d\n",
  1390. d->ram->update_surface);
  1391. break;
  1392. }
  1393. if (update.left >= update.right || update.top >= update.bottom ||
  1394. update.left < 0 || update.top < 0) {
  1395. qxl_set_guest_bug(d,
  1396. "QXL_IO_UPDATE_AREA: invalid area (%ux%u)x(%ux%u)\n",
  1397. update.left, update.top, update.right, update.bottom);
  1398. break;
  1399. }
  1400. if (async == QXL_ASYNC) {
  1401. cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
  1402. QXL_IO_UPDATE_AREA_ASYNC);
  1403. cookie->u.area = update;
  1404. }
  1405. qxl_spice_update_area(d, d->ram->update_surface,
  1406. cookie ? &cookie->u.area : &update,
  1407. NULL, 0, 0, async, cookie);
  1408. break;
  1409. }
  1410. case QXL_IO_NOTIFY_CMD:
  1411. qemu_spice_wakeup(&d->ssd);
  1412. break;
  1413. case QXL_IO_NOTIFY_CURSOR:
  1414. qemu_spice_wakeup(&d->ssd);
  1415. break;
  1416. case QXL_IO_UPDATE_IRQ:
  1417. qxl_update_irq(d);
  1418. break;
  1419. case QXL_IO_NOTIFY_OOM:
  1420. if (!SPICE_RING_IS_EMPTY(&d->ram->release_ring)) {
  1421. break;
  1422. }
  1423. d->oom_running = 1;
  1424. qxl_spice_oom(d);
  1425. d->oom_running = 0;
  1426. break;
  1427. case QXL_IO_SET_MODE:
  1428. qxl_set_mode(d, val, 0);
  1429. break;
  1430. case QXL_IO_LOG:
  1431. trace_qxl_io_log(d->id, d->ram->log_buf);
  1432. if (d->guestdebug) {
  1433. fprintf(stderr, "qxl/guest-%d: %" PRId64 ": %s", d->id,
  1434. qemu_get_clock_ns(vm_clock), d->ram->log_buf);
  1435. }
  1436. break;
  1437. case QXL_IO_RESET:
  1438. qxl_hard_reset(d, 0);
  1439. break;
  1440. case QXL_IO_MEMSLOT_ADD:
  1441. if (val >= NUM_MEMSLOTS) {
  1442. qxl_set_guest_bug(d, "QXL_IO_MEMSLOT_ADD: val out of range");
  1443. break;
  1444. }
  1445. if (d->guest_slots[val].active) {
  1446. qxl_set_guest_bug(d,
  1447. "QXL_IO_MEMSLOT_ADD: memory slot already active");
  1448. break;
  1449. }
  1450. d->guest_slots[val].slot = d->ram->mem_slot;
  1451. qxl_add_memslot(d, val, 0, async);
  1452. break;
  1453. case QXL_IO_MEMSLOT_DEL:
  1454. if (val >= NUM_MEMSLOTS) {
  1455. qxl_set_guest_bug(d, "QXL_IO_MEMSLOT_DEL: val out of range");
  1456. break;
  1457. }
  1458. qxl_del_memslot(d, val);
  1459. break;
  1460. case QXL_IO_CREATE_PRIMARY:
  1461. if (val != 0) {
  1462. qxl_set_guest_bug(d, "QXL_IO_CREATE_PRIMARY (async=%d): val != 0",
  1463. async);
  1464. goto cancel_async;
  1465. }
  1466. d->guest_primary.surface = d->ram->create_surface;
  1467. qxl_create_guest_primary(d, 0, async);
  1468. break;
  1469. case QXL_IO_DESTROY_PRIMARY:
  1470. if (val != 0) {
  1471. qxl_set_guest_bug(d, "QXL_IO_DESTROY_PRIMARY (async=%d): val != 0",
  1472. async);
  1473. goto cancel_async;
  1474. }
  1475. if (!qxl_destroy_primary(d, async)) {
  1476. trace_qxl_io_destroy_primary_ignored(d->id,
  1477. qxl_mode_to_string(d->mode));
  1478. goto cancel_async;
  1479. }
  1480. break;
  1481. case QXL_IO_DESTROY_SURFACE_WAIT:
  1482. if (val >= d->ssd.num_surfaces) {
  1483. qxl_set_guest_bug(d, "QXL_IO_DESTROY_SURFACE (async=%d):"
  1484. "%" PRIu64 " >= NUM_SURFACES", async, val);
  1485. goto cancel_async;
  1486. }
  1487. qxl_spice_destroy_surface_wait(d, val, async);
  1488. break;
  1489. case QXL_IO_FLUSH_RELEASE: {
  1490. QXLReleaseRing *ring = &d->ram->release_ring;
  1491. if (ring->prod - ring->cons + 1 == ring->num_items) {
  1492. fprintf(stderr,
  1493. "ERROR: no flush, full release ring [p%d,%dc]\n",
  1494. ring->prod, ring->cons);
  1495. }
  1496. qxl_push_free_res(d, 1 /* flush */);
  1497. break;
  1498. }
  1499. case QXL_IO_FLUSH_SURFACES_ASYNC:
  1500. qxl_spice_flush_surfaces_async(d);
  1501. break;
  1502. case QXL_IO_DESTROY_ALL_SURFACES:
  1503. d->mode = QXL_MODE_UNDEFINED;
  1504. qxl_spice_destroy_surfaces(d, async);
  1505. break;
  1506. case QXL_IO_MONITORS_CONFIG_ASYNC:
  1507. qxl_spice_monitors_config_async(d, 0);
  1508. break;
  1509. default:
  1510. qxl_set_guest_bug(d, "%s: unexpected ioport=0x%x\n", __func__, io_port);
  1511. }
  1512. return;
  1513. cancel_async:
  1514. if (async) {
  1515. qxl_send_events(d, QXL_INTERRUPT_IO_CMD);
  1516. qemu_mutex_lock(&d->async_lock);
  1517. d->current_async = QXL_UNDEFINED_IO;
  1518. qemu_mutex_unlock(&d->async_lock);
  1519. }
  1520. }
  1521. static uint64_t ioport_read(void *opaque, hwaddr addr,
  1522. unsigned size)
  1523. {
  1524. PCIQXLDevice *qxl = opaque;
  1525. trace_qxl_io_read_unexpected(qxl->id);
  1526. return 0xff;
  1527. }
  1528. static const MemoryRegionOps qxl_io_ops = {
  1529. .read = ioport_read,
  1530. .write = ioport_write,
  1531. .valid = {
  1532. .min_access_size = 1,
  1533. .max_access_size = 1,
  1534. },
  1535. };
  1536. static void pipe_read(void *opaque)
  1537. {
  1538. PCIQXLDevice *d = opaque;
  1539. char dummy;
  1540. int len;
  1541. do {
  1542. len = read(d->pipe[0], &dummy, sizeof(dummy));
  1543. } while (len == sizeof(dummy));
  1544. qxl_update_irq(d);
  1545. }
  1546. static void qxl_send_events(PCIQXLDevice *d, uint32_t events)
  1547. {
  1548. uint32_t old_pending;
  1549. uint32_t le_events = cpu_to_le32(events);
  1550. trace_qxl_send_events(d->id, events);
  1551. if (!qemu_spice_display_is_running(&d->ssd)) {
  1552. /* spice-server tracks guest running state and should not do this */
  1553. fprintf(stderr, "%s: spice-server bug: guest stopped, ignoring\n",
  1554. __func__);
  1555. trace_qxl_send_events_vm_stopped(d->id, events);
  1556. return;
  1557. }
  1558. old_pending = __sync_fetch_and_or(&d->ram->int_pending, le_events);
  1559. if ((old_pending & le_events) == le_events) {
  1560. return;
  1561. }
  1562. if (qemu_thread_is_self(&d->main)) {
  1563. qxl_update_irq(d);
  1564. } else {
  1565. if (write(d->pipe[1], d, 1) != 1) {
  1566. dprint(d, 1, "%s: write to pipe failed\n", __func__);
  1567. }
  1568. }
  1569. }
  1570. static void init_pipe_signaling(PCIQXLDevice *d)
  1571. {
  1572. if (pipe(d->pipe) < 0) {
  1573. fprintf(stderr, "%s:%s: qxl pipe creation failed\n",
  1574. __FILE__, __func__);
  1575. exit(1);
  1576. }
  1577. fcntl(d->pipe[0], F_SETFL, O_NONBLOCK);
  1578. fcntl(d->pipe[1], F_SETFL, O_NONBLOCK);
  1579. fcntl(d->pipe[0], F_SETOWN, getpid());
  1580. qemu_thread_get_self(&d->main);
  1581. qemu_set_fd_handler(d->pipe[0], pipe_read, NULL, d);
  1582. }
  1583. /* graphics console */
  1584. static void qxl_hw_update(void *opaque)
  1585. {
  1586. PCIQXLDevice *qxl = opaque;
  1587. VGACommonState *vga = &qxl->vga;
  1588. switch (qxl->mode) {
  1589. case QXL_MODE_VGA:
  1590. vga->update(vga);
  1591. break;
  1592. case QXL_MODE_COMPAT:
  1593. case QXL_MODE_NATIVE:
  1594. qxl_render_update(qxl);
  1595. break;
  1596. default:
  1597. break;
  1598. }
  1599. }
  1600. static void qxl_hw_invalidate(void *opaque)
  1601. {
  1602. PCIQXLDevice *qxl = opaque;
  1603. VGACommonState *vga = &qxl->vga;
  1604. vga->invalidate(vga);
  1605. }
  1606. static void qxl_hw_screen_dump(void *opaque, const char *filename, bool cswitch,
  1607. Error **errp)
  1608. {
  1609. PCIQXLDevice *qxl = opaque;
  1610. VGACommonState *vga = &qxl->vga;
  1611. switch (qxl->mode) {
  1612. case QXL_MODE_COMPAT:
  1613. case QXL_MODE_NATIVE:
  1614. qxl_render_update(qxl);
  1615. ppm_save(filename, qxl->ssd.ds->surface, errp);
  1616. break;
  1617. case QXL_MODE_VGA:
  1618. vga->screen_dump(vga, filename, cswitch, errp);
  1619. break;
  1620. default:
  1621. break;
  1622. }
  1623. }
  1624. static void qxl_hw_text_update(void *opaque, console_ch_t *chardata)
  1625. {
  1626. PCIQXLDevice *qxl = opaque;
  1627. VGACommonState *vga = &qxl->vga;
  1628. if (qxl->mode == QXL_MODE_VGA) {
  1629. vga->text_update(vga, chardata);
  1630. return;
  1631. }
  1632. }
  1633. static void qxl_dirty_surfaces(PCIQXLDevice *qxl)
  1634. {
  1635. uintptr_t vram_start;
  1636. int i;
  1637. if (qxl->mode != QXL_MODE_NATIVE && qxl->mode != QXL_MODE_COMPAT) {
  1638. return;
  1639. }
  1640. /* dirty the primary surface */
  1641. qxl_set_dirty(&qxl->vga.vram, qxl->shadow_rom.draw_area_offset,
  1642. qxl->shadow_rom.surface0_area_size);
  1643. vram_start = (uintptr_t)memory_region_get_ram_ptr(&qxl->vram_bar);
  1644. /* dirty the off-screen surfaces */
  1645. for (i = 0; i < qxl->ssd.num_surfaces; i++) {
  1646. QXLSurfaceCmd *cmd;
  1647. intptr_t surface_offset;
  1648. int surface_size;
  1649. if (qxl->guest_surfaces.cmds[i] == 0) {
  1650. continue;
  1651. }
  1652. cmd = qxl_phys2virt(qxl, qxl->guest_surfaces.cmds[i],
  1653. MEMSLOT_GROUP_GUEST);
  1654. assert(cmd);
  1655. assert(cmd->type == QXL_SURFACE_CMD_CREATE);
  1656. surface_offset = (intptr_t)qxl_phys2virt(qxl,
  1657. cmd->u.surface_create.data,
  1658. MEMSLOT_GROUP_GUEST);
  1659. assert(surface_offset);
  1660. surface_offset -= vram_start;
  1661. surface_size = cmd->u.surface_create.height *
  1662. abs(cmd->u.surface_create.stride);
  1663. trace_qxl_surfaces_dirty(qxl->id, i, (int)surface_offset, surface_size);
  1664. qxl_set_dirty(&qxl->vram_bar, surface_offset, surface_size);
  1665. }
  1666. }
  1667. static void qxl_vm_change_state_handler(void *opaque, int running,
  1668. RunState state)
  1669. {
  1670. PCIQXLDevice *qxl = opaque;
  1671. if (running) {
  1672. /*
  1673. * if qxl_send_events was called from spice server context before
  1674. * migration ended, qxl_update_irq for these events might not have been
  1675. * called
  1676. */
  1677. qxl_update_irq(qxl);
  1678. } else {
  1679. /* make sure surfaces are saved before migration */
  1680. qxl_dirty_surfaces(qxl);
  1681. }
  1682. }
  1683. /* display change listener */
  1684. static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
  1685. {
  1686. if (qxl0->mode == QXL_MODE_VGA) {
  1687. qemu_spice_display_update(&qxl0->ssd, x, y, w, h);
  1688. }
  1689. }
  1690. static void display_resize(struct DisplayState *ds)
  1691. {
  1692. if (qxl0->mode == QXL_MODE_VGA) {
  1693. qemu_spice_display_resize(&qxl0->ssd);
  1694. }
  1695. }
  1696. static void display_refresh(struct DisplayState *ds)
  1697. {
  1698. if (qxl0->mode == QXL_MODE_VGA) {
  1699. qemu_spice_display_refresh(&qxl0->ssd);
  1700. } else {
  1701. qemu_mutex_lock(&qxl0->ssd.lock);
  1702. qemu_spice_cursor_refresh_unlocked(&qxl0->ssd);
  1703. qemu_mutex_unlock(&qxl0->ssd.lock);
  1704. }
  1705. }
  1706. static DisplayChangeListener display_listener = {
  1707. .dpy_gfx_update = display_update,
  1708. .dpy_gfx_resize = display_resize,
  1709. .dpy_refresh = display_refresh,
  1710. };
  1711. static void qxl_init_ramsize(PCIQXLDevice *qxl)
  1712. {
  1713. /* vga mode framebuffer / primary surface (bar 0, first part) */
  1714. if (qxl->vgamem_size_mb < 8) {
  1715. qxl->vgamem_size_mb = 8;
  1716. }
  1717. qxl->vgamem_size = qxl->vgamem_size_mb * 1024 * 1024;
  1718. /* vga ram (bar 0, total) */
  1719. if (qxl->ram_size_mb != -1) {
  1720. qxl->vga.vram_size = qxl->ram_size_mb * 1024 * 1024;
  1721. }
  1722. if (qxl->vga.vram_size < qxl->vgamem_size * 2) {
  1723. qxl->vga.vram_size = qxl->vgamem_size * 2;
  1724. }
  1725. /* vram32 (surfaces, 32bit, bar 1) */
  1726. if (qxl->vram32_size_mb != -1) {
  1727. qxl->vram32_size = qxl->vram32_size_mb * 1024 * 1024;
  1728. }
  1729. if (qxl->vram32_size < 4096) {
  1730. qxl->vram32_size = 4096;
  1731. }
  1732. /* vram (surfaces, 64bit, bar 4+5) */
  1733. if (qxl->vram_size_mb != -1) {
  1734. qxl->vram_size = qxl->vram_size_mb * 1024 * 1024;
  1735. }
  1736. if (qxl->vram_size < qxl->vram32_size) {
  1737. qxl->vram_size = qxl->vram32_size;
  1738. }
  1739. if (qxl->revision == 1) {
  1740. qxl->vram32_size = 4096;
  1741. qxl->vram_size = 4096;
  1742. }
  1743. qxl->vgamem_size = msb_mask(qxl->vgamem_size * 2 - 1);
  1744. qxl->vga.vram_size = msb_mask(qxl->vga.vram_size * 2 - 1);
  1745. qxl->vram32_size = msb_mask(qxl->vram32_size * 2 - 1);
  1746. qxl->vram_size = msb_mask(qxl->vram_size * 2 - 1);
  1747. }
  1748. static int qxl_init_common(PCIQXLDevice *qxl)
  1749. {
  1750. uint8_t* config = qxl->pci.config;
  1751. uint32_t pci_device_rev;
  1752. uint32_t io_size;
  1753. qxl->mode = QXL_MODE_UNDEFINED;
  1754. qxl->generation = 1;
  1755. qxl->num_memslots = NUM_MEMSLOTS;
  1756. qemu_mutex_init(&qxl->track_lock);
  1757. qemu_mutex_init(&qxl->async_lock);
  1758. qxl->current_async = QXL_UNDEFINED_IO;
  1759. qxl->guest_bug = 0;
  1760. switch (qxl->revision) {
  1761. case 1: /* spice 0.4 -- qxl-1 */
  1762. pci_device_rev = QXL_REVISION_STABLE_V04;
  1763. io_size = 8;
  1764. break;
  1765. case 2: /* spice 0.6 -- qxl-2 */
  1766. pci_device_rev = QXL_REVISION_STABLE_V06;
  1767. io_size = 16;
  1768. break;
  1769. case 3: /* qxl-3 */
  1770. pci_device_rev = QXL_REVISION_STABLE_V10;
  1771. io_size = 32; /* PCI region size must be pow2 */
  1772. break;
  1773. case 4: /* qxl-4 */
  1774. pci_device_rev = QXL_REVISION_STABLE_V12;
  1775. io_size = msb_mask(QXL_IO_RANGE_SIZE * 2 - 1);
  1776. break;
  1777. default:
  1778. error_report("Invalid revision %d for qxl device (max %d)",
  1779. qxl->revision, QXL_DEFAULT_REVISION);
  1780. return -1;
  1781. }
  1782. pci_set_byte(&config[PCI_REVISION_ID], pci_device_rev);
  1783. pci_set_byte(&config[PCI_INTERRUPT_PIN], 1);
  1784. qxl->rom_size = qxl_rom_size();
  1785. memory_region_init_ram(&qxl->rom_bar, "qxl.vrom", qxl->rom_size);
  1786. vmstate_register_ram(&qxl->rom_bar, &qxl->pci.qdev);
  1787. init_qxl_rom(qxl);
  1788. init_qxl_ram(qxl);
  1789. qxl->guest_surfaces.cmds = g_new0(QXLPHYSICAL, qxl->ssd.num_surfaces);
  1790. memory_region_init_ram(&qxl->vram_bar, "qxl.vram", qxl->vram_size);
  1791. vmstate_register_ram(&qxl->vram_bar, &qxl->pci.qdev);
  1792. memory_region_init_alias(&qxl->vram32_bar, "qxl.vram32", &qxl->vram_bar,
  1793. 0, qxl->vram32_size);
  1794. memory_region_init_io(&qxl->io_bar, &qxl_io_ops, qxl,
  1795. "qxl-ioports", io_size);
  1796. if (qxl->id == 0) {
  1797. vga_dirty_log_start(&qxl->vga);
  1798. }
  1799. memory_region_set_flush_coalesced(&qxl->io_bar);
  1800. pci_register_bar(&qxl->pci, QXL_IO_RANGE_INDEX,
  1801. PCI_BASE_ADDRESS_SPACE_IO, &qxl->io_bar);
  1802. pci_register_bar(&qxl->pci, QXL_ROM_RANGE_INDEX,
  1803. PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->rom_bar);
  1804. pci_register_bar(&qxl->pci, QXL_RAM_RANGE_INDEX,
  1805. PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->vga.vram);
  1806. pci_register_bar(&qxl->pci, QXL_VRAM_RANGE_INDEX,
  1807. PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->vram32_bar);
  1808. if (qxl->vram32_size < qxl->vram_size) {
  1809. /*
  1810. * Make the 64bit vram bar show up only in case it is
  1811. * configured to be larger than the 32bit vram bar.
  1812. */
  1813. pci_register_bar(&qxl->pci, QXL_VRAM64_RANGE_INDEX,
  1814. PCI_BASE_ADDRESS_SPACE_MEMORY |
  1815. PCI_BASE_ADDRESS_MEM_TYPE_64 |
  1816. PCI_BASE_ADDRESS_MEM_PREFETCH,
  1817. &qxl->vram_bar);
  1818. }
  1819. /* print pci bar details */
  1820. dprint(qxl, 1, "ram/%s: %d MB [region 0]\n",
  1821. qxl->id == 0 ? "pri" : "sec",
  1822. qxl->vga.vram_size / (1024*1024));
  1823. dprint(qxl, 1, "vram/32: %d MB [region 1]\n",
  1824. qxl->vram32_size / (1024*1024));
  1825. dprint(qxl, 1, "vram/64: %d MB %s\n",
  1826. qxl->vram_size / (1024*1024),
  1827. qxl->vram32_size < qxl->vram_size ? "[region 4]" : "[unmapped]");
  1828. qxl->ssd.qxl.base.sif = &qxl_interface.base;
  1829. qxl->ssd.qxl.id = qxl->id;
  1830. if (qemu_spice_add_interface(&qxl->ssd.qxl.base) != 0) {
  1831. error_report("qxl interface %d.%d not supported by spice-server",
  1832. SPICE_INTERFACE_QXL_MAJOR, SPICE_INTERFACE_QXL_MINOR);
  1833. return -1;
  1834. }
  1835. qemu_add_vm_change_state_handler(qxl_vm_change_state_handler, qxl);
  1836. init_pipe_signaling(qxl);
  1837. qxl_reset_state(qxl);
  1838. qxl->update_area_bh = qemu_bh_new(qxl_render_update_area_bh, qxl);
  1839. return 0;
  1840. }
  1841. static int qxl_init_primary(PCIDevice *dev)
  1842. {
  1843. PCIQXLDevice *qxl = DO_UPCAST(PCIQXLDevice, pci, dev);
  1844. VGACommonState *vga = &qxl->vga;
  1845. PortioList *qxl_vga_port_list = g_new(PortioList, 1);
  1846. int rc;
  1847. qxl->id = 0;
  1848. qxl_init_ramsize(qxl);
  1849. vga->vram_size_mb = qxl->vga.vram_size >> 20;
  1850. vga_common_init(vga);
  1851. vga_init(vga, pci_address_space(dev), pci_address_space_io(dev), false);
  1852. portio_list_init(qxl_vga_port_list, qxl_vga_portio_list, vga, "vga");
  1853. portio_list_add(qxl_vga_port_list, pci_address_space_io(dev), 0x3b0);
  1854. vga->ds = graphic_console_init(qxl_hw_update, qxl_hw_invalidate,
  1855. qxl_hw_screen_dump, qxl_hw_text_update, qxl);
  1856. qemu_spice_display_init_common(&qxl->ssd, vga->ds);
  1857. qxl0 = qxl;
  1858. rc = qxl_init_common(qxl);
  1859. if (rc != 0) {
  1860. return rc;
  1861. }
  1862. register_displaychangelistener(vga->ds, &display_listener);
  1863. return rc;
  1864. }
  1865. static int qxl_init_secondary(PCIDevice *dev)
  1866. {
  1867. static int device_id = 1;
  1868. PCIQXLDevice *qxl = DO_UPCAST(PCIQXLDevice, pci, dev);
  1869. qxl->id = device_id++;
  1870. qxl_init_ramsize(qxl);
  1871. memory_region_init_ram(&qxl->vga.vram, "qxl.vgavram", qxl->vga.vram_size);
  1872. vmstate_register_ram(&qxl->vga.vram, &qxl->pci.qdev);
  1873. qxl->vga.vram_ptr = memory_region_get_ram_ptr(&qxl->vga.vram);
  1874. return qxl_init_common(qxl);
  1875. }
  1876. static void qxl_pre_save(void *opaque)
  1877. {
  1878. PCIQXLDevice* d = opaque;
  1879. uint8_t *ram_start = d->vga.vram_ptr;
  1880. trace_qxl_pre_save(d->id);
  1881. if (d->last_release == NULL) {
  1882. d->last_release_offset = 0;
  1883. } else {
  1884. d->last_release_offset = (uint8_t *)d->last_release - ram_start;
  1885. }
  1886. assert(d->last_release_offset < d->vga.vram_size);
  1887. }
  1888. static int qxl_pre_load(void *opaque)
  1889. {
  1890. PCIQXLDevice* d = opaque;
  1891. trace_qxl_pre_load(d->id);
  1892. qxl_hard_reset(d, 1);
  1893. qxl_exit_vga_mode(d);
  1894. return 0;
  1895. }
  1896. static void qxl_create_memslots(PCIQXLDevice *d)
  1897. {
  1898. int i;
  1899. for (i = 0; i < NUM_MEMSLOTS; i++) {
  1900. if (!d->guest_slots[i].active) {
  1901. continue;
  1902. }
  1903. qxl_add_memslot(d, i, 0, QXL_SYNC);
  1904. }
  1905. }
  1906. static int qxl_post_load(void *opaque, int version)
  1907. {
  1908. PCIQXLDevice* d = opaque;
  1909. uint8_t *ram_start = d->vga.vram_ptr;
  1910. QXLCommandExt *cmds;
  1911. int in, out, newmode;
  1912. assert(d->last_release_offset < d->vga.vram_size);
  1913. if (d->last_release_offset == 0) {
  1914. d->last_release = NULL;
  1915. } else {
  1916. d->last_release = (QXLReleaseInfo *)(ram_start + d->last_release_offset);
  1917. }
  1918. d->modes = (QXLModes*)((uint8_t*)d->rom + d->rom->modes_offset);
  1919. trace_qxl_post_load(d->id, qxl_mode_to_string(d->mode));
  1920. newmode = d->mode;
  1921. d->mode = QXL_MODE_UNDEFINED;
  1922. switch (newmode) {
  1923. case QXL_MODE_UNDEFINED:
  1924. qxl_create_memslots(d);
  1925. break;
  1926. case QXL_MODE_VGA:
  1927. qxl_create_memslots(d);
  1928. qxl_enter_vga_mode(d);
  1929. break;
  1930. case QXL_MODE_NATIVE:
  1931. qxl_create_memslots(d);
  1932. qxl_create_guest_primary(d, 1, QXL_SYNC);
  1933. /* replay surface-create and cursor-set commands */
  1934. cmds = g_malloc0(sizeof(QXLCommandExt) * (d->ssd.num_surfaces + 1));
  1935. for (in = 0, out = 0; in < d->ssd.num_surfaces; in++) {
  1936. if (d->guest_surfaces.cmds[in] == 0) {
  1937. continue;
  1938. }
  1939. cmds[out].cmd.data = d->guest_surfaces.cmds[in];
  1940. cmds[out].cmd.type = QXL_CMD_SURFACE;
  1941. cmds[out].group_id = MEMSLOT_GROUP_GUEST;
  1942. out++;
  1943. }
  1944. if (d->guest_cursor) {
  1945. cmds[out].cmd.data = d->guest_cursor;
  1946. cmds[out].cmd.type = QXL_CMD_CURSOR;
  1947. cmds[out].group_id = MEMSLOT_GROUP_GUEST;
  1948. out++;
  1949. }
  1950. qxl_spice_loadvm_commands(d, cmds, out);
  1951. g_free(cmds);
  1952. if (d->guest_monitors_config) {
  1953. qxl_spice_monitors_config_async(d, 1);
  1954. }
  1955. break;
  1956. case QXL_MODE_COMPAT:
  1957. /* note: no need to call qxl_create_memslots, qxl_set_mode
  1958. * creates the mem slot. */
  1959. qxl_set_mode(d, d->shadow_rom.mode, 1);
  1960. break;
  1961. }
  1962. return 0;
  1963. }
  1964. #define QXL_SAVE_VERSION 21
  1965. static bool qxl_monitors_config_needed(void *opaque)
  1966. {
  1967. PCIQXLDevice *qxl = opaque;
  1968. return qxl->guest_monitors_config != 0;
  1969. }
  1970. static VMStateDescription qxl_memslot = {
  1971. .name = "qxl-memslot",
  1972. .version_id = QXL_SAVE_VERSION,
  1973. .minimum_version_id = QXL_SAVE_VERSION,
  1974. .fields = (VMStateField[]) {
  1975. VMSTATE_UINT64(slot.mem_start, struct guest_slots),
  1976. VMSTATE_UINT64(slot.mem_end, struct guest_slots),
  1977. VMSTATE_UINT32(active, struct guest_slots),
  1978. VMSTATE_END_OF_LIST()
  1979. }
  1980. };
  1981. static VMStateDescription qxl_surface = {
  1982. .name = "qxl-surface",
  1983. .version_id = QXL_SAVE_VERSION,
  1984. .minimum_version_id = QXL_SAVE_VERSION,
  1985. .fields = (VMStateField[]) {
  1986. VMSTATE_UINT32(width, QXLSurfaceCreate),
  1987. VMSTATE_UINT32(height, QXLSurfaceCreate),
  1988. VMSTATE_INT32(stride, QXLSurfaceCreate),
  1989. VMSTATE_UINT32(format, QXLSurfaceCreate),
  1990. VMSTATE_UINT32(position, QXLSurfaceCreate),
  1991. VMSTATE_UINT32(mouse_mode, QXLSurfaceCreate),
  1992. VMSTATE_UINT32(flags, QXLSurfaceCreate),
  1993. VMSTATE_UINT32(type, QXLSurfaceCreate),
  1994. VMSTATE_UINT64(mem, QXLSurfaceCreate),
  1995. VMSTATE_END_OF_LIST()
  1996. }
  1997. };
  1998. static VMStateDescription qxl_vmstate_monitors_config = {
  1999. .name = "qxl/monitors-config",
  2000. .version_id = 1,
  2001. .minimum_version_id = 1,
  2002. .fields = (VMStateField[]) {
  2003. VMSTATE_UINT64(guest_monitors_config, PCIQXLDevice),
  2004. VMSTATE_END_OF_LIST()
  2005. },
  2006. };
  2007. static VMStateDescription qxl_vmstate = {
  2008. .name = "qxl",
  2009. .version_id = QXL_SAVE_VERSION,
  2010. .minimum_version_id = QXL_SAVE_VERSION,
  2011. .pre_save = qxl_pre_save,
  2012. .pre_load = qxl_pre_load,
  2013. .post_load = qxl_post_load,
  2014. .fields = (VMStateField[]) {
  2015. VMSTATE_PCI_DEVICE(pci, PCIQXLDevice),
  2016. VMSTATE_STRUCT(vga, PCIQXLDevice, 0, vmstate_vga_common, VGACommonState),
  2017. VMSTATE_UINT32(shadow_rom.mode, PCIQXLDevice),
  2018. VMSTATE_UINT32(num_free_res, PCIQXLDevice),
  2019. VMSTATE_UINT32(last_release_offset, PCIQXLDevice),
  2020. VMSTATE_UINT32(mode, PCIQXLDevice),
  2021. VMSTATE_UINT32(ssd.unique, PCIQXLDevice),
  2022. VMSTATE_INT32_EQUAL(num_memslots, PCIQXLDevice),
  2023. VMSTATE_STRUCT_ARRAY(guest_slots, PCIQXLDevice, NUM_MEMSLOTS, 0,
  2024. qxl_memslot, struct guest_slots),
  2025. VMSTATE_STRUCT(guest_primary.surface, PCIQXLDevice, 0,
  2026. qxl_surface, QXLSurfaceCreate),
  2027. VMSTATE_INT32_EQUAL(ssd.num_surfaces, PCIQXLDevice),
  2028. VMSTATE_VARRAY_INT32(guest_surfaces.cmds, PCIQXLDevice,
  2029. ssd.num_surfaces, 0,
  2030. vmstate_info_uint64, uint64_t),
  2031. VMSTATE_UINT64(guest_cursor, PCIQXLDevice),
  2032. VMSTATE_END_OF_LIST()
  2033. },
  2034. .subsections = (VMStateSubsection[]) {
  2035. {
  2036. .vmsd = &qxl_vmstate_monitors_config,
  2037. .needed = qxl_monitors_config_needed,
  2038. }, {
  2039. /* empty */
  2040. }
  2041. }
  2042. };
  2043. static Property qxl_properties[] = {
  2044. DEFINE_PROP_UINT32("ram_size", PCIQXLDevice, vga.vram_size,
  2045. 64 * 1024 * 1024),
  2046. DEFINE_PROP_UINT32("vram_size", PCIQXLDevice, vram32_size,
  2047. 64 * 1024 * 1024),
  2048. DEFINE_PROP_UINT32("revision", PCIQXLDevice, revision,
  2049. QXL_DEFAULT_REVISION),
  2050. DEFINE_PROP_UINT32("debug", PCIQXLDevice, debug, 0),
  2051. DEFINE_PROP_UINT32("guestdebug", PCIQXLDevice, guestdebug, 0),
  2052. DEFINE_PROP_UINT32("cmdlog", PCIQXLDevice, cmdlog, 0),
  2053. DEFINE_PROP_UINT32("ram_size_mb", PCIQXLDevice, ram_size_mb, -1),
  2054. DEFINE_PROP_UINT32("vram_size_mb", PCIQXLDevice, vram32_size_mb, -1),
  2055. DEFINE_PROP_UINT32("vram64_size_mb", PCIQXLDevice, vram_size_mb, -1),
  2056. DEFINE_PROP_UINT32("vgamem_mb", PCIQXLDevice, vgamem_size_mb, 16),
  2057. DEFINE_PROP_INT32("surfaces", PCIQXLDevice, ssd.num_surfaces, 1024),
  2058. DEFINE_PROP_END_OF_LIST(),
  2059. };
  2060. static void qxl_primary_class_init(ObjectClass *klass, void *data)
  2061. {
  2062. DeviceClass *dc = DEVICE_CLASS(klass);
  2063. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  2064. k->no_hotplug = 1;
  2065. k->init = qxl_init_primary;
  2066. k->romfile = "vgabios-qxl.bin";
  2067. k->vendor_id = REDHAT_PCI_VENDOR_ID;
  2068. k->device_id = QXL_DEVICE_ID_STABLE;
  2069. k->class_id = PCI_CLASS_DISPLAY_VGA;
  2070. dc->desc = "Spice QXL GPU (primary, vga compatible)";
  2071. dc->reset = qxl_reset_handler;
  2072. dc->vmsd = &qxl_vmstate;
  2073. dc->props = qxl_properties;
  2074. }
  2075. static const TypeInfo qxl_primary_info = {
  2076. .name = "qxl-vga",
  2077. .parent = TYPE_PCI_DEVICE,
  2078. .instance_size = sizeof(PCIQXLDevice),
  2079. .class_init = qxl_primary_class_init,
  2080. };
  2081. static void qxl_secondary_class_init(ObjectClass *klass, void *data)
  2082. {
  2083. DeviceClass *dc = DEVICE_CLASS(klass);
  2084. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  2085. k->init = qxl_init_secondary;
  2086. k->vendor_id = REDHAT_PCI_VENDOR_ID;
  2087. k->device_id = QXL_DEVICE_ID_STABLE;
  2088. k->class_id = PCI_CLASS_DISPLAY_OTHER;
  2089. dc->desc = "Spice QXL GPU (secondary)";
  2090. dc->reset = qxl_reset_handler;
  2091. dc->vmsd = &qxl_vmstate;
  2092. dc->props = qxl_properties;
  2093. }
  2094. static const TypeInfo qxl_secondary_info = {
  2095. .name = "qxl",
  2096. .parent = TYPE_PCI_DEVICE,
  2097. .instance_size = sizeof(PCIQXLDevice),
  2098. .class_init = qxl_secondary_class_init,
  2099. };
  2100. static void qxl_register_types(void)
  2101. {
  2102. type_register_static(&qxl_primary_info);
  2103. type_register_static(&qxl_secondary_info);
  2104. }
  2105. type_init(qxl_register_types)