qxl.c 84 KB

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