client.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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/notify.h>
  27. #include <core/option.h>
  28. #include <nvif/class.h>
  29. #include <nvif/event.h>
  30. #include <nvif/if0000.h>
  31. #include <nvif/unpack.h>
  32. static int
  33. nvkm_uclient_new(const struct nvkm_oclass *oclass, void *argv, u32 argc,
  34. struct nvkm_object **pobject)
  35. {
  36. union {
  37. struct nvif_client_v0 v0;
  38. } *args = argv;
  39. struct nvkm_client *client;
  40. int ret = -ENOSYS;
  41. if (!(ret = nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, false))){
  42. args->v0.name[sizeof(args->v0.name) - 1] = 0;
  43. ret = nvkm_client_new(args->v0.name, args->v0.device, NULL,
  44. NULL, oclass->client->ntfy, &client);
  45. if (ret)
  46. return ret;
  47. } else
  48. return ret;
  49. client->object.client = oclass->client;
  50. client->object.handle = oclass->handle;
  51. client->object.route = oclass->route;
  52. client->object.token = oclass->token;
  53. client->object.object = oclass->object;
  54. client->debug = oclass->client->debug;
  55. *pobject = &client->object;
  56. return 0;
  57. }
  58. const struct nvkm_sclass
  59. nvkm_uclient_sclass = {
  60. .oclass = NVIF_CLASS_CLIENT,
  61. .minver = 0,
  62. .maxver = 0,
  63. .ctor = nvkm_uclient_new,
  64. };
  65. struct nvkm_client_notify {
  66. struct nvkm_client *client;
  67. struct nvkm_notify n;
  68. u8 version;
  69. u8 size;
  70. union {
  71. struct nvif_notify_rep_v0 v0;
  72. } rep;
  73. };
  74. static int
  75. nvkm_client_notify(struct nvkm_notify *n)
  76. {
  77. struct nvkm_client_notify *notify = container_of(n, typeof(*notify), n);
  78. struct nvkm_client *client = notify->client;
  79. return client->ntfy(&notify->rep, notify->size, n->data, n->size);
  80. }
  81. int
  82. nvkm_client_notify_put(struct nvkm_client *client, int index)
  83. {
  84. if (index < ARRAY_SIZE(client->notify)) {
  85. if (client->notify[index]) {
  86. nvkm_notify_put(&client->notify[index]->n);
  87. return 0;
  88. }
  89. }
  90. return -ENOENT;
  91. }
  92. int
  93. nvkm_client_notify_get(struct nvkm_client *client, int index)
  94. {
  95. if (index < ARRAY_SIZE(client->notify)) {
  96. if (client->notify[index]) {
  97. nvkm_notify_get(&client->notify[index]->n);
  98. return 0;
  99. }
  100. }
  101. return -ENOENT;
  102. }
  103. int
  104. nvkm_client_notify_del(struct nvkm_client *client, int index)
  105. {
  106. if (index < ARRAY_SIZE(client->notify)) {
  107. if (client->notify[index]) {
  108. nvkm_notify_fini(&client->notify[index]->n);
  109. kfree(client->notify[index]);
  110. client->notify[index] = NULL;
  111. return 0;
  112. }
  113. }
  114. return -ENOENT;
  115. }
  116. int
  117. nvkm_client_notify_new(struct nvkm_object *object,
  118. struct nvkm_event *event, void *data, u32 size)
  119. {
  120. struct nvkm_client *client = object->client;
  121. struct nvkm_client_notify *notify;
  122. union {
  123. struct nvif_notify_req_v0 v0;
  124. } *req = data;
  125. u8 index, reply;
  126. int ret = -ENOSYS;
  127. for (index = 0; index < ARRAY_SIZE(client->notify); index++) {
  128. if (!client->notify[index])
  129. break;
  130. }
  131. if (index == ARRAY_SIZE(client->notify))
  132. return -ENOSPC;
  133. notify = kzalloc(sizeof(*notify), GFP_KERNEL);
  134. if (!notify)
  135. return -ENOMEM;
  136. nvif_ioctl(object, "notify new size %d\n", size);
  137. if (!(ret = nvif_unpack(ret, &data, &size, req->v0, 0, 0, true))) {
  138. nvif_ioctl(object, "notify new vers %d reply %d route %02x "
  139. "token %llx\n", req->v0.version,
  140. req->v0.reply, req->v0.route, req->v0.token);
  141. notify->version = req->v0.version;
  142. notify->size = sizeof(notify->rep.v0);
  143. notify->rep.v0.version = req->v0.version;
  144. notify->rep.v0.route = req->v0.route;
  145. notify->rep.v0.token = req->v0.token;
  146. reply = req->v0.reply;
  147. }
  148. if (ret == 0) {
  149. ret = nvkm_notify_init(object, event, nvkm_client_notify,
  150. false, data, size, reply, &notify->n);
  151. if (ret == 0) {
  152. client->notify[index] = notify;
  153. notify->client = client;
  154. return index;
  155. }
  156. }
  157. kfree(notify);
  158. return ret;
  159. }
  160. static const struct nvkm_object_func nvkm_client;
  161. struct nvkm_client *
  162. nvkm_client_search(struct nvkm_client *client, u64 handle)
  163. {
  164. struct nvkm_object *object;
  165. object = nvkm_object_search(client, handle, &nvkm_client);
  166. if (IS_ERR(object))
  167. return (void *)object;
  168. return nvkm_client(object);
  169. }
  170. static int
  171. nvkm_client_mthd_devlist(struct nvkm_client *client, void *data, u32 size)
  172. {
  173. union {
  174. struct nvif_client_devlist_v0 v0;
  175. } *args = data;
  176. int ret = -ENOSYS;
  177. nvif_ioctl(&client->object, "client devlist size %d\n", size);
  178. if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
  179. nvif_ioctl(&client->object, "client devlist vers %d count %d\n",
  180. args->v0.version, args->v0.count);
  181. if (size == sizeof(args->v0.device[0]) * args->v0.count) {
  182. ret = nvkm_device_list(args->v0.device, args->v0.count);
  183. if (ret >= 0) {
  184. args->v0.count = ret;
  185. ret = 0;
  186. }
  187. } else {
  188. ret = -EINVAL;
  189. }
  190. }
  191. return ret;
  192. }
  193. static int
  194. nvkm_client_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
  195. {
  196. struct nvkm_client *client = nvkm_client(object);
  197. switch (mthd) {
  198. case NVIF_CLIENT_V0_DEVLIST:
  199. return nvkm_client_mthd_devlist(client, data, size);
  200. default:
  201. break;
  202. }
  203. return -EINVAL;
  204. }
  205. static int
  206. nvkm_client_child_new(const struct nvkm_oclass *oclass,
  207. void *data, u32 size, struct nvkm_object **pobject)
  208. {
  209. return oclass->base.ctor(oclass, data, size, pobject);
  210. }
  211. static int
  212. nvkm_client_child_get(struct nvkm_object *object, int index,
  213. struct nvkm_oclass *oclass)
  214. {
  215. const struct nvkm_sclass *sclass;
  216. switch (index) {
  217. case 0: sclass = &nvkm_uclient_sclass; break;
  218. case 1: sclass = &nvkm_udevice_sclass; break;
  219. default:
  220. return -EINVAL;
  221. }
  222. oclass->ctor = nvkm_client_child_new;
  223. oclass->base = *sclass;
  224. return 0;
  225. }
  226. static int
  227. nvkm_client_fini(struct nvkm_object *object, bool suspend)
  228. {
  229. struct nvkm_client *client = nvkm_client(object);
  230. const char *name[2] = { "fini", "suspend" };
  231. int i;
  232. nvif_debug(object, "%s notify\n", name[suspend]);
  233. for (i = 0; i < ARRAY_SIZE(client->notify); i++)
  234. nvkm_client_notify_put(client, i);
  235. return 0;
  236. }
  237. static void *
  238. nvkm_client_dtor(struct nvkm_object *object)
  239. {
  240. struct nvkm_client *client = nvkm_client(object);
  241. int i;
  242. for (i = 0; i < ARRAY_SIZE(client->notify); i++)
  243. nvkm_client_notify_del(client, i);
  244. return client;
  245. }
  246. static const struct nvkm_object_func
  247. nvkm_client = {
  248. .dtor = nvkm_client_dtor,
  249. .fini = nvkm_client_fini,
  250. .mthd = nvkm_client_mthd,
  251. .sclass = nvkm_client_child_get,
  252. };
  253. int
  254. nvkm_client_new(const char *name, u64 device, const char *cfg,
  255. const char *dbg,
  256. int (*ntfy)(const void *, u32, const void *, u32),
  257. struct nvkm_client **pclient)
  258. {
  259. struct nvkm_oclass oclass = { .base = nvkm_uclient_sclass };
  260. struct nvkm_client *client;
  261. if (!(client = *pclient = kzalloc(sizeof(*client), GFP_KERNEL)))
  262. return -ENOMEM;
  263. oclass.client = client;
  264. nvkm_object_ctor(&nvkm_client, &oclass, &client->object);
  265. snprintf(client->name, sizeof(client->name), "%s", name);
  266. client->device = device;
  267. client->debug = nvkm_dbgopt(dbg, "CLIENT");
  268. client->objroot = RB_ROOT;
  269. client->ntfy = ntfy;
  270. INIT_LIST_HEAD(&client->umem);
  271. spin_lock_init(&client->lock);
  272. return 0;
  273. }