adreno_gpu.c 12.4 KB
Newer Older
R
Rob Clark 已提交
1 2 3 4
/*
 * Copyright (C) 2013 Red Hat
 * Author: Rob Clark <robdclark@gmail.com>
 *
5 6
 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
 *
R
Rob Clark 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "adreno_gpu.h"
#include "msm_gem.h"
22
#include "msm_mmu.h"
R
Rob Clark 已提交
23 24

#define RB_SIZE    SZ_32K
25
#define RB_BLKSIZE 32
R
Rob Clark 已提交
26 27 28 29 30 31 32 33 34 35

int adreno_get_param(struct msm_gpu *gpu, uint32_t param, uint64_t *value)
{
	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);

	switch (param) {
	case MSM_PARAM_GPU_ID:
		*value = adreno_gpu->info->revn;
		return 0;
	case MSM_PARAM_GMEM_SIZE:
R
Rob Clark 已提交
36
		*value = adreno_gpu->gmem;
R
Rob Clark 已提交
37
		return 0;
J
Jordan Crouse 已提交
38 39 40
	case MSM_PARAM_GMEM_BASE:
		*value = 0x100000;
		return 0;
R
Rob Clark 已提交
41 42 43 44 45 46
	case MSM_PARAM_CHIP_ID:
		*value = adreno_gpu->rev.patchid |
				(adreno_gpu->rev.minor << 8) |
				(adreno_gpu->rev.major << 16) |
				(adreno_gpu->rev.core << 24);
		return 0;
47 48 49
	case MSM_PARAM_MAX_FREQ:
		*value = adreno_gpu->base.fast_rate;
		return 0;
R
Rob Clark 已提交
50 51 52 53
	case MSM_PARAM_TIMESTAMP:
		if (adreno_gpu->funcs->get_timestamp)
			return adreno_gpu->funcs->get_timestamp(gpu, value);
		return -EINVAL;
R
Rob Clark 已提交
54 55 56 57 58 59 60 61 62
	default:
		DBG("%s: invalid param: %u", gpu->name, param);
		return -EINVAL;
	}
}

int adreno_hw_init(struct msm_gpu *gpu)
{
	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
R
Rob Clark 已提交
63
	int ret;
R
Rob Clark 已提交
64 65 66

	DBG("%s", gpu->name);

67
	ret = msm_gem_get_iova(gpu->rb->bo, gpu->aspace, &gpu->rb_iova);
R
Rob Clark 已提交
68 69 70 71 72 73
	if (ret) {
		gpu->rb_iova = 0;
		dev_err(gpu->dev->dev, "could not map ringbuffer: %d\n", ret);
		return ret;
	}

74 75 76 77 78 79 80
	/* reset ringbuffer: */
	gpu->rb->cur = gpu->rb->start;

	/* reset completed fence seqno: */
	adreno_gpu->memptrs->fence = gpu->fctx->completed_fence;
	adreno_gpu->memptrs->rptr  = 0;

R
Rob Clark 已提交
81
	/* Setup REG_CP_RB_CNTL: */
82
	adreno_gpu_write(adreno_gpu, REG_ADRENO_CP_RB_CNTL,
R
Rob Clark 已提交
83 84
			/* size is log2(quad-words): */
			AXXX_CP_RB_CNTL_BUFSZ(ilog2(gpu->rb->size / 8)) |
85 86
			AXXX_CP_RB_CNTL_BLKSZ(ilog2(RB_BLKSIZE / 8)) |
			(adreno_is_a430(adreno_gpu) ? AXXX_CP_RB_CNTL_NO_UPDATE : 0));
R
Rob Clark 已提交
87 88

	/* Setup ringbuffer address: */
J
Jordan Crouse 已提交
89 90
	adreno_gpu_write64(adreno_gpu, REG_ADRENO_CP_RB_BASE,
		REG_ADRENO_CP_RB_BASE_HI, gpu->rb_iova);
R
Rob Clark 已提交
91

J
Jordan Crouse 已提交
92 93 94 95 96
	if (!adreno_is_a430(adreno_gpu)) {
		adreno_gpu_write64(adreno_gpu, REG_ADRENO_CP_RB_RPTR_ADDR,
			REG_ADRENO_CP_RB_RPTR_ADDR_HI,
			rbmemptr(adreno_gpu, rptr));
	}
R
Rob Clark 已提交
97 98 99 100 101 102 103 104 105

	return 0;
}

static uint32_t get_wptr(struct msm_ringbuffer *ring)
{
	return ring->cur - ring->start;
}

106 107 108 109 110 111 112 113 114 115
/* Use this helper to read rptr, since a430 doesn't update rptr in memory */
static uint32_t get_rptr(struct adreno_gpu *adreno_gpu)
{
	if (adreno_is_a430(adreno_gpu))
		return adreno_gpu->memptrs->rptr = adreno_gpu_read(
			adreno_gpu, REG_ADRENO_CP_RB_RPTR);
	else
		return adreno_gpu->memptrs->rptr;
}

R
Rob Clark 已提交
116 117 118 119 120 121
uint32_t adreno_last_fence(struct msm_gpu *gpu)
{
	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
	return adreno_gpu->memptrs->fence;
}

122 123 124 125 126
void adreno_recover(struct msm_gpu *gpu)
{
	struct drm_device *dev = gpu->dev;
	int ret;

R
Rob Clark 已提交
127 128 129
	// XXX pm-runtime??  we *need* the device to be off after this
	// so maybe continuing to call ->pm_suspend/resume() is better?

130 131
	gpu->funcs->pm_suspend(gpu);
	gpu->funcs->pm_resume(gpu);
132

R
Rob Clark 已提交
133
	ret = msm_gpu_hw_init(gpu);
134 135 136 137 138 139
	if (ret) {
		dev_err(dev->dev, "gpu hw init failed: %d\n", ret);
		/* hmm, oh well? */
	}
}

140
void adreno_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
R
Rob Clark 已提交
141 142 143 144 145
		struct msm_file_private *ctx)
{
	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
	struct msm_drm_private *priv = gpu->dev->dev_private;
	struct msm_ringbuffer *ring = gpu->rb;
146
	unsigned i;
R
Rob Clark 已提交
147 148 149 150 151 152 153 154 155 156 157

	for (i = 0; i < submit->nr_cmds; i++) {
		switch (submit->cmd[i].type) {
		case MSM_SUBMIT_CMD_IB_TARGET_BUF:
			/* ignore IB-targets */
			break;
		case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
			/* ignore if there has not been a ctx switch: */
			if (priv->lastctx == ctx)
				break;
		case MSM_SUBMIT_CMD_BUF:
158 159
			OUT_PKT3(ring, adreno_is_a430(adreno_gpu) ?
				CP_INDIRECT_BUFFER_PFE : CP_INDIRECT_BUFFER_PFD, 2);
R
Rob Clark 已提交
160 161
			OUT_RING(ring, submit->cmd[i].iova);
			OUT_RING(ring, submit->cmd[i].size);
162
			OUT_PKT2(ring);
R
Rob Clark 已提交
163 164 165 166 167
			break;
		}
	}

	OUT_PKT0(ring, REG_AXXX_CP_SCRATCH_REG2, 1);
R
Rob Clark 已提交
168
	OUT_RING(ring, submit->fence->seqno);
R
Rob Clark 已提交
169

170
	if (adreno_is_a3xx(adreno_gpu) || adreno_is_a4xx(adreno_gpu)) {
R
Rob Clark 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184
		/* Flush HLSQ lazy updates to make sure there is nothing
		 * pending for indirect loads after the timestamp has
		 * passed:
		 */
		OUT_PKT3(ring, CP_EVENT_WRITE, 1);
		OUT_RING(ring, HLSQ_FLUSH);

		OUT_PKT3(ring, CP_WAIT_FOR_IDLE, 1);
		OUT_RING(ring, 0x00000000);
	}

	OUT_PKT3(ring, CP_EVENT_WRITE, 3);
	OUT_RING(ring, CACHE_FLUSH_TS);
	OUT_RING(ring, rbmemptr(adreno_gpu, fence));
R
Rob Clark 已提交
185
	OUT_RING(ring, submit->fence->seqno);
R
Rob Clark 已提交
186 187 188 189 190

	/* we could maybe be clever and only CP_COND_EXEC the interrupt: */
	OUT_PKT3(ring, CP_INTERRUPT, 1);
	OUT_RING(ring, 0x80000000);

191 192 193 194 195 196 197 198 199 200 201
	/* Workaround for missing irq issue on 8x16/a306.  Unsure if the
	 * root cause is a platform issue or some a306 quirk, but this
	 * keeps things humming along:
	 */
	if (adreno_is_a306(adreno_gpu)) {
		OUT_PKT3(ring, CP_WAIT_FOR_IDLE, 1);
		OUT_RING(ring, 0x00000000);
		OUT_PKT3(ring, CP_INTERRUPT, 1);
		OUT_RING(ring, 0x80000000);
	}

R
Rob Clark 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215
#if 0
	if (adreno_is_a3xx(adreno_gpu)) {
		/* Dummy set-constant to trigger context rollover */
		OUT_PKT3(ring, CP_SET_CONSTANT, 2);
		OUT_RING(ring, CP_REG(REG_A3XX_HLSQ_CL_KERNEL_GROUP_X_REG));
		OUT_RING(ring, 0x00000000);
	}
#endif

	gpu->funcs->flush(gpu);
}

void adreno_flush(struct msm_gpu *gpu)
{
216
	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
217 218 219 220 221 222 223 224
	uint32_t wptr;

	/*
	 * Mask wptr value that we calculate to fit in the HW range. This is
	 * to account for the possibility that the last command fit exactly into
	 * the ringbuffer and rb->next hasn't wrapped to zero yet
	 */
	wptr = get_wptr(gpu->rb) & ((gpu->rb->size / 4) - 1);
R
Rob Clark 已提交
225 226 227 228

	/* ensure writes to ringbuffer have hit system memory: */
	mb();

229
	adreno_gpu_write(adreno_gpu, REG_ADRENO_CP_RB_WPTR, wptr);
R
Rob Clark 已提交
230 231
}

232
bool adreno_idle(struct msm_gpu *gpu)
R
Rob Clark 已提交
233 234
{
	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
R
Rob Clark 已提交
235
	uint32_t wptr = get_wptr(gpu->rb);
R
Rob Clark 已提交
236

R
Rob Clark 已提交
237
	/* wait for CP to drain ringbuffer: */
238 239
	if (!spin_until(get_rptr(adreno_gpu) == wptr))
		return true;
R
Rob Clark 已提交
240 241

	/* TODO maybe we need to reset GPU here to recover from hang? */
242 243
	DRM_ERROR("%s: timeout waiting to drain ringbuffer!\n", gpu->name);
	return false;
R
Rob Clark 已提交
244 245 246 247 248 249
}

#ifdef CONFIG_DEBUG_FS
void adreno_show(struct msm_gpu *gpu, struct seq_file *m)
{
	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
250
	int i;
R
Rob Clark 已提交
251 252 253 254 255 256 257

	seq_printf(m, "revision: %d (%d.%d.%d.%d)\n",
			adreno_gpu->info->revn, adreno_gpu->rev.core,
			adreno_gpu->rev.major, adreno_gpu->rev.minor,
			adreno_gpu->rev.patchid);

	seq_printf(m, "fence:    %d/%d\n", adreno_gpu->memptrs->fence,
R
Rob Clark 已提交
258
			gpu->fctx->last_fence);
259
	seq_printf(m, "rptr:     %d\n", get_rptr(adreno_gpu));
R
Rob Clark 已提交
260
	seq_printf(m, "rb wptr:  %d\n", get_wptr(gpu->rb));
261 262 263 264 265 266 267 268 269 270 271 272 273

	/* dump these out in a form that can be parsed by demsm: */
	seq_printf(m, "IO:region %s 00000000 00020000\n", gpu->name);
	for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) {
		uint32_t start = adreno_gpu->registers[i];
		uint32_t end   = adreno_gpu->registers[i+1];
		uint32_t addr;

		for (addr = start; addr <= end; addr++) {
			uint32_t val = gpu_read(gpu, addr);
			seq_printf(m, "IO:R %08x %08x\n", addr<<2, val);
		}
	}
R
Rob Clark 已提交
274 275 276
}
#endif

277 278 279 280 281 282 283
/* Dump common gpu status and scratch registers on any hang, to make
 * the hangcheck logs more useful.  The scratch registers seem always
 * safe to read when GPU has hung (unlike some other regs, depending
 * on how the GPU hung), and they are useful to match up to cmdstream
 * dumps when debugging hangs:
 */
void adreno_dump_info(struct msm_gpu *gpu)
R
Rob Clark 已提交
284 285 286 287 288 289 290 291 292
{
	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);

	printk("revision: %d (%d.%d.%d.%d)\n",
			adreno_gpu->info->revn, adreno_gpu->rev.core,
			adreno_gpu->rev.major, adreno_gpu->rev.minor,
			adreno_gpu->rev.patchid);

	printk("fence:    %d/%d\n", adreno_gpu->memptrs->fence,
R
Rob Clark 已提交
293
			gpu->fctx->last_fence);
294
	printk("rptr:     %d\n", get_rptr(adreno_gpu));
R
Rob Clark 已提交
295
	printk("rb wptr:  %d\n", get_wptr(gpu->rb));
296 297 298 299 300 301 302 303
}

/* would be nice to not have to duplicate the _show() stuff with printk(): */
void adreno_dump(struct msm_gpu *gpu)
{
	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
	int i;

304 305 306 307 308 309 310 311 312 313 314 315
	/* dump these out in a form that can be parsed by demsm: */
	printk("IO:region %s 00000000 00020000\n", gpu->name);
	for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) {
		uint32_t start = adreno_gpu->registers[i];
		uint32_t end   = adreno_gpu->registers[i+1];
		uint32_t addr;

		for (addr = start; addr <= end; addr++) {
			uint32_t val = gpu_read(gpu, addr);
			printk("IO:R %08x %08x\n", addr<<2, val);
		}
	}
R
Rob Clark 已提交
316 317
}

R
Rob Clark 已提交
318
static uint32_t ring_freewords(struct msm_gpu *gpu)
R
Rob Clark 已提交
319 320
{
	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
R
Rob Clark 已提交
321 322
	uint32_t size = gpu->rb->size / 4;
	uint32_t wptr = get_wptr(gpu->rb);
323
	uint32_t rptr = get_rptr(adreno_gpu);
R
Rob Clark 已提交
324 325 326 327 328 329 330
	return (rptr + (size - 1) - wptr) % size;
}

void adreno_wait_ring(struct msm_gpu *gpu, uint32_t ndwords)
{
	if (spin_until(ring_freewords(gpu) >= ndwords))
		DRM_ERROR("%s: timeout waiting for ringbuffer space\n", gpu->name);
R
Rob Clark 已提交
331 332 333 334 335 336 337 338
}

static const char *iommu_ports[] = {
		"gfx3d_user", "gfx3d_priv",
		"gfx3d1_user", "gfx3d1_priv",
};

int adreno_gpu_init(struct drm_device *drm, struct platform_device *pdev,
339
		struct adreno_gpu *adreno_gpu, const struct adreno_gpu_funcs *funcs)
R
Rob Clark 已提交
340
{
341
	struct adreno_platform_config *config = pdev->dev.platform_data;
342
	struct msm_gpu_config adreno_gpu_config  = { 0 };
343
	struct msm_gpu *gpu = &adreno_gpu->base;
344
	int ret;
R
Rob Clark 已提交
345

346 347 348 349 350 351 352 353
	adreno_gpu->funcs = funcs;
	adreno_gpu->info = adreno_info(config->rev);
	adreno_gpu->gmem = adreno_gpu->info->gmem;
	adreno_gpu->revn = adreno_gpu->info->revn;
	adreno_gpu->rev = config->rev;

	gpu->fast_rate = config->fast_rate;
	gpu->bus_freq  = config->bus_freq;
354
#ifdef DOWNSTREAM_CONFIG_MSM_BUS_SCALING
355 356 357
	gpu->bus_scale_table = config->bus_scale_table;
#endif

358 359
	DBG("fast_rate=%u, slow_rate=27000000, bus_freq=%u",
			gpu->fast_rate, gpu->bus_freq);
R
Rob Clark 已提交
360

361 362 363 364 365 366 367 368
	adreno_gpu_config.ioname = "kgsl_3d0_reg_memory";
	adreno_gpu_config.irqname = "kgsl_3d0_irq";

	adreno_gpu_config.va_start = SZ_16M;
	adreno_gpu_config.va_end = 0xffffffff;

	adreno_gpu_config.ringsz = RB_SIZE;

369
	ret = msm_gpu_init(drm, pdev, &adreno_gpu->base, &funcs->base,
370
			adreno_gpu->info->name, &adreno_gpu_config);
371 372 373
	if (ret)
		return ret;

R
Rob Clark 已提交
374 375 376 377
	pm_runtime_set_autosuspend_delay(&pdev->dev, DRM_MSM_INACTIVE_PERIOD);
	pm_runtime_use_autosuspend(&pdev->dev);
	pm_runtime_enable(&pdev->dev);

378
	ret = request_firmware(&adreno_gpu->pm4, adreno_gpu->info->pm4fw, drm->dev);
R
Rob Clark 已提交
379 380
	if (ret) {
		dev_err(drm->dev, "failed to load %s PM4 firmware: %d\n",
381
				adreno_gpu->info->pm4fw, ret);
R
Rob Clark 已提交
382 383 384
		return ret;
	}

385
	ret = request_firmware(&adreno_gpu->pfp, adreno_gpu->info->pfpfw, drm->dev);
R
Rob Clark 已提交
386 387
	if (ret) {
		dev_err(drm->dev, "failed to load %s PFP firmware: %d\n",
388
				adreno_gpu->info->pfpfw, ret);
R
Rob Clark 已提交
389 390 391
		return ret;
	}

392 393
	if (gpu->aspace && gpu->aspace->mmu) {
		struct msm_mmu *mmu = gpu->aspace->mmu;
394 395 396 397 398
		ret = mmu->funcs->attach(mmu, iommu_ports,
				ARRAY_SIZE(iommu_ports));
		if (ret)
			return ret;
	}
R
Rob Clark 已提交
399

400
	adreno_gpu->memptrs_bo = msm_gem_new(drm, sizeof(*adreno_gpu->memptrs),
R
Rob Clark 已提交
401
			MSM_BO_UNCACHED);
402 403 404
	if (IS_ERR(adreno_gpu->memptrs_bo)) {
		ret = PTR_ERR(adreno_gpu->memptrs_bo);
		adreno_gpu->memptrs_bo = NULL;
R
Rob Clark 已提交
405 406 407 408
		dev_err(drm->dev, "could not allocate memptrs: %d\n", ret);
		return ret;
	}

409
	adreno_gpu->memptrs = msm_gem_get_vaddr(adreno_gpu->memptrs_bo);
410
	if (IS_ERR(adreno_gpu->memptrs)) {
R
Rob Clark 已提交
411 412 413 414
		dev_err(drm->dev, "could not vmap memptrs\n");
		return -ENOMEM;
	}

415
	ret = msm_gem_get_iova(adreno_gpu->memptrs_bo, gpu->aspace,
416
			&adreno_gpu->memptrs_iova);
R
Rob Clark 已提交
417 418 419 420 421 422 423 424
	if (ret) {
		dev_err(drm->dev, "could not map memptrs: %d\n", ret);
		return ret;
	}

	return 0;
}

425
void adreno_gpu_cleanup(struct adreno_gpu *adreno_gpu)
R
Rob Clark 已提交
426
{
427 428 429 430 431 432 433
	struct msm_gpu *gpu = &adreno_gpu->base;

	if (adreno_gpu->memptrs_bo) {
		if (adreno_gpu->memptrs)
			msm_gem_put_vaddr(adreno_gpu->memptrs_bo);

		if (adreno_gpu->memptrs_iova)
434
			msm_gem_put_iova(adreno_gpu->memptrs_bo, gpu->aspace);
435 436 437 438 439

		drm_gem_object_unreference_unlocked(adreno_gpu->memptrs_bo);
	}
	release_firmware(adreno_gpu->pm4);
	release_firmware(adreno_gpu->pfp);
440

441
	msm_gpu_cleanup(gpu);
442

443 444 445
	if (gpu->aspace) {
		gpu->aspace->mmu->funcs->detach(gpu->aspace->mmu,
			iommu_ports, ARRAY_SIZE(iommu_ports));
446
		msm_gem_address_space_put(gpu->aspace);
R
Rob Clark 已提交
447 448
	}
}