-
Notifications
You must be signed in to change notification settings - Fork 0
/
bm_pci.c
261 lines (212 loc) · 6.29 KB
/
bm_pci.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* -LICENSE-START-
** Copyright (c) 2013 Blackmagic Design
**
** Permission is hereby granted, free of charge, to any person or organization
** obtaining a copy of the software and accompanying documentation covered by
** this license (the "Software") to use, reproduce, display, distribute,
** execute, and transmit the Software, and to prepare derivative works of the
** Software, and to permit third-parties to whom the Software is furnished to
** do so, all subject to the following:
**
** The copyright notices in the Software and this entire statement, including
** the above license grant, this restriction and the following disclaimer,
** must be included in all copies of the Software, in whole or in part, and
** all derivative works of the Software, unless such copies or derivative
** works are solely in the form of machine-executable object code generated by
** a source language processor.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
** SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
** FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
** -LICENSE-END-
*/
#include <linux/pci.h>
#include <linux/version.h>
#include <linux/interrupt.h>
#include <linux/moduleparam.h>
#include "bm_pci.h"
#include "bm_util.h"
char* default_irq_type = "msi";
module_param_named(irq, default_irq_type, charp, 0444);
MODULE_PARM_DESC(irq, "The default irq type: msi, legacy");
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
#define BM_DMA_64BIT_MASK DMA_BIT_MASK(64)
#define BM_DMA_32BIT_MASK DMA_BIT_MASK(32)
#else
#define BM_DMA_64BIT_MASK DMA_64BIT_MASK
#define BM_DMA_32BIT_MASK DMA_32BIT_MASK
#endif
bm_pci_device_t* bm_pci_alloc(struct pci_dev* pdev)
{
bm_pci_device_t* pci = kzalloc(sizeof(bm_pci_device_t), GFP_KERNEL);
if (!pci)
return NULL;
pci->pdev = pci_dev_get(pdev);
kref_init(&pci->ref);
return pci;
}
bm_pci_device_t* bm_pci_get(bm_pci_device_t* pci)
{
kref_get(&pci->ref);
return pci;
}
static void destroy_pci_device(struct kref* ref)
{
bm_pci_device_t* pci = container_of(ref, bm_pci_device_t, ref);
pci_dev_put(pci->pdev);
kfree(pci);
}
void bm_pci_put(bm_pci_device_t* pci)
{
kref_put(&pci->ref, destroy_pci_device);
}
bool bm_pci_start(bm_pci_device_t* pci)
{
if (pci_enable_device(pci->pdev) < 0)
return false;
pci_set_master(pci->pdev);
if (pci_set_dma_mask(pci->pdev, BM_DMA_64BIT_MASK) < 0)
{
if (pci_set_dma_mask(pci->pdev, BM_DMA_32BIT_MASK) < 0)
goto bail;
}
if (strcmp(default_irq_type, "msi") == 0)
pci_enable_msi(pci->pdev);
return true;
bail:
pci_disable_device(pci->pdev);
return false;
}
void bm_pci_stop(bm_pci_device_t* pci)
{
if (pci->pdev->msi_enabled)
pci_disable_msi(pci->pdev);
pci_disable_device(pci->pdev);
}
void pci_clear_master(struct pci_dev *dev)
{
uint16_t old_cfg;
uint16_t cfg;
pci_read_config_word(dev, PCI_COMMAND, &old_cfg);
cfg = old_cfg & ~PCI_COMMAND_MASTER;
if (cfg != old_cfg)
pci_write_config_word(dev, PCI_COMMAND, cfg);
dev->is_busmaster = false;
}
bool bm_pci_set_bus_master_enable(bm_pci_device_t* pci, bool enable)
{
bool ret = pci->pdev->is_busmaster;
if (enable)
pci_set_master(pci->pdev);
else
pci_clear_master(pci->pdev);
return ret;
}
bool bm_pci_set_memory_enable(bm_pci_device_t* pci, bool enable)
{
uint16_t cfg;
uint16_t old_cfg;
pci_read_config_word(pci->pdev, PCI_COMMAND, &old_cfg);
if (enable)
cfg = old_cfg | PCI_COMMAND_MEMORY;
else
cfg = old_cfg & ~PCI_COMMAND_MEMORY;
if (cfg != old_cfg)
pci_write_config_word(pci->pdev, PCI_COMMAND, cfg);
return old_cfg & PCI_COMMAND_MEMORY;
}
uint8_t bm_pci_bus_number(bm_pci_device_t* pci)
{
return pci->pdev->bus->number;
}
uint8_t bm_pci_device_number(bm_pci_device_t* pci)
{
return PCI_SLOT(pci->pdev->devfn);
}
uint8_t bm_pci_function_number(bm_pci_device_t* pci)
{
return PCI_FUNC(pci->pdev->devfn);
}
uint8_t bm_pci_read_config_u8(bm_pci_device_t* pci, int offset)
{
uint8_t val;
pci_read_config_byte(pci->pdev, offset, &val);
return val;
}
uint16_t bm_pci_read_config_u16(bm_pci_device_t* pci, int offset)
{
uint16_t val;
pci_read_config_word(pci->pdev, offset, &val);
return val;
}
uint32_t bm_pci_read_config_u32(bm_pci_device_t* pci, int offset)
{
uint32_t val;
pci_read_config_dword(pci->pdev, offset, &val);
return val;
}
void bm_pci_write_config_u8(bm_pci_device_t* pci, int offset, uint8_t val)
{
pci_write_config_byte(pci->pdev, offset, val);
}
void bm_pci_write_config_u16(bm_pci_device_t* pci, int offset, uint16_t val)
{
pci_write_config_word(pci->pdev, offset, val);
}
void bm_pci_write_config_u32(bm_pci_device_t* pci, int offset, uint32_t val)
{
pci_write_config_dword(pci->pdev, offset, val);
}
bool bm_pci_using_msi(bm_pci_device_t* pci)
{
return pci->pdev->msi_enabled;
}
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18)
#define bm_interrupt_handler(irq, data, regs) bm_interrupt_handler(irq, data)
#endif
static irqreturn_t bm_interrupt_handler(int irq, void* data, struct pt_regs* regs)
{
bm_pci_device_t* pci = (bm_pci_device_t*)data;
return pci->irq_handler(irq, pci->irq_data);
}
bool bm_pci_register_interrupt(bm_pci_device_t* pci, const char* name, bm_interrupt_handler_t handler, void* data)
{
unsigned long flags = 0;
if (!pci->pdev->msi_enabled)
flags |= IRQF_SHARED;
if (pci->irq_data)
return false;
pci->irq_name[BM_PCI_IRQ_NAME_SIZE - 1] = '\0';
strncpy(pci->irq_name, name, BM_PCI_IRQ_NAME_SIZE - 1);
pci->irq_data = data;
pci->irq_handler = handler;
if (request_irq(pci->pdev->irq, bm_interrupt_handler, flags, pci->irq_name, pci) < 0)
return false;
return true;
}
void bm_pci_unregister_interrupt(bm_pci_device_t* pci)
{
unsigned int irq = pci->pdev->irq;
if (!pci->irq_data)
return;
free_irq(irq, pci);
pci->irq_data = NULL;
}
bool bm_pci_map_bar(bm_pci_device_t* pci, int bar, void** addr, vm_size_t* size)
{
*addr = pcim_iomap(pci->pdev, bar, 0 /* not set */);
if (!*addr)
{
void __iomem **tbl = (void __iomem **)pcim_iomap_table(pci->pdev);
if (tbl)
*addr = tbl[bar];
}
if (!*addr)
return false;
*size = pci_resource_len(pci->pdev, bar);
return true;
}