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 * @brief Given a PCI bus and a devfn number, find the device structure
47 * @param bus The bus number
48 * @param devfn a device/function number
49 * @return pointer to the device structure
51 struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
53 struct device *dev, *result;
56 for (dev = all_devices; dev; dev = dev->next) {
57 if ((dev->path.type == DEVICE_PATH_PCI) &&
58 (dev->bus->secondary == bus) &&
59 (dev->path.u.pci.devfn == devfn)) {
68 * @brief Given a smbus bus and a device number, find the device structure
70 * @param bus The bus number
71 * @param addr a device number
72 * @return pointer to the device structure
74 struct device *dev_find_slot_on_smbus(unsigned int bus, unsigned int addr)
76 struct device *dev, *result;
79 for (dev = all_devices; dev; dev = dev->next) {
80 if ((dev->path.type == DEVICE_PATH_I2C) &&
81 (dev->bus->secondary == bus) &&
82 (dev->path.u.i2c.device == addr)) {
90 /** Find a device of a given vendor and type
91 * @param vendor Vendor ID (e.g. 0x8086 for Intel)
92 * @param device Device ID
93 * @param from Pointer to the device structure, used as a starting point
94 * in the linked list of all_devices, which can be 0 to start at the
95 * head of the list (i.e. all_devices)
96 * @return Pointer to the device struct
98 struct device *dev_find_device(unsigned int vendor, unsigned int device, struct device *from)
104 while (from && (from->vendor != vendor || from->device != device)) {
110 /** Find a device of a given class
111 * @param class Class of the device
112 * @param from Pointer to the device structure, used as a starting point
113 * in the linked list of all_devices, which can be 0 to start at the
114 * head of the list (i.e. all_devices)
115 * @return Pointer to the device struct
117 struct device *dev_find_class(unsigned int class, struct device *from)
123 while (from && (from->class & 0xffffff00) != class)
129 const char *dev_path(device_t dev)
131 static char buffer[DEVICE_PATH_MAX];
134 memcpy(buffer, "<null>", 7);
137 switch(dev->path.type) {
138 case DEVICE_PATH_ROOT:
139 memcpy(buffer, "Root Device", 12);
141 case DEVICE_PATH_PCI:
142 sprintf(buffer, "PCI: %02x:%02x.%01x",
144 PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
146 case DEVICE_PATH_PNP:
147 sprintf(buffer, "PNP: %04x.%01x",
148 dev->path.u.pnp.port, dev->path.u.pnp.device);
150 case DEVICE_PATH_I2C:
151 sprintf(buffer, "I2C: %02x:%02x",
153 dev->path.u.i2c.device);
155 case DEVICE_PATH_APIC:
156 sprintf(buffer, "APIC: %02x",
157 dev->path.u.apic.apic_id);
159 case DEVICE_PATH_PCI_DOMAIN:
160 sprintf(buffer, "PCI_DOMAIN: %04x",
161 dev->path.u.pci_domain.domain);
163 case DEVICE_PATH_APIC_CLUSTER:
164 sprintf(buffer, "APIC_CLUSTER: %01x",
165 dev->path.u.apic_cluster.cluster);
167 case DEVICE_PATH_CPU:
168 sprintf(buffer, "CPU: %02x", dev->path.u.cpu.id);
170 case DEVICE_PATH_CPU_BUS:
171 sprintf(buffer, "CPU_BUS: %02x", dev->path.u.cpu_bus.id);
174 printk_err("Unknown device path type: %d\n", dev->path.type);
181 const char *bus_path(struct bus *bus)
183 static char buffer[BUS_PATH_MAX];
184 sprintf(buffer, "%s,%d",
185 dev_path(bus->dev), bus->link);
189 int path_eq(struct device_path *path1, struct device_path *path2)
192 if (path1->type == path2->type) {
193 switch(path1->type) {
194 case DEVICE_PATH_NONE:
196 case DEVICE_PATH_ROOT:
199 case DEVICE_PATH_PCI:
200 equal = (path1->u.pci.devfn == path2->u.pci.devfn);
202 case DEVICE_PATH_PNP:
203 equal = (path1->u.pnp.port == path2->u.pnp.port) &&
204 (path1->u.pnp.device == path2->u.pnp.device);
206 case DEVICE_PATH_I2C:
207 equal = (path1->u.i2c.device == path2->u.i2c.device);
209 case DEVICE_PATH_APIC:
210 equal = (path1->u.apic.apic_id == path2->u.apic.apic_id);
212 case DEVICE_PATH_PCI_DOMAIN:
213 equal = (path1->u.pci_domain.domain == path2->u.pci_domain.domain);
215 case DEVICE_PATH_APIC_CLUSTER:
216 equal = (path1->u.apic_cluster.cluster == path2->u.apic_cluster.cluster);
218 case DEVICE_PATH_CPU:
219 equal = (path1->u.cpu.id == path2->u.cpu.id);
221 case DEVICE_PATH_CPU_BUS:
222 equal = (path1->u.cpu_bus.id == path2->u.cpu_bus.id);
225 printk_err("Uknown device type: %d\n", path1->type);
233 * See if we have unused but allocated resource structures.
234 * If so remove the allocation.
235 * @param dev The device to find the resource on
237 void compact_resources(device_t dev)
239 struct resource *resource;
241 /* Move all of the free resources to the end */
242 for(i = 0; i < dev->resources;) {
243 resource = &dev->resource[i];
244 if (!resource->flags) {
245 memmove(resource, resource + 1, dev->resources - i);
247 memset(&dev->resource[dev->resources], 0, sizeof(*resource));
256 * See if a resource structure already exists for a given index
257 * @param dev The device to find the resource on
258 * @param index The index of the resource on the device.
259 * @return the resource if it already exists
261 struct resource *probe_resource(device_t dev, unsigned index)
263 struct resource *resource;
265 /* See if there is a resource with the appropriate index */
267 for(i = 0; i < dev->resources; i++) {
268 if (dev->resource[i].index == index) {
269 resource = &dev->resource[i];
277 * See if a resource structure already exists for a given index and if
278 * not allocate one. Then initialize the initialize the resource
280 * @param dev The device to find the resource on
281 * @param index The index of the resource on the device.
283 struct resource *new_resource(device_t dev, unsigned index)
285 struct resource *resource;
287 /* First move all of the free resources to the end */
288 compact_resources(dev);
290 /* See if there is a resource with the appropriate index */
291 resource = probe_resource(dev, index);
293 if (dev->resources == MAX_RESOURCES) {
294 die("MAX_RESOURCES exceeded.");
296 resource = &dev->resource[dev->resources];
297 memset(resource, 0, sizeof(*resource));
300 /* Initialize the resource values */
301 if (!(resource->flags & IORESOURCE_FIXED)) {
307 resource->index = index;
315 * Return an existing resource structure for a given index.
316 * @param dev The device to find the resource on
317 * @param index The index of the resource on the device.
319 struct resource *find_resource(device_t dev, unsigned index)
321 struct resource *resource;
323 /* See if there is a resource with the appropriate index */
324 resource = probe_resource(dev, index);
326 printk_emerg("%s missing resource: %02x\n",
327 dev_path(dev), index);
335 * @brief round a number up to the next multiple of gran
336 * @param val the starting value
337 * @param gran granularity we are aligning the number to.
338 * @returns aligned value
340 static resource_t align_up(resource_t val, unsigned long gran)
343 mask = (1ULL << gran) - 1ULL;
350 * @brief round a number up to the previous multiple of gran
351 * @param val the starting value
352 * @param gran granularity we are aligning the number to.
353 * @returns aligned value
355 static resource_t align_down(resource_t val, unsigned long gran)
358 mask = (1ULL << gran) - 1ULL;
364 * @brief Compute the maximum address that is part of a resource
365 * @param resource the resource whose limit is desired
368 resource_t resource_end(struct resource *resource)
370 resource_t base, end;
371 /* get the base address */
372 base = resource->base;
374 /* For a non bridge resource granularity and alignment are the same.
375 * For a bridge resource align is the largest needed alignment below
376 * the bridge. While the granularity is simply how many low bits of the
377 * address cannot be set.
380 /* Get the end (rounded up) */
381 end = base + align_up(resource->size, resource->gran) - 1;
387 * @brief Compute the maximum legal value for resource->base
388 * @param resource the resource whose maximum is desired
389 * @returns the maximum
391 resource_t resource_max(struct resource *resource)
395 max = align_down(resource->limit - resource->size + 1, resource->align);
401 * @brief return the resource type of a resource
402 * @param resource the resource type to decode.
404 const char *resource_type(struct resource *resource)
406 static char buffer[RESOURCE_TYPE_MAX];
407 sprintf(buffer, "%s%s%s%s",
408 ((resource->flags & IORESOURCE_READONLY)? "ro": ""),
409 ((resource->flags & IORESOURCE_PREFETCH)? "pref":""),
410 ((resource->flags == 0)? "unused":
411 (resource->flags & IORESOURCE_IO)? "io":
412 (resource->flags & IORESOURCE_DRQ)? "drq":
413 (resource->flags & IORESOURCE_IRQ)? "irq":
414 (resource->flags & IORESOURCE_MEM)? "mem":"??????"),
415 ((resource->flags & IORESOURCE_PCI64)?"64":""));
420 * @brief print the resource that was just stored.
421 * @param dev the device the stored resorce lives on
422 * @param resource the resource that was just stored.
424 void report_resource_stored(device_t dev, struct resource *resource, const char *comment)
426 if (resource->flags & IORESOURCE_STORED) {
427 unsigned char buf[10];
428 unsigned long long base, end;
429 base = resource->base;
430 end = resource_end(resource);
432 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
433 sprintf(buf, "bus %d ", dev->link[0].secondary);
436 "%s %02x <- [0x%010Lx - 0x%010Lx] %s%s%s\n",
441 resource_type(resource),
446 void search_bus_resources(struct bus *bus,
447 unsigned long type_mask, unsigned long type,
448 resource_search_t search, void *gp)
450 struct device *curdev;
451 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
453 /* Ignore disabled devices */
454 if (!curdev->have_resources) continue;
455 for(i = 0; i < curdev->resources; i++) {
456 struct resource *resource = &curdev->resource[i];
457 /* If it isn't the right kind of resource ignore it */
458 if ((resource->flags & type_mask) != type) {
461 /* If it is a subtractive resource recurse */
462 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
464 subbus = &curdev->link[IOINDEX_SUBTRACTIVE_LINK(resource->index)];
465 search_bus_resources(subbus, type_mask, type, search, gp);
468 search(gp, curdev, resource);
473 void search_global_resources(
474 unsigned long type_mask, unsigned long type,
475 resource_search_t search, void *gp)
477 struct device *curdev;
478 for(curdev = all_devices; curdev; curdev = curdev->next) {
480 /* Ignore disabled devices */
481 if (!curdev->have_resources) continue;
482 for(i = 0; i < curdev->resources; i++) {
483 struct resource *resource = &curdev->resource[i];
484 /* If it isn't the right kind of resource ignore it */
485 if ((resource->flags & type_mask) != type) {
488 /* If it is a subtractive resource ignore it */
489 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
492 search(gp, curdev, resource);
497 void dev_set_enabled(device_t dev, int enable)
499 if (dev->enabled == enable) {
502 dev->enabled = enable;
503 if (dev->ops && dev->ops->enable) {
504 dev->ops->enable(dev);
506 else if (dev->chip_ops && dev->chip_ops->enable_dev) {
507 dev->chip_ops->enable_dev(dev);
511 void disable_children(struct bus *bus)
514 for(child = bus->children; child; child = child->sibling) {
516 for(link = 0; link < child->links; link++) {
517 disable_children(&child->link[link]);
519 dev_set_enabled(child, 0);