GNU Linux-libre 4.14.251-gnu1
[releases.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/overflow.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 (image->width > info->var.xres || image->height > info->var.yres)
432                 return;
433
434         if (rotate == FB_ROTATE_UR) {
435                 for (x = 0;
436                      x < num && image->dx + image->width <= info->var.xres;
437                      x++) {
438                         info->fbops->fb_imageblit(info, image);
439                         image->dx += image->width + 8;
440                 }
441         } else if (rotate == FB_ROTATE_UD) {
442                 u32 dx = image->dx;
443
444                 for (x = 0; x < num && image->dx <= dx; x++) {
445                         info->fbops->fb_imageblit(info, image);
446                         image->dx -= image->width + 8;
447                 }
448         } else if (rotate == FB_ROTATE_CW) {
449                 for (x = 0;
450                      x < num && image->dy + image->height <= info->var.yres;
451                      x++) {
452                         info->fbops->fb_imageblit(info, image);
453                         image->dy += image->height + 8;
454                 }
455         } else if (rotate == FB_ROTATE_CCW) {
456                 u32 dy = image->dy;
457
458                 for (x = 0; x < num && image->dy <= dy; x++) {
459                         info->fbops->fb_imageblit(info, image);
460                         image->dy -= image->height + 8;
461                 }
462         }
463 }
464
465 static int fb_show_logo_line(struct fb_info *info, int rotate,
466                              const struct linux_logo *logo, int y,
467                              unsigned int n)
468 {
469         u32 *palette = NULL, *saved_pseudo_palette = NULL;
470         unsigned char *logo_new = NULL, *logo_rotate = NULL;
471         struct fb_image image;
472
473         /* Return if the frame buffer is not mapped or suspended */
474         if (logo == NULL || info->state != FBINFO_STATE_RUNNING ||
475             info->fbops->owner)
476                 return 0;
477
478         image.depth = 8;
479         image.data = logo->data;
480
481         if (fb_logo.needs_cmapreset)
482                 fb_set_logocmap(info, logo);
483
484         if (fb_logo.needs_truepalette ||
485             fb_logo.needs_directpalette) {
486                 palette = kmalloc(256 * 4, GFP_KERNEL);
487                 if (palette == NULL)
488                         return 0;
489
490                 if (fb_logo.needs_truepalette)
491                         fb_set_logo_truepalette(info, logo, palette);
492                 else
493                         fb_set_logo_directpalette(info, logo, palette);
494
495                 saved_pseudo_palette = info->pseudo_palette;
496                 info->pseudo_palette = palette;
497         }
498
499         if (fb_logo.depth <= 4) {
500                 logo_new = kmalloc(logo->width * logo->height, GFP_KERNEL);
501                 if (logo_new == NULL) {
502                         kfree(palette);
503                         if (saved_pseudo_palette)
504                                 info->pseudo_palette = saved_pseudo_palette;
505                         return 0;
506                 }
507                 image.data = logo_new;
508                 fb_set_logo(info, logo, logo_new, fb_logo.depth);
509         }
510
511         image.dx = 0;
512         image.dy = y;
513         image.width = logo->width;
514         image.height = logo->height;
515
516         if (rotate) {
517                 logo_rotate = kmalloc(logo->width *
518                                       logo->height, GFP_KERNEL);
519                 if (logo_rotate)
520                         fb_rotate_logo(info, logo_rotate, &image, rotate);
521         }
522
523         fb_do_show_logo(info, &image, rotate, n);
524
525         kfree(palette);
526         if (saved_pseudo_palette != NULL)
527                 info->pseudo_palette = saved_pseudo_palette;
528         kfree(logo_new);
529         kfree(logo_rotate);
530         return logo->height;
531 }
532
533
534 #ifdef CONFIG_FB_LOGO_EXTRA
535
536 #define FB_LOGO_EX_NUM_MAX 10
537 static struct logo_data_extra {
538         const struct linux_logo *logo;
539         unsigned int n;
540 } fb_logo_ex[FB_LOGO_EX_NUM_MAX];
541 static unsigned int fb_logo_ex_num;
542
543 void fb_append_extra_logo(const struct linux_logo *logo, unsigned int n)
544 {
545         if (!n || fb_logo_ex_num == FB_LOGO_EX_NUM_MAX)
546                 return;
547
548         fb_logo_ex[fb_logo_ex_num].logo = logo;
549         fb_logo_ex[fb_logo_ex_num].n = n;
550         fb_logo_ex_num++;
551 }
552
553 static int fb_prepare_extra_logos(struct fb_info *info, unsigned int height,
554                                   unsigned int yres)
555 {
556         unsigned int i;
557
558         /* FIXME: logo_ex supports only truecolor fb. */
559         if (info->fix.visual != FB_VISUAL_TRUECOLOR)
560                 fb_logo_ex_num = 0;
561
562         for (i = 0; i < fb_logo_ex_num; i++) {
563                 if (fb_logo_ex[i].logo->type != fb_logo.logo->type) {
564                         fb_logo_ex[i].logo = NULL;
565                         continue;
566                 }
567                 height += fb_logo_ex[i].logo->height;
568                 if (height > yres) {
569                         height -= fb_logo_ex[i].logo->height;
570                         fb_logo_ex_num = i;
571                         break;
572                 }
573         }
574         return height;
575 }
576
577 static int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
578 {
579         unsigned int i;
580
581         for (i = 0; i < fb_logo_ex_num; i++)
582                 y += fb_show_logo_line(info, rotate,
583                                        fb_logo_ex[i].logo, y, fb_logo_ex[i].n);
584
585         return y;
586 }
587
588 #else /* !CONFIG_FB_LOGO_EXTRA */
589
590 static inline int fb_prepare_extra_logos(struct fb_info *info,
591                                          unsigned int height,
592                                          unsigned int yres)
593 {
594         return height;
595 }
596
597 static inline int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
598 {
599         return y;
600 }
601
602 #endif /* CONFIG_FB_LOGO_EXTRA */
603
604
605 int fb_prepare_logo(struct fb_info *info, int rotate)
606 {
607         int depth = fb_get_color_depth(&info->var, &info->fix);
608         unsigned int yres;
609
610         memset(&fb_logo, 0, sizeof(struct logo_data));
611
612         if (info->flags & FBINFO_MISC_TILEBLITTING ||
613             info->fbops->owner)
614                 return 0;
615
616         if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
617                 depth = info->var.blue.length;
618                 if (info->var.red.length < depth)
619                         depth = info->var.red.length;
620                 if (info->var.green.length < depth)
621                         depth = info->var.green.length;
622         }
623
624         if (info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR && depth > 4) {
625                 /* assume console colormap */
626                 depth = 4;
627         }
628
629         /* Return if no suitable logo was found */
630         fb_logo.logo = fb_find_logo(depth);
631
632         if (!fb_logo.logo) {
633                 return 0;
634         }
635
636         if (rotate == FB_ROTATE_UR || rotate == FB_ROTATE_UD)
637                 yres = info->var.yres;
638         else
639                 yres = info->var.xres;
640
641         if (fb_logo.logo->height > yres) {
642                 fb_logo.logo = NULL;
643                 return 0;
644         }
645
646         /* What depth we asked for might be different from what we get */
647         if (fb_logo.logo->type == LINUX_LOGO_CLUT224)
648                 fb_logo.depth = 8;
649         else if (fb_logo.logo->type == LINUX_LOGO_VGA16)
650                 fb_logo.depth = 4;
651         else
652                 fb_logo.depth = 1;
653
654
655         if (fb_logo.depth > 4 && depth > 4) {
656                 switch (info->fix.visual) {
657                 case FB_VISUAL_TRUECOLOR:
658                         fb_logo.needs_truepalette = 1;
659                         break;
660                 case FB_VISUAL_DIRECTCOLOR:
661                         fb_logo.needs_directpalette = 1;
662                         fb_logo.needs_cmapreset = 1;
663                         break;
664                 case FB_VISUAL_PSEUDOCOLOR:
665                         fb_logo.needs_cmapreset = 1;
666                         break;
667                 }
668         }
669
670         return fb_prepare_extra_logos(info, fb_logo.logo->height, yres);
671 }
672
673 int fb_show_logo(struct fb_info *info, int rotate)
674 {
675         int y;
676
677         y = fb_show_logo_line(info, rotate, fb_logo.logo, 0,
678                               num_online_cpus());
679         y = fb_show_extra_logos(info, y, rotate);
680
681         return y;
682 }
683 #else
684 int fb_prepare_logo(struct fb_info *info, int rotate) { return 0; }
685 int fb_show_logo(struct fb_info *info, int rotate) { return 0; }
686 #endif /* CONFIG_LOGO */
687 EXPORT_SYMBOL(fb_prepare_logo);
688 EXPORT_SYMBOL(fb_show_logo);
689
690 static void *fb_seq_start(struct seq_file *m, loff_t *pos)
691 {
692         mutex_lock(&registration_lock);
693         return (*pos < FB_MAX) ? pos : NULL;
694 }
695
696 static void *fb_seq_next(struct seq_file *m, void *v, loff_t *pos)
697 {
698         (*pos)++;
699         return (*pos < FB_MAX) ? pos : NULL;
700 }
701
702 static void fb_seq_stop(struct seq_file *m, void *v)
703 {
704         mutex_unlock(&registration_lock);
705 }
706
707 static int fb_seq_show(struct seq_file *m, void *v)
708 {
709         int i = *(loff_t *)v;
710         struct fb_info *fi = registered_fb[i];
711
712         if (fi)
713                 seq_printf(m, "%d %s\n", fi->node, fi->fix.id);
714         return 0;
715 }
716
717 static const struct seq_operations proc_fb_seq_ops = {
718         .start  = fb_seq_start,
719         .next   = fb_seq_next,
720         .stop   = fb_seq_stop,
721         .show   = fb_seq_show,
722 };
723
724 static int proc_fb_open(struct inode *inode, struct file *file)
725 {
726         return seq_open(file, &proc_fb_seq_ops);
727 }
728
729 static const struct file_operations fb_proc_fops = {
730         .owner          = THIS_MODULE,
731         .open           = proc_fb_open,
732         .read           = seq_read,
733         .llseek         = seq_lseek,
734         .release        = seq_release,
735 };
736
737 /*
738  * We hold a reference to the fb_info in file->private_data,
739  * but if the current registered fb has changed, we don't
740  * actually want to use it.
741  *
742  * So look up the fb_info using the inode minor number,
743  * and just verify it against the reference we have.
744  */
745 static struct fb_info *file_fb_info(struct file *file)
746 {
747         struct inode *inode = file_inode(file);
748         int fbidx = iminor(inode);
749         struct fb_info *info = registered_fb[fbidx];
750
751         if (info != file->private_data)
752                 info = NULL;
753         return info;
754 }
755
756 static ssize_t
757 fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
758 {
759         unsigned long p = *ppos;
760         struct fb_info *info = file_fb_info(file);
761         u8 *buffer, *dst;
762         u8 __iomem *src;
763         int c, cnt = 0, err = 0;
764         unsigned long total_size;
765
766         if (!info || ! info->screen_base)
767                 return -ENODEV;
768
769         if (info->state != FBINFO_STATE_RUNNING)
770                 return -EPERM;
771
772         if (info->fbops->fb_read)
773                 return info->fbops->fb_read(info, buf, count, ppos);
774         
775         total_size = info->screen_size;
776
777         if (total_size == 0)
778                 total_size = info->fix.smem_len;
779
780         if (p >= total_size)
781                 return 0;
782
783         if (count >= total_size)
784                 count = total_size;
785
786         if (count + p > total_size)
787                 count = total_size - p;
788
789         buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
790                          GFP_KERNEL);
791         if (!buffer)
792                 return -ENOMEM;
793
794         src = (u8 __iomem *) (info->screen_base + p);
795
796         if (info->fbops->fb_sync)
797                 info->fbops->fb_sync(info);
798
799         while (count) {
800                 c  = (count > PAGE_SIZE) ? PAGE_SIZE : count;
801                 dst = buffer;
802                 fb_memcpy_fromfb(dst, src, c);
803                 dst += c;
804                 src += c;
805
806                 if (copy_to_user(buf, buffer, c)) {
807                         err = -EFAULT;
808                         break;
809                 }
810                 *ppos += c;
811                 buf += c;
812                 cnt += c;
813                 count -= c;
814         }
815
816         kfree(buffer);
817
818         return (err) ? err : cnt;
819 }
820
821 static ssize_t
822 fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
823 {
824         unsigned long p = *ppos;
825         struct fb_info *info = file_fb_info(file);
826         u8 *buffer, *src;
827         u8 __iomem *dst;
828         int c, cnt = 0, err = 0;
829         unsigned long total_size;
830
831         if (!info || !info->screen_base)
832                 return -ENODEV;
833
834         if (info->state != FBINFO_STATE_RUNNING)
835                 return -EPERM;
836
837         if (info->fbops->fb_write)
838                 return info->fbops->fb_write(info, buf, count, ppos);
839         
840         total_size = info->screen_size;
841
842         if (total_size == 0)
843                 total_size = info->fix.smem_len;
844
845         if (p > total_size)
846                 return -EFBIG;
847
848         if (count > total_size) {
849                 err = -EFBIG;
850                 count = total_size;
851         }
852
853         if (count + p > total_size) {
854                 if (!err)
855                         err = -ENOSPC;
856
857                 count = total_size - p;
858         }
859
860         buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
861                          GFP_KERNEL);
862         if (!buffer)
863                 return -ENOMEM;
864
865         dst = (u8 __iomem *) (info->screen_base + p);
866
867         if (info->fbops->fb_sync)
868                 info->fbops->fb_sync(info);
869
870         while (count) {
871                 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
872                 src = buffer;
873
874                 if (copy_from_user(src, buf, c)) {
875                         err = -EFAULT;
876                         break;
877                 }
878
879                 fb_memcpy_tofb(dst, src, c);
880                 dst += c;
881                 src += c;
882                 *ppos += c;
883                 buf += c;
884                 cnt += c;
885                 count -= c;
886         }
887
888         kfree(buffer);
889
890         return (cnt) ? cnt : err;
891 }
892
893 int
894 fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var)
895 {
896         struct fb_fix_screeninfo *fix = &info->fix;
897         unsigned int yres = info->var.yres;
898         int err = 0;
899
900         if (var->yoffset > 0) {
901                 if (var->vmode & FB_VMODE_YWRAP) {
902                         if (!fix->ywrapstep || (var->yoffset % fix->ywrapstep))
903                                 err = -EINVAL;
904                         else
905                                 yres = 0;
906                 } else if (!fix->ypanstep || (var->yoffset % fix->ypanstep))
907                         err = -EINVAL;
908         }
909
910         if (var->xoffset > 0 && (!fix->xpanstep ||
911                                  (var->xoffset % fix->xpanstep)))
912                 err = -EINVAL;
913
914         if (err || !info->fbops->fb_pan_display ||
915             var->yoffset > info->var.yres_virtual - yres ||
916             var->xoffset > info->var.xres_virtual - info->var.xres)
917                 return -EINVAL;
918
919         if ((err = info->fbops->fb_pan_display(var, info)))
920                 return err;
921         info->var.xoffset = var->xoffset;
922         info->var.yoffset = var->yoffset;
923         if (var->vmode & FB_VMODE_YWRAP)
924                 info->var.vmode |= FB_VMODE_YWRAP;
925         else
926                 info->var.vmode &= ~FB_VMODE_YWRAP;
927         return 0;
928 }
929 EXPORT_SYMBOL(fb_pan_display);
930
931 static int fb_check_caps(struct fb_info *info, struct fb_var_screeninfo *var,
932                          u32 activate)
933 {
934         struct fb_event event;
935         struct fb_blit_caps caps, fbcaps;
936         int err = 0;
937
938         memset(&caps, 0, sizeof(caps));
939         memset(&fbcaps, 0, sizeof(fbcaps));
940         caps.flags = (activate & FB_ACTIVATE_ALL) ? 1 : 0;
941         event.info = info;
942         event.data = &caps;
943         fb_notifier_call_chain(FB_EVENT_GET_REQ, &event);
944         info->fbops->fb_get_caps(info, &fbcaps, var);
945
946         if (((fbcaps.x ^ caps.x) & caps.x) ||
947             ((fbcaps.y ^ caps.y) & caps.y) ||
948             (fbcaps.len < caps.len))
949                 err = -EINVAL;
950
951         return err;
952 }
953
954 int
955 fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var)
956 {
957         int flags = info->flags;
958         int ret = 0;
959
960         if (var->activate & FB_ACTIVATE_INV_MODE) {
961                 struct fb_videomode mode1, mode2;
962
963                 fb_var_to_videomode(&mode1, var);
964                 fb_var_to_videomode(&mode2, &info->var);
965                 /* make sure we don't delete the videomode of current var */
966                 ret = fb_mode_is_equal(&mode1, &mode2);
967
968                 if (!ret) {
969                     struct fb_event event;
970
971                     event.info = info;
972                     event.data = &mode1;
973                     ret = fb_notifier_call_chain(FB_EVENT_MODE_DELETE, &event);
974                 }
975
976                 if (!ret)
977                     fb_delete_videomode(&mode1, &info->modelist);
978
979
980                 ret = (ret) ? -EINVAL : 0;
981                 goto done;
982         }
983
984         if ((var->activate & FB_ACTIVATE_FORCE) ||
985             memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) {
986                 u32 activate = var->activate;
987                 u32 unused;
988
989                 /* When using FOURCC mode, make sure the red, green, blue and
990                  * transp fields are set to 0.
991                  */
992                 if ((info->fix.capabilities & FB_CAP_FOURCC) &&
993                     var->grayscale > 1) {
994                         if (var->red.offset     || var->green.offset    ||
995                             var->blue.offset    || var->transp.offset   ||
996                             var->red.length     || var->green.length    ||
997                             var->blue.length    || var->transp.length   ||
998                             var->red.msb_right  || var->green.msb_right ||
999                             var->blue.msb_right || var->transp.msb_right)
1000                                 return -EINVAL;
1001                 }
1002
1003                 if (!info->fbops->fb_check_var) {
1004                         *var = info->var;
1005                         goto done;
1006                 }
1007
1008                 /* bitfill_aligned() assumes that it's at least 8x8 */
1009                 if (var->xres < 8 || var->yres < 8)
1010                         return -EINVAL;
1011
1012                 /* Too huge resolution causes multiplication overflow. */
1013                 if (check_mul_overflow(var->xres, var->yres, &unused) ||
1014                     check_mul_overflow(var->xres_virtual, var->yres_virtual, &unused))
1015                         return -EINVAL;
1016
1017                 ret = info->fbops->fb_check_var(var, info);
1018
1019                 if (ret)
1020                         goto done;
1021
1022                 if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
1023                         struct fb_var_screeninfo old_var;
1024                         struct fb_videomode mode;
1025
1026                         if (info->fbops->fb_get_caps) {
1027                                 ret = fb_check_caps(info, var, activate);
1028
1029                                 if (ret)
1030                                         goto done;
1031                         }
1032
1033                         old_var = info->var;
1034                         info->var = *var;
1035
1036                         if (info->fbops->fb_set_par) {
1037                                 ret = info->fbops->fb_set_par(info);
1038
1039                                 if (ret) {
1040                                         info->var = old_var;
1041                                         printk(KERN_WARNING "detected "
1042                                                 "fb_set_par error, "
1043                                                 "error code: %d\n", ret);
1044                                         goto done;
1045                                 }
1046                         }
1047
1048                         fb_pan_display(info, &info->var);
1049                         fb_set_cmap(&info->cmap, info);
1050                         fb_var_to_videomode(&mode, &info->var);
1051
1052                         if (info->modelist.prev && info->modelist.next &&
1053                             !list_empty(&info->modelist))
1054                                 ret = fb_add_videomode(&mode, &info->modelist);
1055
1056                         if (!ret && (flags & FBINFO_MISC_USEREVENT)) {
1057                                 struct fb_event event;
1058                                 int evnt = (activate & FB_ACTIVATE_ALL) ?
1059                                         FB_EVENT_MODE_CHANGE_ALL :
1060                                         FB_EVENT_MODE_CHANGE;
1061
1062                                 info->flags &= ~FBINFO_MISC_USEREVENT;
1063                                 event.info = info;
1064                                 event.data = &mode;
1065                                 fb_notifier_call_chain(evnt, &event);
1066                         }
1067                 }
1068         }
1069
1070  done:
1071         return ret;
1072 }
1073 EXPORT_SYMBOL(fb_set_var);
1074
1075 int
1076 fb_blank(struct fb_info *info, int blank)
1077 {       
1078         struct fb_event event;
1079         int ret = -EINVAL, early_ret;
1080
1081         if (blank > FB_BLANK_POWERDOWN)
1082                 blank = FB_BLANK_POWERDOWN;
1083
1084         event.info = info;
1085         event.data = &blank;
1086
1087         early_ret = fb_notifier_call_chain(FB_EARLY_EVENT_BLANK, &event);
1088
1089         if (info->fbops->fb_blank)
1090                 ret = info->fbops->fb_blank(blank, info);
1091
1092         if (!ret)
1093                 fb_notifier_call_chain(FB_EVENT_BLANK, &event);
1094         else {
1095                 /*
1096                  * if fb_blank is failed then revert effects of
1097                  * the early blank event.
1098                  */
1099                 if (!early_ret)
1100                         fb_notifier_call_chain(FB_R_EARLY_EVENT_BLANK, &event);
1101         }
1102
1103         return ret;
1104 }
1105 EXPORT_SYMBOL(fb_blank);
1106
1107 static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
1108                         unsigned long arg)
1109 {
1110         struct fb_ops *fb;
1111         struct fb_var_screeninfo var;
1112         struct fb_fix_screeninfo fix;
1113         struct fb_con2fbmap con2fb;
1114         struct fb_cmap cmap_from;
1115         struct fb_cmap_user cmap;
1116         struct fb_event event;
1117         void __user *argp = (void __user *)arg;
1118         long ret = 0;
1119
1120         switch (cmd) {
1121         case FBIOGET_VSCREENINFO:
1122                 if (!lock_fb_info(info))
1123                         return -ENODEV;
1124                 var = info->var;
1125                 unlock_fb_info(info);
1126
1127                 ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0;
1128                 break;
1129         case FBIOPUT_VSCREENINFO:
1130                 if (copy_from_user(&var, argp, sizeof(var)))
1131                         return -EFAULT;
1132                 console_lock();
1133                 if (!lock_fb_info(info)) {
1134                         console_unlock();
1135                         return -ENODEV;
1136                 }
1137                 info->flags |= FBINFO_MISC_USEREVENT;
1138                 ret = fb_set_var(info, &var);
1139                 info->flags &= ~FBINFO_MISC_USEREVENT;
1140                 unlock_fb_info(info);
1141                 console_unlock();
1142                 if (!ret && copy_to_user(argp, &var, sizeof(var)))
1143                         ret = -EFAULT;
1144                 break;
1145         case FBIOGET_FSCREENINFO:
1146                 if (!lock_fb_info(info))
1147                         return -ENODEV;
1148                 memcpy(&fix, &info->fix, sizeof(fix));
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         unlock_fb_info(info);
1360         return do_fscreeninfo_to_user(&fix, compat_ptr(arg));
1361 }
1362
1363 static long fb_compat_ioctl(struct file *file, unsigned int cmd,
1364                             unsigned long arg)
1365 {
1366         struct fb_info *info = file_fb_info(file);
1367         struct fb_ops *fb;
1368         long ret = -ENOIOCTLCMD;
1369
1370         if (!info)
1371                 return -ENODEV;
1372         fb = info->fbops;
1373         switch(cmd) {
1374         case FBIOGET_VSCREENINFO:
1375         case FBIOPUT_VSCREENINFO:
1376         case FBIOPAN_DISPLAY:
1377         case FBIOGET_CON2FBMAP:
1378         case FBIOPUT_CON2FBMAP:
1379                 arg = (unsigned long) compat_ptr(arg);
1380         case FBIOBLANK:
1381                 ret = do_fb_ioctl(info, cmd, arg);
1382                 break;
1383
1384         case FBIOGET_FSCREENINFO:
1385                 ret = fb_get_fscreeninfo(info, cmd, arg);
1386                 break;
1387
1388         case FBIOGETCMAP:
1389         case FBIOPUTCMAP:
1390                 ret = fb_getput_cmap(info, cmd, arg);
1391                 break;
1392
1393         default:
1394                 if (fb->fb_compat_ioctl)
1395                         ret = fb->fb_compat_ioctl(info, cmd, arg);
1396                 break;
1397         }
1398         return ret;
1399 }
1400 #endif
1401
1402 static int
1403 fb_mmap(struct file *file, struct vm_area_struct * vma)
1404 {
1405         struct fb_info *info = file_fb_info(file);
1406         struct fb_ops *fb;
1407         unsigned long mmio_pgoff;
1408         unsigned long start;
1409         u32 len;
1410
1411         if (!info)
1412                 return -ENODEV;
1413         fb = info->fbops;
1414         if (!fb)
1415                 return -ENODEV;
1416         mutex_lock(&info->mm_lock);
1417         if (fb->fb_mmap) {
1418                 int res;
1419
1420                 /*
1421                  * The framebuffer needs to be accessed decrypted, be sure
1422                  * SME protection is removed ahead of the call
1423                  */
1424                 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
1425                 res = fb->fb_mmap(info, vma);
1426                 mutex_unlock(&info->mm_lock);
1427                 return res;
1428         }
1429
1430         /*
1431          * Ugh. This can be either the frame buffer mapping, or
1432          * if pgoff points past it, the mmio mapping.
1433          */
1434         start = info->fix.smem_start;
1435         len = info->fix.smem_len;
1436         mmio_pgoff = PAGE_ALIGN((start & ~PAGE_MASK) + len) >> PAGE_SHIFT;
1437         if (vma->vm_pgoff >= mmio_pgoff) {
1438                 if (info->var.accel_flags) {
1439                         mutex_unlock(&info->mm_lock);
1440                         return -EINVAL;
1441                 }
1442
1443                 vma->vm_pgoff -= mmio_pgoff;
1444                 start = info->fix.mmio_start;
1445                 len = info->fix.mmio_len;
1446         }
1447         mutex_unlock(&info->mm_lock);
1448
1449         vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
1450         /*
1451          * The framebuffer needs to be accessed decrypted, be sure
1452          * SME protection is removed
1453          */
1454         vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
1455         fb_pgprotect(file, vma, start);
1456
1457         return vm_iomap_memory(vma, start, len);
1458 }
1459
1460 static int
1461 fb_open(struct inode *inode, struct file *file)
1462 __acquires(&info->lock)
1463 __releases(&info->lock)
1464 {
1465         int fbidx = iminor(inode);
1466         struct fb_info *info;
1467         int res = 0;
1468
1469         info = get_fb_info(fbidx);
1470         if (!info) {
1471                 request_module("fb%d", fbidx);
1472                 info = get_fb_info(fbidx);
1473                 if (!info)
1474                         return -ENODEV;
1475         }
1476         if (IS_ERR(info))
1477                 return PTR_ERR(info);
1478
1479         mutex_lock(&info->lock);
1480         if (!try_module_get(info->fbops->owner)) {
1481                 res = -ENODEV;
1482                 goto out;
1483         }
1484         file->private_data = info;
1485         if (info->fbops->fb_open) {
1486                 res = info->fbops->fb_open(info,1);
1487                 if (res)
1488                         module_put(info->fbops->owner);
1489         }
1490 #ifdef CONFIG_FB_DEFERRED_IO
1491         if (info->fbdefio)
1492                 fb_deferred_io_open(info, inode, file);
1493 #endif
1494 out:
1495         mutex_unlock(&info->lock);
1496         if (res)
1497                 put_fb_info(info);
1498         return res;
1499 }
1500
1501 static int 
1502 fb_release(struct inode *inode, struct file *file)
1503 __acquires(&info->lock)
1504 __releases(&info->lock)
1505 {
1506         struct fb_info * const info = file->private_data;
1507
1508         mutex_lock(&info->lock);
1509         if (info->fbops->fb_release)
1510                 info->fbops->fb_release(info,1);
1511         module_put(info->fbops->owner);
1512         mutex_unlock(&info->lock);
1513         put_fb_info(info);
1514         return 0;
1515 }
1516
1517 #if defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && !defined(CONFIG_MMU)
1518 unsigned long get_fb_unmapped_area(struct file *filp,
1519                                    unsigned long addr, unsigned long len,
1520                                    unsigned long pgoff, unsigned long flags)
1521 {
1522         struct fb_info * const info = filp->private_data;
1523         unsigned long fb_size = PAGE_ALIGN(info->fix.smem_len);
1524
1525         if (pgoff > fb_size || len > fb_size - pgoff)
1526                 return -EINVAL;
1527
1528         return (unsigned long)info->screen_base + pgoff;
1529 }
1530 #endif
1531
1532 static const struct file_operations fb_fops = {
1533         .owner =        THIS_MODULE,
1534         .read =         fb_read,
1535         .write =        fb_write,
1536         .unlocked_ioctl = fb_ioctl,
1537 #ifdef CONFIG_COMPAT
1538         .compat_ioctl = fb_compat_ioctl,
1539 #endif
1540         .mmap =         fb_mmap,
1541         .open =         fb_open,
1542         .release =      fb_release,
1543 #if defined(HAVE_ARCH_FB_UNMAPPED_AREA) || \
1544         (defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && \
1545          !defined(CONFIG_MMU))
1546         .get_unmapped_area = get_fb_unmapped_area,
1547 #endif
1548 #ifdef CONFIG_FB_DEFERRED_IO
1549         .fsync =        fb_deferred_io_fsync,
1550 #endif
1551         .llseek =       default_llseek,
1552 };
1553
1554 struct class *fb_class;
1555 EXPORT_SYMBOL(fb_class);
1556
1557 static int fb_check_foreignness(struct fb_info *fi)
1558 {
1559         const bool foreign_endian = fi->flags & FBINFO_FOREIGN_ENDIAN;
1560
1561         fi->flags &= ~FBINFO_FOREIGN_ENDIAN;
1562
1563 #ifdef __BIG_ENDIAN
1564         fi->flags |= foreign_endian ? 0 : FBINFO_BE_MATH;
1565 #else
1566         fi->flags |= foreign_endian ? FBINFO_BE_MATH : 0;
1567 #endif /* __BIG_ENDIAN */
1568
1569         if (fi->flags & FBINFO_BE_MATH && !fb_be_math(fi)) {
1570                 pr_err("%s: enable CONFIG_FB_BIG_ENDIAN to "
1571                        "support this framebuffer\n", fi->fix.id);
1572                 return -ENOSYS;
1573         } else if (!(fi->flags & FBINFO_BE_MATH) && fb_be_math(fi)) {
1574                 pr_err("%s: enable CONFIG_FB_LITTLE_ENDIAN to "
1575                        "support this framebuffer\n", fi->fix.id);
1576                 return -ENOSYS;
1577         }
1578
1579         return 0;
1580 }
1581
1582 static bool apertures_overlap(struct aperture *gen, struct aperture *hw)
1583 {
1584         /* is the generic aperture base the same as the HW one */
1585         if (gen->base == hw->base)
1586                 return true;
1587         /* is the generic aperture base inside the hw base->hw base+size */
1588         if (gen->base > hw->base && gen->base < hw->base + hw->size)
1589                 return true;
1590         return false;
1591 }
1592
1593 static bool fb_do_apertures_overlap(struct apertures_struct *gena,
1594                                     struct apertures_struct *hwa)
1595 {
1596         int i, j;
1597         if (!hwa || !gena)
1598                 return false;
1599
1600         for (i = 0; i < hwa->count; ++i) {
1601                 struct aperture *h = &hwa->ranges[i];
1602                 for (j = 0; j < gena->count; ++j) {
1603                         struct aperture *g = &gena->ranges[j];
1604                         printk(KERN_DEBUG "checking generic (%llx %llx) vs hw (%llx %llx)\n",
1605                                 (unsigned long long)g->base,
1606                                 (unsigned long long)g->size,
1607                                 (unsigned long long)h->base,
1608                                 (unsigned long long)h->size);
1609                         if (apertures_overlap(g, h))
1610                                 return true;
1611                 }
1612         }
1613
1614         return false;
1615 }
1616
1617 static int do_unregister_framebuffer(struct fb_info *fb_info);
1618
1619 #define VGA_FB_PHYS 0xA0000
1620 static int do_remove_conflicting_framebuffers(struct apertures_struct *a,
1621                                               const char *name, bool primary)
1622 {
1623         int i, ret;
1624
1625         /* check all firmware fbs and kick off if the base addr overlaps */
1626         for (i = 0 ; i < FB_MAX; i++) {
1627                 struct apertures_struct *gen_aper;
1628                 if (!registered_fb[i])
1629                         continue;
1630
1631                 if (!(registered_fb[i]->flags & FBINFO_MISC_FIRMWARE))
1632                         continue;
1633
1634                 gen_aper = registered_fb[i]->apertures;
1635                 if (fb_do_apertures_overlap(gen_aper, a) ||
1636                         (primary && gen_aper && gen_aper->count &&
1637                          gen_aper->ranges[0].base == VGA_FB_PHYS)) {
1638
1639                         printk(KERN_INFO "fb: switching to %s from %s\n",
1640                                name, registered_fb[i]->fix.id);
1641                         ret = do_unregister_framebuffer(registered_fb[i]);
1642                         if (ret)
1643                                 return ret;
1644                 }
1645         }
1646
1647         return 0;
1648 }
1649
1650 static bool lockless_register_fb;
1651 module_param_named_unsafe(lockless_register_fb, lockless_register_fb, bool, 0400);
1652 MODULE_PARM_DESC(lockless_register_fb,
1653         "Lockless framebuffer registration for debugging [default=off]");
1654
1655 static int do_register_framebuffer(struct fb_info *fb_info)
1656 {
1657         int i, ret;
1658         struct fb_event event;
1659         struct fb_videomode mode;
1660
1661         if (fb_check_foreignness(fb_info))
1662                 return -ENOSYS;
1663
1664         ret = do_remove_conflicting_framebuffers(fb_info->apertures,
1665                                                  fb_info->fix.id,
1666                                                  fb_is_primary_device(fb_info));
1667         if (ret)
1668                 return ret;
1669
1670         if (num_registered_fb == FB_MAX)
1671                 return -ENXIO;
1672
1673         num_registered_fb++;
1674         for (i = 0 ; i < FB_MAX; i++)
1675                 if (!registered_fb[i])
1676                         break;
1677         fb_info->node = i;
1678         atomic_set(&fb_info->count, 1);
1679         mutex_init(&fb_info->lock);
1680         mutex_init(&fb_info->mm_lock);
1681
1682         fb_info->dev = device_create(fb_class, fb_info->device,
1683                                      MKDEV(FB_MAJOR, i), NULL, "fb%d", i);
1684         if (IS_ERR(fb_info->dev)) {
1685                 /* Not fatal */
1686                 printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->dev));
1687                 fb_info->dev = NULL;
1688         } else
1689                 fb_init_device(fb_info);
1690
1691         if (fb_info->pixmap.addr == NULL) {
1692                 fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
1693                 if (fb_info->pixmap.addr) {
1694                         fb_info->pixmap.size = FBPIXMAPSIZE;
1695                         fb_info->pixmap.buf_align = 1;
1696                         fb_info->pixmap.scan_align = 1;
1697                         fb_info->pixmap.access_align = 32;
1698                         fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
1699                 }
1700         }       
1701         fb_info->pixmap.offset = 0;
1702
1703         if (!fb_info->pixmap.blit_x)
1704                 fb_info->pixmap.blit_x = ~(u32)0;
1705
1706         if (!fb_info->pixmap.blit_y)
1707                 fb_info->pixmap.blit_y = ~(u32)0;
1708
1709         if (!fb_info->modelist.prev || !fb_info->modelist.next)
1710                 INIT_LIST_HEAD(&fb_info->modelist);
1711
1712         if (fb_info->skip_vt_switch)
1713                 pm_vt_switch_required(fb_info->dev, false);
1714         else
1715                 pm_vt_switch_required(fb_info->dev, true);
1716
1717         fb_var_to_videomode(&mode, &fb_info->var);
1718         fb_add_videomode(&mode, &fb_info->modelist);
1719         registered_fb[i] = fb_info;
1720
1721         event.info = fb_info;
1722         if (!lockless_register_fb)
1723                 console_lock();
1724         if (!lock_fb_info(fb_info)) {
1725                 if (!lockless_register_fb)
1726                         console_unlock();
1727                 return -ENODEV;
1728         }
1729
1730         fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event);
1731         unlock_fb_info(fb_info);
1732         if (!lockless_register_fb)
1733                 console_unlock();
1734         return 0;
1735 }
1736
1737 static int unbind_console(struct fb_info *fb_info)
1738 {
1739         struct fb_event event;
1740         int ret;
1741         int i = fb_info->node;
1742
1743         if (i < 0 || i >= FB_MAX || registered_fb[i] != fb_info)
1744                 return -EINVAL;
1745
1746         console_lock();
1747         if (!lock_fb_info(fb_info)) {
1748                 console_unlock();
1749                 return -ENODEV;
1750         }
1751
1752         event.info = fb_info;
1753         ret = fb_notifier_call_chain(FB_EVENT_FB_UNBIND, &event);
1754         unlock_fb_info(fb_info);
1755         console_unlock();
1756
1757         return ret;
1758 }
1759
1760 static int __unlink_framebuffer(struct fb_info *fb_info);
1761
1762 static int do_unregister_framebuffer(struct fb_info *fb_info)
1763 {
1764         struct fb_event event;
1765         int ret;
1766
1767         ret = unbind_console(fb_info);
1768
1769         if (ret)
1770                 return -EINVAL;
1771
1772         pm_vt_switch_unregister(fb_info->dev);
1773
1774         __unlink_framebuffer(fb_info);
1775         if (fb_info->pixmap.addr &&
1776             (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT))
1777                 kfree(fb_info->pixmap.addr);
1778         fb_destroy_modelist(&fb_info->modelist);
1779         registered_fb[fb_info->node] = NULL;
1780         num_registered_fb--;
1781         fb_cleanup_device(fb_info);
1782         event.info = fb_info;
1783         console_lock();
1784         fb_notifier_call_chain(FB_EVENT_FB_UNREGISTERED, &event);
1785         console_unlock();
1786
1787         /* this may free fb info */
1788         put_fb_info(fb_info);
1789         return 0;
1790 }
1791
1792 static int __unlink_framebuffer(struct fb_info *fb_info)
1793 {
1794         int i;
1795
1796         i = fb_info->node;
1797         if (i < 0 || i >= FB_MAX || registered_fb[i] != fb_info)
1798                 return -EINVAL;
1799
1800         if (fb_info->dev) {
1801                 device_destroy(fb_class, MKDEV(FB_MAJOR, i));
1802                 fb_info->dev = NULL;
1803         }
1804
1805         return 0;
1806 }
1807
1808 int unlink_framebuffer(struct fb_info *fb_info)
1809 {
1810         int ret;
1811
1812         ret = __unlink_framebuffer(fb_info);
1813         if (ret)
1814                 return ret;
1815
1816         unbind_console(fb_info);
1817
1818         return 0;
1819 }
1820 EXPORT_SYMBOL(unlink_framebuffer);
1821
1822 int remove_conflicting_framebuffers(struct apertures_struct *a,
1823                                     const char *name, bool primary)
1824 {
1825         int ret;
1826
1827         mutex_lock(&registration_lock);
1828         ret = do_remove_conflicting_framebuffers(a, name, primary);
1829         mutex_unlock(&registration_lock);
1830
1831         return ret;
1832 }
1833 EXPORT_SYMBOL(remove_conflicting_framebuffers);
1834
1835 /**
1836  *      register_framebuffer - registers a frame buffer device
1837  *      @fb_info: frame buffer info structure
1838  *
1839  *      Registers a frame buffer device @fb_info.
1840  *
1841  *      Returns negative errno on error, or zero for success.
1842  *
1843  */
1844 int
1845 register_framebuffer(struct fb_info *fb_info)
1846 {
1847         int ret;
1848
1849         mutex_lock(&registration_lock);
1850         ret = do_register_framebuffer(fb_info);
1851         mutex_unlock(&registration_lock);
1852
1853         return ret;
1854 }
1855 EXPORT_SYMBOL(register_framebuffer);
1856
1857 /**
1858  *      unregister_framebuffer - releases a frame buffer device
1859  *      @fb_info: frame buffer info structure
1860  *
1861  *      Unregisters a frame buffer device @fb_info.
1862  *
1863  *      Returns negative errno on error, or zero for success.
1864  *
1865  *      This function will also notify the framebuffer console
1866  *      to release the driver.
1867  *
1868  *      This is meant to be called within a driver's module_exit()
1869  *      function. If this is called outside module_exit(), ensure
1870  *      that the driver implements fb_open() and fb_release() to
1871  *      check that no processes are using the device.
1872  */
1873 int
1874 unregister_framebuffer(struct fb_info *fb_info)
1875 {
1876         int ret;
1877
1878         mutex_lock(&registration_lock);
1879         ret = do_unregister_framebuffer(fb_info);
1880         mutex_unlock(&registration_lock);
1881
1882         return ret;
1883 }
1884 EXPORT_SYMBOL(unregister_framebuffer);
1885
1886 /**
1887  *      fb_set_suspend - low level driver signals suspend
1888  *      @info: framebuffer affected
1889  *      @state: 0 = resuming, !=0 = suspending
1890  *
1891  *      This is meant to be used by low level drivers to
1892  *      signal suspend/resume to the core & clients.
1893  *      It must be called with the console semaphore held
1894  */
1895 void fb_set_suspend(struct fb_info *info, int state)
1896 {
1897         struct fb_event event;
1898
1899         event.info = info;
1900         if (state) {
1901                 fb_notifier_call_chain(FB_EVENT_SUSPEND, &event);
1902                 info->state = FBINFO_STATE_SUSPENDED;
1903         } else {
1904                 info->state = FBINFO_STATE_RUNNING;
1905                 fb_notifier_call_chain(FB_EVENT_RESUME, &event);
1906         }
1907 }
1908 EXPORT_SYMBOL(fb_set_suspend);
1909
1910 /**
1911  *      fbmem_init - init frame buffer subsystem
1912  *
1913  *      Initialize the frame buffer subsystem.
1914  *
1915  *      NOTE: This function is _only_ to be called by drivers/char/mem.c.
1916  *
1917  */
1918
1919 static int __init
1920 fbmem_init(void)
1921 {
1922         int ret;
1923
1924         if (!proc_create("fb", 0, NULL, &fb_proc_fops))
1925                 return -ENOMEM;
1926
1927         ret = register_chrdev(FB_MAJOR, "fb", &fb_fops);
1928         if (ret) {
1929                 printk("unable to get major %d for fb devs\n", FB_MAJOR);
1930                 goto err_chrdev;
1931         }
1932
1933         fb_class = class_create(THIS_MODULE, "graphics");
1934         if (IS_ERR(fb_class)) {
1935                 ret = PTR_ERR(fb_class);
1936                 pr_warn("Unable to create fb class; errno = %d\n", ret);
1937                 fb_class = NULL;
1938                 goto err_class;
1939         }
1940
1941         fb_console_init();
1942
1943         return 0;
1944
1945 err_class:
1946         unregister_chrdev(FB_MAJOR, "fb");
1947 err_chrdev:
1948         remove_proc_entry("fb", NULL);
1949         return ret;
1950 }
1951
1952 #ifdef MODULE
1953 module_init(fbmem_init);
1954 static void __exit
1955 fbmem_exit(void)
1956 {
1957         fb_console_exit();
1958
1959         remove_proc_entry("fb", NULL);
1960         class_destroy(fb_class);
1961         unregister_chrdev(FB_MAJOR, "fb");
1962 }
1963
1964 module_exit(fbmem_exit);
1965 MODULE_LICENSE("GPL");
1966 MODULE_DESCRIPTION("Framebuffer base");
1967 #else
1968 subsys_initcall(fbmem_init);
1969 #endif
1970
1971 int fb_new_modelist(struct fb_info *info)
1972 {
1973         struct fb_event event;
1974         struct fb_var_screeninfo var = info->var;
1975         struct list_head *pos, *n;
1976         struct fb_modelist *modelist;
1977         struct fb_videomode *m, mode;
1978         int err = 1;
1979
1980         list_for_each_safe(pos, n, &info->modelist) {
1981                 modelist = list_entry(pos, struct fb_modelist, list);
1982                 m = &modelist->mode;
1983                 fb_videomode_to_var(&var, m);
1984                 var.activate = FB_ACTIVATE_TEST;
1985                 err = fb_set_var(info, &var);
1986                 fb_var_to_videomode(&mode, &var);
1987                 if (err || !fb_mode_is_equal(m, &mode)) {
1988                         list_del(pos);
1989                         kfree(pos);
1990                 }
1991         }
1992
1993         err = 1;
1994
1995         if (!list_empty(&info->modelist)) {
1996                 event.info = info;
1997                 err = fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
1998         }
1999
2000         return err;
2001 }
2002
2003 MODULE_LICENSE("GPL");