pciaccess.h revision 66337f63
1/* 2 * (C) Copyright IBM Corporation 2006 3 * Copyright 2009 Red Hat, Inc. 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 * Copyright (c) 2007 Paulo R. Zanoni, Tiago Vignatti 27 * 28 * Permission is hereby granted, free of charge, to any person 29 * obtaining a copy of this software and associated documentation 30 * files (the "Software"), to deal in the Software without 31 * restriction, including without limitation the rights to use, 32 * copy, modify, merge, publish, distribute, sublicense, and/or sell 33 * copies of the Software, and to permit persons to whom the 34 * Software is furnished to do so, subject to the following 35 * conditions: 36 * 37 * The above copyright notice and this permission notice shall be 38 * included in all copies or substantial portions of the Software. 39 * 40 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 41 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 42 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 43 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 44 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 45 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 46 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 47 * OTHER DEALINGS IN THE SOFTWARE. 48 * 49 */ 50 51/** 52 * \file pciaccess.h 53 * 54 * \author Ian Romanick <idr@us.ibm.com> 55 */ 56 57#ifndef PCIACCESS_H 58#define PCIACCESS_H 59 60#include <inttypes.h> 61 62#if (__GNUC__ >= 3) || (__SUNPRO_C >= 0x5130) 63#define __deprecated __attribute__((deprecated)) 64#else 65#define __deprecated 66#endif 67 68typedef uint64_t pciaddr_t; 69 70struct pci_device; 71struct pci_device_iterator; 72struct pci_id_match; 73struct pci_slot_match; 74 75#ifdef __cplusplus 76extern "C" { 77#endif 78 79int pci_device_has_kernel_driver(struct pci_device *dev); 80 81int pci_device_is_boot_vga(struct pci_device *dev); 82 83int pci_device_read_rom(struct pci_device *dev, void *buffer); 84 85int __deprecated pci_device_map_region(struct pci_device *dev, 86 unsigned region, int write_enable); 87 88int __deprecated pci_device_unmap_region(struct pci_device *dev, 89 unsigned region); 90 91int pci_device_map_range(struct pci_device *dev, pciaddr_t base, 92 pciaddr_t size, unsigned map_flags, void **addr); 93 94int pci_device_unmap_range(struct pci_device *dev, void *memory, 95 pciaddr_t size); 96 97int __deprecated pci_device_map_memory_range(struct pci_device *dev, 98 pciaddr_t base, pciaddr_t size, int write_enable, void **addr); 99 100int __deprecated pci_device_unmap_memory_range(struct pci_device *dev, 101 void *memory, pciaddr_t size); 102 103int pci_device_probe(struct pci_device *dev); 104 105const struct pci_agp_info *pci_device_get_agp_info(struct pci_device *dev); 106 107const struct pci_bridge_info *pci_device_get_bridge_info( 108 struct pci_device *dev); 109 110const struct pci_pcmcia_bridge_info *pci_device_get_pcmcia_bridge_info( 111 struct pci_device *dev); 112 113int pci_device_get_bridge_buses(struct pci_device *dev, int *primary_bus, 114 int *secondary_bus, int *subordinate_bus); 115 116int pci_system_init(void); 117 118void pci_system_init_dev_mem(int fd); 119 120void pci_system_cleanup(void); 121 122struct pci_device_iterator *pci_slot_match_iterator_create( 123 const struct pci_slot_match *match); 124 125struct pci_device_iterator *pci_id_match_iterator_create( 126 const struct pci_id_match *match); 127 128void pci_iterator_destroy(struct pci_device_iterator *iter); 129 130struct pci_device *pci_device_next(struct pci_device_iterator *iter); 131 132struct pci_device *pci_device_find_by_slot(uint32_t domain, uint32_t bus, 133 uint32_t dev, uint32_t func); 134 135struct pci_device *pci_device_get_parent_bridge(struct pci_device *dev); 136 137void pci_get_strings(const struct pci_id_match *m, 138 const char **device_name, const char **vendor_name, 139 const char **subdevice_name, const char **subvendor_name); 140const char *pci_device_get_device_name(const struct pci_device *dev); 141const char *pci_device_get_subdevice_name(const struct pci_device *dev); 142const char *pci_device_get_vendor_name(const struct pci_device *dev); 143const char *pci_device_get_subvendor_name(const struct pci_device *dev); 144 145void pci_device_enable(struct pci_device *dev); 146 147int pci_device_cfg_read (struct pci_device *dev, void *data, 148 pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_read); 149int pci_device_cfg_read_u8 (struct pci_device *dev, uint8_t *data, 150 pciaddr_t offset); 151int pci_device_cfg_read_u16(struct pci_device *dev, uint16_t *data, 152 pciaddr_t offset); 153int pci_device_cfg_read_u32(struct pci_device *dev, uint32_t *data, 154 pciaddr_t offset); 155 156int pci_device_cfg_write (struct pci_device *dev, const void *data, 157 pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_written); 158int pci_device_cfg_write_u8 (struct pci_device *dev, uint8_t data, 159 pciaddr_t offset); 160int pci_device_cfg_write_u16(struct pci_device *dev, uint16_t data, 161 pciaddr_t offset); 162int pci_device_cfg_write_u32(struct pci_device *dev, uint32_t data, 163 pciaddr_t offset); 164int pci_device_cfg_write_bits(struct pci_device *dev, uint32_t mask, 165 uint32_t data, pciaddr_t offset); 166 167#ifdef __cplusplus 168} 169#endif 170 171/** 172 * \name Mapping flags passed to \c pci_device_map_range 173 */ 174/*@{*/ 175#define PCI_DEV_MAP_FLAG_WRITABLE (1U<<0) 176#define PCI_DEV_MAP_FLAG_WRITE_COMBINE (1U<<1) 177#define PCI_DEV_MAP_FLAG_CACHABLE (1U<<2) 178/*@}*/ 179 180 181#define PCI_MATCH_ANY (~0U) 182 183/** 184 * Compare two PCI ID values (either vendor or device). This is used 185 * internally to compare the fields of \c pci_id_match to the fields of 186 * \c pci_device. 187 */ 188#define PCI_ID_COMPARE(a, b) \ 189 (((a) == PCI_MATCH_ANY) || ((a) == (b))) 190 191/** 192 */ 193struct pci_id_match { 194 /** 195 * \name Device / vendor matching controls 196 * 197 * Control the search based on the device, vendor, subdevice, or subvendor 198 * IDs. Setting any of these fields to \c PCI_MATCH_ANY will cause the 199 * field to not be used in the comparison. 200 */ 201 /*@{*/ 202 uint32_t vendor_id; 203 uint32_t device_id; 204 uint32_t subvendor_id; 205 uint32_t subdevice_id; 206 /*@}*/ 207 208 209 /** 210 * \name Device class matching controls 211 * 212 */ 213 /*@{*/ 214 uint32_t device_class; 215 uint32_t device_class_mask; 216 /*@}*/ 217 218 intptr_t match_data; 219}; 220 221 222/** 223 */ 224struct pci_slot_match { 225 /** 226 * \name Device slot matching controls 227 * 228 * Control the search based on the domain, bus, slot, and function of 229 * the device. Setting any of these fields to \c PCI_MATCH_ANY will cause 230 * the field to not be used in the comparison. 231 */ 232 /*@{*/ 233 uint32_t domain; 234 uint32_t bus; 235 uint32_t dev; 236 uint32_t func; 237 /*@}*/ 238 239 intptr_t match_data; 240}; 241 242/** 243 * BAR descriptor for a PCI device. 244 */ 245struct pci_mem_region { 246 /** 247 * When the region is mapped, this is the pointer to the memory. 248 * 249 * This field is \b only set when the deprecated \c pci_device_map_region 250 * interface is used. Use \c pci_device_map_range instead. 251 * 252 * \deprecated 253 */ 254 void *memory; 255 256 257 /** 258 * Base physical address of the region within its bus / domain. 259 * 260 * \warning 261 * This address is really only useful to other devices in the same 262 * domain. It's probably \b not the address applications will ever 263 * use. 264 * 265 * \warning 266 * Most (all?) platform back-ends leave this field unset. 267 */ 268 pciaddr_t bus_addr; 269 270 271 /** 272 * Base physical address of the region from the CPU's point of view. 273 * 274 * This address is typically passed to \c pci_device_map_range to create 275 * a mapping of the region to the CPU's virtual address space. 276 */ 277 pciaddr_t base_addr; 278 279 280 /** 281 * Size, in bytes, of the region. 282 */ 283 pciaddr_t size; 284 285 286 /** 287 * Is the region I/O ports or memory? 288 */ 289 unsigned is_IO:1; 290 291 /** 292 * Is the memory region prefetchable? 293 * 294 * \note 295 * This can only be set if \c is_IO is not set. 296 */ 297 unsigned is_prefetchable:1; 298 299 300 /** 301 * Is the memory at a 64-bit address? 302 * 303 * \note 304 * This can only be set if \c is_IO is not set. 305 */ 306 unsigned is_64:1; 307}; 308 309 310/** 311 * PCI device. 312 * 313 * Contains all of the information about a particular PCI device. 314 * 315 * This structure - like everything else in libpciaccess - is allocated 316 * by the library itself. Do not embed this structure in other structs, 317 * or otherwise allocate them yourself. 318 */ 319struct pci_device { 320 /** 321 * \name Device bus identification. 322 * 323 * Complete bus identification, including domain, of the device. On 324 * platforms that do not support PCI domains (e.g., 32-bit x86 hardware), 325 * the domain will always be zero. 326 * 327 * The domain_16 field is provided for binary compatibility with older 328 * libpciaccess. 329 */ 330 /*@{*/ 331 uint16_t domain_16; 332 uint8_t bus; 333 uint8_t dev; 334 uint8_t func; 335 /*@}*/ 336 337 338 /** 339 * \name Vendor / device ID 340 * 341 * The vendor ID, device ID, and sub-IDs for the device. 342 */ 343 /*@{*/ 344 uint16_t vendor_id; 345 uint16_t device_id; 346 uint16_t subvendor_id; 347 uint16_t subdevice_id; 348 /*@}*/ 349 350 /** 351 * Device's class, subclass, and programming interface packed into a 352 * single 32-bit value. The class is at bits [23:16], subclass is at 353 * bits [15:8], and programming interface is at [7:0]. 354 */ 355 uint32_t device_class; 356 357 358 /** 359 * Device revision number, as read from the configuration header. 360 */ 361 uint8_t revision; 362 363 364 /** 365 * BAR descriptors for the device. 366 */ 367 struct pci_mem_region regions[6]; 368 369 370 /** 371 * Size, in bytes, of the device's expansion ROM. 372 */ 373 pciaddr_t rom_size; 374 375 376 /** 377 * IRQ associated with the device. If there is no IRQ, this value will 378 * be -1. 379 */ 380 int irq; 381 382 383 /** 384 * Storage for user data. Users of the library can store arbitrary 385 * data in this pointer. The library will not use it for any purpose. 386 * It is the user's responsability to free this memory before destroying 387 * the \c pci_device structure. 388 */ 389 intptr_t user_data; 390 391 /** 392 * Used by the VGA arbiter. Type of resource decoded by the device and 393 * the file descriptor (/dev/vga_arbiter). */ 394 int vgaarb_rsrc; 395 396 397 /** 398 * PCI domain value (full 32 bits) 399 */ 400 uint32_t domain; 401}; 402 403 404/** 405 * Description of the AGP capability of the device. 406 * 407 * \sa pci_device_get_agp_info 408 */ 409struct pci_agp_info { 410 /** 411 * Offset of the AGP registers in the devices configuration register 412 * space. This is generally used so that the offset of the AGP command 413 * register can be determined. 414 */ 415 unsigned config_offset; 416 417 418 /** 419 * \name AGP major / minor version. 420 */ 421 /*@{*/ 422 uint8_t major_version; 423 uint8_t minor_version; 424 /*@}*/ 425 426 /** 427 * Logical OR of the supported AGP rates. For example, a value of 0x07 428 * means that the device can support 1x, 2x, and 4x. A value of 0x0c 429 * means that the device can support 8x and 4x. 430 */ 431 uint8_t rates; 432 433 unsigned int fast_writes:1; /**< Are fast-writes supported? */ 434 unsigned int addr64:1; 435 unsigned int htrans:1; 436 unsigned int gart64:1; 437 unsigned int coherent:1; 438 unsigned int sideband:1; /**< Is side-band addressing supported? */ 439 unsigned int isochronus:1; 440 441 uint8_t async_req_size; 442 uint8_t calibration_cycle_timing; 443 uint8_t max_requests; 444}; 445 446/** 447 * Description of a PCI-to-PCI bridge device. 448 * 449 * \sa pci_device_get_bridge_info 450 */ 451struct pci_bridge_info { 452 uint8_t primary_bus; 453 uint8_t secondary_bus; 454 uint8_t subordinate_bus; 455 uint8_t secondary_latency_timer; 456 457 uint8_t io_type; 458 uint8_t mem_type; 459 uint8_t prefetch_mem_type; 460 461 uint16_t secondary_status; 462 uint16_t bridge_control; 463 464 uint32_t io_base; 465 uint32_t io_limit; 466 467 uint32_t mem_base; 468 uint32_t mem_limit; 469 470 uint64_t prefetch_mem_base; 471 uint64_t prefetch_mem_limit; 472}; 473 474/** 475 * Description of a PCI-to-PCMCIA bridge device. 476 * 477 * \sa pci_device_get_pcmcia_bridge_info 478 */ 479struct pci_pcmcia_bridge_info { 480 uint8_t primary_bus; 481 uint8_t card_bus; 482 uint8_t subordinate_bus; 483 uint8_t cardbus_latency_timer; 484 485 uint16_t secondary_status; 486 uint16_t bridge_control; 487 488 struct { 489 uint32_t base; 490 uint32_t limit; 491 } io[2]; 492 493 struct { 494 uint32_t base; 495 uint32_t limit; 496 } mem[2]; 497 498}; 499 500 501/** 502 * VGA Arbiter definitions, functions and related. 503 */ 504 505/* Legacy VGA regions */ 506#define VGA_ARB_RSRC_NONE 0x00 507#define VGA_ARB_RSRC_LEGACY_IO 0x01 508#define VGA_ARB_RSRC_LEGACY_MEM 0x02 509/* Non-legacy access */ 510#define VGA_ARB_RSRC_NORMAL_IO 0x04 511#define VGA_ARB_RSRC_NORMAL_MEM 0x08 512 513int pci_device_vgaarb_init (void); 514void pci_device_vgaarb_fini (void); 515int pci_device_vgaarb_set_target (struct pci_device *dev); 516/* use the targetted device */ 517int pci_device_vgaarb_decodes (int new_vga_rsrc); 518int pci_device_vgaarb_lock (void); 519int pci_device_vgaarb_trylock (void); 520int pci_device_vgaarb_unlock (void); 521/* return the current device count + resource decodes for the device */ 522int pci_device_vgaarb_get_info (struct pci_device *dev, int *vga_count, int *rsrc_decodes); 523 524/* 525 * I/O space access. 526 */ 527 528struct pci_io_handle; 529 530struct pci_io_handle *pci_device_open_io(struct pci_device *dev, pciaddr_t base, 531 pciaddr_t size); 532struct pci_io_handle *pci_legacy_open_io(struct pci_device *dev, pciaddr_t base, 533 pciaddr_t size); 534void pci_device_close_io(struct pci_device *dev, struct pci_io_handle *handle); 535uint32_t pci_io_read32(struct pci_io_handle *handle, uint32_t reg); 536uint16_t pci_io_read16(struct pci_io_handle *handle, uint32_t reg); 537uint8_t pci_io_read8(struct pci_io_handle *handle, uint32_t reg); 538void pci_io_write32(struct pci_io_handle *handle, uint32_t reg, uint32_t data); 539void pci_io_write16(struct pci_io_handle *handle, uint32_t reg, uint16_t data); 540void pci_io_write8(struct pci_io_handle *handle, uint32_t reg, uint8_t data); 541 542/* 543 * Legacy memory access 544 */ 545 546int pci_device_map_legacy(struct pci_device *dev, pciaddr_t base, 547 pciaddr_t size, unsigned map_flags, void **addr); 548int pci_device_unmap_legacy(struct pci_device *dev, void *addr, pciaddr_t size); 549 550#endif /* PCIACCESS_H */ 551