xref: /illumos-gate/usr/src/uts/i86pc/os/graphics.c (revision 9c88ac3ab4edaa5e8c0130ed1b4b376ea57c545a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/systm.h>
28 #include <sys/bootconf.h>
29 #include <sys/thread.h>
30 #include <sys/ddi.h>
31 #include <sys/sunddi.h>
32 #include <vm/seg_kmem.h>
33 #include <sys/file.h>
34 #include <sys/kd.h>
35 #include <sys/sunldi.h>
36 
37 #define	VIDEOMEM	0xa0000
38 
39 extern void outb(int, uchar_t);
40 
41 static int graphics_mode;
42 static int cursor_y = 309;
43 static int cursor_x = 136;
44 
45 #define	BAR_STEPS 46
46 
47 static uchar_t bar[BAR_STEPS];
48 static kthread_t *progressbar_tid;
49 static kmutex_t pbar_lock;
50 static kcondvar_t pbar_cv;
51 static char *videomem = (caddr_t)VIDEOMEM;
52 static int videomem_size;
53 
54 /* select the plane(s) to draw to */
55 static void
56 mapmask(int plane)
57 {
58 	outb(0x3c4, 2);
59 	outb(0x3c5, plane);
60 }
61 
62 static void
63 bitmask(int value)
64 {
65 	outb(0x3ce, 8);
66 	outb(0x3cf, value);
67 }
68 
69 static void
70 progressbar_show(void)
71 {
72 	int j, k, offset;
73 	uchar_t *mem, *ptr;
74 
75 	offset = cursor_y * 80 + cursor_x / 8;
76 	mem = (uchar_t *)videomem + offset;
77 
78 	bitmask(0xff);
79 	mapmask(0xff); /* write to all planes at once? */
80 	for (j = 0; j < 4; j++) {   /* bar height: 4 pixels */
81 		ptr = mem + j * 80;
82 		for (k = 0; k < BAR_STEPS; k++, ptr++)
83 			*ptr = bar[k];
84 	}
85 	bitmask(0x00);
86 }
87 
88 /*
89  * Initialize a rectangle area for progress bar
90  *
91  * Multiboot has initialized graphics mode to 640x480
92  * with 16 colors.
93  */
94 void
95 progressbar_init()
96 {
97 	int i;
98 	char cons[10];
99 
100 	/* see if we are in graphics mode */
101 	if (BOP_GETPROPLEN(bootops, "console") != sizeof ("graphics"))
102 		return;
103 	(void) BOP_GETPROP(bootops, "console", cons);
104 	if (strncmp(cons, "graphics", strlen("graphics")) != 0)
105 		return;
106 	if (BOP_GETPROPLEN(bootops, "efi-systab") > 0)
107 		return;
108 
109 	graphics_mode = 1;
110 
111 	for (i = 0; i < BAR_STEPS; i++) {
112 		bar[i] = 0x00;
113 	}
114 
115 	progressbar_show();
116 }
117 
118 static void
119 progressbar_step()
120 {
121 	static int limit = 0;
122 
123 	bar[limit] = 0xff;
124 
125 	if (limit > 3)
126 		bar[limit - 4] = 0x00;
127 	else
128 		bar[limit + BAR_STEPS - 4] = 0x00;
129 
130 	limit++;
131 	if (limit == BAR_STEPS)
132 		limit = 0;
133 
134 	progressbar_show();
135 }
136 
137 /*ARGSUSED*/
138 static void
139 progressbar_thread(void *arg)
140 {
141 	clock_t end = drv_usectohz(150000);
142 
143 	mutex_enter(&pbar_lock);
144 	while (graphics_mode) {
145 		progressbar_step();
146 		(void) cv_reltimedwait(&pbar_cv, &pbar_lock, end,
147 		    TR_CLOCK_TICK);
148 	}
149 	mutex_exit(&pbar_lock);
150 }
151 
152 void
153 progressbar_start(void)
154 {
155 #if !defined(__xpv)
156 	extern pri_t minclsyspri;
157 
158 	if (graphics_mode == 0)
159 		return;
160 
161 	/* map video memory to kernel heap */
162 	videomem_size = ptob(btopr(38400));	/* 640 x 480 / 8 bytes */
163 	videomem = vmem_alloc(heap_arena, videomem_size, VM_SLEEP);
164 	if (videomem == NULL) {
165 		cmn_err(CE_NOTE, "!failed to start progress bar");
166 		graphics_mode = 0;
167 		return;
168 	}
169 	hat_devload(kas.a_hat, videomem, videomem_size,
170 	    btop(VIDEOMEM), (PROT_READ | PROT_WRITE),
171 	    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
172 
173 	progressbar_tid = thread_create(NULL, 0, progressbar_thread,
174 	    NULL, 0, &p0, TS_RUN, minclsyspri);
175 #endif
176 }
177 
178 void
179 progressbar_stop(void)
180 {
181 #if !defined(__xpv)
182 	if (graphics_mode == 0)
183 		return;
184 
185 	graphics_mode = 0;
186 	mutex_enter(&pbar_lock);
187 	cv_signal(&pbar_cv);
188 	mutex_exit(&pbar_lock);
189 	if (progressbar_tid != NULL)
190 		thread_join(progressbar_tid->t_did);
191 
192 	/* unmap video memory */
193 	hat_unload(kas.a_hat, videomem, videomem_size, HAT_UNLOAD_UNLOCK);
194 	vmem_free(heap_arena, videomem, videomem_size);
195 #endif
196 }
197 
198 /*ARGSUSED*/
199 void
200 progressbar_key_abort(ldi_ident_t li)
201 {
202 #if !defined(__xpv)
203 	char *fbpath;
204 	int ret;
205 	ldi_handle_t hdl;
206 
207 	extern char *consconfig_get_plat_fbpath(void);
208 
209 	if (graphics_mode == 0)
210 		return;
211 
212 	fbpath = consconfig_get_plat_fbpath();
213 
214 	if (ldi_open_by_name(fbpath, FWRITE, kcred, &hdl, li) != 0) {
215 		cmn_err(CE_NOTE, "!ldi_open_by_name failed");
216 	} else {
217 		if (ldi_ioctl(hdl, KDSETMODE, KD_RESETTEXT, FKIOCTL,
218 		    kcred, &ret)
219 		    != 0)
220 				cmn_err(CE_NOTE,
221 				    "!ldi_ioctl for KD_RESETTEXT failed");
222 		(void) ldi_close(hdl, NULL, kcred);
223 	}
224 #endif
225 }
226