source: mainline/uspace/drv/bus/pci/pciintel/pci.c@ 5f6e25e

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

Leave it up to DDF to free driver-specific data. This makes it possible
to ensure soft state is not freed during calls to driver entry points.

This requires some driver changes:

  • minimum change is not to free() driver-data structures (ddf_fun_t.driver_data and ddf_dev_t.driver_data)
  • ideally allocate using ddf_dev_data_alloc(), ddf_fun_data_alloc()

I tried fixing existing drivers accordingly (mostly the minimalistic
change variant), but could have missed something.

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