subr_hal.c revision 124409
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 124409 2004-01-12 03:49:20Z 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
45#include <sys/systm.h>
46#include <machine/clock.h>
47#include <machine/bus_memio.h>
48#include <machine/bus_pio.h>
49#include <machine/bus.h>
50
51#include <sys/bus.h>
52#include <sys/rman.h>
53
54#include <compat/ndis/pe_var.h>
55#include <compat/ndis/hal_var.h>
56#include <compat/ndis/ntoskrnl_var.h>
57
58#define __stdcall __attribute__((__stdcall__))
59#define FUNC void(*)(void)
60
61__stdcall static void hal_stall_exec_cpu(uint32_t);
62__stdcall static void hal_writeport_buf_ulong(uint32_t *,
63	uint32_t *, uint32_t);
64__stdcall static void hal_writeport_buf_ushort(uint16_t *,
65	uint16_t *, uint32_t);
66__stdcall static void hal_writeport_buf_uchar(uint8_t *,
67	uint8_t *, uint32_t);
68__stdcall static void hal_writeport_ulong(uint32_t *, uint32_t);
69__stdcall static void hal_writeport_ushort(uint16_t *, uint16_t);
70__stdcall static void hal_writeport_uchar(uint8_t *, uint8_t);
71__stdcall static uint32_t hal_readport_ulong(uint32_t *);
72__stdcall static uint16_t hal_readport_ushort(uint16_t *);
73__stdcall static uint8_t hal_readport_uchar(uint8_t *);
74__stdcall static void hal_readport_buf_ulong(uint32_t *,
75	uint32_t *, uint32_t);
76__stdcall static void hal_readport_buf_ushort(uint16_t *,
77	uint16_t *, uint32_t);
78__stdcall static void hal_readport_buf_uchar(uint8_t *,
79	uint8_t *, uint32_t);
80__stdcall static uint8_t hal_lock(/*kspin_lock * */void);
81__stdcall static void hal_unlock(/*kspin_lock *, uint8_t*/void);
82__stdcall static uint8_t hal_irql(void);
83__stdcall static void dummy (void);
84
85extern struct mtx_pool *ndis_mtxpool;
86
87__stdcall static void
88hal_stall_exec_cpu(usecs)
89	uint32_t		usecs;
90{
91	DELAY(usecs);
92	return;
93}
94
95__stdcall static void
96hal_writeport_ulong(port, val)
97	uint32_t		*port;
98	uint32_t		val;
99{
100	bus_space_write_4(I386_BUS_SPACE_IO, 0x0, (uint32_t)port, val);
101	return;
102}
103
104__stdcall static void
105hal_writeport_ushort(port, val)
106	uint16_t		*port;
107	uint16_t		val;
108{
109	bus_space_write_2(I386_BUS_SPACE_IO, 0x0, (uint32_t)port, val);
110	return;
111}
112
113__stdcall static void
114hal_writeport_uchar(port, val)
115	uint8_t			*port;
116	uint8_t			val;
117{
118	bus_space_write_1(I386_BUS_SPACE_IO, 0x0, (uint32_t)port, val);
119	return;
120}
121
122__stdcall static void
123hal_writeport_buf_ulong(port, val, cnt)
124	uint32_t		*port;
125	uint32_t		*val;
126	uint32_t		cnt;
127{
128	bus_space_write_multi_4(I386_BUS_SPACE_IO, 0x0,
129	    (bus_size_t)port, val, cnt);
130	return;
131}
132
133__stdcall static void
134hal_writeport_buf_ushort(port, val, cnt)
135	uint16_t		*port;
136	uint16_t		*val;
137	uint32_t		cnt;
138{
139	bus_space_write_multi_2(I386_BUS_SPACE_IO, 0x0,
140	    (bus_size_t)port, val, cnt);
141	return;
142}
143
144__stdcall static void
145hal_writeport_buf_uchar(port, val, cnt)
146	uint8_t			*port;
147	uint8_t			*val;
148	uint32_t		cnt;
149{
150	bus_space_write_multi_1(I386_BUS_SPACE_IO, 0x0,
151	    (bus_size_t)port, val, cnt);
152	return;
153}
154
155__stdcall static uint16_t
156hal_readport_ushort(port)
157	uint16_t		*port;
158{
159	return(bus_space_read_2(I386_BUS_SPACE_IO, 0x0, (uint32_t)port));
160}
161
162__stdcall static uint32_t
163hal_readport_ulong(port)
164	uint32_t		*port;
165{
166	return(bus_space_read_4(I386_BUS_SPACE_IO, 0x0, (uint32_t)port));
167}
168
169__stdcall static uint8_t
170hal_readport_uchar(port)
171	uint8_t			*port;
172{
173	return(bus_space_read_1(I386_BUS_SPACE_IO, 0x0, (uint32_t)port));
174}
175
176__stdcall static void
177hal_readport_buf_ulong(port, val, cnt)
178	uint32_t		*port;
179	uint32_t		*val;
180	uint32_t		cnt;
181{
182	bus_space_read_multi_4(I386_BUS_SPACE_IO, 0x0,
183	    (bus_size_t)port, val, cnt);
184	return;
185}
186
187__stdcall static void
188hal_readport_buf_ushort(port, val, cnt)
189	uint16_t		*port;
190	uint16_t		*val;
191	uint32_t		cnt;
192{
193	bus_space_read_multi_2(I386_BUS_SPACE_IO, 0x0,
194	    (bus_size_t)port, val, cnt);
195	return;
196}
197
198__stdcall static void
199hal_readport_buf_uchar(port, val, cnt)
200	uint8_t			*port;
201	uint8_t			*val;
202	uint32_t		cnt;
203{
204	bus_space_read_multi_1(I386_BUS_SPACE_IO, 0x0,
205	    (bus_size_t)port, val, cnt);
206	return;
207}
208
209__stdcall static uint8_t
210hal_lock(/*lock*/void)
211{
212	kspin_lock		*lock;
213
214	__asm__ __volatile__ ("" : "=c" (lock));
215
216	mtx_pool_lock(ndis_mtxpool, (struct mtx *)*lock);
217	return(0);
218}
219
220__stdcall static void
221hal_unlock(/*lock, newirql*/void)
222{
223	kspin_lock		*lock;
224	uint8_t			newiqrl;
225
226	__asm__ __volatile__ ("" : "=c" (lock), "=d" (newiqrl));
227
228	mtx_pool_unlock(ndis_mtxpool, (struct mtx *)*lock);
229	return;
230}
231
232__stdcall static uint8_t
233hal_irql(void)
234{
235	return(0);
236}
237
238__stdcall
239static void dummy()
240{
241	printf ("hal dummy called...\n");
242	return;
243}
244
245image_patch_table hal_functbl[] = {
246	{ "KeStallExecutionProcessor", (FUNC)hal_stall_exec_cpu },
247	{ "WRITE_PORT_ULONG", (FUNC)hal_writeport_ulong },
248	{ "WRITE_PORT_USHORT", (FUNC)hal_writeport_ushort },
249	{ "WRITE_PORT_UCHAR", (FUNC)hal_writeport_uchar },
250	{ "WRITE_PORT_BUFFER_ULONG", (FUNC)hal_writeport_buf_ulong },
251	{ "WRITE_PORT_BUFFER_USHORT", (FUNC)hal_writeport_buf_ushort },
252	{ "WRITE_PORT_BUFFER_UCHAR", (FUNC)hal_writeport_buf_uchar },
253	{ "READ_PORT_ULONG", (FUNC)hal_readport_ulong },
254	{ "READ_PORT_USHORT", (FUNC)hal_readport_ushort },
255	{ "READ_PORT_UCHAR", (FUNC)hal_readport_uchar },
256	{ "READ_PORT_BUFFER_ULONG", (FUNC)hal_readport_buf_ulong },
257	{ "READ_PORT_BUFFER_USHORT", (FUNC)hal_readport_buf_ushort },
258	{ "READ_PORT_BUFFER_UCHAR", (FUNC)hal_readport_buf_uchar },
259	{ "KfAcquireSpinLock", (FUNC)hal_lock },
260	{ "KfReleaseSpinLock", (FUNC)hal_unlock },
261	{ "KeGetCurrentIrql", (FUNC)hal_irql },
262
263	/*
264	 * This last entry is a catch-all for any function we haven't
265	 * implemented yet. The PE import list patching routine will
266	 * use it for any function that doesn't have an explicit match
267	 * in this table.
268	 */
269
270	{ NULL, (FUNC)dummy },
271
272	/* End of list. */
273
274	{ NULL, NULL },
275};
276