5 * \author Gareth Hughes <gareth@valinux.com>
9 * Created: Wed Dec 13 21:52:19 2000 by gareth@valinux.com
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
36 #if PAGE_SIZE == 65536
37 # define ATI_PCIGART_TABLE_ORDER 0
38 # define ATI_PCIGART_TABLE_PAGES (1 << 0)
39 #elif PAGE_SIZE == 16384
40 # define ATI_PCIGART_TABLE_ORDER 1
41 # define ATI_PCIGART_TABLE_PAGES (1 << 1)
42 #elif PAGE_SIZE == 8192
43 # define ATI_PCIGART_TABLE_ORDER 2
44 # define ATI_PCIGART_TABLE_PAGES (1 << 2)
45 #elif PAGE_SIZE == 4096
46 # define ATI_PCIGART_TABLE_ORDER 3
47 # define ATI_PCIGART_TABLE_PAGES (1 << 3)
49 # error - PAGE_SIZE not 64K, 16K, 8K or 4K
52 # define ATI_MAX_PCIGART_PAGES 8192 /**< 32 MB aperture, 4K pages */
53 # define ATI_PCIGART_PAGE_SIZE 4096 /**< PCI GART page size */
55 static void *drm_ati_alloc_pcigart_table(void)
57 unsigned long address;
60 DRM_DEBUG("%s\n", __FUNCTION__);
62 address = __get_free_pages(GFP_KERNEL | __GFP_COMP,
63 ATI_PCIGART_TABLE_ORDER);
68 page = virt_to_page(address);
70 for (i = 0; i < ATI_PCIGART_TABLE_PAGES; i++, page++)
71 SetPageReserved(page);
73 DRM_DEBUG("%s: returning 0x%08lx\n", __FUNCTION__, address);
74 return (void *)address;
77 static void drm_ati_free_pcigart_table(void *address)
81 DRM_DEBUG("%s\n", __FUNCTION__);
83 page = virt_to_page((unsigned long)address);
85 for (i = 0; i < ATI_PCIGART_TABLE_PAGES; i++, page++)
86 ClearPageReserved(page);
88 free_pages((unsigned long)address, ATI_PCIGART_TABLE_ORDER);
91 int drm_ati_pcigart_cleanup(drm_device_t *dev, drm_ati_pcigart_info *gart_info)
93 drm_sg_mem_t *entry = dev->sg;
97 /* we need to support large memory configurations */
99 DRM_ERROR("no scatter/gather memory!\n");
103 if (gart_info->bus_addr) {
104 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
105 pci_unmap_single(dev->pdev, gart_info->bus_addr,
106 ATI_PCIGART_TABLE_PAGES * PAGE_SIZE,
110 pages = (entry->pages <= ATI_MAX_PCIGART_PAGES)
111 ? entry->pages : ATI_MAX_PCIGART_PAGES;
113 for (i = 0; i < pages; i++) {
114 if (!entry->busaddr[i])
116 pci_unmap_single(dev->pdev, entry->busaddr[i],
117 PAGE_SIZE, PCI_DMA_TODEVICE);
120 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN)
121 gart_info->bus_addr = 0;
124 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN
125 && gart_info->addr) {
126 drm_ati_free_pcigart_table(gart_info->addr);
127 gart_info->addr = NULL;
133 EXPORT_SYMBOL(drm_ati_pcigart_cleanup);
135 int drm_ati_pcigart_init(drm_device_t *dev, drm_ati_pcigart_info *gart_info)
137 drm_sg_mem_t *entry = dev->sg;
138 void *address = NULL;
140 u32 *pci_gart, page_base, bus_address = 0;
144 DRM_ERROR("no scatter/gather memory!\n");
148 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
149 DRM_DEBUG("PCI: no table in VRAM: using normal RAM\n");
151 address = drm_ati_alloc_pcigart_table();
153 DRM_ERROR("cannot allocate PCI GART page!\n");
158 DRM_ERROR("PCI device unknown!\n");
162 bus_address = pci_map_single(dev->pdev, address,
163 ATI_PCIGART_TABLE_PAGES *
164 PAGE_SIZE, PCI_DMA_TODEVICE);
165 if (bus_address == 0) {
166 DRM_ERROR("unable to map PCIGART pages!\n");
167 drm_ati_free_pcigart_table(address);
172 address = gart_info->addr;
173 bus_address = gart_info->bus_addr;
174 DRM_DEBUG("PCI: Gart Table: VRAM %08X mapped at %08lX\n",
175 bus_address, (unsigned long)address);
178 pci_gart = (u32 *) address;
180 pages = (entry->pages <= ATI_MAX_PCIGART_PAGES)
181 ? entry->pages : ATI_MAX_PCIGART_PAGES;
183 memset(pci_gart, 0, ATI_MAX_PCIGART_PAGES * sizeof(u32));
185 for (i = 0; i < pages; i++) {
186 /* we need to support large memory configurations */
187 entry->busaddr[i] = pci_map_single(dev->pdev,
190 PAGE_SIZE, PCI_DMA_TODEVICE);
191 if (entry->busaddr[i] == 0) {
192 DRM_ERROR("unable to map PCIGART pages!\n");
193 drm_ati_pcigart_cleanup(dev, gart_info);
198 page_base = (u32) entry->busaddr[i];
200 for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
201 if (gart_info->is_pcie)
202 *pci_gart = cpu_to_le32((page_base >> 8) | 0xc);
204 *pci_gart = cpu_to_le32(page_base);
206 page_base += ATI_PCIGART_PAGE_SIZE;
212 #if defined(__i386__) || defined(__x86_64__)
219 gart_info->addr = address;
220 gart_info->bus_addr = bus_address;
224 EXPORT_SYMBOL(drm_ati_pcigart_init);