object.c 7.3 KB

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