source: mainline/uspace/drv/pciintel/pci.c@ 8436590

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

Logging functions should append newline automatically. Since one has no
choice but to end log message with a newline, there is no need to do it
manually in every invocation.

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