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>
17 #include <linux/irqdomain.h>
18 #include <linux/interrupt.h>
24 DEFINE_MUTEX(vas_mutex);
25 static LIST_HEAD(vas_instances);
27 static DEFINE_PER_CPU(int, cpu_vas_id);
29 static int vas_irq_fault_window_setup(struct vas_instance *vinst)
33 rc = request_threaded_irq(vinst->virq, vas_fault_handler,
34 vas_fault_thread_fn, 0, vinst->name, vinst);
37 pr_err("VAS[%d]: Request IRQ(%d) failed with %d\n",
38 vinst->vas_id, vinst->virq, rc);
42 rc = vas_setup_fault_window(vinst);
44 free_irq(vinst->virq, vinst);
50 static int init_vas_instance(struct platform_device *pdev)
52 struct device_node *dn = pdev->dev.of_node;
53 struct vas_instance *vinst;
54 struct xive_irq_data *xd;
55 uint32_t chipid, hwirq;
59 rc = of_property_read_u32(dn, "ibm,vas-id", &vasid);
61 pr_err("No ibm,vas-id property for %s?\n", pdev->name);
65 rc = of_property_read_u32(dn, "ibm,chip-id", &chipid);
67 pr_err("No ibm,chip-id property for %s?\n", pdev->name);
71 if (pdev->num_resources != 4) {
72 pr_err("Unexpected DT configuration for [%s, %d]\n",
77 vinst = kzalloc(sizeof(*vinst), GFP_KERNEL);
81 vinst->name = kasprintf(GFP_KERNEL, "vas-%d", vasid);
87 INIT_LIST_HEAD(&vinst->node);
88 ida_init(&vinst->ida);
89 mutex_init(&vinst->mutex);
90 vinst->vas_id = vasid;
93 res = &pdev->resource[0];
94 vinst->hvwc_bar_start = res->start;
96 res = &pdev->resource[1];
97 vinst->uwc_bar_start = res->start;
99 res = &pdev->resource[2];
100 vinst->paste_base_addr = res->start;
102 res = &pdev->resource[3];
104 pr_err("Bad 'paste_win_id_shift' in DT, %llx\n", res->end);
108 vinst->paste_win_id_shift = 63 - res->end;
110 hwirq = xive_native_alloc_irq_on_chip(chipid);
112 pr_err("Inst%d: Unable to allocate global irq for chip %d\n",
113 vinst->vas_id, chipid);
117 vinst->virq = irq_create_mapping(NULL, hwirq);
119 pr_err("Inst%d: Unable to map global irq %d\n",
120 vinst->vas_id, hwirq);
124 xd = irq_get_handler_data(vinst->virq);
126 pr_err("Inst%d: Invalid virq %d\n",
127 vinst->vas_id, vinst->virq);
131 vinst->irq_port = xd->trig_page;
132 pr_devel("Initialized instance [%s, %d] paste_base 0x%llx paste_win_id_shift 0x%llx IRQ %d Port 0x%llx\n",
133 pdev->name, vasid, vinst->paste_base_addr,
134 vinst->paste_win_id_shift, vinst->virq,
137 for_each_possible_cpu(cpu) {
138 if (cpu_to_chip_id(cpu) == of_get_ibm_chip_id(dn))
139 per_cpu(cpu_vas_id, cpu) = vasid;
142 mutex_lock(&vas_mutex);
143 list_add(&vinst->node, &vas_instances);
144 mutex_unlock(&vas_mutex);
146 spin_lock_init(&vinst->fault_lock);
148 * IRQ and fault handling setup is needed only for user space
152 rc = vas_irq_fault_window_setup(vinst);
154 * Fault window is used only for user space send windows.
155 * So if vinst->virq is NULL, tx_win_open returns -ENODEV
162 vas_instance_init_dbgdir(vinst);
164 dev_set_drvdata(&pdev->dev, vinst);
176 * Although this is read/used multiple times, it is written to only
177 * during initialization.
179 struct vas_instance *find_vas_instance(int vasid)
181 struct list_head *ent;
182 struct vas_instance *vinst;
184 mutex_lock(&vas_mutex);
187 vasid = per_cpu(cpu_vas_id, smp_processor_id());
189 list_for_each(ent, &vas_instances) {
190 vinst = list_entry(ent, struct vas_instance, node);
191 if (vinst->vas_id == vasid) {
192 mutex_unlock(&vas_mutex);
196 mutex_unlock(&vas_mutex);
198 pr_devel("Instance %d not found\n", vasid);
202 int chip_to_vas_id(int chipid)
206 for_each_possible_cpu(cpu) {
207 if (cpu_to_chip_id(cpu) == chipid)
208 return per_cpu(cpu_vas_id, cpu);
212 EXPORT_SYMBOL(chip_to_vas_id);
214 static int vas_probe(struct platform_device *pdev)
216 return init_vas_instance(pdev);
219 static const struct of_device_id powernv_vas_match[] = {
220 { .compatible = "ibm,vas",},
224 static struct platform_driver vas_driver = {
227 .of_match_table = powernv_vas_match,
232 static int __init vas_init(void)
235 struct device_node *dn;
237 platform_driver_register(&vas_driver);
239 for_each_compatible_node(dn, NULL, "ibm,vas") {
240 of_platform_device_create(dn, NULL, NULL);
245 platform_driver_unregister(&vas_driver);
249 pr_devel("Found %d instances\n", found);
253 device_initcall(vas_init);