xref: /openbmc/linux/drivers/gpu/drm/nouveau/nvif/object.c (revision 41a634064db489713945e228e216336080ba57f8)
1a04d0423SBen Skeggs /*
2a04d0423SBen Skeggs  * Copyright 2014 Red Hat Inc.
3a04d0423SBen Skeggs  *
4a04d0423SBen Skeggs  * Permission is hereby granted, free of charge, to any person obtaining a
5a04d0423SBen Skeggs  * copy of this software and associated documentation files (the "Software"),
6a04d0423SBen Skeggs  * to deal in the Software without restriction, including without limitation
7a04d0423SBen Skeggs  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8a04d0423SBen Skeggs  * and/or sell copies of the Software, and to permit persons to whom the
9a04d0423SBen Skeggs  * Software is furnished to do so, subject to the following conditions:
10a04d0423SBen Skeggs  *
11a04d0423SBen Skeggs  * The above copyright notice and this permission notice shall be included in
12a04d0423SBen Skeggs  * all copies or substantial portions of the Software.
13a04d0423SBen Skeggs  *
14a04d0423SBen Skeggs  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15a04d0423SBen Skeggs  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16a04d0423SBen Skeggs  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17a04d0423SBen Skeggs  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18a04d0423SBen Skeggs  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19a04d0423SBen Skeggs  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20a04d0423SBen Skeggs  * OTHER DEALINGS IN THE SOFTWARE.
21a04d0423SBen Skeggs  *
22a04d0423SBen Skeggs  * Authors: Ben Skeggs <bskeggs@redhat.com>
23a04d0423SBen Skeggs  */
24a04d0423SBen Skeggs 
25c39f472eSBen Skeggs #include <nvif/object.h>
26c39f472eSBen Skeggs #include <nvif/client.h>
27c39f472eSBen Skeggs #include <nvif/driver.h>
28c39f472eSBen Skeggs #include <nvif/ioctl.h>
29a04d0423SBen Skeggs 
30a04d0423SBen Skeggs int
31a04d0423SBen Skeggs nvif_object_ioctl(struct nvif_object *object, void *data, u32 size, void **hack)
32a04d0423SBen Skeggs {
33a01ca78cSBen Skeggs 	struct nvif_client *client = object->client;
34a04d0423SBen Skeggs 	union {
35a04d0423SBen Skeggs 		struct nvif_ioctl_v0 v0;
36a04d0423SBen Skeggs 	} *args = data;
37a04d0423SBen Skeggs 
38a04d0423SBen Skeggs 	if (size >= sizeof(*args) && args->v0.version == 0) {
39bf81df9bSBen Skeggs 		if (object != &client->object)
40bf81df9bSBen Skeggs 			args->v0.object = nvif_handle(object);
41bf81df9bSBen Skeggs 		else
42bf81df9bSBen Skeggs 			args->v0.object = 0;
43a04d0423SBen Skeggs 		args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY;
44a04d0423SBen Skeggs 	} else
45a04d0423SBen Skeggs 		return -ENOSYS;
46a04d0423SBen Skeggs 
47a01ca78cSBen Skeggs 	return client->driver->ioctl(client->object.priv, client->super,
48a01ca78cSBen Skeggs 				     data, size, hack);
49a04d0423SBen Skeggs }
50a04d0423SBen Skeggs 
51*41a63406SBen Skeggs void
52*41a63406SBen Skeggs nvif_object_sclass_put(struct nvif_sclass **psclass)
53*41a63406SBen Skeggs {
54*41a63406SBen Skeggs 	kfree(*psclass);
55*41a63406SBen Skeggs 	*psclass = NULL;
56*41a63406SBen Skeggs }
57*41a63406SBen Skeggs 
58a04d0423SBen Skeggs int
59*41a63406SBen Skeggs nvif_object_sclass_get(struct nvif_object *object, struct nvif_sclass **psclass)
60a04d0423SBen Skeggs {
61a04d0423SBen Skeggs 	struct {
62a04d0423SBen Skeggs 		struct nvif_ioctl_v0 ioctl;
63a04d0423SBen Skeggs 		struct nvif_ioctl_sclass_v0 sclass;
64*41a63406SBen Skeggs 	} *args = NULL;
65*41a63406SBen Skeggs 	int ret, cnt = 0, i;
66*41a63406SBen Skeggs 	u32 size;
67a04d0423SBen Skeggs 
68*41a63406SBen Skeggs 	while (1) {
69*41a63406SBen Skeggs 		size = sizeof(*args) + cnt * sizeof(args->sclass.oclass[0]);
70*41a63406SBen Skeggs 		if (!(args = kmalloc(size, GFP_KERNEL)))
71a04d0423SBen Skeggs 			return -ENOMEM;
72a04d0423SBen Skeggs 		args->ioctl.version = 0;
73a04d0423SBen Skeggs 		args->ioctl.type = NVIF_IOCTL_V0_SCLASS;
74a04d0423SBen Skeggs 		args->sclass.version = 0;
75*41a63406SBen Skeggs 		args->sclass.count = cnt;
76a04d0423SBen Skeggs 
77*41a63406SBen Skeggs 		ret = nvif_object_ioctl(object, args, size, NULL);
78*41a63406SBen Skeggs 		if (ret == 0 && args->sclass.count <= cnt)
79*41a63406SBen Skeggs 			break;
80*41a63406SBen Skeggs 		cnt = args->sclass.count;
81*41a63406SBen Skeggs 		kfree(args);
82*41a63406SBen Skeggs 		if (ret != 0)
83*41a63406SBen Skeggs 			return ret;
84*41a63406SBen Skeggs 	}
85*41a63406SBen Skeggs 
86*41a63406SBen Skeggs 	*psclass = kzalloc(sizeof(**psclass) * args->sclass.count, GFP_KERNEL);
87*41a63406SBen Skeggs 	if (*psclass) {
88*41a63406SBen Skeggs 		for (i = 0; i < args->sclass.count; i++) {
89*41a63406SBen Skeggs 			(*psclass)[i].oclass = args->sclass.oclass[i].oclass;
90*41a63406SBen Skeggs 			(*psclass)[i].minver = args->sclass.oclass[i].minver;
91*41a63406SBen Skeggs 			(*psclass)[i].maxver = args->sclass.oclass[i].maxver;
92*41a63406SBen Skeggs 		}
93*41a63406SBen Skeggs 		ret = args->sclass.count;
94*41a63406SBen Skeggs 	} else {
95*41a63406SBen Skeggs 		ret = -ENOMEM;
96*41a63406SBen Skeggs 	}
97*41a63406SBen Skeggs 
98a04d0423SBen Skeggs 	kfree(args);
99a04d0423SBen Skeggs 	return ret;
100a04d0423SBen Skeggs }
101a04d0423SBen Skeggs 
102a04d0423SBen Skeggs u32
103a04d0423SBen Skeggs nvif_object_rd(struct nvif_object *object, int size, u64 addr)
104a04d0423SBen Skeggs {
105a04d0423SBen Skeggs 	struct {
106a04d0423SBen Skeggs 		struct nvif_ioctl_v0 ioctl;
107a04d0423SBen Skeggs 		struct nvif_ioctl_rd_v0 rd;
108a04d0423SBen Skeggs 	} args = {
109a04d0423SBen Skeggs 		.ioctl.type = NVIF_IOCTL_V0_RD,
110a04d0423SBen Skeggs 		.rd.size = size,
111a04d0423SBen Skeggs 		.rd.addr = addr,
112a04d0423SBen Skeggs 	};
113a04d0423SBen Skeggs 	int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
114a04d0423SBen Skeggs 	if (ret) {
115a04d0423SBen Skeggs 		/*XXX: warn? */
116a04d0423SBen Skeggs 		return 0;
117a04d0423SBen Skeggs 	}
118a04d0423SBen Skeggs 	return args.rd.data;
119a04d0423SBen Skeggs }
120a04d0423SBen Skeggs 
121a04d0423SBen Skeggs void
122a04d0423SBen Skeggs nvif_object_wr(struct nvif_object *object, int size, u64 addr, u32 data)
123a04d0423SBen Skeggs {
124a04d0423SBen Skeggs 	struct {
125a04d0423SBen Skeggs 		struct nvif_ioctl_v0 ioctl;
126a04d0423SBen Skeggs 		struct nvif_ioctl_wr_v0 wr;
127a04d0423SBen Skeggs 	} args = {
128a04d0423SBen Skeggs 		.ioctl.type = NVIF_IOCTL_V0_WR,
129a04d0423SBen Skeggs 		.wr.size = size,
130a04d0423SBen Skeggs 		.wr.addr = addr,
131a04d0423SBen Skeggs 		.wr.data = data,
132a04d0423SBen Skeggs 	};
133a04d0423SBen Skeggs 	int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
134a04d0423SBen Skeggs 	if (ret) {
135a04d0423SBen Skeggs 		/*XXX: warn? */
136a04d0423SBen Skeggs 	}
137a04d0423SBen Skeggs }
138a04d0423SBen Skeggs 
139a04d0423SBen Skeggs int
140a04d0423SBen Skeggs nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size)
141a04d0423SBen Skeggs {
142a04d0423SBen Skeggs 	struct {
143a04d0423SBen Skeggs 		struct nvif_ioctl_v0 ioctl;
144a04d0423SBen Skeggs 		struct nvif_ioctl_mthd_v0 mthd;
145a04d0423SBen Skeggs 	} *args;
146a04d0423SBen Skeggs 	u8 stack[128];
147a04d0423SBen Skeggs 	int ret;
148a04d0423SBen Skeggs 
149a04d0423SBen Skeggs 	if (sizeof(*args) + size > sizeof(stack)) {
150a04d0423SBen Skeggs 		if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL)))
151a04d0423SBen Skeggs 			return -ENOMEM;
152a04d0423SBen Skeggs 	} else {
153a04d0423SBen Skeggs 		args = (void *)stack;
154a04d0423SBen Skeggs 	}
155a04d0423SBen Skeggs 	args->ioctl.version = 0;
156a04d0423SBen Skeggs 	args->ioctl.type = NVIF_IOCTL_V0_MTHD;
157a04d0423SBen Skeggs 	args->mthd.version = 0;
158a04d0423SBen Skeggs 	args->mthd.method = mthd;
159a04d0423SBen Skeggs 
160a04d0423SBen Skeggs 	memcpy(args->mthd.data, data, size);
161a04d0423SBen Skeggs 	ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL);
162a04d0423SBen Skeggs 	memcpy(data, args->mthd.data, size);
163a04d0423SBen Skeggs 	if (args != (void *)stack)
164a04d0423SBen Skeggs 		kfree(args);
165a04d0423SBen Skeggs 	return ret;
166a04d0423SBen Skeggs }
167a04d0423SBen Skeggs 
168a04d0423SBen Skeggs void
169a04d0423SBen Skeggs nvif_object_unmap(struct nvif_object *object)
170a04d0423SBen Skeggs {
171a04d0423SBen Skeggs 	if (object->map.size) {
172a01ca78cSBen Skeggs 		struct nvif_client *client = object->client;
173a04d0423SBen Skeggs 		struct {
174a04d0423SBen Skeggs 			struct nvif_ioctl_v0 ioctl;
175a04d0423SBen Skeggs 			struct nvif_ioctl_unmap unmap;
176a04d0423SBen Skeggs 		} args = {
177a04d0423SBen Skeggs 			.ioctl.type = NVIF_IOCTL_V0_UNMAP,
178a04d0423SBen Skeggs 		};
179a04d0423SBen Skeggs 
180a04d0423SBen Skeggs 		if (object->map.ptr) {
181a04d0423SBen Skeggs 			client->driver->unmap(client, object->map.ptr,
182a04d0423SBen Skeggs 						      object->map.size);
183a04d0423SBen Skeggs 			object->map.ptr = NULL;
184a04d0423SBen Skeggs 		}
185a04d0423SBen Skeggs 
186a04d0423SBen Skeggs 		nvif_object_ioctl(object, &args, sizeof(args), NULL);
187a04d0423SBen Skeggs 		object->map.size = 0;
188a04d0423SBen Skeggs 	}
189a04d0423SBen Skeggs }
190a04d0423SBen Skeggs 
191a04d0423SBen Skeggs int
192a04d0423SBen Skeggs nvif_object_map(struct nvif_object *object)
193a04d0423SBen Skeggs {
194a01ca78cSBen Skeggs 	struct nvif_client *client = object->client;
195a04d0423SBen Skeggs 	struct {
196a04d0423SBen Skeggs 		struct nvif_ioctl_v0 ioctl;
197a04d0423SBen Skeggs 		struct nvif_ioctl_map_v0 map;
198a04d0423SBen Skeggs 	} args = {
199a04d0423SBen Skeggs 		.ioctl.type = NVIF_IOCTL_V0_MAP,
200a04d0423SBen Skeggs 	};
201a04d0423SBen Skeggs 	int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
202a04d0423SBen Skeggs 	if (ret == 0) {
203a04d0423SBen Skeggs 		object->map.size = args.map.length;
204a04d0423SBen Skeggs 		object->map.ptr = client->driver->map(client, args.map.handle,
205a04d0423SBen Skeggs 						      object->map.size);
206a04d0423SBen Skeggs 		if (ret = -ENOMEM, object->map.ptr)
207a04d0423SBen Skeggs 			return 0;
208a04d0423SBen Skeggs 		nvif_object_unmap(object);
209a04d0423SBen Skeggs 	}
210a04d0423SBen Skeggs 	return ret;
211a04d0423SBen Skeggs }
212a04d0423SBen Skeggs 
213a04d0423SBen Skeggs void
214a04d0423SBen Skeggs nvif_object_fini(struct nvif_object *object)
215a04d0423SBen Skeggs {
216a04d0423SBen Skeggs 	struct {
217a04d0423SBen Skeggs 		struct nvif_ioctl_v0 ioctl;
218a04d0423SBen Skeggs 		struct nvif_ioctl_del del;
219a04d0423SBen Skeggs 	} args = {
220a04d0423SBen Skeggs 		.ioctl.type = NVIF_IOCTL_V0_DEL,
221a04d0423SBen Skeggs 	};
222a04d0423SBen Skeggs 
223a01ca78cSBen Skeggs 	if (!object->client)
224a01ca78cSBen Skeggs 		return;
225a01ca78cSBen Skeggs 
226a04d0423SBen Skeggs 	nvif_object_unmap(object);
227a04d0423SBen Skeggs 	nvif_object_ioctl(object, &args, sizeof(args), NULL);
228a01ca78cSBen Skeggs 	object->client = NULL;
229a04d0423SBen Skeggs }
230a04d0423SBen Skeggs 
231a04d0423SBen Skeggs int
232315a8b2eSBen Skeggs nvif_object_init(struct nvif_object *parent, u32 handle, s32 oclass,
233a01ca78cSBen Skeggs 		 void *data, u32 size, struct nvif_object *object)
234a04d0423SBen Skeggs {
235a01ca78cSBen Skeggs 	struct {
236a01ca78cSBen Skeggs 		struct nvif_ioctl_v0 ioctl;
237a01ca78cSBen Skeggs 		struct nvif_ioctl_new_v0 new;
238a01ca78cSBen Skeggs 	} *args;
239a04d0423SBen Skeggs 	int ret = 0;
240a04d0423SBen Skeggs 
241a01ca78cSBen Skeggs 	object->client = NULL;
242a04d0423SBen Skeggs 	object->handle = handle;
243a04d0423SBen Skeggs 	object->oclass = oclass;
244a04d0423SBen Skeggs 	object->map.ptr = NULL;
245a04d0423SBen Skeggs 	object->map.size = 0;
246a04d0423SBen Skeggs 
247bf81df9bSBen Skeggs 	if (parent) {
248a01ca78cSBen Skeggs 		if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) {
249a04d0423SBen Skeggs 			nvif_object_fini(object);
250a04d0423SBen Skeggs 			return -ENOMEM;
251a04d0423SBen Skeggs 		}
252a04d0423SBen Skeggs 
253a01ca78cSBen Skeggs 		args->ioctl.version = 0;
254a01ca78cSBen Skeggs 		args->ioctl.type = NVIF_IOCTL_V0_NEW;
255a01ca78cSBen Skeggs 		args->new.version = 0;
256a01ca78cSBen Skeggs 		args->new.route = parent->client->route;
257bf81df9bSBen Skeggs 		args->new.token = nvif_handle(object);
258bf81df9bSBen Skeggs 		args->new.object = nvif_handle(object);
259a01ca78cSBen Skeggs 		args->new.handle = handle;
260a01ca78cSBen Skeggs 		args->new.oclass = oclass;
261a04d0423SBen Skeggs 
262a01ca78cSBen Skeggs 		memcpy(args->new.data, data, size);
263a01ca78cSBen Skeggs 		ret = nvif_object_ioctl(parent, args, sizeof(*args) + size,
264a01ca78cSBen Skeggs 					&object->priv);
265a01ca78cSBen Skeggs 		memcpy(data, args->new.data, size);
266a01ca78cSBen Skeggs 		kfree(args);
267a01ca78cSBen Skeggs 		if (ret == 0)
268a01ca78cSBen Skeggs 			object->client = parent->client;
269a04d0423SBen Skeggs 	}
270a04d0423SBen Skeggs 
271a04d0423SBen Skeggs 	if (ret)
272a04d0423SBen Skeggs 		nvif_object_fini(object);
273a04d0423SBen Skeggs 	return ret;
274a04d0423SBen Skeggs }
275