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 USE_PORT_BITMAP 0
\r
14 #define LIST_DEVICES 1
\r
16 // === STRUCTURES ===
\r
17 typedef struct sPCIDevice
\r
19 Uint16 bus, slot, fcn;
\r
20 Uint16 vendor, device;
\r
21 Uint32 class; // Class:Subclass:ProgIf
\r
23 Uint32 ConfigCache[256/4];
\r
28 // === CONSTANTS ===
\r
29 #define SPACE_STEP 5
\r
30 #define MAX_RESERVED_PORT 0xD00
\r
32 // === PROTOTYPES ===
\r
33 int PCI_Install(char **Arguments);
\r
34 int PCI_ScanBus(int ID, int bFill);
\r
36 char *PCI_int_ReadDirRoot(tVFS_Node *node, int pos);
\r
37 tVFS_Node *PCI_int_FindDirRoot(tVFS_Node *node, const char *filename);
\r
38 Uint32 PCI_int_GetBusAddr(Uint16 Bus, Uint16 Slot, Uint16 Fcn, Uint8 Offset);
\r
39 size_t PCI_int_ReadDevice(tVFS_Node *node, off_t Offset, size_t Length, void *buffer);
\r
40 int PCI_int_EnumDevice(Uint16 bus, Uint16 dev, Uint16 fcn, tPCIDevice *info);
\r
43 MODULE_DEFINE(0, 0x0100, PCI, PCI_Install, NULL, NULL);
\r
44 int giPCI_BusCount = 1;
\r
45 int giPCI_InodeHandle = -1;
\r
46 int giPCI_DeviceCount = 0;
\r
47 tPCIDevice *gPCI_Devices = NULL;
\r
48 tVFS_NodeType gPCI_RootNodeType = {
\r
49 .TypeName = "PCI Root Node",
\r
50 .ReadDir = PCI_int_ReadDirRoot,
\r
51 .FindDir = PCI_int_FindDirRoot
\r
53 tVFS_NodeType gPCI_DevNodeType = {
\r
54 .TypeName = "PCI Dev Node",
\r
55 .Read = PCI_int_ReadDevice
\r
57 tDevFS_Driver gPCI_DriverStruct = {
\r
60 .Flags = VFS_FFLAG_DIRECTORY,
\r
63 .ACLs = &gVFS_ACL_EveryoneRX,
\r
64 .Type = &gPCI_RootNodeType
\r
68 Uint32 *gaPCI_PortBitmap = NULL;
\r
70 Uint32 gaPCI_BusBitmap[256/32];
\r
74 * \brief Scan the PCI Bus for devices
\r
75 * \param Arguments Boot-time parameters
\r
77 int PCI_Install(char **Arguments)
\r
84 gaPCI_PortBitmap = malloc( 1 << 13 );
\r
85 if( !gaPCI_PortBitmap ) {
\r
86 Log_Error("PCI", "Unable to allocate %i bytes for bitmap", 1 << 13);
\r
87 return MODULE_ERR_MALLOC;
\r
89 memset( gaPCI_PortBitmap, 0, 1 << 13 );
\r
91 for( i = 0; i < MAX_RESERVED_PORT / 32; i ++ )
\r
92 gaPCI_PortBitmap[i] = -1;
\r
93 for( i = 0; i < MAX_RESERVED_PORT % 32; i ++ )
\r
94 gaPCI_PortBitmap[MAX_RESERVED_PORT / 32] = 1 << i;
\r
97 // Scan Bus (Bus 0, Don't fill gPCI_Devices)
\r
98 for( bus = 0; bus < giPCI_BusCount; bus ++ )
\r
100 ret = PCI_ScanBus(bus, 0);
\r
101 if(ret != MODULE_ERR_OK) return ret;
\r
104 if(giPCI_DeviceCount == 0) {
\r
105 Log_Notice("PCI", "No devices were found");
\r
106 return MODULE_ERR_NOTNEEDED;
\r
109 // Allocate device buffer
\r
110 tmpPtr = malloc(giPCI_DeviceCount * sizeof(tPCIDevice));
\r
111 if(tmpPtr == NULL) {
\r
112 Log_Warning("PCI", "Malloc ERROR");
\r
113 return MODULE_ERR_MALLOC;
\r
115 gPCI_Devices = tmpPtr;
\r
117 Log_Log("PCI", "%i devices, filling structure", giPCI_DeviceCount);
\r
120 giPCI_DeviceCount = 0;
\r
121 giPCI_BusCount = 1;
\r
122 memset(gaPCI_BusBitmap, 0, sizeof(gaPCI_BusBitmap));
\r
123 // Rescan, filling the PCI device array
\r
124 for( bus = 0; bus < giPCI_BusCount; bus ++ )
\r
126 PCI_ScanBus(bus, 1);
\r
129 // Complete Driver Structure
\r
130 gPCI_DriverStruct.RootNode.Size = giPCI_DeviceCount;
\r
132 // And add to DevFS
\r
133 DevFS_AddDevice(&gPCI_DriverStruct);
\r
135 return MODULE_ERR_OK;
\r
139 * \brief Scans a specific PCI Bus
\r
140 * \param BusID PCI Bus ID to scan
\r
141 * \param bFill Fill the \a gPCI_Devices array?
\r
143 int PCI_ScanBus(int BusID, int bFill)
\r
146 tPCIDevice devInfo;
\r
148 if( gaPCI_BusBitmap[BusID/32] & (1 << (BusID%32)) )
\r
149 return MODULE_ERR_OK;
\r
151 gaPCI_BusBitmap[BusID/32] |= (1 << (BusID%32));
\r
153 for( dev = 0; dev < 32; dev++ ) // 32 Devices per bus
\r
155 for( fcn = 0; fcn < 8; fcn++ ) // Max 8 functions per device
\r
157 // Check if the device/function exists
\r
158 if(!PCI_int_EnumDevice(BusID, dev, fcn, &devInfo))
\r
161 if(devInfo.class == PCI_OC_PCIBRIDGE)
\r
165 Log_Log("PCI", "Bridge @ %i,%i:%i (0x%x:0x%x)",
\r
166 BusID, dev, fcn, devInfo.vendor, devInfo.device);
\r
168 //TODO: Handle PCI-PCI Bridges
\r
169 //PCI_ScanBus(devInfo.???, bFill);
\r
176 Log_Log("PCI", "Device %i,%i:%i %06x => 0x%04x:0x%04x",
\r
177 BusID, dev, fcn, devInfo.class, devInfo.vendor, devInfo.device);
\r
182 devInfo.Node.Inode = giPCI_DeviceCount;
\r
183 memcpy(&gPCI_Devices[giPCI_DeviceCount], &devInfo, sizeof(tPCIDevice));
\r
185 giPCI_DeviceCount ++;
\r
187 // If bit 23 of (soemthing) is set, there are sub-functions
\r
188 if(fcn == 0 && !(devInfo.ConfigCache[3] & 0x00800000) )
\r
193 return MODULE_ERR_OK;
\r
197 * \brief Read from Root of PCI Driver
\r
199 char *PCI_int_ReadDirRoot(tVFS_Node *Node, int Pos)
\r
201 ENTER("pNode iPos", Node, Pos);
\r
202 if(Pos < 0 || Pos >= giPCI_DeviceCount) {
\r
207 LEAVE('s', gPCI_Devices[Pos].Name);
\r
208 return strdup( gPCI_Devices[Pos].Name );
\r
212 tVFS_Node *PCI_int_FindDirRoot(tVFS_Node *node, const char *filename)
\r
216 // Validate Filename (Pointer and length)
\r
217 if(!filename || strlen(filename) != 7)
\r
219 // Check for spacers
\r
220 if(filename[2] != '.' || filename[5] != ':')
\r
224 if(filename[0] < '0' || filename[0] > '9') return NULL;
\r
225 bus = (filename[0] - '0')*10;
\r
226 if(filename[1] < '0' || filename[1] > '9') return NULL;
\r
227 bus += filename[1] - '0';
\r
228 if(filename[3] < '0' || filename[3] > '9') return NULL;
\r
229 slot = (filename[3] - '0')*10;
\r
230 if(filename[4] < '0' || filename[4] > '9') return NULL;
\r
231 slot += filename[4] - '0';
\r
232 if(filename[6] < '0' || filename[6] > '9') return NULL;
\r
233 fcn = filename[6] - '0';
\r
236 for(i=0;i<giPCI_DeviceCount;i++)
\r
238 if(gPCI_Devices[i].bus != bus) continue;
\r
239 if(gPCI_Devices[i].slot != slot) continue;
\r
240 if(gPCI_Devices[i].fcn != fcn) continue;
\r
242 return &gPCI_Devices[i].Node;
\r
250 * \brief Read the PCI configuration space of a device
\r
252 size_t PCI_int_ReadDevice(tVFS_Node *node, off_t pos, size_t length, void *buffer)
\r
254 if( pos + length > 256 ) return 0;
\r
258 (char*)gPCI_Devices[node->Inode].ConfigCache + pos,
\r
264 // --- Kernel Code Interface ---
\r
266 * \brief Counts the devices with the specified codes
\r
267 * \param vendor Vendor ID
\r
268 * \param device Device ID
\r
270 int PCI_CountDevices(Uint16 vendor, Uint16 device)
\r
273 for(i=0;i<giPCI_DeviceCount;i++)
\r
275 if(gPCI_Devices[i].vendor != vendor) continue;
\r
276 if(gPCI_Devices[i].device != device) continue;
\r
283 * \brief Gets the ID of the specified PCI device
\r
284 * \param vendor Vendor ID
\r
285 * \param device Device ID
\r
286 * \param idx Number of matching entry wanted
\r
288 tPCIDev PCI_GetDevice(Uint16 vendor, Uint16 device, int idx)
\r
291 for( i = 0; i < giPCI_DeviceCount; i ++ )
\r
293 if(gPCI_Devices[i].vendor != vendor) continue;
\r
294 if(gPCI_Devices[i].device != device) continue;
\r
295 if(j == idx) return i;
\r
302 * \brief Gets the ID of a device by it's class code
\r
303 * \param class Class Code
\r
304 * \param mask Mask for class comparison
\r
305 * \param prev ID of previous device (-1 for no previous)
\r
307 tPCIDev PCI_GetDeviceByClass(Uint32 class, Uint32 mask, tPCIDev prev)
\r
310 // Check if prev is negative (meaning get first)
\r
311 if(prev < 0) i = 0;
\r
314 for( ; i < giPCI_DeviceCount; i++ )
\r
316 if((gPCI_Devices[i].class & mask) == class)
\r
322 int PCI_GetDeviceInfo(tPCIDev ID, Uint16 *Vendor, Uint16 *Device, Uint32 *Class)
\r
324 tPCIDevice *dev = &gPCI_Devices[ID];
\r
325 if(ID < 0 || ID >= giPCI_DeviceCount) return 1;
\r
327 if(Vendor) *Vendor = dev->vendor;
\r
328 if(Device) *Device = dev->device;
\r
329 if(Class) *Class = dev->class;
\r
333 int PCI_GetDeviceVersion(tPCIDev ID, Uint8 *Revision)
\r
335 tPCIDevice *dev = &gPCI_Devices[ID];
\r
336 if(ID < 0 || ID >= giPCI_DeviceCount) return 1;
\r
338 if(Revision) *Revision = dev->revision;
\r
342 int PCI_GetDeviceSubsys(tPCIDev ID, Uint16 *SubsystemVendor, Uint16 *SubsystemID)
\r
344 tPCIDevice *dev = &gPCI_Devices[ID];
\r
345 if(ID < 0 || ID >= giPCI_DeviceCount) return 1;
\r
347 if(SubsystemVendor) *SubsystemVendor = dev->ConfigCache[0x2c/4] & 0xFFFF;
\r
348 if(SubsystemID) *SubsystemID = dev->ConfigCache[0x2c/4] >> 16;
\r
353 Uint32 PCI_int_GetBusAddr(Uint16 Bus, Uint16 Slot, Uint16 Fcn, Uint8 Offset)
\r
359 return ((Uint32)Bus << 16) | (Slot << 11) | (Fcn << 8) | (Offset & 0xFC);
\r
362 Uint32 PCI_ConfigRead(tPCIDev ID, int Offset, int Size)
\r
365 Uint32 dword, addr;
\r
367 if( ID < 0 || ID >= giPCI_DeviceCount ) return 0;
\r
368 if( Offset < 0 || Offset > 256 ) return 0;
\r
370 // TODO: Should I support non-aligned reads?
\r
371 if( Offset & (Size - 1) ) return 0;
\r
373 dev = &gPCI_Devices[ID];
\r
374 addr = PCI_int_GetBusAddr(dev->bus, dev->slot, dev->fcn, Offset);
\r
376 dword = PCI_CfgReadDWord(addr);
\r
377 gPCI_Devices[ID].ConfigCache[Offset/4] = dword;
\r
380 case 1: return (dword >> (8 * (Offset&3))) & 0xFF;
\r
381 case 2: return (dword >> (8 * (Offset&2))) & 0xFFFF;
\r
382 case 4: return dword;
\r
388 void PCI_ConfigWrite(tPCIDev ID, int Offset, int Size, Uint32 Value)
\r
391 Uint32 dword, addr;
\r
393 if( ID < 0 || ID >= giPCI_DeviceCount ) return ;
\r
394 if( Offset < 0 || Offset > 256 ) return ;
\r
396 dev = &gPCI_Devices[ID];
\r
397 addr = PCI_int_GetBusAddr(dev->bus, dev->slot, dev->fcn, Offset);
\r
400 dword = PCI_CfgReadDWord(addr);
\r
404 shift = (Offset&3)*8;
\r
405 dword &= ~(0xFF << shift);
\r
406 dword |= Value << shift;
\r
409 shift = (Offset&2)*8;
\r
410 dword &= ~(0xFFFF << shift);
\r
411 dword |= Value << shift;
\r
419 PCI_CfgWriteDWord(addr, dword);
\r
423 * \brief Get the IRQ assigned to a device
\r
425 Uint8 PCI_GetIRQ(tPCIDev id)
\r
427 if(id < 0 || id >= giPCI_DeviceCount)
\r
429 return gPCI_Devices[id].ConfigCache[15] & 0xFF;
\r
430 //return PCI_CfgReadByte( gPCI_Devices[id].bus, gPCI_Devices[id].slot, gPCI_Devices[id].fcn, 0x3C);
\r
434 * \brief Read the a BAR (base address register) from the PCI config space
\r
436 Uint32 PCI_GetBAR(tPCIDev id, int BARNum)
\r
438 if(id < 0 || id >= giPCI_DeviceCount)
\r
440 if(BARNum < 0 || BARNum >= 6)
\r
442 return gPCI_Devices[id].ConfigCache[4+BARNum];
\r
446 * \brief Get device information for a slot/function
\r
448 int PCI_int_EnumDevice(Uint16 bus, Uint16 slot, Uint16 fcn, tPCIDevice *info)
\r
450 Uint32 vendor_dev, tmp;
\r
453 addr = PCI_int_GetBusAddr(bus, slot, fcn, 0);
\r
455 vendor_dev = PCI_CfgReadDWord( addr );
\r
456 if((vendor_dev & 0xFFFF) == 0xFFFF) // Invalid Device
\r
459 info->ConfigCache[0] = vendor_dev;
\r
460 for( i = 1, addr += 4; i < 256/4; i ++, addr += 4 )
\r
462 info->ConfigCache[i] = PCI_CfgReadDWord(addr);
\r
468 info->vendor = vendor_dev & 0xFFFF;
\r
469 info->device = vendor_dev >> 16;
\r
470 tmp = info->ConfigCache[2];
\r
471 info->revision = tmp & 0xFF;
\r
472 info->class = tmp >> 8;
\r
474 // #if LIST_DEVICES
\r
475 // Log("BAR0 0x%08x BAR1 0x%08x BAR2 0x%08x", info->ConfigCache[4], info->ConfigCache[5], info->ConfigCache[6]);
\r
476 // Log("BAR3 0x%08x BAR4 0x%08x BAR5 0x%08x", info->ConfigCache[7], info->ConfigCache[8], info->ConfigCache[9]);
\r
477 // Log("Class: 0x%06x", info->class);
\r
481 info->Name[0] = '0' + bus/10;
\r
482 info->Name[1] = '0' + bus%10;
\r
483 info->Name[2] = '.';
\r
484 info->Name[3] = '0' + slot/10;
\r
485 info->Name[4] = '0' + slot%10;
\r
486 info->Name[5] = ':';
\r
487 info->Name[6] = '0' + fcn;
\r
488 info->Name[7] = '\0';
\r
491 memset( &info->Node, 0, sizeof(tVFS_Node) );
\r
492 info->Node.Size = 256;
\r
494 info->Node.NumACLs = 1;
\r
495 info->Node.ACLs = &gVFS_ACL_EveryoneRO;
\r
497 info->Node.Type = &gPCI_RootNodeType;
\r
504 EXPORT(PCI_CountDevices);
\r
505 EXPORT(PCI_GetDevice);
\r
506 EXPORT(PCI_GetDeviceByClass);
\r
507 EXPORT(PCI_GetDeviceInfo);
\r
508 EXPORT(PCI_GetDeviceVersion);
\r
509 EXPORT(PCI_GetDeviceSubsys);
\r
510 //EXPORT(PCI_AssignPort);
\r
511 EXPORT(PCI_GetBAR);
\r
512 EXPORT(PCI_GetIRQ);
\r