blob: 3337f2cfb5f676c4cf6dc68c54e49f2ea746f2a0 [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
2 * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
3 * Copyright 2005 Stephane Marchesin
4 *
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.
8 *
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:
15 *
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
18 * Software.
19 *
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.
27 *
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32
33#include "drmP.h"
34#include "drm.h"
35#include "drm_sarea.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100036
Francisco Jerezcbab95d2010-10-11 03:43:58 +020037#include "nouveau_drv.h"
38#include "nouveau_pm.h"
Ben Skeggs573a2a32010-08-25 15:26:04 +100039#include "nouveau_mm.h"
Ben Skeggsa11c3192010-08-27 10:00:25 +100040#include "nouveau_vm.h"
Roy Splieta845fff2010-10-04 23:01:08 +020041
Ben Skeggs6ee73862009-12-11 19:24:15 +100042/*
Francisco Jereza0af9ad2009-12-11 16:51:09 +010043 * NV10-NV40 tiling helpers
44 */
45
46static void
Francisco Jereza5cf68b2010-10-24 16:14:41 +020047nv10_mem_update_tile_region(struct drm_device *dev,
48 struct nouveau_tile_reg *tile, uint32_t addr,
49 uint32_t size, uint32_t pitch, uint32_t flags)
Francisco Jereza0af9ad2009-12-11 16:51:09 +010050{
51 struct drm_nouveau_private *dev_priv = dev->dev_private;
52 struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
53 struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
Ben Skeggs96c50082011-04-01 13:10:45 +100054 int i = tile - dev_priv->tile.reg, j;
Francisco Jereza5cf68b2010-10-24 16:14:41 +020055 unsigned long save;
Francisco Jereza0af9ad2009-12-11 16:51:09 +010056
Marcin Slusarz382d62e2010-10-20 21:50:24 +020057 nouveau_fence_unref(&tile->fence);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010058
Francisco Jereza5cf68b2010-10-24 16:14:41 +020059 if (tile->pitch)
60 pfb->free_tile_region(dev, i);
61
62 if (pitch)
63 pfb->init_tile_region(dev, i, addr, size, pitch, flags);
64
65 spin_lock_irqsave(&dev_priv->context_switch_lock, save);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010066 pfifo->reassign(dev, false);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010067 pfifo->cache_pull(dev, false);
68
69 nouveau_wait_for_idle(dev);
70
Francisco Jereza5cf68b2010-10-24 16:14:41 +020071 pfb->set_tile_region(dev, i);
Ben Skeggs96c50082011-04-01 13:10:45 +100072 for (j = 0; j < NVOBJ_ENGINE_NR; j++) {
73 if (dev_priv->eng[j] && dev_priv->eng[j]->set_tile_region)
74 dev_priv->eng[j]->set_tile_region(dev, i);
75 }
Francisco Jereza0af9ad2009-12-11 16:51:09 +010076
77 pfifo->cache_pull(dev, true);
78 pfifo->reassign(dev, true);
Francisco Jereza5cf68b2010-10-24 16:14:41 +020079 spin_unlock_irqrestore(&dev_priv->context_switch_lock, save);
80}
81
82static struct nouveau_tile_reg *
83nv10_mem_get_tile_region(struct drm_device *dev, int i)
84{
85 struct drm_nouveau_private *dev_priv = dev->dev_private;
86 struct nouveau_tile_reg *tile = &dev_priv->tile.reg[i];
87
88 spin_lock(&dev_priv->tile.lock);
89
90 if (!tile->used &&
91 (!tile->fence || nouveau_fence_signalled(tile->fence)))
92 tile->used = true;
93 else
94 tile = NULL;
95
96 spin_unlock(&dev_priv->tile.lock);
97 return tile;
98}
99
100void
101nv10_mem_put_tile_region(struct drm_device *dev, struct nouveau_tile_reg *tile,
102 struct nouveau_fence *fence)
103{
104 struct drm_nouveau_private *dev_priv = dev->dev_private;
105
106 if (tile) {
107 spin_lock(&dev_priv->tile.lock);
108 if (fence) {
109 /* Mark it as pending. */
110 tile->fence = fence;
111 nouveau_fence_ref(fence);
112 }
113
114 tile->used = false;
115 spin_unlock(&dev_priv->tile.lock);
116 }
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100117}
118
119struct nouveau_tile_reg *
120nv10_mem_set_tiling(struct drm_device *dev, uint32_t addr, uint32_t size,
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200121 uint32_t pitch, uint32_t flags)
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100122{
123 struct drm_nouveau_private *dev_priv = dev->dev_private;
124 struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200125 struct nouveau_tile_reg *tile, *found = NULL;
126 int i;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100127
128 for (i = 0; i < pfb->num_tiles; i++) {
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200129 tile = nv10_mem_get_tile_region(dev, i);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100130
131 if (pitch && !found) {
Francisco Jerez9f56b122010-09-07 18:24:52 +0200132 found = tile;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200133 continue;
134
135 } else if (tile && tile->pitch) {
136 /* Kill an unused tile region. */
137 nv10_mem_update_tile_region(dev, tile, 0, 0, 0, 0);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100138 }
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200139
140 nv10_mem_put_tile_region(dev, tile, NULL);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100141 }
142
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200143 if (found)
144 nv10_mem_update_tile_region(dev, found, addr, size,
145 pitch, flags);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100146 return found;
147}
148
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100149/*
Ben Skeggs6ee73862009-12-11 19:24:15 +1000150 * Cleanup everything
151 */
Ben Skeggsb833ac22010-06-01 15:32:24 +1000152void
Ben Skeggsfbd28952010-09-01 15:24:34 +1000153nouveau_mem_vram_fini(struct drm_device *dev)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000154{
155 struct drm_nouveau_private *dev_priv = dev->dev_private;
156
Ben Skeggs6ee73862009-12-11 19:24:15 +1000157 ttm_bo_device_release(&dev_priv->ttm.bdev);
158
159 nouveau_ttm_global_release(dev_priv);
160
Ben Skeggsfbd28952010-09-01 15:24:34 +1000161 if (dev_priv->fb_mtrr >= 0) {
162 drm_mtrr_del(dev_priv->fb_mtrr,
163 pci_resource_start(dev->pdev, 1),
164 pci_resource_len(dev->pdev, 1), DRM_MTRR_WC);
165 dev_priv->fb_mtrr = -1;
166 }
167}
168
169void
170nouveau_mem_gart_fini(struct drm_device *dev)
171{
172 nouveau_sgdma_takedown(dev);
173
Ben Skeggscd0b0722010-06-01 15:56:22 +1000174 if (drm_core_has_AGP(dev) && dev->agp) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000175 struct drm_agp_mem *entry, *tempe;
176
177 /* Remove AGP resources, but leave dev->agp
178 intact until drv_cleanup is called. */
179 list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
180 if (entry->bound)
181 drm_unbind_agp(entry->memory);
182 drm_free_agp(entry->memory, entry->pages);
183 kfree(entry);
184 }
185 INIT_LIST_HEAD(&dev->agp->memory);
186
187 if (dev->agp->acquired)
188 drm_agp_release(dev);
189
190 dev->agp->acquired = 0;
191 dev->agp->enabled = 0;
192 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000193}
194
Ben Skeggs60d2a882010-12-06 15:28:54 +1000195bool
196nouveau_mem_flags_valid(struct drm_device *dev, u32 tile_flags)
197{
198 if (!(tile_flags & NOUVEAU_GEM_TILE_LAYOUT_MASK))
199 return true;
200
201 return false;
202}
203
Francisco Jerez71d06182010-09-08 02:23:20 +0200204#if __OS_HAS_AGP
205static unsigned long
206get_agp_mode(struct drm_device *dev, unsigned long mode)
207{
208 struct drm_nouveau_private *dev_priv = dev->dev_private;
209
210 /*
211 * FW seems to be broken on nv18, it makes the card lock up
212 * randomly.
213 */
214 if (dev_priv->chipset == 0x18)
215 mode &= ~PCI_AGP_COMMAND_FW;
216
Francisco Jerezde5899b2010-09-08 02:28:23 +0200217 /*
218 * AGP mode set in the command line.
219 */
220 if (nouveau_agpmode > 0) {
221 bool agpv3 = mode & 0x8;
222 int rate = agpv3 ? nouveau_agpmode / 4 : nouveau_agpmode;
223
224 mode = (mode & ~0x7) | (rate & 0x7);
225 }
226
Francisco Jerez71d06182010-09-08 02:23:20 +0200227 return mode;
228}
229#endif
230
Francisco Jereze04d8e82010-07-23 20:29:13 +0200231int
232nouveau_mem_reset_agp(struct drm_device *dev)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000233{
Francisco Jereze04d8e82010-07-23 20:29:13 +0200234#if __OS_HAS_AGP
235 uint32_t saved_pci_nv_1, pmc_enable;
236 int ret;
237
238 /* First of all, disable fast writes, otherwise if it's
239 * already enabled in the AGP bridge and we disable the card's
240 * AGP controller we might be locking ourselves out of it. */
Francisco Jerez316f60a2010-08-26 16:13:49 +0200241 if ((nv_rd32(dev, NV04_PBUS_PCI_NV_19) |
242 dev->agp->mode) & PCI_AGP_COMMAND_FW) {
Francisco Jereze04d8e82010-07-23 20:29:13 +0200243 struct drm_agp_info info;
244 struct drm_agp_mode mode;
245
246 ret = drm_agp_info(dev, &info);
247 if (ret)
248 return ret;
249
Francisco Jerez71d06182010-09-08 02:23:20 +0200250 mode.mode = get_agp_mode(dev, info.mode) & ~PCI_AGP_COMMAND_FW;
Francisco Jereze04d8e82010-07-23 20:29:13 +0200251 ret = drm_agp_enable(dev, mode);
252 if (ret)
253 return ret;
254 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000255
256 saved_pci_nv_1 = nv_rd32(dev, NV04_PBUS_PCI_NV_1);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000257
258 /* clear busmaster bit */
259 nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1 & ~0x4);
Francisco Jereze04d8e82010-07-23 20:29:13 +0200260 /* disable AGP */
261 nv_wr32(dev, NV04_PBUS_PCI_NV_19, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000262
263 /* power cycle pgraph, if enabled */
264 pmc_enable = nv_rd32(dev, NV03_PMC_ENABLE);
265 if (pmc_enable & NV_PMC_ENABLE_PGRAPH) {
266 nv_wr32(dev, NV03_PMC_ENABLE,
267 pmc_enable & ~NV_PMC_ENABLE_PGRAPH);
268 nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) |
269 NV_PMC_ENABLE_PGRAPH);
270 }
271
272 /* and restore (gives effect of resetting AGP) */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000273 nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1);
Ben Skeggsb694dfb2009-12-15 10:38:32 +1000274#endif
Ben Skeggs6ee73862009-12-11 19:24:15 +1000275
Francisco Jereze04d8e82010-07-23 20:29:13 +0200276 return 0;
277}
278
Ben Skeggs6ee73862009-12-11 19:24:15 +1000279int
280nouveau_mem_init_agp(struct drm_device *dev)
281{
Ben Skeggsb694dfb2009-12-15 10:38:32 +1000282#if __OS_HAS_AGP
Ben Skeggs6ee73862009-12-11 19:24:15 +1000283 struct drm_nouveau_private *dev_priv = dev->dev_private;
284 struct drm_agp_info info;
285 struct drm_agp_mode mode;
286 int ret;
287
Ben Skeggs6ee73862009-12-11 19:24:15 +1000288 if (!dev->agp->acquired) {
289 ret = drm_agp_acquire(dev);
290 if (ret) {
291 NV_ERROR(dev, "Unable to acquire AGP: %d\n", ret);
292 return ret;
293 }
294 }
295
Francisco Jerez2b495262010-07-30 13:57:54 +0200296 nouveau_mem_reset_agp(dev);
297
Ben Skeggs6ee73862009-12-11 19:24:15 +1000298 ret = drm_agp_info(dev, &info);
299 if (ret) {
300 NV_ERROR(dev, "Unable to get AGP info: %d\n", ret);
301 return ret;
302 }
303
304 /* see agp.h for the AGPSTAT_* modes available */
Francisco Jerez71d06182010-09-08 02:23:20 +0200305 mode.mode = get_agp_mode(dev, info.mode);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000306 ret = drm_agp_enable(dev, mode);
307 if (ret) {
308 NV_ERROR(dev, "Unable to enable AGP: %d\n", ret);
309 return ret;
310 }
311
312 dev_priv->gart_info.type = NOUVEAU_GART_AGP;
313 dev_priv->gart_info.aper_base = info.aperture_base;
314 dev_priv->gart_info.aper_size = info.aperture_size;
Ben Skeggsb694dfb2009-12-15 10:38:32 +1000315#endif
Ben Skeggs6ee73862009-12-11 19:24:15 +1000316 return 0;
317}
318
Ben Skeggs7ad2d312011-12-11 00:30:05 +1000319static const struct vram_types {
320 int value;
321 const char *name;
322} vram_type_map[] = {
323 { NV_MEM_TYPE_STOLEN , "stolen system memory" },
324 { NV_MEM_TYPE_SGRAM , "SGRAM" },
325 { NV_MEM_TYPE_SDRAM , "SDRAM" },
326 { NV_MEM_TYPE_DDR1 , "DDR1" },
327 { NV_MEM_TYPE_DDR2 , "DDR2" },
328 { NV_MEM_TYPE_DDR3 , "DDR3" },
329 { NV_MEM_TYPE_GDDR2 , "GDDR2" },
330 { NV_MEM_TYPE_GDDR3 , "GDDR3" },
331 { NV_MEM_TYPE_GDDR4 , "GDDR4" },
332 { NV_MEM_TYPE_GDDR5 , "GDDR5" },
333 { NV_MEM_TYPE_UNKNOWN, "unknown type" }
334};
335
Ben Skeggs6ee73862009-12-11 19:24:15 +1000336int
Ben Skeggsfbd28952010-09-01 15:24:34 +1000337nouveau_mem_vram_init(struct drm_device *dev)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000338{
339 struct drm_nouveau_private *dev_priv = dev->dev_private;
340 struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
Ben Skeggs7ad2d312011-12-11 00:30:05 +1000341 const struct vram_types *vram_type;
Ben Skeggsfbd28952010-09-01 15:24:34 +1000342 int ret, dma_bits;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000343
Ben Skeggse0435122011-01-11 15:50:26 +1000344 dma_bits = 32;
345 if (dev_priv->card_type >= NV_50) {
346 if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(40)))
347 dma_bits = 40;
348 } else
Jon Mason58b65422011-06-27 16:07:50 +0000349 if (0 && pci_is_pcie(dev->pdev) &&
Ben Skeggs01d15332011-04-08 10:07:34 +1000350 dev_priv->chipset > 0x40 &&
Ben Skeggse0435122011-01-11 15:50:26 +1000351 dev_priv->chipset != 0x45) {
352 if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(39)))
353 dma_bits = 39;
354 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000355
356 ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
Ben Skeggsfbd28952010-09-01 15:24:34 +1000357 if (ret)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000358 return ret;
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -0400359 ret = pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
360 if (ret) {
361 /* Reset to default value. */
362 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32));
363 }
364
Ben Skeggsfbd28952010-09-01 15:24:34 +1000365
Ben Skeggs6ee73862009-12-11 19:24:15 +1000366 ret = nouveau_ttm_global_init(dev_priv);
367 if (ret)
368 return ret;
369
370 ret = ttm_bo_device_init(&dev_priv->ttm.bdev,
371 dev_priv->ttm.bo_global_ref.ref.object,
372 &nouveau_bo_driver, DRM_FILE_PAGE_OFFSET,
373 dma_bits <= 32 ? true : false);
374 if (ret) {
375 NV_ERROR(dev, "Error initialising bo driver: %d\n", ret);
376 return ret;
377 }
378
Ben Skeggs7ad2d312011-12-11 00:30:05 +1000379 vram_type = vram_type_map;
380 while (vram_type->value != NV_MEM_TYPE_UNKNOWN) {
381 if (nouveau_vram_type) {
382 if (!strcasecmp(nouveau_vram_type, vram_type->name))
383 break;
384 dev_priv->vram_type = vram_type->value;
385 } else {
386 if (vram_type->value == dev_priv->vram_type)
387 break;
388 }
389 vram_type++;
390 }
391
392 NV_INFO(dev, "Detected %dMiB VRAM (%s)\n",
393 (int)(dev_priv->vram_size >> 20), vram_type->name);
Ben Skeggs60d2a882010-12-06 15:28:54 +1000394 if (dev_priv->vram_sys_base) {
395 NV_INFO(dev, "Stolen system memory at: 0x%010llx\n",
396 dev_priv->vram_sys_base);
397 }
398
Ben Skeggs573a2a32010-08-25 15:26:04 +1000399 dev_priv->fb_available_size = dev_priv->vram_size;
400 dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
401 if (dev_priv->fb_mappable_pages > pci_resource_len(dev->pdev, 1))
402 dev_priv->fb_mappable_pages = pci_resource_len(dev->pdev, 1);
403 dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
404
Ben Skeggs6ee73862009-12-11 19:24:15 +1000405 dev_priv->fb_available_size -= dev_priv->ramin_rsvd_vram;
406 dev_priv->fb_aper_free = dev_priv->fb_available_size;
407
408 /* mappable vram */
409 ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
410 dev_priv->fb_available_size >> PAGE_SHIFT);
411 if (ret) {
412 NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
413 return ret;
414 }
415
Ben Skeggsd550c412011-02-16 08:41:56 +1000416 if (dev_priv->card_type < NV_50) {
Ben Skeggs7375c952011-06-07 14:21:29 +1000417 ret = nouveau_bo_new(dev, 256*1024, 0, TTM_PL_FLAG_VRAM,
Ben Skeggsd550c412011-02-16 08:41:56 +1000418 0, 0, &dev_priv->vga_ram);
419 if (ret == 0)
420 ret = nouveau_bo_pin(dev_priv->vga_ram,
421 TTM_PL_FLAG_VRAM);
422
423 if (ret) {
424 NV_WARN(dev, "failed to reserve VGA memory\n");
425 nouveau_bo_ref(NULL, &dev_priv->vga_ram);
426 }
Ben Skeggsac8fb972010-01-15 09:24:20 +1000427 }
428
Ben Skeggsfbd28952010-09-01 15:24:34 +1000429 dev_priv->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 1),
430 pci_resource_len(dev->pdev, 1),
431 DRM_MTRR_WC);
432 return 0;
433}
434
435int
436nouveau_mem_gart_init(struct drm_device *dev)
437{
438 struct drm_nouveau_private *dev_priv = dev->dev_private;
439 struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
440 int ret;
441
442 dev_priv->gart_info.type = NOUVEAU_GART_NONE;
443
Ben Skeggs6ee73862009-12-11 19:24:15 +1000444#if !defined(__powerpc__) && !defined(__ia64__)
Dave Airlie8410ea32010-12-15 03:16:38 +1000445 if (drm_pci_device_is_agp(dev) && dev->agp && nouveau_agpmode) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000446 ret = nouveau_mem_init_agp(dev);
447 if (ret)
448 NV_ERROR(dev, "Error initialising AGP: %d\n", ret);
449 }
450#endif
451
452 if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
453 ret = nouveau_sgdma_init(dev);
454 if (ret) {
455 NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
456 return ret;
457 }
458 }
459
460 NV_INFO(dev, "%d MiB GART (aperture)\n",
461 (int)(dev_priv->gart_info.aper_size >> 20));
462 dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
463
464 ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
465 dev_priv->gart_info.aper_size >> PAGE_SHIFT);
466 if (ret) {
467 NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
468 return ret;
469 }
470
Ben Skeggs6ee73862009-12-11 19:24:15 +1000471 return 0;
472}
473
Roy Spliet9a782482011-07-09 21:18:11 +0200474/* XXX: For now a dummy. More samples required, possibly even a card
475 * Called from nouveau_perf.c */
Martin Peresddb20052011-12-17 12:24:59 +0100476void nv30_mem_timing_entry(struct drm_device *dev,
477 struct nouveau_pm_tbl_header *hdr,
478 struct nouveau_pm_tbl_entry *e, uint8_t magic_number,
479 struct nouveau_pm_memtiming *timing)
480{
Roy Spliet9a782482011-07-09 21:18:11 +0200481
Martin Peresddb20052011-12-17 12:24:59 +0100482 NV_DEBUG(dev, "Timing entry format unknown, "
483 "please contact nouveau developers");
Roy Spliet9a782482011-07-09 21:18:11 +0200484}
485
Martin Peresddb20052011-12-17 12:24:59 +0100486void nv40_mem_timing_entry(struct drm_device *dev,
487 struct nouveau_pm_tbl_header *hdr,
Roy Splietbfb31462011-11-25 15:52:22 +0100488 struct nouveau_pm_tbl_entry *e,
Martin Peresddb20052011-12-17 12:24:59 +0100489 struct nouveau_pm_memtiming *timing)
490{
Roy Spliet9a782482011-07-09 21:18:11 +0200491
Roy Splietbfb31462011-11-25 15:52:22 +0100492 timing->reg_0 = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
Roy Spliet9a782482011-07-09 21:18:11 +0200493
494 /* XXX: I don't trust the -1's and +1's... they must come
495 * from somewhere! */
Roy Splietbfb31462011-11-25 15:52:22 +0100496 timing->reg_1 = (e->tWR + 2 + (timing->tCWL - 1)) << 24 |
497 1 << 16 |
498 (e->tWTR + 2 + (timing->tCWL - 1)) << 8 |
499 (e->tCL + 2 - (timing->tCWL - 1));
500
501 timing->reg_2 = 0x20200000 | ((timing->tCWL - 1) << 24 |
502 e->tRRD << 16 | e->tRCDWR << 8 | e->tRCDRD);
Roy Spliet9a782482011-07-09 21:18:11 +0200503
504 NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x\n", timing->id,
Martin Peresddb20052011-12-17 12:24:59 +0100505 timing->reg_0, timing->reg_1, timing->reg_2);
Roy Spliet9a782482011-07-09 21:18:11 +0200506}
507
Martin Peresddb20052011-12-17 12:24:59 +0100508void nv50_mem_timing_entry(struct drm_device *dev, struct bit_entry *P,
509 struct nouveau_pm_tbl_header *hdr,
Roy Splietbfb31462011-11-25 15:52:22 +0100510 struct nouveau_pm_tbl_entry *e,
Martin Peresddb20052011-12-17 12:24:59 +0100511 struct nouveau_pm_memtiming *timing)
512{
Roy Spliet9a782482011-07-09 21:18:11 +0200513 struct drm_nouveau_private *dev_priv = dev->dev_private;
Roy Spliet9a782482011-07-09 21:18:11 +0200514 uint8_t unk18 = 1,
Roy Spliet9a782482011-07-09 21:18:11 +0200515 unk20 = 0,
Roy Splietbfb31462011-11-25 15:52:22 +0100516 unk21 = 0,
517 tmp7_3;
Roy Spliet9a782482011-07-09 21:18:11 +0200518
519 switch (min(hdr->entry_len, (u8) 22)) {
520 case 22:
521 unk21 = e->tUNK_21;
522 case 21:
523 unk20 = e->tUNK_20;
524 case 20:
Roy Splietbfb31462011-11-25 15:52:22 +0100525 if (e->tCWL > 0)
526 timing->tCWL = e->tCWL;
Roy Spliet9a782482011-07-09 21:18:11 +0200527 case 19:
528 unk18 = e->tUNK_18;
529 break;
530 }
531
Roy Splietbfb31462011-11-25 15:52:22 +0100532 timing->reg_0 = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
Roy Spliet9a782482011-07-09 21:18:11 +0200533
Roy Splietbfb31462011-11-25 15:52:22 +0100534 timing->reg_1 = (e->tWR + 2 + (timing->tCWL - 1)) << 24 |
535 max(unk18, (u8) 1) << 16 |
536 (e->tWTR + 2 + (timing->tCWL - 1)) << 8;
Martin Peresddb20052011-12-17 12:24:59 +0100537
Roy Splietbfb31462011-11-25 15:52:22 +0100538 timing->reg_2 = ((timing->tCWL - 1) << 24 | e->tRRD << 16 |
539 e->tRCDWR << 8 | e->tRCDRD);
Roy Spliet9a782482011-07-09 21:18:11 +0200540
Roy Splietbfb31462011-11-25 15:52:22 +0100541 timing->reg_4 = e->tUNK_13 << 8 | e->tUNK_13;
542
543 timing->reg_5 = (e->tRFC << 24 | max(e->tRCDRD, e->tRCDWR) << 16 |
544 e->tRP);
545
546 timing->reg_8 = (nv_rd32(dev, 0x100240) & 0xffffff00);
Roy Spliet9a782482011-07-09 21:18:11 +0200547
548 if (P->version == 1) {
Roy Splietbfb31462011-11-25 15:52:22 +0100549 timing->reg_1 |= (e->tCL + 2 - (timing->tCWL - 1));
Martin Peresddb20052011-12-17 12:24:59 +0100550
Roy Spliet2228c6f2011-07-14 20:40:10 +0200551 timing->reg_3 = (0x14 + e->tCL) << 24 |
Martin Peresddb20052011-12-17 12:24:59 +0100552 0x16 << 16 |
553 (e->tCL - 1) << 8 |
554 (e->tCL - 1);
555
Roy Splietbfb31462011-11-25 15:52:22 +0100556 timing->reg_4 |= (nv_rd32(dev, 0x100230) & 0xffff0000);
Martin Peresddb20052011-12-17 12:24:59 +0100557
Roy Splietbfb31462011-11-25 15:52:22 +0100558 timing->reg_6 = (0x33 - timing->tCWL) << 16 |
559 timing->tCWL << 8 |
560 (0x2E + e->tCL - timing->tCWL);
Martin Peresddb20052011-12-17 12:24:59 +0100561
Roy Spliet2228c6f2011-07-14 20:40:10 +0200562 timing->reg_7 = 0x4000202 | (e->tCL - 1) << 16;
Roy Spliet9a782482011-07-09 21:18:11 +0200563
Roy Splietbfb31462011-11-25 15:52:22 +0100564 /* XXX: P.version == 1 only has DDR2 and GDDR3? */
565 if (dev_priv->vram_type == NV_MEM_TYPE_DDR2) {
566 timing->reg_5 |= (e->tCL + 3) << 8;
567 timing->reg_6 |= (timing->tCWL - 2) << 8;
568 timing->reg_8 |= (e->tCL - 4);
569 } else {
570 timing->reg_5 |= (e->tCL + 2) << 8;
571 timing->reg_6 |= timing->tCWL << 8;
572 timing->reg_8 |= (e->tCL - 2);
573 }
574 } else {
575 timing->reg_1 |= (5 + e->tCL - (timing->tCWL));
576
577 /* XXX: 0xb? 0x30? */
578 timing->reg_3 = (0x30 + e->tCL) << 24 |
579 (nv_rd32(dev, 0x10022c) & 0x00ff0000) |
580 (0xB + e->tCL) << 8 |
581 (e->tCL - 1);
582
583 timing->reg_4 |= (unk20 << 24 | unk21 << 16);
584
585 /* XXX: +6? */
586 timing->reg_5 |= (timing->tCWL + 6) << 8;
587
588 timing->reg_6 = (0x5A + e->tCL) << 16 |
589 (6 - e->tCL + timing->tCWL) << 8 |
590 (0x50 + e->tCL - timing->tCWL);
591
592 tmp7_3 = (nv_rd32(dev, 0x10023c) & 0xff000000) >> 24;
593 timing->reg_7 = (tmp7_3 << 24) |
594 ((tmp7_3 - 6 + e->tCL) << 16) |
595 0x202;
Roy Spliet9a782482011-07-09 21:18:11 +0200596 }
597
598 NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x %08x\n", timing->id,
599 timing->reg_0, timing->reg_1,
600 timing->reg_2, timing->reg_3);
601 NV_DEBUG(dev, " 230: %08x %08x %08x %08x\n",
602 timing->reg_4, timing->reg_5,
603 timing->reg_6, timing->reg_7);
604 NV_DEBUG(dev, " 240: %08x\n", timing->reg_8);
605}
606
Martin Peresddb20052011-12-17 12:24:59 +0100607void nvc0_mem_timing_entry(struct drm_device *dev,
608 struct nouveau_pm_tbl_header *hdr,
609 struct nouveau_pm_tbl_entry *e,
610 struct nouveau_pm_memtiming *timing)
611{
Roy Splietbfb31462011-11-25 15:52:22 +0100612 timing->tCWL = e->tCWL;
613
614 timing->reg_0 = (e->tRP << 24 | (e->tRAS & 0x7f) << 17 |
615 e->tRFC << 8 | e->tRC);
Martin Peresddb20052011-12-17 12:24:59 +0100616
617 timing->reg_1 = (nv_rd32(dev, 0x10f294) & 0xff000000) |
Roy Splietbfb31462011-11-25 15:52:22 +0100618 (e->tRCDWR & 0x0f) << 20 |
619 (e->tRCDRD & 0x0f) << 14 |
620 (e->tCWL << 7) |
Martin Peresddb20052011-12-17 12:24:59 +0100621 (e->tCL & 0x0f);
622
623 timing->reg_2 = (nv_rd32(dev, 0x10f298) & 0xff0000ff) |
Roy Splietbfb31462011-11-25 15:52:22 +0100624 e->tWR << 16 | e->tWTR << 8;
Martin Peresddb20052011-12-17 12:24:59 +0100625
Roy Splietbfb31462011-11-25 15:52:22 +0100626 timing->reg_3 = (e->tUNK_20&0xf) << 9 |
627 (e->tUNK_21 & 0xf) << 5 |
628 (e->tUNK_13 & 0x1f);
Martin Peresddb20052011-12-17 12:24:59 +0100629
Roy Splietbfb31462011-11-25 15:52:22 +0100630 timing->reg_4 = (nv_rd32(dev, 0x10f2a0) & 0xfff00fff) |
631 (e->tRRD&0x1f) << 15;
Martin Peresddb20052011-12-17 12:24:59 +0100632
Roy Spliet9a782482011-07-09 21:18:11 +0200633 NV_DEBUG(dev, "Entry %d: 290: %08x %08x %08x %08x\n", timing->id,
634 timing->reg_0, timing->reg_1,
635 timing->reg_2, timing->reg_3);
Roy Splietbfb31462011-11-25 15:52:22 +0100636 NV_DEBUG(dev, " 2a0: %08x\n",
637 timing->reg_4);
638}
639
640void
641nouveau_mem_features_entry(uint8_t p_version, struct nouveau_pm_tbl_header *hdr,
642 struct nouveau_pm_tbl_entry *e,
643 struct nouveau_pm_memtiming *timing)
644{
645 if (p_version == 1) {
646 /* XXX: Todo */
647 } else if (p_version == 2) {
648 timing->odt = e->RAM_FT1 & 0x1;
649 timing->dll_disable = (e->RAM_FT1 & 0x2) >> 1;
650 timing->ron_pull = (e->RAM_FT1 & 0x4) >> 2;
651 }
Roy Spliet9a782482011-07-09 21:18:11 +0200652}
653
654/**
655 * Processes the Memory Timing BIOS table, stores generated
656 * register values
657 * @pre init scripts were run, memtiming regs are initialized
658 */
Roy Spliet7760fcb2010-09-17 23:17:24 +0200659void
660nouveau_mem_timing_init(struct drm_device *dev)
661{
662 struct drm_nouveau_private *dev_priv = dev->dev_private;
663 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
664 struct nouveau_pm_memtimings *memtimings = &pm->memtimings;
665 struct nvbios *bios = &dev_priv->vbios;
666 struct bit_entry P;
Roy Spliet9a782482011-07-09 21:18:11 +0200667 struct nouveau_pm_tbl_header *hdr = NULL;
Roy Splietbfb31462011-11-25 15:52:22 +0100668 uint8_t tCWL;
Roy Spliet9a782482011-07-09 21:18:11 +0200669 u8 *entry;
670 int i;
Roy Spliet7760fcb2010-09-17 23:17:24 +0200671
672 if (bios->type == NVBIOS_BIT) {
673 if (bit_table(dev, 'P', &P))
674 return;
675
676 if (P.version == 1)
Martin Peresddb20052011-12-17 12:24:59 +0100677 hdr = (struct nouveau_pm_tbl_header *) ROMPTR(dev,
678 P.data[4]);
679 else if (P.version == 2)
680 hdr = (struct nouveau_pm_tbl_header *) ROMPTR(dev,
681 P.data[8]);
Roy Spliet7760fcb2010-09-17 23:17:24 +0200682 else
Roy Spliet7760fcb2010-09-17 23:17:24 +0200683 NV_WARN(dev, "unknown mem for BIT P %d\n", P.version);
Roy Spliet7760fcb2010-09-17 23:17:24 +0200684 } else {
685 NV_DEBUG(dev, "BMP version too old for memory\n");
686 return;
687 }
688
Roy Spliet9a782482011-07-09 21:18:11 +0200689 if (!hdr) {
Roy Spliet7760fcb2010-09-17 23:17:24 +0200690 NV_DEBUG(dev, "memory timing table pointer invalid\n");
691 return;
692 }
693
Roy Spliet9a782482011-07-09 21:18:11 +0200694 if (hdr->version != 0x10) {
Martin Peresddb20052011-12-17 12:24:59 +0100695 NV_WARN(dev, "memory timing table 0x%02x unknown\n",
696 hdr->version);
Roy Spliet7760fcb2010-09-17 23:17:24 +0200697 return;
698 }
699
700 /* validate record length */
Roy Spliet9a782482011-07-09 21:18:11 +0200701 if (hdr->entry_len < 15) {
Martin Peresddb20052011-12-17 12:24:59 +0100702 NV_ERROR(dev, "mem timing table length unknown: %d\n",
703 hdr->entry_len);
Roy Spliet7760fcb2010-09-17 23:17:24 +0200704 return;
705 }
706
707 /* parse vbios entries into common format */
Martin Peresddb20052011-12-17 12:24:59 +0100708 memtimings->timing = kcalloc(hdr->entry_cnt,
709 sizeof(*memtimings->timing), GFP_KERNEL);
Roy Spliet7760fcb2010-09-17 23:17:24 +0200710 if (!memtimings->timing)
711 return;
712
Roy Splietbfb31462011-11-25 15:52:22 +0100713 /* Get tCWL from the timing reg for NV_40 and NV_50
Roy Spliet9a782482011-07-09 21:18:11 +0200714 * Used in calculations later... source unknown */
Roy Splietbfb31462011-11-25 15:52:22 +0100715 tCWL = 0;
716 if (dev_priv->card_type < NV_C0)
717 tCWL = ((nv_rd32(dev, 0x100228) & 0x0f000000) >> 24) + 1;
Roy Splietac5c15f2011-02-09 14:56:42 +0100718
Martin Peresddb20052011-12-17 12:24:59 +0100719 entry = (u8 *) hdr + hdr->header_len;
Roy Spliet9a782482011-07-09 21:18:11 +0200720 for (i = 0; i < hdr->entry_cnt; i++, entry += hdr->entry_len) {
Roy Spliet7760fcb2010-09-17 23:17:24 +0200721 struct nouveau_pm_memtiming *timing = &pm->memtimings.timing[i];
Martin Peresddb20052011-12-17 12:24:59 +0100722 struct nouveau_pm_tbl_entry *entry_struct =
723 (struct nouveau_pm_tbl_entry *) entry;
Roy Spliet7760fcb2010-09-17 23:17:24 +0200724 if (entry[0] == 0)
725 continue;
726
Martin Perese614b2e2011-04-14 00:46:19 +0200727 timing->id = i;
Roy Spliet2228c6f2011-07-14 20:40:10 +0200728 timing->WR = entry[0];
729 timing->CL = entry[2];
Roy Splietbfb31462011-11-25 15:52:22 +0100730 timing->tCWL = tCWL;
731
732 nouveau_mem_features_entry(P.version, hdr, entry_struct,
733 &pm->memtimings.timing[i]);
Roy Spliet7760fcb2010-09-17 23:17:24 +0200734
Martin Peresddb20052011-12-17 12:24:59 +0100735 if (dev_priv->card_type <= NV_40) {
736 nv40_mem_timing_entry(dev, hdr, entry_struct,
Martin Peresddb20052011-12-17 12:24:59 +0100737 &pm->memtimings.timing[i]);
738 } else if (dev_priv->card_type == NV_50) {
739 nv50_mem_timing_entry(dev, &P, hdr, entry_struct,
Martin Peresddb20052011-12-17 12:24:59 +0100740 &pm->memtimings.timing[i]);
741 } else if (dev_priv->card_type == NV_C0) {
742 nvc0_mem_timing_entry(dev, hdr, entry_struct,
743 &pm->memtimings.timing[i]);
Roy Spliet9a782482011-07-09 21:18:11 +0200744 }
Roy Spliet7760fcb2010-09-17 23:17:24 +0200745 }
746
Roy Spliet9a782482011-07-09 21:18:11 +0200747 memtimings->nr_timing = hdr->entry_cnt;
Martin Peresddb20052011-12-17 12:24:59 +0100748 memtimings->supported = (P.version == 1);
Roy Spliet7760fcb2010-09-17 23:17:24 +0200749}
750
751void
752nouveau_mem_timing_fini(struct drm_device *dev)
753{
754 struct drm_nouveau_private *dev_priv = dev->dev_private;
755 struct nouveau_pm_memtimings *mem = &dev_priv->engine.pm.memtimings;
756
Martin Peresddb20052011-12-17 12:24:59 +0100757 kfree(mem->timing);
758 mem->timing = NULL;
Roy Spliet7760fcb2010-09-17 23:17:24 +0200759}
Ben Skeggs573a2a32010-08-25 15:26:04 +1000760
Ben Skeggsc70c41e2011-12-13 11:57:55 +1000761int
762nouveau_mem_vbios_type(struct drm_device *dev)
763{
764 struct bit_entry M;
765 u8 ramcfg = (nv_rd32(dev, 0x101000) & 0x0000003c) >> 2;
766 if (!bit_table(dev, 'M', &M) || M.version != 2 || M.length < 5) {
767 u8 *table = ROMPTR(dev, M.data[3]);
768 if (table && table[0] == 0x10 && ramcfg < table[3]) {
769 u8 *entry = table + table[1] + (ramcfg * table[2]);
770 switch (entry[0] & 0x0f) {
771 case 0: return NV_MEM_TYPE_DDR2;
772 case 1: return NV_MEM_TYPE_DDR3;
773 case 2: return NV_MEM_TYPE_GDDR3;
774 case 3: return NV_MEM_TYPE_GDDR5;
775 default:
776 break;
777 }
778
779 }
780 }
781 return NV_MEM_TYPE_UNKNOWN;
782}
783
Ben Skeggs573a2a32010-08-25 15:26:04 +1000784static int
Ben Skeggs24f246a2011-06-10 13:36:08 +1000785nouveau_vram_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
Ben Skeggs573a2a32010-08-25 15:26:04 +1000786{
Ben Skeggs24f246a2011-06-10 13:36:08 +1000787 /* nothing to do */
Ben Skeggs573a2a32010-08-25 15:26:04 +1000788 return 0;
789}
790
791static int
792nouveau_vram_manager_fini(struct ttm_mem_type_manager *man)
793{
Ben Skeggs24f246a2011-06-10 13:36:08 +1000794 /* nothing to do */
Ben Skeggs573a2a32010-08-25 15:26:04 +1000795 return 0;
796}
797
Ben Skeggsd2f966662011-06-06 20:54:42 +1000798static inline void
799nouveau_mem_node_cleanup(struct nouveau_mem *node)
800{
801 if (node->vma[0].node) {
802 nouveau_vm_unmap(&node->vma[0]);
803 nouveau_vm_put(&node->vma[0]);
804 }
805
806 if (node->vma[1].node) {
807 nouveau_vm_unmap(&node->vma[1]);
808 nouveau_vm_put(&node->vma[1]);
809 }
810}
811
Ben Skeggs573a2a32010-08-25 15:26:04 +1000812static void
813nouveau_vram_manager_del(struct ttm_mem_type_manager *man,
814 struct ttm_mem_reg *mem)
815{
816 struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
Ben Skeggs60d2a882010-12-06 15:28:54 +1000817 struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
Ben Skeggs573a2a32010-08-25 15:26:04 +1000818 struct drm_device *dev = dev_priv->dev;
819
Ben Skeggsd2f966662011-06-06 20:54:42 +1000820 nouveau_mem_node_cleanup(mem->mm_node);
Ben Skeggsd5f42392011-02-10 12:22:52 +1000821 vram->put(dev, (struct nouveau_mem **)&mem->mm_node);
Ben Skeggs573a2a32010-08-25 15:26:04 +1000822}
823
824static int
825nouveau_vram_manager_new(struct ttm_mem_type_manager *man,
826 struct ttm_buffer_object *bo,
827 struct ttm_placement *placement,
828 struct ttm_mem_reg *mem)
829{
830 struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
Ben Skeggs60d2a882010-12-06 15:28:54 +1000831 struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
Ben Skeggs573a2a32010-08-25 15:26:04 +1000832 struct drm_device *dev = dev_priv->dev;
833 struct nouveau_bo *nvbo = nouveau_bo(bo);
Ben Skeggsd5f42392011-02-10 12:22:52 +1000834 struct nouveau_mem *node;
Ben Skeggs5f6fdca2010-11-12 15:13:59 +1000835 u32 size_nc = 0;
Ben Skeggs573a2a32010-08-25 15:26:04 +1000836 int ret;
837
Ben Skeggs5f6fdca2010-11-12 15:13:59 +1000838 if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG)
Ben Skeggsf91bac52011-06-06 14:15:46 +1000839 size_nc = 1 << nvbo->page_shift;
Ben Skeggs5f6fdca2010-11-12 15:13:59 +1000840
Ben Skeggs60d2a882010-12-06 15:28:54 +1000841 ret = vram->get(dev, mem->num_pages << PAGE_SHIFT,
842 mem->page_alignment << PAGE_SHIFT, size_nc,
Ben Skeggs8f7286f2011-02-14 09:57:35 +1000843 (nvbo->tile_flags >> 8) & 0x3ff, &node);
Ben Skeggsef1b2872011-03-07 17:18:03 +1000844 if (ret) {
845 mem->mm_node = NULL;
846 return (ret == -ENOSPC) ? 0 : ret;
847 }
Ben Skeggs573a2a32010-08-25 15:26:04 +1000848
Ben Skeggsf91bac52011-06-06 14:15:46 +1000849 node->page_shift = nvbo->page_shift;
Ben Skeggs4c74eb72010-11-10 14:10:04 +1000850
Ben Skeggs60d2a882010-12-06 15:28:54 +1000851 mem->mm_node = node;
852 mem->start = node->offset >> PAGE_SHIFT;
Ben Skeggs573a2a32010-08-25 15:26:04 +1000853 return 0;
854}
855
856void
857nouveau_vram_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
858{
Ben Skeggs573a2a32010-08-25 15:26:04 +1000859 struct nouveau_mm *mm = man->priv;
860 struct nouveau_mm_node *r;
Ben Skeggs8b464bf2011-01-14 15:46:30 +1000861 u32 total = 0, free = 0;
Ben Skeggs573a2a32010-08-25 15:26:04 +1000862
863 mutex_lock(&mm->mutex);
864 list_for_each_entry(r, &mm->nodes, nl_entry) {
Ben Skeggs8b464bf2011-01-14 15:46:30 +1000865 printk(KERN_DEBUG "%s %d: 0x%010llx 0x%010llx\n",
866 prefix, r->type, ((u64)r->offset << 12),
Ben Skeggs573a2a32010-08-25 15:26:04 +1000867 (((u64)r->offset + r->length) << 12));
Ben Skeggs8b464bf2011-01-14 15:46:30 +1000868
Ben Skeggs573a2a32010-08-25 15:26:04 +1000869 total += r->length;
Ben Skeggs8b464bf2011-01-14 15:46:30 +1000870 if (!r->type)
871 free += r->length;
Ben Skeggs573a2a32010-08-25 15:26:04 +1000872 }
873 mutex_unlock(&mm->mutex);
874
Ben Skeggs8b464bf2011-01-14 15:46:30 +1000875 printk(KERN_DEBUG "%s total: 0x%010llx free: 0x%010llx\n",
876 prefix, (u64)total << 12, (u64)free << 12);
877 printk(KERN_DEBUG "%s block: 0x%08x\n",
878 prefix, mm->block_size << 12);
Ben Skeggs573a2a32010-08-25 15:26:04 +1000879}
880
881const struct ttm_mem_type_manager_func nouveau_vram_manager = {
882 nouveau_vram_manager_init,
883 nouveau_vram_manager_fini,
884 nouveau_vram_manager_new,
885 nouveau_vram_manager_del,
886 nouveau_vram_manager_debug
887};
Ben Skeggs26c0c9e2011-02-10 12:59:51 +1000888
889static int
890nouveau_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
891{
892 return 0;
893}
894
895static int
896nouveau_gart_manager_fini(struct ttm_mem_type_manager *man)
897{
898 return 0;
899}
900
901static void
902nouveau_gart_manager_del(struct ttm_mem_type_manager *man,
903 struct ttm_mem_reg *mem)
904{
Ben Skeggsd2f966662011-06-06 20:54:42 +1000905 nouveau_mem_node_cleanup(mem->mm_node);
Ben Skeggsd2f966662011-06-06 20:54:42 +1000906 kfree(mem->mm_node);
Marcin Slusarz0de53a52011-06-23 16:35:31 +0200907 mem->mm_node = NULL;
Ben Skeggs26c0c9e2011-02-10 12:59:51 +1000908}
909
910static int
911nouveau_gart_manager_new(struct ttm_mem_type_manager *man,
912 struct ttm_buffer_object *bo,
913 struct ttm_placement *placement,
914 struct ttm_mem_reg *mem)
915{
916 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
Ben Skeggs26c0c9e2011-02-10 12:59:51 +1000917 struct nouveau_mem *node;
Ben Skeggs26c0c9e2011-02-10 12:59:51 +1000918
919 if (unlikely((mem->num_pages << PAGE_SHIFT) >=
920 dev_priv->gart_info.aper_size))
921 return -ENOMEM;
922
923 node = kzalloc(sizeof(*node), GFP_KERNEL);
924 if (!node)
925 return -ENOMEM;
Ben Skeggsd2f966662011-06-06 20:54:42 +1000926 node->page_shift = 12;
Ben Skeggs26c0c9e2011-02-10 12:59:51 +1000927
Ben Skeggs26c0c9e2011-02-10 12:59:51 +1000928 mem->mm_node = node;
929 mem->start = 0;
930 return 0;
931}
932
933void
934nouveau_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
935{
936}
937
938const struct ttm_mem_type_manager_func nouveau_gart_manager = {
939 nouveau_gart_manager_init,
940 nouveau_gart_manager_fini,
941 nouveau_gart_manager_new,
942 nouveau_gart_manager_del,
943 nouveau_gart_manager_debug
944};