1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2016-17 IBM Corp.
4  */
5 
6 #define pr_fmt(fmt) "vas: " fmt
7 
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>
21 
22 #include "vas.h"
23 
24 DEFINE_MUTEX(vas_mutex);
25 static LIST_HEAD(vas_instances);
26 
27 static DEFINE_PER_CPU(int, cpu_vas_id);
28 
29 static int vas_irq_fault_window_setup(struct vas_instance *vinst)
30 {
31 	char devname[64];
32 	int rc = 0;
33 
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);
37 
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;
42 	}
43 
44 	rc = vas_setup_fault_window(vinst);
45 	if (rc)
46 		free_irq(vinst->virq, vinst);
47 
48 out:
49 	return rc;
50 }
51 
52 static int init_vas_instance(struct platform_device *pdev)
53 {
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;
60 
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;
65 	}
66 
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;
71 	}
72 
73 	if (pdev->num_resources != 4) {
74 		pr_err("Unexpected DT configuration for [%s, %d]\n",
75 				pdev->name, vasid);
76 		return -ENODEV;
77 	}
78 
79 	vinst = kzalloc(sizeof(*vinst), GFP_KERNEL);
80 	if (!vinst)
81 		return -ENOMEM;
82 
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;
88 
89 	res = &pdev->resource[0];
90 	vinst->hvwc_bar_start = res->start;
91 
92 	res = &pdev->resource[1];
93 	vinst->uwc_bar_start = res->start;
94 
95 	res = &pdev->resource[2];
96 	vinst->paste_base_addr = res->start;
97 
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;
102 	}
103 
104 	vinst->paste_win_id_shift = 63 - res->end;
105 
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;
111 	}
112 
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;
118 	}
119 
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;
125 	}
126 
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);
132 
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;
136 	}
137 
138 	mutex_lock(&vas_mutex);
139 	list_add(&vinst->node, &vas_instances);
140 	mutex_unlock(&vas_mutex);
141 
142 	spin_lock_init(&vinst->fault_lock);
143 	/*
144 	 * IRQ and fault handling setup is needed only for user space
145 	 * send windows.
146 	 */
147 	if (vinst->virq) {
148 		rc = vas_irq_fault_window_setup(vinst);
149 		/*
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.
153 		 */
154 		if (rc)
155 			vinst->virq = 0;
156 	}
157 
158 	vas_instance_init_dbgdir(vinst);
159 
160 	dev_set_drvdata(&pdev->dev, vinst);
161 
162 	return 0;
163 
164 free_vinst:
165 	kfree(vinst);
166 	return -ENODEV;
167 
168 }
169 
170 /*
171  * Although this is read/used multiple times, it is written to only
172  * during initialization.
173  */
174 struct vas_instance *find_vas_instance(int vasid)
175 {
176 	struct list_head *ent;
177 	struct vas_instance *vinst;
178 
179 	mutex_lock(&vas_mutex);
180 
181 	if (vasid == -1)
182 		vasid = per_cpu(cpu_vas_id, smp_processor_id());
183 
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;
189 		}
190 	}
191 	mutex_unlock(&vas_mutex);
192 
193 	pr_devel("Instance %d not found\n", vasid);
194 	return NULL;
195 }
196 
197 int chip_to_vas_id(int chipid)
198 {
199 	int cpu;
200 
201 	for_each_possible_cpu(cpu) {
202 		if (cpu_to_chip_id(cpu) == chipid)
203 			return per_cpu(cpu_vas_id, cpu);
204 	}
205 	return -1;
206 }
207 EXPORT_SYMBOL(chip_to_vas_id);
208 
209 static int vas_probe(struct platform_device *pdev)
210 {
211 	return init_vas_instance(pdev);
212 }
213 
214 static const struct of_device_id powernv_vas_match[] = {
215 	{ .compatible = "ibm,vas",},
216 	{},
217 };
218 
219 static struct platform_driver vas_driver = {
220 	.driver = {
221 		.name = "vas",
222 		.of_match_table = powernv_vas_match,
223 	},
224 	.probe = vas_probe,
225 };
226 
227 static int __init vas_init(void)
228 {
229 	int found = 0;
230 	struct device_node *dn;
231 
232 	platform_driver_register(&vas_driver);
233 
234 	for_each_compatible_node(dn, NULL, "ibm,vas") {
235 		of_platform_device_create(dn, NULL, NULL);
236 		found++;
237 	}
238 
239 	if (!found) {
240 		platform_driver_unregister(&vas_driver);
241 		return -ENODEV;
242 	}
243 
244 	pr_devel("Found %d instances\n", found);
245 
246 	return 0;
247 }
248 device_initcall(vas_init);
249