blob: 420524e6f8c95c6118462cefb80723312d213437 [file] [log] [blame]
Michael Ellerman85f2bf92007-05-08 12:58:35 +10001/*
2 * Copyright 2006 Jake Moilanen <moilanen@austin.ibm.com>, IBM Corp.
3 * Copyright 2006-2007 Michael Ellerman, IBM Corp.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; version 2 of the
8 * License.
9 *
10 */
11
12#include <linux/device.h>
13#include <linux/irq.h>
14#include <linux/msi.h>
15
16#include <asm/rtas.h>
17#include <asm/hw_irq.h>
18#include <asm/ppc-pci.h>
19
20static int query_token, change_token;
21
22#define RTAS_QUERY_FN 0
23#define RTAS_CHANGE_FN 1
24#define RTAS_RESET_FN 2
25#define RTAS_CHANGE_MSI_FN 3
26#define RTAS_CHANGE_MSIX_FN 4
Brian Kinge61133d2013-05-03 11:30:59 +000027#define RTAS_CHANGE_32MSI_FN 5
Michael Ellerman85f2bf92007-05-08 12:58:35 +100028
29static struct pci_dn *get_pdn(struct pci_dev *pdev)
30{
31 struct device_node *dn;
32 struct pci_dn *pdn;
33
34 dn = pci_device_to_OF_node(pdev);
35 if (!dn) {
36 dev_dbg(&pdev->dev, "rtas_msi: No OF device node\n");
37 return NULL;
38 }
39
40 pdn = PCI_DN(dn);
41 if (!pdn) {
42 dev_dbg(&pdev->dev, "rtas_msi: No PCI DN\n");
43 return NULL;
44 }
45
46 return pdn;
47}
48
49/* RTAS Helpers */
50
51static int rtas_change_msi(struct pci_dn *pdn, u32 func, u32 num_irqs)
52{
53 u32 addr, seq_num, rtas_ret[3];
54 unsigned long buid;
55 int rc;
56
57 addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
58 buid = pdn->phb->buid;
59
60 seq_num = 1;
61 do {
Brian Kinge61133d2013-05-03 11:30:59 +000062 if (func == RTAS_CHANGE_MSI_FN || func == RTAS_CHANGE_MSIX_FN ||
63 func == RTAS_CHANGE_32MSI_FN)
Michael Ellerman85f2bf92007-05-08 12:58:35 +100064 rc = rtas_call(change_token, 6, 4, rtas_ret, addr,
65 BUID_HI(buid), BUID_LO(buid),
66 func, num_irqs, seq_num);
67 else
68 rc = rtas_call(change_token, 6, 3, rtas_ret, addr,
69 BUID_HI(buid), BUID_LO(buid),
70 func, num_irqs, seq_num);
71
72 seq_num = rtas_ret[1];
73 } while (rtas_busy_delay(rc));
74
Michael Ellermand3853662007-09-20 16:36:50 +100075 /*
Michael Ellerman6071ed02009-01-22 20:54:33 +000076 * If the RTAS call succeeded, return the number of irqs allocated.
77 * If not, make sure we return a negative error code.
Michael Ellermand3853662007-09-20 16:36:50 +100078 */
Michael Ellerman6071ed02009-01-22 20:54:33 +000079 if (rc == 0)
80 rc = rtas_ret[0];
81 else if (rc > 0)
82 rc = -rc;
Michael Ellerman85f2bf92007-05-08 12:58:35 +100083
Michael Ellermand3853662007-09-20 16:36:50 +100084 pr_debug("rtas_msi: ibm,change_msi(func=%d,num=%d), got %d rc = %d\n",
85 func, num_irqs, rtas_ret[0], rc);
Michael Ellerman85f2bf92007-05-08 12:58:35 +100086
87 return rc;
88}
89
90static void rtas_disable_msi(struct pci_dev *pdev)
91{
92 struct pci_dn *pdn;
93
94 pdn = get_pdn(pdev);
95 if (!pdn)
96 return;
97
Nishanth Aravamudan964a2992011-03-03 15:41:02 +000098 /*
99 * disabling MSI with the explicit interface also disables MSI-X
100 */
101 if (rtas_change_msi(pdn, RTAS_CHANGE_MSI_FN, 0) != 0) {
102 /*
103 * may have failed because explicit interface is not
104 * present
105 */
106 if (rtas_change_msi(pdn, RTAS_CHANGE_FN, 0) != 0) {
107 pr_debug("rtas_msi: Setting MSIs to 0 failed!\n");
108 }
109 }
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000110}
111
112static int rtas_query_irq_number(struct pci_dn *pdn, int offset)
113{
114 u32 addr, rtas_ret[2];
115 unsigned long buid;
116 int rc;
117
118 addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
119 buid = pdn->phb->buid;
120
121 do {
122 rc = rtas_call(query_token, 4, 3, rtas_ret, addr,
123 BUID_HI(buid), BUID_LO(buid), offset);
124 } while (rtas_busy_delay(rc));
125
126 if (rc) {
127 pr_debug("rtas_msi: error (%d) querying source number\n", rc);
128 return rc;
129 }
130
131 return rtas_ret[0];
132}
133
134static void rtas_teardown_msi_irqs(struct pci_dev *pdev)
135{
136 struct msi_desc *entry;
137
138 list_for_each_entry(entry, &pdev->msi_list, list) {
139 if (entry->irq == NO_IRQ)
140 continue;
141
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100142 irq_set_msi_desc(entry->irq, NULL);
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000143 irq_dispose_mapping(entry->irq);
144 }
145
146 rtas_disable_msi(pdev);
147}
148
Michael Ellerman3a51c0c2009-01-22 20:54:31 +0000149static int check_req(struct pci_dev *pdev, int nvec, char *prop_name)
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000150{
151 struct device_node *dn;
152 struct pci_dn *pdn;
153 const u32 *req_msi;
154
155 pdn = get_pdn(pdev);
156 if (!pdn)
157 return -ENODEV;
158
159 dn = pdn->node;
160
Michael Ellerman3a51c0c2009-01-22 20:54:31 +0000161 req_msi = of_get_property(dn, prop_name, NULL);
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000162 if (!req_msi) {
Michael Ellerman3a51c0c2009-01-22 20:54:31 +0000163 pr_debug("rtas_msi: No %s on %s\n", prop_name, dn->full_name);
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000164 return -ENOENT;
165 }
166
167 if (*req_msi < nvec) {
Michael Ellerman3a51c0c2009-01-22 20:54:31 +0000168 pr_debug("rtas_msi: %s requests < %d MSIs\n", prop_name, nvec);
Michael Ellermand523cc32009-02-17 00:18:49 +0000169
170 if (*req_msi == 0) /* Be paranoid */
171 return -ENOSPC;
172
173 return *req_msi;
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000174 }
175
176 return 0;
177}
178
Michael Ellerman3a51c0c2009-01-22 20:54:31 +0000179static int check_req_msi(struct pci_dev *pdev, int nvec)
180{
181 return check_req(pdev, nvec, "ibm,req#msi");
182}
183
184static int check_req_msix(struct pci_dev *pdev, int nvec)
185{
186 return check_req(pdev, nvec, "ibm,req#msi-x");
187}
188
Michael Ellerman448e2ca2009-02-17 00:21:56 +0000189/* Quota calculation */
190
191static struct device_node *find_pe_total_msi(struct pci_dev *dev, int *total)
192{
193 struct device_node *dn;
194 const u32 *p;
195
196 dn = of_node_get(pci_device_to_OF_node(dev));
197 while (dn) {
198 p = of_get_property(dn, "ibm,pe-total-#msi", NULL);
199 if (p) {
200 pr_debug("rtas_msi: found prop on dn %s\n",
201 dn->full_name);
202 *total = *p;
203 return dn;
204 }
205
206 dn = of_get_next_parent(dn);
207 }
208
209 return NULL;
210}
211
212static struct device_node *find_pe_dn(struct pci_dev *dev, int *total)
213{
214 struct device_node *dn;
Gavin Shan66523d92012-09-07 22:44:13 +0000215 struct eeh_dev *edev;
Michael Ellerman448e2ca2009-02-17 00:21:56 +0000216
217 /* Found our PE and assume 8 at that point. */
218
219 dn = pci_device_to_OF_node(dev);
220 if (!dn)
221 return NULL;
222
Gavin Shan66523d92012-09-07 22:44:13 +0000223 /* Get the top level device in the PE */
224 edev = of_node_to_eeh_dev(dn);
Alexey Kardashevskiybb461882012-11-23 13:25:39 +1100225 if (edev->pe)
226 edev = list_first_entry(&edev->pe->edevs, struct eeh_dev, list);
Gavin Shan66523d92012-09-07 22:44:13 +0000227 dn = eeh_dev_to_of_node(edev);
Michael Ellerman448e2ca2009-02-17 00:21:56 +0000228 if (!dn)
229 return NULL;
230
231 /* We actually want the parent */
232 dn = of_get_parent(dn);
233 if (!dn)
234 return NULL;
235
236 /* Hardcode of 8 for old firmwares */
237 *total = 8;
238 pr_debug("rtas_msi: using PE dn %s\n", dn->full_name);
239
240 return dn;
241}
242
243struct msi_counts {
244 struct device_node *requestor;
245 int num_devices;
246 int request;
247 int quota;
248 int spare;
249 int over_quota;
250};
251
252static void *count_non_bridge_devices(struct device_node *dn, void *data)
253{
254 struct msi_counts *counts = data;
255 const u32 *p;
256 u32 class;
257
258 pr_debug("rtas_msi: counting %s\n", dn->full_name);
259
260 p = of_get_property(dn, "class-code", NULL);
261 class = p ? *p : 0;
262
263 if ((class >> 8) != PCI_CLASS_BRIDGE_PCI)
264 counts->num_devices++;
265
266 return NULL;
267}
268
269static void *count_spare_msis(struct device_node *dn, void *data)
270{
271 struct msi_counts *counts = data;
272 const u32 *p;
273 int req;
274
275 if (dn == counts->requestor)
276 req = counts->request;
277 else {
278 /* We don't know if a driver will try to use MSI or MSI-X,
279 * so we just have to punt and use the larger of the two. */
280 req = 0;
281 p = of_get_property(dn, "ibm,req#msi", NULL);
282 if (p)
283 req = *p;
284
285 p = of_get_property(dn, "ibm,req#msi-x", NULL);
286 if (p)
287 req = max(req, (int)*p);
288 }
289
290 if (req < counts->quota)
291 counts->spare += counts->quota - req;
292 else if (req > counts->quota)
293 counts->over_quota++;
294
295 return NULL;
296}
297
298static int msi_quota_for_device(struct pci_dev *dev, int request)
299{
300 struct device_node *pe_dn;
301 struct msi_counts counts;
302 int total;
303
304 pr_debug("rtas_msi: calc quota for %s, request %d\n", pci_name(dev),
305 request);
306
307 pe_dn = find_pe_total_msi(dev, &total);
308 if (!pe_dn)
309 pe_dn = find_pe_dn(dev, &total);
310
311 if (!pe_dn) {
312 pr_err("rtas_msi: couldn't find PE for %s\n", pci_name(dev));
313 goto out;
314 }
315
316 pr_debug("rtas_msi: found PE %s\n", pe_dn->full_name);
317
318 memset(&counts, 0, sizeof(struct msi_counts));
319
320 /* Work out how many devices we have below this PE */
321 traverse_pci_devices(pe_dn, count_non_bridge_devices, &counts);
322
323 if (counts.num_devices == 0) {
324 pr_err("rtas_msi: found 0 devices under PE for %s\n",
325 pci_name(dev));
326 goto out;
327 }
328
329 counts.quota = total / counts.num_devices;
330 if (request <= counts.quota)
331 goto out;
332
333 /* else, we have some more calculating to do */
334 counts.requestor = pci_device_to_OF_node(dev);
335 counts.request = request;
336 traverse_pci_devices(pe_dn, count_spare_msis, &counts);
337
338 /* If the quota isn't an integer multiple of the total, we can
339 * use the remainder as spare MSIs for anyone that wants them. */
340 counts.spare += total % counts.num_devices;
341
342 /* Divide any spare by the number of over-quota requestors */
343 if (counts.over_quota)
344 counts.quota += counts.spare / counts.over_quota;
345
346 /* And finally clamp the request to the possibly adjusted quota */
347 request = min(counts.quota, request);
348
349 pr_debug("rtas_msi: request clamped to quota %d\n", request);
350out:
351 of_node_put(pe_dn);
352
353 return request;
354}
355
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000356static int rtas_msi_check_device(struct pci_dev *pdev, int nvec, int type)
357{
Michael Ellerman448e2ca2009-02-17 00:21:56 +0000358 int quota, rc;
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000359
Michael Ellerman448e2ca2009-02-17 00:21:56 +0000360 if (type == PCI_CAP_ID_MSIX)
361 rc = check_req_msix(pdev, nvec);
362 else
363 rc = check_req_msi(pdev, nvec);
364
365 if (rc)
366 return rc;
367
368 quota = msi_quota_for_device(pdev, nvec);
369
370 if (quota && quota < nvec)
371 return quota;
372
373 return 0;
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000374}
375
Michael Ellerman94afa5a2009-03-05 14:44:26 +0000376static int check_msix_entries(struct pci_dev *pdev)
377{
378 struct msi_desc *entry;
379 int expected;
380
381 /* There's no way for us to express to firmware that we want
382 * a discontiguous, or non-zero based, range of MSI-X entries.
383 * So we must reject such requests. */
384
385 expected = 0;
386 list_for_each_entry(entry, &pdev->msi_list, list) {
387 if (entry->msi_attrib.entry_nr != expected) {
388 pr_debug("rtas_msi: bad MSI-X entries.\n");
389 return -EINVAL;
390 }
391 expected++;
392 }
393
394 return 0;
395}
396
Anton Blanchard752f5212012-06-04 16:47:03 +0000397static int rtas_setup_msi_irqs(struct pci_dev *pdev, int nvec_in, int type)
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000398{
399 struct pci_dn *pdn;
400 int hwirq, virq, i, rc;
401 struct msi_desc *entry;
Michael Ellerman1db3e892007-10-23 14:23:44 +1000402 struct msi_msg msg;
Anton Blanchard752f5212012-06-04 16:47:03 +0000403 int nvec = nvec_in;
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000404
405 pdn = get_pdn(pdev);
406 if (!pdn)
407 return -ENODEV;
408
Michael Ellerman94afa5a2009-03-05 14:44:26 +0000409 if (type == PCI_CAP_ID_MSIX && check_msix_entries(pdev))
410 return -EINVAL;
411
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000412 /*
Anton Blanchard752f5212012-06-04 16:47:03 +0000413 * Firmware currently refuse any non power of two allocation
414 * so we round up if the quota will allow it.
415 */
416 if (type == PCI_CAP_ID_MSIX) {
417 int m = roundup_pow_of_two(nvec);
418 int quota = msi_quota_for_device(pdev, m);
419
420 if (quota >= m)
421 nvec = m;
422 }
423
424 /*
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000425 * Try the new more explicit firmware interface, if that fails fall
426 * back to the old interface. The old interface is known to never
427 * return MSI-Xs.
428 */
Anton Blanchard752f5212012-06-04 16:47:03 +0000429again:
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000430 if (type == PCI_CAP_ID_MSI) {
Brian Kinge61133d2013-05-03 11:30:59 +0000431 if (pdn->force_32bit_msi)
432 rc = rtas_change_msi(pdn, RTAS_CHANGE_32MSI_FN, nvec);
433 else
434 rc = rtas_change_msi(pdn, RTAS_CHANGE_MSI_FN, nvec);
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000435
Brian Kinge61133d2013-05-03 11:30:59 +0000436 if (rc < 0 && !pdn->force_32bit_msi) {
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000437 pr_debug("rtas_msi: trying the old firmware call.\n");
438 rc = rtas_change_msi(pdn, RTAS_CHANGE_FN, nvec);
439 }
440 } else
441 rc = rtas_change_msi(pdn, RTAS_CHANGE_MSIX_FN, nvec);
442
Michael Ellerman6071ed02009-01-22 20:54:33 +0000443 if (rc != nvec) {
Anton Blanchard752f5212012-06-04 16:47:03 +0000444 if (nvec != nvec_in) {
445 nvec = nvec_in;
446 goto again;
447 }
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000448 pr_debug("rtas_msi: rtas_change_msi() failed\n");
Michael Ellermanfcbe8092007-09-20 16:36:48 +1000449 return rc;
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000450 }
451
452 i = 0;
453 list_for_each_entry(entry, &pdev->msi_list, list) {
Michael Ellermane27ed692009-01-22 20:54:31 +0000454 hwirq = rtas_query_irq_number(pdn, i++);
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000455 if (hwirq < 0) {
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000456 pr_debug("rtas_msi: error (%d) getting hwirq\n", rc);
Michael Ellermanfcbe8092007-09-20 16:36:48 +1000457 return hwirq;
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000458 }
459
460 virq = irq_create_mapping(NULL, hwirq);
461
462 if (virq == NO_IRQ) {
463 pr_debug("rtas_msi: Failed mapping hwirq %d\n", hwirq);
Michael Ellermanfcbe8092007-09-20 16:36:48 +1000464 return -ENOSPC;
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000465 }
466
467 dev_dbg(&pdev->dev, "rtas_msi: allocated virq %d\n", virq);
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100468 irq_set_msi_desc(virq, entry);
Michael Ellerman1db3e892007-10-23 14:23:44 +1000469
470 /* Read config space back so we can restore after reset */
471 read_msi_msg(virq, &msg);
472 entry->msg = msg;
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000473 }
474
475 return 0;
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000476}
477
478static void rtas_msi_pci_irq_fixup(struct pci_dev *pdev)
479{
480 /* No LSI -> leave MSIs (if any) configured */
481 if (pdev->irq == NO_IRQ) {
482 dev_dbg(&pdev->dev, "rtas_msi: no LSI, nothing to do.\n");
483 return;
484 }
485
486 /* No MSI -> MSIs can't have been assigned by fw, leave LSI */
Michael Ellerman649781f2009-01-22 20:54:32 +0000487 if (check_req_msi(pdev, 1) && check_req_msix(pdev, 1)) {
488 dev_dbg(&pdev->dev, "rtas_msi: no req#msi/x, nothing to do.\n");
Michael Ellerman85f2bf92007-05-08 12:58:35 +1000489 return;
490 }
491
492 dev_dbg(&pdev->dev, "rtas_msi: disabling existing MSI.\n");
493 rtas_disable_msi(pdev);
494}
495
496static int rtas_msi_init(void)
497{
498 query_token = rtas_token("ibm,query-interrupt-source-number");
499 change_token = rtas_token("ibm,change-msi");
500
501 if ((query_token == RTAS_UNKNOWN_SERVICE) ||
502 (change_token == RTAS_UNKNOWN_SERVICE)) {
503 pr_debug("rtas_msi: no RTAS tokens, no MSI support.\n");
504 return -1;
505 }
506
507 pr_debug("rtas_msi: Registering RTAS MSI callbacks.\n");
508
509 WARN_ON(ppc_md.setup_msi_irqs);
510 ppc_md.setup_msi_irqs = rtas_setup_msi_irqs;
511 ppc_md.teardown_msi_irqs = rtas_teardown_msi_irqs;
512 ppc_md.msi_check_device = rtas_msi_check_device;
513
514 WARN_ON(ppc_md.pci_irq_fixup);
515 ppc_md.pci_irq_fixup = rtas_msi_pci_irq_fixup;
516
517 return 0;
518}
519arch_initcall(rtas_msi_init);
Brian Kinge61133d2013-05-03 11:30:59 +0000520
521static void quirk_radeon(struct pci_dev *dev)
522{
523 struct pci_dn *pdn = get_pdn(dev);
524
525 if (pdn)
526 pdn->force_32bit_msi = 1;
527}
528DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x68f2, quirk_radeon);
529DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0xaa68, quirk_radeon);