blob: 8a30a16c5706a7898de758e32c2f01d79c763e03 [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
42static const struct aper_size_info_fixed intel_i810_sizes[] =
43{
44 {64, 16384, 4},
45 /* The 32M mode still requires a 64k gatt */
46 {32, 8192, 4}
47};
48
49#define AGP_DCACHE_MEMORY 1
50#define AGP_PHYS_MEMORY 2
51#define INTEL_AGP_CACHED_MEMORY 3
52
53static struct gatt_mask intel_i810_masks[] =
54{
55 {.mask = I810_PTE_VALID, .type = 0},
56 {.mask = (I810_PTE_VALID | I810_PTE_LOCAL), .type = AGP_DCACHE_MEMORY},
57 {.mask = I810_PTE_VALID, .type = 0},
58 {.mask = I810_PTE_VALID | I830_PTE_SYSTEM_CACHED,
59 .type = INTEL_AGP_CACHED_MEMORY}
60};
61
Zhenyu Wangf8f235e2010-08-27 11:08:57 +080062#define INTEL_AGP_UNCACHED_MEMORY 0
63#define INTEL_AGP_CACHED_MEMORY_LLC 1
64#define INTEL_AGP_CACHED_MEMORY_LLC_GFDT 2
65#define INTEL_AGP_CACHED_MEMORY_LLC_MLC 3
66#define INTEL_AGP_CACHED_MEMORY_LLC_MLC_GFDT 4
67
Daniel Vetter1a997ff2010-09-08 21:18:53 +020068struct intel_gtt_driver {
69 unsigned int gen : 8;
70 unsigned int is_g33 : 1;
71 unsigned int is_pineview : 1;
72 unsigned int is_ironlake : 1;
Chris Wilson100519e2010-10-31 10:37:02 +000073 unsigned int has_pgtbl_enable : 1;
Daniel Vetter22533b42010-09-12 16:38:55 +020074 unsigned int dma_mask_size : 8;
Daniel Vetter73800422010-08-29 17:29:50 +020075 /* Chipset specific GTT setup */
76 int (*setup)(void);
Daniel Vetterae83dd52010-09-12 17:11:15 +020077 /* This should undo anything done in ->setup() save the unmapping
78 * of the mmio register file, that's done in the generic code. */
79 void (*cleanup)(void);
Daniel Vetter351bb272010-09-07 22:41:04 +020080 void (*write_entry)(dma_addr_t addr, unsigned int entry, unsigned int flags);
81 /* Flags is a more or less chipset specific opaque value.
82 * For chipsets that need to support old ums (non-gem) code, this
83 * needs to be identical to the various supported agp memory types! */
Daniel Vetter5cbecaf2010-09-11 21:31:04 +020084 bool (*check_flags)(unsigned int flags);
Daniel Vetter1b263f22010-09-12 00:27:24 +020085 void (*chipset_flush)(void);
Daniel Vetter1a997ff2010-09-08 21:18:53 +020086};
87
Daniel Vetterf51b7662010-04-14 00:29:52 +020088static struct _intel_private {
Daniel Vetter0ade6382010-08-24 22:18:41 +020089 struct intel_gtt base;
Daniel Vetter1a997ff2010-09-08 21:18:53 +020090 const struct intel_gtt_driver *driver;
Daniel Vetterf51b7662010-04-14 00:29:52 +020091 struct pci_dev *pcidev; /* device one */
Daniel Vetterd7cca2f2010-08-24 23:06:19 +020092 struct pci_dev *bridge_dev;
Daniel Vetterf51b7662010-04-14 00:29:52 +020093 u8 __iomem *registers;
Daniel Vetterf67eab62010-08-29 17:27:36 +020094 phys_addr_t gtt_bus_addr;
Daniel Vetter73800422010-08-29 17:29:50 +020095 phys_addr_t gma_bus_addr;
Daniel Vetterb3eafc52010-09-23 20:04:17 +020096 u32 PGETBL_save;
Daniel Vetterf51b7662010-04-14 00:29:52 +020097 u32 __iomem *gtt; /* I915G */
98 int num_dcache_entries;
Daniel Vetterf51b7662010-04-14 00:29:52 +020099 union {
100 void __iomem *i9xx_flush_page;
101 void *i8xx_flush_page;
102 };
103 struct page *i8xx_page;
104 struct resource ifp_resource;
105 int resource_valid;
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200106 struct page *scratch_page;
107 dma_addr_t scratch_page_dma;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200108} intel_private;
109
Daniel Vetter1a997ff2010-09-08 21:18:53 +0200110#define INTEL_GTT_GEN intel_private.driver->gen
111#define IS_G33 intel_private.driver->is_g33
112#define IS_PINEVIEW intel_private.driver->is_pineview
113#define IS_IRONLAKE intel_private.driver->is_ironlake
Chris Wilson100519e2010-10-31 10:37:02 +0000114#define HAS_PGTBL_EN intel_private.driver->has_pgtbl_enable
Daniel Vetter1a997ff2010-09-08 21:18:53 +0200115
Daniel Vetterf51b7662010-04-14 00:29:52 +0200116static 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
Daniel Vetterfefaa702010-09-11 22:12:11 +0200135 if (mem->sg_list)
136 return 0; /* already mapped (for e.g. resume */
137
Daniel Vetterf51b7662010-04-14 00:29:52 +0200138 DBG("try mapping %lu pages\n", (unsigned long)mem->page_count);
139
140 if (sg_alloc_table(&st, mem->page_count, GFP_KERNEL))
Chris Wilson831cd442010-07-24 18:29:37 +0100141 goto err;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200142
143 mem->sg_list = sg = st.sgl;
144
145 for (i = 0 ; i < mem->page_count; i++, sg = sg_next(sg))
146 sg_set_page(sg, mem->pages[i], PAGE_SIZE, 0);
147
148 mem->num_sg = pci_map_sg(intel_private.pcidev, mem->sg_list,
149 mem->page_count, PCI_DMA_BIDIRECTIONAL);
Chris Wilson831cd442010-07-24 18:29:37 +0100150 if (unlikely(!mem->num_sg))
151 goto err;
152
Daniel Vetterf51b7662010-04-14 00:29:52 +0200153 return 0;
Chris Wilson831cd442010-07-24 18:29:37 +0100154
155err:
156 sg_free_table(&st);
157 return -ENOMEM;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200158}
159
160static void intel_agp_unmap_memory(struct agp_memory *mem)
161{
162 DBG("try unmapping %lu pages\n", (unsigned long)mem->page_count);
163
164 pci_unmap_sg(intel_private.pcidev, mem->sg_list,
165 mem->page_count, PCI_DMA_BIDIRECTIONAL);
166 intel_agp_free_sglist(mem);
167}
168
Daniel Vetterf51b7662010-04-14 00:29:52 +0200169static int intel_i810_fetch_size(void)
170{
171 u32 smram_miscc;
172 struct aper_size_info_fixed *values;
173
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200174 pci_read_config_dword(intel_private.bridge_dev,
175 I810_SMRAM_MISCC, &smram_miscc);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200176 values = A_SIZE_FIX(agp_bridge->driver->aperture_sizes);
177
178 if ((smram_miscc & I810_GMS) == I810_GMS_DISABLE) {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200179 dev_warn(&intel_private.bridge_dev->dev, "i810 is disabled\n");
Daniel Vetterf51b7662010-04-14 00:29:52 +0200180 return 0;
181 }
182 if ((smram_miscc & I810_GFX_MEM_WIN_SIZE) == I810_GFX_MEM_WIN_32M) {
Daniel Vettere1583162010-04-14 00:29:58 +0200183 agp_bridge->current_size = (void *) (values + 1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200184 agp_bridge->aperture_size_idx = 1;
185 return values[1].size;
186 } else {
Daniel Vettere1583162010-04-14 00:29:58 +0200187 agp_bridge->current_size = (void *) (values);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200188 agp_bridge->aperture_size_idx = 0;
189 return values[0].size;
190 }
191
192 return 0;
193}
194
195static int intel_i810_configure(void)
196{
197 struct aper_size_info_fixed *current_size;
198 u32 temp;
199 int i;
200
201 current_size = A_SIZE_FIX(agp_bridge->current_size);
202
203 if (!intel_private.registers) {
204 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &temp);
205 temp &= 0xfff80000;
206
207 intel_private.registers = ioremap(temp, 128 * 4096);
208 if (!intel_private.registers) {
209 dev_err(&intel_private.pcidev->dev,
210 "can't remap memory\n");
211 return -ENOMEM;
212 }
213 }
214
215 if ((readl(intel_private.registers+I810_DRAM_CTL)
216 & I810_DRAM_ROW_0) == I810_DRAM_ROW_0_SDRAM) {
217 /* This will need to be dynamically assigned */
218 dev_info(&intel_private.pcidev->dev,
219 "detected 4MB dedicated video ram\n");
220 intel_private.num_dcache_entries = 1024;
221 }
222 pci_read_config_dword(intel_private.pcidev, I810_GMADDR, &temp);
223 agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
224 writel(agp_bridge->gatt_bus_addr | I810_PGETBL_ENABLED, intel_private.registers+I810_PGETBL_CTL);
225 readl(intel_private.registers+I810_PGETBL_CTL); /* PCI Posting. */
226
227 if (agp_bridge->driver->needs_scratch_page) {
228 for (i = 0; i < current_size->num_entries; i++) {
229 writel(agp_bridge->scratch_page, intel_private.registers+I810_PTE_BASE+(i*4));
230 }
231 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4)); /* PCI posting. */
232 }
233 global_cache_flush();
234 return 0;
235}
236
237static void intel_i810_cleanup(void)
238{
239 writel(0, intel_private.registers+I810_PGETBL_CTL);
240 readl(intel_private.registers); /* PCI Posting. */
241 iounmap(intel_private.registers);
242}
243
Daniel Vetterffdd7512010-08-27 17:51:29 +0200244static void intel_fake_agp_enable(struct agp_bridge_data *bridge, u32 mode)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200245{
246 return;
247}
248
249/* Exists to support ARGB cursors */
250static struct page *i8xx_alloc_pages(void)
251{
252 struct page *page;
253
254 page = alloc_pages(GFP_KERNEL | GFP_DMA32, 2);
255 if (page == NULL)
256 return NULL;
257
258 if (set_pages_uc(page, 4) < 0) {
259 set_pages_wb(page, 4);
260 __free_pages(page, 2);
261 return NULL;
262 }
263 get_page(page);
264 atomic_inc(&agp_bridge->current_memory_agp);
265 return page;
266}
267
268static void i8xx_destroy_pages(struct page *page)
269{
270 if (page == NULL)
271 return;
272
273 set_pages_wb(page, 4);
274 put_page(page);
275 __free_pages(page, 2);
276 atomic_dec(&agp_bridge->current_memory_agp);
277}
278
Daniel Vetterf51b7662010-04-14 00:29:52 +0200279static int intel_i810_insert_entries(struct agp_memory *mem, off_t pg_start,
280 int type)
281{
282 int i, j, num_entries;
283 void *temp;
284 int ret = -EINVAL;
285 int mask_type;
286
287 if (mem->page_count == 0)
288 goto out;
289
290 temp = agp_bridge->current_size;
291 num_entries = A_SIZE_FIX(temp)->num_entries;
292
293 if ((pg_start + mem->page_count) > num_entries)
294 goto out_err;
295
296
297 for (j = pg_start; j < (pg_start + mem->page_count); j++) {
298 if (!PGE_EMPTY(agp_bridge, readl(agp_bridge->gatt_table+j))) {
299 ret = -EBUSY;
300 goto out_err;
301 }
302 }
303
304 if (type != mem->type)
305 goto out_err;
306
307 mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type);
308
309 switch (mask_type) {
310 case AGP_DCACHE_MEMORY:
311 if (!mem->is_flushed)
312 global_cache_flush();
313 for (i = pg_start; i < (pg_start + mem->page_count); i++) {
314 writel((i*4096)|I810_PTE_LOCAL|I810_PTE_VALID,
315 intel_private.registers+I810_PTE_BASE+(i*4));
316 }
317 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4));
318 break;
319 case AGP_PHYS_MEMORY:
320 case AGP_NORMAL_MEMORY:
321 if (!mem->is_flushed)
322 global_cache_flush();
323 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
324 writel(agp_bridge->driver->mask_memory(agp_bridge,
325 page_to_phys(mem->pages[i]), mask_type),
326 intel_private.registers+I810_PTE_BASE+(j*4));
327 }
328 readl(intel_private.registers+I810_PTE_BASE+((j-1)*4));
329 break;
330 default:
331 goto out_err;
332 }
333
Daniel Vetterf51b7662010-04-14 00:29:52 +0200334out:
335 ret = 0;
336out_err:
337 mem->is_flushed = true;
338 return ret;
339}
340
341static int intel_i810_remove_entries(struct agp_memory *mem, off_t pg_start,
342 int type)
343{
344 int i;
345
346 if (mem->page_count == 0)
347 return 0;
348
349 for (i = pg_start; i < (mem->page_count + pg_start); i++) {
350 writel(agp_bridge->scratch_page, intel_private.registers+I810_PTE_BASE+(i*4));
351 }
352 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4));
353
Daniel Vetterf51b7662010-04-14 00:29:52 +0200354 return 0;
355}
356
357/*
358 * The i810/i830 requires a physical address to program its mouse
359 * pointer into hardware.
360 * However the Xserver still writes to it through the agp aperture.
361 */
362static struct agp_memory *alloc_agpphysmem_i8xx(size_t pg_count, int type)
363{
364 struct agp_memory *new;
365 struct page *page;
366
367 switch (pg_count) {
368 case 1: page = agp_bridge->driver->agp_alloc_page(agp_bridge);
369 break;
370 case 4:
371 /* kludge to get 4 physical pages for ARGB cursor */
372 page = i8xx_alloc_pages();
373 break;
374 default:
375 return NULL;
376 }
377
378 if (page == NULL)
379 return NULL;
380
381 new = agp_create_memory(pg_count);
382 if (new == NULL)
383 return NULL;
384
385 new->pages[0] = page;
386 if (pg_count == 4) {
387 /* kludge to get 4 physical pages for ARGB cursor */
388 new->pages[1] = new->pages[0] + 1;
389 new->pages[2] = new->pages[1] + 1;
390 new->pages[3] = new->pages[2] + 1;
391 }
392 new->page_count = pg_count;
393 new->num_scratch_pages = pg_count;
394 new->type = AGP_PHYS_MEMORY;
395 new->physical = page_to_phys(new->pages[0]);
396 return new;
397}
398
399static struct agp_memory *intel_i810_alloc_by_type(size_t pg_count, int type)
400{
401 struct agp_memory *new;
402
403 if (type == AGP_DCACHE_MEMORY) {
404 if (pg_count != intel_private.num_dcache_entries)
405 return NULL;
406
407 new = agp_create_memory(1);
408 if (new == NULL)
409 return NULL;
410
411 new->type = AGP_DCACHE_MEMORY;
412 new->page_count = pg_count;
413 new->num_scratch_pages = 0;
414 agp_free_page_array(new);
415 return new;
416 }
417 if (type == AGP_PHYS_MEMORY)
418 return alloc_agpphysmem_i8xx(pg_count, type);
419 return NULL;
420}
421
422static void intel_i810_free_by_type(struct agp_memory *curr)
423{
424 agp_free_key(curr->key);
425 if (curr->type == AGP_PHYS_MEMORY) {
426 if (curr->page_count == 4)
427 i8xx_destroy_pages(curr->pages[0]);
428 else {
429 agp_bridge->driver->agp_destroy_page(curr->pages[0],
430 AGP_PAGE_DESTROY_UNMAP);
431 agp_bridge->driver->agp_destroy_page(curr->pages[0],
432 AGP_PAGE_DESTROY_FREE);
433 }
434 agp_free_page_array(curr);
435 }
436 kfree(curr);
437}
438
439static unsigned long intel_i810_mask_memory(struct agp_bridge_data *bridge,
440 dma_addr_t addr, int type)
441{
442 /* Type checking must be done elsewhere */
443 return addr | bridge->driver->masks[type].mask;
444}
445
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200446static int intel_gtt_setup_scratch_page(void)
447{
448 struct page *page;
449 dma_addr_t dma_addr;
450
451 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
452 if (page == NULL)
453 return -ENOMEM;
454 get_page(page);
455 set_pages_uc(page, 1);
456
457 if (USE_PCI_DMA_API && INTEL_GTT_GEN > 2) {
458 dma_addr = pci_map_page(intel_private.pcidev, page, 0,
459 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
460 if (pci_dma_mapping_error(intel_private.pcidev, dma_addr))
461 return -EINVAL;
462
463 intel_private.scratch_page_dma = dma_addr;
464 } else
465 intel_private.scratch_page_dma = page_to_phys(page);
466
467 intel_private.scratch_page = page;
468
469 return 0;
470}
471
Chris Wilson9e76e7b82010-09-14 12:12:11 +0100472static const struct aper_size_info_fixed const intel_fake_agp_sizes[] = {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200473 {128, 32768, 5},
474 /* The 64M mode still requires a 128k gatt */
475 {64, 16384, 5},
476 {256, 65536, 6},
477 {512, 131072, 7},
478};
479
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000480static unsigned int intel_gtt_stolen_size(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200481{
482 u16 gmch_ctrl;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200483 u8 rdct;
484 int local = 0;
485 static const int ddt[4] = { 0, 16, 32, 64 };
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200486 unsigned int stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200487
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200488 pci_read_config_word(intel_private.bridge_dev,
489 I830_GMCH_CTRL, &gmch_ctrl);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200490
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200491 if (intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82830_HB ||
492 intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82845G_HB) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200493 switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
494 case I830_GMCH_GMS_STOLEN_512:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200495 stolen_size = KB(512);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200496 break;
497 case I830_GMCH_GMS_STOLEN_1024:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200498 stolen_size = MB(1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200499 break;
500 case I830_GMCH_GMS_STOLEN_8192:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200501 stolen_size = MB(8);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200502 break;
503 case I830_GMCH_GMS_LOCAL:
504 rdct = readb(intel_private.registers+I830_RDRAM_CHANNEL_TYPE);
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200505 stolen_size = (I830_RDRAM_ND(rdct) + 1) *
Daniel Vetterf51b7662010-04-14 00:29:52 +0200506 MB(ddt[I830_RDRAM_DDT(rdct)]);
507 local = 1;
508 break;
509 default:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200510 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200511 break;
512 }
Daniel Vetter1a997ff2010-09-08 21:18:53 +0200513 } else if (INTEL_GTT_GEN == 6) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200514 /*
515 * SandyBridge has new memory control reg at 0x50.w
516 */
517 u16 snb_gmch_ctl;
518 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl);
519 switch (snb_gmch_ctl & SNB_GMCH_GMS_STOLEN_MASK) {
520 case SNB_GMCH_GMS_STOLEN_32M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200521 stolen_size = MB(32);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200522 break;
523 case SNB_GMCH_GMS_STOLEN_64M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200524 stolen_size = MB(64);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200525 break;
526 case SNB_GMCH_GMS_STOLEN_96M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200527 stolen_size = MB(96);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200528 break;
529 case SNB_GMCH_GMS_STOLEN_128M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200530 stolen_size = MB(128);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200531 break;
532 case SNB_GMCH_GMS_STOLEN_160M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200533 stolen_size = MB(160);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200534 break;
535 case SNB_GMCH_GMS_STOLEN_192M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200536 stolen_size = MB(192);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200537 break;
538 case SNB_GMCH_GMS_STOLEN_224M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200539 stolen_size = MB(224);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200540 break;
541 case SNB_GMCH_GMS_STOLEN_256M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200542 stolen_size = MB(256);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200543 break;
544 case SNB_GMCH_GMS_STOLEN_288M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200545 stolen_size = MB(288);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200546 break;
547 case SNB_GMCH_GMS_STOLEN_320M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200548 stolen_size = MB(320);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200549 break;
550 case SNB_GMCH_GMS_STOLEN_352M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200551 stolen_size = MB(352);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200552 break;
553 case SNB_GMCH_GMS_STOLEN_384M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200554 stolen_size = MB(384);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200555 break;
556 case SNB_GMCH_GMS_STOLEN_416M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200557 stolen_size = MB(416);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200558 break;
559 case SNB_GMCH_GMS_STOLEN_448M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200560 stolen_size = MB(448);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200561 break;
562 case SNB_GMCH_GMS_STOLEN_480M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200563 stolen_size = MB(480);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200564 break;
565 case SNB_GMCH_GMS_STOLEN_512M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200566 stolen_size = MB(512);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200567 break;
568 }
569 } else {
570 switch (gmch_ctrl & I855_GMCH_GMS_MASK) {
571 case I855_GMCH_GMS_STOLEN_1M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200572 stolen_size = MB(1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200573 break;
574 case I855_GMCH_GMS_STOLEN_4M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200575 stolen_size = MB(4);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200576 break;
577 case I855_GMCH_GMS_STOLEN_8M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200578 stolen_size = MB(8);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200579 break;
580 case I855_GMCH_GMS_STOLEN_16M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200581 stolen_size = MB(16);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200582 break;
583 case I855_GMCH_GMS_STOLEN_32M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200584 stolen_size = MB(32);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200585 break;
586 case I915_GMCH_GMS_STOLEN_48M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200587 stolen_size = MB(48);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200588 break;
589 case I915_GMCH_GMS_STOLEN_64M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200590 stolen_size = MB(64);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200591 break;
592 case G33_GMCH_GMS_STOLEN_128M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200593 stolen_size = MB(128);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200594 break;
595 case G33_GMCH_GMS_STOLEN_256M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200596 stolen_size = MB(256);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200597 break;
598 case INTEL_GMCH_GMS_STOLEN_96M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200599 stolen_size = MB(96);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200600 break;
601 case INTEL_GMCH_GMS_STOLEN_160M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200602 stolen_size = MB(160);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200603 break;
604 case INTEL_GMCH_GMS_STOLEN_224M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200605 stolen_size = MB(224);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200606 break;
607 case INTEL_GMCH_GMS_STOLEN_352M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200608 stolen_size = MB(352);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200609 break;
610 default:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200611 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200612 break;
613 }
614 }
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200615
Chris Wilson1b6064d2010-11-23 12:33:54 +0000616 if (stolen_size > 0) {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200617 dev_info(&intel_private.bridge_dev->dev, "detected %dK %s memory\n",
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200618 stolen_size / KB(1), local ? "local" : "stolen");
Daniel Vetterf51b7662010-04-14 00:29:52 +0200619 } else {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200620 dev_info(&intel_private.bridge_dev->dev,
Daniel Vetterf51b7662010-04-14 00:29:52 +0200621 "no pre-allocated video memory detected\n");
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200622 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200623 }
624
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000625 return stolen_size;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200626}
627
Daniel Vetter20172842010-09-24 18:25:59 +0200628static void i965_adjust_pgetbl_size(unsigned int size_flag)
629{
630 u32 pgetbl_ctl, pgetbl_ctl2;
631
632 /* ensure that ppgtt is disabled */
633 pgetbl_ctl2 = readl(intel_private.registers+I965_PGETBL_CTL2);
634 pgetbl_ctl2 &= ~I810_PGETBL_ENABLED;
635 writel(pgetbl_ctl2, intel_private.registers+I965_PGETBL_CTL2);
636
637 /* write the new ggtt size */
638 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
639 pgetbl_ctl &= ~I965_PGETBL_SIZE_MASK;
640 pgetbl_ctl |= size_flag;
641 writel(pgetbl_ctl, intel_private.registers+I810_PGETBL_CTL);
642}
643
644static unsigned int i965_gtt_total_entries(void)
645{
646 int size;
647 u32 pgetbl_ctl;
648 u16 gmch_ctl;
649
650 pci_read_config_word(intel_private.bridge_dev,
651 I830_GMCH_CTRL, &gmch_ctl);
652
653 if (INTEL_GTT_GEN == 5) {
654 switch (gmch_ctl & G4x_GMCH_SIZE_MASK) {
655 case G4x_GMCH_SIZE_1M:
656 case G4x_GMCH_SIZE_VT_1M:
657 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1MB);
658 break;
659 case G4x_GMCH_SIZE_VT_1_5M:
660 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1_5MB);
661 break;
662 case G4x_GMCH_SIZE_2M:
663 case G4x_GMCH_SIZE_VT_2M:
664 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_2MB);
665 break;
666 }
667 }
668
669 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
670
671 switch (pgetbl_ctl & I965_PGETBL_SIZE_MASK) {
672 case I965_PGETBL_SIZE_128KB:
673 size = KB(128);
674 break;
675 case I965_PGETBL_SIZE_256KB:
676 size = KB(256);
677 break;
678 case I965_PGETBL_SIZE_512KB:
679 size = KB(512);
680 break;
681 /* GTT pagetable sizes bigger than 512KB are not possible on G33! */
682 case I965_PGETBL_SIZE_1MB:
683 size = KB(1024);
684 break;
685 case I965_PGETBL_SIZE_2MB:
686 size = KB(2048);
687 break;
688 case I965_PGETBL_SIZE_1_5MB:
689 size = KB(1024 + 512);
690 break;
691 default:
692 dev_info(&intel_private.pcidev->dev,
693 "unknown page table size, assuming 512KB\n");
694 size = KB(512);
695 }
696
697 return size/4;
698}
699
Daniel Vetterfbe40782010-08-27 17:12:41 +0200700static unsigned int intel_gtt_total_entries(void)
701{
702 int size;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200703
Daniel Vetter20172842010-09-24 18:25:59 +0200704 if (IS_G33 || INTEL_GTT_GEN == 4 || INTEL_GTT_GEN == 5)
705 return i965_gtt_total_entries();
706 else if (INTEL_GTT_GEN == 6) {
Daniel Vetter210b23c2010-08-28 16:14:32 +0200707 u16 snb_gmch_ctl;
708
709 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl);
710 switch (snb_gmch_ctl & SNB_GTT_SIZE_MASK) {
711 default:
712 case SNB_GTT_SIZE_0M:
713 printk(KERN_ERR "Bad GTT size mask: 0x%04x.\n", snb_gmch_ctl);
714 size = MB(0);
715 break;
716 case SNB_GTT_SIZE_1M:
717 size = MB(1);
718 break;
719 case SNB_GTT_SIZE_2M:
720 size = MB(2);
721 break;
722 }
723 return size/4;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200724 } else {
725 /* On previous hardware, the GTT size was just what was
726 * required to map the aperture.
727 */
Daniel Vettere5e408f2010-08-28 11:04:32 +0200728 return intel_private.base.gtt_mappable_entries;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200729 }
Daniel Vetterfbe40782010-08-27 17:12:41 +0200730}
Daniel Vetterfbe40782010-08-27 17:12:41 +0200731
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200732static unsigned int intel_gtt_mappable_entries(void)
733{
734 unsigned int aperture_size;
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200735
Daniel Vetter239918f2010-08-31 22:30:43 +0200736 if (INTEL_GTT_GEN == 2) {
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100737 u16 gmch_ctrl;
738
739 pci_read_config_word(intel_private.bridge_dev,
740 I830_GMCH_CTRL, &gmch_ctrl);
741
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200742 if ((gmch_ctrl & I830_GMCH_MEM_MASK) == I830_GMCH_MEM_64M)
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100743 aperture_size = MB(64);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200744 else
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100745 aperture_size = MB(128);
Daniel Vetter239918f2010-08-31 22:30:43 +0200746 } else {
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200747 /* 9xx supports large sizes, just look at the length */
748 aperture_size = pci_resource_len(intel_private.pcidev, 2);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200749 }
750
751 return aperture_size >> PAGE_SHIFT;
752}
753
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200754static void intel_gtt_teardown_scratch_page(void)
755{
756 set_pages_wb(intel_private.scratch_page, 1);
757 pci_unmap_page(intel_private.pcidev, intel_private.scratch_page_dma,
758 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
759 put_page(intel_private.scratch_page);
760 __free_page(intel_private.scratch_page);
761}
762
763static void intel_gtt_cleanup(void)
764{
Daniel Vetterae83dd52010-09-12 17:11:15 +0200765 intel_private.driver->cleanup();
766
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200767 iounmap(intel_private.gtt);
768 iounmap(intel_private.registers);
769
770 intel_gtt_teardown_scratch_page();
771}
772
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200773static int intel_gtt_init(void)
774{
Daniel Vetterf67eab62010-08-29 17:27:36 +0200775 u32 gtt_map_size;
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200776 int ret;
777
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200778 ret = intel_private.driver->setup();
779 if (ret != 0)
780 return ret;
Daniel Vetterf67eab62010-08-29 17:27:36 +0200781
782 intel_private.base.gtt_mappable_entries = intel_gtt_mappable_entries();
783 intel_private.base.gtt_total_entries = intel_gtt_total_entries();
784
Daniel Vetterb3eafc52010-09-23 20:04:17 +0200785 /* save the PGETBL reg for resume */
786 intel_private.PGETBL_save =
787 readl(intel_private.registers+I810_PGETBL_CTL)
788 & ~I810_PGETBL_ENABLED;
Chris Wilson100519e2010-10-31 10:37:02 +0000789 /* we only ever restore the register when enabling the PGTBL... */
790 if (HAS_PGTBL_EN)
791 intel_private.PGETBL_save |= I810_PGETBL_ENABLED;
Daniel Vetterb3eafc52010-09-23 20:04:17 +0200792
Daniel Vetter0af9e922010-09-12 14:04:03 +0200793 dev_info(&intel_private.bridge_dev->dev,
794 "detected gtt size: %dK total, %dK mappable\n",
795 intel_private.base.gtt_total_entries * 4,
796 intel_private.base.gtt_mappable_entries * 4);
797
Daniel Vetterf67eab62010-08-29 17:27:36 +0200798 gtt_map_size = intel_private.base.gtt_total_entries * 4;
799
800 intel_private.gtt = ioremap(intel_private.gtt_bus_addr,
801 gtt_map_size);
802 if (!intel_private.gtt) {
Daniel Vetterae83dd52010-09-12 17:11:15 +0200803 intel_private.driver->cleanup();
Daniel Vetterf67eab62010-08-29 17:27:36 +0200804 iounmap(intel_private.registers);
805 return -ENOMEM;
806 }
807
808 global_cache_flush(); /* FIXME: ? */
809
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200810 /* we have to call this as early as possible after the MMIO base address is known */
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000811 intel_private.base.stolen_size = intel_gtt_stolen_size();
812 if (intel_private.base.stolen_size == 0) {
Daniel Vetterae83dd52010-09-12 17:11:15 +0200813 intel_private.driver->cleanup();
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200814 iounmap(intel_private.registers);
Daniel Vetterf67eab62010-08-29 17:27:36 +0200815 iounmap(intel_private.gtt);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200816 return -ENOMEM;
817 }
818
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200819 ret = intel_gtt_setup_scratch_page();
820 if (ret != 0) {
821 intel_gtt_cleanup();
822 return ret;
823 }
824
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200825 return 0;
826}
827
Daniel Vetter3e921f92010-08-27 15:33:26 +0200828static int intel_fake_agp_fetch_size(void)
829{
Chris Wilson9e76e7b82010-09-14 12:12:11 +0100830 int num_sizes = ARRAY_SIZE(intel_fake_agp_sizes);
Daniel Vetter3e921f92010-08-27 15:33:26 +0200831 unsigned int aper_size;
832 int i;
Daniel Vetter3e921f92010-08-27 15:33:26 +0200833
834 aper_size = (intel_private.base.gtt_mappable_entries << PAGE_SHIFT)
835 / MB(1);
836
837 for (i = 0; i < num_sizes; i++) {
Daniel Vetterffdd7512010-08-27 17:51:29 +0200838 if (aper_size == intel_fake_agp_sizes[i].size) {
Chris Wilson9e76e7b82010-09-14 12:12:11 +0100839 agp_bridge->current_size =
840 (void *) (intel_fake_agp_sizes + i);
Daniel Vetter3e921f92010-08-27 15:33:26 +0200841 return aper_size;
842 }
843 }
844
845 return 0;
846}
847
Daniel Vetterae83dd52010-09-12 17:11:15 +0200848static void i830_cleanup(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200849{
850 kunmap(intel_private.i8xx_page);
851 intel_private.i8xx_flush_page = NULL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200852
853 __free_page(intel_private.i8xx_page);
854 intel_private.i8xx_page = NULL;
855}
856
857static void intel_i830_setup_flush(void)
858{
859 /* return if we've already set the flush mechanism up */
860 if (intel_private.i8xx_page)
861 return;
862
Jan Beuliche61cb0d2010-09-24 13:25:30 +0100863 intel_private.i8xx_page = alloc_page(GFP_KERNEL);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200864 if (!intel_private.i8xx_page)
865 return;
866
867 intel_private.i8xx_flush_page = kmap(intel_private.i8xx_page);
868 if (!intel_private.i8xx_flush_page)
Daniel Vetterae83dd52010-09-12 17:11:15 +0200869 i830_cleanup();
Daniel Vetterf51b7662010-04-14 00:29:52 +0200870}
871
872/* The chipset_flush interface needs to get data that has already been
873 * flushed out of the CPU all the way out to main memory, because the GPU
874 * doesn't snoop those buffers.
875 *
876 * The 8xx series doesn't have the same lovely interface for flushing the
877 * chipset write buffers that the later chips do. According to the 865
878 * specs, it's 64 octwords, or 1KB. So, to get those previous things in
879 * that buffer out, we just fill 1KB and clflush it out, on the assumption
880 * that it'll push whatever was in there out. It appears to work.
881 */
Daniel Vetter1b263f22010-09-12 00:27:24 +0200882static void i830_chipset_flush(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200883{
884 unsigned int *pg = intel_private.i8xx_flush_page;
885
886 memset(pg, 0, 1024);
887
888 if (cpu_has_clflush)
889 clflush_cache_range(pg, 1024);
890 else if (wbinvd_on_all_cpus() != 0)
891 printk(KERN_ERR "Timed out waiting for cache flush.\n");
892}
893
Daniel Vetter351bb272010-09-07 22:41:04 +0200894static void i830_write_entry(dma_addr_t addr, unsigned int entry,
895 unsigned int flags)
896{
897 u32 pte_flags = I810_PTE_VALID;
898
Daniel Vetterb47cf662010-11-04 18:41:50 +0100899 if (flags == AGP_USER_CACHED_MEMORY)
Daniel Vetter351bb272010-09-07 22:41:04 +0200900 pte_flags |= I830_PTE_SYSTEM_CACHED;
Daniel Vetter351bb272010-09-07 22:41:04 +0200901
902 writel(addr | pte_flags, intel_private.gtt + entry);
903}
904
Chris Wilsone380f602010-10-29 18:11:26 +0100905static bool intel_enable_gtt(void)
Daniel Vetter73800422010-08-29 17:29:50 +0200906{
Chris Wilson3f08e4e2010-09-14 20:15:22 +0100907 u32 gma_addr;
Chris Wilsone380f602010-10-29 18:11:26 +0100908 u8 __iomem *reg;
Daniel Vetter73800422010-08-29 17:29:50 +0200909
Daniel Vetter2d2430c2010-08-29 17:35:30 +0200910 if (INTEL_GTT_GEN == 2)
911 pci_read_config_dword(intel_private.pcidev, I810_GMADDR,
912 &gma_addr);
913 else
914 pci_read_config_dword(intel_private.pcidev, I915_GMADDR,
915 &gma_addr);
916
Daniel Vetter73800422010-08-29 17:29:50 +0200917 intel_private.gma_bus_addr = (gma_addr & PCI_BASE_ADDRESS_MEM_MASK);
918
Chris Wilsone380f602010-10-29 18:11:26 +0100919 if (INTEL_GTT_GEN >= 6)
920 return true;
Daniel Vetter73800422010-08-29 17:29:50 +0200921
Chris Wilson100519e2010-10-31 10:37:02 +0000922 if (INTEL_GTT_GEN == 2) {
923 u16 gmch_ctrl;
Chris Wilsone380f602010-10-29 18:11:26 +0100924
Chris Wilson100519e2010-10-31 10:37:02 +0000925 pci_read_config_word(intel_private.bridge_dev,
926 I830_GMCH_CTRL, &gmch_ctrl);
927 gmch_ctrl |= I830_GMCH_ENABLED;
928 pci_write_config_word(intel_private.bridge_dev,
929 I830_GMCH_CTRL, gmch_ctrl);
930
931 pci_read_config_word(intel_private.bridge_dev,
932 I830_GMCH_CTRL, &gmch_ctrl);
933 if ((gmch_ctrl & I830_GMCH_ENABLED) == 0) {
934 dev_err(&intel_private.pcidev->dev,
935 "failed to enable the GTT: GMCH_CTRL=%x\n",
936 gmch_ctrl);
937 return false;
938 }
Chris Wilsone380f602010-10-29 18:11:26 +0100939 }
940
941 reg = intel_private.registers+I810_PGETBL_CTL;
Chris Wilson100519e2010-10-31 10:37:02 +0000942 writel(intel_private.PGETBL_save, reg);
943 if (HAS_PGTBL_EN && (readl(reg) & I810_PGETBL_ENABLED) == 0) {
Chris Wilsone380f602010-10-29 18:11:26 +0100944 dev_err(&intel_private.pcidev->dev,
Chris Wilson100519e2010-10-31 10:37:02 +0000945 "failed to enable the GTT: PGETBL=%x [expected %x]\n",
Chris Wilsone380f602010-10-29 18:11:26 +0100946 readl(reg), intel_private.PGETBL_save);
947 return false;
948 }
949
950 return true;
Daniel Vetter73800422010-08-29 17:29:50 +0200951}
952
953static int i830_setup(void)
954{
955 u32 reg_addr;
956
957 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &reg_addr);
958 reg_addr &= 0xfff80000;
959
960 intel_private.registers = ioremap(reg_addr, KB(64));
961 if (!intel_private.registers)
962 return -ENOMEM;
963
964 intel_private.gtt_bus_addr = reg_addr + I810_PTE_BASE;
965
966 intel_i830_setup_flush();
967
968 return 0;
969}
970
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200971static int intel_fake_agp_create_gatt_table(struct agp_bridge_data *bridge)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200972{
Daniel Vetter73800422010-08-29 17:29:50 +0200973 agp_bridge->gatt_table_real = NULL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200974 agp_bridge->gatt_table = NULL;
Daniel Vetter73800422010-08-29 17:29:50 +0200975 agp_bridge->gatt_bus_addr = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200976
977 return 0;
978}
979
Daniel Vetterffdd7512010-08-27 17:51:29 +0200980static int intel_fake_agp_free_gatt_table(struct agp_bridge_data *bridge)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200981{
982 return 0;
983}
984
Daniel Vetter351bb272010-09-07 22:41:04 +0200985static int intel_fake_agp_configure(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200986{
Daniel Vetterf51b7662010-04-14 00:29:52 +0200987 int i;
988
Chris Wilsone380f602010-10-29 18:11:26 +0100989 if (!intel_enable_gtt())
990 return -EIO;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200991
Daniel Vetter73800422010-08-29 17:29:50 +0200992 agp_bridge->gart_bus_addr = intel_private.gma_bus_addr;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200993
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000994 for (i = 0; i < intel_private.base.gtt_total_entries; i++) {
Daniel Vetter351bb272010-09-07 22:41:04 +0200995 intel_private.driver->write_entry(intel_private.scratch_page_dma,
996 i, 0);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200997 }
Daniel Vetter351bb272010-09-07 22:41:04 +0200998 readl(intel_private.gtt+i-1); /* PCI Posting. */
Daniel Vetterf51b7662010-04-14 00:29:52 +0200999
1000 global_cache_flush();
1001
Daniel Vetterf51b7662010-04-14 00:29:52 +02001002 return 0;
1003}
1004
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001005static bool i830_check_flags(unsigned int flags)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001006{
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001007 switch (flags) {
1008 case 0:
1009 case AGP_PHYS_MEMORY:
1010 case AGP_USER_CACHED_MEMORY:
1011 case AGP_USER_MEMORY:
1012 return true;
1013 }
1014
1015 return false;
1016}
1017
Daniel Vetterfefaa702010-09-11 22:12:11 +02001018static void intel_gtt_insert_sg_entries(struct scatterlist *sg_list,
1019 unsigned int sg_len,
1020 unsigned int pg_start,
1021 unsigned int flags)
1022{
1023 struct scatterlist *sg;
1024 unsigned int len, m;
1025 int i, j;
1026
1027 j = pg_start;
1028
1029 /* sg may merge pages, but we have to separate
1030 * per-page addr for GTT */
1031 for_each_sg(sg_list, sg, sg_len, i) {
1032 len = sg_dma_len(sg) >> PAGE_SHIFT;
1033 for (m = 0; m < len; m++) {
1034 dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
1035 intel_private.driver->write_entry(addr,
1036 j, flags);
1037 j++;
1038 }
1039 }
1040 readl(intel_private.gtt+j-1);
1041}
1042
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001043static int intel_fake_agp_insert_entries(struct agp_memory *mem,
1044 off_t pg_start, int type)
1045{
1046 int i, j;
Daniel Vetterf51b7662010-04-14 00:29:52 +02001047 int ret = -EINVAL;
Daniel Vetterf51b7662010-04-14 00:29:52 +02001048
1049 if (mem->page_count == 0)
1050 goto out;
1051
Chris Wilsonc64f7ba2010-11-23 14:24:24 +00001052 if (pg_start + mem->page_count > intel_private.base.gtt_total_entries)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001053 goto out_err;
1054
Daniel Vetterf51b7662010-04-14 00:29:52 +02001055 if (type != mem->type)
1056 goto out_err;
1057
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001058 if (!intel_private.driver->check_flags(type))
Daniel Vetterf51b7662010-04-14 00:29:52 +02001059 goto out_err;
1060
1061 if (!mem->is_flushed)
1062 global_cache_flush();
1063
Daniel Vetterfefaa702010-09-11 22:12:11 +02001064 if (USE_PCI_DMA_API && INTEL_GTT_GEN > 2) {
1065 ret = intel_agp_map_memory(mem);
1066 if (ret != 0)
1067 return ret;
1068
1069 intel_gtt_insert_sg_entries(mem->sg_list, mem->num_sg,
1070 pg_start, type);
1071 } else {
1072 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
1073 dma_addr_t addr = page_to_phys(mem->pages[i]);
1074 intel_private.driver->write_entry(addr,
1075 j, type);
1076 }
1077 readl(intel_private.gtt+j-1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001078 }
Daniel Vetterf51b7662010-04-14 00:29:52 +02001079
1080out:
1081 ret = 0;
1082out_err:
1083 mem->is_flushed = true;
1084 return ret;
1085}
1086
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001087static int intel_fake_agp_remove_entries(struct agp_memory *mem,
1088 off_t pg_start, int type)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001089{
1090 int i;
1091
1092 if (mem->page_count == 0)
1093 return 0;
1094
Daniel Vetterfefaa702010-09-11 22:12:11 +02001095 if (USE_PCI_DMA_API && INTEL_GTT_GEN > 2)
1096 intel_agp_unmap_memory(mem);
1097
Daniel Vetterf51b7662010-04-14 00:29:52 +02001098 for (i = pg_start; i < (mem->page_count + pg_start); i++) {
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001099 intel_private.driver->write_entry(intel_private.scratch_page_dma,
1100 i, 0);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001101 }
Daniel Vetterfdfb58a2010-08-29 00:15:03 +02001102 readl(intel_private.gtt+i-1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001103
Daniel Vetterf51b7662010-04-14 00:29:52 +02001104 return 0;
1105}
1106
Daniel Vetter1b263f22010-09-12 00:27:24 +02001107static void intel_fake_agp_chipset_flush(struct agp_bridge_data *bridge)
1108{
1109 intel_private.driver->chipset_flush();
1110}
1111
Daniel Vetterffdd7512010-08-27 17:51:29 +02001112static struct agp_memory *intel_fake_agp_alloc_by_type(size_t pg_count,
1113 int type)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001114{
1115 if (type == AGP_PHYS_MEMORY)
1116 return alloc_agpphysmem_i8xx(pg_count, type);
1117 /* always return NULL for other allocation types for now */
1118 return NULL;
1119}
1120
1121static int intel_alloc_chipset_flush_resource(void)
1122{
1123 int ret;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001124 ret = pci_bus_alloc_resource(intel_private.bridge_dev->bus, &intel_private.ifp_resource, PAGE_SIZE,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001125 PAGE_SIZE, PCIBIOS_MIN_MEM, 0,
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001126 pcibios_align_resource, intel_private.bridge_dev);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001127
1128 return ret;
1129}
1130
1131static void intel_i915_setup_chipset_flush(void)
1132{
1133 int ret;
1134 u32 temp;
1135
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001136 pci_read_config_dword(intel_private.bridge_dev, I915_IFPADDR, &temp);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001137 if (!(temp & 0x1)) {
1138 intel_alloc_chipset_flush_resource();
1139 intel_private.resource_valid = 1;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001140 pci_write_config_dword(intel_private.bridge_dev, I915_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001141 } else {
1142 temp &= ~1;
1143
1144 intel_private.resource_valid = 1;
1145 intel_private.ifp_resource.start = temp;
1146 intel_private.ifp_resource.end = temp + PAGE_SIZE;
1147 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
1148 /* some BIOSes reserve this area in a pnp some don't */
1149 if (ret)
1150 intel_private.resource_valid = 0;
1151 }
1152}
1153
1154static void intel_i965_g33_setup_chipset_flush(void)
1155{
1156 u32 temp_hi, temp_lo;
1157 int ret;
1158
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001159 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4, &temp_hi);
1160 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR, &temp_lo);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001161
1162 if (!(temp_lo & 0x1)) {
1163
1164 intel_alloc_chipset_flush_resource();
1165
1166 intel_private.resource_valid = 1;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001167 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001168 upper_32_bits(intel_private.ifp_resource.start));
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001169 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001170 } else {
1171 u64 l64;
1172
1173 temp_lo &= ~0x1;
1174 l64 = ((u64)temp_hi << 32) | temp_lo;
1175
1176 intel_private.resource_valid = 1;
1177 intel_private.ifp_resource.start = l64;
1178 intel_private.ifp_resource.end = l64 + PAGE_SIZE;
1179 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
1180 /* some BIOSes reserve this area in a pnp some don't */
1181 if (ret)
1182 intel_private.resource_valid = 0;
1183 }
1184}
1185
1186static void intel_i9xx_setup_flush(void)
1187{
1188 /* return if already configured */
1189 if (intel_private.ifp_resource.start)
1190 return;
1191
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001192 if (INTEL_GTT_GEN == 6)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001193 return;
1194
1195 /* setup a resource for this object */
1196 intel_private.ifp_resource.name = "Intel Flush Page";
1197 intel_private.ifp_resource.flags = IORESOURCE_MEM;
1198
1199 /* Setup chipset flush for 915 */
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001200 if (IS_G33 || INTEL_GTT_GEN >= 4) {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001201 intel_i965_g33_setup_chipset_flush();
1202 } else {
1203 intel_i915_setup_chipset_flush();
1204 }
1205
Chris Wilsondf51e7a2010-09-04 14:57:27 +01001206 if (intel_private.ifp_resource.start)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001207 intel_private.i9xx_flush_page = ioremap_nocache(intel_private.ifp_resource.start, PAGE_SIZE);
Chris Wilsondf51e7a2010-09-04 14:57:27 +01001208 if (!intel_private.i9xx_flush_page)
1209 dev_err(&intel_private.pcidev->dev,
1210 "can't ioremap flush page - no chipset flushing\n");
Daniel Vetterf51b7662010-04-14 00:29:52 +02001211}
1212
Daniel Vetterae83dd52010-09-12 17:11:15 +02001213static void i9xx_cleanup(void)
1214{
1215 if (intel_private.i9xx_flush_page)
1216 iounmap(intel_private.i9xx_flush_page);
1217 if (intel_private.resource_valid)
1218 release_resource(&intel_private.ifp_resource);
1219 intel_private.ifp_resource.start = 0;
1220 intel_private.resource_valid = 0;
1221}
1222
Daniel Vetter1b263f22010-09-12 00:27:24 +02001223static void i9xx_chipset_flush(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001224{
1225 if (intel_private.i9xx_flush_page)
1226 writel(1, intel_private.i9xx_flush_page);
1227}
1228
Daniel Vettera6963592010-09-11 14:01:43 +02001229static void i965_write_entry(dma_addr_t addr, unsigned int entry,
1230 unsigned int flags)
1231{
1232 /* Shift high bits down */
1233 addr |= (addr >> 28) & 0xf0;
1234 writel(addr | I810_PTE_VALID, intel_private.gtt + entry);
1235}
1236
Daniel Vetter90cb1492010-09-11 23:55:20 +02001237static bool gen6_check_flags(unsigned int flags)
1238{
1239 return true;
1240}
1241
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001242static void gen6_write_entry(dma_addr_t addr, unsigned int entry,
1243 unsigned int flags)
1244{
1245 unsigned int type_mask = flags & ~AGP_USER_CACHED_MEMORY_GFDT;
1246 unsigned int gfdt = flags & AGP_USER_CACHED_MEMORY_GFDT;
1247 u32 pte_flags;
1248
Zhenyu Wang897ef192010-11-02 17:30:47 +08001249 if (type_mask == AGP_USER_MEMORY)
Chris Wilson85ccc352010-10-22 14:59:29 +01001250 pte_flags = GEN6_PTE_UNCACHED | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001251 else if (type_mask == AGP_USER_CACHED_MEMORY_LLC_MLC) {
Zhenyu Wangd1108522010-11-02 17:30:46 +08001252 pte_flags = GEN6_PTE_LLC_MLC | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001253 if (gfdt)
1254 pte_flags |= GEN6_PTE_GFDT;
1255 } else { /* set 'normal'/'cached' to LLC by default */
Zhenyu Wangd1108522010-11-02 17:30:46 +08001256 pte_flags = GEN6_PTE_LLC | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001257 if (gfdt)
1258 pte_flags |= GEN6_PTE_GFDT;
1259 }
1260
1261 /* gen6 has bit11-4 for physical addr bit39-32 */
1262 addr |= (addr >> 28) & 0xff0;
1263 writel(addr | pte_flags, intel_private.gtt + entry);
1264}
1265
Daniel Vetterae83dd52010-09-12 17:11:15 +02001266static void gen6_cleanup(void)
1267{
1268}
1269
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001270static int i9xx_setup(void)
1271{
1272 u32 reg_addr;
1273
1274 pci_read_config_dword(intel_private.pcidev, I915_MMADDR, &reg_addr);
1275
1276 reg_addr &= 0xfff80000;
1277
1278 intel_private.registers = ioremap(reg_addr, 128 * 4096);
1279 if (!intel_private.registers)
1280 return -ENOMEM;
1281
1282 if (INTEL_GTT_GEN == 3) {
1283 u32 gtt_addr;
Chris Wilson3f08e4e2010-09-14 20:15:22 +01001284
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001285 pci_read_config_dword(intel_private.pcidev,
1286 I915_PTEADDR, &gtt_addr);
1287 intel_private.gtt_bus_addr = gtt_addr;
1288 } else {
1289 u32 gtt_offset;
1290
1291 switch (INTEL_GTT_GEN) {
1292 case 5:
1293 case 6:
1294 gtt_offset = MB(2);
1295 break;
1296 case 4:
1297 default:
1298 gtt_offset = KB(512);
1299 break;
1300 }
1301 intel_private.gtt_bus_addr = reg_addr + gtt_offset;
1302 }
1303
1304 intel_i9xx_setup_flush();
1305
1306 return 0;
1307}
1308
Daniel Vetterf51b7662010-04-14 00:29:52 +02001309static const struct agp_bridge_driver intel_810_driver = {
1310 .owner = THIS_MODULE,
1311 .aperture_sizes = intel_i810_sizes,
1312 .size_type = FIXED_APER_SIZE,
1313 .num_aperture_sizes = 2,
1314 .needs_scratch_page = true,
1315 .configure = intel_i810_configure,
1316 .fetch_size = intel_i810_fetch_size,
1317 .cleanup = intel_i810_cleanup,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001318 .mask_memory = intel_i810_mask_memory,
1319 .masks = intel_i810_masks,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001320 .agp_enable = intel_fake_agp_enable,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001321 .cache_flush = global_cache_flush,
1322 .create_gatt_table = agp_generic_create_gatt_table,
1323 .free_gatt_table = agp_generic_free_gatt_table,
1324 .insert_memory = intel_i810_insert_entries,
1325 .remove_memory = intel_i810_remove_entries,
1326 .alloc_by_type = intel_i810_alloc_by_type,
1327 .free_by_type = intel_i810_free_by_type,
1328 .agp_alloc_page = agp_generic_alloc_page,
1329 .agp_alloc_pages = agp_generic_alloc_pages,
1330 .agp_destroy_page = agp_generic_destroy_page,
1331 .agp_destroy_pages = agp_generic_destroy_pages,
1332 .agp_type_to_mask_type = agp_generic_type_to_mask_type,
1333};
1334
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001335static const struct agp_bridge_driver intel_fake_agp_driver = {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001336 .owner = THIS_MODULE,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001337 .size_type = FIXED_APER_SIZE,
Chris Wilson9e76e7b82010-09-14 12:12:11 +01001338 .aperture_sizes = intel_fake_agp_sizes,
1339 .num_aperture_sizes = ARRAY_SIZE(intel_fake_agp_sizes),
Daniel Vettera6963592010-09-11 14:01:43 +02001340 .configure = intel_fake_agp_configure,
Daniel Vetter3e921f92010-08-27 15:33:26 +02001341 .fetch_size = intel_fake_agp_fetch_size,
Daniel Vetterfdfb58a2010-08-29 00:15:03 +02001342 .cleanup = intel_gtt_cleanup,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001343 .agp_enable = intel_fake_agp_enable,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001344 .cache_flush = global_cache_flush,
Daniel Vetter3b15a9d2010-08-29 14:18:49 +02001345 .create_gatt_table = intel_fake_agp_create_gatt_table,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001346 .free_gatt_table = intel_fake_agp_free_gatt_table,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001347 .insert_memory = intel_fake_agp_insert_entries,
1348 .remove_memory = intel_fake_agp_remove_entries,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001349 .alloc_by_type = intel_fake_agp_alloc_by_type,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001350 .free_by_type = intel_i810_free_by_type,
1351 .agp_alloc_page = agp_generic_alloc_page,
1352 .agp_alloc_pages = agp_generic_alloc_pages,
1353 .agp_destroy_page = agp_generic_destroy_page,
1354 .agp_destroy_pages = agp_generic_destroy_pages,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001355 .chipset_flush = intel_fake_agp_chipset_flush,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001356};
Daniel Vetter02c026c2010-08-24 19:39:48 +02001357
Daniel Vetterbdd30722010-09-12 12:34:44 +02001358static const struct intel_gtt_driver i81x_gtt_driver = {
1359 .gen = 1,
Daniel Vetter22533b42010-09-12 16:38:55 +02001360 .dma_mask_size = 32,
Daniel Vetterbdd30722010-09-12 12:34:44 +02001361};
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001362static const struct intel_gtt_driver i8xx_gtt_driver = {
1363 .gen = 2,
Chris Wilson100519e2010-10-31 10:37:02 +00001364 .has_pgtbl_enable = 1,
Daniel Vetter73800422010-08-29 17:29:50 +02001365 .setup = i830_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001366 .cleanup = i830_cleanup,
Daniel Vetter351bb272010-09-07 22:41:04 +02001367 .write_entry = i830_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001368 .dma_mask_size = 32,
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001369 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001370 .chipset_flush = i830_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001371};
1372static const struct intel_gtt_driver i915_gtt_driver = {
1373 .gen = 3,
Chris Wilson100519e2010-10-31 10:37:02 +00001374 .has_pgtbl_enable = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001375 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001376 .cleanup = i9xx_cleanup,
Daniel Vetter351bb272010-09-07 22:41:04 +02001377 /* i945 is the last gpu to need phys mem (for overlay and cursors). */
1378 .write_entry = i830_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001379 .dma_mask_size = 32,
Daniel Vetterfefaa702010-09-11 22:12:11 +02001380 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001381 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001382};
1383static const struct intel_gtt_driver g33_gtt_driver = {
1384 .gen = 3,
1385 .is_g33 = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001386 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001387 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001388 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001389 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001390 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001391 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001392};
1393static const struct intel_gtt_driver pineview_gtt_driver = {
1394 .gen = 3,
1395 .is_pineview = 1, .is_g33 = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001396 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001397 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001398 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001399 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001400 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001401 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001402};
1403static const struct intel_gtt_driver i965_gtt_driver = {
1404 .gen = 4,
Chris Wilson100519e2010-10-31 10:37:02 +00001405 .has_pgtbl_enable = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001406 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001407 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001408 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001409 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001410 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001411 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001412};
1413static const struct intel_gtt_driver g4x_gtt_driver = {
1414 .gen = 5,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001415 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001416 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001417 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001418 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001419 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001420 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001421};
1422static const struct intel_gtt_driver ironlake_gtt_driver = {
1423 .gen = 5,
1424 .is_ironlake = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001425 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001426 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001427 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001428 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001429 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001430 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001431};
1432static const struct intel_gtt_driver sandybridge_gtt_driver = {
1433 .gen = 6,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001434 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001435 .cleanup = gen6_cleanup,
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001436 .write_entry = gen6_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001437 .dma_mask_size = 40,
Daniel Vetter90cb1492010-09-11 23:55:20 +02001438 .check_flags = gen6_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001439 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001440};
1441
Daniel Vetter02c026c2010-08-24 19:39:48 +02001442/* Table to describe Intel GMCH and AGP/PCIE GART drivers. At least one of
1443 * driver and gmch_driver must be non-null, and find_gmch will determine
1444 * which one should be used if a gmch_chip_id is present.
1445 */
1446static const struct intel_gtt_driver_description {
1447 unsigned int gmch_chip_id;
1448 char *name;
1449 const struct agp_bridge_driver *gmch_driver;
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001450 const struct intel_gtt_driver *gtt_driver;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001451} intel_gtt_chipsets[] = {
Daniel Vetterbdd30722010-09-12 12:34:44 +02001452 { PCI_DEVICE_ID_INTEL_82810_IG1, "i810", &intel_810_driver,
1453 &i81x_gtt_driver},
1454 { PCI_DEVICE_ID_INTEL_82810_IG3, "i810", &intel_810_driver,
1455 &i81x_gtt_driver},
1456 { PCI_DEVICE_ID_INTEL_82810E_IG, "i810", &intel_810_driver,
1457 &i81x_gtt_driver},
1458 { PCI_DEVICE_ID_INTEL_82815_CGC, "i815", &intel_810_driver,
1459 &i81x_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001460 { PCI_DEVICE_ID_INTEL_82830_CGC, "830M",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001461 &intel_fake_agp_driver, &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001462 { PCI_DEVICE_ID_INTEL_82845G_IG, "830M",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001463 &intel_fake_agp_driver, &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001464 { PCI_DEVICE_ID_INTEL_82854_IG, "854",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001465 &intel_fake_agp_driver, &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001466 { PCI_DEVICE_ID_INTEL_82855GM_IG, "855GM",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001467 &intel_fake_agp_driver, &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001468 { PCI_DEVICE_ID_INTEL_82865_IG, "865",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001469 &intel_fake_agp_driver, &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001470 { PCI_DEVICE_ID_INTEL_E7221_IG, "E7221 (i915)",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001471 &intel_fake_agp_driver, &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001472 { PCI_DEVICE_ID_INTEL_82915G_IG, "915G",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001473 &intel_fake_agp_driver, &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001474 { PCI_DEVICE_ID_INTEL_82915GM_IG, "915GM",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001475 &intel_fake_agp_driver, &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001476 { PCI_DEVICE_ID_INTEL_82945G_IG, "945G",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001477 &intel_fake_agp_driver, &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001478 { PCI_DEVICE_ID_INTEL_82945GM_IG, "945GM",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001479 &intel_fake_agp_driver, &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001480 { PCI_DEVICE_ID_INTEL_82945GME_IG, "945GME",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001481 &intel_fake_agp_driver, &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001482 { PCI_DEVICE_ID_INTEL_82946GZ_IG, "946GZ",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001483 &intel_fake_agp_driver, &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001484 { PCI_DEVICE_ID_INTEL_82G35_IG, "G35",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001485 &intel_fake_agp_driver, &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001486 { PCI_DEVICE_ID_INTEL_82965Q_IG, "965Q",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001487 &intel_fake_agp_driver, &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001488 { PCI_DEVICE_ID_INTEL_82965G_IG, "965G",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001489 &intel_fake_agp_driver, &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001490 { PCI_DEVICE_ID_INTEL_82965GM_IG, "965GM",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001491 &intel_fake_agp_driver, &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001492 { PCI_DEVICE_ID_INTEL_82965GME_IG, "965GME/GLE",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001493 &intel_fake_agp_driver, &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001494 { PCI_DEVICE_ID_INTEL_G33_IG, "G33",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001495 &intel_fake_agp_driver, &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001496 { PCI_DEVICE_ID_INTEL_Q35_IG, "Q35",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001497 &intel_fake_agp_driver, &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001498 { PCI_DEVICE_ID_INTEL_Q33_IG, "Q33",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001499 &intel_fake_agp_driver, &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001500 { PCI_DEVICE_ID_INTEL_PINEVIEW_M_IG, "GMA3150",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001501 &intel_fake_agp_driver, &pineview_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001502 { PCI_DEVICE_ID_INTEL_PINEVIEW_IG, "GMA3150",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001503 &intel_fake_agp_driver, &pineview_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001504 { PCI_DEVICE_ID_INTEL_GM45_IG, "GM45",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001505 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001506 { PCI_DEVICE_ID_INTEL_EAGLELAKE_IG, "Eaglelake",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001507 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001508 { PCI_DEVICE_ID_INTEL_Q45_IG, "Q45/Q43",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001509 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001510 { PCI_DEVICE_ID_INTEL_G45_IG, "G45/G43",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001511 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001512 { PCI_DEVICE_ID_INTEL_B43_IG, "B43",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001513 &intel_fake_agp_driver, &g4x_gtt_driver },
Chris Wilsone9e5f8e2010-09-21 11:19:32 +01001514 { PCI_DEVICE_ID_INTEL_B43_1_IG, "B43",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001515 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001516 { PCI_DEVICE_ID_INTEL_G41_IG, "G41",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001517 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001518 { PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001519 "HD Graphics", &intel_fake_agp_driver, &ironlake_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001520 { PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001521 "HD Graphics", &intel_fake_agp_driver, &ironlake_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001522 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT1_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001523 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001524 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001525 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001526 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_PLUS_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001527 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001528 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT1_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001529 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001530 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001531 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001532 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_PLUS_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001533 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001534 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001535 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001536 { 0, NULL, NULL }
1537};
1538
1539static int find_gmch(u16 device)
1540{
1541 struct pci_dev *gmch_device;
1542
1543 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL, device, NULL);
1544 if (gmch_device && PCI_FUNC(gmch_device->devfn) != 0) {
1545 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL,
1546 device, gmch_device);
1547 }
1548
1549 if (!gmch_device)
1550 return 0;
1551
1552 intel_private.pcidev = gmch_device;
1553 return 1;
1554}
1555
Daniel Vettere2404e72010-09-08 17:29:51 +02001556int intel_gmch_probe(struct pci_dev *pdev,
Daniel Vetter02c026c2010-08-24 19:39:48 +02001557 struct agp_bridge_data *bridge)
1558{
1559 int i, mask;
1560 bridge->driver = NULL;
1561
1562 for (i = 0; intel_gtt_chipsets[i].name != NULL; i++) {
1563 if (find_gmch(intel_gtt_chipsets[i].gmch_chip_id)) {
1564 bridge->driver =
1565 intel_gtt_chipsets[i].gmch_driver;
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001566 intel_private.driver =
1567 intel_gtt_chipsets[i].gtt_driver;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001568 break;
1569 }
1570 }
1571
1572 if (!bridge->driver)
1573 return 0;
1574
1575 bridge->dev_private_data = &intel_private;
1576 bridge->dev = pdev;
1577
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001578 intel_private.bridge_dev = pci_dev_get(pdev);
1579
Daniel Vetter02c026c2010-08-24 19:39:48 +02001580 dev_info(&pdev->dev, "Intel %s Chipset\n", intel_gtt_chipsets[i].name);
1581
Daniel Vetter22533b42010-09-12 16:38:55 +02001582 mask = intel_private.driver->dma_mask_size;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001583 if (pci_set_dma_mask(intel_private.pcidev, DMA_BIT_MASK(mask)))
1584 dev_err(&intel_private.pcidev->dev,
1585 "set gfx device dma mask %d-bit failed!\n", mask);
1586 else
1587 pci_set_consistent_dma_mask(intel_private.pcidev,
1588 DMA_BIT_MASK(mask));
1589
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001590 if (bridge->driver == &intel_810_driver)
1591 return 1;
1592
Daniel Vetter3b15a9d2010-08-29 14:18:49 +02001593 if (intel_gtt_init() != 0)
1594 return 0;
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001595
Daniel Vetter02c026c2010-08-24 19:39:48 +02001596 return 1;
1597}
Daniel Vettere2404e72010-09-08 17:29:51 +02001598EXPORT_SYMBOL(intel_gmch_probe);
Daniel Vetter02c026c2010-08-24 19:39:48 +02001599
Chris Wilsonc64f7ba2010-11-23 14:24:24 +00001600const struct intel_gtt *intel_gtt_get(void)
Daniel Vetter19966752010-09-06 20:08:44 +02001601{
1602 return &intel_private.base;
1603}
1604EXPORT_SYMBOL(intel_gtt_get);
1605
Daniel Vettere2404e72010-09-08 17:29:51 +02001606void intel_gmch_remove(struct pci_dev *pdev)
Daniel Vetter02c026c2010-08-24 19:39:48 +02001607{
1608 if (intel_private.pcidev)
1609 pci_dev_put(intel_private.pcidev);
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001610 if (intel_private.bridge_dev)
1611 pci_dev_put(intel_private.bridge_dev);
Daniel Vetter02c026c2010-08-24 19:39:48 +02001612}
Daniel Vettere2404e72010-09-08 17:29:51 +02001613EXPORT_SYMBOL(intel_gmch_remove);
1614
1615MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
1616MODULE_LICENSE("GPL and additional rights");