18e0ed500Smrg/*
28e0ed500Smrg * Copyright 2007 George Sapountzis
38e0ed500Smrg *
48e0ed500Smrg * Permission is hereby granted, free of charge, to any person obtaining a
58e0ed500Smrg * copy of this software and associated documentation files (the "Software"),
68e0ed500Smrg * to deal in the Software without restriction, including without limitation
78e0ed500Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88e0ed500Smrg * and/or sell copies of the Software, and to permit persons to whom the
98e0ed500Smrg * Software is furnished to do so, subject to the following conditions:
108e0ed500Smrg *
118e0ed500Smrg * The above copyright notice and this permission notice (including the next
128e0ed500Smrg * paragraph) shall be included in all copies or substantial portions of the
138e0ed500Smrg * Software.
148e0ed500Smrg *
158e0ed500Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168e0ed500Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178e0ed500Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
188e0ed500Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
198e0ed500Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
208e0ed500Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
218e0ed500Smrg * SOFTWARE.
228e0ed500Smrg */
238e0ed500Smrg
248e0ed500Smrg/**
258e0ed500Smrg * Macros for porting drivers from legacy xfree86 PCI code to the pciaccess
268e0ed500Smrg * library. The main purpose being to facilitate source code compatibility.
278e0ed500Smrg */
288e0ed500Smrg
298e0ed500Smrg#ifndef CIRPCIRENAME_H
308e0ed500Smrg#define CIRPCIRENAME_H
318e0ed500Smrg
328e0ed500Smrgenum region_type {
338e0ed500Smrg    REGION_MEM,
348e0ed500Smrg    REGION_IO
358e0ed500Smrg};
368e0ed500Smrg
378e0ed500Smrg#ifndef XSERVER_LIBPCIACCESS
388e0ed500Smrg
398e0ed500Smrg/* pciVideoPtr */
408e0ed500Smrg#define PCI_DEV_VENDOR_ID(_pcidev) ((_pcidev)->vendor)
418e0ed500Smrg#define PCI_DEV_DEVICE_ID(_pcidev) ((_pcidev)->chipType)
428e0ed500Smrg#define PCI_DEV_REVISION(_pcidev)  ((_pcidev)->chipRev)
438e0ed500Smrg
448e0ed500Smrg#define PCI_SUB_VENDOR_ID(_pcidev) ((_pcidev)->subsysVendor)
458e0ed500Smrg#define PCI_SUB_DEVICE_ID(_pcidev) ((_pcidev)->subsysCard)
468e0ed500Smrg
478e0ed500Smrg#define PCI_DEV_TAG(_pcidev) pciTag((_pcidev)->bus,    \
488e0ed500Smrg                                    (_pcidev)->device, \
498e0ed500Smrg                                    (_pcidev)->func)
508e0ed500Smrg#define PCI_DEV_BUS(_pcidev)       ((_pcidev)->bus)
518e0ed500Smrg#define PCI_DEV_DEV(_pcidev)       ((_pcidev)->device)
528e0ed500Smrg#define PCI_DEV_FUNC(_pcidev)      ((_pcidev)->func)
538e0ed500Smrg
548e0ed500Smrg/* pciConfigPtr */
558e0ed500Smrg#define PCI_CFG_TAG(_pcidev)  (((pciConfigPtr)(_pcidev)->thisCard)->tag)
568e0ed500Smrg#define PCI_CFG_BUS(_pcidev)  (((pciConfigPtr)(_pcidev)->thisCard)->busnum)
578e0ed500Smrg#define PCI_CFG_DEV(_pcidev)  (((pciConfigPtr)(_pcidev)->thisCard)->devnum)
588e0ed500Smrg#define PCI_CFG_FUNC(_pcidev) (((pciConfigPtr)(_pcidev)->thisCard)->funcnum)
598e0ed500Smrg
608e0ed500Smrg/* region addr: xfree86 uses different fields for memory regions and I/O ports */
618e0ed500Smrg#define PCI_REGION_BASE(_pcidev, _b, _type)             \
628e0ed500Smrg    (((_type) == REGION_MEM) ? (_pcidev)->memBase[(_b)] \
638e0ed500Smrg                             : (_pcidev)->ioBase[(_b)])
648e0ed500Smrg
658e0ed500Smrg/* region size: xfree86 uses the log2 of the region size,
668e0ed500Smrg * but with zero meaning no region, not size of one XXX */
678e0ed500Smrg#define PCI_REGION_SIZE(_pcidev, _b) \
688e0ed500Smrg    (((_pcidev)->size[(_b)] > 0) ? (1 << (_pcidev)->size[(_b)]) : 0)
698e0ed500Smrg
708e0ed500Smrg/* read/write PCI configuration space */
718e0ed500Smrg#define PCI_READ_BYTE(_pcidev, _value_ptr, _offset) \
728e0ed500Smrg    *(_value_ptr) = pciReadByte(PCI_CFG_TAG(_pcidev), (_offset))
738e0ed500Smrg
748e0ed500Smrg#define PCI_READ_LONG(_pcidev, _value_ptr, _offset) \
758e0ed500Smrg    *(_value_ptr) = pciReadLong(PCI_CFG_TAG(_pcidev), (_offset))
768e0ed500Smrg
778e0ed500Smrg#define PCI_WRITE_LONG(_pcidev, _value, _offset) \
788e0ed500Smrg    pciWriteLong(PCI_CFG_TAG(_pcidev), (_offset), (_value))
798e0ed500Smrg
808e0ed500Smrg#else /* XSERVER_LIBPCIACCESS */
818e0ed500Smrg
828e0ed500Smrgtypedef struct pci_device *pciVideoPtr;
838e0ed500Smrg
848e0ed500Smrg#define PCI_DEV_VENDOR_ID(_pcidev) ((_pcidev)->vendor_id)
858e0ed500Smrg#define PCI_DEV_DEVICE_ID(_pcidev) ((_pcidev)->device_id)
868e0ed500Smrg#define PCI_DEV_REVISION(_pcidev)  ((_pcidev)->revision)
878e0ed500Smrg
888e0ed500Smrg#define PCI_SUB_VENDOR_ID(_pcidev) ((_pcidev)->subvendor_id)
898e0ed500Smrg#define PCI_SUB_DEVICE_ID(_pcidev) ((_pcidev)->subdevice_id)
908e0ed500Smrg
918e0ed500Smrg/* pci-rework functions take a 'pci_device' parameter instead of a tag */
928e0ed500Smrg#define PCI_DEV_TAG(_pcidev)        (_pcidev)
938e0ed500Smrg
948e0ed500Smrg/* PCI_DEV macros, typically used in printf's, add domain ? XXX */
958e0ed500Smrg#define PCI_DEV_BUS(_pcidev)       ((_pcidev)->bus)
968e0ed500Smrg#define PCI_DEV_DEV(_pcidev)       ((_pcidev)->dev)
978e0ed500Smrg#define PCI_DEV_FUNC(_pcidev)      ((_pcidev)->func)
988e0ed500Smrg
998e0ed500Smrg/* pci-rework functions take a 'pci_device' parameter instead of a tag */
1008e0ed500Smrg#define PCI_CFG_TAG(_pcidev)        (_pcidev)
1018e0ed500Smrg
1028e0ed500Smrg/* PCI_CFG macros, typically used in DRI init, contain the domain */
1038e0ed500Smrg#define PCI_CFG_BUS(_pcidev)      (((_pcidev)->domain << 8) | \
1048e0ed500Smrg                                    (_pcidev)->bus)
1058e0ed500Smrg#define PCI_CFG_DEV(_pcidev)       ((_pcidev)->dev)
1068e0ed500Smrg#define PCI_CFG_FUNC(_pcidev)      ((_pcidev)->func)
1078e0ed500Smrg
1088e0ed500Smrg#define PCI_REGION_BASE(_pcidev, _b, _type) ((_pcidev)->regions[(_b)].base_addr)
1098e0ed500Smrg#define PCI_REGION_SIZE(_pcidev, _b)        ((_pcidev)->regions[(_b)].size)
1108e0ed500Smrg
1118e0ed500Smrg#define PCI_READ_BYTE(_pcidev, _value_ptr, _offset) \
1128e0ed500Smrg    pci_device_cfg_read_u8((_pcidev), (_value_ptr), (_offset))
1138e0ed500Smrg
1148e0ed500Smrg#define PCI_READ_LONG(_pcidev, _value_ptr, _offset) \
1158e0ed500Smrg    pci_device_cfg_read_u32((_pcidev), (_value_ptr), (_offset))
1168e0ed500Smrg
1178e0ed500Smrg#define PCI_WRITE_LONG(_pcidev, _value, _offset) \
1188e0ed500Smrg    pci_device_cfg_write_u32((_pcidev), (_value), (_offset))
1198e0ed500Smrg
1208e0ed500Smrg#endif /* XSERVER_LIBPCIACCESS */
1218e0ed500Smrg
1228e0ed500Smrg#endif /* CIRPCIRENAME_H */
123