xref: /libpciaccess/src/common_capability.c (revision e1a0240a)
15a04522aSIan Romanick /*
25a04522aSIan Romanick  * (C) Copyright IBM Corporation 2006
35a04522aSIan Romanick  * All Rights Reserved.
45a04522aSIan Romanick  *
55a04522aSIan Romanick  * Permission is hereby granted, free of charge, to any person obtaining a
65a04522aSIan Romanick  * copy of this software and associated documentation files (the "Software"),
75a04522aSIan Romanick  * to deal in the Software without restriction, including without limitation
85a04522aSIan Romanick  * on the rights to use, copy, modify, merge, publish, distribute, sub
95a04522aSIan Romanick  * license, and/or sell copies of the Software, and to permit persons to whom
105a04522aSIan Romanick  * the Software is furnished to do so, subject to the following conditions:
115a04522aSIan Romanick  *
125a04522aSIan Romanick  * The above copyright notice and this permission notice (including the next
135a04522aSIan Romanick  * paragraph) shall be included in all copies or substantial portions of the
145a04522aSIan Romanick  * Software.
155a04522aSIan Romanick  *
165a04522aSIan Romanick  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
175a04522aSIan Romanick  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
185a04522aSIan Romanick  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
195a04522aSIan Romanick  * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
205a04522aSIan Romanick  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
215a04522aSIan Romanick  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
225a04522aSIan Romanick  * DEALINGS IN THE SOFTWARE.
235a04522aSIan Romanick  */
245a04522aSIan Romanick 
255a04522aSIan Romanick /**
265a04522aSIan Romanick  * \file common_capability.c
275a04522aSIan Romanick  * Platform independent PCI capability related routines.
285a04522aSIan Romanick  *
295a04522aSIan Romanick  * In addition to including the interface glue for \c pci_device_get_agp_info,
305a04522aSIan Romanick  * this file also contains a generic implementation of that function.
315a04522aSIan Romanick  *
325a04522aSIan Romanick  * \author Ian Romanick <[email protected]>
335a04522aSIan Romanick  */
345a04522aSIan Romanick 
355a04522aSIan Romanick #include <stdlib.h>
365a04522aSIan Romanick #include <stdio.h>
375a04522aSIan Romanick #include <errno.h>
385a04522aSIan Romanick 
395a04522aSIan Romanick #include "pciaccess.h"
405a04522aSIan Romanick #include "pciaccess_private.h"
415a04522aSIan Romanick 
425a04522aSIan Romanick /**
435a04522aSIan Romanick  * Generic implementation of \c pci_system_methods::fill_capabilities.
445a04522aSIan Romanick  *
455a04522aSIan Romanick  * \param dev   Device whose capability information is to be processed.
465a04522aSIan Romanick  *
475a04522aSIan Romanick  * \return
485a04522aSIan Romanick  * Zero on success or an errno value on failure.
495a04522aSIan Romanick  *
505a04522aSIan Romanick  * \todo
515a04522aSIan Romanick  * Once more than just the AGP capability is supported, the body of each of
525a04522aSIan Romanick  * the cases in the capability processing loop should probably be broken out
535a04522aSIan Romanick  * into its own function.
545a04522aSIan Romanick  *
555a04522aSIan Romanick  * \todo
565a04522aSIan Romanick  * Once more than just the AGP capability is supported, some care will need
575a04522aSIan Romanick  * to be taken in partial failure cases.  If, say, the first capability is
585a04522aSIan Romanick  * correctly processed but the second fails, the function would be re-called
595a04522aSIan Romanick  * later to try again for the second capability.  This could lead to memory
605a04522aSIan Romanick  * leaks or other quirky behavior.
615a04522aSIan Romanick  */
62adc46f65SJulien Cristau _pci_hidden int
pci_fill_capabilities_generic(struct pci_device * dev)635a04522aSIan Romanick pci_fill_capabilities_generic( struct pci_device * dev )
645a04522aSIan Romanick {
655a04522aSIan Romanick     struct pci_device_private * const dev_priv =
665a04522aSIan Romanick       (struct pci_device_private *) dev;
675a04522aSIan Romanick     int       err;
685a04522aSIan Romanick     uint16_t  status;
695a04522aSIan Romanick     uint8_t   cap_offset;
705a04522aSIan Romanick 
715a04522aSIan Romanick 
725a04522aSIan Romanick     err = pci_device_cfg_read_u16( dev, & status, 6 );
735a04522aSIan Romanick     if ( err ) {
745a04522aSIan Romanick 	return err;
755a04522aSIan Romanick     }
765a04522aSIan Romanick 
775a04522aSIan Romanick     /* Are PCI capabilities supported by this device?
785a04522aSIan Romanick      */
795a04522aSIan Romanick     if ( (status & 0x0010) == 0 ) {
805a04522aSIan Romanick 	return ENOSYS;
815a04522aSIan Romanick     }
825a04522aSIan Romanick 
835a04522aSIan Romanick     err = pci_device_cfg_read_u8( dev, & cap_offset, 52 );
845a04522aSIan Romanick     if ( err ) {
855a04522aSIan Romanick 	return err;
865a04522aSIan Romanick     }
875a04522aSIan Romanick 
885a04522aSIan Romanick 
895a04522aSIan Romanick     /* Process each of the capabilities list in the PCI header.
905a04522aSIan Romanick      */
915a04522aSIan Romanick     while ( cap_offset != 0 ) {
925a04522aSIan Romanick 	uint8_t cap_id;
935a04522aSIan Romanick 	uint8_t next_cap;
945a04522aSIan Romanick 
955a04522aSIan Romanick 	err = pci_device_cfg_read_u8( dev, & cap_id, cap_offset );
965a04522aSIan Romanick 	if ( err ) {
975a04522aSIan Romanick 	    return err;
985a04522aSIan Romanick 	}
995a04522aSIan Romanick 
1005a04522aSIan Romanick 	err = pci_device_cfg_read_u8( dev, & next_cap, cap_offset + 1 );
1015a04522aSIan Romanick 	if ( err ) {
1025a04522aSIan Romanick 	    return err;
1035a04522aSIan Romanick 	}
1045a04522aSIan Romanick 
1055a04522aSIan Romanick 	switch ( cap_id ) {
1065a04522aSIan Romanick 	case 2: {
107*fa7cca61SAlan Coopersmith 	    struct pci_agp_info * agp_info;
1085a04522aSIan Romanick 	    uint32_t agp_status;
1095a04522aSIan Romanick 	    uint8_t agp_ver;
1105a04522aSIan Romanick 
1115a04522aSIan Romanick 
1125a04522aSIan Romanick 	    err = pci_device_cfg_read_u8( dev, & agp_ver, cap_offset + 2 );
1135a04522aSIan Romanick 	    if ( err ) {
1145a04522aSIan Romanick 		return err;
1155a04522aSIan Romanick 	    }
1165a04522aSIan Romanick 
1175a04522aSIan Romanick 	    err = pci_device_cfg_read_u32( dev, & agp_status, cap_offset + 4 );
1185a04522aSIan Romanick 	    if ( err ) {
1195a04522aSIan Romanick 		return err;
1205a04522aSIan Romanick 	    }
1215a04522aSIan Romanick 
122*fa7cca61SAlan Coopersmith 	    agp_info = calloc( 1, sizeof( struct pci_agp_info ) );
123*fa7cca61SAlan Coopersmith 	    if ( agp_info == NULL ) {
124*fa7cca61SAlan Coopersmith 		return ENOMEM;
125*fa7cca61SAlan Coopersmith 	    }
126*fa7cca61SAlan Coopersmith 
1275a04522aSIan Romanick 	    agp_info->config_offset = cap_offset;
1285a04522aSIan Romanick 
1295a04522aSIan Romanick 	    agp_info->major_version = (agp_ver & 0x0f0) >> 4;
1305a04522aSIan Romanick 	    agp_info->minor_version = (agp_ver & 0x00f);
1315a04522aSIan Romanick 
1325a04522aSIan Romanick 	    agp_info->rates = (agp_status & 0x07);
1335a04522aSIan Romanick 
1345a04522aSIan Romanick 	    /* If AGP3 is supported, then the meaning of the rates values
1355a04522aSIan Romanick 	     * changes.
1365a04522aSIan Romanick 	     */
1375a04522aSIan Romanick 	    if ( (agp_status & 0x08) != 0 ) {
1385a04522aSIan Romanick 		agp_info->rates <<= 2;
1395a04522aSIan Romanick 	    }
1405a04522aSIan Romanick 
1415a04522aSIan Romanick 	    /* Some devices, notably motherboard chipsets, have the AGP3
1425a04522aSIan Romanick 	     * capability set and the 4x bit set.  This results in an
1435a04522aSIan Romanick 	     * impossible 16x mode being listed as available.  I'm not 100%
1445a04522aSIan Romanick 	     * sure this is the right solution.
1455a04522aSIan Romanick 	     */
1465a04522aSIan Romanick 	    agp_info->rates &= 0x0f;
1475a04522aSIan Romanick 
1485a04522aSIan Romanick 
1495a04522aSIan Romanick 	    agp_info->fast_writes = (agp_status & 0x0010) != 0;
1505a04522aSIan Romanick 	    agp_info->addr64 =      (agp_status & 0x0020) != 0;
1515a04522aSIan Romanick 	    agp_info->htrans =      (agp_status & 0x0040) == 0;
1525a04522aSIan Romanick 	    agp_info->gart64 =      (agp_status & 0x0080) != 0;
1535a04522aSIan Romanick 	    agp_info->coherent =    (agp_status & 0x0100) != 0;
1545a04522aSIan Romanick 	    agp_info->sideband =    (agp_status & 0x0200) != 0;
1555a04522aSIan Romanick 	    agp_info->isochronus =  (agp_status & 0x10000) != 0;
1565a04522aSIan Romanick 
1575a04522aSIan Romanick 	    agp_info->async_req_size = 4 + (1 << ((agp_status & 0xe000) >> 13));
1585a04522aSIan Romanick 	    agp_info->calibration_cycle_timing = ((agp_status & 0x1c00) >> 10);
1595a04522aSIan Romanick 	    agp_info->max_requests = 1 + ((agp_status & 0xff000000) >> 24);
1605a04522aSIan Romanick 
1615a04522aSIan Romanick 	    dev_priv->agp = agp_info;
1625a04522aSIan Romanick 	    break;
1635a04522aSIan Romanick 	}
1645a04522aSIan Romanick 
1655a04522aSIan Romanick 	/* No other capabilities are currently handled.
1665a04522aSIan Romanick 	 */
1675a04522aSIan Romanick 	default:
1685a04522aSIan Romanick 	    printf( "Unknown cap 0x%02x @ 0x%02x\n", cap_id, cap_offset );
1695a04522aSIan Romanick 	    break;
1705a04522aSIan Romanick 	}
1715a04522aSIan Romanick 
1725a04522aSIan Romanick 	cap_offset = next_cap;
1735a04522aSIan Romanick     }
1745a04522aSIan Romanick 
1755a04522aSIan Romanick     return 0;
1765a04522aSIan Romanick }
1775a04522aSIan Romanick 
1785a04522aSIan Romanick 
1795a04522aSIan Romanick /**
1805a04522aSIan Romanick  * Get AGP capability data for a device.
1815a04522aSIan Romanick  */
1825a04522aSIan Romanick const struct pci_agp_info *
pci_device_get_agp_info(struct pci_device * dev)1835a04522aSIan Romanick pci_device_get_agp_info( struct pci_device * dev )
1845a04522aSIan Romanick {
1855a04522aSIan Romanick     struct pci_device_private * dev_priv = (struct pci_device_private *) dev;
1865a04522aSIan Romanick 
1875a04522aSIan Romanick     if ( dev == NULL ) {
1885a04522aSIan Romanick 	return NULL;
1895a04522aSIan Romanick     }
1905a04522aSIan Romanick 
1915a04522aSIan Romanick     if ( dev_priv->agp == NULL ) {
1925a04522aSIan Romanick 	(void) (*pci_sys->methods->fill_capabilities)( dev );
1935a04522aSIan Romanick     }
1945a04522aSIan Romanick 
1955a04522aSIan Romanick     return dev_priv->agp;
1965a04522aSIan Romanick }
197