1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2016-17 IBM Corp.
6 #define pr_fmt(fmt) "vas: " fmt
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/export.h>
11 #include <linux/types.h>
12 #include <linux/slab.h>
13 #include <linux/platform_device.h>
14 #include <linux/of_platform.h>
15 #include <linux/of_address.h>
21 DEFINE_MUTEX(vas_mutex);
22 static LIST_HEAD(vas_instances);
24 static DEFINE_PER_CPU(int, cpu_vas_id);
26 static int init_vas_instance(struct platform_device *pdev)
30 struct vas_instance *vinst;
31 struct device_node *dn = pdev->dev.of_node;
33 rc = of_property_read_u32(dn, "ibm,vas-id", &vasid);
35 pr_err("No ibm,vas-id property for %s?\n", pdev->name);
39 if (pdev->num_resources != 4) {
40 pr_err("Unexpected DT configuration for [%s, %d]\n",
45 vinst = kzalloc(sizeof(*vinst), GFP_KERNEL);
49 INIT_LIST_HEAD(&vinst->node);
50 ida_init(&vinst->ida);
51 mutex_init(&vinst->mutex);
52 vinst->vas_id = vasid;
55 res = &pdev->resource[0];
56 vinst->hvwc_bar_start = res->start;
58 res = &pdev->resource[1];
59 vinst->uwc_bar_start = res->start;
61 res = &pdev->resource[2];
62 vinst->paste_base_addr = res->start;
64 res = &pdev->resource[3];
66 pr_err("Bad 'paste_win_id_shift' in DT, %llx\n", res->end);
70 vinst->paste_win_id_shift = 63 - res->end;
72 pr_devel("Initialized instance [%s, %d], paste_base 0x%llx, "
73 "paste_win_id_shift 0x%llx\n", pdev->name, vasid,
74 vinst->paste_base_addr, vinst->paste_win_id_shift);
76 for_each_possible_cpu(cpu) {
77 if (cpu_to_chip_id(cpu) == of_get_ibm_chip_id(dn))
78 per_cpu(cpu_vas_id, cpu) = vasid;
81 mutex_lock(&vas_mutex);
82 list_add(&vinst->node, &vas_instances);
83 mutex_unlock(&vas_mutex);
85 vas_instance_init_dbgdir(vinst);
87 dev_set_drvdata(&pdev->dev, vinst);
98 * Although this is read/used multiple times, it is written to only
99 * during initialization.
101 struct vas_instance *find_vas_instance(int vasid)
103 struct list_head *ent;
104 struct vas_instance *vinst;
106 mutex_lock(&vas_mutex);
109 vasid = per_cpu(cpu_vas_id, smp_processor_id());
111 list_for_each(ent, &vas_instances) {
112 vinst = list_entry(ent, struct vas_instance, node);
113 if (vinst->vas_id == vasid) {
114 mutex_unlock(&vas_mutex);
118 mutex_unlock(&vas_mutex);
120 pr_devel("Instance %d not found\n", vasid);
124 int chip_to_vas_id(int chipid)
128 for_each_possible_cpu(cpu) {
129 if (cpu_to_chip_id(cpu) == chipid)
130 return per_cpu(cpu_vas_id, cpu);
134 EXPORT_SYMBOL(chip_to_vas_id);
136 static int vas_probe(struct platform_device *pdev)
138 return init_vas_instance(pdev);
141 static const struct of_device_id powernv_vas_match[] = {
142 { .compatible = "ibm,vas",},
146 static struct platform_driver vas_driver = {
149 .of_match_table = powernv_vas_match,
154 static int __init vas_init(void)
157 struct device_node *dn;
159 platform_driver_register(&vas_driver);
161 for_each_compatible_node(dn, NULL, "ibm,vas") {
162 of_platform_device_create(dn, NULL, NULL);
167 platform_driver_unregister(&vas_driver);
171 pr_devel("Found %d instances\n", found);
175 device_initcall(vas_init);