dpu_mdss.c 8.18 KB
Newer Older
1 2 3 4 5
/*
 * SPDX-License-Identifier: GPL-2.0
 * Copyright (c) 2018, The Linux Foundation
 */

6 7 8 9
#include <linux/irq.h>
#include <linux/irqchip.h>
#include <linux/irqdesc.h>
#include <linux/irqchip/chained_irq.h>
10
#include "dpu_kms.h"
11
#include <linux/interconnect.h>
12 13 14

#define to_dpu_mdss(x) container_of(x, struct dpu_mdss, base)

15
#define HW_REV				0x0
16 17
#define HW_INTR_STATUS			0x0010

18 19 20
/* Max BW defined in KBps */
#define MAX_BW				6800000

21 22 23 24 25
struct dpu_irq_controller {
	unsigned long enabled_mask;
	struct irq_domain *domain;
};

26 27 28 29 30 31 32 33 34 35 36
struct dpu_hw_cfg {
	u32 val;
	u32 offset;
};

struct dpu_mdss_hw_init_handler {
	u32 hw_rev;
	u32 hw_reg_count;
	struct dpu_hw_cfg* hw_cfg;
};

37 38 39 40 41 42
struct dpu_mdss {
	struct msm_mdss base;
	void __iomem *mmio;
	unsigned long mmio_len;
	struct dss_module_power mp;
	struct dpu_irq_controller irq_controller;
43 44
	struct icc_path *path[2];
	u32 num_paths;
45 46
};

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
static struct dpu_hw_cfg hw_cfg[] = {
    {
	/* UBWC global settings */
	.val = 0x1E,
	.offset = 0x144,
    }
};

static struct dpu_mdss_hw_init_handler cfg_handler[] = {
    { .hw_rev = DPU_HW_VER_620,
      .hw_reg_count = ARRAY_SIZE(hw_cfg),
      .hw_cfg = hw_cfg
    },
};

static void dpu_mdss_hw_init(struct dpu_mdss *dpu_mdss, u32 hw_rev)
{
	int i;
	u32 count = 0;
	struct dpu_hw_cfg *hw_cfg = NULL;

	for (i = 0; i < ARRAY_SIZE(cfg_handler); i++) {
		if (cfg_handler[i].hw_rev == hw_rev) {
			hw_cfg = cfg_handler[i].hw_cfg;
			count = cfg_handler[i].hw_reg_count;
			break;
	    }
	}

	for (i = 0; i < count; i++ ) {
		writel_relaxed(hw_cfg->val,
			dpu_mdss->mmio + hw_cfg->offset);
		hw_cfg++;
	}

    return;
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
static int dpu_mdss_parse_data_bus_icc_path(struct drm_device *dev,
						struct dpu_mdss *dpu_mdss)
{
	struct icc_path *path0 = of_icc_get(dev->dev, "mdp0-mem");
	struct icc_path *path1 = of_icc_get(dev->dev, "mdp1-mem");

	if (IS_ERR_OR_NULL(path0))
		return PTR_ERR_OR_ZERO(path0);

	dpu_mdss->path[0] = path0;
	dpu_mdss->num_paths = 1;

	if (!IS_ERR_OR_NULL(path1)) {
		dpu_mdss->path[1] = path1;
		dpu_mdss->num_paths++;
	}

	return 0;
}

105 106 107 108 109 110 111 112 113 114
static void dpu_mdss_icc_request_bw(struct msm_mdss *mdss)
{
	struct dpu_mdss *dpu_mdss = to_dpu_mdss(mdss);
	int i;
	u64 avg_bw = dpu_mdss->num_paths ? MAX_BW / dpu_mdss->num_paths : 0;

	for (i = 0; i < dpu_mdss->num_paths; i++)
		icc_set_bw(dpu_mdss->path[i], avg_bw, kBps_to_icc(MAX_BW));
}

115
static void dpu_mdss_irq(struct irq_desc *desc)
116
{
117 118
	struct dpu_mdss *dpu_mdss = irq_desc_get_handler_data(desc);
	struct irq_chip *chip = irq_desc_get_chip(desc);
119 120
	u32 interrupts;

121 122
	chained_irq_enter(chip, desc);

123 124 125 126 127 128 129 130 131 132 133
	interrupts = readl_relaxed(dpu_mdss->mmio + HW_INTR_STATUS);

	while (interrupts) {
		irq_hw_number_t hwirq = fls(interrupts) - 1;
		unsigned int mapping;
		int rc;

		mapping = irq_find_mapping(dpu_mdss->irq_controller.domain,
					   hwirq);
		if (mapping == 0) {
			DRM_ERROR("couldn't find irq mapping for %lu\n", hwirq);
134
			break;
135 136 137 138 139 140
		}

		rc = generic_handle_irq(mapping);
		if (rc < 0) {
			DRM_ERROR("handle irq fail: irq=%lu mapping=%u rc=%d\n",
				  hwirq, mapping, rc);
141
			break;
142 143 144 145 146
		}

		interrupts &= ~(1 << hwirq);
	}

147
	chained_irq_exit(chip, desc);
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
}

static void dpu_mdss_irq_mask(struct irq_data *irqd)
{
	struct dpu_mdss *dpu_mdss = irq_data_get_irq_chip_data(irqd);

	/* memory barrier */
	smp_mb__before_atomic();
	clear_bit(irqd->hwirq, &dpu_mdss->irq_controller.enabled_mask);
	/* memory barrier */
	smp_mb__after_atomic();
}

static void dpu_mdss_irq_unmask(struct irq_data *irqd)
{
	struct dpu_mdss *dpu_mdss = irq_data_get_irq_chip_data(irqd);

	/* memory barrier */
	smp_mb__before_atomic();
	set_bit(irqd->hwirq, &dpu_mdss->irq_controller.enabled_mask);
	/* memory barrier */
	smp_mb__after_atomic();
}

static struct irq_chip dpu_mdss_irq_chip = {
	.name = "dpu_mdss",
	.irq_mask = dpu_mdss_irq_mask,
	.irq_unmask = dpu_mdss_irq_unmask,
};

178 179
static struct lock_class_key dpu_mdss_lock_key, dpu_mdss_request_key;

180 181 182 183 184
static int dpu_mdss_irqdomain_map(struct irq_domain *domain,
		unsigned int irq, irq_hw_number_t hwirq)
{
	struct dpu_mdss *dpu_mdss = domain->host_data;

185
	irq_set_lockdep_class(irq, &dpu_mdss_lock_key, &dpu_mdss_request_key);
186
	irq_set_chip_and_handler(irq, &dpu_mdss_irq_chip, handle_level_irq);
187
	return irq_set_chip_data(irq, dpu_mdss);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
}

static const struct irq_domain_ops dpu_mdss_irqdomain_ops = {
	.map = dpu_mdss_irqdomain_map,
	.xlate = irq_domain_xlate_onecell,
};

static int _dpu_mdss_irq_domain_add(struct dpu_mdss *dpu_mdss)
{
	struct device *dev;
	struct irq_domain *domain;

	dev = dpu_mdss->base.dev->dev;

	domain = irq_domain_add_linear(dev->of_node, 32,
			&dpu_mdss_irqdomain_ops, dpu_mdss);
	if (!domain) {
		DPU_ERROR("failed to add irq_domain\n");
		return -EINVAL;
	}

	dpu_mdss->irq_controller.enabled_mask = 0;
	dpu_mdss->irq_controller.domain = domain;

	return 0;
}

215
static void _dpu_mdss_irq_domain_fini(struct dpu_mdss *dpu_mdss)
216 217 218 219 220 221 222 223 224 225
{
	if (dpu_mdss->irq_controller.domain) {
		irq_domain_remove(dpu_mdss->irq_controller.domain);
		dpu_mdss->irq_controller.domain = NULL;
	}
}
static int dpu_mdss_enable(struct msm_mdss *mdss)
{
	struct dpu_mdss *dpu_mdss = to_dpu_mdss(mdss);
	struct dss_module_power *mp = &dpu_mdss->mp;
226
	int ret;
227
	u32 mdss_rev;
228

229
	dpu_mdss_icc_request_bw(mdss);
230 231

	ret = msm_dss_enable_clk(mp->clk_config, mp->num_clk, true);
232
	if (ret) {
233
		DPU_ERROR("clock enable failed, ret:%d\n", ret);
234 235 236 237 238
		return ret;
	}

	mdss_rev = readl_relaxed(dpu_mdss->mmio + HW_REV);
	dpu_mdss_hw_init(dpu_mdss, mdss_rev);
239 240 241 242 243 244 245 246

	return ret;
}

static int dpu_mdss_disable(struct msm_mdss *mdss)
{
	struct dpu_mdss *dpu_mdss = to_dpu_mdss(mdss);
	struct dss_module_power *mp = &dpu_mdss->mp;
247
	int ret, i;
248 249 250 251 252

	ret = msm_dss_enable_clk(mp->clk_config, mp->num_clk, false);
	if (ret)
		DPU_ERROR("clock disable failed, ret:%d\n", ret);

253 254 255
	for (i = 0; i < dpu_mdss->num_paths; i++)
		icc_set_bw(dpu_mdss->path[i], 0, 0);

256 257 258 259 260 261 262 263 264
	return ret;
}

static void dpu_mdss_destroy(struct drm_device *dev)
{
	struct platform_device *pdev = to_platform_device(dev->dev);
	struct msm_drm_private *priv = dev->dev_private;
	struct dpu_mdss *dpu_mdss = to_dpu_mdss(priv->mdss);
	struct dss_module_power *mp = &dpu_mdss->mp;
265
	int irq;
266
	int i;
267

268
	pm_runtime_suspend(dev->dev);
269
	pm_runtime_disable(dev->dev);
270
	_dpu_mdss_irq_domain_fini(dpu_mdss);
271 272
	irq = platform_get_irq(pdev, 0);
	irq_set_chained_handler_and_data(irq, NULL, NULL);
273 274 275
	msm_dss_put_clk(mp->clk_config, mp->num_clk);
	devm_kfree(&pdev->dev, mp->clk_config);

276 277 278
	for (i = 0; i < dpu_mdss->num_paths; i++)
		icc_put(dpu_mdss->path[i]);

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	if (dpu_mdss->mmio)
		devm_iounmap(&pdev->dev, dpu_mdss->mmio);
	dpu_mdss->mmio = NULL;
	priv->mdss = NULL;
}

static const struct msm_mdss_funcs mdss_funcs = {
	.enable	= dpu_mdss_enable,
	.disable = dpu_mdss_disable,
	.destroy = dpu_mdss_destroy,
};

int dpu_mdss_init(struct drm_device *dev)
{
	struct platform_device *pdev = to_platform_device(dev->dev);
	struct msm_drm_private *priv = dev->dev_private;
	struct resource *res;
	struct dpu_mdss *dpu_mdss;
	struct dss_module_power *mp;
	int ret = 0;
299
	int irq;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

	dpu_mdss = devm_kzalloc(dev->dev, sizeof(*dpu_mdss), GFP_KERNEL);
	if (!dpu_mdss)
		return -ENOMEM;

	dpu_mdss->mmio = msm_ioremap(pdev, "mdss", "mdss");
	if (IS_ERR(dpu_mdss->mmio))
		return PTR_ERR(dpu_mdss->mmio);

	DRM_DEBUG("mapped mdss address space @%pK\n", dpu_mdss->mmio);

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mdss");
	if (!res) {
		DRM_ERROR("failed to get memory resource for mdss\n");
		return -ENOMEM;
	}
	dpu_mdss->mmio_len = resource_size(res);

318 319 320 321
	ret = dpu_mdss_parse_data_bus_icc_path(dev, dpu_mdss);
	if (ret)
		return ret;

322 323 324 325 326 327 328 329 330 331 332 333 334 335
	mp = &dpu_mdss->mp;
	ret = msm_dss_parse_clock(pdev, mp);
	if (ret) {
		DPU_ERROR("failed to parse clocks, ret=%d\n", ret);
		goto clk_parse_err;
	}

	dpu_mdss->base.dev = dev;
	dpu_mdss->base.funcs = &mdss_funcs;

	ret = _dpu_mdss_irq_domain_add(dpu_mdss);
	if (ret)
		goto irq_domain_error;

336 337
	irq = platform_get_irq(pdev, 0);
	if (irq < 0)
338
		goto irq_error;
339 340 341

	irq_set_chained_handler_and_data(irq, dpu_mdss_irq,
					 dpu_mdss);
342

343 344
	priv->mdss = &dpu_mdss->base;

345 346
	pm_runtime_enable(dev->dev);

347 348
	dpu_mdss_icc_request_bw(priv->mdss);

349 350 351 352 353 354 355 356 357 358 359 360 361
	return ret;

irq_error:
	_dpu_mdss_irq_domain_fini(dpu_mdss);
irq_domain_error:
	msm_dss_put_clk(mp->clk_config, mp->num_clk);
clk_parse_err:
	devm_kfree(&pdev->dev, mp->clk_config);
	if (dpu_mdss->mmio)
		devm_iounmap(&pdev->dev, dpu_mdss->mmio);
	dpu_mdss->mmio = NULL;
	return ret;
}