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 */
46 int feature_resize
; /* XENFB_TYPE_RESIZE ok */
47 struct xenfb_resize resize
; /* protected by resize_lock */
48 int resize_dpy
; /* ditto */
49 spinlock_t resize_lock
;
51 struct xenbus_device
*xbdev
;
54 #define XENFB_DEFAULT_FB_LEN (XENFB_WIDTH * XENFB_HEIGHT * XENFB_DEPTH / 8)
56 enum { KPARAM_MEM
, KPARAM_WIDTH
, KPARAM_HEIGHT
, KPARAM_CNT
};
57 static int video
[KPARAM_CNT
] = { 2, XENFB_WIDTH
, XENFB_HEIGHT
};
58 module_param_array(video
, int, NULL
, 0);
59 MODULE_PARM_DESC(video
,
60 "Video memory size in MB, width, height in pixels (default 2,800,600)");
62 static void xenfb_make_preferred_console(void);
63 static int xenfb_remove(struct xenbus_device
*);
64 static void xenfb_init_shared_page(struct xenfb_info
*, struct fb_info
*);
65 static int xenfb_connect_backend(struct xenbus_device
*, struct xenfb_info
*);
66 static void xenfb_disconnect_backend(struct xenfb_info
*);
68 static void xenfb_send_event(struct xenfb_info
*info
,
69 union xenfb_out_event
*event
)
73 prod
= info
->page
->out_prod
;
74 /* caller ensures !xenfb_queue_full() */
75 mb(); /* ensure ring space available */
76 XENFB_OUT_RING_REF(info
->page
, prod
) = *event
;
77 wmb(); /* ensure ring contents visible */
78 info
->page
->out_prod
= prod
+ 1;
80 notify_remote_via_irq(info
->irq
);
83 static void xenfb_do_update(struct xenfb_info
*info
,
84 int x
, int y
, int w
, int h
)
86 union xenfb_out_event event
;
88 memset(&event
, 0, sizeof(event
));
89 event
.type
= XENFB_TYPE_UPDATE
;
92 event
.update
.width
= w
;
93 event
.update
.height
= h
;
95 /* caller ensures !xenfb_queue_full() */
96 xenfb_send_event(info
, &event
);
99 static void xenfb_do_resize(struct xenfb_info
*info
)
101 union xenfb_out_event event
;
103 memset(&event
, 0, sizeof(event
));
104 event
.resize
= info
->resize
;
106 /* caller ensures !xenfb_queue_full() */
107 xenfb_send_event(info
, &event
);
110 static int xenfb_queue_full(struct xenfb_info
*info
)
114 prod
= info
->page
->out_prod
;
115 cons
= info
->page
->out_cons
;
116 return prod
- cons
== XENFB_OUT_RING_LEN
;
119 static void xenfb_handle_resize_dpy(struct xenfb_info
*info
)
123 spin_lock_irqsave(&info
->resize_lock
, flags
);
124 if (info
->resize_dpy
) {
125 if (!xenfb_queue_full(info
)) {
126 info
->resize_dpy
= 0;
127 xenfb_do_resize(info
);
130 spin_unlock_irqrestore(&info
->resize_lock
, flags
);
133 static void xenfb_refresh(struct xenfb_info
*info
,
134 int x1
, int y1
, int w
, int h
)
140 xenfb_handle_resize_dpy(info
);
142 if (!info
->update_wanted
)
145 spin_lock_irqsave(&info
->dirty_lock
, flags
);
147 /* Combine with dirty rectangle: */
157 if (xenfb_queue_full(info
)) {
158 /* Can't send right now, stash it in the dirty rectangle */
163 spin_unlock_irqrestore(&info
->dirty_lock
, flags
);
167 /* Clear dirty rectangle: */
168 info
->x1
= info
->y1
= INT_MAX
;
169 info
->x2
= info
->y2
= 0;
171 spin_unlock_irqrestore(&info
->dirty_lock
, flags
);
173 if (x1
<= x2
&& y1
<= y2
)
174 xenfb_do_update(info
, x1
, y1
, x2
- x1
+ 1, y2
- y1
+ 1);
177 static void xenfb_deferred_io(struct fb_info
*fb_info
,
178 struct list_head
*pagelist
)
180 struct xenfb_info
*info
= fb_info
->par
;
182 unsigned long beg
, end
;
183 int y1
, y2
, miny
, maxy
;
187 list_for_each_entry(page
, pagelist
, lru
) {
188 beg
= page
->index
<< PAGE_SHIFT
;
189 end
= beg
+ PAGE_SIZE
- 1;
190 y1
= beg
/ fb_info
->fix
.line_length
;
191 y2
= end
/ fb_info
->fix
.line_length
;
192 if (y2
>= fb_info
->var
.yres
)
193 y2
= fb_info
->var
.yres
- 1;
199 xenfb_refresh(info
, 0, miny
, fb_info
->var
.xres
, maxy
- miny
+ 1);
202 static struct fb_deferred_io xenfb_defio
= {
204 .deferred_io
= xenfb_deferred_io
,
207 static int xenfb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
208 unsigned blue
, unsigned transp
,
209 struct fb_info
*info
)
213 if (regno
> info
->cmap
.len
)
216 #define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
217 red
= CNVT_TOHW(red
, info
->var
.red
.length
);
218 green
= CNVT_TOHW(green
, info
->var
.green
.length
);
219 blue
= CNVT_TOHW(blue
, info
->var
.blue
.length
);
220 transp
= CNVT_TOHW(transp
, info
->var
.transp
.length
);
223 v
= (red
<< info
->var
.red
.offset
) |
224 (green
<< info
->var
.green
.offset
) |
225 (blue
<< info
->var
.blue
.offset
);
227 switch (info
->var
.bits_per_pixel
) {
231 ((u32
*)info
->pseudo_palette
)[regno
] = v
;
238 static void xenfb_fillrect(struct fb_info
*p
, const struct fb_fillrect
*rect
)
240 struct xenfb_info
*info
= p
->par
;
242 sys_fillrect(p
, rect
);
243 xenfb_refresh(info
, rect
->dx
, rect
->dy
, rect
->width
, rect
->height
);
246 static void xenfb_imageblit(struct fb_info
*p
, const struct fb_image
*image
)
248 struct xenfb_info
*info
= p
->par
;
250 sys_imageblit(p
, image
);
251 xenfb_refresh(info
, image
->dx
, image
->dy
, image
->width
, image
->height
);
254 static void xenfb_copyarea(struct fb_info
*p
, const struct fb_copyarea
*area
)
256 struct xenfb_info
*info
= p
->par
;
258 sys_copyarea(p
, area
);
259 xenfb_refresh(info
, area
->dx
, area
->dy
, area
->width
, area
->height
);
262 static ssize_t
xenfb_write(struct fb_info
*p
, const char __user
*buf
,
263 size_t count
, loff_t
*ppos
)
265 struct xenfb_info
*info
= p
->par
;
268 res
= fb_sys_write(p
, buf
, count
, ppos
);
269 xenfb_refresh(info
, 0, 0, info
->page
->width
, info
->page
->height
);
274 xenfb_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
276 struct xenfb_info
*xenfb_info
;
277 int required_mem_len
;
279 xenfb_info
= info
->par
;
281 if (!xenfb_info
->feature_resize
) {
282 if (var
->xres
== video
[KPARAM_WIDTH
] &&
283 var
->yres
== video
[KPARAM_HEIGHT
] &&
284 var
->bits_per_pixel
== xenfb_info
->page
->depth
) {
290 /* Can't resize past initial width and height */
291 if (var
->xres
> video
[KPARAM_WIDTH
] || var
->yres
> video
[KPARAM_HEIGHT
])
294 required_mem_len
= var
->xres
* var
->yres
* xenfb_info
->page
->depth
/ 8;
295 if (var
->bits_per_pixel
== xenfb_info
->page
->depth
&&
296 var
->xres
<= info
->fix
.line_length
/ (XENFB_DEPTH
/ 8) &&
297 required_mem_len
<= info
->fix
.smem_len
) {
298 var
->xres_virtual
= var
->xres
;
299 var
->yres_virtual
= var
->yres
;
305 static int xenfb_set_par(struct fb_info
*info
)
307 struct xenfb_info
*xenfb_info
;
310 xenfb_info
= info
->par
;
312 spin_lock_irqsave(&xenfb_info
->resize_lock
, flags
);
313 xenfb_info
->resize
.type
= XENFB_TYPE_RESIZE
;
314 xenfb_info
->resize
.width
= info
->var
.xres
;
315 xenfb_info
->resize
.height
= info
->var
.yres
;
316 xenfb_info
->resize
.stride
= info
->fix
.line_length
;
317 xenfb_info
->resize
.depth
= info
->var
.bits_per_pixel
;
318 xenfb_info
->resize
.offset
= 0;
319 xenfb_info
->resize_dpy
= 1;
320 spin_unlock_irqrestore(&xenfb_info
->resize_lock
, flags
);
324 static struct fb_ops xenfb_fb_ops
= {
325 .owner
= THIS_MODULE
,
326 .fb_read
= fb_sys_read
,
327 .fb_write
= xenfb_write
,
328 .fb_setcolreg
= xenfb_setcolreg
,
329 .fb_fillrect
= xenfb_fillrect
,
330 .fb_copyarea
= xenfb_copyarea
,
331 .fb_imageblit
= xenfb_imageblit
,
332 .fb_check_var
= xenfb_check_var
,
333 .fb_set_par
= xenfb_set_par
,
336 static irqreturn_t
xenfb_event_handler(int rq
, void *dev_id
)
339 * No in events recognized, simply ignore them all.
340 * If you need to recognize some, see xen-kbdfront's
341 * input_handler() for how to do that.
343 struct xenfb_info
*info
= dev_id
;
344 struct xenfb_page
*page
= info
->page
;
346 if (page
->in_cons
!= page
->in_prod
) {
347 info
->page
->in_cons
= info
->page
->in_prod
;
348 notify_remote_via_irq(info
->irq
);
351 /* Flush dirty rectangle: */
352 xenfb_refresh(info
, INT_MAX
, INT_MAX
, -INT_MAX
, -INT_MAX
);
357 static int __devinit
xenfb_probe(struct xenbus_device
*dev
,
358 const struct xenbus_device_id
*id
)
360 struct xenfb_info
*info
;
361 struct fb_info
*fb_info
;
366 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
368 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
372 /* Limit kernel param videoram amount to what is in xenstore */
373 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "videoram", "%d", &val
) == 1) {
374 if (val
< video
[KPARAM_MEM
])
375 video
[KPARAM_MEM
] = val
;
378 /* If requested res does not fit in available memory, use default */
379 fb_size
= video
[KPARAM_MEM
] * 1024 * 1024;
380 if (video
[KPARAM_WIDTH
] * video
[KPARAM_HEIGHT
] * XENFB_DEPTH
/ 8
382 video
[KPARAM_WIDTH
] = XENFB_WIDTH
;
383 video
[KPARAM_HEIGHT
] = XENFB_HEIGHT
;
384 fb_size
= XENFB_DEFAULT_FB_LEN
;
387 dev_set_drvdata(&dev
->dev
, info
);
390 info
->x1
= info
->y1
= INT_MAX
;
391 spin_lock_init(&info
->dirty_lock
);
392 spin_lock_init(&info
->resize_lock
);
394 info
->fb
= vmalloc(fb_size
);
395 if (info
->fb
== NULL
)
397 memset(info
->fb
, 0, fb_size
);
399 info
->nr_pages
= (fb_size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
401 info
->mfns
= vmalloc(sizeof(unsigned long) * info
->nr_pages
);
405 /* set up shared page */
406 info
->page
= (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
410 /* abusing framebuffer_alloc() to allocate pseudo_palette */
411 fb_info
= framebuffer_alloc(sizeof(u32
) * 256, NULL
);
415 /* complete the abuse: */
416 fb_info
->pseudo_palette
= fb_info
->par
;
419 fb_info
->screen_base
= info
->fb
;
421 fb_info
->fbops
= &xenfb_fb_ops
;
422 fb_info
->var
.xres_virtual
= fb_info
->var
.xres
= video
[KPARAM_WIDTH
];
423 fb_info
->var
.yres_virtual
= fb_info
->var
.yres
= video
[KPARAM_HEIGHT
];
424 fb_info
->var
.bits_per_pixel
= XENFB_DEPTH
;
426 fb_info
->var
.red
= (struct fb_bitfield
){16, 8, 0};
427 fb_info
->var
.green
= (struct fb_bitfield
){8, 8, 0};
428 fb_info
->var
.blue
= (struct fb_bitfield
){0, 8, 0};
430 fb_info
->var
.activate
= FB_ACTIVATE_NOW
;
431 fb_info
->var
.height
= -1;
432 fb_info
->var
.width
= -1;
433 fb_info
->var
.vmode
= FB_VMODE_NONINTERLACED
;
435 fb_info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
436 fb_info
->fix
.line_length
= fb_info
->var
.xres
* XENFB_DEPTH
/ 8;
437 fb_info
->fix
.smem_start
= 0;
438 fb_info
->fix
.smem_len
= fb_size
;
439 strcpy(fb_info
->fix
.id
, "xen");
440 fb_info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
441 fb_info
->fix
.accel
= FB_ACCEL_NONE
;
443 fb_info
->flags
= FBINFO_FLAG_DEFAULT
| FBINFO_VIRTFB
;
445 ret
= fb_alloc_cmap(&fb_info
->cmap
, 256, 0);
447 framebuffer_release(fb_info
);
448 xenbus_dev_fatal(dev
, ret
, "fb_alloc_cmap");
452 fb_info
->fbdefio
= &xenfb_defio
;
453 fb_deferred_io_init(fb_info
);
455 xenfb_init_shared_page(info
, fb_info
);
457 ret
= xenfb_connect_backend(dev
, info
);
461 ret
= register_framebuffer(fb_info
);
463 fb_deferred_io_cleanup(fb_info
);
464 fb_dealloc_cmap(&fb_info
->cmap
);
465 framebuffer_release(fb_info
);
466 xenbus_dev_fatal(dev
, ret
, "register_framebuffer");
469 info
->fb_info
= fb_info
;
471 xenfb_make_preferred_console();
476 xenbus_dev_fatal(dev
, ret
, "allocating device memory");
482 static __devinit
void
483 xenfb_make_preferred_console(void)
487 if (console_set_on_cmdline
)
490 acquire_console_sem();
491 for (c
= console_drivers
; c
; c
= c
->next
) {
492 if (!strcmp(c
->name
, "tty") && c
->index
== 0)
495 release_console_sem();
497 unregister_console(c
);
498 c
->flags
|= CON_CONSDEV
;
499 c
->flags
&= ~CON_PRINTBUFFER
; /* don't print again */
504 static int xenfb_resume(struct xenbus_device
*dev
)
506 struct xenfb_info
*info
= dev_get_drvdata(&dev
->dev
);
508 xenfb_disconnect_backend(info
);
509 xenfb_init_shared_page(info
, info
->fb_info
);
510 return xenfb_connect_backend(dev
, info
);
513 static int xenfb_remove(struct xenbus_device
*dev
)
515 struct xenfb_info
*info
= dev_get_drvdata(&dev
->dev
);
517 xenfb_disconnect_backend(info
);
519 fb_deferred_io_cleanup(info
->fb_info
);
520 unregister_framebuffer(info
->fb_info
);
521 fb_dealloc_cmap(&info
->fb_info
->cmap
);
522 framebuffer_release(info
->fb_info
);
524 free_page((unsigned long)info
->page
);
532 static unsigned long vmalloc_to_mfn(void *address
)
534 return pfn_to_mfn(vmalloc_to_pfn(address
));
537 static void xenfb_init_shared_page(struct xenfb_info
*info
,
538 struct fb_info
*fb_info
)
541 int epd
= PAGE_SIZE
/ sizeof(info
->mfns
[0]);
543 for (i
= 0; i
< info
->nr_pages
; i
++)
544 info
->mfns
[i
] = vmalloc_to_mfn(info
->fb
+ i
* PAGE_SIZE
);
546 for (i
= 0; i
* epd
< info
->nr_pages
; i
++)
547 info
->page
->pd
[i
] = vmalloc_to_mfn(&info
->mfns
[i
* epd
]);
549 info
->page
->width
= fb_info
->var
.xres
;
550 info
->page
->height
= fb_info
->var
.yres
;
551 info
->page
->depth
= fb_info
->var
.bits_per_pixel
;
552 info
->page
->line_length
= fb_info
->fix
.line_length
;
553 info
->page
->mem_length
= fb_info
->fix
.smem_len
;
554 info
->page
->in_cons
= info
->page
->in_prod
= 0;
555 info
->page
->out_cons
= info
->page
->out_prod
= 0;
558 static int xenfb_connect_backend(struct xenbus_device
*dev
,
559 struct xenfb_info
*info
)
562 struct xenbus_transaction xbt
;
564 ret
= xenbus_alloc_evtchn(dev
, &evtchn
);
567 ret
= bind_evtchn_to_irqhandler(evtchn
, xenfb_event_handler
,
568 0, dev
->devicetype
, info
);
570 xenbus_free_evtchn(dev
, evtchn
);
571 xenbus_dev_fatal(dev
, ret
, "bind_evtchn_to_irqhandler");
577 ret
= xenbus_transaction_start(&xbt
);
579 xenbus_dev_fatal(dev
, ret
, "starting transaction");
582 ret
= xenbus_printf(xbt
, dev
->nodename
, "page-ref", "%lu",
583 virt_to_mfn(info
->page
));
586 ret
= xenbus_printf(xbt
, dev
->nodename
, "event-channel", "%u",
590 ret
= xenbus_printf(xbt
, dev
->nodename
, "protocol", "%s",
591 XEN_IO_PROTO_ABI_NATIVE
);
594 ret
= xenbus_printf(xbt
, dev
->nodename
, "feature-update", "1");
597 ret
= xenbus_transaction_end(xbt
, 0);
601 xenbus_dev_fatal(dev
, ret
, "completing transaction");
605 xenbus_switch_state(dev
, XenbusStateInitialised
);
609 xenbus_transaction_end(xbt
, 1);
610 xenbus_dev_fatal(dev
, ret
, "writing xenstore");
614 static void xenfb_disconnect_backend(struct xenfb_info
*info
)
617 unbind_from_irqhandler(info
->irq
, info
);
621 static void xenfb_backend_changed(struct xenbus_device
*dev
,
622 enum xenbus_state backend_state
)
624 struct xenfb_info
*info
= dev_get_drvdata(&dev
->dev
);
627 switch (backend_state
) {
628 case XenbusStateInitialising
:
629 case XenbusStateInitialised
:
630 case XenbusStateUnknown
:
631 case XenbusStateClosed
:
634 case XenbusStateInitWait
:
636 xenbus_switch_state(dev
, XenbusStateConnected
);
639 case XenbusStateConnected
:
641 * Work around xenbus race condition: If backend goes
642 * through InitWait to Connected fast enough, we can
643 * get Connected twice here.
645 if (dev
->state
!= XenbusStateConnected
)
646 goto InitWait
; /* no InitWait seen yet, fudge it */
648 if (xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
649 "request-update", "%d", &val
) < 0)
652 info
->update_wanted
= 1;
654 if (xenbus_scanf(XBT_NIL
, dev
->otherend
,
655 "feature-resize", "%d", &val
) < 0)
657 info
->feature_resize
= val
;
660 case XenbusStateClosing
:
661 xenbus_frontend_closed(dev
);
666 static struct xenbus_device_id xenfb_ids
[] = {
671 static struct xenbus_driver xenfb_driver
= {
673 .owner
= THIS_MODULE
,
675 .probe
= xenfb_probe
,
676 .remove
= xenfb_remove
,
677 .resume
= xenfb_resume
,
678 .otherend_changed
= xenfb_backend_changed
,
681 static int __init
xenfb_init(void)
686 /* Nothing to do if running in dom0. */
687 if (xen_initial_domain())
690 return xenbus_register_frontend(&xenfb_driver
);
693 static void __exit
xenfb_cleanup(void)
695 xenbus_unregister_driver(&xenfb_driver
);
698 module_init(xenfb_init
);
699 module_exit(xenfb_cleanup
);
701 MODULE_DESCRIPTION("Xen virtual framebuffer device frontend");
702 MODULE_LICENSE("GPL");
703 MODULE_ALIAS("xen:vfb");