]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/char/agp/intel-gtt.c
drm/i915: Add probe and remove to the gtt ops
[mirror_ubuntu-artful-kernel.git] / drivers / char / agp / intel-gtt.c
CommitLineData
f51b7662
DV
1/*
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
e2404e7c
DV
18#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>
bdb8b975 24#include <linux/delay.h>
e2404e7c
DV
25#include <asm/smp.h>
26#include "agp.h"
27#include "intel-agp.h"
0ade6386 28#include <drm/intel-gtt.h>
e2404e7c 29
f51b7662
DV
30/*
31 * If we have Intel graphics, we're not going to have anything other than
32 * an Intel IOMMU. So make the correct use of the PCI DMA API contingent
d3f13810 33 * on the Intel IOMMU support (CONFIG_INTEL_IOMMU).
f51b7662
DV
34 * Only newer chipsets need to bother with this, of course.
35 */
d3f13810 36#ifdef CONFIG_INTEL_IOMMU
f51b7662 37#define USE_PCI_DMA_API 1
0e87d2b0
DV
38#else
39#define USE_PCI_DMA_API 0
f51b7662
DV
40#endif
41
1a997ff2
DV
42struct intel_gtt_driver {
43 unsigned int gen : 8;
44 unsigned int is_g33 : 1;
45 unsigned int is_pineview : 1;
46 unsigned int is_ironlake : 1;
100519e2 47 unsigned int has_pgtbl_enable : 1;
22533b49 48 unsigned int dma_mask_size : 8;
73800422
DV
49 /* Chipset specific GTT setup */
50 int (*setup)(void);
ae83dd5c
DV
51 /* This should undo anything done in ->setup() save the unmapping
52 * of the mmio register file, that's done in the generic code. */
53 void (*cleanup)(void);
351bb278
DV
54 void (*write_entry)(dma_addr_t addr, unsigned int entry, unsigned int flags);
55 /* Flags is a more or less chipset specific opaque value.
56 * For chipsets that need to support old ums (non-gem) code, this
57 * needs to be identical to the various supported agp memory types! */
5cbecafc 58 bool (*check_flags)(unsigned int flags);
1b263f24 59 void (*chipset_flush)(void);
1a997ff2
DV
60};
61
f51b7662 62static struct _intel_private {
0ade6386 63 struct intel_gtt base;
1a997ff2 64 const struct intel_gtt_driver *driver;
f51b7662 65 struct pci_dev *pcidev; /* device one */
d7cca2f7 66 struct pci_dev *bridge_dev;
f51b7662 67 u8 __iomem *registers;
f67eab66 68 phys_addr_t gtt_bus_addr;
b3eafc5a 69 u32 PGETBL_save;
f51b7662 70 u32 __iomem *gtt; /* I915G */
bee4a186 71 bool clear_fake_agp; /* on first access via agp, fill with scratch */
f51b7662 72 int num_dcache_entries;
bdb8b975 73 void __iomem *i9xx_flush_page;
820647b9 74 char *i81x_gtt_table;
f51b7662
DV
75 struct resource ifp_resource;
76 int resource_valid;
0e87d2b0 77 struct page *scratch_page;
9c61a32d 78 phys_addr_t scratch_page_dma;
14be93dd 79 int refcount;
8d2e6308
BW
80 /* Whether i915 needs to use the dmar apis or not. */
81 unsigned int needs_dmar : 1;
e5c65377 82 phys_addr_t gma_bus_addr;
f51b7662
DV
83} intel_private;
84
1a997ff2
DV
85#define INTEL_GTT_GEN intel_private.driver->gen
86#define IS_G33 intel_private.driver->is_g33
87#define IS_PINEVIEW intel_private.driver->is_pineview
88#define IS_IRONLAKE intel_private.driver->is_ironlake
100519e2 89#define HAS_PGTBL_EN intel_private.driver->has_pgtbl_enable
1a997ff2 90
9da3da66
CW
91static int intel_gtt_map_memory(struct page **pages,
92 unsigned int num_entries,
93 struct sg_table *st)
f51b7662 94{
f51b7662
DV
95 struct scatterlist *sg;
96 int i;
97
4080775b 98 DBG("try mapping %lu pages\n", (unsigned long)num_entries);
f51b7662 99
9da3da66 100 if (sg_alloc_table(st, num_entries, GFP_KERNEL))
831cd445 101 goto err;
f51b7662 102
9da3da66 103 for_each_sg(st->sgl, sg, num_entries, i)
4080775b 104 sg_set_page(sg, pages[i], PAGE_SIZE, 0);
f51b7662 105
9da3da66
CW
106 if (!pci_map_sg(intel_private.pcidev,
107 st->sgl, st->nents, PCI_DMA_BIDIRECTIONAL))
831cd445
CW
108 goto err;
109
f51b7662 110 return 0;
831cd445
CW
111
112err:
9da3da66 113 sg_free_table(st);
831cd445 114 return -ENOMEM;
f51b7662
DV
115}
116
9da3da66 117static void intel_gtt_unmap_memory(struct scatterlist *sg_list, int num_sg)
f51b7662 118{
4080775b 119 struct sg_table st;
f51b7662
DV
120 DBG("try unmapping %lu pages\n", (unsigned long)mem->page_count);
121
4080775b
DV
122 pci_unmap_sg(intel_private.pcidev, sg_list,
123 num_sg, PCI_DMA_BIDIRECTIONAL);
124
125 st.sgl = sg_list;
126 st.orig_nents = st.nents = num_sg;
127
128 sg_free_table(&st);
f51b7662
DV
129}
130
ffdd7510 131static void intel_fake_agp_enable(struct agp_bridge_data *bridge, u32 mode)
f51b7662
DV
132{
133 return;
134}
135
136/* Exists to support ARGB cursors */
137static struct page *i8xx_alloc_pages(void)
138{
139 struct page *page;
140
141 page = alloc_pages(GFP_KERNEL | GFP_DMA32, 2);
142 if (page == NULL)
143 return NULL;
144
145 if (set_pages_uc(page, 4) < 0) {
146 set_pages_wb(page, 4);
147 __free_pages(page, 2);
148 return NULL;
149 }
150 get_page(page);
151 atomic_inc(&agp_bridge->current_memory_agp);
152 return page;
153}
154
155static void i8xx_destroy_pages(struct page *page)
156{
157 if (page == NULL)
158 return;
159
160 set_pages_wb(page, 4);
161 put_page(page);
162 __free_pages(page, 2);
163 atomic_dec(&agp_bridge->current_memory_agp);
164}
165
820647b9
DV
166#define I810_GTT_ORDER 4
167static int i810_setup(void)
168{
169 u32 reg_addr;
170 char *gtt_table;
171
172 /* i81x does not preallocate the gtt. It's always 64kb in size. */
173 gtt_table = alloc_gatt_pages(I810_GTT_ORDER);
174 if (gtt_table == NULL)
175 return -ENOMEM;
176 intel_private.i81x_gtt_table = gtt_table;
177
178 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &reg_addr);
179 reg_addr &= 0xfff80000;
180
181 intel_private.registers = ioremap(reg_addr, KB(64));
182 if (!intel_private.registers)
183 return -ENOMEM;
184
185 writel(virt_to_phys(gtt_table) | I810_PGETBL_ENABLED,
186 intel_private.registers+I810_PGETBL_CTL);
187
188 intel_private.gtt_bus_addr = reg_addr + I810_PTE_BASE;
189
190 if ((readl(intel_private.registers+I810_DRAM_CTL)
191 & I810_DRAM_ROW_0) == I810_DRAM_ROW_0_SDRAM) {
192 dev_info(&intel_private.pcidev->dev,
193 "detected 4MB dedicated video ram\n");
194 intel_private.num_dcache_entries = 1024;
195 }
196
197 return 0;
198}
199
200static void i810_cleanup(void)
201{
202 writel(0, intel_private.registers+I810_PGETBL_CTL);
203 free_gatt_pages(intel_private.i81x_gtt_table, I810_GTT_ORDER);
204}
205
ff26860f
DV
206static int i810_insert_dcache_entries(struct agp_memory *mem, off_t pg_start,
207 int type)
f51b7662 208{
625dd9d3 209 int i;
f51b7662 210
ff26860f
DV
211 if ((pg_start + mem->page_count)
212 > intel_private.num_dcache_entries)
213 return -EINVAL;
625dd9d3 214
ff26860f
DV
215 if (!mem->is_flushed)
216 global_cache_flush();
f51b7662 217
ff26860f
DV
218 for (i = pg_start; i < (pg_start + mem->page_count); i++) {
219 dma_addr_t addr = i << PAGE_SHIFT;
220 intel_private.driver->write_entry(addr,
221 i, type);
f51b7662 222 }
ff26860f 223 readl(intel_private.gtt+i-1);
f51b7662 224
ff26860f 225 return 0;
f51b7662
DV
226}
227
228/*
229 * The i810/i830 requires a physical address to program its mouse
230 * pointer into hardware.
231 * However the Xserver still writes to it through the agp aperture.
232 */
233static struct agp_memory *alloc_agpphysmem_i8xx(size_t pg_count, int type)
234{
235 struct agp_memory *new;
236 struct page *page;
237
238 switch (pg_count) {
239 case 1: page = agp_bridge->driver->agp_alloc_page(agp_bridge);
240 break;
241 case 4:
242 /* kludge to get 4 physical pages for ARGB cursor */
243 page = i8xx_alloc_pages();
244 break;
245 default:
246 return NULL;
247 }
248
249 if (page == NULL)
250 return NULL;
251
252 new = agp_create_memory(pg_count);
253 if (new == NULL)
254 return NULL;
255
256 new->pages[0] = page;
257 if (pg_count == 4) {
258 /* kludge to get 4 physical pages for ARGB cursor */
259 new->pages[1] = new->pages[0] + 1;
260 new->pages[2] = new->pages[1] + 1;
261 new->pages[3] = new->pages[2] + 1;
262 }
263 new->page_count = pg_count;
264 new->num_scratch_pages = pg_count;
265 new->type = AGP_PHYS_MEMORY;
266 new->physical = page_to_phys(new->pages[0]);
267 return new;
268}
269
f51b7662
DV
270static void intel_i810_free_by_type(struct agp_memory *curr)
271{
272 agp_free_key(curr->key);
273 if (curr->type == AGP_PHYS_MEMORY) {
274 if (curr->page_count == 4)
275 i8xx_destroy_pages(curr->pages[0]);
276 else {
277 agp_bridge->driver->agp_destroy_page(curr->pages[0],
278 AGP_PAGE_DESTROY_UNMAP);
279 agp_bridge->driver->agp_destroy_page(curr->pages[0],
280 AGP_PAGE_DESTROY_FREE);
281 }
282 agp_free_page_array(curr);
283 }
284 kfree(curr);
285}
286
0e87d2b0
DV
287static int intel_gtt_setup_scratch_page(void)
288{
289 struct page *page;
290 dma_addr_t dma_addr;
291
292 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
293 if (page == NULL)
294 return -ENOMEM;
295 get_page(page);
296 set_pages_uc(page, 1);
297
8d2e6308 298 if (intel_private.needs_dmar) {
0e87d2b0
DV
299 dma_addr = pci_map_page(intel_private.pcidev, page, 0,
300 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
301 if (pci_dma_mapping_error(intel_private.pcidev, dma_addr))
302 return -EINVAL;
303
9c61a32d 304 intel_private.scratch_page_dma = dma_addr;
0e87d2b0 305 } else
9c61a32d 306 intel_private.scratch_page_dma = page_to_phys(page);
0e87d2b0
DV
307
308 intel_private.scratch_page = page;
309
310 return 0;
311}
312
625dd9d3
DV
313static void i810_write_entry(dma_addr_t addr, unsigned int entry,
314 unsigned int flags)
315{
316 u32 pte_flags = I810_PTE_VALID;
317
318 switch (flags) {
319 case AGP_DCACHE_MEMORY:
320 pte_flags |= I810_PTE_LOCAL;
321 break;
322 case AGP_USER_CACHED_MEMORY:
323 pte_flags |= I830_PTE_SYSTEM_CACHED;
324 break;
325 }
326
327 writel(addr | pte_flags, intel_private.gtt + entry);
328}
329
7bdc9ab0 330static const struct aper_size_info_fixed intel_fake_agp_sizes[] = {
820647b9
DV
331 {32, 8192, 3},
332 {64, 16384, 4},
f51b7662 333 {128, 32768, 5},
f51b7662
DV
334 {256, 65536, 6},
335 {512, 131072, 7},
336};
337
c64f7ba5 338static unsigned int intel_gtt_stolen_size(void)
f51b7662
DV
339{
340 u16 gmch_ctrl;
f51b7662
DV
341 u8 rdct;
342 int local = 0;
343 static const int ddt[4] = { 0, 16, 32, 64 };
d8d9abcd 344 unsigned int stolen_size = 0;
f51b7662 345
820647b9
DV
346 if (INTEL_GTT_GEN == 1)
347 return 0; /* no stolen mem on i81x */
348
d7cca2f7
DV
349 pci_read_config_word(intel_private.bridge_dev,
350 I830_GMCH_CTRL, &gmch_ctrl);
f51b7662 351
d7cca2f7
DV
352 if (intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82830_HB ||
353 intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82845G_HB) {
f51b7662
DV
354 switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
355 case I830_GMCH_GMS_STOLEN_512:
d8d9abcd 356 stolen_size = KB(512);
f51b7662
DV
357 break;
358 case I830_GMCH_GMS_STOLEN_1024:
d8d9abcd 359 stolen_size = MB(1);
f51b7662
DV
360 break;
361 case I830_GMCH_GMS_STOLEN_8192:
d8d9abcd 362 stolen_size = MB(8);
f51b7662
DV
363 break;
364 case I830_GMCH_GMS_LOCAL:
365 rdct = readb(intel_private.registers+I830_RDRAM_CHANNEL_TYPE);
d8d9abcd 366 stolen_size = (I830_RDRAM_ND(rdct) + 1) *
f51b7662
DV
367 MB(ddt[I830_RDRAM_DDT(rdct)]);
368 local = 1;
369 break;
370 default:
d8d9abcd 371 stolen_size = 0;
f51b7662
DV
372 break;
373 }
f51b7662
DV
374 } else {
375 switch (gmch_ctrl & I855_GMCH_GMS_MASK) {
376 case I855_GMCH_GMS_STOLEN_1M:
d8d9abcd 377 stolen_size = MB(1);
f51b7662
DV
378 break;
379 case I855_GMCH_GMS_STOLEN_4M:
d8d9abcd 380 stolen_size = MB(4);
f51b7662
DV
381 break;
382 case I855_GMCH_GMS_STOLEN_8M:
d8d9abcd 383 stolen_size = MB(8);
f51b7662
DV
384 break;
385 case I855_GMCH_GMS_STOLEN_16M:
d8d9abcd 386 stolen_size = MB(16);
f51b7662
DV
387 break;
388 case I855_GMCH_GMS_STOLEN_32M:
d8d9abcd 389 stolen_size = MB(32);
f51b7662
DV
390 break;
391 case I915_GMCH_GMS_STOLEN_48M:
77ad498e 392 stolen_size = MB(48);
f51b7662
DV
393 break;
394 case I915_GMCH_GMS_STOLEN_64M:
77ad498e 395 stolen_size = MB(64);
f51b7662
DV
396 break;
397 case G33_GMCH_GMS_STOLEN_128M:
77ad498e 398 stolen_size = MB(128);
f51b7662
DV
399 break;
400 case G33_GMCH_GMS_STOLEN_256M:
77ad498e 401 stolen_size = MB(256);
f51b7662
DV
402 break;
403 case INTEL_GMCH_GMS_STOLEN_96M:
77ad498e 404 stolen_size = MB(96);
f51b7662
DV
405 break;
406 case INTEL_GMCH_GMS_STOLEN_160M:
77ad498e 407 stolen_size = MB(160);
f51b7662
DV
408 break;
409 case INTEL_GMCH_GMS_STOLEN_224M:
77ad498e 410 stolen_size = MB(224);
f51b7662
DV
411 break;
412 case INTEL_GMCH_GMS_STOLEN_352M:
77ad498e 413 stolen_size = MB(352);
f51b7662
DV
414 break;
415 default:
d8d9abcd 416 stolen_size = 0;
f51b7662
DV
417 break;
418 }
419 }
1784a5fb 420
1b6064d7 421 if (stolen_size > 0) {
d7cca2f7 422 dev_info(&intel_private.bridge_dev->dev, "detected %dK %s memory\n",
d8d9abcd 423 stolen_size / KB(1), local ? "local" : "stolen");
f51b7662 424 } else {
d7cca2f7 425 dev_info(&intel_private.bridge_dev->dev,
f51b7662 426 "no pre-allocated video memory detected\n");
d8d9abcd 427 stolen_size = 0;
f51b7662
DV
428 }
429
c64f7ba5 430 return stolen_size;
f51b7662
DV
431}
432
20172842
DV
433static void i965_adjust_pgetbl_size(unsigned int size_flag)
434{
435 u32 pgetbl_ctl, pgetbl_ctl2;
436
437 /* ensure that ppgtt is disabled */
438 pgetbl_ctl2 = readl(intel_private.registers+I965_PGETBL_CTL2);
439 pgetbl_ctl2 &= ~I810_PGETBL_ENABLED;
440 writel(pgetbl_ctl2, intel_private.registers+I965_PGETBL_CTL2);
441
442 /* write the new ggtt size */
443 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
444 pgetbl_ctl &= ~I965_PGETBL_SIZE_MASK;
445 pgetbl_ctl |= size_flag;
446 writel(pgetbl_ctl, intel_private.registers+I810_PGETBL_CTL);
447}
448
449static unsigned int i965_gtt_total_entries(void)
fbe40783
DV
450{
451 int size;
20172842
DV
452 u32 pgetbl_ctl;
453 u16 gmch_ctl;
fbe40783 454
20172842
DV
455 pci_read_config_word(intel_private.bridge_dev,
456 I830_GMCH_CTRL, &gmch_ctl);
fbe40783 457
20172842
DV
458 if (INTEL_GTT_GEN == 5) {
459 switch (gmch_ctl & G4x_GMCH_SIZE_MASK) {
460 case G4x_GMCH_SIZE_1M:
461 case G4x_GMCH_SIZE_VT_1M:
462 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1MB);
fbe40783 463 break;
20172842
DV
464 case G4x_GMCH_SIZE_VT_1_5M:
465 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1_5MB);
fbe40783 466 break;
20172842
DV
467 case G4x_GMCH_SIZE_2M:
468 case G4x_GMCH_SIZE_VT_2M:
469 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_2MB);
fbe40783 470 break;
fbe40783 471 }
20172842 472 }
e5e408fc 473
20172842
DV
474 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
475
476 switch (pgetbl_ctl & I965_PGETBL_SIZE_MASK) {
477 case I965_PGETBL_SIZE_128KB:
478 size = KB(128);
479 break;
480 case I965_PGETBL_SIZE_256KB:
481 size = KB(256);
482 break;
483 case I965_PGETBL_SIZE_512KB:
484 size = KB(512);
485 break;
486 /* GTT pagetable sizes bigger than 512KB are not possible on G33! */
487 case I965_PGETBL_SIZE_1MB:
488 size = KB(1024);
489 break;
490 case I965_PGETBL_SIZE_2MB:
491 size = KB(2048);
492 break;
493 case I965_PGETBL_SIZE_1_5MB:
494 size = KB(1024 + 512);
495 break;
496 default:
497 dev_info(&intel_private.pcidev->dev,
498 "unknown page table size, assuming 512KB\n");
499 size = KB(512);
500 }
501
502 return size/4;
503}
504
505static unsigned int intel_gtt_total_entries(void)
506{
20172842
DV
507 if (IS_G33 || INTEL_GTT_GEN == 4 || INTEL_GTT_GEN == 5)
508 return i965_gtt_total_entries();
009946f8 509 else {
fbe40783
DV
510 /* On previous hardware, the GTT size was just what was
511 * required to map the aperture.
512 */
e5e408fc 513 return intel_private.base.gtt_mappable_entries;
fbe40783 514 }
fbe40783 515}
fbe40783 516
1784a5fb
DV
517static unsigned int intel_gtt_mappable_entries(void)
518{
519 unsigned int aperture_size;
1784a5fb 520
820647b9
DV
521 if (INTEL_GTT_GEN == 1) {
522 u32 smram_miscc;
523
524 pci_read_config_dword(intel_private.bridge_dev,
525 I810_SMRAM_MISCC, &smram_miscc);
526
527 if ((smram_miscc & I810_GFX_MEM_WIN_SIZE)
528 == I810_GFX_MEM_WIN_32M)
529 aperture_size = MB(32);
530 else
531 aperture_size = MB(64);
532 } else if (INTEL_GTT_GEN == 2) {
b1c5b0f8 533 u16 gmch_ctrl;
1784a5fb 534
b1c5b0f8
CW
535 pci_read_config_word(intel_private.bridge_dev,
536 I830_GMCH_CTRL, &gmch_ctrl);
1784a5fb 537
1784a5fb 538 if ((gmch_ctrl & I830_GMCH_MEM_MASK) == I830_GMCH_MEM_64M)
b1c5b0f8 539 aperture_size = MB(64);
1784a5fb 540 else
b1c5b0f8 541 aperture_size = MB(128);
239918f7 542 } else {
1784a5fb
DV
543 /* 9xx supports large sizes, just look at the length */
544 aperture_size = pci_resource_len(intel_private.pcidev, 2);
1784a5fb
DV
545 }
546
547 return aperture_size >> PAGE_SHIFT;
548}
549
0e87d2b0
DV
550static void intel_gtt_teardown_scratch_page(void)
551{
552 set_pages_wb(intel_private.scratch_page, 1);
9c61a32d 553 pci_unmap_page(intel_private.pcidev, intel_private.scratch_page_dma,
0e87d2b0
DV
554 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
555 put_page(intel_private.scratch_page);
556 __free_page(intel_private.scratch_page);
557}
558
559static void intel_gtt_cleanup(void)
560{
ae83dd5c
DV
561 intel_private.driver->cleanup();
562
0e87d2b0
DV
563 iounmap(intel_private.gtt);
564 iounmap(intel_private.registers);
625dd9d3 565
0e87d2b0
DV
566 intel_gtt_teardown_scratch_page();
567}
568
1784a5fb
DV
569static int intel_gtt_init(void)
570{
32e3cd6e 571 u32 gma_addr;
f67eab66 572 u32 gtt_map_size;
3b15a9d7
DV
573 int ret;
574
3b15a9d7
DV
575 ret = intel_private.driver->setup();
576 if (ret != 0)
577 return ret;
f67eab66
DV
578
579 intel_private.base.gtt_mappable_entries = intel_gtt_mappable_entries();
580 intel_private.base.gtt_total_entries = intel_gtt_total_entries();
581
b3eafc5a
DV
582 /* save the PGETBL reg for resume */
583 intel_private.PGETBL_save =
584 readl(intel_private.registers+I810_PGETBL_CTL)
585 & ~I810_PGETBL_ENABLED;
100519e2
CW
586 /* we only ever restore the register when enabling the PGTBL... */
587 if (HAS_PGTBL_EN)
588 intel_private.PGETBL_save |= I810_PGETBL_ENABLED;
b3eafc5a 589
0af9e92e
DV
590 dev_info(&intel_private.bridge_dev->dev,
591 "detected gtt size: %dK total, %dK mappable\n",
592 intel_private.base.gtt_total_entries * 4,
593 intel_private.base.gtt_mappable_entries * 4);
594
f67eab66
DV
595 gtt_map_size = intel_private.base.gtt_total_entries * 4;
596
edef7e68 597 intel_private.gtt = NULL;
9169d3a8 598 if (INTEL_GTT_GEN < 6 && INTEL_GTT_GEN > 2)
edef7e68
CW
599 intel_private.gtt = ioremap_wc(intel_private.gtt_bus_addr,
600 gtt_map_size);
601 if (intel_private.gtt == NULL)
602 intel_private.gtt = ioremap(intel_private.gtt_bus_addr,
603 gtt_map_size);
604 if (intel_private.gtt == NULL) {
ae83dd5c 605 intel_private.driver->cleanup();
f67eab66
DV
606 iounmap(intel_private.registers);
607 return -ENOMEM;
608 }
609
610 global_cache_flush(); /* FIXME: ? */
611
c64f7ba5 612 intel_private.base.stolen_size = intel_gtt_stolen_size();
1784a5fb 613
8d2e6308 614 intel_private.needs_dmar = USE_PCI_DMA_API && INTEL_GTT_GEN > 2;
a46f3108 615
0e87d2b0
DV
616 ret = intel_gtt_setup_scratch_page();
617 if (ret != 0) {
618 intel_gtt_cleanup();
619 return ret;
620 }
621
32e3cd6e
DV
622 if (INTEL_GTT_GEN <= 2)
623 pci_read_config_dword(intel_private.pcidev, I810_GMADDR,
624 &gma_addr);
625 else
626 pci_read_config_dword(intel_private.pcidev, I915_GMADDR,
627 &gma_addr);
628
e5c65377 629 intel_private.gma_bus_addr = (gma_addr & PCI_BASE_ADDRESS_MEM_MASK);
32e3cd6e 630
1784a5fb
DV
631 return 0;
632}
633
3e921f98
DV
634static int intel_fake_agp_fetch_size(void)
635{
9e76e7b8 636 int num_sizes = ARRAY_SIZE(intel_fake_agp_sizes);
3e921f98
DV
637 unsigned int aper_size;
638 int i;
3e921f98
DV
639
640 aper_size = (intel_private.base.gtt_mappable_entries << PAGE_SHIFT)
641 / MB(1);
642
643 for (i = 0; i < num_sizes; i++) {
ffdd7510 644 if (aper_size == intel_fake_agp_sizes[i].size) {
9e76e7b8
CW
645 agp_bridge->current_size =
646 (void *) (intel_fake_agp_sizes + i);
3e921f98
DV
647 return aper_size;
648 }
649 }
650
651 return 0;
652}
653
ae83dd5c 654static void i830_cleanup(void)
f51b7662 655{
f51b7662
DV
656}
657
658/* The chipset_flush interface needs to get data that has already been
659 * flushed out of the CPU all the way out to main memory, because the GPU
660 * doesn't snoop those buffers.
661 *
662 * The 8xx series doesn't have the same lovely interface for flushing the
663 * chipset write buffers that the later chips do. According to the 865
664 * specs, it's 64 octwords, or 1KB. So, to get those previous things in
665 * that buffer out, we just fill 1KB and clflush it out, on the assumption
666 * that it'll push whatever was in there out. It appears to work.
667 */
1b263f24 668static void i830_chipset_flush(void)
f51b7662 669{
bdb8b975
CW
670 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
671
672 /* Forcibly evict everything from the CPU write buffers.
673 * clflush appears to be insufficient.
674 */
675 wbinvd_on_all_cpus();
676
677 /* Now we've only seen documents for this magic bit on 855GM,
678 * we hope it exists for the other gen2 chipsets...
679 *
680 * Also works as advertised on my 845G.
681 */
682 writel(readl(intel_private.registers+I830_HIC) | (1<<31),
683 intel_private.registers+I830_HIC);
f51b7662 684
bdb8b975
CW
685 while (readl(intel_private.registers+I830_HIC) & (1<<31)) {
686 if (time_after(jiffies, timeout))
687 break;
f51b7662 688
bdb8b975
CW
689 udelay(50);
690 }
f51b7662
DV
691}
692
351bb278
DV
693static void i830_write_entry(dma_addr_t addr, unsigned int entry,
694 unsigned int flags)
695{
696 u32 pte_flags = I810_PTE_VALID;
625dd9d3 697
b47cf66f 698 if (flags == AGP_USER_CACHED_MEMORY)
351bb278 699 pte_flags |= I830_PTE_SYSTEM_CACHED;
351bb278
DV
700
701 writel(addr | pte_flags, intel_private.gtt + entry);
702}
703
8ecd1a66 704bool intel_enable_gtt(void)
f51b7662 705{
e380f60b 706 u8 __iomem *reg;
f51b7662 707
100519e2
CW
708 if (INTEL_GTT_GEN == 2) {
709 u16 gmch_ctrl;
73800422 710
100519e2
CW
711 pci_read_config_word(intel_private.bridge_dev,
712 I830_GMCH_CTRL, &gmch_ctrl);
713 gmch_ctrl |= I830_GMCH_ENABLED;
714 pci_write_config_word(intel_private.bridge_dev,
715 I830_GMCH_CTRL, gmch_ctrl);
716
717 pci_read_config_word(intel_private.bridge_dev,
718 I830_GMCH_CTRL, &gmch_ctrl);
719 if ((gmch_ctrl & I830_GMCH_ENABLED) == 0) {
720 dev_err(&intel_private.pcidev->dev,
721 "failed to enable the GTT: GMCH_CTRL=%x\n",
722 gmch_ctrl);
723 return false;
724 }
e380f60b
CW
725 }
726
c97689d8
CW
727 /* On the resume path we may be adjusting the PGTBL value, so
728 * be paranoid and flush all chipset write buffers...
729 */
730 if (INTEL_GTT_GEN >= 3)
731 writel(0, intel_private.registers+GFX_FLSH_CNTL);
732
e380f60b 733 reg = intel_private.registers+I810_PGETBL_CTL;
100519e2
CW
734 writel(intel_private.PGETBL_save, reg);
735 if (HAS_PGTBL_EN && (readl(reg) & I810_PGETBL_ENABLED) == 0) {
e380f60b 736 dev_err(&intel_private.pcidev->dev,
100519e2 737 "failed to enable the GTT: PGETBL=%x [expected %x]\n",
e380f60b
CW
738 readl(reg), intel_private.PGETBL_save);
739 return false;
740 }
741
c97689d8
CW
742 if (INTEL_GTT_GEN >= 3)
743 writel(0, intel_private.registers+GFX_FLSH_CNTL);
744
e380f60b 745 return true;
73800422 746}
8ecd1a66 747EXPORT_SYMBOL(intel_enable_gtt);
73800422
DV
748
749static int i830_setup(void)
750{
751 u32 reg_addr;
752
753 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &reg_addr);
754 reg_addr &= 0xfff80000;
755
756 intel_private.registers = ioremap(reg_addr, KB(64));
f51b7662
DV
757 if (!intel_private.registers)
758 return -ENOMEM;
759
73800422
DV
760 intel_private.gtt_bus_addr = reg_addr + I810_PTE_BASE;
761
73800422
DV
762 return 0;
763}
764
3b15a9d7 765static int intel_fake_agp_create_gatt_table(struct agp_bridge_data *bridge)
73800422 766{
73800422 767 agp_bridge->gatt_table_real = NULL;
f51b7662 768 agp_bridge->gatt_table = NULL;
73800422 769 agp_bridge->gatt_bus_addr = 0;
f51b7662
DV
770
771 return 0;
772}
773
ffdd7510 774static int intel_fake_agp_free_gatt_table(struct agp_bridge_data *bridge)
f51b7662
DV
775{
776 return 0;
777}
778
351bb278 779static int intel_fake_agp_configure(void)
f51b7662 780{
e380f60b
CW
781 if (!intel_enable_gtt())
782 return -EIO;
f51b7662 783
bee4a186 784 intel_private.clear_fake_agp = true;
e5c65377 785 agp_bridge->gart_bus_addr = intel_private.gma_bus_addr;
f51b7662 786
f51b7662
DV
787 return 0;
788}
789
5cbecafc 790static bool i830_check_flags(unsigned int flags)
f51b7662 791{
5cbecafc
DV
792 switch (flags) {
793 case 0:
794 case AGP_PHYS_MEMORY:
795 case AGP_USER_CACHED_MEMORY:
796 case AGP_USER_MEMORY:
797 return true;
798 }
799
800 return false;
801}
802
9da3da66 803void intel_gtt_insert_sg_entries(struct sg_table *st,
4080775b
DV
804 unsigned int pg_start,
805 unsigned int flags)
fefaa70f
DV
806{
807 struct scatterlist *sg;
808 unsigned int len, m;
809 int i, j;
810
811 j = pg_start;
812
813 /* sg may merge pages, but we have to separate
814 * per-page addr for GTT */
9da3da66 815 for_each_sg(st->sgl, sg, st->nents, i) {
fefaa70f
DV
816 len = sg_dma_len(sg) >> PAGE_SHIFT;
817 for (m = 0; m < len; m++) {
818 dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
9da3da66 819 intel_private.driver->write_entry(addr, j, flags);
fefaa70f
DV
820 j++;
821 }
822 }
823 readl(intel_private.gtt+j-1);
824}
4080775b
DV
825EXPORT_SYMBOL(intel_gtt_insert_sg_entries);
826
9da3da66
CW
827static void intel_gtt_insert_pages(unsigned int first_entry,
828 unsigned int num_entries,
829 struct page **pages,
830 unsigned int flags)
4080775b
DV
831{
832 int i, j;
833
834 for (i = 0, j = first_entry; i < num_entries; i++, j++) {
835 dma_addr_t addr = page_to_phys(pages[i]);
836 intel_private.driver->write_entry(addr,
837 j, flags);
838 }
839 readl(intel_private.gtt+j-1);
840}
fefaa70f 841
5cbecafc
DV
842static int intel_fake_agp_insert_entries(struct agp_memory *mem,
843 off_t pg_start, int type)
844{
f51b7662 845 int ret = -EINVAL;
f51b7662 846
bee4a186
CW
847 if (intel_private.clear_fake_agp) {
848 int start = intel_private.base.stolen_size / PAGE_SIZE;
849 int end = intel_private.base.gtt_mappable_entries;
850 intel_gtt_clear_range(start, end - start);
851 intel_private.clear_fake_agp = false;
852 }
853
ff26860f
DV
854 if (INTEL_GTT_GEN == 1 && type == AGP_DCACHE_MEMORY)
855 return i810_insert_dcache_entries(mem, pg_start, type);
856
f51b7662
DV
857 if (mem->page_count == 0)
858 goto out;
859
c64f7ba5 860 if (pg_start + mem->page_count > intel_private.base.gtt_total_entries)
f51b7662
DV
861 goto out_err;
862
f51b7662
DV
863 if (type != mem->type)
864 goto out_err;
865
5cbecafc 866 if (!intel_private.driver->check_flags(type))
f51b7662
DV
867 goto out_err;
868
869 if (!mem->is_flushed)
870 global_cache_flush();
871
8d2e6308 872 if (intel_private.needs_dmar) {
9da3da66
CW
873 struct sg_table st;
874
875 ret = intel_gtt_map_memory(mem->pages, mem->page_count, &st);
fefaa70f
DV
876 if (ret != 0)
877 return ret;
878
9da3da66
CW
879 intel_gtt_insert_sg_entries(&st, pg_start, type);
880 mem->sg_list = st.sgl;
881 mem->num_sg = st.nents;
4080775b
DV
882 } else
883 intel_gtt_insert_pages(pg_start, mem->page_count, mem->pages,
884 type);
f51b7662
DV
885
886out:
887 ret = 0;
888out_err:
889 mem->is_flushed = true;
890 return ret;
891}
892
4080775b
DV
893void intel_gtt_clear_range(unsigned int first_entry, unsigned int num_entries)
894{
895 unsigned int i;
896
897 for (i = first_entry; i < (first_entry + num_entries); i++) {
9c61a32d 898 intel_private.driver->write_entry(intel_private.scratch_page_dma,
4080775b
DV
899 i, 0);
900 }
901 readl(intel_private.gtt+i-1);
902}
903EXPORT_SYMBOL(intel_gtt_clear_range);
904
5cbecafc
DV
905static int intel_fake_agp_remove_entries(struct agp_memory *mem,
906 off_t pg_start, int type)
f51b7662 907{
f51b7662
DV
908 if (mem->page_count == 0)
909 return 0;
910
d15eda5c
DA
911 intel_gtt_clear_range(pg_start, mem->page_count);
912
8d2e6308 913 if (intel_private.needs_dmar) {
4080775b
DV
914 intel_gtt_unmap_memory(mem->sg_list, mem->num_sg);
915 mem->sg_list = NULL;
916 mem->num_sg = 0;
f51b7662 917 }
4080775b 918
f51b7662
DV
919 return 0;
920}
921
ffdd7510
DV
922static struct agp_memory *intel_fake_agp_alloc_by_type(size_t pg_count,
923 int type)
f51b7662 924{
625dd9d3
DV
925 struct agp_memory *new;
926
927 if (type == AGP_DCACHE_MEMORY && INTEL_GTT_GEN == 1) {
928 if (pg_count != intel_private.num_dcache_entries)
929 return NULL;
930
931 new = agp_create_memory(1);
932 if (new == NULL)
933 return NULL;
934
935 new->type = AGP_DCACHE_MEMORY;
936 new->page_count = pg_count;
937 new->num_scratch_pages = 0;
938 agp_free_page_array(new);
939 return new;
940 }
f51b7662
DV
941 if (type == AGP_PHYS_MEMORY)
942 return alloc_agpphysmem_i8xx(pg_count, type);
943 /* always return NULL for other allocation types for now */
944 return NULL;
945}
946
947static int intel_alloc_chipset_flush_resource(void)
948{
949 int ret;
d7cca2f7 950 ret = pci_bus_alloc_resource(intel_private.bridge_dev->bus, &intel_private.ifp_resource, PAGE_SIZE,
f51b7662 951 PAGE_SIZE, PCIBIOS_MIN_MEM, 0,
d7cca2f7 952 pcibios_align_resource, intel_private.bridge_dev);
f51b7662
DV
953
954 return ret;
955}
956
957static void intel_i915_setup_chipset_flush(void)
958{
959 int ret;
960 u32 temp;
961
d7cca2f7 962 pci_read_config_dword(intel_private.bridge_dev, I915_IFPADDR, &temp);
f51b7662
DV
963 if (!(temp & 0x1)) {
964 intel_alloc_chipset_flush_resource();
965 intel_private.resource_valid = 1;
d7cca2f7 966 pci_write_config_dword(intel_private.bridge_dev, I915_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
f51b7662
DV
967 } else {
968 temp &= ~1;
969
970 intel_private.resource_valid = 1;
971 intel_private.ifp_resource.start = temp;
972 intel_private.ifp_resource.end = temp + PAGE_SIZE;
973 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
974 /* some BIOSes reserve this area in a pnp some don't */
975 if (ret)
976 intel_private.resource_valid = 0;
977 }
978}
979
980static void intel_i965_g33_setup_chipset_flush(void)
981{
982 u32 temp_hi, temp_lo;
983 int ret;
984
d7cca2f7
DV
985 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4, &temp_hi);
986 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR, &temp_lo);
f51b7662
DV
987
988 if (!(temp_lo & 0x1)) {
989
990 intel_alloc_chipset_flush_resource();
991
992 intel_private.resource_valid = 1;
d7cca2f7 993 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4,
f51b7662 994 upper_32_bits(intel_private.ifp_resource.start));
d7cca2f7 995 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
f51b7662
DV
996 } else {
997 u64 l64;
998
999 temp_lo &= ~0x1;
1000 l64 = ((u64)temp_hi << 32) | temp_lo;
1001
1002 intel_private.resource_valid = 1;
1003 intel_private.ifp_resource.start = l64;
1004 intel_private.ifp_resource.end = l64 + PAGE_SIZE;
1005 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
1006 /* some BIOSes reserve this area in a pnp some don't */
1007 if (ret)
1008 intel_private.resource_valid = 0;
1009 }
1010}
1011
1012static void intel_i9xx_setup_flush(void)
1013{
1014 /* return if already configured */
1015 if (intel_private.ifp_resource.start)
1016 return;
1017
1a997ff2 1018 if (INTEL_GTT_GEN == 6)
f51b7662
DV
1019 return;
1020
1021 /* setup a resource for this object */
1022 intel_private.ifp_resource.name = "Intel Flush Page";
1023 intel_private.ifp_resource.flags = IORESOURCE_MEM;
1024
1025 /* Setup chipset flush for 915 */
1a997ff2 1026 if (IS_G33 || INTEL_GTT_GEN >= 4) {
f51b7662
DV
1027 intel_i965_g33_setup_chipset_flush();
1028 } else {
1029 intel_i915_setup_chipset_flush();
1030 }
1031
df51e7aa 1032 if (intel_private.ifp_resource.start)
f51b7662 1033 intel_private.i9xx_flush_page = ioremap_nocache(intel_private.ifp_resource.start, PAGE_SIZE);
df51e7aa
CW
1034 if (!intel_private.i9xx_flush_page)
1035 dev_err(&intel_private.pcidev->dev,
1036 "can't ioremap flush page - no chipset flushing\n");
f51b7662
DV
1037}
1038
ae83dd5c
DV
1039static void i9xx_cleanup(void)
1040{
1041 if (intel_private.i9xx_flush_page)
1042 iounmap(intel_private.i9xx_flush_page);
1043 if (intel_private.resource_valid)
1044 release_resource(&intel_private.ifp_resource);
1045 intel_private.ifp_resource.start = 0;
1046 intel_private.resource_valid = 0;
1047}
1048
1b263f24 1049static void i9xx_chipset_flush(void)
f51b7662
DV
1050{
1051 if (intel_private.i9xx_flush_page)
1052 writel(1, intel_private.i9xx_flush_page);
1053}
1054
71f45660
CW
1055static void i965_write_entry(dma_addr_t addr,
1056 unsigned int entry,
a6963596
DV
1057 unsigned int flags)
1058{
71f45660
CW
1059 u32 pte_flags;
1060
1061 pte_flags = I810_PTE_VALID;
1062 if (flags == AGP_USER_CACHED_MEMORY)
1063 pte_flags |= I830_PTE_SYSTEM_CACHED;
1064
a6963596
DV
1065 /* Shift high bits down */
1066 addr |= (addr >> 28) & 0xf0;
71f45660 1067 writel(addr | pte_flags, intel_private.gtt + entry);
a6963596
DV
1068}
1069
5c042287 1070
2d2430cf 1071static int i9xx_setup(void)
f51b7662 1072{
009946f8 1073 u32 reg_addr, gtt_addr;
4b60d29e 1074 int size = KB(512);
f51b7662 1075
2d2430cf 1076 pci_read_config_dword(intel_private.pcidev, I915_MMADDR, &reg_addr);
f51b7662 1077
2d2430cf 1078 reg_addr &= 0xfff80000;
f1befe71 1079
4b60d29e 1080 intel_private.registers = ioremap(reg_addr, size);
ccc4e67b 1081 if (!intel_private.registers)
f51b7662
DV
1082 return -ENOMEM;
1083
009946f8
BW
1084 switch (INTEL_GTT_GEN) {
1085 case 3:
2d2430cf
DV
1086 pci_read_config_dword(intel_private.pcidev,
1087 I915_PTEADDR, &gtt_addr);
1088 intel_private.gtt_bus_addr = gtt_addr;
009946f8
BW
1089 break;
1090 case 5:
1091 intel_private.gtt_bus_addr = reg_addr + MB(2);
1092 break;
1093 default:
1094 intel_private.gtt_bus_addr = reg_addr + KB(512);
1095 break;
2d2430cf
DV
1096 }
1097
1098 intel_i9xx_setup_flush();
1099
1100 return 0;
1101}
1102
e9b1cc81 1103static const struct agp_bridge_driver intel_fake_agp_driver = {
f51b7662 1104 .owner = THIS_MODULE,
f51b7662 1105 .size_type = FIXED_APER_SIZE,
9e76e7b8
CW
1106 .aperture_sizes = intel_fake_agp_sizes,
1107 .num_aperture_sizes = ARRAY_SIZE(intel_fake_agp_sizes),
a6963596 1108 .configure = intel_fake_agp_configure,
3e921f98 1109 .fetch_size = intel_fake_agp_fetch_size,
fdfb58a9 1110 .cleanup = intel_gtt_cleanup,
ffdd7510 1111 .agp_enable = intel_fake_agp_enable,
f51b7662 1112 .cache_flush = global_cache_flush,
3b15a9d7 1113 .create_gatt_table = intel_fake_agp_create_gatt_table,
ffdd7510 1114 .free_gatt_table = intel_fake_agp_free_gatt_table,
450f2b3d
DV
1115 .insert_memory = intel_fake_agp_insert_entries,
1116 .remove_memory = intel_fake_agp_remove_entries,
ffdd7510 1117 .alloc_by_type = intel_fake_agp_alloc_by_type,
f51b7662
DV
1118 .free_by_type = intel_i810_free_by_type,
1119 .agp_alloc_page = agp_generic_alloc_page,
1120 .agp_alloc_pages = agp_generic_alloc_pages,
1121 .agp_destroy_page = agp_generic_destroy_page,
1122 .agp_destroy_pages = agp_generic_destroy_pages,
f51b7662 1123};
02c026ce 1124
bdd30729
DV
1125static const struct intel_gtt_driver i81x_gtt_driver = {
1126 .gen = 1,
820647b9 1127 .has_pgtbl_enable = 1,
22533b49 1128 .dma_mask_size = 32,
820647b9
DV
1129 .setup = i810_setup,
1130 .cleanup = i810_cleanup,
625dd9d3
DV
1131 .check_flags = i830_check_flags,
1132 .write_entry = i810_write_entry,
bdd30729 1133};
1a997ff2
DV
1134static const struct intel_gtt_driver i8xx_gtt_driver = {
1135 .gen = 2,
100519e2 1136 .has_pgtbl_enable = 1,
73800422 1137 .setup = i830_setup,
ae83dd5c 1138 .cleanup = i830_cleanup,
351bb278 1139 .write_entry = i830_write_entry,
22533b49 1140 .dma_mask_size = 32,
5cbecafc 1141 .check_flags = i830_check_flags,
1b263f24 1142 .chipset_flush = i830_chipset_flush,
1a997ff2
DV
1143};
1144static const struct intel_gtt_driver i915_gtt_driver = {
1145 .gen = 3,
100519e2 1146 .has_pgtbl_enable = 1,
2d2430cf 1147 .setup = i9xx_setup,
ae83dd5c 1148 .cleanup = i9xx_cleanup,
351bb278 1149 /* i945 is the last gpu to need phys mem (for overlay and cursors). */
625dd9d3 1150 .write_entry = i830_write_entry,
22533b49 1151 .dma_mask_size = 32,
fefaa70f 1152 .check_flags = i830_check_flags,
1b263f24 1153 .chipset_flush = i9xx_chipset_flush,
1a997ff2
DV
1154};
1155static const struct intel_gtt_driver g33_gtt_driver = {
1156 .gen = 3,
1157 .is_g33 = 1,
2d2430cf 1158 .setup = i9xx_setup,
ae83dd5c 1159 .cleanup = i9xx_cleanup,
a6963596 1160 .write_entry = i965_write_entry,
22533b49 1161 .dma_mask_size = 36,
450f2b3d 1162 .check_flags = i830_check_flags,
1b263f24 1163 .chipset_flush = i9xx_chipset_flush,
1a997ff2
DV
1164};
1165static const struct intel_gtt_driver pineview_gtt_driver = {
1166 .gen = 3,
1167 .is_pineview = 1, .is_g33 = 1,
2d2430cf 1168 .setup = i9xx_setup,
ae83dd5c 1169 .cleanup = i9xx_cleanup,
a6963596 1170 .write_entry = i965_write_entry,
22533b49 1171 .dma_mask_size = 36,
450f2b3d 1172 .check_flags = i830_check_flags,
1b263f24 1173 .chipset_flush = i9xx_chipset_flush,
1a997ff2
DV
1174};
1175static const struct intel_gtt_driver i965_gtt_driver = {
1176 .gen = 4,
100519e2 1177 .has_pgtbl_enable = 1,
2d2430cf 1178 .setup = i9xx_setup,
ae83dd5c 1179 .cleanup = i9xx_cleanup,
a6963596 1180 .write_entry = i965_write_entry,
22533b49 1181 .dma_mask_size = 36,
450f2b3d 1182 .check_flags = i830_check_flags,
1b263f24 1183 .chipset_flush = i9xx_chipset_flush,
1a997ff2
DV
1184};
1185static const struct intel_gtt_driver g4x_gtt_driver = {
1186 .gen = 5,
2d2430cf 1187 .setup = i9xx_setup,
ae83dd5c 1188 .cleanup = i9xx_cleanup,
a6963596 1189 .write_entry = i965_write_entry,
22533b49 1190 .dma_mask_size = 36,
450f2b3d 1191 .check_flags = i830_check_flags,
1b263f24 1192 .chipset_flush = i9xx_chipset_flush,
1a997ff2
DV
1193};
1194static const struct intel_gtt_driver ironlake_gtt_driver = {
1195 .gen = 5,
1196 .is_ironlake = 1,
2d2430cf 1197 .setup = i9xx_setup,
ae83dd5c 1198 .cleanup = i9xx_cleanup,
a6963596 1199 .write_entry = i965_write_entry,
22533b49 1200 .dma_mask_size = 36,
450f2b3d 1201 .check_flags = i830_check_flags,
1b263f24 1202 .chipset_flush = i9xx_chipset_flush,
1a997ff2 1203};
1a997ff2 1204
02c026ce
DV
1205/* Table to describe Intel GMCH and AGP/PCIE GART drivers. At least one of
1206 * driver and gmch_driver must be non-null, and find_gmch will determine
1207 * which one should be used if a gmch_chip_id is present.
1208 */
1209static const struct intel_gtt_driver_description {
1210 unsigned int gmch_chip_id;
1211 char *name;
1a997ff2 1212 const struct intel_gtt_driver *gtt_driver;
02c026ce 1213} intel_gtt_chipsets[] = {
ff26860f 1214 { PCI_DEVICE_ID_INTEL_82810_IG1, "i810",
bdd30729 1215 &i81x_gtt_driver},
ff26860f 1216 { PCI_DEVICE_ID_INTEL_82810_IG3, "i810",
bdd30729 1217 &i81x_gtt_driver},
ff26860f 1218 { PCI_DEVICE_ID_INTEL_82810E_IG, "i810",
bdd30729 1219 &i81x_gtt_driver},
ff26860f 1220 { PCI_DEVICE_ID_INTEL_82815_CGC, "i815",
bdd30729 1221 &i81x_gtt_driver},
1a997ff2 1222 { PCI_DEVICE_ID_INTEL_82830_CGC, "830M",
ff26860f 1223 &i8xx_gtt_driver},
53371eda 1224 { PCI_DEVICE_ID_INTEL_82845G_IG, "845G",
ff26860f 1225 &i8xx_gtt_driver},
1a997ff2 1226 { PCI_DEVICE_ID_INTEL_82854_IG, "854",
ff26860f 1227 &i8xx_gtt_driver},
1a997ff2 1228 { PCI_DEVICE_ID_INTEL_82855GM_IG, "855GM",
ff26860f 1229 &i8xx_gtt_driver},
1a997ff2 1230 { PCI_DEVICE_ID_INTEL_82865_IG, "865",
ff26860f 1231 &i8xx_gtt_driver},
1a997ff2 1232 { PCI_DEVICE_ID_INTEL_E7221_IG, "E7221 (i915)",
ff26860f 1233 &i915_gtt_driver },
1a997ff2 1234 { PCI_DEVICE_ID_INTEL_82915G_IG, "915G",
ff26860f 1235 &i915_gtt_driver },
1a997ff2 1236 { PCI_DEVICE_ID_INTEL_82915GM_IG, "915GM",
ff26860f 1237 &i915_gtt_driver },
1a997ff2 1238 { PCI_DEVICE_ID_INTEL_82945G_IG, "945G",
ff26860f 1239 &i915_gtt_driver },
1a997ff2 1240 { PCI_DEVICE_ID_INTEL_82945GM_IG, "945GM",
ff26860f 1241 &i915_gtt_driver },
1a997ff2 1242 { PCI_DEVICE_ID_INTEL_82945GME_IG, "945GME",
ff26860f 1243 &i915_gtt_driver },
1a997ff2 1244 { PCI_DEVICE_ID_INTEL_82946GZ_IG, "946GZ",
ff26860f 1245 &i965_gtt_driver },
1a997ff2 1246 { PCI_DEVICE_ID_INTEL_82G35_IG, "G35",
ff26860f 1247 &i965_gtt_driver },
1a997ff2 1248 { PCI_DEVICE_ID_INTEL_82965Q_IG, "965Q",
ff26860f 1249 &i965_gtt_driver },
1a997ff2 1250 { PCI_DEVICE_ID_INTEL_82965G_IG, "965G",
ff26860f 1251 &i965_gtt_driver },
1a997ff2 1252 { PCI_DEVICE_ID_INTEL_82965GM_IG, "965GM",
ff26860f 1253 &i965_gtt_driver },
1a997ff2 1254 { PCI_DEVICE_ID_INTEL_82965GME_IG, "965GME/GLE",
ff26860f 1255 &i965_gtt_driver },
1a997ff2 1256 { PCI_DEVICE_ID_INTEL_G33_IG, "G33",
ff26860f 1257 &g33_gtt_driver },
1a997ff2 1258 { PCI_DEVICE_ID_INTEL_Q35_IG, "Q35",
ff26860f 1259 &g33_gtt_driver },
1a997ff2 1260 { PCI_DEVICE_ID_INTEL_Q33_IG, "Q33",
ff26860f 1261 &g33_gtt_driver },
1a997ff2 1262 { PCI_DEVICE_ID_INTEL_PINEVIEW_M_IG, "GMA3150",
ff26860f 1263 &pineview_gtt_driver },
1a997ff2 1264 { PCI_DEVICE_ID_INTEL_PINEVIEW_IG, "GMA3150",
ff26860f 1265 &pineview_gtt_driver },
1a997ff2 1266 { PCI_DEVICE_ID_INTEL_GM45_IG, "GM45",
ff26860f 1267 &g4x_gtt_driver },
1a997ff2 1268 { PCI_DEVICE_ID_INTEL_EAGLELAKE_IG, "Eaglelake",
ff26860f 1269 &g4x_gtt_driver },
1a997ff2 1270 { PCI_DEVICE_ID_INTEL_Q45_IG, "Q45/Q43",
ff26860f 1271 &g4x_gtt_driver },
1a997ff2 1272 { PCI_DEVICE_ID_INTEL_G45_IG, "G45/G43",
ff26860f 1273 &g4x_gtt_driver },
1a997ff2 1274 { PCI_DEVICE_ID_INTEL_B43_IG, "B43",
ff26860f 1275 &g4x_gtt_driver },
e9e5f8e8 1276 { PCI_DEVICE_ID_INTEL_B43_1_IG, "B43",
ff26860f 1277 &g4x_gtt_driver },
1a997ff2 1278 { PCI_DEVICE_ID_INTEL_G41_IG, "G41",
ff26860f 1279 &g4x_gtt_driver },
02c026ce 1280 { PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG,
ff26860f 1281 "HD Graphics", &ironlake_gtt_driver },
02c026ce 1282 { PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG,
ff26860f 1283 "HD Graphics", &ironlake_gtt_driver },
02c026ce
DV
1284 { 0, NULL, NULL }
1285};
1286
1287static int find_gmch(u16 device)
1288{
1289 struct pci_dev *gmch_device;
1290
1291 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL, device, NULL);
1292 if (gmch_device && PCI_FUNC(gmch_device->devfn) != 0) {
1293 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL,
1294 device, gmch_device);
1295 }
1296
1297 if (!gmch_device)
1298 return 0;
1299
1300 intel_private.pcidev = gmch_device;
1301 return 1;
1302}
1303
14be93dd
DV
1304int intel_gmch_probe(struct pci_dev *bridge_pdev, struct pci_dev *gpu_pdev,
1305 struct agp_bridge_data *bridge)
02c026ce
DV
1306{
1307 int i, mask;
14be93dd
DV
1308
1309 /*
1310 * Can be called from the fake agp driver but also directly from
1311 * drm/i915.ko. Hence we need to check whether everything is set up
1312 * already.
1313 */
1314 if (intel_private.driver) {
1315 intel_private.refcount++;
1316 return 1;
1317 }
02c026ce
DV
1318
1319 for (i = 0; intel_gtt_chipsets[i].name != NULL; i++) {
14be93dd
DV
1320 if (gpu_pdev) {
1321 if (gpu_pdev->device ==
1322 intel_gtt_chipsets[i].gmch_chip_id) {
1323 intel_private.pcidev = pci_dev_get(gpu_pdev);
1324 intel_private.driver =
1325 intel_gtt_chipsets[i].gtt_driver;
1326
1327 break;
1328 }
1329 } else if (find_gmch(intel_gtt_chipsets[i].gmch_chip_id)) {
625dd9d3 1330 intel_private.driver =
1a997ff2 1331 intel_gtt_chipsets[i].gtt_driver;
02c026ce
DV
1332 break;
1333 }
1334 }
1335
ff26860f 1336 if (!intel_private.driver)
02c026ce
DV
1337 return 0;
1338
14be93dd
DV
1339 intel_private.refcount++;
1340
7e8f6306
DV
1341 if (bridge) {
1342 bridge->driver = &intel_fake_agp_driver;
1343 bridge->dev_private_data = &intel_private;
14be93dd 1344 bridge->dev = bridge_pdev;
7e8f6306 1345 }
02c026ce 1346
14be93dd 1347 intel_private.bridge_dev = pci_dev_get(bridge_pdev);
d7cca2f7 1348
14be93dd 1349 dev_info(&bridge_pdev->dev, "Intel %s Chipset\n", intel_gtt_chipsets[i].name);
02c026ce 1350
22533b49 1351 mask = intel_private.driver->dma_mask_size;
02c026ce
DV
1352 if (pci_set_dma_mask(intel_private.pcidev, DMA_BIT_MASK(mask)))
1353 dev_err(&intel_private.pcidev->dev,
1354 "set gfx device dma mask %d-bit failed!\n", mask);
1355 else
1356 pci_set_consistent_dma_mask(intel_private.pcidev,
1357 DMA_BIT_MASK(mask));
1358
14be93dd
DV
1359 if (intel_gtt_init() != 0) {
1360 intel_gmch_remove();
1361
3b15a9d7 1362 return 0;
14be93dd 1363 }
1784a5fb 1364
02c026ce
DV
1365 return 1;
1366}
e2404e7c 1367EXPORT_SYMBOL(intel_gmch_probe);
02c026ce 1368
e76e9aeb 1369struct intel_gtt *intel_gtt_get(void)
19966754
DV
1370{
1371 return &intel_private.base;
1372}
1373EXPORT_SYMBOL(intel_gtt_get);
1374
40ce6575
DV
1375void intel_gtt_chipset_flush(void)
1376{
1377 if (intel_private.driver->chipset_flush)
1378 intel_private.driver->chipset_flush();
1379}
1380EXPORT_SYMBOL(intel_gtt_chipset_flush);
1381
14be93dd 1382void intel_gmch_remove(void)
02c026ce 1383{
14be93dd
DV
1384 if (--intel_private.refcount)
1385 return;
1386
02c026ce
DV
1387 if (intel_private.pcidev)
1388 pci_dev_put(intel_private.pcidev);
d7cca2f7
DV
1389 if (intel_private.bridge_dev)
1390 pci_dev_put(intel_private.bridge_dev);
14be93dd 1391 intel_private.driver = NULL;
02c026ce 1392}
e2404e7c
DV
1393EXPORT_SYMBOL(intel_gmch_remove);
1394
1395MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
1396MODULE_LICENSE("GPL and additional rights");