qxl.c 83 KB

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