1 /* ati_pcigart.h -- ATI PCI GART support -*- linux-c -*-
2 * Created: Wed Dec 13 21:52:19 2000 by gareth@valinux.com
3 *
4 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * Authors:
27 * Gareth Hughes <gareth@valinux.com>
28 */
29
30 #include "drmP.h"
31
32 #if PAGE_SIZE == 65536
33 # define ATI_PCIGART_TABLE_ORDER 0
34 # define ATI_PCIGART_TABLE_PAGES (1 << 0)
35 #elif PAGE_SIZE == 16384
36 # define ATI_PCIGART_TABLE_ORDER 1
37 # define ATI_PCIGART_TABLE_PAGES (1 << 1)
38 #elif PAGE_SIZE == 8192
39 # define ATI_PCIGART_TABLE_ORDER 2
40 # define ATI_PCIGART_TABLE_PAGES (1 << 2)
41 #elif PAGE_SIZE == 4096
42 # define ATI_PCIGART_TABLE_ORDER 3
43 # define ATI_PCIGART_TABLE_PAGES (1 << 3)
44 #else
45 # error - PAGE_SIZE not 64K, 16K, 8K or 4K
46 #endif
47
48 # define ATI_MAX_PCIGART_PAGES 8192 /* 32 MB aperture, 4K pages */
49 # define ATI_PCIGART_PAGE_SIZE 4096 /* PCI GART page size */
50
DRM(ati_alloc_pcigart_table)51 static unsigned long DRM(ati_alloc_pcigart_table)( void )
52 {
53 unsigned long address;
54 struct page *page;
55 int i;
56 DRM_DEBUG( "%s\n", __FUNCTION__ );
57
58 address = __get_free_pages( GFP_KERNEL, ATI_PCIGART_TABLE_ORDER );
59 if ( address == 0UL ) {
60 return 0;
61 }
62
63 page = virt_to_page( address );
64
65 for ( i = 0 ; i < ATI_PCIGART_TABLE_PAGES ; i++, page++ ) {
66 atomic_inc( &page->count );
67 SetPageReserved( page );
68 }
69
70 DRM_DEBUG( "%s: returning 0x%08lx\n", __FUNCTION__, address );
71 return address;
72 }
73
DRM(ati_free_pcigart_table)74 static void DRM(ati_free_pcigart_table)( unsigned long address )
75 {
76 struct page *page;
77 int i;
78 DRM_DEBUG( "%s\n", __FUNCTION__ );
79
80 page = virt_to_page( address );
81
82 for ( i = 0 ; i < ATI_PCIGART_TABLE_PAGES ; i++, page++ ) {
83 atomic_dec( &page->count );
84 ClearPageReserved( page );
85 }
86
87 free_pages( address, ATI_PCIGART_TABLE_ORDER );
88 }
89
DRM(ati_pcigart_init)90 int DRM(ati_pcigart_init)( drm_device_t *dev,
91 unsigned long *addr,
92 dma_addr_t *bus_addr)
93 {
94 drm_sg_mem_t *entry = dev->sg;
95 unsigned long address = 0;
96 unsigned long pages;
97 u32 *pci_gart, page_base, bus_address = 0;
98 int i, j, ret = 0;
99
100 if ( !entry ) {
101 DRM_ERROR( "no scatter/gather memory!\n" );
102 goto done;
103 }
104
105 address = DRM(ati_alloc_pcigart_table)();
106 if ( !address ) {
107 DRM_ERROR( "cannot allocate PCI GART page!\n" );
108 goto done;
109 }
110
111 if ( !dev->pdev ) {
112 DRM_ERROR( "PCI device unknown!\n" );
113 goto done;
114 }
115
116 bus_address = pci_map_single(dev->pdev, (void *)address,
117 ATI_PCIGART_TABLE_PAGES * PAGE_SIZE,
118 PCI_DMA_TODEVICE);
119 if (bus_address == 0) {
120 DRM_ERROR( "unable to map PCIGART pages!\n" );
121 DRM(ati_free_pcigart_table)( address );
122 address = 0;
123 goto done;
124 }
125
126 pci_gart = (u32 *)address;
127
128 pages = ( entry->pages <= ATI_MAX_PCIGART_PAGES )
129 ? entry->pages : ATI_MAX_PCIGART_PAGES;
130
131 memset( pci_gart, 0, ATI_MAX_PCIGART_PAGES * sizeof(u32) );
132
133 for ( i = 0 ; i < pages ; i++ ) {
134 /* we need to support large memory configurations */
135 entry->busaddr[i] = pci_map_single(dev->pdev,
136 page_address( entry->pagelist[i] ),
137 PAGE_SIZE,
138 PCI_DMA_TODEVICE);
139 if (entry->busaddr[i] == 0) {
140 DRM_ERROR( "unable to map PCIGART pages!\n" );
141 DRM(ati_pcigart_cleanup)( dev, address, bus_address );
142 address = 0;
143 bus_address = 0;
144 goto done;
145 }
146 page_base = (u32) entry->busaddr[i];
147
148 for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
149 *pci_gart++ = cpu_to_le32( page_base );
150 page_base += ATI_PCIGART_PAGE_SIZE;
151 }
152 }
153
154 ret = 1;
155
156 #if defined(__i386__) || defined(__x86_64__)
157 asm volatile ( "wbinvd" ::: "memory" );
158 #else
159 mb();
160 #endif
161
162 done:
163 *addr = address;
164 *bus_addr = bus_address;
165 return ret;
166 }
167
DRM(ati_pcigart_cleanup)168 int DRM(ati_pcigart_cleanup)( drm_device_t *dev,
169 unsigned long addr,
170 dma_addr_t bus_addr)
171 {
172 drm_sg_mem_t *entry = dev->sg;
173 unsigned long pages;
174 int i;
175
176 /* we need to support large memory configurations */
177 if ( !entry ) {
178 DRM_ERROR( "no scatter/gather memory!\n" );
179 return 0;
180 }
181
182 if ( bus_addr ) {
183 pci_unmap_single(dev->pdev, bus_addr,
184 ATI_PCIGART_TABLE_PAGES * PAGE_SIZE,
185 PCI_DMA_TODEVICE);
186
187 pages = ( entry->pages <= ATI_MAX_PCIGART_PAGES )
188 ? entry->pages : ATI_MAX_PCIGART_PAGES;
189
190 for ( i = 0 ; i < pages ; i++ ) {
191 if ( !entry->busaddr[i] ) break;
192 pci_unmap_single(dev->pdev, entry->busaddr[i],
193 PAGE_SIZE, PCI_DMA_TODEVICE);
194 }
195 }
196
197 if ( addr ) {
198 DRM(ati_free_pcigart_table)( addr );
199 }
200
201 return 1;
202 }
203