nouveau_sgdma.c 13 KB

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