blob: 122fb3c3ec9d0dbb8abea7259f916ffd86e610a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/video/vgastate.c -- VGA state save/restore
3 *
4 * Copyright 2002 James Simmons
Asaf Vertz96699b42014-12-11 11:04:41 +02005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright history from vga16fb.c:
7 * Copyright 1999 Ben Pfaff and Petr Vandrovec
8 * Based on VGA info at http://www.goodnet.com/~tinara/FreeVGA/home.htm
9 * Based on VESA framebuffer (c) 1998 Gerd Knorr
10 *
11 * This file is subject to the terms and conditions of the GNU General
12 * Public License. See the file COPYING in the main directory of this
Asaf Vertz96699b42014-12-11 11:04:41 +020013 * archive for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/fb.h>
19#include <linux/vmalloc.h>
20#include <video/vga.h>
21
22struct regstate {
23 __u8 *vga_font0;
24 __u8 *vga_font1;
25 __u8 *vga_text;
26 __u8 *vga_cmap;
27 __u8 *attr;
28 __u8 *crtc;
29 __u8 *gfx;
30 __u8 *seq;
31 __u8 misc;
Asaf Vertz96699b42014-12-11 11:04:41 +020032};
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Asaf Vertz96699b42014-12-11 11:04:41 +020034static inline unsigned char vga_rcrtcs(void __iomem *regbase, unsigned short iobase,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 unsigned char reg)
36{
37 vga_w(regbase, iobase + 0x4, reg);
38 return vga_r(regbase, iobase + 0x5);
39}
40
Asaf Vertz96699b42014-12-11 11:04:41 +020041static inline void vga_wcrtcs(void __iomem *regbase, unsigned short iobase,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 unsigned char reg, unsigned char val)
43{
44 vga_w(regbase, iobase + 0x4, reg);
45 vga_w(regbase, iobase + 0x5, val);
46}
47
48static void save_vga_text(struct vgastate *state, void __iomem *fbbase)
49{
50 struct regstate *saved = (struct regstate *) state->vidstate;
51 int i;
52 u8 misc, attr10, gr4, gr5, gr6, seq1, seq2, seq4;
Ondrej Zajicek04493592007-05-08 00:39:41 -070053 unsigned short iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 /* if in graphics mode, no need to save */
Ondrej Zajicek04493592007-05-08 00:39:41 -070056 misc = vga_r(state->vgabase, VGA_MIS_R);
57 iobase = (misc & 1) ? 0x3d0 : 0x3b0;
58
59 vga_r(state->vgabase, iobase + 0xa);
60 vga_w(state->vgabase, VGA_ATT_W, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 attr10 = vga_rattr(state->vgabase, 0x10);
Ondrej Zajicek04493592007-05-08 00:39:41 -070062 vga_r(state->vgabase, iobase + 0xa);
63 vga_w(state->vgabase, VGA_ATT_W, 0x20);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (attr10 & 1)
66 return;
Ondrej Zajicek04493592007-05-08 00:39:41 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 /* save regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 gr4 = vga_rgfx(state->vgabase, VGA_GFX_PLANE_READ);
70 gr5 = vga_rgfx(state->vgabase, VGA_GFX_MODE);
71 gr6 = vga_rgfx(state->vgabase, VGA_GFX_MISC);
72 seq2 = vga_rseq(state->vgabase, VGA_SEQ_PLANE_WRITE);
73 seq4 = vga_rseq(state->vgabase, VGA_SEQ_MEMORY_MODE);
Asaf Vertz96699b42014-12-11 11:04:41 +020074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 /* blank screen */
76 seq1 = vga_rseq(state->vgabase, VGA_SEQ_CLOCK_MODE);
77 vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x1);
78 vga_wseq(state->vgabase, VGA_SEQ_CLOCK_MODE, seq1 | 1 << 5);
79 vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x3);
80
81 /* save font at plane 2 */
82 if (state->flags & VGA_SAVE_FONT0) {
83 vga_wseq(state->vgabase, VGA_SEQ_PLANE_WRITE, 0x4);
84 vga_wseq(state->vgabase, VGA_SEQ_MEMORY_MODE, 0x6);
85 vga_wgfx(state->vgabase, VGA_GFX_PLANE_READ, 0x2);
86 vga_wgfx(state->vgabase, VGA_GFX_MODE, 0x0);
87 vga_wgfx(state->vgabase, VGA_GFX_MISC, 0x5);
Asaf Vertz96699b42014-12-11 11:04:41 +020088 for (i = 0; i < 4 * 8192; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 saved->vga_font0[i] = vga_r(fbbase, i);
90 }
91
92 /* save font at plane 3 */
93 if (state->flags & VGA_SAVE_FONT1) {
94 vga_wseq(state->vgabase, VGA_SEQ_PLANE_WRITE, 0x8);
95 vga_wseq(state->vgabase, VGA_SEQ_MEMORY_MODE, 0x6);
96 vga_wgfx(state->vgabase, VGA_GFX_PLANE_READ, 0x3);
97 vga_wgfx(state->vgabase, VGA_GFX_MODE, 0x0);
98 vga_wgfx(state->vgabase, VGA_GFX_MISC, 0x5);
Asaf Vertz96699b42014-12-11 11:04:41 +020099 for (i = 0; i < state->memsize; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 saved->vga_font1[i] = vga_r(fbbase, i);
101 }
Asaf Vertz96699b42014-12-11 11:04:41 +0200102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /* save font at plane 0/1 */
104 if (state->flags & VGA_SAVE_TEXT) {
105 vga_wseq(state->vgabase, VGA_SEQ_PLANE_WRITE, 0x1);
106 vga_wseq(state->vgabase, VGA_SEQ_MEMORY_MODE, 0x6);
107 vga_wgfx(state->vgabase, VGA_GFX_PLANE_READ, 0x0);
108 vga_wgfx(state->vgabase, VGA_GFX_MODE, 0x0);
109 vga_wgfx(state->vgabase, VGA_GFX_MISC, 0x5);
Asaf Vertz96699b42014-12-11 11:04:41 +0200110 for (i = 0; i < 8192; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 saved->vga_text[i] = vga_r(fbbase, i);
112
113 vga_wseq(state->vgabase, VGA_SEQ_PLANE_WRITE, 0x2);
114 vga_wseq(state->vgabase, VGA_SEQ_MEMORY_MODE, 0x6);
115 vga_wgfx(state->vgabase, VGA_GFX_PLANE_READ, 0x1);
116 vga_wgfx(state->vgabase, VGA_GFX_MODE, 0x0);
117 vga_wgfx(state->vgabase, VGA_GFX_MISC, 0x5);
Asaf Vertz96699b42014-12-11 11:04:41 +0200118 for (i = 0; i < 8192; i++)
119 saved->vga_text[8192+i] = vga_r(fbbase + 2 * 8192, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121
122 /* restore regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 vga_wseq(state->vgabase, VGA_SEQ_PLANE_WRITE, seq2);
124 vga_wseq(state->vgabase, VGA_SEQ_MEMORY_MODE, seq4);
125
126 vga_wgfx(state->vgabase, VGA_GFX_PLANE_READ, gr4);
127 vga_wgfx(state->vgabase, VGA_GFX_MODE, gr5);
128 vga_wgfx(state->vgabase, VGA_GFX_MISC, gr6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 /* unblank screen */
131 vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x1);
132 vga_wseq(state->vgabase, VGA_SEQ_CLOCK_MODE, seq1 & ~(1 << 5));
133 vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x3);
134
135 vga_wseq(state->vgabase, VGA_SEQ_CLOCK_MODE, seq1);
136}
137
138static void restore_vga_text(struct vgastate *state, void __iomem *fbbase)
139{
140 struct regstate *saved = (struct regstate *) state->vidstate;
141 int i;
Ondrej Zajicek04493592007-05-08 00:39:41 -0700142 u8 gr1, gr3, gr4, gr5, gr6, gr8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 u8 seq1, seq2, seq4;
144
145 /* save regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 gr1 = vga_rgfx(state->vgabase, VGA_GFX_SR_ENABLE);
147 gr3 = vga_rgfx(state->vgabase, VGA_GFX_DATA_ROTATE);
148 gr4 = vga_rgfx(state->vgabase, VGA_GFX_PLANE_READ);
149 gr5 = vga_rgfx(state->vgabase, VGA_GFX_MODE);
150 gr6 = vga_rgfx(state->vgabase, VGA_GFX_MISC);
151 gr8 = vga_rgfx(state->vgabase, VGA_GFX_BIT_MASK);
152 seq2 = vga_rseq(state->vgabase, VGA_SEQ_PLANE_WRITE);
153 seq4 = vga_rseq(state->vgabase, VGA_SEQ_MEMORY_MODE);
Asaf Vertz96699b42014-12-11 11:04:41 +0200154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* blank screen */
156 seq1 = vga_rseq(state->vgabase, VGA_SEQ_CLOCK_MODE);
157 vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x1);
158 vga_wseq(state->vgabase, VGA_SEQ_CLOCK_MODE, seq1 | 1 << 5);
159 vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x3);
160
161 if (state->depth == 4) {
162 vga_wgfx(state->vgabase, VGA_GFX_DATA_ROTATE, 0x0);
163 vga_wgfx(state->vgabase, VGA_GFX_BIT_MASK, 0xff);
164 vga_wgfx(state->vgabase, VGA_GFX_SR_ENABLE, 0x00);
165 }
Asaf Vertz96699b42014-12-11 11:04:41 +0200166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /* restore font at plane 2 */
168 if (state->flags & VGA_SAVE_FONT0) {
169 vga_wseq(state->vgabase, VGA_SEQ_PLANE_WRITE, 0x4);
170 vga_wseq(state->vgabase, VGA_SEQ_MEMORY_MODE, 0x6);
171 vga_wgfx(state->vgabase, VGA_GFX_PLANE_READ, 0x2);
172 vga_wgfx(state->vgabase, VGA_GFX_MODE, 0x0);
173 vga_wgfx(state->vgabase, VGA_GFX_MISC, 0x5);
Asaf Vertz96699b42014-12-11 11:04:41 +0200174 for (i = 0; i < 4 * 8192; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 vga_w(fbbase, i, saved->vga_font0[i]);
176 }
177
178 /* restore font at plane 3 */
179 if (state->flags & VGA_SAVE_FONT1) {
180 vga_wseq(state->vgabase, VGA_SEQ_PLANE_WRITE, 0x8);
181 vga_wseq(state->vgabase, VGA_SEQ_MEMORY_MODE, 0x6);
182 vga_wgfx(state->vgabase, VGA_GFX_PLANE_READ, 0x3);
183 vga_wgfx(state->vgabase, VGA_GFX_MODE, 0x0);
184 vga_wgfx(state->vgabase, VGA_GFX_MISC, 0x5);
Asaf Vertz96699b42014-12-11 11:04:41 +0200185 for (i = 0; i < state->memsize; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 vga_w(fbbase, i, saved->vga_font1[i]);
187 }
Asaf Vertz96699b42014-12-11 11:04:41 +0200188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* restore font at plane 0/1 */
190 if (state->flags & VGA_SAVE_TEXT) {
191 vga_wseq(state->vgabase, VGA_SEQ_PLANE_WRITE, 0x1);
192 vga_wseq(state->vgabase, VGA_SEQ_MEMORY_MODE, 0x6);
193 vga_wgfx(state->vgabase, VGA_GFX_PLANE_READ, 0x0);
194 vga_wgfx(state->vgabase, VGA_GFX_MODE, 0x0);
195 vga_wgfx(state->vgabase, VGA_GFX_MISC, 0x5);
Asaf Vertz96699b42014-12-11 11:04:41 +0200196 for (i = 0; i < 8192; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 vga_w(fbbase, i, saved->vga_text[i]);
Asaf Vertz96699b42014-12-11 11:04:41 +0200198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 vga_wseq(state->vgabase, VGA_SEQ_PLANE_WRITE, 0x2);
200 vga_wseq(state->vgabase, VGA_SEQ_MEMORY_MODE, 0x6);
201 vga_wgfx(state->vgabase, VGA_GFX_PLANE_READ, 0x1);
202 vga_wgfx(state->vgabase, VGA_GFX_MODE, 0x0);
203 vga_wgfx(state->vgabase, VGA_GFX_MISC, 0x5);
Asaf Vertz96699b42014-12-11 11:04:41 +0200204 for (i = 0; i < 8192; i++)
205 vga_w(fbbase, i, saved->vga_text[8192+i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
208 /* unblank screen */
209 vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x1);
210 vga_wseq(state->vgabase, VGA_SEQ_CLOCK_MODE, seq1 & ~(1 << 5));
211 vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x3);
212
213 /* restore regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 vga_wgfx(state->vgabase, VGA_GFX_SR_ENABLE, gr1);
215 vga_wgfx(state->vgabase, VGA_GFX_DATA_ROTATE, gr3);
216 vga_wgfx(state->vgabase, VGA_GFX_PLANE_READ, gr4);
217 vga_wgfx(state->vgabase, VGA_GFX_MODE, gr5);
218 vga_wgfx(state->vgabase, VGA_GFX_MISC, gr6);
219 vga_wgfx(state->vgabase, VGA_GFX_BIT_MASK, gr8);
220
221 vga_wseq(state->vgabase, VGA_SEQ_CLOCK_MODE, seq1);
222 vga_wseq(state->vgabase, VGA_SEQ_PLANE_WRITE, seq2);
223 vga_wseq(state->vgabase, VGA_SEQ_MEMORY_MODE, seq4);
224}
Asaf Vertz96699b42014-12-11 11:04:41 +0200225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static void save_vga_mode(struct vgastate *state)
227{
228 struct regstate *saved = (struct regstate *) state->vidstate;
229 unsigned short iobase;
230 int i;
231
232 saved->misc = vga_r(state->vgabase, VGA_MIS_R);
233 if (saved->misc & 1)
234 iobase = 0x3d0;
235 else
236 iobase = 0x3b0;
237
Asaf Vertz96699b42014-12-11 11:04:41 +0200238 for (i = 0; i < state->num_crtc; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 saved->crtc[i] = vga_rcrtcs(state->vgabase, iobase, i);
Asaf Vertz96699b42014-12-11 11:04:41 +0200240
241 vga_r(state->vgabase, iobase + 0xa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 vga_w(state->vgabase, VGA_ATT_W, 0x00);
243 for (i = 0; i < state->num_attr; i++) {
244 vga_r(state->vgabase, iobase + 0xa);
245 saved->attr[i] = vga_rattr(state->vgabase, i);
246 }
247 vga_r(state->vgabase, iobase + 0xa);
248 vga_w(state->vgabase, VGA_ATT_W, 0x20);
249
Asaf Vertz96699b42014-12-11 11:04:41 +0200250 for (i = 0; i < state->num_gfx; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 saved->gfx[i] = vga_rgfx(state->vgabase, i);
252
Asaf Vertz96699b42014-12-11 11:04:41 +0200253 for (i = 0; i < state->num_seq; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 saved->seq[i] = vga_rseq(state->vgabase, i);
255}
256
257static void restore_vga_mode(struct vgastate *state)
258{
259 struct regstate *saved = (struct regstate *) state->vidstate;
260 unsigned short iobase;
261 int i;
262
263 vga_w(state->vgabase, VGA_MIS_W, saved->misc);
264
265 if (saved->misc & 1)
266 iobase = 0x3d0;
267 else
268 iobase = 0x3b0;
269
270 /* turn off display */
Asaf Vertz96699b42014-12-11 11:04:41 +0200271 vga_wseq(state->vgabase, VGA_SEQ_CLOCK_MODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 saved->seq[VGA_SEQ_CLOCK_MODE] | 0x20);
273
274 /* disable sequencer */
275 vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x01);
Asaf Vertz96699b42014-12-11 11:04:41 +0200276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* enable palette addressing */
278 vga_r(state->vgabase, iobase + 0xa);
279 vga_w(state->vgabase, VGA_ATT_W, 0x00);
280
Asaf Vertz96699b42014-12-11 11:04:41 +0200281 for (i = 2; i < state->num_seq; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 vga_wseq(state->vgabase, i, saved->seq[i]);
283
284
285 /* unprotect vga regs */
286 vga_wcrtcs(state->vgabase, iobase, 17, saved->crtc[17] & ~0x80);
Asaf Vertz96699b42014-12-11 11:04:41 +0200287 for (i = 0; i < state->num_crtc; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 vga_wcrtcs(state->vgabase, iobase, i, saved->crtc[i]);
Asaf Vertz96699b42014-12-11 11:04:41 +0200289
290 for (i = 0; i < state->num_gfx; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 vga_wgfx(state->vgabase, i, saved->gfx[i]);
292
293 for (i = 0; i < state->num_attr; i++) {
294 vga_r(state->vgabase, iobase + 0xa);
295 vga_wattr(state->vgabase, i, saved->attr[i]);
296 }
297
298 /* reenable sequencer */
299 vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x03);
300 /* turn display on */
Asaf Vertz96699b42014-12-11 11:04:41 +0200301 vga_wseq(state->vgabase, VGA_SEQ_CLOCK_MODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 saved->seq[VGA_SEQ_CLOCK_MODE] & ~(1 << 5));
303
304 /* disable video/palette source */
305 vga_r(state->vgabase, iobase + 0xa);
306 vga_w(state->vgabase, VGA_ATT_W, 0x20);
307}
308
309static void save_vga_cmap(struct vgastate *state)
310{
311 struct regstate *saved = (struct regstate *) state->vidstate;
312 int i;
313
314 vga_w(state->vgabase, VGA_PEL_MSK, 0xff);
Asaf Vertz96699b42014-12-11 11:04:41 +0200315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* assumes DAC is readable and writable */
317 vga_w(state->vgabase, VGA_PEL_IR, 0x00);
318 for (i = 0; i < 768; i++)
319 saved->vga_cmap[i] = vga_r(state->vgabase, VGA_PEL_D);
320}
321
322static void restore_vga_cmap(struct vgastate *state)
323{
324 struct regstate *saved = (struct regstate *) state->vidstate;
325 int i;
326
327 vga_w(state->vgabase, VGA_PEL_MSK, 0xff);
328
329 /* assumes DAC is readable and writable */
330 vga_w(state->vgabase, VGA_PEL_IW, 0x00);
331 for (i = 0; i < 768; i++)
332 vga_w(state->vgabase, VGA_PEL_D, saved->vga_cmap[i]);
333}
334
335static void vga_cleanup(struct vgastate *state)
336{
337 if (state->vidstate != NULL) {
338 struct regstate *saved = (struct regstate *) state->vidstate;
339
Jesper Juhlf91012102005-09-10 00:26:54 -0700340 vfree(saved->vga_font0);
341 vfree(saved->vga_font1);
342 vfree(saved->vga_text);
343 vfree(saved->vga_cmap);
344 vfree(saved->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 kfree(saved);
346 state->vidstate = NULL;
347 }
348}
Asaf Vertz96699b42014-12-11 11:04:41 +0200349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350int save_vga(struct vgastate *state)
351{
352 struct regstate *saved;
353
Antonino A. Daplasdef1ede2006-01-09 20:53:45 -0800354 saved = kzalloc(sizeof(struct regstate), GFP_KERNEL);
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (saved == NULL)
357 return 1;
Antonino A. Daplasdef1ede2006-01-09 20:53:45 -0800358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 state->vidstate = (void *)saved;
Asaf Vertz96699b42014-12-11 11:04:41 +0200360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (state->flags & VGA_SAVE_CMAP) {
362 saved->vga_cmap = vmalloc(768);
363 if (!saved->vga_cmap) {
364 vga_cleanup(state);
365 return 1;
366 }
367 save_vga_cmap(state);
368 }
369
370 if (state->flags & VGA_SAVE_MODE) {
371 int total;
372
373 if (state->num_attr < 21)
374 state->num_attr = 21;
375 if (state->num_crtc < 25)
376 state->num_crtc = 25;
377 if (state->num_gfx < 9)
378 state->num_gfx = 9;
379 if (state->num_seq < 5)
380 state->num_seq = 5;
381 total = state->num_attr + state->num_crtc +
382 state->num_gfx + state->num_seq;
383
384 saved->attr = vmalloc(total);
385 if (!saved->attr) {
386 vga_cleanup(state);
387 return 1;
388 }
389 saved->crtc = saved->attr + state->num_attr;
390 saved->gfx = saved->crtc + state->num_crtc;
391 saved->seq = saved->gfx + state->num_gfx;
392
393 save_vga_mode(state);
394 }
395
396 if (state->flags & VGA_SAVE_FONTS) {
397 void __iomem *fbbase;
398
399 /* exit if window is less than 32K */
400 if (state->memsize && state->memsize < 4 * 8192) {
401 vga_cleanup(state);
402 return 1;
403 }
404 if (!state->memsize)
405 state->memsize = 8 * 8192;
Asaf Vertz96699b42014-12-11 11:04:41 +0200406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (!state->membase)
408 state->membase = 0xA0000;
409
410 fbbase = ioremap(state->membase, state->memsize);
411
412 if (!fbbase) {
413 vga_cleanup(state);
414 return 1;
415 }
416
Asaf Vertz96699b42014-12-11 11:04:41 +0200417 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 * save only first 32K used by vgacon
419 */
420 if (state->flags & VGA_SAVE_FONT0) {
421 saved->vga_font0 = vmalloc(4 * 8192);
422 if (!saved->vga_font0) {
423 iounmap(fbbase);
424 vga_cleanup(state);
425 return 1;
426 }
427 }
Asaf Vertz96699b42014-12-11 11:04:41 +0200428 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * largely unused, but if required by the caller
430 * we'll just save everything.
431 */
432 if (state->flags & VGA_SAVE_FONT1) {
433 saved->vga_font1 = vmalloc(state->memsize);
434 if (!saved->vga_font1) {
435 iounmap(fbbase);
436 vga_cleanup(state);
437 return 1;
438 }
439 }
440 /*
441 * Save 8K at plane0[0], and 8K at plane1[16K]
442 */
443 if (state->flags & VGA_SAVE_TEXT) {
444 saved->vga_text = vmalloc(8192 * 2);
445 if (!saved->vga_text) {
446 iounmap(fbbase);
447 vga_cleanup(state);
448 return 1;
449 }
450 }
Asaf Vertz96699b42014-12-11 11:04:41 +0200451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 save_vga_text(state, fbbase);
453 iounmap(fbbase);
454 }
455 return 0;
456}
457
Mehdi Bounya811ab8d2018-10-08 12:57:35 +0200458int restore_vga(struct vgastate *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 if (state->vidstate == NULL)
461 return 1;
462
463 if (state->flags & VGA_SAVE_MODE)
464 restore_vga_mode(state);
465
466 if (state->flags & VGA_SAVE_FONTS) {
467 void __iomem *fbbase = ioremap(state->membase, state->memsize);
468
469 if (!fbbase) {
470 vga_cleanup(state);
471 return 1;
472 }
473 restore_vga_text(state, fbbase);
474 iounmap(fbbase);
475 }
476
477 if (state->flags & VGA_SAVE_CMAP)
478 restore_vga_cmap(state);
479
480 vga_cleanup(state);
481 return 0;
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484EXPORT_SYMBOL(save_vga);
485EXPORT_SYMBOL(restore_vga);
486
487MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
488MODULE_DESCRIPTION("VGA State Save/Restore");
489MODULE_LICENSE("GPL");
490