drm/i915: Sync drm_irq.c with Linux 3.8.13
[dragonfly.git] / sys / dev / drm / drm_agpsupport.c
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  * $FreeBSD: src/sys/dev/drm2/drm_agpsupport.c,v 1.1 2012/05/22 11:07:44 kib Exp $
30  */
31
32 /** @file drm_agpsupport.c
33  * Support code for tying the kernel AGP support to DRM drivers and
34  * the DRM's AGP ioctls.
35  */
36
37 #include <drm/drmP.h>
38
39 #include <dev/agp/agpreg.h>
40 #include <bus/pci/pcireg.h>
41
42 /* Returns 1 if AGP or 0 if not. */
43 static int
44 drm_device_find_capability(struct drm_device *dev, int cap)
45 {
46         return (pci_find_extcap(dev->dev, cap, NULL) == 0);
47 }
48
49 int drm_device_is_agp(struct drm_device *dev)
50 {
51         if (dev->driver->device_is_agp != NULL) {
52                 int ret;
53
54                 /* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely
55                  * AGP, 2 = fall back to PCI capability
56                  */
57                 ret = (*dev->driver->device_is_agp)(dev);
58                 if (ret != DRM_MIGHT_BE_AGP)
59                         return ret;
60         }
61
62         return (drm_device_find_capability(dev, PCIY_AGP));
63 }
64
65 int drm_device_is_pcie(struct drm_device *dev)
66 {
67         return (drm_device_find_capability(dev, PCIY_EXPRESS));
68 }
69
70 int drm_agp_info(struct drm_device * dev, struct drm_agp_info *info)
71 {
72         struct agp_info *kern;
73
74         if (!dev->agp || !dev->agp->acquired)
75                 return EINVAL;
76
77         kern                   = &dev->agp->agp_info;
78         agp_get_info(dev->agp->agpdev, kern);
79         info->agp_version_major = 1;
80         info->agp_version_minor = 0;
81         info->mode              = kern->ai_mode;
82         info->aperture_base     = kern->ai_aperture_base;
83         info->aperture_size     = kern->ai_aperture_size;
84         info->memory_allowed    = kern->ai_memory_allowed;
85         info->memory_used       = kern->ai_memory_used;
86         info->id_vendor         = kern->ai_devid & 0xffff;
87         info->id_device         = kern->ai_devid >> 16;
88
89         return 0;
90 }
91
92 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
93                        struct drm_file *file_priv)
94 {
95         int err;
96         struct drm_agp_info info;
97
98         err = drm_agp_info(dev, &info);
99         if (err != 0)
100                 return err;
101
102         *(struct drm_agp_info *) data = info;
103         return 0;
104 }
105
106 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
107                           struct drm_file *file_priv)
108 {
109
110         return drm_agp_acquire(dev);
111 }
112
113 int drm_agp_acquire(struct drm_device *dev)
114 {
115         int retcode;
116
117         if (!dev->agp || dev->agp->acquired)
118                 return EINVAL;
119
120         retcode = agp_acquire(dev->agp->agpdev);
121         if (retcode)
122                 return retcode;
123
124         dev->agp->acquired = 1;
125         return 0;
126 }
127
128 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
129                           struct drm_file *file_priv)
130 {
131
132         return drm_agp_release(dev);
133 }
134
135 int drm_agp_release(struct drm_device * dev)
136 {
137         if (!dev->agp || !dev->agp->acquired)
138                 return EINVAL;
139         agp_release(dev->agp->agpdev);
140         dev->agp->acquired = 0;
141         return 0;
142 }
143
144 int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
145 {
146
147         if (!dev->agp || !dev->agp->acquired)
148                 return EINVAL;
149         
150         dev->agp->mode    = mode.mode;
151         agp_enable(dev->agp->agpdev, mode.mode);
152         dev->agp->enabled = 1;
153         return 0;
154 }
155
156 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
157                          struct drm_file *file_priv)
158 {
159         struct drm_agp_mode mode;
160
161         mode = *(struct drm_agp_mode *) data;
162
163         return drm_agp_enable(dev, mode);
164 }
165
166 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
167 {
168         drm_agp_mem_t    *entry;
169         void             *handle;
170         unsigned long    pages;
171         u_int32_t        type;
172         struct agp_memory_info info;
173
174         if (!dev->agp || !dev->agp->acquired)
175                 return EINVAL;
176
177         entry = kmalloc(sizeof(*entry), DRM_MEM_AGPLISTS, M_NOWAIT | M_ZERO);
178         if (entry == NULL)
179                 return ENOMEM;
180
181         pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
182         type = (u_int32_t) request->type;
183
184         handle = drm_agp_allocate_memory(pages, type);
185         if (handle == NULL) {
186                 drm_free(entry, DRM_MEM_AGPLISTS);
187                 return ENOMEM;
188         }
189         
190         entry->handle    = handle;
191         entry->bound     = 0;
192         entry->pages     = pages;
193         entry->prev      = NULL;
194         entry->next      = dev->agp->memory;
195         if (dev->agp->memory)
196                 dev->agp->memory->prev = entry;
197         dev->agp->memory = entry;
198
199         agp_memory_info(dev->agp->agpdev, entry->handle, &info);
200
201         request->handle   = (unsigned long) entry->handle;
202         request->physical = info.ami_physical;
203
204         return 0;
205 }
206
207 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
208                         struct drm_file *file_priv)
209 {
210         struct drm_agp_buffer request;
211         int retcode;
212
213         request = *(struct drm_agp_buffer *) data;
214
215         retcode = drm_agp_alloc(dev, &request);
216
217         *(struct drm_agp_buffer *) data = request;
218
219         return retcode;
220 }
221
222 static drm_agp_mem_t * drm_agp_lookup_entry(struct drm_device *dev,
223                                             void *handle)
224 {
225         drm_agp_mem_t *entry;
226
227         for (entry = dev->agp->memory; entry; entry = entry->next) {
228                 if (entry->handle == handle) return entry;
229         }
230         return NULL;
231 }
232
233 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
234 {
235         drm_agp_mem_t     *entry;
236         int retcode;
237
238         if (!dev->agp || !dev->agp->acquired)
239                 return EINVAL;
240
241         entry = drm_agp_lookup_entry(dev, (void *)request->handle);
242         if (entry == NULL || !entry->bound)
243                 return EINVAL;
244
245         retcode = drm_agp_unbind_memory(entry->handle);
246
247         if (retcode == 0)
248                 entry->bound = 0;
249
250         return retcode;
251 }
252
253 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
254                          struct drm_file *file_priv)
255 {
256         struct drm_agp_binding request;
257         int retcode;
258
259         request = *(struct drm_agp_binding *) data;
260
261         retcode = drm_agp_unbind(dev, &request);
262
263         return retcode;
264 }
265
266 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
267 {
268         drm_agp_mem_t     *entry;
269         int               retcode;
270         int               page;
271         
272         if (!dev->agp || !dev->agp->acquired)
273                 return EINVAL;
274
275         DRM_DEBUG("agp_bind, page_size=%x\n", (int)PAGE_SIZE);
276
277         entry = drm_agp_lookup_entry(dev, (void *)request->handle);
278         if (entry == NULL || entry->bound)
279                 return EINVAL;
280
281         page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
282
283         retcode = drm_agp_bind_memory(entry->handle, page);
284         if (retcode == 0)
285                 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
286
287         return retcode;
288 }
289
290 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
291                        struct drm_file *file_priv)
292 {
293         struct drm_agp_binding request;
294         int retcode;
295
296         request = *(struct drm_agp_binding *) data;
297
298         retcode = drm_agp_bind(dev, &request);
299
300         return retcode;
301 }
302
303 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
304 {
305         drm_agp_mem_t    *entry;
306         
307         if (!dev->agp || !dev->agp->acquired)
308                 return EINVAL;
309
310         entry = drm_agp_lookup_entry(dev, (void*)request->handle);
311         if (entry == NULL)
312                 return EINVAL;
313    
314         if (entry->prev)
315                 entry->prev->next = entry->next;
316         else
317                 dev->agp->memory  = entry->next;
318         if (entry->next)
319                 entry->next->prev = entry->prev;
320
321         if (entry->bound)
322                 drm_agp_unbind_memory(entry->handle);
323         drm_agp_free_memory(entry->handle);
324
325         drm_free(entry, DRM_MEM_AGPLISTS);
326
327         return 0;
328
329 }
330
331 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
332                        struct drm_file *file_priv)
333 {
334         struct drm_agp_buffer request;
335         int retcode;
336
337         request = *(struct drm_agp_buffer *) data;
338
339         retcode = drm_agp_free(dev, &request);
340
341         return retcode;
342 }
343
344 drm_agp_head_t *drm_agp_init(void)
345 {
346         device_t agpdev;
347         drm_agp_head_t *head   = NULL;
348         int      agp_available = 1;
349    
350         agpdev = DRM_AGP_FIND_DEVICE();
351         if (!agpdev)
352                 agp_available = 0;
353
354         DRM_DEBUG("agp_available = %d\n", agp_available);
355
356         if (agp_available) {
357                 head = kmalloc(sizeof(*head), DRM_MEM_AGPLISTS,
358                     M_NOWAIT | M_ZERO);
359                 if (head == NULL)
360                         return NULL;
361                 head->agpdev = agpdev;
362                 agp_get_info(agpdev, &head->agp_info);
363                 head->base = head->agp_info.ai_aperture_base;
364                 head->memory = NULL;
365                 DRM_INFO("AGP at 0x%08lx %dMB\n",
366                          (long)head->agp_info.ai_aperture_base,
367                          (int)(head->agp_info.ai_aperture_size >> 20));
368         }
369         return head;
370 }
371
372 void *drm_agp_allocate_memory(size_t pages, u32 type)
373 {
374         device_t agpdev;
375
376         agpdev = DRM_AGP_FIND_DEVICE();
377         if (!agpdev)
378                 return NULL;
379
380         return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
381 }
382
383 int drm_agp_free_memory(void *handle)
384 {
385         device_t agpdev;
386
387         agpdev = DRM_AGP_FIND_DEVICE();
388         if (!agpdev || !handle)
389                 return 0;
390
391         agp_free_memory(agpdev, handle);
392         return 1;
393 }
394
395 int drm_agp_bind_memory(void *handle, off_t start)
396 {
397         device_t agpdev;
398
399         agpdev = DRM_AGP_FIND_DEVICE();
400         if (!agpdev || !handle)
401                 return EINVAL;
402
403         return agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
404 }
405
406 int drm_agp_unbind_memory(void *handle)
407 {
408         device_t agpdev;
409
410         agpdev = DRM_AGP_FIND_DEVICE();
411         if (!agpdev || !handle)
412                 return EINVAL;
413
414         return agp_unbind_memory(agpdev, handle);
415 }