Bruce Losure | ff3eb55 | 2005-04-25 13:12:02 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (c) 2005 Silicon Graphics, Inc. All rights reserved. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/version.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | #include <linux/proc_fs.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <asm/uaccess.h> |
| 18 | #include <asm/sn/sn_sal.h> |
| 19 | #include <asm/sn/addrs.h> |
| 20 | #include <asm/sn/io.h> |
| 21 | #include <asm/sn/types.h> |
| 22 | #include <asm/sn/shubio.h> |
| 23 | #include <asm/sn/tiocx.h> |
| 24 | #include "tio.h" |
| 25 | #include "xtalk/xwidgetdev.h" |
| 26 | #include "xtalk/hubdev.h" |
| 27 | |
| 28 | #define CX_DEV_NONE 0 |
| 29 | #define DEVICE_NAME "tiocx" |
| 30 | #define WIDGET_ID 0 |
| 31 | #define TIOCX_DEBUG 0 |
| 32 | |
| 33 | #if TIOCX_DEBUG |
| 34 | #define DBG(fmt...) printk(KERN_ALERT fmt) |
| 35 | #else |
| 36 | #define DBG(fmt...) |
| 37 | #endif |
| 38 | |
| 39 | struct device_attribute dev_attr_cxdev_control; |
| 40 | |
| 41 | /** |
| 42 | * tiocx_match - Try to match driver id list with device. |
| 43 | * @dev: device pointer |
| 44 | * @drv: driver pointer |
| 45 | * |
| 46 | * Returns 1 if match, 0 otherwise. |
| 47 | */ |
| 48 | static int tiocx_match(struct device *dev, struct device_driver *drv) |
| 49 | { |
| 50 | struct cx_dev *cx_dev = to_cx_dev(dev); |
| 51 | struct cx_drv *cx_drv = to_cx_driver(drv); |
| 52 | const struct cx_device_id *ids = cx_drv->id_table; |
| 53 | |
| 54 | if (!ids) |
| 55 | return 0; |
| 56 | |
| 57 | while (ids->part_num) { |
| 58 | if (ids->part_num == cx_dev->cx_id.part_num) |
| 59 | return 1; |
| 60 | ids++; |
| 61 | } |
| 62 | return 0; |
| 63 | |
| 64 | } |
| 65 | |
| 66 | static int tiocx_hotplug(struct device *dev, char **envp, int num_envp, |
| 67 | char *buffer, int buffer_size) |
| 68 | { |
| 69 | return -ENODEV; |
| 70 | } |
| 71 | |
| 72 | static void tiocx_bus_release(struct device *dev) |
| 73 | { |
| 74 | kfree(to_cx_dev(dev)); |
| 75 | } |
| 76 | |
| 77 | struct bus_type tiocx_bus_type = { |
| 78 | .name = "tiocx", |
| 79 | .match = tiocx_match, |
| 80 | .hotplug = tiocx_hotplug, |
| 81 | }; |
| 82 | |
| 83 | /** |
| 84 | * cx_device_match - Find cx_device in the id table. |
| 85 | * @ids: id table from driver |
| 86 | * @cx_device: part/mfg id for the device |
| 87 | * |
| 88 | */ |
| 89 | static const struct cx_device_id *cx_device_match(const struct cx_device_id |
| 90 | *ids, |
| 91 | struct cx_dev *cx_device) |
| 92 | { |
| 93 | /* |
| 94 | * NOTES: We may want to check for CX_ANY_ID too. |
| 95 | * Do we want to match against nasid too? |
| 96 | * CX_DEV_NONE == 0, if the driver tries to register for |
| 97 | * part/mfg == 0 we should return no-match (NULL) here. |
| 98 | */ |
| 99 | while (ids->part_num && ids->mfg_num) { |
| 100 | if (ids->part_num == cx_device->cx_id.part_num && |
| 101 | ids->mfg_num == cx_device->cx_id.mfg_num) |
| 102 | return ids; |
| 103 | ids++; |
| 104 | } |
| 105 | |
| 106 | return NULL; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * cx_device_probe - Look for matching device. |
| 111 | * Call driver probe routine if found. |
| 112 | * @cx_driver: driver table (cx_drv struct) from driver |
| 113 | * @cx_device: part/mfg id for the device |
| 114 | */ |
| 115 | static int cx_device_probe(struct device *dev) |
| 116 | { |
| 117 | const struct cx_device_id *id; |
| 118 | struct cx_drv *cx_drv = to_cx_driver(dev->driver); |
| 119 | struct cx_dev *cx_dev = to_cx_dev(dev); |
| 120 | int error = 0; |
| 121 | |
| 122 | if (!cx_dev->driver && cx_drv->probe) { |
| 123 | id = cx_device_match(cx_drv->id_table, cx_dev); |
| 124 | if (id) { |
| 125 | if ((error = cx_drv->probe(cx_dev, id)) < 0) |
| 126 | return error; |
| 127 | else |
| 128 | cx_dev->driver = cx_drv; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return error; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * cx_driver_remove - Remove driver from device struct. |
| 137 | * @dev: device |
| 138 | */ |
| 139 | static int cx_driver_remove(struct device *dev) |
| 140 | { |
| 141 | struct cx_dev *cx_dev = to_cx_dev(dev); |
| 142 | struct cx_drv *cx_drv = cx_dev->driver; |
| 143 | if (cx_drv->remove) |
| 144 | cx_drv->remove(cx_dev); |
| 145 | cx_dev->driver = NULL; |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * cx_driver_register - Register the driver. |
| 151 | * @cx_driver: driver table (cx_drv struct) from driver |
| 152 | * |
| 153 | * Called from the driver init routine to register a driver. |
| 154 | * The cx_drv struct contains the driver name, a pointer to |
| 155 | * a table of part/mfg numbers and a pointer to the driver's |
| 156 | * probe/attach routine. |
| 157 | */ |
| 158 | int cx_driver_register(struct cx_drv *cx_driver) |
| 159 | { |
| 160 | cx_driver->driver.name = cx_driver->name; |
| 161 | cx_driver->driver.bus = &tiocx_bus_type; |
| 162 | cx_driver->driver.probe = cx_device_probe; |
| 163 | cx_driver->driver.remove = cx_driver_remove; |
| 164 | |
| 165 | return driver_register(&cx_driver->driver); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * cx_driver_unregister - Unregister the driver. |
| 170 | * @cx_driver: driver table (cx_drv struct) from driver |
| 171 | */ |
| 172 | int cx_driver_unregister(struct cx_drv *cx_driver) |
| 173 | { |
| 174 | driver_unregister(&cx_driver->driver); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * cx_device_register - Register a device. |
| 180 | * @nasid: device's nasid |
| 181 | * @part_num: device's part number |
| 182 | * @mfg_num: device's manufacturer number |
| 183 | * @hubdev: hub info associated with this device |
| 184 | * |
| 185 | */ |
| 186 | int |
| 187 | cx_device_register(nasid_t nasid, int part_num, int mfg_num, |
| 188 | struct hubdev_info *hubdev) |
| 189 | { |
| 190 | struct cx_dev *cx_dev; |
| 191 | |
| 192 | cx_dev = kcalloc(1, sizeof(struct cx_dev), GFP_KERNEL); |
| 193 | DBG("cx_dev= 0x%p\n", cx_dev); |
| 194 | if (cx_dev == NULL) |
| 195 | return -ENOMEM; |
| 196 | |
| 197 | cx_dev->cx_id.part_num = part_num; |
| 198 | cx_dev->cx_id.mfg_num = mfg_num; |
| 199 | cx_dev->cx_id.nasid = nasid; |
| 200 | cx_dev->hubdev = hubdev; |
| 201 | |
| 202 | cx_dev->dev.parent = NULL; |
| 203 | cx_dev->dev.bus = &tiocx_bus_type; |
| 204 | cx_dev->dev.release = tiocx_bus_release; |
| 205 | snprintf(cx_dev->dev.bus_id, BUS_ID_SIZE, "%d.0x%x", |
| 206 | cx_dev->cx_id.nasid, cx_dev->cx_id.part_num); |
| 207 | device_register(&cx_dev->dev); |
| 208 | get_device(&cx_dev->dev); |
| 209 | |
| 210 | device_create_file(&cx_dev->dev, &dev_attr_cxdev_control); |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * cx_device_unregister - Unregister a device. |
| 217 | * @cx_dev: part/mfg id for the device |
| 218 | */ |
| 219 | int cx_device_unregister(struct cx_dev *cx_dev) |
| 220 | { |
| 221 | put_device(&cx_dev->dev); |
| 222 | device_unregister(&cx_dev->dev); |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * cx_device_reload - Reload the device. |
| 228 | * @nasid: device's nasid |
| 229 | * @part_num: device's part number |
| 230 | * @mfg_num: device's manufacturer number |
| 231 | * |
| 232 | * Remove the device associated with 'nasid' from device list and then |
| 233 | * call device-register with the given part/mfg numbers. |
| 234 | */ |
| 235 | static int cx_device_reload(struct cx_dev *cx_dev) |
| 236 | { |
| 237 | device_remove_file(&cx_dev->dev, &dev_attr_cxdev_control); |
| 238 | cx_device_unregister(cx_dev); |
| 239 | return cx_device_register(cx_dev->cx_id.nasid, cx_dev->cx_id.part_num, |
| 240 | cx_dev->cx_id.mfg_num, cx_dev->hubdev); |
| 241 | } |
| 242 | |
| 243 | static inline uint64_t tiocx_intr_alloc(nasid_t nasid, int widget, |
| 244 | u64 sn_irq_info, |
| 245 | int req_irq, nasid_t req_nasid, |
| 246 | int req_slice) |
| 247 | { |
| 248 | struct ia64_sal_retval rv; |
| 249 | rv.status = 0; |
| 250 | rv.v0 = 0; |
| 251 | |
| 252 | ia64_sal_oemcall_nolock(&rv, SN_SAL_IOIF_INTERRUPT, |
| 253 | SAL_INTR_ALLOC, nasid, |
| 254 | widget, sn_irq_info, req_irq, |
| 255 | req_nasid, req_slice); |
| 256 | return rv.status; |
| 257 | } |
| 258 | |
| 259 | static inline void tiocx_intr_free(nasid_t nasid, int widget, |
| 260 | struct sn_irq_info *sn_irq_info) |
| 261 | { |
| 262 | struct ia64_sal_retval rv; |
| 263 | rv.status = 0; |
| 264 | rv.v0 = 0; |
| 265 | |
| 266 | ia64_sal_oemcall_nolock(&rv, SN_SAL_IOIF_INTERRUPT, |
| 267 | SAL_INTR_FREE, nasid, |
| 268 | widget, sn_irq_info->irq_irq, |
| 269 | sn_irq_info->irq_cookie, 0, 0); |
| 270 | } |
| 271 | |
| 272 | struct sn_irq_info *tiocx_irq_alloc(nasid_t nasid, int widget, int irq, |
| 273 | nasid_t req_nasid, int slice) |
| 274 | { |
| 275 | struct sn_irq_info *sn_irq_info; |
| 276 | int status; |
| 277 | int sn_irq_size = sizeof(struct sn_irq_info); |
| 278 | |
| 279 | if ((nasid & 1) == 0) |
| 280 | return NULL; |
| 281 | |
| 282 | sn_irq_info = kmalloc(sn_irq_size, GFP_KERNEL); |
| 283 | if (sn_irq_info == NULL) |
| 284 | return NULL; |
| 285 | |
| 286 | memset(sn_irq_info, 0x0, sn_irq_size); |
| 287 | |
| 288 | status = tiocx_intr_alloc(nasid, widget, __pa(sn_irq_info), irq, |
| 289 | req_nasid, slice); |
| 290 | if (status) { |
| 291 | kfree(sn_irq_info); |
| 292 | return NULL; |
| 293 | } else { |
| 294 | return sn_irq_info; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void tiocx_irq_free(struct sn_irq_info *sn_irq_info) |
| 299 | { |
| 300 | uint64_t bridge = (uint64_t) sn_irq_info->irq_bridge; |
| 301 | nasid_t nasid = NASID_GET(bridge); |
| 302 | int widget; |
| 303 | |
| 304 | if (nasid & 1) { |
| 305 | widget = TIO_SWIN_WIDGETNUM(bridge); |
| 306 | tiocx_intr_free(nasid, widget, sn_irq_info); |
| 307 | kfree(sn_irq_info); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | uint64_t |
| 312 | tiocx_dma_addr(uint64_t addr) |
| 313 | { |
| 314 | return PHYS_TO_TIODMA(addr); |
| 315 | } |
| 316 | |
| 317 | uint64_t |
| 318 | tiocx_swin_base(int nasid) |
| 319 | { |
| 320 | return TIO_SWIN_BASE(nasid, TIOCX_CORELET); |
| 321 | } |
| 322 | |
| 323 | EXPORT_SYMBOL(cx_driver_register); |
| 324 | EXPORT_SYMBOL(cx_driver_unregister); |
| 325 | EXPORT_SYMBOL(cx_device_register); |
| 326 | EXPORT_SYMBOL(cx_device_unregister); |
| 327 | EXPORT_SYMBOL(tiocx_irq_alloc); |
| 328 | EXPORT_SYMBOL(tiocx_irq_free); |
| 329 | EXPORT_SYMBOL(tiocx_bus_type); |
| 330 | EXPORT_SYMBOL(tiocx_dma_addr); |
| 331 | EXPORT_SYMBOL(tiocx_swin_base); |
| 332 | |
| 333 | static uint64_t tiocx_get_hubdev_info(u64 handle, u64 address) |
| 334 | { |
| 335 | |
| 336 | struct ia64_sal_retval ret_stuff; |
| 337 | ret_stuff.status = 0; |
| 338 | ret_stuff.v0 = 0; |
| 339 | |
| 340 | ia64_sal_oemcall_nolock(&ret_stuff, |
| 341 | SN_SAL_IOIF_GET_HUBDEV_INFO, |
| 342 | handle, address, 0, 0, 0, 0, 0); |
| 343 | return ret_stuff.v0; |
| 344 | } |
| 345 | |
| 346 | static void tio_conveyor_set(nasid_t nasid, int enable_flag) |
| 347 | { |
| 348 | uint64_t ice_frz; |
| 349 | uint64_t disable_cb = (1ull << 61); |
| 350 | |
| 351 | if (!(nasid & 1)) |
| 352 | return; |
| 353 | |
| 354 | ice_frz = REMOTE_HUB_L(nasid, TIO_ICE_FRZ_CFG); |
| 355 | if (enable_flag) { |
| 356 | if (!(ice_frz & disable_cb)) /* already enabled */ |
| 357 | return; |
| 358 | ice_frz &= ~disable_cb; |
| 359 | } else { |
| 360 | if (ice_frz & disable_cb) /* already disabled */ |
| 361 | return; |
| 362 | ice_frz |= disable_cb; |
| 363 | } |
| 364 | DBG(KERN_ALERT "TIO_ICE_FRZ_CFG= 0x%lx\n", ice_frz); |
| 365 | REMOTE_HUB_S(nasid, TIO_ICE_FRZ_CFG, ice_frz); |
| 366 | } |
| 367 | |
| 368 | #define tio_conveyor_enable(nasid) tio_conveyor_set(nasid, 1) |
| 369 | #define tio_conveyor_disable(nasid) tio_conveyor_set(nasid, 0) |
| 370 | |
| 371 | static void tio_corelet_reset(nasid_t nasid, int corelet) |
| 372 | { |
| 373 | if (!(nasid & 1)) |
| 374 | return; |
| 375 | |
| 376 | REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 1 << corelet); |
| 377 | udelay(2000); |
| 378 | REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 0); |
| 379 | udelay(2000); |
| 380 | } |
| 381 | |
| 382 | static int fpga_attached(nasid_t nasid) |
| 383 | { |
| 384 | uint64_t cx_credits; |
| 385 | |
| 386 | cx_credits = REMOTE_HUB_L(nasid, TIO_ICE_PMI_TX_DYN_CREDIT_STAT_CB3); |
| 387 | cx_credits &= TIO_ICE_PMI_TX_DYN_CREDIT_STAT_CB3_CREDIT_CNT_MASK; |
| 388 | DBG("cx_credits= 0x%lx\n", cx_credits); |
| 389 | |
| 390 | return (cx_credits == 0xf) ? 1 : 0; |
| 391 | } |
| 392 | |
| 393 | static int tiocx_reload(struct cx_dev *cx_dev) |
| 394 | { |
| 395 | int part_num = CX_DEV_NONE; |
| 396 | int mfg_num = CX_DEV_NONE; |
| 397 | nasid_t nasid = cx_dev->cx_id.nasid; |
| 398 | |
| 399 | if (fpga_attached(nasid)) { |
| 400 | uint64_t cx_id; |
| 401 | |
| 402 | cx_id = |
| 403 | *(volatile int32_t *)(TIO_SWIN_BASE(nasid, TIOCX_CORELET) + |
| 404 | WIDGET_ID); |
| 405 | part_num = XWIDGET_PART_NUM(cx_id); |
| 406 | mfg_num = XWIDGET_MFG_NUM(cx_id); |
| 407 | DBG("part= 0x%x, mfg= 0x%x\n", part_num, mfg_num); |
| 408 | /* just ignore it if it's a CE */ |
| 409 | if (part_num == TIO_CE_ASIC_PARTNUM) |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | cx_dev->cx_id.part_num = part_num; |
| 414 | cx_dev->cx_id.mfg_num = mfg_num; |
| 415 | |
| 416 | /* |
| 417 | * Delete old device and register the new one. It's ok if |
| 418 | * part_num/mfg_num == CX_DEV_NONE. We want to register |
| 419 | * devices in the table even if a bitstream isn't loaded. |
| 420 | * That allows use to see that a bitstream isn't loaded via |
| 421 | * TIOCX_IOCTL_DEV_LIST. |
| 422 | */ |
| 423 | return cx_device_reload(cx_dev); |
| 424 | } |
| 425 | |
| 426 | static ssize_t show_cxdev_control(struct device *dev, char *buf) |
| 427 | { |
| 428 | struct cx_dev *cx_dev = to_cx_dev(dev); |
| 429 | |
| 430 | return sprintf(buf, "0x%x 0x%x 0x%x\n", |
| 431 | cx_dev->cx_id.nasid, |
| 432 | cx_dev->cx_id.part_num, cx_dev->cx_id.mfg_num); |
| 433 | } |
| 434 | |
| 435 | static ssize_t store_cxdev_control(struct device *dev, const char *buf, |
| 436 | size_t count) |
| 437 | { |
| 438 | int n; |
| 439 | struct cx_dev *cx_dev = to_cx_dev(dev); |
| 440 | |
| 441 | if (!capable(CAP_SYS_ADMIN)) |
| 442 | return -EPERM; |
| 443 | |
| 444 | if (count <= 0) |
| 445 | return 0; |
| 446 | |
| 447 | n = simple_strtoul(buf, NULL, 0); |
| 448 | |
| 449 | switch (n) { |
| 450 | case 1: |
| 451 | tiocx_reload(cx_dev); |
| 452 | break; |
| 453 | case 3: |
| 454 | tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET); |
| 455 | break; |
| 456 | default: |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | return count; |
| 461 | } |
| 462 | |
| 463 | DEVICE_ATTR(cxdev_control, 0644, show_cxdev_control, store_cxdev_control); |
| 464 | |
| 465 | static int __init tiocx_init(void) |
| 466 | { |
| 467 | cnodeid_t cnodeid; |
| 468 | int found_tiocx_device = 0; |
| 469 | |
| 470 | bus_register(&tiocx_bus_type); |
| 471 | |
| 472 | for (cnodeid = 0; cnodeid < MAX_COMPACT_NODES; cnodeid++) { |
| 473 | nasid_t nasid; |
| 474 | |
| 475 | if ((nasid = cnodeid_to_nasid(cnodeid)) < 0) |
| 476 | break; /* No more nasids .. bail out of loop */ |
| 477 | |
| 478 | if (nasid & 0x1) { /* TIO's are always odd */ |
| 479 | struct hubdev_info *hubdev; |
| 480 | uint64_t status; |
| 481 | struct xwidget_info *widgetp; |
| 482 | |
| 483 | DBG("Found TIO at nasid 0x%x\n", nasid); |
| 484 | |
| 485 | hubdev = |
| 486 | (struct hubdev_info *)(NODEPDA(cnodeid)->pdinfo); |
| 487 | status = |
| 488 | tiocx_get_hubdev_info(nasid, |
| 489 | (uint64_t) __pa(hubdev)); |
| 490 | if (status) |
| 491 | continue; |
| 492 | |
| 493 | widgetp = &hubdev->hdi_xwidget_info[TIOCX_CORELET]; |
| 494 | |
| 495 | /* The CE hangs off of the CX port but is not an FPGA */ |
| 496 | if (widgetp->xwi_hwid.part_num == TIO_CE_ASIC_PARTNUM) |
| 497 | continue; |
| 498 | |
| 499 | tio_corelet_reset(nasid, TIOCX_CORELET); |
| 500 | tio_conveyor_enable(nasid); |
| 501 | |
| 502 | if (cx_device_register |
| 503 | (nasid, widgetp->xwi_hwid.part_num, |
| 504 | widgetp->xwi_hwid.mfg_num, hubdev) < 0) |
| 505 | return -ENXIO; |
| 506 | else |
| 507 | found_tiocx_device++; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /* It's ok if we find zero devices. */ |
| 512 | DBG("found_tiocx_device= %d\n", found_tiocx_device); |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static void __exit tiocx_exit(void) |
| 518 | { |
| 519 | struct device *dev; |
| 520 | struct device *tdev; |
| 521 | |
| 522 | DBG("tiocx_exit\n"); |
| 523 | |
| 524 | /* |
| 525 | * Unregister devices. |
| 526 | */ |
| 527 | list_for_each_entry_safe(dev, tdev, &tiocx_bus_type.devices.list, |
| 528 | bus_list) { |
| 529 | if (dev) { |
| 530 | struct cx_dev *cx_dev = to_cx_dev(dev); |
| 531 | device_remove_file(dev, &dev_attr_cxdev_control); |
| 532 | cx_device_unregister(cx_dev); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | bus_unregister(&tiocx_bus_type); |
| 537 | } |
| 538 | |
| 539 | module_init(tiocx_init); |
| 540 | module_exit(tiocx_exit); |
| 541 | |
| 542 | /************************************************************************ |
| 543 | * Module licensing and description |
| 544 | ************************************************************************/ |
| 545 | MODULE_LICENSE("GPL"); |
| 546 | MODULE_AUTHOR("Bruce Losure <blosure@sgi.com>"); |
| 547 | MODULE_DESCRIPTION("TIOCX module"); |
| 548 | MODULE_SUPPORTED_DEVICE(DEVICE_NAME); |