nouveau_sgdma.c 13 KB

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