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