3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/mm/iospace.c
6 * PURPOSE: Mapping I/O space
8 * PROGRAMMERS: David Welch (welch@mcmail.com)
11 /* INCLUDES *****************************************************************/
15 #include <internal/debug.h>
17 /* FUNCTIONS *****************************************************************/
19 /**********************************************************************
24 * Maps a physical memory range into system space.
28 * First physical address to map;
31 * Number of bytes to map;
34 * Type of memory caching.
37 * The base virtual address which maps the region.
40 * Description moved here from include/ddk/mmfuncs.h.
41 * Code taken from ntoskrnl/mm/special.c.
48 MmMapIoSpace (IN PHYSICAL_ADDRESS PhysicalAddress
,
49 IN ULONG NumberOfBytes
,
50 IN MEMORY_CACHING_TYPE CacheEnable
)
58 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
61 DPRINT("MmMapIoSpace(%lx, %d, %d)\n", PhysicalAddress
, NumberOfBytes
, CacheEnable
);
63 if (CacheEnable
!= MmNonCached
&&
64 CacheEnable
!= MmCached
&&
65 CacheEnable
!= MmWriteCombined
)
70 BoundaryAddressMultiple
.QuadPart
= 0;
72 Offset
= PhysicalAddress
.u
.LowPart
% PAGE_SIZE
;
73 NumberOfBytes
+= Offset
;
74 PhysicalAddress
.QuadPart
-= Offset
;
75 Protect
= PAGE_EXECUTE_READWRITE
| PAGE_SYSTEM
;
76 if (CacheEnable
!= MmCached
)
78 Protect
|= (PAGE_NOCACHE
| PAGE_WRITETHROUGH
);
81 MmLockAddressSpace(MmGetKernelAddressSpace());
82 Status
= MmCreateMemoryArea (MmGetKernelAddressSpace(),
83 MEMORY_AREA_IO_MAPPING
,
90 BoundaryAddressMultiple
);
91 MmUnlockAddressSpace(MmGetKernelAddressSpace());
93 if (!NT_SUCCESS(Status
))
95 DPRINT("MmMapIoSpace failed (%lx)\n", Status
);
98 Pfn
= PhysicalAddress
.QuadPart
>> PAGE_SHIFT
;
99 for (i
= 0; i
< PAGE_ROUND_UP(NumberOfBytes
); i
+= PAGE_SIZE
, Pfn
++)
101 Status
= MmCreateVirtualMappingForKernel((char*)Result
+ i
,
105 if (!NT_SUCCESS(Status
))
107 DbgPrint("Unable to create virtual mapping\n");
111 return (PVOID
)((ULONG_PTR
)Result
+ Offset
);
115 /**********************************************************************
120 * Unmaps a physical memory range from system space.
124 * The base virtual address which maps the region;
127 * Number of bytes to unmap.
133 * Code taken from ntoskrnl/mm/special.c.
140 MmUnmapIoSpace (IN PVOID BaseAddress
,
141 IN ULONG NumberOfBytes
)
144 PVOID Address
= BaseAddress
;
146 Offset
= (ULONG_PTR
)Address
% PAGE_SIZE
;
147 Address
= RVA(Address
, - Offset
);
148 NumberOfBytes
+= Offset
;
150 MmLockAddressSpace(MmGetKernelAddressSpace());
151 MmFreeMemoryAreaByPtr(MmGetKernelAddressSpace(),
155 MmUnlockAddressSpace(MmGetKernelAddressSpace());
159 /**********************************************************************
161 * MmMapVideoDisplay@16
166 MmMapVideoDisplay (IN PHYSICAL_ADDRESS PhysicalAddress
,
167 IN ULONG NumberOfBytes
,
168 IN MEMORY_CACHING_TYPE CacheType
)
170 return MmMapIoSpace (PhysicalAddress
, NumberOfBytes
, (BOOLEAN
)CacheType
);
178 MmUnmapVideoDisplay (IN PVOID BaseAddress
,
179 IN ULONG NumberOfBytes
)
181 MmUnmapIoSpace (BaseAddress
, NumberOfBytes
);