Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 7e61e45 | 2008-01-18 10:35:56 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 3 | * |
Martin Roth | bb5953d | 2016-04-11 20:53:39 -0600 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 12 | */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 13 | |
| 14 | /* |
Martin Roth | 99f83bb | 2019-09-15 20:57:18 -0700 | [diff] [blame] | 15 | * Originally based on the Linux kernel (arch/i386/kernel/pci-pc.c). |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <console/console.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 19 | #include <device/device.h> |
Kyösti Mälkki | 318066f | 2014-02-12 14:18:50 +0200 | [diff] [blame] | 20 | #include <device/pci_def.h> |
Li-Ta Lo | 54f05f6 | 2004-05-14 17:20:29 +0000 | [diff] [blame] | 21 | #include <device/pci_ids.h> |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 24 | #include <smp/spinlock.h> |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 25 | #if CONFIG(ARCH_X86) |
Duncan Laurie | b4aaaa7 | 2012-01-17 09:03:11 -0800 | [diff] [blame] | 26 | #include <arch/ebda.h> |
| 27 | #endif |
Aaron Durbin | 0529429 | 2013-04-30 15:41:13 -0500 | [diff] [blame] | 28 | #include <timer.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 29 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 30 | /** Pointer to the last device */ |
Myles Watson | 70679a0 | 2010-09-01 21:03:03 +0000 | [diff] [blame] | 31 | extern struct device *last_dev; |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 32 | /** Linked list of free resources */ |
| 33 | struct resource *free_resources = NULL; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 34 | |
Nico Huber | acd7d95 | 2012-07-25 10:33:05 +0200 | [diff] [blame] | 35 | /** |
| 36 | * Initialize all chips of statically known devices. |
| 37 | * |
| 38 | * Will be called before bus enumeration to initialize chips stated in the |
| 39 | * device tree. |
| 40 | */ |
| 41 | void dev_initialize_chips(void) |
| 42 | { |
Lubomir Rintel | 9ba8f7c | 2018-04-26 00:00:22 +0200 | [diff] [blame] | 43 | const struct device *dev; |
Nico Huber | acd7d95 | 2012-07-25 10:33:05 +0200 | [diff] [blame] | 44 | |
| 45 | for (dev = all_devices; dev; dev = dev->next) { |
| 46 | /* Initialize chip if we haven't yet. */ |
| 47 | if (dev->chip_ops && dev->chip_ops->init && |
| 48 | !dev->chip_ops->initialized) { |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 49 | post_log_path(dev); |
Nico Huber | acd7d95 | 2012-07-25 10:33:05 +0200 | [diff] [blame] | 50 | dev->chip_ops->init(dev->chip_info); |
| 51 | dev->chip_ops->initialized = 1; |
| 52 | } |
| 53 | } |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 54 | post_log_clear(); |
Nico Huber | acd7d95 | 2012-07-25 10:33:05 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Marc Jones | 2a58ecd | 2013-10-29 17:32:00 -0600 | [diff] [blame] | 57 | /** |
| 58 | * Finalize all chips of statically known devices. |
| 59 | * |
| 60 | * This is the last call before calling the payload. This is a good place |
| 61 | * to lock registers or other final cleanup. |
| 62 | */ |
| 63 | void dev_finalize_chips(void) |
| 64 | { |
Lubomir Rintel | 9ba8f7c | 2018-04-26 00:00:22 +0200 | [diff] [blame] | 65 | const struct device *dev; |
Marc Jones | 2a58ecd | 2013-10-29 17:32:00 -0600 | [diff] [blame] | 66 | |
| 67 | for (dev = all_devices; dev; dev = dev->next) { |
| 68 | /* Initialize chip if we haven't yet. */ |
| 69 | if (dev->chip_ops && dev->chip_ops->final && |
| 70 | !dev->chip_ops->finalized) { |
| 71 | dev->chip_ops->final(dev->chip_info); |
| 72 | dev->chip_ops->finalized = 1; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 77 | DECLARE_SPIN_LOCK(dev_lock) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 78 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 79 | #if CONFIG(GFXUMA) |
Kyösti Mälkki | cc55b9b | 2012-07-11 07:55:21 +0300 | [diff] [blame] | 80 | /* IGD UMA memory */ |
| 81 | uint64_t uma_memory_base = 0; |
| 82 | uint64_t uma_memory_size = 0; |
Kyösti Mälkki | b25374c | 2012-08-01 08:05:22 +0300 | [diff] [blame] | 83 | #endif |
Kyösti Mälkki | cc55b9b | 2012-07-11 07:55:21 +0300 | [diff] [blame] | 84 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 85 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 86 | * Allocate a new device structure. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 87 | * |
Martin Roth | 63373ed | 2013-07-08 16:24:19 -0600 | [diff] [blame] | 88 | * Allocate a new device structure and attach it to the device tree as a |
Li-Ta Lo | 9f0d0f9 | 2004-05-10 16:05:16 +0000 | [diff] [blame] | 89 | * child of the parent bus. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 90 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 91 | * @param parent Parent bus the newly created device should be attached to. |
| 92 | * @param path Path to the device to be created. |
| 93 | * @return Pointer to the newly created device structure. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 94 | * |
| 95 | * @see device_path |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 96 | */ |
Elyes HAOUAS | d34a785 | 2018-09-17 10:44:14 +0200 | [diff] [blame] | 97 | static struct device *__alloc_dev(struct bus *parent, struct device_path *path) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 98 | { |
Elyes HAOUAS | e348066 | 2018-05-06 20:32:23 +0200 | [diff] [blame] | 99 | struct device *dev, *child; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 100 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 101 | /* Find the last child of our parent. */ |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 102 | for (child = parent->children; child && child->sibling; /* */) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 103 | child = child->sibling; |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame] | 104 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 105 | dev = malloc(sizeof(*dev)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 106 | if (dev == 0) |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 107 | die("alloc_dev(): out of memory.\n"); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 108 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 109 | memset(dev, 0, sizeof(*dev)); |
| 110 | memcpy(&dev->path, path, sizeof(*path)); |
| 111 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 112 | /* By default devices are enabled. */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 113 | dev->enabled = 1; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 114 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 115 | /* Add the new device to the list of children of the bus. */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 116 | dev->bus = parent; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 117 | if (child) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 118 | child->sibling = dev; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 119 | else |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 120 | parent->children = dev; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 121 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 122 | /* Append a new device to the global device list. |
| 123 | * The list is used to find devices once everything is set up. |
| 124 | */ |
Myles Watson | 70679a0 | 2010-09-01 21:03:03 +0000 | [diff] [blame] | 125 | last_dev->next = dev; |
| 126 | last_dev = dev; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 127 | |
Kyösti Mälkki | a5650a4 | 2012-07-07 17:15:51 +0300 | [diff] [blame] | 128 | return dev; |
| 129 | } |
| 130 | |
Elyes HAOUAS | d34a785 | 2018-09-17 10:44:14 +0200 | [diff] [blame] | 131 | struct device *alloc_dev(struct bus *parent, struct device_path *path) |
Kyösti Mälkki | a5650a4 | 2012-07-07 17:15:51 +0300 | [diff] [blame] | 132 | { |
Elyes HAOUAS | e348066 | 2018-05-06 20:32:23 +0200 | [diff] [blame] | 133 | struct device *dev; |
Kyösti Mälkki | a5650a4 | 2012-07-07 17:15:51 +0300 | [diff] [blame] | 134 | spin_lock(&dev_lock); |
| 135 | dev = __alloc_dev(parent, path); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 136 | spin_unlock(&dev_lock); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 137 | return dev; |
| 138 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 139 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 140 | /** |
Kyösti Mälkki | a5650a4 | 2012-07-07 17:15:51 +0300 | [diff] [blame] | 141 | * See if a device structure already exists and if not allocate it. |
| 142 | * |
| 143 | * @param parent The bus to find the device on. |
| 144 | * @param path The relative path from the bus to the appropriate device. |
| 145 | * @return Pointer to a device structure for the device on bus at path. |
| 146 | */ |
Elyes HAOUAS | d34a785 | 2018-09-17 10:44:14 +0200 | [diff] [blame] | 147 | struct device *alloc_find_dev(struct bus *parent, struct device_path *path) |
Kyösti Mälkki | a5650a4 | 2012-07-07 17:15:51 +0300 | [diff] [blame] | 148 | { |
Elyes HAOUAS | e348066 | 2018-05-06 20:32:23 +0200 | [diff] [blame] | 149 | struct device *child; |
Kyösti Mälkki | a5650a4 | 2012-07-07 17:15:51 +0300 | [diff] [blame] | 150 | spin_lock(&dev_lock); |
| 151 | child = find_dev_path(parent, path); |
| 152 | if (!child) |
| 153 | child = __alloc_dev(parent, path); |
| 154 | spin_unlock(&dev_lock); |
| 155 | return child; |
| 156 | } |
| 157 | |
| 158 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 159 | * Round a number up to an alignment. |
| 160 | * |
| 161 | * @param val The starting value. |
Martin Roth | 32bc6b6 | 2015-01-04 16:54:35 -0700 | [diff] [blame] | 162 | * @param pow Alignment as a power of two. |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 163 | * @return Rounded up number. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 164 | */ |
Eric Biederman | 448bd63 | 2004-10-14 22:52:15 +0000 | [diff] [blame] | 165 | static resource_t round(resource_t val, unsigned long pow) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 166 | { |
Eric Biederman | 448bd63 | 2004-10-14 22:52:15 +0000 | [diff] [blame] | 167 | resource_t mask; |
| 168 | mask = (1ULL << pow) - 1ULL; |
| 169 | val += mask; |
| 170 | val &= ~mask; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 171 | return val; |
| 172 | } |
| 173 | |
Elyes HAOUAS | b0b0c8c | 2018-07-08 12:33:47 +0200 | [diff] [blame] | 174 | static const char *resource2str(struct resource *res) |
Kyösti Mälkki | e6a9290f | 2015-03-23 19:37:38 +0200 | [diff] [blame] | 175 | { |
| 176 | if (res->flags & IORESOURCE_IO) |
| 177 | return "io"; |
| 178 | if (res->flags & IORESOURCE_PREFETCH) |
| 179 | return "prefmem"; |
| 180 | if (res->flags & IORESOURCE_MEM) |
| 181 | return "mem"; |
| 182 | return "undefined"; |
| 183 | } |
| 184 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 185 | /** |
| 186 | * Read the resources on all devices of a given bus. |
| 187 | * |
| 188 | * @param bus Bus to read the resources on. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 189 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 190 | static void read_resources(struct bus *bus) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 191 | { |
| 192 | struct device *curdev; |
| 193 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 194 | printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev), |
| 195 | __func__, bus->secondary, bus->link_num); |
Eric Biederman | 448bd63 | 2004-10-14 22:52:15 +0000 | [diff] [blame] | 196 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 197 | /* Walk through all devices and find which resources they need. */ |
| 198 | for (curdev = bus->children; curdev; curdev = curdev->sibling) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 199 | struct bus *link; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 200 | |
| 201 | if (!curdev->enabled) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 202 | continue; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 203 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 204 | if (!curdev->ops || !curdev->ops->read_resources) { |
Martin Roth | 7bc74ab | 2015-11-18 20:23:02 -0700 | [diff] [blame] | 205 | if (curdev->path.type != DEVICE_PATH_APIC) |
| 206 | printk(BIOS_ERR, "%s missing read_resources\n", |
| 207 | dev_path(curdev)); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 208 | continue; |
| 209 | } |
Duncan Laurie | 7ed3976 | 2013-07-09 10:46:52 -0700 | [diff] [blame] | 210 | post_log_path(curdev); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 211 | curdev->ops->read_resources(curdev); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 212 | |
| 213 | /* Read in the resources behind the current device's links. */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 214 | for (link = curdev->link_list; link; link = link->next) |
| 215 | read_resources(link); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 216 | } |
Duncan Laurie | 7ed3976 | 2013-07-09 10:46:52 -0700 | [diff] [blame] | 217 | post_log_clear(); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 218 | printk(BIOS_SPEW, "%s read_resources bus %d link: %d done\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 219 | dev_path(bus->dev), bus->secondary, bus->link_num); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 222 | struct pick_largest_state { |
| 223 | struct resource *last; |
Lubomir Rintel | 9ba8f7c | 2018-04-26 00:00:22 +0200 | [diff] [blame] | 224 | const struct device *result_dev; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 225 | struct resource *result; |
| 226 | int seen_last; |
| 227 | }; |
| 228 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 229 | static void pick_largest_resource(void *gp, struct device *dev, |
| 230 | struct resource *resource) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 231 | { |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 232 | struct pick_largest_state *state = gp; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 233 | struct resource *last; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 234 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 235 | last = state->last; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 236 | |
| 237 | /* Be certain to pick the successor to last. */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 238 | if (resource == last) { |
| 239 | state->seen_last = 1; |
| 240 | return; |
| 241 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 242 | if (resource->flags & IORESOURCE_FIXED) |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 243 | return; /* Skip it. */ |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 244 | if (last && ((last->align < resource->align) || |
| 245 | ((last->align == resource->align) && |
| 246 | (last->size < resource->size)) || |
| 247 | ((last->align == resource->align) && |
| 248 | (last->size == resource->size) && (!state->seen_last)))) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 249 | return; |
| 250 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 251 | if (!state->result || |
| 252 | (state->result->align < resource->align) || |
| 253 | ((state->result->align == resource->align) && |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 254 | (state->result->size < resource->size))) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 255 | state->result_dev = dev; |
| 256 | state->result = resource; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 257 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Lubomir Rintel | 9ba8f7c | 2018-04-26 00:00:22 +0200 | [diff] [blame] | 260 | static const struct device *largest_resource(struct bus *bus, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 261 | struct resource **result_res, |
| 262 | unsigned long type_mask, |
| 263 | unsigned long type) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 264 | { |
| 265 | struct pick_largest_state state; |
| 266 | |
| 267 | state.last = *result_res; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 268 | state.result_dev = NULL; |
| 269 | state.result = NULL; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 270 | state.seen_last = 0; |
| 271 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 272 | search_bus_resources(bus, type_mask, type, pick_largest_resource, |
| 273 | &state); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 274 | |
| 275 | *result_res = state.result; |
| 276 | return state.result_dev; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 279 | /** |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 280 | * This function is the guts of the resource allocator. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 281 | * |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 282 | * The problem. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 283 | * - Allocate resource locations for every device. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 284 | * - Don't overlap, and follow the rules of bridges. |
| 285 | * - Don't overlap with resources in fixed locations. |
| 286 | * - Be efficient so we don't have ugly strategies. |
| 287 | * |
| 288 | * The strategy. |
| 289 | * - Devices that have fixed addresses are the minority so don't |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 290 | * worry about them too much. Instead only use part of the address |
| 291 | * space for devices with programmable addresses. This easily handles |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 292 | * everything except bridges. |
| 293 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 294 | * - PCI devices are required to have their sizes and their alignments |
| 295 | * equal. In this case an optimal solution to the packing problem |
| 296 | * exists. Allocate all devices from highest alignment to least |
| 297 | * alignment or vice versa. Use this. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 298 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 299 | * - So we can handle more than PCI run two allocation passes on bridges. The |
| 300 | * first to see how large the resources are behind the bridge, and what |
| 301 | * their alignment requirements are. The second to assign a safe address to |
| 302 | * the devices behind the bridge. This allows us to treat a bridge as just |
| 303 | * a device with a couple of resources, and not need to special case it in |
| 304 | * the allocator. Also this allows handling of other types of bridges. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 305 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 306 | * @param bus The bus we are traversing. |
| 307 | * @param bridge The bridge resource which must contain the bus' resources. |
| 308 | * @param type_mask This value gets ANDed with the resource type. |
| 309 | * @param type This value must match the result of the AND. |
| 310 | * @return TODO |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 311 | */ |
Myles Watson | 54913b9 | 2009-10-13 20:00:09 +0000 | [diff] [blame] | 312 | static void compute_resources(struct bus *bus, struct resource *bridge, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 313 | unsigned long type_mask, unsigned long type) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 314 | { |
Lubomir Rintel | 9ba8f7c | 2018-04-26 00:00:22 +0200 | [diff] [blame] | 315 | const struct device *dev; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 316 | struct resource *resource; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 317 | resource_t base; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 318 | base = round(bridge->base, bridge->align); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 319 | |
John Zhao | 2deb5fb | 2019-05-29 18:12:01 -0700 | [diff] [blame] | 320 | if (!bus) |
| 321 | return; |
| 322 | |
Kyösti Mälkki | e6a9290f | 2015-03-23 19:37:38 +0200 | [diff] [blame] | 323 | printk(BIOS_SPEW, "%s %s: base: %llx size: %llx align: %d gran: %d" |
| 324 | " limit: %llx\n", dev_path(bus->dev), resource2str(bridge), |
| 325 | base, bridge->size, bridge->align, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 326 | bridge->gran, bridge->limit); |
Ronald G. Minnich | 99dcf23 | 2003-09-30 02:16:47 +0000 | [diff] [blame] | 327 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 328 | /* For each child which is a bridge, compute the resource needs. */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 329 | for (dev = bus->children; dev; dev = dev->sibling) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 330 | struct resource *child_bridge; |
| 331 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 332 | if (!dev->link_list) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 333 | continue; |
| 334 | |
| 335 | /* Find the resources with matching type flags. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 336 | for (child_bridge = dev->resource_list; child_bridge; |
| 337 | child_bridge = child_bridge->next) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 338 | struct bus* link; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 339 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 340 | if (!(child_bridge->flags & IORESOURCE_BRIDGE) |
| 341 | || (child_bridge->flags & type_mask) != type) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 342 | continue; |
| 343 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 344 | /* |
| 345 | * Split prefetchable memory if combined. Many domains |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 346 | * use the same address space for prefetchable memory |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 347 | * and non-prefetchable memory. Bridges below them need |
| 348 | * it separated. Add the PREFETCH flag to the type_mask |
| 349 | * and type. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 350 | */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 351 | link = dev->link_list; |
| 352 | while (link && link->link_num != |
| 353 | IOINDEX_LINK(child_bridge->index)) |
| 354 | link = link->next; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 355 | |
| 356 | if (link == NULL) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 357 | printk(BIOS_ERR, "link %ld not found on %s\n", |
| 358 | IOINDEX_LINK(child_bridge->index), |
| 359 | dev_path(dev)); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 362 | compute_resources(link, child_bridge, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 363 | type_mask | IORESOURCE_PREFETCH, |
| 364 | type | (child_bridge->flags & |
| 365 | IORESOURCE_PREFETCH)); |
| 366 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 369 | /* Remember we haven't found anything yet. */ |
| 370 | resource = NULL; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 371 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 372 | /* |
| 373 | * Walk through all the resources on the current bus and compute the |
| 374 | * amount of address space taken by them. Take granularity and |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 375 | * alignment into account. |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 376 | */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 377 | while ((dev = largest_resource(bus, &resource, type_mask, type))) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 378 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 379 | /* Size 0 resources can be skipped. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 380 | if (!resource->size) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 381 | continue; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 382 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 383 | /* Propagate the resource alignment to the bridge resource. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 384 | if (resource->align > bridge->align) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 385 | bridge->align = resource->align; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 386 | |
| 387 | /* Propagate the resource limit to the bridge register. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 388 | if (bridge->limit > resource->limit) |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 389 | bridge->limit = resource->limit; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 390 | |
| 391 | /* Warn if it looks like APICs aren't declared. */ |
| 392 | if ((resource->limit == 0xffffffff) && |
| 393 | (resource->flags & IORESOURCE_ASSIGNED)) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 394 | printk(BIOS_ERR, |
| 395 | "Resource limit looks wrong! (no APIC?)\n"); |
Patrick Georgi | 5161509 | 2012-03-11 19:31:03 +0100 | [diff] [blame] | 396 | printk(BIOS_ERR, "%s %02lx limit %08llx\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 397 | dev_path(dev), resource->index, resource->limit); |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 398 | } |
Stefan Reinauer | 51754a3 | 2008-08-01 12:28:38 +0000 | [diff] [blame] | 399 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 400 | if (resource->flags & IORESOURCE_IO) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 401 | /* |
| 402 | * Don't allow potential aliases over the legacy PCI |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 403 | * expansion card addresses. The legacy PCI decodes |
| 404 | * only 10 bits, uses 0x100 - 0x3ff. Therefore, only |
| 405 | * 0x00 - 0xff can be used out of each 0x400 block of |
| 406 | * I/O space. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 407 | */ |
Eric Biederman | bbb6d10 | 2003-08-04 19:54:48 +0000 | [diff] [blame] | 408 | if ((base & 0x300) != 0) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 409 | base = (base & ~0x3ff) + 0x400; |
| 410 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 411 | /* |
| 412 | * Don't allow allocations in the VGA I/O range. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 413 | * PCI has special cases for that. |
| 414 | */ |
| 415 | else if ((base >= 0x3b0) && (base <= 0x3df)) { |
| 416 | base = 0x3e0; |
| 417 | } |
| 418 | } |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 419 | /* Base must be aligned. */ |
| 420 | base = round(base, resource->align); |
| 421 | resource->base = base; |
| 422 | base += resource->size; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 423 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 424 | printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 425 | dev_path(dev), resource->index, resource->base, |
| 426 | resource->base + resource->size - 1, |
Kyösti Mälkki | e6a9290f | 2015-03-23 19:37:38 +0200 | [diff] [blame] | 427 | resource2str(resource)); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 428 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 429 | |
| 430 | /* |
| 431 | * A PCI bridge resource does not need to be a power of two size, but |
| 432 | * it does have a minimum granularity. Round the size up to that |
| 433 | * minimum granularity so we know not to place something else at an |
Martin Roth | 63373ed | 2013-07-08 16:24:19 -0600 | [diff] [blame] | 434 | * address positively decoded by the bridge. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 435 | */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 436 | bridge->size = round(base, bridge->gran) - |
| 437 | round(bridge->base, bridge->align); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 438 | |
Kyösti Mälkki | e6a9290f | 2015-03-23 19:37:38 +0200 | [diff] [blame] | 439 | printk(BIOS_SPEW, "%s %s: base: %llx size: %llx align: %d gran: %d" |
| 440 | " limit: %llx done\n", dev_path(bus->dev), |
| 441 | resource2str(bridge), |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 442 | base, bridge->size, bridge->align, bridge->gran, bridge->limit); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /** |
| 446 | * This function is the second part of the resource allocator. |
| 447 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 448 | * See the compute_resources function for a more detailed explanation. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 449 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 450 | * This function assigns the resources a value. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 451 | * |
| 452 | * @param bus The bus we are traversing. |
| 453 | * @param bridge The bridge resource which must contain the bus' resources. |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 454 | * @param type_mask This value gets ANDed with the resource type. |
| 455 | * @param type This value must match the result of the AND. |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 456 | * |
| 457 | * @see compute_resources |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 458 | */ |
Myles Watson | 54913b9 | 2009-10-13 20:00:09 +0000 | [diff] [blame] | 459 | static void allocate_resources(struct bus *bus, struct resource *bridge, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 460 | unsigned long type_mask, unsigned long type) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 461 | { |
Lubomir Rintel | 9ba8f7c | 2018-04-26 00:00:22 +0200 | [diff] [blame] | 462 | const struct device *dev; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 463 | struct resource *resource; |
| 464 | resource_t base; |
| 465 | base = bridge->base; |
| 466 | |
John Zhao | 2deb5fb | 2019-05-29 18:12:01 -0700 | [diff] [blame] | 467 | if (!bus) |
| 468 | return; |
| 469 | |
Kyösti Mälkki | e6a9290f | 2015-03-23 19:37:38 +0200 | [diff] [blame] | 470 | printk(BIOS_SPEW, "%s %s: base:%llx size:%llx align:%d gran:%d " |
| 471 | "limit:%llx\n", dev_path(bus->dev), |
| 472 | resource2str(bridge), |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 473 | base, bridge->size, bridge->align, bridge->gran, bridge->limit); |
| 474 | |
| 475 | /* Remember we haven't found anything yet. */ |
| 476 | resource = NULL; |
| 477 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 478 | /* |
| 479 | * Walk through all the resources on the current bus and allocate them |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 480 | * address space. |
| 481 | */ |
| 482 | while ((dev = largest_resource(bus, &resource, type_mask, type))) { |
| 483 | |
| 484 | /* Propagate the bridge limit to the resource register. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 485 | if (resource->limit > bridge->limit) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 486 | resource->limit = bridge->limit; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 487 | |
| 488 | /* Size 0 resources can be skipped. */ |
| 489 | if (!resource->size) { |
| 490 | /* Set the base to limit so it doesn't confuse tolm. */ |
| 491 | resource->base = resource->limit; |
| 492 | resource->flags |= IORESOURCE_ASSIGNED; |
| 493 | continue; |
| 494 | } |
| 495 | |
| 496 | if (resource->flags & IORESOURCE_IO) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 497 | /* |
| 498 | * Don't allow potential aliases over the legacy PCI |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 499 | * expansion card addresses. The legacy PCI decodes |
| 500 | * only 10 bits, uses 0x100 - 0x3ff. Therefore, only |
| 501 | * 0x00 - 0xff can be used out of each 0x400 block of |
| 502 | * I/O space. |
| 503 | */ |
| 504 | if ((base & 0x300) != 0) { |
| 505 | base = (base & ~0x3ff) + 0x400; |
| 506 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 507 | /* |
| 508 | * Don't allow allocations in the VGA I/O range. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 509 | * PCI has special cases for that. |
| 510 | */ |
| 511 | else if ((base >= 0x3b0) && (base <= 0x3df)) { |
| 512 | base = 0x3e0; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | if ((round(base, resource->align) + resource->size - 1) <= |
| 517 | resource->limit) { |
| 518 | /* Base must be aligned. */ |
| 519 | base = round(base, resource->align); |
| 520 | resource->base = base; |
Kyösti Mälkki | 134b616 | 2015-03-23 19:58:23 +0200 | [diff] [blame] | 521 | resource->limit = resource->base + resource->size - 1; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 522 | resource->flags |= IORESOURCE_ASSIGNED; |
| 523 | resource->flags &= ~IORESOURCE_STORED; |
| 524 | base += resource->size; |
| 525 | } else { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 526 | printk(BIOS_ERR, "!! Resource didn't fit !!\n"); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 527 | printk(BIOS_ERR, " aligned base %llx size %llx " |
| 528 | "limit %llx\n", round(base, resource->align), |
| 529 | resource->size, resource->limit); |
| 530 | printk(BIOS_ERR, " %llx needs to be <= %llx " |
| 531 | "(limit)\n", (round(base, resource->align) + |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 532 | resource->size) - 1, resource->limit); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 533 | printk(BIOS_ERR, " %s%s %02lx * [0x%llx - 0x%llx]" |
| 534 | " %s\n", (resource->flags & IORESOURCE_ASSIGNED) |
| 535 | ? "Assigned: " : "", dev_path(dev), |
| 536 | resource->index, resource->base, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 537 | resource->base + resource->size - 1, |
Kyösti Mälkki | e6a9290f | 2015-03-23 19:37:38 +0200 | [diff] [blame] | 538 | resource2str(resource)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Kyösti Mälkki | e6a9290f | 2015-03-23 19:37:38 +0200 | [diff] [blame] | 541 | printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n", |
| 542 | dev_path(dev), resource->index, resource->base, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 543 | resource->size ? resource->base + resource->size - 1 : |
Kyösti Mälkki | e6a9290f | 2015-03-23 19:37:38 +0200 | [diff] [blame] | 544 | resource->base, resource2str(resource)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 545 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 546 | |
| 547 | /* |
| 548 | * A PCI bridge resource does not need to be a power of two size, but |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 549 | * it does have a minimum granularity. Round the size up to that |
| 550 | * minimum granularity so we know not to place something else at an |
| 551 | * address positively decoded by the bridge. |
| 552 | */ |
| 553 | |
| 554 | bridge->flags |= IORESOURCE_ASSIGNED; |
| 555 | |
Kyösti Mälkki | e6a9290f | 2015-03-23 19:37:38 +0200 | [diff] [blame] | 556 | printk(BIOS_SPEW, "%s %s: next_base: %llx size: %llx align: %d " |
| 557 | "gran: %d done\n", dev_path(bus->dev), |
| 558 | resource2str(bridge), base, bridge->size, bridge->align, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 559 | bridge->gran); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 560 | |
| 561 | /* For each child which is a bridge, allocate_resources. */ |
| 562 | for (dev = bus->children; dev; dev = dev->sibling) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 563 | struct resource *child_bridge; |
| 564 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 565 | if (!dev->link_list) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 566 | continue; |
| 567 | |
| 568 | /* Find the resources with matching type flags. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 569 | for (child_bridge = dev->resource_list; child_bridge; |
| 570 | child_bridge = child_bridge->next) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 571 | struct bus* link; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 572 | |
| 573 | if (!(child_bridge->flags & IORESOURCE_BRIDGE) || |
| 574 | (child_bridge->flags & type_mask) != type) |
| 575 | continue; |
| 576 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 577 | /* |
| 578 | * Split prefetchable memory if combined. Many domains |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 579 | * use the same address space for prefetchable memory |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 580 | * and non-prefetchable memory. Bridges below them need |
| 581 | * it separated. Add the PREFETCH flag to the type_mask |
| 582 | * and type. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 583 | */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 584 | link = dev->link_list; |
| 585 | while (link && link->link_num != |
| 586 | IOINDEX_LINK(child_bridge->index)) |
| 587 | link = link->next; |
| 588 | if (link == NULL) |
| 589 | printk(BIOS_ERR, "link %ld not found on %s\n", |
| 590 | IOINDEX_LINK(child_bridge->index), |
| 591 | dev_path(dev)); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 592 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 593 | allocate_resources(link, child_bridge, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 594 | type_mask | IORESOURCE_PREFETCH, |
| 595 | type | (child_bridge->flags & |
| 596 | IORESOURCE_PREFETCH)); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
Kyösti Mälkki | fdc0a90 | 2015-03-26 20:04:38 +0200 | [diff] [blame] | 601 | static int resource_is(struct resource *res, u32 type) |
| 602 | { |
| 603 | return (res->flags & IORESOURCE_TYPE_MASK) == type; |
| 604 | } |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 605 | |
| 606 | struct constraints { |
Kyösti Mälkki | fcbebb6 | 2015-03-17 06:42:54 +0200 | [diff] [blame] | 607 | struct resource io, mem; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 608 | }; |
| 609 | |
Elyes HAOUAS | e348066 | 2018-05-06 20:32:23 +0200 | [diff] [blame] | 610 | static struct resource *resource_limit(struct constraints *limits, |
| 611 | struct resource *res) |
Kyösti Mälkki | fdc0a90 | 2015-03-26 20:04:38 +0200 | [diff] [blame] | 612 | { |
| 613 | struct resource *lim = NULL; |
| 614 | |
| 615 | /* MEM, or I/O - skip any others. */ |
| 616 | if (resource_is(res, IORESOURCE_MEM)) |
| 617 | lim = &limits->mem; |
| 618 | else if (resource_is(res, IORESOURCE_IO)) |
| 619 | lim = &limits->io; |
| 620 | |
| 621 | return lim; |
| 622 | } |
| 623 | |
Elyes HAOUAS | e348066 | 2018-05-06 20:32:23 +0200 | [diff] [blame] | 624 | static void constrain_resources(const struct device *dev, |
| 625 | struct constraints* limits) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 626 | { |
Lubomir Rintel | 9ba8f7c | 2018-04-26 00:00:22 +0200 | [diff] [blame] | 627 | const struct device *child; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 628 | struct resource *res; |
| 629 | struct resource *lim; |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 630 | struct bus *link; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 631 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 632 | /* Constrain limits based on the fixed resources of this device. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 633 | for (res = dev->resource_list; res; res = res->next) { |
Patrick Georgi | 18c585b | 2009-08-28 12:48:02 +0000 | [diff] [blame] | 634 | if (!(res->flags & IORESOURCE_FIXED)) |
| 635 | continue; |
Myles Watson | ce9d864 | 2009-08-19 19:12:39 +0000 | [diff] [blame] | 636 | if (!res->size) { |
| 637 | /* It makes no sense to have 0-sized, fixed resources.*/ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 638 | printk(BIOS_ERR, "skipping %s@%lx fixed resource, " |
| 639 | "size=0!\n", dev_path(dev), res->index); |
Patrick Georgi | 6bd93f4 | 2009-08-19 17:29:41 +0000 | [diff] [blame] | 640 | continue; |
Myles Watson | ce9d864 | 2009-08-19 19:12:39 +0000 | [diff] [blame] | 641 | } |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 642 | |
Kyösti Mälkki | fdc0a90 | 2015-03-26 20:04:38 +0200 | [diff] [blame] | 643 | lim = resource_limit(limits, res); |
| 644 | if (!lim) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 645 | continue; |
| 646 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 647 | /* |
| 648 | * Is it a fixed resource outside the current known region? |
| 649 | * If so, we don't have to consider it - it will be handled |
| 650 | * correctly and doesn't affect current region's limits. |
| 651 | */ |
| 652 | if (((res->base + res->size -1) < lim->base) |
| 653 | || (res->base > lim->limit)) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 654 | continue; |
| 655 | |
Kyösti Mälkki | e6a9290f | 2015-03-23 19:37:38 +0200 | [diff] [blame] | 656 | printk(BIOS_SPEW, "%s: %s %02lx base %08llx limit %08llx %s (fixed)\n", |
| 657 | __func__, dev_path(dev), res->index, res->base, |
| 658 | res->base + res->size - 1, resource2str(res)); |
| 659 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 660 | /* |
| 661 | * Choose to be above or below fixed resources. This check is |
| 662 | * signed so that "negative" amounts of space are handled |
| 663 | * correctly. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 664 | */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 665 | if ((signed long long)(lim->limit - (res->base + res->size -1)) |
| 666 | > (signed long long)(res->base - lim->base)) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 667 | lim->base = res->base + res->size; |
| 668 | else |
| 669 | lim->limit = res->base -1; |
| 670 | } |
| 671 | |
| 672 | /* Descend into every enabled child and look for fixed resources. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 673 | for (link = dev->link_list; link; link = link->next) { |
| 674 | for (child = link->children; child; child = child->sibling) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 675 | if (child->enabled) |
| 676 | constrain_resources(child, limits); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Lubomir Rintel | 9ba8f7c | 2018-04-26 00:00:22 +0200 | [diff] [blame] | 681 | static void avoid_fixed_resources(const struct device *dev) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 682 | { |
| 683 | struct constraints limits; |
| 684 | struct resource *res; |
Kyösti Mälkki | fdc0a90 | 2015-03-26 20:04:38 +0200 | [diff] [blame] | 685 | struct resource *lim; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 686 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 687 | printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 688 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 689 | /* Initialize constraints to maximum size. */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 690 | limits.io.base = 0; |
| 691 | limits.io.limit = 0xffffffffffffffffULL; |
| 692 | limits.mem.base = 0; |
| 693 | limits.mem.limit = 0xffffffffffffffffULL; |
| 694 | |
| 695 | /* Constrain the limits to dev's initial resources. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 696 | for (res = dev->resource_list; res; res = res->next) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 697 | if ((res->flags & IORESOURCE_FIXED)) |
| 698 | continue; |
Patrick Georgi | 5161509 | 2012-03-11 19:31:03 +0100 | [diff] [blame] | 699 | printk(BIOS_SPEW, "%s:@%s %02lx limit %08llx\n", __func__, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 700 | dev_path(dev), res->index, res->limit); |
Kyösti Mälkki | fcbebb6 | 2015-03-17 06:42:54 +0200 | [diff] [blame] | 701 | |
Kyösti Mälkki | fdc0a90 | 2015-03-26 20:04:38 +0200 | [diff] [blame] | 702 | lim = resource_limit(&limits, res); |
| 703 | if (!lim) |
| 704 | continue; |
| 705 | |
| 706 | if (res->base > lim->base) |
| 707 | lim->base = res->base; |
Elyes HAOUAS | 1943f37 | 2018-05-04 16:30:39 +0200 | [diff] [blame] | 708 | if (res->limit < lim->limit) |
Kyösti Mälkki | fdc0a90 | 2015-03-26 20:04:38 +0200 | [diff] [blame] | 709 | lim->limit = res->limit; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /* Look through the tree for fixed resources and update the limits. */ |
| 713 | constrain_resources(dev, &limits); |
| 714 | |
| 715 | /* Update dev's resources with new limits. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 716 | for (res = dev->resource_list; res; res = res->next) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 717 | if ((res->flags & IORESOURCE_FIXED)) |
| 718 | continue; |
| 719 | |
Kyösti Mälkki | fdc0a90 | 2015-03-26 20:04:38 +0200 | [diff] [blame] | 720 | lim = resource_limit(&limits, res); |
| 721 | if (!lim) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 722 | continue; |
| 723 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 724 | /* Is the resource outside the limits? */ |
| 725 | if (lim->base > res->base) |
| 726 | res->base = lim->base; |
| 727 | if (res->limit > lim->limit) |
| 728 | res->limit = lim->limit; |
Kyösti Mälkki | e6a9290f | 2015-03-23 19:37:38 +0200 | [diff] [blame] | 729 | |
Kyösti Mälkki | 634899c | 2015-03-23 14:22:22 +0200 | [diff] [blame] | 730 | /* MEM resources need to start at the highest address manageable. */ |
| 731 | if (res->flags & IORESOURCE_MEM) |
| 732 | res->base = resource_max(res); |
| 733 | |
Kyösti Mälkki | e6a9290f | 2015-03-23 19:37:38 +0200 | [diff] [blame] | 734 | printk(BIOS_SPEW, "%s:@%s %02lx base %08llx limit %08llx\n", |
| 735 | __func__, dev_path(dev), res->index, res->base, res->limit); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 736 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 737 | } |
arch import user (historical) | dc81118 | 2005-07-06 17:16:09 +0000 | [diff] [blame] | 738 | |
Elyes HAOUAS | e348066 | 2018-05-06 20:32:23 +0200 | [diff] [blame] | 739 | struct device *vga_pri = NULL; |
Myles Watson | c7233e0 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 740 | static void set_vga_bridge_bits(void) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 741 | { |
Uwe Hermann | 312673c | 2009-10-27 21:49:33 +0000 | [diff] [blame] | 742 | /* |
Martin Roth | 63373ed | 2013-07-08 16:24:19 -0600 | [diff] [blame] | 743 | * FIXME: Modify set_vga_bridge() so it is less PCI-centric! |
Uwe Hermann | 312673c | 2009-10-27 21:49:33 +0000 | [diff] [blame] | 744 | * This function knows too much about PCI stuff, it should be just |
| 745 | * an iterator/visitor. |
| 746 | */ |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 747 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 748 | /* FIXME: Handle the VGA palette snooping. */ |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 749 | struct device *dev, *vga, *vga_onboard; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 750 | struct bus *bus; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 751 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 752 | bus = 0; |
| 753 | vga = 0; |
Yinghai Lu | 1f1085b | 2005-01-17 21:37:12 +0000 | [diff] [blame] | 754 | vga_onboard = 0; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 755 | |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 756 | dev = NULL; |
| 757 | while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 758 | if (!dev->enabled) |
| 759 | continue; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 760 | |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 761 | printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev)); |
Nico Huber | 061b905 | 2019-09-21 15:58:23 +0200 | [diff] [blame] | 762 | if (dev->bus->no_vga16) { |
| 763 | printk(BIOS_WARNING, |
| 764 | "A bridge on the path doesn't support 16-bit VGA decoding!"); |
| 765 | } |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame] | 766 | |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 767 | if (dev->on_mainboard) { |
| 768 | vga_onboard = dev; |
Kostr | 1f0d379 | 2012-10-06 13:27:58 +0400 | [diff] [blame] | 769 | } else { |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 770 | vga = dev; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 771 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 772 | |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 773 | /* It isn't safe to enable all VGA cards. */ |
| 774 | dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO); |
Patrick Georgi | 594473d | 2012-07-25 08:55:53 +0200 | [diff] [blame] | 775 | } |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame] | 776 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 777 | if (!vga) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 778 | vga = vga_onboard; |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 779 | |
Julius Werner | 5d1f9a0 | 2019-03-07 17:07:26 -0800 | [diff] [blame] | 780 | if (CONFIG(ONBOARD_VGA_IS_PRIMARY) && vga_onboard) |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 781 | vga = vga_onboard; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 782 | |
Patrick Georgi | 5869fa2 | 2012-07-20 12:29:33 +0200 | [diff] [blame] | 783 | /* If we prefer plugin VGA over chipset VGA, the chipset might |
| 784 | want to know. */ |
Julius Werner | 5d1f9a0 | 2019-03-07 17:07:26 -0800 | [diff] [blame] | 785 | if (!CONFIG(ONBOARD_VGA_IS_PRIMARY) && (vga != vga_onboard) && |
Patrick Georgi | 5869fa2 | 2012-07-20 12:29:33 +0200 | [diff] [blame] | 786 | vga_onboard && vga_onboard->ops && vga_onboard->ops->disable) { |
| 787 | printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n"); |
| 788 | vga_onboard->ops->disable(vga_onboard); |
| 789 | } |
| 790 | |
arch import user (historical) | dc81118 | 2005-07-06 17:16:09 +0000 | [diff] [blame] | 791 | if (vga) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 792 | /* VGA is first add-on card or the only onboard VGA. */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 793 | printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 794 | /* All legacy VGA cards have MEM & I/O space registers. */ |
Yinghai Lu | 1f1085b | 2005-01-17 21:37:12 +0000 | [diff] [blame] | 795 | vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO); |
| 796 | vga_pri = vga; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 797 | bus = vga->bus; |
| 798 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 799 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 800 | /* Now walk up the bridges setting the VGA enable. */ |
| 801 | while (bus) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 802 | printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 803 | dev_path(bus->dev)); |
Nico Huber | 061b905 | 2019-09-21 15:58:23 +0200 | [diff] [blame] | 804 | bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA | PCI_BRIDGE_CTL_VGA16; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 805 | bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 806 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 807 | } |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame] | 808 | |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 809 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 810 | * Assign the computed resources to the devices on the bus. |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 811 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 812 | * Use the device specific set_resources() method to store the computed |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 813 | * resources to hardware. For bridge devices, the set_resources() method |
| 814 | * has to recurse into every down stream buses. |
| 815 | * |
| 816 | * Mutual recursion: |
| 817 | * assign_resources() -> device_operation::set_resources() |
| 818 | * device_operation::set_resources() -> assign_resources() |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 819 | * |
| 820 | * @param bus Pointer to the structure for this bus. |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 821 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 822 | void assign_resources(struct bus *bus) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 823 | { |
| 824 | struct device *curdev; |
| 825 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 826 | printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 827 | dev_path(bus->dev), bus->secondary, bus->link_num); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 828 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 829 | for (curdev = bus->children; curdev; curdev = curdev->sibling) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 830 | if (!curdev->enabled || !curdev->resource_list) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 831 | continue; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 832 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 833 | if (!curdev->ops || !curdev->ops->set_resources) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 834 | printk(BIOS_ERR, "%s missing set_resources\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 835 | dev_path(curdev)); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 836 | continue; |
| 837 | } |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 838 | post_log_path(curdev); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 839 | curdev->ops->set_resources(curdev); |
| 840 | } |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 841 | post_log_clear(); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 842 | printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 843 | dev_path(bus->dev), bus->secondary, bus->link_num); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 846 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 847 | * Enable the resources for devices on a link. |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 848 | * |
| 849 | * Enable resources of the device by calling the device specific |
| 850 | * enable_resources() method. |
| 851 | * |
| 852 | * The parent's resources should be enabled first to avoid having enabling |
| 853 | * order problem. This is done by calling the parent's enable_resources() |
Martin Roth | 63373ed | 2013-07-08 16:24:19 -0600 | [diff] [blame] | 854 | * method before its children's enable_resources() methods. |
Li-Ta Lo | 9f0d0f9 | 2004-05-10 16:05:16 +0000 | [diff] [blame] | 855 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 856 | * @param link The link whose devices' resources are to be enabled. |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 857 | */ |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 858 | static void enable_resources(struct bus *link) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 859 | { |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 860 | struct device *dev; |
| 861 | struct bus *c_link; |
| 862 | |
| 863 | for (dev = link->children; dev; dev = dev->sibling) { |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 864 | if (dev->enabled && dev->ops && dev->ops->enable_resources) { |
| 865 | post_log_path(dev); |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 866 | dev->ops->enable_resources(dev); |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 867 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 868 | } |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 869 | |
| 870 | for (dev = link->children; dev; dev = dev->sibling) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 871 | for (c_link = dev->link_list; c_link; c_link = c_link->next) |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 872 | enable_resources(c_link); |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 873 | } |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 874 | post_log_clear(); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 877 | /** |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 878 | * Reset all of the devices on a bus and clear the bus's reset_needed flag. |
| 879 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 880 | * @param bus Pointer to the bus structure. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 881 | * @return 1 if the bus was successfully reset, 0 otherwise. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 882 | */ |
| 883 | int reset_bus(struct bus *bus) |
| 884 | { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 885 | if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 886 | bus->dev->ops->reset_bus(bus); |
| 887 | bus->reset_needed = 0; |
| 888 | return 1; |
| 889 | } |
| 890 | return 0; |
| 891 | } |
| 892 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 893 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 894 | * Scan for devices on a bus. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 895 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 896 | * If there are bridges on the bus, recursively scan the buses behind the |
| 897 | * bridges. If the setting up and tuning of the bus causes a reset to be |
| 898 | * required, reset the bus and scan it again. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 899 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 900 | * @param busdev Pointer to the bus device. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 901 | */ |
Kyösti Mälkki | 580e722 | 2015-03-19 21:04:23 +0200 | [diff] [blame] | 902 | static void scan_bus(struct device *busdev) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 903 | { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 904 | int do_scan_bus; |
Paul Menzel | 662380f | 2015-10-20 10:21:08 +0200 | [diff] [blame] | 905 | struct stopwatch sw; |
Kyösti Mälkki | e3d9d67 | 2019-12-01 12:27:44 +0200 | [diff] [blame^] | 906 | long scan_time; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 907 | |
Kyösti Mälkki | 2d2367c | 2015-02-20 21:28:31 +0200 | [diff] [blame] | 908 | if (!busdev->enabled) |
Kyösti Mälkki | 580e722 | 2015-03-19 21:04:23 +0200 | [diff] [blame] | 909 | return; |
Kyösti Mälkki | 2d2367c | 2015-02-20 21:28:31 +0200 | [diff] [blame] | 910 | |
Kyösti Mälkki | e3d9d67 | 2019-12-01 12:27:44 +0200 | [diff] [blame^] | 911 | printk(BIOS_DEBUG, "%s scanning...\n", dev_path(busdev)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 912 | |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 913 | post_log_path(busdev); |
| 914 | |
Kyösti Mälkki | e3d9d67 | 2019-12-01 12:27:44 +0200 | [diff] [blame^] | 915 | stopwatch_init(&sw); |
| 916 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 917 | do_scan_bus = 1; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 918 | while (do_scan_bus) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 919 | struct bus *link; |
Kyösti Mälkki | 580e722 | 2015-03-19 21:04:23 +0200 | [diff] [blame] | 920 | busdev->ops->scan_bus(busdev); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 921 | do_scan_bus = 0; |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 922 | for (link = busdev->link_list; link; link = link->next) { |
| 923 | if (link->reset_needed) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 924 | if (reset_bus(link)) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 925 | do_scan_bus = 1; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 926 | else |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 927 | busdev->bus->reset_needed = 1; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | } |
Paul Menzel | 662380f | 2015-10-20 10:21:08 +0200 | [diff] [blame] | 931 | |
Kyösti Mälkki | e3d9d67 | 2019-12-01 12:27:44 +0200 | [diff] [blame^] | 932 | scan_time = stopwatch_duration_msecs(&sw); |
| 933 | printk(BIOS_DEBUG, "%s: bus %s finished in %ld msecs\n", __func__, |
| 934 | dev_path(busdev), scan_time); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Kyösti Mälkki | 2d2367c | 2015-02-20 21:28:31 +0200 | [diff] [blame] | 937 | void scan_bridges(struct bus *bus) |
| 938 | { |
| 939 | struct device *child; |
Kyösti Mälkki | 2d2367c | 2015-02-20 21:28:31 +0200 | [diff] [blame] | 940 | |
| 941 | for (child = bus->children; child; child = child->sibling) { |
| 942 | if (!child->ops || !child->ops->scan_bus) |
| 943 | continue; |
Kyösti Mälkki | 580e722 | 2015-03-19 21:04:23 +0200 | [diff] [blame] | 944 | scan_bus(child); |
Kyösti Mälkki | 2d2367c | 2015-02-20 21:28:31 +0200 | [diff] [blame] | 945 | } |
Kyösti Mälkki | 2d2367c | 2015-02-20 21:28:31 +0200 | [diff] [blame] | 946 | } |
| 947 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 948 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 949 | * Determine the existence of devices and extend the device tree. |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 950 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 951 | * Most of the devices in the system are listed in the mainboard devicetree.cb |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 952 | * file. The device structures for these devices are generated at compile |
| 953 | * time by the config tool and are organized into the device tree. This |
| 954 | * function determines if the devices created at compile time actually exist |
| 955 | * in the physical system. |
| 956 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 957 | * For devices in the physical system but not listed in devicetree.cb, |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 958 | * the device structures have to be created at run time and attached to the |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 959 | * device tree. |
| 960 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 961 | * This function starts from the root device 'dev_root', scans the buses in |
| 962 | * the system recursively, and modifies the device tree according to the |
| 963 | * result of the probe. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 964 | * |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 965 | * This function has no idea how to scan and probe buses and devices at all. |
| 966 | * It depends on the bus/device specific scan_bus() method to do it. The |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 967 | * scan_bus() method also has to create the device structure and attach |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 968 | * it to the device tree. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 969 | */ |
| 970 | void dev_enumerate(void) |
| 971 | { |
| 972 | struct device *root; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 973 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 974 | printk(BIOS_INFO, "Enumerating buses...\n"); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 975 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 976 | root = &dev_root; |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 977 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 978 | show_all_devs(BIOS_SPEW, "Before device enumeration."); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 979 | printk(BIOS_SPEW, "Compare with tree...\n"); |
Kyösti Mälkki | 3d3c8c3 | 2016-08-15 10:04:21 +0300 | [diff] [blame] | 980 | show_devs_tree(root, BIOS_SPEW, 0); |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 981 | |
Stefan Reinauer | a675d49 | 2012-08-07 14:50:47 -0700 | [diff] [blame] | 982 | if (root->chip_ops && root->chip_ops->enable_dev) |
| 983 | root->chip_ops->enable_dev(root); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 984 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 985 | if (!root->ops || !root->ops->scan_bus) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 986 | printk(BIOS_ERR, "dev_root missing scan_bus operation"); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 987 | return; |
| 988 | } |
Kyösti Mälkki | 580e722 | 2015-03-19 21:04:23 +0200 | [diff] [blame] | 989 | scan_bus(root); |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 990 | post_log_clear(); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 991 | printk(BIOS_INFO, "done\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 994 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 995 | * Configure devices on the devices tree. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 996 | * |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 997 | * Starting at the root of the device tree, travel it recursively in two |
| 998 | * passes. In the first pass, we compute and allocate resources (ranges) |
Martin Roth | 63373ed | 2013-07-08 16:24:19 -0600 | [diff] [blame] | 999 | * required by each device. In the second pass, the resources ranges are |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 1000 | * relocated to their final position and stored to the hardware. |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 1001 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1002 | * I/O resources grow upward. MEM resources grow downward. |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 1003 | * |
| 1004 | * Since the assignment is hierarchical we set the values into the dev_root |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 1005 | * struct. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1006 | */ |
| 1007 | void dev_configure(void) |
| 1008 | { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1009 | struct resource *res; |
Lubomir Rintel | 9ba8f7c | 2018-04-26 00:00:22 +0200 | [diff] [blame] | 1010 | const struct device *root; |
| 1011 | const struct device *child; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1012 | |
Marc Jones | 80bd74c | 2012-02-21 17:44:35 +0100 | [diff] [blame] | 1013 | set_vga_bridge_bits(); |
Marc Jones | 80bd74c | 2012-02-21 17:44:35 +0100 | [diff] [blame] | 1014 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1015 | printk(BIOS_INFO, "Allocating resources...\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1016 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1017 | root = &dev_root; |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 1018 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1019 | /* |
| 1020 | * Each domain should create resources which contain the entire address |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1021 | * space for IO, MEM, and PREFMEM resources in the domain. The |
| 1022 | * allocation of device resources will be done from this address space. |
| 1023 | */ |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 1024 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1025 | /* Read the resources for the entire tree. */ |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 1026 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1027 | printk(BIOS_INFO, "Reading resources...\n"); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1028 | read_resources(root->link_list); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1029 | printk(BIOS_INFO, "Done reading resources.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1030 | |
Stefan Reinauer | 39e7229 | 2009-10-26 16:47:05 +0000 | [diff] [blame] | 1031 | print_resource_tree(root, BIOS_SPEW, "After reading."); |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 1032 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1033 | /* Compute resources for all domains. */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1034 | for (child = root->link_list->children; child; child = child->sibling) { |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 1035 | if (!(child->path.type == DEVICE_PATH_DOMAIN)) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1036 | continue; |
Duncan Laurie | 7ed3976 | 2013-07-09 10:46:52 -0700 | [diff] [blame] | 1037 | post_log_path(child); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 1038 | for (res = child->resource_list; res; res = res->next) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1039 | if (res->flags & IORESOURCE_FIXED) |
| 1040 | continue; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1041 | if (res->flags & IORESOURCE_MEM) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1042 | compute_resources(child->link_list, |
Kyösti Mälkki | fdc0a90 | 2015-03-26 20:04:38 +0200 | [diff] [blame] | 1043 | res, IORESOURCE_TYPE_MASK, IORESOURCE_MEM); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1044 | continue; |
| 1045 | } |
| 1046 | if (res->flags & IORESOURCE_IO) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1047 | compute_resources(child->link_list, |
Kyösti Mälkki | fdc0a90 | 2015-03-26 20:04:38 +0200 | [diff] [blame] | 1048 | res, IORESOURCE_TYPE_MASK, IORESOURCE_IO); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1049 | continue; |
| 1050 | } |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | /* For all domains. */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1055 | for (child = root->link_list->children; child; child=child->sibling) |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 1056 | if (child->path.type == DEVICE_PATH_DOMAIN) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1057 | avoid_fixed_resources(child); |
| 1058 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1059 | /* Store the computed resource allocations into device registers ... */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1060 | printk(BIOS_INFO, "Setting resources...\n"); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1061 | for (child = root->link_list->children; child; child = child->sibling) { |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 1062 | if (!(child->path.type == DEVICE_PATH_DOMAIN)) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1063 | continue; |
Duncan Laurie | 7ed3976 | 2013-07-09 10:46:52 -0700 | [diff] [blame] | 1064 | post_log_path(child); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 1065 | for (res = child->resource_list; res; res = res->next) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1066 | if (res->flags & IORESOURCE_FIXED) |
| 1067 | continue; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1068 | if (res->flags & IORESOURCE_MEM) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1069 | allocate_resources(child->link_list, |
Kyösti Mälkki | fdc0a90 | 2015-03-26 20:04:38 +0200 | [diff] [blame] | 1070 | res, IORESOURCE_TYPE_MASK, IORESOURCE_MEM); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1071 | continue; |
| 1072 | } |
| 1073 | if (res->flags & IORESOURCE_IO) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1074 | allocate_resources(child->link_list, |
Kyösti Mälkki | fdc0a90 | 2015-03-26 20:04:38 +0200 | [diff] [blame] | 1075 | res, IORESOURCE_TYPE_MASK, IORESOURCE_IO); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1076 | continue; |
| 1077 | } |
| 1078 | } |
| 1079 | } |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1080 | assign_resources(root->link_list); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1081 | printk(BIOS_INFO, "Done setting resources.\n"); |
Stefan Reinauer | 39e7229 | 2009-10-26 16:47:05 +0000 | [diff] [blame] | 1082 | print_resource_tree(root, BIOS_SPEW, "After assigning values."); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 1083 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1084 | printk(BIOS_INFO, "Done allocating resources.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1087 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1088 | * Enable devices on the device tree. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1089 | * |
| 1090 | * Starting at the root, walk the tree and enable all devices/bridges by |
| 1091 | * calling the device's enable_resources() method. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1092 | */ |
| 1093 | void dev_enable(void) |
| 1094 | { |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1095 | struct bus *link; |
| 1096 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1097 | printk(BIOS_INFO, "Enabling resources...\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1098 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1099 | /* Now enable everything. */ |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1100 | for (link = dev_root.link_list; link; link = link->next) |
| 1101 | enable_resources(link); |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1102 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1103 | printk(BIOS_INFO, "done.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1106 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1107 | * Initialize a specific device. |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1108 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1109 | * The parent should be initialized first to avoid having an ordering problem. |
Martin Roth | 63373ed | 2013-07-08 16:24:19 -0600 | [diff] [blame] | 1110 | * This is done by calling the parent's init() method before its children's |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1111 | * init() methods. |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1112 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1113 | * @param dev The device to be initialized. |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1114 | */ |
| 1115 | static void init_dev(struct device *dev) |
| 1116 | { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1117 | if (!dev->enabled) |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1118 | return; |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1119 | |
| 1120 | if (!dev->initialized && dev->ops && dev->ops->init) { |
Aaron Durbin | 46ba480 | 2014-09-23 16:34:40 -0500 | [diff] [blame] | 1121 | struct stopwatch sw; |
Kyösti Mälkki | e3d9d67 | 2019-12-01 12:27:44 +0200 | [diff] [blame^] | 1122 | long init_time; |
| 1123 | |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1124 | if (dev->path.type == DEVICE_PATH_I2C) { |
| 1125 | printk(BIOS_DEBUG, "smbus: %s[%d]->", |
| 1126 | dev_path(dev->bus->dev), dev->bus->link_num); |
| 1127 | } |
| 1128 | |
Kyösti Mälkki | e3d9d67 | 2019-12-01 12:27:44 +0200 | [diff] [blame^] | 1129 | printk(BIOS_DEBUG, "%s init\n", dev_path(dev)); |
| 1130 | |
| 1131 | stopwatch_init(&sw); |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1132 | dev->initialized = 1; |
| 1133 | dev->ops->init(dev); |
Kyösti Mälkki | e3d9d67 | 2019-12-01 12:27:44 +0200 | [diff] [blame^] | 1134 | |
| 1135 | init_time = stopwatch_duration_msecs(&sw); |
| 1136 | printk(BIOS_DEBUG, "%s init finished in %ld msecs\n", dev_path(dev), |
| 1137 | init_time); |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | static void init_link(struct bus *link) |
| 1142 | { |
| 1143 | struct device *dev; |
| 1144 | struct bus *c_link; |
| 1145 | |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 1146 | for (dev = link->children; dev; dev = dev->sibling) { |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 1147 | post_code(POST_BS_DEV_INIT); |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 1148 | post_log_path(dev); |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1149 | init_dev(dev); |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 1150 | } |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1151 | |
| 1152 | for (dev = link->children; dev; dev = dev->sibling) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1153 | for (c_link = dev->link_list; c_link; c_link = c_link->next) |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1154 | init_link(c_link); |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1159 | * Initialize all devices in the global device tree. |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1160 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1161 | * Starting at the root device, call the device's init() method to do |
| 1162 | * device-specific setup, then call each child's init() method. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1163 | */ |
| 1164 | void dev_initialize(void) |
| 1165 | { |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1166 | struct bus *link; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1167 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1168 | printk(BIOS_INFO, "Initializing devices...\n"); |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1169 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 1170 | #if CONFIG(ARCH_X86) |
Arthur Heymans | 8b7cd43 | 2019-10-26 20:31:41 +0200 | [diff] [blame] | 1171 | /* Ensure EBDA is prepared before Option ROMs. */ |
| 1172 | setup_default_ebda(); |
Duncan Laurie | b4aaaa7 | 2012-01-17 09:03:11 -0800 | [diff] [blame] | 1173 | #endif |
| 1174 | |
Myles Watson | 1bd3fb7 | 2010-08-16 16:25:23 +0000 | [diff] [blame] | 1175 | /* First call the mainboard init. */ |
| 1176 | init_dev(&dev_root); |
| 1177 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1178 | /* Now initialize everything. */ |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1179 | for (link = dev_root.link_list; link; link = link->next) |
| 1180 | init_link(link); |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 1181 | post_log_clear(); |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1182 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1183 | printk(BIOS_INFO, "Devices initialized\n"); |
Stefan Reinauer | 39e7229 | 2009-10-26 16:47:05 +0000 | [diff] [blame] | 1184 | show_all_devs(BIOS_SPEW, "After init."); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1185 | } |
Marc Jones | 2a58ecd | 2013-10-29 17:32:00 -0600 | [diff] [blame] | 1186 | |
| 1187 | /** |
| 1188 | * Finalize a specific device. |
| 1189 | * |
| 1190 | * The parent should be finalized first to avoid having an ordering problem. |
| 1191 | * This is done by calling the parent's final() method before its childrens' |
| 1192 | * final() methods. |
| 1193 | * |
| 1194 | * @param dev The device to be initialized. |
| 1195 | */ |
| 1196 | static void final_dev(struct device *dev) |
| 1197 | { |
| 1198 | if (!dev->enabled) |
| 1199 | return; |
| 1200 | |
| 1201 | if (dev->ops && dev->ops->final) { |
| 1202 | printk(BIOS_DEBUG, "%s final\n", dev_path(dev)); |
| 1203 | dev->ops->final(dev); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | static void final_link(struct bus *link) |
| 1208 | { |
| 1209 | struct device *dev; |
| 1210 | struct bus *c_link; |
| 1211 | |
| 1212 | for (dev = link->children; dev; dev = dev->sibling) |
| 1213 | final_dev(dev); |
| 1214 | |
| 1215 | for (dev = link->children; dev; dev = dev->sibling) { |
| 1216 | for (c_link = dev->link_list; c_link; c_link = c_link->next) |
| 1217 | final_link(c_link); |
| 1218 | } |
| 1219 | } |
| 1220 | /** |
| 1221 | * Finalize all devices in the global device tree. |
| 1222 | * |
| 1223 | * Starting at the root device, call the device's final() method to do |
| 1224 | * device-specific cleanup, then call each child's final() method. |
| 1225 | */ |
| 1226 | void dev_finalize(void) |
| 1227 | { |
| 1228 | struct bus *link; |
| 1229 | |
| 1230 | printk(BIOS_INFO, "Finalize devices...\n"); |
| 1231 | |
| 1232 | /* First call the mainboard finalize. */ |
| 1233 | final_dev(&dev_root); |
| 1234 | |
| 1235 | /* Now finalize everything. */ |
| 1236 | for (link = dev_root.link_list; link; link = link->next) |
| 1237 | final_link(link); |
| 1238 | |
| 1239 | printk(BIOS_INFO, "Devices finalized\n"); |
| 1240 | } |