common_capability.c revision 28d65773
14f5e7dd7Smrg/* 24f5e7dd7Smrg * (C) Copyright IBM Corporation 2006 34f5e7dd7Smrg * All Rights Reserved. 44f5e7dd7Smrg * 54f5e7dd7Smrg * Permission is hereby granted, free of charge, to any person obtaining a 64f5e7dd7Smrg * copy of this software and associated documentation files (the "Software"), 74f5e7dd7Smrg * to deal in the Software without restriction, including without limitation 84f5e7dd7Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub 94f5e7dd7Smrg * license, and/or sell copies of the Software, and to permit persons to whom 104f5e7dd7Smrg * the Software is furnished to do so, subject to the following conditions: 114f5e7dd7Smrg * 124f5e7dd7Smrg * The above copyright notice and this permission notice (including the next 134f5e7dd7Smrg * paragraph) shall be included in all copies or substantial portions of the 144f5e7dd7Smrg * Software. 154f5e7dd7Smrg * 164f5e7dd7Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 174f5e7dd7Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 184f5e7dd7Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 194f5e7dd7Smrg * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 204f5e7dd7Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 214f5e7dd7Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 224f5e7dd7Smrg * DEALINGS IN THE SOFTWARE. 234f5e7dd7Smrg */ 244f5e7dd7Smrg 254f5e7dd7Smrg/** 264f5e7dd7Smrg * \file common_capability.c 274f5e7dd7Smrg * Platform independent PCI capability related routines. 284f5e7dd7Smrg * 294f5e7dd7Smrg * In addition to including the interface glue for \c pci_device_get_agp_info, 304f5e7dd7Smrg * this file also contains a generic implementation of that function. 314f5e7dd7Smrg * 324f5e7dd7Smrg * \author Ian Romanick <idr@us.ibm.com> 334f5e7dd7Smrg */ 344f5e7dd7Smrg 354f5e7dd7Smrg#include <stdlib.h> 364f5e7dd7Smrg#include <stdio.h> 374f5e7dd7Smrg#include <errno.h> 384f5e7dd7Smrg 394f5e7dd7Smrg#include "pciaccess.h" 404f5e7dd7Smrg#include "pciaccess_private.h" 414f5e7dd7Smrg 424f5e7dd7Smrg/** 434f5e7dd7Smrg * Generic implementation of \c pci_system_methods::fill_capabilities. 444f5e7dd7Smrg * 454f5e7dd7Smrg * \param dev Device whose capability information is to be processed. 464f5e7dd7Smrg * 474f5e7dd7Smrg * \return 484f5e7dd7Smrg * Zero on success or an errno value on failure. 494f5e7dd7Smrg * 504f5e7dd7Smrg * \todo 514f5e7dd7Smrg * Once more than just the AGP capability is supported, the body of each of 524f5e7dd7Smrg * the cases in the capability processing loop should probably be broken out 534f5e7dd7Smrg * into its own function. 544f5e7dd7Smrg * 554f5e7dd7Smrg * \todo 564f5e7dd7Smrg * Once more than just the AGP capability is supported, some care will need 574f5e7dd7Smrg * to be taken in partial failure cases. If, say, the first capability is 584f5e7dd7Smrg * correctly processed but the second fails, the function would be re-called 594f5e7dd7Smrg * later to try again for the second capability. This could lead to memory 604f5e7dd7Smrg * leaks or other quirky behavior. 614f5e7dd7Smrg */ 624f5e7dd7Smrg_pci_hidden int 634f5e7dd7Smrgpci_fill_capabilities_generic( struct pci_device * dev ) 644f5e7dd7Smrg{ 654f5e7dd7Smrg struct pci_device_private * const dev_priv = 664f5e7dd7Smrg (struct pci_device_private *) dev; 674f5e7dd7Smrg int err; 684f5e7dd7Smrg uint16_t status; 694f5e7dd7Smrg uint8_t cap_offset; 704f5e7dd7Smrg 714f5e7dd7Smrg 724f5e7dd7Smrg err = pci_device_cfg_read_u16( dev, & status, 6 ); 734f5e7dd7Smrg if ( err ) { 744f5e7dd7Smrg return err; 754f5e7dd7Smrg } 764f5e7dd7Smrg 774f5e7dd7Smrg /* Are PCI capabilities supported by this device? 784f5e7dd7Smrg */ 794f5e7dd7Smrg if ( (status & 0x0010) == 0 ) { 804f5e7dd7Smrg return ENOSYS; 814f5e7dd7Smrg } 824f5e7dd7Smrg 834f5e7dd7Smrg err = pci_device_cfg_read_u8( dev, & cap_offset, 52 ); 844f5e7dd7Smrg if ( err ) { 854f5e7dd7Smrg return err; 864f5e7dd7Smrg } 874f5e7dd7Smrg 884f5e7dd7Smrg 894f5e7dd7Smrg /* Process each of the capabilities list in the PCI header. 904f5e7dd7Smrg */ 914f5e7dd7Smrg while ( cap_offset != 0 ) { 924f5e7dd7Smrg uint8_t cap_id; 934f5e7dd7Smrg uint8_t next_cap; 944f5e7dd7Smrg 954f5e7dd7Smrg err = pci_device_cfg_read_u8( dev, & cap_id, cap_offset ); 964f5e7dd7Smrg if ( err ) { 974f5e7dd7Smrg return err; 984f5e7dd7Smrg } 994f5e7dd7Smrg 1004f5e7dd7Smrg err = pci_device_cfg_read_u8( dev, & next_cap, cap_offset + 1 ); 1014f5e7dd7Smrg if ( err ) { 1024f5e7dd7Smrg return err; 1034f5e7dd7Smrg } 1044f5e7dd7Smrg 1054f5e7dd7Smrg switch ( cap_id ) { 1064f5e7dd7Smrg case 2: { 10728d65773Smrg struct pci_agp_info * agp_info; 1084f5e7dd7Smrg uint32_t agp_status; 1094f5e7dd7Smrg uint8_t agp_ver; 1104f5e7dd7Smrg 1114f5e7dd7Smrg 1124f5e7dd7Smrg err = pci_device_cfg_read_u8( dev, & agp_ver, cap_offset + 2 ); 1134f5e7dd7Smrg if ( err ) { 1144f5e7dd7Smrg return err; 1154f5e7dd7Smrg } 1164f5e7dd7Smrg 1174f5e7dd7Smrg err = pci_device_cfg_read_u32( dev, & agp_status, cap_offset + 4 ); 1184f5e7dd7Smrg if ( err ) { 1194f5e7dd7Smrg return err; 1204f5e7dd7Smrg } 1214f5e7dd7Smrg 12228d65773Smrg agp_info = calloc( 1, sizeof( struct pci_agp_info ) ); 12328d65773Smrg if ( agp_info == NULL ) { 12428d65773Smrg return ENOMEM; 12528d65773Smrg } 12628d65773Smrg 1274f5e7dd7Smrg agp_info->config_offset = cap_offset; 1284f5e7dd7Smrg 1294f5e7dd7Smrg agp_info->major_version = (agp_ver & 0x0f0) >> 4; 1304f5e7dd7Smrg agp_info->minor_version = (agp_ver & 0x00f); 1314f5e7dd7Smrg 1324f5e7dd7Smrg agp_info->rates = (agp_status & 0x07); 1334f5e7dd7Smrg 1344f5e7dd7Smrg /* If AGP3 is supported, then the meaning of the rates values 1354f5e7dd7Smrg * changes. 1364f5e7dd7Smrg */ 1374f5e7dd7Smrg if ( (agp_status & 0x08) != 0 ) { 1384f5e7dd7Smrg agp_info->rates <<= 2; 1394f5e7dd7Smrg } 1404f5e7dd7Smrg 1414f5e7dd7Smrg /* Some devices, notably motherboard chipsets, have the AGP3 1424f5e7dd7Smrg * capability set and the 4x bit set. This results in an 1434f5e7dd7Smrg * impossible 16x mode being listed as available. I'm not 100% 1444f5e7dd7Smrg * sure this is the right solution. 1454f5e7dd7Smrg */ 1464f5e7dd7Smrg agp_info->rates &= 0x0f; 1474f5e7dd7Smrg 1484f5e7dd7Smrg 1494f5e7dd7Smrg agp_info->fast_writes = (agp_status & 0x0010) != 0; 1504f5e7dd7Smrg agp_info->addr64 = (agp_status & 0x0020) != 0; 1514f5e7dd7Smrg agp_info->htrans = (agp_status & 0x0040) == 0; 1524f5e7dd7Smrg agp_info->gart64 = (agp_status & 0x0080) != 0; 1534f5e7dd7Smrg agp_info->coherent = (agp_status & 0x0100) != 0; 1544f5e7dd7Smrg agp_info->sideband = (agp_status & 0x0200) != 0; 1554f5e7dd7Smrg agp_info->isochronus = (agp_status & 0x10000) != 0; 1564f5e7dd7Smrg 1574f5e7dd7Smrg agp_info->async_req_size = 4 + (1 << ((agp_status & 0xe000) >> 13)); 1584f5e7dd7Smrg agp_info->calibration_cycle_timing = ((agp_status & 0x1c00) >> 10); 1594f5e7dd7Smrg agp_info->max_requests = 1 + ((agp_status & 0xff000000) >> 24); 1604f5e7dd7Smrg 1614f5e7dd7Smrg dev_priv->agp = agp_info; 1624f5e7dd7Smrg break; 1634f5e7dd7Smrg } 1644f5e7dd7Smrg 1654f5e7dd7Smrg /* No other capabilities are currently handled. 1664f5e7dd7Smrg */ 1674f5e7dd7Smrg default: 1684f5e7dd7Smrg printf( "Unknown cap 0x%02x @ 0x%02x\n", cap_id, cap_offset ); 1694f5e7dd7Smrg break; 1704f5e7dd7Smrg } 1714f5e7dd7Smrg 1724f5e7dd7Smrg cap_offset = next_cap; 1734f5e7dd7Smrg } 1744f5e7dd7Smrg 1754f5e7dd7Smrg return 0; 1764f5e7dd7Smrg} 1774f5e7dd7Smrg 1784f5e7dd7Smrg 1794f5e7dd7Smrg/** 1804f5e7dd7Smrg * Get AGP capability data for a device. 1814f5e7dd7Smrg */ 1824f5e7dd7Smrgconst struct pci_agp_info * 1834f5e7dd7Smrgpci_device_get_agp_info( struct pci_device * dev ) 1844f5e7dd7Smrg{ 1854f5e7dd7Smrg struct pci_device_private * dev_priv = (struct pci_device_private *) dev; 1864f5e7dd7Smrg 1874f5e7dd7Smrg if ( dev == NULL ) { 1884f5e7dd7Smrg return NULL; 1894f5e7dd7Smrg } 1904f5e7dd7Smrg 1914f5e7dd7Smrg if ( dev_priv->agp == NULL ) { 1924f5e7dd7Smrg (void) (*pci_sys->methods->fill_capabilities)( dev ); 1934f5e7dd7Smrg } 1944f5e7dd7Smrg 1954f5e7dd7Smrg return dev_priv->agp; 1964f5e7dd7Smrg} 197