Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / drivers / char / agp / sgi-agp.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2003-2005 Silicon Graphics, Inc.  All Rights Reserved.
7  */
8
9 /*
10  * SGI TIOCA AGPGART routines.
11  *
12  */
13
14 #include <linux/acpi.h>
15 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/init.h>
18 #include <linux/agp_backend.h>
19 #include <asm/sn/addrs.h>
20 #include <asm/sn/pcidev.h>
21 #include <asm/sn/pcibus_provider_defs.h>
22 #include <asm/sn/tioca_provider.h>
23 #include "agp.h"
24
25 extern int agp_memory_reserved;
26 extern uint32_t tioca_gart_found;
27 extern struct list_head tioca_list;
28 static struct agp_bridge_data **sgi_tioca_agp_bridges;
29
30 /*
31  * The aperature size and related information is set up at TIOCA init time.
32  * Values for this table will be extracted and filled in at
33  * sgi_tioca_fetch_size() time.
34  */
35
36 static struct aper_size_info_fixed sgi_tioca_sizes[] = {
37         {0, 0, 0},
38 };
39
40 static void *sgi_tioca_alloc_page(struct agp_bridge_data *bridge)
41 {
42         struct page *page;
43         int nid;
44         struct tioca_kernel *info =
45             (struct tioca_kernel *)bridge->dev_private_data;
46
47         nid = info->ca_closest_node;
48         page = alloc_pages_node(nid, GFP_KERNEL, 0);
49         if (page == NULL) {
50                 return 0;
51         }
52
53         get_page(page);
54         SetPageLocked(page);
55         atomic_inc(&agp_bridge->current_memory_agp);
56         return page_address(page);
57 }
58
59 /*
60  * Flush GART tlb's.  Cannot selectively flush based on memory so the mem
61  * arg is ignored.
62  */
63
64 static void sgi_tioca_tlbflush(struct agp_memory *mem)
65 {
66         tioca_tlbflush(mem->bridge->dev_private_data);
67 }
68
69 /*
70  * Given an address of a host physical page, turn it into a valid gart
71  * entry.
72  */
73 static unsigned long
74 sgi_tioca_mask_memory(struct agp_bridge_data *bridge,
75                       unsigned long addr, int type)
76 {
77         return tioca_physpage_to_gart(addr);
78 }
79
80 static void sgi_tioca_agp_enable(struct agp_bridge_data *bridge, u32 mode)
81 {
82         tioca_fastwrite_enable(bridge->dev_private_data);
83 }
84
85 /*
86  * sgi_tioca_configure() doesn't have anything to do since the base CA driver
87  * has alreay set up the GART.
88  */
89
90 static int sgi_tioca_configure(void)
91 {
92         return 0;
93 }
94
95 /*
96  * Determine gfx aperature size.  This has already been determined by the
97  * CA driver init, so just need to set agp_bridge values accordingly.
98  */
99
100 static int sgi_tioca_fetch_size(void)
101 {
102         struct tioca_kernel *info =
103             (struct tioca_kernel *)agp_bridge->dev_private_data;
104
105         sgi_tioca_sizes[0].size = info->ca_gfxap_size / MB(1);
106         sgi_tioca_sizes[0].num_entries = info->ca_gfxgart_entries;
107
108         return sgi_tioca_sizes[0].size;
109 }
110
111 static int sgi_tioca_create_gatt_table(struct agp_bridge_data *bridge)
112 {
113         struct tioca_kernel *info =
114             (struct tioca_kernel *)bridge->dev_private_data;
115
116         bridge->gatt_table_real = (u32 *) info->ca_gfxgart;
117         bridge->gatt_table = bridge->gatt_table_real;
118         bridge->gatt_bus_addr = info->ca_gfxgart_base;
119
120         return 0;
121 }
122
123 static int sgi_tioca_free_gatt_table(struct agp_bridge_data *bridge)
124 {
125         return 0;
126 }
127
128 static int sgi_tioca_insert_memory(struct agp_memory *mem, off_t pg_start,
129                                    int type)
130 {
131         int num_entries;
132         size_t i;
133         off_t j;
134         void *temp;
135         struct agp_bridge_data *bridge;
136
137         bridge = mem->bridge;
138         if (!bridge)
139                 return -EINVAL;
140
141         temp = bridge->current_size;
142
143         switch (bridge->driver->size_type) {
144         case U8_APER_SIZE:
145                 num_entries = A_SIZE_8(temp)->num_entries;
146                 break;
147         case U16_APER_SIZE:
148                 num_entries = A_SIZE_16(temp)->num_entries;
149                 break;
150         case U32_APER_SIZE:
151                 num_entries = A_SIZE_32(temp)->num_entries;
152                 break;
153         case FIXED_APER_SIZE:
154                 num_entries = A_SIZE_FIX(temp)->num_entries;
155                 break;
156         case LVL2_APER_SIZE:
157                 return -EINVAL;
158                 break;
159         default:
160                 num_entries = 0;
161                 break;
162         }
163
164         num_entries -= agp_memory_reserved / PAGE_SIZE;
165         if (num_entries < 0)
166                 num_entries = 0;
167
168         if (type != 0 || mem->type != 0) {
169                 return -EINVAL;
170         }
171
172         if ((pg_start + mem->page_count) > num_entries)
173                 return -EINVAL;
174
175         j = pg_start;
176
177         while (j < (pg_start + mem->page_count)) {
178                 if (*(bridge->gatt_table + j))
179                         return -EBUSY;
180                 j++;
181         }
182
183         if (mem->is_flushed == FALSE) {
184                 bridge->driver->cache_flush();
185                 mem->is_flushed = TRUE;
186         }
187
188         for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
189                 *(bridge->gatt_table + j) =
190                     bridge->driver->mask_memory(bridge, mem->memory[i],
191                                                 mem->type);
192         }
193
194         bridge->driver->tlb_flush(mem);
195         return 0;
196 }
197
198 static int sgi_tioca_remove_memory(struct agp_memory *mem, off_t pg_start,
199                                    int type)
200 {
201         size_t i;
202         struct agp_bridge_data *bridge;
203
204         bridge = mem->bridge;
205         if (!bridge)
206                 return -EINVAL;
207
208         if (type != 0 || mem->type != 0) {
209                 return -EINVAL;
210         }
211
212         for (i = pg_start; i < (mem->page_count + pg_start); i++) {
213                 *(bridge->gatt_table + i) = 0;
214         }
215
216         bridge->driver->tlb_flush(mem);
217         return 0;
218 }
219
220 static void sgi_tioca_cache_flush(void)
221 {
222 }
223
224 /*
225  * Cleanup.  Nothing to do as the CA driver owns the GART.
226  */
227
228 static void sgi_tioca_cleanup(void)
229 {
230 }
231
232 static struct agp_bridge_data *sgi_tioca_find_bridge(struct pci_dev *pdev)
233 {
234         struct agp_bridge_data *bridge;
235
236         list_for_each_entry(bridge, &agp_bridges, list) {
237                 if (bridge->dev->bus == pdev->bus)
238                         break;
239         }
240         return bridge;
241 }
242
243 struct agp_bridge_driver sgi_tioca_driver = {
244         .owner = THIS_MODULE,
245         .size_type = U16_APER_SIZE,
246         .configure = sgi_tioca_configure,
247         .fetch_size = sgi_tioca_fetch_size,
248         .cleanup = sgi_tioca_cleanup,
249         .tlb_flush = sgi_tioca_tlbflush,
250         .mask_memory = sgi_tioca_mask_memory,
251         .agp_enable = sgi_tioca_agp_enable,
252         .cache_flush = sgi_tioca_cache_flush,
253         .create_gatt_table = sgi_tioca_create_gatt_table,
254         .free_gatt_table = sgi_tioca_free_gatt_table,
255         .insert_memory = sgi_tioca_insert_memory,
256         .remove_memory = sgi_tioca_remove_memory,
257         .alloc_by_type = agp_generic_alloc_by_type,
258         .free_by_type = agp_generic_free_by_type,
259         .agp_alloc_page = sgi_tioca_alloc_page,
260         .agp_destroy_page = agp_generic_destroy_page,
261         .cant_use_aperture = 1,
262         .needs_scratch_page = 0,
263         .num_aperture_sizes = 1,
264 };
265
266 static int __devinit agp_sgi_init(void)
267 {
268         unsigned int j;
269         struct tioca_kernel *info;
270         struct pci_dev *pdev = NULL;
271
272         if (tioca_gart_found)
273                 printk(KERN_INFO PFX "SGI TIO CA GART driver initialized.\n");
274         else
275                 return 0;
276
277         sgi_tioca_agp_bridges =
278             (struct agp_bridge_data **)kmalloc(tioca_gart_found *
279                                                sizeof(struct agp_bridge_data *),
280                                                GFP_KERNEL);
281
282         j = 0;
283         list_for_each_entry(info, &tioca_list, ca_list) {
284                 struct list_head *tmp;
285                 list_for_each(tmp, info->ca_devices) {
286                         u8 cap_ptr;
287                         pdev = pci_dev_b(tmp);
288                         if (pdev->class != (PCI_CLASS_DISPLAY_VGA << 8))
289                                 continue;
290                         cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
291                         if (!cap_ptr)
292                                 continue;
293                 }
294                 sgi_tioca_agp_bridges[j] = agp_alloc_bridge();
295                 printk(KERN_INFO PFX "bridge %d = 0x%p\n", j,
296                        sgi_tioca_agp_bridges[j]);
297                 if (sgi_tioca_agp_bridges[j]) {
298                         sgi_tioca_agp_bridges[j]->dev = pdev;
299                         sgi_tioca_agp_bridges[j]->dev_private_data = info;
300                         sgi_tioca_agp_bridges[j]->driver = &sgi_tioca_driver;
301                         sgi_tioca_agp_bridges[j]->gart_bus_addr =
302                             info->ca_gfxap_base;
303                         sgi_tioca_agp_bridges[j]->mode = (0x7D << 24) | /* 126 requests */
304                             (0x1 << 9) |        /* SBA supported */
305                             (0x1 << 5) |        /* 64-bit addresses supported */
306                             (0x1 << 4) |        /* FW supported */
307                             (0x1 << 3) |        /* AGP 3.0 mode */
308                             0x2;        /* 8x transfer only */
309                         sgi_tioca_agp_bridges[j]->current_size =
310                             sgi_tioca_agp_bridges[j]->previous_size =
311                             (void *)&sgi_tioca_sizes[0];
312                         agp_add_bridge(sgi_tioca_agp_bridges[j]);
313                 }
314                 j++;
315         }
316
317         agp_find_bridge = &sgi_tioca_find_bridge;
318         return 0;
319 }
320
321 static void __devexit agp_sgi_cleanup(void)
322 {
323         if(sgi_tioca_agp_bridges)
324                 kfree(sgi_tioca_agp_bridges);
325         sgi_tioca_agp_bridges=NULL;
326 }
327
328 module_init(agp_sgi_init);
329 module_exit(agp_sgi_cleanup);
330
331 MODULE_LICENSE("GPL and additional rights");