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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cb94e69b was cb94e69b, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

pci: Fix memory leak in error path.

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