aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/emgd/emgd/gmm/gtt.c
blob: 5c1f1c2fddc26b621cab32d0d82bb5ffbf51b51b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* -*- pse-c -*-
 *-----------------------------------------------------------------------------
 * Filename: gtt.c
 * $Revision: 1.9 $
 *-----------------------------------------------------------------------------
 * Copyright © 2002-2010, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *-----------------------------------------------------------------------------
 * Description:
 *  Manage the GTT.
 *-----------------------------------------------------------------------------
 */
#include <igd_debug.h>
#include <drmP.h>
#include <memlist.h>
#include <io.h>
#include <memory.h>
#include <asm/cacheflush.h>

#define PFX "EMGD: "

#define SCR1	0x71410 /* scratch register set by vbios indicating status*/
#define SCR2	0x71418 /* scratch register set by vbios indicating amount of stolen memory */
#define FW_ID	0xE1DF0000 /* firmware identifier */
#define ST_BIT	0x00000004 /* bit2- stolen memory bit */
#define PSB_PTE_VALID  0x0001

static DEFINE_SEMAPHORE(client_sem);
static DEFINE_SEMAPHORE(gtt_sem);

struct client_list_struct {
	struct list_head list;
	struct vm_area_struct *vma;
	pid_t pid;
};

static LIST_HEAD(client_list);

static void ipi_handler(void *null) {
	//flush_agp_cache(); /* from agp.h */
	wbinvd();
}

static void tlb_flush(void) {
	/* If needed, this would flush the SGX/MSVDX mmu TLB's */
}

static void emgd_cache_flush(void) {
	if (on_each_cpu(ipi_handler, NULL, 1) != 0)
		panic(PFX "timed out waiting for the other CPUs!\n");
}

static void invalidate_vma(unsigned long pg_offset, unsigned long bus_addr) {
	int zap;
	struct list_head *tmp;
	struct client_list_struct *entry;
	unsigned long addr_start=0;
	unsigned long addr_end=0;
	unsigned long addr_offset=0;
	unsigned long vaddr;
	pgd_t *pgd;
	pud_t *pud;
	pmd_t *pmd;
	pte_t *pte;

	down(&client_sem);
	list_for_each(tmp, &client_list) {
		entry = list_entry(tmp, struct client_list_struct, list);

		/*
		 * We need to handle invalidating VMA's that are only mapping
		 * a portion of the virtual aperture.  Calculate what if
		 * any invalidated pages need to be zapped
		 */
		addr_start = (entry->vma->vm_pgoff << PAGE_SHIFT)
			- bus_addr;
		addr_end = addr_start + (entry->vma->vm_end - entry->vma->vm_start);
		addr_offset = pg_offset << PAGE_SHIFT;

		vaddr = entry->vma->vm_start + (addr_offset - addr_start);

		zap=0;
		pgd=NULL;
		pud=NULL;
		pmd=NULL;
		pte=NULL;

		/*
		 * Look up page table entries for all VMAs that currently
		 * have the virtual aperture mapped -- to see if the page
		 * has ever faulted
		 */
		pgd = pgd_offset(entry->vma->vm_mm, vaddr);
		if (!pgd_none(*pgd)) {
			pud = pud_offset(pgd, vaddr);
			if (!pud_none(*pud)) {
				pmd = pmd_offset(pud, vaddr);
				if (!pmd_none(*pmd)) {
					pte = pte_offset_map(pmd, vaddr);
					if (!pte_none(*pte)) {
						zap=1;
					}
				}
			}
		}
		/*
		 * Only zap a page if it falls within the mapped region
		 * and it has previously faulted
		 */
		if (zap && (addr_offset >= addr_start) &&
				(addr_offset < addr_end)) {


			if (!page_mapcount(pte_page(*pte))) {
				printk(KERN_ERR "ERROR No mapcount");
				printk(KERN_ALERT "ZR %p %08lX %d %d %p", pte_page(*pte),
						pte_page(*pte)->flags, page_count(pte_page(*pte)),
						page_mapcount(pte_page(*pte)), pte_page(*pte)->mapping);
			} else {
				atomic_add_negative(-1, &pte_page(*pte)->_mapcount);
				put_page(pte_page(*pte));
				dec_mm_counter(entry->vma->vm_mm, MM_FILEPAGES);
			}

			pte_clear(entry->vma->vm_mm, vaddr, pte);
		}

		if(pte) {
			pte_unmap(pte);
		}
	}
	up(&client_sem);
}



/*
 * Allocate pages from the kernel and store in a page list.
 */
gmm_mem_buffer_t *emgd_alloc_pages(unsigned long num_pages, int type) {
	gmm_mem_buffer_t *mem;
	size_t list_size;
	struct page *page;
	int i;
	int order;

	mem = (gmm_mem_buffer_t *)kzalloc(sizeof(gmm_mem_buffer_t), GFP_KERNEL);
	if (mem == NULL) {
		return NULL;
	}

	/* First allocate page array */
	list_size = num_pages * sizeof(struct page *);
	mem->vmalloc_flag = false;

	if (list_size <= (2 * PAGE_SIZE)) {
		mem->pages = kmalloc(list_size, GFP_KERNEL | __GFP_NORETRY);
	}

	if (mem->pages == NULL) {
		mem->pages = vmalloc(list_size);
		mem->vmalloc_flag = true;
	}

	if (mem->pages == NULL) {
		printk(KERN_ERR "Failed to allocate memory info struct.\n");
		return NULL;
	}

	/*
	 * If we need phyical contiguous memory, then do things differently.
	 *   Call alloc_pages(GFP_KERNEL, pages) to allocate all the pages.
	 *   The page structure returned is just the first page of the group.
	 *   ? is it a virtual address ?
	 *
	 *   mem->pages[0] = virt_to_phys(page)
	 *   mem->pages[1] = mem->pages[0] + PAGE_SIZE
	 */

	if ((type == 1) || (type == 0)) {
	/* Next allocate the pages */
		for (i = 0; i < num_pages; i++) {
			page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
			if (page == NULL) {
				/* Error! */
				printk(KERN_ERR "Memory allocation failure!\n");
			}

			get_page(page);
			mem->pages[i] = page;
			mem->page_count++;
		}
	} else {
		if (num_pages == 1) {
			order = 0;
		} else if (num_pages == 4) {
			order = 2;
		} else if (num_pages == 8) {
			order = 3;
		} else {
			printk(KERN_ERR "Page count is not valid for physical allocation.\n");
			kfree(mem);
			return NULL;
		}

		page = alloc_pages(GFP_KERNEL, order);
		if (page == NULL) {
			/* Error! */
			printk(KERN_ERR "Memory allocation failure!\n");
		} else {
			get_page(page);
			mem->pages[0] = page;
			if (num_pages > 1) {
				for (i = 1; i < num_pages; i++) {
					mem->pages[i] = mem->pages[i-1] + 1;
				}
			}
			mem->physical = page_to_phys(mem->pages[0]);
			mem->page_count = num_pages;
		}
	}

	return mem;
}

/*
 * Free memory pages.
 */
void emgd_free_pages(gmm_mem_buffer_t *mem) {
	int i;
	struct page *page;

	for (i = 0; i < mem->page_count; i++) {
		page = mem->pages[i];
		put_page(page);
		__free_page(page);
		mem->pages[i] = NULL;
	}

	if (mem->vmalloc_flag) {
		vfree(mem->pages);
	} else {
		kfree(mem->pages);
	}

	kfree(mem);
}


/*
 * Need a function to populate the GTT with the pages.
 *
 * The caller provides the offset into the GTT where the memory needs
 * to go.  This simply needs to allocate the pages and insert them
 * into the GTT.
 */
void emgd_gtt_insert(igd_context_t *context,
		gmm_mem_buffer_t *mem,
		unsigned long offset)
{
	struct page *page;
	unsigned long pte;
	unsigned long pg_off;
	int i, j;

	pg_off = offset >> PAGE_SHIFT;

	/* Check that the offset is within the gtt's range */
	if ((pg_off + mem->page_count) > context->device_context.gatt_pages) {
		printk(KERN_ERR "emgd_gtt_insert: page offset beyond of GTT range.\n");
		return;
	}

	/* Insert the pages into the GTT */

	down(&gtt_sem);

	for (i = 0, j = pg_off; i < mem->page_count; i++, j++) {
		page = mem->pages[i];
		if (j < context->device_context.stolen_pages) {
			/* Inserting over the top of stolen memory */
			if (j == pg_off) {
				printk(KERN_ALERT " Inserting over stolen memory.\n");
			}
			/* Copy old page to new page */
		}

		/* Mark the page as valid */
		pte = page_to_phys(page) | PSB_PTE_VALID;
		writel(pte, (context->device_context.virt_gttadr + j));
		readl(context->device_context.virt_gttadr + j);
	}
	
	up(&gtt_sem);

	(void)readl(context->device_context.virt_gttadr + j - 1);

	/* Invalidate VMA's */
	invalidate_vma(j,
			(context->device_context.gmch_ctl | PCI_BASE_ADDRESS_MEM_MASK));

	/* Flush */
	emgd_cache_flush();
	tlb_flush();

	return;
}

/*
 * Need a function to remove pages from the GTT (and replace with the
 * scratch page?) and free the pages.
 */

void emgd_gtt_remove(igd_context_t *context,
		gmm_mem_buffer_t *mem,
		unsigned long offset)
{
	struct page *page;
	unsigned long pte;
	int i;
	unsigned long pg_start;

	pg_start = offset >> PAGE_SHIFT;

	down(&gtt_sem);

	page = context->device_context.scratch_page;
	pte = page_to_phys(page) | PSB_PTE_VALID;

	/* Insert the scratch page into the GTT */
	for (i = pg_start; i < (mem->page_count + pg_start); i++) {
		if (i < context->device_context.stolen_pages) {
			/* This is stolen memory.... */
		} else {
			writel(pte, context->device_context.virt_gttadr + i);
			(void)readl(context->device_context.virt_gttadr + i);
		}
	}

	up(&gtt_sem);

	/* Invaidate VMA's */
	invalidate_vma(i,
			(context->device_context.gmch_ctl | PCI_BASE_ADDRESS_MEM_MASK));

	/* Flush */
	emgd_cache_flush();
	tlb_flush();
}