-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagpgart_be.c
More file actions
452 lines (393 loc) · 11.1 KB
/
agpgart_be.c
File metadata and controls
452 lines (393 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* AGPGART module version 2.0
* Copyright (C) 2003 Tungsten Graphics
* Copyright (C) 2003 Jeff Hartmann
* Copyright (C) 1999 Precision Insight, Inc.
* Copyright (C) 1999 Xi Graphics, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* JEFF HARTMANN, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <linux/config.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/pagemap.h>
#include <linux/miscdevice.h>
#include <linux/pm.h>
#include <asm/pgtable.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/page.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
#include <asm/tlbflush.h>
#endif
#include <linux/agp_backend.h>
#include "agp.h"
MODULE_AUTHOR("Jeff Hartmann <jhartmann@addoes.com>");
MODULE_PARM(agp_try_unsupported, "1i");
MODULE_LICENSE("Dual BSD/GPL");
EXPORT_SYMBOL(agp_backend_acquire);
EXPORT_SYMBOL(agp_backend_release);
static __inline__ void flush_cache(void)
{
#if defined(__i386__) || defined(__x86_64__)
asm volatile ("wbinvd":::"memory");
#elif defined(__alpha__) || defined(__ia64__) || defined(__sparc__)
/* ??? I wonder if we'll really need to flush caches, or if the
core logic can manage to keep the system coherent. The ARM
speaks only of using `cflush' to get things in memory in
preparation for power failure.
If we do need to call `cflush', we'll need a target page,
as we can only flush one page at a time.
Ditto for IA-64. --davidm 00/08/07 */
mb();
#else
#error "Please define flush_cache."
#endif
}
static __inline__ void my_flush_tlb(void)
{
__flush_tlb();
}
#ifdef CONFIG_SMP
static void ipi_cache_handler(void *null)
{
flush_cache();
}
static void ipi_tlb_handler(void *null)
{
my_flush_tlb();
}
void global_tlb_flush(void)
{
if (smp_call_function(ipi_tlb_handler, NULL, 1, 1) != 0)
panic(PFX "timed out waiting for the other CPUs!\n");
my_flush_tlb();
}
void global_cache_flush(void)
{
if (smp_call_function(ipi_cache_handler, NULL, 1, 1) != 0)
panic(PFX "timed out waiting for the other CPUs!\n");
flush_cache();
}
#else /* CONFIG_SMP */
void global_tlb_flush(void)
{
flush_tlb();
}
void global_cache_flush(void)
{
flush_cache();
}
#endif /* CONFIG_SMP */
int agp_try_unsupported __initdata = 0;
int agp_memory_reserved;
__u32 *agp_gatt_table;
/* Will be removed in the future: full multiple agp isn't properly
* cooked just yet.
*/
agp_context agp_bridge_dummy;
agp_context *agp_bridge;
int agp_backend_acquire(void)
{
if (agp_bridge->type == NOT_SUPPORTED) {
return -EINVAL;
}
atomic_inc(&agp_bridge->agp_in_use);
if (atomic_read(&agp_bridge->agp_in_use) != 1) {
atomic_dec(&agp_bridge->agp_in_use);
return -EBUSY;
}
MOD_INC_USE_COUNT;
return 0;
}
void agp_backend_release(void)
{
if (agp_bridge->type == NOT_SUPPORTED) {
return;
}
atomic_dec(&agp_bridge->agp_in_use);
MOD_DEC_USE_COUNT;
}
struct agp_max_table {
int mem;
int agp;
};
static struct agp_max_table maxes_table[9] __initdata =
{
{0, 0},
{32, 4},
{64, 28},
{128, 96},
{256, 204},
{512, 440},
{1024, 942},
{2048, 1920},
{4096, 3932}
};
static int __init agp_find_max (void)
{
long memory, index, result;
memory = (num_physpages << PAGE_SHIFT) >> 20;
index = 1;
while ((memory > maxes_table[index].mem) &&
(index < 8)) {
index++;
}
result = maxes_table[index - 1].agp +
( (memory - maxes_table[index - 1].mem) *
(maxes_table[index].agp - maxes_table[index - 1].agp)) /
(maxes_table[index].mem - maxes_table[index - 1].mem);
printk(KERN_INFO PFX "Maximum main memory to use "
"for agp memory: %ldM\n", result);
result = result << (20 - PAGE_SHIFT);
return result;
}
static agp_version agp_current_version =
{
AGPGART_VERSION_MAJOR,
AGPGART_VERSION_MINOR
};
static int __init agp_backend_initialize(void)
{
int size_value, rc, got_gatt=0, got_keylist=0;
agp_memory *resvd;
agp_bridge->type = NOT_SUPPORTED;
agp_bridge->max_memory_agp = agp_find_max();
agp_bridge->version = &agp_current_version;
/* Initialize the lists */
INIT_LIST_HEAD(&agp_bridge->memory_list);
INIT_LIST_HEAD(&agp_bridge->bound_list);
/* Initialize the list spinlock */
spin_lock_init(&agp_bridge->list_lock);
rc = agp_find_supported_device();
if (rc) {
/* not KERN_ERR because error msg should have already printed */
printk(KERN_DEBUG PFX "no supported devices found.\n");
return rc;
}
/* Initialize the agp vm here */
rc = agp_initialize_vm(agp_bridge->dev);
if (rc) {
printk(KERN_ERR PFX "Error initializing agp vm subsystem.\n");
return rc;
}
/* TODO make this prettier or make it mostly go away */
if (agp_bridge->needs_scratch_page == TRUE) {
u8 type_bits =
(u8)agp_bridge->mask_memory(0, AGP_NORMAL_MEMORY);
agp_bridge->scratch_page =
agp_bridge->agp_alloc_page(PAGE_KERNEL_NOCACHE);
if (agp_bridge->scratch_page == NULL) {
printk(KERN_ERR PFX "unable to get memory for "
"scratch page.\n");
return -ENOMEM;
}
agp_bridge->scratch_page_addr = (unsigned long)
page_address(agp_bridge->scratch_page);
if(agp_bridge->using_64b_gatt == TRUE) {
agp_bridge->scratch_page_entry = (unsigned long)
pack_gatt_u64(agp_bridge->scratch_page_addr,
type_bits);
} else {
agp_bridge->scratch_page_entry = (unsigned long)
pack_gatt_u32(agp_bridge->scratch_page_addr,
type_bits);
}
}
size_value = agp_bridge->fetch_size();
if (size_value == 0) {
printk(KERN_ERR PFX "unable to determine aperture size.\n");
rc = -EINVAL;
goto err_out;
}
if (agp_bridge->create_gatt_table()) {
printk(KERN_ERR PFX "unable to get memory for graphics "
"translation table.\n");
rc = -ENOMEM;
goto err_out;
}
got_gatt = 1;
agp_bridge->key_list = vmalloc(PAGE_SIZE * 4);
if (agp_bridge->key_list == NULL) {
printk(KERN_ERR PFX "error allocating memory for key lists.\n");
rc = -ENOMEM;
goto err_out;
}
got_keylist = 1;
/* FIXME vmalloc'd memory not guaranteed contiguous */
memset(agp_bridge->key_list, 0, PAGE_SIZE * 4);
if (agp_bridge->configure()) {
printk(KERN_ERR PFX "error configuring host chipset.\n");
rc = -EINVAL;
goto err_out;
}
printk(KERN_INFO PFX "AGP aperture is %dM @ 0x%lx\n",
size_value, agp_bridge->gart_bus_addr);
/* Add reserved memory to the global memory list */
resvd = kmalloc(sizeof(agp_memory), GFP_KERNEL);
if(!resvd) {
printk(KERN_ERR PFX "couldn't allocate memory for reserved "
"memory block.");
rc = -ENOMEM;
goto err_out;
}
agp_bridge->get_reserved_map(resvd);
INIT_LIST_HEAD(&resvd->memory_list);
INIT_LIST_HEAD(&resvd->bound_list);
INIT_LIST_HEAD(&resvd->my_mappings);
agp_insert_memory_to_full_list(resvd);
if(resvd->is_bound) agp_insert_memory_to_bound_list(resvd);
return 0;
err_out:
if (agp_bridge->needs_scratch_page == TRUE) {
agp_bridge->agp_free_page(agp_bridge->scratch_page);
}
if (got_gatt)
agp_bridge->free_gatt_table();
if (got_keylist)
vfree(agp_bridge->key_list);
return rc;
}
/* cannot be __exit b/c as it could be called from __init code */
static void agp_backend_cleanup(void)
{
agp_clean_up_all_memory();
agp_bridge->cleanup();
agp_bridge->free_gatt_table();
vfree(agp_bridge->key_list);
if (agp_bridge->needs_scratch_page == TRUE) {
agp_bridge->agp_free_page(agp_bridge->scratch_page);
}
}
static int agp_power(struct pm_dev *dev, pm_request_t rq, void *data)
{
switch(rq) {
case PM_SUSPEND:
return agp_bridge->suspend();
case PM_RESUME:
agp_bridge->resume();
return 0;
}
return 0;
}
extern int agp_frontend_initialize(void);
extern void agp_frontend_cleanup(void);
static const drm_agp_t drm_agp = {
&agp_free_memory,
&agp_allocate_memory,
&agp_bind_memory,
&agp_unbind_memory,
&agp_enable,
&agp_backend_acquire,
&agp_backend_release,
&agp_copy_info
};
static const drm_agp_3_0_t drm_agp_3_0 = {
get_num_contexts: agp_get_num_contexts,
get_info_list: agp_get_extended_info,
free_memory: agp_ctx_free_memory,
allocate_memory: agp_ctx_allocate_memory,
bind_memory: agp_ctx_bind_memory,
unbind_memory: agp_ctx_unbind_memory,
enable: agp_ctx_enable,
acquire: agp_ctx_backend_acquire,
release: agp_ctx_backend_release,
copy_info: agp_ctx_copy_info,
get_map: agp_ctx_get_map,
kmap: agp_ctx_kmap,
vma_map_memory: agp_ctx_vma_map_memory,
usermap: agp_ctx_usermap,
userunmap: agp_ctx_userunmap,
kunmap: agp_ctx_kunmap,
};
int __init agp_init(void)
{
int ret_val;
printk(KERN_INFO "Linux agpgart interface v%d.%d (c) Jeff Hartmann\n",
2, 0);
printk(KERN_INFO "AGP 3.0 Infrastructure (c) Tungsten Graphics\n");
ret_val = agp_scan_bus();
if (ret_val) {
agp_bridge = &agp_bridge_dummy;
memset(agp_bridge, 0, sizeof(*agp_bridge));
agp_bridge->type = NOT_SUPPORTED;
return ret_val;
}
ret_val = agp_allocate_contexts();
if (ret_val) {
agp_bridge = &agp_bridge_dummy;
memset(agp_bridge, 0, sizeof(*agp_bridge));
agp_bridge->type = NOT_SUPPORTED;
return ret_val;
}
/* The first context is agp_bridge for now */
agp_bridge = AGP_GET_CONTEXT(0);
ret_val = agp_backend_initialize();
if (ret_val) {
agp_bridge->type = NOT_SUPPORTED;
return ret_val;
}
ret_val = agp_frontend_initialize();
if (ret_val) {
agp_bridge->type = NOT_SUPPORTED;
agp_backend_cleanup();
return ret_val;
}
inter_module_register("drm_agp", THIS_MODULE, &drm_agp);
inter_module_register("drm_agp_3_0", THIS_MODULE, &drm_agp_3_0);
pm_register(PM_PCI_DEV, PM_PCI_ID(agp_bridge->dev), agp_power);
agp_debug_print_lists();
return 0;
}
static void __exit agp_cleanup(void)
{
agp_vm_print_debug_list();
agp_debug_print_lists();
pm_unregister_all(agp_power);
agp_frontend_cleanup();
agp_backend_cleanup();
agp_debug_print_lists();
inter_module_unregister("drm_agp");
inter_module_unregister("drm_agp_3_0");
if(agp_bridge->driver_name) {
kfree((void *)agp_bridge->driver_name);
agp_bridge->driver_name = NULL;
}
if(agp_bridge == &agp_bridge_dummy) {
agp_free_info();
} else {
agp_free_contexts_and_info();
}
}
#ifndef CONFIG_GART_IOMMU
module_init(agp_init);
module_exit(agp_cleanup);
#endif