2 * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
3 * Copyright 2005 Stephane Marchesin
5 * The Weather Channel (TM) funded Tungsten Graphics to develop the
6 * initial release of the Radeon 8500 driver under the XFree86 license.
7 * This notice must be preserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
29 * Keith Whitwell <keith@tungstengraphics.com>
35 #include "drm_sarea.h"
36 #include "nouveau_drv.h"
38 static struct mem_block *
39 split_block(struct mem_block *p, uint64_t start, uint64_t size,
40 struct drm_file *file_priv)
42 /* Maybe cut off the start of an existing block */
43 if (start > p->start) {
44 struct mem_block *newblock =
45 kmalloc(sizeof(*newblock), GFP_KERNEL);
48 newblock->start = start;
49 newblock->size = p->size - (start - p->start);
50 newblock->file_priv = NULL;
51 newblock->next = p->next;
53 p->next->prev = newblock;
55 p->size -= newblock->size;
59 /* Maybe cut off the end of an existing block */
61 struct mem_block *newblock =
62 kmalloc(sizeof(*newblock), GFP_KERNEL);
65 newblock->start = start + size;
66 newblock->size = p->size - size;
67 newblock->file_priv = NULL;
68 newblock->next = p->next;
70 p->next->prev = newblock;
76 /* Our block is in the middle */
77 p->file_priv = file_priv;
82 nouveau_mem_alloc_block(struct mem_block *heap, uint64_t size,
83 int align2, struct drm_file *file_priv, int tail)
86 uint64_t mask = (1 << align2) - 1;
92 list_for_each_prev(p, heap) {
93 uint64_t start = ((p->start + p->size) - size) & ~mask;
95 if (p->file_priv == NULL && start >= p->start &&
96 start + size <= p->start + p->size)
97 return split_block(p, start, size, file_priv);
100 list_for_each(p, heap) {
101 uint64_t start = (p->start + mask) & ~mask;
103 if (p->file_priv == NULL &&
104 start + size <= p->start + p->size)
105 return split_block(p, start, size, file_priv);
112 void nouveau_mem_free_block(struct mem_block *p)
116 /* Assumes a single contiguous range. Needs a special file_priv in
117 * 'heap' to stop it being subsumed.
119 if (p->next->file_priv == NULL) {
120 struct mem_block *q = p->next;
127 if (p->prev->file_priv == NULL) {
128 struct mem_block *q = p->prev;
136 /* Initialize. How to check for an uninitialized heap?
138 int nouveau_mem_init_heap(struct mem_block **heap, uint64_t start,
141 struct mem_block *blocks = kmalloc(sizeof(*blocks), GFP_KERNEL);
146 *heap = kmalloc(sizeof(**heap), GFP_KERNEL);
152 blocks->start = start;
154 blocks->file_priv = NULL;
155 blocks->next = blocks->prev = *heap;
157 memset(*heap, 0, sizeof(**heap));
158 (*heap)->file_priv = (struct drm_file *) -1;
159 (*heap)->next = (*heap)->prev = blocks;
164 * Free all blocks associated with the releasing file_priv
166 void nouveau_mem_release(struct drm_file *file_priv, struct mem_block *heap)
170 if (!heap || !heap->next)
173 list_for_each(p, heap) {
174 if (p->file_priv == file_priv)
178 /* Assumes a single contiguous range. Needs a special file_priv in
179 * 'heap' to stop it being subsumed.
181 list_for_each(p, heap) {
182 while ((p->file_priv == NULL) &&
183 (p->next->file_priv == NULL) &&
185 struct mem_block *q = p->next;
195 * NV10-NV40 tiling helpers
199 nv10_mem_set_region_tiling(struct drm_device *dev, int i, uint32_t addr,
200 uint32_t size, uint32_t pitch)
202 struct drm_nouveau_private *dev_priv = dev->dev_private;
203 struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
204 struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
205 struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
206 struct nouveau_tile_reg *tile = &dev_priv->tile.reg[i];
210 tile->used = !!pitch;
211 nouveau_fence_unref((void **)&tile->fence);
213 if (!pfifo->cache_flush(dev))
216 pfifo->reassign(dev, false);
217 pfifo->cache_flush(dev);
218 pfifo->cache_pull(dev, false);
220 nouveau_wait_for_idle(dev);
222 pgraph->set_region_tiling(dev, i, addr, size, pitch);
223 pfb->set_region_tiling(dev, i, addr, size, pitch);
225 pfifo->cache_pull(dev, true);
226 pfifo->reassign(dev, true);
229 struct nouveau_tile_reg *
230 nv10_mem_set_tiling(struct drm_device *dev, uint32_t addr, uint32_t size,
233 struct drm_nouveau_private *dev_priv = dev->dev_private;
234 struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
235 struct nouveau_tile_reg *tile = dev_priv->tile.reg, *found = NULL;
238 spin_lock(&dev_priv->tile.lock);
240 for (i = 0; i < pfb->num_tiles; i++) {
242 /* Tile region in use. */
246 !nouveau_fence_signalled(tile[i].fence, NULL))
247 /* Pending tile region. */
250 if (max(tile[i].addr, addr) <
251 min(tile[i].addr + tile[i].size, addr + size))
252 /* Kill an intersecting tile region. */
253 nv10_mem_set_region_tiling(dev, i, 0, 0, 0);
255 if (pitch && !found) {
256 /* Free tile region. */
257 nv10_mem_set_region_tiling(dev, i, addr, size, pitch);
262 spin_unlock(&dev_priv->tile.lock);
268 nv10_mem_expire_tiling(struct drm_device *dev, struct nouveau_tile_reg *tile,
269 struct nouveau_fence *fence)
272 /* Mark it as pending. */
274 nouveau_fence_ref(fence);
284 nv50_mem_vm_bind_linear(struct drm_device *dev, uint64_t virt, uint32_t size,
285 uint32_t flags, uint64_t phys)
287 struct drm_nouveau_private *dev_priv = dev->dev_private;
288 struct nouveau_gpuobj *pgt;
292 virt = ((virt - dev_priv->vm_vram_base) >> 16) << 1;
293 size = (size >> 16) << 1;
295 phys |= ((uint64_t)flags << 32);
297 if (dev_priv->vram_sys_base) {
298 phys += dev_priv->vram_sys_base;
302 dev_priv->engine.instmem.prepare_access(dev, true);
304 unsigned offset_h = upper_32_bits(phys);
305 unsigned offset_l = lower_32_bits(phys);
308 for (i = 7; i >= 0; i--) {
309 block = 1 << (i + 1);
310 if (size >= block && !(virt & (block - 1)))
313 offset_l |= (i << 7);
319 pgt = dev_priv->vm_vram_pt[virt >> 14];
325 block -= (end - pte);
329 nv_wo32(dev, pgt, pte++, offset_l);
330 nv_wo32(dev, pgt, pte++, offset_h);
334 dev_priv->engine.instmem.finish_access(dev);
336 nv_wr32(dev, 0x100c80, 0x00050001);
337 if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
338 NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
339 NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
343 nv_wr32(dev, 0x100c80, 0x00000001);
344 if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
345 NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
346 NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
354 nv50_mem_vm_unbind(struct drm_device *dev, uint64_t virt, uint32_t size)
356 struct drm_nouveau_private *dev_priv = dev->dev_private;
357 struct nouveau_gpuobj *pgt;
358 unsigned pages, pte, end;
360 virt -= dev_priv->vm_vram_base;
361 pages = (size >> 16) << 1;
363 dev_priv->engine.instmem.prepare_access(dev, true);
365 pgt = dev_priv->vm_vram_pt[virt >> 29];
366 pte = (virt & 0x1ffe0000ULL) >> 15;
371 pages -= (end - pte);
372 virt += (end - pte) << 15;
375 nv_wo32(dev, pgt, pte++, 0);
377 dev_priv->engine.instmem.finish_access(dev);
379 nv_wr32(dev, 0x100c80, 0x00050001);
380 if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
381 NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
382 NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
386 nv_wr32(dev, 0x100c80, 0x00000001);
387 if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
388 NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
389 NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
396 void nouveau_mem_takedown(struct mem_block **heap)
403 for (p = (*heap)->next; p != *heap;) {
404 struct mem_block *q = p;
413 void nouveau_mem_close(struct drm_device *dev)
415 struct drm_nouveau_private *dev_priv = dev->dev_private;
417 nouveau_bo_unpin(dev_priv->vga_ram);
418 nouveau_bo_ref(NULL, &dev_priv->vga_ram);
420 ttm_bo_device_release(&dev_priv->ttm.bdev);
422 nouveau_ttm_global_release(dev_priv);
424 if (drm_core_has_AGP(dev) && dev->agp &&
425 drm_core_check_feature(dev, DRIVER_MODESET)) {
426 struct drm_agp_mem *entry, *tempe;
428 /* Remove AGP resources, but leave dev->agp
429 intact until drv_cleanup is called. */
430 list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
432 drm_unbind_agp(entry->memory);
433 drm_free_agp(entry->memory, entry->pages);
436 INIT_LIST_HEAD(&dev->agp->memory);
438 if (dev->agp->acquired)
439 drm_agp_release(dev);
441 dev->agp->acquired = 0;
442 dev->agp->enabled = 0;
445 if (dev_priv->fb_mtrr) {
446 drm_mtrr_del(dev_priv->fb_mtrr, drm_get_resource_start(dev, 1),
447 drm_get_resource_len(dev, 1), DRM_MTRR_WC);
448 dev_priv->fb_mtrr = 0;
452 /*XXX won't work on BSD because of pci_read_config_dword */
454 nouveau_mem_fb_amount_igp(struct drm_device *dev)
456 struct drm_nouveau_private *dev_priv = dev->dev_private;
457 struct pci_dev *bridge;
460 bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 1));
462 NV_ERROR(dev, "no bridge device\n");
466 if (dev_priv->flags&NV_NFORCE) {
467 pci_read_config_dword(bridge, 0x7C, &mem);
468 return (uint64_t)(((mem >> 6) & 31) + 1)*1024*1024;
470 if (dev_priv->flags&NV_NFORCE2) {
471 pci_read_config_dword(bridge, 0x84, &mem);
472 return (uint64_t)(((mem >> 4) & 127) + 1)*1024*1024;
475 NV_ERROR(dev, "impossible!\n");
479 /* returns the amount of FB ram in bytes */
480 uint64_t nouveau_mem_fb_amount(struct drm_device *dev)
482 struct drm_nouveau_private *dev_priv = dev->dev_private;
485 switch (dev_priv->card_type) {
487 boot0 = nv_rd32(dev, NV03_BOOT_0);
488 if (boot0 & 0x00000100)
489 return (((boot0 >> 12) & 0xf) * 2 + 2) * 1024 * 1024;
491 switch (boot0 & NV03_BOOT_0_RAM_AMOUNT) {
492 case NV04_BOOT_0_RAM_AMOUNT_32MB:
493 return 32 * 1024 * 1024;
494 case NV04_BOOT_0_RAM_AMOUNT_16MB:
495 return 16 * 1024 * 1024;
496 case NV04_BOOT_0_RAM_AMOUNT_8MB:
497 return 8 * 1024 * 1024;
498 case NV04_BOOT_0_RAM_AMOUNT_4MB:
499 return 4 * 1024 * 1024;
508 if (dev_priv->flags & (NV_NFORCE | NV_NFORCE2)) {
509 return nouveau_mem_fb_amount_igp(dev);
512 mem = (nv_rd32(dev, NV04_FIFO_DATA) &
513 NV10_FIFO_DATA_RAM_AMOUNT_MB_MASK) >>
514 NV10_FIFO_DATA_RAM_AMOUNT_MB_SHIFT;
515 return mem * 1024 * 1024;
521 "Unable to detect video ram size. Please report your setup to "
527 static void nouveau_mem_reset_agp(struct drm_device *dev)
529 uint32_t saved_pci_nv_1, saved_pci_nv_19, pmc_enable;
531 saved_pci_nv_1 = nv_rd32(dev, NV04_PBUS_PCI_NV_1);
532 saved_pci_nv_19 = nv_rd32(dev, NV04_PBUS_PCI_NV_19);
534 /* clear busmaster bit */
535 nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1 & ~0x4);
536 /* clear SBA and AGP bits */
537 nv_wr32(dev, NV04_PBUS_PCI_NV_19, saved_pci_nv_19 & 0xfffff0ff);
539 /* power cycle pgraph, if enabled */
540 pmc_enable = nv_rd32(dev, NV03_PMC_ENABLE);
541 if (pmc_enable & NV_PMC_ENABLE_PGRAPH) {
542 nv_wr32(dev, NV03_PMC_ENABLE,
543 pmc_enable & ~NV_PMC_ENABLE_PGRAPH);
544 nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) |
545 NV_PMC_ENABLE_PGRAPH);
548 /* and restore (gives effect of resetting AGP) */
549 nv_wr32(dev, NV04_PBUS_PCI_NV_19, saved_pci_nv_19);
550 nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1);
555 nouveau_mem_init_agp(struct drm_device *dev)
558 struct drm_nouveau_private *dev_priv = dev->dev_private;
559 struct drm_agp_info info;
560 struct drm_agp_mode mode;
566 nouveau_mem_reset_agp(dev);
568 if (!dev->agp->acquired) {
569 ret = drm_agp_acquire(dev);
571 NV_ERROR(dev, "Unable to acquire AGP: %d\n", ret);
576 ret = drm_agp_info(dev, &info);
578 NV_ERROR(dev, "Unable to get AGP info: %d\n", ret);
582 /* see agp.h for the AGPSTAT_* modes available */
583 mode.mode = info.mode;
584 ret = drm_agp_enable(dev, mode);
586 NV_ERROR(dev, "Unable to enable AGP: %d\n", ret);
590 dev_priv->gart_info.type = NOUVEAU_GART_AGP;
591 dev_priv->gart_info.aper_base = info.aperture_base;
592 dev_priv->gart_info.aper_size = info.aperture_size;
598 nouveau_mem_init(struct drm_device *dev)
600 struct drm_nouveau_private *dev_priv = dev->dev_private;
601 struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
602 int ret, dma_bits = 32;
604 dev_priv->fb_phys = drm_get_resource_start(dev, 1);
605 dev_priv->gart_info.type = NOUVEAU_GART_NONE;
607 if (dev_priv->card_type >= NV_50 &&
608 pci_dma_supported(dev->pdev, DMA_BIT_MASK(40)))
611 ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
613 NV_ERROR(dev, "Error setting DMA mask: %d\n", ret);
617 ret = nouveau_ttm_global_init(dev_priv);
621 ret = ttm_bo_device_init(&dev_priv->ttm.bdev,
622 dev_priv->ttm.bo_global_ref.ref.object,
623 &nouveau_bo_driver, DRM_FILE_PAGE_OFFSET,
624 dma_bits <= 32 ? true : false);
626 NV_ERROR(dev, "Error initialising bo driver: %d\n", ret);
630 INIT_LIST_HEAD(&dev_priv->ttm.bo_list);
631 spin_lock_init(&dev_priv->ttm.bo_list_lock);
632 spin_lock_init(&dev_priv->tile.lock);
634 dev_priv->fb_available_size = nouveau_mem_fb_amount(dev);
636 dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
637 if (dev_priv->fb_mappable_pages > drm_get_resource_len(dev, 1))
638 dev_priv->fb_mappable_pages = drm_get_resource_len(dev, 1);
639 dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
641 NV_INFO(dev, "%d MiB VRAM\n", (int)(dev_priv->fb_available_size >> 20));
643 /* remove reserved space at end of vram from available amount */
644 dev_priv->fb_available_size -= dev_priv->ramin_rsvd_vram;
645 dev_priv->fb_aper_free = dev_priv->fb_available_size;
648 ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
649 dev_priv->fb_available_size >> PAGE_SHIFT);
651 NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
655 ret = nouveau_bo_new(dev, NULL, 256*1024, 0, TTM_PL_FLAG_VRAM,
656 0, 0, true, true, &dev_priv->vga_ram);
658 ret = nouveau_bo_pin(dev_priv->vga_ram, TTM_PL_FLAG_VRAM);
660 NV_WARN(dev, "failed to reserve VGA memory\n");
661 nouveau_bo_ref(NULL, &dev_priv->vga_ram);
665 #if !defined(__powerpc__) && !defined(__ia64__)
666 if (drm_device_is_agp(dev) && dev->agp) {
667 ret = nouveau_mem_init_agp(dev);
669 NV_ERROR(dev, "Error initialising AGP: %d\n", ret);
673 if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
674 ret = nouveau_sgdma_init(dev);
676 NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
681 NV_INFO(dev, "%d MiB GART (aperture)\n",
682 (int)(dev_priv->gart_info.aper_size >> 20));
683 dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
685 ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
686 dev_priv->gart_info.aper_size >> PAGE_SHIFT);
688 NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
692 dev_priv->fb_mtrr = drm_mtrr_add(drm_get_resource_start(dev, 1),
693 drm_get_resource_len(dev, 1),