xref: /linux/drivers/char/agp/uninorth-agp.c (revision 32a92f8c89326985e05dce8b22d3f0aa07a3e1bd)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * UniNorth AGPGART routines.
4  */
5 #include <linux/module.h>
6 #include <linux/of.h>
7 #include <linux/pci.h>
8 #include <linux/slab.h>
9 #include <linux/init.h>
10 #include <linux/pagemap.h>
11 #include <linux/agp_backend.h>
12 #include <linux/delay.h>
13 #include <linux/vmalloc.h>
14 #include <asm/uninorth.h>
15 #include <asm/prom.h>
16 #include <asm/pmac_feature.h>
17 #include "agp.h"
18 
19 /*
20  * NOTES for uninorth3 (G5 AGP) supports :
21  *
22  * There maybe also possibility to have bigger cache line size for
23  * agp (see pmac_pci.c and look for cache line). Need to be investigated
24  * by someone.
25  *
26  * PAGE size are hardcoded but this may change, see asm/page.h.
27  *
28  * Jerome Glisse <j.glisse@gmail.com>
29  */
30 static int uninorth_rev;
31 static int is_u3;
32 static u32 scratch_value;
33 
34 #define DEFAULT_APERTURE_SIZE 256
35 #define DEFAULT_APERTURE_STRING "256"
36 static char *aperture = NULL;
37 
uninorth_fetch_size(void)38 static int uninorth_fetch_size(void)
39 {
40 	int i, size = 0;
41 	struct aper_size_info_32 *values =
42 	    A_SIZE_32(agp_bridge->driver->aperture_sizes);
43 
44 	if (aperture) {
45 		char *save = aperture;
46 
47 		size = memparse(aperture, &aperture) >> 20;
48 		aperture = save;
49 
50 		for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++)
51 			if (size == values[i].size)
52 				break;
53 
54 		if (i == agp_bridge->driver->num_aperture_sizes) {
55 			dev_err(&agp_bridge->dev->dev, "invalid aperture size, "
56 				"using default\n");
57 			size = 0;
58 			aperture = NULL;
59 		}
60 	}
61 
62 	if (!size) {
63 		for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++)
64 			if (values[i].size == DEFAULT_APERTURE_SIZE)
65 				break;
66 	}
67 
68 	agp_bridge->previous_size =
69 	    agp_bridge->current_size = (void *)(values + i);
70 	agp_bridge->aperture_size_idx = i;
71 	return values[i].size;
72 }
73 
uninorth_tlbflush(struct agp_memory * mem)74 static void uninorth_tlbflush(struct agp_memory *mem)
75 {
76 	u32 ctrl = UNI_N_CFG_GART_ENABLE;
77 
78 	if (is_u3)
79 		ctrl |= U3_N_CFG_GART_PERFRD;
80 	pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
81 			       ctrl | UNI_N_CFG_GART_INVAL);
82 	pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, ctrl);
83 
84 	if (!mem && uninorth_rev <= 0x30) {
85 		pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
86 				       ctrl | UNI_N_CFG_GART_2xRESET);
87 		pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
88 				       ctrl);
89 	}
90 }
91 
uninorth_cleanup(void)92 static void uninorth_cleanup(void)
93 {
94 	u32 tmp;
95 
96 	pci_read_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, &tmp);
97 	if (!(tmp & UNI_N_CFG_GART_ENABLE))
98 		return;
99 	tmp |= UNI_N_CFG_GART_INVAL;
100 	pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, tmp);
101 	pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, 0);
102 
103 	if (uninorth_rev <= 0x30) {
104 		pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
105 				       UNI_N_CFG_GART_2xRESET);
106 		pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
107 				       0);
108 	}
109 }
110 
uninorth_configure(void)111 static int uninorth_configure(void)
112 {
113 	struct aper_size_info_32 *current_size;
114 
115 	current_size = A_SIZE_32(agp_bridge->current_size);
116 
117 	dev_info(&agp_bridge->dev->dev, "configuring for size idx: %d\n",
118 		 current_size->size_value);
119 
120 	/* aperture size and gatt addr */
121 	pci_write_config_dword(agp_bridge->dev,
122 		UNI_N_CFG_GART_BASE,
123 		(agp_bridge->gatt_bus_addr & 0xfffff000)
124 			| current_size->size_value);
125 
126 	/* HACK ALERT
127 	 * UniNorth seem to be buggy enough not to handle properly when
128 	 * the AGP aperture isn't mapped at bus physical address 0
129 	 */
130 	agp_bridge->gart_bus_addr = 0;
131 #ifdef CONFIG_PPC64
132 	/* Assume U3 or later on PPC64 systems */
133 	/* high 4 bits of GART physical address go in UNI_N_CFG_AGP_BASE */
134 	pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_AGP_BASE,
135 			       (agp_bridge->gatt_bus_addr >> 32) & 0xf);
136 #else
137 	pci_write_config_dword(agp_bridge->dev,
138 		UNI_N_CFG_AGP_BASE, agp_bridge->gart_bus_addr);
139 #endif
140 
141 	if (is_u3) {
142 		pci_write_config_dword(agp_bridge->dev,
143 				       UNI_N_CFG_GART_DUMMY_PAGE,
144 				       page_to_phys(agp_bridge->scratch_page_page) >> 12);
145 	}
146 
147 	return 0;
148 }
149 
uninorth_insert_memory(struct agp_memory * mem,off_t pg_start,int type)150 static int uninorth_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
151 {
152 	int i, num_entries;
153 	void *temp;
154 	u32 *gp;
155 	int mask_type;
156 
157 	if (type != mem->type)
158 		return -EINVAL;
159 
160 	mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type);
161 	if (mask_type != 0) {
162 		/* We know nothing of memory types */
163 		return -EINVAL;
164 	}
165 
166 	if (mem->page_count == 0)
167 		return 0;
168 
169 	temp = agp_bridge->current_size;
170 	num_entries = A_SIZE_32(temp)->num_entries;
171 
172 	if ((pg_start + mem->page_count) > num_entries)
173 		return -EINVAL;
174 
175 	gp = (u32 *) &agp_bridge->gatt_table[pg_start];
176 	for (i = 0; i < mem->page_count; ++i) {
177 		if (gp[i] != scratch_value) {
178 			dev_info(&agp_bridge->dev->dev,
179 				 "uninorth_insert_memory: entry 0x%x occupied (%x)\n",
180 				 i, gp[i]);
181 			return -EBUSY;
182 		}
183 	}
184 
185 	for (i = 0; i < mem->page_count; i++) {
186 		if (is_u3)
187 			gp[i] = (page_to_phys(mem->pages[i]) >> PAGE_SHIFT) | 0x80000000UL;
188 		else
189 			gp[i] =	cpu_to_le32((page_to_phys(mem->pages[i]) & 0xFFFFF000UL) |
190 					    0x1UL);
191 		flush_dcache_range((unsigned long)__va(page_to_phys(mem->pages[i])),
192 				   (unsigned long)__va(page_to_phys(mem->pages[i]))+0x1000);
193 	}
194 	mb();
195 	uninorth_tlbflush(mem);
196 
197 	return 0;
198 }
199 
uninorth_remove_memory(struct agp_memory * mem,off_t pg_start,int type)200 static int uninorth_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
201 {
202 	size_t i;
203 	u32 *gp;
204 	int mask_type;
205 
206 	if (type != mem->type)
207 		return -EINVAL;
208 
209 	mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type);
210 	if (mask_type != 0) {
211 		/* We know nothing of memory types */
212 		return -EINVAL;
213 	}
214 
215 	if (mem->page_count == 0)
216 		return 0;
217 
218 	gp = (u32 *) &agp_bridge->gatt_table[pg_start];
219 	for (i = 0; i < mem->page_count; ++i) {
220 		gp[i] = scratch_value;
221 	}
222 	mb();
223 	uninorth_tlbflush(mem);
224 
225 	return 0;
226 }
227 
uninorth_agp_enable(struct agp_bridge_data * bridge,u32 mode)228 static void uninorth_agp_enable(struct agp_bridge_data *bridge, u32 mode)
229 {
230 	u32 command, scratch, status;
231 	int timeout;
232 
233 	pci_read_config_dword(bridge->dev,
234 			      bridge->capndx + PCI_AGP_STATUS,
235 			      &status);
236 
237 	command = agp_collect_device_status(bridge, mode, status);
238 	command |= PCI_AGP_COMMAND_AGP;
239 
240 	if (uninorth_rev == 0x21) {
241 		/*
242 		 * Darwin disable AGP 4x on this revision, thus we
243 		 * may assume it's broken. This is an AGP2 controller.
244 		 */
245 		command &= ~AGPSTAT2_4X;
246 	}
247 
248 	if ((uninorth_rev >= 0x30) && (uninorth_rev <= 0x33)) {
249 		/*
250 		 * We need to set REQ_DEPTH to 7 for U3 versions 1.0, 2.1,
251 		 * 2.2 and 2.3, Darwin do so.
252 		 */
253 		if ((command >> AGPSTAT_RQ_DEPTH_SHIFT) > 7)
254 			command = (command & ~AGPSTAT_RQ_DEPTH)
255 				| (7 << AGPSTAT_RQ_DEPTH_SHIFT);
256 	}
257 
258 	uninorth_tlbflush(NULL);
259 
260 	timeout = 0;
261 	do {
262 		pci_write_config_dword(bridge->dev,
263 				       bridge->capndx + PCI_AGP_COMMAND,
264 				       command);
265 		pci_read_config_dword(bridge->dev,
266 				      bridge->capndx + PCI_AGP_COMMAND,
267 				       &scratch);
268 	} while ((scratch & PCI_AGP_COMMAND_AGP) == 0 && ++timeout < 1000);
269 	if ((scratch & PCI_AGP_COMMAND_AGP) == 0)
270 		dev_err(&bridge->dev->dev, "can't write UniNorth AGP "
271 			"command register\n");
272 
273 	if (uninorth_rev >= 0x30) {
274 		/* This is an AGP V3 */
275 		agp_device_command(command, (status & AGPSTAT_MODE_3_0) != 0);
276 	} else {
277 		/* AGP V2 */
278 		agp_device_command(command, false);
279 	}
280 
281 	uninorth_tlbflush(NULL);
282 }
283 
284 #ifdef CONFIG_PM
285 /*
286  * These Power Management routines are _not_ called by the normal PCI PM layer,
287  * but directly by the video driver through function pointers in the device
288  * tree.
289  */
agp_uninorth_suspend(struct pci_dev * pdev)290 static int agp_uninorth_suspend(struct pci_dev *pdev)
291 {
292 	struct agp_bridge_data *bridge;
293 	u32 cmd;
294 	u8 agp;
295 	struct pci_dev *device = NULL;
296 
297 	bridge = agp_find_bridge(pdev);
298 	if (bridge == NULL)
299 		return -ENODEV;
300 
301 	/* Only one suspend supported */
302 	if (bridge->dev_private_data)
303 		return 0;
304 
305 	/* turn off AGP on the video chip, if it was enabled */
306 	for_each_pci_dev(device) {
307 		/* Don't touch the bridge yet, device first */
308 		if (device == pdev)
309 			continue;
310 		/* Only deal with devices on the same bus here, no Mac has a P2P
311 		 * bridge on the AGP port, and mucking around the entire PCI
312 		 * tree is source of problems on some machines because of a bug
313 		 * in some versions of pci_find_capability() when hitting a dead
314 		 * device
315 		 */
316 		if (device->bus != pdev->bus)
317 			continue;
318 		agp = pci_find_capability(device, PCI_CAP_ID_AGP);
319 		if (!agp)
320 			continue;
321 		pci_read_config_dword(device, agp + PCI_AGP_COMMAND, &cmd);
322 		if (!(cmd & PCI_AGP_COMMAND_AGP))
323 			continue;
324 		dev_info(&pdev->dev, "disabling AGP on device %s\n",
325 			 pci_name(device));
326 		cmd &= ~PCI_AGP_COMMAND_AGP;
327 		pci_write_config_dword(device, agp + PCI_AGP_COMMAND, cmd);
328 	}
329 
330 	/* turn off AGP on the bridge */
331 	agp = pci_find_capability(pdev, PCI_CAP_ID_AGP);
332 	pci_read_config_dword(pdev, agp + PCI_AGP_COMMAND, &cmd);
333 	bridge->dev_private_data = (void *)(long)cmd;
334 	if (cmd & PCI_AGP_COMMAND_AGP) {
335 		dev_info(&pdev->dev, "disabling AGP on bridge\n");
336 		cmd &= ~PCI_AGP_COMMAND_AGP;
337 		pci_write_config_dword(pdev, agp + PCI_AGP_COMMAND, cmd);
338 	}
339 	/* turn off the GART */
340 	uninorth_cleanup();
341 
342 	return 0;
343 }
344 
agp_uninorth_resume(struct pci_dev * pdev)345 static int agp_uninorth_resume(struct pci_dev *pdev)
346 {
347 	struct agp_bridge_data *bridge;
348 	u32 command;
349 
350 	bridge = agp_find_bridge(pdev);
351 	if (bridge == NULL)
352 		return -ENODEV;
353 
354 	command = (long)bridge->dev_private_data;
355 	bridge->dev_private_data = NULL;
356 	if (!(command & PCI_AGP_COMMAND_AGP))
357 		return 0;
358 
359 	uninorth_agp_enable(bridge, command);
360 
361 	return 0;
362 }
363 #endif /* CONFIG_PM */
364 
365 static struct {
366 	struct page **pages_arr;
367 } uninorth_priv;
368 
uninorth_create_gatt_table(struct agp_bridge_data * bridge)369 static int uninorth_create_gatt_table(struct agp_bridge_data *bridge)
370 {
371 	char *table;
372 	char *table_end;
373 	int size;
374 	int page_order;
375 	int num_entries;
376 	int i;
377 	void *temp;
378 	struct page *page;
379 
380 	/* We can't handle 2 level gatt's */
381 	if (bridge->driver->size_type == LVL2_APER_SIZE)
382 		return -EINVAL;
383 
384 	table = NULL;
385 	i = bridge->aperture_size_idx;
386 	temp = bridge->current_size;
387 	size = page_order = num_entries = 0;
388 
389 	do {
390 		size = A_SIZE_32(temp)->size;
391 		page_order = A_SIZE_32(temp)->page_order;
392 		num_entries = A_SIZE_32(temp)->num_entries;
393 
394 		table = (char *) __get_free_pages(GFP_KERNEL, page_order);
395 
396 		if (table == NULL) {
397 			i++;
398 			bridge->current_size = A_IDX32(bridge);
399 		} else {
400 			bridge->aperture_size_idx = i;
401 		}
402 	} while (!table && (i < bridge->driver->num_aperture_sizes));
403 
404 	if (table == NULL)
405 		return -ENOMEM;
406 
407 	uninorth_priv.pages_arr = kmalloc_objs(struct page *, 1 << page_order);
408 	if (uninorth_priv.pages_arr == NULL)
409 		goto enomem;
410 
411 	table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
412 
413 	for (page = virt_to_page(table), i = 0; page <= virt_to_page(table_end);
414 	     page++, i++) {
415 		SetPageReserved(page);
416 		uninorth_priv.pages_arr[i] = page;
417 	}
418 
419 	bridge->gatt_table_real = (u32 *) table;
420 	/* Need to clear out any dirty data still sitting in caches */
421 	flush_dcache_range((unsigned long)table,
422 			   (unsigned long)table_end + 1);
423 	bridge->gatt_table = vmap(uninorth_priv.pages_arr, (1 << page_order), 0, PAGE_KERNEL_NCG);
424 
425 	if (bridge->gatt_table == NULL)
426 		goto enomem;
427 
428 	bridge->gatt_bus_addr = virt_to_phys(table);
429 
430 	if (is_u3)
431 		scratch_value = (page_to_phys(agp_bridge->scratch_page_page) >> PAGE_SHIFT) | 0x80000000UL;
432 	else
433 		scratch_value =	cpu_to_le32((page_to_phys(agp_bridge->scratch_page_page) & 0xFFFFF000UL) |
434 				0x1UL);
435 	for (i = 0; i < num_entries; i++)
436 		bridge->gatt_table[i] = scratch_value;
437 
438 	return 0;
439 
440 enomem:
441 	kfree(uninorth_priv.pages_arr);
442 	if (table)
443 		free_pages((unsigned long)table, page_order);
444 	return -ENOMEM;
445 }
446 
uninorth_free_gatt_table(struct agp_bridge_data * bridge)447 static int uninorth_free_gatt_table(struct agp_bridge_data *bridge)
448 {
449 	int page_order;
450 	char *table, *table_end;
451 	void *temp;
452 	struct page *page;
453 
454 	temp = bridge->current_size;
455 	page_order = A_SIZE_32(temp)->page_order;
456 
457 	/* Do not worry about freeing memory, because if this is
458 	 * called, then all agp memory is deallocated and removed
459 	 * from the table.
460 	 */
461 
462 	vunmap(bridge->gatt_table);
463 	kfree(uninorth_priv.pages_arr);
464 	table = (char *) bridge->gatt_table_real;
465 	table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
466 
467 	for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
468 		ClearPageReserved(page);
469 
470 	free_pages((unsigned long) bridge->gatt_table_real, page_order);
471 
472 	return 0;
473 }
474 
null_cache_flush(void)475 static void null_cache_flush(void)
476 {
477 	mb();
478 }
479 
480 /* Setup function */
481 
482 static const struct aper_size_info_32 uninorth_sizes[] =
483 {
484 	{256, 65536, 6, 64},
485 	{128, 32768, 5, 32},
486 	{64, 16384, 4, 16},
487 	{32, 8192, 3, 8},
488 	{16, 4096, 2, 4},
489 	{8, 2048, 1, 2},
490 	{4, 1024, 0, 1}
491 };
492 
493 /*
494  * Not sure that u3 supports that high aperture sizes but it
495  * would strange if it did not :)
496  */
497 static const struct aper_size_info_32 u3_sizes[] =
498 {
499 	{512, 131072, 7, 128},
500 	{256, 65536, 6, 64},
501 	{128, 32768, 5, 32},
502 	{64, 16384, 4, 16},
503 	{32, 8192, 3, 8},
504 	{16, 4096, 2, 4},
505 	{8, 2048, 1, 2},
506 	{4, 1024, 0, 1}
507 };
508 
509 const struct agp_bridge_driver uninorth_agp_driver = {
510 	.owner			= THIS_MODULE,
511 	.aperture_sizes		= (void *)uninorth_sizes,
512 	.size_type		= U32_APER_SIZE,
513 	.num_aperture_sizes	= ARRAY_SIZE(uninorth_sizes),
514 	.configure		= uninorth_configure,
515 	.fetch_size		= uninorth_fetch_size,
516 	.cleanup		= uninorth_cleanup,
517 	.tlb_flush		= uninorth_tlbflush,
518 	.mask_memory		= agp_generic_mask_memory,
519 	.masks			= NULL,
520 	.cache_flush		= null_cache_flush,
521 	.agp_enable		= uninorth_agp_enable,
522 	.create_gatt_table	= uninorth_create_gatt_table,
523 	.free_gatt_table	= uninorth_free_gatt_table,
524 	.insert_memory		= uninorth_insert_memory,
525 	.remove_memory		= uninorth_remove_memory,
526 	.alloc_by_type		= agp_generic_alloc_by_type,
527 	.free_by_type		= agp_generic_free_by_type,
528 	.agp_alloc_page		= agp_generic_alloc_page,
529 	.agp_alloc_pages	= agp_generic_alloc_pages,
530 	.agp_destroy_page	= agp_generic_destroy_page,
531 	.agp_destroy_pages	= agp_generic_destroy_pages,
532 	.agp_type_to_mask_type  = agp_generic_type_to_mask_type,
533 	.cant_use_aperture	= true,
534 	.needs_scratch_page	= true,
535 };
536 
537 const struct agp_bridge_driver u3_agp_driver = {
538 	.owner			= THIS_MODULE,
539 	.aperture_sizes		= (void *)u3_sizes,
540 	.size_type		= U32_APER_SIZE,
541 	.num_aperture_sizes	= ARRAY_SIZE(u3_sizes),
542 	.configure		= uninorth_configure,
543 	.fetch_size		= uninorth_fetch_size,
544 	.cleanup		= uninorth_cleanup,
545 	.tlb_flush		= uninorth_tlbflush,
546 	.mask_memory		= agp_generic_mask_memory,
547 	.masks			= NULL,
548 	.cache_flush		= null_cache_flush,
549 	.agp_enable		= uninorth_agp_enable,
550 	.create_gatt_table	= uninorth_create_gatt_table,
551 	.free_gatt_table	= uninorth_free_gatt_table,
552 	.insert_memory		= uninorth_insert_memory,
553 	.remove_memory		= uninorth_remove_memory,
554 	.alloc_by_type		= agp_generic_alloc_by_type,
555 	.free_by_type		= agp_generic_free_by_type,
556 	.agp_alloc_page		= agp_generic_alloc_page,
557 	.agp_alloc_pages	= agp_generic_alloc_pages,
558 	.agp_destroy_page	= agp_generic_destroy_page,
559 	.agp_destroy_pages	= agp_generic_destroy_pages,
560 	.agp_type_to_mask_type  = agp_generic_type_to_mask_type,
561 	.cant_use_aperture	= true,
562 	.needs_scratch_page	= true,
563 };
564 
565 static struct agp_device_ids uninorth_agp_device_ids[] = {
566 	{
567 		.device_id	= PCI_DEVICE_ID_APPLE_UNI_N_AGP,
568 		.chipset_name	= "UniNorth",
569 	},
570 	{
571 		.device_id	= PCI_DEVICE_ID_APPLE_UNI_N_AGP_P,
572 		.chipset_name	= "UniNorth/Pangea",
573 	},
574 	{
575 		.device_id	= PCI_DEVICE_ID_APPLE_UNI_N_AGP15,
576 		.chipset_name	= "UniNorth 1.5",
577 	},
578 	{
579 		.device_id	= PCI_DEVICE_ID_APPLE_UNI_N_AGP2,
580 		.chipset_name	= "UniNorth 2",
581 	},
582 	{
583 		.device_id	= PCI_DEVICE_ID_APPLE_U3_AGP,
584 		.chipset_name	= "U3",
585 	},
586 	{
587 		.device_id	= PCI_DEVICE_ID_APPLE_U3L_AGP,
588 		.chipset_name	= "U3L",
589 	},
590 	{
591 		.device_id	= PCI_DEVICE_ID_APPLE_U3H_AGP,
592 		.chipset_name	= "U3H",
593 	},
594 	{
595 		.device_id	= PCI_DEVICE_ID_APPLE_IPID2_AGP,
596 		.chipset_name	= "UniNorth/Intrepid2",
597 	},
598 };
599 
agp_uninorth_probe(struct pci_dev * pdev,const struct pci_device_id * ent)600 static int agp_uninorth_probe(struct pci_dev *pdev,
601 			      const struct pci_device_id *ent)
602 {
603 	struct agp_device_ids *devs = uninorth_agp_device_ids;
604 	struct agp_bridge_data *bridge;
605 	struct device_node *uninorth_node;
606 	u8 cap_ptr;
607 	int j;
608 
609 	cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
610 	if (cap_ptr == 0)
611 		return -ENODEV;
612 
613 	/* probe for known chipsets */
614 	for (j = 0; devs[j].chipset_name != NULL; ++j) {
615 		if (pdev->device == devs[j].device_id) {
616 			dev_info(&pdev->dev, "Apple %s chipset\n",
617 				 devs[j].chipset_name);
618 			goto found;
619 		}
620 	}
621 
622 	dev_err(&pdev->dev, "unsupported Apple chipset [%04x/%04x]\n",
623 		pdev->vendor, pdev->device);
624 	return -ENODEV;
625 
626  found:
627 	/* Set revision to 0 if we could not read it. */
628 	uninorth_rev = 0;
629 	is_u3 = 0;
630 	/* Locate core99 Uni-N */
631 	uninorth_node = of_find_node_by_name(NULL, "uni-n");
632 	/* Locate G5 u3 */
633 	if (uninorth_node == NULL) {
634 		is_u3 = 1;
635 		uninorth_node = of_find_node_by_name(NULL, "u3");
636 	}
637 	if (uninorth_node) {
638 		const int *revprop = of_get_property(uninorth_node,
639 				"device-rev", NULL);
640 		if (revprop != NULL)
641 			uninorth_rev = *revprop & 0x3f;
642 		of_node_put(uninorth_node);
643 	}
644 
645 #ifdef CONFIG_PM
646 	/* Inform platform of our suspend/resume caps */
647 	pmac_register_agp_pm(pdev, agp_uninorth_suspend, agp_uninorth_resume);
648 #endif
649 
650 	/* Allocate & setup our driver */
651 	bridge = agp_alloc_bridge();
652 	if (!bridge)
653 		return -ENOMEM;
654 
655 	if (is_u3)
656 		bridge->driver = &u3_agp_driver;
657 	else
658 		bridge->driver = &uninorth_agp_driver;
659 
660 	bridge->dev = pdev;
661 	bridge->capndx = cap_ptr;
662 	bridge->flags = AGP_ERRATA_FASTWRITES;
663 
664 	/* Fill in the mode register */
665 	pci_read_config_dword(pdev, cap_ptr+PCI_AGP_STATUS, &bridge->mode);
666 
667 	pci_set_drvdata(pdev, bridge);
668 	return agp_add_bridge(bridge);
669 }
670 
agp_uninorth_remove(struct pci_dev * pdev)671 static void agp_uninorth_remove(struct pci_dev *pdev)
672 {
673 	struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
674 
675 #ifdef CONFIG_PM
676 	/* Inform platform of our suspend/resume caps */
677 	pmac_register_agp_pm(pdev, NULL, NULL);
678 #endif
679 
680 	agp_remove_bridge(bridge);
681 	agp_put_bridge(bridge);
682 }
683 
684 static const struct pci_device_id agp_uninorth_pci_table[] = {
685 	{
686 	.class		= (PCI_CLASS_BRIDGE_HOST << 8),
687 	.class_mask	= ~0,
688 	.vendor		= PCI_VENDOR_ID_APPLE,
689 	.device		= PCI_ANY_ID,
690 	.subvendor	= PCI_ANY_ID,
691 	.subdevice	= PCI_ANY_ID,
692 	},
693 	{ }
694 };
695 
696 MODULE_DEVICE_TABLE(pci, agp_uninorth_pci_table);
697 
698 static struct pci_driver agp_uninorth_pci_driver = {
699 	.name		= "agpgart-uninorth",
700 	.id_table	= agp_uninorth_pci_table,
701 	.probe		= agp_uninorth_probe,
702 	.remove		= agp_uninorth_remove,
703 };
704 
agp_uninorth_init(void)705 static int __init agp_uninorth_init(void)
706 {
707 	if (agp_off)
708 		return -EINVAL;
709 	return pci_register_driver(&agp_uninorth_pci_driver);
710 }
711 
agp_uninorth_cleanup(void)712 static void __exit agp_uninorth_cleanup(void)
713 {
714 	pci_unregister_driver(&agp_uninorth_pci_driver);
715 }
716 
717 module_init(agp_uninorth_init);
718 module_exit(agp_uninorth_cleanup);
719 
720 module_param(aperture, charp, 0);
721 MODULE_PARM_DESC(aperture,
722 		 "Aperture size, must be power of two between 4MB and an\n"
723 		 "\t\tupper limit specific to the UniNorth revision.\n"
724 		 "\t\tDefault: " DEFAULT_APERTURE_STRING "M");
725 
726 MODULE_AUTHOR("Ben Herrenschmidt & Paul Mackerras");
727 MODULE_DESCRIPTION("Apple UniNorth & U3 AGP support");
728 MODULE_LICENSE("GPL");
729