ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / agp / alpha-agp.c
1 #include <linux/module.h>
2 #include <linux/pci.h>
3 #include <linux/init.h>
4 #include <linux/agp_backend.h>
5 #include <linux/mm.h>
6 #include <linux/slab.h>
7
8 #include <asm/machvec.h>
9 #include <asm/agp_backend.h>
10 #include "../../../arch/alpha/kernel/pci_impl.h"
11
12 #include "agp.h"
13
14 static struct page *alpha_core_agp_vm_nopage(struct vm_area_struct *vma,
15                                              unsigned long address,
16                                              int *type)
17 {
18         alpha_agp_info *agp = agp_bridge->dev_private_data;
19         dma_addr_t dma_addr;
20         unsigned long pa;
21         struct page *page;
22
23         dma_addr = address - vma->vm_start + agp->aperture.bus_base;
24         pa = agp->ops->translate(agp, dma_addr);
25
26         if (pa == (unsigned long)-EINVAL) return NULL;  /* no translation */
27         
28         /*
29          * Get the page, inc the use count, and return it
30          */
31         page = virt_to_page(__va(pa));
32         get_page(page);
33         if (type)
34                 *type = VM_FAULT_MINOR;
35         return page;
36 }
37
38 static struct aper_size_info_fixed alpha_core_agp_sizes[] =
39 {
40         { 0, 0, 0 }, /* filled in by alpha_core_agp_setup */
41 };
42
43 struct vm_operations_struct alpha_core_agp_vm_ops = {
44         .nopage = alpha_core_agp_vm_nopage,
45 };
46
47
48 static int alpha_core_agp_nop(void)
49 {
50         /* just return success */
51         return 0;
52 }
53
54 static int alpha_core_agp_fetch_size(void)
55 {
56         return alpha_core_agp_sizes[0].size;
57 }
58
59 static int alpha_core_agp_configure(void)
60 {
61         alpha_agp_info *agp = agp_bridge->dev_private_data;
62         agp_bridge->gart_bus_addr = agp->aperture.bus_base;
63         return 0;
64 }
65
66 static void alpha_core_agp_cleanup(void)
67 {
68         alpha_agp_info *agp = agp_bridge->dev_private_data;
69
70         agp->ops->cleanup(agp);
71 }
72
73 static void alpha_core_agp_tlbflush(struct agp_memory *mem)
74 {
75         alpha_agp_info *agp = agp_bridge->dev_private_data;
76         alpha_mv.mv_pci_tbi(agp->hose, 0, -1);
77 }
78
79 static void alpha_core_agp_enable(u32 mode)
80 {
81         alpha_agp_info *agp = agp_bridge->dev_private_data;
82
83         agp->mode.lw = agp_collect_device_status(mode, agp->capability.lw);
84
85         agp->mode.bits.enable = 1;
86         agp->ops->configure(agp);
87
88         agp_device_command(agp->mode.lw, 0);
89 }
90
91 static int alpha_core_agp_insert_memory(struct agp_memory *mem, off_t pg_start, 
92                                         int type)
93 {
94         alpha_agp_info *agp = agp_bridge->dev_private_data;
95         int num_entries, status;
96         void *temp;
97
98         temp = agp_bridge->current_size;
99         num_entries = A_SIZE_FIX(temp)->num_entries;
100         if ((pg_start + mem->page_count) > num_entries) return -EINVAL;
101
102         status = agp->ops->bind(agp, pg_start, mem);
103         mb();
104         alpha_core_agp_tlbflush(mem);
105
106         return status;
107 }
108
109 static int alpha_core_agp_remove_memory(struct agp_memory *mem, off_t pg_start, 
110                                         int type)
111 {
112         alpha_agp_info *agp = agp_bridge->dev_private_data;
113         int status;
114
115         status = agp->ops->unbind(agp, pg_start, mem);
116         alpha_core_agp_tlbflush(mem);
117         return status;
118 }
119
120 struct agp_bridge_driver alpha_core_agp_driver = {
121         .owner                  = THIS_MODULE,
122         .aperture_sizes         = alpha_core_agp_sizes,
123         .num_aperture_sizes     = 1,
124         .size_type              = FIXED_APER_SIZE,
125         .cant_use_aperture      = 1,
126         .masks                  = NULL,
127         
128         .fetch_size             = alpha_core_agp_fetch_size,
129         .configure              = alpha_core_agp_configure,
130         .agp_enable             = alpha_core_agp_enable,
131         .cleanup                = alpha_core_agp_cleanup,
132         .tlb_flush              = alpha_core_agp_tlbflush,
133         .mask_memory            = agp_generic_mask_memory,
134         .cache_flush            = global_cache_flush,
135         .create_gatt_table      = alpha_core_agp_nop,
136         .free_gatt_table        = alpha_core_agp_nop,
137         .insert_memory          = alpha_core_agp_insert_memory,
138         .remove_memory          = alpha_core_agp_remove_memory,
139         .alloc_by_type          = agp_generic_alloc_by_type,
140         .free_by_type           = agp_generic_free_by_type,
141         .agp_alloc_page         = agp_generic_alloc_page,
142         .agp_destroy_page       = agp_generic_destroy_page,
143 };
144
145 struct agp_bridge_data *alpha_bridge;
146
147 int __init
148 alpha_core_agp_setup(void)
149 {
150         alpha_agp_info *agp = alpha_mv.agp_info();
151         struct pci_dev *pdev;   /* faked */
152         struct aper_size_info_fixed *aper_size;
153
154         if (!agp)
155                 return -ENODEV;
156         if (agp->ops->setup(agp))
157                 return -ENODEV;
158
159         /*
160          * Build the aperture size descriptor
161          */
162         aper_size = alpha_core_agp_sizes;
163         aper_size->size = agp->aperture.size / (1024 * 1024);
164         aper_size->num_entries = agp->aperture.size / PAGE_SIZE;
165         aper_size->page_order = __ffs(aper_size->num_entries / 1024);
166
167         /*
168          * Build a fake pci_dev struct
169          */
170         pdev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
171         if (!pdev)
172                 return -ENOMEM;
173         pdev->vendor = 0xffff;
174         pdev->device = 0xffff;
175         pdev->sysdata = agp->hose;
176
177         alpha_bridge = agp_alloc_bridge();
178         if (!alpha_bridge)
179                 goto fail;
180
181         alpha_bridge->driver = &alpha_core_agp_driver;
182         alpha_bridge->vm_ops = &alpha_core_agp_vm_ops;
183         alpha_bridge->current_size = aper_size; /* only 1 size */
184         alpha_bridge->dev_private_data = agp;
185         alpha_bridge->dev = pdev;
186         alpha_bridge->mode = agp->capability.lw;
187
188         printk(KERN_INFO PFX "Detected AGP on hose %d\n", agp->hose->index);
189         return agp_add_bridge(alpha_bridge);
190
191  fail:
192         kfree(pdev);
193         return -ENOMEM;
194 }
195
196 static int __init agp_alpha_core_init(void)
197 {
198         if (alpha_mv.agp_info)
199                 return alpha_core_agp_setup();
200         return -ENODEV;
201 }
202
203 static void __exit agp_alpha_core_cleanup(void)
204 {
205         agp_remove_bridge(alpha_bridge);
206         agp_put_bridge(alpha_bridge);
207 }
208
209 module_init(agp_alpha_core_init);
210 module_exit(agp_alpha_core_cleanup);
211
212 MODULE_AUTHOR("Jeff Wiedemeier <Jeff.Wiedemeier@hp.com>");
213 MODULE_LICENSE("GPL and additional rights");