nouveau_mem.c 23 KB

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