object.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 = kzalloc(sizeof(**psclass) * args->sclass.count, 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(struct nvif_object *object)
  154. {
  155. if (object->map.size) {
  156. struct nvif_client *client = object->client;
  157. struct {
  158. struct nvif_ioctl_v0 ioctl;
  159. struct nvif_ioctl_unmap unmap;
  160. } args = {
  161. .ioctl.type = NVIF_IOCTL_V0_UNMAP,
  162. };
  163. if (object->map.ptr) {
  164. client->driver->unmap(client, object->map.ptr,
  165. object->map.size);
  166. object->map.ptr = NULL;
  167. }
  168. nvif_object_ioctl(object, &args, sizeof(args), NULL);
  169. object->map.size = 0;
  170. }
  171. }
  172. int
  173. nvif_object_map(struct nvif_object *object)
  174. {
  175. struct nvif_client *client = object->client;
  176. struct {
  177. struct nvif_ioctl_v0 ioctl;
  178. struct nvif_ioctl_map_v0 map;
  179. } args = {
  180. .ioctl.type = NVIF_IOCTL_V0_MAP,
  181. };
  182. int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
  183. if (ret == 0) {
  184. object->map.size = args.map.length;
  185. object->map.ptr = client->driver->map(client, args.map.handle,
  186. object->map.size);
  187. if (ret = -ENOMEM, object->map.ptr)
  188. return 0;
  189. nvif_object_unmap(object);
  190. }
  191. return ret;
  192. }
  193. void
  194. nvif_object_fini(struct nvif_object *object)
  195. {
  196. struct {
  197. struct nvif_ioctl_v0 ioctl;
  198. struct nvif_ioctl_del del;
  199. } args = {
  200. .ioctl.type = NVIF_IOCTL_V0_DEL,
  201. };
  202. if (!object->client)
  203. return;
  204. nvif_object_unmap(object);
  205. nvif_object_ioctl(object, &args, sizeof(args), NULL);
  206. object->client = NULL;
  207. }
  208. int
  209. nvif_object_init(struct nvif_object *parent, u32 handle, s32 oclass,
  210. void *data, u32 size, struct nvif_object *object)
  211. {
  212. struct {
  213. struct nvif_ioctl_v0 ioctl;
  214. struct nvif_ioctl_new_v0 new;
  215. } *args;
  216. int ret = 0;
  217. object->client = NULL;
  218. object->handle = handle;
  219. object->oclass = oclass;
  220. object->map.ptr = NULL;
  221. object->map.size = 0;
  222. if (parent) {
  223. if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) {
  224. nvif_object_fini(object);
  225. return -ENOMEM;
  226. }
  227. args->ioctl.version = 0;
  228. args->ioctl.type = NVIF_IOCTL_V0_NEW;
  229. args->new.version = 0;
  230. args->new.route = parent->client->route;
  231. args->new.token = nvif_handle(object);
  232. args->new.object = nvif_handle(object);
  233. args->new.handle = handle;
  234. args->new.oclass = oclass;
  235. memcpy(args->new.data, data, size);
  236. ret = nvif_object_ioctl(parent, args, sizeof(*args) + size,
  237. &object->priv);
  238. memcpy(data, args->new.data, size);
  239. kfree(args);
  240. if (ret == 0)
  241. object->client = parent->client;
  242. }
  243. if (ret)
  244. nvif_object_fini(object);
  245. return ret;
  246. }