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)
34 snprintf(devname, sizeof(devname), "vas-%d", vinst->vas_id);
35 rc = request_threaded_irq(vinst->virq, vas_fault_handler,
36 vas_fault_thread_fn, 0, devname, vinst);
39 pr_err("VAS[%d]: Request IRQ(%d) failed with %d\n",
40 vinst->vas_id, vinst->virq, rc);
44 rc = vas_setup_fault_window(vinst);
46 free_irq(vinst->virq, vinst);
52 static int init_vas_instance(struct platform_device *pdev)
54 struct device_node *dn = pdev->dev.of_node;
55 struct vas_instance *vinst;
56 struct xive_irq_data *xd;
57 uint32_t chipid, hwirq;
61 rc = of_property_read_u32(dn, "ibm,vas-id", &vasid);
63 pr_err("No ibm,vas-id property for %s?\n", pdev->name);
67 rc = of_property_read_u32(dn, "ibm,chip-id", &chipid);
69 pr_err("No ibm,chip-id property for %s?\n", pdev->name);
73 if (pdev->num_resources != 4) {
74 pr_err("Unexpected DT configuration for [%s, %d]\n",
79 vinst = kzalloc(sizeof(*vinst), GFP_KERNEL);
83 INIT_LIST_HEAD(&vinst->node);
84 ida_init(&vinst->ida);
85 mutex_init(&vinst->mutex);
86 vinst->vas_id = vasid;
89 res = &pdev->resource[0];
90 vinst->hvwc_bar_start = res->start;
92 res = &pdev->resource[1];
93 vinst->uwc_bar_start = res->start;
95 res = &pdev->resource[2];
96 vinst->paste_base_addr = res->start;
98 res = &pdev->resource[3];
100 pr_err("Bad 'paste_win_id_shift' in DT, %llx\n", res->end);
104 vinst->paste_win_id_shift = 63 - res->end;
106 hwirq = xive_native_alloc_irq_on_chip(chipid);
108 pr_err("Inst%d: Unable to allocate global irq for chip %d\n",
109 vinst->vas_id, chipid);
113 vinst->virq = irq_create_mapping(NULL, hwirq);
115 pr_err("Inst%d: Unable to map global irq %d\n",
116 vinst->vas_id, hwirq);
120 xd = irq_get_handler_data(vinst->virq);
122 pr_err("Inst%d: Invalid virq %d\n",
123 vinst->vas_id, vinst->virq);
127 vinst->irq_port = xd->trig_page;
128 pr_devel("Initialized instance [%s, %d] paste_base 0x%llx paste_win_id_shift 0x%llx IRQ %d Port 0x%llx\n",
129 pdev->name, vasid, vinst->paste_base_addr,
130 vinst->paste_win_id_shift, vinst->virq,
133 for_each_possible_cpu(cpu) {
134 if (cpu_to_chip_id(cpu) == of_get_ibm_chip_id(dn))
135 per_cpu(cpu_vas_id, cpu) = vasid;
138 mutex_lock(&vas_mutex);
139 list_add(&vinst->node, &vas_instances);
140 mutex_unlock(&vas_mutex);
142 spin_lock_init(&vinst->fault_lock);
144 * IRQ and fault handling setup is needed only for user space
148 rc = vas_irq_fault_window_setup(vinst);
150 * Fault window is used only for user space send windows.
151 * So if vinst->virq is NULL, tx_win_open returns -ENODEV
158 vas_instance_init_dbgdir(vinst);
160 dev_set_drvdata(&pdev->dev, vinst);
171 * Although this is read/used multiple times, it is written to only
172 * during initialization.
174 struct vas_instance *find_vas_instance(int vasid)
176 struct list_head *ent;
177 struct vas_instance *vinst;
179 mutex_lock(&vas_mutex);
182 vasid = per_cpu(cpu_vas_id, smp_processor_id());
184 list_for_each(ent, &vas_instances) {
185 vinst = list_entry(ent, struct vas_instance, node);
186 if (vinst->vas_id == vasid) {
187 mutex_unlock(&vas_mutex);
191 mutex_unlock(&vas_mutex);
193 pr_devel("Instance %d not found\n", vasid);
197 int chip_to_vas_id(int chipid)
201 for_each_possible_cpu(cpu) {
202 if (cpu_to_chip_id(cpu) == chipid)
203 return per_cpu(cpu_vas_id, cpu);
207 EXPORT_SYMBOL(chip_to_vas_id);
209 static int vas_probe(struct platform_device *pdev)
211 return init_vas_instance(pdev);
214 static const struct of_device_id powernv_vas_match[] = {
215 { .compatible = "ibm,vas",},
219 static struct platform_driver vas_driver = {
222 .of_match_table = powernv_vas_match,
227 static int __init vas_init(void)
230 struct device_node *dn;
232 platform_driver_register(&vas_driver);
234 for_each_compatible_node(dn, NULL, "ibm,vas") {
235 of_platform_device_create(dn, NULL, NULL);
240 platform_driver_unregister(&vas_driver);
244 pr_devel("Found %d instances\n", found);
248 device_initcall(vas_init);