nouveau_sgdma.c 13 KB

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