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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
/*
* The PCI Library -- Direct Configuration access via memory mapped ports
*
* Copyright (c) 2022 Pali Rohár <pali@kernel.org>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
/*
* Tell 32-bit platforms that we are interested in 64-bit variant of off_t type
* as 32-bit variant of off_t type is signed and so it cannot represent all
* possible 32-bit offsets. It is required because off_t type is used by mmap().
*/
#define _FILE_OFFSET_BITS 64
#include "internal.h"
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#ifndef OFF_MAX
#define OFF_MAX (off_t)((1ULL << (sizeof(off_t) * CHAR_BIT - 1)) - 1)
#endif
struct mmio_cache
{
off_t addr_page;
off_t data_page;
void *addr_map;
void *data_map;
};
static long pagesize;
static void
munmap_regs(struct pci_access *a)
{
struct mmio_cache *cache = a->aux;
if (!cache)
return;
munmap(cache->addr_map, pagesize);
if (cache->addr_page != cache->data_page)
munmap(cache->data_map, pagesize);
pci_mfree(a->aux);
a->aux = NULL;
}
static int
mmap_regs(struct pci_access *a, off_t addr_reg, off_t data_reg, int data_off, volatile void **addr, volatile void **data)
{
struct mmio_cache *cache = a->aux;
off_t addr_page = addr_reg & ~(pagesize-1);
off_t data_page = data_reg & ~(pagesize-1);
void *addr_map = MAP_FAILED;
void *data_map = MAP_FAILED;
if (cache && cache->addr_page == addr_page)
addr_map = cache->addr_map;
if (cache && cache->data_page == data_page)
data_map = cache->data_map;
if (addr_map == MAP_FAILED)
addr_map = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, a->fd, addr_page);
if (addr_map == MAP_FAILED)
return 0;
if (data_map == MAP_FAILED)
{
if (data_page == addr_page)
data_map = addr_map;
else
data_map = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, a->fd, data_page);
}
if (data_map == MAP_FAILED)
{
if (!cache || cache->addr_map != addr_map)
munmap(addr_map, pagesize);
return 0;
}
if (cache && cache->addr_page != addr_page)
munmap(cache->addr_map, pagesize);
if (cache && cache->data_page != data_page && cache->data_page != cache->addr_page)
munmap(cache->data_map, pagesize);
if (!cache)
cache = a->aux = pci_malloc(a, sizeof(*cache));
cache->addr_page = addr_page;
cache->data_page = data_page;
cache->addr_map = addr_map;
cache->data_map = data_map;
*addr = (unsigned char *)addr_map + (addr_reg & (pagesize-1));
*data = (unsigned char *)data_map + (data_reg & (pagesize-1)) + data_off;
return 1;
}
static void
writeb(unsigned char value, volatile void *addr)
{
*(volatile unsigned char *)addr = value;
}
static void
writew(unsigned short value, volatile void *addr)
{
*(volatile unsigned short *)addr = value;
}
static void
writel(unsigned long value, volatile void *addr)
{
*(volatile unsigned long *)addr = value;
}
static unsigned char
readb(volatile void *addr)
{
return *(volatile unsigned char *)addr;
}
static unsigned short
readw(volatile void *addr)
{
return *(volatile unsigned short *)addr;
}
static unsigned long
readl(volatile void *addr)
{
return *(volatile unsigned long *)addr;
}
static int
validate_addrs(const char *addrs)
{
const char *sep, *next;
unsigned long long num;
char *endptr;
if (!*addrs)
return 0;
while (1)
{
next = strchr(addrs, ',');
if (!next)
next = addrs + strlen(addrs);
sep = strchr(addrs, '/');
if (!sep)
return 0;
if (!isxdigit(*addrs) || !isxdigit(*(sep+1)))
return 0;
errno = 0;
num = strtoull(addrs, &endptr, 16);
if (errno || endptr != sep || (num & 3) || num > OFF_MAX)
return 0;
errno = 0;
num = strtoull(sep+1, &endptr, 16);
if (errno || endptr != next || (num & 3) || num > OFF_MAX)
return 0;
if (!*next)
return 1;
addrs = next + 1;
}
}
static int
get_domain_count(const char *addrs)
{
int count = 1;
while (addrs = strchr(addrs, ','))
{
addrs++;
count++;
}
return count;
}
static int
get_domain_addr(const char *addrs, int domain, off_t *addr_reg, off_t *data_reg)
{
char *endptr;
while (domain-- > 0)
{
addrs = strchr(addrs, ',');
if (!addrs)
return 0;
addrs++;
}
*addr_reg = strtoull(addrs, &endptr, 16);
*data_reg = strtoull(endptr+1, NULL, 16);
return 1;
}
static void
conf1_config(struct pci_access *a)
{
pci_define_param(a, "devmem.path", PCI_PATH_DEVMEM_DEVICE, "Path to the /dev/mem device");
pci_define_param(a, "mmio-conf1.addrs", "", "Physical addresses of memory mapped Intel conf1 interface"); /* format: 0xaddr1/0xdata1,0xaddr2/0xdata2,... */
}
static void
conf1_ext_config(struct pci_access *a)
{
pci_define_param(a, "devmem.path", PCI_PATH_DEVMEM_DEVICE, "Path to the /dev/mem device");
pci_define_param(a, "mmio-conf1-ext.addrs", "", "Physical addresses of memory mapped Intel conf1 extended interface"); /* format: 0xaddr1/0xdata1,0xaddr2/0xdata2,... */
}
static int
detect(struct pci_access *a, char *addrs_param_name)
{
char *addrs = pci_get_param(a, addrs_param_name);
char *devmem = pci_get_param(a, "devmem.path");
if (!*addrs)
{
a->debug("%s was not specified", addrs_param_name);
return 0;
}
if (!validate_addrs(addrs))
{
a->debug("%s has invalid address format %s", addrs_param_name, addrs);
return 0;
}
if (access(devmem, R_OK | W_OK))
{
a->debug("cannot access %s: %s", devmem, strerror(errno));
return 0;
}
a->debug("using %s with %s", devmem, addrs);
return 1;
}
static int
conf1_detect(struct pci_access *a)
{
return detect(a, "mmio-conf1.addrs");
}
static int
conf1_ext_detect(struct pci_access *a)
{
return detect(a, "mmio-conf1-ext.addrs");
}
static char*
get_addrs_param_name(struct pci_access *a)
{
if (a->methods->config == conf1_ext_config)
return "mmio-conf1-ext.addrs";
else
return "mmio-conf1.addrs";
}
static void
conf1_init(struct pci_access *a)
{
char *addrs_param_name = get_addrs_param_name(a);
char *addrs = pci_get_param(a, addrs_param_name);
char *devmem = pci_get_param(a, "devmem.path");
pagesize = sysconf(_SC_PAGESIZE);
if (pagesize < 0)
a->error("Cannot get page size: %s", strerror(errno));
if (!*addrs)
a->error("Option %s was not specified.", addrs_param_name);
if (!validate_addrs(addrs))
a->error("Option %s has invalid address format \"%s\".", addrs_param_name, addrs);
a->fd = open(devmem, O_RDWR | O_DSYNC); /* O_DSYNC bypass CPU cache for mmap() on Linux */
if (a->fd < 0)
a->error("Cannot open %s: %s.", devmem, strerror(errno));
}
static void
conf1_cleanup(struct pci_access *a)
{
if (a->fd < 0)
return;
munmap_regs(a);
close(a->fd);
a->fd = -1;
}
static void
conf1_scan(struct pci_access *a)
{
char *addrs_param_name = get_addrs_param_name(a);
char *addrs = pci_get_param(a, addrs_param_name);
int domain_count = get_domain_count(addrs);
int domain;
for (domain = 0; domain < domain_count; domain++)
pci_generic_scan_domain(a, domain);
}
static int
conf1_ext_read(struct pci_dev *d, int pos, byte *buf, int len)
{
char *addrs_param_name = get_addrs_param_name(d->access);
char *addrs = pci_get_param(d->access, addrs_param_name);
volatile void *addr, *data;
off_t addr_reg, data_reg;
if (pos >= 4096)
return 0;
if (len != 1 && len != 2 && len != 4)
return pci_generic_block_read(d, pos, buf, len);
if (!get_domain_addr(addrs, d->domain, &addr_reg, &data_reg))
return 0;
if (!mmap_regs(d->access, addr_reg, data_reg, pos&3, &addr, &data))
return 0;
writel(0x80000000 | ((pos & 0xf00) << 16) | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos & 0xfc), addr);
readl(addr); /* write barrier for address */
switch (len)
{
case 1:
buf[0] = readb(data);
break;
case 2:
((u16 *) buf)[0] = readw(data);
break;
case 4:
((u32 *) buf)[0] = readl(data);
break;
}
return 1;
}
static int
conf1_read(struct pci_dev *d, int pos, byte *buf, int len)
{
if (pos >= 256)
return 0;
return conf1_ext_read(d, pos, buf, len);
}
static int
conf1_ext_write(struct pci_dev *d, int pos, byte *buf, int len)
{
char *addrs_param_name = get_addrs_param_name(d->access);
char *addrs = pci_get_param(d->access, addrs_param_name);
volatile void *addr, *data;
off_t addr_reg, data_reg;
if (pos >= 4096)
return 0;
if (len != 1 && len != 2 && len != 4)
return pci_generic_block_write(d, pos, buf, len);
if (!get_domain_addr(addrs, d->domain, &addr_reg, &data_reg))
return 0;
if (!mmap_regs(d->access, addr_reg, data_reg, pos&3, &addr, &data))
return 0;
writel(0x80000000 | ((pos & 0xf00) << 16) | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos & 0xfc), addr);
readl(addr); /* write barrier for address */
switch (len)
{
case 1:
writeb(buf[0], data);
break;
case 2:
writew(((u16 *) buf)[0], data);
break;
case 4:
writel(((u32 *) buf)[0], data);
break;
}
/*
* write barrier for data
* Note that we cannot read from data port because it may have side effect.
* Instead we read from address port (which should not have side effect) to
* create a barrier between two conf1_write() calls. But this does not have
* to be 100% correct as it does not ensure barrier on data port itself.
* Correct way is to issue CPU instruction for full hw sync barrier but gcc
* does not provide any (builtin) function yet.
*/
readl(addr);
return 1;
}
static int
conf1_write(struct pci_dev *d, int pos, byte *buf, int len)
{
if (pos >= 256)
return 0;
return conf1_ext_write(d, pos, buf, len);
}
struct pci_methods pm_mmio_conf1 = {
"mmio-conf1",
"Raw memory mapped I/O port access using Intel conf1 interface",
conf1_config,
conf1_detect,
conf1_init,
conf1_cleanup,
conf1_scan,
pci_generic_fill_info,
conf1_read,
conf1_write,
NULL, /* read_vpd */
NULL, /* init_dev */
NULL /* cleanup_dev */
};
struct pci_methods pm_mmio_conf1_ext = {
"mmio-conf1-ext",
"Raw memory mapped I/O port access using Intel conf1 extended interface",
conf1_ext_config,
conf1_ext_detect,
conf1_init,
conf1_cleanup,
conf1_scan,
pci_generic_fill_info,
conf1_ext_read,
conf1_ext_write,
NULL, /* read_vpd */
NULL, /* init_dev */
NULL /* cleanup_dev */
};
|