tiocx.c 13 KB

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