xenfb.c 31 KB

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