summaryrefslogtreecommitdiff
path: root/linux-core/drm_compat.c
blob: d387678e3ca75c7136995792196ece6a02aa9d22 (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
/**************************************************************************
 * 
 * This kernel module is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 * 
 **************************************************************************/
/*
 * This code provides access to unexported mm kernel features. It is necessary
 * to use the new DRM memory manager code with kernels that don't support it
 * directly.
 *
 * Authors: Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
 *          Linux kernel mm subsystem authors. 
 *          (Most code taken from there).
 */

#include "drmP.h"
#include <asm/pgtable.h>
#include <asm/cacheflush.h>
#include <asm/tlbflush.h>

#ifdef MODULE
void pgd_clear_bad(pgd_t * pgd)
{
	pgd_ERROR(*pgd);
	pgd_clear(pgd);
}

void pud_clear_bad(pud_t * pud)
{
	pud_ERROR(*pud);
	pud_clear(pud);
}

void pmd_clear_bad(pmd_t * pmd)
{
	pmd_ERROR(*pmd);
	pmd_clear(pmd);
}
#endif

static inline void change_pte_range(struct mm_struct *mm, pmd_t * pmd,
				    unsigned long addr, unsigned long end)
{
	pte_t *pte;
	struct page *page;
	unsigned long pfn;

	pte = pte_offset_map(pmd, addr);
	do {
		if (pte_present(*pte)) {
			pte_t ptent;
			pfn = pte_pfn(*pte);
			ptent = *pte;
			ptep_get_and_clear(mm, addr, pte);
			if (pfn_valid(pfn)) {
				page = pfn_to_page(pfn);
				if (atomic_add_negative(-1, &page->_mapcount)) {
					if (page_test_and_clear_dirty(page))
						set_page_dirty(page);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,18)
					dec_zone_page_state(page, NR_FILE_MAPPED);
#else
					dec_page_state(nr_mapped);
#endif			
				}

				put_page(page);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
				dec_mm_counter(mm, file_rss);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
				dec_mm_counter(mm, rss);
#else
				--mm->rss;
#endif
			}
		}
	} while (pte++, addr += PAGE_SIZE, addr != end);
	pte_unmap(pte - 1);
}

static inline void change_pmd_range(struct mm_struct *mm, pud_t * pud,
				    unsigned long addr, unsigned long end)
{
	pmd_t *pmd;
	unsigned long next;

	pmd = pmd_offset(pud, addr);
	do {
		next = pmd_addr_end(addr, end);
		if (pmd_none_or_clear_bad(pmd))
			continue;
		change_pte_range(mm, pmd, addr, next);
	} while (pmd++, addr = next, addr != end);
}

static inline void change_pud_range(struct mm_struct *mm, pgd_t * pgd,
				    unsigned long addr, unsigned long end)
{
	pud_t *pud;
	unsigned long next;

	pud = pud_offset(pgd, addr);
	do {
		next = pud_addr_end(addr, end);
		if (pud_none_or_clear_bad(pud))
			continue;
		change_pmd_range(mm, pud, addr, next);
	} while (pud++, addr = next, addr != end);
}

/*
 * This function should be called with all relevant spinlocks held.
 */

void drm_clear_vma(struct vm_area_struct *vma,
		   unsigned long addr, unsigned long end)
{
	struct mm_struct *mm = vma->vm_mm;
	pgd_t *pgd;
	unsigned long next;
#if defined(flush_tlb_mm) || !defined(MODULE)
	unsigned long start = addr;
#endif
	BUG_ON(addr >= end);
	pgd = pgd_offset(mm, addr);
	flush_cache_range(vma, addr, end);
	do {
		next = pgd_addr_end(addr, end);
		if (pgd_none_or_clear_bad(pgd))
			continue;
		change_pud_range(mm, pgd, addr, next);
	} while (pgd++, addr = next, addr != end);
#if defined(flush_tlb_mm) || !defined(MODULE)
	flush_tlb_range(vma, addr, end);
#endif
}

pgprot_t vm_get_page_prot(unsigned long vm_flags)
{
#ifdef MODULE
	static pgprot_t drm_protection_map[16] = {
		__P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
		__S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
	};

	return drm_protection_map[vm_flags & 0x0F];
#else
	extern pgprot_t protection_map[];
	return protection_map[vm_flags & 0x0F];
#endif
};