2 * The PCI Library -- Direct Configuration access via memory mapped ports
4 * Copyright (c) 2022 Pali Rohár <pali@kernel.org>
6 * Can be freely distributed and used under the terms of the GNU GPL.
10 * Tell 32-bit platforms that we are interested in 64-bit variant of off_t type
11 * as 32-bit variant of off_t type is signed and so it cannot represent all
12 * possible 32-bit offsets. It is required because off_t type is used by mmap().
14 #define _FILE_OFFSET_BITS 64
25 #include <sys/types.h>
30 #define OFF_MAX (off_t)((1ULL << (sizeof(off_t) * CHAR_BIT - 1)) - 1)
44 munmap_regs(struct pci_access *a)
46 struct mmio_cache *cache = a->aux;
51 munmap(cache->addr_map, pagesize);
52 if (cache->addr_page != cache->data_page)
53 munmap(cache->data_map, pagesize);
60 mmap_regs(struct pci_access *a, off_t addr_reg, off_t data_reg, int data_off, volatile void **addr, volatile void **data)
62 struct mmio_cache *cache = a->aux;
63 off_t addr_page = addr_reg & ~(pagesize-1);
64 off_t data_page = data_reg & ~(pagesize-1);
65 void *addr_map = MAP_FAILED;
66 void *data_map = MAP_FAILED;
68 if (cache && cache->addr_page == addr_page)
69 addr_map = cache->addr_map;
71 if (cache && cache->data_page == data_page)
72 data_map = cache->data_map;
74 if (addr_map == MAP_FAILED)
75 addr_map = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, a->fd, addr_page);
77 if (addr_map == MAP_FAILED)
80 if (data_map == MAP_FAILED)
82 if (data_page == addr_page)
85 data_map = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, a->fd, data_page);
88 if (data_map == MAP_FAILED)
90 if (!cache || cache->addr_map != addr_map)
91 munmap(addr_map, pagesize);
95 if (cache && cache->addr_page != addr_page)
96 munmap(cache->addr_map, pagesize);
98 if (cache && cache->data_page != data_page && cache->data_page != cache->addr_page)
99 munmap(cache->data_map, pagesize);
102 cache = a->aux = pci_malloc(a, sizeof(*cache));
104 cache->addr_page = addr_page;
105 cache->data_page = data_page;
106 cache->addr_map = addr_map;
107 cache->data_map = data_map;
109 *addr = (unsigned char *)addr_map + (addr_reg & (pagesize-1));
110 *data = (unsigned char *)data_map + (data_reg & (pagesize-1)) + data_off;
115 writeb(unsigned char value, volatile void *addr)
117 *(volatile unsigned char *)addr = value;
121 writew(unsigned short value, volatile void *addr)
123 *(volatile unsigned short *)addr = value;
127 writel(u32 value, volatile void *addr)
129 *(volatile u32 *)addr = value;
133 readb(volatile void *addr)
135 return *(volatile unsigned char *)addr;
138 static unsigned short
139 readw(volatile void *addr)
141 return *(volatile unsigned short *)addr;
145 readl(volatile void *addr)
147 return *(volatile u32 *)addr;
151 validate_addrs(const char *addrs)
153 const char *sep, *next;
154 unsigned long long num;
162 next = strchr(addrs, ',');
164 next = addrs + strlen(addrs);
166 sep = strchr(addrs, '/');
170 if (!isxdigit(*addrs) || !isxdigit(*(sep+1)))
174 num = strtoull(addrs, &endptr, 16);
175 if (errno || endptr != sep || (num & 3) || num > OFF_MAX)
179 num = strtoull(sep+1, &endptr, 16);
180 if (errno || endptr != next || (num & 3) || num > OFF_MAX)
191 get_domain_count(const char *addrs)
194 while (addrs = strchr(addrs, ','))
203 get_domain_addr(const char *addrs, int domain, off_t *addr_reg, off_t *data_reg)
209 addrs = strchr(addrs, ',');
215 *addr_reg = strtoull(addrs, &endptr, 16);
216 *data_reg = strtoull(endptr+1, NULL, 16);
222 conf1_config(struct pci_access *a)
224 pci_define_param(a, "devmem.path", PCI_PATH_DEVMEM_DEVICE, "Path to the /dev/mem device");
225 pci_define_param(a, "mmio-conf1.addrs", "", "Physical addresses of memory mapped Intel conf1 interface"); /* format: 0xaddr1/0xdata1,0xaddr2/0xdata2,... */
229 conf1_ext_config(struct pci_access *a)
231 pci_define_param(a, "devmem.path", PCI_PATH_DEVMEM_DEVICE, "Path to the /dev/mem device");
232 pci_define_param(a, "mmio-conf1-ext.addrs", "", "Physical addresses of memory mapped Intel conf1 extended interface"); /* format: 0xaddr1/0xdata1,0xaddr2/0xdata2,... */
236 detect(struct pci_access *a, char *addrs_param_name)
238 char *addrs = pci_get_param(a, addrs_param_name);
239 char *devmem = pci_get_param(a, "devmem.path");
243 a->debug("%s was not specified", addrs_param_name);
247 if (!validate_addrs(addrs))
249 a->debug("%s has invalid address format %s", addrs_param_name, addrs);
253 if (access(devmem, R_OK | W_OK))
255 a->debug("cannot access %s: %s", devmem, strerror(errno));
259 a->debug("using %s with %s", devmem, addrs);
264 conf1_detect(struct pci_access *a)
266 return detect(a, "mmio-conf1.addrs");
270 conf1_ext_detect(struct pci_access *a)
272 return detect(a, "mmio-conf1-ext.addrs");
276 get_addrs_param_name(struct pci_access *a)
278 if (a->methods->config == conf1_ext_config)
279 return "mmio-conf1-ext.addrs";
281 return "mmio-conf1.addrs";
285 conf1_init(struct pci_access *a)
287 char *addrs_param_name = get_addrs_param_name(a);
288 char *addrs = pci_get_param(a, addrs_param_name);
289 char *devmem = pci_get_param(a, "devmem.path");
291 pagesize = sysconf(_SC_PAGESIZE);
293 a->error("Cannot get page size: %s", strerror(errno));
296 a->error("Option %s was not specified.", addrs_param_name);
298 if (!validate_addrs(addrs))
299 a->error("Option %s has invalid address format \"%s\".", addrs_param_name, addrs);
301 a->fd = open(devmem, O_RDWR | O_DSYNC); /* O_DSYNC bypass CPU cache for mmap() on Linux */
303 a->error("Cannot open %s: %s.", devmem, strerror(errno));
307 conf1_cleanup(struct pci_access *a)
318 conf1_scan(struct pci_access *a)
320 char *addrs_param_name = get_addrs_param_name(a);
321 char *addrs = pci_get_param(a, addrs_param_name);
322 int domain_count = get_domain_count(addrs);
325 for (domain = 0; domain < domain_count; domain++)
326 pci_generic_scan_domain(a, domain);
330 conf1_ext_read(struct pci_dev *d, int pos, byte *buf, int len)
332 char *addrs_param_name = get_addrs_param_name(d->access);
333 char *addrs = pci_get_param(d->access, addrs_param_name);
334 volatile void *addr, *data;
335 off_t addr_reg, data_reg;
340 if (len != 1 && len != 2 && len != 4)
341 return pci_generic_block_read(d, pos, buf, len);
343 if (!get_domain_addr(addrs, d->domain, &addr_reg, &data_reg))
346 if (!mmap_regs(d->access, addr_reg, data_reg, pos&3, &addr, &data))
349 writel(0x80000000 | ((pos & 0xf00) << 16) | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos & 0xfc), addr);
350 readl(addr); /* write barrier for address */
355 buf[0] = readb(data);
358 ((u16 *) buf)[0] = readw(data);
361 ((u32 *) buf)[0] = readl(data);
369 conf1_read(struct pci_dev *d, int pos, byte *buf, int len)
374 return conf1_ext_read(d, pos, buf, len);
378 conf1_ext_write(struct pci_dev *d, int pos, byte *buf, int len)
380 char *addrs_param_name = get_addrs_param_name(d->access);
381 char *addrs = pci_get_param(d->access, addrs_param_name);
382 volatile void *addr, *data;
383 off_t addr_reg, data_reg;
388 if (len != 1 && len != 2 && len != 4)
389 return pci_generic_block_write(d, pos, buf, len);
391 if (!get_domain_addr(addrs, d->domain, &addr_reg, &data_reg))
394 if (!mmap_regs(d->access, addr_reg, data_reg, pos&3, &addr, &data))
397 writel(0x80000000 | ((pos & 0xf00) << 16) | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos & 0xfc), addr);
398 readl(addr); /* write barrier for address */
403 writeb(buf[0], data);
406 writew(((u16 *) buf)[0], data);
409 writel(((u32 *) buf)[0], data);
414 * write barrier for data
415 * Note that we cannot read from data port because it may have side effect.
416 * Instead we read from address port (which should not have side effect) to
417 * create a barrier between two conf1_write() calls. But this does not have
418 * to be 100% correct as it does not ensure barrier on data port itself.
419 * Correct way is to issue CPU instruction for full hw sync barrier but gcc
420 * does not provide any (builtin) function yet.
428 conf1_write(struct pci_dev *d, int pos, byte *buf, int len)
433 return conf1_ext_write(d, pos, buf, len);
436 struct pci_methods pm_mmio_conf1 = {
438 "Raw memory mapped I/O port access using Intel conf1 interface",
444 pci_generic_fill_info,
449 NULL /* cleanup_dev */
452 struct pci_methods pm_mmio_conf1_ext = {
454 "Raw memory mapped I/O port access using Intel conf1 extended interface",
460 pci_generic_fill_info,
465 NULL /* cleanup_dev */