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