1 #include <console/console.h>
2 #include <device/device.h>
3 #include <device/path.h>
4 #include <device/pci.h>
5 #include <device/resource.h>
9 * @brief See if a device structure exists for path
11 * @param bus The bus to find the device on
12 * @param path The relative path from the bus to the appropriate device
13 * @return pointer to a device structure for the device on bus at path
14 * or 0/NULL if no device is found
16 device_t find_dev_path(struct bus *parent, struct device_path *path)
19 for(child = parent->children; child; child = child->sibling) {
20 if (path_eq(path, &child->path)) {
28 * @brief See if a device structure already exists and if not allocate it
30 * @param bus The bus to find the device on
31 * @param path The relative path from the bus to the appropriate device
32 * @return pointer to a device structure for the device on bus at path
34 device_t alloc_find_dev(struct bus *parent, struct device_path *path)
37 child = find_dev_path(parent, path);
39 child = alloc_dev(parent, path);
45 * Given a bus and a devfn number, find the device structure
46 * @param bus The bus number
47 * @param devfn a device/function number
48 * @return pointer to the device structure
50 struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
52 struct device *dev, *result;
55 for (dev = all_devices; dev; dev = dev->next) {
56 if ((dev->path.type == DEVICE_PATH_PCI) &&
57 (dev->bus->secondary == bus) &&
58 (dev->path.u.pci.devfn == devfn)) {
66 /** Find a device of a given vendor and type
67 * @param vendor Vendor ID (e.g. 0x8086 for Intel)
68 * @param device Device ID
69 * @param from Pointer to the device structure, used as a starting point
70 * in the linked list of all_devices, which can be 0 to start at the
71 * head of the list (i.e. all_devices)
72 * @return Pointer to the device struct
74 struct device *dev_find_device(unsigned int vendor, unsigned int device, struct device *from)
80 while (from && (from->vendor != vendor || from->device != device)) {
86 /** Find a device of a given class
87 * @param class Class of the device
88 * @param from Pointer to the device structure, used as a starting point
89 * in the linked list of all_devices, which can be 0 to start at the
90 * head of the list (i.e. all_devices)
91 * @return Pointer to the device struct
93 struct device *dev_find_class(unsigned int class, struct device *from)
99 while (from && (from->class & 0xffffff00) != class)
105 const char *dev_path(device_t dev)
107 static char buffer[DEVICE_PATH_MAX];
110 memcpy(buffer, "<null>", 7);
113 switch(dev->path.type) {
114 case DEVICE_PATH_ROOT:
115 memcpy(buffer, "Root Device", 12);
117 case DEVICE_PATH_PCI:
118 sprintf(buffer, "PCI: %02x:%02x.%01x",
120 PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
122 case DEVICE_PATH_PNP:
123 sprintf(buffer, "PNP: %04x.%01x",
124 dev->path.u.pnp.port, dev->path.u.pnp.device);
126 case DEVICE_PATH_I2C:
127 sprintf(buffer, "I2C: %02x",
128 dev->path.u.i2c.device);
130 case DEVICE_PATH_APIC:
131 sprintf(buffer, "APIC: %02x",
132 dev->path.u.apic.apic_id);
134 case DEVICE_PATH_PCI_DOMAIN:
135 sprintf(buffer, "PCI_DOMAIN: %04x",
136 dev->path.u.pci_domain.domain);
138 case DEVICE_PATH_APIC_CLUSTER:
139 sprintf(buffer, "APIC_CLUSTER: %01x",
140 dev->path.u.apic_cluster.cluster);
142 case DEVICE_PATH_CPU:
143 sprintf(buffer, "CPU: %02x", dev->path.u.cpu.id);
145 case DEVICE_PATH_CPU_BUS:
146 sprintf(buffer, "CPU_BUS: %02x", dev->path.u.cpu_bus.id);
149 printk_err("Unknown device path type: %d\n", dev->path.type);
156 int path_eq(struct device_path *path1, struct device_path *path2)
159 if (path1->type == path2->type) {
160 switch(path1->type) {
161 case DEVICE_PATH_NONE:
163 case DEVICE_PATH_ROOT:
166 case DEVICE_PATH_PCI:
167 equal = (path1->u.pci.devfn == path2->u.pci.devfn);
169 case DEVICE_PATH_PNP:
170 equal = (path1->u.pnp.port == path2->u.pnp.port) &&
171 (path1->u.pnp.device == path2->u.pnp.device);
173 case DEVICE_PATH_I2C:
174 equal = (path1->u.i2c.device == path2->u.i2c.device);
176 case DEVICE_PATH_APIC:
177 equal = (path1->u.apic.apic_id == path2->u.apic.apic_id);
179 case DEVICE_PATH_PCI_DOMAIN:
180 equal = (path1->u.pci_domain.domain == path2->u.pci_domain.domain);
182 case DEVICE_PATH_APIC_CLUSTER:
183 equal = (path1->u.apic_cluster.cluster == path2->u.apic_cluster.cluster);
185 case DEVICE_PATH_CPU:
186 equal = (path1->u.cpu.id == path2->u.cpu.id);
188 case DEVICE_PATH_CPU_BUS:
189 equal = (path1->u.cpu_bus.id == path2->u.cpu_bus.id);
192 printk_err("Uknown device type: %d\n", path1->type);
200 * See if we have unused but allocated resource structures.
201 * If so remove the allocation.
202 * @param dev The device to find the resource on
204 void compact_resources(device_t dev)
206 struct resource *resource;
208 /* Move all of the free resources to the end */
209 for(i = 0; i < dev->resources;) {
210 resource = &dev->resource[i];
211 if (!resource->flags) {
212 memmove(resource, resource + 1, dev->resources - i);
214 memset(&dev->resource[dev->resources], 0, sizeof(*resource));
223 * See if a resource structure already exists for a given index
224 * @param dev The device to find the resource on
225 * @param index The index of the resource on the device.
226 * @return the resource if it already exists
228 struct resource *probe_resource(device_t dev, unsigned index)
230 struct resource *resource;
232 /* See if there is a resource with the appropriate index */
234 for(i = 0; i < dev->resources; i++) {
235 if (dev->resource[i].index == index) {
236 resource = &dev->resource[i];
244 * See if a resource structure already exists for a given index and if
245 * not allocate one. Then initialize the initialize the resource
247 * @param dev The device to find the resource on
248 * @param index The index of the resource on the device.
250 struct resource *new_resource(device_t dev, unsigned index)
252 struct resource *resource;
254 /* First move all of the free resources to the end */
255 compact_resources(dev);
257 /* See if there is a resource with the appropriate index */
258 resource = probe_resource(dev, index);
260 if (dev->resources == MAX_RESOURCES) {
261 die("MAX_RESOURCES exceeded.");
263 resource = &dev->resource[dev->resources];
264 memset(resource, 0, sizeof(*resource));
267 /* Initialize the resource values */
268 if (!(resource->flags & IORESOURCE_FIXED)) {
274 resource->index = index;
282 * Return an existing resource structure for a given index.
283 * @param dev The device to find the resource on
284 * @param index The index of the resource on the device.
286 struct resource *find_resource(device_t dev, unsigned index)
288 struct resource *resource;
290 /* See if there is a resource with the appropriate index */
291 resource = probe_resource(dev, index);
293 printk_emerg("%s missing resource: %02x\n",
294 dev_path(dev), index);
302 * @brief round a number up to the next multiple of gran
303 * @param val the starting value
304 * @param gran granularity we are aligning the number to.
305 * @returns aligned value
307 static resource_t align_up(resource_t val, unsigned long gran)
310 mask = (1ULL << gran) - 1ULL;
317 * @brief round a number up to the previous multiple of gran
318 * @param val the starting value
319 * @param gran granularity we are aligning the number to.
320 * @returns aligned value
322 static resource_t align_down(resource_t val, unsigned long gran)
325 mask = (1ULL << gran) - 1ULL;
331 * @brief Compute the maximum address that is part of a resource
332 * @param resource the resource whose limit is desired
335 resource_t resource_end(struct resource *resource)
337 resource_t base, end;
338 /* get the base address */
339 base = resource->base;
341 /* For a non bridge resource granularity and alignment are the same.
342 * For a bridge resource align is the largest needed alignment below
343 * the bridge. While the granularity is simply how many low bits of the
344 * address cannot be set.
347 /* Get the end (rounded up) */
348 end = base + align_up(resource->size, resource->gran) - 1;
354 * @brief Compute the maximum legal value for resource->base
355 * @param resource the resource whose maximum is desired
356 * @returns the maximum
358 resource_t resource_max(struct resource *resource)
362 max = align_down(resource->limit - resource->size + 1, resource->align);
368 * @brief print the resource that was just stored.
369 * @param dev the device the stored resorce lives on
370 * @param resource the resource that was just stored.
372 void report_resource_stored(device_t dev, struct resource *resource, const char *comment)
374 if (resource->flags & IORESOURCE_STORED) {
375 unsigned char buf[10];
376 unsigned long long base, end;
377 base = resource->base;
378 end = resource_end(resource);
380 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
381 sprintf(buf, "bus %d ", dev->link[0].secondary);
384 "%s %02x <- [0x%010Lx - 0x%010Lx] %s%s%s%s\n",
389 (resource->flags & IORESOURCE_PREFETCH) ? "pref" : "",
390 (resource->flags & IORESOURCE_IO)? "io":
391 (resource->flags & IORESOURCE_DRQ)? "drq":
392 (resource->flags & IORESOURCE_IRQ)? "irq":
393 (resource->flags & IORESOURCE_MEM)? "mem":
399 void search_bus_resources(struct bus *bus,
400 unsigned long type_mask, unsigned long type,
401 resource_search_t search, void *gp)
403 struct device *curdev;
404 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
406 /* Ignore disabled devices */
407 if (!curdev->have_resources) continue;
408 for(i = 0; i < curdev->resources; i++) {
409 struct resource *resource = &curdev->resource[i];
410 /* If it isn't the right kind of resource ignore it */
411 if ((resource->flags & type_mask) != type) {
414 /* If it is a subtractive resource recurse */
415 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
417 subbus = &curdev->link[IOINDEX_SUBTRACTIVE_LINK(resource->index)];
418 search_bus_resources(subbus, type_mask, type, search, gp);
421 search(gp, curdev, resource);
426 void search_global_resources(
427 unsigned long type_mask, unsigned long type,
428 resource_search_t search, void *gp)
430 struct device *curdev;
431 for(curdev = all_devices; curdev; curdev = curdev->next) {
433 /* Ignore disabled devices */
434 if (!curdev->have_resources) continue;
435 for(i = 0; i < curdev->resources; i++) {
436 struct resource *resource = &curdev->resource[i];
437 /* If it isn't the right kind of resource ignore it */
438 if ((resource->flags & type_mask) != type) {
441 /* If it is a subtractive resource ignore it */
442 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
445 search(gp, curdev, resource);