nouveau_sgdma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #include "drmP.h"
  2. #include "nouveau_drv.h"
  3. #include <linux/pagemap.h>
  4. #include <linux/slab.h>
  5. #define NV_CTXDMA_PAGE_SHIFT 12
  6. #define NV_CTXDMA_PAGE_SIZE (1 << NV_CTXDMA_PAGE_SHIFT)
  7. #define NV_CTXDMA_PAGE_MASK (NV_CTXDMA_PAGE_SIZE - 1)
  8. struct nouveau_sgdma_be {
  9. /* this has to be the first field so populate/unpopulated in
  10. * nouve_bo.c works properly, otherwise have to move them here
  11. */
  12. struct ttm_dma_tt ttm;
  13. struct drm_device *dev;
  14. u64 offset;
  15. };
  16. static void
  17. nouveau_sgdma_destroy(struct ttm_tt *ttm)
  18. {
  19. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  20. if (ttm) {
  21. NV_DEBUG(nvbe->dev, "\n");
  22. ttm_dma_tt_fini(&nvbe->ttm);
  23. kfree(nvbe);
  24. }
  25. }
  26. static int
  27. nv04_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
  28. {
  29. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  30. struct drm_device *dev = nvbe->dev;
  31. struct drm_nouveau_private *dev_priv = dev->dev_private;
  32. struct nouveau_gpuobj *gpuobj = dev_priv->gart_info.sg_ctxdma;
  33. unsigned i, j, pte;
  34. NV_DEBUG(dev, "pg=0x%lx\n", mem->start);
  35. nvbe->offset = mem->start << PAGE_SHIFT;
  36. pte = (nvbe->offset >> NV_CTXDMA_PAGE_SHIFT) + 2;
  37. for (i = 0; i < ttm->num_pages; i++) {
  38. dma_addr_t dma_offset = nvbe->ttm.dma_address[i];
  39. uint32_t offset_l = lower_32_bits(dma_offset);
  40. for (j = 0; j < PAGE_SIZE / NV_CTXDMA_PAGE_SIZE; j++, pte++) {
  41. nv_wo32(gpuobj, (pte * 4) + 0, offset_l | 3);
  42. offset_l += NV_CTXDMA_PAGE_SIZE;
  43. }
  44. }
  45. return 0;
  46. }
  47. static int
  48. nv04_sgdma_unbind(struct ttm_tt *ttm)
  49. {
  50. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  51. struct drm_device *dev = nvbe->dev;
  52. struct drm_nouveau_private *dev_priv = dev->dev_private;
  53. struct nouveau_gpuobj *gpuobj = dev_priv->gart_info.sg_ctxdma;
  54. unsigned i, j, pte;
  55. NV_DEBUG(dev, "\n");
  56. if (ttm->state != tt_bound)
  57. return 0;
  58. pte = (nvbe->offset >> NV_CTXDMA_PAGE_SHIFT) + 2;
  59. for (i = 0; i < ttm->num_pages; i++) {
  60. for (j = 0; j < PAGE_SIZE / NV_CTXDMA_PAGE_SIZE; j++, pte++)
  61. nv_wo32(gpuobj, (pte * 4) + 0, 0x00000000);
  62. }
  63. return 0;
  64. }
  65. static struct ttm_backend_func nv04_sgdma_backend = {
  66. .bind = nv04_sgdma_bind,
  67. .unbind = nv04_sgdma_unbind,
  68. .destroy = nouveau_sgdma_destroy
  69. };
  70. static void
  71. nv41_sgdma_flush(struct nouveau_sgdma_be *nvbe)
  72. {
  73. struct drm_device *dev = nvbe->dev;
  74. nv_wr32(dev, 0x100810, 0x00000022);
  75. if (!nv_wait(dev, 0x100810, 0x00000100, 0x00000100))
  76. NV_ERROR(dev, "vm flush timeout: 0x%08x\n",
  77. nv_rd32(dev, 0x100810));
  78. nv_wr32(dev, 0x100810, 0x00000000);
  79. }
  80. static int
  81. nv41_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
  82. {
  83. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  84. struct drm_nouveau_private *dev_priv = nvbe->dev->dev_private;
  85. struct nouveau_gpuobj *pgt = dev_priv->gart_info.sg_ctxdma;
  86. dma_addr_t *list = nvbe->ttm.dma_address;
  87. u32 pte = mem->start << 2;
  88. u32 cnt = ttm->num_pages;
  89. nvbe->offset = mem->start << PAGE_SHIFT;
  90. while (cnt--) {
  91. nv_wo32(pgt, pte, (*list++ >> 7) | 1);
  92. pte += 4;
  93. }
  94. nv41_sgdma_flush(nvbe);
  95. return 0;
  96. }
  97. static int
  98. nv41_sgdma_unbind(struct ttm_tt *ttm)
  99. {
  100. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  101. struct drm_nouveau_private *dev_priv = nvbe->dev->dev_private;
  102. struct nouveau_gpuobj *pgt = dev_priv->gart_info.sg_ctxdma;
  103. u32 pte = (nvbe->offset >> 12) << 2;
  104. u32 cnt = ttm->num_pages;
  105. while (cnt--) {
  106. nv_wo32(pgt, pte, 0x00000000);
  107. pte += 4;
  108. }
  109. nv41_sgdma_flush(nvbe);
  110. return 0;
  111. }
  112. static struct ttm_backend_func nv41_sgdma_backend = {
  113. .bind = nv41_sgdma_bind,
  114. .unbind = nv41_sgdma_unbind,
  115. .destroy = nouveau_sgdma_destroy
  116. };
  117. static void
  118. nv44_sgdma_flush(struct ttm_tt *ttm)
  119. {
  120. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  121. struct drm_device *dev = nvbe->dev;
  122. nv_wr32(dev, 0x100814, (ttm->num_pages - 1) << 12);
  123. nv_wr32(dev, 0x100808, nvbe->offset | 0x20);
  124. if (!nv_wait(dev, 0x100808, 0x00000001, 0x00000001))
  125. NV_ERROR(dev, "gart flush timeout: 0x%08x\n",
  126. nv_rd32(dev, 0x100808));
  127. nv_wr32(dev, 0x100808, 0x00000000);
  128. }
  129. static void
  130. nv44_sgdma_fill(struct nouveau_gpuobj *pgt, dma_addr_t *list, u32 base, u32 cnt)
  131. {
  132. struct drm_nouveau_private *dev_priv = pgt->dev->dev_private;
  133. dma_addr_t dummy = dev_priv->gart_info.dummy.addr;
  134. u32 pte, tmp[4];
  135. pte = base >> 2;
  136. base &= ~0x0000000f;
  137. tmp[0] = nv_ro32(pgt, base + 0x0);
  138. tmp[1] = nv_ro32(pgt, base + 0x4);
  139. tmp[2] = nv_ro32(pgt, base + 0x8);
  140. tmp[3] = nv_ro32(pgt, base + 0xc);
  141. while (cnt--) {
  142. u32 addr = list ? (*list++ >> 12) : (dummy >> 12);
  143. switch (pte++ & 0x3) {
  144. case 0:
  145. tmp[0] &= ~0x07ffffff;
  146. tmp[0] |= addr;
  147. break;
  148. case 1:
  149. tmp[0] &= ~0xf8000000;
  150. tmp[0] |= addr << 27;
  151. tmp[1] &= ~0x003fffff;
  152. tmp[1] |= addr >> 5;
  153. break;
  154. case 2:
  155. tmp[1] &= ~0xffc00000;
  156. tmp[1] |= addr << 22;
  157. tmp[2] &= ~0x0001ffff;
  158. tmp[2] |= addr >> 10;
  159. break;
  160. case 3:
  161. tmp[2] &= ~0xfffe0000;
  162. tmp[2] |= addr << 17;
  163. tmp[3] &= ~0x00000fff;
  164. tmp[3] |= addr >> 15;
  165. break;
  166. }
  167. }
  168. tmp[3] |= 0x40000000;
  169. nv_wo32(pgt, base + 0x0, tmp[0]);
  170. nv_wo32(pgt, base + 0x4, tmp[1]);
  171. nv_wo32(pgt, base + 0x8, tmp[2]);
  172. nv_wo32(pgt, base + 0xc, tmp[3]);
  173. }
  174. static int
  175. nv44_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
  176. {
  177. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  178. struct drm_nouveau_private *dev_priv = nvbe->dev->dev_private;
  179. struct nouveau_gpuobj *pgt = dev_priv->gart_info.sg_ctxdma;
  180. dma_addr_t *list = nvbe->ttm.dma_address;
  181. u32 pte = mem->start << 2, tmp[4];
  182. u32 cnt = ttm->num_pages;
  183. int i;
  184. nvbe->offset = mem->start << PAGE_SHIFT;
  185. if (pte & 0x0000000c) {
  186. u32 max = 4 - ((pte >> 2) & 0x3);
  187. u32 part = (cnt > max) ? max : cnt;
  188. nv44_sgdma_fill(pgt, list, pte, part);
  189. pte += (part << 2);
  190. list += part;
  191. cnt -= part;
  192. }
  193. while (cnt >= 4) {
  194. for (i = 0; i < 4; i++)
  195. tmp[i] = *list++ >> 12;
  196. nv_wo32(pgt, pte + 0x0, tmp[0] >> 0 | tmp[1] << 27);
  197. nv_wo32(pgt, pte + 0x4, tmp[1] >> 5 | tmp[2] << 22);
  198. nv_wo32(pgt, pte + 0x8, tmp[2] >> 10 | tmp[3] << 17);
  199. nv_wo32(pgt, pte + 0xc, tmp[3] >> 15 | 0x40000000);
  200. pte += 0x10;
  201. cnt -= 4;
  202. }
  203. if (cnt)
  204. nv44_sgdma_fill(pgt, list, pte, cnt);
  205. nv44_sgdma_flush(ttm);
  206. return 0;
  207. }
  208. static int
  209. nv44_sgdma_unbind(struct ttm_tt *ttm)
  210. {
  211. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  212. struct drm_nouveau_private *dev_priv = nvbe->dev->dev_private;
  213. struct nouveau_gpuobj *pgt = dev_priv->gart_info.sg_ctxdma;
  214. u32 pte = (nvbe->offset >> 12) << 2;
  215. u32 cnt = ttm->num_pages;
  216. if (pte & 0x0000000c) {
  217. u32 max = 4 - ((pte >> 2) & 0x3);
  218. u32 part = (cnt > max) ? max : cnt;
  219. nv44_sgdma_fill(pgt, NULL, pte, part);
  220. pte += (part << 2);
  221. cnt -= part;
  222. }
  223. while (cnt >= 4) {
  224. nv_wo32(pgt, pte + 0x0, 0x00000000);
  225. nv_wo32(pgt, pte + 0x4, 0x00000000);
  226. nv_wo32(pgt, pte + 0x8, 0x00000000);
  227. nv_wo32(pgt, pte + 0xc, 0x00000000);
  228. pte += 0x10;
  229. cnt -= 4;
  230. }
  231. if (cnt)
  232. nv44_sgdma_fill(pgt, NULL, pte, cnt);
  233. nv44_sgdma_flush(ttm);
  234. return 0;
  235. }
  236. static struct ttm_backend_func nv44_sgdma_backend = {
  237. .bind = nv44_sgdma_bind,
  238. .unbind = nv44_sgdma_unbind,
  239. .destroy = nouveau_sgdma_destroy
  240. };
  241. static int
  242. nv50_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
  243. {
  244. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  245. struct nouveau_mem *node = mem->mm_node;
  246. /* noop: bound in move_notify() */
  247. node->pages = nvbe->ttm.dma_address;
  248. return 0;
  249. }
  250. static int
  251. nv50_sgdma_unbind(struct ttm_tt *ttm)
  252. {
  253. /* noop: unbound in move_notify() */
  254. return 0;
  255. }
  256. static struct ttm_backend_func nv50_sgdma_backend = {
  257. .bind = nv50_sgdma_bind,
  258. .unbind = nv50_sgdma_unbind,
  259. .destroy = nouveau_sgdma_destroy
  260. };
  261. struct ttm_tt *
  262. nouveau_sgdma_create_ttm(struct ttm_bo_device *bdev,
  263. unsigned long size, uint32_t page_flags,
  264. struct page *dummy_read_page)
  265. {
  266. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  267. struct drm_device *dev = dev_priv->dev;
  268. struct nouveau_sgdma_be *nvbe;
  269. nvbe = kzalloc(sizeof(*nvbe), GFP_KERNEL);
  270. if (!nvbe)
  271. return NULL;
  272. nvbe->dev = dev;
  273. nvbe->ttm.ttm.func = dev_priv->gart_info.func;
  274. if (ttm_dma_tt_init(&nvbe->ttm, bdev, size, page_flags, dummy_read_page)) {
  275. kfree(nvbe);
  276. return NULL;
  277. }
  278. return &nvbe->ttm.ttm;
  279. }
  280. int
  281. nouveau_sgdma_init(struct drm_device *dev)
  282. {
  283. struct drm_nouveau_private *dev_priv = dev->dev_private;
  284. struct nouveau_gpuobj *gpuobj = NULL;
  285. u32 aper_size, align;
  286. int ret;
  287. if (dev_priv->card_type >= NV_40 && pci_is_pcie(dev->pdev))
  288. aper_size = 512 * 1024 * 1024;
  289. else
  290. aper_size = 64 * 1024 * 1024;
  291. /* Dear NVIDIA, NV44+ would like proper present bits in PTEs for
  292. * christmas. The cards before it have them, the cards after
  293. * it have them, why is NV44 so unloved?
  294. */
  295. dev_priv->gart_info.dummy.page = alloc_page(GFP_DMA32 | GFP_KERNEL);
  296. if (!dev_priv->gart_info.dummy.page)
  297. return -ENOMEM;
  298. dev_priv->gart_info.dummy.addr =
  299. pci_map_page(dev->pdev, dev_priv->gart_info.dummy.page,
  300. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  301. if (pci_dma_mapping_error(dev->pdev, dev_priv->gart_info.dummy.addr)) {
  302. NV_ERROR(dev, "error mapping dummy page\n");
  303. __free_page(dev_priv->gart_info.dummy.page);
  304. dev_priv->gart_info.dummy.page = NULL;
  305. return -ENOMEM;
  306. }
  307. if (dev_priv->card_type >= NV_50) {
  308. dev_priv->gart_info.aper_base = 0;
  309. dev_priv->gart_info.aper_size = aper_size;
  310. dev_priv->gart_info.type = NOUVEAU_GART_HW;
  311. dev_priv->gart_info.func = &nv50_sgdma_backend;
  312. } else
  313. if (0 && pci_is_pcie(dev->pdev) &&
  314. dev_priv->chipset > 0x40 && dev_priv->chipset != 0x45) {
  315. if (nv44_graph_class(dev)) {
  316. dev_priv->gart_info.func = &nv44_sgdma_backend;
  317. align = 512 * 1024;
  318. } else {
  319. dev_priv->gart_info.func = &nv41_sgdma_backend;
  320. align = 16;
  321. }
  322. ret = nouveau_gpuobj_new(dev, NULL, aper_size / 1024, align,
  323. NVOBJ_FLAG_ZERO_ALLOC |
  324. NVOBJ_FLAG_ZERO_FREE, &gpuobj);
  325. if (ret) {
  326. NV_ERROR(dev, "Error creating sgdma object: %d\n", ret);
  327. return ret;
  328. }
  329. dev_priv->gart_info.sg_ctxdma = gpuobj;
  330. dev_priv->gart_info.aper_base = 0;
  331. dev_priv->gart_info.aper_size = aper_size;
  332. dev_priv->gart_info.type = NOUVEAU_GART_HW;
  333. } else {
  334. ret = nouveau_gpuobj_new(dev, NULL, (aper_size / 1024) + 8, 16,
  335. NVOBJ_FLAG_ZERO_ALLOC |
  336. NVOBJ_FLAG_ZERO_FREE, &gpuobj);
  337. if (ret) {
  338. NV_ERROR(dev, "Error creating sgdma object: %d\n", ret);
  339. return ret;
  340. }
  341. nv_wo32(gpuobj, 0, NV_CLASS_DMA_IN_MEMORY |
  342. (1 << 12) /* PT present */ |
  343. (0 << 13) /* PT *not* linear */ |
  344. (0 << 14) /* RW */ |
  345. (2 << 16) /* PCI */);
  346. nv_wo32(gpuobj, 4, aper_size - 1);
  347. dev_priv->gart_info.sg_ctxdma = gpuobj;
  348. dev_priv->gart_info.aper_base = 0;
  349. dev_priv->gart_info.aper_size = aper_size;
  350. dev_priv->gart_info.type = NOUVEAU_GART_PDMA;
  351. dev_priv->gart_info.func = &nv04_sgdma_backend;
  352. }
  353. return 0;
  354. }
  355. void
  356. nouveau_sgdma_takedown(struct drm_device *dev)
  357. {
  358. struct drm_nouveau_private *dev_priv = dev->dev_private;
  359. nouveau_gpuobj_ref(NULL, &dev_priv->gart_info.sg_ctxdma);
  360. if (dev_priv->gart_info.dummy.page) {
  361. pci_unmap_page(dev->pdev, dev_priv->gart_info.dummy.addr,
  362. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  363. __free_page(dev_priv->gart_info.dummy.page);
  364. dev_priv->gart_info.dummy.page = NULL;
  365. }
  366. }
  367. uint32_t
  368. nouveau_sgdma_get_physical(struct drm_device *dev, uint32_t offset)
  369. {
  370. struct drm_nouveau_private *dev_priv = dev->dev_private;
  371. struct nouveau_gpuobj *gpuobj = dev_priv->gart_info.sg_ctxdma;
  372. int pte = (offset >> NV_CTXDMA_PAGE_SHIFT) + 2;
  373. BUG_ON(dev_priv->card_type >= NV_50);
  374. return (nv_ro32(gpuobj, 4 * pte) & ~NV_CTXDMA_PAGE_MASK) |
  375. (offset & NV_CTXDMA_PAGE_MASK);
  376. }