1962c3257Smrg/*
2962c3257Smrg * Copyright 2007 George Sapountzis
3962c3257Smrg *
4962c3257Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5962c3257Smrg * copy of this software and associated documentation files (the "Software"),
6962c3257Smrg * to deal in the Software without restriction, including without limitation
7962c3257Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8962c3257Smrg * and/or sell copies of the Software, and to permit persons to whom the
9962c3257Smrg * Software is furnished to do so, subject to the following conditions:
10962c3257Smrg *
11962c3257Smrg * The above copyright notice and this permission notice (including the next
12962c3257Smrg * paragraph) shall be included in all copies or substantial portions of the
13962c3257Smrg * Software.
14962c3257Smrg *
15962c3257Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16962c3257Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17962c3257Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18962c3257Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19962c3257Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20962c3257Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21962c3257Smrg * SOFTWARE.
22962c3257Smrg */
23962c3257Smrg
24962c3257Smrg/**
25962c3257Smrg * Macros for porting drivers from legacy xfree86 PCI code to the pciaccess
26962c3257Smrg * library. The main purpose being to facilitate source code compatibility.
27962c3257Smrg */
28962c3257Smrg
29962c3257Smrg#ifndef CIRPCIRENAME_H
30962c3257Smrg#define CIRPCIRENAME_H
31962c3257Smrg
32962c3257Smrgenum region_type {
33962c3257Smrg    REGION_MEM,
34962c3257Smrg    REGION_IO
35962c3257Smrg};
36962c3257Smrg
37962c3257Smrg#ifndef XSERVER_LIBPCIACCESS
38962c3257Smrg
39962c3257Smrg/* pciVideoPtr */
40962c3257Smrg#define PCI_DEV_VENDOR_ID(_pcidev) ((_pcidev)->vendor)
41962c3257Smrg#define PCI_DEV_DEVICE_ID(_pcidev) ((_pcidev)->chipType)
42962c3257Smrg#define PCI_DEV_REVISION(_pcidev)  ((_pcidev)->chipRev)
43962c3257Smrg
44962c3257Smrg#define PCI_SUB_VENDOR_ID(_pcidev) ((_pcidev)->subsysVendor)
45962c3257Smrg#define PCI_SUB_DEVICE_ID(_pcidev) ((_pcidev)->subsysCard)
46962c3257Smrg
47962c3257Smrg#define PCI_DEV_TAG(_pcidev) pciTag((_pcidev)->bus,    \
48962c3257Smrg                                    (_pcidev)->device, \
49962c3257Smrg                                    (_pcidev)->func)
50962c3257Smrg#define PCI_DEV_BUS(_pcidev)       ((_pcidev)->bus)
51962c3257Smrg#define PCI_DEV_DEV(_pcidev)       ((_pcidev)->device)
52962c3257Smrg#define PCI_DEV_FUNC(_pcidev)      ((_pcidev)->func)
53962c3257Smrg
54962c3257Smrg/* pciConfigPtr */
55962c3257Smrg#define PCI_CFG_TAG(_pcidev)  (((pciConfigPtr)(_pcidev)->thisCard)->tag)
56962c3257Smrg#define PCI_CFG_BUS(_pcidev)  (((pciConfigPtr)(_pcidev)->thisCard)->busnum)
57962c3257Smrg#define PCI_CFG_DEV(_pcidev)  (((pciConfigPtr)(_pcidev)->thisCard)->devnum)
58962c3257Smrg#define PCI_CFG_FUNC(_pcidev) (((pciConfigPtr)(_pcidev)->thisCard)->funcnum)
59962c3257Smrg
60962c3257Smrg/* region addr: xfree86 uses different fields for memory regions and I/O ports */
61962c3257Smrg#define PCI_REGION_BASE(_pcidev, _b, _type)             \
62962c3257Smrg    (((_type) == REGION_MEM) ? (_pcidev)->memBase[(_b)] \
63962c3257Smrg                             : (_pcidev)->ioBase[(_b)])
64962c3257Smrg
65962c3257Smrg/* region size: xfree86 uses the log2 of the region size,
66962c3257Smrg * but with zero meaning no region, not size of one XXX */
67962c3257Smrg#define PCI_REGION_SIZE(_pcidev, _b) \
68962c3257Smrg    (((_pcidev)->size[(_b)] > 0) ? (1 << (_pcidev)->size[(_b)]) : 0)
69962c3257Smrg
70962c3257Smrg/* read/write PCI configuration space */
71962c3257Smrg#define PCI_READ_BYTE(_pcidev, _value_ptr, _offset) \
72962c3257Smrg    *(_value_ptr) = pciReadByte(PCI_CFG_TAG(_pcidev), (_offset))
73962c3257Smrg
74962c3257Smrg#define PCI_READ_LONG(_pcidev, _value_ptr, _offset) \
75962c3257Smrg    *(_value_ptr) = pciReadLong(PCI_CFG_TAG(_pcidev), (_offset))
76962c3257Smrg
77962c3257Smrg#define PCI_WRITE_LONG(_pcidev, _value, _offset) \
78962c3257Smrg    pciWriteLong(PCI_CFG_TAG(_pcidev), (_offset), (_value))
79962c3257Smrg
80962c3257Smrg#else /* XSERVER_LIBPCIACCESS */
81962c3257Smrg
82962c3257Smrgtypedef struct pci_device *pciVideoPtr;
83962c3257Smrg
84962c3257Smrg#define PCI_DEV_VENDOR_ID(_pcidev) ((_pcidev)->vendor_id)
85962c3257Smrg#define PCI_DEV_DEVICE_ID(_pcidev) ((_pcidev)->device_id)
86962c3257Smrg#define PCI_DEV_REVISION(_pcidev)  ((_pcidev)->revision)
87962c3257Smrg
88962c3257Smrg#define PCI_SUB_VENDOR_ID(_pcidev) ((_pcidev)->subvendor_id)
89962c3257Smrg#define PCI_SUB_DEVICE_ID(_pcidev) ((_pcidev)->subdevice_id)
90962c3257Smrg
91962c3257Smrg/* pci-rework functions take a 'pci_device' parameter instead of a tag */
92962c3257Smrg#define PCI_DEV_TAG(_pcidev)        (_pcidev)
93962c3257Smrg
94962c3257Smrg/* PCI_DEV macros, typically used in printf's, add domain ? XXX */
95962c3257Smrg#define PCI_DEV_BUS(_pcidev)       ((_pcidev)->bus)
96962c3257Smrg#define PCI_DEV_DEV(_pcidev)       ((_pcidev)->dev)
97962c3257Smrg#define PCI_DEV_FUNC(_pcidev)      ((_pcidev)->func)
98962c3257Smrg
99962c3257Smrg/* pci-rework functions take a 'pci_device' parameter instead of a tag */
100962c3257Smrg#define PCI_CFG_TAG(_pcidev)        (_pcidev)
101962c3257Smrg
102962c3257Smrg/* PCI_CFG macros, typically used in DRI init, contain the domain */
103962c3257Smrg#define PCI_CFG_BUS(_pcidev)      (((_pcidev)->domain << 8) | \
104962c3257Smrg                                    (_pcidev)->bus)
105962c3257Smrg#define PCI_CFG_DEV(_pcidev)       ((_pcidev)->dev)
106962c3257Smrg#define PCI_CFG_FUNC(_pcidev)      ((_pcidev)->func)
107962c3257Smrg
108962c3257Smrg#define PCI_REGION_BASE(_pcidev, _b, _type) ((_pcidev)->regions[(_b)].base_addr)
109962c3257Smrg#define PCI_REGION_SIZE(_pcidev, _b)        ((_pcidev)->regions[(_b)].size)
110962c3257Smrg
111962c3257Smrg#define PCI_READ_BYTE(_pcidev, _value_ptr, _offset) \
112962c3257Smrg    pci_device_cfg_read_u8((_pcidev), (_value_ptr), (_offset))
113962c3257Smrg
114962c3257Smrg#define PCI_READ_LONG(_pcidev, _value_ptr, _offset) \
115962c3257Smrg    pci_device_cfg_read_u32((_pcidev), (_value_ptr), (_offset))
116962c3257Smrg
117962c3257Smrg#define PCI_WRITE_LONG(_pcidev, _value, _offset) \
118962c3257Smrg    pci_device_cfg_write_u32((_pcidev), (_value), (_offset))
119962c3257Smrg
120962c3257Smrg#endif /* XSERVER_LIBPCIACCESS */
121962c3257Smrg
122962c3257Smrg#endif /* CIRPCIRENAME_H */
123