@@ -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
105166struct 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}
129192EXPORT_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}
163231EXPORT_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
10671154void 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}
10731160EXPORT_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}
11661275EXPORT_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