2
0

qxl.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548
  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. WITH_QEMU_LOCK_GUARD(&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. }
  435. if (cmd->type == QXL_SURFACE_CMD_DESTROY) {
  436. qxl->guest_surfaces.cmds[id] = 0;
  437. qxl->guest_surfaces.count--;
  438. }
  439. }
  440. break;
  441. }
  442. case QXL_CMD_CURSOR:
  443. {
  444. QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
  445. if (!cmd) {
  446. return 1;
  447. }
  448. if (cmd->type == QXL_CURSOR_SET) {
  449. qemu_mutex_lock(&qxl->track_lock);
  450. qxl->guest_cursor = ext->cmd.data;
  451. qemu_mutex_unlock(&qxl->track_lock);
  452. }
  453. if (cmd->type == QXL_CURSOR_HIDE) {
  454. qemu_mutex_lock(&qxl->track_lock);
  455. qxl->guest_cursor = 0;
  456. qemu_mutex_unlock(&qxl->track_lock);
  457. }
  458. break;
  459. }
  460. }
  461. return 0;
  462. }
  463. /* spice display interface callbacks */
  464. static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
  465. {
  466. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  467. trace_qxl_interface_attach_worker(qxl->id);
  468. }
  469. static void interface_set_compression_level(QXLInstance *sin, int level)
  470. {
  471. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  472. trace_qxl_interface_set_compression_level(qxl->id, level);
  473. qxl->shadow_rom.compression_level = cpu_to_le32(level);
  474. qxl->rom->compression_level = cpu_to_le32(level);
  475. qxl_rom_set_dirty(qxl);
  476. }
  477. #if SPICE_NEEDS_SET_MM_TIME
  478. static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
  479. {
  480. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  481. if (!qemu_spice_display_is_running(&qxl->ssd)) {
  482. return;
  483. }
  484. trace_qxl_interface_set_mm_time(qxl->id, mm_time);
  485. qxl->shadow_rom.mm_clock = cpu_to_le32(mm_time);
  486. qxl->rom->mm_clock = cpu_to_le32(mm_time);
  487. qxl_rom_set_dirty(qxl);
  488. }
  489. #endif
  490. static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
  491. {
  492. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  493. trace_qxl_interface_get_init_info(qxl->id);
  494. info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
  495. info->memslot_id_bits = MEMSLOT_SLOT_BITS;
  496. info->num_memslots = NUM_MEMSLOTS;
  497. info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
  498. info->internal_groupslot_id = 0;
  499. info->qxl_ram_size =
  500. le32_to_cpu(qxl->shadow_rom.num_pages) << QXL_PAGE_BITS;
  501. info->n_surfaces = qxl->ssd.num_surfaces;
  502. }
  503. static const char *qxl_mode_to_string(int mode)
  504. {
  505. switch (mode) {
  506. case QXL_MODE_COMPAT:
  507. return "compat";
  508. case QXL_MODE_NATIVE:
  509. return "native";
  510. case QXL_MODE_UNDEFINED:
  511. return "undefined";
  512. case QXL_MODE_VGA:
  513. return "vga";
  514. }
  515. return "INVALID";
  516. }
  517. static const char *io_port_to_string(uint32_t io_port)
  518. {
  519. if (io_port >= QXL_IO_RANGE_SIZE) {
  520. return "out of range";
  521. }
  522. static const char *io_port_to_string[QXL_IO_RANGE_SIZE + 1] = {
  523. [QXL_IO_NOTIFY_CMD] = "QXL_IO_NOTIFY_CMD",
  524. [QXL_IO_NOTIFY_CURSOR] = "QXL_IO_NOTIFY_CURSOR",
  525. [QXL_IO_UPDATE_AREA] = "QXL_IO_UPDATE_AREA",
  526. [QXL_IO_UPDATE_IRQ] = "QXL_IO_UPDATE_IRQ",
  527. [QXL_IO_NOTIFY_OOM] = "QXL_IO_NOTIFY_OOM",
  528. [QXL_IO_RESET] = "QXL_IO_RESET",
  529. [QXL_IO_SET_MODE] = "QXL_IO_SET_MODE",
  530. [QXL_IO_LOG] = "QXL_IO_LOG",
  531. [QXL_IO_MEMSLOT_ADD] = "QXL_IO_MEMSLOT_ADD",
  532. [QXL_IO_MEMSLOT_DEL] = "QXL_IO_MEMSLOT_DEL",
  533. [QXL_IO_DETACH_PRIMARY] = "QXL_IO_DETACH_PRIMARY",
  534. [QXL_IO_ATTACH_PRIMARY] = "QXL_IO_ATTACH_PRIMARY",
  535. [QXL_IO_CREATE_PRIMARY] = "QXL_IO_CREATE_PRIMARY",
  536. [QXL_IO_DESTROY_PRIMARY] = "QXL_IO_DESTROY_PRIMARY",
  537. [QXL_IO_DESTROY_SURFACE_WAIT] = "QXL_IO_DESTROY_SURFACE_WAIT",
  538. [QXL_IO_DESTROY_ALL_SURFACES] = "QXL_IO_DESTROY_ALL_SURFACES",
  539. [QXL_IO_UPDATE_AREA_ASYNC] = "QXL_IO_UPDATE_AREA_ASYNC",
  540. [QXL_IO_MEMSLOT_ADD_ASYNC] = "QXL_IO_MEMSLOT_ADD_ASYNC",
  541. [QXL_IO_CREATE_PRIMARY_ASYNC] = "QXL_IO_CREATE_PRIMARY_ASYNC",
  542. [QXL_IO_DESTROY_PRIMARY_ASYNC] = "QXL_IO_DESTROY_PRIMARY_ASYNC",
  543. [QXL_IO_DESTROY_SURFACE_ASYNC] = "QXL_IO_DESTROY_SURFACE_ASYNC",
  544. [QXL_IO_DESTROY_ALL_SURFACES_ASYNC]
  545. = "QXL_IO_DESTROY_ALL_SURFACES_ASYNC",
  546. [QXL_IO_FLUSH_SURFACES_ASYNC] = "QXL_IO_FLUSH_SURFACES_ASYNC",
  547. [QXL_IO_FLUSH_RELEASE] = "QXL_IO_FLUSH_RELEASE",
  548. [QXL_IO_MONITORS_CONFIG_ASYNC] = "QXL_IO_MONITORS_CONFIG_ASYNC",
  549. };
  550. return io_port_to_string[io_port];
  551. }
  552. /* called from spice server thread context only */
  553. static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
  554. {
  555. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  556. SimpleSpiceUpdate *update;
  557. QXLCommandRing *ring;
  558. QXLCommand *cmd;
  559. int notify, ret;
  560. trace_qxl_ring_command_check(qxl->id, qxl_mode_to_string(qxl->mode));
  561. switch (qxl->mode) {
  562. case QXL_MODE_VGA:
  563. ret = false;
  564. qemu_mutex_lock(&qxl->ssd.lock);
  565. update = QTAILQ_FIRST(&qxl->ssd.updates);
  566. if (update != NULL) {
  567. QTAILQ_REMOVE(&qxl->ssd.updates, update, next);
  568. *ext = update->ext;
  569. ret = true;
  570. }
  571. qemu_mutex_unlock(&qxl->ssd.lock);
  572. if (ret) {
  573. trace_qxl_ring_command_get(qxl->id, qxl_mode_to_string(qxl->mode));
  574. qxl_log_command(qxl, "vga", ext);
  575. }
  576. return ret;
  577. case QXL_MODE_COMPAT:
  578. case QXL_MODE_NATIVE:
  579. case QXL_MODE_UNDEFINED:
  580. ring = &qxl->ram->cmd_ring;
  581. if (qxl->guest_bug || SPICE_RING_IS_EMPTY(ring)) {
  582. return false;
  583. }
  584. SPICE_RING_CONS_ITEM(qxl, ring, cmd);
  585. if (!cmd) {
  586. return false;
  587. }
  588. ext->cmd = *cmd;
  589. ext->group_id = MEMSLOT_GROUP_GUEST;
  590. ext->flags = qxl->cmdflags;
  591. SPICE_RING_POP(ring, notify);
  592. qxl_ring_set_dirty(qxl);
  593. if (notify) {
  594. qxl_send_events(qxl, QXL_INTERRUPT_DISPLAY);
  595. }
  596. qxl->guest_primary.commands++;
  597. qxl_track_command(qxl, ext);
  598. qxl_log_command(qxl, "cmd", ext);
  599. {
  600. /*
  601. * Windows 8 drivers place qxl commands in the vram
  602. * (instead of the ram) bar. We can't live migrate such a
  603. * guest, so add a migration blocker in case we detect
  604. * this, to avoid triggering the assert in pre_save().
  605. *
  606. * https://cgit.freedesktop.org/spice/win32/qxl-wddm-dod/commit/?id=f6e099db39e7d0787f294d5fd0dce328b5210faa
  607. */
  608. void *msg = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
  609. if (msg != NULL && (
  610. msg < (void *)qxl->vga.vram_ptr ||
  611. msg > ((void *)qxl->vga.vram_ptr + qxl->vga.vram_size))) {
  612. if (!qxl->migration_blocker) {
  613. Error *local_err = NULL;
  614. error_setg(&qxl->migration_blocker,
  615. "qxl: guest bug: command not in ram bar");
  616. migrate_add_blocker(qxl->migration_blocker, &local_err);
  617. if (local_err) {
  618. error_report_err(local_err);
  619. }
  620. }
  621. }
  622. }
  623. trace_qxl_ring_command_get(qxl->id, qxl_mode_to_string(qxl->mode));
  624. return true;
  625. default:
  626. return false;
  627. }
  628. }
  629. /* called from spice server thread context only */
  630. static int interface_req_cmd_notification(QXLInstance *sin)
  631. {
  632. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  633. int wait = 1;
  634. trace_qxl_ring_command_req_notification(qxl->id);
  635. switch (qxl->mode) {
  636. case QXL_MODE_COMPAT:
  637. case QXL_MODE_NATIVE:
  638. case QXL_MODE_UNDEFINED:
  639. SPICE_RING_CONS_WAIT(&qxl->ram->cmd_ring, wait);
  640. qxl_ring_set_dirty(qxl);
  641. break;
  642. default:
  643. /* nothing */
  644. break;
  645. }
  646. return wait;
  647. }
  648. /* called from spice server thread context only */
  649. static inline void qxl_push_free_res(PCIQXLDevice *d, int flush)
  650. {
  651. QXLReleaseRing *ring = &d->ram->release_ring;
  652. uint32_t prod;
  653. int notify;
  654. #define QXL_FREE_BUNCH_SIZE 32
  655. if (ring->prod - ring->cons + 1 == ring->num_items) {
  656. /* ring full -- can't push */
  657. return;
  658. }
  659. if (!flush && d->oom_running) {
  660. /* collect everything from oom handler before pushing */
  661. return;
  662. }
  663. if (!flush && d->num_free_res < QXL_FREE_BUNCH_SIZE) {
  664. /* collect a bit more before pushing */
  665. return;
  666. }
  667. SPICE_RING_PUSH(ring, notify);
  668. trace_qxl_ring_res_push(d->id, qxl_mode_to_string(d->mode),
  669. d->guest_surfaces.count, d->num_free_res,
  670. d->last_release, notify ? "yes" : "no");
  671. trace_qxl_ring_res_push_rest(d->id, ring->prod - ring->cons,
  672. ring->num_items, ring->prod, ring->cons);
  673. if (notify) {
  674. qxl_send_events(d, QXL_INTERRUPT_DISPLAY);
  675. }
  676. ring = &d->ram->release_ring;
  677. prod = ring->prod & SPICE_RING_INDEX_MASK(ring);
  678. if (prod >= ARRAY_SIZE(ring->items)) {
  679. qxl_set_guest_bug(d, "SPICE_RING_PROD_ITEM indices mismatch "
  680. "%u >= %zu", prod, ARRAY_SIZE(ring->items));
  681. return;
  682. }
  683. ring->items[prod].el = 0;
  684. d->num_free_res = 0;
  685. d->last_release = NULL;
  686. qxl_ring_set_dirty(d);
  687. }
  688. /* called from spice server thread context only */
  689. static void interface_release_resource(QXLInstance *sin,
  690. QXLReleaseInfoExt ext)
  691. {
  692. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  693. QXLReleaseRing *ring;
  694. uint32_t prod;
  695. uint64_t id;
  696. if (!ext.info) {
  697. return;
  698. }
  699. if (ext.group_id == MEMSLOT_GROUP_HOST) {
  700. /* host group -> vga mode update request */
  701. QXLCommandExt *cmdext = (void *)(intptr_t)(ext.info->id);
  702. SimpleSpiceUpdate *update;
  703. g_assert(cmdext->cmd.type == QXL_CMD_DRAW);
  704. update = container_of(cmdext, SimpleSpiceUpdate, ext);
  705. qemu_spice_destroy_update(&qxl->ssd, update);
  706. return;
  707. }
  708. /*
  709. * ext->info points into guest-visible memory
  710. * pci bar 0, $command.release_info
  711. */
  712. ring = &qxl->ram->release_ring;
  713. prod = ring->prod & SPICE_RING_INDEX_MASK(ring);
  714. if (prod >= ARRAY_SIZE(ring->items)) {
  715. qxl_set_guest_bug(qxl, "SPICE_RING_PROD_ITEM indices mismatch "
  716. "%u >= %zu", prod, ARRAY_SIZE(ring->items));
  717. return;
  718. }
  719. if (ring->items[prod].el == 0) {
  720. /* stick head into the ring */
  721. id = ext.info->id;
  722. ext.info->next = 0;
  723. qxl_ram_set_dirty(qxl, &ext.info->next);
  724. ring->items[prod].el = id;
  725. qxl_ring_set_dirty(qxl);
  726. } else {
  727. /* append item to the list */
  728. qxl->last_release->next = ext.info->id;
  729. qxl_ram_set_dirty(qxl, &qxl->last_release->next);
  730. ext.info->next = 0;
  731. qxl_ram_set_dirty(qxl, &ext.info->next);
  732. }
  733. qxl->last_release = ext.info;
  734. qxl->num_free_res++;
  735. trace_qxl_ring_res_put(qxl->id, qxl->num_free_res);
  736. qxl_push_free_res(qxl, 0);
  737. }
  738. /* called from spice server thread context only */
  739. static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
  740. {
  741. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  742. QXLCursorRing *ring;
  743. QXLCommand *cmd;
  744. int notify;
  745. trace_qxl_ring_cursor_check(qxl->id, qxl_mode_to_string(qxl->mode));
  746. switch (qxl->mode) {
  747. case QXL_MODE_COMPAT:
  748. case QXL_MODE_NATIVE:
  749. case QXL_MODE_UNDEFINED:
  750. ring = &qxl->ram->cursor_ring;
  751. if (SPICE_RING_IS_EMPTY(ring)) {
  752. return false;
  753. }
  754. SPICE_RING_CONS_ITEM(qxl, ring, cmd);
  755. if (!cmd) {
  756. return false;
  757. }
  758. ext->cmd = *cmd;
  759. ext->group_id = MEMSLOT_GROUP_GUEST;
  760. ext->flags = qxl->cmdflags;
  761. SPICE_RING_POP(ring, notify);
  762. qxl_ring_set_dirty(qxl);
  763. if (notify) {
  764. qxl_send_events(qxl, QXL_INTERRUPT_CURSOR);
  765. }
  766. qxl->guest_primary.commands++;
  767. qxl_track_command(qxl, ext);
  768. qxl_log_command(qxl, "csr", ext);
  769. if (qxl->have_vga) {
  770. qxl_render_cursor(qxl, ext);
  771. }
  772. trace_qxl_ring_cursor_get(qxl->id, qxl_mode_to_string(qxl->mode));
  773. return true;
  774. default:
  775. return false;
  776. }
  777. }
  778. /* called from spice server thread context only */
  779. static int interface_req_cursor_notification(QXLInstance *sin)
  780. {
  781. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  782. int wait = 1;
  783. trace_qxl_ring_cursor_req_notification(qxl->id);
  784. switch (qxl->mode) {
  785. case QXL_MODE_COMPAT:
  786. case QXL_MODE_NATIVE:
  787. case QXL_MODE_UNDEFINED:
  788. SPICE_RING_CONS_WAIT(&qxl->ram->cursor_ring, wait);
  789. qxl_ring_set_dirty(qxl);
  790. break;
  791. default:
  792. /* nothing */
  793. break;
  794. }
  795. return wait;
  796. }
  797. /* called from spice server thread context */
  798. static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
  799. {
  800. /*
  801. * Called by spice-server as a result of a QXL_CMD_UPDATE which is not in
  802. * use by xf86-video-qxl and is defined out in the qxl windows driver.
  803. * Probably was at some earlier version that is prior to git start (2009),
  804. * and is still guest trigerrable.
  805. */
  806. fprintf(stderr, "%s: deprecated\n", __func__);
  807. }
  808. /* called from spice server thread context only */
  809. static int interface_flush_resources(QXLInstance *sin)
  810. {
  811. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  812. int ret;
  813. ret = qxl->num_free_res;
  814. if (ret) {
  815. qxl_push_free_res(qxl, 1);
  816. }
  817. return ret;
  818. }
  819. static void qxl_create_guest_primary_complete(PCIQXLDevice *d);
  820. /* called from spice server thread context only */
  821. static void interface_async_complete_io(PCIQXLDevice *qxl, QXLCookie *cookie)
  822. {
  823. uint32_t current_async;
  824. qemu_mutex_lock(&qxl->async_lock);
  825. current_async = qxl->current_async;
  826. qxl->current_async = QXL_UNDEFINED_IO;
  827. qemu_mutex_unlock(&qxl->async_lock);
  828. trace_qxl_interface_async_complete_io(qxl->id, current_async, cookie);
  829. if (!cookie) {
  830. fprintf(stderr, "qxl: %s: error, cookie is NULL\n", __func__);
  831. return;
  832. }
  833. if (cookie && current_async != cookie->io) {
  834. fprintf(stderr,
  835. "qxl: %s: error: current_async = %d != %"
  836. PRId64 " = cookie->io\n", __func__, current_async, cookie->io);
  837. }
  838. switch (current_async) {
  839. case QXL_IO_MEMSLOT_ADD_ASYNC:
  840. case QXL_IO_DESTROY_PRIMARY_ASYNC:
  841. case QXL_IO_UPDATE_AREA_ASYNC:
  842. case QXL_IO_FLUSH_SURFACES_ASYNC:
  843. case QXL_IO_MONITORS_CONFIG_ASYNC:
  844. break;
  845. case QXL_IO_CREATE_PRIMARY_ASYNC:
  846. qxl_create_guest_primary_complete(qxl);
  847. break;
  848. case QXL_IO_DESTROY_ALL_SURFACES_ASYNC:
  849. qxl_spice_destroy_surfaces_complete(qxl);
  850. break;
  851. case QXL_IO_DESTROY_SURFACE_ASYNC:
  852. qxl_spice_destroy_surface_wait_complete(qxl, cookie->u.surface_id);
  853. break;
  854. default:
  855. fprintf(stderr, "qxl: %s: unexpected current_async %d\n", __func__,
  856. current_async);
  857. }
  858. qxl_send_events(qxl, QXL_INTERRUPT_IO_CMD);
  859. }
  860. /* called from spice server thread context only */
  861. static void interface_update_area_complete(QXLInstance *sin,
  862. uint32_t surface_id,
  863. QXLRect *dirty, uint32_t num_updated_rects)
  864. {
  865. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  866. int i;
  867. int qxl_i;
  868. QEMU_LOCK_GUARD(&qxl->ssd.lock);
  869. if (surface_id != 0 || !num_updated_rects ||
  870. !qxl->render_update_cookie_num) {
  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. return;
  890. }
  891. qxl_i = qxl->num_dirty_rects;
  892. for (i = 0; i < num_updated_rects; i++) {
  893. qxl->dirty[qxl_i++] = dirty[i];
  894. }
  895. qxl->num_dirty_rects += num_updated_rects;
  896. trace_qxl_interface_update_area_complete_schedule_bh(qxl->id,
  897. qxl->num_dirty_rects);
  898. qemu_bh_schedule(qxl->update_area_bh);
  899. }
  900. /* called from spice server thread context only */
  901. static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
  902. {
  903. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  904. QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
  905. switch (cookie->type) {
  906. case QXL_COOKIE_TYPE_IO:
  907. interface_async_complete_io(qxl, cookie);
  908. g_free(cookie);
  909. break;
  910. case QXL_COOKIE_TYPE_RENDER_UPDATE_AREA:
  911. qxl_render_update_area_done(qxl, cookie);
  912. break;
  913. case QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG:
  914. break;
  915. default:
  916. fprintf(stderr, "qxl: %s: unexpected cookie type %d\n",
  917. __func__, cookie->type);
  918. g_free(cookie);
  919. }
  920. }
  921. /* called from spice server thread context only */
  922. static void interface_set_client_capabilities(QXLInstance *sin,
  923. uint8_t client_present,
  924. uint8_t caps[58])
  925. {
  926. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  927. if (qxl->revision < 4) {
  928. trace_qxl_set_client_capabilities_unsupported_by_revision(qxl->id,
  929. qxl->revision);
  930. return;
  931. }
  932. if (runstate_check(RUN_STATE_INMIGRATE) ||
  933. runstate_check(RUN_STATE_POSTMIGRATE)) {
  934. return;
  935. }
  936. qxl->shadow_rom.client_present = client_present;
  937. memcpy(qxl->shadow_rom.client_capabilities, caps,
  938. sizeof(qxl->shadow_rom.client_capabilities));
  939. qxl->rom->client_present = client_present;
  940. memcpy(qxl->rom->client_capabilities, caps,
  941. sizeof(qxl->rom->client_capabilities));
  942. qxl_rom_set_dirty(qxl);
  943. qxl_send_events(qxl, QXL_INTERRUPT_CLIENT);
  944. }
  945. static bool qxl_rom_monitors_config_changed(QXLRom *rom,
  946. VDAgentMonitorsConfig *monitors_config,
  947. unsigned int max_outputs)
  948. {
  949. int i;
  950. unsigned int monitors_count;
  951. monitors_count = MIN(monitors_config->num_of_monitors, max_outputs);
  952. if (rom->client_monitors_config.count != monitors_count) {
  953. return true;
  954. }
  955. for (i = 0 ; i < rom->client_monitors_config.count ; ++i) {
  956. VDAgentMonConfig *monitor = &monitors_config->monitors[i];
  957. QXLURect *rect = &rom->client_monitors_config.heads[i];
  958. /* monitor->depth ignored */
  959. if ((rect->left != monitor->x) ||
  960. (rect->top != monitor->y) ||
  961. (rect->right != monitor->x + monitor->width) ||
  962. (rect->bottom != monitor->y + monitor->height)) {
  963. return true;
  964. }
  965. }
  966. return false;
  967. }
  968. /* called from main context only */
  969. static int interface_client_monitors_config(QXLInstance *sin,
  970. VDAgentMonitorsConfig *monitors_config)
  971. {
  972. PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
  973. QXLRom *rom = memory_region_get_ram_ptr(&qxl->rom_bar);
  974. int i;
  975. unsigned max_outputs = ARRAY_SIZE(rom->client_monitors_config.heads);
  976. bool config_changed = false;
  977. if (qxl->revision < 4) {
  978. trace_qxl_client_monitors_config_unsupported_by_device(qxl->id,
  979. qxl->revision);
  980. return 0;
  981. }
  982. /*
  983. * Older windows drivers set int_mask to 0 when their ISR is called,
  984. * then later set it to ~0. So it doesn't relate to the actual interrupts
  985. * handled. However, they are old, so clearly they don't support this
  986. * interrupt
  987. */
  988. if (qxl->ram->int_mask == 0 || qxl->ram->int_mask == ~0 ||
  989. !(qxl->ram->int_mask & QXL_INTERRUPT_CLIENT_MONITORS_CONFIG)) {
  990. trace_qxl_client_monitors_config_unsupported_by_guest(qxl->id,
  991. qxl->ram->int_mask,
  992. monitors_config);
  993. return 0;
  994. }
  995. if (!monitors_config) {
  996. return 1;
  997. }
  998. #if SPICE_SERVER_VERSION >= 0x000c06 /* release 0.12.6 */
  999. /* limit number of outputs based on setting limit */
  1000. if (qxl->max_outputs && qxl->max_outputs <= max_outputs) {
  1001. max_outputs = qxl->max_outputs;
  1002. }
  1003. #endif
  1004. config_changed = qxl_rom_monitors_config_changed(rom,
  1005. monitors_config,
  1006. max_outputs);
  1007. memset(&rom->client_monitors_config, 0,
  1008. sizeof(rom->client_monitors_config));
  1009. rom->client_monitors_config.count = monitors_config->num_of_monitors;
  1010. /* monitors_config->flags ignored */
  1011. if (rom->client_monitors_config.count >= max_outputs) {
  1012. trace_qxl_client_monitors_config_capped(qxl->id,
  1013. monitors_config->num_of_monitors,
  1014. max_outputs);
  1015. rom->client_monitors_config.count = max_outputs;
  1016. }
  1017. for (i = 0 ; i < rom->client_monitors_config.count ; ++i) {
  1018. VDAgentMonConfig *monitor = &monitors_config->monitors[i];
  1019. QXLURect *rect = &rom->client_monitors_config.heads[i];
  1020. /* monitor->depth ignored */
  1021. rect->left = monitor->x;
  1022. rect->top = monitor->y;
  1023. rect->right = monitor->x + monitor->width;
  1024. rect->bottom = monitor->y + monitor->height;
  1025. }
  1026. rom->client_monitors_config_crc = qxl_crc32(
  1027. (const uint8_t *)&rom->client_monitors_config,
  1028. sizeof(rom->client_monitors_config));
  1029. trace_qxl_client_monitors_config_crc(qxl->id,
  1030. sizeof(rom->client_monitors_config),
  1031. rom->client_monitors_config_crc);
  1032. trace_qxl_interrupt_client_monitors_config(qxl->id,
  1033. rom->client_monitors_config.count,
  1034. rom->client_monitors_config.heads);
  1035. if (config_changed) {
  1036. qxl_send_events(qxl, QXL_INTERRUPT_CLIENT_MONITORS_CONFIG);
  1037. }
  1038. return 1;
  1039. }
  1040. static const QXLInterface qxl_interface = {
  1041. .base.type = SPICE_INTERFACE_QXL,
  1042. .base.description = "qxl gpu",
  1043. .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
  1044. .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
  1045. .attache_worker = interface_attach_worker,
  1046. .set_compression_level = interface_set_compression_level,
  1047. #if SPICE_NEEDS_SET_MM_TIME
  1048. .set_mm_time = interface_set_mm_time,
  1049. #endif
  1050. .get_init_info = interface_get_init_info,
  1051. /* the callbacks below are called from spice server thread context */
  1052. .get_command = interface_get_command,
  1053. .req_cmd_notification = interface_req_cmd_notification,
  1054. .release_resource = interface_release_resource,
  1055. .get_cursor_command = interface_get_cursor_command,
  1056. .req_cursor_notification = interface_req_cursor_notification,
  1057. .notify_update = interface_notify_update,
  1058. .flush_resources = interface_flush_resources,
  1059. .async_complete = interface_async_complete,
  1060. .update_area_complete = interface_update_area_complete,
  1061. .set_client_capabilities = interface_set_client_capabilities,
  1062. .client_monitors_config = interface_client_monitors_config,
  1063. };
  1064. static const GraphicHwOps qxl_ops = {
  1065. .gfx_update = qxl_hw_update,
  1066. .gfx_update_async = true,
  1067. };
  1068. static void qxl_enter_vga_mode(PCIQXLDevice *d)
  1069. {
  1070. if (d->mode == QXL_MODE_VGA) {
  1071. return;
  1072. }
  1073. trace_qxl_enter_vga_mode(d->id);
  1074. spice_qxl_driver_unload(&d->ssd.qxl);
  1075. graphic_console_set_hwops(d->ssd.dcl.con, d->vga.hw_ops, &d->vga);
  1076. update_displaychangelistener(&d->ssd.dcl, GUI_REFRESH_INTERVAL_DEFAULT);
  1077. qemu_spice_create_host_primary(&d->ssd);
  1078. d->mode = QXL_MODE_VGA;
  1079. qemu_spice_display_switch(&d->ssd, d->ssd.ds);
  1080. vga_dirty_log_start(&d->vga);
  1081. graphic_hw_update(d->vga.con);
  1082. }
  1083. static void qxl_exit_vga_mode(PCIQXLDevice *d)
  1084. {
  1085. if (d->mode != QXL_MODE_VGA) {
  1086. return;
  1087. }
  1088. trace_qxl_exit_vga_mode(d->id);
  1089. graphic_console_set_hwops(d->ssd.dcl.con, &qxl_ops, d);
  1090. update_displaychangelistener(&d->ssd.dcl, GUI_REFRESH_INTERVAL_IDLE);
  1091. vga_dirty_log_stop(&d->vga);
  1092. qxl_destroy_primary(d, QXL_SYNC);
  1093. }
  1094. static void qxl_update_irq(PCIQXLDevice *d)
  1095. {
  1096. uint32_t pending = le32_to_cpu(d->ram->int_pending);
  1097. uint32_t mask = le32_to_cpu(d->ram->int_mask);
  1098. int level = !!(pending & mask);
  1099. pci_set_irq(&d->pci, level);
  1100. qxl_ring_set_dirty(d);
  1101. }
  1102. static void qxl_check_state(PCIQXLDevice *d)
  1103. {
  1104. QXLRam *ram = d->ram;
  1105. int spice_display_running = qemu_spice_display_is_running(&d->ssd);
  1106. assert(!spice_display_running || SPICE_RING_IS_EMPTY(&ram->cmd_ring));
  1107. assert(!spice_display_running || SPICE_RING_IS_EMPTY(&ram->cursor_ring));
  1108. }
  1109. static void qxl_reset_state(PCIQXLDevice *d)
  1110. {
  1111. QXLRom *rom = d->rom;
  1112. qxl_check_state(d);
  1113. d->shadow_rom.update_id = cpu_to_le32(0);
  1114. *rom = d->shadow_rom;
  1115. qxl_rom_set_dirty(d);
  1116. init_qxl_ram(d);
  1117. d->num_free_res = 0;
  1118. d->last_release = NULL;
  1119. memset(&d->ssd.dirty, 0, sizeof(d->ssd.dirty));
  1120. qxl_update_irq(d);
  1121. }
  1122. static void qxl_soft_reset(PCIQXLDevice *d)
  1123. {
  1124. trace_qxl_soft_reset(d->id);
  1125. qxl_check_state(d);
  1126. qxl_clear_guest_bug(d);
  1127. qemu_mutex_lock(&d->async_lock);
  1128. d->current_async = QXL_UNDEFINED_IO;
  1129. qemu_mutex_unlock(&d->async_lock);
  1130. if (d->have_vga) {
  1131. qxl_enter_vga_mode(d);
  1132. } else {
  1133. d->mode = QXL_MODE_UNDEFINED;
  1134. }
  1135. }
  1136. static void qxl_hard_reset(PCIQXLDevice *d, int loadvm)
  1137. {
  1138. bool startstop = qemu_spice_display_is_running(&d->ssd);
  1139. trace_qxl_hard_reset(d->id, loadvm);
  1140. if (startstop) {
  1141. qemu_spice_display_stop();
  1142. }
  1143. qxl_spice_reset_cursor(d);
  1144. qxl_spice_reset_image_cache(d);
  1145. qxl_reset_surfaces(d);
  1146. qxl_reset_memslots(d);
  1147. /* pre loadvm reset must not touch QXLRam. This lives in
  1148. * device memory, is migrated together with RAM and thus
  1149. * already loaded at this point */
  1150. if (!loadvm) {
  1151. qxl_reset_state(d);
  1152. }
  1153. qemu_spice_create_host_memslot(&d->ssd);
  1154. qxl_soft_reset(d);
  1155. if (d->migration_blocker) {
  1156. migrate_del_blocker(d->migration_blocker);
  1157. error_free(d->migration_blocker);
  1158. d->migration_blocker = NULL;
  1159. }
  1160. if (startstop) {
  1161. qemu_spice_display_start();
  1162. }
  1163. }
  1164. static void qxl_reset_handler(DeviceState *dev)
  1165. {
  1166. PCIQXLDevice *d = PCI_QXL(PCI_DEVICE(dev));
  1167. qxl_hard_reset(d, 0);
  1168. }
  1169. static void qxl_vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
  1170. {
  1171. VGACommonState *vga = opaque;
  1172. PCIQXLDevice *qxl = container_of(vga, PCIQXLDevice, vga);
  1173. trace_qxl_io_write_vga(qxl->id, qxl_mode_to_string(qxl->mode), addr, val);
  1174. if (qxl->mode != QXL_MODE_VGA &&
  1175. qxl->revision <= QXL_REVISION_STABLE_V12) {
  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;
  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. WITH_QEMU_LOCK_GUARD(&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. return;
  1525. }
  1526. d->current_async = orig_io_port;
  1527. }
  1528. break;
  1529. default:
  1530. break;
  1531. }
  1532. trace_qxl_io_write(d->id, qxl_mode_to_string(d->mode),
  1533. addr, io_port_to_string(addr),
  1534. val, size, async);
  1535. switch (io_port) {
  1536. case QXL_IO_UPDATE_AREA:
  1537. {
  1538. QXLCookie *cookie = NULL;
  1539. QXLRect update = d->ram->update_area;
  1540. if (d->ram->update_surface > d->ssd.num_surfaces) {
  1541. qxl_set_guest_bug(d, "QXL_IO_UPDATE_AREA: invalid surface id %d\n",
  1542. d->ram->update_surface);
  1543. break;
  1544. }
  1545. if (update.left >= update.right || update.top >= update.bottom ||
  1546. update.left < 0 || update.top < 0) {
  1547. qxl_set_guest_bug(d,
  1548. "QXL_IO_UPDATE_AREA: invalid area (%ux%u)x(%ux%u)\n",
  1549. update.left, update.top, update.right, update.bottom);
  1550. if (update.left == update.right || update.top == update.bottom) {
  1551. /* old drivers may provide empty area, keep going */
  1552. qxl_clear_guest_bug(d);
  1553. goto cancel_async;
  1554. }
  1555. break;
  1556. }
  1557. if (async == QXL_ASYNC) {
  1558. cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
  1559. QXL_IO_UPDATE_AREA_ASYNC);
  1560. cookie->u.area = update;
  1561. }
  1562. qxl_spice_update_area(d, d->ram->update_surface,
  1563. cookie ? &cookie->u.area : &update,
  1564. NULL, 0, 0, async, cookie);
  1565. break;
  1566. }
  1567. case QXL_IO_NOTIFY_CMD:
  1568. qemu_spice_wakeup(&d->ssd);
  1569. break;
  1570. case QXL_IO_NOTIFY_CURSOR:
  1571. qemu_spice_wakeup(&d->ssd);
  1572. break;
  1573. case QXL_IO_UPDATE_IRQ:
  1574. qxl_update_irq(d);
  1575. break;
  1576. case QXL_IO_NOTIFY_OOM:
  1577. if (!SPICE_RING_IS_EMPTY(&d->ram->release_ring)) {
  1578. break;
  1579. }
  1580. d->oom_running = 1;
  1581. qxl_spice_oom(d);
  1582. d->oom_running = 0;
  1583. break;
  1584. case QXL_IO_SET_MODE:
  1585. qxl_set_mode(d, val, 0);
  1586. break;
  1587. case QXL_IO_LOG:
  1588. #ifdef CONFIG_MODULES
  1589. /*
  1590. * FIXME
  1591. * trace_event_get_state_backends() does not work for modules,
  1592. * it leads to "undefined symbol: qemu_qxl_io_log_semaphore"
  1593. */
  1594. if (true) {
  1595. #else
  1596. if (trace_event_get_state_backends(TRACE_QXL_IO_LOG) || d->guestdebug) {
  1597. #endif
  1598. /* We cannot trust the guest to NUL terminate d->ram->log_buf */
  1599. char *log_buf = g_strndup((const char *)d->ram->log_buf,
  1600. sizeof(d->ram->log_buf));
  1601. trace_qxl_io_log(d->id, log_buf);
  1602. if (d->guestdebug) {
  1603. fprintf(stderr, "qxl/guest-%d: %" PRId64 ": %s", d->id,
  1604. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), log_buf);
  1605. }
  1606. g_free(log_buf);
  1607. }
  1608. break;
  1609. case QXL_IO_RESET:
  1610. qxl_hard_reset(d, 0);
  1611. break;
  1612. case QXL_IO_MEMSLOT_ADD:
  1613. if (val >= NUM_MEMSLOTS) {
  1614. qxl_set_guest_bug(d, "QXL_IO_MEMSLOT_ADD: val out of range");
  1615. break;
  1616. }
  1617. if (d->guest_slots[val].active) {
  1618. qxl_set_guest_bug(d,
  1619. "QXL_IO_MEMSLOT_ADD: memory slot already active");
  1620. break;
  1621. }
  1622. d->guest_slots[val].slot = d->ram->mem_slot;
  1623. qxl_add_memslot(d, val, 0, async);
  1624. break;
  1625. case QXL_IO_MEMSLOT_DEL:
  1626. if (val >= NUM_MEMSLOTS) {
  1627. qxl_set_guest_bug(d, "QXL_IO_MEMSLOT_DEL: val out of range");
  1628. break;
  1629. }
  1630. qxl_del_memslot(d, val);
  1631. break;
  1632. case QXL_IO_CREATE_PRIMARY:
  1633. if (val != 0) {
  1634. qxl_set_guest_bug(d, "QXL_IO_CREATE_PRIMARY (async=%d): val != 0",
  1635. async);
  1636. goto cancel_async;
  1637. }
  1638. d->guest_primary.surface = d->ram->create_surface;
  1639. qxl_create_guest_primary(d, 0, async);
  1640. break;
  1641. case QXL_IO_DESTROY_PRIMARY:
  1642. if (val != 0) {
  1643. qxl_set_guest_bug(d, "QXL_IO_DESTROY_PRIMARY (async=%d): val != 0",
  1644. async);
  1645. goto cancel_async;
  1646. }
  1647. if (!qxl_destroy_primary(d, async)) {
  1648. trace_qxl_io_destroy_primary_ignored(d->id,
  1649. qxl_mode_to_string(d->mode));
  1650. goto cancel_async;
  1651. }
  1652. break;
  1653. case QXL_IO_DESTROY_SURFACE_WAIT:
  1654. if (val >= d->ssd.num_surfaces) {
  1655. qxl_set_guest_bug(d, "QXL_IO_DESTROY_SURFACE (async=%d):"
  1656. "%" PRIu64 " >= NUM_SURFACES", async, val);
  1657. goto cancel_async;
  1658. }
  1659. qxl_spice_destroy_surface_wait(d, val, async);
  1660. break;
  1661. case QXL_IO_FLUSH_RELEASE: {
  1662. QXLReleaseRing *ring = &d->ram->release_ring;
  1663. if (ring->prod - ring->cons + 1 == ring->num_items) {
  1664. fprintf(stderr,
  1665. "ERROR: no flush, full release ring [p%d,%dc]\n",
  1666. ring->prod, ring->cons);
  1667. }
  1668. qxl_push_free_res(d, 1 /* flush */);
  1669. break;
  1670. }
  1671. case QXL_IO_FLUSH_SURFACES_ASYNC:
  1672. qxl_spice_flush_surfaces_async(d);
  1673. break;
  1674. case QXL_IO_DESTROY_ALL_SURFACES:
  1675. d->mode = QXL_MODE_UNDEFINED;
  1676. qxl_spice_destroy_surfaces(d, async);
  1677. break;
  1678. case QXL_IO_MONITORS_CONFIG_ASYNC:
  1679. qxl_spice_monitors_config_async(d, 0);
  1680. break;
  1681. default:
  1682. qxl_set_guest_bug(d, "%s: unexpected ioport=0x%x\n", __func__, io_port);
  1683. }
  1684. return;
  1685. cancel_async:
  1686. if (async) {
  1687. qxl_send_events(d, QXL_INTERRUPT_IO_CMD);
  1688. qemu_mutex_lock(&d->async_lock);
  1689. d->current_async = QXL_UNDEFINED_IO;
  1690. qemu_mutex_unlock(&d->async_lock);
  1691. }
  1692. }
  1693. static uint64_t ioport_read(void *opaque, hwaddr addr,
  1694. unsigned size)
  1695. {
  1696. PCIQXLDevice *qxl = opaque;
  1697. trace_qxl_io_read_unexpected(qxl->id);
  1698. return 0xff;
  1699. }
  1700. static const MemoryRegionOps qxl_io_ops = {
  1701. .read = ioport_read,
  1702. .write = ioport_write,
  1703. .valid = {
  1704. .min_access_size = 1,
  1705. .max_access_size = 1,
  1706. },
  1707. };
  1708. static void qxl_update_irq_bh(void *opaque)
  1709. {
  1710. PCIQXLDevice *d = opaque;
  1711. qxl_update_irq(d);
  1712. }
  1713. static void qxl_send_events(PCIQXLDevice *d, uint32_t events)
  1714. {
  1715. uint32_t old_pending;
  1716. uint32_t le_events = cpu_to_le32(events);
  1717. trace_qxl_send_events(d->id, events);
  1718. if (!qemu_spice_display_is_running(&d->ssd)) {
  1719. /* spice-server tracks guest running state and should not do this */
  1720. fprintf(stderr, "%s: spice-server bug: guest stopped, ignoring\n",
  1721. __func__);
  1722. trace_qxl_send_events_vm_stopped(d->id, events);
  1723. return;
  1724. }
  1725. /*
  1726. * Older versions of Spice forgot to define the QXLRam struct
  1727. * with the '__aligned__(4)' attribute. clang 7 and newer will
  1728. * thus warn that atomic_fetch_or(&d->ram->int_pending, ...)
  1729. * might be a misaligned atomic access, and will generate an
  1730. * out-of-line call for it, which results in a link error since
  1731. * we don't currently link against libatomic.
  1732. *
  1733. * In fact we set up d->ram in init_qxl_ram() so it always starts
  1734. * at a 4K boundary, so we know that &d->ram->int_pending is
  1735. * naturally aligned for a uint32_t. Newer Spice versions
  1736. * (with Spice commit beda5ec7a6848be20c0cac2a9a8ef2a41e8069c1)
  1737. * will fix the bug directly. To deal with older versions,
  1738. * we tell the compiler to assume the address really is aligned.
  1739. * Any compiler which cares about the misalignment will have
  1740. * __builtin_assume_aligned.
  1741. */
  1742. #ifdef HAS_ASSUME_ALIGNED
  1743. #define ALIGNED_UINT32_PTR(P) ((uint32_t *)__builtin_assume_aligned(P, 4))
  1744. #else
  1745. #define ALIGNED_UINT32_PTR(P) ((uint32_t *)P)
  1746. #endif
  1747. old_pending = atomic_fetch_or(ALIGNED_UINT32_PTR(&d->ram->int_pending),
  1748. le_events);
  1749. if ((old_pending & le_events) == le_events) {
  1750. return;
  1751. }
  1752. qemu_bh_schedule(d->update_irq);
  1753. }
  1754. /* graphics console */
  1755. static void qxl_hw_update(void *opaque)
  1756. {
  1757. PCIQXLDevice *qxl = opaque;
  1758. qxl_render_update(qxl);
  1759. }
  1760. static void qxl_dirty_one_surface(PCIQXLDevice *qxl, QXLPHYSICAL pqxl,
  1761. uint32_t height, int32_t stride)
  1762. {
  1763. uint64_t offset, size;
  1764. uint32_t slot;
  1765. bool rc;
  1766. rc = qxl_get_check_slot_offset(qxl, pqxl, &slot, &offset);
  1767. assert(rc == true);
  1768. size = (uint64_t)height * abs(stride);
  1769. trace_qxl_surfaces_dirty(qxl->id, offset, size);
  1770. qxl_set_dirty(qxl->guest_slots[slot].mr,
  1771. qxl->guest_slots[slot].offset + offset,
  1772. qxl->guest_slots[slot].offset + offset + size);
  1773. }
  1774. static void qxl_dirty_surfaces(PCIQXLDevice *qxl)
  1775. {
  1776. int i;
  1777. if (qxl->mode != QXL_MODE_NATIVE && qxl->mode != QXL_MODE_COMPAT) {
  1778. return;
  1779. }
  1780. /* dirty the primary surface */
  1781. qxl_dirty_one_surface(qxl, qxl->guest_primary.surface.mem,
  1782. qxl->guest_primary.surface.height,
  1783. qxl->guest_primary.surface.stride);
  1784. /* dirty the off-screen surfaces */
  1785. for (i = 0; i < qxl->ssd.num_surfaces; i++) {
  1786. QXLSurfaceCmd *cmd;
  1787. if (qxl->guest_surfaces.cmds[i] == 0) {
  1788. continue;
  1789. }
  1790. cmd = qxl_phys2virt(qxl, qxl->guest_surfaces.cmds[i],
  1791. MEMSLOT_GROUP_GUEST);
  1792. assert(cmd);
  1793. assert(cmd->type == QXL_SURFACE_CMD_CREATE);
  1794. qxl_dirty_one_surface(qxl, cmd->u.surface_create.data,
  1795. cmd->u.surface_create.height,
  1796. cmd->u.surface_create.stride);
  1797. }
  1798. }
  1799. static void qxl_vm_change_state_handler(void *opaque, int running,
  1800. RunState state)
  1801. {
  1802. PCIQXLDevice *qxl = opaque;
  1803. if (running) {
  1804. /*
  1805. * if qxl_send_events was called from spice server context before
  1806. * migration ended, qxl_update_irq for these events might not have been
  1807. * called
  1808. */
  1809. qxl_update_irq(qxl);
  1810. } else {
  1811. /* make sure surfaces are saved before migration */
  1812. qxl_dirty_surfaces(qxl);
  1813. }
  1814. }
  1815. /* display change listener */
  1816. static void display_update(DisplayChangeListener *dcl,
  1817. int x, int y, int w, int h)
  1818. {
  1819. PCIQXLDevice *qxl = container_of(dcl, PCIQXLDevice, ssd.dcl);
  1820. if (qxl->mode == QXL_MODE_VGA) {
  1821. qemu_spice_display_update(&qxl->ssd, x, y, w, h);
  1822. }
  1823. }
  1824. static void display_switch(DisplayChangeListener *dcl,
  1825. struct DisplaySurface *surface)
  1826. {
  1827. PCIQXLDevice *qxl = container_of(dcl, PCIQXLDevice, ssd.dcl);
  1828. qxl->ssd.ds = surface;
  1829. if (qxl->mode == QXL_MODE_VGA) {
  1830. qemu_spice_display_switch(&qxl->ssd, surface);
  1831. }
  1832. }
  1833. static void display_refresh(DisplayChangeListener *dcl)
  1834. {
  1835. PCIQXLDevice *qxl = container_of(dcl, PCIQXLDevice, ssd.dcl);
  1836. if (qxl->mode == QXL_MODE_VGA) {
  1837. qemu_spice_display_refresh(&qxl->ssd);
  1838. }
  1839. }
  1840. static DisplayChangeListenerOps display_listener_ops = {
  1841. .dpy_name = "spice/qxl",
  1842. .dpy_gfx_update = display_update,
  1843. .dpy_gfx_switch = display_switch,
  1844. .dpy_refresh = display_refresh,
  1845. };
  1846. static void qxl_init_ramsize(PCIQXLDevice *qxl)
  1847. {
  1848. /* vga mode framebuffer / primary surface (bar 0, first part) */
  1849. if (qxl->vgamem_size_mb < 8) {
  1850. qxl->vgamem_size_mb = 8;
  1851. }
  1852. /* XXX: we round vgamem_size_mb up to a nearest power of two and it must be
  1853. * less than vga_common_init()'s maximum on qxl->vga.vram_size (512 now).
  1854. */
  1855. if (qxl->vgamem_size_mb > 256) {
  1856. qxl->vgamem_size_mb = 256;
  1857. }
  1858. qxl->vgamem_size = qxl->vgamem_size_mb * MiB;
  1859. /* vga ram (bar 0, total) */
  1860. if (qxl->ram_size_mb != -1) {
  1861. qxl->vga.vram_size = qxl->ram_size_mb * MiB;
  1862. }
  1863. if (qxl->vga.vram_size < qxl->vgamem_size * 2) {
  1864. qxl->vga.vram_size = qxl->vgamem_size * 2;
  1865. }
  1866. /* vram32 (surfaces, 32bit, bar 1) */
  1867. if (qxl->vram32_size_mb != -1) {
  1868. qxl->vram32_size = qxl->vram32_size_mb * MiB;
  1869. }
  1870. if (qxl->vram32_size < 4096) {
  1871. qxl->vram32_size = 4096;
  1872. }
  1873. /* vram (surfaces, 64bit, bar 4+5) */
  1874. if (qxl->vram_size_mb != -1) {
  1875. qxl->vram_size = (uint64_t)qxl->vram_size_mb * MiB;
  1876. }
  1877. if (qxl->vram_size < qxl->vram32_size) {
  1878. qxl->vram_size = qxl->vram32_size;
  1879. }
  1880. if (qxl->revision == 1) {
  1881. qxl->vram32_size = 4096;
  1882. qxl->vram_size = 4096;
  1883. }
  1884. qxl->vgamem_size = pow2ceil(qxl->vgamem_size);
  1885. qxl->vga.vram_size = pow2ceil(qxl->vga.vram_size);
  1886. qxl->vram32_size = pow2ceil(qxl->vram32_size);
  1887. qxl->vram_size = pow2ceil(qxl->vram_size);
  1888. }
  1889. static void qxl_realize_common(PCIQXLDevice *qxl, Error **errp)
  1890. {
  1891. uint8_t* config = qxl->pci.config;
  1892. uint32_t pci_device_rev;
  1893. uint32_t io_size;
  1894. qemu_spice_display_init_common(&qxl->ssd);
  1895. qxl->mode = QXL_MODE_UNDEFINED;
  1896. qxl->num_memslots = NUM_MEMSLOTS;
  1897. qemu_mutex_init(&qxl->track_lock);
  1898. qemu_mutex_init(&qxl->async_lock);
  1899. qxl->current_async = QXL_UNDEFINED_IO;
  1900. qxl->guest_bug = 0;
  1901. switch (qxl->revision) {
  1902. case 1: /* spice 0.4 -- qxl-1 */
  1903. pci_device_rev = QXL_REVISION_STABLE_V04;
  1904. io_size = 8;
  1905. break;
  1906. case 2: /* spice 0.6 -- qxl-2 */
  1907. pci_device_rev = QXL_REVISION_STABLE_V06;
  1908. io_size = 16;
  1909. break;
  1910. case 3: /* qxl-3 */
  1911. pci_device_rev = QXL_REVISION_STABLE_V10;
  1912. io_size = 32; /* PCI region size must be pow2 */
  1913. break;
  1914. case 4: /* qxl-4 */
  1915. pci_device_rev = QXL_REVISION_STABLE_V12;
  1916. io_size = pow2ceil(QXL_IO_RANGE_SIZE);
  1917. break;
  1918. case 5: /* qxl-5 */
  1919. pci_device_rev = QXL_REVISION_STABLE_V12 + 1;
  1920. io_size = pow2ceil(QXL_IO_RANGE_SIZE);
  1921. break;
  1922. default:
  1923. error_setg(errp, "Invalid revision %d for qxl device (max %d)",
  1924. qxl->revision, QXL_DEFAULT_REVISION);
  1925. return;
  1926. }
  1927. pci_set_byte(&config[PCI_REVISION_ID], pci_device_rev);
  1928. pci_set_byte(&config[PCI_INTERRUPT_PIN], 1);
  1929. qxl->rom_size = qxl_rom_size();
  1930. memory_region_init_rom(&qxl->rom_bar, OBJECT(qxl), "qxl.vrom",
  1931. qxl->rom_size, &error_fatal);
  1932. init_qxl_rom(qxl);
  1933. init_qxl_ram(qxl);
  1934. qxl->guest_surfaces.cmds = g_new0(QXLPHYSICAL, qxl->ssd.num_surfaces);
  1935. memory_region_init_ram(&qxl->vram_bar, OBJECT(qxl), "qxl.vram",
  1936. qxl->vram_size, &error_fatal);
  1937. memory_region_init_alias(&qxl->vram32_bar, OBJECT(qxl), "qxl.vram32",
  1938. &qxl->vram_bar, 0, qxl->vram32_size);
  1939. memory_region_init_io(&qxl->io_bar, OBJECT(qxl), &qxl_io_ops, qxl,
  1940. "qxl-ioports", io_size);
  1941. if (qxl->have_vga) {
  1942. vga_dirty_log_start(&qxl->vga);
  1943. }
  1944. memory_region_set_flush_coalesced(&qxl->io_bar);
  1945. pci_register_bar(&qxl->pci, QXL_IO_RANGE_INDEX,
  1946. PCI_BASE_ADDRESS_SPACE_IO, &qxl->io_bar);
  1947. pci_register_bar(&qxl->pci, QXL_ROM_RANGE_INDEX,
  1948. PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->rom_bar);
  1949. pci_register_bar(&qxl->pci, QXL_RAM_RANGE_INDEX,
  1950. PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->vga.vram);
  1951. pci_register_bar(&qxl->pci, QXL_VRAM_RANGE_INDEX,
  1952. PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->vram32_bar);
  1953. if (qxl->vram32_size < qxl->vram_size) {
  1954. /*
  1955. * Make the 64bit vram bar show up only in case it is
  1956. * configured to be larger than the 32bit vram bar.
  1957. */
  1958. pci_register_bar(&qxl->pci, QXL_VRAM64_RANGE_INDEX,
  1959. PCI_BASE_ADDRESS_SPACE_MEMORY |
  1960. PCI_BASE_ADDRESS_MEM_TYPE_64 |
  1961. PCI_BASE_ADDRESS_MEM_PREFETCH,
  1962. &qxl->vram_bar);
  1963. }
  1964. /* print pci bar details */
  1965. dprint(qxl, 1, "ram/%s: %" PRId64 " MB [region 0]\n",
  1966. qxl->have_vga ? "pri" : "sec", qxl->vga.vram_size / MiB);
  1967. dprint(qxl, 1, "vram/32: %" PRIx64 " MB [region 1]\n",
  1968. qxl->vram32_size / MiB);
  1969. dprint(qxl, 1, "vram/64: %" PRIx64 " MB %s\n",
  1970. qxl->vram_size / MiB,
  1971. qxl->vram32_size < qxl->vram_size ? "[region 4]" : "[unmapped]");
  1972. qxl->ssd.qxl.base.sif = &qxl_interface.base;
  1973. if (qemu_spice_add_display_interface(&qxl->ssd.qxl, qxl->vga.con) != 0) {
  1974. error_setg(errp, "qxl interface %d.%d not supported by spice-server",
  1975. SPICE_INTERFACE_QXL_MAJOR, SPICE_INTERFACE_QXL_MINOR);
  1976. return;
  1977. }
  1978. #if SPICE_SERVER_VERSION >= 0x000e02 /* release 0.14.2 */
  1979. char device_address[256] = "";
  1980. if (qemu_spice_fill_device_address(qxl->vga.con, device_address, 256)) {
  1981. spice_qxl_set_device_info(&qxl->ssd.qxl,
  1982. device_address,
  1983. 0,
  1984. qxl->max_outputs);
  1985. }
  1986. #endif
  1987. qemu_add_vm_change_state_handler(qxl_vm_change_state_handler, qxl);
  1988. qxl->update_irq = qemu_bh_new(qxl_update_irq_bh, qxl);
  1989. qxl_reset_state(qxl);
  1990. qxl->update_area_bh = qemu_bh_new(qxl_render_update_area_bh, qxl);
  1991. qxl->ssd.cursor_bh = qemu_bh_new(qemu_spice_cursor_refresh_bh, &qxl->ssd);
  1992. }
  1993. static void qxl_realize_primary(PCIDevice *dev, Error **errp)
  1994. {
  1995. PCIQXLDevice *qxl = PCI_QXL(dev);
  1996. VGACommonState *vga = &qxl->vga;
  1997. Error *local_err = NULL;
  1998. qxl_init_ramsize(qxl);
  1999. vga->vbe_size = qxl->vgamem_size;
  2000. vga->vram_size_mb = qxl->vga.vram_size / MiB;
  2001. vga_common_init(vga, OBJECT(dev));
  2002. vga_init(vga, OBJECT(dev),
  2003. pci_address_space(dev), pci_address_space_io(dev), false);
  2004. portio_list_init(&qxl->vga_port_list, OBJECT(dev), qxl_vga_portio_list,
  2005. vga, "vga");
  2006. portio_list_set_flush_coalesced(&qxl->vga_port_list);
  2007. portio_list_add(&qxl->vga_port_list, pci_address_space_io(dev), 0x3b0);
  2008. qxl->have_vga = true;
  2009. vga->con = graphic_console_init(DEVICE(dev), 0, &qxl_ops, qxl);
  2010. qxl->id = qemu_console_get_index(vga->con); /* == channel_id */
  2011. if (qxl->id != 0) {
  2012. error_setg(errp, "primary qxl-vga device must be console 0 "
  2013. "(first display device on the command line)");
  2014. return;
  2015. }
  2016. qxl_realize_common(qxl, &local_err);
  2017. if (local_err) {
  2018. error_propagate(errp, local_err);
  2019. return;
  2020. }
  2021. qxl->ssd.dcl.ops = &display_listener_ops;
  2022. qxl->ssd.dcl.con = vga->con;
  2023. register_displaychangelistener(&qxl->ssd.dcl);
  2024. }
  2025. static void qxl_realize_secondary(PCIDevice *dev, Error **errp)
  2026. {
  2027. PCIQXLDevice *qxl = PCI_QXL(dev);
  2028. qxl_init_ramsize(qxl);
  2029. memory_region_init_ram(&qxl->vga.vram, OBJECT(dev), "qxl.vgavram",
  2030. qxl->vga.vram_size, &error_fatal);
  2031. qxl->vga.vram_ptr = memory_region_get_ram_ptr(&qxl->vga.vram);
  2032. qxl->vga.con = graphic_console_init(DEVICE(dev), 0, &qxl_ops, qxl);
  2033. qxl->id = qemu_console_get_index(qxl->vga.con); /* == channel_id */
  2034. qxl_realize_common(qxl, errp);
  2035. }
  2036. static int qxl_pre_save(void *opaque)
  2037. {
  2038. PCIQXLDevice* d = opaque;
  2039. uint8_t *ram_start = d->vga.vram_ptr;
  2040. trace_qxl_pre_save(d->id);
  2041. if (d->last_release == NULL) {
  2042. d->last_release_offset = 0;
  2043. } else {
  2044. d->last_release_offset = (uint8_t *)d->last_release - ram_start;
  2045. }
  2046. assert(d->last_release_offset < d->vga.vram_size);
  2047. return 0;
  2048. }
  2049. static int qxl_pre_load(void *opaque)
  2050. {
  2051. PCIQXLDevice* d = opaque;
  2052. trace_qxl_pre_load(d->id);
  2053. qxl_hard_reset(d, 1);
  2054. qxl_exit_vga_mode(d);
  2055. return 0;
  2056. }
  2057. static void qxl_create_memslots(PCIQXLDevice *d)
  2058. {
  2059. int i;
  2060. for (i = 0; i < NUM_MEMSLOTS; i++) {
  2061. if (!d->guest_slots[i].active) {
  2062. continue;
  2063. }
  2064. qxl_add_memslot(d, i, 0, QXL_SYNC);
  2065. }
  2066. }
  2067. static int qxl_post_load(void *opaque, int version)
  2068. {
  2069. PCIQXLDevice* d = opaque;
  2070. uint8_t *ram_start = d->vga.vram_ptr;
  2071. QXLCommandExt *cmds;
  2072. int in, out, newmode;
  2073. assert(d->last_release_offset < d->vga.vram_size);
  2074. if (d->last_release_offset == 0) {
  2075. d->last_release = NULL;
  2076. } else {
  2077. d->last_release = (QXLReleaseInfo *)(ram_start + d->last_release_offset);
  2078. }
  2079. d->modes = (QXLModes*)((uint8_t*)d->rom + d->rom->modes_offset);
  2080. trace_qxl_post_load(d->id, qxl_mode_to_string(d->mode));
  2081. newmode = d->mode;
  2082. d->mode = QXL_MODE_UNDEFINED;
  2083. switch (newmode) {
  2084. case QXL_MODE_UNDEFINED:
  2085. qxl_create_memslots(d);
  2086. break;
  2087. case QXL_MODE_VGA:
  2088. qxl_create_memslots(d);
  2089. qxl_enter_vga_mode(d);
  2090. break;
  2091. case QXL_MODE_NATIVE:
  2092. qxl_create_memslots(d);
  2093. qxl_create_guest_primary(d, 1, QXL_SYNC);
  2094. /* replay surface-create and cursor-set commands */
  2095. cmds = g_new0(QXLCommandExt, d->ssd.num_surfaces + 1);
  2096. for (in = 0, out = 0; in < d->ssd.num_surfaces; in++) {
  2097. if (d->guest_surfaces.cmds[in] == 0) {
  2098. continue;
  2099. }
  2100. cmds[out].cmd.data = d->guest_surfaces.cmds[in];
  2101. cmds[out].cmd.type = QXL_CMD_SURFACE;
  2102. cmds[out].group_id = MEMSLOT_GROUP_GUEST;
  2103. out++;
  2104. }
  2105. if (d->guest_cursor) {
  2106. cmds[out].cmd.data = d->guest_cursor;
  2107. cmds[out].cmd.type = QXL_CMD_CURSOR;
  2108. cmds[out].group_id = MEMSLOT_GROUP_GUEST;
  2109. out++;
  2110. }
  2111. qxl_spice_loadvm_commands(d, cmds, out);
  2112. g_free(cmds);
  2113. if (d->guest_monitors_config) {
  2114. qxl_spice_monitors_config_async(d, 1);
  2115. }
  2116. break;
  2117. case QXL_MODE_COMPAT:
  2118. /* note: no need to call qxl_create_memslots, qxl_set_mode
  2119. * creates the mem slot. */
  2120. qxl_set_mode(d, d->shadow_rom.mode, 1);
  2121. break;
  2122. }
  2123. return 0;
  2124. }
  2125. #define QXL_SAVE_VERSION 21
  2126. static bool qxl_monitors_config_needed(void *opaque)
  2127. {
  2128. PCIQXLDevice *qxl = opaque;
  2129. return qxl->guest_monitors_config != 0;
  2130. }
  2131. static VMStateDescription qxl_memslot = {
  2132. .name = "qxl-memslot",
  2133. .version_id = QXL_SAVE_VERSION,
  2134. .minimum_version_id = QXL_SAVE_VERSION,
  2135. .fields = (VMStateField[]) {
  2136. VMSTATE_UINT64(slot.mem_start, struct guest_slots),
  2137. VMSTATE_UINT64(slot.mem_end, struct guest_slots),
  2138. VMSTATE_UINT32(active, struct guest_slots),
  2139. VMSTATE_END_OF_LIST()
  2140. }
  2141. };
  2142. static VMStateDescription qxl_surface = {
  2143. .name = "qxl-surface",
  2144. .version_id = QXL_SAVE_VERSION,
  2145. .minimum_version_id = QXL_SAVE_VERSION,
  2146. .fields = (VMStateField[]) {
  2147. VMSTATE_UINT32(width, QXLSurfaceCreate),
  2148. VMSTATE_UINT32(height, QXLSurfaceCreate),
  2149. VMSTATE_INT32(stride, QXLSurfaceCreate),
  2150. VMSTATE_UINT32(format, QXLSurfaceCreate),
  2151. VMSTATE_UINT32(position, QXLSurfaceCreate),
  2152. VMSTATE_UINT32(mouse_mode, QXLSurfaceCreate),
  2153. VMSTATE_UINT32(flags, QXLSurfaceCreate),
  2154. VMSTATE_UINT32(type, QXLSurfaceCreate),
  2155. VMSTATE_UINT64(mem, QXLSurfaceCreate),
  2156. VMSTATE_END_OF_LIST()
  2157. }
  2158. };
  2159. static VMStateDescription qxl_vmstate_monitors_config = {
  2160. .name = "qxl/monitors-config",
  2161. .version_id = 1,
  2162. .minimum_version_id = 1,
  2163. .needed = qxl_monitors_config_needed,
  2164. .fields = (VMStateField[]) {
  2165. VMSTATE_UINT64(guest_monitors_config, PCIQXLDevice),
  2166. VMSTATE_END_OF_LIST()
  2167. },
  2168. };
  2169. static VMStateDescription qxl_vmstate = {
  2170. .name = "qxl",
  2171. .version_id = QXL_SAVE_VERSION,
  2172. .minimum_version_id = QXL_SAVE_VERSION,
  2173. .pre_save = qxl_pre_save,
  2174. .pre_load = qxl_pre_load,
  2175. .post_load = qxl_post_load,
  2176. .fields = (VMStateField[]) {
  2177. VMSTATE_PCI_DEVICE(pci, PCIQXLDevice),
  2178. VMSTATE_STRUCT(vga, PCIQXLDevice, 0, vmstate_vga_common, VGACommonState),
  2179. VMSTATE_UINT32(shadow_rom.mode, PCIQXLDevice),
  2180. VMSTATE_UINT32(num_free_res, PCIQXLDevice),
  2181. VMSTATE_UINT32(last_release_offset, PCIQXLDevice),
  2182. VMSTATE_UINT32(mode, PCIQXLDevice),
  2183. VMSTATE_UINT32(ssd.unique, PCIQXLDevice),
  2184. VMSTATE_INT32_EQUAL(num_memslots, PCIQXLDevice, NULL),
  2185. VMSTATE_STRUCT_ARRAY(guest_slots, PCIQXLDevice, NUM_MEMSLOTS, 0,
  2186. qxl_memslot, struct guest_slots),
  2187. VMSTATE_STRUCT(guest_primary.surface, PCIQXLDevice, 0,
  2188. qxl_surface, QXLSurfaceCreate),
  2189. VMSTATE_INT32_EQUAL(ssd.num_surfaces, PCIQXLDevice, NULL),
  2190. VMSTATE_VARRAY_INT32(guest_surfaces.cmds, PCIQXLDevice,
  2191. ssd.num_surfaces, 0,
  2192. vmstate_info_uint64, uint64_t),
  2193. VMSTATE_UINT64(guest_cursor, PCIQXLDevice),
  2194. VMSTATE_END_OF_LIST()
  2195. },
  2196. .subsections = (const VMStateDescription*[]) {
  2197. &qxl_vmstate_monitors_config,
  2198. NULL
  2199. }
  2200. };
  2201. static Property qxl_properties[] = {
  2202. DEFINE_PROP_UINT32("ram_size", PCIQXLDevice, vga.vram_size, 64 * MiB),
  2203. DEFINE_PROP_UINT64("vram_size", PCIQXLDevice, vram32_size, 64 * MiB),
  2204. DEFINE_PROP_UINT32("revision", PCIQXLDevice, revision,
  2205. QXL_DEFAULT_REVISION),
  2206. DEFINE_PROP_UINT32("debug", PCIQXLDevice, debug, 0),
  2207. DEFINE_PROP_UINT32("guestdebug", PCIQXLDevice, guestdebug, 0),
  2208. DEFINE_PROP_UINT32("cmdlog", PCIQXLDevice, cmdlog, 0),
  2209. DEFINE_PROP_UINT32("ram_size_mb", PCIQXLDevice, ram_size_mb, -1),
  2210. DEFINE_PROP_UINT32("vram_size_mb", PCIQXLDevice, vram32_size_mb, -1),
  2211. DEFINE_PROP_UINT32("vram64_size_mb", PCIQXLDevice, vram_size_mb, -1),
  2212. DEFINE_PROP_UINT32("vgamem_mb", PCIQXLDevice, vgamem_size_mb, 16),
  2213. DEFINE_PROP_INT32("surfaces", PCIQXLDevice, ssd.num_surfaces, 1024),
  2214. #if SPICE_SERVER_VERSION >= 0x000c06 /* release 0.12.6 */
  2215. DEFINE_PROP_UINT16("max_outputs", PCIQXLDevice, max_outputs, 0),
  2216. #endif
  2217. DEFINE_PROP_UINT32("xres", PCIQXLDevice, xres, 0),
  2218. DEFINE_PROP_UINT32("yres", PCIQXLDevice, yres, 0),
  2219. DEFINE_PROP_BOOL("global-vmstate", PCIQXLDevice, vga.global_vmstate, false),
  2220. DEFINE_PROP_END_OF_LIST(),
  2221. };
  2222. static void qxl_pci_class_init(ObjectClass *klass, void *data)
  2223. {
  2224. DeviceClass *dc = DEVICE_CLASS(klass);
  2225. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  2226. k->vendor_id = REDHAT_PCI_VENDOR_ID;
  2227. k->device_id = QXL_DEVICE_ID_STABLE;
  2228. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  2229. dc->reset = qxl_reset_handler;
  2230. dc->vmsd = &qxl_vmstate;
  2231. device_class_set_props(dc, qxl_properties);
  2232. }
  2233. static const TypeInfo qxl_pci_type_info = {
  2234. .name = TYPE_PCI_QXL,
  2235. .parent = TYPE_PCI_DEVICE,
  2236. .instance_size = sizeof(PCIQXLDevice),
  2237. .abstract = true,
  2238. .class_init = qxl_pci_class_init,
  2239. .interfaces = (InterfaceInfo[]) {
  2240. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  2241. { },
  2242. },
  2243. };
  2244. static void qxl_primary_class_init(ObjectClass *klass, void *data)
  2245. {
  2246. DeviceClass *dc = DEVICE_CLASS(klass);
  2247. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  2248. k->realize = qxl_realize_primary;
  2249. k->romfile = "vgabios-qxl.bin";
  2250. k->class_id = PCI_CLASS_DISPLAY_VGA;
  2251. dc->desc = "Spice QXL GPU (primary, vga compatible)";
  2252. dc->hotpluggable = false;
  2253. }
  2254. static const TypeInfo qxl_primary_info = {
  2255. .name = "qxl-vga",
  2256. .parent = TYPE_PCI_QXL,
  2257. .class_init = qxl_primary_class_init,
  2258. };
  2259. static void qxl_secondary_class_init(ObjectClass *klass, void *data)
  2260. {
  2261. DeviceClass *dc = DEVICE_CLASS(klass);
  2262. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  2263. k->realize = qxl_realize_secondary;
  2264. k->class_id = PCI_CLASS_DISPLAY_OTHER;
  2265. dc->desc = "Spice QXL GPU (secondary)";
  2266. }
  2267. static const TypeInfo qxl_secondary_info = {
  2268. .name = "qxl",
  2269. .parent = TYPE_PCI_QXL,
  2270. .class_init = qxl_secondary_class_init,
  2271. };
  2272. static void qxl_register_types(void)
  2273. {
  2274. type_register_static(&qxl_pci_type_info);
  2275. type_register_static(&qxl_primary_info);
  2276. type_register_static(&qxl_secondary_info);
  2277. }
  2278. type_init(qxl_register_types)