xref: /linux/drivers/gpu/drm/nouveau/nvkm/core/client.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
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 <core/client.h>
25 #include <core/device.h>
26 #include <core/option.h>
27 
28 #include <nvif/class.h>
29 #include <nvif/event.h>
30 #include <nvif/if0000.h>
31 #include <nvif/unpack.h>
32 
33 static int
nvkm_uclient_new(const struct nvkm_oclass * oclass,void * argv,u32 argc,struct nvkm_object ** pobject)34 nvkm_uclient_new(const struct nvkm_oclass *oclass, void *argv, u32 argc,
35 		 struct nvkm_object **pobject)
36 {
37 	union {
38 		struct nvif_client_v0 v0;
39 	} *args = argv;
40 	struct nvkm_client *client;
41 	int ret = -ENOSYS;
42 
43 	if (!(ret = nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, false))){
44 		args->v0.name[sizeof(args->v0.name) - 1] = 0;
45 		ret = nvkm_client_new(args->v0.name, oclass->client->device, NULL,
46 				      NULL, oclass->client->event, &client);
47 		if (ret)
48 			return ret;
49 	} else
50 		return ret;
51 
52 	client->object.client = oclass->client;
53 	client->object.handle = oclass->handle;
54 	client->object.object = oclass->object;
55 	client->debug = oclass->client->debug;
56 	*pobject = &client->object;
57 	return 0;
58 }
59 
60 static const struct nvkm_sclass
61 nvkm_uclient_sclass = {
62 	.oclass = NVIF_CLASS_CLIENT,
63 	.minver = 0,
64 	.maxver = 0,
65 	.ctor = nvkm_uclient_new,
66 };
67 
68 static int
nvkm_client_child_new(const struct nvkm_oclass * oclass,void * data,u32 size,struct nvkm_object ** pobject)69 nvkm_client_child_new(const struct nvkm_oclass *oclass,
70 		      void *data, u32 size, struct nvkm_object **pobject)
71 {
72 	return oclass->base.ctor(oclass, data, size, pobject);
73 }
74 
75 static int
nvkm_client_child_get(struct nvkm_object * object,int index,struct nvkm_oclass * oclass)76 nvkm_client_child_get(struct nvkm_object *object, int index,
77 		      struct nvkm_oclass *oclass)
78 {
79 	const struct nvkm_sclass *sclass;
80 
81 	switch (index) {
82 	case 0: sclass = &nvkm_uclient_sclass; break;
83 	case 1: sclass = &nvkm_udevice_sclass; break;
84 	default:
85 		return -EINVAL;
86 	}
87 
88 	oclass->ctor = nvkm_client_child_new;
89 	oclass->base = *sclass;
90 	return 0;
91 }
92 
93 static void *
nvkm_client_dtor(struct nvkm_object * object)94 nvkm_client_dtor(struct nvkm_object *object)
95 {
96 	return nvkm_client(object);
97 }
98 
99 static const struct nvkm_object_func
100 nvkm_client = {
101 	.dtor = nvkm_client_dtor,
102 	.sclass = nvkm_client_child_get,
103 };
104 
105 int
nvkm_client_new(const char * name,u64 device,const char * cfg,const char * dbg,int (* event)(u64,void *,u32),struct nvkm_client ** pclient)106 nvkm_client_new(const char *name, u64 device, const char *cfg, const char *dbg,
107 		int (*event)(u64, void *, u32), struct nvkm_client **pclient)
108 {
109 	struct nvkm_oclass oclass = { .base = nvkm_uclient_sclass };
110 	struct nvkm_client *client;
111 
112 	if (!(client = *pclient = kzalloc(sizeof(*client), GFP_KERNEL)))
113 		return -ENOMEM;
114 	oclass.client = client;
115 
116 	nvkm_object_ctor(&nvkm_client, &oclass, &client->object);
117 	snprintf(client->name, sizeof(client->name), "%s", name);
118 	client->device = device;
119 	client->debug = nvkm_dbgopt(dbg, "CLIENT");
120 	client->objroot = RB_ROOT;
121 	spin_lock_init(&client->obj_lock);
122 	client->event = event;
123 	INIT_LIST_HEAD(&client->umem);
124 	spin_lock_init(&client->lock);
125 	return 0;
126 }
127