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(unsigned long value, volatile void *addr)
129 *(volatile unsigned long *)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 unsigned long *)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_detect(struct pci_access *a)
231 char *addrs = pci_get_param(a, "mmio-conf1.addrs");
232 char *devmem = pci_get_param(a, "devmem.path");
236 a->debug("mmio-conf1.addrs was not specified");
240 if (!validate_addrs(addrs))
242 a->debug("mmio-conf1.addrs has invalid address format %s", addrs);
246 if (access(devmem, R_OK))
248 a->debug("cannot access %s", devmem);
252 a->debug("using %s with %s", devmem, addrs);
257 conf1_init(struct pci_access *a)
259 char *addrs = pci_get_param(a, "mmio-conf1.addrs");
260 char *devmem = pci_get_param(a, "devmem.path");
262 pagesize = sysconf(_SC_PAGESIZE);
264 a->error("Cannot get page size: %s", strerror(errno));
267 a->error("Option mmio-conf1.addrs was not specified.");
269 if (!validate_addrs(addrs))
270 a->error("Option mmio-conf1.addrs has invalid address format \"%s\".", addrs);
272 a->fd = open(devmem, O_RDWR);
274 a->error("Cannot open %s: %s.", devmem, strerror(errno));
278 conf1_cleanup(struct pci_access *a)
289 conf1_scan(struct pci_access *a)
291 char *addrs = pci_get_param(a, "mmio-conf1.addrs");
292 int domain_count = get_domain_count(addrs);
295 for (domain = 0; domain < domain_count; domain++)
296 pci_generic_scan_domain(a, domain);
300 conf1_read(struct pci_dev *d, int pos, byte *buf, int len)
302 char *addrs = pci_get_param(d->access, "mmio-conf1.addrs");
303 volatile void *addr, *data;
304 off_t addr_reg, data_reg;
309 if (len != 1 && len != 2 && len != 4)
310 return pci_generic_block_read(d, pos, buf, len);
312 if (!get_domain_addr(addrs, d->domain, &addr_reg, &data_reg))
315 if (!mmap_regs(d->access, addr_reg, data_reg, pos&3, &addr, &data))
318 writel(0x80000000 | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos & 0xfc), addr);
323 buf[0] = readb(data);
326 ((u16 *) buf)[0] = readw(data);
329 ((u32 *) buf)[0] = readl(data);
337 conf1_write(struct pci_dev *d, int pos, byte *buf, int len)
339 char *addrs = pci_get_param(d->access, "mmio-conf1.addrs");
340 volatile void *addr, *data;
341 off_t addr_reg, data_reg;
346 if (len != 1 && len != 2 && len != 4)
347 return pci_generic_block_write(d, pos, buf, len);
349 if (!get_domain_addr(addrs, d->domain, &addr_reg, &data_reg))
352 if (!mmap_regs(d->access, addr_reg, data_reg, pos&3, &addr, &data))
355 writel(0x80000000 | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos & 0xfc), addr);
360 writeb(buf[0], data);
363 writew(((u16 *) buf)[0], data);
366 writel(((u32 *) buf)[0], data);
373 struct pci_methods pm_mmio_conf1 = {
375 "Raw memory mapped I/O port access using Intel conf1 interface",
381 pci_generic_fill_info,
386 NULL /* cleanup_dev */