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.
28cad31331Smrg *
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 */
3449310723Smrg#ifdef HAVE_CONFIG_H
3549310723Smrg#include "config.h"
3649310723Smrg#endif
374f5e7dd7Smrg
384f5e7dd7Smrg#include <stdlib.h>
394f5e7dd7Smrg#include <stdio.h>
404f5e7dd7Smrg#include <errno.h>
414f5e7dd7Smrg
424f5e7dd7Smrg#include "pciaccess.h"
434f5e7dd7Smrg#include "pciaccess_private.h"
444f5e7dd7Smrg
454f5e7dd7Smrg/**
464f5e7dd7Smrg * Generic implementation of \c pci_system_methods::fill_capabilities.
474f5e7dd7Smrg *
484f5e7dd7Smrg * \param dev   Device whose capability information is to be processed.
494f5e7dd7Smrg *
504f5e7dd7Smrg * \return
514f5e7dd7Smrg * Zero on success or an errno value on failure.
524f5e7dd7Smrg *
534f5e7dd7Smrg * \todo
544f5e7dd7Smrg * Once more than just the AGP capability is supported, the body of each of
554f5e7dd7Smrg * the cases in the capability processing loop should probably be broken out
564f5e7dd7Smrg * into its own function.
57cad31331Smrg *
584f5e7dd7Smrg * \todo
594f5e7dd7Smrg * Once more than just the AGP capability is supported, some care will need
604f5e7dd7Smrg * to be taken in partial failure cases.  If, say, the first capability is
614f5e7dd7Smrg * correctly processed but the second fails, the function would be re-called
624f5e7dd7Smrg * later to try again for the second capability.  This could lead to memory
634f5e7dd7Smrg * leaks or other quirky behavior.
644f5e7dd7Smrg */
654f5e7dd7Smrg_pci_hidden int
664f5e7dd7Smrgpci_fill_capabilities_generic( struct pci_device * dev )
674f5e7dd7Smrg{
684f5e7dd7Smrg    struct pci_device_private * const dev_priv =
694f5e7dd7Smrg      (struct pci_device_private *) dev;
704f5e7dd7Smrg    int       err;
714f5e7dd7Smrg    uint16_t  status;
724f5e7dd7Smrg    uint8_t   cap_offset;
734f5e7dd7Smrg
744f5e7dd7Smrg
754f5e7dd7Smrg    err = pci_device_cfg_read_u16( dev, & status, 6 );
764f5e7dd7Smrg    if ( err ) {
774f5e7dd7Smrg	return err;
784f5e7dd7Smrg    }
794f5e7dd7Smrg
804f5e7dd7Smrg    /* Are PCI capabilities supported by this device?
814f5e7dd7Smrg     */
824f5e7dd7Smrg    if ( (status & 0x0010) == 0 ) {
834f5e7dd7Smrg	return ENOSYS;
844f5e7dd7Smrg    }
85cad31331Smrg
864f5e7dd7Smrg    err = pci_device_cfg_read_u8( dev, & cap_offset, 52 );
874f5e7dd7Smrg    if ( err ) {
884f5e7dd7Smrg	return err;
894f5e7dd7Smrg    }
904f5e7dd7Smrg
914f5e7dd7Smrg
924f5e7dd7Smrg    /* Process each of the capabilities list in the PCI header.
934f5e7dd7Smrg     */
944f5e7dd7Smrg    while ( cap_offset != 0 ) {
954f5e7dd7Smrg	uint8_t cap_id;
964f5e7dd7Smrg	uint8_t next_cap;
974f5e7dd7Smrg
984f5e7dd7Smrg	err = pci_device_cfg_read_u8( dev, & cap_id, cap_offset );
994f5e7dd7Smrg	if ( err ) {
1004f5e7dd7Smrg	    return err;
1014f5e7dd7Smrg	}
1024f5e7dd7Smrg
1034f5e7dd7Smrg	err = pci_device_cfg_read_u8( dev, & next_cap, cap_offset + 1 );
1044f5e7dd7Smrg	if ( err ) {
1054f5e7dd7Smrg	    return err;
1064f5e7dd7Smrg	}
107cad31331Smrg
1084f5e7dd7Smrg	switch ( cap_id ) {
1094f5e7dd7Smrg	case 2: {
11028d65773Smrg	    struct pci_agp_info * agp_info;
1114f5e7dd7Smrg	    uint32_t agp_status;
1124f5e7dd7Smrg	    uint8_t agp_ver;
1134f5e7dd7Smrg
1144f5e7dd7Smrg
1154f5e7dd7Smrg	    err = pci_device_cfg_read_u8( dev, & agp_ver, cap_offset + 2 );
1164f5e7dd7Smrg	    if ( err ) {
1174f5e7dd7Smrg		return err;
1184f5e7dd7Smrg	    }
1194f5e7dd7Smrg
1204f5e7dd7Smrg	    err = pci_device_cfg_read_u32( dev, & agp_status, cap_offset + 4 );
1214f5e7dd7Smrg	    if ( err ) {
1224f5e7dd7Smrg		return err;
1234f5e7dd7Smrg	    }
1244f5e7dd7Smrg
12528d65773Smrg	    agp_info = calloc( 1, sizeof( struct pci_agp_info ) );
12628d65773Smrg	    if ( agp_info == NULL ) {
12728d65773Smrg		return ENOMEM;
12828d65773Smrg	    }
12928d65773Smrg
1304f5e7dd7Smrg	    agp_info->config_offset = cap_offset;
1314f5e7dd7Smrg
1324f5e7dd7Smrg	    agp_info->major_version = (agp_ver & 0x0f0) >> 4;
1334f5e7dd7Smrg	    agp_info->minor_version = (agp_ver & 0x00f);
1344f5e7dd7Smrg
1354f5e7dd7Smrg	    agp_info->rates = (agp_status & 0x07);
1364f5e7dd7Smrg
1374f5e7dd7Smrg	    /* If AGP3 is supported, then the meaning of the rates values
1384f5e7dd7Smrg	     * changes.
1394f5e7dd7Smrg	     */
1404f5e7dd7Smrg	    if ( (agp_status & 0x08) != 0 ) {
1414f5e7dd7Smrg		agp_info->rates <<= 2;
1424f5e7dd7Smrg	    }
1434f5e7dd7Smrg
1444f5e7dd7Smrg	    /* Some devices, notably motherboard chipsets, have the AGP3
1454f5e7dd7Smrg	     * capability set and the 4x bit set.  This results in an
1464f5e7dd7Smrg	     * impossible 16x mode being listed as available.  I'm not 100%
1474f5e7dd7Smrg	     * sure this is the right solution.
1484f5e7dd7Smrg	     */
1494f5e7dd7Smrg	    agp_info->rates &= 0x0f;
1504f5e7dd7Smrg
1514f5e7dd7Smrg
1524f5e7dd7Smrg	    agp_info->fast_writes = (agp_status & 0x0010) != 0;
1534f5e7dd7Smrg	    agp_info->addr64 =      (agp_status & 0x0020) != 0;
1544f5e7dd7Smrg	    agp_info->htrans =      (agp_status & 0x0040) == 0;
1554f5e7dd7Smrg	    agp_info->gart64 =      (agp_status & 0x0080) != 0;
1564f5e7dd7Smrg	    agp_info->coherent =    (agp_status & 0x0100) != 0;
1574f5e7dd7Smrg	    agp_info->sideband =    (agp_status & 0x0200) != 0;
1584f5e7dd7Smrg	    agp_info->isochronus =  (agp_status & 0x10000) != 0;
1594f5e7dd7Smrg
1604f5e7dd7Smrg	    agp_info->async_req_size = 4 + (1 << ((agp_status & 0xe000) >> 13));
1614f5e7dd7Smrg	    agp_info->calibration_cycle_timing = ((agp_status & 0x1c00) >> 10);
1624f5e7dd7Smrg	    agp_info->max_requests = 1 + ((agp_status & 0xff000000) >> 24);
1634f5e7dd7Smrg
1644f5e7dd7Smrg	    dev_priv->agp = agp_info;
1654f5e7dd7Smrg	    break;
1664f5e7dd7Smrg	}
1674f5e7dd7Smrg
1684f5e7dd7Smrg	/* No other capabilities are currently handled.
1694f5e7dd7Smrg	 */
1704f5e7dd7Smrg	default:
1714f5e7dd7Smrg	    printf( "Unknown cap 0x%02x @ 0x%02x\n", cap_id, cap_offset );
1724f5e7dd7Smrg	    break;
1734f5e7dd7Smrg	}
1744f5e7dd7Smrg
1754f5e7dd7Smrg	cap_offset = next_cap;
1764f5e7dd7Smrg    }
1774f5e7dd7Smrg
1784f5e7dd7Smrg    return 0;
1794f5e7dd7Smrg}
1804f5e7dd7Smrg
1814f5e7dd7Smrg
1824f5e7dd7Smrg/**
1834f5e7dd7Smrg * Get AGP capability data for a device.
1844f5e7dd7Smrg */
1854f5e7dd7Smrgconst struct pci_agp_info *
1864f5e7dd7Smrgpci_device_get_agp_info( struct pci_device * dev )
1874f5e7dd7Smrg{
1884f5e7dd7Smrg    struct pci_device_private * dev_priv = (struct pci_device_private *) dev;
1894f5e7dd7Smrg
1904f5e7dd7Smrg    if ( dev == NULL ) {
1914f5e7dd7Smrg	return NULL;
1924f5e7dd7Smrg    }
193cad31331Smrg
1944f5e7dd7Smrg    if ( dev_priv->agp == NULL ) {
1954f5e7dd7Smrg	(void) (*pci_sys->methods->fill_capabilities)( dev );
1964f5e7dd7Smrg    }
1974f5e7dd7Smrg
1984f5e7dd7Smrg    return dev_priv->agp;
1994f5e7dd7Smrg}
200