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

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

pci: intel pci is always little endian.

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