blob: 291ac5113576fea3b8a05afe7bd111e3c7384b7b [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"
Daniel Vetter0ade6382010-08-24 22:18:41 +020027#include <drm/intel-gtt.h>
Daniel Vettere2404e72010-09-08 17:29:51 +020028
Daniel Vetterf51b7662010-04-14 00:29:52 +020029/*
30 * If we have Intel graphics, we're not going to have anything other than
31 * an Intel IOMMU. So make the correct use of the PCI DMA API contingent
32 * on the Intel IOMMU support (CONFIG_DMAR).
33 * Only newer chipsets need to bother with this, of course.
34 */
35#ifdef CONFIG_DMAR
36#define USE_PCI_DMA_API 1
Daniel Vetter0e87d2b2010-09-07 22:11:15 +020037#else
38#define USE_PCI_DMA_API 0
Daniel Vetterf51b7662010-04-14 00:29:52 +020039#endif
40
Daniel Vetter1a997ff2010-09-08 21:18:53 +020041struct intel_gtt_driver {
42 unsigned int gen : 8;
43 unsigned int is_g33 : 1;
44 unsigned int is_pineview : 1;
45 unsigned int is_ironlake : 1;
Chris Wilson100519e2010-10-31 10:37:02 +000046 unsigned int has_pgtbl_enable : 1;
Daniel Vetter22533b42010-09-12 16:38:55 +020047 unsigned int dma_mask_size : 8;
Daniel Vetter73800422010-08-29 17:29:50 +020048 /* Chipset specific GTT setup */
49 int (*setup)(void);
Daniel Vetterae83dd52010-09-12 17:11:15 +020050 /* This should undo anything done in ->setup() save the unmapping
51 * of the mmio register file, that's done in the generic code. */
52 void (*cleanup)(void);
Daniel Vetter351bb272010-09-07 22:41:04 +020053 void (*write_entry)(dma_addr_t addr, unsigned int entry, unsigned int flags);
54 /* Flags is a more or less chipset specific opaque value.
55 * For chipsets that need to support old ums (non-gem) code, this
56 * needs to be identical to the various supported agp memory types! */
Daniel Vetter5cbecaf2010-09-11 21:31:04 +020057 bool (*check_flags)(unsigned int flags);
Daniel Vetter1b263f22010-09-12 00:27:24 +020058 void (*chipset_flush)(void);
Daniel Vetter1a997ff2010-09-08 21:18:53 +020059};
60
Daniel Vetterf51b7662010-04-14 00:29:52 +020061static struct _intel_private {
Daniel Vetter0ade6382010-08-24 22:18:41 +020062 struct intel_gtt base;
Daniel Vetter1a997ff2010-09-08 21:18:53 +020063 const struct intel_gtt_driver *driver;
Daniel Vetterf51b7662010-04-14 00:29:52 +020064 struct pci_dev *pcidev; /* device one */
Daniel Vetterd7cca2f2010-08-24 23:06:19 +020065 struct pci_dev *bridge_dev;
Daniel Vetterf51b7662010-04-14 00:29:52 +020066 u8 __iomem *registers;
Daniel Vetterf67eab62010-08-29 17:27:36 +020067 phys_addr_t gtt_bus_addr;
Daniel Vetter73800422010-08-29 17:29:50 +020068 phys_addr_t gma_bus_addr;
Daniel Vetterb3eafc52010-09-23 20:04:17 +020069 u32 PGETBL_save;
Daniel Vetterf51b7662010-04-14 00:29:52 +020070 u32 __iomem *gtt; /* I915G */
71 int num_dcache_entries;
Daniel Vetterf51b7662010-04-14 00:29:52 +020072 union {
73 void __iomem *i9xx_flush_page;
74 void *i8xx_flush_page;
75 };
Daniel Vetter820647b2010-11-05 13:30:14 +010076 char *i81x_gtt_table;
Daniel Vetterf51b7662010-04-14 00:29:52 +020077 struct page *i8xx_page;
78 struct resource ifp_resource;
79 int resource_valid;
Daniel Vetter0e87d2b2010-09-07 22:11:15 +020080 struct page *scratch_page;
81 dma_addr_t scratch_page_dma;
Daniel Vetterf51b7662010-04-14 00:29:52 +020082} intel_private;
83
Daniel Vetter1a997ff2010-09-08 21:18:53 +020084#define INTEL_GTT_GEN intel_private.driver->gen
85#define IS_G33 intel_private.driver->is_g33
86#define IS_PINEVIEW intel_private.driver->is_pineview
87#define IS_IRONLAKE intel_private.driver->is_ironlake
Chris Wilson100519e2010-10-31 10:37:02 +000088#define HAS_PGTBL_EN intel_private.driver->has_pgtbl_enable
Daniel Vetter1a997ff2010-09-08 21:18:53 +020089
Daniel Vetterf51b7662010-04-14 00:29:52 +020090static void intel_agp_free_sglist(struct agp_memory *mem)
91{
92 struct sg_table st;
93
94 st.sgl = mem->sg_list;
95 st.orig_nents = st.nents = mem->page_count;
96
97 sg_free_table(&st);
98
99 mem->sg_list = NULL;
100 mem->num_sg = 0;
101}
102
103static int intel_agp_map_memory(struct agp_memory *mem)
104{
105 struct sg_table st;
106 struct scatterlist *sg;
107 int i;
108
Daniel Vetterfefaa702010-09-11 22:12:11 +0200109 if (mem->sg_list)
110 return 0; /* already mapped (for e.g. resume */
111
Daniel Vetterf51b7662010-04-14 00:29:52 +0200112 DBG("try mapping %lu pages\n", (unsigned long)mem->page_count);
113
114 if (sg_alloc_table(&st, mem->page_count, GFP_KERNEL))
Chris Wilson831cd442010-07-24 18:29:37 +0100115 goto err;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200116
117 mem->sg_list = sg = st.sgl;
118
119 for (i = 0 ; i < mem->page_count; i++, sg = sg_next(sg))
120 sg_set_page(sg, mem->pages[i], PAGE_SIZE, 0);
121
122 mem->num_sg = pci_map_sg(intel_private.pcidev, mem->sg_list,
123 mem->page_count, PCI_DMA_BIDIRECTIONAL);
Chris Wilson831cd442010-07-24 18:29:37 +0100124 if (unlikely(!mem->num_sg))
125 goto err;
126
Daniel Vetterf51b7662010-04-14 00:29:52 +0200127 return 0;
Chris Wilson831cd442010-07-24 18:29:37 +0100128
129err:
130 sg_free_table(&st);
131 return -ENOMEM;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200132}
133
134static void intel_agp_unmap_memory(struct agp_memory *mem)
135{
136 DBG("try unmapping %lu pages\n", (unsigned long)mem->page_count);
137
138 pci_unmap_sg(intel_private.pcidev, mem->sg_list,
139 mem->page_count, PCI_DMA_BIDIRECTIONAL);
140 intel_agp_free_sglist(mem);
141}
142
Daniel Vetterffdd7512010-08-27 17:51:29 +0200143static void intel_fake_agp_enable(struct agp_bridge_data *bridge, u32 mode)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200144{
145 return;
146}
147
148/* Exists to support ARGB cursors */
149static struct page *i8xx_alloc_pages(void)
150{
151 struct page *page;
152
153 page = alloc_pages(GFP_KERNEL | GFP_DMA32, 2);
154 if (page == NULL)
155 return NULL;
156
157 if (set_pages_uc(page, 4) < 0) {
158 set_pages_wb(page, 4);
159 __free_pages(page, 2);
160 return NULL;
161 }
162 get_page(page);
163 atomic_inc(&agp_bridge->current_memory_agp);
164 return page;
165}
166
167static void i8xx_destroy_pages(struct page *page)
168{
169 if (page == NULL)
170 return;
171
172 set_pages_wb(page, 4);
173 put_page(page);
174 __free_pages(page, 2);
175 atomic_dec(&agp_bridge->current_memory_agp);
176}
177
Daniel Vetter820647b2010-11-05 13:30:14 +0100178#define I810_GTT_ORDER 4
179static int i810_setup(void)
180{
181 u32 reg_addr;
182 char *gtt_table;
183
184 /* i81x does not preallocate the gtt. It's always 64kb in size. */
185 gtt_table = alloc_gatt_pages(I810_GTT_ORDER);
186 if (gtt_table == NULL)
187 return -ENOMEM;
188 intel_private.i81x_gtt_table = gtt_table;
189
190 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &reg_addr);
191 reg_addr &= 0xfff80000;
192
193 intel_private.registers = ioremap(reg_addr, KB(64));
194 if (!intel_private.registers)
195 return -ENOMEM;
196
197 writel(virt_to_phys(gtt_table) | I810_PGETBL_ENABLED,
198 intel_private.registers+I810_PGETBL_CTL);
199
200 intel_private.gtt_bus_addr = reg_addr + I810_PTE_BASE;
201
202 if ((readl(intel_private.registers+I810_DRAM_CTL)
203 & I810_DRAM_ROW_0) == I810_DRAM_ROW_0_SDRAM) {
204 dev_info(&intel_private.pcidev->dev,
205 "detected 4MB dedicated video ram\n");
206 intel_private.num_dcache_entries = 1024;
207 }
208
209 return 0;
210}
211
212static void i810_cleanup(void)
213{
214 writel(0, intel_private.registers+I810_PGETBL_CTL);
215 free_gatt_pages(intel_private.i81x_gtt_table, I810_GTT_ORDER);
216}
217
Daniel Vetterff268602010-11-05 15:43:35 +0100218static int i810_insert_dcache_entries(struct agp_memory *mem, off_t pg_start,
219 int type)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200220{
Daniel Vetterf51b7662010-04-14 00:29:52 +0200221 int i;
222
Daniel Vetterff268602010-11-05 15:43:35 +0100223 if ((pg_start + mem->page_count)
224 > intel_private.num_dcache_entries)
225 return -EINVAL;
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100226
Daniel Vetterff268602010-11-05 15:43:35 +0100227 if (!mem->is_flushed)
228 global_cache_flush();
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100229
Daniel Vetterff268602010-11-05 15:43:35 +0100230 for (i = pg_start; i < (pg_start + mem->page_count); i++) {
231 dma_addr_t addr = i << PAGE_SHIFT;
232 intel_private.driver->write_entry(addr,
233 i, type);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200234 }
Daniel Vetterff268602010-11-05 15:43:35 +0100235 readl(intel_private.gtt+i-1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200236
Daniel Vetterff268602010-11-05 15:43:35 +0100237 return 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200238}
239
240/*
241 * The i810/i830 requires a physical address to program its mouse
242 * pointer into hardware.
243 * However the Xserver still writes to it through the agp aperture.
244 */
245static struct agp_memory *alloc_agpphysmem_i8xx(size_t pg_count, int type)
246{
247 struct agp_memory *new;
248 struct page *page;
249
250 switch (pg_count) {
251 case 1: page = agp_bridge->driver->agp_alloc_page(agp_bridge);
252 break;
253 case 4:
254 /* kludge to get 4 physical pages for ARGB cursor */
255 page = i8xx_alloc_pages();
256 break;
257 default:
258 return NULL;
259 }
260
261 if (page == NULL)
262 return NULL;
263
264 new = agp_create_memory(pg_count);
265 if (new == NULL)
266 return NULL;
267
268 new->pages[0] = page;
269 if (pg_count == 4) {
270 /* kludge to get 4 physical pages for ARGB cursor */
271 new->pages[1] = new->pages[0] + 1;
272 new->pages[2] = new->pages[1] + 1;
273 new->pages[3] = new->pages[2] + 1;
274 }
275 new->page_count = pg_count;
276 new->num_scratch_pages = pg_count;
277 new->type = AGP_PHYS_MEMORY;
278 new->physical = page_to_phys(new->pages[0]);
279 return new;
280}
281
Daniel Vetterf51b7662010-04-14 00:29:52 +0200282static void intel_i810_free_by_type(struct agp_memory *curr)
283{
284 agp_free_key(curr->key);
285 if (curr->type == AGP_PHYS_MEMORY) {
286 if (curr->page_count == 4)
287 i8xx_destroy_pages(curr->pages[0]);
288 else {
289 agp_bridge->driver->agp_destroy_page(curr->pages[0],
290 AGP_PAGE_DESTROY_UNMAP);
291 agp_bridge->driver->agp_destroy_page(curr->pages[0],
292 AGP_PAGE_DESTROY_FREE);
293 }
294 agp_free_page_array(curr);
295 }
296 kfree(curr);
297}
298
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200299static int intel_gtt_setup_scratch_page(void)
300{
301 struct page *page;
302 dma_addr_t dma_addr;
303
304 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
305 if (page == NULL)
306 return -ENOMEM;
307 get_page(page);
308 set_pages_uc(page, 1);
309
310 if (USE_PCI_DMA_API && INTEL_GTT_GEN > 2) {
311 dma_addr = pci_map_page(intel_private.pcidev, page, 0,
312 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
313 if (pci_dma_mapping_error(intel_private.pcidev, dma_addr))
314 return -EINVAL;
315
316 intel_private.scratch_page_dma = dma_addr;
317 } else
318 intel_private.scratch_page_dma = page_to_phys(page);
319
320 intel_private.scratch_page = page;
321
322 return 0;
323}
324
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100325static void i810_write_entry(dma_addr_t addr, unsigned int entry,
326 unsigned int flags)
327{
328 u32 pte_flags = I810_PTE_VALID;
329
330 switch (flags) {
331 case AGP_DCACHE_MEMORY:
332 pte_flags |= I810_PTE_LOCAL;
333 break;
334 case AGP_USER_CACHED_MEMORY:
335 pte_flags |= I830_PTE_SYSTEM_CACHED;
336 break;
337 }
338
339 writel(addr | pte_flags, intel_private.gtt + entry);
340}
341
Chris Wilson9e76e7b82010-09-14 12:12:11 +0100342static const struct aper_size_info_fixed const intel_fake_agp_sizes[] = {
Daniel Vetter820647b2010-11-05 13:30:14 +0100343 {32, 8192, 3},
344 {64, 16384, 4},
Daniel Vetterf51b7662010-04-14 00:29:52 +0200345 {128, 32768, 5},
Daniel Vetterf51b7662010-04-14 00:29:52 +0200346 {256, 65536, 6},
347 {512, 131072, 7},
348};
349
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000350static unsigned int intel_gtt_stolen_size(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200351{
352 u16 gmch_ctrl;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200353 u8 rdct;
354 int local = 0;
355 static const int ddt[4] = { 0, 16, 32, 64 };
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200356 unsigned int stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200357
Daniel Vetter820647b2010-11-05 13:30:14 +0100358 if (INTEL_GTT_GEN == 1)
359 return 0; /* no stolen mem on i81x */
360
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200361 pci_read_config_word(intel_private.bridge_dev,
362 I830_GMCH_CTRL, &gmch_ctrl);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200363
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200364 if (intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82830_HB ||
365 intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82845G_HB) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200366 switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
367 case I830_GMCH_GMS_STOLEN_512:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200368 stolen_size = KB(512);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200369 break;
370 case I830_GMCH_GMS_STOLEN_1024:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200371 stolen_size = MB(1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200372 break;
373 case I830_GMCH_GMS_STOLEN_8192:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200374 stolen_size = MB(8);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200375 break;
376 case I830_GMCH_GMS_LOCAL:
377 rdct = readb(intel_private.registers+I830_RDRAM_CHANNEL_TYPE);
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200378 stolen_size = (I830_RDRAM_ND(rdct) + 1) *
Daniel Vetterf51b7662010-04-14 00:29:52 +0200379 MB(ddt[I830_RDRAM_DDT(rdct)]);
380 local = 1;
381 break;
382 default:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200383 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200384 break;
385 }
Daniel Vetter1a997ff2010-09-08 21:18:53 +0200386 } else if (INTEL_GTT_GEN == 6) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200387 /*
388 * SandyBridge has new memory control reg at 0x50.w
389 */
390 u16 snb_gmch_ctl;
391 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl);
392 switch (snb_gmch_ctl & SNB_GMCH_GMS_STOLEN_MASK) {
393 case SNB_GMCH_GMS_STOLEN_32M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200394 stolen_size = MB(32);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200395 break;
396 case SNB_GMCH_GMS_STOLEN_64M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200397 stolen_size = MB(64);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200398 break;
399 case SNB_GMCH_GMS_STOLEN_96M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200400 stolen_size = MB(96);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200401 break;
402 case SNB_GMCH_GMS_STOLEN_128M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200403 stolen_size = MB(128);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200404 break;
405 case SNB_GMCH_GMS_STOLEN_160M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200406 stolen_size = MB(160);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200407 break;
408 case SNB_GMCH_GMS_STOLEN_192M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200409 stolen_size = MB(192);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200410 break;
411 case SNB_GMCH_GMS_STOLEN_224M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200412 stolen_size = MB(224);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200413 break;
414 case SNB_GMCH_GMS_STOLEN_256M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200415 stolen_size = MB(256);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200416 break;
417 case SNB_GMCH_GMS_STOLEN_288M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200418 stolen_size = MB(288);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200419 break;
420 case SNB_GMCH_GMS_STOLEN_320M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200421 stolen_size = MB(320);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200422 break;
423 case SNB_GMCH_GMS_STOLEN_352M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200424 stolen_size = MB(352);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200425 break;
426 case SNB_GMCH_GMS_STOLEN_384M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200427 stolen_size = MB(384);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200428 break;
429 case SNB_GMCH_GMS_STOLEN_416M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200430 stolen_size = MB(416);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200431 break;
432 case SNB_GMCH_GMS_STOLEN_448M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200433 stolen_size = MB(448);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200434 break;
435 case SNB_GMCH_GMS_STOLEN_480M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200436 stolen_size = MB(480);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200437 break;
438 case SNB_GMCH_GMS_STOLEN_512M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200439 stolen_size = MB(512);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200440 break;
441 }
442 } else {
443 switch (gmch_ctrl & I855_GMCH_GMS_MASK) {
444 case I855_GMCH_GMS_STOLEN_1M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200445 stolen_size = MB(1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200446 break;
447 case I855_GMCH_GMS_STOLEN_4M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200448 stolen_size = MB(4);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200449 break;
450 case I855_GMCH_GMS_STOLEN_8M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200451 stolen_size = MB(8);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200452 break;
453 case I855_GMCH_GMS_STOLEN_16M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200454 stolen_size = MB(16);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200455 break;
456 case I855_GMCH_GMS_STOLEN_32M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200457 stolen_size = MB(32);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200458 break;
459 case I915_GMCH_GMS_STOLEN_48M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200460 stolen_size = MB(48);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200461 break;
462 case I915_GMCH_GMS_STOLEN_64M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200463 stolen_size = MB(64);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200464 break;
465 case G33_GMCH_GMS_STOLEN_128M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200466 stolen_size = MB(128);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200467 break;
468 case G33_GMCH_GMS_STOLEN_256M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200469 stolen_size = MB(256);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200470 break;
471 case INTEL_GMCH_GMS_STOLEN_96M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200472 stolen_size = MB(96);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200473 break;
474 case INTEL_GMCH_GMS_STOLEN_160M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200475 stolen_size = MB(160);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200476 break;
477 case INTEL_GMCH_GMS_STOLEN_224M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200478 stolen_size = MB(224);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200479 break;
480 case INTEL_GMCH_GMS_STOLEN_352M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200481 stolen_size = MB(352);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200482 break;
483 default:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200484 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200485 break;
486 }
487 }
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200488
Chris Wilson1b6064d2010-11-23 12:33:54 +0000489 if (stolen_size > 0) {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200490 dev_info(&intel_private.bridge_dev->dev, "detected %dK %s memory\n",
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200491 stolen_size / KB(1), local ? "local" : "stolen");
Daniel Vetterf51b7662010-04-14 00:29:52 +0200492 } else {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200493 dev_info(&intel_private.bridge_dev->dev,
Daniel Vetterf51b7662010-04-14 00:29:52 +0200494 "no pre-allocated video memory detected\n");
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200495 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200496 }
497
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000498 return stolen_size;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200499}
500
Daniel Vetter20172842010-09-24 18:25:59 +0200501static void i965_adjust_pgetbl_size(unsigned int size_flag)
502{
503 u32 pgetbl_ctl, pgetbl_ctl2;
504
505 /* ensure that ppgtt is disabled */
506 pgetbl_ctl2 = readl(intel_private.registers+I965_PGETBL_CTL2);
507 pgetbl_ctl2 &= ~I810_PGETBL_ENABLED;
508 writel(pgetbl_ctl2, intel_private.registers+I965_PGETBL_CTL2);
509
510 /* write the new ggtt size */
511 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
512 pgetbl_ctl &= ~I965_PGETBL_SIZE_MASK;
513 pgetbl_ctl |= size_flag;
514 writel(pgetbl_ctl, intel_private.registers+I810_PGETBL_CTL);
515}
516
517static unsigned int i965_gtt_total_entries(void)
518{
519 int size;
520 u32 pgetbl_ctl;
521 u16 gmch_ctl;
522
523 pci_read_config_word(intel_private.bridge_dev,
524 I830_GMCH_CTRL, &gmch_ctl);
525
526 if (INTEL_GTT_GEN == 5) {
527 switch (gmch_ctl & G4x_GMCH_SIZE_MASK) {
528 case G4x_GMCH_SIZE_1M:
529 case G4x_GMCH_SIZE_VT_1M:
530 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1MB);
531 break;
532 case G4x_GMCH_SIZE_VT_1_5M:
533 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1_5MB);
534 break;
535 case G4x_GMCH_SIZE_2M:
536 case G4x_GMCH_SIZE_VT_2M:
537 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_2MB);
538 break;
539 }
540 }
541
542 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
543
544 switch (pgetbl_ctl & I965_PGETBL_SIZE_MASK) {
545 case I965_PGETBL_SIZE_128KB:
546 size = KB(128);
547 break;
548 case I965_PGETBL_SIZE_256KB:
549 size = KB(256);
550 break;
551 case I965_PGETBL_SIZE_512KB:
552 size = KB(512);
553 break;
554 /* GTT pagetable sizes bigger than 512KB are not possible on G33! */
555 case I965_PGETBL_SIZE_1MB:
556 size = KB(1024);
557 break;
558 case I965_PGETBL_SIZE_2MB:
559 size = KB(2048);
560 break;
561 case I965_PGETBL_SIZE_1_5MB:
562 size = KB(1024 + 512);
563 break;
564 default:
565 dev_info(&intel_private.pcidev->dev,
566 "unknown page table size, assuming 512KB\n");
567 size = KB(512);
568 }
569
570 return size/4;
571}
572
Daniel Vetterfbe40782010-08-27 17:12:41 +0200573static unsigned int intel_gtt_total_entries(void)
574{
575 int size;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200576
Daniel Vetter20172842010-09-24 18:25:59 +0200577 if (IS_G33 || INTEL_GTT_GEN == 4 || INTEL_GTT_GEN == 5)
578 return i965_gtt_total_entries();
579 else if (INTEL_GTT_GEN == 6) {
Daniel Vetter210b23c2010-08-28 16:14:32 +0200580 u16 snb_gmch_ctl;
581
582 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl);
583 switch (snb_gmch_ctl & SNB_GTT_SIZE_MASK) {
584 default:
585 case SNB_GTT_SIZE_0M:
586 printk(KERN_ERR "Bad GTT size mask: 0x%04x.\n", snb_gmch_ctl);
587 size = MB(0);
588 break;
589 case SNB_GTT_SIZE_1M:
590 size = MB(1);
591 break;
592 case SNB_GTT_SIZE_2M:
593 size = MB(2);
594 break;
595 }
596 return size/4;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200597 } else {
598 /* On previous hardware, the GTT size was just what was
599 * required to map the aperture.
600 */
Daniel Vettere5e408f2010-08-28 11:04:32 +0200601 return intel_private.base.gtt_mappable_entries;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200602 }
Daniel Vetterfbe40782010-08-27 17:12:41 +0200603}
Daniel Vetterfbe40782010-08-27 17:12:41 +0200604
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200605static unsigned int intel_gtt_mappable_entries(void)
606{
607 unsigned int aperture_size;
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200608
Daniel Vetter820647b2010-11-05 13:30:14 +0100609 if (INTEL_GTT_GEN == 1) {
610 u32 smram_miscc;
611
612 pci_read_config_dword(intel_private.bridge_dev,
613 I810_SMRAM_MISCC, &smram_miscc);
614
615 if ((smram_miscc & I810_GFX_MEM_WIN_SIZE)
616 == I810_GFX_MEM_WIN_32M)
617 aperture_size = MB(32);
618 else
619 aperture_size = MB(64);
620 } else if (INTEL_GTT_GEN == 2) {
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100621 u16 gmch_ctrl;
622
623 pci_read_config_word(intel_private.bridge_dev,
624 I830_GMCH_CTRL, &gmch_ctrl);
625
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200626 if ((gmch_ctrl & I830_GMCH_MEM_MASK) == I830_GMCH_MEM_64M)
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100627 aperture_size = MB(64);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200628 else
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100629 aperture_size = MB(128);
Daniel Vetter239918f2010-08-31 22:30:43 +0200630 } else {
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200631 /* 9xx supports large sizes, just look at the length */
632 aperture_size = pci_resource_len(intel_private.pcidev, 2);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200633 }
634
635 return aperture_size >> PAGE_SHIFT;
636}
637
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200638static void intel_gtt_teardown_scratch_page(void)
639{
640 set_pages_wb(intel_private.scratch_page, 1);
641 pci_unmap_page(intel_private.pcidev, intel_private.scratch_page_dma,
642 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
643 put_page(intel_private.scratch_page);
644 __free_page(intel_private.scratch_page);
645}
646
647static void intel_gtt_cleanup(void)
648{
Daniel Vetterae83dd52010-09-12 17:11:15 +0200649 intel_private.driver->cleanup();
650
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200651 iounmap(intel_private.gtt);
652 iounmap(intel_private.registers);
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100653
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200654 intel_gtt_teardown_scratch_page();
655}
656
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200657static int intel_gtt_init(void)
658{
Daniel Vetterf67eab62010-08-29 17:27:36 +0200659 u32 gtt_map_size;
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200660 int ret;
661
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200662 ret = intel_private.driver->setup();
663 if (ret != 0)
664 return ret;
Daniel Vetterf67eab62010-08-29 17:27:36 +0200665
666 intel_private.base.gtt_mappable_entries = intel_gtt_mappable_entries();
667 intel_private.base.gtt_total_entries = intel_gtt_total_entries();
668
Daniel Vetterb3eafc52010-09-23 20:04:17 +0200669 /* save the PGETBL reg for resume */
670 intel_private.PGETBL_save =
671 readl(intel_private.registers+I810_PGETBL_CTL)
672 & ~I810_PGETBL_ENABLED;
Chris Wilson100519e2010-10-31 10:37:02 +0000673 /* we only ever restore the register when enabling the PGTBL... */
674 if (HAS_PGTBL_EN)
675 intel_private.PGETBL_save |= I810_PGETBL_ENABLED;
Daniel Vetterb3eafc52010-09-23 20:04:17 +0200676
Daniel Vetter0af9e922010-09-12 14:04:03 +0200677 dev_info(&intel_private.bridge_dev->dev,
678 "detected gtt size: %dK total, %dK mappable\n",
679 intel_private.base.gtt_total_entries * 4,
680 intel_private.base.gtt_mappable_entries * 4);
681
Daniel Vetterf67eab62010-08-29 17:27:36 +0200682 gtt_map_size = intel_private.base.gtt_total_entries * 4;
683
684 intel_private.gtt = ioremap(intel_private.gtt_bus_addr,
685 gtt_map_size);
686 if (!intel_private.gtt) {
Daniel Vetterae83dd52010-09-12 17:11:15 +0200687 intel_private.driver->cleanup();
Daniel Vetterf67eab62010-08-29 17:27:36 +0200688 iounmap(intel_private.registers);
689 return -ENOMEM;
690 }
691
692 global_cache_flush(); /* FIXME: ? */
693
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000694 intel_private.base.stolen_size = intel_gtt_stolen_size();
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200695
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200696 ret = intel_gtt_setup_scratch_page();
697 if (ret != 0) {
698 intel_gtt_cleanup();
699 return ret;
700 }
701
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200702 return 0;
703}
704
Daniel Vetter3e921f92010-08-27 15:33:26 +0200705static int intel_fake_agp_fetch_size(void)
706{
Chris Wilson9e76e7b82010-09-14 12:12:11 +0100707 int num_sizes = ARRAY_SIZE(intel_fake_agp_sizes);
Daniel Vetter3e921f92010-08-27 15:33:26 +0200708 unsigned int aper_size;
709 int i;
Daniel Vetter3e921f92010-08-27 15:33:26 +0200710
711 aper_size = (intel_private.base.gtt_mappable_entries << PAGE_SHIFT)
712 / MB(1);
713
714 for (i = 0; i < num_sizes; i++) {
Daniel Vetterffdd7512010-08-27 17:51:29 +0200715 if (aper_size == intel_fake_agp_sizes[i].size) {
Chris Wilson9e76e7b82010-09-14 12:12:11 +0100716 agp_bridge->current_size =
717 (void *) (intel_fake_agp_sizes + i);
Daniel Vetter3e921f92010-08-27 15:33:26 +0200718 return aper_size;
719 }
720 }
721
722 return 0;
723}
724
Daniel Vetterae83dd52010-09-12 17:11:15 +0200725static void i830_cleanup(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200726{
727 kunmap(intel_private.i8xx_page);
728 intel_private.i8xx_flush_page = NULL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200729
730 __free_page(intel_private.i8xx_page);
731 intel_private.i8xx_page = NULL;
732}
733
734static void intel_i830_setup_flush(void)
735{
736 /* return if we've already set the flush mechanism up */
737 if (intel_private.i8xx_page)
738 return;
739
Jan Beuliche61cb0d2010-09-24 13:25:30 +0100740 intel_private.i8xx_page = alloc_page(GFP_KERNEL);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200741 if (!intel_private.i8xx_page)
742 return;
743
744 intel_private.i8xx_flush_page = kmap(intel_private.i8xx_page);
745 if (!intel_private.i8xx_flush_page)
Daniel Vetterae83dd52010-09-12 17:11:15 +0200746 i830_cleanup();
Daniel Vetterf51b7662010-04-14 00:29:52 +0200747}
748
749/* The chipset_flush interface needs to get data that has already been
750 * flushed out of the CPU all the way out to main memory, because the GPU
751 * doesn't snoop those buffers.
752 *
753 * The 8xx series doesn't have the same lovely interface for flushing the
754 * chipset write buffers that the later chips do. According to the 865
755 * specs, it's 64 octwords, or 1KB. So, to get those previous things in
756 * that buffer out, we just fill 1KB and clflush it out, on the assumption
757 * that it'll push whatever was in there out. It appears to work.
758 */
Daniel Vetter1b263f22010-09-12 00:27:24 +0200759static void i830_chipset_flush(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200760{
761 unsigned int *pg = intel_private.i8xx_flush_page;
762
763 memset(pg, 0, 1024);
764
765 if (cpu_has_clflush)
766 clflush_cache_range(pg, 1024);
767 else if (wbinvd_on_all_cpus() != 0)
768 printk(KERN_ERR "Timed out waiting for cache flush.\n");
769}
770
Daniel Vetter351bb272010-09-07 22:41:04 +0200771static void i830_write_entry(dma_addr_t addr, unsigned int entry,
772 unsigned int flags)
773{
774 u32 pte_flags = I810_PTE_VALID;
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100775
Daniel Vetterb47cf662010-11-04 18:41:50 +0100776 if (flags == AGP_USER_CACHED_MEMORY)
Daniel Vetter351bb272010-09-07 22:41:04 +0200777 pte_flags |= I830_PTE_SYSTEM_CACHED;
Daniel Vetter351bb272010-09-07 22:41:04 +0200778
779 writel(addr | pte_flags, intel_private.gtt + entry);
780}
781
Chris Wilsone380f602010-10-29 18:11:26 +0100782static bool intel_enable_gtt(void)
Daniel Vetter73800422010-08-29 17:29:50 +0200783{
Chris Wilson3f08e4e2010-09-14 20:15:22 +0100784 u32 gma_addr;
Chris Wilsone380f602010-10-29 18:11:26 +0100785 u8 __iomem *reg;
Daniel Vetter73800422010-08-29 17:29:50 +0200786
Daniel Vetter820647b2010-11-05 13:30:14 +0100787 if (INTEL_GTT_GEN <= 2)
Daniel Vetter2d2430c2010-08-29 17:35:30 +0200788 pci_read_config_dword(intel_private.pcidev, I810_GMADDR,
789 &gma_addr);
790 else
791 pci_read_config_dword(intel_private.pcidev, I915_GMADDR,
792 &gma_addr);
793
Daniel Vetter73800422010-08-29 17:29:50 +0200794 intel_private.gma_bus_addr = (gma_addr & PCI_BASE_ADDRESS_MEM_MASK);
795
Chris Wilsone380f602010-10-29 18:11:26 +0100796 if (INTEL_GTT_GEN >= 6)
797 return true;
Daniel Vetter73800422010-08-29 17:29:50 +0200798
Chris Wilson100519e2010-10-31 10:37:02 +0000799 if (INTEL_GTT_GEN == 2) {
800 u16 gmch_ctrl;
Chris Wilsone380f602010-10-29 18:11:26 +0100801
Chris Wilson100519e2010-10-31 10:37:02 +0000802 pci_read_config_word(intel_private.bridge_dev,
803 I830_GMCH_CTRL, &gmch_ctrl);
804 gmch_ctrl |= I830_GMCH_ENABLED;
805 pci_write_config_word(intel_private.bridge_dev,
806 I830_GMCH_CTRL, gmch_ctrl);
807
808 pci_read_config_word(intel_private.bridge_dev,
809 I830_GMCH_CTRL, &gmch_ctrl);
810 if ((gmch_ctrl & I830_GMCH_ENABLED) == 0) {
811 dev_err(&intel_private.pcidev->dev,
812 "failed to enable the GTT: GMCH_CTRL=%x\n",
813 gmch_ctrl);
814 return false;
815 }
Chris Wilsone380f602010-10-29 18:11:26 +0100816 }
817
818 reg = intel_private.registers+I810_PGETBL_CTL;
Chris Wilson100519e2010-10-31 10:37:02 +0000819 writel(intel_private.PGETBL_save, reg);
820 if (HAS_PGTBL_EN && (readl(reg) & I810_PGETBL_ENABLED) == 0) {
Chris Wilsone380f602010-10-29 18:11:26 +0100821 dev_err(&intel_private.pcidev->dev,
Chris Wilson100519e2010-10-31 10:37:02 +0000822 "failed to enable the GTT: PGETBL=%x [expected %x]\n",
Chris Wilsone380f602010-10-29 18:11:26 +0100823 readl(reg), intel_private.PGETBL_save);
824 return false;
825 }
826
827 return true;
Daniel Vetter73800422010-08-29 17:29:50 +0200828}
829
830static int i830_setup(void)
831{
832 u32 reg_addr;
833
834 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &reg_addr);
835 reg_addr &= 0xfff80000;
836
837 intel_private.registers = ioremap(reg_addr, KB(64));
838 if (!intel_private.registers)
839 return -ENOMEM;
840
841 intel_private.gtt_bus_addr = reg_addr + I810_PTE_BASE;
842
843 intel_i830_setup_flush();
844
845 return 0;
846}
847
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200848static int intel_fake_agp_create_gatt_table(struct agp_bridge_data *bridge)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200849{
Daniel Vetter73800422010-08-29 17:29:50 +0200850 agp_bridge->gatt_table_real = NULL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200851 agp_bridge->gatt_table = NULL;
Daniel Vetter73800422010-08-29 17:29:50 +0200852 agp_bridge->gatt_bus_addr = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200853
854 return 0;
855}
856
Daniel Vetterffdd7512010-08-27 17:51:29 +0200857static int intel_fake_agp_free_gatt_table(struct agp_bridge_data *bridge)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200858{
859 return 0;
860}
861
Daniel Vetter351bb272010-09-07 22:41:04 +0200862static int intel_fake_agp_configure(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200863{
Daniel Vetterf51b7662010-04-14 00:29:52 +0200864 int i;
865
Chris Wilsone380f602010-10-29 18:11:26 +0100866 if (!intel_enable_gtt())
867 return -EIO;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200868
Daniel Vetter73800422010-08-29 17:29:50 +0200869 agp_bridge->gart_bus_addr = intel_private.gma_bus_addr;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200870
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000871 for (i = 0; i < intel_private.base.gtt_total_entries; i++) {
Daniel Vetter351bb272010-09-07 22:41:04 +0200872 intel_private.driver->write_entry(intel_private.scratch_page_dma,
873 i, 0);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200874 }
Daniel Vetter351bb272010-09-07 22:41:04 +0200875 readl(intel_private.gtt+i-1); /* PCI Posting. */
Daniel Vetterf51b7662010-04-14 00:29:52 +0200876
877 global_cache_flush();
878
Daniel Vetterf51b7662010-04-14 00:29:52 +0200879 return 0;
880}
881
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200882static bool i830_check_flags(unsigned int flags)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200883{
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200884 switch (flags) {
885 case 0:
886 case AGP_PHYS_MEMORY:
887 case AGP_USER_CACHED_MEMORY:
888 case AGP_USER_MEMORY:
889 return true;
890 }
891
892 return false;
893}
894
Daniel Vetterfefaa702010-09-11 22:12:11 +0200895static void intel_gtt_insert_sg_entries(struct scatterlist *sg_list,
896 unsigned int sg_len,
897 unsigned int pg_start,
898 unsigned int flags)
899{
900 struct scatterlist *sg;
901 unsigned int len, m;
902 int i, j;
903
904 j = pg_start;
905
906 /* sg may merge pages, but we have to separate
907 * per-page addr for GTT */
908 for_each_sg(sg_list, sg, sg_len, i) {
909 len = sg_dma_len(sg) >> PAGE_SHIFT;
910 for (m = 0; m < len; m++) {
911 dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
912 intel_private.driver->write_entry(addr,
913 j, flags);
914 j++;
915 }
916 }
917 readl(intel_private.gtt+j-1);
918}
919
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200920static int intel_fake_agp_insert_entries(struct agp_memory *mem,
921 off_t pg_start, int type)
922{
923 int i, j;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200924 int ret = -EINVAL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200925
Daniel Vetterff268602010-11-05 15:43:35 +0100926 if (INTEL_GTT_GEN == 1 && type == AGP_DCACHE_MEMORY)
927 return i810_insert_dcache_entries(mem, pg_start, type);
928
Daniel Vetterf51b7662010-04-14 00:29:52 +0200929 if (mem->page_count == 0)
930 goto out;
931
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000932 if (pg_start + mem->page_count > intel_private.base.gtt_total_entries)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200933 goto out_err;
934
Daniel Vetterf51b7662010-04-14 00:29:52 +0200935 if (type != mem->type)
936 goto out_err;
937
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200938 if (!intel_private.driver->check_flags(type))
Daniel Vetterf51b7662010-04-14 00:29:52 +0200939 goto out_err;
940
941 if (!mem->is_flushed)
942 global_cache_flush();
943
Daniel Vetterfefaa702010-09-11 22:12:11 +0200944 if (USE_PCI_DMA_API && INTEL_GTT_GEN > 2) {
945 ret = intel_agp_map_memory(mem);
946 if (ret != 0)
947 return ret;
948
949 intel_gtt_insert_sg_entries(mem->sg_list, mem->num_sg,
950 pg_start, type);
951 } else {
952 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
953 dma_addr_t addr = page_to_phys(mem->pages[i]);
954 intel_private.driver->write_entry(addr,
955 j, type);
956 }
957 readl(intel_private.gtt+j-1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200958 }
Daniel Vetterf51b7662010-04-14 00:29:52 +0200959
960out:
961 ret = 0;
962out_err:
963 mem->is_flushed = true;
964 return ret;
965}
966
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200967static int intel_fake_agp_remove_entries(struct agp_memory *mem,
968 off_t pg_start, int type)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200969{
970 int i;
971
972 if (mem->page_count == 0)
973 return 0;
974
Daniel Vetterfefaa702010-09-11 22:12:11 +0200975 if (USE_PCI_DMA_API && INTEL_GTT_GEN > 2)
976 intel_agp_unmap_memory(mem);
977
Daniel Vetterf51b7662010-04-14 00:29:52 +0200978 for (i = pg_start; i < (mem->page_count + pg_start); i++) {
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200979 intel_private.driver->write_entry(intel_private.scratch_page_dma,
980 i, 0);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200981 }
Daniel Vetterfdfb58a2010-08-29 00:15:03 +0200982 readl(intel_private.gtt+i-1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200983
Daniel Vetterf51b7662010-04-14 00:29:52 +0200984 return 0;
985}
986
Daniel Vetter1b263f22010-09-12 00:27:24 +0200987static void intel_fake_agp_chipset_flush(struct agp_bridge_data *bridge)
988{
989 intel_private.driver->chipset_flush();
990}
991
Daniel Vetterffdd7512010-08-27 17:51:29 +0200992static struct agp_memory *intel_fake_agp_alloc_by_type(size_t pg_count,
993 int type)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200994{
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100995 struct agp_memory *new;
996
997 if (type == AGP_DCACHE_MEMORY && INTEL_GTT_GEN == 1) {
998 if (pg_count != intel_private.num_dcache_entries)
999 return NULL;
1000
1001 new = agp_create_memory(1);
1002 if (new == NULL)
1003 return NULL;
1004
1005 new->type = AGP_DCACHE_MEMORY;
1006 new->page_count = pg_count;
1007 new->num_scratch_pages = 0;
1008 agp_free_page_array(new);
1009 return new;
1010 }
Daniel Vetterf51b7662010-04-14 00:29:52 +02001011 if (type == AGP_PHYS_MEMORY)
1012 return alloc_agpphysmem_i8xx(pg_count, type);
1013 /* always return NULL for other allocation types for now */
1014 return NULL;
1015}
1016
1017static int intel_alloc_chipset_flush_resource(void)
1018{
1019 int ret;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001020 ret = pci_bus_alloc_resource(intel_private.bridge_dev->bus, &intel_private.ifp_resource, PAGE_SIZE,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001021 PAGE_SIZE, PCIBIOS_MIN_MEM, 0,
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001022 pcibios_align_resource, intel_private.bridge_dev);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001023
1024 return ret;
1025}
1026
1027static void intel_i915_setup_chipset_flush(void)
1028{
1029 int ret;
1030 u32 temp;
1031
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001032 pci_read_config_dword(intel_private.bridge_dev, I915_IFPADDR, &temp);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001033 if (!(temp & 0x1)) {
1034 intel_alloc_chipset_flush_resource();
1035 intel_private.resource_valid = 1;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001036 pci_write_config_dword(intel_private.bridge_dev, I915_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001037 } else {
1038 temp &= ~1;
1039
1040 intel_private.resource_valid = 1;
1041 intel_private.ifp_resource.start = temp;
1042 intel_private.ifp_resource.end = temp + PAGE_SIZE;
1043 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
1044 /* some BIOSes reserve this area in a pnp some don't */
1045 if (ret)
1046 intel_private.resource_valid = 0;
1047 }
1048}
1049
1050static void intel_i965_g33_setup_chipset_flush(void)
1051{
1052 u32 temp_hi, temp_lo;
1053 int ret;
1054
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001055 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4, &temp_hi);
1056 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR, &temp_lo);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001057
1058 if (!(temp_lo & 0x1)) {
1059
1060 intel_alloc_chipset_flush_resource();
1061
1062 intel_private.resource_valid = 1;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001063 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001064 upper_32_bits(intel_private.ifp_resource.start));
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001065 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001066 } else {
1067 u64 l64;
1068
1069 temp_lo &= ~0x1;
1070 l64 = ((u64)temp_hi << 32) | temp_lo;
1071
1072 intel_private.resource_valid = 1;
1073 intel_private.ifp_resource.start = l64;
1074 intel_private.ifp_resource.end = l64 + PAGE_SIZE;
1075 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
1076 /* some BIOSes reserve this area in a pnp some don't */
1077 if (ret)
1078 intel_private.resource_valid = 0;
1079 }
1080}
1081
1082static void intel_i9xx_setup_flush(void)
1083{
1084 /* return if already configured */
1085 if (intel_private.ifp_resource.start)
1086 return;
1087
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001088 if (INTEL_GTT_GEN == 6)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001089 return;
1090
1091 /* setup a resource for this object */
1092 intel_private.ifp_resource.name = "Intel Flush Page";
1093 intel_private.ifp_resource.flags = IORESOURCE_MEM;
1094
1095 /* Setup chipset flush for 915 */
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001096 if (IS_G33 || INTEL_GTT_GEN >= 4) {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001097 intel_i965_g33_setup_chipset_flush();
1098 } else {
1099 intel_i915_setup_chipset_flush();
1100 }
1101
Chris Wilsondf51e7a2010-09-04 14:57:27 +01001102 if (intel_private.ifp_resource.start)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001103 intel_private.i9xx_flush_page = ioremap_nocache(intel_private.ifp_resource.start, PAGE_SIZE);
Chris Wilsondf51e7a2010-09-04 14:57:27 +01001104 if (!intel_private.i9xx_flush_page)
1105 dev_err(&intel_private.pcidev->dev,
1106 "can't ioremap flush page - no chipset flushing\n");
Daniel Vetterf51b7662010-04-14 00:29:52 +02001107}
1108
Daniel Vetterae83dd52010-09-12 17:11:15 +02001109static void i9xx_cleanup(void)
1110{
1111 if (intel_private.i9xx_flush_page)
1112 iounmap(intel_private.i9xx_flush_page);
1113 if (intel_private.resource_valid)
1114 release_resource(&intel_private.ifp_resource);
1115 intel_private.ifp_resource.start = 0;
1116 intel_private.resource_valid = 0;
1117}
1118
Daniel Vetter1b263f22010-09-12 00:27:24 +02001119static void i9xx_chipset_flush(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001120{
1121 if (intel_private.i9xx_flush_page)
1122 writel(1, intel_private.i9xx_flush_page);
1123}
1124
Daniel Vettera6963592010-09-11 14:01:43 +02001125static void i965_write_entry(dma_addr_t addr, unsigned int entry,
1126 unsigned int flags)
1127{
1128 /* Shift high bits down */
1129 addr |= (addr >> 28) & 0xf0;
1130 writel(addr | I810_PTE_VALID, intel_private.gtt + entry);
1131}
1132
Daniel Vetter90cb1492010-09-11 23:55:20 +02001133static bool gen6_check_flags(unsigned int flags)
1134{
1135 return true;
1136}
1137
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001138static void gen6_write_entry(dma_addr_t addr, unsigned int entry,
1139 unsigned int flags)
1140{
1141 unsigned int type_mask = flags & ~AGP_USER_CACHED_MEMORY_GFDT;
1142 unsigned int gfdt = flags & AGP_USER_CACHED_MEMORY_GFDT;
1143 u32 pte_flags;
1144
Zhenyu Wang897ef192010-11-02 17:30:47 +08001145 if (type_mask == AGP_USER_MEMORY)
Chris Wilson85ccc352010-10-22 14:59:29 +01001146 pte_flags = GEN6_PTE_UNCACHED | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001147 else if (type_mask == AGP_USER_CACHED_MEMORY_LLC_MLC) {
Zhenyu Wangd1108522010-11-02 17:30:46 +08001148 pte_flags = GEN6_PTE_LLC_MLC | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001149 if (gfdt)
1150 pte_flags |= GEN6_PTE_GFDT;
1151 } else { /* set 'normal'/'cached' to LLC by default */
Zhenyu Wangd1108522010-11-02 17:30:46 +08001152 pte_flags = GEN6_PTE_LLC | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001153 if (gfdt)
1154 pte_flags |= GEN6_PTE_GFDT;
1155 }
1156
1157 /* gen6 has bit11-4 for physical addr bit39-32 */
1158 addr |= (addr >> 28) & 0xff0;
1159 writel(addr | pte_flags, intel_private.gtt + entry);
1160}
1161
Daniel Vetterae83dd52010-09-12 17:11:15 +02001162static void gen6_cleanup(void)
1163{
1164}
1165
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001166static int i9xx_setup(void)
1167{
1168 u32 reg_addr;
1169
1170 pci_read_config_dword(intel_private.pcidev, I915_MMADDR, &reg_addr);
1171
1172 reg_addr &= 0xfff80000;
1173
1174 intel_private.registers = ioremap(reg_addr, 128 * 4096);
1175 if (!intel_private.registers)
1176 return -ENOMEM;
1177
1178 if (INTEL_GTT_GEN == 3) {
1179 u32 gtt_addr;
Chris Wilson3f08e4e2010-09-14 20:15:22 +01001180
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001181 pci_read_config_dword(intel_private.pcidev,
1182 I915_PTEADDR, &gtt_addr);
1183 intel_private.gtt_bus_addr = gtt_addr;
1184 } else {
1185 u32 gtt_offset;
1186
1187 switch (INTEL_GTT_GEN) {
1188 case 5:
1189 case 6:
1190 gtt_offset = MB(2);
1191 break;
1192 case 4:
1193 default:
1194 gtt_offset = KB(512);
1195 break;
1196 }
1197 intel_private.gtt_bus_addr = reg_addr + gtt_offset;
1198 }
1199
1200 intel_i9xx_setup_flush();
1201
1202 return 0;
1203}
1204
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001205static const struct agp_bridge_driver intel_fake_agp_driver = {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001206 .owner = THIS_MODULE,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001207 .size_type = FIXED_APER_SIZE,
Chris Wilson9e76e7b82010-09-14 12:12:11 +01001208 .aperture_sizes = intel_fake_agp_sizes,
1209 .num_aperture_sizes = ARRAY_SIZE(intel_fake_agp_sizes),
Daniel Vettera6963592010-09-11 14:01:43 +02001210 .configure = intel_fake_agp_configure,
Daniel Vetter3e921f92010-08-27 15:33:26 +02001211 .fetch_size = intel_fake_agp_fetch_size,
Daniel Vetterfdfb58a2010-08-29 00:15:03 +02001212 .cleanup = intel_gtt_cleanup,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001213 .agp_enable = intel_fake_agp_enable,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001214 .cache_flush = global_cache_flush,
Daniel Vetter3b15a9d2010-08-29 14:18:49 +02001215 .create_gatt_table = intel_fake_agp_create_gatt_table,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001216 .free_gatt_table = intel_fake_agp_free_gatt_table,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001217 .insert_memory = intel_fake_agp_insert_entries,
1218 .remove_memory = intel_fake_agp_remove_entries,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001219 .alloc_by_type = intel_fake_agp_alloc_by_type,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001220 .free_by_type = intel_i810_free_by_type,
1221 .agp_alloc_page = agp_generic_alloc_page,
1222 .agp_alloc_pages = agp_generic_alloc_pages,
1223 .agp_destroy_page = agp_generic_destroy_page,
1224 .agp_destroy_pages = agp_generic_destroy_pages,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001225 .chipset_flush = intel_fake_agp_chipset_flush,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001226};
Daniel Vetter02c026c2010-08-24 19:39:48 +02001227
Daniel Vetterbdd30722010-09-12 12:34:44 +02001228static const struct intel_gtt_driver i81x_gtt_driver = {
1229 .gen = 1,
Daniel Vetter820647b2010-11-05 13:30:14 +01001230 .has_pgtbl_enable = 1,
Daniel Vetter22533b42010-09-12 16:38:55 +02001231 .dma_mask_size = 32,
Daniel Vetter820647b2010-11-05 13:30:14 +01001232 .setup = i810_setup,
1233 .cleanup = i810_cleanup,
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001234 .check_flags = i830_check_flags,
1235 .write_entry = i810_write_entry,
Daniel Vetterbdd30722010-09-12 12:34:44 +02001236};
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001237static const struct intel_gtt_driver i8xx_gtt_driver = {
1238 .gen = 2,
Chris Wilson100519e2010-10-31 10:37:02 +00001239 .has_pgtbl_enable = 1,
Daniel Vetter73800422010-08-29 17:29:50 +02001240 .setup = i830_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001241 .cleanup = i830_cleanup,
Daniel Vetter351bb272010-09-07 22:41:04 +02001242 .write_entry = i830_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001243 .dma_mask_size = 32,
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001244 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001245 .chipset_flush = i830_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001246};
1247static const struct intel_gtt_driver i915_gtt_driver = {
1248 .gen = 3,
Chris Wilson100519e2010-10-31 10:37:02 +00001249 .has_pgtbl_enable = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001250 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001251 .cleanup = i9xx_cleanup,
Daniel Vetter351bb272010-09-07 22:41:04 +02001252 /* i945 is the last gpu to need phys mem (for overlay and cursors). */
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001253 .write_entry = i830_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001254 .dma_mask_size = 32,
Daniel Vetterfefaa702010-09-11 22:12:11 +02001255 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001256 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001257};
1258static const struct intel_gtt_driver g33_gtt_driver = {
1259 .gen = 3,
1260 .is_g33 = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001261 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001262 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001263 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001264 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001265 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001266 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001267};
1268static const struct intel_gtt_driver pineview_gtt_driver = {
1269 .gen = 3,
1270 .is_pineview = 1, .is_g33 = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001271 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001272 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001273 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001274 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001275 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001276 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001277};
1278static const struct intel_gtt_driver i965_gtt_driver = {
1279 .gen = 4,
Chris Wilson100519e2010-10-31 10:37:02 +00001280 .has_pgtbl_enable = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001281 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001282 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001283 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001284 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001285 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001286 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001287};
1288static const struct intel_gtt_driver g4x_gtt_driver = {
1289 .gen = 5,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001290 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001291 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001292 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001293 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001294 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001295 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001296};
1297static const struct intel_gtt_driver ironlake_gtt_driver = {
1298 .gen = 5,
1299 .is_ironlake = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001300 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001301 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001302 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001303 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001304 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001305 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001306};
1307static const struct intel_gtt_driver sandybridge_gtt_driver = {
1308 .gen = 6,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001309 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001310 .cleanup = gen6_cleanup,
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001311 .write_entry = gen6_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001312 .dma_mask_size = 40,
Daniel Vetter90cb1492010-09-11 23:55:20 +02001313 .check_flags = gen6_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001314 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001315};
1316
Daniel Vetter02c026c2010-08-24 19:39:48 +02001317/* Table to describe Intel GMCH and AGP/PCIE GART drivers. At least one of
1318 * driver and gmch_driver must be non-null, and find_gmch will determine
1319 * which one should be used if a gmch_chip_id is present.
1320 */
1321static const struct intel_gtt_driver_description {
1322 unsigned int gmch_chip_id;
1323 char *name;
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001324 const struct intel_gtt_driver *gtt_driver;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001325} intel_gtt_chipsets[] = {
Daniel Vetterff268602010-11-05 15:43:35 +01001326 { PCI_DEVICE_ID_INTEL_82810_IG1, "i810",
Daniel Vetterbdd30722010-09-12 12:34:44 +02001327 &i81x_gtt_driver},
Daniel Vetterff268602010-11-05 15:43:35 +01001328 { PCI_DEVICE_ID_INTEL_82810_IG3, "i810",
Daniel Vetterbdd30722010-09-12 12:34:44 +02001329 &i81x_gtt_driver},
Daniel Vetterff268602010-11-05 15:43:35 +01001330 { PCI_DEVICE_ID_INTEL_82810E_IG, "i810",
Daniel Vetterbdd30722010-09-12 12:34:44 +02001331 &i81x_gtt_driver},
Daniel Vetterff268602010-11-05 15:43:35 +01001332 { PCI_DEVICE_ID_INTEL_82815_CGC, "i815",
Daniel Vetterbdd30722010-09-12 12:34:44 +02001333 &i81x_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001334 { PCI_DEVICE_ID_INTEL_82830_CGC, "830M",
Daniel Vetterff268602010-11-05 15:43:35 +01001335 &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001336 { PCI_DEVICE_ID_INTEL_82845G_IG, "830M",
Daniel Vetterff268602010-11-05 15:43:35 +01001337 &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001338 { PCI_DEVICE_ID_INTEL_82854_IG, "854",
Daniel Vetterff268602010-11-05 15:43:35 +01001339 &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001340 { PCI_DEVICE_ID_INTEL_82855GM_IG, "855GM",
Daniel Vetterff268602010-11-05 15:43:35 +01001341 &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001342 { PCI_DEVICE_ID_INTEL_82865_IG, "865",
Daniel Vetterff268602010-11-05 15:43:35 +01001343 &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001344 { PCI_DEVICE_ID_INTEL_E7221_IG, "E7221 (i915)",
Daniel Vetterff268602010-11-05 15:43:35 +01001345 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001346 { PCI_DEVICE_ID_INTEL_82915G_IG, "915G",
Daniel Vetterff268602010-11-05 15:43:35 +01001347 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001348 { PCI_DEVICE_ID_INTEL_82915GM_IG, "915GM",
Daniel Vetterff268602010-11-05 15:43:35 +01001349 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001350 { PCI_DEVICE_ID_INTEL_82945G_IG, "945G",
Daniel Vetterff268602010-11-05 15:43:35 +01001351 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001352 { PCI_DEVICE_ID_INTEL_82945GM_IG, "945GM",
Daniel Vetterff268602010-11-05 15:43:35 +01001353 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001354 { PCI_DEVICE_ID_INTEL_82945GME_IG, "945GME",
Daniel Vetterff268602010-11-05 15:43:35 +01001355 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001356 { PCI_DEVICE_ID_INTEL_82946GZ_IG, "946GZ",
Daniel Vetterff268602010-11-05 15:43:35 +01001357 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001358 { PCI_DEVICE_ID_INTEL_82G35_IG, "G35",
Daniel Vetterff268602010-11-05 15:43:35 +01001359 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001360 { PCI_DEVICE_ID_INTEL_82965Q_IG, "965Q",
Daniel Vetterff268602010-11-05 15:43:35 +01001361 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001362 { PCI_DEVICE_ID_INTEL_82965G_IG, "965G",
Daniel Vetterff268602010-11-05 15:43:35 +01001363 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001364 { PCI_DEVICE_ID_INTEL_82965GM_IG, "965GM",
Daniel Vetterff268602010-11-05 15:43:35 +01001365 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001366 { PCI_DEVICE_ID_INTEL_82965GME_IG, "965GME/GLE",
Daniel Vetterff268602010-11-05 15:43:35 +01001367 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001368 { PCI_DEVICE_ID_INTEL_G33_IG, "G33",
Daniel Vetterff268602010-11-05 15:43:35 +01001369 &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001370 { PCI_DEVICE_ID_INTEL_Q35_IG, "Q35",
Daniel Vetterff268602010-11-05 15:43:35 +01001371 &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001372 { PCI_DEVICE_ID_INTEL_Q33_IG, "Q33",
Daniel Vetterff268602010-11-05 15:43:35 +01001373 &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001374 { PCI_DEVICE_ID_INTEL_PINEVIEW_M_IG, "GMA3150",
Daniel Vetterff268602010-11-05 15:43:35 +01001375 &pineview_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001376 { PCI_DEVICE_ID_INTEL_PINEVIEW_IG, "GMA3150",
Daniel Vetterff268602010-11-05 15:43:35 +01001377 &pineview_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001378 { PCI_DEVICE_ID_INTEL_GM45_IG, "GM45",
Daniel Vetterff268602010-11-05 15:43:35 +01001379 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001380 { PCI_DEVICE_ID_INTEL_EAGLELAKE_IG, "Eaglelake",
Daniel Vetterff268602010-11-05 15:43:35 +01001381 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001382 { PCI_DEVICE_ID_INTEL_Q45_IG, "Q45/Q43",
Daniel Vetterff268602010-11-05 15:43:35 +01001383 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001384 { PCI_DEVICE_ID_INTEL_G45_IG, "G45/G43",
Daniel Vetterff268602010-11-05 15:43:35 +01001385 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001386 { PCI_DEVICE_ID_INTEL_B43_IG, "B43",
Daniel Vetterff268602010-11-05 15:43:35 +01001387 &g4x_gtt_driver },
Chris Wilsone9e5f8e2010-09-21 11:19:32 +01001388 { PCI_DEVICE_ID_INTEL_B43_1_IG, "B43",
Daniel Vetterff268602010-11-05 15:43:35 +01001389 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001390 { PCI_DEVICE_ID_INTEL_G41_IG, "G41",
Daniel Vetterff268602010-11-05 15:43:35 +01001391 &g4x_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001392 { PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001393 "HD Graphics", &ironlake_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001394 { PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001395 "HD Graphics", &ironlake_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001396 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT1_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001397 "Sandybridge", &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001398 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001399 "Sandybridge", &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001400 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_PLUS_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001401 "Sandybridge", &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001402 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT1_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001403 "Sandybridge", &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001404 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001405 "Sandybridge", &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001406 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_PLUS_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001407 "Sandybridge", &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001408 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001409 "Sandybridge", &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001410 { 0, NULL, NULL }
1411};
1412
1413static int find_gmch(u16 device)
1414{
1415 struct pci_dev *gmch_device;
1416
1417 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL, device, NULL);
1418 if (gmch_device && PCI_FUNC(gmch_device->devfn) != 0) {
1419 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL,
1420 device, gmch_device);
1421 }
1422
1423 if (!gmch_device)
1424 return 0;
1425
1426 intel_private.pcidev = gmch_device;
1427 return 1;
1428}
1429
Daniel Vettere2404e72010-09-08 17:29:51 +02001430int intel_gmch_probe(struct pci_dev *pdev,
Daniel Vetter02c026c2010-08-24 19:39:48 +02001431 struct agp_bridge_data *bridge)
1432{
1433 int i, mask;
Daniel Vetterff268602010-11-05 15:43:35 +01001434 intel_private.driver = NULL;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001435
1436 for (i = 0; intel_gtt_chipsets[i].name != NULL; i++) {
1437 if (find_gmch(intel_gtt_chipsets[i].gmch_chip_id)) {
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001438 intel_private.driver =
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001439 intel_gtt_chipsets[i].gtt_driver;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001440 break;
1441 }
1442 }
1443
Daniel Vetterff268602010-11-05 15:43:35 +01001444 if (!intel_private.driver)
Daniel Vetter02c026c2010-08-24 19:39:48 +02001445 return 0;
1446
Daniel Vetterff268602010-11-05 15:43:35 +01001447 bridge->driver = &intel_fake_agp_driver;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001448 bridge->dev_private_data = &intel_private;
1449 bridge->dev = pdev;
1450
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001451 intel_private.bridge_dev = pci_dev_get(pdev);
1452
Daniel Vetter02c026c2010-08-24 19:39:48 +02001453 dev_info(&pdev->dev, "Intel %s Chipset\n", intel_gtt_chipsets[i].name);
1454
Daniel Vetter22533b42010-09-12 16:38:55 +02001455 mask = intel_private.driver->dma_mask_size;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001456 if (pci_set_dma_mask(intel_private.pcidev, DMA_BIT_MASK(mask)))
1457 dev_err(&intel_private.pcidev->dev,
1458 "set gfx device dma mask %d-bit failed!\n", mask);
1459 else
1460 pci_set_consistent_dma_mask(intel_private.pcidev,
1461 DMA_BIT_MASK(mask));
1462
Daniel Vetter820647b2010-11-05 13:30:14 +01001463 /*if (bridge->driver == &intel_810_driver)
1464 return 1;*/
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001465
Daniel Vetter3b15a9d2010-08-29 14:18:49 +02001466 if (intel_gtt_init() != 0)
1467 return 0;
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001468
Daniel Vetter02c026c2010-08-24 19:39:48 +02001469 return 1;
1470}
Daniel Vettere2404e72010-09-08 17:29:51 +02001471EXPORT_SYMBOL(intel_gmch_probe);
Daniel Vetter02c026c2010-08-24 19:39:48 +02001472
Chris Wilsonc64f7ba2010-11-23 14:24:24 +00001473const struct intel_gtt *intel_gtt_get(void)
Daniel Vetter19966752010-09-06 20:08:44 +02001474{
1475 return &intel_private.base;
1476}
1477EXPORT_SYMBOL(intel_gtt_get);
1478
Daniel Vettere2404e72010-09-08 17:29:51 +02001479void intel_gmch_remove(struct pci_dev *pdev)
Daniel Vetter02c026c2010-08-24 19:39:48 +02001480{
1481 if (intel_private.pcidev)
1482 pci_dev_put(intel_private.pcidev);
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001483 if (intel_private.bridge_dev)
1484 pci_dev_put(intel_private.bridge_dev);
Daniel Vetter02c026c2010-08-24 19:39:48 +02001485}
Daniel Vettere2404e72010-09-08 17:29:51 +02001486EXPORT_SYMBOL(intel_gmch_remove);
1487
1488MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
1489MODULE_LICENSE("GPL and additional rights");