blob: 7fcee5c2e98622be20bb111b10d75f9cc971bdba [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>
Chris Wilsonbdb8b972010-12-22 11:37:09 +000024#include <linux/delay.h>
Daniel Vettere2404e72010-09-08 17:29:51 +020025#include <asm/smp.h>
26#include "agp.h"
27#include "intel-agp.h"
Daniel Vetter0ade6382010-08-24 22:18:41 +020028#include <drm/intel-gtt.h>
Daniel Vettere2404e72010-09-08 17:29:51 +020029
Daniel Vetterf51b7662010-04-14 00:29:52 +020030/*
31 * If we have Intel graphics, we're not going to have anything other than
32 * an Intel IOMMU. So make the correct use of the PCI DMA API contingent
Suresh Siddhad3f13812011-08-23 17:05:25 -070033 * on the Intel IOMMU support (CONFIG_INTEL_IOMMU).
Daniel Vetterf51b7662010-04-14 00:29:52 +020034 * Only newer chipsets need to bother with this, of course.
35 */
Suresh Siddhad3f13812011-08-23 17:05:25 -070036#ifdef CONFIG_INTEL_IOMMU
Daniel Vetterf51b7662010-04-14 00:29:52 +020037#define USE_PCI_DMA_API 1
Daniel Vetter0e87d2b2010-09-07 22:11:15 +020038#else
39#define USE_PCI_DMA_API 0
Daniel Vetterf51b7662010-04-14 00:29:52 +020040#endif
41
Daniel Vetter1a997ff2010-09-08 21:18:53 +020042struct 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;
Chris Wilson100519e2010-10-31 10:37:02 +000047 unsigned int has_pgtbl_enable : 1;
Daniel Vetter22533b42010-09-12 16:38:55 +020048 unsigned int dma_mask_size : 8;
Daniel Vetter73800422010-08-29 17:29:50 +020049 /* Chipset specific GTT setup */
50 int (*setup)(void);
Daniel Vetterae83dd52010-09-12 17:11:15 +020051 /* 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);
Daniel Vetter351bb272010-09-07 22:41:04 +020054 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! */
Daniel Vetter5cbecaf2010-09-11 21:31:04 +020058 bool (*check_flags)(unsigned int flags);
Daniel Vetter1b263f22010-09-12 00:27:24 +020059 void (*chipset_flush)(void);
Daniel Vetter1a997ff2010-09-08 21:18:53 +020060};
61
Daniel Vetterf51b7662010-04-14 00:29:52 +020062static struct _intel_private {
Daniel Vetter0ade6382010-08-24 22:18:41 +020063 struct intel_gtt base;
Daniel Vetter1a997ff2010-09-08 21:18:53 +020064 const struct intel_gtt_driver *driver;
Daniel Vetterf51b7662010-04-14 00:29:52 +020065 struct pci_dev *pcidev; /* device one */
Daniel Vetterd7cca2f2010-08-24 23:06:19 +020066 struct pci_dev *bridge_dev;
Daniel Vetterf51b7662010-04-14 00:29:52 +020067 u8 __iomem *registers;
Daniel Vetterf67eab62010-08-29 17:27:36 +020068 phys_addr_t gtt_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 */
Chris Wilsonbee4a182011-01-21 10:54:32 +000071 bool clear_fake_agp; /* on first access via agp, fill with scratch */
Daniel Vetterf51b7662010-04-14 00:29:52 +020072 int num_dcache_entries;
Chris Wilsonbdb8b972010-12-22 11:37:09 +000073 void __iomem *i9xx_flush_page;
Daniel Vetter820647b2010-11-05 13:30:14 +010074 char *i81x_gtt_table;
Daniel Vetterf51b7662010-04-14 00:29:52 +020075 struct resource ifp_resource;
76 int resource_valid;
Daniel Vetter0e87d2b2010-09-07 22:11:15 +020077 struct page *scratch_page;
Ben Widawsky9c61a322013-01-18 12:30:32 -080078 phys_addr_t scratch_page_dma;
Daniel Vetter14be93d2012-06-08 15:55:40 +020079 int refcount;
Daniel Vetterf51b7662010-04-14 00:29:52 +020080} intel_private;
81
Daniel Vetter1a997ff2010-09-08 21:18:53 +020082#define INTEL_GTT_GEN intel_private.driver->gen
83#define IS_G33 intel_private.driver->is_g33
84#define IS_PINEVIEW intel_private.driver->is_pineview
85#define IS_IRONLAKE intel_private.driver->is_ironlake
Chris Wilson100519e2010-10-31 10:37:02 +000086#define HAS_PGTBL_EN intel_private.driver->has_pgtbl_enable
Daniel Vetter1a997ff2010-09-08 21:18:53 +020087
Chris Wilson9da3da62012-06-01 15:20:22 +010088static int intel_gtt_map_memory(struct page **pages,
89 unsigned int num_entries,
90 struct sg_table *st)
Daniel Vetterf51b7662010-04-14 00:29:52 +020091{
Daniel Vetterf51b7662010-04-14 00:29:52 +020092 struct scatterlist *sg;
93 int i;
94
Daniel Vetter40807752010-11-06 11:18:58 +010095 DBG("try mapping %lu pages\n", (unsigned long)num_entries);
Daniel Vetterf51b7662010-04-14 00:29:52 +020096
Chris Wilson9da3da62012-06-01 15:20:22 +010097 if (sg_alloc_table(st, num_entries, GFP_KERNEL))
Chris Wilson831cd442010-07-24 18:29:37 +010098 goto err;
Daniel Vetterf51b7662010-04-14 00:29:52 +020099
Chris Wilson9da3da62012-06-01 15:20:22 +0100100 for_each_sg(st->sgl, sg, num_entries, i)
Daniel Vetter40807752010-11-06 11:18:58 +0100101 sg_set_page(sg, pages[i], PAGE_SIZE, 0);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200102
Chris Wilson9da3da62012-06-01 15:20:22 +0100103 if (!pci_map_sg(intel_private.pcidev,
104 st->sgl, st->nents, PCI_DMA_BIDIRECTIONAL))
Chris Wilson831cd442010-07-24 18:29:37 +0100105 goto err;
106
Daniel Vetterf51b7662010-04-14 00:29:52 +0200107 return 0;
Chris Wilson831cd442010-07-24 18:29:37 +0100108
109err:
Chris Wilson9da3da62012-06-01 15:20:22 +0100110 sg_free_table(st);
Chris Wilson831cd442010-07-24 18:29:37 +0100111 return -ENOMEM;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200112}
113
Chris Wilson9da3da62012-06-01 15:20:22 +0100114static void intel_gtt_unmap_memory(struct scatterlist *sg_list, int num_sg)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200115{
Daniel Vetter40807752010-11-06 11:18:58 +0100116 struct sg_table st;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200117 DBG("try unmapping %lu pages\n", (unsigned long)mem->page_count);
118
Daniel Vetter40807752010-11-06 11:18:58 +0100119 pci_unmap_sg(intel_private.pcidev, sg_list,
120 num_sg, PCI_DMA_BIDIRECTIONAL);
121
122 st.sgl = sg_list;
123 st.orig_nents = st.nents = num_sg;
124
125 sg_free_table(&st);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200126}
127
Daniel Vetterffdd7512010-08-27 17:51:29 +0200128static void intel_fake_agp_enable(struct agp_bridge_data *bridge, u32 mode)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200129{
130 return;
131}
132
133/* Exists to support ARGB cursors */
134static struct page *i8xx_alloc_pages(void)
135{
136 struct page *page;
137
138 page = alloc_pages(GFP_KERNEL | GFP_DMA32, 2);
139 if (page == NULL)
140 return NULL;
141
142 if (set_pages_uc(page, 4) < 0) {
143 set_pages_wb(page, 4);
144 __free_pages(page, 2);
145 return NULL;
146 }
147 get_page(page);
148 atomic_inc(&agp_bridge->current_memory_agp);
149 return page;
150}
151
152static void i8xx_destroy_pages(struct page *page)
153{
154 if (page == NULL)
155 return;
156
157 set_pages_wb(page, 4);
158 put_page(page);
159 __free_pages(page, 2);
160 atomic_dec(&agp_bridge->current_memory_agp);
161}
162
Daniel Vetter820647b2010-11-05 13:30:14 +0100163#define I810_GTT_ORDER 4
164static int i810_setup(void)
165{
166 u32 reg_addr;
167 char *gtt_table;
168
169 /* i81x does not preallocate the gtt. It's always 64kb in size. */
170 gtt_table = alloc_gatt_pages(I810_GTT_ORDER);
171 if (gtt_table == NULL)
172 return -ENOMEM;
173 intel_private.i81x_gtt_table = gtt_table;
174
175 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &reg_addr);
176 reg_addr &= 0xfff80000;
177
178 intel_private.registers = ioremap(reg_addr, KB(64));
179 if (!intel_private.registers)
180 return -ENOMEM;
181
182 writel(virt_to_phys(gtt_table) | I810_PGETBL_ENABLED,
183 intel_private.registers+I810_PGETBL_CTL);
184
185 intel_private.gtt_bus_addr = reg_addr + I810_PTE_BASE;
186
187 if ((readl(intel_private.registers+I810_DRAM_CTL)
188 & I810_DRAM_ROW_0) == I810_DRAM_ROW_0_SDRAM) {
189 dev_info(&intel_private.pcidev->dev,
190 "detected 4MB dedicated video ram\n");
191 intel_private.num_dcache_entries = 1024;
192 }
193
194 return 0;
195}
196
197static void i810_cleanup(void)
198{
199 writel(0, intel_private.registers+I810_PGETBL_CTL);
200 free_gatt_pages(intel_private.i81x_gtt_table, I810_GTT_ORDER);
201}
202
Daniel Vetterff268602010-11-05 15:43:35 +0100203static int i810_insert_dcache_entries(struct agp_memory *mem, off_t pg_start,
204 int type)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200205{
Daniel Vetterf51b7662010-04-14 00:29:52 +0200206 int i;
207
Daniel Vetterff268602010-11-05 15:43:35 +0100208 if ((pg_start + mem->page_count)
209 > intel_private.num_dcache_entries)
210 return -EINVAL;
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100211
Daniel Vetterff268602010-11-05 15:43:35 +0100212 if (!mem->is_flushed)
213 global_cache_flush();
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100214
Daniel Vetterff268602010-11-05 15:43:35 +0100215 for (i = pg_start; i < (pg_start + mem->page_count); i++) {
216 dma_addr_t addr = i << PAGE_SHIFT;
217 intel_private.driver->write_entry(addr,
218 i, type);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200219 }
Daniel Vetterff268602010-11-05 15:43:35 +0100220 readl(intel_private.gtt+i-1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200221
Daniel Vetterff268602010-11-05 15:43:35 +0100222 return 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200223}
224
225/*
226 * The i810/i830 requires a physical address to program its mouse
227 * pointer into hardware.
228 * However the Xserver still writes to it through the agp aperture.
229 */
230static struct agp_memory *alloc_agpphysmem_i8xx(size_t pg_count, int type)
231{
232 struct agp_memory *new;
233 struct page *page;
234
235 switch (pg_count) {
236 case 1: page = agp_bridge->driver->agp_alloc_page(agp_bridge);
237 break;
238 case 4:
239 /* kludge to get 4 physical pages for ARGB cursor */
240 page = i8xx_alloc_pages();
241 break;
242 default:
243 return NULL;
244 }
245
246 if (page == NULL)
247 return NULL;
248
249 new = agp_create_memory(pg_count);
250 if (new == NULL)
251 return NULL;
252
253 new->pages[0] = page;
254 if (pg_count == 4) {
255 /* kludge to get 4 physical pages for ARGB cursor */
256 new->pages[1] = new->pages[0] + 1;
257 new->pages[2] = new->pages[1] + 1;
258 new->pages[3] = new->pages[2] + 1;
259 }
260 new->page_count = pg_count;
261 new->num_scratch_pages = pg_count;
262 new->type = AGP_PHYS_MEMORY;
263 new->physical = page_to_phys(new->pages[0]);
264 return new;
265}
266
Daniel Vetterf51b7662010-04-14 00:29:52 +0200267static void intel_i810_free_by_type(struct agp_memory *curr)
268{
269 agp_free_key(curr->key);
270 if (curr->type == AGP_PHYS_MEMORY) {
271 if (curr->page_count == 4)
272 i8xx_destroy_pages(curr->pages[0]);
273 else {
274 agp_bridge->driver->agp_destroy_page(curr->pages[0],
275 AGP_PAGE_DESTROY_UNMAP);
276 agp_bridge->driver->agp_destroy_page(curr->pages[0],
277 AGP_PAGE_DESTROY_FREE);
278 }
279 agp_free_page_array(curr);
280 }
281 kfree(curr);
282}
283
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200284static int intel_gtt_setup_scratch_page(void)
285{
286 struct page *page;
287 dma_addr_t dma_addr;
288
289 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
290 if (page == NULL)
291 return -ENOMEM;
292 get_page(page);
293 set_pages_uc(page, 1);
294
Daniel Vetter40807752010-11-06 11:18:58 +0100295 if (intel_private.base.needs_dmar) {
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200296 dma_addr = pci_map_page(intel_private.pcidev, page, 0,
297 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
298 if (pci_dma_mapping_error(intel_private.pcidev, dma_addr))
299 return -EINVAL;
300
Ben Widawsky9c61a322013-01-18 12:30:32 -0800301 intel_private.scratch_page_dma = dma_addr;
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200302 } else
Ben Widawsky9c61a322013-01-18 12:30:32 -0800303 intel_private.scratch_page_dma = page_to_phys(page);
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200304
305 intel_private.scratch_page = page;
306
307 return 0;
308}
309
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100310static void i810_write_entry(dma_addr_t addr, unsigned int entry,
311 unsigned int flags)
312{
313 u32 pte_flags = I810_PTE_VALID;
314
315 switch (flags) {
316 case AGP_DCACHE_MEMORY:
317 pte_flags |= I810_PTE_LOCAL;
318 break;
319 case AGP_USER_CACHED_MEMORY:
320 pte_flags |= I830_PTE_SYSTEM_CACHED;
321 break;
322 }
323
324 writel(addr | pte_flags, intel_private.gtt + entry);
325}
326
Chris Wilson7bdc9ab2010-11-09 17:53:20 +0000327static const struct aper_size_info_fixed intel_fake_agp_sizes[] = {
Daniel Vetter820647b2010-11-05 13:30:14 +0100328 {32, 8192, 3},
329 {64, 16384, 4},
Daniel Vetterf51b7662010-04-14 00:29:52 +0200330 {128, 32768, 5},
Daniel Vetterf51b7662010-04-14 00:29:52 +0200331 {256, 65536, 6},
332 {512, 131072, 7},
333};
334
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000335static unsigned int intel_gtt_stolen_size(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200336{
337 u16 gmch_ctrl;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200338 u8 rdct;
339 int local = 0;
340 static const int ddt[4] = { 0, 16, 32, 64 };
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200341 unsigned int stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200342
Daniel Vetter820647b2010-11-05 13:30:14 +0100343 if (INTEL_GTT_GEN == 1)
344 return 0; /* no stolen mem on i81x */
345
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200346 pci_read_config_word(intel_private.bridge_dev,
347 I830_GMCH_CTRL, &gmch_ctrl);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200348
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200349 if (intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82830_HB ||
350 intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82845G_HB) {
Daniel Vetterf51b7662010-04-14 00:29:52 +0200351 switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
352 case I830_GMCH_GMS_STOLEN_512:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200353 stolen_size = KB(512);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200354 break;
355 case I830_GMCH_GMS_STOLEN_1024:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200356 stolen_size = MB(1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200357 break;
358 case I830_GMCH_GMS_STOLEN_8192:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200359 stolen_size = MB(8);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200360 break;
361 case I830_GMCH_GMS_LOCAL:
362 rdct = readb(intel_private.registers+I830_RDRAM_CHANNEL_TYPE);
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200363 stolen_size = (I830_RDRAM_ND(rdct) + 1) *
Daniel Vetterf51b7662010-04-14 00:29:52 +0200364 MB(ddt[I830_RDRAM_DDT(rdct)]);
365 local = 1;
366 break;
367 default:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200368 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200369 break;
370 }
Daniel Vetterf51b7662010-04-14 00:29:52 +0200371 } else {
372 switch (gmch_ctrl & I855_GMCH_GMS_MASK) {
373 case I855_GMCH_GMS_STOLEN_1M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200374 stolen_size = MB(1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200375 break;
376 case I855_GMCH_GMS_STOLEN_4M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200377 stolen_size = MB(4);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200378 break;
379 case I855_GMCH_GMS_STOLEN_8M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200380 stolen_size = MB(8);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200381 break;
382 case I855_GMCH_GMS_STOLEN_16M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200383 stolen_size = MB(16);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200384 break;
385 case I855_GMCH_GMS_STOLEN_32M:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200386 stolen_size = MB(32);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200387 break;
388 case I915_GMCH_GMS_STOLEN_48M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200389 stolen_size = MB(48);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200390 break;
391 case I915_GMCH_GMS_STOLEN_64M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200392 stolen_size = MB(64);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200393 break;
394 case G33_GMCH_GMS_STOLEN_128M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200395 stolen_size = MB(128);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200396 break;
397 case G33_GMCH_GMS_STOLEN_256M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200398 stolen_size = MB(256);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200399 break;
400 case INTEL_GMCH_GMS_STOLEN_96M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200401 stolen_size = MB(96);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200402 break;
403 case INTEL_GMCH_GMS_STOLEN_160M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200404 stolen_size = MB(160);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200405 break;
406 case INTEL_GMCH_GMS_STOLEN_224M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200407 stolen_size = MB(224);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200408 break;
409 case INTEL_GMCH_GMS_STOLEN_352M:
Daniel Vetter77ad4982010-08-27 16:25:54 +0200410 stolen_size = MB(352);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200411 break;
412 default:
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200413 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200414 break;
415 }
416 }
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200417
Chris Wilson1b6064d2010-11-23 12:33:54 +0000418 if (stolen_size > 0) {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200419 dev_info(&intel_private.bridge_dev->dev, "detected %dK %s memory\n",
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200420 stolen_size / KB(1), local ? "local" : "stolen");
Daniel Vetterf51b7662010-04-14 00:29:52 +0200421 } else {
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200422 dev_info(&intel_private.bridge_dev->dev,
Daniel Vetterf51b7662010-04-14 00:29:52 +0200423 "no pre-allocated video memory detected\n");
Daniel Vetterd8d9abc2010-08-27 16:13:52 +0200424 stolen_size = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200425 }
426
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000427 return stolen_size;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200428}
429
Daniel Vetter20172842010-09-24 18:25:59 +0200430static void i965_adjust_pgetbl_size(unsigned int size_flag)
431{
432 u32 pgetbl_ctl, pgetbl_ctl2;
433
434 /* ensure that ppgtt is disabled */
435 pgetbl_ctl2 = readl(intel_private.registers+I965_PGETBL_CTL2);
436 pgetbl_ctl2 &= ~I810_PGETBL_ENABLED;
437 writel(pgetbl_ctl2, intel_private.registers+I965_PGETBL_CTL2);
438
439 /* write the new ggtt size */
440 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
441 pgetbl_ctl &= ~I965_PGETBL_SIZE_MASK;
442 pgetbl_ctl |= size_flag;
443 writel(pgetbl_ctl, intel_private.registers+I810_PGETBL_CTL);
444}
445
446static unsigned int i965_gtt_total_entries(void)
447{
448 int size;
449 u32 pgetbl_ctl;
450 u16 gmch_ctl;
451
452 pci_read_config_word(intel_private.bridge_dev,
453 I830_GMCH_CTRL, &gmch_ctl);
454
455 if (INTEL_GTT_GEN == 5) {
456 switch (gmch_ctl & G4x_GMCH_SIZE_MASK) {
457 case G4x_GMCH_SIZE_1M:
458 case G4x_GMCH_SIZE_VT_1M:
459 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1MB);
460 break;
461 case G4x_GMCH_SIZE_VT_1_5M:
462 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1_5MB);
463 break;
464 case G4x_GMCH_SIZE_2M:
465 case G4x_GMCH_SIZE_VT_2M:
466 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_2MB);
467 break;
468 }
469 }
470
471 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
472
473 switch (pgetbl_ctl & I965_PGETBL_SIZE_MASK) {
474 case I965_PGETBL_SIZE_128KB:
475 size = KB(128);
476 break;
477 case I965_PGETBL_SIZE_256KB:
478 size = KB(256);
479 break;
480 case I965_PGETBL_SIZE_512KB:
481 size = KB(512);
482 break;
483 /* GTT pagetable sizes bigger than 512KB are not possible on G33! */
484 case I965_PGETBL_SIZE_1MB:
485 size = KB(1024);
486 break;
487 case I965_PGETBL_SIZE_2MB:
488 size = KB(2048);
489 break;
490 case I965_PGETBL_SIZE_1_5MB:
491 size = KB(1024 + 512);
492 break;
493 default:
494 dev_info(&intel_private.pcidev->dev,
495 "unknown page table size, assuming 512KB\n");
496 size = KB(512);
497 }
498
499 return size/4;
500}
501
Daniel Vetterfbe40782010-08-27 17:12:41 +0200502static unsigned int intel_gtt_total_entries(void)
503{
Daniel Vetter20172842010-09-24 18:25:59 +0200504 if (IS_G33 || INTEL_GTT_GEN == 4 || INTEL_GTT_GEN == 5)
505 return i965_gtt_total_entries();
Ben Widawsky009946f2012-11-04 09:21:29 -0800506 else {
Daniel Vetterfbe40782010-08-27 17:12:41 +0200507 /* On previous hardware, the GTT size was just what was
508 * required to map the aperture.
509 */
Daniel Vettere5e408f2010-08-28 11:04:32 +0200510 return intel_private.base.gtt_mappable_entries;
Daniel Vetterfbe40782010-08-27 17:12:41 +0200511 }
Daniel Vetterfbe40782010-08-27 17:12:41 +0200512}
Daniel Vetterfbe40782010-08-27 17:12:41 +0200513
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200514static unsigned int intel_gtt_mappable_entries(void)
515{
516 unsigned int aperture_size;
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200517
Daniel Vetter820647b2010-11-05 13:30:14 +0100518 if (INTEL_GTT_GEN == 1) {
519 u32 smram_miscc;
520
521 pci_read_config_dword(intel_private.bridge_dev,
522 I810_SMRAM_MISCC, &smram_miscc);
523
524 if ((smram_miscc & I810_GFX_MEM_WIN_SIZE)
525 == I810_GFX_MEM_WIN_32M)
526 aperture_size = MB(32);
527 else
528 aperture_size = MB(64);
529 } else if (INTEL_GTT_GEN == 2) {
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100530 u16 gmch_ctrl;
531
532 pci_read_config_word(intel_private.bridge_dev,
533 I830_GMCH_CTRL, &gmch_ctrl);
534
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200535 if ((gmch_ctrl & I830_GMCH_MEM_MASK) == I830_GMCH_MEM_64M)
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100536 aperture_size = MB(64);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200537 else
Chris Wilsonb1c5b0f2010-09-14 19:30:13 +0100538 aperture_size = MB(128);
Daniel Vetter239918f2010-08-31 22:30:43 +0200539 } else {
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200540 /* 9xx supports large sizes, just look at the length */
541 aperture_size = pci_resource_len(intel_private.pcidev, 2);
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200542 }
543
544 return aperture_size >> PAGE_SHIFT;
545}
546
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200547static void intel_gtt_teardown_scratch_page(void)
548{
549 set_pages_wb(intel_private.scratch_page, 1);
Ben Widawsky9c61a322013-01-18 12:30:32 -0800550 pci_unmap_page(intel_private.pcidev, intel_private.scratch_page_dma,
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200551 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
552 put_page(intel_private.scratch_page);
553 __free_page(intel_private.scratch_page);
554}
555
556static void intel_gtt_cleanup(void)
557{
Daniel Vetterae83dd52010-09-12 17:11:15 +0200558 intel_private.driver->cleanup();
559
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200560 iounmap(intel_private.gtt);
561 iounmap(intel_private.registers);
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100562
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200563 intel_gtt_teardown_scratch_page();
564}
565
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200566static int intel_gtt_init(void)
567{
Daniel Vetter32e3cd62012-06-07 15:56:02 +0200568 u32 gma_addr;
Daniel Vetterf67eab62010-08-29 17:27:36 +0200569 u32 gtt_map_size;
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200570 int ret;
571
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200572 ret = intel_private.driver->setup();
573 if (ret != 0)
574 return ret;
Daniel Vetterf67eab62010-08-29 17:27:36 +0200575
576 intel_private.base.gtt_mappable_entries = intel_gtt_mappable_entries();
577 intel_private.base.gtt_total_entries = intel_gtt_total_entries();
578
Daniel Vetterb3eafc52010-09-23 20:04:17 +0200579 /* save the PGETBL reg for resume */
580 intel_private.PGETBL_save =
581 readl(intel_private.registers+I810_PGETBL_CTL)
582 & ~I810_PGETBL_ENABLED;
Chris Wilson100519e2010-10-31 10:37:02 +0000583 /* we only ever restore the register when enabling the PGTBL... */
584 if (HAS_PGTBL_EN)
585 intel_private.PGETBL_save |= I810_PGETBL_ENABLED;
Daniel Vetterb3eafc52010-09-23 20:04:17 +0200586
Daniel Vetter0af9e922010-09-12 14:04:03 +0200587 dev_info(&intel_private.bridge_dev->dev,
588 "detected gtt size: %dK total, %dK mappable\n",
589 intel_private.base.gtt_total_entries * 4,
590 intel_private.base.gtt_mappable_entries * 4);
591
Daniel Vetterf67eab62010-08-29 17:27:36 +0200592 gtt_map_size = intel_private.base.gtt_total_entries * 4;
593
Chris Wilsonedef7e62012-09-14 11:57:47 +0100594 intel_private.gtt = NULL;
Daniel Vetter9169d3a2012-10-10 23:14:01 +0200595 if (INTEL_GTT_GEN < 6 && INTEL_GTT_GEN > 2)
Chris Wilsonedef7e62012-09-14 11:57:47 +0100596 intel_private.gtt = ioremap_wc(intel_private.gtt_bus_addr,
597 gtt_map_size);
598 if (intel_private.gtt == NULL)
599 intel_private.gtt = ioremap(intel_private.gtt_bus_addr,
600 gtt_map_size);
601 if (intel_private.gtt == NULL) {
Daniel Vetterae83dd52010-09-12 17:11:15 +0200602 intel_private.driver->cleanup();
Daniel Vetterf67eab62010-08-29 17:27:36 +0200603 iounmap(intel_private.registers);
604 return -ENOMEM;
605 }
606
607 global_cache_flush(); /* FIXME: ? */
608
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000609 intel_private.base.stolen_size = intel_gtt_stolen_size();
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200610
Dave Airliea46f3102011-01-12 11:38:37 +1000611 intel_private.base.needs_dmar = USE_PCI_DMA_API && INTEL_GTT_GEN > 2;
612
Daniel Vetter0e87d2b2010-09-07 22:11:15 +0200613 ret = intel_gtt_setup_scratch_page();
614 if (ret != 0) {
615 intel_gtt_cleanup();
616 return ret;
617 }
618
Daniel Vetter32e3cd62012-06-07 15:56:02 +0200619 if (INTEL_GTT_GEN <= 2)
620 pci_read_config_dword(intel_private.pcidev, I810_GMADDR,
621 &gma_addr);
622 else
623 pci_read_config_dword(intel_private.pcidev, I915_GMADDR,
624 &gma_addr);
625
626 intel_private.base.gma_bus_addr = (gma_addr & PCI_BASE_ADDRESS_MEM_MASK);
627
Daniel Vetter1784a5f2010-09-08 21:01:04 +0200628 return 0;
629}
630
Daniel Vetter3e921f92010-08-27 15:33:26 +0200631static int intel_fake_agp_fetch_size(void)
632{
Chris Wilson9e76e7b2010-09-14 12:12:11 +0100633 int num_sizes = ARRAY_SIZE(intel_fake_agp_sizes);
Daniel Vetter3e921f92010-08-27 15:33:26 +0200634 unsigned int aper_size;
635 int i;
Daniel Vetter3e921f92010-08-27 15:33:26 +0200636
637 aper_size = (intel_private.base.gtt_mappable_entries << PAGE_SHIFT)
638 / MB(1);
639
640 for (i = 0; i < num_sizes; i++) {
Daniel Vetterffdd7512010-08-27 17:51:29 +0200641 if (aper_size == intel_fake_agp_sizes[i].size) {
Chris Wilson9e76e7b2010-09-14 12:12:11 +0100642 agp_bridge->current_size =
643 (void *) (intel_fake_agp_sizes + i);
Daniel Vetter3e921f92010-08-27 15:33:26 +0200644 return aper_size;
645 }
646 }
647
648 return 0;
649}
650
Daniel Vetterae83dd52010-09-12 17:11:15 +0200651static void i830_cleanup(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200652{
Daniel Vetterf51b7662010-04-14 00:29:52 +0200653}
654
655/* The chipset_flush interface needs to get data that has already been
656 * flushed out of the CPU all the way out to main memory, because the GPU
657 * doesn't snoop those buffers.
658 *
659 * The 8xx series doesn't have the same lovely interface for flushing the
660 * chipset write buffers that the later chips do. According to the 865
661 * specs, it's 64 octwords, or 1KB. So, to get those previous things in
662 * that buffer out, we just fill 1KB and clflush it out, on the assumption
663 * that it'll push whatever was in there out. It appears to work.
664 */
Daniel Vetter1b263f22010-09-12 00:27:24 +0200665static void i830_chipset_flush(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200666{
Chris Wilsonbdb8b972010-12-22 11:37:09 +0000667 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200668
Chris Wilsonbdb8b972010-12-22 11:37:09 +0000669 /* Forcibly evict everything from the CPU write buffers.
670 * clflush appears to be insufficient.
671 */
672 wbinvd_on_all_cpus();
Daniel Vetterf51b7662010-04-14 00:29:52 +0200673
Chris Wilsonbdb8b972010-12-22 11:37:09 +0000674 /* Now we've only seen documents for this magic bit on 855GM,
675 * we hope it exists for the other gen2 chipsets...
676 *
677 * Also works as advertised on my 845G.
678 */
679 writel(readl(intel_private.registers+I830_HIC) | (1<<31),
680 intel_private.registers+I830_HIC);
681
682 while (readl(intel_private.registers+I830_HIC) & (1<<31)) {
683 if (time_after(jiffies, timeout))
684 break;
685
686 udelay(50);
687 }
Daniel Vetterf51b7662010-04-14 00:29:52 +0200688}
689
Daniel Vetter351bb272010-09-07 22:41:04 +0200690static void i830_write_entry(dma_addr_t addr, unsigned int entry,
691 unsigned int flags)
692{
693 u32 pte_flags = I810_PTE_VALID;
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100694
Daniel Vetterb47cf662010-11-04 18:41:50 +0100695 if (flags == AGP_USER_CACHED_MEMORY)
Daniel Vetter351bb272010-09-07 22:41:04 +0200696 pte_flags |= I830_PTE_SYSTEM_CACHED;
Daniel Vetter351bb272010-09-07 22:41:04 +0200697
698 writel(addr | pte_flags, intel_private.gtt + entry);
699}
700
Daniel Vetter8ecd1a62012-06-07 15:56:03 +0200701bool intel_enable_gtt(void)
Daniel Vetter73800422010-08-29 17:29:50 +0200702{
Chris Wilsone380f602010-10-29 18:11:26 +0100703 u8 __iomem *reg;
Daniel Vetter73800422010-08-29 17:29:50 +0200704
Chris Wilson100519e2010-10-31 10:37:02 +0000705 if (INTEL_GTT_GEN == 2) {
706 u16 gmch_ctrl;
Chris Wilsone380f602010-10-29 18:11:26 +0100707
Chris Wilson100519e2010-10-31 10:37:02 +0000708 pci_read_config_word(intel_private.bridge_dev,
709 I830_GMCH_CTRL, &gmch_ctrl);
710 gmch_ctrl |= I830_GMCH_ENABLED;
711 pci_write_config_word(intel_private.bridge_dev,
712 I830_GMCH_CTRL, gmch_ctrl);
713
714 pci_read_config_word(intel_private.bridge_dev,
715 I830_GMCH_CTRL, &gmch_ctrl);
716 if ((gmch_ctrl & I830_GMCH_ENABLED) == 0) {
717 dev_err(&intel_private.pcidev->dev,
718 "failed to enable the GTT: GMCH_CTRL=%x\n",
719 gmch_ctrl);
720 return false;
721 }
Chris Wilsone380f602010-10-29 18:11:26 +0100722 }
723
Chris Wilsonc97689d2010-12-23 10:40:38 +0000724 /* On the resume path we may be adjusting the PGTBL value, so
725 * be paranoid and flush all chipset write buffers...
726 */
727 if (INTEL_GTT_GEN >= 3)
728 writel(0, intel_private.registers+GFX_FLSH_CNTL);
729
Chris Wilsone380f602010-10-29 18:11:26 +0100730 reg = intel_private.registers+I810_PGETBL_CTL;
Chris Wilson100519e2010-10-31 10:37:02 +0000731 writel(intel_private.PGETBL_save, reg);
732 if (HAS_PGTBL_EN && (readl(reg) & I810_PGETBL_ENABLED) == 0) {
Chris Wilsone380f602010-10-29 18:11:26 +0100733 dev_err(&intel_private.pcidev->dev,
Chris Wilson100519e2010-10-31 10:37:02 +0000734 "failed to enable the GTT: PGETBL=%x [expected %x]\n",
Chris Wilsone380f602010-10-29 18:11:26 +0100735 readl(reg), intel_private.PGETBL_save);
736 return false;
737 }
738
Chris Wilsonc97689d2010-12-23 10:40:38 +0000739 if (INTEL_GTT_GEN >= 3)
740 writel(0, intel_private.registers+GFX_FLSH_CNTL);
741
Chris Wilsone380f602010-10-29 18:11:26 +0100742 return true;
Daniel Vetter73800422010-08-29 17:29:50 +0200743}
Daniel Vetter8ecd1a62012-06-07 15:56:03 +0200744EXPORT_SYMBOL(intel_enable_gtt);
Daniel Vetter73800422010-08-29 17:29:50 +0200745
746static int i830_setup(void)
747{
748 u32 reg_addr;
749
750 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &reg_addr);
751 reg_addr &= 0xfff80000;
752
753 intel_private.registers = ioremap(reg_addr, KB(64));
754 if (!intel_private.registers)
755 return -ENOMEM;
756
757 intel_private.gtt_bus_addr = reg_addr + I810_PTE_BASE;
758
Daniel Vetter73800422010-08-29 17:29:50 +0200759 return 0;
760}
761
Daniel Vetter3b15a9d2010-08-29 14:18:49 +0200762static int intel_fake_agp_create_gatt_table(struct agp_bridge_data *bridge)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200763{
Daniel Vetter73800422010-08-29 17:29:50 +0200764 agp_bridge->gatt_table_real = NULL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200765 agp_bridge->gatt_table = NULL;
Daniel Vetter73800422010-08-29 17:29:50 +0200766 agp_bridge->gatt_bus_addr = 0;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200767
768 return 0;
769}
770
Daniel Vetterffdd7512010-08-27 17:51:29 +0200771static int intel_fake_agp_free_gatt_table(struct agp_bridge_data *bridge)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200772{
773 return 0;
774}
775
Daniel Vetter351bb272010-09-07 22:41:04 +0200776static int intel_fake_agp_configure(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200777{
Chris Wilsone380f602010-10-29 18:11:26 +0100778 if (!intel_enable_gtt())
779 return -EIO;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200780
Chris Wilsonbee4a182011-01-21 10:54:32 +0000781 intel_private.clear_fake_agp = true;
Daniel Vetterdd2757f2012-06-07 15:55:57 +0200782 agp_bridge->gart_bus_addr = intel_private.base.gma_bus_addr;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200783
Daniel Vetterf51b7662010-04-14 00:29:52 +0200784 return 0;
785}
786
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200787static bool i830_check_flags(unsigned int flags)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200788{
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200789 switch (flags) {
790 case 0:
791 case AGP_PHYS_MEMORY:
792 case AGP_USER_CACHED_MEMORY:
793 case AGP_USER_MEMORY:
794 return true;
795 }
796
797 return false;
798}
799
Chris Wilson9da3da62012-06-01 15:20:22 +0100800void intel_gtt_insert_sg_entries(struct sg_table *st,
Daniel Vetter40807752010-11-06 11:18:58 +0100801 unsigned int pg_start,
802 unsigned int flags)
Daniel Vetterfefaa702010-09-11 22:12:11 +0200803{
804 struct scatterlist *sg;
805 unsigned int len, m;
806 int i, j;
807
808 j = pg_start;
809
810 /* sg may merge pages, but we have to separate
811 * per-page addr for GTT */
Chris Wilson9da3da62012-06-01 15:20:22 +0100812 for_each_sg(st->sgl, sg, st->nents, i) {
Daniel Vetterfefaa702010-09-11 22:12:11 +0200813 len = sg_dma_len(sg) >> PAGE_SHIFT;
814 for (m = 0; m < len; m++) {
815 dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
Chris Wilson9da3da62012-06-01 15:20:22 +0100816 intel_private.driver->write_entry(addr, j, flags);
Daniel Vetterfefaa702010-09-11 22:12:11 +0200817 j++;
818 }
819 }
820 readl(intel_private.gtt+j-1);
821}
Daniel Vetter40807752010-11-06 11:18:58 +0100822EXPORT_SYMBOL(intel_gtt_insert_sg_entries);
823
Chris Wilson9da3da62012-06-01 15:20:22 +0100824static void intel_gtt_insert_pages(unsigned int first_entry,
825 unsigned int num_entries,
826 struct page **pages,
827 unsigned int flags)
Daniel Vetter40807752010-11-06 11:18:58 +0100828{
829 int i, j;
830
831 for (i = 0, j = first_entry; i < num_entries; i++, j++) {
832 dma_addr_t addr = page_to_phys(pages[i]);
833 intel_private.driver->write_entry(addr,
834 j, flags);
835 }
836 readl(intel_private.gtt+j-1);
837}
Daniel Vetterfefaa702010-09-11 22:12:11 +0200838
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200839static int intel_fake_agp_insert_entries(struct agp_memory *mem,
840 off_t pg_start, int type)
841{
Daniel Vetterf51b7662010-04-14 00:29:52 +0200842 int ret = -EINVAL;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200843
Chris Wilsonbee4a182011-01-21 10:54:32 +0000844 if (intel_private.clear_fake_agp) {
845 int start = intel_private.base.stolen_size / PAGE_SIZE;
846 int end = intel_private.base.gtt_mappable_entries;
847 intel_gtt_clear_range(start, end - start);
848 intel_private.clear_fake_agp = false;
849 }
850
Daniel Vetterff268602010-11-05 15:43:35 +0100851 if (INTEL_GTT_GEN == 1 && type == AGP_DCACHE_MEMORY)
852 return i810_insert_dcache_entries(mem, pg_start, type);
853
Daniel Vetterf51b7662010-04-14 00:29:52 +0200854 if (mem->page_count == 0)
855 goto out;
856
Chris Wilsonc64f7ba2010-11-23 14:24:24 +0000857 if (pg_start + mem->page_count > intel_private.base.gtt_total_entries)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200858 goto out_err;
859
Daniel Vetterf51b7662010-04-14 00:29:52 +0200860 if (type != mem->type)
861 goto out_err;
862
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200863 if (!intel_private.driver->check_flags(type))
Daniel Vetterf51b7662010-04-14 00:29:52 +0200864 goto out_err;
865
866 if (!mem->is_flushed)
867 global_cache_flush();
868
Daniel Vetter40807752010-11-06 11:18:58 +0100869 if (intel_private.base.needs_dmar) {
Chris Wilson9da3da62012-06-01 15:20:22 +0100870 struct sg_table st;
871
872 ret = intel_gtt_map_memory(mem->pages, mem->page_count, &st);
Daniel Vetterfefaa702010-09-11 22:12:11 +0200873 if (ret != 0)
874 return ret;
875
Chris Wilson9da3da62012-06-01 15:20:22 +0100876 intel_gtt_insert_sg_entries(&st, pg_start, type);
877 mem->sg_list = st.sgl;
878 mem->num_sg = st.nents;
Daniel Vetter40807752010-11-06 11:18:58 +0100879 } else
880 intel_gtt_insert_pages(pg_start, mem->page_count, mem->pages,
881 type);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200882
883out:
884 ret = 0;
885out_err:
886 mem->is_flushed = true;
887 return ret;
888}
889
Daniel Vetter40807752010-11-06 11:18:58 +0100890void intel_gtt_clear_range(unsigned int first_entry, unsigned int num_entries)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200891{
Daniel Vetter40807752010-11-06 11:18:58 +0100892 unsigned int i;
Daniel Vetterf51b7662010-04-14 00:29:52 +0200893
Daniel Vetter40807752010-11-06 11:18:58 +0100894 for (i = first_entry; i < (first_entry + num_entries); i++) {
Ben Widawsky9c61a322013-01-18 12:30:32 -0800895 intel_private.driver->write_entry(intel_private.scratch_page_dma,
Daniel Vetter5cbecaf2010-09-11 21:31:04 +0200896 i, 0);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200897 }
Daniel Vetterfdfb58a2010-08-29 00:15:03 +0200898 readl(intel_private.gtt+i-1);
Daniel Vetter40807752010-11-06 11:18:58 +0100899}
900EXPORT_SYMBOL(intel_gtt_clear_range);
901
902static int intel_fake_agp_remove_entries(struct agp_memory *mem,
903 off_t pg_start, int type)
904{
905 if (mem->page_count == 0)
906 return 0;
907
Dave Airlied15eda52011-01-12 11:39:48 +1000908 intel_gtt_clear_range(pg_start, mem->page_count);
909
Daniel Vetter40807752010-11-06 11:18:58 +0100910 if (intel_private.base.needs_dmar) {
911 intel_gtt_unmap_memory(mem->sg_list, mem->num_sg);
912 mem->sg_list = NULL;
913 mem->num_sg = 0;
914 }
915
Daniel Vetterf51b7662010-04-14 00:29:52 +0200916 return 0;
917}
918
Daniel Vetterffdd7512010-08-27 17:51:29 +0200919static struct agp_memory *intel_fake_agp_alloc_by_type(size_t pg_count,
920 int type)
Daniel Vetterf51b7662010-04-14 00:29:52 +0200921{
Daniel Vetter625dd9d2010-11-04 20:07:57 +0100922 struct agp_memory *new;
923
924 if (type == AGP_DCACHE_MEMORY && INTEL_GTT_GEN == 1) {
925 if (pg_count != intel_private.num_dcache_entries)
926 return NULL;
927
928 new = agp_create_memory(1);
929 if (new == NULL)
930 return NULL;
931
932 new->type = AGP_DCACHE_MEMORY;
933 new->page_count = pg_count;
934 new->num_scratch_pages = 0;
935 agp_free_page_array(new);
936 return new;
937 }
Daniel Vetterf51b7662010-04-14 00:29:52 +0200938 if (type == AGP_PHYS_MEMORY)
939 return alloc_agpphysmem_i8xx(pg_count, type);
940 /* always return NULL for other allocation types for now */
941 return NULL;
942}
943
944static int intel_alloc_chipset_flush_resource(void)
945{
946 int ret;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200947 ret = pci_bus_alloc_resource(intel_private.bridge_dev->bus, &intel_private.ifp_resource, PAGE_SIZE,
Daniel Vetterf51b7662010-04-14 00:29:52 +0200948 PAGE_SIZE, PCIBIOS_MIN_MEM, 0,
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200949 pcibios_align_resource, intel_private.bridge_dev);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200950
951 return ret;
952}
953
954static void intel_i915_setup_chipset_flush(void)
955{
956 int ret;
957 u32 temp;
958
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200959 pci_read_config_dword(intel_private.bridge_dev, I915_IFPADDR, &temp);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200960 if (!(temp & 0x1)) {
961 intel_alloc_chipset_flush_resource();
962 intel_private.resource_valid = 1;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200963 pci_write_config_dword(intel_private.bridge_dev, I915_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200964 } else {
965 temp &= ~1;
966
967 intel_private.resource_valid = 1;
968 intel_private.ifp_resource.start = temp;
969 intel_private.ifp_resource.end = temp + PAGE_SIZE;
970 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
971 /* some BIOSes reserve this area in a pnp some don't */
972 if (ret)
973 intel_private.resource_valid = 0;
974 }
975}
976
977static void intel_i965_g33_setup_chipset_flush(void)
978{
979 u32 temp_hi, temp_lo;
980 int ret;
981
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200982 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4, &temp_hi);
983 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR, &temp_lo);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200984
985 if (!(temp_lo & 0x1)) {
986
987 intel_alloc_chipset_flush_resource();
988
989 intel_private.resource_valid = 1;
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200990 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4,
Daniel Vetterf51b7662010-04-14 00:29:52 +0200991 upper_32_bits(intel_private.ifp_resource.start));
Daniel Vetterd7cca2f2010-08-24 23:06:19 +0200992 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
Daniel Vetterf51b7662010-04-14 00:29:52 +0200993 } else {
994 u64 l64;
995
996 temp_lo &= ~0x1;
997 l64 = ((u64)temp_hi << 32) | temp_lo;
998
999 intel_private.resource_valid = 1;
1000 intel_private.ifp_resource.start = l64;
1001 intel_private.ifp_resource.end = l64 + PAGE_SIZE;
1002 ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
1003 /* some BIOSes reserve this area in a pnp some don't */
1004 if (ret)
1005 intel_private.resource_valid = 0;
1006 }
1007}
1008
1009static void intel_i9xx_setup_flush(void)
1010{
1011 /* return if already configured */
1012 if (intel_private.ifp_resource.start)
1013 return;
1014
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001015 if (INTEL_GTT_GEN == 6)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001016 return;
1017
1018 /* setup a resource for this object */
1019 intel_private.ifp_resource.name = "Intel Flush Page";
1020 intel_private.ifp_resource.flags = IORESOURCE_MEM;
1021
1022 /* Setup chipset flush for 915 */
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001023 if (IS_G33 || INTEL_GTT_GEN >= 4) {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001024 intel_i965_g33_setup_chipset_flush();
1025 } else {
1026 intel_i915_setup_chipset_flush();
1027 }
1028
Chris Wilsondf51e7a2010-09-04 14:57:27 +01001029 if (intel_private.ifp_resource.start)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001030 intel_private.i9xx_flush_page = ioremap_nocache(intel_private.ifp_resource.start, PAGE_SIZE);
Chris Wilsondf51e7a2010-09-04 14:57:27 +01001031 if (!intel_private.i9xx_flush_page)
1032 dev_err(&intel_private.pcidev->dev,
1033 "can't ioremap flush page - no chipset flushing\n");
Daniel Vetterf51b7662010-04-14 00:29:52 +02001034}
1035
Daniel Vetterae83dd52010-09-12 17:11:15 +02001036static void i9xx_cleanup(void)
1037{
1038 if (intel_private.i9xx_flush_page)
1039 iounmap(intel_private.i9xx_flush_page);
1040 if (intel_private.resource_valid)
1041 release_resource(&intel_private.ifp_resource);
1042 intel_private.ifp_resource.start = 0;
1043 intel_private.resource_valid = 0;
1044}
1045
Daniel Vetter1b263f22010-09-12 00:27:24 +02001046static void i9xx_chipset_flush(void)
Daniel Vetterf51b7662010-04-14 00:29:52 +02001047{
1048 if (intel_private.i9xx_flush_page)
1049 writel(1, intel_private.i9xx_flush_page);
1050}
1051
Chris Wilson71f45662010-12-14 11:29:23 +00001052static void i965_write_entry(dma_addr_t addr,
1053 unsigned int entry,
Daniel Vettera6963592010-09-11 14:01:43 +02001054 unsigned int flags)
1055{
Chris Wilson71f45662010-12-14 11:29:23 +00001056 u32 pte_flags;
1057
1058 pte_flags = I810_PTE_VALID;
1059 if (flags == AGP_USER_CACHED_MEMORY)
1060 pte_flags |= I830_PTE_SYSTEM_CACHED;
1061
Daniel Vettera6963592010-09-11 14:01:43 +02001062 /* Shift high bits down */
1063 addr |= (addr >> 28) & 0xf0;
Chris Wilson71f45662010-12-14 11:29:23 +00001064 writel(addr | pte_flags, intel_private.gtt + entry);
Daniel Vettera6963592010-09-11 14:01:43 +02001065}
1066
Ben Widawsky5c042282011-10-17 15:51:55 -07001067
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001068static int i9xx_setup(void)
1069{
Ben Widawsky009946f2012-11-04 09:21:29 -08001070 u32 reg_addr, gtt_addr;
Jesse Barnes4b60d292012-03-28 13:39:33 -07001071 int size = KB(512);
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001072
1073 pci_read_config_dword(intel_private.pcidev, I915_MMADDR, &reg_addr);
1074
1075 reg_addr &= 0xfff80000;
1076
Jesse Barnes4b60d292012-03-28 13:39:33 -07001077 intel_private.registers = ioremap(reg_addr, size);
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001078 if (!intel_private.registers)
1079 return -ENOMEM;
1080
Ben Widawsky009946f2012-11-04 09:21:29 -08001081 switch (INTEL_GTT_GEN) {
1082 case 3:
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001083 pci_read_config_dword(intel_private.pcidev,
1084 I915_PTEADDR, &gtt_addr);
1085 intel_private.gtt_bus_addr = gtt_addr;
Ben Widawsky009946f2012-11-04 09:21:29 -08001086 break;
1087 case 5:
1088 intel_private.gtt_bus_addr = reg_addr + MB(2);
1089 break;
1090 default:
1091 intel_private.gtt_bus_addr = reg_addr + KB(512);
1092 break;
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001093 }
1094
1095 intel_i9xx_setup_flush();
1096
1097 return 0;
1098}
1099
Daniel Vettere9b1cc82010-09-12 00:29:26 +02001100static const struct agp_bridge_driver intel_fake_agp_driver = {
Daniel Vetterf51b7662010-04-14 00:29:52 +02001101 .owner = THIS_MODULE,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001102 .size_type = FIXED_APER_SIZE,
Chris Wilson9e76e7b2010-09-14 12:12:11 +01001103 .aperture_sizes = intel_fake_agp_sizes,
1104 .num_aperture_sizes = ARRAY_SIZE(intel_fake_agp_sizes),
Daniel Vettera6963592010-09-11 14:01:43 +02001105 .configure = intel_fake_agp_configure,
Daniel Vetter3e921f92010-08-27 15:33:26 +02001106 .fetch_size = intel_fake_agp_fetch_size,
Daniel Vetterfdfb58a2010-08-29 00:15:03 +02001107 .cleanup = intel_gtt_cleanup,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001108 .agp_enable = intel_fake_agp_enable,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001109 .cache_flush = global_cache_flush,
Daniel Vetter3b15a9d2010-08-29 14:18:49 +02001110 .create_gatt_table = intel_fake_agp_create_gatt_table,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001111 .free_gatt_table = intel_fake_agp_free_gatt_table,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001112 .insert_memory = intel_fake_agp_insert_entries,
1113 .remove_memory = intel_fake_agp_remove_entries,
Daniel Vetterffdd7512010-08-27 17:51:29 +02001114 .alloc_by_type = intel_fake_agp_alloc_by_type,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001115 .free_by_type = intel_i810_free_by_type,
1116 .agp_alloc_page = agp_generic_alloc_page,
1117 .agp_alloc_pages = agp_generic_alloc_pages,
1118 .agp_destroy_page = agp_generic_destroy_page,
1119 .agp_destroy_pages = agp_generic_destroy_pages,
Daniel Vetterf51b7662010-04-14 00:29:52 +02001120};
Daniel Vetter02c026c2010-08-24 19:39:48 +02001121
Daniel Vetterbdd30722010-09-12 12:34:44 +02001122static const struct intel_gtt_driver i81x_gtt_driver = {
1123 .gen = 1,
Daniel Vetter820647b2010-11-05 13:30:14 +01001124 .has_pgtbl_enable = 1,
Daniel Vetter22533b42010-09-12 16:38:55 +02001125 .dma_mask_size = 32,
Daniel Vetter820647b2010-11-05 13:30:14 +01001126 .setup = i810_setup,
1127 .cleanup = i810_cleanup,
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001128 .check_flags = i830_check_flags,
1129 .write_entry = i810_write_entry,
Daniel Vetterbdd30722010-09-12 12:34:44 +02001130};
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001131static const struct intel_gtt_driver i8xx_gtt_driver = {
1132 .gen = 2,
Chris Wilson100519e2010-10-31 10:37:02 +00001133 .has_pgtbl_enable = 1,
Daniel Vetter73800422010-08-29 17:29:50 +02001134 .setup = i830_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001135 .cleanup = i830_cleanup,
Daniel Vetter351bb272010-09-07 22:41:04 +02001136 .write_entry = i830_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001137 .dma_mask_size = 32,
Daniel Vetter5cbecaf2010-09-11 21:31:04 +02001138 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001139 .chipset_flush = i830_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001140};
1141static const struct intel_gtt_driver i915_gtt_driver = {
1142 .gen = 3,
Chris Wilson100519e2010-10-31 10:37:02 +00001143 .has_pgtbl_enable = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001144 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001145 .cleanup = i9xx_cleanup,
Daniel Vetter351bb272010-09-07 22:41:04 +02001146 /* i945 is the last gpu to need phys mem (for overlay and cursors). */
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001147 .write_entry = i830_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001148 .dma_mask_size = 32,
Daniel Vetterfefaa702010-09-11 22:12:11 +02001149 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001150 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001151};
1152static const struct intel_gtt_driver g33_gtt_driver = {
1153 .gen = 3,
1154 .is_g33 = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001155 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001156 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001157 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001158 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001159 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001160 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001161};
1162static const struct intel_gtt_driver pineview_gtt_driver = {
1163 .gen = 3,
1164 .is_pineview = 1, .is_g33 = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001165 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001166 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001167 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001168 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001169 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001170 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001171};
1172static const struct intel_gtt_driver i965_gtt_driver = {
1173 .gen = 4,
Chris Wilson100519e2010-10-31 10:37:02 +00001174 .has_pgtbl_enable = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001175 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001176 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001177 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001178 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001179 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001180 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001181};
1182static const struct intel_gtt_driver g4x_gtt_driver = {
1183 .gen = 5,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001184 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001185 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001186 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001187 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001188 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001189 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001190};
1191static const struct intel_gtt_driver ironlake_gtt_driver = {
1192 .gen = 5,
1193 .is_ironlake = 1,
Daniel Vetter2d2430c2010-08-29 17:35:30 +02001194 .setup = i9xx_setup,
Daniel Vetterae83dd52010-09-12 17:11:15 +02001195 .cleanup = i9xx_cleanup,
Daniel Vettera6963592010-09-11 14:01:43 +02001196 .write_entry = i965_write_entry,
Daniel Vetter22533b42010-09-12 16:38:55 +02001197 .dma_mask_size = 36,
Daniel Vetter450f2b32010-09-11 23:48:25 +02001198 .check_flags = i830_check_flags,
Daniel Vetter1b263f22010-09-12 00:27:24 +02001199 .chipset_flush = i9xx_chipset_flush,
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001200};
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001201
Daniel Vetter02c026c2010-08-24 19:39:48 +02001202/* Table to describe Intel GMCH and AGP/PCIE GART drivers. At least one of
1203 * driver and gmch_driver must be non-null, and find_gmch will determine
1204 * which one should be used if a gmch_chip_id is present.
1205 */
1206static const struct intel_gtt_driver_description {
1207 unsigned int gmch_chip_id;
1208 char *name;
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001209 const struct intel_gtt_driver *gtt_driver;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001210} intel_gtt_chipsets[] = {
Daniel Vetterff268602010-11-05 15:43:35 +01001211 { PCI_DEVICE_ID_INTEL_82810_IG1, "i810",
Daniel Vetterbdd30722010-09-12 12:34:44 +02001212 &i81x_gtt_driver},
Daniel Vetterff268602010-11-05 15:43:35 +01001213 { PCI_DEVICE_ID_INTEL_82810_IG3, "i810",
Daniel Vetterbdd30722010-09-12 12:34:44 +02001214 &i81x_gtt_driver},
Daniel Vetterff268602010-11-05 15:43:35 +01001215 { PCI_DEVICE_ID_INTEL_82810E_IG, "i810",
Daniel Vetterbdd30722010-09-12 12:34:44 +02001216 &i81x_gtt_driver},
Daniel Vetterff268602010-11-05 15:43:35 +01001217 { PCI_DEVICE_ID_INTEL_82815_CGC, "i815",
Daniel Vetterbdd30722010-09-12 12:34:44 +02001218 &i81x_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001219 { PCI_DEVICE_ID_INTEL_82830_CGC, "830M",
Daniel Vetterff268602010-11-05 15:43:35 +01001220 &i8xx_gtt_driver},
Oswald Buddenhagen53371ed2010-06-19 23:08:37 +02001221 { PCI_DEVICE_ID_INTEL_82845G_IG, "845G",
Daniel Vetterff268602010-11-05 15:43:35 +01001222 &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001223 { PCI_DEVICE_ID_INTEL_82854_IG, "854",
Daniel Vetterff268602010-11-05 15:43:35 +01001224 &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001225 { PCI_DEVICE_ID_INTEL_82855GM_IG, "855GM",
Daniel Vetterff268602010-11-05 15:43:35 +01001226 &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001227 { PCI_DEVICE_ID_INTEL_82865_IG, "865",
Daniel Vetterff268602010-11-05 15:43:35 +01001228 &i8xx_gtt_driver},
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001229 { PCI_DEVICE_ID_INTEL_E7221_IG, "E7221 (i915)",
Daniel Vetterff268602010-11-05 15:43:35 +01001230 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001231 { PCI_DEVICE_ID_INTEL_82915G_IG, "915G",
Daniel Vetterff268602010-11-05 15:43:35 +01001232 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001233 { PCI_DEVICE_ID_INTEL_82915GM_IG, "915GM",
Daniel Vetterff268602010-11-05 15:43:35 +01001234 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001235 { PCI_DEVICE_ID_INTEL_82945G_IG, "945G",
Daniel Vetterff268602010-11-05 15:43:35 +01001236 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001237 { PCI_DEVICE_ID_INTEL_82945GM_IG, "945GM",
Daniel Vetterff268602010-11-05 15:43:35 +01001238 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001239 { PCI_DEVICE_ID_INTEL_82945GME_IG, "945GME",
Daniel Vetterff268602010-11-05 15:43:35 +01001240 &i915_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001241 { PCI_DEVICE_ID_INTEL_82946GZ_IG, "946GZ",
Daniel Vetterff268602010-11-05 15:43:35 +01001242 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001243 { PCI_DEVICE_ID_INTEL_82G35_IG, "G35",
Daniel Vetterff268602010-11-05 15:43:35 +01001244 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001245 { PCI_DEVICE_ID_INTEL_82965Q_IG, "965Q",
Daniel Vetterff268602010-11-05 15:43:35 +01001246 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001247 { PCI_DEVICE_ID_INTEL_82965G_IG, "965G",
Daniel Vetterff268602010-11-05 15:43:35 +01001248 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001249 { PCI_DEVICE_ID_INTEL_82965GM_IG, "965GM",
Daniel Vetterff268602010-11-05 15:43:35 +01001250 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001251 { PCI_DEVICE_ID_INTEL_82965GME_IG, "965GME/GLE",
Daniel Vetterff268602010-11-05 15:43:35 +01001252 &i965_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001253 { PCI_DEVICE_ID_INTEL_G33_IG, "G33",
Daniel Vetterff268602010-11-05 15:43:35 +01001254 &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001255 { PCI_DEVICE_ID_INTEL_Q35_IG, "Q35",
Daniel Vetterff268602010-11-05 15:43:35 +01001256 &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001257 { PCI_DEVICE_ID_INTEL_Q33_IG, "Q33",
Daniel Vetterff268602010-11-05 15:43:35 +01001258 &g33_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001259 { PCI_DEVICE_ID_INTEL_PINEVIEW_M_IG, "GMA3150",
Daniel Vetterff268602010-11-05 15:43:35 +01001260 &pineview_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001261 { PCI_DEVICE_ID_INTEL_PINEVIEW_IG, "GMA3150",
Daniel Vetterff268602010-11-05 15:43:35 +01001262 &pineview_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001263 { PCI_DEVICE_ID_INTEL_GM45_IG, "GM45",
Daniel Vetterff268602010-11-05 15:43:35 +01001264 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001265 { PCI_DEVICE_ID_INTEL_EAGLELAKE_IG, "Eaglelake",
Daniel Vetterff268602010-11-05 15:43:35 +01001266 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001267 { PCI_DEVICE_ID_INTEL_Q45_IG, "Q45/Q43",
Daniel Vetterff268602010-11-05 15:43:35 +01001268 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001269 { PCI_DEVICE_ID_INTEL_G45_IG, "G45/G43",
Daniel Vetterff268602010-11-05 15:43:35 +01001270 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001271 { PCI_DEVICE_ID_INTEL_B43_IG, "B43",
Daniel Vetterff268602010-11-05 15:43:35 +01001272 &g4x_gtt_driver },
Chris Wilsone9e5f8e2010-09-21 11:19:32 +01001273 { PCI_DEVICE_ID_INTEL_B43_1_IG, "B43",
Daniel Vetterff268602010-11-05 15:43:35 +01001274 &g4x_gtt_driver },
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001275 { PCI_DEVICE_ID_INTEL_G41_IG, "G41",
Daniel Vetterff268602010-11-05 15:43:35 +01001276 &g4x_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001277 { PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001278 "HD Graphics", &ironlake_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001279 { PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG,
Daniel Vetterff268602010-11-05 15:43:35 +01001280 "HD Graphics", &ironlake_gtt_driver },
Daniel Vetter02c026c2010-08-24 19:39:48 +02001281 { 0, NULL, NULL }
1282};
1283
1284static int find_gmch(u16 device)
1285{
1286 struct pci_dev *gmch_device;
1287
1288 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL, device, NULL);
1289 if (gmch_device && PCI_FUNC(gmch_device->devfn) != 0) {
1290 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL,
1291 device, gmch_device);
1292 }
1293
1294 if (!gmch_device)
1295 return 0;
1296
1297 intel_private.pcidev = gmch_device;
1298 return 1;
1299}
1300
Daniel Vetter14be93d2012-06-08 15:55:40 +02001301int intel_gmch_probe(struct pci_dev *bridge_pdev, struct pci_dev *gpu_pdev,
1302 struct agp_bridge_data *bridge)
Daniel Vetter02c026c2010-08-24 19:39:48 +02001303{
1304 int i, mask;
Daniel Vetter14be93d2012-06-08 15:55:40 +02001305
1306 /*
1307 * Can be called from the fake agp driver but also directly from
1308 * drm/i915.ko. Hence we need to check whether everything is set up
1309 * already.
1310 */
1311 if (intel_private.driver) {
1312 intel_private.refcount++;
1313 return 1;
1314 }
Daniel Vetter02c026c2010-08-24 19:39:48 +02001315
1316 for (i = 0; intel_gtt_chipsets[i].name != NULL; i++) {
Daniel Vetter14be93d2012-06-08 15:55:40 +02001317 if (gpu_pdev) {
1318 if (gpu_pdev->device ==
1319 intel_gtt_chipsets[i].gmch_chip_id) {
1320 intel_private.pcidev = pci_dev_get(gpu_pdev);
1321 intel_private.driver =
1322 intel_gtt_chipsets[i].gtt_driver;
1323
1324 break;
1325 }
1326 } else if (find_gmch(intel_gtt_chipsets[i].gmch_chip_id)) {
Daniel Vetter625dd9d2010-11-04 20:07:57 +01001327 intel_private.driver =
Daniel Vetter1a997ff2010-09-08 21:18:53 +02001328 intel_gtt_chipsets[i].gtt_driver;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001329 break;
1330 }
1331 }
1332
Daniel Vetterff268602010-11-05 15:43:35 +01001333 if (!intel_private.driver)
Daniel Vetter02c026c2010-08-24 19:39:48 +02001334 return 0;
1335
Daniel Vetter14be93d2012-06-08 15:55:40 +02001336 intel_private.refcount++;
1337
Daniel Vetter7e8f6302012-06-07 15:55:58 +02001338 if (bridge) {
1339 bridge->driver = &intel_fake_agp_driver;
1340 bridge->dev_private_data = &intel_private;
Daniel Vetter14be93d2012-06-08 15:55:40 +02001341 bridge->dev = bridge_pdev;
Daniel Vetter7e8f6302012-06-07 15:55:58 +02001342 }
Daniel Vetter02c026c2010-08-24 19:39:48 +02001343
Daniel Vetter14be93d2012-06-08 15:55:40 +02001344 intel_private.bridge_dev = pci_dev_get(bridge_pdev);
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001345
Daniel Vetter14be93d2012-06-08 15:55:40 +02001346 dev_info(&bridge_pdev->dev, "Intel %s Chipset\n", intel_gtt_chipsets[i].name);
Daniel Vetter02c026c2010-08-24 19:39:48 +02001347
Daniel Vetter22533b42010-09-12 16:38:55 +02001348 mask = intel_private.driver->dma_mask_size;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001349 if (pci_set_dma_mask(intel_private.pcidev, DMA_BIT_MASK(mask)))
1350 dev_err(&intel_private.pcidev->dev,
1351 "set gfx device dma mask %d-bit failed!\n", mask);
1352 else
1353 pci_set_consistent_dma_mask(intel_private.pcidev,
1354 DMA_BIT_MASK(mask));
1355
Daniel Vetter14be93d2012-06-08 15:55:40 +02001356 if (intel_gtt_init() != 0) {
1357 intel_gmch_remove();
1358
Daniel Vetter3b15a9d2010-08-29 14:18:49 +02001359 return 0;
Daniel Vetter14be93d2012-06-08 15:55:40 +02001360 }
Daniel Vetter1784a5f2010-09-08 21:01:04 +02001361
Daniel Vetter02c026c2010-08-24 19:39:48 +02001362 return 1;
1363}
Daniel Vettere2404e72010-09-08 17:29:51 +02001364EXPORT_SYMBOL(intel_gmch_probe);
Daniel Vetter02c026c2010-08-24 19:39:48 +02001365
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001366struct intel_gtt *intel_gtt_get(void)
Daniel Vetter19966752010-09-06 20:08:44 +02001367{
1368 return &intel_private.base;
1369}
1370EXPORT_SYMBOL(intel_gtt_get);
1371
Daniel Vetter40ce6572010-11-05 18:12:18 +01001372void intel_gtt_chipset_flush(void)
1373{
1374 if (intel_private.driver->chipset_flush)
1375 intel_private.driver->chipset_flush();
1376}
1377EXPORT_SYMBOL(intel_gtt_chipset_flush);
1378
Daniel Vetter14be93d2012-06-08 15:55:40 +02001379void intel_gmch_remove(void)
Daniel Vetter02c026c2010-08-24 19:39:48 +02001380{
Daniel Vetter14be93d2012-06-08 15:55:40 +02001381 if (--intel_private.refcount)
1382 return;
1383
Daniel Vetter02c026c2010-08-24 19:39:48 +02001384 if (intel_private.pcidev)
1385 pci_dev_put(intel_private.pcidev);
Daniel Vetterd7cca2f2010-08-24 23:06:19 +02001386 if (intel_private.bridge_dev)
1387 pci_dev_put(intel_private.bridge_dev);
Daniel Vetter14be93d2012-06-08 15:55:40 +02001388 intel_private.driver = NULL;
Daniel Vetter02c026c2010-08-24 19:39:48 +02001389}
Daniel Vettere2404e72010-09-08 17:29:51 +02001390EXPORT_SYMBOL(intel_gmch_remove);
1391
1392MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
1393MODULE_LICENSE("GPL and additional rights");