1
0

abi16.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #include <stdlib.h>
  25. #include <stdint.h>
  26. #include <stddef.h>
  27. #include <errno.h>
  28. #include "private.h"
  29. #include "nvif/class.h"
  30. static int
  31. abi16_chan_nv04(struct nouveau_object *obj)
  32. {
  33. struct nouveau_drm *drm = nouveau_drm(obj);
  34. struct nv04_fifo *nv04 = obj->data;
  35. struct drm_nouveau_channel_alloc req = {
  36. .fb_ctxdma_handle = nv04->vram,
  37. .tt_ctxdma_handle = nv04->gart
  38. };
  39. int ret;
  40. ret = drmCommandWriteRead(drm->fd, DRM_NOUVEAU_CHANNEL_ALLOC,
  41. &req, sizeof(req));
  42. if (ret)
  43. return ret;
  44. nv04->base.channel = req.channel;
  45. nv04->base.pushbuf = req.pushbuf_domains;
  46. nv04->notify = req.notifier_handle;
  47. nv04->base.object->handle = req.channel;
  48. nv04->base.object->length = sizeof(*nv04);
  49. return 0;
  50. }
  51. static int
  52. abi16_chan_nvc0(struct nouveau_object *obj)
  53. {
  54. struct nouveau_drm *drm = nouveau_drm(obj);
  55. struct drm_nouveau_channel_alloc req = {};
  56. struct nvc0_fifo *nvc0 = obj->data;
  57. int ret;
  58. ret = drmCommandWriteRead(drm->fd, DRM_NOUVEAU_CHANNEL_ALLOC,
  59. &req, sizeof(req));
  60. if (ret)
  61. return ret;
  62. nvc0->base.channel = req.channel;
  63. nvc0->base.pushbuf = req.pushbuf_domains;
  64. nvc0->notify = req.notifier_handle;
  65. nvc0->base.object->handle = req.channel;
  66. nvc0->base.object->length = sizeof(*nvc0);
  67. return 0;
  68. }
  69. static int
  70. abi16_chan_nve0(struct nouveau_object *obj)
  71. {
  72. struct nouveau_drm *drm = nouveau_drm(obj);
  73. struct drm_nouveau_channel_alloc req = {};
  74. struct nve0_fifo *nve0 = obj->data;
  75. int ret;
  76. if (obj->length > offsetof(struct nve0_fifo, engine)) {
  77. req.fb_ctxdma_handle = 0xffffffff;
  78. req.tt_ctxdma_handle = nve0->engine;
  79. }
  80. ret = drmCommandWriteRead(drm->fd, DRM_NOUVEAU_CHANNEL_ALLOC,
  81. &req, sizeof(req));
  82. if (ret)
  83. return ret;
  84. nve0->base.channel = req.channel;
  85. nve0->base.pushbuf = req.pushbuf_domains;
  86. nve0->notify = req.notifier_handle;
  87. nve0->base.object->handle = req.channel;
  88. nve0->base.object->length = sizeof(*nve0);
  89. return 0;
  90. }
  91. static int
  92. abi16_engobj(struct nouveau_object *obj)
  93. {
  94. struct nouveau_drm *drm = nouveau_drm(obj);
  95. struct drm_nouveau_grobj_alloc req = {
  96. .channel = obj->parent->handle,
  97. .handle = obj->handle,
  98. .class = obj->oclass,
  99. };
  100. int ret;
  101. /* Older kernel versions did not have the concept of nouveau-
  102. * specific classes and abused some NVIDIA-assigned ones for
  103. * a SW class. The ABI16 layer has compatibility in place to
  104. * translate these older identifiers to the newer ones.
  105. *
  106. * Clients that have been updated to use NVIF are required to
  107. * use the newer class identifiers, which means that they'll
  108. * break if running on an older kernel.
  109. *
  110. * To handle this case, when using ABI16, we translate to the
  111. * older values which work on any kernel.
  112. */
  113. switch (req.class) {
  114. case NVIF_CLASS_SW_NV04 : req.class = 0x006e; break;
  115. case NVIF_CLASS_SW_NV10 : req.class = 0x016e; break;
  116. case NVIF_CLASS_SW_NV50 : req.class = 0x506e; break;
  117. case NVIF_CLASS_SW_GF100: req.class = 0x906e; break;
  118. default:
  119. break;
  120. }
  121. ret = drmCommandWrite(drm->fd, DRM_NOUVEAU_GROBJ_ALLOC,
  122. &req, sizeof(req));
  123. if (ret)
  124. return ret;
  125. obj->length = sizeof(struct nouveau_object *);
  126. return 0;
  127. }
  128. static int
  129. abi16_ntfy(struct nouveau_object *obj)
  130. {
  131. struct nouveau_drm *drm = nouveau_drm(obj);
  132. struct nv04_notify *ntfy = obj->data;
  133. struct drm_nouveau_notifierobj_alloc req = {
  134. .channel = obj->parent->handle,
  135. .handle = ntfy->object->handle,
  136. .size = ntfy->length,
  137. };
  138. int ret;
  139. ret = drmCommandWriteRead(drm->fd, DRM_NOUVEAU_NOTIFIEROBJ_ALLOC,
  140. &req, sizeof(req));
  141. if (ret)
  142. return ret;
  143. ntfy->offset = req.offset;
  144. ntfy->object->length = sizeof(*ntfy);
  145. return 0;
  146. }
  147. drm_private int
  148. abi16_sclass(struct nouveau_object *obj, struct nouveau_sclass **psclass)
  149. {
  150. struct nouveau_sclass *sclass;
  151. struct nouveau_device *dev;
  152. if (!(sclass = calloc(8, sizeof(*sclass))))
  153. return -ENOMEM;
  154. *psclass = sclass;
  155. switch (obj->oclass) {
  156. case NOUVEAU_FIFO_CHANNEL_CLASS:
  157. /* Older kernel versions were exposing the wrong video engine
  158. * classes on certain G98:GF100 boards. This has since been
  159. * corrected, but ABI16 has compatibility in place to avoid
  160. * breaking older userspace.
  161. *
  162. * Clients that have been updated to use NVIF are required to
  163. * use the correct classes, which means that they'll break if
  164. * running on an older kernel.
  165. *
  166. * To handle this issue, if using the older kernel interfaces,
  167. * we'll magic up a list containing the vdec classes that the
  168. * kernel will accept for these boards. Clients should make
  169. * use of this information instead of hardcoding classes for
  170. * specific chipsets.
  171. */
  172. dev = (struct nouveau_device *)obj->parent;
  173. if (dev->chipset >= 0x98 &&
  174. dev->chipset != 0xa0 &&
  175. dev->chipset < 0xc0) {
  176. *sclass++ = (struct nouveau_sclass){
  177. GT212_MSVLD, -1, -1
  178. };
  179. *sclass++ = (struct nouveau_sclass){
  180. GT212_MSPDEC, -1, -1
  181. };
  182. *sclass++ = (struct nouveau_sclass){
  183. GT212_MSPPP, -1, -1
  184. };
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. return sclass - *psclass;
  191. }
  192. drm_private void
  193. abi16_delete(struct nouveau_object *obj)
  194. {
  195. struct nouveau_drm *drm = nouveau_drm(obj);
  196. if (obj->oclass == NOUVEAU_FIFO_CHANNEL_CLASS) {
  197. struct drm_nouveau_channel_free req;
  198. req.channel = obj->handle;
  199. drmCommandWrite(drm->fd, DRM_NOUVEAU_CHANNEL_FREE,
  200. &req, sizeof(req));
  201. } else {
  202. struct drm_nouveau_gpuobj_free req;
  203. req.channel = obj->parent->handle;
  204. req.handle = obj->handle;
  205. drmCommandWrite(drm->fd, DRM_NOUVEAU_GPUOBJ_FREE,
  206. &req, sizeof(req));
  207. }
  208. }
  209. drm_private bool
  210. abi16_object(struct nouveau_object *obj, int (**func)(struct nouveau_object *))
  211. {
  212. struct nouveau_object *parent = obj->parent;
  213. /* nouveau_object::length is (ab)used to determine whether the
  214. * object is a legacy object (!=0), or a real NVIF object.
  215. */
  216. if ((parent->length != 0 && parent->oclass == NOUVEAU_DEVICE_CLASS) ||
  217. (parent->length == 0 && parent->oclass == NV_DEVICE)) {
  218. if (obj->oclass == NOUVEAU_FIFO_CHANNEL_CLASS) {
  219. struct nouveau_device *dev = (void *)parent;
  220. if (dev->chipset < 0xc0)
  221. *func = abi16_chan_nv04;
  222. else
  223. if (dev->chipset < 0xe0)
  224. *func = abi16_chan_nvc0;
  225. else
  226. *func = abi16_chan_nve0;
  227. return true;
  228. }
  229. } else
  230. if ((parent->length != 0 &&
  231. parent->oclass == NOUVEAU_FIFO_CHANNEL_CLASS)) {
  232. if (obj->oclass == NOUVEAU_NOTIFIER_CLASS) {
  233. *func = abi16_ntfy;
  234. return true;
  235. }
  236. *func = abi16_engobj;
  237. return false; /* try NVIF, if supported, before calling func */
  238. }
  239. *func = NULL;
  240. return false;
  241. }
  242. drm_private void
  243. abi16_bo_info(struct nouveau_bo *bo, struct drm_nouveau_gem_info *info)
  244. {
  245. struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
  246. nvbo->map_handle = info->map_handle;
  247. bo->handle = info->handle;
  248. bo->size = info->size;
  249. bo->offset = info->offset;
  250. bo->flags = 0;
  251. if (info->domain & NOUVEAU_GEM_DOMAIN_VRAM)
  252. bo->flags |= NOUVEAU_BO_VRAM;
  253. if (info->domain & NOUVEAU_GEM_DOMAIN_GART)
  254. bo->flags |= NOUVEAU_BO_GART;
  255. if (!(info->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG))
  256. bo->flags |= NOUVEAU_BO_CONTIG;
  257. if (nvbo->map_handle)
  258. bo->flags |= NOUVEAU_BO_MAP;
  259. if (bo->device->chipset >= 0xc0) {
  260. bo->config.nvc0.memtype = (info->tile_flags & 0xff00) >> 8;
  261. bo->config.nvc0.tile_mode = info->tile_mode;
  262. } else
  263. if (bo->device->chipset >= 0x80 || bo->device->chipset == 0x50) {
  264. bo->config.nv50.memtype = (info->tile_flags & 0x07f00) >> 8 |
  265. (info->tile_flags & 0x30000) >> 9;
  266. bo->config.nv50.tile_mode = info->tile_mode << 4;
  267. } else {
  268. bo->config.nv04.surf_flags = info->tile_flags & 7;
  269. bo->config.nv04.surf_pitch = info->tile_mode;
  270. }
  271. }
  272. drm_private int
  273. abi16_bo_init(struct nouveau_bo *bo, uint32_t alignment,
  274. union nouveau_bo_config *config)
  275. {
  276. struct nouveau_device *dev = bo->device;
  277. struct nouveau_drm *drm = nouveau_drm(&dev->object);
  278. struct drm_nouveau_gem_new req = {};
  279. struct drm_nouveau_gem_info *info = &req.info;
  280. int ret;
  281. if (bo->flags & NOUVEAU_BO_VRAM)
  282. info->domain |= NOUVEAU_GEM_DOMAIN_VRAM;
  283. if (bo->flags & NOUVEAU_BO_GART)
  284. info->domain |= NOUVEAU_GEM_DOMAIN_GART;
  285. if (!info->domain)
  286. info->domain |= NOUVEAU_GEM_DOMAIN_VRAM |
  287. NOUVEAU_GEM_DOMAIN_GART;
  288. if (bo->flags & NOUVEAU_BO_MAP)
  289. info->domain |= NOUVEAU_GEM_DOMAIN_MAPPABLE;
  290. if (bo->flags & NOUVEAU_BO_COHERENT)
  291. info->domain |= NOUVEAU_GEM_DOMAIN_COHERENT;
  292. if (!(bo->flags & NOUVEAU_BO_CONTIG))
  293. info->tile_flags = NOUVEAU_GEM_TILE_NONCONTIG;
  294. info->size = bo->size;
  295. req.align = alignment;
  296. if (config) {
  297. if (dev->chipset >= 0xc0) {
  298. info->tile_flags = (config->nvc0.memtype & 0xff) << 8;
  299. info->tile_mode = config->nvc0.tile_mode;
  300. } else
  301. if (dev->chipset >= 0x80 || dev->chipset == 0x50) {
  302. info->tile_flags = (config->nv50.memtype & 0x07f) << 8 |
  303. (config->nv50.memtype & 0x180) << 9;
  304. info->tile_mode = config->nv50.tile_mode >> 4;
  305. } else {
  306. info->tile_flags = config->nv04.surf_flags & 7;
  307. info->tile_mode = config->nv04.surf_pitch;
  308. }
  309. }
  310. if (!nouveau_device(dev)->have_bo_usage)
  311. info->tile_flags &= 0x0000ff00;
  312. ret = drmCommandWriteRead(drm->fd, DRM_NOUVEAU_GEM_NEW,
  313. &req, sizeof(req));
  314. if (ret == 0)
  315. abi16_bo_info(bo, &req.info);
  316. return ret;
  317. }