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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-07-07 GuEe-GUI first version
*/
#include <rtdef.h>
#include <drivers/byteorder.h>
#define DBG_TAG "pci.procfs"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#include "procfs.h"
#ifdef RT_USING_LWP
#include <lwp_user_mm.h>
#endif
static struct rt_bus *pci_bus;
static struct proc_dentry *pci_proc_dentry;
rt_inline void copy_to_user(void *to, void *from, size_t size)
{
#ifdef RT_USING_LWP
if (!lwp_put_to_user(to, from, size))
#endif
{
rt_memcpy(to, from, size);
}
}
rt_inline void copy_from_user(void *to, const void *from, size_t size)
{
#ifdef RT_USING_LWP
if (!lwp_get_from_user(to, (void *)from, size))
#endif
{
rt_memcpy(to, (void *)from, size);
}
}
static void pci_pm_runtime_get(struct rt_pci_device *pdev, rt_ubase_t *out_flags)
{
*out_flags = pdev->pm_enabled;
if (!pdev->pm_enabled)
{
rt_pci_pme_active(pdev, RT_TRUE);
}
}
static void pci_pm_runtime_put(struct rt_pci_device *pdev, rt_ubase_t *flags)
{
if (!*flags)
{
rt_pci_pme_active(pdev, RT_FALSE);
}
}
static ssize_t pci_read(struct dfs_file *file, void *buf, size_t count, off_t *ppos)
{
off_t pos = *ppos;
size_t res = count;
rt_ubase_t pm_flags;
struct proc_dentry *dentry = file->vnode->data;
struct rt_pci_device *pdev = dentry->data;
pci_pm_runtime_get(pdev, &pm_flags);
if ((pos & 1) && count)
{
rt_uint8_t val;
rt_pci_read_config_u8(pdev, pos, &val);
copy_to_user(buf, &val, sizeof(val));
++buf;
++pos;
--count;
}
if ((pos & 3) && count > 2)
{
rt_uint16_t val;
rt_pci_read_config_u16(pdev, pos, &val);
val = rt_cpu_to_le16(val);
copy_to_user(buf, &val, sizeof(val));
buf += 2;
pos += 2;
count -= 2;
}
while (count >= 4)
{
rt_uint32_t val;
rt_pci_read_config_u32(pdev, pos, &val);
val = rt_cpu_to_le32(val);
copy_to_user(buf, &val, sizeof(val));
buf += 4;
pos += 4;
count -= 4;
}
if (count >= 2)
{
rt_uint16_t val;
rt_pci_read_config_u16(pdev, pos, &val);
val = rt_cpu_to_le16(val);
copy_to_user(buf, &val, sizeof(val));
buf += 2;
pos += 2;
count -= 2;
}
if (count)
{
rt_uint8_t val;
rt_pci_read_config_u8(pdev, pos, &val);
copy_to_user(buf, &val, sizeof(val));
++pos;
}
pci_pm_runtime_put(pdev, &pm_flags);
*ppos = pos;
return res;
}
static ssize_t pci_write(struct dfs_file *file, const void *buf, size_t count, off_t *ppos)
{
off_t pos = *ppos;
size_t res = count;
rt_ubase_t pm_flags;
struct proc_dentry *dentry = file->vnode->data;
struct rt_pci_device *pdev = dentry->data;
pci_pm_runtime_get(pdev, &pm_flags);
if ((pos & 1) && count)
{
rt_uint8_t val;
copy_from_user(&val, buf, sizeof(val));
rt_pci_write_config_u8(pdev, pos, val);
++buf;
++pos;
--count;
}
if ((pos & 3) && count > 2)
{
rt_le16_t val;
copy_from_user(&val, buf, sizeof(val));
rt_pci_write_config_u16(pdev, pos, rt_le16_to_cpu(val));
buf += 2;
pos += 2;
count -= 2;
}
while (count >= 4)
{
rt_le32_t val;
copy_from_user(&val, buf, sizeof(val));
rt_pci_write_config_u32(pdev, pos, rt_le32_to_cpu(val));
buf += 4;
pos += 4;
count -= 4;
}
if (count >= 2)
{
rt_le16_t val;
copy_from_user(&val, buf, sizeof(val));
rt_pci_write_config_u16(pdev, pos, rt_le16_to_cpu(val));
buf += 2;
pos += 2;
count -= 2;
}
if (count)
{
rt_uint8_t val;
copy_from_user(&val, buf, sizeof(val));
rt_pci_write_config_u8(pdev, pos, val);
++pos;
}
pci_pm_runtime_put(pdev, &pm_flags);
*ppos = pos;
return res;
}
static off_t pci_lseek(struct dfs_file *file, off_t offset, int wherece)
{
struct proc_dentry *dentry = file->vnode->data;
struct rt_pci_device *pdev = dentry->data;
switch (wherece)
{
case SEEK_SET:
break;
case SEEK_CUR:
offset += file->fpos;
break;
case SEEK_END:
offset += pdev->cfg_size;
break;
default:
return -EINVAL;
}
if (offset <= (off_t)pdev->cfg_size)
{
return offset;
}
return -EIO;
}
static const struct dfs_file_ops pci_fops =
{
.read = pci_read,
.write = pci_write,
.lseek = pci_lseek,
};
void pci_procfs_attach(struct rt_pci_device *pdev)
{
const char *name;
struct proc_dentry *dentry;
if (!pci_proc_dentry)
{
return;
}
name = rt_dm_dev_get_name(&pdev->parent);
dentry = proc_create_data(name, 0644, pci_proc_dentry, &pci_fops, pdev);
if (!dentry)
{
LOG_E("Create %s file fail", name);
return;
}
proc_release(dentry);
}
void pci_procfs_detach(struct rt_pci_device *pdev)
{
if (!pci_proc_dentry)
{
return;
}
proc_remove_dentry(rt_dm_dev_get_name(&pdev->parent), pci_proc_dentry);
}
static int pci_single_show(struct dfs_seq_file *seq, void *data)
{
struct rt_device *dev;
struct rt_pci_driver *pdrv;
struct rt_pci_device *pdev;
rt_hw_spin_lock(&pci_bus->dev_lock.lock);
rt_list_for_each_entry(dev, &pci_bus->dev_list, node)
{
pdev = rt_container_of(dev, struct rt_pci_device, parent);
dfs_seq_printf(seq, "%02x%02x\t%04x%04x\t%x",
pdev->bus->number,
pdev->devfn,
pdev->vendor,
pdev->device,
pdev->irq);
/* BAR, ROM base */
for (int bar = 0; bar < RT_PCI_BAR_NR_MAX; ++bar)
{
dfs_seq_printf(seq, "\t%16llx", (rt_uint64_t)pdev->resource[bar].base);
}
dfs_seq_printf(seq, "\t%16llx", (rt_uint64_t)pdev->rom.base);
/* BAR, ROM size */
for (int bar = 0; bar < RT_PCI_BAR_NR_MAX; ++bar)
{
dfs_seq_printf(seq, "\t%16llx", (rt_uint64_t)pdev->resource[bar].size);
}
dfs_seq_printf(seq, "\t%16llx", (rt_uint64_t)pdev->rom.size);
dfs_seq_puts(seq, "\t");
/* Driver Name */
if (dev->drv)
{
pdrv = rt_container_of(dev->drv, struct rt_pci_driver, parent);
dfs_seq_puts(seq, pdrv->name);
}
/* End of a seq */
dfs_seq_puts(seq, "\n");
}
rt_hw_spin_unlock(&pci_bus->dev_lock.lock);
return 0;
}
static int pci_procfs_init(void)
{
struct proc_dentry *dentry;
pci_bus = rt_bus_find_by_name("pci");
RT_ASSERT(pci_bus != RT_NULL);
pci_proc_dentry = proc_mkdir("pci", RT_NULL);
if (!pci_proc_dentry)
{
LOG_E("Create pci entry fail");
return (int)-RT_ERROR;
}
proc_release(pci_proc_dentry);
dentry = proc_create_single_data("devices", 0644, pci_proc_dentry, &pci_single_show, NULL);
if (!dentry)
{
proc_remove(pci_proc_dentry);
pci_proc_dentry = RT_NULL;
LOG_E("Create pci devices fail");
return (int)-RT_ERROR;
}
proc_release(dentry);
return 0;
}
INIT_PREV_EXPORT(pci_procfs_init);
|