2 * (C) Copyright Eric Anholt 2006
3 * (C) Copyright IBM Corporation 2006
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:
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
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.
29 * Access the kernel PCI support using /dev/pci's ioctl and mmap interface.
31 * \author Eric Anholt <eric@anholt.net>
40 #include <sys/types.h>
41 #include <sys/pciio.h>
44 #include "pciaccess.h"
45 #include "pciaccess_private.h"
48 * FreeBSD private pci_system structure that extends the base pci_system
51 * It is initialized once and used as a global, just as pci_system is used.
53 struct freebsd_pci_system {
54 struct pci_system pci_sys;
56 int pcidev; /**< fd for /dev/pci */
60 * Map a memory region for a device using /dev/mem.
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).
67 * Zero on success or an \c errno value on failure.
70 pci_device_freebsd_map( struct pci_device *dev, unsigned region,
73 int fd, err = 0, prot;
75 fd = open( "/dev/mem", write_enable ? O_RDWR : O_RDONLY );
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 );
83 if ( dev->regions[ region ].memory == MAP_FAILED ) {
84 dev->regions[ region ].memory = NULL;
94 * Unmap the specified region.
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.
100 * Zero on success or an \c errno value on failure.
103 pci_device_freebsd_unmap( struct pci_device * dev, unsigned region )
107 if ( munmap( dev->regions[ region ].memory,
108 dev->regions[ region ].size ) == -1) {
112 dev->regions[ region ].memory = NULL;
118 pci_device_freebsd_read( struct pci_device * dev, void * data,
119 pciaddr_t offset, pciaddr_t size,
120 pciaddr_t * bytes_read )
124 io.pi_sel.pc_bus = dev->bus;
125 io.pi_sel.pc_dev = dev->dev;
126 io.pi_sel.pc_func = dev->func;
130 int toread = (size < 4) ? size : 4;
132 /* Only power of two allowed. */
137 io.pi_width = toread;
139 if ( ioctl( freebsd_pci_sys->pcidev, PCIOCREAD, &io ) < 0 )
142 memcpy(data, &io.pi_data, toread );
145 data = (char *)data + toread;
147 *bytes_read += toread;
155 pci_device_freebsd_write( struct pci_device * dev, const void * data,
156 pciaddr_t offset, pciaddr_t size,
157 pciaddr_t * bytes_written )
161 io.pi_sel.pc_bus = dev->bus;
162 io.pi_sel.pc_dev = dev->dev;
163 io.pi_sel.pc_func = dev->func;
167 int towrite = (size < 4 ? size : 4);
170 io.pi_width = towrite;
171 memcpy( &io.pi_data, data, towrite );
173 if ( ioctl( freebsd_pci_sys->pcidev, PCIOCWRITE, &io ) < 0 )
177 data = (char *)data + towrite;
179 *bytes_written += towrite;
185 /** Returns the number of regions (base address registers) the device has */
188 pci_device_freebsd_get_num_regions( struct pci_device * dev )
190 struct pci_device_private *priv = (struct pci_device_private *) dev;
192 switch (priv->header_type & 0x7f) {
200 printf("unknown header type %02x\n", priv->header_type);
205 /** Masks out the flag bigs of the base address register value */
207 get_map_base( uint32_t val )
215 /** Returns the size of a region based on the all-ones test value */
217 get_test_val_size( uint32_t testval )
224 /* Mask out the flag bits */
225 testval = get_map_base( testval );
227 while ((testval & 1) == 0) {
236 * Sets the address and size information for the region from config space
239 * This would be much better provided by a kernel interface.
241 * \return 0 on success, or an errno value.
244 pci_device_freebsd_get_region_info( struct pci_device * dev, int region,
247 uint32_t addr, testval;
250 /* Get the base address */
251 err = pci_device_cfg_read_u32( dev, &addr, bar );
255 /* Test write all ones to the register, then restore it. */
256 err = pci_device_cfg_write_u32( dev, 0xffffffff, bar );
259 pci_device_cfg_read_u32( dev, &testval, bar );
260 err = pci_device_cfg_write_u32( dev, addr, bar );
263 dev->regions[region].is_IO = 1;
265 dev->regions[region].is_64 = 1;
267 dev->regions[region].is_prefetchable = 1;
270 dev->regions[region].size = get_test_val_size( testval );
272 /* Set the base address value */
273 if (dev->regions[region].is_64) {
276 err = pci_device_cfg_read_u32( dev, &top, bar + 4 );
280 dev->regions[region].base_addr = ((uint64_t)top << 32) |
283 dev->regions[region].base_addr = get_map_base(addr);
290 pci_device_freebsd_probe( struct pci_device * dev )
292 struct pci_device_private *priv = (struct pci_device_private *) dev;
296 /* Many of the fields were filled in during initial device enumeration.
297 * At this point, we need to fill in regions, rom_size, and irq.
300 err = pci_device_cfg_read_u8( dev, &irq, 60 );
305 err = pci_device_cfg_read_u8( dev, &priv->header_type, 0x0e );
310 for (i = 0; i < pci_device_freebsd_get_num_regions( dev ); i++) {
311 pci_device_freebsd_get_region_info( dev, i, bar );
312 if (dev->regions[i].is_64)
321 static const struct pci_system_methods freebsd_pci_methods = {
322 .destroy = NULL, /* XXX: free memory */
323 .destroy_device = NULL,
324 .read_rom = NULL, /* XXX: Fill me in */
325 .probe = pci_device_freebsd_probe,
326 .map = pci_device_freebsd_map,
327 .unmap = pci_device_freebsd_unmap,
328 .read = pci_device_freebsd_read,
329 .write = pci_device_freebsd_write,
330 .fill_capabilities = pci_fill_capabilities_generic,
334 * Attempt to access the FreeBSD PCI interface.
337 pci_system_freebsd_create( void )
339 struct pci_conf_io pciconfio;
340 struct pci_conf pciconf[255];
344 /* Try to open the PCI device */
345 pcidev = open( "/dev/pci", O_RDWR );
349 freebsd_pci_sys = calloc( 1, sizeof( struct freebsd_pci_system ) );
350 if ( freebsd_pci_sys == NULL ) {
354 pci_sys = &freebsd_pci_sys->pci_sys;
356 pci_sys->methods = & freebsd_pci_methods;
357 freebsd_pci_sys->pcidev = pcidev;
359 /* Probe the list of devices known by the system */
360 bzero( &pciconfio, sizeof( struct pci_conf_io ) );
361 pciconfio.match_buf_len = sizeof(pciconf);
362 pciconfio.matches = pciconf;
364 if ( ioctl( pcidev, PCIOCGETCONF, &pciconfio ) == -1) {
370 if (pciconfio.status == PCI_GETCONF_ERROR ) {
376 /* Translate the list of devices into pciaccess's format. */
377 pci_sys->num_devices = pciconfio.num_matches;
378 pci_sys->devices = calloc( pciconfio.num_matches,
379 sizeof( struct pci_device_private ) );
381 for ( i = 0; i < pciconfio.num_matches; i++ ) {
382 struct pci_conf *p = &pciconf[ i ];
384 pci_sys->devices[ i ].base.domain = 0; /* XXX */
385 pci_sys->devices[ i ].base.bus = p->pc_sel.pc_bus;
386 pci_sys->devices[ i ].base.dev = p->pc_sel.pc_dev;
387 pci_sys->devices[ i ].base.func = p->pc_sel.pc_func;
388 pci_sys->devices[ i ].base.vendor_id = p->pc_vendor;
389 pci_sys->devices[ i ].base.device_id = p->pc_device;
390 pci_sys->devices[ i ].base.subvendor_id = p->pc_subvendor;
391 pci_sys->devices[ i ].base.device_class = (uint32_t)p->pc_class << 16 |
392 (uint32_t)p->pc_subclass << 8 | (uint32_t)p->pc_progif;