nouveau_sgdma.c 13 KB

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