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>
41 struct fb_info
*fb_info
;
42 int x1
, y1
, x2
, y2
; /* dirty rectangle,
43 protected by dirty_lock */
44 spinlock_t dirty_lock
;
47 struct xenfb_page
*page
;
49 int update_wanted
; /* XENFB_TYPE_UPDATE wanted */
50 int feature_resize
; /* XENFB_TYPE_RESIZE ok */
51 struct xenfb_resize resize
; /* protected by resize_lock */
52 int resize_dpy
; /* ditto */
53 spinlock_t resize_lock
;
55 struct xenbus_device
*xbdev
;
58 #define XENFB_DEFAULT_FB_LEN (XENFB_WIDTH * XENFB_HEIGHT * XENFB_DEPTH / 8)
60 enum { KPARAM_MEM
, KPARAM_WIDTH
, KPARAM_HEIGHT
, KPARAM_CNT
};
61 static int video
[KPARAM_CNT
] = { 2, XENFB_WIDTH
, XENFB_HEIGHT
};
62 module_param_array(video
, int, NULL
, 0);
63 MODULE_PARM_DESC(video
,
64 "Video memory size in MB, width, height in pixels (default 2,800,600)");
66 static void xenfb_make_preferred_console(void);
67 static int xenfb_remove(struct xenbus_device
*);
68 static void xenfb_init_shared_page(struct xenfb_info
*, struct fb_info
*);
69 static int xenfb_connect_backend(struct xenbus_device
*, struct xenfb_info
*);
70 static void xenfb_disconnect_backend(struct xenfb_info
*);
72 static void xenfb_send_event(struct xenfb_info
*info
,
73 union xenfb_out_event
*event
)
77 prod
= info
->page
->out_prod
;
78 /* caller ensures !xenfb_queue_full() */
79 mb(); /* ensure ring space available */
80 XENFB_OUT_RING_REF(info
->page
, prod
) = *event
;
81 wmb(); /* ensure ring contents visible */
82 info
->page
->out_prod
= prod
+ 1;
84 notify_remote_via_irq(info
->irq
);
87 static void xenfb_do_update(struct xenfb_info
*info
,
88 int x
, int y
, int w
, int h
)
90 union xenfb_out_event event
;
92 memset(&event
, 0, sizeof(event
));
93 event
.type
= XENFB_TYPE_UPDATE
;
96 event
.update
.width
= w
;
97 event
.update
.height
= h
;
99 /* caller ensures !xenfb_queue_full() */
100 xenfb_send_event(info
, &event
);
103 static void xenfb_do_resize(struct xenfb_info
*info
)
105 union xenfb_out_event event
;
107 memset(&event
, 0, sizeof(event
));
108 event
.resize
= info
->resize
;
110 /* caller ensures !xenfb_queue_full() */
111 xenfb_send_event(info
, &event
);
114 static int xenfb_queue_full(struct xenfb_info
*info
)
118 prod
= info
->page
->out_prod
;
119 cons
= info
->page
->out_cons
;
120 return prod
- cons
== XENFB_OUT_RING_LEN
;
123 static void xenfb_handle_resize_dpy(struct xenfb_info
*info
)
127 spin_lock_irqsave(&info
->resize_lock
, flags
);
128 if (info
->resize_dpy
) {
129 if (!xenfb_queue_full(info
)) {
130 info
->resize_dpy
= 0;
131 xenfb_do_resize(info
);
134 spin_unlock_irqrestore(&info
->resize_lock
, flags
);
137 static void xenfb_refresh(struct xenfb_info
*info
,
138 int x1
, int y1
, int w
, int h
)
144 xenfb_handle_resize_dpy(info
);
146 if (!info
->update_wanted
)
149 spin_lock_irqsave(&info
->dirty_lock
, flags
);
151 /* Combine with dirty rectangle: */
161 if (xenfb_queue_full(info
)) {
162 /* Can't send right now, stash it in the dirty rectangle */
167 spin_unlock_irqrestore(&info
->dirty_lock
, flags
);
171 /* Clear dirty rectangle: */
172 info
->x1
= info
->y1
= INT_MAX
;
173 info
->x2
= info
->y2
= 0;
175 spin_unlock_irqrestore(&info
->dirty_lock
, flags
);
177 if (x1
<= x2
&& y1
<= y2
)
178 xenfb_do_update(info
, x1
, y1
, x2
- x1
+ 1, y2
- y1
+ 1);
181 static void xenfb_deferred_io(struct fb_info
*fb_info
,
182 struct list_head
*pagelist
)
184 struct xenfb_info
*info
= fb_info
->par
;
186 unsigned long beg
, end
;
187 int y1
, y2
, miny
, maxy
;
191 list_for_each_entry(page
, pagelist
, lru
) {
192 beg
= page
->index
<< PAGE_SHIFT
;
193 end
= beg
+ PAGE_SIZE
- 1;
194 y1
= beg
/ fb_info
->fix
.line_length
;
195 y2
= end
/ fb_info
->fix
.line_length
;
196 if (y2
>= fb_info
->var
.yres
)
197 y2
= fb_info
->var
.yres
- 1;
203 xenfb_refresh(info
, 0, miny
, fb_info
->var
.xres
, maxy
- miny
+ 1);
206 static struct fb_deferred_io xenfb_defio
= {
208 .deferred_io
= xenfb_deferred_io
,
211 static int xenfb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
212 unsigned blue
, unsigned transp
,
213 struct fb_info
*info
)
217 if (regno
> info
->cmap
.len
)
220 #define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
221 red
= CNVT_TOHW(red
, info
->var
.red
.length
);
222 green
= CNVT_TOHW(green
, info
->var
.green
.length
);
223 blue
= CNVT_TOHW(blue
, info
->var
.blue
.length
);
224 transp
= CNVT_TOHW(transp
, info
->var
.transp
.length
);
227 v
= (red
<< info
->var
.red
.offset
) |
228 (green
<< info
->var
.green
.offset
) |
229 (blue
<< info
->var
.blue
.offset
);
231 switch (info
->var
.bits_per_pixel
) {
235 ((u32
*)info
->pseudo_palette
)[regno
] = v
;
242 static void xenfb_fillrect(struct fb_info
*p
, const struct fb_fillrect
*rect
)
244 struct xenfb_info
*info
= p
->par
;
246 sys_fillrect(p
, rect
);
247 xenfb_refresh(info
, rect
->dx
, rect
->dy
, rect
->width
, rect
->height
);
250 static void xenfb_imageblit(struct fb_info
*p
, const struct fb_image
*image
)
252 struct xenfb_info
*info
= p
->par
;
254 sys_imageblit(p
, image
);
255 xenfb_refresh(info
, image
->dx
, image
->dy
, image
->width
, image
->height
);
258 static void xenfb_copyarea(struct fb_info
*p
, const struct fb_copyarea
*area
)
260 struct xenfb_info
*info
= p
->par
;
262 sys_copyarea(p
, area
);
263 xenfb_refresh(info
, area
->dx
, area
->dy
, area
->width
, area
->height
);
266 static ssize_t
xenfb_write(struct fb_info
*p
, const char __user
*buf
,
267 size_t count
, loff_t
*ppos
)
269 struct xenfb_info
*info
= p
->par
;
272 res
= fb_sys_write(p
, buf
, count
, ppos
);
273 xenfb_refresh(info
, 0, 0, info
->page
->width
, info
->page
->height
);
278 xenfb_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
280 struct xenfb_info
*xenfb_info
;
281 int required_mem_len
;
283 xenfb_info
= info
->par
;
285 if (!xenfb_info
->feature_resize
) {
286 if (var
->xres
== video
[KPARAM_WIDTH
] &&
287 var
->yres
== video
[KPARAM_HEIGHT
] &&
288 var
->bits_per_pixel
== xenfb_info
->page
->depth
) {
294 /* Can't resize past initial width and height */
295 if (var
->xres
> video
[KPARAM_WIDTH
] || var
->yres
> video
[KPARAM_HEIGHT
])
298 required_mem_len
= var
->xres
* var
->yres
* xenfb_info
->page
->depth
/ 8;
299 if (var
->bits_per_pixel
== xenfb_info
->page
->depth
&&
300 var
->xres
<= info
->fix
.line_length
/ (XENFB_DEPTH
/ 8) &&
301 required_mem_len
<= info
->fix
.smem_len
) {
302 var
->xres_virtual
= var
->xres
;
303 var
->yres_virtual
= var
->yres
;
309 static int xenfb_set_par(struct fb_info
*info
)
311 struct xenfb_info
*xenfb_info
;
314 xenfb_info
= info
->par
;
316 spin_lock_irqsave(&xenfb_info
->resize_lock
, flags
);
317 xenfb_info
->resize
.type
= XENFB_TYPE_RESIZE
;
318 xenfb_info
->resize
.width
= info
->var
.xres
;
319 xenfb_info
->resize
.height
= info
->var
.yres
;
320 xenfb_info
->resize
.stride
= info
->fix
.line_length
;
321 xenfb_info
->resize
.depth
= info
->var
.bits_per_pixel
;
322 xenfb_info
->resize
.offset
= 0;
323 xenfb_info
->resize_dpy
= 1;
324 spin_unlock_irqrestore(&xenfb_info
->resize_lock
, flags
);
328 static struct fb_ops xenfb_fb_ops
= {
329 .owner
= THIS_MODULE
,
330 .fb_read
= fb_sys_read
,
331 .fb_write
= xenfb_write
,
332 .fb_setcolreg
= xenfb_setcolreg
,
333 .fb_fillrect
= xenfb_fillrect
,
334 .fb_copyarea
= xenfb_copyarea
,
335 .fb_imageblit
= xenfb_imageblit
,
336 .fb_check_var
= xenfb_check_var
,
337 .fb_set_par
= xenfb_set_par
,
340 static irqreturn_t
xenfb_event_handler(int rq
, void *dev_id
)
343 * No in events recognized, simply ignore them all.
344 * If you need to recognize some, see xen-kbdfront's
345 * input_handler() for how to do that.
347 struct xenfb_info
*info
= dev_id
;
348 struct xenfb_page
*page
= info
->page
;
350 if (page
->in_cons
!= page
->in_prod
) {
351 info
->page
->in_cons
= info
->page
->in_prod
;
352 notify_remote_via_irq(info
->irq
);
355 /* Flush dirty rectangle: */
356 xenfb_refresh(info
, INT_MAX
, INT_MAX
, -INT_MAX
, -INT_MAX
);
361 static int __devinit
xenfb_probe(struct xenbus_device
*dev
,
362 const struct xenbus_device_id
*id
)
364 struct xenfb_info
*info
;
365 struct fb_info
*fb_info
;
370 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
372 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
376 /* Limit kernel param videoram amount to what is in xenstore */
377 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "videoram", "%d", &val
) == 1) {
378 if (val
< video
[KPARAM_MEM
])
379 video
[KPARAM_MEM
] = val
;
382 /* If requested res does not fit in available memory, use default */
383 fb_size
= video
[KPARAM_MEM
] * 1024 * 1024;
384 if (video
[KPARAM_WIDTH
] * video
[KPARAM_HEIGHT
] * XENFB_DEPTH
/ 8
386 video
[KPARAM_WIDTH
] = XENFB_WIDTH
;
387 video
[KPARAM_HEIGHT
] = XENFB_HEIGHT
;
388 fb_size
= XENFB_DEFAULT_FB_LEN
;
391 dev_set_drvdata(&dev
->dev
, info
);
394 info
->x1
= info
->y1
= INT_MAX
;
395 spin_lock_init(&info
->dirty_lock
);
396 spin_lock_init(&info
->resize_lock
);
398 info
->fb
= vmalloc(fb_size
);
399 if (info
->fb
== NULL
)
401 memset(info
->fb
, 0, fb_size
);
403 info
->nr_pages
= (fb_size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
405 info
->mfns
= 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
);
465 ret
= register_framebuffer(fb_info
);
467 fb_deferred_io_cleanup(fb_info
);
468 fb_dealloc_cmap(&fb_info
->cmap
);
469 framebuffer_release(fb_info
);
470 xenbus_dev_fatal(dev
, ret
, "register_framebuffer");
473 info
->fb_info
= fb_info
;
475 xenfb_make_preferred_console();
480 xenbus_dev_fatal(dev
, ret
, "allocating device memory");
486 static __devinit
void
487 xenfb_make_preferred_console(void)
491 if (console_set_on_cmdline
)
495 for_each_console(c
) {
496 if (!strcmp(c
->name
, "tty") && c
->index
== 0)
501 unregister_console(c
);
502 c
->flags
|= CON_CONSDEV
;
503 c
->flags
&= ~CON_PRINTBUFFER
; /* don't print again */
508 static int xenfb_resume(struct xenbus_device
*dev
)
510 struct xenfb_info
*info
= dev_get_drvdata(&dev
->dev
);
512 xenfb_disconnect_backend(info
);
513 xenfb_init_shared_page(info
, info
->fb_info
);
514 return xenfb_connect_backend(dev
, info
);
517 static int xenfb_remove(struct xenbus_device
*dev
)
519 struct xenfb_info
*info
= dev_get_drvdata(&dev
->dev
);
521 xenfb_disconnect_backend(info
);
523 fb_deferred_io_cleanup(info
->fb_info
);
524 unregister_framebuffer(info
->fb_info
);
525 fb_dealloc_cmap(&info
->fb_info
->cmap
);
526 framebuffer_release(info
->fb_info
);
528 free_page((unsigned long)info
->page
);
536 static unsigned long vmalloc_to_mfn(void *address
)
538 return pfn_to_mfn(vmalloc_to_pfn(address
));
541 static void xenfb_init_shared_page(struct xenfb_info
*info
,
542 struct fb_info
*fb_info
)
545 int epd
= PAGE_SIZE
/ sizeof(info
->mfns
[0]);
547 for (i
= 0; i
< info
->nr_pages
; i
++)
548 info
->mfns
[i
] = vmalloc_to_mfn(info
->fb
+ i
* PAGE_SIZE
);
550 for (i
= 0; i
* epd
< info
->nr_pages
; i
++)
551 info
->page
->pd
[i
] = vmalloc_to_mfn(&info
->mfns
[i
* epd
]);
553 info
->page
->width
= fb_info
->var
.xres
;
554 info
->page
->height
= fb_info
->var
.yres
;
555 info
->page
->depth
= fb_info
->var
.bits_per_pixel
;
556 info
->page
->line_length
= fb_info
->fix
.line_length
;
557 info
->page
->mem_length
= fb_info
->fix
.smem_len
;
558 info
->page
->in_cons
= info
->page
->in_prod
= 0;
559 info
->page
->out_cons
= info
->page
->out_prod
= 0;
562 static int xenfb_connect_backend(struct xenbus_device
*dev
,
563 struct xenfb_info
*info
)
565 int ret
, evtchn
, irq
;
566 struct xenbus_transaction xbt
;
568 ret
= xenbus_alloc_evtchn(dev
, &evtchn
);
571 irq
= bind_evtchn_to_irqhandler(evtchn
, xenfb_event_handler
,
572 0, dev
->devicetype
, info
);
574 xenbus_free_evtchn(dev
, evtchn
);
575 xenbus_dev_fatal(dev
, ret
, "bind_evtchn_to_irqhandler");
579 ret
= xenbus_transaction_start(&xbt
);
581 xenbus_dev_fatal(dev
, ret
, "starting transaction");
584 ret
= xenbus_printf(xbt
, dev
->nodename
, "page-ref", "%lu",
585 virt_to_mfn(info
->page
));
588 ret
= xenbus_printf(xbt
, dev
->nodename
, "event-channel", "%u",
592 ret
= xenbus_printf(xbt
, dev
->nodename
, "protocol", "%s",
593 XEN_IO_PROTO_ABI_NATIVE
);
596 ret
= xenbus_printf(xbt
, dev
->nodename
, "feature-update", "1");
599 ret
= xenbus_transaction_end(xbt
, 0);
603 xenbus_dev_fatal(dev
, ret
, "completing transaction");
607 xenbus_switch_state(dev
, XenbusStateInitialised
);
612 xenbus_transaction_end(xbt
, 1);
613 xenbus_dev_fatal(dev
, ret
, "writing xenstore");
615 unbind_from_irqhandler(irq
, info
);
619 static void xenfb_disconnect_backend(struct xenfb_info
*info
)
621 /* Prevent xenfb refresh */
622 info
->update_wanted
= 0;
624 unbind_from_irqhandler(info
->irq
, info
);
628 static void xenfb_backend_changed(struct xenbus_device
*dev
,
629 enum xenbus_state backend_state
)
631 struct xenfb_info
*info
= dev_get_drvdata(&dev
->dev
);
634 switch (backend_state
) {
635 case XenbusStateInitialising
:
636 case XenbusStateInitialised
:
637 case XenbusStateReconfiguring
:
638 case XenbusStateReconfigured
:
639 case XenbusStateUnknown
:
640 case XenbusStateClosed
:
643 case XenbusStateInitWait
:
645 xenbus_switch_state(dev
, XenbusStateConnected
);
648 case XenbusStateConnected
:
650 * Work around xenbus race condition: If backend goes
651 * through InitWait to Connected fast enough, we can
652 * get Connected twice here.
654 if (dev
->state
!= XenbusStateConnected
)
655 goto InitWait
; /* no InitWait seen yet, fudge it */
657 if (xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
658 "request-update", "%d", &val
) < 0)
661 info
->update_wanted
= 1;
663 if (xenbus_scanf(XBT_NIL
, dev
->otherend
,
664 "feature-resize", "%d", &val
) < 0)
666 info
->feature_resize
= val
;
669 case XenbusStateClosing
:
670 xenbus_frontend_closed(dev
);
675 static struct xenbus_device_id xenfb_ids
[] = {
680 static struct xenbus_driver xenfb_driver
= {
682 .owner
= THIS_MODULE
,
684 .probe
= xenfb_probe
,
685 .remove
= xenfb_remove
,
686 .resume
= xenfb_resume
,
687 .otherend_changed
= xenfb_backend_changed
,
690 static int __init
xenfb_init(void)
692 if (!xen_pv_domain())
695 /* Nothing to do if running in dom0. */
696 if (xen_initial_domain())
699 return xenbus_register_frontend(&xenfb_driver
);
702 static void __exit
xenfb_cleanup(void)
704 xenbus_unregister_driver(&xenfb_driver
);
707 module_init(xenfb_init
);
708 module_exit(xenfb_cleanup
);
710 MODULE_DESCRIPTION("Xen virtual framebuffer device frontend");
711 MODULE_LICENSE("GPL");
712 MODULE_ALIAS("xen:vfb");