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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 876f6463 was 2df6f6fe, checked in by Martin Decky <martin@…>, 13 years ago

cstyle and cleanup
(no change in functionality)

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