xref: /openbmc/linux/drivers/char/agp/alpha-agp.c (revision 8730046c)
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 int alpha_core_agp_vm_fault(struct vm_area_struct *vma,
15 					struct vm_fault *vmf)
16 {
17 	alpha_agp_info *agp = agp_bridge->dev_private_data;
18 	dma_addr_t dma_addr;
19 	unsigned long pa;
20 	struct page *page;
21 
22 	dma_addr = vmf->address - vma->vm_start + agp->aperture.bus_base;
23 	pa = agp->ops->translate(agp, dma_addr);
24 
25 	if (pa == (unsigned long)-EINVAL)
26 		return VM_FAULT_SIGBUS;	/* 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 	vmf->page = page;
34 	return 0;
35 }
36 
37 static struct aper_size_info_fixed alpha_core_agp_sizes[] =
38 {
39 	{ 0, 0, 0 }, /* filled in by alpha_core_agp_setup */
40 };
41 
42 static const struct vm_operations_struct alpha_core_agp_vm_ops = {
43 	.fault = alpha_core_agp_vm_fault,
44 };
45 
46 
47 static int alpha_core_agp_fetch_size(void)
48 {
49 	return alpha_core_agp_sizes[0].size;
50 }
51 
52 static int alpha_core_agp_configure(void)
53 {
54 	alpha_agp_info *agp = agp_bridge->dev_private_data;
55 	agp_bridge->gart_bus_addr = agp->aperture.bus_base;
56 	return 0;
57 }
58 
59 static void alpha_core_agp_cleanup(void)
60 {
61 	alpha_agp_info *agp = agp_bridge->dev_private_data;
62 
63 	agp->ops->cleanup(agp);
64 }
65 
66 static void alpha_core_agp_tlbflush(struct agp_memory *mem)
67 {
68 	alpha_agp_info *agp = agp_bridge->dev_private_data;
69 	alpha_mv.mv_pci_tbi(agp->hose, 0, -1);
70 }
71 
72 static void alpha_core_agp_enable(struct agp_bridge_data *bridge, u32 mode)
73 {
74 	alpha_agp_info *agp = bridge->dev_private_data;
75 
76 	agp->mode.lw = agp_collect_device_status(bridge, mode,
77 					agp->capability.lw);
78 
79 	agp->mode.bits.enable = 1;
80 	agp->ops->configure(agp);
81 
82 	agp_device_command(agp->mode.lw, false);
83 }
84 
85 static int alpha_core_agp_insert_memory(struct agp_memory *mem, off_t pg_start,
86 					int type)
87 {
88 	alpha_agp_info *agp = agp_bridge->dev_private_data;
89 	int num_entries, status;
90 	void *temp;
91 
92 	if (type >= AGP_USER_TYPES || mem->type >= AGP_USER_TYPES)
93 		return -EINVAL;
94 
95 	temp = agp_bridge->current_size;
96 	num_entries = A_SIZE_FIX(temp)->num_entries;
97 	if ((pg_start + mem->page_count) > num_entries)
98 		return -EINVAL;
99 
100 	status = agp->ops->bind(agp, pg_start, mem);
101 	mb();
102 	alpha_core_agp_tlbflush(mem);
103 
104 	return status;
105 }
106 
107 static int alpha_core_agp_remove_memory(struct agp_memory *mem, off_t pg_start,
108 					int type)
109 {
110 	alpha_agp_info *agp = agp_bridge->dev_private_data;
111 	int status;
112 
113 	status = agp->ops->unbind(agp, pg_start, mem);
114 	alpha_core_agp_tlbflush(mem);
115 	return status;
116 }
117 
118 static int alpha_core_agp_create_free_gatt_table(struct agp_bridge_data *a)
119 {
120 	return 0;
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	= true,
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_create_free_gatt_table,
139 	.free_gatt_table	= alpha_core_agp_create_free_gatt_table,
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_alloc_pages	= agp_generic_alloc_pages,
146 	.agp_destroy_page	= agp_generic_destroy_page,
147 	.agp_destroy_pages	= agp_generic_destroy_pages,
148 	.agp_type_to_mask_type  = agp_generic_type_to_mask_type,
149 };
150 
151 struct agp_bridge_data *alpha_bridge;
152 
153 int __init
154 alpha_core_agp_setup(void)
155 {
156 	alpha_agp_info *agp = alpha_mv.agp_info();
157 	struct pci_dev *pdev;	/* faked */
158 	struct aper_size_info_fixed *aper_size;
159 
160 	if (!agp)
161 		return -ENODEV;
162 	if (agp->ops->setup(agp))
163 		return -ENODEV;
164 
165 	/*
166 	 * Build the aperture size descriptor
167 	 */
168 	aper_size = alpha_core_agp_sizes;
169 	aper_size->size = agp->aperture.size / (1024 * 1024);
170 	aper_size->num_entries = agp->aperture.size / PAGE_SIZE;
171 	aper_size->page_order = __ffs(aper_size->num_entries / 1024);
172 
173 	/*
174 	 * Build a fake pci_dev struct
175 	 */
176 	pdev = pci_alloc_dev(NULL);
177 	if (!pdev)
178 		return -ENOMEM;
179 	pdev->vendor = 0xffff;
180 	pdev->device = 0xffff;
181 	pdev->sysdata = agp->hose;
182 
183 	alpha_bridge = agp_alloc_bridge();
184 	if (!alpha_bridge)
185 		goto fail;
186 
187 	alpha_bridge->driver = &alpha_core_agp_driver;
188 	alpha_bridge->vm_ops = &alpha_core_agp_vm_ops;
189 	alpha_bridge->current_size = aper_size; /* only 1 size */
190 	alpha_bridge->dev_private_data = agp;
191 	alpha_bridge->dev = pdev;
192 	alpha_bridge->mode = agp->capability.lw;
193 
194 	printk(KERN_INFO PFX "Detected AGP on hose %d\n", agp->hose->index);
195 	return agp_add_bridge(alpha_bridge);
196 
197  fail:
198 	kfree(pdev);
199 	return -ENOMEM;
200 }
201 
202 static int __init agp_alpha_core_init(void)
203 {
204 	if (agp_off)
205 		return -EINVAL;
206 	if (alpha_mv.agp_info)
207 		return alpha_core_agp_setup();
208 	return -ENODEV;
209 }
210 
211 static void __exit agp_alpha_core_cleanup(void)
212 {
213 	agp_remove_bridge(alpha_bridge);
214 	agp_put_bridge(alpha_bridge);
215 }
216 
217 module_init(agp_alpha_core_init);
218 module_exit(agp_alpha_core_cleanup);
219 
220 MODULE_AUTHOR("Jeff Wiedemeier <Jeff.Wiedemeier@hp.com>");
221 MODULE_LICENSE("GPL and additional rights");
222