summaryrefslogtreecommitdiff
path: root/drivers/cxl/core/pmu.c
blob: 5d8e06b0ba6e88f383953982cf836c1da8099ec6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright(c) 2023 Huawei. All rights reserved. */

#include <linux/device.h>
#include <linux/slab.h>
#include <linux/idr.h>
#include <cxlmem.h>
#include <pmu.h>
#include <cxl.h>
#include "core.h"

static void cxl_pmu_release(struct device *dev)
{
	struct cxl_pmu *pmu = to_cxl_pmu(dev);

	kfree(pmu);
}

const struct device_type cxl_pmu_type = {
	.name = "cxl_pmu",
	.release = cxl_pmu_release,
};

static void remove_dev(void *dev)
{
	device_unregister(dev);
}

int devm_cxl_pmu_add(struct device *parent, struct cxl_pmu_regs *regs,
		     int assoc_id, int index, enum cxl_pmu_type type)
{
	struct cxl_pmu *pmu;
	struct device *dev;
	int rc;

	pmu = kzalloc(sizeof(*pmu), GFP_KERNEL);
	if (!pmu)
		return -ENOMEM;

	pmu->assoc_id = assoc_id;
	pmu->index = index;
	pmu->type = type;
	pmu->base = regs->pmu;
	dev = &pmu->dev;
	device_initialize(dev);
	device_set_pm_not_required(dev);
	dev->parent = parent;
	dev->bus = &cxl_bus_type;
	dev->type = &cxl_pmu_type;
	switch (pmu->type) {
	case CXL_PMU_MEMDEV:
		rc = dev_set_name(dev, "pmu_mem%d.%d", assoc_id, index);
		break;
	}
	if (rc)
		goto err;

	rc = device_add(dev);
	if (rc)
		goto err;

	return devm_add_action_or_reset(parent, remove_dev, dev);

err:
	put_device(&pmu->dev);
	return rc;
}
EXPORT_SYMBOL_NS_GPL(devm_cxl_pmu_add, CXL);