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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e67c50a was 5a6cc679, checked in by Jenda <jenda.jzqk73@…>, 8 years ago

Merge commit '50f19b7ee8e94570b5c63896736c4eb49cfa18db' into forwardport

Not all ints are converted to errno_t in xhci tree yet, however it compiles and works :)

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