a7d5c0557e68264deeaeb99460bebb6991ba67f7
[platform/upstream/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_WRITE) : PROT_READ;
80     dev->regions[ region ].memory = mmap( NULL, dev->regions[ region ].size,
81                                           prot, MAP_SHARED, fd,
82                                           dev->regions[ region ].base_addr);
83
84     if ( dev->regions[ region ].memory == MAP_FAILED ) {
85         dev->regions[ region ].memory = NULL;
86         err = errno;
87     }
88
89     close( fd );
90
91     return err;
92 }
93
94 /**
95  * Unmap the specified region.
96  *
97  * \param dev          Device whose memory region is to be unmapped.
98  * \param region       Region, on the range [0, 5], that is to be unmapped.
99  *
100  * \return
101  * Zero on success or an \c errno value on failure.
102  */
103 static int
104 pci_device_freebsd_unmap( struct pci_device * dev, unsigned region )
105 {
106     int err = 0;
107
108     if ( munmap( dev->regions[ region ].memory,
109                  dev->regions[ region ].size ) == -1) {
110         err = errno;
111     }
112
113     dev->regions[ region ].memory = NULL;
114
115     return err;
116 }
117
118 static int
119 pci_device_freebsd_read( struct pci_device * dev, void * data,
120                          pciaddr_t offset, pciaddr_t size,
121                          pciaddr_t * bytes_read )
122 {
123     struct pci_io io;
124
125     io.pi_sel.pc_bus = dev->bus;
126     io.pi_sel.pc_dev = dev->dev;
127     io.pi_sel.pc_func = dev->func;
128
129     *bytes_read = 0;
130     while ( size > 0 ) {
131         int toread = (size < 4) ? size : 4;
132
133         /* Only power of two allowed. */
134         if (toread == 3)
135             toread = 2;
136
137         io.pi_reg = offset;
138         io.pi_width = toread;
139
140         if ( ioctl( freebsd_pci_sys->pcidev, PCIOCREAD, &io ) < 0 ) 
141             return errno;
142
143         memcpy(data, &io.pi_data, toread );
144
145         offset += toread;
146         data = (char *)data + toread;
147         size -= toread;
148         *bytes_read += toread;
149     }
150
151     return 0;
152 }
153
154
155 static int
156 pci_device_freebsd_write( struct pci_device * dev, const void * data,
157                           pciaddr_t offset, pciaddr_t size,
158                           pciaddr_t * bytes_written )
159 {
160     struct pci_io io;
161
162     io.pi_sel.pc_bus = dev->bus;
163     io.pi_sel.pc_dev = dev->dev;
164     io.pi_sel.pc_func = dev->func;
165
166     *bytes_written = 0;
167     while ( size > 0 ) {
168         int towrite = (size < 4 ? size : 4);
169
170         io.pi_reg = offset;
171         io.pi_width = towrite;
172         memcpy( &io.pi_data, data, towrite );
173
174         if ( ioctl( freebsd_pci_sys->pcidev, PCIOCWRITE, &io ) < 0 ) 
175             return errno;
176
177         offset += towrite;
178         data = (char *)data + towrite;
179         size -= towrite;
180         *bytes_written += towrite;
181     }
182
183     return 0;
184 }
185
186 /** Returns the number of regions (base address registers) the device has */
187
188 static int
189 pci_device_freebsd_get_num_regions( struct pci_device * dev )
190 {
191     struct pci_device_private *priv = (struct pci_device_private *) dev;
192
193     switch (priv->header_type & 0x7f) {
194     case 0:
195         return 6;
196     case 1:
197         return 2;
198     case 2:
199         return 1;
200     default:
201         printf("unknown header type %02x\n", priv->header_type);
202         return 0;
203     }
204 }
205
206 /** Masks out the flag bigs of the base address register value */
207 static uint32_t
208 get_map_base( uint32_t val )
209 {
210     if (val & 0x01)
211         return val & ~0x03;
212     else
213         return val & ~0x0f;
214 }
215
216 /** Returns the size of a region based on the all-ones test value */
217 static int
218 get_test_val_size( uint32_t testval )
219 {
220     int size = 1;
221
222     if (testval == 0)
223         return 0;
224
225     /* Mask out the flag bits */
226     testval = get_map_base( testval );
227
228     while ((testval & 1) == 0) {
229         size <<= 1;
230         testval >>= 1;
231     }
232
233     return size;
234 }
235
236 /**
237  * Sets the address and size information for the region from config space
238  * registers.
239  *
240  * This would be much better provided by a kernel interface.
241  *
242  * \return 0 on success, or an errno value.
243  */
244 static int
245 pci_device_freebsd_get_region_info( struct pci_device * dev, int region,
246                                     int bar )
247 {
248     uint32_t addr, testval;
249     int err;
250
251     /* Get the base address */
252     err = pci_device_cfg_read_u32( dev, &addr, bar );
253     if (err != 0)
254         return err;
255
256     /* Test write all ones to the register, then restore it. */
257     err = pci_device_cfg_write_u32( dev, 0xffffffff, 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 }