]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - drivers/video/console/vgacon.c
[PATCH] vgacon: make VGA_MAP_MEM take size, remove extra use
[mirror_ubuntu-bionic-kernel.git] / drivers / video / console / vgacon.c
index 274f90543e323b7014e6c61d25a555bd6aa180f6..e64d42e2449e4dd5ac104042000d0fe005ce09ca 100644 (file)
@@ -56,6 +56,8 @@
 static DEFINE_SPINLOCK(vga_lock);
 static int cursor_size_lastfrom;
 static int cursor_size_lastto;
+static u32 vgacon_xres;
+static u32 vgacon_yres;
 static struct vgastate state;
 
 #define BLANK 0x0020
@@ -69,7 +71,7 @@ static struct vgastate state;
  * appear.
  */
 #undef TRIDENT_GLITCH
-
+#define VGA_FONTWIDTH       8   /* VGA does not support fontwidths != 8 */
 /*
  *  Interface used by the world
  */
@@ -91,7 +93,6 @@ static u8 vgacon_build_attr(struct vc_data *c, u8 color, u8 intensity,
 static void vgacon_invert_region(struct vc_data *c, u16 * p, int count);
 static unsigned long vgacon_uni_pagedir[2];
 
-
 /* Description of the hardware situation */
 static unsigned long   vga_vram_base;          /* Base of video memory */
 static unsigned long   vga_vram_end;           /* End of video memory */
@@ -159,6 +160,201 @@ static inline void write_vga(unsigned char reg, unsigned int val)
        spin_unlock_irqrestore(&vga_lock, flags);
 }
 
+static inline void vga_set_mem_top(struct vc_data *c)
+{
+       write_vga(12, (c->vc_visible_origin - vga_vram_base) / 2);
+}
+
+#ifdef CONFIG_VGACON_SOFT_SCROLLBACK
+#include <linux/bootmem.h>
+/* software scrollback */
+static void *vgacon_scrollback;
+static int vgacon_scrollback_tail;
+static int vgacon_scrollback_size;
+static int vgacon_scrollback_rows;
+static int vgacon_scrollback_cnt;
+static int vgacon_scrollback_cur;
+static int vgacon_scrollback_save;
+static int vgacon_scrollback_restore;
+
+static void vgacon_scrollback_init(int pitch)
+{
+       int rows = CONFIG_VGACON_SOFT_SCROLLBACK_SIZE * 1024/pitch;
+
+       if (vgacon_scrollback) {
+               vgacon_scrollback_cnt  = 0;
+               vgacon_scrollback_tail = 0;
+               vgacon_scrollback_cur  = 0;
+               vgacon_scrollback_rows = rows - 1;
+               vgacon_scrollback_size = rows * pitch;
+       }
+}
+
+static void __init vgacon_scrollback_startup(void)
+{
+       vgacon_scrollback = alloc_bootmem(CONFIG_VGACON_SOFT_SCROLLBACK_SIZE
+                                         * 1024);
+       vgacon_scrollback_init(vga_video_num_columns * 2);
+}
+
+static void vgacon_scrollback_update(struct vc_data *c, int t, int count)
+{
+       void *p;
+
+       if (!vgacon_scrollback_size || c->vc_num != fg_console)
+               return;
+
+       p = (void *) (c->vc_origin + t * c->vc_size_row);
+
+       while (count--) {
+               scr_memcpyw(vgacon_scrollback + vgacon_scrollback_tail,
+                           p, c->vc_size_row);
+               vgacon_scrollback_cnt++;
+               p += c->vc_size_row;
+               vgacon_scrollback_tail += c->vc_size_row;
+
+               if (vgacon_scrollback_tail >= vgacon_scrollback_size)
+                       vgacon_scrollback_tail = 0;
+
+               if (vgacon_scrollback_cnt > vgacon_scrollback_rows)
+                       vgacon_scrollback_cnt = vgacon_scrollback_rows;
+
+               vgacon_scrollback_cur = vgacon_scrollback_cnt;
+       }
+}
+
+static void vgacon_restore_screen(struct vc_data *c)
+{
+       vgacon_scrollback_save = 0;
+
+       if (!vga_is_gfx && !vgacon_scrollback_restore) {
+               scr_memcpyw((u16 *) c->vc_origin, (u16 *) c->vc_screenbuf,
+                           c->vc_screenbuf_size > vga_vram_size ?
+                           vga_vram_size : c->vc_screenbuf_size);
+               vgacon_scrollback_restore = 1;
+               vgacon_scrollback_cur = vgacon_scrollback_cnt;
+       }
+}
+
+static int vgacon_scrolldelta(struct vc_data *c, int lines)
+{
+       int start, end, count, soff, diff;
+       void *d, *s;
+
+       if (!lines) {
+               c->vc_visible_origin = c->vc_origin;
+               vga_set_mem_top(c);
+               return 1;
+       }
+
+       if (!vgacon_scrollback)
+               return 1;
+
+       if (!vgacon_scrollback_save) {
+               vgacon_cursor(c, CM_ERASE);
+               vgacon_save_screen(c);
+               vgacon_scrollback_save = 1;
+       }
+
+       vgacon_scrollback_restore = 0;
+       start = vgacon_scrollback_cur + lines;
+       end = start + abs(lines);
+
+       if (start < 0)
+               start = 0;
+
+       if (start > vgacon_scrollback_cnt)
+               start = vgacon_scrollback_cnt;
+
+       if (end < 0)
+               end = 0;
+
+       if (end > vgacon_scrollback_cnt)
+               end = vgacon_scrollback_cnt;
+
+       vgacon_scrollback_cur = start;
+       count = end - start;
+       soff = vgacon_scrollback_tail - ((vgacon_scrollback_cnt - end) *
+                                        c->vc_size_row);
+       soff -= count * c->vc_size_row;
+
+       if (soff < 0)
+               soff += vgacon_scrollback_size;
+
+       count = vgacon_scrollback_cnt - start;
+
+       if (count > c->vc_rows)
+               count = c->vc_rows;
+
+       diff = c->vc_rows - count;
+
+       d = (void *) c->vc_origin;
+       s = (void *) c->vc_screenbuf;
+
+       while (count--) {
+               scr_memcpyw(d, vgacon_scrollback + soff, c->vc_size_row);
+               d += c->vc_size_row;
+               soff += c->vc_size_row;
+
+               if (soff >= vgacon_scrollback_size)
+                       soff = 0;
+       }
+
+       if (diff == c->vc_rows) {
+               vgacon_cursor(c, CM_MOVE);
+       } else {
+               while (diff--) {
+                       scr_memcpyw(d, s, c->vc_size_row);
+                       d += c->vc_size_row;
+                       s += c->vc_size_row;
+               }
+       }
+
+       return 1;
+}
+#else
+#define vgacon_scrollback_startup(...) do { } while (0)
+#define vgacon_scrollback_init(...)    do { } while (0)
+#define vgacon_scrollback_update(...)  do { } while (0)
+
+static void vgacon_restore_screen(struct vc_data *c)
+{
+       if (c->vc_origin != c->vc_visible_origin)
+               vgacon_scrolldelta(c, 0);
+}
+
+static int vgacon_scrolldelta(struct vc_data *c, int lines)
+{
+       if (!lines)             /* Turn scrollback off */
+               c->vc_visible_origin = c->vc_origin;
+       else {
+               int margin = c->vc_size_row * 4;
+               int ul, we, p, st;
+
+               if (vga_rolled_over >
+                   (c->vc_scr_end - vga_vram_base) + margin) {
+                       ul = c->vc_scr_end - vga_vram_base;
+                       we = vga_rolled_over + c->vc_size_row;
+               } else {
+                       ul = 0;
+                       we = vga_vram_size;
+               }
+               p = (c->vc_visible_origin - vga_vram_base - ul + we) % we +
+                   lines * c->vc_size_row;
+               st = (c->vc_origin - vga_vram_base - ul + we) % we;
+               if (st < 2 * margin)
+                       margin = 0;
+               if (p < margin)
+                       p = 0;
+               if (p > st - margin)
+                       p = st;
+               c->vc_visible_origin = vga_vram_base + (p + ul) % we;
+       }
+       vga_set_mem_top(c);
+       return 1;
+}
+#endif /* CONFIG_VGACON_SOFT_SCROLLBACK */
+
 static const char __init *vgacon_startup(void)
 {
        const char *display_desc = NULL;
@@ -195,7 +391,7 @@ static const char __init *vgacon_startup(void)
                        static struct resource ega_console_resource =
                            { "ega", 0x3B0, 0x3BF };
                        vga_video_type = VIDEO_TYPE_EGAM;
-                       vga_vram_end = 0xb8000;
+                       vga_vram_size = 0x8000;
                        display_desc = "EGA+";
                        request_resource(&ioport_resource,
                                         &ega_console_resource);
@@ -205,7 +401,7 @@ static const char __init *vgacon_startup(void)
                        static struct resource mda2_console_resource =
                            { "mda", 0x3BF, 0x3BF };
                        vga_video_type = VIDEO_TYPE_MDA;
-                       vga_vram_end = 0xb2000;
+                       vga_vram_size = 0x2000;
                        display_desc = "*MDA";
                        request_resource(&ioport_resource,
                                         &mda1_console_resource);
@@ -222,7 +418,7 @@ static const char __init *vgacon_startup(void)
                if ((ORIG_VIDEO_EGA_BX & 0xff) != 0x10) {
                        int i;
 
-                       vga_vram_end = 0xc0000;
+                       vga_vram_size = 0x8000;
 
                        if (!ORIG_VIDEO_ISVGA) {
                                static struct resource ega_console_resource
@@ -247,7 +443,7 @@ static const char __init *vgacon_startup(void)
                                 * and COE=1 isn't necessarily a good idea)
                                 */
                                vga_vram_base = 0xa0000;
-                               vga_vram_end = 0xb0000;
+                               vga_vram_size = 0x10000;
                                outb_p(6, VGA_GFX_I);
                                outb_p(6, VGA_GFX_D);
 #endif
@@ -279,7 +475,7 @@ static const char __init *vgacon_startup(void)
                        static struct resource cga_console_resource =
                            { "cga", 0x3D4, 0x3D5 };
                        vga_video_type = VIDEO_TYPE_CGA;
-                       vga_vram_end = 0xba000;
+                       vga_vram_size = 0x2000;
                        display_desc = "*CGA";
                        request_resource(&ioport_resource,
                                         &cga_console_resource);
@@ -287,9 +483,8 @@ static const char __init *vgacon_startup(void)
                }
        }
 
-       vga_vram_base = VGA_MAP_MEM(vga_vram_base);
-       vga_vram_end = VGA_MAP_MEM(vga_vram_end);
-       vga_vram_size = vga_vram_end - vga_vram_base;
+       vga_vram_base = VGA_MAP_MEM(vga_vram_base, vga_vram_size);
+       vga_vram_end = vga_vram_base + vga_vram_size;
 
        /*
         *      Find out if there is a graphics card present.
@@ -325,6 +520,10 @@ static const char __init *vgacon_startup(void)
                vga_scan_lines =
                    vga_video_font_height * vga_video_num_lines;
        }
+
+       vgacon_xres = ORIG_VIDEO_COLS * VGA_FONTWIDTH;
+       vgacon_yres = vga_scan_lines;
+       vgacon_scrollback_startup();
        return display_desc;
 }
 
@@ -351,11 +550,6 @@ static void vgacon_init(struct vc_data *c, int init)
                con_set_default_unimap(c);
 }
 
-static inline void vga_set_mem_top(struct vc_data *c)
-{
-       write_vga(12, (c->vc_visible_origin - vga_vram_base) / 2);
-}
-
 static void vgacon_deinit(struct vc_data *c)
 {
        /* When closing the last console, reset video origin */
@@ -427,29 +621,37 @@ static void vgacon_set_cursor_size(int xpos, int from, int to)
        cursor_size_lastto = to;
 
        spin_lock_irqsave(&vga_lock, flags);
-       outb_p(0x0a, vga_video_port_reg);       /* Cursor start */
-       curs = inb_p(vga_video_port_val);
-       outb_p(0x0b, vga_video_port_reg);       /* Cursor end */
-       cure = inb_p(vga_video_port_val);
+       if (vga_video_type >= VIDEO_TYPE_VGAC) {
+               outb_p(VGA_CRTC_CURSOR_START, vga_video_port_reg);
+               curs = inb_p(vga_video_port_val);
+               outb_p(VGA_CRTC_CURSOR_END, vga_video_port_reg);
+               cure = inb_p(vga_video_port_val);
+       } else {
+               curs = 0;
+               cure = 0;
+       }
 
        curs = (curs & 0xc0) | from;
        cure = (cure & 0xe0) | to;
 
-       outb_p(0x0a, vga_video_port_reg);       /* Cursor start */
+       outb_p(VGA_CRTC_CURSOR_START, vga_video_port_reg);
        outb_p(curs, vga_video_port_val);
-       outb_p(0x0b, vga_video_port_reg);       /* Cursor end */
+       outb_p(VGA_CRTC_CURSOR_END, vga_video_port_reg);
        outb_p(cure, vga_video_port_val);
        spin_unlock_irqrestore(&vga_lock, flags);
 }
 
 static void vgacon_cursor(struct vc_data *c, int mode)
 {
-       if (c->vc_origin != c->vc_visible_origin)
-               vgacon_scrolldelta(c, 0);
+       vgacon_restore_screen(c);
+
        switch (mode) {
        case CM_ERASE:
                write_vga(14, (c->vc_pos - vga_vram_base) / 2);
-               vgacon_set_cursor_size(c->vc_x, 31, 30);
+               if (vga_video_type >= VIDEO_TYPE_VGAC)
+                       vgacon_set_cursor_size(c->vc_x, 31, 30);
+               else
+                       vgacon_set_cursor_size(c->vc_x, 31, 31);
                break;
 
        case CM_MOVE:
@@ -487,7 +689,10 @@ static void vgacon_cursor(struct vc_data *c, int mode)
                                                10 ? 1 : 2));
                        break;
                case CUR_NONE:
-                       vgacon_set_cursor_size(c->vc_x, 31, 30);
+                       if (vga_video_type >= VIDEO_TYPE_VGAC)
+                               vgacon_set_cursor_size(c->vc_x, 31, 30);
+                       else
+                               vgacon_set_cursor_size(c->vc_x, 31, 31);
                        break;
                default:
                        vgacon_set_cursor_size(c->vc_x, 1,
@@ -503,54 +708,69 @@ static int vgacon_doresize(struct vc_data *c,
 {
        unsigned long flags;
        unsigned int scanlines = height * c->vc_font.height;
-       u8 scanlines_lo, r7, vsync_end, mode;
+       u8 scanlines_lo = 0, r7 = 0, vsync_end = 0, mode, max_scan;
 
        spin_lock_irqsave(&vga_lock, flags);
 
-       outb_p(VGA_CRTC_MODE, vga_video_port_reg);
-       mode = inb_p(vga_video_port_val);
+       vgacon_xres = width * VGA_FONTWIDTH;
+       vgacon_yres = height * c->vc_font.height;
+       if (vga_video_type >= VIDEO_TYPE_VGAC) {
+               outb_p(VGA_CRTC_MAX_SCAN, vga_video_port_reg);
+               max_scan = inb_p(vga_video_port_val);
+
+               if (max_scan & 0x80)
+                       scanlines <<= 1;
 
-       if (mode & 0x04)
-               scanlines >>= 1;
+               outb_p(VGA_CRTC_MODE, vga_video_port_reg);
+               mode = inb_p(vga_video_port_val);
 
-       scanlines -= 1;
-       scanlines_lo = scanlines & 0xff;
+               if (mode & 0x04)
+                       scanlines >>= 1;
 
-       outb_p(VGA_CRTC_OVERFLOW, vga_video_port_reg);
-       r7 = inb_p(vga_video_port_val) & ~0x42;
+               scanlines -= 1;
+               scanlines_lo = scanlines & 0xff;
 
-       if (scanlines & 0x100)
-               r7 |= 0x02;
-       if (scanlines & 0x200)
-               r7 |= 0x40;
+               outb_p(VGA_CRTC_OVERFLOW, vga_video_port_reg);
+               r7 = inb_p(vga_video_port_val) & ~0x42;
 
-       /* deprotect registers */
-       outb_p(VGA_CRTC_V_SYNC_END, vga_video_port_reg);
-       vsync_end = inb_p(vga_video_port_val);
-       outb_p(VGA_CRTC_V_SYNC_END, vga_video_port_reg);
-       outb_p(vsync_end & ~0x80, vga_video_port_val);
+               if (scanlines & 0x100)
+                       r7 |= 0x02;
+               if (scanlines & 0x200)
+                       r7 |= 0x40;
+
+               /* deprotect registers */
+               outb_p(VGA_CRTC_V_SYNC_END, vga_video_port_reg);
+               vsync_end = inb_p(vga_video_port_val);
+               outb_p(VGA_CRTC_V_SYNC_END, vga_video_port_reg);
+               outb_p(vsync_end & ~0x80, vga_video_port_val);
+       }
 
        outb_p(VGA_CRTC_H_DISP, vga_video_port_reg);
        outb_p(width - 1, vga_video_port_val);
        outb_p(VGA_CRTC_OFFSET, vga_video_port_reg);
        outb_p(width >> 1, vga_video_port_val);
 
-       outb_p(VGA_CRTC_V_DISP_END, vga_video_port_reg);
-       outb_p(scanlines_lo, vga_video_port_val);
-       outb_p(VGA_CRTC_OVERFLOW, vga_video_port_reg);
-       outb_p(r7,vga_video_port_val);
+       if (vga_video_type >= VIDEO_TYPE_VGAC) {
+               outb_p(VGA_CRTC_V_DISP_END, vga_video_port_reg);
+               outb_p(scanlines_lo, vga_video_port_val);
+               outb_p(VGA_CRTC_OVERFLOW, vga_video_port_reg);
+               outb_p(r7,vga_video_port_val);
 
-       /* reprotect registers */
-       outb_p(VGA_CRTC_V_SYNC_END, vga_video_port_reg);
-       outb_p(vsync_end, vga_video_port_val);
+               /* reprotect registers */
+               outb_p(VGA_CRTC_V_SYNC_END, vga_video_port_reg);
+               outb_p(vsync_end, vga_video_port_val);
+       }
 
        spin_unlock_irqrestore(&vga_lock, flags);
-
        return 0;
 }
 
 static int vgacon_switch(struct vc_data *c)
 {
+       int x = c->vc_cols * VGA_FONTWIDTH;
+       int y = c->vc_rows * c->vc_font.height;
+       int rows = ORIG_VIDEO_LINES * vga_default_font_height/
+               c->vc_font.height;
        /*
         * We need to save screen size here as it's the only way
         * we can spot the screen has been resized and we need to
@@ -566,13 +786,15 @@ static int vgacon_switch(struct vc_data *c)
                scr_memcpyw((u16 *) c->vc_origin, (u16 *) c->vc_screenbuf,
                            c->vc_screenbuf_size > vga_vram_size ?
                                vga_vram_size : c->vc_screenbuf_size);
-               if (!(vga_video_num_columns % 2) &&
-                   vga_video_num_columns <= ORIG_VIDEO_COLS &&
-                   vga_video_num_lines <= (ORIG_VIDEO_LINES *
-                       vga_default_font_height) / c->vc_font.height)
+
+               if ((vgacon_xres != x || vgacon_yres != y) &&
+                   (!(vga_video_num_columns % 2) &&
+                    vga_video_num_columns <= ORIG_VIDEO_COLS &&
+                    vga_video_num_lines <= rows))
                        vgacon_doresize(c, c->vc_cols, c->vc_rows);
        }
 
+       vgacon_scrollback_init(c->vc_size_row);
        return 0;               /* Redrawing not needed */
 }
 
@@ -797,14 +1019,14 @@ static int vgacon_do_font_op(struct vgastate *state,char *arg,int set,int ch512)
        char *charmap;
        
        if (vga_video_type != VIDEO_TYPE_EGAM) {
-               charmap = (char *) VGA_MAP_MEM(colourmap);
+               charmap = (char *) VGA_MAP_MEM(colourmap, 0);
                beg = 0x0e;
 #ifdef VGA_CAN_DO_64KB
                if (vga_video_type == VIDEO_TYPE_VGAC)
                        beg = 0x06;
 #endif
        } else {
-               charmap = (char *) VGA_MAP_MEM(blackwmap);
+               charmap = (char *) VGA_MAP_MEM(blackwmap, 0);
                beg = 0x0a;
        }
 
@@ -966,6 +1188,7 @@ static int vgacon_adjust_height(struct vc_data *vc, unsigned fontheight)
        outb_p(0x12, vga_video_port_reg);       /* Vertical display limit */
        outb_p(vde, vga_video_port_val);
        spin_unlock_irq(&vga_lock);
+       vga_video_font_height = fontheight;
 
        for (i = 0; i < MAX_NR_CONSOLES; i++) {
                struct vc_data *c = vc_cons[i].d;
@@ -992,7 +1215,8 @@ static int vgacon_font_set(struct vc_data *c, struct console_font *font, unsigne
        if (vga_video_type < VIDEO_TYPE_EGAM)
                return -EINVAL;
 
-       if (font->width != 8 || (charcount != 256 && charcount != 512))
+       if (font->width != VGA_FONTWIDTH ||
+           (charcount != 256 && charcount != 512))
                return -EINVAL;
 
        rc = vgacon_do_font_op(&state, font->data, 1, charcount == 512);
@@ -1009,7 +1233,7 @@ static int vgacon_font_get(struct vc_data *c, struct console_font *font)
        if (vga_video_type < VIDEO_TYPE_EGAM)
                return -EINVAL;
 
-       font->width = 8;
+       font->width = VGA_FONTWIDTH;
        font->height = c->vc_font.height;
        font->charcount = vga_512_chars ? 512 : 256;
        if (!font->data)
@@ -1038,37 +1262,6 @@ static int vgacon_resize(struct vc_data *c, unsigned int width,
        return 0;
 }
 
-static int vgacon_scrolldelta(struct vc_data *c, int lines)
-{
-       if (!lines)             /* Turn scrollback off */
-               c->vc_visible_origin = c->vc_origin;
-       else {
-               int margin = c->vc_size_row * 4;
-               int ul, we, p, st;
-
-               if (vga_rolled_over >
-                   (c->vc_scr_end - vga_vram_base) + margin) {
-                       ul = c->vc_scr_end - vga_vram_base;
-                       we = vga_rolled_over + c->vc_size_row;
-               } else {
-                       ul = 0;
-                       we = vga_vram_size;
-               }
-               p = (c->vc_visible_origin - vga_vram_base - ul + we) % we +
-                   lines * c->vc_size_row;
-               st = (c->vc_origin - vga_vram_base - ul + we) % we;
-               if (st < 2 * margin)
-                       margin = 0;
-               if (p < margin)
-                       p = 0;
-               if (p > st - margin)
-                       p = st;
-               c->vc_visible_origin = vga_vram_base + (p + ul) % we;
-       }
-       vga_set_mem_top(c);
-       return 1;
-}
-
 static int vgacon_set_origin(struct vc_data *c)
 {
        if (vga_is_gfx ||       /* We don't play origin tricks in graphic modes */
@@ -1111,15 +1304,14 @@ static int vgacon_scroll(struct vc_data *c, int t, int b, int dir,
        if (t || b != c->vc_rows || vga_is_gfx)
                return 0;
 
-       if (c->vc_origin != c->vc_visible_origin)
-               vgacon_scrolldelta(c, 0);
-
        if (!vga_hardscroll_enabled || lines >= c->vc_rows / 2)
                return 0;
 
+       vgacon_restore_screen(c);
        oldo = c->vc_origin;
        delta = lines * c->vc_size_row;
        if (dir == SM_UP) {
+               vgacon_scrollback_update(c, t, lines);
                if (c->vc_scr_end + delta >= vga_vram_end) {
                        scr_memcpyw((u16 *) vga_vram_base,
                                    (u16 *) (oldo + delta),