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

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

Tolerate empty PCI sub buses.

  • Property mode set to 100644
File size: 22.7 KB
RevLine 
[8c06905]1/*
2 * Copyright (c) 2010 Lenka Trochtova
[184f2f8a]3 * Copyright (c) 2018 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/**
[4122410]31 * @addtogroup pciintel
[8c06905]32 * @{
33 */
34
35/** @file
36 */
37
38#include <assert.h>
[690d2e7]39#include <byteorder.h>
[8c06905]40#include <stdio.h>
41#include <errno.h>
[3e6a98c5]42#include <stdbool.h>
[8c06905]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 <ipc/dev_iface.h>
[ebc9c2c]52#include <irc.h>
[41b56084]53#include <ops/hw_res.h>
[8c06905]54#include <device/hw_res.h>
[6dbc500]55#include <ops/pio_window.h>
56#include <device/pio_window.h>
[8c06905]57#include <ddi.h>
[99e6bfb]58#include <pci_dev_iface.h>
[5e598e0]59
60#include "pci.h"
[8c06905]61
62#define NAME "pciintel"
63
[587478b]64#define CONF_ADDR_ENABLE (((unsigned)1) << 31)
[663f41c4]65#define CONF_ADDR(bus, dev, fn, reg) \
[92d5279]66 ((bus << 16) | (dev << 11) | (fn << 8) | (reg & ~3))
[5e598e0]67
[68414f4a]68/** Obtain PCI function soft-state from DDF function node */
[56fd7cf]69static pci_fun_t *pci_fun(ddf_fun_t *fnode)
70{
71 return ddf_fun_data_get(fnode);
72}
[68414f4a]73
74/** Obtain PCI bus soft-state from DDF device node */
[56fd7cf]75#if 0
76static pci_bus_t *pci_bus(ddf_dev_t *dnode)
77{
78 return ddf_dev_data_get(dnode);
79}
80#endif
[68414f4a]81
82/** Obtain PCI bus soft-state from function soft-state */
[56fd7cf]83static pci_bus_t *pci_bus_from_fun(pci_fun_t *fun)
84{
85 return fun->busptr;
86}
[68414f4a]87
[54de4836]88/** Max is 47, align to something nice. */
89#define ID_MAX_STR_LEN 50
90
[83a2f43]91static hw_resource_list_t *pciintel_get_resources(ddf_fun_t *fnode)
[3843ecb]92{
[56fd7cf]93 pci_fun_t *fun = pci_fun(fnode);
[a35b458]94
[68414f4a]95 if (fun == NULL)
[3843ecb]96 return NULL;
[68414f4a]97 return &fun->hw_resources;
[3843ecb]98}
99
[d5c1051]100static bool pciintel_fun_owns_interrupt(pci_fun_t *fun, int irq)
[3843ecb]101{
[cccd60c3]102 size_t i;
[d51838f]103 hw_resource_list_t *res = &fun->hw_resources;
[a35b458]104
[cccd60c3]105 for (i = 0; i < res->count; i++) {
[d51838f]106 if (res->resources[i].type == INTERRUPT &&
107 res->resources[i].res.interrupt.irq == irq) {
108 return true;
[fb78ae72]109 }
110 }
[a35b458]111
[d51838f]112 return false;
113}
114
[b7fd2a0]115static errno_t pciintel_enable_interrupt(ddf_fun_t *fnode, int irq)
[d51838f]116{
117 pci_fun_t *fun = pci_fun(fnode);
[a35b458]118
[d51838f]119 if (!pciintel_fun_owns_interrupt(fun, irq))
120 return EINVAL;
121
[cccd60c3]122 return irc_enable_interrupt(irq);
[3843ecb]123}
124
[b7fd2a0]125static errno_t pciintel_disable_interrupt(ddf_fun_t *fnode, int irq)
[d51838f]126{
127 pci_fun_t *fun = pci_fun(fnode);
[a35b458]128
[d51838f]129 if (!pciintel_fun_owns_interrupt(fun, irq))
130 return EINVAL;
131
132 return irc_disable_interrupt(irq);
133}
134
[b7fd2a0]135static errno_t pciintel_clear_interrupt(ddf_fun_t *fnode, int irq)
[d51838f]136{
137 pci_fun_t *fun = pci_fun(fnode);
[a35b458]138
[d51838f]139 if (!pciintel_fun_owns_interrupt(fun, irq))
140 return EINVAL;
141
142 return irc_clear_interrupt(irq);
143}
144
[6dbc500]145static pio_window_t *pciintel_get_pio_window(ddf_fun_t *fnode)
146{
147 pci_fun_t *fun = pci_fun(fnode);
[a35b458]148
[6dbc500]149 if (fun == NULL)
150 return NULL;
151 return &fun->pio_window;
152}
153
[b7fd2a0]154static errno_t config_space_write_32(ddf_fun_t *fun, uint32_t address,
[79ae36dd]155 uint32_t data)
[40a5d40]156{
157 if (address > 252)
158 return EINVAL;
[56fd7cf]159 pci_conf_write_32(pci_fun(fun), address, data);
[40a5d40]160 return EOK;
161}
162
[b7fd2a0]163static errno_t config_space_write_16(
[40a5d40]164 ddf_fun_t *fun, uint32_t address, uint16_t data)
[99e6bfb]165{
166 if (address > 254)
167 return EINVAL;
[56fd7cf]168 pci_conf_write_16(pci_fun(fun), address, data);
[99e6bfb]169 return EOK;
170}
171
[b7fd2a0]172static errno_t config_space_write_8(
[40a5d40]173 ddf_fun_t *fun, uint32_t address, uint8_t data)
174{
175 if (address > 255)
176 return EINVAL;
[56fd7cf]177 pci_conf_write_8(pci_fun(fun), address, data);
[40a5d40]178 return EOK;
179}
180
[b7fd2a0]181static errno_t config_space_read_32(
[40a5d40]182 ddf_fun_t *fun, uint32_t address, uint32_t *data)
183{
184 if (address > 252)
185 return EINVAL;
[56fd7cf]186 *data = pci_conf_read_32(pci_fun(fun), address);
[40a5d40]187 return EOK;
188}
189
[b7fd2a0]190static errno_t config_space_read_16(
[40a5d40]191 ddf_fun_t *fun, uint32_t address, uint16_t *data)
192{
193 if (address > 254)
194 return EINVAL;
[56fd7cf]195 *data = pci_conf_read_16(pci_fun(fun), address);
[40a5d40]196 return EOK;
197}
198
[b7fd2a0]199static errno_t config_space_read_8(
[40a5d40]200 ddf_fun_t *fun, uint32_t address, uint8_t *data)
201{
202 if (address > 255)
203 return EINVAL;
[56fd7cf]204 *data = pci_conf_read_8(pci_fun(fun), address);
[40a5d40]205 return EOK;
206}
[99e6bfb]207
[68414f4a]208static hw_res_ops_t pciintel_hw_res_ops = {
[d9cf684a]209 .get_resource_list = &pciintel_get_resources,
210 .enable_interrupt = &pciintel_enable_interrupt,
[d51838f]211 .disable_interrupt = &pciintel_disable_interrupt,
212 .clear_interrupt = &pciintel_clear_interrupt,
[3843ecb]213};
214
[6dbc500]215static pio_window_ops_t pciintel_pio_window_ops = {
216 .get_pio_window = &pciintel_get_pio_window
217};
218
[99e6bfb]219static pci_dev_iface_t pci_dev_ops = {
[99e8fb7b]220 .config_space_read_8 = &config_space_read_8,
221 .config_space_read_16 = &config_space_read_16,
222 .config_space_read_32 = &config_space_read_32,
223 .config_space_write_8 = &config_space_write_8,
224 .config_space_write_16 = &config_space_write_16,
225 .config_space_write_32 = &config_space_write_32
[99e6bfb]226};
227
228static ddf_dev_ops_t pci_fun_ops = {
229 .interfaces[HW_RES_DEV_IFACE] = &pciintel_hw_res_ops,
[6dbc500]230 .interfaces[PIO_WINDOW_DEV_IFACE] = &pciintel_pio_window_ops,
[99e6bfb]231 .interfaces[PCI_DEV_IFACE] = &pci_dev_ops
232};
[3843ecb]233
[b7fd2a0]234static errno_t pci_dev_add(ddf_dev_t *);
235static errno_t pci_fun_online(ddf_fun_t *);
236static errno_t pci_fun_offline(ddf_fun_t *);
[3843ecb]237
[68414f4a]238/** PCI bus driver standard operations */
[8c06905]239static driver_ops_t pci_ops = {
[0c0f823b]240 .dev_add = &pci_dev_add,
[f278930]241 .fun_online = &pci_fun_online,
242 .fun_offline = &pci_fun_offline,
[8c06905]243};
244
[68414f4a]245/** PCI bus driver structure */
[8c06905]246static driver_t pci_driver = {
247 .name = NAME,
248 .driver_ops = &pci_ops
249};
250
[68414f4a]251static void pci_conf_read(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
[5e598e0]252{
[82721f5]253 const uint32_t conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
[56fd7cf]254 pci_bus_t *bus = pci_bus_from_fun(fun);
[82721f5]255 uint32_t val;
[a35b458]256
[68414f4a]257 fibril_mutex_lock(&bus->conf_mutex);
[82721f5]258
[92d5279]259 if (bus->conf_addr_reg) {
260 pio_write_32(bus->conf_addr_reg,
261 host2uint32_t_le(CONF_ADDR_ENABLE | conf_addr));
262 /*
263 * Always read full 32-bits from the PCI conf_data_port
264 * register and get the desired portion of it afterwards. Some
265 * architectures do not support shorter PIO reads offset from
266 * this register.
[ae7d03c]267 */
[92d5279]268 val = uint32_t_le2host(pio_read_32(bus->conf_data_reg));
269 } else {
270 val = uint32_t_le2host(pio_read_32(
271 &bus->conf_space[conf_addr / sizeof(ioport32_t)]));
272 }
[82721f5]273
[5e598e0]274 switch (len) {
[663f41c4]275 case 1:
[82721f5]276 *buf = (uint8_t) (val >> ((reg & 3) * 8));
[663f41c4]277 break;
278 case 2:
[82721f5]279 *((uint16_t *) buf) = (uint16_t) (val >> ((reg & 3)) * 8);
[663f41c4]280 break;
281 case 4:
[82721f5]282 *((uint32_t *) buf) = (uint32_t) val;
[663f41c4]283 break;
[5e598e0]284 }
[a35b458]285
[68414f4a]286 fibril_mutex_unlock(&bus->conf_mutex);
[5e598e0]287}
288
[68414f4a]289static void pci_conf_write(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
[d1fc8f0]290{
[82721f5]291 const uint32_t conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
[56fd7cf]292 pci_bus_t *bus = pci_bus_from_fun(fun);
[0464967]293 uint32_t val = 0;
[a35b458]294
[68414f4a]295 fibril_mutex_lock(&bus->conf_mutex);
[82721f5]296
297 /*
298 * Prepare to write full 32-bits to the PCI conf_data_port register.
299 * Some architectures do not support shorter PIO writes offset from this
300 * register.
[ae7d03c]301 */
[82721f5]302
303 if (len < 4) {
304 /*
[ae7d03c]305 * We have fewer than full 32-bits, so we need to read the
306 * missing bits first.
307 */
[92d5279]308 if (bus->conf_addr_reg) {
309 pio_write_32(bus->conf_addr_reg,
310 host2uint32_t_le(CONF_ADDR_ENABLE | conf_addr));
311 val = uint32_t_le2host(pio_read_32(bus->conf_data_reg));
312 } else {
313 val = uint32_t_le2host(pio_read_32(
314 &bus->conf_space[conf_addr / sizeof(ioport32_t)]));
315 }
[82721f5]316 }
[a35b458]317
[d1fc8f0]318 switch (len) {
[663f41c4]319 case 1:
[82721f5]320 val &= ~(0xffU << ((reg & 3) * 8));
321 val |= *buf << ((reg & 3) * 8);
[663f41c4]322 break;
323 case 2:
[82721f5]324 val &= ~(0xffffU << ((reg & 3) * 8));
325 val |= *((uint16_t *) buf) << ((reg & 3) * 8);
[663f41c4]326 break;
327 case 4:
[82721f5]328 val = *((uint32_t *) buf);
[663f41c4]329 break;
[d1fc8f0]330 }
[82721f5]331
[92d5279]332 if (bus->conf_addr_reg) {
333 pio_write_32(bus->conf_addr_reg,
334 host2uint32_t_le(CONF_ADDR_ENABLE | conf_addr));
335 pio_write_32(bus->conf_data_reg, host2uint32_t_le(val));
336 } else {
337 pio_write_32(&bus->conf_space[conf_addr / sizeof(ioport32_t)],
338 host2uint32_t_le(val));
339 }
[a35b458]340
[68414f4a]341 fibril_mutex_unlock(&bus->conf_mutex);
[d1fc8f0]342}
343
[68414f4a]344uint8_t pci_conf_read_8(pci_fun_t *fun, int reg)
[5e598e0]345{
346 uint8_t res;
[8b1e15ac]347 pci_conf_read(fun, reg, &res, 1);
[5e598e0]348 return res;
349}
350
[68414f4a]351uint16_t pci_conf_read_16(pci_fun_t *fun, int reg)
[5e598e0]352{
353 uint16_t res;
[8b1e15ac]354 pci_conf_read(fun, reg, (uint8_t *) &res, 2);
[5e598e0]355 return res;
356}
357
[68414f4a]358uint32_t pci_conf_read_32(pci_fun_t *fun, int reg)
[5e598e0]359{
360 uint32_t res;
[8b1e15ac]361 pci_conf_read(fun, reg, (uint8_t *) &res, 4);
[663f41c4]362 return res;
[5e598e0]363}
364
[68414f4a]365void pci_conf_write_8(pci_fun_t *fun, int reg, uint8_t val)
[d1fc8f0]366{
[8b1e15ac]367 pci_conf_write(fun, reg, (uint8_t *) &val, 1);
[d1fc8f0]368}
369
[68414f4a]370void pci_conf_write_16(pci_fun_t *fun, int reg, uint16_t val)
[d1fc8f0]371{
[8b1e15ac]372 pci_conf_write(fun, reg, (uint8_t *) &val, 2);
[d1fc8f0]373}
374
[68414f4a]375void pci_conf_write_32(pci_fun_t *fun, int reg, uint32_t val)
[d1fc8f0]376{
[8b1e15ac]377 pci_conf_write(fun, reg, (uint8_t *) &val, 4);
[d1fc8f0]378}
379
[68414f4a]380void pci_fun_create_match_ids(pci_fun_t *fun)
[89ce401a]381{
[b7fd2a0]382 errno_t rc;
[d5c1051]383 int ret;
[1d53a78]384 char match_id_str[ID_MAX_STR_LEN];
[cd0684d]385
[1d53a78]386 /* Vendor ID & Device ID, length(incl \0) 22 */
[d5c1051]387 ret = snprintf(match_id_str, ID_MAX_STR_LEN, "pci/ven=%04"
[c90aed4]388 PRIx16 "&dev=%04" PRIx16, fun->vendor_id, fun->device_id);
[d5c1051]389 if (ret < 0) {
390 ddf_msg(LVL_ERROR, "Failed creating match ID str");
[8304889]391 }
392
[cd0684d]393 rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 90);
394 if (rc != EOK) {
[1d53a78]395 ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
396 }
397
398 /* Class, subclass, prog IF, revision, length(incl \0) 47 */
[d5c1051]399 ret = snprintf(match_id_str, ID_MAX_STR_LEN,
[1d53a78]400 "pci/class=%02x&subclass=%02x&progif=%02x&revision=%02x",
401 fun->class_code, fun->subclass_code, fun->prog_if, fun->revision);
[d5c1051]402 if (ret < 0) {
403 ddf_msg(LVL_ERROR, "Failed creating match ID str");
[8304889]404 }
[1d53a78]405
406 rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 70);
407 if (rc != EOK) {
408 ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
409 }
410
411 /* Class, subclass, prog IF, length(incl \0) 35 */
[d5c1051]412 ret = snprintf(match_id_str, ID_MAX_STR_LEN,
[1d53a78]413 "pci/class=%02x&subclass=%02x&progif=%02x",
414 fun->class_code, fun->subclass_code, fun->prog_if);
[d5c1051]415 if (ret < 0) {
416 ddf_msg(LVL_ERROR, "Failed creating match ID str");
[1d53a78]417 }
418
419 rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 60);
420 if (rc != EOK) {
421 ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
422 }
423
424 /* Class, subclass, length(incl \0) 25 */
[d5c1051]425 ret = snprintf(match_id_str, ID_MAX_STR_LEN,
[1d53a78]426 "pci/class=%02x&subclass=%02x",
427 fun->class_code, fun->subclass_code);
[d5c1051]428 if (ret < 0) {
429 ddf_msg(LVL_ERROR, "Failed creating match ID str");
[1d53a78]430 }
431
432 rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 50);
433 if (rc != EOK) {
434 ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
435 }
436
437 /* Class, length(incl \0) 13 */
[d5c1051]438 ret = snprintf(match_id_str, ID_MAX_STR_LEN, "pci/class=%02x",
[1d53a78]439 fun->class_code);
[d5c1051]440 if (ret < 0) {
441 ddf_msg(LVL_ERROR, "Failed creating match ID str");
[1d53a78]442 }
443
444 rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 40);
445 if (rc != EOK) {
446 ddf_msg(LVL_ERROR, "Failed adding match ID: %s", str_error(rc));
447 }
448
449 /* TODO add subsys ids, but those exist only in header type 0 */
[89ce401a]450}
451
[68414f4a]452void pci_add_range(pci_fun_t *fun, uint64_t range_addr, size_t range_size,
453 bool io)
[d1fc8f0]454{
[68414f4a]455 hw_resource_list_t *hw_res_list = &fun->hw_resources;
[3a5909f]456 hw_resource_t *hw_resources = hw_res_list->resources;
[663f41c4]457 size_t count = hw_res_list->count;
[a35b458]458
[8304889]459 assert(hw_resources != NULL);
[3a5909f]460 assert(count < PCI_MAX_HW_RES);
[a35b458]461
[3a5909f]462 if (io) {
463 hw_resources[count].type = IO_RANGE;
464 hw_resources[count].res.io_range.address = range_addr;
[663f41c4]465 hw_resources[count].res.io_range.size = range_size;
[9e470c0]466 hw_resources[count].res.io_range.relative = true;
[663f41c4]467 hw_resources[count].res.io_range.endianness = LITTLE_ENDIAN;
[3a5909f]468 } else {
469 hw_resources[count].type = MEM_RANGE;
470 hw_resources[count].res.mem_range.address = range_addr;
[663f41c4]471 hw_resources[count].res.mem_range.size = range_size;
[9e470c0]472 hw_resources[count].res.mem_range.relative = false;
[3a5909f]473 hw_resources[count].res.mem_range.endianness = LITTLE_ENDIAN;
474 }
[a35b458]475
[663f41c4]476 hw_res_list->count++;
[d1fc8f0]477}
478
[663f41c4]479/** Read the base address register (BAR) of the device and if it contains valid
480 * address add it to the devices hw resource list.
481 *
[68414f4a]482 * @param fun PCI function
[663f41c4]483 * @param addr The address of the BAR in the PCI configuration address space of
[68414f4a]484 * the device
485 * @return The addr the address of the BAR which should be read next
[d1fc8f0]486 */
[68414f4a]487int pci_read_bar(pci_fun_t *fun, int addr)
[bab6388]488{
[663f41c4]489 /* Value of the BAR */
[e8d6ce2]490 uint32_t val;
491 uint32_t bar;
492 uint32_t mask;
493
[663f41c4]494 /* IO space address */
[d1fc8f0]495 bool io;
[663f41c4]496 /* 64-bit wide address */
[d93aafed]497 bool addrw64;
[a35b458]498
[663f41c4]499 /* Size of the io or memory range specified by the BAR */
[d1fc8f0]500 size_t range_size;
[663f41c4]501 /* Beginning of the io or memory range specified by the BAR */
[d1fc8f0]502 uint64_t range_addr;
[a35b458]503
[663f41c4]504 /* Get the value of the BAR. */
[8b1e15ac]505 val = pci_conf_read_32(fun, addr);
[ad6857c]506
507#define IO_MASK (~0x3)
508#define MEM_MASK (~0xf)
[a35b458]509
[c81132d]510 io = (val & 1) != 0;
[d1fc8f0]511 if (io) {
[d93aafed]512 addrw64 = false;
[ad6857c]513 mask = IO_MASK;
[d1fc8f0]514 } else {
[ad6857c]515 mask = MEM_MASK;
[d1fc8f0]516 switch ((val >> 1) & 3) {
517 case 0:
[d93aafed]518 addrw64 = false;
[d1fc8f0]519 break;
520 case 2:
[d93aafed]521 addrw64 = true;
[d1fc8f0]522 break;
523 default:
[663f41c4]524 /* reserved, go to the next BAR */
525 return addr + 4;
[d1fc8f0]526 }
527 }
[a35b458]528
[663f41c4]529 /* Get the address mask. */
[8b1e15ac]530 pci_conf_write_32(fun, addr, 0xffffffff);
[e8d6ce2]531 bar = pci_conf_read_32(fun, addr);
532
533 /*
[ae7d03c]534 * Unimplemented BARs read back as all 0's.
535 */
[e8d6ce2]536 if (!bar)
537 return addr + (addrw64 ? 8 : 4);
538
[1b20da0]539 mask &= bar;
[e8d6ce2]540
[663f41c4]541 /* Restore the original value. */
[8b1e15ac]542 pci_conf_write_32(fun, addr, val);
543 val = pci_conf_read_32(fun, addr);
[a35b458]544
[3a5909f]545 range_size = pci_bar_mask_to_size(mask);
[a35b458]546
[d93aafed]547 if (addrw64) {
[8b1e15ac]548 range_addr = ((uint64_t)pci_conf_read_32(fun, addr + 4) << 32) |
[663f41c4]549 (val & 0xfffffff0);
[d1fc8f0]550 } else {
551 range_addr = (val & 0xfffffff0);
[663f41c4]552 }
[a35b458]553
[d93aafed]554 if (range_addr != 0) {
[fc51296]555 ddf_msg(LVL_DEBUG, "Function %s : address = %" PRIx64
[56fd7cf]556 ", size = %x", ddf_fun_get_name(fun->fnode), range_addr,
[fc51296]557 (unsigned int) range_size);
[d1fc8f0]558 }
[a35b458]559
[8b1e15ac]560 pci_add_range(fun, range_addr, range_size, io);
[a35b458]561
[d93aafed]562 if (addrw64)
[d1fc8f0]563 return addr + 8;
[a35b458]564
[663f41c4]565 return addr + 4;
[d1fc8f0]566}
567
[68414f4a]568void pci_add_interrupt(pci_fun_t *fun, int irq)
[d1fc8f0]569{
[68414f4a]570 hw_resource_list_t *hw_res_list = &fun->hw_resources;
[663f41c4]571 hw_resource_t *hw_resources = hw_res_list->resources;
572 size_t count = hw_res_list->count;
[a35b458]573
[3a5909f]574 assert(NULL != hw_resources);
575 assert(count < PCI_MAX_HW_RES);
[a35b458]576
[3a5909f]577 hw_resources[count].type = INTERRUPT;
578 hw_resources[count].res.interrupt.irq = irq;
[a35b458]579
[663f41c4]580 hw_res_list->count++;
[a35b458]581
[56fd7cf]582 ddf_msg(LVL_NOTE, "Function %s uses irq %x.", ddf_fun_get_name(fun->fnode), irq);
[3a5909f]583}
584
[68414f4a]585void pci_read_interrupt(pci_fun_t *fun)
[3a5909f]586{
[8b1e15ac]587 uint8_t irq = pci_conf_read_8(fun, PCI_BRIDGE_INT_LINE);
[65f77f4]588 uint8_t pin = pci_conf_read_8(fun, PCI_BRIDGE_INT_PIN);
589
590 if (pin != 0 && irq != 0xff)
[8b1e15ac]591 pci_add_interrupt(fun, irq);
[d1fc8f0]592}
593
594/** Enumerate (recursively) and register the devices connected to a pci bus.
[663f41c4]595 *
[68414f4a]596 * @param bus Host-to-PCI bridge
597 * @param bus_num Bus number
[184f2f8a]598 *
599 * @return EOK on success, ENOENT if no PCI devices found, ENOMEM if out of
600 * memory, EIO on other I/O error
[d1fc8f0]601 */
[184f2f8a]602errno_t pci_bus_scan(pci_bus_t *bus, int bus_num)
[5e598e0]603{
[97a62fe]604 pci_fun_t *fun;
[b7fd2a0]605 errno_t rc;
[a35b458]606
[5e598e0]607 int child_bus = 0;
608 int dnum, fnum;
609 bool multi;
[8b1e15ac]610 uint8_t header_type;
[184f2f8a]611 bool device_found;
612
613 device_found = false;
[a35b458]614
[5e598e0]615 for (dnum = 0; dnum < 32; dnum++) {
616 multi = true;
617 for (fnum = 0; multi && fnum < 8; fnum++) {
[56fd7cf]618 fun = pci_fun_new(bus);
[a35b458]619
[68414f4a]620 pci_fun_init(fun, bus_num, dnum, fnum);
621 if (fun->vendor_id == 0xffff) {
[56fd7cf]622 pci_fun_delete(fun);
[663f41c4]623 /*
624 * The device is not present, go on scanning the
625 * bus.
626 */
627 if (fnum == 0)
[5e598e0]628 break;
[663f41c4]629 else
630 continue;
[5e598e0]631 }
[a35b458]632
[184f2f8a]633 device_found = true;
634
[8b1e15ac]635 header_type = pci_conf_read_8(fun, PCI_HEADER_TYPE);
[5e598e0]636 if (fnum == 0) {
[663f41c4]637 /* Is the device multifunction? */
638 multi = header_type >> 7;
[5e598e0]639 }
[663f41c4]640 /* Clear the multifunction bit. */
641 header_type = header_type & 0x7F;
[a35b458]642
[97a62fe]643 char *fun_name = pci_fun_create_name(fun);
644 if (fun_name == NULL) {
[ebcb05a]645 ddf_msg(LVL_ERROR, "Out of memory.");
[56fd7cf]646 pci_fun_delete(fun);
[184f2f8a]647 return ENOMEM;
[97a62fe]648 }
[a35b458]649
[56fd7cf]650 rc = ddf_fun_set_name(fun->fnode, fun_name);
[cb94e69b]651 free(fun_name);
[56fd7cf]652 if (rc != EOK) {
653 ddf_msg(LVL_ERROR, "Failed setting function name.");
654 pci_fun_delete(fun);
[184f2f8a]655 return EIO;
[97a62fe]656 }
[a35b458]657
[8b1e15ac]658 pci_alloc_resource_list(fun);
659 pci_read_bars(fun);
660 pci_read_interrupt(fun);
[6dbc500]661
662 /* Propagate the PIO window to the function. */
663 fun->pio_window = bus->pio_win;
[a35b458]664
[56fd7cf]665 ddf_fun_set_ops(fun->fnode, &pci_fun_ops);
[a35b458]666
[ebcb05a]667 ddf_msg(LVL_DEBUG, "Adding new function %s.",
[56fd7cf]668 ddf_fun_get_name(fun->fnode));
[92d5279]669
[68414f4a]670 pci_fun_create_match_ids(fun);
[a35b458]671
[663f41c4]672 if (header_type == PCI_HEADER_TYPE_BRIDGE ||
[8304889]673 header_type == PCI_HEADER_TYPE_CARDBUS) {
[8b1e15ac]674 child_bus = pci_conf_read_8(fun,
[663f41c4]675 PCI_BRIDGE_SEC_BUS_NUM);
[fc51296]676 ddf_msg(LVL_DEBUG, "Device is pci-to-pci "
[ebcb05a]677 "bridge, secondary bus number = %d.",
[fc51296]678 bus_num);
[184f2f8a]679 if (child_bus > bus_num) {
680 rc = pci_bus_scan(bus, child_bus);
[94ab1fee]681 if (rc != EOK && rc != ENOENT) {
[184f2f8a]682 pci_fun_delete(fun);
683 return rc;
684 }
685 }
686 }
687
688 if (ddf_fun_bind(fun->fnode) != EOK) {
689 pci_clean_resource_list(fun);
690 pci_fun_delete(fun);
691 continue;
[5e598e0]692 }
693 }
694 }
[184f2f8a]695
696 /* Fail bus scan if no devices are found. */
697 if (!device_found)
698 return ENOENT;
699
700 return EOK;
[5e598e0]701}
[8c06905]702
[b7fd2a0]703static errno_t pci_dev_add(ddf_dev_t *dnode)
[8c06905]704{
[6dbc500]705 hw_resource_list_t hw_resources;
[97a62fe]706 pci_bus_t *bus = NULL;
[83a2f43]707 ddf_fun_t *ctl = NULL;
[97a62fe]708 bool got_res = false;
[56fd7cf]709 async_sess_t *sess;
[b7fd2a0]710 errno_t rc;
[a35b458]711
[0c0f823b]712 ddf_msg(LVL_DEBUG, "pci_dev_add");
[a35b458]713
[5f6e25e]714 bus = ddf_dev_data_alloc(dnode, sizeof(pci_bus_t));
[68414f4a]715 if (bus == NULL) {
[0c0f823b]716 ddf_msg(LVL_ERROR, "pci_dev_add allocation failed.");
[97a62fe]717 rc = ENOMEM;
718 goto fail;
[663f41c4]719 }
[5f6e25e]720 fibril_mutex_initialize(&bus->conf_mutex);
721
[68414f4a]722 bus->dnode = dnode;
[a35b458]723
[2fd26bb]724 sess = ddf_dev_parent_sess_get(dnode);
[56fd7cf]725 if (sess == NULL) {
[0c0f823b]726 ddf_msg(LVL_ERROR, "pci_dev_add failed to connect to the "
[79ae36dd]727 "parent driver.");
728 rc = ENOENT;
[97a62fe]729 goto fail;
[8c06905]730 }
[6dbc500]731
732 rc = pio_window_get(sess, &bus->pio_win);
733 if (rc != EOK) {
734 ddf_msg(LVL_ERROR, "pci_dev_add failed to get PIO window "
735 "for the device.");
736 goto fail;
737 }
[a35b458]738
[56fd7cf]739 rc = hw_res_get_resource_list(sess, &hw_resources);
[be942bc]740 if (rc != EOK) {
[0c0f823b]741 ddf_msg(LVL_ERROR, "pci_dev_add failed to get hw resources "
[ebcb05a]742 "for the device.");
[97a62fe]743 goto fail;
[bab6388]744 }
[97a62fe]745 got_res = true;
[a35b458]746
[92d5279]747 assert(hw_resources.count >= 1);
748
749 if (hw_resources.count == 1) {
750 assert(hw_resources.resources[0].type == MEM_RANGE);
751
752 ddf_msg(LVL_DEBUG, "conf_addr_space = %" PRIx64 ".",
753 hw_resources.resources[0].res.mem_range.address);
754
755 if (pio_enable_resource(&bus->pio_win,
[9e9ced0]756 &hw_resources.resources[0], (void **) &bus->conf_space,
[848e880f]757 NULL, NULL)) {
[92d5279]758 ddf_msg(LVL_ERROR,
759 "Failed to map configuration space.");
760 rc = EADDRNOTAVAIL;
761 goto fail;
762 }
[a35b458]763
[92d5279]764 } else {
765 assert(hw_resources.resources[0].type == IO_RANGE);
766 assert(hw_resources.resources[0].res.io_range.size >= 4);
[a35b458]767
[92d5279]768 assert(hw_resources.resources[1].type == IO_RANGE);
769 assert(hw_resources.resources[1].res.io_range.size >= 4);
[a35b458]770
[92d5279]771 ddf_msg(LVL_DEBUG, "conf_addr = %" PRIx64 ".",
772 hw_resources.resources[0].res.io_range.address);
773 ddf_msg(LVL_DEBUG, "data_addr = %" PRIx64 ".",
774 hw_resources.resources[1].res.io_range.address);
[a35b458]775
[92d5279]776 if (pio_enable_resource(&bus->pio_win,
[9e9ced0]777 &hw_resources.resources[0], (void **) &bus->conf_addr_reg,
[848e880f]778 NULL, NULL)) {
[92d5279]779 ddf_msg(LVL_ERROR,
780 "Failed to enable configuration ports.");
781 rc = EADDRNOTAVAIL;
782 goto fail;
783 }
784 if (pio_enable_resource(&bus->pio_win,
[9e9ced0]785 &hw_resources.resources[1], (void **) &bus->conf_data_reg,
[848e880f]786 NULL, NULL)) {
[92d5279]787 ddf_msg(LVL_ERROR,
788 "Failed to enable configuration ports.");
789 rc = EADDRNOTAVAIL;
790 goto fail;
791 }
[230385c]792 }
[a35b458]793
[68414f4a]794 /* Make the bus device more visible. It has no use yet. */
[ebcb05a]795 ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
[a35b458]796
[97a62fe]797 ctl = ddf_fun_create(bus->dnode, fun_exposed, "ctl");
798 if (ctl == NULL) {
[ebcb05a]799 ddf_msg(LVL_ERROR, "Failed creating control function.");
[97a62fe]800 rc = ENOMEM;
801 goto fail;
802 }
[a35b458]803
[184f2f8a]804 /* Enumerate functions. */
805 ddf_msg(LVL_DEBUG, "Enumerating the bus");
806 rc = pci_bus_scan(bus, 0);
807 if (rc != EOK) {
808 ddf_msg(LVL_ERROR, "Bus enumeration failed.");
809 goto fail;
810 }
811
[97a62fe]812 rc = ddf_fun_bind(ctl);
813 if (rc != EOK) {
[ebcb05a]814 ddf_msg(LVL_ERROR, "Failed binding control function.");
[97a62fe]815 goto fail;
816 }
[a35b458]817
[f724e82]818 hw_res_clean_resource_list(&hw_resources);
[a35b458]819
[df747b9c]820 return EOK;
[a35b458]821
[97a62fe]822fail:
823 if (got_res)
824 hw_res_clean_resource_list(&hw_resources);
[a35b458]825
[97a62fe]826 if (ctl != NULL)
827 ddf_fun_destroy(ctl);
[a35b458]828
[97a62fe]829 return rc;
[8c06905]830}
831
[b7fd2a0]832static errno_t pci_fun_online(ddf_fun_t *fun)
[f278930]833{
834 ddf_msg(LVL_DEBUG, "pci_fun_online()");
835 return ddf_fun_online(fun);
836}
837
[b7fd2a0]838static errno_t pci_fun_offline(ddf_fun_t *fun)
[f278930]839{
840 ddf_msg(LVL_DEBUG, "pci_fun_offline()");
841 return ddf_fun_offline(fun);
842}
843
[663f41c4]844static void pciintel_init(void)
[3843ecb]845{
[267f235]846 ddf_log_init(NAME);
[3843ecb]847}
848
[97a62fe]849pci_fun_t *pci_fun_new(pci_bus_t *bus)
[713a4b9]850{
[97a62fe]851 pci_fun_t *fun;
[56fd7cf]852 ddf_fun_t *fnode;
[a35b458]853
[56fd7cf]854 fnode = ddf_fun_create(bus->dnode, fun_inner, NULL);
855 if (fnode == NULL)
856 return NULL;
857
858 fun = ddf_fun_data_alloc(fnode, sizeof(pci_fun_t));
[97a62fe]859 if (fun == NULL)
860 return NULL;
861
862 fun->busptr = bus;
[56fd7cf]863 fun->fnode = fnode;
[97a62fe]864 return fun;
[713a4b9]865}
866
[68414f4a]867void pci_fun_init(pci_fun_t *fun, int bus, int dev, int fn)
[713a4b9]868{
[68414f4a]869 fun->bus = bus;
870 fun->dev = dev;
871 fun->fn = fn;
[1d53a78]872 fun->vendor_id = pci_conf_read_16(fun, PCI_VENDOR_ID);
873 fun->device_id = pci_conf_read_16(fun, PCI_DEVICE_ID);
[a35b458]874
[c90aed4]875 /* Explicitly enable PCI bus mastering */
876 fun->command = pci_conf_read_16(fun, PCI_COMMAND) |
877 PCI_COMMAND_MASTER;
878 pci_conf_write_16(fun, PCI_COMMAND, fun->command);
[a35b458]879
[1d53a78]880 fun->class_code = pci_conf_read_8(fun, PCI_BASE_CLASS);
881 fun->subclass_code = pci_conf_read_8(fun, PCI_SUB_CLASS);
882 fun->prog_if = pci_conf_read_8(fun, PCI_PROG_IF);
883 fun->revision = pci_conf_read_8(fun, PCI_REVISION_ID);
[713a4b9]884}
885
[68414f4a]886void pci_fun_delete(pci_fun_t *fun)
[713a4b9]887{
[bab6388]888 hw_res_clean_resource_list(&fun->hw_resources);
[56fd7cf]889 if (fun->fnode != NULL)
890 ddf_fun_destroy(fun->fnode);
[713a4b9]891}
892
[97a62fe]893char *pci_fun_create_name(pci_fun_t *fun)
[713a4b9]894{
895 char *name = NULL;
[a35b458]896
[68414f4a]897 asprintf(&name, "%02x:%02x.%01x", fun->bus, fun->dev,
898 fun->fn);
[97a62fe]899 return name;
[713a4b9]900}
901
[68414f4a]902bool pci_alloc_resource_list(pci_fun_t *fun)
[713a4b9]903{
[992b47ea]904 fun->hw_resources.resources = fun->resources;
905 return true;
[713a4b9]906}
907
[68414f4a]908void pci_clean_resource_list(pci_fun_t *fun)
[713a4b9]909{
[992b47ea]910 fun->hw_resources.resources = NULL;
[713a4b9]911}
912
[68414f4a]913/** Read the base address registers (BARs) of the function and add the addresses
914 * to its HW resource list.
[713a4b9]915 *
[68414f4a]916 * @param fun PCI function
[713a4b9]917 */
[68414f4a]918void pci_read_bars(pci_fun_t *fun)
[713a4b9]919{
920 /*
921 * Position of the BAR in the PCI configuration address space of the
922 * device.
923 */
924 int addr = PCI_BASE_ADDR_0;
[a35b458]925
[713a4b9]926 while (addr <= PCI_BASE_ADDR_5)
[8b1e15ac]927 addr = pci_read_bar(fun, addr);
[713a4b9]928}
929
930size_t pci_bar_mask_to_size(uint32_t mask)
931{
[ad6857c]932 size_t size = mask & ~(mask - 1);
933 return size;
[713a4b9]934}
935
[8c06905]936int main(int argc, char *argv[])
937{
[ebcb05a]938 printf(NAME ": HelenOS PCI bus driver (Intel method 1).\n");
[3843ecb]939 pciintel_init();
[83a2f43]940 return ddf_driver_main(&pci_driver);
[8c06905]941}
942
943/**
944 * @}
[472020fc]945 */
Note: See TracBrowser for help on using the repository browser.