nouveau_sgdma.c 13 KB

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