drm_cache.c revision 1.3.4.2 1 1.3.4.2 tls /* $NetBSD: drm_cache.c,v 1.3.4.2 2014/08/20 00:04:20 tls 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.2 2014/08/20 00:04:20 tls Exp $");
34 1.3.4.2 tls
35 1.3.4.2 tls #include <sys/types.h>
36 1.3.4.2 tls #include <sys/xcall.h>
37 1.3.4.2 tls
38 1.3.4.2 tls #include <uvm/uvm_extern.h>
39 1.3.4.2 tls
40 1.3.4.2 tls #include <machine/cpufunc.h>
41 1.3.4.2 tls
42 1.3.4.2 tls #include <linux/mm_types.h>
43 1.3.4.2 tls
44 1.3.4.2 tls #include <drm/drmP.h>
45 1.3.4.2 tls
46 1.3.4.2 tls static bool drm_md_clflush_finegrained_p(void);
47 1.3.4.2 tls static void drm_md_clflush_all(void);
48 1.3.4.2 tls static void drm_md_clflush_page(struct page *);
49 1.3.4.2 tls static void drm_md_clflush_virt_range(const void *, size_t);
50 1.3.4.2 tls
51 1.3.4.2 tls void
52 1.3.4.2 tls drm_clflush_pages(struct page **pages, unsigned long npages)
53 1.3.4.2 tls {
54 1.3.4.2 tls
55 1.3.4.2 tls if (drm_md_clflush_finegrained_p()) {
56 1.3.4.2 tls while (npages--)
57 1.3.4.2 tls drm_md_clflush_page(pages[npages]);
58 1.3.4.2 tls } else {
59 1.3.4.2 tls drm_md_clflush_all();
60 1.3.4.2 tls }
61 1.3.4.2 tls }
62 1.3.4.2 tls
63 1.3.4.2 tls void
64 1.3.4.2 tls drm_clflush_pglist(struct pglist *list)
65 1.3.4.2 tls {
66 1.3.4.2 tls
67 1.3.4.2 tls if (drm_md_clflush_finegrained_p()) {
68 1.3.4.2 tls struct vm_page *page;
69 1.3.4.2 tls
70 1.3.4.2 tls TAILQ_FOREACH(page, list, pageq.queue)
71 1.3.4.2 tls drm_md_clflush_page(container_of(page, struct page,
72 1.3.4.2 tls p_vmp));
73 1.3.4.2 tls } else {
74 1.3.4.2 tls drm_md_clflush_all();
75 1.3.4.2 tls }
76 1.3.4.2 tls }
77 1.3.4.2 tls
78 1.3.4.2 tls void
79 1.3.4.2 tls drm_clflush_page(struct page *page)
80 1.3.4.2 tls {
81 1.3.4.2 tls
82 1.3.4.2 tls if (drm_md_clflush_finegrained_p())
83 1.3.4.2 tls drm_md_clflush_page(page);
84 1.3.4.2 tls else
85 1.3.4.2 tls drm_md_clflush_all();
86 1.3.4.2 tls }
87 1.3.4.2 tls
88 1.3.4.2 tls void
89 1.3.4.2 tls drm_clflush_virt_range(const void *vaddr, size_t nbytes)
90 1.3.4.2 tls {
91 1.3.4.2 tls
92 1.3.4.2 tls if (drm_md_clflush_finegrained_p())
93 1.3.4.2 tls drm_md_clflush_virt_range(vaddr, nbytes);
94 1.3.4.2 tls else
95 1.3.4.2 tls drm_md_clflush_all();
96 1.3.4.2 tls }
97 1.3.4.2 tls
98 1.3.4.2 tls #if defined(__i386__) || defined(__x86_64__)
99 1.3.4.2 tls
100 1.3.4.2 tls static bool
101 1.3.4.2 tls drm_md_clflush_finegrained_p(void)
102 1.3.4.2 tls {
103 1.3.4.2 tls return ISSET(cpu_info_primary.ci_feat_val[0], CPUID_CFLUSH);
104 1.3.4.2 tls }
105 1.3.4.2 tls
106 1.3.4.2 tls static void
107 1.3.4.2 tls drm_x86_clflush_cpu(void)
108 1.3.4.2 tls {
109 1.3.4.2 tls asm volatile ("wbinvd");
110 1.3.4.2 tls }
111 1.3.4.2 tls
112 1.3.4.2 tls static void
113 1.3.4.2 tls drm_x86_clflush(const void *vaddr)
114 1.3.4.2 tls {
115 1.3.4.2 tls asm volatile ("clflush %0" : : "m" (*(const char *)vaddr));
116 1.3.4.2 tls }
117 1.3.4.2 tls
118 1.3.4.2 tls static size_t
119 1.3.4.2 tls drm_x86_clflush_size(void)
120 1.3.4.2 tls {
121 1.3.4.2 tls KASSERT(drm_md_clflush_finegrained_p());
122 1.3.4.2 tls return cpu_info_primary.ci_cflush_lsize;
123 1.3.4.2 tls }
124 1.3.4.2 tls
125 1.3.4.2 tls static void
126 1.3.4.2 tls drm_x86_clflush_xc(void *arg0 __unused, void *arg1 __unused)
127 1.3.4.2 tls {
128 1.3.4.2 tls drm_x86_clflush_cpu();
129 1.3.4.2 tls }
130 1.3.4.2 tls
131 1.3.4.2 tls static void
132 1.3.4.2 tls drm_md_clflush_all(void)
133 1.3.4.2 tls {
134 1.3.4.2 tls xc_wait(xc_broadcast(0, &drm_x86_clflush_xc, NULL, NULL));
135 1.3.4.2 tls }
136 1.3.4.2 tls
137 1.3.4.2 tls static void
138 1.3.4.2 tls drm_md_clflush_page(struct page *page)
139 1.3.4.2 tls {
140 1.3.4.2 tls void *const vaddr = kmap_atomic(page);
141 1.3.4.2 tls
142 1.3.4.2 tls drm_md_clflush_virt_range(vaddr, PAGE_SIZE);
143 1.3.4.2 tls
144 1.3.4.2 tls kunmap_atomic(vaddr);
145 1.3.4.2 tls }
146 1.3.4.2 tls
147 1.3.4.2 tls static void
148 1.3.4.2 tls drm_md_clflush_virt_range(const void *vaddr, size_t nbytes)
149 1.3.4.2 tls
150 1.3.4.2 tls {
151 1.3.4.2 tls const char *const start = vaddr, *const end = (start + nbytes);
152 1.3.4.2 tls const char *p;
153 1.3.4.2 tls const unsigned int clflush_size = drm_x86_clflush_size();
154 1.3.4.2 tls
155 1.3.4.2 tls KASSERT(drm_md_clflush_finegrained_p());
156 1.3.4.2 tls for (p = start; p < end; p += clflush_size)
157 1.3.4.2 tls drm_x86_clflush(p);
158 1.3.4.2 tls }
159 1.3.4.2 tls
160 1.3.4.2 tls #endif /* defined(__i386__) || defined(__x86_64__) */
161