source: mainline/uspace/drv/pciintel/pci.c@ 8e7d724

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8e7d724 was 51e5608, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Merge mainline changes (0.4.3 Sashimi)

  • Property mode set to 100644
File size: 17.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>
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
[91579d5]94 sysarg_t apic;
95 sysarg_t i8259;
[dc75234]96
[51e5608]97 int irc_phone = ENOTSUP;
[fb78ae72]98
[51e5608]99 if (((sysinfo_get_value("apic", &apic) == EOK) && (apic))
100 || ((sysinfo_get_value("i8259", &i8259) == EOK) && (i8259))) {
101 irc_phone = service_connect_blocking(SERVICE_IRC, 0, 0);
[fb78ae72]102 }
103
[91579d5]104 if (irc_phone < 0) {
[fb78ae72]105 return false;
[91579d5]106 }
[fb78ae72]107
108 size_t i;
[91579d5]109 for (i = 0; i < dev_data->hw_resources.count; i++) {
[fb78ae72]110 if (dev_data->hw_resources.resources[i].type == INTERRUPT) {
111 int irq = dev_data->hw_resources.resources[i].res.interrupt.irq;
[dc75234]112 int rc = async_req_1_0(irc_phone, IRC_ENABLE_INTERRUPT, irq);
113 if (rc != EOK) {
114 async_hangup(irc_phone);
115 return false;
116 }
[fb78ae72]117 }
118 }
119
120 async_hangup(irc_phone);
121 return true;
[3843ecb]122}
123
[40a5d40]124static int pci_config_space_write_32(
125 ddf_fun_t *fun, uint32_t address, uint32_t data)
126{
127 if (address > 252)
128 return EINVAL;
129 pci_conf_write_32(PCI_FUN(fun), address, data);
130 return EOK;
131}
132
133static int pci_config_space_write_16(
134 ddf_fun_t *fun, uint32_t address, uint16_t data)
[99e6bfb]135{
136 if (address > 254)
137 return EINVAL;
138 pci_conf_write_16(PCI_FUN(fun), address, data);
139 return EOK;
140}
141
[40a5d40]142static int pci_config_space_write_8(
143 ddf_fun_t *fun, uint32_t address, uint8_t data)
144{
145 if (address > 255)
146 return EINVAL;
147 pci_conf_write_8(PCI_FUN(fun), address, data);
148 return EOK;
149}
150
151static int pci_config_space_read_32(
152 ddf_fun_t *fun, uint32_t address, uint32_t *data)
153{
154 if (address > 252)
155 return EINVAL;
156 *data = pci_conf_read_32(PCI_FUN(fun), address);
157 return EOK;
158}
159
160static int pci_config_space_read_16(
161 ddf_fun_t *fun, uint32_t address, uint16_t *data)
162{
163 if (address > 254)
164 return EINVAL;
165 *data = pci_conf_read_16(PCI_FUN(fun), address);
166 return EOK;
167}
168
169static int pci_config_space_read_8(
170 ddf_fun_t *fun, uint32_t address, uint8_t *data)
171{
172 if (address > 255)
173 return EINVAL;
174 *data = pci_conf_read_8(PCI_FUN(fun), address);
175 return EOK;
176}
[99e6bfb]177
[68414f4a]178static hw_res_ops_t pciintel_hw_res_ops = {
179 &pciintel_get_resources,
180 &pciintel_enable_interrupt
[3843ecb]181};
182
[99e6bfb]183static pci_dev_iface_t pci_dev_ops = {
[40a5d40]184 .config_space_read_8 = &pci_config_space_read_8,
185 .config_space_read_16 = &pci_config_space_read_16,
186 .config_space_read_32 = &pci_config_space_read_32,
187 .config_space_write_8 = &pci_config_space_write_8,
[99e6bfb]188 .config_space_write_16 = &pci_config_space_write_16,
[40a5d40]189 .config_space_write_32 = &pci_config_space_write_32
[99e6bfb]190};
191
192static ddf_dev_ops_t pci_fun_ops = {
193 .interfaces[HW_RES_DEV_IFACE] = &pciintel_hw_res_ops,
194 .interfaces[PCI_DEV_IFACE] = &pci_dev_ops
195};
[3843ecb]196
[83a2f43]197static int pci_add_device(ddf_dev_t *);
[3843ecb]198
[68414f4a]199/** PCI bus driver standard operations */
[8c06905]200static driver_ops_t pci_ops = {
201 .add_device = &pci_add_device
202};
203
[68414f4a]204/** PCI bus driver structure */
[8c06905]205static driver_t pci_driver = {
206 .name = NAME,
207 .driver_ops = &pci_ops
208};
209
[68414f4a]210static pci_bus_t *pci_bus_new(void)
[5e598e0]211{
[68414f4a]212 pci_bus_t *bus;
[663f41c4]213
[bab6388]214 bus = (pci_bus_t *) calloc(1, sizeof(pci_bus_t));
215 if (bus == NULL)
216 return NULL;
217
218 fibril_mutex_initialize(&bus->conf_mutex);
[68414f4a]219 return bus;
[5e598e0]220}
221
[68414f4a]222static void pci_bus_delete(pci_bus_t *bus)
[5e598e0]223{
[bab6388]224 assert(bus != NULL);
[68414f4a]225 free(bus);
[5e598e0]226}
227
[68414f4a]228static void pci_conf_read(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
[5e598e0]229{
[68414f4a]230 pci_bus_t *bus = PCI_BUS_FROM_FUN(fun);
[5e598e0]231
[68414f4a]232 fibril_mutex_lock(&bus->conf_mutex);
[5e598e0]233
[663f41c4]234 uint32_t conf_addr;
[68414f4a]235 conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
236 void *addr = bus->conf_data_port + (reg & 3);
[5e598e0]237
[68414f4a]238 pio_write_32(bus->conf_addr_port, conf_addr);
[5e598e0]239
240 switch (len) {
[663f41c4]241 case 1:
242 buf[0] = pio_read_8(addr);
243 break;
244 case 2:
245 ((uint16_t *) buf)[0] = pio_read_16(addr);
246 break;
247 case 4:
248 ((uint32_t *) buf)[0] = pio_read_32(addr);
249 break;
[5e598e0]250 }
251
[68414f4a]252 fibril_mutex_unlock(&bus->conf_mutex);
[5e598e0]253}
254
[68414f4a]255static void pci_conf_write(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
[d1fc8f0]256{
[68414f4a]257 pci_bus_t *bus = PCI_BUS_FROM_FUN(fun);
[d1fc8f0]258
[68414f4a]259 fibril_mutex_lock(&bus->conf_mutex);
[d1fc8f0]260
[663f41c4]261 uint32_t conf_addr;
[68414f4a]262 conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
263 void *addr = bus->conf_data_port + (reg & 3);
[d1fc8f0]264
[68414f4a]265 pio_write_32(bus->conf_addr_port, conf_addr);
[d1fc8f0]266
267 switch (len) {
[663f41c4]268 case 1:
269 pio_write_8(addr, buf[0]);
270 break;
271 case 2:
272 pio_write_16(addr, ((uint16_t *) buf)[0]);
273 break;
274 case 4:
275 pio_write_32(addr, ((uint32_t *) buf)[0]);
276 break;
[d1fc8f0]277 }
278
[68414f4a]279 fibril_mutex_unlock(&bus->conf_mutex);
[d1fc8f0]280}
281
[68414f4a]282uint8_t pci_conf_read_8(pci_fun_t *fun, int reg)
[5e598e0]283{
284 uint8_t res;
[8b1e15ac]285 pci_conf_read(fun, reg, &res, 1);
[5e598e0]286 return res;
287}
288
[68414f4a]289uint16_t pci_conf_read_16(pci_fun_t *fun, int reg)
[5e598e0]290{
291 uint16_t res;
[8b1e15ac]292 pci_conf_read(fun, reg, (uint8_t *) &res, 2);
[5e598e0]293 return res;
294}
295
[68414f4a]296uint32_t pci_conf_read_32(pci_fun_t *fun, int reg)
[5e598e0]297{
298 uint32_t res;
[8b1e15ac]299 pci_conf_read(fun, reg, (uint8_t *) &res, 4);
[663f41c4]300 return res;
[5e598e0]301}
302
[68414f4a]303void pci_conf_write_8(pci_fun_t *fun, int reg, uint8_t val)
[d1fc8f0]304{
[8b1e15ac]305 pci_conf_write(fun, reg, (uint8_t *) &val, 1);
[d1fc8f0]306}
307
[68414f4a]308void pci_conf_write_16(pci_fun_t *fun, int reg, uint16_t val)
[d1fc8f0]309{
[8b1e15ac]310 pci_conf_write(fun, reg, (uint8_t *) &val, 2);
[d1fc8f0]311}
312
[68414f4a]313void pci_conf_write_32(pci_fun_t *fun, int reg, uint32_t val)
[d1fc8f0]314{
[8b1e15ac]315 pci_conf_write(fun, reg, (uint8_t *) &val, 4);
[d1fc8f0]316}
317
[68414f4a]318void pci_fun_create_match_ids(pci_fun_t *fun)
[89ce401a]319{
[663f41c4]320 char *match_id_str;
[cd0684d]321 int rc;
[663f41c4]322
[cd0684d]323 asprintf(&match_id_str, "pci/ven=%04x&dev=%04x",
324 fun->vendor_id, fun->device_id);
325
326 if (match_id_str == NULL) {
327 printf(NAME ": out of memory creating match ID.\n");
328 return;
[8304889]329 }
330
[cd0684d]331 rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 90);
332 if (rc != EOK) {
333 printf(NAME ": error adding match ID: %s\n",
334 str_error(rc));
[8304889]335 }
[bab6388]336
[663f41c4]337 /* TODO add more ids (with subsys ids, using class id etc.) */
[89ce401a]338}
339
[68414f4a]340void pci_add_range(pci_fun_t *fun, uint64_t range_addr, size_t range_size,
341 bool io)
[d1fc8f0]342{
[68414f4a]343 hw_resource_list_t *hw_res_list = &fun->hw_resources;
[3a5909f]344 hw_resource_t *hw_resources = hw_res_list->resources;
[663f41c4]345 size_t count = hw_res_list->count;
[3a5909f]346
[8304889]347 assert(hw_resources != NULL);
[3a5909f]348 assert(count < PCI_MAX_HW_RES);
349
350 if (io) {
351 hw_resources[count].type = IO_RANGE;
352 hw_resources[count].res.io_range.address = range_addr;
[663f41c4]353 hw_resources[count].res.io_range.size = range_size;
354 hw_resources[count].res.io_range.endianness = LITTLE_ENDIAN;
[3a5909f]355 } else {
356 hw_resources[count].type = MEM_RANGE;
357 hw_resources[count].res.mem_range.address = range_addr;
[663f41c4]358 hw_resources[count].res.mem_range.size = range_size;
[3a5909f]359 hw_resources[count].res.mem_range.endianness = LITTLE_ENDIAN;
360 }
361
[663f41c4]362 hw_res_list->count++;
[d1fc8f0]363}
364
[663f41c4]365/** Read the base address register (BAR) of the device and if it contains valid
366 * address add it to the devices hw resource list.
367 *
[68414f4a]368 * @param fun PCI function
[663f41c4]369 * @param addr The address of the BAR in the PCI configuration address space of
[68414f4a]370 * the device
371 * @return The addr the address of the BAR which should be read next
[d1fc8f0]372 */
[68414f4a]373int pci_read_bar(pci_fun_t *fun, int addr)
[bab6388]374{
[663f41c4]375 /* Value of the BAR */
[d1fc8f0]376 uint32_t val, mask;
[663f41c4]377 /* IO space address */
[d1fc8f0]378 bool io;
[663f41c4]379 /* 64-bit wide address */
[d93aafed]380 bool addrw64;
[d1fc8f0]381
[663f41c4]382 /* Size of the io or memory range specified by the BAR */
[d1fc8f0]383 size_t range_size;
[663f41c4]384 /* Beginning of the io or memory range specified by the BAR */
[d1fc8f0]385 uint64_t range_addr;
386
[663f41c4]387 /* Get the value of the BAR. */
[8b1e15ac]388 val = pci_conf_read_32(fun, addr);
[ad6857c]389
390#define IO_MASK (~0x3)
391#define MEM_MASK (~0xf)
[d1fc8f0]392
[663f41c4]393 io = (bool) (val & 1);
[d1fc8f0]394 if (io) {
[d93aafed]395 addrw64 = false;
[ad6857c]396 mask = IO_MASK;
[d1fc8f0]397 } else {
[ad6857c]398 mask = MEM_MASK;
[d1fc8f0]399 switch ((val >> 1) & 3) {
400 case 0:
[d93aafed]401 addrw64 = false;
[d1fc8f0]402 break;
403 case 2:
[d93aafed]404 addrw64 = true;
[d1fc8f0]405 break;
406 default:
[663f41c4]407 /* reserved, go to the next BAR */
408 return addr + 4;
[d1fc8f0]409 }
410 }
411
[663f41c4]412 /* Get the address mask. */
[8b1e15ac]413 pci_conf_write_32(fun, addr, 0xffffffff);
[ad6857c]414 mask &= pci_conf_read_32(fun, addr);
[d1fc8f0]415
[663f41c4]416 /* Restore the original value. */
[8b1e15ac]417 pci_conf_write_32(fun, addr, val);
418 val = pci_conf_read_32(fun, addr);
[d1fc8f0]419
[3a5909f]420 range_size = pci_bar_mask_to_size(mask);
[d1fc8f0]421
[d93aafed]422 if (addrw64) {
[8b1e15ac]423 range_addr = ((uint64_t)pci_conf_read_32(fun, addr + 4) << 32) |
[663f41c4]424 (val & 0xfffffff0);
[d1fc8f0]425 } else {
426 range_addr = (val & 0xfffffff0);
[663f41c4]427 }
428
[d93aafed]429 if (range_addr != 0) {
[68414f4a]430 printf(NAME ": function %s : ", fun->fnode->name);
[7e752b2]431 printf("address = %" PRIx64, range_addr);
[ab3a851]432 printf(", size = %x\n", (unsigned int) range_size);
[d1fc8f0]433 }
434
[8b1e15ac]435 pci_add_range(fun, range_addr, range_size, io);
[d1fc8f0]436
[d93aafed]437 if (addrw64)
[d1fc8f0]438 return addr + 8;
[663f41c4]439
440 return addr + 4;
[d1fc8f0]441}
442
[68414f4a]443void pci_add_interrupt(pci_fun_t *fun, int irq)
[d1fc8f0]444{
[68414f4a]445 hw_resource_list_t *hw_res_list = &fun->hw_resources;
[663f41c4]446 hw_resource_t *hw_resources = hw_res_list->resources;
447 size_t count = hw_res_list->count;
[d1fc8f0]448
[3a5909f]449 assert(NULL != hw_resources);
450 assert(count < PCI_MAX_HW_RES);
451
452 hw_resources[count].type = INTERRUPT;
453 hw_resources[count].res.interrupt.irq = irq;
454
[663f41c4]455 hw_res_list->count++;
[3a5909f]456
[68414f4a]457 printf(NAME ": function %s uses irq %x.\n", fun->fnode->name, irq);
[3a5909f]458}
459
[68414f4a]460void pci_read_interrupt(pci_fun_t *fun)
[3a5909f]461{
[8b1e15ac]462 uint8_t irq = pci_conf_read_8(fun, PCI_BRIDGE_INT_LINE);
[8304889]463 if (irq != 0xff)
[8b1e15ac]464 pci_add_interrupt(fun, irq);
[d1fc8f0]465}
466
467/** Enumerate (recursively) and register the devices connected to a pci bus.
[663f41c4]468 *
[68414f4a]469 * @param bus Host-to-PCI bridge
470 * @param bus_num Bus number
[d1fc8f0]471 */
[68414f4a]472void pci_bus_scan(pci_bus_t *bus, int bus_num)
[5e598e0]473{
[83a2f43]474 ddf_fun_t *fnode;
[97a62fe]475 pci_fun_t *fun;
[5e598e0]476
477 int child_bus = 0;
478 int dnum, fnum;
479 bool multi;
[8b1e15ac]480 uint8_t header_type;
[bab6388]481
[97a62fe]482 fun = pci_fun_new(bus);
[5e598e0]483
484 for (dnum = 0; dnum < 32; dnum++) {
485 multi = true;
486 for (fnum = 0; multi && fnum < 8; fnum++) {
[68414f4a]487 pci_fun_init(fun, bus_num, dnum, fnum);
488 fun->vendor_id = pci_conf_read_16(fun,
[663f41c4]489 PCI_VENDOR_ID);
[68414f4a]490 fun->device_id = pci_conf_read_16(fun,
[663f41c4]491 PCI_DEVICE_ID);
[68414f4a]492 if (fun->vendor_id == 0xffff) {
[663f41c4]493 /*
494 * The device is not present, go on scanning the
495 * bus.
496 */
497 if (fnum == 0)
[5e598e0]498 break;
[663f41c4]499 else
500 continue;
[5e598e0]501 }
[663f41c4]502
[8b1e15ac]503 header_type = pci_conf_read_8(fun, PCI_HEADER_TYPE);
[5e598e0]504 if (fnum == 0) {
[663f41c4]505 /* Is the device multifunction? */
506 multi = header_type >> 7;
[5e598e0]507 }
[663f41c4]508 /* Clear the multifunction bit. */
509 header_type = header_type & 0x7F;
[5e598e0]510
[97a62fe]511 char *fun_name = pci_fun_create_name(fun);
512 if (fun_name == NULL) {
513 printf(NAME ": out of memory.\n");
514 return;
515 }
516
517 fnode = ddf_fun_create(bus->dnode, fun_inner, fun_name);
518 if (fnode == NULL) {
519 printf(NAME ": error creating function.\n");
520 return;
521 }
[3a5909f]522
[97a62fe]523 free(fun_name);
524 fun->fnode = fnode;
[3a5909f]525
[8b1e15ac]526 pci_alloc_resource_list(fun);
527 pci_read_bars(fun);
528 pci_read_interrupt(fun);
[89ce401a]529
[68414f4a]530 fnode->ops = &pci_fun_ops;
[97a62fe]531 fnode->driver_data = fun;
[89ce401a]532
[8b1e15ac]533 printf(NAME ": adding new function %s.\n",
[68414f4a]534 fnode->name);
[89ce401a]535
[68414f4a]536 pci_fun_create_match_ids(fun);
[89ce401a]537
[97a62fe]538 if (ddf_fun_bind(fnode) != EOK) {
[8b1e15ac]539 pci_clean_resource_list(fun);
[68414f4a]540 clean_match_ids(&fnode->match_ids);
541 free((char *) fnode->name);
542 fnode->name = NULL;
[89ce401a]543 continue;
544 }
[5e598e0]545
[663f41c4]546 if (header_type == PCI_HEADER_TYPE_BRIDGE ||
[8304889]547 header_type == PCI_HEADER_TYPE_CARDBUS) {
[8b1e15ac]548 child_bus = pci_conf_read_8(fun,
[663f41c4]549 PCI_BRIDGE_SEC_BUS_NUM);
550 printf(NAME ": device is pci-to-pci bridge, "
551 "secondary bus number = %d.\n", bus_num);
[8304889]552 if (child_bus > bus_num)
[68414f4a]553 pci_bus_scan(bus, child_bus);
[5e598e0]554 }
555
[97a62fe]556 fun = pci_fun_new(bus);
[5e598e0]557 }
558 }
559
[68414f4a]560 if (fun->vendor_id == 0xffff) {
[8b1e15ac]561 /* Free the auxiliary function structure. */
[68414f4a]562 pci_fun_delete(fun);
[663f41c4]563 }
[5e598e0]564}
[8c06905]565
[83a2f43]566static int pci_add_device(ddf_dev_t *dnode)
[8c06905]567{
[97a62fe]568 pci_bus_t *bus = NULL;
[83a2f43]569 ddf_fun_t *ctl = NULL;
[97a62fe]570 bool got_res = false;
[be942bc]571 int rc;
[68414f4a]572
[8c06905]573 printf(NAME ": pci_add_device\n");
[97a62fe]574 dnode->parent_phone = -1;
[8c06905]575
[97a62fe]576 bus = pci_bus_new();
[68414f4a]577 if (bus == NULL) {
[8c06905]578 printf(NAME ": pci_add_device allocation failed.\n");
[97a62fe]579 rc = ENOMEM;
580 goto fail;
[663f41c4]581 }
[68414f4a]582 bus->dnode = dnode;
583 dnode->driver_data = bus;
[8c06905]584
[68414f4a]585 dnode->parent_phone = devman_parent_device_connect(dnode->handle,
[663f41c4]586 IPC_FLAG_BLOCKING);
[68414f4a]587 if (dnode->parent_phone < 0) {
[663f41c4]588 printf(NAME ": pci_add_device failed to connect to the "
589 "parent's driver.\n");
[97a62fe]590 rc = dnode->parent_phone;
591 goto fail;
[8c06905]592 }
593
594 hw_resource_list_t hw_resources;
595
[68414f4a]596 rc = hw_res_get_resource_list(dnode->parent_phone, &hw_resources);
[be942bc]597 if (rc != EOK) {
[663f41c4]598 printf(NAME ": pci_add_device failed to get hw resources for "
599 "the device.\n");
[97a62fe]600 goto fail;
[bab6388]601 }
[97a62fe]602 got_res = true;
[8c06905]603
[7e752b2]604 printf(NAME ": conf_addr = %" PRIx64 ".\n",
[663f41c4]605 hw_resources.resources[0].res.io_range.address);
[8c06905]606
607 assert(hw_resources.count > 0);
[3a5909f]608 assert(hw_resources.resources[0].type == IO_RANGE);
609 assert(hw_resources.resources[0].res.io_range.size == 8);
[8c06905]610
[68414f4a]611 bus->conf_io_addr =
[663f41c4]612 (uint32_t) hw_resources.resources[0].res.io_range.address;
[8c06905]613
[68414f4a]614 if (pio_enable((void *)(uintptr_t)bus->conf_io_addr, 8,
615 &bus->conf_addr_port)) {
[8c06905]616 printf(NAME ": failed to enable configuration ports.\n");
[97a62fe]617 rc = EADDRNOTAVAIL;
618 goto fail;
[8c06905]619 }
[68414f4a]620 bus->conf_data_port = (char *) bus->conf_addr_port + 4;
[8c06905]621
[68414f4a]622 /* Make the bus device more visible. It has no use yet. */
[8b1e15ac]623 printf(NAME ": adding a 'ctl' function\n");
[68414f4a]624
[97a62fe]625 ctl = ddf_fun_create(bus->dnode, fun_exposed, "ctl");
626 if (ctl == NULL) {
627 printf(NAME ": error creating control function.\n");
628 rc = ENOMEM;
629 goto fail;
630 }
631
632 rc = ddf_fun_bind(ctl);
633 if (rc != EOK) {
634 printf(NAME ": error binding control function.\n");
635 goto fail;
636 }
[8c06905]637
[68414f4a]638 /* Enumerate functions. */
[89ce401a]639 printf(NAME ": scanning the bus\n");
[68414f4a]640 pci_bus_scan(bus, 0);
[8c06905]641
[f724e82]642 hw_res_clean_resource_list(&hw_resources);
[8c06905]643
[df747b9c]644 return EOK;
[97a62fe]645
646fail:
647 if (bus != NULL)
648 pci_bus_delete(bus);
649 if (dnode->parent_phone >= 0)
650 async_hangup(dnode->parent_phone);
651 if (got_res)
652 hw_res_clean_resource_list(&hw_resources);
653 if (ctl != NULL)
654 ddf_fun_destroy(ctl);
655
656 return rc;
[8c06905]657}
658
[663f41c4]659static void pciintel_init(void)
[3843ecb]660{
[68414f4a]661 pci_fun_ops.interfaces[HW_RES_DEV_IFACE] = &pciintel_hw_res_ops;
[99e6bfb]662 pci_fun_ops.interfaces[PCI_DEV_IFACE] = &pci_dev_ops;
[3843ecb]663}
664
[97a62fe]665pci_fun_t *pci_fun_new(pci_bus_t *bus)
[713a4b9]666{
[97a62fe]667 pci_fun_t *fun;
[713a4b9]668
[97a62fe]669 fun = (pci_fun_t *) calloc(1, sizeof(pci_fun_t));
670 if (fun == NULL)
671 return NULL;
672
673 fun->busptr = bus;
674 return fun;
[713a4b9]675}
676
[68414f4a]677void pci_fun_init(pci_fun_t *fun, int bus, int dev, int fn)
[713a4b9]678{
[68414f4a]679 fun->bus = bus;
680 fun->dev = dev;
681 fun->fn = fn;
[713a4b9]682}
683
[68414f4a]684void pci_fun_delete(pci_fun_t *fun)
[713a4b9]685{
[bab6388]686 assert(fun != NULL);
687 hw_res_clean_resource_list(&fun->hw_resources);
688 free(fun);
[713a4b9]689}
690
[97a62fe]691char *pci_fun_create_name(pci_fun_t *fun)
[713a4b9]692{
693 char *name = NULL;
694
[68414f4a]695 asprintf(&name, "%02x:%02x.%01x", fun->bus, fun->dev,
696 fun->fn);
[97a62fe]697 return name;
[713a4b9]698}
699
[68414f4a]700bool pci_alloc_resource_list(pci_fun_t *fun)
[713a4b9]701{
[68414f4a]702 fun->hw_resources.resources =
[713a4b9]703 (hw_resource_t *) malloc(PCI_MAX_HW_RES * sizeof(hw_resource_t));
[68414f4a]704 return fun->hw_resources.resources != NULL;
[713a4b9]705}
706
[68414f4a]707void pci_clean_resource_list(pci_fun_t *fun)
[713a4b9]708{
[68414f4a]709 if (fun->hw_resources.resources != NULL) {
710 free(fun->hw_resources.resources);
711 fun->hw_resources.resources = NULL;
[713a4b9]712 }
713}
714
[68414f4a]715/** Read the base address registers (BARs) of the function and add the addresses
716 * to its HW resource list.
[713a4b9]717 *
[68414f4a]718 * @param fun PCI function
[713a4b9]719 */
[68414f4a]720void pci_read_bars(pci_fun_t *fun)
[713a4b9]721{
722 /*
723 * Position of the BAR in the PCI configuration address space of the
724 * device.
725 */
726 int addr = PCI_BASE_ADDR_0;
727
728 while (addr <= PCI_BASE_ADDR_5)
[8b1e15ac]729 addr = pci_read_bar(fun, addr);
[713a4b9]730}
731
732size_t pci_bar_mask_to_size(uint32_t mask)
733{
[ad6857c]734 size_t size = mask & ~(mask - 1);
735 return size;
[713a4b9]736}
737
[8c06905]738int main(int argc, char *argv[])
739{
[3843ecb]740 printf(NAME ": HelenOS pci bus driver (intel method 1).\n");
741 pciintel_init();
[83a2f43]742 return ddf_driver_main(&pci_driver);
[8c06905]743}
744
745/**
746 * @}
[472020fc]747 */
Note: See TracBrowser for help on using the repository browser.