forked from sachithkadamba/CocoaSampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppleSamplePCI.cpp
429 lines (365 loc) · 15.1 KB
/
AppleSamplePCI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//
// File: AppleSamplePCI.cpp
//
// Abstract: Sample PCI device driver
//
// Version: 2.0
//
// Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. ("Apple")
// in consideration of your agreement to the following terms, and your use,
// installation, modification or redistribution of this Apple software
// constitutes acceptance of these terms. If you do not agree with these
// terms, please do not use, install, modify or redistribute this Apple
// software.
//
// In consideration of your agreement to abide by the following terms, and
// subject to these terms, Apple grants you a personal, non - exclusive
// license, under Apple's copyrights in this original Apple software ( the
// "Apple Software" ), to use, reproduce, modify and redistribute the Apple
// Software, with or without modifications, in source and / or binary forms;
// provided that if you redistribute the Apple Software in its entirety and
// without modifications, you must retain this notice and the following text
// and disclaimers in all such redistributions of the Apple Software. Neither
// the name, trademarks, service marks or logos of Apple Inc. may be used to
// endorse or promote products derived from the Apple Software without specific
// prior written permission from Apple. Except as expressly stated in this
// notice, no other rights or licenses, express or implied, are granted by
// Apple herein, including but not limited to any patent rights that may be
// infringed by your derivative works or by other works in which the Apple
// Software may be incorporated.
//
// The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
// WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
// WARRANTIES OF NON - INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION
// ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
//
// IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
// CONSEQUENTIAL DAMAGES ( INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION ) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION
// AND / OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER
// UNDER THEORY OF CONTRACT, TORT ( INCLUDING NEGLIGENCE ), STRICT LIABILITY OR
// OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright ( C ) 2008 Apple Inc. All Rights Reserved.
//
/*
* This is a tiny driver that attaches to a PCI device and logs information
* about it. It doesn't alter the device in any way. It also supports a
* generic IOUserClient subclass that allows driver specific client code to
* make various kinds of calls into the driver, and map shared memory
* or portions of hardware memory.
*/
#include <IOKit/IOBufferMemoryDescriptor.h>
#include <IOKit/pci/IOPCIDevice.h>
#if IOMEMORYDESCRIPTOR_SUPPORTS_DMACOMMAND
#include <IOKit/IODMACommand.h>
#endif
#include "AppleSamplePCI.h"
#include <IOKit/IOLib.h>
#include <IOKit/assert.h>
/*
* Define the metaclass information that is used for runtime
* typechecking of IOKit objects. We're a subclass of IOService,
* but usually we would subclass from a family class.
*/
#define super IOService
/*
* even though we are defining the convenience macro super for the superclass, you must use the actual class name
* in the OS*MetaClass macros. Note that the class name is different when supporting PowerPC on 10.4.
*/
#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
OSDefineMetaClassAndStructors( com_YourCompany_driver_SamplePCI_10_4, IOService )
#else
OSDefineMetaClassAndStructors( com_YourCompany_driver_SamplePCI, IOService )
#endif
// This function will be called when the user process calls IORegistryEntrySetCFProperties on
// this driver. You can add your custom functionality to this function.
IOReturn SamplePCIClassName::setProperties(OSObject* properties)
{
OSDictionary* dict;
OSNumber* number;
dict = OSDynamicCast(OSDictionary, properties);
if (!dict) {
return kIOReturnBadArgument;
}
// we're adding the property to the registry here
number = OSDynamicCast(OSNumber, dict->getObject(kMyDisplayValueKey));
if (number) {
uint32_t value = number->unsigned32BitValue();
IOLog("%s[%p]::%s(%p) got value %u\n", getName(), this, __FUNCTION__, properties, value);
updateRegistry(value);
return kIOReturnSuccess;
}
else {
return super::setProperties(properties);
}
}
// updateRegistry does the actual I/O Registry update.
// It is important to note that we work on a copy of the section of the I/O Registry
// until the actual reinsertion into the I/O Registry.
// The setProperty call is serialized for us and is the only safe way to
// handle this.
void SamplePCIClassName::updateRegistry(UInt32 value)
{
// Directly changing a collection in the I/O Registry is not supported as it is not protected against
// multiple writers. So expose a copy and work on that instead.
OSDictionary* dict = OSDynamicCast(OSDictionary, copyProperty(kMyDisplayParametersKey));
OSDictionary* copyDict = (OSDictionary *) dict->copyCollection();
if (copyDict != NULL) {
OSDictionary* copyBrightnessDict = OSDynamicCast(OSDictionary, copyDict->getObject(kMyDisplayBrightnessKey));
if (copyBrightnessDict != NULL) {
OSNumber* num = OSDynamicCast(OSNumber, copyBrightnessDict->getObject(kMyDisplayValueKey));
if (num != NULL) {
num->setValue(value);
// setProperty correctly serializes I/O Registry updates for our protection.
setProperty(kMyDisplayParametersKey, copyDict);
}
}
copyDict->release();
}
}
bool SamplePCIClassName::start( IOService* provider )
{
IOMemoryDescriptor * mem;
IOMemoryMap * map;
IOLog("%s[%p]::%s(%p)\n", getName(), this, __FUNCTION__, provider);
if (!super::start( provider ))
return false;
/*
* Our provider class is specified in the driver property table
* as IOPCIDevice, so the provider must be of that class.
* The assert is just to make absolutely sure for debugging.
*/
assert( OSDynamicCast( IOPCIDevice, provider ));
fPCIDevice = (IOPCIDevice *) provider;
/*
* Enable memory response from the card
*/
fPCIDevice->setMemoryEnable( true );
/*
* Log some info about the device
*/
/* Print all the device's memory ranges */
for ( uint32_t index = 0; index < fPCIDevice->getDeviceMemoryCount(); index++ ) {
mem = fPCIDevice->getDeviceMemoryWithIndex( index );
assert( mem );
IOLog("Range[%d] " PhysAddr_FORMAT ":" ByteCount_FORMAT "\n", index,
mem->getPhysicalAddress(), mem->getLength());
}
/* look up a range based on its config space base address register */
mem = fPCIDevice->getDeviceMemoryWithRegister(
kIOPCIConfigBaseAddress0 );
if ( mem )
IOLog("Range@0x%x " PhysAddr_FORMAT ":" ByteCount_FORMAT "\n", kIOPCIConfigBaseAddress0,
mem->getPhysicalAddress(), mem->getLength());
/* Map a range based on its config space base address register,
* This is how the driver gets access to its memory-mapped registers.
* The getVirtualAddress() method returns a kernel virtual address
* for the register mapping */
map = fPCIDevice->mapDeviceMemoryWithRegister(
kIOPCIConfigBaseAddress0 );
if ( map ) {
IOLog("Range@0x%x (" PhysAddr_FORMAT ") mapped to kernel virtual address " VirtAddr_FORMAT "\n",
kIOPCIConfigBaseAddress0,
map->getPhysicalAddress(),
map->getVirtualAddress()
);
/* Release the map object, and the mapping itself */
map->release();
}
/* Read a config space register */
IOLog("Config register@0x%x = " UInt32_FORMAT "\n", kIOPCIConfigCommand,
fPCIDevice->configRead32(kIOPCIConfigCommand) );
// Construct a memory descriptor for a buffer below the 4Gb physical line &
// so addressable by 32-bit DMA. This could be used for a
// DMA program buffer, for example.
IOBufferMemoryDescriptor * bmd =
#if defined(__ppc__) && (MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4)
IOBufferMemoryDescriptor::withOptions(kIOMemoryPhysicallyContiguous, 64 * 1024, page_size);
#else
IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
// task to hold the memory
kernel_task,
// options
kIOMemoryPhysicallyContiguous,
// size
64*1024,
// physicalMask - 32 bit addressable and page aligned
0x00000000FFFFF000ULL);
#endif
if (bmd) {
generateDMAAddresses(bmd);
} else {
IOLog("IOBufferMemoryDescriptor::inTaskWithPhysicalMask failed\n");
}
fLowMemory = bmd;
/* Publish ourselves so clients can find us */
registerService();
return true;
}
/*
* We'll come here when the device goes away, or the driver is unloaded.
*/
void SamplePCIClassName::stop( IOService* provider )
{
IOLog("%s[%p]::%s(%p)\n", getName(), this, __FUNCTION__, provider);
super::stop( provider );
}
/*
* Method to supply an IOMemoryDescriptor for the user client to map into
* the client process. This sample just supplies all of the hardware memory
* associated with the PCI device's Base Address Register 0.
* In a real driver mapping hardware memory would only ever be used in some
* limited high performance scenarios where the device range can be safely
* accessed by client code with compromising system stability.
*/
IOMemoryDescriptor * SamplePCIClassName::copyGlobalMemory( void )
{
IOMemoryDescriptor* memory;
memory = fPCIDevice->getDeviceMemoryWithRegister( kIOPCIConfigBaseAddress0 );
if( memory)
memory->retain();
return memory;
}
#if defined(__ppc__) && (MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4)
IOReturn SamplePCIClassName::generateDMAAddresses( IOMemoryDescriptor* memDesc )
{
// Get the physical segment list. These could be used to generate a scatter gather
// list for hardware.
// This is the old getPhysicalSegment() loop calling IOMemoryDescriptor.
// It will fail (panic) on systems with physical memory above the 4GiB line.
IOByteCount offset = 0;
IOPhysicalAddress physicalAddr;
IOPhysicalLength segmentLength;
uint32_t index = 0;
while ((physicalAddr = memDesc->getPhysicalSegment(offset, &segmentLength))) {
IOLog("Physical segment(%u) " PhysAddr_FORMAT ":" ByteCount_FORMAT "\n", index, physicalAddr, segmentLength);
offset += segmentLength;
index++;
}
return kIOReturnSuccess;
}
#else
IOReturn SamplePCIClassName::generateDMAAddresses( IOMemoryDescriptor* memDesc )
{
// Get the physical segment list. These could be used to generate a scatter gather
// list for hardware.
IODMACommand* cmd;
IOReturn err = kIOReturnSuccess;
// 64 bit physical address generation using IODMACommand
do
{
cmd = IODMACommand::withSpecification(
// outSegFunc - Host endian since we read the address data with the cpu
// and 64 bit wide quantities
kIODMACommandOutputHost64,
// numAddressBits
64,
// maxSegmentSize - zero for unrestricted physically contiguous chunks
0,
// mappingOptions - kMapped for DMA addresses
IODMACommand::kMapped,
// maxTransferSize - no restriction
0,
// alignment - no restriction
1 );
if (!cmd)
{
IOLog("IODMACommand::withSpecification failed\n");
break;
}
// Point at the memory descriptor and use the auto prepare option
// to prepare the entire range
err = cmd->setMemoryDescriptor(memDesc);
if (kIOReturnSuccess != err)
{
IOLog("setMemoryDescriptor failed (0x%08x)\n", err);
break;
}
UInt64 offset = 0;
while ((kIOReturnSuccess == err) && (offset < memDesc->getLength()))
{
// use the 64 bit variant to match outSegFunc
IODMACommand::Segment64 segments[1];
UInt32 numSeg = 1;
// use the 64 bit variant to match outSegFunc
err = cmd->gen64IOVMSegments(&offset, &segments[0], &numSeg);
IOLog("gen64IOVMSegments(%x) addr 0x%016llx, len %llu, nsegs " UInt32_FORMAT "\n",
err, segments[0].fIOVMAddr, segments[0].fLength, numSeg);
}
// if we had a DMA controller, kick off the DMA here
// when the DMA has completed,
// clear the memory descriptor and use the auto complete option
// to complete the transaction
err = cmd->clearMemoryDescriptor();
if (kIOReturnSuccess != err)
{
IOLog("clearMemoryDescriptor failed (0x%08x)\n", err);
}
}
while (false);
if (cmd)
cmd->release();
// end 64 bit loop
// 32 bit physical address generation using IODMACommand
// any memory above 4GiB in the memory descriptor will be bounce-buffered
// to memory below the 4GiB line on machines without remapping HW support
do
{
cmd = IODMACommand::withSpecification(
// outSegFunc - Host endian since we read the address data with the cpu
// and 32 bit wide quantities
kIODMACommandOutputHost32,
// numAddressBits
32,
// maxSegmentSize - zero for unrestricted physically contiguous chunks
0,
// mappingOptions - kMapped for DMA addresses
IODMACommand::kMapped,
// maxTransferSize - no restriction
0,
// alignment - no restriction
1 );
if (!cmd)
{
IOLog("IODMACommand::withSpecification failed\n");
break;
}
// point at the memory descriptor and use the auto prepare option
// to prepare the entire range
err = cmd->setMemoryDescriptor(memDesc);
if (kIOReturnSuccess != err)
{
IOLog("setMemoryDescriptor failed (0x%08x)\n", err);
break;
}
UInt64 offset = 0;
while ((kIOReturnSuccess == err) && (offset < memDesc->getLength()))
{
// use the 32 bit variant to match outSegFunc
IODMACommand::Segment32 segments[1];
UInt32 numSeg = 1;
// use the 32 bit variant to match outSegFunc
err = cmd->gen32IOVMSegments(&offset, &segments[0], &numSeg);
IOLog("gen32IOVMSegments(%x) addr " UInt32_x_FORMAT ", len " UInt32_FORMAT ", nsegs " UInt32_FORMAT "\n",
err, segments[0].fIOVMAddr, segments[0].fLength, numSeg);
}
// if we had a DMA controller, kick off the DMA here
// when the DMA has completed,
// clear the memory descriptor and use the auto complete option
// to complete the transaction
err = cmd->clearMemoryDescriptor();
if (kIOReturnSuccess != err)
{
IOLog("clearMemoryDescriptor failed (0x%08x)\n", err);
}
}
while (false);
if (cmd)
cmd->release();
// end 32 bit loop
return (err);
}
#endif