PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/ia64/sn/kernel/tiocx.c

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