Skip to content

Commit a030ce4

Browse files
Thomas HellstromDave Jones
authored andcommitted
[AGPGART] Allow drm-populated agp memory types
This patch allows drm to populate an agpgart structure with pages of its own. It's needed for the new drm memory manager which dynamically flips pages in and out of AGP. The patch modifies the generic functions as well as the intel agp driver. The intel drm driver is currently the only one supporting the new memory manager. Other agp drivers may need some minor fixing up once they have a corresponding memory manager enabled drm driver. AGP memory types >= AGP_USER_TYPES are not populated by the agpgart driver, but the drm is expected to do that, as well as taking care of cache- and tlb flushing when needed. It's not possible to request these types from user space using agpgart ioctls. The Intel driver also gets a new memory type for pages that can be bound cached to the intel GTT. Signed-off-by: Thomas Hellstrom <thomas@tungstengraphics.com> Signed-off-by: Dave Jones <davej@redhat.com>
1 parent 0316fe8 commit a030ce4

19 files changed

Lines changed: 296 additions & 75 deletions

‎drivers/char/agp/agp.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ struct agp_bridge_driver {
114114
void (*free_by_type)(struct agp_memory *);
115115
void *(*agp_alloc_page)(struct agp_bridge_data *);
116116
void (*agp_destroy_page)(void *);
117+
int (*agp_type_to_mask_type) (struct agp_bridge_data *, int);
117118
};
118119

119120
struct agp_bridge_data {
@@ -218,6 +219,7 @@ struct agp_bridge_data {
218219
#define I810_PTE_MAIN_UNCACHED 0x00000000
219220
#define I810_PTE_LOCAL 0x00000002
220221
#define I810_PTE_VALID 0x00000001
222+
#define I830_PTE_SYSTEM_CACHED 0x00000006
221223
#define I810_SMRAM_MISCC 0x70
222224
#define I810_GFX_MEM_WIN_SIZE 0x00010000
223225
#define I810_GFX_MEM_WIN_32M 0x00010000
@@ -270,8 +272,16 @@ void global_cache_flush(void);
270272
void get_agp_version(struct agp_bridge_data *bridge);
271273
unsigned long agp_generic_mask_memory(struct agp_bridge_data *bridge,
272274
unsigned long addr, int type);
275+
int agp_generic_type_to_mask_type(struct agp_bridge_data *bridge,
276+
int type);
273277
struct agp_bridge_data *agp_generic_find_bridge(struct pci_dev *pdev);
274278

279+
/* generic functions for user-populated AGP memory types */
280+
struct agp_memory *agp_generic_alloc_user(size_t page_count, int type);
281+
void agp_alloc_page_array(size_t size, struct agp_memory *mem);
282+
void agp_free_page_array(struct agp_memory *mem);
283+
284+
275285
/* generic routines for agp>=3 */
276286
int agp3_generic_fetch_size(void);
277287
void agp3_generic_tlbflush(struct agp_memory *mem);

‎drivers/char/agp/ali-agp.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ static struct agp_bridge_driver ali_generic_bridge = {
214214
.free_by_type = agp_generic_free_by_type,
215215
.agp_alloc_page = agp_generic_alloc_page,
216216
.agp_destroy_page = ali_destroy_page,
217+
.agp_type_to_mask_type = agp_generic_type_to_mask_type,
217218
};
218219

219220
static struct agp_bridge_driver ali_m1541_bridge = {
@@ -237,6 +238,7 @@ static struct agp_bridge_driver ali_m1541_bridge = {
237238
.free_by_type = agp_generic_free_by_type,
238239
.agp_alloc_page = m1541_alloc_page,
239240
.agp_destroy_page = m1541_destroy_page,
241+
.agp_type_to_mask_type = agp_generic_type_to_mask_type,
240242
};
241243

242244

‎drivers/char/agp/alpha-agp.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ static int alpha_core_agp_insert_memory(struct agp_memory *mem, off_t pg_start,
9191
int num_entries, status;
9292
void *temp;
9393

94+
if (type >= AGP_USER_TYPES || mem->type >= AGP_USER_TYPES)
95+
return -EINVAL;
96+
9497
temp = agp_bridge->current_size;
9598
num_entries = A_SIZE_FIX(temp)->num_entries;
9699
if ((pg_start + mem->page_count) > num_entries)
@@ -142,6 +145,7 @@ struct agp_bridge_driver alpha_core_agp_driver = {
142145
.free_by_type = agp_generic_free_by_type,
143146
.agp_alloc_page = agp_generic_alloc_page,
144147
.agp_destroy_page = agp_generic_destroy_page,
148+
.agp_type_to_mask_type = agp_generic_type_to_mask_type,
145149
};
146150

147151
struct agp_bridge_data *alpha_bridge;

‎drivers/char/agp/amd-k7-agp.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ static struct agp_bridge_driver amd_irongate_driver = {
381381
.free_by_type = agp_generic_free_by_type,
382382
.agp_alloc_page = agp_generic_alloc_page,
383383
.agp_destroy_page = agp_generic_destroy_page,
384+
.agp_type_to_mask_type = agp_generic_type_to_mask_type,
384385
};
385386

386387
static struct agp_device_ids amd_agp_device_ids[] __devinitdata =

‎drivers/char/agp/amd64-agp.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,18 @@ static int amd64_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
6262
{
6363
int i, j, num_entries;
6464
long long tmp;
65+
int mask_type;
66+
struct agp_bridge_data *bridge = mem->bridge;
6567
u32 pte;
6668

6769
num_entries = agp_num_entries();
6870

69-
if (type != 0 || mem->type != 0)
71+
if (type != mem->type)
7072
return -EINVAL;
73+
mask_type = bridge->driver->agp_type_to_mask_type(bridge, type);
74+
if (mask_type != 0)
75+
return -EINVAL;
76+
7177

7278
/* Make sure we can fit the range in the gatt table. */
7379
/* FIXME: could wrap */
@@ -90,7 +96,7 @@ static int amd64_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
9096

9197
for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
9298
tmp = agp_bridge->driver->mask_memory(agp_bridge,
93-
mem->memory[i], mem->type);
99+
mem->memory[i], mask_type);
94100

95101
BUG_ON(tmp & 0xffffff0000000ffcULL);
96102
pte = (tmp & 0x000000ff00000000ULL) >> 28;
@@ -247,6 +253,7 @@ static struct agp_bridge_driver amd_8151_driver = {
247253
.free_by_type = agp_generic_free_by_type,
248254
.agp_alloc_page = agp_generic_alloc_page,
249255
.agp_destroy_page = agp_generic_destroy_page,
256+
.agp_type_to_mask_type = agp_generic_type_to_mask_type,
250257
};
251258

252259
/* Some basic sanity checks for the aperture. */

‎drivers/char/agp/ati-agp.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ static struct agp_bridge_driver ati_generic_bridge = {
431431
.free_by_type = agp_generic_free_by_type,
432432
.agp_alloc_page = agp_generic_alloc_page,
433433
.agp_destroy_page = agp_generic_destroy_page,
434+
.agp_type_to_mask_type = agp_generic_type_to_mask_type,
434435
};
435436

436437

‎drivers/char/agp/backend.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* fix some real stupidity. It's only by chance we can bump
4444
* past 0.99 at all due to some boolean logic error. */
4545
#define AGPGART_VERSION_MAJOR 0
46-
#define AGPGART_VERSION_MINOR 101
46+
#define AGPGART_VERSION_MINOR 102
4747
static const struct agp_version agp_current_version =
4848
{
4949
.major = AGPGART_VERSION_MAJOR,

‎drivers/char/agp/efficeon-agp.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ static struct agp_bridge_driver efficeon_driver = {
335335
.free_by_type = agp_generic_free_by_type,
336336
.agp_alloc_page = agp_generic_alloc_page,
337337
.agp_destroy_page = agp_generic_destroy_page,
338+
.agp_type_to_mask_type = agp_generic_type_to_mask_type,
338339
};
339340

340341
static int __devinit agp_efficeon_probe(struct pci_dev *pdev,

‎drivers/char/agp/frontend.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,9 @@ static int agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg)
892892
if (copy_from_user(&alloc, arg, sizeof(struct agp_allocate)))
893893
return -EFAULT;
894894

895+
if (alloc.type >= AGP_USER_TYPES)
896+
return -EINVAL;
897+
895898
memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type);
896899

897900
if (memory == NULL)

‎drivers/char/agp/generic.c‎

Lines changed: 124 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,67 @@ static int agp_get_key(void)
101101
return -1;
102102
}
103103

104+
/*
105+
* Use kmalloc if possible for the page list. Otherwise fall back to
106+
* vmalloc. This speeds things up and also saves memory for small AGP
107+
* regions.
108+
*/
109+
110+
void agp_alloc_page_array(size_t size, struct agp_memory *mem)
111+
{
112+
mem->memory = NULL;
113+
mem->vmalloc_flag = 0;
114+
115+
if (size <= 2*PAGE_SIZE) {
116+
mem->memory = kmalloc(size, GFP_KERNEL | __GFP_NORETRY);
117+
}
118+
if (mem->memory == NULL) {
119+
mem->memory = vmalloc(size);
120+
mem->vmalloc_flag = 1;
121+
}
122+
}
123+
EXPORT_SYMBOL(agp_alloc_page_array);
124+
125+
void agp_free_page_array(struct agp_memory *mem)
126+
{
127+
if (mem->vmalloc_flag) {
128+
vfree(mem->memory);
129+
} else {
130+
kfree(mem->memory);
131+
}
132+
}
133+
EXPORT_SYMBOL(agp_free_page_array);
134+
135+
136+
static struct agp_memory *agp_create_user_memory(unsigned long num_agp_pages)
137+
{
138+
struct agp_memory *new;
139+
unsigned long alloc_size = num_agp_pages*sizeof(struct page *);
140+
141+
new = kmalloc(sizeof(struct agp_memory), GFP_KERNEL);
142+
143+
if (new == NULL)
144+
return NULL;
145+
146+
memset(new, 0, sizeof(struct agp_memory));
147+
new->key = agp_get_key();
148+
149+
if (new->key < 0) {
150+
kfree(new);
151+
return NULL;
152+
}
153+
154+
agp_alloc_page_array(alloc_size, new);
155+
156+
if (new->memory == NULL) {
157+
agp_free_key(new->key);
158+
kfree(new);
159+
return NULL;
160+
}
161+
new->num_scratch_pages = 0;
162+
return new;
163+
}
164+
104165

105166
struct agp_memory *agp_create_memory(int scratch_pages)
106167
{
@@ -116,14 +177,16 @@ struct agp_memory *agp_create_memory(int scratch_pages)
116177
kfree(new);
117178
return NULL;
118179
}
119-
new->memory = vmalloc(PAGE_SIZE * scratch_pages);
180+
181+
agp_alloc_page_array(PAGE_SIZE * scratch_pages, new);
120182

121183
if (new->memory == NULL) {
122184
agp_free_key(new->key);
123185
kfree(new);
124186
return NULL;
125187
}
126188
new->num_scratch_pages = scratch_pages;
189+
new->type = AGP_NORMAL_MEMORY;
127190
return new;
128191
}
129192
EXPORT_SYMBOL(agp_create_memory);
@@ -146,6 +209,11 @@ void agp_free_memory(struct agp_memory *curr)
146209
if (curr->is_bound == TRUE)
147210
agp_unbind_memory(curr);
148211

212+
if (curr->type >= AGP_USER_TYPES) {
213+
agp_generic_free_by_type(curr);
214+
return;
215+
}
216+
149217
if (curr->type != 0) {
150218
curr->bridge->driver->free_by_type(curr);
151219
return;
@@ -157,7 +225,7 @@ void agp_free_memory(struct agp_memory *curr)
157225
flush_agp_mappings();
158226
}
159227
agp_free_key(curr->key);
160-
vfree(curr->memory);
228+
agp_free_page_array(curr);
161229
kfree(curr);
162230
}
163231
EXPORT_SYMBOL(agp_free_memory);
@@ -188,6 +256,13 @@ struct agp_memory *agp_allocate_memory(struct agp_bridge_data *bridge,
188256
if ((atomic_read(&bridge->current_memory_agp) + page_count) > bridge->max_memory_agp)
189257
return NULL;
190258

259+
if (type >= AGP_USER_TYPES) {
260+
new = agp_generic_alloc_user(page_count, type);
261+
if (new)
262+
new->bridge = bridge;
263+
return new;
264+
}
265+
191266
if (type != 0) {
192267
new = bridge->driver->alloc_by_type(page_count, type);
193268
if (new)
@@ -960,6 +1035,7 @@ int agp_generic_insert_memory(struct agp_memory * mem, off_t pg_start, int type)
9601035
off_t j;
9611036
void *temp;
9621037
struct agp_bridge_data *bridge;
1038+
int mask_type;
9631039

9641040
bridge = mem->bridge;
9651041
if (!bridge)
@@ -995,7 +1071,12 @@ int agp_generic_insert_memory(struct agp_memory * mem, off_t pg_start, int type)
9951071
num_entries -= agp_memory_reserved/PAGE_SIZE;
9961072
if (num_entries < 0) num_entries = 0;
9971073

998-
if (type != 0 || mem->type != 0) {
1074+
if (type != mem->type) {
1075+
return -EINVAL;
1076+
}
1077+
1078+
mask_type = bridge->driver->agp_type_to_mask_type(bridge, type);
1079+
if (mask_type != 0) {
9991080
/* The generic routines know nothing of memory types */
10001081
return -EINVAL;
10011082
}
@@ -1018,7 +1099,8 @@ int agp_generic_insert_memory(struct agp_memory * mem, off_t pg_start, int type)
10181099
}
10191100

10201101
for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
1021-
writel(bridge->driver->mask_memory(bridge, mem->memory[i], mem->type), bridge->gatt_table+j);
1102+
writel(bridge->driver->mask_memory(bridge, mem->memory[i], mask_type),
1103+
bridge->gatt_table+j);
10221104
}
10231105
readl(bridge->gatt_table+j-1); /* PCI Posting. */
10241106

@@ -1032,6 +1114,7 @@ int agp_generic_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
10321114
{
10331115
size_t i;
10341116
struct agp_bridge_data *bridge;
1117+
int mask_type;
10351118

10361119
bridge = mem->bridge;
10371120
if (!bridge)
@@ -1040,7 +1123,11 @@ int agp_generic_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
10401123
if (mem->page_count == 0)
10411124
return 0;
10421125

1043-
if (type != 0 || mem->type != 0) {
1126+
if (type != mem->type)
1127+
return -EINVAL;
1128+
1129+
mask_type = bridge->driver->agp_type_to_mask_type(bridge, type);
1130+
if (mask_type != 0) {
10441131
/* The generic routines know nothing of memory types */
10451132
return -EINVAL;
10461133
}
@@ -1066,12 +1153,34 @@ EXPORT_SYMBOL(agp_generic_alloc_by_type);
10661153

10671154
void agp_generic_free_by_type(struct agp_memory *curr)
10681155
{
1069-
vfree(curr->memory);
1156+
agp_free_page_array(curr);
10701157
agp_free_key(curr->key);
10711158
kfree(curr);
10721159
}
10731160
EXPORT_SYMBOL(agp_generic_free_by_type);
10741161

1162+
struct agp_memory *agp_generic_alloc_user(size_t page_count, int type)
1163+
{
1164+
struct agp_memory *new;
1165+
int i;
1166+
int pages;
1167+
1168+
pages = (page_count + ENTRIES_PER_PAGE - 1) / ENTRIES_PER_PAGE;
1169+
new = agp_create_user_memory(page_count);
1170+
if (new == NULL)
1171+
return NULL;
1172+
1173+
for (i = 0; i < page_count; i++) {
1174+
new->memory[i] = 0;
1175+
}
1176+
new->page_count = 0;
1177+
new->type = type;
1178+
new->num_scratch_pages = pages;
1179+
1180+
return new;
1181+
}
1182+
EXPORT_SYMBOL(agp_generic_alloc_user);
1183+
10751184

10761185
/*
10771186
* Basic Page Allocation Routines -
@@ -1165,6 +1274,15 @@ unsigned long agp_generic_mask_memory(struct agp_bridge_data *bridge,
11651274
}
11661275
EXPORT_SYMBOL(agp_generic_mask_memory);
11671276

1277+
int agp_generic_type_to_mask_type(struct agp_bridge_data *bridge,
1278+
int type)
1279+
{
1280+
if (type >= AGP_USER_TYPES)
1281+
return 0;
1282+
return type;
1283+
}
1284+
EXPORT_SYMBOL(agp_generic_type_to_mask_type);
1285+
11681286
/*
11691287
* These functions are implemented according to the AGPv3 spec,
11701288
* which covers implementation details that had previously been

0 commit comments

Comments
 (0)