pci-ip27.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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) 2003 Christoph Hellwig (hch@lst.de)
  7. * Copyright (C) 1999, 2000, 04 Ralf Baechle (ralf@linux-mips.org)
  8. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/pci.h>
  13. #include <asm/sn/arch.h>
  14. #include <asm/pci/bridge.h>
  15. #include <asm/paccess.h>
  16. #include <asm/sn/intr.h>
  17. #include <asm/sn/sn0/hub.h>
  18. extern unsigned int allocate_irqno(void);
  19. /*
  20. * Max #PCI busses we can handle; ie, max #PCI bridges.
  21. */
  22. #define MAX_PCI_BUSSES 40
  23. /*
  24. * Max #PCI devices (like scsi controllers) we handle on a bus.
  25. */
  26. #define MAX_DEVICES_PER_PCIBUS 8
  27. /*
  28. * XXX: No kmalloc available when we do our crosstalk scan,
  29. * we should try to move it later in the boot process.
  30. */
  31. static struct bridge_controller bridges[MAX_PCI_BUSSES];
  32. /*
  33. * Translate from irq to software PCI bus number and PCI slot.
  34. */
  35. struct bridge_controller *irq_to_bridge[MAX_PCI_BUSSES * MAX_DEVICES_PER_PCIBUS];
  36. int irq_to_slot[MAX_PCI_BUSSES * MAX_DEVICES_PER_PCIBUS];
  37. /*
  38. * The Bridge ASIC supports both type 0 and type 1 access. Type 1 is
  39. * not really documented, so right now I can't write code which uses it.
  40. * Therefore we use type 0 accesses for now even though they won't work
  41. * correcly for PCI-to-PCI bridges.
  42. *
  43. * The function is complicated by the ultimate brokeness of the IOC3 chip
  44. * which is used in SGI systems. The IOC3 can only handle 32-bit PCI
  45. * accesses and does only decode parts of it's address space.
  46. */
  47. static int pci_conf0_read_config(struct pci_bus *bus, unsigned int devfn,
  48. int where, int size, u32 * value)
  49. {
  50. struct bridge_controller *bc = BRIDGE_CONTROLLER(bus);
  51. bridge_t *bridge = bc->base;
  52. int slot = PCI_SLOT(devfn);
  53. int fn = PCI_FUNC(devfn);
  54. volatile void *addr;
  55. u32 cf, shift, mask;
  56. int res;
  57. addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[PCI_VENDOR_ID];
  58. if (get_dbe(cf, (u32 *) addr))
  59. return PCIBIOS_DEVICE_NOT_FOUND;
  60. /*
  61. * IOC3 is fucked fucked beyond believe ... Don't even give the
  62. * generic PCI code a chance to look at it for real ...
  63. */
  64. if (cf == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16)))
  65. goto oh_my_gawd;
  66. addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[where ^ (4 - size)];
  67. if (size == 1)
  68. res = get_dbe(*value, (u8 *) addr);
  69. else if (size == 2)
  70. res = get_dbe(*value, (u16 *) addr);
  71. else
  72. res = get_dbe(*value, (u32 *) addr);
  73. return res ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
  74. oh_my_gawd:
  75. /*
  76. * IOC3 is fucked fucked beyond believe ... Don't even give the
  77. * generic PCI code a chance to look at the wrong register.
  78. */
  79. if ((where >= 0x14 && where < 0x40) || (where >= 0x48)) {
  80. *value = 0;
  81. return PCIBIOS_SUCCESSFUL;
  82. }
  83. /*
  84. * IOC3 is fucked fucked beyond believe ... Don't try to access
  85. * anything but 32-bit words ...
  86. */
  87. addr = &bridge->b_type0_cfg_dev[slot].f[fn].l[where >> 2];
  88. if (get_dbe(cf, (u32 *) addr))
  89. return PCIBIOS_DEVICE_NOT_FOUND;
  90. shift = ((where & 3) << 3);
  91. mask = (0xffffffffU >> ((4 - size) << 3));
  92. *value = (cf >> shift) & mask;
  93. return PCIBIOS_SUCCESSFUL;
  94. }
  95. static int pci_conf1_read_config(struct pci_bus *bus, unsigned int devfn,
  96. int where, int size, u32 * value)
  97. {
  98. struct bridge_controller *bc = BRIDGE_CONTROLLER(bus);
  99. bridge_t *bridge = bc->base;
  100. int busno = bus->number;
  101. int slot = PCI_SLOT(devfn);
  102. int fn = PCI_FUNC(devfn);
  103. volatile void *addr;
  104. u32 cf, shift, mask;
  105. int res;
  106. bridge->b_pci_cfg = (busno << 16) | (slot << 11);
  107. addr = &bridge->b_type1_cfg.c[(fn << 8) | PCI_VENDOR_ID];
  108. if (get_dbe(cf, (u32 *) addr))
  109. return PCIBIOS_DEVICE_NOT_FOUND;
  110. /*
  111. * IOC3 is fucked fucked beyond believe ... Don't even give the
  112. * generic PCI code a chance to look at it for real ...
  113. */
  114. if (cf == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16)))
  115. goto oh_my_gawd;
  116. bridge->b_pci_cfg = (busno << 16) | (slot << 11);
  117. addr = &bridge->b_type1_cfg.c[(fn << 8) | (where ^ (4 - size))];
  118. if (size == 1)
  119. res = get_dbe(*value, (u8 *) addr);
  120. else if (size == 2)
  121. res = get_dbe(*value, (u16 *) addr);
  122. else
  123. res = get_dbe(*value, (u32 *) addr);
  124. return res ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
  125. oh_my_gawd:
  126. /*
  127. * IOC3 is fucked fucked beyond believe ... Don't even give the
  128. * generic PCI code a chance to look at the wrong register.
  129. */
  130. if ((where >= 0x14 && where < 0x40) || (where >= 0x48)) {
  131. *value = 0;
  132. return PCIBIOS_SUCCESSFUL;
  133. }
  134. /*
  135. * IOC3 is fucked fucked beyond believe ... Don't try to access
  136. * anything but 32-bit words ...
  137. */
  138. bridge->b_pci_cfg = (busno << 16) | (slot << 11);
  139. addr = &bridge->b_type1_cfg.c[(fn << 8) | where];
  140. if (get_dbe(cf, (u32 *) addr))
  141. return PCIBIOS_DEVICE_NOT_FOUND;
  142. shift = ((where & 3) << 3);
  143. mask = (0xffffffffU >> ((4 - size) << 3));
  144. *value = (cf >> shift) & mask;
  145. return PCIBIOS_SUCCESSFUL;
  146. }
  147. static int pci_read_config(struct pci_bus *bus, unsigned int devfn,
  148. int where, int size, u32 * value)
  149. {
  150. if (bus->number > 0)
  151. return pci_conf1_read_config(bus, devfn, where, size, value);
  152. return pci_conf0_read_config(bus, devfn, where, size, value);
  153. }
  154. static int pci_conf0_write_config(struct pci_bus *bus, unsigned int devfn,
  155. int where, int size, u32 value)
  156. {
  157. struct bridge_controller *bc = BRIDGE_CONTROLLER(bus);
  158. bridge_t *bridge = bc->base;
  159. int slot = PCI_SLOT(devfn);
  160. int fn = PCI_FUNC(devfn);
  161. volatile void *addr;
  162. u32 cf, shift, mask, smask;
  163. int res;
  164. addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[PCI_VENDOR_ID];
  165. if (get_dbe(cf, (u32 *) addr))
  166. return PCIBIOS_DEVICE_NOT_FOUND;
  167. /*
  168. * IOC3 is fucked fucked beyond believe ... Don't even give the
  169. * generic PCI code a chance to look at it for real ...
  170. */
  171. if (cf == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16)))
  172. goto oh_my_gawd;
  173. addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[where ^ (4 - size)];
  174. if (size == 1) {
  175. res = put_dbe(value, (u8 *) addr);
  176. } else if (size == 2) {
  177. res = put_dbe(value, (u16 *) addr);
  178. } else {
  179. res = put_dbe(value, (u32 *) addr);
  180. }
  181. if (res)
  182. return PCIBIOS_DEVICE_NOT_FOUND;
  183. return PCIBIOS_SUCCESSFUL;
  184. oh_my_gawd:
  185. /*
  186. * IOC3 is fucked fucked beyond believe ... Don't even give the
  187. * generic PCI code a chance to touch the wrong register.
  188. */
  189. if ((where >= 0x14 && where < 0x40) || (where >= 0x48))
  190. return PCIBIOS_SUCCESSFUL;
  191. /*
  192. * IOC3 is fucked fucked beyond believe ... Don't try to access
  193. * anything but 32-bit words ...
  194. */
  195. addr = &bridge->b_type0_cfg_dev[slot].f[fn].l[where >> 2];
  196. if (get_dbe(cf, (u32 *) addr))
  197. return PCIBIOS_DEVICE_NOT_FOUND;
  198. shift = ((where & 3) << 3);
  199. mask = (0xffffffffU >> ((4 - size) << 3));
  200. smask = mask << shift;
  201. cf = (cf & ~smask) | ((value & mask) << shift);
  202. if (put_dbe(cf, (u32 *) addr))
  203. return PCIBIOS_DEVICE_NOT_FOUND;
  204. return PCIBIOS_SUCCESSFUL;
  205. }
  206. static int pci_conf1_write_config(struct pci_bus *bus, unsigned int devfn,
  207. int where, int size, u32 value)
  208. {
  209. struct bridge_controller *bc = BRIDGE_CONTROLLER(bus);
  210. bridge_t *bridge = bc->base;
  211. int slot = PCI_SLOT(devfn);
  212. int fn = PCI_FUNC(devfn);
  213. int busno = bus->number;
  214. volatile void *addr;
  215. u32 cf, shift, mask, smask;
  216. int res;
  217. bridge->b_pci_cfg = (busno << 16) | (slot << 11);
  218. addr = &bridge->b_type1_cfg.c[(fn << 8) | PCI_VENDOR_ID];
  219. if (get_dbe(cf, (u32 *) addr))
  220. return PCIBIOS_DEVICE_NOT_FOUND;
  221. /*
  222. * IOC3 is fucked fucked beyond believe ... Don't even give the
  223. * generic PCI code a chance to look at it for real ...
  224. */
  225. if (cf == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16)))
  226. goto oh_my_gawd;
  227. addr = &bridge->b_type1_cfg.c[(fn << 8) | (where ^ (4 - size))];
  228. if (size == 1) {
  229. res = put_dbe(value, (u8 *) addr);
  230. } else if (size == 2) {
  231. res = put_dbe(value, (u16 *) addr);
  232. } else {
  233. res = put_dbe(value, (u32 *) addr);
  234. }
  235. if (res)
  236. return PCIBIOS_DEVICE_NOT_FOUND;
  237. return PCIBIOS_SUCCESSFUL;
  238. oh_my_gawd:
  239. /*
  240. * IOC3 is fucked fucked beyond believe ... Don't even give the
  241. * generic PCI code a chance to touch the wrong register.
  242. */
  243. if ((where >= 0x14 && where < 0x40) || (where >= 0x48))
  244. return PCIBIOS_SUCCESSFUL;
  245. /*
  246. * IOC3 is fucked fucked beyond believe ... Don't try to access
  247. * anything but 32-bit words ...
  248. */
  249. addr = &bridge->b_type0_cfg_dev[slot].f[fn].l[where >> 2];
  250. if (get_dbe(cf, (u32 *) addr))
  251. return PCIBIOS_DEVICE_NOT_FOUND;
  252. shift = ((where & 3) << 3);
  253. mask = (0xffffffffU >> ((4 - size) << 3));
  254. smask = mask << shift;
  255. cf = (cf & ~smask) | ((value & mask) << shift);
  256. if (put_dbe(cf, (u32 *) addr))
  257. return PCIBIOS_DEVICE_NOT_FOUND;
  258. return PCIBIOS_SUCCESSFUL;
  259. }
  260. static int pci_write_config(struct pci_bus *bus, unsigned int devfn,
  261. int where, int size, u32 value)
  262. {
  263. if (bus->number > 0)
  264. return pci_conf1_write_config(bus, devfn, where, size, value);
  265. return pci_conf0_write_config(bus, devfn, where, size, value);
  266. }
  267. static struct pci_ops bridge_pci_ops = {
  268. .read = pci_read_config,
  269. .write = pci_write_config,
  270. };
  271. int __init bridge_probe(nasid_t nasid, int widget_id, int masterwid)
  272. {
  273. unsigned long offset = NODE_OFFSET(nasid);
  274. struct bridge_controller *bc;
  275. static int num_bridges = 0;
  276. bridge_t *bridge;
  277. int slot;
  278. printk("a bridge\n");
  279. /* XXX: kludge alert.. */
  280. if (!num_bridges)
  281. ioport_resource.end = ~0UL;
  282. bc = &bridges[num_bridges];
  283. bc->pc.pci_ops = &bridge_pci_ops;
  284. bc->pc.mem_resource = &bc->mem;
  285. bc->pc.io_resource = &bc->io;
  286. bc->pc.index = num_bridges;
  287. bc->mem.name = "Bridge PCI MEM";
  288. bc->pc.mem_offset = offset;
  289. bc->mem.start = 0;
  290. bc->mem.end = ~0UL;
  291. bc->mem.flags = IORESOURCE_MEM;
  292. bc->io.name = "Bridge IO MEM";
  293. bc->pc.io_offset = offset;
  294. bc->io.start = 0UL;
  295. bc->io.end = ~0UL;
  296. bc->io.flags = IORESOURCE_IO;
  297. bc->irq_cpu = smp_processor_id();
  298. bc->widget_id = widget_id;
  299. bc->nasid = nasid;
  300. bc->baddr = (u64)masterwid << 60;
  301. bc->baddr |= (1UL << 56); /* Barrier set */
  302. /*
  303. * point to this bridge
  304. */
  305. bridge = (bridge_t *) RAW_NODE_SWIN_BASE(nasid, widget_id);
  306. /*
  307. * Clear all pending interrupts.
  308. */
  309. bridge->b_int_rst_stat = BRIDGE_IRR_ALL_CLR;
  310. /*
  311. * Until otherwise set up, assume all interrupts are from slot 0
  312. */
  313. bridge->b_int_device = 0x0;
  314. /*
  315. * swap pio's to pci mem and io space (big windows)
  316. */
  317. bridge->b_wid_control |= BRIDGE_CTRL_IO_SWAP |
  318. BRIDGE_CTRL_MEM_SWAP;
  319. /*
  320. * Hmm... IRIX sets additional bits in the address which
  321. * are documented as reserved in the bridge docs.
  322. */
  323. bridge->b_wid_int_upper = 0x8000 | (masterwid << 16);
  324. bridge->b_wid_int_lower = 0x01800090; /* PI_INT_PEND_MOD off*/
  325. bridge->b_dir_map = (masterwid << 20); /* DMA */
  326. bridge->b_int_enable = 0;
  327. for (slot = 0; slot < 8; slot ++) {
  328. bridge->b_device[slot].reg |= BRIDGE_DEV_SWAP_DIR;
  329. bc->pci_int[slot] = -1;
  330. }
  331. bridge->b_wid_tflush; /* wait until Bridge PIO complete */
  332. bc->base = bridge;
  333. register_pci_controller(&bc->pc);
  334. num_bridges++;
  335. return 0;
  336. }
  337. /*
  338. * All observed requests have pin == 1. We could have a global here, that
  339. * gets incremented and returned every time - unfortunately, pci_map_irq
  340. * may be called on the same device over and over, and need to return the
  341. * same value. On O2000, pin can be 0 or 1, and PCI slots can be [0..7].
  342. *
  343. * A given PCI device, in general, should be able to intr any of the cpus
  344. * on any one of the hubs connected to its xbow.
  345. */
  346. int __devinit pcibios_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
  347. {
  348. struct bridge_controller *bc = BRIDGE_CONTROLLER(dev->bus);
  349. int irq = bc->pci_int[slot];
  350. if (irq == -1) {
  351. irq = bc->pci_int[slot] = request_bridge_irq(bc);
  352. if (irq < 0)
  353. panic("Can't allocate interrupt for PCI device %s\n",
  354. pci_name(dev));
  355. }
  356. irq_to_bridge[irq] = bc;
  357. irq_to_slot[irq] = slot;
  358. return irq;
  359. }
  360. /* Do platform specific device initialization at pci_enable_device() time */
  361. int pcibios_plat_dev_init(struct pci_dev *dev)
  362. {
  363. return 0;
  364. }
  365. /*
  366. * Device might live on a subordinate PCI bus. XXX Walk up the chain of buses
  367. * to find the slot number in sense of the bridge device register.
  368. * XXX This also means multiple devices might rely on conflicting bridge
  369. * settings.
  370. */
  371. static inline void pci_disable_swapping(struct pci_dev *dev)
  372. {
  373. struct bridge_controller *bc = BRIDGE_CONTROLLER(dev->bus);
  374. bridge_t *bridge = bc->base;
  375. int slot = PCI_SLOT(dev->devfn);
  376. /* Turn off byte swapping */
  377. bridge->b_device[slot].reg &= ~BRIDGE_DEV_SWAP_DIR;
  378. bridge->b_widget.w_tflush; /* Flush */
  379. }
  380. static inline void pci_enable_swapping(struct pci_dev *dev)
  381. {
  382. struct bridge_controller *bc = BRIDGE_CONTROLLER(dev->bus);
  383. bridge_t *bridge = bc->base;
  384. int slot = PCI_SLOT(dev->devfn);
  385. /* Turn on byte swapping */
  386. bridge->b_device[slot].reg |= BRIDGE_DEV_SWAP_DIR;
  387. bridge->b_widget.w_tflush; /* Flush */
  388. }
  389. static void __init pci_fixup_ioc3(struct pci_dev *d)
  390. {
  391. pci_disable_swapping(d);
  392. }
  393. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3,
  394. pci_fixup_ioc3);