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