linux_writecomb.c revision 1.3 1 1.3 mrg /* $NetBSD: linux_writecomb.c,v 1.3 2015/01/01 01:15:43 mrg Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #include <sys/cdefs.h>
33 1.3 mrg __KERNEL_RCSID(0, "$NetBSD: linux_writecomb.c,v 1.3 2015/01/01 01:15:43 mrg Exp $");
34 1.1 riastrad
35 1.1 riastrad #ifdef _KERNEL_OPT
36 1.1 riastrad #include "opt_mtrr.h"
37 1.1 riastrad #endif
38 1.1 riastrad
39 1.1 riastrad #include <sys/kmem.h>
40 1.1 riastrad #include <sys/mutex.h>
41 1.1 riastrad
42 1.1 riastrad #include <machine/mtrr.h>
43 1.1 riastrad
44 1.1 riastrad #include <linux/idr.h>
45 1.1 riastrad #include <linux/io.h>
46 1.1 riastrad
47 1.1 riastrad static struct {
48 1.1 riastrad kmutex_t lock;
49 1.1 riastrad struct idr idr;
50 1.1 riastrad } linux_writecomb __cacheline_aligned;
51 1.1 riastrad
52 1.1 riastrad int
53 1.1 riastrad linux_writecomb_init(void)
54 1.1 riastrad {
55 1.1 riastrad
56 1.3 mrg mutex_init(&linux_writecomb.lock, MUTEX_DEFAULT, IPL_VM);
57 1.1 riastrad idr_init(&linux_writecomb.idr);
58 1.1 riastrad
59 1.1 riastrad return 0;
60 1.1 riastrad }
61 1.1 riastrad
62 1.1 riastrad void
63 1.1 riastrad linux_writecomb_fini(void)
64 1.1 riastrad {
65 1.1 riastrad
66 1.1 riastrad KASSERT(idr_is_empty(&linux_writecomb.idr));
67 1.1 riastrad idr_destroy(&linux_writecomb.idr);
68 1.1 riastrad mutex_destroy(&linux_writecomb.lock);
69 1.1 riastrad }
70 1.1 riastrad
71 1.1 riastrad int
72 1.1 riastrad arch_phys_wc_add(unsigned long base, unsigned long size)
73 1.1 riastrad {
74 1.1 riastrad struct mtrr *mtrr;
75 1.1 riastrad int n = 1;
76 1.1 riastrad int id;
77 1.1 riastrad int ret;
78 1.1 riastrad
79 1.1 riastrad mtrr = kmem_alloc(sizeof(*mtrr), KM_SLEEP);
80 1.1 riastrad mtrr->base = base;
81 1.1 riastrad mtrr->len = size;
82 1.1 riastrad mtrr->type = MTRR_TYPE_WC;
83 1.1 riastrad mtrr->flags = MTRR_VALID;
84 1.1 riastrad
85 1.1 riastrad /* XXX errno NetBSD->Linux */
86 1.1 riastrad ret = -mtrr_set(mtrr, &n, NULL, MTRR_GETSET_KERNEL);
87 1.1 riastrad if (ret) {
88 1.1 riastrad KASSERT(n == 0);
89 1.1 riastrad goto fail0;
90 1.1 riastrad }
91 1.1 riastrad KASSERT(n == 1);
92 1.1 riastrad
93 1.1 riastrad idr_preload(GFP_KERNEL);
94 1.1 riastrad mutex_spin_enter(&linux_writecomb.lock);
95 1.1 riastrad id = idr_alloc(&linux_writecomb.idr, mtrr, 0, 0, GFP_NOWAIT);
96 1.1 riastrad mutex_spin_exit(&linux_writecomb.lock);
97 1.1 riastrad idr_preload_end();
98 1.1 riastrad if (id < 0)
99 1.1 riastrad goto fail1;
100 1.1 riastrad
101 1.1 riastrad return id;
102 1.1 riastrad
103 1.1 riastrad fail1: KASSERT(id < 0);
104 1.1 riastrad mtrr->type = 0;
105 1.1 riastrad mtrr->flags = 0;
106 1.1 riastrad /* XXX errno NetBSD->Linux */
107 1.1 riastrad ret = -mtrr_set(mtrr, &n, NULL, MTRR_GETSET_KERNEL);
108 1.1 riastrad KASSERT(ret == 0);
109 1.1 riastrad KASSERT(n == 1);
110 1.1 riastrad ret = id;
111 1.1 riastrad fail0: KASSERT(ret < 0);
112 1.1 riastrad kmem_free(mtrr, sizeof(*mtrr));
113 1.1 riastrad return ret;
114 1.1 riastrad }
115 1.1 riastrad
116 1.1 riastrad void
117 1.1 riastrad arch_phys_wc_del(int id)
118 1.1 riastrad {
119 1.1 riastrad struct mtrr *mtrr;
120 1.1 riastrad int n;
121 1.1 riastrad int ret __diagused;
122 1.1 riastrad
123 1.1 riastrad KASSERT(0 <= id);
124 1.1 riastrad
125 1.1 riastrad mutex_spin_enter(&linux_writecomb.lock);
126 1.1 riastrad mtrr = idr_find(&linux_writecomb.idr, id);
127 1.1 riastrad idr_remove(&linux_writecomb.idr, id);
128 1.1 riastrad mutex_spin_enter(&linux_writecomb.lock);
129 1.1 riastrad
130 1.1 riastrad if (mtrr != NULL) {
131 1.1 riastrad mtrr->type = 0;
132 1.1 riastrad mtrr->flags = 0;
133 1.1 riastrad /* XXX errno NetBSD->Linux */
134 1.1 riastrad ret = -mtrr_set(mtrr, &n, NULL, MTRR_GETSET_KERNEL);
135 1.1 riastrad KASSERT(ret == 0);
136 1.1 riastrad KASSERT(n == 1);
137 1.1 riastrad kmem_free(mtrr, sizeof(*mtrr));
138 1.1 riastrad }
139 1.1 riastrad }
140 1.1 riastrad
141 1.1 riastrad int
142 1.1 riastrad phys_wc_to_mtrr_index(int handle)
143 1.1 riastrad {
144 1.1 riastrad
145 1.1 riastrad /* XXX Actually implement this...requires changes to our MTRR API. */
146 1.1 riastrad return handle;
147 1.1 riastrad }
148