|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: This is the header file for the PCI driver test. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #ifndef __TPCI_TEST_H |
|
20 #define __TPCI_TEST_H |
|
21 |
|
22 #ifndef __KERNEL_MODE__ |
|
23 #define __E32TEST_EXTENSION__ |
|
24 #include <e32def_private.h> |
|
25 |
|
26 // Following contents are carbon copy of \os\kernelhwsrv\kerneltest\e32test\misc\test_thread.h |
|
27 #include <e32test.h> |
|
28 #include <e32svr.h> |
|
29 #include <e32des8.h> |
|
30 #include <e32des8_private.h> |
|
31 #include <e32cmn.h> |
|
32 #include <e32cmn_private.h> |
|
33 #include <e32std.h> |
|
34 #include <e32std_private.h> |
|
35 |
|
36 |
|
37 _LIT(KPciPanicCat, "test_thread.h"); |
|
38 |
|
39 static const TInt KPciHeapSize=0x2000; |
|
40 |
|
41 enum TPciPanicCode |
|
42 { |
|
43 EThreadCreateFailed |
|
44 }; |
|
45 |
|
46 /** |
|
47 A utility class for running functions in other threads/processes |
|
48 */ |
|
49 class TTestRemote |
|
50 { |
|
51 public: |
|
52 virtual TInt WaitForExitL() = 0; |
|
53 virtual ~TTestRemote() |
|
54 {} |
|
55 |
|
56 virtual void Rendezvous(TRequestStatus& aStatus) = 0; |
|
57 |
|
58 protected: |
|
59 TTestRemote() |
|
60 {} |
|
61 |
|
62 static TInt RunFunctor(TAny* aFunctor) |
|
63 { |
|
64 TFunctor& functor = *(TFunctor*)aFunctor; |
|
65 functor(); |
|
66 return KErrNone; |
|
67 } |
|
68 |
|
69 TRequestStatus iLogonStatus; |
|
70 static TInt iCount; |
|
71 }; |
|
72 TInt TTestRemote::iCount=0; |
|
73 |
|
74 class TTestThread : public TTestRemote |
|
75 { |
|
76 public: |
|
77 TTestThread(const TDesC& aName, TThreadFunction aFn, TAny* aData, TBool aAutoResume=ETrue) |
|
78 { |
|
79 Init(aName, aFn, aData, aAutoResume); |
|
80 } |
|
81 |
|
82 /** |
|
83 Run aFunctor in another thread |
|
84 */ |
|
85 TTestThread(const TDesC& aName, TFunctor& aFunctor, TBool aAutoResume=ETrue) |
|
86 { |
|
87 Init(aName, RunFunctor, &aFunctor, aAutoResume); |
|
88 } |
|
89 |
|
90 ~TTestThread() |
|
91 { |
|
92 //RTest::CloseHandleAndWaitForDestruction(iThread); |
|
93 iThread.Close(); |
|
94 } |
|
95 |
|
96 void Resume() |
|
97 { |
|
98 iThread.Resume(); |
|
99 } |
|
100 |
|
101 /** |
|
102 If thread exited normally, return its return code |
|
103 Otherwise, leave with exit reason |
|
104 */ |
|
105 virtual TInt WaitForExitL() |
|
106 { |
|
107 User::WaitForRequest(iLogonStatus); |
|
108 const TInt exitType = iThread.ExitType(); |
|
109 const TInt exitReason = iThread.ExitReason(); |
|
110 |
|
111 __ASSERT_ALWAYS(exitType != EExitPending, User::Panic(_L("TTestThread"),0)); |
|
112 |
|
113 if(exitType != EExitKill) |
|
114 User::Leave(exitReason); |
|
115 |
|
116 return exitReason; |
|
117 } |
|
118 |
|
119 virtual void Rendezvous(TRequestStatus& aStatus) |
|
120 { |
|
121 iThread.Rendezvous(aStatus); |
|
122 } |
|
123 |
|
124 private: |
|
125 void Init(const TDesC& aName, TThreadFunction aFn, TAny* aData, TBool aAutoResume) |
|
126 { |
|
127 TKName name(aName); |
|
128 name.AppendFormat(_L("-%d"), iCount++); |
|
129 TInt r=iThread.Create(name, aFn, KDefaultStackSize, KPciHeapSize, KPciHeapSize, aData); |
|
130 if(r!=KErrNone) |
|
131 { |
|
132 RDebug::Printf("RThread::Create failed, code=%d", r); |
|
133 User::Panic(KPciPanicCat, EThreadCreateFailed); |
|
134 } |
|
135 |
|
136 iThread.Logon(iLogonStatus); |
|
137 __ASSERT_ALWAYS(iLogonStatus == KRequestPending, User::Panic(_L("TTestThread"),0)); |
|
138 |
|
139 if(aAutoResume) |
|
140 iThread.Resume(); |
|
141 } |
|
142 |
|
143 RThread iThread; |
|
144 }; |
|
145 |
|
146 class CTest : public CBase, public TFunctor |
|
147 { |
|
148 public: |
|
149 ~CTest() |
|
150 { |
|
151 iName.Close(); |
|
152 } |
|
153 |
|
154 virtual void operator()() |
|
155 { |
|
156 RTest test(iName); |
|
157 test.Start(iName); |
|
158 for(TInt i=0; i<iIterations; i++) |
|
159 { |
|
160 test.Next(iName); |
|
161 RunTest(); |
|
162 } |
|
163 test.End(); |
|
164 } |
|
165 |
|
166 virtual void RunTest() = 0; |
|
167 |
|
168 virtual CTest* Clone() const = 0; |
|
169 |
|
170 const TDesC& Name() const |
|
171 { |
|
172 return iName; |
|
173 } |
|
174 |
|
175 protected: |
|
176 CTest(const TDesC& aName, TInt aIterations) |
|
177 :iIterations(aIterations) |
|
178 { |
|
179 iName.CreateL(aName); |
|
180 } |
|
181 |
|
182 |
|
183 |
|
184 CTest(const CTest& aOther) |
|
185 :iIterations(aOther.iIterations) |
|
186 { |
|
187 iName.CreateL(aOther.iName); |
|
188 } |
|
189 |
|
190 //It would be useful to have an RTest member, but this can't be |
|
191 //initialised untill the new thread is running as it will refer to |
|
192 //the creating thread |
|
193 RBuf iName; |
|
194 const TInt iIterations; |
|
195 }; |
|
196 |
|
197 /** |
|
198 Make aNumberOfThreads copies of aTest and run |
|
199 each in its own thread |
|
200 |
|
201 @param test Reference to test object |
|
202 @param aTest Referance |
|
203 */ |
|
204 void MultipleTestRun(RTest& test, const CTest& aTest, TInt aNumberOfThreads) |
|
205 { |
|
206 RPointerArray<CTest> testArray; |
|
207 RPointerArray<TTestThread> threadArray; |
|
208 |
|
209 for(TInt i=0; i<aNumberOfThreads; i++) |
|
210 { |
|
211 test.Printf(_L("Create test thread")); |
|
212 CTest* newTest = aTest.Clone(); |
|
213 test_NotNull(newTest); |
|
214 |
|
215 TTestThread* thread = new TTestThread(aTest.Name(), *newTest); |
|
216 test_NotNull(thread); |
|
217 |
|
218 threadArray.AppendL(thread); |
|
219 testArray.AppendL(newTest); |
|
220 } |
|
221 |
|
222 const TInt count = threadArray.Count(); |
|
223 for(TInt j=0; j<count; j++) |
|
224 { |
|
225 TTestThread* thread = threadArray[j]; |
|
226 |
|
227 TInt r = KErrNone; |
|
228 TRAPD(leaveCode, r = thread->WaitForExitL()); |
|
229 if(leaveCode != KErrNone) |
|
230 { |
|
231 test.Printf(_L("Thread %d: Panic code:%d\n"), j, leaveCode); |
|
232 test_KErrNone(leaveCode); |
|
233 } |
|
234 |
|
235 if(r!=KErrNone) |
|
236 { |
|
237 test.Printf(_L("Thread Number %d\n"), j); |
|
238 test_KErrNone(r); |
|
239 } |
|
240 } |
|
241 |
|
242 threadArray.ResetAndDestroy(); |
|
243 threadArray.Close(); |
|
244 |
|
245 testArray.ResetAndDestroy(); |
|
246 testArray.Close(); |
|
247 } |
|
248 // end of \os\kernelhwsrv\kerneltest\e32test\misc\test_thread.h |
|
249 |
|
250 #endif // __KERNEL_MODE__ |
|
251 |
|
252 _LIT(KPciLdd, "d_pci.ldd"); |
|
253 _LIT(KPciLddFactory, "PCI_test_factory"); |
|
254 _LIT(KPciTest, "PCI Test LDD"); |
|
255 |
|
256 /** |
|
257 Test driver op-codes |
|
258 */ |
|
259 enum TPciTestCmd |
|
260 { |
|
261 EGetTestInfo, |
|
262 EAccessConfigSpace, |
|
263 EAccessMemorySpace, |
|
264 EOpenPciDChunk, |
|
265 EOpenPciPlatHwChunk, |
|
266 EOpenPciMappedChunk, |
|
267 EOpenPciWindowChunk, |
|
268 ERunUnitTests |
|
269 }; |
|
270 |
|
271 /** |
|
272 Identifies a PCI Function (device) on the system |
|
273 */ |
|
274 struct TPciDevice |
|
275 { |
|
276 TPciDevice() |
|
277 :iVendorId(0xFFFFFFFF), iDeviceId(0xFFFFFFFF), iInstance(0) {} |
|
278 |
|
279 TPciDevice(TUint aVendorId, TUint aDeviceId, TInt aInstance=0) |
|
280 :iVendorId(aVendorId), iDeviceId(aDeviceId), iInstance(aInstance) {} |
|
281 |
|
282 TUint iVendorId; |
|
283 TUint iDeviceId; |
|
284 TInt iInstance; //< Unit to open (there could be multiple devices on system) |
|
285 }; |
|
286 |
|
287 /** |
|
288 Used to send chunk size and recieve |
|
289 PCI address |
|
290 */ |
|
291 struct TPciChunkCreateInfo |
|
292 { |
|
293 TPciChunkCreateInfo() |
|
294 :iSize(0), iPciAddress(NULL) |
|
295 { |
|
296 } |
|
297 |
|
298 TPciChunkCreateInfo(TInt aSize, TUint& aPciAddress, TRequestStatus* aStatus=NULL) |
|
299 :iSize(aSize), iPciAddress(&aPciAddress), iStatus(aStatus) |
|
300 { |
|
301 } |
|
302 TInt iSize; |
|
303 TUint* iPciAddress; |
|
304 TRequestStatus* iStatus; |
|
305 }; |
|
306 |
|
307 /** |
|
308 Information about the PSL required by the |
|
309 user side test |
|
310 */ |
|
311 struct TPciTestInfo |
|
312 { |
|
313 TPciDevice iDevice; //< Probe for this |
|
314 |
|
315 /** |
|
316 Supplies the necessary information to test Read, Write, and |
|
317 Modify for a word of PCI memory or configuration space |
|
318 */ |
|
319 struct TAddrSpaceTest |
|
320 { |
|
321 TAddrSpaceTest() |
|
322 :iOffset(0), iExpectedValue(0), iReadOnlyMask(0) |
|
323 {} |
|
324 |
|
325 TAddrSpaceTest(TUint aOffset, TUint aExpectedValue, TUint aReadOnlyMask) |
|
326 :iOffset(aOffset), iExpectedValue(aExpectedValue), iReadOnlyMask(aReadOnlyMask) |
|
327 {} |
|
328 |
|
329 /** |
|
330 Returns a specified sub byte, or word from the whole dword |
|
331 */ |
|
332 inline TUint Expected(TInt aBitWidth, TInt aExtraOffset) const |
|
333 { |
|
334 //the right shift required to get field to bit 0 |
|
335 const TInt shift = 8 *((aExtraOffset + iOffset) % 4); |
|
336 |
|
337 const TUint mask = 0xFFFFFFFF >> (32-aBitWidth); |
|
338 return (iExpectedValue >> shift) & mask; |
|
339 } |
|
340 |
|
341 const TUint iOffset; |
|
342 const TUint iExpectedValue; //< The initial value of word |
|
343 const TUint iReadOnlyMask; //< Mask of unwritable bits |
|
344 //Future work, memory spaces should state a bar index |
|
345 }; |
|
346 |
|
347 |
|
348 TAddrSpaceTest iCfgSpaceRead; |
|
349 TAddrSpaceTest iCfgSpaceWrite; |
|
350 |
|
351 TUint iMemSpaceIndex; //< Memory space to select |
|
352 TAddrSpaceTest iMemSpaceRead; |
|
353 TAddrSpaceTest iMemSpaceWrite; |
|
354 |
|
355 TInt iNumberOfBars; //< Number of simultaneous mappings into PCI space |
|
356 }; |
|
357 |
|
358 class RPci; |
|
359 class TAddrSpace; |
|
360 /** |
|
361 This class encapsulates all the various read/write/and modify commands |
|
362 that can be carried out on a PCI memory space. The command is stored user |
|
363 side, and then executed on kernel side when KRun() is called. |
|
364 */ |
|
365 class TUserPciSpace |
|
366 { |
|
367 public: |
|
368 TUserPciSpace() |
|
369 :iPci(NULL), iOperation(EInvalid), iBitWidth(0), iOffset(0), |
|
370 iWriteValue(0), iClearMask(0), iSetMask(0) |
|
371 {} |
|
372 TUserPciSpace(RPci& aPci); |
|
373 |
|
374 /** |
|
375 Perform the encapsulated read/write/or modify |
|
376 @note Only run on kernel side |
|
377 */ |
|
378 TUint KRun(TAddrSpace& aAddrSpace); |
|
379 |
|
380 /** |
|
381 Clone method is required so that multiple threads may |
|
382 have their own copy of a TUserPciSpace (without knowing |
|
383 its runtime type) |
|
384 */ |
|
385 virtual TUserPciSpace* Clone() const = 0; |
|
386 |
|
387 TUint Read(TInt aBitWidth, TUint aOffset) |
|
388 { |
|
389 iOffset = aOffset; |
|
390 iOperation = ERead; |
|
391 iBitWidth = aBitWidth; |
|
392 |
|
393 return Call(); |
|
394 } |
|
395 |
|
396 void Write(TInt aBitWidth, TUint aOffset, TUint aValue) |
|
397 { |
|
398 iOffset = aOffset; |
|
399 iOperation = EWrite; |
|
400 iBitWidth = aBitWidth; |
|
401 |
|
402 iWriteValue = aValue; |
|
403 Call(); |
|
404 } |
|
405 |
|
406 void Modify(TInt aBitWidth, TUint aOffset, TUint aClearMask, TUint aSetMask) |
|
407 { |
|
408 iOffset = aOffset; |
|
409 iOperation = EModify; |
|
410 iBitWidth = aBitWidth; |
|
411 |
|
412 iClearMask = aClearMask; |
|
413 iSetMask = aSetMask; |
|
414 Call(); |
|
415 } |
|
416 |
|
417 protected: |
|
418 /** |
|
419 Makes a request to iPci and passes a copy of this object to |
|
420 the kernel side. |
|
421 */ |
|
422 virtual TUint Call() =0; |
|
423 |
|
424 enum TOperation {EInvalid, ERead, EWrite, EModify}; |
|
425 |
|
426 /** |
|
427 Pointer to a PCI device handle |
|
428 */ |
|
429 RPci* iPci; |
|
430 |
|
431 TOperation iOperation; //!< Type of access to perform |
|
432 TInt iBitWidth; |
|
433 |
|
434 TUint iOffset; |
|
435 TUint32 iWriteValue; |
|
436 TUint32 iClearMask; |
|
437 TUint32 iSetMask; |
|
438 }; |
|
439 |
|
440 /** |
|
441 Grants access to a PCI device's (identified |
|
442 by aPci) config space from user side |
|
443 */ |
|
444 class TUserConfigSpace : public TUserPciSpace |
|
445 { |
|
446 public: |
|
447 TUserConfigSpace() |
|
448 :TUserPciSpace() |
|
449 {} |
|
450 TUserConfigSpace(RPci& aPci); |
|
451 |
|
452 virtual TUserPciSpace* Clone() const; |
|
453 private: |
|
454 TUint Call(); |
|
455 }; |
|
456 |
|
457 /** |
|
458 Grants access to some region of a PCI |
|
459 device's memory space. A PCI device(or function) |
|
460 may have up to 8 distinct memory spaces |
|
461 */ |
|
462 class TUserMemorySpace : public TUserPciSpace |
|
463 { |
|
464 public: |
|
465 TUserMemorySpace() |
|
466 :TUserPciSpace(), iBarIndex(-1) |
|
467 {} |
|
468 |
|
469 TUserMemorySpace(RPci& aPci, TInt aBarIndex); |
|
470 |
|
471 virtual TUserPciSpace* Clone() const; |
|
472 |
|
473 inline TInt BarIndex() {return iBarIndex;} |
|
474 |
|
475 private: |
|
476 TUint Call(); |
|
477 |
|
478 TInt iBarIndex; //< Each PCI function may have up to 8 memory spaces |
|
479 }; |
|
480 |
|
481 #endif //__TPCI_TEST_H |
|
482 |