summaryrefslogtreecommitdiff
path: root/sys/external/bsd/drm2/drm/drm_cache.c
blob: cf70126a83d99a97ae41a105719531862969fd18 (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
/*	$NetBSD: drm_cache.c,v 1.19 2022/07/19 23:19:27 riastradh Exp $	*/

/*-
 * Copyright (c) 2013 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Taylor R. Campbell.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: drm_cache.c,v 1.19 2022/07/19 23:19:27 riastradh Exp $");

#include <sys/param.h>
#include <sys/types.h>
#include <sys/xcall.h>

#include <uvm/uvm_extern.h>

#include <linux/highmem.h>
#include <linux/mm_types.h>

#include <drm/drm_cache.h>

#if !defined(__arm__) && !defined(__aarch64__) && !defined(__alpha__)
#define DRM_CLFLUSH	1
#endif

#if defined(DRM_CLFLUSH)
static bool		drm_md_clflush_finegrained_p(void);
static void		drm_md_clflush_all(void);
static void		drm_md_clflush_begin(void);
static void		drm_md_clflush_commit(void);
static void		drm_md_clflush_page(struct page *);
static void		drm_md_clflush_virt_range(const void *, size_t);
#endif

void
drm_clflush_pages(struct page **pages, unsigned long npages)
{
#if defined(DRM_CLFLUSH)
	if (drm_md_clflush_finegrained_p()) {
		drm_md_clflush_begin();
		while (npages--)
			drm_md_clflush_page(pages[npages]);
		drm_md_clflush_commit();
	} else {
		drm_md_clflush_all();
	}
#endif
}

void
drm_clflush_sg(struct sg_table *sgt)
{
	drm_clflush_pages(sgt->sgl->sg_pgs, sgt->sgl->sg_npgs);
}

void
drm_clflush_virt_range(void *vaddr, unsigned long nbytes)
{
#if defined(DRM_CLFLUSH)
	if (drm_md_clflush_finegrained_p()) {
		drm_md_clflush_begin();
		drm_md_clflush_virt_range(vaddr, nbytes);
		drm_md_clflush_commit();
	} else {
		drm_md_clflush_all();
	}
#endif
}

#if defined(__i386__) || defined(__x86_64__)

#include <machine/cpufunc.h>

static bool
drm_md_clflush_finegrained_p(void)
{
	return ISSET(cpu_info_primary.ci_feat_val[0], CPUID_CLFSH);
}

static void
drm_x86_clflush_xc(void *arg0 __unused, void *arg1 __unused)
{
	wbinvd();
}

static void
drm_md_clflush_all(void)
{
	xc_wait(xc_broadcast(0, &drm_x86_clflush_xc, NULL, NULL));
}

static void
drm_md_clflush_begin(void)
{
	/* Support for CLFLUSH implies support for MFENCE.  */
	x86_mfence();
}

static void
drm_md_clflush_commit(void)
{
	x86_mfence();
}

static void
drm_md_clflush_page(struct page *page)
{
	void *const vaddr = kmap_atomic(page);

	drm_md_clflush_virt_range(vaddr, PAGE_SIZE);

	kunmap_atomic(vaddr);
}

static void
drm_md_clflush_virt_range(const void *ptr, size_t nbytes)
{
	const unsigned clflush_size = cpu_info_primary.ci_cflush_lsize;
	const vaddr_t vaddr = (vaddr_t)ptr;
	const vaddr_t start = rounddown(vaddr, clflush_size);
	const vaddr_t end = roundup(vaddr + nbytes, clflush_size);
	vaddr_t va;

	for (va = start; va < end; va += clflush_size)
		asm volatile ("clflush %0" : : "m" (*(const char *)va));
}

#elif defined(__sparc__) || defined(__sparc64__)

#ifdef __sparc64__
#include <sparc64/sparc64/cache.h>
#else
#include <sparc/sparc/cache.h>
#endif

static bool
drm_md_clflush_finegrained_p(void)
{
	return true;
}

static void
drm_md_clflush_all(void)
{
	panic("don't know how to flush entire cache on sparc64");
}

static void
drm_md_clflush_begin(void)
{
	membar_Sync();		/* unsure if needed */
}

static void
drm_md_clflush_commit(void)
{
	membar_Sync();		/* unsure if needed */
}

static void
drm_md_clflush_page(struct page *page)
{
#ifdef __sparc64__
	paddr_t pa = VM_PAGE_TO_PHYS(&page->p_vmp);

	cache_flush_phys(pa, PAGE_SIZE, 0);
#else
	void *const vaddr = kmap_atomic(page);

	cache_flush(vaddr, PAGE_SIZE);

	kunmap_atomic(vaddr);
#endif
}

static void
drm_md_clflush_virt_range(const void *ptr, size_t nbytes)
{
#ifdef __sparc64__
	/* XXX Mega-kludge -- doesn't seem to be a way to flush by vaddr.  */
	blast_dcache();
#else
	cache_flush(ptr, nbytes);
#endif
}

#elif defined(__powerpc__)

static bool
drm_md_clflush_finegrained_p(void)
{
	return true;
}

static void
drm_md_clflush_all(void)
{
	panic("don't know how to flush entire cache on powerpc");
}

static void
drm_md_clflush_begin(void)
{
}

static void
drm_md_clflush_commit(void)
{
	asm volatile ("sync" ::: "memory");
}

static void
drm_md_clflush_page(struct page *page)
{
	void *const vaddr = kmap_atomic(page);

	drm_md_clflush_virt_range(vaddr, PAGE_SIZE);

	kunmap_atomic(vaddr);
}

static void
drm_md_clflush_virt_range(const void *ptr, size_t nbytes)
{
	const unsigned dcsize = curcpu()->ci_ci.dcache_line_size;
	vaddr_t va = (vaddr_t)ptr;
	vaddr_t start = rounddown(va, dcsize);
	vaddr_t end = roundup(va + nbytes, dcsize);
	vsize_t len = end - start;
	vsize_t off;

	for (off = 0; off < len; off += dcsize)
		asm volatile ("dcbf\t%0,%1" : : "b"(start), "r"(off));
}

#endif