nouveau_mem.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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_unpin(dev_priv->vga_ram);
  129. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  130. ttm_bo_device_release(&dev_priv->ttm.bdev);
  131. nouveau_ttm_global_release(dev_priv);
  132. if (dev_priv->fb_mtrr >= 0) {
  133. drm_mtrr_del(dev_priv->fb_mtrr,
  134. pci_resource_start(dev->pdev, 1),
  135. pci_resource_len(dev->pdev, 1), DRM_MTRR_WC);
  136. dev_priv->fb_mtrr = -1;
  137. }
  138. }
  139. void
  140. nouveau_mem_gart_fini(struct drm_device *dev)
  141. {
  142. nouveau_sgdma_takedown(dev);
  143. if (drm_core_has_AGP(dev) && dev->agp) {
  144. struct drm_agp_mem *entry, *tempe;
  145. /* Remove AGP resources, but leave dev->agp
  146. intact until drv_cleanup is called. */
  147. list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
  148. if (entry->bound)
  149. drm_unbind_agp(entry->memory);
  150. drm_free_agp(entry->memory, entry->pages);
  151. kfree(entry);
  152. }
  153. INIT_LIST_HEAD(&dev->agp->memory);
  154. if (dev->agp->acquired)
  155. drm_agp_release(dev);
  156. dev->agp->acquired = 0;
  157. dev->agp->enabled = 0;
  158. }
  159. }
  160. static uint32_t
  161. nouveau_mem_detect_nv04(struct drm_device *dev)
  162. {
  163. uint32_t boot0 = nv_rd32(dev, NV04_PFB_BOOT_0);
  164. if (boot0 & 0x00000100)
  165. return (((boot0 >> 12) & 0xf) * 2 + 2) * 1024 * 1024;
  166. switch (boot0 & NV04_PFB_BOOT_0_RAM_AMOUNT) {
  167. case NV04_PFB_BOOT_0_RAM_AMOUNT_32MB:
  168. return 32 * 1024 * 1024;
  169. case NV04_PFB_BOOT_0_RAM_AMOUNT_16MB:
  170. return 16 * 1024 * 1024;
  171. case NV04_PFB_BOOT_0_RAM_AMOUNT_8MB:
  172. return 8 * 1024 * 1024;
  173. case NV04_PFB_BOOT_0_RAM_AMOUNT_4MB:
  174. return 4 * 1024 * 1024;
  175. }
  176. return 0;
  177. }
  178. static uint32_t
  179. nouveau_mem_detect_nforce(struct drm_device *dev)
  180. {
  181. struct drm_nouveau_private *dev_priv = dev->dev_private;
  182. struct pci_dev *bridge;
  183. uint32_t mem;
  184. bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 1));
  185. if (!bridge) {
  186. NV_ERROR(dev, "no bridge device\n");
  187. return 0;
  188. }
  189. if (dev_priv->flags & NV_NFORCE) {
  190. pci_read_config_dword(bridge, 0x7C, &mem);
  191. return (uint64_t)(((mem >> 6) & 31) + 1)*1024*1024;
  192. } else
  193. if (dev_priv->flags & NV_NFORCE2) {
  194. pci_read_config_dword(bridge, 0x84, &mem);
  195. return (uint64_t)(((mem >> 4) & 127) + 1)*1024*1024;
  196. }
  197. NV_ERROR(dev, "impossible!\n");
  198. return 0;
  199. }
  200. static int
  201. nouveau_mem_detect(struct drm_device *dev)
  202. {
  203. struct drm_nouveau_private *dev_priv = dev->dev_private;
  204. if (dev_priv->card_type == NV_04) {
  205. dev_priv->vram_size = nouveau_mem_detect_nv04(dev);
  206. } else
  207. if (dev_priv->flags & (NV_NFORCE | NV_NFORCE2)) {
  208. dev_priv->vram_size = nouveau_mem_detect_nforce(dev);
  209. } else
  210. if (dev_priv->card_type < NV_50) {
  211. dev_priv->vram_size = nv_rd32(dev, NV04_PFB_FIFO_DATA);
  212. dev_priv->vram_size &= NV10_PFB_FIFO_DATA_RAM_AMOUNT_MB_MASK;
  213. } else
  214. if (dev_priv->card_type < NV_C0) {
  215. if (nv50_vram_init(dev))
  216. return -ENOMEM;
  217. } else {
  218. dev_priv->vram_size = nv_rd32(dev, 0x10f20c) << 20;
  219. dev_priv->vram_size *= nv_rd32(dev, 0x121c74);
  220. }
  221. NV_INFO(dev, "Detected %dMiB VRAM\n", (int)(dev_priv->vram_size >> 20));
  222. if (dev_priv->vram_sys_base) {
  223. NV_INFO(dev, "Stolen system memory at: 0x%010llx\n",
  224. dev_priv->vram_sys_base);
  225. }
  226. if (dev_priv->vram_size)
  227. return 0;
  228. return -ENOMEM;
  229. }
  230. #if __OS_HAS_AGP
  231. static unsigned long
  232. get_agp_mode(struct drm_device *dev, unsigned long mode)
  233. {
  234. struct drm_nouveau_private *dev_priv = dev->dev_private;
  235. /*
  236. * FW seems to be broken on nv18, it makes the card lock up
  237. * randomly.
  238. */
  239. if (dev_priv->chipset == 0x18)
  240. mode &= ~PCI_AGP_COMMAND_FW;
  241. /*
  242. * AGP mode set in the command line.
  243. */
  244. if (nouveau_agpmode > 0) {
  245. bool agpv3 = mode & 0x8;
  246. int rate = agpv3 ? nouveau_agpmode / 4 : nouveau_agpmode;
  247. mode = (mode & ~0x7) | (rate & 0x7);
  248. }
  249. return mode;
  250. }
  251. #endif
  252. int
  253. nouveau_mem_reset_agp(struct drm_device *dev)
  254. {
  255. #if __OS_HAS_AGP
  256. uint32_t saved_pci_nv_1, pmc_enable;
  257. int ret;
  258. /* First of all, disable fast writes, otherwise if it's
  259. * already enabled in the AGP bridge and we disable the card's
  260. * AGP controller we might be locking ourselves out of it. */
  261. if ((nv_rd32(dev, NV04_PBUS_PCI_NV_19) |
  262. dev->agp->mode) & PCI_AGP_COMMAND_FW) {
  263. struct drm_agp_info info;
  264. struct drm_agp_mode mode;
  265. ret = drm_agp_info(dev, &info);
  266. if (ret)
  267. return ret;
  268. mode.mode = get_agp_mode(dev, info.mode) & ~PCI_AGP_COMMAND_FW;
  269. ret = drm_agp_enable(dev, mode);
  270. if (ret)
  271. return ret;
  272. }
  273. saved_pci_nv_1 = nv_rd32(dev, NV04_PBUS_PCI_NV_1);
  274. /* clear busmaster bit */
  275. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1 & ~0x4);
  276. /* disable AGP */
  277. nv_wr32(dev, NV04_PBUS_PCI_NV_19, 0);
  278. /* power cycle pgraph, if enabled */
  279. pmc_enable = nv_rd32(dev, NV03_PMC_ENABLE);
  280. if (pmc_enable & NV_PMC_ENABLE_PGRAPH) {
  281. nv_wr32(dev, NV03_PMC_ENABLE,
  282. pmc_enable & ~NV_PMC_ENABLE_PGRAPH);
  283. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) |
  284. NV_PMC_ENABLE_PGRAPH);
  285. }
  286. /* and restore (gives effect of resetting AGP) */
  287. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1);
  288. #endif
  289. return 0;
  290. }
  291. int
  292. nouveau_mem_init_agp(struct drm_device *dev)
  293. {
  294. #if __OS_HAS_AGP
  295. struct drm_nouveau_private *dev_priv = dev->dev_private;
  296. struct drm_agp_info info;
  297. struct drm_agp_mode mode;
  298. int ret;
  299. if (!dev->agp->acquired) {
  300. ret = drm_agp_acquire(dev);
  301. if (ret) {
  302. NV_ERROR(dev, "Unable to acquire AGP: %d\n", ret);
  303. return ret;
  304. }
  305. }
  306. nouveau_mem_reset_agp(dev);
  307. ret = drm_agp_info(dev, &info);
  308. if (ret) {
  309. NV_ERROR(dev, "Unable to get AGP info: %d\n", ret);
  310. return ret;
  311. }
  312. /* see agp.h for the AGPSTAT_* modes available */
  313. mode.mode = get_agp_mode(dev, info.mode);
  314. ret = drm_agp_enable(dev, mode);
  315. if (ret) {
  316. NV_ERROR(dev, "Unable to enable AGP: %d\n", ret);
  317. return ret;
  318. }
  319. dev_priv->gart_info.type = NOUVEAU_GART_AGP;
  320. dev_priv->gart_info.aper_base = info.aperture_base;
  321. dev_priv->gart_info.aper_size = info.aperture_size;
  322. #endif
  323. return 0;
  324. }
  325. int
  326. nouveau_mem_vram_init(struct drm_device *dev)
  327. {
  328. struct drm_nouveau_private *dev_priv = dev->dev_private;
  329. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  330. int ret, dma_bits;
  331. if (dev_priv->card_type >= NV_50 &&
  332. pci_dma_supported(dev->pdev, DMA_BIT_MASK(40)))
  333. dma_bits = 40;
  334. else
  335. dma_bits = 32;
  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->chipset == 0x40 || dev_priv->chipset == 0x47 ||
  353. dev_priv->chipset == 0x49 || dev_priv->chipset == 0x4b)
  354. dev_priv->ramin_rsvd_vram = (2 * 1024 * 1024);
  355. else
  356. if (dev_priv->card_type >= NV_40)
  357. dev_priv->ramin_rsvd_vram = (1 * 1024 * 1024);
  358. else
  359. dev_priv->ramin_rsvd_vram = (512 * 1024);
  360. /* initialise gpu-specific vram backend */
  361. ret = nouveau_mem_detect(dev);
  362. if (ret)
  363. return ret;
  364. dev_priv->fb_available_size = dev_priv->vram_size;
  365. dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
  366. if (dev_priv->fb_mappable_pages > pci_resource_len(dev->pdev, 1))
  367. dev_priv->fb_mappable_pages = pci_resource_len(dev->pdev, 1);
  368. dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
  369. dev_priv->fb_available_size -= dev_priv->ramin_rsvd_vram;
  370. dev_priv->fb_aper_free = dev_priv->fb_available_size;
  371. /* mappable vram */
  372. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  373. dev_priv->fb_available_size >> PAGE_SHIFT);
  374. if (ret) {
  375. NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
  376. return ret;
  377. }
  378. ret = nouveau_bo_new(dev, NULL, 256*1024, 0, TTM_PL_FLAG_VRAM,
  379. 0, 0, true, true, &dev_priv->vga_ram);
  380. if (ret == 0)
  381. ret = nouveau_bo_pin(dev_priv->vga_ram, TTM_PL_FLAG_VRAM);
  382. if (ret) {
  383. NV_WARN(dev, "failed to reserve VGA memory\n");
  384. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  385. }
  386. dev_priv->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 1),
  387. pci_resource_len(dev->pdev, 1),
  388. DRM_MTRR_WC);
  389. return 0;
  390. }
  391. int
  392. nouveau_mem_gart_init(struct drm_device *dev)
  393. {
  394. struct drm_nouveau_private *dev_priv = dev->dev_private;
  395. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  396. int ret;
  397. dev_priv->gart_info.type = NOUVEAU_GART_NONE;
  398. #if !defined(__powerpc__) && !defined(__ia64__)
  399. if (drm_device_is_agp(dev) && dev->agp && nouveau_agpmode) {
  400. ret = nouveau_mem_init_agp(dev);
  401. if (ret)
  402. NV_ERROR(dev, "Error initialising AGP: %d\n", ret);
  403. }
  404. #endif
  405. if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
  406. ret = nouveau_sgdma_init(dev);
  407. if (ret) {
  408. NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
  409. return ret;
  410. }
  411. }
  412. NV_INFO(dev, "%d MiB GART (aperture)\n",
  413. (int)(dev_priv->gart_info.aper_size >> 20));
  414. dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
  415. ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
  416. dev_priv->gart_info.aper_size >> PAGE_SHIFT);
  417. if (ret) {
  418. NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
  419. return ret;
  420. }
  421. return 0;
  422. }
  423. void
  424. nouveau_mem_timing_init(struct drm_device *dev)
  425. {
  426. /* cards < NVC0 only */
  427. struct drm_nouveau_private *dev_priv = dev->dev_private;
  428. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  429. struct nouveau_pm_memtimings *memtimings = &pm->memtimings;
  430. struct nvbios *bios = &dev_priv->vbios;
  431. struct bit_entry P;
  432. u8 tUNK_0, tUNK_1, tUNK_2;
  433. u8 tRP; /* Byte 3 */
  434. u8 tRAS; /* Byte 5 */
  435. u8 tRFC; /* Byte 7 */
  436. u8 tRC; /* Byte 9 */
  437. u8 tUNK_10, tUNK_11, tUNK_12, tUNK_13, tUNK_14;
  438. u8 tUNK_18, tUNK_19, tUNK_20, tUNK_21;
  439. u8 *mem = NULL, *entry;
  440. int i, recordlen, entries;
  441. if (bios->type == NVBIOS_BIT) {
  442. if (bit_table(dev, 'P', &P))
  443. return;
  444. if (P.version == 1)
  445. mem = ROMPTR(bios, P.data[4]);
  446. else
  447. if (P.version == 2)
  448. mem = ROMPTR(bios, P.data[8]);
  449. else {
  450. NV_WARN(dev, "unknown mem for BIT P %d\n", P.version);
  451. }
  452. } else {
  453. NV_DEBUG(dev, "BMP version too old for memory\n");
  454. return;
  455. }
  456. if (!mem) {
  457. NV_DEBUG(dev, "memory timing table pointer invalid\n");
  458. return;
  459. }
  460. if (mem[0] != 0x10) {
  461. NV_WARN(dev, "memory timing table 0x%02x unknown\n", mem[0]);
  462. return;
  463. }
  464. /* validate record length */
  465. entries = mem[2];
  466. recordlen = mem[3];
  467. if (recordlen < 15) {
  468. NV_ERROR(dev, "mem timing table length unknown: %d\n", mem[3]);
  469. return;
  470. }
  471. /* parse vbios entries into common format */
  472. memtimings->timing =
  473. kcalloc(entries, sizeof(*memtimings->timing), GFP_KERNEL);
  474. if (!memtimings->timing)
  475. return;
  476. entry = mem + mem[1];
  477. for (i = 0; i < entries; i++, entry += recordlen) {
  478. struct nouveau_pm_memtiming *timing = &pm->memtimings.timing[i];
  479. if (entry[0] == 0)
  480. continue;
  481. tUNK_18 = 1;
  482. tUNK_19 = 1;
  483. tUNK_20 = 0;
  484. tUNK_21 = 0;
  485. switch (min(recordlen, 22)) {
  486. case 22:
  487. tUNK_21 = entry[21];
  488. case 21:
  489. tUNK_20 = entry[20];
  490. case 20:
  491. tUNK_19 = entry[19];
  492. case 19:
  493. tUNK_18 = entry[18];
  494. default:
  495. tUNK_0 = entry[0];
  496. tUNK_1 = entry[1];
  497. tUNK_2 = entry[2];
  498. tRP = entry[3];
  499. tRAS = entry[5];
  500. tRFC = entry[7];
  501. tRC = entry[9];
  502. tUNK_10 = entry[10];
  503. tUNK_11 = entry[11];
  504. tUNK_12 = entry[12];
  505. tUNK_13 = entry[13];
  506. tUNK_14 = entry[14];
  507. break;
  508. }
  509. timing->reg_100220 = (tRC << 24 | tRFC << 16 | tRAS << 8 | tRP);
  510. /* XXX: I don't trust the -1's and +1's... they must come
  511. * from somewhere! */
  512. timing->reg_100224 = ((tUNK_0 + tUNK_19 + 1) << 24 |
  513. tUNK_18 << 16 |
  514. (tUNK_1 + tUNK_19 + 1) << 8 |
  515. (tUNK_2 - 1));
  516. timing->reg_100228 = (tUNK_12 << 16 | tUNK_11 << 8 | tUNK_10);
  517. if(recordlen > 19) {
  518. timing->reg_100228 += (tUNK_19 - 1) << 24;
  519. }/* I cannot back-up this else-statement right now
  520. else {
  521. timing->reg_100228 += tUNK_12 << 24;
  522. }*/
  523. /* XXX: reg_10022c */
  524. timing->reg_10022c = tUNK_2 - 1;
  525. timing->reg_100230 = (tUNK_20 << 24 | tUNK_21 << 16 |
  526. tUNK_13 << 8 | tUNK_13);
  527. /* XXX: +6? */
  528. timing->reg_100234 = (tRAS << 24 | (tUNK_19 + 6) << 8 | tRC);
  529. timing->reg_100234 += max(tUNK_10,tUNK_11) << 16;
  530. /* XXX; reg_100238, reg_10023c
  531. * reg: 0x00??????
  532. * reg_10023c:
  533. * 0 for pre-NV50 cards
  534. * 0x????0202 for NV50+ cards (empirical evidence) */
  535. if(dev_priv->card_type >= NV_50) {
  536. timing->reg_10023c = 0x202;
  537. }
  538. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x %08x\n", i,
  539. timing->reg_100220, timing->reg_100224,
  540. timing->reg_100228, timing->reg_10022c);
  541. NV_DEBUG(dev, " 230: %08x %08x %08x %08x\n",
  542. timing->reg_100230, timing->reg_100234,
  543. timing->reg_100238, timing->reg_10023c);
  544. }
  545. memtimings->nr_timing = entries;
  546. memtimings->supported = true;
  547. }
  548. void
  549. nouveau_mem_timing_fini(struct drm_device *dev)
  550. {
  551. struct drm_nouveau_private *dev_priv = dev->dev_private;
  552. struct nouveau_pm_memtimings *mem = &dev_priv->engine.pm.memtimings;
  553. kfree(mem->timing);
  554. }
  555. static int
  556. nouveau_vram_manager_init(struct ttm_mem_type_manager *man, unsigned long p_size)
  557. {
  558. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  559. struct nouveau_mm *mm;
  560. u32 b_size;
  561. int ret;
  562. p_size = (p_size << PAGE_SHIFT) >> 12;
  563. b_size = dev_priv->vram_rblock_size >> 12;
  564. ret = nouveau_mm_init(&mm, 0, p_size, b_size);
  565. if (ret)
  566. return ret;
  567. man->priv = mm;
  568. return 0;
  569. }
  570. static int
  571. nouveau_vram_manager_fini(struct ttm_mem_type_manager *man)
  572. {
  573. struct nouveau_mm *mm = man->priv;
  574. int ret;
  575. ret = nouveau_mm_fini(&mm);
  576. if (ret)
  577. return ret;
  578. man->priv = NULL;
  579. return 0;
  580. }
  581. static void
  582. nouveau_vram_manager_del(struct ttm_mem_type_manager *man,
  583. struct ttm_mem_reg *mem)
  584. {
  585. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  586. struct drm_device *dev = dev_priv->dev;
  587. nv50_vram_del(dev, (struct nouveau_vram **)&mem->mm_node);
  588. }
  589. static int
  590. nouveau_vram_manager_new(struct ttm_mem_type_manager *man,
  591. struct ttm_buffer_object *bo,
  592. struct ttm_placement *placement,
  593. struct ttm_mem_reg *mem)
  594. {
  595. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  596. struct drm_device *dev = dev_priv->dev;
  597. struct nouveau_bo *nvbo = nouveau_bo(bo);
  598. struct nouveau_vram *vram;
  599. u32 size_nc = 0;
  600. int ret;
  601. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG)
  602. size_nc = 1 << nvbo->vma.node->type;
  603. ret = nv50_vram_new(dev, mem->num_pages << PAGE_SHIFT,
  604. mem->page_alignment << PAGE_SHIFT, size_nc,
  605. (nvbo->tile_flags >> 8) & 0x7f, &vram);
  606. if (ret)
  607. return ret;
  608. mem->mm_node = vram;
  609. mem->start = vram->offset >> PAGE_SHIFT;
  610. return 0;
  611. }
  612. void
  613. nouveau_vram_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  614. {
  615. struct ttm_bo_global *glob = man->bdev->glob;
  616. struct nouveau_mm *mm = man->priv;
  617. struct nouveau_mm_node *r;
  618. u64 total = 0, ttotal[3] = {}, tused[3] = {}, tfree[3] = {};
  619. int i;
  620. mutex_lock(&mm->mutex);
  621. list_for_each_entry(r, &mm->nodes, nl_entry) {
  622. printk(KERN_DEBUG "%s %s-%d: 0x%010llx 0x%010llx\n",
  623. prefix, r->free ? "free" : "used", r->type,
  624. ((u64)r->offset << 12),
  625. (((u64)r->offset + r->length) << 12));
  626. total += r->length;
  627. ttotal[r->type] += r->length;
  628. if (r->free)
  629. tfree[r->type] += r->length;
  630. else
  631. tused[r->type] += r->length;
  632. }
  633. mutex_unlock(&mm->mutex);
  634. printk(KERN_DEBUG "%s total: 0x%010llx\n", prefix, total << 12);
  635. for (i = 0; i < 3; i++) {
  636. printk(KERN_DEBUG "%s type %d: 0x%010llx, "
  637. "used 0x%010llx, free 0x%010llx\n", prefix,
  638. i, ttotal[i] << 12, tused[i] << 12, tfree[i] << 12);
  639. }
  640. }
  641. const struct ttm_mem_type_manager_func nouveau_vram_manager = {
  642. nouveau_vram_manager_init,
  643. nouveau_vram_manager_fini,
  644. nouveau_vram_manager_new,
  645. nouveau_vram_manager_del,
  646. nouveau_vram_manager_debug
  647. };