1/*-
2 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Author:
26 *    Rickard E. (Rik) Faith <faith@valinux.com>
27 *    Gareth Hughes <gareth@valinux.com>
28 *
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34/** @file drm_agpsupport.c
35 * Support code for tying the kernel AGP support to DRM drivers and
36 * the DRM's AGP ioctls.
37 */
38
39#include "dev/drm/drmP.h"
40
41#include <dev/agp/agpreg.h>
42#include <dev/pci/pcireg.h>
43
44/* Returns 1 if AGP or 0 if not. */
45static int
46drm_device_find_capability(struct drm_device *dev, int cap)
47{
48
49	return (pci_find_cap(dev->device, cap, NULL) == 0);
50}
51
52int drm_device_is_agp(struct drm_device *dev)
53{
54	if (dev->driver->device_is_agp != NULL) {
55		int ret;
56
57		/* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely
58		 * AGP, 2 = fall back to PCI capability
59		 */
60		ret = (*dev->driver->device_is_agp)(dev);
61		if (ret != DRM_MIGHT_BE_AGP)
62			return ret;
63	}
64
65	return (drm_device_find_capability(dev, PCIY_AGP));
66}
67
68int drm_device_is_pcie(struct drm_device *dev)
69{
70	return (drm_device_find_capability(dev, PCIY_EXPRESS));
71}
72
73int drm_agp_info(struct drm_device * dev, struct drm_agp_info *info)
74{
75	struct agp_info *kern;
76
77	if (!dev->agp || !dev->agp->acquired)
78		return EINVAL;
79
80	kern                   = &dev->agp->info;
81	agp_get_info(dev->agp->agpdev, kern);
82	info->agp_version_major = 1;
83	info->agp_version_minor = 0;
84	info->mode              = kern->ai_mode;
85	info->aperture_base     = kern->ai_aperture_base;
86	info->aperture_size     = kern->ai_aperture_size;
87	info->memory_allowed    = kern->ai_memory_allowed;
88	info->memory_used       = kern->ai_memory_used;
89	info->id_vendor         = kern->ai_devid & 0xffff;
90	info->id_device         = kern->ai_devid >> 16;
91
92	return 0;
93}
94
95int drm_agp_info_ioctl(struct drm_device *dev, void *data,
96		       struct drm_file *file_priv)
97{
98	int err;
99	struct drm_agp_info info;
100
101	err = drm_agp_info(dev, &info);
102	if (err != 0)
103		return err;
104
105	*(struct drm_agp_info *) data = info;
106	return 0;
107}
108
109int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
110			  struct drm_file *file_priv)
111{
112
113	return drm_agp_acquire(dev);
114}
115
116int drm_agp_acquire(struct drm_device *dev)
117{
118	int retcode;
119
120	if (!dev->agp || dev->agp->acquired)
121		return EINVAL;
122
123	retcode = agp_acquire(dev->agp->agpdev);
124	if (retcode)
125		return retcode;
126
127	dev->agp->acquired = 1;
128	return 0;
129}
130
131int drm_agp_release_ioctl(struct drm_device *dev, void *data,
132			  struct drm_file *file_priv)
133{
134
135	return drm_agp_release(dev);
136}
137
138int drm_agp_release(struct drm_device * dev)
139{
140	if (!dev->agp || !dev->agp->acquired)
141		return EINVAL;
142	agp_release(dev->agp->agpdev);
143	dev->agp->acquired = 0;
144	return 0;
145}
146
147int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
148{
149
150	if (!dev->agp || !dev->agp->acquired)
151		return EINVAL;
152
153	dev->agp->mode    = mode.mode;
154	agp_enable(dev->agp->agpdev, mode.mode);
155	dev->agp->enabled = 1;
156	return 0;
157}
158
159int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
160			 struct drm_file *file_priv)
161{
162	struct drm_agp_mode mode;
163
164	mode = *(struct drm_agp_mode *) data;
165
166	return drm_agp_enable(dev, mode);
167}
168
169int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
170{
171	drm_agp_mem_t    *entry;
172	void	         *handle;
173	unsigned long    pages;
174	u_int32_t	 type;
175	struct agp_memory_info info;
176
177	if (!dev->agp || !dev->agp->acquired)
178		return EINVAL;
179
180	entry = malloc(sizeof(*entry), DRM_MEM_AGPLISTS, M_NOWAIT | M_ZERO);
181	if (entry == NULL)
182		return ENOMEM;
183
184	pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
185	type = (u_int32_t) request->type;
186
187	DRM_UNLOCK();
188	handle = drm_agp_allocate_memory(pages, type);
189	DRM_LOCK();
190	if (handle == NULL) {
191		free(entry, DRM_MEM_AGPLISTS);
192		return ENOMEM;
193	}
194
195	entry->handle    = handle;
196	entry->bound     = 0;
197	entry->pages     = pages;
198	entry->prev      = NULL;
199	entry->next      = dev->agp->memory;
200	if (dev->agp->memory)
201		dev->agp->memory->prev = entry;
202	dev->agp->memory = entry;
203
204	agp_memory_info(dev->agp->agpdev, entry->handle, &info);
205
206	request->handle   = (unsigned long) entry->handle;
207        request->physical = info.ami_physical;
208
209	return 0;
210}
211
212int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
213			struct drm_file *file_priv)
214{
215	struct drm_agp_buffer request;
216	int retcode;
217
218	request = *(struct drm_agp_buffer *) data;
219
220	DRM_LOCK();
221	retcode = drm_agp_alloc(dev, &request);
222	DRM_UNLOCK();
223
224	*(struct drm_agp_buffer *) data = request;
225
226	return retcode;
227}
228
229static drm_agp_mem_t * drm_agp_lookup_entry(struct drm_device *dev,
230					    void *handle)
231{
232	drm_agp_mem_t *entry;
233
234	for (entry = dev->agp->memory; entry; entry = entry->next) {
235		if (entry->handle == handle) return entry;
236	}
237	return NULL;
238}
239
240int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
241{
242	drm_agp_mem_t     *entry;
243	int retcode;
244
245	if (!dev->agp || !dev->agp->acquired)
246		return EINVAL;
247
248	entry = drm_agp_lookup_entry(dev, (void *)request->handle);
249	if (entry == NULL || !entry->bound)
250		return EINVAL;
251
252	DRM_UNLOCK();
253	retcode = drm_agp_unbind_memory(entry->handle);
254	DRM_LOCK();
255
256	if (retcode == 0)
257		entry->bound = 0;
258
259	return retcode;
260}
261
262int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
263			 struct drm_file *file_priv)
264{
265	struct drm_agp_binding request;
266	int retcode;
267
268	request = *(struct drm_agp_binding *) data;
269
270	DRM_LOCK();
271	retcode = drm_agp_unbind(dev, &request);
272	DRM_UNLOCK();
273
274	return retcode;
275}
276
277int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
278{
279	drm_agp_mem_t     *entry;
280	int               retcode;
281	int               page;
282
283	if (!dev->agp || !dev->agp->acquired)
284		return EINVAL;
285
286	DRM_DEBUG("agp_bind, page_size=%x\n", (int)PAGE_SIZE);
287
288	entry = drm_agp_lookup_entry(dev, (void *)request->handle);
289	if (entry == NULL || entry->bound)
290		return EINVAL;
291
292	page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
293
294	DRM_UNLOCK();
295	retcode = drm_agp_bind_memory(entry->handle, page);
296	DRM_LOCK();
297	if (retcode == 0)
298		entry->bound = dev->agp->base + (page << PAGE_SHIFT);
299
300	return retcode;
301}
302
303int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
304		       struct drm_file *file_priv)
305{
306	struct drm_agp_binding request;
307	int retcode;
308
309	request = *(struct drm_agp_binding *) data;
310
311	DRM_LOCK();
312	retcode = drm_agp_bind(dev, &request);
313	DRM_UNLOCK();
314
315	return retcode;
316}
317
318int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
319{
320	drm_agp_mem_t    *entry;
321
322	if (!dev->agp || !dev->agp->acquired)
323		return EINVAL;
324
325	entry = drm_agp_lookup_entry(dev, (void*)request->handle);
326	if (entry == NULL)
327		return EINVAL;
328
329	if (entry->prev)
330		entry->prev->next = entry->next;
331	else
332		dev->agp->memory  = entry->next;
333	if (entry->next)
334		entry->next->prev = entry->prev;
335
336	DRM_UNLOCK();
337	if (entry->bound)
338		drm_agp_unbind_memory(entry->handle);
339	drm_agp_free_memory(entry->handle);
340	DRM_LOCK();
341
342	free(entry, DRM_MEM_AGPLISTS);
343
344	return 0;
345
346}
347
348int drm_agp_free_ioctl(struct drm_device *dev, void *data,
349		       struct drm_file *file_priv)
350{
351	struct drm_agp_buffer request;
352	int retcode;
353
354	request = *(struct drm_agp_buffer *) data;
355
356	DRM_LOCK();
357	retcode = drm_agp_free(dev, &request);
358	DRM_UNLOCK();
359
360	return retcode;
361}
362
363drm_agp_head_t *drm_agp_init(void)
364{
365	device_t agpdev;
366	drm_agp_head_t *head   = NULL;
367	int      agp_available = 1;
368
369	agpdev = DRM_AGP_FIND_DEVICE();
370	if (!agpdev)
371		agp_available = 0;
372
373	DRM_DEBUG("agp_available = %d\n", agp_available);
374
375	if (agp_available) {
376		head = malloc(sizeof(*head), DRM_MEM_AGPLISTS,
377		    M_NOWAIT | M_ZERO);
378		if (head == NULL)
379			return NULL;
380		head->agpdev = agpdev;
381		agp_get_info(agpdev, &head->info);
382		head->base = head->info.ai_aperture_base;
383		head->memory = NULL;
384		DRM_INFO("AGP at 0x%08lx %dMB\n",
385			 (long)head->info.ai_aperture_base,
386			 (int)(head->info.ai_aperture_size >> 20));
387	}
388	return head;
389}
390
391void *drm_agp_allocate_memory(size_t pages, u32 type)
392{
393	device_t agpdev;
394
395	agpdev = DRM_AGP_FIND_DEVICE();
396	if (!agpdev)
397		return NULL;
398
399	return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
400}
401
402int drm_agp_free_memory(void *handle)
403{
404	device_t agpdev;
405
406	agpdev = DRM_AGP_FIND_DEVICE();
407	if (!agpdev || !handle)
408		return 0;
409
410	agp_free_memory(agpdev, handle);
411	return 1;
412}
413
414int drm_agp_bind_memory(void *handle, off_t start)
415{
416	device_t agpdev;
417
418	agpdev = DRM_AGP_FIND_DEVICE();
419	if (!agpdev || !handle)
420		return EINVAL;
421
422	return agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
423}
424
425int drm_agp_unbind_memory(void *handle)
426{
427	device_t agpdev;
428
429	agpdev = DRM_AGP_FIND_DEVICE();
430	if (!agpdev || !handle)
431		return EINVAL;
432
433	return agp_unbind_memory(agpdev, handle);
434}
435