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