OSDN Git Service

Add support for getting region information on FreeBSD.
[android-x86/external-libpciaccess.git] / src / freebsd_pci.c
1 /*
2  * (C) Copyright Eric Anholt 2006
3  * (C) Copyright IBM Corporation 2006
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * on the rights to use, copy, modify, merge, publish, distribute, sub
10  * license, and/or sell copies of the Software, and to permit persons to whom
11  * the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
20  * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25
26 /**
27  * \file freebsd_pci.c
28  *
29  * Access the kernel PCI support using /dev/pci's ioctl and mmap interface.
30  *
31  * \author Eric Anholt <eric@anholt.net>
32  */
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <sys/types.h>
41 #include <sys/pciio.h>
42 #include <sys/mman.h>
43
44 #include "pciaccess.h"
45 #include "pciaccess_private.h"
46
47 /**
48  * FreeBSD private pci_system structure that extends the base pci_system
49  * structure.
50  *
51  * It is initialized once and used as a global, just as pci_system is used.
52  */
53 struct freebsd_pci_system {
54     struct pci_system pci_sys;
55
56     int pcidev; /**< fd for /dev/pci */
57 } *freebsd_pci_sys;
58
59 /**
60  * Map a memory region for a device using /dev/mem.
61  *
62  * \param dev          Device whose memory region is to be mapped.
63  * \param region       Region, on the range [0, 5], that is to be mapped.
64  * \param write_enable Map for writing (non-zero).
65  *
66  * \return
67  * Zero on success or an \c errno value on failure.
68  */
69 static int
70 pci_device_freebsd_map( struct pci_device *dev, unsigned region,
71                         int write_enable )
72 {
73     int fd, err = 0, prot;
74
75     fd = open( "/dev/mem", write_enable ? O_RDWR : O_RDONLY );
76     if ( fd == -1 )
77         return errno;
78
79     prot = write_enable ? PROT_READ : (PROT_READ | PROT_WRITE);
80     dev->regions[ region ].memory = mmap( NULL, dev->regions[ region ].size,
81                                           prot, MAP_SHARED, fd, 0 );
82
83     if ( dev->regions[ region ].memory == MAP_FAILED ) {
84         dev->regions[ region ].memory = NULL;
85         err = errno;
86     }
87
88     close( fd );
89
90     return err;
91 }
92
93 /**
94  * Unmap the specified region.
95  *
96  * \param dev          Device whose memory region is to be unmapped.
97  * \param region       Region, on the range [0, 5], that is to be unmapped.
98  *
99  * \return
100  * Zero on success or an \c errno value on failure.
101  */
102 static int
103 pci_device_freebsd_unmap( struct pci_device * dev, unsigned region )
104 {
105     int err = 0;
106
107     if ( munmap( dev->regions[ region ].memory,
108                  dev->regions[ region ].size ) == -1) {
109         err = errno;
110     }
111
112     dev->regions[ region ].memory = NULL;
113
114     return err;
115 }
116
117 static int
118 pci_device_freebsd_read( struct pci_device * dev, void * data,
119                          pciaddr_t offset, pciaddr_t size,
120                          pciaddr_t * bytes_read )
121 {
122     struct pci_io io;
123
124     io.pi_sel.pc_bus = dev->bus;
125     io.pi_sel.pc_dev = dev->dev;
126     io.pi_sel.pc_func = dev->func;
127
128     *bytes_read = 0;
129     while ( size > 0 ) {
130         int toread = (size < 4) ? size : 4;
131
132         /* Only power of two allowed. */
133         if (toread == 3)
134             toread = 2;
135
136         io.pi_reg = offset;
137         io.pi_width = toread;
138
139         if ( ioctl( freebsd_pci_sys->pcidev, PCIOCREAD, &io ) < 0 ) 
140             return errno;
141
142         memcpy(data, &io.pi_data, toread );
143
144         offset += toread;
145         data = (char *)data + toread;
146         size -= toread;
147         *bytes_read += toread;
148     }
149
150     return 0;
151 }
152
153
154 static int
155 pci_device_freebsd_write( struct pci_device * dev, const void * data,
156                           pciaddr_t offset, pciaddr_t size,
157                           pciaddr_t * bytes_written )
158 {
159     struct pci_io io;
160
161     io.pi_sel.pc_bus = dev->bus;
162     io.pi_sel.pc_dev = dev->dev;
163     io.pi_sel.pc_func = dev->func;
164
165     *bytes_written = 0;
166     while ( size > 0 ) {
167         int towrite = (size < 4 ? size : 4);
168
169         io.pi_reg = offset;
170         io.pi_width = towrite;
171         memcpy( &io.pi_data, data, towrite );
172
173         if ( ioctl( freebsd_pci_sys->pcidev, PCIOCWRITE, &io ) < 0 ) 
174             return errno;
175
176         offset += towrite;
177         data = (char *)data + towrite;
178         size -= towrite;
179         *bytes_written += towrite;
180     }
181
182     return 0;
183 }
184
185 /** Returns the number of regions (base address registers) the device has */
186
187 static int
188 pci_device_freebsd_get_num_regions( struct pci_device * dev )
189 {
190     struct pci_device_private *priv = (struct pci_device_private *) dev;
191
192     switch (priv->header_type & 0x7f) {
193     case 0:
194         return 6;
195     case 1:
196         return 2;
197     case 2:
198         return 1;
199     default:
200         printf("unknown header type %02x\n", priv->header_type);
201         return 0;
202     }
203 }
204
205 /** Masks out the flag bigs of the base address register value */
206 static uint32_t
207 get_map_base( uint32_t val )
208 {
209     if (val & 0x01)
210         return val & ~0x03;
211     else
212         return val & ~0x0f;
213 }
214
215 /** Returns the size of a region based on the all-ones test value */
216 static int
217 get_test_val_size( uint32_t testval )
218 {
219     int size = 1;
220
221     if (testval == 0)
222         return 0;
223
224     /* Mask out the flag bits */
225     testval = get_map_base( testval );
226
227     while ((testval & 1) == 0) {
228         size <<= 1;
229         testval >>= 1;
230     }
231
232     return size;
233 }
234
235 /**
236  * Sets the address and size information for the region from config space
237  * registers.
238  *
239  * This would be much better provided by a kernel interface.
240  *
241  * \return 0 on success, or an errno value.
242  */
243 static int
244 pci_device_freebsd_get_region_info( struct pci_device * dev, int region,
245                                     int bar )
246 {
247     uint32_t addr, testval, temp;
248     int err;
249
250     /* Get the base address */
251     err = pci_device_cfg_read_u32( dev, &addr, bar );
252     if (err != 0)
253         return err;
254
255     /* Test write all ones to the register, then restore it. */
256     temp = 0xffffffff;
257     err = pci_device_cfg_write_u32( dev, &temp, bar );
258     if (err != 0)
259         return err;
260     pci_device_cfg_read_u32( dev, &testval, bar );
261     err = pci_device_cfg_write_u32( dev, &addr, bar );
262
263     if (addr & 0x01)
264         dev->regions[region].is_IO = 1;
265     if (addr & 0x04)
266         dev->regions[region].is_64 = 1;
267     if (addr & 0x08)
268         dev->regions[region].is_prefetchable = 1;
269
270     /* Set the size */
271     dev->regions[region].size = get_test_val_size( testval );
272
273     /* Set the base address value */
274     if (dev->regions[region].is_64) {
275         uint32_t top;
276
277         err = pci_device_cfg_read_u32( dev, &top, bar + 4 );
278         if (err != 0)
279             return err;
280
281         dev->regions[region].base_addr = ((uint64_t)top << 32) |
282                                           get_map_base(addr);
283     } else {
284         dev->regions[region].base_addr = get_map_base(addr);
285     }
286
287     return 0;
288 }
289
290 static int
291 pci_device_freebsd_probe( struct pci_device * dev )
292 {
293     struct pci_device_private *priv = (struct pci_device_private *) dev;
294     uint8_t irq;
295     int err, i, bar;
296
297     /* Many of the fields were filled in during initial device enumeration.
298      * At this point, we need to fill in regions, rom_size, and irq.
299      */
300
301     err = pci_device_cfg_read_u8( dev, &irq, 60 );
302     if (err)
303         return errno;
304     dev->irq = irq;
305
306     err = pci_device_cfg_read_u8( dev, &priv->header_type, 0x0e );
307     if (err)
308         return errno;
309
310     bar = 0x10;
311     for (i = 0; i < pci_device_freebsd_get_num_regions( dev ); i++) {
312         pci_device_freebsd_get_region_info( dev, i, bar );
313         if (dev->regions[i].is_64)
314             bar += 0x08;
315         else
316             bar += 0x04;
317     }
318
319     return 0;
320 }
321
322 static const struct pci_system_methods freebsd_pci_methods = {
323     .destroy = NULL, /* XXX: free memory */
324     .destroy_device = NULL,
325     .read_rom = NULL, /* XXX: Fill me in */
326     .probe = pci_device_freebsd_probe,
327     .map = pci_device_freebsd_map,
328     .unmap = pci_device_freebsd_unmap,
329     .read = pci_device_freebsd_read,
330     .write = pci_device_freebsd_write,
331     .fill_capabilities = pci_fill_capabilities_generic,
332 };
333
334 /**
335  * Attempt to access the FreeBSD PCI interface.
336  */
337 int
338 pci_system_freebsd_create( void )
339 {
340     struct pci_conf_io pciconfio;
341     struct pci_conf pciconf[255];
342     int pcidev;
343     int i;
344
345     /* Try to open the PCI device */
346     pcidev = open( "/dev/pci", O_RDWR );
347     if ( pcidev == -1 )
348         return ENXIO;
349
350     freebsd_pci_sys = calloc( 1, sizeof( struct freebsd_pci_system ) );
351     if ( freebsd_pci_sys == NULL ) {
352         close( pcidev );
353         return ENOMEM;
354     }
355     pci_sys = &freebsd_pci_sys->pci_sys;
356
357     pci_sys->methods = & freebsd_pci_methods;
358     freebsd_pci_sys->pcidev = pcidev;
359
360     /* Probe the list of devices known by the system */
361     bzero( &pciconfio, sizeof( struct pci_conf_io ) );
362     pciconfio.match_buf_len = sizeof(pciconf);
363     pciconfio.matches = pciconf;
364
365     if ( ioctl( pcidev, PCIOCGETCONF, &pciconfio ) == -1) {
366         free( pci_sys );
367         close( pcidev );
368         return errno;
369     }
370
371     if (pciconfio.status == PCI_GETCONF_ERROR ) {
372         free( pci_sys );
373         close( pcidev );
374         return EINVAL;
375     }
376
377     /* Translate the list of devices into pciaccess's format. */
378     pci_sys->num_devices = pciconfio.num_matches;
379     pci_sys->devices = calloc( pciconfio.num_matches,
380                                sizeof( struct pci_device_private ) );
381
382     for ( i = 0; i < pciconfio.num_matches; i++ ) {
383         struct pci_conf *p = &pciconf[ i ];
384
385         pci_sys->devices[ i ].base.domain = 0; /* XXX */
386         pci_sys->devices[ i ].base.bus = p->pc_sel.pc_bus;
387         pci_sys->devices[ i ].base.dev = p->pc_sel.pc_dev;
388         pci_sys->devices[ i ].base.func = p->pc_sel.pc_func;
389         pci_sys->devices[ i ].base.vendor_id = p->pc_vendor;
390         pci_sys->devices[ i ].base.device_id = p->pc_device;
391         pci_sys->devices[ i ].base.subvendor_id = p->pc_subvendor;
392         pci_sys->devices[ i ].base.device_class = (uint32_t)p->pc_class << 16 |
393             (uint32_t)p->pc_subclass << 8 | (uint32_t)p->pc_progif;
394     }
395
396     return 0;
397 }