subr_hal.c revision 146015
1/*-
2 * Copyright (c) 2003
3 *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/compat/ndis/subr_hal.c 146015 2005-05-08 23:07:51Z wpaul $");
35
36#include <sys/param.h>
37#include <sys/types.h>
38#include <sys/errno.h>
39
40#include <sys/callout.h>
41#include <sys/kernel.h>
42#include <sys/lock.h>
43#include <sys/mutex.h>
44#include <sys/proc.h>
45#include <sys/sched.h>
46#include <sys/module.h>
47
48#include <sys/systm.h>
49#include <machine/clock.h>
50#include <machine/bus_memio.h>
51#include <machine/bus_pio.h>
52#include <machine/bus.h>
53
54#include <sys/bus.h>
55#include <sys/rman.h>
56
57#include <compat/ndis/pe_var.h>
58#include <compat/ndis/resource_var.h>
59#include <compat/ndis/cfg_var.h>
60#include <compat/ndis/ntoskrnl_var.h>
61#include <compat/ndis/hal_var.h>
62
63static void KeStallExecutionProcessor(uint32_t);
64static void WRITE_PORT_BUFFER_ULONG(uint32_t *,
65	uint32_t *, uint32_t);
66static void WRITE_PORT_BUFFER_USHORT(uint16_t *,
67	uint16_t *, uint32_t);
68static void WRITE_PORT_BUFFER_UCHAR(uint8_t *,
69	uint8_t *, uint32_t);
70static void WRITE_PORT_ULONG(uint32_t *, uint32_t);
71static void WRITE_PORT_USHORT(uint16_t *, uint16_t);
72static void WRITE_PORT_UCHAR(uint8_t *, uint8_t);
73static uint32_t READ_PORT_ULONG(uint32_t *);
74static uint16_t READ_PORT_USHORT(uint16_t *);
75static uint8_t READ_PORT_UCHAR(uint8_t *);
76static void READ_PORT_BUFFER_ULONG(uint32_t *,
77	uint32_t *, uint32_t);
78static void READ_PORT_BUFFER_USHORT(uint16_t *,
79	uint16_t *, uint32_t);
80static void READ_PORT_BUFFER_UCHAR(uint8_t *,
81	uint8_t *, uint32_t);
82static uint64_t KeQueryPerformanceCounter(uint64_t *);
83static void dummy (void);
84
85extern struct mtx_pool *ndis_mtxpool;
86
87int
88hal_libinit()
89{
90	image_patch_table	*patch;
91
92	patch = hal_functbl;
93	while (patch->ipt_func != NULL) {
94		windrv_wrap((funcptr)patch->ipt_func,
95		    (funcptr *)&patch->ipt_wrap,
96		    patch->ipt_argcnt, patch->ipt_ftype);
97		patch++;
98	}
99
100	return(0);
101}
102
103int
104hal_libfini()
105{
106	image_patch_table	*patch;
107
108	patch = hal_functbl;
109	while (patch->ipt_func != NULL) {
110		windrv_unwrap(patch->ipt_wrap);
111		patch++;
112	}
113
114	return(0);
115}
116
117static void
118KeStallExecutionProcessor(usecs)
119	uint32_t		usecs;
120{
121	DELAY(usecs);
122	return;
123}
124
125static void
126WRITE_PORT_ULONG(port, val)
127	uint32_t		*port;
128	uint32_t		val;
129{
130	bus_space_write_4(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port, val);
131	return;
132}
133
134static void
135WRITE_PORT_USHORT(port, val)
136	uint16_t		*port;
137	uint16_t		val;
138{
139	bus_space_write_2(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port, val);
140	return;
141}
142
143static void
144WRITE_PORT_UCHAR(port, val)
145	uint8_t			*port;
146	uint8_t			val;
147{
148	bus_space_write_1(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port, val);
149	return;
150}
151
152static void
153WRITE_PORT_BUFFER_ULONG(port, val, cnt)
154	uint32_t		*port;
155	uint32_t		*val;
156	uint32_t		cnt;
157{
158	bus_space_write_multi_4(NDIS_BUS_SPACE_IO, 0x0,
159	    (bus_size_t)port, val, cnt);
160	return;
161}
162
163static void
164WRITE_PORT_BUFFER_USHORT(port, val, cnt)
165	uint16_t		*port;
166	uint16_t		*val;
167	uint32_t		cnt;
168{
169	bus_space_write_multi_2(NDIS_BUS_SPACE_IO, 0x0,
170	    (bus_size_t)port, val, cnt);
171	return;
172}
173
174static void
175WRITE_PORT_BUFFER_UCHAR(port, val, cnt)
176	uint8_t			*port;
177	uint8_t			*val;
178	uint32_t		cnt;
179{
180	bus_space_write_multi_1(NDIS_BUS_SPACE_IO, 0x0,
181	    (bus_size_t)port, val, cnt);
182	return;
183}
184
185static uint16_t
186READ_PORT_USHORT(port)
187	uint16_t		*port;
188{
189	return(bus_space_read_2(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port));
190}
191
192static uint32_t
193READ_PORT_ULONG(port)
194	uint32_t		*port;
195{
196	return(bus_space_read_4(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port));
197}
198
199static uint8_t
200READ_PORT_UCHAR(port)
201	uint8_t			*port;
202{
203	return(bus_space_read_1(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port));
204}
205
206static void
207READ_PORT_BUFFER_ULONG(port, val, cnt)
208	uint32_t		*port;
209	uint32_t		*val;
210	uint32_t		cnt;
211{
212	bus_space_read_multi_4(NDIS_BUS_SPACE_IO, 0x0,
213	    (bus_size_t)port, val, cnt);
214	return;
215}
216
217static void
218READ_PORT_BUFFER_USHORT(port, val, cnt)
219	uint16_t		*port;
220	uint16_t		*val;
221	uint32_t		cnt;
222{
223	bus_space_read_multi_2(NDIS_BUS_SPACE_IO, 0x0,
224	    (bus_size_t)port, val, cnt);
225	return;
226}
227
228static void
229READ_PORT_BUFFER_UCHAR(port, val, cnt)
230	uint8_t			*port;
231	uint8_t			*val;
232	uint32_t		cnt;
233{
234	bus_space_read_multi_1(NDIS_BUS_SPACE_IO, 0x0,
235	    (bus_size_t)port, val, cnt);
236	return;
237}
238
239/*
240 * The spinlock implementation in Windows differs from that of FreeBSD.
241 * The basic operation of spinlocks involves two steps: 1) spin in a
242 * tight loop while trying to acquire a lock, 2) after obtaining the
243 * lock, disable preemption. (Note that on uniprocessor systems, you're
244 * allowed to skip the first step and just lock out pre-emption, since
245 * it's not possible for you to be in contention with another running
246 * thread.) Later, you release the lock then re-enable preemption.
247 * The difference between Windows and FreeBSD lies in how preemption
248 * is disabled. In FreeBSD, it's done using critical_enter(), which on
249 * the x86 arch translates to a cli instruction. This masks off all
250 * interrupts, and effectively stops the scheduler from ever running
251 * so _nothing_ can execute except the current thread. In Windows,
252 * preemption is disabled by raising the processor IRQL to DISPATCH_LEVEL.
253 * This stops other threads from running, but does _not_ block device
254 * interrupts. This means ISRs can still run, and they can make other
255 * threads runable, but those other threads won't be able to execute
256 * until the current thread lowers the IRQL to something less than
257 * DISPATCH_LEVEL.
258 *
259 * There's another commonly used IRQL in Windows, which is APC_LEVEL.
260 * An APC is an Asynchronous Procedure Call, which differs from a DPC
261 * (Defered Procedure Call) in that a DPC is queued up to run in
262 * another thread, while an APC runs in the thread that scheduled
263 * it (similar to a signal handler in a UNIX process). We don't
264 * actually support the notion of APCs in FreeBSD, so for now, the
265 * only IRQLs we're interested in are DISPATCH_LEVEL and PASSIVE_LEVEL.
266 *
267 * To simulate DISPATCH_LEVEL, we raise the current thread's priority
268 * to PI_REALTIME, which is the highest we can give it. This should,
269 * if I understand things correctly, prevent anything except for an
270 * interrupt thread from preempting us. PASSIVE_LEVEL is basically
271 * everything else.
272 *
273 * Be aware that, at least on the x86 arch, the Windows spinlock
274 * functions are divided up in peculiar ways. The actual spinlock
275 * functions are KfAcquireSpinLock() and KfReleaseSpinLock(), and
276 * they live in HAL.dll. Meanwhile, KeInitializeSpinLock(),
277 * KefAcquireSpinLockAtDpcLevel() and KefReleaseSpinLockFromDpcLevel()
278 * live in ntoskrnl.exe. Most Windows source code will call
279 * KeAcquireSpinLock() and KeReleaseSpinLock(), but these are just
280 * macros that call KfAcquireSpinLock() and KfReleaseSpinLock().
281 * KefAcquireSpinLockAtDpcLevel() and KefReleaseSpinLockFromDpcLevel()
282 * perform the lock aquisition/release functions without doing the
283 * IRQL manipulation, and are used when one is already running at
284 * DISPATCH_LEVEL. Make sense? Good.
285 *
286 * According to the Microsoft documentation, any thread that calls
287 * KeAcquireSpinLock() must be running at IRQL <= DISPATCH_LEVEL. If
288 * we detect someone trying to acquire a spinlock from DEVICE_LEVEL
289 * or HIGH_LEVEL, we panic.
290 */
291
292uint8_t
293KfAcquireSpinLock(lock)
294	kspin_lock		*lock;
295{
296	uint8_t			oldirql;
297
298	/* I am so going to hell for this. */
299	if (KeGetCurrentIrql() > DISPATCH_LEVEL)
300		panic("IRQL_NOT_LESS_THAN_OR_EQUAL");
301
302	oldirql = KeRaiseIrql(DISPATCH_LEVEL);
303	KeAcquireSpinLockAtDpcLevel(lock);
304
305	return(oldirql);
306}
307
308void
309KfReleaseSpinLock(lock, newirql)
310	kspin_lock		*lock;
311	uint8_t			newirql;
312{
313	KeReleaseSpinLockFromDpcLevel(lock);
314	KeLowerIrql(newirql);
315
316	return;
317}
318
319 uint8_t
320KeGetCurrentIrql()
321{
322	if (AT_DISPATCH_LEVEL(curthread))
323		return(DISPATCH_LEVEL);
324	return(PASSIVE_LEVEL);
325}
326
327static uint64_t
328KeQueryPerformanceCounter(freq)
329	uint64_t		*freq;
330{
331	if (freq != NULL)
332		*freq = hz;
333
334	return((uint64_t)ticks);
335}
336
337uint8_t
338KfRaiseIrql(irql)
339	uint8_t			irql;
340{
341	uint8_t			oldirql;
342
343	if (irql < KeGetCurrentIrql())
344		panic("IRQL_NOT_LESS_THAN");
345
346	if (KeGetCurrentIrql() == DISPATCH_LEVEL)
347		return(DISPATCH_LEVEL);
348
349	mtx_lock_spin(&sched_lock);
350	oldirql = curthread->td_base_pri;
351	sched_prio(curthread, PI_REALTIME);
352#if __FreeBSD_version < 600000
353	curthread->td_base_pri = PI_REALTIME;
354#endif
355	mtx_unlock_spin(&sched_lock);
356
357	return(oldirql);
358}
359
360void
361KfLowerIrql(oldirql)
362	uint8_t			oldirql;
363{
364	if (oldirql == DISPATCH_LEVEL)
365		return;
366
367	if (KeGetCurrentIrql() != DISPATCH_LEVEL)
368		panic("IRQL_NOT_GREATER_THAN");
369
370	mtx_lock_spin(&sched_lock);
371#if __FreeBSD_version < 600000
372	curthread->td_base_pri = oldirql;
373#endif
374	sched_prio(curthread, oldirql);
375	mtx_unlock_spin(&sched_lock);
376
377	return;
378}
379
380static void dummy()
381{
382	printf ("hal dummy called...\n");
383	return;
384}
385
386image_patch_table hal_functbl[] = {
387	IMPORT_SFUNC(KeStallExecutionProcessor, 1),
388	IMPORT_SFUNC(WRITE_PORT_ULONG, 2),
389	IMPORT_SFUNC(WRITE_PORT_USHORT, 2),
390	IMPORT_SFUNC(WRITE_PORT_UCHAR, 2),
391	IMPORT_SFUNC(WRITE_PORT_BUFFER_ULONG, 3),
392	IMPORT_SFUNC(WRITE_PORT_BUFFER_USHORT, 3),
393	IMPORT_SFUNC(WRITE_PORT_BUFFER_UCHAR, 3),
394	IMPORT_SFUNC(READ_PORT_ULONG, 1),
395	IMPORT_SFUNC(READ_PORT_USHORT, 1),
396	IMPORT_SFUNC(READ_PORT_UCHAR, 1),
397	IMPORT_SFUNC(READ_PORT_BUFFER_ULONG, 3),
398	IMPORT_SFUNC(READ_PORT_BUFFER_USHORT, 3),
399	IMPORT_SFUNC(READ_PORT_BUFFER_UCHAR, 3),
400	IMPORT_FFUNC(KfAcquireSpinLock, 1),
401	IMPORT_FFUNC(KfReleaseSpinLock, 1),
402	IMPORT_SFUNC(KeGetCurrentIrql, 0),
403	IMPORT_SFUNC(KeQueryPerformanceCounter, 1),
404	IMPORT_FFUNC(KfLowerIrql, 1),
405	IMPORT_FFUNC(KfRaiseIrql, 1),
406
407	/*
408	 * This last entry is a catch-all for any function we haven't
409	 * implemented yet. The PE import list patching routine will
410	 * use it for any function that doesn't have an explicit match
411	 * in this table.
412	 */
413
414	{ NULL, (FUNC)dummy, NULL, 0, WINDRV_WRAP_STDCALL },
415
416	/* End of list. */
417
418	{ NULL, NULL, NULL }
419};
420