8650efd504cdfc4d164c83bf98abd5f618177458
[platform/upstream/libpciaccess.git] / src / linux_devmem.c
1 /*
2  * (C) Copyright IBM Corporation 2007
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_devmem.c
27  * Access PCI subsystem using Linux's the old /dev/mem interface.
28  * 
29  * \note
30  * This is currently just a skeleton.  It only includes the /dev/mem based
31  * function for reading the device ROM.
32  *
33  * \author Ian Romanick <idr@us.ibm.com>
34  */
35
36 #define _GNU_SOURCE
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <sys/mman.h>
46 #include <dirent.h>
47 #include <errno.h>
48
49 #include "pciaccess.h"
50 #include "pciaccess_private.h"
51 #include "linux_devmem.h"
52
53 /**
54  * Read a device's expansion ROM using /dev/mem.
55  * 
56  * \note
57  * This function could probably be used, as-is, on other platforms that have
58  * a /dev/mem interface.
59  * 
60  * \bugs
61  * Before using the VGA special case code, this function should check that
62  * VGA access are routed to the device.  Right?
63  */
64 int pci_device_linux_devmem_read_rom(struct pci_device *dev, void *buffer)
65 {
66     struct pci_device_private *priv = (struct pci_device_private *) dev;
67     int fd;
68     int err = 0;
69     uint32_t rom_base_tmp;
70     pciaddr_t rom_base;
71     pciaddr_t rom_size;
72     int PCI_ROM;
73
74
75     /* Handle some special cases of legacy devices.
76      */
77     if (priv->base.rom_size == 0) {
78         /* VGA ROMs are supposed to be at 0xC0000.
79          */
80         if ((priv->base.device_class & 0x00ffff00) == 0x000030000) {
81             rom_base = 0x000C0000;
82             rom_size = 0x00010000;
83             PCI_ROM = 0;
84         }
85         else {
86             /* "Function not implemented."
87              */
88             return ENOSYS;
89         }
90     }
91     else {
92         rom_base = priv->rom_base;
93         rom_size = priv->base.rom_size;
94         PCI_ROM = 1;
95     }
96     
97
98
99     /* Enable the device's ROM.
100      */
101     if (PCI_ROM) {
102         err = pci_device_cfg_read_u32(& priv->base, & rom_base_tmp, 48);
103         if (err) {
104             return err;
105         }
106
107         if ((rom_base_tmp & 0x000000001) == 0) {
108             err = pci_device_cfg_write_u32(& priv->base, 
109                                            rom_base_tmp | 1, 48);
110             if (err) {
111                 return err;
112             }
113         }
114     }
115     
116     
117     /* Read the portion of /dev/mem that corresponds to the device's ROM.
118      */
119     fd = open("/dev/mem", O_RDONLY, 0);
120     if (fd < 0) {
121         err = errno;
122     }
123     else {
124         size_t bytes;
125
126         for (bytes = 0; bytes < priv->base.rom_size; /* empty */) {
127             const ssize_t got = pread(fd, buffer, rom_size - bytes, 
128                                       rom_base + bytes);
129             if (got == -1) {
130                 err = errno;
131                 break;
132             }
133
134             bytes += got;
135         }
136
137         close(fd);
138     }
139
140     
141     /* Disable the device's ROM.
142      */
143     if (PCI_ROM && ((rom_base_tmp & 0x000000001) == 0)) {
144         const int tmp_err = pci_device_cfg_write_u32(& priv->base,
145                                                      rom_base_tmp, 48);
146
147         /* Prefer to return the first error that occured.
148          */
149         if (err == 0) {
150             err = tmp_err;
151         }
152     }
153
154     return err;
155 }