xref: /illumos-gate/usr/src/uts/i86pc/os/graphics.c (revision 3ccda6479cf240cd732ac4b7a8a82fcc1716496d)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/systm.h>
31 #include <sys/bootconf.h>
32 #include <sys/thread.h>
33 #include <sys/ddi.h>
34 #include <sys/sunddi.h>
35 #include <vm/seg_kmem.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 = 400;
43 static int cursor_x = 210;
44 static uchar_t bar[4][32];
45 static kthread_t *progressbar_tid;
46 static kmutex_t pbar_lock;
47 static kcondvar_t pbar_cv;
48 static char *videomem = (caddr_t)VIDEOMEM;
49 static int videomem_size;
50 
51 static void
52 mapmask(int value)
53 {
54 	outb(0x3c4, 2);
55 	outb(0x3c5, value);
56 }
57 
58 static void
59 bitmask(int value)
60 {
61 	outb(0x3ce, 8);
62 	outb(0x3cf, value);
63 }
64 
65 static void
66 progressbar_show(void)
67 {
68 	int i, j, k, offset;
69 	uchar_t *mem, *ptr;
70 
71 	offset = cursor_y * 80 + cursor_x / 8;
72 	mem = (uchar_t *)videomem + offset;
73 
74 	for (i = 0; i < 4; i++) {
75 		mapmask(1 << i);
76 		for (j = 0; j < 16; j++) {   /* bar height 16 pixel */
77 			ptr = mem + j * 80;
78 			for (k = 0; k < 32; k++, ptr++)
79 				*ptr = bar[i][k];
80 		}
81 	}
82 	mapmask(15);
83 }
84 
85 /*
86  * Initialize a rectangle area for progress bar
87  *
88  * Multiboot has initialized graphics mode to 640x480
89  * with 16 colors.
90  */
91 void
92 progressbar_init()
93 {
94 	int i;
95 	char cons[10];
96 
97 	/* see if we are in graphics mode */
98 	if (BOP_GETPROPLEN(bootops, "console") != sizeof ("graphics"))
99 		return;
100 	(void) BOP_GETPROP(bootops, "console", cons);
101 	if (strncmp(cons, "graphics", strlen("graphics")) != 0)
102 		return;
103 
104 	graphics_mode = 1;
105 	bitmask(0xff);
106 
107 	for (i = 0; i < 32; i++) {
108 		bar[0][i] = bar[1][i] = 0xf0;
109 		bar[2][i] = bar[3][i] = 0xf0;
110 	}
111 
112 	progressbar_show();
113 }
114 
115 static void
116 progressbar_step()
117 {
118 	static int limit = 0;
119 	int i;
120 
121 	if (limit == 0) {	/* reset */
122 		for (i = 0; i < 32; i++)
123 			bar[3][i] = 0xf0;
124 	}
125 	bar[3][limit] = 0xff;
126 	limit++;
127 	if (limit == 32)
128 		limit = 0;
129 
130 	progressbar_show();
131 }
132 
133 /*ARGSUSED*/
134 static void
135 progressbar_thread(void *arg)
136 {
137 	clock_t end;
138 
139 	mutex_enter(&pbar_lock);
140 	while (graphics_mode) {
141 		progressbar_step();
142 		end = ddi_get_lbolt() + drv_usectohz(200000);
143 		(void) cv_timedwait(&pbar_cv, &pbar_lock, end);
144 	}
145 	mutex_exit(&pbar_lock);
146 }
147 
148 void
149 progressbar_start(void)
150 {
151 	extern pri_t minclsyspri;
152 
153 	if (graphics_mode == 0)
154 		return;
155 
156 	/* map video memory to kernel heap */
157 	videomem_size = ptob(btopr(38400));	/* 640 x 480 / 8 bytes */
158 	videomem = vmem_alloc(heap_arena, videomem_size, VM_SLEEP);
159 	if (videomem == NULL) {
160 		cmn_err(CE_NOTE, "!failed to start progress bar");
161 		graphics_mode = 0;
162 		return;
163 	}
164 	hat_devload(kas.a_hat, videomem, videomem_size,
165 	    btop(VIDEOMEM), (PROT_READ | PROT_WRITE),
166 	    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
167 
168 	progressbar_tid = thread_create(NULL, 0, progressbar_thread,
169 	    NULL, 0, &p0, TS_RUN, minclsyspri);
170 }
171 
172 void
173 progressbar_stop(void)
174 {
175 	if (graphics_mode == 0)
176 		return;
177 
178 	graphics_mode = 0;
179 	mutex_enter(&pbar_lock);
180 	cv_signal(&pbar_cv);
181 	mutex_exit(&pbar_lock);
182 	if (progressbar_tid != NULL)
183 		thread_join(progressbar_tid->t_did);
184 
185 	/* unmap video memory */
186 	hat_unload(kas.a_hat, videomem, videomem_size, HAT_UNLOAD_UNLOCK);
187 	vmem_free(heap_arena, videomem, videomem_size);
188 }
189