subr_hal.c revision 125551
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 125551 2004-02-07 06:44:13Z 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 FUNC void(*)(void)
59
60__stdcall static void hal_stall_exec_cpu(uint32_t);
61__stdcall static void hal_writeport_buf_ulong(uint32_t *,
62	uint32_t *, uint32_t);
63__stdcall static void hal_writeport_buf_ushort(uint16_t *,
64	uint16_t *, uint32_t);
65__stdcall static void hal_writeport_buf_uchar(uint8_t *,
66	uint8_t *, uint32_t);
67__stdcall static void hal_writeport_ulong(uint32_t *, uint32_t);
68__stdcall static void hal_writeport_ushort(uint16_t *, uint16_t);
69__stdcall static void hal_writeport_uchar(uint8_t *, uint8_t);
70__stdcall static uint32_t hal_readport_ulong(uint32_t *);
71__stdcall static uint16_t hal_readport_ushort(uint16_t *);
72__stdcall static uint8_t hal_readport_uchar(uint8_t *);
73__stdcall static void hal_readport_buf_ulong(uint32_t *,
74	uint32_t *, uint32_t);
75__stdcall static void hal_readport_buf_ushort(uint16_t *,
76	uint16_t *, uint32_t);
77__stdcall static void hal_readport_buf_uchar(uint8_t *,
78	uint8_t *, uint32_t);
79__stdcall static uint8_t hal_lock(/*kspin_lock * */void);
80__stdcall static void hal_unlock(/*kspin_lock *, uint8_t*/void);
81__stdcall static uint8_t hal_irql(void);
82__stdcall static uint64_t hal_perfcount(uint64_t *);
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(NDIS_BUS_SPACE_IO, 0x0, (bus_size_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(NDIS_BUS_SPACE_IO, 0x0, (bus_size_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(NDIS_BUS_SPACE_IO, 0x0, (bus_size_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(NDIS_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(NDIS_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(NDIS_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(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port));
160}
161
162__stdcall static uint32_t
163hal_readport_ulong(port)
164	uint32_t		*port;
165{
166	return(bus_space_read_4(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port));
167}
168
169__stdcall static uint8_t
170hal_readport_uchar(port)
171	uint8_t			*port;
172{
173	return(bus_space_read_1(NDIS_BUS_SPACE_IO, 0x0, (bus_size_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(NDIS_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(NDIS_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(NDIS_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(DISPATCH_LEVEL);
236}
237
238__stdcall static uint64_t
239hal_perfcount(freq)
240	uint64_t		*freq;
241{
242	if (freq != NULL)
243		*freq = hz;
244
245	return((uint64_t)ticks);
246}
247
248__stdcall
249static void dummy()
250{
251	printf ("hal dummy called...\n");
252	return;
253}
254
255image_patch_table hal_functbl[] = {
256	{ "KeStallExecutionProcessor", (FUNC)hal_stall_exec_cpu },
257	{ "WRITE_PORT_ULONG", (FUNC)hal_writeport_ulong },
258	{ "WRITE_PORT_USHORT", (FUNC)hal_writeport_ushort },
259	{ "WRITE_PORT_UCHAR", (FUNC)hal_writeport_uchar },
260	{ "WRITE_PORT_BUFFER_ULONG", (FUNC)hal_writeport_buf_ulong },
261	{ "WRITE_PORT_BUFFER_USHORT", (FUNC)hal_writeport_buf_ushort },
262	{ "WRITE_PORT_BUFFER_UCHAR", (FUNC)hal_writeport_buf_uchar },
263	{ "READ_PORT_ULONG", (FUNC)hal_readport_ulong },
264	{ "READ_PORT_USHORT", (FUNC)hal_readport_ushort },
265	{ "READ_PORT_UCHAR", (FUNC)hal_readport_uchar },
266	{ "READ_PORT_BUFFER_ULONG", (FUNC)hal_readport_buf_ulong },
267	{ "READ_PORT_BUFFER_USHORT", (FUNC)hal_readport_buf_ushort },
268	{ "READ_PORT_BUFFER_UCHAR", (FUNC)hal_readport_buf_uchar },
269	{ "KfAcquireSpinLock", (FUNC)hal_lock },
270	{ "KfReleaseSpinLock", (FUNC)hal_unlock },
271	{ "KeGetCurrentIrql", (FUNC)hal_irql },
272	{ "KeQueryPerformanceCounter",	(FUNC)hal_perfcount },
273
274	/*
275	 * This last entry is a catch-all for any function we haven't
276	 * implemented yet. The PE import list patching routine will
277	 * use it for any function that doesn't have an explicit match
278	 * in this table.
279	 */
280
281	{ NULL, (FUNC)dummy },
282
283	/* End of list. */
284
285	{ NULL, NULL },
286};
287