source: mainline/uspace/drv/pciintel/pci.c@ dc75234

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

Remove possible endless cycle

  • Property mode set to 100644
File size: 16.4 KB
RevLine 
[8c06905]1/*
2 * Copyright (c) 2010 Lenka Trochtova
[68414f4a]3 * Copyright (c) 2011 Jiri Svoboda
[8c06905]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/**
31 * @defgroup pciintel pci bus driver for intel method 1.
32 * @brief HelenOS root pci bus driver for intel method 1.
33 * @{
34 */
35
36/** @file
37 */
38
39#include <assert.h>
40#include <stdio.h>
41#include <errno.h>
42#include <bool.h>
43#include <fibril_synch.h>
[c47e1a8]44#include <str.h>
[8c06905]45#include <ctype.h>
46#include <macros.h>
[cd0684d]47#include <str_error.h>
[8c06905]48
[af6b5157]49#include <ddf/driver.h>
[8c06905]50#include <devman.h>
51#include <ipc/devman.h>
52#include <ipc/dev_iface.h>
[fb78ae72]53#include <ipc/irc.h>
54#include <ipc/ns.h>
55#include <ipc/services.h>
56#include <sysinfo.h>
[41b56084]57#include <ops/hw_res.h>
[8c06905]58#include <device/hw_res.h>
59#include <ddi.h>
[5e598e0]60#include <libarch/ddi.h>
[99e6bfb]61#include <pci_dev_iface.h>
[5e598e0]62
63#include "pci.h"
[8c06905]64
65#define NAME "pciintel"
66
[663f41c4]67#define CONF_ADDR(bus, dev, fn, reg) \
68 ((1 << 31) | (bus << 16) | (dev << 11) | (fn << 8) | (reg & ~3))
[5e598e0]69
[68414f4a]70/** Obtain PCI function soft-state from DDF function node */
71#define PCI_FUN(fnode) ((pci_fun_t *) (fnode)->driver_data)
72
73/** Obtain PCI bus soft-state from DDF device node */
74#define PCI_BUS(dnode) ((pci_bus_t *) (dnode)->driver_data)
75
76/** Obtain PCI bus soft-state from function soft-state */
[97a62fe]77#define PCI_BUS_FROM_FUN(fun) ((fun)->busptr)
[68414f4a]78
[83a2f43]79static hw_resource_list_t *pciintel_get_resources(ddf_fun_t *fnode)
[3843ecb]80{
[68414f4a]81 pci_fun_t *fun = PCI_FUN(fnode);
[663f41c4]82
[68414f4a]83 if (fun == NULL)
[3843ecb]84 return NULL;
[68414f4a]85 return &fun->hw_resources;
[3843ecb]86}
87
[83a2f43]88static bool pciintel_enable_interrupt(ddf_fun_t *fnode)
[3843ecb]89{
[fb78ae72]90 /* This is an old ugly way, copied from ne2000 driver */
[eb1a2f4]91 assert(fnode);
92 pci_fun_t *dev_data = (pci_fun_t *) fnode->driver_data;
[fb78ae72]93
94 sysarg_t apic;
95 sysarg_t i8259;
[dc75234]96
[fb78ae72]97 int irc_phone = -1;
98 int irc_service = 0;
99
100 if ((sysinfo_get_value("apic", &apic) == EOK) && (apic)) {
101 irc_service = SERVICE_APIC;
102 } else if ((sysinfo_get_value("i8259", &i8259) == EOK) && (i8259)) {
103 irc_service = SERVICE_I8259;
104 }
105
[dc75234]106 if (irc_service == 0)
107 return false;
108
109 irc_phone = service_connect_blocking(irc_service, 0, 0);
110 if (irc_phone < 0)
[fb78ae72]111 return false;
112
113 size_t i;
114 for (i = 0; i < dev_data->hw_resources.count; i++) {
115 if (dev_data->hw_resources.resources[i].type == INTERRUPT) {
116 int irq = dev_data->hw_resources.resources[i].res.interrupt.irq;
[dc75234]117 int rc = async_req_1_0(irc_phone, IRC_ENABLE_INTERRUPT, irq);
118 if (rc != EOK) {
119 async_hangup(irc_phone);
120 return false;
121 }
[fb78ae72]122 }
123 }
124
125 async_hangup(irc_phone);
126 return true;
[3843ecb]127}
128
[99e6bfb]129static int pci_config_space_write_16(ddf_fun_t *fun, uint32_t address, uint16_t data)
130{
131 if (address > 254)
132 return EINVAL;
133 pci_conf_write_16(PCI_FUN(fun), address, data);
134 return EOK;
135}
136
137
[68414f4a]138static hw_res_ops_t pciintel_hw_res_ops = {
139 &pciintel_get_resources,
140 &pciintel_enable_interrupt
[3843ecb]141};
142
[99e6bfb]143static pci_dev_iface_t pci_dev_ops = {
144 .config_space_read_8 = NULL,
145 .config_space_read_16 = NULL,
146 .config_space_read_32 = NULL,
147 .config_space_write_8 = NULL,
148 .config_space_write_16 = &pci_config_space_write_16,
149 .config_space_write_32 = NULL
150};
151
152static ddf_dev_ops_t pci_fun_ops = {
153 .interfaces[HW_RES_DEV_IFACE] = &pciintel_hw_res_ops,
154 .interfaces[PCI_DEV_IFACE] = &pci_dev_ops
155};
[3843ecb]156
[83a2f43]157static int pci_add_device(ddf_dev_t *);
[3843ecb]158
[68414f4a]159/** PCI bus driver standard operations */
[8c06905]160static driver_ops_t pci_ops = {
161 .add_device = &pci_add_device
162};
163
[68414f4a]164/** PCI bus driver structure */
[8c06905]165static driver_t pci_driver = {
166 .name = NAME,
167 .driver_ops = &pci_ops
168};
169
[68414f4a]170static pci_bus_t *pci_bus_new(void)
[5e598e0]171{
[68414f4a]172 pci_bus_t *bus;
[663f41c4]173
[bab6388]174 bus = (pci_bus_t *) calloc(1, sizeof(pci_bus_t));
175 if (bus == NULL)
176 return NULL;
177
178 fibril_mutex_initialize(&bus->conf_mutex);
[68414f4a]179 return bus;
[5e598e0]180}
181
[68414f4a]182static void pci_bus_delete(pci_bus_t *bus)
[5e598e0]183{
[bab6388]184 assert(bus != NULL);
[68414f4a]185 free(bus);
[5e598e0]186}
187
[68414f4a]188static void pci_conf_read(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
[5e598e0]189{
[68414f4a]190 pci_bus_t *bus = PCI_BUS_FROM_FUN(fun);
[5e598e0]191
[68414f4a]192 fibril_mutex_lock(&bus->conf_mutex);
[5e598e0]193
[663f41c4]194 uint32_t conf_addr;
[68414f4a]195 conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
196 void *addr = bus->conf_data_port + (reg & 3);
[5e598e0]197
[68414f4a]198 pio_write_32(bus->conf_addr_port, conf_addr);
[5e598e0]199
200 switch (len) {
[663f41c4]201 case 1:
202 buf[0] = pio_read_8(addr);
203 break;
204 case 2:
205 ((uint16_t *) buf)[0] = pio_read_16(addr);
206 break;
207 case 4:
208 ((uint32_t *) buf)[0] = pio_read_32(addr);
209 break;
[5e598e0]210 }
211
[68414f4a]212 fibril_mutex_unlock(&bus->conf_mutex);
[5e598e0]213}
214
[68414f4a]215static void pci_conf_write(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
[d1fc8f0]216{
[68414f4a]217 pci_bus_t *bus = PCI_BUS_FROM_FUN(fun);
[d1fc8f0]218
[68414f4a]219 fibril_mutex_lock(&bus->conf_mutex);
[d1fc8f0]220
[663f41c4]221 uint32_t conf_addr;
[68414f4a]222 conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
223 void *addr = bus->conf_data_port + (reg & 3);
[d1fc8f0]224
[68414f4a]225 pio_write_32(bus->conf_addr_port, conf_addr);
[d1fc8f0]226
227 switch (len) {
[663f41c4]228 case 1:
229 pio_write_8(addr, buf[0]);
230 break;
231 case 2:
232 pio_write_16(addr, ((uint16_t *) buf)[0]);
233 break;
234 case 4:
235 pio_write_32(addr, ((uint32_t *) buf)[0]);
236 break;
[d1fc8f0]237 }
238
[68414f4a]239 fibril_mutex_unlock(&bus->conf_mutex);
[d1fc8f0]240}
241
[68414f4a]242uint8_t pci_conf_read_8(pci_fun_t *fun, int reg)
[5e598e0]243{
244 uint8_t res;
[8b1e15ac]245 pci_conf_read(fun, reg, &res, 1);
[5e598e0]246 return res;
247}
248
[68414f4a]249uint16_t pci_conf_read_16(pci_fun_t *fun, int reg)
[5e598e0]250{
251 uint16_t res;
[8b1e15ac]252 pci_conf_read(fun, reg, (uint8_t *) &res, 2);
[5e598e0]253 return res;
254}
255
[68414f4a]256uint32_t pci_conf_read_32(pci_fun_t *fun, int reg)
[5e598e0]257{
258 uint32_t res;
[8b1e15ac]259 pci_conf_read(fun, reg, (uint8_t *) &res, 4);
[663f41c4]260 return res;
[5e598e0]261}
262
[68414f4a]263void pci_conf_write_8(pci_fun_t *fun, int reg, uint8_t val)
[d1fc8f0]264{
[8b1e15ac]265 pci_conf_write(fun, reg, (uint8_t *) &val, 1);
[d1fc8f0]266}
267
[68414f4a]268void pci_conf_write_16(pci_fun_t *fun, int reg, uint16_t val)
[d1fc8f0]269{
[8b1e15ac]270 pci_conf_write(fun, reg, (uint8_t *) &val, 2);
[d1fc8f0]271}
272
[68414f4a]273void pci_conf_write_32(pci_fun_t *fun, int reg, uint32_t val)
[d1fc8f0]274{
[8b1e15ac]275 pci_conf_write(fun, reg, (uint8_t *) &val, 4);
[d1fc8f0]276}
277
[68414f4a]278void pci_fun_create_match_ids(pci_fun_t *fun)
[89ce401a]279{
[663f41c4]280 char *match_id_str;
[cd0684d]281 int rc;
[663f41c4]282
[cd0684d]283 asprintf(&match_id_str, "pci/ven=%04x&dev=%04x",
284 fun->vendor_id, fun->device_id);
285
286 if (match_id_str == NULL) {
287 printf(NAME ": out of memory creating match ID.\n");
288 return;
[8304889]289 }
290
[cd0684d]291 rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 90);
292 if (rc != EOK) {
293 printf(NAME ": error adding match ID: %s\n",
294 str_error(rc));
[8304889]295 }
[bab6388]296
[663f41c4]297 /* TODO add more ids (with subsys ids, using class id etc.) */
[89ce401a]298}
299
[68414f4a]300void pci_add_range(pci_fun_t *fun, uint64_t range_addr, size_t range_size,
301 bool io)
[d1fc8f0]302{
[68414f4a]303 hw_resource_list_t *hw_res_list = &fun->hw_resources;
[3a5909f]304 hw_resource_t *hw_resources = hw_res_list->resources;
[663f41c4]305 size_t count = hw_res_list->count;
[3a5909f]306
[8304889]307 assert(hw_resources != NULL);
[3a5909f]308 assert(count < PCI_MAX_HW_RES);
309
310 if (io) {
311 hw_resources[count].type = IO_RANGE;
312 hw_resources[count].res.io_range.address = range_addr;
[663f41c4]313 hw_resources[count].res.io_range.size = range_size;
314 hw_resources[count].res.io_range.endianness = LITTLE_ENDIAN;
[3a5909f]315 } else {
316 hw_resources[count].type = MEM_RANGE;
317 hw_resources[count].res.mem_range.address = range_addr;
[663f41c4]318 hw_resources[count].res.mem_range.size = range_size;
[3a5909f]319 hw_resources[count].res.mem_range.endianness = LITTLE_ENDIAN;
320 }
321
[663f41c4]322 hw_res_list->count++;
[d1fc8f0]323}
324
[663f41c4]325/** Read the base address register (BAR) of the device and if it contains valid
326 * address add it to the devices hw resource list.
327 *
[68414f4a]328 * @param fun PCI function
[663f41c4]329 * @param addr The address of the BAR in the PCI configuration address space of
[68414f4a]330 * the device
331 * @return The addr the address of the BAR which should be read next
[d1fc8f0]332 */
[68414f4a]333int pci_read_bar(pci_fun_t *fun, int addr)
[bab6388]334{
[663f41c4]335 /* Value of the BAR */
[d1fc8f0]336 uint32_t val, mask;
[663f41c4]337 /* IO space address */
[d1fc8f0]338 bool io;
[663f41c4]339 /* 64-bit wide address */
[d93aafed]340 bool addrw64;
[d1fc8f0]341
[663f41c4]342 /* Size of the io or memory range specified by the BAR */
[d1fc8f0]343 size_t range_size;
[663f41c4]344 /* Beginning of the io or memory range specified by the BAR */
[d1fc8f0]345 uint64_t range_addr;
346
[663f41c4]347 /* Get the value of the BAR. */
[8b1e15ac]348 val = pci_conf_read_32(fun, addr);
[ad6857c]349
350#define IO_MASK (~0x3)
351#define MEM_MASK (~0xf)
[d1fc8f0]352
[663f41c4]353 io = (bool) (val & 1);
[d1fc8f0]354 if (io) {
[d93aafed]355 addrw64 = false;
[ad6857c]356 mask = IO_MASK;
[d1fc8f0]357 } else {
[ad6857c]358 mask = MEM_MASK;
[d1fc8f0]359 switch ((val >> 1) & 3) {
360 case 0:
[d93aafed]361 addrw64 = false;
[d1fc8f0]362 break;
363 case 2:
[d93aafed]364 addrw64 = true;
[d1fc8f0]365 break;
366 default:
[663f41c4]367 /* reserved, go to the next BAR */
368 return addr + 4;
[d1fc8f0]369 }
370 }
371
[663f41c4]372 /* Get the address mask. */
[8b1e15ac]373 pci_conf_write_32(fun, addr, 0xffffffff);
[ad6857c]374 mask &= pci_conf_read_32(fun, addr);
[d1fc8f0]375
[663f41c4]376 /* Restore the original value. */
[8b1e15ac]377 pci_conf_write_32(fun, addr, val);
378 val = pci_conf_read_32(fun, addr);
[d1fc8f0]379
[3a5909f]380 range_size = pci_bar_mask_to_size(mask);
[d1fc8f0]381
[d93aafed]382 if (addrw64) {
[8b1e15ac]383 range_addr = ((uint64_t)pci_conf_read_32(fun, addr + 4) << 32) |
[663f41c4]384 (val & 0xfffffff0);
[d1fc8f0]385 } else {
386 range_addr = (val & 0xfffffff0);
[663f41c4]387 }
388
[d93aafed]389 if (range_addr != 0) {
[68414f4a]390 printf(NAME ": function %s : ", fun->fnode->name);
[7e752b2]391 printf("address = %" PRIx64, range_addr);
[ab3a851]392 printf(", size = %x\n", (unsigned int) range_size);
[d1fc8f0]393 }
394
[8b1e15ac]395 pci_add_range(fun, range_addr, range_size, io);
[d1fc8f0]396
[d93aafed]397 if (addrw64)
[d1fc8f0]398 return addr + 8;
[663f41c4]399
400 return addr + 4;
[d1fc8f0]401}
402
[68414f4a]403void pci_add_interrupt(pci_fun_t *fun, int irq)
[d1fc8f0]404{
[68414f4a]405 hw_resource_list_t *hw_res_list = &fun->hw_resources;
[663f41c4]406 hw_resource_t *hw_resources = hw_res_list->resources;
407 size_t count = hw_res_list->count;
[d1fc8f0]408
[3a5909f]409 assert(NULL != hw_resources);
410 assert(count < PCI_MAX_HW_RES);
411
412 hw_resources[count].type = INTERRUPT;
413 hw_resources[count].res.interrupt.irq = irq;
414
[663f41c4]415 hw_res_list->count++;
[3a5909f]416
[68414f4a]417 printf(NAME ": function %s uses irq %x.\n", fun->fnode->name, irq);
[3a5909f]418}
419
[68414f4a]420void pci_read_interrupt(pci_fun_t *fun)
[3a5909f]421{
[8b1e15ac]422 uint8_t irq = pci_conf_read_8(fun, PCI_BRIDGE_INT_LINE);
[8304889]423 if (irq != 0xff)
[8b1e15ac]424 pci_add_interrupt(fun, irq);
[d1fc8f0]425}
426
427/** Enumerate (recursively) and register the devices connected to a pci bus.
[663f41c4]428 *
[68414f4a]429 * @param bus Host-to-PCI bridge
430 * @param bus_num Bus number
[d1fc8f0]431 */
[68414f4a]432void pci_bus_scan(pci_bus_t *bus, int bus_num)
[5e598e0]433{
[83a2f43]434 ddf_fun_t *fnode;
[97a62fe]435 pci_fun_t *fun;
[5e598e0]436
437 int child_bus = 0;
438 int dnum, fnum;
439 bool multi;
[8b1e15ac]440 uint8_t header_type;
[bab6388]441
[97a62fe]442 fun = pci_fun_new(bus);
[5e598e0]443
444 for (dnum = 0; dnum < 32; dnum++) {
445 multi = true;
446 for (fnum = 0; multi && fnum < 8; fnum++) {
[68414f4a]447 pci_fun_init(fun, bus_num, dnum, fnum);
448 fun->vendor_id = pci_conf_read_16(fun,
[663f41c4]449 PCI_VENDOR_ID);
[68414f4a]450 fun->device_id = pci_conf_read_16(fun,
[663f41c4]451 PCI_DEVICE_ID);
[68414f4a]452 if (fun->vendor_id == 0xffff) {
[663f41c4]453 /*
454 * The device is not present, go on scanning the
455 * bus.
456 */
457 if (fnum == 0)
[5e598e0]458 break;
[663f41c4]459 else
460 continue;
[5e598e0]461 }
[663f41c4]462
[8b1e15ac]463 header_type = pci_conf_read_8(fun, PCI_HEADER_TYPE);
[5e598e0]464 if (fnum == 0) {
[663f41c4]465 /* Is the device multifunction? */
466 multi = header_type >> 7;
[5e598e0]467 }
[663f41c4]468 /* Clear the multifunction bit. */
469 header_type = header_type & 0x7F;
[5e598e0]470
[97a62fe]471 char *fun_name = pci_fun_create_name(fun);
472 if (fun_name == NULL) {
473 printf(NAME ": out of memory.\n");
474 return;
475 }
476
477 fnode = ddf_fun_create(bus->dnode, fun_inner, fun_name);
478 if (fnode == NULL) {
479 printf(NAME ": error creating function.\n");
480 return;
481 }
[3a5909f]482
[97a62fe]483 free(fun_name);
484 fun->fnode = fnode;
[3a5909f]485
[8b1e15ac]486 pci_alloc_resource_list(fun);
487 pci_read_bars(fun);
488 pci_read_interrupt(fun);
[89ce401a]489
[68414f4a]490 fnode->ops = &pci_fun_ops;
[97a62fe]491 fnode->driver_data = fun;
[89ce401a]492
[8b1e15ac]493 printf(NAME ": adding new function %s.\n",
[68414f4a]494 fnode->name);
[89ce401a]495
[68414f4a]496 pci_fun_create_match_ids(fun);
[89ce401a]497
[97a62fe]498 if (ddf_fun_bind(fnode) != EOK) {
[8b1e15ac]499 pci_clean_resource_list(fun);
[68414f4a]500 clean_match_ids(&fnode->match_ids);
501 free((char *) fnode->name);
502 fnode->name = NULL;
[89ce401a]503 continue;
504 }
[5e598e0]505
[663f41c4]506 if (header_type == PCI_HEADER_TYPE_BRIDGE ||
[8304889]507 header_type == PCI_HEADER_TYPE_CARDBUS) {
[8b1e15ac]508 child_bus = pci_conf_read_8(fun,
[663f41c4]509 PCI_BRIDGE_SEC_BUS_NUM);
510 printf(NAME ": device is pci-to-pci bridge, "
511 "secondary bus number = %d.\n", bus_num);
[8304889]512 if (child_bus > bus_num)
[68414f4a]513 pci_bus_scan(bus, child_bus);
[5e598e0]514 }
515
[97a62fe]516 fun = pci_fun_new(bus);
[5e598e0]517 }
518 }
519
[68414f4a]520 if (fun->vendor_id == 0xffff) {
[8b1e15ac]521 /* Free the auxiliary function structure. */
[68414f4a]522 pci_fun_delete(fun);
[663f41c4]523 }
[5e598e0]524}
[8c06905]525
[83a2f43]526static int pci_add_device(ddf_dev_t *dnode)
[8c06905]527{
[97a62fe]528 pci_bus_t *bus = NULL;
[83a2f43]529 ddf_fun_t *ctl = NULL;
[97a62fe]530 bool got_res = false;
[be942bc]531 int rc;
[68414f4a]532
[8c06905]533 printf(NAME ": pci_add_device\n");
[97a62fe]534 dnode->parent_phone = -1;
[8c06905]535
[97a62fe]536 bus = pci_bus_new();
[68414f4a]537 if (bus == NULL) {
[8c06905]538 printf(NAME ": pci_add_device allocation failed.\n");
[97a62fe]539 rc = ENOMEM;
540 goto fail;
[663f41c4]541 }
[68414f4a]542 bus->dnode = dnode;
543 dnode->driver_data = bus;
[8c06905]544
[68414f4a]545 dnode->parent_phone = devman_parent_device_connect(dnode->handle,
[663f41c4]546 IPC_FLAG_BLOCKING);
[68414f4a]547 if (dnode->parent_phone < 0) {
[663f41c4]548 printf(NAME ": pci_add_device failed to connect to the "
549 "parent's driver.\n");
[97a62fe]550 rc = dnode->parent_phone;
551 goto fail;
[8c06905]552 }
553
554 hw_resource_list_t hw_resources;
555
[68414f4a]556 rc = hw_res_get_resource_list(dnode->parent_phone, &hw_resources);
[be942bc]557 if (rc != EOK) {
[663f41c4]558 printf(NAME ": pci_add_device failed to get hw resources for "
559 "the device.\n");
[97a62fe]560 goto fail;
[bab6388]561 }
[97a62fe]562 got_res = true;
[8c06905]563
[7e752b2]564 printf(NAME ": conf_addr = %" PRIx64 ".\n",
[663f41c4]565 hw_resources.resources[0].res.io_range.address);
[8c06905]566
567 assert(hw_resources.count > 0);
[3a5909f]568 assert(hw_resources.resources[0].type == IO_RANGE);
569 assert(hw_resources.resources[0].res.io_range.size == 8);
[8c06905]570
[68414f4a]571 bus->conf_io_addr =
[663f41c4]572 (uint32_t) hw_resources.resources[0].res.io_range.address;
[8c06905]573
[68414f4a]574 if (pio_enable((void *)(uintptr_t)bus->conf_io_addr, 8,
575 &bus->conf_addr_port)) {
[8c06905]576 printf(NAME ": failed to enable configuration ports.\n");
[97a62fe]577 rc = EADDRNOTAVAIL;
578 goto fail;
[8c06905]579 }
[68414f4a]580 bus->conf_data_port = (char *) bus->conf_addr_port + 4;
[8c06905]581
[68414f4a]582 /* Make the bus device more visible. It has no use yet. */
[8b1e15ac]583 printf(NAME ": adding a 'ctl' function\n");
[68414f4a]584
[97a62fe]585 ctl = ddf_fun_create(bus->dnode, fun_exposed, "ctl");
586 if (ctl == NULL) {
587 printf(NAME ": error creating control function.\n");
588 rc = ENOMEM;
589 goto fail;
590 }
591
592 rc = ddf_fun_bind(ctl);
593 if (rc != EOK) {
594 printf(NAME ": error binding control function.\n");
595 goto fail;
596 }
[8c06905]597
[68414f4a]598 /* Enumerate functions. */
[89ce401a]599 printf(NAME ": scanning the bus\n");
[68414f4a]600 pci_bus_scan(bus, 0);
[8c06905]601
[f724e82]602 hw_res_clean_resource_list(&hw_resources);
[8c06905]603
[df747b9c]604 return EOK;
[97a62fe]605
606fail:
607 if (bus != NULL)
608 pci_bus_delete(bus);
609 if (dnode->parent_phone >= 0)
610 async_hangup(dnode->parent_phone);
611 if (got_res)
612 hw_res_clean_resource_list(&hw_resources);
613 if (ctl != NULL)
614 ddf_fun_destroy(ctl);
615
616 return rc;
[8c06905]617}
618
[663f41c4]619static void pciintel_init(void)
[3843ecb]620{
[68414f4a]621 pci_fun_ops.interfaces[HW_RES_DEV_IFACE] = &pciintel_hw_res_ops;
[99e6bfb]622 pci_fun_ops.interfaces[PCI_DEV_IFACE] = &pci_dev_ops;
[3843ecb]623}
624
[97a62fe]625pci_fun_t *pci_fun_new(pci_bus_t *bus)
[713a4b9]626{
[97a62fe]627 pci_fun_t *fun;
[713a4b9]628
[97a62fe]629 fun = (pci_fun_t *) calloc(1, sizeof(pci_fun_t));
630 if (fun == NULL)
631 return NULL;
632
633 fun->busptr = bus;
634 return fun;
[713a4b9]635}
636
[68414f4a]637void pci_fun_init(pci_fun_t *fun, int bus, int dev, int fn)
[713a4b9]638{
[68414f4a]639 fun->bus = bus;
640 fun->dev = dev;
641 fun->fn = fn;
[713a4b9]642}
643
[68414f4a]644void pci_fun_delete(pci_fun_t *fun)
[713a4b9]645{
[bab6388]646 assert(fun != NULL);
647 hw_res_clean_resource_list(&fun->hw_resources);
648 free(fun);
[713a4b9]649}
650
[97a62fe]651char *pci_fun_create_name(pci_fun_t *fun)
[713a4b9]652{
653 char *name = NULL;
654
[68414f4a]655 asprintf(&name, "%02x:%02x.%01x", fun->bus, fun->dev,
656 fun->fn);
[97a62fe]657 return name;
[713a4b9]658}
659
[68414f4a]660bool pci_alloc_resource_list(pci_fun_t *fun)
[713a4b9]661{
[68414f4a]662 fun->hw_resources.resources =
[713a4b9]663 (hw_resource_t *) malloc(PCI_MAX_HW_RES * sizeof(hw_resource_t));
[68414f4a]664 return fun->hw_resources.resources != NULL;
[713a4b9]665}
666
[68414f4a]667void pci_clean_resource_list(pci_fun_t *fun)
[713a4b9]668{
[68414f4a]669 if (fun->hw_resources.resources != NULL) {
670 free(fun->hw_resources.resources);
671 fun->hw_resources.resources = NULL;
[713a4b9]672 }
673}
674
[68414f4a]675/** Read the base address registers (BARs) of the function and add the addresses
676 * to its HW resource list.
[713a4b9]677 *
[68414f4a]678 * @param fun PCI function
[713a4b9]679 */
[68414f4a]680void pci_read_bars(pci_fun_t *fun)
[713a4b9]681{
682 /*
683 * Position of the BAR in the PCI configuration address space of the
684 * device.
685 */
686 int addr = PCI_BASE_ADDR_0;
687
688 while (addr <= PCI_BASE_ADDR_5)
[8b1e15ac]689 addr = pci_read_bar(fun, addr);
[713a4b9]690}
691
692size_t pci_bar_mask_to_size(uint32_t mask)
693{
[ad6857c]694 size_t size = mask & ~(mask - 1);
695 return size;
[713a4b9]696}
697
[8c06905]698int main(int argc, char *argv[])
699{
[3843ecb]700 printf(NAME ": HelenOS pci bus driver (intel method 1).\n");
701 pciintel_init();
[83a2f43]702 return ddf_driver_main(&pci_driver);
[8c06905]703}
704
705/**
706 * @}
[472020fc]707 */
Note: See TracBrowser for help on using the repository browser.