WIP FPC-III support
[linux/fpc-iii.git] / arch / powerpc / platforms / powernv / vas.c
blob598e4cd563fb19647e630c95e9231b4e938d58b5
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright 2016-17 IBM Corp.
4 */
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>
16 #include <linux/of.h>
17 #include <linux/irqdomain.h>
18 #include <linux/interrupt.h>
19 #include <asm/prom.h>
20 #include <asm/xive.h>
22 #include "vas.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)
31 char devname[64];
32 int rc = 0;
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);
38 if (rc) {
39 pr_err("VAS[%d]: Request IRQ(%d) failed with %d\n",
40 vinst->vas_id, vinst->virq, rc);
41 goto out;
44 rc = vas_setup_fault_window(vinst);
45 if (rc)
46 free_irq(vinst->virq, vinst);
48 out:
49 return rc;
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;
58 struct resource *res;
59 int rc, cpu, vasid;
61 rc = of_property_read_u32(dn, "ibm,vas-id", &vasid);
62 if (rc) {
63 pr_err("No ibm,vas-id property for %s?\n", pdev->name);
64 return -ENODEV;
67 rc = of_property_read_u32(dn, "ibm,chip-id", &chipid);
68 if (rc) {
69 pr_err("No ibm,chip-id property for %s?\n", pdev->name);
70 return -ENODEV;
73 if (pdev->num_resources != 4) {
74 pr_err("Unexpected DT configuration for [%s, %d]\n",
75 pdev->name, vasid);
76 return -ENODEV;
79 vinst = kzalloc(sizeof(*vinst), GFP_KERNEL);
80 if (!vinst)
81 return -ENOMEM;
83 INIT_LIST_HEAD(&vinst->node);
84 ida_init(&vinst->ida);
85 mutex_init(&vinst->mutex);
86 vinst->vas_id = vasid;
87 vinst->pdev = pdev;
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];
99 if (res->end > 62) {
100 pr_err("Bad 'paste_win_id_shift' in DT, %llx\n", res->end);
101 goto free_vinst;
104 vinst->paste_win_id_shift = 63 - res->end;
106 hwirq = xive_native_alloc_irq_on_chip(chipid);
107 if (!hwirq) {
108 pr_err("Inst%d: Unable to allocate global irq for chip %d\n",
109 vinst->vas_id, chipid);
110 return -ENOENT;
113 vinst->virq = irq_create_mapping(NULL, hwirq);
114 if (!vinst->virq) {
115 pr_err("Inst%d: Unable to map global irq %d\n",
116 vinst->vas_id, hwirq);
117 return -EINVAL;
120 xd = irq_get_handler_data(vinst->virq);
121 if (!xd) {
122 pr_err("Inst%d: Invalid virq %d\n",
123 vinst->vas_id, vinst->virq);
124 return -EINVAL;
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,
131 vinst->irq_port);
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
145 * send windows.
147 if (vinst->virq) {
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
152 * for user space.
154 if (rc)
155 vinst->virq = 0;
158 vas_instance_init_dbgdir(vinst);
160 dev_set_drvdata(&pdev->dev, vinst);
162 return 0;
164 free_vinst:
165 kfree(vinst);
166 return -ENODEV;
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);
181 if (vasid == -1)
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);
188 return vinst;
191 mutex_unlock(&vas_mutex);
193 pr_devel("Instance %d not found\n", vasid);
194 return NULL;
197 int chip_to_vas_id(int chipid)
199 int cpu;
201 for_each_possible_cpu(cpu) {
202 if (cpu_to_chip_id(cpu) == chipid)
203 return per_cpu(cpu_vas_id, cpu);
205 return -1;
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 = {
220 .driver = {
221 .name = "vas",
222 .of_match_table = powernv_vas_match,
224 .probe = vas_probe,
227 static int __init vas_init(void)
229 int found = 0;
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);
236 found++;
239 if (!found) {
240 platform_driver_unregister(&vas_driver);
241 return -ENODEV;
244 pr_devel("Found %d instances\n", found);
246 return 0;
248 device_initcall(vas_init);