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