nouveau_mem.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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. #include "nouveau_pm.h"
  36. #include "nouveau_mm.h"
  37. #include "nouveau_vm.h"
  38. /*
  39. * NV10-NV40 tiling helpers
  40. */
  41. static void
  42. nv10_mem_update_tile_region(struct drm_device *dev,
  43. struct nouveau_tile_reg *tile, uint32_t addr,
  44. uint32_t size, uint32_t pitch, uint32_t flags)
  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. int i = tile - dev_priv->tile.reg;
  51. unsigned long save;
  52. nouveau_fence_unref(&tile->fence);
  53. if (tile->pitch)
  54. pfb->free_tile_region(dev, i);
  55. if (pitch)
  56. pfb->init_tile_region(dev, i, addr, size, pitch, flags);
  57. spin_lock_irqsave(&dev_priv->context_switch_lock, save);
  58. pfifo->reassign(dev, false);
  59. pfifo->cache_pull(dev, false);
  60. nouveau_wait_for_idle(dev);
  61. pfb->set_tile_region(dev, i);
  62. pgraph->set_tile_region(dev, i);
  63. pfifo->cache_pull(dev, true);
  64. pfifo->reassign(dev, true);
  65. spin_unlock_irqrestore(&dev_priv->context_switch_lock, save);
  66. }
  67. static struct nouveau_tile_reg *
  68. nv10_mem_get_tile_region(struct drm_device *dev, int i)
  69. {
  70. struct drm_nouveau_private *dev_priv = dev->dev_private;
  71. struct nouveau_tile_reg *tile = &dev_priv->tile.reg[i];
  72. spin_lock(&dev_priv->tile.lock);
  73. if (!tile->used &&
  74. (!tile->fence || nouveau_fence_signalled(tile->fence)))
  75. tile->used = true;
  76. else
  77. tile = NULL;
  78. spin_unlock(&dev_priv->tile.lock);
  79. return tile;
  80. }
  81. void
  82. nv10_mem_put_tile_region(struct drm_device *dev, struct nouveau_tile_reg *tile,
  83. struct nouveau_fence *fence)
  84. {
  85. struct drm_nouveau_private *dev_priv = dev->dev_private;
  86. if (tile) {
  87. spin_lock(&dev_priv->tile.lock);
  88. if (fence) {
  89. /* Mark it as pending. */
  90. tile->fence = fence;
  91. nouveau_fence_ref(fence);
  92. }
  93. tile->used = false;
  94. spin_unlock(&dev_priv->tile.lock);
  95. }
  96. }
  97. struct nouveau_tile_reg *
  98. nv10_mem_set_tiling(struct drm_device *dev, uint32_t addr, uint32_t size,
  99. uint32_t pitch, uint32_t flags)
  100. {
  101. struct drm_nouveau_private *dev_priv = dev->dev_private;
  102. struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
  103. struct nouveau_tile_reg *tile, *found = NULL;
  104. int i;
  105. for (i = 0; i < pfb->num_tiles; i++) {
  106. tile = nv10_mem_get_tile_region(dev, i);
  107. if (pitch && !found) {
  108. found = tile;
  109. continue;
  110. } else if (tile && tile->pitch) {
  111. /* Kill an unused tile region. */
  112. nv10_mem_update_tile_region(dev, tile, 0, 0, 0, 0);
  113. }
  114. nv10_mem_put_tile_region(dev, tile, NULL);
  115. }
  116. if (found)
  117. nv10_mem_update_tile_region(dev, found, addr, size,
  118. pitch, flags);
  119. return found;
  120. }
  121. /*
  122. * Cleanup everything
  123. */
  124. void
  125. nouveau_mem_vram_fini(struct drm_device *dev)
  126. {
  127. struct drm_nouveau_private *dev_priv = dev->dev_private;
  128. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  129. ttm_bo_device_release(&dev_priv->ttm.bdev);
  130. nouveau_ttm_global_release(dev_priv);
  131. if (dev_priv->fb_mtrr >= 0) {
  132. drm_mtrr_del(dev_priv->fb_mtrr,
  133. pci_resource_start(dev->pdev, 1),
  134. pci_resource_len(dev->pdev, 1), DRM_MTRR_WC);
  135. dev_priv->fb_mtrr = -1;
  136. }
  137. }
  138. void
  139. nouveau_mem_gart_fini(struct drm_device *dev)
  140. {
  141. nouveau_sgdma_takedown(dev);
  142. if (drm_core_has_AGP(dev) && dev->agp) {
  143. struct drm_agp_mem *entry, *tempe;
  144. /* Remove AGP resources, but leave dev->agp
  145. intact until drv_cleanup is called. */
  146. list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
  147. if (entry->bound)
  148. drm_unbind_agp(entry->memory);
  149. drm_free_agp(entry->memory, entry->pages);
  150. kfree(entry);
  151. }
  152. INIT_LIST_HEAD(&dev->agp->memory);
  153. if (dev->agp->acquired)
  154. drm_agp_release(dev);
  155. dev->agp->acquired = 0;
  156. dev->agp->enabled = 0;
  157. }
  158. }
  159. static uint32_t
  160. nouveau_mem_detect_nv04(struct drm_device *dev)
  161. {
  162. uint32_t boot0 = nv_rd32(dev, NV04_PFB_BOOT_0);
  163. if (boot0 & 0x00000100)
  164. return (((boot0 >> 12) & 0xf) * 2 + 2) * 1024 * 1024;
  165. switch (boot0 & NV04_PFB_BOOT_0_RAM_AMOUNT) {
  166. case NV04_PFB_BOOT_0_RAM_AMOUNT_32MB:
  167. return 32 * 1024 * 1024;
  168. case NV04_PFB_BOOT_0_RAM_AMOUNT_16MB:
  169. return 16 * 1024 * 1024;
  170. case NV04_PFB_BOOT_0_RAM_AMOUNT_8MB:
  171. return 8 * 1024 * 1024;
  172. case NV04_PFB_BOOT_0_RAM_AMOUNT_4MB:
  173. return 4 * 1024 * 1024;
  174. }
  175. return 0;
  176. }
  177. static uint32_t
  178. nouveau_mem_detect_nforce(struct drm_device *dev)
  179. {
  180. struct drm_nouveau_private *dev_priv = dev->dev_private;
  181. struct pci_dev *bridge;
  182. uint32_t mem;
  183. bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 1));
  184. if (!bridge) {
  185. NV_ERROR(dev, "no bridge device\n");
  186. return 0;
  187. }
  188. if (dev_priv->flags & NV_NFORCE) {
  189. pci_read_config_dword(bridge, 0x7C, &mem);
  190. return (uint64_t)(((mem >> 6) & 31) + 1)*1024*1024;
  191. } else
  192. if (dev_priv->flags & NV_NFORCE2) {
  193. pci_read_config_dword(bridge, 0x84, &mem);
  194. return (uint64_t)(((mem >> 4) & 127) + 1)*1024*1024;
  195. }
  196. NV_ERROR(dev, "impossible!\n");
  197. return 0;
  198. }
  199. int
  200. nouveau_mem_detect(struct drm_device *dev)
  201. {
  202. struct drm_nouveau_private *dev_priv = dev->dev_private;
  203. if (dev_priv->card_type == NV_04) {
  204. dev_priv->vram_size = nouveau_mem_detect_nv04(dev);
  205. } else
  206. if (dev_priv->flags & (NV_NFORCE | NV_NFORCE2)) {
  207. dev_priv->vram_size = nouveau_mem_detect_nforce(dev);
  208. } else
  209. if (dev_priv->card_type < NV_50) {
  210. dev_priv->vram_size = nv_rd32(dev, NV04_PFB_FIFO_DATA);
  211. dev_priv->vram_size &= NV10_PFB_FIFO_DATA_RAM_AMOUNT_MB_MASK;
  212. }
  213. if (dev_priv->vram_size)
  214. return 0;
  215. return -ENOMEM;
  216. }
  217. bool
  218. nouveau_mem_flags_valid(struct drm_device *dev, u32 tile_flags)
  219. {
  220. if (!(tile_flags & NOUVEAU_GEM_TILE_LAYOUT_MASK))
  221. return true;
  222. return false;
  223. }
  224. #if __OS_HAS_AGP
  225. static unsigned long
  226. get_agp_mode(struct drm_device *dev, unsigned long mode)
  227. {
  228. struct drm_nouveau_private *dev_priv = dev->dev_private;
  229. /*
  230. * FW seems to be broken on nv18, it makes the card lock up
  231. * randomly.
  232. */
  233. if (dev_priv->chipset == 0x18)
  234. mode &= ~PCI_AGP_COMMAND_FW;
  235. /*
  236. * AGP mode set in the command line.
  237. */
  238. if (nouveau_agpmode > 0) {
  239. bool agpv3 = mode & 0x8;
  240. int rate = agpv3 ? nouveau_agpmode / 4 : nouveau_agpmode;
  241. mode = (mode & ~0x7) | (rate & 0x7);
  242. }
  243. return mode;
  244. }
  245. #endif
  246. int
  247. nouveau_mem_reset_agp(struct drm_device *dev)
  248. {
  249. #if __OS_HAS_AGP
  250. uint32_t saved_pci_nv_1, pmc_enable;
  251. int ret;
  252. /* First of all, disable fast writes, otherwise if it's
  253. * already enabled in the AGP bridge and we disable the card's
  254. * AGP controller we might be locking ourselves out of it. */
  255. if ((nv_rd32(dev, NV04_PBUS_PCI_NV_19) |
  256. dev->agp->mode) & PCI_AGP_COMMAND_FW) {
  257. struct drm_agp_info info;
  258. struct drm_agp_mode mode;
  259. ret = drm_agp_info(dev, &info);
  260. if (ret)
  261. return ret;
  262. mode.mode = get_agp_mode(dev, info.mode) & ~PCI_AGP_COMMAND_FW;
  263. ret = drm_agp_enable(dev, mode);
  264. if (ret)
  265. return ret;
  266. }
  267. saved_pci_nv_1 = nv_rd32(dev, NV04_PBUS_PCI_NV_1);
  268. /* clear busmaster bit */
  269. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1 & ~0x4);
  270. /* disable AGP */
  271. nv_wr32(dev, NV04_PBUS_PCI_NV_19, 0);
  272. /* power cycle pgraph, if enabled */
  273. pmc_enable = nv_rd32(dev, NV03_PMC_ENABLE);
  274. if (pmc_enable & NV_PMC_ENABLE_PGRAPH) {
  275. nv_wr32(dev, NV03_PMC_ENABLE,
  276. pmc_enable & ~NV_PMC_ENABLE_PGRAPH);
  277. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) |
  278. NV_PMC_ENABLE_PGRAPH);
  279. }
  280. /* and restore (gives effect of resetting AGP) */
  281. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1);
  282. #endif
  283. return 0;
  284. }
  285. int
  286. nouveau_mem_init_agp(struct drm_device *dev)
  287. {
  288. #if __OS_HAS_AGP
  289. struct drm_nouveau_private *dev_priv = dev->dev_private;
  290. struct drm_agp_info info;
  291. struct drm_agp_mode mode;
  292. int ret;
  293. if (!dev->agp->acquired) {
  294. ret = drm_agp_acquire(dev);
  295. if (ret) {
  296. NV_ERROR(dev, "Unable to acquire AGP: %d\n", ret);
  297. return ret;
  298. }
  299. }
  300. nouveau_mem_reset_agp(dev);
  301. ret = drm_agp_info(dev, &info);
  302. if (ret) {
  303. NV_ERROR(dev, "Unable to get AGP info: %d\n", ret);
  304. return ret;
  305. }
  306. /* see agp.h for the AGPSTAT_* modes available */
  307. mode.mode = get_agp_mode(dev, info.mode);
  308. ret = drm_agp_enable(dev, mode);
  309. if (ret) {
  310. NV_ERROR(dev, "Unable to enable AGP: %d\n", ret);
  311. return ret;
  312. }
  313. dev_priv->gart_info.type = NOUVEAU_GART_AGP;
  314. dev_priv->gart_info.aper_base = info.aperture_base;
  315. dev_priv->gart_info.aper_size = info.aperture_size;
  316. #endif
  317. return 0;
  318. }
  319. int
  320. nouveau_mem_vram_init(struct drm_device *dev)
  321. {
  322. struct drm_nouveau_private *dev_priv = dev->dev_private;
  323. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  324. int ret, dma_bits;
  325. dma_bits = 32;
  326. if (dev_priv->card_type >= NV_50) {
  327. if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(40)))
  328. dma_bits = 40;
  329. } else
  330. if (drm_pci_device_is_pcie(dev) &&
  331. dev_priv->chipset != 0x40 &&
  332. dev_priv->chipset != 0x45) {
  333. if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(39)))
  334. dma_bits = 39;
  335. }
  336. ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  337. if (ret)
  338. return ret;
  339. dev_priv->fb_phys = pci_resource_start(dev->pdev, 1);
  340. ret = nouveau_ttm_global_init(dev_priv);
  341. if (ret)
  342. return ret;
  343. ret = ttm_bo_device_init(&dev_priv->ttm.bdev,
  344. dev_priv->ttm.bo_global_ref.ref.object,
  345. &nouveau_bo_driver, DRM_FILE_PAGE_OFFSET,
  346. dma_bits <= 32 ? true : false);
  347. if (ret) {
  348. NV_ERROR(dev, "Error initialising bo driver: %d\n", ret);
  349. return ret;
  350. }
  351. /* reserve space at end of VRAM for PRAMIN */
  352. if (dev_priv->card_type >= NV_50) {
  353. dev_priv->ramin_rsvd_vram = 1 * 1024 * 1024;
  354. } else
  355. if (dev_priv->card_type >= NV_40) {
  356. u32 vs = hweight8((nv_rd32(dev, 0x001540) & 0x0000ff00) >> 8);
  357. u32 rsvd;
  358. /* estimate grctx size, the magics come from nv40_grctx.c */
  359. if (dev_priv->chipset == 0x40) rsvd = 0x6aa0 * vs;
  360. else if (dev_priv->chipset < 0x43) rsvd = 0x4f00 * vs;
  361. else if (nv44_graph_class(dev)) rsvd = 0x4980 * vs;
  362. else rsvd = 0x4a40 * vs;
  363. rsvd += 16 * 1024;
  364. rsvd *= dev_priv->engine.fifo.channels;
  365. /* pciegart table */
  366. if (drm_pci_device_is_pcie(dev))
  367. rsvd += 512 * 1024;
  368. /* object storage */
  369. rsvd += 512 * 1024;
  370. dev_priv->ramin_rsvd_vram = round_up(rsvd, 4096);
  371. } else {
  372. dev_priv->ramin_rsvd_vram = 512 * 1024;
  373. }
  374. ret = dev_priv->engine.vram.init(dev);
  375. if (ret)
  376. return ret;
  377. NV_INFO(dev, "Detected %dMiB VRAM\n", (int)(dev_priv->vram_size >> 20));
  378. if (dev_priv->vram_sys_base) {
  379. NV_INFO(dev, "Stolen system memory at: 0x%010llx\n",
  380. dev_priv->vram_sys_base);
  381. }
  382. dev_priv->fb_available_size = dev_priv->vram_size;
  383. dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
  384. if (dev_priv->fb_mappable_pages > pci_resource_len(dev->pdev, 1))
  385. dev_priv->fb_mappable_pages = pci_resource_len(dev->pdev, 1);
  386. dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
  387. dev_priv->fb_available_size -= dev_priv->ramin_rsvd_vram;
  388. dev_priv->fb_aper_free = dev_priv->fb_available_size;
  389. /* mappable vram */
  390. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  391. dev_priv->fb_available_size >> PAGE_SHIFT);
  392. if (ret) {
  393. NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
  394. return ret;
  395. }
  396. if (dev_priv->card_type < NV_50) {
  397. ret = nouveau_bo_new(dev, NULL, 256*1024, 0, TTM_PL_FLAG_VRAM,
  398. 0, 0, &dev_priv->vga_ram);
  399. if (ret == 0)
  400. ret = nouveau_bo_pin(dev_priv->vga_ram,
  401. TTM_PL_FLAG_VRAM);
  402. if (ret) {
  403. NV_WARN(dev, "failed to reserve VGA memory\n");
  404. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  405. }
  406. }
  407. dev_priv->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 1),
  408. pci_resource_len(dev->pdev, 1),
  409. DRM_MTRR_WC);
  410. return 0;
  411. }
  412. int
  413. nouveau_mem_gart_init(struct drm_device *dev)
  414. {
  415. struct drm_nouveau_private *dev_priv = dev->dev_private;
  416. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  417. int ret;
  418. dev_priv->gart_info.type = NOUVEAU_GART_NONE;
  419. #if !defined(__powerpc__) && !defined(__ia64__)
  420. if (drm_pci_device_is_agp(dev) && dev->agp && nouveau_agpmode) {
  421. ret = nouveau_mem_init_agp(dev);
  422. if (ret)
  423. NV_ERROR(dev, "Error initialising AGP: %d\n", ret);
  424. }
  425. #endif
  426. if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
  427. ret = nouveau_sgdma_init(dev);
  428. if (ret) {
  429. NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
  430. return ret;
  431. }
  432. }
  433. NV_INFO(dev, "%d MiB GART (aperture)\n",
  434. (int)(dev_priv->gart_info.aper_size >> 20));
  435. dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
  436. ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
  437. dev_priv->gart_info.aper_size >> PAGE_SHIFT);
  438. if (ret) {
  439. NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
  440. return ret;
  441. }
  442. return 0;
  443. }
  444. void
  445. nouveau_mem_timing_init(struct drm_device *dev)
  446. {
  447. /* cards < NVC0 only */
  448. struct drm_nouveau_private *dev_priv = dev->dev_private;
  449. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  450. struct nouveau_pm_memtimings *memtimings = &pm->memtimings;
  451. struct nvbios *bios = &dev_priv->vbios;
  452. struct bit_entry P;
  453. u8 tUNK_0, tUNK_1, tUNK_2;
  454. u8 tRP; /* Byte 3 */
  455. u8 tRAS; /* Byte 5 */
  456. u8 tRFC; /* Byte 7 */
  457. u8 tRC; /* Byte 9 */
  458. u8 tUNK_10, tUNK_11, tUNK_12, tUNK_13, tUNK_14;
  459. u8 tUNK_18, tUNK_19, tUNK_20, tUNK_21;
  460. u8 *mem = NULL, *entry;
  461. int i, recordlen, entries;
  462. if (bios->type == NVBIOS_BIT) {
  463. if (bit_table(dev, 'P', &P))
  464. return;
  465. if (P.version == 1)
  466. mem = ROMPTR(bios, P.data[4]);
  467. else
  468. if (P.version == 2)
  469. mem = ROMPTR(bios, P.data[8]);
  470. else {
  471. NV_WARN(dev, "unknown mem for BIT P %d\n", P.version);
  472. }
  473. } else {
  474. NV_DEBUG(dev, "BMP version too old for memory\n");
  475. return;
  476. }
  477. if (!mem) {
  478. NV_DEBUG(dev, "memory timing table pointer invalid\n");
  479. return;
  480. }
  481. if (mem[0] != 0x10) {
  482. NV_WARN(dev, "memory timing table 0x%02x unknown\n", mem[0]);
  483. return;
  484. }
  485. /* validate record length */
  486. entries = mem[2];
  487. recordlen = mem[3];
  488. if (recordlen < 15) {
  489. NV_ERROR(dev, "mem timing table length unknown: %d\n", mem[3]);
  490. return;
  491. }
  492. /* parse vbios entries into common format */
  493. memtimings->timing =
  494. kcalloc(entries, sizeof(*memtimings->timing), GFP_KERNEL);
  495. if (!memtimings->timing)
  496. return;
  497. entry = mem + mem[1];
  498. for (i = 0; i < entries; i++, entry += recordlen) {
  499. struct nouveau_pm_memtiming *timing = &pm->memtimings.timing[i];
  500. if (entry[0] == 0)
  501. continue;
  502. tUNK_18 = 1;
  503. tUNK_19 = 1;
  504. tUNK_20 = 0;
  505. tUNK_21 = 0;
  506. switch (min(recordlen, 22)) {
  507. case 22:
  508. tUNK_21 = entry[21];
  509. case 21:
  510. tUNK_20 = entry[20];
  511. case 20:
  512. tUNK_19 = entry[19];
  513. case 19:
  514. tUNK_18 = entry[18];
  515. default:
  516. tUNK_0 = entry[0];
  517. tUNK_1 = entry[1];
  518. tUNK_2 = entry[2];
  519. tRP = entry[3];
  520. tRAS = entry[5];
  521. tRFC = entry[7];
  522. tRC = entry[9];
  523. tUNK_10 = entry[10];
  524. tUNK_11 = entry[11];
  525. tUNK_12 = entry[12];
  526. tUNK_13 = entry[13];
  527. tUNK_14 = entry[14];
  528. break;
  529. }
  530. timing->reg_100220 = (tRC << 24 | tRFC << 16 | tRAS << 8 | tRP);
  531. /* XXX: I don't trust the -1's and +1's... they must come
  532. * from somewhere! */
  533. timing->reg_100224 = ((tUNK_0 + tUNK_19 + 1) << 24 |
  534. tUNK_18 << 16 |
  535. (tUNK_1 + tUNK_19 + 1) << 8 |
  536. (tUNK_2 - 1));
  537. timing->reg_100228 = (tUNK_12 << 16 | tUNK_11 << 8 | tUNK_10);
  538. if(recordlen > 19) {
  539. timing->reg_100228 += (tUNK_19 - 1) << 24;
  540. }/* I cannot back-up this else-statement right now
  541. else {
  542. timing->reg_100228 += tUNK_12 << 24;
  543. }*/
  544. /* XXX: reg_10022c */
  545. timing->reg_10022c = tUNK_2 - 1;
  546. timing->reg_100230 = (tUNK_20 << 24 | tUNK_21 << 16 |
  547. tUNK_13 << 8 | tUNK_13);
  548. /* XXX: +6? */
  549. timing->reg_100234 = (tRAS << 24 | (tUNK_19 + 6) << 8 | tRC);
  550. timing->reg_100234 += max(tUNK_10,tUNK_11) << 16;
  551. /* XXX; reg_100238, reg_10023c
  552. * reg: 0x00??????
  553. * reg_10023c:
  554. * 0 for pre-NV50 cards
  555. * 0x????0202 for NV50+ cards (empirical evidence) */
  556. if(dev_priv->card_type >= NV_50) {
  557. timing->reg_10023c = 0x202;
  558. }
  559. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x %08x\n", i,
  560. timing->reg_100220, timing->reg_100224,
  561. timing->reg_100228, timing->reg_10022c);
  562. NV_DEBUG(dev, " 230: %08x %08x %08x %08x\n",
  563. timing->reg_100230, timing->reg_100234,
  564. timing->reg_100238, timing->reg_10023c);
  565. }
  566. memtimings->nr_timing = entries;
  567. memtimings->supported = true;
  568. }
  569. void
  570. nouveau_mem_timing_fini(struct drm_device *dev)
  571. {
  572. struct drm_nouveau_private *dev_priv = dev->dev_private;
  573. struct nouveau_pm_memtimings *mem = &dev_priv->engine.pm.memtimings;
  574. kfree(mem->timing);
  575. }
  576. static int
  577. nouveau_vram_manager_init(struct ttm_mem_type_manager *man, unsigned long p_size)
  578. {
  579. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  580. struct nouveau_mm *mm;
  581. u64 size, block, rsvd;
  582. int ret;
  583. rsvd = (256 * 1024); /* vga memory */
  584. size = (p_size << PAGE_SHIFT) - rsvd;
  585. block = dev_priv->vram_rblock_size;
  586. ret = nouveau_mm_init(&mm, rsvd >> 12, size >> 12, block >> 12);
  587. if (ret)
  588. return ret;
  589. man->priv = mm;
  590. return 0;
  591. }
  592. static int
  593. nouveau_vram_manager_fini(struct ttm_mem_type_manager *man)
  594. {
  595. struct nouveau_mm *mm = man->priv;
  596. int ret;
  597. ret = nouveau_mm_fini(&mm);
  598. if (ret)
  599. return ret;
  600. man->priv = NULL;
  601. return 0;
  602. }
  603. static void
  604. nouveau_vram_manager_del(struct ttm_mem_type_manager *man,
  605. struct ttm_mem_reg *mem)
  606. {
  607. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  608. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  609. struct nouveau_mem *node = mem->mm_node;
  610. struct drm_device *dev = dev_priv->dev;
  611. if (node->tmp_vma.node) {
  612. nouveau_vm_unmap(&node->tmp_vma);
  613. nouveau_vm_put(&node->tmp_vma);
  614. }
  615. vram->put(dev, (struct nouveau_mem **)&mem->mm_node);
  616. }
  617. static int
  618. nouveau_vram_manager_new(struct ttm_mem_type_manager *man,
  619. struct ttm_buffer_object *bo,
  620. struct ttm_placement *placement,
  621. struct ttm_mem_reg *mem)
  622. {
  623. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  624. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  625. struct drm_device *dev = dev_priv->dev;
  626. struct nouveau_bo *nvbo = nouveau_bo(bo);
  627. struct nouveau_mem *node;
  628. u32 size_nc = 0;
  629. int ret;
  630. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG)
  631. size_nc = 1 << nvbo->vma.node->type;
  632. ret = vram->get(dev, mem->num_pages << PAGE_SHIFT,
  633. mem->page_alignment << PAGE_SHIFT, size_nc,
  634. (nvbo->tile_flags >> 8) & 0x3ff, &node);
  635. if (ret) {
  636. mem->mm_node = NULL;
  637. return (ret == -ENOSPC) ? 0 : ret;
  638. }
  639. node->page_shift = 12;
  640. if (nvbo->vma.node)
  641. node->page_shift = nvbo->vma.node->type;
  642. mem->mm_node = node;
  643. mem->start = node->offset >> PAGE_SHIFT;
  644. return 0;
  645. }
  646. void
  647. nouveau_vram_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  648. {
  649. struct nouveau_mm *mm = man->priv;
  650. struct nouveau_mm_node *r;
  651. u32 total = 0, free = 0;
  652. mutex_lock(&mm->mutex);
  653. list_for_each_entry(r, &mm->nodes, nl_entry) {
  654. printk(KERN_DEBUG "%s %d: 0x%010llx 0x%010llx\n",
  655. prefix, r->type, ((u64)r->offset << 12),
  656. (((u64)r->offset + r->length) << 12));
  657. total += r->length;
  658. if (!r->type)
  659. free += r->length;
  660. }
  661. mutex_unlock(&mm->mutex);
  662. printk(KERN_DEBUG "%s total: 0x%010llx free: 0x%010llx\n",
  663. prefix, (u64)total << 12, (u64)free << 12);
  664. printk(KERN_DEBUG "%s block: 0x%08x\n",
  665. prefix, mm->block_size << 12);
  666. }
  667. const struct ttm_mem_type_manager_func nouveau_vram_manager = {
  668. nouveau_vram_manager_init,
  669. nouveau_vram_manager_fini,
  670. nouveau_vram_manager_new,
  671. nouveau_vram_manager_del,
  672. nouveau_vram_manager_debug
  673. };
  674. static int
  675. nouveau_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  676. {
  677. return 0;
  678. }
  679. static int
  680. nouveau_gart_manager_fini(struct ttm_mem_type_manager *man)
  681. {
  682. return 0;
  683. }
  684. static void
  685. nouveau_gart_manager_del(struct ttm_mem_type_manager *man,
  686. struct ttm_mem_reg *mem)
  687. {
  688. struct nouveau_mem *node = mem->mm_node;
  689. if (node->tmp_vma.node) {
  690. nouveau_vm_unmap(&node->tmp_vma);
  691. nouveau_vm_put(&node->tmp_vma);
  692. }
  693. mem->mm_node = NULL;
  694. }
  695. static int
  696. nouveau_gart_manager_new(struct ttm_mem_type_manager *man,
  697. struct ttm_buffer_object *bo,
  698. struct ttm_placement *placement,
  699. struct ttm_mem_reg *mem)
  700. {
  701. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  702. struct nouveau_bo *nvbo = nouveau_bo(bo);
  703. struct nouveau_vma *vma = &nvbo->vma;
  704. struct nouveau_vm *vm = vma->vm;
  705. struct nouveau_mem *node;
  706. int ret;
  707. if (unlikely((mem->num_pages << PAGE_SHIFT) >=
  708. dev_priv->gart_info.aper_size))
  709. return -ENOMEM;
  710. node = kzalloc(sizeof(*node), GFP_KERNEL);
  711. if (!node)
  712. return -ENOMEM;
  713. /* This node must be for evicting large-paged VRAM
  714. * to system memory. Due to a nv50 limitation of
  715. * not being able to mix large/small pages within
  716. * the same PDE, we need to create a temporary
  717. * small-paged VMA for the eviction.
  718. */
  719. if (vma->node->type != vm->spg_shift) {
  720. ret = nouveau_vm_get(vm, (u64)vma->node->length << 12,
  721. vm->spg_shift, NV_MEM_ACCESS_RW,
  722. &node->tmp_vma);
  723. if (ret) {
  724. kfree(node);
  725. return ret;
  726. }
  727. }
  728. node->page_shift = nvbo->vma.node->type;
  729. mem->mm_node = node;
  730. mem->start = 0;
  731. return 0;
  732. }
  733. void
  734. nouveau_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  735. {
  736. }
  737. const struct ttm_mem_type_manager_func nouveau_gart_manager = {
  738. nouveau_gart_manager_init,
  739. nouveau_gart_manager_fini,
  740. nouveau_gart_manager_new,
  741. nouveau_gart_manager_del,
  742. nouveau_gart_manager_debug
  743. };