blob: 5553e517563e478eb070ac439fb954f244d874b6 [file] [log] [blame]
Markus Armbruster4ee36dc2008-04-02 10:54:07 -07001/*
2 * Xen para-virtual frame buffer device
3 *
4 * Copyright (C) 2005-2006 Anthony Liguori <aliguori@us.ibm.com>
5 * Copyright (C) 2006-2008 Red Hat, Inc., Markus Armbruster <armbru@redhat.com>
6 *
7 * Based on linux/drivers/video/q40fb.c
8 *
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
11 * more details.
12 */
13
14/*
15 * TODO:
16 *
17 * Switch to grant tables when they become capable of dealing with the
18 * frame buffer.
19 */
20
Markus Armbruster9e124fe2008-05-26 23:31:07 +010021#include <linux/console.h>
Markus Armbruster4ee36dc2008-04-02 10:54:07 -070022#include <linux/kernel.h>
23#include <linux/errno.h>
24#include <linux/fb.h>
25#include <linux/module.h>
26#include <linux/vmalloc.h>
27#include <linux/mm.h>
28#include <asm/xen/hypervisor.h>
29#include <xen/events.h>
30#include <xen/page.h>
31#include <xen/interface/io/fbif.h>
32#include <xen/interface/io/protocols.h>
33#include <xen/xenbus.h>
34
35struct xenfb_info {
36 unsigned char *fb;
37 struct fb_info *fb_info;
38 int x1, y1, x2, y2; /* dirty rectangle,
39 protected by dirty_lock */
40 spinlock_t dirty_lock;
41 int nr_pages;
42 int irq;
43 struct xenfb_page *page;
44 unsigned long *mfns;
45 int update_wanted; /* XENFB_TYPE_UPDATE wanted */
46
47 struct xenbus_device *xbdev;
48};
49
50static u32 xenfb_mem_len = XENFB_WIDTH * XENFB_HEIGHT * XENFB_DEPTH / 8;
51
Markus Armbruster9e124fe2008-05-26 23:31:07 +010052static void xenfb_make_preferred_console(void);
Markus Armbruster4ee36dc2008-04-02 10:54:07 -070053static int xenfb_remove(struct xenbus_device *);
54static void xenfb_init_shared_page(struct xenfb_info *);
55static int xenfb_connect_backend(struct xenbus_device *, struct xenfb_info *);
56static void xenfb_disconnect_backend(struct xenfb_info *);
57
58static void xenfb_do_update(struct xenfb_info *info,
59 int x, int y, int w, int h)
60{
61 union xenfb_out_event event;
62 u32 prod;
63
64 event.type = XENFB_TYPE_UPDATE;
65 event.update.x = x;
66 event.update.y = y;
67 event.update.width = w;
68 event.update.height = h;
69
70 prod = info->page->out_prod;
71 /* caller ensures !xenfb_queue_full() */
72 mb(); /* ensure ring space available */
73 XENFB_OUT_RING_REF(info->page, prod) = event;
74 wmb(); /* ensure ring contents visible */
75 info->page->out_prod = prod + 1;
76
77 notify_remote_via_irq(info->irq);
78}
79
80static int xenfb_queue_full(struct xenfb_info *info)
81{
82 u32 cons, prod;
83
84 prod = info->page->out_prod;
85 cons = info->page->out_cons;
86 return prod - cons == XENFB_OUT_RING_LEN;
87}
88
89static void xenfb_refresh(struct xenfb_info *info,
90 int x1, int y1, int w, int h)
91{
92 unsigned long flags;
93 int y2 = y1 + h - 1;
94 int x2 = x1 + w - 1;
95
96 if (!info->update_wanted)
97 return;
98
99 spin_lock_irqsave(&info->dirty_lock, flags);
100
101 /* Combine with dirty rectangle: */
102 if (info->y1 < y1)
103 y1 = info->y1;
104 if (info->y2 > y2)
105 y2 = info->y2;
106 if (info->x1 < x1)
107 x1 = info->x1;
108 if (info->x2 > x2)
109 x2 = info->x2;
110
111 if (xenfb_queue_full(info)) {
112 /* Can't send right now, stash it in the dirty rectangle */
113 info->x1 = x1;
114 info->x2 = x2;
115 info->y1 = y1;
116 info->y2 = y2;
117 spin_unlock_irqrestore(&info->dirty_lock, flags);
118 return;
119 }
120
121 /* Clear dirty rectangle: */
122 info->x1 = info->y1 = INT_MAX;
123 info->x2 = info->y2 = 0;
124
125 spin_unlock_irqrestore(&info->dirty_lock, flags);
126
127 if (x1 <= x2 && y1 <= y2)
128 xenfb_do_update(info, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
129}
130
131static void xenfb_deferred_io(struct fb_info *fb_info,
132 struct list_head *pagelist)
133{
134 struct xenfb_info *info = fb_info->par;
135 struct page *page;
136 unsigned long beg, end;
137 int y1, y2, miny, maxy;
138
139 miny = INT_MAX;
140 maxy = 0;
141 list_for_each_entry(page, pagelist, lru) {
142 beg = page->index << PAGE_SHIFT;
143 end = beg + PAGE_SIZE - 1;
144 y1 = beg / fb_info->fix.line_length;
145 y2 = end / fb_info->fix.line_length;
146 if (y2 >= fb_info->var.yres)
147 y2 = fb_info->var.yres - 1;
148 if (miny > y1)
149 miny = y1;
150 if (maxy < y2)
151 maxy = y2;
152 }
153 xenfb_refresh(info, 0, miny, fb_info->var.xres, maxy - miny + 1);
154}
155
156static struct fb_deferred_io xenfb_defio = {
157 .delay = HZ / 20,
158 .deferred_io = xenfb_deferred_io,
159};
160
161static int xenfb_setcolreg(unsigned regno, unsigned red, unsigned green,
162 unsigned blue, unsigned transp,
163 struct fb_info *info)
164{
165 u32 v;
166
167 if (regno > info->cmap.len)
168 return 1;
169
170#define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
171 red = CNVT_TOHW(red, info->var.red.length);
172 green = CNVT_TOHW(green, info->var.green.length);
173 blue = CNVT_TOHW(blue, info->var.blue.length);
174 transp = CNVT_TOHW(transp, info->var.transp.length);
175#undef CNVT_TOHW
176
177 v = (red << info->var.red.offset) |
178 (green << info->var.green.offset) |
179 (blue << info->var.blue.offset);
180
181 switch (info->var.bits_per_pixel) {
182 case 16:
183 case 24:
184 case 32:
185 ((u32 *)info->pseudo_palette)[regno] = v;
186 break;
187 }
188
189 return 0;
190}
191
192static void xenfb_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
193{
194 struct xenfb_info *info = p->par;
195
196 sys_fillrect(p, rect);
197 xenfb_refresh(info, rect->dx, rect->dy, rect->width, rect->height);
198}
199
200static void xenfb_imageblit(struct fb_info *p, const struct fb_image *image)
201{
202 struct xenfb_info *info = p->par;
203
204 sys_imageblit(p, image);
205 xenfb_refresh(info, image->dx, image->dy, image->width, image->height);
206}
207
208static void xenfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
209{
210 struct xenfb_info *info = p->par;
211
212 sys_copyarea(p, area);
213 xenfb_refresh(info, area->dx, area->dy, area->width, area->height);
214}
215
216static ssize_t xenfb_write(struct fb_info *p, const char __user *buf,
217 size_t count, loff_t *ppos)
218{
219 struct xenfb_info *info = p->par;
220 ssize_t res;
221
222 res = fb_sys_write(p, buf, count, ppos);
223 xenfb_refresh(info, 0, 0, info->page->width, info->page->height);
224 return res;
225}
226
227static struct fb_ops xenfb_fb_ops = {
228 .owner = THIS_MODULE,
229 .fb_read = fb_sys_read,
230 .fb_write = xenfb_write,
231 .fb_setcolreg = xenfb_setcolreg,
232 .fb_fillrect = xenfb_fillrect,
233 .fb_copyarea = xenfb_copyarea,
234 .fb_imageblit = xenfb_imageblit,
235};
236
237static irqreturn_t xenfb_event_handler(int rq, void *dev_id)
238{
239 /*
240 * No in events recognized, simply ignore them all.
241 * If you need to recognize some, see xen-kbdfront's
242 * input_handler() for how to do that.
243 */
244 struct xenfb_info *info = dev_id;
245 struct xenfb_page *page = info->page;
246
247 if (page->in_cons != page->in_prod) {
248 info->page->in_cons = info->page->in_prod;
249 notify_remote_via_irq(info->irq);
250 }
251
252 /* Flush dirty rectangle: */
253 xenfb_refresh(info, INT_MAX, INT_MAX, -INT_MAX, -INT_MAX);
254
255 return IRQ_HANDLED;
256}
257
258static int __devinit xenfb_probe(struct xenbus_device *dev,
259 const struct xenbus_device_id *id)
260{
261 struct xenfb_info *info;
262 struct fb_info *fb_info;
263 int ret;
264
265 info = kzalloc(sizeof(*info), GFP_KERNEL);
266 if (info == NULL) {
267 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
268 return -ENOMEM;
269 }
270 dev->dev.driver_data = info;
271 info->xbdev = dev;
272 info->irq = -1;
273 info->x1 = info->y1 = INT_MAX;
274 spin_lock_init(&info->dirty_lock);
275
276 info->fb = vmalloc(xenfb_mem_len);
277 if (info->fb == NULL)
278 goto error_nomem;
279 memset(info->fb, 0, xenfb_mem_len);
280
281 info->nr_pages = (xenfb_mem_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
282
283 info->mfns = vmalloc(sizeof(unsigned long) * info->nr_pages);
284 if (!info->mfns)
285 goto error_nomem;
286
287 /* set up shared page */
288 info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
289 if (!info->page)
290 goto error_nomem;
291
292 xenfb_init_shared_page(info);
293
294 /* abusing framebuffer_alloc() to allocate pseudo_palette */
295 fb_info = framebuffer_alloc(sizeof(u32) * 256, NULL);
296 if (fb_info == NULL)
297 goto error_nomem;
298
299 /* complete the abuse: */
300 fb_info->pseudo_palette = fb_info->par;
301 fb_info->par = info;
302
303 fb_info->screen_base = info->fb;
304
305 fb_info->fbops = &xenfb_fb_ops;
306 fb_info->var.xres_virtual = fb_info->var.xres = info->page->width;
307 fb_info->var.yres_virtual = fb_info->var.yres = info->page->height;
308 fb_info->var.bits_per_pixel = info->page->depth;
309
310 fb_info->var.red = (struct fb_bitfield){16, 8, 0};
311 fb_info->var.green = (struct fb_bitfield){8, 8, 0};
312 fb_info->var.blue = (struct fb_bitfield){0, 8, 0};
313
314 fb_info->var.activate = FB_ACTIVATE_NOW;
315 fb_info->var.height = -1;
316 fb_info->var.width = -1;
317 fb_info->var.vmode = FB_VMODE_NONINTERLACED;
318
319 fb_info->fix.visual = FB_VISUAL_TRUECOLOR;
320 fb_info->fix.line_length = info->page->line_length;
321 fb_info->fix.smem_start = 0;
322 fb_info->fix.smem_len = xenfb_mem_len;
323 strcpy(fb_info->fix.id, "xen");
324 fb_info->fix.type = FB_TYPE_PACKED_PIXELS;
325 fb_info->fix.accel = FB_ACCEL_NONE;
326
327 fb_info->flags = FBINFO_FLAG_DEFAULT;
328
329 ret = fb_alloc_cmap(&fb_info->cmap, 256, 0);
330 if (ret < 0) {
331 framebuffer_release(fb_info);
332 xenbus_dev_fatal(dev, ret, "fb_alloc_cmap");
333 goto error;
334 }
335
336 fb_info->fbdefio = &xenfb_defio;
337 fb_deferred_io_init(fb_info);
338
339 ret = register_framebuffer(fb_info);
340 if (ret) {
341 fb_deferred_io_cleanup(fb_info);
342 fb_dealloc_cmap(&fb_info->cmap);
343 framebuffer_release(fb_info);
344 xenbus_dev_fatal(dev, ret, "register_framebuffer");
345 goto error;
346 }
347 info->fb_info = fb_info;
348
349 ret = xenfb_connect_backend(dev, info);
350 if (ret < 0)
351 goto error;
352
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100353 xenfb_make_preferred_console();
Markus Armbruster4ee36dc2008-04-02 10:54:07 -0700354 return 0;
355
356 error_nomem:
357 ret = -ENOMEM;
358 xenbus_dev_fatal(dev, ret, "allocating device memory");
359 error:
360 xenfb_remove(dev);
361 return ret;
362}
363
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100364static __devinit void
365xenfb_make_preferred_console(void)
366{
367 struct console *c;
368
369 if (console_set_on_cmdline)
370 return;
371
372 acquire_console_sem();
373 for (c = console_drivers; c; c = c->next) {
374 if (!strcmp(c->name, "tty") && c->index == 0)
375 break;
376 }
377 release_console_sem();
378 if (c) {
379 unregister_console(c);
380 c->flags |= CON_CONSDEV;
381 c->flags &= ~CON_PRINTBUFFER; /* don't print again */
382 register_console(c);
383 }
384}
385
Markus Armbruster4ee36dc2008-04-02 10:54:07 -0700386static int xenfb_resume(struct xenbus_device *dev)
387{
388 struct xenfb_info *info = dev->dev.driver_data;
389
390 xenfb_disconnect_backend(info);
391 xenfb_init_shared_page(info);
392 return xenfb_connect_backend(dev, info);
393}
394
395static int xenfb_remove(struct xenbus_device *dev)
396{
397 struct xenfb_info *info = dev->dev.driver_data;
398
399 xenfb_disconnect_backend(info);
400 if (info->fb_info) {
401 fb_deferred_io_cleanup(info->fb_info);
402 unregister_framebuffer(info->fb_info);
403 fb_dealloc_cmap(&info->fb_info->cmap);
404 framebuffer_release(info->fb_info);
405 }
406 free_page((unsigned long)info->page);
407 vfree(info->mfns);
408 vfree(info->fb);
409 kfree(info);
410
411 return 0;
412}
413
414static unsigned long vmalloc_to_mfn(void *address)
415{
416 return pfn_to_mfn(vmalloc_to_pfn(address));
417}
418
419static void xenfb_init_shared_page(struct xenfb_info *info)
420{
421 int i;
422
423 for (i = 0; i < info->nr_pages; i++)
424 info->mfns[i] = vmalloc_to_mfn(info->fb + i * PAGE_SIZE);
425
426 info->page->pd[0] = vmalloc_to_mfn(info->mfns);
427 info->page->pd[1] = 0;
428 info->page->width = XENFB_WIDTH;
429 info->page->height = XENFB_HEIGHT;
430 info->page->depth = XENFB_DEPTH;
431 info->page->line_length = (info->page->depth / 8) * info->page->width;
432 info->page->mem_length = xenfb_mem_len;
433 info->page->in_cons = info->page->in_prod = 0;
434 info->page->out_cons = info->page->out_prod = 0;
435}
436
437static int xenfb_connect_backend(struct xenbus_device *dev,
438 struct xenfb_info *info)
439{
440 int ret, evtchn;
441 struct xenbus_transaction xbt;
442
443 ret = xenbus_alloc_evtchn(dev, &evtchn);
444 if (ret)
445 return ret;
446 ret = bind_evtchn_to_irqhandler(evtchn, xenfb_event_handler,
447 0, dev->devicetype, info);
448 if (ret < 0) {
449 xenbus_free_evtchn(dev, evtchn);
450 xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
451 return ret;
452 }
453 info->irq = ret;
454
455 again:
456 ret = xenbus_transaction_start(&xbt);
457 if (ret) {
458 xenbus_dev_fatal(dev, ret, "starting transaction");
459 return ret;
460 }
461 ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu",
462 virt_to_mfn(info->page));
463 if (ret)
464 goto error_xenbus;
465 ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
466 evtchn);
467 if (ret)
468 goto error_xenbus;
469 ret = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
470 XEN_IO_PROTO_ABI_NATIVE);
471 if (ret)
472 goto error_xenbus;
473 ret = xenbus_printf(xbt, dev->nodename, "feature-update", "1");
474 if (ret)
475 goto error_xenbus;
476 ret = xenbus_transaction_end(xbt, 0);
477 if (ret) {
478 if (ret == -EAGAIN)
479 goto again;
480 xenbus_dev_fatal(dev, ret, "completing transaction");
481 return ret;
482 }
483
484 xenbus_switch_state(dev, XenbusStateInitialised);
485 return 0;
486
487 error_xenbus:
488 xenbus_transaction_end(xbt, 1);
489 xenbus_dev_fatal(dev, ret, "writing xenstore");
490 return ret;
491}
492
493static void xenfb_disconnect_backend(struct xenfb_info *info)
494{
495 if (info->irq >= 0)
496 unbind_from_irqhandler(info->irq, info);
497 info->irq = -1;
498}
499
500static void xenfb_backend_changed(struct xenbus_device *dev,
501 enum xenbus_state backend_state)
502{
503 struct xenfb_info *info = dev->dev.driver_data;
504 int val;
505
506 switch (backend_state) {
507 case XenbusStateInitialising:
508 case XenbusStateInitialised:
509 case XenbusStateUnknown:
510 case XenbusStateClosed:
511 break;
512
513 case XenbusStateInitWait:
514InitWait:
515 xenbus_switch_state(dev, XenbusStateConnected);
516 break;
517
518 case XenbusStateConnected:
519 /*
520 * Work around xenbus race condition: If backend goes
521 * through InitWait to Connected fast enough, we can
522 * get Connected twice here.
523 */
524 if (dev->state != XenbusStateConnected)
525 goto InitWait; /* no InitWait seen yet, fudge it */
526
527 if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
528 "request-update", "%d", &val) < 0)
529 val = 0;
530 if (val)
531 info->update_wanted = 1;
532 break;
533
534 case XenbusStateClosing:
535 xenbus_frontend_closed(dev);
536 break;
537 }
538}
539
540static struct xenbus_device_id xenfb_ids[] = {
541 { "vfb" },
542 { "" }
543};
544
545static struct xenbus_driver xenfb = {
546 .name = "vfb",
547 .owner = THIS_MODULE,
548 .ids = xenfb_ids,
549 .probe = xenfb_probe,
550 .remove = xenfb_remove,
551 .resume = xenfb_resume,
552 .otherend_changed = xenfb_backend_changed,
553};
554
555static int __init xenfb_init(void)
556{
557 if (!is_running_on_xen())
558 return -ENODEV;
559
560 /* Nothing to do if running in dom0. */
561 if (is_initial_xendomain())
562 return -ENODEV;
563
564 return xenbus_register_frontend(&xenfb);
565}
566
567static void __exit xenfb_cleanup(void)
568{
569 xenbus_unregister_driver(&xenfb);
570}
571
572module_init(xenfb_init);
573module_exit(xenfb_cleanup);
574
Markus Armbruster1e892c92008-05-26 23:31:09 +0100575MODULE_DESCRIPTION("Xen virtual framebuffer device frontend");
Markus Armbruster4ee36dc2008-04-02 10:54:07 -0700576MODULE_LICENSE("GPL");
Markus Armbruster1e892c92008-05-26 23:31:09 +0100577MODULE_ALIAS("xen:vfb");