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