Merge /spare/repo/linux-2.6/
[linux-2.6] / arch / ia64 / sn / kernel / tiocx.c
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/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/proc_fs.h>
14 #include <linux/device.h>
15 #include <linux/delay.h>
16 #include <asm/system.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 <asm/sn/l1.h>
25 #include <asm/sn/module.h>
26 #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
41 struct 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  */
50 static 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
68 static int tiocx_hotplug(struct device *dev, char **envp, int num_envp,
69                          char *buffer, int buffer_size)
70 {
71         return -ENODEV;
72 }
73
74 static void tiocx_bus_release(struct device *dev)
75 {
76         kfree(to_cx_dev(dev));
77 }
78
79 struct bus_type tiocx_bus_type = {
80         .name = "tiocx",
81         .match = tiocx_match,
82         .hotplug = tiocx_hotplug,
83 };
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  */
91 static 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  */
117 static 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  */
141 static 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  */
160 int 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  */
174 int 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
186  *
187  */
188 int
189 cx_device_register(nasid_t nasid, int part_num, int mfg_num,
190                    struct hubdev_info *hubdev)
191 {
192         struct cx_dev *cx_dev;
193
194         cx_dev = kcalloc(1, sizeof(struct cx_dev), GFP_KERNEL);
195         DBG("cx_dev= 0x%p\n", cx_dev);
196         if (cx_dev == NULL)
197                 return -ENOMEM;
198
199         cx_dev->cx_id.part_num = part_num;
200         cx_dev->cx_id.mfg_num = mfg_num;
201         cx_dev->cx_id.nasid = nasid;
202         cx_dev->hubdev = hubdev;
203
204         cx_dev->dev.parent = NULL;
205         cx_dev->dev.bus = &tiocx_bus_type;
206         cx_dev->dev.release = tiocx_bus_release;
207         snprintf(cx_dev->dev.bus_id, BUS_ID_SIZE, "%d",
208                  cx_dev->cx_id.nasid);
209         device_register(&cx_dev->dev);
210         get_device(&cx_dev->dev);
211
212         device_create_file(&cx_dev->dev, &dev_attr_cxdev_control);
213
214         return 0;
215 }
216
217 /**
218  * cx_device_unregister - Unregister a device.
219  * @cx_dev: part/mfg id for the device
220  */
221 int cx_device_unregister(struct cx_dev *cx_dev)
222 {
223         put_device(&cx_dev->dev);
224         device_unregister(&cx_dev->dev);
225         return 0;
226 }
227
228 /**
229  * cx_device_reload - Reload the device.
230  * @nasid: device's nasid
231  * @part_num: device's part number
232  * @mfg_num: device's manufacturer number
233  *
234  * Remove the device associated with 'nasid' from device list and then
235  * call device-register with the given part/mfg numbers.
236  */
237 static int cx_device_reload(struct cx_dev *cx_dev)
238 {
239         cx_device_unregister(cx_dev);
240         return cx_device_register(cx_dev->cx_id.nasid, cx_dev->cx_id.part_num,
241                                   cx_dev->cx_id.mfg_num, cx_dev->hubdev);
242 }
243
244 static inline uint64_t tiocx_intr_alloc(nasid_t nasid, int widget,
245                                         u64 sn_irq_info,
246                                         int req_irq, nasid_t req_nasid,
247                                         int req_slice)
248 {
249         struct ia64_sal_retval rv;
250         rv.status = 0;
251         rv.v0 = 0;
252
253         ia64_sal_oemcall_nolock(&rv, SN_SAL_IOIF_INTERRUPT,
254                                 SAL_INTR_ALLOC, nasid,
255                                 widget, sn_irq_info, req_irq,
256                                 req_nasid, req_slice);
257         return rv.status;
258 }
259
260 static inline void tiocx_intr_free(nasid_t nasid, int widget,
261                                    struct sn_irq_info *sn_irq_info)
262 {
263         struct ia64_sal_retval rv;
264         rv.status = 0;
265         rv.v0 = 0;
266
267         ia64_sal_oemcall_nolock(&rv, SN_SAL_IOIF_INTERRUPT,
268                                 SAL_INTR_FREE, nasid,
269                                 widget, sn_irq_info->irq_irq,
270                                 sn_irq_info->irq_cookie, 0, 0);
271 }
272
273 struct sn_irq_info *tiocx_irq_alloc(nasid_t nasid, int widget, int irq,
274                                     nasid_t req_nasid, int slice)
275 {
276         struct sn_irq_info *sn_irq_info;
277         int status;
278         int sn_irq_size = sizeof(struct sn_irq_info);
279
280         if ((nasid & 1) == 0)
281                 return NULL;
282
283         sn_irq_info = kmalloc(sn_irq_size, GFP_KERNEL);
284         if (sn_irq_info == NULL)
285                 return NULL;
286
287         memset(sn_irq_info, 0x0, sn_irq_size);
288
289         status = tiocx_intr_alloc(nasid, widget, __pa(sn_irq_info), irq,
290                                   req_nasid, slice);
291         if (status) {
292                 kfree(sn_irq_info);
293                 return NULL;
294         } else {
295                 return sn_irq_info;
296         }
297 }
298
299 void tiocx_irq_free(struct sn_irq_info *sn_irq_info)
300 {
301         uint64_t bridge = (uint64_t) sn_irq_info->irq_bridge;
302         nasid_t nasid = NASID_GET(bridge);
303         int widget;
304
305         if (nasid & 1) {
306                 widget = TIO_SWIN_WIDGETNUM(bridge);
307                 tiocx_intr_free(nasid, widget, sn_irq_info);
308                 kfree(sn_irq_info);
309         }
310 }
311
312 uint64_t tiocx_dma_addr(uint64_t addr)
313 {
314         return PHYS_TO_TIODMA(addr);
315 }
316
317 uint64_t tiocx_swin_base(int nasid)
318 {
319         return TIO_SWIN_BASE(nasid, TIOCX_CORELET);
320 }
321
322 EXPORT_SYMBOL(cx_driver_register);
323 EXPORT_SYMBOL(cx_driver_unregister);
324 EXPORT_SYMBOL(cx_device_register);
325 EXPORT_SYMBOL(cx_device_unregister);
326 EXPORT_SYMBOL(tiocx_irq_alloc);
327 EXPORT_SYMBOL(tiocx_irq_free);
328 EXPORT_SYMBOL(tiocx_bus_type);
329 EXPORT_SYMBOL(tiocx_dma_addr);
330 EXPORT_SYMBOL(tiocx_swin_base);
331
332 static void tio_conveyor_set(nasid_t nasid, int enable_flag)
333 {
334         uint64_t ice_frz;
335         uint64_t disable_cb = (1ull << 61);
336
337         if (!(nasid & 1))
338                 return;
339
340         ice_frz = REMOTE_HUB_L(nasid, TIO_ICE_FRZ_CFG);
341         if (enable_flag) {
342                 if (!(ice_frz & disable_cb))    /* already enabled */
343                         return;
344                 ice_frz &= ~disable_cb;
345         } else {
346                 if (ice_frz & disable_cb)       /* already disabled */
347                         return;
348                 ice_frz |= disable_cb;
349         }
350         DBG(KERN_ALERT "TIO_ICE_FRZ_CFG= 0x%lx\n", ice_frz);
351         REMOTE_HUB_S(nasid, TIO_ICE_FRZ_CFG, ice_frz);
352 }
353
354 #define tio_conveyor_enable(nasid) tio_conveyor_set(nasid, 1)
355 #define tio_conveyor_disable(nasid) tio_conveyor_set(nasid, 0)
356
357 static void tio_corelet_reset(nasid_t nasid, int corelet)
358 {
359         if (!(nasid & 1))
360                 return;
361
362         REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 1 << corelet);
363         udelay(2000);
364         REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 0);
365         udelay(2000);
366 }
367
368 static int tiocx_btchar_get(int nasid)
369 {
370         moduleid_t module_id;
371         geoid_t geoid;
372         int cnodeid;
373
374         cnodeid = nasid_to_cnodeid(nasid);
375         geoid = cnodeid_get_geoid(cnodeid);
376         module_id = geo_module(geoid);
377         return MODULE_GET_BTCHAR(module_id);
378 }
379
380 static int is_fpga_brick(int nasid)
381 {
382         switch (tiocx_btchar_get(nasid)) {
383         case L1_BRICKTYPE_SA:
384         case L1_BRICKTYPE_ATHENA:
385         case L1_BRICKTYPE_DAYTONA:
386                 return 1;
387         }
388         return 0;
389 }
390
391 static int bitstream_loaded(nasid_t nasid)
392 {
393         uint64_t cx_credits;
394
395         cx_credits = REMOTE_HUB_L(nasid, TIO_ICE_PMI_TX_DYN_CREDIT_STAT_CB3);
396         cx_credits &= TIO_ICE_PMI_TX_DYN_CREDIT_STAT_CB3_CREDIT_CNT_MASK;
397         DBG("cx_credits= 0x%lx\n", cx_credits);
398
399         return (cx_credits == 0xf) ? 1 : 0;
400 }
401
402 static int tiocx_reload(struct cx_dev *cx_dev)
403 {
404         int part_num = CX_DEV_NONE;
405         int mfg_num = CX_DEV_NONE;
406         nasid_t nasid = cx_dev->cx_id.nasid;
407
408         if (bitstream_loaded(nasid)) {
409                 uint64_t cx_id;
410
411                 cx_id =
412                     *(volatile uint64_t *)(TIO_SWIN_BASE(nasid, TIOCX_CORELET) +
413                                           WIDGET_ID);
414                 part_num = XWIDGET_PART_NUM(cx_id);
415                 mfg_num = XWIDGET_MFG_NUM(cx_id);
416                 DBG("part= 0x%x, mfg= 0x%x\n", part_num, mfg_num);
417                 /* just ignore it if it's a CE */
418                 if (part_num == TIO_CE_ASIC_PARTNUM)
419                         return 0;
420         }
421
422         cx_dev->cx_id.part_num = part_num;
423         cx_dev->cx_id.mfg_num = mfg_num;
424
425         /*
426          * Delete old device and register the new one.  It's ok if
427          * part_num/mfg_num == CX_DEV_NONE.  We want to register
428          * devices in the table even if a bitstream isn't loaded.
429          * That allows use to see that a bitstream isn't loaded via
430          * TIOCX_IOCTL_DEV_LIST.
431          */
432         return cx_device_reload(cx_dev);
433 }
434
435 static ssize_t show_cxdev_control(struct device *dev, struct device_attribute *attr, char *buf)
436 {
437         struct cx_dev *cx_dev = to_cx_dev(dev);
438
439         return sprintf(buf, "0x%x 0x%x 0x%x %d\n",
440                        cx_dev->cx_id.nasid,
441                        cx_dev->cx_id.part_num, cx_dev->cx_id.mfg_num,
442                        tiocx_btchar_get(cx_dev->cx_id.nasid));
443 }
444
445 static ssize_t store_cxdev_control(struct device *dev, struct device_attribute *attr, const char *buf,
446                                    size_t count)
447 {
448         int n;
449         struct cx_dev *cx_dev = to_cx_dev(dev);
450
451         if (!capable(CAP_SYS_ADMIN))
452                 return -EPERM;
453
454         if (count <= 0)
455                 return 0;
456
457         n = simple_strtoul(buf, NULL, 0);
458
459         switch (n) {
460         case 1:
461                 tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET);
462                 tiocx_reload(cx_dev);
463                 break;
464         case 2:
465                 tiocx_reload(cx_dev);
466                 break;
467         case 3:
468                 tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET);
469                 break;
470         default:
471                 break;
472         }
473
474         return count;
475 }
476
477 DEVICE_ATTR(cxdev_control, 0644, show_cxdev_control, store_cxdev_control);
478
479 static int __init tiocx_init(void)
480 {
481         cnodeid_t cnodeid;
482         int found_tiocx_device = 0;
483
484         if (!ia64_platform_is("sn2"))
485                 return -ENODEV;
486
487         bus_register(&tiocx_bus_type);
488
489         for (cnodeid = 0; cnodeid < MAX_COMPACT_NODES; cnodeid++) {
490                 nasid_t nasid;
491
492                 if ((nasid = cnodeid_to_nasid(cnodeid)) < 0)
493                         break;  /* No more nasids .. bail out of loop */
494
495                 if ((nasid & 0x1) && is_fpga_brick(nasid)) {
496                         struct hubdev_info *hubdev;
497                         struct xwidget_info *widgetp;
498
499                         DBG("Found TIO at nasid 0x%x\n", nasid);
500
501                         hubdev =
502                             (struct hubdev_info *)(NODEPDA(cnodeid)->pdinfo);
503
504                         widgetp = &hubdev->hdi_xwidget_info[TIOCX_CORELET];
505
506                         /* The CE hangs off of the CX port but is not an FPGA */
507                         if (widgetp->xwi_hwid.part_num == TIO_CE_ASIC_PARTNUM)
508                                 continue;
509
510                         tio_corelet_reset(nasid, TIOCX_CORELET);
511                         tio_conveyor_enable(nasid);
512
513                         if (cx_device_register
514                             (nasid, widgetp->xwi_hwid.part_num,
515                              widgetp->xwi_hwid.mfg_num, hubdev) < 0)
516                                 return -ENXIO;
517                         else
518                                 found_tiocx_device++;
519                 }
520         }
521
522         /* It's ok if we find zero devices. */
523         DBG("found_tiocx_device= %d\n", found_tiocx_device);
524
525         return 0;
526 }
527
528 static int cx_remove_device(struct device * dev, void * data)
529 {
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         return 0;
534 }
535
536 static void __exit tiocx_exit(void)
537 {
538         DBG("tiocx_exit\n");
539
540         /*
541          * Unregister devices.
542          */
543         bus_for_each_dev(&tiocx_bus_type, NULL, NULL, cx_remove_device);
544         bus_unregister(&tiocx_bus_type);
545 }
546
547 subsys_initcall(tiocx_init);
548 module_exit(tiocx_exit);
549
550 /************************************************************************
551  * Module licensing and description
552  ************************************************************************/
553 MODULE_LICENSE("GPL");
554 MODULE_AUTHOR("Bruce Losure <blosure@sgi.com>");
555 MODULE_DESCRIPTION("TIOCX module");
556 MODULE_SUPPORTED_DEVICE(DEVICE_NAME);