xen-fbfront.c 13 KB

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