nouveau_mem.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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 block;
  248. int i;
  249. virt = ((virt - dev_priv->vm_vram_base) >> 16) << 1;
  250. size = (size >> 16) << 1;
  251. phys |= ((uint64_t)flags << 32);
  252. phys |= 1;
  253. if (dev_priv->vram_sys_base) {
  254. phys += dev_priv->vram_sys_base;
  255. phys |= 0x30;
  256. }
  257. dev_priv->engine.instmem.prepare_access(dev, true);
  258. while (size) {
  259. unsigned offset_h = upper_32_bits(phys);
  260. unsigned offset_l = lower_32_bits(phys);
  261. unsigned pte, end;
  262. for (i = 7; i >= 0; i--) {
  263. block = 1 << (i + 1);
  264. if (size >= block && !(virt & (block - 1)))
  265. break;
  266. }
  267. offset_l |= (i << 7);
  268. phys += block << 15;
  269. size -= block;
  270. while (block) {
  271. pgt = dev_priv->vm_vram_pt[virt >> 14];
  272. pte = virt & 0x3ffe;
  273. end = pte + block;
  274. if (end > 16384)
  275. end = 16384;
  276. block -= (end - pte);
  277. virt += (end - pte);
  278. while (pte < end) {
  279. nv_wo32(dev, pgt, pte++, offset_l);
  280. nv_wo32(dev, pgt, pte++, offset_h);
  281. }
  282. }
  283. }
  284. dev_priv->engine.instmem.finish_access(dev);
  285. nv_wr32(dev, 0x100c80, 0x00050001);
  286. if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
  287. NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
  288. NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
  289. return -EBUSY;
  290. }
  291. nv_wr32(dev, 0x100c80, 0x00000001);
  292. if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
  293. NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
  294. NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
  295. return -EBUSY;
  296. }
  297. return 0;
  298. }
  299. void
  300. nv50_mem_vm_unbind(struct drm_device *dev, uint64_t virt, uint32_t size)
  301. {
  302. struct drm_nouveau_private *dev_priv = dev->dev_private;
  303. struct nouveau_gpuobj *pgt;
  304. unsigned pages, pte, end;
  305. virt -= dev_priv->vm_vram_base;
  306. pages = (size >> 16) << 1;
  307. dev_priv->engine.instmem.prepare_access(dev, true);
  308. while (pages) {
  309. pgt = dev_priv->vm_vram_pt[virt >> 29];
  310. pte = (virt & 0x1ffe0000ULL) >> 15;
  311. end = pte + pages;
  312. if (end > 16384)
  313. end = 16384;
  314. pages -= (end - pte);
  315. virt += (end - pte) << 15;
  316. while (pte < end)
  317. nv_wo32(dev, pgt, pte++, 0);
  318. }
  319. dev_priv->engine.instmem.finish_access(dev);
  320. nv_wr32(dev, 0x100c80, 0x00050001);
  321. if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
  322. NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
  323. NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
  324. return;
  325. }
  326. nv_wr32(dev, 0x100c80, 0x00000001);
  327. if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
  328. NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
  329. NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
  330. }
  331. }
  332. /*
  333. * Cleanup everything
  334. */
  335. void nouveau_mem_takedown(struct mem_block **heap)
  336. {
  337. struct mem_block *p;
  338. if (!*heap)
  339. return;
  340. for (p = (*heap)->next; p != *heap;) {
  341. struct mem_block *q = p;
  342. p = p->next;
  343. kfree(q);
  344. }
  345. kfree(*heap);
  346. *heap = NULL;
  347. }
  348. void nouveau_mem_close(struct drm_device *dev)
  349. {
  350. struct drm_nouveau_private *dev_priv = dev->dev_private;
  351. nouveau_bo_unpin(dev_priv->vga_ram);
  352. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  353. ttm_bo_device_release(&dev_priv->ttm.bdev);
  354. nouveau_ttm_global_release(dev_priv);
  355. if (drm_core_has_AGP(dev) && dev->agp &&
  356. drm_core_check_feature(dev, DRIVER_MODESET)) {
  357. struct drm_agp_mem *entry, *tempe;
  358. /* Remove AGP resources, but leave dev->agp
  359. intact until drv_cleanup is called. */
  360. list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
  361. if (entry->bound)
  362. drm_unbind_agp(entry->memory);
  363. drm_free_agp(entry->memory, entry->pages);
  364. kfree(entry);
  365. }
  366. INIT_LIST_HEAD(&dev->agp->memory);
  367. if (dev->agp->acquired)
  368. drm_agp_release(dev);
  369. dev->agp->acquired = 0;
  370. dev->agp->enabled = 0;
  371. }
  372. if (dev_priv->fb_mtrr) {
  373. drm_mtrr_del(dev_priv->fb_mtrr, drm_get_resource_start(dev, 1),
  374. drm_get_resource_len(dev, 1), DRM_MTRR_WC);
  375. dev_priv->fb_mtrr = 0;
  376. }
  377. }
  378. /*XXX won't work on BSD because of pci_read_config_dword */
  379. static uint32_t
  380. nouveau_mem_fb_amount_igp(struct drm_device *dev)
  381. {
  382. struct drm_nouveau_private *dev_priv = dev->dev_private;
  383. struct pci_dev *bridge;
  384. uint32_t mem;
  385. bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 1));
  386. if (!bridge) {
  387. NV_ERROR(dev, "no bridge device\n");
  388. return 0;
  389. }
  390. if (dev_priv->flags&NV_NFORCE) {
  391. pci_read_config_dword(bridge, 0x7C, &mem);
  392. return (uint64_t)(((mem >> 6) & 31) + 1)*1024*1024;
  393. } else
  394. if (dev_priv->flags&NV_NFORCE2) {
  395. pci_read_config_dword(bridge, 0x84, &mem);
  396. return (uint64_t)(((mem >> 4) & 127) + 1)*1024*1024;
  397. }
  398. NV_ERROR(dev, "impossible!\n");
  399. return 0;
  400. }
  401. /* returns the amount of FB ram in bytes */
  402. uint64_t nouveau_mem_fb_amount(struct drm_device *dev)
  403. {
  404. struct drm_nouveau_private *dev_priv = dev->dev_private;
  405. uint32_t boot0;
  406. switch (dev_priv->card_type) {
  407. case NV_04:
  408. boot0 = nv_rd32(dev, NV03_BOOT_0);
  409. if (boot0 & 0x00000100)
  410. return (((boot0 >> 12) & 0xf) * 2 + 2) * 1024 * 1024;
  411. switch (boot0 & NV03_BOOT_0_RAM_AMOUNT) {
  412. case NV04_BOOT_0_RAM_AMOUNT_32MB:
  413. return 32 * 1024 * 1024;
  414. case NV04_BOOT_0_RAM_AMOUNT_16MB:
  415. return 16 * 1024 * 1024;
  416. case NV04_BOOT_0_RAM_AMOUNT_8MB:
  417. return 8 * 1024 * 1024;
  418. case NV04_BOOT_0_RAM_AMOUNT_4MB:
  419. return 4 * 1024 * 1024;
  420. }
  421. break;
  422. case NV_10:
  423. case NV_20:
  424. case NV_30:
  425. case NV_40:
  426. case NV_50:
  427. default:
  428. if (dev_priv->flags & (NV_NFORCE | NV_NFORCE2)) {
  429. return nouveau_mem_fb_amount_igp(dev);
  430. } else {
  431. uint64_t mem;
  432. mem = (nv_rd32(dev, NV04_FIFO_DATA) &
  433. NV10_FIFO_DATA_RAM_AMOUNT_MB_MASK) >>
  434. NV10_FIFO_DATA_RAM_AMOUNT_MB_SHIFT;
  435. return mem * 1024 * 1024;
  436. }
  437. break;
  438. }
  439. NV_ERROR(dev,
  440. "Unable to detect video ram size. Please report your setup to "
  441. DRIVER_EMAIL "\n");
  442. return 0;
  443. }
  444. #if __OS_HAS_AGP
  445. static void nouveau_mem_reset_agp(struct drm_device *dev)
  446. {
  447. uint32_t saved_pci_nv_1, saved_pci_nv_19, pmc_enable;
  448. saved_pci_nv_1 = nv_rd32(dev, NV04_PBUS_PCI_NV_1);
  449. saved_pci_nv_19 = nv_rd32(dev, NV04_PBUS_PCI_NV_19);
  450. /* clear busmaster bit */
  451. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1 & ~0x4);
  452. /* clear SBA and AGP bits */
  453. nv_wr32(dev, NV04_PBUS_PCI_NV_19, saved_pci_nv_19 & 0xfffff0ff);
  454. /* power cycle pgraph, if enabled */
  455. pmc_enable = nv_rd32(dev, NV03_PMC_ENABLE);
  456. if (pmc_enable & NV_PMC_ENABLE_PGRAPH) {
  457. nv_wr32(dev, NV03_PMC_ENABLE,
  458. pmc_enable & ~NV_PMC_ENABLE_PGRAPH);
  459. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) |
  460. NV_PMC_ENABLE_PGRAPH);
  461. }
  462. /* and restore (gives effect of resetting AGP) */
  463. nv_wr32(dev, NV04_PBUS_PCI_NV_19, saved_pci_nv_19);
  464. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1);
  465. }
  466. #endif
  467. int
  468. nouveau_mem_init_agp(struct drm_device *dev)
  469. {
  470. #if __OS_HAS_AGP
  471. struct drm_nouveau_private *dev_priv = dev->dev_private;
  472. struct drm_agp_info info;
  473. struct drm_agp_mode mode;
  474. int ret;
  475. if (nouveau_noagp)
  476. return 0;
  477. nouveau_mem_reset_agp(dev);
  478. if (!dev->agp->acquired) {
  479. ret = drm_agp_acquire(dev);
  480. if (ret) {
  481. NV_ERROR(dev, "Unable to acquire AGP: %d\n", ret);
  482. return ret;
  483. }
  484. }
  485. ret = drm_agp_info(dev, &info);
  486. if (ret) {
  487. NV_ERROR(dev, "Unable to get AGP info: %d\n", ret);
  488. return ret;
  489. }
  490. /* see agp.h for the AGPSTAT_* modes available */
  491. mode.mode = info.mode;
  492. ret = drm_agp_enable(dev, mode);
  493. if (ret) {
  494. NV_ERROR(dev, "Unable to enable AGP: %d\n", ret);
  495. return ret;
  496. }
  497. dev_priv->gart_info.type = NOUVEAU_GART_AGP;
  498. dev_priv->gart_info.aper_base = info.aperture_base;
  499. dev_priv->gart_info.aper_size = info.aperture_size;
  500. #endif
  501. return 0;
  502. }
  503. int
  504. nouveau_mem_init(struct drm_device *dev)
  505. {
  506. struct drm_nouveau_private *dev_priv = dev->dev_private;
  507. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  508. int ret, dma_bits = 32;
  509. dev_priv->fb_phys = drm_get_resource_start(dev, 1);
  510. dev_priv->gart_info.type = NOUVEAU_GART_NONE;
  511. if (dev_priv->card_type >= NV_50 &&
  512. pci_dma_supported(dev->pdev, DMA_BIT_MASK(40)))
  513. dma_bits = 40;
  514. ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  515. if (ret) {
  516. NV_ERROR(dev, "Error setting DMA mask: %d\n", ret);
  517. return ret;
  518. }
  519. ret = nouveau_ttm_global_init(dev_priv);
  520. if (ret)
  521. return ret;
  522. ret = ttm_bo_device_init(&dev_priv->ttm.bdev,
  523. dev_priv->ttm.bo_global_ref.ref.object,
  524. &nouveau_bo_driver, DRM_FILE_PAGE_OFFSET,
  525. dma_bits <= 32 ? true : false);
  526. if (ret) {
  527. NV_ERROR(dev, "Error initialising bo driver: %d\n", ret);
  528. return ret;
  529. }
  530. INIT_LIST_HEAD(&dev_priv->ttm.bo_list);
  531. spin_lock_init(&dev_priv->ttm.bo_list_lock);
  532. spin_lock_init(&dev_priv->tile.lock);
  533. dev_priv->fb_available_size = nouveau_mem_fb_amount(dev);
  534. dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
  535. if (dev_priv->fb_mappable_pages > drm_get_resource_len(dev, 1))
  536. dev_priv->fb_mappable_pages = drm_get_resource_len(dev, 1);
  537. dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
  538. NV_INFO(dev, "%d MiB VRAM\n", (int)(dev_priv->fb_available_size >> 20));
  539. /* remove reserved space at end of vram from available amount */
  540. dev_priv->fb_available_size -= dev_priv->ramin_rsvd_vram;
  541. dev_priv->fb_aper_free = dev_priv->fb_available_size;
  542. /* mappable vram */
  543. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  544. dev_priv->fb_available_size >> PAGE_SHIFT);
  545. if (ret) {
  546. NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
  547. return ret;
  548. }
  549. ret = nouveau_bo_new(dev, NULL, 256*1024, 0, TTM_PL_FLAG_VRAM,
  550. 0, 0, true, true, &dev_priv->vga_ram);
  551. if (ret == 0)
  552. ret = nouveau_bo_pin(dev_priv->vga_ram, TTM_PL_FLAG_VRAM);
  553. if (ret) {
  554. NV_WARN(dev, "failed to reserve VGA memory\n");
  555. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  556. }
  557. /* GART */
  558. #if !defined(__powerpc__) && !defined(__ia64__)
  559. if (drm_device_is_agp(dev) && dev->agp) {
  560. ret = nouveau_mem_init_agp(dev);
  561. if (ret)
  562. NV_ERROR(dev, "Error initialising AGP: %d\n", ret);
  563. }
  564. #endif
  565. if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
  566. ret = nouveau_sgdma_init(dev);
  567. if (ret) {
  568. NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
  569. return ret;
  570. }
  571. }
  572. NV_INFO(dev, "%d MiB GART (aperture)\n",
  573. (int)(dev_priv->gart_info.aper_size >> 20));
  574. dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
  575. ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
  576. dev_priv->gart_info.aper_size >> PAGE_SHIFT);
  577. if (ret) {
  578. NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
  579. return ret;
  580. }
  581. dev_priv->fb_mtrr = drm_mtrr_add(drm_get_resource_start(dev, 1),
  582. drm_get_resource_len(dev, 1),
  583. DRM_MTRR_WC);
  584. return 0;
  585. }