scsi: core: Avoid that system resume triggers a kernel warning
[linux-2.6-microblaze.git] / drivers / video / fbdev / core / fbmem.c
1 /*
2  *  linux/drivers/video/fbmem.c
3  *
4  *  Copyright (C) 1994 Martin Schaller
5  *
6  *      2001 - Documented with DocBook
7  *      - Brad Douglas <brad@neruo.com>
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
11  * for more details.
12  */
13
14 #include <linux/module.h>
15
16 #include <linux/compat.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/major.h>
21 #include <linux/slab.h>
22 #include <linux/mm.h>
23 #include <linux/mman.h>
24 #include <linux/vt.h>
25 #include <linux/init.h>
26 #include <linux/linux_logo.h>
27 #include <linux/proc_fs.h>
28 #include <linux/seq_file.h>
29 #include <linux/console.h>
30 #include <linux/kmod.h>
31 #include <linux/err.h>
32 #include <linux/device.h>
33 #include <linux/efi.h>
34 #include <linux/fb.h>
35 #include <linux/fbcon.h>
36 #include <linux/mem_encrypt.h>
37 #include <linux/pci.h>
38
39 #include <asm/fb.h>
40
41
42     /*
43      *  Frame buffer device initialization and setup routines
44      */
45
46 #define FBPIXMAPSIZE    (1024 * 8)
47
48 static DEFINE_MUTEX(registration_lock);
49
50 struct fb_info *registered_fb[FB_MAX] __read_mostly;
51 EXPORT_SYMBOL(registered_fb);
52
53 int num_registered_fb __read_mostly;
54 EXPORT_SYMBOL(num_registered_fb);
55
56 static struct fb_info *get_fb_info(unsigned int idx)
57 {
58         struct fb_info *fb_info;
59
60         if (idx >= FB_MAX)
61                 return ERR_PTR(-ENODEV);
62
63         mutex_lock(&registration_lock);
64         fb_info = registered_fb[idx];
65         if (fb_info)
66                 atomic_inc(&fb_info->count);
67         mutex_unlock(&registration_lock);
68
69         return fb_info;
70 }
71
72 static void put_fb_info(struct fb_info *fb_info)
73 {
74         if (!atomic_dec_and_test(&fb_info->count))
75                 return;
76         if (fb_info->fbops->fb_destroy)
77                 fb_info->fbops->fb_destroy(fb_info);
78 }
79
80 int lock_fb_info(struct fb_info *info)
81 {
82         mutex_lock(&info->lock);
83         if (!info->fbops) {
84                 mutex_unlock(&info->lock);
85                 return 0;
86         }
87         return 1;
88 }
89 EXPORT_SYMBOL(lock_fb_info);
90
91 /*
92  * Helpers
93  */
94
95 int fb_get_color_depth(struct fb_var_screeninfo *var,
96                        struct fb_fix_screeninfo *fix)
97 {
98         int depth = 0;
99
100         if (fix->visual == FB_VISUAL_MONO01 ||
101             fix->visual == FB_VISUAL_MONO10)
102                 depth = 1;
103         else {
104                 if (var->green.length == var->blue.length &&
105                     var->green.length == var->red.length &&
106                     var->green.offset == var->blue.offset &&
107                     var->green.offset == var->red.offset)
108                         depth = var->green.length;
109                 else
110                         depth = var->green.length + var->red.length +
111                                 var->blue.length;
112         }
113
114         return depth;
115 }
116 EXPORT_SYMBOL(fb_get_color_depth);
117
118 /*
119  * Data padding functions.
120  */
121 void fb_pad_aligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 s_pitch, u32 height)
122 {
123         __fb_pad_aligned_buffer(dst, d_pitch, src, s_pitch, height);
124 }
125 EXPORT_SYMBOL(fb_pad_aligned_buffer);
126
127 void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height,
128                                 u32 shift_high, u32 shift_low, u32 mod)
129 {
130         u8 mask = (u8) (0xfff << shift_high), tmp;
131         int i, j;
132
133         for (i = height; i--; ) {
134                 for (j = 0; j < idx; j++) {
135                         tmp = dst[j];
136                         tmp &= mask;
137                         tmp |= *src >> shift_low;
138                         dst[j] = tmp;
139                         tmp = *src << shift_high;
140                         dst[j+1] = tmp;
141                         src++;
142                 }
143                 tmp = dst[idx];
144                 tmp &= mask;
145                 tmp |= *src >> shift_low;
146                 dst[idx] = tmp;
147                 if (shift_high < mod) {
148                         tmp = *src << shift_high;
149                         dst[idx+1] = tmp;
150                 }
151                 src++;
152                 dst += d_pitch;
153         }
154 }
155 EXPORT_SYMBOL(fb_pad_unaligned_buffer);
156
157 /*
158  * we need to lock this section since fb_cursor
159  * may use fb_imageblit()
160  */
161 char* fb_get_buffer_offset(struct fb_info *info, struct fb_pixmap *buf, u32 size)
162 {
163         u32 align = buf->buf_align - 1, offset;
164         char *addr = buf->addr;
165
166         /* If IO mapped, we need to sync before access, no sharing of
167          * the pixmap is done
168          */
169         if (buf->flags & FB_PIXMAP_IO) {
170                 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
171                         info->fbops->fb_sync(info);
172                 return addr;
173         }
174
175         /* See if we fit in the remaining pixmap space */
176         offset = buf->offset + align;
177         offset &= ~align;
178         if (offset + size > buf->size) {
179                 /* We do not fit. In order to be able to re-use the buffer,
180                  * we must ensure no asynchronous DMA'ing or whatever operation
181                  * is in progress, we sync for that.
182                  */
183                 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
184                         info->fbops->fb_sync(info);
185                 offset = 0;
186         }
187         buf->offset = offset + size;
188         addr += offset;
189
190         return addr;
191 }
192 EXPORT_SYMBOL(fb_get_buffer_offset);
193
194 #ifdef CONFIG_LOGO
195
196 static inline unsigned safe_shift(unsigned d, int n)
197 {
198         return n < 0 ? d >> -n : d << n;
199 }
200
201 static void fb_set_logocmap(struct fb_info *info,
202                                    const struct linux_logo *logo)
203 {
204         struct fb_cmap palette_cmap;
205         u16 palette_green[16];
206         u16 palette_blue[16];
207         u16 palette_red[16];
208         int i, j, n;
209         const unsigned char *clut = logo->clut;
210
211         palette_cmap.start = 0;
212         palette_cmap.len = 16;
213         palette_cmap.red = palette_red;
214         palette_cmap.green = palette_green;
215         palette_cmap.blue = palette_blue;
216         palette_cmap.transp = NULL;
217
218         for (i = 0; i < logo->clutsize; i += n) {
219                 n = logo->clutsize - i;
220                 /* palette_cmap provides space for only 16 colors at once */
221                 if (n > 16)
222                         n = 16;
223                 palette_cmap.start = 32 + i;
224                 palette_cmap.len = n;
225                 for (j = 0; j < n; ++j) {
226                         palette_cmap.red[j] = clut[0] << 8 | clut[0];
227                         palette_cmap.green[j] = clut[1] << 8 | clut[1];
228                         palette_cmap.blue[j] = clut[2] << 8 | clut[2];
229                         clut += 3;
230                 }
231                 fb_set_cmap(&palette_cmap, info);
232         }
233 }
234
235 static void  fb_set_logo_truepalette(struct fb_info *info,
236                                             const struct linux_logo *logo,
237                                             u32 *palette)
238 {
239         static const unsigned char mask[] = { 0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff };
240         unsigned char redmask, greenmask, bluemask;
241         int redshift, greenshift, blueshift;
242         int i;
243         const unsigned char *clut = logo->clut;
244
245         /*
246          * We have to create a temporary palette since console palette is only
247          * 16 colors long.
248          */
249         /* Bug: Doesn't obey msb_right ... (who needs that?) */
250         redmask   = mask[info->var.red.length   < 8 ? info->var.red.length   : 8];
251         greenmask = mask[info->var.green.length < 8 ? info->var.green.length : 8];
252         bluemask  = mask[info->var.blue.length  < 8 ? info->var.blue.length  : 8];
253         redshift   = info->var.red.offset   - (8 - info->var.red.length);
254         greenshift = info->var.green.offset - (8 - info->var.green.length);
255         blueshift  = info->var.blue.offset  - (8 - info->var.blue.length);
256
257         for ( i = 0; i < logo->clutsize; i++) {
258                 palette[i+32] = (safe_shift((clut[0] & redmask), redshift) |
259                                  safe_shift((clut[1] & greenmask), greenshift) |
260                                  safe_shift((clut[2] & bluemask), blueshift));
261                 clut += 3;
262         }
263 }
264
265 static void fb_set_logo_directpalette(struct fb_info *info,
266                                              const struct linux_logo *logo,
267                                              u32 *palette)
268 {
269         int redshift, greenshift, blueshift;
270         int i;
271
272         redshift = info->var.red.offset;
273         greenshift = info->var.green.offset;
274         blueshift = info->var.blue.offset;
275
276         for (i = 32; i < 32 + logo->clutsize; i++)
277                 palette[i] = i << redshift | i << greenshift | i << blueshift;
278 }
279
280 static void fb_set_logo(struct fb_info *info,
281                                const struct linux_logo *logo, u8 *dst,
282                                int depth)
283 {
284         int i, j, k;
285         const u8 *src = logo->data;
286         u8 xor = (info->fix.visual == FB_VISUAL_MONO01) ? 0xff : 0;
287         u8 fg = 1, d;
288
289         switch (fb_get_color_depth(&info->var, &info->fix)) {
290         case 1:
291                 fg = 1;
292                 break;
293         case 2:
294                 fg = 3;
295                 break;
296         default:
297                 fg = 7;
298                 break;
299         }
300
301         if (info->fix.visual == FB_VISUAL_MONO01 ||
302             info->fix.visual == FB_VISUAL_MONO10)
303                 fg = ~((u8) (0xfff << info->var.green.length));
304
305         switch (depth) {
306         case 4:
307                 for (i = 0; i < logo->height; i++)
308                         for (j = 0; j < logo->width; src++) {
309                                 *dst++ = *src >> 4;
310                                 j++;
311                                 if (j < logo->width) {
312                                         *dst++ = *src & 0x0f;
313                                         j++;
314                                 }
315                         }
316                 break;
317         case 1:
318                 for (i = 0; i < logo->height; i++) {
319                         for (j = 0; j < logo->width; src++) {
320                                 d = *src ^ xor;
321                                 for (k = 7; k >= 0 && j < logo->width; k--) {
322                                         *dst++ = ((d >> k) & 1) ? fg : 0;
323                                         j++;
324                                 }
325                         }
326                 }
327                 break;
328         }
329 }
330
331 /*
332  * Three (3) kinds of logo maps exist.  linux_logo_clut224 (>16 colors),
333  * linux_logo_vga16 (16 colors) and linux_logo_mono (2 colors).  Depending on
334  * the visual format and color depth of the framebuffer, the DAC, the
335  * pseudo_palette, and the logo data will be adjusted accordingly.
336  *
337  * Case 1 - linux_logo_clut224:
338  * Color exceeds the number of console colors (16), thus we set the hardware DAC
339  * using fb_set_cmap() appropriately.  The "needs_cmapreset"  flag will be set.
340  *
341  * For visuals that require color info from the pseudo_palette, we also construct
342  * one for temporary use. The "needs_directpalette" or "needs_truepalette" flags
343  * will be set.
344  *
345  * Case 2 - linux_logo_vga16:
346  * The number of colors just matches the console colors, thus there is no need
347  * to set the DAC or the pseudo_palette.  However, the bitmap is packed, ie,
348  * each byte contains color information for two pixels (upper and lower nibble).
349  * To be consistent with fb_imageblit() usage, we therefore separate the two
350  * nibbles into separate bytes. The "depth" flag will be set to 4.
351  *
352  * Case 3 - linux_logo_mono:
353  * This is similar with Case 2.  Each byte contains information for 8 pixels.
354  * We isolate each bit and expand each into a byte. The "depth" flag will
355  * be set to 1.
356  */
357 static struct logo_data {
358         int depth;
359         int needs_directpalette;
360         int needs_truepalette;
361         int needs_cmapreset;
362         const struct linux_logo *logo;
363 } fb_logo __read_mostly;
364
365 static void fb_rotate_logo_ud(const u8 *in, u8 *out, u32 width, u32 height)
366 {
367         u32 size = width * height, i;
368
369         out += size - 1;
370
371         for (i = size; i--; )
372                 *out-- = *in++;
373 }
374
375 static void fb_rotate_logo_cw(const u8 *in, u8 *out, u32 width, u32 height)
376 {
377         int i, j, h = height - 1;
378
379         for (i = 0; i < height; i++)
380                 for (j = 0; j < width; j++)
381                                 out[height * j + h - i] = *in++;
382 }
383
384 static void fb_rotate_logo_ccw(const u8 *in, u8 *out, u32 width, u32 height)
385 {
386         int i, j, w = width - 1;
387
388         for (i = 0; i < height; i++)
389                 for (j = 0; j < width; j++)
390                         out[height * (w - j) + i] = *in++;
391 }
392
393 static void fb_rotate_logo(struct fb_info *info, u8 *dst,
394                            struct fb_image *image, int rotate)
395 {
396         u32 tmp;
397
398         if (rotate == FB_ROTATE_UD) {
399                 fb_rotate_logo_ud(image->data, dst, image->width,
400                                   image->height);
401                 image->dx = info->var.xres - image->width - image->dx;
402                 image->dy = info->var.yres - image->height - image->dy;
403         } else if (rotate == FB_ROTATE_CW) {
404                 fb_rotate_logo_cw(image->data, dst, image->width,
405                                   image->height);
406                 tmp = image->width;
407                 image->width = image->height;
408                 image->height = tmp;
409                 tmp = image->dy;
410                 image->dy = image->dx;
411                 image->dx = info->var.xres - image->width - tmp;
412         } else if (rotate == FB_ROTATE_CCW) {
413                 fb_rotate_logo_ccw(image->data, dst, image->width,
414                                    image->height);
415                 tmp = image->width;
416                 image->width = image->height;
417                 image->height = tmp;
418                 tmp = image->dx;
419                 image->dx = image->dy;
420                 image->dy = info->var.yres - image->height - tmp;
421         }
422
423         image->data = dst;
424 }
425
426 static void fb_do_show_logo(struct fb_info *info, struct fb_image *image,
427                             int rotate, unsigned int num)
428 {
429         unsigned int x;
430
431         if (rotate == FB_ROTATE_UR) {
432                 for (x = 0;
433                      x < num && image->dx + image->width <= info->var.xres;
434                      x++) {
435                         info->fbops->fb_imageblit(info, image);
436                         image->dx += image->width + 8;
437                 }
438         } else if (rotate == FB_ROTATE_UD) {
439                 u32 dx = image->dx;
440
441                 for (x = 0; x < num && image->dx <= dx; x++) {
442                         info->fbops->fb_imageblit(info, image);
443                         image->dx -= image->width + 8;
444                 }
445         } else if (rotate == FB_ROTATE_CW) {
446                 for (x = 0;
447                      x < num && image->dy + image->height <= info->var.yres;
448                      x++) {
449                         info->fbops->fb_imageblit(info, image);
450                         image->dy += image->height + 8;
451                 }
452         } else if (rotate == FB_ROTATE_CCW) {
453                 u32 dy = image->dy;
454
455                 for (x = 0; x < num && image->dy <= dy; x++) {
456                         info->fbops->fb_imageblit(info, image);
457                         image->dy -= image->height + 8;
458                 }
459         }
460 }
461
462 static int fb_show_logo_line(struct fb_info *info, int rotate,
463                              const struct linux_logo *logo, int y,
464                              unsigned int n)
465 {
466         u32 *palette = NULL, *saved_pseudo_palette = NULL;
467         unsigned char *logo_new = NULL, *logo_rotate = NULL;
468         struct fb_image image;
469
470         /* Return if the frame buffer is not mapped or suspended */
471         if (logo == NULL || info->state != FBINFO_STATE_RUNNING ||
472             info->fbops->owner)
473                 return 0;
474
475         image.depth = 8;
476         image.data = logo->data;
477
478         if (fb_logo.needs_cmapreset)
479                 fb_set_logocmap(info, logo);
480
481         if (fb_logo.needs_truepalette ||
482             fb_logo.needs_directpalette) {
483                 palette = kmalloc(256 * 4, GFP_KERNEL);
484                 if (palette == NULL)
485                         return 0;
486
487                 if (fb_logo.needs_truepalette)
488                         fb_set_logo_truepalette(info, logo, palette);
489                 else
490                         fb_set_logo_directpalette(info, logo, palette);
491
492                 saved_pseudo_palette = info->pseudo_palette;
493                 info->pseudo_palette = palette;
494         }
495
496         if (fb_logo.depth <= 4) {
497                 logo_new = kmalloc_array(logo->width, logo->height,
498                                          GFP_KERNEL);
499                 if (logo_new == NULL) {
500                         kfree(palette);
501                         if (saved_pseudo_palette)
502                                 info->pseudo_palette = saved_pseudo_palette;
503                         return 0;
504                 }
505                 image.data = logo_new;
506                 fb_set_logo(info, logo, logo_new, fb_logo.depth);
507         }
508
509 #ifdef CONFIG_FB_LOGO_CENTER
510         {
511                 int xres = info->var.xres;
512                 int yres = info->var.yres;
513
514                 if (rotate == FB_ROTATE_CW || rotate == FB_ROTATE_CCW) {
515                         xres = info->var.yres;
516                         yres = info->var.xres;
517                 }
518
519                 while (n && (n * (logo->width + 8) - 8 > xres))
520                         --n;
521                 image.dx = (xres - n * (logo->width + 8) - 8) / 2;
522                 image.dy = y ?: (yres - logo->height) / 2;
523         }
524 #else
525         image.dx = 0;
526         image.dy = y;
527 #endif
528         image.width = logo->width;
529         image.height = logo->height;
530
531         if (rotate) {
532                 logo_rotate = kmalloc_array(logo->width, logo->height,
533                                             GFP_KERNEL);
534                 if (logo_rotate)
535                         fb_rotate_logo(info, logo_rotate, &image, rotate);
536         }
537
538         fb_do_show_logo(info, &image, rotate, n);
539
540         kfree(palette);
541         if (saved_pseudo_palette != NULL)
542                 info->pseudo_palette = saved_pseudo_palette;
543         kfree(logo_new);
544         kfree(logo_rotate);
545         return image.dy + logo->height;
546 }
547
548
549 #ifdef CONFIG_FB_LOGO_EXTRA
550
551 #define FB_LOGO_EX_NUM_MAX 10
552 static struct logo_data_extra {
553         const struct linux_logo *logo;
554         unsigned int n;
555 } fb_logo_ex[FB_LOGO_EX_NUM_MAX];
556 static unsigned int fb_logo_ex_num;
557
558 void fb_append_extra_logo(const struct linux_logo *logo, unsigned int n)
559 {
560         if (!n || fb_logo_ex_num == FB_LOGO_EX_NUM_MAX)
561                 return;
562
563         fb_logo_ex[fb_logo_ex_num].logo = logo;
564         fb_logo_ex[fb_logo_ex_num].n = n;
565         fb_logo_ex_num++;
566 }
567
568 static int fb_prepare_extra_logos(struct fb_info *info, unsigned int height,
569                                   unsigned int yres)
570 {
571         unsigned int i;
572
573         /* FIXME: logo_ex supports only truecolor fb. */
574         if (info->fix.visual != FB_VISUAL_TRUECOLOR)
575                 fb_logo_ex_num = 0;
576
577         for (i = 0; i < fb_logo_ex_num; i++) {
578                 if (fb_logo_ex[i].logo->type != fb_logo.logo->type) {
579                         fb_logo_ex[i].logo = NULL;
580                         continue;
581                 }
582                 height += fb_logo_ex[i].logo->height;
583                 if (height > yres) {
584                         height -= fb_logo_ex[i].logo->height;
585                         fb_logo_ex_num = i;
586                         break;
587                 }
588         }
589         return height;
590 }
591
592 static int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
593 {
594         unsigned int i;
595
596         for (i = 0; i < fb_logo_ex_num; i++)
597                 y = fb_show_logo_line(info, rotate,
598                                       fb_logo_ex[i].logo, y, fb_logo_ex[i].n);
599
600         return y;
601 }
602
603 #else /* !CONFIG_FB_LOGO_EXTRA */
604
605 static inline int fb_prepare_extra_logos(struct fb_info *info,
606                                          unsigned int height,
607                                          unsigned int yres)
608 {
609         return height;
610 }
611
612 static inline int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
613 {
614         return y;
615 }
616
617 #endif /* CONFIG_FB_LOGO_EXTRA */
618
619
620 int fb_prepare_logo(struct fb_info *info, int rotate)
621 {
622         int depth = fb_get_color_depth(&info->var, &info->fix);
623         unsigned int yres;
624         int height;
625
626         memset(&fb_logo, 0, sizeof(struct logo_data));
627
628         if (info->flags & FBINFO_MISC_TILEBLITTING ||
629             info->fbops->owner)
630                 return 0;
631
632         if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
633                 depth = info->var.blue.length;
634                 if (info->var.red.length < depth)
635                         depth = info->var.red.length;
636                 if (info->var.green.length < depth)
637                         depth = info->var.green.length;
638         }
639
640         if (info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR && depth > 4) {
641                 /* assume console colormap */
642                 depth = 4;
643         }
644
645         /* Return if no suitable logo was found */
646         fb_logo.logo = fb_find_logo(depth);
647
648         if (!fb_logo.logo) {
649                 return 0;
650         }
651
652         if (rotate == FB_ROTATE_UR || rotate == FB_ROTATE_UD)
653                 yres = info->var.yres;
654         else
655                 yres = info->var.xres;
656
657         if (fb_logo.logo->height > yres) {
658                 fb_logo.logo = NULL;
659                 return 0;
660         }
661
662         /* What depth we asked for might be different from what we get */
663         if (fb_logo.logo->type == LINUX_LOGO_CLUT224)
664                 fb_logo.depth = 8;
665         else if (fb_logo.logo->type == LINUX_LOGO_VGA16)
666                 fb_logo.depth = 4;
667         else
668                 fb_logo.depth = 1;
669
670
671         if (fb_logo.depth > 4 && depth > 4) {
672                 switch (info->fix.visual) {
673                 case FB_VISUAL_TRUECOLOR:
674                         fb_logo.needs_truepalette = 1;
675                         break;
676                 case FB_VISUAL_DIRECTCOLOR:
677                         fb_logo.needs_directpalette = 1;
678                         fb_logo.needs_cmapreset = 1;
679                         break;
680                 case FB_VISUAL_PSEUDOCOLOR:
681                         fb_logo.needs_cmapreset = 1;
682                         break;
683                 }
684         }
685
686         height = fb_logo.logo->height;
687 #ifdef CONFIG_FB_LOGO_CENTER
688         height += (yres - fb_logo.logo->height) / 2;
689 #endif
690
691         return fb_prepare_extra_logos(info, height, yres);
692 }
693
694 int fb_show_logo(struct fb_info *info, int rotate)
695 {
696         int y;
697
698         y = fb_show_logo_line(info, rotate, fb_logo.logo, 0,
699                               num_online_cpus());
700         y = fb_show_extra_logos(info, y, rotate);
701
702         return y;
703 }
704 #else
705 int fb_prepare_logo(struct fb_info *info, int rotate) { return 0; }
706 int fb_show_logo(struct fb_info *info, int rotate) { return 0; }
707 #endif /* CONFIG_LOGO */
708 EXPORT_SYMBOL(fb_prepare_logo);
709 EXPORT_SYMBOL(fb_show_logo);
710
711 static void *fb_seq_start(struct seq_file *m, loff_t *pos)
712 {
713         mutex_lock(&registration_lock);
714         return (*pos < FB_MAX) ? pos : NULL;
715 }
716
717 static void *fb_seq_next(struct seq_file *m, void *v, loff_t *pos)
718 {
719         (*pos)++;
720         return (*pos < FB_MAX) ? pos : NULL;
721 }
722
723 static void fb_seq_stop(struct seq_file *m, void *v)
724 {
725         mutex_unlock(&registration_lock);
726 }
727
728 static int fb_seq_show(struct seq_file *m, void *v)
729 {
730         int i = *(loff_t *)v;
731         struct fb_info *fi = registered_fb[i];
732
733         if (fi)
734                 seq_printf(m, "%d %s\n", fi->node, fi->fix.id);
735         return 0;
736 }
737
738 static const struct seq_operations proc_fb_seq_ops = {
739         .start  = fb_seq_start,
740         .next   = fb_seq_next,
741         .stop   = fb_seq_stop,
742         .show   = fb_seq_show,
743 };
744
745 /*
746  * We hold a reference to the fb_info in file->private_data,
747  * but if the current registered fb has changed, we don't
748  * actually want to use it.
749  *
750  * So look up the fb_info using the inode minor number,
751  * and just verify it against the reference we have.
752  */
753 static struct fb_info *file_fb_info(struct file *file)
754 {
755         struct inode *inode = file_inode(file);
756         int fbidx = iminor(inode);
757         struct fb_info *info = registered_fb[fbidx];
758
759         if (info != file->private_data)
760                 info = NULL;
761         return info;
762 }
763
764 static ssize_t
765 fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
766 {
767         unsigned long p = *ppos;
768         struct fb_info *info = file_fb_info(file);
769         u8 *buffer, *dst;
770         u8 __iomem *src;
771         int c, cnt = 0, err = 0;
772         unsigned long total_size;
773
774         if (!info || ! info->screen_base)
775                 return -ENODEV;
776
777         if (info->state != FBINFO_STATE_RUNNING)
778                 return -EPERM;
779
780         if (info->fbops->fb_read)
781                 return info->fbops->fb_read(info, buf, count, ppos);
782         
783         total_size = info->screen_size;
784
785         if (total_size == 0)
786                 total_size = info->fix.smem_len;
787
788         if (p >= total_size)
789                 return 0;
790
791         if (count >= total_size)
792                 count = total_size;
793
794         if (count + p > total_size)
795                 count = total_size - p;
796
797         buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
798                          GFP_KERNEL);
799         if (!buffer)
800                 return -ENOMEM;
801
802         src = (u8 __iomem *) (info->screen_base + p);
803
804         if (info->fbops->fb_sync)
805                 info->fbops->fb_sync(info);
806
807         while (count) {
808                 c  = (count > PAGE_SIZE) ? PAGE_SIZE : count;
809                 dst = buffer;
810                 fb_memcpy_fromfb(dst, src, c);
811                 dst += c;
812                 src += c;
813
814                 if (copy_to_user(buf, buffer, c)) {
815                         err = -EFAULT;
816                         break;
817                 }
818                 *ppos += c;
819                 buf += c;
820                 cnt += c;
821                 count -= c;
822         }
823
824         kfree(buffer);
825
826         return (err) ? err : cnt;
827 }
828
829 static ssize_t
830 fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
831 {
832         unsigned long p = *ppos;
833         struct fb_info *info = file_fb_info(file);
834         u8 *buffer, *src;
835         u8 __iomem *dst;
836         int c, cnt = 0, err = 0;
837         unsigned long total_size;
838
839         if (!info || !info->screen_base)
840                 return -ENODEV;
841
842         if (info->state != FBINFO_STATE_RUNNING)
843                 return -EPERM;
844
845         if (info->fbops->fb_write)
846                 return info->fbops->fb_write(info, buf, count, ppos);
847         
848         total_size = info->screen_size;
849
850         if (total_size == 0)
851                 total_size = info->fix.smem_len;
852
853         if (p > total_size)
854                 return -EFBIG;
855
856         if (count > total_size) {
857                 err = -EFBIG;
858                 count = total_size;
859         }
860
861         if (count + p > total_size) {
862                 if (!err)
863                         err = -ENOSPC;
864
865                 count = total_size - p;
866         }
867
868         buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
869                          GFP_KERNEL);
870         if (!buffer)
871                 return -ENOMEM;
872
873         dst = (u8 __iomem *) (info->screen_base + p);
874
875         if (info->fbops->fb_sync)
876                 info->fbops->fb_sync(info);
877
878         while (count) {
879                 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
880                 src = buffer;
881
882                 if (copy_from_user(src, buf, c)) {
883                         err = -EFAULT;
884                         break;
885                 }
886
887                 fb_memcpy_tofb(dst, src, c);
888                 dst += c;
889                 src += c;
890                 *ppos += c;
891                 buf += c;
892                 cnt += c;
893                 count -= c;
894         }
895
896         kfree(buffer);
897
898         return (cnt) ? cnt : err;
899 }
900
901 int
902 fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var)
903 {
904         struct fb_fix_screeninfo *fix = &info->fix;
905         unsigned int yres = info->var.yres;
906         int err = 0;
907
908         if (var->yoffset > 0) {
909                 if (var->vmode & FB_VMODE_YWRAP) {
910                         if (!fix->ywrapstep || (var->yoffset % fix->ywrapstep))
911                                 err = -EINVAL;
912                         else
913                                 yres = 0;
914                 } else if (!fix->ypanstep || (var->yoffset % fix->ypanstep))
915                         err = -EINVAL;
916         }
917
918         if (var->xoffset > 0 && (!fix->xpanstep ||
919                                  (var->xoffset % fix->xpanstep)))
920                 err = -EINVAL;
921
922         if (err || !info->fbops->fb_pan_display ||
923             var->yoffset > info->var.yres_virtual - yres ||
924             var->xoffset > info->var.xres_virtual - info->var.xres)
925                 return -EINVAL;
926
927         if ((err = info->fbops->fb_pan_display(var, info)))
928                 return err;
929         info->var.xoffset = var->xoffset;
930         info->var.yoffset = var->yoffset;
931         if (var->vmode & FB_VMODE_YWRAP)
932                 info->var.vmode |= FB_VMODE_YWRAP;
933         else
934                 info->var.vmode &= ~FB_VMODE_YWRAP;
935         return 0;
936 }
937 EXPORT_SYMBOL(fb_pan_display);
938
939 static int fb_check_caps(struct fb_info *info, struct fb_var_screeninfo *var,
940                          u32 activate)
941 {
942         struct fb_event event;
943         struct fb_blit_caps caps, fbcaps;
944         int err = 0;
945
946         memset(&caps, 0, sizeof(caps));
947         memset(&fbcaps, 0, sizeof(fbcaps));
948         caps.flags = (activate & FB_ACTIVATE_ALL) ? 1 : 0;
949         event.info = info;
950         event.data = &caps;
951         fb_notifier_call_chain(FB_EVENT_GET_REQ, &event);
952         info->fbops->fb_get_caps(info, &fbcaps, var);
953
954         if (((fbcaps.x ^ caps.x) & caps.x) ||
955             ((fbcaps.y ^ caps.y) & caps.y) ||
956             (fbcaps.len < caps.len))
957                 err = -EINVAL;
958
959         return err;
960 }
961
962 int
963 fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var)
964 {
965         int flags = info->flags;
966         int ret = 0;
967
968         if (var->activate & FB_ACTIVATE_INV_MODE) {
969                 struct fb_videomode mode1, mode2;
970
971                 fb_var_to_videomode(&mode1, var);
972                 fb_var_to_videomode(&mode2, &info->var);
973                 /* make sure we don't delete the videomode of current var */
974                 ret = fb_mode_is_equal(&mode1, &mode2);
975
976                 if (!ret) {
977                     struct fb_event event;
978
979                     event.info = info;
980                     event.data = &mode1;
981                     ret = fb_notifier_call_chain(FB_EVENT_MODE_DELETE, &event);
982                 }
983
984                 if (!ret)
985                     fb_delete_videomode(&mode1, &info->modelist);
986
987
988                 ret = (ret) ? -EINVAL : 0;
989                 goto done;
990         }
991
992         if ((var->activate & FB_ACTIVATE_FORCE) ||
993             memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) {
994                 u32 activate = var->activate;
995
996                 /* When using FOURCC mode, make sure the red, green, blue and
997                  * transp fields are set to 0.
998                  */
999                 if ((info->fix.capabilities & FB_CAP_FOURCC) &&
1000                     var->grayscale > 1) {
1001                         if (var->red.offset     || var->green.offset    ||
1002                             var->blue.offset    || var->transp.offset   ||
1003                             var->red.length     || var->green.length    ||
1004                             var->blue.length    || var->transp.length   ||
1005                             var->red.msb_right  || var->green.msb_right ||
1006                             var->blue.msb_right || var->transp.msb_right)
1007                                 return -EINVAL;
1008                 }
1009
1010                 if (!info->fbops->fb_check_var) {
1011                         *var = info->var;
1012                         goto done;
1013                 }
1014
1015                 ret = info->fbops->fb_check_var(var, info);
1016
1017                 if (ret)
1018                         goto done;
1019
1020                 if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
1021                         struct fb_var_screeninfo old_var;
1022                         struct fb_videomode mode;
1023
1024                         if (info->fbops->fb_get_caps) {
1025                                 ret = fb_check_caps(info, var, activate);
1026
1027                                 if (ret)
1028                                         goto done;
1029                         }
1030
1031                         old_var = info->var;
1032                         info->var = *var;
1033
1034                         if (info->fbops->fb_set_par) {
1035                                 ret = info->fbops->fb_set_par(info);
1036
1037                                 if (ret) {
1038                                         info->var = old_var;
1039                                         printk(KERN_WARNING "detected "
1040                                                 "fb_set_par error, "
1041                                                 "error code: %d\n", ret);
1042                                         goto done;
1043                                 }
1044                         }
1045
1046                         fb_pan_display(info, &info->var);
1047                         fb_set_cmap(&info->cmap, info);
1048                         fb_var_to_videomode(&mode, &info->var);
1049
1050                         if (info->modelist.prev && info->modelist.next &&
1051                             !list_empty(&info->modelist))
1052                                 ret = fb_add_videomode(&mode, &info->modelist);
1053
1054                         if (!ret && (flags & FBINFO_MISC_USEREVENT)) {
1055                                 struct fb_event event;
1056                                 int evnt = (activate & FB_ACTIVATE_ALL) ?
1057                                         FB_EVENT_MODE_CHANGE_ALL :
1058                                         FB_EVENT_MODE_CHANGE;
1059
1060                                 info->flags &= ~FBINFO_MISC_USEREVENT;
1061                                 event.info = info;
1062                                 event.data = &mode;
1063                                 fb_notifier_call_chain(evnt, &event);
1064                         }
1065                 }
1066         }
1067
1068  done:
1069         return ret;
1070 }
1071 EXPORT_SYMBOL(fb_set_var);
1072
1073 int
1074 fb_blank(struct fb_info *info, int blank)
1075 {       
1076         struct fb_event event;
1077         int ret = -EINVAL, early_ret;
1078
1079         if (blank > FB_BLANK_POWERDOWN)
1080                 blank = FB_BLANK_POWERDOWN;
1081
1082         event.info = info;
1083         event.data = &blank;
1084
1085         early_ret = fb_notifier_call_chain(FB_EARLY_EVENT_BLANK, &event);
1086
1087         if (info->fbops->fb_blank)
1088                 ret = info->fbops->fb_blank(blank, info);
1089
1090         if (!ret)
1091                 fb_notifier_call_chain(FB_EVENT_BLANK, &event);
1092         else {
1093                 /*
1094                  * if fb_blank is failed then revert effects of
1095                  * the early blank event.
1096                  */
1097                 if (!early_ret)
1098                         fb_notifier_call_chain(FB_R_EARLY_EVENT_BLANK, &event);
1099         }
1100
1101         return ret;
1102 }
1103 EXPORT_SYMBOL(fb_blank);
1104
1105 static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
1106                         unsigned long arg)
1107 {
1108         struct fb_ops *fb;
1109         struct fb_var_screeninfo var;
1110         struct fb_fix_screeninfo fix;
1111         struct fb_con2fbmap con2fb;
1112         struct fb_cmap cmap_from;
1113         struct fb_cmap_user cmap;
1114         struct fb_event event;
1115         void __user *argp = (void __user *)arg;
1116         long ret = 0;
1117
1118         switch (cmd) {
1119         case FBIOGET_VSCREENINFO:
1120                 if (!lock_fb_info(info))
1121                         return -ENODEV;
1122                 var = info->var;
1123                 unlock_fb_info(info);
1124
1125                 ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0;
1126                 break;
1127         case FBIOPUT_VSCREENINFO:
1128                 if (copy_from_user(&var, argp, sizeof(var)))
1129                         return -EFAULT;
1130                 console_lock();
1131                 if (!lock_fb_info(info)) {
1132                         console_unlock();
1133                         return -ENODEV;
1134                 }
1135                 info->flags |= FBINFO_MISC_USEREVENT;
1136                 ret = fb_set_var(info, &var);
1137                 info->flags &= ~FBINFO_MISC_USEREVENT;
1138                 unlock_fb_info(info);
1139                 console_unlock();
1140                 if (!ret && copy_to_user(argp, &var, sizeof(var)))
1141                         ret = -EFAULT;
1142                 break;
1143         case FBIOGET_FSCREENINFO:
1144                 if (!lock_fb_info(info))
1145                         return -ENODEV;
1146                 fix = info->fix;
1147                 if (info->flags & FBINFO_HIDE_SMEM_START)
1148                         fix.smem_start = 0;
1149                 unlock_fb_info(info);
1150
1151                 ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0;
1152                 break;
1153         case FBIOPUTCMAP:
1154                 if (copy_from_user(&cmap, argp, sizeof(cmap)))
1155                         return -EFAULT;
1156                 ret = fb_set_user_cmap(&cmap, info);
1157                 break;
1158         case FBIOGETCMAP:
1159                 if (copy_from_user(&cmap, argp, sizeof(cmap)))
1160                         return -EFAULT;
1161                 if (!lock_fb_info(info))
1162                         return -ENODEV;
1163                 cmap_from = info->cmap;
1164                 unlock_fb_info(info);
1165                 ret = fb_cmap_to_user(&cmap_from, &cmap);
1166                 break;
1167         case FBIOPAN_DISPLAY:
1168                 if (copy_from_user(&var, argp, sizeof(var)))
1169                         return -EFAULT;
1170                 console_lock();
1171                 if (!lock_fb_info(info)) {
1172                         console_unlock();
1173                         return -ENODEV;
1174                 }
1175                 ret = fb_pan_display(info, &var);
1176                 unlock_fb_info(info);
1177                 console_unlock();
1178                 if (ret == 0 && copy_to_user(argp, &var, sizeof(var)))
1179                         return -EFAULT;
1180                 break;
1181         case FBIO_CURSOR:
1182                 ret = -EINVAL;
1183                 break;
1184         case FBIOGET_CON2FBMAP:
1185                 if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
1186                         return -EFAULT;
1187                 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
1188                         return -EINVAL;
1189                 con2fb.framebuffer = -1;
1190                 event.data = &con2fb;
1191                 if (!lock_fb_info(info))
1192                         return -ENODEV;
1193                 event.info = info;
1194                 fb_notifier_call_chain(FB_EVENT_GET_CONSOLE_MAP, &event);
1195                 unlock_fb_info(info);
1196                 ret = copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0;
1197                 break;
1198         case FBIOPUT_CON2FBMAP:
1199                 if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
1200                         return -EFAULT;
1201                 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
1202                         return -EINVAL;
1203                 if (con2fb.framebuffer >= FB_MAX)
1204                         return -EINVAL;
1205                 if (!registered_fb[con2fb.framebuffer])
1206                         request_module("fb%d", con2fb.framebuffer);
1207                 if (!registered_fb[con2fb.framebuffer]) {
1208                         ret = -EINVAL;
1209                         break;
1210                 }
1211                 event.data = &con2fb;
1212                 console_lock();
1213                 if (!lock_fb_info(info)) {
1214                         console_unlock();
1215                         return -ENODEV;
1216                 }
1217                 event.info = info;
1218                 ret = fb_notifier_call_chain(FB_EVENT_SET_CONSOLE_MAP, &event);
1219                 unlock_fb_info(info);
1220                 console_unlock();
1221                 break;
1222         case FBIOBLANK:
1223                 console_lock();
1224                 if (!lock_fb_info(info)) {
1225                         console_unlock();
1226                         return -ENODEV;
1227                 }
1228                 info->flags |= FBINFO_MISC_USEREVENT;
1229                 ret = fb_blank(info, arg);
1230                 info->flags &= ~FBINFO_MISC_USEREVENT;
1231                 unlock_fb_info(info);
1232                 console_unlock();
1233                 break;
1234         default:
1235                 if (!lock_fb_info(info))
1236                         return -ENODEV;
1237                 fb = info->fbops;
1238                 if (fb->fb_ioctl)
1239                         ret = fb->fb_ioctl(info, cmd, arg);
1240                 else
1241                         ret = -ENOTTY;
1242                 unlock_fb_info(info);
1243         }
1244         return ret;
1245 }
1246
1247 static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1248 {
1249         struct fb_info *info = file_fb_info(file);
1250
1251         if (!info)
1252                 return -ENODEV;
1253         return do_fb_ioctl(info, cmd, arg);
1254 }
1255
1256 #ifdef CONFIG_COMPAT
1257 struct fb_fix_screeninfo32 {
1258         char                    id[16];
1259         compat_caddr_t          smem_start;
1260         u32                     smem_len;
1261         u32                     type;
1262         u32                     type_aux;
1263         u32                     visual;
1264         u16                     xpanstep;
1265         u16                     ypanstep;
1266         u16                     ywrapstep;
1267         u32                     line_length;
1268         compat_caddr_t          mmio_start;
1269         u32                     mmio_len;
1270         u32                     accel;
1271         u16                     reserved[3];
1272 };
1273
1274 struct fb_cmap32 {
1275         u32                     start;
1276         u32                     len;
1277         compat_caddr_t  red;
1278         compat_caddr_t  green;
1279         compat_caddr_t  blue;
1280         compat_caddr_t  transp;
1281 };
1282
1283 static int fb_getput_cmap(struct fb_info *info, unsigned int cmd,
1284                           unsigned long arg)
1285 {
1286         struct fb_cmap_user __user *cmap;
1287         struct fb_cmap32 __user *cmap32;
1288         __u32 data;
1289         int err;
1290
1291         cmap = compat_alloc_user_space(sizeof(*cmap));
1292         cmap32 = compat_ptr(arg);
1293
1294         if (copy_in_user(&cmap->start, &cmap32->start, 2 * sizeof(__u32)))
1295                 return -EFAULT;
1296
1297         if (get_user(data, &cmap32->red) ||
1298             put_user(compat_ptr(data), &cmap->red) ||
1299             get_user(data, &cmap32->green) ||
1300             put_user(compat_ptr(data), &cmap->green) ||
1301             get_user(data, &cmap32->blue) ||
1302             put_user(compat_ptr(data), &cmap->blue) ||
1303             get_user(data, &cmap32->transp) ||
1304             put_user(compat_ptr(data), &cmap->transp))
1305                 return -EFAULT;
1306
1307         err = do_fb_ioctl(info, cmd, (unsigned long) cmap);
1308
1309         if (!err) {
1310                 if (copy_in_user(&cmap32->start,
1311                                  &cmap->start,
1312                                  2 * sizeof(__u32)))
1313                         err = -EFAULT;
1314         }
1315         return err;
1316 }
1317
1318 static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix,
1319                                   struct fb_fix_screeninfo32 __user *fix32)
1320 {
1321         __u32 data;
1322         int err;
1323
1324         err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id));
1325
1326         data = (__u32) (unsigned long) fix->smem_start;
1327         err |= put_user(data, &fix32->smem_start);
1328
1329         err |= put_user(fix->smem_len, &fix32->smem_len);
1330         err |= put_user(fix->type, &fix32->type);
1331         err |= put_user(fix->type_aux, &fix32->type_aux);
1332         err |= put_user(fix->visual, &fix32->visual);
1333         err |= put_user(fix->xpanstep, &fix32->xpanstep);
1334         err |= put_user(fix->ypanstep, &fix32->ypanstep);
1335         err |= put_user(fix->ywrapstep, &fix32->ywrapstep);
1336         err |= put_user(fix->line_length, &fix32->line_length);
1337
1338         data = (__u32) (unsigned long) fix->mmio_start;
1339         err |= put_user(data, &fix32->mmio_start);
1340
1341         err |= put_user(fix->mmio_len, &fix32->mmio_len);
1342         err |= put_user(fix->accel, &fix32->accel);
1343         err |= copy_to_user(fix32->reserved, fix->reserved,
1344                             sizeof(fix->reserved));
1345
1346         if (err)
1347                 return -EFAULT;
1348         return 0;
1349 }
1350
1351 static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd,
1352                               unsigned long arg)
1353 {
1354         struct fb_fix_screeninfo fix;
1355
1356         if (!lock_fb_info(info))
1357                 return -ENODEV;
1358         fix = info->fix;
1359         if (info->flags & FBINFO_HIDE_SMEM_START)
1360                 fix.smem_start = 0;
1361         unlock_fb_info(info);
1362         return do_fscreeninfo_to_user(&fix, compat_ptr(arg));
1363 }
1364
1365 static long fb_compat_ioctl(struct file *file, unsigned int cmd,
1366                             unsigned long arg)
1367 {
1368         struct fb_info *info = file_fb_info(file);
1369         struct fb_ops *fb;
1370         long ret = -ENOIOCTLCMD;
1371
1372         if (!info)
1373                 return -ENODEV;
1374         fb = info->fbops;
1375         switch(cmd) {
1376         case FBIOGET_VSCREENINFO:
1377         case FBIOPUT_VSCREENINFO:
1378         case FBIOPAN_DISPLAY:
1379         case FBIOGET_CON2FBMAP:
1380         case FBIOPUT_CON2FBMAP:
1381                 arg = (unsigned long) compat_ptr(arg);
1382                 /* fall through */
1383         case FBIOBLANK:
1384                 ret = do_fb_ioctl(info, cmd, arg);
1385                 break;
1386
1387         case FBIOGET_FSCREENINFO:
1388                 ret = fb_get_fscreeninfo(info, cmd, arg);
1389                 break;
1390
1391         case FBIOGETCMAP:
1392         case FBIOPUTCMAP:
1393                 ret = fb_getput_cmap(info, cmd, arg);
1394                 break;
1395
1396         default:
1397                 if (fb->fb_compat_ioctl)
1398                         ret = fb->fb_compat_ioctl(info, cmd, arg);
1399                 break;
1400         }
1401         return ret;
1402 }
1403 #endif
1404
1405 static int
1406 fb_mmap(struct file *file, struct vm_area_struct * vma)
1407 {
1408         struct fb_info *info = file_fb_info(file);
1409         struct fb_ops *fb;
1410         unsigned long mmio_pgoff;
1411         unsigned long start;
1412         u32 len;
1413
1414         if (!info)
1415                 return -ENODEV;
1416         fb = info->fbops;
1417         if (!fb)
1418                 return -ENODEV;
1419         mutex_lock(&info->mm_lock);
1420         if (fb->fb_mmap) {
1421                 int res;
1422
1423                 /*
1424                  * The framebuffer needs to be accessed decrypted, be sure
1425                  * SME protection is removed ahead of the call
1426                  */
1427                 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
1428                 res = fb->fb_mmap(info, vma);
1429                 mutex_unlock(&info->mm_lock);
1430                 return res;
1431         }
1432
1433         /*
1434          * Ugh. This can be either the frame buffer mapping, or
1435          * if pgoff points past it, the mmio mapping.
1436          */
1437         start = info->fix.smem_start;
1438         len = info->fix.smem_len;
1439         mmio_pgoff = PAGE_ALIGN((start & ~PAGE_MASK) + len) >> PAGE_SHIFT;
1440         if (vma->vm_pgoff >= mmio_pgoff) {
1441                 if (info->var.accel_flags) {
1442                         mutex_unlock(&info->mm_lock);
1443                         return -EINVAL;
1444                 }
1445
1446                 vma->vm_pgoff -= mmio_pgoff;
1447                 start = info->fix.mmio_start;
1448                 len = info->fix.mmio_len;
1449         }
1450         mutex_unlock(&info->mm_lock);
1451
1452         vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
1453         /*
1454          * The framebuffer needs to be accessed decrypted, be sure
1455          * SME protection is removed
1456          */
1457         vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
1458         fb_pgprotect(file, vma, start);
1459
1460         return vm_iomap_memory(vma, start, len);
1461 }
1462
1463 static int
1464 fb_open(struct inode *inode, struct file *file)
1465 __acquires(&info->lock)
1466 __releases(&info->lock)
1467 {
1468         int fbidx = iminor(inode);
1469         struct fb_info *info;
1470         int res = 0;
1471
1472         info = get_fb_info(fbidx);
1473         if (!info) {
1474                 request_module("fb%d", fbidx);
1475                 info = get_fb_info(fbidx);
1476                 if (!info)
1477                         return -ENODEV;
1478         }
1479         if (IS_ERR(info))
1480                 return PTR_ERR(info);
1481
1482         mutex_lock(&info->lock);
1483         if (!try_module_get(info->fbops->owner)) {
1484                 res = -ENODEV;
1485                 goto out;
1486         }
1487         file->private_data = info;
1488         if (info->fbops->fb_open) {
1489                 res = info->fbops->fb_open(info,1);
1490                 if (res)
1491                         module_put(info->fbops->owner);
1492         }
1493 #ifdef CONFIG_FB_DEFERRED_IO
1494         if (info->fbdefio)
1495                 fb_deferred_io_open(info, inode, file);
1496 #endif
1497 out:
1498         mutex_unlock(&info->lock);
1499         if (res)
1500                 put_fb_info(info);
1501         return res;
1502 }
1503
1504 static int 
1505 fb_release(struct inode *inode, struct file *file)
1506 __acquires(&info->lock)
1507 __releases(&info->lock)
1508 {
1509         struct fb_info * const info = file->private_data;
1510
1511         mutex_lock(&info->lock);
1512         if (info->fbops->fb_release)
1513                 info->fbops->fb_release(info,1);
1514         module_put(info->fbops->owner);
1515         mutex_unlock(&info->lock);
1516         put_fb_info(info);
1517         return 0;
1518 }
1519
1520 #if defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && !defined(CONFIG_MMU)
1521 unsigned long get_fb_unmapped_area(struct file *filp,
1522                                    unsigned long addr, unsigned long len,
1523                                    unsigned long pgoff, unsigned long flags)
1524 {
1525         struct fb_info * const info = filp->private_data;
1526         unsigned long fb_size = PAGE_ALIGN(info->fix.smem_len);
1527
1528         if (pgoff > fb_size || len > fb_size - pgoff)
1529                 return -EINVAL;
1530
1531         return (unsigned long)info->screen_base + pgoff;
1532 }
1533 #endif
1534
1535 static const struct file_operations fb_fops = {
1536         .owner =        THIS_MODULE,
1537         .read =         fb_read,
1538         .write =        fb_write,
1539         .unlocked_ioctl = fb_ioctl,
1540 #ifdef CONFIG_COMPAT
1541         .compat_ioctl = fb_compat_ioctl,
1542 #endif
1543         .mmap =         fb_mmap,
1544         .open =         fb_open,
1545         .release =      fb_release,
1546 #if defined(HAVE_ARCH_FB_UNMAPPED_AREA) || \
1547         (defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && \
1548          !defined(CONFIG_MMU))
1549         .get_unmapped_area = get_fb_unmapped_area,
1550 #endif
1551 #ifdef CONFIG_FB_DEFERRED_IO
1552         .fsync =        fb_deferred_io_fsync,
1553 #endif
1554         .llseek =       default_llseek,
1555 };
1556
1557 struct class *fb_class;
1558 EXPORT_SYMBOL(fb_class);
1559
1560 static int fb_check_foreignness(struct fb_info *fi)
1561 {
1562         const bool foreign_endian = fi->flags & FBINFO_FOREIGN_ENDIAN;
1563
1564         fi->flags &= ~FBINFO_FOREIGN_ENDIAN;
1565
1566 #ifdef __BIG_ENDIAN
1567         fi->flags |= foreign_endian ? 0 : FBINFO_BE_MATH;
1568 #else
1569         fi->flags |= foreign_endian ? FBINFO_BE_MATH : 0;
1570 #endif /* __BIG_ENDIAN */
1571
1572         if (fi->flags & FBINFO_BE_MATH && !fb_be_math(fi)) {
1573                 pr_err("%s: enable CONFIG_FB_BIG_ENDIAN to "
1574                        "support this framebuffer\n", fi->fix.id);
1575                 return -ENOSYS;
1576         } else if (!(fi->flags & FBINFO_BE_MATH) && fb_be_math(fi)) {
1577                 pr_err("%s: enable CONFIG_FB_LITTLE_ENDIAN to "
1578                        "support this framebuffer\n", fi->fix.id);
1579                 return -ENOSYS;
1580         }
1581
1582         return 0;
1583 }
1584
1585 static bool apertures_overlap(struct aperture *gen, struct aperture *hw)
1586 {
1587         /* is the generic aperture base the same as the HW one */
1588         if (gen->base == hw->base)
1589                 return true;
1590         /* is the generic aperture base inside the hw base->hw base+size */
1591         if (gen->base > hw->base && gen->base < hw->base + hw->size)
1592                 return true;
1593         return false;
1594 }
1595
1596 static bool fb_do_apertures_overlap(struct apertures_struct *gena,
1597                                     struct apertures_struct *hwa)
1598 {
1599         int i, j;
1600         if (!hwa || !gena)
1601                 return false;
1602
1603         for (i = 0; i < hwa->count; ++i) {
1604                 struct aperture *h = &hwa->ranges[i];
1605                 for (j = 0; j < gena->count; ++j) {
1606                         struct aperture *g = &gena->ranges[j];
1607                         printk(KERN_DEBUG "checking generic (%llx %llx) vs hw (%llx %llx)\n",
1608                                 (unsigned long long)g->base,
1609                                 (unsigned long long)g->size,
1610                                 (unsigned long long)h->base,
1611                                 (unsigned long long)h->size);
1612                         if (apertures_overlap(g, h))
1613                                 return true;
1614                 }
1615         }
1616
1617         return false;
1618 }
1619
1620 static int do_unregister_framebuffer(struct fb_info *fb_info);
1621
1622 #define VGA_FB_PHYS 0xA0000
1623 static int do_remove_conflicting_framebuffers(struct apertures_struct *a,
1624                                               const char *name, bool primary)
1625 {
1626         int i, ret;
1627
1628         /* check all firmware fbs and kick off if the base addr overlaps */
1629         for_each_registered_fb(i) {
1630                 struct apertures_struct *gen_aper;
1631
1632                 if (!(registered_fb[i]->flags & FBINFO_MISC_FIRMWARE))
1633                         continue;
1634
1635                 gen_aper = registered_fb[i]->apertures;
1636                 if (fb_do_apertures_overlap(gen_aper, a) ||
1637                         (primary && gen_aper && gen_aper->count &&
1638                          gen_aper->ranges[0].base == VGA_FB_PHYS)) {
1639
1640                         printk(KERN_INFO "fb%d: switching to %s from %s\n",
1641                                i, name, registered_fb[i]->fix.id);
1642                         ret = do_unregister_framebuffer(registered_fb[i]);
1643                         if (ret)
1644                                 return ret;
1645                 }
1646         }
1647
1648         return 0;
1649 }
1650
1651 static bool lockless_register_fb;
1652 module_param_named_unsafe(lockless_register_fb, lockless_register_fb, bool, 0400);
1653 MODULE_PARM_DESC(lockless_register_fb,
1654         "Lockless framebuffer registration for debugging [default=off]");
1655
1656 static int do_register_framebuffer(struct fb_info *fb_info)
1657 {
1658         int i, ret;
1659         struct fb_event event;
1660         struct fb_videomode mode;
1661
1662         if (fb_check_foreignness(fb_info))
1663                 return -ENOSYS;
1664
1665         ret = do_remove_conflicting_framebuffers(fb_info->apertures,
1666                                                  fb_info->fix.id,
1667                                                  fb_is_primary_device(fb_info));
1668         if (ret)
1669                 return ret;
1670
1671         if (num_registered_fb == FB_MAX)
1672                 return -ENXIO;
1673
1674         num_registered_fb++;
1675         for (i = 0 ; i < FB_MAX; i++)
1676                 if (!registered_fb[i])
1677                         break;
1678         fb_info->node = i;
1679         atomic_set(&fb_info->count, 1);
1680         mutex_init(&fb_info->lock);
1681         mutex_init(&fb_info->mm_lock);
1682
1683         fb_info->dev = device_create(fb_class, fb_info->device,
1684                                      MKDEV(FB_MAJOR, i), NULL, "fb%d", i);
1685         if (IS_ERR(fb_info->dev)) {
1686                 /* Not fatal */
1687                 printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->dev));
1688                 fb_info->dev = NULL;
1689         } else
1690                 fb_init_device(fb_info);
1691
1692         if (fb_info->pixmap.addr == NULL) {
1693                 fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
1694                 if (fb_info->pixmap.addr) {
1695                         fb_info->pixmap.size = FBPIXMAPSIZE;
1696                         fb_info->pixmap.buf_align = 1;
1697                         fb_info->pixmap.scan_align = 1;
1698                         fb_info->pixmap.access_align = 32;
1699                         fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
1700                 }
1701         }       
1702         fb_info->pixmap.offset = 0;
1703
1704         if (!fb_info->pixmap.blit_x)
1705                 fb_info->pixmap.blit_x = ~(u32)0;
1706
1707         if (!fb_info->pixmap.blit_y)
1708                 fb_info->pixmap.blit_y = ~(u32)0;
1709
1710         if (!fb_info->modelist.prev || !fb_info->modelist.next)
1711                 INIT_LIST_HEAD(&fb_info->modelist);
1712
1713         if (fb_info->skip_vt_switch)
1714                 pm_vt_switch_required(fb_info->dev, false);
1715         else
1716                 pm_vt_switch_required(fb_info->dev, true);
1717
1718         fb_var_to_videomode(&mode, &fb_info->var);
1719         fb_add_videomode(&mode, &fb_info->modelist);
1720         registered_fb[i] = fb_info;
1721
1722         event.info = fb_info;
1723         if (!lockless_register_fb)
1724                 console_lock();
1725         else
1726                 atomic_inc(&ignore_console_lock_warning);
1727         if (!lock_fb_info(fb_info)) {
1728                 ret = -ENODEV;
1729                 goto unlock_console;
1730         }
1731         ret = 0;
1732
1733         fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event);
1734         unlock_fb_info(fb_info);
1735 unlock_console:
1736         if (!lockless_register_fb)
1737                 console_unlock();
1738         else
1739                 atomic_dec(&ignore_console_lock_warning);
1740         return ret;
1741 }
1742
1743 static int unbind_console(struct fb_info *fb_info)
1744 {
1745         struct fb_event event;
1746         int ret;
1747         int i = fb_info->node;
1748
1749         if (i < 0 || i >= FB_MAX || registered_fb[i] != fb_info)
1750                 return -EINVAL;
1751
1752         console_lock();
1753         if (!lock_fb_info(fb_info)) {
1754                 console_unlock();
1755                 return -ENODEV;
1756         }
1757
1758         event.info = fb_info;
1759         ret = fb_notifier_call_chain(FB_EVENT_FB_UNBIND, &event);
1760         unlock_fb_info(fb_info);
1761         console_unlock();
1762
1763         return ret;
1764 }
1765
1766 static int __unlink_framebuffer(struct fb_info *fb_info);
1767
1768 static int do_unregister_framebuffer(struct fb_info *fb_info)
1769 {
1770         struct fb_event event;
1771         int ret;
1772
1773         ret = unbind_console(fb_info);
1774
1775         if (ret)
1776                 return -EINVAL;
1777
1778         pm_vt_switch_unregister(fb_info->dev);
1779
1780         __unlink_framebuffer(fb_info);
1781         if (fb_info->pixmap.addr &&
1782             (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT))
1783                 kfree(fb_info->pixmap.addr);
1784         fb_destroy_modelist(&fb_info->modelist);
1785         registered_fb[fb_info->node] = NULL;
1786         num_registered_fb--;
1787         fb_cleanup_device(fb_info);
1788         event.info = fb_info;
1789         console_lock();
1790         fb_notifier_call_chain(FB_EVENT_FB_UNREGISTERED, &event);
1791         console_unlock();
1792
1793         /* this may free fb info */
1794         put_fb_info(fb_info);
1795         return 0;
1796 }
1797
1798 static int __unlink_framebuffer(struct fb_info *fb_info)
1799 {
1800         int i;
1801
1802         i = fb_info->node;
1803         if (i < 0 || i >= FB_MAX || registered_fb[i] != fb_info)
1804                 return -EINVAL;
1805
1806         if (fb_info->dev) {
1807                 device_destroy(fb_class, MKDEV(FB_MAJOR, i));
1808                 fb_info->dev = NULL;
1809         }
1810
1811         return 0;
1812 }
1813
1814 int unlink_framebuffer(struct fb_info *fb_info)
1815 {
1816         int ret;
1817
1818         ret = __unlink_framebuffer(fb_info);
1819         if (ret)
1820                 return ret;
1821
1822         unbind_console(fb_info);
1823
1824         return 0;
1825 }
1826 EXPORT_SYMBOL(unlink_framebuffer);
1827
1828 /**
1829  * remove_conflicting_framebuffers - remove firmware-configured framebuffers
1830  * @a: memory range, users of which are to be removed
1831  * @name: requesting driver name
1832  * @primary: also kick vga16fb if present
1833  *
1834  * This function removes framebuffer devices (initialized by firmware/bootloader)
1835  * which use memory range described by @a. If @a is NULL all such devices are
1836  * removed.
1837  */
1838 int remove_conflicting_framebuffers(struct apertures_struct *a,
1839                                     const char *name, bool primary)
1840 {
1841         int ret;
1842         bool do_free = false;
1843
1844         if (!a) {
1845                 a = alloc_apertures(1);
1846                 if (!a)
1847                         return -ENOMEM;
1848
1849                 a->ranges[0].base = 0;
1850                 a->ranges[0].size = ~0;
1851                 do_free = true;
1852         }
1853
1854         mutex_lock(&registration_lock);
1855         ret = do_remove_conflicting_framebuffers(a, name, primary);
1856         mutex_unlock(&registration_lock);
1857
1858         if (do_free)
1859                 kfree(a);
1860
1861         return ret;
1862 }
1863 EXPORT_SYMBOL(remove_conflicting_framebuffers);
1864
1865 /**
1866  * remove_conflicting_pci_framebuffers - remove firmware-configured framebuffers for PCI devices
1867  * @pdev: PCI device
1868  * @res_id: index of PCI BAR configuring framebuffer memory
1869  * @name: requesting driver name
1870  *
1871  * This function removes framebuffer devices (eg. initialized by firmware)
1872  * using memory range configured for @pdev's BAR @res_id.
1873  *
1874  * The function assumes that PCI device with shadowed ROM drives a primary
1875  * display and so kicks out vga16fb.
1876  */
1877 int remove_conflicting_pci_framebuffers(struct pci_dev *pdev, int res_id, const char *name)
1878 {
1879         struct apertures_struct *ap;
1880         bool primary = false;
1881         int err;
1882
1883         ap = alloc_apertures(1);
1884         if (!ap)
1885                 return -ENOMEM;
1886
1887         ap->ranges[0].base = pci_resource_start(pdev, res_id);
1888         ap->ranges[0].size = pci_resource_len(pdev, res_id);
1889 #ifdef CONFIG_X86
1890         primary = pdev->resource[PCI_ROM_RESOURCE].flags &
1891                                         IORESOURCE_ROM_SHADOW;
1892 #endif
1893         err = remove_conflicting_framebuffers(ap, name, primary);
1894         kfree(ap);
1895         return err;
1896 }
1897 EXPORT_SYMBOL(remove_conflicting_pci_framebuffers);
1898
1899 /**
1900  *      register_framebuffer - registers a frame buffer device
1901  *      @fb_info: frame buffer info structure
1902  *
1903  *      Registers a frame buffer device @fb_info.
1904  *
1905  *      Returns negative errno on error, or zero for success.
1906  *
1907  */
1908 int
1909 register_framebuffer(struct fb_info *fb_info)
1910 {
1911         int ret;
1912
1913         mutex_lock(&registration_lock);
1914         ret = do_register_framebuffer(fb_info);
1915         mutex_unlock(&registration_lock);
1916
1917         return ret;
1918 }
1919 EXPORT_SYMBOL(register_framebuffer);
1920
1921 /**
1922  *      unregister_framebuffer - releases a frame buffer device
1923  *      @fb_info: frame buffer info structure
1924  *
1925  *      Unregisters a frame buffer device @fb_info.
1926  *
1927  *      Returns negative errno on error, or zero for success.
1928  *
1929  *      This function will also notify the framebuffer console
1930  *      to release the driver.
1931  *
1932  *      This is meant to be called within a driver's module_exit()
1933  *      function. If this is called outside module_exit(), ensure
1934  *      that the driver implements fb_open() and fb_release() to
1935  *      check that no processes are using the device.
1936  */
1937 int
1938 unregister_framebuffer(struct fb_info *fb_info)
1939 {
1940         int ret;
1941
1942         mutex_lock(&registration_lock);
1943         ret = do_unregister_framebuffer(fb_info);
1944         mutex_unlock(&registration_lock);
1945
1946         return ret;
1947 }
1948 EXPORT_SYMBOL(unregister_framebuffer);
1949
1950 /**
1951  *      fb_set_suspend - low level driver signals suspend
1952  *      @info: framebuffer affected
1953  *      @state: 0 = resuming, !=0 = suspending
1954  *
1955  *      This is meant to be used by low level drivers to
1956  *      signal suspend/resume to the core & clients.
1957  *      It must be called with the console semaphore held
1958  */
1959 void fb_set_suspend(struct fb_info *info, int state)
1960 {
1961         struct fb_event event;
1962
1963         event.info = info;
1964         if (state) {
1965                 fb_notifier_call_chain(FB_EVENT_SUSPEND, &event);
1966                 info->state = FBINFO_STATE_SUSPENDED;
1967         } else {
1968                 info->state = FBINFO_STATE_RUNNING;
1969                 fb_notifier_call_chain(FB_EVENT_RESUME, &event);
1970         }
1971 }
1972 EXPORT_SYMBOL(fb_set_suspend);
1973
1974 /**
1975  *      fbmem_init - init frame buffer subsystem
1976  *
1977  *      Initialize the frame buffer subsystem.
1978  *
1979  *      NOTE: This function is _only_ to be called by drivers/char/mem.c.
1980  *
1981  */
1982
1983 static int __init
1984 fbmem_init(void)
1985 {
1986         int ret;
1987
1988         if (!proc_create_seq("fb", 0, NULL, &proc_fb_seq_ops))
1989                 return -ENOMEM;
1990
1991         ret = register_chrdev(FB_MAJOR, "fb", &fb_fops);
1992         if (ret) {
1993                 printk("unable to get major %d for fb devs\n", FB_MAJOR);
1994                 goto err_chrdev;
1995         }
1996
1997         fb_class = class_create(THIS_MODULE, "graphics");
1998         if (IS_ERR(fb_class)) {
1999                 ret = PTR_ERR(fb_class);
2000                 pr_warn("Unable to create fb class; errno = %d\n", ret);
2001                 fb_class = NULL;
2002                 goto err_class;
2003         }
2004
2005         fb_console_init();
2006
2007         return 0;
2008
2009 err_class:
2010         unregister_chrdev(FB_MAJOR, "fb");
2011 err_chrdev:
2012         remove_proc_entry("fb", NULL);
2013         return ret;
2014 }
2015
2016 #ifdef MODULE
2017 module_init(fbmem_init);
2018 static void __exit
2019 fbmem_exit(void)
2020 {
2021         fb_console_exit();
2022
2023         remove_proc_entry("fb", NULL);
2024         class_destroy(fb_class);
2025         unregister_chrdev(FB_MAJOR, "fb");
2026 }
2027
2028 module_exit(fbmem_exit);
2029 MODULE_LICENSE("GPL");
2030 MODULE_DESCRIPTION("Framebuffer base");
2031 #else
2032 subsys_initcall(fbmem_init);
2033 #endif
2034
2035 int fb_new_modelist(struct fb_info *info)
2036 {
2037         struct fb_event event;
2038         struct fb_var_screeninfo var = info->var;
2039         struct list_head *pos, *n;
2040         struct fb_modelist *modelist;
2041         struct fb_videomode *m, mode;
2042         int err = 1;
2043
2044         list_for_each_safe(pos, n, &info->modelist) {
2045                 modelist = list_entry(pos, struct fb_modelist, list);
2046                 m = &modelist->mode;
2047                 fb_videomode_to_var(&var, m);
2048                 var.activate = FB_ACTIVATE_TEST;
2049                 err = fb_set_var(info, &var);
2050                 fb_var_to_videomode(&mode, &var);
2051                 if (err || !fb_mode_is_equal(m, &mode)) {
2052                         list_del(pos);
2053                         kfree(pos);
2054                 }
2055         }
2056
2057         err = 1;
2058
2059         if (!list_empty(&info->modelist)) {
2060                 event.info = info;
2061                 err = fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
2062         }
2063
2064         return err;
2065 }
2066
2067 MODULE_LICENSE("GPL");