drm/nouveau: add instmem flush() hook
[deliverable/linux.git] / drivers / gpu / drm / nouveau / nouveau_mem.c
CommitLineData
6ee73861
BS
1/*
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"
36#include "nouveau_drv.h"
37
a0af9add
FJ
38/*
39 * NV10-NV40 tiling helpers
40 */
41
42static void
43nv10_mem_set_region_tiling(struct drm_device *dev, int i, uint32_t addr,
44 uint32_t size, uint32_t pitch)
45{
46 struct drm_nouveau_private *dev_priv = dev->dev_private;
47 struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
48 struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
49 struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
50 struct nouveau_tile_reg *tile = &dev_priv->tile.reg[i];
51
52 tile->addr = addr;
53 tile->size = size;
54 tile->used = !!pitch;
55 nouveau_fence_unref((void **)&tile->fence);
56
57 if (!pfifo->cache_flush(dev))
58 return;
59
60 pfifo->reassign(dev, false);
61 pfifo->cache_flush(dev);
62 pfifo->cache_pull(dev, false);
63
64 nouveau_wait_for_idle(dev);
65
66 pgraph->set_region_tiling(dev, i, addr, size, pitch);
67 pfb->set_region_tiling(dev, i, addr, size, pitch);
68
69 pfifo->cache_pull(dev, true);
70 pfifo->reassign(dev, true);
71}
72
73struct nouveau_tile_reg *
74nv10_mem_set_tiling(struct drm_device *dev, uint32_t addr, uint32_t size,
75 uint32_t pitch)
76{
77 struct drm_nouveau_private *dev_priv = dev->dev_private;
78 struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
79 struct nouveau_tile_reg *tile = dev_priv->tile.reg, *found = NULL;
80 int i;
81
82 spin_lock(&dev_priv->tile.lock);
83
84 for (i = 0; i < pfb->num_tiles; i++) {
85 if (tile[i].used)
86 /* Tile region in use. */
87 continue;
88
89 if (tile[i].fence &&
90 !nouveau_fence_signalled(tile[i].fence, NULL))
91 /* Pending tile region. */
92 continue;
93
94 if (max(tile[i].addr, addr) <
95 min(tile[i].addr + tile[i].size, addr + size))
96 /* Kill an intersecting tile region. */
97 nv10_mem_set_region_tiling(dev, i, 0, 0, 0);
98
99 if (pitch && !found) {
100 /* Free tile region. */
101 nv10_mem_set_region_tiling(dev, i, addr, size, pitch);
102 found = &tile[i];
103 }
104 }
105
106 spin_unlock(&dev_priv->tile.lock);
107
108 return found;
109}
110
111void
112nv10_mem_expire_tiling(struct drm_device *dev, struct nouveau_tile_reg *tile,
113 struct nouveau_fence *fence)
114{
115 if (fence) {
116 /* Mark it as pending. */
117 tile->fence = fence;
118 nouveau_fence_ref(fence);
119 }
120
121 tile->used = false;
122}
123
6ee73861
BS
124/*
125 * NV50 VM helpers
126 */
127int
128nv50_mem_vm_bind_linear(struct drm_device *dev, uint64_t virt, uint32_t size,
129 uint32_t flags, uint64_t phys)
130{
131 struct drm_nouveau_private *dev_priv = dev->dev_private;
531e7713
BS
132 struct nouveau_gpuobj *pgt;
133 unsigned block;
134 int i;
6ee73861 135
531e7713
BS
136 virt = ((virt - dev_priv->vm_vram_base) >> 16) << 1;
137 size = (size >> 16) << 1;
6c429667
BS
138
139 phys |= ((uint64_t)flags << 32);
140 phys |= 1;
141 if (dev_priv->vram_sys_base) {
142 phys += dev_priv->vram_sys_base;
143 phys |= 0x30;
144 }
6ee73861 145
531e7713
BS
146 while (size) {
147 unsigned offset_h = upper_32_bits(phys);
4c27bd33 148 unsigned offset_l = lower_32_bits(phys);
531e7713
BS
149 unsigned pte, end;
150
151 for (i = 7; i >= 0; i--) {
152 block = 1 << (i + 1);
153 if (size >= block && !(virt & (block - 1)))
154 break;
155 }
156 offset_l |= (i << 7);
6ee73861 157
531e7713
BS
158 phys += block << 15;
159 size -= block;
6ee73861 160
531e7713
BS
161 while (block) {
162 pgt = dev_priv->vm_vram_pt[virt >> 14];
163 pte = virt & 0x3ffe;
164
165 end = pte + block;
166 if (end > 16384)
167 end = 16384;
168 block -= (end - pte);
169 virt += (end - pte);
170
171 while (pte < end) {
172 nv_wo32(dev, pgt, pte++, offset_l);
173 nv_wo32(dev, pgt, pte++, offset_h);
174 }
175 }
6ee73861 176 }
f56cb86f 177 dev_priv->engine.instmem.flush(dev);
6ee73861
BS
178
179 nv_wr32(dev, 0x100c80, 0x00050001);
180 if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
181 NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
182 NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
183 return -EBUSY;
184 }
185
186 nv_wr32(dev, 0x100c80, 0x00000001);
187 if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
188 NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
189 NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
190 return -EBUSY;
191 }
192
40b2a687
BS
193 nv_wr32(dev, 0x100c80, 0x00040001);
194 if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
195 NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
196 NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
197 return -EBUSY;
198 }
199
200 nv_wr32(dev, 0x100c80, 0x00060001);
201 if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
202 NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
203 NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
204 return -EBUSY;
205 }
206
6ee73861
BS
207 return 0;
208}
209
210void
211nv50_mem_vm_unbind(struct drm_device *dev, uint64_t virt, uint32_t size)
212{
4c27bd33
BS
213 struct drm_nouveau_private *dev_priv = dev->dev_private;
214 struct nouveau_gpuobj *pgt;
215 unsigned pages, pte, end;
216
217 virt -= dev_priv->vm_vram_base;
218 pages = (size >> 16) << 1;
219
4c27bd33
BS
220 while (pages) {
221 pgt = dev_priv->vm_vram_pt[virt >> 29];
222 pte = (virt & 0x1ffe0000ULL) >> 15;
223
224 end = pte + pages;
225 if (end > 16384)
226 end = 16384;
227 pages -= (end - pte);
228 virt += (end - pte) << 15;
229
230 while (pte < end)
231 nv_wo32(dev, pgt, pte++, 0);
232 }
f56cb86f 233 dev_priv->engine.instmem.flush(dev);
4c27bd33
BS
234
235 nv_wr32(dev, 0x100c80, 0x00050001);
236 if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
237 NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
238 NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
239 return;
240 }
241
242 nv_wr32(dev, 0x100c80, 0x00000001);
40b2a687
BS
243 if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
244 NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
245 NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
246 return;
247 }
248
249 nv_wr32(dev, 0x100c80, 0x00040001);
250 if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
251 NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
252 NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
253 return;
254 }
255
256 nv_wr32(dev, 0x100c80, 0x00060001);
4c27bd33
BS
257 if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
258 NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
259 NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
260 }
6ee73861
BS
261}
262
263/*
264 * Cleanup everything
265 */
b833ac26
BS
266void
267nouveau_mem_close(struct drm_device *dev)
6ee73861
BS
268{
269 struct drm_nouveau_private *dev_priv = dev->dev_private;
270
ac8fb975
BS
271 nouveau_bo_unpin(dev_priv->vga_ram);
272 nouveau_bo_ref(NULL, &dev_priv->vga_ram);
273
6ee73861
BS
274 ttm_bo_device_release(&dev_priv->ttm.bdev);
275
276 nouveau_ttm_global_release(dev_priv);
277
cd0b072f 278 if (drm_core_has_AGP(dev) && dev->agp) {
6ee73861
BS
279 struct drm_agp_mem *entry, *tempe;
280
281 /* Remove AGP resources, but leave dev->agp
282 intact until drv_cleanup is called. */
283 list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
284 if (entry->bound)
285 drm_unbind_agp(entry->memory);
286 drm_free_agp(entry->memory, entry->pages);
287 kfree(entry);
288 }
289 INIT_LIST_HEAD(&dev->agp->memory);
290
291 if (dev->agp->acquired)
292 drm_agp_release(dev);
293
294 dev->agp->acquired = 0;
295 dev->agp->enabled = 0;
296 }
297
298 if (dev_priv->fb_mtrr) {
01d73a69
JC
299 drm_mtrr_del(dev_priv->fb_mtrr,
300 pci_resource_start(dev->pdev, 1),
301 pci_resource_len(dev->pdev, 1), DRM_MTRR_WC);
6ee73861
BS
302 dev_priv->fb_mtrr = 0;
303 }
304}
305
6ee73861 306static uint32_t
a76fb4e8
BS
307nouveau_mem_detect_nv04(struct drm_device *dev)
308{
309 uint32_t boot0 = nv_rd32(dev, NV03_BOOT_0);
310
311 if (boot0 & 0x00000100)
312 return (((boot0 >> 12) & 0xf) * 2 + 2) * 1024 * 1024;
313
314 switch (boot0 & NV03_BOOT_0_RAM_AMOUNT) {
315 case NV04_BOOT_0_RAM_AMOUNT_32MB:
316 return 32 * 1024 * 1024;
317 case NV04_BOOT_0_RAM_AMOUNT_16MB:
318 return 16 * 1024 * 1024;
319 case NV04_BOOT_0_RAM_AMOUNT_8MB:
320 return 8 * 1024 * 1024;
321 case NV04_BOOT_0_RAM_AMOUNT_4MB:
322 return 4 * 1024 * 1024;
323 }
324
325 return 0;
326}
327
328static uint32_t
329nouveau_mem_detect_nforce(struct drm_device *dev)
6ee73861
BS
330{
331 struct drm_nouveau_private *dev_priv = dev->dev_private;
332 struct pci_dev *bridge;
333 uint32_t mem;
334
335 bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 1));
336 if (!bridge) {
337 NV_ERROR(dev, "no bridge device\n");
338 return 0;
339 }
340
a76fb4e8 341 if (dev_priv->flags & NV_NFORCE) {
6ee73861
BS
342 pci_read_config_dword(bridge, 0x7C, &mem);
343 return (uint64_t)(((mem >> 6) & 31) + 1)*1024*1024;
344 } else
a76fb4e8 345 if (dev_priv->flags & NV_NFORCE2) {
6ee73861
BS
346 pci_read_config_dword(bridge, 0x84, &mem);
347 return (uint64_t)(((mem >> 4) & 127) + 1)*1024*1024;
348 }
349
350 NV_ERROR(dev, "impossible!\n");
351 return 0;
352}
353
354/* returns the amount of FB ram in bytes */
a76fb4e8
BS
355int
356nouveau_mem_detect(struct drm_device *dev)
6ee73861
BS
357{
358 struct drm_nouveau_private *dev_priv = dev->dev_private;
a76fb4e8
BS
359
360 if (dev_priv->card_type == NV_04) {
361 dev_priv->vram_size = nouveau_mem_detect_nv04(dev);
362 } else
363 if (dev_priv->flags & (NV_NFORCE | NV_NFORCE2)) {
364 dev_priv->vram_size = nouveau_mem_detect_nforce(dev);
7a2e4e03
BS
365 } else
366 if (dev_priv->card_type < NV_50) {
a76fb4e8
BS
367 dev_priv->vram_size = nv_rd32(dev, NV04_FIFO_DATA);
368 dev_priv->vram_size &= NV10_FIFO_DATA_RAM_AMOUNT_MB_MASK;
7a2e4e03
BS
369 } else {
370 dev_priv->vram_size = nv_rd32(dev, NV04_FIFO_DATA);
371 dev_priv->vram_size |= (dev_priv->vram_size & 0xff) << 32;
6e86e041 372 dev_priv->vram_size &= 0xffffffff00ll;
fb4f5621 373 if (dev_priv->chipset == 0xaa || dev_priv->chipset == 0xac) {
8b281db5
BS
374 dev_priv->vram_sys_base = nv_rd32(dev, 0x100e10);
375 dev_priv->vram_sys_base <<= 12;
fb4f5621 376 }
6ee73861
BS
377 }
378
a76fb4e8
BS
379 NV_INFO(dev, "Detected %dMiB VRAM\n", (int)(dev_priv->vram_size >> 20));
380 if (dev_priv->vram_sys_base) {
381 NV_INFO(dev, "Stolen system memory at: 0x%010llx\n",
382 dev_priv->vram_sys_base);
383 }
384
385 if (dev_priv->vram_size)
386 return 0;
387 return -ENOMEM;
6ee73861
BS
388}
389
b694dfb2 390#if __OS_HAS_AGP
6ee73861
BS
391static void nouveau_mem_reset_agp(struct drm_device *dev)
392{
393 uint32_t saved_pci_nv_1, saved_pci_nv_19, pmc_enable;
394
395 saved_pci_nv_1 = nv_rd32(dev, NV04_PBUS_PCI_NV_1);
396 saved_pci_nv_19 = nv_rd32(dev, NV04_PBUS_PCI_NV_19);
397
398 /* clear busmaster bit */
399 nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1 & ~0x4);
400 /* clear SBA and AGP bits */
401 nv_wr32(dev, NV04_PBUS_PCI_NV_19, saved_pci_nv_19 & 0xfffff0ff);
402
403 /* power cycle pgraph, if enabled */
404 pmc_enable = nv_rd32(dev, NV03_PMC_ENABLE);
405 if (pmc_enable & NV_PMC_ENABLE_PGRAPH) {
406 nv_wr32(dev, NV03_PMC_ENABLE,
407 pmc_enable & ~NV_PMC_ENABLE_PGRAPH);
408 nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) |
409 NV_PMC_ENABLE_PGRAPH);
410 }
411
412 /* and restore (gives effect of resetting AGP) */
413 nv_wr32(dev, NV04_PBUS_PCI_NV_19, saved_pci_nv_19);
414 nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1);
415}
b694dfb2 416#endif
6ee73861
BS
417
418int
419nouveau_mem_init_agp(struct drm_device *dev)
420{
b694dfb2 421#if __OS_HAS_AGP
6ee73861
BS
422 struct drm_nouveau_private *dev_priv = dev->dev_private;
423 struct drm_agp_info info;
424 struct drm_agp_mode mode;
425 int ret;
426
427 if (nouveau_noagp)
428 return 0;
429
430 nouveau_mem_reset_agp(dev);
431
432 if (!dev->agp->acquired) {
433 ret = drm_agp_acquire(dev);
434 if (ret) {
435 NV_ERROR(dev, "Unable to acquire AGP: %d\n", ret);
436 return ret;
437 }
438 }
439
440 ret = drm_agp_info(dev, &info);
441 if (ret) {
442 NV_ERROR(dev, "Unable to get AGP info: %d\n", ret);
443 return ret;
444 }
445
446 /* see agp.h for the AGPSTAT_* modes available */
447 mode.mode = info.mode;
448 ret = drm_agp_enable(dev, mode);
449 if (ret) {
450 NV_ERROR(dev, "Unable to enable AGP: %d\n", ret);
451 return ret;
452 }
453
454 dev_priv->gart_info.type = NOUVEAU_GART_AGP;
455 dev_priv->gart_info.aper_base = info.aperture_base;
456 dev_priv->gart_info.aper_size = info.aperture_size;
b694dfb2 457#endif
6ee73861
BS
458 return 0;
459}
460
461int
462nouveau_mem_init(struct drm_device *dev)
463{
464 struct drm_nouveau_private *dev_priv = dev->dev_private;
465 struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
466 int ret, dma_bits = 32;
467
01d73a69 468 dev_priv->fb_phys = pci_resource_start(dev->pdev, 1);
6ee73861
BS
469 dev_priv->gart_info.type = NOUVEAU_GART_NONE;
470
471 if (dev_priv->card_type >= NV_50 &&
472 pci_dma_supported(dev->pdev, DMA_BIT_MASK(40)))
473 dma_bits = 40;
474
475 ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
476 if (ret) {
477 NV_ERROR(dev, "Error setting DMA mask: %d\n", ret);
478 return ret;
479 }
480
481 ret = nouveau_ttm_global_init(dev_priv);
482 if (ret)
483 return ret;
484
485 ret = ttm_bo_device_init(&dev_priv->ttm.bdev,
486 dev_priv->ttm.bo_global_ref.ref.object,
487 &nouveau_bo_driver, DRM_FILE_PAGE_OFFSET,
488 dma_bits <= 32 ? true : false);
489 if (ret) {
490 NV_ERROR(dev, "Error initialising bo driver: %d\n", ret);
491 return ret;
492 }
493
494 INIT_LIST_HEAD(&dev_priv->ttm.bo_list);
495 spin_lock_init(&dev_priv->ttm.bo_list_lock);
a0af9add 496 spin_lock_init(&dev_priv->tile.lock);
6ee73861 497
a76fb4e8 498 dev_priv->fb_available_size = dev_priv->vram_size;
6ee73861 499 dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
01d73a69
JC
500 if (dev_priv->fb_mappable_pages > pci_resource_len(dev->pdev, 1))
501 dev_priv->fb_mappable_pages =
502 pci_resource_len(dev->pdev, 1);
6ee73861
BS
503 dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
504
6ee73861
BS
505 /* remove reserved space at end of vram from available amount */
506 dev_priv->fb_available_size -= dev_priv->ramin_rsvd_vram;
507 dev_priv->fb_aper_free = dev_priv->fb_available_size;
508
509 /* mappable vram */
510 ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
511 dev_priv->fb_available_size >> PAGE_SHIFT);
512 if (ret) {
513 NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
514 return ret;
515 }
516
ac8fb975
BS
517 ret = nouveau_bo_new(dev, NULL, 256*1024, 0, TTM_PL_FLAG_VRAM,
518 0, 0, true, true, &dev_priv->vga_ram);
519 if (ret == 0)
520 ret = nouveau_bo_pin(dev_priv->vga_ram, TTM_PL_FLAG_VRAM);
521 if (ret) {
522 NV_WARN(dev, "failed to reserve VGA memory\n");
523 nouveau_bo_ref(NULL, &dev_priv->vga_ram);
524 }
525
6ee73861
BS
526 /* GART */
527#if !defined(__powerpc__) && !defined(__ia64__)
528 if (drm_device_is_agp(dev) && dev->agp) {
529 ret = nouveau_mem_init_agp(dev);
530 if (ret)
531 NV_ERROR(dev, "Error initialising AGP: %d\n", ret);
532 }
533#endif
534
535 if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
536 ret = nouveau_sgdma_init(dev);
537 if (ret) {
538 NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
539 return ret;
540 }
541 }
542
543 NV_INFO(dev, "%d MiB GART (aperture)\n",
544 (int)(dev_priv->gart_info.aper_size >> 20));
545 dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
546
547 ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
548 dev_priv->gart_info.aper_size >> PAGE_SHIFT);
549 if (ret) {
550 NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
551 return ret;
552 }
553
01d73a69
JC
554 dev_priv->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 1),
555 pci_resource_len(dev->pdev, 1),
6ee73861 556 DRM_MTRR_WC);
ac8fb975 557
6ee73861
BS
558 return 0;
559}
560
561
This page took 0.089283 seconds and 5 git commands to generate.