[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>
|
---|
| 40 | #include <stdio.h>
|
---|
| 41 | #include <errno.h>
|
---|
| 42 | #include <bool.h>
|
---|
| 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 <devman.h>
|
---|
| 52 | #include <ipc/devman.h>
|
---|
| 53 | #include <ipc/dev_iface.h>
|
---|
[fb78ae72] | 54 | #include <ipc/irc.h>
|
---|
| 55 | #include <ipc/ns.h>
|
---|
| 56 | #include <ipc/services.h>
|
---|
| 57 | #include <sysinfo.h>
|
---|
[41b56084] | 58 | #include <ops/hw_res.h>
|
---|
[8c06905] | 59 | #include <device/hw_res.h>
|
---|
| 60 | #include <ddi.h>
|
---|
[5e598e0] | 61 | #include <libarch/ddi.h>
|
---|
[99e6bfb] | 62 | #include <pci_dev_iface.h>
|
---|
[5e598e0] | 63 |
|
---|
| 64 | #include "pci.h"
|
---|
[8c06905] | 65 |
|
---|
| 66 | #define NAME "pciintel"
|
---|
| 67 |
|
---|
[663f41c4] | 68 | #define CONF_ADDR(bus, dev, fn, reg) \
|
---|
| 69 | ((1 << 31) | (bus << 16) | (dev << 11) | (fn << 8) | (reg & ~3))
|
---|
[5e598e0] | 70 |
|
---|
[68414f4a] | 71 | /** Obtain PCI function soft-state from DDF function node */
|
---|
| 72 | #define PCI_FUN(fnode) ((pci_fun_t *) (fnode)->driver_data)
|
---|
| 73 |
|
---|
| 74 | /** Obtain PCI bus soft-state from DDF device node */
|
---|
| 75 | #define PCI_BUS(dnode) ((pci_bus_t *) (dnode)->driver_data)
|
---|
| 76 |
|
---|
| 77 | /** Obtain PCI bus soft-state from function soft-state */
|
---|
[97a62fe] | 78 | #define PCI_BUS_FROM_FUN(fun) ((fun)->busptr)
|
---|
[68414f4a] | 79 |
|
---|
[83a2f43] | 80 | static hw_resource_list_t *pciintel_get_resources(ddf_fun_t *fnode)
|
---|
[3843ecb] | 81 | {
|
---|
[68414f4a] | 82 | pci_fun_t *fun = PCI_FUN(fnode);
|
---|
[663f41c4] | 83 |
|
---|
[68414f4a] | 84 | if (fun == NULL)
|
---|
[3843ecb] | 85 | return NULL;
|
---|
[68414f4a] | 86 | return &fun->hw_resources;
|
---|
[3843ecb] | 87 | }
|
---|
| 88 |
|
---|
[83a2f43] | 89 | static bool pciintel_enable_interrupt(ddf_fun_t *fnode)
|
---|
[3843ecb] | 90 | {
|
---|
[fb78ae72] | 91 | /* This is an old ugly way, copied from ne2000 driver */
|
---|
[eb1a2f4] | 92 | assert(fnode);
|
---|
| 93 | pci_fun_t *dev_data = (pci_fun_t *) fnode->driver_data;
|
---|
[fb78ae72] | 94 |
|
---|
[91579d5] | 95 | sysarg_t apic;
|
---|
| 96 | sysarg_t i8259;
|
---|
[dc75234] | 97 |
|
---|
[51e5608] | 98 | int irc_phone = ENOTSUP;
|
---|
[fb78ae72] | 99 |
|
---|
[51e5608] | 100 | if (((sysinfo_get_value("apic", &apic) == EOK) && (apic))
|
---|
| 101 | || ((sysinfo_get_value("i8259", &i8259) == EOK) && (i8259))) {
|
---|
| 102 | irc_phone = service_connect_blocking(SERVICE_IRC, 0, 0);
|
---|
[fb78ae72] | 103 | }
|
---|
| 104 |
|
---|
[91579d5] | 105 | if (irc_phone < 0) {
|
---|
[fb78ae72] | 106 | return false;
|
---|
[91579d5] | 107 | }
|
---|
[fb78ae72] | 108 |
|
---|
[5857be2] | 109 | size_t i = 0;
|
---|
| 110 | hw_resource_list_t *res = &dev_data->hw_resources;
|
---|
| 111 | for (; i < res->count; i++) {
|
---|
| 112 | if (res->resources[i].type == INTERRUPT) {
|
---|
| 113 | const int irq = res->resources[i].res.interrupt.irq;
|
---|
| 114 | const int rc =
|
---|
| 115 | async_req_1_0(irc_phone, IRC_ENABLE_INTERRUPT, irq);
|
---|
[dc75234] | 116 | if (rc != EOK) {
|
---|
| 117 | async_hangup(irc_phone);
|
---|
| 118 | return false;
|
---|
| 119 | }
|
---|
[fb78ae72] | 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | async_hangup(irc_phone);
|
---|
| 124 | return true;
|
---|
[3843ecb] | 125 | }
|
---|
| 126 |
|
---|
[40a5d40] | 127 | static int pci_config_space_write_32(
|
---|
| 128 | ddf_fun_t *fun, uint32_t address, uint32_t data)
|
---|
| 129 | {
|
---|
| 130 | if (address > 252)
|
---|
| 131 | return EINVAL;
|
---|
| 132 | pci_conf_write_32(PCI_FUN(fun), address, data);
|
---|
| 133 | return EOK;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | static int pci_config_space_write_16(
|
---|
| 137 | ddf_fun_t *fun, uint32_t address, uint16_t data)
|
---|
[99e6bfb] | 138 | {
|
---|
| 139 | if (address > 254)
|
---|
| 140 | return EINVAL;
|
---|
| 141 | pci_conf_write_16(PCI_FUN(fun), address, data);
|
---|
| 142 | return EOK;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[40a5d40] | 145 | static int pci_config_space_write_8(
|
---|
| 146 | ddf_fun_t *fun, uint32_t address, uint8_t data)
|
---|
| 147 | {
|
---|
| 148 | if (address > 255)
|
---|
| 149 | return EINVAL;
|
---|
| 150 | pci_conf_write_8(PCI_FUN(fun), address, data);
|
---|
| 151 | return EOK;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | static int pci_config_space_read_32(
|
---|
| 155 | ddf_fun_t *fun, uint32_t address, uint32_t *data)
|
---|
| 156 | {
|
---|
| 157 | if (address > 252)
|
---|
| 158 | return EINVAL;
|
---|
| 159 | *data = pci_conf_read_32(PCI_FUN(fun), address);
|
---|
| 160 | return EOK;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | static int pci_config_space_read_16(
|
---|
| 164 | ddf_fun_t *fun, uint32_t address, uint16_t *data)
|
---|
| 165 | {
|
---|
| 166 | if (address > 254)
|
---|
| 167 | return EINVAL;
|
---|
| 168 | *data = pci_conf_read_16(PCI_FUN(fun), address);
|
---|
| 169 | return EOK;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | static int pci_config_space_read_8(
|
---|
| 173 | ddf_fun_t *fun, uint32_t address, uint8_t *data)
|
---|
| 174 | {
|
---|
| 175 | if (address > 255)
|
---|
| 176 | return EINVAL;
|
---|
| 177 | *data = pci_conf_read_8(PCI_FUN(fun), address);
|
---|
| 178 | return EOK;
|
---|
| 179 | }
|
---|
[99e6bfb] | 180 |
|
---|
[68414f4a] | 181 | static hw_res_ops_t pciintel_hw_res_ops = {
|
---|
| 182 | &pciintel_get_resources,
|
---|
| 183 | &pciintel_enable_interrupt
|
---|
[3843ecb] | 184 | };
|
---|
| 185 |
|
---|
[99e6bfb] | 186 | static pci_dev_iface_t pci_dev_ops = {
|
---|
[40a5d40] | 187 | .config_space_read_8 = &pci_config_space_read_8,
|
---|
| 188 | .config_space_read_16 = &pci_config_space_read_16,
|
---|
| 189 | .config_space_read_32 = &pci_config_space_read_32,
|
---|
| 190 | .config_space_write_8 = &pci_config_space_write_8,
|
---|
[99e6bfb] | 191 | .config_space_write_16 = &pci_config_space_write_16,
|
---|
[40a5d40] | 192 | .config_space_write_32 = &pci_config_space_write_32
|
---|
[99e6bfb] | 193 | };
|
---|
| 194 |
|
---|
| 195 | static ddf_dev_ops_t pci_fun_ops = {
|
---|
| 196 | .interfaces[HW_RES_DEV_IFACE] = &pciintel_hw_res_ops,
|
---|
| 197 | .interfaces[PCI_DEV_IFACE] = &pci_dev_ops
|
---|
| 198 | };
|
---|
[3843ecb] | 199 |
|
---|
[83a2f43] | 200 | static int pci_add_device(ddf_dev_t *);
|
---|
[3843ecb] | 201 |
|
---|
[68414f4a] | 202 | /** PCI bus driver standard operations */
|
---|
[8c06905] | 203 | static driver_ops_t pci_ops = {
|
---|
| 204 | .add_device = &pci_add_device
|
---|
| 205 | };
|
---|
| 206 |
|
---|
[68414f4a] | 207 | /** PCI bus driver structure */
|
---|
[8c06905] | 208 | static driver_t pci_driver = {
|
---|
| 209 | .name = NAME,
|
---|
| 210 | .driver_ops = &pci_ops
|
---|
| 211 | };
|
---|
| 212 |
|
---|
[68414f4a] | 213 | static pci_bus_t *pci_bus_new(void)
|
---|
[5e598e0] | 214 | {
|
---|
[68414f4a] | 215 | pci_bus_t *bus;
|
---|
[663f41c4] | 216 |
|
---|
[bab6388] | 217 | bus = (pci_bus_t *) calloc(1, sizeof(pci_bus_t));
|
---|
| 218 | if (bus == NULL)
|
---|
| 219 | return NULL;
|
---|
| 220 |
|
---|
| 221 | fibril_mutex_initialize(&bus->conf_mutex);
|
---|
[68414f4a] | 222 | return bus;
|
---|
[5e598e0] | 223 | }
|
---|
| 224 |
|
---|
[68414f4a] | 225 | static void pci_bus_delete(pci_bus_t *bus)
|
---|
[5e598e0] | 226 | {
|
---|
[bab6388] | 227 | assert(bus != NULL);
|
---|
[68414f4a] | 228 | free(bus);
|
---|
[5e598e0] | 229 | }
|
---|
| 230 |
|
---|
[68414f4a] | 231 | static void pci_conf_read(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
|
---|
[5e598e0] | 232 | {
|
---|
[68414f4a] | 233 | pci_bus_t *bus = PCI_BUS_FROM_FUN(fun);
|
---|
[5e598e0] | 234 |
|
---|
[68414f4a] | 235 | fibril_mutex_lock(&bus->conf_mutex);
|
---|
[5e598e0] | 236 |
|
---|
[663f41c4] | 237 | uint32_t conf_addr;
|
---|
[68414f4a] | 238 | conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
|
---|
| 239 | void *addr = bus->conf_data_port + (reg & 3);
|
---|
[5e598e0] | 240 |
|
---|
[68414f4a] | 241 | pio_write_32(bus->conf_addr_port, conf_addr);
|
---|
[5e598e0] | 242 |
|
---|
| 243 | switch (len) {
|
---|
[663f41c4] | 244 | case 1:
|
---|
| 245 | buf[0] = pio_read_8(addr);
|
---|
| 246 | break;
|
---|
| 247 | case 2:
|
---|
| 248 | ((uint16_t *) buf)[0] = pio_read_16(addr);
|
---|
| 249 | break;
|
---|
| 250 | case 4:
|
---|
| 251 | ((uint32_t *) buf)[0] = pio_read_32(addr);
|
---|
| 252 | break;
|
---|
[5e598e0] | 253 | }
|
---|
| 254 |
|
---|
[68414f4a] | 255 | fibril_mutex_unlock(&bus->conf_mutex);
|
---|
[5e598e0] | 256 | }
|
---|
| 257 |
|
---|
[68414f4a] | 258 | static void pci_conf_write(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
|
---|
[d1fc8f0] | 259 | {
|
---|
[68414f4a] | 260 | pci_bus_t *bus = PCI_BUS_FROM_FUN(fun);
|
---|
[d1fc8f0] | 261 |
|
---|
[68414f4a] | 262 | fibril_mutex_lock(&bus->conf_mutex);
|
---|
[d1fc8f0] | 263 |
|
---|
[663f41c4] | 264 | uint32_t conf_addr;
|
---|
[68414f4a] | 265 | conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
|
---|
| 266 | void *addr = bus->conf_data_port + (reg & 3);
|
---|
[d1fc8f0] | 267 |
|
---|
[68414f4a] | 268 | pio_write_32(bus->conf_addr_port, conf_addr);
|
---|
[d1fc8f0] | 269 |
|
---|
| 270 | switch (len) {
|
---|
[663f41c4] | 271 | case 1:
|
---|
| 272 | pio_write_8(addr, buf[0]);
|
---|
| 273 | break;
|
---|
| 274 | case 2:
|
---|
| 275 | pio_write_16(addr, ((uint16_t *) buf)[0]);
|
---|
| 276 | break;
|
---|
| 277 | case 4:
|
---|
| 278 | pio_write_32(addr, ((uint32_t *) buf)[0]);
|
---|
| 279 | break;
|
---|
[d1fc8f0] | 280 | }
|
---|
| 281 |
|
---|
[68414f4a] | 282 | fibril_mutex_unlock(&bus->conf_mutex);
|
---|
[d1fc8f0] | 283 | }
|
---|
| 284 |
|
---|
[68414f4a] | 285 | uint8_t pci_conf_read_8(pci_fun_t *fun, int reg)
|
---|
[5e598e0] | 286 | {
|
---|
| 287 | uint8_t res;
|
---|
[8b1e15ac] | 288 | pci_conf_read(fun, reg, &res, 1);
|
---|
[5e598e0] | 289 | return res;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[68414f4a] | 292 | uint16_t pci_conf_read_16(pci_fun_t *fun, int reg)
|
---|
[5e598e0] | 293 | {
|
---|
| 294 | uint16_t res;
|
---|
[8b1e15ac] | 295 | pci_conf_read(fun, reg, (uint8_t *) &res, 2);
|
---|
[5e598e0] | 296 | return res;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[68414f4a] | 299 | uint32_t pci_conf_read_32(pci_fun_t *fun, int reg)
|
---|
[5e598e0] | 300 | {
|
---|
| 301 | uint32_t res;
|
---|
[8b1e15ac] | 302 | pci_conf_read(fun, reg, (uint8_t *) &res, 4);
|
---|
[663f41c4] | 303 | return res;
|
---|
[5e598e0] | 304 | }
|
---|
| 305 |
|
---|
[68414f4a] | 306 | void pci_conf_write_8(pci_fun_t *fun, int reg, uint8_t val)
|
---|
[d1fc8f0] | 307 | {
|
---|
[8b1e15ac] | 308 | pci_conf_write(fun, reg, (uint8_t *) &val, 1);
|
---|
[d1fc8f0] | 309 | }
|
---|
| 310 |
|
---|
[68414f4a] | 311 | void pci_conf_write_16(pci_fun_t *fun, int reg, uint16_t val)
|
---|
[d1fc8f0] | 312 | {
|
---|
[8b1e15ac] | 313 | pci_conf_write(fun, reg, (uint8_t *) &val, 2);
|
---|
[d1fc8f0] | 314 | }
|
---|
| 315 |
|
---|
[68414f4a] | 316 | void pci_conf_write_32(pci_fun_t *fun, int reg, uint32_t val)
|
---|
[d1fc8f0] | 317 | {
|
---|
[8b1e15ac] | 318 | pci_conf_write(fun, reg, (uint8_t *) &val, 4);
|
---|
[d1fc8f0] | 319 | }
|
---|
| 320 |
|
---|
[68414f4a] | 321 | void pci_fun_create_match_ids(pci_fun_t *fun)
|
---|
[89ce401a] | 322 | {
|
---|
[663f41c4] | 323 | char *match_id_str;
|
---|
[cd0684d] | 324 | int rc;
|
---|
[663f41c4] | 325 |
|
---|
[cd0684d] | 326 | asprintf(&match_id_str, "pci/ven=%04x&dev=%04x",
|
---|
| 327 | fun->vendor_id, fun->device_id);
|
---|
| 328 |
|
---|
| 329 | if (match_id_str == NULL) {
|
---|
[ebcb05a] | 330 | ddf_msg(LVL_ERROR, "Out of memory creating match ID.");
|
---|
[cd0684d] | 331 | return;
|
---|
[8304889] | 332 | }
|
---|
| 333 |
|
---|
[cd0684d] | 334 | rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 90);
|
---|
| 335 | if (rc != EOK) {
|
---|
[ebcb05a] | 336 | ddf_msg(LVL_ERROR, "Failed adding match ID: %s",
|
---|
[cd0684d] | 337 | str_error(rc));
|
---|
[8304889] | 338 | }
|
---|
[bab6388] | 339 |
|
---|
[663f41c4] | 340 | /* TODO add more ids (with subsys ids, using class id etc.) */
|
---|
[89ce401a] | 341 | }
|
---|
| 342 |
|
---|
[68414f4a] | 343 | void pci_add_range(pci_fun_t *fun, uint64_t range_addr, size_t range_size,
|
---|
| 344 | bool io)
|
---|
[d1fc8f0] | 345 | {
|
---|
[68414f4a] | 346 | hw_resource_list_t *hw_res_list = &fun->hw_resources;
|
---|
[3a5909f] | 347 | hw_resource_t *hw_resources = hw_res_list->resources;
|
---|
[663f41c4] | 348 | size_t count = hw_res_list->count;
|
---|
[3a5909f] | 349 |
|
---|
[8304889] | 350 | assert(hw_resources != NULL);
|
---|
[3a5909f] | 351 | assert(count < PCI_MAX_HW_RES);
|
---|
| 352 |
|
---|
| 353 | if (io) {
|
---|
| 354 | hw_resources[count].type = IO_RANGE;
|
---|
| 355 | hw_resources[count].res.io_range.address = range_addr;
|
---|
[663f41c4] | 356 | hw_resources[count].res.io_range.size = range_size;
|
---|
| 357 | hw_resources[count].res.io_range.endianness = LITTLE_ENDIAN;
|
---|
[3a5909f] | 358 | } else {
|
---|
| 359 | hw_resources[count].type = MEM_RANGE;
|
---|
| 360 | hw_resources[count].res.mem_range.address = range_addr;
|
---|
[663f41c4] | 361 | hw_resources[count].res.mem_range.size = range_size;
|
---|
[3a5909f] | 362 | hw_resources[count].res.mem_range.endianness = LITTLE_ENDIAN;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
[663f41c4] | 365 | hw_res_list->count++;
|
---|
[d1fc8f0] | 366 | }
|
---|
| 367 |
|
---|
[663f41c4] | 368 | /** Read the base address register (BAR) of the device and if it contains valid
|
---|
| 369 | * address add it to the devices hw resource list.
|
---|
| 370 | *
|
---|
[68414f4a] | 371 | * @param fun PCI function
|
---|
[663f41c4] | 372 | * @param addr The address of the BAR in the PCI configuration address space of
|
---|
[68414f4a] | 373 | * the device
|
---|
| 374 | * @return The addr the address of the BAR which should be read next
|
---|
[d1fc8f0] | 375 | */
|
---|
[68414f4a] | 376 | int pci_read_bar(pci_fun_t *fun, int addr)
|
---|
[bab6388] | 377 | {
|
---|
[663f41c4] | 378 | /* Value of the BAR */
|
---|
[d1fc8f0] | 379 | uint32_t val, mask;
|
---|
[663f41c4] | 380 | /* IO space address */
|
---|
[d1fc8f0] | 381 | bool io;
|
---|
[663f41c4] | 382 | /* 64-bit wide address */
|
---|
[d93aafed] | 383 | bool addrw64;
|
---|
[d1fc8f0] | 384 |
|
---|
[663f41c4] | 385 | /* Size of the io or memory range specified by the BAR */
|
---|
[d1fc8f0] | 386 | size_t range_size;
|
---|
[663f41c4] | 387 | /* Beginning of the io or memory range specified by the BAR */
|
---|
[d1fc8f0] | 388 | uint64_t range_addr;
|
---|
| 389 |
|
---|
[663f41c4] | 390 | /* Get the value of the BAR. */
|
---|
[8b1e15ac] | 391 | val = pci_conf_read_32(fun, addr);
|
---|
[ad6857c] | 392 |
|
---|
| 393 | #define IO_MASK (~0x3)
|
---|
| 394 | #define MEM_MASK (~0xf)
|
---|
[d1fc8f0] | 395 |
|
---|
[663f41c4] | 396 | io = (bool) (val & 1);
|
---|
[d1fc8f0] | 397 | if (io) {
|
---|
[d93aafed] | 398 | addrw64 = false;
|
---|
[ad6857c] | 399 | mask = IO_MASK;
|
---|
[d1fc8f0] | 400 | } else {
|
---|
[ad6857c] | 401 | mask = MEM_MASK;
|
---|
[d1fc8f0] | 402 | switch ((val >> 1) & 3) {
|
---|
| 403 | case 0:
|
---|
[d93aafed] | 404 | addrw64 = false;
|
---|
[d1fc8f0] | 405 | break;
|
---|
| 406 | case 2:
|
---|
[d93aafed] | 407 | addrw64 = true;
|
---|
[d1fc8f0] | 408 | break;
|
---|
| 409 | default:
|
---|
[663f41c4] | 410 | /* reserved, go to the next BAR */
|
---|
| 411 | return addr + 4;
|
---|
[d1fc8f0] | 412 | }
|
---|
| 413 | }
|
---|
| 414 |
|
---|
[663f41c4] | 415 | /* Get the address mask. */
|
---|
[8b1e15ac] | 416 | pci_conf_write_32(fun, addr, 0xffffffff);
|
---|
[ad6857c] | 417 | mask &= pci_conf_read_32(fun, addr);
|
---|
[d1fc8f0] | 418 |
|
---|
[663f41c4] | 419 | /* Restore the original value. */
|
---|
[8b1e15ac] | 420 | pci_conf_write_32(fun, addr, val);
|
---|
| 421 | val = pci_conf_read_32(fun, addr);
|
---|
[d1fc8f0] | 422 |
|
---|
[3a5909f] | 423 | range_size = pci_bar_mask_to_size(mask);
|
---|
[d1fc8f0] | 424 |
|
---|
[d93aafed] | 425 | if (addrw64) {
|
---|
[8b1e15ac] | 426 | range_addr = ((uint64_t)pci_conf_read_32(fun, addr + 4) << 32) |
|
---|
[663f41c4] | 427 | (val & 0xfffffff0);
|
---|
[d1fc8f0] | 428 | } else {
|
---|
| 429 | range_addr = (val & 0xfffffff0);
|
---|
[663f41c4] | 430 | }
|
---|
| 431 |
|
---|
[d93aafed] | 432 | if (range_addr != 0) {
|
---|
[fc51296] | 433 | ddf_msg(LVL_DEBUG, "Function %s : address = %" PRIx64
|
---|
[ebcb05a] | 434 | ", size = %x", fun->fnode->name, range_addr,
|
---|
[fc51296] | 435 | (unsigned int) range_size);
|
---|
[d1fc8f0] | 436 | }
|
---|
| 437 |
|
---|
[8b1e15ac] | 438 | pci_add_range(fun, range_addr, range_size, io);
|
---|
[d1fc8f0] | 439 |
|
---|
[d93aafed] | 440 | if (addrw64)
|
---|
[d1fc8f0] | 441 | return addr + 8;
|
---|
[663f41c4] | 442 |
|
---|
| 443 | return addr + 4;
|
---|
[d1fc8f0] | 444 | }
|
---|
| 445 |
|
---|
[68414f4a] | 446 | void pci_add_interrupt(pci_fun_t *fun, int irq)
|
---|
[d1fc8f0] | 447 | {
|
---|
[68414f4a] | 448 | hw_resource_list_t *hw_res_list = &fun->hw_resources;
|
---|
[663f41c4] | 449 | hw_resource_t *hw_resources = hw_res_list->resources;
|
---|
| 450 | size_t count = hw_res_list->count;
|
---|
[d1fc8f0] | 451 |
|
---|
[3a5909f] | 452 | assert(NULL != hw_resources);
|
---|
| 453 | assert(count < PCI_MAX_HW_RES);
|
---|
| 454 |
|
---|
| 455 | hw_resources[count].type = INTERRUPT;
|
---|
| 456 | hw_resources[count].res.interrupt.irq = irq;
|
---|
| 457 |
|
---|
[663f41c4] | 458 | hw_res_list->count++;
|
---|
[3a5909f] | 459 |
|
---|
[ebcb05a] | 460 | ddf_msg(LVL_NOTE, "Function %s uses irq %x.", fun->fnode->name, irq);
|
---|
[3a5909f] | 461 | }
|
---|
| 462 |
|
---|
[68414f4a] | 463 | void pci_read_interrupt(pci_fun_t *fun)
|
---|
[3a5909f] | 464 | {
|
---|
[8b1e15ac] | 465 | uint8_t irq = pci_conf_read_8(fun, PCI_BRIDGE_INT_LINE);
|
---|
[8304889] | 466 | if (irq != 0xff)
|
---|
[8b1e15ac] | 467 | pci_add_interrupt(fun, irq);
|
---|
[d1fc8f0] | 468 | }
|
---|
| 469 |
|
---|
| 470 | /** Enumerate (recursively) and register the devices connected to a pci bus.
|
---|
[663f41c4] | 471 | *
|
---|
[68414f4a] | 472 | * @param bus Host-to-PCI bridge
|
---|
| 473 | * @param bus_num Bus number
|
---|
[d1fc8f0] | 474 | */
|
---|
[68414f4a] | 475 | void pci_bus_scan(pci_bus_t *bus, int bus_num)
|
---|
[5e598e0] | 476 | {
|
---|
[83a2f43] | 477 | ddf_fun_t *fnode;
|
---|
[97a62fe] | 478 | pci_fun_t *fun;
|
---|
[5e598e0] | 479 |
|
---|
| 480 | int child_bus = 0;
|
---|
| 481 | int dnum, fnum;
|
---|
| 482 | bool multi;
|
---|
[8b1e15ac] | 483 | uint8_t header_type;
|
---|
[bab6388] | 484 |
|
---|
[97a62fe] | 485 | fun = pci_fun_new(bus);
|
---|
[5e598e0] | 486 |
|
---|
| 487 | for (dnum = 0; dnum < 32; dnum++) {
|
---|
| 488 | multi = true;
|
---|
| 489 | for (fnum = 0; multi && fnum < 8; fnum++) {
|
---|
[68414f4a] | 490 | pci_fun_init(fun, bus_num, dnum, fnum);
|
---|
| 491 | fun->vendor_id = pci_conf_read_16(fun,
|
---|
[663f41c4] | 492 | PCI_VENDOR_ID);
|
---|
[68414f4a] | 493 | fun->device_id = pci_conf_read_16(fun,
|
---|
[663f41c4] | 494 | PCI_DEVICE_ID);
|
---|
[68414f4a] | 495 | if (fun->vendor_id == 0xffff) {
|
---|
[663f41c4] | 496 | /*
|
---|
| 497 | * The device is not present, go on scanning the
|
---|
| 498 | * bus.
|
---|
| 499 | */
|
---|
| 500 | if (fnum == 0)
|
---|
[5e598e0] | 501 | break;
|
---|
[663f41c4] | 502 | else
|
---|
| 503 | continue;
|
---|
[5e598e0] | 504 | }
|
---|
[663f41c4] | 505 |
|
---|
[8b1e15ac] | 506 | header_type = pci_conf_read_8(fun, PCI_HEADER_TYPE);
|
---|
[5e598e0] | 507 | if (fnum == 0) {
|
---|
[663f41c4] | 508 | /* Is the device multifunction? */
|
---|
| 509 | multi = header_type >> 7;
|
---|
[5e598e0] | 510 | }
|
---|
[663f41c4] | 511 | /* Clear the multifunction bit. */
|
---|
| 512 | header_type = header_type & 0x7F;
|
---|
[5e598e0] | 513 |
|
---|
[97a62fe] | 514 | char *fun_name = pci_fun_create_name(fun);
|
---|
| 515 | if (fun_name == NULL) {
|
---|
[ebcb05a] | 516 | ddf_msg(LVL_ERROR, "Out of memory.");
|
---|
[97a62fe] | 517 | return;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | fnode = ddf_fun_create(bus->dnode, fun_inner, fun_name);
|
---|
| 521 | if (fnode == NULL) {
|
---|
[ebcb05a] | 522 | ddf_msg(LVL_ERROR, "Failed creating function.");
|
---|
[97a62fe] | 523 | return;
|
---|
| 524 | }
|
---|
[3a5909f] | 525 |
|
---|
[97a62fe] | 526 | free(fun_name);
|
---|
| 527 | fun->fnode = fnode;
|
---|
[3a5909f] | 528 |
|
---|
[8b1e15ac] | 529 | pci_alloc_resource_list(fun);
|
---|
| 530 | pci_read_bars(fun);
|
---|
| 531 | pci_read_interrupt(fun);
|
---|
[89ce401a] | 532 |
|
---|
[68414f4a] | 533 | fnode->ops = &pci_fun_ops;
|
---|
[97a62fe] | 534 | fnode->driver_data = fun;
|
---|
[89ce401a] | 535 |
|
---|
[ebcb05a] | 536 | ddf_msg(LVL_DEBUG, "Adding new function %s.",
|
---|
[68414f4a] | 537 | fnode->name);
|
---|
[89ce401a] | 538 |
|
---|
[68414f4a] | 539 | pci_fun_create_match_ids(fun);
|
---|
[89ce401a] | 540 |
|
---|
[97a62fe] | 541 | if (ddf_fun_bind(fnode) != EOK) {
|
---|
[8b1e15ac] | 542 | pci_clean_resource_list(fun);
|
---|
[68414f4a] | 543 | clean_match_ids(&fnode->match_ids);
|
---|
| 544 | free((char *) fnode->name);
|
---|
| 545 | fnode->name = NULL;
|
---|
[89ce401a] | 546 | continue;
|
---|
| 547 | }
|
---|
[5e598e0] | 548 |
|
---|
[663f41c4] | 549 | if (header_type == PCI_HEADER_TYPE_BRIDGE ||
|
---|
[8304889] | 550 | header_type == PCI_HEADER_TYPE_CARDBUS) {
|
---|
[8b1e15ac] | 551 | child_bus = pci_conf_read_8(fun,
|
---|
[663f41c4] | 552 | PCI_BRIDGE_SEC_BUS_NUM);
|
---|
[fc51296] | 553 | ddf_msg(LVL_DEBUG, "Device is pci-to-pci "
|
---|
[ebcb05a] | 554 | "bridge, secondary bus number = %d.",
|
---|
[fc51296] | 555 | bus_num);
|
---|
[8304889] | 556 | if (child_bus > bus_num)
|
---|
[68414f4a] | 557 | pci_bus_scan(bus, child_bus);
|
---|
[5e598e0] | 558 | }
|
---|
| 559 |
|
---|
[97a62fe] | 560 | fun = pci_fun_new(bus);
|
---|
[5e598e0] | 561 | }
|
---|
| 562 | }
|
---|
| 563 |
|
---|
[68414f4a] | 564 | if (fun->vendor_id == 0xffff) {
|
---|
[8b1e15ac] | 565 | /* Free the auxiliary function structure. */
|
---|
[68414f4a] | 566 | pci_fun_delete(fun);
|
---|
[663f41c4] | 567 | }
|
---|
[5e598e0] | 568 | }
|
---|
[8c06905] | 569 |
|
---|
[83a2f43] | 570 | static int pci_add_device(ddf_dev_t *dnode)
|
---|
[8c06905] | 571 | {
|
---|
[97a62fe] | 572 | pci_bus_t *bus = NULL;
|
---|
[83a2f43] | 573 | ddf_fun_t *ctl = NULL;
|
---|
[97a62fe] | 574 | bool got_res = false;
|
---|
[be942bc] | 575 | int rc;
|
---|
[68414f4a] | 576 |
|
---|
[ebcb05a] | 577 | ddf_msg(LVL_DEBUG, "pci_add_device");
|
---|
[97a62fe] | 578 | dnode->parent_phone = -1;
|
---|
[8c06905] | 579 |
|
---|
[97a62fe] | 580 | bus = pci_bus_new();
|
---|
[68414f4a] | 581 | if (bus == NULL) {
|
---|
[ebcb05a] | 582 | ddf_msg(LVL_ERROR, "pci_add_device allocation failed.");
|
---|
[97a62fe] | 583 | rc = ENOMEM;
|
---|
| 584 | goto fail;
|
---|
[663f41c4] | 585 | }
|
---|
[68414f4a] | 586 | bus->dnode = dnode;
|
---|
| 587 | dnode->driver_data = bus;
|
---|
[8c06905] | 588 |
|
---|
[68414f4a] | 589 | dnode->parent_phone = devman_parent_device_connect(dnode->handle,
|
---|
[663f41c4] | 590 | IPC_FLAG_BLOCKING);
|
---|
[68414f4a] | 591 | if (dnode->parent_phone < 0) {
|
---|
[fc51296] | 592 | ddf_msg(LVL_ERROR, "pci_add_device failed to connect to the "
|
---|
[ebcb05a] | 593 | "parent's driver.");
|
---|
[97a62fe] | 594 | rc = dnode->parent_phone;
|
---|
| 595 | goto fail;
|
---|
[8c06905] | 596 | }
|
---|
| 597 |
|
---|
| 598 | hw_resource_list_t hw_resources;
|
---|
| 599 |
|
---|
[68414f4a] | 600 | rc = hw_res_get_resource_list(dnode->parent_phone, &hw_resources);
|
---|
[be942bc] | 601 | if (rc != EOK) {
|
---|
[fc51296] | 602 | ddf_msg(LVL_ERROR, "pci_add_device failed to get hw resources "
|
---|
[ebcb05a] | 603 | "for the device.");
|
---|
[97a62fe] | 604 | goto fail;
|
---|
[bab6388] | 605 | }
|
---|
[97a62fe] | 606 | got_res = true;
|
---|
[8c06905] | 607 |
|
---|
[ebcb05a] | 608 | ddf_msg(LVL_DEBUG, "conf_addr = %" PRIx64 ".",
|
---|
[663f41c4] | 609 | hw_resources.resources[0].res.io_range.address);
|
---|
[8c06905] | 610 |
|
---|
| 611 | assert(hw_resources.count > 0);
|
---|
[3a5909f] | 612 | assert(hw_resources.resources[0].type == IO_RANGE);
|
---|
| 613 | assert(hw_resources.resources[0].res.io_range.size == 8);
|
---|
[8c06905] | 614 |
|
---|
[68414f4a] | 615 | bus->conf_io_addr =
|
---|
[663f41c4] | 616 | (uint32_t) hw_resources.resources[0].res.io_range.address;
|
---|
[8c06905] | 617 |
|
---|
[68414f4a] | 618 | if (pio_enable((void *)(uintptr_t)bus->conf_io_addr, 8,
|
---|
| 619 | &bus->conf_addr_port)) {
|
---|
[ebcb05a] | 620 | ddf_msg(LVL_ERROR, "Failed to enable configuration ports.");
|
---|
[97a62fe] | 621 | rc = EADDRNOTAVAIL;
|
---|
| 622 | goto fail;
|
---|
[8c06905] | 623 | }
|
---|
[68414f4a] | 624 | bus->conf_data_port = (char *) bus->conf_addr_port + 4;
|
---|
[8c06905] | 625 |
|
---|
[68414f4a] | 626 | /* Make the bus device more visible. It has no use yet. */
|
---|
[ebcb05a] | 627 | ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
|
---|
[68414f4a] | 628 |
|
---|
[97a62fe] | 629 | ctl = ddf_fun_create(bus->dnode, fun_exposed, "ctl");
|
---|
| 630 | if (ctl == NULL) {
|
---|
[ebcb05a] | 631 | ddf_msg(LVL_ERROR, "Failed creating control function.");
|
---|
[97a62fe] | 632 | rc = ENOMEM;
|
---|
| 633 | goto fail;
|
---|
| 634 | }
|
---|
| 635 |
|
---|
| 636 | rc = ddf_fun_bind(ctl);
|
---|
| 637 | if (rc != EOK) {
|
---|
[ebcb05a] | 638 | ddf_msg(LVL_ERROR, "Failed binding control function.");
|
---|
[97a62fe] | 639 | goto fail;
|
---|
| 640 | }
|
---|
[8c06905] | 641 |
|
---|
[68414f4a] | 642 | /* Enumerate functions. */
|
---|
[ebcb05a] | 643 | ddf_msg(LVL_DEBUG, "Scanning the bus");
|
---|
[68414f4a] | 644 | pci_bus_scan(bus, 0);
|
---|
[8c06905] | 645 |
|
---|
[f724e82] | 646 | hw_res_clean_resource_list(&hw_resources);
|
---|
[8c06905] | 647 |
|
---|
[df747b9c] | 648 | return EOK;
|
---|
[97a62fe] | 649 |
|
---|
| 650 | fail:
|
---|
| 651 | if (bus != NULL)
|
---|
| 652 | pci_bus_delete(bus);
|
---|
| 653 | if (dnode->parent_phone >= 0)
|
---|
| 654 | async_hangup(dnode->parent_phone);
|
---|
| 655 | if (got_res)
|
---|
| 656 | hw_res_clean_resource_list(&hw_resources);
|
---|
| 657 | if (ctl != NULL)
|
---|
| 658 | ddf_fun_destroy(ctl);
|
---|
| 659 |
|
---|
| 660 | return rc;
|
---|
[8c06905] | 661 | }
|
---|
| 662 |
|
---|
[663f41c4] | 663 | static void pciintel_init(void)
|
---|
[3843ecb] | 664 | {
|
---|
[fc51296] | 665 | ddf_log_init(NAME, LVL_ERROR);
|
---|
[68414f4a] | 666 | pci_fun_ops.interfaces[HW_RES_DEV_IFACE] = &pciintel_hw_res_ops;
|
---|
[99e6bfb] | 667 | pci_fun_ops.interfaces[PCI_DEV_IFACE] = &pci_dev_ops;
|
---|
[3843ecb] | 668 | }
|
---|
| 669 |
|
---|
[97a62fe] | 670 | pci_fun_t *pci_fun_new(pci_bus_t *bus)
|
---|
[713a4b9] | 671 | {
|
---|
[97a62fe] | 672 | pci_fun_t *fun;
|
---|
[713a4b9] | 673 |
|
---|
[97a62fe] | 674 | fun = (pci_fun_t *) calloc(1, sizeof(pci_fun_t));
|
---|
| 675 | if (fun == NULL)
|
---|
| 676 | return NULL;
|
---|
| 677 |
|
---|
| 678 | fun->busptr = bus;
|
---|
| 679 | return fun;
|
---|
[713a4b9] | 680 | }
|
---|
| 681 |
|
---|
[68414f4a] | 682 | void pci_fun_init(pci_fun_t *fun, int bus, int dev, int fn)
|
---|
[713a4b9] | 683 | {
|
---|
[68414f4a] | 684 | fun->bus = bus;
|
---|
| 685 | fun->dev = dev;
|
---|
| 686 | fun->fn = fn;
|
---|
[713a4b9] | 687 | }
|
---|
| 688 |
|
---|
[68414f4a] | 689 | void pci_fun_delete(pci_fun_t *fun)
|
---|
[713a4b9] | 690 | {
|
---|
[bab6388] | 691 | assert(fun != NULL);
|
---|
| 692 | hw_res_clean_resource_list(&fun->hw_resources);
|
---|
| 693 | free(fun);
|
---|
[713a4b9] | 694 | }
|
---|
| 695 |
|
---|
[97a62fe] | 696 | char *pci_fun_create_name(pci_fun_t *fun)
|
---|
[713a4b9] | 697 | {
|
---|
| 698 | char *name = NULL;
|
---|
| 699 |
|
---|
[68414f4a] | 700 | asprintf(&name, "%02x:%02x.%01x", fun->bus, fun->dev,
|
---|
| 701 | fun->fn);
|
---|
[97a62fe] | 702 | return name;
|
---|
[713a4b9] | 703 | }
|
---|
| 704 |
|
---|
[68414f4a] | 705 | bool pci_alloc_resource_list(pci_fun_t *fun)
|
---|
[713a4b9] | 706 | {
|
---|
[68414f4a] | 707 | fun->hw_resources.resources =
|
---|
[713a4b9] | 708 | (hw_resource_t *) malloc(PCI_MAX_HW_RES * sizeof(hw_resource_t));
|
---|
[68414f4a] | 709 | return fun->hw_resources.resources != NULL;
|
---|
[713a4b9] | 710 | }
|
---|
| 711 |
|
---|
[68414f4a] | 712 | void pci_clean_resource_list(pci_fun_t *fun)
|
---|
[713a4b9] | 713 | {
|
---|
[68414f4a] | 714 | if (fun->hw_resources.resources != NULL) {
|
---|
| 715 | free(fun->hw_resources.resources);
|
---|
| 716 | fun->hw_resources.resources = NULL;
|
---|
[713a4b9] | 717 | }
|
---|
| 718 | }
|
---|
| 719 |
|
---|
[68414f4a] | 720 | /** Read the base address registers (BARs) of the function and add the addresses
|
---|
| 721 | * to its HW resource list.
|
---|
[713a4b9] | 722 | *
|
---|
[68414f4a] | 723 | * @param fun PCI function
|
---|
[713a4b9] | 724 | */
|
---|
[68414f4a] | 725 | void pci_read_bars(pci_fun_t *fun)
|
---|
[713a4b9] | 726 | {
|
---|
| 727 | /*
|
---|
| 728 | * Position of the BAR in the PCI configuration address space of the
|
---|
| 729 | * device.
|
---|
| 730 | */
|
---|
| 731 | int addr = PCI_BASE_ADDR_0;
|
---|
| 732 |
|
---|
| 733 | while (addr <= PCI_BASE_ADDR_5)
|
---|
[8b1e15ac] | 734 | addr = pci_read_bar(fun, addr);
|
---|
[713a4b9] | 735 | }
|
---|
| 736 |
|
---|
| 737 | size_t pci_bar_mask_to_size(uint32_t mask)
|
---|
| 738 | {
|
---|
[ad6857c] | 739 | size_t size = mask & ~(mask - 1);
|
---|
| 740 | return size;
|
---|
[713a4b9] | 741 | }
|
---|
| 742 |
|
---|
[8c06905] | 743 | int main(int argc, char *argv[])
|
---|
| 744 | {
|
---|
[ebcb05a] | 745 | printf(NAME ": HelenOS PCI bus driver (Intel method 1).\n");
|
---|
[3843ecb] | 746 | pciintel_init();
|
---|
[83a2f43] | 747 | return ddf_driver_main(&pci_driver);
|
---|
[8c06905] | 748 | }
|
---|
| 749 |
|
---|
| 750 | /**
|
---|
| 751 | * @}
|
---|
[472020fc] | 752 | */
|
---|