1 /* $NetBSD: nouveau_nvif.c,v 1.6 2021/12/18 23:45:32 riastradh 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@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.6 2021/12/18 23:45:32 riastradh 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_drv.h"
45 #include "nouveau_usif.h"
46
47 #ifdef __NetBSD__
48 #define __iomem __nvif_iomem
49 static int
nvkm_client_map(void * priv,bus_space_tag_t tag,u64 busaddr,u32 size,bus_space_handle_t * handlep,void __iomem ** ptrp)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
nvkm_client_unmap(void * priv,bus_space_tag_t tag,bus_space_handle_t handle,bus_addr_t busaddr,void __iomem * ptr,u32 size)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
nvkm_client_unmap(void * priv,void __iomem * ptr,u32 size)103 nvkm_client_unmap(void *priv, void __iomem *ptr, u32 size)
104 {
105 iounmap(ptr);
106 }
107
108 static void __iomem *
nvkm_client_map(void * priv,u64 handle,u32 size)109 nvkm_client_map(void *priv, u64 handle, u32 size)
110 {
111 return ioremap(handle, size);
112 }
113 #endif
114
115 static int
nvkm_client_ioctl(void * priv,bool super,void * data,u32 size,void ** hack)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
nvkm_client_resume(void * priv)122 nvkm_client_resume(void *priv)
123 {
124 struct nvkm_client *client = priv;
125 return nvkm_object_init(&client->object);
126 }
127
128 static int
nvkm_client_suspend(void * priv)129 nvkm_client_suspend(void *priv)
130 {
131 struct nvkm_client *client = priv;
132 return nvkm_object_fini(&client->object, true);
133 }
134
135 static int
nvkm_client_ntfy(const void * header,u32 length,const void * data,u32 size)136 nvkm_client_ntfy(const void *header, u32 length, const void *data, u32 size)
137 {
138 const union {
139 struct nvif_notify_req_v0 v0;
140 } *args = header;
141 u8 route;
142
143 if (length == sizeof(args->v0) && args->v0.version == 0) {
144 route = args->v0.route;
145 } else {
146 WARN_ON(1);
147 return NVKM_NOTIFY_DROP;
148 }
149
150 switch (route) {
151 case NVDRM_NOTIFY_NVIF:
152 return nvif_notify(header, length, data, size);
153 case NVDRM_NOTIFY_USIF:
154 return usif_notify(header, length, data, size);
155 default:
156 WARN_ON(1);
157 break;
158 }
159
160 return NVKM_NOTIFY_DROP;
161 }
162
163 static int
nvkm_client_driver_init(const char * name,u64 device,const char * cfg,const char * dbg,void ** ppriv)164 nvkm_client_driver_init(const char *name, u64 device, const char *cfg,
165 const char *dbg, void **ppriv)
166 {
167 return nvkm_client_new(name, device, cfg, dbg, nvkm_client_ntfy,
168 (struct nvkm_client **)ppriv);
169 }
170
171 const struct nvif_driver
172 nvif_driver_nvkm = {
173 .name = "nvkm",
174 .init = nvkm_client_driver_init,
175 .suspend = nvkm_client_suspend,
176 .resume = nvkm_client_resume,
177 .ioctl = nvkm_client_ioctl,
178 .map = nvkm_client_map,
179 .unmap = nvkm_client_unmap,
180 .keep = false,
181 };
182