blob: 9d17a6d516409a81ffc292dee2a0b429e52d6d8e [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
Daniel Vetter1a997ff2010-09-08 21:18:53 +020062struct intel_gtt_driver {
63 unsigned int gen : 8;
64 unsigned int is_g33 : 1;
65 unsigned int is_pineview : 1;
66 unsigned int is_ironlake : 1;
Chris Wilson100519e2010-10-31 10:37:02 +000067 unsigned int has_pgtbl_enable : 1;
Daniel Vetter22533b42010-09-12 16:38:55 +020068 unsigned int dma_mask_size : 8;
Daniel Vetter73800422010-08-29 17:29:50 +020069 /* Chipset specific GTT setup */
70 int (*setup)(void);
Daniel Vetterae83dd52010-09-12 17:11:15 +020071 /* This should undo anything done in ->setup() save the unmapping
72 * of the mmio register file, that's done in the generic code. */
73 void (*cleanup)(void);
Daniel Vetter351bb272010-09-07 22:41:04 +020074 void (*write_entry)(dma_addr_t addr, unsigned int entry, unsigned int flags);
75 /* Flags is a more or less chipset specific opaque value.
76 * For chipsets that need to support old ums (non-gem) code, this
77 * needs to be identical to the various supported agp memory types! */
Daniel Vetter5cbecaf2010-09-11 21:31:04 +020078 bool (*check_flags)(unsigned int flags);
Daniel Vetter1b263f22010-09-12 00:27:24 +020079 void (*chipset_flush)(void);
Daniel Vetter1a997ff2010-09-08 21:18:53 +020080};
81
Daniel Vetterf51b7662010-04-14 00:29:52 +020082static struct _intel_private {
Daniel Vetter0ade6382010-08-24 22:18:41 +020083 struct intel_gtt base;
Daniel Vetter1a997ff2010-09-08 21:18:53 +020084 const struct intel_gtt_driver *driver;
Daniel Vetterf51b7662010-04-14 00:29:52 +020085 struct pci_dev *pcidev; /* device one */
Daniel Vetterd7cca2f2010-08-24 23:06:19 +020086 struct pci_dev *bridge_dev;
Daniel Vetterf51b7662010-04-14 00:29:52 +020087 u8 __iomem *registers;
Daniel Vetterf67eab62010-08-29 17:27:36 +020088 phys_addr_t gtt_bus_addr;
Daniel Vetter73800422010-08-29 17:29:50 +020089 phys_addr_t gma_bus_addr;
Daniel Vetterb3eafc52010-09-23 20:04:17 +020090 u32 PGETBL_save;
Daniel Vetterf51b7662010-04-14 00:29:52 +020091 u32 __iomem *gtt; /* I915G */
92 int num_dcache_entries;
Daniel Vetterf51b7662010-04-14 00:29:52 +020093 union {
94 void __iomem *i9xx_flush_page;
95 void *i8xx_flush_page;
96 };
97 struct page *i8xx_page;
98 struct resource ifp_resource;
99 int resource_valid;
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200100 struct page *scratch_page;
101 dma_addr_t scratch_page_dma;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200102} intel_private;
103
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100104static int intel_fake_agp_insert_entries(struct agp_memory *mem,
105 off_t pg_start, int type);
106
Daniel Vetter1a997ff2010-09-08 21:18:53 +0200107#define INTEL_GTT_GEN intel_private.driver->gen
108#define IS_G33 intel_private.driver->is_g33
109#define IS_PINEVIEW intel_private.driver->is_pineview
110#define IS_IRONLAKE intel_private.driver->is_ironlake
Chris Wilson100519e2010-10-31 10:37:02 +0000111#define HAS_PGTBL_EN intel_private.driver->has_pgtbl_enable
Daniel Vetter1a997ff2010-09-08 21:18:53 +0200112
Daniel Vetterf51b7662010-04-14 00:29:52 +0200113static void intel_agp_free_sglist(struct agp_memory *mem)
114{
115 struct sg_table st;
116
117 st.sgl = mem->sg_list;
118 st.orig_nents = st.nents = mem->page_count;
119
120 sg_free_table(&st);
121
122 mem->sg_list = NULL;
123 mem->num_sg = 0;
124}
125
126static int intel_agp_map_memory(struct agp_memory *mem)
127{
128 struct sg_table st;
129 struct scatterlist *sg;
130 int i;
131
Daniel Vetterfefaa702010-09-11 22:12:11 +0200132 if (mem->sg_list)
133 return 0; /* already mapped (for e.g. resume */
134
Daniel Vetterf51b7662010-04-14 00:29:52 +0200135 DBG("try mapping %lu pages\n", (unsigned long)mem->page_count);
136
137 if (sg_alloc_table(&st, mem->page_count, GFP_KERNEL))
Chris Wilson831cd442010-07-24 18:29:37 +0100138 goto err;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200139
140 mem->sg_list = sg = st.sgl;
141
142 for (i = 0 ; i < mem->page_count; i++, sg = sg_next(sg))
143 sg_set_page(sg, mem->pages[i], PAGE_SIZE, 0);
144
145 mem->num_sg = pci_map_sg(intel_private.pcidev, mem->sg_list,
146 mem->page_count, PCI_DMA_BIDIRECTIONAL);
Chris Wilson831cd442010-07-24 18:29:37 +0100147 if (unlikely(!mem->num_sg))
148 goto err;
149
Daniel Vetterf51b7662010-04-14 00:29:52 +0200150 return 0;
Chris Wilson831cd442010-07-24 18:29:37 +0100151
152err:
153 sg_free_table(&st);
154 return -ENOMEM;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200155}
156
157static void intel_agp_unmap_memory(struct agp_memory *mem)
158{
159 DBG("try unmapping %lu pages\n", (unsigned long)mem->page_count);
160
161 pci_unmap_sg(intel_private.pcidev, mem->sg_list,
162 mem->page_count, PCI_DMA_BIDIRECTIONAL);
163 intel_agp_free_sglist(mem);
164}
165
Daniel Vetterf51b7662010-04-14 00:29:52 +0200166static int intel_i810_fetch_size(void)
167{
168 u32 smram_miscc;
169 struct aper_size_info_fixed *values;
170
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200171 pci_read_config_dword(intel_private.bridge_dev,
172 I810_SMRAM_MISCC, &smram_miscc);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200173 values = A_SIZE_FIX(agp_bridge->driver->aperture_sizes);
174
175 if ((smram_miscc & I810_GMS) == I810_GMS_DISABLE) {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200176 dev_warn(&intel_private.bridge_dev->dev, "i810 is disabled\n");
Daniel Vetterf51b7662010-04-14 00:29:52 +0200177 return 0;
178 }
179 if ((smram_miscc & I810_GFX_MEM_WIN_SIZE) == I810_GFX_MEM_WIN_32M) {
Daniel Vettere1583162010-04-14 00:29:58 +0200180 agp_bridge->current_size = (void *) (values + 1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200181 agp_bridge->aperture_size_idx = 1;
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100182 intel_private.base.gtt_total_entries = KB(32) / 4;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200183 return values[1].size;
184 } else {
Daniel Vettere1583162010-04-14 00:29:58 +0200185 agp_bridge->current_size = (void *) (values);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200186 agp_bridge->aperture_size_idx = 0;
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100187 intel_private.base.gtt_total_entries = KB(64) / 4;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200188 return values[0].size;
189 }
190
191 return 0;
192}
193
194static int intel_i810_configure(void)
195{
196 struct aper_size_info_fixed *current_size;
197 u32 temp;
198 int i;
199
200 current_size = A_SIZE_FIX(agp_bridge->current_size);
201
202 if (!intel_private.registers) {
203 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &temp);
204 temp &= 0xfff80000;
205
206 intel_private.registers = ioremap(temp, 128 * 4096);
207 if (!intel_private.registers) {
208 dev_err(&intel_private.pcidev->dev,
209 "can't remap memory\n");
210 return -ENOMEM;
211 }
212 }
213
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100214 intel_private.gtt = intel_private.registers + I810_PTE_BASE;
215 intel_private.scratch_page_dma = agp_bridge->scratch_page & PAGE_MASK;
216
Daniel Vetterf51b7662010-04-14 00:29:52 +0200217 if ((readl(intel_private.registers+I810_DRAM_CTL)
218 & I810_DRAM_ROW_0) == I810_DRAM_ROW_0_SDRAM) {
219 /* This will need to be dynamically assigned */
220 dev_info(&intel_private.pcidev->dev,
221 "detected 4MB dedicated video ram\n");
222 intel_private.num_dcache_entries = 1024;
223 }
224 pci_read_config_dword(intel_private.pcidev, I810_GMADDR, &temp);
225 agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
226 writel(agp_bridge->gatt_bus_addr | I810_PGETBL_ENABLED, intel_private.registers+I810_PGETBL_CTL);
227 readl(intel_private.registers+I810_PGETBL_CTL); /* PCI Posting. */
228
229 if (agp_bridge->driver->needs_scratch_page) {
230 for (i = 0; i < current_size->num_entries; i++) {
231 writel(agp_bridge->scratch_page, intel_private.registers+I810_PTE_BASE+(i*4));
232 }
233 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4)); /* PCI posting. */
234 }
235 global_cache_flush();
236 return 0;
237}
238
239static void intel_i810_cleanup(void)
240{
241 writel(0, intel_private.registers+I810_PGETBL_CTL);
242 readl(intel_private.registers); /* PCI Posting. */
243 iounmap(intel_private.registers);
244}
245
Daniel Vetterffdd7512010-08-27 17:51:29 +0200246static void intel_fake_agp_enable(struct agp_bridge_data *bridge, u32 mode)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200247{
248 return;
249}
250
251/* Exists to support ARGB cursors */
252static struct page *i8xx_alloc_pages(void)
253{
254 struct page *page;
255
256 page = alloc_pages(GFP_KERNEL | GFP_DMA32, 2);
257 if (page == NULL)
258 return NULL;
259
260 if (set_pages_uc(page, 4) < 0) {
261 set_pages_wb(page, 4);
262 __free_pages(page, 2);
263 return NULL;
264 }
265 get_page(page);
266 atomic_inc(&agp_bridge->current_memory_agp);
267 return page;
268}
269
270static void i8xx_destroy_pages(struct page *page)
271{
272 if (page == NULL)
273 return;
274
275 set_pages_wb(page, 4);
276 put_page(page);
277 __free_pages(page, 2);
278 atomic_dec(&agp_bridge->current_memory_agp);
279}
280
Daniel Vetterf51b7662010-04-14 00:29:52 +0200281static int intel_i810_insert_entries(struct agp_memory *mem, off_t pg_start,
282 int type)
283{
Daniel Vetterf51b7662010-04-14 00:29:52 +0200284 int i;
285
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100286 if (type == AGP_DCACHE_MEMORY) {
287 if ((pg_start + mem->page_count)
288 > intel_private.num_dcache_entries)
289 return -EINVAL;
290
291 if (!mem->is_flushed)
292 global_cache_flush();
293
294 for (i = pg_start; i < (pg_start + mem->page_count); i++) {
295 dma_addr_t addr = i << PAGE_SHIFT;
296 intel_private.driver->write_entry(addr,
297 i, type);
298 }
299 readl(intel_private.gtt+i-1);
300
Daniel Vetterf51b7662010-04-14 00:29:52 +0200301 return 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200302 }
Daniel Vetterf51b7662010-04-14 00:29:52 +0200303
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100304 return intel_fake_agp_insert_entries(mem, pg_start, type);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200305}
306
307/*
308 * The i810/i830 requires a physical address to program its mouse
309 * pointer into hardware.
310 * However the Xserver still writes to it through the agp aperture.
311 */
312static struct agp_memory *alloc_agpphysmem_i8xx(size_t pg_count, int type)
313{
314 struct agp_memory *new;
315 struct page *page;
316
317 switch (pg_count) {
318 case 1: page = agp_bridge->driver->agp_alloc_page(agp_bridge);
319 break;
320 case 4:
321 /* kludge to get 4 physical pages for ARGB cursor */
322 page = i8xx_alloc_pages();
323 break;
324 default:
325 return NULL;
326 }
327
328 if (page == NULL)
329 return NULL;
330
331 new = agp_create_memory(pg_count);
332 if (new == NULL)
333 return NULL;
334
335 new->pages[0] = page;
336 if (pg_count == 4) {
337 /* kludge to get 4 physical pages for ARGB cursor */
338 new->pages[1] = new->pages[0] + 1;
339 new->pages[2] = new->pages[1] + 1;
340 new->pages[3] = new->pages[2] + 1;
341 }
342 new->page_count = pg_count;
343 new->num_scratch_pages = pg_count;
344 new->type = AGP_PHYS_MEMORY;
345 new->physical = page_to_phys(new->pages[0]);
346 return new;
347}
348
Daniel Vetterf51b7662010-04-14 00:29:52 +0200349static void intel_i810_free_by_type(struct agp_memory *curr)
350{
351 agp_free_key(curr->key);
352 if (curr->type == AGP_PHYS_MEMORY) {
353 if (curr->page_count == 4)
354 i8xx_destroy_pages(curr->pages[0]);
355 else {
356 agp_bridge->driver->agp_destroy_page(curr->pages[0],
357 AGP_PAGE_DESTROY_UNMAP);
358 agp_bridge->driver->agp_destroy_page(curr->pages[0],
359 AGP_PAGE_DESTROY_FREE);
360 }
361 agp_free_page_array(curr);
362 }
363 kfree(curr);
364}
365
366static unsigned long intel_i810_mask_memory(struct agp_bridge_data *bridge,
367 dma_addr_t addr, int type)
368{
369 /* Type checking must be done elsewhere */
370 return addr | bridge->driver->masks[type].mask;
371}
372
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200373static int intel_gtt_setup_scratch_page(void)
374{
375 struct page *page;
376 dma_addr_t dma_addr;
377
378 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
379 if (page == NULL)
380 return -ENOMEM;
381 get_page(page);
382 set_pages_uc(page, 1);
383
384 if (USE_PCI_DMA_API && INTEL_GTT_GEN > 2) {
385 dma_addr = pci_map_page(intel_private.pcidev, page, 0,
386 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
387 if (pci_dma_mapping_error(intel_private.pcidev, dma_addr))
388 return -EINVAL;
389
390 intel_private.scratch_page_dma = dma_addr;
391 } else
392 intel_private.scratch_page_dma = page_to_phys(page);
393
394 intel_private.scratch_page = page;
395
396 return 0;
397}
398
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100399static void i810_write_entry(dma_addr_t addr, unsigned int entry,
400 unsigned int flags)
401{
402 u32 pte_flags = I810_PTE_VALID;
403
404 switch (flags) {
405 case AGP_DCACHE_MEMORY:
406 pte_flags |= I810_PTE_LOCAL;
407 break;
408 case AGP_USER_CACHED_MEMORY:
409 pte_flags |= I830_PTE_SYSTEM_CACHED;
410 break;
411 }
412
413 writel(addr | pte_flags, intel_private.gtt + entry);
414}
415
Chris Wilson9e76e7b2010-09-14 12:12:11 +0100416static const struct aper_size_info_fixed const intel_fake_agp_sizes[] = {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200417 {128, 32768, 5},
418 /* The 64M mode still requires a 128k gatt */
419 {64, 16384, 5},
420 {256, 65536, 6},
421 {512, 131072, 7},
422};
423
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000424static unsigned int intel_gtt_stolen_size(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200425{
426 u16 gmch_ctrl;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200427 u8 rdct;
428 int local = 0;
429 static const int ddt[4] = { 0, 16, 32, 64 };
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200430 unsigned int stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200431
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200432 pci_read_config_word(intel_private.bridge_dev,
433 I830_GMCH_CTRL, &gmch_ctrl);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200434
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200435 if (intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82830_HB ||
436 intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82845G_HB) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200437 switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
438 case I830_GMCH_GMS_STOLEN_512:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200439 stolen_size = KB(512);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200440 break;
441 case I830_GMCH_GMS_STOLEN_1024:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200442 stolen_size = MB(1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200443 break;
444 case I830_GMCH_GMS_STOLEN_8192:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200445 stolen_size = MB(8);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200446 break;
447 case I830_GMCH_GMS_LOCAL:
448 rdct = readb(intel_private.registers+I830_RDRAM_CHANNEL_TYPE);
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200449 stolen_size = (I830_RDRAM_ND(rdct) + 1) *
Daniel Vetterf51b7662010-04-14 00:29:52 +0200450 MB(ddt[I830_RDRAM_DDT(rdct)]);
451 local = 1;
452 break;
453 default:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200454 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200455 break;
456 }
Daniel Vetter1a997ff2010-09-08 21:18:53 +0200457 } else if (INTEL_GTT_GEN == 6) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200458 /*
459 * SandyBridge has new memory control reg at 0x50.w
460 */
461 u16 snb_gmch_ctl;
462 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl);
463 switch (snb_gmch_ctl & SNB_GMCH_GMS_STOLEN_MASK) {
464 case SNB_GMCH_GMS_STOLEN_32M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200465 stolen_size = MB(32);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200466 break;
467 case SNB_GMCH_GMS_STOLEN_64M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200468 stolen_size = MB(64);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200469 break;
470 case SNB_GMCH_GMS_STOLEN_96M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200471 stolen_size = MB(96);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200472 break;
473 case SNB_GMCH_GMS_STOLEN_128M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200474 stolen_size = MB(128);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200475 break;
476 case SNB_GMCH_GMS_STOLEN_160M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200477 stolen_size = MB(160);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200478 break;
479 case SNB_GMCH_GMS_STOLEN_192M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200480 stolen_size = MB(192);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200481 break;
482 case SNB_GMCH_GMS_STOLEN_224M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200483 stolen_size = MB(224);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200484 break;
485 case SNB_GMCH_GMS_STOLEN_256M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200486 stolen_size = MB(256);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200487 break;
488 case SNB_GMCH_GMS_STOLEN_288M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200489 stolen_size = MB(288);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200490 break;
491 case SNB_GMCH_GMS_STOLEN_320M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200492 stolen_size = MB(320);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200493 break;
494 case SNB_GMCH_GMS_STOLEN_352M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200495 stolen_size = MB(352);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200496 break;
497 case SNB_GMCH_GMS_STOLEN_384M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200498 stolen_size = MB(384);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200499 break;
500 case SNB_GMCH_GMS_STOLEN_416M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200501 stolen_size = MB(416);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200502 break;
503 case SNB_GMCH_GMS_STOLEN_448M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200504 stolen_size = MB(448);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200505 break;
506 case SNB_GMCH_GMS_STOLEN_480M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200507 stolen_size = MB(480);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200508 break;
509 case SNB_GMCH_GMS_STOLEN_512M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200510 stolen_size = MB(512);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200511 break;
512 }
513 } else {
514 switch (gmch_ctrl & I855_GMCH_GMS_MASK) {
515 case I855_GMCH_GMS_STOLEN_1M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200516 stolen_size = MB(1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200517 break;
518 case I855_GMCH_GMS_STOLEN_4M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200519 stolen_size = MB(4);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200520 break;
521 case I855_GMCH_GMS_STOLEN_8M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200522 stolen_size = MB(8);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200523 break;
524 case I855_GMCH_GMS_STOLEN_16M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200525 stolen_size = MB(16);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200526 break;
527 case I855_GMCH_GMS_STOLEN_32M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200528 stolen_size = MB(32);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200529 break;
530 case I915_GMCH_GMS_STOLEN_48M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200531 stolen_size = MB(48);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200532 break;
533 case I915_GMCH_GMS_STOLEN_64M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200534 stolen_size = MB(64);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200535 break;
536 case G33_GMCH_GMS_STOLEN_128M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200537 stolen_size = MB(128);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200538 break;
539 case G33_GMCH_GMS_STOLEN_256M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200540 stolen_size = MB(256);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200541 break;
542 case INTEL_GMCH_GMS_STOLEN_96M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200543 stolen_size = MB(96);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200544 break;
545 case INTEL_GMCH_GMS_STOLEN_160M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200546 stolen_size = MB(160);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200547 break;
548 case INTEL_GMCH_GMS_STOLEN_224M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200549 stolen_size = MB(224);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200550 break;
551 case INTEL_GMCH_GMS_STOLEN_352M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200552 stolen_size = MB(352);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200553 break;
554 default:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200555 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200556 break;
557 }
558 }
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200559
Chris Wilson1b6064d2010-11-23 12:33:54 +0000560 if (stolen_size > 0) {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200561 dev_info(&intel_private.bridge_dev->dev, "detected %dK %s memory\n",
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200562 stolen_size / KB(1), local ? "local" : "stolen");
Daniel Vetterf51b7662010-04-14 00:29:52 +0200563 } else {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200564 dev_info(&intel_private.bridge_dev->dev,
Daniel Vetterf51b7662010-04-14 00:29:52 +0200565 "no pre-allocated video memory detected\n");
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200566 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200567 }
568
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000569 return stolen_size;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200570}
571
Daniel Vetter20172842010-09-24 18:25:59 +0200572static void i965_adjust_pgetbl_size(unsigned int size_flag)
573{
574 u32 pgetbl_ctl, pgetbl_ctl2;
575
576 /* ensure that ppgtt is disabled */
577 pgetbl_ctl2 = readl(intel_private.registers+I965_PGETBL_CTL2);
578 pgetbl_ctl2 &= ~I810_PGETBL_ENABLED;
579 writel(pgetbl_ctl2, intel_private.registers+I965_PGETBL_CTL2);
580
581 /* write the new ggtt size */
582 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
583 pgetbl_ctl &= ~I965_PGETBL_SIZE_MASK;
584 pgetbl_ctl |= size_flag;
585 writel(pgetbl_ctl, intel_private.registers+I810_PGETBL_CTL);
586}
587
588static unsigned int i965_gtt_total_entries(void)
589{
590 int size;
591 u32 pgetbl_ctl;
592 u16 gmch_ctl;
593
594 pci_read_config_word(intel_private.bridge_dev,
595 I830_GMCH_CTRL, &gmch_ctl);
596
597 if (INTEL_GTT_GEN == 5) {
598 switch (gmch_ctl & G4x_GMCH_SIZE_MASK) {
599 case G4x_GMCH_SIZE_1M:
600 case G4x_GMCH_SIZE_VT_1M:
601 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1MB);
602 break;
603 case G4x_GMCH_SIZE_VT_1_5M:
604 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1_5MB);
605 break;
606 case G4x_GMCH_SIZE_2M:
607 case G4x_GMCH_SIZE_VT_2M:
608 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_2MB);
609 break;
610 }
611 }
612
613 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
614
615 switch (pgetbl_ctl & I965_PGETBL_SIZE_MASK) {
616 case I965_PGETBL_SIZE_128KB:
617 size = KB(128);
618 break;
619 case I965_PGETBL_SIZE_256KB:
620 size = KB(256);
621 break;
622 case I965_PGETBL_SIZE_512KB:
623 size = KB(512);
624 break;
625 /* GTT pagetable sizes bigger than 512KB are not possible on G33! */
626 case I965_PGETBL_SIZE_1MB:
627 size = KB(1024);
628 break;
629 case I965_PGETBL_SIZE_2MB:
630 size = KB(2048);
631 break;
632 case I965_PGETBL_SIZE_1_5MB:
633 size = KB(1024 + 512);
634 break;
635 default:
636 dev_info(&intel_private.pcidev->dev,
637 "unknown page table size, assuming 512KB\n");
638 size = KB(512);
639 }
640
641 return size/4;
642}
643
Daniel Vetterfbe40782010-08-27 17:12:41 +0200644static unsigned int intel_gtt_total_entries(void)
645{
646 int size;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200647
Daniel Vetter20172842010-09-24 18:25:59 +0200648 if (IS_G33 || INTEL_GTT_GEN == 4 || INTEL_GTT_GEN == 5)
649 return i965_gtt_total_entries();
650 else if (INTEL_GTT_GEN == 6) {
Daniel Vetter210b23c2010-08-28 16:14:32 +0200651 u16 snb_gmch_ctl;
652
653 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl);
654 switch (snb_gmch_ctl & SNB_GTT_SIZE_MASK) {
655 default:
656 case SNB_GTT_SIZE_0M:
657 printk(KERN_ERR "Bad GTT size mask: 0x%04x.\n", snb_gmch_ctl);
658 size = MB(0);
659 break;
660 case SNB_GTT_SIZE_1M:
661 size = MB(1);
662 break;
663 case SNB_GTT_SIZE_2M:
664 size = MB(2);
665 break;
666 }
667 return size/4;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200668 } else {
669 /* On previous hardware, the GTT size was just what was
670 * required to map the aperture.
671 */
Daniel Vettere5e408f2010-08-28 11:04:32 +0200672 return intel_private.base.gtt_mappable_entries;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200673 }
Daniel Vetterfbe40782010-08-27 17:12:41 +0200674}
Daniel Vetterfbe40782010-08-27 17:12:41 +0200675
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200676static unsigned int intel_gtt_mappable_entries(void)
677{
678 unsigned int aperture_size;
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200679
Daniel Vetter239918f2010-08-31 22:30:43 +0200680 if (INTEL_GTT_GEN == 2) {
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100681 u16 gmch_ctrl;
682
683 pci_read_config_word(intel_private.bridge_dev,
684 I830_GMCH_CTRL, &gmch_ctrl);
685
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200686 if ((gmch_ctrl & I830_GMCH_MEM_MASK) == I830_GMCH_MEM_64M)
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100687 aperture_size = MB(64);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200688 else
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100689 aperture_size = MB(128);
Daniel Vetter239918f2010-08-31 22:30:43 +0200690 } else {
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200691 /* 9xx supports large sizes, just look at the length */
692 aperture_size = pci_resource_len(intel_private.pcidev, 2);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200693 }
694
695 return aperture_size >> PAGE_SHIFT;
696}
697
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200698static void intel_gtt_teardown_scratch_page(void)
699{
700 set_pages_wb(intel_private.scratch_page, 1);
701 pci_unmap_page(intel_private.pcidev, intel_private.scratch_page_dma,
702 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
703 put_page(intel_private.scratch_page);
704 __free_page(intel_private.scratch_page);
705}
706
707static void intel_gtt_cleanup(void)
708{
Daniel Vetterae83dd52010-09-12 17:11:15 +0200709 intel_private.driver->cleanup();
710
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200711 iounmap(intel_private.gtt);
712 iounmap(intel_private.registers);
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100713
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200714 intel_gtt_teardown_scratch_page();
715}
716
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200717static int intel_gtt_init(void)
718{
Daniel Vetterf67eab62010-08-29 17:27:36 +0200719 u32 gtt_map_size;
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200720 int ret;
721
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200722 ret = intel_private.driver->setup();
723 if (ret != 0)
724 return ret;
Daniel Vetterf67eab62010-08-29 17:27:36 +0200725
726 intel_private.base.gtt_mappable_entries = intel_gtt_mappable_entries();
727 intel_private.base.gtt_total_entries = intel_gtt_total_entries();
728
Daniel Vetterb3eafc52010-09-23 20:04:17 +0200729 /* save the PGETBL reg for resume */
730 intel_private.PGETBL_save =
731 readl(intel_private.registers+I810_PGETBL_CTL)
732 & ~I810_PGETBL_ENABLED;
Chris Wilson100519e2010-10-31 10:37:02 +0000733 /* we only ever restore the register when enabling the PGTBL... */
734 if (HAS_PGTBL_EN)
735 intel_private.PGETBL_save |= I810_PGETBL_ENABLED;
Daniel Vetterb3eafc52010-09-23 20:04:17 +0200736
Daniel Vetter0af9e922010-09-12 14:04:03 +0200737 dev_info(&intel_private.bridge_dev->dev,
738 "detected gtt size: %dK total, %dK mappable\n",
739 intel_private.base.gtt_total_entries * 4,
740 intel_private.base.gtt_mappable_entries * 4);
741
Daniel Vetterf67eab62010-08-29 17:27:36 +0200742 gtt_map_size = intel_private.base.gtt_total_entries * 4;
743
744 intel_private.gtt = ioremap(intel_private.gtt_bus_addr,
745 gtt_map_size);
746 if (!intel_private.gtt) {
Daniel Vetterae83dd52010-09-12 17:11:15 +0200747 intel_private.driver->cleanup();
Daniel Vetterf67eab62010-08-29 17:27:36 +0200748 iounmap(intel_private.registers);
749 return -ENOMEM;
750 }
751
752 global_cache_flush(); /* FIXME: ? */
753
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200754 /* we have to call this as early as possible after the MMIO base address is known */
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000755 intel_private.base.stolen_size = intel_gtt_stolen_size();
756 if (intel_private.base.stolen_size == 0) {
Daniel Vetterae83dd52010-09-12 17:11:15 +0200757 intel_private.driver->cleanup();
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200758 iounmap(intel_private.registers);
Daniel Vetterf67eab62010-08-29 17:27:36 +0200759 iounmap(intel_private.gtt);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200760 return -ENOMEM;
761 }
762
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200763 ret = intel_gtt_setup_scratch_page();
764 if (ret != 0) {
765 intel_gtt_cleanup();
766 return ret;
767 }
768
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200769 return 0;
770}
771
Daniel Vetter3e921f92010-08-27 15:33:26 +0200772static int intel_fake_agp_fetch_size(void)
773{
Chris Wilson9e76e7b2010-09-14 12:12:11 +0100774 int num_sizes = ARRAY_SIZE(intel_fake_agp_sizes);
Daniel Vetter3e921f92010-08-27 15:33:26 +0200775 unsigned int aper_size;
776 int i;
Daniel Vetter3e921f92010-08-27 15:33:26 +0200777
778 aper_size = (intel_private.base.gtt_mappable_entries << PAGE_SHIFT)
779 / MB(1);
780
781 for (i = 0; i < num_sizes; i++) {
Daniel Vetterffdd7512010-08-27 17:51:29 +0200782 if (aper_size == intel_fake_agp_sizes[i].size) {
Chris Wilson9e76e7b2010-09-14 12:12:11 +0100783 agp_bridge->current_size =
784 (void *) (intel_fake_agp_sizes + i);
Daniel Vetter3e921f92010-08-27 15:33:26 +0200785 return aper_size;
786 }
787 }
788
789 return 0;
790}
791
Daniel Vetterae83dd52010-09-12 17:11:15 +0200792static void i830_cleanup(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200793{
794 kunmap(intel_private.i8xx_page);
795 intel_private.i8xx_flush_page = NULL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200796
797 __free_page(intel_private.i8xx_page);
798 intel_private.i8xx_page = NULL;
799}
800
801static void intel_i830_setup_flush(void)
802{
803 /* return if we've already set the flush mechanism up */
804 if (intel_private.i8xx_page)
805 return;
806
Jan Beuliche61cb0d2010-09-24 13:25:30 +0100807 intel_private.i8xx_page = alloc_page(GFP_KERNEL);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200808 if (!intel_private.i8xx_page)
809 return;
810
811 intel_private.i8xx_flush_page = kmap(intel_private.i8xx_page);
812 if (!intel_private.i8xx_flush_page)
Daniel Vetterae83dd52010-09-12 17:11:15 +0200813 i830_cleanup();
Daniel Vetterf51b7662010-04-14 00:29:52 +0200814}
815
816/* The chipset_flush interface needs to get data that has already been
817 * flushed out of the CPU all the way out to main memory, because the GPU
818 * doesn't snoop those buffers.
819 *
820 * The 8xx series doesn't have the same lovely interface for flushing the
821 * chipset write buffers that the later chips do. According to the 865
822 * specs, it's 64 octwords, or 1KB. So, to get those previous things in
823 * that buffer out, we just fill 1KB and clflush it out, on the assumption
824 * that it'll push whatever was in there out. It appears to work.
825 */
Daniel Vetter1b263f22010-09-12 00:27:24 +0200826static void i830_chipset_flush(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200827{
828 unsigned int *pg = intel_private.i8xx_flush_page;
829
830 memset(pg, 0, 1024);
831
832 if (cpu_has_clflush)
833 clflush_cache_range(pg, 1024);
834 else if (wbinvd_on_all_cpus() != 0)
835 printk(KERN_ERR "Timed out waiting for cache flush.\n");
836}
837
Daniel Vetter351bb272010-09-07 22:41:04 +0200838static void i830_write_entry(dma_addr_t addr, unsigned int entry,
839 unsigned int flags)
840{
841 u32 pte_flags = I810_PTE_VALID;
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100842
Daniel Vetterb47cf662010-11-04 18:41:50 +0100843 if (flags == AGP_USER_CACHED_MEMORY)
Daniel Vetter351bb272010-09-07 22:41:04 +0200844 pte_flags |= I830_PTE_SYSTEM_CACHED;
Daniel Vetter351bb272010-09-07 22:41:04 +0200845
846 writel(addr | pte_flags, intel_private.gtt + entry);
847}
848
Chris Wilsone380f602010-10-29 18:11:26 +0100849static bool intel_enable_gtt(void)
Daniel Vetter73800422010-08-29 17:29:50 +0200850{
Chris Wilson3f08e4e2010-09-14 20:15:22 +0100851 u32 gma_addr;
Chris Wilsone380f602010-10-29 18:11:26 +0100852 u8 __iomem *reg;
Daniel Vetter73800422010-08-29 17:29:50 +0200853
Daniel Vetter2d2430c2010-08-29 17:35:30 +0200854 if (INTEL_GTT_GEN == 2)
855 pci_read_config_dword(intel_private.pcidev, I810_GMADDR,
856 &gma_addr);
857 else
858 pci_read_config_dword(intel_private.pcidev, I915_GMADDR,
859 &gma_addr);
860
Daniel Vetter73800422010-08-29 17:29:50 +0200861 intel_private.gma_bus_addr = (gma_addr & PCI_BASE_ADDRESS_MEM_MASK);
862
Chris Wilsone380f602010-10-29 18:11:26 +0100863 if (INTEL_GTT_GEN >= 6)
864 return true;
Daniel Vetter73800422010-08-29 17:29:50 +0200865
Chris Wilson100519e2010-10-31 10:37:02 +0000866 if (INTEL_GTT_GEN == 2) {
867 u16 gmch_ctrl;
Chris Wilsone380f602010-10-29 18:11:26 +0100868
Chris Wilson100519e2010-10-31 10:37:02 +0000869 pci_read_config_word(intel_private.bridge_dev,
870 I830_GMCH_CTRL, &gmch_ctrl);
871 gmch_ctrl |= I830_GMCH_ENABLED;
872 pci_write_config_word(intel_private.bridge_dev,
873 I830_GMCH_CTRL, gmch_ctrl);
874
875 pci_read_config_word(intel_private.bridge_dev,
876 I830_GMCH_CTRL, &gmch_ctrl);
877 if ((gmch_ctrl & I830_GMCH_ENABLED) == 0) {
878 dev_err(&intel_private.pcidev->dev,
879 "failed to enable the GTT: GMCH_CTRL=%x\n",
880 gmch_ctrl);
881 return false;
882 }
Chris Wilsone380f602010-10-29 18:11:26 +0100883 }
884
885 reg = intel_private.registers+I810_PGETBL_CTL;
Chris Wilson100519e2010-10-31 10:37:02 +0000886 writel(intel_private.PGETBL_save, reg);
887 if (HAS_PGTBL_EN && (readl(reg) & I810_PGETBL_ENABLED) == 0) {
Chris Wilsone380f602010-10-29 18:11:26 +0100888 dev_err(&intel_private.pcidev->dev,
Chris Wilson100519e2010-10-31 10:37:02 +0000889 "failed to enable the GTT: PGETBL=%x [expected %x]\n",
Chris Wilsone380f602010-10-29 18:11:26 +0100890 readl(reg), intel_private.PGETBL_save);
891 return false;
892 }
893
894 return true;
Daniel Vetter73800422010-08-29 17:29:50 +0200895}
896
897static int i830_setup(void)
898{
899 u32 reg_addr;
900
901 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &reg_addr);
902 reg_addr &= 0xfff80000;
903
904 intel_private.registers = ioremap(reg_addr, KB(64));
905 if (!intel_private.registers)
906 return -ENOMEM;
907
908 intel_private.gtt_bus_addr = reg_addr + I810_PTE_BASE;
909
910 intel_i830_setup_flush();
911
912 return 0;
913}
914
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200915static int intel_fake_agp_create_gatt_table(struct agp_bridge_data *bridge)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200916{
Daniel Vetter73800422010-08-29 17:29:50 +0200917 agp_bridge->gatt_table_real = NULL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200918 agp_bridge->gatt_table = NULL;
Daniel Vetter73800422010-08-29 17:29:50 +0200919 agp_bridge->gatt_bus_addr = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200920
921 return 0;
922}
923
Daniel Vetterffdd7512010-08-27 17:51:29 +0200924static int intel_fake_agp_free_gatt_table(struct agp_bridge_data *bridge)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200925{
926 return 0;
927}
928
Daniel Vetter351bb272010-09-07 22:41:04 +0200929static int intel_fake_agp_configure(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200930{
Daniel Vetterf51b7662010-04-14 00:29:52 +0200931 int i;
932
Chris Wilsone380f602010-10-29 18:11:26 +0100933 if (!intel_enable_gtt())
934 return -EIO;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200935
Daniel Vetter73800422010-08-29 17:29:50 +0200936 agp_bridge->gart_bus_addr = intel_private.gma_bus_addr;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200937
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000938 for (i = 0; i < intel_private.base.gtt_total_entries; i++) {
Daniel Vetter351bb272010-09-07 22:41:04 +0200939 intel_private.driver->write_entry(intel_private.scratch_page_dma,
940 i, 0);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200941 }
Daniel Vetter351bb272010-09-07 22:41:04 +0200942 readl(intel_private.gtt+i-1); /* PCI Posting. */
Daniel Vetterf51b7662010-04-14 00:29:52 +0200943
944 global_cache_flush();
945
Daniel Vetterf51b7662010-04-14 00:29:52 +0200946 return 0;
947}
948
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200949static bool i830_check_flags(unsigned int flags)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200950{
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200951 switch (flags) {
952 case 0:
953 case AGP_PHYS_MEMORY:
954 case AGP_USER_CACHED_MEMORY:
955 case AGP_USER_MEMORY:
956 return true;
957 }
958
959 return false;
960}
961
Daniel Vetterfefaa702010-09-11 22:12:11 +0200962static void intel_gtt_insert_sg_entries(struct scatterlist *sg_list,
963 unsigned int sg_len,
964 unsigned int pg_start,
965 unsigned int flags)
966{
967 struct scatterlist *sg;
968 unsigned int len, m;
969 int i, j;
970
971 j = pg_start;
972
973 /* sg may merge pages, but we have to separate
974 * per-page addr for GTT */
975 for_each_sg(sg_list, sg, sg_len, i) {
976 len = sg_dma_len(sg) >> PAGE_SHIFT;
977 for (m = 0; m < len; m++) {
978 dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
979 intel_private.driver->write_entry(addr,
980 j, flags);
981 j++;
982 }
983 }
984 readl(intel_private.gtt+j-1);
985}
986
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200987static int intel_fake_agp_insert_entries(struct agp_memory *mem,
988 off_t pg_start, int type)
989{
990 int i, j;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200991 int ret = -EINVAL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200992
993 if (mem->page_count == 0)
994 goto out;
995
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000996 if (pg_start + mem->page_count > intel_private.base.gtt_total_entries)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200997 goto out_err;
998
Daniel Vetterf51b7662010-04-14 00:29:52 +0200999 if (type != mem->type)
1000 goto out_err;
1001
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001002 if (!intel_private.driver->check_flags(type))
Daniel Vetterf51b7662010-04-14 00:29:52 +02001003 goto out_err;
1004
1005 if (!mem->is_flushed)
1006 global_cache_flush();
1007
Daniel Vetterfefaa702010-09-11 22:12:11 +02001008 if (USE_PCI_DMA_API && INTEL_GTT_GEN > 2) {
1009 ret = intel_agp_map_memory(mem);
1010 if (ret != 0)
1011 return ret;
1012
1013 intel_gtt_insert_sg_entries(mem->sg_list, mem->num_sg,
1014 pg_start, type);
1015 } else {
1016 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
1017 dma_addr_t addr = page_to_phys(mem->pages[i]);
1018 intel_private.driver->write_entry(addr,
1019 j, type);
1020 }
1021 readl(intel_private.gtt+j-1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001022 }
Daniel Vetterf51b7662010-04-14 00:29:52 +02001023
1024out:
1025 ret = 0;
1026out_err:
1027 mem->is_flushed = true;
1028 return ret;
1029}
1030
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001031static int intel_fake_agp_remove_entries(struct agp_memory *mem,
1032 off_t pg_start, int type)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001033{
1034 int i;
1035
1036 if (mem->page_count == 0)
1037 return 0;
1038
Daniel Vetterfefaa702010-09-11 22:12:11 +02001039 if (USE_PCI_DMA_API && INTEL_GTT_GEN > 2)
1040 intel_agp_unmap_memory(mem);
1041
Daniel Vetterf51b7662010-04-14 00:29:52 +02001042 for (i = pg_start; i < (mem->page_count + pg_start); i++) {
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001043 intel_private.driver->write_entry(intel_private.scratch_page_dma,
1044 i, 0);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001045 }
Daniel Vetterfdfb58a2010-08-29 00:15:03 +02001046 readl(intel_private.gtt+i-1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001047
Daniel Vetterf51b7662010-04-14 00:29:52 +02001048 return 0;
1049}
1050
Daniel Vetter1b263f22010-09-12 00:27:24 +02001051static void intel_fake_agp_chipset_flush(struct agp_bridge_data *bridge)
1052{
1053 intel_private.driver->chipset_flush();
1054}
1055
Daniel Vetterffdd7512010-08-27 17:51:29 +02001056static struct agp_memory *intel_fake_agp_alloc_by_type(size_t pg_count,
1057 int type)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001058{
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001059 struct agp_memory *new;
1060
1061 if (type == AGP_DCACHE_MEMORY && INTEL_GTT_GEN == 1) {
1062 if (pg_count != intel_private.num_dcache_entries)
1063 return NULL;
1064
1065 new = agp_create_memory(1);
1066 if (new == NULL)
1067 return NULL;
1068
1069 new->type = AGP_DCACHE_MEMORY;
1070 new->page_count = pg_count;
1071 new->num_scratch_pages = 0;
1072 agp_free_page_array(new);
1073 return new;
1074 }
Daniel Vetterf51b7662010-04-14 00:29:52 +02001075 if (type == AGP_PHYS_MEMORY)
1076 return alloc_agpphysmem_i8xx(pg_count, type);
1077 /* always return NULL for other allocation types for now */
1078 return NULL;
1079}
1080
1081static int intel_alloc_chipset_flush_resource(void)
1082{
1083 int ret;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001084 ret = pci_bus_alloc_resource(intel_private.bridge_dev->bus, &intel_private.ifp_resource, PAGE_SIZE,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001085 PAGE_SIZE, PCIBIOS_MIN_MEM, 0,
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001086 pcibios_align_resource, intel_private.bridge_dev);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001087
1088 return ret;
1089}
1090
1091static void intel_i915_setup_chipset_flush(void)
1092{
1093 int ret;
1094 u32 temp;
1095
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001096 pci_read_config_dword(intel_private.bridge_dev, I915_IFPADDR, &temp);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001097 if (!(temp & 0x1)) {
1098 intel_alloc_chipset_flush_resource();
1099 intel_private.resource_valid = 1;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001100 pci_write_config_dword(intel_private.bridge_dev, I915_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001101 } else {
1102 temp &= ~1;
1103
1104 intel_private.resource_valid = 1;
1105 intel_private.ifp_resource.start = temp;
1106 intel_private.ifp_resource.end = temp + PAGE_SIZE;
1107 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
1108 /* some BIOSes reserve this area in a pnp some don't */
1109 if (ret)
1110 intel_private.resource_valid = 0;
1111 }
1112}
1113
1114static void intel_i965_g33_setup_chipset_flush(void)
1115{
1116 u32 temp_hi, temp_lo;
1117 int ret;
1118
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001119 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4, &temp_hi);
1120 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR, &temp_lo);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001121
1122 if (!(temp_lo & 0x1)) {
1123
1124 intel_alloc_chipset_flush_resource();
1125
1126 intel_private.resource_valid = 1;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001127 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001128 upper_32_bits(intel_private.ifp_resource.start));
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001129 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001130 } else {
1131 u64 l64;
1132
1133 temp_lo &= ~0x1;
1134 l64 = ((u64)temp_hi << 32) | temp_lo;
1135
1136 intel_private.resource_valid = 1;
1137 intel_private.ifp_resource.start = l64;
1138 intel_private.ifp_resource.end = l64 + PAGE_SIZE;
1139 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
1140 /* some BIOSes reserve this area in a pnp some don't */
1141 if (ret)
1142 intel_private.resource_valid = 0;
1143 }
1144}
1145
1146static void intel_i9xx_setup_flush(void)
1147{
1148 /* return if already configured */
1149 if (intel_private.ifp_resource.start)
1150 return;
1151
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001152 if (INTEL_GTT_GEN == 6)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001153 return;
1154
1155 /* setup a resource for this object */
1156 intel_private.ifp_resource.name = "Intel Flush Page";
1157 intel_private.ifp_resource.flags = IORESOURCE_MEM;
1158
1159 /* Setup chipset flush for 915 */
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001160 if (IS_G33 || INTEL_GTT_GEN >= 4) {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001161 intel_i965_g33_setup_chipset_flush();
1162 } else {
1163 intel_i915_setup_chipset_flush();
1164 }
1165
Chris Wilsondf51e7a2010-09-04 14:57:27 +01001166 if (intel_private.ifp_resource.start)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001167 intel_private.i9xx_flush_page = ioremap_nocache(intel_private.ifp_resource.start, PAGE_SIZE);
Chris Wilsondf51e7a2010-09-04 14:57:27 +01001168 if (!intel_private.i9xx_flush_page)
1169 dev_err(&intel_private.pcidev->dev,
1170 "can't ioremap flush page - no chipset flushing\n");
Daniel Vetterf51b7662010-04-14 00:29:52 +02001171}
1172
Daniel Vetterae83dd52010-09-12 17:11:15 +02001173static void i9xx_cleanup(void)
1174{
1175 if (intel_private.i9xx_flush_page)
1176 iounmap(intel_private.i9xx_flush_page);
1177 if (intel_private.resource_valid)
1178 release_resource(&intel_private.ifp_resource);
1179 intel_private.ifp_resource.start = 0;
1180 intel_private.resource_valid = 0;
1181}
1182
Daniel Vetter1b263f22010-09-12 00:27:24 +02001183static void i9xx_chipset_flush(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001184{
1185 if (intel_private.i9xx_flush_page)
1186 writel(1, intel_private.i9xx_flush_page);
1187}
1188
Daniel Vettera6963592010-09-11 14:01:43 +02001189static void i965_write_entry(dma_addr_t addr, unsigned int entry,
1190 unsigned int flags)
1191{
1192 /* Shift high bits down */
1193 addr |= (addr >> 28) & 0xf0;
1194 writel(addr | I810_PTE_VALID, intel_private.gtt + entry);
1195}
1196
Daniel Vetter90cb1492010-09-11 23:55:20 +02001197static bool gen6_check_flags(unsigned int flags)
1198{
1199 return true;
1200}
1201
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001202static void gen6_write_entry(dma_addr_t addr, unsigned int entry,
1203 unsigned int flags)
1204{
1205 unsigned int type_mask = flags & ~AGP_USER_CACHED_MEMORY_GFDT;
1206 unsigned int gfdt = flags & AGP_USER_CACHED_MEMORY_GFDT;
1207 u32 pte_flags;
1208
Zhenyu Wang897ef192010-11-02 17:30:47 +08001209 if (type_mask == AGP_USER_MEMORY)
Chris Wilson85ccc352010-10-22 14:59:29 +01001210 pte_flags = GEN6_PTE_UNCACHED | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001211 else if (type_mask == AGP_USER_CACHED_MEMORY_LLC_MLC) {
Zhenyu Wangd1108522010-11-02 17:30:46 +08001212 pte_flags = GEN6_PTE_LLC_MLC | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001213 if (gfdt)
1214 pte_flags |= GEN6_PTE_GFDT;
1215 } else { /* set 'normal'/'cached' to LLC by default */
Zhenyu Wangd1108522010-11-02 17:30:46 +08001216 pte_flags = GEN6_PTE_LLC | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001217 if (gfdt)
1218 pte_flags |= GEN6_PTE_GFDT;
1219 }
1220
1221 /* gen6 has bit11-4 for physical addr bit39-32 */
1222 addr |= (addr >> 28) & 0xff0;
1223 writel(addr | pte_flags, intel_private.gtt + entry);
1224}
1225
Daniel Vetterae83dd52010-09-12 17:11:15 +02001226static void gen6_cleanup(void)
1227{
1228}
1229
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001230static int i9xx_setup(void)
1231{
1232 u32 reg_addr;
1233
1234 pci_read_config_dword(intel_private.pcidev, I915_MMADDR, &reg_addr);
1235
1236 reg_addr &= 0xfff80000;
1237
1238 intel_private.registers = ioremap(reg_addr, 128 * 4096);
1239 if (!intel_private.registers)
1240 return -ENOMEM;
1241
1242 if (INTEL_GTT_GEN == 3) {
1243 u32 gtt_addr;
Chris Wilson3f08e4e2010-09-14 20:15:22 +01001244
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001245 pci_read_config_dword(intel_private.pcidev,
1246 I915_PTEADDR, &gtt_addr);
1247 intel_private.gtt_bus_addr = gtt_addr;
1248 } else {
1249 u32 gtt_offset;
1250
1251 switch (INTEL_GTT_GEN) {
1252 case 5:
1253 case 6:
1254 gtt_offset = MB(2);
1255 break;
1256 case 4:
1257 default:
1258 gtt_offset = KB(512);
1259 break;
1260 }
1261 intel_private.gtt_bus_addr = reg_addr + gtt_offset;
1262 }
1263
1264 intel_i9xx_setup_flush();
1265
1266 return 0;
1267}
1268
Daniel Vetterf51b7662010-04-14 00:29:52 +02001269static const struct agp_bridge_driver intel_810_driver = {
1270 .owner = THIS_MODULE,
1271 .aperture_sizes = intel_i810_sizes,
1272 .size_type = FIXED_APER_SIZE,
1273 .num_aperture_sizes = 2,
1274 .needs_scratch_page = true,
1275 .configure = intel_i810_configure,
1276 .fetch_size = intel_i810_fetch_size,
1277 .cleanup = intel_i810_cleanup,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001278 .mask_memory = intel_i810_mask_memory,
1279 .masks = intel_i810_masks,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001280 .agp_enable = intel_fake_agp_enable,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001281 .cache_flush = global_cache_flush,
1282 .create_gatt_table = agp_generic_create_gatt_table,
1283 .free_gatt_table = agp_generic_free_gatt_table,
1284 .insert_memory = intel_i810_insert_entries,
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001285 .remove_memory = intel_fake_agp_remove_entries,
1286 .alloc_by_type = intel_fake_agp_alloc_by_type,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001287 .free_by_type = intel_i810_free_by_type,
1288 .agp_alloc_page = agp_generic_alloc_page,
1289 .agp_alloc_pages = agp_generic_alloc_pages,
1290 .agp_destroy_page = agp_generic_destroy_page,
1291 .agp_destroy_pages = agp_generic_destroy_pages,
1292 .agp_type_to_mask_type = agp_generic_type_to_mask_type,
1293};
1294
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001295static const struct agp_bridge_driver intel_fake_agp_driver = {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001296 .owner = THIS_MODULE,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001297 .size_type = FIXED_APER_SIZE,
Chris Wilson9e76e7b2010-09-14 12:12:11 +01001298 .aperture_sizes = intel_fake_agp_sizes,
1299 .num_aperture_sizes = ARRAY_SIZE(intel_fake_agp_sizes),
Daniel Vettera6963592010-09-11 14:01:43 +02001300 .configure = intel_fake_agp_configure,
Daniel Vetter3e921f92010-08-27 15:33:26 +02001301 .fetch_size = intel_fake_agp_fetch_size,
Daniel Vetterfdfb58a2010-08-29 00:15:03 +02001302 .cleanup = intel_gtt_cleanup,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001303 .agp_enable = intel_fake_agp_enable,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001304 .cache_flush = global_cache_flush,
Daniel Vetter3b15a9d2010-08-29 14:18:49 +02001305 .create_gatt_table = intel_fake_agp_create_gatt_table,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001306 .free_gatt_table = intel_fake_agp_free_gatt_table,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001307 .insert_memory = intel_fake_agp_insert_entries,
1308 .remove_memory = intel_fake_agp_remove_entries,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001309 .alloc_by_type = intel_fake_agp_alloc_by_type,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001310 .free_by_type = intel_i810_free_by_type,
1311 .agp_alloc_page = agp_generic_alloc_page,
1312 .agp_alloc_pages = agp_generic_alloc_pages,
1313 .agp_destroy_page = agp_generic_destroy_page,
1314 .agp_destroy_pages = agp_generic_destroy_pages,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001315 .chipset_flush = intel_fake_agp_chipset_flush,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001316};
Daniel Vetter02c026c2010-08-24 19:39:48 +02001317
Daniel Vetterbdd30722010-09-12 12:34:44 +02001318static const struct intel_gtt_driver i81x_gtt_driver = {
1319 .gen = 1,
Daniel Vetter22533b42010-09-12 16:38:55 +02001320 .dma_mask_size = 32,
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001321 .check_flags = i830_check_flags,
1322 .write_entry = i810_write_entry,
Daniel Vetterbdd30722010-09-12 12:34:44 +02001323};
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001324static const struct intel_gtt_driver i8xx_gtt_driver = {
1325 .gen = 2,
Chris Wilson100519e2010-10-31 10:37:02 +00001326 .has_pgtbl_enable = 1,
Daniel Vetter73800422010-08-29 17:29:50 +02001327 .setup = i830_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001328 .cleanup = i830_cleanup,
Daniel Vetter351bb272010-09-07 22:41:04 +02001329 .write_entry = i830_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001330 .dma_mask_size = 32,
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001331 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001332 .chipset_flush = i830_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001333};
1334static const struct intel_gtt_driver i915_gtt_driver = {
1335 .gen = 3,
Chris Wilson100519e2010-10-31 10:37:02 +00001336 .has_pgtbl_enable = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001337 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001338 .cleanup = i9xx_cleanup,
Daniel Vetter351bb272010-09-07 22:41:04 +02001339 /* i945 is the last gpu to need phys mem (for overlay and cursors). */
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001340 .write_entry = i830_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001341 .dma_mask_size = 32,
Daniel Vetterfefaa702010-09-11 22:12:11 +02001342 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001343 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001344};
1345static const struct intel_gtt_driver g33_gtt_driver = {
1346 .gen = 3,
1347 .is_g33 = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001348 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001349 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001350 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001351 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001352 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001353 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001354};
1355static const struct intel_gtt_driver pineview_gtt_driver = {
1356 .gen = 3,
1357 .is_pineview = 1, .is_g33 = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001358 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001359 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001360 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001361 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001362 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001363 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001364};
1365static const struct intel_gtt_driver i965_gtt_driver = {
1366 .gen = 4,
Chris Wilson100519e2010-10-31 10:37:02 +00001367 .has_pgtbl_enable = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001368 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001369 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001370 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001371 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001372 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001373 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001374};
1375static const struct intel_gtt_driver g4x_gtt_driver = {
1376 .gen = 5,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001377 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001378 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001379 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001380 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001381 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001382 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001383};
1384static const struct intel_gtt_driver ironlake_gtt_driver = {
1385 .gen = 5,
1386 .is_ironlake = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001387 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001388 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001389 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001390 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001391 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001392 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001393};
1394static const struct intel_gtt_driver sandybridge_gtt_driver = {
1395 .gen = 6,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001396 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001397 .cleanup = gen6_cleanup,
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001398 .write_entry = gen6_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001399 .dma_mask_size = 40,
Daniel Vetter90cb1492010-09-11 23:55:20 +02001400 .check_flags = gen6_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001401 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001402};
1403
Daniel Vetter02c026c2010-08-24 19:39:48 +02001404/* Table to describe Intel GMCH and AGP/PCIE GART drivers. At least one of
1405 * driver and gmch_driver must be non-null, and find_gmch will determine
1406 * which one should be used if a gmch_chip_id is present.
1407 */
1408static const struct intel_gtt_driver_description {
1409 unsigned int gmch_chip_id;
1410 char *name;
1411 const struct agp_bridge_driver *gmch_driver;
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001412 const struct intel_gtt_driver *gtt_driver;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001413} intel_gtt_chipsets[] = {
Daniel Vetterbdd30722010-09-12 12:34:44 +02001414 { PCI_DEVICE_ID_INTEL_82810_IG1, "i810", &intel_810_driver,
1415 &i81x_gtt_driver},
1416 { PCI_DEVICE_ID_INTEL_82810_IG3, "i810", &intel_810_driver,
1417 &i81x_gtt_driver},
1418 { PCI_DEVICE_ID_INTEL_82810E_IG, "i810", &intel_810_driver,
1419 &i81x_gtt_driver},
1420 { PCI_DEVICE_ID_INTEL_82815_CGC, "i815", &intel_810_driver,
1421 &i81x_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001422 { PCI_DEVICE_ID_INTEL_82830_CGC, "830M",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001423 &intel_fake_agp_driver, &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001424 { PCI_DEVICE_ID_INTEL_82845G_IG, "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_82854_IG, "854",
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_82855GM_IG, "855GM",
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_82865_IG, "865",
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_E7221_IG, "E7221 (i915)",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001433 &intel_fake_agp_driver, &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001434 { PCI_DEVICE_ID_INTEL_82915G_IG, "915G",
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_82915GM_IG, "915GM",
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_82945G_IG, "945G",
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_82945GM_IG, "945GM",
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_82945GME_IG, "945GME",
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_82946GZ_IG, "946GZ",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001445 &intel_fake_agp_driver, &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001446 { PCI_DEVICE_ID_INTEL_82G35_IG, "G35",
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_82965Q_IG, "965Q",
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_82965G_IG, "965G",
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_82965GM_IG, "965GM",
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_82965GME_IG, "965GME/GLE",
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_G33_IG, "G33",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001457 &intel_fake_agp_driver, &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001458 { PCI_DEVICE_ID_INTEL_Q35_IG, "Q35",
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_Q33_IG, "Q33",
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_PINEVIEW_M_IG, "GMA3150",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001463 &intel_fake_agp_driver, &pineview_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001464 { PCI_DEVICE_ID_INTEL_PINEVIEW_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_GM45_IG, "GM45",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001467 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001468 { PCI_DEVICE_ID_INTEL_EAGLELAKE_IG, "Eaglelake",
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_Q45_IG, "Q45/Q43",
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_G45_IG, "G45/G43",
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_B43_IG, "B43",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001475 &intel_fake_agp_driver, &g4x_gtt_driver },
Chris Wilsone9e5f8e2010-09-21 11:19:32 +01001476 { PCI_DEVICE_ID_INTEL_B43_1_IG, "B43",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001477 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001478 { PCI_DEVICE_ID_INTEL_G41_IG, "G41",
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001479 &intel_fake_agp_driver, &g4x_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001480 { PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001481 "HD Graphics", &intel_fake_agp_driver, &ironlake_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001482 { PCI_DEVICE_ID_INTEL_IRONLAKE_M_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_SANDYBRIDGE_GT1_IG,
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001485 "Sandybridge", &intel_fake_agp_driver, &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001486 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_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_PLUS_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_M_GT1_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_GT2_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_PLUS_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_S_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 { 0, NULL, NULL }
1499};
1500
1501static int find_gmch(u16 device)
1502{
1503 struct pci_dev *gmch_device;
1504
1505 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL, device, NULL);
1506 if (gmch_device && PCI_FUNC(gmch_device->devfn) != 0) {
1507 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL,
1508 device, gmch_device);
1509 }
1510
1511 if (!gmch_device)
1512 return 0;
1513
1514 intel_private.pcidev = gmch_device;
1515 return 1;
1516}
1517
Daniel Vettere2404e72010-09-08 17:29:51 +02001518int intel_gmch_probe(struct pci_dev *pdev,
Daniel Vetter02c026c2010-08-24 19:39:48 +02001519 struct agp_bridge_data *bridge)
1520{
1521 int i, mask;
1522 bridge->driver = NULL;
1523
1524 for (i = 0; intel_gtt_chipsets[i].name != NULL; i++) {
1525 if (find_gmch(intel_gtt_chipsets[i].gmch_chip_id)) {
1526 bridge->driver =
1527 intel_gtt_chipsets[i].gmch_driver;
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001528 intel_private.driver =
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001529 intel_gtt_chipsets[i].gtt_driver;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001530 break;
1531 }
1532 }
1533
1534 if (!bridge->driver)
1535 return 0;
1536
1537 bridge->dev_private_data = &intel_private;
1538 bridge->dev = pdev;
1539
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001540 intel_private.bridge_dev = pci_dev_get(pdev);
1541
Daniel Vetter02c026c2010-08-24 19:39:48 +02001542 dev_info(&pdev->dev, "Intel %s Chipset\n", intel_gtt_chipsets[i].name);
1543
Daniel Vetter22533b42010-09-12 16:38:55 +02001544 mask = intel_private.driver->dma_mask_size;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001545 if (pci_set_dma_mask(intel_private.pcidev, DMA_BIT_MASK(mask)))
1546 dev_err(&intel_private.pcidev->dev,
1547 "set gfx device dma mask %d-bit failed!\n", mask);
1548 else
1549 pci_set_consistent_dma_mask(intel_private.pcidev,
1550 DMA_BIT_MASK(mask));
1551
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001552 if (bridge->driver == &intel_810_driver)
1553 return 1;
1554
Daniel Vetter3b15a9d2010-08-29 14:18:49 +02001555 if (intel_gtt_init() != 0)
1556 return 0;
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001557
Daniel Vetter02c026c2010-08-24 19:39:48 +02001558 return 1;
1559}
Daniel Vettere2404e72010-09-08 17:29:51 +02001560EXPORT_SYMBOL(intel_gmch_probe);
Daniel Vetter02c026c2010-08-24 19:39:48 +02001561
Chris Wilsonc64f7ba2010-11-23 14:24:24 +00001562const struct intel_gtt *intel_gtt_get(void)
Daniel Vetter19966752010-09-06 20:08:44 +02001563{
1564 return &intel_private.base;
1565}
1566EXPORT_SYMBOL(intel_gtt_get);
1567
Daniel Vettere2404e72010-09-08 17:29:51 +02001568void intel_gmch_remove(struct pci_dev *pdev)
Daniel Vetter02c026c2010-08-24 19:39:48 +02001569{
1570 if (intel_private.pcidev)
1571 pci_dev_put(intel_private.pcidev);
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001572 if (intel_private.bridge_dev)
1573 pci_dev_put(intel_private.bridge_dev);
Daniel Vetter02c026c2010-08-24 19:39:48 +02001574}
Daniel Vettere2404e72010-09-08 17:29:51 +02001575EXPORT_SYMBOL(intel_gmch_remove);
1576
1577MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
1578MODULE_LICENSE("GPL and additional rights");