nouveau_vm.c 9.7 KB

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