1771fe6b9SJerome Glisse /*
2771fe6b9SJerome Glisse * Copyright 2008 Advanced Micro Devices, Inc.
3771fe6b9SJerome Glisse * Copyright 2008 Red Hat Inc.
4771fe6b9SJerome Glisse * Copyright 2009 Jerome Glisse.
5771fe6b9SJerome Glisse *
6771fe6b9SJerome Glisse * Permission is hereby granted, free of charge, to any person obtaining a
7771fe6b9SJerome Glisse * copy of this software and associated documentation files (the "Software"),
8771fe6b9SJerome Glisse * to deal in the Software without restriction, including without limitation
9771fe6b9SJerome Glisse * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10771fe6b9SJerome Glisse * and/or sell copies of the Software, and to permit persons to whom the
11771fe6b9SJerome Glisse * Software is furnished to do so, subject to the following conditions:
12771fe6b9SJerome Glisse *
13771fe6b9SJerome Glisse * The above copyright notice and this permission notice shall be included in
14771fe6b9SJerome Glisse * all copies or substantial portions of the Software.
15771fe6b9SJerome Glisse *
16771fe6b9SJerome Glisse * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17771fe6b9SJerome Glisse * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18771fe6b9SJerome Glisse * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19771fe6b9SJerome Glisse * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20771fe6b9SJerome Glisse * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21771fe6b9SJerome Glisse * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22771fe6b9SJerome Glisse * OTHER DEALINGS IN THE SOFTWARE.
23771fe6b9SJerome Glisse *
24771fe6b9SJerome Glisse * Authors: Dave Airlie
25771fe6b9SJerome Glisse * Alex Deucher
26771fe6b9SJerome Glisse * Jerome Glisse
27771fe6b9SJerome Glisse */
28c182615fSSam Ravnborg
29771fe6b9SJerome Glisse #include "radeon.h"
30e6990375SDaniel Vetter #include "radeon_asic.h"
31f0ed1f65SJerome Glisse #include "atom.h"
32f0ed1f65SJerome Glisse #include "r520d.h"
33771fe6b9SJerome Glisse
34f0ed1f65SJerome Glisse /* This files gather functions specifics to: r520,rv530,rv560,rv570,r580 */
35771fe6b9SJerome Glisse
r520_mc_wait_for_idle(struct radeon_device * rdev)3689e5181fSAlex Deucher int r520_mc_wait_for_idle(struct radeon_device *rdev)
37771fe6b9SJerome Glisse {
38771fe6b9SJerome Glisse unsigned i;
39771fe6b9SJerome Glisse uint32_t tmp;
40771fe6b9SJerome Glisse
41771fe6b9SJerome Glisse for (i = 0; i < rdev->usec_timeout; i++) {
42771fe6b9SJerome Glisse /* read MC_STATUS */
43771fe6b9SJerome Glisse tmp = RREG32_MC(R520_MC_STATUS);
44771fe6b9SJerome Glisse if (tmp & R520_MC_STATUS_IDLE) {
45771fe6b9SJerome Glisse return 0;
46771fe6b9SJerome Glisse }
470e1a351dSSam Ravnborg udelay(1);
48771fe6b9SJerome Glisse }
49771fe6b9SJerome Glisse return -1;
50771fe6b9SJerome Glisse }
51771fe6b9SJerome Glisse
r520_gpu_init(struct radeon_device * rdev)52f0ed1f65SJerome Glisse static void r520_gpu_init(struct radeon_device *rdev)
53771fe6b9SJerome Glisse {
54771fe6b9SJerome Glisse unsigned pipe_select_current, gb_pipe_select, tmp;
55771fe6b9SJerome Glisse
56d39c3b89SJerome Glisse rv515_vga_render_disable(rdev);
57771fe6b9SJerome Glisse /*
58771fe6b9SJerome Glisse * DST_PIPE_CONFIG 0x170C
59771fe6b9SJerome Glisse * GB_TILE_CONFIG 0x4018
60771fe6b9SJerome Glisse * GB_FIFO_SIZE 0x4024
61771fe6b9SJerome Glisse * GB_PIPE_SELECT 0x402C
62771fe6b9SJerome Glisse * GB_PIPE_SELECT2 0x4124
63771fe6b9SJerome Glisse * Z_PIPE_SHIFT 0
64771fe6b9SJerome Glisse * Z_PIPE_MASK 0x000000003
65771fe6b9SJerome Glisse * GB_FIFO_SIZE2 0x4128
66771fe6b9SJerome Glisse * SC_SFIFO_SIZE_SHIFT 0
67771fe6b9SJerome Glisse * SC_SFIFO_SIZE_MASK 0x000000003
68771fe6b9SJerome Glisse * SC_MFIFO_SIZE_SHIFT 2
69771fe6b9SJerome Glisse * SC_MFIFO_SIZE_MASK 0x00000000C
70771fe6b9SJerome Glisse * FG_SFIFO_SIZE_SHIFT 4
71771fe6b9SJerome Glisse * FG_SFIFO_SIZE_MASK 0x000000030
72771fe6b9SJerome Glisse * ZB_MFIFO_SIZE_SHIFT 6
73771fe6b9SJerome Glisse * ZB_MFIFO_SIZE_MASK 0x0000000C0
74771fe6b9SJerome Glisse * GA_ENHANCE 0x4274
75771fe6b9SJerome Glisse * SU_REG_DEST 0x42C8
76771fe6b9SJerome Glisse */
77771fe6b9SJerome Glisse /* workaround for RV530 */
78771fe6b9SJerome Glisse if (rdev->family == CHIP_RV530) {
79771fe6b9SJerome Glisse WREG32(0x4128, 0xFF);
80771fe6b9SJerome Glisse }
81771fe6b9SJerome Glisse r420_pipes_init(rdev);
82d75ee3beSAlex Deucher gb_pipe_select = RREG32(R400_GB_PIPE_SELECT);
83d75ee3beSAlex Deucher tmp = RREG32(R300_DST_PIPE_CONFIG);
84771fe6b9SJerome Glisse pipe_select_current = (tmp >> 2) & 3;
85771fe6b9SJerome Glisse tmp = (1 << pipe_select_current) |
86771fe6b9SJerome Glisse (((gb_pipe_select >> 8) & 0xF) << 4);
87771fe6b9SJerome Glisse WREG32_PLL(0x000D, tmp);
88771fe6b9SJerome Glisse if (r520_mc_wait_for_idle(rdev)) {
897ca85295SJoe Perches pr_warn("Failed to wait MC idle while programming pipes. Bad things might happen.\n");
90771fe6b9SJerome Glisse }
91771fe6b9SJerome Glisse }
92771fe6b9SJerome Glisse
r520_vram_get_type(struct radeon_device * rdev)93771fe6b9SJerome Glisse static void r520_vram_get_type(struct radeon_device *rdev)
94771fe6b9SJerome Glisse {
95771fe6b9SJerome Glisse uint32_t tmp;
96771fe6b9SJerome Glisse
97771fe6b9SJerome Glisse rdev->mc.vram_width = 128;
98771fe6b9SJerome Glisse rdev->mc.vram_is_ddr = true;
99771fe6b9SJerome Glisse tmp = RREG32_MC(R520_MC_CNTL0);
100771fe6b9SJerome Glisse switch ((tmp & R520_MEM_NUM_CHANNELS_MASK) >> R520_MEM_NUM_CHANNELS_SHIFT) {
101771fe6b9SJerome Glisse case 0:
102771fe6b9SJerome Glisse rdev->mc.vram_width = 32;
103771fe6b9SJerome Glisse break;
104771fe6b9SJerome Glisse case 1:
105771fe6b9SJerome Glisse rdev->mc.vram_width = 64;
106771fe6b9SJerome Glisse break;
107771fe6b9SJerome Glisse case 2:
108771fe6b9SJerome Glisse rdev->mc.vram_width = 128;
109771fe6b9SJerome Glisse break;
110771fe6b9SJerome Glisse case 3:
111771fe6b9SJerome Glisse rdev->mc.vram_width = 256;
112771fe6b9SJerome Glisse break;
113771fe6b9SJerome Glisse default:
114771fe6b9SJerome Glisse rdev->mc.vram_width = 128;
115771fe6b9SJerome Glisse break;
116771fe6b9SJerome Glisse }
117771fe6b9SJerome Glisse if (tmp & R520_MC_CHANNEL_SIZE)
118771fe6b9SJerome Glisse rdev->mc.vram_width *= 2;
119771fe6b9SJerome Glisse }
120771fe6b9SJerome Glisse
r520_mc_init(struct radeon_device * rdev)1211109ca09SLauri Kasanen static void r520_mc_init(struct radeon_device *rdev)
122771fe6b9SJerome Glisse {
123c93bb85bSJerome Glisse
124771fe6b9SJerome Glisse r520_vram_get_type(rdev);
1252a0f8918SDave Airlie r100_vram_init_sizes(rdev);
126d594e46aSJerome Glisse radeon_vram_location(rdev, &rdev->mc, 0);
1278d369bb1SAlex Deucher rdev->mc.gtt_base_align = 0;
128d594e46aSJerome Glisse if (!(rdev->flags & RADEON_IS_AGP))
129d594e46aSJerome Glisse radeon_gtt_location(rdev, &rdev->mc);
130f47299c5SAlex Deucher radeon_update_bandwidth_info(rdev);
131c93bb85bSJerome Glisse }
132c93bb85bSJerome Glisse
r520_mc_program(struct radeon_device * rdev)1331109ca09SLauri Kasanen static void r520_mc_program(struct radeon_device *rdev)
134c93bb85bSJerome Glisse {
135f0ed1f65SJerome Glisse struct rv515_mc_save save;
136f0ed1f65SJerome Glisse
137f0ed1f65SJerome Glisse /* Stops all mc clients */
138f0ed1f65SJerome Glisse rv515_mc_stop(rdev, &save);
139f0ed1f65SJerome Glisse
140f0ed1f65SJerome Glisse /* Wait for mc idle */
141f0ed1f65SJerome Glisse if (r520_mc_wait_for_idle(rdev))
142f0ed1f65SJerome Glisse dev_warn(rdev->dev, "Wait MC idle timeout before updating MC.\n");
143f0ed1f65SJerome Glisse /* Write VRAM size in case we are limiting it */
144f0ed1f65SJerome Glisse WREG32(R_0000F8_CONFIG_MEMSIZE, rdev->mc.real_vram_size);
145f0ed1f65SJerome Glisse /* Program MC, should be a 32bits limited address space */
146f0ed1f65SJerome Glisse WREG32_MC(R_000004_MC_FB_LOCATION,
147f0ed1f65SJerome Glisse S_000004_MC_FB_START(rdev->mc.vram_start >> 16) |
148f0ed1f65SJerome Glisse S_000004_MC_FB_TOP(rdev->mc.vram_end >> 16));
149f0ed1f65SJerome Glisse WREG32(R_000134_HDP_FB_LOCATION,
150f0ed1f65SJerome Glisse S_000134_HDP_FB_START(rdev->mc.vram_start >> 16));
151f0ed1f65SJerome Glisse if (rdev->flags & RADEON_IS_AGP) {
152f0ed1f65SJerome Glisse WREG32_MC(R_000005_MC_AGP_LOCATION,
153f0ed1f65SJerome Glisse S_000005_MC_AGP_START(rdev->mc.gtt_start >> 16) |
154f0ed1f65SJerome Glisse S_000005_MC_AGP_TOP(rdev->mc.gtt_end >> 16));
155f0ed1f65SJerome Glisse WREG32_MC(R_000006_AGP_BASE, lower_32_bits(rdev->mc.agp_base));
156f0ed1f65SJerome Glisse WREG32_MC(R_000007_AGP_BASE_2,
157f0ed1f65SJerome Glisse S_000007_AGP_BASE_ADDR_2(upper_32_bits(rdev->mc.agp_base)));
158f0ed1f65SJerome Glisse } else {
159f0ed1f65SJerome Glisse WREG32_MC(R_000005_MC_AGP_LOCATION, 0xFFFFFFFF);
160f0ed1f65SJerome Glisse WREG32_MC(R_000006_AGP_BASE, 0);
161f0ed1f65SJerome Glisse WREG32_MC(R_000007_AGP_BASE_2, 0);
162f0ed1f65SJerome Glisse }
163f0ed1f65SJerome Glisse
164f0ed1f65SJerome Glisse rv515_mc_resume(rdev, &save);
165f0ed1f65SJerome Glisse }
166f0ed1f65SJerome Glisse
r520_startup(struct radeon_device * rdev)167f0ed1f65SJerome Glisse static int r520_startup(struct radeon_device *rdev)
168f0ed1f65SJerome Glisse {
169f0ed1f65SJerome Glisse int r;
170f0ed1f65SJerome Glisse
171f0ed1f65SJerome Glisse r520_mc_program(rdev);
172f0ed1f65SJerome Glisse /* Resume clock */
173f0ed1f65SJerome Glisse rv515_clock_startup(rdev);
174f0ed1f65SJerome Glisse /* Initialize GPU configuration (# pipes, ...) */
175f0ed1f65SJerome Glisse r520_gpu_init(rdev);
176f0ed1f65SJerome Glisse /* Initialize GART (initialize after TTM so we can allocate
177f0ed1f65SJerome Glisse * memory through TTM but finalize after TTM) */
178f0ed1f65SJerome Glisse if (rdev->flags & RADEON_IS_PCIE) {
179f0ed1f65SJerome Glisse r = rv370_pcie_gart_enable(rdev);
180f0ed1f65SJerome Glisse if (r)
181f0ed1f65SJerome Glisse return r;
182f0ed1f65SJerome Glisse }
183724c80e1SAlex Deucher
184724c80e1SAlex Deucher /* allocate wb buffer */
185724c80e1SAlex Deucher r = radeon_wb_init(rdev);
186724c80e1SAlex Deucher if (r)
187724c80e1SAlex Deucher return r;
188724c80e1SAlex Deucher
18930eb77f4SJerome Glisse r = radeon_fence_driver_start_ring(rdev, RADEON_RING_TYPE_GFX_INDEX);
19030eb77f4SJerome Glisse if (r) {
19130eb77f4SJerome Glisse dev_err(rdev->dev, "failed initializing CP fences (%d).\n", r);
19230eb77f4SJerome Glisse return r;
19330eb77f4SJerome Glisse }
19430eb77f4SJerome Glisse
195f0ed1f65SJerome Glisse /* Enable IRQ */
196e49f3959SAdis Hamzić if (!rdev->irq.installed) {
197e49f3959SAdis Hamzić r = radeon_irq_kms_init(rdev);
198e49f3959SAdis Hamzić if (r)
199e49f3959SAdis Hamzić return r;
200e49f3959SAdis Hamzić }
201e49f3959SAdis Hamzić
202ac447df4SJerome Glisse rs600_irq_set(rdev);
203cafe6609SJerome Glisse rdev->config.r300.hdp_cntl = RREG32(RADEON_HOST_PATH_CNTL);
204f0ed1f65SJerome Glisse /* 1M ring buffer */
205f0ed1f65SJerome Glisse r = r100_cp_init(rdev, 1024 * 1024);
206f0ed1f65SJerome Glisse if (r) {
207ec4f2ac4SPaul Bolle dev_err(rdev->dev, "failed initializing CP (%d).\n", r);
208f0ed1f65SJerome Glisse return r;
209f0ed1f65SJerome Glisse }
210b15ba512SJerome Glisse
2112898c348SChristian König r = radeon_ib_pool_init(rdev);
2122898c348SChristian König if (r) {
2132898c348SChristian König dev_err(rdev->dev, "IB initialization failed (%d).\n", r);
214b15ba512SJerome Glisse return r;
2152898c348SChristian König }
216b15ba512SJerome Glisse
217f0ed1f65SJerome Glisse return 0;
218f0ed1f65SJerome Glisse }
219f0ed1f65SJerome Glisse
r520_resume(struct radeon_device * rdev)220f0ed1f65SJerome Glisse int r520_resume(struct radeon_device *rdev)
221f0ed1f65SJerome Glisse {
2226b7746e8SJerome Glisse int r;
2236b7746e8SJerome Glisse
224f0ed1f65SJerome Glisse /* Make sur GART are not working */
225f0ed1f65SJerome Glisse if (rdev->flags & RADEON_IS_PCIE)
226f0ed1f65SJerome Glisse rv370_pcie_gart_disable(rdev);
227f0ed1f65SJerome Glisse /* Resume clock before doing reset */
228f0ed1f65SJerome Glisse rv515_clock_startup(rdev);
229f0ed1f65SJerome Glisse /* Reset gpu before posting otherwise ATOM will enter infinite loop */
230a2d07b74SJerome Glisse if (radeon_asic_reset(rdev)) {
231f0ed1f65SJerome Glisse dev_warn(rdev->dev, "GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
232f0ed1f65SJerome Glisse RREG32(R_000E40_RBBM_STATUS),
233f0ed1f65SJerome Glisse RREG32(R_0007C0_CP_STAT));
234f0ed1f65SJerome Glisse }
235f0ed1f65SJerome Glisse /* post */
236f0ed1f65SJerome Glisse atom_asic_init(rdev->mode_info.atom_context);
237f0ed1f65SJerome Glisse /* Resume clock after posting */
238f0ed1f65SJerome Glisse rv515_clock_startup(rdev);
239550e2d92SDave Airlie /* Initialize surface registers */
240550e2d92SDave Airlie radeon_surface_init(rdev);
241b15ba512SJerome Glisse
242b15ba512SJerome Glisse rdev->accel_working = true;
2436b7746e8SJerome Glisse r = r520_startup(rdev);
2446b7746e8SJerome Glisse if (r) {
2456b7746e8SJerome Glisse rdev->accel_working = false;
2466b7746e8SJerome Glisse }
2476b7746e8SJerome Glisse return r;
248771fe6b9SJerome Glisse }
249d39c3b89SJerome Glisse
r520_init(struct radeon_device * rdev)250d39c3b89SJerome Glisse int r520_init(struct radeon_device *rdev)
251d39c3b89SJerome Glisse {
252f0ed1f65SJerome Glisse int r;
253f0ed1f65SJerome Glisse
254f0ed1f65SJerome Glisse /* Initialize scratch registers */
255f0ed1f65SJerome Glisse radeon_scratch_init(rdev);
256f0ed1f65SJerome Glisse /* Initialize surface registers */
257f0ed1f65SJerome Glisse radeon_surface_init(rdev);
2584c712e6cSDave Airlie /* restore some register to sane defaults */
2594c712e6cSDave Airlie r100_restore_sanity(rdev);
260f0ed1f65SJerome Glisse /* TODO: disable VGA need to use VGA request */
261f0ed1f65SJerome Glisse /* BIOS*/
262f0ed1f65SJerome Glisse if (!radeon_get_bios(rdev)) {
263f0ed1f65SJerome Glisse if (ASIC_IS_AVIVO(rdev))
264f0ed1f65SJerome Glisse return -EINVAL;
265f0ed1f65SJerome Glisse }
266f0ed1f65SJerome Glisse if (rdev->is_atom_bios) {
267f0ed1f65SJerome Glisse r = radeon_atombios_init(rdev);
268f0ed1f65SJerome Glisse if (r)
269f0ed1f65SJerome Glisse return r;
270f0ed1f65SJerome Glisse } else {
271f0ed1f65SJerome Glisse dev_err(rdev->dev, "Expecting atombios for RV515 GPU\n");
272f0ed1f65SJerome Glisse return -EINVAL;
273f0ed1f65SJerome Glisse }
274f0ed1f65SJerome Glisse /* Reset gpu before posting otherwise ATOM will enter infinite loop */
275a2d07b74SJerome Glisse if (radeon_asic_reset(rdev)) {
276f0ed1f65SJerome Glisse dev_warn(rdev->dev,
277f0ed1f65SJerome Glisse "GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
278f0ed1f65SJerome Glisse RREG32(R_000E40_RBBM_STATUS),
279f0ed1f65SJerome Glisse RREG32(R_0007C0_CP_STAT));
280f0ed1f65SJerome Glisse }
281f0ed1f65SJerome Glisse /* check if cards are posted or not */
28272542d77SDave Airlie if (radeon_boot_test_post_card(rdev) == false)
28372542d77SDave Airlie return -EINVAL;
28472542d77SDave Airlie
285f0ed1f65SJerome Glisse if (!radeon_card_posted(rdev) && rdev->bios) {
286f0ed1f65SJerome Glisse DRM_INFO("GPU not posted. posting now...\n");
287f0ed1f65SJerome Glisse atom_asic_init(rdev->mode_info.atom_context);
288f0ed1f65SJerome Glisse }
289f0ed1f65SJerome Glisse /* Initialize clocks */
290*fb1b5e1dSWu Hoi Pok radeon_get_clock_info(rdev_to_drm(rdev));
291d594e46aSJerome Glisse /* initialize AGP */
292d594e46aSJerome Glisse if (rdev->flags & RADEON_IS_AGP) {
293d594e46aSJerome Glisse r = radeon_agp_init(rdev);
294d594e46aSJerome Glisse if (r) {
295d594e46aSJerome Glisse radeon_agp_disable(rdev);
296d594e46aSJerome Glisse }
297d594e46aSJerome Glisse }
298d594e46aSJerome Glisse /* initialize memory controller */
299d594e46aSJerome Glisse r520_mc_init(rdev);
300f0ed1f65SJerome Glisse rv515_debugfs(rdev);
301f0ed1f65SJerome Glisse /* Fence driver */
302519424d7SBernard Zhao radeon_fence_driver_init(rdev);
303f0ed1f65SJerome Glisse /* Memory manager */
3044c788679SJerome Glisse r = radeon_bo_init(rdev);
305f0ed1f65SJerome Glisse if (r)
306f0ed1f65SJerome Glisse return r;
307f0ed1f65SJerome Glisse r = rv370_pcie_gart_init(rdev);
308f0ed1f65SJerome Glisse if (r)
309f0ed1f65SJerome Glisse return r;
310d39c3b89SJerome Glisse rv515_set_safe_registers(rdev);
311b15ba512SJerome Glisse
3126c7bcceaSAlex Deucher /* Initialize power management */
3136c7bcceaSAlex Deucher radeon_pm_init(rdev);
3146c7bcceaSAlex Deucher
315f0ed1f65SJerome Glisse rdev->accel_working = true;
316f0ed1f65SJerome Glisse r = r520_startup(rdev);
317f0ed1f65SJerome Glisse if (r) {
318f0ed1f65SJerome Glisse /* Somethings want wront with the accel init stop accel */
319f0ed1f65SJerome Glisse dev_err(rdev->dev, "Disabling GPU acceleration\n");
320f0ed1f65SJerome Glisse r100_cp_fini(rdev);
321724c80e1SAlex Deucher radeon_wb_fini(rdev);
3222898c348SChristian König radeon_ib_pool_fini(rdev);
323655efd3dSJerome Glisse radeon_irq_kms_fini(rdev);
324f0ed1f65SJerome Glisse rv370_pcie_gart_fini(rdev);
325f0ed1f65SJerome Glisse radeon_agp_fini(rdev);
326f0ed1f65SJerome Glisse rdev->accel_working = false;
327f0ed1f65SJerome Glisse }
328d39c3b89SJerome Glisse return 0;
329d39c3b89SJerome Glisse }
330