blob: 768c21deb2e55f38337c8b1439e0e5a0b27613d7 [file] [log] [blame]
Bruce Losureff3eb552005-04-25 13:12:02 -07001/*
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>
Bruce Losureff3eb552005-04-25 13:12:02 -070011#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/proc_fs.h>
14#include <linux/device.h>
15#include <linux/delay.h>
Jack Steiner21517a52005-07-07 09:14:00 -070016#include <asm/system.h>
Bruce Losureff3eb552005-04-25 13:12:02 -070017#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>
Bruce Losurece0a3952005-04-25 19:41:00 -070024#include <asm/sn/l1.h>
25#include <asm/sn/module.h>
Bruce Losureff3eb552005-04-25 13:12:02 -070026#include "tio.h"
27#include "xtalk/xwidgetdev.h"
28#include "xtalk/hubdev.h"
29
30#define CX_DEV_NONE 0
31#define DEVICE_NAME "tiocx"
32#define WIDGET_ID 0
33#define TIOCX_DEBUG 0
34
35#if TIOCX_DEBUG
36#define DBG(fmt...) printk(KERN_ALERT fmt)
37#else
38#define DBG(fmt...)
39#endif
40
41struct device_attribute dev_attr_cxdev_control;
42
43/**
44 * tiocx_match - Try to match driver id list with device.
45 * @dev: device pointer
46 * @drv: driver pointer
47 *
48 * Returns 1 if match, 0 otherwise.
49 */
50static int tiocx_match(struct device *dev, struct device_driver *drv)
51{
52 struct cx_dev *cx_dev = to_cx_dev(dev);
53 struct cx_drv *cx_drv = to_cx_driver(drv);
54 const struct cx_device_id *ids = cx_drv->id_table;
55
56 if (!ids)
57 return 0;
58
59 while (ids->part_num) {
60 if (ids->part_num == cx_dev->cx_id.part_num)
61 return 1;
62 ids++;
63 }
64 return 0;
65
66}
67
Paul Jackson6d20b032005-11-25 20:04:26 -080068static int tiocx_uevent(struct device *dev, char **envp, int num_envp,
Bruce Losureff3eb552005-04-25 13:12:02 -070069 char *buffer, int buffer_size)
70{
71 return -ENODEV;
72}
73
74static void tiocx_bus_release(struct device *dev)
75{
76 kfree(to_cx_dev(dev));
77}
78
79struct bus_type tiocx_bus_type = {
80 .name = "tiocx",
81 .match = tiocx_match,
Paul Jackson6d20b032005-11-25 20:04:26 -080082 .uevent = tiocx_uevent,
Bruce Losureff3eb552005-04-25 13:12:02 -070083};
84
85/**
86 * cx_device_match - Find cx_device in the id table.
87 * @ids: id table from driver
88 * @cx_device: part/mfg id for the device
89 *
90 */
91static const struct cx_device_id *cx_device_match(const struct cx_device_id
92 *ids,
93 struct cx_dev *cx_device)
94{
95 /*
96 * NOTES: We may want to check for CX_ANY_ID too.
97 * Do we want to match against nasid too?
98 * CX_DEV_NONE == 0, if the driver tries to register for
99 * part/mfg == 0 we should return no-match (NULL) here.
100 */
101 while (ids->part_num && ids->mfg_num) {
102 if (ids->part_num == cx_device->cx_id.part_num &&
103 ids->mfg_num == cx_device->cx_id.mfg_num)
104 return ids;
105 ids++;
106 }
107
108 return NULL;
109}
110
111/**
112 * cx_device_probe - Look for matching device.
113 * Call driver probe routine if found.
114 * @cx_driver: driver table (cx_drv struct) from driver
115 * @cx_device: part/mfg id for the device
116 */
117static int cx_device_probe(struct device *dev)
118{
119 const struct cx_device_id *id;
120 struct cx_drv *cx_drv = to_cx_driver(dev->driver);
121 struct cx_dev *cx_dev = to_cx_dev(dev);
122 int error = 0;
123
124 if (!cx_dev->driver && cx_drv->probe) {
125 id = cx_device_match(cx_drv->id_table, cx_dev);
126 if (id) {
127 if ((error = cx_drv->probe(cx_dev, id)) < 0)
128 return error;
129 else
130 cx_dev->driver = cx_drv;
131 }
132 }
133
134 return error;
135}
136
137/**
138 * cx_driver_remove - Remove driver from device struct.
139 * @dev: device
140 */
141static int cx_driver_remove(struct device *dev)
142{
143 struct cx_dev *cx_dev = to_cx_dev(dev);
144 struct cx_drv *cx_drv = cx_dev->driver;
145 if (cx_drv->remove)
146 cx_drv->remove(cx_dev);
147 cx_dev->driver = NULL;
148 return 0;
149}
150
151/**
152 * cx_driver_register - Register the driver.
153 * @cx_driver: driver table (cx_drv struct) from driver
154 *
155 * Called from the driver init routine to register a driver.
156 * The cx_drv struct contains the driver name, a pointer to
157 * a table of part/mfg numbers and a pointer to the driver's
158 * probe/attach routine.
159 */
160int cx_driver_register(struct cx_drv *cx_driver)
161{
162 cx_driver->driver.name = cx_driver->name;
163 cx_driver->driver.bus = &tiocx_bus_type;
164 cx_driver->driver.probe = cx_device_probe;
165 cx_driver->driver.remove = cx_driver_remove;
166
167 return driver_register(&cx_driver->driver);
168}
169
170/**
171 * cx_driver_unregister - Unregister the driver.
172 * @cx_driver: driver table (cx_drv struct) from driver
173 */
174int cx_driver_unregister(struct cx_drv *cx_driver)
175{
176 driver_unregister(&cx_driver->driver);
177 return 0;
178}
179
180/**
181 * cx_device_register - Register a device.
182 * @nasid: device's nasid
183 * @part_num: device's part number
184 * @mfg_num: device's manufacturer number
185 * @hubdev: hub info associated with this device
Bruce Losure25732ad2005-09-02 15:16:35 -0500186 * @bt: board type of the device
Bruce Losureff3eb552005-04-25 13:12:02 -0700187 *
188 */
189int
190cx_device_register(nasid_t nasid, int part_num, int mfg_num,
Bruce Losure25732ad2005-09-02 15:16:35 -0500191 struct hubdev_info *hubdev, int bt)
Bruce Losureff3eb552005-04-25 13:12:02 -0700192{
193 struct cx_dev *cx_dev;
194
Pekka Enbergf96cb1f2005-09-06 15:18:31 -0700195 cx_dev = kzalloc(sizeof(struct cx_dev), GFP_KERNEL);
Bruce Losureff3eb552005-04-25 13:12:02 -0700196 DBG("cx_dev= 0x%p\n", cx_dev);
197 if (cx_dev == NULL)
198 return -ENOMEM;
199
200 cx_dev->cx_id.part_num = part_num;
201 cx_dev->cx_id.mfg_num = mfg_num;
202 cx_dev->cx_id.nasid = nasid;
203 cx_dev->hubdev = hubdev;
Bruce Losure25732ad2005-09-02 15:16:35 -0500204 cx_dev->bt = bt;
Bruce Losureff3eb552005-04-25 13:12:02 -0700205
206 cx_dev->dev.parent = NULL;
207 cx_dev->dev.bus = &tiocx_bus_type;
208 cx_dev->dev.release = tiocx_bus_release;
Bruce Losurec1ffb6a2005-05-24 08:30:00 -0700209 snprintf(cx_dev->dev.bus_id, BUS_ID_SIZE, "%d",
210 cx_dev->cx_id.nasid);
Bruce Losureff3eb552005-04-25 13:12:02 -0700211 device_register(&cx_dev->dev);
212 get_device(&cx_dev->dev);
213
214 device_create_file(&cx_dev->dev, &dev_attr_cxdev_control);
215
216 return 0;
217}
218
219/**
220 * cx_device_unregister - Unregister a device.
221 * @cx_dev: part/mfg id for the device
222 */
223int cx_device_unregister(struct cx_dev *cx_dev)
224{
225 put_device(&cx_dev->dev);
226 device_unregister(&cx_dev->dev);
227 return 0;
228}
229
230/**
231 * cx_device_reload - Reload the device.
232 * @nasid: device's nasid
233 * @part_num: device's part number
234 * @mfg_num: device's manufacturer number
235 *
236 * Remove the device associated with 'nasid' from device list and then
237 * call device-register with the given part/mfg numbers.
238 */
239static int cx_device_reload(struct cx_dev *cx_dev)
240{
Bruce Losureff3eb552005-04-25 13:12:02 -0700241 cx_device_unregister(cx_dev);
242 return cx_device_register(cx_dev->cx_id.nasid, cx_dev->cx_id.part_num,
Bruce Losure25732ad2005-09-02 15:16:35 -0500243 cx_dev->cx_id.mfg_num, cx_dev->hubdev,
244 cx_dev->bt);
Bruce Losureff3eb552005-04-25 13:12:02 -0700245}
246
247static inline uint64_t tiocx_intr_alloc(nasid_t nasid, int widget,
248 u64 sn_irq_info,
249 int req_irq, nasid_t req_nasid,
250 int req_slice)
251{
252 struct ia64_sal_retval rv;
253 rv.status = 0;
254 rv.v0 = 0;
255
256 ia64_sal_oemcall_nolock(&rv, SN_SAL_IOIF_INTERRUPT,
257 SAL_INTR_ALLOC, nasid,
258 widget, sn_irq_info, req_irq,
259 req_nasid, req_slice);
260 return rv.status;
261}
262
263static inline void tiocx_intr_free(nasid_t nasid, int widget,
264 struct sn_irq_info *sn_irq_info)
265{
266 struct ia64_sal_retval rv;
267 rv.status = 0;
268 rv.v0 = 0;
269
270 ia64_sal_oemcall_nolock(&rv, SN_SAL_IOIF_INTERRUPT,
271 SAL_INTR_FREE, nasid,
272 widget, sn_irq_info->irq_irq,
273 sn_irq_info->irq_cookie, 0, 0);
274}
275
276struct sn_irq_info *tiocx_irq_alloc(nasid_t nasid, int widget, int irq,
277 nasid_t req_nasid, int slice)
278{
279 struct sn_irq_info *sn_irq_info;
280 int status;
281 int sn_irq_size = sizeof(struct sn_irq_info);
282
283 if ((nasid & 1) == 0)
284 return NULL;
285
286 sn_irq_info = kmalloc(sn_irq_size, GFP_KERNEL);
287 if (sn_irq_info == NULL)
288 return NULL;
289
290 memset(sn_irq_info, 0x0, sn_irq_size);
291
292 status = tiocx_intr_alloc(nasid, widget, __pa(sn_irq_info), irq,
293 req_nasid, slice);
294 if (status) {
295 kfree(sn_irq_info);
296 return NULL;
297 } else {
298 return sn_irq_info;
299 }
300}
301
302void tiocx_irq_free(struct sn_irq_info *sn_irq_info)
303{
304 uint64_t bridge = (uint64_t) sn_irq_info->irq_bridge;
305 nasid_t nasid = NASID_GET(bridge);
306 int widget;
307
308 if (nasid & 1) {
309 widget = TIO_SWIN_WIDGETNUM(bridge);
310 tiocx_intr_free(nasid, widget, sn_irq_info);
311 kfree(sn_irq_info);
312 }
313}
314
Bruce Losurece0a3952005-04-25 19:41:00 -0700315uint64_t tiocx_dma_addr(uint64_t addr)
Bruce Losureff3eb552005-04-25 13:12:02 -0700316{
317 return PHYS_TO_TIODMA(addr);
318}
319
Bruce Losurece0a3952005-04-25 19:41:00 -0700320uint64_t tiocx_swin_base(int nasid)
Bruce Losureff3eb552005-04-25 13:12:02 -0700321{
322 return TIO_SWIN_BASE(nasid, TIOCX_CORELET);
323}
324
325EXPORT_SYMBOL(cx_driver_register);
326EXPORT_SYMBOL(cx_driver_unregister);
327EXPORT_SYMBOL(cx_device_register);
328EXPORT_SYMBOL(cx_device_unregister);
329EXPORT_SYMBOL(tiocx_irq_alloc);
330EXPORT_SYMBOL(tiocx_irq_free);
331EXPORT_SYMBOL(tiocx_bus_type);
332EXPORT_SYMBOL(tiocx_dma_addr);
333EXPORT_SYMBOL(tiocx_swin_base);
334
Bruce Losureff3eb552005-04-25 13:12:02 -0700335static void tio_conveyor_set(nasid_t nasid, int enable_flag)
336{
337 uint64_t ice_frz;
338 uint64_t disable_cb = (1ull << 61);
339
340 if (!(nasid & 1))
341 return;
342
343 ice_frz = REMOTE_HUB_L(nasid, TIO_ICE_FRZ_CFG);
344 if (enable_flag) {
345 if (!(ice_frz & disable_cb)) /* already enabled */
346 return;
347 ice_frz &= ~disable_cb;
348 } else {
349 if (ice_frz & disable_cb) /* already disabled */
350 return;
351 ice_frz |= disable_cb;
352 }
353 DBG(KERN_ALERT "TIO_ICE_FRZ_CFG= 0x%lx\n", ice_frz);
354 REMOTE_HUB_S(nasid, TIO_ICE_FRZ_CFG, ice_frz);
355}
356
357#define tio_conveyor_enable(nasid) tio_conveyor_set(nasid, 1)
358#define tio_conveyor_disable(nasid) tio_conveyor_set(nasid, 0)
359
360static void tio_corelet_reset(nasid_t nasid, int corelet)
361{
362 if (!(nasid & 1))
363 return;
364
365 REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 1 << corelet);
366 udelay(2000);
367 REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 0);
368 udelay(2000);
369}
370
Bruce Losure25732ad2005-09-02 15:16:35 -0500371static int is_fpga_tio(int nasid, int *bt)
Bruce Losurece0a3952005-04-25 19:41:00 -0700372{
Bruce Losure25732ad2005-09-02 15:16:35 -0500373 int ioboard_type;
Bruce Losurece0a3952005-04-25 19:41:00 -0700374
Bruce Losure25732ad2005-09-02 15:16:35 -0500375 ioboard_type = ia64_sn_sysctl_ioboard_get(nasid);
Bruce Losurece0a3952005-04-25 19:41:00 -0700376
Bruce Losure25732ad2005-09-02 15:16:35 -0500377 switch (ioboard_type) {
Bruce Losurece0a3952005-04-25 19:41:00 -0700378 case L1_BRICKTYPE_SA:
379 case L1_BRICKTYPE_ATHENA:
Bruce Losure25732ad2005-09-02 15:16:35 -0500380 case L1_BOARDTYPE_DAYTONA:
381 *bt = ioboard_type;
Bruce Losurece0a3952005-04-25 19:41:00 -0700382 return 1;
383 }
Bruce Losure25732ad2005-09-02 15:16:35 -0500384
Bruce Losurece0a3952005-04-25 19:41:00 -0700385 return 0;
386}
387
388static int bitstream_loaded(nasid_t nasid)
Bruce Losureff3eb552005-04-25 13:12:02 -0700389{
390 uint64_t cx_credits;
391
392 cx_credits = REMOTE_HUB_L(nasid, TIO_ICE_PMI_TX_DYN_CREDIT_STAT_CB3);
393 cx_credits &= TIO_ICE_PMI_TX_DYN_CREDIT_STAT_CB3_CREDIT_CNT_MASK;
394 DBG("cx_credits= 0x%lx\n", cx_credits);
395
396 return (cx_credits == 0xf) ? 1 : 0;
397}
398
399static int tiocx_reload(struct cx_dev *cx_dev)
400{
401 int part_num = CX_DEV_NONE;
402 int mfg_num = CX_DEV_NONE;
403 nasid_t nasid = cx_dev->cx_id.nasid;
404
Bruce Losurece0a3952005-04-25 19:41:00 -0700405 if (bitstream_loaded(nasid)) {
Bruce Losureff3eb552005-04-25 13:12:02 -0700406 uint64_t cx_id;
Bruce Losure25732ad2005-09-02 15:16:35 -0500407 int rv;
Bruce Losureff3eb552005-04-25 13:12:02 -0700408
Bruce Losure25732ad2005-09-02 15:16:35 -0500409 rv = ia64_sn_sysctl_tio_clock_reset(nasid);
410 if (rv) {
411 printk(KERN_ALERT "CX port JTAG reset failed.\n");
412 } else {
413 cx_id = *(volatile uint64_t *)
414 (TIO_SWIN_BASE(nasid, TIOCX_CORELET) +
Bruce Losureff3eb552005-04-25 13:12:02 -0700415 WIDGET_ID);
Bruce Losure25732ad2005-09-02 15:16:35 -0500416 part_num = XWIDGET_PART_NUM(cx_id);
417 mfg_num = XWIDGET_MFG_NUM(cx_id);
418 DBG("part= 0x%x, mfg= 0x%x\n", part_num, mfg_num);
419 /* just ignore it if it's a CE */
420 if (part_num == TIO_CE_ASIC_PARTNUM)
421 return 0;
422 }
Bruce Losureff3eb552005-04-25 13:12:02 -0700423 }
424
425 cx_dev->cx_id.part_num = part_num;
426 cx_dev->cx_id.mfg_num = mfg_num;
427
428 /*
429 * Delete old device and register the new one. It's ok if
430 * part_num/mfg_num == CX_DEV_NONE. We want to register
431 * devices in the table even if a bitstream isn't loaded.
432 * That allows use to see that a bitstream isn't loaded via
433 * TIOCX_IOCTL_DEV_LIST.
434 */
435 return cx_device_reload(cx_dev);
436}
437
Yani Ioannouff381d22005-05-17 06:40:51 -0400438static ssize_t show_cxdev_control(struct device *dev, struct device_attribute *attr, char *buf)
Bruce Losureff3eb552005-04-25 13:12:02 -0700439{
440 struct cx_dev *cx_dev = to_cx_dev(dev);
441
Bruce Losure25732ad2005-09-02 15:16:35 -0500442 return sprintf(buf, "0x%x 0x%x 0x%x 0x%x\n",
Bruce Losureff3eb552005-04-25 13:12:02 -0700443 cx_dev->cx_id.nasid,
Bruce Losurece0a3952005-04-25 19:41:00 -0700444 cx_dev->cx_id.part_num, cx_dev->cx_id.mfg_num,
Bruce Losure25732ad2005-09-02 15:16:35 -0500445 cx_dev->bt);
Bruce Losureff3eb552005-04-25 13:12:02 -0700446}
447
Yani Ioannouff381d22005-05-17 06:40:51 -0400448static ssize_t store_cxdev_control(struct device *dev, struct device_attribute *attr, const char *buf,
Bruce Losureff3eb552005-04-25 13:12:02 -0700449 size_t count)
450{
451 int n;
452 struct cx_dev *cx_dev = to_cx_dev(dev);
453
454 if (!capable(CAP_SYS_ADMIN))
455 return -EPERM;
456
457 if (count <= 0)
458 return 0;
459
460 n = simple_strtoul(buf, NULL, 0);
461
462 switch (n) {
463 case 1:
Bruce Losurec1ffb6a2005-05-24 08:30:00 -0700464 tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET);
465 tiocx_reload(cx_dev);
466 break;
467 case 2:
Bruce Losureff3eb552005-04-25 13:12:02 -0700468 tiocx_reload(cx_dev);
469 break;
470 case 3:
471 tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET);
472 break;
473 default:
474 break;
475 }
476
477 return count;
478}
479
480DEVICE_ATTR(cxdev_control, 0644, show_cxdev_control, store_cxdev_control);
481
482static int __init tiocx_init(void)
483{
484 cnodeid_t cnodeid;
485 int found_tiocx_device = 0;
486
Jack Steiner21517a52005-07-07 09:14:00 -0700487 if (!ia64_platform_is("sn2"))
488 return -ENODEV;
489
Bruce Losureff3eb552005-04-25 13:12:02 -0700490 bus_register(&tiocx_bus_type);
491
Jack Steiner24ee0a62005-09-12 12:15:43 -0500492 for (cnodeid = 0; cnodeid < num_cnodes; cnodeid++) {
Bruce Losureff3eb552005-04-25 13:12:02 -0700493 nasid_t nasid;
Bruce Losure25732ad2005-09-02 15:16:35 -0500494 int bt;
Bruce Losureff3eb552005-04-25 13:12:02 -0700495
Jack Steiner24ee0a62005-09-12 12:15:43 -0500496 nasid = cnodeid_to_nasid(cnodeid);
Bruce Losureff3eb552005-04-25 13:12:02 -0700497
Bruce Losure25732ad2005-09-02 15:16:35 -0500498 if ((nasid & 0x1) && is_fpga_tio(nasid, &bt)) {
Bruce Losureff3eb552005-04-25 13:12:02 -0700499 struct hubdev_info *hubdev;
Bruce Losureff3eb552005-04-25 13:12:02 -0700500 struct xwidget_info *widgetp;
501
502 DBG("Found TIO at nasid 0x%x\n", nasid);
503
504 hubdev =
505 (struct hubdev_info *)(NODEPDA(cnodeid)->pdinfo);
Bruce Losureff3eb552005-04-25 13:12:02 -0700506
507 widgetp = &hubdev->hdi_xwidget_info[TIOCX_CORELET];
508
509 /* The CE hangs off of the CX port but is not an FPGA */
510 if (widgetp->xwi_hwid.part_num == TIO_CE_ASIC_PARTNUM)
511 continue;
512
513 tio_corelet_reset(nasid, TIOCX_CORELET);
514 tio_conveyor_enable(nasid);
515
516 if (cx_device_register
517 (nasid, widgetp->xwi_hwid.part_num,
Bruce Losure25732ad2005-09-02 15:16:35 -0500518 widgetp->xwi_hwid.mfg_num, hubdev, bt) < 0)
Bruce Losureff3eb552005-04-25 13:12:02 -0700519 return -ENXIO;
520 else
521 found_tiocx_device++;
522 }
523 }
524
525 /* It's ok if we find zero devices. */
526 DBG("found_tiocx_device= %d\n", found_tiocx_device);
527
528 return 0;
529}
530
Patrick Mochel66234152005-04-28 17:11:52 -0700531static int cx_remove_device(struct device * dev, void * data)
532{
533 struct cx_dev *cx_dev = to_cx_dev(dev);
534 device_remove_file(dev, &dev_attr_cxdev_control);
535 cx_device_unregister(cx_dev);
536 return 0;
537}
538
Bruce Losureff3eb552005-04-25 13:12:02 -0700539static void __exit tiocx_exit(void)
540{
Bruce Losureff3eb552005-04-25 13:12:02 -0700541 DBG("tiocx_exit\n");
542
543 /*
544 * Unregister devices.
545 */
Patrick Mochel66234152005-04-28 17:11:52 -0700546 bus_for_each_dev(&tiocx_bus_type, NULL, NULL, cx_remove_device);
Bruce Losureff3eb552005-04-25 13:12:02 -0700547 bus_unregister(&tiocx_bus_type);
548}
549
Bruce Losurec1ffb6a2005-05-24 08:30:00 -0700550subsys_initcall(tiocx_init);
Bruce Losureff3eb552005-04-25 13:12:02 -0700551module_exit(tiocx_exit);
552
553/************************************************************************
554 * Module licensing and description
555 ************************************************************************/
556MODULE_LICENSE("GPL");
557MODULE_AUTHOR("Bruce Losure <blosure@sgi.com>");
558MODULE_DESCRIPTION("TIOCX module");
559MODULE_SUPPORTED_DEVICE(DEVICE_NAME);