user.c 9.3 KB

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