blob: cbf1fc60a38682be197dc1bb1f0f15c8853a06e6 [file] [log] [blame]
Ben Skeggs2a259a32012-05-08 10:24:27 +10001/*
2 * Copyright 2012 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23
Ben Skeggsebb945a2012-07-20 08:17:34 +100024#include <core/object.h>
25#include <core/client.h>
26#include <core/device.h>
27#include <core/class.h>
28#include <core/mm.h>
Ben Skeggs2a259a32012-05-08 10:24:27 +100029
Ben Skeggsebb945a2012-07-20 08:17:34 +100030#include <subdev/fb.h>
31#include <subdev/timer.h>
32#include <subdev/instmem.h>
33
34#include "nouveau_drm.h"
Ben Skeggs2a259a32012-05-08 10:24:27 +100035#include "nouveau_dma.h"
Ben Skeggsebb945a2012-07-20 08:17:34 +100036#include "nouveau_gem.h"
37#include "nouveau_chan.h"
Ben Skeggs2a259a32012-05-08 10:24:27 +100038#include "nouveau_abi16.h"
Ben Skeggsebb945a2012-07-20 08:17:34 +100039
40struct nouveau_abi16 *
41nouveau_abi16_get(struct drm_file *file_priv, struct drm_device *dev)
42{
43 struct nouveau_cli *cli = nouveau_cli(file_priv);
44 mutex_lock(&cli->mutex);
45 if (!cli->abi16) {
46 struct nouveau_abi16 *abi16;
47 cli->abi16 = abi16 = kzalloc(sizeof(*abi16), GFP_KERNEL);
48 if (cli->abi16) {
49 INIT_LIST_HEAD(&abi16->channels);
50 abi16->client = nv_object(cli);
51
52 /* allocate device object targeting client's default
53 * device (ie. the one that belongs to the fd it
54 * opened)
55 */
56 if (nouveau_object_new(abi16->client, NVDRM_CLIENT,
57 NVDRM_DEVICE, 0x0080,
58 &(struct nv_device_class) {
59 .device = ~0ULL,
60 },
61 sizeof(struct nv_device_class),
62 &abi16->device) == 0)
63 return cli->abi16;
64
65 kfree(cli->abi16);
66 cli->abi16 = NULL;
67 }
68
69 mutex_unlock(&cli->mutex);
70 }
71 return cli->abi16;
72}
73
74int
75nouveau_abi16_put(struct nouveau_abi16 *abi16, int ret)
76{
77 struct nouveau_cli *cli = (void *)abi16->client;
78 mutex_unlock(&cli->mutex);
79 return ret;
80}
81
82u16
83nouveau_abi16_swclass(struct nouveau_drm *drm)
84{
85 switch (nv_device(drm->device)->card_type) {
86 case NV_04:
87 return 0x006e;
88 case NV_10:
89 case NV_20:
90 case NV_30:
91 case NV_40:
92 return 0x016e;
93 case NV_50:
94 return 0x506e;
95 case NV_C0:
96 case NV_D0:
97 case NV_E0:
98 return 0x906e;
99 }
100
101 return 0x0000;
102}
103
104static void
105nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan,
106 struct nouveau_abi16_ntfy *ntfy)
107{
108 nouveau_mm_free(&chan->heap, &ntfy->node);
109 list_del(&ntfy->head);
110 kfree(ntfy);
111}
112
113static void
114nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16,
115 struct nouveau_abi16_chan *chan)
116{
117 struct nouveau_abi16_ntfy *ntfy, *temp;
118
119 /* cleanup notifier state */
120 list_for_each_entry_safe(ntfy, temp, &chan->notifiers, head) {
121 nouveau_abi16_ntfy_fini(chan, ntfy);
122 }
123
124 if (chan->ntfy) {
125 nouveau_bo_vma_del(chan->ntfy, &chan->ntfy_vma);
126 drm_gem_object_unreference_unlocked(chan->ntfy->gem);
127 }
128
129 if (chan->heap.block_size)
130 nouveau_mm_fini(&chan->heap);
131
132 /* destroy channel object, all children will be killed too */
133 if (chan->chan) {
134 abi16->handles &= ~(1 << (chan->chan->handle & 0xffff));
135 nouveau_channel_del(&chan->chan);
136 }
137
138 list_del(&chan->head);
139 kfree(chan);
140}
141
142void
143nouveau_abi16_fini(struct nouveau_abi16 *abi16)
144{
145 struct nouveau_cli *cli = (void *)abi16->client;
146 struct nouveau_abi16_chan *chan, *temp;
147
148 /* cleanup channels */
149 list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
150 nouveau_abi16_chan_fini(abi16, chan);
151 }
152
153 /* destroy the device object */
154 nouveau_object_del(abi16->client, NVDRM_CLIENT, NVDRM_DEVICE);
155
156 kfree(cli->abi16);
157 cli->abi16 = NULL;
158}
Ben Skeggs2a259a32012-05-08 10:24:27 +1000159
160int
161nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
162{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000163 struct nouveau_drm *drm = nouveau_drm(dev);
164 struct nouveau_device *device = nv_device(drm->device);
165 struct nouveau_timer *ptimer = nouveau_timer(device);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000166 struct drm_nouveau_getparam *getparam = data;
167
168 switch (getparam->param) {
169 case NOUVEAU_GETPARAM_CHIPSET_ID:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000170 getparam->value = device->chipset;
Ben Skeggs2a259a32012-05-08 10:24:27 +1000171 break;
172 case NOUVEAU_GETPARAM_PCI_VENDOR:
173 getparam->value = dev->pci_vendor;
174 break;
175 case NOUVEAU_GETPARAM_PCI_DEVICE:
176 getparam->value = dev->pci_device;
177 break;
178 case NOUVEAU_GETPARAM_BUS_TYPE:
179 if (drm_pci_device_is_agp(dev))
180 getparam->value = 0;
181 else
182 if (!pci_is_pcie(dev->pdev))
183 getparam->value = 1;
184 else
185 getparam->value = 2;
186 break;
187 case NOUVEAU_GETPARAM_FB_SIZE:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000188 getparam->value = drm->gem.vram_available;
Ben Skeggs2a259a32012-05-08 10:24:27 +1000189 break;
190 case NOUVEAU_GETPARAM_AGP_SIZE:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000191 getparam->value = drm->gem.gart_available;
Ben Skeggs2a259a32012-05-08 10:24:27 +1000192 break;
193 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
194 getparam->value = 0; /* deprecated */
195 break;
196 case NOUVEAU_GETPARAM_PTIMER_TIME:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000197 getparam->value = ptimer->read(ptimer);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000198 break;
199 case NOUVEAU_GETPARAM_HAS_BO_USAGE:
200 getparam->value = 1;
201 break;
202 case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
203 getparam->value = 1;
204 break;
205 case NOUVEAU_GETPARAM_GRAPH_UNITS:
206 /* NV40 and NV50 versions are quite different, but register
207 * address is the same. User is supposed to know the card
208 * family anyway... */
Ben Skeggsebb945a2012-07-20 08:17:34 +1000209 if (device->chipset >= 0x40) {
210 getparam->value = nv_rd32(device, 0x001540);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000211 break;
212 }
213 /* FALLTHRU */
214 default:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000215 nv_debug(device, "unknown parameter %lld\n", getparam->param);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000216 return -EINVAL;
217 }
218
219 return 0;
220}
221
222int
223nouveau_abi16_ioctl_setparam(ABI16_IOCTL_ARGS)
224{
225 return -EINVAL;
226}
227
228int
229nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)
230{
Ben Skeggs2a259a32012-05-08 10:24:27 +1000231 struct drm_nouveau_channel_alloc *init = data;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000232 struct nouveau_cli *cli = nouveau_cli(file_priv);
233 struct nouveau_drm *drm = nouveau_drm(dev);
234 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
235 struct nouveau_abi16_chan *chan;
236 struct nouveau_client *client;
237 struct nouveau_device *device;
238 struct nouveau_instmem *imem;
239 struct nouveau_fb *pfb;
Ben Skeggs2a259a32012-05-08 10:24:27 +1000240 int ret;
241
Ben Skeggsebb945a2012-07-20 08:17:34 +1000242 if (unlikely(!abi16))
243 return -ENOMEM;
Marcin Slusarzbf7e4382012-11-11 20:00:09 +0100244
245 if (!drm->channel)
246 return nouveau_abi16_put(abi16, -ENODEV);
247
Ben Skeggsebb945a2012-07-20 08:17:34 +1000248 client = nv_client(abi16->client);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000249
250 if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000251 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000252
Ben Skeggsebb945a2012-07-20 08:17:34 +1000253 device = nv_device(abi16->device);
254 imem = nouveau_instmem(device);
255 pfb = nouveau_fb(device);
256
257 /* allocate "abi16 channel" data and make up a handle for it */
258 init->channel = ffsll(~abi16->handles);
259 if (!init->channel--)
260 return nouveau_abi16_put(abi16, -ENOSPC);
261
262 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
263 if (!chan)
264 return nouveau_abi16_put(abi16, -ENOMEM);
265
266 INIT_LIST_HEAD(&chan->notifiers);
267 list_add(&chan->head, &abi16->channels);
268 abi16->handles |= (1 << init->channel);
269
270 /* create channel object and initialise dma and fence management */
Ben Skeggs49981042012-08-06 19:38:25 +1000271 if (device->card_type >= NV_E0) {
272 init->fb_ctxdma_handle = NVE0_CHANNEL_IND_ENGINE_GR;
273 init->tt_ctxdma_handle = 0;
274 }
275
Ben Skeggsebb945a2012-07-20 08:17:34 +1000276 ret = nouveau_channel_new(drm, cli, NVDRM_DEVICE, NVDRM_CHAN |
277 init->channel, init->fb_ctxdma_handle,
278 init->tt_ctxdma_handle, &chan->chan);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000279 if (ret)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000280 goto done;
Ben Skeggs2a259a32012-05-08 10:24:27 +1000281
Ben Skeggsebb945a2012-07-20 08:17:34 +1000282 if (device->card_type >= NV_50)
283 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
284 NOUVEAU_GEM_DOMAIN_GART;
285 else
286 if (chan->chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM)
Ben Skeggs2a259a32012-05-08 10:24:27 +1000287 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000288 else
289 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
Ben Skeggs2a259a32012-05-08 10:24:27 +1000290
Ben Skeggsebb945a2012-07-20 08:17:34 +1000291 if (device->card_type < NV_C0) {
Ben Skeggs2a259a32012-05-08 10:24:27 +1000292 init->subchan[0].handle = 0x00000000;
293 init->subchan[0].grclass = 0x0000;
294 init->subchan[1].handle = NvSw;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000295 init->subchan[1].grclass = 0x506e;
Ben Skeggs2a259a32012-05-08 10:24:27 +1000296 init->nr_subchan = 2;
297 }
298
299 /* Named memory object area */
Ben Skeggsebb945a2012-07-20 08:17:34 +1000300 ret = nouveau_gem_new(dev, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART,
301 0, 0, &chan->ntfy);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000302 if (ret == 0)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000303 ret = nouveau_bo_pin(chan->ntfy, TTM_PL_FLAG_TT);
304 if (ret)
305 goto done;
306
307 if (device->card_type >= NV_50) {
308 ret = nouveau_bo_vma_add(chan->ntfy, client->vm,
309 &chan->ntfy_vma);
310 if (ret)
311 goto done;
312 }
313
314 ret = drm_gem_handle_create(file_priv, chan->ntfy->gem,
315 &init->notifier_handle);
316 if (ret)
317 goto done;
318
319 ret = nouveau_mm_init(&chan->heap, 0, PAGE_SIZE, 1);
320done:
321 if (ret)
322 nouveau_abi16_chan_fini(abi16, chan);
323 return nouveau_abi16_put(abi16, ret);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000324}
325
Ben Skeggsebb945a2012-07-20 08:17:34 +1000326
Ben Skeggs2a259a32012-05-08 10:24:27 +1000327int
328nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS)
329{
330 struct drm_nouveau_channel_free *req = data;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000331 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
332 struct nouveau_abi16_chan *chan;
333 int ret = -ENOENT;
Ben Skeggs2a259a32012-05-08 10:24:27 +1000334
Ben Skeggsebb945a2012-07-20 08:17:34 +1000335 if (unlikely(!abi16))
336 return -ENOMEM;
Ben Skeggs2a259a32012-05-08 10:24:27 +1000337
Ben Skeggsebb945a2012-07-20 08:17:34 +1000338 list_for_each_entry(chan, &abi16->channels, head) {
339 if (chan->chan->handle == (NVDRM_CHAN | req->channel)) {
340 nouveau_abi16_chan_fini(abi16, chan);
341 return nouveau_abi16_put(abi16, 0);
342 }
343 }
344
345 return nouveau_abi16_put(abi16, ret);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000346}
347
348int
349nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
350{
351 struct drm_nouveau_grobj_alloc *init = data;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000352 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
353 struct nouveau_drm *drm = nouveau_drm(dev);
354 struct nouveau_object *object;
Ben Skeggs2a259a32012-05-08 10:24:27 +1000355 int ret;
356
Ben Skeggsebb945a2012-07-20 08:17:34 +1000357 if (unlikely(!abi16))
358 return -ENOMEM;
359
Ben Skeggs2a259a32012-05-08 10:24:27 +1000360 if (init->handle == ~0)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000361 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000362
363 /* compatibility with userspace that assumes 506e for all chipsets */
364 if (init->class == 0x506e) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000365 init->class = nouveau_abi16_swclass(drm);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000366 if (init->class == 0x906e)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000367 return nouveau_abi16_put(abi16, 0);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000368 }
369
Ben Skeggsebb945a2012-07-20 08:17:34 +1000370 ret = nouveau_object_new(abi16->client, NVDRM_CHAN | init->channel,
371 init->handle, init->class, NULL, 0, &object);
372 return nouveau_abi16_put(abi16, ret);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000373}
374
375int
376nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
377{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000378 struct drm_nouveau_notifierobj_alloc *info = data;
379 struct nouveau_drm *drm = nouveau_drm(dev);
380 struct nouveau_device *device = nv_device(drm->device);
381 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
382 struct nouveau_abi16_chan *chan, *temp;
383 struct nouveau_abi16_ntfy *ntfy;
384 struct nouveau_object *object;
385 struct nv_dma_class args;
Ben Skeggs2a259a32012-05-08 10:24:27 +1000386 int ret;
387
Ben Skeggsebb945a2012-07-20 08:17:34 +1000388 if (unlikely(!abi16))
389 return -ENOMEM;
390
Ben Skeggs2a259a32012-05-08 10:24:27 +1000391 /* completely unnecessary for these chipsets... */
Ben Skeggsebb945a2012-07-20 08:17:34 +1000392 if (unlikely(nv_device(abi16->device)->card_type >= NV_C0))
393 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000394
Ben Skeggsebb945a2012-07-20 08:17:34 +1000395 list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
396 if (chan->chan->handle == (NVDRM_CHAN | info->channel))
397 break;
398 chan = NULL;
399 }
Ben Skeggs2a259a32012-05-08 10:24:27 +1000400
Ben Skeggsebb945a2012-07-20 08:17:34 +1000401 if (!chan)
402 return nouveau_abi16_put(abi16, -ENOENT);
403
404 ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
405 if (!ntfy)
406 return nouveau_abi16_put(abi16, -ENOMEM);
407
408 list_add(&ntfy->head, &chan->notifiers);
409 ntfy->handle = info->handle;
410
411 ret = nouveau_mm_head(&chan->heap, 1, info->size, info->size, 1,
412 &ntfy->node);
413 if (ret)
414 goto done;
415
416 args.start = ntfy->node->offset;
417 args.limit = ntfy->node->offset + ntfy->node->length - 1;
418 if (device->card_type >= NV_50) {
419 args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
420 args.start += chan->ntfy_vma.offset;
421 args.limit += chan->ntfy_vma.offset;
422 } else
423 if (drm->agp.stat == ENABLED) {
424 args.flags = NV_DMA_TARGET_AGP | NV_DMA_ACCESS_RDWR;
425 args.start += drm->agp.base + chan->ntfy->bo.offset;
426 args.limit += drm->agp.base + chan->ntfy->bo.offset;
427 } else {
428 args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_RDWR;
429 args.start += chan->ntfy->bo.offset;
430 args.limit += chan->ntfy->bo.offset;
431 }
432
433 ret = nouveau_object_new(abi16->client, chan->chan->handle,
434 ntfy->handle, 0x003d, &args,
435 sizeof(args), &object);
436 if (ret)
437 goto done;
438
439done:
440 if (ret)
441 nouveau_abi16_ntfy_fini(chan, ntfy);
442 return nouveau_abi16_put(abi16, ret);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000443}
444
445int
446nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
447{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000448 struct drm_nouveau_gpuobj_free *fini = data;
449 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
450 struct nouveau_abi16_chan *chan, *temp;
451 struct nouveau_abi16_ntfy *ntfy;
Ben Skeggs2a259a32012-05-08 10:24:27 +1000452 int ret;
453
Ben Skeggsebb945a2012-07-20 08:17:34 +1000454 if (unlikely(!abi16))
455 return -ENOMEM;
Ben Skeggs2a259a32012-05-08 10:24:27 +1000456
Ben Skeggsebb945a2012-07-20 08:17:34 +1000457 list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
458 if (chan->chan->handle == (NVDRM_CHAN | fini->channel))
459 break;
460 chan = NULL;
461 }
Ben Skeggs2a259a32012-05-08 10:24:27 +1000462
Ben Skeggsebb945a2012-07-20 08:17:34 +1000463 if (!chan)
464 return nouveau_abi16_put(abi16, -ENOENT);
465
466 /* synchronize with the user channel and destroy the gpu object */
467 nouveau_channel_idle(chan->chan);
468
469 ret = nouveau_object_del(abi16->client, chan->chan->handle, fini->handle);
470 if (ret)
471 return nouveau_abi16_put(abi16, ret);
472
473 /* cleanup extra state if this object was a notifier */
474 list_for_each_entry(ntfy, &chan->notifiers, head) {
475 if (ntfy->handle == fini->handle) {
476 nouveau_mm_free(&chan->heap, &ntfy->node);
477 list_del(&ntfy->head);
478 break;
479 }
480 }
481
482 return nouveau_abi16_put(abi16, 0);
Ben Skeggs2a259a32012-05-08 10:24:27 +1000483}