aboutsummaryrefslogtreecommitdiffstats
path: root/arch/sparc64/kernel/pci_sun4v.c
diff options
context:
space:
mode:
authorDavid S. Miller <davem@sunset.davemloft.net>2007-02-10 20:41:02 -0500
committerDavid S. Miller <davem@sunset.davemloft.net>2007-02-11 02:50:37 -0500
commit35a17eb6a87c9ceb0d35dcb51f464fe6faf584ab (patch)
tree7f56095a56e9f62dca7514cdfe781739548011f5 /arch/sparc64/kernel/pci_sun4v.c
parent68c921869491c119142612fa5796c9f8b4e9970b (diff)
[SPARC64]: Add PCI MSI support on Niagara.
This is kind of hokey, we could use the hardware provided facilities much better. MSIs are assosciated with MSI Queues. MSI Queues generate interrupts when any MSI assosciated with it is signalled. This suggests a two-tiered IRQ dispatch scheme: MSI Queue interrupt --> queue interrupt handler MSI dispatch --> driver interrupt handler But we just get one-level under Linux currently. What I'd like to do is possibly stick the IRQ actions into a per-MSI-Queue data structure, and dispatch them form there, but the generic IRQ layer doesn't provide a way to do that right now. So, the current kludge is to "ACK" the interrupt by processing the MSI Queue data structures and ACK'ing them, then we run the actual handler like normal. We are wasting a lot of useful information, for example the MSI data and address are provided with ever MSI, as well as a system tick if available. If we could pass this into the IRQ handler it could help with certain things, in particular for PCI-Express error messages. The MSI entries on sparc64 also tell you exactly which bus/device/fn sent the MSI, which would be great for error handling when no registered IRQ handler can service the interrupt. We override the disable/enable IRQ chip methods in sun4v_msi, so we have to call {mask,unmask}_msi_irq() directly from there. This is another ugly wart. Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'arch/sparc64/kernel/pci_sun4v.c')
-rw-r--r--arch/sparc64/kernel/pci_sun4v.c444
1 files changed, 444 insertions, 0 deletions
diff --git a/arch/sparc64/kernel/pci_sun4v.c b/arch/sparc64/kernel/pci_sun4v.c
index 6b04794b7a97..ec22cd61ec8c 100644
--- a/arch/sparc64/kernel/pci_sun4v.c
+++ b/arch/sparc64/kernel/pci_sun4v.c
@@ -10,6 +10,8 @@
10#include <linux/slab.h> 10#include <linux/slab.h>
11#include <linux/interrupt.h> 11#include <linux/interrupt.h>
12#include <linux/percpu.h> 12#include <linux/percpu.h>
13#include <linux/irq.h>
14#include <linux/msi.h>
13 15
14#include <asm/pbm.h> 16#include <asm/pbm.h>
15#include <asm/iommu.h> 17#include <asm/iommu.h>
@@ -1074,6 +1076,443 @@ static void pci_sun4v_get_bus_range(struct pci_pbm_info *pbm)
1074 1076
1075} 1077}
1076 1078
1079#ifdef CONFIG_PCI_MSI
1080struct pci_sun4v_msiq_entry {
1081 u64 version_type;
1082#define MSIQ_VERSION_MASK 0xffffffff00000000UL
1083#define MSIQ_VERSION_SHIFT 32
1084#define MSIQ_TYPE_MASK 0x00000000000000ffUL
1085#define MSIQ_TYPE_SHIFT 0
1086#define MSIQ_TYPE_NONE 0x00
1087#define MSIQ_TYPE_MSG 0x01
1088#define MSIQ_TYPE_MSI32 0x02
1089#define MSIQ_TYPE_MSI64 0x03
1090#define MSIQ_TYPE_INTX 0x08
1091#define MSIQ_TYPE_NONE2 0xff
1092
1093 u64 intx_sysino;
1094 u64 reserved1;
1095 u64 stick;
1096 u64 req_id; /* bus/device/func */
1097#define MSIQ_REQID_BUS_MASK 0xff00UL
1098#define MSIQ_REQID_BUS_SHIFT 8
1099#define MSIQ_REQID_DEVICE_MASK 0x00f8UL
1100#define MSIQ_REQID_DEVICE_SHIFT 3
1101#define MSIQ_REQID_FUNC_MASK 0x0007UL
1102#define MSIQ_REQID_FUNC_SHIFT 0
1103
1104 u64 msi_address;
1105
1106 /* The format of this value is message type dependant.
1107 * For MSI bits 15:0 are the data from the MSI packet.
1108 * For MSI-X bits 31:0 are the data from the MSI packet.
1109 * For MSG, the message code and message routing code where:
1110 * bits 39:32 is the bus/device/fn of the msg target-id
1111 * bits 18:16 is the message routing code
1112 * bits 7:0 is the message code
1113 * For INTx the low order 2-bits are:
1114 * 00 - INTA
1115 * 01 - INTB
1116 * 10 - INTC
1117 * 11 - INTD
1118 */
1119 u64 msi_data;
1120
1121 u64 reserved2;
1122};
1123
1124/* For now this just runs as a pre-handler for the real interrupt handler.
1125 * So we just walk through the queue and ACK all the entries, update the
1126 * head pointer, and return.
1127 *
1128 * In the longer term it would be nice to do something more integrated
1129 * wherein we can pass in some of this MSI info to the drivers. This
1130 * would be most useful for PCIe fabric error messages, although we could
1131 * invoke those directly from the loop here in order to pass the info around.
1132 */
1133static void pci_sun4v_msi_prehandler(unsigned int ino, void *data1, void *data2)
1134{
1135 struct pci_pbm_info *pbm = data1;
1136 struct pci_sun4v_msiq_entry *base, *ep;
1137 unsigned long msiqid, orig_head, head, type, err;
1138
1139 msiqid = (unsigned long) data2;
1140
1141 head = 0xdeadbeef;
1142 err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, &head);
1143 if (unlikely(err))
1144 goto hv_error_get;
1145
1146 if (unlikely(head >= (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry))))
1147 goto bad_offset;
1148
1149 head /= sizeof(struct pci_sun4v_msiq_entry);
1150 orig_head = head;
1151 base = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
1152 (pbm->msiq_ent_count *
1153 sizeof(struct pci_sun4v_msiq_entry))));
1154 ep = &base[head];
1155 while ((ep->version_type & MSIQ_TYPE_MASK) != 0) {
1156 type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
1157 if (unlikely(type != MSIQ_TYPE_MSI32 &&
1158 type != MSIQ_TYPE_MSI64))
1159 goto bad_type;
1160
1161 pci_sun4v_msi_setstate(pbm->devhandle,
1162 ep->msi_data /* msi_num */,
1163 HV_MSISTATE_IDLE);
1164
1165 /* Clear the entry. */
1166 ep->version_type &= ~MSIQ_TYPE_MASK;
1167
1168 /* Go to next entry in ring. */
1169 head++;
1170 if (head >= pbm->msiq_ent_count)
1171 head = 0;
1172 ep = &base[head];
1173 }
1174
1175 if (likely(head != orig_head)) {
1176 /* ACK entries by updating head pointer. */
1177 head *= sizeof(struct pci_sun4v_msiq_entry);
1178 err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
1179 if (unlikely(err))
1180 goto hv_error_set;
1181 }
1182 return;
1183
1184hv_error_set:
1185 printk(KERN_EMERG "MSI: Hypervisor set head gives error %lu\n", err);
1186 goto hv_error_cont;
1187
1188hv_error_get:
1189 printk(KERN_EMERG "MSI: Hypervisor get head gives error %lu\n", err);
1190
1191hv_error_cont:
1192 printk(KERN_EMERG "MSI: devhandle[%x] msiqid[%lx] head[%lu]\n",
1193 pbm->devhandle, msiqid, head);
1194 return;
1195
1196bad_offset:
1197 printk(KERN_EMERG "MSI: Hypervisor gives bad offset %lx max(%lx)\n",
1198 head, pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry));
1199 return;
1200
1201bad_type:
1202 printk(KERN_EMERG "MSI: Entry has bad type %lx\n", type);
1203 return;
1204}
1205
1206static int msi_bitmap_alloc(struct pci_pbm_info *pbm)
1207{
1208 unsigned long size, bits_per_ulong;
1209
1210 bits_per_ulong = sizeof(unsigned long) * 8;
1211 size = (pbm->msi_num + (bits_per_ulong - 1)) & ~(bits_per_ulong - 1);
1212 size /= 8;
1213 BUG_ON(size % sizeof(unsigned long));
1214
1215 pbm->msi_bitmap = kzalloc(size, GFP_KERNEL);
1216 if (!pbm->msi_bitmap)
1217 return -ENOMEM;
1218
1219 return 0;
1220}
1221
1222static void msi_bitmap_free(struct pci_pbm_info *pbm)
1223{
1224 kfree(pbm->msi_bitmap);
1225 pbm->msi_bitmap = NULL;
1226}
1227
1228static int msi_queue_alloc(struct pci_pbm_info *pbm)
1229{
1230 unsigned long q_size, alloc_size, pages, order;
1231 int i;
1232
1233 q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
1234 alloc_size = (pbm->msiq_num * q_size);
1235 order = get_order(alloc_size);
1236 pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
1237 if (pages == 0UL) {
1238 printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
1239 order);
1240 return -ENOMEM;
1241 }
1242 memset((char *)pages, 0, PAGE_SIZE << order);
1243 pbm->msi_queues = (void *) pages;
1244
1245 for (i = 0; i < pbm->msiq_num; i++) {
1246 unsigned long err, base = __pa(pages + (i * q_size));
1247 unsigned long ret1, ret2;
1248
1249 err = pci_sun4v_msiq_conf(pbm->devhandle,
1250 pbm->msiq_first + i,
1251 base, pbm->msiq_ent_count);
1252 if (err) {
1253 printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
1254 err);
1255 goto h_error;
1256 }
1257
1258 err = pci_sun4v_msiq_info(pbm->devhandle,
1259 pbm->msiq_first + i,
1260 &ret1, &ret2);
1261 if (err) {
1262 printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
1263 err);
1264 goto h_error;
1265 }
1266 if (ret1 != base || ret2 != pbm->msiq_ent_count) {
1267 printk(KERN_ERR "MSI: Bogus qconf "
1268 "expected[%lx:%x] got[%lx:%lx]\n",
1269 base, pbm->msiq_ent_count,
1270 ret1, ret2);
1271 goto h_error;
1272 }
1273 }
1274
1275 return 0;
1276
1277h_error:
1278 free_pages(pages, order);
1279 return -EINVAL;
1280}
1281
1282static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
1283{
1284 u32 *val;
1285 int len;
1286
1287 val = of_get_property(pbm->prom_node, "#msi-eqs", &len);
1288 if (!val || len != 4)
1289 goto no_msi;
1290 pbm->msiq_num = *val;
1291 if (pbm->msiq_num) {
1292 struct msiq_prop {
1293 u32 first_msiq;
1294 u32 num_msiq;
1295 u32 first_devino;
1296 } *mqp;
1297 struct msi_range_prop {
1298 u32 first_msi;
1299 u32 num_msi;
1300 } *mrng;
1301 struct addr_range_prop {
1302 u32 msi32_high;
1303 u32 msi32_low;
1304 u32 msi32_len;
1305 u32 msi64_high;
1306 u32 msi64_low;
1307 u32 msi64_len;
1308 } *arng;
1309
1310 val = of_get_property(pbm->prom_node, "msi-eq-size", &len);
1311 if (!val || len != 4)
1312 goto no_msi;
1313
1314 pbm->msiq_ent_count = *val;
1315
1316 mqp = of_get_property(pbm->prom_node,
1317 "msi-eq-to-devino", &len);
1318 if (!mqp || len != sizeof(struct msiq_prop))
1319 goto no_msi;
1320
1321 pbm->msiq_first = mqp->first_msiq;
1322 pbm->msiq_first_devino = mqp->first_devino;
1323
1324 val = of_get_property(pbm->prom_node, "#msi", &len);
1325 if (!val || len != 4)
1326 goto no_msi;
1327 pbm->msi_num = *val;
1328
1329 mrng = of_get_property(pbm->prom_node, "msi-ranges", &len);
1330 if (!mrng || len != sizeof(struct msi_range_prop))
1331 goto no_msi;
1332 pbm->msi_first = mrng->first_msi;
1333
1334 val = of_get_property(pbm->prom_node, "msi-data-mask", &len);
1335 if (!val || len != 4)
1336 goto no_msi;
1337 pbm->msi_data_mask = *val;
1338
1339 val = of_get_property(pbm->prom_node, "msix-data-width", &len);
1340 if (!val || len != 4)
1341 goto no_msi;
1342 pbm->msix_data_width = *val;
1343
1344 arng = of_get_property(pbm->prom_node, "msi-address-ranges",
1345 &len);
1346 if (!arng || len != sizeof(struct addr_range_prop))
1347 goto no_msi;
1348 pbm->msi32_start = ((u64)arng->msi32_high << 32) |
1349 (u64) arng->msi32_low;
1350 pbm->msi64_start = ((u64)arng->msi64_high << 32) |
1351 (u64) arng->msi64_low;
1352 pbm->msi32_len = arng->msi32_len;
1353 pbm->msi64_len = arng->msi64_len;
1354
1355 if (msi_bitmap_alloc(pbm))
1356 goto no_msi;
1357
1358 if (msi_queue_alloc(pbm)) {
1359 msi_bitmap_free(pbm);
1360 goto no_msi;
1361 }
1362
1363 printk(KERN_INFO "%s: MSI Queue first[%u] num[%u] count[%u] "
1364 "devino[0x%x]\n",
1365 pbm->name,
1366 pbm->msiq_first, pbm->msiq_num,
1367 pbm->msiq_ent_count,
1368 pbm->msiq_first_devino);
1369 printk(KERN_INFO "%s: MSI first[%u] num[%u] mask[0x%x] "
1370 "width[%u]\n",
1371 pbm->name,
1372 pbm->msi_first, pbm->msi_num, pbm->msi_data_mask,
1373 pbm->msix_data_width);
1374 printk(KERN_INFO "%s: MSI addr32[0x%lx:0x%x] "
1375 "addr64[0x%lx:0x%x]\n",
1376 pbm->name,
1377 pbm->msi32_start, pbm->msi32_len,
1378 pbm->msi64_start, pbm->msi64_len);
1379 printk(KERN_INFO "%s: MSI queues at RA [%p]\n",
1380 pbm->name,
1381 pbm->msi_queues);
1382 }
1383
1384 return;
1385
1386no_msi:
1387 pbm->msiq_num = 0;
1388 printk(KERN_INFO "%s: No MSI support.\n", pbm->name);
1389}
1390
1391static int alloc_msi(struct pci_pbm_info *pbm)
1392{
1393 int i;
1394
1395 for (i = 0; i < pbm->msi_num; i++) {
1396 if (!test_and_set_bit(i, pbm->msi_bitmap))
1397 return i + pbm->msi_first;
1398 }
1399
1400 return -ENOENT;
1401}
1402
1403static void free_msi(struct pci_pbm_info *pbm, int msi_num)
1404{
1405 msi_num -= pbm->msi_first;
1406 clear_bit(msi_num, pbm->msi_bitmap);
1407}
1408
1409static int pci_sun4v_setup_msi_irq(unsigned int *virt_irq_p,
1410 struct pci_dev *pdev,
1411 struct msi_desc *entry)
1412{
1413 struct pcidev_cookie *pcp = pdev->sysdata;
1414 struct pci_pbm_info *pbm = pcp->pbm;
1415 unsigned long devino, msiqid;
1416 struct msi_msg msg;
1417 int msi_num, err;
1418
1419 *virt_irq_p = 0;
1420
1421 msi_num = alloc_msi(pbm);
1422 if (msi_num < 0)
1423 return msi_num;
1424
1425 devino = sun4v_build_msi(pbm->devhandle, virt_irq_p,
1426 pbm->msiq_first_devino,
1427 (pbm->msiq_first_devino +
1428 pbm->msiq_num));
1429 err = -ENOMEM;
1430 if (!devino)
1431 goto out_err;
1432
1433 set_irq_msi(*virt_irq_p, entry);
1434
1435 msiqid = ((devino - pbm->msiq_first_devino) +
1436 pbm->msiq_first);
1437
1438 err = -EINVAL;
1439 if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
1440 if (err)
1441 goto out_err;
1442
1443 if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
1444 goto out_err;
1445
1446 if (pci_sun4v_msi_setmsiq(pbm->devhandle,
1447 msi_num, msiqid,
1448 (entry->msi_attrib.is_64 ?
1449 HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
1450 goto out_err;
1451
1452 if (pci_sun4v_msi_setstate(pbm->devhandle, msi_num, HV_MSISTATE_IDLE))
1453 goto out_err;
1454
1455 if (pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_VALID))
1456 goto out_err;
1457
1458 pcp->msi_num = msi_num;
1459
1460 if (entry->msi_attrib.is_64) {
1461 msg.address_hi = pbm->msi64_start >> 32;
1462 msg.address_lo = pbm->msi64_start & 0xffffffff;
1463 } else {
1464 msg.address_hi = 0;
1465 msg.address_lo = pbm->msi32_start;
1466 }
1467 msg.data = msi_num;
1468 write_msi_msg(*virt_irq_p, &msg);
1469
1470 irq_install_pre_handler(*virt_irq_p,
1471 pci_sun4v_msi_prehandler,
1472 pbm, (void *) msiqid);
1473
1474 return 0;
1475
1476out_err:
1477 free_msi(pbm, msi_num);
1478 sun4v_destroy_msi(*virt_irq_p);
1479 *virt_irq_p = 0;
1480 return err;
1481
1482}
1483
1484static void pci_sun4v_teardown_msi_irq(unsigned int virt_irq,
1485 struct pci_dev *pdev)
1486{
1487 struct pcidev_cookie *pcp = pdev->sysdata;
1488 struct pci_pbm_info *pbm = pcp->pbm;
1489 unsigned long msiqid, err;
1490 unsigned int msi_num;
1491
1492 msi_num = pcp->msi_num;
1493 err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi_num, &msiqid);
1494 if (err) {
1495 printk(KERN_ERR "%s: getmsiq gives error %lu\n",
1496 pbm->name, err);
1497 return;
1498 }
1499
1500 pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_INVALID);
1501 pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_INVALID);
1502
1503 free_msi(pbm, msi_num);
1504
1505 /* The sun4v_destroy_msi() will liberate the devino and thus the MSIQ
1506 * allocation.
1507 */
1508 sun4v_destroy_msi(virt_irq);
1509}
1510#else /* CONFIG_PCI_MSI */
1511static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
1512{
1513}
1514#endif /* !(CONFIG_PCI_MSI) */
1515
1077static void pci_sun4v_pbm_init(struct pci_controller_info *p, struct device_node *dp, u32 devhandle) 1516static void pci_sun4v_pbm_init(struct pci_controller_info *p, struct device_node *dp, u32 devhandle)
1078{ 1517{
1079 struct pci_pbm_info *pbm; 1518 struct pci_pbm_info *pbm;
@@ -1119,6 +1558,7 @@ static void pci_sun4v_pbm_init(struct pci_controller_info *p, struct device_node
1119 1558
1120 pci_sun4v_get_bus_range(pbm); 1559 pci_sun4v_get_bus_range(pbm);
1121 pci_sun4v_iommu_init(pbm); 1560 pci_sun4v_iommu_init(pbm);
1561 pci_sun4v_msi_init(pbm);
1122 1562
1123 pdev_htab_populate(pbm); 1563 pdev_htab_populate(pbm);
1124} 1564}
@@ -1187,6 +1627,10 @@ void sun4v_pci_init(struct device_node *dp, char *model_name)
1187 p->scan_bus = pci_sun4v_scan_bus; 1627 p->scan_bus = pci_sun4v_scan_bus;
1188 p->base_address_update = pci_sun4v_base_address_update; 1628 p->base_address_update = pci_sun4v_base_address_update;
1189 p->resource_adjust = pci_sun4v_resource_adjust; 1629 p->resource_adjust = pci_sun4v_resource_adjust;
1630#ifdef CONFIG_PCI_MSI
1631 p->setup_msi_irq = pci_sun4v_setup_msi_irq;
1632 p->teardown_msi_irq = pci_sun4v_teardown_msi_irq;
1633#endif
1190 p->pci_ops = &pci_sun4v_ops; 1634 p->pci_ops = &pci_sun4v_ops;
1191 1635
1192 /* Like PSYCHO and SCHIZO we have a 2GB aligned area 1636 /* Like PSYCHO and SCHIZO we have a 2GB aligned area