xen-fbfront.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * Xen para-virtual frame buffer device
  3. *
  4. * Copyright (C) 2005-2006 Anthony Liguori <aliguori@us.ibm.com>
  5. * Copyright (C) 2006-2008 Red Hat, Inc., Markus Armbruster <armbru@redhat.com>
  6. *
  7. * Based on linux/drivers/video/q40fb.c
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file COPYING in the main directory of this archive for
  11. * more details.
  12. */
  13. /*
  14. * TODO:
  15. *
  16. * Switch to grant tables when they become capable of dealing with the
  17. * frame buffer.
  18. */
  19. #include <linux/console.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/fb.h>
  23. #include <linux/module.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/mm.h>
  26. #include <asm/xen/hypervisor.h>
  27. #include <xen/events.h>
  28. #include <xen/page.h>
  29. #include <xen/interface/io/fbif.h>
  30. #include <xen/interface/io/protocols.h>
  31. #include <xen/xenbus.h>
  32. struct xenfb_info {
  33. unsigned char *fb;
  34. struct fb_info *fb_info;
  35. int x1, y1, x2, y2; /* dirty rectangle,
  36. protected by dirty_lock */
  37. spinlock_t dirty_lock;
  38. int nr_pages;
  39. int irq;
  40. struct xenfb_page *page;
  41. unsigned long *mfns;
  42. int update_wanted; /* XENFB_TYPE_UPDATE wanted */
  43. struct xenbus_device *xbdev;
  44. };
  45. static u32 xenfb_mem_len = XENFB_WIDTH * XENFB_HEIGHT * XENFB_DEPTH / 8;
  46. static void xenfb_make_preferred_console(void);
  47. static int xenfb_remove(struct xenbus_device *);
  48. static void xenfb_init_shared_page(struct xenfb_info *);
  49. static int xenfb_connect_backend(struct xenbus_device *, struct xenfb_info *);
  50. static void xenfb_disconnect_backend(struct xenfb_info *);
  51. static void xenfb_do_update(struct xenfb_info *info,
  52. int x, int y, int w, int h)
  53. {
  54. union xenfb_out_event event;
  55. u32 prod;
  56. event.type = XENFB_TYPE_UPDATE;
  57. event.update.x = x;
  58. event.update.y = y;
  59. event.update.width = w;
  60. event.update.height = h;
  61. prod = info->page->out_prod;
  62. /* caller ensures !xenfb_queue_full() */
  63. mb(); /* ensure ring space available */
  64. XENFB_OUT_RING_REF(info->page, prod) = event;
  65. wmb(); /* ensure ring contents visible */
  66. info->page->out_prod = prod + 1;
  67. notify_remote_via_irq(info->irq);
  68. }
  69. static int xenfb_queue_full(struct xenfb_info *info)
  70. {
  71. u32 cons, prod;
  72. prod = info->page->out_prod;
  73. cons = info->page->out_cons;
  74. return prod - cons == XENFB_OUT_RING_LEN;
  75. }
  76. static void xenfb_refresh(struct xenfb_info *info,
  77. int x1, int y1, int w, int h)
  78. {
  79. unsigned long flags;
  80. int y2 = y1 + h - 1;
  81. int x2 = x1 + w - 1;
  82. if (!info->update_wanted)
  83. return;
  84. spin_lock_irqsave(&info->dirty_lock, flags);
  85. /* Combine with dirty rectangle: */
  86. if (info->y1 < y1)
  87. y1 = info->y1;
  88. if (info->y2 > y2)
  89. y2 = info->y2;
  90. if (info->x1 < x1)
  91. x1 = info->x1;
  92. if (info->x2 > x2)
  93. x2 = info->x2;
  94. if (xenfb_queue_full(info)) {
  95. /* Can't send right now, stash it in the dirty rectangle */
  96. info->x1 = x1;
  97. info->x2 = x2;
  98. info->y1 = y1;
  99. info->y2 = y2;
  100. spin_unlock_irqrestore(&info->dirty_lock, flags);
  101. return;
  102. }
  103. /* Clear dirty rectangle: */
  104. info->x1 = info->y1 = INT_MAX;
  105. info->x2 = info->y2 = 0;
  106. spin_unlock_irqrestore(&info->dirty_lock, flags);
  107. if (x1 <= x2 && y1 <= y2)
  108. xenfb_do_update(info, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
  109. }
  110. static void xenfb_deferred_io(struct fb_info *fb_info,
  111. struct list_head *pagelist)
  112. {
  113. struct xenfb_info *info = fb_info->par;
  114. struct page *page;
  115. unsigned long beg, end;
  116. int y1, y2, miny, maxy;
  117. miny = INT_MAX;
  118. maxy = 0;
  119. list_for_each_entry(page, pagelist, lru) {
  120. beg = page->index << PAGE_SHIFT;
  121. end = beg + PAGE_SIZE - 1;
  122. y1 = beg / fb_info->fix.line_length;
  123. y2 = end / fb_info->fix.line_length;
  124. if (y2 >= fb_info->var.yres)
  125. y2 = fb_info->var.yres - 1;
  126. if (miny > y1)
  127. miny = y1;
  128. if (maxy < y2)
  129. maxy = y2;
  130. }
  131. xenfb_refresh(info, 0, miny, fb_info->var.xres, maxy - miny + 1);
  132. }
  133. static struct fb_deferred_io xenfb_defio = {
  134. .delay = HZ / 20,
  135. .deferred_io = xenfb_deferred_io,
  136. };
  137. static int xenfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  138. unsigned blue, unsigned transp,
  139. struct fb_info *info)
  140. {
  141. u32 v;
  142. if (regno > info->cmap.len)
  143. return 1;
  144. #define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
  145. red = CNVT_TOHW(red, info->var.red.length);
  146. green = CNVT_TOHW(green, info->var.green.length);
  147. blue = CNVT_TOHW(blue, info->var.blue.length);
  148. transp = CNVT_TOHW(transp, info->var.transp.length);
  149. #undef CNVT_TOHW
  150. v = (red << info->var.red.offset) |
  151. (green << info->var.green.offset) |
  152. (blue << info->var.blue.offset);
  153. switch (info->var.bits_per_pixel) {
  154. case 16:
  155. case 24:
  156. case 32:
  157. ((u32 *)info->pseudo_palette)[regno] = v;
  158. break;
  159. }
  160. return 0;
  161. }
  162. static void xenfb_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
  163. {
  164. struct xenfb_info *info = p->par;
  165. sys_fillrect(p, rect);
  166. xenfb_refresh(info, rect->dx, rect->dy, rect->width, rect->height);
  167. }
  168. static void xenfb_imageblit(struct fb_info *p, const struct fb_image *image)
  169. {
  170. struct xenfb_info *info = p->par;
  171. sys_imageblit(p, image);
  172. xenfb_refresh(info, image->dx, image->dy, image->width, image->height);
  173. }
  174. static void xenfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
  175. {
  176. struct xenfb_info *info = p->par;
  177. sys_copyarea(p, area);
  178. xenfb_refresh(info, area->dx, area->dy, area->width, area->height);
  179. }
  180. static ssize_t xenfb_write(struct fb_info *p, const char __user *buf,
  181. size_t count, loff_t *ppos)
  182. {
  183. struct xenfb_info *info = p->par;
  184. ssize_t res;
  185. res = fb_sys_write(p, buf, count, ppos);
  186. xenfb_refresh(info, 0, 0, info->page->width, info->page->height);
  187. return res;
  188. }
  189. static struct fb_ops xenfb_fb_ops = {
  190. .owner = THIS_MODULE,
  191. .fb_read = fb_sys_read,
  192. .fb_write = xenfb_write,
  193. .fb_setcolreg = xenfb_setcolreg,
  194. .fb_fillrect = xenfb_fillrect,
  195. .fb_copyarea = xenfb_copyarea,
  196. .fb_imageblit = xenfb_imageblit,
  197. };
  198. static irqreturn_t xenfb_event_handler(int rq, void *dev_id)
  199. {
  200. /*
  201. * No in events recognized, simply ignore them all.
  202. * If you need to recognize some, see xen-kbdfront's
  203. * input_handler() for how to do that.
  204. */
  205. struct xenfb_info *info = dev_id;
  206. struct xenfb_page *page = info->page;
  207. if (page->in_cons != page->in_prod) {
  208. info->page->in_cons = info->page->in_prod;
  209. notify_remote_via_irq(info->irq);
  210. }
  211. /* Flush dirty rectangle: */
  212. xenfb_refresh(info, INT_MAX, INT_MAX, -INT_MAX, -INT_MAX);
  213. return IRQ_HANDLED;
  214. }
  215. static int __devinit xenfb_probe(struct xenbus_device *dev,
  216. const struct xenbus_device_id *id)
  217. {
  218. struct xenfb_info *info;
  219. struct fb_info *fb_info;
  220. int ret;
  221. info = kzalloc(sizeof(*info), GFP_KERNEL);
  222. if (info == NULL) {
  223. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  224. return -ENOMEM;
  225. }
  226. dev->dev.driver_data = info;
  227. info->xbdev = dev;
  228. info->irq = -1;
  229. info->x1 = info->y1 = INT_MAX;
  230. spin_lock_init(&info->dirty_lock);
  231. info->fb = vmalloc(xenfb_mem_len);
  232. if (info->fb == NULL)
  233. goto error_nomem;
  234. memset(info->fb, 0, xenfb_mem_len);
  235. info->nr_pages = (xenfb_mem_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  236. info->mfns = vmalloc(sizeof(unsigned long) * info->nr_pages);
  237. if (!info->mfns)
  238. goto error_nomem;
  239. /* set up shared page */
  240. info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  241. if (!info->page)
  242. goto error_nomem;
  243. xenfb_init_shared_page(info);
  244. /* abusing framebuffer_alloc() to allocate pseudo_palette */
  245. fb_info = framebuffer_alloc(sizeof(u32) * 256, NULL);
  246. if (fb_info == NULL)
  247. goto error_nomem;
  248. /* complete the abuse: */
  249. fb_info->pseudo_palette = fb_info->par;
  250. fb_info->par = info;
  251. fb_info->screen_base = info->fb;
  252. fb_info->fbops = &xenfb_fb_ops;
  253. fb_info->var.xres_virtual = fb_info->var.xres = info->page->width;
  254. fb_info->var.yres_virtual = fb_info->var.yres = info->page->height;
  255. fb_info->var.bits_per_pixel = info->page->depth;
  256. fb_info->var.red = (struct fb_bitfield){16, 8, 0};
  257. fb_info->var.green = (struct fb_bitfield){8, 8, 0};
  258. fb_info->var.blue = (struct fb_bitfield){0, 8, 0};
  259. fb_info->var.activate = FB_ACTIVATE_NOW;
  260. fb_info->var.height = -1;
  261. fb_info->var.width = -1;
  262. fb_info->var.vmode = FB_VMODE_NONINTERLACED;
  263. fb_info->fix.visual = FB_VISUAL_TRUECOLOR;
  264. fb_info->fix.line_length = info->page->line_length;
  265. fb_info->fix.smem_start = 0;
  266. fb_info->fix.smem_len = xenfb_mem_len;
  267. strcpy(fb_info->fix.id, "xen");
  268. fb_info->fix.type = FB_TYPE_PACKED_PIXELS;
  269. fb_info->fix.accel = FB_ACCEL_NONE;
  270. fb_info->flags = FBINFO_FLAG_DEFAULT;
  271. ret = fb_alloc_cmap(&fb_info->cmap, 256, 0);
  272. if (ret < 0) {
  273. framebuffer_release(fb_info);
  274. xenbus_dev_fatal(dev, ret, "fb_alloc_cmap");
  275. goto error;
  276. }
  277. fb_info->fbdefio = &xenfb_defio;
  278. fb_deferred_io_init(fb_info);
  279. ret = register_framebuffer(fb_info);
  280. if (ret) {
  281. fb_deferred_io_cleanup(fb_info);
  282. fb_dealloc_cmap(&fb_info->cmap);
  283. framebuffer_release(fb_info);
  284. xenbus_dev_fatal(dev, ret, "register_framebuffer");
  285. goto error;
  286. }
  287. info->fb_info = fb_info;
  288. ret = xenfb_connect_backend(dev, info);
  289. if (ret < 0)
  290. goto error;
  291. xenfb_make_preferred_console();
  292. return 0;
  293. error_nomem:
  294. ret = -ENOMEM;
  295. xenbus_dev_fatal(dev, ret, "allocating device memory");
  296. error:
  297. xenfb_remove(dev);
  298. return ret;
  299. }
  300. static __devinit void
  301. xenfb_make_preferred_console(void)
  302. {
  303. struct console *c;
  304. if (console_set_on_cmdline)
  305. return;
  306. acquire_console_sem();
  307. for (c = console_drivers; c; c = c->next) {
  308. if (!strcmp(c->name, "tty") && c->index == 0)
  309. break;
  310. }
  311. release_console_sem();
  312. if (c) {
  313. unregister_console(c);
  314. c->flags |= CON_CONSDEV;
  315. c->flags &= ~CON_PRINTBUFFER; /* don't print again */
  316. register_console(c);
  317. }
  318. }
  319. static int xenfb_resume(struct xenbus_device *dev)
  320. {
  321. struct xenfb_info *info = dev->dev.driver_data;
  322. xenfb_disconnect_backend(info);
  323. xenfb_init_shared_page(info);
  324. return xenfb_connect_backend(dev, info);
  325. }
  326. static int xenfb_remove(struct xenbus_device *dev)
  327. {
  328. struct xenfb_info *info = dev->dev.driver_data;
  329. xenfb_disconnect_backend(info);
  330. if (info->fb_info) {
  331. fb_deferred_io_cleanup(info->fb_info);
  332. unregister_framebuffer(info->fb_info);
  333. fb_dealloc_cmap(&info->fb_info->cmap);
  334. framebuffer_release(info->fb_info);
  335. }
  336. free_page((unsigned long)info->page);
  337. vfree(info->mfns);
  338. vfree(info->fb);
  339. kfree(info);
  340. return 0;
  341. }
  342. static unsigned long vmalloc_to_mfn(void *address)
  343. {
  344. return pfn_to_mfn(vmalloc_to_pfn(address));
  345. }
  346. static void xenfb_init_shared_page(struct xenfb_info *info)
  347. {
  348. int i;
  349. for (i = 0; i < info->nr_pages; i++)
  350. info->mfns[i] = vmalloc_to_mfn(info->fb + i * PAGE_SIZE);
  351. info->page->pd[0] = vmalloc_to_mfn(info->mfns);
  352. info->page->pd[1] = 0;
  353. info->page->width = XENFB_WIDTH;
  354. info->page->height = XENFB_HEIGHT;
  355. info->page->depth = XENFB_DEPTH;
  356. info->page->line_length = (info->page->depth / 8) * info->page->width;
  357. info->page->mem_length = xenfb_mem_len;
  358. info->page->in_cons = info->page->in_prod = 0;
  359. info->page->out_cons = info->page->out_prod = 0;
  360. }
  361. static int xenfb_connect_backend(struct xenbus_device *dev,
  362. struct xenfb_info *info)
  363. {
  364. int ret, evtchn;
  365. struct xenbus_transaction xbt;
  366. ret = xenbus_alloc_evtchn(dev, &evtchn);
  367. if (ret)
  368. return ret;
  369. ret = bind_evtchn_to_irqhandler(evtchn, xenfb_event_handler,
  370. 0, dev->devicetype, info);
  371. if (ret < 0) {
  372. xenbus_free_evtchn(dev, evtchn);
  373. xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
  374. return ret;
  375. }
  376. info->irq = ret;
  377. again:
  378. ret = xenbus_transaction_start(&xbt);
  379. if (ret) {
  380. xenbus_dev_fatal(dev, ret, "starting transaction");
  381. return ret;
  382. }
  383. ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu",
  384. virt_to_mfn(info->page));
  385. if (ret)
  386. goto error_xenbus;
  387. ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
  388. evtchn);
  389. if (ret)
  390. goto error_xenbus;
  391. ret = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
  392. XEN_IO_PROTO_ABI_NATIVE);
  393. if (ret)
  394. goto error_xenbus;
  395. ret = xenbus_printf(xbt, dev->nodename, "feature-update", "1");
  396. if (ret)
  397. goto error_xenbus;
  398. ret = xenbus_transaction_end(xbt, 0);
  399. if (ret) {
  400. if (ret == -EAGAIN)
  401. goto again;
  402. xenbus_dev_fatal(dev, ret, "completing transaction");
  403. return ret;
  404. }
  405. xenbus_switch_state(dev, XenbusStateInitialised);
  406. return 0;
  407. error_xenbus:
  408. xenbus_transaction_end(xbt, 1);
  409. xenbus_dev_fatal(dev, ret, "writing xenstore");
  410. return ret;
  411. }
  412. static void xenfb_disconnect_backend(struct xenfb_info *info)
  413. {
  414. if (info->irq >= 0)
  415. unbind_from_irqhandler(info->irq, info);
  416. info->irq = -1;
  417. }
  418. static void xenfb_backend_changed(struct xenbus_device *dev,
  419. enum xenbus_state backend_state)
  420. {
  421. struct xenfb_info *info = dev->dev.driver_data;
  422. int val;
  423. switch (backend_state) {
  424. case XenbusStateInitialising:
  425. case XenbusStateInitialised:
  426. case XenbusStateUnknown:
  427. case XenbusStateClosed:
  428. break;
  429. case XenbusStateInitWait:
  430. InitWait:
  431. xenbus_switch_state(dev, XenbusStateConnected);
  432. break;
  433. case XenbusStateConnected:
  434. /*
  435. * Work around xenbus race condition: If backend goes
  436. * through InitWait to Connected fast enough, we can
  437. * get Connected twice here.
  438. */
  439. if (dev->state != XenbusStateConnected)
  440. goto InitWait; /* no InitWait seen yet, fudge it */
  441. if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  442. "request-update", "%d", &val) < 0)
  443. val = 0;
  444. if (val)
  445. info->update_wanted = 1;
  446. break;
  447. case XenbusStateClosing:
  448. xenbus_frontend_closed(dev);
  449. break;
  450. }
  451. }
  452. static struct xenbus_device_id xenfb_ids[] = {
  453. { "vfb" },
  454. { "" }
  455. };
  456. static struct xenbus_driver xenfb = {
  457. .name = "vfb",
  458. .owner = THIS_MODULE,
  459. .ids = xenfb_ids,
  460. .probe = xenfb_probe,
  461. .remove = xenfb_remove,
  462. .resume = xenfb_resume,
  463. .otherend_changed = xenfb_backend_changed,
  464. };
  465. static int __init xenfb_init(void)
  466. {
  467. if (!is_running_on_xen())
  468. return -ENODEV;
  469. /* Nothing to do if running in dom0. */
  470. if (is_initial_xendomain())
  471. return -ENODEV;
  472. return xenbus_register_frontend(&xenfb);
  473. }
  474. static void __exit xenfb_cleanup(void)
  475. {
  476. xenbus_unregister_driver(&xenfb);
  477. }
  478. module_init(xenfb_init);
  479. module_exit(xenfb_cleanup);
  480. MODULE_DESCRIPTION("Xen virtual framebuffer device frontend");
  481. MODULE_LICENSE("GPL");
  482. MODULE_ALIAS("xen:vfb");