1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 /*
4 * Originally based on the Linux kernel (arch/i386/kernel/pci-pc.c).
5 */
6
7 #include <console/console.h>
8 #include <device/device.h>
9 #include <device/pci_def.h>
10 #include <device/pci_ids.h>
11 #include <post.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <smp/spinlock.h>
15 #include <timer.h>
16
17 /** Pointer to the last device */
18 extern struct device *last_dev;
19 /** Linked list of free resources */
20 struct resource *free_resources = NULL;
21 /* Disable a PCI device based on bus, device and function. */
devfn_disable(const struct bus * bus,unsigned int devfn)22 void devfn_disable(const struct bus *bus, unsigned int devfn)
23 {
24 struct device *dev = pcidev_path_behind(bus, devfn);
25 if (dev)
26 dev->enabled = 0;
27 }
28
29 /**
30 * Initialize all chips of statically known devices.
31 *
32 * Will be called before bus enumeration to initialize chips stated in the
33 * device tree.
34 */
dev_initialize_chips(void)35 void dev_initialize_chips(void)
36 {
37 const struct device *dev;
38
39 for (dev = all_devices; dev; dev = dev->next) {
40 /* Initialize chip if we haven't yet. */
41 if (dev->chip_ops && dev->chip_ops->init &&
42 !dev->chip_ops->initialized) {
43 post_log_path(dev);
44 dev->chip_ops->init(dev->chip_info);
45 dev->chip_ops->initialized = 1;
46 }
47 }
48 post_log_clear();
49 }
50
51 /**
52 * Finalize all chips of statically known devices.
53 *
54 * This is the last call before calling the payload. This is a good place
55 * to lock registers or other final cleanup.
56 */
dev_finalize_chips(void)57 void dev_finalize_chips(void)
58 {
59 const struct device *dev;
60
61 for (dev = all_devices; dev; dev = dev->next) {
62 /* Initialize chip if we haven't yet. */
63 if (dev->chip_ops && dev->chip_ops->final &&
64 !dev->chip_ops->finalized) {
65 dev->chip_ops->final(dev->chip_info);
66 dev->chip_ops->finalized = 1;
67 }
68 }
69 }
70
DECLARE_SPIN_LOCK(dev_lock)71 DECLARE_SPIN_LOCK(dev_lock)
72
73 /**
74 * Allocate a new device structure.
75 *
76 * Allocate a new device structure and attach it to the device tree as a
77 * child of the parent bus.
78 *
79 * @param parent Parent bus the newly created device should be attached to.
80 * @param path Path to the device to be created.
81 * @return Pointer to the newly created device structure.
82 *
83 * @see device_path
84 */
85 static struct device *__alloc_dev(struct bus *parent, struct device_path *path)
86 {
87 struct device *dev, *child;
88
89 /* Find the last child of our parent. */
90 for (child = parent->children; child && child->sibling; /* */)
91 child = child->sibling;
92
93 dev = malloc(sizeof(*dev));
94 if (dev == 0)
95 die("alloc_dev(): out of memory.\n");
96
97 memset(dev, 0, sizeof(*dev));
98 memcpy(&dev->path, path, sizeof(*path));
99
100 /* By default devices are enabled. */
101 dev->enabled = 1;
102
103 /* Add the new device to the list of children of the bus. */
104 dev->upstream = parent;
105 if (child)
106 child->sibling = dev;
107 else
108 parent->children = dev;
109
110 /* Append a new device to the global device list.
111 * The list is used to find devices once everything is set up.
112 */
113 last_dev->next = dev;
114 last_dev = dev;
115
116 return dev;
117 }
118
alloc_dev(struct bus * parent,struct device_path * path)119 struct device *alloc_dev(struct bus *parent, struct device_path *path)
120 {
121 struct device *dev;
122 spin_lock(&dev_lock);
123 dev = __alloc_dev(parent, path);
124 spin_unlock(&dev_lock);
125 return dev;
126 }
127
DECLARE_SPIN_LOCK(bus_lock)128 DECLARE_SPIN_LOCK(bus_lock)
129
130 /**
131 * Allocate a new bus structure
132 *
133 * Allocate a new downstream bus structure below a device and attach it
134 * to the device tree if the device doesn't already have a downstream bus.
135 *
136 * @param parent Parent device the to-be-created bus should be attached to.
137 * @return Pointer to the newly created bus structure or the existing bus.
138 *
139 */
140 static struct bus *__alloc_bus(struct device *parent)
141 {
142 if (parent->downstream)
143 return parent->downstream;
144
145 struct bus *bus = calloc(1, sizeof(struct bus));
146 if (!bus)
147 die("Couldn't allocate downstream bus!\n");
148 parent->downstream = bus;
149 bus->dev = parent;
150
151 return bus;
152 }
153
alloc_bus(struct device * parent)154 struct bus *alloc_bus(struct device *parent)
155 {
156 struct bus *bus;
157 spin_lock(&bus_lock);
158 bus = __alloc_bus(parent);
159 spin_unlock(&bus_lock);
160 return bus;
161 }
162
163 /**
164 * See if a device structure already exists and if not allocate it.
165 *
166 * @param parent The bus to find the device on.
167 * @param path The relative path from the bus to the appropriate device.
168 * @return Pointer to a device structure for the device on bus at path.
169 */
alloc_find_dev(struct bus * parent,struct device_path * path)170 struct device *alloc_find_dev(struct bus *parent, struct device_path *path)
171 {
172 struct device *child;
173 spin_lock(&dev_lock);
174 child = find_dev_path(parent, path);
175 if (!child)
176 child = __alloc_dev(parent, path);
177 spin_unlock(&dev_lock);
178 return child;
179 }
180
181 /**
182 * Read the resources on all devices of a given bus.
183 *
184 * @param bus Bus to read the resources on.
185 */
read_resources(struct bus * bus)186 static void read_resources(struct bus *bus)
187 {
188 struct device *curdev;
189
190 printk(BIOS_SPEW, "%s %s segment group %d bus %d\n", dev_path(bus->dev),
191 __func__, bus->segment_group, bus->secondary);
192
193 /* Walk through all devices and find which resources they need. */
194 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
195 if (!curdev->enabled)
196 continue;
197
198 if (!curdev->ops || !curdev->ops->read_resources) {
199 if (curdev->path.type != DEVICE_PATH_APIC)
200 printk(BIOS_ERR, "%s missing %s\n",
201 dev_path(curdev), __func__);
202 continue;
203 }
204 post_log_path(curdev);
205 curdev->ops->read_resources(curdev);
206
207 /* Read in the resources behind the current device's links. */
208 if (curdev->downstream)
209 read_resources(curdev->downstream);
210 }
211 post_log_clear();
212 printk(BIOS_SPEW, "%s %s segment group %d bus %d done\n",
213 dev_path(bus->dev), __func__, bus->segment_group, bus->secondary);
214 }
215
216 struct device *vga_pri = NULL;
set_vga_bridge_bits(void)217 static void set_vga_bridge_bits(void)
218 {
219 /*
220 * FIXME: Modify set_vga_bridge() so it is less PCI-centric!
221 * This function knows too much about PCI stuff, it should be just
222 * an iterator/visitor.
223 */
224
225 /* FIXME: Handle the VGA palette snooping. */
226 struct device *dev, *vga, *vga_onboard;
227 struct bus *bus;
228
229 bus = 0;
230 vga = 0;
231 vga_onboard = 0;
232
233 dev = NULL;
234 while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) {
235 if (!dev->enabled)
236 continue;
237
238 printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev));
239 if (dev->upstream->no_vga16) {
240 printk(BIOS_WARNING,
241 "A bridge on the path doesn't support 16-bit VGA decoding!");
242 }
243
244 if (dev->on_mainboard)
245 vga_onboard = dev;
246 else
247 vga = dev;
248
249 /* It isn't safe to enable all VGA cards. */
250 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
251 }
252
253 if (!vga)
254 vga = vga_onboard;
255
256 if (CONFIG(ONBOARD_VGA_IS_PRIMARY) && vga_onboard)
257 vga = vga_onboard;
258
259 /* If we prefer plugin VGA over chipset VGA, the chipset might
260 want to know. */
261 if (!CONFIG(ONBOARD_VGA_IS_PRIMARY) && (vga != vga_onboard) &&
262 vga_onboard && vga_onboard->ops && vga_onboard->ops->vga_disable) {
263 printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n");
264 vga_onboard->ops->vga_disable(vga_onboard);
265 }
266
267 if (vga) {
268 /* VGA is first add-on card or the only onboard VGA. */
269 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
270 /* All legacy VGA cards have MEM & I/O space registers. */
271 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
272 vga_pri = vga;
273 bus = vga->upstream;
274 }
275
276 /* Now walk up the bridges setting the VGA enable. */
277 while (bus) {
278 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
279 dev_path(bus->dev));
280 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA | PCI_BRIDGE_CTL_VGA16;
281 bus = (bus == bus->dev->upstream) ? 0 : bus->dev->upstream;
282 }
283 }
284
285 /**
286 * Assign the computed resources to the devices on the bus.
287 *
288 * Use the device specific set_resources() method to store the computed
289 * resources to hardware. For bridge devices, the set_resources() method
290 * has to recurse into every down stream buses.
291 *
292 * Mutual recursion:
293 * assign_resources() -> device_operation::set_resources()
294 * device_operation::set_resources() -> assign_resources()
295 *
296 * @param bus Pointer to the structure for this bus.
297 */
assign_resources(struct bus * bus)298 void assign_resources(struct bus *bus)
299 {
300 struct device *curdev;
301
302 printk(BIOS_SPEW, "%s %s, segment group %d bus %d\n",
303 dev_path(bus->dev), __func__, bus->segment_group, bus->secondary);
304
305 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
306 if (!curdev->enabled || !curdev->resource_list)
307 continue;
308
309 if (!curdev->ops || !curdev->ops->set_resources) {
310 printk(BIOS_ERR, "%s missing set_resources\n",
311 dev_path(curdev));
312 continue;
313 }
314 post_log_path(curdev);
315 curdev->ops->set_resources(curdev);
316 }
317 post_log_clear();
318 printk(BIOS_SPEW, "%s %s, segment group %d bus %d done\n",
319 dev_path(bus->dev), __func__, bus->segment_group, bus->secondary);
320 }
321
322 /**
323 * Enable the resources for devices on a link.
324 *
325 * Enable resources of the device by calling the device specific
326 * enable_resources() method.
327 *
328 * The parent's resources should be enabled first to avoid having enabling
329 * order problem. This is done by calling the parent's enable_resources()
330 * method before its children's enable_resources() methods.
331 *
332 * @param link The link whose devices' resources are to be enabled.
333 */
enable_resources(struct bus * link)334 static void enable_resources(struct bus *link)
335 {
336 struct device *dev;
337
338 for (dev = link->children; dev; dev = dev->sibling) {
339 if (dev->enabled && dev->ops && dev->ops->enable_resources) {
340 post_log_path(dev);
341 dev->ops->enable_resources(dev);
342 }
343 }
344
345 for (dev = link->children; dev; dev = dev->sibling) {
346 if (dev->downstream)
347 enable_resources(dev->downstream);
348 }
349 post_log_clear();
350 }
351
352 /**
353 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
354 *
355 * @param bus Pointer to the bus structure.
356 * @return 1 if the bus was successfully reset, 0 otherwise.
357 */
reset_bus(struct bus * bus)358 int reset_bus(struct bus *bus)
359 {
360 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
361 bus->dev->ops->reset_bus(bus);
362 bus->reset_needed = 0;
363 return 1;
364 }
365 return 0;
366 }
367
368 /**
369 * Scan for devices on a bus.
370 *
371 * If there are bridges on the bus, recursively scan the buses behind the
372 * bridges. If the setting up and tuning of the bus causes a reset to be
373 * required, reset the bus and scan it again.
374 *
375 * @param busdev Pointer to the bus device.
376 */
scan_bus(struct device * busdev)377 static void scan_bus(struct device *busdev)
378 {
379 int do_scan_bus;
380 struct stopwatch sw;
381 long scan_time;
382
383 if (!busdev->enabled)
384 return;
385
386 printk(BIOS_DEBUG, "%s scanning...\n", dev_path(busdev));
387
388 post_log_path(busdev);
389
390 stopwatch_init(&sw);
391
392 do_scan_bus = 1;
393 while (do_scan_bus) {
394 struct bus *link = busdev->downstream;
395 busdev->ops->scan_bus(busdev);
396 do_scan_bus = 0;
397 if (!link || !link->reset_needed)
398 continue;
399 if (reset_bus(link))
400 do_scan_bus = 1;
401 else
402 busdev->upstream->reset_needed = 1;
403 }
404
405 scan_time = stopwatch_duration_msecs(&sw);
406 printk(BIOS_DEBUG, "%s: bus %s finished in %ld msecs\n", __func__,
407 dev_path(busdev), scan_time);
408 }
409
scan_bridges(struct bus * bus)410 void scan_bridges(struct bus *bus)
411 {
412 struct device *child;
413
414 for (child = bus->children; child; child = child->sibling) {
415 if (!child->ops || !child->ops->scan_bus)
416 continue;
417 scan_bus(child);
418 }
419 }
420
421 /**
422 * Determine the existence of devices and extend the device tree.
423 *
424 * Most of the devices in the system are listed in the mainboard devicetree.cb
425 * file. The device structures for these devices are generated at compile
426 * time by the config tool and are organized into the device tree. This
427 * function determines if the devices created at compile time actually exist
428 * in the physical system.
429 *
430 * For devices in the physical system but not listed in devicetree.cb,
431 * the device structures have to be created at run time and attached to the
432 * device tree.
433 *
434 * This function starts from the root device 'dev_root', scans the buses in
435 * the system recursively, and modifies the device tree according to the
436 * result of the probe.
437 *
438 * This function has no idea how to scan and probe buses and devices at all.
439 * It depends on the bus/device specific scan_bus() method to do it. The
440 * scan_bus() method also has to create the device structure and attach
441 * it to the device tree.
442 */
dev_enumerate(void)443 void dev_enumerate(void)
444 {
445 struct device *root;
446
447 printk(BIOS_INFO, "Enumerating buses...\n");
448
449 root = &dev_root;
450
451 show_all_devs(BIOS_SPEW, "Before device enumeration.");
452 printk(BIOS_SPEW, "Compare with tree...\n");
453 show_devs_tree(root, BIOS_SPEW, 0);
454
455 if (root->chip_ops && root->chip_ops->enable_dev)
456 root->chip_ops->enable_dev(root);
457
458 if (!root->ops || !root->ops->scan_bus) {
459 printk(BIOS_ERR, "dev_root missing scan_bus operation");
460 return;
461 }
462 scan_bus(root);
463 post_log_clear();
464 printk(BIOS_INFO, "done\n");
465 }
466
467 /**
468 * Configure devices on the devices tree.
469 *
470 * Starting at the root of the device tree, travel it recursively in two
471 * passes. In the first pass, we compute and allocate resources (ranges)
472 * required by each device. In the second pass, the resources ranges are
473 * relocated to their final position and stored to the hardware.
474 *
475 * I/O resources grow upward. MEM resources grow downward.
476 *
477 * Since the assignment is hierarchical we set the values into the dev_root
478 * struct.
479 */
dev_configure(void)480 void dev_configure(void)
481 {
482 const struct device *root;
483
484 set_vga_bridge_bits();
485
486 printk(BIOS_INFO, "Allocating resources...\n");
487
488 root = &dev_root;
489
490 /*
491 * Each domain should create resources which contain the entire address
492 * space for IO, MEM, and PREFMEM resources in the domain. The
493 * allocation of device resources will be done from this address space.
494 */
495
496 /* Read the resources for the entire tree. */
497
498 printk(BIOS_INFO, "Reading resources...\n");
499 read_resources(root->downstream);
500 printk(BIOS_INFO, "Done reading resources.\n");
501
502 print_resource_tree(root, BIOS_SPEW, "After reading.");
503
504 allocate_resources(root);
505
506 assign_resources(root->downstream);
507 printk(BIOS_INFO, "Done setting resources.\n");
508 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
509
510 printk(BIOS_INFO, "Done allocating resources.\n");
511 }
512
513 /**
514 * Enable devices on the device tree.
515 *
516 * Starting at the root, walk the tree and enable all devices/bridges by
517 * calling the device's enable_resources() method.
518 */
dev_enable(void)519 void dev_enable(void)
520 {
521 printk(BIOS_INFO, "Enabling resources...\n");
522
523 /* Now enable everything. */
524 if (dev_root.downstream)
525 enable_resources(dev_root.downstream);
526
527 printk(BIOS_INFO, "done.\n");
528 }
529
530 /**
531 * Initialize a specific device.
532 *
533 * The parent should be initialized first to avoid having an ordering problem.
534 * This is done by calling the parent's init() method before its children's
535 * init() methods.
536 *
537 * @param dev The device to be initialized.
538 */
init_dev(struct device * dev)539 static void init_dev(struct device *dev)
540 {
541 if (!dev->enabled)
542 return;
543
544 if (!dev->initialized && dev->ops && dev->ops->init) {
545 struct stopwatch sw;
546 long init_time;
547
548 if (dev->path.type == DEVICE_PATH_I2C) {
549 printk(BIOS_DEBUG, "smbus: %s->", dev_path(dev->upstream->dev));
550 }
551
552 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
553
554 stopwatch_init(&sw);
555 dev->initialized = 1;
556 dev->ops->init(dev);
557
558 init_time = stopwatch_duration_msecs(&sw);
559 printk(BIOS_DEBUG, "%s init finished in %ld msecs\n", dev_path(dev),
560 init_time);
561 }
562 }
563
init_link(struct bus * link)564 static void init_link(struct bus *link)
565 {
566 struct device *dev;
567
568 for (dev = link->children; dev; dev = dev->sibling) {
569 post_code(POSTCODE_BS_DEV_INIT);
570 post_log_path(dev);
571 init_dev(dev);
572 }
573
574 for (dev = link->children; dev; dev = dev->sibling)
575 if (dev->downstream)
576 init_link(dev->downstream);
577 }
578
579 /**
580 * Initialize all devices in the global device tree.
581 *
582 * Starting at the root device, call the device's init() method to do
583 * device-specific setup, then call each child's init() method.
584 */
dev_initialize(void)585 void dev_initialize(void)
586 {
587 printk(BIOS_INFO, "Initializing devices...\n");
588
589 /* First call the mainboard init. */
590 init_dev(&dev_root);
591
592 /* Now initialize everything. */
593 if (dev_root.downstream)
594 init_link(dev_root.downstream);
595 post_log_clear();
596
597 printk(BIOS_INFO, "Devices initialized\n");
598 show_all_devs(BIOS_SPEW, "After init.");
599 }
600
601 /**
602 * Finalize a specific device.
603 *
604 * The parent should be finalized first to avoid having an ordering problem.
605 * This is done by calling the parent's final() method before its children's
606 * final() methods.
607 *
608 * @param dev The device to be initialized.
609 */
final_dev(struct device * dev)610 static void final_dev(struct device *dev)
611 {
612 if (!dev->enabled)
613 return;
614
615 if (dev->ops && dev->ops->final) {
616 printk(BIOS_DEBUG, "%s final\n", dev_path(dev));
617 dev->ops->final(dev);
618 }
619 }
620
final_link(struct bus * link)621 static void final_link(struct bus *link)
622 {
623 struct device *dev;
624
625 for (dev = link->children; dev; dev = dev->sibling)
626 final_dev(dev);
627
628 for (dev = link->children; dev; dev = dev->sibling)
629 if (dev->downstream)
630 final_link(dev->downstream);
631 }
632 /**
633 * Finalize all devices in the global device tree.
634 *
635 * Starting at the root device, call the device's final() method to do
636 * device-specific cleanup, then call each child's final() method.
637 */
dev_finalize(void)638 void dev_finalize(void)
639 {
640 printk(BIOS_INFO, "Finalize devices...\n");
641
642 /* First call the mainboard finalize. */
643 final_dev(&dev_root);
644
645 /* Now finalize everything. */
646 final_link(dev_root.downstream);
647
648 printk(BIOS_INFO, "Devices finalized\n");
649 }
650