1/*
2 * Copyright 2007 George Sapountzis
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24/**
25 * Macros for porting drivers from legacy xfree86 PCI code to the pciaccess
26 * library. The main purpose being to facilitate source code compatibility.
27 */
28
29#ifndef SISPCIRENAME_H
30#define SISPCIRENAME_H
31
32#include <stdint.h>
33
34enum region_type {
35    REGION_MEM,
36    REGION_IO
37};
38
39#include "xf86Module.h"
40
41#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) >= 12
42
43#if (defined(__alpha__) || defined(__ia64__)) && defined (linux)
44#define PCI_DOM_MASK    0x01fful
45#else
46#define PCI_DOM_MASK 0x0ffu
47#endif
48
49#ifndef PCI_DOM_MASK
50# define PCI_DOM_MASK 0x0ffu
51#endif
52#define PCI_DOMBUS_MASK (((PCI_DOM_MASK) << 8) | 0x0ffu)
53
54static inline uint32_t
55pciTag(int busnum, int devnum, int funcnum)
56{
57	uint32_t tag;
58	tag  = (busnum & (PCI_DOMBUS_MASK)) << 16;
59	tag |= (devnum & 0x00001fu) << 11;
60	tag |= (funcnum & 0x000007u) << 8;
61
62	return tag;
63}
64#endif /* GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) >= 12 */
65
66#ifndef XSERVER_LIBPCIACCESS
67
68/* pciVideoPtr */
69#define PCI_DEV_VENDOR_ID(_pcidev) ((_pcidev)->vendor)
70#define PCI_DEV_DEVICE_ID(_pcidev) ((_pcidev)->chipType)
71#define PCI_DEV_REVISION(_pcidev)  ((_pcidev)->chipRev)
72
73#define PCI_SUB_VENDOR_ID(_pcidev) ((_pcidev)->subsysVendor)
74#define PCI_SUB_DEVICE_ID(_pcidev) ((_pcidev)->subsysCard)
75
76#define PCI_DEV_TAG(_pcidev) pciTag((_pcidev)->bus,    \
77                                    (_pcidev)->device, \
78                                    (_pcidev)->func)
79#define PCI_DEV_BUS(_pcidev)       ((_pcidev)->bus)
80#define PCI_DEV_DEV(_pcidev)       ((_pcidev)->device)
81#define PCI_DEV_FUNC(_pcidev)      ((_pcidev)->func)
82
83/* pciConfigPtr */
84#define PCI_CFG_TAG(_pcidev)  (((pciConfigPtr)(_pcidev)->thisCard)->tag)
85#define PCI_CFG_BUS(_pcidev)  (((pciConfigPtr)(_pcidev)->thisCard)->busnum)
86#define PCI_CFG_DEV(_pcidev)  (((pciConfigPtr)(_pcidev)->thisCard)->devnum)
87#define PCI_CFG_FUNC(_pcidev) (((pciConfigPtr)(_pcidev)->thisCard)->funcnum)
88
89/* region addr: xfree86 uses different fields for memory regions and I/O ports */
90#define PCI_REGION_BASE(_pcidev, _b, _type)             \
91    (((_type) == REGION_MEM) ? (_pcidev)->memBase[(_b)] \
92                             : (_pcidev)->ioBase[(_b)])
93
94/* region size: xfree86 uses the log2 of the region size,
95 * but with zero meaning no region, not size of one XXX */
96#define PCI_REGION_SIZE(_pcidev, _b) \
97    (((_pcidev)->size[(_b)] > 0) ? (1 << (_pcidev)->size[(_b)]) : 0)
98
99/* read/write PCI configuration space */
100#define PCI_READ_BYTE(_pcidev, _value_ptr, _offset) \
101    *(_value_ptr) = pciReadByte(PCI_CFG_TAG(_pcidev), (_offset))
102
103#define PCI_READ_LONG(_pcidev, _value_ptr, _offset) \
104    *(_value_ptr) = pciReadLong(PCI_CFG_TAG(_pcidev), (_offset))
105
106#define PCI_WRITE_LONG(_pcidev, _value, _offset) \
107    pciWriteLong(PCI_CFG_TAG(_pcidev), (_offset), (_value))
108
109#else /* XSERVER_LIBPCIACCESS */
110
111typedef struct pci_device *pciVideoPtr;
112
113#define PCI_DEV_VENDOR_ID(_pcidev) ((_pcidev)->vendor_id)
114#define PCI_DEV_DEVICE_ID(_pcidev) ((_pcidev)->device_id)
115#define PCI_DEV_REVISION(_pcidev)  ((_pcidev)->revision)
116
117#define PCI_SUB_VENDOR_ID(_pcidev) ((_pcidev)->subvendor_id)
118#define PCI_SUB_DEVICE_ID(_pcidev) ((_pcidev)->subdevice_id)
119
120/* pci-rework functions take a 'pci_device' parameter instead of a tag */
121#define PCI_DEV_TAG(_pcidev)        (_pcidev)
122
123/* PCI_DEV macros, typically used in printf's, add domain ? XXX */
124#define PCI_DEV_BUS(_pcidev)       ((_pcidev)->bus)
125#define PCI_DEV_DEV(_pcidev)       ((_pcidev)->dev)
126#define PCI_DEV_FUNC(_pcidev)      ((_pcidev)->func)
127
128/* pci-rework functions take a 'pci_device' parameter instead of a tag */
129#define PCI_CFG_TAG(_pcidev)        (_pcidev)
130
131/* PCI_CFG macros, typically used in DRI init, contain the domain */
132#define PCI_CFG_BUS(_pcidev)      (((_pcidev)->domain << 8) | \
133                                    (_pcidev)->bus)
134#define PCI_CFG_DEV(_pcidev)       ((_pcidev)->dev)
135#define PCI_CFG_FUNC(_pcidev)      ((_pcidev)->func)
136
137#define PCI_REGION_BASE(_pcidev, _b, _type) ((_pcidev)->regions[(_b)].base_addr)
138#define PCI_REGION_SIZE(_pcidev, _b)        ((_pcidev)->regions[(_b)].size)
139
140#define PCI_READ_BYTE(_pcidev, _value_ptr, _offset) \
141    pci_device_cfg_read_u8((_pcidev), (_value_ptr), (_offset))
142
143#define PCI_READ_LONG(_pcidev, _value_ptr, _offset) \
144    pci_device_cfg_read_u32((_pcidev), (_value_ptr), (_offset))
145
146#define PCI_WRITE_LONG(_pcidev, _value, _offset) \
147    pci_device_cfg_write_u32((_pcidev), (_value), (_offset))
148
149#endif /* XSERVER_LIBPCIACCESS */
150
151#endif /* SISPCIRENAME_H */
152