1 #include <console/console.h>
2 #include <device/device.h>
3 #include <device/path.h>
4 #include <device/pci.h>
8 * @brief See if a device structure exists for path
10 * @param bus The bus to find the device on
11 * @param path The relative path from the bus to the appropriate device
12 * @return pointer to a device structure for the device on bus at path
13 * or 0/NULL if no device is found
15 device_t find_dev_path(struct bus *parent, struct device_path *path)
18 for(child = parent->children; child; child = child->sibling) {
19 if (path_eq(path, &child->path)) {
27 * @brief See if a device structure already exists and if not allocate it
29 * @param bus The bus to find the device on
30 * @param path The relative path from the bus to the appropriate device
31 * @return pointer to a device structure for the device on bus at path
33 device_t alloc_find_dev(struct bus *parent, struct device_path *path)
36 child = find_dev_path(parent, path);
38 child = alloc_dev(parent, path);
44 * Given a bus and a devfn number, find the device structure
45 * @param bus The bus number
46 * @param devfn a device/function number
47 * @return pointer to the device structure
49 struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
51 struct device *dev, *result;
54 for (dev = all_devices; dev; dev = dev->next) {
55 if ((dev->path.type == DEVICE_PATH_PCI) &&
56 (dev->bus->secondary == bus) &&
57 (dev->path.u.pci.devfn == devfn)) {
65 /** Find a device of a given vendor and type
66 * @param vendor Vendor ID (e.g. 0x8086 for Intel)
67 * @param device Device ID
68 * @param from Pointer to the device structure, used as a starting point
69 * in the linked list of all_devices, which can be 0 to start at the
70 * head of the list (i.e. all_devices)
71 * @return Pointer to the device struct
73 struct device *dev_find_device(unsigned int vendor, unsigned int device, struct device *from)
79 while (from && (from->vendor != vendor || from->device != device)) {
85 /** Find a device of a given class
86 * @param class Class of the device
87 * @param from Pointer to the device structure, used as a starting point
88 * in the linked list of all_devices, which can be 0 to start at the
89 * head of the list (i.e. all_devices)
90 * @return Pointer to the device struct
92 struct device *dev_find_class(unsigned int class, struct device *from)
98 while (from && (from->class & 0xffffff00) != class)
104 const char *dev_path(device_t dev)
106 static char buffer[DEVICE_PATH_MAX];
109 memcpy(buffer, "<null>", 7);
112 switch(dev->path.type) {
113 case DEVICE_PATH_ROOT:
114 memcpy(buffer, "Root Device", 12);
116 case DEVICE_PATH_DEFAULT_CPU:
117 memcpy(buffer, "Default CPU", 12);
119 case DEVICE_PATH_PCI:
120 sprintf(buffer, "PCI: %02x:%02x.%01x",
122 PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
124 case DEVICE_PATH_PNP:
125 sprintf(buffer, "PNP: %04x.%01x",
126 dev->path.u.pnp.port, dev->path.u.pnp.device);
128 case DEVICE_PATH_I2C:
129 sprintf(buffer, "I2C: %02x",
130 dev->path.u.i2c.device);
132 case DEVICE_PATH_APIC:
133 sprintf(buffer, "APIC: %02x",
134 dev->path.u.apic.apic_id);
137 printk_err("Unknown device path type: %d\n", dev->path.type);
144 int path_eq(struct device_path *path1, struct device_path *path2)
147 if (path1->type == path2->type) {
148 switch(path1->type) {
149 case DEVICE_PATH_NONE:
151 case DEVICE_PATH_ROOT:
154 case DEVICE_PATH_DEFAULT_CPU:
157 case DEVICE_PATH_PCI:
158 equal = (path1->u.pci.bus == path2->u.pci.bus) &&
159 (path1->u.pci.devfn == path2->u.pci.devfn);
161 case DEVICE_PATH_PNP:
162 equal = (path1->u.pnp.port == path2->u.pnp.port) &&
163 (path1->u.pnp.device == path2->u.pnp.device);
165 case DEVICE_PATH_I2C:
166 equal = (path1->u.i2c.device == path2->u.i2c.device);
168 case DEVICE_PATH_APIC:
169 equal = (path1->u.apic.apic_id == path2->u.apic.apic_id);
172 printk_err("Uknown device type: %d\n", path1->type);
180 * See if we have unused but allocated resource structures.
181 * If so remove the allocation.
182 * @param dev The device to find the resource on
184 void compact_resources(device_t dev)
186 struct resource *resource;
188 /* Move all of the free resources to the end */
189 for(i = 0; i < dev->resources;) {
190 resource = &dev->resource[i];
191 if (!resource->flags) {
192 memmove(resource, resource + 1, dev->resources - i);
194 memset(&dev->resource[dev->resources], 0, sizeof(*resource));
203 * See if a resource structure already exists for a given index
204 * @param dev The device to find the resource on
205 * @param index The index of the resource on the device.
206 * @return the resource if it already exists
208 struct resource *probe_resource(device_t dev, unsigned index)
210 struct resource *resource;
212 /* See if there is a resource with the appropriate index */
214 for(i = 0; i < dev->resources; i++) {
215 if (dev->resource[i].index == index) {
216 resource = &dev->resource[i];
224 * See if a resource structure already exists for a given index and if
225 * not allocate one. Then initialize the initialize the resource
227 * @param dev The device to find the resource on
228 * @param index The index of the resource on the device.
230 struct resource *new_resource(device_t dev, unsigned index)
232 struct resource *resource;
234 /* First move all of the free resources to the end */
235 compact_resources(dev);
237 /* See if there is a resource with the appropriate index */
238 resource = probe_resource(dev, index);
240 if (dev->resources == MAX_RESOURCES) {
241 die("MAX_RESOURCES exceeded.");
243 resource = &dev->resource[dev->resources];
244 memset(resource, 0, sizeof(*resource));
247 /* Initialize the resource values */
248 if (!(resource->flags & IORESOURCE_FIXED)) {
254 resource->index = index;
262 * Return an existing resource structure for a given index.
263 * @param dev The device to find the resource on
264 * @param index The index of the resource on the device.
266 struct resource *find_resource(device_t dev, unsigned index)
268 struct resource *resource;
270 /* See if there is a resource with the appropriate index */
271 resource = probe_resource(dev, index);
273 printk_emerg("%s missing resource: %02x\n",
274 dev_path(dev), index);
282 * @brief round a number up to the next multiple of gran
283 * @param val the starting value
284 * @param gran granularity we are aligning the number to.
285 * @returns aligned value
287 static resource_t align_up(resource_t val, unsigned long gran)
290 mask = (1ULL << gran) - 1ULL;
297 * @brief round a number up to the previous multiple of gran
298 * @param val the starting value
299 * @param gran granularity we are aligning the number to.
300 * @returns aligned value
302 static resource_t align_down(resource_t val, unsigned long gran)
305 mask = (1ULL << gran) - 1ULL;
311 * @brief Compute the maximum address that is part of a resource
312 * @param resource the resource whose limit is desired
315 resource_t resource_end(struct resource *resource)
317 resource_t base, end;
318 /* get the base address */
319 base = resource->base;
321 /* For a non bridge resource granularity and alignment are the same.
322 * For a bridge resource align is the largest needed alignment below
323 * the bridge. While the granularity is simply how many low bits of the
324 * address cannot be set.
327 /* Get the end (rounded up) */
328 end = base + align_up(resource->size, resource->gran) - 1;
334 * @brief Compute the maximum legal value for resource->base
335 * @param resource the resource whose maximum is desired
336 * @returns the maximum
338 resource_t resource_max(struct resource *resource)
342 max = align_down(resource->limit - resource->size + 1, resource->align);
348 * @brief print the resource that was just stored.
349 * @param dev the device the stored resorce lives on
350 * @param resource the resource that was just stored.
352 void report_resource_stored(device_t dev, struct resource *resource, const char *comment)
354 if (resource->flags & IORESOURCE_STORED) {
355 unsigned char buf[10];
356 unsigned long long base, end;
357 base = resource->base;
358 end = resource_end(resource);
360 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
361 sprintf(buf, "bus %d ", dev->link[0].secondary);
364 "%s %02x <- [0x%010Lx - 0x%010Lx] %s%s%s%s\n",
369 (resource->flags & IORESOURCE_PREFETCH) ? "pref" : "",
370 (resource->flags & IORESOURCE_IO)? "io":
371 (resource->flags & IORESOURCE_DRQ)? "drq":
372 (resource->flags & IORESOURCE_IRQ)? "irq":
373 (resource->flags & IORESOURCE_MEM)? "mem":