drm_cache.c revision 1.3.4.3 1 1.3.4.2 tls /* $NetBSD: drm_cache.c,v 1.3.4.3 2017/12/03 11:37:58 jdolecek Exp $ */
2 1.3.4.2 tls
3 1.3.4.2 tls /*-
4 1.3.4.2 tls * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.3.4.2 tls * All rights reserved.
6 1.3.4.2 tls *
7 1.3.4.2 tls * This code is derived from software contributed to The NetBSD Foundation
8 1.3.4.2 tls * by Taylor R. Campbell.
9 1.3.4.2 tls *
10 1.3.4.2 tls * Redistribution and use in source and binary forms, with or without
11 1.3.4.2 tls * modification, are permitted provided that the following conditions
12 1.3.4.2 tls * are met:
13 1.3.4.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.3.4.2 tls * notice, this list of conditions and the following disclaimer.
15 1.3.4.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.4.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.3.4.2 tls * documentation and/or other materials provided with the distribution.
18 1.3.4.2 tls *
19 1.3.4.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3.4.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3.4.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3.4.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3.4.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3.4.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3.4.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3.4.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3.4.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3.4.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3.4.2 tls * POSSIBILITY OF SUCH DAMAGE.
30 1.3.4.2 tls */
31 1.3.4.2 tls
32 1.3.4.2 tls #include <sys/cdefs.h>
33 1.3.4.2 tls __KERNEL_RCSID(0, "$NetBSD: drm_cache.c,v 1.3.4.3 2017/12/03 11:37:58 jdolecek Exp $");
34 1.3.4.2 tls
35 1.3.4.3 jdolecek #include <sys/param.h>
36 1.3.4.2 tls #include <sys/types.h>
37 1.3.4.2 tls #include <sys/xcall.h>
38 1.3.4.2 tls
39 1.3.4.2 tls #include <uvm/uvm_extern.h>
40 1.3.4.2 tls
41 1.3.4.2 tls #include <linux/mm_types.h>
42 1.3.4.2 tls
43 1.3.4.2 tls #include <drm/drmP.h>
44 1.3.4.2 tls
45 1.3.4.3 jdolecek #if !defined(__arm__)
46 1.3.4.3 jdolecek #define DRM_CLFLUSH 1
47 1.3.4.3 jdolecek #endif
48 1.3.4.3 jdolecek
49 1.3.4.3 jdolecek #if defined(DRM_CLFLUSH)
50 1.3.4.2 tls static bool drm_md_clflush_finegrained_p(void);
51 1.3.4.2 tls static void drm_md_clflush_all(void);
52 1.3.4.2 tls static void drm_md_clflush_page(struct page *);
53 1.3.4.2 tls static void drm_md_clflush_virt_range(const void *, size_t);
54 1.3.4.3 jdolecek #endif
55 1.3.4.2 tls
56 1.3.4.2 tls void
57 1.3.4.2 tls drm_clflush_pages(struct page **pages, unsigned long npages)
58 1.3.4.2 tls {
59 1.3.4.3 jdolecek #if defined(DRM_CLFLUSH)
60 1.3.4.2 tls if (drm_md_clflush_finegrained_p()) {
61 1.3.4.2 tls while (npages--)
62 1.3.4.2 tls drm_md_clflush_page(pages[npages]);
63 1.3.4.2 tls } else {
64 1.3.4.2 tls drm_md_clflush_all();
65 1.3.4.2 tls }
66 1.3.4.3 jdolecek #endif
67 1.3.4.2 tls }
68 1.3.4.2 tls
69 1.3.4.2 tls void
70 1.3.4.2 tls drm_clflush_pglist(struct pglist *list)
71 1.3.4.2 tls {
72 1.3.4.3 jdolecek #if defined(DRM_CLFLUSH)
73 1.3.4.2 tls if (drm_md_clflush_finegrained_p()) {
74 1.3.4.2 tls struct vm_page *page;
75 1.3.4.2 tls
76 1.3.4.2 tls TAILQ_FOREACH(page, list, pageq.queue)
77 1.3.4.2 tls drm_md_clflush_page(container_of(page, struct page,
78 1.3.4.2 tls p_vmp));
79 1.3.4.2 tls } else {
80 1.3.4.2 tls drm_md_clflush_all();
81 1.3.4.2 tls }
82 1.3.4.3 jdolecek #endif
83 1.3.4.2 tls }
84 1.3.4.2 tls
85 1.3.4.2 tls void
86 1.3.4.2 tls drm_clflush_page(struct page *page)
87 1.3.4.2 tls {
88 1.3.4.3 jdolecek #if defined(DRM_CLFLUSH)
89 1.3.4.2 tls if (drm_md_clflush_finegrained_p())
90 1.3.4.2 tls drm_md_clflush_page(page);
91 1.3.4.2 tls else
92 1.3.4.2 tls drm_md_clflush_all();
93 1.3.4.3 jdolecek #endif
94 1.3.4.2 tls }
95 1.3.4.2 tls
96 1.3.4.2 tls void
97 1.3.4.2 tls drm_clflush_virt_range(const void *vaddr, size_t nbytes)
98 1.3.4.2 tls {
99 1.3.4.3 jdolecek #if defined(DRM_CLFLUSH)
100 1.3.4.2 tls if (drm_md_clflush_finegrained_p())
101 1.3.4.2 tls drm_md_clflush_virt_range(vaddr, nbytes);
102 1.3.4.2 tls else
103 1.3.4.2 tls drm_md_clflush_all();
104 1.3.4.3 jdolecek #endif
105 1.3.4.2 tls }
106 1.3.4.2 tls
107 1.3.4.2 tls #if defined(__i386__) || defined(__x86_64__)
108 1.3.4.2 tls
109 1.3.4.3 jdolecek #include <machine/cpufunc.h>
110 1.3.4.3 jdolecek
111 1.3.4.2 tls static bool
112 1.3.4.2 tls drm_md_clflush_finegrained_p(void)
113 1.3.4.2 tls {
114 1.3.4.2 tls return ISSET(cpu_info_primary.ci_feat_val[0], CPUID_CFLUSH);
115 1.3.4.2 tls }
116 1.3.4.2 tls
117 1.3.4.2 tls static void
118 1.3.4.2 tls drm_x86_clflush(const void *vaddr)
119 1.3.4.2 tls {
120 1.3.4.2 tls asm volatile ("clflush %0" : : "m" (*(const char *)vaddr));
121 1.3.4.2 tls }
122 1.3.4.2 tls
123 1.3.4.2 tls static size_t
124 1.3.4.2 tls drm_x86_clflush_size(void)
125 1.3.4.2 tls {
126 1.3.4.2 tls KASSERT(drm_md_clflush_finegrained_p());
127 1.3.4.2 tls return cpu_info_primary.ci_cflush_lsize;
128 1.3.4.2 tls }
129 1.3.4.2 tls
130 1.3.4.2 tls static void
131 1.3.4.2 tls drm_x86_clflush_xc(void *arg0 __unused, void *arg1 __unused)
132 1.3.4.2 tls {
133 1.3.4.3 jdolecek wbinvd();
134 1.3.4.2 tls }
135 1.3.4.2 tls
136 1.3.4.2 tls static void
137 1.3.4.2 tls drm_md_clflush_all(void)
138 1.3.4.2 tls {
139 1.3.4.2 tls xc_wait(xc_broadcast(0, &drm_x86_clflush_xc, NULL, NULL));
140 1.3.4.2 tls }
141 1.3.4.2 tls
142 1.3.4.2 tls static void
143 1.3.4.2 tls drm_md_clflush_page(struct page *page)
144 1.3.4.2 tls {
145 1.3.4.2 tls void *const vaddr = kmap_atomic(page);
146 1.3.4.2 tls
147 1.3.4.2 tls drm_md_clflush_virt_range(vaddr, PAGE_SIZE);
148 1.3.4.2 tls
149 1.3.4.2 tls kunmap_atomic(vaddr);
150 1.3.4.2 tls }
151 1.3.4.2 tls
152 1.3.4.2 tls static void
153 1.3.4.2 tls drm_md_clflush_virt_range(const void *vaddr, size_t nbytes)
154 1.3.4.2 tls {
155 1.3.4.3 jdolecek const unsigned clflush_size = drm_x86_clflush_size();
156 1.3.4.3 jdolecek const vaddr_t va = (vaddr_t)vaddr;
157 1.3.4.3 jdolecek const char *const start = (const void *)rounddown(va, clflush_size);
158 1.3.4.3 jdolecek const char *const end = (const void *)roundup(va + nbytes,
159 1.3.4.3 jdolecek clflush_size);
160 1.3.4.2 tls const char *p;
161 1.3.4.2 tls
162 1.3.4.3 jdolecek /* Support for CLFLUSH implies support for MFENCE. */
163 1.3.4.2 tls KASSERT(drm_md_clflush_finegrained_p());
164 1.3.4.3 jdolecek x86_mfence();
165 1.3.4.2 tls for (p = start; p < end; p += clflush_size)
166 1.3.4.2 tls drm_x86_clflush(p);
167 1.3.4.3 jdolecek x86_mfence();
168 1.3.4.2 tls }
169 1.3.4.2 tls
170 1.3.4.2 tls #endif /* defined(__i386__) || defined(__x86_64__) */
171