2 * linux/drivers/video/xenfb.c -- Xen para-virtual frame buffer device
4 * Copyright (C) 2005-2006 Anthony Liguori <aliguori@us.ibm.com>
5 * Copyright (C) 2006 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 <linux/freezer.h>
28 #include <asm/hypervisor.h>
29 #include <xen/evtchn.h>
30 #include <xen/interface/io/fbif.h>
31 #include <xen/xenbus.h>
32 #include <linux/kthread.h>
36 struct list_head link;
37 struct vm_area_struct *vma;
40 struct xenfb_info *info;
45 struct task_struct *kthread;
49 struct fb_info *fb_info;
50 struct timer_list refresh;
52 int x1, y1, x2, y2; /* dirty rectangle,
53 protected by dirty_lock */
54 spinlock_t dirty_lock;
58 struct list_head mappings; /* protected by mm_lock */
62 struct xenfb_page *page;
64 int update_wanted; /* XENFB_TYPE_UPDATE wanted */
66 struct xenbus_device *xbdev;
69 static int xenfb_fps = 20;
70 static unsigned long xenfb_mem_len = XENFB_WIDTH * XENFB_HEIGHT * XENFB_DEPTH / 8;
72 static int xenfb_remove(struct xenbus_device *);
73 static void xenfb_init_shared_page(struct xenfb_info *);
74 static int xenfb_connect_backend(struct xenbus_device *, struct xenfb_info *);
75 static void xenfb_disconnect_backend(struct xenfb_info *);
77 static void xenfb_do_update(struct xenfb_info *info,
78 int x, int y, int w, int h)
80 union xenfb_out_event event;
83 event.type = XENFB_TYPE_UPDATE;
86 event.update.width = w;
87 event.update.height = h;
89 prod = info->page->out_prod;
90 /* caller ensures !xenfb_queue_full() */
91 mb(); /* ensure ring space available */
92 XENFB_OUT_RING_REF(info->page, prod) = event;
93 wmb(); /* ensure ring contents visible */
94 info->page->out_prod = prod + 1;
96 notify_remote_via_evtchn(info->evtchn);
99 static int xenfb_queue_full(struct xenfb_info *info)
103 prod = info->page->out_prod;
104 cons = info->page->out_cons;
105 return prod - cons == XENFB_OUT_RING_LEN;
108 static void xenfb_update_screen(struct xenfb_info *info)
112 struct xenfb_mapping *map;
114 if (!info->update_wanted)
116 if (xenfb_queue_full(info))
119 mutex_lock(&info->mm_lock);
121 spin_lock_irqsave(&info->dirty_lock, flags);
126 info->x1 = info->y1 = INT_MAX;
127 info->x2 = info->y2 = 0;
128 spin_unlock_irqrestore(&info->dirty_lock, flags);
130 list_for_each_entry(map, &info->mappings, link) {
133 zap_page_range(map->vma, map->vma->vm_start,
134 map->vma->vm_end - map->vma->vm_start, NULL);
138 mutex_unlock(&info->mm_lock);
140 xenfb_do_update(info, x1, y1, x2 - x1, y2 - y1);
143 static int xenfb_thread(void *data)
145 struct xenfb_info *info = data;
147 while (!kthread_should_stop()) {
150 xenfb_update_screen(info);
152 wait_event_interruptible(info->wq,
153 kthread_should_stop() || info->dirty);
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 red >>= (16 - info->var.red.length);
169 green >>= (16 - info->var.green.length);
170 blue >>= (16 - info->var.blue.length);
172 v = (red << info->var.red.offset) |
173 (green << info->var.green.offset) |
174 (blue << info->var.blue.offset);
176 /* FIXME is this sane? check against xxxfb_setcolreg()! */
177 switch (info->var.bits_per_pixel) {
181 ((u32 *)info->pseudo_palette)[regno] = v;
188 static void xenfb_timer(unsigned long data)
190 struct xenfb_info *info = (struct xenfb_info *)data;
195 static void __xenfb_refresh(struct xenfb_info *info,
196 int x1, int y1, int w, int h)
212 if (timer_pending(&info->refresh))
215 mod_timer(&info->refresh, jiffies + HZ/xenfb_fps);
218 static void xenfb_refresh(struct xenfb_info *info,
219 int x1, int y1, int w, int h)
223 spin_lock_irqsave(&info->dirty_lock, flags);
224 __xenfb_refresh(info, x1, y1, w, h);
225 spin_unlock_irqrestore(&info->dirty_lock, flags);
228 static void xenfb_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
230 struct xenfb_info *info = p->par;
232 cfb_fillrect(p, rect);
233 xenfb_refresh(info, rect->dx, rect->dy, rect->width, rect->height);
236 static void xenfb_imageblit(struct fb_info *p, const struct fb_image *image)
238 struct xenfb_info *info = p->par;
240 cfb_imageblit(p, image);
241 xenfb_refresh(info, image->dx, image->dy, image->width, image->height);
244 static void xenfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
246 struct xenfb_info *info = p->par;
248 cfb_copyarea(p, area);
249 xenfb_refresh(info, area->dx, area->dy, area->width, area->height);
252 static void xenfb_vm_open(struct vm_area_struct *vma)
254 struct xenfb_mapping *map = vma->vm_private_data;
255 atomic_inc(&map->map_refs);
258 static void xenfb_vm_close(struct vm_area_struct *vma)
260 struct xenfb_mapping *map = vma->vm_private_data;
261 struct xenfb_info *info = map->info;
263 mutex_lock(&info->mm_lock);
264 if (atomic_dec_and_test(&map->map_refs)) {
265 list_del(&map->link);
268 mutex_unlock(&info->mm_lock);
271 static struct page *xenfb_vm_nopage(struct vm_area_struct *vma,
272 unsigned long vaddr, int *type)
274 struct xenfb_mapping *map = vma->vm_private_data;
275 struct xenfb_info *info = map->info;
276 int pgnr = (vaddr - vma->vm_start) >> PAGE_SHIFT;
281 if (pgnr >= info->nr_pages)
282 return NOPAGE_SIGBUS;
284 mutex_lock(&info->mm_lock);
285 spin_lock_irqsave(&info->dirty_lock, flags);
286 page = info->pages[pgnr];
290 y1 = pgnr * PAGE_SIZE / info->fb_info->fix.line_length;
291 y2 = (pgnr * PAGE_SIZE + PAGE_SIZE - 1) / info->fb_info->fix.line_length;
292 if (y2 > info->fb_info->var.yres)
293 y2 = info->fb_info->var.yres;
294 __xenfb_refresh(info, 0, y1, info->fb_info->var.xres, y2 - y1);
295 spin_unlock_irqrestore(&info->dirty_lock, flags);
296 mutex_unlock(&info->mm_lock);
299 *type = VM_FAULT_MINOR;
304 static struct vm_operations_struct xenfb_vm_ops = {
305 .open = xenfb_vm_open,
306 .close = xenfb_vm_close,
307 .nopage = xenfb_vm_nopage,
310 static int xenfb_mmap(struct fb_info *fb_info, struct vm_area_struct *vma)
312 struct xenfb_info *info = fb_info->par;
313 struct xenfb_mapping *map;
316 if (!(vma->vm_flags & VM_WRITE))
318 if (!(vma->vm_flags & VM_SHARED))
320 if (vma->vm_pgoff != 0)
323 map_pages = (vma->vm_end - vma->vm_start + PAGE_SIZE-1) >> PAGE_SHIFT;
324 if (map_pages > info->nr_pages)
327 map = kzalloc(sizeof(*map), GFP_KERNEL);
334 atomic_set(&map->map_refs, 1);
336 mutex_lock(&info->mm_lock);
337 list_add(&map->link, &info->mappings);
338 mutex_unlock(&info->mm_lock);
340 vma->vm_ops = &xenfb_vm_ops;
341 vma->vm_flags |= (VM_DONTEXPAND | VM_RESERVED);
342 vma->vm_private_data = map;
347 static struct fb_ops xenfb_fb_ops = {
348 .owner = THIS_MODULE,
349 .fb_setcolreg = xenfb_setcolreg,
350 .fb_fillrect = xenfb_fillrect,
351 .fb_copyarea = xenfb_copyarea,
352 .fb_imageblit = xenfb_imageblit,
353 .fb_mmap = xenfb_mmap,
356 static irqreturn_t xenfb_event_handler(int rq, void *dev_id,
357 struct pt_regs *regs)
360 * No in events recognized, simply ignore them all.
361 * If you need to recognize some, see xenbkd's input_handler()
362 * for how to do that.
364 struct xenfb_info *info = dev_id;
365 struct xenfb_page *page = info->page;
367 if (page->in_cons != page->in_prod) {
368 info->page->in_cons = info->page->in_prod;
369 notify_remote_via_evtchn(info->evtchn);
374 static unsigned long vmalloc_to_mfn(void *address)
376 return pfn_to_mfn(vmalloc_to_pfn(address));
379 static int __devinit xenfb_probe(struct xenbus_device *dev,
380 const struct xenbus_device_id *id)
382 struct xenfb_info *info;
383 struct fb_info *fb_info;
386 info = kzalloc(sizeof(*info), GFP_KERNEL);
388 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
391 dev->dev.driver_data = info;
394 info->x1 = info->y1 = INT_MAX;
395 spin_lock_init(&info->dirty_lock);
396 mutex_init(&info->mm_lock);
397 init_waitqueue_head(&info->wq);
398 init_timer(&info->refresh);
399 info->refresh.function = xenfb_timer;
400 info->refresh.data = (unsigned long)info;
401 INIT_LIST_HEAD(&info->mappings);
403 info->fb = vmalloc(xenfb_mem_len);
404 if (info->fb == NULL)
406 memset(info->fb, 0, xenfb_mem_len);
408 info->nr_pages = (xenfb_mem_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
410 info->pages = kmalloc(sizeof(struct page *) * info->nr_pages,
412 if (info->pages == NULL)
415 info->mfns = vmalloc(sizeof(unsigned long) * info->nr_pages);
419 /* set up shared page */
420 info->page = (void *)__get_free_page(GFP_KERNEL);
424 xenfb_init_shared_page(info);
426 fb_info = framebuffer_alloc(sizeof(u32) * 256, NULL);
427 /* see fishy hackery below */
431 /* FIXME fishy hackery */
432 fb_info->pseudo_palette = fb_info->par;
435 fb_info->screen_base = info->fb;
437 fb_info->fbops = &xenfb_fb_ops;
438 fb_info->var.xres_virtual = fb_info->var.xres = info->page->width;
439 fb_info->var.yres_virtual = fb_info->var.yres = info->page->height;
440 fb_info->var.bits_per_pixel = info->page->depth;
442 fb_info->var.red = (struct fb_bitfield){16, 8, 0};
443 fb_info->var.green = (struct fb_bitfield){8, 8, 0};
444 fb_info->var.blue = (struct fb_bitfield){0, 8, 0};
446 fb_info->var.activate = FB_ACTIVATE_NOW;
447 fb_info->var.height = -1;
448 fb_info->var.width = -1;
449 fb_info->var.vmode = FB_VMODE_NONINTERLACED;
451 fb_info->fix.visual = FB_VISUAL_TRUECOLOR;
452 fb_info->fix.line_length = info->page->line_length;
453 fb_info->fix.smem_start = 0;
454 fb_info->fix.smem_len = xenfb_mem_len;
455 strcpy(fb_info->fix.id, "xen");
456 fb_info->fix.type = FB_TYPE_PACKED_PIXELS;
457 fb_info->fix.accel = FB_ACCEL_NONE;
459 fb_info->flags = FBINFO_FLAG_DEFAULT;
461 ret = fb_alloc_cmap(&fb_info->cmap, 256, 0);
463 framebuffer_release(fb_info);
464 xenbus_dev_fatal(dev, ret, "fb_alloc_cmap");
468 ret = register_framebuffer(fb_info);
470 fb_dealloc_cmap(&info->fb_info->cmap);
471 framebuffer_release(fb_info);
472 xenbus_dev_fatal(dev, ret, "register_framebuffer");
475 info->fb_info = fb_info;
477 /* FIXME should this be delayed until backend XenbusStateConnected? */
478 info->kthread = kthread_run(xenfb_thread, info, "xenfb thread");
479 if (IS_ERR(info->kthread)) {
480 ret = PTR_ERR(info->kthread);
481 info->kthread = NULL;
482 xenbus_dev_fatal(dev, ret, "register_framebuffer");
486 ret = xenfb_connect_backend(dev, info);
494 xenbus_dev_fatal(dev, ret, "allocating device memory");
500 static int xenfb_resume(struct xenbus_device *dev)
502 struct xenfb_info *info = dev->dev.driver_data;
504 xenfb_disconnect_backend(info);
505 xenfb_init_shared_page(info);
506 return xenfb_connect_backend(dev, info);
509 static int xenfb_remove(struct xenbus_device *dev)
511 struct xenfb_info *info = dev->dev.driver_data;
513 del_timer(&info->refresh);
515 kthread_stop(info->kthread);
516 xenfb_disconnect_backend(info);
518 unregister_framebuffer(info->fb_info);
519 fb_dealloc_cmap(&info->fb_info->cmap);
520 framebuffer_release(info->fb_info);
522 free_page((unsigned long)info->page);
531 static void xenfb_init_shared_page(struct xenfb_info *info)
535 for (i = 0; i < info->nr_pages; i++)
536 info->pages[i] = vmalloc_to_page(info->fb + i * PAGE_SIZE);
538 for (i = 0; i < info->nr_pages; i++)
539 info->mfns[i] = vmalloc_to_mfn(info->fb + i * PAGE_SIZE);
541 info->page->pd[0] = vmalloc_to_mfn(info->mfns);
542 info->page->pd[1] = 0;
543 info->page->width = XENFB_WIDTH;
544 info->page->height = XENFB_HEIGHT;
545 info->page->depth = XENFB_DEPTH;
546 info->page->line_length = (info->page->depth / 8) * info->page->width;
547 info->page->mem_length = xenfb_mem_len;
548 info->page->in_cons = info->page->in_prod = 0;
549 info->page->out_cons = info->page->out_prod = 0;
552 static int xenfb_connect_backend(struct xenbus_device *dev,
553 struct xenfb_info *info)
556 struct xenbus_transaction xbt;
558 ret = xenbus_alloc_evtchn(dev, &info->evtchn);
561 ret = bind_evtchn_to_irqhandler(info->evtchn, xenfb_event_handler,
564 xenbus_free_evtchn(dev, info->evtchn);
565 xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
571 ret = xenbus_transaction_start(&xbt);
573 xenbus_dev_fatal(dev, ret, "starting transaction");
576 ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu",
577 virt_to_mfn(info->page));
580 ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
584 ret = xenbus_printf(xbt, dev->nodename, "feature-update", "1");
587 ret = xenbus_transaction_end(xbt, 0);
591 xenbus_dev_fatal(dev, ret, "completing transaction");
595 xenbus_switch_state(dev, XenbusStateInitialised);
599 xenbus_transaction_end(xbt, 1);
600 xenbus_dev_fatal(dev, ret, "writing xenstore");
604 static void xenfb_disconnect_backend(struct xenfb_info *info)
607 unbind_from_irqhandler(info->irq, info);
611 static void xenfb_backend_changed(struct xenbus_device *dev,
612 enum xenbus_state backend_state)
614 struct xenfb_info *info = dev->dev.driver_data;
617 switch (backend_state) {
618 case XenbusStateInitialising:
619 case XenbusStateInitialised:
620 case XenbusStateUnknown:
621 case XenbusStateClosed:
624 case XenbusStateInitWait:
626 xenbus_switch_state(dev, XenbusStateConnected);
629 case XenbusStateConnected:
631 * Work around xenbus race condition: If backend goes
632 * through InitWait to Connected fast enough, we can
633 * get Connected twice here.
635 if (dev->state != XenbusStateConnected)
636 goto InitWait; /* no InitWait seen yet, fudge it */
638 if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
639 "request-update", "%d", &val) < 0)
642 info->update_wanted = 1;
645 case XenbusStateClosing:
646 // FIXME is this safe in any dev->state?
647 xenbus_frontend_closed(dev);
652 static struct xenbus_device_id xenfb_ids[] = {
657 static struct xenbus_driver xenfb = {
659 .owner = THIS_MODULE,
661 .probe = xenfb_probe,
662 .remove = xenfb_remove,
663 .resume = xenfb_resume,
664 .otherend_changed = xenfb_backend_changed,
667 static int __init xenfb_init(void)
669 if (!is_running_on_xen())
672 /* Nothing to do if running in dom0. */
673 if (is_initial_xendomain())
676 return xenbus_register_frontend(&xenfb);
679 static void __exit xenfb_cleanup(void)
681 return xenbus_unregister_driver(&xenfb);
684 module_init(xenfb_init);
685 module_exit(xenfb_cleanup);
687 MODULE_LICENSE("GPL");