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