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