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/kernel.h>
22 #include <linux/errno.h>
24 #include <linux/module.h>
25 #include <linux/vmalloc.h>
27 #include <asm/xen/hypervisor.h>
28 #include <xen/events.h>
30 #include <xen/interface/io/fbif.h>
31 #include <xen/interface/io/protocols.h>
32 #include <xen/xenbus.h>
36 struct fb_info
*fb_info
;
37 int x1
, y1
, x2
, y2
; /* dirty rectangle,
38 protected by dirty_lock */
39 spinlock_t dirty_lock
;
42 struct xenfb_page
*page
;
44 int update_wanted
; /* XENFB_TYPE_UPDATE wanted */
46 struct xenbus_device
*xbdev
;
49 static u32 xenfb_mem_len
= XENFB_WIDTH
* XENFB_HEIGHT
* XENFB_DEPTH
/ 8;
51 static int xenfb_remove(struct xenbus_device
*);
52 static void xenfb_init_shared_page(struct xenfb_info
*);
53 static int xenfb_connect_backend(struct xenbus_device
*, struct xenfb_info
*);
54 static void xenfb_disconnect_backend(struct xenfb_info
*);
56 static void xenfb_do_update(struct xenfb_info
*info
,
57 int x
, int y
, int w
, int h
)
59 union xenfb_out_event event
;
62 event
.type
= XENFB_TYPE_UPDATE
;
65 event
.update
.width
= w
;
66 event
.update
.height
= h
;
68 prod
= info
->page
->out_prod
;
69 /* caller ensures !xenfb_queue_full() */
70 mb(); /* ensure ring space available */
71 XENFB_OUT_RING_REF(info
->page
, prod
) = event
;
72 wmb(); /* ensure ring contents visible */
73 info
->page
->out_prod
= prod
+ 1;
75 notify_remote_via_irq(info
->irq
);
78 static int xenfb_queue_full(struct xenfb_info
*info
)
82 prod
= info
->page
->out_prod
;
83 cons
= info
->page
->out_cons
;
84 return prod
- cons
== XENFB_OUT_RING_LEN
;
87 static void xenfb_refresh(struct xenfb_info
*info
,
88 int x1
, int y1
, int w
, int h
)
94 if (!info
->update_wanted
)
97 spin_lock_irqsave(&info
->dirty_lock
, flags
);
99 /* Combine with dirty rectangle: */
109 if (xenfb_queue_full(info
)) {
110 /* Can't send right now, stash it in the dirty rectangle */
115 spin_unlock_irqrestore(&info
->dirty_lock
, flags
);
119 /* Clear dirty rectangle: */
120 info
->x1
= info
->y1
= INT_MAX
;
121 info
->x2
= info
->y2
= 0;
123 spin_unlock_irqrestore(&info
->dirty_lock
, flags
);
125 if (x1
<= x2
&& y1
<= y2
)
126 xenfb_do_update(info
, x1
, y1
, x2
- x1
+ 1, y2
- y1
+ 1);
129 static void xenfb_deferred_io(struct fb_info
*fb_info
,
130 struct list_head
*pagelist
)
132 struct xenfb_info
*info
= fb_info
->par
;
134 unsigned long beg
, end
;
135 int y1
, y2
, miny
, maxy
;
139 list_for_each_entry(page
, pagelist
, lru
) {
140 beg
= page
->index
<< PAGE_SHIFT
;
141 end
= beg
+ PAGE_SIZE
- 1;
142 y1
= beg
/ fb_info
->fix
.line_length
;
143 y2
= end
/ fb_info
->fix
.line_length
;
144 if (y2
>= fb_info
->var
.yres
)
145 y2
= fb_info
->var
.yres
- 1;
151 xenfb_refresh(info
, 0, miny
, fb_info
->var
.xres
, maxy
- miny
+ 1);
154 static struct fb_deferred_io xenfb_defio
= {
156 .deferred_io
= xenfb_deferred_io
,
159 static int xenfb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
160 unsigned blue
, unsigned transp
,
161 struct fb_info
*info
)
165 if (regno
> info
->cmap
.len
)
168 #define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
169 red
= CNVT_TOHW(red
, info
->var
.red
.length
);
170 green
= CNVT_TOHW(green
, info
->var
.green
.length
);
171 blue
= CNVT_TOHW(blue
, info
->var
.blue
.length
);
172 transp
= CNVT_TOHW(transp
, info
->var
.transp
.length
);
175 v
= (red
<< info
->var
.red
.offset
) |
176 (green
<< info
->var
.green
.offset
) |
177 (blue
<< info
->var
.blue
.offset
);
179 switch (info
->var
.bits_per_pixel
) {
183 ((u32
*)info
->pseudo_palette
)[regno
] = v
;
190 static void xenfb_fillrect(struct fb_info
*p
, const struct fb_fillrect
*rect
)
192 struct xenfb_info
*info
= p
->par
;
194 sys_fillrect(p
, rect
);
195 xenfb_refresh(info
, rect
->dx
, rect
->dy
, rect
->width
, rect
->height
);
198 static void xenfb_imageblit(struct fb_info
*p
, const struct fb_image
*image
)
200 struct xenfb_info
*info
= p
->par
;
202 sys_imageblit(p
, image
);
203 xenfb_refresh(info
, image
->dx
, image
->dy
, image
->width
, image
->height
);
206 static void xenfb_copyarea(struct fb_info
*p
, const struct fb_copyarea
*area
)
208 struct xenfb_info
*info
= p
->par
;
210 sys_copyarea(p
, area
);
211 xenfb_refresh(info
, area
->dx
, area
->dy
, area
->width
, area
->height
);
214 static ssize_t
xenfb_write(struct fb_info
*p
, const char __user
*buf
,
215 size_t count
, loff_t
*ppos
)
217 struct xenfb_info
*info
= p
->par
;
220 res
= fb_sys_write(p
, buf
, count
, ppos
);
221 xenfb_refresh(info
, 0, 0, info
->page
->width
, info
->page
->height
);
225 static struct fb_ops xenfb_fb_ops
= {
226 .owner
= THIS_MODULE
,
227 .fb_read
= fb_sys_read
,
228 .fb_write
= xenfb_write
,
229 .fb_setcolreg
= xenfb_setcolreg
,
230 .fb_fillrect
= xenfb_fillrect
,
231 .fb_copyarea
= xenfb_copyarea
,
232 .fb_imageblit
= xenfb_imageblit
,
235 static irqreturn_t
xenfb_event_handler(int rq
, void *dev_id
)
238 * No in events recognized, simply ignore them all.
239 * If you need to recognize some, see xen-kbdfront's
240 * input_handler() for how to do that.
242 struct xenfb_info
*info
= dev_id
;
243 struct xenfb_page
*page
= info
->page
;
245 if (page
->in_cons
!= page
->in_prod
) {
246 info
->page
->in_cons
= info
->page
->in_prod
;
247 notify_remote_via_irq(info
->irq
);
250 /* Flush dirty rectangle: */
251 xenfb_refresh(info
, INT_MAX
, INT_MAX
, -INT_MAX
, -INT_MAX
);
256 static int __devinit
xenfb_probe(struct xenbus_device
*dev
,
257 const struct xenbus_device_id
*id
)
259 struct xenfb_info
*info
;
260 struct fb_info
*fb_info
;
263 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
265 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
268 dev
->dev
.driver_data
= info
;
271 info
->x1
= info
->y1
= INT_MAX
;
272 spin_lock_init(&info
->dirty_lock
);
274 info
->fb
= vmalloc(xenfb_mem_len
);
275 if (info
->fb
== NULL
)
277 memset(info
->fb
, 0, xenfb_mem_len
);
279 info
->nr_pages
= (xenfb_mem_len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
281 info
->mfns
= vmalloc(sizeof(unsigned long) * info
->nr_pages
);
285 /* set up shared page */
286 info
->page
= (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
290 xenfb_init_shared_page(info
);
292 /* abusing framebuffer_alloc() to allocate pseudo_palette */
293 fb_info
= framebuffer_alloc(sizeof(u32
) * 256, NULL
);
297 /* complete the abuse: */
298 fb_info
->pseudo_palette
= fb_info
->par
;
301 fb_info
->screen_base
= info
->fb
;
303 fb_info
->fbops
= &xenfb_fb_ops
;
304 fb_info
->var
.xres_virtual
= fb_info
->var
.xres
= info
->page
->width
;
305 fb_info
->var
.yres_virtual
= fb_info
->var
.yres
= info
->page
->height
;
306 fb_info
->var
.bits_per_pixel
= info
->page
->depth
;
308 fb_info
->var
.red
= (struct fb_bitfield
){16, 8, 0};
309 fb_info
->var
.green
= (struct fb_bitfield
){8, 8, 0};
310 fb_info
->var
.blue
= (struct fb_bitfield
){0, 8, 0};
312 fb_info
->var
.activate
= FB_ACTIVATE_NOW
;
313 fb_info
->var
.height
= -1;
314 fb_info
->var
.width
= -1;
315 fb_info
->var
.vmode
= FB_VMODE_NONINTERLACED
;
317 fb_info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
318 fb_info
->fix
.line_length
= info
->page
->line_length
;
319 fb_info
->fix
.smem_start
= 0;
320 fb_info
->fix
.smem_len
= xenfb_mem_len
;
321 strcpy(fb_info
->fix
.id
, "xen");
322 fb_info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
323 fb_info
->fix
.accel
= FB_ACCEL_NONE
;
325 fb_info
->flags
= FBINFO_FLAG_DEFAULT
;
327 ret
= fb_alloc_cmap(&fb_info
->cmap
, 256, 0);
329 framebuffer_release(fb_info
);
330 xenbus_dev_fatal(dev
, ret
, "fb_alloc_cmap");
334 fb_info
->fbdefio
= &xenfb_defio
;
335 fb_deferred_io_init(fb_info
);
337 ret
= register_framebuffer(fb_info
);
339 fb_deferred_io_cleanup(fb_info
);
340 fb_dealloc_cmap(&fb_info
->cmap
);
341 framebuffer_release(fb_info
);
342 xenbus_dev_fatal(dev
, ret
, "register_framebuffer");
345 info
->fb_info
= fb_info
;
347 ret
= xenfb_connect_backend(dev
, info
);
355 xenbus_dev_fatal(dev
, ret
, "allocating device memory");
361 static int xenfb_resume(struct xenbus_device
*dev
)
363 struct xenfb_info
*info
= dev
->dev
.driver_data
;
365 xenfb_disconnect_backend(info
);
366 xenfb_init_shared_page(info
);
367 return xenfb_connect_backend(dev
, info
);
370 static int xenfb_remove(struct xenbus_device
*dev
)
372 struct xenfb_info
*info
= dev
->dev
.driver_data
;
374 xenfb_disconnect_backend(info
);
376 fb_deferred_io_cleanup(info
->fb_info
);
377 unregister_framebuffer(info
->fb_info
);
378 fb_dealloc_cmap(&info
->fb_info
->cmap
);
379 framebuffer_release(info
->fb_info
);
381 free_page((unsigned long)info
->page
);
389 static unsigned long vmalloc_to_mfn(void *address
)
391 return pfn_to_mfn(vmalloc_to_pfn(address
));
394 static void xenfb_init_shared_page(struct xenfb_info
*info
)
398 for (i
= 0; i
< info
->nr_pages
; i
++)
399 info
->mfns
[i
] = vmalloc_to_mfn(info
->fb
+ i
* PAGE_SIZE
);
401 info
->page
->pd
[0] = vmalloc_to_mfn(info
->mfns
);
402 info
->page
->pd
[1] = 0;
403 info
->page
->width
= XENFB_WIDTH
;
404 info
->page
->height
= XENFB_HEIGHT
;
405 info
->page
->depth
= XENFB_DEPTH
;
406 info
->page
->line_length
= (info
->page
->depth
/ 8) * info
->page
->width
;
407 info
->page
->mem_length
= xenfb_mem_len
;
408 info
->page
->in_cons
= info
->page
->in_prod
= 0;
409 info
->page
->out_cons
= info
->page
->out_prod
= 0;
412 static int xenfb_connect_backend(struct xenbus_device
*dev
,
413 struct xenfb_info
*info
)
416 struct xenbus_transaction xbt
;
418 ret
= xenbus_alloc_evtchn(dev
, &evtchn
);
421 ret
= bind_evtchn_to_irqhandler(evtchn
, xenfb_event_handler
,
422 0, dev
->devicetype
, info
);
424 xenbus_free_evtchn(dev
, evtchn
);
425 xenbus_dev_fatal(dev
, ret
, "bind_evtchn_to_irqhandler");
431 ret
= xenbus_transaction_start(&xbt
);
433 xenbus_dev_fatal(dev
, ret
, "starting transaction");
436 ret
= xenbus_printf(xbt
, dev
->nodename
, "page-ref", "%lu",
437 virt_to_mfn(info
->page
));
440 ret
= xenbus_printf(xbt
, dev
->nodename
, "event-channel", "%u",
444 ret
= xenbus_printf(xbt
, dev
->nodename
, "protocol", "%s",
445 XEN_IO_PROTO_ABI_NATIVE
);
448 ret
= xenbus_printf(xbt
, dev
->nodename
, "feature-update", "1");
451 ret
= xenbus_transaction_end(xbt
, 0);
455 xenbus_dev_fatal(dev
, ret
, "completing transaction");
459 xenbus_switch_state(dev
, XenbusStateInitialised
);
463 xenbus_transaction_end(xbt
, 1);
464 xenbus_dev_fatal(dev
, ret
, "writing xenstore");
468 static void xenfb_disconnect_backend(struct xenfb_info
*info
)
471 unbind_from_irqhandler(info
->irq
, info
);
475 static void xenfb_backend_changed(struct xenbus_device
*dev
,
476 enum xenbus_state backend_state
)
478 struct xenfb_info
*info
= dev
->dev
.driver_data
;
481 switch (backend_state
) {
482 case XenbusStateInitialising
:
483 case XenbusStateInitialised
:
484 case XenbusStateUnknown
:
485 case XenbusStateClosed
:
488 case XenbusStateInitWait
:
490 xenbus_switch_state(dev
, XenbusStateConnected
);
493 case XenbusStateConnected
:
495 * Work around xenbus race condition: If backend goes
496 * through InitWait to Connected fast enough, we can
497 * get Connected twice here.
499 if (dev
->state
!= XenbusStateConnected
)
500 goto InitWait
; /* no InitWait seen yet, fudge it */
502 if (xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
503 "request-update", "%d", &val
) < 0)
506 info
->update_wanted
= 1;
509 case XenbusStateClosing
:
510 xenbus_frontend_closed(dev
);
515 static struct xenbus_device_id xenfb_ids
[] = {
520 static struct xenbus_driver xenfb
= {
522 .owner
= THIS_MODULE
,
524 .probe
= xenfb_probe
,
525 .remove
= xenfb_remove
,
526 .resume
= xenfb_resume
,
527 .otherend_changed
= xenfb_backend_changed
,
530 static int __init
xenfb_init(void)
532 if (!is_running_on_xen())
535 /* Nothing to do if running in dom0. */
536 if (is_initial_xendomain())
539 return xenbus_register_frontend(&xenfb
);
542 static void __exit
xenfb_cleanup(void)
544 xenbus_unregister_driver(&xenfb
);
547 module_init(xenfb_init
);
548 module_exit(xenfb_cleanup
);
550 MODULE_LICENSE("GPL");