- Fixed memory management.
[planlOS.git] / system / kernel / ke / pci.c
blob3bf586983d6724eb02f08e85cb92c54d6756560f
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "ke/pci.h"
23 #include "ke/errors.h"
24 #include "ke/ports.h"
25 #include "ke/debug.h"
26 #include <stdlib.h>
27 #include <string.h>
29 #define KE_PCI_CONFIG_ADDRESS 0xCF8
30 #define KE_PCI_CONFIG_DATA 0xCFC
32 #define KE_PCI_DEVICE_VENDOR 0x0
33 #define KE_PCI_DEVICE_CLASS 0x8
34 #define KE_PCI_DEVICE_HEADERTYPE 0x0C
35 #define KE_PCI_DEVICE_BAR0 0x10
36 #define KE_PCI_DEVICE_IRQ 0x3C
38 static KePCIDevice *devices = 0;
39 static uint32_t devicecount = 0;
41 static uint32_t kePCIRead(uint8_t bus, uint8_t slot, uint8_t func, uint8_t reg)
43 uint32_t addr = 0x80000000 | (bus << 16) | ((slot & 0x1F) << 11) | ((func & 0x7) << 8) | reg;
44 outl(KE_PCI_CONFIG_ADDRESS, addr);
45 return inl(KE_PCI_CONFIG_DATA);
47 static void kePCIWrite(uint8_t bus, uint8_t slot, uint8_t func, uint8_t reg, uint32_t data)
49 uint32_t addr = 0x80000000 | (bus << 16) | ((slot & 0xF) << 12) | ((func & 0xF) << 8) | reg;
50 outl(KE_PCI_CONFIG_ADDRESS, addr);
51 outl(KE_PCI_CONFIG_DATA, data);
54 int keInitPCI(void)
56 devicecount = 0;
57 uint32_t bus;
58 uint32_t slot;
59 uint32_t func;
60 // Count devices
61 for (bus = 0; bus < 8; bus++)
63 for (slot = 0; slot < 32; slot++)
65 for (func = 0; func < 8; func++)
67 uint32_t device = kePCIRead(bus, slot, func, KE_PCI_DEVICE_VENDOR);
68 if (device != 0xFFFFFFFF)
70 devicecount++;
75 // Allocate memory
76 devices = malloc(sizeof(KePCIDevice) * devicecount);
77 // Read device info
78 uint32_t currentdevice = 0;
79 for (bus = 0; bus < 8; bus++)
81 for (slot = 0; slot < 32; slot++)
83 for (func = 0; func < 8; func++)
85 uint32_t device = kePCIRead(bus, slot, func, KE_PCI_DEVICE_VENDOR);
86 if (((device >> 16) != 0xFFFF) && ((device >> 16) != 0x0))
88 keGetPCIDevice(&devices[currentdevice], bus, slot, func);
89 currentdevice++;
94 return 0;
97 int keGetPCIDevice(KePCIDevice *device, uint8_t bus, uint8_t slot, uint8_t func)
99 if (!device)
101 return KE_ERROR_UNKNOWN;
103 memset(device, 0, sizeof(KePCIDevice));
104 // Vendor/device ID
105 uint32_t devinfo = kePCIRead(bus, slot, func, KE_PCI_DEVICE_VENDOR);
106 if (devinfo == 0xFFFF) return KE_ERROR_UNKNOWN;
107 device->device = devinfo >> 16;
108 device->vendor = devinfo & 0xFFFF;
109 // Class
110 uint32_t class = kePCIRead(bus, slot, func, KE_PCI_DEVICE_CLASS);
111 device->devclass = class >> 24;
112 device->subclass = class >> 16;
113 device->revision = class;
114 device->irq = kePCIRead(bus, slot, func, KE_PCI_DEVICE_IRQ);
115 kePrint("PCI device at %x:%x:%x: %x/%x, class: %x:%x\n", bus, slot, func, device->vendor, device->device, device->devclass, device->subclass);
116 // Memory/ports
117 uint8_t headertype = kePCIRead(bus, slot, func, KE_PCI_DEVICE_HEADERTYPE) >> 16;
118 if (!headertype)
120 // Not a bridge
121 uint32_t i;
122 for (i = 0; i < 6; i++)
124 uint32_t addr = kePCIRead(bus, slot, func, KE_PCI_DEVICE_BAR0 + i * 4);
125 if (!addr) break;
126 device->rescount++;
127 device->res[i].addr = addr & ~0xF;
128 device->res[i].type = addr & 0x1;
129 // Get size
130 kePCIWrite(bus, slot, func, KE_PCI_DEVICE_BAR0 + i * 4, 0xFFFFFFFF);
131 uint32_t size = kePCIRead(bus, slot, func, KE_PCI_DEVICE_BAR0 + i * 4);
132 device->res[i].size = ~(size & ~0xF) + 1;
133 kePCIWrite(bus, slot, func, KE_PCI_DEVICE_BAR0 + i * 4, addr);
134 kePrint("BAR: %x: %x\n", device->res[i].addr, device->res[i].type);
138 return 0;
140 int keGetPCIDeviceCount(void)
142 return devicecount;
144 KePCIDevice *keGetPCIDevices(void)
146 return devices;