blob: 932ede81e726fa05feb5542eafce9da93b61cb74 [file] [log] [blame]
Daniel Vetterf51b7662010-04-14 00:29:52 +02001/*
2 * Intel GTT (Graphics Translation Table) routines
3 *
4 * Caveat: This driver implements the linux agp interface, but this is far from
5 * a agp driver! GTT support ended up here for purely historical reasons: The
6 * old userspace intel graphics drivers needed an interface to map memory into
7 * the GTT. And the drm provides a default interface for graphic devices sitting
8 * on an agp port. So it made sense to fake the GTT support as an agp port to
9 * avoid having to create a new api.
10 *
11 * With gem this does not make much sense anymore, just needlessly complicates
12 * the code. But as long as the old graphics stack is still support, it's stuck
13 * here.
14 *
15 * /fairy-tale-mode off
16 */
17
Daniel Vettere2404e72010-09-08 17:29:51 +020018#include <linux/module.h>
19#include <linux/pci.h>
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/pagemap.h>
23#include <linux/agp_backend.h>
24#include <asm/smp.h>
25#include "agp.h"
26#include "intel-agp.h"
27#include <linux/intel-gtt.h>
Daniel Vetter0ade6382010-08-24 22:18:41 +020028#include <drm/intel-gtt.h>
Daniel Vettere2404e72010-09-08 17:29:51 +020029
Daniel Vetterf51b7662010-04-14 00:29:52 +020030/*
31 * If we have Intel graphics, we're not going to have anything other than
32 * an Intel IOMMU. So make the correct use of the PCI DMA API contingent
33 * on the Intel IOMMU support (CONFIG_DMAR).
34 * Only newer chipsets need to bother with this, of course.
35 */
36#ifdef CONFIG_DMAR
37#define USE_PCI_DMA_API 1
38#endif
39
Jesse Barnesd1d6ca72010-07-08 09:22:46 -070040/* Max amount of stolen space, anything above will be returned to Linux */
41int intel_max_stolen = 32 * 1024 * 1024;
42EXPORT_SYMBOL(intel_max_stolen);
43
Daniel Vetterf51b7662010-04-14 00:29:52 +020044static const struct aper_size_info_fixed intel_i810_sizes[] =
45{
46 {64, 16384, 4},
47 /* The 32M mode still requires a 64k gatt */
48 {32, 8192, 4}
49};
50
51#define AGP_DCACHE_MEMORY 1
52#define AGP_PHYS_MEMORY 2
53#define INTEL_AGP_CACHED_MEMORY 3
54
55static struct gatt_mask intel_i810_masks[] =
56{
57 {.mask = I810_PTE_VALID, .type = 0},
58 {.mask = (I810_PTE_VALID | I810_PTE_LOCAL), .type = AGP_DCACHE_MEMORY},
59 {.mask = I810_PTE_VALID, .type = 0},
60 {.mask = I810_PTE_VALID | I830_PTE_SYSTEM_CACHED,
61 .type = INTEL_AGP_CACHED_MEMORY}
62};
63
Zhenyu Wangf8f235e2010-08-27 11:08:57 +080064#define INTEL_AGP_UNCACHED_MEMORY 0
65#define INTEL_AGP_CACHED_MEMORY_LLC 1
66#define INTEL_AGP_CACHED_MEMORY_LLC_GFDT 2
67#define INTEL_AGP_CACHED_MEMORY_LLC_MLC 3
68#define INTEL_AGP_CACHED_MEMORY_LLC_MLC_GFDT 4
69
70static struct gatt_mask intel_gen6_masks[] =
71{
72 {.mask = I810_PTE_VALID | GEN6_PTE_UNCACHED,
73 .type = INTEL_AGP_UNCACHED_MEMORY },
74 {.mask = I810_PTE_VALID | GEN6_PTE_LLC,
75 .type = INTEL_AGP_CACHED_MEMORY_LLC },
76 {.mask = I810_PTE_VALID | GEN6_PTE_LLC | GEN6_PTE_GFDT,
77 .type = INTEL_AGP_CACHED_MEMORY_LLC_GFDT },
78 {.mask = I810_PTE_VALID | GEN6_PTE_LLC_MLC,
79 .type = INTEL_AGP_CACHED_MEMORY_LLC_MLC },
80 {.mask = I810_PTE_VALID | GEN6_PTE_LLC_MLC | GEN6_PTE_GFDT,
81 .type = INTEL_AGP_CACHED_MEMORY_LLC_MLC_GFDT },
82};
83
Daniel Vetterf51b7662010-04-14 00:29:52 +020084static struct _intel_private {
Daniel Vetter0ade6382010-08-24 22:18:41 +020085 struct intel_gtt base;
Daniel Vetterf51b7662010-04-14 00:29:52 +020086 struct pci_dev *pcidev; /* device one */
Daniel Vetterd7cca2f2010-08-24 23:06:19 +020087 struct pci_dev *bridge_dev;
Daniel Vetterf51b7662010-04-14 00:29:52 +020088 u8 __iomem *registers;
89 u32 __iomem *gtt; /* I915G */
90 int num_dcache_entries;
Daniel Vetterf51b7662010-04-14 00:29:52 +020091 union {
92 void __iomem *i9xx_flush_page;
93 void *i8xx_flush_page;
94 };
95 struct page *i8xx_page;
96 struct resource ifp_resource;
97 int resource_valid;
98} intel_private;
99
100#ifdef USE_PCI_DMA_API
101static int intel_agp_map_page(struct page *page, dma_addr_t *ret)
102{
103 *ret = pci_map_page(intel_private.pcidev, page, 0,
104 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
105 if (pci_dma_mapping_error(intel_private.pcidev, *ret))
106 return -EINVAL;
107 return 0;
108}
109
110static void intel_agp_unmap_page(struct page *page, dma_addr_t dma)
111{
112 pci_unmap_page(intel_private.pcidev, dma,
113 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
114}
115
116static void intel_agp_free_sglist(struct agp_memory *mem)
117{
118 struct sg_table st;
119
120 st.sgl = mem->sg_list;
121 st.orig_nents = st.nents = mem->page_count;
122
123 sg_free_table(&st);
124
125 mem->sg_list = NULL;
126 mem->num_sg = 0;
127}
128
129static int intel_agp_map_memory(struct agp_memory *mem)
130{
131 struct sg_table st;
132 struct scatterlist *sg;
133 int i;
134
135 DBG("try mapping %lu pages\n", (unsigned long)mem->page_count);
136
137 if (sg_alloc_table(&st, mem->page_count, GFP_KERNEL))
Chris Wilson831cd442010-07-24 18:29:37 +0100138 goto err;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200139
140 mem->sg_list = sg = st.sgl;
141
142 for (i = 0 ; i < mem->page_count; i++, sg = sg_next(sg))
143 sg_set_page(sg, mem->pages[i], PAGE_SIZE, 0);
144
145 mem->num_sg = pci_map_sg(intel_private.pcidev, mem->sg_list,
146 mem->page_count, PCI_DMA_BIDIRECTIONAL);
Chris Wilson831cd442010-07-24 18:29:37 +0100147 if (unlikely(!mem->num_sg))
148 goto err;
149
Daniel Vetterf51b7662010-04-14 00:29:52 +0200150 return 0;
Chris Wilson831cd442010-07-24 18:29:37 +0100151
152err:
153 sg_free_table(&st);
154 return -ENOMEM;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200155}
156
157static void intel_agp_unmap_memory(struct agp_memory *mem)
158{
159 DBG("try unmapping %lu pages\n", (unsigned long)mem->page_count);
160
161 pci_unmap_sg(intel_private.pcidev, mem->sg_list,
162 mem->page_count, PCI_DMA_BIDIRECTIONAL);
163 intel_agp_free_sglist(mem);
164}
165
166static void intel_agp_insert_sg_entries(struct agp_memory *mem,
167 off_t pg_start, int mask_type)
168{
169 struct scatterlist *sg;
170 int i, j;
171
172 j = pg_start;
173
174 WARN_ON(!mem->num_sg);
175
176 if (mem->num_sg == mem->page_count) {
177 for_each_sg(mem->sg_list, sg, mem->page_count, i) {
178 writel(agp_bridge->driver->mask_memory(agp_bridge,
179 sg_dma_address(sg), mask_type),
180 intel_private.gtt+j);
181 j++;
182 }
183 } else {
184 /* sg may merge pages, but we have to separate
185 * per-page addr for GTT */
186 unsigned int len, m;
187
188 for_each_sg(mem->sg_list, sg, mem->num_sg, i) {
189 len = sg_dma_len(sg) / PAGE_SIZE;
190 for (m = 0; m < len; m++) {
191 writel(agp_bridge->driver->mask_memory(agp_bridge,
192 sg_dma_address(sg) + m * PAGE_SIZE,
193 mask_type),
194 intel_private.gtt+j);
195 j++;
196 }
197 }
198 }
199 readl(intel_private.gtt+j-1);
200}
201
202#else
203
204static void intel_agp_insert_sg_entries(struct agp_memory *mem,
205 off_t pg_start, int mask_type)
206{
207 int i, j;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200208
209 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
210 writel(agp_bridge->driver->mask_memory(agp_bridge,
211 page_to_phys(mem->pages[i]), mask_type),
212 intel_private.gtt+j);
213 }
214
215 readl(intel_private.gtt+j-1);
216}
217
218#endif
219
220static int intel_i810_fetch_size(void)
221{
222 u32 smram_miscc;
223 struct aper_size_info_fixed *values;
224
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200225 pci_read_config_dword(intel_private.bridge_dev,
226 I810_SMRAM_MISCC, &smram_miscc);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200227 values = A_SIZE_FIX(agp_bridge->driver->aperture_sizes);
228
229 if ((smram_miscc & I810_GMS) == I810_GMS_DISABLE) {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200230 dev_warn(&intel_private.bridge_dev->dev, "i810 is disabled\n");
Daniel Vetterf51b7662010-04-14 00:29:52 +0200231 return 0;
232 }
233 if ((smram_miscc & I810_GFX_MEM_WIN_SIZE) == I810_GFX_MEM_WIN_32M) {
Daniel Vettere1583162010-04-14 00:29:58 +0200234 agp_bridge->current_size = (void *) (values + 1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200235 agp_bridge->aperture_size_idx = 1;
236 return values[1].size;
237 } else {
Daniel Vettere1583162010-04-14 00:29:58 +0200238 agp_bridge->current_size = (void *) (values);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200239 agp_bridge->aperture_size_idx = 0;
240 return values[0].size;
241 }
242
243 return 0;
244}
245
246static int intel_i810_configure(void)
247{
248 struct aper_size_info_fixed *current_size;
249 u32 temp;
250 int i;
251
252 current_size = A_SIZE_FIX(agp_bridge->current_size);
253
254 if (!intel_private.registers) {
255 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &temp);
256 temp &= 0xfff80000;
257
258 intel_private.registers = ioremap(temp, 128 * 4096);
259 if (!intel_private.registers) {
260 dev_err(&intel_private.pcidev->dev,
261 "can't remap memory\n");
262 return -ENOMEM;
263 }
264 }
265
266 if ((readl(intel_private.registers+I810_DRAM_CTL)
267 & I810_DRAM_ROW_0) == I810_DRAM_ROW_0_SDRAM) {
268 /* This will need to be dynamically assigned */
269 dev_info(&intel_private.pcidev->dev,
270 "detected 4MB dedicated video ram\n");
271 intel_private.num_dcache_entries = 1024;
272 }
273 pci_read_config_dword(intel_private.pcidev, I810_GMADDR, &temp);
274 agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
275 writel(agp_bridge->gatt_bus_addr | I810_PGETBL_ENABLED, intel_private.registers+I810_PGETBL_CTL);
276 readl(intel_private.registers+I810_PGETBL_CTL); /* PCI Posting. */
277
278 if (agp_bridge->driver->needs_scratch_page) {
279 for (i = 0; i < current_size->num_entries; i++) {
280 writel(agp_bridge->scratch_page, intel_private.registers+I810_PTE_BASE+(i*4));
281 }
282 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4)); /* PCI posting. */
283 }
284 global_cache_flush();
285 return 0;
286}
287
288static void intel_i810_cleanup(void)
289{
290 writel(0, intel_private.registers+I810_PGETBL_CTL);
291 readl(intel_private.registers); /* PCI Posting. */
292 iounmap(intel_private.registers);
293}
294
Daniel Vetterf51b7662010-04-14 00:29:52 +0200295static void intel_i810_agp_enable(struct agp_bridge_data *bridge, u32 mode)
296{
297 return;
298}
299
300/* Exists to support ARGB cursors */
301static struct page *i8xx_alloc_pages(void)
302{
303 struct page *page;
304
305 page = alloc_pages(GFP_KERNEL | GFP_DMA32, 2);
306 if (page == NULL)
307 return NULL;
308
309 if (set_pages_uc(page, 4) < 0) {
310 set_pages_wb(page, 4);
311 __free_pages(page, 2);
312 return NULL;
313 }
314 get_page(page);
315 atomic_inc(&agp_bridge->current_memory_agp);
316 return page;
317}
318
319static void i8xx_destroy_pages(struct page *page)
320{
321 if (page == NULL)
322 return;
323
324 set_pages_wb(page, 4);
325 put_page(page);
326 __free_pages(page, 2);
327 atomic_dec(&agp_bridge->current_memory_agp);
328}
329
330static int intel_i830_type_to_mask_type(struct agp_bridge_data *bridge,
331 int type)
332{
333 if (type < AGP_USER_TYPES)
334 return type;
335 else if (type == AGP_USER_CACHED_MEMORY)
336 return INTEL_AGP_CACHED_MEMORY;
337 else
338 return 0;
339}
340
Zhenyu Wangf8f235e2010-08-27 11:08:57 +0800341static int intel_gen6_type_to_mask_type(struct agp_bridge_data *bridge,
342 int type)
343{
344 unsigned int type_mask = type & ~AGP_USER_CACHED_MEMORY_GFDT;
345 unsigned int gfdt = type & AGP_USER_CACHED_MEMORY_GFDT;
346
347 if (type_mask == AGP_USER_UNCACHED_MEMORY)
348 return INTEL_AGP_UNCACHED_MEMORY;
349 else if (type_mask == AGP_USER_CACHED_MEMORY_LLC_MLC)
350 return gfdt ? INTEL_AGP_CACHED_MEMORY_LLC_MLC_GFDT :
351 INTEL_AGP_CACHED_MEMORY_LLC_MLC;
352 else /* set 'normal'/'cached' to LLC by default */
353 return gfdt ? INTEL_AGP_CACHED_MEMORY_LLC_GFDT :
354 INTEL_AGP_CACHED_MEMORY_LLC;
355}
356
357
Daniel Vetterf51b7662010-04-14 00:29:52 +0200358static int intel_i810_insert_entries(struct agp_memory *mem, off_t pg_start,
359 int type)
360{
361 int i, j, num_entries;
362 void *temp;
363 int ret = -EINVAL;
364 int mask_type;
365
366 if (mem->page_count == 0)
367 goto out;
368
369 temp = agp_bridge->current_size;
370 num_entries = A_SIZE_FIX(temp)->num_entries;
371
372 if ((pg_start + mem->page_count) > num_entries)
373 goto out_err;
374
375
376 for (j = pg_start; j < (pg_start + mem->page_count); j++) {
377 if (!PGE_EMPTY(agp_bridge, readl(agp_bridge->gatt_table+j))) {
378 ret = -EBUSY;
379 goto out_err;
380 }
381 }
382
383 if (type != mem->type)
384 goto out_err;
385
386 mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type);
387
388 switch (mask_type) {
389 case AGP_DCACHE_MEMORY:
390 if (!mem->is_flushed)
391 global_cache_flush();
392 for (i = pg_start; i < (pg_start + mem->page_count); i++) {
393 writel((i*4096)|I810_PTE_LOCAL|I810_PTE_VALID,
394 intel_private.registers+I810_PTE_BASE+(i*4));
395 }
396 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4));
397 break;
398 case AGP_PHYS_MEMORY:
399 case AGP_NORMAL_MEMORY:
400 if (!mem->is_flushed)
401 global_cache_flush();
402 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
403 writel(agp_bridge->driver->mask_memory(agp_bridge,
404 page_to_phys(mem->pages[i]), mask_type),
405 intel_private.registers+I810_PTE_BASE+(j*4));
406 }
407 readl(intel_private.registers+I810_PTE_BASE+((j-1)*4));
408 break;
409 default:
410 goto out_err;
411 }
412
Daniel Vetterf51b7662010-04-14 00:29:52 +0200413out:
414 ret = 0;
415out_err:
416 mem->is_flushed = true;
417 return ret;
418}
419
420static int intel_i810_remove_entries(struct agp_memory *mem, off_t pg_start,
421 int type)
422{
423 int i;
424
425 if (mem->page_count == 0)
426 return 0;
427
428 for (i = pg_start; i < (mem->page_count + pg_start); i++) {
429 writel(agp_bridge->scratch_page, intel_private.registers+I810_PTE_BASE+(i*4));
430 }
431 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4));
432
Daniel Vetterf51b7662010-04-14 00:29:52 +0200433 return 0;
434}
435
436/*
437 * The i810/i830 requires a physical address to program its mouse
438 * pointer into hardware.
439 * However the Xserver still writes to it through the agp aperture.
440 */
441static struct agp_memory *alloc_agpphysmem_i8xx(size_t pg_count, int type)
442{
443 struct agp_memory *new;
444 struct page *page;
445
446 switch (pg_count) {
447 case 1: page = agp_bridge->driver->agp_alloc_page(agp_bridge);
448 break;
449 case 4:
450 /* kludge to get 4 physical pages for ARGB cursor */
451 page = i8xx_alloc_pages();
452 break;
453 default:
454 return NULL;
455 }
456
457 if (page == NULL)
458 return NULL;
459
460 new = agp_create_memory(pg_count);
461 if (new == NULL)
462 return NULL;
463
464 new->pages[0] = page;
465 if (pg_count == 4) {
466 /* kludge to get 4 physical pages for ARGB cursor */
467 new->pages[1] = new->pages[0] + 1;
468 new->pages[2] = new->pages[1] + 1;
469 new->pages[3] = new->pages[2] + 1;
470 }
471 new->page_count = pg_count;
472 new->num_scratch_pages = pg_count;
473 new->type = AGP_PHYS_MEMORY;
474 new->physical = page_to_phys(new->pages[0]);
475 return new;
476}
477
478static struct agp_memory *intel_i810_alloc_by_type(size_t pg_count, int type)
479{
480 struct agp_memory *new;
481
482 if (type == AGP_DCACHE_MEMORY) {
483 if (pg_count != intel_private.num_dcache_entries)
484 return NULL;
485
486 new = agp_create_memory(1);
487 if (new == NULL)
488 return NULL;
489
490 new->type = AGP_DCACHE_MEMORY;
491 new->page_count = pg_count;
492 new->num_scratch_pages = 0;
493 agp_free_page_array(new);
494 return new;
495 }
496 if (type == AGP_PHYS_MEMORY)
497 return alloc_agpphysmem_i8xx(pg_count, type);
498 return NULL;
499}
500
501static void intel_i810_free_by_type(struct agp_memory *curr)
502{
503 agp_free_key(curr->key);
504 if (curr->type == AGP_PHYS_MEMORY) {
505 if (curr->page_count == 4)
506 i8xx_destroy_pages(curr->pages[0]);
507 else {
508 agp_bridge->driver->agp_destroy_page(curr->pages[0],
509 AGP_PAGE_DESTROY_UNMAP);
510 agp_bridge->driver->agp_destroy_page(curr->pages[0],
511 AGP_PAGE_DESTROY_FREE);
512 }
513 agp_free_page_array(curr);
514 }
515 kfree(curr);
516}
517
518static unsigned long intel_i810_mask_memory(struct agp_bridge_data *bridge,
519 dma_addr_t addr, int type)
520{
521 /* Type checking must be done elsewhere */
522 return addr | bridge->driver->masks[type].mask;
523}
524
525static struct aper_size_info_fixed intel_i830_sizes[] =
526{
527 {128, 32768, 5},
528 /* The 64M mode still requires a 128k gatt */
529 {64, 16384, 5},
530 {256, 65536, 6},
531 {512, 131072, 7},
532};
533
Daniel Vetterbfde0672010-08-24 23:07:59 +0200534static unsigned int intel_gtt_stolen_entries(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200535{
536 u16 gmch_ctrl;
Daniel Vetterbfde0672010-08-24 23:07:59 +0200537 unsigned int gtt_entries = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200538 u8 rdct;
539 int local = 0;
540 static const int ddt[4] = { 0, 16, 32, 64 };
541 int size; /* reserved space (in kb) at the top of stolen memory */
542
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200543 pci_read_config_word(intel_private.bridge_dev,
544 I830_GMCH_CTRL, &gmch_ctrl);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200545
546 if (IS_I965) {
547 u32 pgetbl_ctl;
548 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
549
550 /* The 965 has a field telling us the size of the GTT,
551 * which may be larger than what is necessary to map the
552 * aperture.
553 */
554 switch (pgetbl_ctl & I965_PGETBL_SIZE_MASK) {
555 case I965_PGETBL_SIZE_128KB:
556 size = 128;
557 break;
558 case I965_PGETBL_SIZE_256KB:
559 size = 256;
560 break;
561 case I965_PGETBL_SIZE_512KB:
562 size = 512;
563 break;
564 case I965_PGETBL_SIZE_1MB:
565 size = 1024;
566 break;
567 case I965_PGETBL_SIZE_2MB:
568 size = 2048;
569 break;
570 case I965_PGETBL_SIZE_1_5MB:
571 size = 1024 + 512;
572 break;
573 default:
574 dev_info(&intel_private.pcidev->dev,
575 "unknown page table size, assuming 512KB\n");
576 size = 512;
577 }
578 size += 4; /* add in BIOS popup space */
579 } else if (IS_G33 && !IS_PINEVIEW) {
580 /* G33's GTT size defined in gmch_ctrl */
581 switch (gmch_ctrl & G33_PGETBL_SIZE_MASK) {
582 case G33_PGETBL_SIZE_1M:
583 size = 1024;
584 break;
585 case G33_PGETBL_SIZE_2M:
586 size = 2048;
587 break;
588 default:
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200589 dev_info(&intel_private.bridge_dev->dev,
Daniel Vetterf51b7662010-04-14 00:29:52 +0200590 "unknown page table size 0x%x, assuming 512KB\n",
591 (gmch_ctrl & G33_PGETBL_SIZE_MASK));
592 size = 512;
593 }
594 size += 4;
595 } else if (IS_G4X || IS_PINEVIEW) {
596 /* On 4 series hardware, GTT stolen is separate from graphics
597 * stolen, ignore it in stolen gtt entries counting. However,
598 * 4KB of the stolen memory doesn't get mapped to the GTT.
599 */
600 size = 4;
601 } else {
602 /* On previous hardware, the GTT size was just what was
603 * required to map the aperture.
604 */
605 size = agp_bridge->driver->fetch_size() + 4;
606 }
607
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200608 if (intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82830_HB ||
609 intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82845G_HB) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200610 switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
611 case I830_GMCH_GMS_STOLEN_512:
612 gtt_entries = KB(512) - KB(size);
613 break;
614 case I830_GMCH_GMS_STOLEN_1024:
615 gtt_entries = MB(1) - KB(size);
616 break;
617 case I830_GMCH_GMS_STOLEN_8192:
618 gtt_entries = MB(8) - KB(size);
619 break;
620 case I830_GMCH_GMS_LOCAL:
621 rdct = readb(intel_private.registers+I830_RDRAM_CHANNEL_TYPE);
622 gtt_entries = (I830_RDRAM_ND(rdct) + 1) *
623 MB(ddt[I830_RDRAM_DDT(rdct)]);
624 local = 1;
625 break;
626 default:
627 gtt_entries = 0;
628 break;
629 }
Zhenyu Wang85540482010-09-07 13:45:32 +0800630 } else if (IS_SNB) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200631 /*
632 * SandyBridge has new memory control reg at 0x50.w
633 */
634 u16 snb_gmch_ctl;
635 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl);
636 switch (snb_gmch_ctl & SNB_GMCH_GMS_STOLEN_MASK) {
637 case SNB_GMCH_GMS_STOLEN_32M:
638 gtt_entries = MB(32) - KB(size);
639 break;
640 case SNB_GMCH_GMS_STOLEN_64M:
641 gtt_entries = MB(64) - KB(size);
642 break;
643 case SNB_GMCH_GMS_STOLEN_96M:
644 gtt_entries = MB(96) - KB(size);
645 break;
646 case SNB_GMCH_GMS_STOLEN_128M:
647 gtt_entries = MB(128) - KB(size);
648 break;
649 case SNB_GMCH_GMS_STOLEN_160M:
650 gtt_entries = MB(160) - KB(size);
651 break;
652 case SNB_GMCH_GMS_STOLEN_192M:
653 gtt_entries = MB(192) - KB(size);
654 break;
655 case SNB_GMCH_GMS_STOLEN_224M:
656 gtt_entries = MB(224) - KB(size);
657 break;
658 case SNB_GMCH_GMS_STOLEN_256M:
659 gtt_entries = MB(256) - KB(size);
660 break;
661 case SNB_GMCH_GMS_STOLEN_288M:
662 gtt_entries = MB(288) - KB(size);
663 break;
664 case SNB_GMCH_GMS_STOLEN_320M:
665 gtt_entries = MB(320) - KB(size);
666 break;
667 case SNB_GMCH_GMS_STOLEN_352M:
668 gtt_entries = MB(352) - KB(size);
669 break;
670 case SNB_GMCH_GMS_STOLEN_384M:
671 gtt_entries = MB(384) - KB(size);
672 break;
673 case SNB_GMCH_GMS_STOLEN_416M:
674 gtt_entries = MB(416) - KB(size);
675 break;
676 case SNB_GMCH_GMS_STOLEN_448M:
677 gtt_entries = MB(448) - KB(size);
678 break;
679 case SNB_GMCH_GMS_STOLEN_480M:
680 gtt_entries = MB(480) - KB(size);
681 break;
682 case SNB_GMCH_GMS_STOLEN_512M:
683 gtt_entries = MB(512) - KB(size);
684 break;
685 }
686 } else {
687 switch (gmch_ctrl & I855_GMCH_GMS_MASK) {
688 case I855_GMCH_GMS_STOLEN_1M:
689 gtt_entries = MB(1) - KB(size);
690 break;
691 case I855_GMCH_GMS_STOLEN_4M:
692 gtt_entries = MB(4) - KB(size);
693 break;
694 case I855_GMCH_GMS_STOLEN_8M:
695 gtt_entries = MB(8) - KB(size);
696 break;
697 case I855_GMCH_GMS_STOLEN_16M:
698 gtt_entries = MB(16) - KB(size);
699 break;
700 case I855_GMCH_GMS_STOLEN_32M:
701 gtt_entries = MB(32) - KB(size);
702 break;
703 case I915_GMCH_GMS_STOLEN_48M:
704 /* Check it's really I915G */
705 if (IS_I915 || IS_I965 || IS_G33 || IS_G4X)
706 gtt_entries = MB(48) - KB(size);
707 else
708 gtt_entries = 0;
709 break;
710 case I915_GMCH_GMS_STOLEN_64M:
711 /* Check it's really I915G */
712 if (IS_I915 || IS_I965 || IS_G33 || IS_G4X)
713 gtt_entries = MB(64) - KB(size);
714 else
715 gtt_entries = 0;
716 break;
717 case G33_GMCH_GMS_STOLEN_128M:
718 if (IS_G33 || IS_I965 || IS_G4X)
719 gtt_entries = MB(128) - KB(size);
720 else
721 gtt_entries = 0;
722 break;
723 case G33_GMCH_GMS_STOLEN_256M:
724 if (IS_G33 || IS_I965 || IS_G4X)
725 gtt_entries = MB(256) - KB(size);
726 else
727 gtt_entries = 0;
728 break;
729 case INTEL_GMCH_GMS_STOLEN_96M:
730 if (IS_I965 || IS_G4X)
731 gtt_entries = MB(96) - KB(size);
732 else
733 gtt_entries = 0;
734 break;
735 case INTEL_GMCH_GMS_STOLEN_160M:
736 if (IS_I965 || IS_G4X)
737 gtt_entries = MB(160) - KB(size);
738 else
739 gtt_entries = 0;
740 break;
741 case INTEL_GMCH_GMS_STOLEN_224M:
742 if (IS_I965 || IS_G4X)
743 gtt_entries = MB(224) - KB(size);
744 else
745 gtt_entries = 0;
746 break;
747 case INTEL_GMCH_GMS_STOLEN_352M:
748 if (IS_I965 || IS_G4X)
749 gtt_entries = MB(352) - KB(size);
750 else
751 gtt_entries = 0;
752 break;
753 default:
754 gtt_entries = 0;
755 break;
756 }
757 }
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200758
Jesse Barnesd1d6ca72010-07-08 09:22:46 -0700759 if (!local && gtt_entries > intel_max_stolen) {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200760 dev_info(&intel_private.bridge_dev->dev,
Jesse Barnesd1d6ca72010-07-08 09:22:46 -0700761 "detected %dK stolen memory, trimming to %dK\n",
762 gtt_entries / KB(1), intel_max_stolen / KB(1));
763 gtt_entries = intel_max_stolen / KB(4);
764 } else if (gtt_entries > 0) {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200765 dev_info(&intel_private.bridge_dev->dev, "detected %dK %s memory\n",
Daniel Vetterf51b7662010-04-14 00:29:52 +0200766 gtt_entries / KB(1), local ? "local" : "stolen");
767 gtt_entries /= KB(4);
768 } else {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200769 dev_info(&intel_private.bridge_dev->dev,
Daniel Vetterf51b7662010-04-14 00:29:52 +0200770 "no pre-allocated video memory detected\n");
771 gtt_entries = 0;
772 }
773
Daniel Vetterbfde0672010-08-24 23:07:59 +0200774 return gtt_entries;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200775}
776
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200777static unsigned int intel_gtt_mappable_entries(void)
778{
779 unsigned int aperture_size;
780 u16 gmch_ctrl;
781
782 aperture_size = 1024 * 1024;
783
784 pci_read_config_word(intel_private.bridge_dev,
785 I830_GMCH_CTRL, &gmch_ctrl);
786
787 switch (intel_private.pcidev->device) {
788 case PCI_DEVICE_ID_INTEL_82830_CGC:
789 case PCI_DEVICE_ID_INTEL_82845G_IG:
790 case PCI_DEVICE_ID_INTEL_82855GM_IG:
791 case PCI_DEVICE_ID_INTEL_82865_IG:
792 if ((gmch_ctrl & I830_GMCH_MEM_MASK) == I830_GMCH_MEM_64M)
793 aperture_size *= 64;
794 else
795 aperture_size *= 128;
796 break;
797 default:
798 /* 9xx supports large sizes, just look at the length */
799 aperture_size = pci_resource_len(intel_private.pcidev, 2);
800 break;
801 }
802
803 return aperture_size >> PAGE_SHIFT;
804}
805
806static int intel_gtt_init(void)
807{
808 /* we have to call this as early as possible after the MMIO base address is known */
809 intel_private.base.gtt_stolen_entries = intel_gtt_stolen_entries();
810 if (intel_private.base.gtt_stolen_entries == 0) {
811 iounmap(intel_private.registers);
812 return -ENOMEM;
813 }
814
815 return 0;
816}
817
Daniel Vetterf51b7662010-04-14 00:29:52 +0200818static void intel_i830_fini_flush(void)
819{
820 kunmap(intel_private.i8xx_page);
821 intel_private.i8xx_flush_page = NULL;
822 unmap_page_from_agp(intel_private.i8xx_page);
823
824 __free_page(intel_private.i8xx_page);
825 intel_private.i8xx_page = NULL;
826}
827
828static void intel_i830_setup_flush(void)
829{
830 /* return if we've already set the flush mechanism up */
831 if (intel_private.i8xx_page)
832 return;
833
834 intel_private.i8xx_page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA32);
835 if (!intel_private.i8xx_page)
836 return;
837
838 intel_private.i8xx_flush_page = kmap(intel_private.i8xx_page);
839 if (!intel_private.i8xx_flush_page)
840 intel_i830_fini_flush();
841}
842
843/* The chipset_flush interface needs to get data that has already been
844 * flushed out of the CPU all the way out to main memory, because the GPU
845 * doesn't snoop those buffers.
846 *
847 * The 8xx series doesn't have the same lovely interface for flushing the
848 * chipset write buffers that the later chips do. According to the 865
849 * specs, it's 64 octwords, or 1KB. So, to get those previous things in
850 * that buffer out, we just fill 1KB and clflush it out, on the assumption
851 * that it'll push whatever was in there out. It appears to work.
852 */
853static void intel_i830_chipset_flush(struct agp_bridge_data *bridge)
854{
855 unsigned int *pg = intel_private.i8xx_flush_page;
856
857 memset(pg, 0, 1024);
858
859 if (cpu_has_clflush)
860 clflush_cache_range(pg, 1024);
861 else if (wbinvd_on_all_cpus() != 0)
862 printk(KERN_ERR "Timed out waiting for cache flush.\n");
863}
864
865/* The intel i830 automatically initializes the agp aperture during POST.
866 * Use the memory already set aside for in the GTT.
867 */
868static int intel_i830_create_gatt_table(struct agp_bridge_data *bridge)
869{
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200870 int page_order, ret;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200871 struct aper_size_info_fixed *size;
872 int num_entries;
873 u32 temp;
874
875 size = agp_bridge->current_size;
876 page_order = size->page_order;
877 num_entries = size->num_entries;
878 agp_bridge->gatt_table_real = NULL;
879
880 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &temp);
881 temp &= 0xfff80000;
882
883 intel_private.registers = ioremap(temp, 128 * 4096);
884 if (!intel_private.registers)
885 return -ENOMEM;
886
887 temp = readl(intel_private.registers+I810_PGETBL_CTL) & 0xfffff000;
888 global_cache_flush(); /* FIXME: ?? */
889
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200890 ret = intel_gtt_init();
891 if (ret != 0)
892 return ret;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200893
894 agp_bridge->gatt_table = NULL;
895
896 agp_bridge->gatt_bus_addr = temp;
897
898 return 0;
899}
900
901/* Return the gatt table to a sane state. Use the top of stolen
902 * memory for the GTT.
903 */
904static int intel_i830_free_gatt_table(struct agp_bridge_data *bridge)
905{
906 return 0;
907}
908
909static int intel_i830_fetch_size(void)
910{
911 u16 gmch_ctrl;
912 struct aper_size_info_fixed *values;
913
914 values = A_SIZE_FIX(agp_bridge->driver->aperture_sizes);
915
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200916 if (intel_private.bridge_dev->device != PCI_DEVICE_ID_INTEL_82830_HB &&
917 intel_private.bridge_dev->device != PCI_DEVICE_ID_INTEL_82845G_HB) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200918 /* 855GM/852GM/865G has 128MB aperture size */
Daniel Vettere1583162010-04-14 00:29:58 +0200919 agp_bridge->current_size = (void *) values;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200920 agp_bridge->aperture_size_idx = 0;
921 return values[0].size;
922 }
923
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200924 pci_read_config_word(intel_private.bridge_dev, I830_GMCH_CTRL, &gmch_ctrl);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200925
926 if ((gmch_ctrl & I830_GMCH_MEM_MASK) == I830_GMCH_MEM_128M) {
Daniel Vettere1583162010-04-14 00:29:58 +0200927 agp_bridge->current_size = (void *) values;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200928 agp_bridge->aperture_size_idx = 0;
929 return values[0].size;
930 } else {
Daniel Vettere1583162010-04-14 00:29:58 +0200931 agp_bridge->current_size = (void *) (values + 1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200932 agp_bridge->aperture_size_idx = 1;
933 return values[1].size;
934 }
935
936 return 0;
937}
938
939static int intel_i830_configure(void)
940{
941 struct aper_size_info_fixed *current_size;
942 u32 temp;
943 u16 gmch_ctrl;
944 int i;
945
946 current_size = A_SIZE_FIX(agp_bridge->current_size);
947
948 pci_read_config_dword(intel_private.pcidev, I810_GMADDR, &temp);
949 agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
950
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200951 pci_read_config_word(intel_private.bridge_dev, I830_GMCH_CTRL, &gmch_ctrl);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200952 gmch_ctrl |= I830_GMCH_ENABLED;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200953 pci_write_config_word(intel_private.bridge_dev, I830_GMCH_CTRL, gmch_ctrl);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200954
955 writel(agp_bridge->gatt_bus_addr|I810_PGETBL_ENABLED, intel_private.registers+I810_PGETBL_CTL);
956 readl(intel_private.registers+I810_PGETBL_CTL); /* PCI Posting. */
957
958 if (agp_bridge->driver->needs_scratch_page) {
Daniel Vetter0ade6382010-08-24 22:18:41 +0200959 for (i = intel_private.base.gtt_stolen_entries; i < current_size->num_entries; i++) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200960 writel(agp_bridge->scratch_page, intel_private.registers+I810_PTE_BASE+(i*4));
961 }
962 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4)); /* PCI Posting. */
963 }
964
965 global_cache_flush();
966
967 intel_i830_setup_flush();
968 return 0;
969}
970
971static void intel_i830_cleanup(void)
972{
973 iounmap(intel_private.registers);
974}
975
976static int intel_i830_insert_entries(struct agp_memory *mem, off_t pg_start,
977 int type)
978{
979 int i, j, num_entries;
980 void *temp;
981 int ret = -EINVAL;
982 int mask_type;
983
984 if (mem->page_count == 0)
985 goto out;
986
987 temp = agp_bridge->current_size;
988 num_entries = A_SIZE_FIX(temp)->num_entries;
989
Daniel Vetter0ade6382010-08-24 22:18:41 +0200990 if (pg_start < intel_private.base.gtt_stolen_entries) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200991 dev_printk(KERN_DEBUG, &intel_private.pcidev->dev,
Daniel Vetter0ade6382010-08-24 22:18:41 +0200992 "pg_start == 0x%.8lx, gtt_stolen_entries == 0x%.8x\n",
993 pg_start, intel_private.base.gtt_stolen_entries);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200994
995 dev_info(&intel_private.pcidev->dev,
996 "trying to insert into local/stolen memory\n");
997 goto out_err;
998 }
999
1000 if ((pg_start + mem->page_count) > num_entries)
1001 goto out_err;
1002
1003 /* The i830 can't check the GTT for entries since its read only,
1004 * depend on the caller to make the correct offset decisions.
1005 */
1006
1007 if (type != mem->type)
1008 goto out_err;
1009
1010 mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type);
1011
1012 if (mask_type != 0 && mask_type != AGP_PHYS_MEMORY &&
1013 mask_type != INTEL_AGP_CACHED_MEMORY)
1014 goto out_err;
1015
1016 if (!mem->is_flushed)
1017 global_cache_flush();
1018
1019 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
1020 writel(agp_bridge->driver->mask_memory(agp_bridge,
1021 page_to_phys(mem->pages[i]), mask_type),
1022 intel_private.registers+I810_PTE_BASE+(j*4));
1023 }
1024 readl(intel_private.registers+I810_PTE_BASE+((j-1)*4));
Daniel Vetterf51b7662010-04-14 00:29:52 +02001025
1026out:
1027 ret = 0;
1028out_err:
1029 mem->is_flushed = true;
1030 return ret;
1031}
1032
1033static int intel_i830_remove_entries(struct agp_memory *mem, off_t pg_start,
1034 int type)
1035{
1036 int i;
1037
1038 if (mem->page_count == 0)
1039 return 0;
1040
Daniel Vetter0ade6382010-08-24 22:18:41 +02001041 if (pg_start < intel_private.base.gtt_stolen_entries) {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001042 dev_info(&intel_private.pcidev->dev,
1043 "trying to disable local/stolen memory\n");
1044 return -EINVAL;
1045 }
1046
1047 for (i = pg_start; i < (mem->page_count + pg_start); i++) {
1048 writel(agp_bridge->scratch_page, intel_private.registers+I810_PTE_BASE+(i*4));
1049 }
1050 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4));
1051
Daniel Vetterf51b7662010-04-14 00:29:52 +02001052 return 0;
1053}
1054
1055static struct agp_memory *intel_i830_alloc_by_type(size_t pg_count, int type)
1056{
1057 if (type == AGP_PHYS_MEMORY)
1058 return alloc_agpphysmem_i8xx(pg_count, type);
1059 /* always return NULL for other allocation types for now */
1060 return NULL;
1061}
1062
1063static int intel_alloc_chipset_flush_resource(void)
1064{
1065 int ret;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001066 ret = pci_bus_alloc_resource(intel_private.bridge_dev->bus, &intel_private.ifp_resource, PAGE_SIZE,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001067 PAGE_SIZE, PCIBIOS_MIN_MEM, 0,
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001068 pcibios_align_resource, intel_private.bridge_dev);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001069
1070 return ret;
1071}
1072
1073static void intel_i915_setup_chipset_flush(void)
1074{
1075 int ret;
1076 u32 temp;
1077
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001078 pci_read_config_dword(intel_private.bridge_dev, I915_IFPADDR, &temp);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001079 if (!(temp & 0x1)) {
1080 intel_alloc_chipset_flush_resource();
1081 intel_private.resource_valid = 1;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001082 pci_write_config_dword(intel_private.bridge_dev, I915_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001083 } else {
1084 temp &= ~1;
1085
1086 intel_private.resource_valid = 1;
1087 intel_private.ifp_resource.start = temp;
1088 intel_private.ifp_resource.end = temp + PAGE_SIZE;
1089 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
1090 /* some BIOSes reserve this area in a pnp some don't */
1091 if (ret)
1092 intel_private.resource_valid = 0;
1093 }
1094}
1095
1096static void intel_i965_g33_setup_chipset_flush(void)
1097{
1098 u32 temp_hi, temp_lo;
1099 int ret;
1100
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001101 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4, &temp_hi);
1102 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR, &temp_lo);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001103
1104 if (!(temp_lo & 0x1)) {
1105
1106 intel_alloc_chipset_flush_resource();
1107
1108 intel_private.resource_valid = 1;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001109 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001110 upper_32_bits(intel_private.ifp_resource.start));
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001111 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001112 } else {
1113 u64 l64;
1114
1115 temp_lo &= ~0x1;
1116 l64 = ((u64)temp_hi << 32) | temp_lo;
1117
1118 intel_private.resource_valid = 1;
1119 intel_private.ifp_resource.start = l64;
1120 intel_private.ifp_resource.end = l64 + PAGE_SIZE;
1121 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
1122 /* some BIOSes reserve this area in a pnp some don't */
1123 if (ret)
1124 intel_private.resource_valid = 0;
1125 }
1126}
1127
1128static void intel_i9xx_setup_flush(void)
1129{
1130 /* return if already configured */
1131 if (intel_private.ifp_resource.start)
1132 return;
1133
1134 if (IS_SNB)
1135 return;
1136
1137 /* setup a resource for this object */
1138 intel_private.ifp_resource.name = "Intel Flush Page";
1139 intel_private.ifp_resource.flags = IORESOURCE_MEM;
1140
1141 /* Setup chipset flush for 915 */
1142 if (IS_I965 || IS_G33 || IS_G4X) {
1143 intel_i965_g33_setup_chipset_flush();
1144 } else {
1145 intel_i915_setup_chipset_flush();
1146 }
1147
Chris Wilsondf51e7a2010-09-04 14:57:27 +01001148 if (intel_private.ifp_resource.start)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001149 intel_private.i9xx_flush_page = ioremap_nocache(intel_private.ifp_resource.start, PAGE_SIZE);
Chris Wilsondf51e7a2010-09-04 14:57:27 +01001150 if (!intel_private.i9xx_flush_page)
1151 dev_err(&intel_private.pcidev->dev,
1152 "can't ioremap flush page - no chipset flushing\n");
Daniel Vetterf51b7662010-04-14 00:29:52 +02001153}
1154
Chris Wilsonf1befe72010-05-18 12:24:51 +01001155static int intel_i9xx_configure(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001156{
1157 struct aper_size_info_fixed *current_size;
1158 u32 temp;
1159 u16 gmch_ctrl;
1160 int i;
1161
1162 current_size = A_SIZE_FIX(agp_bridge->current_size);
1163
1164 pci_read_config_dword(intel_private.pcidev, I915_GMADDR, &temp);
1165
1166 agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
1167
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001168 pci_read_config_word(intel_private.bridge_dev, I830_GMCH_CTRL, &gmch_ctrl);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001169 gmch_ctrl |= I830_GMCH_ENABLED;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001170 pci_write_config_word(intel_private.bridge_dev, I830_GMCH_CTRL, gmch_ctrl);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001171
1172 writel(agp_bridge->gatt_bus_addr|I810_PGETBL_ENABLED, intel_private.registers+I810_PGETBL_CTL);
1173 readl(intel_private.registers+I810_PGETBL_CTL); /* PCI Posting. */
1174
1175 if (agp_bridge->driver->needs_scratch_page) {
Daniel Vetter0ade6382010-08-24 22:18:41 +02001176 for (i = intel_private.base.gtt_stolen_entries; i <
1177 intel_private.base.gtt_total_entries; i++) {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001178 writel(agp_bridge->scratch_page, intel_private.gtt+i);
1179 }
1180 readl(intel_private.gtt+i-1); /* PCI Posting. */
1181 }
1182
1183 global_cache_flush();
1184
1185 intel_i9xx_setup_flush();
1186
1187 return 0;
1188}
1189
1190static void intel_i915_cleanup(void)
1191{
1192 if (intel_private.i9xx_flush_page)
1193 iounmap(intel_private.i9xx_flush_page);
1194 if (intel_private.resource_valid)
1195 release_resource(&intel_private.ifp_resource);
1196 intel_private.ifp_resource.start = 0;
1197 intel_private.resource_valid = 0;
1198 iounmap(intel_private.gtt);
1199 iounmap(intel_private.registers);
1200}
1201
1202static void intel_i915_chipset_flush(struct agp_bridge_data *bridge)
1203{
1204 if (intel_private.i9xx_flush_page)
1205 writel(1, intel_private.i9xx_flush_page);
1206}
1207
1208static int intel_i915_insert_entries(struct agp_memory *mem, off_t pg_start,
1209 int type)
1210{
1211 int num_entries;
1212 void *temp;
1213 int ret = -EINVAL;
1214 int mask_type;
1215
1216 if (mem->page_count == 0)
1217 goto out;
1218
1219 temp = agp_bridge->current_size;
1220 num_entries = A_SIZE_FIX(temp)->num_entries;
1221
Daniel Vetter0ade6382010-08-24 22:18:41 +02001222 if (pg_start < intel_private.base.gtt_stolen_entries) {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001223 dev_printk(KERN_DEBUG, &intel_private.pcidev->dev,
Daniel Vetter0ade6382010-08-24 22:18:41 +02001224 "pg_start == 0x%.8lx, gtt_stolen_entries == 0x%.8x\n",
1225 pg_start, intel_private.base.gtt_stolen_entries);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001226
1227 dev_info(&intel_private.pcidev->dev,
1228 "trying to insert into local/stolen memory\n");
1229 goto out_err;
1230 }
1231
1232 if ((pg_start + mem->page_count) > num_entries)
1233 goto out_err;
1234
1235 /* The i915 can't check the GTT for entries since it's read only;
1236 * depend on the caller to make the correct offset decisions.
1237 */
1238
1239 if (type != mem->type)
1240 goto out_err;
1241
1242 mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type);
1243
Zhenyu Wangf8f235e2010-08-27 11:08:57 +08001244 if (!IS_SNB && mask_type != 0 && mask_type != AGP_PHYS_MEMORY &&
Daniel Vetterf51b7662010-04-14 00:29:52 +02001245 mask_type != INTEL_AGP_CACHED_MEMORY)
1246 goto out_err;
1247
1248 if (!mem->is_flushed)
1249 global_cache_flush();
1250
1251 intel_agp_insert_sg_entries(mem, pg_start, mask_type);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001252
1253 out:
1254 ret = 0;
1255 out_err:
1256 mem->is_flushed = true;
1257 return ret;
1258}
1259
1260static int intel_i915_remove_entries(struct agp_memory *mem, off_t pg_start,
1261 int type)
1262{
1263 int i;
1264
1265 if (mem->page_count == 0)
1266 return 0;
1267
Daniel Vetter0ade6382010-08-24 22:18:41 +02001268 if (pg_start < intel_private.base.gtt_stolen_entries) {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001269 dev_info(&intel_private.pcidev->dev,
1270 "trying to disable local/stolen memory\n");
1271 return -EINVAL;
1272 }
1273
1274 for (i = pg_start; i < (mem->page_count + pg_start); i++)
1275 writel(agp_bridge->scratch_page, intel_private.gtt+i);
1276
1277 readl(intel_private.gtt+i-1);
1278
Daniel Vetterf51b7662010-04-14 00:29:52 +02001279 return 0;
1280}
1281
1282/* Return the aperture size by just checking the resource length. The effect
1283 * described in the spec of the MSAC registers is just changing of the
1284 * resource size.
1285 */
1286static int intel_i9xx_fetch_size(void)
1287{
1288 int num_sizes = ARRAY_SIZE(intel_i830_sizes);
1289 int aper_size; /* size in megabytes */
1290 int i;
1291
1292 aper_size = pci_resource_len(intel_private.pcidev, 2) / MB(1);
1293
1294 for (i = 0; i < num_sizes; i++) {
1295 if (aper_size == intel_i830_sizes[i].size) {
1296 agp_bridge->current_size = intel_i830_sizes + i;
Daniel Vetterf51b7662010-04-14 00:29:52 +02001297 return aper_size;
1298 }
1299 }
1300
1301 return 0;
1302}
1303
Chris Wilsonf1befe72010-05-18 12:24:51 +01001304static int intel_i915_get_gtt_size(void)
1305{
1306 int size;
1307
1308 if (IS_G33) {
1309 u16 gmch_ctrl;
1310
1311 /* G33's GTT size defined in gmch_ctrl */
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001312 pci_read_config_word(intel_private.bridge_dev, I830_GMCH_CTRL, &gmch_ctrl);
Tim Gardnere7b96f22010-07-09 14:48:50 -06001313 switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
1314 case I830_GMCH_GMS_STOLEN_512:
1315 size = 512;
1316 break;
1317 case I830_GMCH_GMS_STOLEN_1024:
Chris Wilsonf1befe72010-05-18 12:24:51 +01001318 size = 1024;
1319 break;
Tim Gardnere7b96f22010-07-09 14:48:50 -06001320 case I830_GMCH_GMS_STOLEN_8192:
1321 size = 8*1024;
Chris Wilsonf1befe72010-05-18 12:24:51 +01001322 break;
1323 default:
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001324 dev_info(&intel_private.bridge_dev->dev,
Chris Wilsonf1befe72010-05-18 12:24:51 +01001325 "unknown page table size 0x%x, assuming 512KB\n",
Tim Gardnere7b96f22010-07-09 14:48:50 -06001326 (gmch_ctrl & I830_GMCH_GMS_MASK));
Chris Wilsonf1befe72010-05-18 12:24:51 +01001327 size = 512;
1328 }
1329 } else {
1330 /* On previous hardware, the GTT size was just what was
1331 * required to map the aperture.
1332 */
1333 size = agp_bridge->driver->fetch_size();
1334 }
1335
1336 return KB(size);
1337}
1338
Daniel Vetterf51b7662010-04-14 00:29:52 +02001339/* The intel i915 automatically initializes the agp aperture during POST.
1340 * Use the memory already set aside for in the GTT.
1341 */
1342static int intel_i915_create_gatt_table(struct agp_bridge_data *bridge)
1343{
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001344 int page_order, ret;
Daniel Vetterf51b7662010-04-14 00:29:52 +02001345 struct aper_size_info_fixed *size;
1346 int num_entries;
1347 u32 temp, temp2;
Chris Wilsonf1befe72010-05-18 12:24:51 +01001348 int gtt_map_size;
Daniel Vetterf51b7662010-04-14 00:29:52 +02001349
1350 size = agp_bridge->current_size;
1351 page_order = size->page_order;
1352 num_entries = size->num_entries;
1353 agp_bridge->gatt_table_real = NULL;
1354
1355 pci_read_config_dword(intel_private.pcidev, I915_MMADDR, &temp);
1356 pci_read_config_dword(intel_private.pcidev, I915_PTEADDR, &temp2);
1357
Chris Wilsonf1befe72010-05-18 12:24:51 +01001358 gtt_map_size = intel_i915_get_gtt_size();
1359
Daniel Vetterf51b7662010-04-14 00:29:52 +02001360 intel_private.gtt = ioremap(temp2, gtt_map_size);
1361 if (!intel_private.gtt)
1362 return -ENOMEM;
1363
Daniel Vetter0ade6382010-08-24 22:18:41 +02001364 intel_private.base.gtt_total_entries = gtt_map_size / 4;
Daniel Vetterf51b7662010-04-14 00:29:52 +02001365
1366 temp &= 0xfff80000;
1367
1368 intel_private.registers = ioremap(temp, 128 * 4096);
1369 if (!intel_private.registers) {
1370 iounmap(intel_private.gtt);
1371 return -ENOMEM;
1372 }
1373
1374 temp = readl(intel_private.registers+I810_PGETBL_CTL) & 0xfffff000;
1375 global_cache_flush(); /* FIXME: ? */
1376
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001377 ret = intel_gtt_init();
1378 if (ret != 0) {
Ondrej Zary8699be32010-06-16 10:13:52 +02001379 iounmap(intel_private.gtt);
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001380 return ret;
Ondrej Zary8699be32010-06-16 10:13:52 +02001381 }
Daniel Vetterf51b7662010-04-14 00:29:52 +02001382
1383 agp_bridge->gatt_table = NULL;
1384
1385 agp_bridge->gatt_bus_addr = temp;
1386
1387 return 0;
1388}
1389
1390/*
1391 * The i965 supports 36-bit physical addresses, but to keep
1392 * the format of the GTT the same, the bits that don't fit
1393 * in a 32-bit word are shifted down to bits 4..7.
1394 *
1395 * Gcc is smart enough to notice that "(addr >> 28) & 0xf0"
1396 * is always zero on 32-bit architectures, so no need to make
1397 * this conditional.
1398 */
1399static unsigned long intel_i965_mask_memory(struct agp_bridge_data *bridge,
1400 dma_addr_t addr, int type)
1401{
1402 /* Shift high bits down */
1403 addr |= (addr >> 28) & 0xf0;
1404
1405 /* Type checking must be done elsewhere */
1406 return addr | bridge->driver->masks[type].mask;
1407}
1408
Zhenyu Wang3869d4a2010-07-09 10:40:58 -07001409static unsigned long intel_gen6_mask_memory(struct agp_bridge_data *bridge,
1410 dma_addr_t addr, int type)
1411{
Zhenyu Wang8dfc2b12010-08-23 14:37:52 +08001412 /* gen6 has bit11-4 for physical addr bit39-32 */
1413 addr |= (addr >> 28) & 0xff0;
Zhenyu Wang3869d4a2010-07-09 10:40:58 -07001414
1415 /* Type checking must be done elsewhere */
1416 return addr | bridge->driver->masks[type].mask;
1417}
1418
Daniel Vetterf51b7662010-04-14 00:29:52 +02001419static void intel_i965_get_gtt_range(int *gtt_offset, int *gtt_size)
1420{
1421 u16 snb_gmch_ctl;
1422
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001423 switch (intel_private.bridge_dev->device) {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001424 case PCI_DEVICE_ID_INTEL_GM45_HB:
1425 case PCI_DEVICE_ID_INTEL_EAGLELAKE_HB:
1426 case PCI_DEVICE_ID_INTEL_Q45_HB:
1427 case PCI_DEVICE_ID_INTEL_G45_HB:
1428 case PCI_DEVICE_ID_INTEL_G41_HB:
1429 case PCI_DEVICE_ID_INTEL_B43_HB:
1430 case PCI_DEVICE_ID_INTEL_IRONLAKE_D_HB:
1431 case PCI_DEVICE_ID_INTEL_IRONLAKE_M_HB:
1432 case PCI_DEVICE_ID_INTEL_IRONLAKE_MA_HB:
1433 case PCI_DEVICE_ID_INTEL_IRONLAKE_MC2_HB:
1434 *gtt_offset = *gtt_size = MB(2);
1435 break;
1436 case PCI_DEVICE_ID_INTEL_SANDYBRIDGE_HB:
1437 case PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_HB:
Zhenyu Wang85540482010-09-07 13:45:32 +08001438 case PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_HB:
Daniel Vetterf51b7662010-04-14 00:29:52 +02001439 *gtt_offset = MB(2);
1440
1441 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1442 switch (snb_gmch_ctl & SNB_GTT_SIZE_MASK) {
1443 default:
1444 case SNB_GTT_SIZE_0M:
1445 printk(KERN_ERR "Bad GTT size mask: 0x%04x.\n", snb_gmch_ctl);
1446 *gtt_size = MB(0);
1447 break;
1448 case SNB_GTT_SIZE_1M:
1449 *gtt_size = MB(1);
1450 break;
1451 case SNB_GTT_SIZE_2M:
1452 *gtt_size = MB(2);
1453 break;
1454 }
1455 break;
1456 default:
1457 *gtt_offset = *gtt_size = KB(512);
1458 }
1459}
1460
1461/* The intel i965 automatically initializes the agp aperture during POST.
1462 * Use the memory already set aside for in the GTT.
1463 */
1464static int intel_i965_create_gatt_table(struct agp_bridge_data *bridge)
1465{
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001466 int page_order, ret;
Daniel Vetterf51b7662010-04-14 00:29:52 +02001467 struct aper_size_info_fixed *size;
1468 int num_entries;
1469 u32 temp;
1470 int gtt_offset, gtt_size;
1471
1472 size = agp_bridge->current_size;
1473 page_order = size->page_order;
1474 num_entries = size->num_entries;
1475 agp_bridge->gatt_table_real = NULL;
1476
1477 pci_read_config_dword(intel_private.pcidev, I915_MMADDR, &temp);
1478
1479 temp &= 0xfff00000;
1480
1481 intel_i965_get_gtt_range(&gtt_offset, &gtt_size);
1482
1483 intel_private.gtt = ioremap((temp + gtt_offset) , gtt_size);
1484
1485 if (!intel_private.gtt)
1486 return -ENOMEM;
1487
Daniel Vetter0ade6382010-08-24 22:18:41 +02001488 intel_private.base.gtt_total_entries = gtt_size / 4;
Daniel Vetterf51b7662010-04-14 00:29:52 +02001489
1490 intel_private.registers = ioremap(temp, 128 * 4096);
1491 if (!intel_private.registers) {
1492 iounmap(intel_private.gtt);
1493 return -ENOMEM;
1494 }
1495
1496 temp = readl(intel_private.registers+I810_PGETBL_CTL) & 0xfffff000;
1497 global_cache_flush(); /* FIXME: ? */
1498
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001499 ret = intel_gtt_init();
1500 if (ret != 0) {
Ondrej Zary8699be32010-06-16 10:13:52 +02001501 iounmap(intel_private.gtt);
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001502 return ret;
Ondrej Zary8699be32010-06-16 10:13:52 +02001503 }
Daniel Vetterf51b7662010-04-14 00:29:52 +02001504
1505 agp_bridge->gatt_table = NULL;
1506
1507 agp_bridge->gatt_bus_addr = temp;
1508
1509 return 0;
1510}
1511
1512static const struct agp_bridge_driver intel_810_driver = {
1513 .owner = THIS_MODULE,
1514 .aperture_sizes = intel_i810_sizes,
1515 .size_type = FIXED_APER_SIZE,
1516 .num_aperture_sizes = 2,
1517 .needs_scratch_page = true,
1518 .configure = intel_i810_configure,
1519 .fetch_size = intel_i810_fetch_size,
1520 .cleanup = intel_i810_cleanup,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001521 .mask_memory = intel_i810_mask_memory,
1522 .masks = intel_i810_masks,
1523 .agp_enable = intel_i810_agp_enable,
1524 .cache_flush = global_cache_flush,
1525 .create_gatt_table = agp_generic_create_gatt_table,
1526 .free_gatt_table = agp_generic_free_gatt_table,
1527 .insert_memory = intel_i810_insert_entries,
1528 .remove_memory = intel_i810_remove_entries,
1529 .alloc_by_type = intel_i810_alloc_by_type,
1530 .free_by_type = intel_i810_free_by_type,
1531 .agp_alloc_page = agp_generic_alloc_page,
1532 .agp_alloc_pages = agp_generic_alloc_pages,
1533 .agp_destroy_page = agp_generic_destroy_page,
1534 .agp_destroy_pages = agp_generic_destroy_pages,
1535 .agp_type_to_mask_type = agp_generic_type_to_mask_type,
1536};
1537
1538static const struct agp_bridge_driver intel_830_driver = {
1539 .owner = THIS_MODULE,
1540 .aperture_sizes = intel_i830_sizes,
1541 .size_type = FIXED_APER_SIZE,
1542 .num_aperture_sizes = 4,
1543 .needs_scratch_page = true,
1544 .configure = intel_i830_configure,
1545 .fetch_size = intel_i830_fetch_size,
1546 .cleanup = intel_i830_cleanup,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001547 .mask_memory = intel_i810_mask_memory,
1548 .masks = intel_i810_masks,
1549 .agp_enable = intel_i810_agp_enable,
1550 .cache_flush = global_cache_flush,
1551 .create_gatt_table = intel_i830_create_gatt_table,
1552 .free_gatt_table = intel_i830_free_gatt_table,
1553 .insert_memory = intel_i830_insert_entries,
1554 .remove_memory = intel_i830_remove_entries,
1555 .alloc_by_type = intel_i830_alloc_by_type,
1556 .free_by_type = intel_i810_free_by_type,
1557 .agp_alloc_page = agp_generic_alloc_page,
1558 .agp_alloc_pages = agp_generic_alloc_pages,
1559 .agp_destroy_page = agp_generic_destroy_page,
1560 .agp_destroy_pages = agp_generic_destroy_pages,
1561 .agp_type_to_mask_type = intel_i830_type_to_mask_type,
1562 .chipset_flush = intel_i830_chipset_flush,
1563};
1564
1565static const struct agp_bridge_driver intel_915_driver = {
1566 .owner = THIS_MODULE,
1567 .aperture_sizes = intel_i830_sizes,
1568 .size_type = FIXED_APER_SIZE,
1569 .num_aperture_sizes = 4,
1570 .needs_scratch_page = true,
Chris Wilsonf1befe72010-05-18 12:24:51 +01001571 .configure = intel_i9xx_configure,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001572 .fetch_size = intel_i9xx_fetch_size,
1573 .cleanup = intel_i915_cleanup,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001574 .mask_memory = intel_i810_mask_memory,
1575 .masks = intel_i810_masks,
1576 .agp_enable = intel_i810_agp_enable,
1577 .cache_flush = global_cache_flush,
1578 .create_gatt_table = intel_i915_create_gatt_table,
1579 .free_gatt_table = intel_i830_free_gatt_table,
1580 .insert_memory = intel_i915_insert_entries,
1581 .remove_memory = intel_i915_remove_entries,
1582 .alloc_by_type = intel_i830_alloc_by_type,
1583 .free_by_type = intel_i810_free_by_type,
1584 .agp_alloc_page = agp_generic_alloc_page,
1585 .agp_alloc_pages = agp_generic_alloc_pages,
1586 .agp_destroy_page = agp_generic_destroy_page,
1587 .agp_destroy_pages = agp_generic_destroy_pages,
1588 .agp_type_to_mask_type = intel_i830_type_to_mask_type,
1589 .chipset_flush = intel_i915_chipset_flush,
1590#ifdef USE_PCI_DMA_API
1591 .agp_map_page = intel_agp_map_page,
1592 .agp_unmap_page = intel_agp_unmap_page,
1593 .agp_map_memory = intel_agp_map_memory,
1594 .agp_unmap_memory = intel_agp_unmap_memory,
1595#endif
1596};
1597
1598static const struct agp_bridge_driver intel_i965_driver = {
1599 .owner = THIS_MODULE,
1600 .aperture_sizes = intel_i830_sizes,
1601 .size_type = FIXED_APER_SIZE,
1602 .num_aperture_sizes = 4,
1603 .needs_scratch_page = true,
Chris Wilsonf1befe72010-05-18 12:24:51 +01001604 .configure = intel_i9xx_configure,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001605 .fetch_size = intel_i9xx_fetch_size,
1606 .cleanup = intel_i915_cleanup,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001607 .mask_memory = intel_i965_mask_memory,
1608 .masks = intel_i810_masks,
1609 .agp_enable = intel_i810_agp_enable,
1610 .cache_flush = global_cache_flush,
1611 .create_gatt_table = intel_i965_create_gatt_table,
1612 .free_gatt_table = intel_i830_free_gatt_table,
1613 .insert_memory = intel_i915_insert_entries,
1614 .remove_memory = intel_i915_remove_entries,
1615 .alloc_by_type = intel_i830_alloc_by_type,
1616 .free_by_type = intel_i810_free_by_type,
1617 .agp_alloc_page = agp_generic_alloc_page,
1618 .agp_alloc_pages = agp_generic_alloc_pages,
1619 .agp_destroy_page = agp_generic_destroy_page,
1620 .agp_destroy_pages = agp_generic_destroy_pages,
1621 .agp_type_to_mask_type = intel_i830_type_to_mask_type,
1622 .chipset_flush = intel_i915_chipset_flush,
1623#ifdef USE_PCI_DMA_API
1624 .agp_map_page = intel_agp_map_page,
1625 .agp_unmap_page = intel_agp_unmap_page,
1626 .agp_map_memory = intel_agp_map_memory,
1627 .agp_unmap_memory = intel_agp_unmap_memory,
1628#endif
1629};
1630
Zhenyu Wang3869d4a2010-07-09 10:40:58 -07001631static const struct agp_bridge_driver intel_gen6_driver = {
1632 .owner = THIS_MODULE,
1633 .aperture_sizes = intel_i830_sizes,
1634 .size_type = FIXED_APER_SIZE,
1635 .num_aperture_sizes = 4,
1636 .needs_scratch_page = true,
1637 .configure = intel_i9xx_configure,
1638 .fetch_size = intel_i9xx_fetch_size,
1639 .cleanup = intel_i915_cleanup,
1640 .mask_memory = intel_gen6_mask_memory,
Zhenyu Wangf8f235e2010-08-27 11:08:57 +08001641 .masks = intel_gen6_masks,
Zhenyu Wang3869d4a2010-07-09 10:40:58 -07001642 .agp_enable = intel_i810_agp_enable,
1643 .cache_flush = global_cache_flush,
1644 .create_gatt_table = intel_i965_create_gatt_table,
1645 .free_gatt_table = intel_i830_free_gatt_table,
1646 .insert_memory = intel_i915_insert_entries,
1647 .remove_memory = intel_i915_remove_entries,
1648 .alloc_by_type = intel_i830_alloc_by_type,
1649 .free_by_type = intel_i810_free_by_type,
1650 .agp_alloc_page = agp_generic_alloc_page,
1651 .agp_alloc_pages = agp_generic_alloc_pages,
1652 .agp_destroy_page = agp_generic_destroy_page,
1653 .agp_destroy_pages = agp_generic_destroy_pages,
Zhenyu Wangf8f235e2010-08-27 11:08:57 +08001654 .agp_type_to_mask_type = intel_gen6_type_to_mask_type,
Zhenyu Wang3869d4a2010-07-09 10:40:58 -07001655 .chipset_flush = intel_i915_chipset_flush,
1656#ifdef USE_PCI_DMA_API
1657 .agp_map_page = intel_agp_map_page,
1658 .agp_unmap_page = intel_agp_unmap_page,
1659 .agp_map_memory = intel_agp_map_memory,
1660 .agp_unmap_memory = intel_agp_unmap_memory,
1661#endif
1662};
1663
Daniel Vetterf51b7662010-04-14 00:29:52 +02001664static const struct agp_bridge_driver intel_g33_driver = {
1665 .owner = THIS_MODULE,
1666 .aperture_sizes = intel_i830_sizes,
1667 .size_type = FIXED_APER_SIZE,
1668 .num_aperture_sizes = 4,
1669 .needs_scratch_page = true,
Chris Wilsonf1befe72010-05-18 12:24:51 +01001670 .configure = intel_i9xx_configure,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001671 .fetch_size = intel_i9xx_fetch_size,
1672 .cleanup = intel_i915_cleanup,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001673 .mask_memory = intel_i965_mask_memory,
1674 .masks = intel_i810_masks,
1675 .agp_enable = intel_i810_agp_enable,
1676 .cache_flush = global_cache_flush,
1677 .create_gatt_table = intel_i915_create_gatt_table,
1678 .free_gatt_table = intel_i830_free_gatt_table,
1679 .insert_memory = intel_i915_insert_entries,
1680 .remove_memory = intel_i915_remove_entries,
1681 .alloc_by_type = intel_i830_alloc_by_type,
1682 .free_by_type = intel_i810_free_by_type,
1683 .agp_alloc_page = agp_generic_alloc_page,
1684 .agp_alloc_pages = agp_generic_alloc_pages,
1685 .agp_destroy_page = agp_generic_destroy_page,
1686 .agp_destroy_pages = agp_generic_destroy_pages,
1687 .agp_type_to_mask_type = intel_i830_type_to_mask_type,
1688 .chipset_flush = intel_i915_chipset_flush,
1689#ifdef USE_PCI_DMA_API
1690 .agp_map_page = intel_agp_map_page,
1691 .agp_unmap_page = intel_agp_unmap_page,
1692 .agp_map_memory = intel_agp_map_memory,
1693 .agp_unmap_memory = intel_agp_unmap_memory,
1694#endif
1695};
Daniel Vetter02c026c2010-08-24 19:39:48 +02001696
1697/* Table to describe Intel GMCH and AGP/PCIE GART drivers. At least one of
1698 * driver and gmch_driver must be non-null, and find_gmch will determine
1699 * which one should be used if a gmch_chip_id is present.
1700 */
1701static const struct intel_gtt_driver_description {
1702 unsigned int gmch_chip_id;
1703 char *name;
1704 const struct agp_bridge_driver *gmch_driver;
1705} intel_gtt_chipsets[] = {
1706 { PCI_DEVICE_ID_INTEL_82810_IG1, "i810", &intel_810_driver },
1707 { PCI_DEVICE_ID_INTEL_82810_IG3, "i810", &intel_810_driver },
1708 { PCI_DEVICE_ID_INTEL_82810E_IG, "i810", &intel_810_driver },
1709 { PCI_DEVICE_ID_INTEL_82815_CGC, "i815", &intel_810_driver },
1710 { PCI_DEVICE_ID_INTEL_82830_CGC, "830M", &intel_830_driver },
1711 { PCI_DEVICE_ID_INTEL_82845G_IG, "830M", &intel_830_driver },
1712 { PCI_DEVICE_ID_INTEL_82854_IG, "854", &intel_830_driver },
1713 { PCI_DEVICE_ID_INTEL_82855GM_IG, "855GM", &intel_830_driver },
1714 { PCI_DEVICE_ID_INTEL_82865_IG, "865", &intel_830_driver },
1715 { PCI_DEVICE_ID_INTEL_E7221_IG, "E7221 (i915)", &intel_915_driver },
1716 { PCI_DEVICE_ID_INTEL_82915G_IG, "915G", &intel_915_driver },
1717 { PCI_DEVICE_ID_INTEL_82915GM_IG, "915GM", &intel_915_driver },
1718 { PCI_DEVICE_ID_INTEL_82945G_IG, "945G", &intel_915_driver },
1719 { PCI_DEVICE_ID_INTEL_82945GM_IG, "945GM", &intel_915_driver },
1720 { PCI_DEVICE_ID_INTEL_82945GME_IG, "945GME", &intel_915_driver },
1721 { PCI_DEVICE_ID_INTEL_82946GZ_IG, "946GZ", &intel_i965_driver },
1722 { PCI_DEVICE_ID_INTEL_82G35_IG, "G35", &intel_i965_driver },
1723 { PCI_DEVICE_ID_INTEL_82965Q_IG, "965Q", &intel_i965_driver },
1724 { PCI_DEVICE_ID_INTEL_82965G_IG, "965G", &intel_i965_driver },
1725 { PCI_DEVICE_ID_INTEL_82965GM_IG, "965GM", &intel_i965_driver },
1726 { PCI_DEVICE_ID_INTEL_82965GME_IG, "965GME/GLE", &intel_i965_driver },
1727 { PCI_DEVICE_ID_INTEL_G33_IG, "G33", &intel_g33_driver },
1728 { PCI_DEVICE_ID_INTEL_Q35_IG, "Q35", &intel_g33_driver },
1729 { PCI_DEVICE_ID_INTEL_Q33_IG, "Q33", &intel_g33_driver },
1730 { PCI_DEVICE_ID_INTEL_PINEVIEW_M_IG, "GMA3150", &intel_g33_driver },
1731 { PCI_DEVICE_ID_INTEL_PINEVIEW_IG, "GMA3150", &intel_g33_driver },
1732 { PCI_DEVICE_ID_INTEL_GM45_IG, "GM45", &intel_i965_driver },
1733 { PCI_DEVICE_ID_INTEL_EAGLELAKE_IG, "Eaglelake", &intel_i965_driver },
1734 { PCI_DEVICE_ID_INTEL_Q45_IG, "Q45/Q43", &intel_i965_driver },
1735 { PCI_DEVICE_ID_INTEL_G45_IG, "G45/G43", &intel_i965_driver },
1736 { PCI_DEVICE_ID_INTEL_B43_IG, "B43", &intel_i965_driver },
1737 { PCI_DEVICE_ID_INTEL_G41_IG, "G41", &intel_i965_driver },
1738 { PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG,
1739 "HD Graphics", &intel_i965_driver },
1740 { PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG,
1741 "HD Graphics", &intel_i965_driver },
1742 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT1_IG,
1743 "Sandybridge", &intel_gen6_driver },
1744 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_IG,
1745 "Sandybridge", &intel_gen6_driver },
1746 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_PLUS_IG,
1747 "Sandybridge", &intel_gen6_driver },
1748 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT1_IG,
1749 "Sandybridge", &intel_gen6_driver },
1750 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_IG,
1751 "Sandybridge", &intel_gen6_driver },
1752 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_PLUS_IG,
1753 "Sandybridge", &intel_gen6_driver },
1754 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_IG,
1755 "Sandybridge", &intel_gen6_driver },
1756 { 0, NULL, NULL }
1757};
1758
1759static int find_gmch(u16 device)
1760{
1761 struct pci_dev *gmch_device;
1762
1763 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL, device, NULL);
1764 if (gmch_device && PCI_FUNC(gmch_device->devfn) != 0) {
1765 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL,
1766 device, gmch_device);
1767 }
1768
1769 if (!gmch_device)
1770 return 0;
1771
1772 intel_private.pcidev = gmch_device;
1773 return 1;
1774}
1775
Daniel Vettere2404e72010-09-08 17:29:51 +02001776int intel_gmch_probe(struct pci_dev *pdev,
Daniel Vetter02c026c2010-08-24 19:39:48 +02001777 struct agp_bridge_data *bridge)
1778{
1779 int i, mask;
1780 bridge->driver = NULL;
1781
1782 for (i = 0; intel_gtt_chipsets[i].name != NULL; i++) {
1783 if (find_gmch(intel_gtt_chipsets[i].gmch_chip_id)) {
1784 bridge->driver =
1785 intel_gtt_chipsets[i].gmch_driver;
1786 break;
1787 }
1788 }
1789
1790 if (!bridge->driver)
1791 return 0;
1792
1793 bridge->dev_private_data = &intel_private;
1794 bridge->dev = pdev;
1795
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001796 intel_private.bridge_dev = pci_dev_get(pdev);
1797
Daniel Vetter02c026c2010-08-24 19:39:48 +02001798 dev_info(&pdev->dev, "Intel %s Chipset\n", intel_gtt_chipsets[i].name);
1799
1800 if (bridge->driver->mask_memory == intel_gen6_mask_memory)
1801 mask = 40;
1802 else if (bridge->driver->mask_memory == intel_i965_mask_memory)
1803 mask = 36;
1804 else
1805 mask = 32;
1806
1807 if (pci_set_dma_mask(intel_private.pcidev, DMA_BIT_MASK(mask)))
1808 dev_err(&intel_private.pcidev->dev,
1809 "set gfx device dma mask %d-bit failed!\n", mask);
1810 else
1811 pci_set_consistent_dma_mask(intel_private.pcidev,
1812 DMA_BIT_MASK(mask));
1813
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001814 if (bridge->driver == &intel_810_driver)
1815 return 1;
1816
1817 intel_private.base.gtt_mappable_entries = intel_gtt_mappable_entries();
1818
Daniel Vetter02c026c2010-08-24 19:39:48 +02001819 return 1;
1820}
Daniel Vettere2404e72010-09-08 17:29:51 +02001821EXPORT_SYMBOL(intel_gmch_probe);
Daniel Vetter02c026c2010-08-24 19:39:48 +02001822
Daniel Vettere2404e72010-09-08 17:29:51 +02001823void intel_gmch_remove(struct pci_dev *pdev)
Daniel Vetter02c026c2010-08-24 19:39:48 +02001824{
1825 if (intel_private.pcidev)
1826 pci_dev_put(intel_private.pcidev);
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001827 if (intel_private.bridge_dev)
1828 pci_dev_put(intel_private.bridge_dev);
Daniel Vetter02c026c2010-08-24 19:39:48 +02001829}
Daniel Vettere2404e72010-09-08 17:29:51 +02001830EXPORT_SYMBOL(intel_gmch_remove);
1831
1832MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
1833MODULE_LICENSE("GPL and additional rights");