blob: 9272c38dd3c6ce8a66e9e0d2b0a5085ddd2973a7 [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
Daniel Vetter0e87d2b2010-09-07 22:11:15 +020038#else
39#define USE_PCI_DMA_API 0
Daniel Vetterf51b7662010-04-14 00:29:52 +020040#endif
41
Jesse Barnesd1d6ca72010-07-08 09:22:46 -070042/* Max amount of stolen space, anything above will be returned to Linux */
43int intel_max_stolen = 32 * 1024 * 1024;
Jesse Barnesd1d6ca72010-07-08 09:22:46 -070044
Daniel Vetterf51b7662010-04-14 00:29:52 +020045static const struct aper_size_info_fixed intel_i810_sizes[] =
46{
47 {64, 16384, 4},
48 /* The 32M mode still requires a 64k gatt */
49 {32, 8192, 4}
50};
51
52#define AGP_DCACHE_MEMORY 1
53#define AGP_PHYS_MEMORY 2
54#define INTEL_AGP_CACHED_MEMORY 3
55
56static struct gatt_mask intel_i810_masks[] =
57{
58 {.mask = I810_PTE_VALID, .type = 0},
59 {.mask = (I810_PTE_VALID | I810_PTE_LOCAL), .type = AGP_DCACHE_MEMORY},
60 {.mask = I810_PTE_VALID, .type = 0},
61 {.mask = I810_PTE_VALID | I830_PTE_SYSTEM_CACHED,
62 .type = INTEL_AGP_CACHED_MEMORY}
63};
64
Zhenyu Wangf8f235e2010-08-27 11:08:57 +080065#define INTEL_AGP_UNCACHED_MEMORY 0
66#define INTEL_AGP_CACHED_MEMORY_LLC 1
67#define INTEL_AGP_CACHED_MEMORY_LLC_GFDT 2
68#define INTEL_AGP_CACHED_MEMORY_LLC_MLC 3
69#define INTEL_AGP_CACHED_MEMORY_LLC_MLC_GFDT 4
70
Daniel Vetter1a997ff2010-09-08 21:18:53 +020071struct intel_gtt_driver {
72 unsigned int gen : 8;
73 unsigned int is_g33 : 1;
74 unsigned int is_pineview : 1;
75 unsigned int is_ironlake : 1;
Daniel Vetter22533b42010-09-12 16:38:55 +020076 unsigned int dma_mask_size : 8;
Daniel Vetter73800422010-08-29 17:29:50 +020077 /* Chipset specific GTT setup */
78 int (*setup)(void);
Daniel Vetterae83dd52010-09-12 17:11:15 +020079 /* This should undo anything done in ->setup() save the unmapping
80 * of the mmio register file, that's done in the generic code. */
81 void (*cleanup)(void);
Daniel Vetter351bb272010-09-07 22:41:04 +020082 void (*write_entry)(dma_addr_t addr, unsigned int entry, unsigned int flags);
83 /* Flags is a more or less chipset specific opaque value.
84 * For chipsets that need to support old ums (non-gem) code, this
85 * needs to be identical to the various supported agp memory types! */
Daniel Vetter5cbecaf2010-09-11 21:31:04 +020086 bool (*check_flags)(unsigned int flags);
Daniel Vetter1b263f22010-09-12 00:27:24 +020087 void (*chipset_flush)(void);
Daniel Vetter1a997ff2010-09-08 21:18:53 +020088};
89
Daniel Vetterf51b7662010-04-14 00:29:52 +020090static struct _intel_private {
Daniel Vetter0ade6382010-08-24 22:18:41 +020091 struct intel_gtt base;
Daniel Vetter1a997ff2010-09-08 21:18:53 +020092 const struct intel_gtt_driver *driver;
Daniel Vetterf51b7662010-04-14 00:29:52 +020093 struct pci_dev *pcidev; /* device one */
Daniel Vetterd7cca2f2010-08-24 23:06:19 +020094 struct pci_dev *bridge_dev;
Daniel Vetterf51b7662010-04-14 00:29:52 +020095 u8 __iomem *registers;
Daniel Vetterf67eab62010-08-29 17:27:36 +020096 phys_addr_t gtt_bus_addr;
Daniel Vetter73800422010-08-29 17:29:50 +020097 phys_addr_t gma_bus_addr;
Chris Wilson3f08e4e2010-09-14 20:15:22 +010098 phys_addr_t pte_bus_addr;
Daniel Vetterf51b7662010-04-14 00:29:52 +020099 u32 __iomem *gtt; /* I915G */
100 int num_dcache_entries;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200101 union {
102 void __iomem *i9xx_flush_page;
103 void *i8xx_flush_page;
104 };
105 struct page *i8xx_page;
106 struct resource ifp_resource;
107 int resource_valid;
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200108 struct page *scratch_page;
109 dma_addr_t scratch_page_dma;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200110} intel_private;
111
Daniel Vetter1a997ff2010-09-08 21:18:53 +0200112#define INTEL_GTT_GEN intel_private.driver->gen
113#define IS_G33 intel_private.driver->is_g33
114#define IS_PINEVIEW intel_private.driver->is_pineview
115#define IS_IRONLAKE intel_private.driver->is_ironlake
116
Daniel Vetterf51b7662010-04-14 00:29:52 +0200117static void intel_agp_free_sglist(struct agp_memory *mem)
118{
119 struct sg_table st;
120
121 st.sgl = mem->sg_list;
122 st.orig_nents = st.nents = mem->page_count;
123
124 sg_free_table(&st);
125
126 mem->sg_list = NULL;
127 mem->num_sg = 0;
128}
129
130static int intel_agp_map_memory(struct agp_memory *mem)
131{
132 struct sg_table st;
133 struct scatterlist *sg;
134 int i;
135
Daniel Vetterfefaa702010-09-11 22:12:11 +0200136 if (mem->sg_list)
137 return 0; /* already mapped (for e.g. resume */
138
Daniel Vetterf51b7662010-04-14 00:29:52 +0200139 DBG("try mapping %lu pages\n", (unsigned long)mem->page_count);
140
141 if (sg_alloc_table(&st, mem->page_count, GFP_KERNEL))
Chris Wilson831cd442010-07-24 18:29:37 +0100142 goto err;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200143
144 mem->sg_list = sg = st.sgl;
145
146 for (i = 0 ; i < mem->page_count; i++, sg = sg_next(sg))
147 sg_set_page(sg, mem->pages[i], PAGE_SIZE, 0);
148
149 mem->num_sg = pci_map_sg(intel_private.pcidev, mem->sg_list,
150 mem->page_count, PCI_DMA_BIDIRECTIONAL);
Chris Wilson831cd442010-07-24 18:29:37 +0100151 if (unlikely(!mem->num_sg))
152 goto err;
153
Daniel Vetterf51b7662010-04-14 00:29:52 +0200154 return 0;
Chris Wilson831cd442010-07-24 18:29:37 +0100155
156err:
157 sg_free_table(&st);
158 return -ENOMEM;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200159}
160
161static void intel_agp_unmap_memory(struct agp_memory *mem)
162{
163 DBG("try unmapping %lu pages\n", (unsigned long)mem->page_count);
164
165 pci_unmap_sg(intel_private.pcidev, mem->sg_list,
166 mem->page_count, PCI_DMA_BIDIRECTIONAL);
167 intel_agp_free_sglist(mem);
168}
169
Daniel Vetterf51b7662010-04-14 00:29:52 +0200170static int intel_i810_fetch_size(void)
171{
172 u32 smram_miscc;
173 struct aper_size_info_fixed *values;
174
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200175 pci_read_config_dword(intel_private.bridge_dev,
176 I810_SMRAM_MISCC, &smram_miscc);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200177 values = A_SIZE_FIX(agp_bridge->driver->aperture_sizes);
178
179 if ((smram_miscc & I810_GMS) == I810_GMS_DISABLE) {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200180 dev_warn(&intel_private.bridge_dev->dev, "i810 is disabled\n");
Daniel Vetterf51b7662010-04-14 00:29:52 +0200181 return 0;
182 }
183 if ((smram_miscc & I810_GFX_MEM_WIN_SIZE) == I810_GFX_MEM_WIN_32M) {
Daniel Vettere1583162010-04-14 00:29:58 +0200184 agp_bridge->current_size = (void *) (values + 1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200185 agp_bridge->aperture_size_idx = 1;
186 return values[1].size;
187 } else {
Daniel Vettere1583162010-04-14 00:29:58 +0200188 agp_bridge->current_size = (void *) (values);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200189 agp_bridge->aperture_size_idx = 0;
190 return values[0].size;
191 }
192
193 return 0;
194}
195
196static int intel_i810_configure(void)
197{
198 struct aper_size_info_fixed *current_size;
199 u32 temp;
200 int i;
201
202 current_size = A_SIZE_FIX(agp_bridge->current_size);
203
204 if (!intel_private.registers) {
205 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &temp);
206 temp &= 0xfff80000;
207
208 intel_private.registers = ioremap(temp, 128 * 4096);
209 if (!intel_private.registers) {
210 dev_err(&intel_private.pcidev->dev,
211 "can't remap memory\n");
212 return -ENOMEM;
213 }
214 }
215
216 if ((readl(intel_private.registers+I810_DRAM_CTL)
217 & I810_DRAM_ROW_0) == I810_DRAM_ROW_0_SDRAM) {
218 /* This will need to be dynamically assigned */
219 dev_info(&intel_private.pcidev->dev,
220 "detected 4MB dedicated video ram\n");
221 intel_private.num_dcache_entries = 1024;
222 }
223 pci_read_config_dword(intel_private.pcidev, I810_GMADDR, &temp);
224 agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
225 writel(agp_bridge->gatt_bus_addr | I810_PGETBL_ENABLED, intel_private.registers+I810_PGETBL_CTL);
226 readl(intel_private.registers+I810_PGETBL_CTL); /* PCI Posting. */
227
228 if (agp_bridge->driver->needs_scratch_page) {
229 for (i = 0; i < current_size->num_entries; i++) {
230 writel(agp_bridge->scratch_page, intel_private.registers+I810_PTE_BASE+(i*4));
231 }
232 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4)); /* PCI posting. */
233 }
234 global_cache_flush();
235 return 0;
236}
237
238static void intel_i810_cleanup(void)
239{
240 writel(0, intel_private.registers+I810_PGETBL_CTL);
241 readl(intel_private.registers); /* PCI Posting. */
242 iounmap(intel_private.registers);
243}
244
Daniel Vetterffdd7512010-08-27 17:51:29 +0200245static void intel_fake_agp_enable(struct agp_bridge_data *bridge, u32 mode)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200246{
247 return;
248}
249
250/* Exists to support ARGB cursors */
251static struct page *i8xx_alloc_pages(void)
252{
253 struct page *page;
254
255 page = alloc_pages(GFP_KERNEL | GFP_DMA32, 2);
256 if (page == NULL)
257 return NULL;
258
259 if (set_pages_uc(page, 4) < 0) {
260 set_pages_wb(page, 4);
261 __free_pages(page, 2);
262 return NULL;
263 }
264 get_page(page);
265 atomic_inc(&agp_bridge->current_memory_agp);
266 return page;
267}
268
269static void i8xx_destroy_pages(struct page *page)
270{
271 if (page == NULL)
272 return;
273
274 set_pages_wb(page, 4);
275 put_page(page);
276 __free_pages(page, 2);
277 atomic_dec(&agp_bridge->current_memory_agp);
278}
279
Daniel Vetterf51b7662010-04-14 00:29:52 +0200280static int intel_i810_insert_entries(struct agp_memory *mem, off_t pg_start,
281 int type)
282{
283 int i, j, num_entries;
284 void *temp;
285 int ret = -EINVAL;
286 int mask_type;
287
288 if (mem->page_count == 0)
289 goto out;
290
291 temp = agp_bridge->current_size;
292 num_entries = A_SIZE_FIX(temp)->num_entries;
293
294 if ((pg_start + mem->page_count) > num_entries)
295 goto out_err;
296
297
298 for (j = pg_start; j < (pg_start + mem->page_count); j++) {
299 if (!PGE_EMPTY(agp_bridge, readl(agp_bridge->gatt_table+j))) {
300 ret = -EBUSY;
301 goto out_err;
302 }
303 }
304
305 if (type != mem->type)
306 goto out_err;
307
308 mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type);
309
310 switch (mask_type) {
311 case AGP_DCACHE_MEMORY:
312 if (!mem->is_flushed)
313 global_cache_flush();
314 for (i = pg_start; i < (pg_start + mem->page_count); i++) {
315 writel((i*4096)|I810_PTE_LOCAL|I810_PTE_VALID,
316 intel_private.registers+I810_PTE_BASE+(i*4));
317 }
318 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4));
319 break;
320 case AGP_PHYS_MEMORY:
321 case AGP_NORMAL_MEMORY:
322 if (!mem->is_flushed)
323 global_cache_flush();
324 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
325 writel(agp_bridge->driver->mask_memory(agp_bridge,
326 page_to_phys(mem->pages[i]), mask_type),
327 intel_private.registers+I810_PTE_BASE+(j*4));
328 }
329 readl(intel_private.registers+I810_PTE_BASE+((j-1)*4));
330 break;
331 default:
332 goto out_err;
333 }
334
Daniel Vetterf51b7662010-04-14 00:29:52 +0200335out:
336 ret = 0;
337out_err:
338 mem->is_flushed = true;
339 return ret;
340}
341
342static int intel_i810_remove_entries(struct agp_memory *mem, off_t pg_start,
343 int type)
344{
345 int i;
346
347 if (mem->page_count == 0)
348 return 0;
349
350 for (i = pg_start; i < (mem->page_count + pg_start); i++) {
351 writel(agp_bridge->scratch_page, intel_private.registers+I810_PTE_BASE+(i*4));
352 }
353 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4));
354
Daniel Vetterf51b7662010-04-14 00:29:52 +0200355 return 0;
356}
357
358/*
359 * The i810/i830 requires a physical address to program its mouse
360 * pointer into hardware.
361 * However the Xserver still writes to it through the agp aperture.
362 */
363static struct agp_memory *alloc_agpphysmem_i8xx(size_t pg_count, int type)
364{
365 struct agp_memory *new;
366 struct page *page;
367
368 switch (pg_count) {
369 case 1: page = agp_bridge->driver->agp_alloc_page(agp_bridge);
370 break;
371 case 4:
372 /* kludge to get 4 physical pages for ARGB cursor */
373 page = i8xx_alloc_pages();
374 break;
375 default:
376 return NULL;
377 }
378
379 if (page == NULL)
380 return NULL;
381
382 new = agp_create_memory(pg_count);
383 if (new == NULL)
384 return NULL;
385
386 new->pages[0] = page;
387 if (pg_count == 4) {
388 /* kludge to get 4 physical pages for ARGB cursor */
389 new->pages[1] = new->pages[0] + 1;
390 new->pages[2] = new->pages[1] + 1;
391 new->pages[3] = new->pages[2] + 1;
392 }
393 new->page_count = pg_count;
394 new->num_scratch_pages = pg_count;
395 new->type = AGP_PHYS_MEMORY;
396 new->physical = page_to_phys(new->pages[0]);
397 return new;
398}
399
400static struct agp_memory *intel_i810_alloc_by_type(size_t pg_count, int type)
401{
402 struct agp_memory *new;
403
404 if (type == AGP_DCACHE_MEMORY) {
405 if (pg_count != intel_private.num_dcache_entries)
406 return NULL;
407
408 new = agp_create_memory(1);
409 if (new == NULL)
410 return NULL;
411
412 new->type = AGP_DCACHE_MEMORY;
413 new->page_count = pg_count;
414 new->num_scratch_pages = 0;
415 agp_free_page_array(new);
416 return new;
417 }
418 if (type == AGP_PHYS_MEMORY)
419 return alloc_agpphysmem_i8xx(pg_count, type);
420 return NULL;
421}
422
423static void intel_i810_free_by_type(struct agp_memory *curr)
424{
425 agp_free_key(curr->key);
426 if (curr->type == AGP_PHYS_MEMORY) {
427 if (curr->page_count == 4)
428 i8xx_destroy_pages(curr->pages[0]);
429 else {
430 agp_bridge->driver->agp_destroy_page(curr->pages[0],
431 AGP_PAGE_DESTROY_UNMAP);
432 agp_bridge->driver->agp_destroy_page(curr->pages[0],
433 AGP_PAGE_DESTROY_FREE);
434 }
435 agp_free_page_array(curr);
436 }
437 kfree(curr);
438}
439
440static unsigned long intel_i810_mask_memory(struct agp_bridge_data *bridge,
441 dma_addr_t addr, int type)
442{
443 /* Type checking must be done elsewhere */
444 return addr | bridge->driver->masks[type].mask;
445}
446
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200447static int intel_gtt_setup_scratch_page(void)
448{
449 struct page *page;
450 dma_addr_t dma_addr;
451
452 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
453 if (page == NULL)
454 return -ENOMEM;
455 get_page(page);
456 set_pages_uc(page, 1);
457
458 if (USE_PCI_DMA_API && INTEL_GTT_GEN > 2) {
459 dma_addr = pci_map_page(intel_private.pcidev, page, 0,
460 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
461 if (pci_dma_mapping_error(intel_private.pcidev, dma_addr))
462 return -EINVAL;
463
464 intel_private.scratch_page_dma = dma_addr;
465 } else
466 intel_private.scratch_page_dma = page_to_phys(page);
467
468 intel_private.scratch_page = page;
469
470 return 0;
471}
472
Chris Wilson9e76e7b2010-09-14 12:12:11 +0100473static const struct aper_size_info_fixed const intel_fake_agp_sizes[] = {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200474 {128, 32768, 5},
475 /* The 64M mode still requires a 128k gatt */
476 {64, 16384, 5},
477 {256, 65536, 6},
478 {512, 131072, 7},
479};
480
Daniel Vetterbfde0672010-08-24 23:07:59 +0200481static unsigned int intel_gtt_stolen_entries(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200482{
483 u16 gmch_ctrl;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200484 u8 rdct;
485 int local = 0;
486 static const int ddt[4] = { 0, 16, 32, 64 };
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200487 unsigned int overhead_entries, stolen_entries;
488 unsigned int stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200489
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200490 pci_read_config_word(intel_private.bridge_dev,
491 I830_GMCH_CTRL, &gmch_ctrl);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200492
Daniel Vetter1a997ff2010-09-08 21:18:53 +0200493 if (INTEL_GTT_GEN > 4 || IS_PINEVIEW)
Daniel Vetterfbe40782010-08-27 17:12:41 +0200494 overhead_entries = 0;
495 else
496 overhead_entries = intel_private.base.gtt_mappable_entries
497 / 1024;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200498
Daniel Vetterfbe40782010-08-27 17:12:41 +0200499 overhead_entries += 1; /* BIOS popup */
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200500
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200501 if (intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82830_HB ||
502 intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82845G_HB) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200503 switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
504 case I830_GMCH_GMS_STOLEN_512:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200505 stolen_size = KB(512);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200506 break;
507 case I830_GMCH_GMS_STOLEN_1024:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200508 stolen_size = MB(1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200509 break;
510 case I830_GMCH_GMS_STOLEN_8192:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200511 stolen_size = MB(8);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200512 break;
513 case I830_GMCH_GMS_LOCAL:
514 rdct = readb(intel_private.registers+I830_RDRAM_CHANNEL_TYPE);
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200515 stolen_size = (I830_RDRAM_ND(rdct) + 1) *
Daniel Vetterf51b7662010-04-14 00:29:52 +0200516 MB(ddt[I830_RDRAM_DDT(rdct)]);
517 local = 1;
518 break;
519 default:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200520 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200521 break;
522 }
Daniel Vetter1a997ff2010-09-08 21:18:53 +0200523 } else if (INTEL_GTT_GEN == 6) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200524 /*
525 * SandyBridge has new memory control reg at 0x50.w
526 */
527 u16 snb_gmch_ctl;
528 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl);
529 switch (snb_gmch_ctl & SNB_GMCH_GMS_STOLEN_MASK) {
530 case SNB_GMCH_GMS_STOLEN_32M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200531 stolen_size = MB(32);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200532 break;
533 case SNB_GMCH_GMS_STOLEN_64M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200534 stolen_size = MB(64);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200535 break;
536 case SNB_GMCH_GMS_STOLEN_96M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200537 stolen_size = MB(96);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200538 break;
539 case SNB_GMCH_GMS_STOLEN_128M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200540 stolen_size = MB(128);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200541 break;
542 case SNB_GMCH_GMS_STOLEN_160M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200543 stolen_size = MB(160);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200544 break;
545 case SNB_GMCH_GMS_STOLEN_192M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200546 stolen_size = MB(192);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200547 break;
548 case SNB_GMCH_GMS_STOLEN_224M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200549 stolen_size = MB(224);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200550 break;
551 case SNB_GMCH_GMS_STOLEN_256M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200552 stolen_size = MB(256);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200553 break;
554 case SNB_GMCH_GMS_STOLEN_288M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200555 stolen_size = MB(288);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200556 break;
557 case SNB_GMCH_GMS_STOLEN_320M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200558 stolen_size = MB(320);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200559 break;
560 case SNB_GMCH_GMS_STOLEN_352M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200561 stolen_size = MB(352);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200562 break;
563 case SNB_GMCH_GMS_STOLEN_384M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200564 stolen_size = MB(384);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200565 break;
566 case SNB_GMCH_GMS_STOLEN_416M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200567 stolen_size = MB(416);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200568 break;
569 case SNB_GMCH_GMS_STOLEN_448M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200570 stolen_size = MB(448);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200571 break;
572 case SNB_GMCH_GMS_STOLEN_480M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200573 stolen_size = MB(480);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200574 break;
575 case SNB_GMCH_GMS_STOLEN_512M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200576 stolen_size = MB(512);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200577 break;
578 }
579 } else {
580 switch (gmch_ctrl & I855_GMCH_GMS_MASK) {
581 case I855_GMCH_GMS_STOLEN_1M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200582 stolen_size = MB(1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200583 break;
584 case I855_GMCH_GMS_STOLEN_4M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200585 stolen_size = MB(4);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200586 break;
587 case I855_GMCH_GMS_STOLEN_8M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200588 stolen_size = MB(8);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200589 break;
590 case I855_GMCH_GMS_STOLEN_16M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200591 stolen_size = MB(16);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200592 break;
593 case I855_GMCH_GMS_STOLEN_32M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200594 stolen_size = MB(32);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200595 break;
596 case I915_GMCH_GMS_STOLEN_48M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200597 stolen_size = MB(48);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200598 break;
599 case I915_GMCH_GMS_STOLEN_64M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200600 stolen_size = MB(64);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200601 break;
602 case G33_GMCH_GMS_STOLEN_128M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200603 stolen_size = MB(128);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200604 break;
605 case G33_GMCH_GMS_STOLEN_256M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200606 stolen_size = MB(256);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200607 break;
608 case INTEL_GMCH_GMS_STOLEN_96M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200609 stolen_size = MB(96);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200610 break;
611 case INTEL_GMCH_GMS_STOLEN_160M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200612 stolen_size = MB(160);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200613 break;
614 case INTEL_GMCH_GMS_STOLEN_224M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200615 stolen_size = MB(224);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200616 break;
617 case INTEL_GMCH_GMS_STOLEN_352M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200618 stolen_size = MB(352);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200619 break;
620 default:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200621 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200622 break;
623 }
624 }
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200625
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200626 if (!local && stolen_size > intel_max_stolen) {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200627 dev_info(&intel_private.bridge_dev->dev,
Jesse Barnesd1d6ca72010-07-08 09:22:46 -0700628 "detected %dK stolen memory, trimming to %dK\n",
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200629 stolen_size / KB(1), intel_max_stolen / KB(1));
630 stolen_size = intel_max_stolen;
631 } else if (stolen_size > 0) {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200632 dev_info(&intel_private.bridge_dev->dev, "detected %dK %s memory\n",
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200633 stolen_size / KB(1), local ? "local" : "stolen");
Daniel Vetterf51b7662010-04-14 00:29:52 +0200634 } else {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200635 dev_info(&intel_private.bridge_dev->dev,
Daniel Vetterf51b7662010-04-14 00:29:52 +0200636 "no pre-allocated video memory detected\n");
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200637 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200638 }
639
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200640 stolen_entries = stolen_size/KB(4) - overhead_entries;
641
642 return stolen_entries;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200643}
644
Daniel Vetterfbe40782010-08-27 17:12:41 +0200645static unsigned int intel_gtt_total_entries(void)
646{
647 int size;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200648
Daniel Vetter210b23c2010-08-28 16:14:32 +0200649 if (IS_G33 || INTEL_GTT_GEN == 4 || INTEL_GTT_GEN == 5) {
Daniel Vetterfbe40782010-08-27 17:12:41 +0200650 u32 pgetbl_ctl;
651 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
652
Daniel Vetterfbe40782010-08-27 17:12:41 +0200653 switch (pgetbl_ctl & I965_PGETBL_SIZE_MASK) {
654 case I965_PGETBL_SIZE_128KB:
Daniel Vettere5e408f2010-08-28 11:04:32 +0200655 size = KB(128);
Daniel Vetterfbe40782010-08-27 17:12:41 +0200656 break;
657 case I965_PGETBL_SIZE_256KB:
Daniel Vettere5e408f2010-08-28 11:04:32 +0200658 size = KB(256);
Daniel Vetterfbe40782010-08-27 17:12:41 +0200659 break;
660 case I965_PGETBL_SIZE_512KB:
Daniel Vettere5e408f2010-08-28 11:04:32 +0200661 size = KB(512);
Daniel Vetterfbe40782010-08-27 17:12:41 +0200662 break;
663 case I965_PGETBL_SIZE_1MB:
Daniel Vettere5e408f2010-08-28 11:04:32 +0200664 size = KB(1024);
Daniel Vetterfbe40782010-08-27 17:12:41 +0200665 break;
666 case I965_PGETBL_SIZE_2MB:
Daniel Vettere5e408f2010-08-28 11:04:32 +0200667 size = KB(2048);
Daniel Vetterfbe40782010-08-27 17:12:41 +0200668 break;
669 case I965_PGETBL_SIZE_1_5MB:
Daniel Vettere5e408f2010-08-28 11:04:32 +0200670 size = KB(1024 + 512);
Daniel Vetterfbe40782010-08-27 17:12:41 +0200671 break;
672 default:
673 dev_info(&intel_private.pcidev->dev,
674 "unknown page table size, assuming 512KB\n");
Daniel Vettere5e408f2010-08-28 11:04:32 +0200675 size = KB(512);
Daniel Vetterfbe40782010-08-27 17:12:41 +0200676 }
Daniel Vettere5e408f2010-08-28 11:04:32 +0200677
678 return size/4;
Daniel Vetter210b23c2010-08-28 16:14:32 +0200679 } else if (INTEL_GTT_GEN == 6) {
680 u16 snb_gmch_ctl;
681
682 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl);
683 switch (snb_gmch_ctl & SNB_GTT_SIZE_MASK) {
684 default:
685 case SNB_GTT_SIZE_0M:
686 printk(KERN_ERR "Bad GTT size mask: 0x%04x.\n", snb_gmch_ctl);
687 size = MB(0);
688 break;
689 case SNB_GTT_SIZE_1M:
690 size = MB(1);
691 break;
692 case SNB_GTT_SIZE_2M:
693 size = MB(2);
694 break;
695 }
696 return size/4;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200697 } else {
698 /* On previous hardware, the GTT size was just what was
699 * required to map the aperture.
700 */
Daniel Vettere5e408f2010-08-28 11:04:32 +0200701 return intel_private.base.gtt_mappable_entries;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200702 }
Daniel Vetterfbe40782010-08-27 17:12:41 +0200703}
Daniel Vetterfbe40782010-08-27 17:12:41 +0200704
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200705static unsigned int intel_gtt_mappable_entries(void)
706{
707 unsigned int aperture_size;
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200708
Daniel Vetter239918f2010-08-31 22:30:43 +0200709 if (INTEL_GTT_GEN == 2) {
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100710 u16 gmch_ctrl;
711
712 pci_read_config_word(intel_private.bridge_dev,
713 I830_GMCH_CTRL, &gmch_ctrl);
714
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200715 if ((gmch_ctrl & I830_GMCH_MEM_MASK) == I830_GMCH_MEM_64M)
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100716 aperture_size = MB(64);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200717 else
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100718 aperture_size = MB(128);
Daniel Vetter239918f2010-08-31 22:30:43 +0200719 } else {
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200720 /* 9xx supports large sizes, just look at the length */
721 aperture_size = pci_resource_len(intel_private.pcidev, 2);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200722 }
723
724 return aperture_size >> PAGE_SHIFT;
725}
726
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200727static void intel_gtt_teardown_scratch_page(void)
728{
729 set_pages_wb(intel_private.scratch_page, 1);
730 pci_unmap_page(intel_private.pcidev, intel_private.scratch_page_dma,
731 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
732 put_page(intel_private.scratch_page);
733 __free_page(intel_private.scratch_page);
734}
735
736static void intel_gtt_cleanup(void)
737{
Daniel Vetterae83dd52010-09-12 17:11:15 +0200738 intel_private.driver->cleanup();
739
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200740 iounmap(intel_private.gtt);
741 iounmap(intel_private.registers);
742
743 intel_gtt_teardown_scratch_page();
744}
745
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200746static int intel_gtt_init(void)
747{
Daniel Vetterf67eab62010-08-29 17:27:36 +0200748 u32 gtt_map_size;
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200749 int ret;
750
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200751 ret = intel_private.driver->setup();
752 if (ret != 0)
753 return ret;
Daniel Vetterf67eab62010-08-29 17:27:36 +0200754
755 intel_private.base.gtt_mappable_entries = intel_gtt_mappable_entries();
756 intel_private.base.gtt_total_entries = intel_gtt_total_entries();
757
Daniel Vetter0af9e922010-09-12 14:04:03 +0200758 dev_info(&intel_private.bridge_dev->dev,
759 "detected gtt size: %dK total, %dK mappable\n",
760 intel_private.base.gtt_total_entries * 4,
761 intel_private.base.gtt_mappable_entries * 4);
762
Daniel Vetterf67eab62010-08-29 17:27:36 +0200763 gtt_map_size = intel_private.base.gtt_total_entries * 4;
764
765 intel_private.gtt = ioremap(intel_private.gtt_bus_addr,
766 gtt_map_size);
767 if (!intel_private.gtt) {
Daniel Vetterae83dd52010-09-12 17:11:15 +0200768 intel_private.driver->cleanup();
Daniel Vetterf67eab62010-08-29 17:27:36 +0200769 iounmap(intel_private.registers);
770 return -ENOMEM;
771 }
772
773 global_cache_flush(); /* FIXME: ? */
774
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200775 /* we have to call this as early as possible after the MMIO base address is known */
776 intel_private.base.gtt_stolen_entries = intel_gtt_stolen_entries();
777 if (intel_private.base.gtt_stolen_entries == 0) {
Daniel Vetterae83dd52010-09-12 17:11:15 +0200778 intel_private.driver->cleanup();
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200779 iounmap(intel_private.registers);
Daniel Vetterf67eab62010-08-29 17:27:36 +0200780 iounmap(intel_private.gtt);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200781 return -ENOMEM;
782 }
783
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200784 ret = intel_gtt_setup_scratch_page();
785 if (ret != 0) {
786 intel_gtt_cleanup();
787 return ret;
788 }
789
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200790 return 0;
791}
792
Daniel Vetter3e921f92010-08-27 15:33:26 +0200793static int intel_fake_agp_fetch_size(void)
794{
Chris Wilson9e76e7b2010-09-14 12:12:11 +0100795 int num_sizes = ARRAY_SIZE(intel_fake_agp_sizes);
Daniel Vetter3e921f92010-08-27 15:33:26 +0200796 unsigned int aper_size;
797 int i;
Daniel Vetter3e921f92010-08-27 15:33:26 +0200798
799 aper_size = (intel_private.base.gtt_mappable_entries << PAGE_SHIFT)
800 / MB(1);
801
802 for (i = 0; i < num_sizes; i++) {
Daniel Vetterffdd7512010-08-27 17:51:29 +0200803 if (aper_size == intel_fake_agp_sizes[i].size) {
Chris Wilson9e76e7b2010-09-14 12:12:11 +0100804 agp_bridge->current_size =
805 (void *) (intel_fake_agp_sizes + i);
Daniel Vetter3e921f92010-08-27 15:33:26 +0200806 return aper_size;
807 }
808 }
809
810 return 0;
811}
812
Daniel Vetterae83dd52010-09-12 17:11:15 +0200813static void i830_cleanup(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200814{
815 kunmap(intel_private.i8xx_page);
816 intel_private.i8xx_flush_page = NULL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200817
818 __free_page(intel_private.i8xx_page);
819 intel_private.i8xx_page = NULL;
820}
821
822static void intel_i830_setup_flush(void)
823{
824 /* return if we've already set the flush mechanism up */
825 if (intel_private.i8xx_page)
826 return;
827
Jan Beuliche61cb0d2010-09-24 13:25:30 +0100828 intel_private.i8xx_page = alloc_page(GFP_KERNEL);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200829 if (!intel_private.i8xx_page)
830 return;
831
832 intel_private.i8xx_flush_page = kmap(intel_private.i8xx_page);
833 if (!intel_private.i8xx_flush_page)
Daniel Vetterae83dd52010-09-12 17:11:15 +0200834 i830_cleanup();
Daniel Vetterf51b7662010-04-14 00:29:52 +0200835}
836
837/* The chipset_flush interface needs to get data that has already been
838 * flushed out of the CPU all the way out to main memory, because the GPU
839 * doesn't snoop those buffers.
840 *
841 * The 8xx series doesn't have the same lovely interface for flushing the
842 * chipset write buffers that the later chips do. According to the 865
843 * specs, it's 64 octwords, or 1KB. So, to get those previous things in
844 * that buffer out, we just fill 1KB and clflush it out, on the assumption
845 * that it'll push whatever was in there out. It appears to work.
846 */
Daniel Vetter1b263f22010-09-12 00:27:24 +0200847static void i830_chipset_flush(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200848{
849 unsigned int *pg = intel_private.i8xx_flush_page;
850
851 memset(pg, 0, 1024);
852
853 if (cpu_has_clflush)
854 clflush_cache_range(pg, 1024);
855 else if (wbinvd_on_all_cpus() != 0)
856 printk(KERN_ERR "Timed out waiting for cache flush.\n");
857}
858
Daniel Vetter351bb272010-09-07 22:41:04 +0200859static void i830_write_entry(dma_addr_t addr, unsigned int entry,
860 unsigned int flags)
861{
862 u32 pte_flags = I810_PTE_VALID;
863
864 switch (flags) {
865 case AGP_DCACHE_MEMORY:
866 pte_flags |= I810_PTE_LOCAL;
867 break;
868 case AGP_USER_CACHED_MEMORY:
869 pte_flags |= I830_PTE_SYSTEM_CACHED;
870 break;
871 }
872
873 writel(addr | pte_flags, intel_private.gtt + entry);
874}
875
Daniel Vetter73800422010-08-29 17:29:50 +0200876static void intel_enable_gtt(void)
877{
Chris Wilson3f08e4e2010-09-14 20:15:22 +0100878 u32 gma_addr;
Daniel Vetter73800422010-08-29 17:29:50 +0200879 u16 gmch_ctrl;
880
Daniel Vetter2d2430c2010-08-29 17:35:30 +0200881 if (INTEL_GTT_GEN == 2)
882 pci_read_config_dword(intel_private.pcidev, I810_GMADDR,
883 &gma_addr);
884 else
885 pci_read_config_dword(intel_private.pcidev, I915_GMADDR,
886 &gma_addr);
887
Daniel Vetter73800422010-08-29 17:29:50 +0200888 intel_private.gma_bus_addr = (gma_addr & PCI_BASE_ADDRESS_MEM_MASK);
889
890 pci_read_config_word(intel_private.bridge_dev, I830_GMCH_CTRL, &gmch_ctrl);
891 gmch_ctrl |= I830_GMCH_ENABLED;
892 pci_write_config_word(intel_private.bridge_dev, I830_GMCH_CTRL, gmch_ctrl);
893
Chris Wilson3f08e4e2010-09-14 20:15:22 +0100894 writel(intel_private.pte_bus_addr|I810_PGETBL_ENABLED,
895 intel_private.registers+I810_PGETBL_CTL);
Daniel Vetter73800422010-08-29 17:29:50 +0200896 readl(intel_private.registers+I810_PGETBL_CTL); /* PCI Posting. */
897}
898
899static int i830_setup(void)
900{
901 u32 reg_addr;
902
903 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &reg_addr);
904 reg_addr &= 0xfff80000;
905
906 intel_private.registers = ioremap(reg_addr, KB(64));
907 if (!intel_private.registers)
908 return -ENOMEM;
909
910 intel_private.gtt_bus_addr = reg_addr + I810_PTE_BASE;
Chris Wilson3f08e4e2010-09-14 20:15:22 +0100911 intel_private.pte_bus_addr =
912 readl(intel_private.registers+I810_PGETBL_CTL) & 0xfffff000;
Daniel Vetter73800422010-08-29 17:29:50 +0200913
914 intel_i830_setup_flush();
915
916 return 0;
917}
918
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200919static int intel_fake_agp_create_gatt_table(struct agp_bridge_data *bridge)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200920{
Daniel Vetter73800422010-08-29 17:29:50 +0200921 agp_bridge->gatt_table_real = NULL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200922 agp_bridge->gatt_table = NULL;
Daniel Vetter73800422010-08-29 17:29:50 +0200923 agp_bridge->gatt_bus_addr = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200924
925 return 0;
926}
927
Daniel Vetterffdd7512010-08-27 17:51:29 +0200928static int intel_fake_agp_free_gatt_table(struct agp_bridge_data *bridge)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200929{
930 return 0;
931}
932
Daniel Vetter351bb272010-09-07 22:41:04 +0200933static int intel_fake_agp_configure(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200934{
Daniel Vetterf51b7662010-04-14 00:29:52 +0200935 int i;
936
Daniel Vetter73800422010-08-29 17:29:50 +0200937 intel_enable_gtt();
Daniel Vetterf51b7662010-04-14 00:29:52 +0200938
Daniel Vetter73800422010-08-29 17:29:50 +0200939 agp_bridge->gart_bus_addr = intel_private.gma_bus_addr;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200940
Daniel Vetter351bb272010-09-07 22:41:04 +0200941 for (i = intel_private.base.gtt_stolen_entries;
942 i < intel_private.base.gtt_total_entries; i++) {
943 intel_private.driver->write_entry(intel_private.scratch_page_dma,
944 i, 0);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200945 }
Daniel Vetter351bb272010-09-07 22:41:04 +0200946 readl(intel_private.gtt+i-1); /* PCI Posting. */
Daniel Vetterf51b7662010-04-14 00:29:52 +0200947
948 global_cache_flush();
949
Daniel Vetterf51b7662010-04-14 00:29:52 +0200950 return 0;
951}
952
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200953static bool i830_check_flags(unsigned int flags)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200954{
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200955 switch (flags) {
956 case 0:
957 case AGP_PHYS_MEMORY:
958 case AGP_USER_CACHED_MEMORY:
959 case AGP_USER_MEMORY:
960 return true;
961 }
962
963 return false;
964}
965
Daniel Vetterfefaa702010-09-11 22:12:11 +0200966static void intel_gtt_insert_sg_entries(struct scatterlist *sg_list,
967 unsigned int sg_len,
968 unsigned int pg_start,
969 unsigned int flags)
970{
971 struct scatterlist *sg;
972 unsigned int len, m;
973 int i, j;
974
975 j = pg_start;
976
977 /* sg may merge pages, but we have to separate
978 * per-page addr for GTT */
979 for_each_sg(sg_list, sg, sg_len, i) {
980 len = sg_dma_len(sg) >> PAGE_SHIFT;
981 for (m = 0; m < len; m++) {
982 dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
983 intel_private.driver->write_entry(addr,
984 j, flags);
985 j++;
986 }
987 }
988 readl(intel_private.gtt+j-1);
989}
990
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200991static int intel_fake_agp_insert_entries(struct agp_memory *mem,
992 off_t pg_start, int type)
993{
994 int i, j;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200995 int ret = -EINVAL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200996
997 if (mem->page_count == 0)
998 goto out;
999
Daniel Vetter0ade6382010-08-24 22:18:41 +02001000 if (pg_start < intel_private.base.gtt_stolen_entries) {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001001 dev_printk(KERN_DEBUG, &intel_private.pcidev->dev,
Daniel Vetter0ade6382010-08-24 22:18:41 +02001002 "pg_start == 0x%.8lx, gtt_stolen_entries == 0x%.8x\n",
1003 pg_start, intel_private.base.gtt_stolen_entries);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001004
1005 dev_info(&intel_private.pcidev->dev,
1006 "trying to insert into local/stolen memory\n");
1007 goto out_err;
1008 }
1009
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001010 if ((pg_start + mem->page_count) > intel_private.base.gtt_total_entries)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001011 goto out_err;
1012
Daniel Vetterf51b7662010-04-14 00:29:52 +02001013 if (type != mem->type)
1014 goto out_err;
1015
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001016 if (!intel_private.driver->check_flags(type))
Daniel Vetterf51b7662010-04-14 00:29:52 +02001017 goto out_err;
1018
1019 if (!mem->is_flushed)
1020 global_cache_flush();
1021
Daniel Vetterfefaa702010-09-11 22:12:11 +02001022 if (USE_PCI_DMA_API && INTEL_GTT_GEN > 2) {
1023 ret = intel_agp_map_memory(mem);
1024 if (ret != 0)
1025 return ret;
1026
1027 intel_gtt_insert_sg_entries(mem->sg_list, mem->num_sg,
1028 pg_start, type);
1029 } else {
1030 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
1031 dma_addr_t addr = page_to_phys(mem->pages[i]);
1032 intel_private.driver->write_entry(addr,
1033 j, type);
1034 }
1035 readl(intel_private.gtt+j-1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001036 }
Daniel Vetterf51b7662010-04-14 00:29:52 +02001037
1038out:
1039 ret = 0;
1040out_err:
1041 mem->is_flushed = true;
1042 return ret;
1043}
1044
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001045static int intel_fake_agp_remove_entries(struct agp_memory *mem,
1046 off_t pg_start, int type)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001047{
1048 int i;
1049
1050 if (mem->page_count == 0)
1051 return 0;
1052
Daniel Vetter0ade6382010-08-24 22:18:41 +02001053 if (pg_start < intel_private.base.gtt_stolen_entries) {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001054 dev_info(&intel_private.pcidev->dev,
1055 "trying to disable local/stolen memory\n");
1056 return -EINVAL;
1057 }
1058
Daniel Vetterfefaa702010-09-11 22:12:11 +02001059 if (USE_PCI_DMA_API && INTEL_GTT_GEN > 2)
1060 intel_agp_unmap_memory(mem);
1061
Daniel Vetterf51b7662010-04-14 00:29:52 +02001062 for (i = pg_start; i < (mem->page_count + pg_start); i++) {
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001063 intel_private.driver->write_entry(intel_private.scratch_page_dma,
1064 i, 0);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001065 }
Daniel Vetterfdfb58a2010-08-29 00:15:03 +02001066 readl(intel_private.gtt+i-1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001067
Daniel Vetterf51b7662010-04-14 00:29:52 +02001068 return 0;
1069}
1070
Daniel Vetter1b263f22010-09-12 00:27:24 +02001071static void intel_fake_agp_chipset_flush(struct agp_bridge_data *bridge)
1072{
1073 intel_private.driver->chipset_flush();
1074}
1075
Daniel Vetterffdd7512010-08-27 17:51:29 +02001076static struct agp_memory *intel_fake_agp_alloc_by_type(size_t pg_count,
1077 int type)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001078{
1079 if (type == AGP_PHYS_MEMORY)
1080 return alloc_agpphysmem_i8xx(pg_count, type);
1081 /* always return NULL for other allocation types for now */
1082 return NULL;
1083}
1084
1085static int intel_alloc_chipset_flush_resource(void)
1086{
1087 int ret;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001088 ret = pci_bus_alloc_resource(intel_private.bridge_dev->bus, &intel_private.ifp_resource, PAGE_SIZE,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001089 PAGE_SIZE, PCIBIOS_MIN_MEM, 0,
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001090 pcibios_align_resource, intel_private.bridge_dev);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001091
1092 return ret;
1093}
1094
1095static void intel_i915_setup_chipset_flush(void)
1096{
1097 int ret;
1098 u32 temp;
1099
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001100 pci_read_config_dword(intel_private.bridge_dev, I915_IFPADDR, &temp);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001101 if (!(temp & 0x1)) {
1102 intel_alloc_chipset_flush_resource();
1103 intel_private.resource_valid = 1;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001104 pci_write_config_dword(intel_private.bridge_dev, I915_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001105 } else {
1106 temp &= ~1;
1107
1108 intel_private.resource_valid = 1;
1109 intel_private.ifp_resource.start = temp;
1110 intel_private.ifp_resource.end = temp + PAGE_SIZE;
1111 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
1112 /* some BIOSes reserve this area in a pnp some don't */
1113 if (ret)
1114 intel_private.resource_valid = 0;
1115 }
1116}
1117
1118static void intel_i965_g33_setup_chipset_flush(void)
1119{
1120 u32 temp_hi, temp_lo;
1121 int ret;
1122
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001123 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4, &temp_hi);
1124 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR, &temp_lo);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001125
1126 if (!(temp_lo & 0x1)) {
1127
1128 intel_alloc_chipset_flush_resource();
1129
1130 intel_private.resource_valid = 1;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001131 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001132 upper_32_bits(intel_private.ifp_resource.start));
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001133 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001134 } else {
1135 u64 l64;
1136
1137 temp_lo &= ~0x1;
1138 l64 = ((u64)temp_hi << 32) | temp_lo;
1139
1140 intel_private.resource_valid = 1;
1141 intel_private.ifp_resource.start = l64;
1142 intel_private.ifp_resource.end = l64 + PAGE_SIZE;
1143 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
1144 /* some BIOSes reserve this area in a pnp some don't */
1145 if (ret)
1146 intel_private.resource_valid = 0;
1147 }
1148}
1149
1150static void intel_i9xx_setup_flush(void)
1151{
1152 /* return if already configured */
1153 if (intel_private.ifp_resource.start)
1154 return;
1155
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001156 if (INTEL_GTT_GEN == 6)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001157 return;
1158
1159 /* setup a resource for this object */
1160 intel_private.ifp_resource.name = "Intel Flush Page";
1161 intel_private.ifp_resource.flags = IORESOURCE_MEM;
1162
1163 /* Setup chipset flush for 915 */
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001164 if (IS_G33 || INTEL_GTT_GEN >= 4) {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001165 intel_i965_g33_setup_chipset_flush();
1166 } else {
1167 intel_i915_setup_chipset_flush();
1168 }
1169
Chris Wilsondf51e7a2010-09-04 14:57:27 +01001170 if (intel_private.ifp_resource.start)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001171 intel_private.i9xx_flush_page = ioremap_nocache(intel_private.ifp_resource.start, PAGE_SIZE);
Chris Wilsondf51e7a2010-09-04 14:57:27 +01001172 if (!intel_private.i9xx_flush_page)
1173 dev_err(&intel_private.pcidev->dev,
1174 "can't ioremap flush page - no chipset flushing\n");
Daniel Vetterf51b7662010-04-14 00:29:52 +02001175}
1176
Daniel Vetterae83dd52010-09-12 17:11:15 +02001177static void i9xx_cleanup(void)
1178{
1179 if (intel_private.i9xx_flush_page)
1180 iounmap(intel_private.i9xx_flush_page);
1181 if (intel_private.resource_valid)
1182 release_resource(&intel_private.ifp_resource);
1183 intel_private.ifp_resource.start = 0;
1184 intel_private.resource_valid = 0;
1185}
1186
Daniel Vetter1b263f22010-09-12 00:27:24 +02001187static void i9xx_chipset_flush(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001188{
1189 if (intel_private.i9xx_flush_page)
1190 writel(1, intel_private.i9xx_flush_page);
1191}
1192
Daniel Vettera6963592010-09-11 14:01:43 +02001193static void i965_write_entry(dma_addr_t addr, unsigned int entry,
1194 unsigned int flags)
1195{
1196 /* Shift high bits down */
1197 addr |= (addr >> 28) & 0xf0;
1198 writel(addr | I810_PTE_VALID, intel_private.gtt + entry);
1199}
1200
Daniel Vetter90cb1492010-09-11 23:55:20 +02001201static bool gen6_check_flags(unsigned int flags)
1202{
1203 return true;
1204}
1205
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001206static void gen6_write_entry(dma_addr_t addr, unsigned int entry,
1207 unsigned int flags)
1208{
1209 unsigned int type_mask = flags & ~AGP_USER_CACHED_MEMORY_GFDT;
1210 unsigned int gfdt = flags & AGP_USER_CACHED_MEMORY_GFDT;
1211 u32 pte_flags;
1212
Zhenyu Wang8d0f5672010-11-02 17:30:47 +08001213 if (type_mask == AGP_USER_MEMORY)
Chris Wilson85ccc352010-10-22 14:59:29 +01001214 pte_flags = GEN6_PTE_UNCACHED | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001215 else if (type_mask == AGP_USER_CACHED_MEMORY_LLC_MLC) {
Zhenyu Wang16a02cf2010-11-02 17:30:46 +08001216 pte_flags = GEN6_PTE_LLC_MLC | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001217 if (gfdt)
1218 pte_flags |= GEN6_PTE_GFDT;
1219 } else { /* set 'normal'/'cached' to LLC by default */
Zhenyu Wang16a02cf2010-11-02 17:30:46 +08001220 pte_flags = GEN6_PTE_LLC | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001221 if (gfdt)
1222 pte_flags |= GEN6_PTE_GFDT;
1223 }
1224
1225 /* gen6 has bit11-4 for physical addr bit39-32 */
1226 addr |= (addr >> 28) & 0xff0;
1227 writel(addr | pte_flags, intel_private.gtt + entry);
1228}
1229
Daniel Vetterae83dd52010-09-12 17:11:15 +02001230static void gen6_cleanup(void)
1231{
1232}
1233
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001234static int i9xx_setup(void)
1235{
1236 u32 reg_addr;
1237
1238 pci_read_config_dword(intel_private.pcidev, I915_MMADDR, &reg_addr);
1239
1240 reg_addr &= 0xfff80000;
1241
1242 intel_private.registers = ioremap(reg_addr, 128 * 4096);
1243 if (!intel_private.registers)
1244 return -ENOMEM;
1245
1246 if (INTEL_GTT_GEN == 3) {
1247 u32 gtt_addr;
Chris Wilson3f08e4e2010-09-14 20:15:22 +01001248
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001249 pci_read_config_dword(intel_private.pcidev,
1250 I915_PTEADDR, &gtt_addr);
1251 intel_private.gtt_bus_addr = gtt_addr;
1252 } else {
1253 u32 gtt_offset;
1254
1255 switch (INTEL_GTT_GEN) {
1256 case 5:
1257 case 6:
1258 gtt_offset = MB(2);
1259 break;
1260 case 4:
1261 default:
1262 gtt_offset = KB(512);
1263 break;
1264 }
1265 intel_private.gtt_bus_addr = reg_addr + gtt_offset;
1266 }
1267
Chris Wilson3f08e4e2010-09-14 20:15:22 +01001268 intel_private.pte_bus_addr =
1269 readl(intel_private.registers+I810_PGETBL_CTL) & 0xfffff000;
1270
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001271 intel_i9xx_setup_flush();
1272
1273 return 0;
1274}
1275
Daniel Vetterf51b7662010-04-14 00:29:52 +02001276static const struct agp_bridge_driver intel_810_driver = {
1277 .owner = THIS_MODULE,
1278 .aperture_sizes = intel_i810_sizes,
1279 .size_type = FIXED_APER_SIZE,
1280 .num_aperture_sizes = 2,
1281 .needs_scratch_page = true,
1282 .configure = intel_i810_configure,
1283 .fetch_size = intel_i810_fetch_size,
1284 .cleanup = intel_i810_cleanup,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001285 .mask_memory = intel_i810_mask_memory,
1286 .masks = intel_i810_masks,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001287 .agp_enable = intel_fake_agp_enable,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001288 .cache_flush = global_cache_flush,
1289 .create_gatt_table = agp_generic_create_gatt_table,
1290 .free_gatt_table = agp_generic_free_gatt_table,
1291 .insert_memory = intel_i810_insert_entries,
1292 .remove_memory = intel_i810_remove_entries,
1293 .alloc_by_type = intel_i810_alloc_by_type,
1294 .free_by_type = intel_i810_free_by_type,
1295 .agp_alloc_page = agp_generic_alloc_page,
1296 .agp_alloc_pages = agp_generic_alloc_pages,
1297 .agp_destroy_page = agp_generic_destroy_page,
1298 .agp_destroy_pages = agp_generic_destroy_pages,
1299 .agp_type_to_mask_type = agp_generic_type_to_mask_type,
1300};
1301
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001302static const struct agp_bridge_driver intel_fake_agp_driver = {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001303 .owner = THIS_MODULE,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001304 .size_type = FIXED_APER_SIZE,
Chris Wilson9e76e7b2010-09-14 12:12:11 +01001305 .aperture_sizes = intel_fake_agp_sizes,
1306 .num_aperture_sizes = ARRAY_SIZE(intel_fake_agp_sizes),
Daniel Vettera6963592010-09-11 14:01:43 +02001307 .configure = intel_fake_agp_configure,
Daniel Vetter3e921f92010-08-27 15:33:26 +02001308 .fetch_size = intel_fake_agp_fetch_size,
Daniel Vetterfdfb58a2010-08-29 00:15:03 +02001309 .cleanup = intel_gtt_cleanup,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001310 .agp_enable = intel_fake_agp_enable,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001311 .cache_flush = global_cache_flush,
Daniel Vetter3b15a9d2010-08-29 14:18:49 +02001312 .create_gatt_table = intel_fake_agp_create_gatt_table,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001313 .free_gatt_table = intel_fake_agp_free_gatt_table,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001314 .insert_memory = intel_fake_agp_insert_entries,
1315 .remove_memory = intel_fake_agp_remove_entries,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001316 .alloc_by_type = intel_fake_agp_alloc_by_type,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001317 .free_by_type = intel_i810_free_by_type,
1318 .agp_alloc_page = agp_generic_alloc_page,
1319 .agp_alloc_pages = agp_generic_alloc_pages,
1320 .agp_destroy_page = agp_generic_destroy_page,
1321 .agp_destroy_pages = agp_generic_destroy_pages,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001322 .chipset_flush = intel_fake_agp_chipset_flush,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001323};
Daniel Vetter02c026c2010-08-24 19:39:48 +02001324
Daniel Vetterbdd30722010-09-12 12:34:44 +02001325static const struct intel_gtt_driver i81x_gtt_driver = {
1326 .gen = 1,
Daniel Vetter22533b42010-09-12 16:38:55 +02001327 .dma_mask_size = 32,
Daniel Vetterbdd30722010-09-12 12:34:44 +02001328};
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001329static const struct intel_gtt_driver i8xx_gtt_driver = {
1330 .gen = 2,
Daniel Vetter73800422010-08-29 17:29:50 +02001331 .setup = i830_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001332 .cleanup = i830_cleanup,
Daniel Vetter351bb272010-09-07 22:41:04 +02001333 .write_entry = i830_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001334 .dma_mask_size = 32,
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001335 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001336 .chipset_flush = i830_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001337};
1338static const struct intel_gtt_driver i915_gtt_driver = {
1339 .gen = 3,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001340 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001341 .cleanup = i9xx_cleanup,
Daniel Vetter351bb272010-09-07 22:41:04 +02001342 /* i945 is the last gpu to need phys mem (for overlay and cursors). */
1343 .write_entry = i830_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001344 .dma_mask_size = 32,
Daniel Vetterfefaa702010-09-11 22:12:11 +02001345 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001346 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001347};
1348static const struct intel_gtt_driver g33_gtt_driver = {
1349 .gen = 3,
1350 .is_g33 = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001351 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001352 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001353 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001354 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001355 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001356 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001357};
1358static const struct intel_gtt_driver pineview_gtt_driver = {
1359 .gen = 3,
1360 .is_pineview = 1, .is_g33 = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001361 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001362 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001363 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001364 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001365 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001366 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001367};
1368static const struct intel_gtt_driver i965_gtt_driver = {
1369 .gen = 4,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001370 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001371 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001372 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001373 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001374 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001375 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001376};
1377static const struct intel_gtt_driver g4x_gtt_driver = {
1378 .gen = 5,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001379 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001380 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001381 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001382 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001383 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001384 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001385};
1386static const struct intel_gtt_driver ironlake_gtt_driver = {
1387 .gen = 5,
1388 .is_ironlake = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001389 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001390 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001391 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001392 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001393 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001394 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001395};
1396static const struct intel_gtt_driver sandybridge_gtt_driver = {
1397 .gen = 6,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001398 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001399 .cleanup = gen6_cleanup,
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001400 .write_entry = gen6_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001401 .dma_mask_size = 40,
Daniel Vetter90cb1492010-09-11 23:55:20 +02001402 .check_flags = gen6_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001403 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001404};
1405
Daniel Vetter02c026c2010-08-24 19:39:48 +02001406/* Table to describe Intel GMCH and AGP/PCIE GART drivers. At least one of
1407 * driver and gmch_driver must be non-null, and find_gmch will determine
1408 * which one should be used if a gmch_chip_id is present.
1409 */
1410static const struct intel_gtt_driver_description {
1411 unsigned int gmch_chip_id;
1412 char *name;
1413 const struct agp_bridge_driver *gmch_driver;
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001414 const struct intel_gtt_driver *gtt_driver;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001415} intel_gtt_chipsets[] = {
Daniel Vetterbdd30722010-09-12 12:34:44 +02001416 { PCI_DEVICE_ID_INTEL_82810_IG1, "i810", &intel_810_driver,
1417 &i81x_gtt_driver},
1418 { PCI_DEVICE_ID_INTEL_82810_IG3, "i810", &intel_810_driver,
1419 &i81x_gtt_driver},
1420 { PCI_DEVICE_ID_INTEL_82810E_IG, "i810", &intel_810_driver,
1421 &i81x_gtt_driver},
1422 { PCI_DEVICE_ID_INTEL_82815_CGC, "i815", &intel_810_driver,
1423 &i81x_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001424 { PCI_DEVICE_ID_INTEL_82830_CGC, "830M",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001425 &intel_fake_agp_driver, &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001426 { PCI_DEVICE_ID_INTEL_82845G_IG, "830M",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001427 &intel_fake_agp_driver, &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001428 { PCI_DEVICE_ID_INTEL_82854_IG, "854",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001429 &intel_fake_agp_driver, &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001430 { PCI_DEVICE_ID_INTEL_82855GM_IG, "855GM",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001431 &intel_fake_agp_driver, &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001432 { PCI_DEVICE_ID_INTEL_82865_IG, "865",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001433 &intel_fake_agp_driver, &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001434 { PCI_DEVICE_ID_INTEL_E7221_IG, "E7221 (i915)",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001435 &intel_fake_agp_driver, &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001436 { PCI_DEVICE_ID_INTEL_82915G_IG, "915G",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001437 &intel_fake_agp_driver, &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001438 { PCI_DEVICE_ID_INTEL_82915GM_IG, "915GM",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001439 &intel_fake_agp_driver, &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001440 { PCI_DEVICE_ID_INTEL_82945G_IG, "945G",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001441 &intel_fake_agp_driver, &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001442 { PCI_DEVICE_ID_INTEL_82945GM_IG, "945GM",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001443 &intel_fake_agp_driver, &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001444 { PCI_DEVICE_ID_INTEL_82945GME_IG, "945GME",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001445 &intel_fake_agp_driver, &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001446 { PCI_DEVICE_ID_INTEL_82946GZ_IG, "946GZ",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001447 &intel_fake_agp_driver, &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001448 { PCI_DEVICE_ID_INTEL_82G35_IG, "G35",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001449 &intel_fake_agp_driver, &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001450 { PCI_DEVICE_ID_INTEL_82965Q_IG, "965Q",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001451 &intel_fake_agp_driver, &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001452 { PCI_DEVICE_ID_INTEL_82965G_IG, "965G",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001453 &intel_fake_agp_driver, &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001454 { PCI_DEVICE_ID_INTEL_82965GM_IG, "965GM",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001455 &intel_fake_agp_driver, &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001456 { PCI_DEVICE_ID_INTEL_82965GME_IG, "965GME/GLE",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001457 &intel_fake_agp_driver, &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001458 { PCI_DEVICE_ID_INTEL_G33_IG, "G33",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001459 &intel_fake_agp_driver, &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001460 { PCI_DEVICE_ID_INTEL_Q35_IG, "Q35",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001461 &intel_fake_agp_driver, &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001462 { PCI_DEVICE_ID_INTEL_Q33_IG, "Q33",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001463 &intel_fake_agp_driver, &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001464 { PCI_DEVICE_ID_INTEL_PINEVIEW_M_IG, "GMA3150",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001465 &intel_fake_agp_driver, &pineview_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001466 { PCI_DEVICE_ID_INTEL_PINEVIEW_IG, "GMA3150",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001467 &intel_fake_agp_driver, &pineview_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001468 { PCI_DEVICE_ID_INTEL_GM45_IG, "GM45",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001469 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001470 { PCI_DEVICE_ID_INTEL_EAGLELAKE_IG, "Eaglelake",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001471 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001472 { PCI_DEVICE_ID_INTEL_Q45_IG, "Q45/Q43",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001473 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001474 { PCI_DEVICE_ID_INTEL_G45_IG, "G45/G43",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001475 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001476 { PCI_DEVICE_ID_INTEL_B43_IG, "B43",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001477 &intel_fake_agp_driver, &g4x_gtt_driver },
Chris Wilsone9e5f8e2010-09-21 11:19:32 +01001478 { PCI_DEVICE_ID_INTEL_B43_1_IG, "B43",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001479 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001480 { PCI_DEVICE_ID_INTEL_G41_IG, "G41",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001481 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001482 { PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001483 "HD Graphics", &intel_fake_agp_driver, &ironlake_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001484 { PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001485 "HD Graphics", &intel_fake_agp_driver, &ironlake_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001486 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT1_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001487 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001488 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001489 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001490 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_PLUS_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001491 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001492 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT1_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001493 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001494 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001495 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001496 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_PLUS_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001497 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001498 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001499 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001500 { 0, NULL, NULL }
1501};
1502
1503static int find_gmch(u16 device)
1504{
1505 struct pci_dev *gmch_device;
1506
1507 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL, device, NULL);
1508 if (gmch_device && PCI_FUNC(gmch_device->devfn) != 0) {
1509 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL,
1510 device, gmch_device);
1511 }
1512
1513 if (!gmch_device)
1514 return 0;
1515
1516 intel_private.pcidev = gmch_device;
1517 return 1;
1518}
1519
Daniel Vettere2404e72010-09-08 17:29:51 +02001520int intel_gmch_probe(struct pci_dev *pdev,
Daniel Vetter02c026c2010-08-24 19:39:48 +02001521 struct agp_bridge_data *bridge)
1522{
1523 int i, mask;
1524 bridge->driver = NULL;
1525
1526 for (i = 0; intel_gtt_chipsets[i].name != NULL; i++) {
1527 if (find_gmch(intel_gtt_chipsets[i].gmch_chip_id)) {
1528 bridge->driver =
1529 intel_gtt_chipsets[i].gmch_driver;
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001530 intel_private.driver =
1531 intel_gtt_chipsets[i].gtt_driver;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001532 break;
1533 }
1534 }
1535
1536 if (!bridge->driver)
1537 return 0;
1538
1539 bridge->dev_private_data = &intel_private;
1540 bridge->dev = pdev;
1541
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001542 intel_private.bridge_dev = pci_dev_get(pdev);
1543
Daniel Vetter02c026c2010-08-24 19:39:48 +02001544 dev_info(&pdev->dev, "Intel %s Chipset\n", intel_gtt_chipsets[i].name);
1545
Daniel Vetter22533b42010-09-12 16:38:55 +02001546 mask = intel_private.driver->dma_mask_size;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001547 if (pci_set_dma_mask(intel_private.pcidev, DMA_BIT_MASK(mask)))
1548 dev_err(&intel_private.pcidev->dev,
1549 "set gfx device dma mask %d-bit failed!\n", mask);
1550 else
1551 pci_set_consistent_dma_mask(intel_private.pcidev,
1552 DMA_BIT_MASK(mask));
1553
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001554 if (bridge->driver == &intel_810_driver)
1555 return 1;
1556
Daniel Vetter3b15a9d2010-08-29 14:18:49 +02001557 if (intel_gtt_init() != 0)
1558 return 0;
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001559
Daniel Vetter02c026c2010-08-24 19:39:48 +02001560 return 1;
1561}
Daniel Vettere2404e72010-09-08 17:29:51 +02001562EXPORT_SYMBOL(intel_gmch_probe);
Daniel Vetter02c026c2010-08-24 19:39:48 +02001563
Daniel Vetter19966752010-09-06 20:08:44 +02001564struct intel_gtt *intel_gtt_get(void)
1565{
1566 return &intel_private.base;
1567}
1568EXPORT_SYMBOL(intel_gtt_get);
1569
Daniel Vettere2404e72010-09-08 17:29:51 +02001570void intel_gmch_remove(struct pci_dev *pdev)
Daniel Vetter02c026c2010-08-24 19:39:48 +02001571{
1572 if (intel_private.pcidev)
1573 pci_dev_put(intel_private.pcidev);
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001574 if (intel_private.bridge_dev)
1575 pci_dev_put(intel_private.bridge_dev);
Daniel Vetter02c026c2010-08-24 19:39:48 +02001576}
Daniel Vettere2404e72010-09-08 17:29:51 +02001577EXPORT_SYMBOL(intel_gmch_remove);
1578
1579MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
1580MODULE_LICENSE("GPL and additional rights");