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