1 /*
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 * Authors: Ben Skeggs
23 */
24 #include "user.h"
25
26 #include <core/client.h>
27 #include <core/gpuobj.h>
28 #include <subdev/fb.h>
29 #include <subdev/instmem.h>
30
31 #include <nvif/cl0002.h>
32 #include <nvif/unpack.h>
33
34 static const struct nvkm_object_func nvkm_dmaobj_func;
35 struct nvkm_dmaobj *
nvkm_dmaobj_search(struct nvkm_client * client,u64 handle)36 nvkm_dmaobj_search(struct nvkm_client *client, u64 handle)
37 {
38 struct nvkm_object *object;
39
40 object = nvkm_object_search(client, handle, &nvkm_dmaobj_func);
41 if (IS_ERR(object))
42 return (void *)object;
43
44 return nvkm_dmaobj(object);
45 }
46
47 static int
nvkm_dmaobj_bind(struct nvkm_object * base,struct nvkm_gpuobj * gpuobj,int align,struct nvkm_gpuobj ** pgpuobj)48 nvkm_dmaobj_bind(struct nvkm_object *base, struct nvkm_gpuobj *gpuobj,
49 int align, struct nvkm_gpuobj **pgpuobj)
50 {
51 struct nvkm_dmaobj *dmaobj = nvkm_dmaobj(base);
52 return dmaobj->func->bind(dmaobj, gpuobj, align, pgpuobj);
53 }
54
55 static void *
nvkm_dmaobj_dtor(struct nvkm_object * base)56 nvkm_dmaobj_dtor(struct nvkm_object *base)
57 {
58 return nvkm_dmaobj(base);
59 }
60
61 static const struct nvkm_object_func
62 nvkm_dmaobj_func = {
63 .dtor = nvkm_dmaobj_dtor,
64 .bind = nvkm_dmaobj_bind,
65 };
66
67 int
nvkm_dmaobj_ctor(const struct nvkm_dmaobj_func * func,struct nvkm_dma * dma,const struct nvkm_oclass * oclass,void ** pdata,u32 * psize,struct nvkm_dmaobj * dmaobj)68 nvkm_dmaobj_ctor(const struct nvkm_dmaobj_func *func, struct nvkm_dma *dma,
69 const struct nvkm_oclass *oclass, void **pdata, u32 *psize,
70 struct nvkm_dmaobj *dmaobj)
71 {
72 union {
73 struct nv_dma_v0 v0;
74 } *args = *pdata;
75 struct nvkm_device *device = dma->engine.subdev.device;
76 struct nvkm_client *client = oclass->client;
77 struct nvkm_object *parent = oclass->parent;
78 struct nvkm_instmem *instmem = device->imem;
79 struct nvkm_fb *fb = device->fb;
80 void *data = *pdata;
81 u32 size = *psize;
82 int ret = -ENOSYS;
83
84 nvkm_object_ctor(&nvkm_dmaobj_func, oclass, &dmaobj->object);
85 dmaobj->func = func;
86 dmaobj->dma = dma;
87
88 nvif_ioctl(parent, "create dma size %d\n", *psize);
89 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
90 nvif_ioctl(parent, "create dma vers %d target %d access %d "
91 "start %016llx limit %016llx\n",
92 args->v0.version, args->v0.target, args->v0.access,
93 args->v0.start, args->v0.limit);
94 dmaobj->target = args->v0.target;
95 dmaobj->access = args->v0.access;
96 dmaobj->start = args->v0.start;
97 dmaobj->limit = args->v0.limit;
98 } else
99 return ret;
100
101 *pdata = data;
102 *psize = size;
103
104 if (dmaobj->start > dmaobj->limit)
105 return -EINVAL;
106
107 switch (dmaobj->target) {
108 case NV_DMA_V0_TARGET_VM:
109 dmaobj->target = NV_MEM_TARGET_VM;
110 break;
111 case NV_DMA_V0_TARGET_VRAM:
112 if (!client->super) {
113 if (dmaobj->limit >= fb->ram->size - instmem->reserved)
114 return -EACCES;
115 if (device->card_type >= NV_50)
116 return -EACCES;
117 }
118 dmaobj->target = NV_MEM_TARGET_VRAM;
119 break;
120 case NV_DMA_V0_TARGET_PCI:
121 if (!client->super)
122 return -EACCES;
123 dmaobj->target = NV_MEM_TARGET_PCI;
124 break;
125 case NV_DMA_V0_TARGET_PCI_US:
126 case NV_DMA_V0_TARGET_AGP:
127 if (!client->super)
128 return -EACCES;
129 dmaobj->target = NV_MEM_TARGET_PCI_NOSNOOP;
130 break;
131 default:
132 return -EINVAL;
133 }
134
135 switch (dmaobj->access) {
136 case NV_DMA_V0_ACCESS_VM:
137 dmaobj->access = NV_MEM_ACCESS_VM;
138 break;
139 case NV_DMA_V0_ACCESS_RD:
140 dmaobj->access = NV_MEM_ACCESS_RO;
141 break;
142 case NV_DMA_V0_ACCESS_WR:
143 dmaobj->access = NV_MEM_ACCESS_WO;
144 break;
145 case NV_DMA_V0_ACCESS_RDWR:
146 dmaobj->access = NV_MEM_ACCESS_RW;
147 break;
148 default:
149 return -EINVAL;
150 }
151
152 return ret;
153 }
154