nouveau_nvif.c revision 1.4.2.3 1 /* $NetBSD: nouveau_nvif.c,v 1.4.2.3 2018/09/30 01:45:54 pgoyette Exp $ */
2
3 /*
4 * Copyright 2014 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs <bskeggs (at) redhat.com>
25 */
26
27 /*******************************************************************************
28 * NVIF client driver - NVKM directly linked
29 ******************************************************************************/
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvif.c,v 1.4.2.3 2018/09/30 01:45:54 pgoyette Exp $");
33
34 #include <core/client.h>
35 #include <core/notify.h>
36 #include <core/ioctl.h>
37
38 #include <nvif/client.h>
39 #include <nvif/driver.h>
40 #include <nvif/notify.h>
41 #include <nvif/event.h>
42 #include <nvif/ioctl.h>
43
44 #include "nouveau_drm.h"
45 #include "nouveau_usif.h"
46
47 #ifdef __NetBSD__
48 #define __iomem __nvif_iomem
49 static int
50 nvkm_client_map(void *priv, bus_space_tag_t tag, u64 busaddr, u32 size,
51 bus_space_handle_t *handlep, void __iomem **ptrp)
52 {
53 struct nvkm_client *client = nvxx_client(priv);
54 int ret;
55
56 if (tag == client->mmiot &&
57 client->mmioaddr <= busaddr &&
58 busaddr - client->mmioaddr <= client->mmiosz) {
59 const bus_size_t offset = busaddr - client->mmioaddr;
60 if (size > client->mmiosz - offset) {
61 DRM_ERROR("Invalid register access\n");
62 return -EFAULT;
63 }
64 ret = -bus_space_subregion(client->mmiot, client->mmioh,
65 offset, size, handlep);
66 if (ret)
67 return ret;
68 *ptrp = bus_space_vaddr(tag, *handlep);
69 return 0;
70 }
71
72 ret = -bus_space_map(tag, busaddr, size, BUS_SPACE_MAP_LINEAR,
73 handlep);
74 if (ret)
75 return ret;
76 *ptrp = bus_space_vaddr(tag, *handlep);
77 return 0;
78 }
79
80 static void
81 nvkm_client_unmap(void *priv, bus_space_tag_t tag, bus_space_handle_t handle,
82 bus_addr_t busaddr, void __iomem *ptr, u32 size)
83 {
84 struct nvkm_client *client = nvxx_client(priv);
85
86 KASSERTMSG(ptr == bus_space_vaddr(tag, handle),
87 "nvkm_client ptr %p != %p [bus_space_vaddr(%p, %"PRIxVADDR")]",
88 ptr, bus_space_vaddr(tag, handle), tag, handle);
89
90 if (tag == client->mmiot &&
91 client->mmioaddr <= busaddr &&
92 busaddr - client->mmioaddr <= client->mmiosz) {
93 __diagused const bus_size_t offset = busaddr - client->mmioaddr;
94 KASSERT(size <= client->mmiosz - offset);
95 /* Nothing to do to release a subregion. */
96 return;
97 }
98
99 bus_space_unmap(tag, handle, size);
100 }
101 #else
102 static void
103 nvkm_client_unmap(void *priv, void __iomem *ptr, u32 size)
104 {
105 iounmap(ptr);
106 }
107
108 static void __iomem *
109 nvkm_client_map(void *priv, u64 handle, u32 size)
110 {
111 return ioremap(handle, size);
112 }
113 #endif
114
115 static int
116 nvkm_client_ioctl(void *priv, bool super, void *data, u32 size, void **hack)
117 {
118 return nvkm_ioctl(priv, super, data, size, hack);
119 }
120
121 static int
122 nvkm_client_resume(void *priv)
123 {
124 return nvkm_client_init(priv);
125 }
126
127 static int
128 nvkm_client_suspend(void *priv)
129 {
130 return nvkm_client_fini(priv, true);
131 }
132
133 static void
134 nvkm_client_driver_fini(void *priv)
135 {
136 struct nvkm_client *client = priv;
137 nvkm_client_del(&client);
138 }
139
140 static int
141 nvkm_client_ntfy(const void *header, u32 length, const void *data, u32 size)
142 {
143 const union {
144 struct nvif_notify_req_v0 v0;
145 } *args = header;
146 u8 route;
147
148 if (length == sizeof(args->v0) && args->v0.version == 0) {
149 route = args->v0.route;
150 } else {
151 WARN_ON(1);
152 return NVKM_NOTIFY_DROP;
153 }
154
155 switch (route) {
156 case NVDRM_NOTIFY_NVIF:
157 return nvif_notify(header, length, data, size);
158 case NVDRM_NOTIFY_USIF:
159 return usif_notify(header, length, data, size);
160 default:
161 WARN_ON(1);
162 break;
163 }
164
165 return NVKM_NOTIFY_DROP;
166 }
167
168 static int
169 nvkm_client_driver_init(const char *name, u64 device, const char *cfg,
170 const char *dbg, void **ppriv)
171 {
172 struct nvkm_client *client;
173 int ret;
174
175 ret = nvkm_client_new(name, device, cfg, dbg, &client);
176 *ppriv = client;
177 if (ret)
178 return ret;
179
180 client->ntfy = nvkm_client_ntfy;
181 return 0;
182 }
183
184 const struct nvif_driver
185 nvif_driver_nvkm = {
186 .name = "nvkm",
187 .init = nvkm_client_driver_init,
188 .fini = nvkm_client_driver_fini,
189 .suspend = nvkm_client_suspend,
190 .resume = nvkm_client_resume,
191 .ioctl = nvkm_client_ioctl,
192 .map = nvkm_client_map,
193 .unmap = nvkm_client_unmap,
194 .keep = false,
195 };
196