2 * Xen para-virtual frame buffer device
4 * Copyright (C) 2005-2006 Anthony Liguori <aliguori@us.ibm.com>
5 * Copyright (C) 2006-2008 Red Hat, Inc., Markus Armbruster <armbru@redhat.com>
7 * Based on linux/drivers/video/q40fb.c
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
17 * Switch to grant tables when they become capable of dealing with the
21 #include <linux/console.h>
22 #include <linux/kernel.h>
23 #include <linux/errno.h>
25 #include <linux/module.h>
26 #include <linux/vmalloc.h>
28 #include <asm/xen/hypervisor.h>
29 #include <xen/events.h>
31 #include <xen/interface/io/fbif.h>
32 #include <xen/interface/io/protocols.h>
33 #include <xen/xenbus.h>
37 struct fb_info *fb_info;
38 int x1, y1, x2, y2; /* dirty rectangle,
39 protected by dirty_lock */
40 spinlock_t dirty_lock;
43 struct xenfb_page *page;
45 int update_wanted; /* XENFB_TYPE_UPDATE wanted */
47 struct xenbus_device *xbdev;
50 static u32 xenfb_mem_len = XENFB_WIDTH * XENFB_HEIGHT * XENFB_DEPTH / 8;
52 static void xenfb_make_preferred_console(void);
53 static int xenfb_remove(struct xenbus_device *);
54 static void xenfb_init_shared_page(struct xenfb_info *);
55 static int xenfb_connect_backend(struct xenbus_device *, struct xenfb_info *);
56 static void xenfb_disconnect_backend(struct xenfb_info *);
58 static void xenfb_do_update(struct xenfb_info *info,
59 int x, int y, int w, int h)
61 union xenfb_out_event event;
64 event.type = XENFB_TYPE_UPDATE;
67 event.update.width = w;
68 event.update.height = h;
70 prod = info->page->out_prod;
71 /* caller ensures !xenfb_queue_full() */
72 mb(); /* ensure ring space available */
73 XENFB_OUT_RING_REF(info->page, prod) = event;
74 wmb(); /* ensure ring contents visible */
75 info->page->out_prod = prod + 1;
77 notify_remote_via_irq(info->irq);
80 static int xenfb_queue_full(struct xenfb_info *info)
84 prod = info->page->out_prod;
85 cons = info->page->out_cons;
86 return prod - cons == XENFB_OUT_RING_LEN;
89 static void xenfb_refresh(struct xenfb_info *info,
90 int x1, int y1, int w, int h)
96 if (!info->update_wanted)
99 spin_lock_irqsave(&info->dirty_lock, flags);
101 /* Combine with dirty rectangle: */
111 if (xenfb_queue_full(info)) {
112 /* Can't send right now, stash it in the dirty rectangle */
117 spin_unlock_irqrestore(&info->dirty_lock, flags);
121 /* Clear dirty rectangle: */
122 info->x1 = info->y1 = INT_MAX;
123 info->x2 = info->y2 = 0;
125 spin_unlock_irqrestore(&info->dirty_lock, flags);
127 if (x1 <= x2 && y1 <= y2)
128 xenfb_do_update(info, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
131 static void xenfb_deferred_io(struct fb_info *fb_info,
132 struct list_head *pagelist)
134 struct xenfb_info *info = fb_info->par;
136 unsigned long beg, end;
137 int y1, y2, miny, maxy;
141 list_for_each_entry(page, pagelist, lru) {
142 beg = page->index << PAGE_SHIFT;
143 end = beg + PAGE_SIZE - 1;
144 y1 = beg / fb_info->fix.line_length;
145 y2 = end / fb_info->fix.line_length;
146 if (y2 >= fb_info->var.yres)
147 y2 = fb_info->var.yres - 1;
153 xenfb_refresh(info, 0, miny, fb_info->var.xres, maxy - miny + 1);
156 static struct fb_deferred_io xenfb_defio = {
158 .deferred_io = xenfb_deferred_io,
161 static int xenfb_setcolreg(unsigned regno, unsigned red, unsigned green,
162 unsigned blue, unsigned transp,
163 struct fb_info *info)
167 if (regno > info->cmap.len)
170 #define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
171 red = CNVT_TOHW(red, info->var.red.length);
172 green = CNVT_TOHW(green, info->var.green.length);
173 blue = CNVT_TOHW(blue, info->var.blue.length);
174 transp = CNVT_TOHW(transp, info->var.transp.length);
177 v = (red << info->var.red.offset) |
178 (green << info->var.green.offset) |
179 (blue << info->var.blue.offset);
181 switch (info->var.bits_per_pixel) {
185 ((u32 *)info->pseudo_palette)[regno] = v;
192 static void xenfb_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
194 struct xenfb_info *info = p->par;
196 sys_fillrect(p, rect);
197 xenfb_refresh(info, rect->dx, rect->dy, rect->width, rect->height);
200 static void xenfb_imageblit(struct fb_info *p, const struct fb_image *image)
202 struct xenfb_info *info = p->par;
204 sys_imageblit(p, image);
205 xenfb_refresh(info, image->dx, image->dy, image->width, image->height);
208 static void xenfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
210 struct xenfb_info *info = p->par;
212 sys_copyarea(p, area);
213 xenfb_refresh(info, area->dx, area->dy, area->width, area->height);
216 static ssize_t xenfb_write(struct fb_info *p, const char __user *buf,
217 size_t count, loff_t *ppos)
219 struct xenfb_info *info = p->par;
222 res = fb_sys_write(p, buf, count, ppos);
223 xenfb_refresh(info, 0, 0, info->page->width, info->page->height);
227 static struct fb_ops xenfb_fb_ops = {
228 .owner = THIS_MODULE,
229 .fb_read = fb_sys_read,
230 .fb_write = xenfb_write,
231 .fb_setcolreg = xenfb_setcolreg,
232 .fb_fillrect = xenfb_fillrect,
233 .fb_copyarea = xenfb_copyarea,
234 .fb_imageblit = xenfb_imageblit,
237 static irqreturn_t xenfb_event_handler(int rq, void *dev_id)
240 * No in events recognized, simply ignore them all.
241 * If you need to recognize some, see xen-kbdfront's
242 * input_handler() for how to do that.
244 struct xenfb_info *info = dev_id;
245 struct xenfb_page *page = info->page;
247 if (page->in_cons != page->in_prod) {
248 info->page->in_cons = info->page->in_prod;
249 notify_remote_via_irq(info->irq);
252 /* Flush dirty rectangle: */
253 xenfb_refresh(info, INT_MAX, INT_MAX, -INT_MAX, -INT_MAX);
258 static int __devinit xenfb_probe(struct xenbus_device *dev,
259 const struct xenbus_device_id *id)
261 struct xenfb_info *info;
262 struct fb_info *fb_info;
265 info = kzalloc(sizeof(*info), GFP_KERNEL);
267 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
270 dev->dev.driver_data = info;
273 info->x1 = info->y1 = INT_MAX;
274 spin_lock_init(&info->dirty_lock);
276 info->fb = vmalloc(xenfb_mem_len);
277 if (info->fb == NULL)
279 memset(info->fb, 0, xenfb_mem_len);
281 info->nr_pages = (xenfb_mem_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
283 info->mfns = vmalloc(sizeof(unsigned long) * info->nr_pages);
287 /* set up shared page */
288 info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
292 xenfb_init_shared_page(info);
294 /* abusing framebuffer_alloc() to allocate pseudo_palette */
295 fb_info = framebuffer_alloc(sizeof(u32) * 256, NULL);
299 /* complete the abuse: */
300 fb_info->pseudo_palette = fb_info->par;
303 fb_info->screen_base = info->fb;
305 fb_info->fbops = &xenfb_fb_ops;
306 fb_info->var.xres_virtual = fb_info->var.xres = info->page->width;
307 fb_info->var.yres_virtual = fb_info->var.yres = info->page->height;
308 fb_info->var.bits_per_pixel = info->page->depth;
310 fb_info->var.red = (struct fb_bitfield){16, 8, 0};
311 fb_info->var.green = (struct fb_bitfield){8, 8, 0};
312 fb_info->var.blue = (struct fb_bitfield){0, 8, 0};
314 fb_info->var.activate = FB_ACTIVATE_NOW;
315 fb_info->var.height = -1;
316 fb_info->var.width = -1;
317 fb_info->var.vmode = FB_VMODE_NONINTERLACED;
319 fb_info->fix.visual = FB_VISUAL_TRUECOLOR;
320 fb_info->fix.line_length = info->page->line_length;
321 fb_info->fix.smem_start = 0;
322 fb_info->fix.smem_len = xenfb_mem_len;
323 strcpy(fb_info->fix.id, "xen");
324 fb_info->fix.type = FB_TYPE_PACKED_PIXELS;
325 fb_info->fix.accel = FB_ACCEL_NONE;
327 fb_info->flags = FBINFO_FLAG_DEFAULT;
329 ret = fb_alloc_cmap(&fb_info->cmap, 256, 0);
331 framebuffer_release(fb_info);
332 xenbus_dev_fatal(dev, ret, "fb_alloc_cmap");
336 fb_info->fbdefio = &xenfb_defio;
337 fb_deferred_io_init(fb_info);
339 ret = register_framebuffer(fb_info);
341 fb_deferred_io_cleanup(fb_info);
342 fb_dealloc_cmap(&fb_info->cmap);
343 framebuffer_release(fb_info);
344 xenbus_dev_fatal(dev, ret, "register_framebuffer");
347 info->fb_info = fb_info;
349 ret = xenfb_connect_backend(dev, info);
353 xenfb_make_preferred_console();
358 xenbus_dev_fatal(dev, ret, "allocating device memory");
364 static __devinit void
365 xenfb_make_preferred_console(void)
369 if (console_set_on_cmdline)
372 acquire_console_sem();
373 for (c = console_drivers; c; c = c->next) {
374 if (!strcmp(c->name, "tty") && c->index == 0)
377 release_console_sem();
379 unregister_console(c);
380 c->flags |= CON_CONSDEV;
381 c->flags &= ~CON_PRINTBUFFER; /* don't print again */
386 static int xenfb_resume(struct xenbus_device *dev)
388 struct xenfb_info *info = dev->dev.driver_data;
390 xenfb_disconnect_backend(info);
391 xenfb_init_shared_page(info);
392 return xenfb_connect_backend(dev, info);
395 static int xenfb_remove(struct xenbus_device *dev)
397 struct xenfb_info *info = dev->dev.driver_data;
399 xenfb_disconnect_backend(info);
401 fb_deferred_io_cleanup(info->fb_info);
402 unregister_framebuffer(info->fb_info);
403 fb_dealloc_cmap(&info->fb_info->cmap);
404 framebuffer_release(info->fb_info);
406 free_page((unsigned long)info->page);
414 static unsigned long vmalloc_to_mfn(void *address)
416 return pfn_to_mfn(vmalloc_to_pfn(address));
419 static void xenfb_init_shared_page(struct xenfb_info *info)
423 for (i = 0; i < info->nr_pages; i++)
424 info->mfns[i] = vmalloc_to_mfn(info->fb + i * PAGE_SIZE);
426 info->page->pd[0] = vmalloc_to_mfn(info->mfns);
427 info->page->pd[1] = 0;
428 info->page->width = XENFB_WIDTH;
429 info->page->height = XENFB_HEIGHT;
430 info->page->depth = XENFB_DEPTH;
431 info->page->line_length = (info->page->depth / 8) * info->page->width;
432 info->page->mem_length = xenfb_mem_len;
433 info->page->in_cons = info->page->in_prod = 0;
434 info->page->out_cons = info->page->out_prod = 0;
437 static int xenfb_connect_backend(struct xenbus_device *dev,
438 struct xenfb_info *info)
441 struct xenbus_transaction xbt;
443 ret = xenbus_alloc_evtchn(dev, &evtchn);
446 ret = bind_evtchn_to_irqhandler(evtchn, xenfb_event_handler,
447 0, dev->devicetype, info);
449 xenbus_free_evtchn(dev, evtchn);
450 xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
456 ret = xenbus_transaction_start(&xbt);
458 xenbus_dev_fatal(dev, ret, "starting transaction");
461 ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu",
462 virt_to_mfn(info->page));
465 ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
469 ret = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
470 XEN_IO_PROTO_ABI_NATIVE);
473 ret = xenbus_printf(xbt, dev->nodename, "feature-update", "1");
476 ret = xenbus_transaction_end(xbt, 0);
480 xenbus_dev_fatal(dev, ret, "completing transaction");
484 xenbus_switch_state(dev, XenbusStateInitialised);
488 xenbus_transaction_end(xbt, 1);
489 xenbus_dev_fatal(dev, ret, "writing xenstore");
493 static void xenfb_disconnect_backend(struct xenfb_info *info)
496 unbind_from_irqhandler(info->irq, info);
500 static void xenfb_backend_changed(struct xenbus_device *dev,
501 enum xenbus_state backend_state)
503 struct xenfb_info *info = dev->dev.driver_data;
506 switch (backend_state) {
507 case XenbusStateInitialising:
508 case XenbusStateInitialised:
509 case XenbusStateUnknown:
510 case XenbusStateClosed:
513 case XenbusStateInitWait:
515 xenbus_switch_state(dev, XenbusStateConnected);
518 case XenbusStateConnected:
520 * Work around xenbus race condition: If backend goes
521 * through InitWait to Connected fast enough, we can
522 * get Connected twice here.
524 if (dev->state != XenbusStateConnected)
525 goto InitWait; /* no InitWait seen yet, fudge it */
527 if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
528 "request-update", "%d", &val) < 0)
531 info->update_wanted = 1;
534 case XenbusStateClosing:
535 xenbus_frontend_closed(dev);
540 static struct xenbus_device_id xenfb_ids[] = {
545 static struct xenbus_driver xenfb = {
547 .owner = THIS_MODULE,
549 .probe = xenfb_probe,
550 .remove = xenfb_remove,
551 .resume = xenfb_resume,
552 .otherend_changed = xenfb_backend_changed,
555 static int __init xenfb_init(void)
557 if (!is_running_on_xen())
560 /* Nothing to do if running in dom0. */
561 if (is_initial_xendomain())
564 return xenbus_register_frontend(&xenfb);
567 static void __exit xenfb_cleanup(void)
569 xenbus_unregister_driver(&xenfb);
572 module_init(xenfb_init);
573 module_exit(xenfb_cleanup);
575 MODULE_LICENSE("GPL");