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