coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
device_util.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <console/console.h>
4 #include <device/device.h>
5 #include <device/path.h>
6 #include <device/pci_def.h>
7 #include <device/resource.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 /**
12  * Given a Local APIC ID, find the device structure.
13  *
14  * @param apic_id The Local APIC ID number.
15  * @return Pointer to the device structure (if found), 0 otherwise.
16  */
17 struct device *dev_find_lapic(unsigned int apic_id)
18 {
19  struct device *dev;
20  struct device *result = NULL;
21 
22  for (dev = all_devices; dev; dev = dev->next) {
23  if (dev->path.type == DEVICE_PATH_APIC &&
24  dev->path.apic.apic_id == apic_id) {
25  result = dev;
26  break;
27  }
28  }
29  return result;
30 }
31 
32 /**
33  * Find a device of a given vendor and type.
34  *
35  * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
36  * @param device A PCI device ID.
37  * @param from Pointer to the device structure, used as a starting point in
38  * the linked list of all_devices, which can be 0 to start at the
39  * head of the list (i.e. all_devices).
40  * @return Pointer to the device struct.
41  */
42 struct device *dev_find_device(u16 vendor, u16 device, struct device *from)
43 {
44  if (!from)
45  from = all_devices;
46  else
47  from = from->next;
48 
49  while (from && (from->vendor != vendor || from->device != device))
50  from = from->next;
51 
52  return from;
53 }
54 
55 /**
56  * Find a device of a given class.
57  *
58  * @param class Class of the device.
59  * @param from Pointer to the device structure, used as a starting point in
60  * the linked list of all_devices, which can be 0 to start at the
61  * head of the list (i.e. all_devices).
62  * @return Pointer to the device struct.
63  */
64 struct device *dev_find_class(unsigned int class, struct device *from)
65 {
66  if (!from)
67  from = all_devices;
68  else
69  from = from->next;
70 
71  while (from && (from->class & 0xffffff00) != class)
72  from = from->next;
73 
74  return from;
75 }
76 
77 /**
78  * Encode the device path into 3 bytes for logging to CMOS.
79  *
80  * @param dev The device path to encode.
81  * @return Device path encoded into lower 3 bytes of dword.
82  */
83 u32 dev_path_encode(const struct device *dev)
84 {
85  u32 ret;
86 
87  if (!dev)
88  return 0;
89 
90  /* Store the device type in 3rd byte. */
91  ret = dev->path.type << 16;
92 
93  /* Encode the device specific path in the low word. */
94  switch (dev->path.type) {
95  case DEVICE_PATH_ROOT:
96  break;
97  case DEVICE_PATH_PCI:
98  ret |= dev->bus->secondary << 8 | dev->path.pci.devfn;
99  break;
100  case DEVICE_PATH_PNP:
101  ret |= dev->path.pnp.port << 8 | dev->path.pnp.device;
102  break;
103  case DEVICE_PATH_I2C:
104  ret |= dev->path.i2c.mode_10bit << 8 | dev->path.i2c.device;
105  break;
106  case DEVICE_PATH_APIC:
107  ret |= dev->path.apic.apic_id;
108  break;
109  case DEVICE_PATH_DOMAIN:
110  ret |= dev->path.domain.domain;
111  break;
113  ret |= dev->path.cpu_cluster.cluster;
114  break;
115  case DEVICE_PATH_CPU:
116  ret |= dev->path.cpu.id;
117  break;
118  case DEVICE_PATH_CPU_BUS:
119  ret |= dev->path.cpu_bus.id;
120  break;
121  case DEVICE_PATH_IOAPIC:
122  ret |= dev->path.ioapic.ioapic_id;
123  break;
124  case DEVICE_PATH_GENERIC:
125  ret |= dev->path.generic.subid << 8 | dev->path.generic.id;
126  break;
127  case DEVICE_PATH_SPI:
128  ret |= dev->path.spi.cs;
129  break;
130  case DEVICE_PATH_USB:
131  ret |= dev->path.usb.port_type << 8 | dev->path.usb.port_id;
132  break;
133  case DEVICE_PATH_GPIO:
134  ret |= dev->path.gpio.id;
135  break;
136  case DEVICE_PATH_NONE:
137  case DEVICE_PATH_MMIO: /* don't care */
138  default:
139  break;
140  }
141 
142  return ret;
143 }
144 
145 /*
146  * Warning: This function uses a static buffer. Don't call it more than once
147  * from the same print statement!
148  */
149 const char *dev_path(const struct device *dev)
150 {
151  static char buffer[DEVICE_PATH_MAX];
152 
153  buffer[0] = '\0';
154  if (!dev) {
155  strcpy(buffer, "<null>");
156  } else {
157  switch (dev->path.type) {
158  case DEVICE_PATH_NONE:
159  strcpy(buffer, "NONE");
160  break;
161  case DEVICE_PATH_ROOT:
162  strcpy(buffer, "Root Device");
163  break;
164  case DEVICE_PATH_PCI:
165  snprintf(buffer, sizeof(buffer),
166  "PCI: %02x:%02x.%01x",
167  dev->bus->secondary,
168  PCI_SLOT(dev->path.pci.devfn),
169  PCI_FUNC(dev->path.pci.devfn));
170  break;
171  case DEVICE_PATH_PNP:
172  snprintf(buffer, sizeof(buffer), "PNP: %04x.%01x",
173  dev->path.pnp.port, dev->path.pnp.device);
174  break;
175  case DEVICE_PATH_I2C:
176  snprintf(buffer, sizeof(buffer), "I2C: %02x:%02x",
177  dev->bus->secondary,
178  dev->path.i2c.device);
179  break;
180  case DEVICE_PATH_APIC:
181  snprintf(buffer, sizeof(buffer), "APIC: %02x",
182  dev->path.apic.apic_id);
183  break;
184  case DEVICE_PATH_IOAPIC:
185  snprintf(buffer, sizeof(buffer), "IOAPIC: %02x",
186  dev->path.ioapic.ioapic_id);
187  break;
188  case DEVICE_PATH_DOMAIN:
189  snprintf(buffer, sizeof(buffer), "DOMAIN: %04x",
190  dev->path.domain.domain);
191  break;
193  snprintf(buffer, sizeof(buffer), "CPU_CLUSTER: %01x",
194  dev->path.cpu_cluster.cluster);
195  break;
196  case DEVICE_PATH_CPU:
197  snprintf(buffer, sizeof(buffer),
198  "CPU: %02x", dev->path.cpu.id);
199  break;
200  case DEVICE_PATH_CPU_BUS:
201  snprintf(buffer, sizeof(buffer),
202  "CPU_BUS: %02x", dev->path.cpu_bus.id);
203  break;
204  case DEVICE_PATH_GENERIC:
205  snprintf(buffer, sizeof(buffer),
206  "GENERIC: %d.%d", dev->path.generic.id,
207  dev->path.generic.subid);
208  break;
209  case DEVICE_PATH_SPI:
210  snprintf(buffer, sizeof(buffer), "SPI: %02x",
211  dev->path.spi.cs);
212  break;
213  case DEVICE_PATH_USB:
214  snprintf(buffer, sizeof(buffer), "USB%u port %u",
215  dev->path.usb.port_type, dev->path.usb.port_id);
216  break;
217  case DEVICE_PATH_MMIO:
218  snprintf(buffer, sizeof(buffer), "MMIO: %08lx",
219  dev->path.mmio.addr);
220  break;
221  case DEVICE_PATH_GPIO:
222  snprintf(buffer, sizeof(buffer), "GPIO: %d", dev->path.gpio.id);
223  break;
224  default:
225  printk(BIOS_ERR, "Unknown device path type: %d\n",
226  dev->path.type);
227  break;
228  }
229  }
230  return buffer;
231 }
232 
233 const char *dev_name(const struct device *dev)
234 {
235  if (dev->name)
236  return dev->name;
237  else if (dev->chip_ops && dev->chip_ops->name)
238  return dev->chip_ops->name;
239  else
240  return "unknown";
241 }
242 
243 const char *bus_path(struct bus *bus)
244 {
245  static char buffer[BUS_PATH_MAX];
246  snprintf(buffer, sizeof(buffer),
247  "%s,%d", dev_path(bus->dev), bus->link_num);
248  return buffer;
249 }
250 
251 /**
252  * Allocate 64 more resources to the free list.
253  *
254  * @return TODO.
255  */
256 static int allocate_more_resources(void)
257 {
258  int i;
259  struct resource *new_res_list;
260 
261  new_res_list = malloc(64 * sizeof(*new_res_list));
262 
263  if (new_res_list == NULL)
264  return 0;
265 
266  memset(new_res_list, 0, 64 * sizeof(*new_res_list));
267 
268  for (i = 0; i < 64 - 1; i++)
269  new_res_list[i].next = &new_res_list[i+1];
270 
271  free_resources = new_res_list;
272  return 1;
273 }
274 
275 /**
276  * Remove resource res from the device's list and add it to the free list.
277  *
278  * @param dev TODO
279  * @param res TODO
280  * @param prev TODO
281  * @return TODO.
282  */
283 static void free_resource(struct device *dev, struct resource *res,
284  struct resource *prev)
285 {
286  if (prev)
287  prev->next = res->next;
288  else
289  dev->resource_list = res->next;
290 
291  res->next = free_resources;
292  free_resources = res;
293 }
294 
295 /**
296  * See if we have unused but allocated resource structures.
297  *
298  * If so remove the allocation.
299  *
300  * @param dev The device to find the resource on.
301  */
302 void compact_resources(struct device *dev)
303 {
304  struct resource *res, *next, *prev = NULL;
305 
306  /* Move all of the free resources to the end */
307  for (res = dev->resource_list; res; res = next) {
308  next = res->next;
309  if (!res->flags)
310  free_resource(dev, res, prev);
311  else
312  prev = res;
313  }
314 }
315 
316 /**
317  * See if a resource structure already exists for a given index.
318  *
319  * @param dev The device to find the resource on.
320  * @param index The index of the resource on the device.
321  * @return The resource, if it already exists.
322  */
323 struct resource *probe_resource(const struct device *dev, unsigned int index)
324 {
325  struct resource *res;
326 
327  /* See if there is a resource with the appropriate index */
328  for (res = dev->resource_list; res; res = res->next) {
329  if (res->index == index)
330  break;
331  }
332 
333  return res;
334 }
335 
336 /**
337  * See if a resource structure already exists for a given index and if not
338  * allocate one.
339  *
340  * Then initialize the resource to default values.
341  *
342  * @param dev The device to find the resource on.
343  * @param index The index of the resource on the device.
344  * @return TODO.
345  */
346 struct resource *new_resource(struct device *dev, unsigned int index)
347 {
348  struct resource *resource, *tail;
349 
350  /* First move all of the free resources to the end. */
351  compact_resources(dev);
352 
353  /* See if there is a resource with the appropriate index. */
354  resource = probe_resource(dev, index);
355  if (!resource) {
357  die("Couldn't allocate more resources.");
358 
361  memset(resource, 0, sizeof(*resource));
362  resource->next = NULL;
363  tail = dev->resource_list;
364  if (tail) {
365  while (tail->next)
366  tail = tail->next;
367  tail->next = resource;
368  } else {
369  dev->resource_list = resource;
370  }
371  }
372 
373  /* Initialize the resource values. */
374  if (!(resource->flags & IORESOURCE_FIXED)) {
375  resource->flags = 0;
376  resource->base = 0;
377  }
378  resource->size = 0;
379  resource->limit = 0;
380  resource->index = index;
381  resource->align = 0;
382  resource->gran = 0;
383 
384  return resource;
385 }
386 
387 /**
388  * Return an existing resource structure for a given index.
389  *
390  * @param dev The device to find the resource on.
391  * @param index The index of the resource on the device.
392  * return TODO.
393  */
394 struct resource *find_resource(const struct device *dev, unsigned int index)
395 {
396  struct resource *resource;
397 
398  /* See if there is a resource with the appropriate index. */
399  resource = probe_resource(dev, index);
400  if (!resource) {
401  printk(BIOS_EMERG, "%s missing resource: %02x\n",
402  dev_path(dev), index);
403  die("");
404  }
405  return resource;
406 }
407 
408 /**
409  * Round a number up to the next multiple of gran.
410  *
411  * @param val The starting value.
412  * @param gran Granularity we are aligning the number to.
413  * @return The aligned value.
414  */
415 static resource_t align_up(resource_t val, unsigned long gran)
416 {
418  mask = (1ULL << gran) - 1ULL;
419  val += mask;
420  val &= ~mask;
421  return val;
422 }
423 
424 /**
425  * Round a number up to the previous multiple of gran.
426  *
427  * @param val The starting value.
428  * @param gran Granularity we are aligning the number to.
429  * @return The aligned value.
430  */
431 static resource_t align_down(resource_t val, unsigned long gran)
432 {
434  mask = (1ULL << gran) - 1ULL;
435  val &= ~mask;
436  return val;
437 }
438 
439 /**
440  * Compute the maximum address that is part of a resource.
441  *
442  * @param resource The resource whose limit is desired.
443  * @return The end.
444  */
446 {
447  resource_t base, end;
448 
449  /* Get the base address. */
450  base = resource->base;
451 
452  /*
453  * For a non bridge resource granularity and alignment are the same.
454  * For a bridge resource align is the largest needed alignment below
455  * the bridge. While the granularity is simply how many low bits of
456  * the address cannot be set.
457  */
458 
459  /* Get the end (rounded up). */
460  end = base + align_up(resource->size, resource->gran) - 1;
461 
462  return end;
463 }
464 
465 /**
466  * Compute the maximum legal value for resource->base.
467  *
468  * @param resource The resource whose maximum is desired.
469  * @return The maximum.
470  */
472 {
473  resource_t max;
474 
476 
477  return max;
478 }
479 
480 /**
481  * Return the resource type of a resource.
482  *
483  * @param resource The resource type to decode.
484  * @return TODO.
485  */
486 const char *resource_type(const struct resource *resource)
487 {
488  static char buffer[RESOURCE_TYPE_MAX];
489  snprintf(buffer, sizeof(buffer), "%s%s%s%s",
490  ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
491  ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
492  ((resource->flags == 0) ? "unused" :
493  (resource->flags & IORESOURCE_IO) ? "io" :
494  (resource->flags & IORESOURCE_DRQ) ? "drq" :
495  (resource->flags & IORESOURCE_IRQ) ? "irq" :
496  (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
497  ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
498  return buffer;
499 }
500 
501 /**
502  * Print the resource that was just stored.
503  *
504  * @param dev The device the stored resource lives on.
505  * @param resource The resource that was just stored.
506  * @param comment TODO
507  */
508 void report_resource_stored(struct device *dev, const struct resource *resource,
509  const char *comment)
510 {
511  char buf[10];
512  unsigned long long base, end;
513 
514  if (!(resource->flags & IORESOURCE_STORED))
515  return;
516 
517  base = resource->base;
518  end = resource_end(resource);
519  buf[0] = '\0';
520 
521  if (dev->link_list && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
522  snprintf(buf, sizeof(buf),
523  "bus %02x ", dev->link_list->secondary);
524  }
525  printk(BIOS_DEBUG, "%s %02lx <- [0x%010llx - 0x%010llx] size 0x%08llx "
526  "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
527  base, end, resource->size, resource->gran, buf,
528  resource_type(resource), comment);
529 }
530 
531 void search_bus_resources(struct bus *bus, unsigned long type_mask,
532  unsigned long type, resource_search_t search,
533  void *gp)
534 {
535  struct device *curdev;
536 
537  for (curdev = bus->children; curdev; curdev = curdev->sibling) {
538  struct resource *res;
539 
540  /* Ignore disabled devices. */
541  if (!curdev->enabled)
542  continue;
543 
544  for (res = curdev->resource_list; res; res = res->next) {
545  /* If it isn't the right kind of resource ignore it. */
546  if ((res->flags & type_mask) != type)
547  continue;
548 
549  /* If it is a subtractive resource recurse. */
550  if (res->flags & IORESOURCE_SUBTRACTIVE) {
551  struct bus *subbus;
552  for (subbus = curdev->link_list; subbus;
553  subbus = subbus->next)
554  if (subbus->link_num
556  break;
557  if (!subbus) /* Why can subbus be NULL? */
558  break;
559  search_bus_resources(subbus, type_mask, type,
560  search, gp);
561  continue;
562  }
563  search(gp, curdev, res);
564  }
565  }
566 }
567 
568 void search_global_resources(unsigned long type_mask, unsigned long type,
569  resource_search_t search, void *gp)
570 {
571  struct device *curdev;
572 
573  for (curdev = all_devices; curdev; curdev = curdev->next) {
574  struct resource *res;
575 
576  /* Ignore disabled devices. */
577  if (!curdev->enabled)
578  continue;
579 
580  for (res = curdev->resource_list; res; res = res->next) {
581  /* If it isn't the right kind of resource ignore it. */
582  if ((res->flags & type_mask) != type)
583  continue;
584 
585  /* If it is a subtractive resource ignore it. */
586  if (res->flags & IORESOURCE_SUBTRACTIVE)
587  continue;
588 
589  search(gp, curdev, res);
590  }
591  }
592 }
593 
594 void dev_set_enabled(struct device *dev, int enable)
595 {
596  if (dev->enabled == enable)
597  return;
598 
599  dev->enabled = enable;
600  if (dev->ops && dev->ops->enable)
601  dev->ops->enable(dev);
602  else if (dev->chip_ops && dev->chip_ops->enable_dev)
603  dev->chip_ops->enable_dev(dev);
604 }
605 
606 void disable_children(struct bus *bus)
607 {
608  struct device *child;
609 
610  for (child = bus->children; child; child = child->sibling) {
611  struct bus *link;
612  for (link = child->link_list; link; link = link->next)
613  disable_children(link);
614  dev_set_enabled(child, 0);
615  }
616 }
617 
618 /*
619  * Returns true if the device is an enabled bridge that has at least
620  * one enabled device on its secondary bus that is not of type NONE.
621  */
623 {
624  struct bus *link;
625  struct device *child;
626 
627  if (!dev || !dev->enabled)
628  return 0;
629 
630  if (!dev->link_list || !dev->link_list->children)
631  return 0;
632 
633  for (link = dev->link_list; link; link = link->next) {
634  for (child = link->children; child; child = child->sibling) {
635  if (child->path.type == DEVICE_PATH_NONE)
636  continue;
637 
638  if (child->enabled)
639  return 1;
640  }
641  }
642 
643  return 0;
644 }
645 
646 /**
647  * Ensure the device has a minimum number of bus links.
648  *
649  * @param dev The device to add links to.
650  * @param total_links The minimum number of links to have.
651  */
652 void add_more_links(struct device *dev, unsigned int total_links)
653 {
654  struct bus *link, *last = NULL;
655  int link_num = -1;
656 
657  for (link = dev->link_list; link; link = link->next) {
658  if (link_num < link->link_num)
659  link_num = link->link_num;
660  last = link;
661  }
662 
663  if (last) {
664  int links = total_links - (link_num + 1);
665  if (links > 0) {
666  link = malloc(links * sizeof(*link));
667  if (!link)
668  die("Couldn't allocate more links!\n");
669  memset(link, 0, links * sizeof(*link));
670  last->next = link;
671  } else {
672  /* No more links to add */
673  return;
674  }
675  } else {
676  link = malloc(total_links * sizeof(*link));
677  if (!link)
678  die("Couldn't allocate more links!\n");
679  memset(link, 0, total_links * sizeof(*link));
680  dev->link_list = link;
681  }
682 
683  for (link_num = link_num + 1; link_num < total_links; link_num++) {
684  link->link_num = link_num;
685  link->dev = dev;
686  link->next = link + 1;
687  last = link;
688  link = link->next;
689  }
690  last->next = NULL;
691 }
692 
693 static void resource_tree(const struct device *root, int debug_level, int depth)
694 {
695  int i = 0;
696  struct device *child;
697  struct bus *link;
698  struct resource *res;
699  char indent[30]; /* If your tree has more levels, it's wrong. */
700 
701  for (i = 0; i < depth + 1 && i < 29; i++)
702  indent[i] = ' ';
703  indent[i] = '\0';
704 
705  printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
706  if (root->link_list && root->link_list->children)
707  printk(BIOS_DEBUG, " child on link 0 %s",
708  dev_path(root->link_list->children));
709  printk(BIOS_DEBUG, "\n");
710 
711  for (res = root->resource_list; res; res = res->next) {
712  printk(debug_level, "%s%s resource base %llx size %llx "
713  "align %d gran %d limit %llx flags %lx index %lx\n",
714  indent, dev_path(root), res->base, res->size,
715  res->align, res->gran, res->limit, res->flags,
716  res->index);
717  }
718 
719  for (link = root->link_list; link; link = link->next) {
720  for (child = link->children; child; child = child->sibling)
721  resource_tree(child, debug_level, depth + 1);
722  }
723 }
724 
725 void print_resource_tree(const struct device *root, int debug_level,
726  const char *msg)
727 {
728  /* Bail if root is null. */
729  if (!root) {
730  printk(debug_level, "%s passed NULL for root!\n", __func__);
731  return;
732  }
733 
734  /* Bail if not printing to screen. */
735  if (!printk(debug_level, "Show resources in subtree (%s)...%s\n",
736  dev_path(root), msg))
737  return;
738 
739  resource_tree(root, debug_level, 0);
740 }
741 
742 void show_devs_tree(const struct device *dev, int debug_level, int depth)
743 {
744  char depth_str[20];
745  int i;
746  struct device *sibling;
747  struct bus *link;
748 
749  for (i = 0; i < depth; i++)
750  depth_str[i] = ' ';
751  depth_str[i] = '\0';
752 
753  printk(debug_level, "%s%s: enabled %d\n",
754  depth_str, dev_path(dev), dev->enabled);
755 
756  for (link = dev->link_list; link; link = link->next) {
757  for (sibling = link->children; sibling;
758  sibling = sibling->sibling)
759  show_devs_tree(sibling, debug_level, depth + 1);
760  }
761 }
762 
763 void show_all_devs_tree(int debug_level, const char *msg)
764 {
765  /* Bail if not printing to screen. */
766  if (!printk(debug_level, "Show all devs in tree form... %s\n", msg))
767  return;
768  show_devs_tree(all_devices, debug_level, 0);
769 }
770 
771 void show_devs_subtree(struct device *root, int debug_level, const char *msg)
772 {
773  /* Bail if not printing to screen. */
774  if (!printk(debug_level, "Show all devs in subtree %s... %s\n",
775  dev_path(root), msg))
776  return;
777  printk(debug_level, "%s\n", msg);
778  show_devs_tree(root, debug_level, 0);
779 }
780 
781 void show_all_devs(int debug_level, const char *msg)
782 {
783  struct device *dev;
784 
785  /* Bail if not printing to screen. */
786  if (!printk(debug_level, "Show all devs... %s\n", msg))
787  return;
788  for (dev = all_devices; dev; dev = dev->next) {
789  printk(debug_level, "%s: enabled %d\n",
790  dev_path(dev), dev->enabled);
791  }
792 }
793 
794 void show_one_resource(int debug_level, struct device *dev,
795  struct resource *resource, const char *comment)
796 {
797  char buf[10];
798  unsigned long long base, end;
799  base = resource->base;
800  end = resource_end(resource);
801  buf[0] = '\0';
802 
803  printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
804  "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
806  buf, resource_type(resource), comment);
807 }
808 
809 void show_all_devs_resources(int debug_level, const char *msg)
810 {
811  struct device *dev;
812 
813  if (!printk(debug_level, "Show all devs with resources... %s\n", msg))
814  return;
815 
816  for (dev = all_devices; dev; dev = dev->next) {
817  struct resource *res;
818  printk(debug_level, "%s: enabled %d\n",
819  dev_path(dev), dev->enabled);
820  for (res = dev->resource_list; res; res = res->next)
821  show_one_resource(debug_level, dev, res, "");
822  }
823 }
824 
825 void fixed_mem_resource(struct device *dev, unsigned long index,
826  unsigned long basek, unsigned long sizek,
827  unsigned long type)
828 {
829  struct resource *resource;
830 
831  if (!sizek)
832  return;
833 
834  resource = new_resource(dev, index);
835  resource->base = ((resource_t)basek) << 10;
836  resource->size = ((resource_t)sizek) << 10;
839 
840  resource->flags |= type;
841 }
842 
843 void fixed_io_resource(struct device *dev, unsigned long index,
844  unsigned long base, unsigned long size)
845 {
846  struct resource *resource;
847 
848  resource = new_resource(dev, index);
855 }
856 
857 void mmconf_resource(struct device *dev, unsigned long index)
858 {
859  struct resource *resource = new_resource(dev, index);
860  resource->base = CONFIG_ECAM_MMCONF_BASE_ADDRESS;
861  resource->size = CONFIG_ECAM_MMCONF_LENGTH;
864 
865  printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n",
866  (unsigned long)(resource->base),
867  (unsigned long)(resource->base + resource->size));
868 }
869 
870 void tolm_test(void *gp, struct device *dev, struct resource *new)
871 {
872  struct resource **best_p = gp;
873  struct resource *best;
874 
875  best = *best_p;
876 
877  /*
878  * If resource is not allocated any space i.e. size is zero,
879  * then do not consider this resource in tolm calculations.
880  */
881  if (new->size == 0)
882  return;
883 
884  if (!best || (best->base > new->base))
885  best = new;
886 
887  *best_p = best;
888 }
889 
891 {
892  struct resource *min = NULL;
893  u32 tolm;
894  unsigned long mask_match = IORESOURCE_MEM | IORESOURCE_ASSIGNED;
895 
896  search_bus_resources(bus, mask_match, mask_match, tolm_test, &min);
897 
898  tolm = 0xffffffffUL;
899 
900  if (min && tolm > min->base)
901  tolm = min->base;
902 
903  return tolm;
904 }
905 
906 /* Count of enabled CPUs */
907 int dev_count_cpu(void)
908 {
909  struct device *cpu;
910  int count = 0;
911 
912  for (cpu = all_devices; cpu; cpu = cpu->next) {
913  if ((cpu->path.type != DEVICE_PATH_APIC) ||
915  continue;
916  if (!cpu->enabled)
917  continue;
918  count++;
919  }
920 
921  return count;
922 }
923 
924 /* Get device path name */
926 {
927  static const char *const type_names[] = DEVICE_PATH_NAMES;
928  const char *type_name = "Unknown";
929 
930  /* Translate the type value into a string */
931  if (type < ARRAY_SIZE(type_names))
932  type_name = type_names[type];
933  return type_name;
934 }
int vendor
Definition: cpu.c:91
void * memset(void *dstpp, int c, size_t len)
Definition: memset.c:12
#define ARRAY_SIZE(a)
Definition: helpers.h:12
#define printk(level,...)
Definition: stdlib.h:16
void __noreturn die(const char *fmt,...)
Definition: die.c:17
struct resource * free_resources
Linked list of free resources.
Definition: device.c:23
DEVTREE_CONST struct device *DEVTREE_CONST all_devices
Linked list of ALL devices.
Definition: device_const.c:13
bool dev_is_active_bridge(struct device *dev)
Definition: device_util.c:622
u32 dev_path_encode(const struct device *dev)
Encode the device path into 3 bytes for logging to CMOS.
Definition: device_util.c:83
void fixed_io_resource(struct device *dev, unsigned long index, unsigned long base, unsigned long size)
Definition: device_util.c:843
static void resource_tree(const struct device *root, int debug_level, int depth)
Definition: device_util.c:693
void dev_set_enabled(struct device *dev, int enable)
Definition: device_util.c:594
void show_all_devs_resources(int debug_level, const char *msg)
Definition: device_util.c:809
const char * dev_name(const struct device *dev)
Definition: device_util.c:233
static resource_t align_down(resource_t val, unsigned long gran)
Round a number up to the previous multiple of gran.
Definition: device_util.c:431
void show_all_devs(int debug_level, const char *msg)
Definition: device_util.c:781
void disable_children(struct bus *bus)
Definition: device_util.c:606
void show_devs_subtree(struct device *root, int debug_level, const char *msg)
Definition: device_util.c:771
struct device * dev_find_lapic(unsigned int apic_id)
Given a Local APIC ID, find the device structure.
Definition: device_util.c:17
void fixed_mem_resource(struct device *dev, unsigned long index, unsigned long basek, unsigned long sizek, unsigned long type)
Definition: device_util.c:825
struct resource * new_resource(struct device *dev, unsigned int index)
See if a resource structure already exists for a given index and if not allocate one.
Definition: device_util.c:346
void compact_resources(struct device *dev)
See if we have unused but allocated resource structures.
Definition: device_util.c:302
struct device * dev_find_device(u16 vendor, u16 device, struct device *from)
Find a device of a given vendor and type.
Definition: device_util.c:42
void show_devs_tree(const struct device *dev, int debug_level, int depth)
Definition: device_util.c:742
static void free_resource(struct device *dev, struct resource *res, struct resource *prev)
Remove resource res from the device's list and add it to the free list.
Definition: device_util.c:283
resource_t resource_max(const struct resource *resource)
Compute the maximum legal value for resource->base.
Definition: device_util.c:471
void show_all_devs_tree(int debug_level, const char *msg)
Definition: device_util.c:763
void search_global_resources(unsigned long type_mask, unsigned long type, resource_search_t search, void *gp)
Definition: device_util.c:568
const char * bus_path(struct bus *bus)
Definition: device_util.c:243
void mmconf_resource(struct device *dev, unsigned long index)
Definition: device_util.c:857
void show_one_resource(int debug_level, struct device *dev, struct resource *resource, const char *comment)
Definition: device_util.c:794
void tolm_test(void *gp, struct device *dev, struct resource *new)
Definition: device_util.c:870
struct device * dev_find_class(unsigned int class, struct device *from)
Find a device of a given class.
Definition: device_util.c:64
struct resource * probe_resource(const struct device *dev, unsigned int index)
See if a resource structure already exists for a given index.
Definition: device_util.c:323
u32 find_pci_tolm(struct bus *bus)
Definition: device_util.c:890
void print_resource_tree(const struct device *root, int debug_level, const char *msg)
Definition: device_util.c:725
const char * resource_type(const struct resource *resource)
Return the resource type of a resource.
Definition: device_util.c:486
void report_resource_stored(struct device *dev, const struct resource *resource, const char *comment)
Print the resource that was just stored.
Definition: device_util.c:508
static resource_t align_up(resource_t val, unsigned long gran)
Round a number up to the next multiple of gran.
Definition: device_util.c:415
resource_t resource_end(const struct resource *resource)
Compute the maximum address that is part of a resource.
Definition: device_util.c:445
void search_bus_resources(struct bus *bus, unsigned long type_mask, unsigned long type, resource_search_t search, void *gp)
Definition: device_util.c:531
struct resource * find_resource(const struct device *dev, unsigned int index)
Return an existing resource structure for a given index.
Definition: device_util.c:394
const char * dev_path_name(enum device_path_type type)
Definition: device_util.c:925
static int allocate_more_resources(void)
Allocate 64 more resources to the free list.
Definition: device_util.c:256
const char * dev_path(const struct device *dev)
Definition: device_util.c:149
int dev_count_cpu(void)
Definition: device_util.c:907
void add_more_links(struct device *dev, unsigned int total_links)
Ensure the device has a minimum number of bus links.
Definition: device_util.c:652
void * malloc(size_t size)
Definition: malloc.c:53
unsigned int type
Definition: edid.c:57
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define BIOS_EMERG
BIOS_EMERG - Emergency / Fatal.
Definition: loglevel.h:25
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
static uint8_t * buf
Definition: uart.c:7
result
Definition: mrc_cache.c:35
#define BUS_PATH_MAX
Definition: path.h:134
device_path_type
Definition: path.h:6
@ DEVICE_PATH_I2C
Definition: path.h:11
@ DEVICE_PATH_SPI
Definition: path.h:19
@ DEVICE_PATH_CPU_BUS
Definition: path.h:16
@ DEVICE_PATH_GPIO
Definition: path.h:22
@ DEVICE_PATH_PNP
Definition: path.h:10
@ DEVICE_PATH_PCI
Definition: path.h:9
@ DEVICE_PATH_CPU_CLUSTER
Definition: path.h:14
@ DEVICE_PATH_ROOT
Definition: path.h:8
@ DEVICE_PATH_APIC
Definition: path.h:12
@ DEVICE_PATH_DOMAIN
Definition: path.h:13
@ DEVICE_PATH_IOAPIC
Definition: path.h:17
@ DEVICE_PATH_USB
Definition: path.h:20
@ DEVICE_PATH_GENERIC
Definition: path.h:18
@ DEVICE_PATH_NONE
Definition: path.h:7
@ DEVICE_PATH_CPU
Definition: path.h:15
@ DEVICE_PATH_MMIO
Definition: path.h:21
#define DEVICE_PATH_NAMES
Definition: path.h:30
#define DEVICE_PATH_MAX
Definition: path.h:133
#define PCI_FUNC(devfn)
Definition: pci_def.h:550
#define PCI_SLOT(devfn)
Definition: pci_def.h:549
u8 buffer[C2P_BUFFER_MAXSIZE]
Definition: psp_smm.c:18
static size_t search(const char *p, const u8 *a, size_t lengthp, size_t lengtha)
search: Find first instance of string in a given region
Definition: r8168.c:53
#define IORESOURCE_IRQ
Definition: resource.h:11
#define IORESOURCE_RESERVE
Definition: resource.h:30
#define IORESOURCE_DRQ
Definition: resource.h:12
#define IORESOURCE_MEM
Definition: resource.h:10
#define IORESOURCE_SUBTRACTIVE
Definition: resource.h:24
#define IORESOURCE_PCI64
Definition: resource.h:39
#define IORESOURCE_PCI_BRIDGE
Definition: resource.h:40
#define IORESOURCE_STORED
Definition: resource.h:32
#define IORESOURCE_ASSIGNED
Definition: resource.h:34
#define IORESOURCE_IO
Definition: resource.h:9
u64 resource_t
Definition: resource.h:43
#define RESOURCE_TYPE_MAX
Definition: resource.h:84
void(* resource_search_t)(void *gp, struct device *dev, struct resource *res)
Definition: resource.h:76
#define IORESOURCE_PREFETCH
Definition: resource.h:17
#define IORESOURCE_READONLY
Definition: resource.h:18
#define IOINDEX_SUBTRACTIVE_LINK(IDX)
Definition: resource.h:58
#define IORESOURCE_FIXED
Definition: resource.h:36
uintptr_t base
Definition: uart.c:17
static const int mask[4]
Definition: gpio.c:308
#define NULL
Definition: stddef.h:19
uint32_t u32
Definition: stdint.h:51
uint16_t u16
Definition: stdint.h:48
char * strcpy(char *dst, const char *src)
Definition: string.c:92
unsigned int apic_id
Definition: path.h:72
Definition: device.h:76
DEVTREE_CONST struct bus * next
Definition: device.h:80
DEVTREE_CONST struct device * children
Definition: device.h:79
unsigned char link_num
Definition: device.h:83
DEVTREE_CONST struct device * dev
Definition: device.h:78
uint16_t secondary
Definition: device.h:84
const char * name
Definition: device.h:29
void(* enable_dev)(struct device *dev)
Definition: device.h:24
unsigned int id
Definition: path.h:92
unsigned int cluster
Definition: path.h:84
unsigned int id
Definition: path.h:88
void(* enable)(struct device *dev)
Definition: device.h:45
struct usb_path usb
Definition: path.h:127
struct apic_path apic
Definition: path.h:119
struct domain_path domain
Definition: path.h:121
struct cpu_path cpu
Definition: path.h:123
struct i2c_path i2c
Definition: path.h:118
struct cpu_cluster_path cpu_cluster
Definition: path.h:122
struct cpu_bus_path cpu_bus
Definition: path.h:124
struct mmio_path mmio
Definition: path.h:128
struct generic_path generic
Definition: path.h:125
struct ioapic_path ioapic
Definition: path.h:120
struct pnp_path pnp
Definition: path.h:117
struct gpio_path gpio
Definition: path.h:129
struct spi_path spi
Definition: path.h:126
struct pci_path pci
Definition: path.h:116
enum device_path_type type
Definition: path.h:114
Definition: device.h:107
struct chip_operations * chip_ops
Definition: device.h:144
DEVTREE_CONST struct device * sibling
Definition: device.h:111
const char * name
Definition: device.h:145
unsigned int vendor
Definition: device.h:116
struct device_path path
Definition: device.h:115
struct device_operations * ops
Definition: device.h:143
DEVTREE_CONST struct bus * bus
Definition: device.h:108
unsigned int device
Definition: device.h:117
unsigned int class
Definition: device.h:120
DEVTREE_CONST struct bus * link_list
Definition: device.h:139
DEVTREE_CONST struct device * next
Definition: device.h:113
DEVTREE_CONST struct resource * resource_list
Definition: device.h:134
unsigned int enabled
Definition: device.h:122
unsigned int domain
Definition: path.h:50
unsigned int subid
Definition: path.h:97
unsigned int id
Definition: path.h:96
unsigned int id
Definition: path.h:110
unsigned int device
Definition: path.h:63
unsigned int mode_10bit
Definition: path.h:64
unsigned int ioapic_id
Definition: path.h:80
uintptr_t addr
Definition: path.h:106
unsigned int devfn
Definition: path.h:54
unsigned int port
Definition: path.h:58
unsigned int device
Definition: path.h:59
unsigned long flags
Definition: resource.h:49
unsigned char align
Definition: resource.h:51
resource_t limit
Definition: resource.h:47
unsigned char gran
Definition: resource.h:52
resource_t base
Definition: resource.h:45
unsigned long index
Definition: resource.h:50
resource_t size
Definition: resource.h:46
DEVTREE_CONST struct resource * next
Definition: resource.h:48
unsigned int cs
Definition: path.h:68
unsigned int port_type
Definition: path.h:101
unsigned int port_id
Definition: path.h:102
u8 val
Definition: sys.c:300
#define count
int snprintf(char *buf, size_t size, const char *fmt,...)
Note: This file is only for POSIX compatibility, and is meant to be chain-included via string....
Definition: vsprintf.c:35