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/slab.h>
27 #include <linux/vmalloc.h>
30 #include <asm/xen/hypervisor.h>
33 #include <xen/events.h>
35 #include <xen/interface/io/fbif.h>
36 #include <xen/interface/io/protocols.h>
37 #include <xen/xenbus.h>
38 #include <xen/platform_pci.h>
42 struct fb_info
*fb_info
;
43 int x1
, y1
, x2
, y2
; /* dirty rectangle,
44 protected by dirty_lock */
45 spinlock_t dirty_lock
;
48 struct xenfb_page
*page
;
50 int update_wanted
; /* XENFB_TYPE_UPDATE wanted */
51 int feature_resize
; /* XENFB_TYPE_RESIZE ok */
52 struct xenfb_resize resize
; /* protected by resize_lock */
53 int resize_dpy
; /* ditto */
54 spinlock_t resize_lock
;
56 struct xenbus_device
*xbdev
;
59 #define XENFB_DEFAULT_FB_LEN (XENFB_WIDTH * XENFB_HEIGHT * XENFB_DEPTH / 8)
61 enum { KPARAM_MEM
, KPARAM_WIDTH
, KPARAM_HEIGHT
, KPARAM_CNT
};
62 static int video
[KPARAM_CNT
] = { 2, XENFB_WIDTH
, XENFB_HEIGHT
};
63 module_param_array(video
, int, NULL
, 0);
64 MODULE_PARM_DESC(video
,
65 "Video memory size in MB, width, height in pixels (default 2,800,600)");
67 static void xenfb_make_preferred_console(void);
68 static int xenfb_remove(struct xenbus_device
*);
69 static void xenfb_init_shared_page(struct xenfb_info
*, struct fb_info
*);
70 static int xenfb_connect_backend(struct xenbus_device
*, struct xenfb_info
*);
71 static void xenfb_disconnect_backend(struct xenfb_info
*);
73 static void xenfb_send_event(struct xenfb_info
*info
,
74 union xenfb_out_event
*event
)
78 prod
= info
->page
->out_prod
;
79 /* caller ensures !xenfb_queue_full() */
80 mb(); /* ensure ring space available */
81 XENFB_OUT_RING_REF(info
->page
, prod
) = *event
;
82 wmb(); /* ensure ring contents visible */
83 info
->page
->out_prod
= prod
+ 1;
85 notify_remote_via_irq(info
->irq
);
88 static void xenfb_do_update(struct xenfb_info
*info
,
89 int x
, int y
, int w
, int h
)
91 union xenfb_out_event event
;
93 memset(&event
, 0, sizeof(event
));
94 event
.type
= XENFB_TYPE_UPDATE
;
97 event
.update
.width
= w
;
98 event
.update
.height
= h
;
100 /* caller ensures !xenfb_queue_full() */
101 xenfb_send_event(info
, &event
);
104 static void xenfb_do_resize(struct xenfb_info
*info
)
106 union xenfb_out_event event
;
108 memset(&event
, 0, sizeof(event
));
109 event
.resize
= info
->resize
;
111 /* caller ensures !xenfb_queue_full() */
112 xenfb_send_event(info
, &event
);
115 static int xenfb_queue_full(struct xenfb_info
*info
)
119 prod
= info
->page
->out_prod
;
120 cons
= info
->page
->out_cons
;
121 return prod
- cons
== XENFB_OUT_RING_LEN
;
124 static void xenfb_handle_resize_dpy(struct xenfb_info
*info
)
128 spin_lock_irqsave(&info
->resize_lock
, flags
);
129 if (info
->resize_dpy
) {
130 if (!xenfb_queue_full(info
)) {
131 info
->resize_dpy
= 0;
132 xenfb_do_resize(info
);
135 spin_unlock_irqrestore(&info
->resize_lock
, flags
);
138 static void xenfb_refresh(struct xenfb_info
*info
,
139 int x1
, int y1
, int w
, int h
)
145 xenfb_handle_resize_dpy(info
);
147 if (!info
->update_wanted
)
150 spin_lock_irqsave(&info
->dirty_lock
, flags
);
152 /* Combine with dirty rectangle: */
162 if (xenfb_queue_full(info
)) {
163 /* Can't send right now, stash it in the dirty rectangle */
168 spin_unlock_irqrestore(&info
->dirty_lock
, flags
);
172 /* Clear dirty rectangle: */
173 info
->x1
= info
->y1
= INT_MAX
;
174 info
->x2
= info
->y2
= 0;
176 spin_unlock_irqrestore(&info
->dirty_lock
, flags
);
178 if (x1
<= x2
&& y1
<= y2
)
179 xenfb_do_update(info
, x1
, y1
, x2
- x1
+ 1, y2
- y1
+ 1);
182 static void xenfb_deferred_io(struct fb_info
*fb_info
,
183 struct list_head
*pagelist
)
185 struct xenfb_info
*info
= fb_info
->par
;
187 unsigned long beg
, end
;
188 int y1
, y2
, miny
, maxy
;
192 list_for_each_entry(page
, pagelist
, lru
) {
193 beg
= page
->index
<< PAGE_SHIFT
;
194 end
= beg
+ PAGE_SIZE
- 1;
195 y1
= beg
/ fb_info
->fix
.line_length
;
196 y2
= end
/ fb_info
->fix
.line_length
;
197 if (y2
>= fb_info
->var
.yres
)
198 y2
= fb_info
->var
.yres
- 1;
204 xenfb_refresh(info
, 0, miny
, fb_info
->var
.xres
, maxy
- miny
+ 1);
207 static struct fb_deferred_io xenfb_defio
= {
209 .deferred_io
= xenfb_deferred_io
,
212 static int xenfb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
213 unsigned blue
, unsigned transp
,
214 struct fb_info
*info
)
218 if (regno
> info
->cmap
.len
)
221 #define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
222 red
= CNVT_TOHW(red
, info
->var
.red
.length
);
223 green
= CNVT_TOHW(green
, info
->var
.green
.length
);
224 blue
= CNVT_TOHW(blue
, info
->var
.blue
.length
);
225 transp
= CNVT_TOHW(transp
, info
->var
.transp
.length
);
228 v
= (red
<< info
->var
.red
.offset
) |
229 (green
<< info
->var
.green
.offset
) |
230 (blue
<< info
->var
.blue
.offset
);
232 switch (info
->var
.bits_per_pixel
) {
236 ((u32
*)info
->pseudo_palette
)[regno
] = v
;
243 static void xenfb_fillrect(struct fb_info
*p
, const struct fb_fillrect
*rect
)
245 struct xenfb_info
*info
= p
->par
;
247 sys_fillrect(p
, rect
);
248 xenfb_refresh(info
, rect
->dx
, rect
->dy
, rect
->width
, rect
->height
);
251 static void xenfb_imageblit(struct fb_info
*p
, const struct fb_image
*image
)
253 struct xenfb_info
*info
= p
->par
;
255 sys_imageblit(p
, image
);
256 xenfb_refresh(info
, image
->dx
, image
->dy
, image
->width
, image
->height
);
259 static void xenfb_copyarea(struct fb_info
*p
, const struct fb_copyarea
*area
)
261 struct xenfb_info
*info
= p
->par
;
263 sys_copyarea(p
, area
);
264 xenfb_refresh(info
, area
->dx
, area
->dy
, area
->width
, area
->height
);
267 static ssize_t
xenfb_write(struct fb_info
*p
, const char __user
*buf
,
268 size_t count
, loff_t
*ppos
)
270 struct xenfb_info
*info
= p
->par
;
273 res
= fb_sys_write(p
, buf
, count
, ppos
);
274 xenfb_refresh(info
, 0, 0, info
->page
->width
, info
->page
->height
);
279 xenfb_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
281 struct xenfb_info
*xenfb_info
;
282 int required_mem_len
;
284 xenfb_info
= info
->par
;
286 if (!xenfb_info
->feature_resize
) {
287 if (var
->xres
== video
[KPARAM_WIDTH
] &&
288 var
->yres
== video
[KPARAM_HEIGHT
] &&
289 var
->bits_per_pixel
== xenfb_info
->page
->depth
) {
295 /* Can't resize past initial width and height */
296 if (var
->xres
> video
[KPARAM_WIDTH
] || var
->yres
> video
[KPARAM_HEIGHT
])
299 required_mem_len
= var
->xres
* var
->yres
* xenfb_info
->page
->depth
/ 8;
300 if (var
->bits_per_pixel
== xenfb_info
->page
->depth
&&
301 var
->xres
<= info
->fix
.line_length
/ (XENFB_DEPTH
/ 8) &&
302 required_mem_len
<= info
->fix
.smem_len
) {
303 var
->xres_virtual
= var
->xres
;
304 var
->yres_virtual
= var
->yres
;
310 static int xenfb_set_par(struct fb_info
*info
)
312 struct xenfb_info
*xenfb_info
;
315 xenfb_info
= info
->par
;
317 spin_lock_irqsave(&xenfb_info
->resize_lock
, flags
);
318 xenfb_info
->resize
.type
= XENFB_TYPE_RESIZE
;
319 xenfb_info
->resize
.width
= info
->var
.xres
;
320 xenfb_info
->resize
.height
= info
->var
.yres
;
321 xenfb_info
->resize
.stride
= info
->fix
.line_length
;
322 xenfb_info
->resize
.depth
= info
->var
.bits_per_pixel
;
323 xenfb_info
->resize
.offset
= 0;
324 xenfb_info
->resize_dpy
= 1;
325 spin_unlock_irqrestore(&xenfb_info
->resize_lock
, flags
);
329 static struct fb_ops xenfb_fb_ops
= {
330 .owner
= THIS_MODULE
,
331 .fb_read
= fb_sys_read
,
332 .fb_write
= xenfb_write
,
333 .fb_setcolreg
= xenfb_setcolreg
,
334 .fb_fillrect
= xenfb_fillrect
,
335 .fb_copyarea
= xenfb_copyarea
,
336 .fb_imageblit
= xenfb_imageblit
,
337 .fb_check_var
= xenfb_check_var
,
338 .fb_set_par
= xenfb_set_par
,
341 static irqreturn_t
xenfb_event_handler(int rq
, void *dev_id
)
344 * No in events recognized, simply ignore them all.
345 * If you need to recognize some, see xen-kbdfront's
346 * input_handler() for how to do that.
348 struct xenfb_info
*info
= dev_id
;
349 struct xenfb_page
*page
= info
->page
;
351 if (page
->in_cons
!= page
->in_prod
) {
352 info
->page
->in_cons
= info
->page
->in_prod
;
353 notify_remote_via_irq(info
->irq
);
356 /* Flush dirty rectangle: */
357 xenfb_refresh(info
, INT_MAX
, INT_MAX
, -INT_MAX
, -INT_MAX
);
362 static int xenfb_probe(struct xenbus_device
*dev
,
363 const struct xenbus_device_id
*id
)
365 struct xenfb_info
*info
;
366 struct fb_info
*fb_info
;
371 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
373 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
377 /* Limit kernel param videoram amount to what is in xenstore */
378 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "videoram", "%d", &val
) == 1) {
379 if (val
< video
[KPARAM_MEM
])
380 video
[KPARAM_MEM
] = val
;
383 /* If requested res does not fit in available memory, use default */
384 fb_size
= video
[KPARAM_MEM
] * 1024 * 1024;
385 if (video
[KPARAM_WIDTH
] * video
[KPARAM_HEIGHT
] * XENFB_DEPTH
/ 8
387 video
[KPARAM_WIDTH
] = XENFB_WIDTH
;
388 video
[KPARAM_HEIGHT
] = XENFB_HEIGHT
;
389 fb_size
= XENFB_DEFAULT_FB_LEN
;
392 dev_set_drvdata(&dev
->dev
, info
);
395 info
->x1
= info
->y1
= INT_MAX
;
396 spin_lock_init(&info
->dirty_lock
);
397 spin_lock_init(&info
->resize_lock
);
399 info
->fb
= vzalloc(fb_size
);
400 if (info
->fb
== NULL
)
403 info
->nr_pages
= (fb_size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
405 info
->gfns
= vmalloc(sizeof(unsigned long) * info
->nr_pages
);
409 /* set up shared page */
410 info
->page
= (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
414 /* abusing framebuffer_alloc() to allocate pseudo_palette */
415 fb_info
= framebuffer_alloc(sizeof(u32
) * 256, NULL
);
419 /* complete the abuse: */
420 fb_info
->pseudo_palette
= fb_info
->par
;
423 fb_info
->screen_base
= info
->fb
;
425 fb_info
->fbops
= &xenfb_fb_ops
;
426 fb_info
->var
.xres_virtual
= fb_info
->var
.xres
= video
[KPARAM_WIDTH
];
427 fb_info
->var
.yres_virtual
= fb_info
->var
.yres
= video
[KPARAM_HEIGHT
];
428 fb_info
->var
.bits_per_pixel
= XENFB_DEPTH
;
430 fb_info
->var
.red
= (struct fb_bitfield
){16, 8, 0};
431 fb_info
->var
.green
= (struct fb_bitfield
){8, 8, 0};
432 fb_info
->var
.blue
= (struct fb_bitfield
){0, 8, 0};
434 fb_info
->var
.activate
= FB_ACTIVATE_NOW
;
435 fb_info
->var
.height
= -1;
436 fb_info
->var
.width
= -1;
437 fb_info
->var
.vmode
= FB_VMODE_NONINTERLACED
;
439 fb_info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
440 fb_info
->fix
.line_length
= fb_info
->var
.xres
* XENFB_DEPTH
/ 8;
441 fb_info
->fix
.smem_start
= 0;
442 fb_info
->fix
.smem_len
= fb_size
;
443 strcpy(fb_info
->fix
.id
, "xen");
444 fb_info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
445 fb_info
->fix
.accel
= FB_ACCEL_NONE
;
447 fb_info
->flags
= FBINFO_FLAG_DEFAULT
| FBINFO_VIRTFB
;
449 ret
= fb_alloc_cmap(&fb_info
->cmap
, 256, 0);
451 framebuffer_release(fb_info
);
452 xenbus_dev_fatal(dev
, ret
, "fb_alloc_cmap");
456 fb_info
->fbdefio
= &xenfb_defio
;
457 fb_deferred_io_init(fb_info
);
459 xenfb_init_shared_page(info
, fb_info
);
461 ret
= xenfb_connect_backend(dev
, info
);
463 xenbus_dev_fatal(dev
, ret
, "xenfb_connect_backend");
467 ret
= register_framebuffer(fb_info
);
469 xenbus_dev_fatal(dev
, ret
, "register_framebuffer");
472 info
->fb_info
= fb_info
;
474 xenfb_make_preferred_console();
478 fb_deferred_io_cleanup(fb_info
);
479 fb_dealloc_cmap(&fb_info
->cmap
);
480 framebuffer_release(fb_info
);
484 xenbus_dev_fatal(dev
, ret
, "allocating device memory");
491 static void xenfb_make_preferred_console(void)
495 if (console_set_on_cmdline
)
499 for_each_console(c
) {
500 if (!strcmp(c
->name
, "tty") && c
->index
== 0)
505 unregister_console(c
);
506 c
->flags
|= CON_CONSDEV
;
507 c
->flags
&= ~CON_PRINTBUFFER
; /* don't print again */
512 static int xenfb_resume(struct xenbus_device
*dev
)
514 struct xenfb_info
*info
= dev_get_drvdata(&dev
->dev
);
516 xenfb_disconnect_backend(info
);
517 xenfb_init_shared_page(info
, info
->fb_info
);
518 return xenfb_connect_backend(dev
, info
);
521 static int xenfb_remove(struct xenbus_device
*dev
)
523 struct xenfb_info
*info
= dev_get_drvdata(&dev
->dev
);
525 xenfb_disconnect_backend(info
);
527 fb_deferred_io_cleanup(info
->fb_info
);
528 unregister_framebuffer(info
->fb_info
);
529 fb_dealloc_cmap(&info
->fb_info
->cmap
);
530 framebuffer_release(info
->fb_info
);
532 free_page((unsigned long)info
->page
);
540 static unsigned long vmalloc_to_gfn(void *address
)
542 return xen_page_to_gfn(vmalloc_to_page(address
));
545 static void xenfb_init_shared_page(struct xenfb_info
*info
,
546 struct fb_info
*fb_info
)
549 int epd
= PAGE_SIZE
/ sizeof(info
->gfns
[0]);
551 for (i
= 0; i
< info
->nr_pages
; i
++)
552 info
->gfns
[i
] = vmalloc_to_gfn(info
->fb
+ i
* PAGE_SIZE
);
554 for (i
= 0; i
* epd
< info
->nr_pages
; i
++)
555 info
->page
->pd
[i
] = vmalloc_to_gfn(&info
->gfns
[i
* epd
]);
557 info
->page
->width
= fb_info
->var
.xres
;
558 info
->page
->height
= fb_info
->var
.yres
;
559 info
->page
->depth
= fb_info
->var
.bits_per_pixel
;
560 info
->page
->line_length
= fb_info
->fix
.line_length
;
561 info
->page
->mem_length
= fb_info
->fix
.smem_len
;
562 info
->page
->in_cons
= info
->page
->in_prod
= 0;
563 info
->page
->out_cons
= info
->page
->out_prod
= 0;
566 static int xenfb_connect_backend(struct xenbus_device
*dev
,
567 struct xenfb_info
*info
)
569 int ret
, evtchn
, irq
;
570 struct xenbus_transaction xbt
;
572 ret
= xenbus_alloc_evtchn(dev
, &evtchn
);
575 irq
= bind_evtchn_to_irqhandler(evtchn
, xenfb_event_handler
,
576 0, dev
->devicetype
, info
);
578 xenbus_free_evtchn(dev
, evtchn
);
579 xenbus_dev_fatal(dev
, ret
, "bind_evtchn_to_irqhandler");
583 ret
= xenbus_transaction_start(&xbt
);
585 xenbus_dev_fatal(dev
, ret
, "starting transaction");
588 ret
= xenbus_printf(xbt
, dev
->nodename
, "page-ref", "%lu",
589 virt_to_gfn(info
->page
));
592 ret
= xenbus_printf(xbt
, dev
->nodename
, "event-channel", "%u",
596 ret
= xenbus_printf(xbt
, dev
->nodename
, "protocol", "%s",
597 XEN_IO_PROTO_ABI_NATIVE
);
600 ret
= xenbus_printf(xbt
, dev
->nodename
, "feature-update", "1");
603 ret
= xenbus_transaction_end(xbt
, 0);
607 xenbus_dev_fatal(dev
, ret
, "completing transaction");
611 xenbus_switch_state(dev
, XenbusStateInitialised
);
616 xenbus_transaction_end(xbt
, 1);
617 xenbus_dev_fatal(dev
, ret
, "writing xenstore");
619 unbind_from_irqhandler(irq
, info
);
623 static void xenfb_disconnect_backend(struct xenfb_info
*info
)
625 /* Prevent xenfb refresh */
626 info
->update_wanted
= 0;
628 unbind_from_irqhandler(info
->irq
, info
);
632 static void xenfb_backend_changed(struct xenbus_device
*dev
,
633 enum xenbus_state backend_state
)
635 struct xenfb_info
*info
= dev_get_drvdata(&dev
->dev
);
638 switch (backend_state
) {
639 case XenbusStateInitialising
:
640 case XenbusStateInitialised
:
641 case XenbusStateReconfiguring
:
642 case XenbusStateReconfigured
:
643 case XenbusStateUnknown
:
646 case XenbusStateInitWait
:
648 xenbus_switch_state(dev
, XenbusStateConnected
);
651 case XenbusStateConnected
:
653 * Work around xenbus race condition: If backend goes
654 * through InitWait to Connected fast enough, we can
655 * get Connected twice here.
657 if (dev
->state
!= XenbusStateConnected
)
658 goto InitWait
; /* no InitWait seen yet, fudge it */
660 if (xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
661 "request-update", "%d", &val
) < 0)
664 info
->update_wanted
= 1;
666 if (xenbus_scanf(XBT_NIL
, dev
->otherend
,
667 "feature-resize", "%d", &val
) < 0)
669 info
->feature_resize
= val
;
672 case XenbusStateClosed
:
673 if (dev
->state
== XenbusStateClosed
)
675 /* Missed the backend's CLOSING state -- fallthrough */
676 case XenbusStateClosing
:
677 xenbus_frontend_closed(dev
);
682 static const struct xenbus_device_id xenfb_ids
[] = {
687 static struct xenbus_driver xenfb_driver
= {
689 .probe
= xenfb_probe
,
690 .remove
= xenfb_remove
,
691 .resume
= xenfb_resume
,
692 .otherend_changed
= xenfb_backend_changed
,
695 static int __init
xenfb_init(void)
700 /* Nothing to do if running in dom0. */
701 if (xen_initial_domain())
704 if (!xen_has_pv_devices())
707 return xenbus_register_frontend(&xenfb_driver
);
710 static void __exit
xenfb_cleanup(void)
712 xenbus_unregister_driver(&xenfb_driver
);
715 module_init(xenfb_init
);
716 module_exit(xenfb_cleanup
);
718 MODULE_DESCRIPTION("Xen virtual framebuffer device frontend");
719 MODULE_LICENSE("GPL");
720 MODULE_ALIAS("xen:vfb");