nouveau_mem.c 18 KB

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