nouveau_mem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. 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. bool
  161. nouveau_mem_flags_valid(struct drm_device *dev, u32 tile_flags)
  162. {
  163. if (!(tile_flags & NOUVEAU_GEM_TILE_LAYOUT_MASK))
  164. return true;
  165. return false;
  166. }
  167. #if __OS_HAS_AGP
  168. static unsigned long
  169. get_agp_mode(struct drm_device *dev, unsigned long mode)
  170. {
  171. struct drm_nouveau_private *dev_priv = dev->dev_private;
  172. /*
  173. * FW seems to be broken on nv18, it makes the card lock up
  174. * randomly.
  175. */
  176. if (dev_priv->chipset == 0x18)
  177. mode &= ~PCI_AGP_COMMAND_FW;
  178. /*
  179. * AGP mode set in the command line.
  180. */
  181. if (nouveau_agpmode > 0) {
  182. bool agpv3 = mode & 0x8;
  183. int rate = agpv3 ? nouveau_agpmode / 4 : nouveau_agpmode;
  184. mode = (mode & ~0x7) | (rate & 0x7);
  185. }
  186. return mode;
  187. }
  188. #endif
  189. int
  190. nouveau_mem_reset_agp(struct drm_device *dev)
  191. {
  192. #if __OS_HAS_AGP
  193. uint32_t saved_pci_nv_1, pmc_enable;
  194. int ret;
  195. /* First of all, disable fast writes, otherwise if it's
  196. * already enabled in the AGP bridge and we disable the card's
  197. * AGP controller we might be locking ourselves out of it. */
  198. if ((nv_rd32(dev, NV04_PBUS_PCI_NV_19) |
  199. dev->agp->mode) & PCI_AGP_COMMAND_FW) {
  200. struct drm_agp_info info;
  201. struct drm_agp_mode mode;
  202. ret = drm_agp_info(dev, &info);
  203. if (ret)
  204. return ret;
  205. mode.mode = get_agp_mode(dev, info.mode) & ~PCI_AGP_COMMAND_FW;
  206. ret = drm_agp_enable(dev, mode);
  207. if (ret)
  208. return ret;
  209. }
  210. saved_pci_nv_1 = nv_rd32(dev, NV04_PBUS_PCI_NV_1);
  211. /* clear busmaster bit */
  212. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1 & ~0x4);
  213. /* disable AGP */
  214. nv_wr32(dev, NV04_PBUS_PCI_NV_19, 0);
  215. /* power cycle pgraph, if enabled */
  216. pmc_enable = nv_rd32(dev, NV03_PMC_ENABLE);
  217. if (pmc_enable & NV_PMC_ENABLE_PGRAPH) {
  218. nv_wr32(dev, NV03_PMC_ENABLE,
  219. pmc_enable & ~NV_PMC_ENABLE_PGRAPH);
  220. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) |
  221. NV_PMC_ENABLE_PGRAPH);
  222. }
  223. /* and restore (gives effect of resetting AGP) */
  224. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1);
  225. #endif
  226. return 0;
  227. }
  228. int
  229. nouveau_mem_init_agp(struct drm_device *dev)
  230. {
  231. #if __OS_HAS_AGP
  232. struct drm_nouveau_private *dev_priv = dev->dev_private;
  233. struct drm_agp_info info;
  234. struct drm_agp_mode mode;
  235. int ret;
  236. if (!dev->agp->acquired) {
  237. ret = drm_agp_acquire(dev);
  238. if (ret) {
  239. NV_ERROR(dev, "Unable to acquire AGP: %d\n", ret);
  240. return ret;
  241. }
  242. }
  243. nouveau_mem_reset_agp(dev);
  244. ret = drm_agp_info(dev, &info);
  245. if (ret) {
  246. NV_ERROR(dev, "Unable to get AGP info: %d\n", ret);
  247. return ret;
  248. }
  249. /* see agp.h for the AGPSTAT_* modes available */
  250. mode.mode = get_agp_mode(dev, info.mode);
  251. ret = drm_agp_enable(dev, mode);
  252. if (ret) {
  253. NV_ERROR(dev, "Unable to enable AGP: %d\n", ret);
  254. return ret;
  255. }
  256. dev_priv->gart_info.type = NOUVEAU_GART_AGP;
  257. dev_priv->gart_info.aper_base = info.aperture_base;
  258. dev_priv->gart_info.aper_size = info.aperture_size;
  259. #endif
  260. return 0;
  261. }
  262. static const struct vram_types {
  263. int value;
  264. const char *name;
  265. } vram_type_map[] = {
  266. { NV_MEM_TYPE_STOLEN , "stolen system memory" },
  267. { NV_MEM_TYPE_SGRAM , "SGRAM" },
  268. { NV_MEM_TYPE_SDRAM , "SDRAM" },
  269. { NV_MEM_TYPE_DDR1 , "DDR1" },
  270. { NV_MEM_TYPE_DDR2 , "DDR2" },
  271. { NV_MEM_TYPE_DDR3 , "DDR3" },
  272. { NV_MEM_TYPE_GDDR2 , "GDDR2" },
  273. { NV_MEM_TYPE_GDDR3 , "GDDR3" },
  274. { NV_MEM_TYPE_GDDR4 , "GDDR4" },
  275. { NV_MEM_TYPE_GDDR5 , "GDDR5" },
  276. { NV_MEM_TYPE_UNKNOWN, "unknown type" }
  277. };
  278. int
  279. nouveau_mem_vram_init(struct drm_device *dev)
  280. {
  281. struct drm_nouveau_private *dev_priv = dev->dev_private;
  282. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  283. const struct vram_types *vram_type;
  284. int ret, dma_bits;
  285. dma_bits = 32;
  286. if (dev_priv->card_type >= NV_50) {
  287. if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(40)))
  288. dma_bits = 40;
  289. } else
  290. if (0 && pci_is_pcie(dev->pdev) &&
  291. dev_priv->chipset > 0x40 &&
  292. dev_priv->chipset != 0x45) {
  293. if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(39)))
  294. dma_bits = 39;
  295. }
  296. ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  297. if (ret)
  298. return ret;
  299. ret = pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  300. if (ret) {
  301. /* Reset to default value. */
  302. pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32));
  303. }
  304. ret = nouveau_ttm_global_init(dev_priv);
  305. if (ret)
  306. return ret;
  307. ret = ttm_bo_device_init(&dev_priv->ttm.bdev,
  308. dev_priv->ttm.bo_global_ref.ref.object,
  309. &nouveau_bo_driver, DRM_FILE_PAGE_OFFSET,
  310. dma_bits <= 32 ? true : false);
  311. if (ret) {
  312. NV_ERROR(dev, "Error initialising bo driver: %d\n", ret);
  313. return ret;
  314. }
  315. vram_type = vram_type_map;
  316. while (vram_type->value != NV_MEM_TYPE_UNKNOWN) {
  317. if (nouveau_vram_type) {
  318. if (!strcasecmp(nouveau_vram_type, vram_type->name))
  319. break;
  320. dev_priv->vram_type = vram_type->value;
  321. } else {
  322. if (vram_type->value == dev_priv->vram_type)
  323. break;
  324. }
  325. vram_type++;
  326. }
  327. NV_INFO(dev, "Detected %dMiB VRAM (%s)\n",
  328. (int)(dev_priv->vram_size >> 20), vram_type->name);
  329. if (dev_priv->vram_sys_base) {
  330. NV_INFO(dev, "Stolen system memory at: 0x%010llx\n",
  331. dev_priv->vram_sys_base);
  332. }
  333. dev_priv->fb_available_size = dev_priv->vram_size;
  334. dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
  335. if (dev_priv->fb_mappable_pages > pci_resource_len(dev->pdev, 1))
  336. dev_priv->fb_mappable_pages = pci_resource_len(dev->pdev, 1);
  337. dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
  338. dev_priv->fb_available_size -= dev_priv->ramin_rsvd_vram;
  339. dev_priv->fb_aper_free = dev_priv->fb_available_size;
  340. /* mappable vram */
  341. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  342. dev_priv->fb_available_size >> PAGE_SHIFT);
  343. if (ret) {
  344. NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
  345. return ret;
  346. }
  347. if (dev_priv->card_type < NV_50) {
  348. ret = nouveau_bo_new(dev, 256*1024, 0, TTM_PL_FLAG_VRAM,
  349. 0, 0, &dev_priv->vga_ram);
  350. if (ret == 0)
  351. ret = nouveau_bo_pin(dev_priv->vga_ram,
  352. TTM_PL_FLAG_VRAM);
  353. if (ret) {
  354. NV_WARN(dev, "failed to reserve VGA memory\n");
  355. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  356. }
  357. }
  358. dev_priv->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 1),
  359. pci_resource_len(dev->pdev, 1),
  360. DRM_MTRR_WC);
  361. return 0;
  362. }
  363. int
  364. nouveau_mem_gart_init(struct drm_device *dev)
  365. {
  366. struct drm_nouveau_private *dev_priv = dev->dev_private;
  367. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  368. int ret;
  369. dev_priv->gart_info.type = NOUVEAU_GART_NONE;
  370. #if !defined(__powerpc__) && !defined(__ia64__)
  371. if (drm_pci_device_is_agp(dev) && dev->agp && nouveau_agpmode) {
  372. ret = nouveau_mem_init_agp(dev);
  373. if (ret)
  374. NV_ERROR(dev, "Error initialising AGP: %d\n", ret);
  375. }
  376. #endif
  377. if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
  378. ret = nouveau_sgdma_init(dev);
  379. if (ret) {
  380. NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
  381. return ret;
  382. }
  383. }
  384. NV_INFO(dev, "%d MiB GART (aperture)\n",
  385. (int)(dev_priv->gart_info.aper_size >> 20));
  386. dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
  387. ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
  388. dev_priv->gart_info.aper_size >> PAGE_SHIFT);
  389. if (ret) {
  390. NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
  391. return ret;
  392. }
  393. return 0;
  394. }
  395. /* XXX: For now a dummy. More samples required, possibly even a card
  396. * Called from nouveau_perf.c */
  397. void nv30_mem_timing_entry(struct drm_device *dev,
  398. struct nouveau_pm_tbl_header *hdr,
  399. struct nouveau_pm_tbl_entry *e, uint8_t magic_number,
  400. struct nouveau_pm_memtiming *timing)
  401. {
  402. NV_DEBUG(dev, "Timing entry format unknown, "
  403. "please contact nouveau developers");
  404. }
  405. void nv40_mem_timing_entry(struct drm_device *dev,
  406. struct nouveau_pm_tbl_header *hdr,
  407. struct nouveau_pm_tbl_entry *e, uint8_t magic_number,
  408. struct nouveau_pm_memtiming *timing)
  409. {
  410. timing->reg_0 = (e->tRC << 24 | e->tRFC << 16 | e->tRAS << 8 | e->tRP);
  411. /* XXX: I don't trust the -1's and +1's... they must come
  412. * from somewhere! */
  413. timing->reg_1 = (e->tWR + 2 + magic_number) << 24 |
  414. 1 << 16 |
  415. (e->tUNK_1 + 2 + magic_number) << 8 |
  416. (e->tCL + 2 - magic_number);
  417. timing->reg_2 = magic_number << 24 | e->tUNK_12 << 16 |
  418. e->tUNK_11 << 8 | e->tUNK_10;
  419. timing->reg_2 |= 0x20200000;
  420. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x\n", timing->id,
  421. timing->reg_0, timing->reg_1, timing->reg_2);
  422. }
  423. void nv50_mem_timing_entry(struct drm_device *dev, struct bit_entry *P,
  424. struct nouveau_pm_tbl_header *hdr,
  425. struct nouveau_pm_tbl_entry *e, uint8_t magic_number,
  426. struct nouveau_pm_memtiming *timing)
  427. {
  428. struct drm_nouveau_private *dev_priv = dev->dev_private;
  429. uint8_t unk18 = 1,
  430. unk19 = 1,
  431. unk20 = 0,
  432. unk21 = 0;
  433. switch (min(hdr->entry_len, (u8) 22)) {
  434. case 22:
  435. unk21 = e->tUNK_21;
  436. case 21:
  437. unk20 = e->tUNK_20;
  438. case 20:
  439. unk19 = e->tUNK_19;
  440. case 19:
  441. unk18 = e->tUNK_18;
  442. break;
  443. }
  444. timing->reg_0 = (e->tRC << 24 | e->tRFC << 16 | e->tRAS << 8 | e->tRP);
  445. /* XXX: I don't trust the -1's and +1's... they must come
  446. * from somewhere! */
  447. timing->reg_1 = (e->tWR + unk19 + 1 + magic_number) << 24 |
  448. max(unk18, (u8) 1) << 16 |
  449. (e->tUNK_1 + unk19 + 1 + magic_number) << 8;
  450. if (dev_priv->chipset == 0xa8)
  451. timing->reg_1 |= (e->tCL - 1);
  452. else
  453. timing->reg_1 |= (e->tCL + 2 - magic_number);
  454. timing->reg_2 = (e->tUNK_12 << 16 | e->tUNK_11 << 8 | e->tUNK_10);
  455. timing->reg_5 = (e->tRAS << 24 | e->tRC);
  456. timing->reg_5 += max(e->tUNK_10, e->tUNK_11) << 16;
  457. if (P->version == 1) {
  458. timing->reg_2 |= magic_number << 24;
  459. timing->reg_3 = (0x14 + e->tCL) << 24 |
  460. 0x16 << 16 |
  461. (e->tCL - 1) << 8 |
  462. (e->tCL - 1);
  463. timing->reg_4 = (nv_rd32(dev, 0x10022c) & 0xffff0000) |
  464. e->tUNK_13 << 8 | e->tUNK_13;
  465. timing->reg_5 |= (e->tCL + 2) << 8;
  466. timing->reg_7 = 0x4000202 | (e->tCL - 1) << 16;
  467. } else {
  468. timing->reg_2 |= (unk19 - 1) << 24;
  469. /* XXX: reg_10022c for recentish cards pretty much unknown*/
  470. timing->reg_3 = e->tCL - 1;
  471. timing->reg_4 = (unk20 << 24 | unk21 << 16 |
  472. e->tUNK_13 << 8 | e->tUNK_13);
  473. /* XXX: +6? */
  474. timing->reg_5 |= (unk19 + 6) << 8;
  475. /* XXX: reg_10023c currently unknown
  476. * 10023c seen as 06xxxxxx, 0bxxxxxx or 0fxxxxxx */
  477. timing->reg_7 = 0x202;
  478. }
  479. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x %08x\n", timing->id,
  480. timing->reg_0, timing->reg_1,
  481. timing->reg_2, timing->reg_3);
  482. NV_DEBUG(dev, " 230: %08x %08x %08x %08x\n",
  483. timing->reg_4, timing->reg_5,
  484. timing->reg_6, timing->reg_7);
  485. NV_DEBUG(dev, " 240: %08x\n", timing->reg_8);
  486. }
  487. void nvc0_mem_timing_entry(struct drm_device *dev,
  488. struct nouveau_pm_tbl_header *hdr,
  489. struct nouveau_pm_tbl_entry *e,
  490. struct nouveau_pm_memtiming *timing)
  491. {
  492. timing->reg_0 = (e->tRC << 24 | (e->tRFC & 0x7f) << 17 |
  493. e->tRAS << 8 | e->tRP);
  494. timing->reg_1 = (nv_rd32(dev, 0x10f294) & 0xff000000) |
  495. (e->tUNK_11&0x0f) << 20 | (e->tUNK_19 << 7) |
  496. (e->tCL & 0x0f);
  497. timing->reg_2 = (nv_rd32(dev, 0x10f298) & 0xff0000ff) |
  498. e->tWR << 16 | e->tUNK_1 << 8;
  499. timing->reg_3 = e->tUNK_20 << 9 | e->tUNK_13;
  500. timing->reg_4 = (nv_rd32(dev, 0x10f2a0) & 0xfff000ff) |
  501. e->tUNK_12 << 15;
  502. NV_DEBUG(dev, "Entry %d: 290: %08x %08x %08x %08x\n", timing->id,
  503. timing->reg_0, timing->reg_1,
  504. timing->reg_2, timing->reg_3);
  505. NV_DEBUG(dev, " 2a0: %08x %08x %08x %08x\n",
  506. timing->reg_4, timing->reg_5,
  507. timing->reg_6, timing->reg_7);
  508. }
  509. /**
  510. * Processes the Memory Timing BIOS table, stores generated
  511. * register values
  512. * @pre init scripts were run, memtiming regs are initialized
  513. */
  514. void
  515. nouveau_mem_timing_init(struct drm_device *dev)
  516. {
  517. struct drm_nouveau_private *dev_priv = dev->dev_private;
  518. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  519. struct nouveau_pm_memtimings *memtimings = &pm->memtimings;
  520. struct nvbios *bios = &dev_priv->vbios;
  521. struct bit_entry P;
  522. struct nouveau_pm_tbl_header *hdr = NULL;
  523. uint8_t magic_number;
  524. u8 *entry;
  525. int i;
  526. if (bios->type == NVBIOS_BIT) {
  527. if (bit_table(dev, 'P', &P))
  528. return;
  529. if (P.version == 1)
  530. hdr = (struct nouveau_pm_tbl_header *) ROMPTR(dev,
  531. P.data[4]);
  532. else if (P.version == 2)
  533. hdr = (struct nouveau_pm_tbl_header *) ROMPTR(dev,
  534. P.data[8]);
  535. else
  536. NV_WARN(dev, "unknown mem for BIT P %d\n", P.version);
  537. } else {
  538. NV_DEBUG(dev, "BMP version too old for memory\n");
  539. return;
  540. }
  541. if (!hdr) {
  542. NV_DEBUG(dev, "memory timing table pointer invalid\n");
  543. return;
  544. }
  545. if (hdr->version != 0x10) {
  546. NV_WARN(dev, "memory timing table 0x%02x unknown\n",
  547. hdr->version);
  548. return;
  549. }
  550. /* validate record length */
  551. if (hdr->entry_len < 15) {
  552. NV_ERROR(dev, "mem timing table length unknown: %d\n",
  553. hdr->entry_len);
  554. return;
  555. }
  556. /* parse vbios entries into common format */
  557. memtimings->timing = kcalloc(hdr->entry_cnt,
  558. sizeof(*memtimings->timing), GFP_KERNEL);
  559. if (!memtimings->timing)
  560. return;
  561. /* Get "some number" from the timing reg for NV_40 and NV_50
  562. * Used in calculations later... source unknown */
  563. magic_number = 0;
  564. if (P.version == 1)
  565. magic_number = (nv_rd32(dev, 0x100228) & 0x0f000000) >> 24;
  566. entry = (u8 *) hdr + hdr->header_len;
  567. for (i = 0; i < hdr->entry_cnt; i++, entry += hdr->entry_len) {
  568. struct nouveau_pm_memtiming *timing = &pm->memtimings.timing[i];
  569. struct nouveau_pm_tbl_entry *entry_struct =
  570. (struct nouveau_pm_tbl_entry *) entry;
  571. if (entry[0] == 0)
  572. continue;
  573. timing->id = i;
  574. timing->WR = entry[0];
  575. timing->CL = entry[2];
  576. if (dev_priv->card_type <= NV_40) {
  577. nv40_mem_timing_entry(dev, hdr, entry_struct,
  578. magic_number,
  579. &pm->memtimings.timing[i]);
  580. } else if (dev_priv->card_type == NV_50) {
  581. nv50_mem_timing_entry(dev, &P, hdr, entry_struct,
  582. magic_number,
  583. &pm->memtimings.timing[i]);
  584. } else if (dev_priv->card_type == NV_C0) {
  585. nvc0_mem_timing_entry(dev, hdr, entry_struct,
  586. &pm->memtimings.timing[i]);
  587. }
  588. }
  589. memtimings->nr_timing = hdr->entry_cnt;
  590. memtimings->supported = (P.version == 1);
  591. }
  592. void
  593. nouveau_mem_timing_fini(struct drm_device *dev)
  594. {
  595. struct drm_nouveau_private *dev_priv = dev->dev_private;
  596. struct nouveau_pm_memtimings *mem = &dev_priv->engine.pm.memtimings;
  597. kfree(mem->timing);
  598. mem->timing = NULL;
  599. }
  600. int
  601. nouveau_mem_vbios_type(struct drm_device *dev)
  602. {
  603. struct bit_entry M;
  604. u8 ramcfg = (nv_rd32(dev, 0x101000) & 0x0000003c) >> 2;
  605. if (!bit_table(dev, 'M', &M) || M.version != 2 || M.length < 5) {
  606. u8 *table = ROMPTR(dev, M.data[3]);
  607. if (table && table[0] == 0x10 && ramcfg < table[3]) {
  608. u8 *entry = table + table[1] + (ramcfg * table[2]);
  609. switch (entry[0] & 0x0f) {
  610. case 0: return NV_MEM_TYPE_DDR2;
  611. case 1: return NV_MEM_TYPE_DDR3;
  612. case 2: return NV_MEM_TYPE_GDDR3;
  613. case 3: return NV_MEM_TYPE_GDDR5;
  614. default:
  615. break;
  616. }
  617. }
  618. }
  619. return NV_MEM_TYPE_UNKNOWN;
  620. }
  621. static int
  622. nouveau_vram_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  623. {
  624. /* nothing to do */
  625. return 0;
  626. }
  627. static int
  628. nouveau_vram_manager_fini(struct ttm_mem_type_manager *man)
  629. {
  630. /* nothing to do */
  631. return 0;
  632. }
  633. static inline void
  634. nouveau_mem_node_cleanup(struct nouveau_mem *node)
  635. {
  636. if (node->vma[0].node) {
  637. nouveau_vm_unmap(&node->vma[0]);
  638. nouveau_vm_put(&node->vma[0]);
  639. }
  640. if (node->vma[1].node) {
  641. nouveau_vm_unmap(&node->vma[1]);
  642. nouveau_vm_put(&node->vma[1]);
  643. }
  644. }
  645. static void
  646. nouveau_vram_manager_del(struct ttm_mem_type_manager *man,
  647. struct ttm_mem_reg *mem)
  648. {
  649. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  650. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  651. struct drm_device *dev = dev_priv->dev;
  652. nouveau_mem_node_cleanup(mem->mm_node);
  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->page_shift;
  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 = nvbo->page_shift;
  678. mem->mm_node = node;
  679. mem->start = node->offset >> PAGE_SHIFT;
  680. return 0;
  681. }
  682. void
  683. nouveau_vram_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  684. {
  685. struct nouveau_mm *mm = man->priv;
  686. struct nouveau_mm_node *r;
  687. u32 total = 0, free = 0;
  688. mutex_lock(&mm->mutex);
  689. list_for_each_entry(r, &mm->nodes, nl_entry) {
  690. printk(KERN_DEBUG "%s %d: 0x%010llx 0x%010llx\n",
  691. prefix, r->type, ((u64)r->offset << 12),
  692. (((u64)r->offset + r->length) << 12));
  693. total += r->length;
  694. if (!r->type)
  695. free += r->length;
  696. }
  697. mutex_unlock(&mm->mutex);
  698. printk(KERN_DEBUG "%s total: 0x%010llx free: 0x%010llx\n",
  699. prefix, (u64)total << 12, (u64)free << 12);
  700. printk(KERN_DEBUG "%s block: 0x%08x\n",
  701. prefix, mm->block_size << 12);
  702. }
  703. const struct ttm_mem_type_manager_func nouveau_vram_manager = {
  704. nouveau_vram_manager_init,
  705. nouveau_vram_manager_fini,
  706. nouveau_vram_manager_new,
  707. nouveau_vram_manager_del,
  708. nouveau_vram_manager_debug
  709. };
  710. static int
  711. nouveau_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  712. {
  713. return 0;
  714. }
  715. static int
  716. nouveau_gart_manager_fini(struct ttm_mem_type_manager *man)
  717. {
  718. return 0;
  719. }
  720. static void
  721. nouveau_gart_manager_del(struct ttm_mem_type_manager *man,
  722. struct ttm_mem_reg *mem)
  723. {
  724. nouveau_mem_node_cleanup(mem->mm_node);
  725. kfree(mem->mm_node);
  726. mem->mm_node = NULL;
  727. }
  728. static int
  729. nouveau_gart_manager_new(struct ttm_mem_type_manager *man,
  730. struct ttm_buffer_object *bo,
  731. struct ttm_placement *placement,
  732. struct ttm_mem_reg *mem)
  733. {
  734. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  735. struct nouveau_mem *node;
  736. if (unlikely((mem->num_pages << PAGE_SHIFT) >=
  737. dev_priv->gart_info.aper_size))
  738. return -ENOMEM;
  739. node = kzalloc(sizeof(*node), GFP_KERNEL);
  740. if (!node)
  741. return -ENOMEM;
  742. node->page_shift = 12;
  743. mem->mm_node = node;
  744. mem->start = 0;
  745. return 0;
  746. }
  747. void
  748. nouveau_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  749. {
  750. }
  751. const struct ttm_mem_type_manager_func nouveau_gart_manager = {
  752. nouveau_gart_manager_init,
  753. nouveau_gart_manager_fini,
  754. nouveau_gart_manager_new,
  755. nouveau_gart_manager_del,
  756. nouveau_gart_manager_debug
  757. };