patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / char / drm / ati_pcigart.h
1 /**
2  * \file ati_pcigart.h 
3  * 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
55 static unsigned long DRM(ati_alloc_pcigart_table)( void )
56 {
57         unsigned long address;
58         struct page *page;
59         int i;
60         DRM_DEBUG( "%s\n", __FUNCTION__ );
61
62         address = __get_free_pages( GFP_KERNEL, ATI_PCIGART_TABLE_ORDER );
63         if ( address == 0UL ) {
64                 return 0;
65         }
66
67         page = virt_to_page( address );
68
69         for ( i = 0 ; i < ATI_PCIGART_TABLE_PAGES ; i++, page++ ) {
70                 get_page(page);
71                 SetPageReserved( page );
72         }
73
74         DRM_DEBUG( "%s: returning 0x%08lx\n", __FUNCTION__, address );
75         return address;
76 }
77
78 static void DRM(ati_free_pcigart_table)( unsigned long address )
79 {
80         struct page *page;
81         int i;
82         DRM_DEBUG( "%s\n", __FUNCTION__ );
83
84         page = virt_to_page( address );
85
86         for ( i = 0 ; i < ATI_PCIGART_TABLE_PAGES ; i++, page++ ) {
87                 __put_page(page);
88                 ClearPageReserved( page );
89         }
90
91         free_pages( address, ATI_PCIGART_TABLE_ORDER );
92 }
93
94 int DRM(ati_pcigart_init)( drm_device_t *dev,
95                            unsigned long *addr,
96                            dma_addr_t *bus_addr)
97 {
98         drm_sg_mem_t *entry = dev->sg;
99         unsigned long address = 0;
100         unsigned long pages;
101         u32 *pci_gart, page_base, bus_address = 0;
102         int i, j, ret = 0;
103
104         if ( !entry ) {
105                 DRM_ERROR( "no scatter/gather memory!\n" );
106                 goto done;
107         }
108
109         address = DRM(ati_alloc_pcigart_table)();
110         if ( !address ) {
111                 DRM_ERROR( "cannot allocate PCI GART page!\n" );
112                 goto done;
113         }
114
115         if ( !dev->pdev ) {
116                 DRM_ERROR( "PCI device unknown!\n" );
117                 goto done;
118         }
119
120         bus_address = pci_map_single(dev->pdev, (void *)address,
121                                   ATI_PCIGART_TABLE_PAGES * PAGE_SIZE,
122                                   PCI_DMA_TODEVICE);
123         if (bus_address == 0) {
124                 DRM_ERROR( "unable to map PCIGART pages!\n" );
125                 DRM(ati_free_pcigart_table)( address );
126                 address = 0;
127                 goto done;
128         }
129
130         pci_gart = (u32 *)address;
131
132         pages = ( entry->pages <= ATI_MAX_PCIGART_PAGES )
133                 ? entry->pages : ATI_MAX_PCIGART_PAGES;
134
135         memset( pci_gart, 0, ATI_MAX_PCIGART_PAGES * sizeof(u32) );
136
137         for ( i = 0 ; i < pages ; i++ ) {
138                 /* we need to support large memory configurations */
139                 entry->busaddr[i] = pci_map_single(dev->pdev,
140                                            page_address( entry->pagelist[i] ),
141                                            PAGE_SIZE,
142                                            PCI_DMA_TODEVICE);
143                 if (entry->busaddr[i] == 0) {
144                         DRM_ERROR( "unable to map PCIGART pages!\n" );
145                         DRM(ati_pcigart_cleanup)( dev, address, bus_address );
146                         address = 0;
147                         bus_address = 0;
148                         goto done;
149                 }
150                 page_base = (u32) entry->busaddr[i];
151
152                 for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
153                         *pci_gart++ = cpu_to_le32( page_base );
154                         page_base += ATI_PCIGART_PAGE_SIZE;
155                 }
156         }
157
158         ret = 1;
159
160 #if defined(__i386__) || defined(__x86_64__)
161         asm volatile ( "wbinvd" ::: "memory" );
162 #else
163         mb();
164 #endif
165
166 done:
167         *addr = address;
168         *bus_addr = bus_address;
169         return ret;
170 }
171
172 int DRM(ati_pcigart_cleanup)( drm_device_t *dev,
173                               unsigned long addr,
174                               dma_addr_t bus_addr)
175 {
176         drm_sg_mem_t *entry = dev->sg;
177         unsigned long pages;
178         int i;
179
180         /* we need to support large memory configurations */
181         if ( !entry ) {
182                 DRM_ERROR( "no scatter/gather memory!\n" );
183                 return 0;
184         }
185
186         if ( bus_addr ) {
187                 pci_unmap_single(dev->pdev, bus_addr,
188                                  ATI_PCIGART_TABLE_PAGES * PAGE_SIZE,
189                                  PCI_DMA_TODEVICE);
190
191                 pages = ( entry->pages <= ATI_MAX_PCIGART_PAGES )
192                         ? entry->pages : ATI_MAX_PCIGART_PAGES;
193
194                 for ( i = 0 ; i < pages ; i++ ) {
195                         if ( !entry->busaddr[i] ) break;
196                         pci_unmap_single(dev->pdev, entry->busaddr[i],
197                                          PAGE_SIZE, PCI_DMA_TODEVICE);
198                 }
199         }
200
201         if ( addr ) {
202                 DRM(ati_free_pcigart_table)( addr );
203         }
204
205         return 1;
206 }