source: mainline/uspace/drv/pciintel/pci.h@ 663f41c4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 663f41c4 was 663f41c4, checked in by Jakub Jermar <jakub@…>, 15 years ago

Cstyle fixes in pciintel.

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2 * Copyright (c) 2010 Lenka Trochtova
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup pciintel
30 * @{
31 */
32/** @file
33 */
34
35#ifndef PCI_H_
36#define PCI_H_
37
38#include <stdlib.h>
39#include <driver.h>
40#include <malloc.h>
41
42#include "pci_regs.h"
43
44#define PCI_MAX_HW_RES 8
45
46typedef struct pci_dev_data {
47 int bus;
48 int dev;
49 int fn;
50 int vendor_id;
51 int device_id;
52 hw_resource_list_t hw_resources;
53} pci_dev_data_t;
54
55extern void create_pci_match_ids(device_t *);
56
57extern uint8_t pci_conf_read_8(device_t *, int);
58extern uint16_t pci_conf_read_16(device_t *, int);
59extern uint32_t pci_conf_read_32(device_t *, int);
60extern void pci_conf_write_8(device_t *, int, uint8_t);
61extern void pci_conf_write_16(device_t *, int, uint16_t);
62extern void pci_conf_write_32(device_t *, int, uint32_t);
63
64extern void pci_add_range(device_t *, uint64_t, size_t, bool);
65extern int pci_read_bar(device_t *, int);
66extern void pci_read_interrupt(device_t *);
67extern void pci_add_interrupt(device_t *, int);
68
69extern void pci_bus_scan(device_t *, int);
70
71static inline pci_dev_data_t *create_pci_dev_data(void)
72{
73 pci_dev_data_t *res = (pci_dev_data_t *) malloc(sizeof(pci_dev_data_t));
74
75 if (NULL != res)
76 memset(res, 0, sizeof(pci_dev_data_t));
77 return res;
78}
79
80static inline void
81init_pci_dev_data(pci_dev_data_t *d, int bus, int dev, int fn)
82{
83 d->bus = bus;
84 d->dev = dev;
85 d->fn = fn;
86}
87
88static inline void delete_pci_dev_data(pci_dev_data_t *d)
89{
90 if (NULL != d) {
91 clean_hw_resource_list(&d->hw_resources);
92 free(d);
93 }
94}
95
96static inline void create_pci_dev_name(device_t *dev)
97{
98 pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
99 char *name = NULL;
100
101 asprintf(&name, "%02x:%02x.%01x", dev_data->bus, dev_data->dev,
102 dev_data->fn);
103 dev->name = name;
104}
105
106static inline bool pci_alloc_resource_list(device_t *dev)
107{
108 pci_dev_data_t *dev_data = (pci_dev_data_t *)dev->driver_data;
109
110 dev_data->hw_resources.resources =
111 (hw_resource_t *) malloc(PCI_MAX_HW_RES * sizeof(hw_resource_t));
112 return dev_data->hw_resources.resources != NULL;
113}
114
115static inline void pci_clean_resource_list(device_t *dev)
116{
117 pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
118
119 if (NULL != dev_data->hw_resources.resources) {
120 free(dev_data->hw_resources.resources);
121 dev_data->hw_resources.resources = NULL;
122 }
123}
124
125/** Read the base address registers (BARs) of the device and adds the addresses
126 * to its hw resource list.
127 *
128 * @param dev the pci device.
129 */
130static inline void pci_read_bars(device_t *dev)
131{
132 /*
133 * Position of the BAR in the PCI configuration address space of the
134 * device.
135 */
136 int addr = PCI_BASE_ADDR_0;
137
138 while (addr <= PCI_BASE_ADDR_5)
139 addr = pci_read_bar(dev, addr);
140}
141
142static inline size_t pci_bar_mask_to_size(uint32_t mask)
143{
144 return ((mask & 0xfffffff0) ^ 0xffffffff) + 1;
145}
146
147#endif
148
149/**
150 * @}
151 */
Note: See TracBrowser for help on using the repository browser.