AMD64 - Fix many compile-time warnings. int/ptr type mismatches, %llx, etc.
[dragonfly.git] / sys / dev / drm / drm_agpsupport.c
blobc610c03df71110145fd25459cd4b459d56f681ef
1 /*-
2 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4 * All Rights Reserved.
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:
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.
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.
25 * Author:
26 * Rickard E. (Rik) Faith <faith@valinux.com>
27 * Gareth Hughes <gareth@valinux.com>
31 /** @file drm_agpsupport.c
32 * Support code for tying the kernel AGP support to DRM drivers and
33 * the DRM's AGP ioctls.
36 #include "dev/drm/drmP.h"
38 #include <dev/agp/agpreg.h>
39 #include <bus/pci/pcireg.h>
41 /* Returns 1 if AGP or 0 if not. */
42 static int
43 drm_device_find_capability(struct drm_device *dev, int cap)
45 /* Code taken from agp.c. IWBNI that was a public interface. */
46 u_int32_t status;
47 u_int8_t ptr, next;
50 * Check the CAP_LIST bit of the PCI status register first.
52 status = pci_read_config(dev->device, PCIR_STATUS, 2);
53 if (!(status & 0x10))
54 return 0;
57 * Traverse the capabilities list.
59 for (ptr = pci_read_config(dev->device, AGP_CAPPTR, 1);
60 ptr != 0;
61 ptr = next) {
62 u_int32_t capid = pci_read_config(dev->device, ptr, 4);
63 next = AGP_CAPID_GET_NEXT_PTR(capid);
66 * If this capability entry ID is cap, then we are done.
68 if (AGP_CAPID_GET_CAP_ID(capid) == cap)
69 return 1;
72 return 0;
75 int drm_device_is_agp(struct drm_device *dev)
77 if (dev->driver->device_is_agp != NULL) {
78 int ret;
80 /* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely
81 * AGP, 2 = fall back to PCI capability
83 ret = (*dev->driver->device_is_agp)(dev);
84 if (ret != DRM_MIGHT_BE_AGP)
85 return ret;
88 return (drm_device_find_capability(dev, PCIY_AGP));
91 int drm_device_is_pcie(struct drm_device *dev)
93 return (drm_device_find_capability(dev, PCIY_EXPRESS));
96 int drm_agp_info(struct drm_device * dev, struct drm_agp_info *info)
98 struct agp_info *kern;
100 if (!dev->agp || !dev->agp->acquired)
101 return EINVAL;
103 kern = &dev->agp->info;
104 agp_get_info(dev->agp->agpdev, kern);
105 info->agp_version_major = 1;
106 info->agp_version_minor = 0;
107 info->mode = kern->ai_mode;
108 info->aperture_base = kern->ai_aperture_base;
109 info->aperture_size = kern->ai_aperture_size;
110 info->memory_allowed = kern->ai_memory_allowed;
111 info->memory_used = kern->ai_memory_used;
112 info->id_vendor = kern->ai_devid & 0xffff;
113 info->id_device = kern->ai_devid >> 16;
115 return 0;
118 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
119 struct drm_file *file_priv)
121 int err;
122 struct drm_agp_info info;
124 err = drm_agp_info(dev, &info);
125 if (err != 0)
126 return err;
128 *(struct drm_agp_info *) data = info;
129 return 0;
132 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
133 struct drm_file *file_priv)
136 return drm_agp_acquire(dev);
139 int drm_agp_acquire(struct drm_device *dev)
141 int retcode;
143 if (!dev->agp || dev->agp->acquired)
144 return EINVAL;
146 retcode = agp_acquire(dev->agp->agpdev);
147 if (retcode)
148 return retcode;
150 dev->agp->acquired = 1;
151 return 0;
154 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
155 struct drm_file *file_priv)
158 return drm_agp_release(dev);
161 int drm_agp_release(struct drm_device * dev)
163 if (!dev->agp || !dev->agp->acquired)
164 return EINVAL;
165 agp_release(dev->agp->agpdev);
166 dev->agp->acquired = 0;
167 return 0;
170 int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
173 if (!dev->agp || !dev->agp->acquired)
174 return EINVAL;
176 dev->agp->mode = mode.mode;
177 agp_enable(dev->agp->agpdev, mode.mode);
178 dev->agp->enabled = 1;
179 return 0;
182 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
183 struct drm_file *file_priv)
185 struct drm_agp_mode mode;
187 mode = *(struct drm_agp_mode *) data;
189 return drm_agp_enable(dev, mode);
192 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
194 drm_agp_mem_t *entry;
195 void *handle;
196 unsigned long pages;
197 u_int32_t type;
198 struct agp_memory_info info;
200 if (!dev->agp || !dev->agp->acquired)
201 return EINVAL;
203 entry = malloc(sizeof(*entry), DRM_MEM_AGPLISTS, M_NOWAIT | M_ZERO);
204 if (entry == NULL)
205 return ENOMEM;
207 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
208 type = (u_int32_t) request->type;
210 DRM_UNLOCK();
211 handle = drm_agp_allocate_memory(pages, type);
212 DRM_LOCK();
213 if (handle == NULL) {
214 free(entry, DRM_MEM_AGPLISTS);
215 return ENOMEM;
218 entry->handle = handle;
219 entry->bound = 0;
220 entry->pages = pages;
221 entry->prev = NULL;
222 entry->next = dev->agp->memory;
223 if (dev->agp->memory)
224 dev->agp->memory->prev = entry;
225 dev->agp->memory = entry;
227 agp_memory_info(dev->agp->agpdev, entry->handle, &info);
229 request->handle = (unsigned long) entry->handle;
230 request->physical = info.ami_physical;
232 return 0;
235 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
236 struct drm_file *file_priv)
238 struct drm_agp_buffer request;
239 int retcode;
241 request = *(struct drm_agp_buffer *) data;
243 DRM_LOCK();
244 retcode = drm_agp_alloc(dev, &request);
245 DRM_UNLOCK();
247 *(struct drm_agp_buffer *) data = request;
249 return retcode;
252 static drm_agp_mem_t * drm_agp_lookup_entry(struct drm_device *dev,
253 void *handle)
255 drm_agp_mem_t *entry;
257 for (entry = dev->agp->memory; entry; entry = entry->next) {
258 if (entry->handle == handle) return entry;
260 return NULL;
263 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
265 drm_agp_mem_t *entry;
266 int retcode;
268 if (!dev->agp || !dev->agp->acquired)
269 return EINVAL;
271 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
272 if (entry == NULL || !entry->bound)
273 return EINVAL;
275 DRM_UNLOCK();
276 retcode = drm_agp_unbind_memory(entry->handle);
277 DRM_LOCK();
279 if (retcode == 0)
280 entry->bound = 0;
282 return retcode;
285 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
286 struct drm_file *file_priv)
288 struct drm_agp_binding request;
289 int retcode;
291 request = *(struct drm_agp_binding *) data;
293 DRM_LOCK();
294 retcode = drm_agp_unbind(dev, &request);
295 DRM_UNLOCK();
297 return retcode;
300 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
302 drm_agp_mem_t *entry;
303 int retcode;
304 int page;
306 if (!dev->agp || !dev->agp->acquired)
307 return EINVAL;
309 DRM_DEBUG("agp_bind, page_size=%x\n", PAGE_SIZE);
311 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
312 if (entry == NULL || entry->bound)
313 return EINVAL;
315 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
317 DRM_UNLOCK();
318 retcode = drm_agp_bind_memory(entry->handle, page);
319 DRM_LOCK();
320 if (retcode == 0)
321 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
323 return retcode;
326 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
327 struct drm_file *file_priv)
329 struct drm_agp_binding request;
330 int retcode;
332 request = *(struct drm_agp_binding *) data;
334 DRM_LOCK();
335 retcode = drm_agp_bind(dev, &request);
336 DRM_UNLOCK();
338 return retcode;
341 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
343 drm_agp_mem_t *entry;
345 if (!dev->agp || !dev->agp->acquired)
346 return EINVAL;
348 entry = drm_agp_lookup_entry(dev, (void*)request->handle);
349 if (entry == NULL)
350 return EINVAL;
352 if (entry->prev)
353 entry->prev->next = entry->next;
354 else
355 dev->agp->memory = entry->next;
356 if (entry->next)
357 entry->next->prev = entry->prev;
359 DRM_UNLOCK();
360 if (entry->bound)
361 drm_agp_unbind_memory(entry->handle);
362 drm_agp_free_memory(entry->handle);
363 DRM_LOCK();
365 free(entry, DRM_MEM_AGPLISTS);
367 return 0;
371 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
372 struct drm_file *file_priv)
374 struct drm_agp_buffer request;
375 int retcode;
377 request = *(struct drm_agp_buffer *) data;
379 DRM_LOCK();
380 retcode = drm_agp_free(dev, &request);
381 DRM_UNLOCK();
383 return retcode;
386 drm_agp_head_t *drm_agp_init(void)
388 device_t agpdev;
389 drm_agp_head_t *head = NULL;
390 int agp_available = 1;
392 agpdev = DRM_AGP_FIND_DEVICE();
393 if (!agpdev)
394 agp_available = 0;
396 DRM_DEBUG("agp_available = %d\n", agp_available);
398 if (agp_available) {
399 head = malloc(sizeof(*head), DRM_MEM_AGPLISTS,
400 M_NOWAIT | M_ZERO);
401 if (head == NULL)
402 return NULL;
403 head->agpdev = agpdev;
404 agp_get_info(agpdev, &head->info);
405 head->base = head->info.ai_aperture_base;
406 head->memory = NULL;
407 DRM_INFO("AGP at 0x%08lx %dMB\n",
408 (long)head->info.ai_aperture_base,
409 (int)(head->info.ai_aperture_size >> 20));
411 return head;
414 void *drm_agp_allocate_memory(size_t pages, u32 type)
416 device_t agpdev;
418 agpdev = DRM_AGP_FIND_DEVICE();
419 if (!agpdev)
420 return NULL;
422 return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
425 int drm_agp_free_memory(void *handle)
427 device_t agpdev;
429 agpdev = DRM_AGP_FIND_DEVICE();
430 if (!agpdev || !handle)
431 return 0;
433 agp_free_memory(agpdev, handle);
434 return 1;
437 int drm_agp_bind_memory(void *handle, off_t start)
439 device_t agpdev;
441 agpdev = DRM_AGP_FIND_DEVICE();
442 if (!agpdev || !handle)
443 return EINVAL;
445 return agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
448 int drm_agp_unbind_memory(void *handle)
450 device_t agpdev;
452 agpdev = DRM_AGP_FIND_DEVICE();
453 if (!agpdev || !handle)
454 return EINVAL;
456 return agp_unbind_memory(agpdev, handle);