1 /* agpsupport.c -- DRM support for AGP/GART backend -*- linux-c -*-
2  * Created: Mon Dec 13 09:56:45 1999 by faith@precisioninsight.com
3  *
4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26  *
27  * Author: Rickard E. (Rik) Faith <faith@valinux.com>
28  *
29  */
30 
31 #define __NO_VERSION__
32 #include "drmP.h"
33 #include <linux/module.h>
34 #if LINUX_VERSION_CODE < 0x020400
35 #include "agpsupport-pre24.h"
36 #else
37 #define DRM_AGP_GET (drm_agp_t *)inter_module_get("drm_agp")
38 #define DRM_AGP_PUT inter_module_put("drm_agp")
39 #endif
40 
41 static const drm_agp_t *drm_agp = NULL;
42 
drm_agp_info(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)43 int drm_agp_info(struct inode *inode, struct file *filp, unsigned int cmd,
44 		 unsigned long arg)
45 {
46 	drm_file_t	 *priv	 = filp->private_data;
47 	drm_device_t	 *dev	 = priv->dev;
48 	agp_kern_info    *kern;
49 	drm_agp_info_t   info;
50 
51 	if (!dev->agp->acquired || !drm_agp->copy_info) return -EINVAL;
52 
53 	kern                   = &dev->agp->agp_info;
54 	info.agp_version_major = kern->version.major;
55 	info.agp_version_minor = kern->version.minor;
56 	info.mode              = kern->mode;
57 	info.aperture_base     = kern->aper_base;
58 	info.aperture_size     = kern->aper_size * 1024 * 1024;
59 	info.memory_allowed    = kern->max_memory << PAGE_SHIFT;
60 	info.memory_used       = kern->current_memory << PAGE_SHIFT;
61 	info.id_vendor         = kern->device->vendor;
62 	info.id_device         = kern->device->device;
63 
64 	if (copy_to_user((drm_agp_info_t *)arg, &info, sizeof(info)))
65 		return -EFAULT;
66 	return 0;
67 }
68 
drm_agp_acquire(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)69 int drm_agp_acquire(struct inode *inode, struct file *filp, unsigned int cmd,
70 		    unsigned long arg)
71 {
72 	drm_file_t	 *priv	 = filp->private_data;
73 	drm_device_t	 *dev	 = priv->dev;
74 	int              retcode;
75 
76 	if (dev->agp->acquired || !drm_agp->acquire) return -EINVAL;
77 	if ((retcode = drm_agp->acquire())) return retcode;
78 	dev->agp->acquired = 1;
79 	return 0;
80 }
81 
drm_agp_release(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)82 int drm_agp_release(struct inode *inode, struct file *filp, unsigned int cmd,
83 		    unsigned long arg)
84 {
85 	drm_file_t	 *priv	 = filp->private_data;
86 	drm_device_t	 *dev	 = priv->dev;
87 
88 	if (!dev->agp->acquired || !drm_agp->release) return -EINVAL;
89 	drm_agp->release();
90 	dev->agp->acquired = 0;
91 	return 0;
92 
93 }
94 
_drm_agp_release(void)95 void _drm_agp_release(void)
96 {
97 	if (drm_agp->release) drm_agp->release();
98 }
99 
drm_agp_enable(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)100 int drm_agp_enable(struct inode *inode, struct file *filp, unsigned int cmd,
101 		   unsigned long arg)
102 {
103 	drm_file_t	 *priv	 = filp->private_data;
104 	drm_device_t	 *dev	 = priv->dev;
105 	drm_agp_mode_t   mode;
106 
107 	if (!dev->agp->acquired || !drm_agp->enable) return -EINVAL;
108 
109 	if (copy_from_user(&mode, (drm_agp_mode_t *)arg, sizeof(mode)))
110 		return -EFAULT;
111 
112 	dev->agp->mode    = mode.mode;
113 	drm_agp->enable(mode.mode);
114 	dev->agp->base    = dev->agp->agp_info.aper_base;
115 	dev->agp->enabled = 1;
116 	return 0;
117 }
118 
drm_agp_alloc(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)119 int drm_agp_alloc(struct inode *inode, struct file *filp, unsigned int cmd,
120 		  unsigned long arg)
121 {
122 	drm_file_t	 *priv	 = filp->private_data;
123 	drm_device_t	 *dev	 = priv->dev;
124 	drm_agp_buffer_t request;
125 	drm_agp_mem_t    *entry;
126 	agp_memory       *memory;
127 	unsigned long    pages;
128 	u32 		 type;
129 	if (!dev->agp->acquired) return -EINVAL;
130 	if (copy_from_user(&request, (drm_agp_buffer_t *)arg, sizeof(request)))
131 		return -EFAULT;
132 	if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
133 		return -ENOMEM;
134 
135    	memset(entry, 0, sizeof(*entry));
136 
137 	pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
138 	type = (u32) request.type;
139 
140 	if (!(memory = drm_alloc_agp(pages, type))) {
141 		drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
142 		return -ENOMEM;
143 	}
144 
145 	entry->handle    = (unsigned long)memory->memory;
146 	entry->memory    = memory;
147 	entry->bound     = 0;
148 	entry->pages     = pages;
149 	entry->prev      = NULL;
150 	entry->next      = dev->agp->memory;
151 	if (dev->agp->memory) dev->agp->memory->prev = entry;
152 	dev->agp->memory = entry;
153 
154 	request.handle   = entry->handle;
155         request.physical = memory->physical;
156 
157 	if (copy_to_user((drm_agp_buffer_t *)arg, &request, sizeof(request))) {
158 		dev->agp->memory       = entry->next;
159 		dev->agp->memory->prev = NULL;
160 		drm_free_agp(memory, pages);
161 		drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
162 		return -EFAULT;
163 	}
164 	return 0;
165 }
166 
drm_agp_lookup_entry(drm_device_t * dev,unsigned long handle)167 static drm_agp_mem_t *drm_agp_lookup_entry(drm_device_t *dev,
168 					   unsigned long handle)
169 {
170 	drm_agp_mem_t *entry;
171 
172 	for (entry = dev->agp->memory; entry; entry = entry->next) {
173 		if (entry->handle == handle) return entry;
174 	}
175 	return NULL;
176 }
177 
drm_agp_unbind(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)178 int drm_agp_unbind(struct inode *inode, struct file *filp, unsigned int cmd,
179 		   unsigned long arg)
180 {
181 	drm_file_t	  *priv	 = filp->private_data;
182 	drm_device_t	  *dev	 = priv->dev;
183 	drm_agp_binding_t request;
184 	drm_agp_mem_t     *entry;
185 
186 	if (!dev->agp->acquired) return -EINVAL;
187 	if (copy_from_user(&request, (drm_agp_binding_t *)arg, sizeof(request)))
188 		return -EFAULT;
189 	if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
190 		return -EINVAL;
191 	if (!entry->bound) return -EINVAL;
192 	return drm_unbind_agp(entry->memory);
193 }
194 
drm_agp_bind(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)195 int drm_agp_bind(struct inode *inode, struct file *filp, unsigned int cmd,
196 		 unsigned long arg)
197 {
198 	drm_file_t	  *priv	 = filp->private_data;
199 	drm_device_t	  *dev	 = priv->dev;
200 	drm_agp_binding_t request;
201 	drm_agp_mem_t     *entry;
202 	int               retcode;
203 	int               page;
204 
205 	if (!dev->agp->acquired || !drm_agp->bind_memory) return -EINVAL;
206 	if (copy_from_user(&request, (drm_agp_binding_t *)arg, sizeof(request)))
207 		return -EFAULT;
208 	if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
209 		return -EINVAL;
210 	if (entry->bound) return -EINVAL;
211 	page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
212 	if ((retcode = drm_bind_agp(entry->memory, page))) return retcode;
213 	entry->bound = dev->agp->base + (page << PAGE_SHIFT);
214 	DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
215 		  dev->agp->base, entry->bound);
216 	return 0;
217 }
218 
drm_agp_free(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)219 int drm_agp_free(struct inode *inode, struct file *filp, unsigned int cmd,
220 		 unsigned long arg)
221 {
222 	drm_file_t	 *priv	 = filp->private_data;
223 	drm_device_t	 *dev	 = priv->dev;
224 	drm_agp_buffer_t request;
225 	drm_agp_mem_t    *entry;
226 
227 	if (!dev->agp->acquired) return -EINVAL;
228 	if (copy_from_user(&request, (drm_agp_buffer_t *)arg, sizeof(request)))
229 		return -EFAULT;
230 	if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
231 		return -EINVAL;
232 	if (entry->bound) drm_unbind_agp(entry->memory);
233 
234 	if (entry->prev) entry->prev->next = entry->next;
235 	else             dev->agp->memory  = entry->next;
236 	if (entry->next) entry->next->prev = entry->prev;
237 	drm_free_agp(entry->memory, entry->pages);
238 	drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
239 	return 0;
240 }
241 
drm_agp_init(void)242 drm_agp_head_t *drm_agp_init(void)
243 {
244 	drm_agp_head_t *head         = NULL;
245 
246 	drm_agp = DRM_AGP_GET;
247 	if (drm_agp) {
248 		if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
249 			return NULL;
250 		memset((void *)head, 0, sizeof(*head));
251 		drm_agp->copy_info(&head->agp_info);
252 		if (head->agp_info.chipset == NOT_SUPPORTED) {
253 			drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
254 			return NULL;
255 		}
256 		head->memory = NULL;
257 		switch (head->agp_info.chipset) {
258 		case INTEL_GENERIC:	head->chipset = "Intel";         break;
259 		case INTEL_LX:		head->chipset = "Intel 440LX";   break;
260 		case INTEL_BX:		head->chipset = "Intel 440BX";   break;
261 		case INTEL_GX:		head->chipset = "Intel 440GX";   break;
262 		case INTEL_I810:	head->chipset = "Intel i810";    break;
263 
264 #if LINUX_VERSION_CODE >= 0x020400
265 		case INTEL_I840:	head->chipset = "Intel i840";    break;
266 #endif
267 		case INTEL_460GX:	head->chipset = "Intel 460GX";	 break;
268 
269 		case VIA_GENERIC:	head->chipset = "VIA";           break;
270 		case VIA_VP3:		head->chipset = "VIA VP3";       break;
271 		case VIA_MVP3:		head->chipset = "VIA MVP3";      break;
272 
273 #if LINUX_VERSION_CODE >= 0x020400
274 		case VIA_MVP4:		head->chipset = "VIA MVP4";      break;
275 		case VIA_APOLLO_KX133:	head->chipset = "VIA Apollo KX133";
276 			break;
277 		case VIA_APOLLO_KT133:	head->chipset = "VIA Apollo KT133";
278 			break;
279 		case VIA_APOLLO_KM266:	head->chipset = "VIA Apollo KM266 / KL266";
280 			break;
281 		case VIA_APOLLO_KT400:  head->chipset = "VIA Apollo KT400";
282 			break;
283 		case VIA_APOLLO_P4X400:	head->chipset = "VIA Apollo P4X400";
284 			break;
285 #endif
286 
287 		case VIA_APOLLO_PRO: 	head->chipset = "VIA Apollo Pro";
288 			break;
289 		case SIS_GENERIC:	head->chipset = "SiS";           break;
290 		case AMD_GENERIC:	head->chipset = "AMD";           break;
291 		case AMD_IRONGATE:	head->chipset = "AMD Irongate";  break;
292 		case ALI_GENERIC:	head->chipset = "ALi";           break;
293 		case ALI_M1541: 	head->chipset = "ALi M1541";     break;
294 		case ALI_M1621: 	head->chipset = "ALi M1621";	 break;
295 		case ALI_M1631: 	head->chipset = "ALi M1631";	 break;
296 		case ALI_M1632: 	head->chipset = "ALi M1632";	 break;
297 		case ALI_M1641: 	head->chipset = "ALi M1641";	 break;
298 		case ALI_M1647: 	head->chipset = "ALi M1647";	 break;
299 		case ALI_M1651: 	head->chipset = "ALi M1651";	 break;
300 		case SVWRKS_GENERIC:	head->chipset = "Serverworks Generic";
301 			break;
302 		case SVWRKS_HE: 	head->chipset = "Serverworks HE"; break;
303 		case SVWRKS_LE: 	head->chipset = "Serverworks LE"; break;
304 		case HP_ZX1:		head->chipset = "HP ZX1";	break;
305 
306 		default:		head->chipset = "Unknown";       break;
307 		}
308 		DRM_INFO("AGP %d.%d on %s @ 0x%08lx %ZuMB\n",
309 			 head->agp_info.version.major,
310 			 head->agp_info.version.minor,
311 			 head->chipset,
312 			 head->agp_info.aper_base,
313 			 head->agp_info.aper_size);
314 	}
315 	return head;
316 }
317 
drm_agp_uninit(void)318 void drm_agp_uninit(void)
319 {
320 	DRM_AGP_PUT;
321 	drm_agp = NULL;
322 }
323 
drm_agp_allocate_memory(size_t pages,u32 type)324 agp_memory *drm_agp_allocate_memory(size_t pages, u32 type)
325 {
326 	if (!drm_agp->allocate_memory) return NULL;
327 	return drm_agp->allocate_memory(pages, type);
328 }
329 
drm_agp_free_memory(agp_memory * handle)330 int drm_agp_free_memory(agp_memory *handle)
331 {
332 	if (!handle || !drm_agp->free_memory) return 0;
333 	drm_agp->free_memory(handle);
334 	return 1;
335 }
336 
drm_agp_bind_memory(agp_memory * handle,off_t start)337 int drm_agp_bind_memory(agp_memory *handle, off_t start)
338 {
339 	if (!handle || !drm_agp->bind_memory) return -EINVAL;
340 	return drm_agp->bind_memory(handle, start);
341 }
342 
drm_agp_unbind_memory(agp_memory * handle)343 int drm_agp_unbind_memory(agp_memory *handle)
344 {
345 	if (!handle || !drm_agp->unbind_memory) return -EINVAL;
346 	return drm_agp->unbind_memory(handle);
347 }
348