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 <sys/cdefs.h>
35#include <dev/drm2/drmP.h>
36
37#if __OS_HAS_AGP
38
39/**
40 * Get AGP information.
41 *
42 * \param inode device inode.
43 * \param file_priv DRM file private.
44 * \param cmd command.
45 * \param arg pointer to a (output) drm_agp_info structure.
46 * \return zero on success or a negative number on failure.
47 *
48 * Verifies the AGP device has been initialized and acquired and fills in the
49 * drm_agp_info structure with the information in drm_agp_head::agp_info.
50 */
51int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
52{
53	DRM_AGP_KERN *kern;
54
55	if (!dev->agp || !dev->agp->acquired)
56		return -EINVAL;
57
58	kern = &dev->agp->agp_info;
59	agp_get_info(dev->agp->bridge, kern);
60	info->agp_version_major = 1;
61	info->agp_version_minor = 0;
62	info->mode              = kern->ai_mode;
63	info->aperture_base     = kern->ai_aperture_base;
64	info->aperture_size     = kern->ai_aperture_size;
65	info->memory_allowed    = kern->ai_memory_allowed;
66	info->memory_used       = kern->ai_memory_used;
67	info->id_vendor         = kern->ai_devid & 0xffff;
68	info->id_device         = kern->ai_devid >> 16;
69
70	return 0;
71}
72
73EXPORT_SYMBOL(drm_agp_info);
74
75int drm_agp_info_ioctl(struct drm_device *dev, void *data,
76		       struct drm_file *file_priv)
77{
78	struct drm_agp_info *info = data;
79	int err;
80
81	err = drm_agp_info(dev, info);
82	if (err)
83		return err;
84
85	return 0;
86}
87
88/**
89 * Acquire the AGP device.
90 *
91 * \param dev DRM device that is to acquire AGP.
92 * \return zero on success or a negative number on failure.
93 *
94 * Verifies the AGP device hasn't been acquired before and calls
95 * \c agp_backend_acquire.
96 */
97int drm_agp_acquire(struct drm_device * dev)
98{
99	int retcode;
100
101	if (!dev->agp)
102		return -ENODEV;
103	if (dev->agp->acquired)
104		return -EBUSY;
105	retcode = agp_acquire(dev->agp->bridge);
106	if (retcode)
107		return -retcode;
108	dev->agp->acquired = 1;
109	return 0;
110}
111
112EXPORT_SYMBOL(drm_agp_acquire);
113
114/**
115 * Acquire the AGP device (ioctl).
116 *
117 * \param inode device inode.
118 * \param file_priv DRM file private.
119 * \param cmd command.
120 * \param arg user argument.
121 * \return zero on success or a negative number on failure.
122 *
123 * Verifies the AGP device hasn't been acquired before and calls
124 * \c agp_backend_acquire.
125 */
126int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
127			  struct drm_file *file_priv)
128{
129	return drm_agp_acquire((struct drm_device *) file_priv->minor->dev);
130}
131
132/**
133 * Release the AGP device.
134 *
135 * \param dev DRM device that is to release AGP.
136 * \return zero on success or a negative number on failure.
137 *
138 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
139 */
140int drm_agp_release(struct drm_device * dev)
141{
142	if (!dev->agp || !dev->agp->acquired)
143		return -EINVAL;
144	agp_release(dev->agp->bridge);
145	dev->agp->acquired = 0;
146	return 0;
147}
148EXPORT_SYMBOL(drm_agp_release);
149
150int drm_agp_release_ioctl(struct drm_device *dev, void *data,
151			  struct drm_file *file_priv)
152{
153	return drm_agp_release(dev);
154}
155
156/**
157 * Enable the AGP bus.
158 *
159 * \param dev DRM device that has previously acquired AGP.
160 * \param mode Requested AGP mode.
161 * \return zero on success or a negative number on failure.
162 *
163 * Verifies the AGP device has been acquired but not enabled, and calls
164 * \c agp_enable.
165 */
166int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode)
167{
168	if (!dev->agp || !dev->agp->acquired)
169		return -EINVAL;
170
171	dev->agp->mode = mode.mode;
172	agp_enable(dev->agp->bridge, mode.mode);
173	dev->agp->enabled = 1;
174	return 0;
175}
176
177EXPORT_SYMBOL(drm_agp_enable);
178
179int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
180			 struct drm_file *file_priv)
181{
182	struct drm_agp_mode *mode = data;
183
184	return drm_agp_enable(dev, *mode);
185}
186
187/**
188 * Allocate AGP memory.
189 *
190 * \param inode device inode.
191 * \param file_priv file private pointer.
192 * \param cmd command.
193 * \param arg pointer to a drm_agp_buffer structure.
194 * \return zero on success or a negative number on failure.
195 *
196 * Verifies the AGP device is present and has been acquired, allocates the
197 * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
198 */
199int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
200{
201	struct drm_agp_mem *entry;
202	DRM_AGP_MEM *memory;
203	unsigned long pages;
204	u32 type;
205	struct agp_memory_info info;
206
207	if (!dev->agp || !dev->agp->acquired)
208		return -EINVAL;
209	if (!(entry = malloc(sizeof(*entry), DRM_MEM_AGPLISTS, M_NOWAIT)))
210		return -ENOMEM;
211
212	memset(entry, 0, sizeof(*entry));
213
214	pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
215	type = (u32) request->type;
216	if (!(memory = agp_alloc_memory(dev->agp->bridge, type, pages << PAGE_SHIFT))) {
217		free(entry, DRM_MEM_AGPLISTS);
218		return -ENOMEM;
219	}
220
221	entry->handle = (unsigned long)memory;
222	entry->memory = memory;
223	entry->bound = 0;
224	entry->pages = pages;
225	list_add(&entry->head, &dev->agp->memory);
226
227	agp_memory_info(dev->agp->bridge, entry->memory, &info);
228
229	request->handle = entry->handle;
230	request->physical = info.ami_physical;
231
232	return 0;
233}
234EXPORT_SYMBOL(drm_agp_alloc);
235
236
237int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
238			struct drm_file *file_priv)
239{
240	struct drm_agp_buffer *request = data;
241
242	return drm_agp_alloc(dev, request);
243}
244
245/**
246 * Search for the AGP memory entry associated with a handle.
247 *
248 * \param dev DRM device structure.
249 * \param handle AGP memory handle.
250 * \return pointer to the drm_agp_mem structure associated with \p handle.
251 *
252 * Walks through drm_agp_head::memory until finding a matching handle.
253 */
254static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
255					   unsigned long handle)
256{
257	struct drm_agp_mem *entry;
258
259	list_for_each_entry(entry, &dev->agp->memory, head) {
260		if (entry->handle == handle)
261			return entry;
262	}
263	return NULL;
264}
265
266/**
267 * Unbind AGP memory from the GATT (ioctl).
268 *
269 * \param inode device inode.
270 * \param file_priv DRM file private.
271 * \param cmd command.
272 * \param arg pointer to a drm_agp_binding structure.
273 * \return zero on success or a negative number on failure.
274 *
275 * Verifies the AGP device is present and acquired, looks-up the AGP memory
276 * entry and passes it to the unbind_agp() function.
277 */
278int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
279{
280	struct drm_agp_mem *entry;
281	int ret;
282
283	if (!dev->agp || !dev->agp->acquired)
284		return -EINVAL;
285	if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
286		return -EINVAL;
287	if (!entry->bound)
288		return -EINVAL;
289	ret = drm_unbind_agp(entry->memory);
290	if (ret == 0)
291		entry->bound = 0;
292	return ret;
293}
294EXPORT_SYMBOL(drm_agp_unbind);
295
296
297int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
298			 struct drm_file *file_priv)
299{
300	struct drm_agp_binding *request = data;
301
302	return drm_agp_unbind(dev, request);
303}
304
305/**
306 * Bind AGP memory into the GATT (ioctl)
307 *
308 * \param inode device inode.
309 * \param file_priv DRM file private.
310 * \param cmd command.
311 * \param arg pointer to a drm_agp_binding structure.
312 * \return zero on success or a negative number on failure.
313 *
314 * Verifies the AGP device is present and has been acquired and that no memory
315 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
316 * it to bind_agp() function.
317 */
318int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
319{
320	struct drm_agp_mem *entry;
321	int retcode;
322	int page;
323
324	if (!dev->agp || !dev->agp->acquired)
325		return -EINVAL;
326	if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
327		return -EINVAL;
328	if (entry->bound)
329		return -EINVAL;
330	page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
331	if ((retcode = drm_bind_agp(entry->memory, page)))
332		return retcode;
333	entry->bound = dev->agp->base + (page << PAGE_SHIFT);
334	DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
335		  dev->agp->base, entry->bound);
336	return 0;
337}
338EXPORT_SYMBOL(drm_agp_bind);
339
340
341int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
342		       struct drm_file *file_priv)
343{
344	struct drm_agp_binding *request = data;
345
346	return drm_agp_bind(dev, request);
347}
348
349/**
350 * Free AGP memory (ioctl).
351 *
352 * \param inode device inode.
353 * \param file_priv DRM file private.
354 * \param cmd command.
355 * \param arg pointer to a drm_agp_buffer structure.
356 * \return zero on success or a negative number on failure.
357 *
358 * Verifies the AGP device is present and has been acquired and looks up the
359 * AGP memory entry. If the memory it's currently bound, unbind it via
360 * unbind_agp(). Frees it via free_agp() as well as the entry itself
361 * and unlinks from the doubly linked list it's inserted in.
362 */
363int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
364{
365	struct drm_agp_mem *entry;
366
367	if (!dev->agp || !dev->agp->acquired)
368		return -EINVAL;
369	if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
370		return -EINVAL;
371	if (entry->bound)
372		drm_unbind_agp(entry->memory);
373
374	list_del(&entry->head);
375
376	drm_free_agp(entry->memory, entry->pages);
377	free(entry, DRM_MEM_AGPLISTS);
378	return 0;
379}
380EXPORT_SYMBOL(drm_agp_free);
381
382
383
384int drm_agp_free_ioctl(struct drm_device *dev, void *data,
385		       struct drm_file *file_priv)
386{
387	struct drm_agp_buffer *request = data;
388
389	return drm_agp_free(dev, request);
390}
391
392/**
393 * Initialize the AGP resources.
394 *
395 * \return pointer to a drm_agp_head structure.
396 *
397 * Gets the drm_agp_t structure which is made available by the agpgart module
398 * via the inter_module_* functions. Creates and initializes a drm_agp_head
399 * structure.
400 */
401struct drm_agp_head *drm_agp_init(struct drm_device *dev)
402{
403	struct drm_agp_head *head = NULL;
404
405	if (!(head = malloc(sizeof(*head), DRM_MEM_AGPLISTS, M_NOWAIT)))
406		return NULL;
407	memset((void *)head, 0, sizeof(*head));
408	head->bridge = agp_find_device();
409	if (!head->bridge) {
410		free(head, DRM_MEM_AGPLISTS);
411		return NULL;
412	} else {
413		agp_get_info(head->bridge, &head->agp_info);
414	}
415	INIT_LIST_HEAD(&head->memory);
416	head->cant_use_aperture = 0;
417	head->base = head->agp_info.ai_aperture_base;
418	return head;
419}
420
421#ifdef FREEBSD_NOTYET
422/**
423 * Binds a collection of pages into AGP memory at the given offset, returning
424 * the AGP memory structure containing them.
425 *
426 * No reference is held on the pages during this time -- it is up to the
427 * caller to handle that.
428 */
429DRM_AGP_MEM *
430drm_agp_bind_pages(struct drm_device *dev,
431		   struct page **pages,
432		   unsigned long num_pages,
433		   uint32_t gtt_offset,
434		   u32 type)
435{
436	DRM_AGP_MEM *mem;
437	int ret, i;
438
439	DRM_DEBUG("\n");
440
441	mem = agp_allocate_memory(dev->agp->bridge, num_pages,
442				      type);
443	if (mem == NULL) {
444		DRM_ERROR("Failed to allocate memory for %ld pages\n",
445			  num_pages);
446		return NULL;
447	}
448
449	for (i = 0; i < num_pages; i++)
450		mem->pages[i] = pages[i];
451	mem->page_count = num_pages;
452
453	mem->is_flushed = true;
454	ret = agp_bind_memory(mem, gtt_offset / PAGE_SIZE);
455	if (ret != 0) {
456		DRM_ERROR("Failed to bind AGP memory: %d\n", ret);
457		agp_free_memory(mem);
458		return NULL;
459	}
460
461	return mem;
462}
463EXPORT_SYMBOL(drm_agp_bind_pages);
464#endif /* FREEBSD_NOTYET */
465
466#endif /* __OS_HAS_AGP */
467