OSDN Git Service

linux_sysfs.c: Include <limits.h> for PATH_MAX
[android-x86/external-libpciaccess.git] / src / common_interface.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 common_interface.c
27  * Platform independent interface glue.
28  *
29  * \author Ian Romanick <idr@us.ibm.com>
30  */
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38
39 #include "pciaccess.h"
40 #include "pciaccess_private.h"
41
42 #if defined(__linux__) || defined(__GLIBC__) || defined(__CYGWIN__)
43 #include <byteswap.h>
44
45 #if __BYTE_ORDER == __BIG_ENDIAN
46 # define LETOH_16(x)   bswap_16(x)
47 # define HTOLE_16(x)   bswap_16(x)
48 # define LETOH_32(x)   bswap_32(x)
49 # define HTOLE_32(x)   bswap_32(x)
50 #else
51 # define LETOH_16(x)   (x)
52 # define HTOLE_16(x)   (x)
53 # define LETOH_32(x)   (x)
54 # define HTOLE_32(x)   (x)
55 #endif /* linux */
56
57 #elif defined(__sun)
58
59 #include <sys/byteorder.h>
60
61 #ifdef _BIG_ENDIAN
62 # define LETOH_16(x)   BSWAP_16(x)
63 # define HTOLE_16(x)   BSWAP_16(x)
64 # define LETOH_32(x)   BSWAP_32(x)
65 # define HTOLE_32(x)   BSWAP_32(x)
66 #else
67 # define LETOH_16(x)   (x)
68 # define HTOLE_16(x)   (x)
69 # define LETOH_32(x)   (x)
70 # define HTOLE_32(x)   (x)
71 #endif /* Solaris */
72
73 #else
74
75 #include <sys/endian.h>
76
77 #define HTOLE_16(x)     htole16(x)
78 #define HTOLE_32(x)     htole32(x)
79
80 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
81 #define LETOH_16(x)     le16toh(x)
82 #define LETOH_32(x)     le32toh(x)
83 #else
84 #define LETOH_16(x)     letoh16(x)
85 #define LETOH_32(x)     letoh32(x)
86 #endif
87
88 #endif /* others */
89
90 /**
91  * Read a device's expansion ROM.
92  *
93  * Reads the device's expansion ROM and stores the data in the memory pointed
94  * to by \c buffer.  The buffer must be at least \c pci_device::rom_size
95  * bytes.
96  *
97  * \param dev    Device whose expansion ROM is to be read.
98  * \param buffer Memory in which to store the ROM.
99  *
100  * \return
101  * Zero on success or an \c errno value on failure.
102  */
103 int
104 pci_device_read_rom( struct pci_device * dev, void * buffer )
105 {
106     if ( (dev == NULL) || (buffer == NULL) ) {
107         return EFAULT;
108     }
109
110
111     return (pci_sys->methods->read_rom)( dev, buffer );
112 }
113
114 /**
115  * Probe a PCI (VGA) device to determine if its the boot VGA device
116  *
117  * \param dev    Device whose VGA status to query
118  * \return
119  * Zero if not the boot VGA, 1 if the boot VGA.
120  */
121 int
122 pci_device_is_boot_vga( struct pci_device * dev )
123 {
124         if (!pci_sys->methods->boot_vga)
125                 return 0;
126         return pci_sys->methods->boot_vga( dev );
127 }
128
129 /**
130  * Probe a PCI device to determine if a kernel driver is attached.
131  *
132  * \param dev Device to query
133  * \return
134  * Zero if no driver attached, 1 if attached kernel drviver
135  */
136 int
137 pci_device_has_kernel_driver( struct pci_device * dev )
138 {
139         if (!pci_sys->methods->has_kernel_driver)
140                 return 0;
141         return pci_sys->methods->has_kernel_driver( dev );
142 }
143
144 /**
145  * Probe a PCI device to learn information about the device.
146  *
147  * Probes a PCI device to learn various information about the device.  Before
148  * calling this function, the only public fields in the \c pci_device
149  * structure that have valid values are \c pci_device::domain,
150  * \c pci_device::bus, \c pci_device::dev, and \c pci_device::func.
151  *
152  * \param dev  Device to be probed.
153  *
154  * \return
155  * Zero on success or an \c errno value on failure.
156  */
157 int
158 pci_device_probe( struct pci_device * dev )
159 {
160     if ( dev == NULL ) {
161         return EFAULT;
162     }
163
164
165     return (pci_sys->methods->probe)( dev );
166 }
167
168
169 /**
170  * Map the specified BAR so that it can be accessed by the CPU.
171  *
172  * Maps the specified BAR for access by the processor.  The pointer to the
173  * mapped region is stored in the \c pci_mem_region::memory pointer for the
174  * BAR.
175  *
176  * \param dev          Device whose memory region is to be mapped.
177  * \param region       Region, on the range [0, 5], that is to be mapped.
178  * \param write_enable Map for writing (non-zero).
179  *
180  * \return
181  * Zero on success or an \c errno value on failure.
182  *
183  * \sa pci_device_map_range, pci_device_unmap_range
184  * \deprecated
185  */
186 int
187 pci_device_map_region(struct pci_device * dev, unsigned region,
188                       int write_enable)
189 {
190     const unsigned map_flags =
191         (write_enable) ? PCI_DEV_MAP_FLAG_WRITABLE : 0;
192
193     if ((region > 5) || (dev->regions[region].size == 0))  {
194         return ENOENT;
195     }
196
197     if (dev->regions[region].memory != NULL) {
198         return 0;
199     }
200
201     return pci_device_map_range(dev, dev->regions[region].base_addr,
202                                 dev->regions[region].size, map_flags,
203                                 &dev->regions[region].memory);
204 }
205
206
207 /**
208  * Map the specified memory range so that it can be accessed by the CPU.
209  *
210  * Maps the specified memory range for access by the processor.  The pointer
211  * to the mapped region is stored in \c addr.  In addition, the
212  * \c pci_mem_region::memory pointer for the BAR will be updated.
213  *
214  * \param dev          Device whose memory region is to be mapped.
215  * \param base         Base address of the range to be mapped.
216  * \param size         Size of the range to be mapped.
217  * \param write_enable Map for writing (non-zero).
218  * \param addr         Location to store the mapped address.
219  *
220  * \return
221  * Zero on success or an \c errno value on failure.
222  *
223  * \sa pci_device_map_range
224  */
225 int pci_device_map_memory_range(struct pci_device *dev,
226                                 pciaddr_t base, pciaddr_t size,
227                                 int write_enable, void **addr)
228 {
229     return pci_device_map_range(dev, base, size,
230                                 (write_enable) ? PCI_DEV_MAP_FLAG_WRITABLE : 0,
231                                 addr);
232 }
233
234
235 /**
236  * Map the specified memory range so that it can be accessed by the CPU.
237  *
238  * Maps the specified memory range for access by the processor.  The pointer
239  * to the mapped region is stored in \c addr.  In addition, the
240  * \c pci_mem_region::memory pointer for the BAR will be updated.
241  *
242  * \param dev          Device whose memory region is to be mapped.
243  * \param base         Base address of the range to be mapped.
244  * \param size         Size of the range to be mapped.
245  * \param map_flags    Flag bits controlling how the mapping is accessed.
246  * \param addr         Location to store the mapped address.
247  *
248  * \return
249  * Zero on success or an \c errno value on failure.
250  *
251  * \sa pci_device_unmap_range
252  */
253 int
254 pci_device_map_range(struct pci_device *dev, pciaddr_t base,
255                      pciaddr_t size, unsigned map_flags,
256                      void **addr)
257 {
258     struct pci_device_private *const devp =
259         (struct pci_device_private *) dev;
260     struct pci_device_mapping *mappings;
261     unsigned region;
262     unsigned i;
263     int err = 0;
264
265
266     *addr = NULL;
267
268     if (dev == NULL) {
269         return EFAULT;
270     }
271
272
273     for (region = 0; region < 6; region++) {
274         const struct pci_mem_region * const r = &dev->regions[region];
275
276         if (r->size != 0) {
277             if ((r->base_addr <= base) && ((r->base_addr + r->size) > base)) {
278                 if ((base + size) > (r->base_addr + r->size)) {
279                     return E2BIG;
280                 }
281
282                 break;
283             }
284         }
285     }
286
287     if (region > 5) {
288         return ENOENT;
289     }
290
291     /* Make sure that there isn't already a mapping with the same base and
292      * size.
293      */
294     for (i = 0; i < devp->num_mappings; i++) {
295         if ((devp->mappings[i].base == base)
296             && (devp->mappings[i].size == size)) {
297             return EINVAL;
298         }
299     }
300
301
302     mappings = realloc(devp->mappings,
303                        (sizeof(devp->mappings[0]) * (devp->num_mappings + 1)));
304     if (mappings == NULL) {
305         return ENOMEM;
306     }
307
308     mappings[devp->num_mappings].base = base;
309     mappings[devp->num_mappings].size = size;
310     mappings[devp->num_mappings].region = region;
311     mappings[devp->num_mappings].flags = map_flags;
312     mappings[devp->num_mappings].memory = NULL;
313
314     if (dev->regions[region].memory == NULL) {
315         err = (*pci_sys->methods->map_range)(dev,
316                                              &mappings[devp->num_mappings]);
317     }
318
319     if (err == 0) {
320         *addr =  mappings[devp->num_mappings].memory;
321         devp->num_mappings++;
322     } else {
323         mappings = realloc(mappings,
324                            (sizeof(mappings[0]) * devp->num_mappings));
325     }
326
327     devp->mappings = mappings;
328
329     return err;
330 }
331
332
333 /**
334  * Unmap the specified BAR so that it can no longer be accessed by the CPU.
335  *
336  * Unmaps the specified BAR that was previously mapped via
337  * \c pci_device_map_region.
338  *
339  * \param dev          Device whose memory region is to be mapped.
340  * \param region       Region, on the range [0, 5], that is to be mapped.
341  *
342  * \return
343  * Zero on success or an \c errno value on failure.
344  *
345  * \sa pci_device_map_range, pci_device_unmap_range
346  * \deprecated
347  */
348 int
349 pci_device_unmap_region( struct pci_device * dev, unsigned region )
350 {
351     int err;
352
353     if (dev == NULL) {
354         return EFAULT;
355     }
356
357     if ((region > 5) || (dev->regions[region].size == 0)) {
358         return ENOENT;
359     }
360
361     err = pci_device_unmap_range(dev, dev->regions[region].memory,
362                                  dev->regions[region].size);
363     if (!err) {
364         dev->regions[region].memory = NULL;
365     }
366
367     return err;
368 }
369
370
371 /**
372  * Unmap the specified memory range so that it can no longer be accessed by the CPU.
373  *
374  * Unmaps the specified memory range that was previously mapped via
375  * \c pci_device_map_memory_range.
376  *
377  * \param dev          Device whose memory is to be unmapped.
378  * \param memory       Pointer to the base of the mapped range.
379  * \param size         Size, in bytes, of the range to be unmapped.
380  *
381  * \return
382  * Zero on success or an \c errno value on failure.
383  *
384  * \sa pci_device_map_range, pci_device_unmap_range
385  * \deprecated
386  */
387 int
388 pci_device_unmap_memory_range(struct pci_device *dev, void *memory,
389                               pciaddr_t size)
390 {
391     return pci_device_unmap_range(dev, memory, size);
392 }
393
394
395 /**
396  * Unmap the specified memory range so that it can no longer be accessed by the CPU.
397  *
398  * Unmaps the specified memory range that was previously mapped via
399  * \c pci_device_map_memory_range.
400  *
401  * \param dev          Device whose memory is to be unmapped.
402  * \param memory       Pointer to the base of the mapped range.
403  * \param size         Size, in bytes, of the range to be unmapped.
404  *
405  * \return
406  * Zero on success or an \c errno value on failure.
407  *
408  * \sa pci_device_map_range
409  */
410 int
411 pci_device_unmap_range(struct pci_device *dev, void *memory,
412                        pciaddr_t size)
413 {
414     struct pci_device_private *const devp =
415         (struct pci_device_private *) dev;
416     unsigned i;
417     int err;
418
419
420     if (dev == NULL) {
421         return EFAULT;
422     }
423
424     for (i = 0; i < devp->num_mappings; i++) {
425         if ((devp->mappings[i].memory == memory)
426             && (devp->mappings[i].size == size)) {
427             break;
428         }
429     }
430
431     if (i == devp->num_mappings) {
432         return ENOENT;
433     }
434
435
436     err = (*pci_sys->methods->unmap_range)(dev, &devp->mappings[i]);
437     if (!err) {
438         const unsigned entries_to_move = (devp->num_mappings - i) - 1;
439
440         if (entries_to_move > 0) {
441             (void) memmove(&devp->mappings[i],
442                            &devp->mappings[i + 1],
443                            entries_to_move * sizeof(devp->mappings[0]));
444         }
445
446         devp->num_mappings--;
447         devp->mappings = realloc(devp->mappings,
448                                  (sizeof(devp->mappings[0]) * devp->num_mappings));
449     }
450
451     return err;
452 }
453
454
455 /**
456  * Read arbitrary bytes from device's PCI config space
457  *
458  * Reads data from the device's PCI configuration space.  As with the system
459  * read command, less data may be returned, without an error, than was
460  * requested.  This is particularly the case if a non-root user tries to read
461  * beyond the first 64-bytes of configuration space.
462  *
463  * \param dev         Device whose PCI configuration data is to be read.
464  * \param data        Location to store the data
465  * \param offset      Initial byte offset to read
466  * \param size        Total number of bytes to read
467  * \param bytes_read  Location to store the actual number of bytes read.  This
468  *                    pointer may be \c NULL.
469  *
470  * \returns
471  * Zero on success or an errno value on failure.
472  *
473  * \note
474  * Data read from PCI configuration space using this routine is \b not
475  * byte-swapped to the host's byte order.  PCI configuration data is always
476  * stored in little-endian order, and that is what this routine returns.
477  */
478 int
479 pci_device_cfg_read( struct pci_device * dev, void * data,
480                      pciaddr_t offset, pciaddr_t size,
481                      pciaddr_t * bytes_read )
482 {
483     pciaddr_t  scratch;
484
485     if ( (dev == NULL) || (data == NULL) ) {
486         return EFAULT;
487     }
488
489     return pci_sys->methods->read( dev, data, offset, size,
490                                    (bytes_read == NULL)
491                                    ? & scratch : bytes_read );
492 }
493
494
495 int
496 pci_device_cfg_read_u8( struct pci_device * dev, uint8_t * data,
497                         pciaddr_t offset )
498 {
499     pciaddr_t bytes;
500     int err = pci_device_cfg_read( dev, data, offset, 1, & bytes );
501
502     if ( (err == 0) && (bytes != 1) ) {
503         err = ENXIO;
504     }
505
506     return err;
507 }
508
509
510 int
511 pci_device_cfg_read_u16( struct pci_device * dev, uint16_t * data,
512                          pciaddr_t offset )
513 {
514     pciaddr_t bytes;
515     int err = pci_device_cfg_read( dev, data, offset, 2, & bytes );
516
517     if ( (err == 0) && (bytes != 2) ) {
518         err = ENXIO;
519     }
520
521     *data = LETOH_16( *data );
522     return err;
523 }
524
525
526 int
527 pci_device_cfg_read_u32( struct pci_device * dev, uint32_t * data,
528                          pciaddr_t offset )
529 {
530     pciaddr_t bytes;
531     int err = pci_device_cfg_read( dev, data, offset, 4, & bytes );
532
533     if ( (err == 0) && (bytes != 4) ) {
534         err = ENXIO;
535     }
536
537     *data = LETOH_32( *data );
538     return err;
539 }
540
541
542 /**
543  * Write arbitrary bytes to device's PCI config space
544  *
545  * Writes data to the device's PCI configuration space.  As with the system
546  * write command, less data may be written, without an error, than was
547  * requested.
548  *
549  * \param dev         Device whose PCI configuration data is to be written.
550  * \param data        Location of the source data
551  * \param offset      Initial byte offset to write
552  * \param size        Total number of bytes to write
553  * \param bytes_read  Location to store the actual number of bytes written.
554  *                    This pointer may be \c NULL.
555  *
556  * \returns
557  * Zero on success or an errno value on failure.
558  *
559  * \note
560  * Data written to PCI configuration space using this routine is \b not
561  * byte-swapped from the host's byte order.  PCI configuration data is always
562  * stored in little-endian order, so data written with this routine should be
563  * put in that order in advance.
564  */
565 int
566 pci_device_cfg_write( struct pci_device * dev, const void * data,
567                       pciaddr_t offset, pciaddr_t size,
568                       pciaddr_t * bytes_written )
569 {
570     pciaddr_t  scratch;
571
572     if ( (dev == NULL) || (data == NULL) ) {
573         return EFAULT;
574     }
575
576     return pci_sys->methods->write( dev, data, offset, size,
577                                     (bytes_written == NULL)
578                                     ? & scratch : bytes_written );
579 }
580
581
582 int
583 pci_device_cfg_write_u8(struct pci_device *dev, uint8_t data,
584                         pciaddr_t offset)
585 {
586     pciaddr_t bytes;
587     int err = pci_device_cfg_write(dev, & data, offset, 1, & bytes);
588
589     if ( (err == 0) && (bytes != 1) ) {
590         err = ENOSPC;
591     }
592
593
594     return err;
595 }
596
597
598 int
599 pci_device_cfg_write_u16(struct pci_device *dev, uint16_t data,
600                          pciaddr_t offset)
601 {
602     pciaddr_t bytes;
603     const uint16_t temp = HTOLE_16(data);
604     int err = pci_device_cfg_write( dev, & temp, offset, 2, & bytes );
605
606     if ( (err == 0) && (bytes != 2) ) {
607         err = ENOSPC;
608     }
609
610
611     return err;
612 }
613
614
615 int
616 pci_device_cfg_write_u32(struct pci_device *dev, uint32_t data,
617                          pciaddr_t offset)
618 {
619     pciaddr_t bytes;
620     const uint32_t temp = HTOLE_32(data);
621     int err = pci_device_cfg_write( dev, & temp, offset, 4, & bytes );
622
623     if ( (err == 0) && (bytes != 4) ) {
624         err = ENOSPC;
625     }
626
627
628     return err;
629 }
630
631
632 int
633 pci_device_cfg_write_bits( struct pci_device * dev, uint32_t mask,
634                            uint32_t data, pciaddr_t offset )
635 {
636     uint32_t  temp;
637     int err;
638
639     err = pci_device_cfg_read_u32( dev, & temp, offset );
640     if ( ! err ) {
641         temp &= ~mask;
642         temp |= data;
643
644         err = pci_device_cfg_write_u32(dev, temp, offset);
645     }
646
647     return err;
648 }
649
650 void
651 pci_device_enable(struct pci_device *dev)
652 {
653     if (dev == NULL) {
654         return;
655     }
656
657     if (pci_sys->methods->enable)
658         pci_sys->methods->enable(dev);
659 }
660
661 /**
662  * Map the legacy memory space for the PCI domain containing \c dev.
663  *
664  * \param dev          Device whose memory region is to be mapped.
665  * \param base         Base address of the range to be mapped.
666  * \param size         Size of the range to be mapped.
667  * \param map_flags    Flag bits controlling how the mapping is accessed.
668  * \param addr         Location to store the mapped address.
669  *
670  * \returns
671  * Zero on success or an \c errno value on failure.
672  */
673 int
674 pci_device_map_legacy(struct pci_device *dev, pciaddr_t base, pciaddr_t size,
675                       unsigned map_flags, void **addr)
676 {
677     if (base > 0x100000 || base + size > 0x100000)
678         return EINVAL;
679
680     if (!pci_sys->methods->map_legacy)
681         return ENOSYS;
682
683     return pci_sys->methods->map_legacy(dev, base, size, map_flags, addr);
684 }
685
686 /**
687  * Unmap the legacy memory space for the PCI domain containing \c dev.
688  *
689  * \param dev          Device whose memory region is to be unmapped.
690  * \param addr         Location of the mapped address.
691  * \param size         Size of the range to be unmapped.
692  *
693  * \returns
694  * Zero on success or an \c errno value on failure.
695  */
696 int
697 pci_device_unmap_legacy(struct pci_device *dev, void *addr, pciaddr_t size)
698 {
699     if (!pci_sys->methods->unmap_legacy)
700         return ENOSYS;
701
702     return pci_sys->methods->unmap_legacy(dev, addr, size);
703 }