| [8c06905] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
|---|
| [68414f4a] | 3 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| [8c06905] | 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 | /**
|
|---|
| 31 | * @defgroup pciintel pci bus driver for intel method 1.
|
|---|
| 32 | * @brief HelenOS root pci bus driver for intel method 1.
|
|---|
| 33 | * @{
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | /** @file
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include <assert.h>
|
|---|
| [690d2e7] | 40 | #include <byteorder.h>
|
|---|
| [8c06905] | 41 | #include <stdio.h>
|
|---|
| 42 | #include <errno.h>
|
|---|
| [3e6a98c5] | 43 | #include <stdbool.h>
|
|---|
| [8c06905] | 44 | #include <fibril_synch.h>
|
|---|
| [c47e1a8] | 45 | #include <str.h>
|
|---|
| [8c06905] | 46 | #include <ctype.h>
|
|---|
| 47 | #include <macros.h>
|
|---|
| [cd0684d] | 48 | #include <str_error.h>
|
|---|
| [8c06905] | 49 |
|
|---|
| [af6b5157] | 50 | #include <ddf/driver.h>
|
|---|
| [fc51296] | 51 | #include <ddf/log.h>
|
|---|
| [8c06905] | 52 | #include <ipc/dev_iface.h>
|
|---|
| [ebc9c2c] | 53 | #include <irc.h>
|
|---|
| [41b56084] | 54 | #include <ops/hw_res.h>
|
|---|
| [8c06905] | 55 | #include <device/hw_res.h>
|
|---|
| [6dbc500] | 56 | #include <ops/pio_window.h>
|
|---|
| 57 | #include <device/pio_window.h>
|
|---|
| [8c06905] | 58 | #include <ddi.h>
|
|---|
| [99e6bfb] | 59 | #include <pci_dev_iface.h>
|
|---|
| [5e598e0] | 60 |
|
|---|
| 61 | #include "pci.h"
|
|---|
| [8c06905] | 62 |
|
|---|
| 63 | #define NAME "pciintel"
|
|---|
| 64 |
|
|---|
| [92d5279] | 65 | #define CONF_ADDR_ENABLE (1 << 31)
|
|---|
| [663f41c4] | 66 | #define CONF_ADDR(bus, dev, fn, reg) \
|
|---|
| [92d5279] | 67 | ((bus << 16) | (dev << 11) | (fn << 8) | (reg & ~3))
|
|---|
| [5e598e0] | 68 |
|
|---|
| [68414f4a] | 69 | /** Obtain PCI function soft-state from DDF function node */
|
|---|
| [56fd7cf] | 70 | static pci_fun_t *pci_fun(ddf_fun_t *fnode)
|
|---|
| 71 | {
|
|---|
| 72 | return ddf_fun_data_get(fnode);
|
|---|
| 73 | }
|
|---|
| [68414f4a] | 74 |
|
|---|
| 75 | /** Obtain PCI bus soft-state from DDF device node */
|
|---|
| [56fd7cf] | 76 | #if 0
|
|---|
| 77 | static pci_bus_t *pci_bus(ddf_dev_t *dnode)
|
|---|
| 78 | {
|
|---|
| 79 | return ddf_dev_data_get(dnode);
|
|---|
| 80 | }
|
|---|
| 81 | #endif
|
|---|
| [68414f4a] | 82 |
|
|---|
| 83 | /** Obtain PCI bus soft-state from function soft-state */
|
|---|
| [56fd7cf] | 84 | static pci_bus_t *pci_bus_from_fun(pci_fun_t *fun)
|
|---|
| 85 | {
|
|---|
| 86 | return fun->busptr;
|
|---|
| 87 | }
|
|---|
| [68414f4a] | 88 |
|
|---|
| [54de4836] | 89 | /** Max is 47, align to something nice. */
|
|---|
| 90 | #define ID_MAX_STR_LEN 50
|
|---|
| 91 |
|
|---|
| [83a2f43] | 92 | static hw_resource_list_t *pciintel_get_resources(ddf_fun_t *fnode)
|
|---|
| [3843ecb] | 93 | {
|
|---|
| [56fd7cf] | 94 | pci_fun_t *fun = pci_fun(fnode);
|
|---|
| [663f41c4] | 95 |
|
|---|
| [68414f4a] | 96 | if (fun == NULL)
|
|---|
| [3843ecb] | 97 | return NULL;
|
|---|
| [68414f4a] | 98 | return &fun->hw_resources;
|
|---|
| [3843ecb] | 99 | }
|
|---|
| 100 |
|
|---|
| [d51838f] | 101 | static int pciintel_fun_owns_interrupt(pci_fun_t *fun, int irq)
|
|---|
| [3843ecb] | 102 | {
|
|---|
| [cccd60c3] | 103 | size_t i;
|
|---|
| [d51838f] | 104 | hw_resource_list_t *res = &fun->hw_resources;
|
|---|
| [cccd60c3] | 105 |
|
|---|
| 106 | for (i = 0; i < res->count; i++) {
|
|---|
| [d51838f] | 107 | if (res->resources[i].type == INTERRUPT &&
|
|---|
| 108 | res->resources[i].res.interrupt.irq == irq) {
|
|---|
| 109 | return true;
|
|---|
| [fb78ae72] | 110 | }
|
|---|
| 111 | }
|
|---|
| [79ae36dd] | 112 |
|
|---|
| [d51838f] | 113 | return false;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | static int pciintel_enable_interrupt(ddf_fun_t *fnode, int irq)
|
|---|
| 117 | {
|
|---|
| 118 | pci_fun_t *fun = pci_fun(fnode);
|
|---|
| [cccd60c3] | 119 |
|
|---|
| [d51838f] | 120 | if (!pciintel_fun_owns_interrupt(fun, irq))
|
|---|
| 121 | return EINVAL;
|
|---|
| 122 |
|
|---|
| [cccd60c3] | 123 | return irc_enable_interrupt(irq);
|
|---|
| [3843ecb] | 124 | }
|
|---|
| 125 |
|
|---|
| [d51838f] | 126 | static int pciintel_disable_interrupt(ddf_fun_t *fnode, int irq)
|
|---|
| 127 | {
|
|---|
| 128 | pci_fun_t *fun = pci_fun(fnode);
|
|---|
| 129 |
|
|---|
| 130 | if (!pciintel_fun_owns_interrupt(fun, irq))
|
|---|
| 131 | return EINVAL;
|
|---|
| 132 |
|
|---|
| 133 | return irc_disable_interrupt(irq);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | static int pciintel_clear_interrupt(ddf_fun_t *fnode, int irq)
|
|---|
| 137 | {
|
|---|
| 138 | pci_fun_t *fun = pci_fun(fnode);
|
|---|
| 139 |
|
|---|
| 140 | if (!pciintel_fun_owns_interrupt(fun, irq))
|
|---|
| 141 | return EINVAL;
|
|---|
| 142 |
|
|---|
| 143 | return irc_clear_interrupt(irq);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| [6dbc500] | 146 | static pio_window_t *pciintel_get_pio_window(ddf_fun_t *fnode)
|
|---|
| 147 | {
|
|---|
| 148 | pci_fun_t *fun = pci_fun(fnode);
|
|---|
| 149 |
|
|---|
| 150 | if (fun == NULL)
|
|---|
| 151 | return NULL;
|
|---|
| 152 | return &fun->pio_window;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| [99e8fb7b] | 156 | static int config_space_write_32(ddf_fun_t *fun, uint32_t address,
|
|---|
| [79ae36dd] | 157 | uint32_t data)
|
|---|
| [40a5d40] | 158 | {
|
|---|
| 159 | if (address > 252)
|
|---|
| 160 | return EINVAL;
|
|---|
| [56fd7cf] | 161 | pci_conf_write_32(pci_fun(fun), address, data);
|
|---|
| [40a5d40] | 162 | return EOK;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| [99e8fb7b] | 165 | static int config_space_write_16(
|
|---|
| [40a5d40] | 166 | ddf_fun_t *fun, uint32_t address, uint16_t data)
|
|---|
| [99e6bfb] | 167 | {
|
|---|
| 168 | if (address > 254)
|
|---|
| 169 | return EINVAL;
|
|---|
| [56fd7cf] | 170 | pci_conf_write_16(pci_fun(fun), address, data);
|
|---|
| [99e6bfb] | 171 | return EOK;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| [99e8fb7b] | 174 | static int config_space_write_8(
|
|---|
| [40a5d40] | 175 | ddf_fun_t *fun, uint32_t address, uint8_t data)
|
|---|
| 176 | {
|
|---|
| 177 | if (address > 255)
|
|---|
| 178 | return EINVAL;
|
|---|
| [56fd7cf] | 179 | pci_conf_write_8(pci_fun(fun), address, data);
|
|---|
| [40a5d40] | 180 | return EOK;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| [99e8fb7b] | 183 | static int config_space_read_32(
|
|---|
| [40a5d40] | 184 | ddf_fun_t *fun, uint32_t address, uint32_t *data)
|
|---|
| 185 | {
|
|---|
| 186 | if (address > 252)
|
|---|
| 187 | return EINVAL;
|
|---|
| [56fd7cf] | 188 | *data = pci_conf_read_32(pci_fun(fun), address);
|
|---|
| [40a5d40] | 189 | return EOK;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| [99e8fb7b] | 192 | static int config_space_read_16(
|
|---|
| [40a5d40] | 193 | ddf_fun_t *fun, uint32_t address, uint16_t *data)
|
|---|
| 194 | {
|
|---|
| 195 | if (address > 254)
|
|---|
| 196 | return EINVAL;
|
|---|
| [56fd7cf] | 197 | *data = pci_conf_read_16(pci_fun(fun), address);
|
|---|
| [40a5d40] | 198 | return EOK;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| [99e8fb7b] | 201 | static int config_space_read_8(
|
|---|
| [40a5d40] | 202 | ddf_fun_t *fun, uint32_t address, uint8_t *data)
|
|---|
| 203 | {
|
|---|
| 204 | if (address > 255)
|
|---|
| 205 | return EINVAL;
|
|---|
| [56fd7cf] | 206 | *data = pci_conf_read_8(pci_fun(fun), address);
|
|---|
| [40a5d40] | 207 | return EOK;
|
|---|
| 208 | }
|
|---|
| [99e6bfb] | 209 |
|
|---|
| [68414f4a] | 210 | static hw_res_ops_t pciintel_hw_res_ops = {
|
|---|
| [d9cf684a] | 211 | .get_resource_list = &pciintel_get_resources,
|
|---|
| 212 | .enable_interrupt = &pciintel_enable_interrupt,
|
|---|
| [d51838f] | 213 | .disable_interrupt = &pciintel_disable_interrupt,
|
|---|
| 214 | .clear_interrupt = &pciintel_clear_interrupt,
|
|---|
| [3843ecb] | 215 | };
|
|---|
| 216 |
|
|---|
| [6dbc500] | 217 | static pio_window_ops_t pciintel_pio_window_ops = {
|
|---|
| 218 | .get_pio_window = &pciintel_get_pio_window
|
|---|
| 219 | };
|
|---|
| 220 |
|
|---|
| [99e6bfb] | 221 | static pci_dev_iface_t pci_dev_ops = {
|
|---|
| [99e8fb7b] | 222 | .config_space_read_8 = &config_space_read_8,
|
|---|
| 223 | .config_space_read_16 = &config_space_read_16,
|
|---|
| 224 | .config_space_read_32 = &config_space_read_32,
|
|---|
| 225 | .config_space_write_8 = &config_space_write_8,
|
|---|
| 226 | .config_space_write_16 = &config_space_write_16,
|
|---|
| 227 | .config_space_write_32 = &config_space_write_32
|
|---|
| [99e6bfb] | 228 | };
|
|---|
| 229 |
|
|---|
| 230 | static ddf_dev_ops_t pci_fun_ops = {
|
|---|
| 231 | .interfaces[HW_RES_DEV_IFACE] = &pciintel_hw_res_ops,
|
|---|
| [6dbc500] | 232 | .interfaces[PIO_WINDOW_DEV_IFACE] = &pciintel_pio_window_ops,
|
|---|
| [99e6bfb] | 233 | .interfaces[PCI_DEV_IFACE] = &pci_dev_ops
|
|---|
| 234 | };
|
|---|
| [3843ecb] | 235 |
|
|---|
| [0c0f823b] | 236 | static int pci_dev_add(ddf_dev_t *);
|
|---|
| [f278930] | 237 | static int pci_fun_online(ddf_fun_t *);
|
|---|
| 238 | static int pci_fun_offline(ddf_fun_t *);
|
|---|
| [3843ecb] | 239 |
|
|---|
| [68414f4a] | 240 | /** PCI bus driver standard operations */
|
|---|
| [8c06905] | 241 | static driver_ops_t pci_ops = {
|
|---|
| [0c0f823b] | 242 | .dev_add = &pci_dev_add,
|
|---|
| [f278930] | 243 | .fun_online = &pci_fun_online,
|
|---|
| 244 | .fun_offline = &pci_fun_offline,
|
|---|
| [8c06905] | 245 | };
|
|---|
| 246 |
|
|---|
| [68414f4a] | 247 | /** PCI bus driver structure */
|
|---|
| [8c06905] | 248 | static driver_t pci_driver = {
|
|---|
| 249 | .name = NAME,
|
|---|
| 250 | .driver_ops = &pci_ops
|
|---|
| 251 | };
|
|---|
| 252 |
|
|---|
| [68414f4a] | 253 | static void pci_conf_read(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
|
|---|
| [5e598e0] | 254 | {
|
|---|
| [82721f5] | 255 | const uint32_t conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
|
|---|
| [56fd7cf] | 256 | pci_bus_t *bus = pci_bus_from_fun(fun);
|
|---|
| [82721f5] | 257 | uint32_t val;
|
|---|
| [5e598e0] | 258 |
|
|---|
| [68414f4a] | 259 | fibril_mutex_lock(&bus->conf_mutex);
|
|---|
| [82721f5] | 260 |
|
|---|
| [92d5279] | 261 | if (bus->conf_addr_reg) {
|
|---|
| 262 | pio_write_32(bus->conf_addr_reg,
|
|---|
| 263 | host2uint32_t_le(CONF_ADDR_ENABLE | conf_addr));
|
|---|
| 264 | /*
|
|---|
| 265 | * Always read full 32-bits from the PCI conf_data_port
|
|---|
| 266 | * register and get the desired portion of it afterwards. Some
|
|---|
| 267 | * architectures do not support shorter PIO reads offset from
|
|---|
| 268 | * this register.
|
|---|
| 269 | */
|
|---|
| 270 | val = uint32_t_le2host(pio_read_32(bus->conf_data_reg));
|
|---|
| 271 | } else {
|
|---|
| 272 | val = uint32_t_le2host(pio_read_32(
|
|---|
| 273 | &bus->conf_space[conf_addr / sizeof(ioport32_t)]));
|
|---|
| 274 | }
|
|---|
| [82721f5] | 275 |
|
|---|
| [5e598e0] | 276 | switch (len) {
|
|---|
| [663f41c4] | 277 | case 1:
|
|---|
| [82721f5] | 278 | *buf = (uint8_t) (val >> ((reg & 3) * 8));
|
|---|
| [663f41c4] | 279 | break;
|
|---|
| 280 | case 2:
|
|---|
| [82721f5] | 281 | *((uint16_t *) buf) = (uint16_t) (val >> ((reg & 3)) * 8);
|
|---|
| [663f41c4] | 282 | break;
|
|---|
| 283 | case 4:
|
|---|
| [82721f5] | 284 | *((uint32_t *) buf) = (uint32_t) val;
|
|---|
| [663f41c4] | 285 | break;
|
|---|
| [5e598e0] | 286 | }
|
|---|
| 287 |
|
|---|
| [68414f4a] | 288 | fibril_mutex_unlock(&bus->conf_mutex);
|
|---|
| [5e598e0] | 289 | }
|
|---|
| 290 |
|
|---|
| [68414f4a] | 291 | static void pci_conf_write(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
|
|---|
| [d1fc8f0] | 292 | {
|
|---|
| [82721f5] | 293 | const uint32_t conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
|
|---|
| [56fd7cf] | 294 | pci_bus_t *bus = pci_bus_from_fun(fun);
|
|---|
| [0464967] | 295 | uint32_t val = 0;
|
|---|
| [d1fc8f0] | 296 |
|
|---|
| [68414f4a] | 297 | fibril_mutex_lock(&bus->conf_mutex);
|
|---|
| [82721f5] | 298 |
|
|---|
| 299 | /*
|
|---|
| 300 | * Prepare to write full 32-bits to the PCI conf_data_port register.
|
|---|
| 301 | * Some architectures do not support shorter PIO writes offset from this
|
|---|
| 302 | * register.
|
|---|
| 303 | */
|
|---|
| 304 |
|
|---|
| 305 | if (len < 4) {
|
|---|
| 306 | /*
|
|---|
| 307 | * We have fewer than full 32-bits, so we need to read the
|
|---|
| 308 | * missing bits first.
|
|---|
| 309 | */
|
|---|
| [92d5279] | 310 | if (bus->conf_addr_reg) {
|
|---|
| 311 | pio_write_32(bus->conf_addr_reg,
|
|---|
| 312 | host2uint32_t_le(CONF_ADDR_ENABLE | conf_addr));
|
|---|
| 313 | val = uint32_t_le2host(pio_read_32(bus->conf_data_reg));
|
|---|
| 314 | } else {
|
|---|
| 315 | val = uint32_t_le2host(pio_read_32(
|
|---|
| 316 | &bus->conf_space[conf_addr / sizeof(ioport32_t)]));
|
|---|
| 317 | }
|
|---|
| [82721f5] | 318 | }
|
|---|
| [d1fc8f0] | 319 |
|
|---|
| 320 | switch (len) {
|
|---|
| [663f41c4] | 321 | case 1:
|
|---|
| [82721f5] | 322 | val &= ~(0xffU << ((reg & 3) * 8));
|
|---|
| 323 | val |= *buf << ((reg & 3) * 8);
|
|---|
| [663f41c4] | 324 | break;
|
|---|
| 325 | case 2:
|
|---|
| [82721f5] | 326 | val &= ~(0xffffU << ((reg & 3) * 8));
|
|---|
| 327 | val |= *((uint16_t *) buf) << ((reg & 3) * 8);
|
|---|
| [663f41c4] | 328 | break;
|
|---|
| 329 | case 4:
|
|---|
| [82721f5] | 330 | val = *((uint32_t *) buf);
|
|---|
| [663f41c4] | 331 | break;
|
|---|
| [d1fc8f0] | 332 | }
|
|---|
| [82721f5] | 333 |
|
|---|
| [92d5279] | 334 | if (bus->conf_addr_reg) {
|
|---|
| 335 | pio_write_32(bus->conf_addr_reg,
|
|---|
| 336 | host2uint32_t_le(CONF_ADDR_ENABLE | conf_addr));
|
|---|
| 337 | pio_write_32(bus->conf_data_reg, host2uint32_t_le(val));
|
|---|
| 338 | } else {
|
|---|
| 339 | pio_write_32(&bus->conf_space[conf_addr / sizeof(ioport32_t)],
|
|---|
| 340 | host2uint32_t_le(val));
|
|---|
| 341 | }
|
|---|
| [d1fc8f0] | 342 |
|
|---|
| [68414f4a] | 343 | fibril_mutex_unlock(&bus->conf_mutex);
|
|---|
| [d1fc8f0] | 344 | }
|
|---|
| 345 |
|
|---|
| [68414f4a] | 346 | uint8_t pci_conf_read_8(pci_fun_t *fun, int reg)
|
|---|
| [5e598e0] | 347 | {
|
|---|
| 348 | uint8_t res;
|
|---|
| [8b1e15ac] | 349 | pci_conf_read(fun, reg, &res, 1);
|
|---|
| [5e598e0] | 350 | return res;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| [68414f4a] | 353 | uint16_t pci_conf_read_16(pci_fun_t *fun, int reg)
|
|---|
| [5e598e0] | 354 | {
|
|---|
| 355 | uint16_t res;
|
|---|
| [8b1e15ac] | 356 | pci_conf_read(fun, reg, (uint8_t *) &res, 2);
|
|---|
| [5e598e0] | 357 | return res;
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| [68414f4a] | 360 | uint32_t pci_conf_read_32(pci_fun_t *fun, int reg)
|
|---|
| [5e598e0] | 361 | {
|
|---|
| 362 | uint32_t res;
|
|---|
| [8b1e15ac] | 363 | pci_conf_read(fun, reg, (uint8_t *) &res, 4);
|
|---|
| [663f41c4] | 364 | return res;
|
|---|
| [5e598e0] | 365 | }
|
|---|
| 366 |
|
|---|
| [68414f4a] | 367 | void pci_conf_write_8(pci_fun_t *fun, int reg, uint8_t val)
|
|---|
| [d1fc8f0] | 368 | {
|
|---|
| [8b1e15ac] | 369 | pci_conf_write(fun, reg, (uint8_t *) &val, 1);
|
|---|
| [d1fc8f0] | 370 | }
|
|---|
| 371 |
|
|---|
| [68414f4a] | 372 | void pci_conf_write_16(pci_fun_t *fun, int reg, uint16_t val)
|
|---|
| [d1fc8f0] | 373 | {
|
|---|
| [8b1e15ac] | 374 | pci_conf_write(fun, reg, (uint8_t *) &val, 2);
|
|---|
| [d1fc8f0] | 375 | }
|
|---|
| 376 |
|
|---|
| [68414f4a] | 377 | void pci_conf_write_32(pci_fun_t *fun, int reg, uint32_t val)
|
|---|
| [d1fc8f0] | 378 | {
|
|---|
| [8b1e15ac] | 379 | pci_conf_write(fun, reg, (uint8_t *) &val, 4);
|
|---|
| [d1fc8f0] | 380 | }
|
|---|
| 381 |
|
|---|
| [68414f4a] | 382 | void pci_fun_create_match_ids(pci_fun_t *fun)
|
|---|
| [89ce401a] | 383 | {
|
|---|
| [cd0684d] | 384 | int rc;
|
|---|
| [1d53a78] | 385 | char match_id_str[ID_MAX_STR_LEN];
|
|---|
| [cd0684d] | 386 |
|
|---|
| [1d53a78] | 387 | /* Vendor ID & Device ID, length(incl \0) 22 */
|
|---|
| [c90aed4] | 388 | rc = snprintf(match_id_str, ID_MAX_STR_LEN, "pci/ven=%04"
|
|---|
| 389 | PRIx16 "&dev=%04" PRIx16, fun->vendor_id, fun->device_id);
|
|---|
| [1d53a78] | 390 | if (rc < 0) {
|
|---|
| 391 | ddf_msg(LVL_ERROR, "Failed creating match ID str: %s",
|
|---|
| 392 | str_error(rc));
|
|---|
| [8304889] | 393 | }
|
|---|
| 394 |
|
|---|
| [cd0684d] | 395 | rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 90);
|
|---|
| 396 | if (rc != EOK) {
|
|---|
| [1d53a78] | 397 | ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | /* Class, subclass, prog IF, revision, length(incl \0) 47 */
|
|---|
| 401 | rc = snprintf(match_id_str, ID_MAX_STR_LEN,
|
|---|
| 402 | "pci/class=%02x&subclass=%02x&progif=%02x&revision=%02x",
|
|---|
| 403 | fun->class_code, fun->subclass_code, fun->prog_if, fun->revision);
|
|---|
| 404 | if (rc < 0) {
|
|---|
| 405 | ddf_msg(LVL_ERROR, "Failed creating match ID str: %s",
|
|---|
| [cd0684d] | 406 | str_error(rc));
|
|---|
| [8304889] | 407 | }
|
|---|
| [1d53a78] | 408 |
|
|---|
| 409 | rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 70);
|
|---|
| 410 | if (rc != EOK) {
|
|---|
| 411 | ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | /* Class, subclass, prog IF, length(incl \0) 35 */
|
|---|
| 415 | rc = snprintf(match_id_str, ID_MAX_STR_LEN,
|
|---|
| 416 | "pci/class=%02x&subclass=%02x&progif=%02x",
|
|---|
| 417 | fun->class_code, fun->subclass_code, fun->prog_if);
|
|---|
| 418 | if (rc < 0) {
|
|---|
| 419 | ddf_msg(LVL_ERROR, "Failed creating match ID str: %s",
|
|---|
| 420 | str_error(rc));
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 60);
|
|---|
| 424 | if (rc != EOK) {
|
|---|
| 425 | ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 | /* Class, subclass, length(incl \0) 25 */
|
|---|
| 429 | rc = snprintf(match_id_str, ID_MAX_STR_LEN,
|
|---|
| 430 | "pci/class=%02x&subclass=%02x",
|
|---|
| 431 | fun->class_code, fun->subclass_code);
|
|---|
| 432 | if (rc < 0) {
|
|---|
| 433 | ddf_msg(LVL_ERROR, "Failed creating match ID str: %s",
|
|---|
| 434 | str_error(rc));
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 50);
|
|---|
| 438 | if (rc != EOK) {
|
|---|
| 439 | ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | /* Class, length(incl \0) 13 */
|
|---|
| 443 | rc = snprintf(match_id_str, ID_MAX_STR_LEN, "pci/class=%02x",
|
|---|
| 444 | fun->class_code);
|
|---|
| 445 | if (rc < 0) {
|
|---|
| 446 | ddf_msg(LVL_ERROR, "Failed creating match ID str: %s",
|
|---|
| 447 | str_error(rc));
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 40);
|
|---|
| 451 | if (rc != EOK) {
|
|---|
| 452 | ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | /* TODO add subsys ids, but those exist only in header type 0 */
|
|---|
| [89ce401a] | 456 | }
|
|---|
| 457 |
|
|---|
| [68414f4a] | 458 | void pci_add_range(pci_fun_t *fun, uint64_t range_addr, size_t range_size,
|
|---|
| 459 | bool io)
|
|---|
| [d1fc8f0] | 460 | {
|
|---|
| [68414f4a] | 461 | hw_resource_list_t *hw_res_list = &fun->hw_resources;
|
|---|
| [3a5909f] | 462 | hw_resource_t *hw_resources = hw_res_list->resources;
|
|---|
| [663f41c4] | 463 | size_t count = hw_res_list->count;
|
|---|
| [3a5909f] | 464 |
|
|---|
| [8304889] | 465 | assert(hw_resources != NULL);
|
|---|
| [3a5909f] | 466 | assert(count < PCI_MAX_HW_RES);
|
|---|
| 467 |
|
|---|
| 468 | if (io) {
|
|---|
| 469 | hw_resources[count].type = IO_RANGE;
|
|---|
| 470 | hw_resources[count].res.io_range.address = range_addr;
|
|---|
| [663f41c4] | 471 | hw_resources[count].res.io_range.size = range_size;
|
|---|
| [9e470c0] | 472 | hw_resources[count].res.io_range.relative = true;
|
|---|
| [663f41c4] | 473 | hw_resources[count].res.io_range.endianness = LITTLE_ENDIAN;
|
|---|
| [3a5909f] | 474 | } else {
|
|---|
| 475 | hw_resources[count].type = MEM_RANGE;
|
|---|
| 476 | hw_resources[count].res.mem_range.address = range_addr;
|
|---|
| [663f41c4] | 477 | hw_resources[count].res.mem_range.size = range_size;
|
|---|
| [9e470c0] | 478 | hw_resources[count].res.mem_range.relative = false;
|
|---|
| [3a5909f] | 479 | hw_resources[count].res.mem_range.endianness = LITTLE_ENDIAN;
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| [663f41c4] | 482 | hw_res_list->count++;
|
|---|
| [d1fc8f0] | 483 | }
|
|---|
| 484 |
|
|---|
| [663f41c4] | 485 | /** Read the base address register (BAR) of the device and if it contains valid
|
|---|
| 486 | * address add it to the devices hw resource list.
|
|---|
| 487 | *
|
|---|
| [68414f4a] | 488 | * @param fun PCI function
|
|---|
| [663f41c4] | 489 | * @param addr The address of the BAR in the PCI configuration address space of
|
|---|
| [68414f4a] | 490 | * the device
|
|---|
| 491 | * @return The addr the address of the BAR which should be read next
|
|---|
| [d1fc8f0] | 492 | */
|
|---|
| [68414f4a] | 493 | int pci_read_bar(pci_fun_t *fun, int addr)
|
|---|
| [bab6388] | 494 | {
|
|---|
| [663f41c4] | 495 | /* Value of the BAR */
|
|---|
| [e8d6ce2] | 496 | uint32_t val;
|
|---|
| 497 | uint32_t bar;
|
|---|
| 498 | uint32_t mask;
|
|---|
| 499 |
|
|---|
| [663f41c4] | 500 | /* IO space address */
|
|---|
| [d1fc8f0] | 501 | bool io;
|
|---|
| [663f41c4] | 502 | /* 64-bit wide address */
|
|---|
| [d93aafed] | 503 | bool addrw64;
|
|---|
| [d1fc8f0] | 504 |
|
|---|
| [663f41c4] | 505 | /* Size of the io or memory range specified by the BAR */
|
|---|
| [d1fc8f0] | 506 | size_t range_size;
|
|---|
| [663f41c4] | 507 | /* Beginning of the io or memory range specified by the BAR */
|
|---|
| [d1fc8f0] | 508 | uint64_t range_addr;
|
|---|
| 509 |
|
|---|
| [663f41c4] | 510 | /* Get the value of the BAR. */
|
|---|
| [8b1e15ac] | 511 | val = pci_conf_read_32(fun, addr);
|
|---|
| [ad6857c] | 512 |
|
|---|
| 513 | #define IO_MASK (~0x3)
|
|---|
| 514 | #define MEM_MASK (~0xf)
|
|---|
| [d1fc8f0] | 515 |
|
|---|
| [c81132d] | 516 | io = (val & 1) != 0;
|
|---|
| [d1fc8f0] | 517 | if (io) {
|
|---|
| [d93aafed] | 518 | addrw64 = false;
|
|---|
| [ad6857c] | 519 | mask = IO_MASK;
|
|---|
| [d1fc8f0] | 520 | } else {
|
|---|
| [ad6857c] | 521 | mask = MEM_MASK;
|
|---|
| [d1fc8f0] | 522 | switch ((val >> 1) & 3) {
|
|---|
| 523 | case 0:
|
|---|
| [d93aafed] | 524 | addrw64 = false;
|
|---|
| [d1fc8f0] | 525 | break;
|
|---|
| 526 | case 2:
|
|---|
| [d93aafed] | 527 | addrw64 = true;
|
|---|
| [d1fc8f0] | 528 | break;
|
|---|
| 529 | default:
|
|---|
| [663f41c4] | 530 | /* reserved, go to the next BAR */
|
|---|
| 531 | return addr + 4;
|
|---|
| [d1fc8f0] | 532 | }
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| [663f41c4] | 535 | /* Get the address mask. */
|
|---|
| [8b1e15ac] | 536 | pci_conf_write_32(fun, addr, 0xffffffff);
|
|---|
| [e8d6ce2] | 537 | bar = pci_conf_read_32(fun, addr);
|
|---|
| 538 |
|
|---|
| 539 | /*
|
|---|
| 540 | * Unimplemented BARs read back as all 0's.
|
|---|
| 541 | */
|
|---|
| 542 | if (!bar)
|
|---|
| 543 | return addr + (addrw64 ? 8 : 4);
|
|---|
| 544 |
|
|---|
| 545 | mask &= bar;
|
|---|
| 546 |
|
|---|
| [663f41c4] | 547 | /* Restore the original value. */
|
|---|
| [8b1e15ac] | 548 | pci_conf_write_32(fun, addr, val);
|
|---|
| 549 | val = pci_conf_read_32(fun, addr);
|
|---|
| [d1fc8f0] | 550 |
|
|---|
| [3a5909f] | 551 | range_size = pci_bar_mask_to_size(mask);
|
|---|
| [d1fc8f0] | 552 |
|
|---|
| [d93aafed] | 553 | if (addrw64) {
|
|---|
| [8b1e15ac] | 554 | range_addr = ((uint64_t)pci_conf_read_32(fun, addr + 4) << 32) |
|
|---|
| [663f41c4] | 555 | (val & 0xfffffff0);
|
|---|
| [d1fc8f0] | 556 | } else {
|
|---|
| 557 | range_addr = (val & 0xfffffff0);
|
|---|
| [663f41c4] | 558 | }
|
|---|
| 559 |
|
|---|
| [d93aafed] | 560 | if (range_addr != 0) {
|
|---|
| [fc51296] | 561 | ddf_msg(LVL_DEBUG, "Function %s : address = %" PRIx64
|
|---|
| [56fd7cf] | 562 | ", size = %x", ddf_fun_get_name(fun->fnode), range_addr,
|
|---|
| [fc51296] | 563 | (unsigned int) range_size);
|
|---|
| [d1fc8f0] | 564 | }
|
|---|
| 565 |
|
|---|
| [8b1e15ac] | 566 | pci_add_range(fun, range_addr, range_size, io);
|
|---|
| [d1fc8f0] | 567 |
|
|---|
| [d93aafed] | 568 | if (addrw64)
|
|---|
| [d1fc8f0] | 569 | return addr + 8;
|
|---|
| [663f41c4] | 570 |
|
|---|
| 571 | return addr + 4;
|
|---|
| [d1fc8f0] | 572 | }
|
|---|
| 573 |
|
|---|
| [68414f4a] | 574 | void pci_add_interrupt(pci_fun_t *fun, int irq)
|
|---|
| [d1fc8f0] | 575 | {
|
|---|
| [68414f4a] | 576 | hw_resource_list_t *hw_res_list = &fun->hw_resources;
|
|---|
| [663f41c4] | 577 | hw_resource_t *hw_resources = hw_res_list->resources;
|
|---|
| 578 | size_t count = hw_res_list->count;
|
|---|
| [d1fc8f0] | 579 |
|
|---|
| [3a5909f] | 580 | assert(NULL != hw_resources);
|
|---|
| 581 | assert(count < PCI_MAX_HW_RES);
|
|---|
| 582 |
|
|---|
| 583 | hw_resources[count].type = INTERRUPT;
|
|---|
| 584 | hw_resources[count].res.interrupt.irq = irq;
|
|---|
| 585 |
|
|---|
| [663f41c4] | 586 | hw_res_list->count++;
|
|---|
| [3a5909f] | 587 |
|
|---|
| [56fd7cf] | 588 | ddf_msg(LVL_NOTE, "Function %s uses irq %x.", ddf_fun_get_name(fun->fnode), irq);
|
|---|
| [3a5909f] | 589 | }
|
|---|
| 590 |
|
|---|
| [68414f4a] | 591 | void pci_read_interrupt(pci_fun_t *fun)
|
|---|
| [3a5909f] | 592 | {
|
|---|
| [8b1e15ac] | 593 | uint8_t irq = pci_conf_read_8(fun, PCI_BRIDGE_INT_LINE);
|
|---|
| [65f77f4] | 594 | uint8_t pin = pci_conf_read_8(fun, PCI_BRIDGE_INT_PIN);
|
|---|
| 595 |
|
|---|
| 596 | if (pin != 0 && irq != 0xff)
|
|---|
| [8b1e15ac] | 597 | pci_add_interrupt(fun, irq);
|
|---|
| [d1fc8f0] | 598 | }
|
|---|
| 599 |
|
|---|
| 600 | /** Enumerate (recursively) and register the devices connected to a pci bus.
|
|---|
| [663f41c4] | 601 | *
|
|---|
| [68414f4a] | 602 | * @param bus Host-to-PCI bridge
|
|---|
| 603 | * @param bus_num Bus number
|
|---|
| [d1fc8f0] | 604 | */
|
|---|
| [68414f4a] | 605 | void pci_bus_scan(pci_bus_t *bus, int bus_num)
|
|---|
| [5e598e0] | 606 | {
|
|---|
| [97a62fe] | 607 | pci_fun_t *fun;
|
|---|
| [56fd7cf] | 608 | int rc;
|
|---|
| [5e598e0] | 609 |
|
|---|
| 610 | int child_bus = 0;
|
|---|
| 611 | int dnum, fnum;
|
|---|
| 612 | bool multi;
|
|---|
| [8b1e15ac] | 613 | uint8_t header_type;
|
|---|
| [bab6388] | 614 |
|
|---|
| [5e598e0] | 615 | for (dnum = 0; dnum < 32; dnum++) {
|
|---|
| 616 | multi = true;
|
|---|
| 617 | for (fnum = 0; multi && fnum < 8; fnum++) {
|
|---|
| [56fd7cf] | 618 | fun = pci_fun_new(bus);
|
|---|
| 619 |
|
|---|
| [68414f4a] | 620 | pci_fun_init(fun, bus_num, dnum, fnum);
|
|---|
| 621 | if (fun->vendor_id == 0xffff) {
|
|---|
| [56fd7cf] | 622 | pci_fun_delete(fun);
|
|---|
| [663f41c4] | 623 | /*
|
|---|
| 624 | * The device is not present, go on scanning the
|
|---|
| 625 | * bus.
|
|---|
| 626 | */
|
|---|
| 627 | if (fnum == 0)
|
|---|
| [5e598e0] | 628 | break;
|
|---|
| [663f41c4] | 629 | else
|
|---|
| 630 | continue;
|
|---|
| [5e598e0] | 631 | }
|
|---|
| [663f41c4] | 632 |
|
|---|
| [8b1e15ac] | 633 | header_type = pci_conf_read_8(fun, PCI_HEADER_TYPE);
|
|---|
| [5e598e0] | 634 | if (fnum == 0) {
|
|---|
| [663f41c4] | 635 | /* Is the device multifunction? */
|
|---|
| 636 | multi = header_type >> 7;
|
|---|
| [5e598e0] | 637 | }
|
|---|
| [663f41c4] | 638 | /* Clear the multifunction bit. */
|
|---|
| 639 | header_type = header_type & 0x7F;
|
|---|
| [5e598e0] | 640 |
|
|---|
| [97a62fe] | 641 | char *fun_name = pci_fun_create_name(fun);
|
|---|
| 642 | if (fun_name == NULL) {
|
|---|
| [ebcb05a] | 643 | ddf_msg(LVL_ERROR, "Out of memory.");
|
|---|
| [56fd7cf] | 644 | pci_fun_delete(fun);
|
|---|
| [97a62fe] | 645 | return;
|
|---|
| 646 | }
|
|---|
| 647 |
|
|---|
| [56fd7cf] | 648 | rc = ddf_fun_set_name(fun->fnode, fun_name);
|
|---|
| [cb94e69b] | 649 | free(fun_name);
|
|---|
| [56fd7cf] | 650 | if (rc != EOK) {
|
|---|
| 651 | ddf_msg(LVL_ERROR, "Failed setting function name.");
|
|---|
| 652 | pci_fun_delete(fun);
|
|---|
| [97a62fe] | 653 | return;
|
|---|
| 654 | }
|
|---|
| [3a5909f] | 655 |
|
|---|
| [8b1e15ac] | 656 | pci_alloc_resource_list(fun);
|
|---|
| 657 | pci_read_bars(fun);
|
|---|
| 658 | pci_read_interrupt(fun);
|
|---|
| [6dbc500] | 659 |
|
|---|
| 660 | /* Propagate the PIO window to the function. */
|
|---|
| 661 | fun->pio_window = bus->pio_win;
|
|---|
| [89ce401a] | 662 |
|
|---|
| [56fd7cf] | 663 | ddf_fun_set_ops(fun->fnode, &pci_fun_ops);
|
|---|
| [89ce401a] | 664 |
|
|---|
| [ebcb05a] | 665 | ddf_msg(LVL_DEBUG, "Adding new function %s.",
|
|---|
| [56fd7cf] | 666 | ddf_fun_get_name(fun->fnode));
|
|---|
| [92d5279] | 667 |
|
|---|
| [68414f4a] | 668 | pci_fun_create_match_ids(fun);
|
|---|
| [89ce401a] | 669 |
|
|---|
| [56fd7cf] | 670 | if (ddf_fun_bind(fun->fnode) != EOK) {
|
|---|
| [8b1e15ac] | 671 | pci_clean_resource_list(fun);
|
|---|
| [56fd7cf] | 672 | pci_fun_delete(fun);
|
|---|
| [89ce401a] | 673 | continue;
|
|---|
| 674 | }
|
|---|
| [5e598e0] | 675 |
|
|---|
| [663f41c4] | 676 | if (header_type == PCI_HEADER_TYPE_BRIDGE ||
|
|---|
| [8304889] | 677 | header_type == PCI_HEADER_TYPE_CARDBUS) {
|
|---|
| [8b1e15ac] | 678 | child_bus = pci_conf_read_8(fun,
|
|---|
| [663f41c4] | 679 | PCI_BRIDGE_SEC_BUS_NUM);
|
|---|
| [fc51296] | 680 | ddf_msg(LVL_DEBUG, "Device is pci-to-pci "
|
|---|
| [ebcb05a] | 681 | "bridge, secondary bus number = %d.",
|
|---|
| [fc51296] | 682 | bus_num);
|
|---|
| [8304889] | 683 | if (child_bus > bus_num)
|
|---|
| [68414f4a] | 684 | pci_bus_scan(bus, child_bus);
|
|---|
| [5e598e0] | 685 | }
|
|---|
| 686 | }
|
|---|
| 687 | }
|
|---|
| 688 | }
|
|---|
| [8c06905] | 689 |
|
|---|
| [0c0f823b] | 690 | static int pci_dev_add(ddf_dev_t *dnode)
|
|---|
| [8c06905] | 691 | {
|
|---|
| [6dbc500] | 692 | hw_resource_list_t hw_resources;
|
|---|
| [97a62fe] | 693 | pci_bus_t *bus = NULL;
|
|---|
| [83a2f43] | 694 | ddf_fun_t *ctl = NULL;
|
|---|
| [97a62fe] | 695 | bool got_res = false;
|
|---|
| [56fd7cf] | 696 | async_sess_t *sess;
|
|---|
| [be942bc] | 697 | int rc;
|
|---|
| [68414f4a] | 698 |
|
|---|
| [0c0f823b] | 699 | ddf_msg(LVL_DEBUG, "pci_dev_add");
|
|---|
| [8c06905] | 700 |
|
|---|
| [5f6e25e] | 701 | bus = ddf_dev_data_alloc(dnode, sizeof(pci_bus_t));
|
|---|
| [68414f4a] | 702 | if (bus == NULL) {
|
|---|
| [0c0f823b] | 703 | ddf_msg(LVL_ERROR, "pci_dev_add allocation failed.");
|
|---|
| [97a62fe] | 704 | rc = ENOMEM;
|
|---|
| 705 | goto fail;
|
|---|
| [663f41c4] | 706 | }
|
|---|
| [5f6e25e] | 707 | fibril_mutex_initialize(&bus->conf_mutex);
|
|---|
| 708 |
|
|---|
| [68414f4a] | 709 | bus->dnode = dnode;
|
|---|
| [8c06905] | 710 |
|
|---|
| [2fd26bb] | 711 | sess = ddf_dev_parent_sess_get(dnode);
|
|---|
| [56fd7cf] | 712 | if (sess == NULL) {
|
|---|
| [0c0f823b] | 713 | ddf_msg(LVL_ERROR, "pci_dev_add failed to connect to the "
|
|---|
| [79ae36dd] | 714 | "parent driver.");
|
|---|
| 715 | rc = ENOENT;
|
|---|
| [97a62fe] | 716 | goto fail;
|
|---|
| [8c06905] | 717 | }
|
|---|
| [6dbc500] | 718 |
|
|---|
| 719 | rc = pio_window_get(sess, &bus->pio_win);
|
|---|
| 720 | if (rc != EOK) {
|
|---|
| 721 | ddf_msg(LVL_ERROR, "pci_dev_add failed to get PIO window "
|
|---|
| 722 | "for the device.");
|
|---|
| 723 | goto fail;
|
|---|
| 724 | }
|
|---|
| [8c06905] | 725 |
|
|---|
| [56fd7cf] | 726 | rc = hw_res_get_resource_list(sess, &hw_resources);
|
|---|
| [be942bc] | 727 | if (rc != EOK) {
|
|---|
| [0c0f823b] | 728 | ddf_msg(LVL_ERROR, "pci_dev_add failed to get hw resources "
|
|---|
| [ebcb05a] | 729 | "for the device.");
|
|---|
| [97a62fe] | 730 | goto fail;
|
|---|
| [bab6388] | 731 | }
|
|---|
| [97a62fe] | 732 | got_res = true;
|
|---|
| [8c06905] | 733 |
|
|---|
| 734 |
|
|---|
| [92d5279] | 735 | assert(hw_resources.count >= 1);
|
|---|
| 736 |
|
|---|
| 737 | if (hw_resources.count == 1) {
|
|---|
| 738 | assert(hw_resources.resources[0].type == MEM_RANGE);
|
|---|
| 739 |
|
|---|
| 740 | ddf_msg(LVL_DEBUG, "conf_addr_space = %" PRIx64 ".",
|
|---|
| 741 | hw_resources.resources[0].res.mem_range.address);
|
|---|
| 742 |
|
|---|
| 743 | if (pio_enable_resource(&bus->pio_win,
|
|---|
| 744 | &hw_resources.resources[0],
|
|---|
| 745 | (void **) &bus->conf_space)) {
|
|---|
| 746 | ddf_msg(LVL_ERROR,
|
|---|
| 747 | "Failed to map configuration space.");
|
|---|
| 748 | rc = EADDRNOTAVAIL;
|
|---|
| 749 | goto fail;
|
|---|
| 750 | }
|
|---|
| 751 |
|
|---|
| 752 | } else {
|
|---|
| 753 | assert(hw_resources.resources[0].type == IO_RANGE);
|
|---|
| 754 | assert(hw_resources.resources[0].res.io_range.size >= 4);
|
|---|
| 755 |
|
|---|
| 756 | assert(hw_resources.resources[1].type == IO_RANGE);
|
|---|
| 757 | assert(hw_resources.resources[1].res.io_range.size >= 4);
|
|---|
| 758 |
|
|---|
| 759 | ddf_msg(LVL_DEBUG, "conf_addr = %" PRIx64 ".",
|
|---|
| 760 | hw_resources.resources[0].res.io_range.address);
|
|---|
| 761 | ddf_msg(LVL_DEBUG, "data_addr = %" PRIx64 ".",
|
|---|
| 762 | hw_resources.resources[1].res.io_range.address);
|
|---|
| 763 |
|
|---|
| 764 | if (pio_enable_resource(&bus->pio_win,
|
|---|
| 765 | &hw_resources.resources[0],
|
|---|
| 766 | (void **) &bus->conf_addr_reg)) {
|
|---|
| 767 | ddf_msg(LVL_ERROR,
|
|---|
| 768 | "Failed to enable configuration ports.");
|
|---|
| 769 | rc = EADDRNOTAVAIL;
|
|---|
| 770 | goto fail;
|
|---|
| 771 | }
|
|---|
| 772 | if (pio_enable_resource(&bus->pio_win,
|
|---|
| 773 | &hw_resources.resources[1],
|
|---|
| 774 | (void **) &bus->conf_data_reg)) {
|
|---|
| 775 | ddf_msg(LVL_ERROR,
|
|---|
| 776 | "Failed to enable configuration ports.");
|
|---|
| 777 | rc = EADDRNOTAVAIL;
|
|---|
| 778 | goto fail;
|
|---|
| 779 | }
|
|---|
| [230385c] | 780 | }
|
|---|
| [8c06905] | 781 |
|
|---|
| [68414f4a] | 782 | /* Make the bus device more visible. It has no use yet. */
|
|---|
| [ebcb05a] | 783 | ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
|
|---|
| [68414f4a] | 784 |
|
|---|
| [97a62fe] | 785 | ctl = ddf_fun_create(bus->dnode, fun_exposed, "ctl");
|
|---|
| 786 | if (ctl == NULL) {
|
|---|
| [ebcb05a] | 787 | ddf_msg(LVL_ERROR, "Failed creating control function.");
|
|---|
| [97a62fe] | 788 | rc = ENOMEM;
|
|---|
| 789 | goto fail;
|
|---|
| 790 | }
|
|---|
| 791 |
|
|---|
| 792 | rc = ddf_fun_bind(ctl);
|
|---|
| 793 | if (rc != EOK) {
|
|---|
| [ebcb05a] | 794 | ddf_msg(LVL_ERROR, "Failed binding control function.");
|
|---|
| [97a62fe] | 795 | goto fail;
|
|---|
| 796 | }
|
|---|
| [8c06905] | 797 |
|
|---|
| [68414f4a] | 798 | /* Enumerate functions. */
|
|---|
| [ebcb05a] | 799 | ddf_msg(LVL_DEBUG, "Scanning the bus");
|
|---|
| [68414f4a] | 800 | pci_bus_scan(bus, 0);
|
|---|
| [8c06905] | 801 |
|
|---|
| [f724e82] | 802 | hw_res_clean_resource_list(&hw_resources);
|
|---|
| [8c06905] | 803 |
|
|---|
| [df747b9c] | 804 | return EOK;
|
|---|
| [97a62fe] | 805 |
|
|---|
| 806 | fail:
|
|---|
| 807 | if (got_res)
|
|---|
| 808 | hw_res_clean_resource_list(&hw_resources);
|
|---|
| [79ae36dd] | 809 |
|
|---|
| [97a62fe] | 810 | if (ctl != NULL)
|
|---|
| 811 | ddf_fun_destroy(ctl);
|
|---|
| [79ae36dd] | 812 |
|
|---|
| [97a62fe] | 813 | return rc;
|
|---|
| [8c06905] | 814 | }
|
|---|
| 815 |
|
|---|
| [f278930] | 816 | static int pci_fun_online(ddf_fun_t *fun)
|
|---|
| 817 | {
|
|---|
| 818 | ddf_msg(LVL_DEBUG, "pci_fun_online()");
|
|---|
| 819 | return ddf_fun_online(fun);
|
|---|
| 820 | }
|
|---|
| 821 |
|
|---|
| 822 | static int pci_fun_offline(ddf_fun_t *fun)
|
|---|
| 823 | {
|
|---|
| 824 | ddf_msg(LVL_DEBUG, "pci_fun_offline()");
|
|---|
| 825 | return ddf_fun_offline(fun);
|
|---|
| 826 | }
|
|---|
| 827 |
|
|---|
| [663f41c4] | 828 | static void pciintel_init(void)
|
|---|
| [3843ecb] | 829 | {
|
|---|
| [267f235] | 830 | ddf_log_init(NAME);
|
|---|
| [3843ecb] | 831 | }
|
|---|
| 832 |
|
|---|
| [97a62fe] | 833 | pci_fun_t *pci_fun_new(pci_bus_t *bus)
|
|---|
| [713a4b9] | 834 | {
|
|---|
| [97a62fe] | 835 | pci_fun_t *fun;
|
|---|
| [56fd7cf] | 836 | ddf_fun_t *fnode;
|
|---|
| [713a4b9] | 837 |
|
|---|
| [56fd7cf] | 838 | fnode = ddf_fun_create(bus->dnode, fun_inner, NULL);
|
|---|
| 839 | if (fnode == NULL)
|
|---|
| 840 | return NULL;
|
|---|
| 841 |
|
|---|
| 842 | fun = ddf_fun_data_alloc(fnode, sizeof(pci_fun_t));
|
|---|
| [97a62fe] | 843 | if (fun == NULL)
|
|---|
| 844 | return NULL;
|
|---|
| 845 |
|
|---|
| 846 | fun->busptr = bus;
|
|---|
| [56fd7cf] | 847 | fun->fnode = fnode;
|
|---|
| [97a62fe] | 848 | return fun;
|
|---|
| [713a4b9] | 849 | }
|
|---|
| 850 |
|
|---|
| [68414f4a] | 851 | void pci_fun_init(pci_fun_t *fun, int bus, int dev, int fn)
|
|---|
| [713a4b9] | 852 | {
|
|---|
| [68414f4a] | 853 | fun->bus = bus;
|
|---|
| 854 | fun->dev = dev;
|
|---|
| 855 | fun->fn = fn;
|
|---|
| [1d53a78] | 856 | fun->vendor_id = pci_conf_read_16(fun, PCI_VENDOR_ID);
|
|---|
| 857 | fun->device_id = pci_conf_read_16(fun, PCI_DEVICE_ID);
|
|---|
| [c90aed4] | 858 |
|
|---|
| 859 | /* Explicitly enable PCI bus mastering */
|
|---|
| 860 | fun->command = pci_conf_read_16(fun, PCI_COMMAND) |
|
|---|
| 861 | PCI_COMMAND_MASTER;
|
|---|
| 862 | pci_conf_write_16(fun, PCI_COMMAND, fun->command);
|
|---|
| 863 |
|
|---|
| [1d53a78] | 864 | fun->class_code = pci_conf_read_8(fun, PCI_BASE_CLASS);
|
|---|
| 865 | fun->subclass_code = pci_conf_read_8(fun, PCI_SUB_CLASS);
|
|---|
| 866 | fun->prog_if = pci_conf_read_8(fun, PCI_PROG_IF);
|
|---|
| 867 | fun->revision = pci_conf_read_8(fun, PCI_REVISION_ID);
|
|---|
| [713a4b9] | 868 | }
|
|---|
| 869 |
|
|---|
| [68414f4a] | 870 | void pci_fun_delete(pci_fun_t *fun)
|
|---|
| [713a4b9] | 871 | {
|
|---|
| [bab6388] | 872 | hw_res_clean_resource_list(&fun->hw_resources);
|
|---|
| [56fd7cf] | 873 | if (fun->fnode != NULL)
|
|---|
| 874 | ddf_fun_destroy(fun->fnode);
|
|---|
| [713a4b9] | 875 | }
|
|---|
| 876 |
|
|---|
| [97a62fe] | 877 | char *pci_fun_create_name(pci_fun_t *fun)
|
|---|
| [713a4b9] | 878 | {
|
|---|
| 879 | char *name = NULL;
|
|---|
| 880 |
|
|---|
| [68414f4a] | 881 | asprintf(&name, "%02x:%02x.%01x", fun->bus, fun->dev,
|
|---|
| 882 | fun->fn);
|
|---|
| [97a62fe] | 883 | return name;
|
|---|
| [713a4b9] | 884 | }
|
|---|
| 885 |
|
|---|
| [68414f4a] | 886 | bool pci_alloc_resource_list(pci_fun_t *fun)
|
|---|
| [713a4b9] | 887 | {
|
|---|
| [992b47ea] | 888 | fun->hw_resources.resources = fun->resources;
|
|---|
| 889 | return true;
|
|---|
| [713a4b9] | 890 | }
|
|---|
| 891 |
|
|---|
| [68414f4a] | 892 | void pci_clean_resource_list(pci_fun_t *fun)
|
|---|
| [713a4b9] | 893 | {
|
|---|
| [992b47ea] | 894 | fun->hw_resources.resources = NULL;
|
|---|
| [713a4b9] | 895 | }
|
|---|
| 896 |
|
|---|
| [68414f4a] | 897 | /** Read the base address registers (BARs) of the function and add the addresses
|
|---|
| 898 | * to its HW resource list.
|
|---|
| [713a4b9] | 899 | *
|
|---|
| [68414f4a] | 900 | * @param fun PCI function
|
|---|
| [713a4b9] | 901 | */
|
|---|
| [68414f4a] | 902 | void pci_read_bars(pci_fun_t *fun)
|
|---|
| [713a4b9] | 903 | {
|
|---|
| 904 | /*
|
|---|
| 905 | * Position of the BAR in the PCI configuration address space of the
|
|---|
| 906 | * device.
|
|---|
| 907 | */
|
|---|
| 908 | int addr = PCI_BASE_ADDR_0;
|
|---|
| 909 |
|
|---|
| 910 | while (addr <= PCI_BASE_ADDR_5)
|
|---|
| [8b1e15ac] | 911 | addr = pci_read_bar(fun, addr);
|
|---|
| [713a4b9] | 912 | }
|
|---|
| 913 |
|
|---|
| 914 | size_t pci_bar_mask_to_size(uint32_t mask)
|
|---|
| 915 | {
|
|---|
| [ad6857c] | 916 | size_t size = mask & ~(mask - 1);
|
|---|
| 917 | return size;
|
|---|
| [713a4b9] | 918 | }
|
|---|
| 919 |
|
|---|
| [8c06905] | 920 | int main(int argc, char *argv[])
|
|---|
| 921 | {
|
|---|
| [ebcb05a] | 922 | printf(NAME ": HelenOS PCI bus driver (Intel method 1).\n");
|
|---|
| [3843ecb] | 923 | pciintel_init();
|
|---|
| [83a2f43] | 924 | return ddf_driver_main(&pci_driver);
|
|---|
| [8c06905] | 925 | }
|
|---|
| 926 |
|
|---|
| 927 | /**
|
|---|
| 928 | * @}
|
|---|
| [472020fc] | 929 | */
|
|---|