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