nouveau_mem.c 17 KB

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