object.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright 2014 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 <bskeggs@redhat.com>
  23. */
  24. #include "object.h"
  25. #include "client.h"
  26. #include "driver.h"
  27. #include "ioctl.h"
  28. int
  29. nvif_object_ioctl(struct nvif_object *object, void *data, u32 size, void **hack)
  30. {
  31. struct nvif_client *client = nvif_client(object);
  32. union {
  33. struct nvif_ioctl_v0 v0;
  34. } *args = data;
  35. if (size >= sizeof(*args) && args->v0.version == 0) {
  36. args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY;
  37. args->v0.path_nr = 0;
  38. while (args->v0.path_nr < ARRAY_SIZE(args->v0.path)) {
  39. args->v0.path[args->v0.path_nr++] = object->handle;
  40. if (object->parent == object)
  41. break;
  42. object = object->parent;
  43. }
  44. } else
  45. return -ENOSYS;
  46. return client->driver->ioctl(client->base.priv, client->super, data, size, hack);
  47. }
  48. int
  49. nvif_object_sclass(struct nvif_object *object, u32 *oclass, int count)
  50. {
  51. struct {
  52. struct nvif_ioctl_v0 ioctl;
  53. struct nvif_ioctl_sclass_v0 sclass;
  54. } *args;
  55. u32 size = count * sizeof(args->sclass.oclass[0]);
  56. int ret;
  57. if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL)))
  58. return -ENOMEM;
  59. args->ioctl.version = 0;
  60. args->ioctl.type = NVIF_IOCTL_V0_SCLASS;
  61. args->sclass.version = 0;
  62. args->sclass.count = count;
  63. memcpy(args->sclass.oclass, oclass, size);
  64. ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL);
  65. ret = ret ? ret : args->sclass.count;
  66. memcpy(oclass, args->sclass.oclass, size);
  67. kfree(args);
  68. return ret;
  69. }
  70. u32
  71. nvif_object_rd(struct nvif_object *object, int size, u64 addr)
  72. {
  73. struct {
  74. struct nvif_ioctl_v0 ioctl;
  75. struct nvif_ioctl_rd_v0 rd;
  76. } args = {
  77. .ioctl.type = NVIF_IOCTL_V0_RD,
  78. .rd.size = size,
  79. .rd.addr = addr,
  80. };
  81. int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
  82. if (ret) {
  83. /*XXX: warn? */
  84. return 0;
  85. }
  86. return args.rd.data;
  87. }
  88. void
  89. nvif_object_wr(struct nvif_object *object, int size, u64 addr, u32 data)
  90. {
  91. struct {
  92. struct nvif_ioctl_v0 ioctl;
  93. struct nvif_ioctl_wr_v0 wr;
  94. } args = {
  95. .ioctl.type = NVIF_IOCTL_V0_WR,
  96. .wr.size = size,
  97. .wr.addr = addr,
  98. .wr.data = data,
  99. };
  100. int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
  101. if (ret) {
  102. /*XXX: warn? */
  103. }
  104. }
  105. int
  106. nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size)
  107. {
  108. struct {
  109. struct nvif_ioctl_v0 ioctl;
  110. struct nvif_ioctl_mthd_v0 mthd;
  111. } *args;
  112. u8 stack[128];
  113. int ret;
  114. if (sizeof(*args) + size > sizeof(stack)) {
  115. if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL)))
  116. return -ENOMEM;
  117. } else {
  118. args = (void *)stack;
  119. }
  120. args->ioctl.version = 0;
  121. args->ioctl.type = NVIF_IOCTL_V0_MTHD;
  122. args->mthd.version = 0;
  123. args->mthd.method = mthd;
  124. memcpy(args->mthd.data, data, size);
  125. ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL);
  126. memcpy(data, args->mthd.data, size);
  127. if (args != (void *)stack)
  128. kfree(args);
  129. return ret;
  130. }
  131. void
  132. nvif_object_unmap(struct nvif_object *object)
  133. {
  134. if (object->map.size) {
  135. struct nvif_client *client = nvif_client(object);
  136. struct {
  137. struct nvif_ioctl_v0 ioctl;
  138. struct nvif_ioctl_unmap unmap;
  139. } args = {
  140. .ioctl.type = NVIF_IOCTL_V0_UNMAP,
  141. };
  142. if (object->map.ptr) {
  143. client->driver->unmap(client, object->map.ptr,
  144. object->map.size);
  145. object->map.ptr = NULL;
  146. }
  147. nvif_object_ioctl(object, &args, sizeof(args), NULL);
  148. object->map.size = 0;
  149. }
  150. }
  151. int
  152. nvif_object_map(struct nvif_object *object)
  153. {
  154. struct nvif_client *client = nvif_client(object);
  155. struct {
  156. struct nvif_ioctl_v0 ioctl;
  157. struct nvif_ioctl_map_v0 map;
  158. } args = {
  159. .ioctl.type = NVIF_IOCTL_V0_MAP,
  160. };
  161. int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
  162. if (ret == 0) {
  163. object->map.size = args.map.length;
  164. object->map.ptr = client->driver->map(client, args.map.handle,
  165. object->map.size);
  166. if (ret = -ENOMEM, object->map.ptr)
  167. return 0;
  168. nvif_object_unmap(object);
  169. }
  170. return ret;
  171. }
  172. struct ctor {
  173. struct nvif_ioctl_v0 ioctl;
  174. struct nvif_ioctl_new_v0 new;
  175. };
  176. void
  177. nvif_object_fini(struct nvif_object *object)
  178. {
  179. struct ctor *ctor = container_of(object->data, typeof(*ctor), new.data);
  180. if (object->parent) {
  181. struct {
  182. struct nvif_ioctl_v0 ioctl;
  183. struct nvif_ioctl_del del;
  184. } args = {
  185. .ioctl.type = NVIF_IOCTL_V0_DEL,
  186. };
  187. nvif_object_unmap(object);
  188. nvif_object_ioctl(object, &args, sizeof(args), NULL);
  189. if (object->data) {
  190. object->size = 0;
  191. object->data = NULL;
  192. kfree(ctor);
  193. }
  194. nvif_object_ref(NULL, &object->parent);
  195. }
  196. }
  197. int
  198. nvif_object_init(struct nvif_object *parent, void (*dtor)(struct nvif_object *),
  199. u32 handle, u32 oclass, void *data, u32 size,
  200. struct nvif_object *object)
  201. {
  202. struct ctor *ctor;
  203. int ret = 0;
  204. object->parent = NULL;
  205. object->object = object;
  206. nvif_object_ref(parent, &object->parent);
  207. kref_init(&object->refcount);
  208. object->handle = handle;
  209. object->oclass = oclass;
  210. object->data = NULL;
  211. object->size = 0;
  212. object->dtor = dtor;
  213. object->map.ptr = NULL;
  214. object->map.size = 0;
  215. if (object->parent) {
  216. if (!(ctor = kmalloc(sizeof(*ctor) + size, GFP_KERNEL))) {
  217. nvif_object_fini(object);
  218. return -ENOMEM;
  219. }
  220. object->data = ctor->new.data;
  221. object->size = size;
  222. memcpy(object->data, data, size);
  223. ctor->ioctl.version = 0;
  224. ctor->ioctl.type = NVIF_IOCTL_V0_NEW;
  225. ctor->new.version = 0;
  226. ctor->new.route = NVIF_IOCTL_V0_ROUTE_NVIF;
  227. ctor->new.token = (unsigned long)(void *)object;
  228. ctor->new.handle = handle;
  229. ctor->new.oclass = oclass;
  230. ret = nvif_object_ioctl(parent, ctor, sizeof(*ctor) +
  231. object->size, &object->priv);
  232. }
  233. if (ret)
  234. nvif_object_fini(object);
  235. return ret;
  236. }
  237. static void
  238. nvif_object_del(struct nvif_object *object)
  239. {
  240. nvif_object_fini(object);
  241. kfree(object);
  242. }
  243. int
  244. nvif_object_new(struct nvif_object *parent, u32 handle, u32 oclass,
  245. void *data, u32 size, struct nvif_object **pobject)
  246. {
  247. struct nvif_object *object = kzalloc(sizeof(*object), GFP_KERNEL);
  248. if (object) {
  249. int ret = nvif_object_init(parent, nvif_object_del, handle,
  250. oclass, data, size, object);
  251. if (ret) {
  252. kfree(object);
  253. object = NULL;
  254. }
  255. *pobject = object;
  256. return ret;
  257. }
  258. return -ENOMEM;
  259. }
  260. static void
  261. nvif_object_put(struct kref *kref)
  262. {
  263. struct nvif_object *object =
  264. container_of(kref, typeof(*object), refcount);
  265. object->dtor(object);
  266. }
  267. void
  268. nvif_object_ref(struct nvif_object *object, struct nvif_object **pobject)
  269. {
  270. if (object)
  271. kref_get(&object->refcount);
  272. if (*pobject)
  273. kref_put(&(*pobject)->refcount, nvif_object_put);
  274. *pobject = object;
  275. }