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