user.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. #define nvkm_udevice(p) container_of((p), struct nvkm_udevice, object)
  25. #include "priv.h"
  26. #include "ctrl.h"
  27. #include <core/client.h>
  28. #include <subdev/fb.h>
  29. #include <subdev/instmem.h>
  30. #include <subdev/timer.h>
  31. #include <nvif/class.h>
  32. #include <nvif/cl0080.h>
  33. #include <nvif/unpack.h>
  34. struct nvkm_udevice {
  35. struct nvkm_object object;
  36. struct nvkm_device *device;
  37. };
  38. static int
  39. nvkm_udevice_info(struct nvkm_udevice *udev, void *data, u32 size)
  40. {
  41. struct nvkm_object *object = &udev->object;
  42. struct nvkm_device *device = udev->device;
  43. struct nvkm_fb *fb = device->fb;
  44. struct nvkm_instmem *imem = device->imem;
  45. union {
  46. struct nv_device_info_v0 v0;
  47. } *args = data;
  48. int ret = -ENOSYS;
  49. nvif_ioctl(object, "device info size %d\n", size);
  50. if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) {
  51. nvif_ioctl(object, "device info vers %d\n", args->v0.version);
  52. } else
  53. return ret;
  54. switch (device->chipset) {
  55. case 0x01a:
  56. case 0x01f:
  57. case 0x04c:
  58. case 0x04e:
  59. case 0x063:
  60. case 0x067:
  61. case 0x068:
  62. case 0x0aa:
  63. case 0x0ac:
  64. case 0x0af:
  65. args->v0.platform = NV_DEVICE_INFO_V0_IGP;
  66. break;
  67. default:
  68. switch (device->type) {
  69. case NVKM_DEVICE_PCI:
  70. args->v0.platform = NV_DEVICE_INFO_V0_PCI;
  71. break;
  72. case NVKM_DEVICE_AGP:
  73. args->v0.platform = NV_DEVICE_INFO_V0_AGP;
  74. break;
  75. case NVKM_DEVICE_PCIE:
  76. args->v0.platform = NV_DEVICE_INFO_V0_PCIE;
  77. break;
  78. case NVKM_DEVICE_TEGRA:
  79. args->v0.platform = NV_DEVICE_INFO_V0_SOC;
  80. break;
  81. default:
  82. WARN_ON(1);
  83. break;
  84. }
  85. break;
  86. }
  87. switch (device->card_type) {
  88. case NV_04: args->v0.family = NV_DEVICE_INFO_V0_TNT; break;
  89. case NV_10:
  90. case NV_11: args->v0.family = NV_DEVICE_INFO_V0_CELSIUS; break;
  91. case NV_20: args->v0.family = NV_DEVICE_INFO_V0_KELVIN; break;
  92. case NV_30: args->v0.family = NV_DEVICE_INFO_V0_RANKINE; break;
  93. case NV_40: args->v0.family = NV_DEVICE_INFO_V0_CURIE; break;
  94. case NV_50: args->v0.family = NV_DEVICE_INFO_V0_TESLA; break;
  95. case NV_C0: args->v0.family = NV_DEVICE_INFO_V0_FERMI; break;
  96. case NV_E0: args->v0.family = NV_DEVICE_INFO_V0_KEPLER; break;
  97. case GM100: args->v0.family = NV_DEVICE_INFO_V0_MAXWELL; break;
  98. case GP100: args->v0.family = NV_DEVICE_INFO_V0_PASCAL; break;
  99. default:
  100. args->v0.family = 0;
  101. break;
  102. }
  103. args->v0.chipset = device->chipset;
  104. args->v0.revision = device->chiprev;
  105. if (fb && fb->ram)
  106. args->v0.ram_size = args->v0.ram_user = fb->ram->size;
  107. else
  108. args->v0.ram_size = args->v0.ram_user = 0;
  109. if (imem && args->v0.ram_size > 0)
  110. args->v0.ram_user = args->v0.ram_user - imem->reserved;
  111. strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip));
  112. strncpy(args->v0.name, device->name, sizeof(args->v0.name));
  113. return 0;
  114. }
  115. static int
  116. nvkm_udevice_time(struct nvkm_udevice *udev, void *data, u32 size)
  117. {
  118. struct nvkm_object *object = &udev->object;
  119. struct nvkm_device *device = udev->device;
  120. union {
  121. struct nv_device_time_v0 v0;
  122. } *args = data;
  123. int ret = -ENOSYS;
  124. nvif_ioctl(object, "device time size %d\n", size);
  125. if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) {
  126. nvif_ioctl(object, "device time vers %d\n", args->v0.version);
  127. args->v0.time = nvkm_timer_read(device->timer);
  128. }
  129. return ret;
  130. }
  131. static int
  132. nvkm_udevice_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
  133. {
  134. struct nvkm_udevice *udev = nvkm_udevice(object);
  135. nvif_ioctl(object, "device mthd %08x\n", mthd);
  136. switch (mthd) {
  137. case NV_DEVICE_V0_INFO:
  138. return nvkm_udevice_info(udev, data, size);
  139. case NV_DEVICE_V0_TIME:
  140. return nvkm_udevice_time(udev, data, size);
  141. default:
  142. break;
  143. }
  144. return -EINVAL;
  145. }
  146. static int
  147. nvkm_udevice_rd08(struct nvkm_object *object, u64 addr, u8 *data)
  148. {
  149. struct nvkm_udevice *udev = nvkm_udevice(object);
  150. *data = nvkm_rd08(udev->device, addr);
  151. return 0;
  152. }
  153. static int
  154. nvkm_udevice_rd16(struct nvkm_object *object, u64 addr, u16 *data)
  155. {
  156. struct nvkm_udevice *udev = nvkm_udevice(object);
  157. *data = nvkm_rd16(udev->device, addr);
  158. return 0;
  159. }
  160. static int
  161. nvkm_udevice_rd32(struct nvkm_object *object, u64 addr, u32 *data)
  162. {
  163. struct nvkm_udevice *udev = nvkm_udevice(object);
  164. *data = nvkm_rd32(udev->device, addr);
  165. return 0;
  166. }
  167. static int
  168. nvkm_udevice_wr08(struct nvkm_object *object, u64 addr, u8 data)
  169. {
  170. struct nvkm_udevice *udev = nvkm_udevice(object);
  171. nvkm_wr08(udev->device, addr, data);
  172. return 0;
  173. }
  174. static int
  175. nvkm_udevice_wr16(struct nvkm_object *object, u64 addr, u16 data)
  176. {
  177. struct nvkm_udevice *udev = nvkm_udevice(object);
  178. nvkm_wr16(udev->device, addr, data);
  179. return 0;
  180. }
  181. static int
  182. nvkm_udevice_wr32(struct nvkm_object *object, u64 addr, u32 data)
  183. {
  184. struct nvkm_udevice *udev = nvkm_udevice(object);
  185. nvkm_wr32(udev->device, addr, data);
  186. return 0;
  187. }
  188. static int
  189. nvkm_udevice_map(struct nvkm_object *object, u64 *addr, u32 *size)
  190. {
  191. struct nvkm_udevice *udev = nvkm_udevice(object);
  192. struct nvkm_device *device = udev->device;
  193. *addr = device->func->resource_addr(device, 0);
  194. *size = device->func->resource_size(device, 0);
  195. return 0;
  196. }
  197. static int
  198. nvkm_udevice_fini(struct nvkm_object *object, bool suspend)
  199. {
  200. struct nvkm_udevice *udev = nvkm_udevice(object);
  201. struct nvkm_device *device = udev->device;
  202. int ret = 0;
  203. mutex_lock(&device->mutex);
  204. if (!--device->refcount) {
  205. ret = nvkm_device_fini(device, suspend);
  206. if (ret && suspend) {
  207. device->refcount++;
  208. goto done;
  209. }
  210. }
  211. done:
  212. mutex_unlock(&device->mutex);
  213. return ret;
  214. }
  215. static int
  216. nvkm_udevice_init(struct nvkm_object *object)
  217. {
  218. struct nvkm_udevice *udev = nvkm_udevice(object);
  219. struct nvkm_device *device = udev->device;
  220. int ret = 0;
  221. mutex_lock(&device->mutex);
  222. if (!device->refcount++) {
  223. ret = nvkm_device_init(device);
  224. if (ret) {
  225. device->refcount--;
  226. goto done;
  227. }
  228. }
  229. done:
  230. mutex_unlock(&device->mutex);
  231. return ret;
  232. }
  233. static int
  234. nvkm_udevice_child_new(const struct nvkm_oclass *oclass,
  235. void *data, u32 size, struct nvkm_object **pobject)
  236. {
  237. struct nvkm_udevice *udev = nvkm_udevice(oclass->parent);
  238. const struct nvkm_device_oclass *sclass = oclass->priv;
  239. return sclass->ctor(udev->device, oclass, data, size, pobject);
  240. }
  241. static int
  242. nvkm_udevice_child_get(struct nvkm_object *object, int index,
  243. struct nvkm_oclass *oclass)
  244. {
  245. struct nvkm_udevice *udev = nvkm_udevice(object);
  246. struct nvkm_device *device = udev->device;
  247. struct nvkm_engine *engine;
  248. u64 mask = (1ULL << NVKM_ENGINE_DMAOBJ) |
  249. (1ULL << NVKM_ENGINE_FIFO) |
  250. (1ULL << NVKM_ENGINE_DISP) |
  251. (1ULL << NVKM_ENGINE_PM);
  252. const struct nvkm_device_oclass *sclass = NULL;
  253. int i;
  254. for (; i = __ffs64(mask), mask && !sclass; mask &= ~(1ULL << i)) {
  255. if (!(engine = nvkm_device_engine(device, i)) ||
  256. !(engine->func->base.sclass))
  257. continue;
  258. oclass->engine = engine;
  259. index -= engine->func->base.sclass(oclass, index, &sclass);
  260. }
  261. if (!sclass) {
  262. switch (index) {
  263. case 0: sclass = &nvkm_control_oclass; break;
  264. default:
  265. return -EINVAL;
  266. }
  267. oclass->base = sclass->base;
  268. }
  269. oclass->ctor = nvkm_udevice_child_new;
  270. oclass->priv = sclass;
  271. return 0;
  272. }
  273. static const struct nvkm_object_func
  274. nvkm_udevice_super = {
  275. .init = nvkm_udevice_init,
  276. .fini = nvkm_udevice_fini,
  277. .mthd = nvkm_udevice_mthd,
  278. .map = nvkm_udevice_map,
  279. .rd08 = nvkm_udevice_rd08,
  280. .rd16 = nvkm_udevice_rd16,
  281. .rd32 = nvkm_udevice_rd32,
  282. .wr08 = nvkm_udevice_wr08,
  283. .wr16 = nvkm_udevice_wr16,
  284. .wr32 = nvkm_udevice_wr32,
  285. .sclass = nvkm_udevice_child_get,
  286. };
  287. static const struct nvkm_object_func
  288. nvkm_udevice = {
  289. .init = nvkm_udevice_init,
  290. .fini = nvkm_udevice_fini,
  291. .mthd = nvkm_udevice_mthd,
  292. .sclass = nvkm_udevice_child_get,
  293. };
  294. static int
  295. nvkm_udevice_new(const struct nvkm_oclass *oclass, void *data, u32 size,
  296. struct nvkm_object **pobject)
  297. {
  298. union {
  299. struct nv_device_v0 v0;
  300. } *args = data;
  301. struct nvkm_client *client = oclass->client;
  302. struct nvkm_object *parent = &client->object;
  303. const struct nvkm_object_func *func;
  304. struct nvkm_udevice *udev;
  305. int ret = -ENOSYS;
  306. nvif_ioctl(parent, "create device size %d\n", size);
  307. if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) {
  308. nvif_ioctl(parent, "create device v%d device %016llx\n",
  309. args->v0.version, args->v0.device);
  310. } else
  311. return ret;
  312. /* give priviledged clients register access */
  313. if (client->super)
  314. func = &nvkm_udevice_super;
  315. else
  316. func = &nvkm_udevice;
  317. if (!(udev = kzalloc(sizeof(*udev), GFP_KERNEL)))
  318. return -ENOMEM;
  319. nvkm_object_ctor(func, oclass, &udev->object);
  320. *pobject = &udev->object;
  321. /* find the device that matches what the client requested */
  322. if (args->v0.device != ~0)
  323. udev->device = nvkm_device_find(args->v0.device);
  324. else
  325. udev->device = nvkm_device_find(client->device);
  326. if (!udev->device)
  327. return -ENODEV;
  328. return 0;
  329. }
  330. const struct nvkm_sclass
  331. nvkm_udevice_sclass = {
  332. .oclass = NV_DEVICE,
  333. .minver = 0,
  334. .maxver = 0,
  335. .ctor = nvkm_udevice_new,
  336. };