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
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-10-27 GuEe-GUI first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#define DBG_TAG "rtdm.ivshmem"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#include <cpuport.h>
#include "ivshmem.h"
static void ivshmem_isr(int irqno, void *param)
{
rt_uint32_t status;
struct ivshmem_device *ivdev = param;
status = HWREG32(ivdev->reg + IVSHMEM_INTR_STATUS);
if ((status && status != 0xffffffff) || ivdev->nvectors)
{
if (ivdev->handle_irq)
{
ivdev->handle_irq(ivdev, ivdev->nvectors ? irqno : status);
}
else
{
LOG_E("IVSHMEM<%p> have a irq = %d, by no have irq handers", ivdev, irqno);
}
}
}
rt_err_t ivshmem_install_msix_vectors(struct ivshmem_device *ivdev, int nvectors, const char *name)
{
rt_err_t err = RT_EOK;
char irq_name[RT_NAME_MAX];
if (!ivdev || !name)
{
return -RT_EINVAL;
}
if (ivdev->nvectors != 0)
{
LOG_E("IVSHMEM<%p> '%s' have already install msix", ivdev, name);
return -RT_ERROR;
}
if (!ivdev->handle_irq)
{
LOG_E("IVSHMEM<%p> '%s' no have irq handers", ivdev, name);
return -RT_EINVAL;
}
ivdev->msix_entries = rt_malloc(nvectors * sizeof(*ivdev->msix_entries));
if (!ivdev->msix_entries)
{
return -RT_ENOMEM;
}
ivdev->nvectors = nvectors;
rt_pci_msix_entry_index_linear(ivdev->msix_entries, nvectors);
if ((err = rt_pci_msix_enable(ivdev->pdev, ivdev->msix_entries, ivdev->nvectors)))
{
rt_free(ivdev->msix_entries);
ivdev->msix_entries = RT_NULL;
ivdev->nvectors = 0;
return err;
}
for (int i = 0; i < nvectors; i++)
{
int irq = ivdev->msix_entries[i].irq;
rt_snprintf(irq_name, RT_NAME_MAX, "%s%d", name, irq);
rt_hw_interrupt_install(irq, ivshmem_isr, ivdev, irq_name);
rt_hw_interrupt_umask(irq);
}
return err;
}
rt_err_t ivshmem_install_intx_vector(struct ivshmem_device *ivdev, const char *name)
{
rt_err_t err = RT_EOK;
if (!ivdev || !name)
{
return -RT_EINVAL;
}
if (ivdev->nvectors != 0)
{
LOG_E("IVSHMEM<%p> '%s' have already install msix", ivdev, name);
return -RT_ERROR;
}
if (!ivdev->handle_irq)
{
LOG_E("IVSHMEM<%p> '%s' no have irq handers", ivdev, name);
return -RT_EINVAL;
}
ivdev->nvectors = 0;
ivdev->msix_entries = RT_NULL;
rt_hw_interrupt_install(ivdev->irq, ivshmem_isr, ivdev, name);
rt_pci_irq_unmask(ivdev->pdev);
return err;
}
static void ivshmem_free_resource(struct ivshmem_device *ivdev)
{
if (ivdev->pdev)
{
ivdev->pdev->sysdata = RT_NULL;
}
if (ivdev->reg)
{
rt_iounmap(ivdev->reg);
}
if (ivdev->shmem)
{
rt_iounmap(ivdev->shmem);
}
}
rt_err_t ivshmem_pci_probe(struct rt_pci_device *pdev, struct ivshmem_device *ivdev)
{
rt_err_t err = RT_EOK;
if (!ivdev || !pdev || pdev->irq < 0)
{
return -RT_EINVAL;
}
rt_memset(ivdev, 0, sizeof(*ivdev));
ivdev->pdev = pdev;
ivdev->irq = pdev->irq;
pdev->sysdata = ivdev;
ivdev->reg = rt_pci_iomap(pdev, IVSHMEM_REG_BAR);
if (!ivdev->reg)
{
err = -RT_EIO;
goto _fail;
}
/* Set all masks to on */
HWREG32(ivdev->reg + IVSHMEM_INTR_MASK) = 0xffffffff;
ivdev->shmem_size = pdev->resource[IVSHMEM_SHARE_MEM_BAR].size;
ivdev->shmem = (void *)pdev->resource[IVSHMEM_SHARE_MEM_BAR].base;
if (!ivdev->shmem_size)
{
rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_ADDR_LO, (rt_uint32_t *)&ivdev->shmem);
rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_ADDR_HI, (rt_uint32_t *)&ivdev->shmem + 1);
rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_SIZE_LO, (rt_uint32_t *)&ivdev->shmem_size);
rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_SIZE_HI, (rt_uint32_t *)&ivdev->shmem_size + 1);
}
ivdev->shmem = rt_ioremap(ivdev->shmem, ivdev->shmem_size);
if (!ivdev->shmem)
{
err = -RT_ERROR;
goto _fail;
}
return RT_EOK;
_fail:
ivshmem_free_resource(ivdev);
return err;
}
struct ivshmem_device *ivshmem_pci_remove(struct rt_pci_device *pdev)
{
struct ivshmem_device *ivdev = RT_NULL;
if (!pdev || !pdev->sysdata)
{
return ivdev;
}
ivdev = pdev->sysdata;
if (ivdev->msix_entries)
{
for (int i = 0; i < ivdev->nvectors; ++i)
{
int irq = ivdev->msix_entries[i].irq;
rt_hw_interrupt_umask(irq);
rt_pic_detach_irq(irq, ivdev);
}
}
rt_hw_interrupt_umask(ivdev->irq);
rt_pic_detach_irq(ivdev->irq, ivdev);
if (ivdev->nvectors)
{
rt_pci_msix_disable(ivdev->pdev);
rt_free(ivdev->msix_entries);
}
else
{
rt_pci_irq_unmask(ivdev->pdev);
}
ivshmem_free_resource(ivdev);
return ivdev;
}
|