1*c2c66affSColin Finck /*
2*c2c66affSColin Finck * PROJECT: ReactOS Kernel
3*c2c66affSColin Finck * LICENSE: BSD - See COPYING.ARM in the top level directory
4*c2c66affSColin Finck * FILE: ntoskrnl/mm/ARM3/iosup.c
5*c2c66affSColin Finck * PURPOSE: ARM Memory Manager I/O Mapping Functionality
6*c2c66affSColin Finck * PROGRAMMERS: ReactOS Portable Systems Group
7*c2c66affSColin Finck */
8*c2c66affSColin Finck
9*c2c66affSColin Finck /* INCLUDES *******************************************************************/
10*c2c66affSColin Finck
11*c2c66affSColin Finck #include <ntoskrnl.h>
12*c2c66affSColin Finck #define NDEBUG
13*c2c66affSColin Finck #include <debug.h>
14*c2c66affSColin Finck
15*c2c66affSColin Finck #define MODULE_INVOLVED_IN_ARM3
16*c2c66affSColin Finck #include <mm/ARM3/miarm.h>
17*c2c66affSColin Finck
18*c2c66affSColin Finck /* GLOBALS ********************************************************************/
19*c2c66affSColin Finck
20*c2c66affSColin Finck //
21*c2c66affSColin Finck // Each architecture has its own caching attributes for both I/O and Physical
22*c2c66affSColin Finck // memory mappings.
23*c2c66affSColin Finck //
24*c2c66affSColin Finck // This describes the attributes for the x86 architecture. It eventually needs
25*c2c66affSColin Finck // to go in the appropriate i386 directory.
26*c2c66affSColin Finck //
27*c2c66affSColin Finck MI_PFN_CACHE_ATTRIBUTE MiPlatformCacheAttributes[2][MmMaximumCacheType] =
28*c2c66affSColin Finck {
29*c2c66affSColin Finck //
30*c2c66affSColin Finck // RAM
31*c2c66affSColin Finck //
32*c2c66affSColin Finck {MiNonCached,MiCached,MiWriteCombined,MiCached,MiNonCached,MiWriteCombined},
33*c2c66affSColin Finck
34*c2c66affSColin Finck //
35*c2c66affSColin Finck // Device Memory
36*c2c66affSColin Finck //
37*c2c66affSColin Finck {MiNonCached,MiCached,MiWriteCombined,MiCached,MiNonCached,MiWriteCombined},
38*c2c66affSColin Finck };
39*c2c66affSColin Finck
40*c2c66affSColin Finck /* PUBLIC FUNCTIONS ***********************************************************/
41*c2c66affSColin Finck
42*c2c66affSColin Finck /*
43*c2c66affSColin Finck * @implemented
44*c2c66affSColin Finck */
45*c2c66affSColin Finck PVOID
46*c2c66affSColin Finck NTAPI
MmMapIoSpace(IN PHYSICAL_ADDRESS PhysicalAddress,IN SIZE_T NumberOfBytes,IN MEMORY_CACHING_TYPE CacheType)47*c2c66affSColin Finck MmMapIoSpace(IN PHYSICAL_ADDRESS PhysicalAddress,
48*c2c66affSColin Finck IN SIZE_T NumberOfBytes,
49*c2c66affSColin Finck IN MEMORY_CACHING_TYPE CacheType)
50*c2c66affSColin Finck {
51*c2c66affSColin Finck
52*c2c66affSColin Finck PFN_NUMBER Pfn;
53*c2c66affSColin Finck PFN_COUNT PageCount;
54*c2c66affSColin Finck PMMPTE PointerPte;
55*c2c66affSColin Finck PVOID BaseAddress;
56*c2c66affSColin Finck MMPTE TempPte;
57*c2c66affSColin Finck PMMPFN Pfn1 = NULL;
58*c2c66affSColin Finck MI_PFN_CACHE_ATTRIBUTE CacheAttribute;
59*c2c66affSColin Finck BOOLEAN IsIoMapping;
60*c2c66affSColin Finck
61*c2c66affSColin Finck //
62*c2c66affSColin Finck // Must be called with a non-zero count
63*c2c66affSColin Finck //
64*c2c66affSColin Finck ASSERT(NumberOfBytes != 0);
65*c2c66affSColin Finck
66*c2c66affSColin Finck //
67*c2c66affSColin Finck // Make sure the upper bits are 0 if this system
68*c2c66affSColin Finck // can't describe more than 4 GB of physical memory.
69*c2c66affSColin Finck // FIXME: This doesn't respect PAE, but we currently don't
70*c2c66affSColin Finck // define a PAE build flag since there is no such build.
71*c2c66affSColin Finck //
72*c2c66affSColin Finck #if !defined(_M_AMD64)
73*c2c66affSColin Finck ASSERT(PhysicalAddress.HighPart == 0);
74*c2c66affSColin Finck #endif
75*c2c66affSColin Finck
76*c2c66affSColin Finck //
77*c2c66affSColin Finck // Normalize and validate the caching attributes
78*c2c66affSColin Finck //
79*c2c66affSColin Finck CacheType &= 0xFF;
80*c2c66affSColin Finck if (CacheType >= MmMaximumCacheType) return NULL;
81*c2c66affSColin Finck
82*c2c66affSColin Finck //
83*c2c66affSColin Finck // Calculate page count
84*c2c66affSColin Finck //
85*c2c66affSColin Finck PageCount = ADDRESS_AND_SIZE_TO_SPAN_PAGES(PhysicalAddress.LowPart,
86*c2c66affSColin Finck NumberOfBytes);
87*c2c66affSColin Finck
88*c2c66affSColin Finck //
89*c2c66affSColin Finck // Compute the PFN and check if it's a known I/O mapping
90*c2c66affSColin Finck // Also translate the cache attribute
91*c2c66affSColin Finck //
92*c2c66affSColin Finck Pfn = (PFN_NUMBER)(PhysicalAddress.QuadPart >> PAGE_SHIFT);
93*c2c66affSColin Finck Pfn1 = MiGetPfnEntry(Pfn);
94*c2c66affSColin Finck IsIoMapping = (Pfn1 == NULL) ? TRUE : FALSE;
95*c2c66affSColin Finck CacheAttribute = MiPlatformCacheAttributes[IsIoMapping][CacheType];
96*c2c66affSColin Finck
97*c2c66affSColin Finck //
98*c2c66affSColin Finck // Now allocate system PTEs for the mapping, and get the VA
99*c2c66affSColin Finck //
100*c2c66affSColin Finck PointerPte = MiReserveSystemPtes(PageCount, SystemPteSpace);
101*c2c66affSColin Finck if (!PointerPte) return NULL;
102*c2c66affSColin Finck BaseAddress = MiPteToAddress(PointerPte);
103*c2c66affSColin Finck
104*c2c66affSColin Finck //
105*c2c66affSColin Finck // Check if this is uncached
106*c2c66affSColin Finck //
107*c2c66affSColin Finck if (CacheAttribute != MiCached)
108*c2c66affSColin Finck {
109*c2c66affSColin Finck //
110*c2c66affSColin Finck // Flush all caches
111*c2c66affSColin Finck //
112*c2c66affSColin Finck KeFlushEntireTb(TRUE, TRUE);
113*c2c66affSColin Finck KeInvalidateAllCaches();
114*c2c66affSColin Finck }
115*c2c66affSColin Finck
116*c2c66affSColin Finck //
117*c2c66affSColin Finck // Now compute the VA offset
118*c2c66affSColin Finck //
119*c2c66affSColin Finck BaseAddress = (PVOID)((ULONG_PTR)BaseAddress +
120*c2c66affSColin Finck BYTE_OFFSET(PhysicalAddress.LowPart));
121*c2c66affSColin Finck
122*c2c66affSColin Finck //
123*c2c66affSColin Finck // Get the template and configure caching
124*c2c66affSColin Finck //
125*c2c66affSColin Finck TempPte = ValidKernelPte;
126*c2c66affSColin Finck switch (CacheAttribute)
127*c2c66affSColin Finck {
128*c2c66affSColin Finck case MiNonCached:
129*c2c66affSColin Finck
130*c2c66affSColin Finck //
131*c2c66affSColin Finck // Disable the cache
132*c2c66affSColin Finck //
133*c2c66affSColin Finck MI_PAGE_DISABLE_CACHE(&TempPte);
134*c2c66affSColin Finck MI_PAGE_WRITE_THROUGH(&TempPte);
135*c2c66affSColin Finck break;
136*c2c66affSColin Finck
137*c2c66affSColin Finck case MiCached:
138*c2c66affSColin Finck
139*c2c66affSColin Finck //
140*c2c66affSColin Finck // Leave defaults
141*c2c66affSColin Finck //
142*c2c66affSColin Finck break;
143*c2c66affSColin Finck
144*c2c66affSColin Finck case MiWriteCombined:
145*c2c66affSColin Finck
146*c2c66affSColin Finck //
147*c2c66affSColin Finck // Disable the cache and allow combined writing
148*c2c66affSColin Finck //
149*c2c66affSColin Finck MI_PAGE_DISABLE_CACHE(&TempPte);
150*c2c66affSColin Finck MI_PAGE_WRITE_COMBINED(&TempPte);
151*c2c66affSColin Finck break;
152*c2c66affSColin Finck
153*c2c66affSColin Finck default:
154*c2c66affSColin Finck
155*c2c66affSColin Finck //
156*c2c66affSColin Finck // Should never happen
157*c2c66affSColin Finck //
158*c2c66affSColin Finck ASSERT(FALSE);
159*c2c66affSColin Finck break;
160*c2c66affSColin Finck }
161*c2c66affSColin Finck
162*c2c66affSColin Finck //
163*c2c66affSColin Finck // Sanity check and re-flush
164*c2c66affSColin Finck //
165*c2c66affSColin Finck Pfn = (PFN_NUMBER)(PhysicalAddress.QuadPart >> PAGE_SHIFT);
166*c2c66affSColin Finck ASSERT((Pfn1 == MiGetPfnEntry(Pfn)) || (Pfn1 == NULL));
167*c2c66affSColin Finck KeFlushEntireTb(TRUE, TRUE);
168*c2c66affSColin Finck KeInvalidateAllCaches();
169*c2c66affSColin Finck
170*c2c66affSColin Finck //
171*c2c66affSColin Finck // Do the mapping
172*c2c66affSColin Finck //
173*c2c66affSColin Finck do
174*c2c66affSColin Finck {
175*c2c66affSColin Finck //
176*c2c66affSColin Finck // Write the PFN
177*c2c66affSColin Finck //
178*c2c66affSColin Finck TempPte.u.Hard.PageFrameNumber = Pfn++;
179*c2c66affSColin Finck MI_WRITE_VALID_PTE(PointerPte++, TempPte);
180*c2c66affSColin Finck } while (--PageCount);
181*c2c66affSColin Finck
182*c2c66affSColin Finck //
183*c2c66affSColin Finck // We're done!
184*c2c66affSColin Finck //
185*c2c66affSColin Finck return BaseAddress;
186*c2c66affSColin Finck }
187*c2c66affSColin Finck
188*c2c66affSColin Finck /*
189*c2c66affSColin Finck * @implemented
190*c2c66affSColin Finck */
191*c2c66affSColin Finck VOID
192*c2c66affSColin Finck NTAPI
MmUnmapIoSpace(IN PVOID BaseAddress,IN SIZE_T NumberOfBytes)193*c2c66affSColin Finck MmUnmapIoSpace(IN PVOID BaseAddress,
194*c2c66affSColin Finck IN SIZE_T NumberOfBytes)
195*c2c66affSColin Finck {
196*c2c66affSColin Finck PFN_NUMBER Pfn;
197*c2c66affSColin Finck PFN_COUNT PageCount;
198*c2c66affSColin Finck PMMPTE PointerPte;
199*c2c66affSColin Finck
200*c2c66affSColin Finck //
201*c2c66affSColin Finck // Sanity check
202*c2c66affSColin Finck //
203*c2c66affSColin Finck ASSERT(NumberOfBytes != 0);
204*c2c66affSColin Finck
205*c2c66affSColin Finck //
206*c2c66affSColin Finck // Get the page count
207*c2c66affSColin Finck //
208*c2c66affSColin Finck PageCount = ADDRESS_AND_SIZE_TO_SPAN_PAGES(BaseAddress, NumberOfBytes);
209*c2c66affSColin Finck
210*c2c66affSColin Finck //
211*c2c66affSColin Finck // Get the PTE and PFN
212*c2c66affSColin Finck //
213*c2c66affSColin Finck PointerPte = MiAddressToPte(BaseAddress);
214*c2c66affSColin Finck Pfn = PFN_FROM_PTE(PointerPte);
215*c2c66affSColin Finck
216*c2c66affSColin Finck //
217*c2c66affSColin Finck // Is this an I/O mapping?
218*c2c66affSColin Finck //
219*c2c66affSColin Finck if (!MiGetPfnEntry(Pfn))
220*c2c66affSColin Finck {
221*c2c66affSColin Finck //
222*c2c66affSColin Finck // Destroy the PTE
223*c2c66affSColin Finck //
224*c2c66affSColin Finck RtlZeroMemory(PointerPte, PageCount * sizeof(MMPTE));
225*c2c66affSColin Finck
226*c2c66affSColin Finck //
227*c2c66affSColin Finck // Blow the TLB
228*c2c66affSColin Finck //
229*c2c66affSColin Finck KeFlushEntireTb(TRUE, TRUE);
230*c2c66affSColin Finck }
231*c2c66affSColin Finck
232*c2c66affSColin Finck //
233*c2c66affSColin Finck // Release the PTEs
234*c2c66affSColin Finck //
235*c2c66affSColin Finck MiReleaseSystemPtes(PointerPte, PageCount, 0);
236*c2c66affSColin Finck }
237*c2c66affSColin Finck
238*c2c66affSColin Finck /*
239*c2c66affSColin Finck * @implemented
240*c2c66affSColin Finck */
241*c2c66affSColin Finck PVOID
242*c2c66affSColin Finck NTAPI
MmMapVideoDisplay(IN PHYSICAL_ADDRESS PhysicalAddress,IN SIZE_T NumberOfBytes,IN MEMORY_CACHING_TYPE CacheType)243*c2c66affSColin Finck MmMapVideoDisplay(IN PHYSICAL_ADDRESS PhysicalAddress,
244*c2c66affSColin Finck IN SIZE_T NumberOfBytes,
245*c2c66affSColin Finck IN MEMORY_CACHING_TYPE CacheType)
246*c2c66affSColin Finck {
247*c2c66affSColin Finck PAGED_CODE();
248*c2c66affSColin Finck
249*c2c66affSColin Finck //
250*c2c66affSColin Finck // Call the real function
251*c2c66affSColin Finck //
252*c2c66affSColin Finck return MmMapIoSpace(PhysicalAddress, NumberOfBytes, CacheType);
253*c2c66affSColin Finck }
254*c2c66affSColin Finck
255*c2c66affSColin Finck /*
256*c2c66affSColin Finck * @implemented
257*c2c66affSColin Finck */
258*c2c66affSColin Finck VOID
259*c2c66affSColin Finck NTAPI
MmUnmapVideoDisplay(IN PVOID BaseAddress,IN SIZE_T NumberOfBytes)260*c2c66affSColin Finck MmUnmapVideoDisplay(IN PVOID BaseAddress,
261*c2c66affSColin Finck IN SIZE_T NumberOfBytes)
262*c2c66affSColin Finck {
263*c2c66affSColin Finck //
264*c2c66affSColin Finck // Call the real function
265*c2c66affSColin Finck //
266*c2c66affSColin Finck MmUnmapIoSpace(BaseAddress, NumberOfBytes);
267*c2c66affSColin Finck }
268*c2c66affSColin Finck
269*c2c66affSColin Finck LOGICAL
270*c2c66affSColin Finck NTAPI
MmIsIoSpaceActive(IN PHYSICAL_ADDRESS StartAddress,IN SIZE_T NumberOfBytes)271*c2c66affSColin Finck MmIsIoSpaceActive(IN PHYSICAL_ADDRESS StartAddress,
272*c2c66affSColin Finck IN SIZE_T NumberOfBytes)
273*c2c66affSColin Finck {
274*c2c66affSColin Finck UNIMPLEMENTED;
275*c2c66affSColin Finck return FALSE;
276*c2c66affSColin Finck }
277*c2c66affSColin Finck
278*c2c66affSColin Finck /* EOF */
279