[8c06905] | 1 | /*
|
---|
[832cbe7] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
[8c06905] | 3 | * Copyright (c) 2010 Lenka Trochtova
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
[4122410] | 31 | * @addtogroup pciintel
|
---|
[8c06905] | 32 | * @{
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | /** @file
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <assert.h>
|
---|
[690d2e7] | 39 | #include <byteorder.h>
|
---|
[8c06905] | 40 | #include <stdio.h>
|
---|
| 41 | #include <errno.h>
|
---|
[3e6a98c5] | 42 | #include <stdbool.h>
|
---|
[8c06905] | 43 | #include <fibril_synch.h>
|
---|
[c47e1a8] | 44 | #include <str.h>
|
---|
[8c06905] | 45 | #include <ctype.h>
|
---|
| 46 | #include <macros.h>
|
---|
[cd0684d] | 47 | #include <str_error.h>
|
---|
[8c06905] | 48 |
|
---|
[af6b5157] | 49 | #include <ddf/driver.h>
|
---|
[fc51296] | 50 | #include <ddf/log.h>
|
---|
[8c06905] | 51 | #include <ipc/dev_iface.h>
|
---|
[ebc9c2c] | 52 | #include <irc.h>
|
---|
[41b56084] | 53 | #include <ops/hw_res.h>
|
---|
[8c06905] | 54 | #include <device/hw_res.h>
|
---|
[6dbc500] | 55 | #include <ops/pio_window.h>
|
---|
| 56 | #include <device/pio_window.h>
|
---|
[8c06905] | 57 | #include <ddi.h>
|
---|
[99e6bfb] | 58 | #include <pci_dev_iface.h>
|
---|
[5e598e0] | 59 |
|
---|
[7acd787] | 60 | #include "ctl.h"
|
---|
[5e598e0] | 61 | #include "pci.h"
|
---|
[7acd787] | 62 | #include "pci_regs.h"
|
---|
[8c06905] | 63 |
|
---|
| 64 | #define NAME "pciintel"
|
---|
| 65 |
|
---|
[587478b] | 66 | #define CONF_ADDR_ENABLE (((unsigned)1) << 31)
|
---|
[663f41c4] | 67 | #define CONF_ADDR(bus, dev, fn, reg) \
|
---|
[92d5279] | 68 | ((bus << 16) | (dev << 11) | (fn << 8) | (reg & ~3))
|
---|
[5e598e0] | 69 |
|
---|
[68414f4a] | 70 | /** Obtain PCI function soft-state from DDF function node */
|
---|
[56fd7cf] | 71 | static pci_fun_t *pci_fun(ddf_fun_t *fnode)
|
---|
| 72 | {
|
---|
| 73 | return ddf_fun_data_get(fnode);
|
---|
| 74 | }
|
---|
[68414f4a] | 75 |
|
---|
| 76 | /** Obtain PCI bus soft-state from DDF device node */
|
---|
[7acd787] | 77 | pci_bus_t *pci_bus(ddf_dev_t *dnode)
|
---|
[56fd7cf] | 78 | {
|
---|
| 79 | return ddf_dev_data_get(dnode);
|
---|
| 80 | }
|
---|
[68414f4a] | 81 |
|
---|
| 82 | /** Obtain PCI bus soft-state from function soft-state */
|
---|
[56fd7cf] | 83 | static pci_bus_t *pci_bus_from_fun(pci_fun_t *fun)
|
---|
| 84 | {
|
---|
| 85 | return fun->busptr;
|
---|
| 86 | }
|
---|
[68414f4a] | 87 |
|
---|
[54de4836] | 88 | /** Max is 47, align to something nice. */
|
---|
| 89 | #define ID_MAX_STR_LEN 50
|
---|
| 90 |
|
---|
[83a2f43] | 91 | static hw_resource_list_t *pciintel_get_resources(ddf_fun_t *fnode)
|
---|
[3843ecb] | 92 | {
|
---|
[56fd7cf] | 93 | pci_fun_t *fun = pci_fun(fnode);
|
---|
[a35b458] | 94 |
|
---|
[68414f4a] | 95 | if (fun == NULL)
|
---|
[3843ecb] | 96 | return NULL;
|
---|
[68414f4a] | 97 | return &fun->hw_resources;
|
---|
[3843ecb] | 98 | }
|
---|
| 99 |
|
---|
[d5c1051] | 100 | static bool pciintel_fun_owns_interrupt(pci_fun_t *fun, int irq)
|
---|
[3843ecb] | 101 | {
|
---|
[cccd60c3] | 102 | size_t i;
|
---|
[d51838f] | 103 | hw_resource_list_t *res = &fun->hw_resources;
|
---|
[a35b458] | 104 |
|
---|
[cccd60c3] | 105 | for (i = 0; i < res->count; i++) {
|
---|
[d51838f] | 106 | if (res->resources[i].type == INTERRUPT &&
|
---|
| 107 | res->resources[i].res.interrupt.irq == irq) {
|
---|
| 108 | return true;
|
---|
[fb78ae72] | 109 | }
|
---|
| 110 | }
|
---|
[a35b458] | 111 |
|
---|
[d51838f] | 112 | return false;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[b7fd2a0] | 115 | static errno_t pciintel_enable_interrupt(ddf_fun_t *fnode, int irq)
|
---|
[d51838f] | 116 | {
|
---|
| 117 | pci_fun_t *fun = pci_fun(fnode);
|
---|
[a35b458] | 118 |
|
---|
[d51838f] | 119 | if (!pciintel_fun_owns_interrupt(fun, irq))
|
---|
| 120 | return EINVAL;
|
---|
| 121 |
|
---|
[cccd60c3] | 122 | return irc_enable_interrupt(irq);
|
---|
[3843ecb] | 123 | }
|
---|
| 124 |
|
---|
[b7fd2a0] | 125 | static errno_t pciintel_disable_interrupt(ddf_fun_t *fnode, int irq)
|
---|
[d51838f] | 126 | {
|
---|
| 127 | pci_fun_t *fun = pci_fun(fnode);
|
---|
[a35b458] | 128 |
|
---|
[d51838f] | 129 | if (!pciintel_fun_owns_interrupt(fun, irq))
|
---|
| 130 | return EINVAL;
|
---|
| 131 |
|
---|
| 132 | return irc_disable_interrupt(irq);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[b7fd2a0] | 135 | static errno_t pciintel_clear_interrupt(ddf_fun_t *fnode, int irq)
|
---|
[d51838f] | 136 | {
|
---|
| 137 | pci_fun_t *fun = pci_fun(fnode);
|
---|
[a35b458] | 138 |
|
---|
[d51838f] | 139 | if (!pciintel_fun_owns_interrupt(fun, irq))
|
---|
| 140 | return EINVAL;
|
---|
| 141 |
|
---|
| 142 | return irc_clear_interrupt(irq);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[832cbe7] | 145 | /** Handle legacy IO availability query from function.
|
---|
| 146 | *
|
---|
| 147 | * @param fnode Function performing the query
|
---|
| 148 | * @param rclaims Place to store the legacy IO claims bitmask
|
---|
| 149 | * @return EOK on success or an error code
|
---|
| 150 | */
|
---|
| 151 | static errno_t pciintel_query_legacy_io(ddf_fun_t *fnode,
|
---|
| 152 | hw_res_claims_t *rclaims)
|
---|
| 153 | {
|
---|
| 154 | pci_fun_t *fun = pci_fun(fnode);
|
---|
| 155 | pci_bus_t *bus = fun->busptr;
|
---|
| 156 | pci_fun_t *f;
|
---|
| 157 | hw_res_claims_t claims;
|
---|
| 158 |
|
---|
| 159 | /*
|
---|
| 160 | * We need to wait for enumeration to complete so that we give
|
---|
| 161 | * the PCI IDE driver a chance to claim the legacy ISA IDE
|
---|
| 162 | * ranges.
|
---|
| 163 | */
|
---|
| 164 | ddf_msg(LVL_DEBUG, "pciintel_query_legacy_io");
|
---|
| 165 |
|
---|
| 166 | fun->querying = true;
|
---|
| 167 |
|
---|
| 168 | fibril_mutex_lock(&bus->enum_done_lock);
|
---|
| 169 | while (!bus->enum_done)
|
---|
| 170 | fibril_condvar_wait(&bus->enum_done_cv, &bus->enum_done_lock);
|
---|
| 171 | fibril_mutex_unlock(&bus->enum_done_lock);
|
---|
| 172 |
|
---|
| 173 | ddf_msg(LVL_DEBUG, "Wait for PCI devices to stabilize");
|
---|
| 174 |
|
---|
| 175 | f = pci_fun_first(bus);
|
---|
| 176 | while (f != NULL) {
|
---|
| 177 | if (!f->querying)
|
---|
| 178 | ddf_fun_wait_stable(f->fnode);
|
---|
| 179 | f = pci_fun_next(f);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /* Devices are stable. Now we can determine if ISA IDE was claimed. */
|
---|
| 183 |
|
---|
| 184 | claims = 0;
|
---|
| 185 |
|
---|
| 186 | ddf_msg(LVL_DEBUG, "PCI devices stabilized, leg_ide_claimed=%d\n",
|
---|
| 187 | (int)bus->leg_ide_claimed);
|
---|
| 188 | if (bus->leg_ide_claimed != false) {
|
---|
| 189 | ddf_msg(LVL_NOTE, "Legacy IDE I/O ports claimed by PCI driver.");
|
---|
| 190 | claims |= hwc_isa_ide;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | fun->querying = false;
|
---|
| 194 | *rclaims = claims;
|
---|
| 195 | return EOK;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /** Handle legacy IO claim from function.
|
---|
| 199 | *
|
---|
| 200 | * @param fnode Function claiming the legacy I/O ports
|
---|
| 201 | * @param claims Bitmask of claimed I/O
|
---|
| 202 | * @return EOK on success or an error code
|
---|
| 203 | */
|
---|
| 204 | static errno_t pciintel_claim_legacy_io(ddf_fun_t *fnode,
|
---|
| 205 | hw_res_claims_t claims)
|
---|
| 206 | {
|
---|
| 207 | pci_fun_t *fun = pci_fun(fnode);
|
---|
| 208 | pci_bus_t *bus = fun->busptr;
|
---|
| 209 |
|
---|
| 210 | ddf_msg(LVL_DEBUG, "pciintel_claim_legacy_io() claims=%x", claims);
|
---|
| 211 | if ((claims & hwc_isa_ide) != 0)
|
---|
| 212 | bus->leg_ide_claimed = true;
|
---|
| 213 |
|
---|
| 214 | return EOK;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[6dbc500] | 217 | static pio_window_t *pciintel_get_pio_window(ddf_fun_t *fnode)
|
---|
| 218 | {
|
---|
| 219 | pci_fun_t *fun = pci_fun(fnode);
|
---|
[a35b458] | 220 |
|
---|
[6dbc500] | 221 | if (fun == NULL)
|
---|
| 222 | return NULL;
|
---|
| 223 | return &fun->pio_window;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[b7fd2a0] | 226 | static errno_t config_space_write_32(ddf_fun_t *fun, uint32_t address,
|
---|
[79ae36dd] | 227 | uint32_t data)
|
---|
[40a5d40] | 228 | {
|
---|
| 229 | if (address > 252)
|
---|
| 230 | return EINVAL;
|
---|
[56fd7cf] | 231 | pci_conf_write_32(pci_fun(fun), address, data);
|
---|
[40a5d40] | 232 | return EOK;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[b7fd2a0] | 235 | static errno_t config_space_write_16(
|
---|
[40a5d40] | 236 | ddf_fun_t *fun, uint32_t address, uint16_t data)
|
---|
[99e6bfb] | 237 | {
|
---|
| 238 | if (address > 254)
|
---|
| 239 | return EINVAL;
|
---|
[56fd7cf] | 240 | pci_conf_write_16(pci_fun(fun), address, data);
|
---|
[99e6bfb] | 241 | return EOK;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[b7fd2a0] | 244 | static errno_t config_space_write_8(
|
---|
[40a5d40] | 245 | ddf_fun_t *fun, uint32_t address, uint8_t data)
|
---|
| 246 | {
|
---|
| 247 | if (address > 255)
|
---|
| 248 | return EINVAL;
|
---|
[56fd7cf] | 249 | pci_conf_write_8(pci_fun(fun), address, data);
|
---|
[40a5d40] | 250 | return EOK;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[b7fd2a0] | 253 | static errno_t config_space_read_32(
|
---|
[40a5d40] | 254 | ddf_fun_t *fun, uint32_t address, uint32_t *data)
|
---|
| 255 | {
|
---|
| 256 | if (address > 252)
|
---|
| 257 | return EINVAL;
|
---|
[56fd7cf] | 258 | *data = pci_conf_read_32(pci_fun(fun), address);
|
---|
[40a5d40] | 259 | return EOK;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[b7fd2a0] | 262 | static errno_t config_space_read_16(
|
---|
[40a5d40] | 263 | ddf_fun_t *fun, uint32_t address, uint16_t *data)
|
---|
| 264 | {
|
---|
| 265 | if (address > 254)
|
---|
| 266 | return EINVAL;
|
---|
[56fd7cf] | 267 | *data = pci_conf_read_16(pci_fun(fun), address);
|
---|
[40a5d40] | 268 | return EOK;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[b7fd2a0] | 271 | static errno_t config_space_read_8(
|
---|
[40a5d40] | 272 | ddf_fun_t *fun, uint32_t address, uint8_t *data)
|
---|
| 273 | {
|
---|
| 274 | if (address > 255)
|
---|
| 275 | return EINVAL;
|
---|
[56fd7cf] | 276 | *data = pci_conf_read_8(pci_fun(fun), address);
|
---|
[40a5d40] | 277 | return EOK;
|
---|
| 278 | }
|
---|
[99e6bfb] | 279 |
|
---|
[68414f4a] | 280 | static hw_res_ops_t pciintel_hw_res_ops = {
|
---|
[d9cf684a] | 281 | .get_resource_list = &pciintel_get_resources,
|
---|
| 282 | .enable_interrupt = &pciintel_enable_interrupt,
|
---|
[d51838f] | 283 | .disable_interrupt = &pciintel_disable_interrupt,
|
---|
| 284 | .clear_interrupt = &pciintel_clear_interrupt,
|
---|
[832cbe7] | 285 | .query_legacy_io = &pciintel_query_legacy_io,
|
---|
| 286 | .claim_legacy_io = &pciintel_claim_legacy_io
|
---|
[3843ecb] | 287 | };
|
---|
| 288 |
|
---|
[6dbc500] | 289 | static pio_window_ops_t pciintel_pio_window_ops = {
|
---|
| 290 | .get_pio_window = &pciintel_get_pio_window
|
---|
| 291 | };
|
---|
| 292 |
|
---|
[99e6bfb] | 293 | static pci_dev_iface_t pci_dev_ops = {
|
---|
[99e8fb7b] | 294 | .config_space_read_8 = &config_space_read_8,
|
---|
| 295 | .config_space_read_16 = &config_space_read_16,
|
---|
| 296 | .config_space_read_32 = &config_space_read_32,
|
---|
| 297 | .config_space_write_8 = &config_space_write_8,
|
---|
| 298 | .config_space_write_16 = &config_space_write_16,
|
---|
| 299 | .config_space_write_32 = &config_space_write_32
|
---|
[99e6bfb] | 300 | };
|
---|
| 301 |
|
---|
| 302 | static ddf_dev_ops_t pci_fun_ops = {
|
---|
| 303 | .interfaces[HW_RES_DEV_IFACE] = &pciintel_hw_res_ops,
|
---|
[6dbc500] | 304 | .interfaces[PIO_WINDOW_DEV_IFACE] = &pciintel_pio_window_ops,
|
---|
[99e6bfb] | 305 | .interfaces[PCI_DEV_IFACE] = &pci_dev_ops
|
---|
| 306 | };
|
---|
[3843ecb] | 307 |
|
---|
[b7fd2a0] | 308 | static errno_t pci_dev_add(ddf_dev_t *);
|
---|
| 309 | static errno_t pci_fun_online(ddf_fun_t *);
|
---|
| 310 | static errno_t pci_fun_offline(ddf_fun_t *);
|
---|
[3843ecb] | 311 |
|
---|
[68414f4a] | 312 | /** PCI bus driver standard operations */
|
---|
[8c06905] | 313 | static driver_ops_t pci_ops = {
|
---|
[0c0f823b] | 314 | .dev_add = &pci_dev_add,
|
---|
[f278930] | 315 | .fun_online = &pci_fun_online,
|
---|
| 316 | .fun_offline = &pci_fun_offline,
|
---|
[8c06905] | 317 | };
|
---|
| 318 |
|
---|
[68414f4a] | 319 | /** PCI bus driver structure */
|
---|
[8c06905] | 320 | static driver_t pci_driver = {
|
---|
| 321 | .name = NAME,
|
---|
| 322 | .driver_ops = &pci_ops
|
---|
| 323 | };
|
---|
| 324 |
|
---|
[68414f4a] | 325 | static void pci_conf_read(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
|
---|
[5e598e0] | 326 | {
|
---|
[82721f5] | 327 | const uint32_t conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
|
---|
[56fd7cf] | 328 | pci_bus_t *bus = pci_bus_from_fun(fun);
|
---|
[82721f5] | 329 | uint32_t val;
|
---|
[a35b458] | 330 |
|
---|
[68414f4a] | 331 | fibril_mutex_lock(&bus->conf_mutex);
|
---|
[82721f5] | 332 |
|
---|
[92d5279] | 333 | if (bus->conf_addr_reg) {
|
---|
| 334 | pio_write_32(bus->conf_addr_reg,
|
---|
| 335 | host2uint32_t_le(CONF_ADDR_ENABLE | conf_addr));
|
---|
| 336 | /*
|
---|
| 337 | * Always read full 32-bits from the PCI conf_data_port
|
---|
| 338 | * register and get the desired portion of it afterwards. Some
|
---|
| 339 | * architectures do not support shorter PIO reads offset from
|
---|
| 340 | * this register.
|
---|
[ae7d03c] | 341 | */
|
---|
[92d5279] | 342 | val = uint32_t_le2host(pio_read_32(bus->conf_data_reg));
|
---|
| 343 | } else {
|
---|
| 344 | val = uint32_t_le2host(pio_read_32(
|
---|
| 345 | &bus->conf_space[conf_addr / sizeof(ioport32_t)]));
|
---|
| 346 | }
|
---|
[82721f5] | 347 |
|
---|
[5e598e0] | 348 | switch (len) {
|
---|
[663f41c4] | 349 | case 1:
|
---|
[82721f5] | 350 | *buf = (uint8_t) (val >> ((reg & 3) * 8));
|
---|
[663f41c4] | 351 | break;
|
---|
| 352 | case 2:
|
---|
[82721f5] | 353 | *((uint16_t *) buf) = (uint16_t) (val >> ((reg & 3)) * 8);
|
---|
[663f41c4] | 354 | break;
|
---|
| 355 | case 4:
|
---|
[82721f5] | 356 | *((uint32_t *) buf) = (uint32_t) val;
|
---|
[663f41c4] | 357 | break;
|
---|
[5e598e0] | 358 | }
|
---|
[a35b458] | 359 |
|
---|
[68414f4a] | 360 | fibril_mutex_unlock(&bus->conf_mutex);
|
---|
[5e598e0] | 361 | }
|
---|
| 362 |
|
---|
[68414f4a] | 363 | static void pci_conf_write(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
|
---|
[d1fc8f0] | 364 | {
|
---|
[82721f5] | 365 | const uint32_t conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
|
---|
[56fd7cf] | 366 | pci_bus_t *bus = pci_bus_from_fun(fun);
|
---|
[0464967] | 367 | uint32_t val = 0;
|
---|
[a35b458] | 368 |
|
---|
[68414f4a] | 369 | fibril_mutex_lock(&bus->conf_mutex);
|
---|
[82721f5] | 370 |
|
---|
| 371 | /*
|
---|
| 372 | * Prepare to write full 32-bits to the PCI conf_data_port register.
|
---|
| 373 | * Some architectures do not support shorter PIO writes offset from this
|
---|
| 374 | * register.
|
---|
[ae7d03c] | 375 | */
|
---|
[82721f5] | 376 |
|
---|
| 377 | if (len < 4) {
|
---|
| 378 | /*
|
---|
[ae7d03c] | 379 | * We have fewer than full 32-bits, so we need to read the
|
---|
| 380 | * missing bits first.
|
---|
| 381 | */
|
---|
[92d5279] | 382 | if (bus->conf_addr_reg) {
|
---|
| 383 | pio_write_32(bus->conf_addr_reg,
|
---|
| 384 | host2uint32_t_le(CONF_ADDR_ENABLE | conf_addr));
|
---|
| 385 | val = uint32_t_le2host(pio_read_32(bus->conf_data_reg));
|
---|
| 386 | } else {
|
---|
| 387 | val = uint32_t_le2host(pio_read_32(
|
---|
| 388 | &bus->conf_space[conf_addr / sizeof(ioport32_t)]));
|
---|
| 389 | }
|
---|
[82721f5] | 390 | }
|
---|
[a35b458] | 391 |
|
---|
[d1fc8f0] | 392 | switch (len) {
|
---|
[663f41c4] | 393 | case 1:
|
---|
[82721f5] | 394 | val &= ~(0xffU << ((reg & 3) * 8));
|
---|
| 395 | val |= *buf << ((reg & 3) * 8);
|
---|
[663f41c4] | 396 | break;
|
---|
| 397 | case 2:
|
---|
[82721f5] | 398 | val &= ~(0xffffU << ((reg & 3) * 8));
|
---|
| 399 | val |= *((uint16_t *) buf) << ((reg & 3) * 8);
|
---|
[663f41c4] | 400 | break;
|
---|
| 401 | case 4:
|
---|
[82721f5] | 402 | val = *((uint32_t *) buf);
|
---|
[663f41c4] | 403 | break;
|
---|
[d1fc8f0] | 404 | }
|
---|
[82721f5] | 405 |
|
---|
[92d5279] | 406 | if (bus->conf_addr_reg) {
|
---|
| 407 | pio_write_32(bus->conf_addr_reg,
|
---|
| 408 | host2uint32_t_le(CONF_ADDR_ENABLE | conf_addr));
|
---|
| 409 | pio_write_32(bus->conf_data_reg, host2uint32_t_le(val));
|
---|
| 410 | } else {
|
---|
| 411 | pio_write_32(&bus->conf_space[conf_addr / sizeof(ioport32_t)],
|
---|
| 412 | host2uint32_t_le(val));
|
---|
| 413 | }
|
---|
[a35b458] | 414 |
|
---|
[68414f4a] | 415 | fibril_mutex_unlock(&bus->conf_mutex);
|
---|
[d1fc8f0] | 416 | }
|
---|
| 417 |
|
---|
[68414f4a] | 418 | uint8_t pci_conf_read_8(pci_fun_t *fun, int reg)
|
---|
[5e598e0] | 419 | {
|
---|
| 420 | uint8_t res;
|
---|
[8b1e15ac] | 421 | pci_conf_read(fun, reg, &res, 1);
|
---|
[5e598e0] | 422 | return res;
|
---|
| 423 | }
|
---|
| 424 |
|
---|
[68414f4a] | 425 | uint16_t pci_conf_read_16(pci_fun_t *fun, int reg)
|
---|
[5e598e0] | 426 | {
|
---|
| 427 | uint16_t res;
|
---|
[8b1e15ac] | 428 | pci_conf_read(fun, reg, (uint8_t *) &res, 2);
|
---|
[5e598e0] | 429 | return res;
|
---|
| 430 | }
|
---|
| 431 |
|
---|
[68414f4a] | 432 | uint32_t pci_conf_read_32(pci_fun_t *fun, int reg)
|
---|
[5e598e0] | 433 | {
|
---|
| 434 | uint32_t res;
|
---|
[8b1e15ac] | 435 | pci_conf_read(fun, reg, (uint8_t *) &res, 4);
|
---|
[663f41c4] | 436 | return res;
|
---|
[5e598e0] | 437 | }
|
---|
| 438 |
|
---|
[68414f4a] | 439 | void pci_conf_write_8(pci_fun_t *fun, int reg, uint8_t val)
|
---|
[d1fc8f0] | 440 | {
|
---|
[8b1e15ac] | 441 | pci_conf_write(fun, reg, (uint8_t *) &val, 1);
|
---|
[d1fc8f0] | 442 | }
|
---|
| 443 |
|
---|
[68414f4a] | 444 | void pci_conf_write_16(pci_fun_t *fun, int reg, uint16_t val)
|
---|
[d1fc8f0] | 445 | {
|
---|
[8b1e15ac] | 446 | pci_conf_write(fun, reg, (uint8_t *) &val, 2);
|
---|
[d1fc8f0] | 447 | }
|
---|
| 448 |
|
---|
[68414f4a] | 449 | void pci_conf_write_32(pci_fun_t *fun, int reg, uint32_t val)
|
---|
[d1fc8f0] | 450 | {
|
---|
[8b1e15ac] | 451 | pci_conf_write(fun, reg, (uint8_t *) &val, 4);
|
---|
[d1fc8f0] | 452 | }
|
---|
| 453 |
|
---|
[68414f4a] | 454 | void pci_fun_create_match_ids(pci_fun_t *fun)
|
---|
[89ce401a] | 455 | {
|
---|
[b7fd2a0] | 456 | errno_t rc;
|
---|
[d5c1051] | 457 | int ret;
|
---|
[1d53a78] | 458 | char match_id_str[ID_MAX_STR_LEN];
|
---|
[cd0684d] | 459 |
|
---|
[1d53a78] | 460 | /* Vendor ID & Device ID, length(incl \0) 22 */
|
---|
[d5c1051] | 461 | ret = snprintf(match_id_str, ID_MAX_STR_LEN, "pci/ven=%04"
|
---|
[c90aed4] | 462 | PRIx16 "&dev=%04" PRIx16, fun->vendor_id, fun->device_id);
|
---|
[d5c1051] | 463 | if (ret < 0) {
|
---|
| 464 | ddf_msg(LVL_ERROR, "Failed creating match ID str");
|
---|
[8304889] | 465 | }
|
---|
| 466 |
|
---|
[cd0684d] | 467 | rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 90);
|
---|
| 468 | if (rc != EOK) {
|
---|
[1d53a78] | 469 | ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | /* Class, subclass, prog IF, revision, length(incl \0) 47 */
|
---|
[d5c1051] | 473 | ret = snprintf(match_id_str, ID_MAX_STR_LEN,
|
---|
[1d53a78] | 474 | "pci/class=%02x&subclass=%02x&progif=%02x&revision=%02x",
|
---|
| 475 | fun->class_code, fun->subclass_code, fun->prog_if, fun->revision);
|
---|
[d5c1051] | 476 | if (ret < 0) {
|
---|
| 477 | ddf_msg(LVL_ERROR, "Failed creating match ID str");
|
---|
[8304889] | 478 | }
|
---|
[1d53a78] | 479 |
|
---|
| 480 | rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 70);
|
---|
| 481 | if (rc != EOK) {
|
---|
| 482 | ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | /* Class, subclass, prog IF, length(incl \0) 35 */
|
---|
[d5c1051] | 486 | ret = snprintf(match_id_str, ID_MAX_STR_LEN,
|
---|
[1d53a78] | 487 | "pci/class=%02x&subclass=%02x&progif=%02x",
|
---|
| 488 | fun->class_code, fun->subclass_code, fun->prog_if);
|
---|
[d5c1051] | 489 | if (ret < 0) {
|
---|
| 490 | ddf_msg(LVL_ERROR, "Failed creating match ID str");
|
---|
[1d53a78] | 491 | }
|
---|
| 492 |
|
---|
| 493 | rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 60);
|
---|
| 494 | if (rc != EOK) {
|
---|
| 495 | ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
|
---|
| 496 | }
|
---|
| 497 |
|
---|
| 498 | /* Class, subclass, length(incl \0) 25 */
|
---|
[d5c1051] | 499 | ret = snprintf(match_id_str, ID_MAX_STR_LEN,
|
---|
[1d53a78] | 500 | "pci/class=%02x&subclass=%02x",
|
---|
| 501 | fun->class_code, fun->subclass_code);
|
---|
[d5c1051] | 502 | if (ret < 0) {
|
---|
| 503 | ddf_msg(LVL_ERROR, "Failed creating match ID str");
|
---|
[1d53a78] | 504 | }
|
---|
| 505 |
|
---|
| 506 | rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 50);
|
---|
| 507 | if (rc != EOK) {
|
---|
| 508 | ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
|
---|
| 509 | }
|
---|
| 510 |
|
---|
| 511 | /* Class, length(incl \0) 13 */
|
---|
[d5c1051] | 512 | ret = snprintf(match_id_str, ID_MAX_STR_LEN, "pci/class=%02x",
|
---|
[1d53a78] | 513 | fun->class_code);
|
---|
[d5c1051] | 514 | if (ret < 0) {
|
---|
| 515 | ddf_msg(LVL_ERROR, "Failed creating match ID str");
|
---|
[1d53a78] | 516 | }
|
---|
| 517 |
|
---|
| 518 | rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 40);
|
---|
| 519 | if (rc != EOK) {
|
---|
| 520 | ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | /* TODO add subsys ids, but those exist only in header type 0 */
|
---|
[89ce401a] | 524 | }
|
---|
| 525 |
|
---|
[7acd787] | 526 | /** Get first PCI function.
|
---|
| 527 | *
|
---|
| 528 | * @param bus PCI bus
|
---|
| 529 | * @return First PCI function on @a bus or @c NULL if there is none
|
---|
| 530 | */
|
---|
| 531 | pci_fun_t *pci_fun_first(pci_bus_t *bus)
|
---|
| 532 | {
|
---|
| 533 | link_t *link;
|
---|
| 534 |
|
---|
| 535 | link = list_first(&bus->funs);
|
---|
| 536 | if (link == NULL)
|
---|
| 537 | return NULL;
|
---|
| 538 |
|
---|
| 539 | return list_get_instance(link, pci_fun_t, lfuns);
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | /** Get next PCI function.
|
---|
| 543 | *
|
---|
| 544 | * @param cur Current function
|
---|
| 545 | * @return Next PCI function on the same bus or @c NULL if there is none
|
---|
| 546 | */
|
---|
| 547 | pci_fun_t *pci_fun_next(pci_fun_t *cur)
|
---|
| 548 | {
|
---|
| 549 | link_t *link;
|
---|
| 550 |
|
---|
| 551 | link = list_next(&cur->lfuns, &cur->busptr->funs);
|
---|
| 552 | if (link == NULL)
|
---|
| 553 | return NULL;
|
---|
| 554 |
|
---|
| 555 | return list_get_instance(link, pci_fun_t, lfuns);
|
---|
| 556 | }
|
---|
| 557 |
|
---|
[68414f4a] | 558 | void pci_add_range(pci_fun_t *fun, uint64_t range_addr, size_t range_size,
|
---|
| 559 | bool io)
|
---|
[d1fc8f0] | 560 | {
|
---|
[68414f4a] | 561 | hw_resource_list_t *hw_res_list = &fun->hw_resources;
|
---|
[3a5909f] | 562 | hw_resource_t *hw_resources = hw_res_list->resources;
|
---|
[663f41c4] | 563 | size_t count = hw_res_list->count;
|
---|
[a35b458] | 564 |
|
---|
[8304889] | 565 | assert(hw_resources != NULL);
|
---|
[3a5909f] | 566 | assert(count < PCI_MAX_HW_RES);
|
---|
[a35b458] | 567 |
|
---|
[3a5909f] | 568 | if (io) {
|
---|
| 569 | hw_resources[count].type = IO_RANGE;
|
---|
| 570 | hw_resources[count].res.io_range.address = range_addr;
|
---|
[663f41c4] | 571 | hw_resources[count].res.io_range.size = range_size;
|
---|
[9e470c0] | 572 | hw_resources[count].res.io_range.relative = true;
|
---|
[663f41c4] | 573 | hw_resources[count].res.io_range.endianness = LITTLE_ENDIAN;
|
---|
[3a5909f] | 574 | } else {
|
---|
| 575 | hw_resources[count].type = MEM_RANGE;
|
---|
| 576 | hw_resources[count].res.mem_range.address = range_addr;
|
---|
[663f41c4] | 577 | hw_resources[count].res.mem_range.size = range_size;
|
---|
[9e470c0] | 578 | hw_resources[count].res.mem_range.relative = false;
|
---|
[3a5909f] | 579 | hw_resources[count].res.mem_range.endianness = LITTLE_ENDIAN;
|
---|
| 580 | }
|
---|
[a35b458] | 581 |
|
---|
[663f41c4] | 582 | hw_res_list->count++;
|
---|
[d1fc8f0] | 583 | }
|
---|
| 584 |
|
---|
[663f41c4] | 585 | /** Read the base address register (BAR) of the device and if it contains valid
|
---|
| 586 | * address add it to the devices hw resource list.
|
---|
| 587 | *
|
---|
[68414f4a] | 588 | * @param fun PCI function
|
---|
[663f41c4] | 589 | * @param addr The address of the BAR in the PCI configuration address space of
|
---|
[68414f4a] | 590 | * the device
|
---|
| 591 | * @return The addr the address of the BAR which should be read next
|
---|
[d1fc8f0] | 592 | */
|
---|
[68414f4a] | 593 | int pci_read_bar(pci_fun_t *fun, int addr)
|
---|
[bab6388] | 594 | {
|
---|
[663f41c4] | 595 | /* Value of the BAR */
|
---|
[e8d6ce2] | 596 | uint32_t val;
|
---|
| 597 | uint32_t bar;
|
---|
| 598 | uint32_t mask;
|
---|
| 599 |
|
---|
[663f41c4] | 600 | /* IO space address */
|
---|
[d1fc8f0] | 601 | bool io;
|
---|
[663f41c4] | 602 | /* 64-bit wide address */
|
---|
[d93aafed] | 603 | bool addrw64;
|
---|
[a35b458] | 604 |
|
---|
[663f41c4] | 605 | /* Size of the io or memory range specified by the BAR */
|
---|
[d1fc8f0] | 606 | size_t range_size;
|
---|
[663f41c4] | 607 | /* Beginning of the io or memory range specified by the BAR */
|
---|
[d1fc8f0] | 608 | uint64_t range_addr;
|
---|
[a35b458] | 609 |
|
---|
[663f41c4] | 610 | /* Get the value of the BAR. */
|
---|
[8b1e15ac] | 611 | val = pci_conf_read_32(fun, addr);
|
---|
[ad6857c] | 612 |
|
---|
| 613 | #define IO_MASK (~0x3)
|
---|
| 614 | #define MEM_MASK (~0xf)
|
---|
[a35b458] | 615 |
|
---|
[c81132d] | 616 | io = (val & 1) != 0;
|
---|
[d1fc8f0] | 617 | if (io) {
|
---|
[d93aafed] | 618 | addrw64 = false;
|
---|
[ad6857c] | 619 | mask = IO_MASK;
|
---|
[d1fc8f0] | 620 | } else {
|
---|
[ad6857c] | 621 | mask = MEM_MASK;
|
---|
[d1fc8f0] | 622 | switch ((val >> 1) & 3) {
|
---|
| 623 | case 0:
|
---|
[d93aafed] | 624 | addrw64 = false;
|
---|
[d1fc8f0] | 625 | break;
|
---|
| 626 | case 2:
|
---|
[d93aafed] | 627 | addrw64 = true;
|
---|
[d1fc8f0] | 628 | break;
|
---|
| 629 | default:
|
---|
[663f41c4] | 630 | /* reserved, go to the next BAR */
|
---|
| 631 | return addr + 4;
|
---|
[d1fc8f0] | 632 | }
|
---|
| 633 | }
|
---|
[a35b458] | 634 |
|
---|
[663f41c4] | 635 | /* Get the address mask. */
|
---|
[8b1e15ac] | 636 | pci_conf_write_32(fun, addr, 0xffffffff);
|
---|
[e8d6ce2] | 637 | bar = pci_conf_read_32(fun, addr);
|
---|
| 638 |
|
---|
| 639 | /*
|
---|
[ae7d03c] | 640 | * Unimplemented BARs read back as all 0's.
|
---|
| 641 | */
|
---|
[e8d6ce2] | 642 | if (!bar)
|
---|
| 643 | return addr + (addrw64 ? 8 : 4);
|
---|
| 644 |
|
---|
[1b20da0] | 645 | mask &= bar;
|
---|
[e8d6ce2] | 646 |
|
---|
[663f41c4] | 647 | /* Restore the original value. */
|
---|
[8b1e15ac] | 648 | pci_conf_write_32(fun, addr, val);
|
---|
| 649 | val = pci_conf_read_32(fun, addr);
|
---|
[a35b458] | 650 |
|
---|
[3a5909f] | 651 | range_size = pci_bar_mask_to_size(mask);
|
---|
[a35b458] | 652 |
|
---|
[d93aafed] | 653 | if (addrw64) {
|
---|
[8b1e15ac] | 654 | range_addr = ((uint64_t)pci_conf_read_32(fun, addr + 4) << 32) |
|
---|
[663f41c4] | 655 | (val & 0xfffffff0);
|
---|
[d1fc8f0] | 656 | } else {
|
---|
| 657 | range_addr = (val & 0xfffffff0);
|
---|
[663f41c4] | 658 | }
|
---|
[a35b458] | 659 |
|
---|
[d93aafed] | 660 | if (range_addr != 0) {
|
---|
[fc51296] | 661 | ddf_msg(LVL_DEBUG, "Function %s : address = %" PRIx64
|
---|
[56fd7cf] | 662 | ", size = %x", ddf_fun_get_name(fun->fnode), range_addr,
|
---|
[fc51296] | 663 | (unsigned int) range_size);
|
---|
[d1fc8f0] | 664 | }
|
---|
[a35b458] | 665 |
|
---|
[8b1e15ac] | 666 | pci_add_range(fun, range_addr, range_size, io);
|
---|
[a35b458] | 667 |
|
---|
[d93aafed] | 668 | if (addrw64)
|
---|
[d1fc8f0] | 669 | return addr + 8;
|
---|
[a35b458] | 670 |
|
---|
[663f41c4] | 671 | return addr + 4;
|
---|
[d1fc8f0] | 672 | }
|
---|
| 673 |
|
---|
[68414f4a] | 674 | void pci_add_interrupt(pci_fun_t *fun, int irq)
|
---|
[d1fc8f0] | 675 | {
|
---|
[68414f4a] | 676 | hw_resource_list_t *hw_res_list = &fun->hw_resources;
|
---|
[663f41c4] | 677 | hw_resource_t *hw_resources = hw_res_list->resources;
|
---|
| 678 | size_t count = hw_res_list->count;
|
---|
[a35b458] | 679 |
|
---|
[3a5909f] | 680 | assert(NULL != hw_resources);
|
---|
| 681 | assert(count < PCI_MAX_HW_RES);
|
---|
[a35b458] | 682 |
|
---|
[3a5909f] | 683 | hw_resources[count].type = INTERRUPT;
|
---|
| 684 | hw_resources[count].res.interrupt.irq = irq;
|
---|
[a35b458] | 685 |
|
---|
[663f41c4] | 686 | hw_res_list->count++;
|
---|
[a35b458] | 687 |
|
---|
[56fd7cf] | 688 | ddf_msg(LVL_NOTE, "Function %s uses irq %x.", ddf_fun_get_name(fun->fnode), irq);
|
---|
[3a5909f] | 689 | }
|
---|
| 690 |
|
---|
[68414f4a] | 691 | void pci_read_interrupt(pci_fun_t *fun)
|
---|
[3a5909f] | 692 | {
|
---|
[8b1e15ac] | 693 | uint8_t irq = pci_conf_read_8(fun, PCI_BRIDGE_INT_LINE);
|
---|
[65f77f4] | 694 | uint8_t pin = pci_conf_read_8(fun, PCI_BRIDGE_INT_PIN);
|
---|
| 695 |
|
---|
| 696 | if (pin != 0 && irq != 0xff)
|
---|
[8b1e15ac] | 697 | pci_add_interrupt(fun, irq);
|
---|
[d1fc8f0] | 698 | }
|
---|
| 699 |
|
---|
| 700 | /** Enumerate (recursively) and register the devices connected to a pci bus.
|
---|
[663f41c4] | 701 | *
|
---|
[68414f4a] | 702 | * @param bus Host-to-PCI bridge
|
---|
| 703 | * @param bus_num Bus number
|
---|
[184f2f8a] | 704 | *
|
---|
| 705 | * @return EOK on success, ENOENT if no PCI devices found, ENOMEM if out of
|
---|
| 706 | * memory, EIO on other I/O error
|
---|
[d1fc8f0] | 707 | */
|
---|
[184f2f8a] | 708 | errno_t pci_bus_scan(pci_bus_t *bus, int bus_num)
|
---|
[5e598e0] | 709 | {
|
---|
[97a62fe] | 710 | pci_fun_t *fun;
|
---|
[b7fd2a0] | 711 | errno_t rc;
|
---|
[a35b458] | 712 |
|
---|
[5e598e0] | 713 | int child_bus = 0;
|
---|
| 714 | int dnum, fnum;
|
---|
| 715 | bool multi;
|
---|
[8b1e15ac] | 716 | uint8_t header_type;
|
---|
[184f2f8a] | 717 | bool device_found;
|
---|
| 718 |
|
---|
| 719 | device_found = false;
|
---|
[a35b458] | 720 |
|
---|
[5e598e0] | 721 | for (dnum = 0; dnum < 32; dnum++) {
|
---|
| 722 | multi = true;
|
---|
| 723 | for (fnum = 0; multi && fnum < 8; fnum++) {
|
---|
[56fd7cf] | 724 | fun = pci_fun_new(bus);
|
---|
[a35b458] | 725 |
|
---|
[68414f4a] | 726 | pci_fun_init(fun, bus_num, dnum, fnum);
|
---|
| 727 | if (fun->vendor_id == 0xffff) {
|
---|
[56fd7cf] | 728 | pci_fun_delete(fun);
|
---|
[663f41c4] | 729 | /*
|
---|
| 730 | * The device is not present, go on scanning the
|
---|
| 731 | * bus.
|
---|
| 732 | */
|
---|
| 733 | if (fnum == 0)
|
---|
[5e598e0] | 734 | break;
|
---|
[663f41c4] | 735 | else
|
---|
| 736 | continue;
|
---|
[5e598e0] | 737 | }
|
---|
[a35b458] | 738 |
|
---|
[184f2f8a] | 739 | device_found = true;
|
---|
| 740 |
|
---|
[8b1e15ac] | 741 | header_type = pci_conf_read_8(fun, PCI_HEADER_TYPE);
|
---|
[5e598e0] | 742 | if (fnum == 0) {
|
---|
[663f41c4] | 743 | /* Is the device multifunction? */
|
---|
| 744 | multi = header_type >> 7;
|
---|
[5e598e0] | 745 | }
|
---|
[663f41c4] | 746 | /* Clear the multifunction bit. */
|
---|
| 747 | header_type = header_type & 0x7F;
|
---|
[a35b458] | 748 |
|
---|
[97a62fe] | 749 | char *fun_name = pci_fun_create_name(fun);
|
---|
| 750 | if (fun_name == NULL) {
|
---|
[ebcb05a] | 751 | ddf_msg(LVL_ERROR, "Out of memory.");
|
---|
[56fd7cf] | 752 | pci_fun_delete(fun);
|
---|
[184f2f8a] | 753 | return ENOMEM;
|
---|
[97a62fe] | 754 | }
|
---|
[a35b458] | 755 |
|
---|
[56fd7cf] | 756 | rc = ddf_fun_set_name(fun->fnode, fun_name);
|
---|
[cb94e69b] | 757 | free(fun_name);
|
---|
[56fd7cf] | 758 | if (rc != EOK) {
|
---|
| 759 | ddf_msg(LVL_ERROR, "Failed setting function name.");
|
---|
| 760 | pci_fun_delete(fun);
|
---|
[184f2f8a] | 761 | return EIO;
|
---|
[97a62fe] | 762 | }
|
---|
[a35b458] | 763 |
|
---|
[8b1e15ac] | 764 | pci_alloc_resource_list(fun);
|
---|
| 765 | pci_read_bars(fun);
|
---|
| 766 | pci_read_interrupt(fun);
|
---|
[6dbc500] | 767 |
|
---|
| 768 | /* Propagate the PIO window to the function. */
|
---|
| 769 | fun->pio_window = bus->pio_win;
|
---|
[a35b458] | 770 |
|
---|
[56fd7cf] | 771 | ddf_fun_set_ops(fun->fnode, &pci_fun_ops);
|
---|
[a35b458] | 772 |
|
---|
[ebcb05a] | 773 | ddf_msg(LVL_DEBUG, "Adding new function %s.",
|
---|
[56fd7cf] | 774 | ddf_fun_get_name(fun->fnode));
|
---|
[92d5279] | 775 |
|
---|
[68414f4a] | 776 | pci_fun_create_match_ids(fun);
|
---|
[a35b458] | 777 |
|
---|
[663f41c4] | 778 | if (header_type == PCI_HEADER_TYPE_BRIDGE ||
|
---|
[8304889] | 779 | header_type == PCI_HEADER_TYPE_CARDBUS) {
|
---|
[8b1e15ac] | 780 | child_bus = pci_conf_read_8(fun,
|
---|
[663f41c4] | 781 | PCI_BRIDGE_SEC_BUS_NUM);
|
---|
[fc51296] | 782 | ddf_msg(LVL_DEBUG, "Device is pci-to-pci "
|
---|
[ebcb05a] | 783 | "bridge, secondary bus number = %d.",
|
---|
[fc51296] | 784 | bus_num);
|
---|
[184f2f8a] | 785 | if (child_bus > bus_num) {
|
---|
| 786 | rc = pci_bus_scan(bus, child_bus);
|
---|
[94ab1fee] | 787 | if (rc != EOK && rc != ENOENT) {
|
---|
[184f2f8a] | 788 | pci_fun_delete(fun);
|
---|
| 789 | return rc;
|
---|
| 790 | }
|
---|
| 791 | }
|
---|
| 792 | }
|
---|
| 793 |
|
---|
| 794 | if (ddf_fun_bind(fun->fnode) != EOK) {
|
---|
| 795 | pci_clean_resource_list(fun);
|
---|
| 796 | pci_fun_delete(fun);
|
---|
| 797 | continue;
|
---|
[5e598e0] | 798 | }
|
---|
[7acd787] | 799 |
|
---|
| 800 | list_append(&fun->lfuns, &bus->funs);
|
---|
[5e598e0] | 801 | }
|
---|
| 802 | }
|
---|
[184f2f8a] | 803 |
|
---|
| 804 | /* Fail bus scan if no devices are found. */
|
---|
| 805 | if (!device_found)
|
---|
| 806 | return ENOENT;
|
---|
| 807 |
|
---|
| 808 | return EOK;
|
---|
[5e598e0] | 809 | }
|
---|
[8c06905] | 810 |
|
---|
[b7fd2a0] | 811 | static errno_t pci_dev_add(ddf_dev_t *dnode)
|
---|
[8c06905] | 812 | {
|
---|
[6dbc500] | 813 | hw_resource_list_t hw_resources;
|
---|
[97a62fe] | 814 | pci_bus_t *bus = NULL;
|
---|
[83a2f43] | 815 | ddf_fun_t *ctl = NULL;
|
---|
[97a62fe] | 816 | bool got_res = false;
|
---|
[56fd7cf] | 817 | async_sess_t *sess;
|
---|
[b7fd2a0] | 818 | errno_t rc;
|
---|
[a35b458] | 819 |
|
---|
[0c0f823b] | 820 | ddf_msg(LVL_DEBUG, "pci_dev_add");
|
---|
[a35b458] | 821 |
|
---|
[5f6e25e] | 822 | bus = ddf_dev_data_alloc(dnode, sizeof(pci_bus_t));
|
---|
[68414f4a] | 823 | if (bus == NULL) {
|
---|
[0c0f823b] | 824 | ddf_msg(LVL_ERROR, "pci_dev_add allocation failed.");
|
---|
[97a62fe] | 825 | rc = ENOMEM;
|
---|
| 826 | goto fail;
|
---|
[663f41c4] | 827 | }
|
---|
[7acd787] | 828 |
|
---|
| 829 | list_initialize(&bus->funs);
|
---|
[5f6e25e] | 830 | fibril_mutex_initialize(&bus->conf_mutex);
|
---|
[832cbe7] | 831 | fibril_mutex_initialize(&bus->enum_done_lock);
|
---|
| 832 | fibril_condvar_initialize(&bus->enum_done_cv);
|
---|
[5f6e25e] | 833 |
|
---|
[68414f4a] | 834 | bus->dnode = dnode;
|
---|
[a35b458] | 835 |
|
---|
[2fd26bb] | 836 | sess = ddf_dev_parent_sess_get(dnode);
|
---|
[56fd7cf] | 837 | if (sess == NULL) {
|
---|
[0c0f823b] | 838 | ddf_msg(LVL_ERROR, "pci_dev_add failed to connect to the "
|
---|
[79ae36dd] | 839 | "parent driver.");
|
---|
| 840 | rc = ENOENT;
|
---|
[97a62fe] | 841 | goto fail;
|
---|
[8c06905] | 842 | }
|
---|
[6dbc500] | 843 |
|
---|
| 844 | rc = pio_window_get(sess, &bus->pio_win);
|
---|
| 845 | if (rc != EOK) {
|
---|
| 846 | ddf_msg(LVL_ERROR, "pci_dev_add failed to get PIO window "
|
---|
| 847 | "for the device.");
|
---|
| 848 | goto fail;
|
---|
| 849 | }
|
---|
[a35b458] | 850 |
|
---|
[56fd7cf] | 851 | rc = hw_res_get_resource_list(sess, &hw_resources);
|
---|
[be942bc] | 852 | if (rc != EOK) {
|
---|
[0c0f823b] | 853 | ddf_msg(LVL_ERROR, "pci_dev_add failed to get hw resources "
|
---|
[ebcb05a] | 854 | "for the device.");
|
---|
[97a62fe] | 855 | goto fail;
|
---|
[bab6388] | 856 | }
|
---|
[97a62fe] | 857 | got_res = true;
|
---|
[a35b458] | 858 |
|
---|
[92d5279] | 859 | assert(hw_resources.count >= 1);
|
---|
| 860 |
|
---|
| 861 | if (hw_resources.count == 1) {
|
---|
| 862 | assert(hw_resources.resources[0].type == MEM_RANGE);
|
---|
| 863 |
|
---|
| 864 | ddf_msg(LVL_DEBUG, "conf_addr_space = %" PRIx64 ".",
|
---|
| 865 | hw_resources.resources[0].res.mem_range.address);
|
---|
| 866 |
|
---|
| 867 | if (pio_enable_resource(&bus->pio_win,
|
---|
[9e9ced0] | 868 | &hw_resources.resources[0], (void **) &bus->conf_space,
|
---|
[848e880f] | 869 | NULL, NULL)) {
|
---|
[92d5279] | 870 | ddf_msg(LVL_ERROR,
|
---|
| 871 | "Failed to map configuration space.");
|
---|
| 872 | rc = EADDRNOTAVAIL;
|
---|
| 873 | goto fail;
|
---|
| 874 | }
|
---|
[a35b458] | 875 |
|
---|
[92d5279] | 876 | } else {
|
---|
| 877 | assert(hw_resources.resources[0].type == IO_RANGE);
|
---|
| 878 | assert(hw_resources.resources[0].res.io_range.size >= 4);
|
---|
[a35b458] | 879 |
|
---|
[92d5279] | 880 | assert(hw_resources.resources[1].type == IO_RANGE);
|
---|
| 881 | assert(hw_resources.resources[1].res.io_range.size >= 4);
|
---|
[a35b458] | 882 |
|
---|
[92d5279] | 883 | ddf_msg(LVL_DEBUG, "conf_addr = %" PRIx64 ".",
|
---|
| 884 | hw_resources.resources[0].res.io_range.address);
|
---|
| 885 | ddf_msg(LVL_DEBUG, "data_addr = %" PRIx64 ".",
|
---|
| 886 | hw_resources.resources[1].res.io_range.address);
|
---|
[a35b458] | 887 |
|
---|
[92d5279] | 888 | if (pio_enable_resource(&bus->pio_win,
|
---|
[9e9ced0] | 889 | &hw_resources.resources[0], (void **) &bus->conf_addr_reg,
|
---|
[848e880f] | 890 | NULL, NULL)) {
|
---|
[92d5279] | 891 | ddf_msg(LVL_ERROR,
|
---|
| 892 | "Failed to enable configuration ports.");
|
---|
| 893 | rc = EADDRNOTAVAIL;
|
---|
| 894 | goto fail;
|
---|
| 895 | }
|
---|
| 896 | if (pio_enable_resource(&bus->pio_win,
|
---|
[9e9ced0] | 897 | &hw_resources.resources[1], (void **) &bus->conf_data_reg,
|
---|
[848e880f] | 898 | NULL, NULL)) {
|
---|
[92d5279] | 899 | ddf_msg(LVL_ERROR,
|
---|
| 900 | "Failed to enable configuration ports.");
|
---|
| 901 | rc = EADDRNOTAVAIL;
|
---|
| 902 | goto fail;
|
---|
| 903 | }
|
---|
[230385c] | 904 | }
|
---|
[a35b458] | 905 |
|
---|
[68414f4a] | 906 | /* Make the bus device more visible. It has no use yet. */
|
---|
[ebcb05a] | 907 | ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
|
---|
[a35b458] | 908 |
|
---|
[97a62fe] | 909 | ctl = ddf_fun_create(bus->dnode, fun_exposed, "ctl");
|
---|
| 910 | if (ctl == NULL) {
|
---|
[ebcb05a] | 911 | ddf_msg(LVL_ERROR, "Failed creating control function.");
|
---|
[97a62fe] | 912 | rc = ENOMEM;
|
---|
| 913 | goto fail;
|
---|
| 914 | }
|
---|
[a35b458] | 915 |
|
---|
[7acd787] | 916 | ddf_fun_set_conn_handler(ctl, pci_ctl_connection);
|
---|
| 917 |
|
---|
[184f2f8a] | 918 | /* Enumerate functions. */
|
---|
| 919 | ddf_msg(LVL_DEBUG, "Enumerating the bus");
|
---|
| 920 | rc = pci_bus_scan(bus, 0);
|
---|
| 921 | if (rc != EOK) {
|
---|
| 922 | ddf_msg(LVL_ERROR, "Bus enumeration failed.");
|
---|
| 923 | goto fail;
|
---|
| 924 | }
|
---|
| 925 |
|
---|
[97a62fe] | 926 | rc = ddf_fun_bind(ctl);
|
---|
| 927 | if (rc != EOK) {
|
---|
[ebcb05a] | 928 | ddf_msg(LVL_ERROR, "Failed binding control function.");
|
---|
[97a62fe] | 929 | goto fail;
|
---|
| 930 | }
|
---|
[a35b458] | 931 |
|
---|
[7acd787] | 932 | rc = ddf_fun_add_to_category(ctl, "pci");
|
---|
| 933 | if (rc != EOK) {
|
---|
| 934 | ddf_msg(LVL_ERROR, "Failed adding control function to category "
|
---|
| 935 | "'pci'.");
|
---|
| 936 | goto fail;
|
---|
| 937 | }
|
---|
| 938 |
|
---|
[f724e82] | 939 | hw_res_clean_resource_list(&hw_resources);
|
---|
[a35b458] | 940 |
|
---|
[832cbe7] | 941 | ddf_msg(LVL_DEBUG, "Bus enumeration done.");
|
---|
[a35b458] | 942 |
|
---|
[832cbe7] | 943 | fibril_mutex_lock(&bus->enum_done_lock);
|
---|
| 944 | bus->enum_done = true;
|
---|
| 945 | fibril_mutex_unlock(&bus->enum_done_lock);
|
---|
| 946 | fibril_condvar_broadcast(&bus->enum_done_cv);
|
---|
| 947 |
|
---|
| 948 | return EOK;
|
---|
[97a62fe] | 949 | fail:
|
---|
| 950 | if (got_res)
|
---|
| 951 | hw_res_clean_resource_list(&hw_resources);
|
---|
[a35b458] | 952 |
|
---|
[97a62fe] | 953 | if (ctl != NULL)
|
---|
| 954 | ddf_fun_destroy(ctl);
|
---|
[a35b458] | 955 |
|
---|
[97a62fe] | 956 | return rc;
|
---|
[8c06905] | 957 | }
|
---|
| 958 |
|
---|
[b7fd2a0] | 959 | static errno_t pci_fun_online(ddf_fun_t *fun)
|
---|
[f278930] | 960 | {
|
---|
| 961 | ddf_msg(LVL_DEBUG, "pci_fun_online()");
|
---|
| 962 | return ddf_fun_online(fun);
|
---|
| 963 | }
|
---|
| 964 |
|
---|
[b7fd2a0] | 965 | static errno_t pci_fun_offline(ddf_fun_t *fun)
|
---|
[f278930] | 966 | {
|
---|
| 967 | ddf_msg(LVL_DEBUG, "pci_fun_offline()");
|
---|
| 968 | return ddf_fun_offline(fun);
|
---|
| 969 | }
|
---|
| 970 |
|
---|
[663f41c4] | 971 | static void pciintel_init(void)
|
---|
[3843ecb] | 972 | {
|
---|
[267f235] | 973 | ddf_log_init(NAME);
|
---|
[3843ecb] | 974 | }
|
---|
| 975 |
|
---|
[97a62fe] | 976 | pci_fun_t *pci_fun_new(pci_bus_t *bus)
|
---|
[713a4b9] | 977 | {
|
---|
[97a62fe] | 978 | pci_fun_t *fun;
|
---|
[56fd7cf] | 979 | ddf_fun_t *fnode;
|
---|
[a35b458] | 980 |
|
---|
[56fd7cf] | 981 | fnode = ddf_fun_create(bus->dnode, fun_inner, NULL);
|
---|
| 982 | if (fnode == NULL)
|
---|
| 983 | return NULL;
|
---|
| 984 |
|
---|
| 985 | fun = ddf_fun_data_alloc(fnode, sizeof(pci_fun_t));
|
---|
[97a62fe] | 986 | if (fun == NULL)
|
---|
| 987 | return NULL;
|
---|
| 988 |
|
---|
| 989 | fun->busptr = bus;
|
---|
[56fd7cf] | 990 | fun->fnode = fnode;
|
---|
[97a62fe] | 991 | return fun;
|
---|
[713a4b9] | 992 | }
|
---|
| 993 |
|
---|
[68414f4a] | 994 | void pci_fun_init(pci_fun_t *fun, int bus, int dev, int fn)
|
---|
[713a4b9] | 995 | {
|
---|
[68414f4a] | 996 | fun->bus = bus;
|
---|
| 997 | fun->dev = dev;
|
---|
| 998 | fun->fn = fn;
|
---|
[1d53a78] | 999 | fun->vendor_id = pci_conf_read_16(fun, PCI_VENDOR_ID);
|
---|
| 1000 | fun->device_id = pci_conf_read_16(fun, PCI_DEVICE_ID);
|
---|
[a35b458] | 1001 |
|
---|
[c90aed4] | 1002 | /* Explicitly enable PCI bus mastering */
|
---|
| 1003 | fun->command = pci_conf_read_16(fun, PCI_COMMAND) |
|
---|
| 1004 | PCI_COMMAND_MASTER;
|
---|
| 1005 | pci_conf_write_16(fun, PCI_COMMAND, fun->command);
|
---|
[a35b458] | 1006 |
|
---|
[1d53a78] | 1007 | fun->class_code = pci_conf_read_8(fun, PCI_BASE_CLASS);
|
---|
| 1008 | fun->subclass_code = pci_conf_read_8(fun, PCI_SUB_CLASS);
|
---|
| 1009 | fun->prog_if = pci_conf_read_8(fun, PCI_PROG_IF);
|
---|
| 1010 | fun->revision = pci_conf_read_8(fun, PCI_REVISION_ID);
|
---|
[713a4b9] | 1011 | }
|
---|
| 1012 |
|
---|
[68414f4a] | 1013 | void pci_fun_delete(pci_fun_t *fun)
|
---|
[713a4b9] | 1014 | {
|
---|
[bab6388] | 1015 | hw_res_clean_resource_list(&fun->hw_resources);
|
---|
[56fd7cf] | 1016 | if (fun->fnode != NULL)
|
---|
| 1017 | ddf_fun_destroy(fun->fnode);
|
---|
[713a4b9] | 1018 | }
|
---|
| 1019 |
|
---|
[97a62fe] | 1020 | char *pci_fun_create_name(pci_fun_t *fun)
|
---|
[713a4b9] | 1021 | {
|
---|
| 1022 | char *name = NULL;
|
---|
[a35b458] | 1023 |
|
---|
[68414f4a] | 1024 | asprintf(&name, "%02x:%02x.%01x", fun->bus, fun->dev,
|
---|
| 1025 | fun->fn);
|
---|
[97a62fe] | 1026 | return name;
|
---|
[713a4b9] | 1027 | }
|
---|
| 1028 |
|
---|
[68414f4a] | 1029 | bool pci_alloc_resource_list(pci_fun_t *fun)
|
---|
[713a4b9] | 1030 | {
|
---|
[992b47ea] | 1031 | fun->hw_resources.resources = fun->resources;
|
---|
| 1032 | return true;
|
---|
[713a4b9] | 1033 | }
|
---|
| 1034 |
|
---|
[68414f4a] | 1035 | void pci_clean_resource_list(pci_fun_t *fun)
|
---|
[713a4b9] | 1036 | {
|
---|
[992b47ea] | 1037 | fun->hw_resources.resources = NULL;
|
---|
[713a4b9] | 1038 | }
|
---|
| 1039 |
|
---|
[68414f4a] | 1040 | /** Read the base address registers (BARs) of the function and add the addresses
|
---|
| 1041 | * to its HW resource list.
|
---|
[713a4b9] | 1042 | *
|
---|
[68414f4a] | 1043 | * @param fun PCI function
|
---|
[713a4b9] | 1044 | */
|
---|
[68414f4a] | 1045 | void pci_read_bars(pci_fun_t *fun)
|
---|
[713a4b9] | 1046 | {
|
---|
| 1047 | /*
|
---|
| 1048 | * Position of the BAR in the PCI configuration address space of the
|
---|
| 1049 | * device.
|
---|
| 1050 | */
|
---|
| 1051 | int addr = PCI_BASE_ADDR_0;
|
---|
[a35b458] | 1052 |
|
---|
[713a4b9] | 1053 | while (addr <= PCI_BASE_ADDR_5)
|
---|
[8b1e15ac] | 1054 | addr = pci_read_bar(fun, addr);
|
---|
[713a4b9] | 1055 | }
|
---|
| 1056 |
|
---|
| 1057 | size_t pci_bar_mask_to_size(uint32_t mask)
|
---|
| 1058 | {
|
---|
[ad6857c] | 1059 | size_t size = mask & ~(mask - 1);
|
---|
| 1060 | return size;
|
---|
[713a4b9] | 1061 | }
|
---|
| 1062 |
|
---|
[8c06905] | 1063 | int main(int argc, char *argv[])
|
---|
| 1064 | {
|
---|
[ebcb05a] | 1065 | printf(NAME ": HelenOS PCI bus driver (Intel method 1).\n");
|
---|
[3843ecb] | 1066 | pciintel_init();
|
---|
[83a2f43] | 1067 | return ddf_driver_main(&pci_driver);
|
---|
[8c06905] | 1068 | }
|
---|
| 1069 |
|
---|
| 1070 | /**
|
---|
| 1071 | * @}
|
---|
[472020fc] | 1072 | */
|
---|