tiocx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 = kzalloc(sn_irq_size, GFP_KERNEL);
  253. if (sn_irq_info == NULL)
  254. return NULL;
  255. status = tiocx_intr_alloc(nasid, widget, __pa(sn_irq_info), irq,
  256. req_nasid, slice);
  257. if (status) {
  258. kfree(sn_irq_info);
  259. return NULL;
  260. } else {
  261. return sn_irq_info;
  262. }
  263. }
  264. void tiocx_irq_free(struct sn_irq_info *sn_irq_info)
  265. {
  266. u64 bridge = (u64) sn_irq_info->irq_bridge;
  267. nasid_t nasid = NASID_GET(bridge);
  268. int widget;
  269. if (nasid & 1) {
  270. widget = TIO_SWIN_WIDGETNUM(bridge);
  271. tiocx_intr_free(nasid, widget, sn_irq_info);
  272. kfree(sn_irq_info);
  273. }
  274. }
  275. u64 tiocx_dma_addr(u64 addr)
  276. {
  277. return PHYS_TO_TIODMA(addr);
  278. }
  279. u64 tiocx_swin_base(int nasid)
  280. {
  281. return TIO_SWIN_BASE(nasid, TIOCX_CORELET);
  282. }
  283. EXPORT_SYMBOL(cx_driver_register);
  284. EXPORT_SYMBOL(cx_driver_unregister);
  285. EXPORT_SYMBOL(cx_device_register);
  286. EXPORT_SYMBOL(cx_device_unregister);
  287. EXPORT_SYMBOL(tiocx_irq_alloc);
  288. EXPORT_SYMBOL(tiocx_irq_free);
  289. EXPORT_SYMBOL(tiocx_bus_type);
  290. EXPORT_SYMBOL(tiocx_dma_addr);
  291. EXPORT_SYMBOL(tiocx_swin_base);
  292. static void tio_conveyor_set(nasid_t nasid, int enable_flag)
  293. {
  294. u64 ice_frz;
  295. u64 disable_cb = (1ull << 61);
  296. if (!(nasid & 1))
  297. return;
  298. ice_frz = REMOTE_HUB_L(nasid, TIO_ICE_FRZ_CFG);
  299. if (enable_flag) {
  300. if (!(ice_frz & disable_cb)) /* already enabled */
  301. return;
  302. ice_frz &= ~disable_cb;
  303. } else {
  304. if (ice_frz & disable_cb) /* already disabled */
  305. return;
  306. ice_frz |= disable_cb;
  307. }
  308. DBG(KERN_ALERT "TIO_ICE_FRZ_CFG= 0x%lx\n", ice_frz);
  309. REMOTE_HUB_S(nasid, TIO_ICE_FRZ_CFG, ice_frz);
  310. }
  311. #define tio_conveyor_enable(nasid) tio_conveyor_set(nasid, 1)
  312. #define tio_conveyor_disable(nasid) tio_conveyor_set(nasid, 0)
  313. static void tio_corelet_reset(nasid_t nasid, int corelet)
  314. {
  315. if (!(nasid & 1))
  316. return;
  317. REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 1 << corelet);
  318. udelay(2000);
  319. REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 0);
  320. udelay(2000);
  321. }
  322. static int is_fpga_tio(int nasid, int *bt)
  323. {
  324. u16 ioboard_type;
  325. s64 rc;
  326. rc = ia64_sn_sysctl_ioboard_get(nasid, &ioboard_type);
  327. if (rc) {
  328. printk(KERN_WARNING "ia64_sn_sysctl_ioboard_get failed: %ld\n",
  329. rc);
  330. return 0;
  331. }
  332. switch (ioboard_type) {
  333. case L1_BRICKTYPE_SA:
  334. case L1_BRICKTYPE_ATHENA:
  335. case L1_BOARDTYPE_DAYTONA:
  336. *bt = ioboard_type;
  337. return 1;
  338. }
  339. return 0;
  340. }
  341. static int bitstream_loaded(nasid_t nasid)
  342. {
  343. u64 cx_credits;
  344. cx_credits = REMOTE_HUB_L(nasid, TIO_ICE_PMI_TX_DYN_CREDIT_STAT_CB3);
  345. cx_credits &= TIO_ICE_PMI_TX_DYN_CREDIT_STAT_CB3_CREDIT_CNT_MASK;
  346. DBG("cx_credits= 0x%lx\n", cx_credits);
  347. return (cx_credits == 0xf) ? 1 : 0;
  348. }
  349. static int tiocx_reload(struct cx_dev *cx_dev)
  350. {
  351. int part_num = CX_DEV_NONE;
  352. int mfg_num = CX_DEV_NONE;
  353. nasid_t nasid = cx_dev->cx_id.nasid;
  354. if (bitstream_loaded(nasid)) {
  355. u64 cx_id;
  356. int rv;
  357. rv = ia64_sn_sysctl_tio_clock_reset(nasid);
  358. if (rv) {
  359. printk(KERN_ALERT "CX port JTAG reset failed.\n");
  360. } else {
  361. cx_id = *(volatile u64 *)
  362. (TIO_SWIN_BASE(nasid, TIOCX_CORELET) +
  363. WIDGET_ID);
  364. part_num = XWIDGET_PART_NUM(cx_id);
  365. mfg_num = XWIDGET_MFG_NUM(cx_id);
  366. DBG("part= 0x%x, mfg= 0x%x\n", part_num, mfg_num);
  367. /* just ignore it if it's a CE */
  368. if (part_num == TIO_CE_ASIC_PARTNUM)
  369. return 0;
  370. }
  371. }
  372. cx_dev->cx_id.part_num = part_num;
  373. cx_dev->cx_id.mfg_num = mfg_num;
  374. /*
  375. * Delete old device and register the new one. It's ok if
  376. * part_num/mfg_num == CX_DEV_NONE. We want to register
  377. * devices in the table even if a bitstream isn't loaded.
  378. * That allows use to see that a bitstream isn't loaded via
  379. * TIOCX_IOCTL_DEV_LIST.
  380. */
  381. return cx_device_reload(cx_dev);
  382. }
  383. static ssize_t show_cxdev_control(struct device *dev, struct device_attribute *attr, char *buf)
  384. {
  385. struct cx_dev *cx_dev = to_cx_dev(dev);
  386. return sprintf(buf, "0x%x 0x%x 0x%x 0x%x\n",
  387. cx_dev->cx_id.nasid,
  388. cx_dev->cx_id.part_num, cx_dev->cx_id.mfg_num,
  389. cx_dev->bt);
  390. }
  391. static ssize_t store_cxdev_control(struct device *dev, struct device_attribute *attr, const char *buf,
  392. size_t count)
  393. {
  394. int n;
  395. struct cx_dev *cx_dev = to_cx_dev(dev);
  396. if (!capable(CAP_SYS_ADMIN))
  397. return -EPERM;
  398. if (count <= 0)
  399. return 0;
  400. n = simple_strtoul(buf, NULL, 0);
  401. switch (n) {
  402. case 1:
  403. tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET);
  404. tiocx_reload(cx_dev);
  405. break;
  406. case 2:
  407. tiocx_reload(cx_dev);
  408. break;
  409. case 3:
  410. tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET);
  411. break;
  412. default:
  413. break;
  414. }
  415. return count;
  416. }
  417. DEVICE_ATTR(cxdev_control, 0644, show_cxdev_control, store_cxdev_control);
  418. static int __init tiocx_init(void)
  419. {
  420. cnodeid_t cnodeid;
  421. int found_tiocx_device = 0;
  422. if (!ia64_platform_is("sn2"))
  423. return 0;
  424. bus_register(&tiocx_bus_type);
  425. for (cnodeid = 0; cnodeid < num_cnodes; cnodeid++) {
  426. nasid_t nasid;
  427. int bt;
  428. nasid = cnodeid_to_nasid(cnodeid);
  429. if ((nasid & 0x1) && is_fpga_tio(nasid, &bt)) {
  430. struct hubdev_info *hubdev;
  431. struct xwidget_info *widgetp;
  432. DBG("Found TIO at nasid 0x%x\n", nasid);
  433. hubdev =
  434. (struct hubdev_info *)(NODEPDA(cnodeid)->pdinfo);
  435. widgetp = &hubdev->hdi_xwidget_info[TIOCX_CORELET];
  436. /* The CE hangs off of the CX port but is not an FPGA */
  437. if (widgetp->xwi_hwid.part_num == TIO_CE_ASIC_PARTNUM)
  438. continue;
  439. tio_corelet_reset(nasid, TIOCX_CORELET);
  440. tio_conveyor_enable(nasid);
  441. if (cx_device_register
  442. (nasid, widgetp->xwi_hwid.part_num,
  443. widgetp->xwi_hwid.mfg_num, hubdev, bt) < 0)
  444. return -ENXIO;
  445. else
  446. found_tiocx_device++;
  447. }
  448. }
  449. /* It's ok if we find zero devices. */
  450. DBG("found_tiocx_device= %d\n", found_tiocx_device);
  451. return 0;
  452. }
  453. static int cx_remove_device(struct device * dev, void * data)
  454. {
  455. struct cx_dev *cx_dev = to_cx_dev(dev);
  456. device_remove_file(dev, &dev_attr_cxdev_control);
  457. cx_device_unregister(cx_dev);
  458. return 0;
  459. }
  460. static void __exit tiocx_exit(void)
  461. {
  462. DBG("tiocx_exit\n");
  463. /*
  464. * Unregister devices.
  465. */
  466. bus_for_each_dev(&tiocx_bus_type, NULL, NULL, cx_remove_device);
  467. bus_unregister(&tiocx_bus_type);
  468. }
  469. subsys_initcall(tiocx_init);
  470. module_exit(tiocx_exit);
  471. /************************************************************************
  472. * Module licensing and description
  473. ************************************************************************/
  474. MODULE_LICENSE("GPL");
  475. MODULE_AUTHOR("Bruce Losure <blosure@sgi.com>");
  476. MODULE_DESCRIPTION("TIOCX module");
  477. MODULE_SUPPORTED_DEVICE(DEVICE_NAME);