2 #include "nouveau_drv.h"
3 #include <linux/pagemap.h>
4 #include <linux/slab.h>
6 #define NV_CTXDMA_PAGE_SHIFT 12
7 #define NV_CTXDMA_PAGE_SIZE (1 << NV_CTXDMA_PAGE_SHIFT)
8 #define NV_CTXDMA_PAGE_MASK (NV_CTXDMA_PAGE_SIZE - 1)
10 struct nouveau_sgdma_be {
11 struct ttm_backend backend;
12 struct drm_device *dev;
23 nouveau_sgdma_populate(struct ttm_backend *be, unsigned long num_pages,
24 struct page **pages, struct page *dummy_read_page,
25 dma_addr_t *dma_addrs)
27 struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)be;
28 struct drm_device *dev = nvbe->dev;
30 NV_DEBUG(nvbe->dev, "num_pages = %ld\n", num_pages);
35 nvbe->pages = kmalloc(sizeof(dma_addr_t) * num_pages, GFP_KERNEL);
39 nvbe->ttm_alloced = kmalloc(sizeof(bool) * num_pages, GFP_KERNEL);
40 if (!nvbe->ttm_alloced)
45 /* this code path isn't called and is incorrect anyways */
46 if (0) { /*dma_addrs[nvbe->nr_pages] != DMA_ERROR_CODE)*/
47 nvbe->pages[nvbe->nr_pages] =
48 dma_addrs[nvbe->nr_pages];
49 nvbe->ttm_alloced[nvbe->nr_pages] = true;
51 nvbe->pages[nvbe->nr_pages] =
52 pci_map_page(dev->pdev, pages[nvbe->nr_pages], 0,
53 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
54 if (pci_dma_mapping_error(dev->pdev,
55 nvbe->pages[nvbe->nr_pages])) {
59 nvbe->ttm_alloced[nvbe->nr_pages] = false;
69 nouveau_sgdma_clear(struct ttm_backend *be)
71 struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)be;
72 struct drm_device *dev;
74 if (nvbe && nvbe->pages) {
81 while (nvbe->nr_pages--) {
82 if (!nvbe->ttm_alloced[nvbe->nr_pages])
83 pci_unmap_page(dev->pdev, nvbe->pages[nvbe->nr_pages],
84 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
87 kfree(nvbe->ttm_alloced);
89 nvbe->ttm_alloced = NULL;
95 nouveau_sgdma_destroy(struct ttm_backend *be)
97 struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)be;
100 NV_DEBUG(nvbe->dev, "\n");
111 nv04_sgdma_bind(struct ttm_backend *be, struct ttm_mem_reg *mem)
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;
119 NV_DEBUG(dev, "pg=0x%lx\n", mem->start);
121 nvbe->offset = mem->start << PAGE_SHIFT;
122 pte = (nvbe->offset >> NV_CTXDMA_PAGE_SHIFT) + 2;
123 for (i = 0; i < nvbe->nr_pages; i++) {
124 dma_addr_t dma_offset = nvbe->pages[i];
125 uint32_t offset_l = lower_32_bits(dma_offset);
127 for (j = 0; j < PAGE_SIZE / NV_CTXDMA_PAGE_SIZE; j++, pte++) {
128 nv_wo32(gpuobj, (pte * 4) + 0, offset_l | 3);
129 dma_offset += NV_CTXDMA_PAGE_SIZE;
138 nv04_sgdma_unbind(struct ttm_backend *be)
140 struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)be;
141 struct drm_device *dev = nvbe->dev;
142 struct drm_nouveau_private *dev_priv = dev->dev_private;
143 struct nouveau_gpuobj *gpuobj = dev_priv->gart_info.sg_ctxdma;
151 pte = (nvbe->offset >> NV_CTXDMA_PAGE_SHIFT) + 2;
152 for (i = 0; i < nvbe->nr_pages; i++) {
153 for (j = 0; j < PAGE_SIZE / NV_CTXDMA_PAGE_SIZE; j++, pte++)
154 nv_wo32(gpuobj, (pte * 4) + 0, 0x00000000);
161 static struct ttm_backend_func nv04_sgdma_backend = {
162 .populate = nouveau_sgdma_populate,
163 .clear = nouveau_sgdma_clear,
164 .bind = nv04_sgdma_bind,
165 .unbind = nv04_sgdma_unbind,
166 .destroy = nouveau_sgdma_destroy
170 nv41_sgdma_flush(struct nouveau_sgdma_be *nvbe)
172 struct drm_device *dev = nvbe->dev;
174 nv_wr32(dev, 0x100810, 0x00000022);
175 if (!nv_wait(dev, 0x100810, 0x00000100, 0x00000100))
176 NV_ERROR(dev, "vm flush timeout: 0x%08x\n",
177 nv_rd32(dev, 0x100810));
178 nv_wr32(dev, 0x100810, 0x00000000);
182 nv41_sgdma_bind(struct ttm_backend *be, struct ttm_mem_reg *mem)
184 struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)be;
185 struct drm_nouveau_private *dev_priv = nvbe->dev->dev_private;
186 struct nouveau_gpuobj *pgt = dev_priv->gart_info.sg_ctxdma;
187 dma_addr_t *list = nvbe->pages;
188 u32 pte = mem->start << 2;
189 u32 cnt = nvbe->nr_pages;
191 nvbe->offset = mem->start << PAGE_SHIFT;
194 nv_wo32(pgt, pte, (*list++ >> 7) | 1);
198 nv41_sgdma_flush(nvbe);
204 nv41_sgdma_unbind(struct ttm_backend *be)
206 struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)be;
207 struct drm_nouveau_private *dev_priv = nvbe->dev->dev_private;
208 struct nouveau_gpuobj *pgt = dev_priv->gart_info.sg_ctxdma;
209 u32 pte = (nvbe->offset >> 12) << 2;
210 u32 cnt = nvbe->nr_pages;
213 nv_wo32(pgt, pte, 0x00000000);
217 nv41_sgdma_flush(nvbe);
222 static struct ttm_backend_func nv41_sgdma_backend = {
223 .populate = nouveau_sgdma_populate,
224 .clear = nouveau_sgdma_clear,
225 .bind = nv41_sgdma_bind,
226 .unbind = nv41_sgdma_unbind,
227 .destroy = nouveau_sgdma_destroy
231 nv44_sgdma_flush(struct nouveau_sgdma_be *nvbe)
233 struct drm_device *dev = nvbe->dev;
235 nv_wr32(dev, 0x100814, (nvbe->nr_pages - 1) << 12);
236 nv_wr32(dev, 0x100808, nvbe->offset | 0x20);
237 if (!nv_wait(dev, 0x100808, 0x00000001, 0x00000001))
238 NV_ERROR(dev, "gart flush timeout: 0x%08x\n",
239 nv_rd32(dev, 0x100808));
240 nv_wr32(dev, 0x100808, 0x00000000);
244 nv44_sgdma_fill(struct nouveau_gpuobj *pgt, dma_addr_t *list, u32 base, u32 cnt)
246 struct drm_nouveau_private *dev_priv = pgt->dev->dev_private;
247 dma_addr_t dummy = dev_priv->gart_info.dummy.addr;
253 tmp[0] = nv_ro32(pgt, base + 0x0);
254 tmp[1] = nv_ro32(pgt, base + 0x4);
255 tmp[2] = nv_ro32(pgt, base + 0x8);
256 tmp[3] = nv_ro32(pgt, base + 0xc);
258 u32 addr = list ? (*list++ >> 12) : (dummy >> 12);
259 switch (pte++ & 0x3) {
261 tmp[0] &= ~0x07ffffff;
265 tmp[0] &= ~0xf8000000;
266 tmp[0] |= addr << 27;
267 tmp[1] &= ~0x003fffff;
271 tmp[1] &= ~0xffc00000;
272 tmp[1] |= addr << 22;
273 tmp[2] &= ~0x0001ffff;
274 tmp[2] |= addr >> 10;
277 tmp[2] &= ~0xfffe0000;
278 tmp[2] |= addr << 17;
279 tmp[3] &= ~0x00000fff;
280 tmp[3] |= addr >> 15;
285 tmp[3] |= 0x40000000;
287 nv_wo32(pgt, base + 0x0, tmp[0]);
288 nv_wo32(pgt, base + 0x4, tmp[1]);
289 nv_wo32(pgt, base + 0x8, tmp[2]);
290 nv_wo32(pgt, base + 0xc, tmp[3]);
294 nv44_sgdma_bind(struct ttm_backend *be, struct ttm_mem_reg *mem)
296 struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)be;
297 struct drm_nouveau_private *dev_priv = nvbe->dev->dev_private;
298 struct nouveau_gpuobj *pgt = dev_priv->gart_info.sg_ctxdma;
299 dma_addr_t *list = nvbe->pages;
300 u32 pte = mem->start << 2, tmp[4];
301 u32 cnt = nvbe->nr_pages;
304 nvbe->offset = mem->start << PAGE_SHIFT;
306 if (pte & 0x0000000c) {
307 u32 max = 4 - ((pte >> 2) & 0x3);
308 u32 part = (cnt > max) ? max : cnt;
309 nv44_sgdma_fill(pgt, list, pte, part);
316 for (i = 0; i < 4; i++)
317 tmp[i] = *list++ >> 12;
318 nv_wo32(pgt, pte + 0x0, tmp[0] >> 0 | tmp[1] << 27);
319 nv_wo32(pgt, pte + 0x4, tmp[1] >> 5 | tmp[2] << 22);
320 nv_wo32(pgt, pte + 0x8, tmp[2] >> 10 | tmp[3] << 17);
321 nv_wo32(pgt, pte + 0xc, tmp[3] >> 15 | 0x40000000);
327 nv44_sgdma_fill(pgt, list, pte, cnt);
329 nv44_sgdma_flush(nvbe);
335 nv44_sgdma_unbind(struct ttm_backend *be)
337 struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)be;
338 struct drm_nouveau_private *dev_priv = nvbe->dev->dev_private;
339 struct nouveau_gpuobj *pgt = dev_priv->gart_info.sg_ctxdma;
340 u32 pte = (nvbe->offset >> 12) << 2;
341 u32 cnt = nvbe->nr_pages;
343 if (pte & 0x0000000c) {
344 u32 max = 4 - ((pte >> 2) & 0x3);
345 u32 part = (cnt > max) ? max : cnt;
346 nv44_sgdma_fill(pgt, NULL, pte, part);
352 nv_wo32(pgt, pte + 0x0, 0x00000000);
353 nv_wo32(pgt, pte + 0x4, 0x00000000);
354 nv_wo32(pgt, pte + 0x8, 0x00000000);
355 nv_wo32(pgt, pte + 0xc, 0x00000000);
361 nv44_sgdma_fill(pgt, NULL, pte, cnt);
363 nv44_sgdma_flush(nvbe);
368 static struct ttm_backend_func nv44_sgdma_backend = {
369 .populate = nouveau_sgdma_populate,
370 .clear = nouveau_sgdma_clear,
371 .bind = nv44_sgdma_bind,
372 .unbind = nv44_sgdma_unbind,
373 .destroy = nouveau_sgdma_destroy
377 nv50_sgdma_bind(struct ttm_backend *be, struct ttm_mem_reg *mem)
379 struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)be;
380 struct nouveau_mem *node = mem->mm_node;
381 /* noop: bound in move_notify() */
382 node->pages = nvbe->pages;
383 nvbe->pages = (dma_addr_t *)node;
389 nv50_sgdma_unbind(struct ttm_backend *be)
391 struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)be;
392 struct nouveau_mem *node = (struct nouveau_mem *)nvbe->pages;
393 /* noop: unbound in move_notify() */
394 nvbe->pages = node->pages;
400 static struct ttm_backend_func nv50_sgdma_backend = {
401 .populate = nouveau_sgdma_populate,
402 .clear = nouveau_sgdma_clear,
403 .bind = nv50_sgdma_bind,
404 .unbind = nv50_sgdma_unbind,
405 .destroy = nouveau_sgdma_destroy
409 nouveau_sgdma_init_ttm(struct drm_device *dev)
411 struct drm_nouveau_private *dev_priv = dev->dev_private;
412 struct nouveau_sgdma_be *nvbe;
414 nvbe = kzalloc(sizeof(*nvbe), GFP_KERNEL);
420 nvbe->backend.func = dev_priv->gart_info.func;
421 return &nvbe->backend;
425 nouveau_sgdma_init(struct drm_device *dev)
427 struct drm_nouveau_private *dev_priv = dev->dev_private;
428 struct nouveau_gpuobj *gpuobj = NULL;
429 u32 aper_size, align;
432 if (dev_priv->card_type >= NV_40 && drm_pci_device_is_pcie(dev))
433 aper_size = 512 * 1024 * 1024;
435 aper_size = 64 * 1024 * 1024;
437 /* Dear NVIDIA, NV44+ would like proper present bits in PTEs for
438 * christmas. The cards before it have them, the cards after
439 * it have them, why is NV44 so unloved?
441 dev_priv->gart_info.dummy.page = alloc_page(GFP_DMA32 | GFP_KERNEL);
442 if (!dev_priv->gart_info.dummy.page)
445 dev_priv->gart_info.dummy.addr =
446 pci_map_page(dev->pdev, dev_priv->gart_info.dummy.page,
447 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
448 if (pci_dma_mapping_error(dev->pdev, dev_priv->gart_info.dummy.addr)) {
449 NV_ERROR(dev, "error mapping dummy page\n");
450 __free_page(dev_priv->gart_info.dummy.page);
451 dev_priv->gart_info.dummy.page = NULL;
455 if (dev_priv->card_type >= NV_50) {
456 dev_priv->gart_info.aper_base = 0;
457 dev_priv->gart_info.aper_size = aper_size;
458 dev_priv->gart_info.type = NOUVEAU_GART_HW;
459 dev_priv->gart_info.func = &nv50_sgdma_backend;
461 if (0 && drm_pci_device_is_pcie(dev) &&
462 dev_priv->chipset > 0x40 && dev_priv->chipset != 0x45) {
463 if (nv44_graph_class(dev)) {
464 dev_priv->gart_info.func = &nv44_sgdma_backend;
467 dev_priv->gart_info.func = &nv41_sgdma_backend;
471 ret = nouveau_gpuobj_new(dev, NULL, aper_size / 1024, align,
472 NVOBJ_FLAG_ZERO_ALLOC |
473 NVOBJ_FLAG_ZERO_FREE, &gpuobj);
475 NV_ERROR(dev, "Error creating sgdma object: %d\n", ret);
479 dev_priv->gart_info.sg_ctxdma = gpuobj;
480 dev_priv->gart_info.aper_base = 0;
481 dev_priv->gart_info.aper_size = aper_size;
482 dev_priv->gart_info.type = NOUVEAU_GART_HW;
484 ret = nouveau_gpuobj_new(dev, NULL, (aper_size / 1024) + 8, 16,
485 NVOBJ_FLAG_ZERO_ALLOC |
486 NVOBJ_FLAG_ZERO_FREE, &gpuobj);
488 NV_ERROR(dev, "Error creating sgdma object: %d\n", ret);
492 nv_wo32(gpuobj, 0, NV_CLASS_DMA_IN_MEMORY |
493 (1 << 12) /* PT present */ |
494 (0 << 13) /* PT *not* linear */ |
496 (2 << 16) /* PCI */);
497 nv_wo32(gpuobj, 4, aper_size - 1);
499 dev_priv->gart_info.sg_ctxdma = gpuobj;
500 dev_priv->gart_info.aper_base = 0;
501 dev_priv->gart_info.aper_size = aper_size;
502 dev_priv->gart_info.type = NOUVEAU_GART_PDMA;
503 dev_priv->gart_info.func = &nv04_sgdma_backend;
510 nouveau_sgdma_takedown(struct drm_device *dev)
512 struct drm_nouveau_private *dev_priv = dev->dev_private;
514 nouveau_gpuobj_ref(NULL, &dev_priv->gart_info.sg_ctxdma);
516 if (dev_priv->gart_info.dummy.page) {
517 pci_unmap_page(dev->pdev, dev_priv->gart_info.dummy.addr,
518 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
519 __free_page(dev_priv->gart_info.dummy.page);
520 dev_priv->gart_info.dummy.page = NULL;
525 nouveau_sgdma_get_physical(struct drm_device *dev, uint32_t offset)
527 struct drm_nouveau_private *dev_priv = dev->dev_private;
528 struct nouveau_gpuobj *gpuobj = dev_priv->gart_info.sg_ctxdma;
529 int pte = (offset >> NV_CTXDMA_PAGE_SHIFT) + 2;
531 BUG_ON(dev_priv->card_type >= NV_50);
533 return (nv_ro32(gpuobj, 4 * pte) & ~NV_CTXDMA_PAGE_MASK) |
534 (offset & NV_CTXDMA_PAGE_MASK);