nouveau_sgdma.c 11 KB

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