Subversion Repositories Kolibri OS

Rev

Rev 5060 | Rev 6084 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5060 Rev 5354
1
/*
1
/*
2
 * Copyright © 2010 Daniel Vetter
2
 * Copyright © 2010 Daniel Vetter
3
 * Copyright © 2011-2014 Intel Corporation
3
 * Copyright © 2011-2014 Intel Corporation
4
 *
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
11
 *
12
 * The above copyright notice and this permission notice (including the next
12
 * The above copyright notice and this permission notice (including the next
13
 * paragraph) shall be included in all copies or substantial portions of the
13
 * paragraph) shall be included in all copies or substantial portions of the
14
 * Software.
14
 * Software.
15
 *
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
 * IN THE SOFTWARE.
22
 * IN THE SOFTWARE.
23
 *
23
 *
24
 */
24
 */
25
 
-
 
26
 
25
 
27
#define AGP_NORMAL_MEMORY 0
-
 
28
 
-
 
29
#define AGP_USER_TYPES (1 << 16)
-
 
30
#define AGP_USER_MEMORY (AGP_USER_TYPES)
-
 
31
#define AGP_USER_CACHED_MEMORY (AGP_USER_TYPES + 1)
-
 
32
 
26
#include 
33
#include 
27
#include 
34
#include 
28
#include 
35
#include "i915_drv.h"
29
#include "i915_drv.h"
36
#include "i915_trace.h"
30
#include "i915_trace.h"
37
#include "intel_drv.h"
31
#include "intel_drv.h"
-
 
32
 
-
 
33
#include 
38
 
34
 
39
static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv);
35
static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv);
40
static void chv_setup_private_ppat(struct drm_i915_private *dev_priv);
36
static void chv_setup_private_ppat(struct drm_i915_private *dev_priv);
41
 
37
 
42
bool intel_enable_ppgtt(struct drm_device *dev, bool full)
38
static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
43
{
39
{
44
	if (i915.enable_ppgtt == 0)
40
	bool has_aliasing_ppgtt;
45
		return false;
41
	bool has_full_ppgtt;
-
 
42
 
46
 
43
	has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
-
 
44
	has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
47
	if (i915.enable_ppgtt == 1 && full)
-
 
48
		return false;
45
	if (IS_GEN8(dev))
49
 
-
 
50
	return true;
46
		has_full_ppgtt = false; /* XXX why? */
-
 
47
 
51
}
48
	/*
-
 
49
	 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
52
 
50
	 * execlists, the sole mechanism available to submit work.
53
static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
51
	 */
54
{
52
	if (INTEL_INFO(dev)->gen < 9 &&
55
	if (enable_ppgtt == 0 || !HAS_ALIASING_PPGTT(dev))
53
	    (enable_ppgtt == 0 || !has_aliasing_ppgtt))
56
		return 0;
54
		return 0;
57
 
55
 
58
	if (enable_ppgtt == 1)
56
	if (enable_ppgtt == 1)
59
		return 1;
57
		return 1;
60
 
58
 
61
	if (enable_ppgtt == 2 && HAS_PPGTT(dev))
59
	if (enable_ppgtt == 2 && has_full_ppgtt)
62
		return 2;
60
		return 2;
63
 
61
 
64
#ifdef CONFIG_INTEL_IOMMU
62
#ifdef CONFIG_INTEL_IOMMU
65
	/* Disable ppgtt on SNB if VT-d is on. */
63
	/* Disable ppgtt on SNB if VT-d is on. */
66
	if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
64
	if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
67
		DRM_INFO("Disabling PPGTT because VT-d is on\n");
65
		DRM_INFO("Disabling PPGTT because VT-d is on\n");
68
		return 0;
66
		return 0;
69
	}
67
	}
70
#endif
68
#endif
71
 
69
 
72
	/* Early VLV doesn't have this */
70
	/* Early VLV doesn't have this */
73
	if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
71
	if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
74
	    dev->pdev->revision < 0xb) {
72
	    dev->pdev->revision < 0xb) {
75
		DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
73
		DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
76
		return 0;
74
		return 0;
77
	}
75
	}
78
 
76
 
79
	return HAS_ALIASING_PPGTT(dev) ? 1 : 0;
77
	return has_aliasing_ppgtt ? 1 : 0;
80
}
78
}
81
 
79
 
82
 
80
 
83
static void ppgtt_bind_vma(struct i915_vma *vma,
81
static void ppgtt_bind_vma(struct i915_vma *vma,
84
			   enum i915_cache_level cache_level,
82
			   enum i915_cache_level cache_level,
85
			   u32 flags);
83
			   u32 flags);
86
static void ppgtt_unbind_vma(struct i915_vma *vma);
84
static void ppgtt_unbind_vma(struct i915_vma *vma);
87
static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt);
-
 
88
 
85
 
89
static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
86
static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
90
					     enum i915_cache_level level,
87
					     enum i915_cache_level level,
91
					     bool valid)
88
					     bool valid)
92
{
89
{
93
	gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
90
	gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
94
	pte |= addr;
91
	pte |= addr;
95
 
92
 
96
	switch (level) {
93
	switch (level) {
97
	case I915_CACHE_NONE:
94
	case I915_CACHE_NONE:
98
		pte |= PPAT_UNCACHED_INDEX;
95
		pte |= PPAT_UNCACHED_INDEX;
99
		break;
96
		break;
100
	case I915_CACHE_WT:
97
	case I915_CACHE_WT:
101
		pte |= PPAT_DISPLAY_ELLC_INDEX;
98
		pte |= PPAT_DISPLAY_ELLC_INDEX;
102
		break;
99
		break;
103
	default:
100
	default:
104
		pte |= PPAT_CACHED_INDEX;
101
		pte |= PPAT_CACHED_INDEX;
105
		break;
102
		break;
106
	}
103
	}
107
 
104
 
108
	return pte;
105
	return pte;
109
}
106
}
110
 
107
 
111
static inline gen8_ppgtt_pde_t gen8_pde_encode(struct drm_device *dev,
108
static inline gen8_ppgtt_pde_t gen8_pde_encode(struct drm_device *dev,
112
					     dma_addr_t addr,
109
					     dma_addr_t addr,
113
					     enum i915_cache_level level)
110
					     enum i915_cache_level level)
114
{
111
{
115
	gen8_ppgtt_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
112
	gen8_ppgtt_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
116
	pde |= addr;
113
	pde |= addr;
117
	if (level != I915_CACHE_NONE)
114
	if (level != I915_CACHE_NONE)
118
		pde |= PPAT_CACHED_PDE_INDEX;
115
		pde |= PPAT_CACHED_PDE_INDEX;
119
	else
116
	else
120
		pde |= PPAT_UNCACHED_INDEX;
117
		pde |= PPAT_UNCACHED_INDEX;
121
	return pde;
118
	return pde;
122
}
119
}
123
 
120
 
124
static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
121
static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
125
				     enum i915_cache_level level,
122
				     enum i915_cache_level level,
126
				     bool valid, u32 unused)
123
				     bool valid, u32 unused)
127
{
124
{
128
	gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
125
	gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
129
	pte |= GEN6_PTE_ADDR_ENCODE(addr);
126
	pte |= GEN6_PTE_ADDR_ENCODE(addr);
130
 
127
 
131
	switch (level) {
128
	switch (level) {
132
	case I915_CACHE_L3_LLC:
129
	case I915_CACHE_L3_LLC:
133
	case I915_CACHE_LLC:
130
	case I915_CACHE_LLC:
134
		pte |= GEN6_PTE_CACHE_LLC;
131
		pte |= GEN6_PTE_CACHE_LLC;
135
		break;
132
		break;
136
	case I915_CACHE_NONE:
133
	case I915_CACHE_NONE:
137
		pte |= GEN6_PTE_UNCACHED;
134
		pte |= GEN6_PTE_UNCACHED;
138
		break;
135
		break;
139
	default:
136
	default:
140
		WARN_ON(1);
137
		WARN_ON(1);
141
	}
138
	}
142
 
139
 
143
	return pte;
140
	return pte;
144
}
141
}
145
 
142
 
146
static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
143
static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
147
				     enum i915_cache_level level,
144
				     enum i915_cache_level level,
148
				     bool valid, u32 unused)
145
				     bool valid, u32 unused)
149
{
146
{
150
	gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
147
	gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
151
	pte |= GEN6_PTE_ADDR_ENCODE(addr);
148
	pte |= GEN6_PTE_ADDR_ENCODE(addr);
152
 
149
 
153
	switch (level) {
150
	switch (level) {
154
	case I915_CACHE_L3_LLC:
151
	case I915_CACHE_L3_LLC:
155
		pte |= GEN7_PTE_CACHE_L3_LLC;
152
		pte |= GEN7_PTE_CACHE_L3_LLC;
156
		break;
153
		break;
157
	case I915_CACHE_LLC:
154
	case I915_CACHE_LLC:
158
		pte |= GEN6_PTE_CACHE_LLC;
155
		pte |= GEN6_PTE_CACHE_LLC;
159
		break;
156
		break;
160
	case I915_CACHE_NONE:
157
	case I915_CACHE_NONE:
161
			pte |= GEN6_PTE_UNCACHED;
158
			pte |= GEN6_PTE_UNCACHED;
162
		break;
159
		break;
163
	default:
160
	default:
164
		WARN_ON(1);
161
		WARN_ON(1);
165
	}
162
	}
166
 
163
 
167
	return pte;
164
	return pte;
168
}
165
}
169
 
166
 
170
static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
167
static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
171
				     enum i915_cache_level level,
168
				     enum i915_cache_level level,
172
				     bool valid, u32 flags)
169
				     bool valid, u32 flags)
173
{
170
{
174
	gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
171
	gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
175
	pte |= GEN6_PTE_ADDR_ENCODE(addr);
172
	pte |= GEN6_PTE_ADDR_ENCODE(addr);
176
 
-
 
177
	/* Mark the page as writeable.  Other platforms don't have a
-
 
178
	 * setting for read-only/writable, so this matches that behavior.
-
 
179
	 */
173
 
180
	if (!(flags & PTE_READ_ONLY))
174
	if (!(flags & PTE_READ_ONLY))
181
	pte |= BYT_PTE_WRITEABLE;
175
	pte |= BYT_PTE_WRITEABLE;
182
 
176
 
183
	if (level != I915_CACHE_NONE)
177
	if (level != I915_CACHE_NONE)
184
		pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
178
		pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
185
 
179
 
186
	return pte;
180
	return pte;
187
}
181
}
188
 
182
 
189
static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
183
static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
190
				     enum i915_cache_level level,
184
				     enum i915_cache_level level,
191
				     bool valid, u32 unused)
185
				     bool valid, u32 unused)
192
{
186
{
193
	gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
187
	gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
194
	pte |= HSW_PTE_ADDR_ENCODE(addr);
188
	pte |= HSW_PTE_ADDR_ENCODE(addr);
195
 
189
 
196
	if (level != I915_CACHE_NONE)
190
	if (level != I915_CACHE_NONE)
197
		pte |= HSW_WB_LLC_AGE3;
191
		pte |= HSW_WB_LLC_AGE3;
198
 
192
 
199
	return pte;
193
	return pte;
200
}
194
}
201
 
195
 
202
static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
196
static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
203
				      enum i915_cache_level level,
197
				      enum i915_cache_level level,
204
				      bool valid, u32 unused)
198
				      bool valid, u32 unused)
205
{
199
{
206
	gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
200
	gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
207
	pte |= HSW_PTE_ADDR_ENCODE(addr);
201
	pte |= HSW_PTE_ADDR_ENCODE(addr);
208
 
202
 
209
	switch (level) {
203
	switch (level) {
210
	case I915_CACHE_NONE:
204
	case I915_CACHE_NONE:
211
		break;
205
		break;
212
	case I915_CACHE_WT:
206
	case I915_CACHE_WT:
213
		pte |= HSW_WT_ELLC_LLC_AGE3;
207
		pte |= HSW_WT_ELLC_LLC_AGE3;
214
		break;
208
		break;
215
	default:
209
	default:
216
		pte |= HSW_WB_ELLC_LLC_AGE3;
210
		pte |= HSW_WB_ELLC_LLC_AGE3;
217
		break;
211
		break;
218
	}
212
	}
219
 
213
 
220
	return pte;
214
	return pte;
221
}
215
}
222
 
216
 
223
/* Broadwell Page Directory Pointer Descriptors */
217
/* Broadwell Page Directory Pointer Descriptors */
224
static int gen8_write_pdp(struct intel_engine_cs *ring, unsigned entry,
218
static int gen8_write_pdp(struct intel_engine_cs *ring, unsigned entry,
225
			   uint64_t val, bool synchronous)
219
			   uint64_t val)
226
{
220
{
227
	struct drm_i915_private *dev_priv = ring->dev->dev_private;
-
 
228
	int ret;
221
	int ret;
229
 
222
 
230
	BUG_ON(entry >= 4);
223
	BUG_ON(entry >= 4);
231
 
-
 
232
	if (synchronous) {
-
 
233
		I915_WRITE(GEN8_RING_PDP_UDW(ring, entry), val >> 32);
-
 
234
		I915_WRITE(GEN8_RING_PDP_LDW(ring, entry), (u32)val);
-
 
235
		return 0;
-
 
236
	}
-
 
237
 
224
 
238
	ret = intel_ring_begin(ring, 6);
225
	ret = intel_ring_begin(ring, 6);
239
	if (ret)
226
	if (ret)
240
		return ret;
227
		return ret;
241
 
228
 
242
	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
229
	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
243
	intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
230
	intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
244
	intel_ring_emit(ring, (u32)(val >> 32));
231
	intel_ring_emit(ring, (u32)(val >> 32));
245
	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
232
	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
246
	intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
233
	intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
247
	intel_ring_emit(ring, (u32)(val));
234
	intel_ring_emit(ring, (u32)(val));
248
	intel_ring_advance(ring);
235
	intel_ring_advance(ring);
249
 
236
 
250
	return 0;
237
	return 0;
251
}
238
}
252
 
239
 
253
static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
240
static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
254
			  struct intel_engine_cs *ring,
241
			  struct intel_engine_cs *ring)
255
			  bool synchronous)
-
 
256
{
242
{
257
	int i, ret;
243
	int i, ret;
258
 
244
 
259
	/* bit of a hack to find the actual last used pd */
245
	/* bit of a hack to find the actual last used pd */
260
	int used_pd = ppgtt->num_pd_entries / GEN8_PDES_PER_PAGE;
246
	int used_pd = ppgtt->num_pd_entries / GEN8_PDES_PER_PAGE;
261
 
247
 
262
	for (i = used_pd - 1; i >= 0; i--) {
248
	for (i = used_pd - 1; i >= 0; i--) {
263
		dma_addr_t addr = ppgtt->pd_dma_addr[i];
249
		dma_addr_t addr = ppgtt->pd_dma_addr[i];
264
		ret = gen8_write_pdp(ring, i, addr, synchronous);
250
		ret = gen8_write_pdp(ring, i, addr);
265
			if (ret)
251
			if (ret)
266
			return ret;
252
			return ret;
267
	}
253
	}
268
 
254
 
269
	return 0;
255
	return 0;
270
}
256
}
271
 
257
 
272
static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
258
static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
273
				   uint64_t start,
259
				   uint64_t start,
274
				   uint64_t length,
260
				   uint64_t length,
275
				   bool use_scratch)
261
				   bool use_scratch)
276
{
262
{
277
	struct i915_hw_ppgtt *ppgtt =
263
	struct i915_hw_ppgtt *ppgtt =
278
		container_of(vm, struct i915_hw_ppgtt, base);
264
		container_of(vm, struct i915_hw_ppgtt, base);
279
	gen8_gtt_pte_t *pt_vaddr, scratch_pte;
265
	gen8_gtt_pte_t *pt_vaddr, scratch_pte;
280
	unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
266
	unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
281
	unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
267
	unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
282
	unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
268
	unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
283
	unsigned num_entries = length >> PAGE_SHIFT;
269
	unsigned num_entries = length >> PAGE_SHIFT;
284
	unsigned last_pte, i;
270
	unsigned last_pte, i;
285
 
-
 
286
    pt_vaddr = (gen8_gtt_pte_t*)AllocKernelSpace(4096);
-
 
287
    if(pt_vaddr == NULL)
-
 
288
        return;
-
 
289
 
271
 
290
    scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
272
    scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
291
                      I915_CACHE_LLC, use_scratch);
273
                      I915_CACHE_LLC, use_scratch);
292
 
274
 
293
	while (num_entries) {
275
	while (num_entries) {
294
		struct page *page_table = ppgtt->gen8_pt_pages[pdpe][pde];
276
		struct page *page_table = ppgtt->gen8_pt_pages[pdpe][pde];
295
 
277
 
296
		last_pte = pte + num_entries;
278
		last_pte = pte + num_entries;
297
		if (last_pte > GEN8_PTES_PER_PAGE)
279
		if (last_pte > GEN8_PTES_PER_PAGE)
298
			last_pte = GEN8_PTES_PER_PAGE;
280
			last_pte = GEN8_PTES_PER_PAGE;
299
 
281
 
300
        MapPage(pt_vaddr,(addr_t)page_table, PG_SW);
282
		pt_vaddr = kmap_atomic(page_table);
301
 
283
 
302
		for (i = pte; i < last_pte; i++) {
284
		for (i = pte; i < last_pte; i++) {
303
			pt_vaddr[i] = scratch_pte;
285
			pt_vaddr[i] = scratch_pte;
304
			num_entries--;
286
			num_entries--;
305
		}
287
		}
306
 
288
 
307
		if (!HAS_LLC(ppgtt->base.dev))
289
		if (!HAS_LLC(ppgtt->base.dev))
308
			drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
290
			drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
-
 
291
		kunmap_atomic(pt_vaddr);
309
 
292
 
310
		pte = 0;
293
		pte = 0;
311
		if (++pde == GEN8_PDES_PER_PAGE) {
294
		if (++pde == GEN8_PDES_PER_PAGE) {
312
			pdpe++;
295
			pdpe++;
313
			pde = 0;
296
			pde = 0;
314
		}
297
		}
315
	}
298
	}
316
    FreeKernelSpace(pt_vaddr);
-
 
317
}
299
}
318
 
300
 
319
static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
301
static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
320
				      struct sg_table *pages,
302
				      struct sg_table *pages,
321
				      uint64_t start,
303
				      uint64_t start,
322
				      enum i915_cache_level cache_level, u32 unused)
304
				      enum i915_cache_level cache_level, u32 unused)
323
{
305
{
324
	struct i915_hw_ppgtt *ppgtt =
306
	struct i915_hw_ppgtt *ppgtt =
325
		container_of(vm, struct i915_hw_ppgtt, base);
307
		container_of(vm, struct i915_hw_ppgtt, base);
326
	gen8_gtt_pte_t *pt_vaddr;
308
	gen8_gtt_pte_t *pt_vaddr;
327
	unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
309
	unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
328
	unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
310
	unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
329
	unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
311
	unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
330
	struct sg_page_iter sg_iter;
312
	struct sg_page_iter sg_iter;
331
 
-
 
332
    pt_vaddr = AllocKernelSpace(4096);
313
 
333
    if(pt_vaddr == NULL)
-
 
334
        return;
-
 
335
 
-
 
336
    MapPage(pt_vaddr,(addr_t)(ppgtt->gen8_pt_pages[pdpe][pde]), 3);
314
	pt_vaddr = NULL;
337
 
315
 
338
	for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
316
	for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
339
		if (WARN_ON(pdpe >= GEN8_LEGACY_PDPS))
317
		if (WARN_ON(pdpe >= GEN8_LEGACY_PDPS))
340
			break;
318
			break;
-
 
319
 
-
 
320
		if (pt_vaddr == NULL)
-
 
321
			pt_vaddr = kmap_atomic(ppgtt->gen8_pt_pages[pdpe][pde]);
341
 
322
 
342
		pt_vaddr[pte] =
323
		pt_vaddr[pte] =
343
			gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
324
			gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
344
					cache_level, true);
325
					cache_level, true);
345
		if (++pte == GEN8_PTES_PER_PAGE) {
326
		if (++pte == GEN8_PTES_PER_PAGE) {
346
			if (!HAS_LLC(ppgtt->base.dev))
327
			if (!HAS_LLC(ppgtt->base.dev))
347
				drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
328
				drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
-
 
329
			kunmap_atomic(pt_vaddr);
-
 
330
			pt_vaddr = NULL;
348
			if (++pde == GEN8_PDES_PER_PAGE) {
331
			if (++pde == GEN8_PDES_PER_PAGE) {
349
				pdpe++;
332
				pdpe++;
350
				pde = 0;
333
				pde = 0;
351
			}
334
			}
352
			pte = 0;
335
			pte = 0;
353
            MapPage(pt_vaddr,(addr_t)(ppgtt->gen8_pt_pages[pdpe][pde]), 3);
-
 
354
		}
336
		}
355
	}
337
	}
-
 
338
	if (pt_vaddr) {
-
 
339
		if (!HAS_LLC(ppgtt->base.dev))
-
 
340
			drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
356
    FreeKernelSpace(pt_vaddr);
341
		kunmap_atomic(pt_vaddr);
-
 
342
	}
357
}
343
}
358
 
344
 
359
static void gen8_free_page_tables(struct page **pt_pages)
345
static void gen8_free_page_tables(struct page **pt_pages)
360
{
346
{
361
	int i;
347
	int i;
362
 
348
 
363
	if (pt_pages == NULL)
349
	if (pt_pages == NULL)
364
		return;
350
		return;
365
 
351
 
366
//   for (i = 0; i < GEN8_PDES_PER_PAGE; i++)
352
//   for (i = 0; i < GEN8_PDES_PER_PAGE; i++)
367
//       if (pt_pages[i])
353
//       if (pt_pages[i])
368
//           __free_pages(pt_pages[i], 0);
354
//           __free_pages(pt_pages[i], 0);
369
}
355
}
370
 
356
 
371
static void gen8_ppgtt_free(const struct i915_hw_ppgtt *ppgtt)
357
static void gen8_ppgtt_free(const struct i915_hw_ppgtt *ppgtt)
372
{
358
{
373
	int i;
359
	int i;
374
 
360
 
375
	for (i = 0; i < ppgtt->num_pd_pages; i++) {
361
	for (i = 0; i < ppgtt->num_pd_pages; i++) {
376
		gen8_free_page_tables(ppgtt->gen8_pt_pages[i]);
362
		gen8_free_page_tables(ppgtt->gen8_pt_pages[i]);
377
		kfree(ppgtt->gen8_pt_pages[i]);
363
		kfree(ppgtt->gen8_pt_pages[i]);
378
		kfree(ppgtt->gen8_pt_dma_addr[i]);
364
		kfree(ppgtt->gen8_pt_dma_addr[i]);
379
	}
365
	}
380
 
366
 
381
//	__free_pages(ppgtt->pd_pages, get_order(ppgtt->num_pd_pages << PAGE_SHIFT));
367
//	__free_pages(ppgtt->pd_pages, get_order(ppgtt->num_pd_pages << PAGE_SHIFT));
382
}
368
}
383
 
369
 
384
static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
370
static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
385
{
371
{
386
	struct pci_dev *hwdev = ppgtt->base.dev->pdev;
372
	struct pci_dev *hwdev = ppgtt->base.dev->pdev;
387
	int i, j;
373
	int i, j;
388
 
374
 
389
	for (i = 0; i < ppgtt->num_pd_pages; i++) {
375
	for (i = 0; i < ppgtt->num_pd_pages; i++) {
390
		/* TODO: In the future we'll support sparse mappings, so this
376
		/* TODO: In the future we'll support sparse mappings, so this
391
		 * will have to change. */
377
		 * will have to change. */
392
		if (!ppgtt->pd_dma_addr[i])
378
		if (!ppgtt->pd_dma_addr[i])
393
			continue;
379
			continue;
394
 
380
 
395
		pci_unmap_page(hwdev, ppgtt->pd_dma_addr[i], PAGE_SIZE,
381
		pci_unmap_page(hwdev, ppgtt->pd_dma_addr[i], PAGE_SIZE,
396
			       PCI_DMA_BIDIRECTIONAL);
382
			       PCI_DMA_BIDIRECTIONAL);
397
 
383
 
398
			for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
384
			for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
399
				dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
385
				dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
400
				if (addr)
386
				if (addr)
401
				pci_unmap_page(hwdev, addr, PAGE_SIZE,
387
				pci_unmap_page(hwdev, addr, PAGE_SIZE,
402
						       PCI_DMA_BIDIRECTIONAL);
388
						       PCI_DMA_BIDIRECTIONAL);
403
		}
389
		}
404
	}
390
	}
405
}
391
}
406
 
392
 
407
static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
393
static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
408
{
394
{
409
	struct i915_hw_ppgtt *ppgtt =
395
	struct i915_hw_ppgtt *ppgtt =
410
		container_of(vm, struct i915_hw_ppgtt, base);
396
		container_of(vm, struct i915_hw_ppgtt, base);
411
 
-
 
412
	list_del(&vm->global_link);
-
 
413
	drm_mm_takedown(&vm->mm);
-
 
414
 
397
 
415
	gen8_ppgtt_unmap_pages(ppgtt);
398
	gen8_ppgtt_unmap_pages(ppgtt);
416
	gen8_ppgtt_free(ppgtt);
399
	gen8_ppgtt_free(ppgtt);
417
}
400
}
418
 
401
 
419
static struct page **__gen8_alloc_page_tables(void)
402
static struct page **__gen8_alloc_page_tables(void)
420
{
403
{
421
	struct page **pt_pages;
404
	struct page **pt_pages;
422
	int i;
405
	int i;
423
 
406
 
424
	pt_pages = kcalloc(GEN8_PDES_PER_PAGE, sizeof(struct page *), GFP_KERNEL);
407
	pt_pages = kcalloc(GEN8_PDES_PER_PAGE, sizeof(struct page *), GFP_KERNEL);
425
	if (!pt_pages)
408
	if (!pt_pages)
426
		return ERR_PTR(-ENOMEM);
409
		return ERR_PTR(-ENOMEM);
427
 
410
 
428
	for (i = 0; i < GEN8_PDES_PER_PAGE; i++) {
411
	for (i = 0; i < GEN8_PDES_PER_PAGE; i++) {
429
		pt_pages[i] = alloc_page(GFP_KERNEL);
412
		pt_pages[i] = alloc_page(GFP_KERNEL);
430
		if (!pt_pages[i])
413
		if (!pt_pages[i])
431
			goto bail;
414
			goto bail;
432
			}
415
			}
433
 
416
 
434
	return pt_pages;
417
	return pt_pages;
435
 
418
 
436
bail:
419
bail:
437
	gen8_free_page_tables(pt_pages);
420
	gen8_free_page_tables(pt_pages);
438
	kfree(pt_pages);
421
	kfree(pt_pages);
439
	return ERR_PTR(-ENOMEM);
422
	return ERR_PTR(-ENOMEM);
440
}
423
}
441
 
424
 
442
static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt,
425
static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt,
443
					   const int max_pdp)
426
					   const int max_pdp)
444
{
427
{
445
	struct page **pt_pages[GEN8_LEGACY_PDPS];
428
	struct page **pt_pages[GEN8_LEGACY_PDPS];
446
	int i, ret;
429
	int i, ret;
447
 
430
 
448
	for (i = 0; i < max_pdp; i++) {
431
	for (i = 0; i < max_pdp; i++) {
449
		pt_pages[i] = __gen8_alloc_page_tables();
432
		pt_pages[i] = __gen8_alloc_page_tables();
450
		if (IS_ERR(pt_pages[i])) {
433
		if (IS_ERR(pt_pages[i])) {
451
			ret = PTR_ERR(pt_pages[i]);
434
			ret = PTR_ERR(pt_pages[i]);
452
			goto unwind_out;
435
			goto unwind_out;
453
		}
436
		}
454
	}
437
	}
455
 
438
 
456
	/* NB: Avoid touching gen8_pt_pages until last to keep the allocation,
439
	/* NB: Avoid touching gen8_pt_pages until last to keep the allocation,
457
	 * "atomic" - for cleanup purposes.
440
	 * "atomic" - for cleanup purposes.
458
	 */
441
	 */
459
	for (i = 0; i < max_pdp; i++)
442
	for (i = 0; i < max_pdp; i++)
460
		ppgtt->gen8_pt_pages[i] = pt_pages[i];
443
		ppgtt->gen8_pt_pages[i] = pt_pages[i];
461
 
444
 
462
	return 0;
445
	return 0;
463
 
446
 
464
unwind_out:
447
unwind_out:
465
	while (i--) {
448
	while (i--) {
466
		gen8_free_page_tables(pt_pages[i]);
449
		gen8_free_page_tables(pt_pages[i]);
467
		kfree(pt_pages[i]);
450
		kfree(pt_pages[i]);
468
	}
451
	}
469
 
452
 
470
	return ret;
453
	return ret;
471
}
454
}
472
 
455
 
473
static int gen8_ppgtt_allocate_dma(struct i915_hw_ppgtt *ppgtt)
456
static int gen8_ppgtt_allocate_dma(struct i915_hw_ppgtt *ppgtt)
474
{
457
{
475
	int i;
458
	int i;
476
 
459
 
477
	for (i = 0; i < ppgtt->num_pd_pages; i++) {
460
	for (i = 0; i < ppgtt->num_pd_pages; i++) {
478
		ppgtt->gen8_pt_dma_addr[i] = kcalloc(GEN8_PDES_PER_PAGE,
461
		ppgtt->gen8_pt_dma_addr[i] = kcalloc(GEN8_PDES_PER_PAGE,
479
						     sizeof(dma_addr_t),
462
						     sizeof(dma_addr_t),
480
						     GFP_KERNEL);
463
						     GFP_KERNEL);
481
		if (!ppgtt->gen8_pt_dma_addr[i])
464
		if (!ppgtt->gen8_pt_dma_addr[i])
482
			return -ENOMEM;
465
			return -ENOMEM;
483
		}
466
		}
484
 
467
 
485
	return 0;
468
	return 0;
486
}
469
}
487
 
470
 
488
static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt,
471
static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt,
489
						const int max_pdp)
472
						const int max_pdp)
490
{
473
{
491
//	ppgtt->pd_pages = alloc_pages(GFP_KERNEL, get_order(max_pdp << PAGE_SHIFT));
474
//	ppgtt->pd_pages = alloc_pages(GFP_KERNEL, get_order(max_pdp << PAGE_SHIFT));
492
	if (!ppgtt->pd_pages)
475
	if (!ppgtt->pd_pages)
493
		return -ENOMEM;
476
		return -ENOMEM;
494
 
477
 
495
//   ppgtt->num_pd_pages = 1 << get_order(max_pdp << PAGE_SHIFT);
478
//   ppgtt->num_pd_pages = 1 << get_order(max_pdp << PAGE_SHIFT);
496
	BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPS);
479
	BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPS);
497
 
480
 
498
	return 0;
481
	return 0;
499
}
482
}
500
 
483
 
501
static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt,
484
static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt,
502
			    const int max_pdp)
485
			    const int max_pdp)
503
{
486
{
504
	int ret;
487
	int ret;
505
 
488
 
506
	ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp);
489
	ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp);
507
	if (ret)
490
	if (ret)
508
		return ret;
491
		return ret;
509
 
492
 
510
	ret = gen8_ppgtt_allocate_page_tables(ppgtt, max_pdp);
493
	ret = gen8_ppgtt_allocate_page_tables(ppgtt, max_pdp);
511
	if (ret) {
494
	if (ret) {
512
//		__free_pages(ppgtt->pd_pages, get_order(max_pdp << PAGE_SHIFT));
495
//		__free_pages(ppgtt->pd_pages, get_order(max_pdp << PAGE_SHIFT));
513
		return ret;
496
		return ret;
514
	}
497
	}
515
 
498
 
516
	ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE;
499
	ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE;
517
 
500
 
518
	ret = gen8_ppgtt_allocate_dma(ppgtt);
501
	ret = gen8_ppgtt_allocate_dma(ppgtt);
519
	if (ret)
502
	if (ret)
520
		gen8_ppgtt_free(ppgtt);
503
		gen8_ppgtt_free(ppgtt);
521
 
504
 
522
	return ret;
505
	return ret;
523
}
506
}
524
 
507
 
525
static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt,
508
static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt,
526
					     const int pd)
509
					     const int pd)
527
{
510
{
528
	dma_addr_t pd_addr;
511
	dma_addr_t pd_addr;
529
	int ret;
512
	int ret;
530
 
513
 
531
	pd_addr = pci_map_page(ppgtt->base.dev->pdev,
514
	pd_addr = pci_map_page(ppgtt->base.dev->pdev,
532
			       &ppgtt->pd_pages[pd], 0,
515
			       &ppgtt->pd_pages[pd], 0,
533
			       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
516
			       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
534
 
517
 
535
//   ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr);
518
//   ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr);
536
//   if (ret)
519
//   if (ret)
537
//       return ret;
520
//       return ret;
538
 
521
 
539
	ppgtt->pd_dma_addr[pd] = pd_addr;
522
	ppgtt->pd_dma_addr[pd] = pd_addr;
540
 
523
 
541
	return 0;
524
	return 0;
542
}
525
}
543
 
526
 
544
static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt,
527
static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt,
545
					const int pd,
528
					const int pd,
546
					const int pt)
529
					const int pt)
547
{
530
{
548
	dma_addr_t pt_addr;
531
	dma_addr_t pt_addr;
549
	struct page *p;
532
	struct page *p;
550
	int ret;
533
	int ret;
551
 
534
 
552
	p = ppgtt->gen8_pt_pages[pd][pt];
535
	p = ppgtt->gen8_pt_pages[pd][pt];
553
	pt_addr = pci_map_page(ppgtt->base.dev->pdev,
536
	pt_addr = pci_map_page(ppgtt->base.dev->pdev,
554
			       p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
537
			       p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
555
//   ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr);
538
//   ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr);
556
//   if (ret)
539
//   if (ret)
557
//       return ret;
540
//       return ret;
558
 
541
 
559
	ppgtt->gen8_pt_dma_addr[pd][pt] = pt_addr;
542
	ppgtt->gen8_pt_dma_addr[pd][pt] = pt_addr;
560
 
543
 
561
	return 0;
544
	return 0;
562
}
545
}
563
 
546
 
564
/**
547
/**
565
 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
548
 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
566
 * with a net effect resembling a 2-level page table in normal x86 terms. Each
549
 * with a net effect resembling a 2-level page table in normal x86 terms. Each
567
 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
550
 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
568
 * space.
551
 * space.
569
 *
552
 *
570
 * FIXME: split allocation into smaller pieces. For now we only ever do this
553
 * FIXME: split allocation into smaller pieces. For now we only ever do this
571
 * once, but with full PPGTT, the multiple contiguous allocations will be bad.
554
 * once, but with full PPGTT, the multiple contiguous allocations will be bad.
572
 * TODO: Do something with the size parameter
555
 * TODO: Do something with the size parameter
573
 */
556
 */
574
static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
557
static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
575
{
558
{
576
	const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
559
	const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
577
	const int min_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
560
	const int min_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
578
	int i, j, ret;
561
	int i, j, ret;
579
	gen8_ppgtt_pde_t *pd_vaddr;
-
 
580
 
562
 
581
	if (size % (1<<30))
563
	if (size % (1<<30))
582
		DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
564
		DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
583
 
565
 
584
	/* 1. Do all our allocations for page directories and page tables. */
566
	/* 1. Do all our allocations for page directories and page tables. */
585
	ret = gen8_ppgtt_alloc(ppgtt, max_pdp);
567
	ret = gen8_ppgtt_alloc(ppgtt, max_pdp);
586
	if (ret)
568
	if (ret)
587
		return ret;
569
		return ret;
588
 
570
 
589
	/*
571
	/*
590
	 * 2. Create DMA mappings for the page directories and page tables.
572
	 * 2. Create DMA mappings for the page directories and page tables.
591
	 */
573
	 */
592
	for (i = 0; i < max_pdp; i++) {
574
	for (i = 0; i < max_pdp; i++) {
593
		ret = gen8_ppgtt_setup_page_directories(ppgtt, i);
575
		ret = gen8_ppgtt_setup_page_directories(ppgtt, i);
594
		if (ret)
576
		if (ret)
595
			goto bail;
577
			goto bail;
596
 
578
 
597
		for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
579
		for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
598
			ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j);
580
			ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j);
599
			if (ret)
581
			if (ret)
600
				goto bail;
582
				goto bail;
601
		}
583
		}
602
	}
584
	}
603
 
585
 
604
	/*
586
	/*
605
	 * 3. Map all the page directory entires to point to the page tables
587
	 * 3. Map all the page directory entires to point to the page tables
606
	 * we've allocated.
588
	 * we've allocated.
607
	 *
589
	 *
608
	 * For now, the PPGTT helper functions all require that the PDEs are
590
	 * For now, the PPGTT helper functions all require that the PDEs are
609
	 * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
591
	 * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
610
	 * will never need to touch the PDEs again.
592
	 * will never need to touch the PDEs again.
611
	 */
593
	 */
612
 
-
 
613
    pd_vaddr = AllocKernelSpace(4096);
-
 
614
 
-
 
615
    for (i = 0; i < max_pdp; i++) {
594
    for (i = 0; i < max_pdp; i++) {
-
 
595
		gen8_ppgtt_pde_t *pd_vaddr;
616
        MapPage(pd_vaddr,(addr_t)(&ppgtt->pd_pages[i]), 3);
596
		pd_vaddr = kmap_atomic(&ppgtt->pd_pages[i]);
617
		for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
597
		for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
618
			dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
598
			dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
619
			pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
599
			pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
620
						      I915_CACHE_LLC);
600
						      I915_CACHE_LLC);
621
		}
601
		}
622
		if (!HAS_LLC(ppgtt->base.dev))
602
		if (!HAS_LLC(ppgtt->base.dev))
623
			drm_clflush_virt_range(pd_vaddr, PAGE_SIZE);
603
			drm_clflush_virt_range(pd_vaddr, PAGE_SIZE);
-
 
604
		kunmap_atomic(pd_vaddr);
624
	}
605
	}
625
    FreeKernelSpace(pd_vaddr);
-
 
626
 
-
 
627
	ppgtt->enable = gen8_ppgtt_enable;
606
 
628
	ppgtt->switch_mm = gen8_mm_switch;
607
	ppgtt->switch_mm = gen8_mm_switch;
629
	ppgtt->base.clear_range = gen8_ppgtt_clear_range;
608
	ppgtt->base.clear_range = gen8_ppgtt_clear_range;
630
	ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
609
	ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
631
	ppgtt->base.cleanup = gen8_ppgtt_cleanup;
610
	ppgtt->base.cleanup = gen8_ppgtt_cleanup;
632
	ppgtt->base.start = 0;
611
	ppgtt->base.start = 0;
633
	ppgtt->base.total = ppgtt->num_pd_entries * GEN8_PTES_PER_PAGE * PAGE_SIZE;
612
	ppgtt->base.total = ppgtt->num_pd_entries * GEN8_PTES_PER_PAGE * PAGE_SIZE;
634
 
613
 
635
	ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
614
	ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
636
 
615
 
637
	DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
616
	DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
638
			 ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
617
			 ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
639
	DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
618
	DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
640
			 ppgtt->num_pd_entries,
619
			 ppgtt->num_pd_entries,
641
			 (ppgtt->num_pd_entries - min_pt_pages) + size % (1<<30));
620
			 (ppgtt->num_pd_entries - min_pt_pages) + size % (1<<30));
642
	return 0;
621
	return 0;
643
 
622
 
644
bail:
623
bail:
645
	gen8_ppgtt_unmap_pages(ppgtt);
624
	gen8_ppgtt_unmap_pages(ppgtt);
646
	gen8_ppgtt_free(ppgtt);
625
	gen8_ppgtt_free(ppgtt);
647
	return ret;
626
	return ret;
648
}
627
}
649
 
628
 
650
static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
629
static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
651
{
630
{
652
	struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
631
	struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
653
	gen6_gtt_pte_t __iomem *pd_addr;
632
	gen6_gtt_pte_t __iomem *pd_addr;
654
	uint32_t pd_entry;
633
	uint32_t pd_entry;
655
	int i;
634
	int i;
656
 
635
 
657
	WARN_ON(ppgtt->pd_offset & 0x3f);
636
	WARN_ON(ppgtt->pd_offset & 0x3f);
658
	pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
637
	pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
659
		ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
638
		ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
660
	for (i = 0; i < ppgtt->num_pd_entries; i++) {
639
	for (i = 0; i < ppgtt->num_pd_entries; i++) {
661
		dma_addr_t pt_addr;
640
		dma_addr_t pt_addr;
662
 
641
 
663
		pt_addr = ppgtt->pt_dma_addr[i];
642
		pt_addr = ppgtt->pt_dma_addr[i];
664
		pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
643
		pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
665
		pd_entry |= GEN6_PDE_VALID;
644
		pd_entry |= GEN6_PDE_VALID;
666
 
645
 
667
		writel(pd_entry, pd_addr + i);
646
		writel(pd_entry, pd_addr + i);
668
	}
647
	}
669
	readl(pd_addr);
648
	readl(pd_addr);
670
}
649
}
671
 
650
 
672
static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
651
static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
673
{
652
{
674
	BUG_ON(ppgtt->pd_offset & 0x3f);
653
	BUG_ON(ppgtt->pd_offset & 0x3f);
675
 
654
 
676
	return (ppgtt->pd_offset / 64) << 16;
655
	return (ppgtt->pd_offset / 64) << 16;
677
}
656
}
678
 
657
 
679
static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
658
static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
680
			 struct intel_engine_cs *ring,
659
			 struct intel_engine_cs *ring)
681
			 bool synchronous)
-
 
682
{
660
{
683
	struct drm_device *dev = ppgtt->base.dev;
-
 
684
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
685
	int ret;
661
	int ret;
686
 
-
 
687
	/* If we're in reset, we can assume the GPU is sufficiently idle to
-
 
688
	 * manually frob these bits. Ideally we could use the ring functions,
-
 
689
	 * except our error handling makes it quite difficult (can't use
-
 
690
	 * intel_ring_begin, ring->flush, or intel_ring_advance)
-
 
691
	 *
-
 
692
	 * FIXME: We should try not to special case reset
-
 
693
	 */
-
 
694
	if (synchronous ||
-
 
695
	    i915_reset_in_progress(&dev_priv->gpu_error)) {
-
 
696
		WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
-
 
697
		I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
-
 
698
		I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
-
 
699
		POSTING_READ(RING_PP_DIR_BASE(ring));
-
 
700
		return 0;
-
 
701
	}
-
 
702
 
662
 
703
	/* NB: TLBs must be flushed and invalidated before a switch */
663
	/* NB: TLBs must be flushed and invalidated before a switch */
704
	ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
664
	ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
705
	if (ret)
665
	if (ret)
706
		return ret;
666
		return ret;
707
 
667
 
708
	ret = intel_ring_begin(ring, 6);
668
	ret = intel_ring_begin(ring, 6);
709
	if (ret)
669
	if (ret)
710
		return ret;
670
		return ret;
711
 
671
 
712
	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
672
	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
713
	intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
673
	intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
714
	intel_ring_emit(ring, PP_DIR_DCLV_2G);
674
	intel_ring_emit(ring, PP_DIR_DCLV_2G);
715
	intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
675
	intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
716
	intel_ring_emit(ring, get_pd_offset(ppgtt));
676
	intel_ring_emit(ring, get_pd_offset(ppgtt));
717
	intel_ring_emit(ring, MI_NOOP);
677
	intel_ring_emit(ring, MI_NOOP);
718
	intel_ring_advance(ring);
678
	intel_ring_advance(ring);
719
 
679
 
720
	return 0;
680
	return 0;
721
}
681
}
722
 
682
 
723
static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
683
static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
724
			  struct intel_engine_cs *ring,
684
			  struct intel_engine_cs *ring)
725
			  bool synchronous)
-
 
726
{
685
{
727
	struct drm_device *dev = ppgtt->base.dev;
-
 
728
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
729
	int ret;
686
	int ret;
730
 
-
 
731
	/* If we're in reset, we can assume the GPU is sufficiently idle to
-
 
732
	 * manually frob these bits. Ideally we could use the ring functions,
-
 
733
	 * except our error handling makes it quite difficult (can't use
-
 
734
	 * intel_ring_begin, ring->flush, or intel_ring_advance)
-
 
735
	 *
-
 
736
	 * FIXME: We should try not to special case reset
-
 
737
	 */
-
 
738
	if (synchronous ||
-
 
739
	    i915_reset_in_progress(&dev_priv->gpu_error)) {
-
 
740
		WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
-
 
741
		I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
-
 
742
		I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
-
 
743
		POSTING_READ(RING_PP_DIR_BASE(ring));
-
 
744
		return 0;
-
 
745
	}
-
 
746
 
687
 
747
	/* NB: TLBs must be flushed and invalidated before a switch */
688
	/* NB: TLBs must be flushed and invalidated before a switch */
748
	ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
689
	ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
749
	if (ret)
690
	if (ret)
750
		return ret;
691
		return ret;
751
 
692
 
752
	ret = intel_ring_begin(ring, 6);
693
	ret = intel_ring_begin(ring, 6);
753
	if (ret)
694
	if (ret)
754
		return ret;
695
		return ret;
755
 
696
 
756
	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
697
	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
757
	intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
698
	intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
758
	intel_ring_emit(ring, PP_DIR_DCLV_2G);
699
	intel_ring_emit(ring, PP_DIR_DCLV_2G);
759
	intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
700
	intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
760
	intel_ring_emit(ring, get_pd_offset(ppgtt));
701
	intel_ring_emit(ring, get_pd_offset(ppgtt));
761
	intel_ring_emit(ring, MI_NOOP);
702
	intel_ring_emit(ring, MI_NOOP);
762
	intel_ring_advance(ring);
703
	intel_ring_advance(ring);
763
 
704
 
764
	/* XXX: RCS is the only one to auto invalidate the TLBs? */
705
	/* XXX: RCS is the only one to auto invalidate the TLBs? */
765
	if (ring->id != RCS) {
706
	if (ring->id != RCS) {
766
		ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
707
		ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
767
		if (ret)
708
		if (ret)
768
			return ret;
709
			return ret;
769
	}
710
	}
770
 
711
 
771
	return 0;
712
	return 0;
772
}
713
}
773
 
714
 
774
static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
715
static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
775
			  struct intel_engine_cs *ring,
716
			  struct intel_engine_cs *ring)
776
			  bool synchronous)
-
 
777
{
717
{
778
	struct drm_device *dev = ppgtt->base.dev;
718
	struct drm_device *dev = ppgtt->base.dev;
779
	struct drm_i915_private *dev_priv = dev->dev_private;
719
	struct drm_i915_private *dev_priv = dev->dev_private;
780
 
-
 
781
	if (!synchronous)
-
 
782
		return 0;
720
 
783
 
721
 
784
	I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
722
	I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
785
	I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
723
	I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
786
 
724
 
787
	POSTING_READ(RING_PP_DIR_DCLV(ring));
725
	POSTING_READ(RING_PP_DIR_DCLV(ring));
788
 
726
 
789
	return 0;
727
	return 0;
790
}
728
}
791
 
729
 
792
static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
730
static void gen8_ppgtt_enable(struct drm_device *dev)
793
{
-
 
794
	struct drm_device *dev = ppgtt->base.dev;
731
{
795
	struct drm_i915_private *dev_priv = dev->dev_private;
732
	struct drm_i915_private *dev_priv = dev->dev_private;
796
	struct intel_engine_cs *ring;
733
	struct intel_engine_cs *ring;
797
	int j, ret;
734
	int j;
798
 
735
 
799
	for_each_ring(ring, dev_priv, j) {
736
	for_each_ring(ring, dev_priv, j) {
800
		I915_WRITE(RING_MODE_GEN7(ring),
737
		I915_WRITE(RING_MODE_GEN7(ring),
801
			   _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
738
			   _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
802
 
-
 
803
		/* We promise to do a switch later with FULL PPGTT. If this is
-
 
804
		 * aliasing, this is the one and only switch we'll do */
-
 
805
		if (USES_FULL_PPGTT(dev))
-
 
806
			continue;
-
 
807
 
-
 
808
		ret = ppgtt->switch_mm(ppgtt, ring, true);
-
 
809
		if (ret)
-
 
810
			goto err_out;
-
 
811
	}
739
	}
812
 
-
 
813
	return 0;
-
 
814
 
-
 
815
err_out:
-
 
816
	for_each_ring(ring, dev_priv, j)
-
 
817
		I915_WRITE(RING_MODE_GEN7(ring),
-
 
818
			   _MASKED_BIT_DISABLE(GFX_PPGTT_ENABLE));
-
 
819
	return ret;
-
 
820
}
740
}
821
 
741
 
822
static int gen7_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
742
static void gen7_ppgtt_enable(struct drm_device *dev)
823
{
-
 
824
	struct drm_device *dev = ppgtt->base.dev;
743
{
825
	struct drm_i915_private *dev_priv = dev->dev_private;
744
	struct drm_i915_private *dev_priv = dev->dev_private;
826
	struct intel_engine_cs *ring;
745
	struct intel_engine_cs *ring;
827
		uint32_t ecochk, ecobits;
746
		uint32_t ecochk, ecobits;
828
	int i;
747
	int i;
829
 
748
 
830
		ecobits = I915_READ(GAC_ECO_BITS);
749
		ecobits = I915_READ(GAC_ECO_BITS);
831
		I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
750
		I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
832
 
751
 
833
		ecochk = I915_READ(GAM_ECOCHK);
752
		ecochk = I915_READ(GAM_ECOCHK);
834
		if (IS_HASWELL(dev)) {
753
		if (IS_HASWELL(dev)) {
835
			ecochk |= ECOCHK_PPGTT_WB_HSW;
754
			ecochk |= ECOCHK_PPGTT_WB_HSW;
836
		} else {
755
		} else {
837
			ecochk |= ECOCHK_PPGTT_LLC_IVB;
756
			ecochk |= ECOCHK_PPGTT_LLC_IVB;
838
			ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
757
			ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
839
		}
758
		}
840
		I915_WRITE(GAM_ECOCHK, ecochk);
759
		I915_WRITE(GAM_ECOCHK, ecochk);
841
 
760
 
842
	for_each_ring(ring, dev_priv, i) {
761
	for_each_ring(ring, dev_priv, i) {
843
		int ret;
-
 
844
		/* GFX_MODE is per-ring on gen7+ */
762
		/* GFX_MODE is per-ring on gen7+ */
845
			I915_WRITE(RING_MODE_GEN7(ring),
763
			I915_WRITE(RING_MODE_GEN7(ring),
846
				   _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
764
				   _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
847
 
-
 
848
		/* We promise to do a switch later with FULL PPGTT. If this is
-
 
849
		 * aliasing, this is the one and only switch we'll do */
-
 
850
		if (USES_FULL_PPGTT(dev))
-
 
851
			continue;
-
 
852
 
-
 
853
		ret = ppgtt->switch_mm(ppgtt, ring, true);
-
 
854
		if (ret)
-
 
855
			return ret;
-
 
856
	}
765
	}
857
 
-
 
858
	return 0;
-
 
859
}
766
}
860
 
767
 
861
static int gen6_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
768
static void gen6_ppgtt_enable(struct drm_device *dev)
862
{
-
 
863
	struct drm_device *dev = ppgtt->base.dev;
769
{
864
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
865
	struct intel_engine_cs *ring;
770
	struct drm_i915_private *dev_priv = dev->dev_private;
866
	uint32_t ecochk, gab_ctl, ecobits;
-
 
867
	int i;
771
	uint32_t ecochk, gab_ctl, ecobits;
868
 
772
 
869
	ecobits = I915_READ(GAC_ECO_BITS);
773
	ecobits = I915_READ(GAC_ECO_BITS);
870
	I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
774
	I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
871
		   ECOBITS_PPGTT_CACHE64B);
775
		   ECOBITS_PPGTT_CACHE64B);
872
 
776
 
873
	gab_ctl = I915_READ(GAB_CTL);
777
	gab_ctl = I915_READ(GAB_CTL);
874
	I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
778
	I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
875
 
779
 
876
	ecochk = I915_READ(GAM_ECOCHK);
780
	ecochk = I915_READ(GAM_ECOCHK);
877
	I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
781
	I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
878
 
782
 
879
	I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
783
	I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
880
 
-
 
881
	for_each_ring(ring, dev_priv, i) {
-
 
882
		int ret = ppgtt->switch_mm(ppgtt, ring, true);
-
 
883
		if (ret)
-
 
884
			return ret;
-
 
885
	}
-
 
886
 
-
 
887
	return 0;
-
 
888
}
784
}
889
 
785
 
890
/* PPGTT support for Sandybdrige/Gen6 and later */
786
/* PPGTT support for Sandybdrige/Gen6 and later */
891
static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
787
static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
892
				   uint64_t start,
788
				   uint64_t start,
893
				   uint64_t length,
789
				   uint64_t length,
894
				   bool use_scratch)
790
				   bool use_scratch)
895
{
791
{
896
	struct i915_hw_ppgtt *ppgtt =
792
	struct i915_hw_ppgtt *ppgtt =
897
		container_of(vm, struct i915_hw_ppgtt, base);
793
		container_of(vm, struct i915_hw_ppgtt, base);
898
	gen6_gtt_pte_t *pt_vaddr, scratch_pte;
794
	gen6_gtt_pte_t *pt_vaddr, scratch_pte;
899
	unsigned first_entry = start >> PAGE_SHIFT;
795
	unsigned first_entry = start >> PAGE_SHIFT;
900
	unsigned num_entries = length >> PAGE_SHIFT;
796
	unsigned num_entries = length >> PAGE_SHIFT;
901
	unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
797
	unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
902
	unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
798
	unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
903
	unsigned last_pte, i;
799
	unsigned last_pte, i;
904
 
800
 
905
	scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
801
	scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
906
 
-
 
907
    pt_vaddr = AllocKernelSpace(4096);
-
 
908
 
-
 
909
    if(pt_vaddr == NULL)
-
 
910
        return;
-
 
911
 
802
 
912
	while (num_entries) {
803
	while (num_entries) {
913
            last_pte = first_pte + num_entries;
804
            last_pte = first_pte + num_entries;
914
            if (last_pte > I915_PPGTT_PT_ENTRIES)
805
            if (last_pte > I915_PPGTT_PT_ENTRIES)
915
                last_pte = I915_PPGTT_PT_ENTRIES;
806
                last_pte = I915_PPGTT_PT_ENTRIES;
916
 
807
 
917
            MapPage(pt_vaddr,(addr_t)(ppgtt->pt_pages[act_pt]), 3);
808
		pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
918
 
809
 
919
            for (i = first_pte; i < last_pte; i++)
810
            for (i = first_pte; i < last_pte; i++)
920
                pt_vaddr[i] = scratch_pte;
811
                pt_vaddr[i] = scratch_pte;
-
 
812
 
-
 
813
		kunmap_atomic(pt_vaddr);
921
 
814
 
922
            num_entries -= last_pte - first_pte;
815
            num_entries -= last_pte - first_pte;
923
            first_pte = 0;
816
            first_pte = 0;
924
            act_pt++;
817
            act_pt++;
925
    };
-
 
926
 
818
	}
927
    FreeKernelSpace(pt_vaddr);
-
 
928
}
819
}
929
 
820
 
930
static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
821
static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
931
				      struct sg_table *pages,
822
				      struct sg_table *pages,
932
				      uint64_t start,
823
				      uint64_t start,
933
				      enum i915_cache_level cache_level, u32 flags)
824
				      enum i915_cache_level cache_level, u32 flags)
934
{
825
{
935
	struct i915_hw_ppgtt *ppgtt =
826
	struct i915_hw_ppgtt *ppgtt =
936
		container_of(vm, struct i915_hw_ppgtt, base);
827
		container_of(vm, struct i915_hw_ppgtt, base);
937
	gen6_gtt_pte_t *pt_vaddr;
828
	gen6_gtt_pte_t *pt_vaddr;
938
	unsigned first_entry = start >> PAGE_SHIFT;
829
	unsigned first_entry = start >> PAGE_SHIFT;
939
	unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
830
	unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
940
	unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
831
	unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
941
	struct sg_page_iter sg_iter;
832
	struct sg_page_iter sg_iter;
942
 
-
 
943
    pt_vaddr = AllocKernelSpace(4096);
-
 
944
 
833
 
945
    if(pt_vaddr == NULL)
-
 
946
        return;
-
 
947
 
-
 
948
    MapPage(pt_vaddr,(addr_t)(ppgtt->pt_pages[act_pt]), 3);
834
	pt_vaddr = NULL;
-
 
835
	for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
-
 
836
		if (pt_vaddr == NULL)
949
	for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
837
			pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
950
 
838
 
951
		pt_vaddr[act_pte] =
839
		pt_vaddr[act_pte] =
952
			vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
840
			vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
953
				       cache_level, true, flags);
841
				       cache_level, true, flags);
954
 
842
 
955
		if (++act_pte == I915_PPGTT_PT_ENTRIES) {
843
		if (++act_pte == I915_PPGTT_PT_ENTRIES) {
-
 
844
			kunmap_atomic(pt_vaddr);
-
 
845
			pt_vaddr = NULL;
956
			act_pt++;
846
			act_pt++;
957
    		MapPage(pt_vaddr,(addr_t)(ppgtt->pt_pages[act_pt]), 3);
-
 
958
			act_pte = 0;
847
			act_pte = 0;
959
			}
848
			}
960
		}
849
		}
-
 
850
	if (pt_vaddr)
961
    FreeKernelSpace(pt_vaddr);
851
		kunmap_atomic(pt_vaddr);
962
}
852
}
963
 
853
 
964
static void gen6_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
854
static void gen6_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
965
{
855
{
966
	int i;
856
	int i;
967
 
857
 
968
	if (ppgtt->pt_dma_addr) {
858
	if (ppgtt->pt_dma_addr) {
969
		for (i = 0; i < ppgtt->num_pd_entries; i++)
859
		for (i = 0; i < ppgtt->num_pd_entries; i++)
970
			pci_unmap_page(ppgtt->base.dev->pdev,
860
			pci_unmap_page(ppgtt->base.dev->pdev,
971
				       ppgtt->pt_dma_addr[i],
861
				       ppgtt->pt_dma_addr[i],
972
				       4096, PCI_DMA_BIDIRECTIONAL);
862
				       4096, PCI_DMA_BIDIRECTIONAL);
973
	}
863
	}
974
}
864
}
975
 
865
 
976
static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
866
static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
977
{
867
{
978
	int i;
868
	int i;
979
 
869
 
980
	kfree(ppgtt->pt_dma_addr);
870
	kfree(ppgtt->pt_dma_addr);
981
	for (i = 0; i < ppgtt->num_pd_entries; i++)
871
	for (i = 0; i < ppgtt->num_pd_entries; i++)
982
		__free_page(ppgtt->pt_pages[i]);
872
		__free_page(ppgtt->pt_pages[i]);
983
	kfree(ppgtt->pt_pages);
873
	kfree(ppgtt->pt_pages);
984
}
874
}
985
 
875
 
986
static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
876
static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
987
{
877
{
988
	struct i915_hw_ppgtt *ppgtt =
878
	struct i915_hw_ppgtt *ppgtt =
989
		container_of(vm, struct i915_hw_ppgtt, base);
879
		container_of(vm, struct i915_hw_ppgtt, base);
990
 
-
 
991
	list_del(&vm->global_link);
-
 
992
	drm_mm_takedown(&ppgtt->base.mm);
880
 
993
	drm_mm_remove_node(&ppgtt->node);
881
	drm_mm_remove_node(&ppgtt->node);
994
 
882
 
995
	gen6_ppgtt_unmap_pages(ppgtt);
883
	gen6_ppgtt_unmap_pages(ppgtt);
996
	gen6_ppgtt_free(ppgtt);
884
	gen6_ppgtt_free(ppgtt);
997
}
885
}
998
 
886
 
999
static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
887
static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
1000
{
888
{
1001
	struct drm_device *dev = ppgtt->base.dev;
889
	struct drm_device *dev = ppgtt->base.dev;
1002
	struct drm_i915_private *dev_priv = dev->dev_private;
890
	struct drm_i915_private *dev_priv = dev->dev_private;
1003
	bool retried = false;
891
	bool retried = false;
1004
	int ret;
892
	int ret;
1005
 
893
 
1006
	/* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
894
	/* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1007
	 * allocator works in address space sizes, so it's multiplied by page
895
	 * allocator works in address space sizes, so it's multiplied by page
1008
	 * size. We allocate at the top of the GTT to avoid fragmentation.
896
	 * size. We allocate at the top of the GTT to avoid fragmentation.
1009
	 */
897
	 */
1010
	BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
898
	BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
1011
alloc:
899
alloc:
1012
	ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
900
	ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
1013
						  &ppgtt->node, GEN6_PD_SIZE,
901
						  &ppgtt->node, GEN6_PD_SIZE,
1014
						  GEN6_PD_ALIGN, 0,
902
						  GEN6_PD_ALIGN, 0,
1015
						  0, dev_priv->gtt.base.total,
903
						  0, dev_priv->gtt.base.total,
1016
						  DRM_MM_TOPDOWN);
904
						  DRM_MM_TOPDOWN);
1017
	if (ret == -ENOSPC && !retried) {
905
	if (ret == -ENOSPC && !retried) {
1018
		ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
906
		ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
1019
					       GEN6_PD_SIZE, GEN6_PD_ALIGN,
907
					       GEN6_PD_SIZE, GEN6_PD_ALIGN,
1020
					       I915_CACHE_NONE,
908
					       I915_CACHE_NONE,
1021
					       0, dev_priv->gtt.base.total,
909
					       0, dev_priv->gtt.base.total,
1022
					       0);
910
					       0);
1023
		if (ret)
911
		if (ret)
1024
			return ret;
912
			return ret;
1025
 
913
 
1026
		retried = true;
914
		retried = true;
1027
		goto alloc;
915
		goto alloc;
1028
	}
916
	}
1029
 
917
 
1030
	if (ppgtt->node.start < dev_priv->gtt.mappable_end)
918
	if (ppgtt->node.start < dev_priv->gtt.mappable_end)
1031
		DRM_DEBUG("Forced to use aperture for PDEs\n");
919
		DRM_DEBUG("Forced to use aperture for PDEs\n");
1032
 
920
 
1033
	ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
921
	ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
1034
	return ret;
922
	return ret;
1035
}
923
}
1036
 
924
 
1037
static int gen6_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt)
925
static int gen6_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt)
1038
{
926
{
1039
	int i;
927
	int i;
1040
 
928
 
1041
	ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
929
	ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
1042
				  GFP_KERNEL);
930
				  GFP_KERNEL);
1043
 
931
 
1044
	if (!ppgtt->pt_pages)
932
	if (!ppgtt->pt_pages)
1045
		return -ENOMEM;
933
		return -ENOMEM;
1046
 
934
 
1047
	for (i = 0; i < ppgtt->num_pd_entries; i++) {
935
	for (i = 0; i < ppgtt->num_pd_entries; i++) {
1048
		ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
936
		ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
1049
		if (!ppgtt->pt_pages[i]) {
937
		if (!ppgtt->pt_pages[i]) {
1050
			gen6_ppgtt_free(ppgtt);
938
			gen6_ppgtt_free(ppgtt);
1051
			return -ENOMEM;
939
			return -ENOMEM;
1052
		}
940
		}
1053
	}
941
	}
1054
 
942
 
1055
	return 0;
943
	return 0;
1056
}
944
}
1057
 
945
 
1058
static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
946
static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1059
{
947
{
1060
	int ret;
948
	int ret;
1061
 
949
 
1062
	ret = gen6_ppgtt_allocate_page_directories(ppgtt);
950
	ret = gen6_ppgtt_allocate_page_directories(ppgtt);
1063
	if (ret)
951
	if (ret)
1064
		return ret;
952
		return ret;
1065
 
953
 
1066
	ret = gen6_ppgtt_allocate_page_tables(ppgtt);
954
	ret = gen6_ppgtt_allocate_page_tables(ppgtt);
1067
	if (ret) {
955
	if (ret) {
1068
		drm_mm_remove_node(&ppgtt->node);
956
		drm_mm_remove_node(&ppgtt->node);
1069
		return ret;
957
		return ret;
1070
	}
958
	}
1071
 
959
 
1072
	ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
960
	ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
1073
				     GFP_KERNEL);
961
				     GFP_KERNEL);
1074
	if (!ppgtt->pt_dma_addr) {
962
	if (!ppgtt->pt_dma_addr) {
1075
		drm_mm_remove_node(&ppgtt->node);
963
		drm_mm_remove_node(&ppgtt->node);
1076
		gen6_ppgtt_free(ppgtt);
964
		gen6_ppgtt_free(ppgtt);
1077
		return -ENOMEM;
965
		return -ENOMEM;
1078
	}
966
	}
1079
 
967
 
1080
	return 0;
968
	return 0;
1081
}
969
}
1082
 
970
 
1083
static int gen6_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt)
971
static int gen6_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt)
1084
{
972
{
1085
	struct drm_device *dev = ppgtt->base.dev;
973
	struct drm_device *dev = ppgtt->base.dev;
1086
	int i;
974
	int i;
1087
 
975
 
1088
	for (i = 0; i < ppgtt->num_pd_entries; i++) {
976
	for (i = 0; i < ppgtt->num_pd_entries; i++) {
1089
		dma_addr_t pt_addr;
977
		dma_addr_t pt_addr;
1090
 
978
 
1091
		pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
979
		pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
1092
				       PCI_DMA_BIDIRECTIONAL);
980
				       PCI_DMA_BIDIRECTIONAL);
1093
 
981
 
1094
//       if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
982
//       if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
1095
//           gen6_ppgtt_unmap_pages(ppgtt);
983
//           gen6_ppgtt_unmap_pages(ppgtt);
1096
//           return -EIO;
984
//           return -EIO;
1097
//       }
985
//       }
1098
 
986
 
1099
		ppgtt->pt_dma_addr[i] = pt_addr;
987
		ppgtt->pt_dma_addr[i] = pt_addr;
1100
	}
988
	}
1101
 
989
 
1102
	return 0;
990
	return 0;
1103
}
991
}
1104
 
992
 
1105
static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
993
static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1106
{
994
{
1107
	struct drm_device *dev = ppgtt->base.dev;
995
	struct drm_device *dev = ppgtt->base.dev;
1108
	struct drm_i915_private *dev_priv = dev->dev_private;
996
	struct drm_i915_private *dev_priv = dev->dev_private;
1109
	int ret;
997
	int ret;
1110
 
998
 
1111
	ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
999
	ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
1112
	if (IS_GEN6(dev)) {
1000
	if (IS_GEN6(dev)) {
1113
	ppgtt->enable = gen6_ppgtt_enable;
-
 
1114
		ppgtt->switch_mm = gen6_mm_switch;
1001
		ppgtt->switch_mm = gen6_mm_switch;
1115
	} else if (IS_HASWELL(dev)) {
1002
	} else if (IS_HASWELL(dev)) {
1116
		ppgtt->enable = gen7_ppgtt_enable;
-
 
1117
		ppgtt->switch_mm = hsw_mm_switch;
1003
		ppgtt->switch_mm = hsw_mm_switch;
1118
	} else if (IS_GEN7(dev)) {
1004
	} else if (IS_GEN7(dev)) {
1119
		ppgtt->enable = gen7_ppgtt_enable;
-
 
1120
		ppgtt->switch_mm = gen7_mm_switch;
1005
		ppgtt->switch_mm = gen7_mm_switch;
1121
	} else
1006
	} else
1122
		BUG();
1007
		BUG();
1123
 
1008
 
1124
	ret = gen6_ppgtt_alloc(ppgtt);
1009
	ret = gen6_ppgtt_alloc(ppgtt);
1125
	if (ret)
1010
	if (ret)
1126
		return ret;
1011
		return ret;
1127
 
1012
 
1128
	ret = gen6_ppgtt_setup_page_tables(ppgtt);
1013
	ret = gen6_ppgtt_setup_page_tables(ppgtt);
1129
	if (ret) {
1014
	if (ret) {
1130
		gen6_ppgtt_free(ppgtt);
1015
		gen6_ppgtt_free(ppgtt);
1131
		return ret;
1016
		return ret;
1132
	}
1017
	}
1133
 
1018
 
1134
	ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1019
	ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1135
	ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
1020
	ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
1136
	ppgtt->base.cleanup = gen6_ppgtt_cleanup;
1021
	ppgtt->base.cleanup = gen6_ppgtt_cleanup;
1137
	ppgtt->base.start = 0;
1022
	ppgtt->base.start = 0;
1138
	ppgtt->base.total =  ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
1023
	ppgtt->base.total =  ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
1139
//	ppgtt->debug_dump = gen6_dump_ppgtt;
1024
//	ppgtt->debug_dump = gen6_dump_ppgtt;
1140
 
1025
 
1141
	ppgtt->pd_offset =
1026
	ppgtt->pd_offset =
1142
		ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
1027
		ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
1143
 
1028
 
1144
	ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
1029
	ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
1145
 
1030
 
1146
	DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
1031
	DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
1147
			 ppgtt->node.size >> 20,
1032
			 ppgtt->node.size >> 20,
1148
			 ppgtt->node.start / PAGE_SIZE);
1033
			 ppgtt->node.start / PAGE_SIZE);
-
 
1034
 
-
 
1035
	gen6_write_pdes(ppgtt);
-
 
1036
	DRM_DEBUG("Adding PPGTT at offset %x\n",
-
 
1037
		  ppgtt->pd_offset << 10);
1149
 
1038
 
1150
	return 0;
1039
	return 0;
1151
}
1040
}
1152
 
1041
 
1153
int i915_gem_init_ppgtt(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1042
static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1154
{
1043
{
1155
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
1156
	int ret = 0;
1044
	struct drm_i915_private *dev_priv = dev->dev_private;
1157
 
1045
 
1158
	ppgtt->base.dev = dev;
1046
	ppgtt->base.dev = dev;
1159
	ppgtt->base.scratch = dev_priv->gtt.base.scratch;
1047
	ppgtt->base.scratch = dev_priv->gtt.base.scratch;
1160
 
1048
 
1161
	if (INTEL_INFO(dev)->gen < 8)
1049
	if (INTEL_INFO(dev)->gen < 8)
1162
		ret = gen6_ppgtt_init(ppgtt);
1050
		return gen6_ppgtt_init(ppgtt);
1163
	else if (IS_GEN8(dev))
1051
	else if (IS_GEN8(dev) || IS_GEN9(dev))
1164
		ret = gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
1052
		return gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
1165
	else
1053
	else
1166
		BUG();
1054
		BUG();
1167
 
1055
}
-
 
1056
int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1168
	if (!ret) {
1057
{
1169
		struct drm_i915_private *dev_priv = dev->dev_private;
1058
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
1059
	int ret = 0;
-
 
1060
 
-
 
1061
	ret = __hw_ppgtt_init(dev, ppgtt);
-
 
1062
	if (ret == 0) {
1170
		kref_init(&ppgtt->ref);
1063
		kref_init(&ppgtt->ref);
1171
		drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1064
		drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1172
			    ppgtt->base.total);
1065
			    ppgtt->base.total);
1173
		i915_init_vm(dev_priv, &ppgtt->base);
1066
		i915_init_vm(dev_priv, &ppgtt->base);
1174
		if (INTEL_INFO(dev)->gen < 8) {
-
 
1175
			gen6_write_pdes(ppgtt);
-
 
1176
			DRM_DEBUG("Adding PPGTT at offset %x\n",
-
 
1177
				  ppgtt->pd_offset << 10);
-
 
1178
		}
1067
	}
-
 
1068
 
-
 
1069
	return ret;
1179
	}
1070
}
-
 
1071
 
-
 
1072
int i915_ppgtt_init_hw(struct drm_device *dev)
-
 
1073
{
-
 
1074
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
1075
	struct intel_engine_cs *ring;
-
 
1076
	struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
-
 
1077
	int i, ret = 0;
-
 
1078
 
-
 
1079
	/* In the case of execlists, PPGTT is enabled by the context descriptor
-
 
1080
	 * and the PDPs are contained within the context itself.  We don't
-
 
1081
	 * need to do anything here. */
-
 
1082
	if (i915.enable_execlists)
-
 
1083
		return 0;
-
 
1084
 
-
 
1085
	if (!USES_PPGTT(dev))
-
 
1086
		return 0;
-
 
1087
 
-
 
1088
	if (IS_GEN6(dev))
-
 
1089
		gen6_ppgtt_enable(dev);
-
 
1090
	else if (IS_GEN7(dev))
-
 
1091
		gen7_ppgtt_enable(dev);
-
 
1092
	else if (INTEL_INFO(dev)->gen >= 8)
-
 
1093
		gen8_ppgtt_enable(dev);
-
 
1094
	else
-
 
1095
		WARN_ON(1);
-
 
1096
 
-
 
1097
	if (ppgtt) {
-
 
1098
		for_each_ring(ring, dev_priv, i) {
-
 
1099
			ret = ppgtt->switch_mm(ppgtt, ring);
1180
 
1100
			if (ret != 0)
1181
	return ret;
1101
				return ret;
-
 
1102
		}
-
 
1103
	}
-
 
1104
 
-
 
1105
	return ret;
-
 
1106
}
-
 
1107
struct i915_hw_ppgtt *
-
 
1108
i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
-
 
1109
{
-
 
1110
	struct i915_hw_ppgtt *ppgtt;
-
 
1111
	int ret;
-
 
1112
 
-
 
1113
	ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
-
 
1114
	if (!ppgtt)
-
 
1115
		return ERR_PTR(-ENOMEM);
-
 
1116
 
-
 
1117
	ret = i915_ppgtt_init(dev, ppgtt);
-
 
1118
	if (ret) {
-
 
1119
		kfree(ppgtt);
-
 
1120
		return ERR_PTR(ret);
-
 
1121
	}
-
 
1122
 
-
 
1123
	ppgtt->file_priv = fpriv;
-
 
1124
 
-
 
1125
	trace_i915_ppgtt_create(&ppgtt->base);
-
 
1126
 
-
 
1127
	return ppgtt;
-
 
1128
}
-
 
1129
 
-
 
1130
void  i915_ppgtt_release(struct kref *kref)
-
 
1131
{
-
 
1132
	struct i915_hw_ppgtt *ppgtt =
-
 
1133
		container_of(kref, struct i915_hw_ppgtt, ref);
-
 
1134
 
-
 
1135
	trace_i915_ppgtt_release(&ppgtt->base);
-
 
1136
 
-
 
1137
	/* vmas should already be unbound */
-
 
1138
	WARN_ON(!list_empty(&ppgtt->base.active_list));
-
 
1139
	WARN_ON(!list_empty(&ppgtt->base.inactive_list));
-
 
1140
 
-
 
1141
	list_del(&ppgtt->base.global_link);
-
 
1142
	drm_mm_takedown(&ppgtt->base.mm);
-
 
1143
 
-
 
1144
	ppgtt->base.cleanup(&ppgtt->base);
-
 
1145
	kfree(ppgtt);
1182
}
1146
}
1183
 
1147
 
1184
static void
1148
static void
1185
ppgtt_bind_vma(struct i915_vma *vma,
1149
ppgtt_bind_vma(struct i915_vma *vma,
1186
	       enum i915_cache_level cache_level,
1150
	       enum i915_cache_level cache_level,
1187
	       u32 flags)
1151
	       u32 flags)
1188
{
1152
{
1189
	/* Currently applicable only to VLV */
1153
	/* Currently applicable only to VLV */
1190
	if (vma->obj->gt_ro)
1154
	if (vma->obj->gt_ro)
1191
		flags |= PTE_READ_ONLY;
1155
		flags |= PTE_READ_ONLY;
1192
 
1156
 
1193
	vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
1157
	vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
1194
				cache_level, flags);
1158
				cache_level, flags);
1195
}
1159
}
1196
 
1160
 
1197
static void ppgtt_unbind_vma(struct i915_vma *vma)
1161
static void ppgtt_unbind_vma(struct i915_vma *vma)
1198
{
1162
{
1199
	vma->vm->clear_range(vma->vm,
1163
	vma->vm->clear_range(vma->vm,
1200
			     vma->node.start,
1164
			     vma->node.start,
1201
			     vma->obj->base.size,
1165
			     vma->obj->base.size,
1202
				true);
1166
				true);
1203
}
1167
}
1204
 
1168
 
1205
extern int intel_iommu_gfx_mapped;
1169
extern int intel_iommu_gfx_mapped;
1206
/* Certain Gen5 chipsets require require idling the GPU before
1170
/* Certain Gen5 chipsets require require idling the GPU before
1207
 * unmapping anything from the GTT when VT-d is enabled.
1171
 * unmapping anything from the GTT when VT-d is enabled.
1208
 */
1172
 */
1209
static inline bool needs_idle_maps(struct drm_device *dev)
1173
static inline bool needs_idle_maps(struct drm_device *dev)
1210
{
1174
{
1211
#ifdef CONFIG_INTEL_IOMMU
1175
#ifdef CONFIG_INTEL_IOMMU
1212
	/* Query intel_iommu to see if we need the workaround. Presumably that
1176
	/* Query intel_iommu to see if we need the workaround. Presumably that
1213
	 * was loaded first.
1177
	 * was loaded first.
1214
	 */
1178
	 */
1215
	if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1179
	if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1216
		return true;
1180
		return true;
1217
#endif
1181
#endif
1218
	return false;
1182
	return false;
1219
}
1183
}
1220
 
1184
 
1221
static bool do_idling(struct drm_i915_private *dev_priv)
1185
static bool do_idling(struct drm_i915_private *dev_priv)
1222
{
1186
{
1223
	bool ret = dev_priv->mm.interruptible;
1187
	bool ret = dev_priv->mm.interruptible;
1224
 
1188
 
1225
	if (unlikely(dev_priv->gtt.do_idle_maps)) {
1189
	if (unlikely(dev_priv->gtt.do_idle_maps)) {
1226
		dev_priv->mm.interruptible = false;
1190
		dev_priv->mm.interruptible = false;
1227
		if (i915_gpu_idle(dev_priv->dev)) {
1191
		if (i915_gpu_idle(dev_priv->dev)) {
1228
			DRM_ERROR("Couldn't idle GPU\n");
1192
			DRM_ERROR("Couldn't idle GPU\n");
1229
			/* Wait a bit, in hopes it avoids the hang */
1193
			/* Wait a bit, in hopes it avoids the hang */
1230
			udelay(10);
1194
			udelay(10);
1231
		}
1195
		}
1232
	}
1196
	}
1233
 
1197
 
1234
	return ret;
1198
	return ret;
1235
}
1199
}
1236
 
1200
 
1237
static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1201
static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1238
{
1202
{
1239
	if (unlikely(dev_priv->gtt.do_idle_maps))
1203
	if (unlikely(dev_priv->gtt.do_idle_maps))
1240
		dev_priv->mm.interruptible = interruptible;
1204
		dev_priv->mm.interruptible = interruptible;
1241
}
1205
}
1242
 
1206
 
1243
void i915_check_and_clear_faults(struct drm_device *dev)
1207
void i915_check_and_clear_faults(struct drm_device *dev)
1244
{
1208
{
1245
	struct drm_i915_private *dev_priv = dev->dev_private;
1209
	struct drm_i915_private *dev_priv = dev->dev_private;
1246
	struct intel_engine_cs *ring;
1210
	struct intel_engine_cs *ring;
1247
	int i;
1211
	int i;
1248
 
1212
 
1249
	if (INTEL_INFO(dev)->gen < 6)
1213
	if (INTEL_INFO(dev)->gen < 6)
1250
		return;
1214
		return;
1251
 
1215
 
1252
	for_each_ring(ring, dev_priv, i) {
1216
	for_each_ring(ring, dev_priv, i) {
1253
		u32 fault_reg;
1217
		u32 fault_reg;
1254
		fault_reg = I915_READ(RING_FAULT_REG(ring));
1218
		fault_reg = I915_READ(RING_FAULT_REG(ring));
1255
		if (fault_reg & RING_FAULT_VALID) {
1219
		if (fault_reg & RING_FAULT_VALID) {
1256
			DRM_DEBUG_DRIVER("Unexpected fault\n"
1220
			DRM_DEBUG_DRIVER("Unexpected fault\n"
1257
					 "\tAddr: 0x%08lx\\n"
1221
					 "\tAddr: 0x%08lx\n"
1258
					 "\tAddress space: %s\n"
1222
					 "\tAddress space: %s\n"
1259
					 "\tSource ID: %d\n"
1223
					 "\tSource ID: %d\n"
1260
					 "\tType: %d\n",
1224
					 "\tType: %d\n",
1261
					 fault_reg & PAGE_MASK,
1225
					 fault_reg & PAGE_MASK,
1262
					 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1226
					 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1263
					 RING_FAULT_SRCID(fault_reg),
1227
					 RING_FAULT_SRCID(fault_reg),
1264
					 RING_FAULT_FAULT_TYPE(fault_reg));
1228
					 RING_FAULT_FAULT_TYPE(fault_reg));
1265
			I915_WRITE(RING_FAULT_REG(ring),
1229
			I915_WRITE(RING_FAULT_REG(ring),
1266
				   fault_reg & ~RING_FAULT_VALID);
1230
				   fault_reg & ~RING_FAULT_VALID);
1267
		}
1231
		}
1268
	}
1232
	}
1269
	POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1233
	POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1270
}
1234
}
-
 
1235
 
-
 
1236
static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
-
 
1237
{
-
 
1238
	if (INTEL_INFO(dev_priv->dev)->gen < 6) {
-
 
1239
		intel_gtt_chipset_flush();
-
 
1240
	} else {
-
 
1241
		I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
-
 
1242
		POSTING_READ(GFX_FLSH_CNTL_GEN6);
-
 
1243
	}
-
 
1244
}
1271
 
1245
 
1272
void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1246
void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1273
{
1247
{
1274
	struct drm_i915_private *dev_priv = dev->dev_private;
1248
	struct drm_i915_private *dev_priv = dev->dev_private;
1275
 
1249
 
1276
	/* Don't bother messing with faults pre GEN6 as we have little
1250
	/* Don't bother messing with faults pre GEN6 as we have little
1277
	 * documentation supporting that it's a good idea.
1251
	 * documentation supporting that it's a good idea.
1278
	 */
1252
	 */
1279
	if (INTEL_INFO(dev)->gen < 6)
1253
	if (INTEL_INFO(dev)->gen < 6)
1280
		return;
1254
		return;
1281
 
1255
 
1282
	i915_check_and_clear_faults(dev);
1256
	i915_check_and_clear_faults(dev);
1283
 
1257
 
1284
	dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1258
	dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1285
				       dev_priv->gtt.base.start,
1259
				       dev_priv->gtt.base.start,
1286
				       dev_priv->gtt.base.total,
1260
				       dev_priv->gtt.base.total,
1287
				       true);
1261
				       true);
-
 
1262
 
-
 
1263
	i915_ggtt_flush(dev_priv);
1288
}
1264
}
1289
 
1265
 
1290
void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1266
void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1291
{
1267
{
1292
	struct drm_i915_private *dev_priv = dev->dev_private;
1268
	struct drm_i915_private *dev_priv = dev->dev_private;
1293
	struct drm_i915_gem_object *obj;
1269
	struct drm_i915_gem_object *obj;
1294
	struct i915_address_space *vm;
1270
	struct i915_address_space *vm;
1295
 
1271
 
1296
	i915_check_and_clear_faults(dev);
1272
	i915_check_and_clear_faults(dev);
1297
 
1273
 
1298
	/* First fill our portion of the GTT with scratch pages */
1274
	/* First fill our portion of the GTT with scratch pages */
1299
	dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1275
	dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1300
				       dev_priv->gtt.base.start,
1276
				       dev_priv->gtt.base.start,
1301
				       dev_priv->gtt.base.total,
1277
				       dev_priv->gtt.base.total,
1302
				       true);
1278
				       true);
1303
 
1279
 
1304
	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
1280
	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
1305
		struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1281
		struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1306
							   &dev_priv->gtt.base);
1282
							   &dev_priv->gtt.base);
1307
		if (!vma)
1283
		if (!vma)
1308
			continue;
1284
			continue;
1309
 
1285
 
1310
		i915_gem_clflush_object(obj, obj->pin_display);
1286
		i915_gem_clflush_object(obj, obj->pin_display);
1311
		/* The bind_vma code tries to be smart about tracking mappings.
1287
		/* The bind_vma code tries to be smart about tracking mappings.
1312
		 * Unfortunately above, we've just wiped out the mappings
1288
		 * Unfortunately above, we've just wiped out the mappings
1313
		 * without telling our object about it. So we need to fake it.
1289
		 * without telling our object about it. So we need to fake it.
1314
		 */
1290
		 */
1315
		obj->has_global_gtt_mapping = 0;
1291
		vma->bound &= ~GLOBAL_BIND;
1316
		vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
1292
		vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
1317
	}
1293
	}
1318
 
1294
 
1319
 
1295
 
1320
	if (INTEL_INFO(dev)->gen >= 8) {
1296
	if (INTEL_INFO(dev)->gen >= 8) {
1321
		if (IS_CHERRYVIEW(dev))
1297
		if (IS_CHERRYVIEW(dev))
1322
			chv_setup_private_ppat(dev_priv);
1298
			chv_setup_private_ppat(dev_priv);
1323
		else
1299
		else
1324
			bdw_setup_private_ppat(dev_priv);
1300
			bdw_setup_private_ppat(dev_priv);
1325
 
1301
 
1326
		return;
1302
		return;
1327
	}
1303
	}
1328
 
1304
 
1329
	list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1305
	list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1330
		/* TODO: Perhaps it shouldn't be gen6 specific */
1306
		/* TODO: Perhaps it shouldn't be gen6 specific */
1331
		if (i915_is_ggtt(vm)) {
1307
		if (i915_is_ggtt(vm)) {
1332
			if (dev_priv->mm.aliasing_ppgtt)
1308
			if (dev_priv->mm.aliasing_ppgtt)
1333
				gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
1309
				gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
1334
			continue;
1310
			continue;
1335
		}
1311
		}
1336
 
1312
 
1337
		gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
1313
		gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
1338
	}
1314
	}
1339
 
1315
 
1340
	i915_gem_chipset_flush(dev);
1316
	i915_ggtt_flush(dev_priv);
1341
}
1317
}
1342
 
1318
 
1343
int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
1319
int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
1344
{
1320
{
1345
	if (obj->has_dma_mapping)
1321
	if (obj->has_dma_mapping)
1346
		return 0;
1322
		return 0;
1347
 
1323
 
1348
	if (!dma_map_sg(&obj->base.dev->pdev->dev,
1324
	if (!dma_map_sg(&obj->base.dev->pdev->dev,
1349
			obj->pages->sgl, obj->pages->nents,
1325
			obj->pages->sgl, obj->pages->nents,
1350
			PCI_DMA_BIDIRECTIONAL))
1326
			PCI_DMA_BIDIRECTIONAL))
1351
		return -ENOSPC;
1327
		return -ENOSPC;
1352
 
1328
 
1353
	return 0;
1329
	return 0;
1354
}
1330
}
1355
 
1331
 
1356
static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
1332
static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
1357
{
1333
{
1358
#ifdef writeq
1334
#ifdef writeq
1359
	writeq(pte, addr);
1335
	writeq(pte, addr);
1360
#else
1336
#else
1361
	iowrite32((u32)pte, addr);
1337
	iowrite32((u32)pte, addr);
1362
	iowrite32(pte >> 32, addr + 4);
1338
	iowrite32(pte >> 32, addr + 4);
1363
#endif
1339
#endif
1364
}
1340
}
1365
 
1341
 
1366
static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1342
static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1367
				     struct sg_table *st,
1343
				     struct sg_table *st,
1368
				     uint64_t start,
1344
				     uint64_t start,
1369
				     enum i915_cache_level level, u32 unused)
1345
				     enum i915_cache_level level, u32 unused)
1370
{
1346
{
1371
	struct drm_i915_private *dev_priv = vm->dev->dev_private;
1347
	struct drm_i915_private *dev_priv = vm->dev->dev_private;
1372
	unsigned first_entry = start >> PAGE_SHIFT;
1348
	unsigned first_entry = start >> PAGE_SHIFT;
1373
	gen8_gtt_pte_t __iomem *gtt_entries =
1349
	gen8_gtt_pte_t __iomem *gtt_entries =
1374
		(gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1350
		(gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1375
	int i = 0;
1351
	int i = 0;
1376
	struct sg_page_iter sg_iter;
1352
	struct sg_page_iter sg_iter;
1377
	dma_addr_t addr = 0; /* shut up gcc */
1353
	dma_addr_t addr = 0; /* shut up gcc */
1378
 
1354
 
1379
	for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1355
	for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1380
		addr = sg_dma_address(sg_iter.sg) +
1356
		addr = sg_dma_address(sg_iter.sg) +
1381
			(sg_iter.sg_pgoffset << PAGE_SHIFT);
1357
			(sg_iter.sg_pgoffset << PAGE_SHIFT);
1382
		gen8_set_pte(>t_entries[i],
1358
		gen8_set_pte(>t_entries[i],
1383
			     gen8_pte_encode(addr, level, true));
1359
			     gen8_pte_encode(addr, level, true));
1384
		i++;
1360
		i++;
1385
	}
1361
	}
1386
 
1362
 
1387
	/*
1363
	/*
1388
	 * XXX: This serves as a posting read to make sure that the PTE has
1364
	 * XXX: This serves as a posting read to make sure that the PTE has
1389
	 * actually been updated. There is some concern that even though
1365
	 * actually been updated. There is some concern that even though
1390
	 * registers and PTEs are within the same BAR that they are potentially
1366
	 * registers and PTEs are within the same BAR that they are potentially
1391
	 * of NUMA access patterns. Therefore, even with the way we assume
1367
	 * of NUMA access patterns. Therefore, even with the way we assume
1392
	 * hardware should work, we must keep this posting read for paranoia.
1368
	 * hardware should work, we must keep this posting read for paranoia.
1393
	 */
1369
	 */
1394
	if (i != 0)
1370
	if (i != 0)
1395
		WARN_ON(readq(>t_entries[i-1])
1371
		WARN_ON(readq(>t_entries[i-1])
1396
			!= gen8_pte_encode(addr, level, true));
1372
			!= gen8_pte_encode(addr, level, true));
1397
 
1373
 
1398
	/* This next bit makes the above posting read even more important. We
1374
	/* This next bit makes the above posting read even more important. We
1399
	 * want to flush the TLBs only after we're certain all the PTE updates
1375
	 * want to flush the TLBs only after we're certain all the PTE updates
1400
	 * have finished.
1376
	 * have finished.
1401
	 */
1377
	 */
1402
	I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1378
	I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1403
	POSTING_READ(GFX_FLSH_CNTL_GEN6);
1379
	POSTING_READ(GFX_FLSH_CNTL_GEN6);
1404
}
1380
}
1405
 
1381
 
1406
/*
1382
/*
1407
 * Binds an object into the global gtt with the specified cache level. The object
1383
 * Binds an object into the global gtt with the specified cache level. The object
1408
 * will be accessible to the GPU via commands whose operands reference offsets
1384
 * will be accessible to the GPU via commands whose operands reference offsets
1409
 * within the global GTT as well as accessible by the GPU through the GMADR
1385
 * within the global GTT as well as accessible by the GPU through the GMADR
1410
 * mapped BAR (dev_priv->mm.gtt->gtt).
1386
 * mapped BAR (dev_priv->mm.gtt->gtt).
1411
 */
1387
 */
1412
static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
1388
static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
1413
				     struct sg_table *st,
1389
				     struct sg_table *st,
1414
				     uint64_t start,
1390
				     uint64_t start,
1415
				     enum i915_cache_level level, u32 flags)
1391
				     enum i915_cache_level level, u32 flags)
1416
{
1392
{
1417
	struct drm_i915_private *dev_priv = vm->dev->dev_private;
1393
	struct drm_i915_private *dev_priv = vm->dev->dev_private;
1418
	unsigned first_entry = start >> PAGE_SHIFT;
1394
	unsigned first_entry = start >> PAGE_SHIFT;
1419
	gen6_gtt_pte_t __iomem *gtt_entries =
1395
	gen6_gtt_pte_t __iomem *gtt_entries =
1420
		(gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1396
		(gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1421
	int i = 0;
1397
	int i = 0;
1422
	struct sg_page_iter sg_iter;
1398
	struct sg_page_iter sg_iter;
1423
	dma_addr_t addr = 0;
1399
	dma_addr_t addr = 0;
1424
 
1400
 
1425
	for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1401
	for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1426
		addr = sg_page_iter_dma_address(&sg_iter);
1402
		addr = sg_page_iter_dma_address(&sg_iter);
1427
		iowrite32(vm->pte_encode(addr, level, true, flags), >t_entries[i]);
1403
		iowrite32(vm->pte_encode(addr, level, true, flags), >t_entries[i]);
1428
			i++;
1404
			i++;
1429
		}
1405
		}
1430
 
1406
 
1431
	/* XXX: This serves as a posting read to make sure that the PTE has
1407
	/* XXX: This serves as a posting read to make sure that the PTE has
1432
	 * actually been updated. There is some concern that even though
1408
	 * actually been updated. There is some concern that even though
1433
	 * registers and PTEs are within the same BAR that they are potentially
1409
	 * registers and PTEs are within the same BAR that they are potentially
1434
	 * of NUMA access patterns. Therefore, even with the way we assume
1410
	 * of NUMA access patterns. Therefore, even with the way we assume
1435
	 * hardware should work, we must keep this posting read for paranoia.
1411
	 * hardware should work, we must keep this posting read for paranoia.
1436
	 */
1412
	 */
1437
	if (i != 0) {
1413
	if (i != 0) {
1438
		unsigned long gtt = readl(>t_entries[i-1]);
1414
		unsigned long gtt = readl(>t_entries[i-1]);
1439
		WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
1415
		WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
1440
	}
1416
	}
1441
 
1417
 
1442
	/* This next bit makes the above posting read even more important. We
1418
	/* This next bit makes the above posting read even more important. We
1443
	 * want to flush the TLBs only after we're certain all the PTE updates
1419
	 * want to flush the TLBs only after we're certain all the PTE updates
1444
	 * have finished.
1420
	 * have finished.
1445
	 */
1421
	 */
1446
	I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1422
	I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1447
	POSTING_READ(GFX_FLSH_CNTL_GEN6);
1423
	POSTING_READ(GFX_FLSH_CNTL_GEN6);
1448
}
1424
}
1449
 
1425
 
1450
static void gen8_ggtt_clear_range(struct i915_address_space *vm,
1426
static void gen8_ggtt_clear_range(struct i915_address_space *vm,
1451
				  uint64_t start,
1427
				  uint64_t start,
1452
				  uint64_t length,
1428
				  uint64_t length,
1453
				  bool use_scratch)
1429
				  bool use_scratch)
1454
{
1430
{
1455
	struct drm_i915_private *dev_priv = vm->dev->dev_private;
1431
	struct drm_i915_private *dev_priv = vm->dev->dev_private;
1456
	unsigned first_entry = start >> PAGE_SHIFT;
1432
	unsigned first_entry = start >> PAGE_SHIFT;
1457
	unsigned num_entries = length >> PAGE_SHIFT;
1433
	unsigned num_entries = length >> PAGE_SHIFT;
1458
	gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
1434
	gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
1459
		(gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1435
		(gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1460
	const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1436
	const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1461
	int i;
1437
	int i;
1462
 
1438
 
1463
	if (WARN(num_entries > max_entries,
1439
	if (WARN(num_entries > max_entries,
1464
		 "First entry = %d; Num entries = %d (max=%d)\n",
1440
		 "First entry = %d; Num entries = %d (max=%d)\n",
1465
		 first_entry, num_entries, max_entries))
1441
		 first_entry, num_entries, max_entries))
1466
		num_entries = max_entries;
1442
		num_entries = max_entries;
1467
 
1443
 
1468
	scratch_pte = gen8_pte_encode(vm->scratch.addr,
1444
	scratch_pte = gen8_pte_encode(vm->scratch.addr,
1469
				      I915_CACHE_LLC,
1445
				      I915_CACHE_LLC,
1470
				      use_scratch);
1446
				      use_scratch);
1471
	for (i = 0; i < num_entries; i++)
1447
	for (i = 0; i < num_entries; i++)
1472
		gen8_set_pte(>t_base[i], scratch_pte);
1448
		gen8_set_pte(>t_base[i], scratch_pte);
1473
	readl(gtt_base);
1449
	readl(gtt_base);
1474
}
1450
}
1475
 
1451
 
1476
static void gen6_ggtt_clear_range(struct i915_address_space *vm,
1452
static void gen6_ggtt_clear_range(struct i915_address_space *vm,
1477
				  uint64_t start,
1453
				  uint64_t start,
1478
				  uint64_t length,
1454
				  uint64_t length,
1479
				  bool use_scratch)
1455
				  bool use_scratch)
1480
{
1456
{
1481
	struct drm_i915_private *dev_priv = vm->dev->dev_private;
1457
	struct drm_i915_private *dev_priv = vm->dev->dev_private;
1482
	unsigned first_entry = start >> PAGE_SHIFT;
1458
	unsigned first_entry = start >> PAGE_SHIFT;
1483
	unsigned num_entries = length >> PAGE_SHIFT;
1459
	unsigned num_entries = length >> PAGE_SHIFT;
1484
	gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
1460
	gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
1485
		(gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1461
		(gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1486
	const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1462
	const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1487
	int i;
1463
	int i;
1488
 
1464
 
1489
	if (WARN(num_entries > max_entries,
1465
	if (WARN(num_entries > max_entries,
1490
		 "First entry = %d; Num entries = %d (max=%d)\n",
1466
		 "First entry = %d; Num entries = %d (max=%d)\n",
1491
		 first_entry, num_entries, max_entries))
1467
		 first_entry, num_entries, max_entries))
1492
        num_entries = max_entries;
1468
        num_entries = max_entries;
1493
 
1469
 
1494
	scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
1470
	scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
1495
 
1471
 
1496
	for (i = 0; i < num_entries; i++)
1472
	for (i = 0; i < num_entries; i++)
1497
		iowrite32(scratch_pte, >t_base[i]);
1473
		iowrite32(scratch_pte, >t_base[i]);
1498
	readl(gtt_base);
1474
	readl(gtt_base);
1499
}
1475
}
1500
 
1476
 
1501
 
1477
 
1502
static void i915_ggtt_bind_vma(struct i915_vma *vma,
1478
static void i915_ggtt_bind_vma(struct i915_vma *vma,
1503
			       enum i915_cache_level cache_level,
1479
			       enum i915_cache_level cache_level,
1504
			       u32 unused)
1480
			       u32 unused)
1505
{
1481
{
1506
	const unsigned long entry = vma->node.start >> PAGE_SHIFT;
1482
	const unsigned long entry = vma->node.start >> PAGE_SHIFT;
1507
	unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1483
	unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1508
		AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1484
		AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1509
 
1485
 
1510
	BUG_ON(!i915_is_ggtt(vma->vm));
1486
	BUG_ON(!i915_is_ggtt(vma->vm));
1511
	intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags);
1487
	intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags);
1512
	vma->obj->has_global_gtt_mapping = 1;
1488
	vma->bound = GLOBAL_BIND;
1513
}
1489
}
1514
 
1490
 
1515
static void i915_ggtt_clear_range(struct i915_address_space *vm,
1491
static void i915_ggtt_clear_range(struct i915_address_space *vm,
1516
				  uint64_t start,
1492
				  uint64_t start,
1517
				  uint64_t length,
1493
				  uint64_t length,
1518
				  bool unused)
1494
				  bool unused)
1519
{
1495
{
1520
	unsigned first_entry = start >> PAGE_SHIFT;
1496
	unsigned first_entry = start >> PAGE_SHIFT;
1521
	unsigned num_entries = length >> PAGE_SHIFT;
1497
	unsigned num_entries = length >> PAGE_SHIFT;
1522
	intel_gtt_clear_range(first_entry, num_entries);
1498
	intel_gtt_clear_range(first_entry, num_entries);
1523
}
1499
}
1524
 
1500
 
1525
static void i915_ggtt_unbind_vma(struct i915_vma *vma)
1501
static void i915_ggtt_unbind_vma(struct i915_vma *vma)
1526
{
1502
{
1527
	const unsigned int first = vma->node.start >> PAGE_SHIFT;
1503
	const unsigned int first = vma->node.start >> PAGE_SHIFT;
1528
	const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
1504
	const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
1529
 
1505
 
1530
	BUG_ON(!i915_is_ggtt(vma->vm));
1506
	BUG_ON(!i915_is_ggtt(vma->vm));
1531
	vma->obj->has_global_gtt_mapping = 0;
1507
	vma->bound = 0;
1532
	intel_gtt_clear_range(first, size);
1508
	intel_gtt_clear_range(first, size);
1533
}
1509
}
1534
 
1510
 
1535
static void ggtt_bind_vma(struct i915_vma *vma,
1511
static void ggtt_bind_vma(struct i915_vma *vma,
1536
			  enum i915_cache_level cache_level,
1512
			  enum i915_cache_level cache_level,
1537
			  u32 flags)
1513
			  u32 flags)
1538
{
1514
{
1539
	struct drm_device *dev = vma->vm->dev;
1515
	struct drm_device *dev = vma->vm->dev;
1540
	struct drm_i915_private *dev_priv = dev->dev_private;
1516
	struct drm_i915_private *dev_priv = dev->dev_private;
1541
	struct drm_i915_gem_object *obj = vma->obj;
1517
	struct drm_i915_gem_object *obj = vma->obj;
1542
 
1518
 
1543
	/* Currently applicable only to VLV */
1519
	/* Currently applicable only to VLV */
1544
	if (obj->gt_ro)
1520
	if (obj->gt_ro)
1545
		flags |= PTE_READ_ONLY;
1521
		flags |= PTE_READ_ONLY;
1546
 
1522
 
1547
	/* If there is no aliasing PPGTT, or the caller needs a global mapping,
1523
	/* If there is no aliasing PPGTT, or the caller needs a global mapping,
1548
	 * or we have a global mapping already but the cacheability flags have
1524
	 * or we have a global mapping already but the cacheability flags have
1549
	 * changed, set the global PTEs.
1525
	 * changed, set the global PTEs.
1550
	 *
1526
	 *
1551
	 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1527
	 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1552
	 * instead if none of the above hold true.
1528
	 * instead if none of the above hold true.
1553
	 *
1529
	 *
1554
	 * NB: A global mapping should only be needed for special regions like
1530
	 * NB: A global mapping should only be needed for special regions like
1555
	 * "gtt mappable", SNB errata, or if specified via special execbuf
1531
	 * "gtt mappable", SNB errata, or if specified via special execbuf
1556
	 * flags. At all other times, the GPU will use the aliasing PPGTT.
1532
	 * flags. At all other times, the GPU will use the aliasing PPGTT.
1557
	 */
1533
	 */
1558
	if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
1534
	if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
1559
		if (!obj->has_global_gtt_mapping ||
1535
		if (!(vma->bound & GLOBAL_BIND) ||
1560
		    (cache_level != obj->cache_level)) {
1536
		    (cache_level != obj->cache_level)) {
1561
			vma->vm->insert_entries(vma->vm, obj->pages,
1537
			vma->vm->insert_entries(vma->vm, obj->pages,
1562
						vma->node.start,
1538
						vma->node.start,
1563
						cache_level, flags);
1539
						cache_level, flags);
1564
			obj->has_global_gtt_mapping = 1;
1540
			vma->bound |= GLOBAL_BIND;
1565
		}
1541
		}
1566
	}
1542
	}
1567
 
1543
 
1568
	if (dev_priv->mm.aliasing_ppgtt &&
1544
	if (dev_priv->mm.aliasing_ppgtt &&
1569
	    (!obj->has_aliasing_ppgtt_mapping ||
1545
	    (!(vma->bound & LOCAL_BIND) ||
1570
	     (cache_level != obj->cache_level))) {
1546
	     (cache_level != obj->cache_level))) {
1571
		struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1547
		struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1572
		appgtt->base.insert_entries(&appgtt->base,
1548
		appgtt->base.insert_entries(&appgtt->base,
1573
					    vma->obj->pages,
1549
					    vma->obj->pages,
1574
					    vma->node.start,
1550
					    vma->node.start,
1575
					    cache_level, flags);
1551
					    cache_level, flags);
1576
		vma->obj->has_aliasing_ppgtt_mapping = 1;
1552
		vma->bound |= LOCAL_BIND;
1577
	}
1553
	}
1578
}
1554
}
1579
 
1555
 
1580
static void ggtt_unbind_vma(struct i915_vma *vma)
1556
static void ggtt_unbind_vma(struct i915_vma *vma)
1581
{
1557
{
1582
	struct drm_device *dev = vma->vm->dev;
1558
	struct drm_device *dev = vma->vm->dev;
1583
	struct drm_i915_private *dev_priv = dev->dev_private;
1559
	struct drm_i915_private *dev_priv = dev->dev_private;
1584
	struct drm_i915_gem_object *obj = vma->obj;
1560
	struct drm_i915_gem_object *obj = vma->obj;
1585
 
1561
 
1586
	if (obj->has_global_gtt_mapping) {
1562
	if (vma->bound & GLOBAL_BIND) {
1587
		vma->vm->clear_range(vma->vm,
1563
		vma->vm->clear_range(vma->vm,
1588
				     vma->node.start,
1564
				     vma->node.start,
1589
				     obj->base.size,
1565
				     obj->base.size,
1590
				       true);
1566
				       true);
1591
		obj->has_global_gtt_mapping = 0;
1567
		vma->bound &= ~GLOBAL_BIND;
1592
	}
1568
	}
1593
 
1569
 
1594
	if (obj->has_aliasing_ppgtt_mapping) {
1570
	if (vma->bound & LOCAL_BIND) {
1595
		struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1571
		struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1596
		appgtt->base.clear_range(&appgtt->base,
1572
		appgtt->base.clear_range(&appgtt->base,
1597
					 vma->node.start,
1573
					 vma->node.start,
1598
					 obj->base.size,
1574
					 obj->base.size,
1599
					 true);
1575
					 true);
1600
		obj->has_aliasing_ppgtt_mapping = 0;
1576
		vma->bound &= ~LOCAL_BIND;
1601
	}
1577
	}
1602
}
1578
}
1603
 
1579
 
1604
void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1580
void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1605
{
1581
{
1606
	struct drm_device *dev = obj->base.dev;
1582
	struct drm_device *dev = obj->base.dev;
1607
	struct drm_i915_private *dev_priv = dev->dev_private;
1583
	struct drm_i915_private *dev_priv = dev->dev_private;
1608
	bool interruptible;
1584
	bool interruptible;
1609
 
1585
 
1610
	interruptible = do_idling(dev_priv);
1586
	interruptible = do_idling(dev_priv);
1611
 
1587
 
1612
	if (!obj->has_dma_mapping)
1588
	if (!obj->has_dma_mapping)
1613
		dma_unmap_sg(&dev->pdev->dev,
1589
		dma_unmap_sg(&dev->pdev->dev,
1614
			     obj->pages->sgl, obj->pages->nents,
1590
			     obj->pages->sgl, obj->pages->nents,
1615
			     PCI_DMA_BIDIRECTIONAL);
1591
			     PCI_DMA_BIDIRECTIONAL);
1616
 
1592
 
1617
	undo_idling(dev_priv, interruptible);
1593
	undo_idling(dev_priv, interruptible);
1618
}
1594
}
1619
 
1595
 
1620
static void i915_gtt_color_adjust(struct drm_mm_node *node,
1596
static void i915_gtt_color_adjust(struct drm_mm_node *node,
1621
				  unsigned long color,
1597
				  unsigned long color,
1622
				  unsigned long *start,
1598
				  unsigned long *start,
1623
				  unsigned long *end)
1599
				  unsigned long *end)
1624
{
1600
{
1625
	if (node->color != color)
1601
	if (node->color != color)
1626
		*start += 4096;
1602
		*start += 4096;
1627
 
1603
 
1628
	if (!list_empty(&node->node_list)) {
1604
	if (!list_empty(&node->node_list)) {
1629
		node = list_entry(node->node_list.next,
1605
		node = list_entry(node->node_list.next,
1630
				  struct drm_mm_node,
1606
				  struct drm_mm_node,
1631
				  node_list);
1607
				  node_list);
1632
		if (node->allocated && node->color != color)
1608
		if (node->allocated && node->color != color)
1633
			*end -= 4096;
1609
			*end -= 4096;
1634
	}
1610
	}
1635
}
1611
}
1636
 
1612
 
1637
void i915_gem_setup_global_gtt(struct drm_device *dev,
1613
static int i915_gem_setup_global_gtt(struct drm_device *dev,
1638
			      unsigned long start,
1614
			      unsigned long start,
1639
			      unsigned long mappable_end,
1615
			      unsigned long mappable_end,
1640
			      unsigned long end)
1616
			      unsigned long end)
1641
{
1617
{
1642
	/* Let GEM Manage all of the aperture.
1618
	/* Let GEM Manage all of the aperture.
1643
	 *
1619
	 *
1644
	 * However, leave one page at the end still bound to the scratch page.
1620
	 * However, leave one page at the end still bound to the scratch page.
1645
	 * There are a number of places where the hardware apparently prefetches
1621
	 * There are a number of places where the hardware apparently prefetches
1646
	 * past the end of the object, and we've seen multiple hangs with the
1622
	 * past the end of the object, and we've seen multiple hangs with the
1647
	 * GPU head pointer stuck in a batchbuffer bound at the last page of the
1623
	 * GPU head pointer stuck in a batchbuffer bound at the last page of the
1648
	 * aperture.  One page should be enough to keep any prefetching inside
1624
	 * aperture.  One page should be enough to keep any prefetching inside
1649
	 * of the aperture.
1625
	 * of the aperture.
1650
	 */
1626
	 */
1651
	struct drm_i915_private *dev_priv = dev->dev_private;
1627
	struct drm_i915_private *dev_priv = dev->dev_private;
1652
	struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
1628
	struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
1653
	struct drm_mm_node *entry;
1629
	struct drm_mm_node *entry;
1654
	struct drm_i915_gem_object *obj;
1630
	struct drm_i915_gem_object *obj;
1655
	unsigned long hole_start, hole_end;
1631
	unsigned long hole_start, hole_end;
-
 
1632
	int ret;
1656
 
1633
 
1657
	BUG_ON(mappable_end > end);
1634
	BUG_ON(mappable_end > end);
1658
 
1635
 
1659
	/* Subtract the guard page ... */
1636
	/* Subtract the guard page ... */
1660
	drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
1637
	drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
1661
	if (!HAS_LLC(dev))
1638
	if (!HAS_LLC(dev))
1662
		dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
1639
		dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
1663
 
1640
 
1664
	/* Mark any preallocated objects as occupied */
1641
	/* Mark any preallocated objects as occupied */
1665
	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
1642
	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
1666
		struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
1643
		struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
1667
		int ret;
1644
 
1668
		DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
1645
		DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
1669
			      i915_gem_obj_ggtt_offset(obj), obj->base.size);
1646
			      i915_gem_obj_ggtt_offset(obj), obj->base.size);
1670
 
1647
 
1671
		WARN_ON(i915_gem_obj_ggtt_bound(obj));
1648
		WARN_ON(i915_gem_obj_ggtt_bound(obj));
1672
		ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
1649
		ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
1673
		if (ret)
1650
		if (ret) {
1674
			DRM_DEBUG_KMS("Reservation failed\n");
1651
			DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
-
 
1652
			return ret;
-
 
1653
		}
1675
		obj->has_global_gtt_mapping = 1;
1654
		vma->bound |= GLOBAL_BIND;
1676
	}
1655
	}
1677
 
1656
 
1678
	dev_priv->gtt.base.start = start;
1657
	dev_priv->gtt.base.start = start;
1679
	dev_priv->gtt.base.total = end - start;
1658
	dev_priv->gtt.base.total = end - start;
1680
 
1659
 
1681
	/* Clear any non-preallocated blocks */
1660
	/* Clear any non-preallocated blocks */
1682
	drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
1661
	drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
1683
		DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
1662
		DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
1684
			      hole_start, hole_end);
1663
			      hole_start, hole_end);
1685
		ggtt_vm->clear_range(ggtt_vm, hole_start,
1664
		ggtt_vm->clear_range(ggtt_vm, hole_start,
1686
				     hole_end - hole_start, true);
1665
				     hole_end - hole_start, true);
1687
	}
1666
	}
1688
 
1667
 
1689
	/* And finally clear the reserved guard page */
1668
	/* And finally clear the reserved guard page */
1690
	ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
1669
	ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
-
 
1670
 
-
 
1671
	if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
-
 
1672
		struct i915_hw_ppgtt *ppgtt;
-
 
1673
 
-
 
1674
		ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
-
 
1675
		if (!ppgtt)
-
 
1676
			return -ENOMEM;
-
 
1677
 
-
 
1678
		ret = __hw_ppgtt_init(dev, ppgtt);
-
 
1679
		if (ret != 0)
-
 
1680
			return ret;
-
 
1681
 
-
 
1682
		dev_priv->mm.aliasing_ppgtt = ppgtt;
-
 
1683
	}
-
 
1684
 
-
 
1685
	return 0;
1691
}
1686
}
1692
 
1687
 
1693
void i915_gem_init_global_gtt(struct drm_device *dev)
1688
void i915_gem_init_global_gtt(struct drm_device *dev)
1694
{
1689
{
1695
	struct drm_i915_private *dev_priv = dev->dev_private;
1690
	struct drm_i915_private *dev_priv = dev->dev_private;
1696
	unsigned long gtt_size, mappable_size;
1691
	unsigned long gtt_size, mappable_size;
1697
 
1692
 
1698
	gtt_size = dev_priv->gtt.base.total;
1693
	gtt_size = dev_priv->gtt.base.total;
1699
	mappable_size = dev_priv->gtt.mappable_end;
1694
	mappable_size = dev_priv->gtt.mappable_end;
1700
 
1695
 
1701
	i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
1696
	i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
1702
}
1697
}
-
 
1698
 
-
 
1699
void i915_global_gtt_cleanup(struct drm_device *dev)
-
 
1700
{
-
 
1701
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
1702
	struct i915_address_space *vm = &dev_priv->gtt.base;
-
 
1703
 
-
 
1704
	if (dev_priv->mm.aliasing_ppgtt) {
-
 
1705
		struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
-
 
1706
 
-
 
1707
		ppgtt->base.cleanup(&ppgtt->base);
-
 
1708
	}
-
 
1709
 
-
 
1710
	if (drm_mm_initialized(&vm->mm)) {
-
 
1711
		drm_mm_takedown(&vm->mm);
-
 
1712
		list_del(&vm->global_link);
-
 
1713
	}
-
 
1714
 
-
 
1715
	vm->cleanup(vm);
-
 
1716
}
1703
 
1717
 
1704
static int setup_scratch_page(struct drm_device *dev)
1718
static int setup_scratch_page(struct drm_device *dev)
1705
{
1719
{
1706
	struct drm_i915_private *dev_priv = dev->dev_private;
1720
	struct drm_i915_private *dev_priv = dev->dev_private;
1707
	struct page *page;
1721
	struct page *page;
1708
	dma_addr_t dma_addr;
1722
	dma_addr_t dma_addr;
1709
 
1723
 
1710
	page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
1724
	page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
1711
	if (page == NULL)
1725
	if (page == NULL)
1712
		return -ENOMEM;
1726
		return -ENOMEM;
1713
    get_page(page);
-
 
1714
	set_pages_uc(page, 1);
1727
	set_pages_uc(page, 1);
1715
 
1728
 
1716
#ifdef CONFIG_INTEL_IOMMU
1729
#ifdef CONFIG_INTEL_IOMMU
1717
	dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
1730
	dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
1718
				PCI_DMA_BIDIRECTIONAL);
1731
				PCI_DMA_BIDIRECTIONAL);
1719
	if (pci_dma_mapping_error(dev->pdev, dma_addr))
1732
	if (pci_dma_mapping_error(dev->pdev, dma_addr))
1720
		return -EINVAL;
1733
		return -EINVAL;
1721
#else
1734
#else
1722
	dma_addr = page_to_phys(page);
1735
	dma_addr = page_to_phys(page);
1723
#endif
1736
#endif
1724
	dev_priv->gtt.base.scratch.page = page;
1737
	dev_priv->gtt.base.scratch.page = page;
1725
	dev_priv->gtt.base.scratch.addr = dma_addr;
1738
	dev_priv->gtt.base.scratch.addr = dma_addr;
1726
 
1739
 
1727
	return 0;
1740
	return 0;
1728
}
1741
}
1729
 
1742
 
1730
static void teardown_scratch_page(struct drm_device *dev)
1743
static void teardown_scratch_page(struct drm_device *dev)
1731
{
1744
{
1732
	struct drm_i915_private *dev_priv = dev->dev_private;
1745
	struct drm_i915_private *dev_priv = dev->dev_private;
1733
	struct page *page = dev_priv->gtt.base.scratch.page;
1746
	struct page *page = dev_priv->gtt.base.scratch.page;
1734
 
1747
 
1735
	set_pages_wb(page, 1);
1748
	set_pages_wb(page, 1);
1736
	pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
1749
	pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
1737
		       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
1750
		       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
1738
	put_page(page);
-
 
1739
	__free_page(page);
1751
	__free_page(page);
1740
}
1752
}
1741
 
1753
 
1742
static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1754
static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1743
{
1755
{
1744
	snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1756
	snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1745
	snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1757
	snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1746
	return snb_gmch_ctl << 20;
1758
	return snb_gmch_ctl << 20;
1747
}
1759
}
1748
 
1760
 
1749
static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1761
static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1750
{
1762
{
1751
	bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1763
	bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1752
	bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1764
	bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1753
	if (bdw_gmch_ctl)
1765
	if (bdw_gmch_ctl)
1754
		bdw_gmch_ctl = 1 << bdw_gmch_ctl;
1766
		bdw_gmch_ctl = 1 << bdw_gmch_ctl;
1755
 
1767
 
1756
#ifdef CONFIG_X86_32
1768
#ifdef CONFIG_X86_32
1757
	/* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
1769
	/* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
1758
	if (bdw_gmch_ctl > 4)
1770
	if (bdw_gmch_ctl > 4)
1759
		bdw_gmch_ctl = 4;
1771
		bdw_gmch_ctl = 4;
1760
#endif
1772
#endif
1761
 
1773
 
1762
	return bdw_gmch_ctl << 20;
1774
	return bdw_gmch_ctl << 20;
1763
}
1775
}
1764
 
1776
 
1765
static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
1777
static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
1766
{
1778
{
1767
	gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
1779
	gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
1768
	gmch_ctrl &= SNB_GMCH_GGMS_MASK;
1780
	gmch_ctrl &= SNB_GMCH_GGMS_MASK;
1769
 
1781
 
1770
	if (gmch_ctrl)
1782
	if (gmch_ctrl)
1771
		return 1 << (20 + gmch_ctrl);
1783
		return 1 << (20 + gmch_ctrl);
1772
 
1784
 
1773
	return 0;
1785
	return 0;
1774
}
1786
}
1775
 
1787
 
1776
static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
1788
static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
1777
{
1789
{
1778
	snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
1790
	snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
1779
	snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
1791
	snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
1780
	return snb_gmch_ctl << 25; /* 32 MB units */
1792
	return snb_gmch_ctl << 25; /* 32 MB units */
1781
}
1793
}
1782
 
1794
 
1783
static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
1795
static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
1784
{
1796
{
1785
	bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
1797
	bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
1786
	bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
1798
	bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
1787
	return bdw_gmch_ctl << 25; /* 32 MB units */
1799
	return bdw_gmch_ctl << 25; /* 32 MB units */
1788
}
1800
}
1789
 
1801
 
1790
static size_t chv_get_stolen_size(u16 gmch_ctrl)
1802
static size_t chv_get_stolen_size(u16 gmch_ctrl)
1791
{
1803
{
1792
	gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
1804
	gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
1793
	gmch_ctrl &= SNB_GMCH_GMS_MASK;
1805
	gmch_ctrl &= SNB_GMCH_GMS_MASK;
1794
 
1806
 
1795
	/*
1807
	/*
1796
	 * 0x0  to 0x10: 32MB increments starting at 0MB
1808
	 * 0x0  to 0x10: 32MB increments starting at 0MB
1797
	 * 0x11 to 0x16: 4MB increments starting at 8MB
1809
	 * 0x11 to 0x16: 4MB increments starting at 8MB
1798
	 * 0x17 to 0x1d: 4MB increments start at 36MB
1810
	 * 0x17 to 0x1d: 4MB increments start at 36MB
1799
	 */
1811
	 */
1800
	if (gmch_ctrl < 0x11)
1812
	if (gmch_ctrl < 0x11)
1801
		return gmch_ctrl << 25;
1813
		return gmch_ctrl << 25;
1802
	else if (gmch_ctrl < 0x17)
1814
	else if (gmch_ctrl < 0x17)
1803
		return (gmch_ctrl - 0x11 + 2) << 22;
1815
		return (gmch_ctrl - 0x11 + 2) << 22;
1804
	else
1816
	else
1805
		return (gmch_ctrl - 0x17 + 9) << 22;
1817
		return (gmch_ctrl - 0x17 + 9) << 22;
1806
}
1818
}
-
 
1819
 
-
 
1820
static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
-
 
1821
{
-
 
1822
	gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
-
 
1823
	gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
-
 
1824
 
-
 
1825
	if (gen9_gmch_ctl < 0xf0)
-
 
1826
		return gen9_gmch_ctl << 25; /* 32 MB units */
-
 
1827
	else
-
 
1828
		/* 4MB increments starting at 0xf0 for 4MB */
-
 
1829
		return (gen9_gmch_ctl - 0xf0 + 1) << 22;
-
 
1830
}
1807
 
1831
 
1808
static int ggtt_probe_common(struct drm_device *dev,
1832
static int ggtt_probe_common(struct drm_device *dev,
1809
			     size_t gtt_size)
1833
			     size_t gtt_size)
1810
{
1834
{
1811
	struct drm_i915_private *dev_priv = dev->dev_private;
1835
	struct drm_i915_private *dev_priv = dev->dev_private;
1812
	phys_addr_t gtt_phys_addr;
1836
	phys_addr_t gtt_phys_addr;
1813
	int ret;
1837
	int ret;
1814
 
1838
 
1815
	/* For Modern GENs the PTEs and register space are split in the BAR */
1839
	/* For Modern GENs the PTEs and register space are split in the BAR */
1816
	gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
1840
	gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
1817
		(pci_resource_len(dev->pdev, 0) / 2);
1841
		(pci_resource_len(dev->pdev, 0) / 2);
1818
 
1842
 
1819
	dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
1843
	dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
1820
	if (!dev_priv->gtt.gsm) {
1844
	if (!dev_priv->gtt.gsm) {
1821
		DRM_ERROR("Failed to map the gtt page table\n");
1845
		DRM_ERROR("Failed to map the gtt page table\n");
1822
		return -ENOMEM;
1846
		return -ENOMEM;
1823
	}
1847
	}
1824
 
1848
 
1825
	ret = setup_scratch_page(dev);
1849
	ret = setup_scratch_page(dev);
1826
	if (ret) {
1850
	if (ret) {
1827
		DRM_ERROR("Scratch setup failed\n");
1851
		DRM_ERROR("Scratch setup failed\n");
1828
		/* iounmap will also get called at remove, but meh */
1852
		/* iounmap will also get called at remove, but meh */
1829
		iounmap(dev_priv->gtt.gsm);
1853
		iounmap(dev_priv->gtt.gsm);
1830
	}
1854
	}
1831
 
1855
 
1832
	return ret;
1856
	return ret;
1833
}
1857
}
1834
 
1858
 
1835
/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
1859
/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
1836
 * bits. When using advanced contexts each context stores its own PAT, but
1860
 * bits. When using advanced contexts each context stores its own PAT, but
1837
 * writing this data shouldn't be harmful even in those cases. */
1861
 * writing this data shouldn't be harmful even in those cases. */
1838
static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
1862
static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
1839
{
1863
{
1840
	uint64_t pat;
1864
	uint64_t pat;
1841
 
1865
 
1842
	pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC)     | /* for normal objects, no eLLC */
1866
	pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC)     | /* for normal objects, no eLLC */
1843
	      GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
1867
	      GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
1844
	      GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
1868
	      GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
1845
	      GEN8_PPAT(3, GEN8_PPAT_UC)                     | /* Uncached objects, mostly for scanout */
1869
	      GEN8_PPAT(3, GEN8_PPAT_UC)                     | /* Uncached objects, mostly for scanout */
1846
	      GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
1870
	      GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
1847
	      GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
1871
	      GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
1848
	      GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
1872
	      GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
1849
	      GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
1873
	      GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
-
 
1874
 
-
 
1875
	if (!USES_PPGTT(dev_priv->dev))
-
 
1876
		/* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
-
 
1877
		 * so RTL will always use the value corresponding to
-
 
1878
		 * pat_sel = 000".
-
 
1879
		 * So let's disable cache for GGTT to avoid screen corruptions.
-
 
1880
		 * MOCS still can be used though.
-
 
1881
		 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
-
 
1882
		 * before this patch, i.e. the same uncached + snooping access
-
 
1883
		 * like on gen6/7 seems to be in effect.
-
 
1884
		 * - So this just fixes blitter/render access. Again it looks
-
 
1885
		 * like it's not just uncached access, but uncached + snooping.
-
 
1886
		 * So we can still hold onto all our assumptions wrt cpu
-
 
1887
		 * clflushing on LLC machines.
-
 
1888
		 */
-
 
1889
		pat = GEN8_PPAT(0, GEN8_PPAT_UC);
1850
 
1890
 
1851
	/* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
1891
	/* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
1852
	 * write would work. */
1892
	 * write would work. */
1853
	I915_WRITE(GEN8_PRIVATE_PAT, pat);
1893
	I915_WRITE(GEN8_PRIVATE_PAT, pat);
1854
	I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1894
	I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1855
}
1895
}
1856
 
1896
 
1857
static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
1897
static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
1858
{
1898
{
1859
	uint64_t pat;
1899
	uint64_t pat;
1860
 
1900
 
1861
	/*
1901
	/*
1862
	 * Map WB on BDW to snooped on CHV.
1902
	 * Map WB on BDW to snooped on CHV.
1863
	 *
1903
	 *
1864
	 * Only the snoop bit has meaning for CHV, the rest is
1904
	 * Only the snoop bit has meaning for CHV, the rest is
1865
	 * ignored.
1905
	 * ignored.
1866
	 *
1906
	 *
1867
	 * Note that the harware enforces snooping for all page
1907
	 * The hardware will never snoop for certain types of accesses:
-
 
1908
	 * - CPU GTT (GMADR->GGTT->no snoop->memory)
-
 
1909
	 * - PPGTT page tables
-
 
1910
	 * - some other special cycles
-
 
1911
	 *
-
 
1912
	 * As with BDW, we also need to consider the following for GT accesses:
-
 
1913
	 * "For GGTT, there is NO pat_sel[2:0] from the entry,
1868
	 * table accesses. The snoop bit is actually ignored for
1914
	 * so RTL will always use the value corresponding to
1869
	 * PDEs.
1915
	 * pat_sel = 000".
-
 
1916
	 * Which means we must set the snoop bit in PAT entry 0
-
 
1917
	 * in order to keep the global status page working.
1870
	 */
1918
	 */
1871
	pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
1919
	pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
1872
	      GEN8_PPAT(1, 0) |
1920
	      GEN8_PPAT(1, 0) |
1873
	      GEN8_PPAT(2, 0) |
1921
	      GEN8_PPAT(2, 0) |
1874
	      GEN8_PPAT(3, 0) |
1922
	      GEN8_PPAT(3, 0) |
1875
	      GEN8_PPAT(4, CHV_PPAT_SNOOP) |
1923
	      GEN8_PPAT(4, CHV_PPAT_SNOOP) |
1876
	      GEN8_PPAT(5, CHV_PPAT_SNOOP) |
1924
	      GEN8_PPAT(5, CHV_PPAT_SNOOP) |
1877
	      GEN8_PPAT(6, CHV_PPAT_SNOOP) |
1925
	      GEN8_PPAT(6, CHV_PPAT_SNOOP) |
1878
	      GEN8_PPAT(7, CHV_PPAT_SNOOP);
1926
	      GEN8_PPAT(7, CHV_PPAT_SNOOP);
1879
 
1927
 
1880
	I915_WRITE(GEN8_PRIVATE_PAT, pat);
1928
	I915_WRITE(GEN8_PRIVATE_PAT, pat);
1881
	I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1929
	I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1882
}
1930
}
1883
 
1931
 
1884
static int gen8_gmch_probe(struct drm_device *dev,
1932
static int gen8_gmch_probe(struct drm_device *dev,
1885
			   size_t *gtt_total,
1933
			   size_t *gtt_total,
1886
			   size_t *stolen,
1934
			   size_t *stolen,
1887
			   phys_addr_t *mappable_base,
1935
			   phys_addr_t *mappable_base,
1888
			   unsigned long *mappable_end)
1936
			   unsigned long *mappable_end)
1889
{
1937
{
1890
	struct drm_i915_private *dev_priv = dev->dev_private;
1938
	struct drm_i915_private *dev_priv = dev->dev_private;
1891
	unsigned int gtt_size;
1939
	unsigned int gtt_size;
1892
	u16 snb_gmch_ctl;
1940
	u16 snb_gmch_ctl;
1893
	int ret;
1941
	int ret;
1894
 
1942
 
1895
	/* TODO: We're not aware of mappable constraints on gen8 yet */
1943
	/* TODO: We're not aware of mappable constraints on gen8 yet */
1896
	*mappable_base = pci_resource_start(dev->pdev, 2);
1944
	*mappable_base = pci_resource_start(dev->pdev, 2);
1897
	*mappable_end = pci_resource_len(dev->pdev, 2);
1945
	*mappable_end = pci_resource_len(dev->pdev, 2);
1898
 
1946
 
1899
	if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
1947
	if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
1900
		pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
1948
		pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
1901
 
1949
 
1902
	pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1950
	pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
-
 
1951
 
-
 
1952
	if (INTEL_INFO(dev)->gen >= 9) {
-
 
1953
		*stolen = gen9_get_stolen_size(snb_gmch_ctl);
1903
 
1954
		gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
1904
	if (IS_CHERRYVIEW(dev)) {
1955
	} else if (IS_CHERRYVIEW(dev)) {
1905
		*stolen = chv_get_stolen_size(snb_gmch_ctl);
1956
		*stolen = chv_get_stolen_size(snb_gmch_ctl);
1906
		gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
1957
		gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
1907
	} else {
1958
	} else {
1908
	*stolen = gen8_get_stolen_size(snb_gmch_ctl);
1959
	*stolen = gen8_get_stolen_size(snb_gmch_ctl);
1909
		gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
1960
		gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
1910
	}
1961
	}
1911
 
1962
 
1912
	*gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
1963
	*gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
1913
 
1964
 
1914
	if (IS_CHERRYVIEW(dev))
1965
	if (IS_CHERRYVIEW(dev))
1915
		chv_setup_private_ppat(dev_priv);
1966
		chv_setup_private_ppat(dev_priv);
1916
	else
1967
	else
1917
		bdw_setup_private_ppat(dev_priv);
1968
		bdw_setup_private_ppat(dev_priv);
1918
 
1969
 
1919
	ret = ggtt_probe_common(dev, gtt_size);
1970
	ret = ggtt_probe_common(dev, gtt_size);
1920
 
1971
 
1921
	dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
1972
	dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
1922
	dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
1973
	dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
1923
 
1974
 
1924
	return ret;
1975
	return ret;
1925
}
1976
}
1926
 
1977
 
1927
static int gen6_gmch_probe(struct drm_device *dev,
1978
static int gen6_gmch_probe(struct drm_device *dev,
1928
			   size_t *gtt_total,
1979
			   size_t *gtt_total,
1929
			   size_t *stolen,
1980
			   size_t *stolen,
1930
			   phys_addr_t *mappable_base,
1981
			   phys_addr_t *mappable_base,
1931
			   unsigned long *mappable_end)
1982
			   unsigned long *mappable_end)
1932
{
1983
{
1933
	struct drm_i915_private *dev_priv = dev->dev_private;
1984
	struct drm_i915_private *dev_priv = dev->dev_private;
1934
	unsigned int gtt_size;
1985
	unsigned int gtt_size;
1935
	u16 snb_gmch_ctl;
1986
	u16 snb_gmch_ctl;
1936
	int ret;
1987
	int ret;
1937
 
1988
 
1938
	*mappable_base = pci_resource_start(dev->pdev, 2);
1989
	*mappable_base = pci_resource_start(dev->pdev, 2);
1939
	*mappable_end = pci_resource_len(dev->pdev, 2);
1990
	*mappable_end = pci_resource_len(dev->pdev, 2);
1940
 
1991
 
1941
	/* 64/512MB is the current min/max we actually know of, but this is just
1992
	/* 64/512MB is the current min/max we actually know of, but this is just
1942
	 * a coarse sanity check.
1993
	 * a coarse sanity check.
1943
	 */
1994
	 */
1944
	if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
1995
	if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
1945
		DRM_ERROR("Unknown GMADR size (%lx)\n",
1996
		DRM_ERROR("Unknown GMADR size (%lx)\n",
1946
			  dev_priv->gtt.mappable_end);
1997
			  dev_priv->gtt.mappable_end);
1947
		return -ENXIO;
1998
		return -ENXIO;
1948
		}
1999
		}
1949
 
2000
 
1950
	if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
2001
	if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
1951
		pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
2002
		pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
1952
	pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
2003
	pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1953
 
2004
 
1954
	*stolen = gen6_get_stolen_size(snb_gmch_ctl);
2005
	*stolen = gen6_get_stolen_size(snb_gmch_ctl);
1955
 
2006
 
1956
	gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
2007
	gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
1957
	*gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
2008
	*gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
1958
 
2009
 
1959
	ret = ggtt_probe_common(dev, gtt_size);
2010
	ret = ggtt_probe_common(dev, gtt_size);
1960
 
2011
 
1961
	dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
2012
	dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
1962
	dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
2013
	dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
1963
 
2014
 
1964
	return ret;
2015
	return ret;
1965
}
2016
}
1966
 
2017
 
1967
static void gen6_gmch_remove(struct i915_address_space *vm)
2018
static void gen6_gmch_remove(struct i915_address_space *vm)
1968
{
2019
{
1969
 
2020
 
1970
	struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
2021
	struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
1971
 
-
 
1972
	if (drm_mm_initialized(&vm->mm)) {
-
 
1973
		drm_mm_takedown(&vm->mm);
-
 
1974
		list_del(&vm->global_link);
-
 
1975
	}
2022
 
1976
	iounmap(gtt->gsm);
2023
	iounmap(gtt->gsm);
1977
	teardown_scratch_page(vm->dev);
2024
	teardown_scratch_page(vm->dev);
1978
}
2025
}
1979
 
2026
 
1980
static int i915_gmch_probe(struct drm_device *dev,
2027
static int i915_gmch_probe(struct drm_device *dev,
1981
			   size_t *gtt_total,
2028
			   size_t *gtt_total,
1982
			   size_t *stolen,
2029
			   size_t *stolen,
1983
			   phys_addr_t *mappable_base,
2030
			   phys_addr_t *mappable_base,
1984
			   unsigned long *mappable_end)
2031
			   unsigned long *mappable_end)
1985
{
2032
{
1986
	struct drm_i915_private *dev_priv = dev->dev_private;
2033
	struct drm_i915_private *dev_priv = dev->dev_private;
1987
	int ret;
2034
	int ret;
1988
 
2035
 
1989
	ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
2036
	ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
1990
	if (!ret) {
2037
	if (!ret) {
1991
		DRM_ERROR("failed to set up gmch\n");
2038
		DRM_ERROR("failed to set up gmch\n");
1992
		return -EIO;
2039
		return -EIO;
1993
	}
2040
	}
1994
 
2041
 
1995
	intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
2042
	intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
1996
 
2043
 
1997
	dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
2044
	dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
1998
	dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
2045
	dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
1999
 
2046
 
2000
	if (unlikely(dev_priv->gtt.do_idle_maps))
2047
	if (unlikely(dev_priv->gtt.do_idle_maps))
2001
		DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2048
		DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2002
 
2049
 
2003
	return 0;
2050
	return 0;
2004
}
2051
}
2005
 
2052
 
2006
static void i915_gmch_remove(struct i915_address_space *vm)
2053
static void i915_gmch_remove(struct i915_address_space *vm)
2007
{
2054
{
2008
//	intel_gmch_remove();
2055
//	intel_gmch_remove();
2009
}
2056
}
2010
 
2057
 
2011
int i915_gem_gtt_init(struct drm_device *dev)
2058
int i915_gem_gtt_init(struct drm_device *dev)
2012
{
2059
{
2013
	struct drm_i915_private *dev_priv = dev->dev_private;
2060
	struct drm_i915_private *dev_priv = dev->dev_private;
2014
	struct i915_gtt *gtt = &dev_priv->gtt;
2061
	struct i915_gtt *gtt = &dev_priv->gtt;
2015
	int ret;
2062
	int ret;
2016
 
2063
 
2017
	if (INTEL_INFO(dev)->gen <= 5) {
2064
	if (INTEL_INFO(dev)->gen <= 5) {
2018
		gtt->gtt_probe = i915_gmch_probe;
2065
		gtt->gtt_probe = i915_gmch_probe;
2019
		gtt->base.cleanup = i915_gmch_remove;
2066
		gtt->base.cleanup = i915_gmch_remove;
2020
	} else if (INTEL_INFO(dev)->gen < 8) {
2067
	} else if (INTEL_INFO(dev)->gen < 8) {
2021
		gtt->gtt_probe = gen6_gmch_probe;
2068
		gtt->gtt_probe = gen6_gmch_probe;
2022
		gtt->base.cleanup = gen6_gmch_remove;
2069
		gtt->base.cleanup = gen6_gmch_remove;
2023
		if (IS_HASWELL(dev) && dev_priv->ellc_size)
2070
		if (IS_HASWELL(dev) && dev_priv->ellc_size)
2024
			gtt->base.pte_encode = iris_pte_encode;
2071
			gtt->base.pte_encode = iris_pte_encode;
2025
		else if (IS_HASWELL(dev))
2072
		else if (IS_HASWELL(dev))
2026
			gtt->base.pte_encode = hsw_pte_encode;
2073
			gtt->base.pte_encode = hsw_pte_encode;
2027
		else if (IS_VALLEYVIEW(dev))
2074
		else if (IS_VALLEYVIEW(dev))
2028
			gtt->base.pte_encode = byt_pte_encode;
2075
			gtt->base.pte_encode = byt_pte_encode;
2029
		else if (INTEL_INFO(dev)->gen >= 7)
2076
		else if (INTEL_INFO(dev)->gen >= 7)
2030
			gtt->base.pte_encode = ivb_pte_encode;
2077
			gtt->base.pte_encode = ivb_pte_encode;
2031
		else
2078
		else
2032
			gtt->base.pte_encode = snb_pte_encode;
2079
			gtt->base.pte_encode = snb_pte_encode;
2033
	} else {
2080
	} else {
2034
		dev_priv->gtt.gtt_probe = gen8_gmch_probe;
2081
		dev_priv->gtt.gtt_probe = gen8_gmch_probe;
2035
		dev_priv->gtt.base.cleanup = gen6_gmch_remove;
2082
		dev_priv->gtt.base.cleanup = gen6_gmch_remove;
2036
	}
2083
	}
2037
 
2084
 
2038
	ret = gtt->gtt_probe(dev, >t->base.total, >t->stolen_size,
2085
	ret = gtt->gtt_probe(dev, >t->base.total, >t->stolen_size,
2039
			     >t->mappable_base, >t->mappable_end);
2086
			     >t->mappable_base, >t->mappable_end);
2040
	if (ret)
2087
	if (ret)
2041
		return ret;
2088
		return ret;
2042
 
2089
 
2043
	gtt->base.dev = dev;
2090
	gtt->base.dev = dev;
2044
 
2091
 
2045
	/* GMADR is the PCI mmio aperture into the global GTT. */
2092
	/* GMADR is the PCI mmio aperture into the global GTT. */
2046
	DRM_INFO("Memory usable by graphics device = %zdM\n",
2093
	DRM_INFO("Memory usable by graphics device = %zdM\n",
2047
		 gtt->base.total >> 20);
2094
		 gtt->base.total >> 20);
2048
	DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
2095
	DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
2049
	DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
2096
	DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
2050
#ifdef CONFIG_INTEL_IOMMU
2097
#ifdef CONFIG_INTEL_IOMMU
2051
	if (intel_iommu_gfx_mapped)
2098
	if (intel_iommu_gfx_mapped)
2052
		DRM_INFO("VT-d active for gfx access\n");
2099
		DRM_INFO("VT-d active for gfx access\n");
2053
#endif
2100
#endif
2054
	/*
2101
	/*
2055
	 * i915.enable_ppgtt is read-only, so do an early pass to validate the
2102
	 * i915.enable_ppgtt is read-only, so do an early pass to validate the
2056
	 * user's requested state against the hardware/driver capabilities.  We
2103
	 * user's requested state against the hardware/driver capabilities.  We
2057
	 * do this now so that we can print out any log messages once rather
2104
	 * do this now so that we can print out any log messages once rather
2058
	 * than every time we check intel_enable_ppgtt().
2105
	 * than every time we check intel_enable_ppgtt().
2059
	 */
2106
	 */
2060
	i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
2107
	i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
2061
	DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
2108
	DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
2062
 
2109
 
2063
	return 0;
2110
	return 0;
2064
}
2111
}
2065
 
2112
 
2066
static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
2113
static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
2067
					      struct i915_address_space *vm)
2114
					      struct i915_address_space *vm)
2068
{
2115
{
2069
	struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
2116
	struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
2070
	if (vma == NULL)
2117
	if (vma == NULL)
2071
		return ERR_PTR(-ENOMEM);
2118
		return ERR_PTR(-ENOMEM);
2072
 
2119
 
2073
	INIT_LIST_HEAD(&vma->vma_link);
2120
	INIT_LIST_HEAD(&vma->vma_link);
2074
	INIT_LIST_HEAD(&vma->mm_list);
2121
	INIT_LIST_HEAD(&vma->mm_list);
2075
	INIT_LIST_HEAD(&vma->exec_list);
2122
	INIT_LIST_HEAD(&vma->exec_list);
2076
	vma->vm = vm;
2123
	vma->vm = vm;
2077
	vma->obj = obj;
2124
	vma->obj = obj;
2078
 
2125
 
2079
	switch (INTEL_INFO(vm->dev)->gen) {
2126
	switch (INTEL_INFO(vm->dev)->gen) {
-
 
2127
	case 9:
2080
	case 8:
2128
	case 8:
2081
	case 7:
2129
	case 7:
2082
	case 6:
2130
	case 6:
2083
		if (i915_is_ggtt(vm)) {
2131
		if (i915_is_ggtt(vm)) {
2084
			vma->unbind_vma = ggtt_unbind_vma;
2132
			vma->unbind_vma = ggtt_unbind_vma;
2085
			vma->bind_vma = ggtt_bind_vma;
2133
			vma->bind_vma = ggtt_bind_vma;
2086
		} else {
2134
		} else {
2087
			vma->unbind_vma = ppgtt_unbind_vma;
2135
			vma->unbind_vma = ppgtt_unbind_vma;
2088
			vma->bind_vma = ppgtt_bind_vma;
2136
			vma->bind_vma = ppgtt_bind_vma;
2089
		}
2137
		}
2090
		break;
2138
		break;
2091
	case 5:
2139
	case 5:
2092
	case 4:
2140
	case 4:
2093
	case 3:
2141
	case 3:
2094
	case 2:
2142
	case 2:
2095
		BUG_ON(!i915_is_ggtt(vm));
2143
		BUG_ON(!i915_is_ggtt(vm));
2096
		vma->unbind_vma = i915_ggtt_unbind_vma;
2144
		vma->unbind_vma = i915_ggtt_unbind_vma;
2097
		vma->bind_vma = i915_ggtt_bind_vma;
2145
		vma->bind_vma = i915_ggtt_bind_vma;
2098
		break;
2146
		break;
2099
	default:
2147
	default:
2100
		BUG();
2148
		BUG();
2101
	}
2149
	}
2102
 
2150
 
2103
	/* Keep GGTT vmas first to make debug easier */
2151
	/* Keep GGTT vmas first to make debug easier */
2104
	if (i915_is_ggtt(vm))
2152
	if (i915_is_ggtt(vm))
2105
		list_add(&vma->vma_link, &obj->vma_list);
2153
		list_add(&vma->vma_link, &obj->vma_list);
2106
	else
2154
	else {
2107
		list_add_tail(&vma->vma_link, &obj->vma_list);
2155
		list_add_tail(&vma->vma_link, &obj->vma_list);
-
 
2156
		i915_ppgtt_get(i915_vm_to_ppgtt(vm));
-
 
2157
	}
2108
 
2158
 
2109
	return vma;
2159
	return vma;
2110
}
2160
}
2111
 
2161
 
2112
struct i915_vma *
2162
struct i915_vma *
2113
i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2163
i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2114
				  struct i915_address_space *vm)
2164
				  struct i915_address_space *vm)
2115
{
2165
{
2116
	struct i915_vma *vma;
2166
	struct i915_vma *vma;
2117
 
2167
 
2118
	vma = i915_gem_obj_to_vma(obj, vm);
2168
	vma = i915_gem_obj_to_vma(obj, vm);
2119
	if (!vma)
2169
	if (!vma)
2120
		vma = __i915_gem_vma_create(obj, vm);
2170
		vma = __i915_gem_vma_create(obj, vm);
2121
 
2171
 
2122
	return vma;
2172
	return vma;
2123
}
2173
}
2124
 
2174
 
2125
struct scatterlist *sg_next(struct scatterlist *sg)
2175
struct scatterlist *sg_next(struct scatterlist *sg)
2126
{
2176
{
2127
    if (sg_is_last(sg))
2177
    if (sg_is_last(sg))
2128
        return NULL;
2178
        return NULL;
2129
 
2179
 
2130
    sg++;
2180
    sg++;
2131
    if (unlikely(sg_is_chain(sg)))
2181
    if (unlikely(sg_is_chain(sg)))
2132
            sg = sg_chain_ptr(sg);
2182
            sg = sg_chain_ptr(sg);
2133
 
2183
 
2134
    return sg;
2184
    return sg;
2135
}
2185
}
2136
 
2186
 
2137
 
2187
 
2138
void __sg_free_table(struct sg_table *table, unsigned int max_ents,
2188
void __sg_free_table(struct sg_table *table, unsigned int max_ents,
2139
             bool skip_first_chunk, sg_free_fn *free_fn)
2189
             bool skip_first_chunk, sg_free_fn *free_fn)
2140
{
2190
{
2141
    struct scatterlist *sgl, *next;
2191
    struct scatterlist *sgl, *next;
2142
 
2192
 
2143
    if (unlikely(!table->sgl))
2193
    if (unlikely(!table->sgl))
2144
        return;
2194
        return;
2145
 
2195
 
2146
    sgl = table->sgl;
2196
    sgl = table->sgl;
2147
    while (table->orig_nents) {
2197
    while (table->orig_nents) {
2148
        unsigned int alloc_size = table->orig_nents;
2198
        unsigned int alloc_size = table->orig_nents;
2149
        unsigned int sg_size;
2199
        unsigned int sg_size;
2150
 
2200
 
2151
        /*
2201
        /*
2152
         * If we have more than max_ents segments left,
2202
         * If we have more than max_ents segments left,
2153
         * then assign 'next' to the sg table after the current one.
2203
         * then assign 'next' to the sg table after the current one.
2154
         * sg_size is then one less than alloc size, since the last
2204
         * sg_size is then one less than alloc size, since the last
2155
         * element is the chain pointer.
2205
         * element is the chain pointer.
2156
         */
2206
         */
2157
        if (alloc_size > max_ents) {
2207
        if (alloc_size > max_ents) {
2158
            next = sg_chain_ptr(&sgl[max_ents - 1]);
2208
            next = sg_chain_ptr(&sgl[max_ents - 1]);
2159
            alloc_size = max_ents;
2209
            alloc_size = max_ents;
2160
            sg_size = alloc_size - 1;
2210
            sg_size = alloc_size - 1;
2161
        } else {
2211
        } else {
2162
            sg_size = alloc_size;
2212
            sg_size = alloc_size;
2163
            next = NULL;
2213
            next = NULL;
2164
        }
2214
        }
2165
 
2215
 
2166
        table->orig_nents -= sg_size;
2216
        table->orig_nents -= sg_size;
2167
        if (!skip_first_chunk) {
2217
        if (!skip_first_chunk) {
2168
            kfree(sgl);
2218
            kfree(sgl);
2169
            skip_first_chunk = false;
2219
            skip_first_chunk = false;
2170
        }
2220
        }
2171
        sgl = next;
2221
        sgl = next;
2172
    }
2222
    }
2173
 
2223
 
2174
    table->sgl = NULL;
2224
    table->sgl = NULL;
2175
}
2225
}
2176
void sg_free_table(struct sg_table *table)
2226
void sg_free_table(struct sg_table *table)
2177
{
2227
{
2178
    __sg_free_table(table, SG_MAX_SINGLE_ALLOC, false, NULL);
2228
    __sg_free_table(table, SG_MAX_SINGLE_ALLOC, false, NULL);
2179
}
2229
}
2180
 
2230
 
2181
int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
2231
int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
2182
{
2232
{
2183
    struct scatterlist *sg, *prv;
2233
    struct scatterlist *sg, *prv;
2184
    unsigned int left;
2234
    unsigned int left;
2185
    unsigned int max_ents = SG_MAX_SINGLE_ALLOC;
2235
    unsigned int max_ents = SG_MAX_SINGLE_ALLOC;
2186
 
2236
 
2187
#ifndef ARCH_HAS_SG_CHAIN
2237
#ifndef ARCH_HAS_SG_CHAIN
2188
    BUG_ON(nents > max_ents);
2238
    BUG_ON(nents > max_ents);
2189
#endif
2239
#endif
2190
 
2240
 
2191
    memset(table, 0, sizeof(*table));
2241
    memset(table, 0, sizeof(*table));
2192
 
2242
 
2193
    left = nents;
2243
    left = nents;
2194
    prv = NULL;
2244
    prv = NULL;
2195
    do {
2245
    do {
2196
        unsigned int sg_size, alloc_size = left;
2246
        unsigned int sg_size, alloc_size = left;
2197
 
2247
 
2198
        if (alloc_size > max_ents) {
2248
        if (alloc_size > max_ents) {
2199
                alloc_size = max_ents;
2249
                alloc_size = max_ents;
2200
                sg_size = alloc_size - 1;
2250
                sg_size = alloc_size - 1;
2201
        } else
2251
        } else
2202
                sg_size = alloc_size;
2252
                sg_size = alloc_size;
2203
 
2253
 
2204
        left -= sg_size;
2254
        left -= sg_size;
2205
 
2255
 
2206
        sg = kmalloc(alloc_size * sizeof(struct scatterlist), gfp_mask);
2256
        sg = kmalloc(alloc_size * sizeof(struct scatterlist), gfp_mask);
2207
        if (unlikely(!sg)) {
2257
        if (unlikely(!sg)) {
2208
                /*
2258
                /*
2209
                 * Adjust entry count to reflect that the last
2259
                 * Adjust entry count to reflect that the last
2210
                 * entry of the previous table won't be used for
2260
                 * entry of the previous table won't be used for
2211
                 * linkage.  Without this, sg_kfree() may get
2261
                 * linkage.  Without this, sg_kfree() may get
2212
                 * confused.
2262
                 * confused.
2213
                 */
2263
                 */
2214
                if (prv)
2264
                if (prv)
2215
                        table->nents = ++table->orig_nents;
2265
                        table->nents = ++table->orig_nents;
2216
 
2266
 
2217
                goto err;
2267
                goto err;
2218
        }
2268
        }
2219
 
2269
 
2220
        sg_init_table(sg, alloc_size);
2270
        sg_init_table(sg, alloc_size);
2221
        table->nents = table->orig_nents += sg_size;
2271
        table->nents = table->orig_nents += sg_size;
2222
 
2272
 
2223
        /*
2273
        /*
2224
         * If this is the first mapping, assign the sg table header.
2274
         * If this is the first mapping, assign the sg table header.
2225
         * If this is not the first mapping, chain previous part.
2275
         * If this is not the first mapping, chain previous part.
2226
         */
2276
         */
2227
        if (prv)
2277
        if (prv)
2228
                sg_chain(prv, max_ents, sg);
2278
                sg_chain(prv, max_ents, sg);
2229
        else
2279
        else
2230
                table->sgl = sg;
2280
                table->sgl = sg;
2231
 
2281
 
2232
        /*
2282
        /*
2233
         * If no more entries after this one, mark the end
2283
         * If no more entries after this one, mark the end
2234
         */
2284
         */
2235
        if (!left)
2285
        if (!left)
2236
                sg_mark_end(&sg[sg_size - 1]);
2286
                sg_mark_end(&sg[sg_size - 1]);
2237
 
2287
 
2238
        prv = sg;
2288
        prv = sg;
2239
    } while (left);
2289
    } while (left);
2240
 
2290
 
2241
    return 0;
2291
    return 0;
2242
 
2292
 
2243
err:
2293
err:
2244
    __sg_free_table(table, SG_MAX_SINGLE_ALLOC, false, NULL);
2294
    __sg_free_table(table, SG_MAX_SINGLE_ALLOC, false, NULL);
2245
 
2295
 
2246
    return -ENOMEM;
2296
    return -ENOMEM;
2247
}
2297
}
2248
 
2298
 
2249
 
2299
 
2250
void sg_init_table(struct scatterlist *sgl, unsigned int nents)
2300
void sg_init_table(struct scatterlist *sgl, unsigned int nents)
2251
{
2301
{
2252
    memset(sgl, 0, sizeof(*sgl) * nents);
2302
    memset(sgl, 0, sizeof(*sgl) * nents);
2253
#ifdef CONFIG_DEBUG_SG
2303
#ifdef CONFIG_DEBUG_SG
2254
    {
2304
    {
2255
            unsigned int i;
2305
            unsigned int i;
2256
            for (i = 0; i < nents; i++)
2306
            for (i = 0; i < nents; i++)
2257
                    sgl[i].sg_magic = SG_MAGIC;
2307
                    sgl[i].sg_magic = SG_MAGIC;
2258
    }
2308
    }
2259
#endif
2309
#endif
2260
    sg_mark_end(&sgl[nents - 1]);
2310
    sg_mark_end(&sgl[nents - 1]);
2261
}
2311
}
2262
 
2312
 
2263
 
2313
 
2264
void __sg_page_iter_start(struct sg_page_iter *piter,
2314
void __sg_page_iter_start(struct sg_page_iter *piter,
2265
              struct scatterlist *sglist, unsigned int nents,
2315
              struct scatterlist *sglist, unsigned int nents,
2266
              unsigned long pgoffset)
2316
              unsigned long pgoffset)
2267
{
2317
{
2268
    piter->__pg_advance = 0;
2318
    piter->__pg_advance = 0;
2269
    piter->__nents = nents;
2319
    piter->__nents = nents;
2270
 
2320
 
2271
    piter->sg = sglist;
2321
    piter->sg = sglist;
2272
    piter->sg_pgoffset = pgoffset;
2322
    piter->sg_pgoffset = pgoffset;
2273
}
2323
}
2274
 
2324
 
2275
static int sg_page_count(struct scatterlist *sg)
2325
static int sg_page_count(struct scatterlist *sg)
2276
{
2326
{
2277
    return PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT;
2327
    return PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT;
2278
}
2328
}
2279
 
2329
 
2280
bool __sg_page_iter_next(struct sg_page_iter *piter)
2330
bool __sg_page_iter_next(struct sg_page_iter *piter)
2281
{
2331
{
2282
    if (!piter->__nents || !piter->sg)
2332
    if (!piter->__nents || !piter->sg)
2283
        return false;
2333
        return false;
2284
 
2334
 
2285
    piter->sg_pgoffset += piter->__pg_advance;
2335
    piter->sg_pgoffset += piter->__pg_advance;
2286
    piter->__pg_advance = 1;
2336
    piter->__pg_advance = 1;
2287
 
2337
 
2288
    while (piter->sg_pgoffset >= sg_page_count(piter->sg)) {
2338
    while (piter->sg_pgoffset >= sg_page_count(piter->sg)) {
2289
        piter->sg_pgoffset -= sg_page_count(piter->sg);
2339
        piter->sg_pgoffset -= sg_page_count(piter->sg);
2290
        piter->sg = sg_next(piter->sg);
2340
        piter->sg = sg_next(piter->sg);
2291
        if (!--piter->__nents || !piter->sg)
2341
        if (!--piter->__nents || !piter->sg)
2292
            return false;
2342
            return false;
2293
    }
2343
    }
2294
 
2344
 
2295
    return true;
2345
    return true;
2296
}
2346
}
2297
EXPORT_SYMBOL(__sg_page_iter_next);
2347
EXPORT_SYMBOL(__sg_page_iter_next);
2298
>
2348
>
2299
>
2349
>
2300
>
2350
>
2301
>
2351
>