Continue of MSVC-compiling changes....
[reactos.git] / reactos / ntoskrnl / mm / iospace.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id: iospace.c,v 1.23 2003/12/30 18:52:05 fireball Exp $
20 *
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/mm/iospace.c
23 * PURPOSE: Mapping I/O space
24 * PROGRAMMER: David Welch (welch@mcmail.com)
25 * UPDATE HISTORY:
26 * Created 22/05/98
27 */
28
29 /* INCLUDES *****************************************************************/
30
31 #include <ddk/ntddk.h>
32 #include <internal/mm.h>
33 #include <internal/ps.h>
34
35 //#define NDEBUG
36 #include <internal/debug.h>
37
38 /* FUNCTIONS *****************************************************************/
39
40 /**********************************************************************
41 * NAME EXPORTED
42 * MmMapIoSpace@16
43 *
44 * DESCRIPTION
45 * Maps a physical memory range into system space.
46 *
47 * ARGUMENTS
48 * PhysicalAddress
49 * First physical address to map;
50 *
51 * NumberOfBytes
52 * Number of bytes to map;
53 *
54 * CacheEnable
55 * TRUE if the range can be cached.
56 *
57 * RETURN VALUE
58 * The base virtual address which maps the region.
59 *
60 * NOTE
61 * Description moved here from include/ddk/mmfuncs.h.
62 * Code taken from ntoskrnl/mm/special.c.
63 *
64 * REVISIONS
65 *
66 * @implemented
67 */
68 PVOID STDCALL
69 MmMapIoSpace (IN PHYSICAL_ADDRESS PhysicalAddress,
70 IN ULONG NumberOfBytes,
71 IN BOOLEAN CacheEnable)
72 {
73 PVOID Result;
74 MEMORY_AREA* marea;
75 NTSTATUS Status;
76 ULONG i;
77 ULONG Attributes;
78
79 DPRINT("MmMapIoSpace(%lx, %d, %d)\n", PhysicalAddress, NumberOfBytes, CacheEnable);
80
81 MmLockAddressSpace(MmGetKernelAddressSpace());
82 Result = NULL;
83 Status = MmCreateMemoryArea (NULL,
84 MmGetKernelAddressSpace(),
85 MEMORY_AREA_IO_MAPPING,
86 &Result,
87 NumberOfBytes,
88 0,
89 &marea,
90 FALSE,
91 FALSE);
92 MmUnlockAddressSpace(MmGetKernelAddressSpace());
93
94 if (!NT_SUCCESS(Status))
95 {
96 DPRINT("MmMapIoSpace failed (%lx)\n", Status);
97 return (NULL);
98 }
99 Attributes = PAGE_EXECUTE_READWRITE | PAGE_SYSTEM;
100 if (!CacheEnable)
101 {
102 Attributes |= (PAGE_NOCACHE | PAGE_WRITETHROUGH);
103 }
104 for (i = 0; (i < (PAGE_ROUND_UP(NumberOfBytes) / PAGE_SIZE)); i++)
105 {
106 #if !defined(__GNUC__)
107 PHYSICAL_ADDRESS dummyJunkNeeded;
108 dummyJunkNeeded.QuadPart = PhysicalAddress.QuadPart + (i * PAGE_SIZE);
109 #endif
110 Status =
111 MmCreateVirtualMappingForKernel((char*)Result + (i * PAGE_SIZE),
112 Attributes,
113 #if defined(__GNUC__)
114 (PHYSICAL_ADDRESS)
115 (PhysicalAddress.QuadPart +
116 (i * PAGE_SIZE))
117 #else
118 dummyJunkNeeded
119 #endif
120 );
121 if (!NT_SUCCESS(Status))
122 {
123 DbgPrint("Unable to create virtual mapping\n");
124 KEBUGCHECK(0);
125 }
126 #if defined(__GNUC__)
127 MmMarkPageMapped((PHYSICAL_ADDRESS) (PhysicalAddress.QuadPart +
128 (i * PAGE_SIZE)));
129 #else
130 MmMarkPageMapped(dummyJunkNeeded);
131 #endif
132 }
133 return ((PVOID)((char*)Result + PhysicalAddress.QuadPart % PAGE_SIZE));
134 }
135
136
137 /**********************************************************************
138 * NAME EXPORTED
139 * MmUnmapIoSpace@8
140 *
141 * DESCRIPTION
142 * Unmaps a physical memory range from system space.
143 *
144 * ARGUMENTS
145 * BaseAddress
146 * The base virtual address which maps the region;
147 *
148 * NumberOfBytes
149 * Number of bytes to unmap.
150 *
151 * RETURN VALUE
152 * None.
153 *
154 * NOTE
155 * Code taken from ntoskrnl/mm/special.c.
156 *
157 * REVISIONS
158 *
159 * @implemented
160 */
161 VOID STDCALL
162 MmUnmapIoSpace (IN PVOID BaseAddress,
163 IN ULONG NumberOfBytes)
164 {
165 MmLockAddressSpace(MmGetKernelAddressSpace());
166 MmFreeMemoryArea(MmGetKernelAddressSpace(),
167 (PVOID)(((ULONG)BaseAddress / PAGE_SIZE) * PAGE_SIZE),
168 NumberOfBytes,
169 NULL,
170 NULL);
171 MmUnlockAddressSpace(MmGetKernelAddressSpace());
172 }
173
174
175 /**********************************************************************
176 * NAME EXPORTED
177 * MmMapVideoDisplay@16
178 *
179 * @implemented
180 */
181 PVOID STDCALL
182 MmMapVideoDisplay (IN PHYSICAL_ADDRESS PhysicalAddress,
183 IN ULONG NumberOfBytes,
184 IN MEMORY_CACHING_TYPE CacheType)
185 {
186 return MmMapIoSpace (PhysicalAddress, NumberOfBytes, (BOOLEAN)CacheType);
187 }
188
189
190 /*
191 * @implemented
192 */
193 VOID STDCALL
194 MmUnmapVideoDisplay (IN PVOID BaseAddress,
195 IN ULONG NumberOfBytes)
196 {
197 MmUnmapIoSpace (BaseAddress, NumberOfBytes);
198 }
199
200
201 /* EOF */