2 * AcessOS/AcessBasic v0.1
\r
9 #include <fs_devfs.h>
\r
10 #include <drv_pci.h>
\r
11 #include <drv_pci_int.h>
\r
13 #define LIST_DEVICES 1
\r
15 // === STRUCTURES ===
\r
16 typedef struct sPCIDevice
\r
18 Uint16 bus, slot, fcn;
\r
19 Uint16 vendor, device;
\r
20 Uint32 class; // Class:Subclass:ProgIf
\r
22 Uint32 ConfigCache[256/4];
\r
27 // === CONSTANTS ===
\r
28 #define SPACE_STEP 5
\r
29 #define MAX_RESERVED_PORT 0xD00
\r
31 // === PROTOTYPES ===
\r
32 int PCI_Install(char **Arguments);
\r
33 int PCI_ScanBus(int ID, int bFill);
\r
35 char *PCI_int_ReadDirRoot(tVFS_Node *node, int pos);
\r
36 tVFS_Node *PCI_int_FindDirRoot(tVFS_Node *node, const char *filename);
\r
37 Uint32 PCI_int_GetBusAddr(Uint16 Bus, Uint16 Slot, Uint16 Fcn, Uint8 Offset);
\r
38 Uint64 PCI_int_ReadDevice(tVFS_Node *node, Uint64 pos, Uint64 length, void *buffer);
\r
39 int PCI_int_EnumDevice(Uint16 bus, Uint16 dev, Uint16 fcn, tPCIDevice *info);
\r
42 MODULE_DEFINE(0, 0x0100, PCI, PCI_Install, NULL, NULL);
\r
43 int giPCI_BusCount = 1;
\r
44 int giPCI_InodeHandle = -1;
\r
45 int giPCI_DeviceCount = 0;
\r
46 tPCIDevice *gPCI_Devices = NULL;
\r
47 tVFS_NodeType gPCI_RootNodeType = {
\r
48 .TypeName = "PCI Root Node",
\r
49 .ReadDir = PCI_int_ReadDirRoot,
\r
50 .FindDir = PCI_int_FindDirRoot
\r
52 tVFS_NodeType gPCI_DevNodeType = {
\r
53 .TypeName = "PCI Dev Node",
\r
54 .Read = PCI_int_ReadDevice
\r
56 tDevFS_Driver gPCI_DriverStruct = {
\r
59 .Flags = VFS_FFLAG_DIRECTORY,
\r
62 .ACLs = &gVFS_ACL_EveryoneRX,
\r
63 .Type = &gPCI_RootNodeType
\r
66 Uint32 *gaPCI_PortBitmap = NULL;
\r
67 Uint32 gaPCI_BusBitmap[256/32];
\r
71 * \brief Scan the PCI Bus for devices
\r
72 * \param Arguments Boot-time parameters
\r
74 int PCI_Install(char **Arguments)
\r
80 gaPCI_PortBitmap = malloc( 1 << 13 );
\r
81 if( !gaPCI_PortBitmap ) {
\r
82 Log_Error("PCI", "Unable to allocate %i bytes for bitmap", 1 << 13);
\r
83 return MODULE_ERR_MALLOC;
\r
85 memset( gaPCI_PortBitmap, 0, 1 << 13 );
\r
86 for( i = 0; i < MAX_RESERVED_PORT / 32; i ++ )
\r
87 gaPCI_PortBitmap[i] = -1;
\r
88 for( i = 0; i < MAX_RESERVED_PORT % 32; i ++ )
\r
89 gaPCI_PortBitmap[MAX_RESERVED_PORT / 32] = 1 << i;
\r
91 // Scan Bus (Bus 0, Don't fill gPCI_Devices)
\r
92 i = PCI_ScanBus(0, 0);
\r
93 if(i != MODULE_ERR_OK) return i;
\r
95 if(giPCI_DeviceCount == 0) {
\r
96 Log_Notice("PCI", "No devices were found");
\r
97 return MODULE_ERR_NOTNEEDED;
\r
100 // Allocate device buffer
\r
101 tmpPtr = malloc(giPCI_DeviceCount * sizeof(tPCIDevice));
\r
102 if(tmpPtr == NULL) {
\r
103 Log_Warning("PCI", "Malloc ERROR");
\r
104 return MODULE_ERR_MALLOC;
\r
106 gPCI_Devices = tmpPtr;
\r
108 Log_Log("PCI", "%i devices, filling structure", giPCI_DeviceCount);
\r
111 giPCI_DeviceCount = 0;
\r
112 giPCI_BusCount = 0;
\r
113 memset(gaPCI_BusBitmap, 0, sizeof(gaPCI_BusBitmap));
\r
114 // Rescan, filling the PCI device array
\r
117 // Complete Driver Structure
\r
118 gPCI_DriverStruct.RootNode.Size = giPCI_DeviceCount;
\r
120 // And add to DevFS
\r
121 DevFS_AddDevice(&gPCI_DriverStruct);
\r
123 return MODULE_ERR_OK;
\r
127 * \brief Scans a specific PCI Bus
\r
128 * \param BusID PCI Bus ID to scan
\r
129 * \param bFill Fill the \a gPCI_Devices array?
\r
131 int PCI_ScanBus(int BusID, int bFill)
\r
134 tPCIDevice devInfo;
\r
136 if( gaPCI_BusBitmap[BusID/32] & (1 << (BusID%32)) )
\r
137 return MODULE_ERR_OK;
\r
139 gaPCI_BusBitmap[BusID/32] |= (1 << (BusID%32));
\r
141 for( dev = 0; dev < 32; dev++ ) // 32 Devices per bus
\r
143 for( fcn = 0; fcn < 8; fcn++ ) // Max 8 functions per device
\r
145 // Check if the device/function exists
\r
146 if(!PCI_int_EnumDevice(BusID, dev, fcn, &devInfo))
\r
149 if(devInfo.class == PCI_OC_PCIBRIDGE)
\r
153 Log_Log("PCI", "Bridge @ %i,%i:%i (0x%x:0x%x)",
\r
154 BusID, dev, fcn, devInfo.vendor, devInfo.device);
\r
156 //TODO: Handle PCI-PCI Bridges
\r
157 //PCI_ScanBus(devInfo.???, bFill);
\r
164 Log_Log("PCI", "Device %i,%i:%i %06x => 0x%04x:0x%04x",
\r
165 BusID, dev, fcn, devInfo.class, devInfo.vendor, devInfo.device);
\r
170 devInfo.Node.Inode = giPCI_DeviceCount;
\r
171 memcpy(&gPCI_Devices[giPCI_DeviceCount], &devInfo, sizeof(tPCIDevice));
\r
173 giPCI_DeviceCount ++;
\r
175 // If bit 23 of (soemthing) is set, there are sub-functions
\r
176 if(fcn == 0 && !(devInfo.ConfigCache[3] & 0x00800000) )
\r
181 return MODULE_ERR_OK;
\r
185 * \brief Read from Root of PCI Driver
\r
187 char *PCI_int_ReadDirRoot(tVFS_Node *Node, int Pos)
\r
189 ENTER("pNode iPos", Node, Pos);
\r
190 if(Pos < 0 || Pos >= giPCI_DeviceCount) {
\r
195 LEAVE('s', gPCI_Devices[Pos].Name);
\r
196 return strdup( gPCI_Devices[Pos].Name );
\r
200 tVFS_Node *PCI_int_FindDirRoot(tVFS_Node *node, const char *filename)
\r
204 // Validate Filename (Pointer and length)
\r
205 if(!filename || strlen(filename) != 7)
\r
207 // Check for spacers
\r
208 if(filename[2] != '.' || filename[5] != ':')
\r
212 if(filename[0] < '0' || filename[0] > '9') return NULL;
\r
213 bus = (filename[0] - '0')*10;
\r
214 if(filename[1] < '0' || filename[1] > '9') return NULL;
\r
215 bus += filename[1] - '0';
\r
216 if(filename[3] < '0' || filename[3] > '9') return NULL;
\r
217 slot = (filename[3] - '0')*10;
\r
218 if(filename[4] < '0' || filename[4] > '9') return NULL;
\r
219 slot += filename[4] - '0';
\r
220 if(filename[6] < '0' || filename[6] > '9') return NULL;
\r
221 fcn = filename[6] - '0';
\r
224 for(i=0;i<giPCI_DeviceCount;i++)
\r
226 if(gPCI_Devices[i].bus != bus) continue;
\r
227 if(gPCI_Devices[i].slot != slot) continue;
\r
228 if(gPCI_Devices[i].fcn != fcn) continue;
\r
230 return &gPCI_Devices[i].Node;
\r
239 Uint64 PCI_int_ReadDevice(tVFS_Node *node, Uint64 pos, Uint64 length, void *buffer)
\r
241 if( pos + length > 256 ) return 0;
\r
245 (char*)gPCI_Devices[node->Inode].ConfigCache + pos,
\r
251 // --- Kernel Code Interface ---
\r
253 * \brief Counts the devices with the specified codes
\r
254 * \param vendor Vendor ID
\r
255 * \param device Device ID
\r
257 int PCI_CountDevices(Uint16 vendor, Uint16 device)
\r
260 for(i=0;i<giPCI_DeviceCount;i++)
\r
262 if(gPCI_Devices[i].vendor != vendor) continue;
\r
263 if(gPCI_Devices[i].device != device) continue;
\r
270 * \brief Gets the ID of the specified PCI device
\r
271 * \param vendor Vendor ID
\r
272 * \param device Device ID
\r
273 * \param idx Number of matching entry wanted
\r
275 tPCIDev PCI_GetDevice(Uint16 vendor, Uint16 device, int idx)
\r
278 for( i = 0; i < giPCI_DeviceCount; i ++ )
\r
280 if(gPCI_Devices[i].vendor != vendor) continue;
\r
281 if(gPCI_Devices[i].device != device) continue;
\r
282 if(j == idx) return i;
\r
289 * \brief Gets the ID of a device by it's class code
\r
290 * \param class Class Code
\r
291 * \param mask Mask for class comparison
\r
292 * \param prev ID of previous device (-1 for no previous)
\r
294 tPCIDev PCI_GetDeviceByClass(Uint32 class, Uint32 mask, tPCIDev prev)
\r
297 // Check if prev is negative (meaning get first)
\r
298 if(prev < 0) i = 0;
\r
301 for( ; i < giPCI_DeviceCount; i++ )
\r
303 if((gPCI_Devices[i].class & mask) == class)
\r
309 int PCI_GetDeviceInfo(tPCIDev ID, Uint16 *Vendor, Uint16 *Device, Uint32 *Class)
\r
311 tPCIDevice *dev = &gPCI_Devices[ID];
\r
312 if(ID < 0 || ID >= giPCI_DeviceCount) return 1;
\r
314 if(Vendor) *Vendor = dev->vendor;
\r
315 if(Device) *Device = dev->device;
\r
316 if(Class) *Class = dev->class;
\r
320 int PCI_GetDeviceVersion(tPCIDev ID, Uint8 *Revision)
\r
322 tPCIDevice *dev = &gPCI_Devices[ID];
\r
323 if(ID < 0 || ID >= giPCI_DeviceCount) return 1;
\r
325 if(Revision) *Revision = dev->revision;
\r
329 int PCI_GetDeviceSubsys(tPCIDev ID, Uint16 *SubsystemVendor, Uint16 *SubsystemID)
\r
331 tPCIDevice *dev = &gPCI_Devices[ID];
\r
332 if(ID < 0 || ID >= giPCI_DeviceCount) return 1;
\r
334 if(SubsystemVendor) *SubsystemVendor = dev->ConfigCache[0x2c/4] & 0xFFFF;
\r
335 if(SubsystemID) *SubsystemID = dev->ConfigCache[0x2c/4] >> 16;
\r
340 Uint32 PCI_int_GetBusAddr(Uint16 Bus, Uint16 Slot, Uint16 Fcn, Uint8 Offset)
\r
346 return ((Uint32)Bus << 16) | (Slot << 11) | (Fcn << 8) | (Offset & 0xFC);
\r
349 Uint32 PCI_ConfigRead(tPCIDev ID, int Offset, int Size)
\r
352 Uint32 dword, addr;
\r
354 if( ID < 0 || ID >= giPCI_DeviceCount ) return 0;
\r
355 if( Offset < 0 || Offset > 256 ) return 0;
\r
357 // TODO: Should I support non-aligned reads?
\r
358 if( Offset & (Size - 1) ) return 0;
\r
360 dev = &gPCI_Devices[ID];
\r
361 addr = PCI_int_GetBusAddr(dev->bus, dev->slot, dev->fcn, Offset);
\r
363 dword = PCI_CfgReadDWord(addr);
\r
364 gPCI_Devices[ID].ConfigCache[Offset/4] = dword;
\r
367 case 1: return (dword >> (8 * (Offset&3))) & 0xFF;
\r
368 case 2: return (dword >> (8 * (Offset&2))) & 0xFFFF;
\r
369 case 4: return dword;
\r
375 void PCI_ConfigWrite(tPCIDev ID, int Offset, int Size, Uint32 Value)
\r
378 Uint32 dword, addr;
\r
380 if( ID < 0 || ID >= giPCI_DeviceCount ) return ;
\r
381 if( Offset < 0 || Offset > 256 ) return ;
\r
383 dev = &gPCI_Devices[ID];
\r
384 addr = PCI_int_GetBusAddr(dev->bus, dev->slot, dev->fcn, Offset);
\r
387 dword = PCI_CfgReadDWord(addr);
\r
391 shift = (Offset&3)*8;
\r
392 dword &= ~(0xFF << shift);
\r
393 dword |= Value << shift;
\r
396 shift = (Offset&2)*8;
\r
397 dword &= ~(0xFFFF << shift);
\r
398 dword |= Value << shift;
\r
406 PCI_CfgWriteDWord(addr, dword);
\r
410 * \brief Get the IRQ assigned to a device
\r
412 Uint8 PCI_GetIRQ(tPCIDev id)
\r
414 if(id < 0 || id >= giPCI_DeviceCount)
\r
416 return gPCI_Devices[id].ConfigCache[15] & 0xFF;
\r
417 //return PCI_CfgReadByte( gPCI_Devices[id].bus, gPCI_Devices[id].slot, gPCI_Devices[id].fcn, 0x3C);
\r
421 * \brief Read the a BAR (base address register) from the PCI config space
\r
423 Uint32 PCI_GetBAR(tPCIDev id, int BARNum)
\r
425 if(id < 0 || id >= giPCI_DeviceCount)
\r
427 if(BARNum < 0 || BARNum >= 6)
\r
429 return gPCI_Devices[id].ConfigCache[4+BARNum];
\r
433 * \brief Get device information for a slot/function
\r
435 int PCI_int_EnumDevice(Uint16 bus, Uint16 slot, Uint16 fcn, tPCIDevice *info)
\r
437 Uint32 vendor_dev, tmp;
\r
440 addr = PCI_int_GetBusAddr(bus, slot, fcn, 0);
\r
442 vendor_dev = PCI_CfgReadDWord( addr );
\r
443 if((vendor_dev & 0xFFFF) == 0xFFFF) // Invalid Device
\r
446 info->ConfigCache[0] = vendor_dev;
\r
447 for( i = 1, addr += 4; i < 256/4; i ++, addr += 4 )
\r
449 info->ConfigCache[i] = PCI_CfgReadDWord(addr);
\r
455 info->vendor = vendor_dev & 0xFFFF;
\r
456 info->device = vendor_dev >> 16;
\r
457 tmp = info->ConfigCache[2];
\r
458 info->revision = tmp & 0xFF;
\r
459 info->class = tmp >> 8;
\r
461 // #if LIST_DEVICES
\r
462 // Log("BAR0 0x%08x BAR1 0x%08x BAR2 0x%08x", info->ConfigCache[4], info->ConfigCache[5], info->ConfigCache[6]);
\r
463 // Log("BAR3 0x%08x BAR4 0x%08x BAR5 0x%08x", info->ConfigCache[7], info->ConfigCache[8], info->ConfigCache[9]);
\r
464 // Log("Class: 0x%06x", info->class);
\r
468 info->Name[0] = '0' + bus/10;
\r
469 info->Name[1] = '0' + bus%10;
\r
470 info->Name[2] = '.';
\r
471 info->Name[3] = '0' + slot/10;
\r
472 info->Name[4] = '0' + slot%10;
\r
473 info->Name[5] = ':';
\r
474 info->Name[6] = '0' + fcn;
\r
475 info->Name[7] = '\0';
\r
478 memset( &info->Node, 0, sizeof(tVFS_Node) );
\r
479 info->Node.Size = 256;
\r
481 info->Node.NumACLs = 1;
\r
482 info->Node.ACLs = &gVFS_ACL_EveryoneRO;
\r
484 info->Node.Type = &gPCI_RootNodeType;
\r
491 EXPORT(PCI_CountDevices);
\r
492 EXPORT(PCI_GetDevice);
\r
493 EXPORT(PCI_GetDeviceByClass);
\r
494 EXPORT(PCI_GetDeviceInfo);
\r
495 EXPORT(PCI_GetDeviceVersion);
\r
496 EXPORT(PCI_GetDeviceSubsys);
\r
497 //EXPORT(PCI_AssignPort);
\r
498 EXPORT(PCI_GetBAR);
\r
499 EXPORT(PCI_GetIRQ);
\r