Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 1 | /* |
| 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 Armbruster | 9e124fe | 2008-05-26 23:31:07 +0100 | [diff] [blame] | 21 | #include <linux/console.h> |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 22 | #include <linux/kernel.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/fb.h> |
| 25 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 27 | #include <linux/vmalloc.h> |
| 28 | #include <linux/mm.h> |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 29 | |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 30 | #include <asm/xen/hypervisor.h> |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 31 | |
| 32 | #include <xen/xen.h> |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 33 | #include <xen/events.h> |
| 34 | #include <xen/page.h> |
| 35 | #include <xen/interface/io/fbif.h> |
| 36 | #include <xen/interface/io/protocols.h> |
| 37 | #include <xen/xenbus.h> |
| 38 | |
| 39 | struct xenfb_info { |
| 40 | unsigned char *fb; |
| 41 | struct fb_info *fb_info; |
| 42 | int x1, y1, x2, y2; /* dirty rectangle, |
| 43 | protected by dirty_lock */ |
| 44 | spinlock_t dirty_lock; |
| 45 | int nr_pages; |
| 46 | int irq; |
| 47 | struct xenfb_page *page; |
| 48 | unsigned long *mfns; |
| 49 | int update_wanted; /* XENFB_TYPE_UPDATE wanted */ |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 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; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 54 | |
| 55 | struct xenbus_device *xbdev; |
| 56 | }; |
| 57 | |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 58 | #define XENFB_DEFAULT_FB_LEN (XENFB_WIDTH * XENFB_HEIGHT * XENFB_DEPTH / 8) |
| 59 | |
| 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)"); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 65 | |
Markus Armbruster | 9e124fe | 2008-05-26 23:31:07 +0100 | [diff] [blame] | 66 | static void xenfb_make_preferred_console(void); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 67 | static int xenfb_remove(struct xenbus_device *); |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 68 | static void xenfb_init_shared_page(struct xenfb_info *, struct fb_info *); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 69 | static int xenfb_connect_backend(struct xenbus_device *, struct xenfb_info *); |
| 70 | static void xenfb_disconnect_backend(struct xenfb_info *); |
| 71 | |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 72 | static void xenfb_send_event(struct xenfb_info *info, |
| 73 | union xenfb_out_event *event) |
| 74 | { |
| 75 | u32 prod; |
| 76 | |
| 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; |
| 83 | |
| 84 | notify_remote_via_irq(info->irq); |
| 85 | } |
| 86 | |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 87 | static void xenfb_do_update(struct xenfb_info *info, |
| 88 | int x, int y, int w, int h) |
| 89 | { |
| 90 | union xenfb_out_event event; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 91 | |
Markus Armbruster | f4ad1eb | 2008-05-26 23:31:10 +0100 | [diff] [blame] | 92 | memset(&event, 0, sizeof(event)); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 93 | event.type = XENFB_TYPE_UPDATE; |
| 94 | event.update.x = x; |
| 95 | event.update.y = y; |
| 96 | event.update.width = w; |
| 97 | event.update.height = h; |
| 98 | |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 99 | /* caller ensures !xenfb_queue_full() */ |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 100 | xenfb_send_event(info, &event); |
| 101 | } |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 102 | |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 103 | static void xenfb_do_resize(struct xenfb_info *info) |
| 104 | { |
| 105 | union xenfb_out_event event; |
| 106 | |
| 107 | memset(&event, 0, sizeof(event)); |
| 108 | event.resize = info->resize; |
| 109 | |
| 110 | /* caller ensures !xenfb_queue_full() */ |
| 111 | xenfb_send_event(info, &event); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static int xenfb_queue_full(struct xenfb_info *info) |
| 115 | { |
| 116 | u32 cons, prod; |
| 117 | |
| 118 | prod = info->page->out_prod; |
| 119 | cons = info->page->out_cons; |
| 120 | return prod - cons == XENFB_OUT_RING_LEN; |
| 121 | } |
| 122 | |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 123 | static void xenfb_handle_resize_dpy(struct xenfb_info *info) |
| 124 | { |
| 125 | unsigned long flags; |
| 126 | |
| 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); |
| 132 | } |
| 133 | } |
| 134 | spin_unlock_irqrestore(&info->resize_lock, flags); |
| 135 | } |
| 136 | |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 137 | static void xenfb_refresh(struct xenfb_info *info, |
| 138 | int x1, int y1, int w, int h) |
| 139 | { |
| 140 | unsigned long flags; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 141 | int x2 = x1 + w - 1; |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 142 | int y2 = y1 + h - 1; |
| 143 | |
| 144 | xenfb_handle_resize_dpy(info); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 145 | |
| 146 | if (!info->update_wanted) |
| 147 | return; |
| 148 | |
| 149 | spin_lock_irqsave(&info->dirty_lock, flags); |
| 150 | |
| 151 | /* Combine with dirty rectangle: */ |
| 152 | if (info->y1 < y1) |
| 153 | y1 = info->y1; |
| 154 | if (info->y2 > y2) |
| 155 | y2 = info->y2; |
| 156 | if (info->x1 < x1) |
| 157 | x1 = info->x1; |
| 158 | if (info->x2 > x2) |
| 159 | x2 = info->x2; |
| 160 | |
| 161 | if (xenfb_queue_full(info)) { |
| 162 | /* Can't send right now, stash it in the dirty rectangle */ |
| 163 | info->x1 = x1; |
| 164 | info->x2 = x2; |
| 165 | info->y1 = y1; |
| 166 | info->y2 = y2; |
| 167 | spin_unlock_irqrestore(&info->dirty_lock, flags); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | /* Clear dirty rectangle: */ |
| 172 | info->x1 = info->y1 = INT_MAX; |
| 173 | info->x2 = info->y2 = 0; |
| 174 | |
| 175 | spin_unlock_irqrestore(&info->dirty_lock, flags); |
| 176 | |
| 177 | if (x1 <= x2 && y1 <= y2) |
| 178 | xenfb_do_update(info, x1, y1, x2 - x1 + 1, y2 - y1 + 1); |
| 179 | } |
| 180 | |
| 181 | static void xenfb_deferred_io(struct fb_info *fb_info, |
| 182 | struct list_head *pagelist) |
| 183 | { |
| 184 | struct xenfb_info *info = fb_info->par; |
| 185 | struct page *page; |
| 186 | unsigned long beg, end; |
| 187 | int y1, y2, miny, maxy; |
| 188 | |
| 189 | miny = INT_MAX; |
| 190 | maxy = 0; |
| 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; |
| 198 | if (miny > y1) |
| 199 | miny = y1; |
| 200 | if (maxy < y2) |
| 201 | maxy = y2; |
| 202 | } |
| 203 | xenfb_refresh(info, 0, miny, fb_info->var.xres, maxy - miny + 1); |
| 204 | } |
| 205 | |
| 206 | static struct fb_deferred_io xenfb_defio = { |
| 207 | .delay = HZ / 20, |
| 208 | .deferred_io = xenfb_deferred_io, |
| 209 | }; |
| 210 | |
| 211 | static int xenfb_setcolreg(unsigned regno, unsigned red, unsigned green, |
| 212 | unsigned blue, unsigned transp, |
| 213 | struct fb_info *info) |
| 214 | { |
| 215 | u32 v; |
| 216 | |
| 217 | if (regno > info->cmap.len) |
| 218 | return 1; |
| 219 | |
| 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); |
| 225 | #undef CNVT_TOHW |
| 226 | |
| 227 | v = (red << info->var.red.offset) | |
| 228 | (green << info->var.green.offset) | |
| 229 | (blue << info->var.blue.offset); |
| 230 | |
| 231 | switch (info->var.bits_per_pixel) { |
| 232 | case 16: |
| 233 | case 24: |
| 234 | case 32: |
| 235 | ((u32 *)info->pseudo_palette)[regno] = v; |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static void xenfb_fillrect(struct fb_info *p, const struct fb_fillrect *rect) |
| 243 | { |
| 244 | struct xenfb_info *info = p->par; |
| 245 | |
| 246 | sys_fillrect(p, rect); |
| 247 | xenfb_refresh(info, rect->dx, rect->dy, rect->width, rect->height); |
| 248 | } |
| 249 | |
| 250 | static void xenfb_imageblit(struct fb_info *p, const struct fb_image *image) |
| 251 | { |
| 252 | struct xenfb_info *info = p->par; |
| 253 | |
| 254 | sys_imageblit(p, image); |
| 255 | xenfb_refresh(info, image->dx, image->dy, image->width, image->height); |
| 256 | } |
| 257 | |
| 258 | static void xenfb_copyarea(struct fb_info *p, const struct fb_copyarea *area) |
| 259 | { |
| 260 | struct xenfb_info *info = p->par; |
| 261 | |
| 262 | sys_copyarea(p, area); |
| 263 | xenfb_refresh(info, area->dx, area->dy, area->width, area->height); |
| 264 | } |
| 265 | |
| 266 | static ssize_t xenfb_write(struct fb_info *p, const char __user *buf, |
| 267 | size_t count, loff_t *ppos) |
| 268 | { |
| 269 | struct xenfb_info *info = p->par; |
| 270 | ssize_t res; |
| 271 | |
| 272 | res = fb_sys_write(p, buf, count, ppos); |
| 273 | xenfb_refresh(info, 0, 0, info->page->width, info->page->height); |
| 274 | return res; |
| 275 | } |
| 276 | |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 277 | static int |
| 278 | xenfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info) |
| 279 | { |
| 280 | struct xenfb_info *xenfb_info; |
| 281 | int required_mem_len; |
| 282 | |
| 283 | xenfb_info = info->par; |
| 284 | |
| 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) { |
| 289 | return 0; |
| 290 | } |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | |
| 294 | /* Can't resize past initial width and height */ |
| 295 | if (var->xres > video[KPARAM_WIDTH] || var->yres > video[KPARAM_HEIGHT]) |
| 296 | return -EINVAL; |
| 297 | |
| 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; |
| 304 | return 0; |
| 305 | } |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | |
| 309 | static int xenfb_set_par(struct fb_info *info) |
| 310 | { |
| 311 | struct xenfb_info *xenfb_info; |
| 312 | unsigned long flags; |
| 313 | |
| 314 | xenfb_info = info->par; |
| 315 | |
| 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); |
| 325 | return 0; |
| 326 | } |
| 327 | |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 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, |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 336 | .fb_check_var = xenfb_check_var, |
| 337 | .fb_set_par = xenfb_set_par, |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | static irqreturn_t xenfb_event_handler(int rq, void *dev_id) |
| 341 | { |
| 342 | /* |
| 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. |
| 346 | */ |
| 347 | struct xenfb_info *info = dev_id; |
| 348 | struct xenfb_page *page = info->page; |
| 349 | |
| 350 | if (page->in_cons != page->in_prod) { |
| 351 | info->page->in_cons = info->page->in_prod; |
| 352 | notify_remote_via_irq(info->irq); |
| 353 | } |
| 354 | |
| 355 | /* Flush dirty rectangle: */ |
| 356 | xenfb_refresh(info, INT_MAX, INT_MAX, -INT_MAX, -INT_MAX); |
| 357 | |
| 358 | return IRQ_HANDLED; |
| 359 | } |
| 360 | |
| 361 | static int __devinit xenfb_probe(struct xenbus_device *dev, |
| 362 | const struct xenbus_device_id *id) |
| 363 | { |
| 364 | struct xenfb_info *info; |
| 365 | struct fb_info *fb_info; |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 366 | int fb_size; |
| 367 | int val; |
Julia Lawall | a7a97c6 | 2012-04-22 11:57:40 +0200 | [diff] [blame] | 368 | int ret = 0; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 369 | |
| 370 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 371 | if (info == NULL) { |
| 372 | xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure"); |
| 373 | return -ENOMEM; |
| 374 | } |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 375 | |
| 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; |
| 380 | } |
| 381 | |
| 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 |
| 385 | > fb_size) { |
| 386 | video[KPARAM_WIDTH] = XENFB_WIDTH; |
| 387 | video[KPARAM_HEIGHT] = XENFB_HEIGHT; |
| 388 | fb_size = XENFB_DEFAULT_FB_LEN; |
| 389 | } |
| 390 | |
Greg Kroah-Hartman | 1b713e0 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 391 | dev_set_drvdata(&dev->dev, info); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 392 | info->xbdev = dev; |
| 393 | info->irq = -1; |
| 394 | info->x1 = info->y1 = INT_MAX; |
| 395 | spin_lock_init(&info->dirty_lock); |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 396 | spin_lock_init(&info->resize_lock); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 397 | |
Joe Perches | 1b86d77 | 2011-05-28 11:13:33 -0700 | [diff] [blame] | 398 | info->fb = vzalloc(fb_size); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 399 | if (info->fb == NULL) |
| 400 | goto error_nomem; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 401 | |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 402 | info->nr_pages = (fb_size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 403 | |
| 404 | info->mfns = vmalloc(sizeof(unsigned long) * info->nr_pages); |
| 405 | if (!info->mfns) |
| 406 | goto error_nomem; |
| 407 | |
| 408 | /* set up shared page */ |
| 409 | info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO); |
| 410 | if (!info->page) |
| 411 | goto error_nomem; |
| 412 | |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 413 | /* abusing framebuffer_alloc() to allocate pseudo_palette */ |
| 414 | fb_info = framebuffer_alloc(sizeof(u32) * 256, NULL); |
| 415 | if (fb_info == NULL) |
| 416 | goto error_nomem; |
| 417 | |
| 418 | /* complete the abuse: */ |
| 419 | fb_info->pseudo_palette = fb_info->par; |
| 420 | fb_info->par = info; |
| 421 | |
| 422 | fb_info->screen_base = info->fb; |
| 423 | |
| 424 | fb_info->fbops = &xenfb_fb_ops; |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 425 | fb_info->var.xres_virtual = fb_info->var.xres = video[KPARAM_WIDTH]; |
| 426 | fb_info->var.yres_virtual = fb_info->var.yres = video[KPARAM_HEIGHT]; |
| 427 | fb_info->var.bits_per_pixel = XENFB_DEPTH; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 428 | |
| 429 | fb_info->var.red = (struct fb_bitfield){16, 8, 0}; |
| 430 | fb_info->var.green = (struct fb_bitfield){8, 8, 0}; |
| 431 | fb_info->var.blue = (struct fb_bitfield){0, 8, 0}; |
| 432 | |
| 433 | fb_info->var.activate = FB_ACTIVATE_NOW; |
| 434 | fb_info->var.height = -1; |
| 435 | fb_info->var.width = -1; |
| 436 | fb_info->var.vmode = FB_VMODE_NONINTERLACED; |
| 437 | |
| 438 | fb_info->fix.visual = FB_VISUAL_TRUECOLOR; |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 439 | fb_info->fix.line_length = fb_info->var.xres * XENFB_DEPTH / 8; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 440 | fb_info->fix.smem_start = 0; |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 441 | fb_info->fix.smem_len = fb_size; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 442 | strcpy(fb_info->fix.id, "xen"); |
| 443 | fb_info->fix.type = FB_TYPE_PACKED_PIXELS; |
| 444 | fb_info->fix.accel = FB_ACCEL_NONE; |
| 445 | |
Konrad Rzeszutek Wilk | df11303 | 2009-12-03 10:31:57 -0500 | [diff] [blame] | 446 | fb_info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 447 | |
| 448 | ret = fb_alloc_cmap(&fb_info->cmap, 256, 0); |
| 449 | if (ret < 0) { |
| 450 | framebuffer_release(fb_info); |
| 451 | xenbus_dev_fatal(dev, ret, "fb_alloc_cmap"); |
| 452 | goto error; |
| 453 | } |
| 454 | |
| 455 | fb_info->fbdefio = &xenfb_defio; |
| 456 | fb_deferred_io_init(fb_info); |
| 457 | |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 458 | xenfb_init_shared_page(info, fb_info); |
| 459 | |
Jeremy Fitzhardinge | 0a80fb1 | 2009-08-27 12:22:43 -0700 | [diff] [blame] | 460 | ret = xenfb_connect_backend(dev, info); |
Julia Lawall | a7a97c6 | 2012-04-22 11:57:40 +0200 | [diff] [blame] | 461 | if (ret < 0) { |
| 462 | xenbus_dev_fatal(dev, ret, "xenfb_connect_backend"); |
| 463 | goto error_fb; |
| 464 | } |
Jeremy Fitzhardinge | 0a80fb1 | 2009-08-27 12:22:43 -0700 | [diff] [blame] | 465 | |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 466 | ret = register_framebuffer(fb_info); |
| 467 | if (ret) { |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 468 | xenbus_dev_fatal(dev, ret, "register_framebuffer"); |
Julia Lawall | a7a97c6 | 2012-04-22 11:57:40 +0200 | [diff] [blame] | 469 | goto error_fb; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 470 | } |
| 471 | info->fb_info = fb_info; |
| 472 | |
Markus Armbruster | 9e124fe | 2008-05-26 23:31:07 +0100 | [diff] [blame] | 473 | xenfb_make_preferred_console(); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 474 | return 0; |
| 475 | |
Julia Lawall | a7a97c6 | 2012-04-22 11:57:40 +0200 | [diff] [blame] | 476 | error_fb: |
| 477 | fb_deferred_io_cleanup(fb_info); |
| 478 | fb_dealloc_cmap(&fb_info->cmap); |
| 479 | framebuffer_release(fb_info); |
| 480 | error_nomem: |
| 481 | if (!ret) { |
| 482 | ret = -ENOMEM; |
| 483 | xenbus_dev_fatal(dev, ret, "allocating device memory"); |
| 484 | } |
| 485 | error: |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 486 | xenfb_remove(dev); |
| 487 | return ret; |
| 488 | } |
| 489 | |
Markus Armbruster | 9e124fe | 2008-05-26 23:31:07 +0100 | [diff] [blame] | 490 | static __devinit void |
| 491 | xenfb_make_preferred_console(void) |
| 492 | { |
| 493 | struct console *c; |
| 494 | |
| 495 | if (console_set_on_cmdline) |
| 496 | return; |
| 497 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 498 | console_lock(); |
Jiri Slaby | 281e660 | 2010-11-04 16:20:22 +0100 | [diff] [blame] | 499 | for_each_console(c) { |
Markus Armbruster | 9e124fe | 2008-05-26 23:31:07 +0100 | [diff] [blame] | 500 | if (!strcmp(c->name, "tty") && c->index == 0) |
| 501 | break; |
| 502 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 503 | console_unlock(); |
Markus Armbruster | 9e124fe | 2008-05-26 23:31:07 +0100 | [diff] [blame] | 504 | if (c) { |
| 505 | unregister_console(c); |
| 506 | c->flags |= CON_CONSDEV; |
| 507 | c->flags &= ~CON_PRINTBUFFER; /* don't print again */ |
| 508 | register_console(c); |
| 509 | } |
| 510 | } |
| 511 | |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 512 | static int xenfb_resume(struct xenbus_device *dev) |
| 513 | { |
Greg Kroah-Hartman | 1b713e0 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 514 | struct xenfb_info *info = dev_get_drvdata(&dev->dev); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 515 | |
| 516 | xenfb_disconnect_backend(info); |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 517 | xenfb_init_shared_page(info, info->fb_info); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 518 | return xenfb_connect_backend(dev, info); |
| 519 | } |
| 520 | |
| 521 | static int xenfb_remove(struct xenbus_device *dev) |
| 522 | { |
Greg Kroah-Hartman | 1b713e0 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 523 | struct xenfb_info *info = dev_get_drvdata(&dev->dev); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 524 | |
| 525 | xenfb_disconnect_backend(info); |
| 526 | if (info->fb_info) { |
| 527 | fb_deferred_io_cleanup(info->fb_info); |
| 528 | unregister_framebuffer(info->fb_info); |
| 529 | fb_dealloc_cmap(&info->fb_info->cmap); |
| 530 | framebuffer_release(info->fb_info); |
| 531 | } |
| 532 | free_page((unsigned long)info->page); |
| 533 | vfree(info->mfns); |
| 534 | vfree(info->fb); |
| 535 | kfree(info); |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static unsigned long vmalloc_to_mfn(void *address) |
| 541 | { |
| 542 | return pfn_to_mfn(vmalloc_to_pfn(address)); |
| 543 | } |
| 544 | |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 545 | static void xenfb_init_shared_page(struct xenfb_info *info, |
| 546 | struct fb_info *fb_info) |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 547 | { |
| 548 | int i; |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 549 | int epd = PAGE_SIZE / sizeof(info->mfns[0]); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 550 | |
| 551 | for (i = 0; i < info->nr_pages; i++) |
| 552 | info->mfns[i] = vmalloc_to_mfn(info->fb + i * PAGE_SIZE); |
| 553 | |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 554 | for (i = 0; i * epd < info->nr_pages; i++) |
| 555 | info->page->pd[i] = vmalloc_to_mfn(&info->mfns[i * epd]); |
| 556 | |
| 557 | info->page->width = fb_info->var.xres; |
| 558 | info->page->height = fb_info->var.yres; |
| 559 | info->page->depth = fb_info->var.bits_per_pixel; |
| 560 | info->page->line_length = fb_info->fix.line_length; |
| 561 | info->page->mem_length = fb_info->fix.smem_len; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 562 | info->page->in_cons = info->page->in_prod = 0; |
| 563 | info->page->out_cons = info->page->out_prod = 0; |
| 564 | } |
| 565 | |
| 566 | static int xenfb_connect_backend(struct xenbus_device *dev, |
| 567 | struct xenfb_info *info) |
| 568 | { |
Joe Jin | fc550e9 | 2011-01-07 18:20:54 +0800 | [diff] [blame] | 569 | int ret, evtchn, irq; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 570 | struct xenbus_transaction xbt; |
| 571 | |
| 572 | ret = xenbus_alloc_evtchn(dev, &evtchn); |
| 573 | if (ret) |
| 574 | return ret; |
Joe Jin | fc550e9 | 2011-01-07 18:20:54 +0800 | [diff] [blame] | 575 | irq = bind_evtchn_to_irqhandler(evtchn, xenfb_event_handler, |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 576 | 0, dev->devicetype, info); |
Joe Jin | fc550e9 | 2011-01-07 18:20:54 +0800 | [diff] [blame] | 577 | if (irq < 0) { |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 578 | xenbus_free_evtchn(dev, evtchn); |
| 579 | xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler"); |
Joe Jin | fc550e9 | 2011-01-07 18:20:54 +0800 | [diff] [blame] | 580 | return irq; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 581 | } |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 582 | again: |
| 583 | ret = xenbus_transaction_start(&xbt); |
| 584 | if (ret) { |
| 585 | xenbus_dev_fatal(dev, ret, "starting transaction"); |
Joe Jin | fc550e9 | 2011-01-07 18:20:54 +0800 | [diff] [blame] | 586 | goto unbind_irq; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 587 | } |
| 588 | ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu", |
| 589 | virt_to_mfn(info->page)); |
| 590 | if (ret) |
| 591 | goto error_xenbus; |
| 592 | ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u", |
| 593 | evtchn); |
| 594 | if (ret) |
| 595 | goto error_xenbus; |
| 596 | ret = xenbus_printf(xbt, dev->nodename, "protocol", "%s", |
| 597 | XEN_IO_PROTO_ABI_NATIVE); |
| 598 | if (ret) |
| 599 | goto error_xenbus; |
| 600 | ret = xenbus_printf(xbt, dev->nodename, "feature-update", "1"); |
| 601 | if (ret) |
| 602 | goto error_xenbus; |
| 603 | ret = xenbus_transaction_end(xbt, 0); |
| 604 | if (ret) { |
| 605 | if (ret == -EAGAIN) |
| 606 | goto again; |
| 607 | xenbus_dev_fatal(dev, ret, "completing transaction"); |
Joe Jin | fc550e9 | 2011-01-07 18:20:54 +0800 | [diff] [blame] | 608 | goto unbind_irq; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | xenbus_switch_state(dev, XenbusStateInitialised); |
Joe Jin | fc550e9 | 2011-01-07 18:20:54 +0800 | [diff] [blame] | 612 | info->irq = irq; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 613 | return 0; |
| 614 | |
| 615 | error_xenbus: |
| 616 | xenbus_transaction_end(xbt, 1); |
| 617 | xenbus_dev_fatal(dev, ret, "writing xenstore"); |
Joe Jin | fc550e9 | 2011-01-07 18:20:54 +0800 | [diff] [blame] | 618 | unbind_irq: |
| 619 | unbind_from_irqhandler(irq, info); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 620 | return ret; |
| 621 | } |
| 622 | |
| 623 | static void xenfb_disconnect_backend(struct xenfb_info *info) |
| 624 | { |
Joe Jin | 731f3ab | 2011-01-07 18:17:17 +0800 | [diff] [blame] | 625 | /* Prevent xenfb refresh */ |
| 626 | info->update_wanted = 0; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 627 | if (info->irq >= 0) |
| 628 | unbind_from_irqhandler(info->irq, info); |
| 629 | info->irq = -1; |
| 630 | } |
| 631 | |
| 632 | static void xenfb_backend_changed(struct xenbus_device *dev, |
| 633 | enum xenbus_state backend_state) |
| 634 | { |
Greg Kroah-Hartman | 1b713e0 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 635 | struct xenfb_info *info = dev_get_drvdata(&dev->dev); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 636 | int val; |
| 637 | |
| 638 | switch (backend_state) { |
| 639 | case XenbusStateInitialising: |
| 640 | case XenbusStateInitialised: |
Noboru Iwamatsu | b78c951 | 2009-10-13 17:22:29 -0400 | [diff] [blame] | 641 | case XenbusStateReconfiguring: |
| 642 | case XenbusStateReconfigured: |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 643 | case XenbusStateUnknown: |
| 644 | case XenbusStateClosed: |
| 645 | break; |
| 646 | |
| 647 | case XenbusStateInitWait: |
| 648 | InitWait: |
| 649 | xenbus_switch_state(dev, XenbusStateConnected); |
| 650 | break; |
| 651 | |
| 652 | case XenbusStateConnected: |
| 653 | /* |
| 654 | * Work around xenbus race condition: If backend goes |
| 655 | * through InitWait to Connected fast enough, we can |
| 656 | * get Connected twice here. |
| 657 | */ |
| 658 | if (dev->state != XenbusStateConnected) |
| 659 | goto InitWait; /* no InitWait seen yet, fudge it */ |
| 660 | |
| 661 | if (xenbus_scanf(XBT_NIL, info->xbdev->otherend, |
| 662 | "request-update", "%d", &val) < 0) |
| 663 | val = 0; |
| 664 | if (val) |
| 665 | info->update_wanted = 1; |
Markus Armbruster | e4dcff1 | 2008-05-26 23:31:11 +0100 | [diff] [blame] | 666 | |
| 667 | if (xenbus_scanf(XBT_NIL, dev->otherend, |
| 668 | "feature-resize", "%d", &val) < 0) |
| 669 | val = 0; |
| 670 | info->feature_resize = val; |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 671 | break; |
| 672 | |
| 673 | case XenbusStateClosing: |
| 674 | xenbus_frontend_closed(dev); |
| 675 | break; |
| 676 | } |
| 677 | } |
| 678 | |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 679 | static const struct xenbus_device_id xenfb_ids[] = { |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 680 | { "vfb" }, |
| 681 | { "" } |
| 682 | }; |
| 683 | |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 684 | static DEFINE_XENBUS_DRIVER(xenfb, , |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 685 | .probe = xenfb_probe, |
| 686 | .remove = xenfb_remove, |
| 687 | .resume = xenfb_resume, |
| 688 | .otherend_changed = xenfb_backend_changed, |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 689 | ); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 690 | |
| 691 | static int __init xenfb_init(void) |
| 692 | { |
Sheng Yang | bee6ab5 | 2010-05-14 12:39:33 +0100 | [diff] [blame] | 693 | if (!xen_pv_domain()) |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 694 | return -ENODEV; |
| 695 | |
| 696 | /* Nothing to do if running in dom0. */ |
Jeremy Fitzhardinge | 6e83358 | 2008-08-19 13:16:17 -0700 | [diff] [blame] | 697 | if (xen_initial_domain()) |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 698 | return -ENODEV; |
| 699 | |
Al Viro | ffb78a2 | 2008-11-22 17:38:14 +0000 | [diff] [blame] | 700 | return xenbus_register_frontend(&xenfb_driver); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | static void __exit xenfb_cleanup(void) |
| 704 | { |
Al Viro | ffb78a2 | 2008-11-22 17:38:14 +0000 | [diff] [blame] | 705 | xenbus_unregister_driver(&xenfb_driver); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | module_init(xenfb_init); |
| 709 | module_exit(xenfb_cleanup); |
| 710 | |
Markus Armbruster | 1e892c9 | 2008-05-26 23:31:09 +0100 | [diff] [blame] | 711 | MODULE_DESCRIPTION("Xen virtual framebuffer device frontend"); |
Markus Armbruster | 4ee36dc | 2008-04-02 10:54:07 -0700 | [diff] [blame] | 712 | MODULE_LICENSE("GPL"); |
Markus Armbruster | 1e892c9 | 2008-05-26 23:31:09 +0100 | [diff] [blame] | 713 | MODULE_ALIAS("xen:vfb"); |