blob: e921b693412b9ec4dc994340c3724ee99dcc94b4 [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 Vetter40807752010-11-06 11:18:58 +010090int intel_gtt_map_memory(struct page **pages, unsigned int num_entries,
91 struct scatterlist **sg_list, int *num_sg)
Daniel Vetterf51b7662010-04-14 00:29:52 +020092{
93 struct sg_table st;
94 struct scatterlist *sg;
95 int i;
96
Daniel Vetter40807752010-11-06 11:18:58 +010097 if (*sg_list)
Daniel Vetterfefaa702010-09-11 22:12:11 +020098 return 0; /* already mapped (for e.g. resume */
99
Daniel Vetter40807752010-11-06 11:18:58 +0100100 DBG("try mapping %lu pages\n", (unsigned long)num_entries);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200101
Daniel Vetter40807752010-11-06 11:18:58 +0100102 if (sg_alloc_table(&st, num_entries, GFP_KERNEL))
Chris Wilson831cd442010-07-24 18:29:37 +0100103 goto err;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200104
Daniel Vetter40807752010-11-06 11:18:58 +0100105 *sg_list = sg = st.sgl;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200106
Daniel Vetter40807752010-11-06 11:18:58 +0100107 for (i = 0 ; i < num_entries; i++, sg = sg_next(sg))
108 sg_set_page(sg, pages[i], PAGE_SIZE, 0);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200109
Daniel Vetter40807752010-11-06 11:18:58 +0100110 *num_sg = pci_map_sg(intel_private.pcidev, *sg_list,
111 num_entries, PCI_DMA_BIDIRECTIONAL);
112 if (unlikely(!*num_sg))
Chris Wilson831cd442010-07-24 18:29:37 +0100113 goto err;
114
Daniel Vetterf51b7662010-04-14 00:29:52 +0200115 return 0;
Chris Wilson831cd442010-07-24 18:29:37 +0100116
117err:
118 sg_free_table(&st);
119 return -ENOMEM;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200120}
Daniel Vetter40807752010-11-06 11:18:58 +0100121EXPORT_SYMBOL(intel_gtt_map_memory);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200122
Daniel Vetter40807752010-11-06 11:18:58 +0100123void intel_gtt_unmap_memory(struct scatterlist *sg_list, int num_sg)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200124{
Daniel Vetter40807752010-11-06 11:18:58 +0100125 struct sg_table st;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200126 DBG("try unmapping %lu pages\n", (unsigned long)mem->page_count);
127
Daniel Vetter40807752010-11-06 11:18:58 +0100128 pci_unmap_sg(intel_private.pcidev, sg_list,
129 num_sg, PCI_DMA_BIDIRECTIONAL);
130
131 st.sgl = sg_list;
132 st.orig_nents = st.nents = num_sg;
133
134 sg_free_table(&st);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200135}
Daniel Vetter40807752010-11-06 11:18:58 +0100136EXPORT_SYMBOL(intel_gtt_unmap_memory);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200137
Daniel Vetterffdd7512010-08-27 17:51:29 +0200138static void intel_fake_agp_enable(struct agp_bridge_data *bridge, u32 mode)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200139{
140 return;
141}
142
143/* Exists to support ARGB cursors */
144static struct page *i8xx_alloc_pages(void)
145{
146 struct page *page;
147
148 page = alloc_pages(GFP_KERNEL | GFP_DMA32, 2);
149 if (page == NULL)
150 return NULL;
151
152 if (set_pages_uc(page, 4) < 0) {
153 set_pages_wb(page, 4);
154 __free_pages(page, 2);
155 return NULL;
156 }
157 get_page(page);
158 atomic_inc(&agp_bridge->current_memory_agp);
159 return page;
160}
161
162static void i8xx_destroy_pages(struct page *page)
163{
164 if (page == NULL)
165 return;
166
167 set_pages_wb(page, 4);
168 put_page(page);
169 __free_pages(page, 2);
170 atomic_dec(&agp_bridge->current_memory_agp);
171}
172
Daniel Vetter820647b2010-11-05 13:30:14 +0100173#define I810_GTT_ORDER 4
174static int i810_setup(void)
175{
176 u32 reg_addr;
177 char *gtt_table;
178
179 /* i81x does not preallocate the gtt. It's always 64kb in size. */
180 gtt_table = alloc_gatt_pages(I810_GTT_ORDER);
181 if (gtt_table == NULL)
182 return -ENOMEM;
183 intel_private.i81x_gtt_table = gtt_table;
184
185 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &reg_addr);
186 reg_addr &= 0xfff80000;
187
188 intel_private.registers = ioremap(reg_addr, KB(64));
189 if (!intel_private.registers)
190 return -ENOMEM;
191
192 writel(virt_to_phys(gtt_table) | I810_PGETBL_ENABLED,
193 intel_private.registers+I810_PGETBL_CTL);
194
195 intel_private.gtt_bus_addr = reg_addr + I810_PTE_BASE;
196
197 if ((readl(intel_private.registers+I810_DRAM_CTL)
198 & I810_DRAM_ROW_0) == I810_DRAM_ROW_0_SDRAM) {
199 dev_info(&intel_private.pcidev->dev,
200 "detected 4MB dedicated video ram\n");
201 intel_private.num_dcache_entries = 1024;
202 }
203
204 return 0;
205}
206
207static void i810_cleanup(void)
208{
209 writel(0, intel_private.registers+I810_PGETBL_CTL);
210 free_gatt_pages(intel_private.i81x_gtt_table, I810_GTT_ORDER);
211}
212
Daniel Vetterff268602010-11-05 15:43:35 +0100213static int i810_insert_dcache_entries(struct agp_memory *mem, off_t pg_start,
214 int type)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200215{
Daniel Vetterf51b7662010-04-14 00:29:52 +0200216 int i;
217
Daniel Vetterff268602010-11-05 15:43:35 +0100218 if ((pg_start + mem->page_count)
219 > intel_private.num_dcache_entries)
220 return -EINVAL;
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100221
Daniel Vetterff268602010-11-05 15:43:35 +0100222 if (!mem->is_flushed)
223 global_cache_flush();
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100224
Daniel Vetterff268602010-11-05 15:43:35 +0100225 for (i = pg_start; i < (pg_start + mem->page_count); i++) {
226 dma_addr_t addr = i << PAGE_SHIFT;
227 intel_private.driver->write_entry(addr,
228 i, type);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200229 }
Daniel Vetterff268602010-11-05 15:43:35 +0100230 readl(intel_private.gtt+i-1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200231
Daniel Vetterff268602010-11-05 15:43:35 +0100232 return 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200233}
234
235/*
236 * The i810/i830 requires a physical address to program its mouse
237 * pointer into hardware.
238 * However the Xserver still writes to it through the agp aperture.
239 */
240static struct agp_memory *alloc_agpphysmem_i8xx(size_t pg_count, int type)
241{
242 struct agp_memory *new;
243 struct page *page;
244
245 switch (pg_count) {
246 case 1: page = agp_bridge->driver->agp_alloc_page(agp_bridge);
247 break;
248 case 4:
249 /* kludge to get 4 physical pages for ARGB cursor */
250 page = i8xx_alloc_pages();
251 break;
252 default:
253 return NULL;
254 }
255
256 if (page == NULL)
257 return NULL;
258
259 new = agp_create_memory(pg_count);
260 if (new == NULL)
261 return NULL;
262
263 new->pages[0] = page;
264 if (pg_count == 4) {
265 /* kludge to get 4 physical pages for ARGB cursor */
266 new->pages[1] = new->pages[0] + 1;
267 new->pages[2] = new->pages[1] + 1;
268 new->pages[3] = new->pages[2] + 1;
269 }
270 new->page_count = pg_count;
271 new->num_scratch_pages = pg_count;
272 new->type = AGP_PHYS_MEMORY;
273 new->physical = page_to_phys(new->pages[0]);
274 return new;
275}
276
Daniel Vetterf51b7662010-04-14 00:29:52 +0200277static void intel_i810_free_by_type(struct agp_memory *curr)
278{
279 agp_free_key(curr->key);
280 if (curr->type == AGP_PHYS_MEMORY) {
281 if (curr->page_count == 4)
282 i8xx_destroy_pages(curr->pages[0]);
283 else {
284 agp_bridge->driver->agp_destroy_page(curr->pages[0],
285 AGP_PAGE_DESTROY_UNMAP);
286 agp_bridge->driver->agp_destroy_page(curr->pages[0],
287 AGP_PAGE_DESTROY_FREE);
288 }
289 agp_free_page_array(curr);
290 }
291 kfree(curr);
292}
293
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200294static int intel_gtt_setup_scratch_page(void)
295{
296 struct page *page;
297 dma_addr_t dma_addr;
298
299 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
300 if (page == NULL)
301 return -ENOMEM;
302 get_page(page);
303 set_pages_uc(page, 1);
304
Daniel Vetter40807752010-11-06 11:18:58 +0100305 if (intel_private.base.needs_dmar) {
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200306 dma_addr = pci_map_page(intel_private.pcidev, page, 0,
307 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
308 if (pci_dma_mapping_error(intel_private.pcidev, dma_addr))
309 return -EINVAL;
310
311 intel_private.scratch_page_dma = dma_addr;
312 } else
313 intel_private.scratch_page_dma = page_to_phys(page);
314
315 intel_private.scratch_page = page;
316
317 return 0;
318}
319
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100320static void i810_write_entry(dma_addr_t addr, unsigned int entry,
321 unsigned int flags)
322{
323 u32 pte_flags = I810_PTE_VALID;
324
325 switch (flags) {
326 case AGP_DCACHE_MEMORY:
327 pte_flags |= I810_PTE_LOCAL;
328 break;
329 case AGP_USER_CACHED_MEMORY:
330 pte_flags |= I830_PTE_SYSTEM_CACHED;
331 break;
332 }
333
334 writel(addr | pte_flags, intel_private.gtt + entry);
335}
336
Chris Wilson7bdc9ab2010-11-09 17:53:20 +0000337static const struct aper_size_info_fixed intel_fake_agp_sizes[] = {
Daniel Vetter820647b2010-11-05 13:30:14 +0100338 {32, 8192, 3},
339 {64, 16384, 4},
Daniel Vetterf51b7662010-04-14 00:29:52 +0200340 {128, 32768, 5},
Daniel Vetterf51b7662010-04-14 00:29:52 +0200341 {256, 65536, 6},
342 {512, 131072, 7},
343};
344
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000345static unsigned int intel_gtt_stolen_size(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200346{
347 u16 gmch_ctrl;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200348 u8 rdct;
349 int local = 0;
350 static const int ddt[4] = { 0, 16, 32, 64 };
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200351 unsigned int stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200352
Daniel Vetter820647b2010-11-05 13:30:14 +0100353 if (INTEL_GTT_GEN == 1)
354 return 0; /* no stolen mem on i81x */
355
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200356 pci_read_config_word(intel_private.bridge_dev,
357 I830_GMCH_CTRL, &gmch_ctrl);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200358
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200359 if (intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82830_HB ||
360 intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82845G_HB) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200361 switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
362 case I830_GMCH_GMS_STOLEN_512:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200363 stolen_size = KB(512);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200364 break;
365 case I830_GMCH_GMS_STOLEN_1024:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200366 stolen_size = MB(1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200367 break;
368 case I830_GMCH_GMS_STOLEN_8192:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200369 stolen_size = MB(8);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200370 break;
371 case I830_GMCH_GMS_LOCAL:
372 rdct = readb(intel_private.registers+I830_RDRAM_CHANNEL_TYPE);
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200373 stolen_size = (I830_RDRAM_ND(rdct) + 1) *
Daniel Vetterf51b7662010-04-14 00:29:52 +0200374 MB(ddt[I830_RDRAM_DDT(rdct)]);
375 local = 1;
376 break;
377 default:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200378 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200379 break;
380 }
Daniel Vetter1a997ff2010-09-08 21:18:53 +0200381 } else if (INTEL_GTT_GEN == 6) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200382 /*
383 * SandyBridge has new memory control reg at 0x50.w
384 */
385 u16 snb_gmch_ctl;
386 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl);
387 switch (snb_gmch_ctl & SNB_GMCH_GMS_STOLEN_MASK) {
388 case SNB_GMCH_GMS_STOLEN_32M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200389 stolen_size = MB(32);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200390 break;
391 case SNB_GMCH_GMS_STOLEN_64M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200392 stolen_size = MB(64);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200393 break;
394 case SNB_GMCH_GMS_STOLEN_96M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200395 stolen_size = MB(96);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200396 break;
397 case SNB_GMCH_GMS_STOLEN_128M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200398 stolen_size = MB(128);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200399 break;
400 case SNB_GMCH_GMS_STOLEN_160M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200401 stolen_size = MB(160);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200402 break;
403 case SNB_GMCH_GMS_STOLEN_192M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200404 stolen_size = MB(192);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200405 break;
406 case SNB_GMCH_GMS_STOLEN_224M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200407 stolen_size = MB(224);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200408 break;
409 case SNB_GMCH_GMS_STOLEN_256M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200410 stolen_size = MB(256);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200411 break;
412 case SNB_GMCH_GMS_STOLEN_288M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200413 stolen_size = MB(288);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200414 break;
415 case SNB_GMCH_GMS_STOLEN_320M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200416 stolen_size = MB(320);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200417 break;
418 case SNB_GMCH_GMS_STOLEN_352M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200419 stolen_size = MB(352);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200420 break;
421 case SNB_GMCH_GMS_STOLEN_384M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200422 stolen_size = MB(384);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200423 break;
424 case SNB_GMCH_GMS_STOLEN_416M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200425 stolen_size = MB(416);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200426 break;
427 case SNB_GMCH_GMS_STOLEN_448M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200428 stolen_size = MB(448);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200429 break;
430 case SNB_GMCH_GMS_STOLEN_480M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200431 stolen_size = MB(480);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200432 break;
433 case SNB_GMCH_GMS_STOLEN_512M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200434 stolen_size = MB(512);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200435 break;
436 }
437 } else {
438 switch (gmch_ctrl & I855_GMCH_GMS_MASK) {
439 case I855_GMCH_GMS_STOLEN_1M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200440 stolen_size = MB(1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200441 break;
442 case I855_GMCH_GMS_STOLEN_4M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200443 stolen_size = MB(4);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200444 break;
445 case I855_GMCH_GMS_STOLEN_8M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200446 stolen_size = MB(8);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200447 break;
448 case I855_GMCH_GMS_STOLEN_16M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200449 stolen_size = MB(16);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200450 break;
451 case I855_GMCH_GMS_STOLEN_32M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200452 stolen_size = MB(32);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200453 break;
454 case I915_GMCH_GMS_STOLEN_48M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200455 stolen_size = MB(48);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200456 break;
457 case I915_GMCH_GMS_STOLEN_64M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200458 stolen_size = MB(64);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200459 break;
460 case G33_GMCH_GMS_STOLEN_128M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200461 stolen_size = MB(128);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200462 break;
463 case G33_GMCH_GMS_STOLEN_256M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200464 stolen_size = MB(256);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200465 break;
466 case INTEL_GMCH_GMS_STOLEN_96M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200467 stolen_size = MB(96);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200468 break;
469 case INTEL_GMCH_GMS_STOLEN_160M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200470 stolen_size = MB(160);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200471 break;
472 case INTEL_GMCH_GMS_STOLEN_224M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200473 stolen_size = MB(224);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200474 break;
475 case INTEL_GMCH_GMS_STOLEN_352M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200476 stolen_size = MB(352);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200477 break;
478 default:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200479 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200480 break;
481 }
482 }
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200483
Chris Wilson1b6064d2010-11-23 12:33:54 +0000484 if (stolen_size > 0) {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200485 dev_info(&intel_private.bridge_dev->dev, "detected %dK %s memory\n",
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200486 stolen_size / KB(1), local ? "local" : "stolen");
Daniel Vetterf51b7662010-04-14 00:29:52 +0200487 } else {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200488 dev_info(&intel_private.bridge_dev->dev,
Daniel Vetterf51b7662010-04-14 00:29:52 +0200489 "no pre-allocated video memory detected\n");
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200490 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200491 }
492
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000493 return stolen_size;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200494}
495
Daniel Vetter20172842010-09-24 18:25:59 +0200496static void i965_adjust_pgetbl_size(unsigned int size_flag)
497{
498 u32 pgetbl_ctl, pgetbl_ctl2;
499
500 /* ensure that ppgtt is disabled */
501 pgetbl_ctl2 = readl(intel_private.registers+I965_PGETBL_CTL2);
502 pgetbl_ctl2 &= ~I810_PGETBL_ENABLED;
503 writel(pgetbl_ctl2, intel_private.registers+I965_PGETBL_CTL2);
504
505 /* write the new ggtt size */
506 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
507 pgetbl_ctl &= ~I965_PGETBL_SIZE_MASK;
508 pgetbl_ctl |= size_flag;
509 writel(pgetbl_ctl, intel_private.registers+I810_PGETBL_CTL);
510}
511
512static unsigned int i965_gtt_total_entries(void)
513{
514 int size;
515 u32 pgetbl_ctl;
516 u16 gmch_ctl;
517
518 pci_read_config_word(intel_private.bridge_dev,
519 I830_GMCH_CTRL, &gmch_ctl);
520
521 if (INTEL_GTT_GEN == 5) {
522 switch (gmch_ctl & G4x_GMCH_SIZE_MASK) {
523 case G4x_GMCH_SIZE_1M:
524 case G4x_GMCH_SIZE_VT_1M:
525 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1MB);
526 break;
527 case G4x_GMCH_SIZE_VT_1_5M:
528 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1_5MB);
529 break;
530 case G4x_GMCH_SIZE_2M:
531 case G4x_GMCH_SIZE_VT_2M:
532 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_2MB);
533 break;
534 }
535 }
536
537 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
538
539 switch (pgetbl_ctl & I965_PGETBL_SIZE_MASK) {
540 case I965_PGETBL_SIZE_128KB:
541 size = KB(128);
542 break;
543 case I965_PGETBL_SIZE_256KB:
544 size = KB(256);
545 break;
546 case I965_PGETBL_SIZE_512KB:
547 size = KB(512);
548 break;
549 /* GTT pagetable sizes bigger than 512KB are not possible on G33! */
550 case I965_PGETBL_SIZE_1MB:
551 size = KB(1024);
552 break;
553 case I965_PGETBL_SIZE_2MB:
554 size = KB(2048);
555 break;
556 case I965_PGETBL_SIZE_1_5MB:
557 size = KB(1024 + 512);
558 break;
559 default:
560 dev_info(&intel_private.pcidev->dev,
561 "unknown page table size, assuming 512KB\n");
562 size = KB(512);
563 }
564
565 return size/4;
566}
567
Daniel Vetterfbe40782010-08-27 17:12:41 +0200568static unsigned int intel_gtt_total_entries(void)
569{
570 int size;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200571
Daniel Vetter20172842010-09-24 18:25:59 +0200572 if (IS_G33 || INTEL_GTT_GEN == 4 || INTEL_GTT_GEN == 5)
573 return i965_gtt_total_entries();
574 else if (INTEL_GTT_GEN == 6) {
Daniel Vetter210b23c2010-08-28 16:14:32 +0200575 u16 snb_gmch_ctl;
576
577 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl);
578 switch (snb_gmch_ctl & SNB_GTT_SIZE_MASK) {
579 default:
580 case SNB_GTT_SIZE_0M:
581 printk(KERN_ERR "Bad GTT size mask: 0x%04x.\n", snb_gmch_ctl);
582 size = MB(0);
583 break;
584 case SNB_GTT_SIZE_1M:
585 size = MB(1);
586 break;
587 case SNB_GTT_SIZE_2M:
588 size = MB(2);
589 break;
590 }
591 return size/4;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200592 } else {
593 /* On previous hardware, the GTT size was just what was
594 * required to map the aperture.
595 */
Daniel Vettere5e408f2010-08-28 11:04:32 +0200596 return intel_private.base.gtt_mappable_entries;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200597 }
Daniel Vetterfbe40782010-08-27 17:12:41 +0200598}
Daniel Vetterfbe40782010-08-27 17:12:41 +0200599
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200600static unsigned int intel_gtt_mappable_entries(void)
601{
602 unsigned int aperture_size;
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200603
Daniel Vetter820647b2010-11-05 13:30:14 +0100604 if (INTEL_GTT_GEN == 1) {
605 u32 smram_miscc;
606
607 pci_read_config_dword(intel_private.bridge_dev,
608 I810_SMRAM_MISCC, &smram_miscc);
609
610 if ((smram_miscc & I810_GFX_MEM_WIN_SIZE)
611 == I810_GFX_MEM_WIN_32M)
612 aperture_size = MB(32);
613 else
614 aperture_size = MB(64);
615 } else if (INTEL_GTT_GEN == 2) {
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100616 u16 gmch_ctrl;
617
618 pci_read_config_word(intel_private.bridge_dev,
619 I830_GMCH_CTRL, &gmch_ctrl);
620
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200621 if ((gmch_ctrl & I830_GMCH_MEM_MASK) == I830_GMCH_MEM_64M)
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100622 aperture_size = MB(64);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200623 else
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100624 aperture_size = MB(128);
Daniel Vetter239918f2010-08-31 22:30:43 +0200625 } else {
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200626 /* 9xx supports large sizes, just look at the length */
627 aperture_size = pci_resource_len(intel_private.pcidev, 2);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200628 }
629
630 return aperture_size >> PAGE_SHIFT;
631}
632
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200633static void intel_gtt_teardown_scratch_page(void)
634{
635 set_pages_wb(intel_private.scratch_page, 1);
636 pci_unmap_page(intel_private.pcidev, intel_private.scratch_page_dma,
637 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
638 put_page(intel_private.scratch_page);
639 __free_page(intel_private.scratch_page);
640}
641
642static void intel_gtt_cleanup(void)
643{
Daniel Vetterae83dd52010-09-12 17:11:15 +0200644 intel_private.driver->cleanup();
645
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200646 iounmap(intel_private.gtt);
647 iounmap(intel_private.registers);
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100648
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200649 intel_gtt_teardown_scratch_page();
650}
651
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200652static int intel_gtt_init(void)
653{
Daniel Vetterf67eab62010-08-29 17:27:36 +0200654 u32 gtt_map_size;
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200655 int ret;
656
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200657 ret = intel_private.driver->setup();
658 if (ret != 0)
659 return ret;
Daniel Vetterf67eab62010-08-29 17:27:36 +0200660
661 intel_private.base.gtt_mappable_entries = intel_gtt_mappable_entries();
662 intel_private.base.gtt_total_entries = intel_gtt_total_entries();
663
Daniel Vetterb3eafc52010-09-23 20:04:17 +0200664 /* save the PGETBL reg for resume */
665 intel_private.PGETBL_save =
666 readl(intel_private.registers+I810_PGETBL_CTL)
667 & ~I810_PGETBL_ENABLED;
Chris Wilson100519e2010-10-31 10:37:02 +0000668 /* we only ever restore the register when enabling the PGTBL... */
669 if (HAS_PGTBL_EN)
670 intel_private.PGETBL_save |= I810_PGETBL_ENABLED;
Daniel Vetterb3eafc52010-09-23 20:04:17 +0200671
Daniel Vetter0af9e922010-09-12 14:04:03 +0200672 dev_info(&intel_private.bridge_dev->dev,
673 "detected gtt size: %dK total, %dK mappable\n",
674 intel_private.base.gtt_total_entries * 4,
675 intel_private.base.gtt_mappable_entries * 4);
676
Daniel Vetterf67eab62010-08-29 17:27:36 +0200677 gtt_map_size = intel_private.base.gtt_total_entries * 4;
678
679 intel_private.gtt = ioremap(intel_private.gtt_bus_addr,
680 gtt_map_size);
681 if (!intel_private.gtt) {
Daniel Vetterae83dd52010-09-12 17:11:15 +0200682 intel_private.driver->cleanup();
Daniel Vetterf67eab62010-08-29 17:27:36 +0200683 iounmap(intel_private.registers);
684 return -ENOMEM;
685 }
686
687 global_cache_flush(); /* FIXME: ? */
688
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000689 intel_private.base.stolen_size = intel_gtt_stolen_size();
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200690
Dave Airliea46f3102011-01-12 11:38:37 +1000691 intel_private.base.needs_dmar = USE_PCI_DMA_API && INTEL_GTT_GEN > 2;
692
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200693 ret = intel_gtt_setup_scratch_page();
694 if (ret != 0) {
695 intel_gtt_cleanup();
696 return ret;
697 }
698
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200699 return 0;
700}
701
Daniel Vetter3e921f92010-08-27 15:33:26 +0200702static int intel_fake_agp_fetch_size(void)
703{
Chris Wilson9e76e7b2010-09-14 12:12:11 +0100704 int num_sizes = ARRAY_SIZE(intel_fake_agp_sizes);
Daniel Vetter3e921f92010-08-27 15:33:26 +0200705 unsigned int aper_size;
706 int i;
Daniel Vetter3e921f92010-08-27 15:33:26 +0200707
708 aper_size = (intel_private.base.gtt_mappable_entries << PAGE_SHIFT)
709 / MB(1);
710
711 for (i = 0; i < num_sizes; i++) {
Daniel Vetterffdd7512010-08-27 17:51:29 +0200712 if (aper_size == intel_fake_agp_sizes[i].size) {
Chris Wilson9e76e7b2010-09-14 12:12:11 +0100713 agp_bridge->current_size =
714 (void *) (intel_fake_agp_sizes + i);
Daniel Vetter3e921f92010-08-27 15:33:26 +0200715 return aper_size;
716 }
717 }
718
719 return 0;
720}
721
Daniel Vetterae83dd52010-09-12 17:11:15 +0200722static void i830_cleanup(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200723{
Takashi Iwai136711b2010-12-04 16:13:06 +0100724 if (intel_private.i8xx_flush_page) {
725 kunmap(intel_private.i8xx_flush_page);
726 intel_private.i8xx_flush_page = NULL;
727 }
Daniel Vetterf51b7662010-04-14 00:29:52 +0200728
729 __free_page(intel_private.i8xx_page);
730 intel_private.i8xx_page = NULL;
731}
732
733static void intel_i830_setup_flush(void)
734{
735 /* return if we've already set the flush mechanism up */
736 if (intel_private.i8xx_page)
737 return;
738
Jan Beuliche61cb0d2010-09-24 13:25:30 +0100739 intel_private.i8xx_page = alloc_page(GFP_KERNEL);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200740 if (!intel_private.i8xx_page)
741 return;
742
743 intel_private.i8xx_flush_page = kmap(intel_private.i8xx_page);
744 if (!intel_private.i8xx_flush_page)
Daniel Vetterae83dd52010-09-12 17:11:15 +0200745 i830_cleanup();
Daniel Vetterf51b7662010-04-14 00:29:52 +0200746}
747
748/* The chipset_flush interface needs to get data that has already been
749 * flushed out of the CPU all the way out to main memory, because the GPU
750 * doesn't snoop those buffers.
751 *
752 * The 8xx series doesn't have the same lovely interface for flushing the
753 * chipset write buffers that the later chips do. According to the 865
754 * specs, it's 64 octwords, or 1KB. So, to get those previous things in
755 * that buffer out, we just fill 1KB and clflush it out, on the assumption
756 * that it'll push whatever was in there out. It appears to work.
757 */
Daniel Vetter1b263f22010-09-12 00:27:24 +0200758static void i830_chipset_flush(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200759{
760 unsigned int *pg = intel_private.i8xx_flush_page;
761
762 memset(pg, 0, 1024);
763
764 if (cpu_has_clflush)
765 clflush_cache_range(pg, 1024);
766 else if (wbinvd_on_all_cpus() != 0)
767 printk(KERN_ERR "Timed out waiting for cache flush.\n");
768}
769
Daniel Vetter351bb272010-09-07 22:41:04 +0200770static void i830_write_entry(dma_addr_t addr, unsigned int entry,
771 unsigned int flags)
772{
773 u32 pte_flags = I810_PTE_VALID;
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100774
Daniel Vetterb47cf662010-11-04 18:41:50 +0100775 if (flags == AGP_USER_CACHED_MEMORY)
Daniel Vetter351bb272010-09-07 22:41:04 +0200776 pte_flags |= I830_PTE_SYSTEM_CACHED;
Daniel Vetter351bb272010-09-07 22:41:04 +0200777
778 writel(addr | pte_flags, intel_private.gtt + entry);
779}
780
Chris Wilsone380f602010-10-29 18:11:26 +0100781static bool intel_enable_gtt(void)
Daniel Vetter73800422010-08-29 17:29:50 +0200782{
Chris Wilson3f08e4e2010-09-14 20:15:22 +0100783 u32 gma_addr;
Chris Wilsone380f602010-10-29 18:11:26 +0100784 u8 __iomem *reg;
Daniel Vetter73800422010-08-29 17:29:50 +0200785
Daniel Vetter820647b2010-11-05 13:30:14 +0100786 if (INTEL_GTT_GEN <= 2)
Daniel Vetter2d2430c2010-08-29 17:35:30 +0200787 pci_read_config_dword(intel_private.pcidev, I810_GMADDR,
788 &gma_addr);
789 else
790 pci_read_config_dword(intel_private.pcidev, I915_GMADDR,
791 &gma_addr);
792
Daniel Vetter73800422010-08-29 17:29:50 +0200793 intel_private.gma_bus_addr = (gma_addr & PCI_BASE_ADDRESS_MEM_MASK);
794
Chris Wilsone380f602010-10-29 18:11:26 +0100795 if (INTEL_GTT_GEN >= 6)
796 return true;
Daniel Vetter73800422010-08-29 17:29:50 +0200797
Chris Wilson100519e2010-10-31 10:37:02 +0000798 if (INTEL_GTT_GEN == 2) {
799 u16 gmch_ctrl;
Chris Wilsone380f602010-10-29 18:11:26 +0100800
Chris Wilson100519e2010-10-31 10:37:02 +0000801 pci_read_config_word(intel_private.bridge_dev,
802 I830_GMCH_CTRL, &gmch_ctrl);
803 gmch_ctrl |= I830_GMCH_ENABLED;
804 pci_write_config_word(intel_private.bridge_dev,
805 I830_GMCH_CTRL, gmch_ctrl);
806
807 pci_read_config_word(intel_private.bridge_dev,
808 I830_GMCH_CTRL, &gmch_ctrl);
809 if ((gmch_ctrl & I830_GMCH_ENABLED) == 0) {
810 dev_err(&intel_private.pcidev->dev,
811 "failed to enable the GTT: GMCH_CTRL=%x\n",
812 gmch_ctrl);
813 return false;
814 }
Chris Wilsone380f602010-10-29 18:11:26 +0100815 }
816
Chris Wilsonc97689d2010-12-23 10:40:38 +0000817 /* On the resume path we may be adjusting the PGTBL value, so
818 * be paranoid and flush all chipset write buffers...
819 */
820 if (INTEL_GTT_GEN >= 3)
821 writel(0, intel_private.registers+GFX_FLSH_CNTL);
822
Chris Wilsone380f602010-10-29 18:11:26 +0100823 reg = intel_private.registers+I810_PGETBL_CTL;
Chris Wilson100519e2010-10-31 10:37:02 +0000824 writel(intel_private.PGETBL_save, reg);
825 if (HAS_PGTBL_EN && (readl(reg) & I810_PGETBL_ENABLED) == 0) {
Chris Wilsone380f602010-10-29 18:11:26 +0100826 dev_err(&intel_private.pcidev->dev,
Chris Wilson100519e2010-10-31 10:37:02 +0000827 "failed to enable the GTT: PGETBL=%x [expected %x]\n",
Chris Wilsone380f602010-10-29 18:11:26 +0100828 readl(reg), intel_private.PGETBL_save);
829 return false;
830 }
831
Chris Wilsonc97689d2010-12-23 10:40:38 +0000832 if (INTEL_GTT_GEN >= 3)
833 writel(0, intel_private.registers+GFX_FLSH_CNTL);
834
Chris Wilsone380f602010-10-29 18:11:26 +0100835 return true;
Daniel Vetter73800422010-08-29 17:29:50 +0200836}
837
838static int i830_setup(void)
839{
840 u32 reg_addr;
841
842 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &reg_addr);
843 reg_addr &= 0xfff80000;
844
845 intel_private.registers = ioremap(reg_addr, KB(64));
846 if (!intel_private.registers)
847 return -ENOMEM;
848
849 intel_private.gtt_bus_addr = reg_addr + I810_PTE_BASE;
850
851 intel_i830_setup_flush();
852
853 return 0;
854}
855
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200856static int intel_fake_agp_create_gatt_table(struct agp_bridge_data *bridge)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200857{
Daniel Vetter73800422010-08-29 17:29:50 +0200858 agp_bridge->gatt_table_real = NULL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200859 agp_bridge->gatt_table = NULL;
Daniel Vetter73800422010-08-29 17:29:50 +0200860 agp_bridge->gatt_bus_addr = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200861
862 return 0;
863}
864
Daniel Vetterffdd7512010-08-27 17:51:29 +0200865static int intel_fake_agp_free_gatt_table(struct agp_bridge_data *bridge)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200866{
867 return 0;
868}
869
Daniel Vetter351bb272010-09-07 22:41:04 +0200870static int intel_fake_agp_configure(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200871{
Daniel Vetterf51b7662010-04-14 00:29:52 +0200872 int i;
873
Chris Wilsone380f602010-10-29 18:11:26 +0100874 if (!intel_enable_gtt())
875 return -EIO;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200876
Daniel Vetter73800422010-08-29 17:29:50 +0200877 agp_bridge->gart_bus_addr = intel_private.gma_bus_addr;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200878
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000879 for (i = 0; i < intel_private.base.gtt_total_entries; i++) {
Daniel Vetter351bb272010-09-07 22:41:04 +0200880 intel_private.driver->write_entry(intel_private.scratch_page_dma,
881 i, 0);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200882 }
Daniel Vetter351bb272010-09-07 22:41:04 +0200883 readl(intel_private.gtt+i-1); /* PCI Posting. */
Daniel Vetterf51b7662010-04-14 00:29:52 +0200884
885 global_cache_flush();
886
Daniel Vetterf51b7662010-04-14 00:29:52 +0200887 return 0;
888}
889
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200890static bool i830_check_flags(unsigned int flags)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200891{
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200892 switch (flags) {
893 case 0:
894 case AGP_PHYS_MEMORY:
895 case AGP_USER_CACHED_MEMORY:
896 case AGP_USER_MEMORY:
897 return true;
898 }
899
900 return false;
901}
902
Daniel Vetter40807752010-11-06 11:18:58 +0100903void intel_gtt_insert_sg_entries(struct scatterlist *sg_list,
904 unsigned int sg_len,
905 unsigned int pg_start,
906 unsigned int flags)
Daniel Vetterfefaa702010-09-11 22:12:11 +0200907{
908 struct scatterlist *sg;
909 unsigned int len, m;
910 int i, j;
911
912 j = pg_start;
913
914 /* sg may merge pages, but we have to separate
915 * per-page addr for GTT */
916 for_each_sg(sg_list, sg, sg_len, i) {
917 len = sg_dma_len(sg) >> PAGE_SHIFT;
918 for (m = 0; m < len; m++) {
919 dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
920 intel_private.driver->write_entry(addr,
921 j, flags);
922 j++;
923 }
924 }
925 readl(intel_private.gtt+j-1);
926}
Daniel Vetter40807752010-11-06 11:18:58 +0100927EXPORT_SYMBOL(intel_gtt_insert_sg_entries);
928
929void intel_gtt_insert_pages(unsigned int first_entry, unsigned int num_entries,
930 struct page **pages, unsigned int flags)
931{
932 int i, j;
933
934 for (i = 0, j = first_entry; i < num_entries; i++, j++) {
935 dma_addr_t addr = page_to_phys(pages[i]);
936 intel_private.driver->write_entry(addr,
937 j, flags);
938 }
939 readl(intel_private.gtt+j-1);
940}
941EXPORT_SYMBOL(intel_gtt_insert_pages);
Daniel Vetterfefaa702010-09-11 22:12:11 +0200942
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200943static int intel_fake_agp_insert_entries(struct agp_memory *mem,
944 off_t pg_start, int type)
945{
Daniel Vetterf51b7662010-04-14 00:29:52 +0200946 int ret = -EINVAL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200947
Daniel Vetterff268602010-11-05 15:43:35 +0100948 if (INTEL_GTT_GEN == 1 && type == AGP_DCACHE_MEMORY)
949 return i810_insert_dcache_entries(mem, pg_start, type);
950
Daniel Vetterf51b7662010-04-14 00:29:52 +0200951 if (mem->page_count == 0)
952 goto out;
953
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000954 if (pg_start + mem->page_count > intel_private.base.gtt_total_entries)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200955 goto out_err;
956
Daniel Vetterf51b7662010-04-14 00:29:52 +0200957 if (type != mem->type)
958 goto out_err;
959
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200960 if (!intel_private.driver->check_flags(type))
Daniel Vetterf51b7662010-04-14 00:29:52 +0200961 goto out_err;
962
963 if (!mem->is_flushed)
964 global_cache_flush();
965
Daniel Vetter40807752010-11-06 11:18:58 +0100966 if (intel_private.base.needs_dmar) {
967 ret = intel_gtt_map_memory(mem->pages, mem->page_count,
968 &mem->sg_list, &mem->num_sg);
Daniel Vetterfefaa702010-09-11 22:12:11 +0200969 if (ret != 0)
970 return ret;
971
972 intel_gtt_insert_sg_entries(mem->sg_list, mem->num_sg,
973 pg_start, type);
Daniel Vetter40807752010-11-06 11:18:58 +0100974 } else
975 intel_gtt_insert_pages(pg_start, mem->page_count, mem->pages,
976 type);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200977
978out:
979 ret = 0;
980out_err:
981 mem->is_flushed = true;
982 return ret;
983}
984
Daniel Vetter40807752010-11-06 11:18:58 +0100985void intel_gtt_clear_range(unsigned int first_entry, unsigned int num_entries)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200986{
Daniel Vetter40807752010-11-06 11:18:58 +0100987 unsigned int i;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200988
Daniel Vetter40807752010-11-06 11:18:58 +0100989 for (i = first_entry; i < (first_entry + num_entries); i++) {
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200990 intel_private.driver->write_entry(intel_private.scratch_page_dma,
991 i, 0);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200992 }
Daniel Vetterfdfb58a2010-08-29 00:15:03 +0200993 readl(intel_private.gtt+i-1);
Daniel Vetter40807752010-11-06 11:18:58 +0100994}
995EXPORT_SYMBOL(intel_gtt_clear_range);
996
997static int intel_fake_agp_remove_entries(struct agp_memory *mem,
998 off_t pg_start, int type)
999{
1000 if (mem->page_count == 0)
1001 return 0;
1002
Dave Airlied15eda52011-01-12 11:39:48 +10001003 intel_gtt_clear_range(pg_start, mem->page_count);
1004
Daniel Vetter40807752010-11-06 11:18:58 +01001005 if (intel_private.base.needs_dmar) {
1006 intel_gtt_unmap_memory(mem->sg_list, mem->num_sg);
1007 mem->sg_list = NULL;
1008 mem->num_sg = 0;
1009 }
1010
Daniel Vetterf51b7662010-04-14 00:29:52 +02001011 return 0;
1012}
1013
Daniel Vetterffdd7512010-08-27 17:51:29 +02001014static struct agp_memory *intel_fake_agp_alloc_by_type(size_t pg_count,
1015 int type)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001016{
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001017 struct agp_memory *new;
1018
1019 if (type == AGP_DCACHE_MEMORY && INTEL_GTT_GEN == 1) {
1020 if (pg_count != intel_private.num_dcache_entries)
1021 return NULL;
1022
1023 new = agp_create_memory(1);
1024 if (new == NULL)
1025 return NULL;
1026
1027 new->type = AGP_DCACHE_MEMORY;
1028 new->page_count = pg_count;
1029 new->num_scratch_pages = 0;
1030 agp_free_page_array(new);
1031 return new;
1032 }
Daniel Vetterf51b7662010-04-14 00:29:52 +02001033 if (type == AGP_PHYS_MEMORY)
1034 return alloc_agpphysmem_i8xx(pg_count, type);
1035 /* always return NULL for other allocation types for now */
1036 return NULL;
1037}
1038
1039static int intel_alloc_chipset_flush_resource(void)
1040{
1041 int ret;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001042 ret = pci_bus_alloc_resource(intel_private.bridge_dev->bus, &intel_private.ifp_resource, PAGE_SIZE,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001043 PAGE_SIZE, PCIBIOS_MIN_MEM, 0,
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001044 pcibios_align_resource, intel_private.bridge_dev);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001045
1046 return ret;
1047}
1048
1049static void intel_i915_setup_chipset_flush(void)
1050{
1051 int ret;
1052 u32 temp;
1053
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001054 pci_read_config_dword(intel_private.bridge_dev, I915_IFPADDR, &temp);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001055 if (!(temp & 0x1)) {
1056 intel_alloc_chipset_flush_resource();
1057 intel_private.resource_valid = 1;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001058 pci_write_config_dword(intel_private.bridge_dev, I915_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001059 } else {
1060 temp &= ~1;
1061
1062 intel_private.resource_valid = 1;
1063 intel_private.ifp_resource.start = temp;
1064 intel_private.ifp_resource.end = temp + PAGE_SIZE;
1065 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
1066 /* some BIOSes reserve this area in a pnp some don't */
1067 if (ret)
1068 intel_private.resource_valid = 0;
1069 }
1070}
1071
1072static void intel_i965_g33_setup_chipset_flush(void)
1073{
1074 u32 temp_hi, temp_lo;
1075 int ret;
1076
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001077 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4, &temp_hi);
1078 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR, &temp_lo);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001079
1080 if (!(temp_lo & 0x1)) {
1081
1082 intel_alloc_chipset_flush_resource();
1083
1084 intel_private.resource_valid = 1;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001085 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001086 upper_32_bits(intel_private.ifp_resource.start));
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001087 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
Daniel Vetterf51b7662010-04-14 00:29:52 +02001088 } else {
1089 u64 l64;
1090
1091 temp_lo &= ~0x1;
1092 l64 = ((u64)temp_hi << 32) | temp_lo;
1093
1094 intel_private.resource_valid = 1;
1095 intel_private.ifp_resource.start = l64;
1096 intel_private.ifp_resource.end = l64 + PAGE_SIZE;
1097 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
1098 /* some BIOSes reserve this area in a pnp some don't */
1099 if (ret)
1100 intel_private.resource_valid = 0;
1101 }
1102}
1103
1104static void intel_i9xx_setup_flush(void)
1105{
1106 /* return if already configured */
1107 if (intel_private.ifp_resource.start)
1108 return;
1109
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001110 if (INTEL_GTT_GEN == 6)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001111 return;
1112
1113 /* setup a resource for this object */
1114 intel_private.ifp_resource.name = "Intel Flush Page";
1115 intel_private.ifp_resource.flags = IORESOURCE_MEM;
1116
1117 /* Setup chipset flush for 915 */
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001118 if (IS_G33 || INTEL_GTT_GEN >= 4) {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001119 intel_i965_g33_setup_chipset_flush();
1120 } else {
1121 intel_i915_setup_chipset_flush();
1122 }
1123
Chris Wilsondf51e7a2010-09-04 14:57:27 +01001124 if (intel_private.ifp_resource.start)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001125 intel_private.i9xx_flush_page = ioremap_nocache(intel_private.ifp_resource.start, PAGE_SIZE);
Chris Wilsondf51e7a2010-09-04 14:57:27 +01001126 if (!intel_private.i9xx_flush_page)
1127 dev_err(&intel_private.pcidev->dev,
1128 "can't ioremap flush page - no chipset flushing\n");
Daniel Vetterf51b7662010-04-14 00:29:52 +02001129}
1130
Daniel Vetterae83dd52010-09-12 17:11:15 +02001131static void i9xx_cleanup(void)
1132{
1133 if (intel_private.i9xx_flush_page)
1134 iounmap(intel_private.i9xx_flush_page);
1135 if (intel_private.resource_valid)
1136 release_resource(&intel_private.ifp_resource);
1137 intel_private.ifp_resource.start = 0;
1138 intel_private.resource_valid = 0;
1139}
1140
Daniel Vetter1b263f22010-09-12 00:27:24 +02001141static void i9xx_chipset_flush(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001142{
1143 if (intel_private.i9xx_flush_page)
1144 writel(1, intel_private.i9xx_flush_page);
1145}
1146
Chris Wilson71f45662010-12-14 11:29:23 +00001147static void i965_write_entry(dma_addr_t addr,
1148 unsigned int entry,
Daniel Vettera6963592010-09-11 14:01:43 +02001149 unsigned int flags)
1150{
Chris Wilson71f45662010-12-14 11:29:23 +00001151 u32 pte_flags;
1152
1153 pte_flags = I810_PTE_VALID;
1154 if (flags == AGP_USER_CACHED_MEMORY)
1155 pte_flags |= I830_PTE_SYSTEM_CACHED;
1156
Daniel Vettera6963592010-09-11 14:01:43 +02001157 /* Shift high bits down */
1158 addr |= (addr >> 28) & 0xf0;
Chris Wilson71f45662010-12-14 11:29:23 +00001159 writel(addr | pte_flags, intel_private.gtt + entry);
Daniel Vettera6963592010-09-11 14:01:43 +02001160}
1161
Daniel Vetter90cb1492010-09-11 23:55:20 +02001162static bool gen6_check_flags(unsigned int flags)
1163{
1164 return true;
1165}
1166
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001167static void gen6_write_entry(dma_addr_t addr, unsigned int entry,
1168 unsigned int flags)
1169{
1170 unsigned int type_mask = flags & ~AGP_USER_CACHED_MEMORY_GFDT;
1171 unsigned int gfdt = flags & AGP_USER_CACHED_MEMORY_GFDT;
1172 u32 pte_flags;
1173
Zhenyu Wang897ef192010-11-02 17:30:47 +08001174 if (type_mask == AGP_USER_MEMORY)
Chris Wilson85ccc352010-10-22 14:59:29 +01001175 pte_flags = GEN6_PTE_UNCACHED | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001176 else if (type_mask == AGP_USER_CACHED_MEMORY_LLC_MLC) {
Zhenyu Wangd1108522010-11-02 17:30:46 +08001177 pte_flags = GEN6_PTE_LLC_MLC | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001178 if (gfdt)
1179 pte_flags |= GEN6_PTE_GFDT;
1180 } else { /* set 'normal'/'cached' to LLC by default */
Zhenyu Wangd1108522010-11-02 17:30:46 +08001181 pte_flags = GEN6_PTE_LLC | I810_PTE_VALID;
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001182 if (gfdt)
1183 pte_flags |= GEN6_PTE_GFDT;
1184 }
1185
1186 /* gen6 has bit11-4 for physical addr bit39-32 */
1187 addr |= (addr >> 28) & 0xff0;
1188 writel(addr | pte_flags, intel_private.gtt + entry);
1189}
1190
Daniel Vetterae83dd52010-09-12 17:11:15 +02001191static void gen6_cleanup(void)
1192{
1193}
1194
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001195static int i9xx_setup(void)
1196{
1197 u32 reg_addr;
1198
1199 pci_read_config_dword(intel_private.pcidev, I915_MMADDR, &reg_addr);
1200
1201 reg_addr &= 0xfff80000;
1202
1203 intel_private.registers = ioremap(reg_addr, 128 * 4096);
1204 if (!intel_private.registers)
1205 return -ENOMEM;
1206
1207 if (INTEL_GTT_GEN == 3) {
1208 u32 gtt_addr;
Chris Wilson3f08e4e2010-09-14 20:15:22 +01001209
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001210 pci_read_config_dword(intel_private.pcidev,
1211 I915_PTEADDR, &gtt_addr);
1212 intel_private.gtt_bus_addr = gtt_addr;
1213 } else {
1214 u32 gtt_offset;
1215
1216 switch (INTEL_GTT_GEN) {
1217 case 5:
1218 case 6:
1219 gtt_offset = MB(2);
1220 break;
1221 case 4:
1222 default:
1223 gtt_offset = KB(512);
1224 break;
1225 }
1226 intel_private.gtt_bus_addr = reg_addr + gtt_offset;
1227 }
1228
1229 intel_i9xx_setup_flush();
1230
1231 return 0;
1232}
1233
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001234static const struct agp_bridge_driver intel_fake_agp_driver = {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001235 .owner = THIS_MODULE,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001236 .size_type = FIXED_APER_SIZE,
Chris Wilson9e76e7b2010-09-14 12:12:11 +01001237 .aperture_sizes = intel_fake_agp_sizes,
1238 .num_aperture_sizes = ARRAY_SIZE(intel_fake_agp_sizes),
Daniel Vettera6963592010-09-11 14:01:43 +02001239 .configure = intel_fake_agp_configure,
Daniel Vetter3e921f92010-08-27 15:33:26 +02001240 .fetch_size = intel_fake_agp_fetch_size,
Daniel Vetterfdfb58a2010-08-29 00:15:03 +02001241 .cleanup = intel_gtt_cleanup,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001242 .agp_enable = intel_fake_agp_enable,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001243 .cache_flush = global_cache_flush,
Daniel Vetter3b15a9d2010-08-29 14:18:49 +02001244 .create_gatt_table = intel_fake_agp_create_gatt_table,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001245 .free_gatt_table = intel_fake_agp_free_gatt_table,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001246 .insert_memory = intel_fake_agp_insert_entries,
1247 .remove_memory = intel_fake_agp_remove_entries,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001248 .alloc_by_type = intel_fake_agp_alloc_by_type,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001249 .free_by_type = intel_i810_free_by_type,
1250 .agp_alloc_page = agp_generic_alloc_page,
1251 .agp_alloc_pages = agp_generic_alloc_pages,
1252 .agp_destroy_page = agp_generic_destroy_page,
1253 .agp_destroy_pages = agp_generic_destroy_pages,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001254};
Daniel Vetter02c026c2010-08-24 19:39:48 +02001255
Daniel Vetterbdd30722010-09-12 12:34:44 +02001256static const struct intel_gtt_driver i81x_gtt_driver = {
1257 .gen = 1,
Daniel Vetter820647b2010-11-05 13:30:14 +01001258 .has_pgtbl_enable = 1,
Daniel Vetter22533b42010-09-12 16:38:55 +02001259 .dma_mask_size = 32,
Daniel Vetter820647b2010-11-05 13:30:14 +01001260 .setup = i810_setup,
1261 .cleanup = i810_cleanup,
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001262 .check_flags = i830_check_flags,
1263 .write_entry = i810_write_entry,
Daniel Vetterbdd30722010-09-12 12:34:44 +02001264};
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001265static const struct intel_gtt_driver i8xx_gtt_driver = {
1266 .gen = 2,
Chris Wilson100519e2010-10-31 10:37:02 +00001267 .has_pgtbl_enable = 1,
Daniel Vetter73800422010-08-29 17:29:50 +02001268 .setup = i830_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001269 .cleanup = i830_cleanup,
Daniel Vetter351bb272010-09-07 22:41:04 +02001270 .write_entry = i830_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001271 .dma_mask_size = 32,
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001272 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001273 .chipset_flush = i830_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001274};
1275static const struct intel_gtt_driver i915_gtt_driver = {
1276 .gen = 3,
Chris Wilson100519e2010-10-31 10:37:02 +00001277 .has_pgtbl_enable = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001278 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001279 .cleanup = i9xx_cleanup,
Daniel Vetter351bb272010-09-07 22:41:04 +02001280 /* i945 is the last gpu to need phys mem (for overlay and cursors). */
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001281 .write_entry = i830_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001282 .dma_mask_size = 32,
Daniel Vetterfefaa702010-09-11 22:12:11 +02001283 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001284 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001285};
1286static const struct intel_gtt_driver g33_gtt_driver = {
1287 .gen = 3,
1288 .is_g33 = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001289 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001290 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001291 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001292 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001293 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001294 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001295};
1296static const struct intel_gtt_driver pineview_gtt_driver = {
1297 .gen = 3,
1298 .is_pineview = 1, .is_g33 = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001299 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001300 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001301 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001302 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001303 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001304 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001305};
1306static const struct intel_gtt_driver i965_gtt_driver = {
1307 .gen = 4,
Chris Wilson100519e2010-10-31 10:37:02 +00001308 .has_pgtbl_enable = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001309 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001310 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001311 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001312 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001313 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001314 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001315};
1316static const struct intel_gtt_driver g4x_gtt_driver = {
1317 .gen = 5,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001318 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001319 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001320 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001321 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001322 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001323 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001324};
1325static const struct intel_gtt_driver ironlake_gtt_driver = {
1326 .gen = 5,
1327 .is_ironlake = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001328 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001329 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001330 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001331 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001332 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001333 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001334};
1335static const struct intel_gtt_driver sandybridge_gtt_driver = {
1336 .gen = 6,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001337 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001338 .cleanup = gen6_cleanup,
Daniel Vetter97ef1bd2010-09-09 17:52:20 +02001339 .write_entry = gen6_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001340 .dma_mask_size = 40,
Daniel Vetter90cb1492010-09-11 23:55:20 +02001341 .check_flags = gen6_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001342 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001343};
1344
Daniel Vetter02c026c2010-08-24 19:39:48 +02001345/* Table to describe Intel GMCH and AGP/PCIE GART drivers. At least one of
1346 * driver and gmch_driver must be non-null, and find_gmch will determine
1347 * which one should be used if a gmch_chip_id is present.
1348 */
1349static const struct intel_gtt_driver_description {
1350 unsigned int gmch_chip_id;
1351 char *name;
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001352 const struct intel_gtt_driver *gtt_driver;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001353} intel_gtt_chipsets[] = {
Daniel Vetterff268602010-11-05 15:43:35 +01001354 { PCI_DEVICE_ID_INTEL_82810_IG1, "i810",
Daniel Vetterbdd30722010-09-12 12:34:44 +02001355 &i81x_gtt_driver},
Daniel Vetterff268602010-11-05 15:43:35 +01001356 { PCI_DEVICE_ID_INTEL_82810_IG3, "i810",
Daniel Vetterbdd30722010-09-12 12:34:44 +02001357 &i81x_gtt_driver},
Daniel Vetterff268602010-11-05 15:43:35 +01001358 { PCI_DEVICE_ID_INTEL_82810E_IG, "i810",
Daniel Vetterbdd30722010-09-12 12:34:44 +02001359 &i81x_gtt_driver},
Daniel Vetterff268602010-11-05 15:43:35 +01001360 { PCI_DEVICE_ID_INTEL_82815_CGC, "i815",
Daniel Vetterbdd30722010-09-12 12:34:44 +02001361 &i81x_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001362 { PCI_DEVICE_ID_INTEL_82830_CGC, "830M",
Daniel Vetterff268602010-11-05 15:43:35 +01001363 &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001364 { PCI_DEVICE_ID_INTEL_82845G_IG, "830M",
Daniel Vetterff268602010-11-05 15:43:35 +01001365 &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001366 { PCI_DEVICE_ID_INTEL_82854_IG, "854",
Daniel Vetterff268602010-11-05 15:43:35 +01001367 &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001368 { PCI_DEVICE_ID_INTEL_82855GM_IG, "855GM",
Daniel Vetterff268602010-11-05 15:43:35 +01001369 &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001370 { PCI_DEVICE_ID_INTEL_82865_IG, "865",
Daniel Vetterff268602010-11-05 15:43:35 +01001371 &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001372 { PCI_DEVICE_ID_INTEL_E7221_IG, "E7221 (i915)",
Daniel Vetterff268602010-11-05 15:43:35 +01001373 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001374 { PCI_DEVICE_ID_INTEL_82915G_IG, "915G",
Daniel Vetterff268602010-11-05 15:43:35 +01001375 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001376 { PCI_DEVICE_ID_INTEL_82915GM_IG, "915GM",
Daniel Vetterff268602010-11-05 15:43:35 +01001377 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001378 { PCI_DEVICE_ID_INTEL_82945G_IG, "945G",
Daniel Vetterff268602010-11-05 15:43:35 +01001379 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001380 { PCI_DEVICE_ID_INTEL_82945GM_IG, "945GM",
Daniel Vetterff268602010-11-05 15:43:35 +01001381 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001382 { PCI_DEVICE_ID_INTEL_82945GME_IG, "945GME",
Daniel Vetterff268602010-11-05 15:43:35 +01001383 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001384 { PCI_DEVICE_ID_INTEL_82946GZ_IG, "946GZ",
Daniel Vetterff268602010-11-05 15:43:35 +01001385 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001386 { PCI_DEVICE_ID_INTEL_82G35_IG, "G35",
Daniel Vetterff268602010-11-05 15:43:35 +01001387 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001388 { PCI_DEVICE_ID_INTEL_82965Q_IG, "965Q",
Daniel Vetterff268602010-11-05 15:43:35 +01001389 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001390 { PCI_DEVICE_ID_INTEL_82965G_IG, "965G",
Daniel Vetterff268602010-11-05 15:43:35 +01001391 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001392 { PCI_DEVICE_ID_INTEL_82965GM_IG, "965GM",
Daniel Vetterff268602010-11-05 15:43:35 +01001393 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001394 { PCI_DEVICE_ID_INTEL_82965GME_IG, "965GME/GLE",
Daniel Vetterff268602010-11-05 15:43:35 +01001395 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001396 { PCI_DEVICE_ID_INTEL_G33_IG, "G33",
Daniel Vetterff268602010-11-05 15:43:35 +01001397 &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001398 { PCI_DEVICE_ID_INTEL_Q35_IG, "Q35",
Daniel Vetterff268602010-11-05 15:43:35 +01001399 &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001400 { PCI_DEVICE_ID_INTEL_Q33_IG, "Q33",
Daniel Vetterff268602010-11-05 15:43:35 +01001401 &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001402 { PCI_DEVICE_ID_INTEL_PINEVIEW_M_IG, "GMA3150",
Daniel Vetterff268602010-11-05 15:43:35 +01001403 &pineview_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001404 { PCI_DEVICE_ID_INTEL_PINEVIEW_IG, "GMA3150",
Daniel Vetterff268602010-11-05 15:43:35 +01001405 &pineview_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001406 { PCI_DEVICE_ID_INTEL_GM45_IG, "GM45",
Daniel Vetterff268602010-11-05 15:43:35 +01001407 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001408 { PCI_DEVICE_ID_INTEL_EAGLELAKE_IG, "Eaglelake",
Daniel Vetterff268602010-11-05 15:43:35 +01001409 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001410 { PCI_DEVICE_ID_INTEL_Q45_IG, "Q45/Q43",
Daniel Vetterff268602010-11-05 15:43:35 +01001411 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001412 { PCI_DEVICE_ID_INTEL_G45_IG, "G45/G43",
Daniel Vetterff268602010-11-05 15:43:35 +01001413 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001414 { PCI_DEVICE_ID_INTEL_B43_IG, "B43",
Daniel Vetterff268602010-11-05 15:43:35 +01001415 &g4x_gtt_driver },
Chris Wilsone9e5f8e2010-09-21 11:19:32 +01001416 { PCI_DEVICE_ID_INTEL_B43_1_IG, "B43",
Daniel Vetterff268602010-11-05 15:43:35 +01001417 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001418 { PCI_DEVICE_ID_INTEL_G41_IG, "G41",
Daniel Vetterff268602010-11-05 15:43:35 +01001419 &g4x_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001420 { PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001421 "HD Graphics", &ironlake_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001422 { PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001423 "HD Graphics", &ironlake_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001424 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT1_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001425 "Sandybridge", &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001426 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001427 "Sandybridge", &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001428 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_PLUS_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001429 "Sandybridge", &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001430 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT1_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001431 "Sandybridge", &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001432 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001433 "Sandybridge", &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001434 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_PLUS_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001435 "Sandybridge", &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001436 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001437 "Sandybridge", &sandybridge_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001438 { 0, NULL, NULL }
1439};
1440
1441static int find_gmch(u16 device)
1442{
1443 struct pci_dev *gmch_device;
1444
1445 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL, device, NULL);
1446 if (gmch_device && PCI_FUNC(gmch_device->devfn) != 0) {
1447 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL,
1448 device, gmch_device);
1449 }
1450
1451 if (!gmch_device)
1452 return 0;
1453
1454 intel_private.pcidev = gmch_device;
1455 return 1;
1456}
1457
Daniel Vettere2404e72010-09-08 17:29:51 +02001458int intel_gmch_probe(struct pci_dev *pdev,
Daniel Vetter02c026c2010-08-24 19:39:48 +02001459 struct agp_bridge_data *bridge)
1460{
1461 int i, mask;
Daniel Vetterff268602010-11-05 15:43:35 +01001462 intel_private.driver = NULL;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001463
1464 for (i = 0; intel_gtt_chipsets[i].name != NULL; i++) {
1465 if (find_gmch(intel_gtt_chipsets[i].gmch_chip_id)) {
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001466 intel_private.driver =
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001467 intel_gtt_chipsets[i].gtt_driver;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001468 break;
1469 }
1470 }
1471
Daniel Vetterff268602010-11-05 15:43:35 +01001472 if (!intel_private.driver)
Daniel Vetter02c026c2010-08-24 19:39:48 +02001473 return 0;
1474
Daniel Vetterff268602010-11-05 15:43:35 +01001475 bridge->driver = &intel_fake_agp_driver;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001476 bridge->dev_private_data = &intel_private;
1477 bridge->dev = pdev;
1478
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001479 intel_private.bridge_dev = pci_dev_get(pdev);
1480
Daniel Vetter02c026c2010-08-24 19:39:48 +02001481 dev_info(&pdev->dev, "Intel %s Chipset\n", intel_gtt_chipsets[i].name);
1482
Daniel Vetter22533b42010-09-12 16:38:55 +02001483 mask = intel_private.driver->dma_mask_size;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001484 if (pci_set_dma_mask(intel_private.pcidev, DMA_BIT_MASK(mask)))
1485 dev_err(&intel_private.pcidev->dev,
1486 "set gfx device dma mask %d-bit failed!\n", mask);
1487 else
1488 pci_set_consistent_dma_mask(intel_private.pcidev,
1489 DMA_BIT_MASK(mask));
1490
Daniel Vetter820647b2010-11-05 13:30:14 +01001491 /*if (bridge->driver == &intel_810_driver)
1492 return 1;*/
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001493
Daniel Vetter3b15a9d2010-08-29 14:18:49 +02001494 if (intel_gtt_init() != 0)
1495 return 0;
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001496
Daniel Vetter02c026c2010-08-24 19:39:48 +02001497 return 1;
1498}
Daniel Vettere2404e72010-09-08 17:29:51 +02001499EXPORT_SYMBOL(intel_gmch_probe);
Daniel Vetter02c026c2010-08-24 19:39:48 +02001500
Chris Wilsonc64f7ba2010-11-23 14:24:24 +00001501const struct intel_gtt *intel_gtt_get(void)
Daniel Vetter19966752010-09-06 20:08:44 +02001502{
1503 return &intel_private.base;
1504}
1505EXPORT_SYMBOL(intel_gtt_get);
1506
Daniel Vetter40ce6572010-11-05 18:12:18 +01001507void intel_gtt_chipset_flush(void)
1508{
1509 if (intel_private.driver->chipset_flush)
1510 intel_private.driver->chipset_flush();
1511}
1512EXPORT_SYMBOL(intel_gtt_chipset_flush);
1513
Daniel Vettere2404e72010-09-08 17:29:51 +02001514void intel_gmch_remove(struct pci_dev *pdev)
Daniel Vetter02c026c2010-08-24 19:39:48 +02001515{
1516 if (intel_private.pcidev)
1517 pci_dev_put(intel_private.pcidev);
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001518 if (intel_private.bridge_dev)
1519 pci_dev_put(intel_private.bridge_dev);
Daniel Vetter02c026c2010-08-24 19:39:48 +02001520}
Daniel Vettere2404e72010-09-08 17:29:51 +02001521EXPORT_SYMBOL(intel_gmch_remove);
1522
1523MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
1524MODULE_LICENSE("GPL and additional rights");