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