source: mainline/uspace/drv/bus/pci/pciintel/pci.c@ 4069f5c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4069f5c was ef9460b, checked in by Jiri Svoboda <jiri@…>, 14 years ago

ddf_fun_add_match_id() should copy its string argument.

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