3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
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.
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.
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.
19 /* $Id: iospace.c,v 1.23 2003/12/30 18:52:05 fireball Exp $
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/mm/iospace.c
23 * PURPOSE: Mapping I/O space
24 * PROGRAMMER: David Welch (welch@mcmail.com)
29 /* INCLUDES *****************************************************************/
31 #include <ddk/ntddk.h>
32 #include <internal/mm.h>
33 #include <internal/ps.h>
36 #include <internal/debug.h>
38 /* FUNCTIONS *****************************************************************/
40 /**********************************************************************
45 * Maps a physical memory range into system space.
49 * First physical address to map;
52 * Number of bytes to map;
55 * TRUE if the range can be cached.
58 * The base virtual address which maps the region.
61 * Description moved here from include/ddk/mmfuncs.h.
62 * Code taken from ntoskrnl/mm/special.c.
69 MmMapIoSpace (IN PHYSICAL_ADDRESS PhysicalAddress
,
70 IN ULONG NumberOfBytes
,
71 IN BOOLEAN CacheEnable
)
79 DPRINT("MmMapIoSpace(%lx, %d, %d)\n", PhysicalAddress
, NumberOfBytes
, CacheEnable
);
81 MmLockAddressSpace(MmGetKernelAddressSpace());
83 Status
= MmCreateMemoryArea (NULL
,
84 MmGetKernelAddressSpace(),
85 MEMORY_AREA_IO_MAPPING
,
92 MmUnlockAddressSpace(MmGetKernelAddressSpace());
94 if (!NT_SUCCESS(Status
))
96 DPRINT("MmMapIoSpace failed (%lx)\n", Status
);
99 Attributes
= PAGE_EXECUTE_READWRITE
| PAGE_SYSTEM
;
102 Attributes
|= (PAGE_NOCACHE
| PAGE_WRITETHROUGH
);
104 for (i
= 0; (i
< (PAGE_ROUND_UP(NumberOfBytes
) / PAGE_SIZE
)); i
++)
106 #if !defined(__GNUC__)
107 PHYSICAL_ADDRESS dummyJunkNeeded
;
108 dummyJunkNeeded
.QuadPart
= PhysicalAddress
.QuadPart
+ (i
* PAGE_SIZE
);
111 MmCreateVirtualMappingForKernel((char*)Result
+ (i
* PAGE_SIZE
),
113 #if defined(__GNUC__)
115 (PhysicalAddress
.QuadPart
+
121 if (!NT_SUCCESS(Status
))
123 DbgPrint("Unable to create virtual mapping\n");
126 #if defined(__GNUC__)
127 MmMarkPageMapped((PHYSICAL_ADDRESS
) (PhysicalAddress
.QuadPart
+
130 MmMarkPageMapped(dummyJunkNeeded
);
133 return ((PVOID
)((char*)Result
+ PhysicalAddress
.QuadPart
% PAGE_SIZE
));
137 /**********************************************************************
142 * Unmaps a physical memory range from system space.
146 * The base virtual address which maps the region;
149 * Number of bytes to unmap.
155 * Code taken from ntoskrnl/mm/special.c.
162 MmUnmapIoSpace (IN PVOID BaseAddress
,
163 IN ULONG NumberOfBytes
)
165 MmLockAddressSpace(MmGetKernelAddressSpace());
166 MmFreeMemoryArea(MmGetKernelAddressSpace(),
167 (PVOID
)(((ULONG
)BaseAddress
/ PAGE_SIZE
) * PAGE_SIZE
),
171 MmUnlockAddressSpace(MmGetKernelAddressSpace());
175 /**********************************************************************
177 * MmMapVideoDisplay@16
182 MmMapVideoDisplay (IN PHYSICAL_ADDRESS PhysicalAddress
,
183 IN ULONG NumberOfBytes
,
184 IN MEMORY_CACHING_TYPE CacheType
)
186 return MmMapIoSpace (PhysicalAddress
, NumberOfBytes
, (BOOLEAN
)CacheType
);
194 MmUnmapVideoDisplay (IN PVOID BaseAddress
,
195 IN ULONG NumberOfBytes
)
197 MmUnmapIoSpace (BaseAddress
, NumberOfBytes
);