• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Alex Deucher
23  */
24 #include <linux/firmware.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include "drmP.h"
29 #include "radeon.h"
30 #include "radeon_asic.h"
31 #include "radeon_drm.h"
32 #include "sid.h"
33 #include "atom.h"
34 #include "si_blit_shaders.h"
35 
36 #define SI_PFP_UCODE_SIZE 2144
37 #define SI_PM4_UCODE_SIZE 2144
38 #define SI_CE_UCODE_SIZE 2144
39 #define SI_RLC_UCODE_SIZE 2048
40 #define SI_MC_UCODE_SIZE 7769
41 
42 MODULE_FIRMWARE("radeon/TAHITI_pfp.bin");
43 MODULE_FIRMWARE("radeon/TAHITI_me.bin");
44 MODULE_FIRMWARE("radeon/TAHITI_ce.bin");
45 MODULE_FIRMWARE("radeon/TAHITI_mc.bin");
46 MODULE_FIRMWARE("radeon/TAHITI_rlc.bin");
47 MODULE_FIRMWARE("radeon/PITCAIRN_pfp.bin");
48 MODULE_FIRMWARE("radeon/PITCAIRN_me.bin");
49 MODULE_FIRMWARE("radeon/PITCAIRN_ce.bin");
50 MODULE_FIRMWARE("radeon/PITCAIRN_mc.bin");
51 MODULE_FIRMWARE("radeon/PITCAIRN_rlc.bin");
52 MODULE_FIRMWARE("radeon/VERDE_pfp.bin");
53 MODULE_FIRMWARE("radeon/VERDE_me.bin");
54 MODULE_FIRMWARE("radeon/VERDE_ce.bin");
55 MODULE_FIRMWARE("radeon/VERDE_mc.bin");
56 MODULE_FIRMWARE("radeon/VERDE_rlc.bin");
57 
58 extern int r600_ih_ring_alloc(struct radeon_device *rdev);
59 extern void r600_ih_ring_fini(struct radeon_device *rdev);
60 extern void evergreen_fix_pci_max_read_req_size(struct radeon_device *rdev);
61 extern void evergreen_mc_stop(struct radeon_device *rdev, struct evergreen_mc_save *save);
62 extern void evergreen_mc_resume(struct radeon_device *rdev, struct evergreen_mc_save *save);
63 extern u32 evergreen_get_number_of_dram_channels(struct radeon_device *rdev);
64 
65 /* get temperature in millidegrees */
si_get_temp(struct radeon_device * rdev)66 int si_get_temp(struct radeon_device *rdev)
67 {
68 	u32 temp;
69 	int actual_temp = 0;
70 
71 	temp = (RREG32(CG_MULT_THERMAL_STATUS) & CTF_TEMP_MASK) >>
72 		CTF_TEMP_SHIFT;
73 
74 	if (temp & 0x200)
75 		actual_temp = 255;
76 	else
77 		actual_temp = temp & 0x1ff;
78 
79 	actual_temp = (actual_temp * 1000);
80 
81 	return actual_temp;
82 }
83 
84 #define TAHITI_IO_MC_REGS_SIZE 36
85 
86 static const u32 tahiti_io_mc_regs[TAHITI_IO_MC_REGS_SIZE][2] = {
87 	{0x0000006f, 0x03044000},
88 	{0x00000070, 0x0480c018},
89 	{0x00000071, 0x00000040},
90 	{0x00000072, 0x01000000},
91 	{0x00000074, 0x000000ff},
92 	{0x00000075, 0x00143400},
93 	{0x00000076, 0x08ec0800},
94 	{0x00000077, 0x040000cc},
95 	{0x00000079, 0x00000000},
96 	{0x0000007a, 0x21000409},
97 	{0x0000007c, 0x00000000},
98 	{0x0000007d, 0xe8000000},
99 	{0x0000007e, 0x044408a8},
100 	{0x0000007f, 0x00000003},
101 	{0x00000080, 0x00000000},
102 	{0x00000081, 0x01000000},
103 	{0x00000082, 0x02000000},
104 	{0x00000083, 0x00000000},
105 	{0x00000084, 0xe3f3e4f4},
106 	{0x00000085, 0x00052024},
107 	{0x00000087, 0x00000000},
108 	{0x00000088, 0x66036603},
109 	{0x00000089, 0x01000000},
110 	{0x0000008b, 0x1c0a0000},
111 	{0x0000008c, 0xff010000},
112 	{0x0000008e, 0xffffefff},
113 	{0x0000008f, 0xfff3efff},
114 	{0x00000090, 0xfff3efbf},
115 	{0x00000094, 0x00101101},
116 	{0x00000095, 0x00000fff},
117 	{0x00000096, 0x00116fff},
118 	{0x00000097, 0x60010000},
119 	{0x00000098, 0x10010000},
120 	{0x00000099, 0x00006000},
121 	{0x0000009a, 0x00001000},
122 	{0x0000009f, 0x00a77400}
123 };
124 
125 static const u32 pitcairn_io_mc_regs[TAHITI_IO_MC_REGS_SIZE][2] = {
126 	{0x0000006f, 0x03044000},
127 	{0x00000070, 0x0480c018},
128 	{0x00000071, 0x00000040},
129 	{0x00000072, 0x01000000},
130 	{0x00000074, 0x000000ff},
131 	{0x00000075, 0x00143400},
132 	{0x00000076, 0x08ec0800},
133 	{0x00000077, 0x040000cc},
134 	{0x00000079, 0x00000000},
135 	{0x0000007a, 0x21000409},
136 	{0x0000007c, 0x00000000},
137 	{0x0000007d, 0xe8000000},
138 	{0x0000007e, 0x044408a8},
139 	{0x0000007f, 0x00000003},
140 	{0x00000080, 0x00000000},
141 	{0x00000081, 0x01000000},
142 	{0x00000082, 0x02000000},
143 	{0x00000083, 0x00000000},
144 	{0x00000084, 0xe3f3e4f4},
145 	{0x00000085, 0x00052024},
146 	{0x00000087, 0x00000000},
147 	{0x00000088, 0x66036603},
148 	{0x00000089, 0x01000000},
149 	{0x0000008b, 0x1c0a0000},
150 	{0x0000008c, 0xff010000},
151 	{0x0000008e, 0xffffefff},
152 	{0x0000008f, 0xfff3efff},
153 	{0x00000090, 0xfff3efbf},
154 	{0x00000094, 0x00101101},
155 	{0x00000095, 0x00000fff},
156 	{0x00000096, 0x00116fff},
157 	{0x00000097, 0x60010000},
158 	{0x00000098, 0x10010000},
159 	{0x00000099, 0x00006000},
160 	{0x0000009a, 0x00001000},
161 	{0x0000009f, 0x00a47400}
162 };
163 
164 static const u32 verde_io_mc_regs[TAHITI_IO_MC_REGS_SIZE][2] = {
165 	{0x0000006f, 0x03044000},
166 	{0x00000070, 0x0480c018},
167 	{0x00000071, 0x00000040},
168 	{0x00000072, 0x01000000},
169 	{0x00000074, 0x000000ff},
170 	{0x00000075, 0x00143400},
171 	{0x00000076, 0x08ec0800},
172 	{0x00000077, 0x040000cc},
173 	{0x00000079, 0x00000000},
174 	{0x0000007a, 0x21000409},
175 	{0x0000007c, 0x00000000},
176 	{0x0000007d, 0xe8000000},
177 	{0x0000007e, 0x044408a8},
178 	{0x0000007f, 0x00000003},
179 	{0x00000080, 0x00000000},
180 	{0x00000081, 0x01000000},
181 	{0x00000082, 0x02000000},
182 	{0x00000083, 0x00000000},
183 	{0x00000084, 0xe3f3e4f4},
184 	{0x00000085, 0x00052024},
185 	{0x00000087, 0x00000000},
186 	{0x00000088, 0x66036603},
187 	{0x00000089, 0x01000000},
188 	{0x0000008b, 0x1c0a0000},
189 	{0x0000008c, 0xff010000},
190 	{0x0000008e, 0xffffefff},
191 	{0x0000008f, 0xfff3efff},
192 	{0x00000090, 0xfff3efbf},
193 	{0x00000094, 0x00101101},
194 	{0x00000095, 0x00000fff},
195 	{0x00000096, 0x00116fff},
196 	{0x00000097, 0x60010000},
197 	{0x00000098, 0x10010000},
198 	{0x00000099, 0x00006000},
199 	{0x0000009a, 0x00001000},
200 	{0x0000009f, 0x00a37400}
201 };
202 
203 /* ucode loading */
si_mc_load_microcode(struct radeon_device * rdev)204 static int si_mc_load_microcode(struct radeon_device *rdev)
205 {
206 	const __be32 *fw_data;
207 	u32 running, blackout = 0;
208 	u32 *io_mc_regs;
209 	int i, ucode_size, regs_size;
210 
211 	if (!rdev->mc_fw)
212 		return -EINVAL;
213 
214 	switch (rdev->family) {
215 	case CHIP_TAHITI:
216 		io_mc_regs = (u32 *)&tahiti_io_mc_regs;
217 		ucode_size = SI_MC_UCODE_SIZE;
218 		regs_size = TAHITI_IO_MC_REGS_SIZE;
219 		break;
220 	case CHIP_PITCAIRN:
221 		io_mc_regs = (u32 *)&pitcairn_io_mc_regs;
222 		ucode_size = SI_MC_UCODE_SIZE;
223 		regs_size = TAHITI_IO_MC_REGS_SIZE;
224 		break;
225 	case CHIP_VERDE:
226 	default:
227 		io_mc_regs = (u32 *)&verde_io_mc_regs;
228 		ucode_size = SI_MC_UCODE_SIZE;
229 		regs_size = TAHITI_IO_MC_REGS_SIZE;
230 		break;
231 	}
232 
233 	running = RREG32(MC_SEQ_SUP_CNTL) & RUN_MASK;
234 
235 	if (running == 0) {
236 		if (running) {
237 			blackout = RREG32(MC_SHARED_BLACKOUT_CNTL);
238 			WREG32(MC_SHARED_BLACKOUT_CNTL, blackout | 1);
239 		}
240 
241 		/* reset the engine and set to writable */
242 		WREG32(MC_SEQ_SUP_CNTL, 0x00000008);
243 		WREG32(MC_SEQ_SUP_CNTL, 0x00000010);
244 
245 		/* load mc io regs */
246 		for (i = 0; i < regs_size; i++) {
247 			WREG32(MC_SEQ_IO_DEBUG_INDEX, io_mc_regs[(i << 1)]);
248 			WREG32(MC_SEQ_IO_DEBUG_DATA, io_mc_regs[(i << 1) + 1]);
249 		}
250 		/* load the MC ucode */
251 		fw_data = (const __be32 *)rdev->mc_fw->data;
252 		for (i = 0; i < ucode_size; i++)
253 			WREG32(MC_SEQ_SUP_PGM, be32_to_cpup(fw_data++));
254 
255 		/* put the engine back into the active state */
256 		WREG32(MC_SEQ_SUP_CNTL, 0x00000008);
257 		WREG32(MC_SEQ_SUP_CNTL, 0x00000004);
258 		WREG32(MC_SEQ_SUP_CNTL, 0x00000001);
259 
260 		/* wait for training to complete */
261 		for (i = 0; i < rdev->usec_timeout; i++) {
262 			if (RREG32(MC_SEQ_TRAIN_WAKEUP_CNTL) & TRAIN_DONE_D0)
263 				break;
264 			udelay(1);
265 		}
266 		for (i = 0; i < rdev->usec_timeout; i++) {
267 			if (RREG32(MC_SEQ_TRAIN_WAKEUP_CNTL) & TRAIN_DONE_D1)
268 				break;
269 			udelay(1);
270 		}
271 
272 		if (running)
273 			WREG32(MC_SHARED_BLACKOUT_CNTL, blackout);
274 	}
275 
276 	return 0;
277 }
278 
si_init_microcode(struct radeon_device * rdev)279 static int si_init_microcode(struct radeon_device *rdev)
280 {
281 	struct platform_device *pdev;
282 	const char *chip_name;
283 	const char *rlc_chip_name;
284 	size_t pfp_req_size, me_req_size, ce_req_size, rlc_req_size, mc_req_size;
285 	char fw_name[30];
286 	int err;
287 
288 	DRM_DEBUG("\n");
289 
290 	pdev = platform_device_register_simple("radeon_cp", 0, NULL, 0);
291 	err = IS_ERR(pdev);
292 	if (err) {
293 		printk(KERN_ERR "radeon_cp: Failed to register firmware\n");
294 		return -EINVAL;
295 	}
296 
297 	switch (rdev->family) {
298 	case CHIP_TAHITI:
299 		chip_name = "TAHITI";
300 		rlc_chip_name = "TAHITI";
301 		pfp_req_size = SI_PFP_UCODE_SIZE * 4;
302 		me_req_size = SI_PM4_UCODE_SIZE * 4;
303 		ce_req_size = SI_CE_UCODE_SIZE * 4;
304 		rlc_req_size = SI_RLC_UCODE_SIZE * 4;
305 		mc_req_size = SI_MC_UCODE_SIZE * 4;
306 		break;
307 	case CHIP_PITCAIRN:
308 		chip_name = "PITCAIRN";
309 		rlc_chip_name = "PITCAIRN";
310 		pfp_req_size = SI_PFP_UCODE_SIZE * 4;
311 		me_req_size = SI_PM4_UCODE_SIZE * 4;
312 		ce_req_size = SI_CE_UCODE_SIZE * 4;
313 		rlc_req_size = SI_RLC_UCODE_SIZE * 4;
314 		mc_req_size = SI_MC_UCODE_SIZE * 4;
315 		break;
316 	case CHIP_VERDE:
317 		chip_name = "VERDE";
318 		rlc_chip_name = "VERDE";
319 		pfp_req_size = SI_PFP_UCODE_SIZE * 4;
320 		me_req_size = SI_PM4_UCODE_SIZE * 4;
321 		ce_req_size = SI_CE_UCODE_SIZE * 4;
322 		rlc_req_size = SI_RLC_UCODE_SIZE * 4;
323 		mc_req_size = SI_MC_UCODE_SIZE * 4;
324 		break;
325 	default: BUG();
326 	}
327 
328 	DRM_INFO("Loading %s Microcode\n", chip_name);
329 
330 	snprintf(fw_name, sizeof(fw_name), "radeon/%s_pfp.bin", chip_name);
331 	err = request_firmware(&rdev->pfp_fw, fw_name, &pdev->dev);
332 	if (err)
333 		goto out;
334 	if (rdev->pfp_fw->size != pfp_req_size) {
335 		printk(KERN_ERR
336 		       "si_cp: Bogus length %zu in firmware \"%s\"\n",
337 		       rdev->pfp_fw->size, fw_name);
338 		err = -EINVAL;
339 		goto out;
340 	}
341 
342 	snprintf(fw_name, sizeof(fw_name), "radeon/%s_me.bin", chip_name);
343 	err = request_firmware(&rdev->me_fw, fw_name, &pdev->dev);
344 	if (err)
345 		goto out;
346 	if (rdev->me_fw->size != me_req_size) {
347 		printk(KERN_ERR
348 		       "si_cp: Bogus length %zu in firmware \"%s\"\n",
349 		       rdev->me_fw->size, fw_name);
350 		err = -EINVAL;
351 	}
352 
353 	snprintf(fw_name, sizeof(fw_name), "radeon/%s_ce.bin", chip_name);
354 	err = request_firmware(&rdev->ce_fw, fw_name, &pdev->dev);
355 	if (err)
356 		goto out;
357 	if (rdev->ce_fw->size != ce_req_size) {
358 		printk(KERN_ERR
359 		       "si_cp: Bogus length %zu in firmware \"%s\"\n",
360 		       rdev->ce_fw->size, fw_name);
361 		err = -EINVAL;
362 	}
363 
364 	snprintf(fw_name, sizeof(fw_name), "radeon/%s_rlc.bin", rlc_chip_name);
365 	err = request_firmware(&rdev->rlc_fw, fw_name, &pdev->dev);
366 	if (err)
367 		goto out;
368 	if (rdev->rlc_fw->size != rlc_req_size) {
369 		printk(KERN_ERR
370 		       "si_rlc: Bogus length %zu in firmware \"%s\"\n",
371 		       rdev->rlc_fw->size, fw_name);
372 		err = -EINVAL;
373 	}
374 
375 	snprintf(fw_name, sizeof(fw_name), "radeon/%s_mc.bin", chip_name);
376 	err = request_firmware(&rdev->mc_fw, fw_name, &pdev->dev);
377 	if (err)
378 		goto out;
379 	if (rdev->mc_fw->size != mc_req_size) {
380 		printk(KERN_ERR
381 		       "si_mc: Bogus length %zu in firmware \"%s\"\n",
382 		       rdev->mc_fw->size, fw_name);
383 		err = -EINVAL;
384 	}
385 
386 out:
387 	platform_device_unregister(pdev);
388 
389 	if (err) {
390 		if (err != -EINVAL)
391 			printk(KERN_ERR
392 			       "si_cp: Failed to load firmware \"%s\"\n",
393 			       fw_name);
394 		release_firmware(rdev->pfp_fw);
395 		rdev->pfp_fw = NULL;
396 		release_firmware(rdev->me_fw);
397 		rdev->me_fw = NULL;
398 		release_firmware(rdev->ce_fw);
399 		rdev->ce_fw = NULL;
400 		release_firmware(rdev->rlc_fw);
401 		rdev->rlc_fw = NULL;
402 		release_firmware(rdev->mc_fw);
403 		rdev->mc_fw = NULL;
404 	}
405 	return err;
406 }
407 
408 /* watermark setup */
dce6_line_buffer_adjust(struct radeon_device * rdev,struct radeon_crtc * radeon_crtc,struct drm_display_mode * mode,struct drm_display_mode * other_mode)409 static u32 dce6_line_buffer_adjust(struct radeon_device *rdev,
410 				   struct radeon_crtc *radeon_crtc,
411 				   struct drm_display_mode *mode,
412 				   struct drm_display_mode *other_mode)
413 {
414 	u32 tmp, buffer_alloc, i;
415 	u32 pipe_offset = radeon_crtc->crtc_id * 0x20;
416 	/*
417 	 * Line Buffer Setup
418 	 * There are 3 line buffers, each one shared by 2 display controllers.
419 	 * DC_LB_MEMORY_SPLIT controls how that line buffer is shared between
420 	 * the display controllers.  The paritioning is done via one of four
421 	 * preset allocations specified in bits 21:20:
422 	 *  0 - half lb
423 	 *  2 - whole lb, other crtc must be disabled
424 	 */
425 	/* this can get tricky if we have two large displays on a paired group
426 	 * of crtcs.  Ideally for multiple large displays we'd assign them to
427 	 * non-linked crtcs for maximum line buffer allocation.
428 	 */
429 	if (radeon_crtc->base.enabled && mode) {
430 		if (other_mode) {
431 			tmp = 0; /* 1/2 */
432 			buffer_alloc = 1;
433 		} else {
434 			tmp = 2; /* whole */
435 			buffer_alloc = 2;
436 		}
437 	} else {
438 		tmp = 0;
439 		buffer_alloc = 0;
440 	}
441 
442 	WREG32(DC_LB_MEMORY_SPLIT + radeon_crtc->crtc_offset,
443 	       DC_LB_MEMORY_CONFIG(tmp));
444 
445 	WREG32(PIPE0_DMIF_BUFFER_CONTROL + pipe_offset,
446 	       DMIF_BUFFERS_ALLOCATED(buffer_alloc));
447 	for (i = 0; i < rdev->usec_timeout; i++) {
448 		if (RREG32(PIPE0_DMIF_BUFFER_CONTROL + pipe_offset) &
449 		    DMIF_BUFFERS_ALLOCATED_COMPLETED)
450 			break;
451 		udelay(1);
452 	}
453 
454 	if (radeon_crtc->base.enabled && mode) {
455 		switch (tmp) {
456 		case 0:
457 		default:
458 			return 4096 * 2;
459 		case 2:
460 			return 8192 * 2;
461 		}
462 	}
463 
464 	/* controller not enabled, so no lb used */
465 	return 0;
466 }
467 
si_get_number_of_dram_channels(struct radeon_device * rdev)468 static u32 si_get_number_of_dram_channels(struct radeon_device *rdev)
469 {
470 	u32 tmp = RREG32(MC_SHARED_CHMAP);
471 
472 	switch ((tmp & NOOFCHAN_MASK) >> NOOFCHAN_SHIFT) {
473 	case 0:
474 	default:
475 		return 1;
476 	case 1:
477 		return 2;
478 	case 2:
479 		return 4;
480 	case 3:
481 		return 8;
482 	case 4:
483 		return 3;
484 	case 5:
485 		return 6;
486 	case 6:
487 		return 10;
488 	case 7:
489 		return 12;
490 	case 8:
491 		return 16;
492 	}
493 }
494 
495 struct dce6_wm_params {
496 	u32 dram_channels; /* number of dram channels */
497 	u32 yclk;          /* bandwidth per dram data pin in kHz */
498 	u32 sclk;          /* engine clock in kHz */
499 	u32 disp_clk;      /* display clock in kHz */
500 	u32 src_width;     /* viewport width */
501 	u32 active_time;   /* active display time in ns */
502 	u32 blank_time;    /* blank time in ns */
503 	bool interlaced;    /* mode is interlaced */
504 	fixed20_12 vsc;    /* vertical scale ratio */
505 	u32 num_heads;     /* number of active crtcs */
506 	u32 bytes_per_pixel; /* bytes per pixel display + overlay */
507 	u32 lb_size;       /* line buffer allocated to pipe */
508 	u32 vtaps;         /* vertical scaler taps */
509 };
510 
dce6_dram_bandwidth(struct dce6_wm_params * wm)511 static u32 dce6_dram_bandwidth(struct dce6_wm_params *wm)
512 {
513 	/* Calculate raw DRAM Bandwidth */
514 	fixed20_12 dram_efficiency; /* 0.7 */
515 	fixed20_12 yclk, dram_channels, bandwidth;
516 	fixed20_12 a;
517 
518 	a.full = dfixed_const(1000);
519 	yclk.full = dfixed_const(wm->yclk);
520 	yclk.full = dfixed_div(yclk, a);
521 	dram_channels.full = dfixed_const(wm->dram_channels * 4);
522 	a.full = dfixed_const(10);
523 	dram_efficiency.full = dfixed_const(7);
524 	dram_efficiency.full = dfixed_div(dram_efficiency, a);
525 	bandwidth.full = dfixed_mul(dram_channels, yclk);
526 	bandwidth.full = dfixed_mul(bandwidth, dram_efficiency);
527 
528 	return dfixed_trunc(bandwidth);
529 }
530 
dce6_dram_bandwidth_for_display(struct dce6_wm_params * wm)531 static u32 dce6_dram_bandwidth_for_display(struct dce6_wm_params *wm)
532 {
533 	/* Calculate DRAM Bandwidth and the part allocated to display. */
534 	fixed20_12 disp_dram_allocation; /* 0.3 to 0.7 */
535 	fixed20_12 yclk, dram_channels, bandwidth;
536 	fixed20_12 a;
537 
538 	a.full = dfixed_const(1000);
539 	yclk.full = dfixed_const(wm->yclk);
540 	yclk.full = dfixed_div(yclk, a);
541 	dram_channels.full = dfixed_const(wm->dram_channels * 4);
542 	a.full = dfixed_const(10);
543 	disp_dram_allocation.full = dfixed_const(3); /* XXX worse case value 0.3 */
544 	disp_dram_allocation.full = dfixed_div(disp_dram_allocation, a);
545 	bandwidth.full = dfixed_mul(dram_channels, yclk);
546 	bandwidth.full = dfixed_mul(bandwidth, disp_dram_allocation);
547 
548 	return dfixed_trunc(bandwidth);
549 }
550 
dce6_data_return_bandwidth(struct dce6_wm_params * wm)551 static u32 dce6_data_return_bandwidth(struct dce6_wm_params *wm)
552 {
553 	/* Calculate the display Data return Bandwidth */
554 	fixed20_12 return_efficiency; /* 0.8 */
555 	fixed20_12 sclk, bandwidth;
556 	fixed20_12 a;
557 
558 	a.full = dfixed_const(1000);
559 	sclk.full = dfixed_const(wm->sclk);
560 	sclk.full = dfixed_div(sclk, a);
561 	a.full = dfixed_const(10);
562 	return_efficiency.full = dfixed_const(8);
563 	return_efficiency.full = dfixed_div(return_efficiency, a);
564 	a.full = dfixed_const(32);
565 	bandwidth.full = dfixed_mul(a, sclk);
566 	bandwidth.full = dfixed_mul(bandwidth, return_efficiency);
567 
568 	return dfixed_trunc(bandwidth);
569 }
570 
dce6_get_dmif_bytes_per_request(struct dce6_wm_params * wm)571 static u32 dce6_get_dmif_bytes_per_request(struct dce6_wm_params *wm)
572 {
573 	return 32;
574 }
575 
dce6_dmif_request_bandwidth(struct dce6_wm_params * wm)576 static u32 dce6_dmif_request_bandwidth(struct dce6_wm_params *wm)
577 {
578 	/* Calculate the DMIF Request Bandwidth */
579 	fixed20_12 disp_clk_request_efficiency; /* 0.8 */
580 	fixed20_12 disp_clk, sclk, bandwidth;
581 	fixed20_12 a, b1, b2;
582 	u32 min_bandwidth;
583 
584 	a.full = dfixed_const(1000);
585 	disp_clk.full = dfixed_const(wm->disp_clk);
586 	disp_clk.full = dfixed_div(disp_clk, a);
587 	a.full = dfixed_const(dce6_get_dmif_bytes_per_request(wm) / 2);
588 	b1.full = dfixed_mul(a, disp_clk);
589 
590 	a.full = dfixed_const(1000);
591 	sclk.full = dfixed_const(wm->sclk);
592 	sclk.full = dfixed_div(sclk, a);
593 	a.full = dfixed_const(dce6_get_dmif_bytes_per_request(wm));
594 	b2.full = dfixed_mul(a, sclk);
595 
596 	a.full = dfixed_const(10);
597 	disp_clk_request_efficiency.full = dfixed_const(8);
598 	disp_clk_request_efficiency.full = dfixed_div(disp_clk_request_efficiency, a);
599 
600 	min_bandwidth = min(dfixed_trunc(b1), dfixed_trunc(b2));
601 
602 	a.full = dfixed_const(min_bandwidth);
603 	bandwidth.full = dfixed_mul(a, disp_clk_request_efficiency);
604 
605 	return dfixed_trunc(bandwidth);
606 }
607 
dce6_available_bandwidth(struct dce6_wm_params * wm)608 static u32 dce6_available_bandwidth(struct dce6_wm_params *wm)
609 {
610 	/* Calculate the Available bandwidth. Display can use this temporarily but not in average. */
611 	u32 dram_bandwidth = dce6_dram_bandwidth(wm);
612 	u32 data_return_bandwidth = dce6_data_return_bandwidth(wm);
613 	u32 dmif_req_bandwidth = dce6_dmif_request_bandwidth(wm);
614 
615 	return min(dram_bandwidth, min(data_return_bandwidth, dmif_req_bandwidth));
616 }
617 
dce6_average_bandwidth(struct dce6_wm_params * wm)618 static u32 dce6_average_bandwidth(struct dce6_wm_params *wm)
619 {
620 	/* Calculate the display mode Average Bandwidth
621 	 * DisplayMode should contain the source and destination dimensions,
622 	 * timing, etc.
623 	 */
624 	fixed20_12 bpp;
625 	fixed20_12 line_time;
626 	fixed20_12 src_width;
627 	fixed20_12 bandwidth;
628 	fixed20_12 a;
629 
630 	a.full = dfixed_const(1000);
631 	line_time.full = dfixed_const(wm->active_time + wm->blank_time);
632 	line_time.full = dfixed_div(line_time, a);
633 	bpp.full = dfixed_const(wm->bytes_per_pixel);
634 	src_width.full = dfixed_const(wm->src_width);
635 	bandwidth.full = dfixed_mul(src_width, bpp);
636 	bandwidth.full = dfixed_mul(bandwidth, wm->vsc);
637 	bandwidth.full = dfixed_div(bandwidth, line_time);
638 
639 	return dfixed_trunc(bandwidth);
640 }
641 
dce6_latency_watermark(struct dce6_wm_params * wm)642 static u32 dce6_latency_watermark(struct dce6_wm_params *wm)
643 {
644 	/* First calcualte the latency in ns */
645 	u32 mc_latency = 2000; /* 2000 ns. */
646 	u32 available_bandwidth = dce6_available_bandwidth(wm);
647 	u32 worst_chunk_return_time = (512 * 8 * 1000) / available_bandwidth;
648 	u32 cursor_line_pair_return_time = (128 * 4 * 1000) / available_bandwidth;
649 	u32 dc_latency = 40000000 / wm->disp_clk; /* dc pipe latency */
650 	u32 other_heads_data_return_time = ((wm->num_heads + 1) * worst_chunk_return_time) +
651 		(wm->num_heads * cursor_line_pair_return_time);
652 	u32 latency = mc_latency + other_heads_data_return_time + dc_latency;
653 	u32 max_src_lines_per_dst_line, lb_fill_bw, line_fill_time;
654 	u32 tmp, dmif_size = 12288;
655 	fixed20_12 a, b, c;
656 
657 	if (wm->num_heads == 0)
658 		return 0;
659 
660 	a.full = dfixed_const(2);
661 	b.full = dfixed_const(1);
662 	if ((wm->vsc.full > a.full) ||
663 	    ((wm->vsc.full > b.full) && (wm->vtaps >= 3)) ||
664 	    (wm->vtaps >= 5) ||
665 	    ((wm->vsc.full >= a.full) && wm->interlaced))
666 		max_src_lines_per_dst_line = 4;
667 	else
668 		max_src_lines_per_dst_line = 2;
669 
670 	a.full = dfixed_const(available_bandwidth);
671 	b.full = dfixed_const(wm->num_heads);
672 	a.full = dfixed_div(a, b);
673 
674 	b.full = dfixed_const(mc_latency + 512);
675 	c.full = dfixed_const(wm->disp_clk);
676 	b.full = dfixed_div(b, c);
677 
678 	c.full = dfixed_const(dmif_size);
679 	b.full = dfixed_div(c, b);
680 
681 	tmp = min(dfixed_trunc(a), dfixed_trunc(b));
682 
683 	b.full = dfixed_const(1000);
684 	c.full = dfixed_const(wm->disp_clk);
685 	b.full = dfixed_div(c, b);
686 	c.full = dfixed_const(wm->bytes_per_pixel);
687 	b.full = dfixed_mul(b, c);
688 
689 	lb_fill_bw = min(tmp, dfixed_trunc(b));
690 
691 	a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
692 	b.full = dfixed_const(1000);
693 	c.full = dfixed_const(lb_fill_bw);
694 	b.full = dfixed_div(c, b);
695 	a.full = dfixed_div(a, b);
696 	line_fill_time = dfixed_trunc(a);
697 
698 	if (line_fill_time < wm->active_time)
699 		return latency;
700 	else
701 		return latency + (line_fill_time - wm->active_time);
702 
703 }
704 
dce6_average_bandwidth_vs_dram_bandwidth_for_display(struct dce6_wm_params * wm)705 static bool dce6_average_bandwidth_vs_dram_bandwidth_for_display(struct dce6_wm_params *wm)
706 {
707 	if (dce6_average_bandwidth(wm) <=
708 	    (dce6_dram_bandwidth_for_display(wm) / wm->num_heads))
709 		return true;
710 	else
711 		return false;
712 };
713 
dce6_average_bandwidth_vs_available_bandwidth(struct dce6_wm_params * wm)714 static bool dce6_average_bandwidth_vs_available_bandwidth(struct dce6_wm_params *wm)
715 {
716 	if (dce6_average_bandwidth(wm) <=
717 	    (dce6_available_bandwidth(wm) / wm->num_heads))
718 		return true;
719 	else
720 		return false;
721 };
722 
dce6_check_latency_hiding(struct dce6_wm_params * wm)723 static bool dce6_check_latency_hiding(struct dce6_wm_params *wm)
724 {
725 	u32 lb_partitions = wm->lb_size / wm->src_width;
726 	u32 line_time = wm->active_time + wm->blank_time;
727 	u32 latency_tolerant_lines;
728 	u32 latency_hiding;
729 	fixed20_12 a;
730 
731 	a.full = dfixed_const(1);
732 	if (wm->vsc.full > a.full)
733 		latency_tolerant_lines = 1;
734 	else {
735 		if (lb_partitions <= (wm->vtaps + 1))
736 			latency_tolerant_lines = 1;
737 		else
738 			latency_tolerant_lines = 2;
739 	}
740 
741 	latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
742 
743 	if (dce6_latency_watermark(wm) <= latency_hiding)
744 		return true;
745 	else
746 		return false;
747 }
748 
dce6_program_watermarks(struct radeon_device * rdev,struct radeon_crtc * radeon_crtc,u32 lb_size,u32 num_heads)749 static void dce6_program_watermarks(struct radeon_device *rdev,
750 					 struct radeon_crtc *radeon_crtc,
751 					 u32 lb_size, u32 num_heads)
752 {
753 	struct drm_display_mode *mode = &radeon_crtc->base.mode;
754 	struct dce6_wm_params wm;
755 	u32 pixel_period;
756 	u32 line_time = 0;
757 	u32 latency_watermark_a = 0, latency_watermark_b = 0;
758 	u32 priority_a_mark = 0, priority_b_mark = 0;
759 	u32 priority_a_cnt = PRIORITY_OFF;
760 	u32 priority_b_cnt = PRIORITY_OFF;
761 	u32 tmp, arb_control3;
762 	fixed20_12 a, b, c;
763 
764 	if (radeon_crtc->base.enabled && num_heads && mode) {
765 		pixel_period = 1000000 / (u32)mode->clock;
766 		line_time = min((u32)mode->crtc_htotal * pixel_period, (u32)65535);
767 		priority_a_cnt = 0;
768 		priority_b_cnt = 0;
769 
770 		wm.yclk = rdev->pm.current_mclk * 10;
771 		wm.sclk = rdev->pm.current_sclk * 10;
772 		wm.disp_clk = mode->clock;
773 		wm.src_width = mode->crtc_hdisplay;
774 		wm.active_time = mode->crtc_hdisplay * pixel_period;
775 		wm.blank_time = line_time - wm.active_time;
776 		wm.interlaced = false;
777 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
778 			wm.interlaced = true;
779 		wm.vsc = radeon_crtc->vsc;
780 		wm.vtaps = 1;
781 		if (radeon_crtc->rmx_type != RMX_OFF)
782 			wm.vtaps = 2;
783 		wm.bytes_per_pixel = 4; /* XXX: get this from fb config */
784 		wm.lb_size = lb_size;
785 		if (rdev->family == CHIP_ARUBA)
786 			wm.dram_channels = evergreen_get_number_of_dram_channels(rdev);
787 		else
788 			wm.dram_channels = si_get_number_of_dram_channels(rdev);
789 		wm.num_heads = num_heads;
790 
791 		/* set for high clocks */
792 		latency_watermark_a = min(dce6_latency_watermark(&wm), (u32)65535);
793 		/* set for low clocks */
794 		/* wm.yclk = low clk; wm.sclk = low clk */
795 		latency_watermark_b = min(dce6_latency_watermark(&wm), (u32)65535);
796 
797 		/* possibly force display priority to high */
798 		/* should really do this at mode validation time... */
799 		if (!dce6_average_bandwidth_vs_dram_bandwidth_for_display(&wm) ||
800 		    !dce6_average_bandwidth_vs_available_bandwidth(&wm) ||
801 		    !dce6_check_latency_hiding(&wm) ||
802 		    (rdev->disp_priority == 2)) {
803 			DRM_DEBUG_KMS("force priority to high\n");
804 			priority_a_cnt |= PRIORITY_ALWAYS_ON;
805 			priority_b_cnt |= PRIORITY_ALWAYS_ON;
806 		}
807 
808 		a.full = dfixed_const(1000);
809 		b.full = dfixed_const(mode->clock);
810 		b.full = dfixed_div(b, a);
811 		c.full = dfixed_const(latency_watermark_a);
812 		c.full = dfixed_mul(c, b);
813 		c.full = dfixed_mul(c, radeon_crtc->hsc);
814 		c.full = dfixed_div(c, a);
815 		a.full = dfixed_const(16);
816 		c.full = dfixed_div(c, a);
817 		priority_a_mark = dfixed_trunc(c);
818 		priority_a_cnt |= priority_a_mark & PRIORITY_MARK_MASK;
819 
820 		a.full = dfixed_const(1000);
821 		b.full = dfixed_const(mode->clock);
822 		b.full = dfixed_div(b, a);
823 		c.full = dfixed_const(latency_watermark_b);
824 		c.full = dfixed_mul(c, b);
825 		c.full = dfixed_mul(c, radeon_crtc->hsc);
826 		c.full = dfixed_div(c, a);
827 		a.full = dfixed_const(16);
828 		c.full = dfixed_div(c, a);
829 		priority_b_mark = dfixed_trunc(c);
830 		priority_b_cnt |= priority_b_mark & PRIORITY_MARK_MASK;
831 	}
832 
833 	/* select wm A */
834 	arb_control3 = RREG32(DPG_PIPE_ARBITRATION_CONTROL3 + radeon_crtc->crtc_offset);
835 	tmp = arb_control3;
836 	tmp &= ~LATENCY_WATERMARK_MASK(3);
837 	tmp |= LATENCY_WATERMARK_MASK(1);
838 	WREG32(DPG_PIPE_ARBITRATION_CONTROL3 + radeon_crtc->crtc_offset, tmp);
839 	WREG32(DPG_PIPE_LATENCY_CONTROL + radeon_crtc->crtc_offset,
840 	       (LATENCY_LOW_WATERMARK(latency_watermark_a) |
841 		LATENCY_HIGH_WATERMARK(line_time)));
842 	/* select wm B */
843 	tmp = RREG32(DPG_PIPE_ARBITRATION_CONTROL3 + radeon_crtc->crtc_offset);
844 	tmp &= ~LATENCY_WATERMARK_MASK(3);
845 	tmp |= LATENCY_WATERMARK_MASK(2);
846 	WREG32(DPG_PIPE_ARBITRATION_CONTROL3 + radeon_crtc->crtc_offset, tmp);
847 	WREG32(DPG_PIPE_LATENCY_CONTROL + radeon_crtc->crtc_offset,
848 	       (LATENCY_LOW_WATERMARK(latency_watermark_b) |
849 		LATENCY_HIGH_WATERMARK(line_time)));
850 	/* restore original selection */
851 	WREG32(DPG_PIPE_ARBITRATION_CONTROL3 + radeon_crtc->crtc_offset, arb_control3);
852 
853 	/* write the priority marks */
854 	WREG32(PRIORITY_A_CNT + radeon_crtc->crtc_offset, priority_a_cnt);
855 	WREG32(PRIORITY_B_CNT + radeon_crtc->crtc_offset, priority_b_cnt);
856 
857 }
858 
dce6_bandwidth_update(struct radeon_device * rdev)859 void dce6_bandwidth_update(struct radeon_device *rdev)
860 {
861 	struct drm_display_mode *mode0 = NULL;
862 	struct drm_display_mode *mode1 = NULL;
863 	u32 num_heads = 0, lb_size;
864 	int i;
865 
866 	radeon_update_display_priority(rdev);
867 
868 	for (i = 0; i < rdev->num_crtc; i++) {
869 		if (rdev->mode_info.crtcs[i]->base.enabled)
870 			num_heads++;
871 	}
872 	for (i = 0; i < rdev->num_crtc; i += 2) {
873 		mode0 = &rdev->mode_info.crtcs[i]->base.mode;
874 		mode1 = &rdev->mode_info.crtcs[i+1]->base.mode;
875 		lb_size = dce6_line_buffer_adjust(rdev, rdev->mode_info.crtcs[i], mode0, mode1);
876 		dce6_program_watermarks(rdev, rdev->mode_info.crtcs[i], lb_size, num_heads);
877 		lb_size = dce6_line_buffer_adjust(rdev, rdev->mode_info.crtcs[i+1], mode1, mode0);
878 		dce6_program_watermarks(rdev, rdev->mode_info.crtcs[i+1], lb_size, num_heads);
879 	}
880 }
881 
882 /*
883  * Core functions
884  */
si_get_tile_pipe_to_backend_map(struct radeon_device * rdev,u32 num_tile_pipes,u32 num_backends_per_asic,u32 * backend_disable_mask_per_asic,u32 num_shader_engines)885 static u32 si_get_tile_pipe_to_backend_map(struct radeon_device *rdev,
886 					   u32 num_tile_pipes,
887 					   u32 num_backends_per_asic,
888 					   u32 *backend_disable_mask_per_asic,
889 					   u32 num_shader_engines)
890 {
891 	u32 backend_map = 0;
892 	u32 enabled_backends_mask = 0;
893 	u32 enabled_backends_count = 0;
894 	u32 num_backends_per_se;
895 	u32 cur_pipe;
896 	u32 swizzle_pipe[SI_MAX_PIPES];
897 	u32 cur_backend = 0;
898 	u32 i;
899 	bool force_no_swizzle;
900 
901 	/* force legal values */
902 	if (num_tile_pipes < 1)
903 		num_tile_pipes = 1;
904 	if (num_tile_pipes > rdev->config.si.max_tile_pipes)
905 		num_tile_pipes = rdev->config.si.max_tile_pipes;
906 	if (num_shader_engines < 1)
907 		num_shader_engines = 1;
908 	if (num_shader_engines > rdev->config.si.max_shader_engines)
909 		num_shader_engines = rdev->config.si.max_shader_engines;
910 	if (num_backends_per_asic < num_shader_engines)
911 		num_backends_per_asic = num_shader_engines;
912 	if (num_backends_per_asic > (rdev->config.si.max_backends_per_se * num_shader_engines))
913 		num_backends_per_asic = rdev->config.si.max_backends_per_se * num_shader_engines;
914 
915 	/* make sure we have the same number of backends per se */
916 	num_backends_per_asic = ALIGN(num_backends_per_asic, num_shader_engines);
917 	/* set up the number of backends per se */
918 	num_backends_per_se = num_backends_per_asic / num_shader_engines;
919 	if (num_backends_per_se > rdev->config.si.max_backends_per_se) {
920 		num_backends_per_se = rdev->config.si.max_backends_per_se;
921 		num_backends_per_asic = num_backends_per_se * num_shader_engines;
922 	}
923 
924 	/* create enable mask and count for enabled backends */
925 	for (i = 0; i < SI_MAX_BACKENDS; ++i) {
926 		if (((*backend_disable_mask_per_asic >> i) & 1) == 0) {
927 			enabled_backends_mask |= (1 << i);
928 			++enabled_backends_count;
929 		}
930 		if (enabled_backends_count == num_backends_per_asic)
931 			break;
932 	}
933 
934 	/* force the backends mask to match the current number of backends */
935 	if (enabled_backends_count != num_backends_per_asic) {
936 		u32 this_backend_enabled;
937 		u32 shader_engine;
938 		u32 backend_per_se;
939 
940 		enabled_backends_mask = 0;
941 		enabled_backends_count = 0;
942 		*backend_disable_mask_per_asic = SI_MAX_BACKENDS_MASK;
943 		for (i = 0; i < SI_MAX_BACKENDS; ++i) {
944 			/* calc the current se */
945 			shader_engine = i / rdev->config.si.max_backends_per_se;
946 			/* calc the backend per se */
947 			backend_per_se = i % rdev->config.si.max_backends_per_se;
948 			/* default to not enabled */
949 			this_backend_enabled = 0;
950 			if ((shader_engine < num_shader_engines) &&
951 			    (backend_per_se < num_backends_per_se))
952 				this_backend_enabled = 1;
953 			if (this_backend_enabled) {
954 				enabled_backends_mask |= (1 << i);
955 				*backend_disable_mask_per_asic &= ~(1 << i);
956 				++enabled_backends_count;
957 			}
958 		}
959 	}
960 
961 
962 	memset((uint8_t *)&swizzle_pipe[0], 0, sizeof(u32) * SI_MAX_PIPES);
963 	switch (rdev->family) {
964 	case CHIP_TAHITI:
965 	case CHIP_PITCAIRN:
966 	case CHIP_VERDE:
967 		force_no_swizzle = true;
968 		break;
969 	default:
970 		force_no_swizzle = false;
971 		break;
972 	}
973 	if (force_no_swizzle) {
974 		bool last_backend_enabled = false;
975 
976 		force_no_swizzle = false;
977 		for (i = 0; i < SI_MAX_BACKENDS; ++i) {
978 			if (((enabled_backends_mask >> i) & 1) == 1) {
979 				if (last_backend_enabled)
980 					force_no_swizzle = true;
981 				last_backend_enabled = true;
982 			} else
983 				last_backend_enabled = false;
984 		}
985 	}
986 
987 	switch (num_tile_pipes) {
988 	case 1:
989 	case 3:
990 	case 5:
991 	case 7:
992 		DRM_ERROR("odd number of pipes!\n");
993 		break;
994 	case 2:
995 		swizzle_pipe[0] = 0;
996 		swizzle_pipe[1] = 1;
997 		break;
998 	case 4:
999 		if (force_no_swizzle) {
1000 			swizzle_pipe[0] = 0;
1001 			swizzle_pipe[1] = 1;
1002 			swizzle_pipe[2] = 2;
1003 			swizzle_pipe[3] = 3;
1004 		} else {
1005 			swizzle_pipe[0] = 0;
1006 			swizzle_pipe[1] = 2;
1007 			swizzle_pipe[2] = 1;
1008 			swizzle_pipe[3] = 3;
1009 		}
1010 		break;
1011 	case 6:
1012 		if (force_no_swizzle) {
1013 			swizzle_pipe[0] = 0;
1014 			swizzle_pipe[1] = 1;
1015 			swizzle_pipe[2] = 2;
1016 			swizzle_pipe[3] = 3;
1017 			swizzle_pipe[4] = 4;
1018 			swizzle_pipe[5] = 5;
1019 		} else {
1020 			swizzle_pipe[0] = 0;
1021 			swizzle_pipe[1] = 2;
1022 			swizzle_pipe[2] = 4;
1023 			swizzle_pipe[3] = 1;
1024 			swizzle_pipe[4] = 3;
1025 			swizzle_pipe[5] = 5;
1026 		}
1027 		break;
1028 	case 8:
1029 		if (force_no_swizzle) {
1030 			swizzle_pipe[0] = 0;
1031 			swizzle_pipe[1] = 1;
1032 			swizzle_pipe[2] = 2;
1033 			swizzle_pipe[3] = 3;
1034 			swizzle_pipe[4] = 4;
1035 			swizzle_pipe[5] = 5;
1036 			swizzle_pipe[6] = 6;
1037 			swizzle_pipe[7] = 7;
1038 		} else {
1039 			swizzle_pipe[0] = 0;
1040 			swizzle_pipe[1] = 2;
1041 			swizzle_pipe[2] = 4;
1042 			swizzle_pipe[3] = 6;
1043 			swizzle_pipe[4] = 1;
1044 			swizzle_pipe[5] = 3;
1045 			swizzle_pipe[6] = 5;
1046 			swizzle_pipe[7] = 7;
1047 		}
1048 		break;
1049 	}
1050 
1051 	for (cur_pipe = 0; cur_pipe < num_tile_pipes; ++cur_pipe) {
1052 		while (((1 << cur_backend) & enabled_backends_mask) == 0)
1053 			cur_backend = (cur_backend + 1) % SI_MAX_BACKENDS;
1054 
1055 		backend_map |= (((cur_backend & 0xf) << (swizzle_pipe[cur_pipe] * 4)));
1056 
1057 		cur_backend = (cur_backend + 1) % SI_MAX_BACKENDS;
1058 	}
1059 
1060 	return backend_map;
1061 }
1062 
si_get_disable_mask_per_asic(struct radeon_device * rdev,u32 disable_mask_per_se,u32 max_disable_mask_per_se,u32 num_shader_engines)1063 static u32 si_get_disable_mask_per_asic(struct radeon_device *rdev,
1064 					u32 disable_mask_per_se,
1065 					u32 max_disable_mask_per_se,
1066 					u32 num_shader_engines)
1067 {
1068 	u32 disable_field_width_per_se = r600_count_pipe_bits(disable_mask_per_se);
1069 	u32 disable_mask_per_asic = disable_mask_per_se & max_disable_mask_per_se;
1070 
1071 	if (num_shader_engines == 1)
1072 		return disable_mask_per_asic;
1073 	else if (num_shader_engines == 2)
1074 		return disable_mask_per_asic | (disable_mask_per_asic << disable_field_width_per_se);
1075 	else
1076 		return 0xffffffff;
1077 }
1078 
si_tiling_mode_table_init(struct radeon_device * rdev)1079 static void si_tiling_mode_table_init(struct radeon_device *rdev)
1080 {
1081 	const u32 num_tile_mode_states = 32;
1082 	u32 reg_offset, gb_tile_moden, split_equal_to_row_size;
1083 
1084 	switch (rdev->config.si.mem_row_size_in_kb) {
1085 	case 1:
1086 		split_equal_to_row_size = ADDR_SURF_TILE_SPLIT_1KB;
1087 		break;
1088 	case 2:
1089 	default:
1090 		split_equal_to_row_size = ADDR_SURF_TILE_SPLIT_2KB;
1091 		break;
1092 	case 4:
1093 		split_equal_to_row_size = ADDR_SURF_TILE_SPLIT_4KB;
1094 		break;
1095 	}
1096 
1097 	if ((rdev->family == CHIP_TAHITI) ||
1098 	    (rdev->family == CHIP_PITCAIRN)) {
1099 		for (reg_offset = 0; reg_offset < num_tile_mode_states; reg_offset++) {
1100 			switch (reg_offset) {
1101 			case 0:  /* non-AA compressed depth or any compressed stencil */
1102 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1103 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1104 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1105 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
1106 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1107 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1108 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1109 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1110 				break;
1111 			case 1:  /* 2xAA/4xAA compressed depth only */
1112 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1113 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1114 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1115 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_128B) |
1116 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1117 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1118 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1119 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1120 				break;
1121 			case 2:  /* 8xAA compressed depth only */
1122 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1123 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1124 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1125 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1126 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1127 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1128 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1129 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1130 				break;
1131 			case 3:  /* 2xAA/4xAA compressed depth with stencil (for depth buffer) */
1132 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1133 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1134 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1135 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_128B) |
1136 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1137 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1138 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1139 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1140 				break;
1141 			case 4:  /* Maps w/ a dimension less than the 2D macro-tile dimensions (for mipmapped depth textures) */
1142 				gb_tile_moden = (ARRAY_MODE(ARRAY_1D_TILED_THIN1) |
1143 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1144 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1145 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
1146 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1147 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1148 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1149 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1150 				break;
1151 			case 5:  /* Uncompressed 16bpp depth - and stencil buffer allocated with it */
1152 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1153 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1154 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1155 						 TILE_SPLIT(split_equal_to_row_size) |
1156 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1157 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1158 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1159 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1160 				break;
1161 			case 6:  /* Uncompressed 32bpp depth - and stencil buffer allocated with it */
1162 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1163 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1164 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1165 						 TILE_SPLIT(split_equal_to_row_size) |
1166 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1167 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1168 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
1169 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
1170 				break;
1171 			case 7:  /* Uncompressed 8bpp stencil without depth (drivers typically do not use) */
1172 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1173 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1174 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1175 						 TILE_SPLIT(split_equal_to_row_size) |
1176 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1177 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1178 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1179 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1180 				break;
1181 			case 8:  /* 1D and 1D Array Surfaces */
1182 				gb_tile_moden = (ARRAY_MODE(ARRAY_LINEAR_ALIGNED) |
1183 						 MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
1184 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1185 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
1186 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1187 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1188 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1189 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1190 				break;
1191 			case 9:  /* Displayable maps. */
1192 				gb_tile_moden = (ARRAY_MODE(ARRAY_1D_TILED_THIN1) |
1193 						 MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
1194 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1195 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
1196 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1197 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1198 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1199 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1200 				break;
1201 			case 10:  /* Display 8bpp. */
1202 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1203 						 MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
1204 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1205 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1206 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1207 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1208 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1209 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1210 				break;
1211 			case 11:  /* Display 16bpp. */
1212 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1213 						 MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
1214 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1215 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1216 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1217 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1218 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1219 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1220 				break;
1221 			case 12:  /* Display 32bpp. */
1222 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1223 						 MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
1224 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1225 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_512B) |
1226 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1227 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1228 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
1229 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
1230 				break;
1231 			case 13:  /* Thin. */
1232 				gb_tile_moden = (ARRAY_MODE(ARRAY_1D_TILED_THIN1) |
1233 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1234 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1235 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
1236 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1237 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1238 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1239 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1240 				break;
1241 			case 14:  /* Thin 8 bpp. */
1242 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1243 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1244 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1245 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1246 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1247 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1248 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1249 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
1250 				break;
1251 			case 15:  /* Thin 16 bpp. */
1252 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1253 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1254 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1255 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1256 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1257 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1258 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1259 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
1260 				break;
1261 			case 16:  /* Thin 32 bpp. */
1262 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1263 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1264 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1265 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_512B) |
1266 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1267 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1268 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
1269 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
1270 				break;
1271 			case 17:  /* Thin 64 bpp. */
1272 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1273 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1274 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1275 						 TILE_SPLIT(split_equal_to_row_size) |
1276 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1277 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1278 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
1279 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
1280 				break;
1281 			case 21:  /* 8 bpp PRT. */
1282 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1283 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1284 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1285 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1286 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1287 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_2) |
1288 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1289 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1290 				break;
1291 			case 22:  /* 16 bpp PRT */
1292 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1293 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1294 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1295 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1296 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1297 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1298 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1299 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
1300 				break;
1301 			case 23:  /* 32 bpp PRT */
1302 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1303 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1304 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1305 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1306 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1307 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1308 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1309 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1310 				break;
1311 			case 24:  /* 64 bpp PRT */
1312 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1313 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1314 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1315 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_512B) |
1316 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1317 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1318 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
1319 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1320 				break;
1321 			case 25:  /* 128 bpp PRT */
1322 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1323 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1324 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1325 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_1KB) |
1326 						 NUM_BANKS(ADDR_SURF_8_BANK) |
1327 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1328 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
1329 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
1330 				break;
1331 			default:
1332 				gb_tile_moden = 0;
1333 				break;
1334 			}
1335 			WREG32(GB_TILE_MODE0 + (reg_offset * 4), gb_tile_moden);
1336 		}
1337 	} else if (rdev->family == CHIP_VERDE) {
1338 		for (reg_offset = 0; reg_offset < num_tile_mode_states; reg_offset++) {
1339 			switch (reg_offset) {
1340 			case 0:  /* non-AA compressed depth or any compressed stencil */
1341 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1342 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1343 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1344 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
1345 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1346 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1347 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1348 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
1349 				break;
1350 			case 1:  /* 2xAA/4xAA compressed depth only */
1351 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1352 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1353 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1354 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_128B) |
1355 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1356 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1357 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1358 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
1359 				break;
1360 			case 2:  /* 8xAA compressed depth only */
1361 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1362 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1363 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1364 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1365 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1366 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1367 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1368 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
1369 				break;
1370 			case 3:  /* 2xAA/4xAA compressed depth with stencil (for depth buffer) */
1371 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1372 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1373 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1374 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_128B) |
1375 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1376 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1377 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1378 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
1379 				break;
1380 			case 4:  /* Maps w/ a dimension less than the 2D macro-tile dimensions (for mipmapped depth textures) */
1381 				gb_tile_moden = (ARRAY_MODE(ARRAY_1D_TILED_THIN1) |
1382 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1383 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1384 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
1385 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1386 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1387 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1388 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1389 				break;
1390 			case 5:  /* Uncompressed 16bpp depth - and stencil buffer allocated with it */
1391 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1392 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1393 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1394 						 TILE_SPLIT(split_equal_to_row_size) |
1395 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1396 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1397 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1398 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1399 				break;
1400 			case 6:  /* Uncompressed 32bpp depth - and stencil buffer allocated with it */
1401 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1402 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1403 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1404 						 TILE_SPLIT(split_equal_to_row_size) |
1405 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1406 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1407 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
1408 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1409 				break;
1410 			case 7:  /* Uncompressed 8bpp stencil without depth (drivers typically do not use) */
1411 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1412 						 MICRO_TILE_MODE(ADDR_SURF_DEPTH_MICRO_TILING) |
1413 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1414 						 TILE_SPLIT(split_equal_to_row_size) |
1415 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1416 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1417 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1418 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
1419 				break;
1420 			case 8:  /* 1D and 1D Array Surfaces */
1421 				gb_tile_moden = (ARRAY_MODE(ARRAY_LINEAR_ALIGNED) |
1422 						 MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
1423 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1424 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
1425 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1426 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1427 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1428 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1429 				break;
1430 			case 9:  /* Displayable maps. */
1431 				gb_tile_moden = (ARRAY_MODE(ARRAY_1D_TILED_THIN1) |
1432 						 MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
1433 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1434 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
1435 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1436 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1437 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1438 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1439 				break;
1440 			case 10:  /* Display 8bpp. */
1441 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1442 						 MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
1443 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1444 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1445 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1446 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1447 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1448 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
1449 				break;
1450 			case 11:  /* Display 16bpp. */
1451 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1452 						 MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
1453 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1454 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1455 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1456 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1457 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1458 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1459 				break;
1460 			case 12:  /* Display 32bpp. */
1461 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1462 						 MICRO_TILE_MODE(ADDR_SURF_DISPLAY_MICRO_TILING) |
1463 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1464 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_512B) |
1465 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1466 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1467 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
1468 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1469 				break;
1470 			case 13:  /* Thin. */
1471 				gb_tile_moden = (ARRAY_MODE(ARRAY_1D_TILED_THIN1) |
1472 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1473 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1474 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_64B) |
1475 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1476 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1477 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1478 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1479 				break;
1480 			case 14:  /* Thin 8 bpp. */
1481 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1482 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1483 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1484 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1485 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1486 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1487 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1488 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1489 				break;
1490 			case 15:  /* Thin 16 bpp. */
1491 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1492 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1493 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1494 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1495 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1496 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1497 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1498 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1499 				break;
1500 			case 16:  /* Thin 32 bpp. */
1501 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1502 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1503 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1504 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_512B) |
1505 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1506 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1507 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
1508 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1509 				break;
1510 			case 17:  /* Thin 64 bpp. */
1511 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1512 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1513 						 PIPE_CONFIG(ADDR_SURF_P4_8x16) |
1514 						 TILE_SPLIT(split_equal_to_row_size) |
1515 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1516 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1517 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
1518 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1519 				break;
1520 			case 21:  /* 8 bpp PRT. */
1521 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1522 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1523 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1524 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1525 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1526 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_2) |
1527 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1528 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1529 				break;
1530 			case 22:  /* 16 bpp PRT */
1531 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1532 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1533 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1534 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1535 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1536 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1537 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_4) |
1538 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_4));
1539 				break;
1540 			case 23:  /* 32 bpp PRT */
1541 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1542 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1543 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1544 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_256B) |
1545 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1546 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1547 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_2) |
1548 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1549 				break;
1550 			case 24:  /* 64 bpp PRT */
1551 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1552 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1553 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1554 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_512B) |
1555 						 NUM_BANKS(ADDR_SURF_16_BANK) |
1556 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1557 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
1558 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_2));
1559 				break;
1560 			case 25:  /* 128 bpp PRT */
1561 				gb_tile_moden = (ARRAY_MODE(ARRAY_2D_TILED_THIN1) |
1562 						 MICRO_TILE_MODE(ADDR_SURF_THIN_MICRO_TILING) |
1563 						 PIPE_CONFIG(ADDR_SURF_P8_32x32_8x16) |
1564 						 TILE_SPLIT(ADDR_SURF_TILE_SPLIT_1KB) |
1565 						 NUM_BANKS(ADDR_SURF_8_BANK) |
1566 						 BANK_WIDTH(ADDR_SURF_BANK_WIDTH_1) |
1567 						 BANK_HEIGHT(ADDR_SURF_BANK_HEIGHT_1) |
1568 						 MACRO_TILE_ASPECT(ADDR_SURF_MACRO_ASPECT_1));
1569 				break;
1570 			default:
1571 				gb_tile_moden = 0;
1572 				break;
1573 			}
1574 			WREG32(GB_TILE_MODE0 + (reg_offset * 4), gb_tile_moden);
1575 		}
1576 	} else
1577 		DRM_ERROR("unknown asic: 0x%x\n", rdev->family);
1578 }
1579 
si_gpu_init(struct radeon_device * rdev)1580 static void si_gpu_init(struct radeon_device *rdev)
1581 {
1582 	u32 cc_rb_backend_disable = 0;
1583 	u32 cc_gc_shader_array_config;
1584 	u32 gb_addr_config = 0;
1585 	u32 mc_shared_chmap, mc_arb_ramcfg;
1586 	u32 gb_backend_map;
1587 	u32 cgts_tcc_disable;
1588 	u32 sx_debug_1;
1589 	u32 gc_user_shader_array_config;
1590 	u32 gc_user_rb_backend_disable;
1591 	u32 cgts_user_tcc_disable;
1592 	u32 hdp_host_path_cntl;
1593 	u32 tmp;
1594 	int i, j;
1595 
1596 	switch (rdev->family) {
1597 	case CHIP_TAHITI:
1598 		rdev->config.si.max_shader_engines = 2;
1599 		rdev->config.si.max_pipes_per_simd = 4;
1600 		rdev->config.si.max_tile_pipes = 12;
1601 		rdev->config.si.max_simds_per_se = 8;
1602 		rdev->config.si.max_backends_per_se = 4;
1603 		rdev->config.si.max_texture_channel_caches = 12;
1604 		rdev->config.si.max_gprs = 256;
1605 		rdev->config.si.max_gs_threads = 32;
1606 		rdev->config.si.max_hw_contexts = 8;
1607 
1608 		rdev->config.si.sc_prim_fifo_size_frontend = 0x20;
1609 		rdev->config.si.sc_prim_fifo_size_backend = 0x100;
1610 		rdev->config.si.sc_hiz_tile_fifo_size = 0x30;
1611 		rdev->config.si.sc_earlyz_tile_fifo_size = 0x130;
1612 		break;
1613 	case CHIP_PITCAIRN:
1614 		rdev->config.si.max_shader_engines = 2;
1615 		rdev->config.si.max_pipes_per_simd = 4;
1616 		rdev->config.si.max_tile_pipes = 8;
1617 		rdev->config.si.max_simds_per_se = 5;
1618 		rdev->config.si.max_backends_per_se = 4;
1619 		rdev->config.si.max_texture_channel_caches = 8;
1620 		rdev->config.si.max_gprs = 256;
1621 		rdev->config.si.max_gs_threads = 32;
1622 		rdev->config.si.max_hw_contexts = 8;
1623 
1624 		rdev->config.si.sc_prim_fifo_size_frontend = 0x20;
1625 		rdev->config.si.sc_prim_fifo_size_backend = 0x100;
1626 		rdev->config.si.sc_hiz_tile_fifo_size = 0x30;
1627 		rdev->config.si.sc_earlyz_tile_fifo_size = 0x130;
1628 		break;
1629 	case CHIP_VERDE:
1630 	default:
1631 		rdev->config.si.max_shader_engines = 1;
1632 		rdev->config.si.max_pipes_per_simd = 4;
1633 		rdev->config.si.max_tile_pipes = 4;
1634 		rdev->config.si.max_simds_per_se = 2;
1635 		rdev->config.si.max_backends_per_se = 4;
1636 		rdev->config.si.max_texture_channel_caches = 4;
1637 		rdev->config.si.max_gprs = 256;
1638 		rdev->config.si.max_gs_threads = 32;
1639 		rdev->config.si.max_hw_contexts = 8;
1640 
1641 		rdev->config.si.sc_prim_fifo_size_frontend = 0x20;
1642 		rdev->config.si.sc_prim_fifo_size_backend = 0x40;
1643 		rdev->config.si.sc_hiz_tile_fifo_size = 0x30;
1644 		rdev->config.si.sc_earlyz_tile_fifo_size = 0x130;
1645 		break;
1646 	}
1647 
1648 	/* Initialize HDP */
1649 	for (i = 0, j = 0; i < 32; i++, j += 0x18) {
1650 		WREG32((0x2c14 + j), 0x00000000);
1651 		WREG32((0x2c18 + j), 0x00000000);
1652 		WREG32((0x2c1c + j), 0x00000000);
1653 		WREG32((0x2c20 + j), 0x00000000);
1654 		WREG32((0x2c24 + j), 0x00000000);
1655 	}
1656 
1657 	WREG32(GRBM_CNTL, GRBM_READ_TIMEOUT(0xff));
1658 
1659 	evergreen_fix_pci_max_read_req_size(rdev);
1660 
1661 	WREG32(BIF_FB_EN, FB_READ_EN | FB_WRITE_EN);
1662 
1663 	mc_shared_chmap = RREG32(MC_SHARED_CHMAP);
1664 	mc_arb_ramcfg = RREG32(MC_ARB_RAMCFG);
1665 
1666 	cc_rb_backend_disable = RREG32(CC_RB_BACKEND_DISABLE);
1667 	cc_gc_shader_array_config = RREG32(CC_GC_SHADER_ARRAY_CONFIG);
1668 	cgts_tcc_disable = 0xffff0000;
1669 	for (i = 0; i < rdev->config.si.max_texture_channel_caches; i++)
1670 		cgts_tcc_disable &= ~(1 << (16 + i));
1671 	gc_user_rb_backend_disable = RREG32(GC_USER_RB_BACKEND_DISABLE);
1672 	gc_user_shader_array_config = RREG32(GC_USER_SHADER_ARRAY_CONFIG);
1673 	cgts_user_tcc_disable = RREG32(CGTS_USER_TCC_DISABLE);
1674 
1675 	rdev->config.si.num_shader_engines = rdev->config.si.max_shader_engines;
1676 	rdev->config.si.num_tile_pipes = rdev->config.si.max_tile_pipes;
1677 	tmp = ((~gc_user_rb_backend_disable) & BACKEND_DISABLE_MASK) >> BACKEND_DISABLE_SHIFT;
1678 	rdev->config.si.num_backends_per_se = r600_count_pipe_bits(tmp);
1679 	tmp = (gc_user_rb_backend_disable & BACKEND_DISABLE_MASK) >> BACKEND_DISABLE_SHIFT;
1680 	rdev->config.si.backend_disable_mask_per_asic =
1681 		si_get_disable_mask_per_asic(rdev, tmp, SI_MAX_BACKENDS_PER_SE_MASK,
1682 					     rdev->config.si.num_shader_engines);
1683 	rdev->config.si.backend_map =
1684 		si_get_tile_pipe_to_backend_map(rdev, rdev->config.si.num_tile_pipes,
1685 						rdev->config.si.num_backends_per_se *
1686 						rdev->config.si.num_shader_engines,
1687 						&rdev->config.si.backend_disable_mask_per_asic,
1688 						rdev->config.si.num_shader_engines);
1689 	tmp = ((~cgts_user_tcc_disable) & TCC_DISABLE_MASK) >> TCC_DISABLE_SHIFT;
1690 	rdev->config.si.num_texture_channel_caches = r600_count_pipe_bits(tmp);
1691 	rdev->config.si.mem_max_burst_length_bytes = 256;
1692 	tmp = (mc_arb_ramcfg & NOOFCOLS_MASK) >> NOOFCOLS_SHIFT;
1693 	rdev->config.si.mem_row_size_in_kb = (4 * (1 << (8 + tmp))) / 1024;
1694 	if (rdev->config.si.mem_row_size_in_kb > 4)
1695 		rdev->config.si.mem_row_size_in_kb = 4;
1696 	/* XXX use MC settings? */
1697 	rdev->config.si.shader_engine_tile_size = 32;
1698 	rdev->config.si.num_gpus = 1;
1699 	rdev->config.si.multi_gpu_tile_size = 64;
1700 
1701 	gb_addr_config = 0;
1702 	switch (rdev->config.si.num_tile_pipes) {
1703 	case 1:
1704 		gb_addr_config |= NUM_PIPES(0);
1705 		break;
1706 	case 2:
1707 		gb_addr_config |= NUM_PIPES(1);
1708 		break;
1709 	case 4:
1710 		gb_addr_config |= NUM_PIPES(2);
1711 		break;
1712 	case 8:
1713 	default:
1714 		gb_addr_config |= NUM_PIPES(3);
1715 		break;
1716 	}
1717 
1718 	tmp = (rdev->config.si.mem_max_burst_length_bytes / 256) - 1;
1719 	gb_addr_config |= PIPE_INTERLEAVE_SIZE(tmp);
1720 	gb_addr_config |= NUM_SHADER_ENGINES(rdev->config.si.num_shader_engines - 1);
1721 	tmp = (rdev->config.si.shader_engine_tile_size / 16) - 1;
1722 	gb_addr_config |= SHADER_ENGINE_TILE_SIZE(tmp);
1723 	switch (rdev->config.si.num_gpus) {
1724 	case 1:
1725 	default:
1726 		gb_addr_config |= NUM_GPUS(0);
1727 		break;
1728 	case 2:
1729 		gb_addr_config |= NUM_GPUS(1);
1730 		break;
1731 	case 4:
1732 		gb_addr_config |= NUM_GPUS(2);
1733 		break;
1734 	}
1735 	switch (rdev->config.si.multi_gpu_tile_size) {
1736 	case 16:
1737 		gb_addr_config |= MULTI_GPU_TILE_SIZE(0);
1738 		break;
1739 	case 32:
1740 	default:
1741 		gb_addr_config |= MULTI_GPU_TILE_SIZE(1);
1742 		break;
1743 	case 64:
1744 		gb_addr_config |= MULTI_GPU_TILE_SIZE(2);
1745 		break;
1746 	case 128:
1747 		gb_addr_config |= MULTI_GPU_TILE_SIZE(3);
1748 		break;
1749 	}
1750 	switch (rdev->config.si.mem_row_size_in_kb) {
1751 	case 1:
1752 	default:
1753 		gb_addr_config |= ROW_SIZE(0);
1754 		break;
1755 	case 2:
1756 		gb_addr_config |= ROW_SIZE(1);
1757 		break;
1758 	case 4:
1759 		gb_addr_config |= ROW_SIZE(2);
1760 		break;
1761 	}
1762 
1763 	tmp = (gb_addr_config & NUM_PIPES_MASK) >> NUM_PIPES_SHIFT;
1764 	rdev->config.si.num_tile_pipes = (1 << tmp);
1765 	tmp = (gb_addr_config & PIPE_INTERLEAVE_SIZE_MASK) >> PIPE_INTERLEAVE_SIZE_SHIFT;
1766 	rdev->config.si.mem_max_burst_length_bytes = (tmp + 1) * 256;
1767 	tmp = (gb_addr_config & NUM_SHADER_ENGINES_MASK) >> NUM_SHADER_ENGINES_SHIFT;
1768 	rdev->config.si.num_shader_engines = tmp + 1;
1769 	tmp = (gb_addr_config & NUM_GPUS_MASK) >> NUM_GPUS_SHIFT;
1770 	rdev->config.si.num_gpus = tmp + 1;
1771 	tmp = (gb_addr_config & MULTI_GPU_TILE_SIZE_MASK) >> MULTI_GPU_TILE_SIZE_SHIFT;
1772 	rdev->config.si.multi_gpu_tile_size = 1 << tmp;
1773 	tmp = (gb_addr_config & ROW_SIZE_MASK) >> ROW_SIZE_SHIFT;
1774 	rdev->config.si.mem_row_size_in_kb = 1 << tmp;
1775 
1776 	gb_backend_map =
1777 		si_get_tile_pipe_to_backend_map(rdev, rdev->config.si.num_tile_pipes,
1778 						rdev->config.si.num_backends_per_se *
1779 						rdev->config.si.num_shader_engines,
1780 						&rdev->config.si.backend_disable_mask_per_asic,
1781 						rdev->config.si.num_shader_engines);
1782 
1783 	/* setup tiling info dword.  gb_addr_config is not adequate since it does
1784 	 * not have bank info, so create a custom tiling dword.
1785 	 * bits 3:0   num_pipes
1786 	 * bits 7:4   num_banks
1787 	 * bits 11:8  group_size
1788 	 * bits 15:12 row_size
1789 	 */
1790 	rdev->config.si.tile_config = 0;
1791 	switch (rdev->config.si.num_tile_pipes) {
1792 	case 1:
1793 		rdev->config.si.tile_config |= (0 << 0);
1794 		break;
1795 	case 2:
1796 		rdev->config.si.tile_config |= (1 << 0);
1797 		break;
1798 	case 4:
1799 		rdev->config.si.tile_config |= (2 << 0);
1800 		break;
1801 	case 8:
1802 	default:
1803 		/* XXX what about 12? */
1804 		rdev->config.si.tile_config |= (3 << 0);
1805 		break;
1806 	}
1807 	rdev->config.si.tile_config |=
1808 		((mc_arb_ramcfg & NOOFBANK_MASK) >> NOOFBANK_SHIFT) << 4;
1809 	rdev->config.si.tile_config |=
1810 		((gb_addr_config & PIPE_INTERLEAVE_SIZE_MASK) >> PIPE_INTERLEAVE_SIZE_SHIFT) << 8;
1811 	rdev->config.si.tile_config |=
1812 		((gb_addr_config & ROW_SIZE_MASK) >> ROW_SIZE_SHIFT) << 12;
1813 
1814 	rdev->config.si.backend_map = gb_backend_map;
1815 	WREG32(GB_ADDR_CONFIG, gb_addr_config);
1816 	WREG32(DMIF_ADDR_CONFIG, gb_addr_config);
1817 	WREG32(DMIF_ADDR_CALC, gb_addr_config);
1818 	WREG32(HDP_ADDR_CONFIG, gb_addr_config);
1819 
1820 	/* primary versions */
1821 	WREG32(CC_RB_BACKEND_DISABLE, cc_rb_backend_disable);
1822 	WREG32(CC_SYS_RB_BACKEND_DISABLE, cc_rb_backend_disable);
1823 	WREG32(CC_GC_SHADER_ARRAY_CONFIG, cc_gc_shader_array_config);
1824 
1825 	WREG32(CGTS_TCC_DISABLE, cgts_tcc_disable);
1826 
1827 	/* user versions */
1828 	WREG32(GC_USER_RB_BACKEND_DISABLE, cc_rb_backend_disable);
1829 	WREG32(GC_USER_SYS_RB_BACKEND_DISABLE, cc_rb_backend_disable);
1830 	WREG32(GC_USER_SHADER_ARRAY_CONFIG, cc_gc_shader_array_config);
1831 
1832 	WREG32(CGTS_USER_TCC_DISABLE, cgts_tcc_disable);
1833 
1834 	si_tiling_mode_table_init(rdev);
1835 
1836 	/* set HW defaults for 3D engine */
1837 	WREG32(CP_QUEUE_THRESHOLDS, (ROQ_IB1_START(0x16) |
1838 				     ROQ_IB2_START(0x2b)));
1839 	WREG32(CP_MEQ_THRESHOLDS, MEQ1_START(0x30) | MEQ2_START(0x60));
1840 
1841 	sx_debug_1 = RREG32(SX_DEBUG_1);
1842 	WREG32(SX_DEBUG_1, sx_debug_1);
1843 
1844 	WREG32(SPI_CONFIG_CNTL_1, VTX_DONE_DELAY(4));
1845 
1846 	WREG32(PA_SC_FIFO_SIZE, (SC_FRONTEND_PRIM_FIFO_SIZE(rdev->config.si.sc_prim_fifo_size_frontend) |
1847 				 SC_BACKEND_PRIM_FIFO_SIZE(rdev->config.si.sc_prim_fifo_size_backend) |
1848 				 SC_HIZ_TILE_FIFO_SIZE(rdev->config.si.sc_hiz_tile_fifo_size) |
1849 				 SC_EARLYZ_TILE_FIFO_SIZE(rdev->config.si.sc_earlyz_tile_fifo_size)));
1850 
1851 	WREG32(VGT_NUM_INSTANCES, 1);
1852 
1853 	WREG32(CP_PERFMON_CNTL, 0);
1854 
1855 	WREG32(SQ_CONFIG, 0);
1856 
1857 	WREG32(PA_SC_FORCE_EOV_MAX_CNTS, (FORCE_EOV_MAX_CLK_CNT(4095) |
1858 					  FORCE_EOV_MAX_REZ_CNT(255)));
1859 
1860 	WREG32(VGT_CACHE_INVALIDATION, CACHE_INVALIDATION(VC_AND_TC) |
1861 	       AUTO_INVLD_EN(ES_AND_GS_AUTO));
1862 
1863 	WREG32(VGT_GS_VERTEX_REUSE, 16);
1864 	WREG32(PA_SC_LINE_STIPPLE_STATE, 0);
1865 
1866 	WREG32(CB_PERFCOUNTER0_SELECT0, 0);
1867 	WREG32(CB_PERFCOUNTER0_SELECT1, 0);
1868 	WREG32(CB_PERFCOUNTER1_SELECT0, 0);
1869 	WREG32(CB_PERFCOUNTER1_SELECT1, 0);
1870 	WREG32(CB_PERFCOUNTER2_SELECT0, 0);
1871 	WREG32(CB_PERFCOUNTER2_SELECT1, 0);
1872 	WREG32(CB_PERFCOUNTER3_SELECT0, 0);
1873 	WREG32(CB_PERFCOUNTER3_SELECT1, 0);
1874 
1875 	tmp = RREG32(HDP_MISC_CNTL);
1876 	tmp |= HDP_FLUSH_INVALIDATE_CACHE;
1877 	WREG32(HDP_MISC_CNTL, tmp);
1878 
1879 	hdp_host_path_cntl = RREG32(HDP_HOST_PATH_CNTL);
1880 	WREG32(HDP_HOST_PATH_CNTL, hdp_host_path_cntl);
1881 
1882 	WREG32(PA_CL_ENHANCE, CLIP_VTX_REORDER_ENA | NUM_CLIP_SEQ(3));
1883 
1884 	udelay(50);
1885 }
1886 
1887 /*
1888  * GPU scratch registers helpers function.
1889  */
si_scratch_init(struct radeon_device * rdev)1890 static void si_scratch_init(struct radeon_device *rdev)
1891 {
1892 	int i;
1893 
1894 	rdev->scratch.num_reg = 7;
1895 	rdev->scratch.reg_base = SCRATCH_REG0;
1896 	for (i = 0; i < rdev->scratch.num_reg; i++) {
1897 		rdev->scratch.free[i] = true;
1898 		rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
1899 	}
1900 }
1901 
si_fence_ring_emit(struct radeon_device * rdev,struct radeon_fence * fence)1902 void si_fence_ring_emit(struct radeon_device *rdev,
1903 			struct radeon_fence *fence)
1904 {
1905 	struct radeon_ring *ring = &rdev->ring[fence->ring];
1906 	u64 addr = rdev->fence_drv[fence->ring].gpu_addr;
1907 
1908 	/* flush read cache over gart */
1909 	radeon_ring_write(ring, PACKET3(PACKET3_SET_CONFIG_REG, 1));
1910 	radeon_ring_write(ring, (CP_COHER_CNTL2 - PACKET3_SET_CONFIG_REG_START) >> 2);
1911 	radeon_ring_write(ring, 0);
1912 	radeon_ring_write(ring, PACKET3(PACKET3_SURFACE_SYNC, 3));
1913 	radeon_ring_write(ring, PACKET3_TCL1_ACTION_ENA |
1914 			  PACKET3_TC_ACTION_ENA |
1915 			  PACKET3_SH_KCACHE_ACTION_ENA |
1916 			  PACKET3_SH_ICACHE_ACTION_ENA);
1917 	radeon_ring_write(ring, 0xFFFFFFFF);
1918 	radeon_ring_write(ring, 0);
1919 	radeon_ring_write(ring, 10); /* poll interval */
1920 	/* EVENT_WRITE_EOP - flush caches, send int */
1921 	radeon_ring_write(ring, PACKET3(PACKET3_EVENT_WRITE_EOP, 4));
1922 	radeon_ring_write(ring, EVENT_TYPE(CACHE_FLUSH_AND_INV_TS_EVENT) | EVENT_INDEX(5));
1923 	radeon_ring_write(ring, addr & 0xffffffff);
1924 	radeon_ring_write(ring, (upper_32_bits(addr) & 0xff) | DATA_SEL(1) | INT_SEL(2));
1925 	radeon_ring_write(ring, fence->seq);
1926 	radeon_ring_write(ring, 0);
1927 }
1928 
1929 /*
1930  * IB stuff
1931  */
si_ring_ib_execute(struct radeon_device * rdev,struct radeon_ib * ib)1932 void si_ring_ib_execute(struct radeon_device *rdev, struct radeon_ib *ib)
1933 {
1934 	struct radeon_ring *ring = &rdev->ring[ib->fence->ring];
1935 	u32 header;
1936 
1937 	if (ib->is_const_ib)
1938 		header = PACKET3(PACKET3_INDIRECT_BUFFER_CONST, 2);
1939 	else
1940 		header = PACKET3(PACKET3_INDIRECT_BUFFER, 2);
1941 
1942 	radeon_ring_write(ring, header);
1943 	radeon_ring_write(ring,
1944 #ifdef __BIG_ENDIAN
1945 			  (2 << 0) |
1946 #endif
1947 			  (ib->gpu_addr & 0xFFFFFFFC));
1948 	radeon_ring_write(ring, upper_32_bits(ib->gpu_addr) & 0xFFFF);
1949 	radeon_ring_write(ring, ib->length_dw | (ib->vm_id << 24));
1950 
1951 	/* flush read cache over gart for this vmid */
1952 	radeon_ring_write(ring, PACKET3(PACKET3_SET_CONFIG_REG, 1));
1953 	radeon_ring_write(ring, (CP_COHER_CNTL2 - PACKET3_SET_CONFIG_REG_START) >> 2);
1954 	radeon_ring_write(ring, ib->vm_id);
1955 	radeon_ring_write(ring, PACKET3(PACKET3_SURFACE_SYNC, 3));
1956 	radeon_ring_write(ring, PACKET3_TCL1_ACTION_ENA |
1957 			  PACKET3_TC_ACTION_ENA |
1958 			  PACKET3_SH_KCACHE_ACTION_ENA |
1959 			  PACKET3_SH_ICACHE_ACTION_ENA);
1960 	radeon_ring_write(ring, 0xFFFFFFFF);
1961 	radeon_ring_write(ring, 0);
1962 	radeon_ring_write(ring, 10); /* poll interval */
1963 }
1964 
1965 /*
1966  * CP.
1967  */
si_cp_enable(struct radeon_device * rdev,bool enable)1968 static void si_cp_enable(struct radeon_device *rdev, bool enable)
1969 {
1970 	if (enable)
1971 		WREG32(CP_ME_CNTL, 0);
1972 	else {
1973 		radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
1974 		WREG32(CP_ME_CNTL, (CP_ME_HALT | CP_PFP_HALT | CP_CE_HALT));
1975 		WREG32(SCRATCH_UMSK, 0);
1976 	}
1977 	udelay(50);
1978 }
1979 
si_cp_load_microcode(struct radeon_device * rdev)1980 static int si_cp_load_microcode(struct radeon_device *rdev)
1981 {
1982 	const __be32 *fw_data;
1983 	int i;
1984 
1985 	if (!rdev->me_fw || !rdev->pfp_fw)
1986 		return -EINVAL;
1987 
1988 	si_cp_enable(rdev, false);
1989 
1990 	/* PFP */
1991 	fw_data = (const __be32 *)rdev->pfp_fw->data;
1992 	WREG32(CP_PFP_UCODE_ADDR, 0);
1993 	for (i = 0; i < SI_PFP_UCODE_SIZE; i++)
1994 		WREG32(CP_PFP_UCODE_DATA, be32_to_cpup(fw_data++));
1995 	WREG32(CP_PFP_UCODE_ADDR, 0);
1996 
1997 	/* CE */
1998 	fw_data = (const __be32 *)rdev->ce_fw->data;
1999 	WREG32(CP_CE_UCODE_ADDR, 0);
2000 	for (i = 0; i < SI_CE_UCODE_SIZE; i++)
2001 		WREG32(CP_CE_UCODE_DATA, be32_to_cpup(fw_data++));
2002 	WREG32(CP_CE_UCODE_ADDR, 0);
2003 
2004 	/* ME */
2005 	fw_data = (const __be32 *)rdev->me_fw->data;
2006 	WREG32(CP_ME_RAM_WADDR, 0);
2007 	for (i = 0; i < SI_PM4_UCODE_SIZE; i++)
2008 		WREG32(CP_ME_RAM_DATA, be32_to_cpup(fw_data++));
2009 	WREG32(CP_ME_RAM_WADDR, 0);
2010 
2011 	WREG32(CP_PFP_UCODE_ADDR, 0);
2012 	WREG32(CP_CE_UCODE_ADDR, 0);
2013 	WREG32(CP_ME_RAM_WADDR, 0);
2014 	WREG32(CP_ME_RAM_RADDR, 0);
2015 	return 0;
2016 }
2017 
si_cp_start(struct radeon_device * rdev)2018 static int si_cp_start(struct radeon_device *rdev)
2019 {
2020 	struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
2021 	int r, i;
2022 
2023 	r = radeon_ring_lock(rdev, ring, 7 + 4);
2024 	if (r) {
2025 		DRM_ERROR("radeon: cp failed to lock ring (%d).\n", r);
2026 		return r;
2027 	}
2028 	/* init the CP */
2029 	radeon_ring_write(ring, PACKET3(PACKET3_ME_INITIALIZE, 5));
2030 	radeon_ring_write(ring, 0x1);
2031 	radeon_ring_write(ring, 0x0);
2032 	radeon_ring_write(ring, rdev->config.si.max_hw_contexts - 1);
2033 	radeon_ring_write(ring, PACKET3_ME_INITIALIZE_DEVICE_ID(1));
2034 	radeon_ring_write(ring, 0);
2035 	radeon_ring_write(ring, 0);
2036 
2037 	/* init the CE partitions */
2038 	radeon_ring_write(ring, PACKET3(PACKET3_SET_BASE, 2));
2039 	radeon_ring_write(ring, PACKET3_BASE_INDEX(CE_PARTITION_BASE));
2040 	radeon_ring_write(ring, 0xc000);
2041 	radeon_ring_write(ring, 0xe000);
2042 	radeon_ring_unlock_commit(rdev, ring);
2043 
2044 	si_cp_enable(rdev, true);
2045 
2046 	r = radeon_ring_lock(rdev, ring, si_default_size + 10);
2047 	if (r) {
2048 		DRM_ERROR("radeon: cp failed to lock ring (%d).\n", r);
2049 		return r;
2050 	}
2051 
2052 	/* setup clear context state */
2053 	radeon_ring_write(ring, PACKET3(PACKET3_PREAMBLE_CNTL, 0));
2054 	radeon_ring_write(ring, PACKET3_PREAMBLE_BEGIN_CLEAR_STATE);
2055 
2056 	for (i = 0; i < si_default_size; i++)
2057 		radeon_ring_write(ring, si_default_state[i]);
2058 
2059 	radeon_ring_write(ring, PACKET3(PACKET3_PREAMBLE_CNTL, 0));
2060 	radeon_ring_write(ring, PACKET3_PREAMBLE_END_CLEAR_STATE);
2061 
2062 	/* set clear context state */
2063 	radeon_ring_write(ring, PACKET3(PACKET3_CLEAR_STATE, 0));
2064 	radeon_ring_write(ring, 0);
2065 
2066 	radeon_ring_write(ring, PACKET3(PACKET3_SET_CONTEXT_REG, 2));
2067 	radeon_ring_write(ring, 0x00000316);
2068 	radeon_ring_write(ring, 0x0000000e); /* VGT_VERTEX_REUSE_BLOCK_CNTL */
2069 	radeon_ring_write(ring, 0x00000010); /* VGT_OUT_DEALLOC_CNTL */
2070 
2071 	radeon_ring_unlock_commit(rdev, ring);
2072 
2073 	for (i = RADEON_RING_TYPE_GFX_INDEX; i <= CAYMAN_RING_TYPE_CP2_INDEX; ++i) {
2074 		ring = &rdev->ring[i];
2075 		r = radeon_ring_lock(rdev, ring, 2);
2076 
2077 		/* clear the compute context state */
2078 		radeon_ring_write(ring, PACKET3_COMPUTE(PACKET3_CLEAR_STATE, 0));
2079 		radeon_ring_write(ring, 0);
2080 
2081 		radeon_ring_unlock_commit(rdev, ring);
2082 	}
2083 
2084 	return 0;
2085 }
2086 
si_cp_fini(struct radeon_device * rdev)2087 static void si_cp_fini(struct radeon_device *rdev)
2088 {
2089 	si_cp_enable(rdev, false);
2090 	radeon_ring_fini(rdev, &rdev->ring[RADEON_RING_TYPE_GFX_INDEX]);
2091 	radeon_ring_fini(rdev, &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX]);
2092 	radeon_ring_fini(rdev, &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX]);
2093 }
2094 
si_cp_resume(struct radeon_device * rdev)2095 static int si_cp_resume(struct radeon_device *rdev)
2096 {
2097 	struct radeon_ring *ring;
2098 	u32 tmp;
2099 	u32 rb_bufsz;
2100 	int r;
2101 
2102 	/* Reset cp; if cp is reset, then PA, SH, VGT also need to be reset */
2103 	WREG32(GRBM_SOFT_RESET, (SOFT_RESET_CP |
2104 				 SOFT_RESET_PA |
2105 				 SOFT_RESET_VGT |
2106 				 SOFT_RESET_SPI |
2107 				 SOFT_RESET_SX));
2108 	RREG32(GRBM_SOFT_RESET);
2109 	mdelay(15);
2110 	WREG32(GRBM_SOFT_RESET, 0);
2111 	RREG32(GRBM_SOFT_RESET);
2112 
2113 	WREG32(CP_SEM_WAIT_TIMER, 0x0);
2114 	WREG32(CP_SEM_INCOMPLETE_TIMER_CNTL, 0x0);
2115 
2116 	/* Set the write pointer delay */
2117 	WREG32(CP_RB_WPTR_DELAY, 0);
2118 
2119 	WREG32(CP_DEBUG, 0);
2120 	WREG32(SCRATCH_ADDR, ((rdev->wb.gpu_addr + RADEON_WB_SCRATCH_OFFSET) >> 8) & 0xFFFFFFFF);
2121 
2122 	/* ring 0 - compute and gfx */
2123 	/* Set ring buffer size */
2124 	ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
2125 	rb_bufsz = drm_order(ring->ring_size / 8);
2126 	tmp = (drm_order(RADEON_GPU_PAGE_SIZE/8) << 8) | rb_bufsz;
2127 #ifdef __BIG_ENDIAN
2128 	tmp |= BUF_SWAP_32BIT;
2129 #endif
2130 	WREG32(CP_RB0_CNTL, tmp);
2131 
2132 	/* Initialize the ring buffer's read and write pointers */
2133 	WREG32(CP_RB0_CNTL, tmp | RB_RPTR_WR_ENA);
2134 	ring->wptr = 0;
2135 	WREG32(CP_RB0_WPTR, ring->wptr);
2136 
2137 	/* set the wb address wether it's enabled or not */
2138 	WREG32(CP_RB0_RPTR_ADDR, (rdev->wb.gpu_addr + RADEON_WB_CP_RPTR_OFFSET) & 0xFFFFFFFC);
2139 	WREG32(CP_RB0_RPTR_ADDR_HI, upper_32_bits(rdev->wb.gpu_addr + RADEON_WB_CP_RPTR_OFFSET) & 0xFF);
2140 
2141 	if (rdev->wb.enabled)
2142 		WREG32(SCRATCH_UMSK, 0xff);
2143 	else {
2144 		tmp |= RB_NO_UPDATE;
2145 		WREG32(SCRATCH_UMSK, 0);
2146 	}
2147 
2148 	mdelay(1);
2149 	WREG32(CP_RB0_CNTL, tmp);
2150 
2151 	WREG32(CP_RB0_BASE, ring->gpu_addr >> 8);
2152 
2153 	ring->rptr = RREG32(CP_RB0_RPTR);
2154 
2155 	/* ring1  - compute only */
2156 	/* Set ring buffer size */
2157 	ring = &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX];
2158 	rb_bufsz = drm_order(ring->ring_size / 8);
2159 	tmp = (drm_order(RADEON_GPU_PAGE_SIZE/8) << 8) | rb_bufsz;
2160 #ifdef __BIG_ENDIAN
2161 	tmp |= BUF_SWAP_32BIT;
2162 #endif
2163 	WREG32(CP_RB1_CNTL, tmp);
2164 
2165 	/* Initialize the ring buffer's read and write pointers */
2166 	WREG32(CP_RB1_CNTL, tmp | RB_RPTR_WR_ENA);
2167 	ring->wptr = 0;
2168 	WREG32(CP_RB1_WPTR, ring->wptr);
2169 
2170 	/* set the wb address wether it's enabled or not */
2171 	WREG32(CP_RB1_RPTR_ADDR, (rdev->wb.gpu_addr + RADEON_WB_CP1_RPTR_OFFSET) & 0xFFFFFFFC);
2172 	WREG32(CP_RB1_RPTR_ADDR_HI, upper_32_bits(rdev->wb.gpu_addr + RADEON_WB_CP1_RPTR_OFFSET) & 0xFF);
2173 
2174 	mdelay(1);
2175 	WREG32(CP_RB1_CNTL, tmp);
2176 
2177 	WREG32(CP_RB1_BASE, ring->gpu_addr >> 8);
2178 
2179 	ring->rptr = RREG32(CP_RB1_RPTR);
2180 
2181 	/* ring2 - compute only */
2182 	/* Set ring buffer size */
2183 	ring = &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX];
2184 	rb_bufsz = drm_order(ring->ring_size / 8);
2185 	tmp = (drm_order(RADEON_GPU_PAGE_SIZE/8) << 8) | rb_bufsz;
2186 #ifdef __BIG_ENDIAN
2187 	tmp |= BUF_SWAP_32BIT;
2188 #endif
2189 	WREG32(CP_RB2_CNTL, tmp);
2190 
2191 	/* Initialize the ring buffer's read and write pointers */
2192 	WREG32(CP_RB2_CNTL, tmp | RB_RPTR_WR_ENA);
2193 	ring->wptr = 0;
2194 	WREG32(CP_RB2_WPTR, ring->wptr);
2195 
2196 	/* set the wb address wether it's enabled or not */
2197 	WREG32(CP_RB2_RPTR_ADDR, (rdev->wb.gpu_addr + RADEON_WB_CP2_RPTR_OFFSET) & 0xFFFFFFFC);
2198 	WREG32(CP_RB2_RPTR_ADDR_HI, upper_32_bits(rdev->wb.gpu_addr + RADEON_WB_CP2_RPTR_OFFSET) & 0xFF);
2199 
2200 	mdelay(1);
2201 	WREG32(CP_RB2_CNTL, tmp);
2202 
2203 	WREG32(CP_RB2_BASE, ring->gpu_addr >> 8);
2204 
2205 	ring->rptr = RREG32(CP_RB2_RPTR);
2206 
2207 	/* start the rings */
2208 	si_cp_start(rdev);
2209 	rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready = true;
2210 	rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX].ready = true;
2211 	rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX].ready = true;
2212 	r = radeon_ring_test(rdev, RADEON_RING_TYPE_GFX_INDEX, &rdev->ring[RADEON_RING_TYPE_GFX_INDEX]);
2213 	if (r) {
2214 		rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready = false;
2215 		rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX].ready = false;
2216 		rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX].ready = false;
2217 		return r;
2218 	}
2219 	r = radeon_ring_test(rdev, CAYMAN_RING_TYPE_CP1_INDEX, &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX]);
2220 	if (r) {
2221 		rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX].ready = false;
2222 	}
2223 	r = radeon_ring_test(rdev, CAYMAN_RING_TYPE_CP2_INDEX, &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX]);
2224 	if (r) {
2225 		rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX].ready = false;
2226 	}
2227 
2228 	return 0;
2229 }
2230 
si_gpu_is_lockup(struct radeon_device * rdev,struct radeon_ring * ring)2231 bool si_gpu_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
2232 {
2233 	u32 srbm_status;
2234 	u32 grbm_status, grbm_status2;
2235 	u32 grbm_status_se0, grbm_status_se1;
2236 	struct r100_gpu_lockup *lockup = &rdev->config.si.lockup;
2237 	int r;
2238 
2239 	srbm_status = RREG32(SRBM_STATUS);
2240 	grbm_status = RREG32(GRBM_STATUS);
2241 	grbm_status2 = RREG32(GRBM_STATUS2);
2242 	grbm_status_se0 = RREG32(GRBM_STATUS_SE0);
2243 	grbm_status_se1 = RREG32(GRBM_STATUS_SE1);
2244 	if (!(grbm_status & GUI_ACTIVE)) {
2245 		r100_gpu_lockup_update(lockup, ring);
2246 		return false;
2247 	}
2248 	/* force CP activities */
2249 	r = radeon_ring_lock(rdev, ring, 2);
2250 	if (!r) {
2251 		/* PACKET2 NOP */
2252 		radeon_ring_write(ring, 0x80000000);
2253 		radeon_ring_write(ring, 0x80000000);
2254 		radeon_ring_unlock_commit(rdev, ring);
2255 	}
2256 	/* XXX deal with CP0,1,2 */
2257 	ring->rptr = RREG32(ring->rptr_reg);
2258 	return r100_gpu_cp_is_lockup(rdev, lockup, ring);
2259 }
2260 
si_gpu_soft_reset(struct radeon_device * rdev)2261 static int si_gpu_soft_reset(struct radeon_device *rdev)
2262 {
2263 	struct evergreen_mc_save save;
2264 	u32 grbm_reset = 0;
2265 
2266 	if (!(RREG32(GRBM_STATUS) & GUI_ACTIVE))
2267 		return 0;
2268 
2269 	dev_info(rdev->dev, "GPU softreset \n");
2270 	dev_info(rdev->dev, "  GRBM_STATUS=0x%08X\n",
2271 		RREG32(GRBM_STATUS));
2272 	dev_info(rdev->dev, "  GRBM_STATUS2=0x%08X\n",
2273 		RREG32(GRBM_STATUS2));
2274 	dev_info(rdev->dev, "  GRBM_STATUS_SE0=0x%08X\n",
2275 		RREG32(GRBM_STATUS_SE0));
2276 	dev_info(rdev->dev, "  GRBM_STATUS_SE1=0x%08X\n",
2277 		RREG32(GRBM_STATUS_SE1));
2278 	dev_info(rdev->dev, "  SRBM_STATUS=0x%08X\n",
2279 		RREG32(SRBM_STATUS));
2280 	evergreen_mc_stop(rdev, &save);
2281 	if (radeon_mc_wait_for_idle(rdev)) {
2282 		dev_warn(rdev->dev, "Wait for MC idle timedout !\n");
2283 	}
2284 	/* Disable CP parsing/prefetching */
2285 	WREG32(CP_ME_CNTL, CP_ME_HALT | CP_PFP_HALT | CP_CE_HALT);
2286 
2287 	/* reset all the gfx blocks */
2288 	grbm_reset = (SOFT_RESET_CP |
2289 		      SOFT_RESET_CB |
2290 		      SOFT_RESET_DB |
2291 		      SOFT_RESET_GDS |
2292 		      SOFT_RESET_PA |
2293 		      SOFT_RESET_SC |
2294 		      SOFT_RESET_SPI |
2295 		      SOFT_RESET_SX |
2296 		      SOFT_RESET_TC |
2297 		      SOFT_RESET_TA |
2298 		      SOFT_RESET_VGT |
2299 		      SOFT_RESET_IA);
2300 
2301 	dev_info(rdev->dev, "  GRBM_SOFT_RESET=0x%08X\n", grbm_reset);
2302 	WREG32(GRBM_SOFT_RESET, grbm_reset);
2303 	(void)RREG32(GRBM_SOFT_RESET);
2304 	udelay(50);
2305 	WREG32(GRBM_SOFT_RESET, 0);
2306 	(void)RREG32(GRBM_SOFT_RESET);
2307 	/* Wait a little for things to settle down */
2308 	udelay(50);
2309 	dev_info(rdev->dev, "  GRBM_STATUS=0x%08X\n",
2310 		RREG32(GRBM_STATUS));
2311 	dev_info(rdev->dev, "  GRBM_STATUS2=0x%08X\n",
2312 		RREG32(GRBM_STATUS2));
2313 	dev_info(rdev->dev, "  GRBM_STATUS_SE0=0x%08X\n",
2314 		RREG32(GRBM_STATUS_SE0));
2315 	dev_info(rdev->dev, "  GRBM_STATUS_SE1=0x%08X\n",
2316 		RREG32(GRBM_STATUS_SE1));
2317 	dev_info(rdev->dev, "  SRBM_STATUS=0x%08X\n",
2318 		RREG32(SRBM_STATUS));
2319 	evergreen_mc_resume(rdev, &save);
2320 	return 0;
2321 }
2322 
si_asic_reset(struct radeon_device * rdev)2323 int si_asic_reset(struct radeon_device *rdev)
2324 {
2325 	return si_gpu_soft_reset(rdev);
2326 }
2327 
2328 /* MC */
si_mc_program(struct radeon_device * rdev)2329 static void si_mc_program(struct radeon_device *rdev)
2330 {
2331 	struct evergreen_mc_save save;
2332 	u32 tmp;
2333 	int i, j;
2334 
2335 	/* Initialize HDP */
2336 	for (i = 0, j = 0; i < 32; i++, j += 0x18) {
2337 		WREG32((0x2c14 + j), 0x00000000);
2338 		WREG32((0x2c18 + j), 0x00000000);
2339 		WREG32((0x2c1c + j), 0x00000000);
2340 		WREG32((0x2c20 + j), 0x00000000);
2341 		WREG32((0x2c24 + j), 0x00000000);
2342 	}
2343 	WREG32(HDP_REG_COHERENCY_FLUSH_CNTL, 0);
2344 
2345 	evergreen_mc_stop(rdev, &save);
2346 	if (radeon_mc_wait_for_idle(rdev)) {
2347 		dev_warn(rdev->dev, "Wait for MC idle timedout !\n");
2348 	}
2349 	/* Lockout access through VGA aperture*/
2350 	WREG32(VGA_HDP_CONTROL, VGA_MEMORY_DISABLE);
2351 	/* Update configuration */
2352 	WREG32(MC_VM_SYSTEM_APERTURE_LOW_ADDR,
2353 	       rdev->mc.vram_start >> 12);
2354 	WREG32(MC_VM_SYSTEM_APERTURE_HIGH_ADDR,
2355 	       rdev->mc.vram_end >> 12);
2356 	WREG32(MC_VM_SYSTEM_APERTURE_DEFAULT_ADDR,
2357 	       rdev->vram_scratch.gpu_addr >> 12);
2358 	tmp = ((rdev->mc.vram_end >> 24) & 0xFFFF) << 16;
2359 	tmp |= ((rdev->mc.vram_start >> 24) & 0xFFFF);
2360 	WREG32(MC_VM_FB_LOCATION, tmp);
2361 	/* XXX double check these! */
2362 	WREG32(HDP_NONSURFACE_BASE, (rdev->mc.vram_start >> 8));
2363 	WREG32(HDP_NONSURFACE_INFO, (2 << 7) | (1 << 30));
2364 	WREG32(HDP_NONSURFACE_SIZE, 0x3FFFFFFF);
2365 	WREG32(MC_VM_AGP_BASE, 0);
2366 	WREG32(MC_VM_AGP_TOP, 0x0FFFFFFF);
2367 	WREG32(MC_VM_AGP_BOT, 0x0FFFFFFF);
2368 	if (radeon_mc_wait_for_idle(rdev)) {
2369 		dev_warn(rdev->dev, "Wait for MC idle timedout !\n");
2370 	}
2371 	evergreen_mc_resume(rdev, &save);
2372 	/* we need to own VRAM, so turn off the VGA renderer here
2373 	 * to stop it overwriting our objects */
2374 	rv515_vga_render_disable(rdev);
2375 }
2376 
2377 /* SI MC address space is 40 bits */
si_vram_location(struct radeon_device * rdev,struct radeon_mc * mc,u64 base)2378 static void si_vram_location(struct radeon_device *rdev,
2379 			     struct radeon_mc *mc, u64 base)
2380 {
2381 	mc->vram_start = base;
2382 	if (mc->mc_vram_size > (0xFFFFFFFFFFULL - base + 1)) {
2383 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
2384 		mc->real_vram_size = mc->aper_size;
2385 		mc->mc_vram_size = mc->aper_size;
2386 	}
2387 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
2388 	dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
2389 			mc->mc_vram_size >> 20, mc->vram_start,
2390 			mc->vram_end, mc->real_vram_size >> 20);
2391 }
2392 
si_gtt_location(struct radeon_device * rdev,struct radeon_mc * mc)2393 static void si_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
2394 {
2395 	u64 size_af, size_bf;
2396 
2397 	size_af = ((0xFFFFFFFFFFULL - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
2398 	size_bf = mc->vram_start & ~mc->gtt_base_align;
2399 	if (size_bf > size_af) {
2400 		if (mc->gtt_size > size_bf) {
2401 			dev_warn(rdev->dev, "limiting GTT\n");
2402 			mc->gtt_size = size_bf;
2403 		}
2404 		mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
2405 	} else {
2406 		if (mc->gtt_size > size_af) {
2407 			dev_warn(rdev->dev, "limiting GTT\n");
2408 			mc->gtt_size = size_af;
2409 		}
2410 		mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
2411 	}
2412 	mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
2413 	dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
2414 			mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
2415 }
2416 
si_vram_gtt_location(struct radeon_device * rdev,struct radeon_mc * mc)2417 static void si_vram_gtt_location(struct radeon_device *rdev,
2418 				 struct radeon_mc *mc)
2419 {
2420 	if (mc->mc_vram_size > 0xFFC0000000ULL) {
2421 		/* leave room for at least 1024M GTT */
2422 		dev_warn(rdev->dev, "limiting VRAM\n");
2423 		mc->real_vram_size = 0xFFC0000000ULL;
2424 		mc->mc_vram_size = 0xFFC0000000ULL;
2425 	}
2426 	si_vram_location(rdev, &rdev->mc, 0);
2427 	rdev->mc.gtt_base_align = 0;
2428 	si_gtt_location(rdev, mc);
2429 }
2430 
si_mc_init(struct radeon_device * rdev)2431 static int si_mc_init(struct radeon_device *rdev)
2432 {
2433 	u32 tmp;
2434 	int chansize, numchan;
2435 
2436 	/* Get VRAM informations */
2437 	rdev->mc.vram_is_ddr = true;
2438 	tmp = RREG32(MC_ARB_RAMCFG);
2439 	if (tmp & CHANSIZE_OVERRIDE) {
2440 		chansize = 16;
2441 	} else if (tmp & CHANSIZE_MASK) {
2442 		chansize = 64;
2443 	} else {
2444 		chansize = 32;
2445 	}
2446 	tmp = RREG32(MC_SHARED_CHMAP);
2447 	switch ((tmp & NOOFCHAN_MASK) >> NOOFCHAN_SHIFT) {
2448 	case 0:
2449 	default:
2450 		numchan = 1;
2451 		break;
2452 	case 1:
2453 		numchan = 2;
2454 		break;
2455 	case 2:
2456 		numchan = 4;
2457 		break;
2458 	case 3:
2459 		numchan = 8;
2460 		break;
2461 	case 4:
2462 		numchan = 3;
2463 		break;
2464 	case 5:
2465 		numchan = 6;
2466 		break;
2467 	case 6:
2468 		numchan = 10;
2469 		break;
2470 	case 7:
2471 		numchan = 12;
2472 		break;
2473 	case 8:
2474 		numchan = 16;
2475 		break;
2476 	}
2477 	rdev->mc.vram_width = numchan * chansize;
2478 	/* Could aper size report 0 ? */
2479 	rdev->mc.aper_base = pci_resource_start(rdev->pdev, 0);
2480 	rdev->mc.aper_size = pci_resource_len(rdev->pdev, 0);
2481 	/* size in MB on si */
2482 	rdev->mc.mc_vram_size = RREG32(CONFIG_MEMSIZE) * 1024ULL * 1024ULL;
2483 	rdev->mc.real_vram_size = RREG32(CONFIG_MEMSIZE) * 1024ULL * 1024ULL;
2484 	rdev->mc.visible_vram_size = rdev->mc.aper_size;
2485 	si_vram_gtt_location(rdev, &rdev->mc);
2486 	radeon_update_bandwidth_info(rdev);
2487 
2488 	return 0;
2489 }
2490 
2491 /*
2492  * GART
2493  */
si_pcie_gart_tlb_flush(struct radeon_device * rdev)2494 void si_pcie_gart_tlb_flush(struct radeon_device *rdev)
2495 {
2496 	/* flush hdp cache */
2497 	WREG32(HDP_MEM_COHERENCY_FLUSH_CNTL, 0x1);
2498 
2499 	/* bits 0-15 are the VM contexts0-15 */
2500 	WREG32(VM_INVALIDATE_REQUEST, 1);
2501 }
2502 
si_pcie_gart_enable(struct radeon_device * rdev)2503 int si_pcie_gart_enable(struct radeon_device *rdev)
2504 {
2505 	int r, i;
2506 
2507 	if (rdev->gart.robj == NULL) {
2508 		dev_err(rdev->dev, "No VRAM object for PCIE GART.\n");
2509 		return -EINVAL;
2510 	}
2511 	r = radeon_gart_table_vram_pin(rdev);
2512 	if (r)
2513 		return r;
2514 	radeon_gart_restore(rdev);
2515 	/* Setup TLB control */
2516 	WREG32(MC_VM_MX_L1_TLB_CNTL,
2517 	       (0xA << 7) |
2518 	       ENABLE_L1_TLB |
2519 	       SYSTEM_ACCESS_MODE_NOT_IN_SYS |
2520 	       ENABLE_ADVANCED_DRIVER_MODEL |
2521 	       SYSTEM_APERTURE_UNMAPPED_ACCESS_PASS_THRU);
2522 	/* Setup L2 cache */
2523 	WREG32(VM_L2_CNTL, ENABLE_L2_CACHE |
2524 	       ENABLE_L2_PTE_CACHE_LRU_UPDATE_BY_WRITE |
2525 	       ENABLE_L2_PDE0_CACHE_LRU_UPDATE_BY_WRITE |
2526 	       EFFECTIVE_L2_QUEUE_SIZE(7) |
2527 	       CONTEXT1_IDENTITY_ACCESS_MODE(1));
2528 	WREG32(VM_L2_CNTL2, INVALIDATE_ALL_L1_TLBS | INVALIDATE_L2_CACHE);
2529 	WREG32(VM_L2_CNTL3, L2_CACHE_BIGK_ASSOCIATIVITY |
2530 	       L2_CACHE_BIGK_FRAGMENT_SIZE(0));
2531 	/* setup context0 */
2532 	WREG32(VM_CONTEXT0_PAGE_TABLE_START_ADDR, rdev->mc.gtt_start >> 12);
2533 	WREG32(VM_CONTEXT0_PAGE_TABLE_END_ADDR, rdev->mc.gtt_end >> 12);
2534 	WREG32(VM_CONTEXT0_PAGE_TABLE_BASE_ADDR, rdev->gart.table_addr >> 12);
2535 	WREG32(VM_CONTEXT0_PROTECTION_FAULT_DEFAULT_ADDR,
2536 			(u32)(rdev->dummy_page.addr >> 12));
2537 	WREG32(VM_CONTEXT0_CNTL2, 0);
2538 	WREG32(VM_CONTEXT0_CNTL, (ENABLE_CONTEXT | PAGE_TABLE_DEPTH(0) |
2539 				  RANGE_PROTECTION_FAULT_ENABLE_DEFAULT));
2540 
2541 	WREG32(0x15D4, 0);
2542 	WREG32(0x15D8, 0);
2543 	WREG32(0x15DC, 0);
2544 
2545 	/* empty context1-15 */
2546 	/* FIXME start with 4G, once using 2 level pt switch to full
2547 	 * vm size space
2548 	 */
2549 	/* set vm size, must be a multiple of 4 */
2550 	WREG32(VM_CONTEXT1_PAGE_TABLE_START_ADDR, 0);
2551 	WREG32(VM_CONTEXT1_PAGE_TABLE_END_ADDR, rdev->vm_manager.max_pfn);
2552 	for (i = 1; i < 16; i++) {
2553 		if (i < 8)
2554 			WREG32(VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (i << 2),
2555 			       rdev->gart.table_addr >> 12);
2556 		else
2557 			WREG32(VM_CONTEXT8_PAGE_TABLE_BASE_ADDR + ((i - 8) << 2),
2558 			       rdev->gart.table_addr >> 12);
2559 	}
2560 
2561 	/* enable context1-15 */
2562 	WREG32(VM_CONTEXT1_PROTECTION_FAULT_DEFAULT_ADDR,
2563 	       (u32)(rdev->dummy_page.addr >> 12));
2564 	WREG32(VM_CONTEXT1_CNTL2, 0);
2565 	WREG32(VM_CONTEXT1_CNTL, ENABLE_CONTEXT | PAGE_TABLE_DEPTH(0) |
2566 				RANGE_PROTECTION_FAULT_ENABLE_DEFAULT);
2567 
2568 	si_pcie_gart_tlb_flush(rdev);
2569 	DRM_INFO("PCIE GART of %uM enabled (table at 0x%016llX).\n",
2570 		 (unsigned)(rdev->mc.gtt_size >> 20),
2571 		 (unsigned long long)rdev->gart.table_addr);
2572 	rdev->gart.ready = true;
2573 	return 0;
2574 }
2575 
si_pcie_gart_disable(struct radeon_device * rdev)2576 void si_pcie_gart_disable(struct radeon_device *rdev)
2577 {
2578 	/* Disable all tables */
2579 	WREG32(VM_CONTEXT0_CNTL, 0);
2580 	WREG32(VM_CONTEXT1_CNTL, 0);
2581 	/* Setup TLB control */
2582 	WREG32(MC_VM_MX_L1_TLB_CNTL, SYSTEM_ACCESS_MODE_NOT_IN_SYS |
2583 	       SYSTEM_APERTURE_UNMAPPED_ACCESS_PASS_THRU);
2584 	/* Setup L2 cache */
2585 	WREG32(VM_L2_CNTL, ENABLE_L2_PTE_CACHE_LRU_UPDATE_BY_WRITE |
2586 	       ENABLE_L2_PDE0_CACHE_LRU_UPDATE_BY_WRITE |
2587 	       EFFECTIVE_L2_QUEUE_SIZE(7) |
2588 	       CONTEXT1_IDENTITY_ACCESS_MODE(1));
2589 	WREG32(VM_L2_CNTL2, 0);
2590 	WREG32(VM_L2_CNTL3, L2_CACHE_BIGK_ASSOCIATIVITY |
2591 	       L2_CACHE_BIGK_FRAGMENT_SIZE(0));
2592 	radeon_gart_table_vram_unpin(rdev);
2593 }
2594 
si_pcie_gart_fini(struct radeon_device * rdev)2595 void si_pcie_gart_fini(struct radeon_device *rdev)
2596 {
2597 	si_pcie_gart_disable(rdev);
2598 	radeon_gart_table_vram_free(rdev);
2599 	radeon_gart_fini(rdev);
2600 }
2601 
2602 /* vm parser */
si_vm_reg_valid(u32 reg)2603 static bool si_vm_reg_valid(u32 reg)
2604 {
2605 	/* context regs are fine */
2606 	if (reg >= 0x28000)
2607 		return true;
2608 
2609 	/* check config regs */
2610 	switch (reg) {
2611 	case GRBM_GFX_INDEX:
2612 	case CP_STRMOUT_CNTL:
2613 	case VGT_VTX_VECT_EJECT_REG:
2614 	case VGT_CACHE_INVALIDATION:
2615 	case VGT_ESGS_RING_SIZE:
2616 	case VGT_GSVS_RING_SIZE:
2617 	case VGT_GS_VERTEX_REUSE:
2618 	case VGT_PRIMITIVE_TYPE:
2619 	case VGT_INDEX_TYPE:
2620 	case VGT_NUM_INDICES:
2621 	case VGT_NUM_INSTANCES:
2622 	case VGT_TF_RING_SIZE:
2623 	case VGT_HS_OFFCHIP_PARAM:
2624 	case VGT_TF_MEMORY_BASE:
2625 	case PA_CL_ENHANCE:
2626 	case PA_SU_LINE_STIPPLE_VALUE:
2627 	case PA_SC_LINE_STIPPLE_STATE:
2628 	case PA_SC_ENHANCE:
2629 	case SQC_CACHES:
2630 	case SPI_STATIC_THREAD_MGMT_1:
2631 	case SPI_STATIC_THREAD_MGMT_2:
2632 	case SPI_STATIC_THREAD_MGMT_3:
2633 	case SPI_PS_MAX_WAVE_ID:
2634 	case SPI_CONFIG_CNTL:
2635 	case SPI_CONFIG_CNTL_1:
2636 	case TA_CNTL_AUX:
2637 		return true;
2638 	default:
2639 		DRM_ERROR("Invalid register 0x%x in CS\n", reg);
2640 		return false;
2641 	}
2642 }
2643 
si_vm_packet3_ce_check(struct radeon_device * rdev,u32 * ib,struct radeon_cs_packet * pkt)2644 static int si_vm_packet3_ce_check(struct radeon_device *rdev,
2645 				  u32 *ib, struct radeon_cs_packet *pkt)
2646 {
2647 	switch (pkt->opcode) {
2648 	case PACKET3_NOP:
2649 	case PACKET3_SET_BASE:
2650 	case PACKET3_SET_CE_DE_COUNTERS:
2651 	case PACKET3_LOAD_CONST_RAM:
2652 	case PACKET3_WRITE_CONST_RAM:
2653 	case PACKET3_WRITE_CONST_RAM_OFFSET:
2654 	case PACKET3_DUMP_CONST_RAM:
2655 	case PACKET3_INCREMENT_CE_COUNTER:
2656 	case PACKET3_WAIT_ON_DE_COUNTER:
2657 	case PACKET3_CE_WRITE:
2658 		break;
2659 	default:
2660 		DRM_ERROR("Invalid CE packet3: 0x%x\n", pkt->opcode);
2661 		return -EINVAL;
2662 	}
2663 	return 0;
2664 }
2665 
si_vm_packet3_gfx_check(struct radeon_device * rdev,u32 * ib,struct radeon_cs_packet * pkt)2666 static int si_vm_packet3_gfx_check(struct radeon_device *rdev,
2667 				   u32 *ib, struct radeon_cs_packet *pkt)
2668 {
2669 	u32 idx = pkt->idx + 1;
2670 	u32 idx_value = ib[idx];
2671 	u32 start_reg, end_reg, reg, i;
2672 
2673 	switch (pkt->opcode) {
2674 	case PACKET3_NOP:
2675 	case PACKET3_SET_BASE:
2676 	case PACKET3_CLEAR_STATE:
2677 	case PACKET3_INDEX_BUFFER_SIZE:
2678 	case PACKET3_DISPATCH_DIRECT:
2679 	case PACKET3_DISPATCH_INDIRECT:
2680 	case PACKET3_ALLOC_GDS:
2681 	case PACKET3_WRITE_GDS_RAM:
2682 	case PACKET3_ATOMIC_GDS:
2683 	case PACKET3_ATOMIC:
2684 	case PACKET3_OCCLUSION_QUERY:
2685 	case PACKET3_SET_PREDICATION:
2686 	case PACKET3_COND_EXEC:
2687 	case PACKET3_PRED_EXEC:
2688 	case PACKET3_DRAW_INDIRECT:
2689 	case PACKET3_DRAW_INDEX_INDIRECT:
2690 	case PACKET3_INDEX_BASE:
2691 	case PACKET3_DRAW_INDEX_2:
2692 	case PACKET3_CONTEXT_CONTROL:
2693 	case PACKET3_INDEX_TYPE:
2694 	case PACKET3_DRAW_INDIRECT_MULTI:
2695 	case PACKET3_DRAW_INDEX_AUTO:
2696 	case PACKET3_DRAW_INDEX_IMMD:
2697 	case PACKET3_NUM_INSTANCES:
2698 	case PACKET3_DRAW_INDEX_MULTI_AUTO:
2699 	case PACKET3_STRMOUT_BUFFER_UPDATE:
2700 	case PACKET3_DRAW_INDEX_OFFSET_2:
2701 	case PACKET3_DRAW_INDEX_MULTI_ELEMENT:
2702 	case PACKET3_DRAW_INDEX_INDIRECT_MULTI:
2703 	case PACKET3_MPEG_INDEX:
2704 	case PACKET3_WAIT_REG_MEM:
2705 	case PACKET3_MEM_WRITE:
2706 	case PACKET3_PFP_SYNC_ME:
2707 	case PACKET3_SURFACE_SYNC:
2708 	case PACKET3_EVENT_WRITE:
2709 	case PACKET3_EVENT_WRITE_EOP:
2710 	case PACKET3_EVENT_WRITE_EOS:
2711 	case PACKET3_SET_CONTEXT_REG:
2712 	case PACKET3_SET_CONTEXT_REG_INDIRECT:
2713 	case PACKET3_SET_SH_REG:
2714 	case PACKET3_SET_SH_REG_OFFSET:
2715 	case PACKET3_INCREMENT_DE_COUNTER:
2716 	case PACKET3_WAIT_ON_CE_COUNTER:
2717 	case PACKET3_WAIT_ON_AVAIL_BUFFER:
2718 	case PACKET3_ME_WRITE:
2719 		break;
2720 	case PACKET3_COPY_DATA:
2721 		if ((idx_value & 0xf00) == 0) {
2722 			reg = ib[idx + 3] * 4;
2723 			if (!si_vm_reg_valid(reg))
2724 				return -EINVAL;
2725 		}
2726 		break;
2727 	case PACKET3_WRITE_DATA:
2728 		if ((idx_value & 0xf00) == 0) {
2729 			start_reg = ib[idx + 1] * 4;
2730 			if (idx_value & 0x10000) {
2731 				if (!si_vm_reg_valid(start_reg))
2732 					return -EINVAL;
2733 			} else {
2734 				for (i = 0; i < (pkt->count - 2); i++) {
2735 					reg = start_reg + (4 * i);
2736 					if (!si_vm_reg_valid(reg))
2737 						return -EINVAL;
2738 				}
2739 			}
2740 		}
2741 		break;
2742 	case PACKET3_COND_WRITE:
2743 		if (idx_value & 0x100) {
2744 			reg = ib[idx + 5] * 4;
2745 			if (!si_vm_reg_valid(reg))
2746 				return -EINVAL;
2747 		}
2748 		break;
2749 	case PACKET3_COPY_DW:
2750 		if (idx_value & 0x2) {
2751 			reg = ib[idx + 3] * 4;
2752 			if (!si_vm_reg_valid(reg))
2753 				return -EINVAL;
2754 		}
2755 		break;
2756 	case PACKET3_SET_CONFIG_REG:
2757 		start_reg = (idx_value << 2) + PACKET3_SET_CONFIG_REG_START;
2758 		end_reg = 4 * pkt->count + start_reg - 4;
2759 		if ((start_reg < PACKET3_SET_CONFIG_REG_START) ||
2760 		    (start_reg >= PACKET3_SET_CONFIG_REG_END) ||
2761 		    (end_reg >= PACKET3_SET_CONFIG_REG_END)) {
2762 			DRM_ERROR("bad PACKET3_SET_CONFIG_REG\n");
2763 			return -EINVAL;
2764 		}
2765 		for (i = 0; i < pkt->count; i++) {
2766 			reg = start_reg + (4 * i);
2767 			if (!si_vm_reg_valid(reg))
2768 				return -EINVAL;
2769 		}
2770 		break;
2771 	default:
2772 		DRM_ERROR("Invalid GFX packet3: 0x%x\n", pkt->opcode);
2773 		return -EINVAL;
2774 	}
2775 	return 0;
2776 }
2777 
si_vm_packet3_compute_check(struct radeon_device * rdev,u32 * ib,struct radeon_cs_packet * pkt)2778 static int si_vm_packet3_compute_check(struct radeon_device *rdev,
2779 				       u32 *ib, struct radeon_cs_packet *pkt)
2780 {
2781 	u32 idx = pkt->idx + 1;
2782 	u32 idx_value = ib[idx];
2783 	u32 start_reg, reg, i;
2784 
2785 	switch (pkt->opcode) {
2786 	case PACKET3_NOP:
2787 	case PACKET3_SET_BASE:
2788 	case PACKET3_CLEAR_STATE:
2789 	case PACKET3_DISPATCH_DIRECT:
2790 	case PACKET3_DISPATCH_INDIRECT:
2791 	case PACKET3_ALLOC_GDS:
2792 	case PACKET3_WRITE_GDS_RAM:
2793 	case PACKET3_ATOMIC_GDS:
2794 	case PACKET3_ATOMIC:
2795 	case PACKET3_OCCLUSION_QUERY:
2796 	case PACKET3_SET_PREDICATION:
2797 	case PACKET3_COND_EXEC:
2798 	case PACKET3_PRED_EXEC:
2799 	case PACKET3_CONTEXT_CONTROL:
2800 	case PACKET3_STRMOUT_BUFFER_UPDATE:
2801 	case PACKET3_WAIT_REG_MEM:
2802 	case PACKET3_MEM_WRITE:
2803 	case PACKET3_PFP_SYNC_ME:
2804 	case PACKET3_SURFACE_SYNC:
2805 	case PACKET3_EVENT_WRITE:
2806 	case PACKET3_EVENT_WRITE_EOP:
2807 	case PACKET3_EVENT_WRITE_EOS:
2808 	case PACKET3_SET_CONTEXT_REG:
2809 	case PACKET3_SET_CONTEXT_REG_INDIRECT:
2810 	case PACKET3_SET_SH_REG:
2811 	case PACKET3_SET_SH_REG_OFFSET:
2812 	case PACKET3_INCREMENT_DE_COUNTER:
2813 	case PACKET3_WAIT_ON_CE_COUNTER:
2814 	case PACKET3_WAIT_ON_AVAIL_BUFFER:
2815 	case PACKET3_ME_WRITE:
2816 		break;
2817 	case PACKET3_COPY_DATA:
2818 		if ((idx_value & 0xf00) == 0) {
2819 			reg = ib[idx + 3] * 4;
2820 			if (!si_vm_reg_valid(reg))
2821 				return -EINVAL;
2822 		}
2823 		break;
2824 	case PACKET3_WRITE_DATA:
2825 		if ((idx_value & 0xf00) == 0) {
2826 			start_reg = ib[idx + 1] * 4;
2827 			if (idx_value & 0x10000) {
2828 				if (!si_vm_reg_valid(start_reg))
2829 					return -EINVAL;
2830 			} else {
2831 				for (i = 0; i < (pkt->count - 2); i++) {
2832 					reg = start_reg + (4 * i);
2833 					if (!si_vm_reg_valid(reg))
2834 						return -EINVAL;
2835 				}
2836 			}
2837 		}
2838 		break;
2839 	case PACKET3_COND_WRITE:
2840 		if (idx_value & 0x100) {
2841 			reg = ib[idx + 5] * 4;
2842 			if (!si_vm_reg_valid(reg))
2843 				return -EINVAL;
2844 		}
2845 		break;
2846 	case PACKET3_COPY_DW:
2847 		if (idx_value & 0x2) {
2848 			reg = ib[idx + 3] * 4;
2849 			if (!si_vm_reg_valid(reg))
2850 				return -EINVAL;
2851 		}
2852 		break;
2853 	default:
2854 		DRM_ERROR("Invalid Compute packet3: 0x%x\n", pkt->opcode);
2855 		return -EINVAL;
2856 	}
2857 	return 0;
2858 }
2859 
si_ib_parse(struct radeon_device * rdev,struct radeon_ib * ib)2860 int si_ib_parse(struct radeon_device *rdev, struct radeon_ib *ib)
2861 {
2862 	int ret = 0;
2863 	u32 idx = 0;
2864 	struct radeon_cs_packet pkt;
2865 
2866 	do {
2867 		pkt.idx = idx;
2868 		pkt.type = CP_PACKET_GET_TYPE(ib->ptr[idx]);
2869 		pkt.count = CP_PACKET_GET_COUNT(ib->ptr[idx]);
2870 		pkt.one_reg_wr = 0;
2871 		switch (pkt.type) {
2872 		case PACKET_TYPE0:
2873 			dev_err(rdev->dev, "Packet0 not allowed!\n");
2874 			ret = -EINVAL;
2875 			break;
2876 		case PACKET_TYPE2:
2877 			idx += 1;
2878 			break;
2879 		case PACKET_TYPE3:
2880 			pkt.opcode = CP_PACKET3_GET_OPCODE(ib->ptr[idx]);
2881 			if (ib->is_const_ib)
2882 				ret = si_vm_packet3_ce_check(rdev, ib->ptr, &pkt);
2883 			else {
2884 				switch (ib->fence->ring) {
2885 				case RADEON_RING_TYPE_GFX_INDEX:
2886 					ret = si_vm_packet3_gfx_check(rdev, ib->ptr, &pkt);
2887 					break;
2888 				case CAYMAN_RING_TYPE_CP1_INDEX:
2889 				case CAYMAN_RING_TYPE_CP2_INDEX:
2890 					ret = si_vm_packet3_compute_check(rdev, ib->ptr, &pkt);
2891 					break;
2892 				default:
2893 					dev_err(rdev->dev, "Non-PM4 ring %d !\n", ib->fence->ring);
2894 					ret = -EINVAL;
2895 					break;
2896 				}
2897 			}
2898 			idx += pkt.count + 2;
2899 			break;
2900 		default:
2901 			dev_err(rdev->dev, "Unknown packet type %d !\n", pkt.type);
2902 			ret = -EINVAL;
2903 			break;
2904 		}
2905 		if (ret)
2906 			break;
2907 	} while (idx < ib->length_dw);
2908 
2909 	return ret;
2910 }
2911 
2912 /*
2913  * vm
2914  */
si_vm_init(struct radeon_device * rdev)2915 int si_vm_init(struct radeon_device *rdev)
2916 {
2917 	/* number of VMs */
2918 	rdev->vm_manager.nvm = 16;
2919 	/* base offset of vram pages */
2920 	rdev->vm_manager.vram_base_offset = 0;
2921 
2922 	return 0;
2923 }
2924 
si_vm_fini(struct radeon_device * rdev)2925 void si_vm_fini(struct radeon_device *rdev)
2926 {
2927 }
2928 
si_vm_bind(struct radeon_device * rdev,struct radeon_vm * vm,int id)2929 int si_vm_bind(struct radeon_device *rdev, struct radeon_vm *vm, int id)
2930 {
2931 	if (id < 8)
2932 		WREG32(VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (id << 2), vm->pt_gpu_addr >> 12);
2933 	else
2934 		WREG32(VM_CONTEXT8_PAGE_TABLE_BASE_ADDR + ((id - 8) << 2),
2935 		       vm->pt_gpu_addr >> 12);
2936 	/* flush hdp cache */
2937 	WREG32(HDP_MEM_COHERENCY_FLUSH_CNTL, 0x1);
2938 	/* bits 0-15 are the VM contexts0-15 */
2939 	WREG32(VM_INVALIDATE_REQUEST, 1 << id);
2940 	return 0;
2941 }
2942 
si_vm_unbind(struct radeon_device * rdev,struct radeon_vm * vm)2943 void si_vm_unbind(struct radeon_device *rdev, struct radeon_vm *vm)
2944 {
2945 	if (vm->id < 8)
2946 		WREG32(VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (vm->id << 2), 0);
2947 	else
2948 		WREG32(VM_CONTEXT8_PAGE_TABLE_BASE_ADDR + ((vm->id - 8) << 2), 0);
2949 	/* flush hdp cache */
2950 	WREG32(HDP_MEM_COHERENCY_FLUSH_CNTL, 0x1);
2951 	/* bits 0-15 are the VM contexts0-15 */
2952 	WREG32(VM_INVALIDATE_REQUEST, 1 << vm->id);
2953 }
2954 
si_vm_tlb_flush(struct radeon_device * rdev,struct radeon_vm * vm)2955 void si_vm_tlb_flush(struct radeon_device *rdev, struct radeon_vm *vm)
2956 {
2957 	if (vm->id == -1)
2958 		return;
2959 
2960 	/* flush hdp cache */
2961 	WREG32(HDP_MEM_COHERENCY_FLUSH_CNTL, 0x1);
2962 	/* bits 0-15 are the VM contexts0-15 */
2963 	WREG32(VM_INVALIDATE_REQUEST, 1 << vm->id);
2964 }
2965 
2966 /*
2967  * RLC
2968  */
si_rlc_fini(struct radeon_device * rdev)2969 void si_rlc_fini(struct radeon_device *rdev)
2970 {
2971 	int r;
2972 
2973 	/* save restore block */
2974 	if (rdev->rlc.save_restore_obj) {
2975 		r = radeon_bo_reserve(rdev->rlc.save_restore_obj, false);
2976 		if (unlikely(r != 0))
2977 			dev_warn(rdev->dev, "(%d) reserve RLC sr bo failed\n", r);
2978 		radeon_bo_unpin(rdev->rlc.save_restore_obj);
2979 		radeon_bo_unreserve(rdev->rlc.save_restore_obj);
2980 
2981 		radeon_bo_unref(&rdev->rlc.save_restore_obj);
2982 		rdev->rlc.save_restore_obj = NULL;
2983 	}
2984 
2985 	/* clear state block */
2986 	if (rdev->rlc.clear_state_obj) {
2987 		r = radeon_bo_reserve(rdev->rlc.clear_state_obj, false);
2988 		if (unlikely(r != 0))
2989 			dev_warn(rdev->dev, "(%d) reserve RLC c bo failed\n", r);
2990 		radeon_bo_unpin(rdev->rlc.clear_state_obj);
2991 		radeon_bo_unreserve(rdev->rlc.clear_state_obj);
2992 
2993 		radeon_bo_unref(&rdev->rlc.clear_state_obj);
2994 		rdev->rlc.clear_state_obj = NULL;
2995 	}
2996 }
2997 
si_rlc_init(struct radeon_device * rdev)2998 int si_rlc_init(struct radeon_device *rdev)
2999 {
3000 	int r;
3001 
3002 	/* save restore block */
3003 	if (rdev->rlc.save_restore_obj == NULL) {
3004 		r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
3005 				RADEON_GEM_DOMAIN_VRAM, &rdev->rlc.save_restore_obj);
3006 		if (r) {
3007 			dev_warn(rdev->dev, "(%d) create RLC sr bo failed\n", r);
3008 			return r;
3009 		}
3010 	}
3011 
3012 	r = radeon_bo_reserve(rdev->rlc.save_restore_obj, false);
3013 	if (unlikely(r != 0)) {
3014 		si_rlc_fini(rdev);
3015 		return r;
3016 	}
3017 	r = radeon_bo_pin(rdev->rlc.save_restore_obj, RADEON_GEM_DOMAIN_VRAM,
3018 			  &rdev->rlc.save_restore_gpu_addr);
3019 	radeon_bo_unreserve(rdev->rlc.save_restore_obj);
3020 	if (r) {
3021 		dev_warn(rdev->dev, "(%d) pin RLC sr bo failed\n", r);
3022 		si_rlc_fini(rdev);
3023 		return r;
3024 	}
3025 
3026 	/* clear state block */
3027 	if (rdev->rlc.clear_state_obj == NULL) {
3028 		r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
3029 				RADEON_GEM_DOMAIN_VRAM, &rdev->rlc.clear_state_obj);
3030 		if (r) {
3031 			dev_warn(rdev->dev, "(%d) create RLC c bo failed\n", r);
3032 			si_rlc_fini(rdev);
3033 			return r;
3034 		}
3035 	}
3036 	r = radeon_bo_reserve(rdev->rlc.clear_state_obj, false);
3037 	if (unlikely(r != 0)) {
3038 		si_rlc_fini(rdev);
3039 		return r;
3040 	}
3041 	r = radeon_bo_pin(rdev->rlc.clear_state_obj, RADEON_GEM_DOMAIN_VRAM,
3042 			  &rdev->rlc.clear_state_gpu_addr);
3043 	radeon_bo_unreserve(rdev->rlc.clear_state_obj);
3044 	if (r) {
3045 		dev_warn(rdev->dev, "(%d) pin RLC c bo failed\n", r);
3046 		si_rlc_fini(rdev);
3047 		return r;
3048 	}
3049 
3050 	return 0;
3051 }
3052 
si_rlc_stop(struct radeon_device * rdev)3053 static void si_rlc_stop(struct radeon_device *rdev)
3054 {
3055 	WREG32(RLC_CNTL, 0);
3056 }
3057 
si_rlc_start(struct radeon_device * rdev)3058 static void si_rlc_start(struct radeon_device *rdev)
3059 {
3060 	WREG32(RLC_CNTL, RLC_ENABLE);
3061 }
3062 
si_rlc_resume(struct radeon_device * rdev)3063 static int si_rlc_resume(struct radeon_device *rdev)
3064 {
3065 	u32 i;
3066 	const __be32 *fw_data;
3067 
3068 	if (!rdev->rlc_fw)
3069 		return -EINVAL;
3070 
3071 	si_rlc_stop(rdev);
3072 
3073 	WREG32(RLC_RL_BASE, 0);
3074 	WREG32(RLC_RL_SIZE, 0);
3075 	WREG32(RLC_LB_CNTL, 0);
3076 	WREG32(RLC_LB_CNTR_MAX, 0xffffffff);
3077 	WREG32(RLC_LB_CNTR_INIT, 0);
3078 
3079 	WREG32(RLC_SAVE_AND_RESTORE_BASE, rdev->rlc.save_restore_gpu_addr >> 8);
3080 	WREG32(RLC_CLEAR_STATE_RESTORE_BASE, rdev->rlc.clear_state_gpu_addr >> 8);
3081 
3082 	WREG32(RLC_MC_CNTL, 0);
3083 	WREG32(RLC_UCODE_CNTL, 0);
3084 
3085 	fw_data = (const __be32 *)rdev->rlc_fw->data;
3086 	for (i = 0; i < SI_RLC_UCODE_SIZE; i++) {
3087 		WREG32(RLC_UCODE_ADDR, i);
3088 		WREG32(RLC_UCODE_DATA, be32_to_cpup(fw_data++));
3089 	}
3090 	WREG32(RLC_UCODE_ADDR, 0);
3091 
3092 	si_rlc_start(rdev);
3093 
3094 	return 0;
3095 }
3096 
si_enable_interrupts(struct radeon_device * rdev)3097 static void si_enable_interrupts(struct radeon_device *rdev)
3098 {
3099 	u32 ih_cntl = RREG32(IH_CNTL);
3100 	u32 ih_rb_cntl = RREG32(IH_RB_CNTL);
3101 
3102 	ih_cntl |= ENABLE_INTR;
3103 	ih_rb_cntl |= IH_RB_ENABLE;
3104 	WREG32(IH_CNTL, ih_cntl);
3105 	WREG32(IH_RB_CNTL, ih_rb_cntl);
3106 	rdev->ih.enabled = true;
3107 }
3108 
si_disable_interrupts(struct radeon_device * rdev)3109 static void si_disable_interrupts(struct radeon_device *rdev)
3110 {
3111 	u32 ih_rb_cntl = RREG32(IH_RB_CNTL);
3112 	u32 ih_cntl = RREG32(IH_CNTL);
3113 
3114 	ih_rb_cntl &= ~IH_RB_ENABLE;
3115 	ih_cntl &= ~ENABLE_INTR;
3116 	WREG32(IH_RB_CNTL, ih_rb_cntl);
3117 	WREG32(IH_CNTL, ih_cntl);
3118 	/* set rptr, wptr to 0 */
3119 	WREG32(IH_RB_RPTR, 0);
3120 	WREG32(IH_RB_WPTR, 0);
3121 	rdev->ih.enabled = false;
3122 	rdev->ih.wptr = 0;
3123 	rdev->ih.rptr = 0;
3124 }
3125 
si_disable_interrupt_state(struct radeon_device * rdev)3126 static void si_disable_interrupt_state(struct radeon_device *rdev)
3127 {
3128 	u32 tmp;
3129 
3130 	WREG32(CP_INT_CNTL_RING0, CNTX_BUSY_INT_ENABLE | CNTX_EMPTY_INT_ENABLE);
3131 	WREG32(CP_INT_CNTL_RING1, 0);
3132 	WREG32(CP_INT_CNTL_RING2, 0);
3133 	WREG32(GRBM_INT_CNTL, 0);
3134 	WREG32(INT_MASK + EVERGREEN_CRTC0_REGISTER_OFFSET, 0);
3135 	WREG32(INT_MASK + EVERGREEN_CRTC1_REGISTER_OFFSET, 0);
3136 	if (rdev->num_crtc >= 4) {
3137 		WREG32(INT_MASK + EVERGREEN_CRTC2_REGISTER_OFFSET, 0);
3138 		WREG32(INT_MASK + EVERGREEN_CRTC3_REGISTER_OFFSET, 0);
3139 	}
3140 	if (rdev->num_crtc >= 6) {
3141 		WREG32(INT_MASK + EVERGREEN_CRTC4_REGISTER_OFFSET, 0);
3142 		WREG32(INT_MASK + EVERGREEN_CRTC5_REGISTER_OFFSET, 0);
3143 	}
3144 
3145 	WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET, 0);
3146 	WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET, 0);
3147 	if (rdev->num_crtc >= 4) {
3148 		WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET, 0);
3149 		WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET, 0);
3150 	}
3151 	if (rdev->num_crtc >= 6) {
3152 		WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET, 0);
3153 		WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET, 0);
3154 	}
3155 
3156 	WREG32(DACA_AUTODETECT_INT_CONTROL, 0);
3157 
3158 	tmp = RREG32(DC_HPD1_INT_CONTROL) & DC_HPDx_INT_POLARITY;
3159 	WREG32(DC_HPD1_INT_CONTROL, tmp);
3160 	tmp = RREG32(DC_HPD2_INT_CONTROL) & DC_HPDx_INT_POLARITY;
3161 	WREG32(DC_HPD2_INT_CONTROL, tmp);
3162 	tmp = RREG32(DC_HPD3_INT_CONTROL) & DC_HPDx_INT_POLARITY;
3163 	WREG32(DC_HPD3_INT_CONTROL, tmp);
3164 	tmp = RREG32(DC_HPD4_INT_CONTROL) & DC_HPDx_INT_POLARITY;
3165 	WREG32(DC_HPD4_INT_CONTROL, tmp);
3166 	tmp = RREG32(DC_HPD5_INT_CONTROL) & DC_HPDx_INT_POLARITY;
3167 	WREG32(DC_HPD5_INT_CONTROL, tmp);
3168 	tmp = RREG32(DC_HPD6_INT_CONTROL) & DC_HPDx_INT_POLARITY;
3169 	WREG32(DC_HPD6_INT_CONTROL, tmp);
3170 
3171 }
3172 
si_irq_init(struct radeon_device * rdev)3173 static int si_irq_init(struct radeon_device *rdev)
3174 {
3175 	int ret = 0;
3176 	int rb_bufsz;
3177 	u32 interrupt_cntl, ih_cntl, ih_rb_cntl;
3178 
3179 	/* allocate ring */
3180 	ret = r600_ih_ring_alloc(rdev);
3181 	if (ret)
3182 		return ret;
3183 
3184 	/* disable irqs */
3185 	si_disable_interrupts(rdev);
3186 
3187 	/* init rlc */
3188 	ret = si_rlc_resume(rdev);
3189 	if (ret) {
3190 		r600_ih_ring_fini(rdev);
3191 		return ret;
3192 	}
3193 
3194 	/* setup interrupt control */
3195 	/* set dummy read address to ring address */
3196 	WREG32(INTERRUPT_CNTL2, rdev->ih.gpu_addr >> 8);
3197 	interrupt_cntl = RREG32(INTERRUPT_CNTL);
3198 	/* IH_DUMMY_RD_OVERRIDE=0 - dummy read disabled with msi, enabled without msi
3199 	 * IH_DUMMY_RD_OVERRIDE=1 - dummy read controlled by IH_DUMMY_RD_EN
3200 	 */
3201 	interrupt_cntl &= ~IH_DUMMY_RD_OVERRIDE;
3202 	/* IH_REQ_NONSNOOP_EN=1 if ring is in non-cacheable memory, e.g., vram */
3203 	interrupt_cntl &= ~IH_REQ_NONSNOOP_EN;
3204 	WREG32(INTERRUPT_CNTL, interrupt_cntl);
3205 
3206 	WREG32(IH_RB_BASE, rdev->ih.gpu_addr >> 8);
3207 	rb_bufsz = drm_order(rdev->ih.ring_size / 4);
3208 
3209 	ih_rb_cntl = (IH_WPTR_OVERFLOW_ENABLE |
3210 		      IH_WPTR_OVERFLOW_CLEAR |
3211 		      (rb_bufsz << 1));
3212 
3213 	if (rdev->wb.enabled)
3214 		ih_rb_cntl |= IH_WPTR_WRITEBACK_ENABLE;
3215 
3216 	/* set the writeback address whether it's enabled or not */
3217 	WREG32(IH_RB_WPTR_ADDR_LO, (rdev->wb.gpu_addr + R600_WB_IH_WPTR_OFFSET) & 0xFFFFFFFC);
3218 	WREG32(IH_RB_WPTR_ADDR_HI, upper_32_bits(rdev->wb.gpu_addr + R600_WB_IH_WPTR_OFFSET) & 0xFF);
3219 
3220 	WREG32(IH_RB_CNTL, ih_rb_cntl);
3221 
3222 	/* set rptr, wptr to 0 */
3223 	WREG32(IH_RB_RPTR, 0);
3224 	WREG32(IH_RB_WPTR, 0);
3225 
3226 	/* Default settings for IH_CNTL (disabled at first) */
3227 	ih_cntl = MC_WRREQ_CREDIT(0x10) | MC_WR_CLEAN_CNT(0x10) | MC_VMID(0);
3228 	/* RPTR_REARM only works if msi's are enabled */
3229 	if (rdev->msi_enabled)
3230 		ih_cntl |= RPTR_REARM;
3231 	WREG32(IH_CNTL, ih_cntl);
3232 
3233 	/* force the active interrupt state to all disabled */
3234 	si_disable_interrupt_state(rdev);
3235 
3236 	/* enable irqs */
3237 	si_enable_interrupts(rdev);
3238 
3239 	return ret;
3240 }
3241 
si_irq_set(struct radeon_device * rdev)3242 int si_irq_set(struct radeon_device *rdev)
3243 {
3244 	u32 cp_int_cntl = CNTX_BUSY_INT_ENABLE | CNTX_EMPTY_INT_ENABLE;
3245 	u32 cp_int_cntl1 = 0, cp_int_cntl2 = 0;
3246 	u32 crtc1 = 0, crtc2 = 0, crtc3 = 0, crtc4 = 0, crtc5 = 0, crtc6 = 0;
3247 	u32 hpd1, hpd2, hpd3, hpd4, hpd5, hpd6;
3248 	u32 grbm_int_cntl = 0;
3249 	u32 grph1 = 0, grph2 = 0, grph3 = 0, grph4 = 0, grph5 = 0, grph6 = 0;
3250 
3251 	if (!rdev->irq.installed) {
3252 		WARN(1, "Can't enable IRQ/MSI because no handler is installed\n");
3253 		return -EINVAL;
3254 	}
3255 	/* don't enable anything if the ih is disabled */
3256 	if (!rdev->ih.enabled) {
3257 		si_disable_interrupts(rdev);
3258 		/* force the active interrupt state to all disabled */
3259 		si_disable_interrupt_state(rdev);
3260 		return 0;
3261 	}
3262 
3263 	hpd1 = RREG32(DC_HPD1_INT_CONTROL) & ~DC_HPDx_INT_EN;
3264 	hpd2 = RREG32(DC_HPD2_INT_CONTROL) & ~DC_HPDx_INT_EN;
3265 	hpd3 = RREG32(DC_HPD3_INT_CONTROL) & ~DC_HPDx_INT_EN;
3266 	hpd4 = RREG32(DC_HPD4_INT_CONTROL) & ~DC_HPDx_INT_EN;
3267 	hpd5 = RREG32(DC_HPD5_INT_CONTROL) & ~DC_HPDx_INT_EN;
3268 	hpd6 = RREG32(DC_HPD6_INT_CONTROL) & ~DC_HPDx_INT_EN;
3269 
3270 	/* enable CP interrupts on all rings */
3271 	if (rdev->irq.sw_int[RADEON_RING_TYPE_GFX_INDEX]) {
3272 		DRM_DEBUG("si_irq_set: sw int gfx\n");
3273 		cp_int_cntl |= TIME_STAMP_INT_ENABLE;
3274 	}
3275 	if (rdev->irq.sw_int[CAYMAN_RING_TYPE_CP1_INDEX]) {
3276 		DRM_DEBUG("si_irq_set: sw int cp1\n");
3277 		cp_int_cntl1 |= TIME_STAMP_INT_ENABLE;
3278 	}
3279 	if (rdev->irq.sw_int[CAYMAN_RING_TYPE_CP2_INDEX]) {
3280 		DRM_DEBUG("si_irq_set: sw int cp2\n");
3281 		cp_int_cntl2 |= TIME_STAMP_INT_ENABLE;
3282 	}
3283 	if (rdev->irq.crtc_vblank_int[0] ||
3284 	    rdev->irq.pflip[0]) {
3285 		DRM_DEBUG("si_irq_set: vblank 0\n");
3286 		crtc1 |= VBLANK_INT_MASK;
3287 	}
3288 	if (rdev->irq.crtc_vblank_int[1] ||
3289 	    rdev->irq.pflip[1]) {
3290 		DRM_DEBUG("si_irq_set: vblank 1\n");
3291 		crtc2 |= VBLANK_INT_MASK;
3292 	}
3293 	if (rdev->irq.crtc_vblank_int[2] ||
3294 	    rdev->irq.pflip[2]) {
3295 		DRM_DEBUG("si_irq_set: vblank 2\n");
3296 		crtc3 |= VBLANK_INT_MASK;
3297 	}
3298 	if (rdev->irq.crtc_vblank_int[3] ||
3299 	    rdev->irq.pflip[3]) {
3300 		DRM_DEBUG("si_irq_set: vblank 3\n");
3301 		crtc4 |= VBLANK_INT_MASK;
3302 	}
3303 	if (rdev->irq.crtc_vblank_int[4] ||
3304 	    rdev->irq.pflip[4]) {
3305 		DRM_DEBUG("si_irq_set: vblank 4\n");
3306 		crtc5 |= VBLANK_INT_MASK;
3307 	}
3308 	if (rdev->irq.crtc_vblank_int[5] ||
3309 	    rdev->irq.pflip[5]) {
3310 		DRM_DEBUG("si_irq_set: vblank 5\n");
3311 		crtc6 |= VBLANK_INT_MASK;
3312 	}
3313 	if (rdev->irq.hpd[0]) {
3314 		DRM_DEBUG("si_irq_set: hpd 1\n");
3315 		hpd1 |= DC_HPDx_INT_EN;
3316 	}
3317 	if (rdev->irq.hpd[1]) {
3318 		DRM_DEBUG("si_irq_set: hpd 2\n");
3319 		hpd2 |= DC_HPDx_INT_EN;
3320 	}
3321 	if (rdev->irq.hpd[2]) {
3322 		DRM_DEBUG("si_irq_set: hpd 3\n");
3323 		hpd3 |= DC_HPDx_INT_EN;
3324 	}
3325 	if (rdev->irq.hpd[3]) {
3326 		DRM_DEBUG("si_irq_set: hpd 4\n");
3327 		hpd4 |= DC_HPDx_INT_EN;
3328 	}
3329 	if (rdev->irq.hpd[4]) {
3330 		DRM_DEBUG("si_irq_set: hpd 5\n");
3331 		hpd5 |= DC_HPDx_INT_EN;
3332 	}
3333 	if (rdev->irq.hpd[5]) {
3334 		DRM_DEBUG("si_irq_set: hpd 6\n");
3335 		hpd6 |= DC_HPDx_INT_EN;
3336 	}
3337 	if (rdev->irq.gui_idle) {
3338 		DRM_DEBUG("gui idle\n");
3339 		grbm_int_cntl |= GUI_IDLE_INT_ENABLE;
3340 	}
3341 
3342 	WREG32(CP_INT_CNTL_RING0, cp_int_cntl);
3343 	WREG32(CP_INT_CNTL_RING1, cp_int_cntl1);
3344 	WREG32(CP_INT_CNTL_RING2, cp_int_cntl2);
3345 
3346 	WREG32(GRBM_INT_CNTL, grbm_int_cntl);
3347 
3348 	WREG32(INT_MASK + EVERGREEN_CRTC0_REGISTER_OFFSET, crtc1);
3349 	WREG32(INT_MASK + EVERGREEN_CRTC1_REGISTER_OFFSET, crtc2);
3350 	if (rdev->num_crtc >= 4) {
3351 		WREG32(INT_MASK + EVERGREEN_CRTC2_REGISTER_OFFSET, crtc3);
3352 		WREG32(INT_MASK + EVERGREEN_CRTC3_REGISTER_OFFSET, crtc4);
3353 	}
3354 	if (rdev->num_crtc >= 6) {
3355 		WREG32(INT_MASK + EVERGREEN_CRTC4_REGISTER_OFFSET, crtc5);
3356 		WREG32(INT_MASK + EVERGREEN_CRTC5_REGISTER_OFFSET, crtc6);
3357 	}
3358 
3359 	WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET, grph1);
3360 	WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET, grph2);
3361 	if (rdev->num_crtc >= 4) {
3362 		WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET, grph3);
3363 		WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET, grph4);
3364 	}
3365 	if (rdev->num_crtc >= 6) {
3366 		WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET, grph5);
3367 		WREG32(GRPH_INT_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET, grph6);
3368 	}
3369 
3370 	WREG32(DC_HPD1_INT_CONTROL, hpd1);
3371 	WREG32(DC_HPD2_INT_CONTROL, hpd2);
3372 	WREG32(DC_HPD3_INT_CONTROL, hpd3);
3373 	WREG32(DC_HPD4_INT_CONTROL, hpd4);
3374 	WREG32(DC_HPD5_INT_CONTROL, hpd5);
3375 	WREG32(DC_HPD6_INT_CONTROL, hpd6);
3376 
3377 	return 0;
3378 }
3379 
si_irq_ack(struct radeon_device * rdev)3380 static inline void si_irq_ack(struct radeon_device *rdev)
3381 {
3382 	u32 tmp;
3383 
3384 	rdev->irq.stat_regs.evergreen.disp_int = RREG32(DISP_INTERRUPT_STATUS);
3385 	rdev->irq.stat_regs.evergreen.disp_int_cont = RREG32(DISP_INTERRUPT_STATUS_CONTINUE);
3386 	rdev->irq.stat_regs.evergreen.disp_int_cont2 = RREG32(DISP_INTERRUPT_STATUS_CONTINUE2);
3387 	rdev->irq.stat_regs.evergreen.disp_int_cont3 = RREG32(DISP_INTERRUPT_STATUS_CONTINUE3);
3388 	rdev->irq.stat_regs.evergreen.disp_int_cont4 = RREG32(DISP_INTERRUPT_STATUS_CONTINUE4);
3389 	rdev->irq.stat_regs.evergreen.disp_int_cont5 = RREG32(DISP_INTERRUPT_STATUS_CONTINUE5);
3390 	rdev->irq.stat_regs.evergreen.d1grph_int = RREG32(GRPH_INT_STATUS + EVERGREEN_CRTC0_REGISTER_OFFSET);
3391 	rdev->irq.stat_regs.evergreen.d2grph_int = RREG32(GRPH_INT_STATUS + EVERGREEN_CRTC1_REGISTER_OFFSET);
3392 	if (rdev->num_crtc >= 4) {
3393 		rdev->irq.stat_regs.evergreen.d3grph_int = RREG32(GRPH_INT_STATUS + EVERGREEN_CRTC2_REGISTER_OFFSET);
3394 		rdev->irq.stat_regs.evergreen.d4grph_int = RREG32(GRPH_INT_STATUS + EVERGREEN_CRTC3_REGISTER_OFFSET);
3395 	}
3396 	if (rdev->num_crtc >= 6) {
3397 		rdev->irq.stat_regs.evergreen.d5grph_int = RREG32(GRPH_INT_STATUS + EVERGREEN_CRTC4_REGISTER_OFFSET);
3398 		rdev->irq.stat_regs.evergreen.d6grph_int = RREG32(GRPH_INT_STATUS + EVERGREEN_CRTC5_REGISTER_OFFSET);
3399 	}
3400 
3401 	if (rdev->irq.stat_regs.evergreen.d1grph_int & GRPH_PFLIP_INT_OCCURRED)
3402 		WREG32(GRPH_INT_STATUS + EVERGREEN_CRTC0_REGISTER_OFFSET, GRPH_PFLIP_INT_CLEAR);
3403 	if (rdev->irq.stat_regs.evergreen.d2grph_int & GRPH_PFLIP_INT_OCCURRED)
3404 		WREG32(GRPH_INT_STATUS + EVERGREEN_CRTC1_REGISTER_OFFSET, GRPH_PFLIP_INT_CLEAR);
3405 	if (rdev->irq.stat_regs.evergreen.disp_int & LB_D1_VBLANK_INTERRUPT)
3406 		WREG32(VBLANK_STATUS + EVERGREEN_CRTC0_REGISTER_OFFSET, VBLANK_ACK);
3407 	if (rdev->irq.stat_regs.evergreen.disp_int & LB_D1_VLINE_INTERRUPT)
3408 		WREG32(VLINE_STATUS + EVERGREEN_CRTC0_REGISTER_OFFSET, VLINE_ACK);
3409 	if (rdev->irq.stat_regs.evergreen.disp_int_cont & LB_D2_VBLANK_INTERRUPT)
3410 		WREG32(VBLANK_STATUS + EVERGREEN_CRTC1_REGISTER_OFFSET, VBLANK_ACK);
3411 	if (rdev->irq.stat_regs.evergreen.disp_int_cont & LB_D2_VLINE_INTERRUPT)
3412 		WREG32(VLINE_STATUS + EVERGREEN_CRTC1_REGISTER_OFFSET, VLINE_ACK);
3413 
3414 	if (rdev->num_crtc >= 4) {
3415 		if (rdev->irq.stat_regs.evergreen.d3grph_int & GRPH_PFLIP_INT_OCCURRED)
3416 			WREG32(GRPH_INT_STATUS + EVERGREEN_CRTC2_REGISTER_OFFSET, GRPH_PFLIP_INT_CLEAR);
3417 		if (rdev->irq.stat_regs.evergreen.d4grph_int & GRPH_PFLIP_INT_OCCURRED)
3418 			WREG32(GRPH_INT_STATUS + EVERGREEN_CRTC3_REGISTER_OFFSET, GRPH_PFLIP_INT_CLEAR);
3419 		if (rdev->irq.stat_regs.evergreen.disp_int_cont2 & LB_D3_VBLANK_INTERRUPT)
3420 			WREG32(VBLANK_STATUS + EVERGREEN_CRTC2_REGISTER_OFFSET, VBLANK_ACK);
3421 		if (rdev->irq.stat_regs.evergreen.disp_int_cont2 & LB_D3_VLINE_INTERRUPT)
3422 			WREG32(VLINE_STATUS + EVERGREEN_CRTC2_REGISTER_OFFSET, VLINE_ACK);
3423 		if (rdev->irq.stat_regs.evergreen.disp_int_cont3 & LB_D4_VBLANK_INTERRUPT)
3424 			WREG32(VBLANK_STATUS + EVERGREEN_CRTC3_REGISTER_OFFSET, VBLANK_ACK);
3425 		if (rdev->irq.stat_regs.evergreen.disp_int_cont3 & LB_D4_VLINE_INTERRUPT)
3426 			WREG32(VLINE_STATUS + EVERGREEN_CRTC3_REGISTER_OFFSET, VLINE_ACK);
3427 	}
3428 
3429 	if (rdev->num_crtc >= 6) {
3430 		if (rdev->irq.stat_regs.evergreen.d5grph_int & GRPH_PFLIP_INT_OCCURRED)
3431 			WREG32(GRPH_INT_STATUS + EVERGREEN_CRTC4_REGISTER_OFFSET, GRPH_PFLIP_INT_CLEAR);
3432 		if (rdev->irq.stat_regs.evergreen.d6grph_int & GRPH_PFLIP_INT_OCCURRED)
3433 			WREG32(GRPH_INT_STATUS + EVERGREEN_CRTC5_REGISTER_OFFSET, GRPH_PFLIP_INT_CLEAR);
3434 		if (rdev->irq.stat_regs.evergreen.disp_int_cont4 & LB_D5_VBLANK_INTERRUPT)
3435 			WREG32(VBLANK_STATUS + EVERGREEN_CRTC4_REGISTER_OFFSET, VBLANK_ACK);
3436 		if (rdev->irq.stat_regs.evergreen.disp_int_cont4 & LB_D5_VLINE_INTERRUPT)
3437 			WREG32(VLINE_STATUS + EVERGREEN_CRTC4_REGISTER_OFFSET, VLINE_ACK);
3438 		if (rdev->irq.stat_regs.evergreen.disp_int_cont5 & LB_D6_VBLANK_INTERRUPT)
3439 			WREG32(VBLANK_STATUS + EVERGREEN_CRTC5_REGISTER_OFFSET, VBLANK_ACK);
3440 		if (rdev->irq.stat_regs.evergreen.disp_int_cont5 & LB_D6_VLINE_INTERRUPT)
3441 			WREG32(VLINE_STATUS + EVERGREEN_CRTC5_REGISTER_OFFSET, VLINE_ACK);
3442 	}
3443 
3444 	if (rdev->irq.stat_regs.evergreen.disp_int & DC_HPD1_INTERRUPT) {
3445 		tmp = RREG32(DC_HPD1_INT_CONTROL);
3446 		tmp |= DC_HPDx_INT_ACK;
3447 		WREG32(DC_HPD1_INT_CONTROL, tmp);
3448 	}
3449 	if (rdev->irq.stat_regs.evergreen.disp_int_cont & DC_HPD2_INTERRUPT) {
3450 		tmp = RREG32(DC_HPD2_INT_CONTROL);
3451 		tmp |= DC_HPDx_INT_ACK;
3452 		WREG32(DC_HPD2_INT_CONTROL, tmp);
3453 	}
3454 	if (rdev->irq.stat_regs.evergreen.disp_int_cont2 & DC_HPD3_INTERRUPT) {
3455 		tmp = RREG32(DC_HPD3_INT_CONTROL);
3456 		tmp |= DC_HPDx_INT_ACK;
3457 		WREG32(DC_HPD3_INT_CONTROL, tmp);
3458 	}
3459 	if (rdev->irq.stat_regs.evergreen.disp_int_cont3 & DC_HPD4_INTERRUPT) {
3460 		tmp = RREG32(DC_HPD4_INT_CONTROL);
3461 		tmp |= DC_HPDx_INT_ACK;
3462 		WREG32(DC_HPD4_INT_CONTROL, tmp);
3463 	}
3464 	if (rdev->irq.stat_regs.evergreen.disp_int_cont4 & DC_HPD5_INTERRUPT) {
3465 		tmp = RREG32(DC_HPD5_INT_CONTROL);
3466 		tmp |= DC_HPDx_INT_ACK;
3467 		WREG32(DC_HPD5_INT_CONTROL, tmp);
3468 	}
3469 	if (rdev->irq.stat_regs.evergreen.disp_int_cont5 & DC_HPD6_INTERRUPT) {
3470 		tmp = RREG32(DC_HPD5_INT_CONTROL);
3471 		tmp |= DC_HPDx_INT_ACK;
3472 		WREG32(DC_HPD6_INT_CONTROL, tmp);
3473 	}
3474 }
3475 
si_irq_disable(struct radeon_device * rdev)3476 static void si_irq_disable(struct radeon_device *rdev)
3477 {
3478 	si_disable_interrupts(rdev);
3479 	/* Wait and acknowledge irq */
3480 	mdelay(1);
3481 	si_irq_ack(rdev);
3482 	si_disable_interrupt_state(rdev);
3483 }
3484 
si_irq_suspend(struct radeon_device * rdev)3485 static void si_irq_suspend(struct radeon_device *rdev)
3486 {
3487 	si_irq_disable(rdev);
3488 	si_rlc_stop(rdev);
3489 }
3490 
si_irq_fini(struct radeon_device * rdev)3491 static void si_irq_fini(struct radeon_device *rdev)
3492 {
3493 	si_irq_suspend(rdev);
3494 	r600_ih_ring_fini(rdev);
3495 }
3496 
si_get_ih_wptr(struct radeon_device * rdev)3497 static inline u32 si_get_ih_wptr(struct radeon_device *rdev)
3498 {
3499 	u32 wptr, tmp;
3500 
3501 	if (rdev->wb.enabled)
3502 		wptr = le32_to_cpu(rdev->wb.wb[R600_WB_IH_WPTR_OFFSET/4]);
3503 	else
3504 		wptr = RREG32(IH_RB_WPTR);
3505 
3506 	if (wptr & RB_OVERFLOW) {
3507 		/* When a ring buffer overflow happen start parsing interrupt
3508 		 * from the last not overwritten vector (wptr + 16). Hopefully
3509 		 * this should allow us to catchup.
3510 		 */
3511 		dev_warn(rdev->dev, "IH ring buffer overflow (0x%08X, %d, %d)\n",
3512 			wptr, rdev->ih.rptr, (wptr + 16) + rdev->ih.ptr_mask);
3513 		rdev->ih.rptr = (wptr + 16) & rdev->ih.ptr_mask;
3514 		tmp = RREG32(IH_RB_CNTL);
3515 		tmp |= IH_WPTR_OVERFLOW_CLEAR;
3516 		WREG32(IH_RB_CNTL, tmp);
3517 	}
3518 	return (wptr & rdev->ih.ptr_mask);
3519 }
3520 
3521 /*        SI IV Ring
3522  * Each IV ring entry is 128 bits:
3523  * [7:0]    - interrupt source id
3524  * [31:8]   - reserved
3525  * [59:32]  - interrupt source data
3526  * [63:60]  - reserved
3527  * [71:64]  - RINGID
3528  * [79:72]  - VMID
3529  * [127:80] - reserved
3530  */
si_irq_process(struct radeon_device * rdev)3531 int si_irq_process(struct radeon_device *rdev)
3532 {
3533 	u32 wptr;
3534 	u32 rptr;
3535 	u32 src_id, src_data, ring_id;
3536 	u32 ring_index;
3537 	unsigned long flags;
3538 	bool queue_hotplug = false;
3539 
3540 	if (!rdev->ih.enabled || rdev->shutdown)
3541 		return IRQ_NONE;
3542 
3543 	wptr = si_get_ih_wptr(rdev);
3544 	rptr = rdev->ih.rptr;
3545 	DRM_DEBUG("si_irq_process start: rptr %d, wptr %d\n", rptr, wptr);
3546 
3547 	spin_lock_irqsave(&rdev->ih.lock, flags);
3548 	if (rptr == wptr) {
3549 		spin_unlock_irqrestore(&rdev->ih.lock, flags);
3550 		return IRQ_NONE;
3551 	}
3552 restart_ih:
3553 	/* Order reading of wptr vs. reading of IH ring data */
3554 	rmb();
3555 
3556 	/* display interrupts */
3557 	si_irq_ack(rdev);
3558 
3559 	rdev->ih.wptr = wptr;
3560 	while (rptr != wptr) {
3561 		/* wptr/rptr are in bytes! */
3562 		ring_index = rptr / 4;
3563 		src_id =  le32_to_cpu(rdev->ih.ring[ring_index]) & 0xff;
3564 		src_data = le32_to_cpu(rdev->ih.ring[ring_index + 1]) & 0xfffffff;
3565 		ring_id = le32_to_cpu(rdev->ih.ring[ring_index + 2]) & 0xff;
3566 
3567 		switch (src_id) {
3568 		case 1: /* D1 vblank/vline */
3569 			switch (src_data) {
3570 			case 0: /* D1 vblank */
3571 				if (rdev->irq.stat_regs.evergreen.disp_int & LB_D1_VBLANK_INTERRUPT) {
3572 					if (rdev->irq.crtc_vblank_int[0]) {
3573 						drm_handle_vblank(rdev->ddev, 0);
3574 						rdev->pm.vblank_sync = true;
3575 						wake_up(&rdev->irq.vblank_queue);
3576 					}
3577 					if (rdev->irq.pflip[0])
3578 						radeon_crtc_handle_flip(rdev, 0);
3579 					rdev->irq.stat_regs.evergreen.disp_int &= ~LB_D1_VBLANK_INTERRUPT;
3580 					DRM_DEBUG("IH: D1 vblank\n");
3581 				}
3582 				break;
3583 			case 1: /* D1 vline */
3584 				if (rdev->irq.stat_regs.evergreen.disp_int & LB_D1_VLINE_INTERRUPT) {
3585 					rdev->irq.stat_regs.evergreen.disp_int &= ~LB_D1_VLINE_INTERRUPT;
3586 					DRM_DEBUG("IH: D1 vline\n");
3587 				}
3588 				break;
3589 			default:
3590 				DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
3591 				break;
3592 			}
3593 			break;
3594 		case 2: /* D2 vblank/vline */
3595 			switch (src_data) {
3596 			case 0: /* D2 vblank */
3597 				if (rdev->irq.stat_regs.evergreen.disp_int_cont & LB_D2_VBLANK_INTERRUPT) {
3598 					if (rdev->irq.crtc_vblank_int[1]) {
3599 						drm_handle_vblank(rdev->ddev, 1);
3600 						rdev->pm.vblank_sync = true;
3601 						wake_up(&rdev->irq.vblank_queue);
3602 					}
3603 					if (rdev->irq.pflip[1])
3604 						radeon_crtc_handle_flip(rdev, 1);
3605 					rdev->irq.stat_regs.evergreen.disp_int_cont &= ~LB_D2_VBLANK_INTERRUPT;
3606 					DRM_DEBUG("IH: D2 vblank\n");
3607 				}
3608 				break;
3609 			case 1: /* D2 vline */
3610 				if (rdev->irq.stat_regs.evergreen.disp_int_cont & LB_D2_VLINE_INTERRUPT) {
3611 					rdev->irq.stat_regs.evergreen.disp_int_cont &= ~LB_D2_VLINE_INTERRUPT;
3612 					DRM_DEBUG("IH: D2 vline\n");
3613 				}
3614 				break;
3615 			default:
3616 				DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
3617 				break;
3618 			}
3619 			break;
3620 		case 3: /* D3 vblank/vline */
3621 			switch (src_data) {
3622 			case 0: /* D3 vblank */
3623 				if (rdev->irq.stat_regs.evergreen.disp_int_cont2 & LB_D3_VBLANK_INTERRUPT) {
3624 					if (rdev->irq.crtc_vblank_int[2]) {
3625 						drm_handle_vblank(rdev->ddev, 2);
3626 						rdev->pm.vblank_sync = true;
3627 						wake_up(&rdev->irq.vblank_queue);
3628 					}
3629 					if (rdev->irq.pflip[2])
3630 						radeon_crtc_handle_flip(rdev, 2);
3631 					rdev->irq.stat_regs.evergreen.disp_int_cont2 &= ~LB_D3_VBLANK_INTERRUPT;
3632 					DRM_DEBUG("IH: D3 vblank\n");
3633 				}
3634 				break;
3635 			case 1: /* D3 vline */
3636 				if (rdev->irq.stat_regs.evergreen.disp_int_cont2 & LB_D3_VLINE_INTERRUPT) {
3637 					rdev->irq.stat_regs.evergreen.disp_int_cont2 &= ~LB_D3_VLINE_INTERRUPT;
3638 					DRM_DEBUG("IH: D3 vline\n");
3639 				}
3640 				break;
3641 			default:
3642 				DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
3643 				break;
3644 			}
3645 			break;
3646 		case 4: /* D4 vblank/vline */
3647 			switch (src_data) {
3648 			case 0: /* D4 vblank */
3649 				if (rdev->irq.stat_regs.evergreen.disp_int_cont3 & LB_D4_VBLANK_INTERRUPT) {
3650 					if (rdev->irq.crtc_vblank_int[3]) {
3651 						drm_handle_vblank(rdev->ddev, 3);
3652 						rdev->pm.vblank_sync = true;
3653 						wake_up(&rdev->irq.vblank_queue);
3654 					}
3655 					if (rdev->irq.pflip[3])
3656 						radeon_crtc_handle_flip(rdev, 3);
3657 					rdev->irq.stat_regs.evergreen.disp_int_cont3 &= ~LB_D4_VBLANK_INTERRUPT;
3658 					DRM_DEBUG("IH: D4 vblank\n");
3659 				}
3660 				break;
3661 			case 1: /* D4 vline */
3662 				if (rdev->irq.stat_regs.evergreen.disp_int_cont3 & LB_D4_VLINE_INTERRUPT) {
3663 					rdev->irq.stat_regs.evergreen.disp_int_cont3 &= ~LB_D4_VLINE_INTERRUPT;
3664 					DRM_DEBUG("IH: D4 vline\n");
3665 				}
3666 				break;
3667 			default:
3668 				DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
3669 				break;
3670 			}
3671 			break;
3672 		case 5: /* D5 vblank/vline */
3673 			switch (src_data) {
3674 			case 0: /* D5 vblank */
3675 				if (rdev->irq.stat_regs.evergreen.disp_int_cont4 & LB_D5_VBLANK_INTERRUPT) {
3676 					if (rdev->irq.crtc_vblank_int[4]) {
3677 						drm_handle_vblank(rdev->ddev, 4);
3678 						rdev->pm.vblank_sync = true;
3679 						wake_up(&rdev->irq.vblank_queue);
3680 					}
3681 					if (rdev->irq.pflip[4])
3682 						radeon_crtc_handle_flip(rdev, 4);
3683 					rdev->irq.stat_regs.evergreen.disp_int_cont4 &= ~LB_D5_VBLANK_INTERRUPT;
3684 					DRM_DEBUG("IH: D5 vblank\n");
3685 				}
3686 				break;
3687 			case 1: /* D5 vline */
3688 				if (rdev->irq.stat_regs.evergreen.disp_int_cont4 & LB_D5_VLINE_INTERRUPT) {
3689 					rdev->irq.stat_regs.evergreen.disp_int_cont4 &= ~LB_D5_VLINE_INTERRUPT;
3690 					DRM_DEBUG("IH: D5 vline\n");
3691 				}
3692 				break;
3693 			default:
3694 				DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
3695 				break;
3696 			}
3697 			break;
3698 		case 6: /* D6 vblank/vline */
3699 			switch (src_data) {
3700 			case 0: /* D6 vblank */
3701 				if (rdev->irq.stat_regs.evergreen.disp_int_cont5 & LB_D6_VBLANK_INTERRUPT) {
3702 					if (rdev->irq.crtc_vblank_int[5]) {
3703 						drm_handle_vblank(rdev->ddev, 5);
3704 						rdev->pm.vblank_sync = true;
3705 						wake_up(&rdev->irq.vblank_queue);
3706 					}
3707 					if (rdev->irq.pflip[5])
3708 						radeon_crtc_handle_flip(rdev, 5);
3709 					rdev->irq.stat_regs.evergreen.disp_int_cont5 &= ~LB_D6_VBLANK_INTERRUPT;
3710 					DRM_DEBUG("IH: D6 vblank\n");
3711 				}
3712 				break;
3713 			case 1: /* D6 vline */
3714 				if (rdev->irq.stat_regs.evergreen.disp_int_cont5 & LB_D6_VLINE_INTERRUPT) {
3715 					rdev->irq.stat_regs.evergreen.disp_int_cont5 &= ~LB_D6_VLINE_INTERRUPT;
3716 					DRM_DEBUG("IH: D6 vline\n");
3717 				}
3718 				break;
3719 			default:
3720 				DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
3721 				break;
3722 			}
3723 			break;
3724 		case 42: /* HPD hotplug */
3725 			switch (src_data) {
3726 			case 0:
3727 				if (rdev->irq.stat_regs.evergreen.disp_int & DC_HPD1_INTERRUPT) {
3728 					rdev->irq.stat_regs.evergreen.disp_int &= ~DC_HPD1_INTERRUPT;
3729 					queue_hotplug = true;
3730 					DRM_DEBUG("IH: HPD1\n");
3731 				}
3732 				break;
3733 			case 1:
3734 				if (rdev->irq.stat_regs.evergreen.disp_int_cont & DC_HPD2_INTERRUPT) {
3735 					rdev->irq.stat_regs.evergreen.disp_int_cont &= ~DC_HPD2_INTERRUPT;
3736 					queue_hotplug = true;
3737 					DRM_DEBUG("IH: HPD2\n");
3738 				}
3739 				break;
3740 			case 2:
3741 				if (rdev->irq.stat_regs.evergreen.disp_int_cont2 & DC_HPD3_INTERRUPT) {
3742 					rdev->irq.stat_regs.evergreen.disp_int_cont2 &= ~DC_HPD3_INTERRUPT;
3743 					queue_hotplug = true;
3744 					DRM_DEBUG("IH: HPD3\n");
3745 				}
3746 				break;
3747 			case 3:
3748 				if (rdev->irq.stat_regs.evergreen.disp_int_cont3 & DC_HPD4_INTERRUPT) {
3749 					rdev->irq.stat_regs.evergreen.disp_int_cont3 &= ~DC_HPD4_INTERRUPT;
3750 					queue_hotplug = true;
3751 					DRM_DEBUG("IH: HPD4\n");
3752 				}
3753 				break;
3754 			case 4:
3755 				if (rdev->irq.stat_regs.evergreen.disp_int_cont4 & DC_HPD5_INTERRUPT) {
3756 					rdev->irq.stat_regs.evergreen.disp_int_cont4 &= ~DC_HPD5_INTERRUPT;
3757 					queue_hotplug = true;
3758 					DRM_DEBUG("IH: HPD5\n");
3759 				}
3760 				break;
3761 			case 5:
3762 				if (rdev->irq.stat_regs.evergreen.disp_int_cont5 & DC_HPD6_INTERRUPT) {
3763 					rdev->irq.stat_regs.evergreen.disp_int_cont5 &= ~DC_HPD6_INTERRUPT;
3764 					queue_hotplug = true;
3765 					DRM_DEBUG("IH: HPD6\n");
3766 				}
3767 				break;
3768 			default:
3769 				DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
3770 				break;
3771 			}
3772 			break;
3773 		case 176: /* RINGID0 CP_INT */
3774 			radeon_fence_process(rdev, RADEON_RING_TYPE_GFX_INDEX);
3775 			break;
3776 		case 177: /* RINGID1 CP_INT */
3777 			radeon_fence_process(rdev, CAYMAN_RING_TYPE_CP1_INDEX);
3778 			break;
3779 		case 178: /* RINGID2 CP_INT */
3780 			radeon_fence_process(rdev, CAYMAN_RING_TYPE_CP2_INDEX);
3781 			break;
3782 		case 181: /* CP EOP event */
3783 			DRM_DEBUG("IH: CP EOP\n");
3784 			switch (ring_id) {
3785 			case 0:
3786 				radeon_fence_process(rdev, RADEON_RING_TYPE_GFX_INDEX);
3787 				break;
3788 			case 1:
3789 				radeon_fence_process(rdev, CAYMAN_RING_TYPE_CP1_INDEX);
3790 				break;
3791 			case 2:
3792 				radeon_fence_process(rdev, CAYMAN_RING_TYPE_CP2_INDEX);
3793 				break;
3794 			}
3795 			break;
3796 		case 233: /* GUI IDLE */
3797 			DRM_DEBUG("IH: GUI idle\n");
3798 			rdev->pm.gui_idle = true;
3799 			wake_up(&rdev->irq.idle_queue);
3800 			break;
3801 		default:
3802 			DRM_DEBUG("Unhandled interrupt: %d %d\n", src_id, src_data);
3803 			break;
3804 		}
3805 
3806 		/* wptr/rptr are in bytes! */
3807 		rptr += 16;
3808 		rptr &= rdev->ih.ptr_mask;
3809 	}
3810 	/* make sure wptr hasn't changed while processing */
3811 	wptr = si_get_ih_wptr(rdev);
3812 	if (wptr != rdev->ih.wptr)
3813 		goto restart_ih;
3814 	if (queue_hotplug)
3815 		schedule_work(&rdev->hotplug_work);
3816 	rdev->ih.rptr = rptr;
3817 	WREG32(IH_RB_RPTR, rdev->ih.rptr);
3818 	spin_unlock_irqrestore(&rdev->ih.lock, flags);
3819 	return IRQ_HANDLED;
3820 }
3821 
3822 /*
3823  * startup/shutdown callbacks
3824  */
si_startup(struct radeon_device * rdev)3825 static int si_startup(struct radeon_device *rdev)
3826 {
3827 	struct radeon_ring *ring;
3828 	int r;
3829 
3830 	if (!rdev->me_fw || !rdev->pfp_fw || !rdev->ce_fw ||
3831 	    !rdev->rlc_fw || !rdev->mc_fw) {
3832 		r = si_init_microcode(rdev);
3833 		if (r) {
3834 			DRM_ERROR("Failed to load firmware!\n");
3835 			return r;
3836 		}
3837 	}
3838 
3839 	r = si_mc_load_microcode(rdev);
3840 	if (r) {
3841 		DRM_ERROR("Failed to load MC firmware!\n");
3842 		return r;
3843 	}
3844 
3845 	r = r600_vram_scratch_init(rdev);
3846 	if (r)
3847 		return r;
3848 
3849 	si_mc_program(rdev);
3850 	r = si_pcie_gart_enable(rdev);
3851 	if (r)
3852 		return r;
3853 	si_gpu_init(rdev);
3854 
3855 #if 0
3856 	r = evergreen_blit_init(rdev);
3857 	if (r) {
3858 		r600_blit_fini(rdev);
3859 		rdev->asic->copy = NULL;
3860 		dev_warn(rdev->dev, "failed blitter (%d) falling back to memcpy\n", r);
3861 	}
3862 #endif
3863 	/* allocate rlc buffers */
3864 	r = si_rlc_init(rdev);
3865 	if (r) {
3866 		DRM_ERROR("Failed to init rlc BOs!\n");
3867 		return r;
3868 	}
3869 
3870 	/* allocate wb buffer */
3871 	r = radeon_wb_init(rdev);
3872 	if (r)
3873 		return r;
3874 
3875 	r = radeon_fence_driver_start_ring(rdev, RADEON_RING_TYPE_GFX_INDEX);
3876 	if (r) {
3877 		dev_err(rdev->dev, "failed initializing CP fences (%d).\n", r);
3878 		return r;
3879 	}
3880 
3881 	r = radeon_fence_driver_start_ring(rdev, CAYMAN_RING_TYPE_CP1_INDEX);
3882 	if (r) {
3883 		dev_err(rdev->dev, "failed initializing CP fences (%d).\n", r);
3884 		return r;
3885 	}
3886 
3887 	r = radeon_fence_driver_start_ring(rdev, CAYMAN_RING_TYPE_CP2_INDEX);
3888 	if (r) {
3889 		dev_err(rdev->dev, "failed initializing CP fences (%d).\n", r);
3890 		return r;
3891 	}
3892 
3893 	/* Enable IRQ */
3894 	if (!rdev->irq.installed) {
3895 		r = radeon_irq_kms_init(rdev);
3896 		if (r)
3897 			return r;
3898 	}
3899 
3900 	r = si_irq_init(rdev);
3901 	if (r) {
3902 		DRM_ERROR("radeon: IH init failed (%d).\n", r);
3903 		radeon_irq_kms_fini(rdev);
3904 		return r;
3905 	}
3906 	si_irq_set(rdev);
3907 
3908 	ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
3909 	r = radeon_ring_init(rdev, ring, ring->ring_size, RADEON_WB_CP_RPTR_OFFSET,
3910 			     CP_RB0_RPTR, CP_RB0_WPTR,
3911 			     0, 0xfffff, RADEON_CP_PACKET2);
3912 	if (r)
3913 		return r;
3914 
3915 	ring = &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX];
3916 	r = radeon_ring_init(rdev, ring, ring->ring_size, RADEON_WB_CP1_RPTR_OFFSET,
3917 			     CP_RB1_RPTR, CP_RB1_WPTR,
3918 			     0, 0xfffff, RADEON_CP_PACKET2);
3919 	if (r)
3920 		return r;
3921 
3922 	ring = &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX];
3923 	r = radeon_ring_init(rdev, ring, ring->ring_size, RADEON_WB_CP2_RPTR_OFFSET,
3924 			     CP_RB2_RPTR, CP_RB2_WPTR,
3925 			     0, 0xfffff, RADEON_CP_PACKET2);
3926 	if (r)
3927 		return r;
3928 
3929 	r = si_cp_load_microcode(rdev);
3930 	if (r)
3931 		return r;
3932 	r = si_cp_resume(rdev);
3933 	if (r)
3934 		return r;
3935 
3936 	r = radeon_ib_pool_start(rdev);
3937 	if (r)
3938 		return r;
3939 
3940 	r = radeon_ib_test(rdev, RADEON_RING_TYPE_GFX_INDEX, &rdev->ring[RADEON_RING_TYPE_GFX_INDEX]);
3941 	if (r) {
3942 		DRM_ERROR("radeon: failed testing IB (%d) on CP ring 0\n", r);
3943 		rdev->accel_working = false;
3944 		return r;
3945 	}
3946 
3947 	r = radeon_ib_test(rdev, CAYMAN_RING_TYPE_CP1_INDEX, &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX]);
3948 	if (r) {
3949 		DRM_ERROR("radeon: failed testing IB (%d) on CP ring 1\n", r);
3950 		rdev->accel_working = false;
3951 		return r;
3952 	}
3953 
3954 	r = radeon_ib_test(rdev, CAYMAN_RING_TYPE_CP2_INDEX, &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX]);
3955 	if (r) {
3956 		DRM_ERROR("radeon: failed testing IB (%d) on CP ring 2\n", r);
3957 		rdev->accel_working = false;
3958 		return r;
3959 	}
3960 
3961 	r = radeon_vm_manager_start(rdev);
3962 	if (r)
3963 		return r;
3964 
3965 	return 0;
3966 }
3967 
si_resume(struct radeon_device * rdev)3968 int si_resume(struct radeon_device *rdev)
3969 {
3970 	int r;
3971 
3972 	/* Do not reset GPU before posting, on rv770 hw unlike on r500 hw,
3973 	 * posting will perform necessary task to bring back GPU into good
3974 	 * shape.
3975 	 */
3976 	/* post card */
3977 	atom_asic_init(rdev->mode_info.atom_context);
3978 
3979 	rdev->accel_working = true;
3980 	r = si_startup(rdev);
3981 	if (r) {
3982 		DRM_ERROR("si startup failed on resume\n");
3983 		rdev->accel_working = false;
3984 		return r;
3985 	}
3986 
3987 	return r;
3988 
3989 }
3990 
si_suspend(struct radeon_device * rdev)3991 int si_suspend(struct radeon_device *rdev)
3992 {
3993 	/* FIXME: we should wait for ring to be empty */
3994 	radeon_ib_pool_suspend(rdev);
3995 	radeon_vm_manager_suspend(rdev);
3996 #if 0
3997 	r600_blit_suspend(rdev);
3998 #endif
3999 	si_cp_enable(rdev, false);
4000 	rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready = false;
4001 	rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX].ready = false;
4002 	rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX].ready = false;
4003 	si_irq_suspend(rdev);
4004 	radeon_wb_disable(rdev);
4005 	si_pcie_gart_disable(rdev);
4006 	return 0;
4007 }
4008 
4009 /* Plan is to move initialization in that function and use
4010  * helper function so that radeon_device_init pretty much
4011  * do nothing more than calling asic specific function. This
4012  * should also allow to remove a bunch of callback function
4013  * like vram_info.
4014  */
si_init(struct radeon_device * rdev)4015 int si_init(struct radeon_device *rdev)
4016 {
4017 	struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
4018 	int r;
4019 
4020 	/* This don't do much */
4021 	r = radeon_gem_init(rdev);
4022 	if (r)
4023 		return r;
4024 	/* Read BIOS */
4025 	if (!radeon_get_bios(rdev)) {
4026 		if (ASIC_IS_AVIVO(rdev))
4027 			return -EINVAL;
4028 	}
4029 	/* Must be an ATOMBIOS */
4030 	if (!rdev->is_atom_bios) {
4031 		dev_err(rdev->dev, "Expecting atombios for cayman GPU\n");
4032 		return -EINVAL;
4033 	}
4034 	r = radeon_atombios_init(rdev);
4035 	if (r)
4036 		return r;
4037 
4038 	/* Post card if necessary */
4039 	if (!radeon_card_posted(rdev)) {
4040 		if (!rdev->bios) {
4041 			dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
4042 			return -EINVAL;
4043 		}
4044 		DRM_INFO("GPU not posted. posting now...\n");
4045 		atom_asic_init(rdev->mode_info.atom_context);
4046 	}
4047 	/* Initialize scratch registers */
4048 	si_scratch_init(rdev);
4049 	/* Initialize surface registers */
4050 	radeon_surface_init(rdev);
4051 	/* Initialize clocks */
4052 	radeon_get_clock_info(rdev->ddev);
4053 
4054 	/* Fence driver */
4055 	r = radeon_fence_driver_init(rdev);
4056 	if (r)
4057 		return r;
4058 
4059 	/* initialize memory controller */
4060 	r = si_mc_init(rdev);
4061 	if (r)
4062 		return r;
4063 	/* Memory manager */
4064 	r = radeon_bo_init(rdev);
4065 	if (r)
4066 		return r;
4067 
4068 	ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
4069 	ring->ring_obj = NULL;
4070 	r600_ring_init(rdev, ring, 1024 * 1024);
4071 
4072 	ring = &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX];
4073 	ring->ring_obj = NULL;
4074 	r600_ring_init(rdev, ring, 1024 * 1024);
4075 
4076 	ring = &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX];
4077 	ring->ring_obj = NULL;
4078 	r600_ring_init(rdev, ring, 1024 * 1024);
4079 
4080 	rdev->ih.ring_obj = NULL;
4081 	r600_ih_ring_init(rdev, 64 * 1024);
4082 
4083 	r = r600_pcie_gart_init(rdev);
4084 	if (r)
4085 		return r;
4086 
4087 	r = radeon_ib_pool_init(rdev);
4088 	rdev->accel_working = true;
4089 	if (r) {
4090 		dev_err(rdev->dev, "IB initialization failed (%d).\n", r);
4091 		rdev->accel_working = false;
4092 	}
4093 	r = radeon_vm_manager_init(rdev);
4094 	if (r) {
4095 		dev_err(rdev->dev, "vm manager initialization failed (%d).\n", r);
4096 	}
4097 
4098 	r = si_startup(rdev);
4099 	if (r) {
4100 		dev_err(rdev->dev, "disabling GPU acceleration\n");
4101 		si_cp_fini(rdev);
4102 		si_irq_fini(rdev);
4103 		si_rlc_fini(rdev);
4104 		radeon_wb_fini(rdev);
4105 		r100_ib_fini(rdev);
4106 		radeon_vm_manager_fini(rdev);
4107 		radeon_irq_kms_fini(rdev);
4108 		si_pcie_gart_fini(rdev);
4109 		rdev->accel_working = false;
4110 	}
4111 
4112 	/* Don't start up if the MC ucode is missing.
4113 	 * The default clocks and voltages before the MC ucode
4114 	 * is loaded are not suffient for advanced operations.
4115 	 */
4116 	if (!rdev->mc_fw) {
4117 		DRM_ERROR("radeon: MC ucode required for NI+.\n");
4118 		return -EINVAL;
4119 	}
4120 
4121 	return 0;
4122 }
4123 
si_fini(struct radeon_device * rdev)4124 void si_fini(struct radeon_device *rdev)
4125 {
4126 #if 0
4127 	r600_blit_fini(rdev);
4128 #endif
4129 	si_cp_fini(rdev);
4130 	si_irq_fini(rdev);
4131 	si_rlc_fini(rdev);
4132 	radeon_wb_fini(rdev);
4133 	radeon_vm_manager_fini(rdev);
4134 	r100_ib_fini(rdev);
4135 	radeon_irq_kms_fini(rdev);
4136 	si_pcie_gart_fini(rdev);
4137 	r600_vram_scratch_fini(rdev);
4138 	radeon_gem_fini(rdev);
4139 	radeon_semaphore_driver_fini(rdev);
4140 	radeon_fence_driver_fini(rdev);
4141 	radeon_bo_fini(rdev);
4142 	radeon_atombios_fini(rdev);
4143 	kfree(rdev->bios);
4144 	rdev->bios = NULL;
4145 }
4146 
4147