[8c06905] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
| 30 | * @defgroup pciintel pci bus driver for intel method 1.
|
---|
| 31 | * @brief HelenOS root pci bus driver for intel method 1.
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | /** @file
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <assert.h>
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 | #include <errno.h>
|
---|
| 41 | #include <bool.h>
|
---|
| 42 | #include <fibril_synch.h>
|
---|
[c47e1a8] | 43 | #include <str.h>
|
---|
[8c06905] | 44 | #include <ctype.h>
|
---|
| 45 | #include <macros.h>
|
---|
| 46 |
|
---|
| 47 | #include <driver.h>
|
---|
| 48 | #include <devman.h>
|
---|
| 49 | #include <ipc/devman.h>
|
---|
| 50 | #include <ipc/dev_iface.h>
|
---|
[41b56084] | 51 | #include <ops/hw_res.h>
|
---|
[8c06905] | 52 | #include <device/hw_res.h>
|
---|
| 53 | #include <ddi.h>
|
---|
[5e598e0] | 54 | #include <libarch/ddi.h>
|
---|
| 55 |
|
---|
| 56 | #include "pci.h"
|
---|
[8c06905] | 57 |
|
---|
| 58 | #define NAME "pciintel"
|
---|
| 59 |
|
---|
[663f41c4] | 60 | #define CONF_ADDR(bus, dev, fn, reg) \
|
---|
| 61 | ((1 << 31) | (bus << 16) | (dev << 11) | (fn << 8) | (reg & ~3))
|
---|
[5e598e0] | 62 |
|
---|
[8b1e15ac] | 63 | static hw_resource_list_t *pciintel_get_child_resources(function_t *fun)
|
---|
[3843ecb] | 64 | {
|
---|
[8b1e15ac] | 65 | pci_fun_data_t *fun_data = (pci_fun_data_t *) fun->driver_data;
|
---|
[663f41c4] | 66 |
|
---|
[8b1e15ac] | 67 | if (fun_data == NULL)
|
---|
[3843ecb] | 68 | return NULL;
|
---|
[8b1e15ac] | 69 | return &fun_data->hw_resources;
|
---|
[3843ecb] | 70 | }
|
---|
| 71 |
|
---|
[8b1e15ac] | 72 | static bool pciintel_enable_child_interrupt(function_t *fun)
|
---|
[3843ecb] | 73 | {
|
---|
[663f41c4] | 74 | /* TODO */
|
---|
[3843ecb] | 75 |
|
---|
| 76 | return false;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[50c57df] | 79 | static hw_res_ops_t pciintel_child_hw_res_ops = {
|
---|
[3843ecb] | 80 | &pciintel_get_child_resources,
|
---|
[663f41c4] | 81 | &pciintel_enable_child_interrupt
|
---|
[3843ecb] | 82 | };
|
---|
| 83 |
|
---|
[5159ae9] | 84 | static device_ops_t pci_child_ops;
|
---|
[3843ecb] | 85 |
|
---|
[663f41c4] | 86 | static int pci_add_device(device_t *);
|
---|
[3843ecb] | 87 |
|
---|
[663f41c4] | 88 | /** The pci bus driver's standard operations. */
|
---|
[8c06905] | 89 | static driver_ops_t pci_ops = {
|
---|
| 90 | .add_device = &pci_add_device
|
---|
| 91 | };
|
---|
| 92 |
|
---|
[663f41c4] | 93 | /** The pci bus driver structure. */
|
---|
[8c06905] | 94 | static driver_t pci_driver = {
|
---|
| 95 | .name = NAME,
|
---|
| 96 | .driver_ops = &pci_ops
|
---|
| 97 | };
|
---|
| 98 |
|
---|
| 99 | typedef struct pciintel_bus_data {
|
---|
[d1fc8f0] | 100 | uint32_t conf_io_addr;
|
---|
[8c06905] | 101 | void *conf_data_port;
|
---|
[663f41c4] | 102 | void *conf_addr_port;
|
---|
[5e598e0] | 103 | fibril_mutex_t conf_mutex;
|
---|
[8c06905] | 104 | } pci_bus_data_t;
|
---|
| 105 |
|
---|
[663f41c4] | 106 | static pci_bus_data_t *create_pci_bus_data(void)
|
---|
[5e598e0] | 107 | {
|
---|
[663f41c4] | 108 | pci_bus_data_t *bus_data;
|
---|
| 109 |
|
---|
| 110 | bus_data = (pci_bus_data_t *) malloc(sizeof(pci_bus_data_t));
|
---|
[8304889] | 111 | if (bus_data != NULL) {
|
---|
[5e598e0] | 112 | memset(bus_data, 0, sizeof(pci_bus_data_t));
|
---|
| 113 | fibril_mutex_initialize(&bus_data->conf_mutex);
|
---|
| 114 | }
|
---|
[8304889] | 115 |
|
---|
[663f41c4] | 116 | return bus_data;
|
---|
[5e598e0] | 117 | }
|
---|
| 118 |
|
---|
[663f41c4] | 119 | static void delete_pci_bus_data(pci_bus_data_t *bus_data)
|
---|
[5e598e0] | 120 | {
|
---|
[663f41c4] | 121 | free(bus_data);
|
---|
[5e598e0] | 122 | }
|
---|
| 123 |
|
---|
[8b1e15ac] | 124 | static void pci_conf_read(function_t *fun, int reg, uint8_t *buf, size_t len)
|
---|
[5e598e0] | 125 | {
|
---|
[8b1e15ac] | 126 | assert(fun->dev != NULL);
|
---|
[5e598e0] | 127 |
|
---|
[8b1e15ac] | 128 | pci_fun_data_t *fun_data = (pci_fun_data_t *) fun->driver_data;
|
---|
| 129 | pci_bus_data_t *bus_data = (pci_bus_data_t *) fun->dev->driver_data;
|
---|
[5e598e0] | 130 |
|
---|
| 131 | fibril_mutex_lock(&bus_data->conf_mutex);
|
---|
| 132 |
|
---|
[663f41c4] | 133 | uint32_t conf_addr;
|
---|
[8b1e15ac] | 134 | conf_addr = CONF_ADDR(fun_data->bus, fun_data->dev, fun_data->fn, reg);
|
---|
[5e598e0] | 135 | void *addr = bus_data->conf_data_port + (reg & 3);
|
---|
| 136 |
|
---|
| 137 | pio_write_32(bus_data->conf_addr_port, conf_addr);
|
---|
| 138 |
|
---|
| 139 | switch (len) {
|
---|
[663f41c4] | 140 | case 1:
|
---|
| 141 | buf[0] = pio_read_8(addr);
|
---|
| 142 | break;
|
---|
| 143 | case 2:
|
---|
| 144 | ((uint16_t *) buf)[0] = pio_read_16(addr);
|
---|
| 145 | break;
|
---|
| 146 | case 4:
|
---|
| 147 | ((uint32_t *) buf)[0] = pio_read_32(addr);
|
---|
| 148 | break;
|
---|
[5e598e0] | 149 | }
|
---|
| 150 |
|
---|
[663f41c4] | 151 | fibril_mutex_unlock(&bus_data->conf_mutex);
|
---|
[5e598e0] | 152 | }
|
---|
| 153 |
|
---|
[8b1e15ac] | 154 | static void pci_conf_write(function_t *fun, int reg, uint8_t *buf, size_t len)
|
---|
[d1fc8f0] | 155 | {
|
---|
[8b1e15ac] | 156 | assert(fun->dev != NULL);
|
---|
[d1fc8f0] | 157 |
|
---|
[8b1e15ac] | 158 | pci_fun_data_t *fun_data = (pci_fun_data_t *) fun->driver_data;
|
---|
| 159 | pci_bus_data_t *bus_data = (pci_bus_data_t *) fun->dev->driver_data;
|
---|
[d1fc8f0] | 160 |
|
---|
| 161 | fibril_mutex_lock(&bus_data->conf_mutex);
|
---|
| 162 |
|
---|
[663f41c4] | 163 | uint32_t conf_addr;
|
---|
[8b1e15ac] | 164 | conf_addr = CONF_ADDR(fun_data->bus, fun_data->dev, fun_data->fn, reg);
|
---|
[d1fc8f0] | 165 | void *addr = bus_data->conf_data_port + (reg & 3);
|
---|
| 166 |
|
---|
| 167 | pio_write_32(bus_data->conf_addr_port, conf_addr);
|
---|
| 168 |
|
---|
| 169 | switch (len) {
|
---|
[663f41c4] | 170 | case 1:
|
---|
| 171 | pio_write_8(addr, buf[0]);
|
---|
| 172 | break;
|
---|
| 173 | case 2:
|
---|
| 174 | pio_write_16(addr, ((uint16_t *) buf)[0]);
|
---|
| 175 | break;
|
---|
| 176 | case 4:
|
---|
| 177 | pio_write_32(addr, ((uint32_t *) buf)[0]);
|
---|
| 178 | break;
|
---|
[d1fc8f0] | 179 | }
|
---|
| 180 |
|
---|
[663f41c4] | 181 | fibril_mutex_unlock(&bus_data->conf_mutex);
|
---|
[d1fc8f0] | 182 | }
|
---|
| 183 |
|
---|
[8b1e15ac] | 184 | uint8_t pci_conf_read_8(function_t *fun, int reg)
|
---|
[5e598e0] | 185 | {
|
---|
| 186 | uint8_t res;
|
---|
[8b1e15ac] | 187 | pci_conf_read(fun, reg, &res, 1);
|
---|
[5e598e0] | 188 | return res;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[8b1e15ac] | 191 | uint16_t pci_conf_read_16(function_t *fun, int reg)
|
---|
[5e598e0] | 192 | {
|
---|
| 193 | uint16_t res;
|
---|
[8b1e15ac] | 194 | pci_conf_read(fun, reg, (uint8_t *) &res, 2);
|
---|
[5e598e0] | 195 | return res;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[8b1e15ac] | 198 | uint32_t pci_conf_read_32(function_t *fun, int reg)
|
---|
[5e598e0] | 199 | {
|
---|
| 200 | uint32_t res;
|
---|
[8b1e15ac] | 201 | pci_conf_read(fun, reg, (uint8_t *) &res, 4);
|
---|
[663f41c4] | 202 | return res;
|
---|
[5e598e0] | 203 | }
|
---|
| 204 |
|
---|
[8b1e15ac] | 205 | void pci_conf_write_8(function_t *fun, int reg, uint8_t val)
|
---|
[d1fc8f0] | 206 | {
|
---|
[8b1e15ac] | 207 | pci_conf_write(fun, reg, (uint8_t *) &val, 1);
|
---|
[d1fc8f0] | 208 | }
|
---|
| 209 |
|
---|
[8b1e15ac] | 210 | void pci_conf_write_16(function_t *fun, int reg, uint16_t val)
|
---|
[d1fc8f0] | 211 | {
|
---|
[8b1e15ac] | 212 | pci_conf_write(fun, reg, (uint8_t *) &val, 2);
|
---|
[d1fc8f0] | 213 | }
|
---|
| 214 |
|
---|
[8b1e15ac] | 215 | void pci_conf_write_32(function_t *fun, int reg, uint32_t val)
|
---|
[d1fc8f0] | 216 | {
|
---|
[8b1e15ac] | 217 | pci_conf_write(fun, reg, (uint8_t *) &val, 4);
|
---|
[d1fc8f0] | 218 | }
|
---|
| 219 |
|
---|
[8b1e15ac] | 220 | void create_pci_match_ids(function_t *fun)
|
---|
[89ce401a] | 221 | {
|
---|
[8b1e15ac] | 222 | pci_fun_data_t *fun_data = (pci_fun_data_t *) fun->driver_data;
|
---|
[663f41c4] | 223 | match_id_t *match_id = NULL;
|
---|
| 224 | char *match_id_str;
|
---|
| 225 |
|
---|
[89ce401a] | 226 | match_id = create_match_id();
|
---|
[8304889] | 227 | if (match_id != NULL) {
|
---|
[663f41c4] | 228 | asprintf(&match_id_str, "pci/ven=%04x&dev=%04x",
|
---|
[8b1e15ac] | 229 | fun_data->vendor_id, fun_data->device_id);
|
---|
[c47e1a8] | 230 | match_id->id = match_id_str;
|
---|
[89ce401a] | 231 | match_id->score = 90;
|
---|
[8b1e15ac] | 232 | add_match_id(&fun->match_ids, match_id);
|
---|
[8304889] | 233 | }
|
---|
| 234 |
|
---|
[663f41c4] | 235 | /* TODO add more ids (with subsys ids, using class id etc.) */
|
---|
[89ce401a] | 236 | }
|
---|
| 237 |
|
---|
[663f41c4] | 238 | void
|
---|
[8b1e15ac] | 239 | pci_add_range(function_t *fun, uint64_t range_addr, size_t range_size, bool io)
|
---|
[d1fc8f0] | 240 | {
|
---|
[8b1e15ac] | 241 | pci_fun_data_t *fun_data = (pci_fun_data_t *) fun->driver_data;
|
---|
| 242 | hw_resource_list_t *hw_res_list = &fun_data->hw_resources;
|
---|
[3a5909f] | 243 | hw_resource_t *hw_resources = hw_res_list->resources;
|
---|
[663f41c4] | 244 | size_t count = hw_res_list->count;
|
---|
[3a5909f] | 245 |
|
---|
[8304889] | 246 | assert(hw_resources != NULL);
|
---|
[3a5909f] | 247 | assert(count < PCI_MAX_HW_RES);
|
---|
| 248 |
|
---|
| 249 | if (io) {
|
---|
| 250 | hw_resources[count].type = IO_RANGE;
|
---|
| 251 | hw_resources[count].res.io_range.address = range_addr;
|
---|
[663f41c4] | 252 | hw_resources[count].res.io_range.size = range_size;
|
---|
| 253 | hw_resources[count].res.io_range.endianness = LITTLE_ENDIAN;
|
---|
[3a5909f] | 254 | } else {
|
---|
| 255 | hw_resources[count].type = MEM_RANGE;
|
---|
| 256 | hw_resources[count].res.mem_range.address = range_addr;
|
---|
[663f41c4] | 257 | hw_resources[count].res.mem_range.size = range_size;
|
---|
[3a5909f] | 258 | hw_resources[count].res.mem_range.endianness = LITTLE_ENDIAN;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[663f41c4] | 261 | hw_res_list->count++;
|
---|
[d1fc8f0] | 262 | }
|
---|
| 263 |
|
---|
[663f41c4] | 264 | /** Read the base address register (BAR) of the device and if it contains valid
|
---|
| 265 | * address add it to the devices hw resource list.
|
---|
| 266 | *
|
---|
| 267 | * @param dev The pci device.
|
---|
| 268 | * @param addr The address of the BAR in the PCI configuration address space of
|
---|
| 269 | * the device.
|
---|
| 270 | * @return The addr the address of the BAR which should be read next.
|
---|
[d1fc8f0] | 271 | */
|
---|
[8b1e15ac] | 272 | int pci_read_bar(function_t *fun, int addr)
|
---|
[d1fc8f0] | 273 | {
|
---|
[663f41c4] | 274 | /* Value of the BAR */
|
---|
[d1fc8f0] | 275 | uint32_t val, mask;
|
---|
[663f41c4] | 276 | /* IO space address */
|
---|
[d1fc8f0] | 277 | bool io;
|
---|
[663f41c4] | 278 | /* 64-bit wide address */
|
---|
[d93aafed] | 279 | bool addrw64;
|
---|
[d1fc8f0] | 280 |
|
---|
[663f41c4] | 281 | /* Size of the io or memory range specified by the BAR */
|
---|
[d1fc8f0] | 282 | size_t range_size;
|
---|
[663f41c4] | 283 | /* Beginning of the io or memory range specified by the BAR */
|
---|
[d1fc8f0] | 284 | uint64_t range_addr;
|
---|
| 285 |
|
---|
[663f41c4] | 286 | /* Get the value of the BAR. */
|
---|
[8b1e15ac] | 287 | val = pci_conf_read_32(fun, addr);
|
---|
[d1fc8f0] | 288 |
|
---|
[663f41c4] | 289 | io = (bool) (val & 1);
|
---|
[d1fc8f0] | 290 | if (io) {
|
---|
[d93aafed] | 291 | addrw64 = false;
|
---|
[d1fc8f0] | 292 | } else {
|
---|
| 293 | switch ((val >> 1) & 3) {
|
---|
| 294 | case 0:
|
---|
[d93aafed] | 295 | addrw64 = false;
|
---|
[d1fc8f0] | 296 | break;
|
---|
| 297 | case 2:
|
---|
[d93aafed] | 298 | addrw64 = true;
|
---|
[d1fc8f0] | 299 | break;
|
---|
| 300 | default:
|
---|
[663f41c4] | 301 | /* reserved, go to the next BAR */
|
---|
| 302 | return addr + 4;
|
---|
[d1fc8f0] | 303 | }
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[663f41c4] | 306 | /* Get the address mask. */
|
---|
[8b1e15ac] | 307 | pci_conf_write_32(fun, addr, 0xffffffff);
|
---|
| 308 | mask = pci_conf_read_32(fun, addr);
|
---|
[d1fc8f0] | 309 |
|
---|
[663f41c4] | 310 | /* Restore the original value. */
|
---|
[8b1e15ac] | 311 | pci_conf_write_32(fun, addr, val);
|
---|
| 312 | val = pci_conf_read_32(fun, addr);
|
---|
[d1fc8f0] | 313 |
|
---|
[3a5909f] | 314 | range_size = pci_bar_mask_to_size(mask);
|
---|
[d1fc8f0] | 315 |
|
---|
[d93aafed] | 316 | if (addrw64) {
|
---|
[8b1e15ac] | 317 | range_addr = ((uint64_t)pci_conf_read_32(fun, addr + 4) << 32) |
|
---|
[663f41c4] | 318 | (val & 0xfffffff0);
|
---|
[d1fc8f0] | 319 | } else {
|
---|
| 320 | range_addr = (val & 0xfffffff0);
|
---|
[663f41c4] | 321 | }
|
---|
| 322 |
|
---|
[d93aafed] | 323 | if (range_addr != 0) {
|
---|
[8b1e15ac] | 324 | printf(NAME ": function %s : ", fun->name);
|
---|
[7e752b2] | 325 | printf("address = %" PRIx64, range_addr);
|
---|
[ab3a851] | 326 | printf(", size = %x\n", (unsigned int) range_size);
|
---|
[d1fc8f0] | 327 | }
|
---|
| 328 |
|
---|
[8b1e15ac] | 329 | pci_add_range(fun, range_addr, range_size, io);
|
---|
[d1fc8f0] | 330 |
|
---|
[d93aafed] | 331 | if (addrw64)
|
---|
[d1fc8f0] | 332 | return addr + 8;
|
---|
[663f41c4] | 333 |
|
---|
| 334 | return addr + 4;
|
---|
[d1fc8f0] | 335 | }
|
---|
| 336 |
|
---|
[8b1e15ac] | 337 | void pci_add_interrupt(function_t *fun, int irq)
|
---|
[d1fc8f0] | 338 | {
|
---|
[8b1e15ac] | 339 | pci_fun_data_t *fun_data = (pci_fun_data_t *) fun->driver_data;
|
---|
| 340 | hw_resource_list_t *hw_res_list = &fun_data->hw_resources;
|
---|
[663f41c4] | 341 | hw_resource_t *hw_resources = hw_res_list->resources;
|
---|
| 342 | size_t count = hw_res_list->count;
|
---|
[d1fc8f0] | 343 |
|
---|
[3a5909f] | 344 | assert(NULL != hw_resources);
|
---|
| 345 | assert(count < PCI_MAX_HW_RES);
|
---|
| 346 |
|
---|
| 347 | hw_resources[count].type = INTERRUPT;
|
---|
| 348 | hw_resources[count].res.interrupt.irq = irq;
|
---|
| 349 |
|
---|
[663f41c4] | 350 | hw_res_list->count++;
|
---|
[3a5909f] | 351 |
|
---|
[8b1e15ac] | 352 | printf(NAME ": function %s uses irq %x.\n", fun->name, irq);
|
---|
[3a5909f] | 353 | }
|
---|
| 354 |
|
---|
[8b1e15ac] | 355 | void pci_read_interrupt(function_t *fun)
|
---|
[3a5909f] | 356 | {
|
---|
[8b1e15ac] | 357 | uint8_t irq = pci_conf_read_8(fun, PCI_BRIDGE_INT_LINE);
|
---|
[8304889] | 358 | if (irq != 0xff)
|
---|
[8b1e15ac] | 359 | pci_add_interrupt(fun, irq);
|
---|
[d1fc8f0] | 360 | }
|
---|
| 361 |
|
---|
| 362 | /** Enumerate (recursively) and register the devices connected to a pci bus.
|
---|
[663f41c4] | 363 | *
|
---|
[8b1e15ac] | 364 | * @param dev The host-to-pci bridge device.
|
---|
[663f41c4] | 365 | * @param bus_num The bus number.
|
---|
[d1fc8f0] | 366 | */
|
---|
[8b1e15ac] | 367 | void pci_bus_scan(device_t *dev, int bus_num)
|
---|
[5e598e0] | 368 | {
|
---|
[8b1e15ac] | 369 | function_t *fun = create_function();
|
---|
| 370 | pci_fun_data_t *fun_data = create_pci_fun_data();
|
---|
| 371 | fun->driver_data = fun_data;
|
---|
[5e598e0] | 372 |
|
---|
| 373 | int child_bus = 0;
|
---|
| 374 | int dnum, fnum;
|
---|
| 375 | bool multi;
|
---|
[8b1e15ac] | 376 | uint8_t header_type;
|
---|
| 377 |
|
---|
| 378 | /* We need this early, before registering. */
|
---|
| 379 | fun->dev = dev;
|
---|
[5e598e0] | 380 |
|
---|
| 381 | for (dnum = 0; dnum < 32; dnum++) {
|
---|
| 382 | multi = true;
|
---|
| 383 | for (fnum = 0; multi && fnum < 8; fnum++) {
|
---|
[8b1e15ac] | 384 | init_pci_fun_data(fun_data, bus_num, dnum, fnum);
|
---|
| 385 | fun_data->vendor_id = pci_conf_read_16(fun,
|
---|
[663f41c4] | 386 | PCI_VENDOR_ID);
|
---|
[8b1e15ac] | 387 | fun_data->device_id = pci_conf_read_16(fun,
|
---|
[663f41c4] | 388 | PCI_DEVICE_ID);
|
---|
[8b1e15ac] | 389 | if (fun_data->vendor_id == 0xffff) {
|
---|
[663f41c4] | 390 | /*
|
---|
| 391 | * The device is not present, go on scanning the
|
---|
| 392 | * bus.
|
---|
| 393 | */
|
---|
| 394 | if (fnum == 0)
|
---|
[5e598e0] | 395 | break;
|
---|
[663f41c4] | 396 | else
|
---|
| 397 | continue;
|
---|
[5e598e0] | 398 | }
|
---|
[663f41c4] | 399 |
|
---|
[8b1e15ac] | 400 | header_type = pci_conf_read_8(fun, PCI_HEADER_TYPE);
|
---|
[5e598e0] | 401 | if (fnum == 0) {
|
---|
[663f41c4] | 402 | /* Is the device multifunction? */
|
---|
| 403 | multi = header_type >> 7;
|
---|
[5e598e0] | 404 | }
|
---|
[663f41c4] | 405 | /* Clear the multifunction bit. */
|
---|
| 406 | header_type = header_type & 0x7F;
|
---|
[5e598e0] | 407 |
|
---|
[8b1e15ac] | 408 | create_pci_fun_name(fun);
|
---|
[3a5909f] | 409 |
|
---|
[8b1e15ac] | 410 | pci_alloc_resource_list(fun);
|
---|
| 411 | pci_read_bars(fun);
|
---|
| 412 | pci_read_interrupt(fun);
|
---|
[3a5909f] | 413 |
|
---|
[8b1e15ac] | 414 | fun->ftype = fun_inner;
|
---|
| 415 | fun->ops = &pci_child_ops;
|
---|
[89ce401a] | 416 |
|
---|
[8b1e15ac] | 417 | printf(NAME ": adding new function %s.\n",
|
---|
| 418 | fun->name);
|
---|
[89ce401a] | 419 |
|
---|
[8b1e15ac] | 420 | create_pci_match_ids(fun);
|
---|
[89ce401a] | 421 |
|
---|
[8b1e15ac] | 422 | if (register_function(fun, dev) != EOK) {
|
---|
| 423 | pci_clean_resource_list(fun);
|
---|
| 424 | clean_match_ids(&fun->match_ids);
|
---|
| 425 | free((char *) fun->name);
|
---|
| 426 | fun->name = NULL;
|
---|
[89ce401a] | 427 | continue;
|
---|
| 428 | }
|
---|
[5e598e0] | 429 |
|
---|
[663f41c4] | 430 | if (header_type == PCI_HEADER_TYPE_BRIDGE ||
|
---|
[8304889] | 431 | header_type == PCI_HEADER_TYPE_CARDBUS) {
|
---|
[8b1e15ac] | 432 | child_bus = pci_conf_read_8(fun,
|
---|
[663f41c4] | 433 | PCI_BRIDGE_SEC_BUS_NUM);
|
---|
| 434 | printf(NAME ": device is pci-to-pci bridge, "
|
---|
| 435 | "secondary bus number = %d.\n", bus_num);
|
---|
[8304889] | 436 | if (child_bus > bus_num)
|
---|
[8b1e15ac] | 437 | pci_bus_scan(dev, child_bus);
|
---|
[5e598e0] | 438 | }
|
---|
| 439 |
|
---|
[8b1e15ac] | 440 | /* Alloc new aux. fun. structure. */
|
---|
| 441 | fun = create_function();
|
---|
| 442 |
|
---|
| 443 | /* We need this early, before registering. */
|
---|
| 444 | fun->dev = dev;
|
---|
| 445 |
|
---|
| 446 | fun_data = create_pci_fun_data();
|
---|
| 447 | fun->driver_data = fun_data;
|
---|
[5e598e0] | 448 | }
|
---|
| 449 | }
|
---|
| 450 |
|
---|
[8b1e15ac] | 451 | if (fun_data->vendor_id == 0xffff) {
|
---|
| 452 | delete_function(fun);
|
---|
| 453 | /* Free the auxiliary function structure. */
|
---|
| 454 | delete_pci_fun_data(fun_data);
|
---|
[663f41c4] | 455 | }
|
---|
[5e598e0] | 456 | }
|
---|
[8c06905] | 457 |
|
---|
[df747b9c] | 458 | static int pci_add_device(device_t *dev)
|
---|
[8c06905] | 459 | {
|
---|
[be942bc] | 460 | int rc;
|
---|
| 461 |
|
---|
[8c06905] | 462 | printf(NAME ": pci_add_device\n");
|
---|
| 463 |
|
---|
[5e598e0] | 464 | pci_bus_data_t *bus_data = create_pci_bus_data();
|
---|
[8304889] | 465 | if (bus_data == NULL) {
|
---|
[8c06905] | 466 | printf(NAME ": pci_add_device allocation failed.\n");
|
---|
[df747b9c] | 467 | return ENOMEM;
|
---|
[663f41c4] | 468 | }
|
---|
[8c06905] | 469 |
|
---|
[663f41c4] | 470 | dev->parent_phone = devman_parent_device_connect(dev->handle,
|
---|
| 471 | IPC_FLAG_BLOCKING);
|
---|
| 472 | if (dev->parent_phone < 0) {
|
---|
| 473 | printf(NAME ": pci_add_device failed to connect to the "
|
---|
| 474 | "parent's driver.\n");
|
---|
[89ce401a] | 475 | delete_pci_bus_data(bus_data);
|
---|
[be942bc] | 476 | return dev->parent_phone;
|
---|
[8c06905] | 477 | }
|
---|
| 478 |
|
---|
| 479 | hw_resource_list_t hw_resources;
|
---|
| 480 |
|
---|
[f724e82] | 481 | rc = hw_res_get_resource_list(dev->parent_phone, &hw_resources);
|
---|
[be942bc] | 482 | if (rc != EOK) {
|
---|
[663f41c4] | 483 | printf(NAME ": pci_add_device failed to get hw resources for "
|
---|
| 484 | "the device.\n");
|
---|
[89ce401a] | 485 | delete_pci_bus_data(bus_data);
|
---|
[ffa2c8ef] | 486 | async_hangup(dev->parent_phone);
|
---|
[be942bc] | 487 | return rc;
|
---|
[3a5909f] | 488 | }
|
---|
[8c06905] | 489 |
|
---|
[7e752b2] | 490 | printf(NAME ": conf_addr = %" PRIx64 ".\n",
|
---|
[663f41c4] | 491 | hw_resources.resources[0].res.io_range.address);
|
---|
[8c06905] | 492 |
|
---|
| 493 | assert(hw_resources.count > 0);
|
---|
[3a5909f] | 494 | assert(hw_resources.resources[0].type == IO_RANGE);
|
---|
| 495 | assert(hw_resources.resources[0].res.io_range.size == 8);
|
---|
[8c06905] | 496 |
|
---|
[663f41c4] | 497 | bus_data->conf_io_addr =
|
---|
| 498 | (uint32_t) hw_resources.resources[0].res.io_range.address;
|
---|
[8c06905] | 499 |
|
---|
[178673c] | 500 | if (pio_enable((void *)(uintptr_t)bus_data->conf_io_addr, 8,
|
---|
[663f41c4] | 501 | &bus_data->conf_addr_port)) {
|
---|
[8c06905] | 502 | printf(NAME ": failed to enable configuration ports.\n");
|
---|
[89ce401a] | 503 | delete_pci_bus_data(bus_data);
|
---|
[ffa2c8ef] | 504 | async_hangup(dev->parent_phone);
|
---|
[f724e82] | 505 | hw_res_clean_resource_list(&hw_resources);
|
---|
[663f41c4] | 506 | return EADDRNOTAVAIL;
|
---|
[8c06905] | 507 | }
|
---|
[663f41c4] | 508 | bus_data->conf_data_port = (char *) bus_data->conf_addr_port + 4;
|
---|
[8c06905] | 509 |
|
---|
| 510 | dev->driver_data = bus_data;
|
---|
| 511 |
|
---|
[8b1e15ac] | 512 | /* Make the bus device more visible. Does not do anything. */
|
---|
| 513 | printf(NAME ": adding a 'ctl' function\n");
|
---|
| 514 |
|
---|
| 515 | function_t *ctl = create_function();
|
---|
| 516 | ctl->ftype = fun_exposed;
|
---|
| 517 | ctl->name = "ctl";
|
---|
| 518 | register_function(ctl, dev);
|
---|
| 519 |
|
---|
[663f41c4] | 520 | /* Enumerate child devices. */
|
---|
[89ce401a] | 521 | printf(NAME ": scanning the bus\n");
|
---|
[5e598e0] | 522 | pci_bus_scan(dev, 0);
|
---|
[8c06905] | 523 |
|
---|
[f724e82] | 524 | hw_res_clean_resource_list(&hw_resources);
|
---|
[8c06905] | 525 |
|
---|
[df747b9c] | 526 | return EOK;
|
---|
[8c06905] | 527 | }
|
---|
| 528 |
|
---|
[663f41c4] | 529 | static void pciintel_init(void)
|
---|
[3843ecb] | 530 | {
|
---|
[50c57df] | 531 | pci_child_ops.interfaces[HW_RES_DEV_IFACE] = &pciintel_child_hw_res_ops;
|
---|
[3843ecb] | 532 | }
|
---|
| 533 |
|
---|
[8b1e15ac] | 534 | pci_fun_data_t *create_pci_fun_data(void)
|
---|
[713a4b9] | 535 | {
|
---|
[8b1e15ac] | 536 | pci_fun_data_t *res = (pci_fun_data_t *) malloc(sizeof(pci_fun_data_t));
|
---|
[713a4b9] | 537 |
|
---|
[8304889] | 538 | if (res != NULL)
|
---|
[8b1e15ac] | 539 | memset(res, 0, sizeof(pci_fun_data_t));
|
---|
[713a4b9] | 540 | return res;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
[8b1e15ac] | 543 | void init_pci_fun_data(pci_fun_data_t *fun_data, int bus, int dev, int fn)
|
---|
[713a4b9] | 544 | {
|
---|
[8b1e15ac] | 545 | fun_data->bus = bus;
|
---|
| 546 | fun_data->dev = dev;
|
---|
| 547 | fun_data->fn = fn;
|
---|
[713a4b9] | 548 | }
|
---|
| 549 |
|
---|
[8b1e15ac] | 550 | void delete_pci_fun_data(pci_fun_data_t *fun_data)
|
---|
[713a4b9] | 551 | {
|
---|
[8b1e15ac] | 552 | if (fun_data != NULL) {
|
---|
| 553 | hw_res_clean_resource_list(&fun_data->hw_resources);
|
---|
| 554 | free(fun_data);
|
---|
[713a4b9] | 555 | }
|
---|
| 556 | }
|
---|
| 557 |
|
---|
[8b1e15ac] | 558 | void create_pci_fun_name(function_t *fun)
|
---|
[713a4b9] | 559 | {
|
---|
[8b1e15ac] | 560 | pci_fun_data_t *fun_data = (pci_fun_data_t *) fun->driver_data;
|
---|
[713a4b9] | 561 | char *name = NULL;
|
---|
| 562 |
|
---|
[8b1e15ac] | 563 | asprintf(&name, "%02x:%02x.%01x", fun_data->bus, fun_data->dev,
|
---|
| 564 | fun_data->fn);
|
---|
| 565 | fun->name = name;
|
---|
[713a4b9] | 566 | }
|
---|
| 567 |
|
---|
[8b1e15ac] | 568 | bool pci_alloc_resource_list(function_t *fun)
|
---|
[713a4b9] | 569 | {
|
---|
[8b1e15ac] | 570 | pci_fun_data_t *fun_data = (pci_fun_data_t *)fun->driver_data;
|
---|
[713a4b9] | 571 |
|
---|
[8b1e15ac] | 572 | fun_data->hw_resources.resources =
|
---|
[713a4b9] | 573 | (hw_resource_t *) malloc(PCI_MAX_HW_RES * sizeof(hw_resource_t));
|
---|
[8b1e15ac] | 574 | return fun_data->hw_resources.resources != NULL;
|
---|
[713a4b9] | 575 | }
|
---|
| 576 |
|
---|
[8b1e15ac] | 577 | void pci_clean_resource_list(function_t *fun)
|
---|
[713a4b9] | 578 | {
|
---|
[8b1e15ac] | 579 | pci_fun_data_t *fun_data = (pci_fun_data_t *) fun->driver_data;
|
---|
[713a4b9] | 580 |
|
---|
[8b1e15ac] | 581 | if (fun_data->hw_resources.resources != NULL) {
|
---|
| 582 | free(fun_data->hw_resources.resources);
|
---|
| 583 | fun_data->hw_resources.resources = NULL;
|
---|
[713a4b9] | 584 | }
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | /** Read the base address registers (BARs) of the device and adds the addresses
|
---|
| 588 | * to its hw resource list.
|
---|
| 589 | *
|
---|
| 590 | * @param dev the pci device.
|
---|
| 591 | */
|
---|
[8b1e15ac] | 592 | void pci_read_bars(function_t *fun)
|
---|
[713a4b9] | 593 | {
|
---|
| 594 | /*
|
---|
| 595 | * Position of the BAR in the PCI configuration address space of the
|
---|
| 596 | * device.
|
---|
| 597 | */
|
---|
| 598 | int addr = PCI_BASE_ADDR_0;
|
---|
| 599 |
|
---|
| 600 | while (addr <= PCI_BASE_ADDR_5)
|
---|
[8b1e15ac] | 601 | addr = pci_read_bar(fun, addr);
|
---|
[713a4b9] | 602 | }
|
---|
| 603 |
|
---|
| 604 | size_t pci_bar_mask_to_size(uint32_t mask)
|
---|
| 605 | {
|
---|
| 606 | return ((mask & 0xfffffff0) ^ 0xffffffff) + 1;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
[8c06905] | 609 | int main(int argc, char *argv[])
|
---|
| 610 | {
|
---|
[3843ecb] | 611 | printf(NAME ": HelenOS pci bus driver (intel method 1).\n");
|
---|
| 612 | pciintel_init();
|
---|
[8c06905] | 613 | return driver_main(&pci_driver);
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | /**
|
---|
| 617 | * @}
|
---|
[472020fc] | 618 | */
|
---|