xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/drm_agpsupport.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /**
2  * \file drm_agpsupport.c
3  * DRM support for AGP/GART backend
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  */
8 
9 /*
10  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12  * All Rights Reserved.
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the "Software"),
16  * to deal in the Software without restriction, including without limitation
17  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18  * and/or sell copies of the Software, and to permit persons to whom the
19  * Software is furnished to do so, subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice (including the next
22  * paragraph) shall be included in all copies or substantial portions of the
23  * Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
28  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31  * OTHER DEALINGS IN THE SOFTWARE.
32  */
33 
34 #include <drm/drmP.h>
35 #include <linux/errno.h>
36 #include <linux/export.h>
37 #include <linux/module.h>
38 #include <linux/slab.h>
39 
40 #if __OS_HAS_AGP
41 
42 #include <asm/agp.h>
43 
44 /**
45  * Get AGP information.
46  *
47  * \param inode device inode.
48  * \param file_priv DRM file private.
49  * \param cmd command.
50  * \param arg pointer to a (output) drm_agp_info structure.
51  * \return zero on success or a negative number on failure.
52  *
53  * Verifies the AGP device has been initialized and acquired and fills in the
54  * drm_agp_info structure with the information in drm_agp_head::agp_info.
55  */
56 int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
57 {
58 	DRM_AGP_KERN *kern;
59 
60 	if (!dev->agp || !dev->agp->acquired)
61 		return -EINVAL;
62 
63 	kern = &dev->agp->agp_info;
64 #if __NetBSD__
65 	info->agp_version_major = 1;
66 	info->agp_version_minor = 0;
67 	info->mode = kern->ai_mode;
68 	info->aperture_base = kern->ai_aperture_base;
69 	info->aperture_size = kern->ai_aperture_size;
70 	info->memory_allowed = kern->ai_memory_allowed;
71 	info->memory_used = kern->ai_memory_used;
72 	info->id_vendor = PCI_VENDOR(kern->ai_devid);
73 	info->id_device = PCI_PRODUCT(kern->ai_devid);
74 #else
75 	info->agp_version_major = kern->version.major;
76 	info->agp_version_minor = kern->version.minor;
77 	info->mode = kern->mode;
78 	info->aperture_base = kern->aper_base;
79 	info->aperture_size = kern->aper_size * 1024 * 1024;
80 	info->memory_allowed = kern->max_memory << PAGE_SHIFT;
81 	info->memory_used = kern->current_memory << PAGE_SHIFT;
82 	info->id_vendor = kern->device->vendor;
83 	info->id_device = kern->device->device;
84 #endif
85 
86 	return 0;
87 }
88 
89 EXPORT_SYMBOL(drm_agp_info);
90 
91 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
92 		       struct drm_file *file_priv)
93 {
94 	struct drm_agp_info *info = data;
95 	int err;
96 
97 	err = drm_agp_info(dev, info);
98 	if (err)
99 		return err;
100 
101 	return 0;
102 }
103 
104 /**
105  * Acquire the AGP device.
106  *
107  * \param dev DRM device that is to acquire AGP.
108  * \return zero on success or a negative number on failure.
109  *
110  * Verifies the AGP device hasn't been acquired before and calls
111  * \c agp_backend_acquire.
112  */
113 int drm_agp_acquire(struct drm_device * dev)
114 {
115 	if (!dev->agp)
116 		return -ENODEV;
117 	if (dev->agp->acquired)
118 		return -EBUSY;
119 	if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev)))
120 		return -ENODEV;
121 	dev->agp->acquired = 1;
122 	return 0;
123 }
124 
125 EXPORT_SYMBOL(drm_agp_acquire);
126 
127 /**
128  * Acquire the AGP device (ioctl).
129  *
130  * \param inode device inode.
131  * \param file_priv DRM file private.
132  * \param cmd command.
133  * \param arg user argument.
134  * \return zero on success or a negative number on failure.
135  *
136  * Verifies the AGP device hasn't been acquired before and calls
137  * \c agp_backend_acquire.
138  */
139 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
140 			  struct drm_file *file_priv)
141 {
142 	return drm_agp_acquire((struct drm_device *) file_priv->minor->dev);
143 }
144 
145 /**
146  * Release the AGP device.
147  *
148  * \param dev DRM device that is to release AGP.
149  * \return zero on success or a negative number on failure.
150  *
151  * Verifies the AGP device has been acquired and calls \c agp_backend_release.
152  */
153 int drm_agp_release(struct drm_device * dev)
154 {
155 	if (!dev->agp || !dev->agp->acquired)
156 		return -EINVAL;
157 	agp_backend_release(dev->agp->bridge);
158 	dev->agp->acquired = 0;
159 	return 0;
160 }
161 EXPORT_SYMBOL(drm_agp_release);
162 
163 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
164 			  struct drm_file *file_priv)
165 {
166 	return drm_agp_release(dev);
167 }
168 
169 /**
170  * Enable the AGP bus.
171  *
172  * \param dev DRM device that has previously acquired AGP.
173  * \param mode Requested AGP mode.
174  * \return zero on success or a negative number on failure.
175  *
176  * Verifies the AGP device has been acquired but not enabled, and calls
177  * \c agp_enable.
178  */
179 int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode)
180 {
181 	if (!dev->agp || !dev->agp->acquired)
182 		return -EINVAL;
183 
184 	dev->agp->mode = mode.mode;
185 	agp_enable(dev->agp->bridge, mode.mode);
186 	dev->agp->enabled = 1;
187 	return 0;
188 }
189 
190 EXPORT_SYMBOL(drm_agp_enable);
191 
192 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
193 			 struct drm_file *file_priv)
194 {
195 	struct drm_agp_mode *mode = data;
196 
197 	return drm_agp_enable(dev, *mode);
198 }
199 
200 /**
201  * Allocate AGP memory.
202  *
203  * \param inode device inode.
204  * \param file_priv file private pointer.
205  * \param cmd command.
206  * \param arg pointer to a drm_agp_buffer structure.
207  * \return zero on success or a negative number on failure.
208  *
209  * Verifies the AGP device is present and has been acquired, allocates the
210  * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
211  */
212 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
213 {
214 	struct drm_agp_mem *entry;
215 	DRM_AGP_MEM *memory;
216 	unsigned long pages;
217 	u32 type;
218 
219 	if (!dev->agp || !dev->agp->acquired)
220 		return -EINVAL;
221 	if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
222 		return -ENOMEM;
223 
224 	memset(entry, 0, sizeof(*entry));
225 
226 	pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
227 	type = (u32) request->type;
228 	if (!(memory = agp_allocate_memory(dev->agp->bridge, pages, type))) {
229 		kfree(entry);
230 		return -ENOMEM;
231 	}
232 
233 #ifdef __NetBSD__
234 	/* I presume the `+ 1' is there to avoid an id of 0 or something.  */
235 	entry->handle = (unsigned long)memory->am_id + 1;
236 #else
237 	entry->handle = (unsigned long)memory->key + 1;
238 #endif
239 	entry->memory = memory;
240 	entry->bound = 0;
241 	entry->pages = pages;
242 	list_add(&entry->head, &dev->agp->memory);
243 
244 	request->handle = entry->handle;
245 #ifdef __NetBSD__
246 	{
247 		struct agp_memory_info info;
248 		agp_memory_info(dev->agp->bridge, memory, &info);
249 		request->physical = info.ami_physical;
250 	}
251 #else
252 	request->physical = memory->physical;
253 #endif
254 
255 	return 0;
256 }
257 EXPORT_SYMBOL(drm_agp_alloc);
258 
259 
260 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
261 			struct drm_file *file_priv)
262 {
263 	struct drm_agp_buffer *request = data;
264 
265 	return drm_agp_alloc(dev, request);
266 }
267 
268 /**
269  * Search for the AGP memory entry associated with a handle.
270  *
271  * \param dev DRM device structure.
272  * \param handle AGP memory handle.
273  * \return pointer to the drm_agp_mem structure associated with \p handle.
274  *
275  * Walks through drm_agp_head::memory until finding a matching handle.
276  */
277 static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
278 					   unsigned long handle)
279 {
280 	struct drm_agp_mem *entry;
281 
282 	list_for_each_entry(entry, &dev->agp->memory, head) {
283 		if (entry->handle == handle)
284 			return entry;
285 	}
286 	return NULL;
287 }
288 
289 /**
290  * Unbind AGP memory from the GATT (ioctl).
291  *
292  * \param inode device inode.
293  * \param file_priv DRM file private.
294  * \param cmd command.
295  * \param arg pointer to a drm_agp_binding structure.
296  * \return zero on success or a negative number on failure.
297  *
298  * Verifies the AGP device is present and acquired, looks-up the AGP memory
299  * entry and passes it to the unbind_agp() function.
300  */
301 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
302 {
303 	struct drm_agp_mem *entry;
304 	int ret;
305 
306 	if (!dev->agp || !dev->agp->acquired)
307 		return -EINVAL;
308 	if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
309 		return -EINVAL;
310 	if (!entry->bound)
311 		return -EINVAL;
312 #ifdef __NetBSD__
313 	ret = drm_unbind_agp(dev->agp->bridge, entry->memory);
314 #else
315 	ret = drm_unbind_agp(entry->memory);
316 #endif
317 	if (ret == 0)
318 		entry->bound = 0;
319 	return ret;
320 }
321 EXPORT_SYMBOL(drm_agp_unbind);
322 
323 
324 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
325 			 struct drm_file *file_priv)
326 {
327 	struct drm_agp_binding *request = data;
328 
329 	return drm_agp_unbind(dev, request);
330 }
331 
332 /**
333  * Bind AGP memory into the GATT (ioctl)
334  *
335  * \param inode device inode.
336  * \param file_priv DRM file private.
337  * \param cmd command.
338  * \param arg pointer to a drm_agp_binding structure.
339  * \return zero on success or a negative number on failure.
340  *
341  * Verifies the AGP device is present and has been acquired and that no memory
342  * is currently bound into the GATT. Looks-up the AGP memory entry and passes
343  * it to bind_agp() function.
344  */
345 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
346 {
347 	struct drm_agp_mem *entry;
348 	int retcode;
349 	int page;
350 
351 	if (!dev->agp || !dev->agp->acquired)
352 		return -EINVAL;
353 	if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
354 		return -EINVAL;
355 	if (entry->bound)
356 		return -EINVAL;
357 	page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
358 #ifdef __NetBSD__
359 	if ((retcode = drm_bind_agp(dev->agp->bridge, entry->memory, page)))
360 		return retcode;
361 #else
362 	if ((retcode = drm_bind_agp(entry->memory, page)))
363 		return retcode;
364 #endif
365 	entry->bound = dev->agp->base + (page << PAGE_SHIFT);
366 	DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
367 		  dev->agp->base, entry->bound);
368 	return 0;
369 }
370 EXPORT_SYMBOL(drm_agp_bind);
371 
372 
373 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
374 		       struct drm_file *file_priv)
375 {
376 	struct drm_agp_binding *request = data;
377 
378 	return drm_agp_bind(dev, request);
379 }
380 
381 /**
382  * Free AGP memory (ioctl).
383  *
384  * \param inode device inode.
385  * \param file_priv DRM file private.
386  * \param cmd command.
387  * \param arg pointer to a drm_agp_buffer structure.
388  * \return zero on success or a negative number on failure.
389  *
390  * Verifies the AGP device is present and has been acquired and looks up the
391  * AGP memory entry. If the memory it's currently bound, unbind it via
392  * unbind_agp(). Frees it via free_agp() as well as the entry itself
393  * and unlinks from the doubly linked list it's inserted in.
394  */
395 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
396 {
397 	struct drm_agp_mem *entry;
398 
399 	if (!dev->agp || !dev->agp->acquired)
400 		return -EINVAL;
401 	if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
402 		return -EINVAL;
403 	if (entry->bound)
404 #ifdef __NetBSD__
405 		drm_unbind_agp(dev->agp->bridge, entry->memory);
406 #else
407 		drm_unbind_agp(entry->memory);
408 #endif
409 
410 	list_del(&entry->head);
411 
412 #ifdef __NetBSD__
413 	drm_free_agp(dev->agp->bridge, entry->memory, entry->pages);
414 #else
415 	drm_free_agp(entry->memory, entry->pages);
416 #endif
417 	kfree(entry);
418 	return 0;
419 }
420 EXPORT_SYMBOL(drm_agp_free);
421 
422 
423 
424 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
425 		       struct drm_file *file_priv)
426 {
427 	struct drm_agp_buffer *request = data;
428 
429 	return drm_agp_free(dev, request);
430 }
431 
432 /**
433  * Initialize the AGP resources.
434  *
435  * \return pointer to a drm_agp_head structure.
436  *
437  * Gets the drm_agp_t structure which is made available by the agpgart module
438  * via the inter_module_* functions. Creates and initializes a drm_agp_head
439  * structure.
440  */
441 struct drm_agp_head *drm_agp_init(struct drm_device *dev)
442 {
443 	struct drm_agp_head *head = NULL;
444 
445 	if (!(head = kmalloc(sizeof(*head), GFP_KERNEL)))
446 		return NULL;
447 	memset((void *)head, 0, sizeof(*head));
448 	head->bridge = agp_find_bridge(dev->pdev);
449 	if (!head->bridge) {
450 		if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
451 			kfree(head);
452 			return NULL;
453 		}
454 		agp_copy_info(head->bridge, &head->agp_info);
455 		agp_backend_release(head->bridge);
456 	} else {
457 		agp_copy_info(head->bridge, &head->agp_info);
458 	}
459 #ifndef __NetBSD__
460 	/* Why would anything even attach in this case?  */
461 	if (head->agp_info.chipset == NOT_SUPPORTED) {
462 		kfree(head);
463 		return NULL;
464 	}
465 #endif
466 	INIT_LIST_HEAD(&head->memory);
467 #ifdef __NetBSD__
468 	/* Not sure what the other fields are used for...  */
469 	head->base = head->agp_info.ai_aperture_base;
470 #else
471 	head->cant_use_aperture = head->agp_info.cant_use_aperture;
472 	head->page_mask = head->agp_info.page_mask;
473 	head->base = head->agp_info.aper_base;
474 #endif
475 	return head;
476 }
477 
478 #ifndef __NetBSD__
479 
480 /**
481  * Binds a collection of pages into AGP memory at the given offset, returning
482  * the AGP memory structure containing them.
483  *
484  * No reference is held on the pages during this time -- it is up to the
485  * caller to handle that.
486  */
487 DRM_AGP_MEM *
488 drm_agp_bind_pages(struct drm_device *dev,
489 		   struct page **pages,
490 		   unsigned long num_pages,
491 		   uint32_t gtt_offset,
492 		   u32 type)
493 {
494 	DRM_AGP_MEM *mem;
495 	int ret, i;
496 
497 	DRM_DEBUG("\n");
498 
499 	mem = agp_allocate_memory(dev->agp->bridge, num_pages,
500 				      type);
501 	if (mem == NULL) {
502 		DRM_ERROR("Failed to allocate memory for %ld pages\n",
503 			  num_pages);
504 		return NULL;
505 	}
506 
507 	for (i = 0; i < num_pages; i++)
508 		mem->pages[i] = pages[i];
509 	mem->page_count = num_pages;
510 
511 	mem->is_flushed = true;
512 	ret = agp_bind_memory(mem, gtt_offset / PAGE_SIZE);
513 	if (ret != 0) {
514 		DRM_ERROR("Failed to bind AGP memory: %d\n", ret);
515 		agp_free_memory(mem);
516 		return NULL;
517 	}
518 
519 	return mem;
520 }
521 EXPORT_SYMBOL(drm_agp_bind_pages);
522 
523 #endif	/* __NetBSD__ */
524 
525 #endif /* __OS_HAS_AGP */
526