r420.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <linux/pci.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/slab.h>
  31. #include <drm/drm_device.h>
  32. #include <drm/drm_file.h>
  33. #include "atom.h"
  34. #include "r100d.h"
  35. #include "r420_reg_safe.h"
  36. #include "r420d.h"
  37. #include "radeon.h"
  38. #include "radeon_asic.h"
  39. #include "radeon_reg.h"
  40. void r420_pm_init_profile(struct radeon_device *rdev)
  41. {
  42. /* default */
  43. rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
  44. rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
  45. rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_cm_idx = 0;
  46. rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_cm_idx = 0;
  47. /* low sh */
  48. rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_ps_idx = 0;
  49. rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_ps_idx = 0;
  50. rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_cm_idx = 0;
  51. rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_cm_idx = 0;
  52. /* mid sh */
  53. rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_ps_idx = 0;
  54. rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_ps_idx = 1;
  55. rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_cm_idx = 0;
  56. rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_cm_idx = 0;
  57. /* high sh */
  58. rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_ps_idx = 0;
  59. rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
  60. rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_cm_idx = 0;
  61. rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_cm_idx = 0;
  62. /* low mh */
  63. rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_ps_idx = 0;
  64. rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
  65. rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_cm_idx = 0;
  66. rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_cm_idx = 0;
  67. /* mid mh */
  68. rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_ps_idx = 0;
  69. rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
  70. rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_cm_idx = 0;
  71. rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_cm_idx = 0;
  72. /* high mh */
  73. rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_ps_idx = 0;
  74. rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
  75. rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_cm_idx = 0;
  76. rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx = 0;
  77. }
  78. static void r420_set_reg_safe(struct radeon_device *rdev)
  79. {
  80. rdev->config.r300.reg_safe_bm = r420_reg_safe_bm;
  81. rdev->config.r300.reg_safe_bm_size = ARRAY_SIZE(r420_reg_safe_bm);
  82. }
  83. void r420_pipes_init(struct radeon_device *rdev)
  84. {
  85. unsigned tmp;
  86. unsigned gb_pipe_select;
  87. unsigned num_pipes;
  88. /* GA_ENHANCE workaround TCL deadlock issue */
  89. WREG32(R300_GA_ENHANCE, R300_GA_DEADLOCK_CNTL | R300_GA_FASTSYNC_CNTL |
  90. (1 << 2) | (1 << 3));
  91. /* add idle wait as per freedesktop.org bug 24041 */
  92. if (r100_gui_wait_for_idle(rdev)) {
  93. pr_warn("Failed to wait GUI idle while programming pipes. Bad things might happen.\n");
  94. }
  95. /* get max number of pipes */
  96. gb_pipe_select = RREG32(R400_GB_PIPE_SELECT);
  97. num_pipes = ((gb_pipe_select >> 12) & 3) + 1;
  98. /* SE chips have 1 pipe */
  99. if ((rdev->pdev->device == 0x5e4c) ||
  100. (rdev->pdev->device == 0x5e4f))
  101. num_pipes = 1;
  102. rdev->num_gb_pipes = num_pipes;
  103. tmp = 0;
  104. switch (num_pipes) {
  105. default:
  106. /* force to 1 pipe */
  107. num_pipes = 1;
  108. fallthrough;
  109. case 1:
  110. tmp = (0 << 1);
  111. break;
  112. case 2:
  113. tmp = (3 << 1);
  114. break;
  115. case 3:
  116. tmp = (6 << 1);
  117. break;
  118. case 4:
  119. tmp = (7 << 1);
  120. break;
  121. }
  122. WREG32(R500_SU_REG_DEST, (1 << num_pipes) - 1);
  123. /* Sub pixel 1/12 so we can have 4K rendering according to doc */
  124. tmp |= R300_TILE_SIZE_16 | R300_ENABLE_TILING;
  125. WREG32(R300_GB_TILE_CONFIG, tmp);
  126. if (r100_gui_wait_for_idle(rdev)) {
  127. pr_warn("Failed to wait GUI idle while programming pipes. Bad things might happen.\n");
  128. }
  129. tmp = RREG32(R300_DST_PIPE_CONFIG);
  130. WREG32(R300_DST_PIPE_CONFIG, tmp | R300_PIPE_AUTO_CONFIG);
  131. WREG32(R300_RB2D_DSTCACHE_MODE,
  132. RREG32(R300_RB2D_DSTCACHE_MODE) |
  133. R300_DC_AUTOFLUSH_ENABLE |
  134. R300_DC_DC_DISABLE_IGNORE_PE);
  135. if (r100_gui_wait_for_idle(rdev)) {
  136. pr_warn("Failed to wait GUI idle while programming pipes. Bad things might happen.\n");
  137. }
  138. if (rdev->family == CHIP_RV530) {
  139. tmp = RREG32(RV530_GB_PIPE_SELECT2);
  140. if ((tmp & 3) == 3)
  141. rdev->num_z_pipes = 2;
  142. else
  143. rdev->num_z_pipes = 1;
  144. } else
  145. rdev->num_z_pipes = 1;
  146. DRM_INFO("radeon: %d quad pipes, %d z pipes initialized.\n",
  147. rdev->num_gb_pipes, rdev->num_z_pipes);
  148. }
  149. u32 r420_mc_rreg(struct radeon_device *rdev, u32 reg)
  150. {
  151. unsigned long flags;
  152. u32 r;
  153. spin_lock_irqsave(&rdev->mc_idx_lock, flags);
  154. WREG32(R_0001F8_MC_IND_INDEX, S_0001F8_MC_IND_ADDR(reg));
  155. r = RREG32(R_0001FC_MC_IND_DATA);
  156. spin_unlock_irqrestore(&rdev->mc_idx_lock, flags);
  157. return r;
  158. }
  159. void r420_mc_wreg(struct radeon_device *rdev, u32 reg, u32 v)
  160. {
  161. unsigned long flags;
  162. spin_lock_irqsave(&rdev->mc_idx_lock, flags);
  163. WREG32(R_0001F8_MC_IND_INDEX, S_0001F8_MC_IND_ADDR(reg) |
  164. S_0001F8_MC_IND_WR_EN(1));
  165. WREG32(R_0001FC_MC_IND_DATA, v);
  166. spin_unlock_irqrestore(&rdev->mc_idx_lock, flags);
  167. }
  168. static void r420_debugfs(struct radeon_device *rdev)
  169. {
  170. r100_debugfs_rbbm_init(rdev);
  171. r420_debugfs_pipes_info_init(rdev);
  172. }
  173. static void r420_clock_resume(struct radeon_device *rdev)
  174. {
  175. u32 sclk_cntl;
  176. if (radeon_dynclks != -1 && radeon_dynclks)
  177. radeon_atom_set_clock_gating(rdev, 1);
  178. sclk_cntl = RREG32_PLL(R_00000D_SCLK_CNTL);
  179. sclk_cntl |= S_00000D_FORCE_CP(1) | S_00000D_FORCE_VIP(1);
  180. if (rdev->family == CHIP_R420)
  181. sclk_cntl |= S_00000D_FORCE_PX(1) | S_00000D_FORCE_TX(1);
  182. WREG32_PLL(R_00000D_SCLK_CNTL, sclk_cntl);
  183. }
  184. static void r420_cp_errata_init(struct radeon_device *rdev)
  185. {
  186. int r;
  187. struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
  188. /* RV410 and R420 can lock up if CP DMA to host memory happens
  189. * while the 2D engine is busy.
  190. *
  191. * The proper workaround is to queue a RESYNC at the beginning
  192. * of the CP init, apparently.
  193. */
  194. radeon_scratch_get(rdev, &rdev->config.r300.resync_scratch);
  195. r = radeon_ring_lock(rdev, ring, 8);
  196. WARN_ON(r);
  197. radeon_ring_write(ring, PACKET0(R300_CP_RESYNC_ADDR, 1));
  198. radeon_ring_write(ring, rdev->config.r300.resync_scratch);
  199. radeon_ring_write(ring, 0xDEADBEEF);
  200. radeon_ring_unlock_commit(rdev, ring, false);
  201. }
  202. static void r420_cp_errata_fini(struct radeon_device *rdev)
  203. {
  204. int r;
  205. struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
  206. /* Catch the RESYNC we dispatched all the way back,
  207. * at the very beginning of the CP init.
  208. */
  209. r = radeon_ring_lock(rdev, ring, 8);
  210. WARN_ON(r);
  211. radeon_ring_write(ring, PACKET0(R300_RB3D_DSTCACHE_CTLSTAT, 0));
  212. radeon_ring_write(ring, R300_RB3D_DC_FINISH);
  213. radeon_ring_unlock_commit(rdev, ring, false);
  214. radeon_scratch_free(rdev, rdev->config.r300.resync_scratch);
  215. }
  216. static int r420_startup(struct radeon_device *rdev)
  217. {
  218. int r;
  219. /* set common regs */
  220. r100_set_common_regs(rdev);
  221. /* program mc */
  222. r300_mc_program(rdev);
  223. /* Resume clock */
  224. r420_clock_resume(rdev);
  225. /* Initialize GART (initialize after TTM so we can allocate
  226. * memory through TTM but finalize after TTM) */
  227. if (rdev->flags & RADEON_IS_PCIE) {
  228. r = rv370_pcie_gart_enable(rdev);
  229. if (r)
  230. return r;
  231. }
  232. if (rdev->flags & RADEON_IS_PCI) {
  233. r = r100_pci_gart_enable(rdev);
  234. if (r)
  235. return r;
  236. }
  237. r420_pipes_init(rdev);
  238. /* allocate wb buffer */
  239. r = radeon_wb_init(rdev);
  240. if (r)
  241. return r;
  242. r = radeon_fence_driver_start_ring(rdev, RADEON_RING_TYPE_GFX_INDEX);
  243. if (r) {
  244. dev_err(rdev->dev, "failed initializing CP fences (%d).\n", r);
  245. return r;
  246. }
  247. /* Enable IRQ */
  248. if (!rdev->irq.installed) {
  249. r = radeon_irq_kms_init(rdev);
  250. if (r)
  251. return r;
  252. }
  253. r100_irq_set(rdev);
  254. rdev->config.r300.hdp_cntl = RREG32(RADEON_HOST_PATH_CNTL);
  255. /* 1M ring buffer */
  256. r = r100_cp_init(rdev, 1024 * 1024);
  257. if (r) {
  258. dev_err(rdev->dev, "failed initializing CP (%d).\n", r);
  259. return r;
  260. }
  261. r420_cp_errata_init(rdev);
  262. r = radeon_ib_pool_init(rdev);
  263. if (r) {
  264. dev_err(rdev->dev, "IB initialization failed (%d).\n", r);
  265. return r;
  266. }
  267. return 0;
  268. }
  269. int r420_resume(struct radeon_device *rdev)
  270. {
  271. int r;
  272. /* Make sur GART are not working */
  273. if (rdev->flags & RADEON_IS_PCIE)
  274. rv370_pcie_gart_disable(rdev);
  275. if (rdev->flags & RADEON_IS_PCI)
  276. r100_pci_gart_disable(rdev);
  277. /* Resume clock before doing reset */
  278. r420_clock_resume(rdev);
  279. /* Reset gpu before posting otherwise ATOM will enter infinite loop */
  280. if (radeon_asic_reset(rdev)) {
  281. dev_warn(rdev->dev, "GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
  282. RREG32(R_000E40_RBBM_STATUS),
  283. RREG32(R_0007C0_CP_STAT));
  284. }
  285. /* check if cards are posted or not */
  286. if (rdev->is_atom_bios) {
  287. atom_asic_init(rdev->mode_info.atom_context);
  288. } else {
  289. radeon_combios_asic_init(rdev->ddev);
  290. }
  291. /* Resume clock after posting */
  292. r420_clock_resume(rdev);
  293. /* Initialize surface registers */
  294. radeon_surface_init(rdev);
  295. rdev->accel_working = true;
  296. r = r420_startup(rdev);
  297. if (r) {
  298. rdev->accel_working = false;
  299. }
  300. return r;
  301. }
  302. int r420_suspend(struct radeon_device *rdev)
  303. {
  304. radeon_pm_suspend(rdev);
  305. r420_cp_errata_fini(rdev);
  306. r100_cp_disable(rdev);
  307. radeon_wb_disable(rdev);
  308. r100_irq_disable(rdev);
  309. if (rdev->flags & RADEON_IS_PCIE)
  310. rv370_pcie_gart_disable(rdev);
  311. if (rdev->flags & RADEON_IS_PCI)
  312. r100_pci_gart_disable(rdev);
  313. return 0;
  314. }
  315. void r420_fini(struct radeon_device *rdev)
  316. {
  317. radeon_pm_fini(rdev);
  318. r100_cp_fini(rdev);
  319. radeon_wb_fini(rdev);
  320. radeon_ib_pool_fini(rdev);
  321. radeon_gem_fini(rdev);
  322. if (rdev->flags & RADEON_IS_PCIE)
  323. rv370_pcie_gart_fini(rdev);
  324. if (rdev->flags & RADEON_IS_PCI)
  325. r100_pci_gart_fini(rdev);
  326. radeon_agp_fini(rdev);
  327. radeon_irq_kms_fini(rdev);
  328. radeon_fence_driver_fini(rdev);
  329. radeon_bo_fini(rdev);
  330. if (rdev->is_atom_bios) {
  331. radeon_atombios_fini(rdev);
  332. } else {
  333. radeon_combios_fini(rdev);
  334. }
  335. kfree(rdev->bios);
  336. rdev->bios = NULL;
  337. }
  338. int r420_init(struct radeon_device *rdev)
  339. {
  340. int r;
  341. /* Initialize scratch registers */
  342. radeon_scratch_init(rdev);
  343. /* Initialize surface registers */
  344. radeon_surface_init(rdev);
  345. /* TODO: disable VGA need to use VGA request */
  346. /* restore some register to sane defaults */
  347. r100_restore_sanity(rdev);
  348. /* BIOS*/
  349. if (!radeon_get_bios(rdev)) {
  350. if (ASIC_IS_AVIVO(rdev))
  351. return -EINVAL;
  352. }
  353. if (rdev->is_atom_bios) {
  354. r = radeon_atombios_init(rdev);
  355. if (r) {
  356. return r;
  357. }
  358. } else {
  359. r = radeon_combios_init(rdev);
  360. if (r) {
  361. return r;
  362. }
  363. }
  364. /* Reset gpu before posting otherwise ATOM will enter infinite loop */
  365. if (radeon_asic_reset(rdev)) {
  366. dev_warn(rdev->dev,
  367. "GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
  368. RREG32(R_000E40_RBBM_STATUS),
  369. RREG32(R_0007C0_CP_STAT));
  370. }
  371. /* check if cards are posted or not */
  372. if (radeon_boot_test_post_card(rdev) == false)
  373. return -EINVAL;
  374. /* Initialize clocks */
  375. radeon_get_clock_info(rdev->ddev);
  376. /* initialize AGP */
  377. if (rdev->flags & RADEON_IS_AGP) {
  378. r = radeon_agp_init(rdev);
  379. if (r) {
  380. radeon_agp_disable(rdev);
  381. }
  382. }
  383. /* initialize memory controller */
  384. r300_mc_init(rdev);
  385. r420_debugfs(rdev);
  386. /* Fence driver */
  387. radeon_fence_driver_init(rdev);
  388. /* Memory manager */
  389. r = radeon_bo_init(rdev);
  390. if (r) {
  391. return r;
  392. }
  393. if (rdev->family == CHIP_R420)
  394. r100_enable_bm(rdev);
  395. if (rdev->flags & RADEON_IS_PCIE) {
  396. r = rv370_pcie_gart_init(rdev);
  397. if (r)
  398. return r;
  399. }
  400. if (rdev->flags & RADEON_IS_PCI) {
  401. r = r100_pci_gart_init(rdev);
  402. if (r)
  403. return r;
  404. }
  405. r420_set_reg_safe(rdev);
  406. /* Initialize power management */
  407. radeon_pm_init(rdev);
  408. rdev->accel_working = true;
  409. r = r420_startup(rdev);
  410. if (r) {
  411. /* Somethings want wront with the accel init stop accel */
  412. dev_err(rdev->dev, "Disabling GPU acceleration\n");
  413. r100_cp_fini(rdev);
  414. radeon_wb_fini(rdev);
  415. radeon_ib_pool_fini(rdev);
  416. radeon_irq_kms_fini(rdev);
  417. if (rdev->flags & RADEON_IS_PCIE)
  418. rv370_pcie_gart_fini(rdev);
  419. if (rdev->flags & RADEON_IS_PCI)
  420. r100_pci_gart_fini(rdev);
  421. radeon_agp_fini(rdev);
  422. rdev->accel_working = false;
  423. }
  424. return 0;
  425. }
  426. /*
  427. * Debugfs info
  428. */
  429. #if defined(CONFIG_DEBUG_FS)
  430. static int r420_debugfs_pipes_info_show(struct seq_file *m, void *unused)
  431. {
  432. struct radeon_device *rdev = (struct radeon_device *)m->private;
  433. uint32_t tmp;
  434. tmp = RREG32(R400_GB_PIPE_SELECT);
  435. seq_printf(m, "GB_PIPE_SELECT 0x%08x\n", tmp);
  436. tmp = RREG32(R300_GB_TILE_CONFIG);
  437. seq_printf(m, "GB_TILE_CONFIG 0x%08x\n", tmp);
  438. tmp = RREG32(R300_DST_PIPE_CONFIG);
  439. seq_printf(m, "DST_PIPE_CONFIG 0x%08x\n", tmp);
  440. return 0;
  441. }
  442. DEFINE_SHOW_ATTRIBUTE(r420_debugfs_pipes_info);
  443. #endif
  444. void r420_debugfs_pipes_info_init(struct radeon_device *rdev)
  445. {
  446. #if defined(CONFIG_DEBUG_FS)
  447. struct dentry *root = rdev->ddev->primary->debugfs_root;
  448. debugfs_create_file("r420_pipes_info", 0444, root, rdev,
  449. &r420_debugfs_pipes_info_fops);
  450. #endif
  451. }