blob: bd7be09ea53df24a7bea3301f579d18902fa79bb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file ati_pcigart.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * ATI PCI GART support
4 *
5 * \author Gareth Hughes <gareth@valinux.com>
6 */
7
8/*
9 * Created: Wed Dec 13 21:52:19 2000 by gareth@valinux.com
10 *
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
13 *
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:
20 *
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
23 * Software.
24 *
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.
32 */
33
34#include "drmP.h"
35
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)
48#else
49# error - PAGE_SIZE not 64K, 16K, 8K or 4K
50#endif
51
52# define ATI_MAX_PCIGART_PAGES 8192 /**< 32 MB aperture, 4K pages */
53# define ATI_PCIGART_PAGE_SIZE 4096 /**< PCI GART page size */
54
Dave Airlief26c4732006-01-02 17:18:39 +110055static void *drm_ati_alloc_pcigart_table(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 unsigned long address;
58 struct page *page;
59 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100060 DRM_DEBUG("%s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Dave Airlie507d2562006-01-25 14:58:58 +110062 address = __get_free_pages(GFP_KERNEL | __GFP_COMP,
63 ATI_PCIGART_TABLE_ORDER);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100064 if (address == 0UL) {
Dave Airlief1e5c032006-01-25 14:54:15 +110065 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
67
Dave Airlieb5e89ed2005-09-25 14:28:13 +100068 page = virt_to_page(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Dave Airlie507d2562006-01-25 14:58:58 +110070 for (i = 0; i < ATI_PCIGART_TABLE_PAGES; i++, page++)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100071 SetPageReserved(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Dave Airlieb5e89ed2005-09-25 14:28:13 +100073 DRM_DEBUG("%s: returning 0x%08lx\n", __FUNCTION__, address);
Dave Airlief26c4732006-01-02 17:18:39 +110074 return (void *)address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Dave Airlief26c4732006-01-02 17:18:39 +110077static void drm_ati_free_pcigart_table(void *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 struct page *page;
80 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100081 DRM_DEBUG("%s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Dave Airlief26c4732006-01-02 17:18:39 +110083 page = virt_to_page((unsigned long)address);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Dave Airlie507d2562006-01-25 14:58:58 +110085 for (i = 0; i < ATI_PCIGART_TABLE_PAGES; i++, page++)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100086 ClearPageReserved(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Dave Airlief26c4732006-01-02 17:18:39 +110088 free_pages((unsigned long)address, ATI_PCIGART_TABLE_ORDER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Dave Airlief26c4732006-01-02 17:18:39 +110091int drm_ati_pcigart_cleanup(drm_device_t *dev, drm_ati_pcigart_info *gart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 drm_sg_mem_t *entry = dev->sg;
94 unsigned long pages;
95 int i;
96
97 /* we need to support large memory configurations */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098 if (!entry) {
99 DRM_ERROR("no scatter/gather memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
101 }
102
Dave Airlieea98a922005-09-11 20:28:11 +1000103 if (gart_info->bus_addr) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000104 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
Dave Airlieea98a922005-09-11 20:28:11 +1000105 pci_unmap_single(dev->pdev, gart_info->bus_addr,
106 ATI_PCIGART_TABLE_PAGES * PAGE_SIZE,
107 PCI_DMA_TODEVICE);
108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000110 pages = (entry->pages <= ATI_MAX_PCIGART_PAGES)
111 ? entry->pages : ATI_MAX_PCIGART_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000113 for (i = 0; i < pages; i++) {
114 if (!entry->busaddr[i])
115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 pci_unmap_single(dev->pdev, entry->busaddr[i],
117 PAGE_SIZE, PCI_DMA_TODEVICE);
118 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000119
120 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN)
121 gart_info->bus_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000124 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN
125 && gart_info->addr) {
Dave Airlieea98a922005-09-11 20:28:11 +1000126 drm_ati_free_pcigart_table(gart_info->addr);
Dave Airlief1e5c032006-01-25 14:54:15 +1100127 gart_info->addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129
130 return 1;
131}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133EXPORT_SYMBOL(drm_ati_pcigart_cleanup);
134
Dave Airlief26c4732006-01-02 17:18:39 +1100135int drm_ati_pcigart_init(drm_device_t *dev, drm_ati_pcigart_info *gart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 drm_sg_mem_t *entry = dev->sg;
Dave Airlief26c4732006-01-02 17:18:39 +1100138 void *address = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 unsigned long pages;
140 u32 *pci_gart, page_base, bus_address = 0;
141 int i, j, ret = 0;
142
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000143 if (!entry) {
144 DRM_ERROR("no scatter/gather memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto done;
146 }
147
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000148 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
Dave Airlieea98a922005-09-11 20:28:11 +1000149 DRM_DEBUG("PCI: no table in VRAM: using normal RAM\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000150
Dave Airlieea98a922005-09-11 20:28:11 +1000151 address = drm_ati_alloc_pcigart_table();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000152 if (!address) {
153 DRM_ERROR("cannot allocate PCI GART page!\n");
Dave Airlieea98a922005-09-11 20:28:11 +1000154 goto done;
155 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000156
157 if (!dev->pdev) {
158 DRM_ERROR("PCI device unknown!\n");
Dave Airlieea98a922005-09-11 20:28:11 +1000159 goto done;
160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Dave Airlief26c4732006-01-02 17:18:39 +1100162 bus_address = pci_map_single(dev->pdev, address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000163 ATI_PCIGART_TABLE_PAGES *
164 PAGE_SIZE, PCI_DMA_TODEVICE);
Dave Airlieea98a922005-09-11 20:28:11 +1000165 if (bus_address == 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000166 DRM_ERROR("unable to map PCIGART pages!\n");
167 drm_ati_free_pcigart_table(address);
Dave Airlief1e5c032006-01-25 14:54:15 +1100168 address = NULL;
Dave Airlieea98a922005-09-11 20:28:11 +1000169 goto done;
170 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000171 } else {
Dave Airlieea98a922005-09-11 20:28:11 +1000172 address = gart_info->addr;
173 bus_address = gart_info->bus_addr;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000174 DRM_DEBUG("PCI: Gart Table: VRAM %08X mapped at %08lX\n",
Dave Airlief26c4732006-01-02 17:18:39 +1100175 bus_address, (unsigned long)address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000178 pci_gart = (u32 *) address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000180 pages = (entry->pages <= ATI_MAX_PCIGART_PAGES)
181 ? entry->pages : ATI_MAX_PCIGART_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000183 memset(pci_gart, 0, ATI_MAX_PCIGART_PAGES * sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000185 for (i = 0; i < pages; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* we need to support large memory configurations */
187 entry->busaddr[i] = pci_map_single(dev->pdev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000188 page_address(entry->
189 pagelist[i]),
190 PAGE_SIZE, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (entry->busaddr[i] == 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000192 DRM_ERROR("unable to map PCIGART pages!\n");
Dave Airlieea98a922005-09-11 20:28:11 +1000193 drm_ati_pcigart_cleanup(dev, gart_info);
Dave Airlief26c4732006-01-02 17:18:39 +1100194 address = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 bus_address = 0;
196 goto done;
197 }
198 page_base = (u32) entry->busaddr[i];
199
200 for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
Dave Airlieea98a922005-09-11 20:28:11 +1000201 if (gart_info->is_pcie)
Dave Airlied34d7ae2005-11-08 21:34:31 -0800202 *pci_gart = cpu_to_le32((page_base >> 8) | 0xc);
Dave Airlieea98a922005-09-11 20:28:11 +1000203 else
Dave Airlie3d5efad2005-09-30 19:12:46 +1000204 *pci_gart = cpu_to_le32(page_base);
Dave Airlied34d7ae2005-11-08 21:34:31 -0800205 pci_gart++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 page_base += ATI_PCIGART_PAGE_SIZE;
207 }
208 }
209
210 ret = 1;
211
212#if defined(__i386__) || defined(__x86_64__)
213 wbinvd();
214#else
215 mb();
216#endif
217
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000218 done:
Dave Airlieea98a922005-09-11 20:28:11 +1000219 gart_info->addr = address;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000220 gart_info->bus_addr = bus_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return ret;
222}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224EXPORT_SYMBOL(drm_ati_pcigart_init);