nouveau_vm.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright 2010 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 "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_mm.h"
  27. #include "nouveau_vm.h"
  28. void
  29. nouveau_vm_map_at(struct nouveau_vma *vma, u64 delta, struct nouveau_vram *vram)
  30. {
  31. struct nouveau_vm *vm = vma->vm;
  32. struct nouveau_mm_node *r;
  33. int big = vma->node->type != vm->spg_shift;
  34. u32 offset = vma->node->offset + (delta >> 12);
  35. u32 bits = vma->node->type - 12;
  36. u32 pde = (offset >> vm->pgt_bits) - vm->fpde;
  37. u32 pte = (offset & ((1 << vm->pgt_bits) - 1)) >> bits;
  38. u32 max = 1 << (vm->pgt_bits - bits);
  39. u32 end, len;
  40. list_for_each_entry(r, &vram->regions, rl_entry) {
  41. u64 phys = (u64)r->offset << 12;
  42. u32 num = r->length >> bits;
  43. while (num) {
  44. struct nouveau_gpuobj *pgt = vm->pgt[pde].obj[big];
  45. end = (pte + num);
  46. if (unlikely(end >= max))
  47. end = max;
  48. len = end - pte;
  49. vm->map(vma, pgt, vram, pte, len, phys);
  50. num -= len;
  51. pte += len;
  52. if (unlikely(end >= max)) {
  53. pde++;
  54. pte = 0;
  55. }
  56. }
  57. }
  58. vm->flush(vm);
  59. }
  60. void
  61. nouveau_vm_map(struct nouveau_vma *vma, struct nouveau_vram *vram)
  62. {
  63. nouveau_vm_map_at(vma, 0, vram);
  64. }
  65. void
  66. nouveau_vm_map_sg(struct nouveau_vma *vma, u64 delta, u64 length,
  67. dma_addr_t *list)
  68. {
  69. struct nouveau_vm *vm = vma->vm;
  70. int big = vma->node->type != vm->spg_shift;
  71. u32 offset = vma->node->offset + (delta >> 12);
  72. u32 bits = vma->node->type - 12;
  73. u32 num = length >> vma->node->type;
  74. u32 pde = (offset >> vm->pgt_bits) - vm->fpde;
  75. u32 pte = (offset & ((1 << vm->pgt_bits) - 1)) >> bits;
  76. u32 max = 1 << (vm->pgt_bits - bits);
  77. u32 end, len;
  78. while (num) {
  79. struct nouveau_gpuobj *pgt = vm->pgt[pde].obj[big];
  80. end = (pte + num);
  81. if (unlikely(end >= max))
  82. end = max;
  83. len = end - pte;
  84. vm->map_sg(vma, pgt, pte, list, len);
  85. num -= len;
  86. pte += len;
  87. list += len;
  88. if (unlikely(end >= max)) {
  89. pde++;
  90. pte = 0;
  91. }
  92. }
  93. vm->flush(vm);
  94. }
  95. void
  96. nouveau_vm_unmap_at(struct nouveau_vma *vma, u64 delta, u64 length)
  97. {
  98. struct nouveau_vm *vm = vma->vm;
  99. int big = vma->node->type != vm->spg_shift;
  100. u32 offset = vma->node->offset + (delta >> 12);
  101. u32 bits = vma->node->type - 12;
  102. u32 num = length >> vma->node->type;
  103. u32 pde = (offset >> vm->pgt_bits) - vm->fpde;
  104. u32 pte = (offset & ((1 << vm->pgt_bits) - 1)) >> bits;
  105. u32 max = 1 << (vm->pgt_bits - bits);
  106. u32 end, len;
  107. while (num) {
  108. struct nouveau_gpuobj *pgt = vm->pgt[pde].obj[big];
  109. end = (pte + num);
  110. if (unlikely(end >= max))
  111. end = max;
  112. len = end - pte;
  113. vm->unmap(pgt, pte, len);
  114. num -= len;
  115. pte += len;
  116. if (unlikely(end >= max)) {
  117. pde++;
  118. pte = 0;
  119. }
  120. }
  121. vm->flush(vm);
  122. }
  123. void
  124. nouveau_vm_unmap(struct nouveau_vma *vma)
  125. {
  126. nouveau_vm_unmap_at(vma, 0, (u64)vma->node->length << 12);
  127. }
  128. static void
  129. nouveau_vm_unmap_pgt(struct nouveau_vm *vm, int big, u32 fpde, u32 lpde)
  130. {
  131. struct nouveau_vm_pgd *vpgd;
  132. struct nouveau_vm_pgt *vpgt;
  133. struct nouveau_gpuobj *pgt;
  134. u32 pde;
  135. for (pde = fpde; pde <= lpde; pde++) {
  136. vpgt = &vm->pgt[pde - vm->fpde];
  137. if (--vpgt->refcount[big])
  138. continue;
  139. pgt = vpgt->obj[big];
  140. vpgt->obj[big] = NULL;
  141. list_for_each_entry(vpgd, &vm->pgd_list, head) {
  142. vm->map_pgt(vpgd->obj, pde, vpgt->obj);
  143. }
  144. mutex_unlock(&vm->mm->mutex);
  145. nouveau_gpuobj_ref(NULL, &pgt);
  146. mutex_lock(&vm->mm->mutex);
  147. }
  148. }
  149. static int
  150. nouveau_vm_map_pgt(struct nouveau_vm *vm, u32 pde, u32 type)
  151. {
  152. struct nouveau_vm_pgt *vpgt = &vm->pgt[pde - vm->fpde];
  153. struct nouveau_vm_pgd *vpgd;
  154. struct nouveau_gpuobj *pgt;
  155. int big = (type != vm->spg_shift);
  156. u32 pgt_size;
  157. int ret;
  158. pgt_size = (1 << (vm->pgt_bits + 12)) >> type;
  159. pgt_size *= 8;
  160. mutex_unlock(&vm->mm->mutex);
  161. ret = nouveau_gpuobj_new(vm->dev, NULL, pgt_size, 0x1000,
  162. NVOBJ_FLAG_ZERO_ALLOC, &pgt);
  163. mutex_lock(&vm->mm->mutex);
  164. if (unlikely(ret))
  165. return ret;
  166. /* someone beat us to filling the PDE while we didn't have the lock */
  167. if (unlikely(vpgt->refcount[big]++)) {
  168. mutex_unlock(&vm->mm->mutex);
  169. nouveau_gpuobj_ref(NULL, &pgt);
  170. mutex_lock(&vm->mm->mutex);
  171. return 0;
  172. }
  173. vpgt->obj[big] = pgt;
  174. list_for_each_entry(vpgd, &vm->pgd_list, head) {
  175. vm->map_pgt(vpgd->obj, pde, vpgt->obj);
  176. }
  177. return 0;
  178. }
  179. int
  180. nouveau_vm_get(struct nouveau_vm *vm, u64 size, u32 page_shift,
  181. u32 access, struct nouveau_vma *vma)
  182. {
  183. u32 align = (1 << page_shift) >> 12;
  184. u32 msize = size >> 12;
  185. u32 fpde, lpde, pde;
  186. int ret;
  187. mutex_lock(&vm->mm->mutex);
  188. ret = nouveau_mm_get(vm->mm, page_shift, msize, 0, align, &vma->node);
  189. if (unlikely(ret != 0)) {
  190. mutex_unlock(&vm->mm->mutex);
  191. return ret;
  192. }
  193. fpde = (vma->node->offset >> vm->pgt_bits);
  194. lpde = (vma->node->offset + vma->node->length - 1) >> vm->pgt_bits;
  195. for (pde = fpde; pde <= lpde; pde++) {
  196. struct nouveau_vm_pgt *vpgt = &vm->pgt[pde - vm->fpde];
  197. int big = (vma->node->type != vm->spg_shift);
  198. if (likely(vpgt->refcount[big])) {
  199. vpgt->refcount[big]++;
  200. continue;
  201. }
  202. ret = nouveau_vm_map_pgt(vm, pde, vma->node->type);
  203. if (ret) {
  204. if (pde != fpde)
  205. nouveau_vm_unmap_pgt(vm, big, fpde, pde - 1);
  206. nouveau_mm_put(vm->mm, vma->node);
  207. mutex_unlock(&vm->mm->mutex);
  208. vma->node = NULL;
  209. return ret;
  210. }
  211. }
  212. mutex_unlock(&vm->mm->mutex);
  213. vma->vm = vm;
  214. vma->offset = (u64)vma->node->offset << 12;
  215. vma->access = access;
  216. return 0;
  217. }
  218. void
  219. nouveau_vm_put(struct nouveau_vma *vma)
  220. {
  221. struct nouveau_vm *vm = vma->vm;
  222. u32 fpde, lpde;
  223. if (unlikely(vma->node == NULL))
  224. return;
  225. fpde = (vma->node->offset >> vm->pgt_bits);
  226. lpde = (vma->node->offset + vma->node->length - 1) >> vm->pgt_bits;
  227. mutex_lock(&vm->mm->mutex);
  228. nouveau_vm_unmap_pgt(vm, vma->node->type != vm->spg_shift, fpde, lpde);
  229. nouveau_mm_put(vm->mm, vma->node);
  230. vma->node = NULL;
  231. mutex_unlock(&vm->mm->mutex);
  232. }
  233. int
  234. nouveau_vm_new(struct drm_device *dev, u64 offset, u64 length, u64 mm_offset,
  235. struct nouveau_vm **pvm)
  236. {
  237. struct drm_nouveau_private *dev_priv = dev->dev_private;
  238. struct nouveau_vm *vm;
  239. u64 mm_length = (offset + length) - mm_offset;
  240. u32 block, pgt_bits;
  241. int ret;
  242. vm = kzalloc(sizeof(*vm), GFP_KERNEL);
  243. if (!vm)
  244. return -ENOMEM;
  245. if (dev_priv->card_type == NV_50) {
  246. vm->map_pgt = nv50_vm_map_pgt;
  247. vm->map = nv50_vm_map;
  248. vm->map_sg = nv50_vm_map_sg;
  249. vm->unmap = nv50_vm_unmap;
  250. vm->flush = nv50_vm_flush;
  251. vm->spg_shift = 12;
  252. vm->lpg_shift = 16;
  253. pgt_bits = 29;
  254. block = (1 << pgt_bits);
  255. if (length < block)
  256. block = length;
  257. } else
  258. if (dev_priv->card_type == NV_C0) {
  259. vm->map_pgt = nvc0_vm_map_pgt;
  260. vm->map = nvc0_vm_map;
  261. vm->map_sg = nvc0_vm_map_sg;
  262. vm->unmap = nvc0_vm_unmap;
  263. vm->flush = nvc0_vm_flush;
  264. vm->spg_shift = 12;
  265. vm->lpg_shift = 17;
  266. pgt_bits = 27;
  267. /* Should be 4096 everywhere, this is a hack that's
  268. * currently necessary to avoid an elusive bug that
  269. * causes corruption when mixing small/large pages
  270. */
  271. if (length < (1ULL << 40))
  272. block = 4096;
  273. else {
  274. block = (1 << pgt_bits);
  275. if (length < block)
  276. block = length;
  277. }
  278. } else {
  279. kfree(vm);
  280. return -ENOSYS;
  281. }
  282. vm->fpde = offset >> pgt_bits;
  283. vm->lpde = (offset + length - 1) >> pgt_bits;
  284. vm->pgt = kcalloc(vm->lpde - vm->fpde + 1, sizeof(*vm->pgt), GFP_KERNEL);
  285. if (!vm->pgt) {
  286. kfree(vm);
  287. return -ENOMEM;
  288. }
  289. INIT_LIST_HEAD(&vm->pgd_list);
  290. vm->dev = dev;
  291. vm->refcount = 1;
  292. vm->pgt_bits = pgt_bits - 12;
  293. ret = nouveau_mm_init(&vm->mm, mm_offset >> 12, mm_length >> 12,
  294. block >> 12);
  295. if (ret) {
  296. kfree(vm);
  297. return ret;
  298. }
  299. *pvm = vm;
  300. return 0;
  301. }
  302. static int
  303. nouveau_vm_link(struct nouveau_vm *vm, struct nouveau_gpuobj *pgd)
  304. {
  305. struct nouveau_vm_pgd *vpgd;
  306. int i;
  307. if (!pgd)
  308. return 0;
  309. vpgd = kzalloc(sizeof(*vpgd), GFP_KERNEL);
  310. if (!vpgd)
  311. return -ENOMEM;
  312. nouveau_gpuobj_ref(pgd, &vpgd->obj);
  313. mutex_lock(&vm->mm->mutex);
  314. for (i = vm->fpde; i <= vm->lpde; i++)
  315. vm->map_pgt(pgd, i, vm->pgt[i - vm->fpde].obj);
  316. list_add(&vpgd->head, &vm->pgd_list);
  317. mutex_unlock(&vm->mm->mutex);
  318. return 0;
  319. }
  320. static void
  321. nouveau_vm_unlink(struct nouveau_vm *vm, struct nouveau_gpuobj *pgd)
  322. {
  323. struct nouveau_vm_pgd *vpgd, *tmp;
  324. if (!pgd)
  325. return;
  326. mutex_lock(&vm->mm->mutex);
  327. list_for_each_entry_safe(vpgd, tmp, &vm->pgd_list, head) {
  328. if (vpgd->obj != pgd)
  329. continue;
  330. list_del(&vpgd->head);
  331. nouveau_gpuobj_ref(NULL, &vpgd->obj);
  332. kfree(vpgd);
  333. }
  334. mutex_unlock(&vm->mm->mutex);
  335. }
  336. static void
  337. nouveau_vm_del(struct nouveau_vm *vm)
  338. {
  339. struct nouveau_vm_pgd *vpgd, *tmp;
  340. list_for_each_entry_safe(vpgd, tmp, &vm->pgd_list, head) {
  341. nouveau_vm_unlink(vm, vpgd->obj);
  342. }
  343. WARN_ON(nouveau_mm_fini(&vm->mm) != 0);
  344. kfree(vm->pgt);
  345. kfree(vm);
  346. }
  347. int
  348. nouveau_vm_ref(struct nouveau_vm *ref, struct nouveau_vm **ptr,
  349. struct nouveau_gpuobj *pgd)
  350. {
  351. struct nouveau_vm *vm;
  352. int ret;
  353. vm = ref;
  354. if (vm) {
  355. ret = nouveau_vm_link(vm, pgd);
  356. if (ret)
  357. return ret;
  358. vm->refcount++;
  359. }
  360. vm = *ptr;
  361. *ptr = ref;
  362. if (vm) {
  363. nouveau_vm_unlink(vm, pgd);
  364. if (--vm->refcount == 0)
  365. nouveau_vm_del(vm);
  366. }
  367. return 0;
  368. }