1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/*
* Copyright 2012 Red Hat Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Authors: Ben Skeggs
*/
#include <nvif/client.h>
#include <nvif/driver.h>
#include <nvif/notify.h>
#include <nvif/ioctl.h>
#include <nvif/class.h>
#include <nvif/event.h>
#include <core/object.h>
#include <core/handle.h>
#include <core/client.h>
#include <core/device.h>
#include <core/ioctl.h>
#include <core/event.h>
#include <subdev/mc.h>
#include "priv.h"
static DEFINE_MUTEX(os_mutex);
static LIST_HEAD(os_device_list);
static int os_client_nr = 0;
/******************************************************************************
* horrific stuff to implement linux's ioremap interface on top of pciaccess
*****************************************************************************/
static DEFINE_MUTEX(os_ioremap_mutex);
static struct os_ioremap_info {
struct pci_device *pdev;
int refs;
u64 addr;
u64 size;
void *ptr;
} os_ioremap[32];
void __iomem *
nvos_ioremap_bar(struct pci_device *pdev, int bar, u64 addr)
{
u64 base = pdev->regions[bar].base_addr;
u64 size = pdev->regions[bar].size;
u64 offset = addr - base;
void __iomem *ptr = NULL;
int i;
mutex_lock(&os_ioremap_mutex);
for (i = 0; !ptr && i < ARRAY_SIZE(os_ioremap); i++) {
if (os_ioremap[i].refs && os_ioremap[i].addr == base) {
os_ioremap[i].refs++;
ptr = os_ioremap[i].ptr + offset;
}
}
for (i = 0; !ptr && i < ARRAY_SIZE(os_ioremap); i++) {
if (!os_ioremap[i].refs &&
!pci_device_map_range(pdev, base, size,
PCI_DEV_MAP_FLAG_WRITABLE,
&os_ioremap[i].ptr)) {
os_ioremap[i].pdev = pdev;
os_ioremap[i].refs = 1;
os_ioremap[i].addr = base;
os_ioremap[i].size = size;
ptr = os_ioremap[i].ptr + offset;
}
}
mutex_unlock(&os_ioremap_mutex);
return ptr;
}
void __iomem *
nvos_ioremap(u64 addr, u64 size)
{
struct os_device *odev;
int i;
list_for_each_entry(odev, &os_device_list, head) {
struct pci_device *pdev = odev->pdev.pdev;
for (i = 0; i < ARRAY_SIZE(pdev->regions); i++) {
if (addr >= pdev->regions[i].base_addr &&
addr + size <= pdev->regions[i].base_addr +
pdev->regions[i].size) {
return nvos_ioremap_bar(pdev, i, addr);
}
}
}
return NULL;
}
void
nvos_iounmap(void __iomem *ptr)
{
int i;
mutex_lock(&os_ioremap_mutex);
for (i = 0; ptr && i < ARRAY_SIZE(os_ioremap); i++) {
if (os_ioremap[i].refs &&
ptr >= os_ioremap[i].ptr &&
ptr < os_ioremap[i].ptr + os_ioremap[i].size) {
if (!--os_ioremap[i].refs) {
pci_device_unmap_range(os_ioremap[i].pdev,
os_ioremap[i].ptr,
os_ioremap[i].size);
}
break;
}
}
mutex_unlock(&os_ioremap_mutex);
}
/******************************************************************************
* client interfaces
*****************************************************************************/
static void
os_fini_device(struct os_device *odev)
{
nvkm_device_del(&odev->device);
list_del(&odev->head);
kfree(odev);
}
static int
os_init_device(struct pci_device *pdev, u64 handle, const char *cfg, const char *dbg)
{
struct os_device *odev;
int ret;
ret = pci_device_probe(pdev);
if (ret) {
fprintf(stderr, "pci_device_probe failed, %d\n", ret);
return ret;
}
odev = calloc(1, sizeof(*odev));
if (!odev)
return -ENOMEM;
snprintf(odev->pdev.dev.name, sizeof(odev->pdev.dev.name),
"%04x:%02x:%02x.%1x",
pdev->domain, pdev->bus, pdev->dev, pdev->func);
odev->pdev.pdev = pdev;
odev->pdev.device = pdev->vendor_id;
odev->pdev.subsystem_vendor = pdev->subvendor_id;
odev->pdev.subsystem_device = pdev->subdevice_id;
list_add_tail(&odev->head, &os_device_list);
ret = nvkm_device_new(&odev->pdev, NVKM_BUS_PCI, handle,
odev->pdev.dev.name, cfg, dbg,
&odev->device);
if (ret) {
fprintf(stderr, "failed to create device, %d\n", ret);
os_fini_device(odev);
return ret;
}
return 0;
}
static int
os_init(const char *cfg, const char *dbg, bool init)
{
struct pci_device_iterator *iter;
struct pci_device *pdev;
u64 handle;
int ret, n = 0;
ret = pci_system_init();
if (ret) {
fprintf(stderr, "pci_system_init failed, %d\n", ret);
return ret;
}
iter = pci_slot_match_iterator_create(NULL);
while ((pdev = pci_device_next(iter))) {
if ((pdev->device_class & 0x00ff0000) != 0x00030000)
continue;
if (pdev->vendor_id != 0x10de)
continue;
handle = ((u64)pdev->domain << 32) | (pdev->bus << 16) |
(pdev->dev << 8) | pdev->func;
if (!init) {
printf("%d: 0x%010llx PCI:%04x:%02x:%02x:%02x "
"(%04x:%04x)\n", n++, handle, pdev->domain,
pdev->bus, pdev->dev, pdev->func,
pdev->vendor_id, pdev->device_id);
continue;
}
os_init_device(pdev, handle, cfg, dbg);
}
pci_iterator_destroy(iter);
return 0;
}
static void
os_fini(void)
{
struct os_device *odev, *temp;
list_for_each_entry_safe(odev, temp, &os_device_list, head) {
os_fini_device(odev);
}
pci_system_cleanup();
}
static void
os_client_unmap(void *priv, void *ptr, u32 size)
{
iounmap(ptr);
}
static void *
os_client_map(void *priv, u64 handle, u32 size)
{
return ioremap(handle, size);
}
static int
os_client_ioctl(void *priv, bool super, void *data, u32 size, void **hack)
{
return nvkm_ioctl(priv, super, data, size, hack);
}
static int
os_client_resume(void *priv)
{
return nvkm_client_init(priv);
}
static int
os_client_suspend(void *priv)
{
return nvkm_client_fini(priv, true);
}
static void
os_client_fini(void *priv)
{
struct nvkm_client *client = priv;
nvkm_client_del(&client);
mutex_lock(&os_mutex);
if (--os_client_nr == 0)
os_fini();
mutex_unlock(&os_mutex);
}
static int
os_client_init(const char *name, u64 device, const char *cfg,
const char *dbg, void **ppriv)
{
struct nvkm_client *client;
int ret;
mutex_lock(&os_mutex);
if (os_client_nr++ == 0)
os_init(cfg, dbg, true);
mutex_unlock(&os_mutex);
ret = nvkm_client_new(name, device, cfg, dbg, &client);
*ppriv = client;
if (ret == 0)
client->ntfy = nvif_notify;
return ret;
}
const struct nvif_driver
nvif_driver_lib = {
.name = "lib",
.init = os_client_init,
.fini = os_client_fini,
.suspend = os_client_suspend,
.resume = os_client_resume,
.ioctl = os_client_ioctl,
.map = os_client_map,
.unmap = os_client_unmap,
.keep = false,
};
|