xenfb.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * xen paravirt framebuffer backend
  3. *
  4. * Copyright IBM, Corp. 2005-2006
  5. * Copyright Red Hat, Inc. 2006-2008
  6. *
  7. * Authors:
  8. * Anthony Liguori <aliguori@us.ibm.com>,
  9. * Markus Armbruster <armbru@redhat.com>,
  10. * Daniel P. Berrange <berrange@redhat.com>,
  11. * Pat Campbell <plc@novell.com>,
  12. * Gerd Hoffmann <kraxel@redhat.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; under version 2 of the License.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, see <http://www.gnu.org/licenses/>.
  25. */
  26. #include "qemu/osdep.h"
  27. #include "qemu/units.h"
  28. #include "ui/input.h"
  29. #include "ui/console.h"
  30. #include "system/system.h"
  31. #include "hw/xen/xen-legacy-backend.h"
  32. #include "hw/xen/interface/io/fbif.h"
  33. #include "hw/xen/interface/io/kbdif.h"
  34. #include "hw/xen/interface/io/protocols.h"
  35. #include "trace.h"
  36. #ifndef BTN_LEFT
  37. #define BTN_LEFT 0x110 /* from <linux/input.h> */
  38. #endif
  39. /* -------------------------------------------------------------------- */
  40. struct common {
  41. struct XenLegacyDevice xendev; /* must be first */
  42. void *page;
  43. };
  44. struct XenInput {
  45. struct common c;
  46. int abs_pointer_wanted; /* Whether guest supports absolute pointer */
  47. int raw_pointer_wanted; /* Whether guest supports raw (unscaled) pointer */
  48. QemuInputHandlerState *qkbd;
  49. QemuInputHandlerState *qmou;
  50. int axis[INPUT_AXIS__MAX];
  51. int wheel;
  52. };
  53. #define UP_QUEUE 8
  54. struct XenFB {
  55. struct common c;
  56. QemuConsole *con;
  57. size_t fb_len;
  58. int row_stride;
  59. int depth;
  60. int width;
  61. int height;
  62. int offset;
  63. void *pixels;
  64. int fbpages;
  65. int feature_update;
  66. int bug_trigger;
  67. int do_resize;
  68. struct {
  69. int x,y,w,h;
  70. } up_rects[UP_QUEUE];
  71. int up_count;
  72. int up_fullscreen;
  73. };
  74. static const GraphicHwOps xenfb_ops;
  75. /* -------------------------------------------------------------------- */
  76. static int common_bind(struct common *c)
  77. {
  78. uint64_t val;
  79. xen_pfn_t mfn;
  80. if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &val) == -1)
  81. return -1;
  82. mfn = (xen_pfn_t)val;
  83. assert(val == mfn);
  84. if (xenstore_read_fe_int(&c->xendev, "event-channel", &c->xendev.remote_port) == -1)
  85. return -1;
  86. c->page = qemu_xen_foreignmem_map(c->xendev.dom, NULL,
  87. PROT_READ | PROT_WRITE, 1, &mfn,
  88. NULL);
  89. if (c->page == NULL)
  90. return -1;
  91. xen_be_bind_evtchn(&c->xendev);
  92. xen_pv_printf(&c->xendev, 1,
  93. "ring mfn %"PRI_xen_pfn", remote-port %d, local-port %d\n",
  94. mfn, c->xendev.remote_port, c->xendev.local_port);
  95. return 0;
  96. }
  97. static void common_unbind(struct common *c)
  98. {
  99. xen_pv_unbind_evtchn(&c->xendev);
  100. if (c->page) {
  101. qemu_xen_foreignmem_unmap(c->page, 1);
  102. c->page = NULL;
  103. }
  104. }
  105. /* -------------------------------------------------------------------- */
  106. /* Send an event to the keyboard frontend driver */
  107. static int xenfb_kbd_event(struct XenInput *xenfb,
  108. union xenkbd_in_event *event)
  109. {
  110. struct xenkbd_page *page = xenfb->c.page;
  111. uint32_t prod;
  112. if (xenfb->c.xendev.be_state != XenbusStateConnected)
  113. return 0;
  114. if (!page)
  115. return 0;
  116. prod = page->in_prod;
  117. if (prod - page->in_cons == XENKBD_IN_RING_LEN) {
  118. errno = EAGAIN;
  119. return -1;
  120. }
  121. xen_mb(); /* ensure ring space available */
  122. XENKBD_IN_RING_REF(page, prod) = *event;
  123. xen_wmb(); /* ensure ring contents visible */
  124. page->in_prod = prod + 1;
  125. return xen_pv_send_notify(&xenfb->c.xendev);
  126. }
  127. /* Send a keyboard (or mouse button) event */
  128. static int xenfb_send_key(struct XenInput *xenfb, bool down, int keycode)
  129. {
  130. union xenkbd_in_event event;
  131. memset(&event, 0, XENKBD_IN_EVENT_SIZE);
  132. event.type = XENKBD_TYPE_KEY;
  133. event.key.pressed = down ? 1 : 0;
  134. event.key.keycode = keycode;
  135. return xenfb_kbd_event(xenfb, &event);
  136. }
  137. /* Send a relative mouse movement event */
  138. static int xenfb_send_motion(struct XenInput *xenfb,
  139. int rel_x, int rel_y, int rel_z)
  140. {
  141. union xenkbd_in_event event;
  142. memset(&event, 0, XENKBD_IN_EVENT_SIZE);
  143. event.type = XENKBD_TYPE_MOTION;
  144. event.motion.rel_x = rel_x;
  145. event.motion.rel_y = rel_y;
  146. event.motion.rel_z = rel_z;
  147. return xenfb_kbd_event(xenfb, &event);
  148. }
  149. /* Send an absolute mouse movement event */
  150. static int xenfb_send_position(struct XenInput *xenfb,
  151. int abs_x, int abs_y, int z)
  152. {
  153. union xenkbd_in_event event;
  154. memset(&event, 0, XENKBD_IN_EVENT_SIZE);
  155. event.type = XENKBD_TYPE_POS;
  156. event.pos.abs_x = abs_x;
  157. event.pos.abs_y = abs_y;
  158. event.pos.rel_z = z;
  159. return xenfb_kbd_event(xenfb, &event);
  160. }
  161. /*
  162. * Send a key event from the client to the guest OS
  163. * QEMU gives us a QCode.
  164. * We have to turn this into a Linux Input layer keycode.
  165. *
  166. * Wish we could just send scancodes straight to the guest which
  167. * already has code for dealing with this...
  168. */
  169. static void xenfb_key_event(DeviceState *dev, QemuConsole *src,
  170. InputEvent *evt)
  171. {
  172. struct XenInput *xenfb = (struct XenInput *)dev;
  173. InputKeyEvent *key = evt->u.key.data;
  174. int qcode = qemu_input_key_value_to_qcode(key->key);
  175. int lnx;
  176. if (qcode < qemu_input_map_qcode_to_linux_len) {
  177. lnx = qemu_input_map_qcode_to_linux[qcode];
  178. if (lnx) {
  179. trace_xenfb_key_event(xenfb, lnx, key->down);
  180. xenfb_send_key(xenfb, key->down, lnx);
  181. }
  182. }
  183. }
  184. /*
  185. * Send a mouse event from the client to the guest OS
  186. *
  187. * The QEMU mouse can be in either relative, or absolute mode.
  188. * Movement is sent separately from button state, which has to
  189. * be encoded as virtual key events. We also don't actually get
  190. * given any button up/down events, so have to track changes in
  191. * the button state.
  192. */
  193. static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src,
  194. InputEvent *evt)
  195. {
  196. struct XenInput *xenfb = (struct XenInput *)dev;
  197. InputBtnEvent *btn;
  198. InputMoveEvent *move;
  199. QemuConsole *con;
  200. DisplaySurface *surface;
  201. int scale;
  202. switch (evt->type) {
  203. case INPUT_EVENT_KIND_BTN:
  204. btn = evt->u.btn.data;
  205. switch (btn->button) {
  206. case INPUT_BUTTON_LEFT:
  207. xenfb_send_key(xenfb, btn->down, BTN_LEFT);
  208. break;
  209. case INPUT_BUTTON_RIGHT:
  210. xenfb_send_key(xenfb, btn->down, BTN_LEFT + 1);
  211. break;
  212. case INPUT_BUTTON_MIDDLE:
  213. xenfb_send_key(xenfb, btn->down, BTN_LEFT + 2);
  214. break;
  215. case INPUT_BUTTON_WHEEL_UP:
  216. if (btn->down) {
  217. xenfb->wheel--;
  218. }
  219. break;
  220. case INPUT_BUTTON_WHEEL_DOWN:
  221. if (btn->down) {
  222. xenfb->wheel++;
  223. }
  224. break;
  225. default:
  226. break;
  227. }
  228. break;
  229. case INPUT_EVENT_KIND_ABS:
  230. move = evt->u.abs.data;
  231. if (xenfb->raw_pointer_wanted) {
  232. xenfb->axis[move->axis] = move->value;
  233. } else {
  234. con = qemu_console_lookup_by_index(0);
  235. if (!con) {
  236. xen_pv_printf(&xenfb->c.xendev, 0, "No QEMU console available");
  237. return;
  238. }
  239. surface = qemu_console_surface(con);
  240. switch (move->axis) {
  241. case INPUT_AXIS_X:
  242. scale = surface_width(surface) - 1;
  243. break;
  244. case INPUT_AXIS_Y:
  245. scale = surface_height(surface) - 1;
  246. break;
  247. default:
  248. scale = 0x8000;
  249. break;
  250. }
  251. xenfb->axis[move->axis] = move->value * scale / 0x7fff;
  252. }
  253. break;
  254. case INPUT_EVENT_KIND_REL:
  255. move = evt->u.rel.data;
  256. xenfb->axis[move->axis] += move->value;
  257. break;
  258. default:
  259. break;
  260. }
  261. }
  262. static void xenfb_mouse_sync(DeviceState *dev)
  263. {
  264. struct XenInput *xenfb = (struct XenInput *)dev;
  265. trace_xenfb_mouse_event(xenfb, xenfb->axis[INPUT_AXIS_X],
  266. xenfb->axis[INPUT_AXIS_Y],
  267. xenfb->wheel, 0,
  268. xenfb->abs_pointer_wanted);
  269. if (xenfb->abs_pointer_wanted) {
  270. xenfb_send_position(xenfb, xenfb->axis[INPUT_AXIS_X],
  271. xenfb->axis[INPUT_AXIS_Y],
  272. xenfb->wheel);
  273. } else {
  274. xenfb_send_motion(xenfb, xenfb->axis[INPUT_AXIS_X],
  275. xenfb->axis[INPUT_AXIS_Y],
  276. xenfb->wheel);
  277. xenfb->axis[INPUT_AXIS_X] = 0;
  278. xenfb->axis[INPUT_AXIS_Y] = 0;
  279. }
  280. xenfb->wheel = 0;
  281. }
  282. static const QemuInputHandler xenfb_keyboard = {
  283. .name = "Xen PV Keyboard",
  284. .mask = INPUT_EVENT_MASK_KEY,
  285. .event = xenfb_key_event,
  286. };
  287. static const QemuInputHandler xenfb_abs_mouse = {
  288. .name = "Xen PV Mouse",
  289. .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_ABS,
  290. .event = xenfb_mouse_event,
  291. .sync = xenfb_mouse_sync,
  292. };
  293. static const QemuInputHandler xenfb_rel_mouse = {
  294. .name = "Xen PV Mouse",
  295. .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
  296. .event = xenfb_mouse_event,
  297. .sync = xenfb_mouse_sync,
  298. };
  299. static int input_init(struct XenLegacyDevice *xendev)
  300. {
  301. xenstore_write_be_int(xendev, "feature-abs-pointer", 1);
  302. xenstore_write_be_int(xendev, "feature-raw-pointer", 1);
  303. return 0;
  304. }
  305. static int input_initialise(struct XenLegacyDevice *xendev)
  306. {
  307. struct XenInput *in = container_of(xendev, struct XenInput, c.xendev);
  308. int rc;
  309. rc = common_bind(&in->c);
  310. if (rc != 0)
  311. return rc;
  312. return 0;
  313. }
  314. static void input_connected(struct XenLegacyDevice *xendev)
  315. {
  316. struct XenInput *in = container_of(xendev, struct XenInput, c.xendev);
  317. if (xenstore_read_fe_int(xendev, "request-abs-pointer",
  318. &in->abs_pointer_wanted) == -1) {
  319. in->abs_pointer_wanted = 0;
  320. }
  321. if (xenstore_read_fe_int(xendev, "request-raw-pointer",
  322. &in->raw_pointer_wanted) == -1) {
  323. in->raw_pointer_wanted = 0;
  324. }
  325. if (in->raw_pointer_wanted && in->abs_pointer_wanted == 0) {
  326. xen_pv_printf(xendev, 0, "raw pointer set without abs pointer");
  327. }
  328. if (in->qkbd) {
  329. qemu_input_handler_unregister(in->qkbd);
  330. }
  331. if (in->qmou) {
  332. qemu_input_handler_unregister(in->qmou);
  333. }
  334. trace_xenfb_input_connected(xendev, in->abs_pointer_wanted);
  335. in->qkbd = qemu_input_handler_register((DeviceState *)in, &xenfb_keyboard);
  336. in->qmou = qemu_input_handler_register((DeviceState *)in,
  337. in->abs_pointer_wanted ? &xenfb_abs_mouse : &xenfb_rel_mouse);
  338. if (in->raw_pointer_wanted) {
  339. qemu_input_handler_activate(in->qkbd);
  340. qemu_input_handler_activate(in->qmou);
  341. }
  342. }
  343. static void input_disconnect(struct XenLegacyDevice *xendev)
  344. {
  345. struct XenInput *in = container_of(xendev, struct XenInput, c.xendev);
  346. if (in->qkbd) {
  347. qemu_input_handler_unregister(in->qkbd);
  348. in->qkbd = NULL;
  349. }
  350. if (in->qmou) {
  351. qemu_input_handler_unregister(in->qmou);
  352. in->qmou = NULL;
  353. }
  354. common_unbind(&in->c);
  355. }
  356. static void input_event(struct XenLegacyDevice *xendev)
  357. {
  358. struct XenInput *xenfb = container_of(xendev, struct XenInput, c.xendev);
  359. struct xenkbd_page *page = xenfb->c.page;
  360. /* We don't understand any keyboard events, so just ignore them. */
  361. if (page->out_prod == page->out_cons)
  362. return;
  363. page->out_cons = page->out_prod;
  364. xen_pv_send_notify(&xenfb->c.xendev);
  365. }
  366. /* -------------------------------------------------------------------- */
  367. static void xenfb_copy_mfns(int mode, int count, xen_pfn_t *dst, void *src)
  368. {
  369. uint32_t *src32 = src;
  370. uint64_t *src64 = src;
  371. int i;
  372. for (i = 0; i < count; i++)
  373. dst[i] = (mode == 32) ? src32[i] : src64[i];
  374. }
  375. static int xenfb_map_fb(struct XenFB *xenfb)
  376. {
  377. struct xenfb_page *page = xenfb->c.page;
  378. char *protocol = xenfb->c.xendev.protocol;
  379. int n_fbdirs;
  380. xen_pfn_t *pgmfns = NULL;
  381. xen_pfn_t *fbmfns = NULL;
  382. void *map, *pd;
  383. int mode, ret = -1;
  384. /* default to native */
  385. pd = page->pd;
  386. mode = sizeof(unsigned long) * 8;
  387. if (!protocol) {
  388. /*
  389. * Undefined protocol, some guesswork needed.
  390. *
  391. * Old frontends which don't set the protocol use
  392. * one page directory only, thus pd[1] must be zero.
  393. * pd[1] of the 32bit struct layout and the lower
  394. * 32 bits of pd[0] of the 64bit struct layout have
  395. * the same location, so we can check that ...
  396. */
  397. uint32_t *ptr32 = NULL;
  398. uint32_t *ptr64 = NULL;
  399. #if defined(__i386__)
  400. ptr32 = (void*)page->pd;
  401. ptr64 = ((void*)page->pd) + 4;
  402. #elif defined(__x86_64__)
  403. ptr32 = ((void*)page->pd) - 4;
  404. ptr64 = (void*)page->pd;
  405. #endif
  406. if (ptr32) {
  407. if (ptr32[1] == 0) {
  408. mode = 32;
  409. pd = ptr32;
  410. } else {
  411. mode = 64;
  412. pd = ptr64;
  413. }
  414. }
  415. #if defined(__x86_64__)
  416. } else if (strcmp(protocol, XEN_IO_PROTO_ABI_X86_32) == 0) {
  417. /* 64bit dom0, 32bit domU */
  418. mode = 32;
  419. pd = ((void*)page->pd) - 4;
  420. #elif defined(__i386__)
  421. } else if (strcmp(protocol, XEN_IO_PROTO_ABI_X86_64) == 0) {
  422. /* 32bit dom0, 64bit domU */
  423. mode = 64;
  424. pd = ((void*)page->pd) + 4;
  425. #endif
  426. }
  427. if (xenfb->pixels) {
  428. munmap(xenfb->pixels, xenfb->fbpages * XEN_PAGE_SIZE);
  429. xenfb->pixels = NULL;
  430. }
  431. xenfb->fbpages = DIV_ROUND_UP(xenfb->fb_len, XEN_PAGE_SIZE);
  432. n_fbdirs = xenfb->fbpages * mode / 8;
  433. n_fbdirs = DIV_ROUND_UP(n_fbdirs, XEN_PAGE_SIZE);
  434. pgmfns = g_new0(xen_pfn_t, n_fbdirs);
  435. fbmfns = g_new0(xen_pfn_t, xenfb->fbpages);
  436. xenfb_copy_mfns(mode, n_fbdirs, pgmfns, pd);
  437. map = qemu_xen_foreignmem_map(xenfb->c.xendev.dom, NULL, PROT_READ,
  438. n_fbdirs, pgmfns, NULL);
  439. if (map == NULL)
  440. goto out;
  441. xenfb_copy_mfns(mode, xenfb->fbpages, fbmfns, map);
  442. qemu_xen_foreignmem_unmap(map, n_fbdirs);
  443. xenfb->pixels = qemu_xen_foreignmem_map(xenfb->c.xendev.dom, NULL,
  444. PROT_READ, xenfb->fbpages,
  445. fbmfns, NULL);
  446. if (xenfb->pixels == NULL)
  447. goto out;
  448. ret = 0; /* all is fine */
  449. out:
  450. g_free(pgmfns);
  451. g_free(fbmfns);
  452. return ret;
  453. }
  454. static int xenfb_configure_fb(struct XenFB *xenfb, size_t fb_len_lim,
  455. int width, int height, int depth,
  456. size_t fb_len, int offset, int row_stride)
  457. {
  458. size_t mfn_sz = sizeof_field(struct xenfb_page, pd[0]);
  459. size_t pd_len = sizeof_field(struct xenfb_page, pd) / mfn_sz;
  460. size_t fb_pages = pd_len * XEN_PAGE_SIZE / mfn_sz;
  461. size_t fb_len_max = fb_pages * XEN_PAGE_SIZE;
  462. int max_width, max_height;
  463. if (fb_len_lim > fb_len_max) {
  464. xen_pv_printf(&xenfb->c.xendev, 0,
  465. "fb size limit %zu exceeds %zu, corrected\n",
  466. fb_len_lim, fb_len_max);
  467. fb_len_lim = fb_len_max;
  468. }
  469. if (fb_len_lim && fb_len > fb_len_lim) {
  470. xen_pv_printf(&xenfb->c.xendev, 0,
  471. "frontend fb size %zu limited to %zu\n",
  472. fb_len, fb_len_lim);
  473. fb_len = fb_len_lim;
  474. }
  475. if (depth != 8 && depth != 16 && depth != 24 && depth != 32) {
  476. xen_pv_printf(&xenfb->c.xendev, 0,
  477. "can't handle frontend fb depth %d\n",
  478. depth);
  479. return -1;
  480. }
  481. if (row_stride <= 0 || row_stride > fb_len) {
  482. xen_pv_printf(&xenfb->c.xendev, 0, "invalid frontend stride %d\n",
  483. row_stride);
  484. return -1;
  485. }
  486. max_width = row_stride / (depth / 8);
  487. if (width < 0 || width > max_width) {
  488. xen_pv_printf(&xenfb->c.xendev, 0,
  489. "invalid frontend width %d limited to %d\n",
  490. width, max_width);
  491. width = max_width;
  492. }
  493. if (offset < 0 || offset >= fb_len) {
  494. xen_pv_printf(&xenfb->c.xendev, 0,
  495. "invalid frontend offset %d (max %zu)\n",
  496. offset, fb_len - 1);
  497. return -1;
  498. }
  499. max_height = (fb_len - offset) / row_stride;
  500. if (height < 0 || height > max_height) {
  501. xen_pv_printf(&xenfb->c.xendev, 0,
  502. "invalid frontend height %d limited to %d\n",
  503. height, max_height);
  504. height = max_height;
  505. }
  506. xenfb->fb_len = fb_len;
  507. xenfb->row_stride = row_stride;
  508. xenfb->depth = depth;
  509. xenfb->width = width;
  510. xenfb->height = height;
  511. xenfb->offset = offset;
  512. xenfb->up_fullscreen = 1;
  513. xenfb->do_resize = 1;
  514. xen_pv_printf(&xenfb->c.xendev, 1,
  515. "framebuffer %dx%dx%d offset %d stride %d\n",
  516. width, height, depth, offset, row_stride);
  517. return 0;
  518. }
  519. /* A convenient function for munging pixels between different depths */
  520. #define BLT(SRC_T,DST_T,RSB,GSB,BSB,RDB,GDB,BDB) \
  521. for (line = y ; line < (y+h) ; line++) { \
  522. SRC_T *src = (SRC_T *)(xenfb->pixels \
  523. + xenfb->offset \
  524. + (line * xenfb->row_stride) \
  525. + (x * xenfb->depth / 8)); \
  526. DST_T *dst = (DST_T *)(data \
  527. + (line * linesize) \
  528. + (x * bpp / 8)); \
  529. int col; \
  530. const int RSS = 32 - (RSB + GSB + BSB); \
  531. const int GSS = 32 - (GSB + BSB); \
  532. const int BSS = 32 - (BSB); \
  533. const uint32_t RSM = (~0U) << (32 - RSB); \
  534. const uint32_t GSM = (~0U) << (32 - GSB); \
  535. const uint32_t BSM = (~0U) << (32 - BSB); \
  536. const int RDS = 32 - (RDB + GDB + BDB); \
  537. const int GDS = 32 - (GDB + BDB); \
  538. const int BDS = 32 - (BDB); \
  539. const uint32_t RDM = (~0U) << (32 - RDB); \
  540. const uint32_t GDM = (~0U) << (32 - GDB); \
  541. const uint32_t BDM = (~0U) << (32 - BDB); \
  542. for (col = x ; col < (x+w) ; col++) { \
  543. uint32_t spix = *src; \
  544. *dst = (((spix << RSS) & RSM & RDM) >> RDS) | \
  545. (((spix << GSS) & GSM & GDM) >> GDS) | \
  546. (((spix << BSS) & BSM & BDM) >> BDS); \
  547. src = (SRC_T *) ((unsigned long) src + xenfb->depth / 8); \
  548. dst = (DST_T *) ((unsigned long) dst + bpp / 8); \
  549. } \
  550. }
  551. /*
  552. * This copies data from the guest framebuffer region, into QEMU's
  553. * displaysurface. qemu uses 16 or 32 bpp. In case the pv framebuffer
  554. * uses something else we must convert and copy, otherwise we can
  555. * supply the buffer directly and no thing here.
  556. */
  557. static void xenfb_guest_copy(struct XenFB *xenfb, int x, int y, int w, int h)
  558. {
  559. DisplaySurface *surface = qemu_console_surface(xenfb->con);
  560. int line, oops = 0;
  561. int bpp = surface_bits_per_pixel(surface);
  562. int linesize = surface_stride(surface);
  563. uint8_t *data = surface_data(surface);
  564. if (surface_is_allocated(surface)) {
  565. switch (xenfb->depth) {
  566. case 8:
  567. if (bpp == 16) {
  568. BLT(uint8_t, uint16_t, 3, 3, 2, 5, 6, 5);
  569. } else if (bpp == 32) {
  570. BLT(uint8_t, uint32_t, 3, 3, 2, 8, 8, 8);
  571. } else {
  572. oops = 1;
  573. }
  574. break;
  575. case 24:
  576. if (bpp == 16) {
  577. BLT(uint32_t, uint16_t, 8, 8, 8, 5, 6, 5);
  578. } else if (bpp == 32) {
  579. BLT(uint32_t, uint32_t, 8, 8, 8, 8, 8, 8);
  580. } else {
  581. oops = 1;
  582. }
  583. break;
  584. default:
  585. oops = 1;
  586. }
  587. }
  588. if (oops) /* should not happen */
  589. xen_pv_printf(&xenfb->c.xendev, 0, "%s: oops: convert %d -> %d bpp?\n",
  590. __func__, xenfb->depth, bpp);
  591. dpy_gfx_update(xenfb->con, x, y, w, h);
  592. }
  593. #ifdef XENFB_TYPE_REFRESH_PERIOD
  594. static int xenfb_queue_full(struct XenFB *xenfb)
  595. {
  596. struct xenfb_page *page = xenfb->c.page;
  597. uint32_t cons, prod;
  598. if (!page)
  599. return 1;
  600. prod = page->in_prod;
  601. cons = page->in_cons;
  602. return prod - cons == XENFB_IN_RING_LEN;
  603. }
  604. static void xenfb_send_event(struct XenFB *xenfb, union xenfb_in_event *event)
  605. {
  606. uint32_t prod;
  607. struct xenfb_page *page = xenfb->c.page;
  608. prod = page->in_prod;
  609. /* caller ensures !xenfb_queue_full() */
  610. xen_mb(); /* ensure ring space available */
  611. XENFB_IN_RING_REF(page, prod) = *event;
  612. xen_wmb(); /* ensure ring contents visible */
  613. page->in_prod = prod + 1;
  614. xen_pv_send_notify(&xenfb->c.xendev);
  615. }
  616. static void xenfb_send_refresh_period(struct XenFB *xenfb, int period)
  617. {
  618. union xenfb_in_event event;
  619. memset(&event, 0, sizeof(event));
  620. event.type = XENFB_TYPE_REFRESH_PERIOD;
  621. event.refresh_period.period = period;
  622. xenfb_send_event(xenfb, &event);
  623. }
  624. #endif
  625. /*
  626. * Periodic update of display.
  627. * Also transmit the refresh interval to the frontend.
  628. *
  629. * Never ever do any qemu display operations
  630. * (resize, screen update) outside this function.
  631. * Our screen might be inactive. When asked for
  632. * an update we know it is active.
  633. */
  634. static void xenfb_update(void *opaque)
  635. {
  636. struct XenFB *xenfb = opaque;
  637. DisplaySurface *surface;
  638. int i;
  639. if (xenfb->c.xendev.be_state != XenbusStateConnected)
  640. return;
  641. if (!xenfb->feature_update) {
  642. /* we don't get update notifications, thus use the
  643. * sledge hammer approach ... */
  644. xenfb->up_fullscreen = 1;
  645. }
  646. /* resize if needed */
  647. if (xenfb->do_resize) {
  648. pixman_format_code_t format;
  649. xenfb->do_resize = 0;
  650. switch (xenfb->depth) {
  651. case 16:
  652. case 32:
  653. /* console.c supported depth -> buffer can be used directly */
  654. format = qemu_default_pixman_format(xenfb->depth, true);
  655. surface = qemu_create_displaysurface_from
  656. (xenfb->width, xenfb->height, format,
  657. xenfb->row_stride, xenfb->pixels + xenfb->offset);
  658. break;
  659. default:
  660. /* we must convert stuff */
  661. surface = qemu_create_displaysurface(xenfb->width, xenfb->height);
  662. break;
  663. }
  664. dpy_gfx_replace_surface(xenfb->con, surface);
  665. xen_pv_printf(&xenfb->c.xendev, 1,
  666. "update: resizing: %dx%d @ %d bpp%s\n",
  667. xenfb->width, xenfb->height, xenfb->depth,
  668. surface_is_allocated(surface)
  669. ? " (allocated)" : " (borrowed)");
  670. xenfb->up_fullscreen = 1;
  671. }
  672. /* run queued updates */
  673. if (xenfb->up_fullscreen) {
  674. xen_pv_printf(&xenfb->c.xendev, 3, "update: fullscreen\n");
  675. xenfb_guest_copy(xenfb, 0, 0, xenfb->width, xenfb->height);
  676. } else if (xenfb->up_count) {
  677. xen_pv_printf(&xenfb->c.xendev, 3, "update: %d rects\n",
  678. xenfb->up_count);
  679. for (i = 0; i < xenfb->up_count; i++)
  680. xenfb_guest_copy(xenfb,
  681. xenfb->up_rects[i].x,
  682. xenfb->up_rects[i].y,
  683. xenfb->up_rects[i].w,
  684. xenfb->up_rects[i].h);
  685. } else {
  686. xen_pv_printf(&xenfb->c.xendev, 3, "update: nothing\n");
  687. }
  688. xenfb->up_count = 0;
  689. xenfb->up_fullscreen = 0;
  690. }
  691. static void xenfb_ui_info(void *opaque, uint32_t idx, QemuUIInfo *info)
  692. {
  693. struct XenFB *xenfb = opaque;
  694. uint32_t refresh_rate;
  695. if (xenfb->feature_update) {
  696. #ifdef XENFB_TYPE_REFRESH_PERIOD
  697. if (xenfb_queue_full(xenfb)) {
  698. return;
  699. }
  700. refresh_rate = info->refresh_rate;
  701. if (!refresh_rate) {
  702. refresh_rate = 75;
  703. }
  704. /* T = 1 / f = 1 [s*Hz] / f = 1000*1000 [ms*mHz] / f */
  705. xenfb_send_refresh_period(xenfb, 1000 * 1000 / refresh_rate);
  706. #endif
  707. }
  708. }
  709. /* QEMU display state changed, so refresh the framebuffer copy */
  710. static void xenfb_invalidate(void *opaque)
  711. {
  712. struct XenFB *xenfb = opaque;
  713. xenfb->up_fullscreen = 1;
  714. }
  715. static void xenfb_handle_events(struct XenFB *xenfb)
  716. {
  717. uint32_t prod, cons, out_cons;
  718. struct xenfb_page *page = xenfb->c.page;
  719. prod = page->out_prod;
  720. out_cons = page->out_cons;
  721. if (prod - out_cons > XENFB_OUT_RING_LEN) {
  722. return;
  723. }
  724. xen_rmb(); /* ensure we see ring contents up to prod */
  725. for (cons = out_cons; cons != prod; cons++) {
  726. union xenfb_out_event *event = &XENFB_OUT_RING_REF(page, cons);
  727. uint8_t type = event->type;
  728. int x, y, w, h;
  729. switch (type) {
  730. case XENFB_TYPE_UPDATE:
  731. if (xenfb->up_count == UP_QUEUE)
  732. xenfb->up_fullscreen = 1;
  733. if (xenfb->up_fullscreen)
  734. break;
  735. x = MAX(event->update.x, 0);
  736. y = MAX(event->update.y, 0);
  737. w = MIN(event->update.width, xenfb->width - x);
  738. h = MIN(event->update.height, xenfb->height - y);
  739. if (w < 0 || h < 0) {
  740. xen_pv_printf(&xenfb->c.xendev, 1, "bogus update ignored\n");
  741. break;
  742. }
  743. if (x != event->update.x ||
  744. y != event->update.y ||
  745. w != event->update.width ||
  746. h != event->update.height) {
  747. xen_pv_printf(&xenfb->c.xendev, 1, "bogus update clipped\n");
  748. }
  749. if (w == xenfb->width && h > xenfb->height / 2) {
  750. /* scroll detector: updated more than 50% of the lines,
  751. * don't bother keeping track of the rectangles then */
  752. xenfb->up_fullscreen = 1;
  753. } else {
  754. xenfb->up_rects[xenfb->up_count].x = x;
  755. xenfb->up_rects[xenfb->up_count].y = y;
  756. xenfb->up_rects[xenfb->up_count].w = w;
  757. xenfb->up_rects[xenfb->up_count].h = h;
  758. xenfb->up_count++;
  759. }
  760. break;
  761. #ifdef XENFB_TYPE_RESIZE
  762. case XENFB_TYPE_RESIZE:
  763. if (xenfb_configure_fb(xenfb, xenfb->fb_len,
  764. event->resize.width,
  765. event->resize.height,
  766. event->resize.depth,
  767. xenfb->fb_len,
  768. event->resize.offset,
  769. event->resize.stride) < 0)
  770. break;
  771. xenfb_invalidate(xenfb);
  772. break;
  773. #endif
  774. }
  775. }
  776. xen_mb(); /* ensure we're done with ring contents */
  777. page->out_cons = cons;
  778. }
  779. static int fb_init(struct XenLegacyDevice *xendev)
  780. {
  781. #ifdef XENFB_TYPE_RESIZE
  782. xenstore_write_be_int(xendev, "feature-resize", 1);
  783. #endif
  784. return 0;
  785. }
  786. static int fb_initialise(struct XenLegacyDevice *xendev)
  787. {
  788. struct XenFB *fb = container_of(xendev, struct XenFB, c.xendev);
  789. struct xenfb_page *fb_page;
  790. int videoram;
  791. int rc;
  792. if (xenstore_read_fe_int(xendev, "videoram", &videoram) == -1)
  793. videoram = 0;
  794. rc = common_bind(&fb->c);
  795. if (rc != 0)
  796. return rc;
  797. fb_page = fb->c.page;
  798. rc = xenfb_configure_fb(fb, videoram * MiB,
  799. fb_page->width, fb_page->height, fb_page->depth,
  800. fb_page->mem_length, 0, fb_page->line_length);
  801. if (rc != 0)
  802. return rc;
  803. rc = xenfb_map_fb(fb);
  804. if (rc != 0)
  805. return rc;
  806. fb->con = graphic_console_init(NULL, 0, &xenfb_ops, fb);
  807. if (xenstore_read_fe_int(xendev, "feature-update", &fb->feature_update) == -1)
  808. fb->feature_update = 0;
  809. if (fb->feature_update)
  810. xenstore_write_be_int(xendev, "request-update", 1);
  811. xen_pv_printf(xendev, 1, "feature-update=%d, videoram=%d\n",
  812. fb->feature_update, videoram);
  813. return 0;
  814. }
  815. static void fb_disconnect(struct XenLegacyDevice *xendev)
  816. {
  817. struct XenFB *fb = container_of(xendev, struct XenFB, c.xendev);
  818. /*
  819. * FIXME: qemu can't un-init gfx display (yet?).
  820. * Replacing the framebuffer with anonymous shared memory
  821. * instead. This releases the guest pages and keeps qemu happy.
  822. */
  823. qemu_xen_foreignmem_unmap(fb->pixels, fb->fbpages);
  824. fb->pixels = mmap(fb->pixels, fb->fbpages * XEN_PAGE_SIZE,
  825. PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON,
  826. -1, 0);
  827. if (fb->pixels == MAP_FAILED) {
  828. xen_pv_printf(xendev, 0,
  829. "Couldn't replace the framebuffer with anonymous memory errno=%d\n",
  830. errno);
  831. }
  832. common_unbind(&fb->c);
  833. fb->feature_update = 0;
  834. fb->bug_trigger = 0;
  835. }
  836. static void fb_frontend_changed(struct XenLegacyDevice *xendev,
  837. const char *node)
  838. {
  839. struct XenFB *fb = container_of(xendev, struct XenFB, c.xendev);
  840. /*
  841. * Set state to Connected *again* once the frontend switched
  842. * to connected. We must trigger the watch a second time to
  843. * workaround a frontend bug.
  844. */
  845. if (fb->bug_trigger == 0 && strcmp(node, "state") == 0 &&
  846. xendev->fe_state == XenbusStateConnected &&
  847. xendev->be_state == XenbusStateConnected) {
  848. xen_pv_printf(xendev, 2, "re-trigger connected (frontend bug)\n");
  849. xen_be_set_state(xendev, XenbusStateConnected);
  850. fb->bug_trigger = 1; /* only once */
  851. }
  852. }
  853. static void fb_event(struct XenLegacyDevice *xendev)
  854. {
  855. struct XenFB *xenfb = container_of(xendev, struct XenFB, c.xendev);
  856. xenfb_handle_events(xenfb);
  857. xen_pv_send_notify(&xenfb->c.xendev);
  858. }
  859. /* -------------------------------------------------------------------- */
  860. static const struct XenDevOps xen_kbdmouse_ops = {
  861. .size = sizeof(struct XenInput),
  862. .init = input_init,
  863. .initialise = input_initialise,
  864. .connected = input_connected,
  865. .disconnect = input_disconnect,
  866. .event = input_event,
  867. };
  868. const struct XenDevOps xen_framebuffer_ops = {
  869. .size = sizeof(struct XenFB),
  870. .init = fb_init,
  871. .initialise = fb_initialise,
  872. .disconnect = fb_disconnect,
  873. .event = fb_event,
  874. .frontend_changed = fb_frontend_changed,
  875. };
  876. static const GraphicHwOps xenfb_ops = {
  877. .invalidate = xenfb_invalidate,
  878. .gfx_update = xenfb_update,
  879. .ui_info = xenfb_ui_info,
  880. };
  881. static void xen_ui_register_backend(void)
  882. {
  883. xen_be_register("vkbd", &xen_kbdmouse_ops);
  884. if (vga_interface_type == VGA_XENFB) {
  885. xen_be_register("vfb", &xen_framebuffer_ops);
  886. }
  887. }
  888. xen_backend_init(xen_ui_register_backend);