xref: /libpciaccess/src/linux_sysfs.c (revision ec53d6ef)
1 /*
2  * (C) Copyright IBM Corporation 2006
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19  * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * \file linux_sysfs.c
27  * Access PCI subsystem using Linux's sysfs interface.  This interface is
28  * available starting somewhere in the late 2.5.x kernel phase, and is the
29  * prefered method on all 2.6.x kernels.
30  *
31  * \author Ian Romanick <[email protected]>
32  */
33 
34 #define _GNU_SOURCE
35 
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <sys/mman.h>
44 #include <dirent.h>
45 #include <errno.h>
46 
47 #include "config.h"
48 
49 #ifdef HAVE_MTRR
50 #include <asm/mtrr.h>
51 #include <sys/ioctl.h>
52 #endif
53 
54 #include "pciaccess.h"
55 #include "pciaccess_private.h"
56 #include "linux_devmem.h"
57 
58 static int pci_device_linux_sysfs_read_rom( struct pci_device * dev,
59     void * buffer );
60 
61 static int pci_device_linux_sysfs_probe( struct pci_device * dev );
62 
63 static int pci_device_linux_sysfs_map_range(struct pci_device *dev,
64     struct pci_device_mapping *map);
65 
66 static int pci_device_linux_sysfs_unmap_range(struct pci_device *dev,
67     struct pci_device_mapping *map);
68 
69 static int pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
70     pciaddr_t offset, pciaddr_t size, pciaddr_t * bytes_read );
71 
72 static int pci_device_linux_sysfs_write( struct pci_device * dev,
73     const void * data, pciaddr_t offset, pciaddr_t size,
74     pciaddr_t * bytes_wrtten );
75 
76 static const struct pci_system_methods linux_sysfs_methods = {
77     .destroy = NULL,
78     .destroy_device = NULL,
79     .read_rom = pci_device_linux_sysfs_read_rom,
80     .probe = pci_device_linux_sysfs_probe,
81     .map_range = pci_device_linux_sysfs_map_range,
82     .unmap_range = pci_device_linux_sysfs_unmap_range,
83 
84     .read = pci_device_linux_sysfs_read,
85     .write = pci_device_linux_sysfs_write,
86 
87     .fill_capabilities = pci_fill_capabilities_generic
88 };
89 
90 #define SYS_BUS_PCI "/sys/bus/pci/devices"
91 
92 
93 static int populate_entries(struct pci_system * pci_sys);
94 
95 
96 /**
97  * Attempt to access PCI subsystem using Linux's sysfs interface.
98  */
99 _pci_hidden int
100 pci_system_linux_sysfs_create( void )
101 {
102     int err = 0;
103     struct stat st;
104 
105 
106     /* If the directory "/sys/bus/pci/devices" exists, then the PCI subsystem
107      * can be accessed using this interface.
108      */
109 
110     if ( stat( SYS_BUS_PCI, & st ) == 0 ) {
111 	pci_sys = calloc( 1, sizeof( struct pci_system ) );
112 	if ( pci_sys != NULL ) {
113 	    pci_sys->methods = & linux_sysfs_methods;
114 	    err = populate_entries(pci_sys);
115 	}
116 	else {
117 	    err = ENOMEM;
118 	}
119     }
120     else {
121 	err = errno;
122     }
123 
124 #ifdef HAVE_MTRR
125     pci_sys->mtrr_fd = open("/proc/mtrr", O_WRONLY);
126 #endif
127 
128     return err;
129 }
130 
131 
132 /**
133  * Filter out the names "." and ".." from the scanned sysfs entries.
134  *
135  * \param d  Directory entry being processed by \c scandir.
136  *
137  * \return
138  * Zero if the entry name matches either "." or "..", non-zero otherwise.
139  *
140  * \sa scandir, populate_entries
141  */
142 static int
143 scan_sys_pci_filter( const struct dirent * d )
144 {
145     return !((strcmp( d->d_name, "." ) == 0)
146 	     || (strcmp( d->d_name, ".." ) == 0));
147 }
148 
149 
150 int
151 populate_entries( struct pci_system * p )
152 {
153     struct dirent ** devices;
154     int n;
155     int i;
156     int err = 0;
157 
158 
159     n = scandir( SYS_BUS_PCI, & devices, scan_sys_pci_filter, alphasort );
160     if ( n > 0 ) {
161 	p->num_devices = n;
162 	p->devices = calloc( n, sizeof( struct pci_device_private ) );
163 
164 	if (p->devices != NULL) {
165 	    for (i = 0 ; i < n ; i++) {
166 		uint8_t config[48];
167 		pciaddr_t bytes;
168 		unsigned dom, bus, dev, func;
169 		struct pci_device_private *device =
170 			(struct pci_device_private *) &p->devices[i];
171 
172 
173 		sscanf(devices[i]->d_name, "%04x:%02x:%02x.%1u",
174 		       & dom, & bus, & dev, & func);
175 
176 		device->base.domain = dom;
177 		device->base.bus = bus;
178 		device->base.dev = dev;
179 		device->base.func = func;
180 
181 
182 		err = pci_device_linux_sysfs_read(& device->base, config, 0,
183 						  48, & bytes);
184 		if ((bytes == 48) && !err) {
185 		    device->base.vendor_id = (uint16_t)config[0]
186 			+ ((uint16_t)config[1] << 8);
187 		    device->base.device_id = (uint16_t)config[2]
188 			+ ((uint16_t)config[3] << 8);
189 		    device->base.device_class = (uint32_t)config[9]
190 			+ ((uint32_t)config[10] << 8)
191 			+ ((uint32_t)config[11] << 16);
192 		    device->base.revision = config[8];
193 		    device->base.subvendor_id = (uint16_t)config[44]
194 			+ ((uint16_t)config[45] << 8);
195 		    device->base.subdevice_id = (uint16_t)config[46]
196 			+ ((uint16_t)config[47] << 8);
197 		}
198 
199 		if (err) {
200 		    break;
201 		}
202 	    }
203 	}
204 	else {
205 	    err = ENOMEM;
206 	}
207     }
208 
209     if (err) {
210 	free(p->devices);
211 	p->devices = NULL;
212     }
213 
214     return err;
215 }
216 
217 
218 static int
219 pci_device_linux_sysfs_probe( struct pci_device * dev )
220 {
221     char     name[256];
222     uint8_t  config[256];
223     char     resource[512];
224     int fd;
225     pciaddr_t bytes;
226     unsigned i;
227     int err;
228 
229 
230     err = pci_device_linux_sysfs_read( dev, config, 0, 256, & bytes );
231     if ( bytes >= 64 ) {
232 	struct pci_device_private *priv = (struct pci_device_private *) dev;
233 
234 	dev->irq = config[60];
235 	priv->header_type = config[14];
236 
237 
238 	/* The PCI config registers can be used to obtain information
239 	 * about the memory and I/O regions for the device.  However,
240 	 * doing so requires some tricky parsing (to correctly handle
241 	 * 64-bit memory regions) and requires writing to the config
242 	 * registers.  Since we'd like to avoid having to deal with the
243 	 * parsing issues and non-root users can write to PCI config
244 	 * registers, we use a different file in the device's sysfs
245 	 * directory called "resource".
246 	 *
247 	 * The resource file contains all of the needed information in
248 	 * a format that is consistent across all platforms.  Each BAR
249 	 * and the expansion ROM have a single line of data containing
250 	 * 3, 64-bit hex values:  the first address in the region,
251 	 * the last address in the region, and the region's flags.
252 	 */
253 	snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/resource",
254 		  SYS_BUS_PCI,
255 		  dev->domain,
256 		  dev->bus,
257 		  dev->dev,
258 		  dev->func );
259 	fd = open( name, O_RDONLY );
260 	if ( fd != -1 ) {
261 	    char * next;
262 	    pciaddr_t  low_addr;
263 	    pciaddr_t  high_addr;
264 	    pciaddr_t  flags;
265 
266 
267 	    bytes = read( fd, resource, 512 );
268 	    resource[511] = '\0';
269 
270 	    close( fd );
271 
272 	    next = resource;
273 	    for ( i = 0 ; i < 6 ; i++ ) {
274 
275 		dev->regions[i].base_addr = strtoull( next, & next, 16 );
276 		high_addr = strtoull( next, & next, 16 );
277 		flags = strtoull( next, & next, 16 );
278 
279 		if ( dev->regions[i].base_addr != 0 ) {
280 		    dev->regions[i].size = (high_addr
281 					    - dev->regions[i].base_addr) + 1;
282 
283 		    dev->regions[i].is_IO = (flags & 0x01);
284 		    dev->regions[i].is_64 = (flags & 0x04);
285 		    dev->regions[i].is_prefetchable = (flags & 0x08);
286 		}
287 	    }
288 
289 	    low_addr = strtoull( next, & next, 16 );
290 	    high_addr = strtoull( next, & next, 16 );
291 	    flags = strtoull( next, & next, 16 );
292 	    if ( low_addr != 0 ) {
293 		priv->rom_base = low_addr;
294 		dev->rom_size = (high_addr - low_addr) + 1;
295 	    }
296 	}
297     }
298 
299     return err;
300 }
301 
302 
303 static int
304 pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer )
305 {
306     char name[256];
307     int fd;
308     struct stat  st;
309     int err = 0;
310     size_t rom_size;
311     size_t total_bytes;
312 
313 
314     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/rom",
315 	      SYS_BUS_PCI,
316 	      dev->domain,
317 	      dev->bus,
318 	      dev->dev,
319 	      dev->func );
320 
321     fd = open( name, O_RDWR );
322     if ( fd == -1 ) {
323 	/* If reading the ROM using sysfs fails, fall back to the old
324 	 * /dev/mem based interface.
325 	 */
326 	return pci_device_linux_devmem_read_rom(dev, buffer);
327     }
328 
329 
330     if ( fstat( fd, & st ) == -1 ) {
331 	close( fd );
332 	return errno;
333     }
334 
335     rom_size = st.st_size;
336     if ( rom_size == 0 )
337 	rom_size = 0x10000;
338 
339     /* This is a quirky thing on Linux.  Even though the ROM and the file
340      * for the ROM in sysfs are read-only, the string "1" must be written to
341      * the file to enable the ROM.  After the data has been read, "0" must be
342      * written to the file to disable the ROM.
343      */
344     write( fd, "1", 1 );
345     lseek( fd, 0, SEEK_SET );
346 
347     for ( total_bytes = 0 ; total_bytes < rom_size ; /* empty */ ) {
348 	const int bytes = read( fd, (char *) buffer + total_bytes,
349 				rom_size - total_bytes );
350 	if ( bytes == -1 ) {
351 	    err = errno;
352 	    break;
353 	}
354 	else if ( bytes == 0 ) {
355 	    break;
356 	}
357 
358 	total_bytes += bytes;
359     }
360 
361 
362     lseek( fd, 0, SEEK_SET );
363     write( fd, "0", 1 );
364 
365     close( fd );
366     return err;
367 }
368 
369 
370 static int
371 pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
372 			     pciaddr_t offset, pciaddr_t size,
373 			     pciaddr_t * bytes_read )
374 {
375     char name[256];
376     pciaddr_t temp_size = size;
377     int err = 0;
378     int fd;
379     char *data_bytes = data;
380 
381     if ( bytes_read != NULL ) {
382 	*bytes_read = 0;
383     }
384 
385     /* Each device has a directory under sysfs.  Within that directory there
386      * is a file named "config".  This file used to access the PCI config
387      * space.  It is used here to obtain most of the information about the
388      * device.
389      */
390     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
391 	      SYS_BUS_PCI,
392 	      dev->domain,
393 	      dev->bus,
394 	      dev->dev,
395 	      dev->func );
396 
397     fd = open( name, O_RDONLY );
398     if ( fd == -1 ) {
399 	return errno;
400     }
401 
402 
403     while ( temp_size > 0 ) {
404 	const ssize_t bytes = pread64( fd, data_bytes, temp_size, offset );
405 
406 	/* If zero bytes were read, then we assume it's the end of the
407 	 * config file.
408 	 */
409 	if ( bytes <= 0 ) {
410 	    err = errno;
411 	    break;
412 	}
413 
414 	temp_size -= bytes;
415 	offset += bytes;
416 	data_bytes += bytes;
417     }
418 
419     if ( bytes_read != NULL ) {
420 	*bytes_read = size - temp_size;
421     }
422 
423     close( fd );
424     return err;
425 }
426 
427 
428 static int
429 pci_device_linux_sysfs_write( struct pci_device * dev, const void * data,
430 			     pciaddr_t offset, pciaddr_t size,
431 			     pciaddr_t * bytes_written )
432 {
433     char name[256];
434     pciaddr_t temp_size = size;
435     int err = 0;
436     int fd;
437     const char *data_bytes = data;
438 
439     if ( bytes_written != NULL ) {
440 	*bytes_written = 0;
441     }
442 
443     /* Each device has a directory under sysfs.  Within that directory there
444      * is a file named "config".  This file used to access the PCI config
445      * space.  It is used here to obtain most of the information about the
446      * device.
447      */
448     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
449 	      SYS_BUS_PCI,
450 	      dev->domain,
451 	      dev->bus,
452 	      dev->dev,
453 	      dev->func );
454 
455     fd = open( name, O_WRONLY );
456     if ( fd == -1 ) {
457 	return errno;
458     }
459 
460 
461     while ( temp_size > 0 ) {
462 	const ssize_t bytes = pwrite64( fd, data_bytes, temp_size, offset );
463 
464 	/* If zero bytes were written, then we assume it's the end of the
465 	 * config file.
466 	 */
467 	if ( bytes <= 0 ) {
468 	    err = errno;
469 	    break;
470 	}
471 
472 	temp_size -= bytes;
473 	offset += bytes;
474 	data_bytes += bytes;
475     }
476 
477     if ( bytes_written != NULL ) {
478 	*bytes_written = size - temp_size;
479     }
480 
481     close( fd );
482     return err;
483 }
484 
485 
486 /**
487  * Map a memory region for a device using the Linux sysfs interface.
488  *
489  * \param dev   Device whose memory region is to be mapped.
490  * \param map   Parameters of the mapping that is to be created.
491  *
492  * \return
493  * Zero on success or an \c errno value on failure.
494  *
495  * \sa pci_device_map_rrange, pci_device_linux_sysfs_unmap_range
496  *
497  * \todo
498  * Some older 2.6.x kernels don't implement the resourceN files.  On those
499  * systems /dev/mem must be used.  On these systems it is also possible that
500  * \c mmap64 may need to be used.
501  */
502 static int
503 pci_device_linux_sysfs_map_range(struct pci_device *dev,
504                                  struct pci_device_mapping *map)
505 {
506     char name[256];
507     int fd;
508     int err = 0;
509     const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
510         ? (PROT_READ | PROT_WRITE) : PROT_READ;
511     const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
512         ? O_RDWR : O_RDONLY;
513     const off_t offset = map->base - dev->regions[map->region].base_addr;
514 #ifdef HAVE_MTRR
515     struct mtrr_sentry sentry = {
516 	.base = map->base,
517         .size = map->size,
518 	.type = MTRR_TYPE_UNCACHABLE
519     };
520 #endif
521 
522     snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u",
523              SYS_BUS_PCI,
524              dev->domain,
525              dev->bus,
526              dev->dev,
527              dev->func,
528              map->region);
529 
530     fd = open(name, open_flags);
531     if (fd == -1) {
532         return errno;
533     }
534 
535 
536     map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
537     if (map->memory == MAP_FAILED) {
538         err = errno;
539         map->memory = NULL;
540     }
541 
542     close(fd);
543 
544 #ifdef HAVE_MTRR
545     if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
546         sentry.type = MTRR_TYPE_WRBACK;
547     } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
548         sentry.type = MTRR_TYPE_WRCOMB;
549     }
550 
551     if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
552 	if (ioctl(pci_sys->mtrr_fd, MTRRIOC_ADD_ENTRY, &sentry) < 0) {
553 	    /* FIXME: Should we report an error in this case?
554 	     */
555 	    fprintf(stderr, "error setting MTRR "
556 		    "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
557 		    sentry.base, sentry.size, sentry.type,
558 		    strerror(errno), errno);
559 /*            err = errno;*/
560 	}
561     }
562 #endif
563 
564     return err;
565 }
566 
567 /**
568  * Unmap a memory region for a device using the Linux sysfs interface.
569  *
570  * \param dev   Device whose memory region is to be unmapped.
571  * \param map   Parameters of the mapping that is to be destroyed.
572  *
573  * \return
574  * Zero on success or an \c errno value on failure.
575  *
576  * \sa pci_device_map_rrange, pci_device_linux_sysfs_map_range
577  *
578  * \todo
579  * Some older 2.6.x kernels don't implement the resourceN files.  On those
580  * systems /dev/mem must be used.  On these systems it is also possible that
581  * \c mmap64 may need to be used.
582  */
583 static int
584 pci_device_linux_sysfs_unmap_range(struct pci_device *dev,
585 				   struct pci_device_mapping *map)
586 {
587     int err = 0;
588 #ifdef HAVE_MTRR
589     struct mtrr_sentry sentry = {
590 	.base = map->base,
591         .size = map->size,
592 	.type = MTRR_TYPE_UNCACHABLE
593     };
594 #endif
595 
596     err = pci_device_generic_unmap_range (dev, map);
597     if (err)
598 	return err;
599 
600 #ifdef HAVE_MTRR
601     if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
602         sentry.type = MTRR_TYPE_WRBACK;
603     } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
604         sentry.type = MTRR_TYPE_WRCOMB;
605     }
606 
607     if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
608 	if (ioctl(pci_sys->mtrr_fd, MTRRIOC_DEL_ENTRY, &sentry) < 0) {
609 	    /* FIXME: Should we report an error in this case?
610 	     */
611 	    fprintf(stderr, "error setting MTRR "
612 		    "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
613 		    sentry.base, sentry.size, sentry.type,
614 		    strerror(errno), errno);
615 /*            err = errno;*/
616 	}
617     }
618 #endif
619 
620     return err;
621 }
622