Merge branch 'vendor/DHCPCD'
[dragonfly.git] / sys / emulation / ndis / subr_hal.c
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  * $FreeBSD: src/sys/compat/ndis/subr_hal.c,v 1.34 2012/11/17 01:51:26 svnexp Exp $
33  */
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/errno.h>
38
39 #include <sys/callout.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/proc.h>
44 #include <sys/sched.h>
45 #include <sys/module.h>
46
47 #include <sys/systm.h>
48
49 #include <sys/bus.h>
50 #include <sys/rman.h>
51
52 #include <emulation/ndis/pe_var.h>
53 #include <emulation/ndis/resource_var.h>
54 #include <emulation/ndis/cfg_var.h>
55 #include <emulation/ndis/ntoskrnl_var.h>
56 #include <emulation/ndis/hal_var.h>
57
58 static void KeStallExecutionProcessor(uint32_t);
59 static void WRITE_PORT_BUFFER_ULONG(uint32_t *,
60         uint32_t *, uint32_t);
61 static void WRITE_PORT_BUFFER_USHORT(uint16_t *,
62         uint16_t *, uint32_t);
63 static void WRITE_PORT_BUFFER_UCHAR(uint8_t *,
64         uint8_t *, uint32_t);
65 static void WRITE_PORT_ULONG(uint32_t *, uint32_t);
66 static void WRITE_PORT_USHORT(uint16_t *, uint16_t);
67 static void WRITE_PORT_UCHAR(uint8_t *, uint8_t);
68 static uint32_t READ_PORT_ULONG(uint32_t *);
69 static uint16_t READ_PORT_USHORT(uint16_t *);
70 static uint8_t READ_PORT_UCHAR(uint8_t *);
71 static void READ_PORT_BUFFER_ULONG(uint32_t *,
72         uint32_t *, uint32_t);
73 static void READ_PORT_BUFFER_USHORT(uint16_t *,
74         uint16_t *, uint32_t);
75 static void READ_PORT_BUFFER_UCHAR(uint8_t *,
76         uint8_t *, uint32_t);
77 static uint64_t KeQueryPerformanceCounter(uint64_t *);
78 static void _KeLowerIrql(uint8_t);
79 static uint8_t KeRaiseIrqlToDpcLevel(void);
80 static void dummy (void);
81
82 #define NDIS_MAXCPUS 64
83 static struct lock disp_lock[NDIS_MAXCPUS];
84
85 int
86 hal_libinit(void)
87 {
88         image_patch_table       *patch;
89         int                     i;
90
91         for (i = 0; i < NDIS_MAXCPUS; i++)
92                 lockinit(&disp_lock[i], "HAL preemption lock", 0,
93                     LK_CANRECURSE);
94
95         patch = hal_functbl;
96         while (patch->ipt_func != NULL) {
97                 windrv_wrap((funcptr)patch->ipt_func,
98                     (funcptr *)&patch->ipt_wrap,
99                     patch->ipt_argcnt, patch->ipt_ftype);
100                 patch++;
101         }
102
103         return (0);
104 }
105
106 int
107 hal_libfini(void)
108 {
109         image_patch_table       *patch;
110         int                     i;
111
112         for (i = 0; i < NDIS_MAXCPUS; i++)
113                 lockuninit(&disp_lock[i]);
114
115         patch = hal_functbl;
116         while (patch->ipt_func != NULL) {
117                 windrv_unwrap(patch->ipt_wrap);
118                 patch++;
119         }
120
121         return (0);
122 }
123
124 static void
125 KeStallExecutionProcessor(uint32_t usecs)
126 {
127         DELAY(usecs);
128 }
129
130 static void
131 WRITE_PORT_ULONG(uint32_t *port, uint32_t val)
132 {
133         bus_space_write_4(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port, val);
134 }
135
136 static void
137 WRITE_PORT_USHORT(uint16_t *port, uint16_t val)
138 {
139         bus_space_write_2(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port, val);
140 }
141
142 static void
143 WRITE_PORT_UCHAR(uint8_t *port, uint8_t val)
144 {
145         bus_space_write_1(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port, val);
146 }
147
148 static void
149 WRITE_PORT_BUFFER_ULONG(uint32_t *port, uint32_t *val, uint32_t cnt)
150 {
151         bus_space_write_multi_4(NDIS_BUS_SPACE_IO, 0x0,
152             (bus_size_t)port, val, cnt);
153 }
154
155 static void
156 WRITE_PORT_BUFFER_USHORT(uint16_t *port, uint16_t *val, uint32_t cnt)
157 {
158         bus_space_write_multi_2(NDIS_BUS_SPACE_IO, 0x0,
159             (bus_size_t)port, val, cnt);
160 }
161
162 static void
163 WRITE_PORT_BUFFER_UCHAR(uint8_t *port, uint8_t *val, uint32_t cnt)
164 {
165         bus_space_write_multi_1(NDIS_BUS_SPACE_IO, 0x0,
166             (bus_size_t)port, val, cnt);
167 }
168
169 static uint16_t
170 READ_PORT_USHORT(uint16_t *port)
171 {
172         return (bus_space_read_2(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port));
173 }
174
175 static uint32_t
176 READ_PORT_ULONG(uint32_t *port)
177 {
178         return (bus_space_read_4(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port));
179 }
180
181 static uint8_t
182 READ_PORT_UCHAR(uint8_t *port)
183 {
184         return (bus_space_read_1(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port));
185 }
186
187 static void
188 READ_PORT_BUFFER_ULONG(uint32_t *port, uint32_t *val, uint32_t cnt)
189 {
190         bus_space_read_multi_4(NDIS_BUS_SPACE_IO, 0x0,
191             (bus_size_t)port, val, cnt);
192 }
193
194 static void
195 READ_PORT_BUFFER_USHORT(uint16_t *port, uint16_t *val, uint32_t cnt)
196 {
197         bus_space_read_multi_2(NDIS_BUS_SPACE_IO, 0x0,
198             (bus_size_t)port, val, cnt);
199 }
200
201 static void
202 READ_PORT_BUFFER_UCHAR(uint8_t *port, uint8_t *val, uint32_t cnt)
203 {
204         bus_space_read_multi_1(NDIS_BUS_SPACE_IO, 0x0,
205             (bus_size_t)port, val, cnt);
206 }
207
208 /*
209  * The spinlock implementation in Windows differs from that of FreeBSD.
210  * The basic operation of spinlocks involves two steps: 1) spin in a
211  * tight loop while trying to acquire a lock, 2) after obtaining the
212  * lock, disable preemption. (Note that on uniprocessor systems, you're
213  * allowed to skip the first step and just lock out pre-emption, since
214  * it's not possible for you to be in contention with another running
215  * thread.) Later, you release the lock then re-enable preemption.
216  * The difference between Windows and FreeBSD lies in how preemption
217  * is disabled. In FreeBSD, it's done using critical_enter(), which on
218  * the x86 arch translates to a cli instruction. This masks off all
219  * interrupts, and effectively stops the scheduler from ever running
220  * so _nothing_ can execute except the current thread. In Windows,
221  * preemption is disabled by raising the processor IRQL to DISPATCH_LEVEL.
222  * This stops other threads from running, but does _not_ block device
223  * interrupts. This means ISRs can still run, and they can make other
224  * threads runable, but those other threads won't be able to execute
225  * until the current thread lowers the IRQL to something less than
226  * DISPATCH_LEVEL.
227  *
228  * There's another commonly used IRQL in Windows, which is APC_LEVEL.
229  * An APC is an Asynchronous Procedure Call, which differs from a DPC
230  * (Defered Procedure Call) in that a DPC is queued up to run in
231  * another thread, while an APC runs in the thread that scheduled
232  * it (similar to a signal handler in a UNIX process). We don't
233  * actually support the notion of APCs in FreeBSD, so for now, the
234  * only IRQLs we're interested in are DISPATCH_LEVEL and PASSIVE_LEVEL.
235  *
236  * To simulate DISPATCH_LEVEL, we raise the current thread's priority
237  * to TDPRI_INT_HIGH, which is the highest we can give it. This should,
238  * if I understand things correctly, prevent anything except for an
239  * interrupt thread from preempting us. PASSIVE_LEVEL is basically
240  * everything else.
241  *
242  * Be aware that, at least on the x86 arch, the Windows spinlock
243  * functions are divided up in peculiar ways. The actual spinlock
244  * functions are KfAcquireSpinLock() and KfReleaseSpinLock(), and
245  * they live in HAL.dll. Meanwhile, KeInitializeSpinLock(),
246  * KefAcquireSpinLockAtDpcLevel() and KefReleaseSpinLockFromDpcLevel()
247  * live in ntoskrnl.exe. Most Windows source code will call
248  * KeAcquireSpinLock() and KeReleaseSpinLock(), but these are just
249  * macros that call KfAcquireSpinLock() and KfReleaseSpinLock().
250  * KefAcquireSpinLockAtDpcLevel() and KefReleaseSpinLockFromDpcLevel()
251  * perform the lock aquisition/release functions without doing the
252  * IRQL manipulation, and are used when one is already running at
253  * DISPATCH_LEVEL. Make sense? Good.
254  *
255  * According to the Microsoft documentation, any thread that calls
256  * KeAcquireSpinLock() must be running at IRQL <= DISPATCH_LEVEL. If
257  * we detect someone trying to acquire a spinlock from DEVICE_LEVEL
258  * or HIGH_LEVEL, we panic.
259  *
260  * Alternate sleep-lock-based spinlock implementation
261  * --------------------------------------------------
262  *
263  * The earlier spinlock implementation was arguably a bit of a hack
264  * and presented several problems. It was basically designed to provide
265  * the functionality of spinlocks without incurring the wrath of
266  * WITNESS. We could get away with using both our spinlock implementation
267  * and FreeBSD sleep locks at the same time, but if WITNESS knew what
268  * we were really up to, it would have spanked us rather severely.
269  *
270  * There's another method we can use based entirely on sleep locks.
271  * First, it's important to realize that everything we're locking
272  * resides inside Project Evil itself: any critical data being locked
273  * by drivers belongs to the drivers, and should not be referenced
274  * by any other OS code outside of the NDISulator. The priority-based
275  * locking scheme has system-wide effects, just like real spinlocks
276  * (blocking preemption affects the whole CPU), but since we keep all
277  * our critical data private, we can use a simpler mechanism that
278  * affects only code/threads directly related to Project Evil.
279  *
280  * The idea is to create a sleep lock mutex for each CPU in the system.
281  * When a CPU running in the NDISulator wants to acquire a spinlock, it
282  * does the following:
283  * - Pin ourselves to the current CPU
284  * - Acquire the mutex for the current CPU
285  * - Spin on the spinlock variable using atomic test and set, just like
286  *   a real spinlock.
287  * - Once we have the lock, we execute our critical code
288  *
289  * To give up the lock, we do:
290  * - Clear the spinlock variable with an atomic op
291  * - Release the per-CPU mutex
292  * - Unpin ourselves from the current CPU.
293  *
294  * On a uniprocessor system, this means all threads that access protected
295  * data are serialized through the per-CPU mutex. After one thread
296  * acquires the 'spinlock,' any other thread that uses a spinlock on the
297  * current CPU will block on the per-CPU mutex, which has the same general
298  * effect of blocking pre-emption, but _only_ for those threads that are
299  * running NDISulator code.
300  *
301  * On a multiprocessor system, threads on different CPUs all block on
302  * their respective per-CPU mutex, and the atomic test/set operation
303  * on the spinlock variable provides inter-CPU synchronization, though
304  * only for threads running NDISulator code.
305  *
306  * This method solves an important problem. In Windows, you're allowed
307  * to do an ExAllocatePoolWithTag() with a spinlock held, provided you
308  * allocate from NonPagedPool. This implies an atomic heap allocation
309  * that will not cause the current thread to sleep. (You can't sleep
310  * while holding real spinlock: clowns will eat you.) But in FreeBSD,
311  * malloc(9) _always_ triggers the acquisition of a sleep lock, even
312  * when you use M_NOWAIT. This is not a problem for FreeBSD native
313  * code: you're allowed to sleep in things like interrupt threads. But
314  * it is a problem with the old priority-based spinlock implementation:
315  * even though we get away with it most of the time, we really can't
316  * do a malloc(9) after doing a KeAcquireSpinLock() or KeRaiseIrql().
317  * With the new implementation, it's not a problem: you're allowed to
318  * acquire more than one sleep lock (as long as you avoid lock order
319  * reversals).
320  *
321  * The one drawback to this approach is that now we have a lot of
322  * contention on one per-CPU mutex within the NDISulator code. Whether
323  * or not this is preferable to the expected Windows spinlock behavior
324  * of blocking pre-emption is debatable.
325  */
326
327 uint8_t
328 KfAcquireSpinLock(kspin_lock *lock)
329 {
330         uint8_t                 oldirql;
331
332         KeRaiseIrql(DISPATCH_LEVEL, &oldirql);
333         KeAcquireSpinLockAtDpcLevel(lock);
334
335         return (oldirql);
336 }
337
338 void
339 KfReleaseSpinLock(kspin_lock *lock, uint8_t newirql)
340 {
341         KeReleaseSpinLockFromDpcLevel(lock);
342         KeLowerIrql(newirql);
343 }
344
345 uint8_t
346 KeGetCurrentIrql(void)
347 {
348         if (lockstatus(&disp_lock[curthread->td_gd->gd_cpuid], curthread))
349                 return (DISPATCH_LEVEL);
350         return (PASSIVE_LEVEL);
351 }
352
353 static uint64_t
354 KeQueryPerformanceCounter(uint64_t *freq)
355 {
356         if (freq != NULL)
357                 *freq = hz;
358
359         return ((uint64_t)ticks);
360 }
361
362 uint8_t
363 KfRaiseIrql(uint8_t irql)
364 {
365         uint8_t                 oldirql;
366
367 #if 0 /* XXX swildner */
368                 sched_pin();
369 #endif
370         oldirql = KeGetCurrentIrql();
371
372         /* I am so going to hell for this. */
373         if (oldirql > irql)
374                 panic("IRQL_NOT_LESS_THAN_OR_EQUAL");
375
376         if (oldirql != DISPATCH_LEVEL)
377                 lockmgr(&disp_lock[curthread->td_gd->gd_cpuid], LK_EXCLUSIVE);
378 #if 0 /* XXX swildner */
379         else
380                 sched_unpin();
381 #endif
382
383 /*kprintf("RAISE IRQL: %d %d\n", irql, oldirql);*/
384
385         return (oldirql);
386 }
387
388 void
389 KfLowerIrql(uint8_t oldirql)
390 {
391         if (oldirql == DISPATCH_LEVEL)
392                 return;
393
394         if (KeGetCurrentIrql() != DISPATCH_LEVEL)
395                 panic("IRQL_NOT_GREATER_THAN");
396
397         lockmgr(&disp_lock[curthread->td_gd->gd_cpuid], LK_RELEASE);
398 #if 0 /* XXX swildner */
399         sched_unpin();
400 #endif
401 }
402
403 static uint8_t
404 KeRaiseIrqlToDpcLevel(void)
405 {
406         uint8_t                 irql;
407
408         KeRaiseIrql(DISPATCH_LEVEL, &irql);
409         return (irql);
410 }
411
412 static void
413 _KeLowerIrql(uint8_t oldirql)
414 {
415         KeLowerIrql(oldirql);
416 }
417
418 static void dummy(void)
419 {
420         kprintf("hal dummy called...\n");
421 }
422
423 image_patch_table hal_functbl[] = {
424         IMPORT_SFUNC(KeStallExecutionProcessor, 1),
425         IMPORT_SFUNC(WRITE_PORT_ULONG, 2),
426         IMPORT_SFUNC(WRITE_PORT_USHORT, 2),
427         IMPORT_SFUNC(WRITE_PORT_UCHAR, 2),
428         IMPORT_SFUNC(WRITE_PORT_BUFFER_ULONG, 3),
429         IMPORT_SFUNC(WRITE_PORT_BUFFER_USHORT, 3),
430         IMPORT_SFUNC(WRITE_PORT_BUFFER_UCHAR, 3),
431         IMPORT_SFUNC(READ_PORT_ULONG, 1),
432         IMPORT_SFUNC(READ_PORT_USHORT, 1),
433         IMPORT_SFUNC(READ_PORT_UCHAR, 1),
434         IMPORT_SFUNC(READ_PORT_BUFFER_ULONG, 3),
435         IMPORT_SFUNC(READ_PORT_BUFFER_USHORT, 3),
436         IMPORT_SFUNC(READ_PORT_BUFFER_UCHAR, 3),
437         IMPORT_FFUNC(KfAcquireSpinLock, 1),
438         IMPORT_FFUNC(KfReleaseSpinLock, 1),
439         IMPORT_SFUNC(KeGetCurrentIrql, 0),
440         IMPORT_SFUNC(KeQueryPerformanceCounter, 1),
441         IMPORT_FFUNC(KfLowerIrql, 1),
442         IMPORT_FFUNC(KfRaiseIrql, 1),
443         IMPORT_SFUNC(KeRaiseIrqlToDpcLevel, 0),
444 #undef KeLowerIrql
445         IMPORT_SFUNC_MAP(KeLowerIrql, _KeLowerIrql, 1),
446
447         /*
448          * This last entry is a catch-all for any function we haven't
449          * implemented yet. The PE import list patching routine will
450          * use it for any function that doesn't have an explicit match
451          * in this table.
452          */
453
454         { NULL, (FUNC)dummy, NULL, 0, WINDRV_WRAP_STDCALL },
455
456         /* End of list. */
457
458         { NULL, NULL, NULL }
459 };