blob: 4e05021f3f18b04d1096e8babce9e7438ef82ba1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * UniNorth AGPGART routines.
3 */
4#include <linux/module.h>
5#include <linux/pci.h>
6#include <linux/init.h>
7#include <linux/pagemap.h>
8#include <linux/agp_backend.h>
9#include <linux/delay.h>
Michel Dänzere8a5f902009-08-04 11:51:04 +000010#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/uninorth.h>
12#include <asm/pci-bridge.h>
13#include <asm/prom.h>
Benjamin Herrenschmidt0c541b42005-04-16 15:24:19 -070014#include <asm/pmac_feature.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "agp.h"
16
17/*
18 * NOTES for uninorth3 (G5 AGP) supports :
19 *
20 * There maybe also possibility to have bigger cache line size for
21 * agp (see pmac_pci.c and look for cache line). Need to be investigated
22 * by someone.
23 *
24 * PAGE size are hardcoded but this may change, see asm/page.h.
25 *
26 * Jerome Glisse <j.glisse@gmail.com>
27 */
28static int uninorth_rev;
29static int is_u3;
30
Michel Dänzer52f072c2009-08-04 11:51:03 +000031#define DEFAULT_APERTURE_SIZE 256
32#define DEFAULT_APERTURE_STRING "256"
Al Virob0385142008-11-22 17:36:34 +000033static char *aperture = NULL;
Benjamin Herrenschmidt0c541b42005-04-16 15:24:19 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static int uninorth_fetch_size(void)
36{
Michel Dänzer18088742006-10-04 14:56:44 +020037 int i, size = 0;
38 struct aper_size_info_32 *values =
39 A_SIZE_32(agp_bridge->driver->aperture_sizes);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Michel Dänzer18088742006-10-04 14:56:44 +020041 if (aperture) {
42 char *save = aperture;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Michel Dänzer18088742006-10-04 14:56:44 +020044 size = memparse(aperture, &aperture) >> 20;
45 aperture = save;
46
47 for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++)
48 if (size == values[i].size)
49 break;
50
51 if (i == agp_bridge->driver->num_aperture_sizes) {
Bjorn Helgaase3cf6952008-07-30 12:26:51 -070052 dev_err(&agp_bridge->dev->dev, "invalid aperture size, "
53 "using default\n");
Michel Dänzer18088742006-10-04 14:56:44 +020054 size = 0;
55 aperture = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
57 }
58
Michel Dänzer18088742006-10-04 14:56:44 +020059 if (!size) {
60 for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++)
Michel Dänzer52f072c2009-08-04 11:51:03 +000061 if (values[i].size == DEFAULT_APERTURE_SIZE)
Michel Dänzer18088742006-10-04 14:56:44 +020062 break;
63 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Michel Dänzer18088742006-10-04 14:56:44 +020065 agp_bridge->previous_size =
66 agp_bridge->current_size = (void *)(values + i);
67 agp_bridge->aperture_size_idx = i;
68 return values[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71static void uninorth_tlbflush(struct agp_memory *mem)
72{
73 u32 ctrl = UNI_N_CFG_GART_ENABLE;
74
75 if (is_u3)
76 ctrl |= U3_N_CFG_GART_PERFRD;
77 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
78 ctrl | UNI_N_CFG_GART_INVAL);
79 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, ctrl);
80
81 if (uninorth_rev <= 0x30) {
82 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
83 ctrl | UNI_N_CFG_GART_2xRESET);
84 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
85 ctrl);
86 }
87}
88
89static void uninorth_cleanup(void)
90{
91 u32 tmp;
92
93 pci_read_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, &tmp);
94 if (!(tmp & UNI_N_CFG_GART_ENABLE))
95 return;
96 tmp |= UNI_N_CFG_GART_INVAL;
97 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, tmp);
98 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, 0);
99
100 if (uninorth_rev <= 0x30) {
101 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
102 UNI_N_CFG_GART_2xRESET);
103 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
104 0);
105 }
106}
107
108static int uninorth_configure(void)
109{
110 struct aper_size_info_32 *current_size;
Dave Jones6a92a4e2006-02-28 00:54:25 -0500111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 current_size = A_SIZE_32(agp_bridge->current_size);
113
Bjorn Helgaase3cf6952008-07-30 12:26:51 -0700114 dev_info(&agp_bridge->dev->dev, "configuring for size idx: %d\n",
115 current_size->size_value);
Dave Jones6a92a4e2006-02-28 00:54:25 -0500116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* aperture size and gatt addr */
118 pci_write_config_dword(agp_bridge->dev,
119 UNI_N_CFG_GART_BASE,
120 (agp_bridge->gatt_bus_addr & 0xfffff000)
121 | current_size->size_value);
122
123 /* HACK ALERT
124 * UniNorth seem to be buggy enough not to handle properly when
125 * the AGP aperture isn't mapped at bus physical address 0
126 */
127 agp_bridge->gart_bus_addr = 0;
128#ifdef CONFIG_PPC64
129 /* Assume U3 or later on PPC64 systems */
130 /* high 4 bits of GART physical address go in UNI_N_CFG_AGP_BASE */
131 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_AGP_BASE,
132 (agp_bridge->gatt_bus_addr >> 32) & 0xf);
133#else
134 pci_write_config_dword(agp_bridge->dev,
135 UNI_N_CFG_AGP_BASE, agp_bridge->gart_bus_addr);
136#endif
137
138 if (is_u3) {
139 pci_write_config_dword(agp_bridge->dev,
140 UNI_N_CFG_GART_DUMMY_PAGE,
David Woodhouse5e8d6b82009-08-06 20:20:43 +1000141 page_to_phys(agp_bridge->scratch_page_page) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
Dave Jones6a92a4e2006-02-28 00:54:25 -0500143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return 0;
145}
146
147static int uninorth_insert_memory(struct agp_memory *mem, off_t pg_start,
148 int type)
149{
150 int i, j, num_entries;
151 void *temp;
Michel Dänzer62369022009-06-15 16:56:15 +0200152 int mask_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Michel Dänzer62369022009-06-15 16:56:15 +0200154 if (type != mem->type)
155 return -EINVAL;
156
157 mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type);
158 if (mask_type != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* We know nothing of memory types */
160 return -EINVAL;
Michel Dänzer62369022009-06-15 16:56:15 +0200161 }
162
Michel Dänzer3fc3a6b2009-12-06 02:15:55 +0000163 if (mem->page_count == 0)
164 return 0;
165
166 temp = agp_bridge->current_size;
167 num_entries = A_SIZE_32(temp)->num_entries;
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if ((pg_start + mem->page_count) > num_entries)
170 return -EINVAL;
171
172 j = pg_start;
173
174 while (j < (pg_start + mem->page_count)) {
175 if (agp_bridge->gatt_table[j])
176 return -EBUSY;
177 j++;
178 }
179
180 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
181 agp_bridge->gatt_table[j] =
Dave Airlie07613ba2009-06-12 14:11:41 +1000182 cpu_to_le32((page_to_phys(mem->pages[i]) & 0xFFFFF000UL) | 0x1UL);
183 flush_dcache_range((unsigned long)__va(page_to_phys(mem->pages[i])),
184 (unsigned long)__va(page_to_phys(mem->pages[i]))+0x1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186 (void)in_le32((volatile u32*)&agp_bridge->gatt_table[pg_start]);
187 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 uninorth_tlbflush(mem);
190 return 0;
191}
192
193static int u3_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
194{
195 int i, num_entries;
196 void *temp;
197 u32 *gp;
Michel Dänzer62369022009-06-15 16:56:15 +0200198 int mask_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Michel Dänzer62369022009-06-15 16:56:15 +0200200 if (type != mem->type)
201 return -EINVAL;
202
203 mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type);
204 if (mask_type != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* We know nothing of memory types */
206 return -EINVAL;
Michel Dänzer62369022009-06-15 16:56:15 +0200207 }
208
Michel Dänzer3fc3a6b2009-12-06 02:15:55 +0000209 if (mem->page_count == 0)
210 return 0;
211
212 temp = agp_bridge->current_size;
213 num_entries = A_SIZE_32(temp)->num_entries;
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if ((pg_start + mem->page_count) > num_entries)
216 return -EINVAL;
217
218 gp = (u32 *) &agp_bridge->gatt_table[pg_start];
219 for (i = 0; i < mem->page_count; ++i) {
220 if (gp[i]) {
Bjorn Helgaase3cf6952008-07-30 12:26:51 -0700221 dev_info(&agp_bridge->dev->dev,
222 "u3_insert_memory: entry 0x%x occupied (%x)\n",
223 i, gp[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EBUSY;
225 }
226 }
227
228 for (i = 0; i < mem->page_count; i++) {
Dave Airlie07613ba2009-06-12 14:11:41 +1000229 gp[i] = (page_to_phys(mem->pages[i]) >> PAGE_SHIFT) | 0x80000000UL;
230 flush_dcache_range((unsigned long)__va(page_to_phys(mem->pages[i])),
231 (unsigned long)__va(page_to_phys(mem->pages[i]))+0x1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 uninorth_tlbflush(mem);
235
236 return 0;
237}
238
239int u3_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
240{
241 size_t i;
242 u32 *gp;
Michel Dänzer3fc3a6b2009-12-06 02:15:55 +0000243 int mask_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Michel Dänzer3fc3a6b2009-12-06 02:15:55 +0000245 if (type != mem->type)
246 return -EINVAL;
247
248 mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type);
249 if (mask_type != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* We know nothing of memory types */
251 return -EINVAL;
Michel Dänzer3fc3a6b2009-12-06 02:15:55 +0000252 }
253
254 if (mem->page_count == 0)
255 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 gp = (u32 *) &agp_bridge->gatt_table[pg_start];
258 for (i = 0; i < mem->page_count; ++i)
259 gp[i] = 0;
260 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 uninorth_tlbflush(mem);
262
263 return 0;
264}
265
266static void uninorth_agp_enable(struct agp_bridge_data *bridge, u32 mode)
267{
268 u32 command, scratch, status;
269 int timeout;
270
271 pci_read_config_dword(bridge->dev,
272 bridge->capndx + PCI_AGP_STATUS,
273 &status);
274
275 command = agp_collect_device_status(bridge, mode, status);
276 command |= PCI_AGP_COMMAND_AGP;
Dave Jones6a92a4e2006-02-28 00:54:25 -0500277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (uninorth_rev == 0x21) {
279 /*
280 * Darwin disable AGP 4x on this revision, thus we
281 * may assume it's broken. This is an AGP2 controller.
282 */
283 command &= ~AGPSTAT2_4X;
284 }
285
286 if ((uninorth_rev >= 0x30) && (uninorth_rev <= 0x33)) {
287 /*
Anand Gadiyarfd589a82009-07-16 17:13:03 +0200288 * We need to set REQ_DEPTH to 7 for U3 versions 1.0, 2.1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * 2.2 and 2.3, Darwin do so.
290 */
291 if ((command >> AGPSTAT_RQ_DEPTH_SHIFT) > 7)
292 command = (command & ~AGPSTAT_RQ_DEPTH)
293 | (7 << AGPSTAT_RQ_DEPTH_SHIFT);
294 }
295
296 uninorth_tlbflush(NULL);
297
298 timeout = 0;
299 do {
300 pci_write_config_dword(bridge->dev,
301 bridge->capndx + PCI_AGP_COMMAND,
302 command);
303 pci_read_config_dword(bridge->dev,
304 bridge->capndx + PCI_AGP_COMMAND,
305 &scratch);
306 } while ((scratch & PCI_AGP_COMMAND_AGP) == 0 && ++timeout < 1000);
307 if ((scratch & PCI_AGP_COMMAND_AGP) == 0)
Bjorn Helgaase3cf6952008-07-30 12:26:51 -0700308 dev_err(&bridge->dev->dev, "can't write UniNorth AGP "
309 "command register\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 if (uninorth_rev >= 0x30) {
312 /* This is an AGP V3 */
Joe Perchesc7258012008-03-26 14:10:02 -0700313 agp_device_command(command, (status & AGPSTAT_MODE_3_0) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 } else {
315 /* AGP V2 */
Joe Perchesc7258012008-03-26 14:10:02 -0700316 agp_device_command(command, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
319 uninorth_tlbflush(NULL);
320}
321
322#ifdef CONFIG_PM
Benjamin Herrenschmidt0c541b42005-04-16 15:24:19 -0700323/*
324 * These Power Management routines are _not_ called by the normal PCI PM layer,
325 * but directly by the video driver through function pointers in the device
326 * tree.
327 */
328static int agp_uninorth_suspend(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Benjamin Herrenschmidt0c541b42005-04-16 15:24:19 -0700330 struct agp_bridge_data *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 u32 cmd;
332 u8 agp;
333 struct pci_dev *device = NULL;
334
Benjamin Herrenschmidt0c541b42005-04-16 15:24:19 -0700335 bridge = agp_find_bridge(pdev);
336 if (bridge == NULL)
337 return -ENODEV;
338
339 /* Only one suspend supported */
340 if (bridge->dev_private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return 0;
342
343 /* turn off AGP on the video chip, if it was enabled */
344 for_each_pci_dev(device) {
345 /* Don't touch the bridge yet, device first */
346 if (device == pdev)
347 continue;
348 /* Only deal with devices on the same bus here, no Mac has a P2P
349 * bridge on the AGP port, and mucking around the entire PCI
350 * tree is source of problems on some machines because of a bug
351 * in some versions of pci_find_capability() when hitting a dead
352 * device
353 */
354 if (device->bus != pdev->bus)
355 continue;
356 agp = pci_find_capability(device, PCI_CAP_ID_AGP);
357 if (!agp)
358 continue;
359 pci_read_config_dword(device, agp + PCI_AGP_COMMAND, &cmd);
360 if (!(cmd & PCI_AGP_COMMAND_AGP))
361 continue;
Bjorn Helgaase3cf6952008-07-30 12:26:51 -0700362 dev_info(&pdev->dev, "disabling AGP on device %s\n",
363 pci_name(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 cmd &= ~PCI_AGP_COMMAND_AGP;
365 pci_write_config_dword(device, agp + PCI_AGP_COMMAND, cmd);
366 }
367
368 /* turn off AGP on the bridge */
369 agp = pci_find_capability(pdev, PCI_CAP_ID_AGP);
370 pci_read_config_dword(pdev, agp + PCI_AGP_COMMAND, &cmd);
Andrew Mortonb07cd512006-06-01 20:19:35 -0700371 bridge->dev_private_data = (void *)(long)cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (cmd & PCI_AGP_COMMAND_AGP) {
Bjorn Helgaase3cf6952008-07-30 12:26:51 -0700373 dev_info(&pdev->dev, "disabling AGP on bridge\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 cmd &= ~PCI_AGP_COMMAND_AGP;
375 pci_write_config_dword(pdev, agp + PCI_AGP_COMMAND, cmd);
376 }
377 /* turn off the GART */
378 uninorth_cleanup();
379
380 return 0;
381}
382
383static int agp_uninorth_resume(struct pci_dev *pdev)
384{
Benjamin Herrenschmidt0c541b42005-04-16 15:24:19 -0700385 struct agp_bridge_data *bridge;
386 u32 command;
387
388 bridge = agp_find_bridge(pdev);
389 if (bridge == NULL)
390 return -ENODEV;
391
Andrew Mortonb07cd512006-06-01 20:19:35 -0700392 command = (long)bridge->dev_private_data;
Benjamin Herrenschmidt0c541b42005-04-16 15:24:19 -0700393 bridge->dev_private_data = NULL;
394 if (!(command & PCI_AGP_COMMAND_AGP))
395 return 0;
396
397 uninorth_agp_enable(bridge, command);
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return 0;
400}
Benjamin Herrenschmidt0c541b42005-04-16 15:24:19 -0700401#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403static int uninorth_create_gatt_table(struct agp_bridge_data *bridge)
404{
405 char *table;
406 char *table_end;
407 int size;
408 int page_order;
409 int num_entries;
410 int i;
411 void *temp;
412 struct page *page;
Michel Dänzere8a5f902009-08-04 11:51:04 +0000413 struct page **pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* We can't handle 2 level gatt's */
416 if (bridge->driver->size_type == LVL2_APER_SIZE)
417 return -EINVAL;
418
419 table = NULL;
420 i = bridge->aperture_size_idx;
421 temp = bridge->current_size;
422 size = page_order = num_entries = 0;
423
424 do {
425 size = A_SIZE_32(temp)->size;
426 page_order = A_SIZE_32(temp)->page_order;
427 num_entries = A_SIZE_32(temp)->num_entries;
428
429 table = (char *) __get_free_pages(GFP_KERNEL, page_order);
430
431 if (table == NULL) {
432 i++;
433 bridge->current_size = A_IDX32(bridge);
434 } else {
435 bridge->aperture_size_idx = i;
436 }
437 } while (!table && (i < bridge->driver->num_aperture_sizes));
438
439 if (table == NULL)
440 return -ENOMEM;
441
Michel Dänzere8a5f902009-08-04 11:51:04 +0000442 pages = kmalloc((1 << page_order) * sizeof(struct page*), GFP_KERNEL);
443 if (pages == NULL)
444 goto enomem;
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
447
Michel Dänzere8a5f902009-08-04 11:51:04 +0000448 for (page = virt_to_page(table), i = 0; page <= virt_to_page(table_end);
449 page++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 SetPageReserved(page);
Michel Dänzere8a5f902009-08-04 11:51:04 +0000451 pages[i] = page;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 bridge->gatt_table_real = (u32 *) table;
Michel Dänzere8a5f902009-08-04 11:51:04 +0000455 /* Need to clear out any dirty data still sitting in caches */
456 flush_dcache_range((unsigned long)table,
457 (unsigned long)(table_end + PAGE_SIZE));
458 bridge->gatt_table = vmap(pages, (1 << page_order), 0, PAGE_KERNEL_NCG);
459
460 if (bridge->gatt_table == NULL)
461 goto enomem;
462
David Woodhouse6a122352009-07-29 10:25:58 +0100463 bridge->gatt_bus_addr = virt_to_phys(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 for (i = 0; i < num_entries; i++)
466 bridge->gatt_table[i] = 0;
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return 0;
Michel Dänzere8a5f902009-08-04 11:51:04 +0000469
470enomem:
471 kfree(pages);
472 if (table)
473 free_pages((unsigned long)table, page_order);
474 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477static int uninorth_free_gatt_table(struct agp_bridge_data *bridge)
478{
479 int page_order;
480 char *table, *table_end;
481 void *temp;
482 struct page *page;
483
484 temp = bridge->current_size;
485 page_order = A_SIZE_32(temp)->page_order;
486
487 /* Do not worry about freeing memory, because if this is
488 * called, then all agp memory is deallocated and removed
489 * from the table.
490 */
491
Michel Dänzere8a5f902009-08-04 11:51:04 +0000492 vunmap(bridge->gatt_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 table = (char *) bridge->gatt_table_real;
494 table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
495
496 for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
497 ClearPageReserved(page);
498
499 free_pages((unsigned long) bridge->gatt_table_real, page_order);
500
501 return 0;
502}
503
504void null_cache_flush(void)
505{
506 mb();
507}
508
509/* Setup function */
510
Michel Dänzer52f072c2009-08-04 11:51:03 +0000511static const struct aper_size_info_32 uninorth_sizes[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 {256, 65536, 6, 64},
514 {128, 32768, 5, 32},
515 {64, 16384, 4, 16},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 {32, 8192, 3, 8},
517 {16, 4096, 2, 4},
518 {8, 2048, 1, 2},
519 {4, 1024, 0, 1}
520};
521
522/*
523 * Not sure that u3 supports that high aperture sizes but it
524 * would strange if it did not :)
525 */
Michel Dänzer52f072c2009-08-04 11:51:03 +0000526static const struct aper_size_info_32 u3_sizes[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 {512, 131072, 7, 128},
529 {256, 65536, 6, 64},
530 {128, 32768, 5, 32},
531 {64, 16384, 4, 16},
532 {32, 8192, 3, 8},
533 {16, 4096, 2, 4},
534 {8, 2048, 1, 2},
535 {4, 1024, 0, 1}
536};
537
Ryusuke Konishie047d1c2007-02-27 14:13:02 +0900538const struct agp_bridge_driver uninorth_agp_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 .owner = THIS_MODULE,
540 .aperture_sizes = (void *)uninorth_sizes,
541 .size_type = U32_APER_SIZE,
Michel Dänzer52f072c2009-08-04 11:51:03 +0000542 .num_aperture_sizes = ARRAY_SIZE(uninorth_sizes),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 .configure = uninorth_configure,
544 .fetch_size = uninorth_fetch_size,
545 .cleanup = uninorth_cleanup,
546 .tlb_flush = uninorth_tlbflush,
547 .mask_memory = agp_generic_mask_memory,
548 .masks = NULL,
549 .cache_flush = null_cache_flush,
550 .agp_enable = uninorth_agp_enable,
551 .create_gatt_table = uninorth_create_gatt_table,
552 .free_gatt_table = uninorth_free_gatt_table,
553 .insert_memory = uninorth_insert_memory,
554 .remove_memory = agp_generic_remove_memory,
555 .alloc_by_type = agp_generic_alloc_by_type,
556 .free_by_type = agp_generic_free_by_type,
557 .agp_alloc_page = agp_generic_alloc_page,
Rene Herman5f310b62008-08-21 19:15:46 +0200558 .agp_alloc_pages = agp_generic_alloc_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 .agp_destroy_page = agp_generic_destroy_page,
Rene Herman5f310b62008-08-21 19:15:46 +0200560 .agp_destroy_pages = agp_generic_destroy_pages,
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100561 .agp_type_to_mask_type = agp_generic_type_to_mask_type,
Joe Perchesc7258012008-03-26 14:10:02 -0700562 .cant_use_aperture = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563};
564
Ryusuke Konishie047d1c2007-02-27 14:13:02 +0900565const struct agp_bridge_driver u3_agp_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 .owner = THIS_MODULE,
567 .aperture_sizes = (void *)u3_sizes,
568 .size_type = U32_APER_SIZE,
Michel Dänzer52f072c2009-08-04 11:51:03 +0000569 .num_aperture_sizes = ARRAY_SIZE(u3_sizes),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 .configure = uninorth_configure,
571 .fetch_size = uninorth_fetch_size,
572 .cleanup = uninorth_cleanup,
573 .tlb_flush = uninorth_tlbflush,
574 .mask_memory = agp_generic_mask_memory,
575 .masks = NULL,
576 .cache_flush = null_cache_flush,
577 .agp_enable = uninorth_agp_enable,
578 .create_gatt_table = uninorth_create_gatt_table,
579 .free_gatt_table = uninorth_free_gatt_table,
580 .insert_memory = u3_insert_memory,
581 .remove_memory = u3_remove_memory,
582 .alloc_by_type = agp_generic_alloc_by_type,
583 .free_by_type = agp_generic_free_by_type,
584 .agp_alloc_page = agp_generic_alloc_page,
Rene Herman5f310b62008-08-21 19:15:46 +0200585 .agp_alloc_pages = agp_generic_alloc_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 .agp_destroy_page = agp_generic_destroy_page,
Stephen Rothwellc09ff7e2008-08-25 20:22:21 +1000587 .agp_destroy_pages = agp_generic_destroy_pages,
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100588 .agp_type_to_mask_type = agp_generic_type_to_mask_type,
Joe Perchesc7258012008-03-26 14:10:02 -0700589 .cant_use_aperture = true,
590 .needs_scratch_page = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591};
592
593static struct agp_device_ids uninorth_agp_device_ids[] __devinitdata = {
594 {
595 .device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP,
596 .chipset_name = "UniNorth",
597 },
598 {
599 .device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP_P,
600 .chipset_name = "UniNorth/Pangea",
601 },
602 {
603 .device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP15,
604 .chipset_name = "UniNorth 1.5",
605 },
606 {
607 .device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP2,
608 .chipset_name = "UniNorth 2",
609 },
610 {
611 .device_id = PCI_DEVICE_ID_APPLE_U3_AGP,
612 .chipset_name = "U3",
613 },
614 {
615 .device_id = PCI_DEVICE_ID_APPLE_U3L_AGP,
616 .chipset_name = "U3L",
617 },
618 {
619 .device_id = PCI_DEVICE_ID_APPLE_U3H_AGP,
620 .chipset_name = "U3H",
621 },
Olof Johansson7fce2602005-11-13 16:06:48 -0800622 {
623 .device_id = PCI_DEVICE_ID_APPLE_IPID2_AGP,
624 .chipset_name = "UniNorth/Intrepid2",
625 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626};
627
628static int __devinit agp_uninorth_probe(struct pci_dev *pdev,
629 const struct pci_device_id *ent)
630{
631 struct agp_device_ids *devs = uninorth_agp_device_ids;
632 struct agp_bridge_data *bridge;
633 struct device_node *uninorth_node;
634 u8 cap_ptr;
635 int j;
636
637 cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
638 if (cap_ptr == 0)
639 return -ENODEV;
640
641 /* probe for known chipsets */
642 for (j = 0; devs[j].chipset_name != NULL; ++j) {
643 if (pdev->device == devs[j].device_id) {
Bjorn Helgaase3cf6952008-07-30 12:26:51 -0700644 dev_info(&pdev->dev, "Apple %s chipset\n",
645 devs[j].chipset_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 goto found;
647 }
648 }
649
Bjorn Helgaase3cf6952008-07-30 12:26:51 -0700650 dev_err(&pdev->dev, "unsupported Apple chipset [%04x/%04x]\n",
651 pdev->vendor, pdev->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return -ENODEV;
653
654 found:
655 /* Set revision to 0 if we could not read it. */
656 uninorth_rev = 0;
657 is_u3 = 0;
658 /* Locate core99 Uni-N */
659 uninorth_node = of_find_node_by_name(NULL, "uni-n");
660 /* Locate G5 u3 */
661 if (uninorth_node == NULL) {
662 is_u3 = 1;
663 uninorth_node = of_find_node_by_name(NULL, "u3");
664 }
665 if (uninorth_node) {
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000666 const int *revprop = of_get_property(uninorth_node,
Jeremy Kerrb04e3dd2006-07-12 15:40:40 +1000667 "device-rev", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (revprop != NULL)
669 uninorth_rev = *revprop & 0x3f;
670 of_node_put(uninorth_node);
671 }
672
Benjamin Herrenschmidt0c541b42005-04-16 15:24:19 -0700673#ifdef CONFIG_PM
674 /* Inform platform of our suspend/resume caps */
675 pmac_register_agp_pm(pdev, agp_uninorth_suspend, agp_uninorth_resume);
676#endif
677
678 /* Allocate & setup our driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 bridge = agp_alloc_bridge();
680 if (!bridge)
681 return -ENOMEM;
682
683 if (is_u3)
684 bridge->driver = &u3_agp_driver;
685 else
686 bridge->driver = &uninorth_agp_driver;
687
688 bridge->dev = pdev;
689 bridge->capndx = cap_ptr;
690 bridge->flags = AGP_ERRATA_FASTWRITES;
691
692 /* Fill in the mode register */
693 pci_read_config_dword(pdev, cap_ptr+PCI_AGP_STATUS, &bridge->mode);
694
695 pci_set_drvdata(pdev, bridge);
696 return agp_add_bridge(bridge);
697}
698
699static void __devexit agp_uninorth_remove(struct pci_dev *pdev)
700{
701 struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
702
Benjamin Herrenschmidt0c541b42005-04-16 15:24:19 -0700703#ifdef CONFIG_PM
704 /* Inform platform of our suspend/resume caps */
705 pmac_register_agp_pm(pdev, NULL, NULL);
706#endif
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 agp_remove_bridge(bridge);
709 agp_put_bridge(bridge);
710}
711
712static struct pci_device_id agp_uninorth_pci_table[] = {
713 {
714 .class = (PCI_CLASS_BRIDGE_HOST << 8),
715 .class_mask = ~0,
716 .vendor = PCI_VENDOR_ID_APPLE,
717 .device = PCI_ANY_ID,
718 .subvendor = PCI_ANY_ID,
719 .subdevice = PCI_ANY_ID,
720 },
721 { }
722};
723
724MODULE_DEVICE_TABLE(pci, agp_uninorth_pci_table);
725
726static struct pci_driver agp_uninorth_pci_driver = {
727 .name = "agpgart-uninorth",
728 .id_table = agp_uninorth_pci_table,
729 .probe = agp_uninorth_probe,
730 .remove = agp_uninorth_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731};
732
733static int __init agp_uninorth_init(void)
734{
735 if (agp_off)
736 return -EINVAL;
737 return pci_register_driver(&agp_uninorth_pci_driver);
738}
739
740static void __exit agp_uninorth_cleanup(void)
741{
742 pci_unregister_driver(&agp_uninorth_pci_driver);
743}
744
745module_init(agp_uninorth_init);
746module_exit(agp_uninorth_cleanup);
747
Michel Dänzer18088742006-10-04 14:56:44 +0200748module_param(aperture, charp, 0);
749MODULE_PARM_DESC(aperture,
750 "Aperture size, must be power of two between 4MB and an\n"
751 "\t\tupper limit specific to the UniNorth revision.\n"
Michel Dänzer52f072c2009-08-04 11:51:03 +0000752 "\t\tDefault: " DEFAULT_APERTURE_STRING "M");
Michel Dänzer18088742006-10-04 14:56:44 +0200753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754MODULE_AUTHOR("Ben Herrenschmidt & Paul Mackerras");
755MODULE_LICENSE("GPL");