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

Last change on this file since 0dab4850 was 7acd787, checked in by Jiri Svoboda <jiri@…>, 6 years ago

Utility for listing PCI devices

Currently all information presented can also be gathered from devctl -v,
although 'pci' prints it in a nice compact format. Ability to print
more information, including information not available from devctl,
can be added later.

  • Property mode set to 100644
File size: 23.6 KB
RevLine 
[8c06905]1/*
[7acd787]2 * Copyright (c) 2019 Jiri Svoboda
[8c06905]3 * Copyright (c) 2010 Lenka Trochtova
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
[7acd787]60#include "ctl.h"
[5e598e0]61#include "pci.h"
[7acd787]62#include "pci_regs.h"
[8c06905]63
64#define NAME "pciintel"
65
[587478b]66#define CONF_ADDR_ENABLE (((unsigned)1) << 31)
[663f41c4]67#define CONF_ADDR(bus, dev, fn, reg) \
[92d5279]68 ((bus << 16) | (dev << 11) | (fn << 8) | (reg & ~3))
[5e598e0]69
[68414f4a]70/** Obtain PCI function soft-state from DDF function node */
[56fd7cf]71static pci_fun_t *pci_fun(ddf_fun_t *fnode)
72{
73 return ddf_fun_data_get(fnode);
74}
[68414f4a]75
76/** Obtain PCI bus soft-state from DDF device node */
[7acd787]77pci_bus_t *pci_bus(ddf_dev_t *dnode)
[56fd7cf]78{
79 return ddf_dev_data_get(dnode);
80}
[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
[7acd787]452/** Get first PCI function.
453 *
454 * @param bus PCI bus
455 * @return First PCI function on @a bus or @c NULL if there is none
456 */
457pci_fun_t *pci_fun_first(pci_bus_t *bus)
458{
459 link_t *link;
460
461 link = list_first(&bus->funs);
462 if (link == NULL)
463 return NULL;
464
465 return list_get_instance(link, pci_fun_t, lfuns);
466}
467
468/** Get next PCI function.
469 *
470 * @param cur Current function
471 * @return Next PCI function on the same bus or @c NULL if there is none
472 */
473pci_fun_t *pci_fun_next(pci_fun_t *cur)
474{
475 link_t *link;
476
477 link = list_next(&cur->lfuns, &cur->busptr->funs);
478 if (link == NULL)
479 return NULL;
480
481 return list_get_instance(link, pci_fun_t, lfuns);
482}
483
[68414f4a]484void pci_add_range(pci_fun_t *fun, uint64_t range_addr, size_t range_size,
485 bool io)
[d1fc8f0]486{
[68414f4a]487 hw_resource_list_t *hw_res_list = &fun->hw_resources;
[3a5909f]488 hw_resource_t *hw_resources = hw_res_list->resources;
[663f41c4]489 size_t count = hw_res_list->count;
[a35b458]490
[8304889]491 assert(hw_resources != NULL);
[3a5909f]492 assert(count < PCI_MAX_HW_RES);
[a35b458]493
[3a5909f]494 if (io) {
495 hw_resources[count].type = IO_RANGE;
496 hw_resources[count].res.io_range.address = range_addr;
[663f41c4]497 hw_resources[count].res.io_range.size = range_size;
[9e470c0]498 hw_resources[count].res.io_range.relative = true;
[663f41c4]499 hw_resources[count].res.io_range.endianness = LITTLE_ENDIAN;
[3a5909f]500 } else {
501 hw_resources[count].type = MEM_RANGE;
502 hw_resources[count].res.mem_range.address = range_addr;
[663f41c4]503 hw_resources[count].res.mem_range.size = range_size;
[9e470c0]504 hw_resources[count].res.mem_range.relative = false;
[3a5909f]505 hw_resources[count].res.mem_range.endianness = LITTLE_ENDIAN;
506 }
[a35b458]507
[663f41c4]508 hw_res_list->count++;
[d1fc8f0]509}
510
[663f41c4]511/** Read the base address register (BAR) of the device and if it contains valid
512 * address add it to the devices hw resource list.
513 *
[68414f4a]514 * @param fun PCI function
[663f41c4]515 * @param addr The address of the BAR in the PCI configuration address space of
[68414f4a]516 * the device
517 * @return The addr the address of the BAR which should be read next
[d1fc8f0]518 */
[68414f4a]519int pci_read_bar(pci_fun_t *fun, int addr)
[bab6388]520{
[663f41c4]521 /* Value of the BAR */
[e8d6ce2]522 uint32_t val;
523 uint32_t bar;
524 uint32_t mask;
525
[663f41c4]526 /* IO space address */
[d1fc8f0]527 bool io;
[663f41c4]528 /* 64-bit wide address */
[d93aafed]529 bool addrw64;
[a35b458]530
[663f41c4]531 /* Size of the io or memory range specified by the BAR */
[d1fc8f0]532 size_t range_size;
[663f41c4]533 /* Beginning of the io or memory range specified by the BAR */
[d1fc8f0]534 uint64_t range_addr;
[a35b458]535
[663f41c4]536 /* Get the value of the BAR. */
[8b1e15ac]537 val = pci_conf_read_32(fun, addr);
[ad6857c]538
539#define IO_MASK (~0x3)
540#define MEM_MASK (~0xf)
[a35b458]541
[c81132d]542 io = (val & 1) != 0;
[d1fc8f0]543 if (io) {
[d93aafed]544 addrw64 = false;
[ad6857c]545 mask = IO_MASK;
[d1fc8f0]546 } else {
[ad6857c]547 mask = MEM_MASK;
[d1fc8f0]548 switch ((val >> 1) & 3) {
549 case 0:
[d93aafed]550 addrw64 = false;
[d1fc8f0]551 break;
552 case 2:
[d93aafed]553 addrw64 = true;
[d1fc8f0]554 break;
555 default:
[663f41c4]556 /* reserved, go to the next BAR */
557 return addr + 4;
[d1fc8f0]558 }
559 }
[a35b458]560
[663f41c4]561 /* Get the address mask. */
[8b1e15ac]562 pci_conf_write_32(fun, addr, 0xffffffff);
[e8d6ce2]563 bar = pci_conf_read_32(fun, addr);
564
565 /*
[ae7d03c]566 * Unimplemented BARs read back as all 0's.
567 */
[e8d6ce2]568 if (!bar)
569 return addr + (addrw64 ? 8 : 4);
570
[1b20da0]571 mask &= bar;
[e8d6ce2]572
[663f41c4]573 /* Restore the original value. */
[8b1e15ac]574 pci_conf_write_32(fun, addr, val);
575 val = pci_conf_read_32(fun, addr);
[a35b458]576
[3a5909f]577 range_size = pci_bar_mask_to_size(mask);
[a35b458]578
[d93aafed]579 if (addrw64) {
[8b1e15ac]580 range_addr = ((uint64_t)pci_conf_read_32(fun, addr + 4) << 32) |
[663f41c4]581 (val & 0xfffffff0);
[d1fc8f0]582 } else {
583 range_addr = (val & 0xfffffff0);
[663f41c4]584 }
[a35b458]585
[d93aafed]586 if (range_addr != 0) {
[fc51296]587 ddf_msg(LVL_DEBUG, "Function %s : address = %" PRIx64
[56fd7cf]588 ", size = %x", ddf_fun_get_name(fun->fnode), range_addr,
[fc51296]589 (unsigned int) range_size);
[d1fc8f0]590 }
[a35b458]591
[8b1e15ac]592 pci_add_range(fun, range_addr, range_size, io);
[a35b458]593
[d93aafed]594 if (addrw64)
[d1fc8f0]595 return addr + 8;
[a35b458]596
[663f41c4]597 return addr + 4;
[d1fc8f0]598}
599
[68414f4a]600void pci_add_interrupt(pci_fun_t *fun, int irq)
[d1fc8f0]601{
[68414f4a]602 hw_resource_list_t *hw_res_list = &fun->hw_resources;
[663f41c4]603 hw_resource_t *hw_resources = hw_res_list->resources;
604 size_t count = hw_res_list->count;
[a35b458]605
[3a5909f]606 assert(NULL != hw_resources);
607 assert(count < PCI_MAX_HW_RES);
[a35b458]608
[3a5909f]609 hw_resources[count].type = INTERRUPT;
610 hw_resources[count].res.interrupt.irq = irq;
[a35b458]611
[663f41c4]612 hw_res_list->count++;
[a35b458]613
[56fd7cf]614 ddf_msg(LVL_NOTE, "Function %s uses irq %x.", ddf_fun_get_name(fun->fnode), irq);
[3a5909f]615}
616
[68414f4a]617void pci_read_interrupt(pci_fun_t *fun)
[3a5909f]618{
[8b1e15ac]619 uint8_t irq = pci_conf_read_8(fun, PCI_BRIDGE_INT_LINE);
[65f77f4]620 uint8_t pin = pci_conf_read_8(fun, PCI_BRIDGE_INT_PIN);
621
622 if (pin != 0 && irq != 0xff)
[8b1e15ac]623 pci_add_interrupt(fun, irq);
[d1fc8f0]624}
625
626/** Enumerate (recursively) and register the devices connected to a pci bus.
[663f41c4]627 *
[68414f4a]628 * @param bus Host-to-PCI bridge
629 * @param bus_num Bus number
[184f2f8a]630 *
631 * @return EOK on success, ENOENT if no PCI devices found, ENOMEM if out of
632 * memory, EIO on other I/O error
[d1fc8f0]633 */
[184f2f8a]634errno_t pci_bus_scan(pci_bus_t *bus, int bus_num)
[5e598e0]635{
[97a62fe]636 pci_fun_t *fun;
[b7fd2a0]637 errno_t rc;
[a35b458]638
[5e598e0]639 int child_bus = 0;
640 int dnum, fnum;
641 bool multi;
[8b1e15ac]642 uint8_t header_type;
[184f2f8a]643 bool device_found;
644
645 device_found = false;
[a35b458]646
[5e598e0]647 for (dnum = 0; dnum < 32; dnum++) {
648 multi = true;
649 for (fnum = 0; multi && fnum < 8; fnum++) {
[56fd7cf]650 fun = pci_fun_new(bus);
[a35b458]651
[68414f4a]652 pci_fun_init(fun, bus_num, dnum, fnum);
653 if (fun->vendor_id == 0xffff) {
[56fd7cf]654 pci_fun_delete(fun);
[663f41c4]655 /*
656 * The device is not present, go on scanning the
657 * bus.
658 */
659 if (fnum == 0)
[5e598e0]660 break;
[663f41c4]661 else
662 continue;
[5e598e0]663 }
[a35b458]664
[184f2f8a]665 device_found = true;
666
[8b1e15ac]667 header_type = pci_conf_read_8(fun, PCI_HEADER_TYPE);
[5e598e0]668 if (fnum == 0) {
[663f41c4]669 /* Is the device multifunction? */
670 multi = header_type >> 7;
[5e598e0]671 }
[663f41c4]672 /* Clear the multifunction bit. */
673 header_type = header_type & 0x7F;
[a35b458]674
[97a62fe]675 char *fun_name = pci_fun_create_name(fun);
676 if (fun_name == NULL) {
[ebcb05a]677 ddf_msg(LVL_ERROR, "Out of memory.");
[56fd7cf]678 pci_fun_delete(fun);
[184f2f8a]679 return ENOMEM;
[97a62fe]680 }
[a35b458]681
[56fd7cf]682 rc = ddf_fun_set_name(fun->fnode, fun_name);
[cb94e69b]683 free(fun_name);
[56fd7cf]684 if (rc != EOK) {
685 ddf_msg(LVL_ERROR, "Failed setting function name.");
686 pci_fun_delete(fun);
[184f2f8a]687 return EIO;
[97a62fe]688 }
[a35b458]689
[8b1e15ac]690 pci_alloc_resource_list(fun);
691 pci_read_bars(fun);
692 pci_read_interrupt(fun);
[6dbc500]693
694 /* Propagate the PIO window to the function. */
695 fun->pio_window = bus->pio_win;
[a35b458]696
[56fd7cf]697 ddf_fun_set_ops(fun->fnode, &pci_fun_ops);
[a35b458]698
[ebcb05a]699 ddf_msg(LVL_DEBUG, "Adding new function %s.",
[56fd7cf]700 ddf_fun_get_name(fun->fnode));
[92d5279]701
[68414f4a]702 pci_fun_create_match_ids(fun);
[a35b458]703
[663f41c4]704 if (header_type == PCI_HEADER_TYPE_BRIDGE ||
[8304889]705 header_type == PCI_HEADER_TYPE_CARDBUS) {
[8b1e15ac]706 child_bus = pci_conf_read_8(fun,
[663f41c4]707 PCI_BRIDGE_SEC_BUS_NUM);
[fc51296]708 ddf_msg(LVL_DEBUG, "Device is pci-to-pci "
[ebcb05a]709 "bridge, secondary bus number = %d.",
[fc51296]710 bus_num);
[184f2f8a]711 if (child_bus > bus_num) {
712 rc = pci_bus_scan(bus, child_bus);
[94ab1fee]713 if (rc != EOK && rc != ENOENT) {
[184f2f8a]714 pci_fun_delete(fun);
715 return rc;
716 }
717 }
718 }
719
720 if (ddf_fun_bind(fun->fnode) != EOK) {
721 pci_clean_resource_list(fun);
722 pci_fun_delete(fun);
723 continue;
[5e598e0]724 }
[7acd787]725
726 list_append(&fun->lfuns, &bus->funs);
[5e598e0]727 }
728 }
[184f2f8a]729
730 /* Fail bus scan if no devices are found. */
731 if (!device_found)
732 return ENOENT;
733
734 return EOK;
[5e598e0]735}
[8c06905]736
[b7fd2a0]737static errno_t pci_dev_add(ddf_dev_t *dnode)
[8c06905]738{
[6dbc500]739 hw_resource_list_t hw_resources;
[97a62fe]740 pci_bus_t *bus = NULL;
[83a2f43]741 ddf_fun_t *ctl = NULL;
[97a62fe]742 bool got_res = false;
[56fd7cf]743 async_sess_t *sess;
[b7fd2a0]744 errno_t rc;
[a35b458]745
[0c0f823b]746 ddf_msg(LVL_DEBUG, "pci_dev_add");
[a35b458]747
[5f6e25e]748 bus = ddf_dev_data_alloc(dnode, sizeof(pci_bus_t));
[68414f4a]749 if (bus == NULL) {
[0c0f823b]750 ddf_msg(LVL_ERROR, "pci_dev_add allocation failed.");
[97a62fe]751 rc = ENOMEM;
752 goto fail;
[663f41c4]753 }
[7acd787]754
755 list_initialize(&bus->funs);
[5f6e25e]756 fibril_mutex_initialize(&bus->conf_mutex);
757
[68414f4a]758 bus->dnode = dnode;
[a35b458]759
[2fd26bb]760 sess = ddf_dev_parent_sess_get(dnode);
[56fd7cf]761 if (sess == NULL) {
[0c0f823b]762 ddf_msg(LVL_ERROR, "pci_dev_add failed to connect to the "
[79ae36dd]763 "parent driver.");
764 rc = ENOENT;
[97a62fe]765 goto fail;
[8c06905]766 }
[6dbc500]767
768 rc = pio_window_get(sess, &bus->pio_win);
769 if (rc != EOK) {
770 ddf_msg(LVL_ERROR, "pci_dev_add failed to get PIO window "
771 "for the device.");
772 goto fail;
773 }
[a35b458]774
[56fd7cf]775 rc = hw_res_get_resource_list(sess, &hw_resources);
[be942bc]776 if (rc != EOK) {
[0c0f823b]777 ddf_msg(LVL_ERROR, "pci_dev_add failed to get hw resources "
[ebcb05a]778 "for the device.");
[97a62fe]779 goto fail;
[bab6388]780 }
[97a62fe]781 got_res = true;
[a35b458]782
[92d5279]783 assert(hw_resources.count >= 1);
784
785 if (hw_resources.count == 1) {
786 assert(hw_resources.resources[0].type == MEM_RANGE);
787
788 ddf_msg(LVL_DEBUG, "conf_addr_space = %" PRIx64 ".",
789 hw_resources.resources[0].res.mem_range.address);
790
791 if (pio_enable_resource(&bus->pio_win,
[9e9ced0]792 &hw_resources.resources[0], (void **) &bus->conf_space,
[848e880f]793 NULL, NULL)) {
[92d5279]794 ddf_msg(LVL_ERROR,
795 "Failed to map configuration space.");
796 rc = EADDRNOTAVAIL;
797 goto fail;
798 }
[a35b458]799
[92d5279]800 } else {
801 assert(hw_resources.resources[0].type == IO_RANGE);
802 assert(hw_resources.resources[0].res.io_range.size >= 4);
[a35b458]803
[92d5279]804 assert(hw_resources.resources[1].type == IO_RANGE);
805 assert(hw_resources.resources[1].res.io_range.size >= 4);
[a35b458]806
[92d5279]807 ddf_msg(LVL_DEBUG, "conf_addr = %" PRIx64 ".",
808 hw_resources.resources[0].res.io_range.address);
809 ddf_msg(LVL_DEBUG, "data_addr = %" PRIx64 ".",
810 hw_resources.resources[1].res.io_range.address);
[a35b458]811
[92d5279]812 if (pio_enable_resource(&bus->pio_win,
[9e9ced0]813 &hw_resources.resources[0], (void **) &bus->conf_addr_reg,
[848e880f]814 NULL, NULL)) {
[92d5279]815 ddf_msg(LVL_ERROR,
816 "Failed to enable configuration ports.");
817 rc = EADDRNOTAVAIL;
818 goto fail;
819 }
820 if (pio_enable_resource(&bus->pio_win,
[9e9ced0]821 &hw_resources.resources[1], (void **) &bus->conf_data_reg,
[848e880f]822 NULL, NULL)) {
[92d5279]823 ddf_msg(LVL_ERROR,
824 "Failed to enable configuration ports.");
825 rc = EADDRNOTAVAIL;
826 goto fail;
827 }
[230385c]828 }
[a35b458]829
[68414f4a]830 /* Make the bus device more visible. It has no use yet. */
[ebcb05a]831 ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
[a35b458]832
[97a62fe]833 ctl = ddf_fun_create(bus->dnode, fun_exposed, "ctl");
834 if (ctl == NULL) {
[ebcb05a]835 ddf_msg(LVL_ERROR, "Failed creating control function.");
[97a62fe]836 rc = ENOMEM;
837 goto fail;
838 }
[a35b458]839
[7acd787]840 ddf_fun_set_conn_handler(ctl, pci_ctl_connection);
841
[184f2f8a]842 /* Enumerate functions. */
843 ddf_msg(LVL_DEBUG, "Enumerating the bus");
844 rc = pci_bus_scan(bus, 0);
845 if (rc != EOK) {
846 ddf_msg(LVL_ERROR, "Bus enumeration failed.");
847 goto fail;
848 }
849
[97a62fe]850 rc = ddf_fun_bind(ctl);
851 if (rc != EOK) {
[ebcb05a]852 ddf_msg(LVL_ERROR, "Failed binding control function.");
[97a62fe]853 goto fail;
854 }
[a35b458]855
[7acd787]856 rc = ddf_fun_add_to_category(ctl, "pci");
857 if (rc != EOK) {
858 ddf_msg(LVL_ERROR, "Failed adding control function to category "
859 "'pci'.");
860 goto fail;
861 }
862
[f724e82]863 hw_res_clean_resource_list(&hw_resources);
[a35b458]864
[df747b9c]865 return EOK;
[a35b458]866
[97a62fe]867fail:
868 if (got_res)
869 hw_res_clean_resource_list(&hw_resources);
[a35b458]870
[97a62fe]871 if (ctl != NULL)
872 ddf_fun_destroy(ctl);
[a35b458]873
[97a62fe]874 return rc;
[8c06905]875}
876
[b7fd2a0]877static errno_t pci_fun_online(ddf_fun_t *fun)
[f278930]878{
879 ddf_msg(LVL_DEBUG, "pci_fun_online()");
880 return ddf_fun_online(fun);
881}
882
[b7fd2a0]883static errno_t pci_fun_offline(ddf_fun_t *fun)
[f278930]884{
885 ddf_msg(LVL_DEBUG, "pci_fun_offline()");
886 return ddf_fun_offline(fun);
887}
888
[663f41c4]889static void pciintel_init(void)
[3843ecb]890{
[267f235]891 ddf_log_init(NAME);
[3843ecb]892}
893
[97a62fe]894pci_fun_t *pci_fun_new(pci_bus_t *bus)
[713a4b9]895{
[97a62fe]896 pci_fun_t *fun;
[56fd7cf]897 ddf_fun_t *fnode;
[a35b458]898
[56fd7cf]899 fnode = ddf_fun_create(bus->dnode, fun_inner, NULL);
900 if (fnode == NULL)
901 return NULL;
902
903 fun = ddf_fun_data_alloc(fnode, sizeof(pci_fun_t));
[97a62fe]904 if (fun == NULL)
905 return NULL;
906
907 fun->busptr = bus;
[56fd7cf]908 fun->fnode = fnode;
[97a62fe]909 return fun;
[713a4b9]910}
911
[68414f4a]912void pci_fun_init(pci_fun_t *fun, int bus, int dev, int fn)
[713a4b9]913{
[68414f4a]914 fun->bus = bus;
915 fun->dev = dev;
916 fun->fn = fn;
[1d53a78]917 fun->vendor_id = pci_conf_read_16(fun, PCI_VENDOR_ID);
918 fun->device_id = pci_conf_read_16(fun, PCI_DEVICE_ID);
[a35b458]919
[c90aed4]920 /* Explicitly enable PCI bus mastering */
921 fun->command = pci_conf_read_16(fun, PCI_COMMAND) |
922 PCI_COMMAND_MASTER;
923 pci_conf_write_16(fun, PCI_COMMAND, fun->command);
[a35b458]924
[1d53a78]925 fun->class_code = pci_conf_read_8(fun, PCI_BASE_CLASS);
926 fun->subclass_code = pci_conf_read_8(fun, PCI_SUB_CLASS);
927 fun->prog_if = pci_conf_read_8(fun, PCI_PROG_IF);
928 fun->revision = pci_conf_read_8(fun, PCI_REVISION_ID);
[713a4b9]929}
930
[68414f4a]931void pci_fun_delete(pci_fun_t *fun)
[713a4b9]932{
[bab6388]933 hw_res_clean_resource_list(&fun->hw_resources);
[56fd7cf]934 if (fun->fnode != NULL)
935 ddf_fun_destroy(fun->fnode);
[713a4b9]936}
937
[97a62fe]938char *pci_fun_create_name(pci_fun_t *fun)
[713a4b9]939{
940 char *name = NULL;
[a35b458]941
[68414f4a]942 asprintf(&name, "%02x:%02x.%01x", fun->bus, fun->dev,
943 fun->fn);
[97a62fe]944 return name;
[713a4b9]945}
946
[68414f4a]947bool pci_alloc_resource_list(pci_fun_t *fun)
[713a4b9]948{
[992b47ea]949 fun->hw_resources.resources = fun->resources;
950 return true;
[713a4b9]951}
952
[68414f4a]953void pci_clean_resource_list(pci_fun_t *fun)
[713a4b9]954{
[992b47ea]955 fun->hw_resources.resources = NULL;
[713a4b9]956}
957
[68414f4a]958/** Read the base address registers (BARs) of the function and add the addresses
959 * to its HW resource list.
[713a4b9]960 *
[68414f4a]961 * @param fun PCI function
[713a4b9]962 */
[68414f4a]963void pci_read_bars(pci_fun_t *fun)
[713a4b9]964{
965 /*
966 * Position of the BAR in the PCI configuration address space of the
967 * device.
968 */
969 int addr = PCI_BASE_ADDR_0;
[a35b458]970
[713a4b9]971 while (addr <= PCI_BASE_ADDR_5)
[8b1e15ac]972 addr = pci_read_bar(fun, addr);
[713a4b9]973}
974
975size_t pci_bar_mask_to_size(uint32_t mask)
976{
[ad6857c]977 size_t size = mask & ~(mask - 1);
978 return size;
[713a4b9]979}
980
[8c06905]981int main(int argc, char *argv[])
982{
[ebcb05a]983 printf(NAME ": HelenOS PCI bus driver (Intel method 1).\n");
[3843ecb]984 pciintel_init();
[83a2f43]985 return ddf_driver_main(&pci_driver);
[8c06905]986}
987
988/**
989 * @}
[472020fc]990 */
Note: See TracBrowser for help on using the repository browser.