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