update for HEAD-2003091401
[reactos.git] / 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$
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    MmLockAddressSpace(MmGetKernelAddressSpace());
80    Result = NULL;
81    Status = MmCreateMemoryArea (NULL,
82                                 MmGetKernelAddressSpace(),
83                                 MEMORY_AREA_IO_MAPPING,
84                                 &Result,
85                                 NumberOfBytes,
86                                 0,
87                                 &marea,
88                                 FALSE,
89                                 FALSE);
90    MmUnlockAddressSpace(MmGetKernelAddressSpace());
91
92    if (!NT_SUCCESS(STATUS_SUCCESS))
93      {
94         return (NULL);
95      }
96    Attributes = PAGE_EXECUTE_READWRITE | PAGE_SYSTEM;
97    if (!CacheEnable)
98      {
99         Attributes |= (PAGE_NOCACHE | PAGE_WRITETHROUGH);
100      }
101    for (i = 0; (i < (PAGE_ROUND_UP(NumberOfBytes) / PAGE_SIZE)); i++)
102      {
103         Status = 
104           MmCreateVirtualMappingForKernel(Result + (i * PAGE_SIZE),
105                                           Attributes,
106                                           (PHYSICAL_ADDRESS)
107                                           (PhysicalAddress.QuadPart + 
108                                            (i * PAGE_SIZE)));
109         if (!NT_SUCCESS(Status))
110           {
111              DbgPrint("Unable to create virtual mapping\n");
112              KEBUGCHECK(0);
113           }
114         MmMarkPageMapped((PHYSICAL_ADDRESS) (PhysicalAddress.QuadPart + 
115                                              (i * PAGE_SIZE)));
116      }
117    return ((PVOID)(Result + PhysicalAddress.QuadPart % PAGE_SIZE));
118 }
119  
120
121 /**********************************************************************
122  * NAME                                                 EXPORTED
123  *      MmUnmapIoSpace@8
124  * 
125  * DESCRIPTION
126  *      Unmaps a physical memory range from system space.
127  *      
128  * ARGUMENTS
129  *      BaseAddress
130  *              The base virtual address which maps the region; 
131  *              
132  *      NumberOfBytes
133  *              Number of bytes to unmap.
134  *
135  * RETURN VALUE
136  *      None.
137  *
138  * NOTE
139  *      Code taken from ntoskrnl/mm/special.c.
140  *
141  * REVISIONS
142  *
143  * @implemented
144  */
145 VOID STDCALL 
146 MmUnmapIoSpace (IN PVOID BaseAddress,
147                 IN ULONG NumberOfBytes)
148 {
149    MmLockAddressSpace(MmGetKernelAddressSpace());
150    MmFreeMemoryArea(MmGetKernelAddressSpace(),
151                     (PVOID)(((ULONG)BaseAddress / PAGE_SIZE) * PAGE_SIZE),
152                     NumberOfBytes,
153                     NULL,
154                     NULL);
155    MmUnlockAddressSpace(MmGetKernelAddressSpace());
156 }
157
158
159 /**********************************************************************
160  * NAME                                                 EXPORTED
161  *      MmMapVideoDisplay@16
162  *
163  * @implemented
164  */
165 PVOID STDCALL
166 MmMapVideoDisplay (IN   PHYSICAL_ADDRESS        PhysicalAddress,
167                    IN   ULONG                   NumberOfBytes,
168                    IN   MEMORY_CACHING_TYPE     CacheType)
169 {
170   return MmMapIoSpace (PhysicalAddress, NumberOfBytes, CacheType);
171 }
172
173
174 /*
175  * @implemented
176  */
177 VOID STDCALL
178 MmUnmapVideoDisplay (IN PVOID   BaseAddress,
179                      IN ULONG   NumberOfBytes)
180 {
181   MmUnmapIoSpace (BaseAddress, NumberOfBytes);
182 }
183
184
185 /* EOF */